blob: d1e327c9e619817e9c7f42b5e6fa47d8ae5cb7ca [file] [log] [blame]
Miklos Szeredia1482422005-08-14 23:00:27 +00001/*
2 FUSE: Filesystem in Userspace
Miklos Szeredi95da8602006-01-06 18:29:40 +00003 Copyright (C) 2001-2006 Miklos Szeredi <miklos@szeredi.hu>
Miklos Szeredia1482422005-08-14 23:00:27 +00004
5 This program can be distributed under the terms of the GNU LGPL.
6 See the file COPYING.LIB.
7*/
8
9#include "fuse_lowlevel.h"
Miklos Szeredi38f152c2006-09-03 18:28:52 +000010#include "fuse_misc.h"
Miklos Szeredia1482422005-08-14 23:00:27 +000011
12#include <stdio.h>
13#include <stdlib.h>
14#include <string.h>
Miklos Szeredia1482422005-08-14 23:00:27 +000015#include <unistd.h>
16#include <signal.h>
17#include <errno.h>
18#include <sys/time.h>
19
Miklos Szeredia1482422005-08-14 23:00:27 +000020struct fuse_worker {
Miklos Szeredi38f152c2006-09-03 18:28:52 +000021 struct fuse_worker *prev;
22 struct fuse_worker *next;
23 pthread_t thread_id;
24 size_t bufsize;
25 char *buf;
26 struct fuse_mt *mt;
27};
28
29struct fuse_mt {
Miklos Szeredia1482422005-08-14 23:00:27 +000030 pthread_mutex_t lock;
31 int numworker;
32 int numavail;
33 struct fuse_session *se;
Miklos Szeredia1482422005-08-14 23:00:27 +000034 struct fuse_chan *prevch;
Miklos Szeredi38f152c2006-09-03 18:28:52 +000035 struct fuse_worker main;
Miklos Szeredi8393e3d2005-11-21 11:37:01 +000036 int exit;
Miklos Szeredia1482422005-08-14 23:00:27 +000037 int error;
38};
39
Miklos Szeredi38f152c2006-09-03 18:28:52 +000040static void list_add_worker(struct fuse_worker *w, struct fuse_worker *next)
Miklos Szeredia1482422005-08-14 23:00:27 +000041{
Miklos Szeredi38f152c2006-09-03 18:28:52 +000042 struct fuse_worker *prev = next->prev;
43 w->next = next;
44 w->prev = prev;
45 prev->next = w;
46 next->prev = w;
Miklos Szeredia1482422005-08-14 23:00:27 +000047}
48
Miklos Szeredi38f152c2006-09-03 18:28:52 +000049static void list_del_worker(struct fuse_worker *w)
50{
51 struct fuse_worker *prev = w->prev;
52 struct fuse_worker *next = w->next;
53 prev->next = next;
54 next->prev = prev;
55}
Miklos Szeredia1482422005-08-14 23:00:27 +000056
Miklos Szeredi38f152c2006-09-03 18:28:52 +000057static int fuse_start_thread(struct fuse_mt *mt);
58
59static void *fuse_do_work(void *data)
Miklos Szeredia1482422005-08-14 23:00:27 +000060{
61 struct fuse_worker *w = (struct fuse_worker *) data;
Miklos Szeredi38f152c2006-09-03 18:28:52 +000062 struct fuse_mt *mt = w->mt;
Miklos Szeredia1482422005-08-14 23:00:27 +000063
Miklos Szeredi38f152c2006-09-03 18:28:52 +000064 while (!fuse_session_exited(mt->se)) {
65 struct fuse_chan *ch = mt->prevch;
66 int res = fuse_chan_recv(&ch, w->buf, w->bufsize);
Miklos Szeredi5d9ce362006-03-01 12:10:13 +000067 if (res == -EINTR)
Miklos Szeredia1482422005-08-14 23:00:27 +000068 continue;
Miklos Szeredi5d9ce362006-03-01 12:10:13 +000069 if (res <= 0) {
70 if (res < 0) {
Miklos Szeredi38f152c2006-09-03 18:28:52 +000071 fuse_session_exit(mt->se);
72 mt->error = -1;
Miklos Szeredi5d9ce362006-03-01 12:10:13 +000073 }
Miklos Szeredi0d41f2f2005-09-12 16:40:26 +000074 break;
Miklos Szeredia1482422005-08-14 23:00:27 +000075 }
76
Miklos Szeredi38f152c2006-09-03 18:28:52 +000077 pthread_mutex_lock(&mt->lock);
78 if (mt->exit) {
79 pthread_mutex_unlock(&mt->lock);
80 return NULL;
Miklos Szeredi8393e3d2005-11-21 11:37:01 +000081 }
Miklos Szeredi38f152c2006-09-03 18:28:52 +000082 mt->numavail--;
83 if (mt->numavail == 0)
84 fuse_start_thread(mt);
85 pthread_mutex_unlock(&mt->lock);
Miklos Szeredia1482422005-08-14 23:00:27 +000086
Miklos Szeredi38f152c2006-09-03 18:28:52 +000087 fuse_session_process(mt->se, w->buf, res, ch);
88
89 pthread_mutex_lock(&mt->lock);
90 mt->numavail ++;
91 if (mt->numavail > 10) {
92 if (mt->exit) {
93 pthread_mutex_unlock(&mt->lock);
94 return NULL;
95 }
96 list_del_worker(w);
97 mt->numavail--;
98 mt->numworker--;
99 pthread_mutex_unlock(&mt->lock);
100
101 pthread_detach(w->thread_id);
102 free(w->buf);
103 free(w);
104 return NULL;
105 }
106 pthread_mutex_unlock(&mt->lock);
Miklos Szeredi40d7b382005-11-29 20:07:23 +0000107 }
Miklos Szeredia1482422005-08-14 23:00:27 +0000108
Miklos Szeredi38f152c2006-09-03 18:28:52 +0000109 pthread_kill(mt->main.thread_id, SIGHUP);
110 pause();
111
Miklos Szeredia1482422005-08-14 23:00:27 +0000112 return NULL;
113}
114
Miklos Szeredi38f152c2006-09-03 18:28:52 +0000115static int fuse_start_thread(struct fuse_mt *mt)
Miklos Szeredia1482422005-08-14 23:00:27 +0000116{
Miklos Szeredia90b7342005-11-27 19:22:42 +0000117 sigset_t oldset;
118 sigset_t newset;
119 int res;
Miklos Szeredi38f152c2006-09-03 18:28:52 +0000120 struct fuse_worker *w = malloc(sizeof(struct fuse_worker));
121 if (!w) {
122 fprintf(stderr, "fuse: failed to allocate worker structure\n");
123 return -1;
124 }
125 memset(w, 0, sizeof(struct fuse_worker));
126 w->bufsize = fuse_chan_bufsize(mt->prevch);
127 w->buf = malloc(w->bufsize);
128 w->mt = mt;
129 if (!w->buf) {
130 fprintf(stderr, "fuse: failed to allocate read buffer\n");
131 free(w);
132 return -1;
133 }
Miklos Szeredia90b7342005-11-27 19:22:42 +0000134
135 /* Disallow signal reception in worker threads */
136 sigemptyset(&newset);
137 sigaddset(&newset, SIGTERM);
138 sigaddset(&newset, SIGINT);
139 sigaddset(&newset, SIGHUP);
140 sigaddset(&newset, SIGQUIT);
141 pthread_sigmask(SIG_BLOCK, &newset, &oldset);
Miklos Szeredi38f152c2006-09-03 18:28:52 +0000142 res = pthread_create(&w->thread_id, NULL, fuse_do_work, w);
Miklos Szeredia90b7342005-11-27 19:22:42 +0000143 pthread_sigmask(SIG_SETMASK, &oldset, NULL);
Miklos Szeredia1482422005-08-14 23:00:27 +0000144 if (res != 0) {
145 fprintf(stderr, "fuse: error creating thread: %s\n", strerror(res));
146 return -1;
147 }
Miklos Szeredi38f152c2006-09-03 18:28:52 +0000148 list_add_worker(w, &mt->main);
149 mt->numavail ++;
150 mt->numworker ++;
Miklos Szeredia1482422005-08-14 23:00:27 +0000151
Miklos Szeredia1482422005-08-14 23:00:27 +0000152 return 0;
153}
154
Miklos Szeredi38f152c2006-09-03 18:28:52 +0000155static void fuse_join_worker(struct fuse_mt *mt, struct fuse_worker *w)
156{
157 pthread_join(w->thread_id, NULL);
158 pthread_mutex_lock(&mt->lock);
159 list_del_worker(w);
160 pthread_mutex_unlock(&mt->lock);
161 free(w->buf);
162 free(w);
163}
164
Miklos Szeredia1482422005-08-14 23:00:27 +0000165int fuse_session_loop_mt(struct fuse_session *se)
166{
Miklos Szeredia1482422005-08-14 23:00:27 +0000167 int err;
Miklos Szeredi38f152c2006-09-03 18:28:52 +0000168 struct fuse_mt mt;
Miklos Szeredia1482422005-08-14 23:00:27 +0000169 struct fuse_worker *w;
Miklos Szeredia1482422005-08-14 23:00:27 +0000170
Miklos Szeredi38f152c2006-09-03 18:28:52 +0000171 memset(&mt, 0, sizeof(struct fuse_mt));
172 mt.se = se;
173 mt.prevch = fuse_session_next_chan(se, NULL);
174 mt.error = 0;
175 mt.numworker = 0;
176 mt.numavail = 0;
177 mt.main.thread_id = pthread_self();
178 mt.main.prev = mt.main.next = &mt.main;
179 fuse_mutex_init(&mt.lock);
Miklos Szeredia1482422005-08-14 23:00:27 +0000180
Miklos Szeredi38f152c2006-09-03 18:28:52 +0000181 pthread_mutex_lock(&mt.lock);
Miklos Szeredi288ed4e2006-09-07 06:02:44 +0000182 err = fuse_start_thread(&mt);
Miklos Szeredi38f152c2006-09-03 18:28:52 +0000183 pthread_mutex_unlock(&mt.lock);
Miklos Szeredi288ed4e2006-09-07 06:02:44 +0000184 if (!err) {
185 while (!fuse_session_exited(se))
186 pause();
Miklos Szeredi38f152c2006-09-03 18:28:52 +0000187
Miklos Szeredi288ed4e2006-09-07 06:02:44 +0000188 for (w = mt.main.next; w != &mt.main; w = w->next)
189 pthread_cancel(w->thread_id);
190 mt.exit = 1;
191 pthread_mutex_unlock(&mt.lock);
Miklos Szeredi38f152c2006-09-03 18:28:52 +0000192
Miklos Szeredi288ed4e2006-09-07 06:02:44 +0000193 while (mt.main.next != &mt.main)
194 fuse_join_worker(&mt, mt.main.next);
195
196 err = mt.error;
197 }
Miklos Szeredi38f152c2006-09-03 18:28:52 +0000198
199 pthread_mutex_destroy(&mt.lock);
Miklos Szeredia1482422005-08-14 23:00:27 +0000200 fuse_session_reset(se);
201 return err;
202}