| xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1 | /* Copyright (C) 2003-2016 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, see | 
 | 17 |    <http://www.gnu.org/licenses/>.  */ | 
 | 18 |  | 
 | 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> | 
 | 26 |  | 
 | 27 |  | 
 | 28 | static struct | 
 | 29 | { | 
 | 30 |   int (*fp) (const char *, mode_t); | 
 | 31 |   const char *name; | 
 | 32 |   bool is_fd; | 
 | 33 | } fcts[] = | 
 | 34 | { | 
 | 35 |   { creat, "creat", true }, | 
 | 36 |   { mkdir, "mkdir", false }, | 
 | 37 |   { mkfifo, "mkfifo", false }, | 
 | 38 | }; | 
 | 39 | #define nfcts (sizeof (fcts) / sizeof (fcts[0])) | 
 | 40 |  | 
 | 41 |  | 
 | 42 | static int | 
 | 43 | work (const char *fname, int mask) | 
 | 44 | { | 
 | 45 |   int result = 0; | 
 | 46 |   size_t i; | 
 | 47 |   for (i = 0; i < nfcts; ++i) | 
 | 48 |     { | 
 | 49 |       remove (fname); | 
 | 50 |       int fd = fcts[i].fp (fname, 0777); | 
 | 51 |       if (fd == -1) | 
 | 52 | 	{ | 
 | 53 | 	  printf ("cannot %s %s: %m\n", fcts[i].name, fname); | 
 | 54 | 	  exit (1); | 
 | 55 | 	} | 
 | 56 |       if (fcts[i].is_fd) | 
 | 57 | 	close (fd); | 
 | 58 |       struct stat64 st; | 
 | 59 |       if (stat64 (fname, &st) == -1) | 
 | 60 | 	{ | 
 | 61 | 	  printf ("cannot stat %s after %s: %m\n", fname, fcts[i].name); | 
 | 62 | 	  exit (1); | 
 | 63 | 	} | 
 | 64 |  | 
 | 65 |       if ((st.st_mode & mask) != 0) | 
 | 66 | 	{ | 
 | 67 | 	  printf ("mask not successful after %s: %x still set\n", | 
 | 68 | 		  fcts[i].name, (unsigned int) (st.st_mode & mask)); | 
 | 69 | 	  result = 1; | 
 | 70 | 	} | 
 | 71 |     } | 
 | 72 |  | 
 | 73 |   return result; | 
 | 74 | } | 
 | 75 |  | 
 | 76 |  | 
 | 77 | static pthread_barrier_t bar; | 
 | 78 |  | 
 | 79 |  | 
 | 80 | static void * | 
 | 81 | tf (void *arg) | 
 | 82 | { | 
 | 83 |   pthread_barrier_wait (&bar); | 
 | 84 |  | 
 | 85 |   int result = work (arg, 022); | 
 | 86 |  | 
 | 87 |   pthread_barrier_wait (&bar); | 
 | 88 |  | 
 | 89 |   pthread_barrier_wait (&bar); | 
 | 90 |  | 
 | 91 |   return (work (arg, 0) | result) ? (void *) -1l : NULL; | 
 | 92 | } | 
 | 93 |  | 
 | 94 |  | 
 | 95 | static int | 
 | 96 | do_test (const char *fname) | 
 | 97 | { | 
 | 98 |   int result = 0; | 
 | 99 |  | 
 | 100 |   umask (0); | 
 | 101 |   result |= work (fname, 0); | 
 | 102 |  | 
 | 103 |   pthread_barrier_init (&bar, NULL, 2); | 
 | 104 |  | 
 | 105 |   pthread_t th; | 
 | 106 |   if (pthread_create (&th, NULL, tf, (void *) fname) != 0) | 
 | 107 |     { | 
 | 108 |       puts ("cannot create thread"); | 
 | 109 |       exit (1); | 
 | 110 |     } | 
 | 111 |  | 
 | 112 |   umask (022); | 
 | 113 |   result |= work (fname, 022); | 
 | 114 |  | 
 | 115 |   pthread_barrier_wait (&bar); | 
 | 116 |  | 
 | 117 |   pthread_barrier_wait (&bar); | 
 | 118 |  | 
 | 119 |   umask (0); | 
 | 120 |  | 
 | 121 |   pthread_barrier_wait (&bar); | 
 | 122 |  | 
 | 123 |   void *res; | 
 | 124 |   if (pthread_join (th, &res) != 0) | 
 | 125 |     { | 
 | 126 |       puts ("join failed"); | 
 | 127 |       exit (1); | 
 | 128 |     } | 
 | 129 |  | 
 | 130 |   remove (fname); | 
 | 131 |  | 
 | 132 |   return result || res != NULL; | 
 | 133 | } | 
 | 134 |  | 
 | 135 | #define TEST_FUNCTION do_test (argc < 2 ? "/tmp/tst-umask.tmp" : argv[1]) | 
 | 136 | #include "../test-skeleton.c" |