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