1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
24*0Sstevel@tonic-gate 
25*0Sstevel@tonic-gate 
26*0Sstevel@tonic-gate /*
27*0Sstevel@tonic-gate  * Copyright (c) 1998 by Sun Microsystems, Inc.
28*0Sstevel@tonic-gate  * All rights reserved.
29*0Sstevel@tonic-gate  */
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate #include <stdio.h>
34*0Sstevel@tonic-gate #include <fcntl.h>
35*0Sstevel@tonic-gate #include <time.h>
36*0Sstevel@tonic-gate #include <stdlib.h>
37*0Sstevel@tonic-gate #include <string.h>
38*0Sstevel@tonic-gate #include <unistd.h>
39*0Sstevel@tonic-gate #include <stropts.h>
40*0Sstevel@tonic-gate #include <sys/strlog.h>
41*0Sstevel@tonic-gate 
42*0Sstevel@tonic-gate #define	CTLSIZE sizeof (struct log_ctl)
43*0Sstevel@tonic-gate #define	DATSIZE 8192
44*0Sstevel@tonic-gate #define	LOGDEV "/dev/log"
45*0Sstevel@tonic-gate #define	MAXTID 50
46*0Sstevel@tonic-gate 
47*0Sstevel@tonic-gate static int errflg = 0;	/* set if error in argument parsing */
48*0Sstevel@tonic-gate static int infile = 0;  /* set if using standard input for arguments */
49*0Sstevel@tonic-gate static int log;
50*0Sstevel@tonic-gate 
51*0Sstevel@tonic-gate static void prlog(FILE *log, struct log_ctl *lp, char *dp);
52*0Sstevel@tonic-gate static int getid(int ac, char **av, struct trace_ids *tp);
53*0Sstevel@tonic-gate static int convarg(char *ap);
54*0Sstevel@tonic-gate static char *getarg(void);
55*0Sstevel@tonic-gate 
56*0Sstevel@tonic-gate #define	numeric(c) ((c <= '9') && (c >= '0'))
57*0Sstevel@tonic-gate 
58*0Sstevel@tonic-gate static int
59*0Sstevel@tonic-gate convarg(char *ap)
60*0Sstevel@tonic-gate {
61*0Sstevel@tonic-gate 	short ids[2];
62*0Sstevel@tonic-gate 
63*0Sstevel@tonic-gate 	if (!ap)
64*0Sstevel@tonic-gate 		return (-2);
65*0Sstevel@tonic-gate 	if (numeric(*ap))
66*0Sstevel@tonic-gate 		return (atoi(ap));
67*0Sstevel@tonic-gate 	if (strcmp(ap, "all") == 0)
68*0Sstevel@tonic-gate 		return (-1);
69*0Sstevel@tonic-gate 	errflg = 1;
70*0Sstevel@tonic-gate 	return (-2);
71*0Sstevel@tonic-gate }
72*0Sstevel@tonic-gate 
73*0Sstevel@tonic-gate static char *
74*0Sstevel@tonic-gate getarg(void)
75*0Sstevel@tonic-gate {
76*0Sstevel@tonic-gate 	static char argbuf[40];
77*0Sstevel@tonic-gate 	static eofflg = 0;
78*0Sstevel@tonic-gate 	char *ap;
79*0Sstevel@tonic-gate 	int c;
80*0Sstevel@tonic-gate 
81*0Sstevel@tonic-gate 	if (eofflg) {
82*0Sstevel@tonic-gate 		infile = 0;
83*0Sstevel@tonic-gate 		return (NULL);
84*0Sstevel@tonic-gate 	}
85*0Sstevel@tonic-gate 
86*0Sstevel@tonic-gate 	ap = argbuf;
87*0Sstevel@tonic-gate 
88*0Sstevel@tonic-gate 	/*
89*0Sstevel@tonic-gate 	 * Scan to first significant character in standard input.
90*0Sstevel@tonic-gate 	 * If EOF is encountered turn off standard input scanning and
91*0Sstevel@tonic-gate 	 * return NULL
92*0Sstevel@tonic-gate 	 */
93*0Sstevel@tonic-gate 	while ((c = getchar()) == ' ' || c == '\n' || c == '\t')
94*0Sstevel@tonic-gate 		;
95*0Sstevel@tonic-gate 	if (c == EOF) {
96*0Sstevel@tonic-gate 		infile = 0;
97*0Sstevel@tonic-gate 		eofflg++;
98*0Sstevel@tonic-gate 		return (NULL);
99*0Sstevel@tonic-gate 	}
100*0Sstevel@tonic-gate 	/*
101*0Sstevel@tonic-gate 	 * collect token until whitespace is encountered.  Don't do anything
102*0Sstevel@tonic-gate 	 * with EOF here as it will be caught the next time around.
103*0Sstevel@tonic-gate 	 */
104*0Sstevel@tonic-gate 	while (1) {
105*0Sstevel@tonic-gate 		*ap++ = c;
106*0Sstevel@tonic-gate 		if ((c = getchar()) == ' ' || c == '\n' ||
107*0Sstevel@tonic-gate 		    c == '\t' || c == EOF) {
108*0Sstevel@tonic-gate 			if (c == EOF) eofflg++;
109*0Sstevel@tonic-gate 			*ap = '\0';
110*0Sstevel@tonic-gate 			return (argbuf);
111*0Sstevel@tonic-gate 		}
112*0Sstevel@tonic-gate 	}
113*0Sstevel@tonic-gate }
114*0Sstevel@tonic-gate 
115*0Sstevel@tonic-gate 
116*0Sstevel@tonic-gate static int
117*0Sstevel@tonic-gate getid(int ac, char **av, struct trace_ids *tp)
118*0Sstevel@tonic-gate {
119*0Sstevel@tonic-gate 	static index = 1;
120*0Sstevel@tonic-gate 
121*0Sstevel@tonic-gate 	/*
122*0Sstevel@tonic-gate 	 * if inside of standard input scan take arguments from there.
123*0Sstevel@tonic-gate 	 */
124*0Sstevel@tonic-gate retry:
125*0Sstevel@tonic-gate 	if (infile) {
126*0Sstevel@tonic-gate 		tp->ti_mid = convarg(getarg());
127*0Sstevel@tonic-gate 		tp->ti_sid = convarg(getarg());
128*0Sstevel@tonic-gate 		tp->ti_level = convarg(getarg());
129*0Sstevel@tonic-gate 		if (errflg)
130*0Sstevel@tonic-gate 			return (0);
131*0Sstevel@tonic-gate 		/*
132*0Sstevel@tonic-gate 		 * if the previous operations encountered EOF, infile
133*0Sstevel@tonic-gate 		 * will be set to zero.  The trace_ids structure must
134*0Sstevel@tonic-gate 		 * then be loaded from the command line arguments.
135*0Sstevel@tonic-gate 		 * Otherwise, the structure is now valid and should
136*0Sstevel@tonic-gate 		 * be returned.
137*0Sstevel@tonic-gate 		 */
138*0Sstevel@tonic-gate 		if (infile)
139*0Sstevel@tonic-gate 			return (1);
140*0Sstevel@tonic-gate 	}
141*0Sstevel@tonic-gate 	/*
142*0Sstevel@tonic-gate 	 * if we get here we are either taking arguments from the
143*0Sstevel@tonic-gate 	 * command line argument list or we hit end of file on standard
144*0Sstevel@tonic-gate 	 * input and should return to finish off the command line arguments
145*0Sstevel@tonic-gate 	 */
146*0Sstevel@tonic-gate 	if (index >= ac)
147*0Sstevel@tonic-gate 		return (0);
148*0Sstevel@tonic-gate 
149*0Sstevel@tonic-gate 	/*
150*0Sstevel@tonic-gate 	 * if a '-' is present, start parsing from standard input
151*0Sstevel@tonic-gate 	 */
152*0Sstevel@tonic-gate 	if (strcmp(av[index], "-") == 0) {
153*0Sstevel@tonic-gate 		infile = 1;
154*0Sstevel@tonic-gate 		index++;
155*0Sstevel@tonic-gate 		goto retry;
156*0Sstevel@tonic-gate 	}
157*0Sstevel@tonic-gate 
158*0Sstevel@tonic-gate 	/*
159*0Sstevel@tonic-gate 	 * Parsing from command line, make sure there are
160*0Sstevel@tonic-gate 	 * at least 3 arguments remaining.
161*0Sstevel@tonic-gate 	 */
162*0Sstevel@tonic-gate 	if ((index+2) >= ac)
163*0Sstevel@tonic-gate 		return (0);
164*0Sstevel@tonic-gate 
165*0Sstevel@tonic-gate 	tp->ti_mid = convarg(av[index++]);
166*0Sstevel@tonic-gate 	tp->ti_sid = convarg(av[index++]);
167*0Sstevel@tonic-gate 	tp->ti_level = convarg(av[index++]);
168*0Sstevel@tonic-gate 
169*0Sstevel@tonic-gate 	if (errflg)
170*0Sstevel@tonic-gate 		return (0);
171*0Sstevel@tonic-gate 	return (1);
172*0Sstevel@tonic-gate }
173*0Sstevel@tonic-gate 
174*0Sstevel@tonic-gate int
175*0Sstevel@tonic-gate main(int ac, char *av[])
176*0Sstevel@tonic-gate {
177*0Sstevel@tonic-gate 	int  n;
178*0Sstevel@tonic-gate 	char cbuf[CTLSIZE];
179*0Sstevel@tonic-gate 	char dbuf[DATSIZE];
180*0Sstevel@tonic-gate 	struct strioctl istr;
181*0Sstevel@tonic-gate 	struct strbuf ctl, dat;
182*0Sstevel@tonic-gate 	struct log_ctl *lp = (struct log_ctl *)cbuf;
183*0Sstevel@tonic-gate 	struct trace_ids tid[MAXTID];
184*0Sstevel@tonic-gate 	struct trace_ids *tp;
185*0Sstevel@tonic-gate 	int ntid;
186*0Sstevel@tonic-gate 	int val;
187*0Sstevel@tonic-gate 	int flag;
188*0Sstevel@tonic-gate 
189*0Sstevel@tonic-gate 	ctl.buf = cbuf;
190*0Sstevel@tonic-gate 	ctl.maxlen = CTLSIZE;
191*0Sstevel@tonic-gate 	dat.buf = dbuf;
192*0Sstevel@tonic-gate 	dat.len = dat.maxlen = DATSIZE;
193*0Sstevel@tonic-gate 
194*0Sstevel@tonic-gate 	log = open(LOGDEV, O_RDWR);
195*0Sstevel@tonic-gate 	if (log < 0) {
196*0Sstevel@tonic-gate 		fprintf(stderr, "ERROR: unable to open %s\n", LOGDEV);
197*0Sstevel@tonic-gate 		return (1);
198*0Sstevel@tonic-gate 	}
199*0Sstevel@tonic-gate 
200*0Sstevel@tonic-gate 	tp = tid;
201*0Sstevel@tonic-gate 	ntid = 0;
202*0Sstevel@tonic-gate 
203*0Sstevel@tonic-gate 	if (ac == 1) {
204*0Sstevel@tonic-gate 		ntid++;
205*0Sstevel@tonic-gate 		tid[0].ti_mid = -1;
206*0Sstevel@tonic-gate 		tid[0].ti_sid = -1;
207*0Sstevel@tonic-gate 		tid[0].ti_level = -1;
208*0Sstevel@tonic-gate 	} else {
209*0Sstevel@tonic-gate 		while (getid(ac, av, tp)) {
210*0Sstevel@tonic-gate 			ntid++;
211*0Sstevel@tonic-gate 			tp++;
212*0Sstevel@tonic-gate 		}
213*0Sstevel@tonic-gate 	}
214*0Sstevel@tonic-gate 
215*0Sstevel@tonic-gate 	if (errflg)
216*0Sstevel@tonic-gate 		return (errflg);
217*0Sstevel@tonic-gate 
218*0Sstevel@tonic-gate 	istr.ic_cmd = I_TRCLOG;
219*0Sstevel@tonic-gate 	istr.ic_dp = (char *)tid;
220*0Sstevel@tonic-gate 	istr.ic_len = ntid * sizeof (struct trace_ids);
221*0Sstevel@tonic-gate 	istr.ic_timout = 0;
222*0Sstevel@tonic-gate 	if (ioctl(log, I_STR, &istr) < 0) {
223*0Sstevel@tonic-gate 		fprintf(stderr, "ERROR: tracer already exists\n");
224*0Sstevel@tonic-gate 		return (1);
225*0Sstevel@tonic-gate 	}
226*0Sstevel@tonic-gate 
227*0Sstevel@tonic-gate 	setbuf(stdout, (char *)NULL);
228*0Sstevel@tonic-gate 	flag = 0;
229*0Sstevel@tonic-gate 	while (getmsg(log, &ctl, &dat, &flag) >= 0) {
230*0Sstevel@tonic-gate 		flag = 0;
231*0Sstevel@tonic-gate 		lp = (struct log_ctl *)cbuf;
232*0Sstevel@tonic-gate 		prlog(stdout, lp, dbuf);
233*0Sstevel@tonic-gate 	}
234*0Sstevel@tonic-gate 
235*0Sstevel@tonic-gate 	return (0);
236*0Sstevel@tonic-gate }
237*0Sstevel@tonic-gate 
238*0Sstevel@tonic-gate static void
239*0Sstevel@tonic-gate prlog(FILE *log, struct log_ctl *lp, char *dp)
240*0Sstevel@tonic-gate {
241*0Sstevel@tonic-gate 	char *ts;
242*0Sstevel@tonic-gate 	int *args;
243*0Sstevel@tonic-gate 	char *ap;
244*0Sstevel@tonic-gate 	time_t t = (time_t)lp->ttime;
245*0Sstevel@tonic-gate 
246*0Sstevel@tonic-gate 	ts = ctime(&t);
247*0Sstevel@tonic-gate 	ts[19] = '\0';
248*0Sstevel@tonic-gate 	fprintf(log, "%06d %s %08x %2d %s%s%s %d %d %s\n",
249*0Sstevel@tonic-gate 	    lp->seq_no, (ts+11), lp->ltime, lp->level,
250*0Sstevel@tonic-gate 	    ((lp->flags & SL_FATAL) ? "F" : "."),
251*0Sstevel@tonic-gate 	    ((lp->flags & SL_NOTIFY) ? "N" : "."),
252*0Sstevel@tonic-gate 	    ((lp->flags & SL_ERROR) ? "E" : "."),
253*0Sstevel@tonic-gate 	    lp->mid, lp->sid, dp);
254*0Sstevel@tonic-gate }
255