Blob
1 #include <u.h>2 #include <libc.h>3 #include <regexp.h>4 #include <thread.h>5 #include <plumb.h>6 #include <fcall.h>7 #include "plumber.h"9 int debug;10 char *plumbfile;11 char *user;12 char *home;13 char *progname;14 Ruleset **rules;15 int printerrors=1;16 jmp_buf parsejmp;17 char *lasterror;19 void20 makeports(Ruleset *rules[])21 {22 int i;24 for(i=0; rules[i]; i++)25 addport(rules[i]->port);26 }28 void29 threadmain(int argc, char *argv[])30 {31 char buf[512];32 int fd, dofork;34 progname = "plumber";35 dofork = 1;37 ARGBEGIN{38 case 'd':39 debug = 1;40 break;41 case 'f':42 dofork = 0;43 break;44 case 'p':45 plumbfile = ARGF();46 break;47 }ARGEND49 user = getuser();50 home = getenv("home");51 if(home == nil)52 home = getenv("HOME");53 if(user==nil || home==nil)54 error("can't initialize $user or $home: %r");55 if(plumbfile == nil){56 sprint(buf, "%s/lib/plumbing", home);57 if(access(buf, 0) >= 0)58 plumbfile = estrdup(buf);59 else60 plumbfile = unsharp("#9/plumb/initial.plumbing");61 }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");69 rules = readrules(plumbfile, fd);70 close(fd);72 /*73 * Start all processes and threads from other proc74 * so we (main pid) can return to user.75 */76 if(dofork)77 switch(fork()){78 case -1:79 sysfatal("fork: %r");80 case 0:81 break;82 default:83 _exit(0);84 }86 printerrors = 0;87 makeports(rules);88 startfsys();89 }91 void92 error(char *fmt, ...)93 {94 char buf[512];95 va_list args;97 va_start(args, fmt);98 vseprint(buf, buf+sizeof buf, fmt, args);99 va_end(args);101 fprint(2, "%s: %s\n", progname, buf);102 threadexitsall("error");103 }105 void106 parseerror(char *fmt, ...)107 {108 char buf[512];109 va_list args;111 va_start(args, fmt);112 vseprint(buf, buf+sizeof buf, fmt, args);113 va_end(args);115 if(printerrors){116 printinputstack();117 fprint(2, "%s\n", buf);118 }119 do; while(popinput());120 lasterror = estrdup(buf);121 longjmp(parsejmp, 1);122 }124 void*125 emalloc(long n)126 {127 void *p;129 p = malloc(n);130 if(p == nil)131 error("malloc failed: %r");132 memset(p, 0, n);133 return p;134 }136 void*137 erealloc(void *p, long n)138 {139 p = realloc(p, n);140 if(p == nil)141 error("realloc failed: %r");142 return p;143 }145 char*146 estrdup(char *s)147 {148 char *t;150 t = strdup(s);151 if(t == nil)152 error("estrdup failed: %r");153 return t;154 }