1 jelson 1.37 /*
|
2 cerpa 1.60 *
|
3 cerpa 1.61 * Copyright (c) 2003 The Regents of the University of California. All
4 * rights reserved.
|
5 cerpa 1.60 *
|
6 cerpa 1.61 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * - Neither the name of the University nor the names of its
14 * contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS''
18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
19 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
21 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
25 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 cerpa 1.61 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
28 cerpa 1.60 *
29 */
30
31
32 /*
|
33 jelson 1.37 * fusd userspace library: functions that know how to properly talk
34 * to the fusd kernel module
35 *
36 * authors: jelson and girod
37 *
|
38 girod 1.72 * $Id: libfusd.c,v 1.71 2004/04/26 06:25:26 girod Exp $
|
39 jelson 1.37 */
40
|
41 girod 1.72 char libfusd_c_id[] = "$Id: libfusd.c,v 1.71 2004/04/26 06:25:26 girod Exp $";
|
42 jelson 1.37
|
43 cvs 1.1 #include <stdio.h>
44 #include <stdlib.h>
45 #include <unistd.h>
46 #include <sys/types.h>
47 #include <sys/stat.h>
|
48 cvs 1.8 #include <sys/time.h>
|
49 cvs 1.11 #include <sys/uio.h>
|
50 jelson 1.30 #include <sys/ioctl.h>
|
51 cvs 1.1 #include <fcntl.h>
52 #include <errno.h>
|
53 cvs 1.23 #include <string.h>
|
54 cvs 1.39 #include <time.h>
|
55 girod 1.63 #include <sys/poll.h>
|
56 cvs 1.1
57 #include "fusd.h"
58 #include "fusd_msg.h"
59
|
60 jelson 1.21 #define MIN(a, b) ((a) < (b) ? (a) : (b))
61
|
62 jelson 1.44 /* maximum number of messages processed by a single call to fusd_dispatch */
|
63 girod 1.50 #define MAX_MESSAGES_PER_DISPATCH 40
|
64 jelson 1.44
|
65 cvs 1.27 /* used for fusd_run */
66 static fd_set fusd_fds;
67
|
68 jelson 1.43 /* default prefix of devices (often "/dev/") */
|
69 girod 1.66 char *dev_root = DEFAULT_DEV_ROOT;
|
70 jelson 1.43
|
71 cvs 1.3 /*
|
72 jelson 1.37 * fusd_fops_set is an array that keeps track of the file operations
73 * struct for each fusd fd.
|
74 cvs 1.3 */
|
75 jelson 1.37 static fusd_file_operations_t fusd_fops_set[FD_SETSIZE];
76 fusd_file_operations_t null_fops = { NULL };
|
77 cvs 1.3
78 /*
79 * accessor macros
80 */
|
81 jelson 1.37 #define FUSD_GET_FOPS(fd) \
82 (fusd_fops_set + (fd))
83
84 #define FUSD_SET_FOPS(fd,fi) \
85 (fusd_fops_set[(fd)]=(*fi))
86
|
87 cvs 1.3 #define FUSD_FD_VALID(fd) \
88 (((fd)>=0) && \
89 ((fd)<FD_SETSIZE) && \
|
90 jelson 1.37 (memcmp(FUSD_GET_FOPS(fd), &null_fops, sizeof(fusd_file_operations_t))))
|
91 cvs 1.3
92
93 /*
94 * fusd_init
95 *
96 * this is called automatically before the first
97 * register call
98 */
99 void fusd_init()
100 {
|
101 jelson 1.43 static int fusd_init_needed = 1;
102
103 if (fusd_init_needed) {
104 int i;
105 struct stat statbuf;
106
107 fusd_init_needed = 0;
|
108 jelson 1.37
109 for (i = 0; i < FD_SETSIZE; i++)
110 FUSD_SET_FOPS(i, &null_fops);
|
111 cvs 1.27 FD_ZERO(&fusd_fds);
|
112 jelson 1.43
113 /* test to see if devfs is actually mounted where we think it is */
|
114 girod 1.62 if (stat(FUSD_CONTROL_DEVNAME, &statbuf) < 0)
115 fprintf(stderr, "libfusd: FUSD is not installed!\n");
|
116 cvs 1.3 }
117 }
118
119
|
120 girod 1.63 /*
121 * FUSD status handling helper functions
122 */
123
124 int fusd_status_process(fusd_status_context_t *ctx)
125 {
126 int retval = STATUS_ERR;
127
128 int status = read(ctx->fd, ctx->buf+ctx->len, sizeof(ctx->buf)-ctx->len);
129 if (status > 0) {
130 int count;
131
132 ctx->len += status;
133 count = ctx->len / sizeof(fusd_status_t);
134 if (ctx->verbose)
135 fprintf(stderr, "Got %d records (%d).. processing\n", count, ctx->len);
136 ctx->len = ctx->len % sizeof(fusd_status_t);
137
138 if (count > 0) {
139 fusd_status_t *fs = (fusd_status_t *)ctx->buf;
140 if (fs->full_mode && !ctx->full_mode) {
141 girod 1.63 ctx->full_mode = 1;
142 if (ctx->begin_full_cb) ctx->begin_full_cb(ctx->private_data);
143 }
144 retval = ctx->new_data_cb((fusd_status_t *)ctx->buf, count, ctx->private_data);
145 memmove(ctx->buf, ctx->buf+(count*sizeof(fusd_status_t)), ctx->len);
146 }
147 }
148
149 else if (status == 0) {
150 if (ctx->full_mode) {
151 if (ctx->end_full_cb) ctx->end_full_cb(ctx->private_data);
152 ctx->full_mode = 0;
153 }
154 retval = STATUS_DONE;
155 }
156
157 return retval;
158 }
159
160
161 int fusd_status_wait(fusd_status_context_t *ctx)
162 girod 1.63 {
163 while (1) {
164 /* poll */
165 struct pollfd pfd = {
166 fd: ctx->fd,
167 events: POLLIN
168 };
169 int status = poll(&pfd, 1, -1);
170
171 /* error? */
172 if (status < 0) {
173 if (errno && (errno != EINTR))
174 return -1;
175 }
176
177 /* got data? */
178 if ((status == 1) && (pfd.revents & POLLIN))
179 return 0;
180 }
181 }
182
183 girod 1.63
|
184 girod 1.66 #if 0
|
185 girod 1.63 static
186 int fusd_status_check_aux(fusd_status_t *fs, int count, void *data)
187 {
188 int i;
189 char *name = (char*)data;
190 for (i=0; i<count; i++) {
191 if (strcmp(fs[i].name, name) == 0)
192 return STATUS_FOUND;
193 }
194 return STATUS_OK;
195 }
|
196 girod 1.66 #endif
|
197 girod 1.63
198
199 /*
|
200 girod 1.66 * FUSD registration helper functions
|
201 girod 1.63 */
202
|
203 girod 1.66 int fusd_build_reg_msg(fusd_msg_t *message, const char *name, mode_t mode, void *device_info)
|
204 cvs 1.1 {
|
205 girod 1.66 int retval = 0;
206
|
207 jelson 1.55 /*
208 * convenience: if the first characters of the name you're trying
209 * to register are SKIP_PREFIX (usually "/dev/"), skip over them.
210 */
211 if (dev_root != NULL && strlen(name) > strlen(dev_root) &&
212 !strncmp(name, dev_root, strlen(dev_root))) {
213 name += strlen(dev_root);
|
214 jelson 1.58 }
215
216 if (strlen(name) > FUSD_MAX_NAME_LENGTH) {
217 fprintf(stderr, "name '%s' too long, sorry :(", name);
218 retval = -EINVAL;
219 goto done;
|
220 jelson 1.41 }
|
221 cvs 1.1
|
222 girod 1.66 /* set up the message */
|
223 jelson 1.69 memset(message, 0, sizeof(fusd_msg_t));
|
224 girod 1.66 message->magic = FUSD_MSG_MAGIC;
225 message->cmd = FUSD_REGISTER_DEVICE;
226 message->datalen = 0;
227 strcpy(message->parm.register_msg.name, name);
228 message->parm.register_msg.mode = mode;
229 message->parm.register_msg.device_info = device_info;
230
231 done:
232 return retval;
233 }
234
|
235 jelson 1.42
|
236 girod 1.66 int fusd_open_control()
237 {
238 int fd = open(FUSD_CONTROL_DEVNAME, O_RDWR | O_NONBLOCK);
239
240 if (fd < 0) {
241 int retval = 0;
242
|
243 girod 1.65 if (errno == ENOPKG) {
244 fprintf(stderr, "libfusd: ensure fusdd is running!\n");
245 retval = -ENOPKG;
246 }
247
|
248 jelson 1.42 /* if the problem is that /dev/fusd does not exist, return the
249 * message "Package not installed", which is hopefully more
250 * illuminating than "no such file or directory" */
|
251 girod 1.65 else if (errno == ENOENT) {
|
252 jelson 1.59 fprintf(stderr, "libfusd: %s does not exist; ensure FUSD's kernel module is installed\n",
253 FUSD_CONTROL_DEVNAME);
|
254 girod 1.67 fprintf(stderr, "libfusd: if DevFS not in use, be sure that fusdd is running\n");
|
255 jelson 1.57 retval = -ENOPKG;
256 } else {
|
257 jelson 1.59 perror("libfusd: trying to open FUSD control channel");
|
258 jelson 1.57 retval = -errno;
|
259 jelson 1.42 }
|
260 girod 1.66 return retval;
261 }
262
263 return fd;
264 }
265
266
267 int fusd_postreg_block(const char *name)
268 {
269 int stat;
270 int x;
271
272 /* waitfor the device to be created, if we are NOT devfs enabled */
273 if ((stat = open(FUSD_STATUS_DEVNAME, O_RDWR | O_NONBLOCK)) < 0) {
274 fprintf(stderr, "libfusd: %s does not exist; ensure FUSD's kernel module is installed\n",
275 FUSD_STATUS_DEVNAME);
276 return -1;
277 }
278
279 /* skip blocking waitfor if devfs */
280 if (ioctl(stat, FUSD_STATUS_NO_DEVFS, &x) != 0)
281 girod 1.66 goto out;
282
283 /* attempt to write request */
284 if (write(stat, name, strlen(name)) >= 0) {
285 fusd_status_context_t fsctx = {
286 fd: stat
287 };
288
289 if (fusd_status_wait(&fsctx) < 0) {
290 fprintf(stderr, "WARNING: Specific waitfor mode failed (%s): %m\n", name);
291 goto out;
292 }
293 goto out;
294 }
295 else if (errno != ENOSYS)
296 fprintf(stderr, "WARNING: Failed to enter specific waitfor mode (%s): %m\n", name);
297
298 out:
299 close(stat);
300 return 0;
301 }
302 girod 1.66
303
304 /*
305 * FUSD Registration function
306 */
307
308 int fusd_register(const char *name, mode_t mode, void *device_info,
309 struct fusd_file_operations *fops)
310 {
311 int fd = -1, retval = 0;
312 fusd_msg_t message;
313
314 /* need initialization? */
315 fusd_init();
316
317 /* make sure the name is valid and we have a valid set of fops... */
|
318 girod 1.72 if (name == NULL || (strlen(name) < 1) ||
319 (name[strlen(name)-1] == '/') || fops == NULL) {
|
320 girod 1.66 fprintf(stderr, "fusd_register: invalid name or fops argument\n");
321 retval = -EINVAL;
322 goto done;
323 }
324
325 /* open the fusd control channel */
326 fd = fusd_open_control();
327 if (fd < 0) {
328 retval = -errno;
|
329 jelson 1.57 goto done;
|
330 jelson 1.42 }
|
331 cvs 1.1
|
332 cvs 1.3 /* fd in use? */
333 if (FUSD_FD_VALID(fd)) {
|
334 jelson 1.57 retval = -EBADF;
335 goto done;
|
336 cvs 1.3 }
337
|
338 girod 1.66 /* build the message */
339 retval = fusd_build_reg_msg(&message, name, mode, device_info);
340 if (retval == 0) name = message.parm.register_msg.name;
341 else goto done;
|
342 cvs 1.1
343 /* make the request */
|
344 cvs 1.3 if (write(fd, &message, sizeof(fusd_msg_t)) < 0) {
|
345 girod 1.71 if (errno == EIO) {
346 fprintf(stderr, "FUSD version mismatch?\n");
347 }
|
348 jelson 1.57 retval = -errno;
349 goto done;
|
350 cvs 1.3 }
351
352 /* OK, store the new file state */
|
353 cvs 1.7 FUSD_SET_FOPS(fd, fops);
|
354 cvs 1.27 FD_SET(fd, &fusd_fds);
|
355 cvs 1.6
|
356 cvs 1.1 /* success! */
|
357 jelson 1.57 done:
358 if (retval < 0) {
359 if (fd >= 0)
360 close(fd);
361 errno = -retval;
362 retval = -1;
|
363 girod 1.66 }
364 else if (fusd_postreg_block(name) == 0) {
365 /* report the fd */
366 retval = fd;
367 errno = 0;
|
368 jelson 1.57 }
|
369 girod 1.66
|
370 jelson 1.57 return retval;
|
371 cvs 1.1 }
372
373
374 int fusd_unregister(int fd)
375 {
|
376 cvs 1.3 if (FUSD_FD_VALID(fd)) {
377 /* clear fd location */
|
378 jelson 1.37 FUSD_SET_FOPS(fd, &null_fops);
|
379 cvs 1.27 FD_CLR(fd, &fusd_fds);
|
380 cvs 1.3 /* close */
381 return close(fd);
382 }
383
384 else {
|
385 cvs 1.8 errno = EBADF;
386 return -1;
|
387 cvs 1.3 }
|
388 cvs 1.1 }
|
389 cvs 1.3
390
391 /*
|
392 jelson 1.44 * fusd_run: a convenience function for automatically running a FUSD
393 * driver, for drivers that don't want to manually select on file
394 * descriptors and call fusd_dispatch. This function will
395 * automatically select on all devices the user has registered and
396 * call fusd_dispatch on any one that becomes readable.
|
397 cvs 1.3 */
|
398 jelson 1.44 void fusd_run(void)
|
399 cvs 1.3 {
|
400 cvs 1.22 fd_set tfds;
|
401 cvs 1.3 int status;
|
402 cvs 1.22 int maxfd;
403 int i;
|
404 cvs 1.3
|
405 cvs 1.22 /* locate maxmimum fd in use */
|
406 jelson 1.44 for (maxfd=0, i=0; i < FD_SETSIZE; i++) {
|
407 cvs 1.27 if (FD_ISSET(i, &fusd_fds)) {
|
408 cvs 1.22 maxfd = i;
|
409 cvs 1.24 }
|
410 cvs 1.22 }
411 maxfd++;
412
|
413 jelson 1.44
|
414 cvs 1.3 while (1) {
|
415 cvs 1.4 /* select */
|
416 cvs 1.27 memmove(&tfds, &fusd_fds, sizeof(fd_set));
|
417 jelson 1.44 status = select(maxfd, &tfds, NULL, NULL, NULL);
|
418 cvs 1.24
|
419 cvs 1.4 /* error? */
420 if (status < 0) {
|
421 jelson 1.44 perror("libfusd: fusd_run: error on select");
422 continue;
|
423 cvs 1.4 }
424
425 /* readable? */
|
426 jelson 1.44 for (i = 0; i < maxfd; i++)
427 if (FD_ISSET(i, &tfds))
|
428 jelson 1.48 fusd_dispatch(i);
|
429 cvs 1.3 }
430 }
431
|
432 cvs 1.4
|
433 cvs 1.40 /************************************************************************/
434
435
436 /* reads a fusd kernel-to-userspace message from fd, and puts a
437 * fusd_msg into the memory pointed to by msg (we assume we are passed
438 * a buffer managed by the caller). if there is a data portion to the
439 * message (msg->datalen > 0), we allocate memory for it, set data to
440 * point to that memory. the returned data pointer must also be
441 * managed by the caller. */
|
442 girod 1.66 static
443 int fusd_get_message(int fd, fusd_msg_t *msg, void **local_client)
|
444 cvs 1.5 {
|
445 jelson 1.45 /* read the header part into the kernel */
|
446 cvs 1.5 if (read(fd, msg, sizeof(fusd_msg_t)) < 0) {
|
447 jelson 1.44 if (errno != EAGAIN)
|
448 jelson 1.56 perror("error talking to FUSD control channel on header read");
|
449 jelson 1.44 return -errno;
|
450 cvs 1.5 }
|
451 girod 1.66
452 /* if this is from the net, we return the local client demux token */
453 if (local_client)
454 *local_client = msg->data;
455
456 /* clear the data pointer */
457 msg->data = NULL;
|
458 cvs 1.5
|
459 cvs 1.8 if (msg->magic != FUSD_MSG_MAGIC) {
|
460 cvs 1.35 fprintf(stderr, "libfusd magic number failure\n");
|
461 jelson 1.44 return -EINVAL;
|
462 cvs 1.8 }
463
|
464 jelson 1.45 /* if there's a data part to the message, read it from the kernel. */
|
465 cvs 1.5 if (msg->datalen) {
|
466 jelson 1.45 if ((msg->data = malloc(msg->datalen + 1)) == NULL) {
|
467 jelson 1.28 fprintf(stderr, "libfusd: can't allocate memory\n");
|
468 jelson 1.44 return -ENOMEM; /* this is bad, we are now unsynced */
|
469 cvs 1.8 }
470
|
471 cvs 1.15 if (read(fd, msg->data, msg->datalen) < 0) {
|
472 jelson 1.56 perror("error talking to FUSD control channel on data read");
|
473 cvs 1.15 free(msg->data);
474 msg->data = NULL;
|
475 jelson 1.44 return -EIO;
|
476 cvs 1.5 }
|
477 jelson 1.45
478 /* For convenience, we now ensure that the byte *after* the buffer
479 * is set to 0. (Note we malloc'd one extra byte above.) */
480 msg->data[msg->datalen] = '\0';
|
481 cvs 1.5 }
482
483 return 0;
484 }
485
486
|
487 cvs 1.40 /*
488 * fusd_fdset_add: given an FDSET and "max", add the currently valid
489 * FUSD fds to the set and update max accordingly.
490 */
491 void fusd_fdset_add(fd_set *set, int *max)
492 {
493 int i;
494
495 for (i = 0; i < FD_SETSIZE; i++) {
496 if (FD_ISSET(i, &fusd_fds)) {
497 FD_SET(i, set);
498 if (i > *max) {
499 *max = i;
500 }
501 }
502 }
503 }
504
505
506
507 /*
508 cvs 1.40 * fusd_dispatch_fdset: given an fd_set full of descriptors, call
509 * fusd_dispatch on every descriptor in the set which is a valid FUSD
510 * fd.
511 */
|
512 jelson 1.48 void fusd_dispatch_fdset(fd_set *set)
|
513 cvs 1.40 {
|
514 jelson 1.48 int i;
|
515 cvs 1.40
|
516 jelson 1.48 for (i = 0; i < FD_SETSIZE; i++)
517 if (FD_ISSET(i, set) && FD_ISSET(i, &fusd_fds))
518 fusd_dispatch(i);
|
519 cvs 1.40 }
520
|
521 cvs 1.7
|
522 cvs 1.4 /*
|
523 jelson 1.44 * fusd_dispatch_one() -- read a single kernel-to-userspace message
524 * from fd, then call the appropriate userspace callback function,
525 * based on the message that was read. finally, return the result
526 * back to the kernel, IF the return value from the callback is not
527 * FUSD_NOREPLY.
528 *
529 * On success, returns 0.
530 * On failure, returns a negative number indicating the errno.
|
531 cvs 1.4 */
|
532 girod 1.66 int fusd_dispatch_one(int fd, fusd_file_operations_t *fops,
533 int from_net, void **local_device_info)
|
534 cvs 1.4 {
|
535 cvs 1.15 fusd_file_info_t *file = NULL;
536 fusd_msg_t *msg = NULL;
|
537 jelson 1.44 int driver_retval = 0; /* returned to the FUSD driver */
538 int user_retval = 0; /* returned to the user who made the syscall */
|
539 girod 1.66 void *local_client = NULL;
|
540 cvs 1.8
|
541 cvs 1.7 /* check for valid, look up ops */
|
542 jelson 1.44 if (fops == NULL) {
543 fprintf(stderr, "fusd_dispatch: no fops provided!\n");
544 driver_retval = -EBADF;
545 goto out_noreply;
|
546 cvs 1.6 }
|
547 cvs 1.8
|
548 cvs 1.7 /* allocate memory for fusd_msg_t */
|
549 cvs 1.8 if ((msg = malloc(sizeof(fusd_msg_t))) == NULL) {
|
550 jelson 1.44 driver_retval = -ENOMEM;
|
551 jelson 1.28 fprintf(stderr, "libfusd: can't allocate memory\n");
|
552 jelson 1.44 goto out_noreply;
|
553 cvs 1.8 }
|
554 cvs 1.16 memset(msg, '\0', sizeof(fusd_msg_t));
|
555 cvs 1.7
|
556 cvs 1.8 /* read header and data, if it's there */
|
557 girod 1.66 if ((driver_retval = fusd_get_message(fd, msg, &local_client)) < 0)
|
558 jelson 1.44 goto out_noreply;
|
559 cvs 1.7
560 /* allocate file info struct */
561 file = malloc(sizeof(fusd_file_info_t));
562 if (NULL == file) {
|
563 jelson 1.28 fprintf(stderr, "libfusd: can't allocate memory\n");
|
564 jelson 1.44 driver_retval = -ENOMEM;
565 goto out_noreply;
|
566 cvs 1.7 }
567
568 /* fill the file info struct */
|
569 cvs 1.16 memset(file, '\0', sizeof(fusd_file_info_t));
|
570 girod 1.66
571 /* if we're processing stuff from the net, we need to keep track
572 * of demux info required to route back to the remote client */
573 if (from_net) {
574 file->remote_client = msg->parm.fops_msg.device_info;
575 file->local_client = local_client;
576 if (local_device_info == NULL)
577 fprintf(stderr, "libfusd: net support requires local device info parameter!\n");
578 else
579 msg->parm.fops_msg.device_info = *local_device_info;
580 }
581
582 /* fill the rest of the fields */
|
583 cvs 1.16 file->fd = fd;
|
584 cvs 1.8 file->device_info = msg->parm.fops_msg.device_info;
585 file->private_data = msg->parm.fops_msg.private_info;
586 file->flags = msg->parm.fops_msg.flags;
587 file->pid = msg->parm.fops_msg.pid;
|
588 cvs 1.38 file->uid = msg->parm.fops_msg.uid;
589 file->gid = msg->parm.fops_msg.gid;
|
590 cvs 1.8 file->fusd_msg = msg;
|
591 jelson 1.44
|
592 girod 1.66 /* net support must track its own local device info */
593 file->local_device_info = local_device_info;
594
|
595 jelson 1.44 /* right now we only handle fops requests */
|
596 jelson 1.46 if (msg->cmd != FUSD_FOPS_CALL && msg->cmd != FUSD_FOPS_NONBLOCK &&
597 msg->cmd != FUSD_FOPS_CALL_DROPREPLY) {
|
598 jelson 1.44 fprintf(stderr, "libfusd: got unknown msg->cmd from kernel\n");
599 user_retval = -EINVAL;
600 goto send_reply;
601 }
|
602 cvs 1.4
|
603 cvs 1.7 /* dispatch on operation type */
|
604 jelson 1.44 user_retval = -ENOSYS;
|
605 cvs 1.7 switch (msg->subcmd) {
606 case FUSD_OPEN:
|
607 cvs 1.16 if (fops && fops->open)
|
608 jelson 1.44 user_retval = fops->open(file);
|
609 cvs 1.9 break;
|
610 cvs 1.7 case FUSD_CLOSE:
|
611 cvs 1.17 if (fops && fops->close)
|
612 jelson 1.44 user_retval = fops->close(file);
|
613 cvs 1.17 break;
|
614 cvs 1.7 case FUSD_READ:
|
615 cvs 1.18 /* allocate a buffer and make the call */
616 if (fops && fops->read) {
617 if ((msg->data = malloc(msg->parm.fops_msg.length)) == NULL) {
|
618 jelson 1.44 user_retval = -ENOMEM;
|
619 jelson 1.28 fprintf(stderr, "libfusd: can't allocate memory\n");
|
620 cvs 1.18 } else {
|
621 jelson 1.21 msg->datalen = msg->parm.fops_msg.length;
|
622 nithya 1.70 memset(msg->data, 0, msg->datalen);
|
623 jelson 1.44 user_retval = fops->read(file, msg->data, msg->datalen,
624 &msg->parm.fops_msg.offset);
|
625 cvs 1.18 }
626 }
627 break;
|
628 cvs 1.7 case FUSD_WRITE:
|
629 jelson 1.29 if (fops && fops->write)
|
630 jelson 1.44 user_retval = fops->write(file, msg->data, msg->datalen,
631 &msg->parm.fops_msg.offset);
|
632 jelson 1.29 break;
|
633 cvs 1.7 case FUSD_IOCTL:
|
634 jelson 1.30 if (fops && fops->ioctl) {
635 /* in the case of an ioctl read, allocate a buffer for the
636 * driver to write to, IF there isn't already a buffer. (there
637 * might already be a buffer if this is a read+write) */
638 if ((_IOC_DIR(msg->parm.fops_msg.cmd) & _IOC_READ) &&
639 msg->data == NULL) {
640 msg->datalen = _IOC_SIZE(msg->parm.fops_msg.cmd);
641 if ((msg->data = malloc(msg->datalen)) == NULL) {
|
642 jelson 1.44 user_retval = -ENOMEM;
|
643 jelson 1.30 break;
644 }
645 }
646 if (msg->data != NULL)
|
647 jelson 1.44 user_retval = fops->ioctl(file, msg->parm.fops_msg.cmd, msg->data);
|
648 jelson 1.30 else
|
649 jelson 1.44 user_retval = fops->ioctl(file, msg->parm.fops_msg.cmd,
650 (void *) msg->parm.fops_msg.arg);
|
651 jelson 1.30 }
652 break;
653
|
654 cvs 1.32 case FUSD_POLL_DIFF:
|
655 cvs 1.31 /* This callback requests notification when an event occurs on a file,
656 * e.g. becoming readable or writable */
|
657 cvs 1.32 if (fops && fops->poll_diff)
|
658 jelson 1.44 user_retval = fops->poll_diff(file, msg->parm.fops_msg.cmd);
|
659 cvs 1.31 break;
660
661 case FUSD_UNBLOCK:
662 /* This callback is called when a system call is interrupted */
663 if (fops && fops->unblock)
|
664 jelson 1.44 user_retval = fops->unblock(file);
|
665 cvs 1.31 break;
|
666 cvs 1.7
667 default:
|
668 jelson 1.42 fprintf(stderr, "libfusd: Got unsupported operation\n");
|
669 jelson 1.44 user_retval = -ENOSYS;
|
670 cvs 1.10 break;
|
671 cvs 1.7 }
|
672 cvs 1.31
|
673 jelson 1.44 goto send_reply;
|
674 cvs 1.9
|
675 cvs 1.8
|
676 jelson 1.44 /* out_noreply is only used for handling errors */
677 out_noreply:
|
678 cvs 1.15 if (msg->data != NULL)
679 free(msg->data);
680 if (msg != NULL)
681 free(msg);
|
682 jelson 1.44 goto done;
683
684 /* send_reply is only used for success */
685 send_reply:
686 if (-user_retval <= 0xff) {
687 /* 0xff is the maximum legal return value (?) - return val to user */
688 driver_retval = fusd_return(file, user_retval);
689 } else {
690 /* if we got a FUSD_NOREPLY, don't free the msg structure */
691 driver_retval = 0;
692 }
693
694 /* this is common to both errors and success */
695 done:
696 if (driver_retval < 0) {
697 errno = -driver_retval;
698 driver_retval = -1;
699 }
700 return driver_retval;
701 }
702
703 jelson 1.44
704 /* fusd_dispatch is now a wrapper around fusd_dispatch_one that calls
705 * it repeatedly, until it fails. this helps a lot with bulk data
706 * transfer since there is no intermediate select in between the
707 * reads. (the kernel module helps by running the user process in
708 * between).
709 *
|
710 jelson 1.48 * This function now prints an error to stderr in case of error,
711 * instead of returning a -1.
|
712 jelson 1.44 */
|
713 girod 1.66 void fusd_dispatch_aux(int fd, int from_net, fusd_file_operations_t *use_fops, void **local_device_info)
|
714 jelson 1.44 {
715 int retval, num_dispatches = 0;
|
716 girod 1.66 fusd_file_operations_t *fops = use_fops;
|
717 jelson 1.44
|
718 girod 1.66 if (fops == NULL) {
719 /* make sure we have a valid FD, and get its fops structure */
720 if (!FUSD_FD_VALID(fd)) {
721 errno = EBADF;
722 retval = -1;
723 goto out;
724 }
725 fops = FUSD_GET_FOPS(fd);
|
726 jelson 1.44 }
|
727 girod 1.66
|
728 jelson 1.44 /* now keep dispatching until a dispatch returns an error */
729 do {
|
730 girod 1.66 retval = fusd_dispatch_one(fd, fops, from_net, local_device_info);
731
|
732 jelson 1.44 if (retval >= 0)
733 num_dispatches++;
734 } while (retval >= 0 && num_dispatches <= MAX_MESSAGES_PER_DISPATCH);
735
736 /* if we've dispatched at least one message successfully, and then
737 * stopped because of EAGAIN - do not report an error. this is the
738 * common case. */
739 if (num_dispatches > 0 && errno == EAGAIN) {
740 retval = 0;
741 errno = 0;
742 }
743
744 out:
|
745 jelson 1.48 if (retval < 0 && errno != EPIPE)
746 fprintf(stderr, "libfusd: fusd_dispatch error on fd %d: %m\n", fd);
|
747 cvs 1.4 }
|
748 cvs 1.3
|
749 cvs 1.9
|
750 girod 1.66 void fusd_dispatch(int fd)
751 {
752 fusd_dispatch_aux(fd, 0, NULL, NULL);
753 }
754
|
755 cvs 1.40 /*
|
756 jelson 1.47 * fusd_destroy destroys all state associated with a fusd_file_info
757 * pointer. (It is implicitly called by fusd_return.) If a driver
758 * saves a fusd_file_info pointer by calling -FUSD_NOREPLY in order to
759 * block a read, but gets a "close" request on the file before the
760 * pointer is returned with fusd_return, it should be thrown away
761 * using fusd_destroy.
762 */
763 void fusd_destroy(struct fusd_file_info *file)
764 {
765 if (file == NULL)
766 return;
767
768 if (file->fusd_msg->data != NULL)
769 free(file->fusd_msg->data);
770 free(file->fusd_msg);
771 free(file);
772 }
773
774
775 /*
|
776 jelson 1.44 * construct a user-to-kernel message in reply to a file function
777 * call.
778 *
779 * On success, returns 0.
780 * On failure, returns a negative number indicating the errno.
|
781 cvs 1.40 */
|
782 cvs 1.10 int fusd_return(fusd_file_info_t *file, ssize_t retval)
|
783 cvs 1.9 {
|
784 cvs 1.15 fusd_msg_t *msg = NULL;
|
785 jelson 1.44 int fd;
|
786 jelson 1.46 int driver_retval = 0;
|
787 cvs 1.11 struct iovec iov[2];
|
788 girod 1.66 char *msg_data = NULL;
789 int fd_tracking = file->local_device_info == NULL;
790
|
791 jelson 1.44 if (file == NULL) {
792 fprintf(stderr, "fusd_return: NULL file\n");
793 return -EINVAL;
794 }
795
796 fd = file->fd;
|
797 girod 1.66 if (fd_tracking && !FUSD_FD_VALID(fd)) {
|
798 cvs 1.16 fprintf(stderr, "fusd_return: badfd (fd %d)\n", fd);
|
799 jelson 1.44 return -EBADF;
|
800 cvs 1.15 }
|
801 girod 1.66
|
802 cvs 1.15 if ((msg = file->fusd_msg) == NULL) {
|
803 cvs 1.36 fprintf(stderr, "fusd_return: fusd_msg is gone\n");
|
804 jelson 1.44 return -EINVAL;
|
805 cvs 1.15 }
806
|
807 jelson 1.46 /* if this was a "DONTREPLY" message, just free the struct */
808 if (msg->cmd == FUSD_FOPS_CALL_DROPREPLY)
809 goto free_memory;
810
|
811 jelson 1.29 /* do we copy data back to kernel? how much? */
812 switch(msg->subcmd) {
813 case FUSD_READ:
814 /* these operations can return data to userspace */
815 if (retval > 0) {
816 msg->datalen = MIN(retval, msg->parm.fops_msg.length);
817 retval = msg->datalen;
818 } else {
819 msg->datalen = 0;
820 }
|
821 jelson 1.30 break;
822 case FUSD_IOCTL:
823 /* ioctl CAN (in read mode) return data to userspace */
|
824 girod 1.52 if ((retval == 0) &&
825 (_IOC_DIR(msg->parm.fops_msg.cmd) & _IOC_READ))
|
826 jelson 1.30 msg->datalen = _IOC_SIZE(msg->parm.fops_msg.cmd);
827 else
828 msg->datalen = 0;
|
829 jelson 1.29 break;
830 default:
831 /* open, close, write, etc. do not return data */
|
832 cvs 1.18 msg->datalen = 0;
|
833 jelson 1.29 break;
|
834 jelson 1.21 }
|
835 cvs 1.18
|
836 girod 1.66 /* save the local device info if we're tracking it */
837 if (file->local_device_info)
838 *(file->local_device_info) = file->device_info;
839
840 /* handle net case: if we have one, patch in the remote's device info */
841 msg->parm.fops_msg.device_info =
842 file->remote_client ? file->remote_client : file->device_info;
843
844 /* handle net case: patch in the local demux info */
845 msg_data = msg->data;
846 msg->data = file->local_client;
847
|
848 cvs 1.10 /* fill the file info struct */
|
849 cvs 1.34 msg->cmd++; /* change FOPS_CALL to FOPS_REPLY; NONBLOCK to NONBLOCK_REPLY */
|
850 cvs 1.10 msg->parm.fops_msg.retval = retval;
851 msg->parm.fops_msg.private_info = file->private_data;
852 msg->parm.fops_msg.flags = file->flags;
853 /* pid is NOT copied back. */
854
|
855 cvs 1.18 /* send message to kernel */
|
856 girod 1.68 if (msg->datalen > 0 && msg_data != NULL) {
|
857 cvs 1.11 iov[0].iov_base = msg;
858 iov[0].iov_len = sizeof(fusd_msg_t);
|
859 girod 1.66 iov[1].iov_base = msg_data;
|
860 cvs 1.11 iov[1].iov_len = msg->datalen;
|
861 jelson 1.46 driver_retval = writev(fd, iov, 2);
|
862 cvs 1.11 }
|
863 jelson 1.29 else {
|
864 jelson 1.46 driver_retval = write(fd, msg, sizeof(fusd_msg_t));
|
865 jelson 1.29 }
|
866 girod 1.68
867 /* restore data pointer for free */
868 msg->data = msg_data;
|
869 cvs 1.9
|
870 jelson 1.46 free_memory:
|
871 jelson 1.47 fusd_destroy(file);
|
872 jelson 1.44
|
873 jelson 1.46 if (driver_retval < 0)
|
874 jelson 1.44 return -errno;
875 else
876 return 0;
|
877 cvs 1.9 }
|
878 cvs 1.40
|
879 girod 1.53
880 /* returns static string representing the flagset (e.g. RWE) */
881 #define RING 5
882 char *fusd_unparse_flags(int flags)
883 {
884 static int i = 0;
885 static char ringbuf[RING][5];
886 char *s = ringbuf[i];
887 i = (i + 1) % RING;
888
889 sprintf(s, "%c%c%c",
890 (flags & FUSD_NOTIFY_INPUT)?'R':'-',
891 (flags & FUSD_NOTIFY_OUTPUT)?'W':'-',
892 (flags & FUSD_NOTIFY_EXCEPT)?'E':'-');
893
894 return s;
895 }
896 #undef RING
|
897 girod 1.66
898
|