2 * utils.c - various utility functions used in pppd.
4 * Copyright (c) 1999 The Australian National University.
7 * Redistribution and use in source and binary forms are permitted
8 * provided that the above copyright notice and this paragraph are
9 * duplicated in all such forms and that any documentation,
10 * advertising materials, and other materials related to such
11 * distribution and use acknowledge that the software was developed
12 * by the Australian National University. The name of the University
13 * may not be used to endorse or promote products derived from this
14 * software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
17 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20 #define RCSID "$Id: utils.c,v 1.1.1.4 2003/10/14 08:09:53 sparq Exp $"
34 #include <sys/param.h>
35 #include <sys/types.h>
38 #include <sys/resource.h>
40 #include <sys/socket.h>
41 #include <netinet/in.h>
43 #include <sys/mkdev.h>
48 static const char rcsid
[] = RCSID
;
51 extern char *strerror();
54 static void logit
__P((int, char *, va_list));
55 static void log_write
__P((int, char *));
56 static void vslp_printer
__P((void *, char *, ...));
57 static void format_packet
__P((u_char
*, int, void (*) (void *, char *, ...),
66 * strlcpy - like strcpy/strncpy, doesn't overflow destination buffer,
67 * always leaves destination null-terminated (for len > 0).
70 strlcpy(dest
, src
, len
)
75 size_t ret
= strlen(src
);
81 strncpy(dest
, src
, len
- 1);
89 * strlcat - like strcat/strncat, doesn't overflow destination buffer,
90 * always leaves destination null-terminated (for len > 0).
93 strlcat(dest
, src
, len
)
98 size_t dlen
= strlen(dest
);
100 return dlen
+ strlcpy(dest
+ dlen
, src
, (len
> dlen
? len
- dlen
: 0));
105 * slprintf - format a message into a buffer. Like sprintf except we
106 * also specify the length of the output buffer, and we handle
107 * %r (recursive format), %m (error message), %v (visible string),
108 * %q (quoted string), %t (current time) and %I (IP address) formats.
109 * Doesn't do floating-point formats.
110 * Returns the number of chars put into buf.
113 slprintf
__V((char *buf
, int buflen
, char *fmt
, ...))
118 #if defined(__STDC__)
125 buf
= va_arg(args
, char *);
126 buflen
= va_arg(args
, int);
127 fmt
= va_arg(args
, char *);
129 n
= vslprintf(buf
, buflen
, fmt
, args
);
135 * vslprintf - like slprintf, takes a va_list instead of a list of args.
137 #define OUTCHAR(c) (buflen > 0? (--buflen, *buf++ = (c)): 0)
140 vslprintf(buf
, buflen
, fmt
, args
)
147 int width
, prec
, fillch
;
148 int base
, len
, neg
, quoted
;
149 unsigned long val
= 0;
150 char *str
, *f
, *buf0
;
155 static char hexchars
[] = "0123456789abcdef";
156 struct buffer_info bufinfo
;
161 for (f
= fmt
; *f
!= '%' && *f
!= 0; ++f
)
167 memcpy(buf
, fmt
, len
);
183 width
= va_arg(args
, int);
187 width
= width
* 10 + c
- '0';
194 prec
= va_arg(args
, int);
199 prec
= prec
* 10 + c
- '0';
210 i
= va_arg(args
, int);
219 val
= va_arg(args
, unsigned int);
223 val
= va_arg(args
, unsigned int);
228 val
= va_arg(args
, unsigned int);
232 val
= (unsigned long) va_arg(args
, void *);
237 str
= va_arg(args
, char *);
240 num
[0] = va_arg(args
, int);
245 str
= strerror(errno
);
248 ip
= va_arg(args
, u_int32_t
);
250 slprintf(num
, sizeof(num
), "%d.%d.%d.%d", (ip
>> 24) & 0xff,
251 (ip
>> 16) & 0xff, (ip
>> 8) & 0xff, ip
& 0xff);
255 f
= va_arg(args
, char *);
257 n
= vslprintf(buf
, buflen
+ 1, f
, va_arg(args
, va_list));
259 /* On the powerpc, a va_list is an array of 1 structure */
260 n
= vslprintf(buf
, buflen
+ 1, f
, va_arg(args
, void *));
268 str
+= 4; /* chop off the day name */
269 str
[15] = 0; /* chop off year and newline */
271 case 'v': /* "visible" string */
272 case 'q': /* quoted string */
274 p
= va_arg(args
, unsigned char *);
275 if (fillch
== '0' && prec
>= 0) {
278 n
= strlen((char *)p
);
279 if (prec
>= 0 && n
> prec
)
282 while (n
> 0 && buflen
> 0) {
285 if (!quoted
&& c
>= 0x80) {
290 if (quoted
&& (c
== '"' || c
== '\\'))
292 if (c
< 0x20 || (0x7f <= c
&& c
< 0xa0)) {
296 case '\t': OUTCHAR('t'); break;
297 case '\n': OUTCHAR('n'); break;
298 case '\b': OUTCHAR('b'); break;
299 case '\f': OUTCHAR('f'); break;
302 OUTCHAR(hexchars
[c
>> 4]);
303 OUTCHAR(hexchars
[c
& 0xf]);
317 case 'P': /* print PPP packet */
319 bufinfo
.len
= buflen
+ 1;
320 p
= va_arg(args
, unsigned char *);
321 n
= va_arg(args
, int);
322 format_packet(p
, n
, vslp_printer
, &bufinfo
);
324 buflen
= bufinfo
.len
- 1;
327 p
= va_arg(args
, unsigned char *);
328 for (n
= prec
; n
> 0; --n
) {
332 OUTCHAR(hexchars
[(c
>> 4) & 0xf]);
333 OUTCHAR(hexchars
[c
& 0xf]);
339 --fmt
; /* so %z outputs %z etc. */
344 str
= num
+ sizeof(num
);
346 while (str
> num
+ neg
) {
347 *--str
= hexchars
[val
% base
];
349 if (--prec
<= 0 && val
== 0)
361 len
= num
+ sizeof(num
) - 1 - str
;
364 if (prec
>= 0 && len
> prec
)
370 if ((n
= width
- len
) > 0) {
378 memcpy(buf
, str
, len
);
387 * vslp_printer - used in processing a %P format
390 vslp_printer
__V((void *arg
, char *fmt
, ...))
394 struct buffer_info
*bi
;
396 #if defined(__STDC__)
402 arg
= va_arg(pvar
, void *);
403 fmt
= va_arg(pvar
, char *);
406 bi
= (struct buffer_info
*) arg
;
407 n
= vslprintf(bi
->ptr
, bi
->len
, fmt
, pvar
);
416 * log_packet - format a packet and log it.
420 log_packet(p
, len
, prefix
, level
)
426 init_pr_log(prefix
, level
);
427 format_packet(p
, len
, pr_log
, &level
);
433 * format_packet - make a readable representation of a packet,
434 * calling `printer(arg, format, ...)' to output it.
437 format_packet(p
, len
, printer
, arg
)
440 void (*printer
) __P((void *, char *, ...));
445 struct protent
*protp
;
447 if (len
>= PPP_HDRLEN
&& p
[0] == PPP_ALLSTATIONS
&& p
[1] == PPP_UI
) {
451 for (i
= 0; (protp
= protocols
[i
]) != NULL
; ++i
)
452 if (proto
== protp
->protocol
)
455 printer(arg
, "[%s", protp
->name
);
456 n
= (*protp
->printpkt
)(p
, len
, printer
, arg
);
461 for (i
= 0; (protp
= protocols
[i
]) != NULL
; ++i
)
462 if (proto
== (protp
->protocol
& ~0x8000))
464 if (protp
!= 0 && protp
->data_name
!= 0) {
465 printer(arg
, "[%s data]", protp
->data_name
);
467 printer(arg
, "%.8B ...", p
);
469 printer(arg
, "%.*B", len
, p
);
472 printer(arg
, "[proto=0x%x]", proto
);
477 printer(arg
, "%.32B ...", p
);
479 printer(arg
, "%.*B", len
, p
);
483 * init_pr_log, end_pr_log - initialize and finish use of pr_log.
486 static char line
[256]; /* line to be logged accumulated here */
487 static char *linep
; /* current pointer within line */
488 static int llevel
; /* level for logging */
491 init_pr_log(prefix
, level
)
496 if (prefix
!= NULL
) {
497 strlcpy(line
, prefix
, sizeof(line
));
498 linep
= line
+ strlen(line
);
508 log_write(llevel
, line
);
513 * pr_log - printer routine for outputting to syslog
516 pr_log
__V((void *arg
, char *fmt
, ...))
523 #if defined(__STDC__)
529 arg
= va_arg(pvar
, void *);
530 fmt
= va_arg(pvar
, char *);
533 n
= vslprintf(buf
, sizeof(buf
), fmt
, pvar
);
537 eol
= strchr(buf
, '\n');
539 l
= (eol
== NULL
)? n
: eol
- buf
;
540 if (linep
+ l
< line
+ sizeof(line
)) {
542 memcpy(linep
, buf
, l
);
548 eol
= strchr(p
, '\n');
551 log_write(llevel
, line
);
555 while (eol
!= NULL
) {
557 log_write(llevel
, p
);
559 eol
= strchr(p
, '\n');
562 /* assumes sizeof(buf) <= sizeof(line) */
571 * print_string - print a readable representation of a string using
575 print_string(p
, len
, printer
, arg
)
578 void (*printer
) __P((void *, char *, ...));
584 for (; len
> 0; --len
) {
586 if (' ' <= c
&& c
<= '~') {
587 if (c
== '\\' || c
== '"')
589 printer(arg
, "%c", c
);
602 printer(arg
, "\\%.3o", c
);
610 * logit - does the hard work for fatal et al.
613 logit(level
, fmt
, args
)
621 n
= vslprintf(buf
, sizeof(buf
), fmt
, args
);
622 log_write(level
, buf
);
626 log_write(level
, buf
)
630 syslog(level
, "%s", buf
);
631 if (log_to_fd
>= 0 && (level
!= LOG_DEBUG
|| debug
)) {
634 if (n
> 0 && buf
[n
-1] == '\n')
636 if (write(log_to_fd
, buf
, n
) != n
637 || write(log_to_fd
, "\n", 1) != 1)
643 * fatal - log an error message and die horribly.
646 fatal
__V((char *fmt
, ...))
650 #if defined(__STDC__)
655 fmt
= va_arg(pvar
, char *);
658 logit(LOG_ERR
, fmt
, pvar
);
661 die(1); /* as promised */
665 * error - log an error message.
668 error
__V((char *fmt
, ...))
672 #if defined(__STDC__)
677 fmt
= va_arg(pvar
, char *);
680 logit(LOG_ERR
, fmt
, pvar
);
685 * warn - log a warning message.
688 warn
__V((char *fmt
, ...))
692 #if defined(__STDC__)
697 fmt
= va_arg(pvar
, char *);
700 logit(LOG_WARNING
, fmt
, pvar
);
705 * notice - log a notice-level message.
708 notice
__V((char *fmt
, ...))
712 #if defined(__STDC__)
717 fmt
= va_arg(pvar
, char *);
720 logit(LOG_NOTICE
, fmt
, pvar
);
725 * info - log an informational message.
728 info
__V((char *fmt
, ...))
732 #if defined(__STDC__)
737 fmt
= va_arg(pvar
, char *);
740 logit(LOG_INFO
, fmt
, pvar
);
745 * dbglog - log a debug message.
748 dbglog
__V((char *fmt
, ...))
752 #if defined(__STDC__)
757 fmt
= va_arg(pvar
, char *);
760 logit(LOG_DEBUG
, fmt
, pvar
);
764 /* Procedures for locking the serial device using a lock file. */
767 #define LOCK_DIR "/var/lock"
770 #define LOCK_DIR "/var/spool/locks"
772 #define LOCK_DIR "/var/spool/lock"
775 #endif /* LOCK_DIR */
777 static char lock_file
[MAXPATHLEN
];
780 * lock - create a lock file for the named device
789 result
= mklock (dev
, (void *) 0);
791 strlcpy(lock_file
, sizeof(lock_file
), dev
);
796 notice("Device %s is locked by pid %d", dev
, result
);
798 error("Can't create lock file %s", lock_file
);
803 char lock_buffer
[12];
809 if (stat(dev
, &sbuf
) < 0) {
810 error("Can't get device number for %s: %m", dev
);
813 if ((sbuf
.st_mode
& S_IFMT
) != S_IFCHR
) {
814 error("Can't lock %s: not a character device", dev
);
817 slprintf(lock_file
, sizeof(lock_file
), "%s/LK.%03d.%03d.%03d",
818 LOCK_DIR
, major(sbuf
.st_dev
),
819 major(sbuf
.st_rdev
), minor(sbuf
.st_rdev
));
823 if ((p
= strrchr(dev
, '/')) != NULL
)
825 slprintf(lock_file
, sizeof(lock_file
), "%s/LCK..%s", LOCK_DIR
, dev
);
828 while ((fd
= open(lock_file
, O_EXCL
| O_CREAT
| O_RDWR
, 0644)) < 0) {
829 if (errno
!= EEXIST
) {
830 error("Can't create lock file %s: %m", lock_file
);
834 /* Read the lock file to find out who has the device locked. */
835 fd
= open(lock_file
, O_RDONLY
, 0);
837 if (errno
== ENOENT
) /* This is just a timing problem. */
839 error("Can't open existing lock file %s: %m", lock_file
);
843 n
= read(fd
, lock_buffer
, 11);
845 n
= read(fd
, &pid
, sizeof(pid
));
846 #endif /* LOCK_BINARY */
850 error("Can't read pid from lock file %s", lock_file
);
854 /* See if the process still exists. */
857 pid
= atoi(lock_buffer
);
858 #endif /* LOCK_BINARY */
860 return 1; /* somebody else locked it for us */
862 || (kill(pid
, 0) == -1 && errno
== ESRCH
)) {
863 if (unlink (lock_file
) == 0) {
864 notice("Removed stale lock on %s (pid %d)", dev
, pid
);
867 warn("Couldn't remove stale lock on %s", dev
);
869 notice("Device %s is locked by pid %d", dev
, pid
);
880 slprintf(lock_buffer
, sizeof(lock_buffer
), "%10d\n", pid
);
881 write (fd
, lock_buffer
, 11);
883 write(fd
, &pid
, sizeof (pid
));
892 * relock - called to update our lockfile when we are about to detach,
893 * thus changing our pid (we fork, the child carries on, and the parent dies).
894 * Note that this is called by the parent, with pid equal to the pid
895 * of the child. This avoids a potential race which would exist if
896 * we had the child rewrite the lockfile (the parent might die first,
897 * and another process could think the lock was stale if it checked
898 * between when the parent died and the child rewrote the lockfile).
909 char lock_buffer
[12];
911 if (lock_file
[0] == 0)
913 fd
= open(lock_file
, O_WRONLY
, 0);
915 error("Couldn't reopen lock file %s: %m", lock_file
);
921 slprintf(lock_buffer
, sizeof(lock_buffer
), "%10d\n", pid
);
922 write (fd
, lock_buffer
, 11);
924 write(fd
, &pid
, sizeof(pid
));
925 #endif /* LOCK_BINARY */
933 * unlock - remove our lockfile
940 (void) rmlock(lock_file
, (void *) 0);