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