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.71 * $Id: libfusd.c,v 1.70 2004/03/17 07:39:41 nithya Exp $
|
39 jelson 1.37 */
40
|
41 girod 1.71 char libfusd_c_id[] = "$Id: libfusd.c,v 1.70 2004/03/17 07:39:41 nithya 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 if (name == NULL || fops == NULL) {
319 fprintf(stderr, "fusd_register: invalid name or fops argument\n");
320 retval = -EINVAL;
321 goto done;
322 }
323 girod 1.66
324 /* open the fusd control channel */
325 fd = fusd_open_control();
326 if (fd < 0) {
327 retval = -errno;
|
328 jelson 1.57 goto done;
|
329 jelson 1.42 }
|
330 cvs 1.1
|
331 cvs 1.3 /* fd in use? */
332 if (FUSD_FD_VALID(fd)) {
|
333 jelson 1.57 retval = -EBADF;
334 goto done;
|
335 cvs 1.3 }
336
|
337 girod 1.66 /* build the message */
338 retval = fusd_build_reg_msg(&message, name, mode, device_info);
339 if (retval == 0) name = message.parm.register_msg.name;
340 else goto done;
|
341 cvs 1.1
342 /* make the request */
|
343 cvs 1.3 if (write(fd, &message, sizeof(fusd_msg_t)) < 0) {
|
344 girod 1.71 if (errno == EIO) {
345 fprintf(stderr, "FUSD version mismatch?\n");
346 }
|
347 jelson 1.57 retval = -errno;
348 goto done;
|
349 cvs 1.3 }
350
351 /* OK, store the new file state */
|
352 cvs 1.7 FUSD_SET_FOPS(fd, fops);
|
353 cvs 1.27 FD_SET(fd, &fusd_fds);
|
354 cvs 1.6
|
355 cvs 1.1 /* success! */
|
356 jelson 1.57 done:
357 if (retval < 0) {
358 if (fd >= 0)
359 close(fd);
360 errno = -retval;
361 retval = -1;
|
362 girod 1.66 }
363 else if (fusd_postreg_block(name) == 0) {
364 /* report the fd */
365 retval = fd;
366 errno = 0;
|
367 jelson 1.57 }
|
368 girod 1.66
|
369 jelson 1.57 return retval;
|
370 cvs 1.1 }
371
372
373 int fusd_unregister(int fd)
374 {
|
375 cvs 1.3 if (FUSD_FD_VALID(fd)) {
376 /* clear fd location */
|
377 jelson 1.37 FUSD_SET_FOPS(fd, &null_fops);
|
378 cvs 1.27 FD_CLR(fd, &fusd_fds);
|
379 cvs 1.3 /* close */
380 return close(fd);
381 }
382
383 else {
|
384 cvs 1.8 errno = EBADF;
385 return -1;
|
386 cvs 1.3 }
|
387 cvs 1.1 }
|
388 cvs 1.3
389
390 /*
|
391 jelson 1.44 * fusd_run: a convenience function for automatically running a FUSD
392 * driver, for drivers that don't want to manually select on file
393 * descriptors and call fusd_dispatch. This function will
394 * automatically select on all devices the user has registered and
395 * call fusd_dispatch on any one that becomes readable.
|
396 cvs 1.3 */
|
397 jelson 1.44 void fusd_run(void)
|
398 cvs 1.3 {
|
399 cvs 1.22 fd_set tfds;
|
400 cvs 1.3 int status;
|
401 cvs 1.22 int maxfd;
402 int i;
|
403 cvs 1.3
|
404 cvs 1.22 /* locate maxmimum fd in use */
|
405 jelson 1.44 for (maxfd=0, i=0; i < FD_SETSIZE; i++) {
|
406 cvs 1.27 if (FD_ISSET(i, &fusd_fds)) {
|
407 cvs 1.22 maxfd = i;
|
408 cvs 1.24 }
|
409 cvs 1.22 }
410 maxfd++;
411
|
412 jelson 1.44
|
413 cvs 1.3 while (1) {
|
414 cvs 1.4 /* select */
|
415 cvs 1.27 memmove(&tfds, &fusd_fds, sizeof(fd_set));
|
416 jelson 1.44 status = select(maxfd, &tfds, NULL, NULL, NULL);
|
417 cvs 1.24
|
418 cvs 1.4 /* error? */
419 if (status < 0) {
|
420 jelson 1.44 perror("libfusd: fusd_run: error on select");
421 continue;
|
422 cvs 1.4 }
423
424 /* readable? */
|
425 jelson 1.44 for (i = 0; i < maxfd; i++)
426 if (FD_ISSET(i, &tfds))
|
427 jelson 1.48 fusd_dispatch(i);
|
428 cvs 1.3 }
429 }
430
|
431 cvs 1.4
|
432 cvs 1.40 /************************************************************************/
433
434
435 /* reads a fusd kernel-to-userspace message from fd, and puts a
436 * fusd_msg into the memory pointed to by msg (we assume we are passed
437 * a buffer managed by the caller). if there is a data portion to the
438 * message (msg->datalen > 0), we allocate memory for it, set data to
439 * point to that memory. the returned data pointer must also be
440 * managed by the caller. */
|
441 girod 1.66 static
442 int fusd_get_message(int fd, fusd_msg_t *msg, void **local_client)
|
443 cvs 1.5 {
|
444 jelson 1.45 /* read the header part into the kernel */
|
445 cvs 1.5 if (read(fd, msg, sizeof(fusd_msg_t)) < 0) {
|
446 jelson 1.44 if (errno != EAGAIN)
|
447 jelson 1.56 perror("error talking to FUSD control channel on header read");
|
448 jelson 1.44 return -errno;
|
449 cvs 1.5 }
|
450 girod 1.66
451 /* if this is from the net, we return the local client demux token */
452 if (local_client)
453 *local_client = msg->data;
454
455 /* clear the data pointer */
456 msg->data = NULL;
|
457 cvs 1.5
|
458 cvs 1.8 if (msg->magic != FUSD_MSG_MAGIC) {
|
459 cvs 1.35 fprintf(stderr, "libfusd magic number failure\n");
|
460 jelson 1.44 return -EINVAL;
|
461 cvs 1.8 }
462
|
463 jelson 1.45 /* if there's a data part to the message, read it from the kernel. */
|
464 cvs 1.5 if (msg->datalen) {
|
465 jelson 1.45 if ((msg->data = malloc(msg->datalen + 1)) == NULL) {
|
466 jelson 1.28 fprintf(stderr, "libfusd: can't allocate memory\n");
|
467 jelson 1.44 return -ENOMEM; /* this is bad, we are now unsynced */
|
468 cvs 1.8 }
469
|
470 cvs 1.15 if (read(fd, msg->data, msg->datalen) < 0) {
|
471 jelson 1.56 perror("error talking to FUSD control channel on data read");
|
472 cvs 1.15 free(msg->data);
473 msg->data = NULL;
|
474 jelson 1.44 return -EIO;
|
475 cvs 1.5 }
|
476 jelson 1.45
477 /* For convenience, we now ensure that the byte *after* the buffer
478 * is set to 0. (Note we malloc'd one extra byte above.) */
479 msg->data[msg->datalen] = '\0';
|
480 cvs 1.5 }
481
482 return 0;
483 }
484
485
|
486 cvs 1.40 /*
487 * fusd_fdset_add: given an FDSET and "max", add the currently valid
488 * FUSD fds to the set and update max accordingly.
489 */
490 void fusd_fdset_add(fd_set *set, int *max)
491 {
492 int i;
493
494 for (i = 0; i < FD_SETSIZE; i++) {
495 if (FD_ISSET(i, &fusd_fds)) {
496 FD_SET(i, set);
497 if (i > *max) {
498 *max = i;
499 }
500 }
501 }
502 }
503
504
505
506 /*
507 cvs 1.40 * fusd_dispatch_fdset: given an fd_set full of descriptors, call
508 * fusd_dispatch on every descriptor in the set which is a valid FUSD
509 * fd.
510 */
|
511 jelson 1.48 void fusd_dispatch_fdset(fd_set *set)
|
512 cvs 1.40 {
|
513 jelson 1.48 int i;
|
514 cvs 1.40
|
515 jelson 1.48 for (i = 0; i < FD_SETSIZE; i++)
516 if (FD_ISSET(i, set) && FD_ISSET(i, &fusd_fds))
517 fusd_dispatch(i);
|
518 cvs 1.40 }
519
|
520 cvs 1.7
|
521 cvs 1.4 /*
|
522 jelson 1.44 * fusd_dispatch_one() -- read a single kernel-to-userspace message
523 * from fd, then call the appropriate userspace callback function,
524 * based on the message that was read. finally, return the result
525 * back to the kernel, IF the return value from the callback is not
526 * FUSD_NOREPLY.
527 *
528 * On success, returns 0.
529 * On failure, returns a negative number indicating the errno.
|
530 cvs 1.4 */
|
531 girod 1.66 int fusd_dispatch_one(int fd, fusd_file_operations_t *fops,
532 int from_net, void **local_device_info)
|
533 cvs 1.4 {
|
534 cvs 1.15 fusd_file_info_t *file = NULL;
535 fusd_msg_t *msg = NULL;
|
536 jelson 1.44 int driver_retval = 0; /* returned to the FUSD driver */
537 int user_retval = 0; /* returned to the user who made the syscall */
|
538 girod 1.66 void *local_client = NULL;
|
539 cvs 1.8
|
540 cvs 1.7 /* check for valid, look up ops */
|
541 jelson 1.44 if (fops == NULL) {
542 fprintf(stderr, "fusd_dispatch: no fops provided!\n");
543 driver_retval = -EBADF;
544 goto out_noreply;
|
545 cvs 1.6 }
|
546 cvs 1.8
|
547 cvs 1.7 /* allocate memory for fusd_msg_t */
|
548 cvs 1.8 if ((msg = malloc(sizeof(fusd_msg_t))) == NULL) {
|
549 jelson 1.44 driver_retval = -ENOMEM;
|
550 jelson 1.28 fprintf(stderr, "libfusd: can't allocate memory\n");
|
551 jelson 1.44 goto out_noreply;
|
552 cvs 1.8 }
|
553 cvs 1.16 memset(msg, '\0', sizeof(fusd_msg_t));
|
554 cvs 1.7
|
555 cvs 1.8 /* read header and data, if it's there */
|
556 girod 1.66 if ((driver_retval = fusd_get_message(fd, msg, &local_client)) < 0)
|
557 jelson 1.44 goto out_noreply;
|
558 cvs 1.7
559 /* allocate file info struct */
560 file = malloc(sizeof(fusd_file_info_t));
561 if (NULL == file) {
|
562 jelson 1.28 fprintf(stderr, "libfusd: can't allocate memory\n");
|
563 jelson 1.44 driver_retval = -ENOMEM;
564 goto out_noreply;
|
565 cvs 1.7 }
566
567 /* fill the file info struct */
|
568 cvs 1.16 memset(file, '\0', sizeof(fusd_file_info_t));
|
569 girod 1.66
570 /* if we're processing stuff from the net, we need to keep track
571 * of demux info required to route back to the remote client */
572 if (from_net) {
573 file->remote_client = msg->parm.fops_msg.device_info;
574 file->local_client = local_client;
575 if (local_device_info == NULL)
576 fprintf(stderr, "libfusd: net support requires local device info parameter!\n");
577 else
578 msg->parm.fops_msg.device_info = *local_device_info;
579 }
580
581 /* fill the rest of the fields */
|
582 cvs 1.16 file->fd = fd;
|
583 cvs 1.8 file->device_info = msg->parm.fops_msg.device_info;
584 file->private_data = msg->parm.fops_msg.private_info;
585 file->flags = msg->parm.fops_msg.flags;
586 file->pid = msg->parm.fops_msg.pid;
|
587 cvs 1.38 file->uid = msg->parm.fops_msg.uid;
588 file->gid = msg->parm.fops_msg.gid;
|
589 cvs 1.8 file->fusd_msg = msg;
|
590 jelson 1.44
|
591 girod 1.66 /* net support must track its own local device info */
592 file->local_device_info = local_device_info;
593
|
594 jelson 1.44 /* right now we only handle fops requests */
|
595 jelson 1.46 if (msg->cmd != FUSD_FOPS_CALL && msg->cmd != FUSD_FOPS_NONBLOCK &&
596 msg->cmd != FUSD_FOPS_CALL_DROPREPLY) {
|
597 jelson 1.44 fprintf(stderr, "libfusd: got unknown msg->cmd from kernel\n");
598 user_retval = -EINVAL;
599 goto send_reply;
600 }
|
601 cvs 1.4
|
602 cvs 1.7 /* dispatch on operation type */
|
603 jelson 1.44 user_retval = -ENOSYS;
|
604 cvs 1.7 switch (msg->subcmd) {
605 case FUSD_OPEN:
|
606 cvs 1.16 if (fops && fops->open)
|
607 jelson 1.44 user_retval = fops->open(file);
|
608 cvs 1.9 break;
|
609 cvs 1.7 case FUSD_CLOSE:
|
610 cvs 1.17 if (fops && fops->close)
|
611 jelson 1.44 user_retval = fops->close(file);
|
612 cvs 1.17 break;
|
613 cvs 1.7 case FUSD_READ:
|
614 cvs 1.18 /* allocate a buffer and make the call */
615 if (fops && fops->read) {
616 if ((msg->data = malloc(msg->parm.fops_msg.length)) == NULL) {
|
617 jelson 1.44 user_retval = -ENOMEM;
|
618 jelson 1.28 fprintf(stderr, "libfusd: can't allocate memory\n");
|
619 cvs 1.18 } else {
|
620 jelson 1.21 msg->datalen = msg->parm.fops_msg.length;
|
621 nithya 1.70 memset(msg->data, 0, msg->datalen);
|
622 jelson 1.44 user_retval = fops->read(file, msg->data, msg->datalen,
623 &msg->parm.fops_msg.offset);
|
624 cvs 1.18 }
625 }
626 break;
|
627 cvs 1.7 case FUSD_WRITE:
|
628 jelson 1.29 if (fops && fops->write)
|
629 jelson 1.44 user_retval = fops->write(file, msg->data, msg->datalen,
630 &msg->parm.fops_msg.offset);
|
631 jelson 1.29 break;
|
632 cvs 1.7 case FUSD_IOCTL:
|
633 jelson 1.30 if (fops && fops->ioctl) {
634 /* in the case of an ioctl read, allocate a buffer for the
635 * driver to write to, IF there isn't already a buffer. (there
636 * might already be a buffer if this is a read+write) */
637 if ((_IOC_DIR(msg->parm.fops_msg.cmd) & _IOC_READ) &&
638 msg->data == NULL) {
639 msg->datalen = _IOC_SIZE(msg->parm.fops_msg.cmd);
640 if ((msg->data = malloc(msg->datalen)) == NULL) {
|
641 jelson 1.44 user_retval = -ENOMEM;
|
642 jelson 1.30 break;
643 }
644 }
645 if (msg->data != NULL)
|
646 jelson 1.44 user_retval = fops->ioctl(file, msg->parm.fops_msg.cmd, msg->data);
|
647 jelson 1.30 else
|
648 jelson 1.44 user_retval = fops->ioctl(file, msg->parm.fops_msg.cmd,
649 (void *) msg->parm.fops_msg.arg);
|
650 jelson 1.30 }
651 break;
652
|
653 cvs 1.32 case FUSD_POLL_DIFF:
|
654 cvs 1.31 /* This callback requests notification when an event occurs on a file,
655 * e.g. becoming readable or writable */
|
656 cvs 1.32 if (fops && fops->poll_diff)
|
657 jelson 1.44 user_retval = fops->poll_diff(file, msg->parm.fops_msg.cmd);
|
658 cvs 1.31 break;
659
660 case FUSD_UNBLOCK:
661 /* This callback is called when a system call is interrupted */
662 if (fops && fops->unblock)
|
663 jelson 1.44 user_retval = fops->unblock(file);
|
664 cvs 1.31 break;
|
665 cvs 1.7
666 default:
|
667 jelson 1.42 fprintf(stderr, "libfusd: Got unsupported operation\n");
|
668 jelson 1.44 user_retval = -ENOSYS;
|
669 cvs 1.10 break;
|
670 cvs 1.7 }
|
671 cvs 1.31
|
672 jelson 1.44 goto send_reply;
|
673 cvs 1.9
|
674 cvs 1.8
|
675 jelson 1.44 /* out_noreply is only used for handling errors */
676 out_noreply:
|
677 cvs 1.15 if (msg->data != NULL)
678 free(msg->data);
679 if (msg != NULL)
680 free(msg);
|
681 jelson 1.44 goto done;
682
683 /* send_reply is only used for success */
684 send_reply:
685 if (-user_retval <= 0xff) {
686 /* 0xff is the maximum legal return value (?) - return val to user */
687 driver_retval = fusd_return(file, user_retval);
688 } else {
689 /* if we got a FUSD_NOREPLY, don't free the msg structure */
690 driver_retval = 0;
691 }
692
693 /* this is common to both errors and success */
694 done:
695 if (driver_retval < 0) {
696 errno = -driver_retval;
697 driver_retval = -1;
698 }
699 return driver_retval;
700 }
701
702 jelson 1.44
703 /* fusd_dispatch is now a wrapper around fusd_dispatch_one that calls
704 * it repeatedly, until it fails. this helps a lot with bulk data
705 * transfer since there is no intermediate select in between the
706 * reads. (the kernel module helps by running the user process in
707 * between).
708 *
|
709 jelson 1.48 * This function now prints an error to stderr in case of error,
710 * instead of returning a -1.
|
711 jelson 1.44 */
|
712 girod 1.66 void fusd_dispatch_aux(int fd, int from_net, fusd_file_operations_t *use_fops, void **local_device_info)
|
713 jelson 1.44 {
714 int retval, num_dispatches = 0;
|
715 girod 1.66 fusd_file_operations_t *fops = use_fops;
|
716 jelson 1.44
|
717 girod 1.66 if (fops == NULL) {
718 /* make sure we have a valid FD, and get its fops structure */
719 if (!FUSD_FD_VALID(fd)) {
720 errno = EBADF;
721 retval = -1;
722 goto out;
723 }
724 fops = FUSD_GET_FOPS(fd);
|
725 jelson 1.44 }
|
726 girod 1.66
|
727 jelson 1.44 /* now keep dispatching until a dispatch returns an error */
728 do {
|
729 girod 1.66 retval = fusd_dispatch_one(fd, fops, from_net, local_device_info);
730
|
731 jelson 1.44 if (retval >= 0)
732 num_dispatches++;
733 } while (retval >= 0 && num_dispatches <= MAX_MESSAGES_PER_DISPATCH);
734
735 /* if we've dispatched at least one message successfully, and then
736 * stopped because of EAGAIN - do not report an error. this is the
737 * common case. */
738 if (num_dispatches > 0 && errno == EAGAIN) {
739 retval = 0;
740 errno = 0;
741 }
742
743 out:
|
744 jelson 1.48 if (retval < 0 && errno != EPIPE)
745 fprintf(stderr, "libfusd: fusd_dispatch error on fd %d: %m\n", fd);
|
746 cvs 1.4 }
|
747 cvs 1.3
|
748 cvs 1.9
|
749 girod 1.66 void fusd_dispatch(int fd)
750 {
751 fusd_dispatch_aux(fd, 0, NULL, NULL);
752 }
753
|
754 cvs 1.40 /*
|
755 jelson 1.47 * fusd_destroy destroys all state associated with a fusd_file_info
756 * pointer. (It is implicitly called by fusd_return.) If a driver
757 * saves a fusd_file_info pointer by calling -FUSD_NOREPLY in order to
758 * block a read, but gets a "close" request on the file before the
759 * pointer is returned with fusd_return, it should be thrown away
760 * using fusd_destroy.
761 */
762 void fusd_destroy(struct fusd_file_info *file)
763 {
764 if (file == NULL)
765 return;
766
767 if (file->fusd_msg->data != NULL)
768 free(file->fusd_msg->data);
769 free(file->fusd_msg);
770 free(file);
771 }
772
773
774 /*
|
775 jelson 1.44 * construct a user-to-kernel message in reply to a file function
776 * call.
777 *
778 * On success, returns 0.
779 * On failure, returns a negative number indicating the errno.
|
780 cvs 1.40 */
|
781 cvs 1.10 int fusd_return(fusd_file_info_t *file, ssize_t retval)
|
782 cvs 1.9 {
|
783 cvs 1.15 fusd_msg_t *msg = NULL;
|
784 jelson 1.44 int fd;
|
785 jelson 1.46 int driver_retval = 0;
|
786 cvs 1.11 struct iovec iov[2];
|
787 girod 1.66 char *msg_data = NULL;
788 int fd_tracking = file->local_device_info == NULL;
789
|
790 jelson 1.44 if (file == NULL) {
791 fprintf(stderr, "fusd_return: NULL file\n");
792 return -EINVAL;
793 }
794
795 fd = file->fd;
|
796 girod 1.66 if (fd_tracking && !FUSD_FD_VALID(fd)) {
|
797 cvs 1.16 fprintf(stderr, "fusd_return: badfd (fd %d)\n", fd);
|
798 jelson 1.44 return -EBADF;
|
799 cvs 1.15 }
|
800 girod 1.66
|
801 cvs 1.15 if ((msg = file->fusd_msg) == NULL) {
|
802 cvs 1.36 fprintf(stderr, "fusd_return: fusd_msg is gone\n");
|
803 jelson 1.44 return -EINVAL;
|
804 cvs 1.15 }
805
|
806 jelson 1.46 /* if this was a "DONTREPLY" message, just free the struct */
807 if (msg->cmd == FUSD_FOPS_CALL_DROPREPLY)
808 goto free_memory;
809
|
810 jelson 1.29 /* do we copy data back to kernel? how much? */
811 switch(msg->subcmd) {
812 case FUSD_READ:
813 /* these operations can return data to userspace */
814 if (retval > 0) {
815 msg->datalen = MIN(retval, msg->parm.fops_msg.length);
816 retval = msg->datalen;
817 } else {
818 msg->datalen = 0;
819 }
|
820 jelson 1.30 break;
821 case FUSD_IOCTL:
822 /* ioctl CAN (in read mode) return data to userspace */
|
823 girod 1.52 if ((retval == 0) &&
824 (_IOC_DIR(msg->parm.fops_msg.cmd) & _IOC_READ))
|
825 jelson 1.30 msg->datalen = _IOC_SIZE(msg->parm.fops_msg.cmd);
826 else
827 msg->datalen = 0;
|
828 jelson 1.29 break;
829 default:
830 /* open, close, write, etc. do not return data */
|
831 cvs 1.18 msg->datalen = 0;
|
832 jelson 1.29 break;
|
833 jelson 1.21 }
|
834 cvs 1.18
|
835 girod 1.66 /* save the local device info if we're tracking it */
836 if (file->local_device_info)
837 *(file->local_device_info) = file->device_info;
838
839 /* handle net case: if we have one, patch in the remote's device info */
840 msg->parm.fops_msg.device_info =
841 file->remote_client ? file->remote_client : file->device_info;
842
843 /* handle net case: patch in the local demux info */
844 msg_data = msg->data;
845 msg->data = file->local_client;
846
|
847 cvs 1.10 /* fill the file info struct */
|
848 cvs 1.34 msg->cmd++; /* change FOPS_CALL to FOPS_REPLY; NONBLOCK to NONBLOCK_REPLY */
|
849 cvs 1.10 msg->parm.fops_msg.retval = retval;
850 msg->parm.fops_msg.private_info = file->private_data;
851 msg->parm.fops_msg.flags = file->flags;
852 /* pid is NOT copied back. */
853
|
854 cvs 1.18 /* send message to kernel */
|
855 girod 1.68 if (msg->datalen > 0 && msg_data != NULL) {
|
856 cvs 1.11 iov[0].iov_base = msg;
857 iov[0].iov_len = sizeof(fusd_msg_t);
|
858 girod 1.66 iov[1].iov_base = msg_data;
|
859 cvs 1.11 iov[1].iov_len = msg->datalen;
|
860 jelson 1.46 driver_retval = writev(fd, iov, 2);
|
861 cvs 1.11 }
|
862 jelson 1.29 else {
|
863 jelson 1.46 driver_retval = write(fd, msg, sizeof(fusd_msg_t));
|
864 jelson 1.29 }
|
865 girod 1.68
866 /* restore data pointer for free */
867 msg->data = msg_data;
|
868 cvs 1.9
|
869 jelson 1.46 free_memory:
|
870 jelson 1.47 fusd_destroy(file);
|
871 jelson 1.44
|
872 jelson 1.46 if (driver_retval < 0)
|
873 jelson 1.44 return -errno;
874 else
875 return 0;
|
876 cvs 1.9 }
|
877 cvs 1.40
|
878 girod 1.53
879 /* returns static string representing the flagset (e.g. RWE) */
880 #define RING 5
881 char *fusd_unparse_flags(int flags)
882 {
883 static int i = 0;
884 static char ringbuf[RING][5];
885 char *s = ringbuf[i];
886 i = (i + 1) % RING;
887
888 sprintf(s, "%c%c%c",
889 (flags & FUSD_NOTIFY_INPUT)?'R':'-',
890 (flags & FUSD_NOTIFY_OUTPUT)?'W':'-',
891 (flags & FUSD_NOTIFY_EXCEPT)?'E':'-');
892
893 return s;
894 }
895 #undef RING
|
896 girod 1.66
897
|