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.6 1999/06/24 00:17:48 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/mkdev.h>
50 extern char *strerror();
53 static void pr_log __P((void *, char *, ...));
54 static void logit __P((int, char *, va_list));
55 static void vslp_printer __P((void *, char *, ...));
56 static void format_packet __P((u_char *, int, void (*) (void *, char *, ...),
65 * strlcpy - like strcpy/strncpy, doesn't overflow destination buffer,
66 * always leaves destination null-terminated (for len > 0).
69 strlcpy(dest, src, len)
74 size_t ret = strlen(src);
80 strncpy(dest, src, len - 1);
88 * strlcat - like strcat/strncat, doesn't overflow destination buffer,
89 * always leaves destination null-terminated (for len > 0).
92 strlcat(dest, src, len)
97 size_t dlen = strlen(dest);
99 return dlen + strlcpy(dest + dlen, src, (len > dlen? len - dlen: 0));
104 * slprintf - format a message into a buffer. Like sprintf except we
105 * also specify the length of the output buffer, and we handle
106 * %r (recursive format), %m (error message), %v (visible string),
107 * %q (quoted string), %t (current time) and %I (IP address) formats.
108 * Doesn't do floating-point formats.
109 * Returns the number of chars put into buf.
112 slprintf __V((char *buf, int buflen, char *fmt, ...))
124 buf = va_arg(args, char *);
125 buflen = va_arg(args, int);
126 fmt = va_arg(args, char *);
128 n = vslprintf(buf, buflen, fmt, args);
134 * vslprintf - like slprintf, takes a va_list instead of a list of args.
136 #define OUTCHAR(c) (buflen > 0? (--buflen, *buf++ = (c)): 0)
139 vslprintf(buf, buflen, fmt, args)
146 int width, prec, fillch;
147 int base, len, neg, quoted;
148 unsigned long val = 0;
149 char *str, *f, *buf0;
154 static char hexchars[] = "0123456789abcdef";
155 struct buffer_info bufinfo;
160 for (f = fmt; *f != '%' && *f != 0; ++f)
166 memcpy(buf, fmt, len);
182 width = va_arg(args, int);
186 width = width * 10 + c - '0';
193 prec = va_arg(args, int);
198 prec = prec * 10 + c - '0';
209 i = va_arg(args, int);
218 val = va_arg(args, unsigned int);
223 val = va_arg(args, unsigned int);
227 val = (unsigned long) va_arg(args, void *);
232 str = va_arg(args, char *);
235 num[0] = va_arg(args, int);
240 str = strerror(errno);
243 ip = va_arg(args, u_int32_t);
245 slprintf(num, sizeof(num), "%d.%d.%d.%d", (ip >> 24) & 0xff,
246 (ip >> 16) & 0xff, (ip >> 8) & 0xff, ip & 0xff);
250 f = va_arg(args, char *);
252 n = vslprintf(buf, buflen + 1, f, va_arg(args, va_list));
254 /* On the powerpc, a va_list is an array of 1 structure */
255 n = vslprintf(buf, buflen + 1, f, va_arg(args, void *));
263 str += 4; /* chop off the day name */
264 str[15] = 0; /* chop off year and newline */
266 case 'v': /* "visible" string */
267 case 'q': /* quoted string */
269 p = va_arg(args, unsigned char *);
270 if (fillch == '0' && prec >= 0) {
273 n = strlen((char *)p);
274 if (prec >= 0 && n > prec)
277 while (n > 0 && buflen > 0) {
280 if (!quoted && c >= 0x80) {
285 if (quoted && (c == '"' || c == '\\'))
287 if (c < 0x20 || (0x7f <= c && c < 0xa0)) {
291 case '\t': OUTCHAR('t'); break;
292 case '\n': OUTCHAR('n'); break;
293 case '\b': OUTCHAR('b'); break;
294 case '\f': OUTCHAR('f'); break;
297 OUTCHAR(hexchars[c >> 4]);
298 OUTCHAR(hexchars[c & 0xf]);
312 case 'P': /* print PPP packet */
314 bufinfo.len = buflen + 1;
315 p = va_arg(args, unsigned char *);
316 n = va_arg(args, int);
317 format_packet(p, n, vslp_printer, &bufinfo);
319 buflen = bufinfo.len - 1;
322 p = va_arg(args, unsigned char *);
323 for (n = prec; n > 0; --n) {
327 OUTCHAR(hexchars[(c >> 4) & 0xf]);
328 OUTCHAR(hexchars[c & 0xf]);
334 --fmt; /* so %z outputs %z etc. */
339 str = num + sizeof(num);
341 while (str > num + neg) {
342 *--str = hexchars[val % base];
344 if (--prec <= 0 && val == 0)
356 len = num + sizeof(num) - 1 - str;
359 if (prec >= 0 && len > prec)
365 if ((n = width - len) > 0) {
373 memcpy(buf, str, len);
382 * vslp_printer - used in processing a %P format
385 vslp_printer __V((void *arg, char *fmt, ...))
389 struct buffer_info *bi;
397 arg = va_arg(pvar, void *);
398 fmt = va_arg(pvar, char *);
401 bi = (struct buffer_info *) arg;
402 n = vslprintf(bi->ptr, bi->len, fmt, pvar);
410 * log_packet - format a packet and log it.
413 char line[256]; /* line to be logged accumulated here */
417 log_packet(p, len, prefix, level)
423 strlcpy(line, prefix, sizeof(line));
424 linep = line + strlen(line);
425 format_packet(p, len, pr_log, NULL);
427 syslog(level, "%s", line);
431 * format_packet - make a readable representation of a packet,
432 * calling `printer(arg, format, ...)' to output it.
435 format_packet(p, len, printer, arg)
438 void (*printer) __P((void *, char *, ...));
443 struct protent *protp;
445 if (len >= PPP_HDRLEN && p[0] == PPP_ALLSTATIONS && p[1] == PPP_UI) {
449 for (i = 0; (protp = protocols[i]) != NULL; ++i)
450 if (proto == protp->protocol)
453 printer(arg, "[%s", protp->name);
454 n = (*protp->printpkt)(p, len, printer, arg);
459 for (i = 0; (protp = protocols[i]) != NULL; ++i)
460 if (proto == (protp->protocol & ~0x8000))
462 if (protp != 0 && protp->data_name != 0) {
463 printer(arg, "[%s data]", protp->data_name);
465 printer(arg, "%.8B ...", p);
467 printer(arg, "%.*B", len, p);
470 printer(arg, "[proto=0x%x]", proto);
475 printer(arg, "%.32B ...", p);
477 printer(arg, "%.*B", len, p);
481 pr_log __V((void *arg, char *fmt, ...))
493 arg = va_arg(pvar, void *);
494 fmt = va_arg(pvar, char *);
497 n = vslprintf(buf, sizeof(buf), fmt, pvar);
500 if (linep + n + 1 > line + sizeof(line)) {
501 syslog(LOG_DEBUG, "%s", line);
504 strlcpy(linep, buf, line + sizeof(line) - linep);
509 * print_string - print a readable representation of a string using
513 print_string(p, len, printer, arg)
516 void (*printer) __P((void *, char *, ...));
522 for (; len > 0; --len) {
524 if (' ' <= c && c <= '~') {
525 if (c == '\\' || c == '"')
527 printer(arg, "%c", c);
540 printer(arg, "\\%.3o", c);
548 * logit - does the hard work for fatal et al.
551 logit(level, fmt, args)
559 n = vslprintf(buf, sizeof(buf), fmt, args);
560 syslog(level, "%s", buf);
561 if (log_to_fd >= 0 && (level != LOG_DEBUG || debug)) {
562 if (buf[n-1] != '\n')
564 if (write(log_to_fd, buf, n) != n)
570 * fatal - log an error message and die horribly.
573 fatal __V((char *fmt, ...))
582 fmt = va_arg(pvar, char *);
585 logit(LOG_ERR, fmt, pvar);
588 die(1); /* as promised */
592 * error - log an error message.
595 error __V((char *fmt, ...))
604 fmt = va_arg(pvar, char *);
607 logit(LOG_ERR, fmt, pvar);
612 * warn - log a warning message.
615 warn __V((char *fmt, ...))
624 fmt = va_arg(pvar, char *);
627 logit(LOG_WARNING, fmt, pvar);
632 * notice - log a notice-level message.
635 notice __V((char *fmt, ...))
644 fmt = va_arg(pvar, char *);
647 logit(LOG_NOTICE, fmt, pvar);
652 * info - log an informational message.
655 info __V((char *fmt, ...))
664 fmt = va_arg(pvar, char *);
667 logit(LOG_INFO, fmt, pvar);
672 * dbglog - log a debug message.
675 dbglog __V((char *fmt, ...))
684 fmt = va_arg(pvar, char *);
687 logit(LOG_DEBUG, fmt, pvar);
691 /* Procedures for locking the serial device using a lock file. */
694 #define LOCK_DIR "/var/lock"
697 #define LOCK_DIR "/var/spool/locks"
699 #define LOCK_DIR "/var/spool/lock"
702 #endif /* LOCK_DIR */
704 static char lock_file[MAXPATHLEN];
707 * lock - create a lock file for the named device
716 result = mklock (dev, (void *) 0);
718 strlcpy(lock_file, sizeof(lock_file), dev);
723 notice("Device %s is locked by pid %d", dev, result);
725 error("Can't create lock file %s", lock_file);
730 char lock_buffer[12];
736 if (stat(dev, &sbuf) < 0) {
737 error("Can't get device number for %s: %m", dev);
740 if ((sbuf.st_mode & S_IFMT) != S_IFCHR) {
741 error("Can't lock %s: not a character device", dev);
744 slprintf(lock_file, sizeof(lock_file), "%s/LK.%03d.%03d.%03d",
745 LOCK_DIR, major(sbuf.st_dev),
746 major(sbuf.st_rdev), minor(sbuf.st_rdev));
750 if ((p = strrchr(dev, '/')) != NULL)
752 slprintf(lock_file, sizeof(lock_file), "%s/LCK..%s", LOCK_DIR, dev);
755 while ((fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644)) < 0) {
756 if (errno != EEXIST) {
757 error("Can't create lock file %s: %m", lock_file);
761 /* Read the lock file to find out who has the device locked. */
762 fd = open(lock_file, O_RDONLY, 0);
764 if (errno == ENOENT) /* This is just a timing problem. */
766 error("Can't open existing lock file %s: %m", lock_file);
770 n = read(fd, lock_buffer, 11);
772 n = read(fd, &pid, sizeof(pid));
773 #endif /* LOCK_BINARY */
777 error("Can't read pid from lock file %s", lock_file);
781 /* See if the process still exists. */
784 pid = atoi(lock_buffer);
785 #endif /* LOCK_BINARY */
787 return 1; /* somebody else locked it for us */
789 || (kill(pid, 0) == -1 && errno == ESRCH)) {
790 if (unlink (lock_file) == 0) {
791 notice("Removed stale lock on %s (pid %d)", dev, pid);
794 warn("Couldn't remove stale lock on %s", dev);
796 notice("Device %s is locked by pid %d", dev, pid);
807 slprintf(lock_buffer, sizeof(lock_buffer), "%10d\n", pid);
808 write (fd, lock_buffer, 11);
810 write(fd, &pid, sizeof (pid));
819 * relock - called to update our lockfile when we are about to detach,
820 * thus changing our pid (we fork, the child carries on, and the parent dies).
821 * Note that this is called by the parent, with pid equal to the pid
822 * of the child. This avoids a potential race which would exist if
823 * we had the child rewrite the lockfile (the parent might die first,
824 * and another process could think the lock was stale if it checked
825 * between when the parent died and the child rewrote the lockfile).
832 /* XXX is there a way to do this? */
837 char lock_buffer[12];
839 if (lock_file[0] == 0)
841 fd = open(lock_file, O_WRONLY, 0);
843 error("Couldn't reopen lock file %s: %m", lock_file);
849 slprintf(lock_buffer, sizeof(lock_buffer), "%10d\n", pid);
850 write (fd, lock_buffer, 11);
852 write(fd, &pid, sizeof(pid));
853 #endif /* LOCK_BINARY */
861 * unlock - remove our lockfile
868 (void) rmlock(lock_file, (void *) 0);