xref: /onnv-gate/usr/src/cmd/logger/logger.c (revision 236)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
23*236Schin  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
300Sstevel@tonic-gate /*
310Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
320Sstevel@tonic-gate  * The Regents of the University of California
330Sstevel@tonic-gate  * All Rights Reserved
340Sstevel@tonic-gate  *
350Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
360Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
370Sstevel@tonic-gate  * contributors.
380Sstevel@tonic-gate  */
390Sstevel@tonic-gate 
400Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
410Sstevel@tonic-gate 
420Sstevel@tonic-gate #include <sys/types.h>
430Sstevel@tonic-gate #include <unistd.h>
440Sstevel@tonic-gate #include <stdio.h>
450Sstevel@tonic-gate #include <syslog.h>
460Sstevel@tonic-gate #include <ctype.h>
470Sstevel@tonic-gate #include <stdlib.h>
480Sstevel@tonic-gate #include <string.h>
490Sstevel@tonic-gate #include <locale.h>
500Sstevel@tonic-gate #include <limits.h>
510Sstevel@tonic-gate #include <pwd.h>
520Sstevel@tonic-gate #include <errno.h>
530Sstevel@tonic-gate 
540Sstevel@tonic-gate #define	LOG_MARK	(LOG_NFACILITIES << 3)	/* mark "facility" */
550Sstevel@tonic-gate #define	LOGGER_BUFLEN	1024
560Sstevel@tonic-gate 
570Sstevel@tonic-gate struct code {
580Sstevel@tonic-gate 	char	*c_name;
590Sstevel@tonic-gate 	int	c_val;
600Sstevel@tonic-gate };
610Sstevel@tonic-gate 
620Sstevel@tonic-gate static struct code	PriNames[] = {
630Sstevel@tonic-gate 	"panic",	LOG_EMERG,
640Sstevel@tonic-gate 	"emerg",	LOG_EMERG,
650Sstevel@tonic-gate 	"alert",	LOG_ALERT,
660Sstevel@tonic-gate 	"crit",		LOG_CRIT,
670Sstevel@tonic-gate 	"err",		LOG_ERR,
680Sstevel@tonic-gate 	"error",	LOG_ERR,
690Sstevel@tonic-gate 	"warn",		LOG_WARNING,
700Sstevel@tonic-gate 	"warning", 	LOG_WARNING,
710Sstevel@tonic-gate 	"notice",	LOG_NOTICE,
720Sstevel@tonic-gate 	"info",		LOG_INFO,
730Sstevel@tonic-gate 	"debug",	LOG_DEBUG,
740Sstevel@tonic-gate 	NULL,		-1
750Sstevel@tonic-gate };
760Sstevel@tonic-gate 
770Sstevel@tonic-gate static struct code	FacNames[] = {
780Sstevel@tonic-gate 	"kern",		LOG_KERN,
790Sstevel@tonic-gate 	"user",		LOG_USER,
800Sstevel@tonic-gate 	"mail",		LOG_MAIL,
810Sstevel@tonic-gate 	"daemon",	LOG_DAEMON,
820Sstevel@tonic-gate 	"auth",		LOG_AUTH,
830Sstevel@tonic-gate 	"security",	LOG_AUTH,
840Sstevel@tonic-gate 	"mark",		LOG_MARK,
850Sstevel@tonic-gate 	"syslog",	LOG_SYSLOG,
860Sstevel@tonic-gate 	"lpr",		LOG_LPR,
870Sstevel@tonic-gate 	"news",		LOG_NEWS,
880Sstevel@tonic-gate 	"uucp",		LOG_UUCP,
890Sstevel@tonic-gate 	"cron",		LOG_CRON,
900Sstevel@tonic-gate 	"audit",	LOG_AUDIT,
910Sstevel@tonic-gate 	"local0",	LOG_LOCAL0,
920Sstevel@tonic-gate 	"local1",	LOG_LOCAL1,
930Sstevel@tonic-gate 	"local2",	LOG_LOCAL2,
940Sstevel@tonic-gate 	"local3",	LOG_LOCAL3,
950Sstevel@tonic-gate 	"local4",	LOG_LOCAL4,
960Sstevel@tonic-gate 	"local5",	LOG_LOCAL5,
970Sstevel@tonic-gate 	"local6",	LOG_LOCAL6,
980Sstevel@tonic-gate 	"local7",	LOG_LOCAL7,
990Sstevel@tonic-gate 	NULL,		-1
1000Sstevel@tonic-gate };
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate static int	pencode(register char *);
1030Sstevel@tonic-gate static int	decode(char *, struct code *);
1040Sstevel@tonic-gate static void	bailout(char *, char *);
1050Sstevel@tonic-gate static void	usage(void);
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate /*
1080Sstevel@tonic-gate  *  LOGGER -- read and log utility
1090Sstevel@tonic-gate  *
1100Sstevel@tonic-gate  *	This routine reads from an input and arranges to write the
1110Sstevel@tonic-gate  *	result on the system log, along with a useful tag.
1120Sstevel@tonic-gate  */
1130Sstevel@tonic-gate 
114*236Schin int
115*236Schin main(int argc, char **argv)
1160Sstevel@tonic-gate {
1170Sstevel@tonic-gate 	char tmp[23];
1180Sstevel@tonic-gate 	char *tag = NULL;
1190Sstevel@tonic-gate 	char *infile = NULL;
1200Sstevel@tonic-gate 	char *buf = NULL;
1210Sstevel@tonic-gate 	size_t buflen;
1220Sstevel@tonic-gate 	int pri = LOG_NOTICE;
1230Sstevel@tonic-gate 	int logflags = 0;
1240Sstevel@tonic-gate 	int opt;
1250Sstevel@tonic-gate 	int pid_len = 0;
1260Sstevel@tonic-gate 	struct passwd *pw;
1270Sstevel@tonic-gate 	uid_t u;
1280Sstevel@tonic-gate 	char fmt_uid[16];
1290Sstevel@tonic-gate 	char *p, *endp;
1300Sstevel@tonic-gate 	size_t len;
1310Sstevel@tonic-gate 	ptrdiff_t offset = 0;
1320Sstevel@tonic-gate 	int status = 0;
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1350Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
1360Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
1370Sstevel@tonic-gate #endif
1380Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
1390Sstevel@tonic-gate 	/* initialize */
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate 	while ((opt = getopt(argc, argv, "it:p:f:")) != EOF)
1420Sstevel@tonic-gate 		switch (opt) {
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate 		    case 't':		/* tag */
1450Sstevel@tonic-gate 			tag = optarg;
1460Sstevel@tonic-gate 			break;
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 		    case 'p':		/* priority */
1490Sstevel@tonic-gate 			pri = pencode(optarg);
1500Sstevel@tonic-gate 			break;
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate 		    case 'i':		/* log process id also */
1530Sstevel@tonic-gate 			logflags |= LOG_PID;
1540Sstevel@tonic-gate 			pid_len = sprintf(tmp, "%ld", (long)getpid());
1550Sstevel@tonic-gate 			pid_len = (pid_len <= 0) ? 0 : pid_len +2;
1560Sstevel@tonic-gate 			break;
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate 		    case 'f':		/* file to log */
1590Sstevel@tonic-gate 			if (strcmp(optarg, "-") == 0)
1600Sstevel@tonic-gate 				break;
1610Sstevel@tonic-gate 			infile = optarg;
162*236Schin 			if (freopen(infile, "r", stdin) == NULL) {
1630Sstevel@tonic-gate 				(void) fprintf(stderr, gettext("logger: "));
1640Sstevel@tonic-gate 				perror(infile);
1650Sstevel@tonic-gate 				exit(1);
1660Sstevel@tonic-gate 			}
1670Sstevel@tonic-gate 			break;
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate 		    default:
1700Sstevel@tonic-gate 			usage();
1710Sstevel@tonic-gate 		}
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate 		argc -= optind;
1740Sstevel@tonic-gate 		argv = &argv[optind];
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate 	if ((tag == NULL) && ((tag = getlogin()) == NULL)) {
1770Sstevel@tonic-gate 		u = getuid();
1780Sstevel@tonic-gate 		if ((pw = getpwuid(u)) == NULL) {
1790Sstevel@tonic-gate 			(void) sprintf(fmt_uid, "%ld", u);
1800Sstevel@tonic-gate 			tag = fmt_uid;
1810Sstevel@tonic-gate 		} else
1820Sstevel@tonic-gate 			tag = pw->pw_name;
1830Sstevel@tonic-gate 	}
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate 	/* setup for logging */
1860Sstevel@tonic-gate 	openlog(tag, logflags, 0);
1870Sstevel@tonic-gate 	(void) fclose(stdout);
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate 	/* log input line if appropriate */
1900Sstevel@tonic-gate 	if (argc > 0) {
1910Sstevel@tonic-gate 		/*
1920Sstevel@tonic-gate 		 * Log arguments from command line
1930Sstevel@tonic-gate 		 */
1940Sstevel@tonic-gate 		int i;
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate 		len = 0;
1970Sstevel@tonic-gate 		for (i = 0; i < argc; i++) {
1980Sstevel@tonic-gate 			len += strlen(argv[i]) + 1;	/* add 1 for <space> */
1990Sstevel@tonic-gate 		}
2000Sstevel@tonic-gate 		if ((buf = malloc(len + 1)) == NULL) {
2010Sstevel@tonic-gate 			perror("logger");
2020Sstevel@tonic-gate 			exit(1);
2030Sstevel@tonic-gate 		}
2040Sstevel@tonic-gate 		buf[0] = '\0';
2050Sstevel@tonic-gate 		for (i = 0; i < argc; i++) {
2060Sstevel@tonic-gate 			if (i != 0) {
2070Sstevel@tonic-gate 				(void) strcat(buf, " ");
2080Sstevel@tonic-gate 			}
2090Sstevel@tonic-gate 			(void) strcat(buf, argv[i]);
2100Sstevel@tonic-gate 		}
2110Sstevel@tonic-gate #ifdef DEBUG
2120Sstevel@tonic-gate 		(void) fprintf(stderr, "len=%d, buf >%s<\n", len, buf);
2130Sstevel@tonic-gate #endif
2140Sstevel@tonic-gate 		syslog(pri, "%s", buf);
2150Sstevel@tonic-gate 	} else {
2160Sstevel@tonic-gate 		/*
2170Sstevel@tonic-gate 		 * Log arguments from stdin (or input file).
2180Sstevel@tonic-gate 		 * When reading from stdin, logger grows its buffer if
2190Sstevel@tonic-gate 		 * needed, to handle long lines.
2200Sstevel@tonic-gate 		 */
2210Sstevel@tonic-gate 		if ((buf = malloc(LOGGER_BUFLEN)) == NULL) {
2220Sstevel@tonic-gate 			perror("logger");
2230Sstevel@tonic-gate 			exit(1);
2240Sstevel@tonic-gate 		}
2250Sstevel@tonic-gate 		buflen = LOGGER_BUFLEN;
2260Sstevel@tonic-gate 		p = buf;
2270Sstevel@tonic-gate 		endp = buf + buflen;
2280Sstevel@tonic-gate 		offset = 0;
2290Sstevel@tonic-gate 		while (fgets(p, endp - p, stdin) != NULL) {
2300Sstevel@tonic-gate 			len = strlen(p);
2310Sstevel@tonic-gate 			if (p[len - 1] == '\n') {
2320Sstevel@tonic-gate #ifdef DEBUG
2330Sstevel@tonic-gate 				(void) fprintf(stderr,
2340Sstevel@tonic-gate 				    "p-buf =%d, len=%d, buflen=%d, buf >%s<\n",
2350Sstevel@tonic-gate 				    p-buf, len, buflen, buf);
2360Sstevel@tonic-gate #endif
2370Sstevel@tonic-gate 				syslog(pri, "%s", buf);
2380Sstevel@tonic-gate 				p = buf;
2390Sstevel@tonic-gate 				offset = 0;
2400Sstevel@tonic-gate 			} else if (len < endp - p - 1) {
2410Sstevel@tonic-gate 				/* short read or line with no <newline> */
2420Sstevel@tonic-gate 				p += len;
2430Sstevel@tonic-gate 				offset += len;
2440Sstevel@tonic-gate #ifdef DEBUG
2450Sstevel@tonic-gate 				(void) fprintf(stderr,
2460Sstevel@tonic-gate 				    "p-buf=%d, len=%d, buflen=%d, buf >%s<\n",
2470Sstevel@tonic-gate 				    p-buf, len, buflen, buf);
2480Sstevel@tonic-gate #endif
2490Sstevel@tonic-gate 				continue;
2500Sstevel@tonic-gate 			} else {
2510Sstevel@tonic-gate 				/* line longer than buflen, so get larger buf */
2520Sstevel@tonic-gate 				buflen += LOGGER_BUFLEN;
2530Sstevel@tonic-gate 				offset += len;
2540Sstevel@tonic-gate #ifdef DEBUG
2550Sstevel@tonic-gate 				(void) fprintf(stderr,
2560Sstevel@tonic-gate 				    "Realloc endp-p=%d, len=%d, offset=%d, "
2570Sstevel@tonic-gate 				    "buflen %d\n",
2580Sstevel@tonic-gate 				    endp - p, len, offset, buflen);
2590Sstevel@tonic-gate #endif
2600Sstevel@tonic-gate 				if ((buf = realloc(buf, buflen)) == NULL) {
2610Sstevel@tonic-gate 					perror("logger");
2620Sstevel@tonic-gate 					exit(1);
2630Sstevel@tonic-gate 				}
2640Sstevel@tonic-gate 				p = buf + offset;
2650Sstevel@tonic-gate 				endp = buf + buflen;
2660Sstevel@tonic-gate 			}
2670Sstevel@tonic-gate 		}	/* while */
2680Sstevel@tonic-gate 
2690Sstevel@tonic-gate 		if (feof(stdin)) {
2700Sstevel@tonic-gate 			if (p > buf) {
2710Sstevel@tonic-gate 				/* the last line did not end with newline */
2720Sstevel@tonic-gate #ifdef DEBUG
2730Sstevel@tonic-gate 				(void) fprintf(stderr,
2740Sstevel@tonic-gate 				    "(2) p-buf=%d, len=%d, buflen=%d, "
2750Sstevel@tonic-gate 				    "buf >%s<\n",
2760Sstevel@tonic-gate 				    p-buf, len, buflen, buf);
2770Sstevel@tonic-gate #endif
2780Sstevel@tonic-gate 				syslog(pri, "%s", buf);
2790Sstevel@tonic-gate 			}
2800Sstevel@tonic-gate 		} else {
2810Sstevel@tonic-gate 			/*
2820Sstevel@tonic-gate 			 * fgets() encountered an error.  Log unlogged data
2830Sstevel@tonic-gate 			 * from earlier fgets() (if any).  Write null byte
2840Sstevel@tonic-gate 			 * after last full read, in case the fgets() that
2850Sstevel@tonic-gate 			 * encountered error removed it and failed to null
2860Sstevel@tonic-gate 			 * terminate.
2870Sstevel@tonic-gate 			 */
2880Sstevel@tonic-gate 			perror("logger");
2890Sstevel@tonic-gate 			if (p > buf) {
2900Sstevel@tonic-gate 				*p = '\0';
2910Sstevel@tonic-gate 				syslog(pri, "%s", buf);
2920Sstevel@tonic-gate 			}
2930Sstevel@tonic-gate 			status = 1;
2940Sstevel@tonic-gate 		}
2950Sstevel@tonic-gate 	}	/* else !(argc > 0) */
2960Sstevel@tonic-gate 	free(buf);
2970Sstevel@tonic-gate 	return (status);
2980Sstevel@tonic-gate }
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate /*
3010Sstevel@tonic-gate  *  Decode a symbolic name to a numeric value
3020Sstevel@tonic-gate  */
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate 
3050Sstevel@tonic-gate static int
3060Sstevel@tonic-gate pencode(s)
3070Sstevel@tonic-gate register char *s;
3080Sstevel@tonic-gate {
3090Sstevel@tonic-gate 	register char *p;
3100Sstevel@tonic-gate 	int lev;
3110Sstevel@tonic-gate 	int fac = 0;
3120Sstevel@tonic-gate 
3130Sstevel@tonic-gate 	for (p = s; *s && *s != '.'; s++);
3140Sstevel@tonic-gate 	if (*s) {
3150Sstevel@tonic-gate 		*s = '\0';
3160Sstevel@tonic-gate 		fac = decode(p, FacNames);
3170Sstevel@tonic-gate 		if (fac < 0)
3180Sstevel@tonic-gate 			bailout("unknown facility name: ", p);
3190Sstevel@tonic-gate 		*s++ = '.';
3200Sstevel@tonic-gate 	} else
3210Sstevel@tonic-gate 		s = p;
3220Sstevel@tonic-gate 	lev = decode(s, PriNames);
3230Sstevel@tonic-gate 	if (lev < 0)
3240Sstevel@tonic-gate 		bailout("unknown priority name: ", s);
3250Sstevel@tonic-gate 
3260Sstevel@tonic-gate 	return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK));
3270Sstevel@tonic-gate }
3280Sstevel@tonic-gate 
3290Sstevel@tonic-gate 
3300Sstevel@tonic-gate static int
3310Sstevel@tonic-gate decode(name, codetab)
3320Sstevel@tonic-gate char *name;
3330Sstevel@tonic-gate struct code *codetab;
3340Sstevel@tonic-gate {
3350Sstevel@tonic-gate 	register struct code *c;
3360Sstevel@tonic-gate 
3370Sstevel@tonic-gate 	if (isdigit(*name))
3380Sstevel@tonic-gate 		return (atoi(name));
3390Sstevel@tonic-gate 
3400Sstevel@tonic-gate 	for (c = codetab; c->c_name; c++)
3410Sstevel@tonic-gate 		if (strcasecmp(name, c->c_name) == 0)
3420Sstevel@tonic-gate 			return (c->c_val);
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate 	return (-1);
3450Sstevel@tonic-gate }
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate 
3480Sstevel@tonic-gate static void
3490Sstevel@tonic-gate bailout(a, b)
3500Sstevel@tonic-gate char *a, *b;
3510Sstevel@tonic-gate {
3520Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("logger: %s%s\n"), a, b);
3530Sstevel@tonic-gate 	exit(1);
3540Sstevel@tonic-gate }
3550Sstevel@tonic-gate 
3560Sstevel@tonic-gate 
3570Sstevel@tonic-gate static void
3580Sstevel@tonic-gate usage(void)
3590Sstevel@tonic-gate {
3600Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
3610Sstevel@tonic-gate 	    "Usage:\tlogger string\n"
3620Sstevel@tonic-gate 	    "\tlogger [-i] [-f filename] [-p priority] [-t tag] "
3630Sstevel@tonic-gate 		"[message] ...\n"));
3640Sstevel@tonic-gate 	exit(1);
3650Sstevel@tonic-gate }
366