1*03704f43SChristopher Simons /* $NetBSD: logger.c,v 1.17 2012/04/27 06:30:48 wiz Exp $ */
2*03704f43SChristopher Simons
3*03704f43SChristopher Simons /*
4*03704f43SChristopher Simons * Copyright (c) 1983, 1993
5*03704f43SChristopher Simons * The Regents of the University of California. All rights reserved.
6*03704f43SChristopher Simons *
7*03704f43SChristopher Simons * Redistribution and use in source and binary forms, with or without
8*03704f43SChristopher Simons * modification, are permitted provided that the following conditions
9*03704f43SChristopher Simons * are met:
10*03704f43SChristopher Simons * 1. Redistributions of source code must retain the above copyright
11*03704f43SChristopher Simons * notice, this list of conditions and the following disclaimer.
12*03704f43SChristopher Simons * 2. Redistributions in binary form must reproduce the above copyright
13*03704f43SChristopher Simons * notice, this list of conditions and the following disclaimer in the
14*03704f43SChristopher Simons * documentation and/or other materials provided with the distribution.
15*03704f43SChristopher Simons * 3. Neither the name of the University nor the names of its contributors
16*03704f43SChristopher Simons * may be used to endorse or promote products derived from this software
17*03704f43SChristopher Simons * without specific prior written permission.
18*03704f43SChristopher Simons *
19*03704f43SChristopher Simons * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20*03704f43SChristopher Simons * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21*03704f43SChristopher Simons * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22*03704f43SChristopher Simons * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23*03704f43SChristopher Simons * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24*03704f43SChristopher Simons * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25*03704f43SChristopher Simons * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26*03704f43SChristopher Simons * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27*03704f43SChristopher Simons * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28*03704f43SChristopher Simons * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29*03704f43SChristopher Simons * SUCH DAMAGE.
30*03704f43SChristopher Simons */
31*03704f43SChristopher Simons
32*03704f43SChristopher Simons #include <sys/cdefs.h>
33*03704f43SChristopher Simons #ifndef lint
34*03704f43SChristopher Simons __COPYRIGHT("@(#) Copyright (c) 1983, 1993\
35*03704f43SChristopher Simons The Regents of the University of California. All rights reserved.");
36*03704f43SChristopher Simons #endif /* not lint */
37*03704f43SChristopher Simons
38*03704f43SChristopher Simons #ifndef lint
39*03704f43SChristopher Simons #if 0
40*03704f43SChristopher Simons static char sccsid[] = "@(#)logger.c 8.1 (Berkeley) 6/6/93";
41*03704f43SChristopher Simons #endif
42*03704f43SChristopher Simons __RCSID("$NetBSD: logger.c,v 1.17 2012/04/27 06:30:48 wiz Exp $");
43*03704f43SChristopher Simons #endif /* not lint */
44*03704f43SChristopher Simons
45*03704f43SChristopher Simons #include <errno.h>
46*03704f43SChristopher Simons #include <unistd.h>
47*03704f43SChristopher Simons #include <stdlib.h>
48*03704f43SChristopher Simons #include <stdio.h>
49*03704f43SChristopher Simons #include <ctype.h>
50*03704f43SChristopher Simons #include <string.h>
51*03704f43SChristopher Simons #include <err.h>
52*03704f43SChristopher Simons
53*03704f43SChristopher Simons #define SYSLOG_NAMES
54*03704f43SChristopher Simons #include <syslog.h>
55*03704f43SChristopher Simons
56*03704f43SChristopher Simons static int decode(const char *, const CODE *);
57*03704f43SChristopher Simons static int pencode(char *);
58*03704f43SChristopher Simons __dead static void usage(void);
59*03704f43SChristopher Simons
60*03704f43SChristopher Simons /*
61*03704f43SChristopher Simons * logger -- read and log utility
62*03704f43SChristopher Simons *
63*03704f43SChristopher Simons * Reads from an input and arranges to write the result on the system
64*03704f43SChristopher Simons * log.
65*03704f43SChristopher Simons */
66*03704f43SChristopher Simons int
main(int argc,char * argv[])67*03704f43SChristopher Simons main(int argc, char *argv[])
68*03704f43SChristopher Simons {
69*03704f43SChristopher Simons int ch, logflags, pri;
70*03704f43SChristopher Simons const char *tag;
71*03704f43SChristopher Simons const char *sd = "-";
72*03704f43SChristopher Simons const char *msgid = "-";
73*03704f43SChristopher Simons char buf[1024];
74*03704f43SChristopher Simons
75*03704f43SChristopher Simons tag = NULL;
76*03704f43SChristopher Simons pri = LOG_NOTICE;
77*03704f43SChristopher Simons logflags = 0;
78*03704f43SChristopher Simons while ((ch = getopt(argc, argv, "cd:f:im:np:st:")) != -1)
79*03704f43SChristopher Simons switch((char)ch) {
80*03704f43SChristopher Simons case 'c': /* log to console */
81*03704f43SChristopher Simons logflags |= LOG_CONS;
82*03704f43SChristopher Simons break;
83*03704f43SChristopher Simons case 'd': /* structured data field */
84*03704f43SChristopher Simons sd = optarg;
85*03704f43SChristopher Simons break;
86*03704f43SChristopher Simons case 'f': /* file to log */
87*03704f43SChristopher Simons if (freopen(optarg, "r", stdin) == NULL)
88*03704f43SChristopher Simons err(EXIT_FAILURE, "%s", optarg);
89*03704f43SChristopher Simons break;
90*03704f43SChristopher Simons case 'i': /* log process id also */
91*03704f43SChristopher Simons logflags |= LOG_PID;
92*03704f43SChristopher Simons break;
93*03704f43SChristopher Simons case 'm': /* msgid field */
94*03704f43SChristopher Simons msgid = optarg;
95*03704f43SChristopher Simons break;
96*03704f43SChristopher Simons case 'n': /* open log file immediately */
97*03704f43SChristopher Simons logflags |= LOG_NDELAY;
98*03704f43SChristopher Simons break;
99*03704f43SChristopher Simons case 'p': /* priority */
100*03704f43SChristopher Simons pri = pencode(optarg);
101*03704f43SChristopher Simons break;
102*03704f43SChristopher Simons case 's': /* log to standard error */
103*03704f43SChristopher Simons logflags |= LOG_PERROR;
104*03704f43SChristopher Simons break;
105*03704f43SChristopher Simons case 't': /* tag */
106*03704f43SChristopher Simons tag = optarg;
107*03704f43SChristopher Simons break;
108*03704f43SChristopher Simons case '?':
109*03704f43SChristopher Simons default:
110*03704f43SChristopher Simons usage();
111*03704f43SChristopher Simons }
112*03704f43SChristopher Simons argc -= optind;
113*03704f43SChristopher Simons argv += optind;
114*03704f43SChristopher Simons
115*03704f43SChristopher Simons /* setup for logging */
116*03704f43SChristopher Simons openlog(tag != NULL ? tag : getlogin(), logflags, 0);
117*03704f43SChristopher Simons (void)fclose(stdout);
118*03704f43SChristopher Simons
119*03704f43SChristopher Simons /* log input line if appropriate */
120*03704f43SChristopher Simons if (argc > 0) {
121*03704f43SChristopher Simons char *p, *endp;
122*03704f43SChristopher Simons size_t len;
123*03704f43SChristopher Simons
124*03704f43SChristopher Simons for (p = buf, endp = buf + sizeof(buf) - 2; *argv != NULL;) {
125*03704f43SChristopher Simons len = strlen(*argv);
126*03704f43SChristopher Simons if (p + len > endp && p > buf) {
127*03704f43SChristopher Simons syslogp(pri, msgid, sd, "%s", buf);
128*03704f43SChristopher Simons p = buf;
129*03704f43SChristopher Simons }
130*03704f43SChristopher Simons if (len > sizeof(buf) - 1)
131*03704f43SChristopher Simons syslogp(pri, msgid, sd, "%s", *argv++);
132*03704f43SChristopher Simons else {
133*03704f43SChristopher Simons if (p != buf)
134*03704f43SChristopher Simons *p++ = ' ';
135*03704f43SChristopher Simons memmove(p, *argv++, len);
136*03704f43SChristopher Simons *(p += len) = '\0';
137*03704f43SChristopher Simons }
138*03704f43SChristopher Simons }
139*03704f43SChristopher Simons if (p != buf)
140*03704f43SChristopher Simons syslogp(pri, msgid, sd, "%s", buf);
141*03704f43SChristopher Simons } else /* TODO: allow syslog-protocol messages from file/stdin
142*03704f43SChristopher Simons * but that will require parsing the line to split
143*03704f43SChristopher Simons * it into three fields.
144*03704f43SChristopher Simons */
145*03704f43SChristopher Simons while (fgets(buf, sizeof(buf), stdin) != NULL)
146*03704f43SChristopher Simons syslogp(pri, msgid, sd, "%s", buf);
147*03704f43SChristopher Simons
148*03704f43SChristopher Simons exit(EXIT_SUCCESS);
149*03704f43SChristopher Simons /* NOTREACHED */
150*03704f43SChristopher Simons }
151*03704f43SChristopher Simons
152*03704f43SChristopher Simons /*
153*03704f43SChristopher Simons * Decode a symbolic name to a numeric value
154*03704f43SChristopher Simons */
155*03704f43SChristopher Simons static int
pencode(char * s)156*03704f43SChristopher Simons pencode(char *s)
157*03704f43SChristopher Simons {
158*03704f43SChristopher Simons char *save;
159*03704f43SChristopher Simons int fac, lev;
160*03704f43SChristopher Simons
161*03704f43SChristopher Simons for (save = s; *s != '\0' && *s != '.'; ++s)
162*03704f43SChristopher Simons ;
163*03704f43SChristopher Simons if (*s != '\0') {
164*03704f43SChristopher Simons *s = '\0';
165*03704f43SChristopher Simons fac = decode(save, facilitynames);
166*03704f43SChristopher Simons if (fac < 0)
167*03704f43SChristopher Simons errx(EXIT_FAILURE, "unknown facility name: %s", save);
168*03704f43SChristopher Simons *s++ = '.';
169*03704f43SChristopher Simons } else {
170*03704f43SChristopher Simons fac = 0;
171*03704f43SChristopher Simons s = save;
172*03704f43SChristopher Simons }
173*03704f43SChristopher Simons lev = decode(s, prioritynames);
174*03704f43SChristopher Simons if (lev < 0)
175*03704f43SChristopher Simons errx(EXIT_FAILURE, "unknown priority name: %s", s);
176*03704f43SChristopher Simons return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK));
177*03704f43SChristopher Simons }
178*03704f43SChristopher Simons
179*03704f43SChristopher Simons static int
decode(const char * name,const CODE * codetab)180*03704f43SChristopher Simons decode(const char *name, const CODE *codetab)
181*03704f43SChristopher Simons {
182*03704f43SChristopher Simons const CODE *c;
183*03704f43SChristopher Simons
184*03704f43SChristopher Simons if (isdigit((unsigned char)*name))
185*03704f43SChristopher Simons return (atoi(name));
186*03704f43SChristopher Simons
187*03704f43SChristopher Simons for (c = codetab; c->c_name != NULL; c++)
188*03704f43SChristopher Simons if (strcasecmp(name, c->c_name) == 0)
189*03704f43SChristopher Simons return (c->c_val);
190*03704f43SChristopher Simons
191*03704f43SChristopher Simons return (-1);
192*03704f43SChristopher Simons }
193*03704f43SChristopher Simons
194*03704f43SChristopher Simons static void
usage(void)195*03704f43SChristopher Simons usage(void)
196*03704f43SChristopher Simons {
197*03704f43SChristopher Simons
198*03704f43SChristopher Simons (void)fprintf(stderr,
199*03704f43SChristopher Simons "Usage: %s [-cins] [-d SD] [-f file] [-m msgid] "
200*03704f43SChristopher Simons "[-p pri] [-t tag] [message ...]\n",
201*03704f43SChristopher Simons getprogname());
202*03704f43SChristopher Simons exit(EXIT_FAILURE);
203*03704f43SChristopher Simons }
204