]> git.ipfire.org Git - thirdparty/util-linux.git/blob - misc-utils/logger.c
e3b67d2a2029153aa84fb621a985a500d5ca1579
[thirdparty/util-linux.git] / misc-utils / logger.c
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 * 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@pld.ORG.PL>
34 * - added Native Language Support
35 * Sun Mar 21 1999 - Arnaldo Carvalho de Melo <acme@conectiva.com.br>
36 * - fixed strerr(errno) in gettext calls
37 */
38
39 #include <errno.h>
40 #include <unistd.h>
41 #include <stdlib.h>
42 #include <time.h>
43 #include <stdio.h>
44 #include <ctype.h>
45 #include <string.h>
46 #include <strings.h>
47 #include <sys/types.h>
48 #include <sys/socket.h>
49 #include <sys/un.h>
50 #include <arpa/inet.h>
51 #include <netdb.h>
52 #include <getopt.h>
53
54 #include "c.h"
55 #include "closestream.h"
56 #include "nls.h"
57 #include "strutils.h"
58
59 #define SYSLOG_NAMES
60 #include <syslog.h>
61
62 int decode __P((char *, CODE *));
63 int pencode __P((char *));
64
65 static int optd = 0;
66 static int udpport = 514;
67
68 static int
69 myopenlog(const char *sock) {
70 int fd;
71 static struct sockaddr_un s_addr; /* AF_UNIX address of local logger */
72
73 if (strlen(sock) >= sizeof(s_addr.sun_path))
74 errx(EXIT_FAILURE, _("openlog %s: pathname too long"), sock);
75
76 s_addr.sun_family = AF_UNIX;
77 (void)strcpy(s_addr.sun_path, sock);
78
79 if ((fd = socket(AF_UNIX, optd ? SOCK_DGRAM : SOCK_STREAM, 0)) == -1)
80 err(EXIT_FAILURE, _("socket %s"), sock);
81
82 if (connect(fd, (struct sockaddr *) &s_addr, sizeof(s_addr)) == -1)
83 err(EXIT_FAILURE, _("connect %s"), sock);
84
85 return fd;
86 }
87
88 static int
89 udpopenlog(const char *servername,int port) {
90 int fd;
91 struct sockaddr_in s_addr;
92 struct hostent *serverhost;
93
94 if ((serverhost = gethostbyname(servername)) == NULL )
95 errx(EXIT_FAILURE, _("unable to resolve '%s'"), servername);
96
97 if ((fd = socket(AF_INET, SOCK_DGRAM , 0)) == -1)
98 err(EXIT_FAILURE, _("socket"));
99
100 bcopy(serverhost->h_addr,&s_addr.sin_addr,serverhost->h_length);
101 s_addr.sin_family=AF_INET;
102 s_addr.sin_port=htons(port);
103
104 if (connect(fd, (struct sockaddr *) &s_addr, sizeof(s_addr)) == -1)
105 err(EXIT_FAILURE, _("connect"));
106
107 return fd;
108 }
109 static void
110 mysyslog(int fd, int logflags, int pri, char *tag, char *msg) {
111 char buf[1000], pid[30], *cp, *tp;
112 time_t now;
113
114 if (fd > -1) {
115 if (logflags & LOG_PID)
116 snprintf (pid, sizeof(pid), "[%d]", getpid());
117 else
118 pid[0] = 0;
119 if (tag)
120 cp = tag;
121 else {
122 cp = getlogin();
123 if (!cp)
124 cp = "<someone>";
125 }
126 (void)time(&now);
127 tp = ctime(&now)+4;
128
129 snprintf(buf, sizeof(buf), "<%d>%.15s %.200s%s: %.400s",
130 pri, tp, cp, pid, msg);
131
132 if (write(fd, buf, strlen(buf)+1) < 0)
133 return; /* error */
134 }
135 }
136
137 static void __attribute__ ((__noreturn__)) usage(FILE *out)
138 {
139 fputs(_("\nUsage:\n"), out);
140 fprintf(out,
141 _(" %s [options] [message]\n"), program_invocation_short_name);
142
143 fputs(_("\nOptions:\n"), out);
144 fputs(_(" -d, --udp use UDP (TCP is default)\n"
145 " -i, --id log the process ID too\n"
146 " -f, --file <file> log the contents of this file\n"
147 " -h, --help display this help text and exit\n"), out);
148 fputs(_(" -n, --server <name> write to this remote syslog server\n"
149 " -P, --port <number> use this UDP port\n"
150 " -p, --priority <prio> mark given message with this priority\n"
151 " -s, --stderr output message to standard error as well\n"), out);
152 fputs(_(" -t, --tag <tag> mark every line with this tag\n"
153 " -u, --socket <socket> write to this Unix socket\n"
154 " -V, --version output version information and exit\n\n"), out);
155
156 exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
157 }
158
159 /*
160 * logger -- read and log utility
161 *
162 * Reads from an input and arranges to write the result on the system
163 * log.
164 */
165 int
166 main(int argc, char **argv) {
167 int ch, logflags, pri;
168 char *tag, buf[1024];
169 char *usock = NULL;
170 char *udpserver = NULL;
171 int LogSock = -1;
172 long tmpport;
173
174 static const struct option longopts[] = {
175 { "id", no_argument, 0, 'i' },
176 { "stderr", no_argument, 0, 's' },
177 { "file", required_argument, 0, 'f' },
178 { "priority", required_argument, 0, 'p' },
179 { "tag", required_argument, 0, 't' },
180 { "socket", required_argument, 0, 'u' },
181 { "udp", no_argument, 0, 'd' },
182 { "server", required_argument, 0, 'n' },
183 { "port", required_argument, 0, 'P' },
184 { "version", no_argument, 0, 'V' },
185 { "help", no_argument, 0, 'h' },
186 { NULL, 0, 0, 0 }
187 };
188
189 setlocale(LC_ALL, "");
190 bindtextdomain(PACKAGE, LOCALEDIR);
191 textdomain(PACKAGE);
192 atexit(close_stdout);
193
194 tag = NULL;
195 pri = LOG_NOTICE;
196 logflags = 0;
197 while ((ch = getopt_long(argc, argv, "f:ip:st:u:dn:P:Vh",
198 longopts, NULL)) != -1) {
199 switch((char)ch) {
200 case 'f': /* file to log */
201 if (freopen(optarg, "r", stdin) == NULL)
202 err(EXIT_FAILURE, _("file %s"),
203 optarg);
204 break;
205 case 'i': /* log process id also */
206 logflags |= LOG_PID;
207 break;
208 case 'p': /* priority */
209 pri = pencode(optarg);
210 break;
211 case 's': /* log to standard error */
212 logflags |= LOG_PERROR;
213 break;
214 case 't': /* tag */
215 tag = optarg;
216 break;
217 case 'u': /* unix socket */
218 usock = optarg;
219 break;
220 case 'd':
221 optd = 1; /* use datagrams */
222 break;
223 case 'n': /* udp socket */
224 optd = 1; /* use datagrams because udp */
225 udpserver = optarg;
226 break;
227 case 'P': /* change udp port */
228 tmpport = strtol_or_err(optarg,
229 _("failed to parse port number"));
230 if (tmpport < 0 || 65535 < tmpport)
231 errx(EXIT_FAILURE, _("port `%ld' out of range"),
232 tmpport);
233 udpport = (int) tmpport;
234 break;
235 case 'V':
236 printf(_("%s from %s\n"), program_invocation_short_name,
237 PACKAGE_STRING);
238 exit(EXIT_SUCCESS);
239 case 'h':
240 usage(stdout);
241 case '?':
242 default:
243 usage(stderr);
244 }
245 }
246 argc -= optind;
247 argv += optind;
248
249 /* setup for logging */
250 if (!usock && !udpserver)
251 openlog(tag ? tag : getlogin(), logflags, 0);
252 else if (udpserver)
253 LogSock = udpopenlog(udpserver,udpport);
254 else
255 LogSock = myopenlog(usock);
256
257 (void) fclose(stdout);
258
259 /* log input line if appropriate */
260 if (argc > 0) {
261 register char *p, *endp;
262 size_t len;
263
264 for (p = buf, endp = buf + sizeof(buf) - 2; *argv;) {
265 len = strlen(*argv);
266 if (p + len > endp && p > buf) {
267 if (!usock && !udpserver)
268 syslog(pri, "%s", buf);
269 else
270 mysyslog(LogSock, logflags, pri, tag, buf);
271 p = buf;
272 }
273 if (len > sizeof(buf) - 1) {
274 if (!usock && !udpserver)
275 syslog(pri, "%s", *argv++);
276 else
277 mysyslog(LogSock, logflags, pri, tag, *argv++);
278 } else {
279 if (p != buf)
280 *p++ = ' ';
281 memmove(p, *argv++, len);
282 *(p += len) = '\0';
283 }
284 }
285 if (p != buf) {
286 if (!usock && !udpserver)
287 syslog(pri, "%s", buf);
288 else
289 mysyslog(LogSock, logflags, pri, tag, buf);
290 }
291 } else {
292 while (fgets(buf, sizeof(buf), stdin) != NULL) {
293 /* glibc is buggy and adds an additional newline,
294 so we have to remove it here until glibc is fixed */
295 int len = strlen(buf);
296
297 if (len > 0 && buf[len - 1] == '\n')
298 buf[len - 1] = '\0';
299
300 if (!usock && !udpserver)
301 syslog(pri, "%s", buf);
302 else
303 mysyslog(LogSock, logflags, pri, tag, buf);
304 }
305 }
306 if (!usock && !udpserver)
307 closelog();
308 else
309 close(LogSock);
310
311 return EXIT_SUCCESS;
312 }
313
314 /*
315 * Decode a symbolic name to a numeric value
316 */
317 int
318 pencode(char *s)
319 {
320 char *save;
321 int fac, lev;
322
323 for (save = s; *s && *s != '.'; ++s);
324 if (*s) {
325 *s = '\0';
326 fac = decode(save, facilitynames);
327 if (fac < 0)
328 errx(EXIT_FAILURE,
329 _("unknown facility name: %s."), save);
330 *s++ = '.';
331 }
332 else {
333 fac = LOG_USER;
334 s = save;
335 }
336 lev = decode(s, prioritynames);
337 if (lev < 0)
338 errx(EXIT_FAILURE,
339 _("unknown priority name: %s."), save);
340 return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK));
341 }
342
343 int
344 decode(char *name, CODE *codetab)
345 {
346 register CODE *c;
347
348 if (isdigit(*name))
349 return (atoi(name));
350
351 for (c = codetab; c->c_name; c++)
352 if (!strcasecmp(name, c->c_name))
353 return (c->c_val);
354
355 return (-1);
356 }