Switch to UTF-8 for INSTALL
[glibc.git] / sysdeps / pthread / tst-umask1.c
blob47feb10fa9859d22fe05e6c265149c42cc46f89d
1 /* Copyright (C) 2003-2023 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
18 #include <fcntl.h>
19 #include <pthread.h>
20 #include <stdbool.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <sys/stat.h>
27 static struct
29 int (*fp) (const char *, mode_t);
30 const char *name;
31 bool is_fd;
32 } fcts[] =
34 { creat, "creat", true },
35 { mkdir, "mkdir", false },
36 { mkfifo, "mkfifo", false },
38 #define nfcts (sizeof (fcts) / sizeof (fcts[0]))
41 static int
42 work (const char *fname, int mask)
44 int result = 0;
45 size_t i;
46 for (i = 0; i < nfcts; ++i)
48 remove (fname);
49 int fd = fcts[i].fp (fname, 0777);
50 if (fd == -1)
52 printf ("cannot %s %s: %m\n", fcts[i].name, fname);
53 exit (1);
55 if (fcts[i].is_fd)
56 close (fd);
57 struct stat64 st;
58 if (stat64 (fname, &st) == -1)
60 printf ("cannot stat %s after %s: %m\n", fname, fcts[i].name);
61 exit (1);
64 if ((st.st_mode & mask) != 0)
66 printf ("mask not successful after %s: %x still set\n",
67 fcts[i].name, (unsigned int) (st.st_mode & mask));
68 result = 1;
72 return result;
76 static pthread_barrier_t bar;
79 static void *
80 tf (void *arg)
82 pthread_barrier_wait (&bar);
84 int result = work (arg, 022);
86 pthread_barrier_wait (&bar);
88 pthread_barrier_wait (&bar);
90 return (work (arg, 0) | result) ? (void *) -1l : NULL;
94 static int
95 do_test (const char *fname)
97 int result = 0;
99 umask (0);
100 result |= work (fname, 0);
102 pthread_barrier_init (&bar, NULL, 2);
104 pthread_t th;
105 if (pthread_create (&th, NULL, tf, (void *) fname) != 0)
107 puts ("cannot create thread");
108 exit (1);
111 umask (022);
112 result |= work (fname, 022);
114 pthread_barrier_wait (&bar);
116 pthread_barrier_wait (&bar);
118 umask (0);
120 pthread_barrier_wait (&bar);
122 void *res;
123 if (pthread_join (th, &res) != 0)
125 puts ("join failed");
126 exit (1);
129 remove (fname);
131 return result || res != NULL;
134 #define TEST_FUNCTION do_test (argc < 2 ? "/tmp/tst-umask.tmp" : argv[1])
135 #include "../test-skeleton.c"