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 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
230Sstevel@tonic-gate /* All Rights Reserved */
240Sstevel@tonic-gate
250Sstevel@tonic-gate
260Sstevel@tonic-gate /*
27*1110Smeem * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
28*1110Smeem * Use is subject to license terms.
290Sstevel@tonic-gate */
300Sstevel@tonic-gate
310Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
320Sstevel@tonic-gate
330Sstevel@tonic-gate #include <stdio.h>
340Sstevel@tonic-gate #include <fcntl.h>
350Sstevel@tonic-gate #include <time.h>
360Sstevel@tonic-gate #include <stdlib.h>
370Sstevel@tonic-gate #include <string.h>
380Sstevel@tonic-gate #include <unistd.h>
390Sstevel@tonic-gate #include <stropts.h>
400Sstevel@tonic-gate #include <sys/strlog.h>
410Sstevel@tonic-gate
420Sstevel@tonic-gate #define CTLSIZE sizeof (struct log_ctl)
430Sstevel@tonic-gate #define DATSIZE 8192
440Sstevel@tonic-gate #define LOGDEV "/dev/log"
450Sstevel@tonic-gate #define MAXTID 50
460Sstevel@tonic-gate
470Sstevel@tonic-gate static int errflg = 0; /* set if error in argument parsing */
480Sstevel@tonic-gate static int infile = 0; /* set if using standard input for arguments */
490Sstevel@tonic-gate static int log;
500Sstevel@tonic-gate
510Sstevel@tonic-gate static void prlog(FILE *log, struct log_ctl *lp, char *dp);
520Sstevel@tonic-gate static int getid(int ac, char **av, struct trace_ids *tp);
530Sstevel@tonic-gate static int convarg(char *ap);
540Sstevel@tonic-gate static char *getarg(void);
550Sstevel@tonic-gate
560Sstevel@tonic-gate #define numeric(c) ((c <= '9') && (c >= '0'))
570Sstevel@tonic-gate
580Sstevel@tonic-gate static int
convarg(char * ap)590Sstevel@tonic-gate convarg(char *ap)
600Sstevel@tonic-gate {
610Sstevel@tonic-gate short ids[2];
620Sstevel@tonic-gate
630Sstevel@tonic-gate if (!ap)
640Sstevel@tonic-gate return (-2);
650Sstevel@tonic-gate if (numeric(*ap))
660Sstevel@tonic-gate return (atoi(ap));
670Sstevel@tonic-gate if (strcmp(ap, "all") == 0)
680Sstevel@tonic-gate return (-1);
690Sstevel@tonic-gate errflg = 1;
700Sstevel@tonic-gate return (-2);
710Sstevel@tonic-gate }
720Sstevel@tonic-gate
730Sstevel@tonic-gate static char *
getarg(void)740Sstevel@tonic-gate getarg(void)
750Sstevel@tonic-gate {
760Sstevel@tonic-gate static char argbuf[40];
77*1110Smeem static int eofflg = 0;
780Sstevel@tonic-gate char *ap;
790Sstevel@tonic-gate int c;
800Sstevel@tonic-gate
810Sstevel@tonic-gate if (eofflg) {
820Sstevel@tonic-gate infile = 0;
830Sstevel@tonic-gate return (NULL);
840Sstevel@tonic-gate }
850Sstevel@tonic-gate
860Sstevel@tonic-gate ap = argbuf;
870Sstevel@tonic-gate
880Sstevel@tonic-gate /*
890Sstevel@tonic-gate * Scan to first significant character in standard input.
900Sstevel@tonic-gate * If EOF is encountered turn off standard input scanning and
910Sstevel@tonic-gate * return NULL
920Sstevel@tonic-gate */
930Sstevel@tonic-gate while ((c = getchar()) == ' ' || c == '\n' || c == '\t')
940Sstevel@tonic-gate ;
950Sstevel@tonic-gate if (c == EOF) {
960Sstevel@tonic-gate infile = 0;
970Sstevel@tonic-gate eofflg++;
980Sstevel@tonic-gate return (NULL);
990Sstevel@tonic-gate }
1000Sstevel@tonic-gate /*
1010Sstevel@tonic-gate * collect token until whitespace is encountered. Don't do anything
1020Sstevel@tonic-gate * with EOF here as it will be caught the next time around.
1030Sstevel@tonic-gate */
1040Sstevel@tonic-gate while (1) {
1050Sstevel@tonic-gate *ap++ = c;
1060Sstevel@tonic-gate if ((c = getchar()) == ' ' || c == '\n' ||
1070Sstevel@tonic-gate c == '\t' || c == EOF) {
1080Sstevel@tonic-gate if (c == EOF) eofflg++;
1090Sstevel@tonic-gate *ap = '\0';
1100Sstevel@tonic-gate return (argbuf);
1110Sstevel@tonic-gate }
1120Sstevel@tonic-gate }
1130Sstevel@tonic-gate }
1140Sstevel@tonic-gate
1150Sstevel@tonic-gate
1160Sstevel@tonic-gate static int
getid(int ac,char ** av,struct trace_ids * tp)1170Sstevel@tonic-gate getid(int ac, char **av, struct trace_ids *tp)
1180Sstevel@tonic-gate {
119*1110Smeem static int index = 1;
1200Sstevel@tonic-gate
1210Sstevel@tonic-gate /*
1220Sstevel@tonic-gate * if inside of standard input scan take arguments from there.
1230Sstevel@tonic-gate */
1240Sstevel@tonic-gate retry:
1250Sstevel@tonic-gate if (infile) {
1260Sstevel@tonic-gate tp->ti_mid = convarg(getarg());
1270Sstevel@tonic-gate tp->ti_sid = convarg(getarg());
1280Sstevel@tonic-gate tp->ti_level = convarg(getarg());
1290Sstevel@tonic-gate if (errflg)
1300Sstevel@tonic-gate return (0);
1310Sstevel@tonic-gate /*
1320Sstevel@tonic-gate * if the previous operations encountered EOF, infile
1330Sstevel@tonic-gate * will be set to zero. The trace_ids structure must
1340Sstevel@tonic-gate * then be loaded from the command line arguments.
1350Sstevel@tonic-gate * Otherwise, the structure is now valid and should
1360Sstevel@tonic-gate * be returned.
1370Sstevel@tonic-gate */
1380Sstevel@tonic-gate if (infile)
1390Sstevel@tonic-gate return (1);
1400Sstevel@tonic-gate }
1410Sstevel@tonic-gate /*
1420Sstevel@tonic-gate * if we get here we are either taking arguments from the
1430Sstevel@tonic-gate * command line argument list or we hit end of file on standard
1440Sstevel@tonic-gate * input and should return to finish off the command line arguments
1450Sstevel@tonic-gate */
1460Sstevel@tonic-gate if (index >= ac)
1470Sstevel@tonic-gate return (0);
1480Sstevel@tonic-gate
1490Sstevel@tonic-gate /*
1500Sstevel@tonic-gate * if a '-' is present, start parsing from standard input
1510Sstevel@tonic-gate */
1520Sstevel@tonic-gate if (strcmp(av[index], "-") == 0) {
1530Sstevel@tonic-gate infile = 1;
1540Sstevel@tonic-gate index++;
1550Sstevel@tonic-gate goto retry;
1560Sstevel@tonic-gate }
1570Sstevel@tonic-gate
1580Sstevel@tonic-gate /*
1590Sstevel@tonic-gate * Parsing from command line, make sure there are
1600Sstevel@tonic-gate * at least 3 arguments remaining.
1610Sstevel@tonic-gate */
1620Sstevel@tonic-gate if ((index+2) >= ac)
1630Sstevel@tonic-gate return (0);
1640Sstevel@tonic-gate
1650Sstevel@tonic-gate tp->ti_mid = convarg(av[index++]);
1660Sstevel@tonic-gate tp->ti_sid = convarg(av[index++]);
1670Sstevel@tonic-gate tp->ti_level = convarg(av[index++]);
1680Sstevel@tonic-gate
1690Sstevel@tonic-gate if (errflg)
1700Sstevel@tonic-gate return (0);
1710Sstevel@tonic-gate return (1);
1720Sstevel@tonic-gate }
1730Sstevel@tonic-gate
1740Sstevel@tonic-gate int
main(int ac,char * av[])1750Sstevel@tonic-gate main(int ac, char *av[])
1760Sstevel@tonic-gate {
1770Sstevel@tonic-gate int n;
1780Sstevel@tonic-gate char cbuf[CTLSIZE];
1790Sstevel@tonic-gate char dbuf[DATSIZE];
1800Sstevel@tonic-gate struct strioctl istr;
1810Sstevel@tonic-gate struct strbuf ctl, dat;
1820Sstevel@tonic-gate struct log_ctl *lp = (struct log_ctl *)cbuf;
1830Sstevel@tonic-gate struct trace_ids tid[MAXTID];
1840Sstevel@tonic-gate struct trace_ids *tp;
1850Sstevel@tonic-gate int ntid;
1860Sstevel@tonic-gate int val;
1870Sstevel@tonic-gate int flag;
1880Sstevel@tonic-gate
1890Sstevel@tonic-gate ctl.buf = cbuf;
1900Sstevel@tonic-gate ctl.maxlen = CTLSIZE;
1910Sstevel@tonic-gate dat.buf = dbuf;
1920Sstevel@tonic-gate dat.len = dat.maxlen = DATSIZE;
1930Sstevel@tonic-gate
1940Sstevel@tonic-gate log = open(LOGDEV, O_RDWR);
1950Sstevel@tonic-gate if (log < 0) {
1960Sstevel@tonic-gate fprintf(stderr, "ERROR: unable to open %s\n", LOGDEV);
1970Sstevel@tonic-gate return (1);
1980Sstevel@tonic-gate }
1990Sstevel@tonic-gate
2000Sstevel@tonic-gate tp = tid;
2010Sstevel@tonic-gate ntid = 0;
2020Sstevel@tonic-gate
2030Sstevel@tonic-gate if (ac == 1) {
2040Sstevel@tonic-gate ntid++;
2050Sstevel@tonic-gate tid[0].ti_mid = -1;
2060Sstevel@tonic-gate tid[0].ti_sid = -1;
2070Sstevel@tonic-gate tid[0].ti_level = -1;
2080Sstevel@tonic-gate } else {
2090Sstevel@tonic-gate while (getid(ac, av, tp)) {
2100Sstevel@tonic-gate ntid++;
2110Sstevel@tonic-gate tp++;
2120Sstevel@tonic-gate }
2130Sstevel@tonic-gate }
2140Sstevel@tonic-gate
2150Sstevel@tonic-gate if (errflg)
2160Sstevel@tonic-gate return (errflg);
2170Sstevel@tonic-gate
2180Sstevel@tonic-gate istr.ic_cmd = I_TRCLOG;
2190Sstevel@tonic-gate istr.ic_dp = (char *)tid;
2200Sstevel@tonic-gate istr.ic_len = ntid * sizeof (struct trace_ids);
2210Sstevel@tonic-gate istr.ic_timout = 0;
2220Sstevel@tonic-gate if (ioctl(log, I_STR, &istr) < 0) {
2230Sstevel@tonic-gate fprintf(stderr, "ERROR: tracer already exists\n");
2240Sstevel@tonic-gate return (1);
2250Sstevel@tonic-gate }
2260Sstevel@tonic-gate
2270Sstevel@tonic-gate setbuf(stdout, (char *)NULL);
2280Sstevel@tonic-gate flag = 0;
2290Sstevel@tonic-gate while (getmsg(log, &ctl, &dat, &flag) >= 0) {
2300Sstevel@tonic-gate flag = 0;
2310Sstevel@tonic-gate lp = (struct log_ctl *)cbuf;
2320Sstevel@tonic-gate prlog(stdout, lp, dbuf);
2330Sstevel@tonic-gate }
2340Sstevel@tonic-gate
2350Sstevel@tonic-gate return (0);
2360Sstevel@tonic-gate }
2370Sstevel@tonic-gate
2380Sstevel@tonic-gate static void
prlog(FILE * log,struct log_ctl * lp,char * dp)2390Sstevel@tonic-gate prlog(FILE *log, struct log_ctl *lp, char *dp)
2400Sstevel@tonic-gate {
2410Sstevel@tonic-gate char *ts;
2420Sstevel@tonic-gate int *args;
2430Sstevel@tonic-gate char *ap;
2440Sstevel@tonic-gate time_t t = (time_t)lp->ttime;
2450Sstevel@tonic-gate
2460Sstevel@tonic-gate ts = ctime(&t);
2470Sstevel@tonic-gate ts[19] = '\0';
2480Sstevel@tonic-gate fprintf(log, "%06d %s %08x %2d %s%s%s %d %d %s\n",
2490Sstevel@tonic-gate lp->seq_no, (ts+11), lp->ltime, lp->level,
2500Sstevel@tonic-gate ((lp->flags & SL_FATAL) ? "F" : "."),
2510Sstevel@tonic-gate ((lp->flags & SL_NOTIFY) ? "N" : "."),
2520Sstevel@tonic-gate ((lp->flags & SL_ERROR) ? "E" : "."),
2530Sstevel@tonic-gate lp->mid, lp->sid, dp);
2540Sstevel@tonic-gate }
255