plumber.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 <regexp.h>
  12. #include <thread.h>
  13. #include <plumb.h>
  14. #include <auth.h>
  15. #include <fcall.h>
  16. #include "plumber.h"
  17. char *plumbfile;
  18. char *user;
  19. char *home;
  20. char *progname;
  21. Ruleset **rules;
  22. int printerrors=1;
  23. jmp_buf parsejmp;
  24. char *lasterror;
  25. int mainstacksize = 20*1024;
  26. void
  27. makeports(Ruleset *rules[])
  28. {
  29. int i;
  30. for(i=0; rules[i]; i++)
  31. addport(rules[i]->port);
  32. }
  33. void
  34. mainproc(void *v)
  35. {
  36. Channel *c;
  37. c = v;
  38. printerrors = 0;
  39. makeports(rules);
  40. startfsys();
  41. sendp(c, nil);
  42. }
  43. void
  44. threadmain(int argc, char *argv[])
  45. {
  46. char buf[512];
  47. int fd;
  48. Channel *c;
  49. progname = "plumber";
  50. ARGBEGIN{
  51. case 'p':
  52. plumbfile = ARGF();
  53. break;
  54. }ARGEND
  55. user = getenv("user");
  56. home = getenv("home");
  57. if(user==nil || home==nil)
  58. error("can't initialize $user or $home: %r");
  59. if(plumbfile == nil){
  60. sprint(buf, "%s/lib/plumbing", home);
  61. plumbfile = estrdup(buf);
  62. }
  63. fd = open(plumbfile, OREAD);
  64. if(fd < 0)
  65. error("can't open rules file %s: %r", plumbfile);
  66. if(setjmp(parsejmp))
  67. error("parse error");
  68. rules = readrules(plumbfile, fd);
  69. close(fd);
  70. /*
  71. * Start all processes and threads from other proc
  72. * so we (main pid) can return to user.
  73. */
  74. c = chancreate(sizeof(void*), 0);
  75. proccreate(mainproc, c, 8192);
  76. recvp(c);
  77. chanfree(c);
  78. threadexits(nil);
  79. }
  80. void
  81. error(char *fmt, ...)
  82. {
  83. char buf[512];
  84. va_list args;
  85. va_start(args, fmt);
  86. vseprint(buf, buf+sizeof buf, fmt, args);
  87. va_end(args);
  88. fprint(2, "%s: %s\n", progname, buf);
  89. threadexitsall("error");
  90. }
  91. void
  92. parseerror(char *fmt, ...)
  93. {
  94. char buf[512];
  95. va_list args;
  96. va_start(args, fmt);
  97. vseprint(buf, buf+sizeof buf, fmt, args);
  98. va_end(args);
  99. if(printerrors){
  100. printinputstack();
  101. fprint(2, "%s\n", buf);
  102. }
  103. do; while(popinput());
  104. lasterror = estrdup(buf);
  105. longjmp(parsejmp, 1);
  106. }
  107. void*
  108. emalloc(int32_t n)
  109. {
  110. void *p;
  111. p = malloc(n);
  112. if(p == nil)
  113. error("malloc failed: %r");
  114. memset(p, 0, n);
  115. return p;
  116. }
  117. void*
  118. erealloc(void *p, int32_t n)
  119. {
  120. p = realloc(p, n);
  121. if(p == nil)
  122. error("realloc failed: %r");
  123. return p;
  124. }
  125. char*
  126. estrdup(char *s)
  127. {
  128. char *t;
  129. t = strdup(s);
  130. if(t == nil)
  131. error("estrdup failed: %r");
  132. setmalloctag(t, getcallerpc());
  133. return t;
  134. }