common/rc: factor out _scratch_xfs_[get|set]_sb_field
[xfstests-dev.git] / src / writemod.c
1 /*
2  * Copyright (c) 2004 Silicon Graphics, Inc.
3  * All Rights Reserved.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it would be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write the Free Software Foundation,
16  * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  */
18
19 /*
20  * tests out if access checking is done on write path
21  * 1. opens with write perms
22  * 2. fchmod to turn off write perms
23  * 3. writes to file
24  */
25
26 #include <unistd.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <string.h>
30 #include <fcntl.h>
31 #include <stdio.h>
32
33 int 
34 main(int argc, char* argv[])
35 {
36     char *path;
37     int fd;
38     char *buf = "hi there\n";
39     ssize_t x;
40     int sts;
41
42     if (argc != 2) {
43         fprintf(stderr, "%s: requires path argument\n", argv[0]);
44         return 1;
45     }
46
47     path = argv[1];
48
49     printf("open for write \"%s\" with 777\n", path);
50     fd = open(path, O_RDWR, 0777);
51     if (fd == -1) {
52         perror("open");
53         return 1;
54     }
55     printf("remove perms on file\n");
56     sts = fchmod(fd, 0);
57     if (sts == -1) {
58         perror("fchmod");
59         return 1;
60     }
61     printf("write to the file\n");
62     x = write(fd, buf, strlen(buf));
63     if (x == -1) {
64         perror("write");
65         return 1;
66     }
67     return 0;
68 }