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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#) Copyright (c) 1983, 1993 The Regents of the University of California. All rights reserved.
30 * @(#)logger.c 8.1 (Berkeley) 6/6/93
31 * $FreeBSD: src/usr.bin/logger/logger.c,v 1.5.2.3 2001/09/06 17:38:57 ru Exp $
32 * $DragonFly: src/usr.bin/logger/logger.c,v 1.4 2005/02/25 18:08:05 liamfoy Exp $
35 #include <sys/types.h>
36 #include <sys/socket.h>
49 static int decode(const char *, CODE
*);
50 static int pencode(char *);
51 static void logmessage(int, const char *, const char *);
52 static void usage(void);
57 struct sockaddr_storage addr
;
61 int family
= PF_UNSPEC
; /* protocol family (IPv4, IPv6 or both) */
63 int family
= PF_INET
; /* protocol family (IPv4 only) */
65 int send_to_all
= 0; /* send message to all IPv4/IPv6 addresses */
68 * logger -- read and log utility
70 * Reads from an input and arranges to write the result on the system
74 main(int argc
, char **argv
)
76 int ch
, logflags
, pri
;
77 char *tag
, *host
, buf
[1024];
81 pri
= LOG_USER
| LOG_NOTICE
;
84 while ((ch
= getopt(argc
, argv
, "46Af:h:ip:st:")) != -1)
97 case 'f': /* file to log */
98 if (freopen(optarg
, "r", stdin
) == NULL
)
101 case 'h': /* hostname to deliver to */
104 case 'i': /* log process id also */
107 case 'p': /* priority */
108 pri
= pencode(optarg
);
110 case 's': /* log to standard error */
111 logflags
|= LOG_PERROR
;
123 /* setup for logging */
124 openlog(tag
? tag
: getlogin(), logflags
, 0);
127 /* log input line if appropriate */
132 for (p
= buf
, endp
= buf
+ sizeof(buf
) - 2; *argv
;) {
134 if (p
+ len
> endp
&& p
> buf
) {
135 logmessage(pri
, host
, buf
);
138 if (len
> sizeof(buf
) - 1)
139 logmessage(pri
, host
, *argv
++);
143 bcopy(*argv
++, p
, len
);
148 logmessage(pri
, host
, buf
);
150 while (fgets(buf
, sizeof(buf
), stdin
) != NULL
)
151 logmessage(pri
, host
, buf
);
156 * Send the message to syslog, either on the local host, or on a remote host
159 logmessage(int pri
, const char *host
, const char *buf
)
161 static struct socks
*socks
;
162 static int nsock
= 0;
163 struct addrinfo hints
, *res
, *r
;
165 int maxs
, len
, sock
, error
, i
, lsent
= 0;
168 syslog(pri
, "%s", buf
);
172 if (nsock
<= 0) { /* set up socket stuff */
173 /* resolve hostname */
174 memset(&hints
, 0, sizeof(hints
));
175 hints
.ai_family
= family
;
176 hints
.ai_socktype
= SOCK_DGRAM
;
177 error
= getaddrinfo(host
, "syslog", &hints
, &res
);
178 if (error
== EAI_SERVICE
) {
179 warnx ("syslog/udp: unknown service"); /* not fatal */
180 error
= getaddrinfo(host
, "514", &hints
, &res
);
183 errx(1, "%s: %s", gai_strerror(error
), host
);
184 /* count max number of sockets we may open */
185 for (maxs
= 0, r
= res
; r
; r
= r
->ai_next
, maxs
++);
186 socks
= malloc(maxs
* sizeof(struct socks
));
188 err(1, "couldn't allocate memory for sockets");
189 for (r
= res
; r
; r
= r
->ai_next
) {
190 sock
= socket(r
->ai_family
, r
->ai_socktype
,
194 memcpy(&socks
[nsock
].addr
, r
->ai_addr
, r
->ai_addrlen
);
195 socks
[nsock
].addrlen
= r
->ai_addrlen
;
196 socks
[nsock
++].sock
= sock
;
203 if ((len
= asprintf(&line
, "<%d>%s", pri
, buf
)) == -1)
206 for (i
= 0; i
< nsock
; ++i
) {
207 lsent
= sendto(socks
[i
].sock
, line
, len
, 0,
208 (struct sockaddr
*)&socks
[i
].addr
,
210 if (lsent
== len
&& !send_to_all
)
220 * Decode a symbolic name to a numeric value
228 for (save
= s
; *s
&& *s
!= '.'; ++s
);
231 fac
= decode(save
, facilitynames
);
233 errx(1, "unknown facility name: %s", save
);
240 lev
= decode(s
, prioritynames
);
242 errx(1, "unknown priority name: %s", save
);
243 return ((lev
& LOG_PRIMASK
) | (fac
& LOG_FACMASK
));
247 decode(const char *name
, CODE
*codetab
)
254 for (c
= codetab
; c
->c_name
; c
++)
255 if (!strcasecmp(name
, c
->c_name
))
264 fprintf(stderr
, "usage: %s\n",
265 "logger [-46Ais] [-f file] [-h host] [-p pri] [-t tag] [message ...]");