source: vendor/glibc-tests/2005-06-14/nptl/tst-umask1.c

Last change on this file was 2036, checked in by bird, 20 years ago

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 2.8 KB
Line 
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.
4
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.
9
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.
14
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. */
19
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>
27
28
29static struct
30{
31 int (*fp) (const char *, mode_t);
32 const char *name;
33 bool is_fd;
34} fcts[] =
35{
36 { creat, "creat", true },
37 { mkdir, "mkdir", false },
38 { mkfifo, "mkfifo", false },
39};
40#define nfcts (sizeof (fcts) / sizeof (fcts[0]))
41
42
43static int
44work (const char *fname, int mask)
45{
46 int result = 0;
47 size_t i;
48 for (i = 0; i < nfcts; ++i)
49 {
50 remove (fname);
51 int fd = fcts[i].fp (fname, 0777);
52 if (fd == -1)
53 {
54 printf ("cannot %s %s: %m\n", fcts[i].name, fname);
55 exit (1);
56 }
57 if (fcts[i].is_fd)
58 close (fd);
59 struct stat64 st;
60 if (stat64 (fname, &st) == -1)
61 {
62 printf ("cannot stat %s after %s: %m\n", fname, fcts[i].name);
63 exit (1);
64 }
65
66 if ((st.st_mode & mask) != 0)
67 {
68 printf ("mask not successful after %s: %x still set\n",
69 fcts[i].name, (unsigned int) (st.st_mode & mask));
70 result = 1;
71 }
72 }
73
74 return result;
75}
76
77
78static pthread_barrier_t bar;
79
80
81static void *
82tf (void *arg)
83{
84 pthread_barrier_wait (&bar);
85
86 int result = work (arg, 022);
87
88 pthread_barrier_wait (&bar);
89
90 pthread_barrier_wait (&bar);
91
92 return (work (arg, 0) | result) ? (void *) -1l : NULL;
93}
94
95
96static int
97do_test (const char *fname)
98{
99 int result = 0;
100
101 umask (0);
102 result |= work (fname, 0);
103
104 pthread_barrier_init (&bar, NULL, 2);
105
106 pthread_t th;
107 if (pthread_create (&th, NULL, tf, (void *) fname) != 0)
108 {
109 puts ("cannot create thread");
110 exit (1);
111 }
112
113 umask (022);
114 result |= work (fname, 022);
115
116 pthread_barrier_wait (&bar);
117
118 pthread_barrier_wait (&bar);
119
120 umask (0);
121
122 pthread_barrier_wait (&bar);
123
124 void *res;
125 if (pthread_join (th, &res) != 0)
126 {
127 puts ("join failed");
128 exit (1);
129 }
130
131 remove (fname);
132
133 return result || res != NULL;
134}
135
136#define TEST_FUNCTION do_test (argc < 2 ? "/tmp/tst-umask.tmp" : argv[1])
137#include "../test-skeleton.c"
Note: See TracBrowser for help on using the repository browser.