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