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