1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * utils.c - various utility functions used in pppd.
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * Copyright (c) 2000-2001 by Sun Microsystems, Inc.
5*7c478bd9Sstevel@tonic-gate  * All rights reserved.
6*7c478bd9Sstevel@tonic-gate  *
7*7c478bd9Sstevel@tonic-gate  * Permission to use, copy, modify, and distribute this software and its
8*7c478bd9Sstevel@tonic-gate  * documentation is hereby granted, provided that the above copyright
9*7c478bd9Sstevel@tonic-gate  * notice appears in all copies.
10*7c478bd9Sstevel@tonic-gate  *
11*7c478bd9Sstevel@tonic-gate  * SUN MAKES NO REPRESENTATION OR WARRANTIES ABOUT THE SUITABILITY OF
12*7c478bd9Sstevel@tonic-gate  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
13*7c478bd9Sstevel@tonic-gate  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
14*7c478bd9Sstevel@tonic-gate  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT.  SUN SHALL NOT BE LIABLE FOR
15*7c478bd9Sstevel@tonic-gate  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
16*7c478bd9Sstevel@tonic-gate  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES
17*7c478bd9Sstevel@tonic-gate  *
18*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1999 The Australian National University.
19*7c478bd9Sstevel@tonic-gate  * All rights reserved.
20*7c478bd9Sstevel@tonic-gate  *
21*7c478bd9Sstevel@tonic-gate  * Redistribution and use in source and binary forms are permitted
22*7c478bd9Sstevel@tonic-gate  * provided that the above copyright notice and this paragraph are
23*7c478bd9Sstevel@tonic-gate  * duplicated in all such forms and that any documentation,
24*7c478bd9Sstevel@tonic-gate  * advertising materials, and other materials related to such
25*7c478bd9Sstevel@tonic-gate  * distribution and use acknowledge that the software was developed
26*7c478bd9Sstevel@tonic-gate  * by the Australian National University.  The name of the University
27*7c478bd9Sstevel@tonic-gate  * may not be used to endorse or promote products derived from this
28*7c478bd9Sstevel@tonic-gate  * software without specific prior written permission.
29*7c478bd9Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
30*7c478bd9Sstevel@tonic-gate  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
31*7c478bd9Sstevel@tonic-gate  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32*7c478bd9Sstevel@tonic-gate  */
33*7c478bd9Sstevel@tonic-gate 
34*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
35*7c478bd9Sstevel@tonic-gate #define RCSID	"$Id: utils.c,v 1.10 2000/03/27 01:36:48 paulus Exp $"
36*7c478bd9Sstevel@tonic-gate 
37*7c478bd9Sstevel@tonic-gate #ifdef __linux__
38*7c478bd9Sstevel@tonic-gate #define	_GNU_SOURCE
39*7c478bd9Sstevel@tonic-gate #endif
40*7c478bd9Sstevel@tonic-gate #include <stdio.h>
41*7c478bd9Sstevel@tonic-gate #include <ctype.h>
42*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
43*7c478bd9Sstevel@tonic-gate #include <string.h>
44*7c478bd9Sstevel@tonic-gate #include <unistd.h>
45*7c478bd9Sstevel@tonic-gate #include <signal.h>
46*7c478bd9Sstevel@tonic-gate #include <errno.h>
47*7c478bd9Sstevel@tonic-gate #include <fcntl.h>
48*7c478bd9Sstevel@tonic-gate #include <syslog.h>
49*7c478bd9Sstevel@tonic-gate #include <netdb.h>
50*7c478bd9Sstevel@tonic-gate #include <utmp.h>
51*7c478bd9Sstevel@tonic-gate #include <pwd.h>
52*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
53*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
54*7c478bd9Sstevel@tonic-gate #include <sys/wait.h>
55*7c478bd9Sstevel@tonic-gate #include <sys/time.h>
56*7c478bd9Sstevel@tonic-gate #include <sys/resource.h>
57*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
58*7c478bd9Sstevel@tonic-gate #include <sys/socket.h>
59*7c478bd9Sstevel@tonic-gate #include <netinet/in.h>
60*7c478bd9Sstevel@tonic-gate #ifdef SVR4
61*7c478bd9Sstevel@tonic-gate #include <sys/mkdev.h>
62*7c478bd9Sstevel@tonic-gate #endif
63*7c478bd9Sstevel@tonic-gate 
64*7c478bd9Sstevel@tonic-gate #include "pppd.h"
65*7c478bd9Sstevel@tonic-gate 
66*7c478bd9Sstevel@tonic-gate #if !defined(lint) && !defined(_lint)
67*7c478bd9Sstevel@tonic-gate static const char rcsid[] = RCSID;
68*7c478bd9Sstevel@tonic-gate #endif
69*7c478bd9Sstevel@tonic-gate 
70*7c478bd9Sstevel@tonic-gate #if defined(SUNOS4)
71*7c478bd9Sstevel@tonic-gate extern char *strerror();
72*7c478bd9Sstevel@tonic-gate #endif
73*7c478bd9Sstevel@tonic-gate 
74*7c478bd9Sstevel@tonic-gate /* Don't log to stdout until we're sure it's ok to do so. */
75*7c478bd9Sstevel@tonic-gate bool early_log = 1;
76*7c478bd9Sstevel@tonic-gate 
77*7c478bd9Sstevel@tonic-gate static void pr_log __P((void *, const char *, ...));
78*7c478bd9Sstevel@tonic-gate static void logit __P((int, const char *, va_list));
79*7c478bd9Sstevel@tonic-gate static void vslp_printer __P((void *, const char *, ...));
80*7c478bd9Sstevel@tonic-gate static void format_packet __P((u_char *, int,
81*7c478bd9Sstevel@tonic-gate     void (*) (void *, const char *, ...), void *));
82*7c478bd9Sstevel@tonic-gate 
83*7c478bd9Sstevel@tonic-gate struct buffer_info {
84*7c478bd9Sstevel@tonic-gate     char *ptr;
85*7c478bd9Sstevel@tonic-gate     int len;
86*7c478bd9Sstevel@tonic-gate };
87*7c478bd9Sstevel@tonic-gate 
88*7c478bd9Sstevel@tonic-gate /*
89*7c478bd9Sstevel@tonic-gate  * strllen - like strlen, but doesn't run past end of input.
90*7c478bd9Sstevel@tonic-gate  */
91*7c478bd9Sstevel@tonic-gate size_t
92*7c478bd9Sstevel@tonic-gate strllen(str, len)
93*7c478bd9Sstevel@tonic-gate     const char *str;
94*7c478bd9Sstevel@tonic-gate     size_t len;
95*7c478bd9Sstevel@tonic-gate {
96*7c478bd9Sstevel@tonic-gate     size_t ret;
97*7c478bd9Sstevel@tonic-gate 
98*7c478bd9Sstevel@tonic-gate     for (ret = 0; ret < len; ret++)
99*7c478bd9Sstevel@tonic-gate 	if (*str++ == '\0')
100*7c478bd9Sstevel@tonic-gate 	    break;
101*7c478bd9Sstevel@tonic-gate     return (ret);
102*7c478bd9Sstevel@tonic-gate }
103*7c478bd9Sstevel@tonic-gate 
104*7c478bd9Sstevel@tonic-gate /*
105*7c478bd9Sstevel@tonic-gate  * strlcpy - like strcpy/strncpy, doesn't overflow destination buffer,
106*7c478bd9Sstevel@tonic-gate  * always leaves destination null-terminated (for len > 0).
107*7c478bd9Sstevel@tonic-gate  */
108*7c478bd9Sstevel@tonic-gate size_t
109*7c478bd9Sstevel@tonic-gate strlcpy(dest, src, len)
110*7c478bd9Sstevel@tonic-gate     char *dest;
111*7c478bd9Sstevel@tonic-gate     const char *src;
112*7c478bd9Sstevel@tonic-gate     size_t len;
113*7c478bd9Sstevel@tonic-gate {
114*7c478bd9Sstevel@tonic-gate     size_t ret = strlen(src);
115*7c478bd9Sstevel@tonic-gate 
116*7c478bd9Sstevel@tonic-gate     if (len != 0) {
117*7c478bd9Sstevel@tonic-gate 	if (ret < len)
118*7c478bd9Sstevel@tonic-gate 	    (void) strcpy(dest, src);
119*7c478bd9Sstevel@tonic-gate 	else {
120*7c478bd9Sstevel@tonic-gate 	    (void) strncpy(dest, src, len - 1);
121*7c478bd9Sstevel@tonic-gate 	    dest[len-1] = 0;
122*7c478bd9Sstevel@tonic-gate 	}
123*7c478bd9Sstevel@tonic-gate     }
124*7c478bd9Sstevel@tonic-gate     return (ret);
125*7c478bd9Sstevel@tonic-gate }
126*7c478bd9Sstevel@tonic-gate 
127*7c478bd9Sstevel@tonic-gate /*
128*7c478bd9Sstevel@tonic-gate  * strlcat - like strcat/strncat, doesn't overflow destination buffer,
129*7c478bd9Sstevel@tonic-gate  * always leaves destination null-terminated (for len > 0).
130*7c478bd9Sstevel@tonic-gate  */
131*7c478bd9Sstevel@tonic-gate size_t
132*7c478bd9Sstevel@tonic-gate strlcat(dest, src, len)
133*7c478bd9Sstevel@tonic-gate     char *dest;
134*7c478bd9Sstevel@tonic-gate     const char *src;
135*7c478bd9Sstevel@tonic-gate     size_t len;
136*7c478bd9Sstevel@tonic-gate {
137*7c478bd9Sstevel@tonic-gate     size_t dlen = strlen(dest);
138*7c478bd9Sstevel@tonic-gate 
139*7c478bd9Sstevel@tonic-gate     return (dlen + strlcpy(dest + dlen, src, (len > dlen? len - dlen: 0)));
140*7c478bd9Sstevel@tonic-gate }
141*7c478bd9Sstevel@tonic-gate 
142*7c478bd9Sstevel@tonic-gate 
143*7c478bd9Sstevel@tonic-gate /*
144*7c478bd9Sstevel@tonic-gate  * slprintf - format a message into a buffer.  Like sprintf except we
145*7c478bd9Sstevel@tonic-gate  * also specify the length of the output buffer, and we handle %m
146*7c478bd9Sstevel@tonic-gate  * (error message), %v (visible string), %q (quoted string), %t
147*7c478bd9Sstevel@tonic-gate  * (current time), %I (IP address), %P (PPP packet), and %B (sequence
148*7c478bd9Sstevel@tonic-gate  * of bytes) formats.  Doesn't do floating-point formats.  Returns the
149*7c478bd9Sstevel@tonic-gate  * number of chars put into buf.
150*7c478bd9Sstevel@tonic-gate  */
151*7c478bd9Sstevel@tonic-gate int
152*7c478bd9Sstevel@tonic-gate slprintf __V((char *buf, int buflen, const char *fmt, ...))
153*7c478bd9Sstevel@tonic-gate {
154*7c478bd9Sstevel@tonic-gate     va_list args;
155*7c478bd9Sstevel@tonic-gate     int n;
156*7c478bd9Sstevel@tonic-gate 
157*7c478bd9Sstevel@tonic-gate #if defined(__STDC__)
158*7c478bd9Sstevel@tonic-gate     va_start(args, fmt);
159*7c478bd9Sstevel@tonic-gate #else
160*7c478bd9Sstevel@tonic-gate     char *buf;
161*7c478bd9Sstevel@tonic-gate     int buflen;
162*7c478bd9Sstevel@tonic-gate     const char *fmt;
163*7c478bd9Sstevel@tonic-gate     va_start(args);
164*7c478bd9Sstevel@tonic-gate     buf = va_arg(args, char *);
165*7c478bd9Sstevel@tonic-gate     buflen = va_arg(args, int);
166*7c478bd9Sstevel@tonic-gate     fmt = va_arg(args, const char *);
167*7c478bd9Sstevel@tonic-gate #endif
168*7c478bd9Sstevel@tonic-gate     n = vslprintf(buf, buflen, fmt, args);
169*7c478bd9Sstevel@tonic-gate     va_end(args);
170*7c478bd9Sstevel@tonic-gate     return (n);
171*7c478bd9Sstevel@tonic-gate }
172*7c478bd9Sstevel@tonic-gate 
173*7c478bd9Sstevel@tonic-gate /*
174*7c478bd9Sstevel@tonic-gate  * Print to file or, if argument is NULL, to syslog at debug level.
175*7c478bd9Sstevel@tonic-gate  */
176*7c478bd9Sstevel@tonic-gate int
177*7c478bd9Sstevel@tonic-gate flprintf __V((FILE *strptr, const char *fmt, ...))
178*7c478bd9Sstevel@tonic-gate {
179*7c478bd9Sstevel@tonic-gate     va_list args;
180*7c478bd9Sstevel@tonic-gate     int n;
181*7c478bd9Sstevel@tonic-gate     char buf[1024], *bp, *nlp, *ebp;
182*7c478bd9Sstevel@tonic-gate 
183*7c478bd9Sstevel@tonic-gate #if defined(__STDC__)
184*7c478bd9Sstevel@tonic-gate     va_start(args, fmt);
185*7c478bd9Sstevel@tonic-gate #else
186*7c478bd9Sstevel@tonic-gate     FILE *strptr;
187*7c478bd9Sstevel@tonic-gate     const char *fmt;
188*7c478bd9Sstevel@tonic-gate     va_start(args);
189*7c478bd9Sstevel@tonic-gate     strptr = va_arg(args, FILE *);
190*7c478bd9Sstevel@tonic-gate     fmt = va_arg(args, const char *);
191*7c478bd9Sstevel@tonic-gate #endif
192*7c478bd9Sstevel@tonic-gate     n = vslprintf(buf, sizeof (buf), fmt, args);
193*7c478bd9Sstevel@tonic-gate     va_end(args);
194*7c478bd9Sstevel@tonic-gate     if (strptr == NULL) {
195*7c478bd9Sstevel@tonic-gate 	bp = buf;
196*7c478bd9Sstevel@tonic-gate 	ebp = buf + n;
197*7c478bd9Sstevel@tonic-gate 	while (bp < ebp) {
198*7c478bd9Sstevel@tonic-gate 	    if ((nlp = strchr(bp, '\n')) == NULL)
199*7c478bd9Sstevel@tonic-gate 		nlp = ebp;
200*7c478bd9Sstevel@tonic-gate 	    if (nlp > bp) {
201*7c478bd9Sstevel@tonic-gate 		*nlp = '\0';
202*7c478bd9Sstevel@tonic-gate 		syslog(LOG_DEBUG, "%s", bp);
203*7c478bd9Sstevel@tonic-gate 	    }
204*7c478bd9Sstevel@tonic-gate 	    bp = nlp + 1;
205*7c478bd9Sstevel@tonic-gate 	}
206*7c478bd9Sstevel@tonic-gate     } else {
207*7c478bd9Sstevel@tonic-gate 	n = fwrite(buf, 1, n, strptr);
208*7c478bd9Sstevel@tonic-gate     }
209*7c478bd9Sstevel@tonic-gate     return (n);
210*7c478bd9Sstevel@tonic-gate }
211*7c478bd9Sstevel@tonic-gate 
212*7c478bd9Sstevel@tonic-gate /*
213*7c478bd9Sstevel@tonic-gate  * vslprintf - like slprintf, takes a va_list instead of a list of args.
214*7c478bd9Sstevel@tonic-gate  */
215*7c478bd9Sstevel@tonic-gate #define OUTCHAR(c)	(buflen > 0? (--buflen, *buf++ = (c)): 0)
216*7c478bd9Sstevel@tonic-gate 
217*7c478bd9Sstevel@tonic-gate int
218*7c478bd9Sstevel@tonic-gate vslprintf(buf, buflen, fmt, args)
219*7c478bd9Sstevel@tonic-gate     char *buf;
220*7c478bd9Sstevel@tonic-gate     int buflen;
221*7c478bd9Sstevel@tonic-gate     const char *fmt;
222*7c478bd9Sstevel@tonic-gate     va_list args;
223*7c478bd9Sstevel@tonic-gate {
224*7c478bd9Sstevel@tonic-gate     int c, n, longs;
225*7c478bd9Sstevel@tonic-gate     int width, prec, fillch;
226*7c478bd9Sstevel@tonic-gate     int base, len, neg, quoted;
227*7c478bd9Sstevel@tonic-gate #ifdef SOL2
228*7c478bd9Sstevel@tonic-gate     uint64_t val;
229*7c478bd9Sstevel@tonic-gate     int64_t sval;
230*7c478bd9Sstevel@tonic-gate #else
231*7c478bd9Sstevel@tonic-gate     unsigned long val;
232*7c478bd9Sstevel@tonic-gate     long sval;
233*7c478bd9Sstevel@tonic-gate #endif
234*7c478bd9Sstevel@tonic-gate     char *buf0, *mstr;
235*7c478bd9Sstevel@tonic-gate     const char *f, *str;
236*7c478bd9Sstevel@tonic-gate     unsigned char *p;
237*7c478bd9Sstevel@tonic-gate     char num[32];	/* 2^64 is 20 chars decimal, 22 octal */
238*7c478bd9Sstevel@tonic-gate     time_t t;
239*7c478bd9Sstevel@tonic-gate     u_int32_t ip;
240*7c478bd9Sstevel@tonic-gate     static const char hexchars[] = "0123456789abcdef";
241*7c478bd9Sstevel@tonic-gate     struct buffer_info bufinfo;
242*7c478bd9Sstevel@tonic-gate 
243*7c478bd9Sstevel@tonic-gate     buf0 = buf;
244*7c478bd9Sstevel@tonic-gate     --buflen;
245*7c478bd9Sstevel@tonic-gate     while (buflen > 0) {
246*7c478bd9Sstevel@tonic-gate 	for (f = fmt; *f != '%' && *f != 0; ++f)
247*7c478bd9Sstevel@tonic-gate 	    ;
248*7c478bd9Sstevel@tonic-gate 	if (f > fmt) {
249*7c478bd9Sstevel@tonic-gate 	    len = f - fmt;
250*7c478bd9Sstevel@tonic-gate 	    if (len > buflen)
251*7c478bd9Sstevel@tonic-gate 		len = buflen;
252*7c478bd9Sstevel@tonic-gate 	    (void) memcpy(buf, fmt, len);
253*7c478bd9Sstevel@tonic-gate 	    buf += len;
254*7c478bd9Sstevel@tonic-gate 	    buflen -= len;
255*7c478bd9Sstevel@tonic-gate 	    fmt = f;
256*7c478bd9Sstevel@tonic-gate 	}
257*7c478bd9Sstevel@tonic-gate 	if (*fmt == 0)
258*7c478bd9Sstevel@tonic-gate 	    break;
259*7c478bd9Sstevel@tonic-gate 	c = *++fmt;
260*7c478bd9Sstevel@tonic-gate 	width = 0;
261*7c478bd9Sstevel@tonic-gate 	prec = -1;
262*7c478bd9Sstevel@tonic-gate 	fillch = ' ';
263*7c478bd9Sstevel@tonic-gate 	if (c == '0') {
264*7c478bd9Sstevel@tonic-gate 	    fillch = '0';
265*7c478bd9Sstevel@tonic-gate 	    c = *++fmt;
266*7c478bd9Sstevel@tonic-gate 	}
267*7c478bd9Sstevel@tonic-gate 	if (c == '*') {
268*7c478bd9Sstevel@tonic-gate 	    width = va_arg(args, int);
269*7c478bd9Sstevel@tonic-gate 	    c = *++fmt;
270*7c478bd9Sstevel@tonic-gate 	} else {
271*7c478bd9Sstevel@tonic-gate 	    while (isdigit(c)) {
272*7c478bd9Sstevel@tonic-gate 		width = width * 10 + c - '0';
273*7c478bd9Sstevel@tonic-gate 		c = *++fmt;
274*7c478bd9Sstevel@tonic-gate 	    }
275*7c478bd9Sstevel@tonic-gate 	}
276*7c478bd9Sstevel@tonic-gate 	if (c == '.') {
277*7c478bd9Sstevel@tonic-gate 	    c = *++fmt;
278*7c478bd9Sstevel@tonic-gate 	    if (c == '*') {
279*7c478bd9Sstevel@tonic-gate 		prec = va_arg(args, int);
280*7c478bd9Sstevel@tonic-gate 		c = *++fmt;
281*7c478bd9Sstevel@tonic-gate 	    } else {
282*7c478bd9Sstevel@tonic-gate 		prec = 0;
283*7c478bd9Sstevel@tonic-gate 		while (isdigit(c)) {
284*7c478bd9Sstevel@tonic-gate 		    prec = prec * 10 + c - '0';
285*7c478bd9Sstevel@tonic-gate 		    c = *++fmt;
286*7c478bd9Sstevel@tonic-gate 		}
287*7c478bd9Sstevel@tonic-gate 	    }
288*7c478bd9Sstevel@tonic-gate 	}
289*7c478bd9Sstevel@tonic-gate 	longs = 0;
290*7c478bd9Sstevel@tonic-gate 	if (c == 'l') {
291*7c478bd9Sstevel@tonic-gate 	    longs++;
292*7c478bd9Sstevel@tonic-gate 	    c = *++fmt;
293*7c478bd9Sstevel@tonic-gate 	    if (c == 'l') {
294*7c478bd9Sstevel@tonic-gate 		longs++;
295*7c478bd9Sstevel@tonic-gate 		c = *++fmt;
296*7c478bd9Sstevel@tonic-gate 	    }
297*7c478bd9Sstevel@tonic-gate 	}
298*7c478bd9Sstevel@tonic-gate 	str = 0;
299*7c478bd9Sstevel@tonic-gate 	base = 0;
300*7c478bd9Sstevel@tonic-gate 	neg = 0;
301*7c478bd9Sstevel@tonic-gate 	val = 0;
302*7c478bd9Sstevel@tonic-gate 	++fmt;
303*7c478bd9Sstevel@tonic-gate 	switch (c) {
304*7c478bd9Sstevel@tonic-gate 	case 'u':
305*7c478bd9Sstevel@tonic-gate #ifdef SOL2
306*7c478bd9Sstevel@tonic-gate 	    if (longs >= 2)
307*7c478bd9Sstevel@tonic-gate 		val = va_arg(args, uint64_t);
308*7c478bd9Sstevel@tonic-gate 	    else
309*7c478bd9Sstevel@tonic-gate #endif
310*7c478bd9Sstevel@tonic-gate 	    if (longs > 0)
311*7c478bd9Sstevel@tonic-gate 		val = va_arg(args, unsigned long);
312*7c478bd9Sstevel@tonic-gate 	    else
313*7c478bd9Sstevel@tonic-gate 		val = va_arg(args, unsigned int);
314*7c478bd9Sstevel@tonic-gate 	    base = 10;
315*7c478bd9Sstevel@tonic-gate 	    break;
316*7c478bd9Sstevel@tonic-gate 	case 'd':
317*7c478bd9Sstevel@tonic-gate #ifdef SOL2
318*7c478bd9Sstevel@tonic-gate 	    if (longs >= 2)
319*7c478bd9Sstevel@tonic-gate 		sval = va_arg(args, int64_t);
320*7c478bd9Sstevel@tonic-gate 	    else
321*7c478bd9Sstevel@tonic-gate #endif
322*7c478bd9Sstevel@tonic-gate 	    if (longs > 0)
323*7c478bd9Sstevel@tonic-gate 		sval = va_arg(args, long);
324*7c478bd9Sstevel@tonic-gate 	    else
325*7c478bd9Sstevel@tonic-gate 		sval = va_arg(args, int);
326*7c478bd9Sstevel@tonic-gate 	    if (sval < 0) {
327*7c478bd9Sstevel@tonic-gate 		neg = 1;
328*7c478bd9Sstevel@tonic-gate 		val = -sval;
329*7c478bd9Sstevel@tonic-gate 	    } else
330*7c478bd9Sstevel@tonic-gate 		val = sval;
331*7c478bd9Sstevel@tonic-gate 	    base = 10;
332*7c478bd9Sstevel@tonic-gate 	    break;
333*7c478bd9Sstevel@tonic-gate 	case 'o':
334*7c478bd9Sstevel@tonic-gate #ifdef SOL2
335*7c478bd9Sstevel@tonic-gate 	    if (longs >= 2)
336*7c478bd9Sstevel@tonic-gate 		val = va_arg(args, uint64_t);
337*7c478bd9Sstevel@tonic-gate 	    else
338*7c478bd9Sstevel@tonic-gate #endif
339*7c478bd9Sstevel@tonic-gate 	    if (longs > 0)
340*7c478bd9Sstevel@tonic-gate 		val = va_arg(args, unsigned long);
341*7c478bd9Sstevel@tonic-gate 	    else
342*7c478bd9Sstevel@tonic-gate 		val = va_arg(args, unsigned int);
343*7c478bd9Sstevel@tonic-gate 	    base = 8;
344*7c478bd9Sstevel@tonic-gate 	    break;
345*7c478bd9Sstevel@tonic-gate 	case 'x':
346*7c478bd9Sstevel@tonic-gate 	case 'X':
347*7c478bd9Sstevel@tonic-gate #ifdef SOL2
348*7c478bd9Sstevel@tonic-gate 	    if (longs >= 2)
349*7c478bd9Sstevel@tonic-gate 		val = va_arg(args, uint64_t);
350*7c478bd9Sstevel@tonic-gate 	    else
351*7c478bd9Sstevel@tonic-gate #endif
352*7c478bd9Sstevel@tonic-gate 	    if (longs > 0)
353*7c478bd9Sstevel@tonic-gate 		val = va_arg(args, unsigned long);
354*7c478bd9Sstevel@tonic-gate 	    else
355*7c478bd9Sstevel@tonic-gate 		val = va_arg(args, unsigned int);
356*7c478bd9Sstevel@tonic-gate 	    base = 16;
357*7c478bd9Sstevel@tonic-gate 	    break;
358*7c478bd9Sstevel@tonic-gate 	case 'p':
359*7c478bd9Sstevel@tonic-gate 	    val = (unsigned long) va_arg(args, void *);
360*7c478bd9Sstevel@tonic-gate 	    base = 16;
361*7c478bd9Sstevel@tonic-gate 	    neg = 2;
362*7c478bd9Sstevel@tonic-gate 	    break;
363*7c478bd9Sstevel@tonic-gate 	case 's':
364*7c478bd9Sstevel@tonic-gate 	    str = va_arg(args, const char *);
365*7c478bd9Sstevel@tonic-gate 	    break;
366*7c478bd9Sstevel@tonic-gate 	case 'c':
367*7c478bd9Sstevel@tonic-gate 	    num[0] = va_arg(args, int);
368*7c478bd9Sstevel@tonic-gate 	    num[1] = 0;
369*7c478bd9Sstevel@tonic-gate 	    str = num;
370*7c478bd9Sstevel@tonic-gate 	    break;
371*7c478bd9Sstevel@tonic-gate 	case 'm':
372*7c478bd9Sstevel@tonic-gate 	    str = strerror(errno);
373*7c478bd9Sstevel@tonic-gate 	    break;
374*7c478bd9Sstevel@tonic-gate 	case 'I':
375*7c478bd9Sstevel@tonic-gate 	    ip = va_arg(args, u_int32_t);
376*7c478bd9Sstevel@tonic-gate 	    ip = ntohl(ip);
377*7c478bd9Sstevel@tonic-gate 	    (void) slprintf(num, sizeof(num), "%d.%d.%d.%d", (ip >> 24) & 0xff,
378*7c478bd9Sstevel@tonic-gate 		(ip >> 16) & 0xff, (ip >> 8) & 0xff, ip & 0xff);
379*7c478bd9Sstevel@tonic-gate 	    str = num;
380*7c478bd9Sstevel@tonic-gate 	    break;
381*7c478bd9Sstevel@tonic-gate 	case 't':
382*7c478bd9Sstevel@tonic-gate 	    (void) time(&t);
383*7c478bd9Sstevel@tonic-gate 	    mstr = ctime(&t);
384*7c478bd9Sstevel@tonic-gate 	    mstr += 4;		/* chop off the day name */
385*7c478bd9Sstevel@tonic-gate 	    mstr[15] = 0;	/* chop off year and newline */
386*7c478bd9Sstevel@tonic-gate 	    str = (const char *)mstr;
387*7c478bd9Sstevel@tonic-gate 	    break;
388*7c478bd9Sstevel@tonic-gate 	case 'v':		/* "visible" string */
389*7c478bd9Sstevel@tonic-gate 	case 'q':		/* quoted string */
390*7c478bd9Sstevel@tonic-gate 	    quoted = c == 'q';
391*7c478bd9Sstevel@tonic-gate 	    p = va_arg(args, unsigned char *);
392*7c478bd9Sstevel@tonic-gate 	    if (fillch == '0' && prec >= 0) {
393*7c478bd9Sstevel@tonic-gate 		n = prec;
394*7c478bd9Sstevel@tonic-gate 	    } else {
395*7c478bd9Sstevel@tonic-gate 		n = strlen((char *)p);
396*7c478bd9Sstevel@tonic-gate 		if (prec >= 0 && n > prec)
397*7c478bd9Sstevel@tonic-gate 		    n = prec;
398*7c478bd9Sstevel@tonic-gate 	    }
399*7c478bd9Sstevel@tonic-gate 	    while (n > 0 && buflen > 0) {
400*7c478bd9Sstevel@tonic-gate 		c = *p++;
401*7c478bd9Sstevel@tonic-gate 		--n;
402*7c478bd9Sstevel@tonic-gate 		if (!quoted && c >= 0x80) {
403*7c478bd9Sstevel@tonic-gate 		    (void) OUTCHAR('M');
404*7c478bd9Sstevel@tonic-gate 		    (void) OUTCHAR('-');
405*7c478bd9Sstevel@tonic-gate 		    c -= 0x80;
406*7c478bd9Sstevel@tonic-gate 		}
407*7c478bd9Sstevel@tonic-gate 		if (quoted && (c == '"' || c == '\\'))
408*7c478bd9Sstevel@tonic-gate 		    (void) OUTCHAR('\\');
409*7c478bd9Sstevel@tonic-gate 		if (c < 0x20 || (0x7f <= c && c < 0xa0)) {
410*7c478bd9Sstevel@tonic-gate 		    if (quoted) {
411*7c478bd9Sstevel@tonic-gate 			(void) OUTCHAR('\\');
412*7c478bd9Sstevel@tonic-gate 			switch (c) {
413*7c478bd9Sstevel@tonic-gate 			case '\t':	(void) OUTCHAR('t');	break;
414*7c478bd9Sstevel@tonic-gate 			case '\n':	(void) OUTCHAR('n');	break;
415*7c478bd9Sstevel@tonic-gate 			case '\b':	(void) OUTCHAR('b');	break;
416*7c478bd9Sstevel@tonic-gate 			case '\f':	(void) OUTCHAR('f');	break;
417*7c478bd9Sstevel@tonic-gate 			default:
418*7c478bd9Sstevel@tonic-gate 			    (void) OUTCHAR('x');
419*7c478bd9Sstevel@tonic-gate 			    (void) OUTCHAR(hexchars[c >> 4]);
420*7c478bd9Sstevel@tonic-gate 			    (void) OUTCHAR(hexchars[c & 0xf]);
421*7c478bd9Sstevel@tonic-gate 			}
422*7c478bd9Sstevel@tonic-gate 		    } else {
423*7c478bd9Sstevel@tonic-gate 			if (c == '\t')
424*7c478bd9Sstevel@tonic-gate 			    (void) OUTCHAR(c);
425*7c478bd9Sstevel@tonic-gate 			else {
426*7c478bd9Sstevel@tonic-gate 			    (void) OUTCHAR('^');
427*7c478bd9Sstevel@tonic-gate 			    (void) OUTCHAR(c ^ 0x40);
428*7c478bd9Sstevel@tonic-gate 			}
429*7c478bd9Sstevel@tonic-gate 		    }
430*7c478bd9Sstevel@tonic-gate 		} else
431*7c478bd9Sstevel@tonic-gate 		    (void) OUTCHAR(c);
432*7c478bd9Sstevel@tonic-gate 	    }
433*7c478bd9Sstevel@tonic-gate 	    continue;
434*7c478bd9Sstevel@tonic-gate 	case 'P':		/* print PPP packet */
435*7c478bd9Sstevel@tonic-gate 	    bufinfo.ptr = buf;
436*7c478bd9Sstevel@tonic-gate 	    bufinfo.len = buflen + 1;
437*7c478bd9Sstevel@tonic-gate 	    p = va_arg(args, unsigned char *);
438*7c478bd9Sstevel@tonic-gate 	    n = va_arg(args, int);
439*7c478bd9Sstevel@tonic-gate 	    format_packet(p, n, vslp_printer, &bufinfo);
440*7c478bd9Sstevel@tonic-gate 	    buf = bufinfo.ptr;
441*7c478bd9Sstevel@tonic-gate 	    buflen = bufinfo.len - 1;
442*7c478bd9Sstevel@tonic-gate 	    continue;
443*7c478bd9Sstevel@tonic-gate 	case 'B':
444*7c478bd9Sstevel@tonic-gate 	    p = va_arg(args, unsigned char *);
445*7c478bd9Sstevel@tonic-gate 	    if ((n = prec) > width && width > 0)
446*7c478bd9Sstevel@tonic-gate 		n = width;
447*7c478bd9Sstevel@tonic-gate 	    /* For safety's sake */
448*7c478bd9Sstevel@tonic-gate 	    if (n > 2000)
449*7c478bd9Sstevel@tonic-gate 		    n = 2000;
450*7c478bd9Sstevel@tonic-gate 	    while (--n >= 0) {
451*7c478bd9Sstevel@tonic-gate 		c = *p++;
452*7c478bd9Sstevel@tonic-gate 		if (fillch == ' ')
453*7c478bd9Sstevel@tonic-gate 		    (void) OUTCHAR(' ');
454*7c478bd9Sstevel@tonic-gate 		(void) OUTCHAR(hexchars[(c >> 4) & 0xf]);
455*7c478bd9Sstevel@tonic-gate 		(void) OUTCHAR(hexchars[c & 0xf]);
456*7c478bd9Sstevel@tonic-gate 	    }
457*7c478bd9Sstevel@tonic-gate 	    if (prec > width && width > 0) {
458*7c478bd9Sstevel@tonic-gate 		(void) OUTCHAR('.');
459*7c478bd9Sstevel@tonic-gate 		(void) OUTCHAR('.');
460*7c478bd9Sstevel@tonic-gate 		(void) OUTCHAR('.');
461*7c478bd9Sstevel@tonic-gate 	    }
462*7c478bd9Sstevel@tonic-gate 	    continue;
463*7c478bd9Sstevel@tonic-gate 	default:
464*7c478bd9Sstevel@tonic-gate 	    *buf++ = '%';
465*7c478bd9Sstevel@tonic-gate 	    if (c != '%')
466*7c478bd9Sstevel@tonic-gate 		--fmt;		/* so %z outputs %z etc. */
467*7c478bd9Sstevel@tonic-gate 	    --buflen;
468*7c478bd9Sstevel@tonic-gate 	    continue;
469*7c478bd9Sstevel@tonic-gate 	}
470*7c478bd9Sstevel@tonic-gate 	if (base != 0) {
471*7c478bd9Sstevel@tonic-gate 	    mstr = num + sizeof(num);
472*7c478bd9Sstevel@tonic-gate 	    *--mstr = 0;
473*7c478bd9Sstevel@tonic-gate 	    while (mstr > num + neg) {
474*7c478bd9Sstevel@tonic-gate 		*--mstr = hexchars[val % base];
475*7c478bd9Sstevel@tonic-gate 		val = val / base;
476*7c478bd9Sstevel@tonic-gate 		if (--prec <= 0 && val == 0)
477*7c478bd9Sstevel@tonic-gate 		    break;
478*7c478bd9Sstevel@tonic-gate 	    }
479*7c478bd9Sstevel@tonic-gate 	    switch (neg) {
480*7c478bd9Sstevel@tonic-gate 	    case 1:
481*7c478bd9Sstevel@tonic-gate 		*--mstr = '-';
482*7c478bd9Sstevel@tonic-gate 		break;
483*7c478bd9Sstevel@tonic-gate 	    case 2:
484*7c478bd9Sstevel@tonic-gate 		*--mstr = 'x';
485*7c478bd9Sstevel@tonic-gate 		*--mstr = '0';
486*7c478bd9Sstevel@tonic-gate 		break;
487*7c478bd9Sstevel@tonic-gate 	    }
488*7c478bd9Sstevel@tonic-gate 	    len = num + sizeof(num) - 1 - mstr;
489*7c478bd9Sstevel@tonic-gate 	    str = (const char *)mstr;
490*7c478bd9Sstevel@tonic-gate 	} else {
491*7c478bd9Sstevel@tonic-gate 	    len = strlen(str);
492*7c478bd9Sstevel@tonic-gate 	    if (prec >= 0 && len > prec)
493*7c478bd9Sstevel@tonic-gate 		len = prec;
494*7c478bd9Sstevel@tonic-gate 	}
495*7c478bd9Sstevel@tonic-gate 	if (width > 0) {
496*7c478bd9Sstevel@tonic-gate 	    if (width > buflen)
497*7c478bd9Sstevel@tonic-gate 		width = buflen;
498*7c478bd9Sstevel@tonic-gate 	    if ((n = width - len) > 0) {
499*7c478bd9Sstevel@tonic-gate 		buflen -= n;
500*7c478bd9Sstevel@tonic-gate 		for (; n > 0; --n)
501*7c478bd9Sstevel@tonic-gate 		    *buf++ = fillch;
502*7c478bd9Sstevel@tonic-gate 	    }
503*7c478bd9Sstevel@tonic-gate 	}
504*7c478bd9Sstevel@tonic-gate 	if (len > buflen)
505*7c478bd9Sstevel@tonic-gate 	    len = buflen;
506*7c478bd9Sstevel@tonic-gate 	(void) memcpy(buf, str, len);
507*7c478bd9Sstevel@tonic-gate 	buf += len;
508*7c478bd9Sstevel@tonic-gate 	buflen -= len;
509*7c478bd9Sstevel@tonic-gate     }
510*7c478bd9Sstevel@tonic-gate     *buf = 0;
511*7c478bd9Sstevel@tonic-gate     return (buf - buf0);
512*7c478bd9Sstevel@tonic-gate }
513*7c478bd9Sstevel@tonic-gate 
514*7c478bd9Sstevel@tonic-gate /*
515*7c478bd9Sstevel@tonic-gate  * vslp_printer - used in processing a %P format
516*7c478bd9Sstevel@tonic-gate  */
517*7c478bd9Sstevel@tonic-gate static void
518*7c478bd9Sstevel@tonic-gate vslp_printer __V((void *arg, const char *fmt, ...))
519*7c478bd9Sstevel@tonic-gate {
520*7c478bd9Sstevel@tonic-gate     int n;
521*7c478bd9Sstevel@tonic-gate     va_list pvar;
522*7c478bd9Sstevel@tonic-gate     struct buffer_info *bi;
523*7c478bd9Sstevel@tonic-gate 
524*7c478bd9Sstevel@tonic-gate #if defined(__STDC__)
525*7c478bd9Sstevel@tonic-gate     va_start(pvar, fmt);
526*7c478bd9Sstevel@tonic-gate #else
527*7c478bd9Sstevel@tonic-gate     void *arg;
528*7c478bd9Sstevel@tonic-gate     const char *fmt;
529*7c478bd9Sstevel@tonic-gate     va_start(pvar);
530*7c478bd9Sstevel@tonic-gate     arg = va_arg(pvar, void *);
531*7c478bd9Sstevel@tonic-gate     fmt = va_arg(pvar, const char *);
532*7c478bd9Sstevel@tonic-gate #endif
533*7c478bd9Sstevel@tonic-gate 
534*7c478bd9Sstevel@tonic-gate     bi = (struct buffer_info *) arg;
535*7c478bd9Sstevel@tonic-gate     n = vslprintf(bi->ptr, bi->len, fmt, pvar);
536*7c478bd9Sstevel@tonic-gate     va_end(pvar);
537*7c478bd9Sstevel@tonic-gate 
538*7c478bd9Sstevel@tonic-gate     bi->ptr += n;
539*7c478bd9Sstevel@tonic-gate     bi->len -= n;
540*7c478bd9Sstevel@tonic-gate }
541*7c478bd9Sstevel@tonic-gate 
542*7c478bd9Sstevel@tonic-gate /*
543*7c478bd9Sstevel@tonic-gate  * log_packet - format a packet and log it.
544*7c478bd9Sstevel@tonic-gate  */
545*7c478bd9Sstevel@tonic-gate 
546*7c478bd9Sstevel@tonic-gate static char line[256];		/* line to be logged accumulated here */
547*7c478bd9Sstevel@tonic-gate static char *linep;
548*7c478bd9Sstevel@tonic-gate 
549*7c478bd9Sstevel@tonic-gate void
550*7c478bd9Sstevel@tonic-gate log_packet(p, len, prefix, level)
551*7c478bd9Sstevel@tonic-gate     u_char *p;
552*7c478bd9Sstevel@tonic-gate     int len;
553*7c478bd9Sstevel@tonic-gate     const char *prefix;
554*7c478bd9Sstevel@tonic-gate     int level;
555*7c478bd9Sstevel@tonic-gate {
556*7c478bd9Sstevel@tonic-gate     (void) strlcpy(line, prefix, sizeof(line));
557*7c478bd9Sstevel@tonic-gate     linep = line + strlen(line);
558*7c478bd9Sstevel@tonic-gate     format_packet(p, len, pr_log, (void *)level);
559*7c478bd9Sstevel@tonic-gate     if (linep != line)
560*7c478bd9Sstevel@tonic-gate 	syslog(level, "%s", line);
561*7c478bd9Sstevel@tonic-gate }
562*7c478bd9Sstevel@tonic-gate 
563*7c478bd9Sstevel@tonic-gate /*
564*7c478bd9Sstevel@tonic-gate  * format_packet - make a readable representation of a packet,
565*7c478bd9Sstevel@tonic-gate  * calling `printer(arg, format, ...)' to output it.
566*7c478bd9Sstevel@tonic-gate  */
567*7c478bd9Sstevel@tonic-gate static void
568*7c478bd9Sstevel@tonic-gate format_packet(p, len, printer, arg)
569*7c478bd9Sstevel@tonic-gate     u_char *p;
570*7c478bd9Sstevel@tonic-gate     int len;
571*7c478bd9Sstevel@tonic-gate     void (*printer) __P((void *, const char *, ...));
572*7c478bd9Sstevel@tonic-gate     void *arg;
573*7c478bd9Sstevel@tonic-gate {
574*7c478bd9Sstevel@tonic-gate     int i, n;
575*7c478bd9Sstevel@tonic-gate     u_short proto;
576*7c478bd9Sstevel@tonic-gate     struct protent *protp;
577*7c478bd9Sstevel@tonic-gate 
578*7c478bd9Sstevel@tonic-gate     if (len >= PPP_HDRLEN && p[0] == PPP_ALLSTATIONS && p[1] == PPP_UI) {
579*7c478bd9Sstevel@tonic-gate 	p += 2;
580*7c478bd9Sstevel@tonic-gate 	GETSHORT(proto, p);
581*7c478bd9Sstevel@tonic-gate 	len -= PPP_HDRLEN;
582*7c478bd9Sstevel@tonic-gate 	for (i = 0; (protp = protocols[i]) != NULL; ++i)
583*7c478bd9Sstevel@tonic-gate 	    if (proto == protp->protocol)
584*7c478bd9Sstevel@tonic-gate 		break;
585*7c478bd9Sstevel@tonic-gate 	if (protp != NULL) {
586*7c478bd9Sstevel@tonic-gate 	    printer(arg, "[%s", protp->name);
587*7c478bd9Sstevel@tonic-gate 	    n = (*protp->printpkt)(p, len, printer, arg);
588*7c478bd9Sstevel@tonic-gate 	    printer(arg, "]");
589*7c478bd9Sstevel@tonic-gate 	    p += n;
590*7c478bd9Sstevel@tonic-gate 	    len -= n;
591*7c478bd9Sstevel@tonic-gate 	} else {
592*7c478bd9Sstevel@tonic-gate 	    for (i = 0; (protp = protocols[i]) != NULL; ++i)
593*7c478bd9Sstevel@tonic-gate 		if (proto == (protp->protocol & ~0x8000))
594*7c478bd9Sstevel@tonic-gate 		    break;
595*7c478bd9Sstevel@tonic-gate 	    if (protp != NULL && protp->data_name != NULL) {
596*7c478bd9Sstevel@tonic-gate 		printer(arg, "[%s data] %8.*B", protp->data_name, len, p);
597*7c478bd9Sstevel@tonic-gate 		len = 0;
598*7c478bd9Sstevel@tonic-gate 	    } else
599*7c478bd9Sstevel@tonic-gate 		printer(arg, "[proto=0x%x]", proto);
600*7c478bd9Sstevel@tonic-gate 	}
601*7c478bd9Sstevel@tonic-gate     }
602*7c478bd9Sstevel@tonic-gate 
603*7c478bd9Sstevel@tonic-gate     printer(arg, "%32.*B", len, p);
604*7c478bd9Sstevel@tonic-gate }
605*7c478bd9Sstevel@tonic-gate 
606*7c478bd9Sstevel@tonic-gate static void
607*7c478bd9Sstevel@tonic-gate pr_log __V((void *arg, const char *fmt, ...))
608*7c478bd9Sstevel@tonic-gate {
609*7c478bd9Sstevel@tonic-gate     int n;
610*7c478bd9Sstevel@tonic-gate     va_list pvar;
611*7c478bd9Sstevel@tonic-gate     char buf[256];
612*7c478bd9Sstevel@tonic-gate 
613*7c478bd9Sstevel@tonic-gate #if defined(__STDC__)
614*7c478bd9Sstevel@tonic-gate     va_start(pvar, fmt);
615*7c478bd9Sstevel@tonic-gate #else
616*7c478bd9Sstevel@tonic-gate     void *arg;
617*7c478bd9Sstevel@tonic-gate     const char *fmt;
618*7c478bd9Sstevel@tonic-gate     va_start(pvar);
619*7c478bd9Sstevel@tonic-gate     arg = va_arg(pvar, void *);
620*7c478bd9Sstevel@tonic-gate     fmt = va_arg(pvar, const char *);
621*7c478bd9Sstevel@tonic-gate #endif
622*7c478bd9Sstevel@tonic-gate 
623*7c478bd9Sstevel@tonic-gate     n = vslprintf(buf, sizeof(buf), fmt, pvar);
624*7c478bd9Sstevel@tonic-gate     va_end(pvar);
625*7c478bd9Sstevel@tonic-gate 
626*7c478bd9Sstevel@tonic-gate     if (linep + n + 1 > line + sizeof(line)) {
627*7c478bd9Sstevel@tonic-gate 	syslog((int)arg, "%s", line);
628*7c478bd9Sstevel@tonic-gate 	linep = line;
629*7c478bd9Sstevel@tonic-gate     }
630*7c478bd9Sstevel@tonic-gate     (void) strlcpy(linep, buf, line + sizeof(line) - linep);
631*7c478bd9Sstevel@tonic-gate     linep += n;
632*7c478bd9Sstevel@tonic-gate }
633*7c478bd9Sstevel@tonic-gate 
634*7c478bd9Sstevel@tonic-gate /*
635*7c478bd9Sstevel@tonic-gate  * print_string - print a readable representation of a string using
636*7c478bd9Sstevel@tonic-gate  * printer.
637*7c478bd9Sstevel@tonic-gate  */
638*7c478bd9Sstevel@tonic-gate void
639*7c478bd9Sstevel@tonic-gate print_string(p, len, printer, arg)
640*7c478bd9Sstevel@tonic-gate     char *p;
641*7c478bd9Sstevel@tonic-gate     int len;
642*7c478bd9Sstevel@tonic-gate     void (*printer) __P((void *, const char *, ...));
643*7c478bd9Sstevel@tonic-gate     void *arg;
644*7c478bd9Sstevel@tonic-gate {
645*7c478bd9Sstevel@tonic-gate     int c;
646*7c478bd9Sstevel@tonic-gate 
647*7c478bd9Sstevel@tonic-gate     printer(arg, "\"");
648*7c478bd9Sstevel@tonic-gate     for (; len > 0; --len) {
649*7c478bd9Sstevel@tonic-gate 	c = *p++;
650*7c478bd9Sstevel@tonic-gate 	if (isprint(c)) {
651*7c478bd9Sstevel@tonic-gate 	    if (c == '\\' || c == '"')
652*7c478bd9Sstevel@tonic-gate 		printer(arg, "\\");
653*7c478bd9Sstevel@tonic-gate 	    printer(arg, "%c", c);
654*7c478bd9Sstevel@tonic-gate 	} else {
655*7c478bd9Sstevel@tonic-gate 	    switch (c) {
656*7c478bd9Sstevel@tonic-gate 	    case '\n':
657*7c478bd9Sstevel@tonic-gate 		printer(arg, "\\n");
658*7c478bd9Sstevel@tonic-gate 		break;
659*7c478bd9Sstevel@tonic-gate 	    case '\r':
660*7c478bd9Sstevel@tonic-gate 		printer(arg, "\\r");
661*7c478bd9Sstevel@tonic-gate 		break;
662*7c478bd9Sstevel@tonic-gate 	    case '\t':
663*7c478bd9Sstevel@tonic-gate 		printer(arg, "\\t");
664*7c478bd9Sstevel@tonic-gate 		break;
665*7c478bd9Sstevel@tonic-gate 	    default:
666*7c478bd9Sstevel@tonic-gate 		printer(arg, "\\%.3o", c);
667*7c478bd9Sstevel@tonic-gate 	    }
668*7c478bd9Sstevel@tonic-gate 	}
669*7c478bd9Sstevel@tonic-gate     }
670*7c478bd9Sstevel@tonic-gate     printer(arg, "\"");
671*7c478bd9Sstevel@tonic-gate }
672*7c478bd9Sstevel@tonic-gate 
673*7c478bd9Sstevel@tonic-gate /*
674*7c478bd9Sstevel@tonic-gate  * logit - does the hard work for fatal et al.
675*7c478bd9Sstevel@tonic-gate  */
676*7c478bd9Sstevel@tonic-gate static void
677*7c478bd9Sstevel@tonic-gate logit(level, fmt, args)
678*7c478bd9Sstevel@tonic-gate     int level;
679*7c478bd9Sstevel@tonic-gate     const char *fmt;
680*7c478bd9Sstevel@tonic-gate     va_list args;
681*7c478bd9Sstevel@tonic-gate {
682*7c478bd9Sstevel@tonic-gate     int n;
683*7c478bd9Sstevel@tonic-gate     char buf[1024];
684*7c478bd9Sstevel@tonic-gate 
685*7c478bd9Sstevel@tonic-gate     n = vslprintf(buf, sizeof(buf), fmt, args);
686*7c478bd9Sstevel@tonic-gate     syslog(level, "%s", buf);
687*7c478bd9Sstevel@tonic-gate     if (log_to_fd >= 0 && (level != LOG_DEBUG || debug) &&
688*7c478bd9Sstevel@tonic-gate 	(!early_log || log_to_specific_fd)) {
689*7c478bd9Sstevel@tonic-gate 	if (buf[n-1] != '\n')
690*7c478bd9Sstevel@tonic-gate 	    buf[n++] = '\n';
691*7c478bd9Sstevel@tonic-gate 	if (write(log_to_fd, buf, n) != n)
692*7c478bd9Sstevel@tonic-gate 	    log_to_fd = -1;
693*7c478bd9Sstevel@tonic-gate     }
694*7c478bd9Sstevel@tonic-gate }
695*7c478bd9Sstevel@tonic-gate 
696*7c478bd9Sstevel@tonic-gate /*
697*7c478bd9Sstevel@tonic-gate  * fatal - log an error message and die horribly.
698*7c478bd9Sstevel@tonic-gate  */
699*7c478bd9Sstevel@tonic-gate void
700*7c478bd9Sstevel@tonic-gate fatal __V((const char *fmt, ...))
701*7c478bd9Sstevel@tonic-gate {
702*7c478bd9Sstevel@tonic-gate     va_list pvar;
703*7c478bd9Sstevel@tonic-gate 
704*7c478bd9Sstevel@tonic-gate #if defined(__STDC__)
705*7c478bd9Sstevel@tonic-gate     va_start(pvar, fmt);
706*7c478bd9Sstevel@tonic-gate #else
707*7c478bd9Sstevel@tonic-gate     const char *fmt;
708*7c478bd9Sstevel@tonic-gate     va_start(pvar);
709*7c478bd9Sstevel@tonic-gate     fmt = va_arg(pvar, const char *);
710*7c478bd9Sstevel@tonic-gate #endif
711*7c478bd9Sstevel@tonic-gate 
712*7c478bd9Sstevel@tonic-gate     logit(LOG_ERR, fmt, pvar);
713*7c478bd9Sstevel@tonic-gate     va_end(pvar);
714*7c478bd9Sstevel@tonic-gate 
715*7c478bd9Sstevel@tonic-gate     die(1);			/* as promised */
716*7c478bd9Sstevel@tonic-gate }
717*7c478bd9Sstevel@tonic-gate 
718*7c478bd9Sstevel@tonic-gate /*
719*7c478bd9Sstevel@tonic-gate  * error - log an error message.
720*7c478bd9Sstevel@tonic-gate  */
721*7c478bd9Sstevel@tonic-gate void
722*7c478bd9Sstevel@tonic-gate error __V((const char *fmt, ...))
723*7c478bd9Sstevel@tonic-gate {
724*7c478bd9Sstevel@tonic-gate     va_list pvar;
725*7c478bd9Sstevel@tonic-gate 
726*7c478bd9Sstevel@tonic-gate #if defined(__STDC__)
727*7c478bd9Sstevel@tonic-gate     va_start(pvar, fmt);
728*7c478bd9Sstevel@tonic-gate #else
729*7c478bd9Sstevel@tonic-gate     const char *fmt;
730*7c478bd9Sstevel@tonic-gate     va_start(pvar);
731*7c478bd9Sstevel@tonic-gate     fmt = va_arg(pvar, const char *);
732*7c478bd9Sstevel@tonic-gate #endif
733*7c478bd9Sstevel@tonic-gate 
734*7c478bd9Sstevel@tonic-gate     logit(LOG_ERR, fmt, pvar);
735*7c478bd9Sstevel@tonic-gate     va_end(pvar);
736*7c478bd9Sstevel@tonic-gate }
737*7c478bd9Sstevel@tonic-gate 
738*7c478bd9Sstevel@tonic-gate /*
739*7c478bd9Sstevel@tonic-gate  * warn - log a warning message.
740*7c478bd9Sstevel@tonic-gate  */
741*7c478bd9Sstevel@tonic-gate void
742*7c478bd9Sstevel@tonic-gate warn __V((const char *fmt, ...))
743*7c478bd9Sstevel@tonic-gate {
744*7c478bd9Sstevel@tonic-gate     va_list pvar;
745*7c478bd9Sstevel@tonic-gate 
746*7c478bd9Sstevel@tonic-gate #if defined(__STDC__)
747*7c478bd9Sstevel@tonic-gate     va_start(pvar, fmt);
748*7c478bd9Sstevel@tonic-gate #else
749*7c478bd9Sstevel@tonic-gate     const char *fmt;
750*7c478bd9Sstevel@tonic-gate     va_start(pvar);
751*7c478bd9Sstevel@tonic-gate     fmt = va_arg(pvar, const char *);
752*7c478bd9Sstevel@tonic-gate #endif
753*7c478bd9Sstevel@tonic-gate 
754*7c478bd9Sstevel@tonic-gate     logit(LOG_WARNING, fmt, pvar);
755*7c478bd9Sstevel@tonic-gate     va_end(pvar);
756*7c478bd9Sstevel@tonic-gate }
757*7c478bd9Sstevel@tonic-gate 
758*7c478bd9Sstevel@tonic-gate /*
759*7c478bd9Sstevel@tonic-gate  * notice - log a notice-level message.
760*7c478bd9Sstevel@tonic-gate  */
761*7c478bd9Sstevel@tonic-gate void
762*7c478bd9Sstevel@tonic-gate notice __V((const char *fmt, ...))
763*7c478bd9Sstevel@tonic-gate {
764*7c478bd9Sstevel@tonic-gate     va_list pvar;
765*7c478bd9Sstevel@tonic-gate 
766*7c478bd9Sstevel@tonic-gate #if defined(__STDC__)
767*7c478bd9Sstevel@tonic-gate     va_start(pvar, fmt);
768*7c478bd9Sstevel@tonic-gate #else
769*7c478bd9Sstevel@tonic-gate     const char *fmt;
770*7c478bd9Sstevel@tonic-gate     va_start(pvar);
771*7c478bd9Sstevel@tonic-gate     fmt = va_arg(pvar, const char *);
772*7c478bd9Sstevel@tonic-gate #endif
773*7c478bd9Sstevel@tonic-gate 
774*7c478bd9Sstevel@tonic-gate     logit(LOG_NOTICE, fmt, pvar);
775*7c478bd9Sstevel@tonic-gate     va_end(pvar);
776*7c478bd9Sstevel@tonic-gate }
777*7c478bd9Sstevel@tonic-gate 
778*7c478bd9Sstevel@tonic-gate /*
779*7c478bd9Sstevel@tonic-gate  * info - log an informational message.
780*7c478bd9Sstevel@tonic-gate  */
781*7c478bd9Sstevel@tonic-gate void
782*7c478bd9Sstevel@tonic-gate info __V((const char *fmt, ...))
783*7c478bd9Sstevel@tonic-gate {
784*7c478bd9Sstevel@tonic-gate     va_list pvar;
785*7c478bd9Sstevel@tonic-gate 
786*7c478bd9Sstevel@tonic-gate #if defined(__STDC__)
787*7c478bd9Sstevel@tonic-gate     va_start(pvar, fmt);
788*7c478bd9Sstevel@tonic-gate #else
789*7c478bd9Sstevel@tonic-gate     const char *fmt;
790*7c478bd9Sstevel@tonic-gate     va_start(pvar);
791*7c478bd9Sstevel@tonic-gate     fmt = va_arg(pvar, const char *);
792*7c478bd9Sstevel@tonic-gate #endif
793*7c478bd9Sstevel@tonic-gate 
794*7c478bd9Sstevel@tonic-gate     logit(LOG_INFO, fmt, pvar);
795*7c478bd9Sstevel@tonic-gate     va_end(pvar);
796*7c478bd9Sstevel@tonic-gate }
797*7c478bd9Sstevel@tonic-gate 
798*7c478bd9Sstevel@tonic-gate /*
799*7c478bd9Sstevel@tonic-gate  * dbglog - log a debug message.
800*7c478bd9Sstevel@tonic-gate  */
801*7c478bd9Sstevel@tonic-gate void
802*7c478bd9Sstevel@tonic-gate dbglog __V((const char *fmt, ...))
803*7c478bd9Sstevel@tonic-gate {
804*7c478bd9Sstevel@tonic-gate     va_list pvar;
805*7c478bd9Sstevel@tonic-gate 
806*7c478bd9Sstevel@tonic-gate #if defined(__STDC__)
807*7c478bd9Sstevel@tonic-gate     va_start(pvar, fmt);
808*7c478bd9Sstevel@tonic-gate #else
809*7c478bd9Sstevel@tonic-gate     const char *fmt;
810*7c478bd9Sstevel@tonic-gate     va_start(pvar);
811*7c478bd9Sstevel@tonic-gate     fmt = va_arg(pvar, const char *);
812*7c478bd9Sstevel@tonic-gate #endif
813*7c478bd9Sstevel@tonic-gate 
814*7c478bd9Sstevel@tonic-gate     logit(LOG_DEBUG, fmt, pvar);
815*7c478bd9Sstevel@tonic-gate     va_end(pvar);
816*7c478bd9Sstevel@tonic-gate }
817*7c478bd9Sstevel@tonic-gate 
818*7c478bd9Sstevel@tonic-gate /*
819*7c478bd9Sstevel@tonic-gate  * Code names for regular PPP messages.  Used by LCP and most NCPs,
820*7c478bd9Sstevel@tonic-gate  * not used by authentication protocols.
821*7c478bd9Sstevel@tonic-gate  */
822*7c478bd9Sstevel@tonic-gate const char *
823*7c478bd9Sstevel@tonic-gate code_name(int code, int shortflag)
824*7c478bd9Sstevel@tonic-gate {
825*7c478bd9Sstevel@tonic-gate     static const char *codelist[] = {
826*7c478bd9Sstevel@tonic-gate 	"Vendor-Extension", "Configure-Request", "Configure-Ack",
827*7c478bd9Sstevel@tonic-gate 	"Configure-Nak", "Configure-Reject", "Terminate-Request",
828*7c478bd9Sstevel@tonic-gate 	"Terminate-Ack", "Code-Reject", "Protocol-Reject",
829*7c478bd9Sstevel@tonic-gate 	"Echo-Request", "Echo-Reply", "Discard-Request",
830*7c478bd9Sstevel@tonic-gate 	"Identification", "Time-Remaining",
831*7c478bd9Sstevel@tonic-gate 	"Reset-Request", "Reset-Ack"
832*7c478bd9Sstevel@tonic-gate     };
833*7c478bd9Sstevel@tonic-gate     static const char *shortcode[] = {
834*7c478bd9Sstevel@tonic-gate 	"VendExt", "ConfReq", "ConfAck",
835*7c478bd9Sstevel@tonic-gate 	"ConfNak", "ConfRej", "TermReq",
836*7c478bd9Sstevel@tonic-gate 	"TermAck", "CodeRej", "ProtRej",
837*7c478bd9Sstevel@tonic-gate 	"EchoReq", "EchoRep", "DiscReq",
838*7c478bd9Sstevel@tonic-gate 	"Ident", "TimeRem",
839*7c478bd9Sstevel@tonic-gate 	"ResetReq", "ResetAck"
840*7c478bd9Sstevel@tonic-gate     };
841*7c478bd9Sstevel@tonic-gate     static char msgbuf[64];
842*7c478bd9Sstevel@tonic-gate 
843*7c478bd9Sstevel@tonic-gate     if (code < 0 || code >= sizeof (codelist) / sizeof (*codelist)) {
844*7c478bd9Sstevel@tonic-gate 	if (shortflag)
845*7c478bd9Sstevel@tonic-gate 	    (void) slprintf(msgbuf, sizeof (msgbuf), "Code#%d", code);
846*7c478bd9Sstevel@tonic-gate 	else
847*7c478bd9Sstevel@tonic-gate 	    (void) slprintf(msgbuf, sizeof (msgbuf), "unknown code %d", code);
848*7c478bd9Sstevel@tonic-gate 	return ((const char *)msgbuf);
849*7c478bd9Sstevel@tonic-gate     }
850*7c478bd9Sstevel@tonic-gate     return (shortflag ? shortcode[code] : codelist[code]);
851*7c478bd9Sstevel@tonic-gate }
852*7c478bd9Sstevel@tonic-gate 
853*7c478bd9Sstevel@tonic-gate /* Procedures for locking the serial device using a lock file. */
854*7c478bd9Sstevel@tonic-gate #ifndef LOCK_DIR
855*7c478bd9Sstevel@tonic-gate #ifdef _linux_
856*7c478bd9Sstevel@tonic-gate #define LOCK_DIR	"/var/lock"
857*7c478bd9Sstevel@tonic-gate #else
858*7c478bd9Sstevel@tonic-gate #ifdef SVR4
859*7c478bd9Sstevel@tonic-gate #define LOCK_DIR	"/var/spool/locks"
860*7c478bd9Sstevel@tonic-gate #else
861*7c478bd9Sstevel@tonic-gate #define LOCK_DIR	"/var/spool/lock"
862*7c478bd9Sstevel@tonic-gate #endif
863*7c478bd9Sstevel@tonic-gate #endif
864*7c478bd9Sstevel@tonic-gate #endif /* LOCK_DIR */
865*7c478bd9Sstevel@tonic-gate 
866*7c478bd9Sstevel@tonic-gate static char lock_file[MAXPATHLEN];
867*7c478bd9Sstevel@tonic-gate 
868*7c478bd9Sstevel@tonic-gate /*
869*7c478bd9Sstevel@tonic-gate  * lock - create a lock file for the named device
870*7c478bd9Sstevel@tonic-gate  */
871*7c478bd9Sstevel@tonic-gate int
872*7c478bd9Sstevel@tonic-gate lock(dev)
873*7c478bd9Sstevel@tonic-gate     char *dev;
874*7c478bd9Sstevel@tonic-gate {
875*7c478bd9Sstevel@tonic-gate #ifdef LOCKLIB
876*7c478bd9Sstevel@tonic-gate     int result;
877*7c478bd9Sstevel@tonic-gate 
878*7c478bd9Sstevel@tonic-gate     result = mklock (dev, (void *) 0);
879*7c478bd9Sstevel@tonic-gate     if (result == 0) {
880*7c478bd9Sstevel@tonic-gate 	(void) strlcpy(lock_file, sizeof(lock_file), dev);
881*7c478bd9Sstevel@tonic-gate 	return (0);
882*7c478bd9Sstevel@tonic-gate     }
883*7c478bd9Sstevel@tonic-gate 
884*7c478bd9Sstevel@tonic-gate     if (result > 0)
885*7c478bd9Sstevel@tonic-gate         notice("Device %s is locked by pid %d", dev, result);
886*7c478bd9Sstevel@tonic-gate     else
887*7c478bd9Sstevel@tonic-gate 	error("Can't create lock file %s", lock_file);
888*7c478bd9Sstevel@tonic-gate     return (-1);
889*7c478bd9Sstevel@tonic-gate 
890*7c478bd9Sstevel@tonic-gate #else /* LOCKLIB */
891*7c478bd9Sstevel@tonic-gate 
892*7c478bd9Sstevel@tonic-gate     char lock_buffer[12];
893*7c478bd9Sstevel@tonic-gate     int fd, pid, n;
894*7c478bd9Sstevel@tonic-gate 
895*7c478bd9Sstevel@tonic-gate #ifdef SVR4
896*7c478bd9Sstevel@tonic-gate     struct stat sbuf;
897*7c478bd9Sstevel@tonic-gate 
898*7c478bd9Sstevel@tonic-gate     if (stat(dev, &sbuf) < 0) {
899*7c478bd9Sstevel@tonic-gate 	error("Can't get device number for %s: %m", dev);
900*7c478bd9Sstevel@tonic-gate 	return (-1);
901*7c478bd9Sstevel@tonic-gate     }
902*7c478bd9Sstevel@tonic-gate     if ((sbuf.st_mode & S_IFMT) != S_IFCHR) {
903*7c478bd9Sstevel@tonic-gate 	error("Can't lock %s: not a character device", dev);
904*7c478bd9Sstevel@tonic-gate 	return (-1);
905*7c478bd9Sstevel@tonic-gate     }
906*7c478bd9Sstevel@tonic-gate     (void) slprintf(lock_file, sizeof(lock_file), "%s/LK.%03d.%03d.%03d",
907*7c478bd9Sstevel@tonic-gate 	     LOCK_DIR, major(sbuf.st_dev),
908*7c478bd9Sstevel@tonic-gate 	     major(sbuf.st_rdev), minor(sbuf.st_rdev));
909*7c478bd9Sstevel@tonic-gate #else
910*7c478bd9Sstevel@tonic-gate     char *p;
911*7c478bd9Sstevel@tonic-gate 
912*7c478bd9Sstevel@tonic-gate     if ((p = strrchr(dev, '/')) != NULL)
913*7c478bd9Sstevel@tonic-gate 	dev = p + 1;
914*7c478bd9Sstevel@tonic-gate     (void) slprintf(lock_file, sizeof(lock_file), "%s/LCK..%s", LOCK_DIR, dev);
915*7c478bd9Sstevel@tonic-gate #endif
916*7c478bd9Sstevel@tonic-gate 
917*7c478bd9Sstevel@tonic-gate     while ((fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644)) < 0) {
918*7c478bd9Sstevel@tonic-gate 	if (errno != EEXIST) {
919*7c478bd9Sstevel@tonic-gate 	    error("Can't create lock file %s: %m", lock_file);
920*7c478bd9Sstevel@tonic-gate 	    break;
921*7c478bd9Sstevel@tonic-gate 	}
922*7c478bd9Sstevel@tonic-gate 
923*7c478bd9Sstevel@tonic-gate 	/* Read the lock file to find out who has the device locked. */
924*7c478bd9Sstevel@tonic-gate 	fd = open(lock_file, O_RDONLY, 0);
925*7c478bd9Sstevel@tonic-gate 	if (fd < 0) {
926*7c478bd9Sstevel@tonic-gate 	    if (errno == ENOENT) /* This is just a timing problem. */
927*7c478bd9Sstevel@tonic-gate 		continue;
928*7c478bd9Sstevel@tonic-gate 	    error("Can't open existing lock file %s: %m", lock_file);
929*7c478bd9Sstevel@tonic-gate 	    break;
930*7c478bd9Sstevel@tonic-gate 	}
931*7c478bd9Sstevel@tonic-gate #ifndef LOCK_BINARY
932*7c478bd9Sstevel@tonic-gate 	n = read(fd, lock_buffer, 11);
933*7c478bd9Sstevel@tonic-gate #else
934*7c478bd9Sstevel@tonic-gate 	n = read(fd, &pid, sizeof(pid));
935*7c478bd9Sstevel@tonic-gate #endif /* LOCK_BINARY */
936*7c478bd9Sstevel@tonic-gate 	(void) close(fd);
937*7c478bd9Sstevel@tonic-gate 	fd = -1;
938*7c478bd9Sstevel@tonic-gate 	if (n <= 0) {
939*7c478bd9Sstevel@tonic-gate 	    error("Can't read pid from lock file %s", lock_file);
940*7c478bd9Sstevel@tonic-gate 	    break;
941*7c478bd9Sstevel@tonic-gate 	}
942*7c478bd9Sstevel@tonic-gate 
943*7c478bd9Sstevel@tonic-gate 	/* See if the process still exists. */
944*7c478bd9Sstevel@tonic-gate #ifndef LOCK_BINARY
945*7c478bd9Sstevel@tonic-gate 	lock_buffer[n] = 0;
946*7c478bd9Sstevel@tonic-gate 	pid = atoi(lock_buffer);
947*7c478bd9Sstevel@tonic-gate #endif /* LOCK_BINARY */
948*7c478bd9Sstevel@tonic-gate 	if (pid == getpid())
949*7c478bd9Sstevel@tonic-gate 	    return (1);		/* somebody else locked it for us */
950*7c478bd9Sstevel@tonic-gate 	if (pid == 0
951*7c478bd9Sstevel@tonic-gate 	    || (kill(pid, 0) == -1 && errno == ESRCH)) {
952*7c478bd9Sstevel@tonic-gate 	    if (unlink (lock_file) == 0) {
953*7c478bd9Sstevel@tonic-gate 		notice("Removed stale lock on %s (pid %d)", dev, pid);
954*7c478bd9Sstevel@tonic-gate 		continue;
955*7c478bd9Sstevel@tonic-gate 	    }
956*7c478bd9Sstevel@tonic-gate 	    warn("Couldn't remove stale lock on %s", dev);
957*7c478bd9Sstevel@tonic-gate 	} else
958*7c478bd9Sstevel@tonic-gate 	    notice("Device %s is locked by pid %d", dev, pid);
959*7c478bd9Sstevel@tonic-gate 	break;
960*7c478bd9Sstevel@tonic-gate     }
961*7c478bd9Sstevel@tonic-gate 
962*7c478bd9Sstevel@tonic-gate     if (fd < 0) {
963*7c478bd9Sstevel@tonic-gate 	lock_file[0] = 0;
964*7c478bd9Sstevel@tonic-gate 	return (-1);
965*7c478bd9Sstevel@tonic-gate     }
966*7c478bd9Sstevel@tonic-gate 
967*7c478bd9Sstevel@tonic-gate     pid = getpid();
968*7c478bd9Sstevel@tonic-gate #ifndef LOCK_BINARY
969*7c478bd9Sstevel@tonic-gate     (void) slprintf(lock_buffer, sizeof(lock_buffer), "%10d\n", pid);
970*7c478bd9Sstevel@tonic-gate     (void) write (fd, lock_buffer, 11);
971*7c478bd9Sstevel@tonic-gate #else
972*7c478bd9Sstevel@tonic-gate     (void) write(fd, &pid, sizeof (pid));
973*7c478bd9Sstevel@tonic-gate #endif
974*7c478bd9Sstevel@tonic-gate     (void) close(fd);
975*7c478bd9Sstevel@tonic-gate     return (0);
976*7c478bd9Sstevel@tonic-gate 
977*7c478bd9Sstevel@tonic-gate #endif
978*7c478bd9Sstevel@tonic-gate }
979*7c478bd9Sstevel@tonic-gate 
980*7c478bd9Sstevel@tonic-gate /*
981*7c478bd9Sstevel@tonic-gate  * relock - called to update our lockfile when we are about to detach,
982*7c478bd9Sstevel@tonic-gate  * thus changing our pid (we fork, the child carries on, and the parent dies).
983*7c478bd9Sstevel@tonic-gate  * Note that this is called by the parent, with pid equal to the pid
984*7c478bd9Sstevel@tonic-gate  * of the child.  This avoids a potential race which would exist if
985*7c478bd9Sstevel@tonic-gate  * we had the child rewrite the lockfile (the parent might die first,
986*7c478bd9Sstevel@tonic-gate  * and another process could think the lock was stale if it checked
987*7c478bd9Sstevel@tonic-gate  * between when the parent died and the child rewrote the lockfile).
988*7c478bd9Sstevel@tonic-gate  */
989*7c478bd9Sstevel@tonic-gate int
990*7c478bd9Sstevel@tonic-gate relock(pid)
991*7c478bd9Sstevel@tonic-gate     int pid;
992*7c478bd9Sstevel@tonic-gate {
993*7c478bd9Sstevel@tonic-gate #ifdef LOCKLIB
994*7c478bd9Sstevel@tonic-gate     /* XXX is there a way to do this? */
995*7c478bd9Sstevel@tonic-gate     return (-1);
996*7c478bd9Sstevel@tonic-gate #else /* LOCKLIB */
997*7c478bd9Sstevel@tonic-gate 
998*7c478bd9Sstevel@tonic-gate     int fd;
999*7c478bd9Sstevel@tonic-gate     char lock_buffer[12];
1000*7c478bd9Sstevel@tonic-gate 
1001*7c478bd9Sstevel@tonic-gate     if (lock_file[0] == 0)
1002*7c478bd9Sstevel@tonic-gate 	return (-1);
1003*7c478bd9Sstevel@tonic-gate     fd = open(lock_file, O_WRONLY, 0);
1004*7c478bd9Sstevel@tonic-gate     if (fd < 0) {
1005*7c478bd9Sstevel@tonic-gate 	error("Couldn't reopen lock file %s: %m", lock_file);
1006*7c478bd9Sstevel@tonic-gate 	lock_file[0] = 0;
1007*7c478bd9Sstevel@tonic-gate 	return (-1);
1008*7c478bd9Sstevel@tonic-gate     }
1009*7c478bd9Sstevel@tonic-gate 
1010*7c478bd9Sstevel@tonic-gate #ifndef LOCK_BINARY
1011*7c478bd9Sstevel@tonic-gate     (void) slprintf(lock_buffer, sizeof(lock_buffer), "%10d\n", pid);
1012*7c478bd9Sstevel@tonic-gate     (void) write (fd, lock_buffer, 11);
1013*7c478bd9Sstevel@tonic-gate #else
1014*7c478bd9Sstevel@tonic-gate     (void) write(fd, &pid, sizeof(pid));
1015*7c478bd9Sstevel@tonic-gate #endif /* LOCK_BINARY */
1016*7c478bd9Sstevel@tonic-gate     (void) close(fd);
1017*7c478bd9Sstevel@tonic-gate     return (0);
1018*7c478bd9Sstevel@tonic-gate 
1019*7c478bd9Sstevel@tonic-gate #endif /* LOCKLIB */
1020*7c478bd9Sstevel@tonic-gate }
1021*7c478bd9Sstevel@tonic-gate 
1022*7c478bd9Sstevel@tonic-gate /*
1023*7c478bd9Sstevel@tonic-gate  * unlock - remove our lockfile
1024*7c478bd9Sstevel@tonic-gate  */
1025*7c478bd9Sstevel@tonic-gate void
1026*7c478bd9Sstevel@tonic-gate unlock()
1027*7c478bd9Sstevel@tonic-gate {
1028*7c478bd9Sstevel@tonic-gate     if (lock_file[0]) {
1029*7c478bd9Sstevel@tonic-gate #ifdef LOCKLIB
1030*7c478bd9Sstevel@tonic-gate 	(void) rmlock(lock_file, (void *) 0);
1031*7c478bd9Sstevel@tonic-gate #else
1032*7c478bd9Sstevel@tonic-gate 	(void) unlink(lock_file);
1033*7c478bd9Sstevel@tonic-gate #endif
1034*7c478bd9Sstevel@tonic-gate 	lock_file[0] = 0;
1035*7c478bd9Sstevel@tonic-gate     }
1036*7c478bd9Sstevel@tonic-gate }
1037*7c478bd9Sstevel@tonic-gate 
1038*7c478bd9Sstevel@tonic-gate const char *
1039*7c478bd9Sstevel@tonic-gate signal_name(int signum)
1040*7c478bd9Sstevel@tonic-gate {
1041*7c478bd9Sstevel@tonic-gate #if defined(SOL2) || defined(__linux__) || defined(_linux_)
1042*7c478bd9Sstevel@tonic-gate     const char *cp;
1043*7c478bd9Sstevel@tonic-gate 
1044*7c478bd9Sstevel@tonic-gate     if ((cp = strsignal(signum)) != NULL)
1045*7c478bd9Sstevel@tonic-gate 	return (cp);
1046*7c478bd9Sstevel@tonic-gate #else
1047*7c478bd9Sstevel@tonic-gate     extern char *sys_siglist[];
1048*7c478bd9Sstevel@tonic-gate     extern int sys_nsig;
1049*7c478bd9Sstevel@tonic-gate 
1050*7c478bd9Sstevel@tonic-gate     if (signum >= 0 && signum < sys_nsig && sys_siglist[signum] != NULL)
1051*7c478bd9Sstevel@tonic-gate 	return (sys_siglist[signum]);
1052*7c478bd9Sstevel@tonic-gate #endif
1053*7c478bd9Sstevel@tonic-gate     return ("??");
1054*7c478bd9Sstevel@tonic-gate }
1055