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 * 4. 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
31 static const char copyright[] =
32 "@(#) Copyright (c) 1983, 1993\n\
33 The Regents of the University of California. All rights reserved.\n";
38 static char sccsid[] = "@(#)logger.c 8.1 (Berkeley) 6/6/93";
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD$");
45 #include <sys/types.h>
46 #include <sys/socket.h>
47 #include <netinet/in.h>
60 static int decode(char *, const CODE *);
61 static int pencode(char *);
62 static void logmessage(int, const char *, const char *, const char *,
64 static void usage(void);
69 struct sockaddr_storage addr;
73 static int family = PF_UNSPEC; /* protocol family (IPv4, IPv6 or both) */
75 static int family = PF_INET; /* protocol family (IPv4 only) */
77 static int send_to_all = 0; /* send message to all IPv4/IPv6 addresses */
80 * logger -- read and log utility
82 * Reads from an input and arranges to write the result on the system
86 main(int argc, char *argv[])
88 int ch, logflags, pri;
89 char *tag, *host, buf[1024];
95 pri = LOG_USER | LOG_NOTICE;
98 while ((ch = getopt(argc, argv, "46Af:h:iP:p:st:")) != -1)
111 case 'f': /* file to log */
112 if (freopen(optarg, "r", stdin) == NULL)
113 err(1, "%s", optarg);
114 setvbuf(stdin, 0, _IONBF, 0);
116 case 'h': /* hostname to deliver to */
119 case 'i': /* log process id also */
122 case 'P': /* service name or port number */
125 case 'p': /* priority */
126 pri = pencode(optarg);
128 case 's': /* log to standard error */
129 logflags |= LOG_PERROR;
143 /* setup for logging */
145 openlog(tag, logflags, 0);
146 (void) fclose(stdout);
148 /* log input line if appropriate */
153 for (p = buf, endp = buf + sizeof(buf) - 2; *argv;) {
155 if (p + len > endp && p > buf) {
156 logmessage(pri, tag, host, svcname, buf);
159 if (len > sizeof(buf) - 1)
160 logmessage(pri, tag, host, svcname, *argv++);
164 bcopy(*argv++, p, len);
169 logmessage(pri, tag, host, svcname, buf);
171 while (fgets(buf, sizeof(buf), stdin) != NULL)
172 logmessage(pri, tag, host, svcname, buf);
177 * Send the message to syslog, either on the local host, or on a remote host
180 logmessage(int pri, const char *tag, const char *host, const char *svcname,
183 static struct socks *socks;
184 static int nsock = 0;
185 struct addrinfo hints, *res, *r;
187 int maxs, len, sock, error, i, lsent;
190 syslog(pri, "%s", buf);
194 if (nsock <= 0) { /* set up socket stuff */
195 /* resolve hostname */
196 memset(&hints, 0, sizeof(hints));
197 hints.ai_family = family;
198 hints.ai_socktype = SOCK_DGRAM;
199 error = getaddrinfo(host, svcname, &hints, &res);
200 if (error == EAI_SERVICE) {
201 warnx("%s/udp: unknown service", svcname);
202 error = getaddrinfo(host, "514", &hints, &res);
205 errx(1, "%s: %s", gai_strerror(error), host);
206 /* count max number of sockets we may open */
207 for (maxs = 0, r = res; r; r = r->ai_next, maxs++);
208 socks = malloc(maxs * sizeof(struct socks));
210 errx(1, "couldn't allocate memory for sockets");
211 for (r = res; r; r = r->ai_next) {
212 sock = socket(r->ai_family, r->ai_socktype,
216 memcpy(&socks[nsock].addr, r->ai_addr, r->ai_addrlen);
217 socks[nsock].addrlen = r->ai_addrlen;
218 socks[nsock++].sock = sock;
225 if ((len = asprintf(&line, "<%d>%s: %s", pri, tag, buf)) == -1)
229 for (i = 0; i < nsock; ++i) {
230 lsent = sendto(socks[i].sock, line, len, 0,
231 (struct sockaddr *)&socks[i].addr,
233 if (lsent == len && !send_to_all)
240 warnx ("sendto: short send - %d bytes", lsent);
247 * Decode a symbolic name to a numeric value
255 for (save = s; *s && *s != '.'; ++s);
258 fac = decode(save, facilitynames);
260 errx(1, "unknown facility name: %s", save);
267 lev = decode(s, prioritynames);
269 errx(1, "unknown priority name: %s", save);
270 return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK));
274 decode(char *name, const CODE *codetab)
281 for (c = codetab; c->c_name; c++)
282 if (!strcasecmp(name, c->c_name))
291 (void)fprintf(stderr, "usage: %s\n",
292 "logger [-46Ais] [-f file] [-h host] [-P port] [-p pri] [-t tag]\n"