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.2 1999/04/16 11:34:27 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>
46 static void pr_log __P((void *, char *, ...));
47 static void logit __P((int, char *, va_list));
48 static void vslp_printer __P((void *, char *, ...));
49 static void format_packet __P((u_char *, int, void (*) (void *, char *, ...),
58 * strlcpy - like strcpy/strncpy, doesn't overflow destination buffer,
59 * always leaves destination null-terminated (for len > 0).
62 strlcpy(dest, src, len)
67 size_t ret = strlen(src);
73 strncpy(dest, src, len - 1);
81 * strlcat - like strcat/strncat, doesn't overflow destination buffer,
82 * always leaves destination null-terminated (for len > 0).
85 strlcat(dest, src, len)
90 size_t dlen = strlen(dest);
92 return dlen + strlcpy(dest + dlen, src, (len > dlen? len - dlen: 0));
97 * slprintf - format a message into a buffer. Like sprintf except we
98 * also specify the length of the output buffer, and we handle
99 * %r (recursive format), %m (error message), %v (visible string),
100 * %q (quoted string), %t (current time) and %I (IP address) formats.
101 * Doesn't do floating-point formats.
102 * Returns the number of chars put into buf.
105 slprintf __V((char *buf, int buflen, char *fmt, ...))
117 buf = va_arg(args, char *);
118 buflen = va_arg(args, int);
119 fmt = va_arg(args, char *);
121 n = vslprintf(buf, buflen, fmt, args);
127 * vslprintf - like slprintf, takes a va_list instead of a list of args.
129 #define OUTCHAR(c) (buflen > 0? (--buflen, *buf++ = (c)): 0)
132 vslprintf(buf, buflen, fmt, args)
139 int width, prec, fillch;
140 int base, len, neg, quoted;
141 unsigned long val = 0;
142 char *str, *f, *buf0;
147 static char hexchars[] = "0123456789abcdef";
148 struct buffer_info bufinfo;
153 for (f = fmt; *f != '%' && *f != 0; ++f)
159 memcpy(buf, fmt, len);
174 width = va_arg(args, int);
178 width = width * 10 + c - '0';
185 prec = va_arg(args, int);
189 prec = prec * 10 + c - '0';
200 i = va_arg(args, int);
209 val = va_arg(args, unsigned int);
214 val = va_arg(args, unsigned int);
218 val = (unsigned long) va_arg(args, void *);
223 str = va_arg(args, char *);
226 num[0] = va_arg(args, int);
231 str = strerror(errno);
234 ip = va_arg(args, u_int32_t);
236 slprintf(num, sizeof(num), "%d.%d.%d.%d", (ip >> 24) & 0xff,
237 (ip >> 16) & 0xff, (ip >> 8) & 0xff, ip & 0xff);
241 f = va_arg(args, char *);
243 n = vslprintf(buf, buflen + 1, f, va_arg(args, va_list));
245 /* On the powerpc, a va_list is an array of 1 structure */
246 n = vslprintf(buf, buflen + 1, f, va_arg(args, void *));
254 str += 4; /* chop off the day name */
255 str[15] = 0; /* chop off year and newline */
257 case 'v': /* "visible" string */
258 case 'q': /* quoted string */
260 p = va_arg(args, unsigned char *);
261 if (fillch == '0' && prec > 0) {
264 n = strlen((char *)p);
265 if (prec > 0 && prec < n)
268 while (n > 0 && buflen > 0) {
271 if (!quoted && c >= 0x80) {
276 if (quoted && (c == '"' || c == '\\'))
278 if (c < 0x20 || (0x7f <= c && c < 0xa0)) {
282 case '\t': OUTCHAR('t'); break;
283 case '\n': OUTCHAR('n'); break;
284 case '\b': OUTCHAR('b'); break;
285 case '\f': OUTCHAR('f'); break;
288 OUTCHAR(hexchars[c >> 4]);
289 OUTCHAR(hexchars[c & 0xf]);
303 case 'P': /* print PPP packet */
305 bufinfo.len = buflen + 1;
306 p = va_arg(args, unsigned char *);
307 n = va_arg(args, int);
308 format_packet(p, n, vslp_printer, &bufinfo);
310 buflen = bufinfo.len - 1;
313 p = va_arg(args, unsigned char *);
314 for (n = prec; n > 0; --n) {
318 OUTCHAR(hexchars[(c >> 4) & 0xf]);
319 OUTCHAR(hexchars[c & 0xf]);
325 --fmt; /* so %z outputs %z etc. */
330 str = num + sizeof(num);
332 while (str > num + neg) {
333 *--str = hexchars[val % base];
335 if (--prec <= 0 && val == 0)
347 len = num + sizeof(num) - 1 - str;
350 if (prec > 0 && len > prec)
356 if ((n = width - len) > 0) {
364 memcpy(buf, str, len);
373 * vslp_printer - used in processing a %P format
376 vslp_printer __V((void *arg, char *fmt, ...))
380 struct buffer_info *bi;
388 arg = va_arg(pvar, void *);
389 fmt = va_arg(pvar, char *);
392 bi = (struct buffer_info *) arg;
393 n = vslprintf(bi->ptr, bi->len, fmt, pvar);
401 * log_packet - format a packet and log it.
404 char line[256]; /* line to be logged accumulated here */
408 log_packet(p, len, prefix, level)
414 strlcpy(line, prefix, sizeof(line));
415 linep = line + strlen(line);
416 format_packet(p, len, pr_log, NULL);
418 syslog(level, "%s", line);
422 * format_packet - make a readable representation of a packet,
423 * calling `printer(arg, format, ...)' to output it.
426 format_packet(p, len, printer, arg)
429 void (*printer) __P((void *, char *, ...));
434 struct protent *protp;
436 if (len >= PPP_HDRLEN && p[0] == PPP_ALLSTATIONS && p[1] == PPP_UI) {
440 for (i = 0; (protp = protocols[i]) != NULL; ++i)
441 if (proto == protp->protocol)
444 printer(arg, "[%s", protp->name);
445 n = (*protp->printpkt)(p, len, printer, arg);
450 for (i = 0; (protp = protocols[i]) != NULL; ++i)
451 if (proto == (protp->protocol & ~0x8000))
453 if (protp != 0 && protp->data_name != 0) {
454 printer(arg, "[%s data]", protp->data_name);
456 printer(arg, "%.8B ...", p);
458 printer(arg, "%.*B", len, p);
461 printer(arg, "[proto=0x%x]", proto);
466 printer(arg, "%.32B ...", p);
468 printer(arg, "%.*B", len, p);
472 pr_log __V((void *arg, char *fmt, ...))
484 arg = va_arg(pvar, void *);
485 fmt = va_arg(pvar, char *);
488 n = vslprintf(buf, sizeof(buf), fmt, pvar);
491 if (linep + n + 1 > line + sizeof(line)) {
492 syslog(LOG_DEBUG, "%s", line);
495 strlcpy(linep, buf, line + sizeof(line) - linep);
500 * print_string - print a readable representation of a string using
504 print_string(p, len, printer, arg)
507 void (*printer) __P((void *, char *, ...));
513 for (; len > 0; --len) {
515 if (' ' <= c && c <= '~') {
516 if (c == '\\' || c == '"')
518 printer(arg, "%c", c);
531 printer(arg, "\\%.3o", c);
539 * logit - does the hard work for fatal et al.
542 logit(level, fmt, args)
550 n = vslprintf(buf, sizeof(buf), fmt, args);
551 syslog(level, "%s", buf);
552 if (log_to_fd >= 0 && (level != LOG_DEBUG || debug)) {
553 if (buf[n-1] != '\n')
555 if (write(log_to_fd, buf, n) != n)
561 * fatal - log an error message and die horribly.
564 fatal __V((char *fmt, ...))
573 fmt = va_arg(pvar, char *);
576 logit(LOG_ERR, fmt, pvar);
579 die(1); /* as promised */
583 * error - log an error message.
586 error __V((char *fmt, ...))
595 fmt = va_arg(pvar, char *);
598 logit(LOG_ERR, fmt, pvar);
603 * warn - log a warning message.
606 warn __V((char *fmt, ...))
615 fmt = va_arg(pvar, char *);
618 logit(LOG_WARNING, fmt, pvar);
623 * notice - log a notice-level message.
626 notice __V((char *fmt, ...))
635 fmt = va_arg(pvar, char *);
638 logit(LOG_NOTICE, fmt, pvar);
643 * info - log an informational message.
646 info __V((char *fmt, ...))
655 fmt = va_arg(pvar, char *);
658 logit(LOG_INFO, fmt, pvar);
663 * dbglog - log a debug message.
666 dbglog __V((char *fmt, ...))
675 fmt = va_arg(pvar, char *);
678 logit(LOG_DEBUG, fmt, pvar);
682 /* Procedures for locking the serial device using a lock file. */
685 #define LOCK_DIR "/var/lock"
688 #define LOCK_DIR "/var/spool/locks"
690 #define LOCK_DIR "/var/spool/lock"
693 #endif /* LOCK_DIR */
695 static char lock_file[MAXPATHLEN];
698 * lock - create a lock file for the named device
706 result = mklock (dev, (void *) 0);
708 strlcpy(lock_file, sizeof(lock_file), dev);
713 notice("Device %s is locked by pid %d", dev, result);
715 error("Can't create lock file %s", lock_file);
720 char lock_buffer[12];
726 if (stat(dev, &sbuf) < 0) {
727 error("Can't get device number for %s: %m", dev);
730 if ((sbuf.st_mode & S_IFMT) != S_IFCHR) {
731 error("Can't lock %s: not a character device", dev);
734 slprintf(lock_file, sizeof(lock_file), "%s/LK.%03d.%03d.%03d",
735 LOCK_DIR, major(sbuf.st_dev),
736 major(sbuf.st_rdev), minor(sbuf.st_rdev));
740 if ((p = strrchr(dev, '/')) != NULL)
742 slprintf(lock_file, sizeof(lock_file), "%s/LCK..%s", LOCK_DIR, dev);
745 while ((fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644)) < 0) {
746 if (errno != EEXIST) {
747 error("Can't create lock file %s: %m", lock_file);
751 /* Read the lock file to find out who has the device locked. */
752 fd = open(lock_file, O_RDONLY, 0);
754 if (errno == ENOENT) /* This is just a timing problem. */
756 error("Can't open existing lock file %s: %m", lock_file);
760 n = read(fd, lock_buffer, 11);
762 n = read(fd, &pid, sizeof(pid));
763 #endif /* LOCK_BINARY */
767 error("Can't read pid from lock file %s", lock_file);
771 /* See if the process still exists. */
774 pid = atoi(lock_buffer);
775 #endif /* LOCK_BINARY */
777 return 1; /* somebody else locked it for us */
779 || (kill(pid, 0) == -1 && errno == ESRCH)) {
780 if (unlink (lock_file) == 0) {
781 notice("Removed stale lock on %s (pid %d)", dev, pid);
784 warn("Couldn't remove stale lock on %s", dev);
786 notice("Device %s is locked by pid %d", dev, pid);
797 slprintf(lock_buffer, sizeof(lock_buffer), "%10d\n", pid);
798 write (fd, lock_buffer, 11);
800 write(fd, &pid, sizeof (pid));
809 * relock - called to update our lockfile when we are about to detach,
810 * thus changing our pid (we fork, the child carries on, and the parent dies).
811 * Note that this is called by the parent, with pid equal to the pid
812 * of the child. This avoids a potential race which would exist if
813 * we had the child rewrite the lockfile (the parent might die first,
814 * and another process could think the lock was stale if it checked
815 * between when the parent died and the child rewrote the lockfile).
821 /* XXX is there a way to do this? */
826 char lock_buffer[12];
828 if (lock_file[0] == 0)
830 fd = open(lock_file, O_WRONLY, 0);
832 error("Couldn't reopen lock file %s: %m", lock_file);
838 slprintf(lock_buffer, sizeof(lock_buffer), "%10d\n", pid);
839 write (fd, lock_buffer, 11);
841 write(fd, &pid, sizeof(pid));
842 #endif /* LOCK_BINARY */
850 * unlock - remove our lockfile
857 (void) rmlock(lock_file, (void *) 0);