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.

125 lines
2.7 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_lowlevel.h"
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <pthread.h>
  14. #include <assert.h>
  15. struct procdata
  16. {
  17. struct fuse *f;
  18. struct fuse_chan *prevch;
  19. struct fuse_session *prevse;
  20. fuse_processor_t proc;
  21. void *data;
  22. };
  23. static void mt_session_proc(void *data, const char *buf, size_t len,
  24. struct fuse_chan *ch)
  25. {
  26. struct procdata *pd = (struct procdata *) data;
  27. struct fuse_cmd *cmd = *(struct fuse_cmd **) buf;
  28. (void) len;
  29. (void) ch;
  30. pd->proc(pd->f, cmd, pd->data);
  31. }
  32. static void mt_session_exit(void *data, int val)
  33. {
  34. struct procdata *pd = (struct procdata *) data;
  35. if (val)
  36. fuse_session_exit(pd->prevse);
  37. else
  38. fuse_session_reset(pd->prevse);
  39. }
  40. static int mt_session_exited(void *data)
  41. {
  42. struct procdata *pd = (struct procdata *) data;
  43. return fuse_session_exited(pd->prevse);
  44. }
  45. static int mt_chan_receive(struct fuse_chan **chp, char *buf, size_t size)
  46. {
  47. struct fuse_cmd *cmd;
  48. struct procdata *pd = (struct procdata *) fuse_chan_data(*chp);
  49. assert(size >= sizeof(cmd));
  50. cmd = fuse_read_cmd(pd->f);
  51. if (cmd == NULL)
  52. return 0;
  53. *(struct fuse_cmd **)buf = cmd;
  54. return sizeof(cmd);
  55. }
  56. int fuse_loop_mt_proc(struct fuse *f, fuse_processor_t proc, void *data)
  57. {
  58. int res;
  59. struct procdata pd;
  60. struct fuse_session *prevse = fuse_get_session(f);
  61. struct fuse_session *se;
  62. struct fuse_chan *prevch = fuse_session_next_chan(prevse, NULL);
  63. struct fuse_chan *ch;
  64. struct fuse_session_ops sop = {
  65. .exit = mt_session_exit,
  66. .exited = mt_session_exited,
  67. .process = mt_session_proc,
  68. };
  69. struct fuse_chan_ops cop = {
  70. .receive = mt_chan_receive,
  71. };
  72. pd.f = f;
  73. pd.prevch = prevch;
  74. pd.prevse = prevse;
  75. pd.proc = proc;
  76. pd.data = data;
  77. se = fuse_session_new(&sop, &pd);
  78. if (se == NULL)
  79. return -1;
  80. ch = fuse_chan_new(&cop, fuse_chan_fd(prevch),
  81. sizeof(struct fuse_cmd *), &pd);
  82. if (ch == NULL) {
  83. fuse_session_destroy(se);
  84. return -1;
  85. }
  86. fuse_session_add_chan(se, ch);
  87. res = fuse_session_loop_mt(se,
  88. fuse_config_num_threads(f));
  89. fuse_session_destroy(se);
  90. return res;
  91. }
  92. int fuse_loop_mt(struct fuse *f)
  93. {
  94. if (f == NULL)
  95. return -1;
  96. int res = fuse_start_maintenance_thread(f);
  97. if (res)
  98. return -1;
  99. res = fuse_session_loop_mt(fuse_get_session(f),
  100. fuse_config_num_threads(f));
  101. fuse_stop_maintenance_thread(f);
  102. return res;
  103. }