-
Notifications
You must be signed in to change notification settings - Fork 133
/
opensnoop
executable file
·244 lines (224 loc) · 7.02 KB
/
opensnoop
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
#!/usr/bin/sh
#
# opensnoop - snoop file opens as they occur.
# Written using DTrace (Solaris 10 3/05).
#
# $Id: opensnoop 3 2007-08-01 10:50:08Z brendan $
#
# USAGE: opensnoop [-a|-A|-ceghsvxZ] [-f pathname] [-n name] [-p PID]
#
# opensnoop # default output
#
# -a # print most data
# -A # dump all data, space delimited
# -c # print cwd of process
# -e # print errno value
# -g # print command arguments
# -s # print start time, us
# -v # print start time, string
# -x # only print failed opens
# -Z # print zonename
# -f pathname # file pathname to snoop
# -n name # command name to snoop
# -p PID # process ID to snoop
# eg,
# opensnoop -v # human readable timestamps
# opensnoop -e # see error codes
# opensnoop -f /etc/passwd # snoop this file only
#
# FIELDS:
# ZONE Zone name
# UID User ID
# PID Process ID
# PPID Parent Process ID
# FD file descriptor (-1 for error)
# ERR errno value (see /usr/include/sys/errno.h)
# CWD print current working directory of process
# PATH pathname for file open
# COMM command name for the process
# ARGS argument listing for the process
# TIME timestamp for the open event, us
# STRTIME timestamp for the open event, string
#
# SEE ALSO: truss, BSM auditing.
#
# COPYRIGHT: Copyright (c) 2006 Brendan Gregg.
#
# 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 Docs/cddl1.txt
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# CDDL HEADER END
#
# Author: Brendan Gregg [Sydney, Australia]
#
# 09-May-2004 Brendan Gregg Created this.
# 21-Jan-2005 " " Wrapped in sh to provide options.
# 08-May-2005 " " Rewritten for performance.
# 14-May-2005 " " Added errno.
# 28-Jun-2005 " " Added cwd, zonename.
# 17-Sep-2005 " " Increased switchrate, fixed page fault bug.
# 16-Jan-2006 " " Added -n, -p.
# 16-Jan-2006 " " Last update.
#
##############################
# --- Process Arguments ---
#
### Default variables
opt_dump=0; opt_file=0; opt_time=0; opt_timestr=0; opt_args=0
opt_zone=0; opt_cwd=0; opt_failonly=0; opt_err=0; filter=0; pathname=.
opt_name=0; opt_pid=0; pname=.; pid=0
### Process options
while getopts aAcef:ghn:p:svxZ name
do
case $name in
a) opt_time=1; opt_timestr=1; opt_args=1; opt_err=1 ;;
A) opt_dump=1 ;;
c) opt_cwd=1 ;;
e) opt_err=1 ;;
g) opt_args=1 ;;
f) opt_file=1; pathname=$OPTARG ;;
n) opt_name=1; pname=$OPTARG ;;
p) opt_pid=1; pid=$OPTARG ;;
s) opt_time=1 ;;
v) opt_timestr=1 ;;
x) opt_failonly=1 ;;
Z) opt_zone=1 ;;
h|?) cat <<-END >&2
USAGE: opensnoop [-a|-A|-ceghsvxZ] [-f pathname]
[-n name] [-p PID]
opensnoop # default output
-a # print most data
-A # dump all data, space delimited
-c # print cwd of process
-e # print errno value
-g # print command arguments
-s # print start time, us
-v # print start time, string
-x # only print failed opens
-Z # print zonename
-f pathname # pathname name to snoop
-n name # process name to snoop
-p PID # process ID to snoop
eg,
opensnoop -v # human readable timestamps
opensnoop -e # see error codes
opensnoop -f /etc/motd # snoop this file only
END
exit 1
esac
done
### Option logic
if [ $opt_dump -eq 1 ]; then
opt_zone=0; opt_cwd=0; opt_time=0; opt_timestr=0; opt_args=2
fi
if [ $opt_name -eq 1 -o $opt_pid -eq 1 ]; then
filter=1
fi
#################################
# --- Main Program, DTrace ---
#
/usr/sbin/dtrace -n '
/*
* Command line arguments
*/
inline int OPT_dump = '$opt_dump';
inline int OPT_file = '$opt_file';
inline int OPT_args = '$opt_args';
inline int OPT_cwd = '$opt_cwd';
inline int OPT_err = '$opt_err';
inline int OPT_zone = '$opt_zone';
inline int OPT_time = '$opt_time';
inline int OPT_timestr = '$opt_timestr';
inline int OPT_failonly = '$opt_failonly';
inline int OPT_pid = '$opt_pid';
inline int OPT_name = '$opt_name';
inline int FILTER = '$filter';
inline int PID = '$pid';
inline string PATHNAME = "'$pathname'";
inline string NAME = "'$pname'";
#pragma D option quiet
#pragma D option switchrate=10hz
/*
* Print header
*/
dtrace:::BEGIN
{
/*
* ternary operators are used to improve performance.
* OPT_args is unusual in that it can have one of three values.
*/
/* print optional headers */
OPT_time ? printf("%-14s ", "TIME") : 1;
OPT_timestr ? printf("%-20s ", "STRTIME") : 1;
OPT_zone ? printf("%-10s ", "ZONE") : 1;
/* print dump headers */
OPT_dump ? printf("%s %s %s %s %s %s %s %s %s %s %s", "ZONE",
"TIME", "UID", "PID", "PPID", "COMM", "FD", "ERR", "CWD",
"PATH", "ARGS") : printf("%5s %6s ","UID","PID");
/* print main headers */
OPT_args == 0 ? printf("%-12s ", "COMM") : 1;
OPT_dump == 0 ? printf("%3s ", "FD") : 1;
OPT_err ? printf("%3s ", "ERR") : 1;
OPT_cwd ? printf("%-20s ", "CWD") : 1;
OPT_dump == 0 ? printf("%-20s ", "PATH") : 1;
OPT_args == 1 ? printf("%s", "ARGS") : 1;
printf("\n");
}
/*
* Print open event
*/
syscall::open:entry, syscall::open64:entry
{
/* save pathname */
self->pathp = arg0;
/* default is to trace unless filtering */
self->ok = FILTER ? 0 : 1;
/* check each filter */
(OPT_name == 1 && NAME == execname) ? self->ok = 1 : 1;
(OPT_pid == 1 && PID == pid) ? self->ok = 1 : 1;
/* OPT_file is checked on return to ensure pathp is mapped */
}
syscall::open:return, syscall::open64:return
/self->ok && (! OPT_failonly || (int)arg0 < 0) &&
((OPT_file == 0) || (OPT_file == 1 && PATHNAME == copyinstr(self->pathp)))/
{
/* print optional fields */
OPT_time ? printf("%-14d ", timestamp/1000) : 1;
OPT_timestr ? printf("%-20Y ", walltimestamp) : 1;
OPT_zone ? printf("%-10s ", zonename) : 1;
/* print dump fields */
OPT_dump ? printf("%s %d %d %d %d %s %d %d %s %s %S", zonename,
timestamp/1000, uid, pid, ppid, execname, (int)arg0, errno,
cwd, copyinstr(self->pathp), curpsinfo->pr_psargs) :
printf("%5d %6d ", uid, pid);
/* print main fields */
OPT_args == 0 ? printf("%-12s ", execname) : 1;
OPT_dump == 0 ? printf("%3d ", (int)arg0) : 1;
OPT_err ? printf("%3d ", errno) : 1;
OPT_cwd ? printf("%-20s ", cwd) : 1;
OPT_dump == 0 ? printf("%-20s ", copyinstr(self->pathp)) : 1;
OPT_args == 1 ? printf("%S", curpsinfo->pr_psargs) : 1;
printf("\n");
/* cleanup */
self->pathp = 0;
self->ok = 0;
}
/*
* Cleanup
*/
syscall::open:return, syscall::open64:return
/self->ok/
{
self->pathp = 0;
self->ok = 0;
}
'