syslogd.c revision 19963
1/* 2 * Copyright (c) 1983, 1988, 1993, 1994 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34#ifndef lint 35static const char copyright[] = 36"@(#) Copyright (c) 1983, 1988, 1993, 1994\n\ 37 The Regents of the University of California. All rights reserved.\n"; 38/* 39static char sccsid[] = "@(#)syslogd.c 8.3 (Berkeley) 4/4/94"; 40*/ 41static const char rcsid[] = 42 "$Id: syslogd.c,v 1.14 1996/11/26 02:24:42 peter Exp $"; 43#endif /* not lint */ 44 45/* 46 * syslogd -- log system messages 47 * 48 * This program implements a system log. It takes a series of lines. 49 * Each line may have a priority, signified as "<n>" as 50 * the first characters of the line. If this is 51 * not present, a default priority is used. 52 * 53 * To kill syslogd, send a signal 15 (terminate). A signal 1 (hup) will 54 * cause it to reread its configuration file. 55 * 56 * Defined Constants: 57 * 58 * MAXLINE -- the maximimum line length that can be handled. 59 * DEFUPRI -- the default priority for user messages 60 * DEFSPRI -- the default priority for kernel messages 61 * 62 * Author: Eric Allman 63 * extensive changes by Ralph Campbell 64 * more extensive changes by Eric Allman (again) 65 * Extension to log by program name as well as facility and priority 66 * by Peter da Silva. 67 */ 68 69#define MAXLINE 1024 /* maximum line length */ 70#define MAXSVLINE 120 /* maximum saved line length */ 71#define DEFUPRI (LOG_USER|LOG_NOTICE) 72#define DEFSPRI (LOG_KERN|LOG_CRIT) 73#define TIMERINTVL 30 /* interval for checking flush, mark */ 74#define TTYMSGTIME 1 /* timed out passed to ttymsg */ 75 76#include <sys/param.h> 77#include <sys/ioctl.h> 78#include <sys/stat.h> 79#include <sys/wait.h> 80#include <sys/socket.h> 81#include <sys/msgbuf.h> 82#include <sys/uio.h> 83#include <sys/un.h> 84#include <sys/time.h> 85#include <sys/resource.h> 86#include <sys/syslimits.h> 87#include <paths.h> 88 89#include <netinet/in.h> 90#include <netdb.h> 91#include <arpa/inet.h> 92 93#include <ctype.h> 94#include <errno.h> 95#include <fcntl.h> 96#include <setjmp.h> 97#include <signal.h> 98#include <stdio.h> 99#include <stdlib.h> 100#include <string.h> 101#include <unistd.h> 102#include <utmp.h> 103#include "pathnames.h" 104 105#define SYSLOG_NAMES 106#include <sys/syslog.h> 107 108const char *LogName = _PATH_LOG; 109const char *ConfFile = _PATH_LOGCONF; 110const char *PidFile = _PATH_LOGPID; 111const char ctty[] = _PATH_CONSOLE; 112 113#define FDMASK(fd) (1 << (fd)) 114 115#define dprintf if (Debug) printf 116 117#define MAXUNAMES 20 /* maximum number of user names */ 118 119/* 120 * Flags to logmsg(). 121 */ 122 123#define IGN_CONS 0x001 /* don't print on console */ 124#define SYNC_FILE 0x002 /* do fsync on file after printing */ 125#define ADDDATE 0x004 /* add a date to the message */ 126#define MARK 0x008 /* this message is a mark */ 127 128/* 129 * This structure represents the files that will have log 130 * copies printed. 131 */ 132 133struct filed { 134 struct filed *f_next; /* next in linked list */ 135 short f_type; /* entry type, see below */ 136 short f_file; /* file descriptor */ 137 time_t f_time; /* time this was last written */ 138 u_char f_pmask[LOG_NFACILITIES+1]; /* priority mask */ 139 char *f_program; /* program this applies to */ 140 union { 141 char f_uname[MAXUNAMES][UT_NAMESIZE+1]; 142 struct { 143 char f_hname[MAXHOSTNAMELEN+1]; 144 struct sockaddr_in f_addr; 145 } f_forw; /* forwarding address */ 146 char f_fname[MAXPATHLEN]; 147 } f_un; 148 char f_prevline[MAXSVLINE]; /* last message logged */ 149 char f_lasttime[16]; /* time of last occurrence */ 150 char f_prevhost[MAXHOSTNAMELEN+1]; /* host from which recd. */ 151 int f_prevpri; /* pri of f_prevline */ 152 int f_prevlen; /* length of f_prevline */ 153 int f_prevcount; /* repetition cnt of prevline */ 154 int f_repeatcount; /* number of "repeated" msgs */ 155}; 156 157/* 158 * Intervals at which we flush out "message repeated" messages, 159 * in seconds after previous message is logged. After each flush, 160 * we move to the next interval until we reach the largest. 161 */ 162int repeatinterval[] = { 30, 120, 600 }; /* # of secs before flush */ 163#define MAXREPEAT ((sizeof(repeatinterval) / sizeof(repeatinterval[0])) - 1) 164#define REPEATTIME(f) ((f)->f_time + repeatinterval[(f)->f_repeatcount]) 165#define BACKOFF(f) { if (++(f)->f_repeatcount > MAXREPEAT) \ 166 (f)->f_repeatcount = MAXREPEAT; \ 167 } 168 169/* values for f_type */ 170#define F_UNUSED 0 /* unused entry */ 171#define F_FILE 1 /* regular file */ 172#define F_TTY 2 /* terminal */ 173#define F_CONSOLE 3 /* console terminal */ 174#define F_FORW 4 /* remote machine */ 175#define F_USERS 5 /* list of users */ 176#define F_WALL 6 /* everyone logged on */ 177 178char *TypeNames[7] = { 179 "UNUSED", "FILE", "TTY", "CONSOLE", 180 "FORW", "USERS", "WALL" 181}; 182 183struct filed *Files; 184struct filed consfile; 185 186int Debug; /* debug flag */ 187char LocalHostName[MAXHOSTNAMELEN+1]; /* our hostname */ 188char *LocalDomain; /* our local domain name */ 189int InetInuse = 0; /* non-zero if INET sockets are being used */ 190int finet; /* Internet datagram socket */ 191int LogPort; /* port number for INET connections */ 192int Initialized = 0; /* set when we have initialized ourselves */ 193int MarkInterval = 20 * 60; /* interval between marks in seconds */ 194int MarkSeq = 0; /* mark sequence number */ 195int SecureMode = 0; /* when true, speak only unix domain socks */ 196 197int created_lsock = 0; /* Flag if local socket created */ 198char bootfile[MAXLINE+1]; /* booted kernel file */ 199 200void cfline __P((char *, struct filed *, char *)); 201char *cvthname __P((struct sockaddr_in *)); 202int decode __P((const char *, CODE *)); 203void die __P((int)); 204void domark __P((int)); 205void fprintlog __P((struct filed *, int, char *)); 206void init __P((int)); 207void logerror __P((const char *)); 208void logmsg __P((int, char *, char *, int)); 209void printline __P((char *, char *)); 210void printsys __P((char *)); 211void reapchild __P((int)); 212char *ttymsg __P((struct iovec *, int, char *, int)); 213void usage __P((void)); 214void wallmsg __P((struct filed *, struct iovec *)); 215int waitdaemon __P((int, int, int)); 216void timedout __P((int)); 217 218int 219main(argc, argv) 220 int argc; 221 char *argv[]; 222{ 223 int ch, funix, i, inetm, fklog, klogm, len; 224 struct sockaddr_un sunx, fromunix; 225 struct sockaddr_in sin, frominet; 226 FILE *fp; 227 char *p, line[MSG_BSIZE + 1]; 228 struct timeval tv, *tvp; 229 pid_t ppid; 230 231 while ((ch = getopt(argc, argv, "dsf:Im:p:")) != EOF) 232 switch(ch) { 233 case 'd': /* debug */ 234 Debug++; 235 break; 236 case 'f': /* configuration file */ 237 ConfFile = optarg; 238 break; 239 case 'm': /* mark interval */ 240 MarkInterval = atoi(optarg) * 60; 241 break; 242 case 'p': /* path */ 243 LogName = optarg; 244 break; 245 case 'I': /* backwards compatible w/FreeBSD */ 246 case 's': /* no network mode */ 247 SecureMode++; 248 break; 249 case '?': 250 default: 251 usage(); 252 } 253 if ((argc -= optind) != 0) 254 usage(); 255 256 if (!Debug) { 257 ppid = waitdaemon(0, 0, 30); 258 if (ppid < 0) 259 err(1, "could not become daemon"); 260 } else 261 setlinebuf(stdout); 262 263 consfile.f_type = F_CONSOLE; 264 (void)strcpy(consfile.f_un.f_fname, ctty + sizeof _PATH_DEV - 1); 265 (void)gethostname(LocalHostName, sizeof(LocalHostName)); 266 if ((p = strchr(LocalHostName, '.')) != NULL) { 267 *p++ = '\0'; 268 LocalDomain = p; 269 } else 270 LocalDomain = ""; 271 (void)strcpy(bootfile, getbootfile()); 272 (void)signal(SIGTERM, die); 273 (void)signal(SIGINT, Debug ? die : SIG_IGN); 274 (void)signal(SIGQUIT, Debug ? die : SIG_IGN); 275 (void)signal(SIGCHLD, reapchild); 276 (void)signal(SIGALRM, domark); 277 (void)alarm(TIMERINTVL); 278 279#ifndef SUN_LEN 280#define SUN_LEN(unp) (strlen((unp)->sun_path) + 2) 281#endif 282 memset(&sunx, 0, sizeof(sunx)); 283 sunx.sun_family = AF_UNIX; 284 (void)strncpy(sunx.sun_path, LogName, sizeof(sunx.sun_path)); 285 funix = socket(AF_UNIX, SOCK_DGRAM, 0); 286 if (funix < 0 || 287 bind(funix, (struct sockaddr *)&sunx, SUN_LEN(&sunx)) < 0 || 288 chmod(LogName, 0666) < 0) { 289 (void) sprintf(line, "cannot create %s", LogName); 290 logerror(line); 291 dprintf("cannot create %s (%d)\n", LogName, errno); 292 die(0); 293 } else 294 created_lsock = 1; 295 296 if (!SecureMode) 297 finet = socket(AF_INET, SOCK_DGRAM, 0); 298 else 299 finet = -1; 300 301 inetm = 0; 302 if (finet >= 0) { 303 struct servent *sp; 304 305 sp = getservbyname("syslog", "udp"); 306 if (sp == NULL) { 307 errno = 0; 308 logerror("syslog/udp: unknown service"); 309 die(0); 310 } 311 memset(&sin, 0, sizeof(sin)); 312 sin.sin_family = AF_INET; 313 sin.sin_port = LogPort = sp->s_port; 314 if (bind(finet, (struct sockaddr *)&sin, sizeof(sin)) < 0) { 315 logerror("bind"); 316 if (!Debug) 317 die(0); 318 } else { 319 inetm = FDMASK(finet); 320 InetInuse = 1; 321 } 322 } 323 if ((fklog = open(_PATH_KLOG, O_RDONLY, 0)) >= 0) 324 klogm = FDMASK(fklog); 325 else { 326 dprintf("can't open %s (%d)\n", _PATH_KLOG, errno); 327 klogm = 0; 328 } 329 330 /* tuck my process id away */ 331 fp = fopen(PidFile, "w"); 332 if (fp != NULL) { 333 fprintf(fp, "%d\n", getpid()); 334 (void) fclose(fp); 335 } 336 337 dprintf("off & running....\n"); 338 339 init(0); 340 (void)signal(SIGHUP, init); 341 342 tvp = &tv; 343 tv.tv_sec = tv.tv_usec = 0; 344 345 for (;;) { 346 int nfds, readfds = FDMASK(funix) | inetm | klogm; 347 348 dprintf("readfds = %#x\n", readfds); 349 nfds = select(20, (fd_set *)&readfds, (fd_set *)NULL, 350 (fd_set *)NULL, tvp); 351 if (nfds == 0) { 352 if (tvp) { 353 tvp = NULL; 354 if (ppid != 1) 355 kill(ppid, SIGALRM); 356 } 357 continue; 358 } 359 if (nfds < 0) { 360 if (errno != EINTR) 361 logerror("select"); 362 continue; 363 } 364 dprintf("got a message (%d, %#x)\n", nfds, readfds); 365 if (readfds & klogm) { 366 i = read(fklog, line, sizeof(line) - 1); 367 if (i > 0) { 368 line[i] = '\0'; 369 printsys(line); 370 } else if (i < 0 && errno != EINTR) { 371 logerror("klog"); 372 fklog = -1; 373 klogm = 0; 374 } 375 } 376 if (readfds & FDMASK(funix)) { 377 len = sizeof(fromunix); 378 i = recvfrom(funix, line, MAXLINE, 0, 379 (struct sockaddr *)&fromunix, &len); 380 if (i > 0) { 381 line[i] = '\0'; 382 printline(LocalHostName, line); 383 } else if (i < 0 && errno != EINTR) 384 logerror("recvfrom unix"); 385 } 386 if (readfds & inetm) { 387 len = sizeof(frominet); 388 i = recvfrom(finet, line, MAXLINE, 0, 389 (struct sockaddr *)&frominet, &len); 390 if (i > 0) { 391 line[i] = '\0'; 392 printline(cvthname(&frominet), line); 393 } else if (i < 0 && errno != EINTR) 394 logerror("recvfrom inet"); 395 } 396 } 397} 398 399void 400usage() 401{ 402 403 fprintf(stderr, 404 "usage: syslogd [-ds] [-f conffile] [-m markinterval]" 405 " [-p logpath]\n"); 406 exit(1); 407} 408 409/* 410 * Take a raw input line, decode the message, and print the message 411 * on the appropriate log files. 412 */ 413void 414printline(hname, msg) 415 char *hname; 416 char *msg; 417{ 418 int c, pri; 419 char *p, *q, line[MAXLINE + 1]; 420 421 /* test for special codes */ 422 pri = DEFUPRI; 423 p = msg; 424 if (*p == '<') { 425 pri = 0; 426 while (isdigit(*++p)) 427 pri = 10 * pri + (*p - '0'); 428 if (*p == '>') 429 ++p; 430 } 431 if (pri &~ (LOG_FACMASK|LOG_PRIMASK)) 432 pri = DEFUPRI; 433 434 /* don't allow users to log kernel messages */ 435 if (LOG_FAC(pri) == LOG_KERN) 436 pri = LOG_MAKEPRI(LOG_USER, LOG_PRI(pri)); 437 438 q = line; 439 440 while ((c = *p++ & 0177) != '\0' && 441 q < &line[sizeof(line) - 1]) 442 if (iscntrl(c)) 443 if (c == '\n') 444 *q++ = ' '; 445 else if (c == '\t') 446 *q++ = '\t'; 447 else { 448 *q++ = '^'; 449 *q++ = c ^ 0100; 450 } 451 else 452 *q++ = c; 453 *q = '\0'; 454 455 logmsg(pri, line, hname, 0); 456} 457 458/* 459 * Take a raw input line from /dev/klog, split and format similar to syslog(). 460 */ 461void 462printsys(msg) 463 char *msg; 464{ 465 int c, pri, flags; 466 char *lp, *p, *q, line[MAXLINE + 1]; 467 468 (void)strcpy(line, bootfile); 469 (void)strcat(line, ": "); 470 lp = line + strlen(line); 471 for (p = msg; *p != '\0'; ) { 472 flags = SYNC_FILE | ADDDATE; /* fsync file after write */ 473 pri = DEFSPRI; 474 if (*p == '<') { 475 pri = 0; 476 while (isdigit(*++p)) 477 pri = 10 * pri + (*p - '0'); 478 if (*p == '>') 479 ++p; 480 } else { 481 /* kernel printf's come out on console */ 482 flags |= IGN_CONS; 483 } 484 if (pri &~ (LOG_FACMASK|LOG_PRIMASK)) 485 pri = DEFSPRI; 486 q = lp; 487 while (*p != '\0' && (c = *p++) != '\n' && 488 q < &line[MAXLINE]) 489 *q++ = c; 490 *q = '\0'; 491 logmsg(pri, line, LocalHostName, flags); 492 } 493} 494 495time_t now; 496 497/* 498 * Log a message to the appropriate log files, users, etc. based on 499 * the priority. 500 */ 501void 502logmsg(pri, msg, from, flags) 503 int pri; 504 char *msg, *from; 505 int flags; 506{ 507 struct filed *f; 508 int fac, msglen, omask, prilev; 509 char *timestamp; 510 char prog[NAME_MAX+1]; 511 int i; 512 513 dprintf("logmsg: pri %o, flags %x, from %s, msg %s\n", 514 pri, flags, from, msg); 515 516 omask = sigblock(sigmask(SIGHUP)|sigmask(SIGALRM)); 517 518 /* 519 * Check to see if msg looks non-standard. 520 */ 521 msglen = strlen(msg); 522 if (msglen < 16 || msg[3] != ' ' || msg[6] != ' ' || 523 msg[9] != ':' || msg[12] != ':' || msg[15] != ' ') 524 flags |= ADDDATE; 525 526 (void)time(&now); 527 if (flags & ADDDATE) 528 timestamp = ctime(&now) + 4; 529 else { 530 timestamp = msg; 531 msg += 16; 532 msglen -= 16; 533 } 534 535 /* skip leading blanks */ 536 while(isspace(*msg)) { 537 msg++; 538 msglen--; 539 } 540 541 /* extract facility and priority level */ 542 if (flags & MARK) 543 fac = LOG_NFACILITIES; 544 else 545 fac = LOG_FAC(pri); 546 prilev = LOG_PRI(pri); 547 548 /* extract program name */ 549 for(i = 0; i < NAME_MAX; i++) { 550 if(!isalnum(msg[i])) 551 break; 552 prog[i] = msg[i]; 553 } 554 prog[i] = 0; 555 556 /* log the message to the particular outputs */ 557 if (!Initialized) { 558 f = &consfile; 559 f->f_file = open(ctty, O_WRONLY, 0); 560 561 if (f->f_file >= 0) { 562 fprintlog(f, flags, msg); 563 (void)close(f->f_file); 564 } 565 (void)sigsetmask(omask); 566 return; 567 } 568 for (f = Files; f; f = f->f_next) { 569 /* skip messages that are incorrect priority */ 570 if (f->f_pmask[fac] < prilev || 571 f->f_pmask[fac] == INTERNAL_NOPRI) 572 continue; 573 /* skip messages with the incorrect program name */ 574 if(f->f_program) 575 if(strcmp(prog, f->f_program) != 0) 576 continue; 577 578 if (f->f_type == F_CONSOLE && (flags & IGN_CONS)) 579 continue; 580 581 /* don't output marks to recently written files */ 582 if ((flags & MARK) && (now - f->f_time) < MarkInterval / 2) 583 continue; 584 585 /* 586 * suppress duplicate lines to this file 587 */ 588 if ((flags & MARK) == 0 && msglen == f->f_prevlen && 589 !strcmp(msg, f->f_prevline) && 590 !strcmp(from, f->f_prevhost)) { 591 (void)strncpy(f->f_lasttime, timestamp, 15); 592 f->f_prevcount++; 593 dprintf("msg repeated %d times, %ld sec of %d\n", 594 f->f_prevcount, now - f->f_time, 595 repeatinterval[f->f_repeatcount]); 596 /* 597 * If domark would have logged this by now, 598 * flush it now (so we don't hold isolated messages), 599 * but back off so we'll flush less often 600 * in the future. 601 */ 602 if (now > REPEATTIME(f)) { 603 fprintlog(f, flags, (char *)NULL); 604 BACKOFF(f); 605 } 606 } else { 607 /* new line, save it */ 608 if (f->f_prevcount) 609 fprintlog(f, 0, (char *)NULL); 610 f->f_repeatcount = 0; 611 f->f_prevpri = pri; 612 (void)strncpy(f->f_lasttime, timestamp, 15); 613 (void)strncpy(f->f_prevhost, from, 614 sizeof(f->f_prevhost)); 615 if (msglen < MAXSVLINE) { 616 f->f_prevlen = msglen; 617 (void)strcpy(f->f_prevline, msg); 618 fprintlog(f, flags, (char *)NULL); 619 } else { 620 f->f_prevline[0] = 0; 621 f->f_prevlen = 0; 622 fprintlog(f, flags, msg); 623 } 624 } 625 } 626 (void)sigsetmask(omask); 627} 628 629void 630fprintlog(f, flags, msg) 631 struct filed *f; 632 int flags; 633 char *msg; 634{ 635 struct iovec iov[6]; 636 struct iovec *v; 637 int l; 638 char line[MAXLINE + 1], repbuf[80], greetings[200]; 639 char *msgret; 640 641 v = iov; 642 if (f->f_type == F_WALL) { 643 v->iov_base = greetings; 644 v->iov_len = sprintf(greetings, 645 "\r\n\7Message from syslogd@%s at %.24s ...\r\n", 646 f->f_prevhost, ctime(&now)); 647 v++; 648 v->iov_base = ""; 649 v->iov_len = 0; 650 v++; 651 } else { 652 v->iov_base = f->f_lasttime; 653 v->iov_len = 15; 654 v++; 655 v->iov_base = " "; 656 v->iov_len = 1; 657 v++; 658 } 659 v->iov_base = f->f_prevhost; 660 v->iov_len = strlen(v->iov_base); 661 v++; 662 v->iov_base = " "; 663 v->iov_len = 1; 664 v++; 665 666 if (msg) { 667 v->iov_base = msg; 668 v->iov_len = strlen(msg); 669 } else if (f->f_prevcount > 1) { 670 v->iov_base = repbuf; 671 v->iov_len = sprintf(repbuf, "last message repeated %d times", 672 f->f_prevcount); 673 } else { 674 v->iov_base = f->f_prevline; 675 v->iov_len = f->f_prevlen; 676 } 677 v++; 678 679 dprintf("Logging to %s", TypeNames[f->f_type]); 680 f->f_time = now; 681 682 switch (f->f_type) { 683 case F_UNUSED: 684 dprintf("\n"); 685 break; 686 687 case F_FORW: 688 dprintf(" %s\n", f->f_un.f_forw.f_hname); 689 l = sprintf(line, "<%d>%.15s %s", f->f_prevpri, 690 iov[0].iov_base, iov[4].iov_base); 691 if (l > MAXLINE) 692 l = MAXLINE; 693 if ((finet >= 0) && 694 (sendto(finet, line, l, 0, 695 (struct sockaddr *)&f->f_un.f_forw.f_addr, 696 sizeof(f->f_un.f_forw.f_addr)) != l)) { 697 int e = errno; 698 (void)close(f->f_file); 699 f->f_type = F_UNUSED; 700 errno = e; 701 logerror("sendto"); 702 } 703 break; 704 705 case F_FILE: 706 dprintf(" %s\n", f->f_un.f_fname); 707 v->iov_base = "\n"; 708 v->iov_len = 1; 709 if (writev(f->f_file, iov, 6) < 0) { 710 int e = errno; 711 (void)close(f->f_file); 712 f->f_type = F_UNUSED; 713 errno = e; 714 logerror(f->f_un.f_fname); 715 } else if (flags & SYNC_FILE) 716 (void)fsync(f->f_file); 717 break; 718 719 case F_CONSOLE: 720 if (flags & IGN_CONS) { 721 dprintf(" (ignored)\n"); 722 break; 723 } 724 /* FALLTHROUGH */ 725 726 case F_TTY: 727 dprintf(" %s%s\n", _PATH_DEV, f->f_un.f_fname); 728 v->iov_base = "\r\n"; 729 v->iov_len = 2; 730 731 errno = 0; /* ttymsg() only sometimes returns an errno */ 732 if ((msgret = ttymsg(iov, 6, f->f_un.f_fname, 10))) { 733 f->f_type = F_UNUSED; 734 logerror(msgret); 735 } 736 break; 737 738 case F_USERS: 739 case F_WALL: 740 dprintf("\n"); 741 v->iov_base = "\r\n"; 742 v->iov_len = 2; 743 wallmsg(f, iov); 744 break; 745 } 746 f->f_prevcount = 0; 747} 748 749/* 750 * WALLMSG -- Write a message to the world at large 751 * 752 * Write the specified message to either the entire 753 * world, or a list of approved users. 754 */ 755void 756wallmsg(f, iov) 757 struct filed *f; 758 struct iovec *iov; 759{ 760 static int reenter; /* avoid calling ourselves */ 761 FILE *uf; 762 struct utmp ut; 763 int i; 764 char *p; 765 char line[sizeof(ut.ut_line) + 1]; 766 767 if (reenter++) 768 return; 769 if ((uf = fopen(_PATH_UTMP, "r")) == NULL) { 770 logerror(_PATH_UTMP); 771 reenter = 0; 772 return; 773 } 774 /* NOSTRICT */ 775 while (fread((char *)&ut, sizeof(ut), 1, uf) == 1) { 776 if (ut.ut_name[0] == '\0') 777 continue; 778 strncpy(line, ut.ut_line, sizeof(ut.ut_line)); 779 line[sizeof(ut.ut_line)] = '\0'; 780 if (f->f_type == F_WALL) { 781 if ((p = ttymsg(iov, 6, line, TTYMSGTIME)) != NULL) { 782 errno = 0; /* already in msg */ 783 logerror(p); 784 } 785 continue; 786 } 787 /* should we send the message to this user? */ 788 for (i = 0; i < MAXUNAMES; i++) { 789 if (!f->f_un.f_uname[i][0]) 790 break; 791 if (!strncmp(f->f_un.f_uname[i], ut.ut_name, 792 UT_NAMESIZE)) { 793 if ((p = ttymsg(iov, 6, line, TTYMSGTIME)) 794 != NULL) { 795 errno = 0; /* already in msg */ 796 logerror(p); 797 } 798 break; 799 } 800 } 801 } 802 (void)fclose(uf); 803 reenter = 0; 804} 805 806void 807reapchild(signo) 808 int signo; 809{ 810 int status; 811 812 while (wait3(&status, WNOHANG, (struct rusage *)NULL) > 0) 813 ; 814} 815 816/* 817 * Return a printable representation of a host address. 818 */ 819char * 820cvthname(f) 821 struct sockaddr_in *f; 822{ 823 struct hostent *hp; 824 char *p; 825 826 dprintf("cvthname(%s)\n", inet_ntoa(f->sin_addr)); 827 828 if (f->sin_family != AF_INET) { 829 dprintf("Malformed from address\n"); 830 return ("???"); 831 } 832 hp = gethostbyaddr((char *)&f->sin_addr, 833 sizeof(struct in_addr), f->sin_family); 834 if (hp == 0) { 835 dprintf("Host name for your address (%s) unknown\n", 836 inet_ntoa(f->sin_addr)); 837 return (inet_ntoa(f->sin_addr)); 838 } 839 if ((p = strchr(hp->h_name, '.')) && strcmp(p + 1, LocalDomain) == 0) 840 *p = '\0'; 841 return (hp->h_name); 842} 843 844void 845domark(signo) 846 int signo; 847{ 848 struct filed *f; 849 850 now = time((time_t *)NULL); 851 MarkSeq += TIMERINTVL; 852 if (MarkSeq >= MarkInterval) { 853 logmsg(LOG_INFO, "-- MARK --", LocalHostName, ADDDATE|MARK); 854 MarkSeq = 0; 855 } 856 857 for (f = Files; f; f = f->f_next) { 858 if (f->f_prevcount && now >= REPEATTIME(f)) { 859 dprintf("flush %s: repeated %d times, %d sec.\n", 860 TypeNames[f->f_type], f->f_prevcount, 861 repeatinterval[f->f_repeatcount]); 862 fprintlog(f, 0, (char *)NULL); 863 BACKOFF(f); 864 } 865 } 866 (void)alarm(TIMERINTVL); 867} 868 869/* 870 * Print syslogd errors some place. 871 */ 872void 873logerror(type) 874 const char *type; 875{ 876 char buf[100]; 877 878 if (errno) 879 (void)snprintf(buf, 880 sizeof(buf), "syslogd: %s: %s", type, strerror(errno)); 881 else 882 (void)snprintf(buf, sizeof(buf), "syslogd: %s", type); 883 errno = 0; 884 dprintf("%s\n", buf); 885 logmsg(LOG_SYSLOG|LOG_ERR, buf, LocalHostName, ADDDATE); 886} 887 888void 889die(signo) 890 int signo; 891{ 892 struct filed *f; 893 char buf[100]; 894 895 for (f = Files; f != NULL; f = f->f_next) { 896 /* flush any pending output */ 897 if (f->f_prevcount) 898 fprintlog(f, 0, (char *)NULL); 899 } 900 if (signo) { 901 dprintf("syslogd: exiting on signal %d\n", signo); 902 (void)sprintf(buf, "exiting on signal %d", signo); 903 errno = 0; 904 logerror(buf); 905 } 906 if (created_lsock) 907 (void)unlink(LogName); 908 exit(1); 909} 910 911/* 912 * INIT -- Initialize syslogd from configuration table 913 */ 914void 915init(signo) 916 int signo; 917{ 918 int i; 919 FILE *cf; 920 struct filed *f, *next, **nextp; 921 char *p; 922 char cline[LINE_MAX]; 923 char prog[NAME_MAX+1]; 924 925 dprintf("init\n"); 926 927 /* 928 * Close all open log files. 929 */ 930 Initialized = 0; 931 for (f = Files; f != NULL; f = next) { 932 /* flush any pending output */ 933 if (f->f_prevcount) 934 fprintlog(f, 0, (char *)NULL); 935 936 switch (f->f_type) { 937 case F_FILE: 938 case F_FORW: 939 (void)close(f->f_file); 940 break; 941 case F_CONSOLE: 942 case F_TTY: 943 break; 944 } 945 next = f->f_next; 946 if(f->f_program) free(f->f_program); 947 free((char *)f); 948 } 949 Files = NULL; 950 nextp = &Files; 951 952 /* open the configuration file */ 953 if ((cf = fopen(ConfFile, "r")) == NULL) { 954 dprintf("cannot open %s\n", ConfFile); 955 *nextp = (struct filed *)calloc(1, sizeof(*f)); 956 cfline("*.ERR\t/dev/console", *nextp, "*"); 957 (*nextp)->f_next = (struct filed *)calloc(1, sizeof(*f)); 958 cfline("*.PANIC\t*", (*nextp)->f_next, "*"); 959 Initialized = 1; 960 return; 961 } 962 963 /* 964 * Foreach line in the conf table, open that file. 965 */ 966 f = NULL; 967 strcpy(prog, "*"); 968 while (fgets(cline, sizeof(cline), cf) != NULL) { 969 /* 970 * check for end-of-section, comments, strip off trailing 971 * spaces and newline character. #!prog is treated specially: 972 * following lines apply only to that program. 973 */ 974 for (p = cline; isspace(*p); ++p) 975 continue; 976 if (*p == 0) 977 continue; 978 if(*p == '#') { 979 p++; 980 if(*p!='!') 981 continue; 982 } 983 if(*p=='!') { 984 p++; 985 while(isspace(*p)) p++; 986 if(!*p) { 987 strcpy(prog, "*"); 988 continue; 989 } 990 for(i = 0; i < NAME_MAX; i++) { 991 if(!isalnum(p[i])) 992 break; 993 prog[i] = p[i]; 994 } 995 prog[i] = 0; 996 continue; 997 } 998 for (p = strchr(cline, '\0'); isspace(*--p);) 999 continue; 1000 *++p = '\0'; 1001 f = (struct filed *)calloc(1, sizeof(*f)); 1002 *nextp = f; 1003 nextp = &f->f_next; 1004 cfline(cline, f, prog); 1005 } 1006 1007 /* close the configuration file */ 1008 (void)fclose(cf); 1009 1010 Initialized = 1; 1011 1012 if (Debug) { 1013 for (f = Files; f; f = f->f_next) { 1014 for (i = 0; i <= LOG_NFACILITIES; i++) 1015 if (f->f_pmask[i] == INTERNAL_NOPRI) 1016 printf("X "); 1017 else 1018 printf("%d ", f->f_pmask[i]); 1019 printf("%s: ", TypeNames[f->f_type]); 1020 switch (f->f_type) { 1021 case F_FILE: 1022 printf("%s", f->f_un.f_fname); 1023 break; 1024 1025 case F_CONSOLE: 1026 case F_TTY: 1027 printf("%s%s", _PATH_DEV, f->f_un.f_fname); 1028 break; 1029 1030 case F_FORW: 1031 printf("%s", f->f_un.f_forw.f_hname); 1032 break; 1033 1034 case F_USERS: 1035 for (i = 0; i < MAXUNAMES && *f->f_un.f_uname[i]; i++) 1036 printf("%s, ", f->f_un.f_uname[i]); 1037 break; 1038 } 1039 if(f->f_program) { 1040 printf(" (%s)", f->f_program); 1041 } 1042 printf("\n"); 1043 } 1044 } 1045 1046 logmsg(LOG_SYSLOG|LOG_INFO, "syslogd: restart", LocalHostName, ADDDATE); 1047 dprintf("syslogd: restarted\n"); 1048} 1049 1050/* 1051 * Crack a configuration file line 1052 */ 1053void 1054cfline(line, f, prog) 1055 char *line; 1056 struct filed *f; 1057 char *prog; 1058{ 1059 struct hostent *hp; 1060 int i, pri; 1061 char *bp, *p, *q; 1062 char buf[MAXLINE], ebuf[100]; 1063 1064 dprintf("cfline(\"%s\", f, \"%s\")\n", line, prog); 1065 1066 errno = 0; /* keep strerror() stuff out of logerror messages */ 1067 1068 /* clear out file entry */ 1069 memset(f, 0, sizeof(*f)); 1070 for (i = 0; i <= LOG_NFACILITIES; i++) 1071 f->f_pmask[i] = INTERNAL_NOPRI; 1072 1073 /* save program name if any */ 1074 if(prog && *prog=='*') prog = NULL; 1075 if(prog) { 1076 f->f_program = calloc(1, strlen(prog)+1); 1077 if(f->f_program) { 1078 strcpy(f->f_program, prog); 1079 } 1080 } 1081 1082 /* scan through the list of selectors */ 1083 for (p = line; *p && *p != '\t';) { 1084 1085 /* find the end of this facility name list */ 1086 for (q = p; *q && *q != '\t' && *q++ != '.'; ) 1087 continue; 1088 1089 /* collect priority name */ 1090 for (bp = buf; *q && !strchr("\t,;", *q); ) 1091 *bp++ = *q++; 1092 *bp = '\0'; 1093 1094 /* skip cruft */ 1095 while (strchr(", ;", *q)) 1096 q++; 1097 1098 /* decode priority name */ 1099 if (*buf == '*') 1100 pri = LOG_PRIMASK + 1; 1101 else { 1102 pri = decode(buf, prioritynames); 1103 if (pri < 0) { 1104 (void)sprintf(ebuf, 1105 "unknown priority name \"%s\"", buf); 1106 logerror(ebuf); 1107 return; 1108 } 1109 } 1110 1111 /* scan facilities */ 1112 while (*p && !strchr("\t.;", *p)) { 1113 for (bp = buf; *p && !strchr("\t,;.", *p); ) 1114 *bp++ = *p++; 1115 *bp = '\0'; 1116 if (*buf == '*') 1117 for (i = 0; i < LOG_NFACILITIES; i++) 1118 f->f_pmask[i] = pri; 1119 else { 1120 i = decode(buf, facilitynames); 1121 if (i < 0) { 1122 (void)sprintf(ebuf, 1123 "unknown facility name \"%s\"", 1124 buf); 1125 logerror(ebuf); 1126 return; 1127 } 1128 f->f_pmask[i >> 3] = pri; 1129 } 1130 while (*p == ',' || *p == ' ') 1131 p++; 1132 } 1133 1134 p = q; 1135 } 1136 1137 /* skip to action part */ 1138 while (*p == '\t') 1139 p++; 1140 1141 switch (*p) 1142 { 1143 case '@': 1144 if (!InetInuse) 1145 break; 1146 (void)strcpy(f->f_un.f_forw.f_hname, ++p); 1147 hp = gethostbyname(p); 1148 if (hp == NULL) { 1149 extern int h_errno; 1150 1151 logerror(hstrerror(h_errno)); 1152 break; 1153 } 1154 memset(&f->f_un.f_forw.f_addr, 0, 1155 sizeof(f->f_un.f_forw.f_addr)); 1156 f->f_un.f_forw.f_addr.sin_family = AF_INET; 1157 f->f_un.f_forw.f_addr.sin_port = LogPort; 1158 memmove(&f->f_un.f_forw.f_addr.sin_addr, hp->h_addr, hp->h_length); 1159 f->f_type = F_FORW; 1160 break; 1161 1162 case '/': 1163 if ((f->f_file = open(p, O_WRONLY|O_APPEND, 0)) < 0) { 1164 f->f_file = F_UNUSED; 1165 logerror(p); 1166 break; 1167 } 1168 if (isatty(f->f_file)) { 1169 if (strcmp(p, ctty) == 0) 1170 f->f_type = F_CONSOLE; 1171 else 1172 f->f_type = F_TTY; 1173 close(f->f_file); 1174 (void)strcpy(f->f_un.f_fname, p + sizeof _PATH_DEV - 1); 1175 } else { 1176 (void)strcpy(f->f_un.f_fname, p); 1177 f->f_type = F_FILE; 1178 } 1179 break; 1180 1181 case '*': 1182 f->f_type = F_WALL; 1183 break; 1184 1185 default: 1186 for (i = 0; i < MAXUNAMES && *p; i++) { 1187 for (q = p; *q && *q != ','; ) 1188 q++; 1189 (void)strncpy(f->f_un.f_uname[i], p, UT_NAMESIZE); 1190 if ((q - p) > UT_NAMESIZE) 1191 f->f_un.f_uname[i][UT_NAMESIZE] = '\0'; 1192 else 1193 f->f_un.f_uname[i][q - p] = '\0'; 1194 while (*q == ',' || *q == ' ') 1195 q++; 1196 p = q; 1197 } 1198 f->f_type = F_USERS; 1199 break; 1200 } 1201} 1202 1203 1204/* 1205 * Decode a symbolic name to a numeric value 1206 */ 1207int 1208decode(name, codetab) 1209 const char *name; 1210 CODE *codetab; 1211{ 1212 CODE *c; 1213 char *p, buf[40]; 1214 1215 if (isdigit(*name)) 1216 return (atoi(name)); 1217 1218 for (p = buf; *name && p < &buf[sizeof(buf) - 1]; p++, name++) { 1219 if (isupper(*name)) 1220 *p = tolower(*name); 1221 else 1222 *p = *name; 1223 } 1224 *p = '\0'; 1225 for (c = codetab; c->c_name; c++) 1226 if (!strcmp(buf, c->c_name)) 1227 return (c->c_val); 1228 1229 return (-1); 1230} 1231 1232/* 1233 * fork off and become a daemon, but wait for the child to come online 1234 * before returing to the parent, or we get disk thrashing at boot etc. 1235 * Set a timer so we don't hang forever if it wedges. 1236 */ 1237int 1238waitdaemon(nochdir, noclose, maxwait) 1239 int nochdir, noclose, maxwait; 1240{ 1241 int fd; 1242 int status; 1243 pid_t pid, childpid; 1244 1245 switch (childpid = fork()) { 1246 case -1: 1247 return (-1); 1248 case 0: 1249 break; 1250 default: 1251 signal(SIGALRM, timedout); 1252 alarm(maxwait); 1253 while ((pid = wait3(&status, 0, NULL)) != -1) { 1254 if (WIFEXITED(status)) 1255 errx(1, "child pid %d exited with return code %d", 1256 pid, WEXITSTATUS(status)); 1257 if (WIFSIGNALED(status)) 1258 errx(1, "child pid %d exited on signal %d%s", 1259 pid, WTERMSIG(status), 1260 WCOREDUMP(status) ? " (core dumped)" : 1261 ""); 1262 if (pid == childpid) /* it's gone... */ 1263 break; 1264 } 1265 exit(0); 1266 } 1267 1268 if (setsid() == -1) 1269 return (-1); 1270 1271 if (!nochdir) 1272 (void)chdir("/"); 1273 1274 if (!noclose && (fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) { 1275 (void)dup2(fd, STDIN_FILENO); 1276 (void)dup2(fd, STDOUT_FILENO); 1277 (void)dup2(fd, STDERR_FILENO); 1278 if (fd > 2) 1279 (void)close (fd); 1280 } 1281 return (getppid()); 1282} 1283 1284/* 1285 * We get a SIGALRM from the child when it's running and finished doing it's 1286 * fsync()'s or O_SYNC writes for all the boot messages. 1287 * 1288 * We also get a signal from the kernel if the timer expires, so check to 1289 * see what happened. 1290 */ 1291void 1292timedout(sig) 1293 int sig __unused; 1294{ 1295 int left; 1296 left = alarm(0); 1297 signal(SIGALRM, SIG_DFL); 1298 if (left == 0) 1299 errx(1, "timed out waiting for child"); 1300 else 1301 exit(0); 1302} 1303