2003-05-28 Roland McGrath <roland@redhat.com>
[glibc.git] / nptl / tst-umask1.c
blobbd7531901d0f52d61e9285e2b3b854de0868d30b
1 /* Copyright (C) 2003 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2003.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library 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 the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <fcntl.h>
21 #include <pthread.h>
22 #include <stdbool.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <sys/stat.h>
29 static struct
31 int (*fp) (const char *, mode_t);
32 const char *name;
33 bool is_fd;
34 } fcts[] =
36 { creat, "creat", true },
37 { mkdir, "mkdir", false },
38 { mkfifo, "mkfifo", false },
40 #define nfcts (sizeof (fcts) / sizeof (fcts[0]))
43 static int
44 work (const char *fname, int mask)
46 int result = 0;
47 size_t i;
48 for (i = 0; i < nfcts; ++i)
50 remove (fname);
51 int fd = fcts[i].fp (fname, 0777);
52 if (fd == -1)
54 printf ("cannot %s %s: %m\n", fcts[i].name, fname);
55 exit (1);
57 if (fcts[i].is_fd)
58 close (fd);
59 struct stat64 st;
60 if (stat64 (fname, &st) == -1)
62 printf ("cannot stat %s after %s: %m\n", fname, fcts[i].name);
63 exit (1);
66 if ((st.st_mode & mask) != 0)
68 printf ("mask not successful after %s: %x still set\n",
69 fcts[i].name, (unsigned int) (st.st_mode & mask));
70 result = 1;
74 return result;
78 static pthread_barrier_t bar;
81 static void *
82 tf (void *arg)
84 pthread_barrier_wait (&bar);
86 int result = work (arg, 022);
88 pthread_barrier_wait (&bar);
90 pthread_barrier_wait (&bar);
92 return (work (arg, 0) | result) ? (void *) -1l : NULL;
96 static int
97 do_test (const char *fname)
99 int result = 0;
101 umask (0);
102 result |= work (fname, 0);
104 pthread_barrier_init (&bar, NULL, 2);
106 pthread_t th;
107 if (pthread_create (&th, NULL, tf, (void *) fname) != 0)
109 puts ("cannot create thread");
110 exit (1);
113 umask (022);
114 result |= work (fname, 022);
116 pthread_barrier_wait (&bar);
118 pthread_barrier_wait (&bar);
120 umask (0);
122 pthread_barrier_wait (&bar);
124 void *res;
125 if (pthread_join (th, &res) != 0)
127 puts ("join failed");
128 exit (1);
131 remove (fname);
133 return result || res != NULL;
136 #define TEST_FUNCTION do_test (argc < 2 ? "/tmp/tst-umask.tmp" : argv[1])
137 #include "../test-skeleton.c"