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