2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
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
47 #include <sys/types.h>
48 #include <sys/socket.h>
50 #include <arpa/inet.h>
55 #include "closestream.h"
62 int decode
__P((char *, CODE
*));
63 int pencode
__P((char *));
66 static int udpport
= 514;
69 myopenlog(const char *sock
) {
71 static struct sockaddr_un s_addr
; /* AF_UNIX address of local logger */
73 if (strlen(sock
) >= sizeof(s_addr
.sun_path
))
74 errx(EXIT_FAILURE
, _("openlog %s: pathname too long"), sock
);
76 s_addr
.sun_family
= AF_UNIX
;
77 (void)strcpy(s_addr
.sun_path
, sock
);
79 if ((fd
= socket(AF_UNIX
, optd
? SOCK_DGRAM
: SOCK_STREAM
, 0)) == -1)
80 err(EXIT_FAILURE
, _("socket %s"), sock
);
82 if (connect(fd
, (struct sockaddr
*) &s_addr
, sizeof(s_addr
)) == -1)
83 err(EXIT_FAILURE
, _("connect %s"), sock
);
89 udpopenlog(const char *servername
,int port
) {
91 struct sockaddr_in s_addr
;
92 struct hostent
*serverhost
;
94 if ((serverhost
= gethostbyname(servername
)) == NULL
)
95 errx(EXIT_FAILURE
, _("unable to resolve '%s'"), servername
);
97 if ((fd
= socket(AF_INET
, SOCK_DGRAM
, 0)) == -1)
98 err(EXIT_FAILURE
, _("socket"));
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
);
104 if (connect(fd
, (struct sockaddr
*) &s_addr
, sizeof(s_addr
)) == -1)
105 err(EXIT_FAILURE
, _("connect"));
110 mysyslog(int fd
, int logflags
, int pri
, char *tag
, char *msg
) {
111 char buf
[1000], pid
[30], *cp
, *tp
;
115 if (logflags
& LOG_PID
)
116 snprintf (pid
, sizeof(pid
), "[%d]", getpid());
129 snprintf(buf
, sizeof(buf
), "<%d>%.15s %.200s%s: %.400s",
130 pri
, tp
, cp
, pid
, msg
);
132 if (write(fd
, buf
, strlen(buf
)+1) < 0)
137 static void __attribute__ ((__noreturn__
)) usage(FILE *out
)
139 fputs(_("\nUsage:\n"), out
);
141 _(" %s [options] [message]\n"), program_invocation_short_name
);
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
);
156 exit(out
== stderr
? EXIT_FAILURE
: EXIT_SUCCESS
);
160 * logger -- read and log utility
162 * Reads from an input and arranges to write the result on the system
166 main(int argc
, char **argv
) {
167 int ch
, logflags
, pri
;
168 char *tag
, buf
[1024];
170 char *udpserver
= NULL
;
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' },
189 setlocale(LC_ALL
, "");
190 bindtextdomain(PACKAGE
, LOCALEDIR
);
192 atexit(close_stdout
);
197 while ((ch
= getopt_long(argc
, argv
, "f:ip:st:u:dn:P:Vh",
198 longopts
, NULL
)) != -1) {
200 case 'f': /* file to log */
201 if (freopen(optarg
, "r", stdin
) == NULL
)
202 err(EXIT_FAILURE
, _("file %s"),
205 case 'i': /* log process id also */
208 case 'p': /* priority */
209 pri
= pencode(optarg
);
211 case 's': /* log to standard error */
212 logflags
|= LOG_PERROR
;
217 case 'u': /* unix socket */
221 optd
= 1; /* use datagrams */
223 case 'n': /* udp socket */
224 optd
= 1; /* use datagrams because udp */
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"),
233 udpport
= (int) tmpport
;
236 printf(_("%s from %s\n"), program_invocation_short_name
,
249 /* setup for logging */
250 if (!usock
&& !udpserver
)
251 openlog(tag
? tag
: getlogin(), logflags
, 0);
253 LogSock
= udpopenlog(udpserver
,udpport
);
255 LogSock
= myopenlog(usock
);
257 (void) fclose(stdout
);
259 /* log input line if appropriate */
261 register char *p
, *endp
;
264 for (p
= buf
, endp
= buf
+ sizeof(buf
) - 2; *argv
;) {
266 if (p
+ len
> endp
&& p
> buf
) {
267 if (!usock
&& !udpserver
)
268 syslog(pri
, "%s", buf
);
270 mysyslog(LogSock
, logflags
, pri
, tag
, buf
);
273 if (len
> sizeof(buf
) - 1) {
274 if (!usock
&& !udpserver
)
275 syslog(pri
, "%s", *argv
++);
277 mysyslog(LogSock
, logflags
, pri
, tag
, *argv
++);
281 memmove(p
, *argv
++, len
);
286 if (!usock
&& !udpserver
)
287 syslog(pri
, "%s", buf
);
289 mysyslog(LogSock
, logflags
, pri
, tag
, buf
);
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
);
297 if (len
> 0 && buf
[len
- 1] == '\n')
300 if (!usock
&& !udpserver
)
301 syslog(pri
, "%s", buf
);
303 mysyslog(LogSock
, logflags
, pri
, tag
, buf
);
306 if (!usock
&& !udpserver
)
315 * Decode a symbolic name to a numeric value
323 for (save
= s
; *s
&& *s
!= '.'; ++s
);
326 fac
= decode(save
, facilitynames
);
329 _("unknown facility name: %s."), save
);
336 lev
= decode(s
, prioritynames
);
339 _("unknown priority name: %s."), save
);
340 return ((lev
& LOG_PRIMASK
) | (fac
& LOG_FACMASK
));
344 decode(char *name
, CODE
*codetab
)
351 for (c
= codetab
; c
->c_name
; c
++)
352 if (!strcasecmp(name
, c
->c_name
))