blob: aefc3ec9828dc409c4f832a99ef66e0339623cc0 [file] [log] [blame]
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001/*
2 FUSE: Filesystem in Userspace
3 Copyright (C) 2001-2007 Miklos Szeredi <miklos@szeredi.hu>
4
5 This program can be distributed under the terms of the GNU LGPLv2.
6 See the file COPYING.LIB.
7*/
8
9#include "fuse_lowlevel.h"
10#include "fuse_misc.h"
11#include "fuse_kernel.h"
Dees_Troye34c1332013-02-06 19:13:00 +000012#include "fuse_i.h"
bigbiff bigbiff9c754052013-01-09 09:09:08 -050013
14#include <stdio.h>
15#include <stdlib.h>
16#include <string.h>
17#include <unistd.h>
18#include <signal.h>
19#include <semaphore.h>
20#include <errno.h>
21#include <sys/time.h>
Dees_Troye34c1332013-02-06 19:13:00 +000022#include <pthread.h>
bigbiff bigbiff9c754052013-01-09 09:09:08 -050023
24/* Environment var controlling the thread stack size */
25#define ENVNAME_THREAD_STACK "FUSE_THREAD_STACK"
26
27struct fuse_worker {
28 struct fuse_worker *prev;
29 struct fuse_worker *next;
30 pthread_t thread_id;
31 size_t bufsize;
32 char *buf;
33 struct fuse_mt *mt;
34};
35
36struct fuse_mt {
37 pthread_mutex_t lock;
38 int numworker;
39 int numavail;
40 struct fuse_session *se;
41 struct fuse_chan *prevch;
42 struct fuse_worker main;
43 sem_t finish;
44 int exit;
45 int error;
46};
47
48static void list_add_worker(struct fuse_worker *w, struct fuse_worker *next)
49{
50 struct fuse_worker *prev = next->prev;
51 w->next = next;
52 w->prev = prev;
53 prev->next = w;
54 next->prev = w;
55}
56
57static void list_del_worker(struct fuse_worker *w)
58{
59 struct fuse_worker *prev = w->prev;
60 struct fuse_worker *next = w->next;
61 prev->next = next;
62 next->prev = prev;
63}
64
65#define PTHREAD_CANCEL_ENABLE 0
66#define PTHREAD_CANCEL_DISABLE 1
67
Dees_Troye34c1332013-02-06 19:13:00 +000068static int fuse_loop_start_thread(struct fuse_mt *mt);
bigbiff bigbiff9c754052013-01-09 09:09:08 -050069
70static void *fuse_do_work(void *data)
71{
72 struct fuse_worker *w = (struct fuse_worker *) data;
73 struct fuse_mt *mt = w->mt;
74
75 while (!fuse_session_exited(mt->se)) {
76 int isforget = 0;
77 struct fuse_chan *ch = mt->prevch;
Dees_Troye34c1332013-02-06 19:13:00 +000078 struct fuse_buf fbuf = {
79 .mem = w->buf,
80 .size = w->bufsize,
81 };
bigbiff bigbiff9c754052013-01-09 09:09:08 -050082 int res;
83
Flemmard60bf94e2014-03-10 20:18:45 +010084#ifndef ANDROID
bigbiff bigbiff9c754052013-01-09 09:09:08 -050085 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
Flemmard60bf94e2014-03-10 20:18:45 +010086#endif
Dees_Troye34c1332013-02-06 19:13:00 +000087 res = fuse_session_receive_buf(mt->se, &fbuf, &ch);
Flemmard60bf94e2014-03-10 20:18:45 +010088#ifndef ANDROID
bigbiff bigbiff9c754052013-01-09 09:09:08 -050089 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
Flemmard60bf94e2014-03-10 20:18:45 +010090#endif
bigbiff bigbiff9c754052013-01-09 09:09:08 -050091 if (res == -EINTR)
92 continue;
93 if (res <= 0) {
94 if (res < 0) {
95 fuse_session_exit(mt->se);
96 mt->error = -1;
97 }
98 break;
99 }
100
101 pthread_mutex_lock(&mt->lock);
102 if (mt->exit) {
103 pthread_mutex_unlock(&mt->lock);
104 return NULL;
105 }
106
107 /*
108 * This disgusting hack is needed so that zillions of threads
109 * are not created on a burst of FORGET messages
110 */
Dees_Troye34c1332013-02-06 19:13:00 +0000111 if (!(fbuf.flags & FUSE_BUF_IS_FD)) {
112 struct fuse_in_header *in = fbuf.mem;
113
114 if (in->opcode == FUSE_FORGET ||
115 in->opcode == FUSE_BATCH_FORGET)
116 isforget = 1;
117 }
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500118
119 if (!isforget)
120 mt->numavail--;
121 if (mt->numavail == 0)
Dees_Troye34c1332013-02-06 19:13:00 +0000122 fuse_loop_start_thread(mt);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500123 pthread_mutex_unlock(&mt->lock);
124
Dees_Troye34c1332013-02-06 19:13:00 +0000125 fuse_session_process_buf(mt->se, &fbuf, ch);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500126
127 pthread_mutex_lock(&mt->lock);
128 if (!isforget)
129 mt->numavail++;
130 if (mt->numavail > 10) {
131 if (mt->exit) {
132 pthread_mutex_unlock(&mt->lock);
133 return NULL;
134 }
135 list_del_worker(w);
136 mt->numavail--;
137 mt->numworker--;
138 pthread_mutex_unlock(&mt->lock);
139
140 pthread_detach(w->thread_id);
141 free(w->buf);
142 free(w);
143 return NULL;
144 }
145 pthread_mutex_unlock(&mt->lock);
146 }
147
148 sem_post(&mt->finish);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500149
150 return NULL;
151}
152
Dees_Troye34c1332013-02-06 19:13:00 +0000153int fuse_start_thread(pthread_t *thread_id, void *(*func)(void *), void *arg)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500154{
155 sigset_t oldset;
156 sigset_t newset;
157 int res;
158 pthread_attr_t attr;
159 char *stack_size;
Dees_Troye34c1332013-02-06 19:13:00 +0000160
161 /* Override default stack size */
162 pthread_attr_init(&attr);
163 stack_size = getenv(ENVNAME_THREAD_STACK);
164 if (stack_size && pthread_attr_setstacksize(&attr, atoi(stack_size)))
165 fprintf(stderr, "fuse: invalid stack size: %s\n", stack_size);
166
167 /* Disallow signal reception in worker threads */
168 sigemptyset(&newset);
169 sigaddset(&newset, SIGTERM);
170 sigaddset(&newset, SIGINT);
171 sigaddset(&newset, SIGHUP);
172 sigaddset(&newset, SIGQUIT);
173 pthread_sigmask(SIG_BLOCK, &newset, &oldset);
174 res = pthread_create(thread_id, &attr, func, arg);
175 pthread_sigmask(SIG_SETMASK, &oldset, NULL);
176 pthread_attr_destroy(&attr);
177 if (res != 0) {
178 fprintf(stderr, "fuse: error creating thread: %s\n",
179 strerror(res));
180 return -1;
181 }
182
183 return 0;
184}
185
186static int fuse_loop_start_thread(struct fuse_mt *mt)
187{
188 int res;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500189 struct fuse_worker *w = malloc(sizeof(struct fuse_worker));
190 if (!w) {
191 fprintf(stderr, "fuse: failed to allocate worker structure\n");
192 return -1;
193 }
194 memset(w, 0, sizeof(struct fuse_worker));
195 w->bufsize = fuse_chan_bufsize(mt->prevch);
196 w->buf = malloc(w->bufsize);
197 w->mt = mt;
198 if (!w->buf) {
199 fprintf(stderr, "fuse: failed to allocate read buffer\n");
200 free(w);
201 return -1;
202 }
203
Dees_Troye34c1332013-02-06 19:13:00 +0000204 res = fuse_start_thread(&w->thread_id, fuse_do_work, w);
205 if (res == -1) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500206 free(w->buf);
207 free(w);
208 return -1;
209 }
210 list_add_worker(w, &mt->main);
211 mt->numavail ++;
212 mt->numworker ++;
213
214 return 0;
215}
216
217static void fuse_join_worker(struct fuse_mt *mt, struct fuse_worker *w)
218{
219 pthread_join(w->thread_id, NULL);
220 pthread_mutex_lock(&mt->lock);
221 list_del_worker(w);
222 pthread_mutex_unlock(&mt->lock);
223 free(w->buf);
224 free(w);
225}
226
227int fuse_session_loop_mt(struct fuse_session *se)
228{
229 int err;
230 struct fuse_mt mt;
231 struct fuse_worker *w;
232
233 memset(&mt, 0, sizeof(struct fuse_mt));
234 mt.se = se;
235 mt.prevch = fuse_session_next_chan(se, NULL);
236 mt.error = 0;
237 mt.numworker = 0;
238 mt.numavail = 0;
239 mt.main.thread_id = pthread_self();
240 mt.main.prev = mt.main.next = &mt.main;
241 sem_init(&mt.finish, 0, 0);
242 fuse_mutex_init(&mt.lock);
243
244 pthread_mutex_lock(&mt.lock);
Dees_Troye34c1332013-02-06 19:13:00 +0000245 err = fuse_loop_start_thread(&mt);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500246 pthread_mutex_unlock(&mt.lock);
247 if (!err) {
248 /* sem_wait() is interruptible */
249 while (!fuse_session_exited(se))
250 sem_wait(&mt.finish);
251
Flemmard60bf94e2014-03-10 20:18:45 +0100252#ifndef ANDROID
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500253 for (w = mt.main.next; w != &mt.main; w = w->next)
254 pthread_cancel(w->thread_id);
Flemmard60bf94e2014-03-10 20:18:45 +0100255#else
256 for (w = mt.main.next; w != &mt.main; w = w->next)
257 pthread_kill(w->thread_id, SIGUSR1);
258#endif
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500259 mt.exit = 1;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500260
261 while (mt.main.next != &mt.main)
262 fuse_join_worker(&mt, mt.main.next);
263
264 err = mt.error;
265 }
266
267 pthread_mutex_destroy(&mt.lock);
268 sem_destroy(&mt.finish);
269 fuse_session_reset(se);
270 return err;
271}