libfusd.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  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. * fusd userspace library: functions that know how to properly talk
  32. * to the fusd kernel module
  33. *
  34. * authors: jelson and girod
  35. *
  36. * $Id$
  37. */
  38. char libfusd_c_id[] = "$Id$";
  39. #include <stdio.h>
  40. #include <stdlib.h>
  41. #include <unistd.h>
  42. #include <sys/types.h>
  43. #include <sys/stat.h>
  44. #include <sys/time.h>
  45. #include <sys/uio.h>
  46. #include <sys/ioctl.h>
  47. #include <fcntl.h>
  48. #include <errno.h>
  49. #include <string.h>
  50. #include <time.h>
  51. #include "fusd.h"
  52. #include "fusd_msg.h"
  53. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  54. /* maximum number of messages processed by a single call to fusd_dispatch */
  55. #define MAX_MESSAGES_PER_DISPATCH 40
  56. /* used for fusd_run */
  57. static fd_set fusd_fds;
  58. /* default prefix of devices (often "/dev/") */
  59. char *dev_root = NULL;
  60. /*
  61. * fusd_fops_set is an array that keeps track of the file operations
  62. * struct for each fusd fd.
  63. */
  64. static fusd_file_operations_t fusd_fops_set[FD_SETSIZE];
  65. fusd_file_operations_t null_fops = { NULL };
  66. /*
  67. * accessor macros
  68. */
  69. #define FUSD_GET_FOPS(fd) \
  70. (fusd_fops_set + (fd))
  71. #define FUSD_SET_FOPS(fd,fi) \
  72. (fusd_fops_set[(fd)]=(*fi))
  73. #define FUSD_FD_VALID(fd) \
  74. (((fd)>=0) && \
  75. ((fd)<FD_SETSIZE) && \
  76. (memcmp(FUSD_GET_FOPS(fd), &null_fops, sizeof(fusd_file_operations_t))))
  77. /*
  78. * fusd_init
  79. *
  80. * this is called automatically before the first
  81. * register call
  82. */
  83. void fusd_init()
  84. {
  85. static int fusd_init_needed = 1;
  86. if (fusd_init_needed) {
  87. int i;
  88. fusd_init_needed = 0;
  89. for (i = 0; i < FD_SETSIZE; i++)
  90. FUSD_SET_FOPS(i, &null_fops);
  91. FD_ZERO(&fusd_fds);
  92. dev_root = DEFAULT_DEV_ROOT;
  93. }
  94. }
  95. int fusd_register(const char *name, const char* clazz, const char* devname, mode_t mode, void *device_info,
  96. struct fusd_file_operations *fops)
  97. {
  98. int fd = -1, retval = 0;
  99. fusd_msg_t message;
  100. /* need initialization? */
  101. fusd_init();
  102. /* make sure the name is valid and we have a valid set of fops... */
  103. if (name == NULL || fops == NULL) {
  104. fprintf(stderr, "fusd_register: invalid name or fops argument\n");
  105. retval = -EINVAL;
  106. goto done;
  107. }
  108. /*
  109. * convenience: if the first characters of the name you're trying
  110. * to register are SKIP_PREFIX (usually "/dev/"), skip over them.
  111. */
  112. if (dev_root != NULL && strlen(name) > strlen(dev_root) &&
  113. !strncmp(name, dev_root, strlen(dev_root))) {
  114. name += strlen(dev_root);
  115. }
  116. if (strlen(name) > FUSD_MAX_NAME_LENGTH) {
  117. fprintf(stderr, "name '%s' too long, sorry :(", name);
  118. retval = -EINVAL;
  119. goto done;
  120. }
  121. /* open the fusd control channel */
  122. if ((fd = open(FUSD_CONTROL_DEVNAME, O_RDWR | O_NONBLOCK)) < 0) {
  123. /* if the problem is that /dev/fusd does not exist, return the
  124. * message "Package not installed", which is hopefully more
  125. * illuminating than "no such file or directory" */
  126. if (errno == ENOENT) {
  127. fprintf(stderr, "libfusd: %s does not exist; ensure FUSD's kernel module is installed\n",
  128. FUSD_CONTROL_DEVNAME);
  129. retval = -ENOPKG;
  130. } else {
  131. perror("libfusd: trying to open FUSD control channel");
  132. retval = -errno;
  133. }
  134. goto done;
  135. }
  136. /* fd in use? */
  137. if (FUSD_FD_VALID(fd)) {
  138. retval = -EBADF;
  139. goto done;
  140. }
  141. /* set up the message */
  142. memset(&message, 0, sizeof(message));
  143. message.magic = FUSD_MSG_MAGIC;
  144. message.cmd = FUSD_REGISTER_DEVICE;
  145. message.datalen = 0;
  146. strcpy(message.parm.register_msg.name, name);
  147. strcpy(message.parm.register_msg.clazz, clazz);
  148. strcpy(message.parm.register_msg.devname, devname);
  149. message.parm.register_msg.mode = mode;
  150. message.parm.register_msg.device_info = device_info;
  151. /* make the request */
  152. if (write(fd, &message, sizeof(fusd_msg_t)) < 0) {
  153. retval = -errno;
  154. goto done;
  155. }
  156. /* OK, store the new file state */
  157. FUSD_SET_FOPS(fd, fops);
  158. FD_SET(fd, &fusd_fds);
  159. /* success! */
  160. done:
  161. if (retval < 0) {
  162. if (fd >= 0)
  163. close(fd);
  164. errno = -retval;
  165. retval = -1;
  166. } else {
  167. errno = 0;
  168. retval = fd;
  169. }
  170. return retval;
  171. }
  172. int fusd_unregister(int fd)
  173. {
  174. if (FUSD_FD_VALID(fd)) {
  175. /* clear fd location */
  176. FUSD_SET_FOPS(fd, &null_fops);
  177. FD_CLR(fd, &fusd_fds);
  178. /* close */
  179. return close(fd);
  180. }
  181. else {
  182. errno = EBADF;
  183. return -1;
  184. }
  185. }
  186. /*
  187. * fusd_run: a convenience function for automatically running a FUSD
  188. * driver, for drivers that don't want to manually select on file
  189. * descriptors and call fusd_dispatch. This function will
  190. * automatically select on all devices the user has registered and
  191. * call fusd_dispatch on any one that becomes readable.
  192. */
  193. void fusd_run(void)
  194. {
  195. fd_set tfds;
  196. int status;
  197. int maxfd;
  198. int i;
  199. /* locate maxmimum fd in use */
  200. for (maxfd=0, i=0; i < FD_SETSIZE; i++) {
  201. if (FD_ISSET(i, &fusd_fds)) {
  202. maxfd = i;
  203. }
  204. }
  205. maxfd++;
  206. while (1) {
  207. /* select */
  208. memmove(&tfds, &fusd_fds, sizeof(fd_set));
  209. status = select(maxfd, &tfds, NULL, NULL, NULL);
  210. /* error? */
  211. if (status < 0) {
  212. perror("libfusd: fusd_run: error on select");
  213. continue;
  214. }
  215. /* readable? */
  216. for (i = 0; i < maxfd; i++)
  217. if (FD_ISSET(i, &tfds))
  218. fusd_dispatch(i);
  219. }
  220. }
  221. /************************************************************************/
  222. /* reads a fusd kernel-to-userspace message from fd, and puts a
  223. * fusd_msg into the memory pointed to by msg (we assume we are passed
  224. * a buffer managed by the caller). if there is a data portion to the
  225. * message (msg->datalen > 0), we allocate memory for it, set data to
  226. * point to that memory. the returned data pointer must also be
  227. * managed by the caller. */
  228. static int fusd_get_message(int fd, fusd_msg_t *msg)
  229. {
  230. /* read the header part into the kernel */
  231. if (read(fd, msg, sizeof(fusd_msg_t)) < 0) {
  232. if (errno != EAGAIN)
  233. perror("error talking to FUSD control channel on header read");
  234. return -errno;
  235. }
  236. msg->data = NULL; /* pointers in kernelspace have no meaning */
  237. if (msg->magic != FUSD_MSG_MAGIC) {
  238. fprintf(stderr, "libfusd magic number failure\n");
  239. return -EINVAL;
  240. }
  241. /* if there's a data part to the message, read it from the kernel. */
  242. if (msg->datalen) {
  243. if ((msg->data = malloc(msg->datalen + 1)) == NULL) {
  244. fprintf(stderr, "libfusd: can't allocate memory\n");
  245. return -ENOMEM; /* this is bad, we are now unsynced */
  246. }
  247. if (read(fd, msg->data, msg->datalen) < 0) {
  248. perror("error talking to FUSD control channel on data read");
  249. free(msg->data);
  250. msg->data = NULL;
  251. return -EIO;
  252. }
  253. /* For convenience, we now ensure that the byte *after* the buffer
  254. * is set to 0. (Note we malloc'd one extra byte above.) */
  255. msg->data[msg->datalen] = '\0';
  256. }
  257. return 0;
  258. }
  259. /*
  260. * fusd_fdset_add: given an FDSET and "max", add the currently valid
  261. * FUSD fds to the set and update max accordingly.
  262. */
  263. void fusd_fdset_add(fd_set *set, int *max)
  264. {
  265. int i;
  266. for (i = 0; i < FD_SETSIZE; i++) {
  267. if (FD_ISSET(i, &fusd_fds)) {
  268. FD_SET(i, set);
  269. if (i > *max) {
  270. *max = i;
  271. }
  272. }
  273. }
  274. }
  275. /*
  276. * fusd_dispatch_fdset: given an fd_set full of descriptors, call
  277. * fusd_dispatch on every descriptor in the set which is a valid FUSD
  278. * fd.
  279. */
  280. void fusd_dispatch_fdset(fd_set *set)
  281. {
  282. int i;
  283. for (i = 0; i < FD_SETSIZE; i++)
  284. if (FD_ISSET(i, set) && FD_ISSET(i, &fusd_fds))
  285. fusd_dispatch(i);
  286. }
  287. /*
  288. * fusd_dispatch_one() -- read a single kernel-to-userspace message
  289. * from fd, then call the appropriate userspace callback function,
  290. * based on the message that was read. finally, return the result
  291. * back to the kernel, IF the return value from the callback is not
  292. * FUSD_NOREPLY.
  293. *
  294. * On success, returns 0.
  295. * On failure, returns a negative number indicating the errno.
  296. */
  297. static int fusd_dispatch_one(int fd, fusd_file_operations_t *fops)
  298. {
  299. fusd_file_info_t *file = NULL;
  300. fusd_msg_t *msg = NULL;
  301. int driver_retval = 0; /* returned to the FUSD driver */
  302. int user_retval = 0; /* returned to the user who made the syscall */
  303. /* check for valid, look up ops */
  304. if (fops == NULL) {
  305. fprintf(stderr, "fusd_dispatch: no fops provided!\n");
  306. driver_retval = -EBADF;
  307. goto out_noreply;
  308. }
  309. /* allocate memory for fusd_msg_t */
  310. if ((msg = malloc(sizeof(fusd_msg_t))) == NULL) {
  311. driver_retval = -ENOMEM;
  312. fprintf(stderr, "libfusd: can't allocate memory\n");
  313. goto out_noreply;
  314. }
  315. memset(msg, '\0', sizeof(fusd_msg_t));
  316. /* read header and data, if it's there */
  317. if ((driver_retval = fusd_get_message(fd, msg)) < 0)
  318. goto out_noreply;
  319. /* allocate file info struct */
  320. file = malloc(sizeof(fusd_file_info_t));
  321. if (NULL == file) {
  322. fprintf(stderr, "libfusd: can't allocate memory\n");
  323. driver_retval = -ENOMEM;
  324. goto out_noreply;
  325. }
  326. /* fill the file info struct */
  327. memset(file, '\0', sizeof(fusd_file_info_t));
  328. file->fd = fd;
  329. file->device_info = msg->parm.fops_msg.device_info;
  330. file->private_data = msg->parm.fops_msg.private_info;
  331. file->flags = msg->parm.fops_msg.flags;
  332. file->pid = msg->parm.fops_msg.pid;
  333. file->uid = msg->parm.fops_msg.uid;
  334. file->gid = msg->parm.fops_msg.gid;
  335. file->fusd_msg = msg;
  336. /* right now we only handle fops requests */
  337. if (msg->cmd != FUSD_FOPS_CALL && msg->cmd != FUSD_FOPS_NONBLOCK &&
  338. msg->cmd != FUSD_FOPS_CALL_DROPREPLY) {
  339. fprintf(stderr, "libfusd: got unknown msg->cmd from kernel\n");
  340. user_retval = -EINVAL;
  341. goto send_reply;
  342. }
  343. /* dispatch on operation type */
  344. user_retval = -ENOSYS;
  345. switch (msg->subcmd) {
  346. case FUSD_OPEN:
  347. if (fops && fops->open)
  348. user_retval = fops->open(file);
  349. break;
  350. case FUSD_CLOSE:
  351. if (fops && fops->close)
  352. user_retval = fops->close(file);
  353. break;
  354. case FUSD_READ:
  355. /* allocate a buffer and make the call */
  356. if (fops && fops->read) {
  357. if ((msg->data = malloc(msg->parm.fops_msg.length)) == NULL) {
  358. user_retval = -ENOMEM;
  359. fprintf(stderr, "libfusd: can't allocate memory\n");
  360. } else {
  361. msg->datalen = msg->parm.fops_msg.length;
  362. user_retval = fops->read(file, msg->data, msg->datalen,
  363. &msg->parm.fops_msg.offset);
  364. }
  365. }
  366. break;
  367. case FUSD_WRITE:
  368. if (fops && fops->write)
  369. user_retval = fops->write(file, msg->data, msg->datalen,
  370. &msg->parm.fops_msg.offset);
  371. break;
  372. case FUSD_MMAP:
  373. if (fops && fops->mmap)
  374. {
  375. user_retval = fops->mmap(file, msg->parm.fops_msg.offset, msg->parm.fops_msg.length, msg->parm.fops_msg.flags,
  376. &msg->parm.fops_msg.arg.ptr_arg, &msg->parm.fops_msg.length);
  377. }
  378. break;
  379. case FUSD_IOCTL:
  380. if (fops && fops->ioctl) {
  381. /* in the case of an ioctl read, allocate a buffer for the
  382. * driver to write to, IF there isn't already a buffer. (there
  383. * might already be a buffer if this is a read+write) */
  384. if ((_IOC_DIR(msg->parm.fops_msg.cmd) & _IOC_READ) &&
  385. msg->data == NULL) {
  386. msg->datalen = _IOC_SIZE(msg->parm.fops_msg.cmd);
  387. if ((msg->data = malloc(msg->datalen)) == NULL) {
  388. user_retval = -ENOMEM;
  389. break;
  390. }
  391. }
  392. if (msg->data != NULL)
  393. user_retval = fops->ioctl(file, msg->parm.fops_msg.cmd, msg->data);
  394. else
  395. user_retval = fops->ioctl(file, msg->parm.fops_msg.cmd,
  396. (void *) msg->parm.fops_msg.arg.ptr_arg);
  397. }
  398. break;
  399. case FUSD_POLL_DIFF:
  400. /* This callback requests notification when an event occurs on a file,
  401. * e.g. becoming readable or writable */
  402. if (fops && fops->poll_diff)
  403. user_retval = fops->poll_diff(file, msg->parm.fops_msg.cmd);
  404. break;
  405. case FUSD_UNBLOCK:
  406. /* This callback is called when a system call is interrupted */
  407. if (fops && fops->unblock)
  408. user_retval = fops->unblock(file);
  409. break;
  410. default:
  411. fprintf(stderr, "libfusd: Got unsupported operation\n");
  412. user_retval = -ENOSYS;
  413. break;
  414. }
  415. goto send_reply;
  416. /* out_noreply is only used for handling errors */
  417. out_noreply:
  418. if (msg->data != NULL)
  419. free(msg->data);
  420. if (msg != NULL)
  421. free(msg);
  422. goto done;
  423. /* send_reply is only used for success */
  424. send_reply:
  425. if (-user_retval <= 0xff) {
  426. /* 0xff is the maximum legal return value (?) - return val to user */
  427. driver_retval = fusd_return(file, user_retval);
  428. } else {
  429. /* if we got a FUSD_NOREPLY, don't free the msg structure */
  430. driver_retval = 0;
  431. }
  432. /* this is common to both errors and success */
  433. done:
  434. if (driver_retval < 0) {
  435. errno = -driver_retval;
  436. driver_retval = -1;
  437. }
  438. return driver_retval;
  439. }
  440. /* fusd_dispatch is now a wrapper around fusd_dispatch_one that calls
  441. * it repeatedly, until it fails. this helps a lot with bulk data
  442. * transfer since there is no intermediate select in between the
  443. * reads. (the kernel module helps by running the user process in
  444. * between).
  445. *
  446. * This function now prints an error to stderr in case of error,
  447. * instead of returning a -1.
  448. */
  449. void fusd_dispatch(int fd)
  450. {
  451. int retval, num_dispatches = 0;
  452. fusd_file_operations_t *fops = NULL;
  453. /* make sure we have a valid FD, and get its fops structure */
  454. if (!FUSD_FD_VALID(fd)) {
  455. errno = EBADF;
  456. retval = -1;
  457. goto out;
  458. }
  459. fops = FUSD_GET_FOPS(fd);
  460. /* now keep dispatching until a dispatch returns an error */
  461. do {
  462. retval = fusd_dispatch_one(fd, fops);
  463. if (retval >= 0)
  464. num_dispatches++;
  465. } while (retval >= 0 && num_dispatches <= MAX_MESSAGES_PER_DISPATCH);
  466. /* if we've dispatched at least one message successfully, and then
  467. * stopped because of EAGAIN - do not report an error. this is the
  468. * common case. */
  469. if (num_dispatches > 0 && errno == EAGAIN) {
  470. retval = 0;
  471. errno = 0;
  472. }
  473. out:
  474. if (retval < 0 && errno != EPIPE)
  475. fprintf(stderr, "libfusd: fusd_dispatch error on fd %d: %m\n", fd);
  476. }
  477. /*
  478. * fusd_destroy destroys all state associated with a fusd_file_info
  479. * pointer. (It is implicitly called by fusd_return.) If a driver
  480. * saves a fusd_file_info pointer by calling -FUSD_NOREPLY in order to
  481. * block a read, but gets a "close" request on the file before the
  482. * pointer is returned with fusd_return, it should be thrown away
  483. * using fusd_destroy.
  484. */
  485. void fusd_destroy(struct fusd_file_info *file)
  486. {
  487. if (file == NULL)
  488. return;
  489. if (file->fusd_msg->data != NULL)
  490. free(file->fusd_msg->data);
  491. free(file->fusd_msg);
  492. free(file);
  493. }
  494. /*
  495. * construct a user-to-kernel message in reply to a file function
  496. * call.
  497. *
  498. * On success, returns 0.
  499. * On failure, returns a negative number indicating the errno.
  500. */
  501. int fusd_return(fusd_file_info_t *file, ssize_t retval)
  502. {
  503. fusd_msg_t *msg = NULL;
  504. int fd;
  505. int driver_retval = 0;
  506. struct iovec iov[2];
  507. if (file == NULL) {
  508. fprintf(stderr, "fusd_return: NULL file\n");
  509. return -EINVAL;
  510. }
  511. fd = file->fd;
  512. if (!FUSD_FD_VALID(fd)) {
  513. fprintf(stderr, "fusd_return: badfd (fd %d)\n", fd);
  514. return -EBADF;
  515. }
  516. if ((msg = file->fusd_msg) == NULL) {
  517. fprintf(stderr, "fusd_return: fusd_msg is gone\n");
  518. return -EINVAL;
  519. }
  520. /* if this was a "DONTREPLY" message, just free the struct */
  521. if (msg->cmd == FUSD_FOPS_CALL_DROPREPLY)
  522. goto free_memory;
  523. /* do we copy data back to kernel? how much? */
  524. switch(msg->subcmd) {
  525. case FUSD_READ:
  526. /* these operations can return data to userspace */
  527. if (retval > 0) {
  528. msg->datalen = MIN(retval, msg->parm.fops_msg.length);
  529. retval = msg->datalen;
  530. } else {
  531. msg->datalen = 0;
  532. }
  533. break;
  534. case FUSD_IOCTL:
  535. /* ioctl CAN (in read mode) return data to userspace */
  536. if ((retval == 0) &&
  537. (_IOC_DIR(msg->parm.fops_msg.cmd) & _IOC_READ))
  538. msg->datalen = _IOC_SIZE(msg->parm.fops_msg.cmd);
  539. else
  540. msg->datalen = 0;
  541. break;
  542. default:
  543. /* open, close, write, etc. do not return data */
  544. msg->datalen = 0;
  545. break;
  546. }
  547. /* fill the file info struct */
  548. msg->cmd++; /* change FOPS_CALL to FOPS_REPLY; NONBLOCK to NONBLOCK_REPLY */
  549. msg->parm.fops_msg.retval = retval;
  550. msg->parm.fops_msg.device_info = file->device_info;
  551. msg->parm.fops_msg.private_info = file->private_data;
  552. msg->parm.fops_msg.flags = file->flags;
  553. /* pid is NOT copied back. */
  554. /* send message to kernel */
  555. if (msg->datalen && msg->data != NULL) {
  556. iov[0].iov_base = msg;
  557. iov[0].iov_len = sizeof(fusd_msg_t);
  558. iov[1].iov_base = msg->data;
  559. iov[1].iov_len = msg->datalen;
  560. driver_retval = writev(fd, iov, 2);
  561. }
  562. else {
  563. driver_retval = write(fd, msg, sizeof(fusd_msg_t));
  564. }
  565. free_memory:
  566. fusd_destroy(file);
  567. if (driver_retval < 0)
  568. return -errno;
  569. else
  570. return 0;
  571. }
  572. /* returns static string representing the flagset (e.g. RWE) */
  573. #define RING 5
  574. char *fusd_unparse_flags(int flags)
  575. {
  576. static int i = 0;
  577. static char ringbuf[RING][5];
  578. char *s = ringbuf[i];
  579. i = (i + 1) % RING;
  580. sprintf(s, "%c%c%c",
  581. (flags & FUSD_NOTIFY_INPUT)?'R':'-',
  582. (flags & FUSD_NOTIFY_OUTPUT)?'W':'-',
  583. (flags & FUSD_NOTIFY_EXCEPT)?'E':'-');
  584. return s;
  585. }
  586. #undef RING