blob: 90fc1e69469a7897af834bd2002bccd574e81b1c [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>
bigbiff bigbiff9c754052013-01-09 09:09:08 -050022
23/* Environment var controlling the thread stack size */
24#define ENVNAME_THREAD_STACK "FUSE_THREAD_STACK"
25
26struct fuse_worker {
27 struct fuse_worker *prev;
28 struct fuse_worker *next;
29 pthread_t thread_id;
30 size_t bufsize;
31 char *buf;
32 struct fuse_mt *mt;
33};
34
35struct fuse_mt {
36 pthread_mutex_t lock;
37 int numworker;
38 int numavail;
39 struct fuse_session *se;
40 struct fuse_chan *prevch;
41 struct fuse_worker main;
42 sem_t finish;
43 int exit;
44 int error;
45};
46
47static void list_add_worker(struct fuse_worker *w, struct fuse_worker *next)
48{
49 struct fuse_worker *prev = next->prev;
50 w->next = next;
51 w->prev = prev;
52 prev->next = w;
53 next->prev = w;
54}
55
56static void list_del_worker(struct fuse_worker *w)
57{
58 struct fuse_worker *prev = w->prev;
59 struct fuse_worker *next = w->next;
60 prev->next = next;
61 next->prev = prev;
62}
63
Dees_Troye34c1332013-02-06 19:13:00 +000064static int fuse_loop_start_thread(struct fuse_mt *mt);
bigbiff bigbiff9c754052013-01-09 09:09:08 -050065
Matt Mower523a0592015-12-13 11:31:00 -060066static void thread_exit_handler(int sig)
67{
68 pthread_exit(0);
69}
70
bigbiff bigbiff9c754052013-01-09 09:09:08 -050071static void *fuse_do_work(void *data)
72{
73 struct fuse_worker *w = (struct fuse_worker *) data;
74 struct fuse_mt *mt = w->mt;
75
Matt Mower523a0592015-12-13 11:31:00 -060076#if defined(__ANDROID__)
77 struct sigaction actions;
78 memset(&actions, 0, sizeof(actions));
79 sigemptyset(&actions.sa_mask);
80 actions.sa_flags = 0;
81 actions.sa_handler = thread_exit_handler;
82 sigaction(SIGUSR1, &actions, NULL);
83
84 sigset_t setusr1;
85 sigemptyset(&setusr1);
86 sigaddset(&setusr1, SIGUSR1);
87 pthread_sigmask(SIG_BLOCK, &setusr1, NULL);
88#endif
89
bigbiff bigbiff9c754052013-01-09 09:09:08 -050090 while (!fuse_session_exited(mt->se)) {
91 int isforget = 0;
92 struct fuse_chan *ch = mt->prevch;
Dees_Troye34c1332013-02-06 19:13:00 +000093 struct fuse_buf fbuf = {
94 .mem = w->buf,
95 .size = w->bufsize,
96 };
bigbiff bigbiff9c754052013-01-09 09:09:08 -050097 int res;
98
Matt Mower523a0592015-12-13 11:31:00 -060099#if defined(__ANDROID__)
100 pthread_sigmask(SIG_UNBLOCK, &setusr1, NULL);
101#else
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500102 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
Flemmard60bf94e2014-03-10 20:18:45 +0100103#endif
Dees_Troye34c1332013-02-06 19:13:00 +0000104 res = fuse_session_receive_buf(mt->se, &fbuf, &ch);
Matt Mower523a0592015-12-13 11:31:00 -0600105#if defined(__ANDROID__)
106 pthread_sigmask(SIG_BLOCK, &setusr1, NULL);
107#else
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500108 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
Flemmard60bf94e2014-03-10 20:18:45 +0100109#endif
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500110 if (res == -EINTR)
111 continue;
112 if (res <= 0) {
113 if (res < 0) {
114 fuse_session_exit(mt->se);
115 mt->error = -1;
116 }
117 break;
118 }
119
120 pthread_mutex_lock(&mt->lock);
121 if (mt->exit) {
122 pthread_mutex_unlock(&mt->lock);
123 return NULL;
124 }
125
126 /*
127 * This disgusting hack is needed so that zillions of threads
128 * are not created on a burst of FORGET messages
129 */
Dees_Troye34c1332013-02-06 19:13:00 +0000130 if (!(fbuf.flags & FUSE_BUF_IS_FD)) {
131 struct fuse_in_header *in = fbuf.mem;
132
133 if (in->opcode == FUSE_FORGET ||
134 in->opcode == FUSE_BATCH_FORGET)
135 isforget = 1;
136 }
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500137
138 if (!isforget)
139 mt->numavail--;
140 if (mt->numavail == 0)
Dees_Troye34c1332013-02-06 19:13:00 +0000141 fuse_loop_start_thread(mt);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500142 pthread_mutex_unlock(&mt->lock);
143
Dees_Troye34c1332013-02-06 19:13:00 +0000144 fuse_session_process_buf(mt->se, &fbuf, ch);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500145
146 pthread_mutex_lock(&mt->lock);
147 if (!isforget)
148 mt->numavail++;
149 if (mt->numavail > 10) {
150 if (mt->exit) {
151 pthread_mutex_unlock(&mt->lock);
152 return NULL;
153 }
154 list_del_worker(w);
155 mt->numavail--;
156 mt->numworker--;
157 pthread_mutex_unlock(&mt->lock);
158
159 pthread_detach(w->thread_id);
160 free(w->buf);
161 free(w);
162 return NULL;
163 }
164 pthread_mutex_unlock(&mt->lock);
165 }
166
167 sem_post(&mt->finish);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500168
169 return NULL;
170}
171
Dees_Troye34c1332013-02-06 19:13:00 +0000172int fuse_start_thread(pthread_t *thread_id, void *(*func)(void *), void *arg)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500173{
174 sigset_t oldset;
175 sigset_t newset;
176 int res;
177 pthread_attr_t attr;
178 char *stack_size;
Dees_Troye34c1332013-02-06 19:13:00 +0000179
180 /* Override default stack size */
181 pthread_attr_init(&attr);
182 stack_size = getenv(ENVNAME_THREAD_STACK);
183 if (stack_size && pthread_attr_setstacksize(&attr, atoi(stack_size)))
184 fprintf(stderr, "fuse: invalid stack size: %s\n", stack_size);
185
186 /* Disallow signal reception in worker threads */
187 sigemptyset(&newset);
188 sigaddset(&newset, SIGTERM);
189 sigaddset(&newset, SIGINT);
190 sigaddset(&newset, SIGHUP);
191 sigaddset(&newset, SIGQUIT);
192 pthread_sigmask(SIG_BLOCK, &newset, &oldset);
193 res = pthread_create(thread_id, &attr, func, arg);
194 pthread_sigmask(SIG_SETMASK, &oldset, NULL);
195 pthread_attr_destroy(&attr);
196 if (res != 0) {
197 fprintf(stderr, "fuse: error creating thread: %s\n",
198 strerror(res));
199 return -1;
200 }
201
202 return 0;
203}
204
205static int fuse_loop_start_thread(struct fuse_mt *mt)
206{
207 int res;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500208 struct fuse_worker *w = malloc(sizeof(struct fuse_worker));
209 if (!w) {
210 fprintf(stderr, "fuse: failed to allocate worker structure\n");
211 return -1;
212 }
213 memset(w, 0, sizeof(struct fuse_worker));
214 w->bufsize = fuse_chan_bufsize(mt->prevch);
215 w->buf = malloc(w->bufsize);
216 w->mt = mt;
217 if (!w->buf) {
218 fprintf(stderr, "fuse: failed to allocate read buffer\n");
219 free(w);
220 return -1;
221 }
222
Dees_Troye34c1332013-02-06 19:13:00 +0000223 res = fuse_start_thread(&w->thread_id, fuse_do_work, w);
224 if (res == -1) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500225 free(w->buf);
226 free(w);
227 return -1;
228 }
229 list_add_worker(w, &mt->main);
230 mt->numavail ++;
231 mt->numworker ++;
232
233 return 0;
234}
235
236static void fuse_join_worker(struct fuse_mt *mt, struct fuse_worker *w)
237{
238 pthread_join(w->thread_id, NULL);
239 pthread_mutex_lock(&mt->lock);
240 list_del_worker(w);
241 pthread_mutex_unlock(&mt->lock);
242 free(w->buf);
243 free(w);
244}
245
246int fuse_session_loop_mt(struct fuse_session *se)
247{
248 int err;
249 struct fuse_mt mt;
250 struct fuse_worker *w;
251
252 memset(&mt, 0, sizeof(struct fuse_mt));
253 mt.se = se;
254 mt.prevch = fuse_session_next_chan(se, NULL);
255 mt.error = 0;
256 mt.numworker = 0;
257 mt.numavail = 0;
258 mt.main.thread_id = pthread_self();
259 mt.main.prev = mt.main.next = &mt.main;
260 sem_init(&mt.finish, 0, 0);
261 fuse_mutex_init(&mt.lock);
262
263 pthread_mutex_lock(&mt.lock);
Dees_Troye34c1332013-02-06 19:13:00 +0000264 err = fuse_loop_start_thread(&mt);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500265 pthread_mutex_unlock(&mt.lock);
266 if (!err) {
267 /* sem_wait() is interruptible */
268 while (!fuse_session_exited(se))
269 sem_wait(&mt.finish);
270
Matt Mower523a0592015-12-13 11:31:00 -0600271 pthread_mutex_lock(&mt.lock);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500272 for (w = mt.main.next; w != &mt.main; w = w->next)
Matt Mower523a0592015-12-13 11:31:00 -0600273#if defined(__ANDROID__)
Flemmard60bf94e2014-03-10 20:18:45 +0100274 pthread_kill(w->thread_id, SIGUSR1);
Matt Mower523a0592015-12-13 11:31:00 -0600275#else
276 pthread_cancel(w->thread_id);
Flemmard60bf94e2014-03-10 20:18:45 +0100277#endif
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500278 mt.exit = 1;
Matt Mower523a0592015-12-13 11:31:00 -0600279 pthread_mutex_unlock(&mt.lock);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500280
281 while (mt.main.next != &mt.main)
282 fuse_join_worker(&mt, mt.main.next);
283
284 err = mt.error;
285 }
286
287 pthread_mutex_destroy(&mt.lock);
288 sem_destroy(&mt.finish);
289 fuse_session_reset(se);
290 return err;
291}