2 * Copyright (c) 2011, Comtrol Corp.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <sys/param.h>
34 #ifdef HAVE_SYS_POLL_H
35 # include <sys/poll.h>
40 const char **paths_selected = NULL;
41 static unsigned num_selected = 0;
44 * Return true if specified path matches one that we're tracing.
47 pathmatch(const char *path)
51 for (i = 0; i < num_selected; ++i) {
52 if (strcmp(path, paths_selected[i]) == 0)
59 * Return true if specified path (in user-space) matches.
62 upathmatch(struct tcb *tcp, unsigned long upath)
64 char path[PATH_MAX + 1];
66 return umovestr(tcp, upath, sizeof path, path) > 0 &&
71 * Return true if specified fd maps to a path we're tracing.
74 fdmatch(struct tcb *tcp, int fd)
76 const char *path = getfdpath(tcp, fd);
78 return path && pathmatch(path);
82 * Add a path to the set we're tracing.
83 * Secifying NULL will delete all paths.
86 storepath(const char *path)
91 return; /* already in table */
94 paths_selected = realloc(paths_selected, num_selected * sizeof(paths_selected[0]));
97 paths_selected[i] = path;
101 * Get path associated with fd.
104 getfdpath(struct tcb *tcp, int fd)
106 static char path[PATH_MAX+1];
107 char linkpath[sizeof("/proc/%u/fd/%u") + 2 * sizeof(int)*3];
113 sprintf(linkpath, "/proc/%u/fd/%u", tcp->pid, fd);
114 n = readlink(linkpath, path, (sizeof path) - 1);
122 * Add a path to the set we're tracing. Also add the canonicalized
123 * version of the path. Secifying NULL will delete all paths.
126 pathtrace_select(const char *path)
132 rpath = realpath(path, NULL);
137 /* if realpath and specified path are same, we're done */
138 if (strcmp(path, rpath) == 0) {
143 fprintf(stderr, "Requested path '%s' resolved into '%s'\n",
149 * Return true if syscall accesses a selected path
150 * (or if no paths have been specified for tracing).
153 pathtrace_match(struct tcb *tcp)
155 const struct_sysent *s;
159 if (!(s->sys_flags & (TRACE_FILE | TRACE_DESC)))
163 * Check for special cases where we need to do something
164 * other than test arg[0].
167 if (s->sys_func == sys_dup2 ||
168 s->sys_func == sys_dup3 ||
169 s->sys_func == sys_sendfile ||
170 s->sys_func == sys_sendfile64 ||
171 s->sys_func == sys_tee)
174 return fdmatch(tcp, tcp->u_arg[0]) ||
175 fdmatch(tcp, tcp->u_arg[1]);
178 if (s->sys_func == sys_inotify_add_watch ||
179 s->sys_func == sys_faccessat ||
180 s->sys_func == sys_fchmodat ||
181 s->sys_func == sys_futimesat ||
182 s->sys_func == sys_mkdirat ||
183 s->sys_func == sys_unlinkat ||
184 s->sys_func == sys_newfstatat ||
185 s->sys_func == sys_mknodat ||
186 s->sys_func == sys_openat ||
187 s->sys_func == sys_readlinkat ||
188 s->sys_func == sys_utimensat ||
189 s->sys_func == sys_fchownat ||
190 s->sys_func == sys_pipe2)
193 return fdmatch(tcp, tcp->u_arg[0]) ||
194 upathmatch(tcp, tcp->u_arg[1]);
197 if (s->sys_func == sys_link ||
198 s->sys_func == sys_mount)
201 return upathmatch(tcp, tcp->u_arg[0]) ||
202 upathmatch(tcp, tcp->u_arg[1]);
205 if (s->sys_func == sys_quotactl)
208 return upathmatch(tcp, tcp->u_arg[1]);
211 if (s->sys_func == sys_renameat ||
212 s->sys_func == sys_linkat)
214 /* fd, path, fd, path */
215 return fdmatch(tcp, tcp->u_arg[0]) ||
216 fdmatch(tcp, tcp->u_arg[2]) ||
217 upathmatch(tcp, tcp->u_arg[1]) ||
218 upathmatch(tcp, tcp->u_arg[3]);
222 s->sys_func == sys_old_mmap ||
224 s->sys_func == sys_old_mmap_pgoff ||
226 s->sys_func == sys_mmap ||
227 s->sys_func == sys_mmap_pgoff ||
228 s->sys_func == sys_mmap_4koff
231 return fdmatch(tcp, tcp->u_arg[4]);
234 if (s->sys_func == sys_symlinkat) {
236 return fdmatch(tcp, tcp->u_arg[1]) ||
237 upathmatch(tcp, tcp->u_arg[0]) ||
238 upathmatch(tcp, tcp->u_arg[2]);
241 if (s->sys_func == sys_splice) {
242 /* fd, x, fd, x, x */
243 return fdmatch(tcp, tcp->u_arg[0]) ||
244 fdmatch(tcp, tcp->u_arg[2]);
247 if (s->sys_func == sys_epoll_ctl) {
249 return fdmatch(tcp, tcp->u_arg[2]);
252 if (s->sys_func == sys_select ||
253 s->sys_func == sys_oldselect ||
254 s->sys_func == sys_pselect6)
258 long *args, oldargs[5];
262 if (s->sys_func == sys_oldselect) {
263 if (umoven(tcp, tcp->u_arg[0], sizeof oldargs,
264 (char*) oldargs) < 0)
266 fprintf(stderr, "umoven() failed\n");
274 /* Beware of select(2^31-1, NULL, NULL, NULL) and similar... */
275 if (args[0] > 1024*1024)
279 fdsize = ((((nfds + 7) / 8) + sizeof(long) - 1)
281 fds = malloc(fdsize);
285 for (i = 1; i <= 3; ++i) {
289 if (umoven(tcp, args[i], fdsize, (char *) fds) < 0) {
290 fprintf(stderr, "umoven() failed\n");
294 for (j = 0; j < nfds; ++j)
295 if (FD_ISSET(j, fds) && fdmatch(tcp, j)) {
304 if (s->sys_func == sys_poll ||
305 s->sys_func == sys_ppoll)
309 unsigned long start, cur, end;
311 start = tcp->u_arg[0];
312 nfds = tcp->u_arg[1];
314 end = start + sizeof(fds) * nfds;
316 if (nfds == 0 || end < start)
319 for (cur = start; cur < end; cur += sizeof(fds))
320 if ((umoven(tcp, cur, sizeof fds, (char *) &fds) == 0)
321 && fdmatch(tcp, fds.fd))
327 if (s->sys_func == printargs ||
328 s->sys_func == sys_pipe ||
329 s->sys_func == sys_pipe2 ||
330 s->sys_func == sys_eventfd2 ||
331 s->sys_func == sys_eventfd ||
332 s->sys_func == sys_inotify_init1 ||
333 s->sys_func == sys_timerfd_create ||
334 s->sys_func == sys_timerfd_settime ||
335 s->sys_func == sys_timerfd_gettime ||
336 s->sys_func == sys_epoll_create ||
337 strcmp(s->sys_name, "fanotify_init") == 0)
340 * These have TRACE_FILE or TRACE_DESCRIPTOR set, but they
341 * don't have any file descriptor or path args to test.
347 * Our fallback position for calls that haven't already
348 * been handled is to just check arg[0].
351 if (s->sys_flags & TRACE_FILE)
352 return upathmatch(tcp, tcp->u_arg[0]);
354 if (s->sys_flags & TRACE_DESC)
355 return fdmatch(tcp, tcp->u_arg[0]);