summaryrefslogtreecommitdiff
path: root/usr/src/test/zfs-tests/cmd/dos_ro/dos_ro.c
blob: 4f0510f72bd44a6391094d02b8f0566d22a94b2e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
 * This file and its contents are supplied under the terms of the
 * Common Development and Distribution License ("CDDL"), version 1.0.
 * You may only use this file in accordance with the terms of version
 * 1.0 of the CDDL.
 *
 * A full copy of the text of the CDDL should have accompanied this
 * source.  A copy of the CDDL is also available via the Internet at
 * http://www.illumos.org/license/CDDL.
 */

/*
 * Copyright 2017 Nexenta Systems, Inc.  All rights reserved.
 */

#include <sys/types.h>
#include <sys/stat.h>

#include <attr.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <libnvpair.h>

extern const char *__progname;

int vflag = 0;

static int
dosattr_set_ro(int fildes, const char *fname)
{
	nvlist_t	*nvl = NULL;
	int		err;

	err = nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0);
	if (err != 0)
		return (err);

	(void) nvlist_add_boolean_value(nvl, A_READONLY, 1);

	if (fname == NULL) {
		err = fsetattr(fildes, XATTR_VIEW_READWRITE, nvl);
	} else {
		err = setattrat(fildes, XATTR_VIEW_READWRITE, fname, nvl);
	}
	if (err < 0) {
		err = errno;
		if (vflag > 1) {
			(void) fprintf(stderr,
			    "dosattr_set: setattrat (%s), err %d\n",
			    fname, err);
		}
	}

	nvlist_free(nvl);

	return (err);
}

void
usage(void)
{
	(void) fprintf(stderr, "usage: %s [-v] file\n",
	    __progname);
	exit(1);
}

int
main(int argc, char **argv)
{
	char *fname;
	int c, fd, n;

	while ((c = getopt(argc, argv, "v")) != -1) {
		switch (c) {
		case 'v':
			vflag++;
			break;
		case '?':
		default:
			usage();
			break;
		}
	}

	if (optind + 1 != argc)
		usage();
	fname = argv[optind];

	fd = open(fname, O_CREAT | O_RDWR, 0644);
	if (fd < 0) {
		perror(fname);
		exit(1);
	}

	if (vflag)
		(void) fprintf(stderr, "Write 1 (mode 644)\n");
	n = write(fd, "mode 644 OK\n", 12);
	if (n != 12) {
		(void) fprintf(stderr, "write mode 644, err=%d\n", errno);
		exit(1);
	}

	if (vflag)
		(void) fprintf(stderr, "Chmod 444\n");
	n = fchmod(fd, 0444);
	if (n < 0) {
		(void) fprintf(stderr, "chmod 444, err=%d\n", errno);
		exit(1);
	}

	if (vflag)
		(void) fprintf(stderr, "Write 2 (mode 444)\n");
	n = write(fd, "mode 444 OK\n", 12);
	if (n != 12) {
		(void) fprintf(stderr, "write mode 444, err=%d\n", errno);
		exit(1);
	}

	if (vflag)
		(void) fprintf(stderr, "Set DOS R/O\n");
	n = dosattr_set_ro(fd, NULL /* fname? */);
	if (n != 0) {
		(void) fprintf(stderr, "Set R/O, err=%d\n", n);
		exit(1);
	}

	/*
	 * This fails, but write on an already open handle should succeed
	 * the same as when we've set the mode to 444 after open.
	 */
	if (vflag)
		(void) fprintf(stderr, "Write 3 (DOS R/O)\n");
	n = write(fd, "Write DOS RO?\n", 14);
	if (n != 14) {
		(void) fprintf(stderr, "write (DOS R/O), err=%d\n", errno);
		exit(1);
	}

	return (0);
}