common/rc: teach _scratch_mkfs_sized to set a size on an xfs realtime volume
[xfstests-dev.git] / src / writemod.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2004 Silicon Graphics, Inc.
4  * All Rights Reserved.
5  */
6
7 /*
8  * tests out if access checking is done on write path
9  * 1. opens with write perms
10  * 2. fchmod to turn off write perms
11  * 3. writes to file
12  */
13
14 #include <unistd.h>
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 #include <string.h>
18 #include <fcntl.h>
19 #include <stdio.h>
20
21 int 
22 main(int argc, char* argv[])
23 {
24     char *path;
25     int fd;
26     char *buf = "hi there\n";
27     ssize_t x;
28     int sts;
29
30     if (argc != 2) {
31         fprintf(stderr, "%s: requires path argument\n", argv[0]);
32         return 1;
33     }
34
35     path = argv[1];
36
37     printf("open for write \"%s\" with 777\n", path);
38     fd = open(path, O_RDWR, 0777);
39     if (fd == -1) {
40         perror("open");
41         return 1;
42     }
43     printf("remove perms on file\n");
44     sts = fchmod(fd, 0);
45     if (sts == -1) {
46         perror("fchmod");
47         return 1;
48     }
49     printf("write to the file\n");
50     x = write(fd, buf, strlen(buf));
51     if (x == -1) {
52         perror("write");
53         return 1;
54     }
55     return 0;
56 }