tst-umask1.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <fcntl.h>
  16. #include <pthread.h>
  17. #include <stdbool.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <unistd.h>
  21. #include <sys/stat.h>
  22. static struct
  23. {
  24. int (*fp) (const char *, mode_t);
  25. const char *name;
  26. bool is_fd;
  27. } fcts[] =
  28. {
  29. { creat, "creat", true },
  30. { mkdir, "mkdir", false },
  31. { mkfifo, "mkfifo", false },
  32. };
  33. #define nfcts (sizeof (fcts) / sizeof (fcts[0]))
  34. static int
  35. work (const char *fname, int mask)
  36. {
  37. int result = 0;
  38. size_t i;
  39. for (i = 0; i < nfcts; ++i)
  40. {
  41. remove (fname);
  42. int fd = fcts[i].fp (fname, 0777);
  43. if (fd == -1)
  44. {
  45. printf ("cannot %s %s: %m\n", fcts[i].name, fname);
  46. exit (1);
  47. }
  48. if (fcts[i].is_fd)
  49. close (fd);
  50. struct stat st;
  51. if (stat (fname, &st) == -1)
  52. {
  53. printf ("cannot stat %s after %s: %m\n", fname, fcts[i].name);
  54. exit (1);
  55. }
  56. if ((st.st_mode & mask) != 0)
  57. {
  58. printf ("mask not successful after %s: %x still set\n",
  59. fcts[i].name, (unsigned int) (st.st_mode & mask));
  60. result = 1;
  61. }
  62. }
  63. return result;
  64. }
  65. static pthread_barrier_t bar;
  66. static void *
  67. tf (void *arg)
  68. {
  69. pthread_barrier_wait (&bar);
  70. int result = work (arg, 022);
  71. pthread_barrier_wait (&bar);
  72. pthread_barrier_wait (&bar);
  73. return (work (arg, 0) | result) ? (void *) -1l : NULL;
  74. }
  75. static int
  76. do_test (const char *fname)
  77. {
  78. int result = 0;
  79. umask (0);
  80. result |= work (fname, 0);
  81. pthread_barrier_init (&bar, NULL, 2);
  82. pthread_t th;
  83. if (pthread_create (&th, NULL, tf, (void *) fname) != 0)
  84. {
  85. puts ("cannot create thread");
  86. exit (1);
  87. }
  88. umask (022);
  89. result |= work (fname, 022);
  90. pthread_barrier_wait (&bar);
  91. pthread_barrier_wait (&bar);
  92. umask (0);
  93. pthread_barrier_wait (&bar);
  94. void *res;
  95. if (pthread_join (th, &res) != 0)
  96. {
  97. puts ("join failed");
  98. exit (1);
  99. }
  100. remove (fname);
  101. return result || res != NULL;
  102. }
  103. #define TEST_FUNCTION do_test (argc < 2 ? "/tmp/tst-umask.tmp" : argv[1])
  104. #include "../test-skeleton.c"