tst-umask1.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. 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.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, write to the Free
  14. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA. */
  16. #include <fcntl.h>
  17. #include <pthread.h>
  18. #include <stdbool.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <unistd.h>
  22. #include <sys/stat.h>
  23. static struct
  24. {
  25. int (*fp) (const char *, mode_t);
  26. const char *name;
  27. bool is_fd;
  28. } fcts[] =
  29. {
  30. { creat, "creat", true },
  31. { mkdir, "mkdir", false },
  32. { mkfifo, "mkfifo", false },
  33. };
  34. #define nfcts (sizeof (fcts) / sizeof (fcts[0]))
  35. static int
  36. work (const char *fname, int mask)
  37. {
  38. int result = 0;
  39. size_t i;
  40. for (i = 0; i < nfcts; ++i)
  41. {
  42. remove (fname);
  43. int fd = fcts[i].fp (fname, 0777);
  44. if (fd == -1)
  45. {
  46. printf ("cannot %s %s: %m\n", fcts[i].name, fname);
  47. exit (1);
  48. }
  49. if (fcts[i].is_fd)
  50. close (fd);
  51. struct stat st;
  52. if (stat (fname, &st) == -1)
  53. {
  54. printf ("cannot stat %s after %s: %m\n", fname, fcts[i].name);
  55. exit (1);
  56. }
  57. if ((st.st_mode & mask) != 0)
  58. {
  59. printf ("mask not successful after %s: %x still set\n",
  60. fcts[i].name, (unsigned int) (st.st_mode & mask));
  61. result = 1;
  62. }
  63. }
  64. return result;
  65. }
  66. static pthread_barrier_t bar;
  67. static void *
  68. tf (void *arg)
  69. {
  70. pthread_barrier_wait (&bar);
  71. int result = work (arg, 022);
  72. pthread_barrier_wait (&bar);
  73. pthread_barrier_wait (&bar);
  74. return (work (arg, 0) | result) ? (void *) -1l : NULL;
  75. }
  76. static int
  77. do_test (const char *fname)
  78. {
  79. int result = 0;
  80. umask (0);
  81. result |= work (fname, 0);
  82. pthread_barrier_init (&bar, NULL, 2);
  83. pthread_t th;
  84. if (pthread_create (&th, NULL, tf, (void *) fname) != 0)
  85. {
  86. puts ("cannot create thread");
  87. exit (1);
  88. }
  89. umask (022);
  90. result |= work (fname, 022);
  91. pthread_barrier_wait (&bar);
  92. pthread_barrier_wait (&bar);
  93. umask (0);
  94. pthread_barrier_wait (&bar);
  95. void *res;
  96. if (pthread_join (th, &res) != 0)
  97. {
  98. puts ("join failed");
  99. exit (1);
  100. }
  101. remove (fname);
  102. return result || res != NULL;
  103. }
  104. #define TEST_FUNCTION do_test (argc < 2 ? "/tmp/tst-umask.tmp" : argv[1])
  105. #include "../test-skeleton.c"