]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - usr.bin/logger/logger.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.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  * 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.
16  *
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
27  * SUCH DAMAGE.
28  */
29
30 #ifndef lint
31 static const char copyright[] =
32 "@(#) Copyright (c) 1983, 1993\n\
33         The Regents of the University of California.  All rights reserved.\n";
34 #endif /* not lint */
35
36 #if 0
37 #ifndef lint
38 static char sccsid[] = "@(#)logger.c    8.1 (Berkeley) 6/6/93";
39 #endif /* not lint */
40 #endif
41
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD$");
44
45 #include <sys/types.h>
46 #include <sys/socket.h>
47 #include <netinet/in.h>
48
49 #include <ctype.h>
50 #include <err.h>
51 #include <netdb.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <unistd.h>
56
57 #define SYSLOG_NAMES
58 #include <syslog.h>
59
60 static int      decode(char *, const CODE *);
61 static int      pencode(char *);
62 static void     logmessage(int, const char *, const char *, const char *,
63                            const char *);
64 static void     usage(void);
65
66 struct socks {
67     int sock;
68     int addrlen;
69     struct sockaddr_storage addr;
70 };
71
72 #ifdef INET6
73 static int family = PF_UNSPEC;  /* protocol family (IPv4, IPv6 or both) */
74 #else
75 static int family = PF_INET;    /* protocol family (IPv4 only) */
76 #endif
77 static int send_to_all = 0;     /* send message to all IPv4/IPv6 addresses */
78
79 /*
80  * logger -- read and log utility
81  *
82  *      Reads from an input and arranges to write the result on the system
83  *      log.
84  */
85 int
86 main(int argc, char *argv[])
87 {
88         int ch, logflags, pri;
89         char *tag, *host, buf[1024];
90         const char *svcname;
91
92         tag = NULL;
93         host = NULL;
94         svcname = "syslog";
95         pri = LOG_USER | LOG_NOTICE;
96         logflags = 0;
97         unsetenv("TZ");
98         while ((ch = getopt(argc, argv, "46Af:h:iP:p:st:")) != -1)
99                 switch((char)ch) {
100                 case '4':
101                         family = PF_INET;
102                         break;
103 #ifdef INET6
104                 case '6':
105                         family = PF_INET6;
106                         break;
107 #endif
108                 case 'A':
109                         send_to_all++;
110                         break;
111                 case 'f':               /* file to log */
112                         if (freopen(optarg, "r", stdin) == NULL)
113                                 err(1, "%s", optarg);
114                         setvbuf(stdin, 0, _IONBF, 0);
115                         break;
116                 case 'h':               /* hostname to deliver to */
117                         host = optarg;
118                         break;
119                 case 'i':               /* log process id also */
120                         logflags |= LOG_PID;
121                         break;
122                 case 'P':               /* service name or port number */
123                         svcname = optarg;
124                         break;
125                 case 'p':               /* priority */
126                         pri = pencode(optarg);
127                         break;
128                 case 's':               /* log to standard error */
129                         logflags |= LOG_PERROR;
130                         break;
131                 case 't':               /* tag */
132                         tag = optarg;
133                         break;
134                 case '?':
135                 default:
136                         usage();
137                 }
138         argc -= optind;
139         argv += optind;
140
141         if (tag == NULL)
142                 tag = getlogin();
143         /* setup for logging */
144         if (host == NULL)
145                 openlog(tag, logflags, 0);
146         (void) fclose(stdout);
147
148         /* log input line if appropriate */
149         if (argc > 0) {
150                 char *p, *endp;
151                 size_t len;
152
153                 for (p = buf, endp = buf + sizeof(buf) - 2; *argv;) {
154                         len = strlen(*argv);
155                         if (p + len > endp && p > buf) {
156                                 logmessage(pri, tag, host, svcname, buf);
157                                 p = buf;
158                         }
159                         if (len > sizeof(buf) - 1)
160                                 logmessage(pri, tag, host, svcname, *argv++);
161                         else {
162                                 if (p != buf)
163                                         *p++ = ' ';
164                                 bcopy(*argv++, p, len);
165                                 *(p += len) = '\0';
166                         }
167                 }
168                 if (p != buf)
169                         logmessage(pri, tag, host, svcname, buf);
170         } else
171                 while (fgets(buf, sizeof(buf), stdin) != NULL)
172                         logmessage(pri, tag, host, svcname, buf);
173         exit(0);
174 }
175
176 /*
177  *  Send the message to syslog, either on the local host, or on a remote host
178  */
179 static void
180 logmessage(int pri, const char *tag, const char *host, const char *svcname,
181            const char *buf)
182 {
183         static struct socks *socks;
184         static int nsock = 0;
185         struct addrinfo hints, *res, *r;
186         char *line;
187         int maxs, len, sock, error, i, lsent;
188
189         if (host == NULL) {
190                 syslog(pri, "%s", buf);
191                 return;
192         }
193
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);
203                 }
204                 if (error)
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));
209                 if (!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,
213                                       r->ai_protocol);
214                         if (sock < 0)
215                                 continue;
216                         memcpy(&socks[nsock].addr, r->ai_addr, r->ai_addrlen);
217                         socks[nsock].addrlen = r->ai_addrlen;
218                         socks[nsock++].sock = sock;
219                 }
220                 freeaddrinfo(res);
221                 if (nsock <= 0)
222                         errx(1, "socket");
223         }
224
225         if ((len = asprintf(&line, "<%d>%s: %s", pri, tag, buf)) == -1)
226                 errx(1, "asprintf");
227
228         lsent = -1;
229         for (i = 0; i < nsock; ++i) {
230                 lsent = sendto(socks[i].sock, line, len, 0,
231                                (struct sockaddr *)&socks[i].addr,
232                                socks[i].addrlen);
233                 if (lsent == len && !send_to_all)
234                         break;
235         }
236         if (lsent != len) {
237                 if (lsent == -1)
238                         warn ("sendto");
239                 else
240                         warnx ("sendto: short send - %d bytes", lsent);
241         }
242
243         free(line);
244 }
245
246 /*
247  *  Decode a symbolic name to a numeric value
248  */
249 static int
250 pencode(char *s)
251 {
252         char *save;
253         int fac, lev;
254
255         for (save = s; *s && *s != '.'; ++s);
256         if (*s) {
257                 *s = '\0';
258                 fac = decode(save, facilitynames);
259                 if (fac < 0)
260                         errx(1, "unknown facility name: %s", save);
261                 *s++ = '.';
262         }
263         else {
264                 fac = 0;
265                 s = save;
266         }
267         lev = decode(s, prioritynames);
268         if (lev < 0)
269                 errx(1, "unknown priority name: %s", save);
270         return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK));
271 }
272
273 static int
274 decode(char *name, const CODE *codetab)
275 {
276         const CODE *c;
277
278         if (isdigit(*name))
279                 return (atoi(name));
280
281         for (c = codetab; c->c_name; c++)
282                 if (!strcasecmp(name, c->c_name))
283                         return (c->c_val);
284
285         return (-1);
286 }
287
288 static void
289 usage(void)
290 {
291         (void)fprintf(stderr, "usage: %s\n",
292             "logger [-46Ais] [-f file] [-h host] [-P port] [-p pri] [-t tag]\n"
293             "              [message ...]"
294             );
295         exit(1);
296 }