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.4 1999/05/12 06:15:33 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>
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);
181 width = va_arg(args, int);
185 width = width * 10 + c - '0';
192 prec = va_arg(args, int);
196 prec = prec * 10 + c - '0';
207 i = va_arg(args, int);
216 val = va_arg(args, unsigned int);
221 val = va_arg(args, unsigned int);
225 val = (unsigned long) va_arg(args, void *);
230 str = va_arg(args, char *);
233 num[0] = va_arg(args, int);
238 str = strerror(errno);
241 ip = va_arg(args, u_int32_t);
243 slprintf(num, sizeof(num), "%d.%d.%d.%d", (ip >> 24) & 0xff,
244 (ip >> 16) & 0xff, (ip >> 8) & 0xff, ip & 0xff);
248 f = va_arg(args, char *);
250 n = vslprintf(buf, buflen + 1, f, va_arg(args, va_list));
252 /* On the powerpc, a va_list is an array of 1 structure */
253 n = vslprintf(buf, buflen + 1, f, va_arg(args, void *));
261 str += 4; /* chop off the day name */
262 str[15] = 0; /* chop off year and newline */
264 case 'v': /* "visible" string */
265 case 'q': /* quoted string */
267 p = va_arg(args, unsigned char *);
268 if (fillch == '0' && prec > 0) {
271 n = strlen((char *)p);
272 if (prec > 0 && prec < n)
275 while (n > 0 && buflen > 0) {
278 if (!quoted && c >= 0x80) {
283 if (quoted && (c == '"' || c == '\\'))
285 if (c < 0x20 || (0x7f <= c && c < 0xa0)) {
289 case '\t': OUTCHAR('t'); break;
290 case '\n': OUTCHAR('n'); break;
291 case '\b': OUTCHAR('b'); break;
292 case '\f': OUTCHAR('f'); break;
295 OUTCHAR(hexchars[c >> 4]);
296 OUTCHAR(hexchars[c & 0xf]);
310 case 'P': /* print PPP packet */
312 bufinfo.len = buflen + 1;
313 p = va_arg(args, unsigned char *);
314 n = va_arg(args, int);
315 format_packet(p, n, vslp_printer, &bufinfo);
317 buflen = bufinfo.len - 1;
320 p = va_arg(args, unsigned char *);
321 for (n = prec; n > 0; --n) {
325 OUTCHAR(hexchars[(c >> 4) & 0xf]);
326 OUTCHAR(hexchars[c & 0xf]);
332 --fmt; /* so %z outputs %z etc. */
337 str = num + sizeof(num);
339 while (str > num + neg) {
340 *--str = hexchars[val % base];
342 if (--prec <= 0 && val == 0)
354 len = num + sizeof(num) - 1 - str;
357 if (prec > 0 && len > prec)
363 if ((n = width - len) > 0) {
371 memcpy(buf, str, len);
380 * vslp_printer - used in processing a %P format
383 vslp_printer __V((void *arg, char *fmt, ...))
387 struct buffer_info *bi;
395 arg = va_arg(pvar, void *);
396 fmt = va_arg(pvar, char *);
399 bi = (struct buffer_info *) arg;
400 n = vslprintf(bi->ptr, bi->len, fmt, pvar);
408 * log_packet - format a packet and log it.
411 char line[256]; /* line to be logged accumulated here */
415 log_packet(p, len, prefix, level)
421 strlcpy(line, prefix, sizeof(line));
422 linep = line + strlen(line);
423 format_packet(p, len, pr_log, NULL);
425 syslog(level, "%s", line);
429 * format_packet - make a readable representation of a packet,
430 * calling `printer(arg, format, ...)' to output it.
433 format_packet(p, len, printer, arg)
436 void (*printer) __P((void *, char *, ...));
441 struct protent *protp;
443 if (len >= PPP_HDRLEN && p[0] == PPP_ALLSTATIONS && p[1] == PPP_UI) {
447 for (i = 0; (protp = protocols[i]) != NULL; ++i)
448 if (proto == protp->protocol)
451 printer(arg, "[%s", protp->name);
452 n = (*protp->printpkt)(p, len, printer, arg);
457 for (i = 0; (protp = protocols[i]) != NULL; ++i)
458 if (proto == (protp->protocol & ~0x8000))
460 if (protp != 0 && protp->data_name != 0) {
461 printer(arg, "[%s data]", protp->data_name);
463 printer(arg, "%.8B ...", p);
465 printer(arg, "%.*B", len, p);
468 printer(arg, "[proto=0x%x]", proto);
473 printer(arg, "%.32B ...", p);
475 printer(arg, "%.*B", len, p);
479 pr_log __V((void *arg, char *fmt, ...))
491 arg = va_arg(pvar, void *);
492 fmt = va_arg(pvar, char *);
495 n = vslprintf(buf, sizeof(buf), fmt, pvar);
498 if (linep + n + 1 > line + sizeof(line)) {
499 syslog(LOG_DEBUG, "%s", line);
502 strlcpy(linep, buf, line + sizeof(line) - linep);
507 * print_string - print a readable representation of a string using
511 print_string(p, len, printer, arg)
514 void (*printer) __P((void *, char *, ...));
520 for (; len > 0; --len) {
522 if (' ' <= c && c <= '~') {
523 if (c == '\\' || c == '"')
525 printer(arg, "%c", c);
538 printer(arg, "\\%.3o", c);
546 * logit - does the hard work for fatal et al.
549 logit(level, fmt, args)
557 n = vslprintf(buf, sizeof(buf), fmt, args);
558 syslog(level, "%s", buf);
559 if (log_to_fd >= 0 && (level != LOG_DEBUG || debug)) {
560 if (buf[n-1] != '\n')
562 if (write(log_to_fd, buf, n) != n)
568 * fatal - log an error message and die horribly.
571 fatal __V((char *fmt, ...))
580 fmt = va_arg(pvar, char *);
583 logit(LOG_ERR, fmt, pvar);
586 die(1); /* as promised */
590 * error - log an error message.
593 error __V((char *fmt, ...))
602 fmt = va_arg(pvar, char *);
605 logit(LOG_ERR, fmt, pvar);
610 * warn - log a warning message.
613 warn __V((char *fmt, ...))
622 fmt = va_arg(pvar, char *);
625 logit(LOG_WARNING, fmt, pvar);
630 * notice - log a notice-level message.
633 notice __V((char *fmt, ...))
642 fmt = va_arg(pvar, char *);
645 logit(LOG_NOTICE, fmt, pvar);
650 * info - log an informational message.
653 info __V((char *fmt, ...))
662 fmt = va_arg(pvar, char *);
665 logit(LOG_INFO, fmt, pvar);
670 * dbglog - log a debug message.
673 dbglog __V((char *fmt, ...))
682 fmt = va_arg(pvar, char *);
685 logit(LOG_DEBUG, fmt, pvar);
689 /* Procedures for locking the serial device using a lock file. */
692 #define LOCK_DIR "/var/lock"
695 #define LOCK_DIR "/var/spool/locks"
697 #define LOCK_DIR "/var/spool/lock"
700 #endif /* LOCK_DIR */
702 static char lock_file[MAXPATHLEN];
705 * lock - create a lock file for the named device
714 result = mklock (dev, (void *) 0);
716 strlcpy(lock_file, sizeof(lock_file), dev);
721 notice("Device %s is locked by pid %d", dev, result);
723 error("Can't create lock file %s", lock_file);
728 char lock_buffer[12];
734 if (stat(dev, &sbuf) < 0) {
735 error("Can't get device number for %s: %m", dev);
738 if ((sbuf.st_mode & S_IFMT) != S_IFCHR) {
739 error("Can't lock %s: not a character device", dev);
742 slprintf(lock_file, sizeof(lock_file), "%s/LK.%03d.%03d.%03d",
743 LOCK_DIR, major(sbuf.st_dev),
744 major(sbuf.st_rdev), minor(sbuf.st_rdev));
748 if ((p = strrchr(dev, '/')) != NULL)
750 slprintf(lock_file, sizeof(lock_file), "%s/LCK..%s", LOCK_DIR, dev);
753 while ((fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644)) < 0) {
754 if (errno != EEXIST) {
755 error("Can't create lock file %s: %m", lock_file);
759 /* Read the lock file to find out who has the device locked. */
760 fd = open(lock_file, O_RDONLY, 0);
762 if (errno == ENOENT) /* This is just a timing problem. */
764 error("Can't open existing lock file %s: %m", lock_file);
768 n = read(fd, lock_buffer, 11);
770 n = read(fd, &pid, sizeof(pid));
771 #endif /* LOCK_BINARY */
775 error("Can't read pid from lock file %s", lock_file);
779 /* See if the process still exists. */
782 pid = atoi(lock_buffer);
783 #endif /* LOCK_BINARY */
785 return 1; /* somebody else locked it for us */
787 || (kill(pid, 0) == -1 && errno == ESRCH)) {
788 if (unlink (lock_file) == 0) {
789 notice("Removed stale lock on %s (pid %d)", dev, pid);
792 warn("Couldn't remove stale lock on %s", dev);
794 notice("Device %s is locked by pid %d", dev, pid);
805 slprintf(lock_buffer, sizeof(lock_buffer), "%10d\n", pid);
806 write (fd, lock_buffer, 11);
808 write(fd, &pid, sizeof (pid));
817 * relock - called to update our lockfile when we are about to detach,
818 * thus changing our pid (we fork, the child carries on, and the parent dies).
819 * Note that this is called by the parent, with pid equal to the pid
820 * of the child. This avoids a potential race which would exist if
821 * we had the child rewrite the lockfile (the parent might die first,
822 * and another process could think the lock was stale if it checked
823 * between when the parent died and the child rewrote the lockfile).
830 /* XXX is there a way to do this? */
835 char lock_buffer[12];
837 if (lock_file[0] == 0)
839 fd = open(lock_file, O_WRONLY, 0);
841 error("Couldn't reopen lock file %s: %m", lock_file);
847 slprintf(lock_buffer, sizeof(lock_buffer), "%10d\n", pid);
848 write (fd, lock_buffer, 11);
850 write(fd, &pid, sizeof(pid));
851 #endif /* LOCK_BINARY */
859 * unlock - remove our lockfile
866 (void) rmlock(lock_file, (void *) 0);