logger.c revision 216370
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 31static 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 38static 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: head/usr.bin/logger/logger.c 216370 2010-12-11 08:32:16Z joel $"); 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 60int decode(char *, CODE *); 61int pencode(char *); 62static void logmessage(int, const char *, const char *, const char *); 63static void usage(void); 64 65struct socks { 66 int sock; 67 int addrlen; 68 struct sockaddr_storage addr; 69}; 70 71#ifdef INET6 72int family = PF_UNSPEC; /* protocol family (IPv4, IPv6 or both) */ 73#else 74int family = PF_INET; /* protocol family (IPv4 only) */ 75#endif 76int send_to_all = 0; /* send message to all IPv4/IPv6 addresses */ 77 78/* 79 * logger -- read and log utility 80 * 81 * Reads from an input and arranges to write the result on the system 82 * log. 83 */ 84int 85main(int argc, char *argv[]) 86{ 87 int ch, logflags, pri; 88 char *tag, *host, buf[1024]; 89 const char *svcname; 90 91 tag = NULL; 92 host = NULL; 93 svcname = "syslog"; 94 pri = LOG_USER | LOG_NOTICE; 95 logflags = 0; 96 unsetenv("TZ"); 97 while ((ch = getopt(argc, argv, "46Af:h:iP:p:st:")) != -1) 98 switch((char)ch) { 99 case '4': 100 family = PF_INET; 101 break; 102#ifdef INET6 103 case '6': 104 family = PF_INET6; 105 break; 106#endif 107 case 'A': 108 send_to_all++; 109 break; 110 case 'f': /* file to log */ 111 if (freopen(optarg, "r", stdin) == NULL) 112 err(1, "%s", optarg); 113 setvbuf(stdin, 0, _IONBF, 0); 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': /* service name or port number */ 122 svcname = optarg; 123 break; 124 case 'p': /* priority */ 125 pri = pencode(optarg); 126 break; 127 case 's': /* log to standard error */ 128 logflags |= LOG_PERROR; 129 break; 130 case 't': /* tag */ 131 tag = optarg; 132 break; 133 case '?': 134 default: 135 usage(); 136 } 137 argc -= optind; 138 argv += optind; 139 140 /* setup for logging */ 141 openlog(tag ? tag : getlogin(), logflags, 0); 142 (void) fclose(stdout); 143 144 /* log input line if appropriate */ 145 if (argc > 0) { 146 char *p, *endp; 147 size_t len; 148 149 for (p = buf, endp = buf + sizeof(buf) - 2; *argv;) { 150 len = strlen(*argv); 151 if (p + len > endp && p > buf) { 152 logmessage(pri, host, svcname, buf); 153 p = buf; 154 } 155 if (len > sizeof(buf) - 1) 156 logmessage(pri, host, svcname, *argv++); 157 else { 158 if (p != buf) 159 *p++ = ' '; 160 bcopy(*argv++, p, len); 161 *(p += len) = '\0'; 162 } 163 } 164 if (p != buf) 165 logmessage(pri, host, svcname, buf); 166 } else 167 while (fgets(buf, sizeof(buf), stdin) != NULL) 168 logmessage(pri, host, svcname, buf); 169 exit(0); 170} 171 172/* 173 * Send the message to syslog, either on the local host, or on a remote host 174 */ 175void 176logmessage(int pri, const char *host, const char *svcname, const char *buf) 177{ 178 static struct socks *socks; 179 static int nsock = 0; 180 struct addrinfo hints, *res, *r; 181 char *line; 182 int maxs, len, sock, error, i, lsent; 183 184 if (host == NULL) { 185 syslog(pri, "%s", buf); 186 return; 187 } 188 189 if (nsock <= 0) { /* set up socket stuff */ 190 /* resolve hostname */ 191 memset(&hints, 0, sizeof(hints)); 192 hints.ai_family = family; 193 hints.ai_socktype = SOCK_DGRAM; 194 error = getaddrinfo(host, svcname, &hints, &res); 195 if (error == EAI_SERVICE) { 196 warnx("%s/udp: unknown service", svcname); 197 error = getaddrinfo(host, "514", &hints, &res); 198 } 199 if (error) 200 errx(1, "%s: %s", gai_strerror(error), host); 201 /* count max number of sockets we may open */ 202 for (maxs = 0, r = res; r; r = r->ai_next, maxs++); 203 socks = malloc(maxs * sizeof(struct socks)); 204 if (!socks) 205 errx(1, "couldn't allocate memory for sockets"); 206 for (r = res; r; r = r->ai_next) { 207 sock = socket(r->ai_family, r->ai_socktype, 208 r->ai_protocol); 209 if (sock < 0) 210 continue; 211 memcpy(&socks[nsock].addr, r->ai_addr, r->ai_addrlen); 212 socks[nsock].addrlen = r->ai_addrlen; 213 socks[nsock++].sock = sock; 214 } 215 freeaddrinfo(res); 216 if (nsock <= 0) 217 errx(1, "socket"); 218 } 219 220 if ((len = asprintf(&line, "<%d>%s", pri, buf)) == -1) 221 errx(1, "asprintf"); 222 223 lsent = -1; 224 for (i = 0; i < nsock; ++i) { 225 lsent = sendto(socks[i].sock, line, len, 0, 226 (struct sockaddr *)&socks[i].addr, 227 socks[i].addrlen); 228 if (lsent == len && !send_to_all) 229 break; 230 } 231 if (lsent != len) { 232 if (lsent == -1) 233 warn ("sendto"); 234 else 235 warnx ("sendto: short send - %d bytes", lsent); 236 } 237 238 free(line); 239} 240 241/* 242 * Decode a symbolic name to a numeric value 243 */ 244int 245pencode(char *s) 246{ 247 char *save; 248 int fac, lev; 249 250 for (save = s; *s && *s != '.'; ++s); 251 if (*s) { 252 *s = '\0'; 253 fac = decode(save, facilitynames); 254 if (fac < 0) 255 errx(1, "unknown facility name: %s", save); 256 *s++ = '.'; 257 } 258 else { 259 fac = 0; 260 s = save; 261 } 262 lev = decode(s, prioritynames); 263 if (lev < 0) 264 errx(1, "unknown priority name: %s", save); 265 return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK)); 266} 267 268int 269decode(char *name, CODE *codetab) 270{ 271 CODE *c; 272 273 if (isdigit(*name)) 274 return (atoi(name)); 275 276 for (c = codetab; c->c_name; c++) 277 if (!strcasecmp(name, c->c_name)) 278 return (c->c_val); 279 280 return (-1); 281} 282 283static void 284usage(void) 285{ 286 (void)fprintf(stderr, "usage: %s\n", 287 "logger [-46Ais] [-f file] [-h host] [-P port] [-p pri] [-t tag]\n" 288 " [message ...]" 289 ); 290 exit(1); 291} 292