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