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