1 /* $NetBSD: logger.c,v 1.17 2012/04/27 06:30:48 wiz Exp $ */ 2 3 /* 4 * Copyright (c) 1983, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 #ifndef lint 34 __COPYRIGHT("@(#) Copyright (c) 1983, 1993\ 35 The Regents of the University of California. All rights reserved."); 36 #endif /* not lint */ 37 38 #ifndef lint 39 #if 0 40 static char sccsid[] = "@(#)logger.c 8.1 (Berkeley) 6/6/93"; 41 #endif 42 __RCSID("$NetBSD: logger.c,v 1.17 2012/04/27 06:30:48 wiz Exp $"); 43 #endif /* not lint */ 44 45 #include <errno.h> 46 #include <unistd.h> 47 #include <stdlib.h> 48 #include <stdio.h> 49 #include <ctype.h> 50 #include <string.h> 51 #include <err.h> 52 53 #define SYSLOG_NAMES 54 #include <syslog.h> 55 56 static int decode(const char *, const CODE *); 57 static int pencode(char *); 58 __dead static void usage(void); 59 60 /* 61 * logger -- read and log utility 62 * 63 * Reads from an input and arranges to write the result on the system 64 * log. 65 */ 66 int 67 main(int argc, char *argv[]) 68 { 69 int ch, logflags, pri; 70 const char *tag; 71 const char *sd = "-"; 72 const char *msgid = "-"; 73 char buf[1024]; 74 75 tag = NULL; 76 pri = LOG_NOTICE; 77 logflags = 0; 78 while ((ch = getopt(argc, argv, "cd:f:im:np:st:")) != -1) 79 switch((char)ch) { 80 case 'c': /* log to console */ 81 logflags |= LOG_CONS; 82 break; 83 case 'd': /* structured data field */ 84 sd = optarg; 85 break; 86 case 'f': /* file to log */ 87 if (freopen(optarg, "r", stdin) == NULL) 88 err(EXIT_FAILURE, "%s", optarg); 89 break; 90 case 'i': /* log process id also */ 91 logflags |= LOG_PID; 92 break; 93 case 'm': /* msgid field */ 94 msgid = optarg; 95 break; 96 case 'n': /* open log file immediately */ 97 logflags |= LOG_NDELAY; 98 break; 99 case 'p': /* priority */ 100 pri = pencode(optarg); 101 break; 102 case 's': /* log to standard error */ 103 logflags |= LOG_PERROR; 104 break; 105 case 't': /* tag */ 106 tag = optarg; 107 break; 108 case '?': 109 default: 110 usage(); 111 } 112 argc -= optind; 113 argv += optind; 114 115 /* setup for logging */ 116 openlog(tag != NULL ? tag : getlogin(), logflags, 0); 117 (void)fclose(stdout); 118 119 /* log input line if appropriate */ 120 if (argc > 0) { 121 char *p, *endp; 122 size_t len; 123 124 for (p = buf, endp = buf + sizeof(buf) - 2; *argv != NULL;) { 125 len = strlen(*argv); 126 if (p + len > endp && p > buf) { 127 syslogp(pri, msgid, sd, "%s", buf); 128 p = buf; 129 } 130 if (len > sizeof(buf) - 1) 131 syslogp(pri, msgid, sd, "%s", *argv++); 132 else { 133 if (p != buf) 134 *p++ = ' '; 135 memmove(p, *argv++, len); 136 *(p += len) = '\0'; 137 } 138 } 139 if (p != buf) 140 syslogp(pri, msgid, sd, "%s", buf); 141 } else /* TODO: allow syslog-protocol messages from file/stdin 142 * but that will require parsing the line to split 143 * it into three fields. 144 */ 145 while (fgets(buf, sizeof(buf), stdin) != NULL) 146 syslogp(pri, msgid, sd, "%s", buf); 147 148 exit(EXIT_SUCCESS); 149 /* NOTREACHED */ 150 } 151 152 /* 153 * Decode a symbolic name to a numeric value 154 */ 155 static int 156 pencode(char *s) 157 { 158 char *save; 159 int fac, lev; 160 161 for (save = s; *s != '\0' && *s != '.'; ++s) 162 ; 163 if (*s != '\0') { 164 *s = '\0'; 165 fac = decode(save, facilitynames); 166 if (fac < 0) 167 errx(EXIT_FAILURE, "unknown facility name: %s", save); 168 *s++ = '.'; 169 } else { 170 fac = 0; 171 s = save; 172 } 173 lev = decode(s, prioritynames); 174 if (lev < 0) 175 errx(EXIT_FAILURE, "unknown priority name: %s", s); 176 return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK)); 177 } 178 179 static int 180 decode(const char *name, const CODE *codetab) 181 { 182 const CODE *c; 183 184 if (isdigit((unsigned char)*name)) 185 return (atoi(name)); 186 187 for (c = codetab; c->c_name != NULL; c++) 188 if (strcasecmp(name, c->c_name) == 0) 189 return (c->c_val); 190 191 return (-1); 192 } 193 194 static void 195 usage(void) 196 { 197 198 (void)fprintf(stderr, 199 "Usage: %s [-cins] [-d SD] [-f file] [-m msgid] " 200 "[-p pri] [-t tag] [message ...]\n", 201 getprogname()); 202 exit(EXIT_FAILURE); 203 } 204