Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 1 | /* |
| 2 | FUSE: Filesystem in Userspace |
Miklos Szeredi | 95da860 | 2006-01-06 18:29:40 +0000 | [diff] [blame] | 3 | Copyright (C) 2001-2006 Miklos Szeredi <miklos@szeredi.hu> |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 4 | |
| 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 Szeredi | 38f152c | 2006-09-03 18:28:52 +0000 | [diff] [blame] | 10 | #include "fuse_misc.h" |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 11 | |
| 12 | #include <stdio.h> |
| 13 | #include <stdlib.h> |
| 14 | #include <string.h> |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 15 | #include <unistd.h> |
| 16 | #include <signal.h> |
| 17 | #include <errno.h> |
| 18 | #include <sys/time.h> |
| 19 | |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 20 | struct fuse_worker { |
Miklos Szeredi | 38f152c | 2006-09-03 18:28:52 +0000 | [diff] [blame] | 21 | 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 | |
| 29 | struct fuse_mt { |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 30 | pthread_mutex_t lock; |
| 31 | int numworker; |
| 32 | int numavail; |
| 33 | struct fuse_session *se; |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 34 | struct fuse_chan *prevch; |
Miklos Szeredi | 38f152c | 2006-09-03 18:28:52 +0000 | [diff] [blame] | 35 | struct fuse_worker main; |
Miklos Szeredi | 8393e3d | 2005-11-21 11:37:01 +0000 | [diff] [blame] | 36 | int exit; |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 37 | int error; |
| 38 | }; |
| 39 | |
Miklos Szeredi | 38f152c | 2006-09-03 18:28:52 +0000 | [diff] [blame] | 40 | static void list_add_worker(struct fuse_worker *w, struct fuse_worker *next) |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 41 | { |
Miklos Szeredi | 38f152c | 2006-09-03 18:28:52 +0000 | [diff] [blame] | 42 | struct fuse_worker *prev = next->prev; |
| 43 | w->next = next; |
| 44 | w->prev = prev; |
| 45 | prev->next = w; |
| 46 | next->prev = w; |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 47 | } |
| 48 | |
Miklos Szeredi | 38f152c | 2006-09-03 18:28:52 +0000 | [diff] [blame] | 49 | static 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 Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 56 | |
Miklos Szeredi | 38f152c | 2006-09-03 18:28:52 +0000 | [diff] [blame] | 57 | static int fuse_start_thread(struct fuse_mt *mt); |
| 58 | |
| 59 | static void *fuse_do_work(void *data) |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 60 | { |
| 61 | struct fuse_worker *w = (struct fuse_worker *) data; |
Miklos Szeredi | 38f152c | 2006-09-03 18:28:52 +0000 | [diff] [blame] | 62 | struct fuse_mt *mt = w->mt; |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 63 | |
Miklos Szeredi | 38f152c | 2006-09-03 18:28:52 +0000 | [diff] [blame] | 64 | 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 Szeredi | 5d9ce36 | 2006-03-01 12:10:13 +0000 | [diff] [blame] | 67 | if (res == -EINTR) |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 68 | continue; |
Miklos Szeredi | 5d9ce36 | 2006-03-01 12:10:13 +0000 | [diff] [blame] | 69 | if (res <= 0) { |
| 70 | if (res < 0) { |
Miklos Szeredi | 38f152c | 2006-09-03 18:28:52 +0000 | [diff] [blame] | 71 | fuse_session_exit(mt->se); |
| 72 | mt->error = -1; |
Miklos Szeredi | 5d9ce36 | 2006-03-01 12:10:13 +0000 | [diff] [blame] | 73 | } |
Miklos Szeredi | 0d41f2f | 2005-09-12 16:40:26 +0000 | [diff] [blame] | 74 | break; |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Miklos Szeredi | 38f152c | 2006-09-03 18:28:52 +0000 | [diff] [blame] | 77 | pthread_mutex_lock(&mt->lock); |
| 78 | if (mt->exit) { |
| 79 | pthread_mutex_unlock(&mt->lock); |
| 80 | return NULL; |
Miklos Szeredi | 8393e3d | 2005-11-21 11:37:01 +0000 | [diff] [blame] | 81 | } |
Miklos Szeredi | 38f152c | 2006-09-03 18:28:52 +0000 | [diff] [blame] | 82 | mt->numavail--; |
| 83 | if (mt->numavail == 0) |
| 84 | fuse_start_thread(mt); |
| 85 | pthread_mutex_unlock(&mt->lock); |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 86 | |
Miklos Szeredi | 38f152c | 2006-09-03 18:28:52 +0000 | [diff] [blame] | 87 | 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 Szeredi | 40d7b38 | 2005-11-29 20:07:23 +0000 | [diff] [blame] | 107 | } |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 108 | |
Miklos Szeredi | 38f152c | 2006-09-03 18:28:52 +0000 | [diff] [blame] | 109 | pthread_kill(mt->main.thread_id, SIGHUP); |
| 110 | pause(); |
| 111 | |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 112 | return NULL; |
| 113 | } |
| 114 | |
Miklos Szeredi | 38f152c | 2006-09-03 18:28:52 +0000 | [diff] [blame] | 115 | static int fuse_start_thread(struct fuse_mt *mt) |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 116 | { |
Miklos Szeredi | a90b734 | 2005-11-27 19:22:42 +0000 | [diff] [blame] | 117 | sigset_t oldset; |
| 118 | sigset_t newset; |
| 119 | int res; |
Miklos Szeredi | 38f152c | 2006-09-03 18:28:52 +0000 | [diff] [blame] | 120 | 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 Szeredi | a90b734 | 2005-11-27 19:22:42 +0000 | [diff] [blame] | 134 | |
| 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 Szeredi | 38f152c | 2006-09-03 18:28:52 +0000 | [diff] [blame] | 142 | res = pthread_create(&w->thread_id, NULL, fuse_do_work, w); |
Miklos Szeredi | a90b734 | 2005-11-27 19:22:42 +0000 | [diff] [blame] | 143 | pthread_sigmask(SIG_SETMASK, &oldset, NULL); |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 144 | if (res != 0) { |
| 145 | fprintf(stderr, "fuse: error creating thread: %s\n", strerror(res)); |
| 146 | return -1; |
| 147 | } |
Miklos Szeredi | 38f152c | 2006-09-03 18:28:52 +0000 | [diff] [blame] | 148 | list_add_worker(w, &mt->main); |
| 149 | mt->numavail ++; |
| 150 | mt->numworker ++; |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 151 | |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 152 | return 0; |
| 153 | } |
| 154 | |
Miklos Szeredi | 38f152c | 2006-09-03 18:28:52 +0000 | [diff] [blame] | 155 | static 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 Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 165 | int fuse_session_loop_mt(struct fuse_session *se) |
| 166 | { |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 167 | int err; |
Miklos Szeredi | 38f152c | 2006-09-03 18:28:52 +0000 | [diff] [blame] | 168 | struct fuse_mt mt; |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 169 | struct fuse_worker *w; |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 170 | |
Miklos Szeredi | 38f152c | 2006-09-03 18:28:52 +0000 | [diff] [blame] | 171 | 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 Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 180 | |
Miklos Szeredi | 38f152c | 2006-09-03 18:28:52 +0000 | [diff] [blame] | 181 | pthread_mutex_lock(&mt.lock); |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 182 | err = fuse_start_thread(&mt); |
Miklos Szeredi | 38f152c | 2006-09-03 18:28:52 +0000 | [diff] [blame] | 183 | pthread_mutex_unlock(&mt.lock); |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 184 | if (!err) { |
| 185 | while (!fuse_session_exited(se)) |
| 186 | pause(); |
Miklos Szeredi | 38f152c | 2006-09-03 18:28:52 +0000 | [diff] [blame] | 187 | |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 188 | 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 Szeredi | 38f152c | 2006-09-03 18:28:52 +0000 | [diff] [blame] | 192 | |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 193 | while (mt.main.next != &mt.main) |
| 194 | fuse_join_worker(&mt, mt.main.next); |
| 195 | |
| 196 | err = mt.error; |
| 197 | } |
Miklos Szeredi | 38f152c | 2006-09-03 18:28:52 +0000 | [diff] [blame] | 198 | |
| 199 | pthread_mutex_destroy(&mt.lock); |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 200 | fuse_session_reset(se); |
| 201 | return err; |
| 202 | } |