Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / usr.bin / logger / logger.c
1 /*
2  * Copyright (c) 1983, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
20  *
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
31  * SUCH DAMAGE.
32  *
33  * @(#) Copyright (c) 1983, 1993 The Regents of the University of California.  All rights reserved.
34  * @(#)logger.c 8.1 (Berkeley) 6/6/93
35  * $FreeBSD: src/usr.bin/logger/logger.c,v 1.5.2.3 2001/09/06 17:38:57 ru Exp $
36  * $DragonFly: src/usr.bin/logger/logger.c,v 1.2 2003/06/17 04:29:28 dillon Exp $
37  */
38
39 #include <sys/types.h>
40 #include <sys/socket.h>
41 #include <netinet/in.h>
42
43 #include <ctype.h>
44 #include <err.h>
45 #include <netdb.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <unistd.h>
50
51 #define SYSLOG_NAMES
52 #include <syslog.h>
53
54 int     decode __P((char *, CODE *));
55 int     pencode __P((char *));
56 static void     logmessage __P((int, char *, char *));
57 static void     usage __P((void));
58
59 struct socks {
60     int sock;
61     int addrlen;
62     struct sockaddr_storage addr;
63 };
64
65 #ifdef INET6
66 int     family = PF_UNSPEC;     /* protocol family (IPv4, IPv6 or both) */
67 #else
68 int     family = PF_INET;       /* protocol family (IPv4 only) */
69 #endif
70 int     send_to_all = 0;        /* send message to all IPv4/IPv6 addresses */
71
72 /*
73  * logger -- read and log utility
74  *
75  *      Reads from an input and arranges to write the result on the system
76  *      log.
77  */
78 int
79 main(argc, argv)
80         int argc;
81         char *argv[];
82 {
83         int ch, logflags, pri;
84         char *tag, *host, buf[1024];
85
86         tag = NULL;
87         host = NULL;
88         pri = LOG_USER | LOG_NOTICE;
89         logflags = 0;
90         unsetenv("TZ");
91         while ((ch = getopt(argc, argv, "46Af:h:ip:st:")) != -1)
92                 switch((char)ch) {
93                 case '4':
94                         family = PF_INET;
95                         break;
96 #ifdef INET6
97                 case '6':
98                         family = PF_INET6;
99                         break;
100 #endif
101                 case 'A':
102                         send_to_all++;
103                         break;
104                 case 'f':               /* file to log */
105                         if (freopen(optarg, "r", stdin) == NULL)
106                                 err(1, "%s", optarg);
107                         break;
108                 case 'h':               /* hostname to deliver to */
109                         host = optarg;
110                         break;
111                 case 'i':               /* log process id also */
112                         logflags |= LOG_PID;
113                         break;
114                 case 'p':               /* priority */
115                         pri = pencode(optarg);
116                         break;
117                 case 's':               /* log to standard error */
118                         logflags |= LOG_PERROR;
119                         break;
120                 case 't':               /* tag */
121                         tag = optarg;
122                         break;
123                 case '?':
124                 default:
125                         usage();
126                 }
127         argc -= optind;
128         argv += optind;
129
130         /* setup for logging */
131         openlog(tag ? tag : getlogin(), logflags, 0);
132         (void) fclose(stdout);
133
134         /* log input line if appropriate */
135         if (argc > 0) {
136                 register char *p, *endp;
137                 int len;
138
139                 for (p = buf, endp = buf + sizeof(buf) - 2; *argv;) {
140                         len = strlen(*argv);
141                         if (p + len > endp && p > buf) {
142                                 logmessage(pri, host, buf);
143                                 p = buf;
144                         }
145                         if (len > sizeof(buf) - 1)
146                                 logmessage(pri, host, *argv++);
147                         else {
148                                 if (p != buf)
149                                         *p++ = ' ';
150                                 bcopy(*argv++, p, len);
151                                 *(p += len) = '\0';
152                         }
153                 }
154                 if (p != buf)
155                         logmessage(pri, host, buf);
156         } else
157                 while (fgets(buf, sizeof(buf), stdin) != NULL)
158                         logmessage(pri, host, buf);
159         exit(0);
160 }
161
162 /*
163  *  Send the message to syslog, either on the local host, or on a remote host
164  */
165 void 
166 logmessage(int pri, char *host, char *buf)
167 {
168         static struct socks *socks;
169         static int nsock = 0;
170         struct addrinfo hints, *res, *r;
171         char *line;
172         int maxs, len, sock, error, i, lsent;
173
174         if (host == NULL) {
175                 syslog(pri, "%s", buf);
176                 return;
177         }
178
179         if (nsock <= 0) {       /* set up socket stuff */
180                 /* resolve hostname */
181                 memset(&hints, 0, sizeof(hints));
182                 hints.ai_family = family;
183                 hints.ai_socktype = SOCK_DGRAM;
184                 error = getaddrinfo(host, "syslog", &hints, &res);
185                 if (error == EAI_SERVICE) {
186                         warnx ("syslog/udp: unknown service");  /* not fatal */
187                         error = getaddrinfo(host, "514", &hints, &res);
188                 }
189                 if (error)
190                         errx(1, "%s: %s", gai_strerror(error), host);
191                 /* count max number of sockets we may open */
192                 for (maxs = 0, r = res; r; r = r->ai_next, maxs++);
193                 socks = malloc(maxs * sizeof(struct socks));
194                 if (!socks)
195                         errx(1, "couldn't allocate memory for sockets");
196                 for (r = res; r; r = r->ai_next) {
197                         sock = socket(r->ai_family, r->ai_socktype,
198                                       r->ai_protocol);
199                         if (sock < 0)
200                                 continue;
201                         memcpy(&socks[nsock].addr, r->ai_addr, r->ai_addrlen);
202                         socks[nsock].addrlen = r->ai_addrlen;
203                         socks[nsock++].sock = sock;
204                 }
205                 freeaddrinfo(res);
206                 if (nsock <= 0)
207                         errx(1, "socket");
208         }
209
210         if ((len = asprintf(&line, "<%d>%s", pri, buf)) == -1)
211                 errx(1, "asprintf");
212
213         for (i = 0; i < nsock; ++i) {
214                 lsent = sendto(socks[i].sock, line, len, 0,
215                                (struct sockaddr *)&socks[i].addr,
216                                socks[i].addrlen);
217                 if (lsent == len && !send_to_all)
218                         break;
219         }
220         if (lsent != len)
221                 warnx ("sendmsg");
222
223         free(line);
224 }
225
226 /*
227  *  Decode a symbolic name to a numeric value
228  */
229 int
230 pencode(s)
231         register char *s;
232 {
233         char *save;
234         int fac, lev;
235
236         for (save = s; *s && *s != '.'; ++s);
237         if (*s) {
238                 *s = '\0';
239                 fac = decode(save, facilitynames);
240                 if (fac < 0)
241                         errx(1, "unknown facility name: %s", save);
242                 *s++ = '.';
243         }
244         else {
245                 fac = 0;
246                 s = save;
247         }
248         lev = decode(s, prioritynames);
249         if (lev < 0)
250                 errx(1, "unknown priority name: %s", save);
251         return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK));
252 }
253
254 int
255 decode(name, codetab)
256         char *name;
257         CODE *codetab;
258 {
259         register CODE *c;
260
261         if (isdigit(*name))
262                 return (atoi(name));
263
264         for (c = codetab; c->c_name; c++)
265                 if (!strcasecmp(name, c->c_name))
266                         return (c->c_val);
267
268         return (-1);
269 }
270
271 static void
272 usage()
273 {
274         (void)fprintf(stderr, "usage: %s\n",
275             "logger [-46Ais] [-f file] [-h host] [-p pri] [-t tag] [message ...]"
276             );
277         exit(1);
278 }