More linking issues fixed.
[glibc.git] / posix / tst-chmod.c
blobd37e4820fa26dee236ab028915b907a3331cef8a
1 /* Test for chmod functions.
2 Copyright (C) 2000, 2004 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 2000.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 #include <dirent.h>
22 #include <errno.h>
23 #include <error.h>
24 #include <fcntl.h>
25 #include <mcheck.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
30 #include <sys/stat.h>
33 #define OUT_OF_MEMORY \
34 do { \
35 puts ("cannot allocate memory"); \
36 result = 1; \
37 goto fail; \
38 } while (0)
40 static int
41 do_test (int argc, char *argv[])
43 const char *builddir;
44 struct stat64 st1;
45 struct stat64 st2;
46 char *buf;
47 char *testdir;
48 char *testfile = NULL;
49 char *startdir;
50 size_t buflen;
51 int fd;
52 int result = 0;
53 DIR *dir;
55 mtrace ();
57 if (argc <= 1)
58 error (EXIT_FAILURE, 0, "no parameters");
60 /* This is where we will create the test files. */
61 builddir = argv[1];
62 buflen = strlen (builddir) + 50;
64 startdir = getcwd (NULL, 0);
65 if (startdir == NULL)
67 printf ("cannot get current directory: %m\n");
68 exit (EXIT_FAILURE);
71 /* A buffer large enough for everything we need. */
72 buf = (char *) alloca (buflen);
74 /* Create the directory name. */
75 snprintf (buf, buflen, "%s/chmoddirXXXXXX", builddir);
77 if (mkdtemp (buf) == NULL)
79 printf ("cannot create test directory: %m\n");
80 exit (EXIT_FAILURE);
83 if (chmod ("", 0600) == 0)
85 puts ("chmod(\"\", 0600 didn't fail");
86 result = 1;
88 else if (errno != ENOENT)
90 puts ("chmod(\"\",0600) does not set errno to ENOENT");
91 result = 1;
94 /* Create a duplicate. */
95 testdir = strdup (buf);
96 if (testdir == NULL)
97 OUT_OF_MEMORY;
99 if (stat64 (testdir, &st1) != 0)
101 printf ("cannot stat test directory: %m\n");
102 exit (1);
104 if (!S_ISDIR (st1.st_mode))
106 printf ("file not created as directory: %m\n");
107 exit (1);
110 /* We have to wait for a second to make sure the ctime changes. */
111 sleep (1);
113 /* Remove all access rights from the directory. */
114 if (chmod (testdir, 0) != 0)
116 printf ("cannot change mode of test directory: %m\n");
117 result = 1;
118 goto fail;
121 if (stat64 (testdir, &st2) != 0)
123 printf ("cannot stat test directory: %m\n");
124 result = 1;
125 goto fail;
128 /* Compare result. */
129 if ((st2.st_mode & ALLPERMS) != 0)
131 printf ("chmod(...,0) on directory left bits nonzero: %o\n",
132 st2.st_mode & ALLPERMS);
133 result = 1;
135 if (st1.st_ctime >= st2.st_ctime)
137 puts ("chmod(...,0) did not set ctime correctly");
138 result = 1;
141 /* Name of a file in the directory. */
142 snprintf (buf, buflen, "%s/file", testdir);
143 testfile = strdup (buf);
144 if (testfile == NULL)
145 OUT_OF_MEMORY;
147 fd = creat (testfile, 0);
148 if (fd != -1)
150 if (getuid () != 0)
152 puts ("managed to create test file in protected directory");
153 result = 1;
155 close (fd);
157 else if (errno != EACCES)
159 puts ("creat didn't generate correct errno value");
160 result = 1;
163 /* With this mode it still shouldn't be possible to create a file. */
164 if (chmod (testdir, 0600) != 0)
166 printf ("cannot change mode of test directory to 0600: %m\n");
167 result = 1;
168 goto fail;
171 fd = creat (testfile, 0);
172 if (fd != -1)
174 if (getuid () != 0)
176 puts ("managed to create test file in no-x protected directory");
177 result = 1;
179 close (fd);
181 else if (errno != EACCES)
183 puts ("creat didn't generate correct errno value");
184 result = 1;
187 /* Change the directory mode back to allow creating a file. This
188 time with fchmod. */
189 dir = opendir (testdir);
190 if (dir != NULL)
192 if (fchmod (dirfd (dir), 0700) != 0)
194 printf ("cannot change mode of test directory to 0700: %m\n");
195 result = 1;
196 closedir (dir);
197 goto fail;
200 closedir (dir);
202 else
204 printf ("cannot open directory: %m\n");
205 result = 1;
207 if (chmod (testdir, 0700) != 0)
209 printf ("cannot change mode of test directory to 0700: %m\n");
210 goto fail;
214 fd = creat (testfile, 0);
215 if (fd == -1)
217 puts ("still didn't manage to create test file in protected directory");
218 result = 1;
219 goto fail;
221 if (fstat64 (fd, &st1) != 0)
223 printf ("cannot stat new file: %m\n");
224 result = 1;
226 else if ((st1.st_mode & ALLPERMS) != 0)
228 puts ("file not created with access mode 0");
229 result = 1;
231 close (fd);
233 snprintf (buf, buflen, "%s/..", testdir);
234 chdir (buf);
235 /* We are now in the directory above the one we create the test
236 directory in. */
238 sleep (1);
239 snprintf (buf, buflen, "./%s/../%s/file",
240 basename (testdir), basename (testdir));
241 if (chmod (buf, 0600) != 0)
243 printf ("cannot change mode of file to 0600: %m\n");
244 result = 1;
245 goto fail;
247 snprintf (buf, buflen, "./%s//file", basename (testdir));
248 if (stat64 (buf, &st2) != 0)
250 printf ("cannot stat new file: %m\n");
251 result = 1;
253 else if ((st2.st_mode & ALLPERMS) != 0600)
255 puts ("file mode not changed to 0600");
256 result = 1;
258 else if (st1.st_ctime >= st2.st_ctime)
260 puts ("chmod(\".../file\",0600) did not set ctime correctly");
261 result = 1;
264 if (chmod (buf, 0777 | S_ISUID | S_ISGID) != 0)
266 printf ("cannot change mode of file to %o: %m\n",
267 0777 | S_ISUID | S_ISGID);
268 result = 1;
270 if (stat64 (buf, &st2) != 0)
272 printf ("cannot stat test file: %m\n");
273 result = 1;
275 else if ((st2.st_mode & ALLPERMS) != (0777 | S_ISUID | S_ISGID))
277 puts ("file mode not changed to 0777 | S_ISUID | S_ISGID");
278 result = 1;
281 if (chmod (basename (testdir), 0777 | S_ISUID | S_ISGID | S_ISVTX) != 0)
283 printf ("cannot change mode of test directory to %o: %m\n",
284 0777 | S_ISUID | S_ISGID | S_ISVTX);
285 result = 1;
287 if (stat64 (basename (testdir), &st2) != 0)
289 printf ("cannot stat test directory: %m\n");
290 result = 1;
292 else if ((st2.st_mode & ALLPERMS) != (0777 | S_ISUID | S_ISGID | S_ISVTX))
294 puts ("directory mode not changed to 0777 | S_ISUID | S_ISGID | S_ISGID");
295 result = 1;
298 snprintf (buf, buflen, "./%s/no-such-file", basename (testdir));
299 if (chmod (buf, 0600) != -1)
301 puts ("chmod(\".../no-such-file\",0600) did not fail");
302 result = 1;
304 else if (errno != ENOENT)
306 puts ("chmod(\".../no-such-file\",0600) does not set errno to ENOENT");
307 result = 1;
310 snprintf (buf, buflen, "%s/", basename (testdir));
311 if (chmod (basename (testdir), 0677) != 0)
313 printf ("cannot change mode of test directory to 0677: %m\n");
314 result = 1;
316 else
318 snprintf (buf, buflen, "./%s/file", basename (testdir));
319 if (chmod (buf, 0600) == 0)
321 if (getuid () != 0)
323 puts ("chmod(\".../file\") with no-exec directory succeeded");
324 result = 1;
327 else if (errno != EACCES)
329 puts ("chmod(\".../file\") with no-exec directory didn't set EACCES");
330 result = 1;
334 if (chmod (basename (testdir), 0777) != 0)
336 printf ("cannot change mode of test directory to 0777: %m\n");
337 result = 1;
338 goto fail;
341 snprintf (buf, buflen, "%s/file/cannot-be", basename (testdir));
342 if (chmod (buf, 0600) == 0)
344 puts ("chmod(\".../file/cannot-be\",0600) did not fail");
345 result = 1;
347 else if (errno != ENOTDIR)
349 puts ("chmod(\".../file/cannot-be\",0600) does not set errno to ENOTDIR");
350 result = 1;
353 fail:
354 chdir (startdir);
356 /* Remove all the files. */
357 chmod (testdir, 0700);
358 if (testfile != NULL)
360 chmod (testfile, 0700);
361 unlink (testfile);
363 rmdir (testdir);
365 /* Free the resources. */
366 free (testfile);
367 free (testdir);
368 free (startdir);
370 return result;
374 /* We need a few seconds since we have a few sleeps in the code. */
375 #define TIMEOUT 20
378 #include "../test-skeleton.c"