]> git.lizzy.rs Git - plan9front.git/blob - sys/src/cmd/plumb/plumber.c
rsa: rename getkey() to getrsakey(), document rsa2csr in rsa(8)
[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         close(fd);
72
73         /*
74          * Start all processes and threads from other proc
75          * so we (main pid) can return to user.
76          */
77         c = chancreate(sizeof(void*), 0);
78         proccreate(mainproc, c, 8192);
79         recvp(c);
80         chanfree(c);
81         threadexits(nil);
82 }
83
84 void
85 error(char *fmt, ...)
86 {
87         char buf[512];
88         va_list args;
89
90         va_start(args, fmt);
91         vseprint(buf, buf+sizeof buf, fmt, args);
92         va_end(args);
93
94         fprint(2, "%s: %s\n", progname, buf);
95         threadexitsall("error");
96 }
97
98 void
99 parseerror(char *fmt, ...)
100 {
101         char buf[512];
102         va_list args;
103
104         va_start(args, fmt);
105         vseprint(buf, buf+sizeof buf, fmt, args);
106         va_end(args);
107
108         if(printerrors){
109                 printinputstack();
110                 fprint(2, "%s\n", buf);
111         }
112         do; while(popinput());
113         lasterror = estrdup(buf);
114         longjmp(parsejmp, 1);
115 }
116
117 void*
118 emalloc(long n)
119 {
120         void *p;
121
122         p = malloc(n);
123         if(p == nil)
124                 error("malloc failed: %r");
125         setmalloctag(p, getcallerpc(&n));
126         memset(p, 0, n);
127         return p;
128 }
129
130 void*
131 erealloc(void *p, long n)
132 {
133         p = realloc(p, n);
134         if(p == nil)
135                 error("realloc failed: %r");
136         setrealloctag(p, getcallerpc(&p));
137         return p;
138 }
139
140 char*
141 estrdup(char *s)
142 {
143         char *t;
144
145         t = strdup(s);
146         if(t == nil)
147                 error("estrdup failed: %r");
148         setmalloctag(t, getcallerpc(&s));
149         return t;
150 }