execsnoop revision 1.2 1 1.1 christos #!/bin/sh
2 1.1 christos #
3 1.1 christos # execsnoop - snoop process execution as it occurs.
4 1.1 christos # Written using DTrace (Solaris 10 3/05).
5 1.1 christos #
6 1.2 wiz # $Id: execsnoop,v 1.2 2026/01/11 22:24:23 wiz Exp $
7 1.1 christos #
8 1.2 wiz # USAGE: execsnoop [-a|-A|-ehsv] [-c command]
9 1.1 christos #
10 1.1 christos # execsnoop # default output
11 1.1 christos #
12 1.1 christos # -a # print all data
13 1.1 christos # -A # dump all data, space delimited
14 1.1 christos # -e # safe output - parseable
15 1.1 christos # -s # print start time, us
16 1.1 christos # -v # print start time, string
17 1.1 christos # -c command # command name to snoop
18 1.1 christos # eg,
19 1.1 christos # execsnoop -v # human readable timestamps
20 1.1 christos # execsnoop -c ls # snoop ls commands only
21 1.1 christos #
22 1.1 christos # The parseable output ensures that the ARGS field doesn't contain
23 1.1 christos # any "\n"s, which normally sometimes can - and would wreck postprocessing.
24 1.1 christos #
25 1.1 christos # FIELDS:
26 1.1 christos # UID User ID
27 1.1 christos # PID Process ID
28 1.1 christos # PPID Parent Process ID
29 1.1 christos # COMM command name for the process
30 1.1 christos # ARGS argument listing for the process
31 1.1 christos # TIME timestamp for the command, us
32 1.1 christos # STRTIME timestamp for the command, string
33 1.1 christos #
34 1.1 christos # SEE ALSO: BSM auditing.
35 1.1 christos #
36 1.1 christos # COPYRIGHT: Copyright (c) 2005 Brendan Gregg.
37 1.1 christos #
38 1.1 christos # CDDL HEADER START
39 1.1 christos #
40 1.1 christos # The contents of this file are subject to the terms of the
41 1.1 christos # Common Development and Distribution License, Version 1.0 only
42 1.1 christos # (the "License"). You may not use this file except in compliance
43 1.1 christos # with the License.
44 1.1 christos #
45 1.1 christos # You can obtain a copy of the license at Docs/cddl1.txt
46 1.1 christos # or http://www.opensolaris.org/os/licensing.
47 1.1 christos # See the License for the specific language governing permissions
48 1.1 christos # and limitations under the License.
49 1.1 christos #
50 1.1 christos # CDDL HEADER END
51 1.1 christos #
52 1.1 christos # Author: Brendan Gregg [Sydney, Australia]
53 1.1 christos #
54 1.1 christos # 27-Mar-2004 Brendan Gregg Created this.
55 1.1 christos # 21-Jan-2005 " " Wrapped in sh to provide options.
56 1.1 christos # 08-May-2005 " " Rewritten for performance.
57 1.1 christos # 14-May-2005 " " Added zonename.
58 1.1 christos # 02-Jul-2005 " " Added projid, safe printing.
59 1.1 christos # 11-Sep-2005 " " Increased switchrate.
60 1.1 christos # 11-Sep-2005 " " Last update.
61 1.2 wiz #
62 1.1 christos
63 1.1 christos
64 1.1 christos ##############################
65 1.1 christos # --- Process Arguments ---
66 1.1 christos #
67 1.1 christos
68 1.1 christos ### default variables
69 1.1 christos opt_dump=0; opt_cmd=0; opt_time=0; opt_timestr=0; filter=0; command=.
70 1.2 wiz opt_safe=0
71 1.1 christos
72 1.1 christos ### process options
73 1.2 wiz while getopts aAc:ehsv name
74 1.1 christos do
75 1.1 christos case $name in
76 1.1 christos a) opt_time=1; opt_timestr=1; opt_jailid=1 ;;
77 1.1 christos A) opt_dump=1 ;;
78 1.1 christos c) opt_cmd=1; command=$OPTARG ;;
79 1.1 christos e) opt_safe=1 ;;
80 1.1 christos s) opt_time=1 ;;
81 1.1 christos v) opt_timestr=1 ;;
82 1.1 christos h|?) cat <<-END >&2
83 1.2 wiz USAGE: execsnoop [-A|-a|-ehsv] [-c command]
84 1.1 christos execsnoop # default output
85 1.2 wiz -A # dump all data, space delimited
86 1.1 christos -a # print all data
87 1.2 wiz -c command # command name to snoop
88 1.1 christos -e # safe output, parseable
89 1.1 christos -s # print start time, us
90 1.1 christos -v # print start time, string
91 1.2 wiz e.g.,
92 1.1 christos execsnoop -v # human readable timestamps
93 1.1 christos execsnoop -c ls # snoop ls commands only
94 1.1 christos END
95 1.1 christos exit 1
96 1.1 christos esac
97 1.1 christos done
98 1.1 christos
99 1.1 christos ### option logic
100 1.1 christos if [ $opt_dump -eq 1 ]; then
101 1.2 wiz opt_time=0; opt_timestr=0
102 1.1 christos fi
103 1.1 christos if [ $opt_cmd -eq 1 ]; then
104 1.1 christos filter=1
105 1.1 christos fi
106 1.1 christos
107 1.1 christos
108 1.1 christos #################################
109 1.1 christos # --- Main Program, DTrace ---
110 1.1 christos #
111 1.1 christos /usr/sbin/dtrace -n '
112 1.1 christos /*
113 1.1 christos * Command line arguments
114 1.1 christos */
115 1.1 christos inline int OPT_dump = '$opt_dump';
116 1.1 christos inline int OPT_cmd = '$opt_cmd';
117 1.1 christos inline int OPT_time = '$opt_time';
118 1.1 christos inline int OPT_timestr = '$opt_timestr';
119 1.1 christos inline int OPT_safe = '$opt_safe';
120 1.1 christos inline int FILTER = '$filter';
121 1.1 christos inline string COMMAND = "'$command'";
122 1.2 wiz
123 1.1 christos #pragma D option quiet
124 1.1 christos #pragma D option switchrate=10hz
125 1.2 wiz
126 1.1 christos /*
127 1.1 christos * Print header
128 1.1 christos */
129 1.2 wiz dtrace:::BEGIN
130 1.1 christos {
131 1.1 christos /* print optional headers */
132 1.1 christos OPT_time ? printf("%-14s ", "TIME") : 1;
133 1.1 christos OPT_timestr ? printf("%-20s ", "STRTIME") : 1;
134 1.1 christos
135 1.1 christos /* print main headers */
136 1.2 wiz OPT_dump ? printf("%s %s %s %s %s %s\n",
137 1.2 wiz "TIME", "UID", "PID", "PPID", "COMM", "ARGS") :
138 1.1 christos printf("%5s %6s %6s %s\n", "UID", "PID", "PPID", "ARGS");
139 1.1 christos }
140 1.1 christos
141 1.1 christos /*
142 1.1 christos * Print exec event
143 1.1 christos */
144 1.1 christos syscall::execve:return
145 1.2 wiz /(FILTER == 0) || (OPT_cmd == 1 && COMMAND == execname)/
146 1.1 christos {
147 1.1 christos /* print optional fields */
148 1.1 christos OPT_time ? printf("%-14d ", timestamp/1000) : 1;
149 1.1 christos OPT_timestr ? printf("%-20Y ", walltimestamp) : 1;
150 1.1 christos
151 1.1 christos /* print main data */
152 1.2 wiz OPT_dump ? printf("%d %d %d %d %s ", timestamp/1000,
153 1.2 wiz uid, pid, ppid, execname) :
154 1.1 christos printf("%5d %6d %6d ", uid, pid, ppid);
155 1.1 christos OPT_safe ? printf("%S\n", curpsinfo->pr_psargs) :
156 1.1 christos printf("%s\n", curpsinfo->pr_psargs);
157 1.1 christos }
158 1.1 christos '
159