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