exporter.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include "compat.h"
  12. typedef struct Exporter Exporter;
  13. struct Exporter
  14. {
  15. int fd;
  16. Chan **roots;
  17. int nroots;
  18. };
  19. int
  20. mounter(char *mntpt, int how, int fd, int n)
  21. {
  22. char buf[32];
  23. int i, ok, mfd;
  24. ok = 1;
  25. for(i = 0; i < n; i++){
  26. snprint(buf, sizeof buf, "%d", i);
  27. mfd = dup(fd, -1);
  28. if(mount(mfd, -1, mntpt, how, buf, 'M') == -1){
  29. close(mfd);
  30. fprint(2, "can't mount on %s: %r\n", mntpt);
  31. ok = 0;
  32. break;
  33. }
  34. close(mfd);
  35. if(how == MREPL)
  36. how = MAFTER;
  37. }
  38. close(fd);
  39. return ok;
  40. }
  41. static void
  42. extramp(void *v)
  43. {
  44. Exporter *ex;
  45. rfork(RFNAMEG);
  46. ex = v;
  47. sysexport(ex->fd, ex->roots, ex->nroots);
  48. shutdown();
  49. exits(nil);
  50. }
  51. int
  52. exporter(Dev **dt, int *fd, int *sfd)
  53. {
  54. Chan **roots;
  55. Exporter ex;
  56. int p[2], i, n, ed;
  57. for(n = 0; dt[n] != nil; n++)
  58. ;
  59. if(!n){
  60. werrstr("no devices specified");
  61. return 0;
  62. }
  63. ed = errdepth(-1);
  64. if(waserror()){
  65. werrstr(up->error);
  66. return 0;
  67. }
  68. roots = smalloc(n * sizeof *roots);
  69. for(i = 0; i < n; i++){
  70. (*dt[i]->reset)();
  71. (*dt[i]->init)();
  72. roots[i] = (*dt[i]->attach)("");
  73. }
  74. poperror();
  75. errdepth(ed);
  76. if(pipe(p) < 0){
  77. werrstr("can't make pipe: %r");
  78. return 0;
  79. }
  80. *sfd = p[0];
  81. *fd = p[1];
  82. ex.fd = *sfd;
  83. ex.roots = roots;
  84. ex.nroots = n;
  85. kproc("exporter", extramp, &ex);
  86. return n;
  87. }