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 static char rcsid[] = "$Id: utils.c,v 1.3 1999/05/04 06:58:36 paulus Exp $";
35 #include <sys/param.h>
36 #include <sys/types.h>
39 #include <sys/resource.h>
41 #include <sys/socket.h>
42 #include <netinet/in.h>
44 #include <sys/sysmacros.h>
49 static void pr_log __P((void *, char *, ...));
50 static void logit __P((int, char *, va_list));
51 static void vslp_printer __P((void *, char *, ...));
52 static void format_packet __P((u_char *, int, void (*) (void *, char *, ...),
61 * strlcpy - like strcpy/strncpy, doesn't overflow destination buffer,
62 * always leaves destination null-terminated (for len > 0).
65 strlcpy(dest, src, len)
70 size_t ret = strlen(src);
76 strncpy(dest, src, len - 1);
84 * strlcat - like strcat/strncat, doesn't overflow destination buffer,
85 * always leaves destination null-terminated (for len > 0).
88 strlcat(dest, src, len)
93 size_t dlen = strlen(dest);
95 return dlen + strlcpy(dest + dlen, src, (len > dlen? len - dlen: 0));
100 * slprintf - format a message into a buffer. Like sprintf except we
101 * also specify the length of the output buffer, and we handle
102 * %r (recursive format), %m (error message), %v (visible string),
103 * %q (quoted string), %t (current time) and %I (IP address) formats.
104 * Doesn't do floating-point formats.
105 * Returns the number of chars put into buf.
108 slprintf __V((char *buf, int buflen, char *fmt, ...))
120 buf = va_arg(args, char *);
121 buflen = va_arg(args, int);
122 fmt = va_arg(args, char *);
124 n = vslprintf(buf, buflen, fmt, args);
130 * vslprintf - like slprintf, takes a va_list instead of a list of args.
132 #define OUTCHAR(c) (buflen > 0? (--buflen, *buf++ = (c)): 0)
135 vslprintf(buf, buflen, fmt, args)
142 int width, prec, fillch;
143 int base, len, neg, quoted;
144 unsigned long val = 0;
145 char *str, *f, *buf0;
150 static char hexchars[] = "0123456789abcdef";
151 struct buffer_info bufinfo;
156 for (f = fmt; *f != '%' && *f != 0; ++f)
162 memcpy(buf, fmt, len);
177 width = va_arg(args, int);
181 width = width * 10 + c - '0';
188 prec = va_arg(args, int);
192 prec = prec * 10 + c - '0';
203 i = va_arg(args, int);
212 val = va_arg(args, unsigned int);
217 val = va_arg(args, unsigned int);
221 val = (unsigned long) va_arg(args, void *);
226 str = va_arg(args, char *);
229 num[0] = va_arg(args, int);
234 str = strerror(errno);
237 ip = va_arg(args, u_int32_t);
239 slprintf(num, sizeof(num), "%d.%d.%d.%d", (ip >> 24) & 0xff,
240 (ip >> 16) & 0xff, (ip >> 8) & 0xff, ip & 0xff);
244 f = va_arg(args, char *);
246 n = vslprintf(buf, buflen + 1, f, va_arg(args, va_list));
248 /* On the powerpc, a va_list is an array of 1 structure */
249 n = vslprintf(buf, buflen + 1, f, va_arg(args, void *));
257 str += 4; /* chop off the day name */
258 str[15] = 0; /* chop off year and newline */
260 case 'v': /* "visible" string */
261 case 'q': /* quoted string */
263 p = va_arg(args, unsigned char *);
264 if (fillch == '0' && prec > 0) {
267 n = strlen((char *)p);
268 if (prec > 0 && prec < n)
271 while (n > 0 && buflen > 0) {
274 if (!quoted && c >= 0x80) {
279 if (quoted && (c == '"' || c == '\\'))
281 if (c < 0x20 || (0x7f <= c && c < 0xa0)) {
285 case '\t': OUTCHAR('t'); break;
286 case '\n': OUTCHAR('n'); break;
287 case '\b': OUTCHAR('b'); break;
288 case '\f': OUTCHAR('f'); break;
291 OUTCHAR(hexchars[c >> 4]);
292 OUTCHAR(hexchars[c & 0xf]);
306 case 'P': /* print PPP packet */
308 bufinfo.len = buflen + 1;
309 p = va_arg(args, unsigned char *);
310 n = va_arg(args, int);
311 format_packet(p, n, vslp_printer, &bufinfo);
313 buflen = bufinfo.len - 1;
316 p = va_arg(args, unsigned char *);
317 for (n = prec; n > 0; --n) {
321 OUTCHAR(hexchars[(c >> 4) & 0xf]);
322 OUTCHAR(hexchars[c & 0xf]);
328 --fmt; /* so %z outputs %z etc. */
333 str = num + sizeof(num);
335 while (str > num + neg) {
336 *--str = hexchars[val % base];
338 if (--prec <= 0 && val == 0)
350 len = num + sizeof(num) - 1 - str;
353 if (prec > 0 && len > prec)
359 if ((n = width - len) > 0) {
367 memcpy(buf, str, len);
376 * vslp_printer - used in processing a %P format
379 vslp_printer __V((void *arg, char *fmt, ...))
383 struct buffer_info *bi;
391 arg = va_arg(pvar, void *);
392 fmt = va_arg(pvar, char *);
395 bi = (struct buffer_info *) arg;
396 n = vslprintf(bi->ptr, bi->len, fmt, pvar);
404 * log_packet - format a packet and log it.
407 char line[256]; /* line to be logged accumulated here */
411 log_packet(p, len, prefix, level)
417 strlcpy(line, prefix, sizeof(line));
418 linep = line + strlen(line);
419 format_packet(p, len, pr_log, NULL);
421 syslog(level, "%s", line);
425 * format_packet - make a readable representation of a packet,
426 * calling `printer(arg, format, ...)' to output it.
429 format_packet(p, len, printer, arg)
432 void (*printer) __P((void *, char *, ...));
437 struct protent *protp;
439 if (len >= PPP_HDRLEN && p[0] == PPP_ALLSTATIONS && p[1] == PPP_UI) {
443 for (i = 0; (protp = protocols[i]) != NULL; ++i)
444 if (proto == protp->protocol)
447 printer(arg, "[%s", protp->name);
448 n = (*protp->printpkt)(p, len, printer, arg);
453 for (i = 0; (protp = protocols[i]) != NULL; ++i)
454 if (proto == (protp->protocol & ~0x8000))
456 if (protp != 0 && protp->data_name != 0) {
457 printer(arg, "[%s data]", protp->data_name);
459 printer(arg, "%.8B ...", p);
461 printer(arg, "%.*B", len, p);
464 printer(arg, "[proto=0x%x]", proto);
469 printer(arg, "%.32B ...", p);
471 printer(arg, "%.*B", len, p);
475 pr_log __V((void *arg, char *fmt, ...))
487 arg = va_arg(pvar, void *);
488 fmt = va_arg(pvar, char *);
491 n = vslprintf(buf, sizeof(buf), fmt, pvar);
494 if (linep + n + 1 > line + sizeof(line)) {
495 syslog(LOG_DEBUG, "%s", line);
498 strlcpy(linep, buf, line + sizeof(line) - linep);
503 * print_string - print a readable representation of a string using
507 print_string(p, len, printer, arg)
510 void (*printer) __P((void *, char *, ...));
516 for (; len > 0; --len) {
518 if (' ' <= c && c <= '~') {
519 if (c == '\\' || c == '"')
521 printer(arg, "%c", c);
534 printer(arg, "\\%.3o", c);
542 * logit - does the hard work for fatal et al.
545 logit(level, fmt, args)
553 n = vslprintf(buf, sizeof(buf), fmt, args);
554 syslog(level, "%s", buf);
555 if (log_to_fd >= 0 && (level != LOG_DEBUG || debug)) {
556 if (buf[n-1] != '\n')
558 if (write(log_to_fd, buf, n) != n)
564 * fatal - log an error message and die horribly.
567 fatal __V((char *fmt, ...))
576 fmt = va_arg(pvar, char *);
579 logit(LOG_ERR, fmt, pvar);
582 die(1); /* as promised */
586 * error - log an error message.
589 error __V((char *fmt, ...))
598 fmt = va_arg(pvar, char *);
601 logit(LOG_ERR, fmt, pvar);
606 * warn - log a warning message.
609 warn __V((char *fmt, ...))
618 fmt = va_arg(pvar, char *);
621 logit(LOG_WARNING, fmt, pvar);
626 * notice - log a notice-level message.
629 notice __V((char *fmt, ...))
638 fmt = va_arg(pvar, char *);
641 logit(LOG_NOTICE, fmt, pvar);
646 * info - log an informational message.
649 info __V((char *fmt, ...))
658 fmt = va_arg(pvar, char *);
661 logit(LOG_INFO, fmt, pvar);
666 * dbglog - log a debug message.
669 dbglog __V((char *fmt, ...))
678 fmt = va_arg(pvar, char *);
681 logit(LOG_DEBUG, fmt, pvar);
685 /* Procedures for locking the serial device using a lock file. */
688 #define LOCK_DIR "/var/lock"
691 #define LOCK_DIR "/var/spool/locks"
693 #define LOCK_DIR "/var/spool/lock"
696 #endif /* LOCK_DIR */
698 static char lock_file[MAXPATHLEN];
701 * lock - create a lock file for the named device
709 result = mklock (dev, (void *) 0);
711 strlcpy(lock_file, sizeof(lock_file), dev);
716 notice("Device %s is locked by pid %d", dev, result);
718 error("Can't create lock file %s", lock_file);
723 char lock_buffer[12];
729 if (stat(dev, &sbuf) < 0) {
730 error("Can't get device number for %s: %m", dev);
733 if ((sbuf.st_mode & S_IFMT) != S_IFCHR) {
734 error("Can't lock %s: not a character device", dev);
737 slprintf(lock_file, sizeof(lock_file), "%s/LK.%03d.%03d.%03d",
738 LOCK_DIR, major(sbuf.st_dev),
739 major(sbuf.st_rdev), minor(sbuf.st_rdev));
743 if ((p = strrchr(dev, '/')) != NULL)
745 slprintf(lock_file, sizeof(lock_file), "%s/LCK..%s", LOCK_DIR, dev);
748 while ((fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644)) < 0) {
749 if (errno != EEXIST) {
750 error("Can't create lock file %s: %m", lock_file);
754 /* Read the lock file to find out who has the device locked. */
755 fd = open(lock_file, O_RDONLY, 0);
757 if (errno == ENOENT) /* This is just a timing problem. */
759 error("Can't open existing lock file %s: %m", lock_file);
763 n = read(fd, lock_buffer, 11);
765 n = read(fd, &pid, sizeof(pid));
766 #endif /* LOCK_BINARY */
770 error("Can't read pid from lock file %s", lock_file);
774 /* See if the process still exists. */
777 pid = atoi(lock_buffer);
778 #endif /* LOCK_BINARY */
780 return 1; /* somebody else locked it for us */
782 || (kill(pid, 0) == -1 && errno == ESRCH)) {
783 if (unlink (lock_file) == 0) {
784 notice("Removed stale lock on %s (pid %d)", dev, pid);
787 warn("Couldn't remove stale lock on %s", dev);
789 notice("Device %s is locked by pid %d", dev, pid);
800 slprintf(lock_buffer, sizeof(lock_buffer), "%10d\n", pid);
801 write (fd, lock_buffer, 11);
803 write(fd, &pid, sizeof (pid));
812 * relock - called to update our lockfile when we are about to detach,
813 * thus changing our pid (we fork, the child carries on, and the parent dies).
814 * Note that this is called by the parent, with pid equal to the pid
815 * of the child. This avoids a potential race which would exist if
816 * we had the child rewrite the lockfile (the parent might die first,
817 * and another process could think the lock was stale if it checked
818 * between when the parent died and the child rewrote the lockfile).
824 /* XXX is there a way to do this? */
829 char lock_buffer[12];
831 if (lock_file[0] == 0)
833 fd = open(lock_file, O_WRONLY, 0);
835 error("Couldn't reopen lock file %s: %m", lock_file);
841 slprintf(lock_buffer, sizeof(lock_buffer), "%10d\n", pid);
842 write (fd, lock_buffer, 11);
844 write(fd, &pid, sizeof(pid));
845 #endif /* LOCK_BINARY */
853 * unlock - remove our lockfile
860 (void) rmlock(lock_file, (void *) 0);