Update copyright dates with scripts/update-copyrights.
[glibc.git] / nptl / tst-umask1.c
blob786fbd50b2ae0a3727488332734dd81fc90b7646
1 /* Copyright (C) 2003-2015 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, see
17 <http://www.gnu.org/licenses/>. */
19 #include <fcntl.h>
20 #include <pthread.h>
21 #include <stdbool.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <sys/stat.h>
28 static struct
30 int (*fp) (const char *, mode_t);
31 const char *name;
32 bool is_fd;
33 } fcts[] =
35 { creat, "creat", true },
36 { mkdir, "mkdir", false },
37 { mkfifo, "mkfifo", false },
39 #define nfcts (sizeof (fcts) / sizeof (fcts[0]))
42 static int
43 work (const char *fname, int mask)
45 int result = 0;
46 size_t i;
47 for (i = 0; i < nfcts; ++i)
49 remove (fname);
50 int fd = fcts[i].fp (fname, 0777);
51 if (fd == -1)
53 printf ("cannot %s %s: %m\n", fcts[i].name, fname);
54 exit (1);
56 if (fcts[i].is_fd)
57 close (fd);
58 struct stat64 st;
59 if (stat64 (fname, &st) == -1)
61 printf ("cannot stat %s after %s: %m\n", fname, fcts[i].name);
62 exit (1);
65 if ((st.st_mode & mask) != 0)
67 printf ("mask not successful after %s: %x still set\n",
68 fcts[i].name, (unsigned int) (st.st_mode & mask));
69 result = 1;
73 return result;
77 static pthread_barrier_t bar;
80 static void *
81 tf (void *arg)
83 pthread_barrier_wait (&bar);
85 int result = work (arg, 022);
87 pthread_barrier_wait (&bar);
89 pthread_barrier_wait (&bar);
91 return (work (arg, 0) | result) ? (void *) -1l : NULL;
95 static int
96 do_test (const char *fname)
98 int result = 0;
100 umask (0);
101 result |= work (fname, 0);
103 pthread_barrier_init (&bar, NULL, 2);
105 pthread_t th;
106 if (pthread_create (&th, NULL, tf, (void *) fname) != 0)
108 puts ("cannot create thread");
109 exit (1);
112 umask (022);
113 result |= work (fname, 022);
115 pthread_barrier_wait (&bar);
117 pthread_barrier_wait (&bar);
119 umask (0);
121 pthread_barrier_wait (&bar);
123 void *res;
124 if (pthread_join (th, &res) != 0)
126 puts ("join failed");
127 exit (1);
130 remove (fname);
132 return result || res != NULL;
135 #define TEST_FUNCTION do_test (argc < 2 ? "/tmp/tst-umask.tmp" : argv[1])
136 #include "../test-skeleton.c"