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
35 static const char copyright[] =
36 "@(#) Copyright (c) 1983, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
42 static char sccsid[] = "@(#)logger.c 8.1 (Berkeley) 6/6/93";
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
48 #include <sys/types.h>
49 #include <sys/socket.h>
50 #include <netinet/in.h>
63 int decode(char *, CODE *);
65 static void logmessage(int, char *, char *);
66 static void usage(void);
71 struct sockaddr_storage addr;
75 int family = PF_UNSPEC; /* protocol family (IPv4, IPv6 or both) */
77 int family = PF_INET; /* protocol family (IPv4 only) */
79 int send_to_all = 0; /* send message to all IPv4/IPv6 addresses */
82 * logger -- read and log utility
84 * Reads from an input and arranges to write the result on the system
88 main(int argc, char *argv[])
90 int ch, logflags, pri;
91 char *tag, *host, buf[1024];
95 pri = LOG_USER | LOG_NOTICE;
98 while ((ch = getopt(argc, argv, "46Af:h:ip:st:")) != -1)
111 case 'f': /* file to log */
112 if (freopen(optarg, "r", stdin) == NULL)
113 err(1, "%s", optarg);
115 case 'h': /* hostname to deliver to */
118 case 'i': /* log process id also */
121 case 'p': /* priority */
122 pri = pencode(optarg);
124 case 's': /* log to standard error */
125 logflags |= LOG_PERROR;
137 /* setup for logging */
138 openlog(tag ? tag : getlogin(), logflags, 0);
139 (void) fclose(stdout);
141 /* log input line if appropriate */
146 for (p = buf, endp = buf + sizeof(buf) - 2; *argv;) {
148 if (p + len > endp && p > buf) {
149 logmessage(pri, host, buf);
152 if (len > sizeof(buf) - 1)
153 logmessage(pri, host, *argv++);
157 bcopy(*argv++, p, len);
162 logmessage(pri, host, buf);
164 while (fgets(buf, sizeof(buf), stdin) != NULL)
165 logmessage(pri, host, buf);
170 * Send the message to syslog, either on the local host, or on a remote host
173 logmessage(int pri, char *host, char *buf)
175 static struct socks *socks;
176 static int nsock = 0;
177 struct addrinfo hints, *res, *r;
179 int maxs, len, sock, error, i, lsent;
182 syslog(pri, "%s", buf);
186 if (nsock <= 0) { /* set up socket stuff */
187 /* resolve hostname */
188 memset(&hints, 0, sizeof(hints));
189 hints.ai_family = family;
190 hints.ai_socktype = SOCK_DGRAM;
191 error = getaddrinfo(host, "syslog", &hints, &res);
192 if (error == EAI_SERVICE) {
193 warnx ("syslog/udp: unknown service"); /* not fatal */
194 error = getaddrinfo(host, "514", &hints, &res);
197 errx(1, "%s: %s", gai_strerror(error), host);
198 /* count max number of sockets we may open */
199 for (maxs = 0, r = res; r; r = r->ai_next, maxs++);
200 socks = malloc(maxs * sizeof(struct socks));
202 errx(1, "couldn't allocate memory for sockets");
203 for (r = res; r; r = r->ai_next) {
204 sock = socket(r->ai_family, r->ai_socktype,
208 memcpy(&socks[nsock].addr, r->ai_addr, r->ai_addrlen);
209 socks[nsock].addrlen = r->ai_addrlen;
210 socks[nsock++].sock = sock;
217 if ((len = asprintf(&line, "<%d>%s", pri, buf)) == -1)
220 for (i = 0; i < nsock; ++i) {
221 lsent = sendto(socks[i].sock, line, len, 0,
222 (struct sockaddr *)&socks[i].addr,
224 if (lsent == len && !send_to_all)
231 warnx ("sendto: short send - %d bytes", lsent);
238 * Decode a symbolic name to a numeric value
246 for (save = s; *s && *s != '.'; ++s);
249 fac = decode(save, facilitynames);
251 errx(1, "unknown facility name: %s", save);
258 lev = decode(s, prioritynames);
260 errx(1, "unknown priority name: %s", save);
261 return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK));
265 decode(char *name, CODE *codetab)
272 for (c = codetab; c->c_name; c++)
273 if (!strcasecmp(name, c->c_name))
282 (void)fprintf(stderr, "usage: %s\n",
283 "logger [-46Ais] [-f file] [-h host] [-p pri] [-t tag] [message ...]"