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";
46 #include <sys/cdefs.h>
47 __FBSDID("$FreeBSD$");
49 #include <sys/types.h>
50 #include <sys/socket.h>
51 #include <netinet/in.h>
64 int decode(char *, CODE *);
66 static void logmessage(int, const char *, const char *, const char *,
68 static void usage(void);
73 struct sockaddr_storage addr;
77 int family = PF_UNSPEC; /* protocol family (IPv4, IPv6 or both) */
79 int family = PF_INET; /* protocol family (IPv4 only) */
81 int send_to_all = 0; /* send message to all IPv4/IPv6 addresses */
84 * logger -- read and log utility
86 * Reads from an input and arranges to write the result on the system
90 main(int argc, char *argv[])
92 int ch, logflags, pri;
93 char *tag, *host, buf[1024];
99 pri = LOG_USER | LOG_NOTICE;
102 while ((ch = getopt(argc, argv, "46Af:h:iP:p:st:")) != -1)
115 case 'f': /* file to log */
116 if (freopen(optarg, "r", stdin) == NULL)
117 err(1, "%s", optarg);
119 case 'h': /* hostname to deliver to */
122 case 'i': /* log process id also */
125 case 'P': /* service name or port number */
128 case 'p': /* priority */
129 pri = pencode(optarg);
131 case 's': /* log to standard error */
132 logflags |= LOG_PERROR;
146 /* setup for logging */
148 openlog(tag, logflags, 0);
149 (void) fclose(stdout);
151 /* log input line if appropriate */
156 for (p = buf, endp = buf + sizeof(buf) - 2; *argv;) {
158 if (p + len > endp && p > buf) {
159 logmessage(pri, tag, host, svcname, buf);
162 if (len > sizeof(buf) - 1)
163 logmessage(pri, tag, host, svcname, *argv++);
167 bcopy(*argv++, p, len);
172 logmessage(pri, tag, host, svcname, buf);
174 while (fgets(buf, sizeof(buf), stdin) != NULL)
175 logmessage(pri, tag, host, svcname, buf);
180 * Send the message to syslog, either on the local host, or on a remote host
183 logmessage(int pri, const char *tag, const char *host, const char *svcname,
186 static struct socks *socks;
187 static int nsock = 0;
188 struct addrinfo hints, *res, *r;
190 int maxs, len, sock, error, i, lsent;
193 syslog(pri, "%s", buf);
197 if (nsock <= 0) { /* set up socket stuff */
198 /* resolve hostname */
199 memset(&hints, 0, sizeof(hints));
200 hints.ai_family = family;
201 hints.ai_socktype = SOCK_DGRAM;
202 error = getaddrinfo(host, svcname, &hints, &res);
203 if (error == EAI_SERVICE) {
204 warnx("%s/udp: unknown service", svcname);
205 error = getaddrinfo(host, "514", &hints, &res);
208 errx(1, "%s: %s", gai_strerror(error), host);
209 /* count max number of sockets we may open */
210 for (maxs = 0, r = res; r; r = r->ai_next, maxs++);
211 socks = malloc(maxs * sizeof(struct socks));
213 errx(1, "couldn't allocate memory for sockets");
214 for (r = res; r; r = r->ai_next) {
215 sock = socket(r->ai_family, r->ai_socktype,
219 memcpy(&socks[nsock].addr, r->ai_addr, r->ai_addrlen);
220 socks[nsock].addrlen = r->ai_addrlen;
221 socks[nsock++].sock = sock;
228 if ((len = asprintf(&line, "<%d>%s: %s", pri, tag, buf)) == -1)
232 for (i = 0; i < nsock; ++i) {
233 lsent = sendto(socks[i].sock, line, len, 0,
234 (struct sockaddr *)&socks[i].addr,
236 if (lsent == len && !send_to_all)
243 warnx ("sendto: short send - %d bytes", lsent);
250 * Decode a symbolic name to a numeric value
258 for (save = s; *s && *s != '.'; ++s);
261 fac = decode(save, facilitynames);
263 errx(1, "unknown facility name: %s", save);
270 lev = decode(s, prioritynames);
272 errx(1, "unknown priority name: %s", save);
273 return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK));
277 decode(char *name, CODE *codetab)
284 for (c = codetab; c->c_name; c++)
285 if (!strcasecmp(name, c->c_name))
294 (void)fprintf(stderr, "usage: %s\n",
295 "logger [-46Ais] [-f file] [-h host] [-P port] [-p pri] [-t tag]\n"