sem_open.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #include <semaphore.h>
  2. #include <sys/mman.h>
  3. #include <limits.h>
  4. #include <fcntl.h>
  5. #include <unistd.h>
  6. #include <string.h>
  7. #include <stdarg.h>
  8. #include <errno.h>
  9. #include <time.h>
  10. #include <stdio.h>
  11. #include <sys/stat.h>
  12. #include <stdlib.h>
  13. #include <pthread.h>
  14. static struct {
  15. ino_t ino;
  16. sem_t *sem;
  17. int refcnt;
  18. } *semtab;
  19. static int semcnt;
  20. static pthread_spinlock_t lock;
  21. static pthread_once_t once;
  22. static void init()
  23. {
  24. semtab = calloc(sizeof *semtab, SEM_NSEMS_MAX);
  25. }
  26. static sem_t *find_map(ino_t ino)
  27. {
  28. int i;
  29. for (i=0; i<SEM_NSEMS_MAX && semtab[i].ino != ino; i++);
  30. if (i==SEM_NSEMS_MAX) return 0;
  31. if (semtab[i].refcnt == INT_MAX) return (sem_t *)-1;
  32. semtab[i].refcnt++;
  33. return semtab[i].sem;
  34. }
  35. sem_t *sem_open(const char *name, int flags, ...)
  36. {
  37. va_list ap;
  38. mode_t mode;
  39. unsigned value;
  40. int fd, tfd, dir;
  41. sem_t newsem;
  42. void *map;
  43. char tmp[64];
  44. struct timespec ts;
  45. struct stat st;
  46. int i;
  47. while (*name=='/') name++;
  48. if (strchr(name, '/')) {
  49. errno = EINVAL;
  50. return SEM_FAILED;
  51. }
  52. pthread_once(&once, init);
  53. if (!semtab) {
  54. errno = ENOMEM;
  55. return SEM_FAILED;
  56. }
  57. if (flags & O_CREAT) {
  58. va_start(ap, flags);
  59. mode = va_arg(ap, mode_t) & 0666;
  60. value = va_arg(ap, unsigned);
  61. va_end(ap);
  62. if (value > SEM_VALUE_MAX) {
  63. errno = EINVAL;
  64. return SEM_FAILED;
  65. }
  66. sem_init(&newsem, 0, value);
  67. clock_gettime(CLOCK_REALTIME, &ts);
  68. snprintf(tmp, sizeof(tmp), "/dev/shm/%p-%p-%d-%d",
  69. &name, name, (int)getpid(), (int)ts.tv_nsec);
  70. tfd = open(tmp, O_CREAT|O_EXCL|O_RDWR, mode);
  71. if (tfd<0) return SEM_FAILED;
  72. dir = open("/dev/shm", O_DIRECTORY|O_RDONLY);
  73. if (dir<0 || write(tfd,&newsem,sizeof newsem)!=sizeof newsem) {
  74. if (dir >= 0) close(dir);
  75. close(tfd);
  76. unlink(tmp);
  77. return SEM_FAILED;
  78. }
  79. }
  80. flags &= ~O_ACCMODE;
  81. flags |= O_RDWR;
  82. pthread_spin_lock(&lock);
  83. for (;;) {
  84. if (!(flags & O_EXCL)) {
  85. fd = shm_open(name, flags&~O_CREAT, mode);
  86. if (fd >= 0 || errno != ENOENT) {
  87. if (flags & O_CREAT) {
  88. close(dir);
  89. close(tfd);
  90. unlink(tmp);
  91. }
  92. if (fd >= 0 && fstat(fd, &st) < 0) {
  93. close(fd);
  94. fd = -1;
  95. }
  96. if (fd < 0) {
  97. pthread_spin_unlock(&lock);
  98. return SEM_FAILED;
  99. }
  100. if ((map = find_map(st.st_ino))) {
  101. pthread_spin_unlock(&lock);
  102. close(fd);
  103. if (map == (sem_t *)-1)
  104. return SEM_FAILED;
  105. return map;
  106. }
  107. break;
  108. }
  109. }
  110. if (!(flags & O_CREAT)) {
  111. pthread_spin_unlock(&lock);
  112. return SEM_FAILED;
  113. }
  114. if (!linkat(AT_FDCWD, tmp, dir, name, 0)) {
  115. fd = tfd;
  116. close(dir);
  117. unlink(tmp);
  118. break;
  119. }
  120. if ((flags & O_EXCL) || errno != EEXIST) {
  121. close(dir);
  122. close(tfd);
  123. unlink(tmp);
  124. return SEM_FAILED;
  125. }
  126. }
  127. if (fstat(fd, &st) < 0) {
  128. pthread_spin_unlock(&lock);
  129. close(fd);
  130. return SEM_FAILED;
  131. }
  132. if (semcnt == SEM_NSEMS_MAX) {
  133. pthread_spin_unlock(&lock);
  134. close(fd);
  135. errno = EMFILE;
  136. return SEM_FAILED;
  137. }
  138. for (i=0; i<SEM_NSEMS_MAX && semtab[i].sem; i++);
  139. map = mmap(0, sizeof(sem_t), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
  140. close(fd);
  141. if (map == MAP_FAILED) {
  142. pthread_spin_unlock(&lock);
  143. return SEM_FAILED;
  144. }
  145. semtab[i].ino = st.st_ino;
  146. semtab[i].sem = map;
  147. semtab[i].refcnt = 1;
  148. pthread_spin_unlock(&lock);
  149. return map;
  150. }
  151. int sem_close(sem_t *sem)
  152. {
  153. int i;
  154. pthread_spin_lock(&lock);
  155. for (i=0; i<SEM_NSEMS_MAX && semtab[i].sem != sem; i++);
  156. if (!--semtab[i].refcnt) {
  157. semtab[i].sem = 0;
  158. semtab[i].ino = 0;
  159. }
  160. pthread_spin_unlock(&lock);
  161. return munmap(sem, sizeof *sem);
  162. }