1 /*
2 * Copyright (c) International Business Machines Corp., 2001
3 * 07/2001 Ported by Wayne Boyer
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 * the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 /*
21 * DESCRIPTION
22 * This test will verify the mkdir(2) syscall basic functionality
23 */
24
25 #include <errno.h>
26 #include <string.h>
27 #include <signal.h>
28 #include <sys/stat.h>
29 #include <sys/types.h>
30 #include <fcntl.h>
31 #include <unistd.h>
32 #include <pwd.h>
33 #include "test.h"
34
35 void setup();
36 void cleanup();
37
38 #define PERMS 0777
39
40 char *TCID = "mkdir05";
41 int TST_TOTAL = 1;
42
43 char nobody_uid[] = "nobody";
44 struct passwd *ltpuser;
45
46 char tstdir1[100];
47
main(int ac,char ** av)48 int main(int ac, char **av)
49 {
50 int lc;
51 struct stat buf;
52
53 /*
54 * parse standard options
55 */
56 tst_parse_opts(ac, av, NULL, NULL);
57
58 /*
59 * perform global setup for test
60 */
61 setup();
62
63 /*
64 * check looping state if -i option given
65 */
66 for (lc = 0; TEST_LOOPING(lc); lc++) {
67
68 tst_count = 0;
69
70 /*
71 * TEST mkdir() base functionality
72 */
73
74 /* Initialize the test directory name */
75 sprintf(tstdir1, "tstdir1.%d", getpid());
76
77 /* Call mkdir(2) using the TEST macro */
78 TEST(mkdir(tstdir1, PERMS));
79
80 if (TEST_RETURN == -1) {
81 tst_resm(TFAIL, "mkdir(%s, %#o) Failed",
82 tstdir1, PERMS);
83 continue;
84 }
85
86 if (stat(tstdir1, &buf) == -1) {
87 tst_brkm(TBROK, cleanup, "failed to stat the "
88 "new directory");
89 }
90 /* check the owner */
91 if (buf.st_uid != geteuid()) {
92 tst_resm(TFAIL, "mkdir() FAILED to set owner ID"
93 " as process's effective ID");
94 continue;
95 }
96 /* check the group ID */
97 if (buf.st_gid != getegid()) {
98 tst_resm(TFAIL, "mkdir() failed to set group ID"
99 " as the process's group ID");
100 continue;
101 }
102 tst_resm(TPASS, "mkdir() functionality is correct");
103
104 /* clean up things in case we are looping */
105 if (rmdir(tstdir1) == -1) {
106 tst_brkm(TBROK, cleanup, "could not remove directory");
107 }
108
109 }
110
111 cleanup();
112 tst_exit();
113 }
114
115 /*
116 * setup() - performs all ONE TIME setup for this test.
117 */
setup(void)118 void setup(void)
119 {
120 tst_require_root();
121
122 ltpuser = getpwnam(nobody_uid);
123 if (setuid(ltpuser->pw_uid) == -1) {
124 tst_resm(TINFO, "setuid failed to "
125 "to set the effective uid to %d", ltpuser->pw_uid);
126 perror("setuid");
127 }
128
129 tst_sig(FORK, DEF_HANDLER, cleanup);
130
131 TEST_PAUSE;
132
133 /* Create a temporary directory and make it current. */
134 tst_tmpdir();
135 }
136
137 /*
138 * cleanup() - performs all ONE TIME cleanup for this test at
139 * completion or premature exit.
140 */
cleanup(void)141 void cleanup(void)
142 {
143
144 /*
145 * Remove the temporary directory.
146 */
147 tst_rmdir();
148
149 /*
150 * Exit with return code appropriate for results.
151 */
152
153 }
154