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>
61 int decode
__P((char *, CODE
*));
62 int pencode
__P((char *));
65 static int udpport
= 514;
68 myopenlog(const char *sock
) {
70 static struct sockaddr_un s_addr
; /* AF_UNIX address of local logger */
72 if (strlen(sock
) >= sizeof(s_addr
.sun_path
))
73 errx(EXIT_FAILURE
, _("openlog %s: pathname too long"), sock
);
75 s_addr
.sun_family
= AF_UNIX
;
76 (void)strcpy(s_addr
.sun_path
, sock
);
78 if ((fd
= socket(AF_UNIX
, optd
? SOCK_DGRAM
: SOCK_STREAM
, 0)) == -1)
79 err(EXIT_FAILURE
, _("socket %s"), sock
);
81 if (connect(fd
, (struct sockaddr
*) &s_addr
, sizeof(s_addr
)) == -1)
82 err(EXIT_FAILURE
, _("connect %s"), sock
);
88 udpopenlog(const char *servername
,int port
) {
90 struct sockaddr_in s_addr
;
91 struct hostent
*serverhost
;
93 if ((serverhost
= gethostbyname(servername
)) == NULL
)
94 errx(EXIT_FAILURE
, _("unable to resolve '%s'"), servername
);
96 if ((fd
= socket(AF_INET
, SOCK_DGRAM
, 0)) == -1)
97 err(EXIT_FAILURE
, _("socket"));
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
);
103 if (connect(fd
, (struct sockaddr
*) &s_addr
, sizeof(s_addr
)) == -1)
104 err(EXIT_FAILURE
, _("connect"));
109 mysyslog(int fd
, int logflags
, int pri
, char *tag
, char *msg
) {
110 char buf
[1000], pid
[30], *cp
, *tp
;
114 if (logflags
& LOG_PID
)
115 snprintf (pid
, sizeof(pid
), "[%d]", getpid());
128 snprintf(buf
, sizeof(buf
), "<%d>%.15s %.200s%s: %.400s",
129 pri
, tp
, cp
, pid
, msg
);
131 if (write(fd
, buf
, strlen(buf
)+1) < 0)
136 static void __attribute__ ((__noreturn__
)) usage(FILE *out
)
138 fputs(_("\nUsage:\n"), out
);
140 _(" %s [options] [message]\n"), program_invocation_short_name
);
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
);
155 exit(out
== stderr
? EXIT_FAILURE
: EXIT_SUCCESS
);
159 * logger -- read and log utility
161 * Reads from an input and arranges to write the result on the system
165 main(int argc
, char **argv
) {
166 int ch
, logflags
, pri
;
167 char *tag
, buf
[1024];
169 char *udpserver
= NULL
;
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' },
188 setlocale(LC_ALL
, "");
189 bindtextdomain(PACKAGE
, LOCALEDIR
);
195 while ((ch
= getopt_long(argc
, argv
, "f:ip:st:u:dn:P:Vh",
196 longopts
, NULL
)) != -1) {
198 case 'f': /* file to log */
199 if (freopen(optarg
, "r", stdin
) == NULL
)
200 err(EXIT_FAILURE
, _("file %s"),
203 case 'i': /* log process id also */
206 case 'p': /* priority */
207 pri
= pencode(optarg
);
209 case 's': /* log to standard error */
210 logflags
|= LOG_PERROR
;
215 case 'u': /* unix socket */
219 optd
= 1; /* use datagrams */
221 case 'n': /* udp socket */
222 optd
= 1; /* use datagrams because udp */
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"),
231 udpport
= (int) tmpport
;
234 printf(_("%s from %s\n"), program_invocation_short_name
,
247 /* setup for logging */
248 if (!usock
&& !udpserver
)
249 openlog(tag
? tag
: getlogin(), logflags
, 0);
251 LogSock
= udpopenlog(udpserver
,udpport
);
253 LogSock
= myopenlog(usock
);
255 (void) fclose(stdout
);
257 /* log input line if appropriate */
259 register char *p
, *endp
;
262 for (p
= buf
, endp
= buf
+ sizeof(buf
) - 2; *argv
;) {
264 if (p
+ len
> endp
&& p
> buf
) {
265 if (!usock
&& !udpserver
)
266 syslog(pri
, "%s", buf
);
268 mysyslog(LogSock
, logflags
, pri
, tag
, buf
);
271 if (len
> sizeof(buf
) - 1) {
272 if (!usock
&& !udpserver
)
273 syslog(pri
, "%s", *argv
++);
275 mysyslog(LogSock
, logflags
, pri
, tag
, *argv
++);
279 memmove(p
, *argv
++, len
);
284 if (!usock
&& !udpserver
)
285 syslog(pri
, "%s", buf
);
287 mysyslog(LogSock
, logflags
, pri
, tag
, buf
);
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
);
295 if (len
> 0 && buf
[len
- 1] == '\n')
298 if (!usock
&& !udpserver
)
299 syslog(pri
, "%s", buf
);
301 mysyslog(LogSock
, logflags
, pri
, tag
, buf
);
304 if (!usock
&& !udpserver
)
313 * Decode a symbolic name to a numeric value
321 for (save
= s
; *s
&& *s
!= '.'; ++s
);
324 fac
= decode(save
, facilitynames
);
327 _("unknown facility name: %s."), save
);
334 lev
= decode(s
, prioritynames
);
337 _("unknown priority name: %s."), save
);
338 return ((lev
& LOG_PRIMASK
) | (fac
& LOG_FACMASK
));
342 decode(char *name
, CODE
*codetab
)
349 for (c
= codetab
; c
->c_name
; c
++)
350 if (!strcasecmp(name
, c
->c_name
))