bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1 | /* |
| 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_i.h" |
| 10 | #include "fuse_misc.h" |
| 11 | #include "fuse_lowlevel.h" |
| 12 | |
| 13 | #include <stdio.h> |
| 14 | #include <stdlib.h> |
| 15 | #include <string.h> |
| 16 | #include <pthread.h> |
| 17 | #include <assert.h> |
| 18 | |
| 19 | struct procdata { |
| 20 | struct fuse *f; |
| 21 | struct fuse_chan *prevch; |
| 22 | struct fuse_session *prevse; |
| 23 | fuse_processor_t proc; |
| 24 | void *data; |
| 25 | }; |
| 26 | |
| 27 | #ifdef __MULTI_THREAD |
| 28 | |
| 29 | static void mt_session_proc(void *data, const char *buf, size_t len, |
| 30 | struct fuse_chan *ch) |
| 31 | { |
| 32 | struct procdata *pd = (struct procdata *) data; |
| 33 | struct fuse_cmd *cmd = *(struct fuse_cmd **) buf; |
| 34 | |
| 35 | (void) len; |
| 36 | (void) ch; |
| 37 | pd->proc(pd->f, cmd, pd->data); |
| 38 | } |
| 39 | |
| 40 | static void mt_session_exit(void *data, int val) |
| 41 | { |
| 42 | struct procdata *pd = (struct procdata *) data; |
| 43 | if (val) |
| 44 | fuse_session_exit(pd->prevse); |
| 45 | else |
| 46 | fuse_session_reset(pd->prevse); |
| 47 | } |
| 48 | |
| 49 | static int mt_session_exited(void *data) |
| 50 | { |
| 51 | struct procdata *pd = (struct procdata *) data; |
| 52 | return fuse_session_exited(pd->prevse); |
| 53 | } |
| 54 | |
| 55 | static int mt_chan_receive(struct fuse_chan **chp, char *buf, size_t size) |
| 56 | { |
| 57 | struct fuse_cmd *cmd; |
| 58 | struct procdata *pd = (struct procdata *) fuse_chan_data(*chp); |
| 59 | |
| 60 | assert(size >= sizeof(cmd)); |
| 61 | |
| 62 | cmd = fuse_read_cmd(pd->f); |
| 63 | if (cmd == NULL) |
| 64 | return 0; |
| 65 | |
| 66 | *(struct fuse_cmd **) buf = cmd; |
| 67 | |
| 68 | return sizeof(cmd); |
| 69 | } |
| 70 | |
| 71 | int fuse_loop_mt_proc(struct fuse *f, fuse_processor_t proc, void *data) |
| 72 | { |
| 73 | int res; |
| 74 | struct procdata pd; |
| 75 | struct fuse_session *prevse = fuse_get_session(f); |
| 76 | struct fuse_session *se; |
| 77 | struct fuse_chan *prevch = fuse_session_next_chan(prevse, NULL); |
| 78 | struct fuse_chan *ch; |
| 79 | struct fuse_session_ops sop = { |
| 80 | .exit = mt_session_exit, |
| 81 | .exited = mt_session_exited, |
| 82 | .process = mt_session_proc, |
| 83 | }; |
| 84 | struct fuse_chan_ops cop = { |
| 85 | .receive = mt_chan_receive, |
| 86 | }; |
| 87 | |
| 88 | pd.f = f; |
| 89 | pd.prevch = prevch; |
| 90 | pd.prevse = prevse; |
| 91 | pd.proc = proc; |
| 92 | pd.data = data; |
| 93 | |
| 94 | se = fuse_session_new(&sop, &pd); |
| 95 | if (se == NULL) |
| 96 | return -1; |
| 97 | |
| 98 | ch = fuse_chan_new(&cop, fuse_chan_fd(prevch), |
| 99 | sizeof(struct fuse_cmd *), &pd); |
| 100 | if (ch == NULL) { |
| 101 | fuse_session_destroy(se); |
| 102 | return -1; |
| 103 | } |
| 104 | fuse_session_add_chan(se, ch); |
| 105 | res = fuse_session_loop_mt(se); |
| 106 | fuse_session_destroy(se); |
| 107 | return res; |
| 108 | } |
| 109 | |
| 110 | int fuse_loop_mt(struct fuse *f) |
| 111 | { |
| 112 | if (f == NULL) |
| 113 | return -1; |
| 114 | |
| 115 | return fuse_session_loop_mt(fuse_get_session(f)); |
| 116 | } |
| 117 | |
| 118 | FUSE_SYMVER(".symver fuse_loop_mt_proc,__fuse_loop_mt@"); |
| 119 | |
| 120 | #endif |