X-Git-Url: http://git.ozlabs.org/?p=ppp.git;a=blobdiff_plain;f=pppd%2Fmain.c;h=555ef957f9326c2bc8709cb6b15daf0e3403dc9b;hp=c3d72a2dae9840b83ce13552721b50f2748267a4;hb=d3a41a77dbfeaee46fb1f00b66bddd4677e1e00f;hpb=00d6a7e0e3fbe64dcfbfd36eeb45388236c4345e diff --git a/pppd/main.c b/pppd/main.c index c3d72a2..555ef95 100644 --- a/pppd/main.c +++ b/pppd/main.c @@ -18,7 +18,7 @@ */ #ifndef lint -static char rcsid[] = "$Id: main.c,v 1.59 1999/03/16 02:50:15 paulus Exp $"; +static char rcsid[] = "$Id: main.c,v 1.62 1999/03/19 01:26:41 paulus Exp $"; #endif #include @@ -88,7 +88,7 @@ int hungup; /* terminal has been hung up */ int privileged; /* we're running as real uid root */ int need_holdoff; /* need holdoff period before restarting */ int detached; /* have detached from terminal */ -int log_to_stderr; /* send log messages to stderr too */ +int log_to_fd; /* send log messages to this fd too */ static int fd_ppp; /* fd for talking PPP */ static int fd_loop; /* fd for getting demand-dial packets */ @@ -120,6 +120,7 @@ int ngroups; /* How many groups valid in groups */ static void create_pidfile __P((void)); static void cleanup __P((void)); static void close_tty __P((void)); +static void hangup_modem __P((int)); static void get_input __P((void)); static void calltimeout __P((void)); static struct timeval *timeleft __P((struct timeval *)); @@ -131,11 +132,13 @@ static void toggle_debug __P((int)); static void open_ccp __P((int)); static void bad_signal __P((int)); static void holdoff_end __P((void *)); -static int device_script __P((char *, int, int)); +static int device_script __P((char *, int)); static void reap_kids __P((void)); static void pr_log __P((void *, char *, ...)); static void logit __P((int, char *, va_list)); static void vslp_printer __P((void *, char *, ...)); +static void format_packet __P((u_char *, int, void (*) (void *, char *, ...), + void *)); extern char *ttyname __P((int)); extern char *getlogin __P((void)); @@ -191,8 +194,8 @@ main(argc, argv) phase = PHASE_INITIALIZE; p = ttyname(0); if (p) - strlcpy(devnam, sizeof(devnam), p); - strlcpy(default_devnam, sizeof(default_devnam), devnam); + strlcpy(devnam, p, sizeof(devnam)); + strlcpy(default_devnam, devnam, sizeof(default_devnam)); script_env = NULL; @@ -275,7 +278,7 @@ main(argc, argv) default_device = 1; if (default_device) nodetach = 1; - log_to_stderr = !default_device; + log_to_fd = !default_device? 1: -1; /* default to stdout */ /* * Initialize system-dependent stuff and magic number package. @@ -511,7 +514,7 @@ main(argc, argv) */ set_up_tty(ttyfd, 1); - if (device_script(connector, ttyfd, ttyfd) < 0) { + if (device_script(connector, ttyfd) < 0) { error("Connect script failed"); goto fail; } @@ -538,7 +541,7 @@ main(argc, argv) /* run welcome script, if any */ if (welcomer && welcomer[0]) { - if (device_script(welcomer, ttyfd, ttyfd) < 0) + if (device_script(welcomer, ttyfd) < 0) warn("Welcome script failed"); } @@ -576,11 +579,11 @@ main(argc, argv) } waiting = 0; calltimeout(); + get_input(); if (kill_link) { lcp_close(0, "User request"); kill_link = 0; } - get_input(); if (open_ccp_flag) { if (phase == PHASE_NETWORK) { ccp_fsm[0].flags = OPT_RESTART; /* clears OPT_SILENT */ @@ -608,12 +611,14 @@ main(argc, argv) */ if (disconnector && !hungup) { set_up_tty(ttyfd, 1); - if (device_script(disconnector, ttyfd, ttyfd) < 0) { + if (device_script(disconnector, ttyfd) < 0) { warn("disconnect script failed"); } else { info("Serial link disconnected."); } } + if (!hungup) + lcp_lowerdown(0); fail: if (ttyfd >= 0) @@ -684,7 +689,7 @@ detach() die(1); } detached = 1; - log_to_stderr = 0; + log_to_fd = -1; pid = getpid(); /* update pid file if it has been written already */ if (pidfilename[0]) @@ -802,16 +807,7 @@ get_input() /* - * quit - Clean up state and exit (with an error indication). - */ -void -quit() -{ - die(1); -} - -/* - * die - like quit, except we can specify an exit status. + * die - clean up state and exit with the specified status. */ void die(status) @@ -876,7 +872,8 @@ close_tty() /* * hangup_modem - hang up the modem by clearing DTR. */ -void hangup_modem(ttyfd) +static void +hangup_modem(ttyfd) int ttyfd; { setdtr(ttyfd, 0); @@ -1138,9 +1135,9 @@ bad_signal(sig) * serial device. */ static int -device_script(program, in, out) +device_script(program, inout) char *program; - int in, out; + int inout; { int pid; int status; @@ -1157,25 +1154,16 @@ device_script(program, in, out) if (pid == 0) { sys_close(); closelog(); - if (in == out) { - if (in != 0) { - dup2(in, 0); - close(in); - } - dup2(0, 1); - } else { - if (out == 0) - out = dup(out); - if (in != 0) { - dup2(in, 0); - close(in); - } - if (out != 1) { - dup2(out, 1); - close(out); - } + if (inout == 2) { + /* aargh!!! */ + int newio = dup(inout); + close(inout); + inout = newio; } - if (!nodetach && !updetach) { + if (log_to_fd >= 0) { + if (log_to_fd != 2) + dup2(log_to_fd, 2); + } else { close(2); errfd = open(_PATH_CONNERRS, O_WRONLY | O_APPEND | O_CREAT, 0600); if (errfd >= 0 && errfd != 2) { @@ -1183,6 +1171,11 @@ device_script(program, in, out) close(errfd); } } + if (inout != 0) { + dup2(inout, 0); + close(inout); + } + dup2(0, 1); setuid(uid); if (getuid() != uid) { error("setuid failed"); @@ -1369,7 +1362,7 @@ log_packet(p, len, prefix, level) char *prefix; int level; { - strlcpy(line, sizeof(line), prefix); + strlcpy(line, prefix, sizeof(line)); linep = line + strlen(line); format_packet(p, len, pr_log, NULL); if (linep != line) @@ -1380,7 +1373,7 @@ log_packet(p, len, prefix, level) * format_packet - make a readable representation of a packet, * calling `printer(arg, format, ...)' to output it. */ -void +static void format_packet(p, len, printer, arg) u_char *p; int len; @@ -1389,7 +1382,6 @@ format_packet(p, len, printer, arg) { int i, n; u_short proto; - u_char x; struct protent *protp; if (len >= PPP_HDRLEN && p[0] == PPP_ALLSTATIONS && p[1] == PPP_UI) { @@ -1406,16 +1398,25 @@ format_packet(p, len, printer, arg) p += n; len -= n; } else { - printer(arg, "[proto=0x%x]", proto); + for (i = 0; (protp = protocols[i]) != NULL; ++i) + if (proto == (protp->protocol & ~0x8000)) + break; + if (protp != 0 && protp->data_name != 0) { + printer(arg, "[%s data]", protp->data_name); + if (len > 8) + printer(arg, "%.8B ...", p); + else + printer(arg, "%.*B", len, p); + len = 0; + } else + printer(arg, "[proto=0x%x]", proto); } } - for (i = 0; i < len && i < 32; ++i) { - GETCHAR(x, p); - printer(arg, " %.2x", x); - } - if (i < len) - printer(arg, " ..."); + if (len > 32) + printer(arg, "%.32B ...", p); + else + printer(arg, "%.*B", len, p); } static void @@ -1442,7 +1443,7 @@ pr_log __V((void *arg, char *fmt, ...)) syslog(LOG_DEBUG, "%s", line); linep = line; } - strlcpy(linep, line + sizeof(line) - linep, buf); + strlcpy(linep, buf, line + sizeof(line) - linep); linep += n; } @@ -1881,33 +1882,32 @@ script_unsetenv(var) * strlcpy - like strcpy/strncpy, doesn't overflow destination buffer, * always leaves destination null-terminated (for len > 0). */ -void -strlcpy(char *dest, size_t len, const char *src) +size_t +strlcpy(char *dest, const char *src, size_t len) { - if (len == 0) - return; - if (strlen(src) < len) - strcpy(dest, src); - else { - strncpy(dest, src, len - 1); - dest[len-1] = 0; + size_t ret = strlen(src); + + if (len != 0) { + if (ret < len) + strcpy(dest, src); + else { + strncpy(dest, src, len - 1); + dest[len-1] = 0; + } } + return ret; } /* * strlcat - like strcat/strncat, doesn't overflow destination buffer, * always leaves destination null-terminated (for len > 0). */ -void -strlcat(char *dest, size_t len, const char *src) +size_t +strlcat(char *dest, const char *src, size_t len) { - size_t dlen; + size_t dlen = strlen(dest); - if (len == 0) - return; - dlen = strlen(dest); - if (dlen < len - 1) - strlcpy(dest + dlen, len - dlen, src); + return dlen + strlcpy(dest + dlen, src, (len > dlen? len - dlen: 0)); } /* @@ -1921,14 +1921,14 @@ logit(level, fmt, args) { int n; char buf[256]; - static char nl = '\n'; n = vslprintf(buf, sizeof(buf), fmt, args); syslog(level, "%s", buf); - if (log_to_stderr) { - if (write(2, buf, n) != n - || (buf[n-1] != '\n' && write(2, &nl, 1) != 1)) - log_to_stderr = 0; + if (log_to_fd >= 0 && (level != LOG_DEBUG || debug)) { + if (buf[n-1] != '\n') + buf[n++] = '\n'; + if (write(log_to_fd, buf, n) != n) + log_to_fd = -1; } }