]> git.lizzy.rs Git - plan9front.git/blob - sys/src/cmd/plumb/plumber.c
ip/ipconfig: use ewrite() to enable routing command for sendra
[plan9front.git] / sys / src / cmd / plumb / plumber.c
1 #include <u.h>
2 #include <libc.h>
3 #include <regexp.h>
4 #include <thread.h>
5 #include <plumb.h>
6 #include <auth.h>
7 #include <fcall.h>
8 #include "plumber.h"
9
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;
18
19 void
20 makeports(Ruleset *rules[])
21 {
22         int i;
23
24         for(i=0; rules[i]; i++)
25                 addport(rules[i]->port);
26 }
27
28 void
29 mainproc(void *v)
30 {
31         Channel *c;
32
33         c = v;
34         printerrors = 0;
35         makeports(rules);
36         startfsys();
37         sendp(c, nil);
38 }
39
40 void
41 threadmain(int argc, char *argv[])
42 {
43         char buf[512];
44         int fd;
45         Channel *c;
46
47         progname = "plumber";
48
49         ARGBEGIN{
50         case 'p':
51                 plumbfile = ARGF();
52                 break;
53         }ARGEND
54
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
64         fd = open(plumbfile, OREAD);
65         if(fd < 0)
66                 error("can't open rules file %s: %r", plumbfile);
67         if(setjmp(parsejmp))
68                 error("parse error");
69
70         rules = readrules(plumbfile, fd);
71
72         /*
73          * Start all processes and threads from other proc
74          * so we (main pid) can return to user.
75          */
76         c = chancreate(sizeof(void*), 0);
77         proccreate(mainproc, c, 8192);
78         recvp(c);
79         chanfree(c);
80         threadexits(nil);
81 }
82
83 void
84 error(char *fmt, ...)
85 {
86         char buf[512];
87         va_list args;
88
89         va_start(args, fmt);
90         vseprint(buf, buf+sizeof buf, fmt, args);
91         va_end(args);
92
93         fprint(2, "%s: %s\n", progname, buf);
94         threadexitsall("error");
95 }
96
97 void
98 parseerror(char *fmt, ...)
99 {
100         char buf[512];
101         va_list args;
102
103         va_start(args, fmt);
104         vseprint(buf, buf+sizeof buf, fmt, args);
105         va_end(args);
106
107         if(printerrors){
108                 printinputstack();
109                 fprint(2, "%s\n", buf);
110         }
111         do; while(popinput());
112         lasterror = estrdup(buf);
113         longjmp(parsejmp, 1);
114 }
115
116 void*
117 emalloc(long n)
118 {
119         void *p;
120
121         p = malloc(n);
122         if(p == nil)
123                 error("malloc failed: %r");
124         setmalloctag(p, getcallerpc(&n));
125         memset(p, 0, n);
126         return p;
127 }
128
129 void*
130 erealloc(void *p, long n)
131 {
132         p = realloc(p, n);
133         if(p == nil)
134                 error("realloc failed: %r");
135         setrealloctag(p, getcallerpc(&p));
136         return p;
137 }
138
139 char*
140 estrdup(char *s)
141 {
142         char *t;
143
144         t = strdup(s);
145         if(t == nil)
146                 error("estrdup failed: %r");
147         setmalloctag(t, getcallerpc(&s));
148         return t;
149 }