1716fd348SMartin Matuska /*
2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
3716fd348SMartin Matuska *
4716fd348SMartin Matuska * Copyright (c) 2021 iXsystems, Inc.
5716fd348SMartin Matuska *
6716fd348SMartin Matuska * Redistribution and use in source and binary forms, with or without
7716fd348SMartin Matuska * modification, are permitted provided that the following conditions
8716fd348SMartin Matuska * are met:
9716fd348SMartin Matuska * 1. Redistributions of source code must retain the above copyright
10716fd348SMartin Matuska * notice, this list of conditions and the following disclaimer.
11716fd348SMartin Matuska * 2. Redistributions in binary form must reproduce the above copyright
12716fd348SMartin Matuska * notice, this list of conditions and the following disclaimer in the
13716fd348SMartin Matuska * documentation and/or other materials provided with the distribution.
14716fd348SMartin Matuska *
15716fd348SMartin Matuska * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16716fd348SMartin Matuska * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17716fd348SMartin Matuska * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18716fd348SMartin Matuska * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19716fd348SMartin Matuska * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20716fd348SMartin Matuska * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21716fd348SMartin Matuska * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22716fd348SMartin Matuska * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23716fd348SMartin Matuska * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24716fd348SMartin Matuska * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25716fd348SMartin Matuska * SUCH DAMAGE.
26716fd348SMartin Matuska *
27716fd348SMartin Matuska * Test for correct behavior of DOS mode READONLY flag on a file.
28716fd348SMartin Matuska * We should be able to open a file RW, set READONLY, and still write to the fd.
29716fd348SMartin Matuska */
30716fd348SMartin Matuska
31716fd348SMartin Matuska #include <sys/stat.h>
32716fd348SMartin Matuska #include <err.h>
33716fd348SMartin Matuska #include <errno.h>
34716fd348SMartin Matuska #include <fcntl.h>
35716fd348SMartin Matuska #include <stdio.h>
36716fd348SMartin Matuska #include <string.h>
37716fd348SMartin Matuska #include <unistd.h>
38716fd348SMartin Matuska
39716fd348SMartin Matuska #ifdef __linux__
40716fd348SMartin Matuska #include <stdint.h>
41716fd348SMartin Matuska #include <sys/fs/zfs.h>
42716fd348SMartin Matuska #endif
43716fd348SMartin Matuska
44716fd348SMartin Matuska int
main(int argc,const char * argv[])45716fd348SMartin Matuska main(int argc, const char *argv[])
46716fd348SMartin Matuska {
47716fd348SMartin Matuska const char *buf = "We should be allowed to write this to the fd.\n";
48716fd348SMartin Matuska const char *path;
49716fd348SMartin Matuska int fd;
50716fd348SMartin Matuska
51716fd348SMartin Matuska if (argc != 2) {
52716fd348SMartin Matuska fprintf(stderr, "usage: %s PATH\n", argv[0]);
53716fd348SMartin Matuska return (EXIT_FAILURE);
54716fd348SMartin Matuska }
55716fd348SMartin Matuska path = argv[1];
56716fd348SMartin Matuska fd = open(path, O_CREAT|O_RDWR, 0777);
57716fd348SMartin Matuska if (fd == -1)
58716fd348SMartin Matuska err(EXIT_FAILURE, "%s: open failed", path);
59716fd348SMartin Matuska #ifdef __linux__
60716fd348SMartin Matuska uint64_t dosflags = ZFS_READONLY;
61716fd348SMartin Matuska if (ioctl(fd, ZFS_IOC_SETDOSFLAGS, &dosflags) == -1)
62716fd348SMartin Matuska err(EXIT_FAILURE, "%s: ZFS_IOC_SETDOSFLAGS failed", path);
63716fd348SMartin Matuska #else
64716fd348SMartin Matuska if (chflags(path, UF_READONLY) == -1)
65716fd348SMartin Matuska err(EXIT_FAILURE, "%s: chflags failed", path);
66716fd348SMartin Matuska #endif
67716fd348SMartin Matuska if (write(fd, buf, strlen(buf)) == -1)
68716fd348SMartin Matuska err(EXIT_FAILURE, "%s: write failed", path);
69716fd348SMartin Matuska if (close(fd) == -1)
70716fd348SMartin Matuska err(EXIT_FAILURE, "%s: close failed", path);
71716fd348SMartin Matuska return (EXIT_SUCCESS);
72716fd348SMartin Matuska }
73