17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate * with the License.
87c478bd9Sstevel@tonic-gate *
97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate * and limitations under the License.
137c478bd9Sstevel@tonic-gate *
147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate *
207c478bd9Sstevel@tonic-gate * CDDL HEADER END
217c478bd9Sstevel@tonic-gate */
227c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
237c478bd9Sstevel@tonic-gate /* All Rights Reserved */
247c478bd9Sstevel@tonic-gate
257c478bd9Sstevel@tonic-gate
267c478bd9Sstevel@tonic-gate /*
27*ae35285aSmeem * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
28*ae35285aSmeem * Use is subject to license terms.
297c478bd9Sstevel@tonic-gate */
307c478bd9Sstevel@tonic-gate
317c478bd9Sstevel@tonic-gate #include <stdio.h>
327c478bd9Sstevel@tonic-gate #include <fcntl.h>
337c478bd9Sstevel@tonic-gate #include <time.h>
347c478bd9Sstevel@tonic-gate #include <stdlib.h>
357c478bd9Sstevel@tonic-gate #include <string.h>
367c478bd9Sstevel@tonic-gate #include <unistd.h>
377c478bd9Sstevel@tonic-gate #include <stropts.h>
387c478bd9Sstevel@tonic-gate #include <sys/strlog.h>
397c478bd9Sstevel@tonic-gate
407c478bd9Sstevel@tonic-gate #define CTLSIZE sizeof (struct log_ctl)
417c478bd9Sstevel@tonic-gate #define DATSIZE 8192
427c478bd9Sstevel@tonic-gate #define LOGDEV "/dev/log"
437c478bd9Sstevel@tonic-gate #define MAXTID 50
447c478bd9Sstevel@tonic-gate
457c478bd9Sstevel@tonic-gate static int errflg = 0; /* set if error in argument parsing */
467c478bd9Sstevel@tonic-gate static int infile = 0; /* set if using standard input for arguments */
477c478bd9Sstevel@tonic-gate static int log;
487c478bd9Sstevel@tonic-gate
497c478bd9Sstevel@tonic-gate static void prlog(FILE *log, struct log_ctl *lp, char *dp);
507c478bd9Sstevel@tonic-gate static int getid(int ac, char **av, struct trace_ids *tp);
517c478bd9Sstevel@tonic-gate static int convarg(char *ap);
527c478bd9Sstevel@tonic-gate static char *getarg(void);
537c478bd9Sstevel@tonic-gate
547c478bd9Sstevel@tonic-gate #define numeric(c) ((c <= '9') && (c >= '0'))
557c478bd9Sstevel@tonic-gate
567c478bd9Sstevel@tonic-gate static int
convarg(char * ap)577c478bd9Sstevel@tonic-gate convarg(char *ap)
587c478bd9Sstevel@tonic-gate {
597c478bd9Sstevel@tonic-gate short ids[2];
607c478bd9Sstevel@tonic-gate
617c478bd9Sstevel@tonic-gate if (!ap)
627c478bd9Sstevel@tonic-gate return (-2);
637c478bd9Sstevel@tonic-gate if (numeric(*ap))
647c478bd9Sstevel@tonic-gate return (atoi(ap));
657c478bd9Sstevel@tonic-gate if (strcmp(ap, "all") == 0)
667c478bd9Sstevel@tonic-gate return (-1);
677c478bd9Sstevel@tonic-gate errflg = 1;
687c478bd9Sstevel@tonic-gate return (-2);
697c478bd9Sstevel@tonic-gate }
707c478bd9Sstevel@tonic-gate
717c478bd9Sstevel@tonic-gate static char *
getarg(void)727c478bd9Sstevel@tonic-gate getarg(void)
737c478bd9Sstevel@tonic-gate {
747c478bd9Sstevel@tonic-gate static char argbuf[40];
75*ae35285aSmeem static int eofflg = 0;
767c478bd9Sstevel@tonic-gate char *ap;
777c478bd9Sstevel@tonic-gate int c;
787c478bd9Sstevel@tonic-gate
797c478bd9Sstevel@tonic-gate if (eofflg) {
807c478bd9Sstevel@tonic-gate infile = 0;
817c478bd9Sstevel@tonic-gate return (NULL);
827c478bd9Sstevel@tonic-gate }
837c478bd9Sstevel@tonic-gate
847c478bd9Sstevel@tonic-gate ap = argbuf;
857c478bd9Sstevel@tonic-gate
867c478bd9Sstevel@tonic-gate /*
877c478bd9Sstevel@tonic-gate * Scan to first significant character in standard input.
887c478bd9Sstevel@tonic-gate * If EOF is encountered turn off standard input scanning and
897c478bd9Sstevel@tonic-gate * return NULL
907c478bd9Sstevel@tonic-gate */
917c478bd9Sstevel@tonic-gate while ((c = getchar()) == ' ' || c == '\n' || c == '\t')
927c478bd9Sstevel@tonic-gate ;
937c478bd9Sstevel@tonic-gate if (c == EOF) {
947c478bd9Sstevel@tonic-gate infile = 0;
957c478bd9Sstevel@tonic-gate eofflg++;
967c478bd9Sstevel@tonic-gate return (NULL);
977c478bd9Sstevel@tonic-gate }
987c478bd9Sstevel@tonic-gate /*
997c478bd9Sstevel@tonic-gate * collect token until whitespace is encountered. Don't do anything
1007c478bd9Sstevel@tonic-gate * with EOF here as it will be caught the next time around.
1017c478bd9Sstevel@tonic-gate */
1027c478bd9Sstevel@tonic-gate while (1) {
1037c478bd9Sstevel@tonic-gate *ap++ = c;
1047c478bd9Sstevel@tonic-gate if ((c = getchar()) == ' ' || c == '\n' ||
1057c478bd9Sstevel@tonic-gate c == '\t' || c == EOF) {
1067c478bd9Sstevel@tonic-gate if (c == EOF) eofflg++;
1077c478bd9Sstevel@tonic-gate *ap = '\0';
1087c478bd9Sstevel@tonic-gate return (argbuf);
1097c478bd9Sstevel@tonic-gate }
1107c478bd9Sstevel@tonic-gate }
1117c478bd9Sstevel@tonic-gate }
1127c478bd9Sstevel@tonic-gate
1137c478bd9Sstevel@tonic-gate
1147c478bd9Sstevel@tonic-gate static int
getid(int ac,char ** av,struct trace_ids * tp)1157c478bd9Sstevel@tonic-gate getid(int ac, char **av, struct trace_ids *tp)
1167c478bd9Sstevel@tonic-gate {
117*ae35285aSmeem static int index = 1;
1187c478bd9Sstevel@tonic-gate
1197c478bd9Sstevel@tonic-gate /*
1207c478bd9Sstevel@tonic-gate * if inside of standard input scan take arguments from there.
1217c478bd9Sstevel@tonic-gate */
1227c478bd9Sstevel@tonic-gate retry:
1237c478bd9Sstevel@tonic-gate if (infile) {
1247c478bd9Sstevel@tonic-gate tp->ti_mid = convarg(getarg());
1257c478bd9Sstevel@tonic-gate tp->ti_sid = convarg(getarg());
1267c478bd9Sstevel@tonic-gate tp->ti_level = convarg(getarg());
1277c478bd9Sstevel@tonic-gate if (errflg)
1287c478bd9Sstevel@tonic-gate return (0);
1297c478bd9Sstevel@tonic-gate /*
1307c478bd9Sstevel@tonic-gate * if the previous operations encountered EOF, infile
1317c478bd9Sstevel@tonic-gate * will be set to zero. The trace_ids structure must
1327c478bd9Sstevel@tonic-gate * then be loaded from the command line arguments.
1337c478bd9Sstevel@tonic-gate * Otherwise, the structure is now valid and should
1347c478bd9Sstevel@tonic-gate * be returned.
1357c478bd9Sstevel@tonic-gate */
1367c478bd9Sstevel@tonic-gate if (infile)
1377c478bd9Sstevel@tonic-gate return (1);
1387c478bd9Sstevel@tonic-gate }
1397c478bd9Sstevel@tonic-gate /*
1407c478bd9Sstevel@tonic-gate * if we get here we are either taking arguments from the
1417c478bd9Sstevel@tonic-gate * command line argument list or we hit end of file on standard
1427c478bd9Sstevel@tonic-gate * input and should return to finish off the command line arguments
1437c478bd9Sstevel@tonic-gate */
1447c478bd9Sstevel@tonic-gate if (index >= ac)
1457c478bd9Sstevel@tonic-gate return (0);
1467c478bd9Sstevel@tonic-gate
1477c478bd9Sstevel@tonic-gate /*
1487c478bd9Sstevel@tonic-gate * if a '-' is present, start parsing from standard input
1497c478bd9Sstevel@tonic-gate */
1507c478bd9Sstevel@tonic-gate if (strcmp(av[index], "-") == 0) {
1517c478bd9Sstevel@tonic-gate infile = 1;
1527c478bd9Sstevel@tonic-gate index++;
1537c478bd9Sstevel@tonic-gate goto retry;
1547c478bd9Sstevel@tonic-gate }
1557c478bd9Sstevel@tonic-gate
1567c478bd9Sstevel@tonic-gate /*
1577c478bd9Sstevel@tonic-gate * Parsing from command line, make sure there are
1587c478bd9Sstevel@tonic-gate * at least 3 arguments remaining.
1597c478bd9Sstevel@tonic-gate */
1607c478bd9Sstevel@tonic-gate if ((index+2) >= ac)
1617c478bd9Sstevel@tonic-gate return (0);
1627c478bd9Sstevel@tonic-gate
1637c478bd9Sstevel@tonic-gate tp->ti_mid = convarg(av[index++]);
1647c478bd9Sstevel@tonic-gate tp->ti_sid = convarg(av[index++]);
1657c478bd9Sstevel@tonic-gate tp->ti_level = convarg(av[index++]);
1667c478bd9Sstevel@tonic-gate
1677c478bd9Sstevel@tonic-gate if (errflg)
1687c478bd9Sstevel@tonic-gate return (0);
1697c478bd9Sstevel@tonic-gate return (1);
1707c478bd9Sstevel@tonic-gate }
1717c478bd9Sstevel@tonic-gate
1727c478bd9Sstevel@tonic-gate int
main(int ac,char * av[])1737c478bd9Sstevel@tonic-gate main(int ac, char *av[])
1747c478bd9Sstevel@tonic-gate {
1757c478bd9Sstevel@tonic-gate int n;
1767c478bd9Sstevel@tonic-gate char cbuf[CTLSIZE];
1777c478bd9Sstevel@tonic-gate char dbuf[DATSIZE];
1787c478bd9Sstevel@tonic-gate struct strioctl istr;
1797c478bd9Sstevel@tonic-gate struct strbuf ctl, dat;
1807c478bd9Sstevel@tonic-gate struct log_ctl *lp = (struct log_ctl *)cbuf;
1817c478bd9Sstevel@tonic-gate struct trace_ids tid[MAXTID];
1827c478bd9Sstevel@tonic-gate struct trace_ids *tp;
1837c478bd9Sstevel@tonic-gate int ntid;
1847c478bd9Sstevel@tonic-gate int val;
1857c478bd9Sstevel@tonic-gate int flag;
1867c478bd9Sstevel@tonic-gate
1877c478bd9Sstevel@tonic-gate ctl.buf = cbuf;
1887c478bd9Sstevel@tonic-gate ctl.maxlen = CTLSIZE;
1897c478bd9Sstevel@tonic-gate dat.buf = dbuf;
1907c478bd9Sstevel@tonic-gate dat.len = dat.maxlen = DATSIZE;
1917c478bd9Sstevel@tonic-gate
1927c478bd9Sstevel@tonic-gate log = open(LOGDEV, O_RDWR);
1937c478bd9Sstevel@tonic-gate if (log < 0) {
1947c478bd9Sstevel@tonic-gate fprintf(stderr, "ERROR: unable to open %s\n", LOGDEV);
1957c478bd9Sstevel@tonic-gate return (1);
1967c478bd9Sstevel@tonic-gate }
1977c478bd9Sstevel@tonic-gate
1987c478bd9Sstevel@tonic-gate tp = tid;
1997c478bd9Sstevel@tonic-gate ntid = 0;
2007c478bd9Sstevel@tonic-gate
2017c478bd9Sstevel@tonic-gate if (ac == 1) {
2027c478bd9Sstevel@tonic-gate ntid++;
2037c478bd9Sstevel@tonic-gate tid[0].ti_mid = -1;
2047c478bd9Sstevel@tonic-gate tid[0].ti_sid = -1;
2057c478bd9Sstevel@tonic-gate tid[0].ti_level = -1;
2067c478bd9Sstevel@tonic-gate } else {
2077c478bd9Sstevel@tonic-gate while (getid(ac, av, tp)) {
2087c478bd9Sstevel@tonic-gate ntid++;
2097c478bd9Sstevel@tonic-gate tp++;
2107c478bd9Sstevel@tonic-gate }
2117c478bd9Sstevel@tonic-gate }
2127c478bd9Sstevel@tonic-gate
2137c478bd9Sstevel@tonic-gate if (errflg)
2147c478bd9Sstevel@tonic-gate return (errflg);
2157c478bd9Sstevel@tonic-gate
2167c478bd9Sstevel@tonic-gate istr.ic_cmd = I_TRCLOG;
2177c478bd9Sstevel@tonic-gate istr.ic_dp = (char *)tid;
2187c478bd9Sstevel@tonic-gate istr.ic_len = ntid * sizeof (struct trace_ids);
2197c478bd9Sstevel@tonic-gate istr.ic_timout = 0;
2207c478bd9Sstevel@tonic-gate if (ioctl(log, I_STR, &istr) < 0) {
2217c478bd9Sstevel@tonic-gate fprintf(stderr, "ERROR: tracer already exists\n");
2227c478bd9Sstevel@tonic-gate return (1);
2237c478bd9Sstevel@tonic-gate }
2247c478bd9Sstevel@tonic-gate
2257c478bd9Sstevel@tonic-gate setbuf(stdout, (char *)NULL);
2267c478bd9Sstevel@tonic-gate flag = 0;
2277c478bd9Sstevel@tonic-gate while (getmsg(log, &ctl, &dat, &flag) >= 0) {
2287c478bd9Sstevel@tonic-gate flag = 0;
2297c478bd9Sstevel@tonic-gate lp = (struct log_ctl *)cbuf;
2307c478bd9Sstevel@tonic-gate prlog(stdout, lp, dbuf);
2317c478bd9Sstevel@tonic-gate }
2327c478bd9Sstevel@tonic-gate
2337c478bd9Sstevel@tonic-gate return (0);
2347c478bd9Sstevel@tonic-gate }
2357c478bd9Sstevel@tonic-gate
2367c478bd9Sstevel@tonic-gate static void
prlog(FILE * log,struct log_ctl * lp,char * dp)2377c478bd9Sstevel@tonic-gate prlog(FILE *log, struct log_ctl *lp, char *dp)
2387c478bd9Sstevel@tonic-gate {
2397c478bd9Sstevel@tonic-gate char *ts;
2407c478bd9Sstevel@tonic-gate int *args;
2417c478bd9Sstevel@tonic-gate char *ap;
2427c478bd9Sstevel@tonic-gate time_t t = (time_t)lp->ttime;
2437c478bd9Sstevel@tonic-gate
2447c478bd9Sstevel@tonic-gate ts = ctime(&t);
2457c478bd9Sstevel@tonic-gate ts[19] = '\0';
2467c478bd9Sstevel@tonic-gate fprintf(log, "%06d %s %08x %2d %s%s%s %d %d %s\n",
2477c478bd9Sstevel@tonic-gate lp->seq_no, (ts+11), lp->ltime, lp->level,
2487c478bd9Sstevel@tonic-gate ((lp->flags & SL_FATAL) ? "F" : "."),
2497c478bd9Sstevel@tonic-gate ((lp->flags & SL_NOTIFY) ? "N" : "."),
2507c478bd9Sstevel@tonic-gate ((lp->flags & SL_ERROR) ? "E" : "."),
2517c478bd9Sstevel@tonic-gate lp->mid, lp->sid, dp);
2527c478bd9Sstevel@tonic-gate }
253