1*70c9435fSwiz /* $NetBSD: logger.c,v 1.17 2012/04/27 06:30:48 wiz Exp $ */
27f8ee3fcSjtc
361f28255Scgd /*
47f8ee3fcSjtc * Copyright (c) 1983, 1993
57f8ee3fcSjtc * The Regents of the University of California. All rights reserved.
661f28255Scgd *
761f28255Scgd * Redistribution and use in source and binary forms, with or without
861f28255Scgd * modification, are permitted provided that the following conditions
961f28255Scgd * are met:
1061f28255Scgd * 1. Redistributions of source code must retain the above copyright
1161f28255Scgd * notice, this list of conditions and the following disclaimer.
1261f28255Scgd * 2. Redistributions in binary form must reproduce the above copyright
1361f28255Scgd * notice, this list of conditions and the following disclaimer in the
1461f28255Scgd * documentation and/or other materials provided with the distribution.
1589aaa1bbSagc * 3. Neither the name of the University nor the names of its contributors
1661f28255Scgd * may be used to endorse or promote products derived from this software
1761f28255Scgd * without specific prior written permission.
1861f28255Scgd *
1961f28255Scgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2061f28255Scgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2161f28255Scgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2261f28255Scgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2361f28255Scgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2461f28255Scgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2561f28255Scgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2661f28255Scgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2761f28255Scgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2861f28255Scgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2961f28255Scgd * SUCH DAMAGE.
3061f28255Scgd */
3161f28255Scgd
322dfca6c3Slukem #include <sys/cdefs.h>
3361f28255Scgd #ifndef lint
3498e5374cSlukem __COPYRIGHT("@(#) Copyright (c) 1983, 1993\
3598e5374cSlukem The Regents of the University of California. All rights reserved.");
3661f28255Scgd #endif /* not lint */
3761f28255Scgd
3861f28255Scgd #ifndef lint
397f8ee3fcSjtc #if 0
407f8ee3fcSjtc static char sccsid[] = "@(#)logger.c 8.1 (Berkeley) 6/6/93";
417f8ee3fcSjtc #endif
42*70c9435fSwiz __RCSID("$NetBSD: logger.c,v 1.17 2012/04/27 06:30:48 wiz Exp $");
4361f28255Scgd #endif /* not lint */
4461f28255Scgd
457f8ee3fcSjtc #include <errno.h>
467f8ee3fcSjtc #include <unistd.h>
477f8ee3fcSjtc #include <stdlib.h>
4861f28255Scgd #include <stdio.h>
497f8ee3fcSjtc #include <ctype.h>
507f8ee3fcSjtc #include <string.h>
5194fcdfbaSad #include <err.h>
527f8ee3fcSjtc
53e0be09eaSderaadt #define SYSLOG_NAMES
5461f28255Scgd #include <syslog.h>
557f8ee3fcSjtc
5692486326Sjoerg static int decode(const char *, const CODE *);
5792486326Sjoerg static int pencode(char *);
5892486326Sjoerg __dead static void usage(void);
5961f28255Scgd
6061f28255Scgd /*
617f8ee3fcSjtc * logger -- read and log utility
627f8ee3fcSjtc *
637f8ee3fcSjtc * Reads from an input and arranges to write the result on the system
647f8ee3fcSjtc * log.
6561f28255Scgd */
667f8ee3fcSjtc int
main(int argc,char * argv[])6794fcdfbaSad main(int argc, char *argv[])
6861f28255Scgd {
697f8ee3fcSjtc int ch, logflags, pri;
7094fcdfbaSad const char *tag;
711c6aec20Schristos const char *sd = "-";
721c6aec20Schristos const char *msgid = "-";
7394fcdfbaSad char buf[1024];
7461f28255Scgd
7561f28255Scgd tag = NULL;
767f8ee3fcSjtc pri = LOG_NOTICE;
777f8ee3fcSjtc logflags = 0;
78ed9eb316Schristos while ((ch = getopt(argc, argv, "cd:f:im:np:st:")) != -1)
7961f28255Scgd switch((char)ch) {
80ed9eb316Schristos case 'c': /* log to console */
81ed9eb316Schristos logflags |= LOG_CONS;
82ed9eb316Schristos break;
831c6aec20Schristos case 'd': /* structured data field */
841c6aec20Schristos sd = optarg;
851c6aec20Schristos break;
8661f28255Scgd case 'f': /* file to log */
8794fcdfbaSad if (freopen(optarg, "r", stdin) == NULL)
8894fcdfbaSad err(EXIT_FAILURE, "%s", optarg);
8961f28255Scgd break;
9061f28255Scgd case 'i': /* log process id also */
9161f28255Scgd logflags |= LOG_PID;
9261f28255Scgd break;
931c6aec20Schristos case 'm': /* msgid field */
941c6aec20Schristos msgid = optarg;
951c6aec20Schristos break;
96ed9eb316Schristos case 'n': /* open log file immediately */
97ed9eb316Schristos logflags |= LOG_NDELAY;
98ed9eb316Schristos break;
9961f28255Scgd case 'p': /* priority */
10061f28255Scgd pri = pencode(optarg);
10161f28255Scgd break;
10261f28255Scgd case 's': /* log to standard error */
10361f28255Scgd logflags |= LOG_PERROR;
10461f28255Scgd break;
10561f28255Scgd case 't': /* tag */
10661f28255Scgd tag = optarg;
10761f28255Scgd break;
10861f28255Scgd case '?':
10961f28255Scgd default:
11061f28255Scgd usage();
11161f28255Scgd }
11261f28255Scgd argc -= optind;
11361f28255Scgd argv += optind;
11461f28255Scgd
11561f28255Scgd /* setup for logging */
11694fcdfbaSad openlog(tag != NULL ? tag : getlogin(), logflags, 0);
11761f28255Scgd (void)fclose(stdout);
11861f28255Scgd
11961f28255Scgd /* log input line if appropriate */
12061f28255Scgd if (argc > 0) {
1212dfca6c3Slukem char *p, *endp;
122743d88d8Slukem size_t len;
12361f28255Scgd
12494fcdfbaSad for (p = buf, endp = buf + sizeof(buf) - 2; *argv != NULL;) {
12561f28255Scgd len = strlen(*argv);
12661f28255Scgd if (p + len > endp && p > buf) {
127d5e5a2c9Schristos syslogp(pri, msgid, sd, "%s", buf);
12861f28255Scgd p = buf;
12961f28255Scgd }
13061f28255Scgd if (len > sizeof(buf) - 1)
131d5e5a2c9Schristos syslogp(pri, msgid, sd, "%s", *argv++);
13261f28255Scgd else {
13361f28255Scgd if (p != buf)
13461f28255Scgd *p++ = ' ';
1352dfca6c3Slukem memmove(p, *argv++, len);
13661f28255Scgd *(p += len) = '\0';
13761f28255Scgd }
13861f28255Scgd }
13961f28255Scgd if (p != buf)
140d5e5a2c9Schristos syslogp(pri, msgid, sd, "%s", buf);
1411c6aec20Schristos } else /* TODO: allow syslog-protocol messages from file/stdin
1421c6aec20Schristos * but that will require parsing the line to split
1431c6aec20Schristos * it into three fields.
1441c6aec20Schristos */
14561f28255Scgd while (fgets(buf, sizeof(buf), stdin) != NULL)
146d5e5a2c9Schristos syslogp(pri, msgid, sd, "%s", buf);
14794fcdfbaSad
14894fcdfbaSad exit(EXIT_SUCCESS);
14994fcdfbaSad /* NOTREACHED */
15061f28255Scgd }
15161f28255Scgd
15261f28255Scgd /*
15361f28255Scgd * Decode a symbolic name to a numeric value
15461f28255Scgd */
15592486326Sjoerg static int
pencode(char * s)15694fcdfbaSad pencode(char *s)
15761f28255Scgd {
15861f28255Scgd char *save;
15961f28255Scgd int fac, lev;
16061f28255Scgd
16194fcdfbaSad for (save = s; *s != '\0' && *s != '.'; ++s)
16294fcdfbaSad ;
16394fcdfbaSad if (*s != '\0') {
16461f28255Scgd *s = '\0';
16561f28255Scgd fac = decode(save, facilitynames);
16694fcdfbaSad if (fac < 0)
16794fcdfbaSad errx(EXIT_FAILURE, "unknown facility name: %s", save);
16861f28255Scgd *s++ = '.';
16994fcdfbaSad } else {
17061f28255Scgd fac = 0;
17161f28255Scgd s = save;
17261f28255Scgd }
17361f28255Scgd lev = decode(s, prioritynames);
17494fcdfbaSad if (lev < 0)
17594fcdfbaSad errx(EXIT_FAILURE, "unknown priority name: %s", s);
17661f28255Scgd return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK));
17761f28255Scgd }
17861f28255Scgd
17992486326Sjoerg static int
decode(const char * name,const CODE * codetab)18094fcdfbaSad decode(const char *name, const CODE *codetab)
18161f28255Scgd {
18294fcdfbaSad const CODE *c;
18361f28255Scgd
184e8ab739aSchristos if (isdigit((unsigned char)*name))
18561f28255Scgd return (atoi(name));
18661f28255Scgd
18794fcdfbaSad for (c = codetab; c->c_name != NULL; c++)
18894fcdfbaSad if (strcasecmp(name, c->c_name) == 0)
18961f28255Scgd return (c->c_val);
19061f28255Scgd
19161f28255Scgd return (-1);
19261f28255Scgd }
19361f28255Scgd
19492486326Sjoerg static void
usage(void)19594fcdfbaSad usage(void)
19661f28255Scgd {
19794fcdfbaSad
19861f28255Scgd (void)fprintf(stderr,
199*70c9435fSwiz "Usage: %s [-cins] [-d SD] [-f file] [-m msgid] "
200*70c9435fSwiz "[-p pri] [-t tag] [message ...]\n",
201c2bdafabScgd getprogname());
20294fcdfbaSad exit(EXIT_FAILURE);
20361f28255Scgd }
204