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 *);
67 static void usage(void);
72 struct sockaddr_storage addr;
76 int family = PF_UNSPEC; /* protocol family (IPv4, IPv6 or both) */
78 int family = PF_INET; /* protocol family (IPv4 only) */
80 int send_to_all = 0; /* send message to all IPv4/IPv6 addresses */
83 * logger -- read and log utility
85 * Reads from an input and arranges to write the result on the system
89 main(int argc, char *argv[])
91 int ch, logflags, pri;
92 char *tag, *host, buf[1024];
98 pri = LOG_USER | LOG_NOTICE;
101 while ((ch = getopt(argc, argv, "46Af:h:iP:p:st:")) != -1)
114 case 'f': /* file to log */
115 if (freopen(optarg, "r", stdin) == NULL)
116 err(1, "%s", optarg);
118 case 'h': /* hostname to deliver to */
121 case 'i': /* log process id also */
124 case 'P': /* service name or port number */
127 case 'p': /* priority */
128 pri = pencode(optarg);
130 case 's': /* log to standard error */
131 logflags |= LOG_PERROR;
143 /* setup for logging */
144 openlog(tag ? tag : getlogin(), logflags, 0);
145 (void) fclose(stdout);
147 /* log input line if appropriate */
152 for (p = buf, endp = buf + sizeof(buf) - 2; *argv;) {
154 if (p + len > endp && p > buf) {
155 logmessage(pri, host, svcname, buf);
158 if (len > sizeof(buf) - 1)
159 logmessage(pri, host, svcname, *argv++);
163 bcopy(*argv++, p, len);
168 logmessage(pri, host, svcname, buf);
170 while (fgets(buf, sizeof(buf), stdin) != NULL)
171 logmessage(pri, host, svcname, buf);
176 * Send the message to syslog, either on the local host, or on a remote host
179 logmessage(int pri, const char *host, const char *svcname, const char *buf)
181 static struct socks *socks;
182 static int nsock = 0;
183 struct addrinfo hints, *res, *r;
185 int maxs, len, sock, error, i, lsent;
188 syslog(pri, "%s", buf);
192 if (nsock <= 0) { /* set up socket stuff */
193 /* resolve hostname */
194 memset(&hints, 0, sizeof(hints));
195 hints.ai_family = family;
196 hints.ai_socktype = SOCK_DGRAM;
197 error = getaddrinfo(host, svcname, &hints, &res);
198 if (error == EAI_SERVICE) {
199 warnx("%s/udp: unknown service", svcname);
200 error = getaddrinfo(host, "514", &hints, &res);
203 errx(1, "%s: %s", gai_strerror(error), host);
204 /* count max number of sockets we may open */
205 for (maxs = 0, r = res; r; r = r->ai_next, maxs++);
206 socks = malloc(maxs * sizeof(struct socks));
208 errx(1, "couldn't allocate memory for sockets");
209 for (r = res; r; r = r->ai_next) {
210 sock = socket(r->ai_family, r->ai_socktype,
214 memcpy(&socks[nsock].addr, r->ai_addr, r->ai_addrlen);
215 socks[nsock].addrlen = r->ai_addrlen;
216 socks[nsock++].sock = sock;
223 if ((len = asprintf(&line, "<%d>%s", pri, buf)) == -1)
227 for (i = 0; i < nsock; ++i) {
228 lsent = sendto(socks[i].sock, line, len, 0,
229 (struct sockaddr *)&socks[i].addr,
231 if (lsent == len && !send_to_all)
238 warnx ("sendto: short send - %d bytes", lsent);
245 * Decode a symbolic name to a numeric value
253 for (save = s; *s && *s != '.'; ++s);
256 fac = decode(save, facilitynames);
258 errx(1, "unknown facility name: %s", save);
265 lev = decode(s, prioritynames);
267 errx(1, "unknown priority name: %s", save);
268 return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK));
272 decode(char *name, CODE *codetab)
279 for (c = codetab; c->c_name; c++)
280 if (!strcasecmp(name, c->c_name))
289 (void)fprintf(stderr, "usage: %s\n",
290 "logger [-46Ais] [-f file] [-h host] [-P port] [-p pri] [-t tag]\n"