xref: /onnv-gate/usr/src/cmd/cmd-inet/usr.bin/pppd/utils.c (revision 4271:dda1ded496b7)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * utils.c - various utility functions used in pppd.
30Sstevel@tonic-gate  *
4*4271Srie  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
5*4271Srie  * Use is subject to license terms.
60Sstevel@tonic-gate  *
70Sstevel@tonic-gate  * Permission to use, copy, modify, and distribute this software and its
80Sstevel@tonic-gate  * documentation is hereby granted, provided that the above copyright
90Sstevel@tonic-gate  * notice appears in all copies.
100Sstevel@tonic-gate  *
110Sstevel@tonic-gate  * SUN MAKES NO REPRESENTATION OR WARRANTIES ABOUT THE SUITABILITY OF
120Sstevel@tonic-gate  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
130Sstevel@tonic-gate  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
140Sstevel@tonic-gate  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT.  SUN SHALL NOT BE LIABLE FOR
150Sstevel@tonic-gate  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
160Sstevel@tonic-gate  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES
170Sstevel@tonic-gate  *
180Sstevel@tonic-gate  * Copyright (c) 1999 The Australian National University.
190Sstevel@tonic-gate  * All rights reserved.
200Sstevel@tonic-gate  *
210Sstevel@tonic-gate  * Redistribution and use in source and binary forms are permitted
220Sstevel@tonic-gate  * provided that the above copyright notice and this paragraph are
230Sstevel@tonic-gate  * duplicated in all such forms and that any documentation,
240Sstevel@tonic-gate  * advertising materials, and other materials related to such
250Sstevel@tonic-gate  * distribution and use acknowledge that the software was developed
260Sstevel@tonic-gate  * by the Australian National University.  The name of the University
270Sstevel@tonic-gate  * may not be used to endorse or promote products derived from this
280Sstevel@tonic-gate  * software without specific prior written permission.
290Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
300Sstevel@tonic-gate  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
310Sstevel@tonic-gate  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
320Sstevel@tonic-gate  */
330Sstevel@tonic-gate 
340Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
350Sstevel@tonic-gate #define RCSID	"$Id: utils.c,v 1.10 2000/03/27 01:36:48 paulus Exp $"
360Sstevel@tonic-gate 
370Sstevel@tonic-gate #ifdef __linux__
380Sstevel@tonic-gate #define	_GNU_SOURCE
390Sstevel@tonic-gate #endif
400Sstevel@tonic-gate #include <stdio.h>
410Sstevel@tonic-gate #include <ctype.h>
420Sstevel@tonic-gate #include <stdlib.h>
430Sstevel@tonic-gate #include <string.h>
440Sstevel@tonic-gate #include <unistd.h>
450Sstevel@tonic-gate #include <signal.h>
460Sstevel@tonic-gate #include <errno.h>
470Sstevel@tonic-gate #include <fcntl.h>
480Sstevel@tonic-gate #include <syslog.h>
490Sstevel@tonic-gate #include <netdb.h>
500Sstevel@tonic-gate #include <utmp.h>
510Sstevel@tonic-gate #include <pwd.h>
520Sstevel@tonic-gate #include <sys/param.h>
530Sstevel@tonic-gate #include <sys/types.h>
540Sstevel@tonic-gate #include <sys/wait.h>
550Sstevel@tonic-gate #include <sys/time.h>
560Sstevel@tonic-gate #include <sys/resource.h>
570Sstevel@tonic-gate #include <sys/stat.h>
580Sstevel@tonic-gate #include <sys/socket.h>
590Sstevel@tonic-gate #include <netinet/in.h>
600Sstevel@tonic-gate #ifdef SVR4
610Sstevel@tonic-gate #include <sys/mkdev.h>
620Sstevel@tonic-gate #endif
630Sstevel@tonic-gate 
640Sstevel@tonic-gate #include "pppd.h"
650Sstevel@tonic-gate 
660Sstevel@tonic-gate #if !defined(lint) && !defined(_lint)
670Sstevel@tonic-gate static const char rcsid[] = RCSID;
680Sstevel@tonic-gate #endif
690Sstevel@tonic-gate 
700Sstevel@tonic-gate #if defined(SUNOS4)
710Sstevel@tonic-gate extern char *strerror();
720Sstevel@tonic-gate #endif
730Sstevel@tonic-gate 
740Sstevel@tonic-gate /* Don't log to stdout until we're sure it's ok to do so. */
750Sstevel@tonic-gate bool early_log = 1;
760Sstevel@tonic-gate 
770Sstevel@tonic-gate static void pr_log __P((void *, const char *, ...));
780Sstevel@tonic-gate static void logit __P((int, const char *, va_list));
790Sstevel@tonic-gate static void vslp_printer __P((void *, const char *, ...));
800Sstevel@tonic-gate static void format_packet __P((u_char *, int,
810Sstevel@tonic-gate     void (*) (void *, const char *, ...), void *));
820Sstevel@tonic-gate 
830Sstevel@tonic-gate struct buffer_info {
840Sstevel@tonic-gate     char *ptr;
850Sstevel@tonic-gate     int len;
860Sstevel@tonic-gate };
870Sstevel@tonic-gate 
880Sstevel@tonic-gate /*
890Sstevel@tonic-gate  * strllen - like strlen, but doesn't run past end of input.
900Sstevel@tonic-gate  */
910Sstevel@tonic-gate size_t
strllen(str,len)920Sstevel@tonic-gate strllen(str, len)
930Sstevel@tonic-gate     const char *str;
940Sstevel@tonic-gate     size_t len;
950Sstevel@tonic-gate {
960Sstevel@tonic-gate     size_t ret;
970Sstevel@tonic-gate 
980Sstevel@tonic-gate     for (ret = 0; ret < len; ret++)
990Sstevel@tonic-gate 	if (*str++ == '\0')
1000Sstevel@tonic-gate 	    break;
1010Sstevel@tonic-gate     return (ret);
1020Sstevel@tonic-gate }
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate /*
1050Sstevel@tonic-gate  * slprintf - format a message into a buffer.  Like sprintf except we
1060Sstevel@tonic-gate  * also specify the length of the output buffer, and we handle %m
1070Sstevel@tonic-gate  * (error message), %v (visible string), %q (quoted string), %t
1080Sstevel@tonic-gate  * (current time), %I (IP address), %P (PPP packet), and %B (sequence
1090Sstevel@tonic-gate  * of bytes) formats.  Doesn't do floating-point formats.  Returns the
1100Sstevel@tonic-gate  * number of chars put into buf.
1110Sstevel@tonic-gate  */
1120Sstevel@tonic-gate int
slprintf(char * buf,int buflen,const char * fmt,...)1130Sstevel@tonic-gate slprintf __V((char *buf, int buflen, const char *fmt, ...))
1140Sstevel@tonic-gate {
1150Sstevel@tonic-gate     va_list args;
1160Sstevel@tonic-gate     int n;
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate #if defined(__STDC__)
1190Sstevel@tonic-gate     va_start(args, fmt);
1200Sstevel@tonic-gate #else
1210Sstevel@tonic-gate     char *buf;
1220Sstevel@tonic-gate     int buflen;
1230Sstevel@tonic-gate     const char *fmt;
1240Sstevel@tonic-gate     va_start(args);
1250Sstevel@tonic-gate     buf = va_arg(args, char *);
1260Sstevel@tonic-gate     buflen = va_arg(args, int);
1270Sstevel@tonic-gate     fmt = va_arg(args, const char *);
1280Sstevel@tonic-gate #endif
1290Sstevel@tonic-gate     n = vslprintf(buf, buflen, fmt, args);
1300Sstevel@tonic-gate     va_end(args);
1310Sstevel@tonic-gate     return (n);
1320Sstevel@tonic-gate }
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate /*
1350Sstevel@tonic-gate  * Print to file or, if argument is NULL, to syslog at debug level.
1360Sstevel@tonic-gate  */
1370Sstevel@tonic-gate int
flprintf(FILE * strptr,const char * fmt,...)1380Sstevel@tonic-gate flprintf __V((FILE *strptr, const char *fmt, ...))
1390Sstevel@tonic-gate {
1400Sstevel@tonic-gate     va_list args;
1410Sstevel@tonic-gate     int n;
1420Sstevel@tonic-gate     char buf[1024], *bp, *nlp, *ebp;
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate #if defined(__STDC__)
1450Sstevel@tonic-gate     va_start(args, fmt);
1460Sstevel@tonic-gate #else
1470Sstevel@tonic-gate     FILE *strptr;
1480Sstevel@tonic-gate     const char *fmt;
1490Sstevel@tonic-gate     va_start(args);
1500Sstevel@tonic-gate     strptr = va_arg(args, FILE *);
1510Sstevel@tonic-gate     fmt = va_arg(args, const char *);
1520Sstevel@tonic-gate #endif
1530Sstevel@tonic-gate     n = vslprintf(buf, sizeof (buf), fmt, args);
1540Sstevel@tonic-gate     va_end(args);
1550Sstevel@tonic-gate     if (strptr == NULL) {
1560Sstevel@tonic-gate 	bp = buf;
1570Sstevel@tonic-gate 	ebp = buf + n;
1580Sstevel@tonic-gate 	while (bp < ebp) {
1590Sstevel@tonic-gate 	    if ((nlp = strchr(bp, '\n')) == NULL)
1600Sstevel@tonic-gate 		nlp = ebp;
1610Sstevel@tonic-gate 	    if (nlp > bp) {
1620Sstevel@tonic-gate 		*nlp = '\0';
1630Sstevel@tonic-gate 		syslog(LOG_DEBUG, "%s", bp);
1640Sstevel@tonic-gate 	    }
1650Sstevel@tonic-gate 	    bp = nlp + 1;
1660Sstevel@tonic-gate 	}
1670Sstevel@tonic-gate     } else {
1680Sstevel@tonic-gate 	n = fwrite(buf, 1, n, strptr);
1690Sstevel@tonic-gate     }
1700Sstevel@tonic-gate     return (n);
1710Sstevel@tonic-gate }
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate /*
1740Sstevel@tonic-gate  * vslprintf - like slprintf, takes a va_list instead of a list of args.
1750Sstevel@tonic-gate  */
1760Sstevel@tonic-gate #define OUTCHAR(c)	(buflen > 0? (--buflen, *buf++ = (c)): 0)
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate int
vslprintf(buf,buflen,fmt,args)1790Sstevel@tonic-gate vslprintf(buf, buflen, fmt, args)
1800Sstevel@tonic-gate     char *buf;
1810Sstevel@tonic-gate     int buflen;
1820Sstevel@tonic-gate     const char *fmt;
1830Sstevel@tonic-gate     va_list args;
1840Sstevel@tonic-gate {
1850Sstevel@tonic-gate     int c, n, longs;
1860Sstevel@tonic-gate     int width, prec, fillch;
1870Sstevel@tonic-gate     int base, len, neg, quoted;
1880Sstevel@tonic-gate #ifdef SOL2
1890Sstevel@tonic-gate     uint64_t val;
1900Sstevel@tonic-gate     int64_t sval;
1910Sstevel@tonic-gate #else
1920Sstevel@tonic-gate     unsigned long val;
1930Sstevel@tonic-gate     long sval;
1940Sstevel@tonic-gate #endif
1950Sstevel@tonic-gate     char *buf0, *mstr;
1960Sstevel@tonic-gate     const char *f, *str;
1970Sstevel@tonic-gate     unsigned char *p;
1980Sstevel@tonic-gate     char num[32];	/* 2^64 is 20 chars decimal, 22 octal */
1990Sstevel@tonic-gate     time_t t;
2000Sstevel@tonic-gate     u_int32_t ip;
2010Sstevel@tonic-gate     static const char hexchars[] = "0123456789abcdef";
2020Sstevel@tonic-gate     struct buffer_info bufinfo;
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate     buf0 = buf;
2050Sstevel@tonic-gate     --buflen;
2060Sstevel@tonic-gate     while (buflen > 0) {
2070Sstevel@tonic-gate 	for (f = fmt; *f != '%' && *f != 0; ++f)
2080Sstevel@tonic-gate 	    ;
2090Sstevel@tonic-gate 	if (f > fmt) {
2100Sstevel@tonic-gate 	    len = f - fmt;
2110Sstevel@tonic-gate 	    if (len > buflen)
2120Sstevel@tonic-gate 		len = buflen;
2130Sstevel@tonic-gate 	    (void) memcpy(buf, fmt, len);
2140Sstevel@tonic-gate 	    buf += len;
2150Sstevel@tonic-gate 	    buflen -= len;
2160Sstevel@tonic-gate 	    fmt = f;
2170Sstevel@tonic-gate 	}
2180Sstevel@tonic-gate 	if (*fmt == 0)
2190Sstevel@tonic-gate 	    break;
2200Sstevel@tonic-gate 	c = *++fmt;
2210Sstevel@tonic-gate 	width = 0;
2220Sstevel@tonic-gate 	prec = -1;
2230Sstevel@tonic-gate 	fillch = ' ';
2240Sstevel@tonic-gate 	if (c == '0') {
2250Sstevel@tonic-gate 	    fillch = '0';
2260Sstevel@tonic-gate 	    c = *++fmt;
2270Sstevel@tonic-gate 	}
2280Sstevel@tonic-gate 	if (c == '*') {
2290Sstevel@tonic-gate 	    width = va_arg(args, int);
2300Sstevel@tonic-gate 	    c = *++fmt;
2310Sstevel@tonic-gate 	} else {
2320Sstevel@tonic-gate 	    while (isdigit(c)) {
2330Sstevel@tonic-gate 		width = width * 10 + c - '0';
2340Sstevel@tonic-gate 		c = *++fmt;
2350Sstevel@tonic-gate 	    }
2360Sstevel@tonic-gate 	}
2370Sstevel@tonic-gate 	if (c == '.') {
2380Sstevel@tonic-gate 	    c = *++fmt;
2390Sstevel@tonic-gate 	    if (c == '*') {
2400Sstevel@tonic-gate 		prec = va_arg(args, int);
2410Sstevel@tonic-gate 		c = *++fmt;
2420Sstevel@tonic-gate 	    } else {
2430Sstevel@tonic-gate 		prec = 0;
2440Sstevel@tonic-gate 		while (isdigit(c)) {
2450Sstevel@tonic-gate 		    prec = prec * 10 + c - '0';
2460Sstevel@tonic-gate 		    c = *++fmt;
2470Sstevel@tonic-gate 		}
2480Sstevel@tonic-gate 	    }
2490Sstevel@tonic-gate 	}
2500Sstevel@tonic-gate 	longs = 0;
2510Sstevel@tonic-gate 	if (c == 'l') {
2520Sstevel@tonic-gate 	    longs++;
2530Sstevel@tonic-gate 	    c = *++fmt;
2540Sstevel@tonic-gate 	    if (c == 'l') {
2550Sstevel@tonic-gate 		longs++;
2560Sstevel@tonic-gate 		c = *++fmt;
2570Sstevel@tonic-gate 	    }
2580Sstevel@tonic-gate 	}
2590Sstevel@tonic-gate 	str = 0;
2600Sstevel@tonic-gate 	base = 0;
2610Sstevel@tonic-gate 	neg = 0;
2620Sstevel@tonic-gate 	val = 0;
2630Sstevel@tonic-gate 	++fmt;
2640Sstevel@tonic-gate 	switch (c) {
2650Sstevel@tonic-gate 	case 'u':
2660Sstevel@tonic-gate #ifdef SOL2
2670Sstevel@tonic-gate 	    if (longs >= 2)
2680Sstevel@tonic-gate 		val = va_arg(args, uint64_t);
2690Sstevel@tonic-gate 	    else
2700Sstevel@tonic-gate #endif
2710Sstevel@tonic-gate 	    if (longs > 0)
2720Sstevel@tonic-gate 		val = va_arg(args, unsigned long);
2730Sstevel@tonic-gate 	    else
2740Sstevel@tonic-gate 		val = va_arg(args, unsigned int);
2750Sstevel@tonic-gate 	    base = 10;
2760Sstevel@tonic-gate 	    break;
2770Sstevel@tonic-gate 	case 'd':
2780Sstevel@tonic-gate #ifdef SOL2
2790Sstevel@tonic-gate 	    if (longs >= 2)
2800Sstevel@tonic-gate 		sval = va_arg(args, int64_t);
2810Sstevel@tonic-gate 	    else
2820Sstevel@tonic-gate #endif
2830Sstevel@tonic-gate 	    if (longs > 0)
2840Sstevel@tonic-gate 		sval = va_arg(args, long);
2850Sstevel@tonic-gate 	    else
2860Sstevel@tonic-gate 		sval = va_arg(args, int);
2870Sstevel@tonic-gate 	    if (sval < 0) {
2880Sstevel@tonic-gate 		neg = 1;
2890Sstevel@tonic-gate 		val = -sval;
2900Sstevel@tonic-gate 	    } else
2910Sstevel@tonic-gate 		val = sval;
2920Sstevel@tonic-gate 	    base = 10;
2930Sstevel@tonic-gate 	    break;
2940Sstevel@tonic-gate 	case 'o':
2950Sstevel@tonic-gate #ifdef SOL2
2960Sstevel@tonic-gate 	    if (longs >= 2)
2970Sstevel@tonic-gate 		val = va_arg(args, uint64_t);
2980Sstevel@tonic-gate 	    else
2990Sstevel@tonic-gate #endif
3000Sstevel@tonic-gate 	    if (longs > 0)
3010Sstevel@tonic-gate 		val = va_arg(args, unsigned long);
3020Sstevel@tonic-gate 	    else
3030Sstevel@tonic-gate 		val = va_arg(args, unsigned int);
3040Sstevel@tonic-gate 	    base = 8;
3050Sstevel@tonic-gate 	    break;
3060Sstevel@tonic-gate 	case 'x':
3070Sstevel@tonic-gate 	case 'X':
3080Sstevel@tonic-gate #ifdef SOL2
3090Sstevel@tonic-gate 	    if (longs >= 2)
3100Sstevel@tonic-gate 		val = va_arg(args, uint64_t);
3110Sstevel@tonic-gate 	    else
3120Sstevel@tonic-gate #endif
3130Sstevel@tonic-gate 	    if (longs > 0)
3140Sstevel@tonic-gate 		val = va_arg(args, unsigned long);
3150Sstevel@tonic-gate 	    else
3160Sstevel@tonic-gate 		val = va_arg(args, unsigned int);
3170Sstevel@tonic-gate 	    base = 16;
3180Sstevel@tonic-gate 	    break;
3190Sstevel@tonic-gate 	case 'p':
3200Sstevel@tonic-gate 	    val = (unsigned long) va_arg(args, void *);
3210Sstevel@tonic-gate 	    base = 16;
3220Sstevel@tonic-gate 	    neg = 2;
3230Sstevel@tonic-gate 	    break;
3240Sstevel@tonic-gate 	case 's':
3250Sstevel@tonic-gate 	    str = va_arg(args, const char *);
3260Sstevel@tonic-gate 	    break;
3270Sstevel@tonic-gate 	case 'c':
3280Sstevel@tonic-gate 	    num[0] = va_arg(args, int);
3290Sstevel@tonic-gate 	    num[1] = 0;
3300Sstevel@tonic-gate 	    str = num;
3310Sstevel@tonic-gate 	    break;
3320Sstevel@tonic-gate 	case 'm':
3330Sstevel@tonic-gate 	    str = strerror(errno);
3340Sstevel@tonic-gate 	    break;
3350Sstevel@tonic-gate 	case 'I':
3360Sstevel@tonic-gate 	    ip = va_arg(args, u_int32_t);
3370Sstevel@tonic-gate 	    ip = ntohl(ip);
3380Sstevel@tonic-gate 	    (void) slprintf(num, sizeof(num), "%d.%d.%d.%d", (ip >> 24) & 0xff,
3390Sstevel@tonic-gate 		(ip >> 16) & 0xff, (ip >> 8) & 0xff, ip & 0xff);
3400Sstevel@tonic-gate 	    str = num;
3410Sstevel@tonic-gate 	    break;
3420Sstevel@tonic-gate 	case 't':
3430Sstevel@tonic-gate 	    (void) time(&t);
3440Sstevel@tonic-gate 	    mstr = ctime(&t);
3450Sstevel@tonic-gate 	    mstr += 4;		/* chop off the day name */
3460Sstevel@tonic-gate 	    mstr[15] = 0;	/* chop off year and newline */
3470Sstevel@tonic-gate 	    str = (const char *)mstr;
3480Sstevel@tonic-gate 	    break;
3490Sstevel@tonic-gate 	case 'v':		/* "visible" string */
3500Sstevel@tonic-gate 	case 'q':		/* quoted string */
3510Sstevel@tonic-gate 	    quoted = c == 'q';
3520Sstevel@tonic-gate 	    p = va_arg(args, unsigned char *);
3530Sstevel@tonic-gate 	    if (fillch == '0' && prec >= 0) {
3540Sstevel@tonic-gate 		n = prec;
3550Sstevel@tonic-gate 	    } else {
3560Sstevel@tonic-gate 		n = strlen((char *)p);
3570Sstevel@tonic-gate 		if (prec >= 0 && n > prec)
3580Sstevel@tonic-gate 		    n = prec;
3590Sstevel@tonic-gate 	    }
3600Sstevel@tonic-gate 	    while (n > 0 && buflen > 0) {
3610Sstevel@tonic-gate 		c = *p++;
3620Sstevel@tonic-gate 		--n;
3630Sstevel@tonic-gate 		if (!quoted && c >= 0x80) {
3640Sstevel@tonic-gate 		    (void) OUTCHAR('M');
3650Sstevel@tonic-gate 		    (void) OUTCHAR('-');
3660Sstevel@tonic-gate 		    c -= 0x80;
3670Sstevel@tonic-gate 		}
3680Sstevel@tonic-gate 		if (quoted && (c == '"' || c == '\\'))
3690Sstevel@tonic-gate 		    (void) OUTCHAR('\\');
3700Sstevel@tonic-gate 		if (c < 0x20 || (0x7f <= c && c < 0xa0)) {
3710Sstevel@tonic-gate 		    if (quoted) {
3720Sstevel@tonic-gate 			(void) OUTCHAR('\\');
3730Sstevel@tonic-gate 			switch (c) {
3740Sstevel@tonic-gate 			case '\t':	(void) OUTCHAR('t');	break;
3750Sstevel@tonic-gate 			case '\n':	(void) OUTCHAR('n');	break;
3760Sstevel@tonic-gate 			case '\b':	(void) OUTCHAR('b');	break;
3770Sstevel@tonic-gate 			case '\f':	(void) OUTCHAR('f');	break;
3780Sstevel@tonic-gate 			default:
3790Sstevel@tonic-gate 			    (void) OUTCHAR('x');
3800Sstevel@tonic-gate 			    (void) OUTCHAR(hexchars[c >> 4]);
3810Sstevel@tonic-gate 			    (void) OUTCHAR(hexchars[c & 0xf]);
3820Sstevel@tonic-gate 			}
3830Sstevel@tonic-gate 		    } else {
3840Sstevel@tonic-gate 			if (c == '\t')
3850Sstevel@tonic-gate 			    (void) OUTCHAR(c);
3860Sstevel@tonic-gate 			else {
3870Sstevel@tonic-gate 			    (void) OUTCHAR('^');
3880Sstevel@tonic-gate 			    (void) OUTCHAR(c ^ 0x40);
3890Sstevel@tonic-gate 			}
3900Sstevel@tonic-gate 		    }
3910Sstevel@tonic-gate 		} else
3920Sstevel@tonic-gate 		    (void) OUTCHAR(c);
3930Sstevel@tonic-gate 	    }
3940Sstevel@tonic-gate 	    continue;
3950Sstevel@tonic-gate 	case 'P':		/* print PPP packet */
3960Sstevel@tonic-gate 	    bufinfo.ptr = buf;
3970Sstevel@tonic-gate 	    bufinfo.len = buflen + 1;
3980Sstevel@tonic-gate 	    p = va_arg(args, unsigned char *);
3990Sstevel@tonic-gate 	    n = va_arg(args, int);
4000Sstevel@tonic-gate 	    format_packet(p, n, vslp_printer, &bufinfo);
4010Sstevel@tonic-gate 	    buf = bufinfo.ptr;
4020Sstevel@tonic-gate 	    buflen = bufinfo.len - 1;
4030Sstevel@tonic-gate 	    continue;
4040Sstevel@tonic-gate 	case 'B':
4050Sstevel@tonic-gate 	    p = va_arg(args, unsigned char *);
4060Sstevel@tonic-gate 	    if ((n = prec) > width && width > 0)
4070Sstevel@tonic-gate 		n = width;
4080Sstevel@tonic-gate 	    /* For safety's sake */
4090Sstevel@tonic-gate 	    if (n > 2000)
4100Sstevel@tonic-gate 		    n = 2000;
4110Sstevel@tonic-gate 	    while (--n >= 0) {
4120Sstevel@tonic-gate 		c = *p++;
4130Sstevel@tonic-gate 		if (fillch == ' ')
4140Sstevel@tonic-gate 		    (void) OUTCHAR(' ');
4150Sstevel@tonic-gate 		(void) OUTCHAR(hexchars[(c >> 4) & 0xf]);
4160Sstevel@tonic-gate 		(void) OUTCHAR(hexchars[c & 0xf]);
4170Sstevel@tonic-gate 	    }
4180Sstevel@tonic-gate 	    if (prec > width && width > 0) {
4190Sstevel@tonic-gate 		(void) OUTCHAR('.');
4200Sstevel@tonic-gate 		(void) OUTCHAR('.');
4210Sstevel@tonic-gate 		(void) OUTCHAR('.');
4220Sstevel@tonic-gate 	    }
4230Sstevel@tonic-gate 	    continue;
4240Sstevel@tonic-gate 	default:
4250Sstevel@tonic-gate 	    *buf++ = '%';
4260Sstevel@tonic-gate 	    if (c != '%')
4270Sstevel@tonic-gate 		--fmt;		/* so %z outputs %z etc. */
4280Sstevel@tonic-gate 	    --buflen;
4290Sstevel@tonic-gate 	    continue;
4300Sstevel@tonic-gate 	}
4310Sstevel@tonic-gate 	if (base != 0) {
4320Sstevel@tonic-gate 	    mstr = num + sizeof(num);
4330Sstevel@tonic-gate 	    *--mstr = 0;
4340Sstevel@tonic-gate 	    while (mstr > num + neg) {
4350Sstevel@tonic-gate 		*--mstr = hexchars[val % base];
4360Sstevel@tonic-gate 		val = val / base;
4370Sstevel@tonic-gate 		if (--prec <= 0 && val == 0)
4380Sstevel@tonic-gate 		    break;
4390Sstevel@tonic-gate 	    }
4400Sstevel@tonic-gate 	    switch (neg) {
4410Sstevel@tonic-gate 	    case 1:
4420Sstevel@tonic-gate 		*--mstr = '-';
4430Sstevel@tonic-gate 		break;
4440Sstevel@tonic-gate 	    case 2:
4450Sstevel@tonic-gate 		*--mstr = 'x';
4460Sstevel@tonic-gate 		*--mstr = '0';
4470Sstevel@tonic-gate 		break;
4480Sstevel@tonic-gate 	    }
4490Sstevel@tonic-gate 	    len = num + sizeof(num) - 1 - mstr;
4500Sstevel@tonic-gate 	    str = (const char *)mstr;
4510Sstevel@tonic-gate 	} else {
4520Sstevel@tonic-gate 	    len = strlen(str);
4530Sstevel@tonic-gate 	    if (prec >= 0 && len > prec)
4540Sstevel@tonic-gate 		len = prec;
4550Sstevel@tonic-gate 	}
4560Sstevel@tonic-gate 	if (width > 0) {
4570Sstevel@tonic-gate 	    if (width > buflen)
4580Sstevel@tonic-gate 		width = buflen;
4590Sstevel@tonic-gate 	    if ((n = width - len) > 0) {
4600Sstevel@tonic-gate 		buflen -= n;
4610Sstevel@tonic-gate 		for (; n > 0; --n)
4620Sstevel@tonic-gate 		    *buf++ = fillch;
4630Sstevel@tonic-gate 	    }
4640Sstevel@tonic-gate 	}
4650Sstevel@tonic-gate 	if (len > buflen)
4660Sstevel@tonic-gate 	    len = buflen;
4670Sstevel@tonic-gate 	(void) memcpy(buf, str, len);
4680Sstevel@tonic-gate 	buf += len;
4690Sstevel@tonic-gate 	buflen -= len;
4700Sstevel@tonic-gate     }
4710Sstevel@tonic-gate     *buf = 0;
4720Sstevel@tonic-gate     return (buf - buf0);
4730Sstevel@tonic-gate }
4740Sstevel@tonic-gate 
4750Sstevel@tonic-gate /*
4760Sstevel@tonic-gate  * vslp_printer - used in processing a %P format
4770Sstevel@tonic-gate  */
4780Sstevel@tonic-gate static void
vslp_printer(void * arg,const char * fmt,...)4790Sstevel@tonic-gate vslp_printer __V((void *arg, const char *fmt, ...))
4800Sstevel@tonic-gate {
4810Sstevel@tonic-gate     int n;
4820Sstevel@tonic-gate     va_list pvar;
4830Sstevel@tonic-gate     struct buffer_info *bi;
4840Sstevel@tonic-gate 
4850Sstevel@tonic-gate #if defined(__STDC__)
4860Sstevel@tonic-gate     va_start(pvar, fmt);
4870Sstevel@tonic-gate #else
4880Sstevel@tonic-gate     void *arg;
4890Sstevel@tonic-gate     const char *fmt;
4900Sstevel@tonic-gate     va_start(pvar);
4910Sstevel@tonic-gate     arg = va_arg(pvar, void *);
4920Sstevel@tonic-gate     fmt = va_arg(pvar, const char *);
4930Sstevel@tonic-gate #endif
4940Sstevel@tonic-gate 
4950Sstevel@tonic-gate     bi = (struct buffer_info *) arg;
4960Sstevel@tonic-gate     n = vslprintf(bi->ptr, bi->len, fmt, pvar);
4970Sstevel@tonic-gate     va_end(pvar);
4980Sstevel@tonic-gate 
4990Sstevel@tonic-gate     bi->ptr += n;
5000Sstevel@tonic-gate     bi->len -= n;
5010Sstevel@tonic-gate }
5020Sstevel@tonic-gate 
5030Sstevel@tonic-gate /*
5040Sstevel@tonic-gate  * log_packet - format a packet and log it.
5050Sstevel@tonic-gate  */
5060Sstevel@tonic-gate 
5070Sstevel@tonic-gate static char line[256];		/* line to be logged accumulated here */
5080Sstevel@tonic-gate static char *linep;
5090Sstevel@tonic-gate 
5100Sstevel@tonic-gate void
log_packet(p,len,prefix,level)5110Sstevel@tonic-gate log_packet(p, len, prefix, level)
5120Sstevel@tonic-gate     u_char *p;
5130Sstevel@tonic-gate     int len;
5140Sstevel@tonic-gate     const char *prefix;
5150Sstevel@tonic-gate     int level;
5160Sstevel@tonic-gate {
5170Sstevel@tonic-gate     (void) strlcpy(line, prefix, sizeof(line));
5180Sstevel@tonic-gate     linep = line + strlen(line);
5190Sstevel@tonic-gate     format_packet(p, len, pr_log, (void *)level);
5200Sstevel@tonic-gate     if (linep != line)
5210Sstevel@tonic-gate 	syslog(level, "%s", line);
5220Sstevel@tonic-gate }
5230Sstevel@tonic-gate 
5240Sstevel@tonic-gate /*
5250Sstevel@tonic-gate  * format_packet - make a readable representation of a packet,
5260Sstevel@tonic-gate  * calling `printer(arg, format, ...)' to output it.
5270Sstevel@tonic-gate  */
5280Sstevel@tonic-gate static void
format_packet(p,len,printer,arg)5290Sstevel@tonic-gate format_packet(p, len, printer, arg)
5300Sstevel@tonic-gate     u_char *p;
5310Sstevel@tonic-gate     int len;
5320Sstevel@tonic-gate     void (*printer) __P((void *, const char *, ...));
5330Sstevel@tonic-gate     void *arg;
5340Sstevel@tonic-gate {
5350Sstevel@tonic-gate     int i, n;
5360Sstevel@tonic-gate     u_short proto;
5370Sstevel@tonic-gate     struct protent *protp;
5380Sstevel@tonic-gate 
5390Sstevel@tonic-gate     if (len >= PPP_HDRLEN && p[0] == PPP_ALLSTATIONS && p[1] == PPP_UI) {
5400Sstevel@tonic-gate 	p += 2;
5410Sstevel@tonic-gate 	GETSHORT(proto, p);
5420Sstevel@tonic-gate 	len -= PPP_HDRLEN;
5430Sstevel@tonic-gate 	for (i = 0; (protp = protocols[i]) != NULL; ++i)
5440Sstevel@tonic-gate 	    if (proto == protp->protocol)
5450Sstevel@tonic-gate 		break;
5460Sstevel@tonic-gate 	if (protp != NULL) {
5470Sstevel@tonic-gate 	    printer(arg, "[%s", protp->name);
5480Sstevel@tonic-gate 	    n = (*protp->printpkt)(p, len, printer, arg);
5490Sstevel@tonic-gate 	    printer(arg, "]");
5500Sstevel@tonic-gate 	    p += n;
5510Sstevel@tonic-gate 	    len -= n;
5520Sstevel@tonic-gate 	} else {
5530Sstevel@tonic-gate 	    for (i = 0; (protp = protocols[i]) != NULL; ++i)
5540Sstevel@tonic-gate 		if (proto == (protp->protocol & ~0x8000))
5550Sstevel@tonic-gate 		    break;
5560Sstevel@tonic-gate 	    if (protp != NULL && protp->data_name != NULL) {
5570Sstevel@tonic-gate 		printer(arg, "[%s data] %8.*B", protp->data_name, len, p);
5580Sstevel@tonic-gate 		len = 0;
5590Sstevel@tonic-gate 	    } else
5600Sstevel@tonic-gate 		printer(arg, "[proto=0x%x]", proto);
5610Sstevel@tonic-gate 	}
5620Sstevel@tonic-gate     }
5630Sstevel@tonic-gate 
5640Sstevel@tonic-gate     printer(arg, "%32.*B", len, p);
5650Sstevel@tonic-gate }
5660Sstevel@tonic-gate 
5670Sstevel@tonic-gate static void
pr_log(void * arg,const char * fmt,...)5680Sstevel@tonic-gate pr_log __V((void *arg, const char *fmt, ...))
5690Sstevel@tonic-gate {
5700Sstevel@tonic-gate     int n;
5710Sstevel@tonic-gate     va_list pvar;
5720Sstevel@tonic-gate     char buf[256];
5730Sstevel@tonic-gate 
5740Sstevel@tonic-gate #if defined(__STDC__)
5750Sstevel@tonic-gate     va_start(pvar, fmt);
5760Sstevel@tonic-gate #else
5770Sstevel@tonic-gate     void *arg;
5780Sstevel@tonic-gate     const char *fmt;
5790Sstevel@tonic-gate     va_start(pvar);
5800Sstevel@tonic-gate     arg = va_arg(pvar, void *);
5810Sstevel@tonic-gate     fmt = va_arg(pvar, const char *);
5820Sstevel@tonic-gate #endif
5830Sstevel@tonic-gate 
5840Sstevel@tonic-gate     n = vslprintf(buf, sizeof(buf), fmt, pvar);
5850Sstevel@tonic-gate     va_end(pvar);
5860Sstevel@tonic-gate 
5870Sstevel@tonic-gate     if (linep + n + 1 > line + sizeof(line)) {
5880Sstevel@tonic-gate 	syslog((int)arg, "%s", line);
5890Sstevel@tonic-gate 	linep = line;
5900Sstevel@tonic-gate     }
5910Sstevel@tonic-gate     (void) strlcpy(linep, buf, line + sizeof(line) - linep);
5920Sstevel@tonic-gate     linep += n;
5930Sstevel@tonic-gate }
5940Sstevel@tonic-gate 
5950Sstevel@tonic-gate /*
5960Sstevel@tonic-gate  * print_string - print a readable representation of a string using
5970Sstevel@tonic-gate  * printer.
5980Sstevel@tonic-gate  */
5990Sstevel@tonic-gate void
print_string(p,len,printer,arg)6000Sstevel@tonic-gate print_string(p, len, printer, arg)
6010Sstevel@tonic-gate     char *p;
6020Sstevel@tonic-gate     int len;
6030Sstevel@tonic-gate     void (*printer) __P((void *, const char *, ...));
6040Sstevel@tonic-gate     void *arg;
6050Sstevel@tonic-gate {
6060Sstevel@tonic-gate     int c;
6070Sstevel@tonic-gate 
6080Sstevel@tonic-gate     printer(arg, "\"");
6090Sstevel@tonic-gate     for (; len > 0; --len) {
6100Sstevel@tonic-gate 	c = *p++;
6110Sstevel@tonic-gate 	if (isprint(c)) {
6120Sstevel@tonic-gate 	    if (c == '\\' || c == '"')
6130Sstevel@tonic-gate 		printer(arg, "\\");
6140Sstevel@tonic-gate 	    printer(arg, "%c", c);
6150Sstevel@tonic-gate 	} else {
6160Sstevel@tonic-gate 	    switch (c) {
6170Sstevel@tonic-gate 	    case '\n':
6180Sstevel@tonic-gate 		printer(arg, "\\n");
6190Sstevel@tonic-gate 		break;
6200Sstevel@tonic-gate 	    case '\r':
6210Sstevel@tonic-gate 		printer(arg, "\\r");
6220Sstevel@tonic-gate 		break;
6230Sstevel@tonic-gate 	    case '\t':
6240Sstevel@tonic-gate 		printer(arg, "\\t");
6250Sstevel@tonic-gate 		break;
6260Sstevel@tonic-gate 	    default:
6270Sstevel@tonic-gate 		printer(arg, "\\%.3o", c);
6280Sstevel@tonic-gate 	    }
6290Sstevel@tonic-gate 	}
6300Sstevel@tonic-gate     }
6310Sstevel@tonic-gate     printer(arg, "\"");
6320Sstevel@tonic-gate }
6330Sstevel@tonic-gate 
6340Sstevel@tonic-gate /*
6350Sstevel@tonic-gate  * logit - does the hard work for fatal et al.
6360Sstevel@tonic-gate  */
6370Sstevel@tonic-gate static void
logit(level,fmt,args)6380Sstevel@tonic-gate logit(level, fmt, args)
6390Sstevel@tonic-gate     int level;
6400Sstevel@tonic-gate     const char *fmt;
6410Sstevel@tonic-gate     va_list args;
6420Sstevel@tonic-gate {
6430Sstevel@tonic-gate     int n;
6440Sstevel@tonic-gate     char buf[1024];
6450Sstevel@tonic-gate 
6460Sstevel@tonic-gate     n = vslprintf(buf, sizeof(buf), fmt, args);
6470Sstevel@tonic-gate     syslog(level, "%s", buf);
6480Sstevel@tonic-gate     if (log_to_fd >= 0 && (level != LOG_DEBUG || debug) &&
6490Sstevel@tonic-gate 	(!early_log || log_to_specific_fd)) {
6500Sstevel@tonic-gate 	if (buf[n-1] != '\n')
6510Sstevel@tonic-gate 	    buf[n++] = '\n';
6520Sstevel@tonic-gate 	if (write(log_to_fd, buf, n) != n)
6530Sstevel@tonic-gate 	    log_to_fd = -1;
6540Sstevel@tonic-gate     }
6550Sstevel@tonic-gate }
6560Sstevel@tonic-gate 
6570Sstevel@tonic-gate /*
6580Sstevel@tonic-gate  * fatal - log an error message and die horribly.
6590Sstevel@tonic-gate  */
6600Sstevel@tonic-gate void
fatal(const char * fmt,...)6610Sstevel@tonic-gate fatal __V((const char *fmt, ...))
6620Sstevel@tonic-gate {
6630Sstevel@tonic-gate     va_list pvar;
6640Sstevel@tonic-gate 
6650Sstevel@tonic-gate #if defined(__STDC__)
6660Sstevel@tonic-gate     va_start(pvar, fmt);
6670Sstevel@tonic-gate #else
6680Sstevel@tonic-gate     const char *fmt;
6690Sstevel@tonic-gate     va_start(pvar);
6700Sstevel@tonic-gate     fmt = va_arg(pvar, const char *);
6710Sstevel@tonic-gate #endif
6720Sstevel@tonic-gate 
6730Sstevel@tonic-gate     logit(LOG_ERR, fmt, pvar);
6740Sstevel@tonic-gate     va_end(pvar);
6750Sstevel@tonic-gate 
6760Sstevel@tonic-gate     die(1);			/* as promised */
6770Sstevel@tonic-gate }
6780Sstevel@tonic-gate 
6790Sstevel@tonic-gate /*
6800Sstevel@tonic-gate  * error - log an error message.
6810Sstevel@tonic-gate  */
6820Sstevel@tonic-gate void
error(const char * fmt,...)6830Sstevel@tonic-gate error __V((const char *fmt, ...))
6840Sstevel@tonic-gate {
6850Sstevel@tonic-gate     va_list pvar;
6860Sstevel@tonic-gate 
6870Sstevel@tonic-gate #if defined(__STDC__)
6880Sstevel@tonic-gate     va_start(pvar, fmt);
6890Sstevel@tonic-gate #else
6900Sstevel@tonic-gate     const char *fmt;
6910Sstevel@tonic-gate     va_start(pvar);
6920Sstevel@tonic-gate     fmt = va_arg(pvar, const char *);
6930Sstevel@tonic-gate #endif
6940Sstevel@tonic-gate 
6950Sstevel@tonic-gate     logit(LOG_ERR, fmt, pvar);
6960Sstevel@tonic-gate     va_end(pvar);
6970Sstevel@tonic-gate }
6980Sstevel@tonic-gate 
6990Sstevel@tonic-gate /*
7000Sstevel@tonic-gate  * warn - log a warning message.
7010Sstevel@tonic-gate  */
7020Sstevel@tonic-gate void
warn(const char * fmt,...)7030Sstevel@tonic-gate warn __V((const char *fmt, ...))
7040Sstevel@tonic-gate {
7050Sstevel@tonic-gate     va_list pvar;
7060Sstevel@tonic-gate 
7070Sstevel@tonic-gate #if defined(__STDC__)
7080Sstevel@tonic-gate     va_start(pvar, fmt);
7090Sstevel@tonic-gate #else
7100Sstevel@tonic-gate     const char *fmt;
7110Sstevel@tonic-gate     va_start(pvar);
7120Sstevel@tonic-gate     fmt = va_arg(pvar, const char *);
7130Sstevel@tonic-gate #endif
7140Sstevel@tonic-gate 
7150Sstevel@tonic-gate     logit(LOG_WARNING, fmt, pvar);
7160Sstevel@tonic-gate     va_end(pvar);
7170Sstevel@tonic-gate }
7180Sstevel@tonic-gate 
7190Sstevel@tonic-gate /*
7200Sstevel@tonic-gate  * notice - log a notice-level message.
7210Sstevel@tonic-gate  */
7220Sstevel@tonic-gate void
notice(const char * fmt,...)7230Sstevel@tonic-gate notice __V((const char *fmt, ...))
7240Sstevel@tonic-gate {
7250Sstevel@tonic-gate     va_list pvar;
7260Sstevel@tonic-gate 
7270Sstevel@tonic-gate #if defined(__STDC__)
7280Sstevel@tonic-gate     va_start(pvar, fmt);
7290Sstevel@tonic-gate #else
7300Sstevel@tonic-gate     const char *fmt;
7310Sstevel@tonic-gate     va_start(pvar);
7320Sstevel@tonic-gate     fmt = va_arg(pvar, const char *);
7330Sstevel@tonic-gate #endif
7340Sstevel@tonic-gate 
7350Sstevel@tonic-gate     logit(LOG_NOTICE, fmt, pvar);
7360Sstevel@tonic-gate     va_end(pvar);
7370Sstevel@tonic-gate }
7380Sstevel@tonic-gate 
7390Sstevel@tonic-gate /*
7400Sstevel@tonic-gate  * info - log an informational message.
7410Sstevel@tonic-gate  */
7420Sstevel@tonic-gate void
info(const char * fmt,...)7430Sstevel@tonic-gate info __V((const char *fmt, ...))
7440Sstevel@tonic-gate {
7450Sstevel@tonic-gate     va_list pvar;
7460Sstevel@tonic-gate 
7470Sstevel@tonic-gate #if defined(__STDC__)
7480Sstevel@tonic-gate     va_start(pvar, fmt);
7490Sstevel@tonic-gate #else
7500Sstevel@tonic-gate     const char *fmt;
7510Sstevel@tonic-gate     va_start(pvar);
7520Sstevel@tonic-gate     fmt = va_arg(pvar, const char *);
7530Sstevel@tonic-gate #endif
7540Sstevel@tonic-gate 
7550Sstevel@tonic-gate     logit(LOG_INFO, fmt, pvar);
7560Sstevel@tonic-gate     va_end(pvar);
7570Sstevel@tonic-gate }
7580Sstevel@tonic-gate 
7590Sstevel@tonic-gate /*
7600Sstevel@tonic-gate  * dbglog - log a debug message.
7610Sstevel@tonic-gate  */
7620Sstevel@tonic-gate void
dbglog(const char * fmt,...)7630Sstevel@tonic-gate dbglog __V((const char *fmt, ...))
7640Sstevel@tonic-gate {
7650Sstevel@tonic-gate     va_list pvar;
7660Sstevel@tonic-gate 
7670Sstevel@tonic-gate #if defined(__STDC__)
7680Sstevel@tonic-gate     va_start(pvar, fmt);
7690Sstevel@tonic-gate #else
7700Sstevel@tonic-gate     const char *fmt;
7710Sstevel@tonic-gate     va_start(pvar);
7720Sstevel@tonic-gate     fmt = va_arg(pvar, const char *);
7730Sstevel@tonic-gate #endif
7740Sstevel@tonic-gate 
7750Sstevel@tonic-gate     logit(LOG_DEBUG, fmt, pvar);
7760Sstevel@tonic-gate     va_end(pvar);
7770Sstevel@tonic-gate }
7780Sstevel@tonic-gate 
7790Sstevel@tonic-gate /*
7800Sstevel@tonic-gate  * Code names for regular PPP messages.  Used by LCP and most NCPs,
7810Sstevel@tonic-gate  * not used by authentication protocols.
7820Sstevel@tonic-gate  */
7830Sstevel@tonic-gate const char *
code_name(int code,int shortflag)7840Sstevel@tonic-gate code_name(int code, int shortflag)
7850Sstevel@tonic-gate {
7860Sstevel@tonic-gate     static const char *codelist[] = {
7870Sstevel@tonic-gate 	"Vendor-Extension", "Configure-Request", "Configure-Ack",
7880Sstevel@tonic-gate 	"Configure-Nak", "Configure-Reject", "Terminate-Request",
7890Sstevel@tonic-gate 	"Terminate-Ack", "Code-Reject", "Protocol-Reject",
7900Sstevel@tonic-gate 	"Echo-Request", "Echo-Reply", "Discard-Request",
7910Sstevel@tonic-gate 	"Identification", "Time-Remaining",
7920Sstevel@tonic-gate 	"Reset-Request", "Reset-Ack"
7930Sstevel@tonic-gate     };
7940Sstevel@tonic-gate     static const char *shortcode[] = {
7950Sstevel@tonic-gate 	"VendExt", "ConfReq", "ConfAck",
7960Sstevel@tonic-gate 	"ConfNak", "ConfRej", "TermReq",
7970Sstevel@tonic-gate 	"TermAck", "CodeRej", "ProtRej",
7980Sstevel@tonic-gate 	"EchoReq", "EchoRep", "DiscReq",
7990Sstevel@tonic-gate 	"Ident", "TimeRem",
8000Sstevel@tonic-gate 	"ResetReq", "ResetAck"
8010Sstevel@tonic-gate     };
8020Sstevel@tonic-gate     static char msgbuf[64];
8030Sstevel@tonic-gate 
8040Sstevel@tonic-gate     if (code < 0 || code >= sizeof (codelist) / sizeof (*codelist)) {
8050Sstevel@tonic-gate 	if (shortflag)
8060Sstevel@tonic-gate 	    (void) slprintf(msgbuf, sizeof (msgbuf), "Code#%d", code);
8070Sstevel@tonic-gate 	else
8080Sstevel@tonic-gate 	    (void) slprintf(msgbuf, sizeof (msgbuf), "unknown code %d", code);
8090Sstevel@tonic-gate 	return ((const char *)msgbuf);
8100Sstevel@tonic-gate     }
8110Sstevel@tonic-gate     return (shortflag ? shortcode[code] : codelist[code]);
8120Sstevel@tonic-gate }
8130Sstevel@tonic-gate 
8140Sstevel@tonic-gate /* Procedures for locking the serial device using a lock file. */
8150Sstevel@tonic-gate #ifndef LOCK_DIR
8160Sstevel@tonic-gate #ifdef _linux_
8170Sstevel@tonic-gate #define LOCK_DIR	"/var/lock"
8180Sstevel@tonic-gate #else
8190Sstevel@tonic-gate #ifdef SVR4
8200Sstevel@tonic-gate #define LOCK_DIR	"/var/spool/locks"
8210Sstevel@tonic-gate #else
8220Sstevel@tonic-gate #define LOCK_DIR	"/var/spool/lock"
8230Sstevel@tonic-gate #endif
8240Sstevel@tonic-gate #endif
8250Sstevel@tonic-gate #endif /* LOCK_DIR */
8260Sstevel@tonic-gate 
8270Sstevel@tonic-gate static char lock_file[MAXPATHLEN];
8280Sstevel@tonic-gate 
8290Sstevel@tonic-gate /*
8300Sstevel@tonic-gate  * lock - create a lock file for the named device
8310Sstevel@tonic-gate  */
8320Sstevel@tonic-gate int
lock(dev)8330Sstevel@tonic-gate lock(dev)
8340Sstevel@tonic-gate     char *dev;
8350Sstevel@tonic-gate {
8360Sstevel@tonic-gate #ifdef LOCKLIB
8370Sstevel@tonic-gate     int result;
8380Sstevel@tonic-gate 
8390Sstevel@tonic-gate     result = mklock (dev, (void *) 0);
8400Sstevel@tonic-gate     if (result == 0) {
8410Sstevel@tonic-gate 	(void) strlcpy(lock_file, sizeof(lock_file), dev);
8420Sstevel@tonic-gate 	return (0);
8430Sstevel@tonic-gate     }
8440Sstevel@tonic-gate 
8450Sstevel@tonic-gate     if (result > 0)
8460Sstevel@tonic-gate         notice("Device %s is locked by pid %d", dev, result);
8470Sstevel@tonic-gate     else
8480Sstevel@tonic-gate 	error("Can't create lock file %s", lock_file);
8490Sstevel@tonic-gate     return (-1);
8500Sstevel@tonic-gate 
8510Sstevel@tonic-gate #else /* LOCKLIB */
8520Sstevel@tonic-gate 
8530Sstevel@tonic-gate     char lock_buffer[12];
8540Sstevel@tonic-gate     int fd, pid, n;
8550Sstevel@tonic-gate 
8560Sstevel@tonic-gate #ifdef SVR4
8570Sstevel@tonic-gate     struct stat sbuf;
8580Sstevel@tonic-gate 
8590Sstevel@tonic-gate     if (stat(dev, &sbuf) < 0) {
8600Sstevel@tonic-gate 	error("Can't get device number for %s: %m", dev);
8610Sstevel@tonic-gate 	return (-1);
8620Sstevel@tonic-gate     }
8630Sstevel@tonic-gate     if ((sbuf.st_mode & S_IFMT) != S_IFCHR) {
8640Sstevel@tonic-gate 	error("Can't lock %s: not a character device", dev);
8650Sstevel@tonic-gate 	return (-1);
8660Sstevel@tonic-gate     }
8670Sstevel@tonic-gate     (void) slprintf(lock_file, sizeof(lock_file), "%s/LK.%03d.%03d.%03d",
8680Sstevel@tonic-gate 	     LOCK_DIR, major(sbuf.st_dev),
8690Sstevel@tonic-gate 	     major(sbuf.st_rdev), minor(sbuf.st_rdev));
8700Sstevel@tonic-gate #else
8710Sstevel@tonic-gate     char *p;
8720Sstevel@tonic-gate 
8730Sstevel@tonic-gate     if ((p = strrchr(dev, '/')) != NULL)
8740Sstevel@tonic-gate 	dev = p + 1;
8750Sstevel@tonic-gate     (void) slprintf(lock_file, sizeof(lock_file), "%s/LCK..%s", LOCK_DIR, dev);
8760Sstevel@tonic-gate #endif
8770Sstevel@tonic-gate 
8780Sstevel@tonic-gate     while ((fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644)) < 0) {
8790Sstevel@tonic-gate 	if (errno != EEXIST) {
8800Sstevel@tonic-gate 	    error("Can't create lock file %s: %m", lock_file);
8810Sstevel@tonic-gate 	    break;
8820Sstevel@tonic-gate 	}
8830Sstevel@tonic-gate 
8840Sstevel@tonic-gate 	/* Read the lock file to find out who has the device locked. */
8850Sstevel@tonic-gate 	fd = open(lock_file, O_RDONLY, 0);
8860Sstevel@tonic-gate 	if (fd < 0) {
8870Sstevel@tonic-gate 	    if (errno == ENOENT) /* This is just a timing problem. */
8880Sstevel@tonic-gate 		continue;
8890Sstevel@tonic-gate 	    error("Can't open existing lock file %s: %m", lock_file);
8900Sstevel@tonic-gate 	    break;
8910Sstevel@tonic-gate 	}
8920Sstevel@tonic-gate #ifndef LOCK_BINARY
8930Sstevel@tonic-gate 	n = read(fd, lock_buffer, 11);
8940Sstevel@tonic-gate #else
8950Sstevel@tonic-gate 	n = read(fd, &pid, sizeof(pid));
8960Sstevel@tonic-gate #endif /* LOCK_BINARY */
8970Sstevel@tonic-gate 	(void) close(fd);
8980Sstevel@tonic-gate 	fd = -1;
8990Sstevel@tonic-gate 	if (n <= 0) {
9000Sstevel@tonic-gate 	    error("Can't read pid from lock file %s", lock_file);
9010Sstevel@tonic-gate 	    break;
9020Sstevel@tonic-gate 	}
9030Sstevel@tonic-gate 
9040Sstevel@tonic-gate 	/* See if the process still exists. */
9050Sstevel@tonic-gate #ifndef LOCK_BINARY
9060Sstevel@tonic-gate 	lock_buffer[n] = 0;
9070Sstevel@tonic-gate 	pid = atoi(lock_buffer);
9080Sstevel@tonic-gate #endif /* LOCK_BINARY */
9090Sstevel@tonic-gate 	if (pid == getpid())
9100Sstevel@tonic-gate 	    return (1);		/* somebody else locked it for us */
9110Sstevel@tonic-gate 	if (pid == 0
9120Sstevel@tonic-gate 	    || (kill(pid, 0) == -1 && errno == ESRCH)) {
9130Sstevel@tonic-gate 	    if (unlink (lock_file) == 0) {
9140Sstevel@tonic-gate 		notice("Removed stale lock on %s (pid %d)", dev, pid);
9150Sstevel@tonic-gate 		continue;
9160Sstevel@tonic-gate 	    }
9170Sstevel@tonic-gate 	    warn("Couldn't remove stale lock on %s", dev);
9180Sstevel@tonic-gate 	} else
9190Sstevel@tonic-gate 	    notice("Device %s is locked by pid %d", dev, pid);
9200Sstevel@tonic-gate 	break;
9210Sstevel@tonic-gate     }
9220Sstevel@tonic-gate 
9230Sstevel@tonic-gate     if (fd < 0) {
9240Sstevel@tonic-gate 	lock_file[0] = 0;
9250Sstevel@tonic-gate 	return (-1);
9260Sstevel@tonic-gate     }
9270Sstevel@tonic-gate 
9280Sstevel@tonic-gate     pid = getpid();
9290Sstevel@tonic-gate #ifndef LOCK_BINARY
9300Sstevel@tonic-gate     (void) slprintf(lock_buffer, sizeof(lock_buffer), "%10d\n", pid);
9310Sstevel@tonic-gate     (void) write (fd, lock_buffer, 11);
9320Sstevel@tonic-gate #else
9330Sstevel@tonic-gate     (void) write(fd, &pid, sizeof (pid));
9340Sstevel@tonic-gate #endif
9350Sstevel@tonic-gate     (void) close(fd);
9360Sstevel@tonic-gate     return (0);
9370Sstevel@tonic-gate 
9380Sstevel@tonic-gate #endif
9390Sstevel@tonic-gate }
9400Sstevel@tonic-gate 
9410Sstevel@tonic-gate /*
9420Sstevel@tonic-gate  * relock - called to update our lockfile when we are about to detach,
9430Sstevel@tonic-gate  * thus changing our pid (we fork, the child carries on, and the parent dies).
9440Sstevel@tonic-gate  * Note that this is called by the parent, with pid equal to the pid
9450Sstevel@tonic-gate  * of the child.  This avoids a potential race which would exist if
9460Sstevel@tonic-gate  * we had the child rewrite the lockfile (the parent might die first,
9470Sstevel@tonic-gate  * and another process could think the lock was stale if it checked
9480Sstevel@tonic-gate  * between when the parent died and the child rewrote the lockfile).
9490Sstevel@tonic-gate  */
9500Sstevel@tonic-gate int
relock(pid)9510Sstevel@tonic-gate relock(pid)
9520Sstevel@tonic-gate     int pid;
9530Sstevel@tonic-gate {
9540Sstevel@tonic-gate #ifdef LOCKLIB
9550Sstevel@tonic-gate     /* XXX is there a way to do this? */
9560Sstevel@tonic-gate     return (-1);
9570Sstevel@tonic-gate #else /* LOCKLIB */
9580Sstevel@tonic-gate 
9590Sstevel@tonic-gate     int fd;
9600Sstevel@tonic-gate     char lock_buffer[12];
9610Sstevel@tonic-gate 
9620Sstevel@tonic-gate     if (lock_file[0] == 0)
9630Sstevel@tonic-gate 	return (-1);
9640Sstevel@tonic-gate     fd = open(lock_file, O_WRONLY, 0);
9650Sstevel@tonic-gate     if (fd < 0) {
9660Sstevel@tonic-gate 	error("Couldn't reopen lock file %s: %m", lock_file);
9670Sstevel@tonic-gate 	lock_file[0] = 0;
9680Sstevel@tonic-gate 	return (-1);
9690Sstevel@tonic-gate     }
9700Sstevel@tonic-gate 
9710Sstevel@tonic-gate #ifndef LOCK_BINARY
9720Sstevel@tonic-gate     (void) slprintf(lock_buffer, sizeof(lock_buffer), "%10d\n", pid);
9730Sstevel@tonic-gate     (void) write (fd, lock_buffer, 11);
9740Sstevel@tonic-gate #else
9750Sstevel@tonic-gate     (void) write(fd, &pid, sizeof(pid));
9760Sstevel@tonic-gate #endif /* LOCK_BINARY */
9770Sstevel@tonic-gate     (void) close(fd);
9780Sstevel@tonic-gate     return (0);
9790Sstevel@tonic-gate 
9800Sstevel@tonic-gate #endif /* LOCKLIB */
9810Sstevel@tonic-gate }
9820Sstevel@tonic-gate 
9830Sstevel@tonic-gate /*
9840Sstevel@tonic-gate  * unlock - remove our lockfile
9850Sstevel@tonic-gate  */
9860Sstevel@tonic-gate void
unlock()9870Sstevel@tonic-gate unlock()
9880Sstevel@tonic-gate {
9890Sstevel@tonic-gate     if (lock_file[0]) {
9900Sstevel@tonic-gate #ifdef LOCKLIB
9910Sstevel@tonic-gate 	(void) rmlock(lock_file, (void *) 0);
9920Sstevel@tonic-gate #else
9930Sstevel@tonic-gate 	(void) unlink(lock_file);
9940Sstevel@tonic-gate #endif
9950Sstevel@tonic-gate 	lock_file[0] = 0;
9960Sstevel@tonic-gate     }
9970Sstevel@tonic-gate }
9980Sstevel@tonic-gate 
9990Sstevel@tonic-gate const char *
signal_name(int signum)10000Sstevel@tonic-gate signal_name(int signum)
10010Sstevel@tonic-gate {
10020Sstevel@tonic-gate #if defined(SOL2) || defined(__linux__) || defined(_linux_)
10030Sstevel@tonic-gate     const char *cp;
10040Sstevel@tonic-gate 
10050Sstevel@tonic-gate     if ((cp = strsignal(signum)) != NULL)
10060Sstevel@tonic-gate 	return (cp);
10070Sstevel@tonic-gate #else
10080Sstevel@tonic-gate     extern char *sys_siglist[];
10090Sstevel@tonic-gate     extern int sys_nsig;
10100Sstevel@tonic-gate 
10110Sstevel@tonic-gate     if (signum >= 0 && signum < sys_nsig && sys_siglist[signum] != NULL)
10120Sstevel@tonic-gate 	return (sys_siglist[signum]);
10130Sstevel@tonic-gate #endif
10140Sstevel@tonic-gate     return ("??");
10150Sstevel@tonic-gate }
1016