4 * Bring up a PPP connection and Do The Right Thing[tm] to make bringing
5 * the connection up or down with ifup and ifdown syncronous. Takes
6 * one argument: the logical name of the device to bring up. Does not
7 * detach until the interface is up or has permanently failed to come up.
9 * Copyright 1999-2003 Red Hat, Inc.
11 * This is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
30 * Register with netreport. (now exit implies deregister first)
31 * fork/exec ifup-ppp daemon <interface>
35 * if SIGTERM or SIGINT:
41 * wait for SIGCHLD to redial
43 * if no physical device found: continue
44 * elif physical device is down:
45 * wait for pppd to exit to redial if appropriate
46 * else: (physical device is up)
48 * if SIGCHLD: (pppd exited)
53 * else: (pppd was killed)
57 * When ppp-watch itself dies for reasons of its own, it uses a return code
58 * higher than 25 so as not to clash with pppd return codes, which, as of
59 * this writing, range from 0 to 19.
69 #include <sys/ioctl.h>
71 #include <sys/types.h>
72 #include <sys/resource.h>
73 #include <sys/socket.h>
81 #define IFCFGPREFIX "/etc/sysconfig/interfaces/ifcfg-"
82 #define IFUP_PPP "/lib/rc-scripts/ifup-ppp"
84 static int theSigterm = 0;
85 static int theSigint = 0;
86 static int theSighup = 0;
87 static int theSigio = 0;
88 static int theSigchld = 0;
89 static int theSigalrm = 0;
90 static int pipeArray[2];
92 // patch to respect the maxfail parameter to ppp
93 // Scott Sharkey <ssharkey@linux-no-limits.com>
94 static int dialCount = 0;
96 static void failureExit(int exitCode);
99 interrupt_child(int signo) {
100 kill(theChild, SIGINT);
104 set_signal(int signo, void (*handler)(int)) {
105 struct sigaction act;
106 act.sa_handler = handler;
107 act.sa_flags = SA_RESTART;
108 sigemptyset(&act.sa_mask);
109 sigaction(signo, &act, NULL);
112 /* Create a pipe, and fork off a child. This is the end of the road for
113 * the parent, which will wait for an exit status byte on the pipe (which
114 * is written by the child). */
116 detach(char *device) {
118 unsigned char exitCode;
121 if (pipe(pipeArray) == -1)
129 /* The parent only cares about notifications from the child. */
130 close (pipeArray[1]);
132 /* Certain signals are meant for our child, the watcher process. */
134 set_signal(SIGINT, interrupt_child);
135 set_signal(SIGTERM, interrupt_child);
136 set_signal(SIGHUP, interrupt_child);
138 /* Read the pipe until the child gives us an exit code as a byte. */
139 while (read (pipeArray[0], &exitCode, 1) == -1) {
141 case EINTR: continue;
142 default: exit (27); /* this will catch EIO in particular */
149 fprintf(stderr, "%s already up, initiating redial\n", device);
152 fprintf(stderr, "Failed to activate %s, retrying in the background\n", device);
155 fprintf(stderr, "Failed to activate %s with error %d\n", device, exitCode);
161 /* We're in the child process, which only writes the exit status
162 * of the pppd process to its parent (i.e., it reads nothing). */
163 close (pipeArray[0]);
165 /* Don't leak this into programs we call. */
166 fcntl(pipeArray[1], F_SETFD, FD_CLOEXEC);
168 /* Redirect stdio to /dev/null. */
169 fd = open("/dev/null", O_RDONLY);
170 dup2(fd, STDIN_FILENO);
173 fd = open("/dev/null", O_WRONLY);
174 dup2(fd, STDOUT_FILENO);
175 dup2(fd, STDERR_FILENO);
178 /* Become session and process group leader. */
183 /* Do magic with the pid file (/var/run/pppwatch-$DEVICE.pid):
184 * Try to open it for writing. If it exists, send a SIGHUP to whatever PID
185 * is already listed in it and remove it. Repeat until we can open it.
186 * Write out our PID, and return. */
188 doPidFile(char *device) {
189 static char pidFilePath[PATH_MAX] = "";
194 if (device == NULL) {
195 /* Remove an existing pid file -- we're exiting. */
196 if(strlen(pidFilePath) > 0) {
200 /* Set up the name of the pid file, used only the first time. */
201 snprintf(pidFilePath, sizeof(pidFilePath), "/var/run/pppwatch-%s.pid",
204 /* Create the pid file. */
206 fd = open(pidFilePath, O_WRONLY|O_TRUNC|O_CREAT|O_EXCL|O_NOFOLLOW,
207 S_IRUSR|S_IWUSR | S_IRGRP | S_IROTH);
209 /* Try to open the file for read. */
210 fd = open(pidFilePath, O_RDONLY);
212 failureExit(36); /* This is not good. */
214 /* We're already running, send a SIGHUP (we presume that they
215 * are calling ifup for a reason, so they probably want to
216 * redial) and then exit cleanly and let things go on in the
217 * background. Muck with the filename so that we don't go
218 * deleting the pid file for the already-running instance.
225 fscanf(f, "%d", &pid);
229 /* Try to kill it. */
230 if (kill(pid, SIGHUP) == -1) {
231 /* No such pid, remove the bogus pid file. */
234 /* Got it. Don't mess with the pid file on
236 memset(pidFilePath, '\0', sizeof(pidFilePath));
246 fprintf(f, "%d\n", getpid());
251 /* Fork off and exec() a child process. If reap_child is non-zero,
252 * wait for the child to exit and return 0 if it ran successfully,
253 * otherwise return 0 right away and let the SIGCHLD handler deal. */
255 fork_exec(gboolean reap, char *path, char *arg1, char *arg2, char *arg3)
267 /* Do the exec magic. Prepare by clearing the signal mask for pppd. */
269 sigprocmask(SIG_SETMASK, &sigs, NULL);
272 /* Make sure that the pppd is the leader for its process group. */
277 execl(path, path, arg1, arg2, arg3, NULL);
283 waitpid (childpid, &status, 0);
284 if (WIFEXITED(status) && (WEXITSTATUS(status) == 0)) {
294 /* Relay the pppd's exit code up to the parent -- can only be called once,
295 * because the parent exits as soon as it reads a byte. */
297 relay_exitcode(unsigned char code)
299 unsigned char exitCode;
301 write(pipeArray[1], &exitCode, 1);
305 /* Unregister with netreport, relay a status byte to the parent, clean up
306 * the pid file, and bail. */
308 failureExit(int exitCode) {
309 fork_exec(TRUE, "/sbin/netreport", "-r", NULL, NULL);
310 relay_exitcode(exitCode);
315 /* Keeps track of which signals we've seen so far. */
317 signal_tracker (int signum) {
320 theSigterm = 1; break;
322 theSigint = 1; break;
324 theSighup = 1; break;
328 theSigchld = 1; break;
330 theSigalrm = 1; break;
334 /* Return a shvarFile for this interface, taking into account one level of
335 * inheritance (eeewww). */
337 shvarfilesGet(const char *interfaceName) {
338 shvarFile *ifcfg = NULL;
339 char ifcfgName[PATH_MAX];
340 char *ifcfgParentDiff = NULL;
342 /* Start with the basic configuration. */
343 snprintf(ifcfgName, sizeof(ifcfgName), "%s%s", IFCFGPREFIX, interfaceName);
344 ifcfg = svNewFile(ifcfgName);
348 /* Do we have a parent interface (i.e., for ppp0-blah, ppp0) to inherit? */
349 ifcfgParentDiff = strchr(ifcfgName + sizeof(IFCFGPREFIX), '-');
350 if (ifcfgParentDiff) {
351 *ifcfgParentDiff = '\0';
352 ifcfg->parent = svNewFile(ifcfgName);
355 /* This is very unclean, but we have to close the shvar descriptors in
356 * case they've been numbered STDOUT_FILENO or STDERR_FILENO, which would
357 * be disastrous if inherited by a child process. */
362 close (ifcfg->parent->fd);
363 ifcfg->parent->fd = 0;
369 /* Convert a logical interface name to a real one by reading the lock
370 * file created by pppd. */
372 pppLogicalToPhysical(int *pppdPid, char *logicalName, char **physicalName) {
373 char mapFileName[PATH_MAX];
377 char *physicalDevice = NULL;
379 snprintf(mapFileName, sizeof(mapFileName), "/var/run/ppp-%s.pid",
381 fd = open(mapFileName, O_RDONLY);
383 n = read(fd, buffer, sizeof(buffer));
387 /* Split up the file at the first line break -- the PID is on the
389 if((p = strchr(buffer, '\n')) != NULL) {
393 *pppdPid = atoi(buffer);
395 /* The physical device name is on the second line. */
396 if((q = strchr(p, '\n')) != NULL) {
398 physicalDevice = strdup(p);
404 if (physicalDevice) {
406 *physicalName = physicalDevice;
408 free(physicalDevice);
412 *physicalName = NULL;
417 /* Return a boolean value indicating if the interface is up. If not, or
418 * if we don't know, return FALSE. */
420 interfaceIsUp(char *device) {
422 int family[] = {PF_INET, PF_IPX, PF_AX25, PF_APPLETALK, 0};
425 gboolean retcode = FALSE;
427 /* Create a socket suitable for doing routing ioctls. */
428 for (p = 0; (sock == -1) && family[p]; p++) {
429 sock = socket(family[p], SOCK_DGRAM, 0);
434 /* Populate the request structure for getting the interface's status. */
435 memset(&ifr, 0, sizeof(ifr));
436 strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name) - 1);
437 ifr.ifr_name[sizeof(ifr.ifr_name) - 1] = '\0';
439 /* We return TRUE iff the ioctl succeeded and the interface is UP. */
440 if (ioctl(sock, SIOCGIFFLAGS, &ifr) == -1) {
442 } else if (ifr.ifr_flags & IFF_UP) {
451 /* Very, very minimal hangup function. This just attempts to hang up a device
452 * that should already be hung up, so it does not need to be bulletproof. */
454 hangup(shvarFile *ifcfg) {
457 struct termios original_ts, ts;
459 line = svGetValue(ifcfg, "MODEMPORT");
463 fd = open(line, O_RDWR | O_NOCTTY | O_NONBLOCK);
465 if (tcgetattr(fd, &ts) != -1) {
467 write(fd, "\r", 1); /* tickle modems that do not like dropped DTR */
469 cfsetospeed(&ts, B0);
470 tcsetattr(fd, TCSANOW, &ts);
472 tcsetattr(fd, TCSANOW, &original_ts);
480 main(int argc, char **argv) {
483 char *device, *real_device, *physicalDevice = NULL;
486 sigset_t blockedsigs, unblockedsigs;
490 gboolean dying = FALSE;
492 gboolean connectedOnce = FALSE;
493 int maxfail = 0; // MAXFAIL Patch <ssharkey@linux-no-limits.com>
496 fprintf (stderr, "usage: ppp-watch <interface-name> [boot]\n");
500 if (strncmp(argv[1], "ifcfg-", 6) == 0) {
501 device = argv[1] + 6;
506 detach(device); /* Prepare a child process to monitor pppd. When we
507 return, we'll be in the child. */
509 if ((argc > 2) && (strcmp("boot", argv[2]) == 0)) {
513 ifcfg = shvarfilesGet(device);
517 real_device = svGetValue(ifcfg, "DEVICE");
518 if (real_device == NULL)
519 real_device = device;
521 doPidFile(real_device);
523 /* We'll want to know which signal interrupted our sleep below, so
524 * attach a signal handler to these. */
525 set_signal(SIGTERM, signal_tracker);
526 set_signal(SIGINT, signal_tracker);
527 set_signal(SIGHUP, signal_tracker);
528 set_signal(SIGIO, signal_tracker);
529 set_signal(SIGCHLD, signal_tracker);
531 /* We time out only if we're being run at boot-time. */
533 temp = svGetValue(ifcfg, "BOOTTIMEOUT");
535 timeout = atoi(temp);
536 if (timeout < 1) timeout = 1;
541 set_signal(SIGALRM, signal_tracker);
545 /* Register us to get a signal when something changes. Yes, that's vague. */
546 fork_exec(TRUE, "/sbin/netreport", NULL, NULL, NULL);
548 /* Reset theSigchld, which should have been triggered by netreport. */
551 /* We don't set up the procmask until after we have received the netreport
552 * signal. Do so now. */
553 sigemptyset(&blockedsigs);
554 sigaddset(&blockedsigs, SIGTERM);
555 sigaddset(&blockedsigs, SIGINT);
556 sigaddset(&blockedsigs, SIGHUP);
557 sigaddset(&blockedsigs, SIGIO);
558 sigaddset(&blockedsigs, SIGCHLD);
560 sigaddset(&blockedsigs, SIGALRM);
562 sigprocmask(SIG_BLOCK, &blockedsigs, NULL);
564 sigfillset(&unblockedsigs);
565 sigdelset(&unblockedsigs, SIGTERM);
566 sigdelset(&unblockedsigs, SIGINT);
567 sigdelset(&unblockedsigs, SIGHUP);
568 sigdelset(&unblockedsigs, SIGIO);
569 sigdelset(&unblockedsigs, SIGCHLD);
571 sigdelset(&unblockedsigs, SIGALRM);
573 sigprocmask(SIG_UNBLOCK, &unblockedsigs, NULL);
575 /* Initialize the retry timeout using the RETRYTIMEOUT setting. */
576 temp = svGetValue(ifcfg, "RETRYTIMEOUT");
578 timeout = atoi(temp);
584 /* Start trying to bring the interface up. */
585 fork_exec(FALSE, IFUP_PPP, "daemon", device, boot);
588 /* Wait for a signal. */
595 sigsuspend(&unblockedsigs);
598 /* If we got SIGTERM or SIGINT, give up and hang up. */
599 if (theSigterm || theSigint) {
600 theSigterm = theSigint = 0;
602 /* If we've already tried to exit this way, use SIGKILL instead
603 * of SIGTERM, because pppd's just being stubborn. */
611 /* Get the pid of our child pppd. */
612 pppLogicalToPhysical(&pppdPid, device, NULL);
614 /* We don't know what our child pid is. This is very confusing. */
619 /* Die, pppd, die. */
620 kill(pppdPid, sendsig);
621 if (sendsig == SIGKILL) {
622 kill(-pppdPid, SIGTERM); /* Give it a chance to die nicely, then
623 kill its whole process group. */
625 kill(-pppdPid, sendsig);
631 /* If we got SIGHUP, reload and redial. */
635 /* Free and reload the configuration structure. */
637 svCloseFile(ifcfg->parent);
639 ifcfg = shvarfilesGet(device);
641 /* Get the PID of our child pppd. */
642 pppLogicalToPhysical(&pppdPid, device, NULL);
643 kill(pppdPid, SIGTERM);
645 /* We'll redial when the SIGCHLD arrives, even if PERSIST is
646 * not set (the latter handled by clearing the "we've connected
647 * at least once" flag). */
648 connectedOnce = FALSE;
650 /* We don't want to delay before redialing, either, so cut
651 * the retry timeout to zero. */
655 /* If we got a SIGIO (from netreport, presumably), check if the
656 * interface is up and return zero (via our parent) if it is. */
660 pppLogicalToPhysical(NULL, device, &physicalDevice);
661 if (physicalDevice) {
662 if (interfaceIsUp(physicalDevice)) {
663 /* The interface is up, so report a success to a parent if
664 * we have one. Any errors after this we just swallow. */
666 connectedOnce = TRUE;
668 free(physicalDevice);
672 /* If we got a SIGCHLD, then pppd died (possibly because we killed it),
673 * and we need to restart it after timeout seconds. */
677 /* Find its pid, which is also its process group ID. */
678 waited = waitpid(-1, &status, 0);
683 /* Now, we need to kill any children of pppd still in pppd's
684 * process group, in case they are hanging around.
685 * pppd is dead (we just waited for it) but there is no
686 * guarantee that its children are dead, and they will
687 * hold the modem if we do not get rid of them.
688 * We have kept the old pid/pgrp around in pppdPid. */
690 kill(-pppdPid, SIGTERM); /* give it a chance to die nicely */
692 kill(-pppdPid, SIGKILL);
697 /* Bail if the child exitted abnormally or we were already
698 * signalled to kill it. */
699 if (!WIFEXITED(status)) {
703 failureExit(WEXITSTATUS(status));
706 /* Error conditions from which we do not expect to recover
707 * without user intervention -- do not fill up the logs. */
708 switch (WEXITSTATUS(status)) {
709 case 1: case 2: case 3: case 4: case 6:
710 case 7: case 9: case 14: case 17:
711 failureExit(WEXITSTATUS(status));
717 /* PGB 08/20/02: We no longer retry connecting MAXFAIL
718 times on a failed connect script unless RETRYCONNECT is
720 if ((WEXITSTATUS(status) == 8) &&
721 !svTrueValue(ifcfg, "RETRYCONNECT", FALSE)) {
722 failureExit(WEXITSTATUS(status));
725 /* If we've never connected, or PERSIST is set, dial again, up
726 * to MAXFAIL times. */
727 if ((WEXITSTATUS(status) == 8) ||
729 svTrueValue(ifcfg, "PERSIST", FALSE)) {
730 /* If we've been connected (i.e., if we didn't force a redial,
731 * but the connection went down) wait for DISCONNECTTIMEOUT
732 * seconds before redialing. */
734 connectedOnce = FALSE;
735 temp = svGetValue(ifcfg, "DISCONNECTTIMEOUT");
737 timeout = atoi(temp);
743 sigprocmask(SIG_UNBLOCK, &blockedsigs, NULL);
745 sigprocmask(SIG_BLOCK, &blockedsigs, NULL);
752 fork_exec(FALSE, IFUP_PPP, "daemon", device, boot);
754 /* Reinitialize the retry timeout. */
755 temp = svGetValue(ifcfg, "RETRYTIMEOUT");
757 timeout = atoi(temp);
762 // Scott Sharkey <ssharkey@linux-no-limits.com>
764 temp = svGetValue(ifcfg, "MAXFAIL");
766 maxfail = atoi(temp);
771 if ( maxfail != 0 ) {
773 if ( dialCount >= maxfail )
774 failureExit(WEXITSTATUS(status));
777 failureExit(WEXITSTATUS(status));
781 /* We timed out, and we're running at boot-time. */