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.73 * $Id: libfusd.c,v 1.72 2004/11/23 05:33:08 girod Exp $
|
39 jelson 1.37 */
40
|
41 girod 1.73 char libfusd_c_id[] = "$Id: libfusd.c,v 1.72 2004/11/23 05:33:08 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 girod 1.73
78 /* Global flag to enable export via fusdnet by default */
79 int fusdnet_export_devs = 0;
|
80 cvs 1.3
81 /*
82 * accessor macros
83 */
|
84 jelson 1.37 #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 cvs 1.3 #define FUSD_FD_VALID(fd) \
91 (((fd)>=0) && \
92 ((fd)<FD_SETSIZE) && \
|
93 jelson 1.37 (memcmp(FUSD_GET_FOPS(fd), &null_fops, sizeof(fusd_file_operations_t))))
|
94 cvs 1.3
95
96 /*
97 * fusd_init
98 *
99 * this is called automatically before the first
100 * register call
101 */
102 void fusd_init()
103 {
|
104 jelson 1.43 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 jelson 1.37
112 for (i = 0; i < FD_SETSIZE; i++)
113 FUSD_SET_FOPS(i, &null_fops);
|
114 cvs 1.27 FD_ZERO(&fusd_fds);
|
115 jelson 1.43
116 /* test to see if devfs is actually mounted where we think it is */
|
117 girod 1.62 if (stat(FUSD_CONTROL_DEVNAME, &statbuf) < 0)
118 fprintf(stderr, "libfusd: FUSD is not installed!\n");
|
119 cvs 1.3 }
120 }
121
122
|
123 girod 1.63 /*
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 girod 1.63 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 girod 1.63 {
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 girod 1.63
|
187 girod 1.66 #if 0
|
188 girod 1.63 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 girod 1.66 #endif
|
200 girod 1.63
201
202 /*
|
203 girod 1.66 * FUSD registration helper functions
|
204 girod 1.63 */
205
|
206 girod 1.66 int fusd_build_reg_msg(fusd_msg_t *message, const char *name, mode_t mode, void *device_info)
|
207 cvs 1.1 {
|
208 girod 1.66 int retval = 0;
209
|
210 jelson 1.55 /*
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 jelson 1.58 }
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 jelson 1.41 }
|
224 cvs 1.1
|
225 girod 1.66 /* set up the message */
|
226 jelson 1.69 memset(message, 0, sizeof(fusd_msg_t));
|
227 girod 1.66 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 jelson 1.42
|
239 girod 1.66 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 girod 1.65 if (errno == ENOPKG) {
247 fprintf(stderr, "libfusd: ensure fusdd is running!\n");
248 retval = -ENOPKG;
249 }
250
|
251 jelson 1.42 /* 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 girod 1.65 else if (errno == ENOENT) {
|
255 jelson 1.59 fprintf(stderr, "libfusd: %s does not exist; ensure FUSD's kernel module is installed\n",
256 FUSD_CONTROL_DEVNAME);
|
257 girod 1.67 fprintf(stderr, "libfusd: if DevFS not in use, be sure that fusdd is running\n");
|
258 jelson 1.57 retval = -ENOPKG;
259 } else {
|
260 jelson 1.59 perror("libfusd: trying to open FUSD control channel");
|
261 jelson 1.57 retval = -errno;
|
262 jelson 1.42 }
|
263 girod 1.66 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 girod 1.66 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 girod 1.66
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 girod 1.72 if (name == NULL || (strlen(name) < 1) ||
322 (name[strlen(name)-1] == '/') || fops == NULL) {
|
323 girod 1.66 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 jelson 1.57 goto done;
|
333 jelson 1.42 }
|
334 cvs 1.1
|
335 cvs 1.3 /* fd in use? */
336 if (FUSD_FD_VALID(fd)) {
|
337 jelson 1.57 retval = -EBADF;
338 goto done;
|
339 cvs 1.3 }
340
|
341 girod 1.66 /* 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 cvs 1.1
346 /* make the request */
|
347 cvs 1.3 if (write(fd, &message, sizeof(fusd_msg_t)) < 0) {
|
348 girod 1.71 if (errno == EIO) {
349 fprintf(stderr, "FUSD version mismatch?\n");
350 }
|
351 jelson 1.57 retval = -errno;
352 goto done;
|
353 cvs 1.3 }
354
355 /* OK, store the new file state */
|
356 cvs 1.7 FUSD_SET_FOPS(fd, fops);
|
357 cvs 1.27 FD_SET(fd, &fusd_fds);
|
358 cvs 1.6
|
359 cvs 1.1 /* success! */
|
360 jelson 1.57 done:
361 if (retval < 0) {
362 if (fd >= 0)
363 close(fd);
364 errno = -retval;
365 retval = -1;
|
366 girod 1.66 }
367 else if (fusd_postreg_block(name) == 0) {
368 /* report the fd */
369 retval = fd;
370 errno = 0;
|
371 jelson 1.57 }
|
372 girod 1.66
|
373 jelson 1.57 return retval;
|
374 cvs 1.1 }
375
376
377 int fusd_unregister(int fd)
378 {
|
379 cvs 1.3 if (FUSD_FD_VALID(fd)) {
380 /* clear fd location */
|
381 jelson 1.37 FUSD_SET_FOPS(fd, &null_fops);
|
382 cvs 1.27 FD_CLR(fd, &fusd_fds);
|
383 cvs 1.3 /* close */
384 return close(fd);
385 }
386
387 else {
|
388 cvs 1.8 errno = EBADF;
389 return -1;
|
390 cvs 1.3 }
|
391 cvs 1.1 }
|
392 cvs 1.3
393
394 /*
|
395 jelson 1.44 * 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 cvs 1.3 */
|
401 jelson 1.44 void fusd_run(void)
|
402 cvs 1.3 {
|
403 cvs 1.22 fd_set tfds;
|
404 cvs 1.3 int status;
|
405 cvs 1.22 int maxfd;
406 int i;
|
407 cvs 1.3
|
408 cvs 1.22 /* locate maxmimum fd in use */
|
409 jelson 1.44 for (maxfd=0, i=0; i < FD_SETSIZE; i++) {
|
410 cvs 1.27 if (FD_ISSET(i, &fusd_fds)) {
|
411 cvs 1.22 maxfd = i;
|
412 cvs 1.24 }
|
413 cvs 1.22 }
414 maxfd++;
415
|
416 jelson 1.44
|
417 cvs 1.3 while (1) {
|
418 cvs 1.4 /* select */
|
419 cvs 1.27 memmove(&tfds, &fusd_fds, sizeof(fd_set));
|
420 jelson 1.44 status = select(maxfd, &tfds, NULL, NULL, NULL);
|
421 cvs 1.24
|
422 cvs 1.4 /* error? */
423 if (status < 0) {
|
424 jelson 1.44 perror("libfusd: fusd_run: error on select");
425 continue;
|
426 cvs 1.4 }
427
428 /* readable? */
|
429 jelson 1.44 for (i = 0; i < maxfd; i++)
430 if (FD_ISSET(i, &tfds))
|
431 jelson 1.48 fusd_dispatch(i);
|
432 cvs 1.3 }
433 }
434
|
435 cvs 1.4
|
436 cvs 1.40 /************************************************************************/
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 girod 1.66 static
446 int fusd_get_message(int fd, fusd_msg_t *msg, void **local_client)
|
447 cvs 1.5 {
|
448 jelson 1.45 /* read the header part into the kernel */
|
449 cvs 1.5 if (read(fd, msg, sizeof(fusd_msg_t)) < 0) {
|
450 jelson 1.44 if (errno != EAGAIN)
|
451 jelson 1.56 perror("error talking to FUSD control channel on header read");
|
452 jelson 1.44 return -errno;
|
453 cvs 1.5 }
|
454 girod 1.66
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 cvs 1.5
|
462 cvs 1.8 if (msg->magic != FUSD_MSG_MAGIC) {
|
463 cvs 1.35 fprintf(stderr, "libfusd magic number failure\n");
|
464 jelson 1.44 return -EINVAL;
|
465 cvs 1.8 }
466
|
467 jelson 1.45 /* if there's a data part to the message, read it from the kernel. */
|
468 cvs 1.5 if (msg->datalen) {
|
469 jelson 1.45 if ((msg->data = malloc(msg->datalen + 1)) == NULL) {
|
470 jelson 1.28 fprintf(stderr, "libfusd: can't allocate memory\n");
|
471 jelson 1.44 return -ENOMEM; /* this is bad, we are now unsynced */
|
472 cvs 1.8 }
473
|
474 cvs 1.15 if (read(fd, msg->data, msg->datalen) < 0) {
|
475 jelson 1.56 perror("error talking to FUSD control channel on data read");
|
476 cvs 1.15 free(msg->data);
477 msg->data = NULL;
|
478 jelson 1.44 return -EIO;
|
479 cvs 1.5 }
|
480 jelson 1.45
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 cvs 1.5 }
485
486 return 0;
487 }
488
489
|
490 cvs 1.40 /*
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 cvs 1.40 * 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 jelson 1.48 void fusd_dispatch_fdset(fd_set *set)
|
516 cvs 1.40 {
|
517 jelson 1.48 int i;
|
518 cvs 1.40
|
519 jelson 1.48 for (i = 0; i < FD_SETSIZE; i++)
520 if (FD_ISSET(i, set) && FD_ISSET(i, &fusd_fds))
521 fusd_dispatch(i);
|
522 cvs 1.40 }
523
|
524 cvs 1.7
|
525 cvs 1.4 /*
|
526 jelson 1.44 * 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 cvs 1.4 */
|
535 girod 1.66 int fusd_dispatch_one(int fd, fusd_file_operations_t *fops,
536 int from_net, void **local_device_info)
|
537 cvs 1.4 {
|
538 cvs 1.15 fusd_file_info_t *file = NULL;
539 fusd_msg_t *msg = NULL;
|
540 jelson 1.44 int driver_retval = 0; /* returned to the FUSD driver */
541 int user_retval = 0; /* returned to the user who made the syscall */
|
542 girod 1.66 void *local_client = NULL;
|
543 cvs 1.8
|
544 cvs 1.7 /* check for valid, look up ops */
|
545 jelson 1.44 if (fops == NULL) {
546 fprintf(stderr, "fusd_dispatch: no fops provided!\n");
547 driver_retval = -EBADF;
548 goto out_noreply;
|
549 cvs 1.6 }
|
550 cvs 1.8
|
551 cvs 1.7 /* allocate memory for fusd_msg_t */
|
552 cvs 1.8 if ((msg = malloc(sizeof(fusd_msg_t))) == NULL) {
|
553 jelson 1.44 driver_retval = -ENOMEM;
|
554 jelson 1.28 fprintf(stderr, "libfusd: can't allocate memory\n");
|
555 jelson 1.44 goto out_noreply;
|
556 cvs 1.8 }
|
557 cvs 1.16 memset(msg, '\0', sizeof(fusd_msg_t));
|
558 cvs 1.7
|
559 cvs 1.8 /* read header and data, if it's there */
|
560 girod 1.66 if ((driver_retval = fusd_get_message(fd, msg, &local_client)) < 0)
|
561 jelson 1.44 goto out_noreply;
|
562 cvs 1.7
563 /* allocate file info struct */
564 file = malloc(sizeof(fusd_file_info_t));
565 if (NULL == file) {
|
566 jelson 1.28 fprintf(stderr, "libfusd: can't allocate memory\n");
|
567 jelson 1.44 driver_retval = -ENOMEM;
568 goto out_noreply;
|
569 cvs 1.7 }
570
571 /* fill the file info struct */
|
572 cvs 1.16 memset(file, '\0', sizeof(fusd_file_info_t));
|
573 girod 1.66
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 cvs 1.16 file->fd = fd;
|
587 cvs 1.8 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 cvs 1.38 file->uid = msg->parm.fops_msg.uid;
592 file->gid = msg->parm.fops_msg.gid;
|
593 cvs 1.8 file->fusd_msg = msg;
|
594 jelson 1.44
|
595 girod 1.66 /* net support must track its own local device info */
596 file->local_device_info = local_device_info;
597
|
598 jelson 1.44 /* right now we only handle fops requests */
|
599 jelson 1.46 if (msg->cmd != FUSD_FOPS_CALL && msg->cmd != FUSD_FOPS_NONBLOCK &&
600 msg->cmd != FUSD_FOPS_CALL_DROPREPLY) {
|
601 jelson 1.44 fprintf(stderr, "libfusd: got unknown msg->cmd from kernel\n");
602 user_retval = -EINVAL;
603 goto send_reply;
604 }
|
605 cvs 1.4
|
606 cvs 1.7 /* dispatch on operation type */
|
607 jelson 1.44 user_retval = -ENOSYS;
|
608 cvs 1.7 switch (msg->subcmd) {
609 case FUSD_OPEN:
|
610 cvs 1.16 if (fops && fops->open)
|
611 jelson 1.44 user_retval = fops->open(file);
|
612 cvs 1.9 break;
|
613 cvs 1.7 case FUSD_CLOSE:
|
614 cvs 1.17 if (fops && fops->close)
|
615 jelson 1.44 user_retval = fops->close(file);
|
616 cvs 1.17 break;
|
617 cvs 1.7 case FUSD_READ:
|
618 cvs 1.18 /* allocate a buffer and make the call */
619 if (fops && fops->read) {
620 if ((msg->data = malloc(msg->parm.fops_msg.length)) == NULL) {
|
621 jelson 1.44 user_retval = -ENOMEM;
|
622 jelson 1.28 fprintf(stderr, "libfusd: can't allocate memory\n");
|
623 cvs 1.18 } else {
|
624 jelson 1.21 msg->datalen = msg->parm.fops_msg.length;
|
625 nithya 1.70 memset(msg->data, 0, msg->datalen);
|
626 jelson 1.44 user_retval = fops->read(file, msg->data, msg->datalen,
627 &msg->parm.fops_msg.offset);
|
628 cvs 1.18 }
629 }
630 break;
|
631 cvs 1.7 case FUSD_WRITE:
|
632 jelson 1.29 if (fops && fops->write)
|
633 jelson 1.44 user_retval = fops->write(file, msg->data, msg->datalen,
634 &msg->parm.fops_msg.offset);
|
635 jelson 1.29 break;
|
636 cvs 1.7 case FUSD_IOCTL:
|
637 jelson 1.30 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 jelson 1.44 user_retval = -ENOMEM;
|
646 jelson 1.30 break;
647 }
648 }
649 if (msg->data != NULL)
|
650 jelson 1.44 user_retval = fops->ioctl(file, msg->parm.fops_msg.cmd, msg->data);
|
651 jelson 1.30 else
|
652 jelson 1.44 user_retval = fops->ioctl(file, msg->parm.fops_msg.cmd,
653 (void *) msg->parm.fops_msg.arg);
|
654 jelson 1.30 }
655 break;
656
|
657 cvs 1.32 case FUSD_POLL_DIFF:
|
658 cvs 1.31 /* This callback requests notification when an event occurs on a file,
659 * e.g. becoming readable or writable */
|
660 cvs 1.32 if (fops && fops->poll_diff)
|
661 jelson 1.44 user_retval = fops->poll_diff(file, msg->parm.fops_msg.cmd);
|
662 cvs 1.31 break;
663
664 case FUSD_UNBLOCK:
665 /* This callback is called when a system call is interrupted */
666 if (fops && fops->unblock)
|
667 jelson 1.44 user_retval = fops->unblock(file);
|
668 cvs 1.31 break;
|
669 cvs 1.7
670 default:
|
671 jelson 1.42 fprintf(stderr, "libfusd: Got unsupported operation\n");
|
672 jelson 1.44 user_retval = -ENOSYS;
|
673 cvs 1.10 break;
|
674 cvs 1.7 }
|
675 cvs 1.31
|
676 jelson 1.44 goto send_reply;
|
677 cvs 1.9
|
678 cvs 1.8
|
679 jelson 1.44 /* out_noreply is only used for handling errors */
680 out_noreply:
|
681 cvs 1.15 if (msg->data != NULL)
682 free(msg->data);
683 if (msg != NULL)
684 free(msg);
|
685 jelson 1.44 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 jelson 1.44
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 jelson 1.48 * This function now prints an error to stderr in case of error,
714 * instead of returning a -1.
|
715 jelson 1.44 */
|
716 girod 1.66 void fusd_dispatch_aux(int fd, int from_net, fusd_file_operations_t *use_fops, void **local_device_info)
|
717 jelson 1.44 {
718 int retval, num_dispatches = 0;
|
719 girod 1.66 fusd_file_operations_t *fops = use_fops;
|
720 jelson 1.44
|
721 girod 1.66 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 jelson 1.44 }
|
730 girod 1.66
|
731 jelson 1.44 /* now keep dispatching until a dispatch returns an error */
732 do {
|
733 girod 1.66 retval = fusd_dispatch_one(fd, fops, from_net, local_device_info);
734
|
735 jelson 1.44 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 jelson 1.48 if (retval < 0 && errno != EPIPE)
749 fprintf(stderr, "libfusd: fusd_dispatch error on fd %d: %m\n", fd);
|
750 cvs 1.4 }
|
751 cvs 1.3
|
752 cvs 1.9
|
753 girod 1.66 void fusd_dispatch(int fd)
754 {
755 fusd_dispatch_aux(fd, 0, NULL, NULL);
756 }
757
|
758 cvs 1.40 /*
|
759 jelson 1.47 * 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 jelson 1.44 * 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 cvs 1.40 */
|
785 cvs 1.10 int fusd_return(fusd_file_info_t *file, ssize_t retval)
|
786 cvs 1.9 {
|
787 cvs 1.15 fusd_msg_t *msg = NULL;
|
788 jelson 1.44 int fd;
|
789 jelson 1.46 int driver_retval = 0;
|
790 cvs 1.11 struct iovec iov[2];
|
791 girod 1.66 char *msg_data = NULL;
792 int fd_tracking = file->local_device_info == NULL;
793
|
794 jelson 1.44 if (file == NULL) {
795 fprintf(stderr, "fusd_return: NULL file\n");
796 return -EINVAL;
797 }
798
799 fd = file->fd;
|
800 girod 1.66 if (fd_tracking && !FUSD_FD_VALID(fd)) {
|
801 cvs 1.16 fprintf(stderr, "fusd_return: badfd (fd %d)\n", fd);
|
802 jelson 1.44 return -EBADF;
|
803 cvs 1.15 }
|
804 girod 1.66
|
805 cvs 1.15 if ((msg = file->fusd_msg) == NULL) {
|
806 cvs 1.36 fprintf(stderr, "fusd_return: fusd_msg is gone\n");
|
807 jelson 1.44 return -EINVAL;
|
808 cvs 1.15 }
809
|
810 jelson 1.46 /* if this was a "DONTREPLY" message, just free the struct */
811 if (msg->cmd == FUSD_FOPS_CALL_DROPREPLY)
812 goto free_memory;
813
|
814 jelson 1.29 /* 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 jelson 1.30 break;
825 case FUSD_IOCTL:
826 /* ioctl CAN (in read mode) return data to userspace */
|
827 girod 1.52 if ((retval == 0) &&
828 (_IOC_DIR(msg->parm.fops_msg.cmd) & _IOC_READ))
|
829 jelson 1.30 msg->datalen = _IOC_SIZE(msg->parm.fops_msg.cmd);
830 else
831 msg->datalen = 0;
|
832 jelson 1.29 break;
833 default:
834 /* open, close, write, etc. do not return data */
|
835 cvs 1.18 msg->datalen = 0;
|
836 jelson 1.29 break;
|
837 jelson 1.21 }
|
838 cvs 1.18
|
839 girod 1.66 /* 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 cvs 1.10 /* fill the file info struct */
|
852 cvs 1.34 msg->cmd++; /* change FOPS_CALL to FOPS_REPLY; NONBLOCK to NONBLOCK_REPLY */
|
853 cvs 1.10 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 cvs 1.18 /* send message to kernel */
|
859 girod 1.68 if (msg->datalen > 0 && msg_data != NULL) {
|
860 cvs 1.11 iov[0].iov_base = msg;
861 iov[0].iov_len = sizeof(fusd_msg_t);
|
862 girod 1.66 iov[1].iov_base = msg_data;
|
863 cvs 1.11 iov[1].iov_len = msg->datalen;
|
864 jelson 1.46 driver_retval = writev(fd, iov, 2);
|
865 cvs 1.11 }
|
866 jelson 1.29 else {
|
867 jelson 1.46 driver_retval = write(fd, msg, sizeof(fusd_msg_t));
|
868 jelson 1.29 }
|
869 girod 1.68
870 /* restore data pointer for free */
871 msg->data = msg_data;
|
872 cvs 1.9
|
873 jelson 1.46 free_memory:
|
874 jelson 1.47 fusd_destroy(file);
|
875 jelson 1.44
|
876 jelson 1.46 if (driver_retval < 0)
|
877 jelson 1.44 return -errno;
878 else
879 return 0;
|
880 cvs 1.9 }
|
881 cvs 1.40
|
882 girod 1.53
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 girod 1.66
901
|