Commit | Line | Data |
---|---|---|
410c8acf | 1 | /* -*-c-*- |
2 | * | |
410c8acf | 3 | * Client for TrIPE |
4 | * | |
5 | * (c) 2001 Straylight/Edgeware | |
6 | */ | |
7 | ||
e04c2d50 | 8 | /*----- Licensing notice --------------------------------------------------* |
410c8acf | 9 | * |
10 | * This file is part of Trivial IP Encryption (TrIPE). | |
11 | * | |
11ad66c2 MW |
12 | * TrIPE is free software: you can redistribute it and/or modify it under |
13 | * the terms of the GNU General Public License as published by the Free | |
14 | * Software Foundation; either version 3 of the License, or (at your | |
15 | * option) any later version. | |
e04c2d50 | 16 | * |
11ad66c2 MW |
17 | * TrIPE is distributed in the hope that it will be useful, but WITHOUT |
18 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
19 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
20 | * for more details. | |
e04c2d50 | 21 | * |
410c8acf | 22 | * You should have received a copy of the GNU General Public License |
11ad66c2 | 23 | * along with TrIPE. If not, see <https://www.gnu.org/licenses/>. |
410c8acf | 24 | */ |
25 | ||
410c8acf | 26 | /*----- Header files ------------------------------------------------------*/ |
27 | ||
73189848 | 28 | #include "config.h" |
29 | ||
410c8acf | 30 | #include <ctype.h> |
31 | #include <errno.h> | |
32 | #include <signal.h> | |
33 | #include <stdio.h> | |
34 | #include <stdlib.h> | |
35 | #include <string.h> | |
36 | #include <time.h> | |
37 | ||
38 | #include <sys/types.h> | |
39 | #include <sys/time.h> | |
40 | #include <unistd.h> | |
41 | #include <fcntl.h> | |
42 | #include <sys/wait.h> | |
43 | #include <syslog.h> | |
44 | ||
45 | #include <sys/socket.h> | |
46 | #include <sys/un.h> | |
47 | #include <arpa/inet.h> | |
48 | #include <netdb.h> | |
49 | ||
50 | #include <mLib/alloc.h> | |
19dd2531 | 51 | #include <mLib/daemonize.h> |
410c8acf | 52 | #include <mLib/darray.h> |
53 | #include <mLib/dstr.h> | |
ddb384f1 | 54 | #include <mLib/macros.h> |
b9537f3b | 55 | #include <mLib/mdup.h> |
410c8acf | 56 | #include <mLib/mdwopt.h> |
57 | #include <mLib/quis.h> | |
58 | #include <mLib/report.h> | |
59 | #include <mLib/sel.h> | |
60 | #include <mLib/selbuf.h> | |
5f5150b1 | 61 | #include <mLib/sig.h> |
410c8acf | 62 | #include <mLib/str.h> |
19dd2531 | 63 | #include <mLib/versioncmp.h> |
410c8acf | 64 | |
65 | #include "util.h" | |
66 | ||
67 | #undef sun | |
68 | ||
69 | /*----- Data structures ---------------------------------------------------*/ | |
70 | ||
71 | #ifndef STRING_V | |
72 | # define STRING_V | |
73 | DA_DECL(string_v, char *); | |
74 | #endif | |
75 | ||
76 | /*----- Static variables --------------------------------------------------*/ | |
77 | ||
8efdf64b | 78 | static sel_state sel; |
06b2a088 | 79 | static const char *pidfile = 0; |
80 | static const char *logname = 0; | |
410c8acf | 81 | static FILE *logfp = 0; |
82 | static unsigned f = 0; | |
5729b4bf | 83 | static const char *watch = 0; |
410c8acf | 84 | static int fd; |
de014da6 | 85 | static const char *bgtag = 0; |
410c8acf | 86 | |
87 | #define f_bogus 1u | |
88 | #define f_spawn 2u | |
89 | #define f_daemon 4u | |
90 | #define f_spawnopts 8u | |
91 | #define f_syslog 16u | |
92 | #define f_command 32u | |
93 | #define f_noinput 64u | |
94 | #define f_warn 128u | |
95 | #define f_uclose 256u | |
1a372224 | 96 | #define f_losing 512u |
ae8928d2 | 97 | #define f_nostamp 1024u |
410c8acf | 98 | |
99 | /*----- Main code ---------------------------------------------------------*/ | |
100 | ||
101 | static void reap(int sig) | |
102 | { | |
410c8acf | 103 | int e = errno; |
06b2a088 | 104 | while (waitpid(-1, 0, WNOHANG) > 0) |
410c8acf | 105 | ; |
106 | errno = e; | |
107 | } | |
108 | ||
109 | static void writelog(const char *cat, const char *msg) | |
110 | { | |
111 | char buf[256]; | |
112 | time_t t = time(0); | |
113 | struct tm *tm = localtime(&t); | |
ae8928d2 MW |
114 | if (f & f_nostamp) buf[0] = 0; |
115 | else strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S ", tm); | |
116 | fprintf(logfp, "%s%s: %s\n", buf, cat, msg); | |
410c8acf | 117 | } |
118 | ||
de014da6 | 119 | static void checkbg(char **p) |
120 | { | |
121 | char *q = str_getword(p); | |
122 | if (!q) | |
123 | die(EXIT_FAILURE, "missing background tag"); | |
124 | if (!bgtag || strcmp(bgtag, q) != 0) | |
125 | die(EXIT_FAILURE, "unexpected background tag `%s'", q); | |
126 | } | |
127 | ||
ddb384f1 | 128 | static void PRINTF_LIKE(2, 3) dolog(int prio, const char *msg, ...) |
2b3ef395 MW |
129 | { |
130 | va_list ap; | |
131 | dstr d = DSTR_INIT; | |
132 | const char *cat; | |
133 | ||
134 | va_start(ap, msg); | |
135 | dstr_vputf(&d, msg, &ap); | |
136 | va_end(ap); | |
137 | if (f & f_syslog) syslog(prio, "%s", d.buf); | |
138 | if (logfp) { | |
139 | switch (prio) { | |
155a970a | 140 | case LOG_WARNING: cat = "warning"; break; |
2b3ef395 MW |
141 | case LOG_DEBUG: cat = "debug"; break; |
142 | case LOG_ERR: cat = "error"; break; | |
143 | default: cat = "message"; break; | |
144 | } | |
145 | writelog(cat, d.buf); | |
146 | } | |
155a970a | 147 | if (prio == LOG_WARNING && (f & f_warn)) |
2b3ef395 MW |
148 | fprintf(stderr, "Warning: %s\n", d.buf); |
149 | dstr_destroy(&d); | |
150 | } | |
151 | ||
de014da6 | 152 | static void checkfg(void) |
6047fbac | 153 | { if (bgtag) die(EXIT_FAILURE, "unexpected foreground response"); } |
de014da6 | 154 | |
6be88c14 | 155 | static void cline(char *p, size_t len, void *b) |
410c8acf | 156 | { |
157 | char *q; | |
158 | if (!p) { | |
159 | if (f & f_command) | |
160 | die(EXIT_FAILURE, "server dropped the connection"); | |
1a372224 | 161 | f &= ~f_losing; |
410c8acf | 162 | exit(0); |
163 | } | |
164 | q = str_getword(&p); | |
165 | if (!q) | |
166 | return; | |
2b3ef395 | 167 | if (strcmp(q, "WARN") == 0) |
f7a19d39 | 168 | dolog(LOG_WARNING, "%s", p); |
2b3ef395 | 169 | else if (strcmp(q, "TRACE") == 0) |
f7a19d39 | 170 | dolog(LOG_DEBUG, "%s", p); |
2b3ef395 MW |
171 | else if (!(f & f_command)) |
172 | dolog(LOG_ERR, "unexpected output `%s %s'", q, p); | |
173 | else if (strcmp(q, "FAIL") == 0) { | |
de014da6 | 174 | checkfg(); |
410c8acf | 175 | die(EXIT_FAILURE, "%s", p); |
de014da6 | 176 | } else if (strcmp(q, "INFO") == 0) { |
177 | checkfg(); | |
410c8acf | 178 | puts(p); |
5136c31e | 179 | fflush(stdout); |
de014da6 | 180 | } else if (strcmp(q, "OK") == 0) { |
181 | checkfg(); | |
182 | exit(0); | |
183 | } else if (strcmp(q, "BGDETACH") == 0) { | |
184 | if (bgtag) | |
185 | die(EXIT_FAILURE, "repeat detach"); | |
186 | bgtag = xstrdup(p); | |
187 | } else if (strcmp(q, "BGOK") == 0) { | |
188 | checkbg(&p); | |
410c8acf | 189 | exit(0); |
de014da6 | 190 | } else if (strcmp(q, "BGINFO") == 0) { |
191 | checkbg(&p); | |
192 | puts(p); | |
5136c31e | 193 | fflush(stdout); |
de014da6 | 194 | } else if (strcmp(q, "BGFAIL") == 0) { |
195 | checkbg(&p); | |
196 | die(EXIT_FAILURE, "%s", p); | |
197 | } else | |
e04c2d50 | 198 | die(EXIT_FAILURE, "unexpected output `%s %s'", q, p); |
410c8acf | 199 | } |
200 | ||
6be88c14 | 201 | static void sline(char *p, size_t len, void *b) |
410c8acf | 202 | { |
203 | if (!p) { | |
204 | if (!(f & f_uclose)) | |
205 | moan("server closed the connection"); | |
206 | exit(0); | |
207 | } | |
208 | puts(p); | |
5136c31e | 209 | fflush(stdout); |
410c8acf | 210 | } |
211 | ||
6be88c14 | 212 | static void uline(char *p, size_t len, void *b) |
410c8acf | 213 | { |
410c8acf | 214 | if (!p) { |
215 | selbuf_destroy(b); | |
216 | shutdown(fd, 1); | |
217 | f |= f_uclose; | |
218 | } else { | |
6be88c14 | 219 | p[len] = '\n'; |
3cdc3f3a | 220 | errno = EIO; |
221 | if (write(fd, p, len + 1) != len + 1) | |
222 | moan("write failed: %s", strerror(errno)); | |
223 | } | |
224 | } | |
225 | ||
155a970a MW |
226 | static void eline(char *p, size_t len, void *b) |
227 | { | |
228 | if (p) | |
229 | dolog(LOG_WARNING, "(stderr): %s", p); | |
230 | else { | |
231 | selbuf_destroy(b); | |
232 | close(fd); | |
233 | } | |
234 | } | |
235 | ||
3cdc3f3a | 236 | static void setup(const char *cmd) |
237 | { | |
238 | dstr d = DSTR_INIT; | |
239 | char ch; | |
240 | char *p, *q; | |
241 | int n; | |
242 | ||
243 | dstr_puts(&d, cmd); | |
244 | dstr_putc(&d, '\n'); | |
245 | errno = EIO; /* Relax: be vague */ | |
246 | if (write(fd, d.buf, d.len) != d.len) { | |
247 | die(EXIT_FAILURE, "error sending setup command `%s': %s", | |
248 | cmd, strerror(errno)); | |
249 | } | |
250 | dstr_reset(&d); | |
251 | for (;;) { | |
252 | n = read(fd, &ch, 1); | |
253 | if (!n) | |
254 | die(EXIT_FAILURE, "unexpected EOF during setup"); | |
255 | if (n < 0) { | |
256 | die(EXIT_FAILURE, "error receiving reply to `%s': %s", | |
257 | cmd, strerror(errno)); | |
258 | } | |
259 | if (d.len < 256) | |
260 | dstr_putc(&d, ch); | |
261 | if (ch == '\n') { | |
262 | p = d.buf; | |
263 | q = str_getword(&p); | |
264 | if (!q) | |
265 | ; | |
266 | else if (strcmp(q, "OK") == 0) | |
267 | return; | |
268 | else if (strcmp(q, "FAIL") == 0) | |
269 | die(EXIT_FAILURE, "setup command `%s' failed: %s", cmd, p); | |
270 | dstr_reset(&d); | |
271 | } | |
410c8acf | 272 | } |
273 | } | |
274 | ||
9dd01bc6 | 275 | static void logfile(const char *name) |
276 | { | |
fa4bef80 | 277 | FILE *fp; |
278 | ||
ae8928d2 MW |
279 | if (strcmp(name, "-") == 0) |
280 | logfp = stdout; | |
281 | else if (strcmp(name, "!") == 0) | |
282 | logfp = stderr; | |
283 | else if ((fp = fopen(name, "a")) != 0) { | |
fa4bef80 | 284 | if (logfp) |
285 | fclose(logfp); | |
286 | logfp = fp; | |
287 | setvbuf(logfp, 0, _IOLBF, BUFSIZ); | |
288 | } else { | |
289 | dstr d = DSTR_INIT; | |
290 | dstr_putf(&d, "error opening logfile `%s': %s", name, strerror(errno)); | |
291 | if (logfp) | |
292 | writelog("error", d.buf); | |
293 | else if (logname) | |
f7a19d39 | 294 | die(EXIT_FAILURE, "%s", d.buf); |
fa4bef80 | 295 | if (f & f_syslog) |
92e57abc | 296 | syslog(LOG_ERR, "%s", d.buf); |
fa4bef80 | 297 | dstr_destroy(&d); |
9dd01bc6 | 298 | } |
9dd01bc6 | 299 | } |
300 | ||
f98df549 | 301 | static void sighup(int sig, void *v) { logfile(logname); } |
5f5150b1 | 302 | |
6047fbac | 303 | static void cleanup(void) { if (pidfile) unlink(pidfile); } |
06b2a088 | 304 | |
305 | static void sigdie(int sig) | |
6047fbac | 306 | { cleanup(); signal(sig, SIG_DFL); raise(sig); } |
06b2a088 | 307 | |
ddb384f1 | 308 | static void PRINTF_LIKE(2, 3) putarg(string_v *av, const char *fmt, ...) |
f685e692 MW |
309 | { |
310 | va_list ap; | |
311 | dstr d = DSTR_INIT; | |
312 | ||
313 | va_start(ap, fmt); | |
314 | dstr_vputf(&d, fmt, &ap); | |
315 | dstr_putz(&d); | |
316 | va_end(ap); | |
317 | DA_UNSHIFT(av, xstrdup(d.buf)); | |
318 | dstr_destroy(&d); | |
319 | } | |
320 | ||
410c8acf | 321 | static void version(FILE *fp) |
f98df549 | 322 | { pquis(fp, "$, TrIPE version " VERSION "\n"); } |
410c8acf | 323 | |
324 | static void usage(FILE *fp) | |
325 | { | |
326 | pquis(fp, "\ | |
327 | Usage:\n\ | |
2d752320 | 328 | $ [-w] [-OPTIONS] [COMMAND [ARGS]...]\n\ |
329 | $ [-Dl] [-f FILE] [-OPTIONS]\n\ | |
410c8acf | 330 | Options:\n\ |
2d752320 | 331 | [-s] [-d DIRECTORY] [-a SOCKET] [-P PIDFILE]\n\ |
332 | [-p PROGRAM] [-S ARG,ARG,...]\n\ | |
410c8acf | 333 | "); |
334 | } | |
335 | ||
336 | static void help(FILE *fp) | |
337 | { | |
338 | version(fp); | |
339 | fputc('\n', fp); | |
340 | usage(fp); | |
341 | fputs("\ | |
342 | \n\ | |
343 | Options in full:\n\ | |
344 | \n\ | |
345 | -h, --help Show this help text.\n\ | |
346 | -v, --version Show version number.\n\ | |
347 | -u, --usage Show brief usage message.\n\ | |
348 | \n\ | |
349 | -D, --daemon Become a background task after connecting.\n\ | |
ef4a1ab7 | 350 | -d, --directory=DIR Select current directory [default " CONFIGDIR "].\n\ |
ab46a787 MW |
351 | -U, --setuid=USER Set uid to USER after initialization.\n\ |
352 | -G, --setgid=GROUP Set gid to GROUP after initialization.\n\ | |
3cdc3f3a | 353 | -a, --admin-socket=FILE Select socket to connect to\n\ |
e04c2d50 | 354 | [default " SOCKETDIR "/tripesock].\n\ |
06b2a088 | 355 | -P, --pidfile=FILE Write process-id to FILE.\n\ |
410c8acf | 356 | \n\ |
357 | -s, --spawn Start server rather than connecting.\n\ | |
358 | -p, --spawn-path=PATH Specify path to executable.\n\ | |
359 | -S, --spawn-args=ARGS Specify comma-separated arguments.\n\ | |
360 | \n\ | |
361 | -l, --syslog Log messages to system log.\n\ | |
362 | -f, --logfile=FILE Log messages to FILE.\n\ | |
ae8928d2 | 363 | -t, --no-timestamp When logging to a file, don't emit timestamps.\n\ |
410c8acf | 364 | -w, --warnings Show warnings when running commands.\n\ |
5729b4bf | 365 | -W, --watch=THINGS Watch for THINGS after connecting.\n\ |
410c8acf | 366 | ", fp); |
367 | } | |
368 | ||
369 | int main(int argc, char *argv[]) | |
370 | { | |
ef4a1ab7 | 371 | const char *dir = CONFIGDIR; |
372 | const char *sock = SOCKETDIR "/tripesock"; | |
410c8acf | 373 | const char *spawnpath = "tripe"; |
374 | string_v spawnopts = DA_INIT; | |
375 | char *p; | |
06b2a088 | 376 | FILE *pidfp = 0; |
8efdf64b MW |
377 | int i; |
378 | size_t sz; | |
ab46a787 MW |
379 | uid_t u = -1; |
380 | gid_t g = -1; | |
155a970a | 381 | int pfd[2], efd[2]; |
b9537f3b | 382 | mdup_fd md[3]; |
8efdf64b MW |
383 | pid_t kid; |
384 | struct sigaction sa; | |
385 | sigset_t newmask, oldmask; | |
386 | struct sockaddr_un sun; | |
155a970a | 387 | selbuf bu, bs, be; |
8efdf64b MW |
388 | dstr d = DSTR_INIT; |
389 | sig hup; | |
410c8acf | 390 | |
391 | ego(argv[0]); | |
392 | ||
393 | if ((p = getenv("TRIPEDIR")) != 0) | |
394 | dir = p; | |
797cf76b MW |
395 | if ((p = getenv("TRIPESOCK")) != 0) |
396 | sock = p; | |
410c8acf | 397 | |
398 | /* --- Parse the arguments --- */ | |
399 | ||
400 | for (;;) { | |
401 | static const struct option opts[] = { | |
402 | { "help", 0, 0, 'h' }, | |
403 | { "version", 0, 0, 'v' }, | |
404 | { "usage", 0, 0, 'u' }, | |
405 | { "daemon", 0, 0, 'D' }, | |
ab46a787 MW |
406 | { "uid", OPTF_ARGREQ, 0, 'U' }, |
407 | { "setuid", OPTF_ARGREQ, 0, 'U' }, | |
408 | { "gid", OPTF_ARGREQ, 0, 'G' }, | |
409 | { "setgid", OPTF_ARGREQ, 0, 'G' }, | |
410c8acf | 410 | { "directory", OPTF_ARGREQ, 0, 'd' }, |
411 | { "admin-socket", OPTF_ARGREQ, 0, 'a' }, | |
412 | { "spawn", 0, 0, 's' }, | |
413 | { "spawn-path", OPTF_ARGREQ, 0, 'p' }, | |
414 | { "spawn-args", OPTF_ARGREQ, 0, 'S' }, | |
415 | { "syslog", 0, 0, 'l' }, | |
416 | { "logfile", OPTF_ARGREQ, 0, 'f' }, | |
ae8928d2 | 417 | { "no-timestamp", 0, 0, 't' }, |
410c8acf | 418 | { "warnings", 0, 0, 'w' }, |
5729b4bf | 419 | { "watch", OPTF_ARGREQ, 0, 'W' }, |
06b2a088 | 420 | { "pidfile", OPTF_ARGREQ, 0, 'P' }, |
410c8acf | 421 | { 0, 0, 0, 0 } |
422 | }; | |
423 | ||
5729b4bf | 424 | i = mdwopt(argc, argv, "+hvuDU:G:d:a:sp:S:lwW:f:nP:t", opts, 0, 0, 0); |
410c8acf | 425 | if (i < 0) |
426 | break; | |
427 | switch (i) { | |
428 | case 'h': | |
429 | help(stdout); | |
430 | exit(0); | |
431 | case 'v': | |
432 | version(stdout); | |
433 | exit(0); | |
434 | case 'u': | |
435 | usage(stdout); | |
436 | exit(0); | |
437 | case 'D': | |
438 | f |= f_daemon | f_noinput; | |
439 | break; | |
ab46a787 MW |
440 | case 'U': |
441 | u = u_getuser(optarg, &g); | |
442 | break; | |
443 | case 'G': | |
444 | g = u_getgroup(optarg); | |
445 | break; | |
410c8acf | 446 | case 'd': |
447 | dir = optarg; | |
448 | break; | |
449 | case 'a': | |
450 | sock = optarg; | |
451 | break; | |
452 | case 's': | |
453 | f |= f_spawn; | |
454 | break; | |
455 | case 'p': | |
456 | f |= f_spawn; | |
457 | spawnpath = optarg; | |
458 | break; | |
459 | case 'S': | |
460 | f |= f_spawn | f_spawnopts; | |
461 | for (p = strtok(optarg, ","); p; p = strtok(0, ",")) | |
462 | DA_PUSH(&spawnopts, p); | |
463 | break; | |
464 | case 'l': | |
465 | f |= f_syslog | f_noinput; | |
466 | break; | |
467 | case 'w': | |
468 | f |= f_warn; | |
469 | break; | |
5729b4bf MW |
470 | case 'W': |
471 | watch = optarg; | |
472 | break; | |
410c8acf | 473 | case 'f': |
9dd01bc6 | 474 | logname = optarg; |
410c8acf | 475 | f |= f_noinput; |
476 | break; | |
ae8928d2 MW |
477 | case 't': |
478 | f |= f_nostamp; | |
479 | break; | |
06b2a088 | 480 | case 'P': |
481 | pidfile = optarg; | |
482 | break; | |
410c8acf | 483 | default: |
484 | f |= f_bogus; | |
485 | break; | |
486 | } | |
487 | } | |
488 | if ((f & f_bogus) || ((f & f_noinput) && optind < argc)) { | |
489 | usage(stderr); | |
490 | exit(EXIT_FAILURE); | |
491 | } | |
492 | ||
06b2a088 | 493 | /* --- Set various things up --- */ |
494 | ||
495 | if (chdir(dir)) { | |
496 | die(EXIT_FAILURE, "couldn't set `%s' as current directory: %s", | |
497 | dir, strerror(errno)); | |
498 | } | |
06b2a088 | 499 | if (!pidfile && (f & f_daemon) && ((f & f_syslog) || logname)) |
500 | pidfile = "tripectl.pid"; | |
501 | if (pidfile && (pidfp = fopen(pidfile, "w")) == 0) { | |
502 | die(EXIT_FAILURE, "couldn't open `%s' for writing: %s", | |
503 | pidfile, strerror(errno)); | |
504 | } | |
8efdf64b MW |
505 | sel_init(&sel); |
506 | sig_init(&sel); | |
06b2a088 | 507 | signal(SIGINT, sigdie); |
508 | signal(SIGQUIT, sigdie); | |
509 | signal(SIGTERM, sigdie); | |
510 | atexit(cleanup); | |
511 | ||
410c8acf | 512 | /* --- Connect to the server --- */ |
513 | ||
514 | if (f & f_spawn) { | |
410c8acf | 515 | sa.sa_handler = reap; |
516 | sigemptyset(&sa.sa_mask); | |
517 | sa.sa_flags = SA_NOCLDSTOP; | |
518 | #ifdef SA_RESTART | |
519 | sa.sa_flags |= SA_RESTART; | |
520 | #endif | |
521 | sigaction(SIGCHLD, &sa, 0); | |
522 | ||
410c8acf | 523 | DA_PUSH(&spawnopts, 0); |
ab46a787 MW |
524 | if (g != (gid_t)-1) putarg(&spawnopts, "-G%lu", (unsigned long)g); |
525 | if (u != (uid_t)-1) putarg(&spawnopts, "-U%lu", (unsigned long)u); | |
f685e692 MW |
526 | putarg(&spawnopts, "-a%s", sock); |
527 | putarg(&spawnopts, "-d."); | |
528 | putarg(&spawnopts, "-F"); | |
529 | putarg(&spawnopts, "%s", spawnpath); | |
155a970a | 530 | if (socketpair(PF_UNIX, SOCK_STREAM, 0, pfd) || pipe(efd)) |
410c8acf | 531 | die(EXIT_FAILURE, "error from socketpair: %s", strerror(errno)); |
532 | sigemptyset(&newmask); | |
533 | sigaddset(&newmask, SIGCHLD); | |
534 | sigprocmask(SIG_BLOCK, &newmask, &oldmask); | |
535 | if ((kid = fork()) < 0) | |
536 | die(EXIT_FAILURE, "fork failed: %s", strerror(errno)); | |
537 | if (!kid) { | |
b9537f3b | 538 | close(pfd[0]); close(efd[0]); |
0dba5cb9 | 539 | sigprocmask(SIG_SETMASK, &oldmask, 0); |
b9537f3b MW |
540 | md[0].cur = pfd[1]; md[0].want = STDIN_FILENO; |
541 | md[1].cur = pfd[1]; md[1].want = STDOUT_FILENO; | |
542 | md[2].cur = efd[1]; md[2].want = STDERR_FILENO; | |
543 | mdup(md, 3); | |
6047fbac | 544 | if (pidfp) fclose(pidfp); |
9dd01bc6 | 545 | closelog(); |
6047fbac | 546 | if (f & f_daemon) detachtty(); |
410c8acf | 547 | execvp(DA(&spawnopts)[0], DA(&spawnopts)); |
548 | die(127, "couldn't exec `%s': %s", spawnpath, strerror(errno)); | |
549 | } | |
550 | sigprocmask(SIG_SETMASK, &oldmask, 0); | |
551 | fd = pfd[0]; | |
155a970a MW |
552 | close(pfd[1]); close(efd[1]); |
553 | selbuf_init(&be, &sel, efd[0], eline, &be); | |
410c8acf | 554 | } else { |
8efdf64b | 555 | sz = strlen(sock) + 1; |
06b2a088 | 556 | if (sz > sizeof(sun.sun_path)) |
410c8acf | 557 | die(EXIT_FAILURE, "socket name `%s' too long", sock); |
558 | memset(&sun, 0, sizeof(sun)); | |
559 | sun.sun_family = AF_UNIX; | |
06b2a088 | 560 | memcpy(sun.sun_path, sock, sz); |
561 | sz = sz + offsetof(struct sockaddr_un, sun_path); | |
410c8acf | 562 | if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) |
563 | die(EXIT_FAILURE, "error making socket: %s", strerror(errno)); | |
564 | if (connect(fd, (struct sockaddr *)&sun, sz)) { | |
565 | die(EXIT_FAILURE, "error connecting to `%s': %s", | |
9dd01bc6 | 566 | sun.sun_path, strerror(errno)); |
410c8acf | 567 | } |
568 | } | |
569 | ||
1a372224 | 570 | f |= f_losing; /* pessimism */ |
ab46a787 | 571 | u_setugid(u, g); |
1a372224 MW |
572 | if (logname) |
573 | logfile(logname); | |
410c8acf | 574 | if (f & f_daemon) { |
19dd2531 | 575 | if (daemonize()) |
410c8acf | 576 | die(EXIT_FAILURE, "error becoming daemon: %s", strerror(errno)); |
577 | } | |
06b2a088 | 578 | if (pidfp) { |
a803add7 | 579 | fprintf(pidfp, "%li\n", (long)getpid()); |
06b2a088 | 580 | fclose(pidfp); |
581 | } | |
3cdc3f3a | 582 | signal(SIGPIPE, SIG_IGN); |
410c8acf | 583 | |
584 | /* --- If we're meant to be interactive, do that --- */ | |
585 | ||
5729b4bf MW |
586 | if (optind == argc) { |
587 | DRESET(&d); | |
588 | dstr_puts(&d, "watch"); | |
589 | u_quotify(&d, watch ? watch : "-A+tw"); | |
590 | setup(d.buf); | |
591 | } | |
410c8acf | 592 | if (!(f & f_noinput) && optind == argc) { |
410c8acf | 593 | selbuf_init(&bu, &sel, STDIN_FILENO, uline, &bu); |
594 | selbuf_init(&bs, &sel, fd, sline, &bs); | |
595 | for (;;) { | |
5f5150b1 | 596 | if (sel_select(&sel) && errno != EINTR && errno != EAGAIN) |
410c8acf | 597 | die(EXIT_FAILURE, "select failed: %s", strerror(errno)); |
598 | } | |
599 | } | |
600 | ||
601 | /* --- If there's a command, submit it --- */ | |
602 | ||
603 | if (optind < argc) { | |
5729b4bf MW |
604 | DRESET(&d); |
605 | dstr_puts(&d, "watch"); | |
606 | u_quotify(&d, watch ? watch : (f & f_warn) ? "-A+w" : "-A"); | |
607 | setup(d.buf); | |
608 | DRESET(&d); | |
0ed0735f MW |
609 | while (optind < argc) |
610 | u_quotify(&d, argv[optind++]); | |
410c8acf | 611 | dstr_putc(&d, '\n'); |
3cdc3f3a | 612 | errno = EIO; |
613 | if (write(fd, d.buf, d.len) != d.len || shutdown(fd, 1)) | |
614 | die(EXIT_FAILURE, "write failed: %s", strerror(errno)); | |
410c8acf | 615 | dstr_destroy(&d); |
616 | f |= f_command; | |
617 | } | |
618 | ||
619 | /* --- Pull everything else out of the box --- */ | |
620 | ||
8efdf64b | 621 | selbuf_init(&bs, &sel, fd, cline, 0); |
5f5150b1 | 622 | |
8efdf64b MW |
623 | if (f & f_syslog) |
624 | openlog(QUIS, 0, LOG_DAEMON); | |
625 | if (logfp) | |
626 | sig_add(&hup, SIGHUP, sighup, 0); | |
627 | for (;;) { | |
628 | if (sel_select(&sel) && errno != EINTR && errno != EAGAIN) | |
629 | die(EXIT_FAILURE, "select failed: %s", strerror(errno)); | |
410c8acf | 630 | } |
631 | ||
632 | return (0); | |
633 | } | |
634 | ||
635 | /*----- That's all, folks -------------------------------------------------*/ |