You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

229 lines
4.0 KiB

  1. /*
  2. FUSE: Filesystem in Userspace
  3. Copyright (C) 2001-2007 Miklos Szeredi <miklos@szeredi.hu>
  4. This program can be distributed under the terms of the GNU LGPLv2.
  5. See the file COPYING.LIB
  6. */
  7. #include "fuse_i.h"
  8. #include "fuse_misc.h"
  9. #include "fuse_kernel.h"
  10. #include <assert.h>
  11. #include <errno.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <unistd.h>
  16. #include <sys/uio.h>
  17. struct fuse_chan
  18. {
  19. struct fuse_session *se;
  20. int fd;
  21. size_t bufsize;
  22. };
  23. struct fuse_session *fuse_session_new(void *data,
  24. void *receive_buf,
  25. void *process_buf,
  26. void *destroy)
  27. {
  28. struct fuse_session *se = (struct fuse_session *) malloc(sizeof(*se));
  29. if (se == NULL) {
  30. fprintf(stderr, "fuse: failed to allocate session\n");
  31. return NULL;
  32. }
  33. memset(se, 0, sizeof(*se));
  34. se->data = data;
  35. se->receive_buf = receive_buf;
  36. se->process_buf = process_buf;
  37. se->destroy = destroy;
  38. return se;
  39. }
  40. void fuse_session_add_chan(struct fuse_session *se, struct fuse_chan *ch)
  41. {
  42. assert(se->ch == NULL);
  43. assert(ch->se == NULL);
  44. se->ch = ch;
  45. ch->se = se;
  46. }
  47. void fuse_session_remove_chan(struct fuse_chan *ch)
  48. {
  49. struct fuse_session *se = ch->se;
  50. if (se) {
  51. assert(se->ch == ch);
  52. se->ch = NULL;
  53. ch->se = NULL;
  54. }
  55. }
  56. void
  57. fuse_session_destroy(struct fuse_session *se)
  58. {
  59. se->destroy(se->data);
  60. if(se->ch != NULL)
  61. fuse_chan_destroy(se->ch);
  62. free(se);
  63. }
  64. void fuse_session_reset(struct fuse_session *se)
  65. {
  66. se->exited = 0;
  67. }
  68. int
  69. fuse_session_exited(struct fuse_session *se)
  70. {
  71. return se->exited;
  72. }
  73. void
  74. fuse_session_exit(struct fuse_session *se_)
  75. {
  76. se_->exited = 1;
  77. }
  78. void *fuse_session_data(struct fuse_session *se)
  79. {
  80. return se->data;
  81. }
  82. struct fuse_chan *
  83. fuse_chan_new(int fd,
  84. size_t bufsize)
  85. {
  86. struct fuse_chan *ch;
  87. ch = (struct fuse_chan*)malloc(sizeof(*ch));
  88. if(ch == NULL)
  89. {
  90. fprintf(stderr, "fuse: failed to allocate channel\n");
  91. return NULL;
  92. }
  93. memset(ch, 0, sizeof(*ch));
  94. ch->fd = fd;
  95. ch->bufsize = bufsize;
  96. return ch;
  97. }
  98. int fuse_chan_fd(struct fuse_chan *ch)
  99. {
  100. return ch->fd;
  101. }
  102. int fuse_chan_clearfd(struct fuse_chan *ch)
  103. {
  104. int fd = ch->fd;
  105. ch->fd = -1;
  106. return fd;
  107. }
  108. size_t fuse_chan_bufsize(struct fuse_chan *ch)
  109. {
  110. return ch->bufsize;
  111. }
  112. struct fuse_session *fuse_chan_session(struct fuse_chan *ch)
  113. {
  114. return ch->se;
  115. }
  116. int
  117. fuse_chan_recv(struct fuse_chan *ch,
  118. char *buf,
  119. size_t size)
  120. {
  121. int err;
  122. ssize_t res;
  123. struct fuse_session *se = fuse_chan_session(ch);
  124. assert(se != NULL);
  125. restart:
  126. res = read(fuse_chan_fd(ch), buf, size);
  127. err = errno;
  128. if(fuse_session_exited(se))
  129. return 0;
  130. if(res == -1)
  131. {
  132. /* ENOENT means the operation was interrupted, it's safe
  133. to restart */
  134. if (err == ENOENT)
  135. goto restart;
  136. if(err == ENODEV)
  137. {
  138. se->exited = 1;
  139. return 0;
  140. }
  141. /* Errors occurring during normal operation: EINTR (read
  142. interrupted), EAGAIN (nonblocking I/O), ENODEV (filesystem
  143. umounted) */
  144. if(err != EINTR && err != EAGAIN)
  145. perror("fuse: reading device");
  146. return -err;
  147. }
  148. if((size_t) res < sizeof(struct fuse_in_header))
  149. {
  150. fprintf(stderr, "short read on fuse device\n");
  151. return -EIO;
  152. }
  153. return res;
  154. }
  155. int
  156. fuse_chan_send(struct fuse_chan *ch,
  157. const struct iovec iov[],
  158. size_t count)
  159. {
  160. if(!iov)
  161. return 0;
  162. int err;
  163. ssize_t res;
  164. res = writev(fuse_chan_fd(ch), iov, count);
  165. err = errno;
  166. if(res == -1)
  167. {
  168. struct fuse_session *se = fuse_chan_session(ch);
  169. assert(se != NULL);
  170. /* ENOENT means the operation was interrupted */
  171. if(!fuse_session_exited(se) && err != ENOENT)
  172. perror("fuse: writing device");
  173. return -err;
  174. }
  175. return 0;
  176. }
  177. void
  178. fuse_chan_destroy(struct fuse_chan *ch)
  179. {
  180. int fd;
  181. fuse_session_remove_chan(ch);
  182. fd = fuse_chan_fd(ch);
  183. if(fd != -1)
  184. close(fd);
  185. free(ch);
  186. }