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.

243 lines
4.3 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. int
  83. fuse_session_receive(struct fuse_session *se_,
  84. struct fuse_buf *buf_)
  85. {
  86. return se_->receive_buf(se_,buf_,se_->ch);
  87. }
  88. void
  89. fuse_session_process(struct fuse_session *se_,
  90. const struct fuse_buf *buf_)
  91. {
  92. se_->process_buf(se_->data,buf_,se_->ch);
  93. }
  94. struct fuse_chan *
  95. fuse_chan_new(int fd,
  96. size_t bufsize)
  97. {
  98. struct fuse_chan *ch;
  99. ch = (struct fuse_chan*)malloc(sizeof(*ch));
  100. if(ch == NULL)
  101. {
  102. fprintf(stderr, "fuse: failed to allocate channel\n");
  103. return NULL;
  104. }
  105. memset(ch, 0, sizeof(*ch));
  106. ch->fd = fd;
  107. ch->bufsize = bufsize;
  108. return ch;
  109. }
  110. int fuse_chan_fd(struct fuse_chan *ch)
  111. {
  112. return ch->fd;
  113. }
  114. int fuse_chan_clearfd(struct fuse_chan *ch)
  115. {
  116. int fd = ch->fd;
  117. ch->fd = -1;
  118. return fd;
  119. }
  120. size_t fuse_chan_bufsize(struct fuse_chan *ch)
  121. {
  122. return ch->bufsize;
  123. }
  124. struct fuse_session *fuse_chan_session(struct fuse_chan *ch)
  125. {
  126. return ch->se;
  127. }
  128. int
  129. fuse_chan_recv(struct fuse_chan *ch,
  130. char *buf,
  131. size_t size)
  132. {
  133. int err;
  134. ssize_t res;
  135. struct fuse_session *se = fuse_chan_session(ch);
  136. assert(se != NULL);
  137. restart:
  138. res = read(fuse_chan_fd(ch), buf, size);
  139. err = errno;
  140. if(fuse_session_exited(se))
  141. return 0;
  142. if(res == -1)
  143. {
  144. /* ENOENT means the operation was interrupted, it's safe
  145. to restart */
  146. if (err == ENOENT)
  147. goto restart;
  148. if(err == ENODEV)
  149. {
  150. se->exited = 1;
  151. return 0;
  152. }
  153. /* Errors occurring during normal operation: EINTR (read
  154. interrupted), EAGAIN (nonblocking I/O), ENODEV (filesystem
  155. umounted) */
  156. if(err != EINTR && err != EAGAIN)
  157. perror("fuse: reading device");
  158. return -err;
  159. }
  160. if((size_t) res < sizeof(struct fuse_in_header))
  161. {
  162. fprintf(stderr, "short read on fuse device\n");
  163. return -EIO;
  164. }
  165. return res;
  166. }
  167. int
  168. fuse_chan_send(struct fuse_chan *ch,
  169. const struct iovec iov[],
  170. size_t count)
  171. {
  172. if(!iov)
  173. return 0;
  174. int err;
  175. ssize_t res;
  176. res = writev(fuse_chan_fd(ch), iov, count);
  177. err = errno;
  178. if(res == -1)
  179. {
  180. struct fuse_session *se = fuse_chan_session(ch);
  181. assert(se != NULL);
  182. /* ENOENT means the operation was interrupted */
  183. if(!fuse_session_exited(se) && err != ENOENT)
  184. perror("fuse: writing device");
  185. return -err;
  186. }
  187. return 0;
  188. }
  189. void
  190. fuse_chan_destroy(struct fuse_chan *ch)
  191. {
  192. int fd;
  193. fuse_session_remove_chan(ch);
  194. fd = fuse_chan_fd(ch);
  195. if(fd != -1)
  196. close(fd);
  197. free(ch);
  198. }