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