[2f1b930] | 1 | /* |
---|
| 2 | * utils.c - various utility functions used in pppd. |
---|
| 3 | * |
---|
| 4 | * Copyright (c) 1999 The Australian National University. |
---|
| 5 | * All rights reserved. |
---|
| 6 | * |
---|
| 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. |
---|
| 18 | */ |
---|
| 19 | |
---|
| 20 | #define RCSID "$Id$" |
---|
| 21 | |
---|
| 22 | #include <stdio.h> |
---|
| 23 | #include <ctype.h> |
---|
| 24 | #include <stdlib.h> |
---|
| 25 | #include <string.h> |
---|
| 26 | #include <unistd.h> |
---|
| 27 | #include <signal.h> |
---|
| 28 | #include <errno.h> |
---|
| 29 | #include <fcntl.h> |
---|
| 30 | #include <netdb.h> |
---|
| 31 | #include <syslog.h> |
---|
| 32 | #include <pwd.h> |
---|
| 33 | #include <sys/param.h> |
---|
| 34 | #include <sys/types.h> |
---|
| 35 | #include <sys/wait.h> |
---|
| 36 | #include <sys/time.h> |
---|
| 37 | #include <sys/resource.h> |
---|
| 38 | #include <sys/stat.h> |
---|
| 39 | #include <sys/socket.h> |
---|
| 40 | #include <netinet/in.h> |
---|
| 41 | #ifdef SVR4 |
---|
| 42 | #include <sys/mkdev.h> |
---|
| 43 | #endif |
---|
| 44 | |
---|
| 45 | #include "pppd.h" |
---|
| 46 | |
---|
| 47 | static const char rcsid[] = RCSID; |
---|
| 48 | |
---|
| 49 | #if defined(SUNOS4) |
---|
| 50 | extern char *strerror(); |
---|
| 51 | #endif |
---|
| 52 | |
---|
| 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 *, ...), |
---|
| 57 | void *)); |
---|
| 58 | |
---|
| 59 | struct buffer_info { |
---|
| 60 | char *ptr; |
---|
| 61 | int len; |
---|
| 62 | }; |
---|
| 63 | |
---|
| 64 | /* |
---|
| 65 | * slprintf - format a message into a buffer. Like sprintf except we |
---|
| 66 | * also specify the length of the output buffer, and we handle |
---|
| 67 | * %r (recursive format), %m (error message), %v (visible string), |
---|
| 68 | * %q (quoted string), %t (current time) and %I (IP address) formats. |
---|
| 69 | * Doesn't do floating-point formats. |
---|
| 70 | * Returns the number of chars put into buf. |
---|
| 71 | */ |
---|
| 72 | int |
---|
| 73 | slprintf __V((char *buf, int buflen, char *fmt, ...)) |
---|
| 74 | { |
---|
| 75 | va_list args; |
---|
| 76 | int n; |
---|
| 77 | |
---|
| 78 | #if defined(__STDC__) |
---|
| 79 | va_start(args, fmt); |
---|
| 80 | #else |
---|
| 81 | char *buf; |
---|
| 82 | int buflen; |
---|
| 83 | char *fmt; |
---|
| 84 | va_start(args); |
---|
| 85 | buf = va_arg(args, char *); |
---|
| 86 | buflen = va_arg(args, int); |
---|
| 87 | fmt = va_arg(args, char *); |
---|
| 88 | #endif |
---|
| 89 | n = vslprintf(buf, buflen, fmt, args); |
---|
| 90 | va_end(args); |
---|
| 91 | return n; |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | /* |
---|
| 95 | * vslprintf - like slprintf, takes a va_list instead of a list of args. |
---|
| 96 | */ |
---|
| 97 | #define OUTCHAR(c) (buflen > 0? (--buflen, *buf++ = (c)): 0) |
---|
| 98 | |
---|
| 99 | int |
---|
| 100 | vslprintf(buf, buflen, fmt, args) |
---|
| 101 | char *buf; |
---|
| 102 | int buflen; |
---|
| 103 | char *fmt; |
---|
| 104 | va_list args; |
---|
| 105 | { |
---|
| 106 | int c, i, n; |
---|
| 107 | int width, prec, fillch; |
---|
| 108 | int base, len, neg, quoted; |
---|
| 109 | unsigned long val = 0; |
---|
| 110 | char *str, *f, *buf0; |
---|
| 111 | unsigned char *p; |
---|
| 112 | char num[32]; |
---|
| 113 | time_t t; |
---|
| 114 | u_int32_t ip; |
---|
| 115 | static char hexchars[] = "0123456789abcdef"; |
---|
| 116 | struct buffer_info bufinfo; |
---|
| 117 | |
---|
| 118 | buf0 = buf; |
---|
| 119 | --buflen; |
---|
| 120 | while (buflen > 0) { |
---|
| 121 | for (f = fmt; *f != '%' && *f != 0; ++f) |
---|
| 122 | ; |
---|
| 123 | if (f > fmt) { |
---|
| 124 | len = f - fmt; |
---|
| 125 | if (len > buflen) |
---|
| 126 | len = buflen; |
---|
| 127 | memcpy(buf, fmt, len); |
---|
| 128 | buf += len; |
---|
| 129 | buflen -= len; |
---|
| 130 | fmt = f; |
---|
| 131 | } |
---|
| 132 | if (*fmt == 0) |
---|
| 133 | break; |
---|
| 134 | c = *++fmt; |
---|
| 135 | width = 0; |
---|
| 136 | prec = -1; |
---|
| 137 | fillch = ' '; |
---|
| 138 | if (c == '0') { |
---|
| 139 | fillch = '0'; |
---|
| 140 | c = *++fmt; |
---|
| 141 | } |
---|
| 142 | if (c == '*') { |
---|
| 143 | width = va_arg(args, int); |
---|
| 144 | c = *++fmt; |
---|
| 145 | } else { |
---|
| 146 | while (isdigit(c)) { |
---|
| 147 | width = width * 10 + c - '0'; |
---|
| 148 | c = *++fmt; |
---|
| 149 | } |
---|
| 150 | } |
---|
| 151 | if (c == '.') { |
---|
| 152 | c = *++fmt; |
---|
| 153 | if (c == '*') { |
---|
| 154 | prec = va_arg(args, int); |
---|
| 155 | c = *++fmt; |
---|
| 156 | } else { |
---|
| 157 | prec = 0; |
---|
| 158 | while (isdigit(c)) { |
---|
| 159 | prec = prec * 10 + c - '0'; |
---|
| 160 | c = *++fmt; |
---|
| 161 | } |
---|
| 162 | } |
---|
| 163 | } |
---|
| 164 | str = 0; |
---|
| 165 | base = 0; |
---|
| 166 | neg = 0; |
---|
| 167 | ++fmt; |
---|
| 168 | switch (c) { |
---|
| 169 | case 'd': |
---|
| 170 | i = va_arg(args, int); |
---|
| 171 | if (i < 0) { |
---|
| 172 | neg = 1; |
---|
| 173 | val = -i; |
---|
| 174 | } else |
---|
| 175 | val = i; |
---|
| 176 | base = 10; |
---|
| 177 | break; |
---|
| 178 | case 'o': |
---|
| 179 | val = va_arg(args, unsigned int); |
---|
| 180 | base = 8; |
---|
| 181 | break; |
---|
| 182 | case 'x': |
---|
| 183 | case 'X': |
---|
| 184 | val = va_arg(args, unsigned int); |
---|
| 185 | base = 16; |
---|
| 186 | break; |
---|
| 187 | case 'p': |
---|
| 188 | val = (unsigned long) va_arg(args, void *); |
---|
| 189 | base = 16; |
---|
| 190 | neg = 2; |
---|
| 191 | break; |
---|
| 192 | case 's': |
---|
| 193 | str = va_arg(args, char *); |
---|
| 194 | break; |
---|
| 195 | case 'c': |
---|
| 196 | num[0] = va_arg(args, int); |
---|
| 197 | num[1] = 0; |
---|
| 198 | str = num; |
---|
| 199 | break; |
---|
| 200 | case 'm': |
---|
| 201 | str = strerror(errno); |
---|
| 202 | break; |
---|
| 203 | case 'I': |
---|
| 204 | ip = va_arg(args, u_int32_t); |
---|
| 205 | ip = ntohl(ip); |
---|
| 206 | slprintf(num, sizeof(num), "%d.%d.%d.%d", (ip >> 24) & 0xff, |
---|
| 207 | (ip >> 16) & 0xff, (ip >> 8) & 0xff, ip & 0xff); |
---|
| 208 | str = num; |
---|
| 209 | break; |
---|
| 210 | case 'r': |
---|
| 211 | f = va_arg(args, char *); |
---|
[299a523f] | 212 | #if !defined(__PPC__) && !defined(__i960__) |
---|
[2f1b930] | 213 | n = vslprintf(buf, buflen + 1, f, va_arg(args, va_list)); |
---|
| 214 | #else |
---|
[a81a8f8] | 215 | /* HACK: On the powerpc, a va_list is an array of 1 structure */ |
---|
[2f1b930] | 216 | n = vslprintf(buf, buflen + 1, f, va_arg(args, void *)); |
---|
| 217 | #endif |
---|
| 218 | buf += n; |
---|
| 219 | buflen -= n; |
---|
| 220 | continue; |
---|
| 221 | case 't': |
---|
| 222 | time(&t); |
---|
| 223 | str = ctime(&t); |
---|
| 224 | str += 4; /* chop off the day name */ |
---|
| 225 | str[15] = 0; /* chop off year and newline */ |
---|
| 226 | break; |
---|
| 227 | case 'v': /* "visible" string */ |
---|
| 228 | case 'q': /* quoted string */ |
---|
| 229 | quoted = c == 'q'; |
---|
| 230 | p = va_arg(args, unsigned char *); |
---|
| 231 | if (fillch == '0' && prec >= 0) { |
---|
| 232 | n = prec; |
---|
| 233 | } else { |
---|
| 234 | n = strlen((char *)p); |
---|
| 235 | if (prec >= 0 && n > prec) |
---|
| 236 | n = prec; |
---|
| 237 | } |
---|
| 238 | while (n > 0 && buflen > 0) { |
---|
| 239 | c = *p++; |
---|
| 240 | --n; |
---|
| 241 | if (!quoted && c >= 0x80) { |
---|
| 242 | OUTCHAR('M'); |
---|
| 243 | OUTCHAR('-'); |
---|
| 244 | c -= 0x80; |
---|
| 245 | } |
---|
| 246 | if (quoted && (c == '"' || c == '\\')) |
---|
| 247 | OUTCHAR('\\'); |
---|
| 248 | if (c < 0x20 || (0x7f <= c && c < 0xa0)) { |
---|
| 249 | if (quoted) { |
---|
| 250 | OUTCHAR('\\'); |
---|
| 251 | switch (c) { |
---|
| 252 | case '\t': OUTCHAR('t'); break; |
---|
| 253 | case '\n': OUTCHAR('n'); break; |
---|
| 254 | case '\b': OUTCHAR('b'); break; |
---|
| 255 | case '\f': OUTCHAR('f'); break; |
---|
| 256 | default: |
---|
| 257 | OUTCHAR('x'); |
---|
| 258 | OUTCHAR(hexchars[c >> 4]); |
---|
| 259 | OUTCHAR(hexchars[c & 0xf]); |
---|
| 260 | } |
---|
| 261 | } else { |
---|
| 262 | if (c == '\t') |
---|
| 263 | OUTCHAR(c); |
---|
| 264 | else { |
---|
| 265 | OUTCHAR('^'); |
---|
| 266 | OUTCHAR(c ^ 0x40); |
---|
| 267 | } |
---|
| 268 | } |
---|
| 269 | } else |
---|
| 270 | OUTCHAR(c); |
---|
| 271 | } |
---|
| 272 | continue; |
---|
| 273 | case 'P': /* print PPP packet */ |
---|
| 274 | bufinfo.ptr = buf; |
---|
| 275 | bufinfo.len = buflen + 1; |
---|
| 276 | p = va_arg(args, unsigned char *); |
---|
| 277 | n = va_arg(args, int); |
---|
| 278 | format_packet(p, n, vslp_printer, &bufinfo); |
---|
| 279 | buf = bufinfo.ptr; |
---|
| 280 | buflen = bufinfo.len - 1; |
---|
| 281 | continue; |
---|
| 282 | case 'B': |
---|
| 283 | p = va_arg(args, unsigned char *); |
---|
| 284 | for (n = prec; n > 0; --n) { |
---|
| 285 | c = *p++; |
---|
| 286 | if (fillch == ' ') |
---|
| 287 | OUTCHAR(' '); |
---|
| 288 | OUTCHAR(hexchars[(c >> 4) & 0xf]); |
---|
| 289 | OUTCHAR(hexchars[c & 0xf]); |
---|
| 290 | } |
---|
| 291 | continue; |
---|
| 292 | default: |
---|
| 293 | *buf++ = '%'; |
---|
| 294 | if (c != '%') |
---|
| 295 | --fmt; /* so %z outputs %z etc. */ |
---|
| 296 | --buflen; |
---|
| 297 | continue; |
---|
| 298 | } |
---|
| 299 | if (base != 0) { |
---|
| 300 | str = num + sizeof(num); |
---|
| 301 | *--str = 0; |
---|
| 302 | while (str > num + neg) { |
---|
| 303 | *--str = hexchars[val % base]; |
---|
| 304 | val = val / base; |
---|
| 305 | if (--prec <= 0 && val == 0) |
---|
| 306 | break; |
---|
| 307 | } |
---|
| 308 | switch (neg) { |
---|
| 309 | case 1: |
---|
| 310 | *--str = '-'; |
---|
| 311 | break; |
---|
| 312 | case 2: |
---|
| 313 | *--str = 'x'; |
---|
| 314 | *--str = '0'; |
---|
| 315 | break; |
---|
| 316 | } |
---|
| 317 | len = num + sizeof(num) - 1 - str; |
---|
| 318 | } else { |
---|
| 319 | len = strlen(str); |
---|
| 320 | if (prec >= 0 && len > prec) |
---|
| 321 | len = prec; |
---|
| 322 | } |
---|
| 323 | if (width > 0) { |
---|
| 324 | if (width > buflen) |
---|
| 325 | width = buflen; |
---|
| 326 | if ((n = width - len) > 0) { |
---|
| 327 | buflen -= n; |
---|
| 328 | for (; n > 0; --n) |
---|
| 329 | *buf++ = fillch; |
---|
| 330 | } |
---|
| 331 | } |
---|
| 332 | if (len > buflen) |
---|
| 333 | len = buflen; |
---|
| 334 | memcpy(buf, str, len); |
---|
| 335 | buf += len; |
---|
| 336 | buflen -= len; |
---|
| 337 | } |
---|
| 338 | *buf = 0; |
---|
| 339 | return buf - buf0; |
---|
| 340 | } |
---|
| 341 | |
---|
| 342 | /* |
---|
| 343 | * vslp_printer - used in processing a %P format |
---|
| 344 | */ |
---|
| 345 | static void |
---|
| 346 | vslp_printer __V((void *arg, char *fmt, ...)) |
---|
| 347 | { |
---|
| 348 | int n; |
---|
| 349 | va_list pvar; |
---|
| 350 | struct buffer_info *bi; |
---|
| 351 | |
---|
| 352 | #if defined(__STDC__) |
---|
| 353 | va_start(pvar, fmt); |
---|
| 354 | #else |
---|
| 355 | void *arg; |
---|
| 356 | char *fmt; |
---|
| 357 | va_start(pvar); |
---|
| 358 | arg = va_arg(pvar, void *); |
---|
| 359 | fmt = va_arg(pvar, char *); |
---|
| 360 | #endif |
---|
| 361 | |
---|
| 362 | bi = (struct buffer_info *) arg; |
---|
| 363 | n = vslprintf(bi->ptr, bi->len, fmt, pvar); |
---|
| 364 | va_end(pvar); |
---|
| 365 | |
---|
| 366 | bi->ptr += n; |
---|
| 367 | bi->len -= n; |
---|
| 368 | } |
---|
| 369 | |
---|
| 370 | /* |
---|
| 371 | * log_packet - format a packet and log it. |
---|
| 372 | */ |
---|
| 373 | |
---|
| 374 | char line[256]; /* line to be logged accumulated here */ |
---|
| 375 | char *linep; |
---|
| 376 | |
---|
| 377 | void |
---|
| 378 | log_packet(p, len, prefix, level) |
---|
| 379 | u_char *p; |
---|
| 380 | int len; |
---|
| 381 | char *prefix; |
---|
| 382 | int level; |
---|
| 383 | { |
---|
| 384 | strlcpy(line, prefix, sizeof(line)); |
---|
| 385 | linep = line + strlen(line); |
---|
| 386 | format_packet(p, len, pr_log, NULL); |
---|
| 387 | } |
---|
| 388 | |
---|
| 389 | /* |
---|
| 390 | * format_packet - make a readable representation of a packet, |
---|
| 391 | * calling `printer(arg, format, ...)' to output it. |
---|
| 392 | */ |
---|
| 393 | static void |
---|
| 394 | format_packet(p, len, printer, arg) |
---|
| 395 | u_char *p; |
---|
| 396 | int len; |
---|
| 397 | void (*printer) __P((void *, char *, ...)); |
---|
| 398 | void *arg; |
---|
| 399 | { |
---|
| 400 | int i, n; |
---|
| 401 | u_short proto; |
---|
| 402 | struct protent *protp; |
---|
| 403 | |
---|
| 404 | if (len >= PPP_HDRLEN && p[0] == PPP_ALLSTATIONS && p[1] == PPP_UI) { |
---|
| 405 | p += 2; |
---|
| 406 | GETSHORT(proto, p); |
---|
| 407 | len -= PPP_HDRLEN; |
---|
| 408 | for (i = 0; (protp = protocols[i]) != NULL; ++i) |
---|
| 409 | if (proto == protp->protocol) |
---|
| 410 | break; |
---|
| 411 | if (protp != NULL) { |
---|
| 412 | printer(arg, "[%s", protp->name); |
---|
| 413 | n = (*protp->printpkt)(p, len, printer, arg); |
---|
| 414 | printer(arg, "]"); |
---|
| 415 | p += n; |
---|
| 416 | len -= n; |
---|
| 417 | } else { |
---|
| 418 | for (i = 0; (protp = protocols[i]) != NULL; ++i) |
---|
| 419 | if (proto == (protp->protocol & ~0x8000)) |
---|
| 420 | break; |
---|
| 421 | if (protp != 0 && protp->data_name != 0) { |
---|
| 422 | printer(arg, "[%s data]", protp->data_name); |
---|
| 423 | if (len > 8) |
---|
| 424 | printer(arg, "%.8B ...", p); |
---|
| 425 | else |
---|
| 426 | printer(arg, "%.*B", len, p); |
---|
| 427 | len = 0; |
---|
| 428 | } else |
---|
| 429 | printer(arg, "[proto=0x%x]", proto); |
---|
| 430 | } |
---|
| 431 | } |
---|
| 432 | |
---|
| 433 | if (len > 32) |
---|
| 434 | printer(arg, "%.32B ...", p); |
---|
| 435 | else |
---|
| 436 | printer(arg, "%.*B", len, p); |
---|
| 437 | } |
---|
| 438 | |
---|
| 439 | static void |
---|
| 440 | pr_log __V((void *arg, char *fmt, ...)) |
---|
| 441 | { |
---|
| 442 | int n; |
---|
| 443 | va_list pvar; |
---|
| 444 | char buf[256]; |
---|
| 445 | |
---|
| 446 | #if defined(__STDC__) |
---|
| 447 | va_start(pvar, fmt); |
---|
| 448 | #else |
---|
| 449 | void *arg; |
---|
| 450 | char *fmt; |
---|
| 451 | va_start(pvar); |
---|
| 452 | arg = va_arg(pvar, void *); |
---|
| 453 | fmt = va_arg(pvar, char *); |
---|
| 454 | #endif |
---|
| 455 | |
---|
| 456 | n = vslprintf(buf, sizeof(buf), fmt, pvar); |
---|
| 457 | va_end(pvar); |
---|
| 458 | |
---|
| 459 | if (linep + n + 1 > line + sizeof(line)) { |
---|
| 460 | linep = line; |
---|
| 461 | } |
---|
| 462 | strlcpy(linep, buf, line + sizeof(line) - linep); |
---|
| 463 | linep += n; |
---|
| 464 | } |
---|
| 465 | |
---|
| 466 | /* |
---|
| 467 | * print_string - print a readable representation of a string using |
---|
| 468 | * printer. |
---|
| 469 | */ |
---|
| 470 | void |
---|
| 471 | print_string(p, len, printer, arg) |
---|
| 472 | char *p; |
---|
| 473 | int len; |
---|
| 474 | void (*printer) __P((void *, char *, ...)); |
---|
| 475 | void *arg; |
---|
| 476 | { |
---|
| 477 | int c; |
---|
| 478 | |
---|
| 479 | printer(arg, "\""); |
---|
| 480 | for (; len > 0; --len) { |
---|
| 481 | c = *p++; |
---|
| 482 | if (' ' <= c && c <= '~') { |
---|
| 483 | if (c == '\\' || c == '"') |
---|
| 484 | printer(arg, "\\"); |
---|
| 485 | printer(arg, "%c", c); |
---|
| 486 | } else { |
---|
| 487 | switch (c) { |
---|
| 488 | case '\n': |
---|
| 489 | printer(arg, "\\n"); |
---|
| 490 | break; |
---|
| 491 | case '\r': |
---|
| 492 | printer(arg, "\\r"); |
---|
| 493 | break; |
---|
| 494 | case '\t': |
---|
| 495 | printer(arg, "\\t"); |
---|
| 496 | break; |
---|
| 497 | default: |
---|
| 498 | printer(arg, "\\%.3o", c); |
---|
| 499 | } |
---|
| 500 | } |
---|
| 501 | } |
---|
| 502 | printer(arg, "\""); |
---|
| 503 | } |
---|
| 504 | |
---|
| 505 | /* |
---|
| 506 | * logit - does the hard work for fatal et al. |
---|
| 507 | */ |
---|
| 508 | static void |
---|
| 509 | logit(level, fmt, args) |
---|
| 510 | int level; |
---|
| 511 | char *fmt; |
---|
| 512 | va_list args; |
---|
| 513 | { |
---|
| 514 | int n; |
---|
| 515 | char buf[256]; |
---|
| 516 | |
---|
| 517 | n = vslprintf(buf, sizeof(buf), fmt, args); |
---|
| 518 | /* if (log_to_fd >= 0 && (level != LOG_DEBUG || debug)) { */ |
---|
| 519 | if (log_to_fd >= 0 && (debug)) { |
---|
| 520 | if (buf[n-1] != '\n') |
---|
| 521 | buf[n++] = '\n'; |
---|
| 522 | if (write(log_to_fd, buf, n) != n) |
---|
| 523 | log_to_fd = -1; |
---|
| 524 | } |
---|
| 525 | } |
---|
| 526 | |
---|
| 527 | /* |
---|
| 528 | * fatal - log an error message and die horribly. |
---|
| 529 | */ |
---|
| 530 | void |
---|
| 531 | pppd_fatal __V((char *fmt, ...)) |
---|
| 532 | { |
---|
| 533 | va_list pvar; |
---|
| 534 | |
---|
| 535 | #if defined(__STDC__) |
---|
| 536 | va_start(pvar, fmt); |
---|
| 537 | #else |
---|
| 538 | char *fmt; |
---|
| 539 | va_start(pvar); |
---|
| 540 | fmt = va_arg(pvar, char *); |
---|
| 541 | #endif |
---|
| 542 | |
---|
| 543 | logit(LOG_ERR, fmt, pvar); |
---|
| 544 | va_end(pvar); |
---|
| 545 | |
---|
| 546 | die(1); /* as promised */ |
---|
| 547 | } |
---|
| 548 | |
---|
| 549 | /* |
---|
| 550 | * error - log an error message. |
---|
| 551 | */ |
---|
| 552 | void |
---|
| 553 | pppd_error __V((char *fmt, ...)) |
---|
| 554 | { |
---|
| 555 | va_list pvar; |
---|
| 556 | |
---|
| 557 | #if defined(__STDC__) |
---|
| 558 | va_start(pvar, fmt); |
---|
| 559 | #else |
---|
| 560 | char *fmt; |
---|
| 561 | va_start(pvar); |
---|
| 562 | fmt = va_arg(pvar, char *); |
---|
| 563 | #endif |
---|
| 564 | |
---|
| 565 | logit(LOG_ERR, fmt, pvar); |
---|
| 566 | va_end(pvar); |
---|
| 567 | } |
---|
| 568 | |
---|
| 569 | /* |
---|
| 570 | * warn - log a warning message. |
---|
| 571 | */ |
---|
| 572 | void |
---|
| 573 | pppd_warn __V((char *fmt, ...)) |
---|
| 574 | { |
---|
| 575 | va_list pvar; |
---|
| 576 | |
---|
| 577 | #if defined(__STDC__) |
---|
| 578 | va_start(pvar, fmt); |
---|
| 579 | #else |
---|
| 580 | char *fmt; |
---|
| 581 | va_start(pvar); |
---|
| 582 | fmt = va_arg(pvar, char *); |
---|
| 583 | #endif |
---|
| 584 | |
---|
| 585 | logit(LOG_WARNING, fmt, pvar); |
---|
| 586 | va_end(pvar); |
---|
| 587 | } |
---|
| 588 | |
---|
| 589 | /* |
---|
| 590 | * notice - log a notice-level message. |
---|
| 591 | */ |
---|
| 592 | void |
---|
| 593 | pppd_notice __V((char *fmt, ...)) |
---|
| 594 | { |
---|
| 595 | va_list pvar; |
---|
| 596 | |
---|
| 597 | #if defined(__STDC__) |
---|
| 598 | va_start(pvar, fmt); |
---|
| 599 | #else |
---|
| 600 | char *fmt; |
---|
| 601 | va_start(pvar); |
---|
| 602 | fmt = va_arg(pvar, char *); |
---|
| 603 | #endif |
---|
| 604 | |
---|
| 605 | logit(LOG_NOTICE, fmt, pvar); |
---|
| 606 | va_end(pvar); |
---|
| 607 | } |
---|
| 608 | |
---|
| 609 | /* |
---|
| 610 | * info - log an informational message. |
---|
| 611 | */ |
---|
| 612 | void |
---|
| 613 | pppd_info __V((char *fmt, ...)) |
---|
| 614 | { |
---|
| 615 | va_list pvar; |
---|
| 616 | |
---|
| 617 | #if defined(__STDC__) |
---|
| 618 | va_start(pvar, fmt); |
---|
| 619 | #else |
---|
| 620 | char *fmt; |
---|
| 621 | va_start(pvar); |
---|
| 622 | fmt = va_arg(pvar, char *); |
---|
| 623 | #endif |
---|
| 624 | |
---|
| 625 | logit(LOG_INFO, fmt, pvar); |
---|
| 626 | va_end(pvar); |
---|
| 627 | } |
---|
| 628 | |
---|
| 629 | /* |
---|
| 630 | * dbglog - log a debug message. |
---|
| 631 | */ |
---|
| 632 | void |
---|
| 633 | pppd_dbglog __V((char *fmt, ...)) |
---|
| 634 | { |
---|
| 635 | va_list pvar; |
---|
| 636 | |
---|
| 637 | #if defined(__STDC__) |
---|
| 638 | va_start(pvar, fmt); |
---|
| 639 | #else |
---|
| 640 | char *fmt; |
---|
| 641 | va_start(pvar); |
---|
| 642 | fmt = va_arg(pvar, char *); |
---|
| 643 | #endif |
---|
| 644 | |
---|
| 645 | logit(LOG_DEBUG, fmt, pvar); |
---|
| 646 | va_end(pvar); |
---|
| 647 | } |
---|
| 648 | |
---|
| 649 | /* Procedures for locking the serial device using a lock file. */ |
---|
| 650 | #ifndef LOCK_DIR |
---|
| 651 | #ifdef _linux_ |
---|
| 652 | #define LOCK_DIR "/var/lock" |
---|
| 653 | #else |
---|
| 654 | #ifdef SVR4 |
---|
| 655 | #define LOCK_DIR "/var/spool/locks" |
---|
| 656 | #else |
---|
| 657 | #define LOCK_DIR "/var/spool/lock" |
---|
| 658 | #endif |
---|
| 659 | #endif |
---|
| 660 | #endif /* LOCK_DIR */ |
---|
| 661 | |
---|
| 662 | static char lock_file[MAXPATHLEN]; |
---|
| 663 | |
---|
| 664 | /* |
---|
| 665 | * lock - create a lock file for the named device |
---|
| 666 | */ |
---|
| 667 | int |
---|
| 668 | lock(dev) |
---|
| 669 | char *dev; |
---|
| 670 | { |
---|
| 671 | #ifdef LOCKLIB |
---|
| 672 | int result; |
---|
| 673 | |
---|
| 674 | result = mklock (dev, (void *) 0); |
---|
| 675 | if (result == 0) { |
---|
| 676 | strlcpy(lock_file, sizeof(lock_file), dev); |
---|
| 677 | return 0; |
---|
| 678 | } |
---|
| 679 | |
---|
| 680 | if (result > 0) |
---|
| 681 | notice("Device %s is locked by pid %d", dev, result); |
---|
| 682 | else |
---|
| 683 | error("Can't create lock file %s", lock_file); |
---|
| 684 | return -1; |
---|
| 685 | |
---|
| 686 | #else /* LOCKLIB */ |
---|
| 687 | |
---|
| 688 | char lock_buffer[12]; |
---|
| 689 | int fd, pid, n; |
---|
| 690 | |
---|
| 691 | #ifdef SVR4 |
---|
| 692 | struct stat sbuf; |
---|
| 693 | |
---|
| 694 | if (stat(dev, &sbuf) < 0) { |
---|
| 695 | error("Can't get device number for %s: %m", dev); |
---|
| 696 | return -1; |
---|
| 697 | } |
---|
| 698 | if ((sbuf.st_mode & S_IFMT) != S_IFCHR) { |
---|
| 699 | error("Can't lock %s: not a character device", dev); |
---|
| 700 | return -1; |
---|
| 701 | } |
---|
| 702 | slprintf(lock_file, sizeof(lock_file), "%s/LK.%03d.%03d.%03d", |
---|
| 703 | LOCK_DIR, major(sbuf.st_dev), |
---|
| 704 | major(sbuf.st_rdev), minor(sbuf.st_rdev)); |
---|
| 705 | #else |
---|
| 706 | char *p; |
---|
| 707 | |
---|
| 708 | if ((p = strrchr(dev, '/')) != NULL) |
---|
| 709 | dev = p + 1; |
---|
| 710 | slprintf(lock_file, sizeof(lock_file), "%s/LCK..%s", LOCK_DIR, dev); |
---|
| 711 | #endif |
---|
| 712 | |
---|
| 713 | while ((fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644)) < 0) { |
---|
| 714 | if (errno != EEXIST) { |
---|
| 715 | error("Can't create lock file %s: %m", lock_file); |
---|
| 716 | break; |
---|
| 717 | } |
---|
| 718 | |
---|
| 719 | /* Read the lock file to find out who has the device locked. */ |
---|
| 720 | fd = open(lock_file, O_RDONLY, 0); |
---|
| 721 | if (fd < 0) { |
---|
| 722 | if (errno == ENOENT) /* This is just a timing problem. */ |
---|
| 723 | continue; |
---|
| 724 | error("Can't open existing lock file %s: %m", lock_file); |
---|
| 725 | break; |
---|
| 726 | } |
---|
| 727 | #ifndef LOCK_BINARY |
---|
| 728 | n = read(fd, lock_buffer, 11); |
---|
| 729 | #else |
---|
| 730 | n = read(fd, &pid, sizeof(pid)); |
---|
| 731 | #endif /* LOCK_BINARY */ |
---|
| 732 | close(fd); |
---|
| 733 | fd = -1; |
---|
| 734 | if (n <= 0) { |
---|
| 735 | error("Can't read pid from lock file %s", lock_file); |
---|
| 736 | break; |
---|
| 737 | } |
---|
| 738 | |
---|
| 739 | /* See if the process still exists. */ |
---|
| 740 | #ifndef LOCK_BINARY |
---|
| 741 | lock_buffer[n] = 0; |
---|
| 742 | pid = atoi(lock_buffer); |
---|
| 743 | #endif /* LOCK_BINARY */ |
---|
| 744 | if (pid == getpid()) |
---|
| 745 | return 1; /* somebody else locked it for us */ |
---|
| 746 | if (pid == 0 |
---|
| 747 | || (kill(pid, 0) == -1 && errno == ESRCH)) { |
---|
| 748 | if (unlink (lock_file) == 0) { |
---|
| 749 | notice("Removed stale lock on %s (pid %d)", dev, pid); |
---|
| 750 | continue; |
---|
| 751 | } |
---|
| 752 | warn("Couldn't remove stale lock on %s", dev); |
---|
| 753 | } else |
---|
| 754 | notice("Device %s is locked by pid %d", dev, pid); |
---|
| 755 | break; |
---|
| 756 | } |
---|
| 757 | |
---|
| 758 | if (fd < 0) { |
---|
| 759 | lock_file[0] = 0; |
---|
| 760 | return -1; |
---|
| 761 | } |
---|
| 762 | |
---|
| 763 | pid = getpid(); |
---|
| 764 | #ifndef LOCK_BINARY |
---|
| 765 | slprintf(lock_buffer, sizeof(lock_buffer), "%10d\n", pid); |
---|
| 766 | write (fd, lock_buffer, 11); |
---|
| 767 | #else |
---|
| 768 | write(fd, &pid, sizeof (pid)); |
---|
| 769 | #endif |
---|
| 770 | close(fd); |
---|
| 771 | return 0; |
---|
| 772 | |
---|
| 773 | #endif |
---|
| 774 | } |
---|
| 775 | |
---|
| 776 | /* |
---|
| 777 | * relock - called to update our lockfile when we are about to detach, |
---|
| 778 | * thus changing our pid (we fork, the child carries on, and the parent dies). |
---|
| 779 | * Note that this is called by the parent, with pid equal to the pid |
---|
| 780 | * of the child. This avoids a potential race which would exist if |
---|
| 781 | * we had the child rewrite the lockfile (the parent might die first, |
---|
| 782 | * and another process could think the lock was stale if it checked |
---|
| 783 | * between when the parent died and the child rewrote the lockfile). |
---|
| 784 | */ |
---|
| 785 | int |
---|
| 786 | relock(pid) |
---|
| 787 | int pid; |
---|
| 788 | { |
---|
| 789 | #ifdef LOCKLIB |
---|
| 790 | /* XXX is there a way to do this? */ |
---|
| 791 | return -1; |
---|
| 792 | #else /* LOCKLIB */ |
---|
| 793 | |
---|
| 794 | int fd; |
---|
| 795 | char lock_buffer[12]; |
---|
| 796 | |
---|
| 797 | if (lock_file[0] == 0) |
---|
| 798 | return -1; |
---|
| 799 | fd = open(lock_file, O_WRONLY, 0); |
---|
| 800 | if (fd < 0) { |
---|
| 801 | error("Couldn't reopen lock file %s: %m", lock_file); |
---|
| 802 | lock_file[0] = 0; |
---|
| 803 | return -1; |
---|
| 804 | } |
---|
| 805 | |
---|
| 806 | #ifndef LOCK_BINARY |
---|
| 807 | slprintf(lock_buffer, sizeof(lock_buffer), "%10d\n", pid); |
---|
| 808 | write (fd, lock_buffer, 11); |
---|
| 809 | #else |
---|
| 810 | write(fd, &pid, sizeof(pid)); |
---|
| 811 | #endif /* LOCK_BINARY */ |
---|
| 812 | close(fd); |
---|
| 813 | return 0; |
---|
| 814 | |
---|
| 815 | #endif /* LOCKLIB */ |
---|
| 816 | } |
---|
| 817 | |
---|
| 818 | /* |
---|
| 819 | * unlock - remove our lockfile |
---|
| 820 | */ |
---|
| 821 | void |
---|
| 822 | unlock() |
---|
| 823 | { |
---|
| 824 | if (lock_file[0]) { |
---|
| 825 | #ifdef LOCKLIB |
---|
| 826 | (void) rmlock(lock_file, (void *) 0); |
---|
| 827 | #else |
---|
| 828 | unlink(lock_file); |
---|
| 829 | #endif |
---|
| 830 | lock_file[0] = 0; |
---|
| 831 | } |
---|
| 832 | } |
---|