You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

225 lines
4.8 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. /*
  2. FUSE: Filesystem in Userspace
  3. Copyright (C) 2001-2007 Miklos Szeredi <miklos@szeredi.hu>
  4. This program can be distributed under the terms of the GNU LGPLv2.
  5. See the file COPYING.LIB.
  6. */
  7. #include "fuse_lowlevel.h"
  8. #include "fuse_misc.h"
  9. #include "fuse_kernel.h"
  10. #include "fuse_i.h"
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <unistd.h>
  15. #include <signal.h>
  16. #include <semaphore.h>
  17. #include <errno.h>
  18. #include <sys/time.h>
  19. #include <unistd.h>
  20. /* Environment var controlling the thread stack size */
  21. #define ENVNAME_THREAD_STACK "FUSE_THREAD_STACK"
  22. struct fuse_worker {
  23. struct fuse_worker *prev;
  24. struct fuse_worker *next;
  25. pthread_t thread_id;
  26. size_t bufsize;
  27. char *buf;
  28. struct fuse_mt *mt;
  29. };
  30. struct fuse_mt {
  31. struct fuse_session *se;
  32. struct fuse_chan *prevch;
  33. struct fuse_worker main;
  34. sem_t finish;
  35. int exit;
  36. int error;
  37. };
  38. static void list_add_worker(struct fuse_worker *w, struct fuse_worker *next)
  39. {
  40. struct fuse_worker *prev = next->prev;
  41. w->next = next;
  42. w->prev = prev;
  43. prev->next = w;
  44. next->prev = w;
  45. }
  46. static void list_del_worker(struct fuse_worker *w)
  47. {
  48. struct fuse_worker *prev = w->prev;
  49. struct fuse_worker *next = w->next;
  50. prev->next = next;
  51. next->prev = prev;
  52. }
  53. static int fuse_loop_start_thread(struct fuse_mt *mt);
  54. static void *fuse_do_work(void *data)
  55. {
  56. struct fuse_worker *w = (struct fuse_worker *) data;
  57. struct fuse_mt *mt = w->mt;
  58. while (!fuse_session_exited(mt->se)) {
  59. struct fuse_chan *ch = mt->prevch;
  60. struct fuse_buf fbuf = {
  61. .mem = w->buf,
  62. .size = w->bufsize,
  63. };
  64. int res;
  65. pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
  66. res = fuse_session_receive_buf(mt->se, &fbuf, &ch);
  67. pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
  68. if (res == -EINTR)
  69. continue;
  70. if (res <= 0) {
  71. if (res < 0) {
  72. fuse_session_exit(mt->se);
  73. mt->error = -1;
  74. }
  75. break;
  76. }
  77. if (mt->exit)
  78. return NULL;
  79. fuse_session_process_buf(mt->se, &fbuf, ch);
  80. }
  81. sem_post(&mt->finish);
  82. return NULL;
  83. }
  84. int fuse_start_thread(pthread_t *thread_id, void *(*func)(void *), void *arg)
  85. {
  86. sigset_t oldset;
  87. sigset_t newset;
  88. int res;
  89. pthread_attr_t attr;
  90. char *stack_size;
  91. /* Override default stack size */
  92. pthread_attr_init(&attr);
  93. stack_size = getenv(ENVNAME_THREAD_STACK);
  94. if (stack_size && pthread_attr_setstacksize(&attr, atoi(stack_size)))
  95. fprintf(stderr, "fuse: invalid stack size: %s\n", stack_size);
  96. /* Disallow signal reception in worker threads */
  97. sigemptyset(&newset);
  98. sigaddset(&newset, SIGTERM);
  99. sigaddset(&newset, SIGINT);
  100. sigaddset(&newset, SIGHUP);
  101. sigaddset(&newset, SIGQUIT);
  102. pthread_sigmask(SIG_BLOCK, &newset, &oldset);
  103. res = pthread_create(thread_id, &attr, func, arg);
  104. pthread_sigmask(SIG_SETMASK, &oldset, NULL);
  105. pthread_attr_destroy(&attr);
  106. if (res != 0) {
  107. fprintf(stderr, "fuse: error creating thread: %s\n",
  108. strerror(res));
  109. return -1;
  110. }
  111. return 0;
  112. }
  113. static int fuse_loop_start_thread(struct fuse_mt *mt)
  114. {
  115. int res;
  116. struct fuse_worker *w = malloc(sizeof(struct fuse_worker));
  117. if (!w) {
  118. fprintf(stderr, "fuse: failed to allocate worker structure\n");
  119. return -1;
  120. }
  121. memset(w, 0, sizeof(struct fuse_worker));
  122. w->bufsize = fuse_chan_bufsize(mt->prevch);
  123. w->buf = calloc(w->bufsize,1);
  124. w->mt = mt;
  125. if (!w->buf) {
  126. fprintf(stderr, "fuse: failed to allocate read buffer\n");
  127. free(w);
  128. return -1;
  129. }
  130. res = fuse_start_thread(&w->thread_id, fuse_do_work, w);
  131. if (res == -1) {
  132. free(w->buf);
  133. free(w);
  134. return -1;
  135. }
  136. list_add_worker(w, &mt->main);
  137. return 0;
  138. }
  139. static void fuse_join_worker(struct fuse_worker *w)
  140. {
  141. pthread_join(w->thread_id, NULL);
  142. list_del_worker(w);
  143. free(w->buf);
  144. free(w);
  145. }
  146. static int number_of_threads(void)
  147. {
  148. #ifdef _SC_NPROCESSORS_ONLN
  149. return sysconf(_SC_NPROCESSORS_ONLN);
  150. #endif
  151. return 4;
  152. }
  153. int fuse_session_loop_mt(struct fuse_session *se,
  154. const int _threads)
  155. {
  156. int i;
  157. int err;
  158. int threads;
  159. struct fuse_mt mt;
  160. struct fuse_worker *w;
  161. memset(&mt, 0, sizeof(struct fuse_mt));
  162. mt.se = se;
  163. mt.prevch = fuse_session_next_chan(se, NULL);
  164. mt.error = 0;
  165. mt.main.thread_id = pthread_self();
  166. mt.main.prev = mt.main.next = &mt.main;
  167. sem_init(&mt.finish, 0, 0);
  168. threads = ((_threads > 0) ? _threads : number_of_threads());
  169. if(_threads < 0)
  170. threads /= -_threads;
  171. if(threads == 0)
  172. threads = 1;
  173. err = 0;
  174. for(i = 0; (i < threads) && !err; i++)
  175. err = fuse_loop_start_thread(&mt);
  176. if (!err) {
  177. /* sem_wait() is interruptible */
  178. while (!fuse_session_exited(se))
  179. sem_wait(&mt.finish);
  180. for (w = mt.main.next; w != &mt.main; w = w->next)
  181. pthread_cancel(w->thread_id);
  182. mt.exit = 1;
  183. while (mt.main.next != &mt.main)
  184. fuse_join_worker(mt.main.next);
  185. err = mt.error;
  186. }
  187. sem_destroy(&mt.finish);
  188. fuse_session_reset(se);
  189. return err;
  190. }