Collapse gd_astpending and gd_reqpri together into gd_reqflags. gd_reqflags
[dragonfly.git] / libexec / pppoed / pppoed.c
CommitLineData
984263bc
MD
1/*-
2 * Copyright (c) 1999-2001 Brian Somers <brian@Awfulhak.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD: src/libexec/pppoed/pppoed.c,v 1.2.6.8 2002/06/17 02:21:25 brian Exp $
1de703da 27 * $DragonFly: src/libexec/pppoed/pppoed.c,v 1.2 2003/06/17 04:27:07 dillon Exp $
984263bc
MD
28 */
29
30#include <sys/param.h>
31#include <sys/socket.h>
32#include <sys/un.h>
33#include <netinet/in.h>
34#include <arpa/inet.h>
35#include <netdb.h>
36#include <netgraph.h>
37#include <net/ethernet.h>
38#include <netinet/in_systm.h>
39#include <netinet/ip.h>
40#include <netgraph/ng_ether.h>
41#include <netgraph/ng_message.h>
42#include <netgraph/ng_pppoe.h>
43#include <netgraph/ng_socket.h>
44
45#include <errno.h>
46#include <paths.h>
47#include <signal.h>
48#include <stdio.h>
49#include <stdarg.h>
50#include <stdlib.h>
51#include <string.h>
52#include <sysexits.h>
53#include <sys/fcntl.h>
54#ifndef NOKLDLOAD
55#include <sys/linker.h>
56#include <sys/module.h>
57#endif
58#include <sys/uio.h>
59#include <sys/wait.h>
60#include <syslog.h>
61#include <termios.h>
62#include <unistd.h>
63
64
65#define DEFAULT_EXEC_PREFIX "exec /usr/sbin/ppp -direct "
66#define HISMACADDR "HISMACADDR"
67#define SESSION_ID "SESSION_ID"
68
69static void nglogx(const char *, ...) __printflike(1, 2);
70
71static int ReceivedSignal;
72
73static int
74usage(const char *prog)
75{
76 fprintf(stderr, "usage: %s [-Fd] [-P pidfile] [-a name] [-e exec | -l label]"
77 " [-p provider] interface\n", prog);
78 return EX_USAGE;
79}
80
81static void
82Farewell(int sig)
83{
84 ReceivedSignal = sig;
85}
86
87static int
88ConfigureNode(const char *prog, const char *iface, const char *provider,
89 int cs, int ds, int debug, struct ngm_connect *ngc)
90{
91 /*
92 * We're going to do this with the passed `ds' & `cs' descriptors:
93 *
94 * .---------.
95 * | ether |
96 * | <iface> |
97 * `---------'
98 * (orphan) ds cs
99 * | | |
100 * | | |
101 * (ethernet) | |
102 * .---------. .-----------.
103 * | pppoe | | socket |
104 * | <iface> |(pppoe-<pid>)<---->(pppoe-<pid>)| <unnamed> |
105 * `--------- `-----------'
106 * (exec-<pid>)
107 * ^ .-----------. .-------------.
108 * | | socket | | ppp -direct |
109 * `--->(exec-<pid>)| <unnamed> |--fd--| provider |
110 * `-----------' `-------------'
111 *
112 * where there are potentially many ppp processes running off of the
113 * same PPPoE node.
114 * The exec-<pid> hook isn't made 'till we Spawn().
115 */
116
117 char *epath, *spath;
118 struct ngpppoe_init_data *data;
119 const struct hooklist *hlist;
120 const struct nodeinfo *ninfo;
121 const struct linkinfo *nlink;
122 struct ngm_mkpeer mkp;
123 struct ng_mesg *resp;
124 u_char rbuf[2048];
125 int f, plen;
126
127 /*
128 * Ask for a list of hooks attached to the "ether" node. This node should
129 * magically exist as a way of hooking stuff onto an ethernet device
130 */
131 epath = (char *)alloca(strlen(iface) + 2);
132 sprintf(epath, "%s:", iface);
133
134 if (debug)
135 fprintf(stderr, "Sending NGM_LISTHOOKS to %s\n", epath);
136
137 if (NgSendMsg(cs, epath, NGM_GENERIC_COOKIE, NGM_LISTHOOKS, NULL, 0) < 0) {
138 if (errno == ENOENT)
139 fprintf(stderr, "%s Cannot send a netgraph message: Invalid interface\n",
140 epath);
141 else
142 fprintf(stderr, "%s Cannot send a netgraph message: %s\n",
143 epath, strerror(errno));
144 return EX_UNAVAILABLE;
145 }
146
147 /* Get our list back */
148 resp = (struct ng_mesg *)rbuf;
149 if (NgRecvMsg(cs, resp, sizeof rbuf, NULL) <= 0) {
150 perror("Cannot get netgraph response");
151 return EX_UNAVAILABLE;
152 }
153
154 hlist = (const struct hooklist *)resp->data;
155 ninfo = &hlist->nodeinfo;
156
157 if (debug)
158 fprintf(stderr, "Got reply from id [%x]: Type %s with %d hooks\n",
159 ninfo->id, ninfo->type, ninfo->hooks);
160
161 /* Make sure we've got the right type of node */
162 if (strncmp(ninfo->type, NG_ETHER_NODE_TYPE, sizeof NG_ETHER_NODE_TYPE - 1)) {
163 fprintf(stderr, "%s Unexpected node type ``%s'' (wanted ``"
164 NG_ETHER_NODE_TYPE "'')\n", epath, ninfo->type);
165 return EX_DATAERR;
166 }
167
168 /* look for a hook already attached. */
169 for (f = 0; f < ninfo->hooks; f++) {
170 nlink = &hlist->link[f];
171
172 if (debug)
173 fprintf(stderr, " Got [%x]:%s -> [%x]:%s\n", ninfo->id,
174 nlink->ourhook, nlink->nodeinfo.id, nlink->peerhook);
175
176 if (!strcmp(nlink->ourhook, NG_ETHER_HOOK_ORPHAN) ||
177 !strcmp(nlink->ourhook, NG_ETHER_HOOK_DIVERT)) {
178 /*
179 * Something is using the data coming out of this `ether' node.
180 * If it's a PPPoE node, we use that node, otherwise we complain that
181 * someone else is using the node.
182 */
183 if (strcmp(nlink->nodeinfo.type, NG_PPPOE_NODE_TYPE)) {
184 fprintf(stderr, "%s Node type %s is currently active\n",
185 epath, nlink->nodeinfo.type);
186 return EX_UNAVAILABLE;
187 }
188 break;
189 }
190 }
191
192 if (f == ninfo->hooks) {
193 /*
194 * Create a new PPPoE node connected to the `ether' node using
195 * the magic `orphan' and `ethernet' hooks
196 */
197 snprintf(mkp.type, sizeof mkp.type, "%s", NG_PPPOE_NODE_TYPE);
198 snprintf(mkp.ourhook, sizeof mkp.ourhook, "%s", NG_ETHER_HOOK_ORPHAN);
199 snprintf(mkp.peerhook, sizeof mkp.peerhook, "%s", NG_PPPOE_HOOK_ETHERNET);
200
201 if (debug)
202 fprintf(stderr, "Send MKPEER: %s%s -> [type %s]:%s\n", epath,
203 mkp.ourhook, mkp.type, mkp.peerhook);
204
205 if (NgSendMsg(cs, epath, NGM_GENERIC_COOKIE,
206 NGM_MKPEER, &mkp, sizeof mkp) < 0) {
207 fprintf(stderr, "%s Cannot create a peer PPPoE node: %s\n",
208 epath, strerror(errno));
209 return EX_OSERR;
210 }
211 }
212
213 /* Connect the PPPoE node to our socket node. */
214 snprintf(ngc->path, sizeof ngc->path, "%s%s", epath, NG_ETHER_HOOK_ORPHAN);
215 snprintf(ngc->ourhook, sizeof ngc->ourhook, "pppoe-%ld", (long)getpid());
216 memcpy(ngc->peerhook, ngc->ourhook, sizeof ngc->peerhook);
217
218 if (NgSendMsg(cs, ".:", NGM_GENERIC_COOKIE,
219 NGM_CONNECT, ngc, sizeof *ngc) < 0) {
220 perror("Cannot CONNECT PPPoE and socket nodes");
221 return EX_OSERR;
222 }
223
224 plen = strlen(provider);
225
226 data = (struct ngpppoe_init_data *)alloca(sizeof *data + plen);
227 snprintf(data->hook, sizeof data->hook, "%s", ngc->peerhook);
228 memcpy(data->data, provider, plen);
229 data->data_len = plen;
230
231 spath = (char *)alloca(strlen(ngc->peerhook) + 3);
232 strcpy(spath, ".:");
233 strcpy(spath + 2, ngc->ourhook);
234
235 if (debug) {
236 if (provider)
237 fprintf(stderr, "Sending PPPOE_LISTEN to %s, provider %s\n",
238 spath, provider);
239 else
240 fprintf(stderr, "Sending PPPOE_LISTEN to %s\n", spath);
241 }
242
243 if (NgSendMsg(cs, spath, NGM_PPPOE_COOKIE, NGM_PPPOE_LISTEN,
244 data, sizeof *data + plen) == -1) {
245 fprintf(stderr, "%s: Cannot LISTEN on netgraph node: %s\n",
246 spath, strerror(errno));
247 return EX_OSERR;
248 }
249
250 return 0;
251}
252
253static void
254Spawn(const char *prog, const char *acname, const char *provider,
255 const char *exec, struct ngm_connect ngc, int cs, int ds, void *request,
256 int sz, int debug)
257{
258 char msgbuf[sizeof(struct ng_mesg) + sizeof(struct ngpppoe_sts)];
259 struct ng_mesg *rep = (struct ng_mesg *)msgbuf;
260 struct ngpppoe_sts *sts = (struct ngpppoe_sts *)(msgbuf + sizeof *rep);
261 struct ngpppoe_init_data *data;
262 char env[sizeof(HISMACADDR)+18], unknown[14], sessionid[5], *path;
263 unsigned char *macaddr;
264 const char *msg;
265 int ret, slen;
266
267 switch ((ret = fork())) {
268 case -1:
269 syslog(LOG_ERR, "fork: %m");
270 break;
271
272 case 0:
273 switch (fork()) {
274 case 0:
275 break;
276 case -1:
277 _exit(errno);
278 default:
279 _exit(0);
280 }
281 close(cs);
282 close(ds);
283
284 /* Create a new socket node */
285 if (debug)
286 syslog(LOG_INFO, "Creating a new socket node");
287
288 if (NgMkSockNode(NULL, &cs, &ds) == -1) {
289 syslog(LOG_ERR, "Cannot create netgraph socket node: %m");
290 _exit(EX_CANTCREAT);
291 }
292
293 /* Connect the PPPoE node to our new socket node. */
294 snprintf(ngc.ourhook, sizeof ngc.ourhook, "exec-%ld", (long)getpid());
295 memcpy(ngc.peerhook, ngc.ourhook, sizeof ngc.peerhook);
296
297 if (debug)
298 syslog(LOG_INFO, "Sending CONNECT from .:%s -> %s.%s",
299 ngc.ourhook, ngc.path, ngc.peerhook);
300 if (NgSendMsg(cs, ".:", NGM_GENERIC_COOKIE,
301 NGM_CONNECT, &ngc, sizeof ngc) < 0) {
302 syslog(LOG_ERR, "Cannot CONNECT PPPoE and socket nodes: %m");
303 _exit(EX_OSERR);
304 }
305
306 /*
307 * If we tell the socket node not to LINGER, it will go away when
308 * the last hook is removed.
309 */
310 if (debug)
311 syslog(LOG_INFO, "Sending NGM_SOCK_CMD_NOLINGER to socket");
312 if (NgSendMsg(cs, ".:", NGM_SOCKET_COOKIE,
313 NGM_SOCK_CMD_NOLINGER, NULL, 0) < 0) {
314 syslog(LOG_ERR, "Cannot send NGM_SOCK_CMD_NOLINGER: %m");
315 _exit(EX_OSERR);
316 }
317
318 /* Put the PPPoE node into OFFER mode */
319 slen = strlen(acname);
320 data = (struct ngpppoe_init_data *)alloca(sizeof *data + slen);
321 snprintf(data->hook, sizeof data->hook, "%s", ngc.ourhook);
322 memcpy(data->data, acname, slen);
323 data->data_len = slen;
324
325 path = (char *)alloca(strlen(ngc.ourhook) + 3);
326 strcpy(path, ".:");
327 strcpy(path + 2, ngc.ourhook);
328
329 syslog(LOG_INFO, "Offering to %s as access concentrator %s",
330 path, acname);
331 if (NgSendMsg(cs, path, NGM_PPPOE_COOKIE, NGM_PPPOE_OFFER,
332 data, sizeof *data + slen) == -1) {
333 syslog(LOG_INFO, "%s: Cannot OFFER on netgraph node: %m", path);
334 _exit(EX_OSERR);
335 }
336 /* If we have a provider code, set it */
337 if (provider) {
338 slen = strlen(provider);
339 data = (struct ngpppoe_init_data *)alloca(sizeof *data + slen);
340 snprintf(data->hook, sizeof data->hook, "%s", ngc.ourhook);
341 memcpy(data->data, provider, slen);
342 data->data_len = slen;
343
344 syslog(LOG_INFO, "adding to %s as offered service %s",
345 path, acname);
346 if (NgSendMsg(cs, path, NGM_PPPOE_COOKIE, NGM_PPPOE_SERVICE,
347 data, sizeof *data + slen) == -1) {
348 syslog(LOG_INFO, "%s: Cannot add service on netgraph node: %m", path);
349 _exit(EX_OSERR);
350 }
351 }
352
353 /* Put the peer's MAC address in the environment */
354 if (sz >= sizeof(struct ether_header)) {
355 macaddr = ((struct ether_header *)request)->ether_shost;
356 snprintf(env, sizeof(env), "%s=%x:%x:%x:%x:%x:%x", HISMACADDR,
357 macaddr[0], macaddr[1], macaddr[2], macaddr[3], macaddr[4],
358 macaddr[5]);
359 if (putenv(env) != 0)
360 syslog(LOG_INFO, "putenv: cannot set %s: %m", env);
361 }
362
363 /* And send our request data to the waiting node */
364 if (debug)
365 syslog(LOG_INFO, "Sending original request to %s (%d bytes)", path, sz);
366 if (NgSendData(ds, ngc.ourhook, request, sz) == -1) {
367 syslog(LOG_ERR, "Cannot send original request to %s: %m", path);
368 _exit(EX_OSERR);
369 }
370
371 /* Then wait for a success indication */
372
373 if (debug)
374 syslog(LOG_INFO, "Waiting for a SUCCESS reply %s", path);
375
376 do {
377 if ((ret = NgRecvMsg(cs, rep, sizeof msgbuf, NULL)) < 0) {
378 syslog(LOG_ERR, "%s: Cannot receive a message: %m", path);
379 _exit(EX_OSERR);
380 }
381
382 if (ret == 0) {
383 /* The socket has been closed */
384 syslog(LOG_INFO, "%s: Client timed out", path);
385 _exit(EX_TEMPFAIL);
386 }
387
388 if (rep->header.version != NG_VERSION) {
389 syslog(LOG_ERR, "%ld: Unexpected netgraph version, expected %ld",
390 (long)rep->header.version, (long)NG_VERSION);
391 _exit(EX_PROTOCOL);
392 }
393
394 if (rep->header.typecookie != NGM_PPPOE_COOKIE) {
395 syslog(LOG_INFO, "%ld: Unexpected netgraph cookie, expected %ld",
396 (long)rep->header.typecookie, (long)NGM_PPPOE_COOKIE);
397 continue;
398 }
399
400 switch (rep->header.cmd) {
401 case NGM_PPPOE_SET_FLAG: msg = "SET_FLAG"; break;
402 case NGM_PPPOE_CONNECT: msg = "CONNECT"; break;
403 case NGM_PPPOE_LISTEN: msg = "LISTEN"; break;
404 case NGM_PPPOE_OFFER: msg = "OFFER"; break;
405 case NGM_PPPOE_SUCCESS: msg = "SUCCESS"; break;
406 case NGM_PPPOE_FAIL: msg = "FAIL"; break;
407 case NGM_PPPOE_CLOSE: msg = "CLOSE"; break;
408 case NGM_PPPOE_GET_STATUS: msg = "GET_STATUS"; break;
409 case NGM_PPPOE_ACNAME:
410 msg = "ACNAME";
411 if (setenv("ACNAME", sts->hook, 1) != 0)
412 syslog(LOG_WARNING, "setenv: cannot set ACNAME=%s: %m",
413 sts->hook);
414 break;
415 case NGM_PPPOE_SESSIONID:
416 msg = "SESSIONID";
417 snprintf(sessionid, sizeof sessionid, "%04x", *(u_int16_t *)sts);
418 if (setenv("SESSIONID", sessionid, 1) != 0)
419 syslog(LOG_WARNING, "setenv: cannot set SESSIONID=%s: %m",
420 sessionid);
421 break;
422 default:
423 snprintf(unknown, sizeof unknown, "<%d>", (int)rep->header.cmd);
424 msg = unknown;
425 break;
426 }
427
428 switch (rep->header.cmd) {
429 case NGM_PPPOE_FAIL:
430 case NGM_PPPOE_CLOSE:
431 syslog(LOG_ERR, "Received NGM_PPPOE_%s (hook \"%s\")",
432 msg, sts->hook);
433 _exit(0);
434 }
435
436 syslog(LOG_INFO, "Received NGM_PPPOE_%s (hook \"%s\")", msg, sts->hook);
437 } while (rep->header.cmd != NGM_PPPOE_SUCCESS);
438
439 dup2(ds, STDIN_FILENO);
440 dup2(ds, STDOUT_FILENO);
441 close(ds);
442 close(cs);
443
444 setsid();
445 syslog(LOG_INFO, "Executing: %s", exec);
446 execlp(_PATH_BSHELL, _PATH_BSHELL, "-c", exec, (char *)NULL);
447 syslog(LOG_ERR, "execlp failed: %m");
448 _exit(EX_OSFILE);
449
450 default:
451 wait(&ret);
452 errno = ret;
453 if (errno)
454 syslog(LOG_ERR, "Second fork failed: %m");
455 break;
456 }
457}
458
459#ifndef NOKLDLOAD
460static int
461LoadModules(void)
462{
463 const char *module[] = { "netgraph", "ng_socket", "ng_ether", "ng_pppoe" };
464 int f;
465
466 for (f = 0; f < sizeof module / sizeof *module; f++)
467 if (modfind(module[f]) == -1 && kldload(module[f]) == -1) {
468 fprintf(stderr, "kldload: %s: %s\n", module[f], strerror(errno));
469 return 0;
470 }
471
472 return 1;
473}
474#endif
475
476static void
477nglog(const char *fmt, ...)
478{
479 char nfmt[256];
480 va_list ap;
481
482 snprintf(nfmt, sizeof nfmt, "%s: %s", fmt, strerror(errno));
483 va_start(ap, fmt);
484 vsyslog(LOG_INFO, nfmt, ap);
485 va_end(ap);
486}
487
488static void
489nglogx(const char *fmt, ...)
490{
491 va_list ap;
492
493 va_start(ap, fmt);
494 vsyslog(LOG_INFO, fmt, ap);
495 va_end(ap);
496}
497
498int
499main(int argc, char *argv[])
500{
501 char hostname[MAXHOSTNAMELEN], *exec, rhook[NG_HOOKLEN + 1];
502 unsigned char response[1024];
503 const char *label, *prog, *provider, *acname;
504 struct ngm_connect ngc;
505 struct sigaction act;
506 int ch, cs, ds, ret, optF, optd, optn, sz, f;
507 const char *pidfile;
508
509 prog = strrchr(argv[0], '/');
510 prog = prog ? prog + 1 : argv[0];
511 pidfile = NULL;
512 exec = NULL;
513 label = NULL;
514 acname = NULL;
515 provider = "";
516 optF = optd = optn = 0;
517
518 while ((ch = getopt(argc, argv, "FP:a:de:l:n:p:")) != -1) {
519 switch (ch) {
520 case 'F':
521 optF = 1;
522 break;
523
524 case 'P':
525 pidfile = optarg;
526 break;
527
528 case 'a':
529 acname = optarg;
530 break;
531
532 case 'd':
533 optd = 1;
534 break;
535
536 case 'e':
537 exec = optarg;
538 break;
539
540 case 'l':
541 label = optarg;
542 break;
543
544 case 'n':
545 optn = 1;
546 NgSetDebug(atoi(optarg));
547 break;
548
549 case 'p':
550 provider = optarg;
551 break;
552
553 default:
554 return usage(prog);
555 }
556 }
557
558 if (optind >= argc || optind + 2 < argc)
559 return usage(prog);
560
561 if (exec != NULL && label != NULL)
562 return usage(prog);
563
564 if (exec == NULL) {
565 if (label == NULL)
566 label = provider;
567 if (label == NULL) {
568 fprintf(stderr, "%s: Either a provider, a label or an exec command"
569 " must be given\n", prog);
570 return usage(prog);
571 }
572 exec = (char *)alloca(sizeof DEFAULT_EXEC_PREFIX + strlen(label));
573 if (exec == NULL) {
574 fprintf(stderr, "%s: Cannot allocate %d bytes\n", prog,
575 (int)(sizeof DEFAULT_EXEC_PREFIX) + strlen(label));
576 return EX_OSERR;
577 }
578 strcpy(exec, DEFAULT_EXEC_PREFIX);
579 strcpy(exec + sizeof DEFAULT_EXEC_PREFIX - 1, label);
580 }
581
582 if (acname == NULL) {
583 char *dot;
584
585 if (gethostname(hostname, sizeof hostname))
586 strcpy(hostname, "localhost");
587 else if ((dot = strchr(hostname, '.')))
588 *dot = '\0';
589
590 acname = hostname;
591 }
592
593#ifndef NOKLDLOAD
594 if (!LoadModules())
595 return EX_UNAVAILABLE;
596#endif
597
598 /* Create a socket node */
599 if (NgMkSockNode(NULL, &cs, &ds) == -1) {
600 perror("Cannot create netgraph socket node");
601 return EX_CANTCREAT;
602 }
603
604 /* Connect it up (and fill in `ngc') */
605 if ((ret = ConfigureNode(prog, argv[optind], provider, cs, ds,
606 optd, &ngc)) != 0) {
607 close(cs);
608 close(ds);
609 return ret;
610 }
611
612 if (!optF && daemon(1, 0) == -1) {
613 perror("daemon()");
614 close(cs);
615 close(ds);
616 return EX_OSERR;
617 }
618
619
620 if (pidfile != NULL) {
621 FILE *fp;
622
623 if ((fp = fopen(pidfile, "w")) == NULL) {
624 perror(pidfile);
625 close(cs);
626 close(ds);
627 return EX_CANTCREAT;
628 } else {
629 fprintf(fp, "%d\n", (int)getpid());
630 fclose(fp);
631 }
632 }
633
634 openlog(prog, LOG_PID | (optF ? LOG_PERROR : 0), LOG_DAEMON);
635 if (!optF && optn)
636 NgSetErrLog(nglog, nglogx);
637
638 memset(&act, '\0', sizeof act);
639 act.sa_handler = Farewell;
640 act.sa_flags = 0;
641 sigemptyset(&act.sa_mask);
642 sigaction(SIGHUP, &act, NULL);
643 sigaction(SIGINT, &act, NULL);
644 sigaction(SIGQUIT, &act, NULL);
645 sigaction(SIGTERM, &act, NULL);
646
647 while (!ReceivedSignal) {
648 if (*provider)
649 syslog(LOG_INFO, "Listening as provider %s", provider);
650 else
651 syslog(LOG_INFO, "Listening");
652
653 switch (sz = NgRecvData(ds, response, sizeof response, rhook)) {
654 case -1:
655 syslog(LOG_INFO, "NgRecvData: %m");
656 break;
657 case 0:
658 syslog(LOG_INFO, "NgRecvData: socket closed");
659 break;
660 default:
661 if (optd) {
662 char *dbuf, *ptr;
663
664 ptr = dbuf = alloca(sz * 2 + 1);
665 for (f = 0; f < sz; f++, ptr += 2)
666 sprintf(ptr, "%02x", (u_char)response[f]);
667 *ptr = '\0';
668 syslog(LOG_INFO, "Got %d bytes of data: %s", sz, dbuf);
669 }
670 }
671 if (sz <= 0) {
672 ret = EX_UNAVAILABLE;
673 break;
674 }
675 Spawn(prog, acname, provider, exec, ngc, cs, ds, response, sz, optd);
676 }
677
678 if (pidfile)
679 remove(pidfile);
680
681 if (ReceivedSignal) {
682 syslog(LOG_INFO, "Received signal %d, exiting", ReceivedSignal);
683
684 signal(ReceivedSignal, SIG_DFL);
685 raise(ReceivedSignal);
686
687 /* NOTREACHED */
688
689 ret = -ReceivedSignal;
690 }
691
692 return ret;
693}