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_common_compat.h" |
| 12 | #include "fuse_lowlevel_compat.h" |
| 13 | |
| 14 | #include <stdio.h> |
| 15 | #include <stdlib.h> |
| 16 | #include <string.h> |
| 17 | #include <assert.h> |
| 18 | #include <errno.h> |
| 19 | |
| 20 | struct fuse_chan { |
| 21 | struct fuse_chan_ops op; |
| 22 | |
| 23 | struct fuse_session *se; |
| 24 | |
| 25 | int fd; |
| 26 | |
| 27 | size_t bufsize; |
| 28 | |
| 29 | void *data; |
| 30 | |
| 31 | int compat; |
| 32 | }; |
| 33 | |
| 34 | struct fuse_session *fuse_session_new(struct fuse_session_ops *op, void *data) |
| 35 | { |
| 36 | struct fuse_session *se = (struct fuse_session *) malloc(sizeof(*se)); |
| 37 | if (se == NULL) { |
| 38 | fprintf(stderr, "fuse: failed to allocate session\n"); |
| 39 | return NULL; |
| 40 | } |
| 41 | |
| 42 | memset(se, 0, sizeof(*se)); |
| 43 | se->op = *op; |
| 44 | se->data = data; |
| 45 | |
| 46 | return se; |
| 47 | } |
| 48 | |
| 49 | void fuse_session_add_chan(struct fuse_session *se, struct fuse_chan *ch) |
| 50 | { |
| 51 | assert(se->ch == NULL); |
| 52 | assert(ch->se == NULL); |
| 53 | se->ch = ch; |
| 54 | ch->se = se; |
| 55 | } |
| 56 | |
| 57 | void fuse_session_remove_chan(struct fuse_chan *ch) |
| 58 | { |
| 59 | struct fuse_session *se = ch->se; |
| 60 | if (se) { |
| 61 | assert(se->ch == ch); |
| 62 | se->ch = NULL; |
| 63 | ch->se = NULL; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | struct fuse_chan *fuse_session_next_chan(struct fuse_session *se, |
| 68 | struct fuse_chan *ch) |
| 69 | { |
| 70 | assert(ch == NULL || ch == se->ch); |
| 71 | if (ch == NULL) |
| 72 | return se->ch; |
| 73 | else |
| 74 | return NULL; |
| 75 | } |
| 76 | |
| 77 | void fuse_session_process(struct fuse_session *se, const char *buf, size_t len, |
| 78 | struct fuse_chan *ch) |
| 79 | { |
| 80 | se->op.process(se->data, buf, len, ch); |
| 81 | } |
| 82 | |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 83 | void fuse_session_process_buf(struct fuse_session *se, |
| 84 | const struct fuse_buf *buf, struct fuse_chan *ch) |
| 85 | { |
| 86 | if (se->process_buf) { |
| 87 | se->process_buf(se->data, buf, ch); |
| 88 | } else { |
| 89 | assert(!(buf->flags & FUSE_BUF_IS_FD)); |
| 90 | fuse_session_process(se->data, buf->mem, buf->size, ch); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | int fuse_session_receive_buf(struct fuse_session *se, struct fuse_buf *buf, |
| 95 | struct fuse_chan **chp) |
| 96 | { |
| 97 | int res; |
| 98 | |
| 99 | if (se->receive_buf) { |
| 100 | res = se->receive_buf(se, buf, chp); |
| 101 | } else { |
| 102 | res = fuse_chan_recv(chp, buf->mem, buf->size); |
| 103 | if (res > 0) |
| 104 | buf->size = res; |
| 105 | } |
| 106 | |
| 107 | return res; |
| 108 | } |
| 109 | |
| 110 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 111 | void fuse_session_destroy(struct fuse_session *se) |
| 112 | { |
| 113 | if (se->op.destroy) |
| 114 | se->op.destroy(se->data); |
| 115 | if (se->ch != NULL) |
| 116 | fuse_chan_destroy(se->ch); |
| 117 | free(se); |
| 118 | } |
| 119 | |
| 120 | void fuse_session_exit(struct fuse_session *se) |
| 121 | { |
| 122 | if (se->op.exit) |
| 123 | se->op.exit(se->data, 1); |
| 124 | se->exited = 1; |
| 125 | } |
| 126 | |
| 127 | void fuse_session_reset(struct fuse_session *se) |
| 128 | { |
| 129 | if (se->op.exit) |
| 130 | se->op.exit(se->data, 0); |
| 131 | se->exited = 0; |
| 132 | } |
| 133 | |
| 134 | int fuse_session_exited(struct fuse_session *se) |
| 135 | { |
| 136 | if (se->op.exited) |
| 137 | return se->op.exited(se->data); |
| 138 | else |
| 139 | return se->exited; |
| 140 | } |
| 141 | |
| 142 | void *fuse_session_data(struct fuse_session *se) |
| 143 | { |
| 144 | return se->data; |
| 145 | } |
| 146 | |
| 147 | static struct fuse_chan *fuse_chan_new_common(struct fuse_chan_ops *op, int fd, |
| 148 | size_t bufsize, void *data, |
| 149 | int compat) |
| 150 | { |
| 151 | struct fuse_chan *ch = (struct fuse_chan *) malloc(sizeof(*ch)); |
| 152 | if (ch == NULL) { |
| 153 | fprintf(stderr, "fuse: failed to allocate channel\n"); |
| 154 | return NULL; |
| 155 | } |
| 156 | |
| 157 | memset(ch, 0, sizeof(*ch)); |
| 158 | ch->op = *op; |
| 159 | ch->fd = fd; |
| 160 | ch->bufsize = bufsize; |
| 161 | ch->data = data; |
| 162 | ch->compat = compat; |
| 163 | |
| 164 | return ch; |
| 165 | } |
| 166 | |
| 167 | struct fuse_chan *fuse_chan_new(struct fuse_chan_ops *op, int fd, |
| 168 | size_t bufsize, void *data) |
| 169 | { |
| 170 | return fuse_chan_new_common(op, fd, bufsize, data, 0); |
| 171 | } |
| 172 | |
| 173 | struct fuse_chan *fuse_chan_new_compat24(struct fuse_chan_ops_compat24 *op, |
| 174 | int fd, size_t bufsize, void *data) |
| 175 | { |
| 176 | return fuse_chan_new_common((struct fuse_chan_ops *) op, fd, bufsize, |
| 177 | data, 24); |
| 178 | } |
| 179 | |
| 180 | int fuse_chan_fd(struct fuse_chan *ch) |
| 181 | { |
| 182 | return ch->fd; |
| 183 | } |
| 184 | |
Matt Mower | 523a059 | 2015-12-13 11:31:00 -0600 | [diff] [blame] | 185 | int fuse_chan_clearfd(struct fuse_chan *ch) |
| 186 | { |
| 187 | if (ch == NULL) |
| 188 | return -1; |
| 189 | |
| 190 | int fd = ch->fd; |
| 191 | ch->fd = -1; |
| 192 | return fd; |
| 193 | } |
| 194 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 195 | size_t fuse_chan_bufsize(struct fuse_chan *ch) |
| 196 | { |
| 197 | return ch->bufsize; |
| 198 | } |
| 199 | |
| 200 | void *fuse_chan_data(struct fuse_chan *ch) |
| 201 | { |
| 202 | return ch->data; |
| 203 | } |
| 204 | |
| 205 | struct fuse_session *fuse_chan_session(struct fuse_chan *ch) |
| 206 | { |
| 207 | return ch->se; |
| 208 | } |
| 209 | |
| 210 | int fuse_chan_recv(struct fuse_chan **chp, char *buf, size_t size) |
| 211 | { |
| 212 | struct fuse_chan *ch = *chp; |
| 213 | if (ch->compat) |
| 214 | return ((struct fuse_chan_ops_compat24 *) &ch->op) |
| 215 | ->receive(ch, buf, size); |
| 216 | else |
| 217 | return ch->op.receive(chp, buf, size); |
| 218 | } |
| 219 | |
| 220 | int fuse_chan_receive(struct fuse_chan *ch, char *buf, size_t size) |
| 221 | { |
| 222 | int res; |
| 223 | |
| 224 | res = fuse_chan_recv(&ch, buf, size); |
| 225 | return res >= 0 ? res : (res != -EINTR && res != -EAGAIN) ? -1 : 0; |
| 226 | } |
| 227 | |
| 228 | int fuse_chan_send(struct fuse_chan *ch, const struct iovec iov[], size_t count) |
| 229 | { |
| 230 | return ch->op.send(ch, iov, count); |
| 231 | } |
| 232 | |
| 233 | void fuse_chan_destroy(struct fuse_chan *ch) |
| 234 | { |
| 235 | fuse_session_remove_chan(ch); |
| 236 | if (ch->op.destroy) |
| 237 | ch->op.destroy(ch); |
| 238 | free(ch); |
| 239 | } |
| 240 | |
| 241 | #ifndef __FreeBSD__ |
| 242 | FUSE_SYMVER(".symver fuse_chan_new_compat24,fuse_chan_new@FUSE_2.4"); |
| 243 | #endif |