Print this page
4211 Some syslog facility names and symbols are missing
1762 Syslogd man page: missing reference.
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/cmd/syslogd/syslogd.c
+++ new/usr/src/cmd/syslogd/syslogd.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
↓ open down ↓ |
14 lines elided |
↑ open up ↑ |
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21 /*
22 22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23 23 * Use is subject to license terms.
24 24 * Copyright 2012 Milan Jurik. All rights reserved.
25 + * Copyright (c) 2013 Gary Mills
25 26 */
26 27
27 28 /*
28 29 * Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T
29 30 * All Rights Reserved
30 31 */
31 32
32 33 /*
33 34 * University Copyright- Copyright (c) 1982, 1986, 1988
34 35 * The Regents of the University of California
35 36 * All Rights Reserved
36 37 *
37 38 * University Acknowledgment- Portions of this document are derived from
38 39 * software developed by the University of California, Berkeley, and its
39 40 * contributors.
40 41 */
41 42
42 43 /*
43 44 * syslogd -- log system messages
44 45 *
45 46 * This program implements a system log. It takes a series of lines.
46 47 * Each line may have a priority, signified as "<n>" as
47 48 * the first characters of the line. If this is
48 49 * not present, a default priority is used.
49 50 *
50 51 * To kill syslogd, send a signal 15 (terminate). A signal 1 (hup) will
51 52 * cause it to reconfigure.
52 53 *
53 54 * Defined Constants:
54 55 *
55 56 * MAXLINE -- the maximimum line length that can be handled.
56 57 * DEFUPRI -- the default priority for user messages.
57 58 * DEFSPRI -- the default priority for kernel messages.
58 59 *
59 60 */
60 61
61 62 #include <unistd.h>
62 63 #include <note.h>
63 64 #include <errno.h>
64 65 #include <sys/types.h>
65 66 #include <stdio.h>
66 67 #include <stdio_ext.h>
67 68 #include <stdlib.h>
68 69 #include <ctype.h>
69 70 #include <signal.h>
70 71 #include <string.h>
71 72 #include <strings.h>
72 73 #include <libscf.h>
73 74 #include <netconfig.h>
74 75 #include <netdir.h>
75 76 #include <pwd.h>
76 77 #include <sys/socket.h>
77 78 #include <tiuser.h>
78 79 #include <utmpx.h>
79 80 #include <limits.h>
80 81 #include <pthread.h>
81 82 #include <fcntl.h>
82 83 #include <stropts.h>
83 84 #include <assert.h>
84 85 #include <sys/statvfs.h>
85 86
86 87 #include <sys/param.h>
87 88 #include <sys/sysmacros.h>
88 89 #include <sys/syslog.h>
89 90 #include <sys/strlog.h>
90 91 #include <sys/stat.h>
91 92 #include <sys/time.h>
92 93 #include <sys/utsname.h>
93 94 #include <sys/poll.h>
94 95 #include <sys/wait.h>
95 96 #include <sys/resource.h>
96 97 #include <sys/mman.h>
97 98 #include <sys/note.h>
98 99 #include <door.h>
99 100
100 101 #include <wchar.h>
101 102 #include <locale.h>
102 103 #include <stdarg.h>
103 104
104 105 #include "dataq.h"
105 106 #include "conf.h"
106 107 #include "syslogd.h"
107 108
108 109 #define DOORFILE "/var/run/syslog_door"
109 110 #define RELATIVE_DOORFILE "../var/run/syslog_door"
110 111 #define OLD_DOORFILE "/etc/.syslog_door"
111 112
112 113 #define PIDFILE "/var/run/syslog.pid"
113 114 #define RELATIVE_PIDFILE "../var/run/syslog.pid"
114 115 #define OLD_PIDFILE "/etc/syslog.pid"
115 116
116 117 static char *LogName = "/dev/log";
117 118 static char *ConfFile = "/etc/syslog.conf";
118 119 static char ctty[] = "/dev/console";
119 120 static char sysmsg[] = "/dev/sysmsg";
120 121 static int DoorFd = -1;
121 122 static int DoorCreated = 0;
122 123 static int PidfileCreated = 0;
123 124 static char *DoorFileName = DOORFILE;
124 125 static char *PidFileName = PIDFILE;
125 126
126 127 /*
127 128 * configuration file directives
128 129 */
129 130
130 131 static struct code PriNames[] = {
131 132 "panic", LOG_EMERG,
132 133 "emerg", LOG_EMERG,
133 134 "alert", LOG_ALERT,
134 135 "crit", LOG_CRIT,
135 136 "err", LOG_ERR,
136 137 "error", LOG_ERR,
137 138 "warn", LOG_WARNING,
138 139 "warning", LOG_WARNING,
139 140 "notice", LOG_NOTICE,
140 141 "info", LOG_INFO,
141 142 "debug", LOG_DEBUG,
142 143 "none", NOPRI,
143 144 NULL, -1
144 145 };
145 146
146 147 static struct code FacNames[] = {
147 148 "kern", LOG_KERN,
↓ open down ↓ |
113 lines elided |
↑ open up ↑ |
148 149 "user", LOG_USER,
149 150 "mail", LOG_MAIL,
150 151 "daemon", LOG_DAEMON,
151 152 "auth", LOG_AUTH,
152 153 "security", LOG_AUTH,
153 154 "mark", LOG_MARK,
154 155 "syslog", LOG_SYSLOG,
155 156 "lpr", LOG_LPR,
156 157 "news", LOG_NEWS,
157 158 "uucp", LOG_UUCP,
159 + "altcron", LOG_ALTCRON,
160 + "authpriv", LOG_AUTHPRIV,
161 + "ftp", LOG_FTP,
162 + "ntp", LOG_NTP,
158 163 "audit", LOG_AUDIT,
164 + "console", LOG_CONSOLE,
159 165 "cron", LOG_CRON,
160 166 "local0", LOG_LOCAL0,
161 167 "local1", LOG_LOCAL1,
162 168 "local2", LOG_LOCAL2,
163 169 "local3", LOG_LOCAL3,
164 170 "local4", LOG_LOCAL4,
165 171 "local5", LOG_LOCAL5,
166 172 "local6", LOG_LOCAL6,
167 173 "local7", LOG_LOCAL7,
168 174 NULL, -1
169 175 };
170 176
171 177 static char *TypeNames[7] = {
172 178 "UNUSED", "FILE", "TTY", "CONSOLE",
173 179 "FORW", "USERS", "WALL"
174 180 };
175 181
176 182 /*
177 183 * we allocate our own thread stacks so we can create them
178 184 * without the MAP_NORESERVE option. We need to be sure
179 185 * we have stack space even if the machine runs out of swap
180 186 */
181 187
182 188 #define DEFAULT_STACKSIZE (100 * 1024) /* 100 k stack */
183 189 #define DEFAULT_REDZONESIZE (8 * 1024) /* 8k redzone */
184 190
185 191 static pthread_mutex_t wmp = PTHREAD_MUTEX_INITIALIZER; /* wallmsg lock */
186 192
187 193 static pthread_mutex_t cft = PTHREAD_MUTEX_INITIALIZER;
188 194 static int conf_threads = 0;
189 195
190 196 static pthread_mutex_t hup_lock = PTHREAD_MUTEX_INITIALIZER;
191 197 static pthread_cond_t hup_done = PTHREAD_COND_INITIALIZER;
192 198
193 199 static pthread_mutex_t logerror_lock = PTHREAD_MUTEX_INITIALIZER;
194 200
195 201 #define HUP_ACCEPTABLE 0x0000 /* can start SIGHUP process */
196 202 #define HUP_INPROGRESS 0x0001 /* SIGHUP process in progress */
197 203 #define HUP_COMPLETED 0x0002 /* SIGHUP process completed */
198 204 #define HUP_SUSP_LOGMSG_REQD 0x1000 /* request to suspend */
199 205 #define HUP_LOGMSG_SUSPENDED 0x2000 /* logmsg is suspended */
200 206 static int hup_state = HUP_ACCEPTABLE;
201 207
202 208 static size_t stacksize; /* thread stack size */
203 209 static size_t redzonesize; /* thread stack redzone size */
204 210 static char *stack_ptr; /* ptr to allocated stacks */
205 211 static char *cstack_ptr; /* ptr to conf_thr stacks */
206 212
207 213 static time_t start_time;
208 214
209 215 static pthread_t sys_thread; /* queues messages from us */
210 216 static pthread_t net_thread; /* queues messages from the net */
211 217 static pthread_t log_thread; /* message processing thread */
212 218 static pthread_t hnl_thread; /* hostname lookup thread */
213 219
214 220 static dataq_t inputq; /* the input queue */
215 221 static dataq_t tmpq; /* temporary queue for err msg */
216 222 static dataq_t hnlq; /* hostname lookup queue */
217 223
218 224 static struct filed fallback[2];
219 225 static struct filed *Files;
220 226 static int nlogs;
221 227 static int Debug; /* debug flag */
222 228 static host_list_t LocalHostName; /* our hostname */
223 229 static host_list_t NullHostName; /* in case of lookup failure */
224 230 static int debuglev = 1; /* debug print level */
225 231 static int interrorlog; /* internal error logging */
226 232
227 233 static int MarkInterval = 20; /* interval between marks (mins) */
228 234 static int Marking = 0; /* non-zero if marking some file */
229 235 static int Ninputs = 0; /* number of network inputs */
230 236 static int curalarm = 0; /* current timeout value (secs) */
231 237 static int sys_msg_count = 0; /* total msgs rcvd from local log */
232 238 static int sys_init_msg_count = 0; /* initially received */
233 239 static int net_msg_count = 0; /* total msgs rcvd from net */
234 240
235 241 static struct pollfd Pfd; /* Pollfd for local the log device */
236 242 static struct pollfd *Nfd; /* Array of pollfds for udp ports */
237 243 static struct netconfig *Ncf;
238 244 static struct netbuf **Myaddrs;
239 245 static struct t_unitdata **Udp;
240 246 static struct t_uderr **Errp;
241 247 static int turnoff = 0;
242 248 static int shutting_down;
243 249
244 250 /* for managing door server threads */
245 251 static pthread_mutex_t door_server_cnt_lock = PTHREAD_MUTEX_INITIALIZER;
246 252 static uint_t door_server_cnt = 0;
247 253 static pthread_attr_t door_thr_attr;
248 254
249 255 static struct hostname_cache **hnc_cache;
250 256 static pthread_mutex_t hnc_mutex = PTHREAD_MUTEX_INITIALIZER;
251 257 static size_t hnc_size = DEF_HNC_SIZE;
252 258 static unsigned int hnc_ttl = DEF_HNC_TTL;
253 259
254 260 #define DPRINT0(d, m) if ((Debug) && debuglev >= (d)) \
255 261 (void) fprintf(stderr, m)
256 262 #define DPRINT1(d, m, a) if ((Debug) && debuglev >= (d)) \
257 263 (void) fprintf(stderr, m, a)
258 264 #define DPRINT2(d, m, a, b) if ((Debug) && debuglev >= (d)) \
259 265 (void) fprintf(stderr, m, a, b)
260 266 #define DPRINT3(d, m, a, b, c) if ((Debug) && debuglev >= (d)) \
261 267 (void) fprintf(stderr, m, a, b, c)
262 268 #define DPRINT4(d, m, a, b, c, e) if ((Debug) && debuglev >= (d)) \
263 269 (void) fprintf(stderr, m, a, b, c, e)
264 270 #define MALLOC_FAIL(x) \
265 271 logerror("malloc failed: " x)
266 272 #define MALLOC_FAIL_EXIT \
267 273 logerror("malloc failed - fatal"); \
268 274 exit(1)
269 275
270 276
271 277 #define MAILCMD "mailx -s \"syslogd shut down\" root"
272 278
273 279 /*
274 280 * Number of seconds to wait before giving up on threads that won't
275 281 * shutdown: (that's right, 10 minutes!)
276 282 */
277 283 #define LOOP_MAX (10 * 60)
278 284
279 285 /*
280 286 * Interval(sec) to check the status of output queue while processing
281 287 * HUP signal.
282 288 */
283 289 #define LOOP_INTERVAL (15)
284 290
285 291 int
286 292 main(int argc, char **argv)
287 293 {
288 294 int i;
289 295 char *pstr;
290 296 int sig, fd;
291 297 int tflag = 0, Tflag = 0;
292 298 sigset_t sigs, allsigs;
293 299 struct rlimit rlim;
294 300 char *debugstr;
295 301 int mcount = 0;
296 302 struct sigaction act;
297 303 pthread_t mythreadno = 0;
298 304 char cbuf [30];
299 305 struct stat sb;
300 306
301 307 #ifdef DEBUG
302 308 #define DEBUGDIR "/var/tmp"
303 309 if (chdir(DEBUGDIR))
304 310 DPRINT2(1, "main(%u): Unable to cd to %s\n", mythreadno,
305 311 DEBUGDIR);
306 312 #endif /* DEBUG */
307 313
308 314 (void) setlocale(LC_ALL, "");
309 315
310 316 if ((debugstr = getenv("SYSLOGD_DEBUG")) != NULL)
311 317 if ((debuglev = atoi(debugstr)) == 0)
312 318 debuglev = 1;
313 319
314 320 #if ! defined(TEXT_DOMAIN) /* should be defined by cc -D */
315 321 #define TEXT_DOMAIN "SYS_TEST"
316 322 #endif
317 323 (void) textdomain(TEXT_DOMAIN);
318 324
319 325 (void) time(&start_time);
320 326
321 327 if (lstat("/var/run", &sb) != 0 || !(S_ISDIR(sb.st_mode))) {
322 328 DoorFileName = OLD_DOORFILE;
323 329 PidFileName = OLD_PIDFILE;
324 330 }
325 331
326 332 properties();
327 333
328 334 while ((i = getopt(argc, argv, "df:p:m:tT")) != EOF) {
329 335 switch (i) {
330 336 case 'f': /* configuration file */
331 337 ConfFile = optarg;
332 338 break;
333 339
334 340 case 'd': /* debug */
335 341 Debug++;
336 342 break;
337 343
338 344 case 'p': /* path */
339 345 LogName = optarg;
340 346 break;
341 347
342 348 case 'm': /* mark interval */
343 349 for (pstr = optarg; *pstr; pstr++) {
344 350 if (! (isdigit(*pstr))) {
345 351 (void) fprintf(stderr,
346 352 "Illegal interval\n");
347 353 usage();
348 354 }
349 355 }
350 356 MarkInterval = atoi(optarg);
351 357 if (MarkInterval < 1 || MarkInterval > INT_MAX) {
352 358 (void) fprintf(stderr,
353 359 "Interval must be between 1 and %d\n",
354 360 INT_MAX);
355 361 usage();
356 362 }
357 363 break;
358 364 case 't': /* turn off remote reception */
359 365 tflag++;
360 366 turnoff++;
361 367 break;
362 368 case 'T': /* turn on remote reception */
363 369 Tflag++;
364 370 turnoff = 0;
365 371 break;
366 372 default:
367 373 usage();
368 374 }
369 375 }
370 376
371 377 if (optind < argc)
372 378 usage();
373 379
374 380 if (tflag && Tflag) {
375 381 (void) fprintf(stderr, "specify only one of -t and -T\n");
376 382 usage();
377 383 }
378 384
379 385 /*
380 386 * close all fd's except 0-2
381 387 */
382 388
383 389 closefrom(3);
384 390
385 391 if (!Debug) {
386 392 if (fork())
387 393 return (0);
388 394 (void) close(0);
389 395 (void) open("/", 0);
390 396 (void) dup2(0, 1);
391 397 (void) dup2(0, 2);
392 398 untty();
393 399 }
394 400
395 401 if (Debug) {
396 402 mythreadno = pthread_self();
397 403 }
398 404
399 405 /*
400 406 * DO NOT call logerror() until tmpq is initialized.
401 407 */
402 408 disable_errorlog();
403 409
404 410 /*
405 411 * ensure that file descriptor limit is "high enough"
406 412 */
407 413 (void) getrlimit(RLIMIT_NOFILE, &rlim);
408 414 if (rlim.rlim_cur < rlim.rlim_max)
409 415 rlim.rlim_cur = rlim.rlim_max;
410 416 if (setrlimit(RLIMIT_NOFILE, &rlim) < 0)
411 417 logerror("Unable to increase file descriptor limit.");
412 418 (void) enable_extended_FILE_stdio(-1, -1);
413 419
414 420 /* block all signals from all threads initially */
415 421 (void) sigfillset(&allsigs);
416 422 (void) pthread_sigmask(SIG_BLOCK, &allsigs, NULL);
417 423
418 424 DPRINT2(1, "main(%u): Started at time %s", mythreadno,
419 425 ctime_r(&start_time, cbuf));
420 426
421 427 init(); /* read configuration, start threads */
422 428
423 429 DPRINT1(1, "main(%u): off & running....\n", mythreadno);
424 430
425 431 /* now set up to catch signals we care about */
426 432
427 433 (void) sigemptyset(&sigs);
428 434 (void) sigaddset(&sigs, SIGHUP); /* reconfigure */
429 435 (void) sigaddset(&sigs, SIGALRM); /* mark & flush timer */
430 436 (void) sigaddset(&sigs, SIGTERM); /* exit */
431 437 (void) sigaddset(&sigs, SIGINT); /* exit if debugging */
432 438 (void) sigaddset(&sigs, SIGQUIT); /* exit if debugging */
433 439 (void) sigaddset(&sigs, SIGPIPE); /* catch & discard */
434 440 (void) sigaddset(&sigs, SIGUSR1); /* dump debug stats */
435 441
436 442 /*
437 443 * We must set up to catch these signals, even though sigwait
438 444 * will get them before the isr does. Setting SA_SIGINFO ensures
439 445 * that signals will be enqueued.
440 446 */
441 447
442 448 act.sa_flags = SA_SIGINFO;
443 449 act.sa_sigaction = signull;
444 450
445 451 (void) sigaction(SIGHUP, &act, NULL);
446 452 (void) sigaction(SIGALRM, &act, NULL);
447 453 (void) sigaction(SIGTERM, &act, NULL);
448 454 (void) sigaction(SIGINT, &act, NULL);
449 455 (void) sigaction(SIGQUIT, &act, NULL);
450 456 (void) sigaction(SIGPIPE, &act, NULL);
451 457 (void) sigaction(SIGUSR1, &act, NULL);
452 458
453 459 /* we now turn into the signal handling thread */
454 460
455 461 DPRINT1(2, "main(%u): now handling signals\n", mythreadno);
456 462 for (;;) {
457 463 (void) sigwait(&sigs, &sig);
458 464 DPRINT2(2, "main(%u): received signal %d\n", mythreadno, sig);
459 465 switch (sig) {
460 466 case SIGALRM:
461 467 DPRINT1(1, "main(%u): Got SIGALRM\n",
462 468 mythreadno);
463 469 flushmsg(NOCOPY);
464 470 if (Marking && (++mcount % MARKCOUNT == 0)) {
465 471 if (logmymsg(LOG_INFO, "-- MARK --",
466 472 ADDDATE|MARK|NOCOPY, 0) == -1) {
467 473 MALLOC_FAIL(
468 474 "dropping MARK message");
469 475 }
470 476
471 477 mcount = 0;
472 478 }
473 479 curalarm = MarkInterval * 60 / MARKCOUNT;
474 480 (void) alarm((unsigned)curalarm);
475 481 DPRINT2(2, "main(%u): Next alarm in %d "
476 482 "seconds\n", mythreadno, curalarm);
477 483 break;
478 484 case SIGHUP:
479 485 DPRINT1(1, "main(%u): got SIGHUP - "
480 486 "reconfiguring\n", mythreadno);
481 487
482 488 reconfigure();
483 489
484 490 DPRINT1(1, "main(%u): done processing SIGHUP\n",
485 491 mythreadno);
486 492 break;
487 493 case SIGQUIT:
488 494 case SIGINT:
489 495 if (!Debug) {
490 496 /* allow these signals if debugging */
491 497 break;
492 498 }
493 499 /* FALLTHROUGH */
494 500 case SIGTERM:
495 501 DPRINT2(1, "main(%u): going down on signal %d\n",
496 502 mythreadno, sig);
497 503 (void) alarm(0);
498 504 flushmsg(0);
499 505 errno = 0;
500 506 t_errno = 0;
501 507 logerror("going down on signal %d", sig);
502 508 disable_errorlog(); /* force msg to console */
503 509 (void) shutdown_msg(); /* stop threads */
504 510 shutdown_input();
505 511 close_door();
506 512 delete_doorfiles();
507 513 return (0);
508 514 case SIGUSR1: /* secret debug dump mode */
509 515 /* if in debug mode, use stdout */
510 516
511 517 if (Debug) {
512 518 dumpstats(STDOUT_FILENO);
513 519 break;
514 520 }
515 521 /* otherwise dump to a debug file */
516 522 if ((fd = open(DEBUGFILE,
517 523 (O_WRONLY|O_CREAT|O_TRUNC|O_EXCL),
518 524 0644)) < 0)
519 525 break;
520 526 dumpstats(fd);
521 527 (void) close(fd);
522 528 break;
523 529 default:
524 530 DPRINT2(2, "main(%u): unexpected signal %d\n",
525 531 mythreadno, sig);
526 532 break;
527 533 }
528 534 }
529 535 }
530 536
531 537 /*
532 538 * Attempts to open the local log device
533 539 * and return a file descriptor.
534 540 */
535 541 static int
536 542 openklog(char *name, int mode)
537 543 {
538 544 int fd;
539 545 struct strioctl str;
540 546 pthread_t mythreadno;
541 547
542 548 if (Debug) {
543 549 mythreadno = pthread_self();
544 550 }
545 551
546 552 if ((fd = open(name, mode)) < 0) {
547 553 logerror("cannot open %s", name);
548 554 DPRINT3(1, "openklog(%u): cannot create %s (%d)\n",
549 555 mythreadno, name, errno);
550 556 return (-1);
551 557 }
552 558 str.ic_cmd = I_CONSLOG;
553 559 str.ic_timout = 0;
554 560 str.ic_len = 0;
555 561 str.ic_dp = NULL;
556 562 if (ioctl(fd, I_STR, &str) < 0) {
557 563 logerror("cannot register to log console messages");
558 564 DPRINT2(1, "openklog(%u): cannot register to log "
559 565 "console messages (%d)\n", mythreadno, errno);
560 566 return (-1);
561 567 }
562 568 return (fd);
563 569 }
564 570
565 571
566 572 /*
567 573 * Open the log device, and pull up all pending messages.
568 574 */
569 575 static void
570 576 prepare_sys_poll()
571 577 {
572 578 int nfds, funix;
573 579
574 580 if ((funix = openklog(LogName, O_RDONLY)) < 0) {
575 581 logerror("can't open kernel log device - fatal");
576 582 exit(1);
577 583 }
578 584
579 585 Pfd.fd = funix;
580 586 Pfd.events = POLLIN;
581 587
582 588 for (;;) {
583 589 nfds = poll(&Pfd, 1, 0);
584 590 if (nfds <= 0) {
585 591 if (sys_init_msg_count > 0)
586 592 flushmsg(SYNC_FILE);
587 593 break;
588 594 }
589 595
590 596 if (Pfd.revents & POLLIN) {
591 597 getkmsg(0);
592 598 } else if (Pfd.revents & (POLLNVAL|POLLHUP|POLLERR)) {
593 599 logerror("kernel log driver poll error");
594 600 break;
595 601 }
596 602 }
597 603
598 604 }
599 605
600 606 /*
601 607 * this thread listens to the local stream log driver for log messages
602 608 * generated by this host, formats them, and queues them to the logger
603 609 * thread.
604 610 */
605 611 /*ARGSUSED*/
606 612 static void *
607 613 sys_poll(void *ap)
608 614 {
609 615 int nfds;
↓ open down ↓ |
441 lines elided |
↑ open up ↑ |
610 616 static int klogerrs = 0;
611 617 pthread_t mythreadno;
612 618
613 619 if (Debug) {
614 620 mythreadno = pthread_self();
615 621 }
616 622
617 623 DPRINT1(1, "sys_poll(%u): sys_thread started\n", mythreadno);
618 624
619 625 /*
620 - * Try to process as many messages as we can without blocking on poll.
621 - * We count such "initial" messages with sys_init_msg_count and
622 - * enqueue them without the SYNC_FILE flag. When no more data is
623 - * waiting on the local log device, we set timeout to INFTIM,
624 - * clear sys_init_msg_count, and generate a flush message to sync
625 - * the previously counted initial messages out to disk.
626 + * Process messages, blocking on poll because timeout is set
627 + * to INFTIM. When poll returns with a message, call getkmsg
628 + * to pull up one message from the log driver and enqueue it
629 + * with the sync flag set.
626 630 */
627 631
628 632 sys_init_msg_count = 0;
629 633
630 634 for (;;) {
631 635 errno = 0;
632 636 t_errno = 0;
633 637
634 638 nfds = poll(&Pfd, 1, INFTIM);
635 639
636 640 if (nfds == 0)
637 641 continue;
638 642
639 643 if (nfds < 0) {
640 644 if (errno != EINTR)
641 645 logerror("poll");
642 646 continue;
643 647 }
644 648 if (Pfd.revents & POLLIN) {
645 649 getkmsg(INFTIM);
646 650 } else {
647 651 if (shutting_down) {
648 652 pthread_exit(0);
649 653 }
650 654 if (Pfd.revents & (POLLNVAL|POLLHUP|POLLERR)) {
651 655 logerror("kernel log driver poll error");
652 656 (void) close(Pfd.fd);
653 657 Pfd.fd = -1;
654 658 }
655 659 }
656 660
657 661 while (Pfd.fd == -1 && klogerrs++ < 10) {
658 662 Pfd.fd = openklog(LogName, O_RDONLY);
659 663 }
660 664 if (klogerrs >= 10) {
661 665 logerror("can't reopen kernel log device - fatal");
662 666 exit(1);
663 667 }
664 668 }
665 669 /*NOTREACHED*/
666 670 return (NULL);
667 671 }
668 672
669 673 /*
670 674 * Pull up one message from log driver.
671 675 */
672 676 static void
673 677 getkmsg(int timeout)
674 678 {
675 679 int flags = 0, i;
676 680 char *lastline;
677 681 struct strbuf ctl, dat;
678 682 struct log_ctl hdr;
679 683 char buf[MAXLINE+1];
680 684 size_t buflen;
681 685 size_t len;
682 686 char tmpbuf[MAXLINE+1];
683 687 pthread_t mythreadno;
684 688
685 689 if (Debug) {
686 690 mythreadno = pthread_self();
687 691 }
↓ open down ↓ |
52 lines elided |
↑ open up ↑ |
688 692
689 693 dat.maxlen = MAXLINE;
690 694 dat.buf = buf;
691 695 ctl.maxlen = sizeof (struct log_ctl);
692 696 ctl.buf = (caddr_t)&hdr;
693 697
694 698 while ((i = getmsg(Pfd.fd, &ctl, &dat, &flags)) == MOREDATA) {
695 699 lastline = &dat.buf[dat.len];
696 700 *lastline = '\0';
697 701
698 - DPRINT2(5, "sys_poll:(%u): getmsg: dat.len = %d\n",
702 + DPRINT2(5, "getkmsg:(%u): getmsg: dat.len = %d\n",
699 703 mythreadno, dat.len);
700 704 buflen = strlen(buf);
701 705 len = findnl_bkwd(buf, buflen);
702 706
703 707 (void) memcpy(tmpbuf, buf, len);
704 708 tmpbuf[len] = '\0';
705 709
706 710 /*
707 711 * Format sys will enqueue the log message.
708 712 * Set the sync flag if timeout != 0, which
709 713 * means that we're done handling all the
710 714 * initial messages ready during startup.
711 715 */
712 716 if (timeout == 0) {
713 717 formatsys(&hdr, tmpbuf, 0);
714 718 sys_init_msg_count++;
715 719 } else {
716 720 formatsys(&hdr, tmpbuf, 1);
717 721 }
718 722 sys_msg_count++;
719 723
720 724 if (len != buflen) {
721 725 /* If anything remains in buf */
722 726 size_t remlen;
723 727
724 728 if (buf[len] == '\n') {
725 729 /* skip newline */
726 730 len++;
727 731 }
728 732
729 733 /*
730 734 * Move the remaining bytes to
731 735 * the beginnning of buf.
732 736 */
733 737
734 738 remlen = buflen - len;
735 739 (void) memcpy(buf, &buf[len], remlen);
736 740 dat.maxlen = MAXLINE - remlen;
737 741 dat.buf = &buf[remlen];
738 742 } else {
739 743 dat.maxlen = MAXLINE;
740 744 dat.buf = buf;
741 745 }
742 746 }
743 747
744 748 if (i == 0 && dat.len > 0) {
745 749 dat.buf[dat.len] = '\0';
746 750 /*
747 751 * Format sys will enqueue the log message.
748 752 * Set the sync flag if timeout != 0, which
749 753 * means that we're done handling all the
750 754 * initial messages ready during startup.
751 755 */
752 756 DPRINT2(5, "getkmsg(%u): getmsg: dat.maxlen = %d\n",
753 757 mythreadno, dat.maxlen);
754 758 DPRINT2(5, "getkmsg(%u): getmsg: dat.len = %d\n",
755 759 mythreadno, dat.len);
756 760 DPRINT2(5, "getkmsg(%u): getmsg: strlen(dat.buf) = %d\n",
757 761 mythreadno, strlen(dat.buf));
758 762 DPRINT2(5, "getkmsg(%u): getmsg: dat.buf = \"%s\"\n",
759 763 mythreadno, dat.buf);
760 764 DPRINT2(5, "getkmsg(%u): buf len = %d\n",
761 765 mythreadno, strlen(buf));
762 766 if (timeout == 0) {
763 767 formatsys(&hdr, buf, 0);
764 768 sys_init_msg_count++;
765 769 } else {
766 770 formatsys(&hdr, buf, 1);
767 771 }
768 772 sys_msg_count++;
769 773 } else if (i < 0 && errno != EINTR) {
770 774 if (!shutting_down) {
771 775 logerror("kernel log driver read error");
772 776 }
773 777 (void) close(Pfd.fd);
774 778 Pfd.fd = -1;
775 779 }
776 780 }
777 781
778 782 /*
779 783 * this thread polls all the network interfaces for syslog messages
780 784 * forwarded to us, tags them with the hostname they are received
781 785 * from, and queues them to the logger thread.
782 786 */
783 787 /*ARGSUSED*/
784 788 static void *
785 789 net_poll(void *ap)
786 790 {
787 791 int nfds, i;
788 792 int flags = 0;
789 793 struct t_unitdata *udp;
790 794 struct t_uderr *errp;
791 795 char buf[MAXLINE+1];
792 796 char *uap;
793 797 log_message_t *mp;
794 798 host_info_t *hinfo;
795 799 pthread_t mythreadno;
796 800
797 801 if (Debug) {
798 802 mythreadno = pthread_self();
799 803 }
800 804
801 805 DPRINT1(1, "net_poll(%u): net_thread started\n", mythreadno);
802 806
803 807 _NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*mp))
804 808
805 809 for (;;) {
806 810 errno = 0;
807 811 t_errno = 0;
808 812 nfds = poll(Nfd, Ninputs, -1);
809 813 if (nfds == 0)
810 814 continue;
811 815
812 816 if (nfds < 0) {
813 817 if (errno != EINTR)
814 818 logerror("poll");
815 819 continue;
816 820 }
817 821 for (i = 0; nfds > 0 && i < Ninputs; i++) {
818 822 if ((Nfd[i].revents & POLLIN) == 0) {
819 823 if (shutting_down) {
820 824 pthread_exit(0);
821 825 }
822 826 if (Nfd[i].revents &
823 827 (POLLNVAL|POLLHUP|POLLERR)) {
824 828 logerror("POLLNVAL|POLLHUP|POLLERR");
825 829 (void) t_close(Nfd[i].fd);
826 830 Nfd[i].fd = -1;
827 831 nfds--;
828 832 }
829 833 continue;
830 834 }
831 835
832 836 udp = Udp[i];
833 837 udp->udata.buf = buf;
834 838 udp->udata.maxlen = MAXLINE;
835 839 udp->udata.len = 0;
836 840 flags = 0;
837 841 if (t_rcvudata(Nfd[i].fd, udp, &flags) < 0) {
838 842 errp = Errp[i];
839 843 if (t_errno == TLOOK) {
840 844 if (t_rcvuderr(Nfd[i].fd, errp) < 0) {
841 845 if (!shutting_down) {
842 846 logerror("t_rcvuderr");
843 847 }
844 848 (void) t_close(Nfd[i].fd);
845 849 Nfd[i].fd = -1;
846 850 }
847 851 } else {
848 852 if (!shutting_down) {
849 853 logerror("t_rcvudata");
850 854 }
851 855 (void) t_close(Nfd[i].fd);
852 856 Nfd[i].fd = -1;
853 857 }
854 858 nfds--;
855 859 if (shutting_down) {
856 860 pthread_exit(0);
857 861 }
858 862 continue;
859 863 }
860 864 nfds--;
861 865
862 866 if (udp->udata.len == 0) {
863 867 if (Debug) {
864 868 uap = NULL;
865 869 if (udp->addr.len > 0) {
866 870 uap = taddr2uaddr(&Ncf[i],
867 871 &udp->addr);
868 872 }
869 873 DPRINT2(1, "net_poll(%u):"
870 874 " received empty packet"
871 875 " from %s\n", mythreadno,
872 876 uap ? uap : "<unknown>");
873 877 if (uap)
874 878 free(uap);
875 879 }
876 880 continue; /* No data */
877 881 }
878 882 if (udp->addr.len == 0) {
879 883 /*
880 884 * The previous message was larger than
881 885 * MAXLINE, and T_MORE should have been set.
882 886 * Further data needs to be discarded as
883 887 * we've already received MAXLINE.
884 888 */
885 889 DPRINT1(1, "net_poll(%u): discarding packet "
886 890 "exceeds max line size\n", mythreadno);
887 891 continue;
888 892 }
889 893
890 894 net_msg_count++;
891 895
892 896 if ((mp = new_msg()) == NULL) {
893 897 MALLOC_FAIL("dropping message from "
894 898 "remote");
895 899 continue;
896 900 }
897 901
898 902 buf[udp->udata.len] = '\0';
899 903 formatnet(&udp->udata, mp);
900 904
901 905 if (Debug) {
902 906 uap = taddr2uaddr(&Ncf[i], &udp->addr);
903 907 DPRINT2(1, "net_poll(%u): received message"
904 908 " from %s\n", mythreadno,
905 909 uap ? uap : "<unknown>");
906 910 free(uap);
907 911 }
908 912 if ((hinfo = malloc(sizeof (*hinfo))) == NULL ||
909 913 (hinfo->addr.buf =
910 914 malloc(udp->addr.len)) == NULL) {
911 915 MALLOC_FAIL("dropping message from "
912 916 "remote");
913 917 if (hinfo) {
914 918 free(hinfo);
915 919 }
916 920 free_msg(mp);
917 921 continue;
918 922 }
919 923
920 924 hinfo->ncp = &Ncf[i];
921 925 hinfo->addr.len = udp->addr.len;
922 926 (void) memcpy(hinfo->addr.buf, udp->addr.buf,
923 927 udp->addr.len);
924 928 mp->ptr = hinfo;
925 929 if (dataq_enqueue(&hnlq, (void *)mp) == -1) {
926 930 MALLOC_FAIL("dropping message from "
927 931 "remote");
928 932 free_msg(mp);
929 933 free(hinfo->addr.buf);
930 934 free(hinfo);
931 935 continue;
932 936 }
933 937 DPRINT3(5, "net_poll(%u): enqueued msg %p "
934 938 "on queue %p\n", mythreadno, (void *)mp,
935 939 (void *)&hnlq);
936 940 }
937 941 }
938 942 /*NOTREACHED*/
939 943 return (NULL);
940 944 }
941 945
942 946 static void
943 947 usage(void)
944 948 {
945 949 (void) fprintf(stderr,
946 950 "usage: syslogd [-d] [-t|-T] [-mmarkinterval] [-ppath]"
947 951 " [-fconffile]\n");
948 952 exit(1);
949 953 }
950 954
951 955 static void
952 956 untty(void)
953 957 {
954 958 if (!Debug)
955 959 (void) setsid();
956 960 }
957 961
958 962 /*
959 963 * generate a log message internally. The original version of syslogd
960 964 * simply called logmsg directly, but because everything is now based
961 965 * on message passing, we need an internal way to generate and queue
962 966 * log messages from within syslogd itself.
963 967 */
964 968 static int
965 969 logmymsg(int pri, char *msg, int flags, int pending)
966 970 {
967 971 log_message_t *mp;
968 972 pthread_t mythreadno;
969 973 dataq_t *qptr;
970 974
971 975 if (Debug) {
972 976 mythreadno = pthread_self();
973 977 }
974 978
975 979 if ((mp = new_msg()) == NULL) {
976 980 return (-1);
977 981 }
978 982
979 983 _NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*mp))
980 984 mp->pri = pri;
981 985 mp->hlp = &LocalHostName;
982 986 (void) strlcpy(mp->msg, msg, MAXLINE+1);
983 987 mp->flags = flags;
984 988 (void) time(&mp->ts);
985 989
986 990 qptr = pending ? &tmpq : &inputq;
987 991 if (dataq_enqueue(qptr, (void *)mp) == -1) {
988 992 free_msg(mp);
989 993 return (-1);
990 994 }
991 995
992 996 DPRINT3(5, "logmymsg(%u): enqueued msg %p on queue %p\n",
993 997 mythreadno, (void *)mp, (void *)qptr);
994 998 DPRINT2(5, "logmymsg(%u): Message content: %s\n", mythreadno, msg);
995 999 return (0);
996 1000 }
997 1001
998 1002 /*
999 1003 * Generate an internal shutdown message
1000 1004 */
1001 1005 static int
1002 1006 shutdown_msg(void)
1003 1007 {
1004 1008 pthread_t mythreadno;
1005 1009 log_message_t *mp;
1006 1010
1007 1011 if (Debug) {
1008 1012 mythreadno = pthread_self();
1009 1013 }
1010 1014
1011 1015 if ((mp = new_msg()) == NULL) {
1012 1016 return (-1);
1013 1017 }
1014 1018
1015 1019 _NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*mp));
1016 1020 mp->flags = SHUTDOWN;
1017 1021 mp->hlp = &LocalHostName;
1018 1022
1019 1023 if (dataq_enqueue(&inputq, (void *)mp) == -1) {
1020 1024 free_msg(mp);
1021 1025 return (-1);
1022 1026 }
1023 1027
1024 1028 DPRINT3(5, "shutdown_msg(%u): enqueued msg %p on queue %p\n",
1025 1029 mythreadno, (void *)mp, (void *)&inputq);
1026 1030 return (0);
1027 1031 }
1028 1032
1029 1033 /*
1030 1034 * Generate an internal flush message
1031 1035 */
1032 1036 static void
1033 1037 flushmsg(int flags)
1034 1038 {
1035 1039 log_message_t *mp;
1036 1040 pthread_t mythreadno;
1037 1041
1038 1042 if (Debug) {
1039 1043 mythreadno = pthread_self();
1040 1044 }
1041 1045
1042 1046 if ((mp = new_msg()) == NULL) {
1043 1047 MALLOC_FAIL("dropping flush msg");
1044 1048 return;
1045 1049 }
1046 1050
1047 1051 _NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*mp));
1048 1052 mp->flags = FLUSHMSG | flags;
1049 1053 mp->hlp = &LocalHostName;
1050 1054
1051 1055 if (dataq_enqueue(&inputq, (void *)mp) == -1) {
1052 1056 free_msg(mp);
1053 1057 MALLOC_FAIL("dropping flush msg");
1054 1058 return;
1055 1059 }
1056 1060
1057 1061 DPRINT4(5, "flush_msg(%u): enqueued msg %p on queue %p, flags "
1058 1062 "0x%x\n", mythreadno, (void *)mp, (void *)&inputq, flags);
1059 1063 }
1060 1064
1061 1065 /*
1062 1066 * Do some processing on messages received from the net
1063 1067 */
1064 1068 static void
1065 1069 formatnet(struct netbuf *nbp, log_message_t *mp)
1066 1070 {
1067 1071 char *p;
1068 1072 int pri;
1069 1073 pthread_t mythreadno;
1070 1074
1071 1075 if (Debug) {
1072 1076 mythreadno = pthread_self();
1073 1077 }
1074 1078
1075 1079 DPRINT2(5, "formatnet(%u): called for msg %p\n", mythreadno,
1076 1080 (void *)mp);
1077 1081
1078 1082 mp->flags = NETWORK;
1079 1083 (void) time(&mp->ts);
1080 1084
1081 1085 /* test for special codes */
1082 1086 pri = DEFUPRI;
1083 1087 p = nbp->buf;
1084 1088 DPRINT2(9, "formatnet(%u): Message content:\n>%s<\n", mythreadno,
1085 1089 p);
1086 1090 if (*p == '<' && isdigit(*(p+1))) {
1087 1091 pri = 0;
1088 1092 while (isdigit(*++p))
1089 1093 pri = 10 * pri + (*p - '0');
1090 1094 if (*p == '>')
1091 1095 ++p;
1092 1096 if (pri <= 0 || pri >= (LOG_NFACILITIES << 3))
1093 1097 pri = DEFUPRI;
1094 1098 }
1095 1099
1096 1100 mp->pri = pri;
1097 1101 (void) strlcpy(mp->msg, p, MAXLINE+1);
1098 1102 }
1099 1103
1100 1104 /*
1101 1105 * Do some processing on messages generated by this host
1102 1106 * and then enqueue the log message.
1103 1107 */
1104 1108 static void
1105 1109 formatsys(struct log_ctl *lp, char *msg, int sync)
1106 1110 {
1107 1111 char *p, *q;
1108 1112 char line[MAXLINE + 1];
1109 1113 size_t msglen;
1110 1114 log_message_t *mp;
1111 1115 char cbuf[30];
1112 1116 pthread_t mythreadno;
1113 1117
1114 1118 if (Debug) {
1115 1119 mythreadno = pthread_self();
1116 1120 }
1117 1121
1118 1122 DPRINT3(3, "formatsys(%u): log_ctl.mid = %d, log_ctl.sid = %d\n",
1119 1123 mythreadno, lp->mid, lp->sid);
1120 1124 DPRINT2(9, "formatsys(%u): Message Content:\n>%s<\n", mythreadno,
1121 1125 msg);
1122 1126
1123 1127 /* msglen includes the null termination */
1124 1128 msglen = strlen(msg) + 1;
1125 1129
1126 1130 for (p = msg; *p != '\0'; ) {
1127 1131 size_t linelen;
1128 1132 size_t len;
1129 1133
1130 1134 /*
1131 1135 * Allocate a log_message_t structure.
1132 1136 * We should do it here since a single message (msg)
1133 1137 * could be composed of many lines.
1134 1138 */
1135 1139 _NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*mp));
1136 1140
1137 1141 if ((mp = new_msg()) == NULL) {
1138 1142 MALLOC_FAIL("dropping message");
1139 1143 /*
1140 1144 * Should bail out from the loop.
1141 1145 */
1142 1146 break;
1143 1147 }
1144 1148
1145 1149 mp->flags &= ~NETWORK;
1146 1150 mp->hlp = &LocalHostName;
1147 1151 mp->ts = lp->ttime;
1148 1152 if (lp->flags & SL_LOGONLY)
1149 1153 mp->flags |= IGN_CONS;
1150 1154 if (lp->flags & SL_CONSONLY)
1151 1155 mp->flags |= IGN_FILE;
1152 1156
1153 1157 /* extract facility */
1154 1158 if ((lp->pri & LOG_FACMASK) == LOG_KERN) {
1155 1159 (void) sprintf(line, "%.15s ",
1156 1160 ctime_r(&mp->ts, cbuf) + 4);
1157 1161 } else {
1158 1162 (void) sprintf(line, "");
1159 1163 }
1160 1164
1161 1165 linelen = strlen(line);
1162 1166 q = line + linelen;
1163 1167
1164 1168 DPRINT2(5, "formatsys(%u): msglen = %d\n", mythreadno, msglen);
1165 1169 len = copynl_frwd(q, MAXLINE + 1 - linelen, p, msglen);
1166 1170 DPRINT2(5, "formatsys(%u): len (copynl_frwd) = %d\n",
1167 1171 mythreadno, len);
1168 1172
1169 1173 p += len;
1170 1174 msglen -= len;
1171 1175
1172 1176 if (*p == '\n') {
1173 1177 /* skip newline */
1174 1178 p++;
1175 1179 }
1176 1180
1177 1181 if (sync && ((lp->pri & LOG_FACMASK) == LOG_KERN))
1178 1182 mp->flags |= SYNC_FILE; /* fsync file after write */
1179 1183
1180 1184 if (len != 0) {
1181 1185 (void) strlcpy(mp->msg, line, MAXLINE+1);
1182 1186 mp->pri = lp->pri;
1183 1187
1184 1188 if (dataq_enqueue(&inputq, (void *)mp) == -1) {
1185 1189 free_msg(mp);
1186 1190 MALLOC_FAIL("dropping message");
1187 1191 break;
1188 1192 }
1189 1193
1190 1194 DPRINT3(5, "formatsys(%u): sys_thread enqueued msg "
1191 1195 "%p on queue %p\n", mythreadno, (void *)mp,
1192 1196 (void *)&inputq);
1193 1197 } else
1194 1198 free_msg(mp);
1195 1199 }
1196 1200 }
1197 1201
1198 1202 /*
1199 1203 * Log a message to the appropriate log files, users, etc. based on
1200 1204 * the priority.
1201 1205 */
1202 1206 /*ARGSUSED*/
1203 1207 static void *
1204 1208 logmsg(void *ap)
1205 1209 {
1206 1210 struct filed *f;
1207 1211 int fac, prilev, flags, refcnt;
1208 1212 int fake_shutdown, skip_shutdown;
1209 1213 log_message_t *mp, *save_mp;
1210 1214 pthread_t mythreadno;
1211 1215
1212 1216 if (Debug) {
1213 1217 mythreadno = pthread_self();
1214 1218 }
1215 1219
1216 1220 DPRINT1(1, "logmsg(%u): msg dispatcher started\n", mythreadno);
1217 1221
1218 1222 fake_shutdown = skip_shutdown = 0;
1219 1223 save_mp = NULL;
1220 1224 for (;;) {
1221 1225 if (save_mp) {
1222 1226 /*
1223 1227 * If we have set aside a message in order to fake a
1224 1228 * SHUTDOWN, use that message before picking from the
1225 1229 * queue again.
1226 1230 */
1227 1231 mp = save_mp;
1228 1232 save_mp = NULL;
1229 1233 } else {
1230 1234 (void) dataq_dequeue(&inputq, (void **)&mp, 0);
1231 1235 }
1232 1236 _NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*mp))
1233 1237 DPRINT3(5, "logmsg(%u): msg dispatcher dequeued %p from "
1234 1238 "queue %p\n", mythreadno, (void *)mp,
1235 1239 (void *)&inputq);
1236 1240
1237 1241 /*
1238 1242 * In most cases, if the message traffic is low, logmsg() wakes
1239 1243 * up when it receives the SHUTDOWN msg, and will sleep until
1240 1244 * HUP process is complete. However, if the inputq is too
1241 1245 * long, logmsg() may not receive SHUTDOWN before reconfigure()
1242 1246 * releases the logger fds, filed and logit threads. That, in
1243 1247 * turn, will cause logmsg to refer to invalid fileds.
1244 1248 *
1245 1249 * logmsg() needs to respond to the SHUTDOWN message within
1246 1250 * LOOP_INTERVAL seconds when reconfigure() enqueues it. It
1247 1251 * does so in most cases. When it does not respond in time,
1248 1252 * logmsg() needs to be in suspended state immediately, since
1249 1253 * filed may have been invalidated. reconfigure() will set the
1250 1254 * HUP_SUSP_LOGMSG_REQD bit in hup_state and wait another
1251 1255 * LOOP_INTERVAL seconds before proceeding.
1252 1256 *
1253 1257 * When HUP_SUSP_LOGMSG_REQD is set, we will create a fake
1254 1258 * SHUTDOWN message, and dispatch it to the various logit
1255 1259 * threads, and logmsg() itself will suspend. In order to
1256 1260 * ignore the real SHUTDOWN which will arrive later, we keep a
1257 1261 * counter (skip_shutdown) and decrement it when the SHUTDOWN
1258 1262 * message arrives.
1259 1263 */
1260 1264 if ((hup_state & HUP_SUSP_LOGMSG_REQD) &&
1261 1265 (mp->flags & SHUTDOWN) == 0) {
1262 1266 DPRINT1(3, "logmsg(%u): suspend request\n",
1263 1267 mythreadno);
1264 1268
1265 1269 save_mp = mp;
1266 1270
1267 1271 /* create a fake SHUTDOWN msg */
1268 1272 if ((mp = new_msg()) == NULL) {
1269 1273 MALLOC_FAIL("dropping message");
1270 1274 if (mp->flags & SHUTDOWN) {
1271 1275 (void) logerror_to_console(1,
1272 1276 "unable to shutdown "
1273 1277 "logger thread");
1274 1278 }
1275 1279 continue;
1276 1280 }
1277 1281 mp->flags = SHUTDOWN;
1278 1282 mp->hlp = &LocalHostName;
1279 1283 fake_shutdown = 1;
1280 1284 skip_shutdown++;
1281 1285 DPRINT2(3, "logmsg(%u): pending SHUTDOWN %d\n",
1282 1286 mythreadno, skip_shutdown);
1283 1287 }
1284 1288
1285 1289 /*
1286 1290 * is it a shutdown or flush message ?
1287 1291 */
1288 1292 if ((mp->flags & SHUTDOWN) || (mp->flags & FLUSHMSG)) {
1289 1293 (void) pthread_mutex_lock(&mp->msg_mutex);
1290 1294
1291 1295 if ((mp->flags & SHUTDOWN) &&
1292 1296 !fake_shutdown && skip_shutdown > 0) {
1293 1297 skip_shutdown--;
1294 1298 (void) pthread_mutex_unlock(&mp->msg_mutex);
1295 1299 free_msg(mp);
1296 1300 DPRINT2(3, "logmsg(%u): released late "
1297 1301 "arrived SHUTDOWN. pending %d\n",
1298 1302 mythreadno, skip_shutdown);
1299 1303 continue;
1300 1304 }
1301 1305
1302 1306 for (f = Files; f < &Files[nlogs]; f++) {
1303 1307 (void) pthread_mutex_lock(&f->filed_mutex);
1304 1308
1305 1309 if (f->f_type == F_UNUSED) {
1306 1310 (void) pthread_mutex_unlock(
1307 1311 &f->filed_mutex);
1308 1312 continue;
1309 1313 }
1310 1314
1311 1315 f->f_queue_count++;
1312 1316 mp->refcnt++;
1313 1317
1314 1318 if (dataq_enqueue(&f->f_queue,
1315 1319 (void *)mp) == -1) {
1316 1320 f->f_queue_count--;
1317 1321 mp->refcnt--;
1318 1322 (void) pthread_mutex_unlock(
1319 1323 &f->filed_mutex);
1320 1324 MALLOC_FAIL("dropping message");
1321 1325
1322 1326 if (mp->flags & SHUTDOWN) {
1323 1327 (void) logerror_to_console(1,
1324 1328 "unable to shutdown "
1325 1329 "logger thread");
1326 1330 }
1327 1331
1328 1332 continue;
1329 1333 }
1330 1334 DPRINT3(5, "logmsg(%u): enqueued msg %p "
1331 1335 "on queue %p\n", mythreadno,
1332 1336 (void *)mp, (void *)&f->f_queue);
1333 1337 (void) pthread_mutex_unlock(&f->filed_mutex);
1334 1338 }
1335 1339
1336 1340 /*
1337 1341 * flags value needs to be saved because mp may
1338 1342 * have been freed before SHUTDOWN test below.
1339 1343 */
1340 1344 flags = mp->flags;
1341 1345 refcnt = mp->refcnt;
1342 1346
1343 1347 (void) pthread_mutex_unlock(&mp->msg_mutex);
1344 1348 if (refcnt == 0)
1345 1349 free_msg(mp);
1346 1350
1347 1351 if (flags & SHUTDOWN) {
1348 1352 (void) pthread_mutex_lock(&hup_lock);
1349 1353 while (hup_state != HUP_COMPLETED) {
1350 1354 hup_state |= HUP_LOGMSG_SUSPENDED;
1351 1355 (void) pthread_cond_wait(&hup_done,
1352 1356 &hup_lock);
1353 1357 hup_state &= ~HUP_LOGMSG_SUSPENDED;
1354 1358 }
1355 1359 hup_state = HUP_ACCEPTABLE;
1356 1360 (void) pthread_mutex_unlock(&hup_lock);
1357 1361 fake_shutdown = 0;
1358 1362 }
1359 1363 continue;
1360 1364 }
1361 1365
1362 1366 /*
1363 1367 * Check to see if msg looks non-standard.
1364 1368 */
1365 1369 if ((int)strlen(mp->msg) < 16 || mp->msg[3] != ' ' ||
1366 1370 mp->msg[6] != ' ' || mp->msg[9] != ':' ||
1367 1371 mp->msg[12] != ':' || mp->msg[15] != ' ')
1368 1372 mp->flags |= ADDDATE;
1369 1373
1370 1374 /* extract facility and priority level */
1371 1375 fac = (mp->pri & LOG_FACMASK) >> 3;
1372 1376 if (mp->flags & MARK)
1373 1377 fac = LOG_NFACILITIES;
1374 1378 prilev = mp->pri & LOG_PRIMASK;
1375 1379
1376 1380 DPRINT3(3, "logmsg(%u): fac = %d, pri = %d\n",
1377 1381 mythreadno, fac, prilev);
1378 1382
1379 1383 /*
1380 1384 * Because different devices log at different speeds,
1381 1385 * it's important to hold the mutex for the current
1382 1386 * message until it's been enqueued to all log files,
1383 1387 * so the reference count is accurate before any
1384 1388 * of the log threads can decrement it.
1385 1389 */
1386 1390 _NOTE(NOW_VISIBLE_TO_OTHER_THREADS(*mp))
1387 1391 _NOTE(COMPETING_THREADS_NOW)
1388 1392 (void) pthread_mutex_lock(&mp->msg_mutex);
1389 1393
1390 1394 for (f = Files; f < &Files[nlogs]; f++) {
1391 1395 /* skip messages that are incorrect priority */
1392 1396 if (f->f_pmask[fac] < (unsigned)prilev ||
1393 1397 f->f_pmask[fac] == NOPRI)
1394 1398 continue;
1395 1399 if (f->f_queue_count > Q_HIGHWATER_MARK) {
1396 1400 DPRINT4(5, "logmsg(%u): Dropping message "
1397 1401 "%p on file %p, count = %d\n",
1398 1402 mythreadno, (void *)mp, (void *)f,
1399 1403 f->f_queue_count);
1400 1404 continue;
1401 1405 }
1402 1406
1403 1407 /*
1404 1408 * Need to grab filed_mutex before testing the f_type.
1405 1409 * Otherwise logit() may set F_UNUSED after the test
1406 1410 * below, and start pulling out the pending messages.
1407 1411 */
1408 1412
1409 1413 (void) pthread_mutex_lock(&f->filed_mutex);
1410 1414
1411 1415 if (f->f_type == F_UNUSED ||
1412 1416 (f->f_type == F_FILE && (mp->flags & IGN_FILE)) ||
1413 1417 (f->f_type == F_CONSOLE &&
1414 1418 (mp->flags & IGN_CONS))) {
1415 1419 (void) pthread_mutex_unlock(&f->filed_mutex);
1416 1420 continue;
1417 1421 }
1418 1422
1419 1423 f->f_queue_count++;
1420 1424 mp->refcnt++;
1421 1425
1422 1426 if (dataq_enqueue(&f->f_queue, (void *)mp) == -1) {
1423 1427 f->f_queue_count--;
1424 1428 mp->refcnt--;
1425 1429 (void) pthread_mutex_unlock(&f->filed_mutex);
1426 1430 MALLOC_FAIL("dropping message");
1427 1431 continue;
1428 1432 }
1429 1433
1430 1434 DPRINT3(5, "logmsg(%u): enqueued msg %p on queue "
1431 1435 "%p\n", mythreadno, (void *)mp,
1432 1436 (void *)&f->f_queue);
1433 1437 (void) pthread_mutex_unlock(&f->filed_mutex);
1434 1438 }
1435 1439 refcnt = mp->refcnt;
1436 1440 (void) pthread_mutex_unlock(&mp->msg_mutex);
1437 1441 if (refcnt == 0)
1438 1442 free_msg(mp);
1439 1443 }
1440 1444 /*NOTREACHED*/
1441 1445 return (NULL);
1442 1446 }
1443 1447
1444 1448 /*
1445 1449 * function to actually write the log message to the selected file.
1446 1450 * each file has a logger thread that runs this routine. The function
1447 1451 * is called with a pointer to its file structure.
1448 1452 */
1449 1453 static void *
1450 1454 logit(void *ap)
1451 1455 {
1452 1456 struct filed *f = ap;
1453 1457 log_message_t *mp;
1454 1458 int forwardingloop = 0;
1455 1459 const char *errmsg = "logit(%u): %s to %s forwarding loop detected\n";
1456 1460 int i, currofst, prevofst, refcnt;
1457 1461 host_list_t *hlp;
1458 1462
1459 1463 assert(f != NULL);
1460 1464
1461 1465 DPRINT4(5, "logit(%u): logger started for \"%s\" (queue %p, filed "
1462 1466 "%p)\n", f->f_thread, f->f_un.f_fname, (void *)&f->f_queue,
1463 1467 (void *)f);
1464 1468 _NOTE(COMPETING_THREADS_NOW);
1465 1469
1466 1470 while (f->f_type != F_UNUSED) {
1467 1471 (void) dataq_dequeue(&f->f_queue, (void **)&mp, 0);
1468 1472 DPRINT3(5, "logit(%u): logger dequeued msg %p from queue "
1469 1473 "%p\n", f->f_thread, (void *)mp, (void *)&f->f_queue);
1470 1474 (void) pthread_mutex_lock(&f->filed_mutex);
1471 1475 assert(f->f_queue_count > 0);
1472 1476 f->f_queue_count--;
1473 1477 (void) pthread_mutex_unlock(&f->filed_mutex);
1474 1478 assert(mp->refcnt > 0);
1475 1479
1476 1480 /*
1477 1481 * is it a shutdown message ?
1478 1482 */
1479 1483 if (mp->flags & SHUTDOWN) {
1480 1484 (void) pthread_mutex_lock(&mp->msg_mutex);
1481 1485 refcnt = --mp->refcnt;
1482 1486 (void) pthread_mutex_unlock(&mp->msg_mutex);
1483 1487 if (refcnt == 0)
1484 1488 free_msg(mp);
1485 1489 break;
1486 1490 }
1487 1491
1488 1492 /*
1489 1493 * Is it a logsync message?
1490 1494 */
1491 1495 if ((mp->flags & (FLUSHMSG | LOGSYNC)) ==
1492 1496 (FLUSHMSG | LOGSYNC)) {
1493 1497 if (f->f_type != F_FILE)
1494 1498 goto out; /* nothing to do */
1495 1499 (void) close(f->f_file);
1496 1500 f->f_file = open64(f->f_un.f_fname,
1497 1501 O_WRONLY|O_APPEND|O_NOCTTY);
1498 1502 if (f->f_file < 0) {
1499 1503 f->f_type = F_UNUSED;
1500 1504 logerror(f->f_un.f_fname);
1501 1505 f->f_stat.errs++;
1502 1506 }
1503 1507 goto out;
1504 1508 }
1505 1509
1506 1510 /*
1507 1511 * If the message flags include both flush and sync,
1508 1512 * then just sync the file out to disk if appropriate.
1509 1513 */
1510 1514 if ((mp->flags & (FLUSHMSG | SYNC_FILE)) ==
1511 1515 (FLUSHMSG | SYNC_FILE)) {
1512 1516 if (f->f_type == F_FILE) {
1513 1517 DPRINT2(5, "logit(%u): got FLUSH|SYNC "
1514 1518 "for filed %p\n", f->f_thread,
1515 1519 (void *)f);
1516 1520 (void) fsync(f->f_file);
1517 1521 }
1518 1522 goto out;
1519 1523 }
1520 1524
1521 1525 /*
1522 1526 * Otherwise if it's a standard flush message, write
1523 1527 * out any saved messages to the file.
1524 1528 */
1525 1529 if ((mp->flags & FLUSHMSG) && (f->f_prevcount > 0)) {
1526 1530 set_flush_msg(f);
1527 1531 writemsg(SAVED, f);
1528 1532 goto out;
1529 1533 }
1530 1534
1531 1535 (void) strlcpy(f->f_current.msg, mp->msg, MAXLINE+1);
1532 1536 (void) strlcpy(f->f_current.host, mp->hlp->hl_hosts[0],
1533 1537 SYS_NMLN);
1534 1538 f->f_current.pri = mp->pri;
1535 1539 f->f_current.flags = mp->flags;
1536 1540 f->f_current.time = mp->ts;
1537 1541 f->f_msgflag &= ~CURRENT_VALID;
1538 1542 hlp = mp->hlp;
1539 1543
1540 1544 prevofst = (f->f_prevmsg.flags & ADDDATE) ? 0 : 16;
1541 1545 currofst = (f->f_current.flags & ADDDATE) ? 0 : 16;
1542 1546
1543 1547 if (f->f_type == F_FORW) {
1544 1548 /*
1545 1549 * Should not forward MARK messages, as they are
1546 1550 * not defined outside of the current system.
1547 1551 */
1548 1552
1549 1553 if (mp->flags & MARK) {
1550 1554 DPRINT1(1, "logit(%u): cannot forward "
1551 1555 "Mark\n", f->f_thread);
1552 1556 goto out;
1553 1557 }
1554 1558
1555 1559 /*
1556 1560 * can not forward message if we do
1557 1561 * not have a host to forward to
1558 1562 */
1559 1563 if (hlp == (host_list_t *)NULL)
1560 1564 goto out;
1561 1565 /*
1562 1566 * a forwarding loop is created on machines
1563 1567 * with multiple interfaces because the
1564 1568 * network address of the sender is different
1565 1569 * to the receiver even though it is the
1566 1570 * same machine. Instead, if the
1567 1571 * hostname the source and target are
1568 1572 * the same the message if thrown away
1569 1573 */
1570 1574 forwardingloop = 0;
1571 1575 for (i = 0; i < hlp->hl_cnt; i++) {
1572 1576 if (strcmp(hlp->hl_hosts[i],
1573 1577 f->f_un.f_forw.f_hname) == 0) {
1574 1578 DPRINT3(1, errmsg, f->f_thread,
1575 1579 f->f_un.f_forw.f_hname,
1576 1580 hlp->hl_hosts[i]);
1577 1581 forwardingloop = 1;
1578 1582 break;
1579 1583 }
1580 1584 }
1581 1585
1582 1586 if (forwardingloop == 1) {
1583 1587 f->f_stat.cantfwd++;
1584 1588 goto out;
1585 1589 }
1586 1590 }
1587 1591
1588 1592 f->f_msgflag |= CURRENT_VALID;
1589 1593
1590 1594 /* check for dup message */
1591 1595 if (f->f_type != F_FORW &&
1592 1596 (f->f_msgflag & OLD_VALID) &&
1593 1597 prevofst == currofst &&
1594 1598 (strcmp(f->f_prevmsg.msg + prevofst,
1595 1599 f->f_current.msg + currofst) == 0) &&
1596 1600 (strcmp(f->f_prevmsg.host,
1597 1601 f->f_current.host) == 0)) {
1598 1602 /* a dup */
1599 1603 DPRINT2(2, "logit(%u): msg is dup - %p\n",
1600 1604 f->f_thread, (void *)mp);
1601 1605 if (currofst == 16) {
1602 1606 (void) strncpy(f->f_prevmsg.msg,
1603 1607 f->f_current.msg, 15); /* update time */
1604 1608 }
1605 1609 f->f_prevcount++;
1606 1610 f->f_stat.dups++;
1607 1611 f->f_stat.total++;
1608 1612 f->f_msgflag &= ~CURRENT_VALID;
1609 1613 } else {
1610 1614 /* new: mark or prior dups exist */
1611 1615 if (f->f_current.flags & MARK || f->f_prevcount > 0) {
1612 1616 if (f->f_prevcount > 0 && f->f_type != F_FORW) {
1613 1617 set_flush_msg(f);
1614 1618 if (f->f_msgflag & OLD_VALID) {
1615 1619 writemsg(SAVED, f);
1616 1620 }
1617 1621 }
1618 1622 if (f->f_msgflag & CURRENT_VALID)
1619 1623 writemsg(CURRENT, f);
1620 1624 if (!(mp->flags & NOCOPY))
1621 1625 copy_msg(f);
1622 1626 if (f->f_current.flags & MARK) {
1623 1627 DPRINT2(2, "logit(%u): msg is "
1624 1628 "mark - %p)\n", f->f_thread,
1625 1629 (void *)mp);
1626 1630 f->f_msgflag &= ~OLD_VALID;
1627 1631 } else {
1628 1632 DPRINT2(2, "logit(%u): saving "
1629 1633 "message - %p\n", f->f_thread,
1630 1634 (void *)mp);
1631 1635 }
1632 1636 f->f_stat.total++;
1633 1637 } else { /* new message */
1634 1638 DPRINT2(2, "logit(%u): msg is new "
1635 1639 "- %p\n", f->f_thread, (void *)mp);
1636 1640 writemsg(CURRENT, f);
1637 1641 if (!(mp->flags & NOCOPY))
1638 1642 copy_msg(f);
1639 1643 f->f_stat.total++;
1640 1644 }
1641 1645 }
1642 1646 /*
1643 1647 * if message refcnt goes to zero after we decrement
1644 1648 * it here, we are the last consumer of the message,
1645 1649 * and we should free it. We need to hold the lock
1646 1650 * between decrementing the count and checking for
1647 1651 * zero so another thread doesn't beat us to it.
1648 1652 */
1649 1653 out:
1650 1654 (void) pthread_mutex_lock(&mp->msg_mutex);
1651 1655 refcnt = --mp->refcnt;
1652 1656 (void) pthread_mutex_unlock(&mp->msg_mutex);
1653 1657 if (refcnt == 0)
1654 1658 free_msg(mp);
1655 1659 }
1656 1660 /* register our exit */
1657 1661
1658 1662 /*
1659 1663 * Pull out all pending messages, if they exist.
1660 1664 */
1661 1665
1662 1666 (void) pthread_mutex_lock(&f->filed_mutex);
1663 1667
1664 1668 while (f->f_queue_count > 0) {
1665 1669 (void) dataq_dequeue(&f->f_queue, (void **)&mp, 0);
1666 1670 DPRINT3(5, "logit(%u): logger dequeued msg %p from queue "
1667 1671 "%p\n",
1668 1672 f->f_thread, (void *)mp, (void *)&f->f_queue);
1669 1673 (void) pthread_mutex_lock(&mp->msg_mutex);
1670 1674 refcnt = --mp->refcnt;
1671 1675 (void) pthread_mutex_unlock(&mp->msg_mutex);
1672 1676 if (refcnt == 0)
1673 1677 free_msg(mp);
1674 1678 f->f_queue_count--;
1675 1679 }
1676 1680
1677 1681 (void) pthread_mutex_unlock(&f->filed_mutex);
1678 1682
1679 1683 if (f->f_type != F_USERS && f->f_type != F_WALL &&
1680 1684 f->f_type != F_UNUSED) {
1681 1685 if (f->f_type == F_FORW)
1682 1686 (void) t_close(f->f_file);
1683 1687 else
1684 1688 (void) close(f->f_file);
1685 1689 }
1686 1690
1687 1691 /*
1688 1692 * Since f_type may have been changed before this point, we need
1689 1693 * to test orig_type.
1690 1694 */
1691 1695 if (f->f_orig_type == F_FORW) {
1692 1696 free(f->f_un.f_forw.f_addr.buf);
1693 1697 }
1694 1698
1695 1699 f->f_type = F_UNUSED;
1696 1700 (void) pthread_mutex_lock(&cft);
1697 1701 --conf_threads;
1698 1702 (void) pthread_mutex_unlock(&cft);
1699 1703 DPRINT1(5, "logit(%u): logging thread exited\n", f->f_thread);
1700 1704 return (NULL);
1701 1705 }
1702 1706
1703 1707 /*
1704 1708 * change the previous message to a flush message, stating how
1705 1709 * many repeats occurred since the last flush
1706 1710 */
1707 1711 static void
1708 1712 set_flush_msg(struct filed *f)
1709 1713 {
1710 1714 char tbuf[10];
1711 1715 int prevofst = (f->f_prevmsg.flags & ADDDATE) ? 0 : 16;
1712 1716
1713 1717 if (f->f_prevcount == 1)
1714 1718 (void) strncpy(tbuf, "time", sizeof (tbuf));
1715 1719 else
1716 1720 (void) strncpy(tbuf, "times", sizeof (tbuf));
1717 1721
1718 1722 (void) snprintf(f->f_prevmsg.msg+prevofst,
1719 1723 sizeof (f->f_prevmsg.msg) - prevofst,
1720 1724 "last message repeated %d %s", f->f_prevcount, tbuf);
1721 1725 f->f_prevcount = 0;
1722 1726 f->f_msgflag |= OLD_VALID;
1723 1727 }
1724 1728
1725 1729
1726 1730 /*
1727 1731 * the actual writing of the message is broken into a separate function
1728 1732 * because each file has a current and saved message associated with
1729 1733 * it (for duplicate message detection). It is necessary to be able
1730 1734 * to write either the saved message or the current message.
1731 1735 */
1732 1736 static void
1733 1737 writemsg(int selection, struct filed *f)
1734 1738 {
1735 1739 char *cp, *p;
1736 1740 int pri;
1737 1741 int flags;
1738 1742 int l;
1739 1743 time_t ts;
1740 1744 struct t_unitdata ud;
1741 1745 char *eomp, *eomp2, *from, *text, *msg;
1742 1746 char line[MAXLINE*2];
1743 1747 char head[MAXLINE+1];
1744 1748 char tmpbuf[MAXLINE+1];
1745 1749 char cbuf[30];
1746 1750 char *filtered;
1747 1751 char *msgid_start, *msgid_end;
1748 1752 pthread_t mythreadno;
1749 1753 size_t hlen, filter_len;
1750 1754
1751 1755 if (Debug) {
1752 1756 mythreadno = pthread_self();
1753 1757 }
1754 1758
1755 1759 switch (selection) {
1756 1760 default:
1757 1761 case CURRENT: /* print current message */
1758 1762 msg = f->f_current.msg;
1759 1763 from = f->f_current.host;
1760 1764 pri = f->f_current.pri;
1761 1765 flags = f->f_current.flags;
1762 1766 ts = f->f_current.time;
1763 1767 f->f_msgflag &= ~CURRENT_VALID;
1764 1768 break;
1765 1769 case SAVED: /* print saved message */
1766 1770 msg = f->f_prevmsg.msg;
1767 1771 from = f->f_prevmsg.host;
1768 1772 pri = f->f_prevmsg.pri;
1769 1773 flags = f->f_prevmsg.flags;
1770 1774 ts = f->f_prevmsg.time;
1771 1775 f->f_msgflag &= ~OLD_VALID;
1772 1776 break;
1773 1777 }
1774 1778
1775 1779 if (msg[0] == '\0')
1776 1780 return;
1777 1781
1778 1782 cp = line;
1779 1783
1780 1784 if (flags & ADDDATE)
1781 1785 (void) strncpy(cp, ctime_r(&ts, cbuf) + 4, 15);
1782 1786 else
1783 1787 (void) strncpy(cp, msg, 15);
1784 1788
1785 1789 line[15] = '\0';
1786 1790 (void) strcat(cp, " ");
1787 1791 (void) strcat(cp, from);
1788 1792 (void) strcat(cp, " ");
1789 1793 text = cp + strlen(cp);
1790 1794
1791 1795 if (flags & ADDDATE)
1792 1796 (void) strcat(cp, msg);
1793 1797 else
1794 1798 (void) strcat(cp, msg+16);
1795 1799 DPRINT2(5, "writemsg(%u): text = \"%s\"\n", mythreadno, text);
1796 1800
1797 1801 errno = 0;
1798 1802 t_errno = 0;
1799 1803 switch (f->f_type) {
1800 1804 case F_UNUSED:
1801 1805 DPRINT1(1, "writemsg(%u): UNUSED\n", mythreadno);
1802 1806 break;
1803 1807 case F_FORW:
1804 1808 DPRINT4(1, "writemsg(%u): Logging msg '%s' to %s %s\n",
1805 1809 mythreadno, msg, TypeNames[f->f_type],
1806 1810 f->f_un.f_forw.f_hname);
1807 1811
1808 1812 hlen = snprintf(head, sizeof (head),
1809 1813 "<%d>%.15s ", pri, cp);
1810 1814
1811 1815 DPRINT2(5, "writemsg(%u): head = \"%s\"\n", mythreadno, head);
1812 1816 DPRINT2(5, "writemsg(%u): hlen = %d\n", mythreadno, hlen);
1813 1817
1814 1818 l = strlen(text);
1815 1819 p = text;
1816 1820
1817 1821 DPRINT2(5, "writemsg(%u): text = \"%s\"\n", mythreadno, text);
1818 1822 DPRINT2(5, "writemsg(%u): strlen(text) = %d\n", mythreadno, l);
1819 1823
1820 1824 (void) strncpy(tmpbuf, head, hlen);
1821 1825
1822 1826 while (l > 0) {
1823 1827 size_t len;
1824 1828
1825 1829 len = copy_frwd(tmpbuf + hlen, sizeof (tmpbuf) - hlen,
1826 1830 p, l);
1827 1831
1828 1832 DPRINT2(5, "writemsg(%u): tmpbuf = \"%s\"\n",
1829 1833 mythreadno, tmpbuf);
1830 1834 DPRINT2(5, "writemsg(%u): len = %d\n", mythreadno,
1831 1835 len);
1832 1836 DPRINT2(5, "writemsg(%u): strlen(tmpbuf) = %d\n",
1833 1837 mythreadno, strlen(tmpbuf));
1834 1838
1835 1839 ud.opt.buf = NULL;
1836 1840 ud.opt.len = 0;
1837 1841 ud.udata.buf = tmpbuf;
1838 1842 ud.udata.len = len + hlen;
1839 1843 ud.addr.maxlen = f->f_un.f_forw.f_addr.maxlen;
1840 1844 ud.addr.buf = f->f_un.f_forw.f_addr.buf;
1841 1845 ud.addr.len = f->f_un.f_forw.f_addr.len;
1842 1846 if (t_sndudata(f->f_file, &ud) < 0) {
1843 1847 if ((hup_state & HUP_INPROGRESS) &&
1844 1848 f->f_type == F_UNUSED) {
1845 1849 break;
1846 1850 }
1847 1851 (void) t_close(f->f_file);
1848 1852 f->f_type = F_UNUSED;
1849 1853 logerror("t_sndudata");
1850 1854
1851 1855 /*
1852 1856 * Since it has already failed, it's not worth
1853 1857 * continuing output from the middle of
1854 1858 * message string.
1855 1859 */
1856 1860 break;
1857 1861 }
1858 1862 p += len;
1859 1863 l -= len;
1860 1864 }
1861 1865 break;
1862 1866 case F_CONSOLE:
1863 1867 case F_TTY:
1864 1868 case F_FILE:
1865 1869 case F_USERS:
1866 1870 case F_WALL:
1867 1871 DPRINT4(1, "writemsg(%u): Logging msg '%s' to %s %s\n",
1868 1872 mythreadno, msg, TypeNames[f->f_type],
1869 1873 ((f->f_type == F_USERS) || (f->f_type == F_WALL)) ?
1870 1874 "" : f->f_un.f_fname);
1871 1875 /*
1872 1876 * filter the string in preparation for writing it
1873 1877 * save the original for possible forwarding.
1874 1878 * In case every byte in cp is a control character,
1875 1879 * allocates large enough buffer for filtered.
1876 1880 */
1877 1881
1878 1882 filter_len = strlen(cp) * 4 + 1;
1879 1883 filtered = (char *)malloc(filter_len);
1880 1884 if (!filtered) {
1881 1885 MALLOC_FAIL("dropping message");
1882 1886 /* seems we can just return */
1883 1887 return;
1884 1888 }
1885 1889 DPRINT3(5, "writemsg(%u): "
1886 1890 "filtered allocated (%p: %d bytes)\n",
1887 1891 mythreadno, (void *)filtered, filter_len);
1888 1892 /* -3 : we may add "\r\n" to ecomp(filtered) later */
1889 1893 filter_string(cp, filtered, filter_len - 3);
1890 1894
1891 1895 DPRINT2(5, "writemsg(%u): strlen(filtered) = %d\n",
1892 1896 mythreadno, strlen(filtered));
1893 1897 /*
1894 1898 * If we're writing to the console, strip out the message ID
1895 1899 * to reduce visual clutter.
1896 1900 */
1897 1901 if ((msgid_start = strstr(filtered, "[ID ")) != NULL &&
1898 1902 (msgid_end = strstr(msgid_start, "] ")) != NULL &&
1899 1903 f->f_type == F_CONSOLE)
1900 1904 (void) strcpy(msgid_start, msgid_end + 2);
1901 1905
1902 1906 eomp = filtered + strlen(filtered);
1903 1907
1904 1908 if ((f->f_type == F_USERS) || (f->f_type == F_WALL)) {
1905 1909 /* CSTYLED */
1906 1910 (void) strcat(eomp, "\r\n"); /*lint !e669*/
1907 1911 /*
1908 1912 * Since wallmsg messes with utmpx we need
1909 1913 * to guarantee single threadedness...
1910 1914 */
1911 1915 (void) pthread_mutex_lock(&wmp);
1912 1916 wallmsg(f, from, filtered);
1913 1917 (void) pthread_mutex_unlock(&wmp);
1914 1918
1915 1919 /*
1916 1920 * The contents of filtered have been copied
1917 1921 * out to the struct walldev. We should free it here.
1918 1922 */
1919 1923
1920 1924 free(filtered);
1921 1925
1922 1926 /* exiting the switch */
1923 1927 break;
1924 1928 } else if (f->f_type != F_FILE) {
1925 1929 /* CSTYLED */
1926 1930 (void) strncpy(eomp, "\r\n", 3); /*lint !e669*/
1927 1931 } else {
1928 1932 if ((eomp2 = strchr(filtered, '\r')) != NULL) {
1929 1933 (void) strncpy(eomp2, "\n", 2);
1930 1934 } else {
1931 1935 /* CSTYLED */
1932 1936 (void) strncpy(eomp, "\n", 2); /*lint !e669*/
1933 1937 }
1934 1938 }
1935 1939 if (write(f->f_file, filtered, strlen(filtered)) < 0) {
1936 1940 int e = errno;
1937 1941
1938 1942 if ((hup_state & HUP_INPROGRESS) &&
1939 1943 f->f_type == F_UNUSED) {
1940 1944 free(filtered);
1941 1945 break;
1942 1946 }
1943 1947 (void) close(f->f_file);
1944 1948 /*
1945 1949 * Check for EBADF on TTY's due
1946 1950 * to vhangup() XXX
1947 1951 */
1948 1952 if (e == EBADF && f->f_type != F_FILE) {
1949 1953 f->f_file = open(f->f_un.f_fname,
1950 1954 O_WRONLY|O_APPEND|O_NOCTTY);
1951 1955 if (f->f_file < 0) {
1952 1956 f->f_type = F_UNUSED;
1953 1957 logerror(f->f_un.f_fname);
1954 1958 f->f_stat.errs++;
1955 1959 }
1956 1960 untty();
1957 1961 } else {
1958 1962 f->f_type = F_UNUSED;
1959 1963 f->f_stat.errs++;
1960 1964 errno = e;
1961 1965 logerror(f->f_un.f_fname);
1962 1966 }
1963 1967 } else if (flags & SYNC_FILE)
1964 1968 if (((pri & LOG_FACMASK) >> 3) == LOG_KERN)
1965 1969 (void) fsync(f->f_file);
1966 1970
1967 1971 DPRINT2(5, "writemsg(%u): freeing filtered (%p)\n",
1968 1972 mythreadno, (void *)filtered);
1969 1973
1970 1974 free(filtered);
1971 1975 break;
1972 1976 }
1973 1977 }
1974 1978
1975 1979 /*
1976 1980 * WALLMSG -- Write a message to the world at large
1977 1981 *
1978 1982 * Write the specified message to either the entire
1979 1983 * world, or a list of approved users.
1980 1984 */
1981 1985 static void
1982 1986 wallmsg(struct filed *f, char *from, char *msg)
1983 1987 {
1984 1988 int i;
1985 1989 size_t len, clen;
1986 1990 char *buf = NULL;
1987 1991 struct utmpx *utxp;
1988 1992 time_t now;
1989 1993 char line[512], dev[100];
1990 1994 char cp[MAXLINE+1];
1991 1995 struct stat statbuf;
1992 1996 walldev_t *w;
1993 1997 char cbuf[30];
1994 1998 pthread_t mythreadno;
1995 1999
1996 2000 if (Debug) {
1997 2001 mythreadno = pthread_self();
1998 2002 }
1999 2003
2000 2004 if (access(UTMPX_FILE, R_OK) != 0 || stat(UTMPX_FILE, &statbuf) != 0) {
2001 2005 logerror(UTMPX_FILE);
2002 2006 return;
2003 2007 } else if (statbuf.st_uid != 0 || (statbuf.st_mode & 07777) != 0644) {
2004 2008 (void) snprintf(line, sizeof (line), "%s %s", UTMPX_FILE,
2005 2009 "not owned by root or not mode 644.\n"
2006 2010 "This file must be owned by root "
2007 2011 "and not writable by\n"
2008 2012 "anyone other than root. This alert is being "
2009 2013 "dropped because of\n"
2010 2014 "this problem.");
2011 2015 logerror(line);
2012 2016 return;
2013 2017 }
2014 2018
2015 2019 if (f->f_type == F_WALL) {
2016 2020 (void) time(&now);
2017 2021 len = snprintf(line, sizeof (line),
2018 2022 "\r\n\7Message from syslogd@%s "
2019 2023 "at %.24s ...\r\n", from, ctime_r(&now, cbuf));
2020 2024 len += strlen(msg + 16);
2021 2025 buf = (char *)malloc(len + 1);
2022 2026 if (!buf) {
2023 2027 MALLOC_FAIL("dropping message");
2024 2028 return;
2025 2029 }
2026 2030 DPRINT3(5, "wallmsg(%u): buf allocated (%p: %d bytes)\n",
2027 2031 mythreadno, (void *)buf, len + 1);
2028 2032 (void) strcpy(buf, line);
2029 2033 (void) strcat(buf, msg + 16);
2030 2034 clen = copy_frwd(cp, sizeof (cp), buf, len);
2031 2035 DPRINT2(5, "wallmsg(%u): clen = %d\n",
2032 2036 mythreadno, clen);
2033 2037 DPRINT2(5, "wallmsg(%u): freeing buf (%p)\n",
2034 2038 mythreadno, (void *)buf);
2035 2039 free(buf);
2036 2040 } else {
2037 2041 clen = copy_frwd(cp, sizeof (cp), msg, strlen(msg));
2038 2042 DPRINT2(5, "wallmsg(%u): clen = %d\n",
2039 2043 mythreadno, clen);
2040 2044 }
2041 2045 /* scan the user login file */
2042 2046 setutxent();
2043 2047 while ((utxp = getutxent()) != NULL) {
2044 2048 /* is this slot used? */
2045 2049 if (utxp->ut_name[0] == '\0' ||
2046 2050 utxp->ut_line[0] == '\0' ||
2047 2051 utxp->ut_type != USER_PROCESS)
2048 2052 continue;
2049 2053 /* should we send the message to this user? */
2050 2054 if (f->f_type == F_USERS) {
2051 2055 for (i = 0; i < MAXUNAMES; i++) {
2052 2056 if (!f->f_un.f_uname[i][0]) {
2053 2057 i = MAXUNAMES;
2054 2058 break;
2055 2059 }
2056 2060 if (strncmp(f->f_un.f_uname[i],
2057 2061 utxp->ut_name, UNAMESZ) == 0)
2058 2062 break;
2059 2063 }
2060 2064 if (i >= MAXUNAMES)
2061 2065 continue;
2062 2066 }
2063 2067
2064 2068 /* compute the device name */
2065 2069 if (utxp->ut_line[0] == '/') {
2066 2070 (void) strncpy(dev, utxp->ut_line, UDEVSZ);
2067 2071 } else {
2068 2072 (void) strcpy(dev, "/dev/");
2069 2073 (void) strncat(dev, utxp->ut_line, UDEVSZ);
2070 2074 }
2071 2075 DPRINT2(1, "wallmsg(%u): write to '%s'\n", mythreadno,
2072 2076 dev);
2073 2077
2074 2078 if ((w = malloc(sizeof (walldev_t))) != NULL) {
2075 2079 int rc;
2076 2080 (void) pthread_attr_init(&w->thread_attr);
2077 2081 (void) pthread_attr_setdetachstate(&w->thread_attr,
2078 2082 PTHREAD_CREATE_DETACHED);
2079 2083 (void) strncpy(w->dev, dev, PATH_MAX);
2080 2084 (void) strncpy(w->msg, cp, MAXLINE+1);
2081 2085 (void) strncpy(w->ut_name, utxp->ut_name,
2082 2086 sizeof (w->ut_name));
2083 2087
2084 2088 if ((rc = pthread_create(&w->thread, &w->thread_attr,
2085 2089 writetodev, (void *) w)) != 0) {
2086 2090 DPRINT2(5, "wallmsg(%u): wallmsg thread "
2087 2091 "create failed rc = %d\n",
2088 2092 mythreadno, rc);
2089 2093 free(w);
2090 2094 break;
2091 2095 }
2092 2096 } else {
2093 2097 MALLOC_FAIL("dropping message to user");
2094 2098 }
2095 2099 }
2096 2100 /* close the user login file */
2097 2101 endutxent();
2098 2102 }
2099 2103
2100 2104 /*
2101 2105 * Each time we need to write to a tty device (a potentially expensive
2102 2106 * or long-running operation) this routine gets called as a new
2103 2107 * detached, unbound thread. This allows writes to many devices
2104 2108 * to proceed nearly in parallel, without having to resort to
2105 2109 * asynchronous I/O or forking.
2106 2110 */
2107 2111 static void *
2108 2112 writetodev(void *ap)
2109 2113 {
2110 2114 walldev_t *w = ap;
2111 2115 int ttyf;
2112 2116 int len;
2113 2117 struct stat statb;
2114 2118 struct passwd pw, *pwp;
2115 2119 char pwbuf[MAXLINE];
2116 2120 pthread_t mythreadno;
2117 2121
2118 2122 if (Debug) {
2119 2123 mythreadno = pthread_self();
2120 2124 }
2121 2125
2122 2126 DPRINT1(1, "writetodev(%u): Device writer thread started\n",
2123 2127 mythreadno);
2124 2128
2125 2129 len = strlen(w->msg);
2126 2130
2127 2131 ttyf = open(w->dev, O_WRONLY|O_NOCTTY|O_NDELAY);
2128 2132 if (ttyf >= 0) {
2129 2133 if (fstat(ttyf, &statb) != 0) {
2130 2134 DPRINT2(1, "writetodev(%u): Can't stat '%s'\n",
2131 2135 mythreadno, w->dev);
2132 2136 errno = 0;
2133 2137 logerror("Can't stat '%s'", w->dev);
2134 2138 } else if (!(statb.st_mode & S_IWRITE)) {
2135 2139 DPRINT2(1, "writetodev(%u): Can't write to "
2136 2140 "'%s'\n", mythreadno, w->dev);
2137 2141 } else if (!isatty(ttyf)) {
2138 2142 DPRINT2(1, "writetodev(%u): '%s' not a tty\n",
2139 2143 mythreadno, w->dev);
2140 2144 /*
2141 2145 * We might hit dtremote here. Don't generate
2142 2146 * error message.
2143 2147 */
2144 2148 } else if (getpwuid_r(statb.st_uid, &pw, pwbuf,
2145 2149 sizeof (pwbuf), &pwp) != 0) {
2146 2150 DPRINT2(1, "writetodev(%u): Can't determine owner "
2147 2151 "of '%s'\n", mythreadno, w->dev);
2148 2152 errno = 0;
2149 2153 logerror("Can't determine owner of '%s'", w->dev);
2150 2154 } else if (strncmp(pw.pw_name, w->ut_name, UNAMESZ) != 0) {
2151 2155 DPRINT2(1, "writetodev(%u): Bad terminal owner '%s'"
2152 2156 "\n", mythreadno, w->dev);
2153 2157 errno = 0;
2154 2158 logerror("%s %s owns '%s' %s %.*s",
2155 2159 "Bad terminal owner;", pw.pw_name, w->dev,
2156 2160 "but utmpx says", UNAMESZ, w->ut_name);
2157 2161 } else if (write(ttyf, w->msg, len) != len) {
2158 2162 DPRINT2(1, "writetodev(%u): Write failed to "
2159 2163 "'%s'\n", mythreadno, w->dev);
2160 2164 errno = 0;
2161 2165 logerror("Write failed to '%s'", w->dev);
2162 2166 }
2163 2167
2164 2168 DPRINT2(1, "writetodev(%u): write to '%s' succeeded\n",
2165 2169 mythreadno, w->dev);
2166 2170
2167 2171 (void) close(ttyf);
2168 2172 } else {
2169 2173 DPRINT2(1, "writetodev(%u): Can't open '%s'\n",
2170 2174 mythreadno, w->dev);
2171 2175 }
2172 2176
2173 2177 (void) pthread_attr_destroy(&w->thread_attr);
2174 2178 free(w);
2175 2179
2176 2180 DPRINT1(1, "writetodev(%u): Device writer thread exiting\n",
2177 2181 mythreadno);
2178 2182
2179 2183 pthread_exit(0);
2180 2184 return (NULL);
2181 2185 /*NOTREACHED*/
2182 2186 }
2183 2187
2184 2188 /*
2185 2189 * Return a printable representation of a host address. If unable to
2186 2190 * look up hostname, format the numeric address for display instead.
2187 2191 *
2188 2192 * First calls hnc_lookup to see if there is valid cache entry for
2189 2193 * given network address. If it failed, cvthname looks up hostname,
2190 2194 * and push the results into the hostname cache.
2191 2195 */
2192 2196 static host_list_t *
2193 2197 cvthname(struct netbuf *nbp, struct netconfig *ncp, char *failsafe_addr)
2194 2198 {
2195 2199 int i;
2196 2200 host_list_t *h;
2197 2201 struct nd_hostservlist *hsp;
2198 2202 struct nd_hostserv *hspp;
2199 2203 pthread_t mythreadno;
2200 2204 int hindex;
2201 2205 char *uap;
2202 2206
2203 2207 if (Debug) {
2204 2208 mythreadno = pthread_self();
2205 2209 }
2206 2210
2207 2211 if (Debug)
2208 2212 uap = taddr2uaddr(ncp, nbp);
2209 2213
2210 2214 DPRINT2(2, "cvthname(%u): looking up hostname for %s\n",
2211 2215 mythreadno, uap ? uap : "<unknown>");
2212 2216
2213 2217 if ((h = hnc_lookup(nbp, ncp, &hindex)) != NULL) {
2214 2218 DPRINT4(2, "cvthname(%u): Cache found %p for %s (%s)\n",
2215 2219 mythreadno, (void *)h, uap ? uap : "<unknown>",
2216 2220 h->hl_hosts[0]);
2217 2221 return (h);
2218 2222 }
2219 2223 DPRINT2(2, "cvthname(%u): No cache found for %s\n",
2220 2224 mythreadno, uap ? uap : "<unknown>");
2221 2225
2222 2226 if (Debug)
2223 2227 free(uap);
2224 2228
2225 2229 if (ncp->nc_semantics != NC_TPI_CLTS) {
2226 2230 return (NULL);
2227 2231 }
2228 2232
2229 2233 /* memory allocation failure here is fatal */
2230 2234 if ((h = malloc(sizeof (host_list_t))) == NULL) {
2231 2235 MALLOC_FAIL("host name conversion");
2232 2236 return (NULL);
2233 2237 }
2234 2238
2235 2239 if (netdir_getbyaddr(ncp, &hsp, nbp) == 0) {
2236 2240 if (hsp->h_cnt <= 0) {
2237 2241 out: netdir_free((void *)hsp, ND_HOSTSERVLIST);
2238 2242 free(h);
2239 2243 return (NULL);
2240 2244 }
2241 2245
2242 2246 hspp = hsp->h_hostservs;
2243 2247 h->hl_cnt = hsp->h_cnt;
2244 2248 h->hl_hosts = (char **)malloc(sizeof (char *) * (h->hl_cnt));
2245 2249 if (h->hl_hosts == NULL) {
2246 2250 MALLOC_FAIL("host name conversion");
2247 2251 goto out;
2248 2252 }
2249 2253
2250 2254 DPRINT2(2, "cvthname(%u): Found %d hostnames\n",
2251 2255 mythreadno, h->hl_cnt);
2252 2256 for (i = 0; i < h->hl_cnt; i++) {
2253 2257 h->hl_hosts[i] = (char *)
2254 2258 malloc(sizeof (char) * (strlen(hspp->h_host) + 1));
2255 2259 if (h->hl_hosts[i] == NULL) {
2256 2260 int j;
2257 2261 for (j = 0; j < i; j++) {
2258 2262 free(h->hl_hosts[j]);
2259 2263 }
2260 2264 free(h->hl_hosts);
2261 2265 MALLOC_FAIL("host name conversion");
2262 2266 goto out;
2263 2267 }
2264 2268 (void) strcpy(h->hl_hosts[i], hspp->h_host);
2265 2269 hspp++;
2266 2270 }
2267 2271 netdir_free((void *)hsp, ND_HOSTSERVLIST);
2268 2272 } else { /* unknown address */
2269 2273 h->hl_cnt = 1;
2270 2274 h->hl_hosts = (char **)malloc(sizeof (char *));
2271 2275 if (h->hl_hosts == NULL) {
2272 2276 free(h);
2273 2277 MALLOC_FAIL("host name conversion");
2274 2278 return (NULL);
2275 2279 }
2276 2280 h->hl_hosts[0] = (char *)malloc(strlen(failsafe_addr) + 3);
2277 2281 if (h->hl_hosts[0] == NULL) {
2278 2282 free(h->hl_hosts);
2279 2283 free(h);
2280 2284 MALLOC_FAIL("host name conversion");
2281 2285 return (NULL);
2282 2286 }
2283 2287 /*LINTED*/
2284 2288 (void) sprintf(h->hl_hosts[0], "[%s]", failsafe_addr);
2285 2289 DPRINT2(1, "cvthname(%u): Hostname lookup failed "
2286 2290 "- using address %s instead\n",
2287 2291 mythreadno, h->hl_hosts[0]);
2288 2292 }
2289 2293
2290 2294 h->hl_refcnt = 1;
2291 2295 if (pthread_mutex_init(&h->hl_mutex, NULL) != 0) {
2292 2296 logerror("pthread_mutex_init failed");
2293 2297 /* This host_list won't be shared by the cache. */
2294 2298 return (h);
2295 2299 }
2296 2300 hnc_register(nbp, ncp, h, hindex);
2297 2301 DPRINT3(2, "cvthname(%u): returning %p for %s\n",
2298 2302 mythreadno, (void *)h, h->hl_hosts[0]);
2299 2303 return (h);
2300 2304 }
2301 2305
2302 2306 /*
2303 2307 * Print syslogd errors some place. Need to be careful here, because
2304 2308 * this routine is called at times when we're not initialized and
2305 2309 * ready to log messages...in this case, fall back to using the console.
2306 2310 */
2307 2311 void
2308 2312 logerror(const char *type, ...)
2309 2313 {
2310 2314 char buf[MAXLINE+1];
2311 2315 pthread_t mythreadno;
2312 2316 int flag;
2313 2317 va_list ap;
2314 2318
2315 2319 if (Debug) {
2316 2320 mythreadno = pthread_self();
2317 2321 }
2318 2322
2319 2323 va_start(ap, type);
2320 2324 logerror_format(type, buf, ap);
2321 2325 va_end(ap);
2322 2326 DPRINT2(1, "logerror(%u): %s\n", mythreadno, buf);
2323 2327
2324 2328 (void) pthread_mutex_lock(&logerror_lock);
2325 2329 if (!interrorlog) {
2326 2330 flag = 0;
2327 2331 if (logerror_to_console(1, buf) == 0) {
2328 2332 /* has written to the console */
2329 2333 flag = IGN_CONS;
2330 2334 }
2331 2335 (void) logmymsg(LOG_SYSLOG|LOG_ERR, buf, ADDDATE|flag, 1);
2332 2336 } else {
2333 2337 if (logmymsg(LOG_SYSLOG|LOG_ERR, buf, ADDDATE, 0) == -1) {
2334 2338 (void) logerror_to_console(1, buf);
2335 2339 }
2336 2340 }
2337 2341 (void) pthread_mutex_unlock(&logerror_lock);
2338 2342
2339 2343 errno = 0;
2340 2344 t_errno = 0;
2341 2345 }
2342 2346
2343 2347 static void
2344 2348 logerror_format(const char *type, char *buf, va_list ap)
2345 2349 {
2346 2350 char tmpbuf[MAXLINE + 1];
2347 2351 pthread_t mythreadno;
2348 2352
2349 2353 if (Debug) {
2350 2354 mythreadno = pthread_self();
2351 2355 }
2352 2356
2353 2357 (void) vsnprintf(tmpbuf, MAXLINE, type, ap);
2354 2358
2355 2359 if (t_errno == 0 || t_errno == TSYSERR) {
2356 2360 char *errstr;
2357 2361
2358 2362 if (errno == 0) {
2359 2363 (void) snprintf(buf, MAXLINE, "syslogd: %.*s",
2360 2364 MAXLINE, tmpbuf);
2361 2365 } else if ((errstr = strerror(errno)) == (char *)NULL) {
2362 2366 (void) snprintf(buf, MAXLINE, "syslogd: %s: error"
2363 2367 " %d", tmpbuf, errno);
2364 2368 } else {
2365 2369 (void) snprintf(buf, MAXLINE, "syslogd: %s: %s",
2366 2370 tmpbuf, errstr);
2367 2371 }
2368 2372 } else {
2369 2373 if (t_errno > t_nerr) {
2370 2374 (void) snprintf(buf, MAXLINE, "syslogd: %s:"
2371 2375 " t_error %d", tmpbuf, t_errno);
2372 2376 } else {
2373 2377 (void) snprintf(buf, MAXLINE, "syslogd: %s: %s",
2374 2378 tmpbuf, t_errlist[t_errno]);
2375 2379 }
2376 2380 }
2377 2381
2378 2382 DPRINT2(5, "logerror_format(%u): out %s\n", mythreadno, buf);
2379 2383 }
2380 2384
2381 2385 static int
2382 2386 logerror_to_console(int nonblock, const char *buf)
2383 2387 {
2384 2388 int cfd, modes;
2385 2389 pthread_t mythreadno;
2386 2390 int ret = 0, len;
2387 2391 char tmpbuf[MAXLINE + 1];
2388 2392
2389 2393 if (Debug) {
2390 2394 mythreadno = pthread_self();
2391 2395 }
2392 2396
2393 2397 DPRINT2(1, "logerror_to_console(%u): %s\n", mythreadno, buf);
2394 2398
2395 2399 /*
2396 2400 * must use open here instead of fopen, because
2397 2401 * we need the O_NOCTTY behavior - otherwise we
2398 2402 * could hang the console at boot time
2399 2403 */
2400 2404
2401 2405 modes = (nonblock) ?
2402 2406 O_WRONLY|O_APPEND|O_NOCTTY|O_NONBLOCK :
2403 2407 O_WRONLY|O_APPEND|O_NOCTTY;
2404 2408
2405 2409 if (((cfd = open(sysmsg, modes)) >= 0) ||
2406 2410 ((cfd = open(ctty, modes)) >= 0)) {
2407 2411 (void) snprintf(tmpbuf, MAXLINE, "%s\n", buf);
2408 2412 len = strlen(tmpbuf);
2409 2413 if (write(cfd, tmpbuf, len) != len) {
2410 2414 ret = 1;
2411 2415 }
2412 2416 (void) close(cfd);
2413 2417 } else {
2414 2418 ret = 1;
2415 2419
2416 2420 /* punt */
2417 2421 DPRINT1(1, "logerror_console(%u): can't open console\n",
2418 2422 mythreadno);
2419 2423 }
2420 2424 return (ret);
2421 2425 }
2422 2426
2423 2427 /*
2424 2428 * copy current message to saved message in filed structure.
2425 2429 */
2426 2430 static void
2427 2431 copy_msg(struct filed *f)
2428 2432 {
2429 2433 (void) strlcpy(f->f_prevmsg.msg, f->f_current.msg, MAXLINE+1);
2430 2434 (void) strlcpy(f->f_prevmsg.host, f->f_current.host, SYS_NMLN);
2431 2435 f->f_prevmsg.pri = f->f_current.pri;
2432 2436 f->f_prevmsg.flags = f->f_current.flags;
2433 2437 f->f_prevmsg.time = f->f_current.time;
2434 2438 f->f_msgflag |= OLD_VALID;
2435 2439 }
2436 2440
2437 2441
2438 2442 /*
2439 2443 * function to free a host_list_t struct that was allocated
2440 2444 * out of cvthname(). There is a special case where we don't
2441 2445 * free the hostname list in LocalHostName, because that's
2442 2446 * our own addresses, and we just want to have to look it
2443 2447 * up once and save it. Also don't free it if it's
2444 2448 * NullHostName, because that's a special one we use if
2445 2449 * name service lookup fails.
2446 2450 *
2447 2451 * By having hostname cache, now host_list_t will be shared
2448 2452 * by messages and hostname cache. hl_refcnt is used for
2449 2453 * the purpose.
2450 2454 */
2451 2455 static void
2452 2456 freehl(host_list_t *h)
2453 2457 {
2454 2458 int i, refcnt;
2455 2459 pthread_t mythreadno;
2456 2460
2457 2461 if (Debug) {
2458 2462 mythreadno = pthread_self();
2459 2463 }
2460 2464
2461 2465 DPRINT2(2, "freehl(%u): releasing %p\n", mythreadno, (void *)h);
2462 2466
2463 2467 if (h == NULL || h == &LocalHostName || h == &NullHostName) {
2464 2468 return;
2465 2469 }
2466 2470
2467 2471 (void) pthread_mutex_lock(&h->hl_mutex);
2468 2472 refcnt = --h->hl_refcnt;
2469 2473 (void) pthread_mutex_unlock(&h->hl_mutex);
2470 2474
2471 2475 if (refcnt != 0) {
2472 2476 DPRINT3(5, "freehl(%u): %p has reference %d\n",
2473 2477 mythreadno, (void *)h, refcnt);
2474 2478 return;
2475 2479 }
2476 2480
2477 2481 (void) pthread_mutex_destroy(&h->hl_mutex);
2478 2482
2479 2483 DPRINT2(5, "freehl(%u): freeing %p\n", mythreadno, (void *)h);
2480 2484
2481 2485 for (i = 0; i < h->hl_cnt; i++) {
2482 2486 free(h->hl_hosts[i]);
2483 2487 }
2484 2488
2485 2489 free(h->hl_hosts);
2486 2490 free(h);
2487 2491 }
2488 2492
2489 2493 /*
2490 2494 * Create the door file and the pid file in /var/run. If the filesystem
2491 2495 * containing /etc is writable, create symlinks /etc/.syslog_door and
2492 2496 * /etc/syslog.pid to them. On systems that do not support /var/run, create
2493 2497 * /etc/.syslog_door and /etc/syslog.pid directly.
2494 2498 *
2495 2499 * Note: it is not considered fatal to fail to create the pid file or its
2496 2500 * symlink. Attempts to use them in the usual way will fail, of course, but
2497 2501 * syslogd will function nicely without it (not so for the door file).
2498 2502 */
2499 2503
2500 2504 static void
2501 2505 open_door(void)
2502 2506 {
2503 2507 struct stat buf;
2504 2508 door_info_t info;
2505 2509 char line[MAXLINE+1];
2506 2510 pthread_t mythreadno;
2507 2511 int err;
2508 2512
2509 2513 if (Debug) {
2510 2514 mythreadno = pthread_self();
2511 2515 }
2512 2516
2513 2517 /*
2514 2518 * first see if another syslogd is running by trying
2515 2519 * a door call - if it succeeds, there is already
2516 2520 * a syslogd process active
2517 2521 */
2518 2522
2519 2523 if (!DoorCreated) {
2520 2524 int door;
2521 2525
2522 2526 if ((door = open(DoorFileName, O_RDONLY)) >= 0) {
2523 2527 DPRINT2(5, "open_door(%u): %s opened "
2524 2528 "successfully\n", mythreadno, DoorFileName);
2525 2529
2526 2530 if (door_info(door, &info) >= 0) {
2527 2531 DPRINT2(5, "open_door(%u): "
2528 2532 "door_info:info.di_target = %ld\n",
2529 2533 mythreadno, info.di_target);
2530 2534
2531 2535 if (info.di_target > 0) {
2532 2536 (void) sprintf(line, "syslogd pid %ld"
2533 2537 " already running. Cannot "
2534 2538 "start another syslogd pid %ld",
2535 2539 info.di_target, getpid());
2536 2540 DPRINT2(5, "open_door(%u): error: "
2537 2541 "%s\n", mythreadno, line);
2538 2542 errno = 0;
2539 2543 logerror(line);
2540 2544 exit(1);
2541 2545 }
2542 2546 }
2543 2547
2544 2548 (void) close(door);
2545 2549 } else {
2546 2550 if (lstat(DoorFileName, &buf) < 0) {
2547 2551 err = errno;
2548 2552
2549 2553 DPRINT3(5, "open_door(%u): lstat() of %s "
2550 2554 "failed, errno=%d\n",
2551 2555 mythreadno, DoorFileName, err);
2552 2556
2553 2557 if ((door = creat(DoorFileName, 0644)) < 0) {
2554 2558 err = errno;
2555 2559 (void) snprintf(line, sizeof (line),
2556 2560 "creat() of %s failed - fatal",
2557 2561 DoorFileName);
2558 2562 DPRINT3(1, "open_door(%u): error: %s, "
2559 2563 "errno=%d\n", mythreadno, line,
2560 2564 err);
2561 2565 errno = err;
2562 2566 logerror(line);
2563 2567 delete_doorfiles();
2564 2568 exit(1);
2565 2569 }
2566 2570
2567 2571 (void) fchmod(door,
2568 2572 S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
2569 2573
2570 2574 DPRINT2(5, "open_door(%u): creat() of %s "
2571 2575 "succeeded\n", mythreadno,
2572 2576 DoorFileName);
2573 2577
2574 2578 (void) close(door);
2575 2579 }
2576 2580 }
2577 2581
2578 2582 if (strcmp(DoorFileName, DOORFILE) == 0) {
2579 2583 if (lstat(OLD_DOORFILE, &buf) == 0) {
2580 2584 DPRINT2(5, "open_door(%u): lstat() of %s "
2581 2585 "succeeded\n", mythreadno,
2582 2586 OLD_DOORFILE);
2583 2587
2584 2588 if (S_ISDIR(buf.st_mode)) {
2585 2589 (void) snprintf(line, sizeof (line),
2586 2590 "%s is a directory - fatal",
2587 2591 OLD_DOORFILE);
2588 2592 DPRINT2(1, "open_door(%u): error: "
2589 2593 "%s\n", mythreadno, line);
2590 2594 errno = 0;
2591 2595 logerror(line);
2592 2596 delete_doorfiles();
2593 2597 exit(1);
2594 2598 }
2595 2599
2596 2600 DPRINT2(5, "open_door(%u): %s is not a "
2597 2601 "directory\n",
2598 2602 mythreadno, OLD_DOORFILE);
2599 2603
2600 2604 if (unlink(OLD_DOORFILE) < 0) {
2601 2605 err = errno;
2602 2606 (void) snprintf(line, sizeof (line),
2603 2607 "unlink() of %s failed",
2604 2608 OLD_DOORFILE);
2605 2609 DPRINT2(5, "open_door(%u): %s\n",
2606 2610 mythreadno, line);
2607 2611
2608 2612 if (err != EROFS) {
2609 2613 DPRINT3(1, "open_door(%u): "
2610 2614 "error: %s, "
2611 2615 "errno=%d\n",
2612 2616 mythreadno, line, err);
2613 2617 (void) strcat(line, " - fatal");
2614 2618 errno = err;
2615 2619 logerror(line);
2616 2620 delete_doorfiles();
2617 2621 exit(1);
2618 2622 }
2619 2623
2620 2624 DPRINT1(5, "open_door(%u): unlink "
2621 2625 "failure OK on RO file "
2622 2626 "system\n", mythreadno);
2623 2627 }
2624 2628 } else {
2625 2629 DPRINT2(5, "open_door(%u): file %s doesn't "
2626 2630 "exist\n", mythreadno, OLD_DOORFILE);
2627 2631 }
2628 2632
2629 2633 if (symlink(RELATIVE_DOORFILE, OLD_DOORFILE) < 0) {
2630 2634 err = errno;
2631 2635 (void) snprintf(line, sizeof (line),
2632 2636 "symlink %s -> %s failed", OLD_DOORFILE,
2633 2637 RELATIVE_DOORFILE);
2634 2638 DPRINT2(5, "open_door(%u): %s\n", mythreadno,
2635 2639 line);
2636 2640
2637 2641 if (err != EROFS) {
2638 2642 DPRINT3(1, "open_door(%u): error: %s, "
2639 2643 "errno=%d\n", mythreadno, line,
2640 2644 err);
2641 2645 errno = err;
2642 2646 (void) strcat(line, " - fatal");
2643 2647 logerror(line);
2644 2648 delete_doorfiles();
2645 2649 exit(1);
2646 2650 }
2647 2651
2648 2652 DPRINT1(5, "open_door(%u): symlink failure OK "
2649 2653 "on RO file system\n", mythreadno);
2650 2654 } else {
2651 2655 DPRINT3(5, "open_door(%u): symlink %s -> %s "
2652 2656 "succeeded\n", mythreadno,
2653 2657 OLD_DOORFILE, RELATIVE_DOORFILE);
2654 2658 }
2655 2659 }
2656 2660
2657 2661 if ((DoorFd = door_create(server, 0,
2658 2662 DOOR_REFUSE_DESC | DOOR_NO_CANCEL)) < 0) {
2659 2663 err = errno;
2660 2664 (void) sprintf(line, "door_create() failed - fatal");
2661 2665 DPRINT3(1, "open_door(%u): error: %s, errno=%d\n",
2662 2666 mythreadno, line, err);
2663 2667 errno = err;
2664 2668 logerror(line);
2665 2669 delete_doorfiles();
2666 2670 exit(1);
2667 2671 }
2668 2672 (void) door_setparam(DoorFd, DOOR_PARAM_DATA_MAX, 0);
2669 2673 DPRINT2(5, "open_door(%u): door_create() succeeded, "
2670 2674 "DoorFd=%d\n", mythreadno, DoorFd);
2671 2675
2672 2676 DoorCreated = 1;
2673 2677 }
2674 2678
2675 2679 (void) fdetach(DoorFileName); /* just in case... */
2676 2680
2677 2681 (void) door_server_create(door_server_pool);
2678 2682
2679 2683 if (fattach(DoorFd, DoorFileName) < 0) {
2680 2684 err = errno;
2681 2685 (void) snprintf(line, sizeof (line), "fattach() of fd"
2682 2686 " %d to %s failed - fatal", DoorFd, DoorFileName);
2683 2687 DPRINT3(1, "open_door(%u): error: %s, errno=%d\n", mythreadno,
2684 2688 line, err);
2685 2689 errno = err;
2686 2690 logerror(line);
2687 2691 delete_doorfiles();
2688 2692 exit(1);
2689 2693 }
2690 2694
2691 2695 DPRINT2(5, "open_door(%u): attached server() to %s\n", mythreadno,
2692 2696 DoorFileName);
2693 2697
2694 2698 /*
2695 2699 * create pidfile anyway, so those using it to control
2696 2700 * syslogd (with kill `cat /etc/syslog.pid` perhaps)
2697 2701 * don't get broken.
2698 2702 */
2699 2703
2700 2704 if (!PidfileCreated) {
2701 2705 int pidfd;
2702 2706
2703 2707 PidfileCreated = 1;
2704 2708
2705 2709 if ((pidfd = open(PidFileName, O_RDWR|O_CREAT|O_TRUNC, 0644))
2706 2710 < 0) {
2707 2711 err = errno;
2708 2712 (void) snprintf(line, sizeof (line),
2709 2713 "open() of %s failed", PidFileName);
2710 2714 DPRINT3(1, "open_door(%u): warning: %s, errno=%d\n",
2711 2715 mythreadno, line, err);
2712 2716 errno = err;
2713 2717 logerror(line);
2714 2718 return;
2715 2719 }
2716 2720
2717 2721 (void) fchmod(pidfd, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
2718 2722 (void) sprintf(line, "%ld\n", getpid());
2719 2723
2720 2724 if (write(pidfd, line, strlen(line)) < 0) {
2721 2725 err = errno;
2722 2726 (void) snprintf(line, sizeof (line),
2723 2727 "write to %s on fd %d failed", PidFileName, pidfd);
2724 2728 DPRINT3(1, "open_door(%u): warning: %s, errno=%d\n",
2725 2729 mythreadno, line, err);
2726 2730 errno = err;
2727 2731 logerror(line);
2728 2732 return;
2729 2733 }
2730 2734
2731 2735 (void) close(pidfd);
2732 2736
2733 2737 DPRINT2(5, "open_door(%u): %s created\n",
2734 2738 mythreadno, PidFileName);
2735 2739
2736 2740 if (strcmp(PidFileName, PIDFILE) == 0) {
2737 2741 if (lstat(OLD_PIDFILE, &buf) == 0) {
2738 2742 DPRINT2(5, "open_door(%u): lstat() of %s "
2739 2743 "succeded\n", mythreadno, OLD_PIDFILE);
2740 2744
2741 2745 if (S_ISDIR(buf.st_mode)) {
2742 2746 (void) snprintf(line, sizeof (line),
2743 2747 "file %s is a directory",
2744 2748 OLD_PIDFILE);
2745 2749 DPRINT2(1, "open_door(%u): warning: "
2746 2750 "%s\n", mythreadno, line);
2747 2751 errno = 0;
2748 2752 logerror(line);
2749 2753 return;
2750 2754 }
2751 2755
2752 2756 if (unlink(OLD_PIDFILE) < 0) {
2753 2757 err = errno;
2754 2758 (void) snprintf(line, sizeof (line),
2755 2759 "unlink() of %s failed",
2756 2760 OLD_PIDFILE);
2757 2761 DPRINT2(5, "open_door(%u): %s\n",
2758 2762 mythreadno, line);
2759 2763
2760 2764 if (err != EROFS) {
2761 2765 DPRINT3(1, "open_door (%u): "
2762 2766 "warning: %s, "
2763 2767 "errno=%d\n",
2764 2768 mythreadno, line, err);
2765 2769 errno = err;
2766 2770 logerror(line);
2767 2771 return;
2768 2772 }
2769 2773
2770 2774 DPRINT1(5, "open_door(%u): unlink "
2771 2775 "failure OK on RO file "
2772 2776 "system\n", mythreadno);
2773 2777 }
2774 2778 } else {
2775 2779 DPRINT2(5, "open_door(%u): file %s doesn't "
2776 2780 "exist\n", mythreadno, OLD_PIDFILE);
2777 2781 }
2778 2782
2779 2783 if (symlink(RELATIVE_PIDFILE, OLD_PIDFILE) < 0) {
2780 2784 err = errno;
2781 2785 (void) snprintf(line, sizeof (line),
2782 2786 "symlink %s -> %s failed", OLD_PIDFILE,
2783 2787 RELATIVE_PIDFILE);
2784 2788 DPRINT2(5, "open_door(%u): %s\n", mythreadno,
2785 2789 line);
2786 2790
2787 2791 if (err != EROFS) {
2788 2792 DPRINT3(1, "open_door(%u): warning: "
2789 2793 "%s, errno=%d\n", mythreadno,
2790 2794 line, err);
2791 2795 errno = err;
2792 2796 logerror(line);
2793 2797 return;
2794 2798 }
2795 2799
2796 2800 DPRINT1(5, "open_door(%u): symlink failure OK "
2797 2801 "on RO file system\n", mythreadno);
2798 2802 return;
2799 2803 }
2800 2804
2801 2805 DPRINT3(5, "open_door(%u): symlink %s -> %s "
2802 2806 "succeeded\n", mythreadno, OLD_PIDFILE,
2803 2807 RELATIVE_PIDFILE);
2804 2808 }
2805 2809 }
2806 2810 }
2807 2811
2808 2812 /*
2809 2813 * the 'server' function that we export via the door. It does
2810 2814 * nothing but return.
2811 2815 */
2812 2816 /*ARGSUSED*/
2813 2817 static void
2814 2818 server(void *cookie, char *argp, size_t arg_size,
2815 2819 door_desc_t *dp, uint_t n)
2816 2820 {
2817 2821 (void) door_return(NULL, 0, NULL, 0);
2818 2822 /* NOTREACHED */
2819 2823 }
2820 2824
2821 2825 /*ARGSUSED*/
2822 2826 static void *
2823 2827 create_door_thr(void *arg)
2824 2828 {
2825 2829 (void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
2826 2830 (void) door_return(NULL, 0, NULL, 0);
2827 2831
2828 2832 /*
2829 2833 * If there is an error in door_return(), it will return here and
2830 2834 * the thread will exit. Hence we need to decrement door_server_cnt.
2831 2835 */
2832 2836 (void) pthread_mutex_lock(&door_server_cnt_lock);
2833 2837 door_server_cnt--;
2834 2838 (void) pthread_mutex_unlock(&door_server_cnt_lock);
2835 2839 return (NULL);
2836 2840 }
2837 2841
2838 2842 /*
2839 2843 * Max number of door server threads for syslogd. Since door is used
2840 2844 * to check the health of syslogd, we don't need large number of
2841 2845 * server threads.
2842 2846 */
2843 2847 #define MAX_DOOR_SERVER_THR 3
2844 2848
2845 2849 /*
2846 2850 * Manage door server thread pool.
2847 2851 */
2848 2852 /*ARGSUSED*/
2849 2853 static void
2850 2854 door_server_pool(door_info_t *dip)
2851 2855 {
2852 2856 (void) pthread_mutex_lock(&door_server_cnt_lock);
2853 2857 if (door_server_cnt <= MAX_DOOR_SERVER_THR &&
2854 2858 pthread_create(NULL, &door_thr_attr, create_door_thr, NULL) == 0) {
2855 2859 door_server_cnt++;
2856 2860 (void) pthread_mutex_unlock(&door_server_cnt_lock);
2857 2861 return;
2858 2862 }
2859 2863
2860 2864 (void) pthread_mutex_unlock(&door_server_cnt_lock);
2861 2865 }
2862 2866
2863 2867 /*
2864 2868 * checkm4 - used to verify that the external utilities that
2865 2869 * syslogd depends on are where we expect them to be.
2866 2870 * Returns 0 if all utilities are found, > 0 if any are missing.
2867 2871 * Also logs errors so user knows what's missing
2868 2872 */
2869 2873 static int
2870 2874 checkm4(void)
2871 2875 {
2872 2876 int notfound = 0;
2873 2877 int saverrno;
2874 2878 pthread_t mythreadno;
2875 2879
2876 2880 if (Debug) {
2877 2881 mythreadno = pthread_self();
2878 2882 }
2879 2883
2880 2884 if (access("/usr/ccs/bin/m4", X_OK) < 0) {
2881 2885 saverrno = errno;
2882 2886 logerror("/usr/ccs/bin/m4");
2883 2887 DPRINT2(1, "checkm4(%u): /usr/ccs/bin/m4 - access "
2884 2888 "returned %d\n", mythreadno, saverrno);
2885 2889 notfound++;
2886 2890 }
2887 2891
2888 2892 return (notfound);
2889 2893 }
2890 2894
2891 2895 /*
2892 2896 * INIT -- Initialize syslogd from configuration table, start up
2893 2897 * input and logger threads. This routine is called only once.
2894 2898 */
2895 2899 static void
2896 2900 init(void)
2897 2901 {
2898 2902 struct utsname *up;
2899 2903 pthread_attr_t sys_attr, net_attr, log_attr, hnl_attr;
2900 2904 int nthread;
2901 2905 pthread_t mythreadno;
2902 2906
2903 2907 if (Debug) {
2904 2908 mythreadno = pthread_self();
2905 2909 }
2906 2910
2907 2911 DPRINT1(2, "init(%u): initializing\n", mythreadno);
2908 2912
2909 2913 /* hand-craft a host_list_t entry for our local host name */
2910 2914 if ((up = malloc(sizeof (struct utsname))) == NULL) {
2911 2915 MALLOC_FAIL_EXIT;
2912 2916 }
2913 2917 (void) uname(up);
2914 2918 LocalHostName.hl_cnt = 1;
2915 2919 if ((LocalHostName.hl_hosts = malloc(sizeof (char *))) == NULL) {
2916 2920 MALLOC_FAIL_EXIT;
2917 2921 }
2918 2922 if ((LocalHostName.hl_hosts[0] = strdup(up->nodename)) == NULL) {
2919 2923 free(LocalHostName.hl_hosts);
2920 2924 MALLOC_FAIL_EXIT;
2921 2925 }
2922 2926 free(up);
2923 2927 /* also hand craft one for use if name resolution fails */
2924 2928 NullHostName.hl_cnt = 1;
2925 2929 if ((NullHostName.hl_hosts = malloc(sizeof (char *))) == NULL) {
2926 2930 MALLOC_FAIL_EXIT;
2927 2931 }
2928 2932 if ((NullHostName.hl_hosts[0] = strdup("name lookup failed")) == NULL) {
2929 2933 MALLOC_FAIL_EXIT;
2930 2934 }
2931 2935
2932 2936 hnc_init(0);
2933 2937
2934 2938 /*
2935 2939 * Note that getnets will allocate network resources, but won't be
2936 2940 * binding UDP port. This is because, there could be a race
2937 2941 * condition between door. If we bind here, one syslogd could grab
2938 2942 * UDP port first, but later another syslogd could take over without
2939 2943 * getting UDP port but grab the door file. The 2nd syslogd could
2940 2944 * continue to run without listening network.
2941 2945 * bindnet() will be called after door was successfully opened.
2942 2946 */
2943 2947 getnets();
2944 2948
2945 2949 /*
2946 2950 * Start up configured theads
2947 2951 */
2948 2952 conf_init();
2949 2953
2950 2954 /*
2951 2955 * allocate thread stacks for the persistant threads
2952 2956 */
2953 2957 nthread = (turnoff == 0) ? 4 : 2;
2954 2958
2955 2959 if ((stack_ptr = alloc_stacks(nthread)) == NULL) {
2956 2960 logerror("alloc_stacks failed - fatal");
2957 2961 exit(1);
2958 2962 }
2959 2963
2960 2964 if (Debug) {
2961 2965 dumpstats(STDOUT_FILENO);
2962 2966 }
2963 2967
2964 2968 (void) dataq_init(&inputq); /* init the input queue */
2965 2969
2966 2970 if (pthread_attr_init(&sys_attr) != 0 ||
2967 2971 pthread_attr_init(&log_attr) != 0 ||
2968 2972 pthread_attr_init(&net_attr) != 0 ||
2969 2973 pthread_attr_init(&hnl_attr) != 0 ||
2970 2974 pthread_attr_init(&door_thr_attr) != 0) {
2971 2975 logerror("pthread_attr_init failed - fatal");
2972 2976 exit(1);
2973 2977 }
2974 2978
2975 2979 (void) pthread_attr_setscope(&sys_attr, PTHREAD_SCOPE_PROCESS);
2976 2980 (void) pthread_attr_setscope(&log_attr, PTHREAD_SCOPE_PROCESS);
2977 2981 (void) pthread_attr_setscope(&net_attr, PTHREAD_SCOPE_PROCESS);
2978 2982 (void) pthread_attr_setscope(&hnl_attr, PTHREAD_SCOPE_PROCESS);
2979 2983 (void) pthread_attr_setscope(&door_thr_attr, PTHREAD_SCOPE_SYSTEM);
2980 2984 (void) pthread_attr_setdetachstate(&door_thr_attr,
2981 2985 PTHREAD_CREATE_DETACHED);
2982 2986
2983 2987 /* 1: logmsg thread */
2984 2988 (void) pthread_attr_setstacksize(&log_attr, stacksize);
2985 2989 (void) pthread_attr_setstackaddr(&log_attr, stack_ptr);
2986 2990 stack_ptr += stacksize + redzonesize;
2987 2991 if (pthread_create(&log_thread, &log_attr, logmsg, NULL) != 0) {
2988 2992 logerror("pthread_create failed - fatal");
2989 2993 exit(1);
2990 2994 }
2991 2995
2992 2996 /*
2993 2997 * open the log device, and pull up all pending message
2994 2998 * from the log driver.
2995 2999 */
2996 3000 prepare_sys_poll();
2997 3001
2998 3002 /*
2999 3003 * Now we can deliver the pending internal error messages.
3000 3004 */
3001 3005 enable_errorlog();
3002 3006
3003 3007 /* 2: sys_poll thread */
3004 3008 (void) pthread_attr_setstacksize(&sys_attr, stacksize);
3005 3009 (void) pthread_attr_setstackaddr(&sys_attr, stack_ptr);
3006 3010 stack_ptr += stacksize + redzonesize;
3007 3011 if (pthread_create(&sys_thread, &sys_attr, sys_poll, NULL) != 0) {
3008 3012 logerror("pthread_create failed - fatal");
3009 3013 exit(1);
3010 3014 }
3011 3015
3012 3016 /*
3013 3017 * We've started the sys_poll() and logmsg() threads. Now we are ready
3014 3018 * to open the door. This cannot happen before spawning sys_poll(),
3015 3019 * because after opening the door, syslog() will no longer take care of
3016 3020 * LOG_CONS. Therefor, we should pull up all pending log messages and
3017 3021 * activate sys_poll() before opening the door, so that log driver
3018 3022 * won't drop messages.
3019 3023 */
3020 3024 open_door();
3021 3025
3022 3026 DPRINT1(1, "init(%u): accepting messages from local system\n",
3023 3027 mythreadno);
3024 3028
3025 3029 if (turnoff == 0) {
3026 3030 /* init the hostname lookup queue */
3027 3031 (void) dataq_init(&hnlq);
3028 3032
3029 3033 /* 3: hostname lookup thread */
3030 3034 (void) pthread_attr_setstacksize(&hnl_attr, stacksize);
3031 3035 (void) pthread_attr_setstackaddr(&hnl_attr, stack_ptr);
3032 3036 stack_ptr += stacksize + redzonesize;
3033 3037 if (pthread_create(&hnl_thread, &hnl_attr,
3034 3038 hostname_lookup, NULL) != 0) {
3035 3039 logerror("pthread_create failed - fatal");
3036 3040 exit(1);
3037 3041 }
3038 3042
3039 3043 /* 4: net_poll thread */
3040 3044 (void) pthread_attr_setstacksize(&net_attr, stacksize);
3041 3045 (void) pthread_attr_setstackaddr(&net_attr, stack_ptr);
3042 3046 stack_ptr += stacksize + redzonesize;
3043 3047
3044 3048 /* grab UDP port */
3045 3049 bindnet();
3046 3050
3047 3051 if (pthread_create(&net_thread, &net_attr, net_poll,
3048 3052 NULL) != 0) {
3049 3053 logerror("pthread_create failed - fatal");
3050 3054 exit(1);
3051 3055 }
3052 3056 DPRINT1(1, "init(%u): accepting messages from remote\n",
3053 3057 mythreadno);
3054 3058 }
3055 3059
3056 3060 (void) pthread_attr_destroy(&sys_attr);
3057 3061 (void) pthread_attr_destroy(&net_attr);
3058 3062 (void) pthread_attr_destroy(&log_attr);
3059 3063 (void) pthread_attr_destroy(&hnl_attr);
3060 3064
3061 3065 curalarm = MarkInterval * 60 / MARKCOUNT;
3062 3066 (void) alarm((unsigned)curalarm);
3063 3067 DPRINT2(2, "init(%u): Next alarm in %d seconds\n",
3064 3068 mythreadno, curalarm);
3065 3069 DPRINT1(1, "init(%u): syslogd: started\n", mythreadno);
3066 3070 }
3067 3071
3068 3072 /*
3069 3073 * will print a bunch of debugging stats on 'fd'
3070 3074 */
3071 3075 static void
3072 3076 dumpstats(int fd)
3073 3077 {
3074 3078 FILE *out;
3075 3079 struct filed *f;
3076 3080 int i;
3077 3081 char users[1024];
3078 3082 char cbuf[30];
3079 3083 char *dashes = "------------------------";
3080 3084 static int conversion_printed;
3081 3085
3082 3086 if ((out = fdopen(fd, "w+")) == NULL)
3083 3087 return;
3084 3088
3085 3089 (void) fprintf(out, "\nSyslogd started: %s",
3086 3090 ctime_r(&start_time, cbuf));
3087 3091 (void) fprintf(out, "Input message count: system %d, network %d\n",
3088 3092 sys_msg_count, net_msg_count);
3089 3093 (void) fprintf(out, "# Outputs: %d\n\n", nlogs);
3090 3094
3091 3095 (void) fprintf(out, "%s priority = [file, facility] %s\n\n",
3092 3096 dashes, dashes);
3093 3097
3094 3098 for (i = 0; i < LOG_NFACILITIES + 1; i++) {
3095 3099 (void) fprintf(out, "%d ", i / 10);
3096 3100 }
3097 3101 (void) fprintf(out, "\n");
3098 3102 for (i = 0; i < LOG_NFACILITIES + 1; i++) {
3099 3103 (void) fprintf(out, "%d ", i % 10);
3100 3104 }
3101 3105 (void) fprintf(out, "\n");
3102 3106 for (i = 0; i < LOG_NFACILITIES + 1; i++) {
3103 3107 (void) fprintf(out, "--");
3104 3108 }
3105 3109 (void) fprintf(out, "\n");
3106 3110
3107 3111 for (f = Files; f < &Files[nlogs]; f++) {
3108 3112 for (i = 0; i < LOG_NFACILITIES + 1; i++) {
3109 3113 if (f->f_pmask[i] == NOPRI)
3110 3114 (void) fprintf(out, "X ");
3111 3115 else
3112 3116 (void) fprintf(out, "%d ",
3113 3117 f->f_pmask[i]);
3114 3118 }
3115 3119 (void) fprintf(out, "%s: ", TypeNames[f->f_type]);
3116 3120 switch (f->f_type) {
3117 3121 case F_FILE:
3118 3122 case F_TTY:
3119 3123 case F_CONSOLE:
3120 3124 (void) fprintf(out, "%s", f->f_un.f_fname);
3121 3125 break;
3122 3126 case F_FORW:
3123 3127 (void) fprintf(out, "%s", f->f_un.f_forw.f_hname);
3124 3128 break;
3125 3129 case F_USERS:
3126 3130 for (i = 0; i < MAXUNAMES &&
3127 3131 *f->f_un.f_uname[i]; i++) {
3128 3132 if (!i)
3129 3133 (void) fprintf(out, "%s",
3130 3134 f->f_un.f_uname[i]);
3131 3135 else
3132 3136 (void) fprintf(out, ", %s",
3133 3137 f->f_un.f_uname[i]);
3134 3138 }
3135 3139 break;
3136 3140 }
3137 3141 (void) fprintf(out, "\n");
3138 3142 }
3139 3143
3140 3144 if (!conversion_printed) {
3141 3145 (void) fprintf(out, "\nFacilities:\n");
3142 3146
3143 3147 for (i = 0; FacNames[i].c_val != -1; i++) {
3144 3148 (void) fprintf(out, " [%02d] %s: %3d\n", i,
3145 3149 FacNames[i].c_name, FacNames[i].c_val);
3146 3150 }
3147 3151
3148 3152 (void) fprintf(out, "\nPriorities:\n");
3149 3153
3150 3154 for (i = 0; PriNames[i].c_val != -1; i++) {
3151 3155 (void) fprintf(out, " [%02d] %s: %3d\n", i,
3152 3156 PriNames[i].c_name, PriNames[i].c_val);
3153 3157 }
3154 3158
3155 3159 conversion_printed = 1;
3156 3160 }
3157 3161
3158 3162 (void) fprintf(out, "\n\n\n\t\tPer File Statistics\n");
3159 3163 (void) fprintf(out, "%-24s\tTot\tDups\tNofwd\tErrs\n", "File");
3160 3164 (void) fprintf(out, "%-24s\t---\t----\t-----\t----\n", "----");
3161 3165 for (f = Files; f < &Files[nlogs]; f++) {
3162 3166 switch (f->f_type) {
3163 3167 case F_FILE:
3164 3168 case F_TTY:
3165 3169 case F_CONSOLE:
3166 3170 (void) fprintf(out, "%-24s", f->f_un.f_fname);
3167 3171 break;
3168 3172 case F_WALL:
3169 3173 (void) fprintf(out, "%-24s", TypeNames[f->f_type]);
3170 3174 break;
3171 3175 case F_FORW:
3172 3176 (void) fprintf(out, "%-24s", f->f_un.f_forw.f_hname);
3173 3177 break;
3174 3178 case F_USERS:
3175 3179 for (i = 0; i < MAXUNAMES &&
3176 3180 *f->f_un.f_uname[i]; i++) {
3177 3181 if (!i)
3178 3182 (void) strcpy(users,
3179 3183 f->f_un.f_uname[i]);
3180 3184 else {
3181 3185 (void) strcat(users, ",");
3182 3186 (void) strcat(users,
3183 3187 f->f_un.f_uname[i]);
3184 3188 }
3185 3189 }
3186 3190 (void) fprintf(out, "%-24s", users);
3187 3191 break;
3188 3192 }
3189 3193 (void) fprintf(out, "\t%d\t%d\t%d\t%d\n",
3190 3194 f->f_stat.total, f->f_stat.dups,
3191 3195 f->f_stat.cantfwd, f->f_stat.errs);
3192 3196 }
3193 3197 (void) fprintf(out, "\n\n");
3194 3198 if (Debug && fd == 1)
3195 3199 return;
3196 3200 (void) fclose(out);
3197 3201 }
3198 3202
3199 3203 /*
3200 3204 * conf_init - This routine is code seperated from the
3201 3205 * init routine in order to be re-callable when we get
3202 3206 * a SIGHUP signal.
3203 3207 */
3204 3208 static void
3205 3209 conf_init(void)
3206 3210 {
3207 3211 char *p;
3208 3212 int i;
3209 3213 struct filed *f;
3210 3214 char *m4argv[4];
3211 3215 int m4argc = 0;
3212 3216 conf_t cf;
3213 3217 pthread_t mythreadno;
3214 3218
3215 3219 if (Debug) {
3216 3220 mythreadno = pthread_self();
3217 3221 }
3218 3222
3219 3223 DPRINT1(2, "conf_init(%u): starting logger threads\n",
3220 3224 mythreadno);
3221 3225
3222 3226 m4argv[m4argc++] = "m4";
3223 3227
3224 3228 if (amiloghost() == 1) {
3225 3229 DPRINT1(1, "conf_init(%u): I am loghost\n", mythreadno);
3226 3230 m4argv[m4argc++] = "-DLOGHOST=1";
3227 3231 }
3228 3232
3229 3233 m4argv[m4argc++] = ConfFile;
3230 3234 m4argv[m4argc] = NULL;
3231 3235
3232 3236 /*
3233 3237 * Make sure the configuration file and m4 exist, and then parse
3234 3238 * the configuration file with m4. If any of these fail, resort
3235 3239 * to our hardcoded fallback configuration.
3236 3240 */
3237 3241
3238 3242 if (access(ConfFile, R_OK) == -1) {
3239 3243 DPRINT2(1, "conf_init(%u): %s does not exist\n", mythreadno,
3240 3244 ConfFile);
3241 3245 logerror("can't open configuration file");
3242 3246 /* CSTYLED */
3243 3247 Files = (struct filed *) &fallback; /*lint !e545 */
3244 3248 cfline("*.ERR\t/dev/sysmsg", 0, &Files[0]);
3245 3249 cfline("*.PANIC\t*", 0, &Files[1]);
3246 3250 nlogs = 2;
3247 3251 goto nofile;
3248 3252 }
3249 3253
3250 3254 if (checkm4() != 0 || conf_open(&cf, "/usr/ccs/bin/m4", m4argv) == -1) {
3251 3255 DPRINT2(1, "conf_init(%u): cannot open %s\n", mythreadno,
3252 3256 ConfFile);
3253 3257 /* CSTYLED */
3254 3258 Files = (struct filed *) &fallback; /*lint !e545 */
3255 3259 cfline("*.ERR\t/dev/sysmsg", 0, &Files[0]);
3256 3260 cfline("*.PANIC\t*", 0, &Files[1]);
3257 3261 nlogs = 2;
3258 3262 goto nofile;
3259 3263 }
3260 3264
3261 3265 /* Count the number of lines which are not blanks or comments */
3262 3266 nlogs = 0;
3263 3267 while ((p = conf_read(&cf)) != NULL) {
3264 3268 if (p[0] != '\0' && p[0] != '#')
3265 3269 nlogs++;
3266 3270 }
3267 3271
3268 3272 Files = (struct filed *)malloc(sizeof (struct filed) * nlogs);
3269 3273
3270 3274 if (!Files) {
3271 3275 DPRINT1(1, "conf_init(%u): malloc failed - can't "
3272 3276 "allocate 'Files' array\n", mythreadno);
3273 3277 MALLOC_FAIL("loading minimum configuration");
3274 3278 /* CSTYLED */
3275 3279 Files = (struct filed *) &fallback; /*lint !e545 */
3276 3280 cfline("*.ERR\t/dev/sysmsg", 0, &Files[0]);
3277 3281 cfline("*.PANIC\t*", 0, &Files[1]);
3278 3282 nlogs = 2;
3279 3283 conf_close(&cf);
3280 3284 goto nofile;
3281 3285 }
3282 3286
3283 3287 /*
3284 3288 * Foreach line in the conf table, open that file.
3285 3289 */
3286 3290 conf_rewind(&cf);
3287 3291 f = Files;
3288 3292 i = 0;
3289 3293 while (((p = conf_read(&cf)) != NULL) && (f < &Files[nlogs])) {
3290 3294 i++;
3291 3295 /* check for end-of-section */
3292 3296 if (p[0] == '\0' || p[0] == '#')
3293 3297 continue;
3294 3298
3295 3299 cfline(p, i, f);
3296 3300 if (f->f_type == F_UNUSED)
3297 3301 nlogs--;
3298 3302 else
3299 3303 f++;
3300 3304 }
3301 3305
3302 3306 conf_close(&cf);
3303 3307
3304 3308 /*
3305 3309 * See if marks are to be written to any files. If so, set up a
3306 3310 * timeout for marks.
3307 3311 */
3308 3312 nofile:
3309 3313 Marking = 0;
3310 3314
3311 3315 /*
3312 3316 * allocate thread stacks - one for each logger thread.
3313 3317 */
3314 3318 if ((cstack_ptr = alloc_stacks(nlogs)) == NULL) {
3315 3319 logerror("alloc_stacks failed - fatal");
3316 3320 exit(1);
3317 3321 }
3318 3322
3319 3323 /* And now one thread for each configured file */
3320 3324 for (f = Files; f < &Files[nlogs]; f++) {
3321 3325 if (filed_init(f) != 0) {
3322 3326 logerror("pthread_create failed - fatal");
3323 3327 exit(1);
3324 3328 }
3325 3329
3326 3330 (void) pthread_mutex_lock(&cft);
3327 3331 ++conf_threads;
3328 3332 (void) pthread_mutex_unlock(&cft);
3329 3333
3330 3334 if (f->f_type != F_UNUSED &&
3331 3335 f->f_pmask[LOG_NFACILITIES] != NOPRI)
3332 3336 Marking = 1;
3333 3337 }
3334 3338 }
3335 3339
3336 3340 /*
3337 3341 * filed init - initialize fields in a file descriptor struct
3338 3342 * this is called before multiple threads are running, so no mutex
3339 3343 * needs to be held at this time.
3340 3344 */
3341 3345 static int
3342 3346 filed_init(struct filed *f)
3343 3347 {
3344 3348 pthread_attr_t stack_attr;
3345 3349 pthread_t mythreadno;
3346 3350
3347 3351 if (Debug) {
3348 3352 mythreadno = pthread_self();
3349 3353 }
3350 3354
3351 3355 if (pthread_mutex_init(&f->filed_mutex, NULL) != 0) {
3352 3356 logerror("pthread_mutex_init failed");
3353 3357 return (-1);
3354 3358 }
3355 3359
3356 3360 DPRINT2(5, "filed_init(%u): dataq_init for queue %p\n",
3357 3361 mythreadno, (void *)&f->f_queue);
3358 3362 (void) dataq_init(&f->f_queue);
3359 3363
3360 3364 if (pthread_attr_init(&stack_attr) != 0) {
3361 3365 logerror("pthread_attr_init failed");
3362 3366 return (-1);
3363 3367 }
3364 3368
3365 3369 (void) pthread_attr_setstacksize(&stack_attr, stacksize);
3366 3370 (void) pthread_attr_setstackaddr(&stack_attr, cstack_ptr);
3367 3371 cstack_ptr += stacksize + redzonesize;
3368 3372
3369 3373 f->f_msgflag = 0;
3370 3374 f->f_prevmsg.msg[0] = '\0';
3371 3375 f->f_prevmsg.flags = 0;
3372 3376 f->f_prevmsg.pri = 0;
3373 3377 f->f_prevmsg.host[0] = '\0';
3374 3378
3375 3379 f->f_current.msg[0] = '\0';
3376 3380 f->f_current.flags = 0;
3377 3381 f->f_current.pri = 0;
3378 3382 f->f_current.host[0] = '\0';
3379 3383
3380 3384 f->f_prevcount = 0;
3381 3385
3382 3386 f->f_stat.flag = 0;
3383 3387 f->f_stat.total = 0;
3384 3388 f->f_stat.dups = 0;
3385 3389 f->f_stat.cantfwd = 0;
3386 3390 f->f_stat.errs = 0;
3387 3391
3388 3392 if (pthread_create(&f->f_thread, NULL, logit, (void *)f) != 0) {
3389 3393 logerror("pthread_create failed");
3390 3394 (void) pthread_attr_destroy(&stack_attr);
3391 3395 return (-1);
3392 3396 }
3393 3397
3394 3398 (void) pthread_attr_destroy(&stack_attr);
3395 3399 return (0);
3396 3400 }
3397 3401
3398 3402
3399 3403 /*
3400 3404 * Crack a configuration file line
3401 3405 */
3402 3406 static void
3403 3407 cfline(char *line, int lineno, struct filed *f)
3404 3408 {
3405 3409 char *p;
3406 3410 char *q;
3407 3411 int i;
3408 3412 char *bp;
3409 3413 int pri;
3410 3414 char buf[MAXLINE];
3411 3415 char ebuf[SYS_NMLN+1+40];
3412 3416 mode_t fmode, omode = O_WRONLY|O_APPEND|O_NOCTTY;
3413 3417 struct stat64 sbuf;
3414 3418 pthread_t mythreadno;
3415 3419
3416 3420 if (Debug) {
3417 3421 mythreadno = pthread_self();
3418 3422 }
3419 3423
3420 3424 DPRINT2(1, "cfline(%u): (%s)\n", mythreadno, line);
3421 3425
3422 3426 errno = 0; /* keep errno related stuff out of logerror messages */
3423 3427
3424 3428 /* clear out file entry */
3425 3429 bzero((char *)f, sizeof (*f));
3426 3430 for (i = 0; i <= LOG_NFACILITIES; i++)
3427 3431 f->f_pmask[i] = NOPRI;
3428 3432
3429 3433 /* scan through the list of selectors */
3430 3434 for (p = line; *p && *p != '\t'; ) {
3431 3435
3432 3436 /* find the end of this facility name list */
3433 3437 for (q = p; *q && *q != '\t' && *q++ != '.'; )
3434 3438 continue;
3435 3439
3436 3440 /* collect priority name */
3437 3441 for (bp = buf; *q && !strchr("\t,;", *q); )
3438 3442 *bp++ = *q++;
3439 3443 *bp = '\0';
3440 3444
3441 3445 /* skip cruft */
3442 3446 while (strchr(", ;", *q))
3443 3447 q++;
3444 3448
3445 3449 /* decode priority name */
3446 3450 pri = decode(buf, PriNames);
3447 3451 if (pri < 0) {
3448 3452 logerror("line %d: unknown priority name \"%s\"",
3449 3453 lineno, buf);
3450 3454 return;
3451 3455 }
3452 3456
3453 3457 /* scan facilities */
3454 3458 while (*p && !strchr("\t.;", *p)) {
3455 3459 for (bp = buf; *p && !strchr("\t,;.", *p); )
3456 3460 *bp++ = *p++;
3457 3461 *bp = '\0';
3458 3462 if (*buf == '*')
3459 3463 for (i = 0; i < LOG_NFACILITIES; i++)
3460 3464 f->f_pmask[i] = (uchar_t)pri;
3461 3465 else {
3462 3466 i = decode(buf, FacNames);
3463 3467 if (i < 0) {
3464 3468 logerror("line %d: unknown facility"
3465 3469 " name \"%s\"", lineno, buf);
3466 3470 return;
3467 3471 }
3468 3472 f->f_pmask[i >> 3] = (uchar_t)pri;
3469 3473 }
3470 3474 while (*p == ',' || *p == ' ')
3471 3475 p++;
3472 3476 }
3473 3477
3474 3478 p = q;
3475 3479 }
3476 3480
3477 3481 /* skip to action part */
3478 3482 while (*p == '\t' || *p == ' ')
3479 3483 p++;
3480 3484
3481 3485 switch (*p) {
3482 3486 case '\0':
3483 3487 errno = 0;
3484 3488 logerror("line %d: no action part", lineno);
3485 3489 break;
3486 3490
3487 3491 case '@':
3488 3492 (void) strlcpy(f->f_un.f_forw.f_hname, ++p, SYS_NMLN);
3489 3493 if (logforward(f, ebuf, sizeof (ebuf)) != 0) {
3490 3494 logerror("line %d: %s", lineno, ebuf);
3491 3495 break;
3492 3496 }
3493 3497 f->f_type = F_FORW;
3494 3498 break;
3495 3499
3496 3500 case '/':
3497 3501 (void) strlcpy(f->f_un.f_fname, p, MAXPATHLEN);
3498 3502 if (stat64(p, &sbuf) < 0) {
3499 3503 logerror(p);
3500 3504 break;
3501 3505 }
3502 3506 /*
3503 3507 * don't block trying to open a pipe
3504 3508 * with no reader on the other end
3505 3509 */
3506 3510 fmode = 0; /* reset each pass */
3507 3511 if (S_ISFIFO(sbuf.st_mode))
3508 3512 fmode = O_NONBLOCK;
3509 3513
3510 3514 f->f_file = open64(p, omode|fmode);
3511 3515 if (f->f_file < 0) {
3512 3516 if (fmode && errno == ENXIO) {
3513 3517 errno = 0;
3514 3518 logerror("%s - no reader", p);
3515 3519 } else
3516 3520 logerror(p);
3517 3521 break;
3518 3522 }
3519 3523
3520 3524 /*
3521 3525 * Fifos are initially opened NONBLOCK
3522 3526 * to insure we don't hang, but once
3523 3527 * we are open, we need to change the
3524 3528 * behavior back to blocking, otherwise
3525 3529 * we may get write errors, and the log
3526 3530 * will get closed down the line.
3527 3531 */
3528 3532 if (S_ISFIFO(sbuf.st_mode))
3529 3533 (void) fcntl(f->f_file, F_SETFL, omode);
3530 3534
3531 3535 if (isatty(f->f_file)) {
3532 3536 f->f_type = F_TTY;
3533 3537 untty();
3534 3538 } else
3535 3539 f->f_type = F_FILE;
3536 3540
3537 3541 if ((strcmp(p, ctty) == 0) || (strcmp(p, sysmsg) == 0))
3538 3542 f->f_type = F_CONSOLE;
3539 3543 break;
3540 3544
3541 3545 case '*':
3542 3546 f->f_type = F_WALL;
3543 3547 break;
3544 3548
3545 3549 default:
3546 3550 for (i = 0; i < MAXUNAMES && *p; i++) {
3547 3551 for (q = p; *q && *q != ','; )
3548 3552 q++;
3549 3553 (void) strlcpy(f->f_un.f_uname[i], p, UNAMESZ);
3550 3554 if ((q - p) > UNAMESZ)
3551 3555 f->f_un.f_uname[i][UNAMESZ] = '\0';
3552 3556 else
3553 3557 f->f_un.f_uname[i][q - p] = '\0';
3554 3558 while (*q == ',' || *q == ' ')
3555 3559 q++;
3556 3560 p = q;
3557 3561 }
3558 3562 f->f_type = F_USERS;
3559 3563 break;
3560 3564 }
3561 3565 f->f_orig_type = f->f_type;
3562 3566 }
3563 3567
3564 3568
3565 3569 /*
3566 3570 * Decode a symbolic name to a numeric value
3567 3571 */
3568 3572 static int
3569 3573 decode(char *name, struct code *codetab)
3570 3574 {
3571 3575 struct code *c;
3572 3576 char *p;
3573 3577 char buf[40];
3574 3578
3575 3579 if (isdigit(*name))
3576 3580 return (atoi(name));
3577 3581
3578 3582 (void) strncpy(buf, name, sizeof (buf) - 1);
3579 3583 for (p = buf; *p; p++)
3580 3584 if (isupper(*p))
3581 3585 *p = tolower(*p);
3582 3586 for (c = codetab; c->c_name; c++)
3583 3587 if (!(strcmp(buf, c->c_name)))
3584 3588 return (c->c_val);
3585 3589
3586 3590 return (-1);
3587 3591 }
3588 3592
3589 3593 static int
3590 3594 ismyaddr(struct netbuf *nbp)
3591 3595 {
3592 3596 int i;
3593 3597
3594 3598 if (nbp == NULL)
3595 3599 return (0);
3596 3600
3597 3601 for (i = 1; i < Ninputs; i++) {
3598 3602 if (same_addr(nbp, Myaddrs[i]))
3599 3603 return (1);
3600 3604 }
3601 3605 return (0);
3602 3606 }
3603 3607
3604 3608 static void
3605 3609 getnets(void)
3606 3610 {
3607 3611 struct nd_hostserv hs;
3608 3612 struct netconfig *ncp;
3609 3613 struct nd_addrlist *nap;
3610 3614 struct netbuf *nbp;
3611 3615 int i, inputs;
3612 3616 void *handle;
3613 3617 char *uap;
3614 3618 pthread_t mythreadno;
3615 3619
3616 3620 if (Debug) {
3617 3621 mythreadno = pthread_self();
3618 3622 }
3619 3623
3620 3624 if (turnoff) {
3621 3625 DPRINT1(1, "getnets(%u): network is being turned off\n",
3622 3626 mythreadno);
3623 3627 return;
3624 3628 }
3625 3629
3626 3630 hs.h_host = HOST_SELF;
3627 3631 hs.h_serv = "syslog";
3628 3632
3629 3633 if ((handle = setnetconfig()) == NULL) {
3630 3634 return;
3631 3635 }
3632 3636
3633 3637 while ((ncp = getnetconfig(handle)) != NULL) {
3634 3638 if (ncp->nc_semantics != NC_TPI_CLTS) {
3635 3639 continue;
3636 3640 }
3637 3641
3638 3642 if (netdir_getbyname(ncp, &hs, &nap) != 0) {
3639 3643 continue;
3640 3644 }
3641 3645
3642 3646 if (nap == NULL || nap->n_cnt <= 0) {
3643 3647 DPRINT1(1, "getnets(%u): found no address\n",
3644 3648 mythreadno);
3645 3649 netdir_free((void *)nap, ND_ADDRLIST);
3646 3650 continue;
3647 3651 }
3648 3652
3649 3653 if (Debug) {
3650 3654 DPRINT2(1, "getnets(%u): found %d addresses",
3651 3655 mythreadno, nap->n_cnt);
3652 3656 DPRINT0(1, ", they are: ");
3653 3657 nbp = nap->n_addrs;
3654 3658
3655 3659 for (i = 0; i < nap->n_cnt; i++) {
3656 3660 if ((uap = taddr2uaddr(ncp, nbp)) != NULL) {
3657 3661 DPRINT1(1, "%s ", uap);
3658 3662 free(uap);
3659 3663 }
3660 3664 nbp++;
3661 3665 }
3662 3666
3663 3667 DPRINT0(1, "\n");
3664 3668 }
3665 3669
3666 3670 inputs = Ninputs + nap->n_cnt;
3667 3671
3668 3672 Nfd = realloc(Nfd, inputs * sizeof (struct pollfd));
3669 3673 Ncf = realloc(Ncf, inputs * sizeof (struct netconfig));
3670 3674 Myaddrs = realloc(Myaddrs, inputs * sizeof (struct netbuf *));
3671 3675 Udp = realloc(Udp, inputs * sizeof (struct t_unitdata *));
3672 3676 Errp = realloc(Errp, inputs * sizeof (struct t_uderr *));
3673 3677
3674 3678 /*
3675 3679 * all malloc failures here are fatal
3676 3680 */
3677 3681 if (Nfd == NULL || Ncf == NULL || Myaddrs == NULL ||
3678 3682 Udp == NULL || Errp == NULL) {
3679 3683 MALLOC_FAIL_EXIT;
3680 3684 }
3681 3685
3682 3686 nbp = nap->n_addrs;
3683 3687
3684 3688 for (i = 0; i < nap->n_cnt; i++, nbp++) {
3685 3689 char ebuf[128];
3686 3690
3687 3691 if (addnet(ncp, nbp) == 0) {
3688 3692 /* no error */
3689 3693 continue;
3690 3694 }
3691 3695
3692 3696 (void) strcpy(ebuf, "Unable to configure syslog port");
3693 3697
3694 3698 if ((uap = taddr2uaddr(ncp, nbp)) != NULL) {
3695 3699 size_t l = strlen(ebuf);
3696 3700 (void) snprintf(ebuf + l, sizeof (ebuf) - l,
3697 3701 " for %s", uap);
3698 3702 }
3699 3703
3700 3704 DPRINT2(1, "getnets(%u): %s",
3701 3705 mythreadno, ebuf);
3702 3706
3703 3707 if (uap) {
3704 3708 free(uap);
3705 3709 }
3706 3710
3707 3711 logerror(ebuf);
3708 3712 /*
3709 3713 * Here maybe syslogd can quit. However, syslogd
3710 3714 * has been ignoring this error and keep running.
3711 3715 * So we won't break it.
3712 3716 */
3713 3717 }
3714 3718
3715 3719 netdir_free((void *)nap, ND_ADDRLIST);
3716 3720 }
3717 3721
3718 3722 (void) endnetconfig(handle);
3719 3723 }
3720 3724
3721 3725 /*
3722 3726 * Open the network device, and allocate necessary resources.
3723 3727 * Myaddrs will also be filled, so that we can call ismyaddr() before
3724 3728 * being bound to the network.
3725 3729 */
3726 3730 static int
3727 3731 addnet(struct netconfig *ncp, struct netbuf *nbp)
3728 3732 {
3729 3733 int fd;
3730 3734 struct netbuf *bp;
3731 3735
3732 3736 fd = t_open(ncp->nc_device, O_RDWR, NULL);
3733 3737
3734 3738 if (fd < 0) {
3735 3739 return (1);
3736 3740 }
3737 3741
3738 3742 (void) memcpy(&Ncf[Ninputs], ncp, sizeof (struct netconfig));
3739 3743
3740 3744 /*LINTED*/
3741 3745 Udp[Ninputs] = (struct t_unitdata *)t_alloc(fd, T_UNITDATA, T_ADDR);
3742 3746
3743 3747 if (Udp[Ninputs] == NULL) {
3744 3748 (void) t_close(fd);
3745 3749 return (1);
3746 3750 }
3747 3751
3748 3752 /*LINTED*/
3749 3753 Errp[Ninputs] = (struct t_uderr *)t_alloc(fd, T_UDERROR, T_ADDR);
3750 3754
3751 3755 if (Errp[Ninputs] == NULL) {
3752 3756 (void) t_close(fd);
3753 3757 (void) t_free((char *)Udp[Ninputs], T_UNITDATA);
3754 3758 return (1);
3755 3759 }
3756 3760
3757 3761 if ((bp = malloc(sizeof (struct netbuf))) == NULL ||
3758 3762 (bp->buf = malloc(nbp->len)) == NULL) {
3759 3763 MALLOC_FAIL("allocating address buffer");
3760 3764 (void) t_close(fd);
3761 3765 (void) t_free((char *)Udp[Ninputs], T_UNITDATA);
3762 3766 (void) t_free((char *)Errp[Ninputs], T_UDERROR);
3763 3767
3764 3768 if (bp) {
3765 3769 free(bp);
3766 3770 }
3767 3771
3768 3772 return (1);
3769 3773 }
3770 3774
3771 3775 bp->len = nbp->len;
3772 3776 (void) memcpy(bp->buf, nbp->buf, nbp->len);
3773 3777 Myaddrs[Ninputs] = bp;
3774 3778
3775 3779 Nfd[Ninputs].fd = fd;
3776 3780 Nfd[Ninputs].events = POLLIN;
3777 3781 Ninputs++;
3778 3782 return (0);
3779 3783 }
3780 3784
3781 3785 /*
3782 3786 * Allocate UDP buffer to minimize packet loss.
3783 3787 */
3784 3788 static void
3785 3789 set_udp_buffer(int fd)
3786 3790 {
3787 3791 struct t_optmgmt req, resp;
3788 3792 struct opthdr *opt;
3789 3793 size_t optsize, bsize = 256 * 1024;
3790 3794 pthread_t mythreadno;
3791 3795
3792 3796 if (Debug) {
3793 3797 mythreadno = pthread_self();
3794 3798 }
3795 3799
3796 3800 optsize = sizeof (struct opthdr) + sizeof (int);
3797 3801 if ((opt = malloc(optsize)) == NULL) {
3798 3802 MALLOC_FAIL("will have no udp buffer");
3799 3803 return;
3800 3804 }
3801 3805 opt->level = SOL_SOCKET;
3802 3806 opt->name = SO_RCVBUF;
3803 3807 opt->len = sizeof (int);
3804 3808 *(int *)(opt + 1) = bsize;
3805 3809
3806 3810 req.flags = T_NEGOTIATE;
3807 3811 req.opt.len = optsize;
3808 3812 req.opt.buf = (char *)opt;
3809 3813
3810 3814 resp.flags = 0;
3811 3815 resp.opt.maxlen = optsize;
3812 3816 resp.opt.buf = (char *)opt;
3813 3817
3814 3818 while (t_optmgmt(fd, &req, &resp) == -1 || resp.flags != T_SUCCESS) {
3815 3819 if (t_errno != TSYSERR || errno != ENOBUFS) {
3816 3820 bsize = 0;
3817 3821 break;
3818 3822 }
3819 3823 bsize >>= 1;
3820 3824 if (bsize < 8192) {
3821 3825 break;
3822 3826 }
3823 3827 *(int *)(opt + 1) = bsize;
3824 3828 }
3825 3829 if (bsize == 0) {
3826 3830 logerror("failed to allocate UDP buffer");
3827 3831 }
3828 3832 DPRINT3(1, "set_udp_buffer(%u): allocate %d for fd %d\n",
3829 3833 mythreadno, bsize, fd);
3830 3834 free(opt);
3831 3835 }
3832 3836
3833 3837 /*
3834 3838 * Attach the network, and allocate UDP buffer for the interface.
3835 3839 */
3836 3840 static void
3837 3841 bindnet(void)
3838 3842 {
3839 3843 struct t_bind bind, *bound;
3840 3844 int cnt, i;
3841 3845 char *uap;
3842 3846 pthread_t mythreadno;
3843 3847
3844 3848 if (Debug) {
3845 3849 mythreadno = pthread_self();
3846 3850 }
3847 3851
3848 3852 cnt = 0;
3849 3853
3850 3854 while (cnt < Ninputs) {
3851 3855 char ebuf[128];
3852 3856
3853 3857 /*LINTED*/
3854 3858 bound = (struct t_bind *)t_alloc(Nfd[cnt].fd, T_BIND, T_ADDR);
3855 3859 bind.addr = *Myaddrs[cnt];
3856 3860 bind.qlen = 0;
3857 3861
3858 3862 if (t_bind(Nfd[cnt].fd, &bind, bound) == 0) {
3859 3863 if (same_addr(&bind.addr, &bound->addr)) {
3860 3864 (void) t_free((char *)bound, T_BIND);
3861 3865 set_udp_buffer(Nfd[cnt].fd);
3862 3866 cnt++;
3863 3867 continue;
3864 3868 }
3865 3869 }
3866 3870
3867 3871 /* failed to bind port */
3868 3872 (void) t_free((char *)bound, T_BIND);
3869 3873
3870 3874 (void) strcpy(ebuf, "Unable to bind syslog port");
3871 3875
3872 3876 uap = taddr2uaddr(&Ncf[cnt], Myaddrs[cnt]);
3873 3877 if (uap) {
3874 3878 i = strlen(ebuf);
3875 3879 (void) snprintf(ebuf + i, sizeof (ebuf) - i,
3876 3880 " for %s", uap);
3877 3881 }
3878 3882
3879 3883 DPRINT2(1, "bindnet(%u): failed to bind port (%s)\n",
3880 3884 mythreadno, uap ? uap : "<unknown>");
3881 3885
3882 3886 if (uap) {
3883 3887 free(uap);
3884 3888 }
3885 3889
3886 3890 errno = 0;
3887 3891 logerror(ebuf);
3888 3892
3889 3893 (void) t_close(Nfd[cnt].fd);
3890 3894 free(Myaddrs[cnt]->buf);
3891 3895 free(Myaddrs[cnt]);
3892 3896 (void) t_free((char *)Udp[cnt], T_UNITDATA);
3893 3897 (void) t_free((char *)Errp[cnt], T_UDERROR);
3894 3898
3895 3899 for (i = cnt; i < (Ninputs-1); i++) {
3896 3900 Nfd[i] = Nfd[i + 1];
3897 3901 Ncf[i] = Ncf[i + 1];
3898 3902 Myaddrs[i] = Myaddrs[i + 1];
3899 3903 Udp[i] = Udp[i + 1];
3900 3904 Errp[i] = Errp[i + 1];
3901 3905 }
3902 3906
3903 3907 Ninputs--;
3904 3908 }
3905 3909 }
3906 3910
3907 3911 static int
3908 3912 logforward(struct filed *f, char *ebuf, size_t elen)
3909 3913 {
3910 3914 struct nd_hostserv hs;
3911 3915 struct netbuf *nbp;
3912 3916 struct netconfig *ncp;
3913 3917 struct nd_addrlist *nap;
3914 3918 void *handle;
3915 3919 char *hp;
3916 3920
3917 3921 hp = f->f_un.f_forw.f_hname;
3918 3922 hs.h_host = hp;
3919 3923 hs.h_serv = "syslog";
3920 3924
3921 3925 if ((handle = setnetconfig()) == NULL) {
3922 3926 (void) strlcpy(ebuf,
3923 3927 "unable to rewind the netconfig database", elen);
3924 3928 errno = 0;
3925 3929 return (-1);
3926 3930 }
3927 3931 nap = (struct nd_addrlist *)NULL;
3928 3932 while ((ncp = getnetconfig(handle)) != NULL) {
3929 3933 if (ncp->nc_semantics == NC_TPI_CLTS) {
3930 3934 if (netdir_getbyname(ncp, &hs, &nap) == 0) {
3931 3935 if (!nap)
3932 3936 continue;
3933 3937 nbp = nap->n_addrs;
3934 3938 break;
3935 3939 }
3936 3940 }
3937 3941 }
3938 3942 if (ncp == NULL) {
3939 3943 (void) endnetconfig(handle);
3940 3944 (void) snprintf(ebuf, elen,
3941 3945 "WARNING: %s could not be resolved", hp);
3942 3946 errno = 0;
3943 3947 return (-1);
3944 3948 }
3945 3949 if (nap == (struct nd_addrlist *)NULL) {
3946 3950 (void) endnetconfig(handle);
3947 3951 (void) snprintf(ebuf, elen, "unknown host %s", hp);
3948 3952 errno = 0;
3949 3953 return (-1);
3950 3954 }
3951 3955 /* CSTYLED */
3952 3956 if (ismyaddr(nbp)) { /*lint !e644 */
3953 3957 netdir_free((void *)nap, ND_ADDRLIST);
3954 3958 (void) endnetconfig(handle);
3955 3959 (void) snprintf(ebuf, elen,
3956 3960 "host %s is this host - logging loop", hp);
3957 3961 errno = 0;
3958 3962 return (-1);
3959 3963 }
3960 3964 f->f_un.f_forw.f_addr.buf = malloc(nbp->len);
3961 3965 if (f->f_un.f_forw.f_addr.buf == NULL) {
3962 3966 netdir_free((void *)nap, ND_ADDRLIST);
3963 3967 (void) endnetconfig(handle);
3964 3968 (void) strlcpy(ebuf, "malloc failed", elen);
3965 3969 return (-1);
3966 3970 }
3967 3971 bcopy(nbp->buf, f->f_un.f_forw.f_addr.buf, nbp->len);
3968 3972 f->f_un.f_forw.f_addr.len = nbp->len;
3969 3973 f->f_file = t_open(ncp->nc_device, O_RDWR, NULL);
3970 3974 if (f->f_file < 0) {
3971 3975 netdir_free((void *)nap, ND_ADDRLIST);
3972 3976 (void) endnetconfig(handle);
3973 3977 free(f->f_un.f_forw.f_addr.buf);
3974 3978 (void) strlcpy(ebuf, "t_open", elen);
3975 3979 return (-1);
3976 3980 }
3977 3981 netdir_free((void *)nap, ND_ADDRLIST);
3978 3982 (void) endnetconfig(handle);
3979 3983 if (t_bind(f->f_file, NULL, NULL) < 0) {
3980 3984 (void) strlcpy(ebuf, "t_bind", elen);
3981 3985 free(f->f_un.f_forw.f_addr.buf);
3982 3986 (void) t_close(f->f_file);
3983 3987 return (-1);
3984 3988 }
3985 3989 return (0);
3986 3990 }
3987 3991
3988 3992 static int
3989 3993 amiloghost(void)
3990 3994 {
3991 3995 struct nd_hostserv hs;
3992 3996 struct netconfig *ncp;
3993 3997 struct nd_addrlist *nap;
3994 3998 struct netbuf *nbp;
3995 3999 int i, fd;
3996 4000 void *handle;
3997 4001 char *uap;
3998 4002 struct t_bind bind, *bound;
3999 4003 pthread_t mythreadno;
4000 4004
4001 4005 if (Debug) {
4002 4006 mythreadno = pthread_self();
4003 4007 }
4004 4008
4005 4009 /*
4006 4010 * we need to know if we are running on the loghost. This is
4007 4011 * checked by binding to the address associated with "loghost"
4008 4012 * and "syslogd" service over the connectionless transport
4009 4013 */
4010 4014 hs.h_host = "loghost";
4011 4015 hs.h_serv = "syslog";
4012 4016
4013 4017 if ((handle = setnetconfig()) == NULL) {
4014 4018 return (0);
4015 4019 }
4016 4020
4017 4021 while ((ncp = getnetconfig(handle)) != NULL) {
4018 4022 if (ncp->nc_semantics != NC_TPI_CLTS) {
4019 4023 continue;
4020 4024 }
4021 4025
4022 4026 if (netdir_getbyname(ncp, &hs, &nap) != 0) {
4023 4027 continue;
4024 4028 }
4025 4029
4026 4030 if (nap == NULL) {
4027 4031 continue;
4028 4032 }
4029 4033
4030 4034 nbp = nap->n_addrs;
4031 4035
4032 4036 for (i = 0; i < nap->n_cnt; i++) {
4033 4037 if ((uap = taddr2uaddr(ncp, nbp)) != (char *)NULL) {
4034 4038 DPRINT2(1, "amiloghost(%u): testing %s\n",
4035 4039 mythreadno, uap);
4036 4040 }
4037 4041
4038 4042 free(uap);
4039 4043
4040 4044 fd = t_open(ncp->nc_device, O_RDWR, NULL);
4041 4045
4042 4046 if (fd < 0) {
4043 4047 netdir_free((void *)nap, ND_ADDRLIST);
4044 4048 (void) endnetconfig(handle);
4045 4049 return (0);
4046 4050 }
4047 4051
4048 4052 /*LINTED*/
4049 4053 bound = (struct t_bind *)t_alloc(fd, T_BIND, T_ADDR);
4050 4054 bind.addr = *nbp;
4051 4055 bind.qlen = 0;
4052 4056
4053 4057 if (t_bind(fd, &bind, bound) == 0) {
4054 4058 (void) t_close(fd);
4055 4059 (void) t_free((char *)bound, T_BIND);
4056 4060 netdir_free((void *)nap, ND_ADDRLIST);
4057 4061 (void) endnetconfig(handle);
4058 4062 return (1);
4059 4063 } else {
4060 4064 (void) t_close(fd);
4061 4065 (void) t_free((char *)bound, T_BIND);
4062 4066 }
4063 4067
4064 4068 nbp++;
4065 4069 }
4066 4070
4067 4071 netdir_free((void *)nap, ND_ADDRLIST);
4068 4072 }
4069 4073
4070 4074 (void) endnetconfig(handle);
4071 4075 return (0);
4072 4076 }
4073 4077
4074 4078 int
4075 4079 same_addr(struct netbuf *na, struct netbuf *nb)
4076 4080 {
4077 4081 char *a, *b;
4078 4082 size_t n;
4079 4083
4080 4084 assert(na->buf != NULL && nb->buf != NULL);
4081 4085
4082 4086 if (na->len != nb->len) {
4083 4087 return (0);
4084 4088 }
4085 4089
4086 4090 a = na->buf;
4087 4091 b = nb->buf;
4088 4092 n = nb->len;
4089 4093
4090 4094 while (n-- > 0) {
4091 4095 if (*a++ != *b++) {
4092 4096 return (0);
4093 4097 }
4094 4098 }
4095 4099
4096 4100 return (1);
4097 4101 }
4098 4102
4099 4103 /*
4100 4104 * allocates a new message structure, initializes it
4101 4105 * and returns a pointer to it
4102 4106 */
4103 4107 static log_message_t *
4104 4108 new_msg(void)
4105 4109 {
4106 4110 log_message_t *lm;
4107 4111 pthread_t mythreadno;
4108 4112
4109 4113 if (Debug) {
4110 4114 mythreadno = pthread_self();
4111 4115 }
4112 4116
4113 4117 if ((lm = malloc(sizeof (log_message_t))) == NULL)
4114 4118 return ((log_message_t *)NULL);
4115 4119
4116 4120 _NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*lm))
4117 4121
4118 4122 if (pthread_mutex_init(&lm->msg_mutex, NULL) != 0)
4119 4123 return ((log_message_t *)NULL);
4120 4124 lm->refcnt = 0;
4121 4125 lm->pri = 0;
4122 4126 lm->flags = 0;
4123 4127 lm->hlp = NULL;
4124 4128 lm->msg[0] = '\0';
4125 4129 lm->ptr = NULL;
4126 4130
4127 4131 DPRINT2(3, "new_msg(%u): creating msg %p\n", mythreadno, (void *)lm);
4128 4132 return (lm);
4129 4133 }
4130 4134
4131 4135 /*
4132 4136 * frees a message structure - should only be called if
4133 4137 * the refcount is 0
4134 4138 */
4135 4139 static void
4136 4140 free_msg(log_message_t *lm)
4137 4141 {
4138 4142 pthread_t mythreadno;
4139 4143
4140 4144 if (Debug) {
4141 4145 mythreadno = pthread_self();
4142 4146 }
4143 4147
4144 4148 assert(lm != NULL && lm->refcnt == 0);
4145 4149 if (lm->hlp != NULL)
4146 4150 freehl(lm->hlp);
4147 4151 DPRINT2(3, "free_msg(%u): freeing msg %p\n", mythreadno, (void *)lm);
4148 4152 free(lm);
4149 4153 }
4150 4154
4151 4155 /*
4152 4156 * Make sure that the message makes sense in the current locale, and
4153 4157 * does not contain stray control characters.
4154 4158 */
4155 4159 static void
4156 4160 filter_string(char *mbstr, char *filtered, size_t max)
4157 4161 {
4158 4162 size_t cs = 0;
4159 4163 size_t mb_cur_max;
4160 4164 unsigned char *p = (unsigned char *)mbstr;
4161 4165 pthread_t mythreadno = 0;
4162 4166
4163 4167 if (Debug) {
4164 4168 mythreadno = pthread_self();
4165 4169 }
4166 4170
4167 4171 assert(mbstr != NULL && filtered != NULL);
4168 4172
4169 4173 /*
4170 4174 * Since the access to MB_CUR_MAX is expensive (because
4171 4175 * MB_CUR_MAX lives in a global area), it should be
4172 4176 * restrained for the better performance.
4173 4177 */
4174 4178 mb_cur_max = (size_t)MB_CUR_MAX;
4175 4179 if (mb_cur_max > 1) {
4176 4180 /* multibyte locale */
4177 4181 int mlen;
4178 4182 wchar_t wc;
4179 4183
4180 4184 while (*p != '\0') {
4181 4185 if ((mlen = mbtowc(&wc, (char *)p,
4182 4186 mb_cur_max)) == -1) {
4183 4187 /*
4184 4188 * Invalid byte sequence found.
4185 4189 *
4186 4190 * try to print one byte
4187 4191 * in ASCII format.
4188 4192 */
4189 4193 DPRINT2(9, "filter_string(%u): Invalid "
4190 4194 "MB sequence: %ld\n", mythreadno,
4191 4195 wc);
4192 4196
4193 4197 if (!putctrlc(*p++, &filtered, &cs, max)) {
4194 4198 /* not enough buffer */
4195 4199 goto end;
4196 4200 } else {
4197 4201 continue;
4198 4202 }
4199 4203 } else {
4200 4204 /*
4201 4205 * Since *p is not a null byte here,
4202 4206 * mbtowc should have never returned 0.
4203 4207 *
4204 4208 * A valid wide character found.
4205 4209 */
4206 4210
4207 4211 if (wc != L'\t' && iswcntrl(wc)) {
4208 4212 /*
4209 4213 * non-tab, non-newline, and
4210 4214 * control character found.
4211 4215 *
4212 4216 * try to print this wide character
4213 4217 * in ASCII-format.
4214 4218 */
4215 4219 char *q = filtered;
4216 4220
4217 4221 DPRINT2(9, "filter_string(%u): MB"
4218 4222 " control character: %ld\n",
4219 4223 mythreadno, wc);
4220 4224
4221 4225 while (mlen--) {
4222 4226 if (!putctrlc(*p++, &filtered,
4223 4227 &cs, max)) {
4224 4228 /*
4225 4229 * not enough buffer in
4226 4230 * filtered
4227 4231 *
4228 4232 * cancel already
4229 4233 * stored bytes in
4230 4234 * filtered for this
4231 4235 * wide character.
4232 4236 */
4233 4237 filtered = q;
4234 4238 goto end;
4235 4239 }
4236 4240 }
4237 4241 continue;
4238 4242 } else {
4239 4243 /*
4240 4244 * tab, newline, or non-control
4241 4245 * character found.
4242 4246 */
4243 4247 if (cs + mlen < max) {
4244 4248 /* enough buffer */
4245 4249 cs += mlen;
4246 4250 while (mlen--) {
4247 4251 *filtered++ = *p++;
4248 4252 }
4249 4253 continue;
4250 4254 } else {
4251 4255 /* not enough buffer */
4252 4256 goto end;
4253 4257 }
4254 4258 }
4255 4259 }
4256 4260 }
4257 4261 } else {
4258 4262 /* singlebyte locale */
4259 4263
4260 4264 while (*p != '\0') {
4261 4265 if (*p != '\t' && iscntrl(*p)) {
4262 4266 /*
4263 4267 * non-tab, non-newline,
4264 4268 * and control character found.
4265 4269 *
4266 4270 * try to print this singlebyte character
4267 4271 * in ASCII format.
4268 4272 */
4269 4273 DPRINT2(9, "filter_string(%u): control "
4270 4274 "character: %d\n", mythreadno, *p);
4271 4275
4272 4276 if (!putctrlc(*p++, &filtered, &cs, max)) {
4273 4277 /* not enough buffer */
4274 4278 goto end;
4275 4279 } else {
4276 4280 continue;
4277 4281 }
4278 4282 } else if (*p != '\t' && !isprint(*p)) {
4279 4283 /*
4280 4284 * non-tab and non printable character found
4281 4285 * this check is required for the C locale
4282 4286 */
4283 4287 DPRINT2(9, "filter_string(%u): non-printable "
4284 4288 "character: %d\n", mythreadno, *p);
4285 4289 if (!putctrlc(*p++, &filtered, &cs, max)) {
4286 4290 /* not enough buffer */
4287 4291 goto end;
4288 4292 } else {
4289 4293 continue;
4290 4294 }
4291 4295 } else {
4292 4296 /*
4293 4297 * tab, newline, non-control character, or
4294 4298 * printable found.
4295 4299 */
4296 4300 if (cs + 1 < max) {
4297 4301 *filtered++ = *p++;
4298 4302 cs++;
4299 4303 continue;
4300 4304 } else {
4301 4305 /* not enough buffer */
4302 4306 goto end;
4303 4307 }
4304 4308 }
4305 4309 }
4306 4310 }
4307 4311
4308 4312 end:
4309 4313 *filtered = '\0';
4310 4314
4311 4315 if (cs >= 2 &&
4312 4316 filtered[-2] == '\\' && filtered[-1] == 'n') {
4313 4317 filtered[-2] = '\0';
4314 4318 }
4315 4319 }
4316 4320
4317 4321 static char *
4318 4322 alloc_stacks(int numstacks)
4319 4323 {
4320 4324 size_t pagesize, mapsize;
4321 4325 char *stack_top;
4322 4326 char *addr;
4323 4327 int i;
4324 4328
4325 4329 pagesize = (size_t)sysconf(_SC_PAGESIZE);
4326 4330 /*
4327 4331 * stacksize and redzonesize are global so threads
4328 4332 * can be created elsewhere and refer to the sizes
4329 4333 */
4330 4334 stacksize = (size_t)roundup(sysconf(_SC_THREAD_STACK_MIN) +
4331 4335 DEFAULT_STACKSIZE, pagesize);
4332 4336 redzonesize = (size_t)roundup(DEFAULT_REDZONESIZE, pagesize);
4333 4337
4334 4338 /*
4335 4339 * allocate an additional "redzonesize" chunk in addition
4336 4340 * to what we require, so we can create a redzone at the
4337 4341 * bottom of the last stack as well.
4338 4342 */
4339 4343 mapsize = redzonesize + numstacks * (stacksize + redzonesize);
4340 4344 stack_top = mmap(NULL, mapsize, PROT_READ|PROT_WRITE,
4341 4345 MAP_PRIVATE|MAP_ANON, -1, 0);
4342 4346 if (stack_top == MAP_FAILED)
4343 4347 return (NULL);
4344 4348
4345 4349 addr = stack_top;
4346 4350 /*
4347 4351 * this loop is intentionally <= instead of <, so we can
4348 4352 * protect the redzone at the bottom of the last stack
4349 4353 */
4350 4354 for (i = 0; i <= numstacks; i++) {
4351 4355 (void) mprotect(addr, redzonesize, PROT_NONE);
4352 4356 addr += stacksize + redzonesize;
4353 4357 }
4354 4358 return ((char *)(stack_top + redzonesize));
4355 4359 }
4356 4360
4357 4361 static void
4358 4362 dealloc_stacks(int numstacks)
4359 4363 {
4360 4364 size_t pagesize, mapsize;
4361 4365
4362 4366 pagesize = (size_t)sysconf(_SC_PAGESIZE);
4363 4367
4364 4368 stacksize = (size_t)roundup(sysconf(_SC_THREAD_STACK_MIN) +
4365 4369 DEFAULT_STACKSIZE, pagesize);
4366 4370
4367 4371 redzonesize = (size_t)roundup(DEFAULT_REDZONESIZE, pagesize);
4368 4372
4369 4373 mapsize = redzonesize + numstacks * (stacksize + redzonesize);
4370 4374 (void) munmap(cstack_ptr - mapsize, mapsize);
4371 4375 }
4372 4376
4373 4377 static void
4374 4378 filed_destroy(struct filed *f)
4375 4379 {
4376 4380 (void) dataq_destroy(&f->f_queue);
4377 4381 (void) pthread_mutex_destroy(&f->filed_mutex);
4378 4382 }
4379 4383
4380 4384 static void
4381 4385 close_door(void)
4382 4386 {
4383 4387 pthread_t mythreadno;
4384 4388
4385 4389 if (Debug) {
4386 4390 mythreadno = pthread_self();
4387 4391 }
4388 4392
4389 4393 (void) fdetach(DoorFileName);
4390 4394
4391 4395 DPRINT2(5, "close_door(%u): detached server() from %s\n",
4392 4396 mythreadno, DoorFileName);
4393 4397 }
4394 4398
4395 4399 static void
4396 4400 delete_doorfiles(void)
4397 4401 {
4398 4402 pthread_t mythreadno;
4399 4403 struct stat sb;
4400 4404 int err;
4401 4405 char line[MAXLINE+1];
4402 4406
4403 4407 if (Debug) {
4404 4408 mythreadno = pthread_self();
4405 4409 }
4406 4410
4407 4411
4408 4412 if (lstat(DoorFileName, &sb) == 0 && !S_ISDIR(sb.st_mode)) {
4409 4413 if (unlink(DoorFileName) < 0) {
4410 4414 err = errno;
4411 4415 (void) snprintf(line, sizeof (line),
4412 4416 "unlink() of %s failed - fatal", DoorFileName);
4413 4417 errno = err;
4414 4418 logerror(line);
4415 4419 DPRINT3(1, "delete_doorfiles(%u): error: %s, "
4416 4420 "errno=%d\n", mythreadno, line, err);
4417 4421 exit(1);
4418 4422 }
4419 4423
4420 4424 DPRINT2(5, "delete_doorfiles(%u): deleted %s\n",
4421 4425 mythreadno, DoorFileName);
4422 4426 }
4423 4427
4424 4428 if (strcmp(DoorFileName, DOORFILE) == 0) {
4425 4429 if (lstat(OLD_DOORFILE, &sb) == 0 && !S_ISDIR(sb.st_mode)) {
4426 4430 if (unlink(OLD_DOORFILE) < 0) {
4427 4431 err = errno;
4428 4432 (void) snprintf(line, sizeof (line),
4429 4433 "unlink() of %s failed", OLD_DOORFILE);
4430 4434 DPRINT2(5, "delete_doorfiles(%u): %s\n",
4431 4435 mythreadno, line);
4432 4436
4433 4437 if (err != EROFS) {
4434 4438 errno = err;
4435 4439 (void) strlcat(line, " - fatal",
4436 4440 sizeof (line));
4437 4441 logerror(line);
4438 4442 DPRINT3(1, "delete_doorfiles(%u): "
4439 4443 "error: %s, errno=%d\n",
4440 4444 mythreadno, line, err);
4441 4445 exit(1);
4442 4446 }
4443 4447
4444 4448 DPRINT1(5, "delete_doorfiles(%u): unlink() "
4445 4449 "failure OK on RO file system\n",
4446 4450 mythreadno);
4447 4451 }
4448 4452
4449 4453 DPRINT2(5, "delete_doorfiles(%u): deleted %s\n",
4450 4454 mythreadno, OLD_DOORFILE);
4451 4455 }
4452 4456 }
4453 4457
4454 4458 if (lstat(PidFileName, &sb) == 0 && !S_ISDIR(sb.st_mode)) {
4455 4459 if (unlink(PidFileName) < 0) {
4456 4460 err = errno;
4457 4461 (void) snprintf(line, sizeof (line),
4458 4462 "unlink() of %s failed - fatal", PidFileName);
4459 4463 errno = err;
4460 4464 logerror(line);
4461 4465 DPRINT3(1, "delete_doorfiles(%u): error: %s, "
4462 4466 "errno=%d\n", mythreadno, line, err);
4463 4467 exit(1);
4464 4468 }
4465 4469
4466 4470 DPRINT2(5, "delete_doorfiles(%u): deleted %s\n", mythreadno,
4467 4471 PidFileName);
4468 4472 }
4469 4473
4470 4474 if (strcmp(PidFileName, PIDFILE) == 0) {
4471 4475 if (lstat(OLD_PIDFILE, &sb) == 0 && !S_ISDIR(sb.st_mode)) {
4472 4476 if (unlink(OLD_PIDFILE) < 0) {
4473 4477 err = errno;
4474 4478 (void) snprintf(line, sizeof (line),
4475 4479 "unlink() of %s failed", OLD_PIDFILE);
4476 4480 DPRINT2(5, "delete_doorfiles(%u): %s, \n",
4477 4481 mythreadno, line);
4478 4482
4479 4483 if (err != EROFS) {
4480 4484 errno = err;
4481 4485 (void) strlcat(line, " - fatal",
4482 4486 sizeof (line));
4483 4487 logerror(line);
4484 4488 DPRINT3(1, "delete_doorfiles(%u): "
4485 4489 "error: %s, errno=%d\n",
4486 4490 mythreadno, line, err);
4487 4491 exit(1);
4488 4492 }
4489 4493
4490 4494 DPRINT1(5, "delete_doorfiles(%u): unlink "
4491 4495 "failure OK on RO file system\n",
4492 4496 mythreadno);
4493 4497 }
4494 4498
4495 4499 DPRINT2(5, "delete_doorfiles(%u): deleted %s\n",
4496 4500 mythreadno, OLD_PIDFILE);
4497 4501 }
4498 4502 }
4499 4503
4500 4504 if (DoorFd != -1) {
4501 4505 (void) door_revoke(DoorFd);
4502 4506 }
4503 4507
4504 4508 DPRINT2(1, "delete_doorfiles(%u): revoked door: DoorFd=%d\n",
4505 4509 mythreadno, DoorFd);
4506 4510 }
4507 4511
4508 4512
4509 4513 /*ARGSUSED*/
4510 4514 static void
4511 4515 signull(int sig, siginfo_t *sip, void *utp)
4512 4516 {
4513 4517 DPRINT1(1, "signull(%u): THIS CALL SHOULD NEVER HAPPEN\n",
4514 4518 pthread_self());
4515 4519 /*
4516 4520 * Do nothing, as this is a place-holder used in conjunction with
4517 4521 * sigaction()/sigwait() to ensure that the proper disposition is
4518 4522 * given to the signals we handle in main().
4519 4523 */
4520 4524 }
4521 4525
4522 4526 /*
4523 4527 * putctrlc returns zero, if failed due to not enough buffer.
4524 4528 * Otherwise, putctrlc returns non-zero.
4525 4529 *
4526 4530 * c: a byte to print in ASCII format
4527 4531 * **buf: a pointer to the pointer to the output buffer.
4528 4532 * *cl: current length of characters in the output buffer
4529 4533 * max: maximum length of the buffer
4530 4534 */
4531 4535
4532 4536 static int
4533 4537 putctrlc(int c, char **buf, size_t *cl, size_t max)
4534 4538 {
4535 4539 char *p = *buf;
4536 4540
4537 4541 if (c == '\n') {
4538 4542 if (*cl + 2 < max) {
4539 4543 *p++ = '\\';
4540 4544 *p++ = 'n';
4541 4545 *cl += 2;
4542 4546 *buf = p;
4543 4547 return (2);
4544 4548 } else {
4545 4549 return (0);
4546 4550 }
4547 4551 } else if (c < 0200) {
4548 4552 /* ascii control character */
4549 4553 if (*cl + 2 < max) {
4550 4554 *p++ = '^';
4551 4555 *p++ = c ^ 0100;
4552 4556 *cl += 2;
4553 4557 *buf = p;
4554 4558 return (2);
4555 4559 } else {
4556 4560 return (0);
4557 4561 }
4558 4562 } else {
4559 4563 if (*cl + 4 < max) {
4560 4564 *p++ = '\\';
4561 4565 *p++ = ((c >> 6) & 07) + '0';
4562 4566 *p++ = ((c >> 3) & 07) + '0';
4563 4567 *p++ = (c & 07) + '0';
4564 4568 *cl += 4;
4565 4569 *buf = p;
4566 4570 return (4);
4567 4571 } else {
4568 4572 return (0);
4569 4573 }
4570 4574 }
4571 4575 }
4572 4576
4573 4577 /*
4574 4578 * findnl_bkwd:
4575 4579 * Scans each character in buf until it finds the last newline in buf,
4576 4580 * or the scanned character becomes the last COMPLETE character in buf.
4577 4581 * Returns the number of scanned bytes.
4578 4582 *
4579 4583 * buf - pointer to a buffer containing the message string
4580 4584 * len - the length of the buffer
4581 4585 */
4582 4586 size_t
4583 4587 findnl_bkwd(const char *buf, const size_t len)
4584 4588 {
4585 4589 const char *p;
4586 4590 size_t mb_cur_max;
4587 4591 pthread_t mythreadno;
4588 4592
4589 4593 if (Debug) {
4590 4594 mythreadno = pthread_self();
4591 4595 }
4592 4596
4593 4597 if (len == 0) {
4594 4598 return (0);
4595 4599 }
4596 4600
4597 4601 mb_cur_max = MB_CUR_MAX;
4598 4602
4599 4603 if (mb_cur_max == 1) {
4600 4604 /* single-byte locale */
4601 4605 for (p = buf + len - 1; p != buf; p--) {
4602 4606 if (*p == '\n') {
4603 4607 return ((size_t)(p - buf));
4604 4608 }
4605 4609 }
4606 4610 return ((size_t)len);
4607 4611 } else {
4608 4612 /* multi-byte locale */
4609 4613 int mlen;
4610 4614 const char *nl;
4611 4615 size_t rem;
4612 4616
4613 4617 p = buf;
4614 4618 nl = NULL;
4615 4619 for (rem = len; rem >= mb_cur_max; ) {
4616 4620 mlen = mblen(p, mb_cur_max);
4617 4621 if (mlen == -1) {
4618 4622 /*
4619 4623 * Invalid character found.
4620 4624 */
4621 4625 DPRINT1(9, "findnl_bkwd(%u): Invalid MB "
4622 4626 "sequence\n", mythreadno);
4623 4627 /*
4624 4628 * handle as a single byte character.
4625 4629 */
4626 4630 p++;
4627 4631 rem--;
4628 4632 } else {
4629 4633 /*
4630 4634 * It's guaranteed that *p points to
4631 4635 * the 1st byte of a multibyte character.
4632 4636 */
4633 4637 if (*p == '\n') {
4634 4638 nl = p;
4635 4639 }
4636 4640 p += mlen;
4637 4641 rem -= mlen;
4638 4642 }
4639 4643 }
4640 4644 if (nl) {
4641 4645 return ((size_t)(nl - buf));
4642 4646 }
4643 4647 /*
4644 4648 * no newline nor null byte found.
4645 4649 * Also it's guaranteed that *p points to
4646 4650 * the 1st byte of a (multibyte) character
4647 4651 * at this point.
4648 4652 */
4649 4653 return (len - rem);
4650 4654 }
4651 4655 }
4652 4656
4653 4657 /*
4654 4658 * copynl_frwd:
4655 4659 * Scans each character in buf and copies the scanned character to obuf
4656 4660 * until it finds a null byte or a newline, or
4657 4661 * the number of the remaining bytes in obuf gets to exceed obuflen
4658 4662 * if copying the scanned character to obuf.
4659 4663 * Returns the number of scanned bytes.
4660 4664 *
4661 4665 * obuf - buffer to be copied the scanned character
4662 4666 * obuflen - the size of obuf
4663 4667 * buf - pointer to a buffer containing the message string
4664 4668 * len - the length of the buffer
4665 4669 */
4666 4670 size_t
4667 4671 copynl_frwd(char *obuf, const size_t obuflen,
4668 4672 const char *buf, const size_t len)
4669 4673 {
4670 4674 const char *p;
4671 4675 char *q = obuf;
4672 4676 size_t olen = 0;
4673 4677 size_t mb_cur_max;
4674 4678 pthread_t mythreadno;
4675 4679
4676 4680 if (Debug) {
4677 4681 mythreadno = pthread_self();
4678 4682 }
4679 4683
4680 4684 if (len == 0) {
4681 4685 return (0);
4682 4686 }
4683 4687
4684 4688 mb_cur_max = MB_CUR_MAX;
4685 4689
4686 4690 if (mb_cur_max == 1) {
4687 4691 /* single-byte locale */
4688 4692 for (p = buf; *p; ) {
4689 4693 if (obuflen > olen + 1) {
4690 4694 if (*p != '\n') {
4691 4695 *q++ = *p++;
4692 4696 olen++;
4693 4697 } else {
4694 4698 *q = '\0';
4695 4699 return ((size_t)(p - buf));
4696 4700 }
4697 4701 } else {
4698 4702 *q = '\0';
4699 4703 return ((size_t)(p - buf));
4700 4704 }
4701 4705 }
4702 4706 *q = '\0';
4703 4707 return ((size_t)(p - buf));
4704 4708 } else {
4705 4709 /* multi-byte locale */
4706 4710 int mlen;
4707 4711
4708 4712 for (p = buf; *p; ) {
4709 4713 mlen = mblen(p, mb_cur_max);
4710 4714 if (mlen == -1) {
4711 4715 /*
4712 4716 * Invalid character found.
4713 4717 */
4714 4718 DPRINT1(9, "copynl_frwd(%u): Invalid MB "
4715 4719 "sequence\n", mythreadno);
4716 4720 /*
4717 4721 * handle as a single byte character.
4718 4722 */
4719 4723 if (obuflen > olen + 1) {
4720 4724 *q++ = *p++;
4721 4725 olen++;
4722 4726 } else {
4723 4727 *q = '\0';
4724 4728 return ((size_t)(p - buf));
4725 4729 }
4726 4730 } else {
4727 4731 /*
4728 4732 * It's guaranteed that *p points to
4729 4733 * the 1st byte of a multibyte character.
4730 4734 */
4731 4735 if (*p == '\n') {
4732 4736 *q = '\0';
4733 4737 return ((size_t)(p - buf));
4734 4738 }
4735 4739 if (obuflen > olen + mlen) {
4736 4740 int n;
4737 4741 for (n = 0; n < mlen; n++) {
4738 4742 *q++ = *p++;
4739 4743 }
4740 4744 olen += mlen;
4741 4745 } else {
4742 4746 *q = '\0';
4743 4747 return ((size_t)(p - buf));
4744 4748 }
4745 4749 }
4746 4750 }
4747 4751 /*
4748 4752 * no newline nor null byte found.
4749 4753 * Also it's guaranteed that *p points to
4750 4754 * the 1st byte of a (multibyte) character
4751 4755 * at this point.
4752 4756 */
4753 4757 *q = '\0';
4754 4758 return ((size_t)(p - buf));
4755 4759 }
4756 4760 }
4757 4761
4758 4762 /*
4759 4763 * copy_frwd:
4760 4764 * Scans each character in buf and copies the scanned character to obuf
4761 4765 * until the number of the remaining bytes in obuf gets to exceed obuflen
4762 4766 * if copying the scanned character to obuf.
4763 4767 * Returns the number of scanned (copied) bytes.
4764 4768 *
4765 4769 * obuf - buffer to be copied the scanned character
4766 4770 * obuflen - the size of obuf
4767 4771 * buf - pointer to a buffer containing the message string
4768 4772 * len - the length of the buffer
4769 4773 */
4770 4774 size_t
4771 4775 copy_frwd(char *obuf, const size_t obuflen,
4772 4776 const char *buf, const size_t len)
4773 4777 {
4774 4778 const char *p;
4775 4779 char *q = obuf;
4776 4780 size_t olen = 0;
4777 4781 size_t mb_cur_max;
4778 4782 pthread_t mythreadno;
4779 4783
4780 4784 if (Debug) {
4781 4785 mythreadno = pthread_self();
4782 4786 }
4783 4787
4784 4788 if (len == 0) {
4785 4789 return (0);
4786 4790 }
4787 4791
4788 4792 mb_cur_max = MB_CUR_MAX;
4789 4793
4790 4794 if (mb_cur_max == 1) {
4791 4795 /* single-byte locale */
4792 4796 if (obuflen > len) {
4793 4797 (void) memcpy(obuf, buf, len);
4794 4798 obuf[len] = '\0';
4795 4799 return ((size_t)len);
4796 4800 } else {
4797 4801 (void) memcpy(obuf, buf, obuflen - 1);
4798 4802 obuf[obuflen - 1] = '\0';
4799 4803 return (obuflen - 1);
4800 4804 }
4801 4805 } else {
4802 4806 /* multi-byte locale */
4803 4807 int mlen;
4804 4808
4805 4809 for (p = buf; *p; ) {
4806 4810 mlen = mblen(p, mb_cur_max);
4807 4811 if (mlen == -1) {
4808 4812 /*
4809 4813 * Invalid character found.
4810 4814 */
4811 4815 DPRINT1(9, "copy_frwd(%u): Invalid MB "
4812 4816 "sequence\n", mythreadno);
4813 4817 /*
4814 4818 * handle as a single byte character.
4815 4819 */
4816 4820 if (obuflen > olen + 1) {
4817 4821 *q++ = *p++;
4818 4822 olen++;
4819 4823 } else {
4820 4824 *q = '\0';
4821 4825 return ((size_t)(p - buf));
4822 4826 }
4823 4827 } else {
4824 4828 if (obuflen > olen + mlen) {
4825 4829 int n;
4826 4830 for (n = 0; n < mlen; n++) {
4827 4831 *q++ = *p++;
4828 4832 }
4829 4833 olen += mlen;
4830 4834 } else {
4831 4835 *q = '\0';
4832 4836 return ((size_t)(p - buf));
4833 4837 }
4834 4838 }
4835 4839 }
4836 4840 *q = '\0';
4837 4841 return ((size_t)(p - buf));
4838 4842 }
4839 4843 }
4840 4844
4841 4845 /*
4842 4846 * properties:
4843 4847 * Get properties from SMF framework.
4844 4848 */
4845 4849 static void
4846 4850 properties(void)
4847 4851 {
4848 4852 scf_simple_prop_t *prop;
4849 4853 uint8_t *bool;
4850 4854
4851 4855 if ((prop = scf_simple_prop_get(NULL, NULL, "config",
4852 4856 "log_from_remote")) != NULL) {
4853 4857 if ((bool = scf_simple_prop_next_boolean(prop)) != NULL) {
4854 4858 if (*bool == 0)
4855 4859 turnoff = 1; /* log_from_remote = false */
4856 4860 else
4857 4861 turnoff = 0; /* log_from_remote = true */
4858 4862 }
4859 4863 scf_simple_prop_free(prop);
4860 4864 DPRINT1(1, "properties: setting turnoff to %s\n",
4861 4865 turnoff ? "true" : "false");
4862 4866 }
4863 4867 }
4864 4868
4865 4869 /*
4866 4870 * close all the input devices.
4867 4871 */
4868 4872 static void
4869 4873 shutdown_input(void)
4870 4874 {
4871 4875 int cnt;
4872 4876
4873 4877 shutting_down = 1;
4874 4878
4875 4879 for (cnt = 0; cnt < Ninputs; cnt++) {
4876 4880 (void) t_close(Nfd[cnt].fd);
4877 4881 }
4878 4882
4879 4883 (void) close(Pfd.fd);
4880 4884 }
4881 4885
4882 4886 /*
4883 4887 * This is for the one thread that dedicates to resolve the
4884 4888 * hostname. This will get the messages from net_poll() through
4885 4889 * hnlq, and resolve the hostname, and push the messages back
4886 4890 * into the inputq.
4887 4891 */
4888 4892 /*ARGSUSED*/
4889 4893 static void *
4890 4894 hostname_lookup(void *ap)
4891 4895 {
4892 4896 char *uap;
4893 4897 log_message_t *mp;
4894 4898 host_info_t *hip;
4895 4899 char failsafe_addr[SYS_NMLN + 1];
4896 4900 pthread_t mythreadno;
4897 4901
4898 4902 if (Debug) {
4899 4903 mythreadno = pthread_self();
4900 4904 }
4901 4905
4902 4906 DPRINT1(1, "hostname_lookup(%u): hostname_lookup started\n",
4903 4907 mythreadno);
4904 4908
4905 4909 for (;;) {
4906 4910 (void) dataq_dequeue(&hnlq, (void **)&mp, 0);
4907 4911
4908 4912 DPRINT3(5, "hostname_lookup(%u): dequeued msg %p"
4909 4913 " from queue %p\n", mythreadno, (void *)mp,
4910 4914 (void *)&hnlq);
4911 4915
4912 4916 hip = (host_info_t *)mp->ptr;
4913 4917 if ((uap = taddr2uaddr(hip->ncp, &hip->addr)) != NULL) {
4914 4918 (void) strlcpy(failsafe_addr, uap, SYS_NMLN);
4915 4919 free(uap);
4916 4920 } else {
4917 4921 (void) strlcpy(failsafe_addr, "<unknown>", SYS_NMLN);
4918 4922 }
4919 4923
4920 4924 mp->hlp = cvthname(&hip->addr, hip->ncp, failsafe_addr);
4921 4925
4922 4926 if (mp->hlp == NULL) {
4923 4927 mp->hlp = &NullHostName;
4924 4928 }
4925 4929
4926 4930 free(hip->addr.buf);
4927 4931 free(hip);
4928 4932 mp->ptr = NULL;
4929 4933
4930 4934 if (dataq_enqueue(&inputq, (void *)mp) == -1) {
4931 4935 MALLOC_FAIL("dropping message from remote");
4932 4936 free_msg(mp);
4933 4937 continue;
4934 4938 }
4935 4939
4936 4940 DPRINT3(5, "hostname_lookup(%u): enqueued msg %p on queue "
4937 4941 "%p\n", mythreadno, (void *)mp, (void *)&inputq);
4938 4942 }
4939 4943
4940 4944 /*NOTREACHED*/
4941 4945 return (NULL);
4942 4946 }
4943 4947
4944 4948 /*
4945 4949 * Does all HUP(re-configuration) process.
4946 4950 */
4947 4951 static void
4948 4952 reconfigure()
4949 4953 {
4950 4954 int cnt, loop, drops;
4951 4955 int really_stuck;
4952 4956 int console_stuck = 0;
4953 4957 struct filed *f;
4954 4958 char buf[LINE_MAX];
4955 4959 struct utsname up;
4956 4960 char cbuf[30];
4957 4961 time_t tim;
4958 4962 pthread_t mythreadno;
4959 4963
4960 4964 if (Debug) {
4961 4965 mythreadno = pthread_self();
4962 4966 }
4963 4967
4964 4968 /* If we get here then we must need to regen */
4965 4969 flushmsg(0);
4966 4970
4967 4971 if (logmymsg(LOG_SYSLOG|LOG_INFO, "syslogd: configuration restart",
4968 4972 ADDDATE, 0) == -1) {
4969 4973 MALLOC_FAIL("dropping message");
4970 4974 }
4971 4975
4972 4976 /*
4973 4977 * make sure the logmsg thread is not in the waiting state.
4974 4978 * Otherwise, changing hup_state will prevent the logmsg thread
4975 4979 * getting out from the waiting loop.
4976 4980 */
4977 4981
4978 4982 if (Debug) {
4979 4983 tim = time(NULL);
4980 4984 DPRINT2(3, "reconfigure(%u): %.15s: awaiting logmsg()"
4981 4985 " moving to the safe place\n",
4982 4986 mythreadno, ctime_r(&tim, cbuf)+4);
4983 4987 }
4984 4988
4985 4989 for (loop = 0; loop < LOOP_MAX; loop++) {
4986 4990 /* we don't need the mutex to read */
4987 4991 if (hup_state == HUP_ACCEPTABLE)
4988 4992 break;
4989 4993 (void) sleep(1);
4990 4994 }
4991 4995 if (hup_state != HUP_ACCEPTABLE) {
4992 4996 goto thread_stuck;
4993 4997 }
4994 4998
4995 4999 if (Debug) {
4996 5000 tim = time(NULL);
4997 5001 DPRINT2(3, "reconfigure(%u): %.15s: logmsg() will accept HUP\n",
4998 5002 mythreadno, ctime_r(&tim, cbuf)+4);
4999 5003 }
5000 5004
5001 5005 /*
5002 5006 * Prevent logging until we are truly done processing the HUP
5003 5007 */
5004 5008 (void) pthread_mutex_lock(&hup_lock);
5005 5009 hup_state = HUP_INPROGRESS;
5006 5010 (void) pthread_mutex_unlock(&hup_lock);
5007 5011
5008 5012 /*
5009 5013 * We will be going into a critical state. Any error message
5010 5014 * from syslogd needs to be dumped to the console by default
5011 5015 * immediately. Also, those error messages are quened in a temporary
5012 5016 * queue to be able to post into the regular stream later.
5013 5017 */
5014 5018 disable_errorlog();
5015 5019
5016 5020 if (Debug) {
5017 5021 tim = time(NULL);
5018 5022 DPRINT2(3, "reconfigure(%u): %.15s: sending SHUTDOWN\n",
5019 5023 mythreadno, ctime_r(&tim, cbuf)+4);
5020 5024 }
5021 5025
5022 5026 /* stop configured threads */
5023 5027 if (shutdown_msg() == -1) {
5024 5028 /*
5025 5029 * No memory, message will be dumped to the console.
5026 5030 */
5027 5031 MALLOC_FAIL("unable to restart syslogd");
5028 5032 goto out;
5029 5033 }
5030 5034
5031 5035 /* make sure logmsg() is in suspended state */
5032 5036 for (loop = 0; loop < LOOP_INTERVAL; loop++) {
5033 5037 if (hup_state & HUP_LOGMSG_SUSPENDED)
5034 5038 break;
5035 5039 (void) sleep(1);
5036 5040 }
5037 5041
5038 5042 if ((hup_state & HUP_LOGMSG_SUSPENDED) == 0) {
5039 5043 if (Debug) {
5040 5044 tim = time(NULL);
5041 5045 DPRINT2(3, "reconfigure(%u): %.15s: logmsg() does not "
5042 5046 "stop. enforcing\n",
5043 5047 mythreadno, ctime_r(&tim, cbuf)+4);
5044 5048 }
5045 5049
5046 5050 /* probably we have too long input queue, or really stuck */
5047 5051 (void) pthread_mutex_lock(&hup_lock);
5048 5052 hup_state |= HUP_SUSP_LOGMSG_REQD;
5049 5053 (void) pthread_mutex_unlock(&hup_lock);
5050 5054
5051 5055 for (loop = 0; loop < LOOP_MAX; loop++) {
5052 5056 if (hup_state & HUP_LOGMSG_SUSPENDED)
5053 5057 break;
5054 5058 (void) sleep(1);
5055 5059 }
5056 5060 if ((hup_state & HUP_LOGMSG_SUSPENDED) == 0) {
5057 5061 if (Debug) {
5058 5062 tim = time(NULL);
5059 5063 DPRINT2(3, "reconfigure(%u): %.15s: logmsg()"
5060 5064 " does not stop. give up\n",
5061 5065 mythreadno, ctime_r(&tim, cbuf)+4);
5062 5066 }
5063 5067 logerror("could not suspend logmsg - fatal");
5064 5068 goto thread_stuck;
5065 5069 }
5066 5070 }
5067 5071
5068 5072 if (Debug) {
5069 5073 tim = time(NULL);
5070 5074 DPRINT2(3, "reconfigure(%u): %.15s: logmsg() suspended\n",
5071 5075 mythreadno, ctime_r(&tim, cbuf)+4);
5072 5076 }
5073 5077
5074 5078 /*
5075 5079 * Will wait for LOOP_MAX secs with watching queue lengths for the
5076 5080 * each logger threads. If they have backlogs, and no change in the
5077 5081 * length of queue found in 30 seconds, those will be counted as
5078 5082 * "really stuck".
5079 5083 * If all running logger threads become "really stuck" state, there
5080 5084 * should be no worth waiting for them to quit.
5081 5085 * In that case, we will go ahead and close out file descriptors to
5082 5086 * have them pull out from hanging system call, and give them a last
5083 5087 * chance(LOOP_INTERVAL sec) to quit.
5084 5088 */
5085 5089
5086 5090 if (Debug) {
5087 5091 tim = time(NULL);
5088 5092 DPRINT2(3, "reconfigure(%u): %.15s: awaiting logit() to be"
5089 5093 " shutdown\n", mythreadno, ctime_r(&tim, cbuf)+4);
5090 5094 }
5091 5095
5092 5096 cnt = 0;
5093 5097 really_stuck = 0;
5094 5098 while (cnt < (LOOP_MAX/LOOP_INTERVAL) &&
5095 5099 conf_threads > really_stuck) {
5096 5100
5097 5101 /* save initial queue count */
5098 5102 for (f = Files; f < &Files[nlogs]; f++) {
5099 5103 f->f_prev_queue_count = (f->f_type == F_UNUSED) ?
5100 5104 -1 : f->f_queue_count;
5101 5105 }
5102 5106
5103 5107 for (loop = 0; loop < LOOP_INTERVAL; loop++) {
5104 5108 if (conf_threads == 0)
5105 5109 break;
5106 5110 (void) sleep(1);
5107 5111 }
5108 5112
5109 5113 if (conf_threads == 0)
5110 5114 break;
5111 5115
5112 5116 if (Debug) {
5113 5117 tim = time(NULL);
5114 5118 DPRINT3(3, "reconfigure(%u): %.15s: "
5115 5119 "%d threads are still alive.\n",
5116 5120 mythreadno, ctime_r(&tim, cbuf)+4,
5117 5121 conf_threads);
5118 5122 }
5119 5123
5120 5124 really_stuck = 0;
5121 5125 for (f = Files; f < &Files[nlogs]; f++) {
5122 5126 if (f->f_type == F_UNUSED) {
5123 5127 f->f_prev_queue_count = -1;
5124 5128 continue;
5125 5129 }
5126 5130 if (f->f_prev_queue_count == f->f_queue_count) {
5127 5131 really_stuck++;
5128 5132 f->f_prev_queue_count = 1;
5129 5133 DPRINT2(3, "reconfigure(%u): "
5130 5134 "tid=%d is really stuck.\n",
5131 5135 mythreadno, f->f_thread);
5132 5136 } else {
5133 5137 f->f_prev_queue_count = 0;
5134 5138 DPRINT2(3, "reconfigure(%u): "
5135 5139 "tid=%d is still active.\n",
5136 5140 mythreadno, f->f_thread);
5137 5141 }
5138 5142 }
5139 5143 /*
5140 5144 * Here we have one of following values in the
5141 5145 * f_prev_queue_count:
5142 5146 * 0: logger thread is still actively working.
5143 5147 * 1: logger thread is really stuck.
5144 5148 * -1: logger thread has already died.
5145 5149 */
5146 5150
5147 5151 cnt++;
5148 5152 }
5149 5153
5150 5154 if (Debug) {
5151 5155 tim = time(NULL);
5152 5156 DPRINT2(3, "reconfigure(%u): %.15s:"
5153 5157 " complete awaiting logit()\n",
5154 5158 mythreadno, ctime_r(&tim, cbuf)+4);
5155 5159 DPRINT3(3, "reconfigure(%u): %d threads alive."
5156 5160 " %d threads stuck\n",
5157 5161 mythreadno, conf_threads, really_stuck);
5158 5162 }
5159 5163
5160 5164 /*
5161 5165 * Still running? If so, mark it as UNUSED, and close
5162 5166 * the fd so that logger threads can bail out from the loop.
5163 5167 */
5164 5168 drops = 0;
5165 5169 if (conf_threads) {
5166 5170 for (f = Files; f < &Files[nlogs]; f++) {
5167 5171 if (f->f_type == F_CONSOLE &&
5168 5172 f->f_prev_queue_count == 1) {
5169 5173 /* console is really stuck */
5170 5174 console_stuck = 1;
5171 5175 }
5172 5176 if (f->f_type == F_USERS || f->f_type == F_WALL ||
5173 5177 f->f_type == F_UNUSED)
5174 5178 continue;
5175 5179 cnt = f->f_queue_count;
5176 5180 drops += (cnt > 0) ? cnt - 1: 0;
5177 5181 f->f_type = F_UNUSED;
5178 5182
5179 5183 if (f->f_orig_type == F_FORW)
5180 5184 (void) t_close(f->f_file);
5181 5185 else
5182 5186 (void) close(f->f_file);
5183 5187 }
5184 5188
5185 5189 if (Debug) {
5186 5190 tim = time(NULL);
5187 5191 DPRINT1(3, "reconfigure(%u): terminating logit()\n",
5188 5192 mythreadno);
5189 5193 }
5190 5194
5191 5195 /* last chance to exit */
5192 5196 for (loop = 0; loop < LOOP_MAX; loop++) {
5193 5197 if (conf_threads == 0)
5194 5198 break;
5195 5199 (void) sleep(1);
5196 5200 }
5197 5201
5198 5202 if (Debug) {
5199 5203 tim = time(NULL);
5200 5204 DPRINT3(3, "reconfigure(%u): %.15s: %d alive\n",
5201 5205 mythreadno, ctime_r(&tim, cbuf)+4,
5202 5206 conf_threads);
5203 5207 }
5204 5208 }
5205 5209
5206 5210 if (conf_threads == 0 && drops) {
5207 5211 errno = 0;
5208 5212 logerror("Could not completely output pending messages"
5209 5213 " while preparing re-configuration");
5210 5214 logerror("discarded %d messages and restart configuration.",
5211 5215 drops);
5212 5216 if (Debug) {
5213 5217 tim = time(NULL);
5214 5218 DPRINT3(3, "reconfigure(%u): %.15s: "
5215 5219 "discarded %d messages\n",
5216 5220 mythreadno, ctime_r(&tim, cbuf)+4, drops);
5217 5221 }
5218 5222 }
5219 5223
5220 5224 /*
5221 5225 * If all threads still haven't exited
5222 5226 * something is stuck or hosed. We just
5223 5227 * have no option but to exit.
5224 5228 */
5225 5229 if (conf_threads) {
5226 5230 thread_stuck:
5227 5231 if (Debug) {
5228 5232 tim = time(NULL);
5229 5233 DPRINT2(3, "reconfigure(%u): %.15s: really stuck\n",
5230 5234 mythreadno, ctime_r(&tim, cbuf)+4);
5231 5235 }
5232 5236
5233 5237 shutdown_input();
5234 5238 delete_doorfiles();
5235 5239 (void) uname(&up);
5236 5240
5237 5241 (void) snprintf(buf, sizeof (buf),
5238 5242 "syslogd(%s): some logger thread(s) "
5239 5243 "are stuck%s; syslogd is shutting down.",
5240 5244 up.nodename,
5241 5245 console_stuck ? " (including the console)" : "");
5242 5246
5243 5247 if (console_stuck) {
5244 5248 FILE *m = popen(MAILCMD, "w");
5245 5249
5246 5250 if (m != NULL) {
5247 5251 (void) fprintf(m, "%s\n", buf);
5248 5252 (void) pclose(m);
5249 5253 }
5250 5254 }
5251 5255
5252 5256 disable_errorlog();
5253 5257 logerror(buf);
5254 5258 exit(1);
5255 5259 }
5256 5260
5257 5261 /* Free up some resources */
5258 5262 if (Files != (struct filed *)&fallback) {
5259 5263 for (f = Files; f < &Files[nlogs]; f++) {
5260 5264 (void) pthread_join(f->f_thread, NULL);
5261 5265 filed_destroy(f);
5262 5266 }
5263 5267 free(Files);
5264 5268 }
5265 5269
5266 5270 dealloc_stacks(nlogs);
5267 5271
5268 5272 if (Debug) {
5269 5273 tim = time(NULL);
5270 5274 DPRINT2(3, "reconfigure(%u): %.15s: cleanup complete\n",
5271 5275 mythreadno, ctime_r(&tim, cbuf)+4);
5272 5276 }
5273 5277
5274 5278 hnc_init(1); /* purge hostname cache */
5275 5279 conf_init(); /* start reconfigure */
5276 5280
5277 5281 out:;
5278 5282 /* Now should be ready to dispatch error messages from syslogd. */
5279 5283 enable_errorlog();
5280 5284
5281 5285 /* Wake up the log thread */
5282 5286
5283 5287 if (Debug) {
5284 5288 tim = time(NULL);
5285 5289 DPRINT2(3, "reconfigure(%u): %.15s: resuming logmsg()\n",
5286 5290 mythreadno, ctime_r(&tim, cbuf)+4);
5287 5291 }
5288 5292
5289 5293 (void) pthread_mutex_lock(&hup_lock);
5290 5294 hup_state = HUP_COMPLETED;
5291 5295 (void) pthread_cond_signal(&hup_done);
5292 5296 (void) pthread_mutex_unlock(&hup_lock);
5293 5297 }
5294 5298
5295 5299 /*
5296 5300 * The following function implements simple hostname cache mechanism.
5297 5301 * Host name cache is implemented through hash table bucket chaining method.
5298 5302 * Collisions are handled by bucket chaining.
5299 5303 *
5300 5304 * hnc_init():
5301 5305 * allocate and initialize the cache. If reinit is set,
5302 5306 * invalidate all cache entries.
5303 5307 * hnc_look():
5304 5308 * It hashes the ipaddress gets the index and walks thru the
5305 5309 * single linked list. if cached entry was found, it will
5306 5310 * put in the head of the list, and return.While going through
5307 5311 * the entries, an entry which has already expired will be invalidated.
5308 5312 * hnc_register():
5309 5313 * Hashes the ipaddress finds the index and puts current entry to the list.
5310 5314 * hnc_unreg():
5311 5315 * invalidate the cachep.
5312 5316 */
5313 5317
5314 5318 static void
5315 5319 hnc_init(int reinit)
5316 5320 {
5317 5321 struct hostname_cache **hpp;
5318 5322 pthread_t mythreadno;
5319 5323 int i;
5320 5324
5321 5325 if (Debug) {
5322 5326 mythreadno = pthread_self();
5323 5327 }
5324 5328
5325 5329 if (reinit) {
5326 5330 (void) pthread_mutex_lock(&hnc_mutex);
5327 5331
5328 5332 for (i = 0; i < hnc_size; i++) {
5329 5333 for (hpp = &hnc_cache[i]; *hpp != NULL; ) {
5330 5334 hnc_unreg(hpp);
5331 5335 }
5332 5336 }
5333 5337
5334 5338 (void) pthread_mutex_unlock(&hnc_mutex);
5335 5339 DPRINT1(2, "hnc_init(%u): hostname cache re-configured\n",
5336 5340 mythreadno);
5337 5341 } else {
5338 5342
5339 5343 hnc_cache = calloc(hnc_size, sizeof (struct hostname_cache *));
5340 5344
5341 5345 if (hnc_cache == NULL) {
5342 5346 MALLOC_FAIL("hostname cache");
5343 5347 logerror("hostname cache disabled");
5344 5348 return;
5345 5349 }
5346 5350
5347 5351 DPRINT3(1, "hnc_init(%u): hostname cache configured %d entry"
5348 5352 " ttl:%d\n", mythreadno, hnc_size, hnc_ttl);
5349 5353 }
5350 5354 }
5351 5355
5352 5356 static host_list_t *
5353 5357 hnc_lookup(struct netbuf *nbp, struct netconfig *ncp, int *hindex)
5354 5358 {
5355 5359 struct hostname_cache **hpp, *hp;
5356 5360 time_t now;
5357 5361 pthread_t mythreadno;
5358 5362 int index;
5359 5363
5360 5364 if (Debug) {
5361 5365 mythreadno = pthread_self();
5362 5366 }
5363 5367
5364 5368 if (hnc_cache == NULL) {
5365 5369 return (NULL);
5366 5370 }
5367 5371
5368 5372 (void) pthread_mutex_lock(&hnc_mutex);
5369 5373 now = time(0);
5370 5374
5371 5375 *hindex = index = addr_hash(nbp);
5372 5376
5373 5377 for (hpp = &hnc_cache[index]; (hp = *hpp) != NULL; ) {
5374 5378 DPRINT4(10, "hnc_lookup(%u): check %p on %p for %s\n",
5375 5379 mythreadno, (void *)hp->h, (void *)hp,
5376 5380 hp->h->hl_hosts[0]);
5377 5381
5378 5382 if (hp->expire < now) {
5379 5383 DPRINT2(9, "hnc_lookup(%u): purge %p\n",
5380 5384 mythreadno, (void *)hp);
5381 5385 hnc_unreg(hpp);
5382 5386 continue;
5383 5387 }
5384 5388
5385 5389 if (ncp == hp->ncp && same_addr(&hp->addr, nbp)) {
5386 5390 /*
5387 5391 * found!
5388 5392 * Put the entry at the top.
5389 5393 */
5390 5394
5391 5395 if (hp != hnc_cache[index]) {
5392 5396 /* unlink from active list */
5393 5397 *hpp = (*hpp)->next;
5394 5398 /* push it onto the top */
5395 5399 hp->next = hnc_cache[index];
5396 5400 hnc_cache[index] = hp;
5397 5401 }
5398 5402
5399 5403 (void) pthread_mutex_lock(&hp->h->hl_mutex);
5400 5404 hp->h->hl_refcnt++;
5401 5405 (void) pthread_mutex_unlock(&hp->h->hl_mutex);
5402 5406
5403 5407 DPRINT4(9, "hnc_lookup(%u): found %p on %p for %s\n",
5404 5408 mythreadno, (void *)hp->h, (void *)hp,
5405 5409 hp->h->hl_hosts[0]);
5406 5410
5407 5411 (void) pthread_mutex_unlock(&hnc_mutex);
5408 5412 return (hp->h);
5409 5413 }
5410 5414
5411 5415 hpp = &hp->next;
5412 5416 }
5413 5417
5414 5418 (void) pthread_mutex_unlock(&hnc_mutex);
5415 5419 return (NULL);
5416 5420 }
5417 5421
5418 5422 static void
5419 5423 hnc_register(struct netbuf *nbp, struct netconfig *ncp,
5420 5424 host_list_t *h, int hindex)
5421 5425 {
5422 5426 struct hostname_cache **hpp, **tailp, *hp, *entry;
5423 5427 void *addrbuf;
5424 5428 time_t now;
5425 5429 pthread_t mythreadno;
5426 5430 int i;
5427 5431
5428 5432 if (Debug) {
5429 5433 mythreadno = pthread_self();
5430 5434 }
5431 5435
5432 5436 if (hnc_cache == NULL) {
5433 5437 return;
5434 5438 }
5435 5439
5436 5440 if ((addrbuf = malloc(nbp->len)) == NULL) {
5437 5441 MALLOC_FAIL("pushing hostname cache");
5438 5442 return;
5439 5443 }
5440 5444
5441 5445 if ((entry = malloc(sizeof (struct hostname_cache))) == NULL) {
5442 5446 MALLOC_FAIL("pushing hostname entry");
5443 5447 free(addrbuf);
5444 5448 return;
5445 5449 }
5446 5450
5447 5451 (void) pthread_mutex_lock(&hnc_mutex);
5448 5452
5449 5453 i = 0;
5450 5454
5451 5455 now = time(0);
5452 5456 /*
5453 5457 * first go through active list, and discard the
5454 5458 * caches which has been invalid. Count number of
5455 5459 * non-expired buckets.
5456 5460 */
5457 5461
5458 5462 for (hpp = &hnc_cache[hindex]; (hp = *hpp) != NULL; ) {
5459 5463 tailp = hpp;
5460 5464
5461 5465 if (hp->expire < now) {
5462 5466 DPRINT2(9, "hnc_register(%u): discard %p\n",
5463 5467 mythreadno, (void *)hp);
5464 5468 hnc_unreg(hpp);
5465 5469 } else {
5466 5470 i++;
5467 5471 hpp = &hp->next;
5468 5472 }
5469 5473 }
5470 5474
5471 5475 /*
5472 5476 * If max limit of chained hash buckets has been used up
5473 5477 * delete the least active element in the chain.
5474 5478 */
5475 5479 if (i == MAX_BUCKETS) {
5476 5480 hnc_unreg(tailp);
5477 5481 }
5478 5482
5479 5483 (void) memcpy(addrbuf, nbp->buf, nbp->len);
5480 5484 entry->addr.len = nbp->len;
5481 5485 entry->addr.buf = addrbuf;
5482 5486 entry->ncp = ncp;
5483 5487 entry->h = h;
5484 5488 entry->expire = time(NULL) + hnc_ttl;
5485 5489
5486 5490 /* insert it at the top */
5487 5491 entry->next = hnc_cache[hindex];
5488 5492 hnc_cache[hindex] = entry;
5489 5493
5490 5494 /*
5491 5495 * As far as cache is valid, corresponding host_list must
5492 5496 * also be valid. Increments the refcnt to avoid freeing
5493 5497 * host_list.
5494 5498 */
5495 5499 h->hl_refcnt++;
5496 5500 DPRINT4(9, "hnc_register(%u): reg %p onto %p for %s\n",
5497 5501 mythreadno, (void *)entry->h, (void *)entry, entry->h->hl_hosts[0]);
5498 5502 (void) pthread_mutex_unlock(&hnc_mutex);
5499 5503 }
5500 5504
5501 5505 static void
5502 5506 hnc_unreg(struct hostname_cache **hpp)
5503 5507 {
5504 5508 struct hostname_cache *hp = *hpp;
5505 5509 pthread_t mythreadno;
5506 5510
5507 5511 if (Debug) {
5508 5512 mythreadno = pthread_self();
5509 5513 }
5510 5514
5511 5515 DPRINT4(9, "hnc_unreg(%u): unreg %p on %p for %s\n",
5512 5516 mythreadno, (void *)hp->h, (void *)hp, hp->h->hl_hosts[0]);
5513 5517 free(hp->addr.buf);
5514 5518 freehl(hp->h);
5515 5519
5516 5520 /* unlink from active list */
5517 5521 *hpp = (*hpp)->next;
5518 5522
5519 5523 free(hp);
5520 5524 }
5521 5525
5522 5526 /*
5523 5527 * Once this is called, error messages through logerror() will go to
5524 5528 * the console immediately. Also, messages are queued into the tmpq
5525 5529 * to be able to later put them into inputq.
5526 5530 */
5527 5531 static void
5528 5532 disable_errorlog()
5529 5533 {
5530 5534 (void) dataq_init(&tmpq);
5531 5535
5532 5536 (void) pthread_mutex_lock(&logerror_lock);
5533 5537 interrorlog = 0;
5534 5538 (void) pthread_mutex_unlock(&logerror_lock);
5535 5539 }
5536 5540
5537 5541 /*
5538 5542 * Turn internal error messages to regular input stream.
5539 5543 * All pending messages are pulled and pushed into the regular
5540 5544 * input queue.
5541 5545 */
5542 5546 static void
5543 5547 enable_errorlog()
5544 5548 {
5545 5549 log_message_t *mp;
5546 5550
5547 5551 (void) pthread_mutex_lock(&logerror_lock);
5548 5552 interrorlog = 1;
5549 5553 (void) pthread_mutex_unlock(&logerror_lock);
5550 5554
5551 5555 /*
5552 5556 * push all the pending messages into inputq.
5553 5557 */
5554 5558 while (dataq_dequeue(&tmpq, (void **)&mp, 1) == 0) {
5555 5559 (void) dataq_enqueue(&inputq, mp);
5556 5560 }
5557 5561 (void) dataq_destroy(&tmpq);
5558 5562 }
5559 5563
5560 5564 /*
5561 5565 * Generate a hash value of the given address and derive
5562 5566 * an index into the hnc_cache hashtable.
5563 5567 * The hashing method is similar to what Java does for strings.
5564 5568 */
5565 5569 static int
5566 5570 addr_hash(struct netbuf *nbp)
5567 5571 {
5568 5572 char *uap;
5569 5573 int i;
5570 5574 unsigned long hcode = 0;
5571 5575
5572 5576 uap = nbp->buf;
5573 5577
5574 5578 if (uap == NULL) {
5575 5579 return (0);
5576 5580 }
5577 5581
5578 5582 /*
5579 5583 * Compute a hashcode of the address string
5580 5584 */
5581 5585 for (i = 0; i < nbp->len; i++)
5582 5586 hcode = (31 * hcode) + uap[i];
5583 5587
5584 5588 /*
5585 5589 * Scramble the hashcode for better distribution
5586 5590 */
5587 5591 hcode += ~(hcode << 9);
5588 5592 hcode ^= (hcode >> 14);
5589 5593 hcode += (hcode << 4);
5590 5594 hcode ^= (hcode >> 10);
5591 5595
5592 5596 return ((int)(hcode % hnc_size));
5593 5597 }
↓ open down ↓ |
4885 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX