Back to index

glibc  2.9
tst-umask1.c
Go to the documentation of this file.
00001 /* Copyright (C) 2003 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Ulrich Drepper <drepper@redhat.com>, 2003.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <fcntl.h>
00021 #include <pthread.h>
00022 #include <stdbool.h>
00023 #include <stdio.h>
00024 #include <stdlib.h>
00025 #include <unistd.h>
00026 #include <sys/stat.h>
00027 
00028 
00029 static struct
00030 {
00031   int (*fp) (const char *, mode_t);
00032   const char *name;
00033   bool is_fd;
00034 } fcts[] =
00035 {
00036   { creat, "creat", true },
00037   { mkdir, "mkdir", false },
00038   { mkfifo, "mkfifo", false },
00039 };
00040 #define nfcts (sizeof (fcts) / sizeof (fcts[0]))
00041 
00042 
00043 static int
00044 work (const char *fname, int mask)
00045 {
00046   int result = 0;
00047   size_t i;
00048   for (i = 0; i < nfcts; ++i)
00049     {
00050       remove (fname);
00051       int fd = fcts[i].fp (fname, 0777);
00052       if (fd == -1)
00053        {
00054          printf ("cannot %s %s: %m\n", fcts[i].name, fname);
00055          exit (1);
00056        }
00057       if (fcts[i].is_fd)
00058        close (fd);
00059       struct stat64 st;
00060       if (stat64 (fname, &st) == -1)
00061        {
00062          printf ("cannot stat %s after %s: %m\n", fname, fcts[i].name);
00063          exit (1);
00064        }
00065 
00066       if ((st.st_mode & mask) != 0)
00067        {
00068          printf ("mask not successful after %s: %x still set\n",
00069                 fcts[i].name, (unsigned int) (st.st_mode & mask));
00070          result = 1;
00071        }
00072     }
00073 
00074   return result;
00075 }
00076 
00077 
00078 static pthread_barrier_t bar;
00079 
00080 
00081 static void *
00082 tf (void *arg)
00083 {
00084   pthread_barrier_wait (&bar);
00085 
00086   int result = work (arg, 022);
00087 
00088   pthread_barrier_wait (&bar);
00089 
00090   pthread_barrier_wait (&bar);
00091 
00092   return (work (arg, 0) | result) ? (void *) -1l : NULL;
00093 }
00094 
00095 
00096 static int
00097 do_test (const char *fname)
00098 {
00099   int result = 0;
00100 
00101   umask (0);
00102   result |= work (fname, 0);
00103 
00104   pthread_barrier_init (&bar, NULL, 2);
00105 
00106   pthread_t th;
00107   if (pthread_create (&th, NULL, tf, (void *) fname) != 0)
00108     {
00109       puts ("cannot create thread");
00110       exit (1);
00111     }
00112 
00113   umask (022);
00114   result |= work (fname, 022);
00115 
00116   pthread_barrier_wait (&bar);
00117 
00118   pthread_barrier_wait (&bar);
00119 
00120   umask (0);
00121 
00122   pthread_barrier_wait (&bar);
00123 
00124   void *res;
00125   if (pthread_join (th, &res) != 0)
00126     {
00127       puts ("join failed");
00128       exit (1);
00129     }
00130 
00131   remove (fname);
00132 
00133   return result || res != NULL;
00134 }
00135 
00136 #define TEST_FUNCTION do_test (argc < 2 ? "/tmp/tst-umask.tmp" : argv[1])
00137 #include "../test-skeleton.c"