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