18a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards/*
28a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards * Copyright (c) 2011, Comtrol Corp.
38a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards * All rights reserved.
48a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards *
58a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards * Redistribution and use in source and binary forms, with or without
68a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards * modification, are permitted provided that the following conditions
78a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards * are met:
88a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards * 1. Redistributions of source code must retain the above copyright
98a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards *    notice, this list of conditions and the following disclaimer.
108a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards * 2. Redistributions in binary form must reproduce the above copyright
118a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards *    notice, this list of conditions and the following disclaimer in the
128a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards *    documentation and/or other materials provided with the distribution.
138a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards * 3. The name of the author may not be used to endorse or promote products
148a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards *    derived from this software without specific prior written permission.
158a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards *
168a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
178a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
188a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
198a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
208a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
218a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
228a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
238a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
248a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
258a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
268a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards *
278a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards */
288a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards
298a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards#include "defs.h"
30b038a43d756c84f98868557a1bbacd048faf1e53Dmitry V. Levin#include <sys/param.h>
31d64a7e4755abe2d073676741f38328aa0b83db9fDmitry V. Levin#if defined HAVE_POLL_H
32a6d91ded3f2d5401e09a9c2fa442aabfbfe593a8Denys Vlasenko# include <poll.h>
33d64a7e4755abe2d073676741f38328aa0b83db9fDmitry V. Levin#elif defined HAVE_SYS_POLL_H
34a6d91ded3f2d5401e09a9c2fa442aabfbfe593a8Denys Vlasenko# include <sys/poll.h>
358a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards#endif
368a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards
378a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards#include "syscall.h"
388a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards
3938cfe7c92328e940c71074c9b579b5bf9f2cb2f6Denys Vlasenkoconst char **paths_selected = NULL;
407239dbcf2dff792f408c60fe3e278f707fdf497bDenys Vlasenkostatic unsigned num_selected = 0;
418a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards
428a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards/*
438a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards * Return true if specified path matches one that we're tracing.
448a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards */
458a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwardsstatic int
468a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwardspathmatch(const char *path)
478a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards{
487239dbcf2dff792f408c60fe3e278f707fdf497bDenys Vlasenko	unsigned i;
498a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards
507239dbcf2dff792f408c60fe3e278f707fdf497bDenys Vlasenko	for (i = 0; i < num_selected; ++i) {
5138cfe7c92328e940c71074c9b579b5bf9f2cb2f6Denys Vlasenko		if (strcmp(path, paths_selected[i]) == 0)
528a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards			return 1;
538a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards	}
548a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards	return 0;
558a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards}
568a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards
578a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards/*
588a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards * Return true if specified path (in user-space) matches.
598a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards */
608a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwardsstatic int
618a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwardsupathmatch(struct tcb *tcp, unsigned long upath)
628a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards{
637b609d5ba0852e6c56ba311350ebd4412361777bDenys Vlasenko	char path[PATH_MAX + 1];
648a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards
651a880cf3bd8c12af6d831c2dfa5b259be074d77aDmitry V. Levin	return umovestr(tcp, upath, sizeof path, path) > 0 &&
668a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards		pathmatch(path);
678a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards}
688a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards
698a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards/*
708a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards * Return true if specified fd maps to a path we're tracing.
718a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards */
728a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwardsstatic int
738a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwardsfdmatch(struct tcb *tcp, int fd)
748a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards{
7561ad0a401c6d1b7b4c1a209f9d61c3790e1e7d15Denys Vlasenko	char path[PATH_MAX + 1];
7661ad0a401c6d1b7b4c1a209f9d61c3790e1e7d15Denys Vlasenko	int n = getfdpath(tcp, fd, path, sizeof(path));
778a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards
7861ad0a401c6d1b7b4c1a209f9d61c3790e1e7d15Denys Vlasenko	return n >= 0 && pathmatch(path);
798a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards}
808a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards
818a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards/*
828a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards * Add a path to the set we're tracing.
83894c7e3858ede412e37a1752c716bed7f30a9605Philippe Ombredanne * Specifying NULL will delete all paths.
848a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards */
857239dbcf2dff792f408c60fe3e278f707fdf497bDenys Vlasenkostatic void
868a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwardsstorepath(const char *path)
878a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards{
887239dbcf2dff792f408c60fe3e278f707fdf497bDenys Vlasenko	unsigned i;
898a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards
907239dbcf2dff792f408c60fe3e278f707fdf497bDenys Vlasenko	if (pathmatch(path))
917239dbcf2dff792f408c60fe3e278f707fdf497bDenys Vlasenko		return; /* already in table */
928a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards
937239dbcf2dff792f408c60fe3e278f707fdf497bDenys Vlasenko	i = num_selected++;
9438cfe7c92328e940c71074c9b579b5bf9f2cb2f6Denys Vlasenko	paths_selected = realloc(paths_selected, num_selected * sizeof(paths_selected[0]));
9538cfe7c92328e940c71074c9b579b5bf9f2cb2f6Denys Vlasenko	if (!paths_selected)
967239dbcf2dff792f408c60fe3e278f707fdf497bDenys Vlasenko		die_out_of_memory();
9738cfe7c92328e940c71074c9b579b5bf9f2cb2f6Denys Vlasenko	paths_selected[i] = path;
988a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards}
998a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards
1008a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards/*
1018a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards * Get path associated with fd.
1028a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards */
10361ad0a401c6d1b7b4c1a209f9d61c3790e1e7d15Denys Vlasenkoint
10461ad0a401c6d1b7b4c1a209f9d61c3790e1e7d15Denys Vlasenkogetfdpath(struct tcb *tcp, int fd, char *buf, unsigned bufsize)
1058a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards{
106384b0ada7d821f3797bf5d72dcf096032d662193Denys Vlasenko	char linkpath[sizeof("/proc/%u/fd/%u") + 2 * sizeof(int)*3];
1078a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards	ssize_t n;
1088a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards
1098a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards	if (fd < 0)
11061ad0a401c6d1b7b4c1a209f9d61c3790e1e7d15Denys Vlasenko		return -1;
1118a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards
11229865e77e6eee0c380befd0ce53657b5604975efDenys Vlasenko	sprintf(linkpath, "/proc/%u/fd/%u", tcp->pid, fd);
11361ad0a401c6d1b7b4c1a209f9d61c3790e1e7d15Denys Vlasenko	n = readlink(linkpath, buf, bufsize - 1);
11461ad0a401c6d1b7b4c1a209f9d61c3790e1e7d15Denys Vlasenko	/*
11561ad0a401c6d1b7b4c1a209f9d61c3790e1e7d15Denys Vlasenko	 * NB: if buf is too small, readlink doesn't fail,
11661ad0a401c6d1b7b4c1a209f9d61c3790e1e7d15Denys Vlasenko	 * it returns truncated result (IOW: n == bufsize - 1).
11761ad0a401c6d1b7b4c1a209f9d61c3790e1e7d15Denys Vlasenko	 */
11861ad0a401c6d1b7b4c1a209f9d61c3790e1e7d15Denys Vlasenko	if (n >= 0)
11961ad0a401c6d1b7b4c1a209f9d61c3790e1e7d15Denys Vlasenko		buf[n] = '\0';
12061ad0a401c6d1b7b4c1a209f9d61c3790e1e7d15Denys Vlasenko	return n;
1218a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards}
1228a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards
1238a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards/*
1248a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards * Add a path to the set we're tracing.  Also add the canonicalized
1258a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards * version of the path.  Secifying NULL will delete all paths.
1268a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards */
1277239dbcf2dff792f408c60fe3e278f707fdf497bDenys Vlasenkovoid
1288a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwardspathtrace_select(const char *path)
1298a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards{
13029865e77e6eee0c380befd0ce53657b5604975efDenys Vlasenko	char *rpath;
1318a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards
1327239dbcf2dff792f408c60fe3e278f707fdf497bDenys Vlasenko	storepath(path);
1338a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards
1348a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards	rpath = realpath(path, NULL);
1358a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards
1368a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards	if (rpath == NULL)
1377239dbcf2dff792f408c60fe3e278f707fdf497bDenys Vlasenko		return;
1388a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards
1398a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards	/* if realpath and specified path are same, we're done */
14029865e77e6eee0c380befd0ce53657b5604975efDenys Vlasenko	if (strcmp(path, rpath) == 0) {
1418a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards		free(rpath);
1427239dbcf2dff792f408c60fe3e278f707fdf497bDenys Vlasenko		return;
1438a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards	}
1448a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards
1458a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards	fprintf(stderr, "Requested path '%s' resolved into '%s'\n",
1468a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards		path, rpath);
1477239dbcf2dff792f408c60fe3e278f707fdf497bDenys Vlasenko	storepath(rpath);
1488a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards}
1498a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards
1508a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards/*
1518a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards * Return true if syscall accesses a selected path
1528a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards * (or if no paths have been specified for tracing).
1538a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards */
1548a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwardsint
1558a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwardspathtrace_match(struct tcb *tcp)
1568a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards{
157a9fe13c9437707fa132fde4e51a20d88381e7430Denys Vlasenko	const struct_sysent *s;
1588a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards
15974ec14f968a418691b851cbbfeb0269174c64b08Denys Vlasenko	s = tcp->s_ent;
1608a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards
161894c7e3858ede412e37a1752c716bed7f30a9605Philippe Ombredanne	if (!(s->sys_flags & (TRACE_FILE | TRACE_DESC | TRACE_NETWORK)))
1628a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards		return 0;
1638a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards
1648a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards	/*
1658a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards	 * Check for special cases where we need to do something
1668a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards	 * other than test arg[0].
1678a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards	 */
1688a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards
1698a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards	if (s->sys_func == sys_dup2 ||
1708a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards	    s->sys_func == sys_dup3 ||
1718a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards	    s->sys_func == sys_sendfile ||
1728a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards	    s->sys_func == sys_sendfile64 ||
173ad178c0dc050a0bd68179ca60f3aebe0ff6ddd89Dmitry V. Levin	    s->sys_func == sys_tee)
1748a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards	{
1758a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards		/* fd, fd */
1768a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards		return fdmatch(tcp, tcp->u_arg[0]) ||
1778a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards			fdmatch(tcp, tcp->u_arg[1]);
1788a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards	}
1798a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards
1808a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards	if (s->sys_func == sys_inotify_add_watch ||
1818a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards	    s->sys_func == sys_faccessat ||
1828a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards	    s->sys_func == sys_fchmodat ||
1838a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards	    s->sys_func == sys_futimesat ||
1848a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards	    s->sys_func == sys_unlinkat ||
1858a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards	    s->sys_func == sys_newfstatat ||
1868a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards	    s->sys_func == sys_mknodat ||
1878a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards	    s->sys_func == sys_openat ||
1888a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards	    s->sys_func == sys_readlinkat ||
1898a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards	    s->sys_func == sys_utimensat ||
1908a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards	    s->sys_func == sys_fchownat ||
191b63256e69bf3f1a74aadb0e14556490bc8f4ef95Denys Vlasenko	    s->sys_func == sys_pipe2)
1928a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards	{
1938a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards		/* fd, path */
1948a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards		return fdmatch(tcp, tcp->u_arg[0]) ||
1958a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards			upathmatch(tcp, tcp->u_arg[1]);
1968a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards	}
1978a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards
1988a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards	if (s->sys_func == sys_link ||
199b63256e69bf3f1a74aadb0e14556490bc8f4ef95Denys Vlasenko	    s->sys_func == sys_mount)
2008a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards	{
2018a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards		/* path, path */
2028a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards		return upathmatch(tcp, tcp->u_arg[0]) ||
2038a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards			upathmatch(tcp, tcp->u_arg[1]);
2048a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards	}
2058a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards
2067943966f6cd137e44f27d56aaf21b023fb1fae63Dmitry V. Levin	if (s->sys_func == sys_quotactl)
2077943966f6cd137e44f27d56aaf21b023fb1fae63Dmitry V. Levin	{
2087943966f6cd137e44f27d56aaf21b023fb1fae63Dmitry V. Levin		/* x, path */
2097943966f6cd137e44f27d56aaf21b023fb1fae63Dmitry V. Levin		return upathmatch(tcp, tcp->u_arg[1]);
2107943966f6cd137e44f27d56aaf21b023fb1fae63Dmitry V. Levin	}
2117943966f6cd137e44f27d56aaf21b023fb1fae63Dmitry V. Levin
2128a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards	if (s->sys_func == sys_renameat ||
2135b677ab904f4ab8b272fdc78b2b9e8321412392dMike Frysinger	    s->sys_func == sys_renameat2 ||
214b63256e69bf3f1a74aadb0e14556490bc8f4ef95Denys Vlasenko	    s->sys_func == sys_linkat)
2158a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards	{
2168a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards		/* fd, path, fd, path */
2178a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards		return fdmatch(tcp, tcp->u_arg[0]) ||
2188a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards			fdmatch(tcp, tcp->u_arg[2]) ||
2198a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards			upathmatch(tcp, tcp->u_arg[1]) ||
2208a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards			upathmatch(tcp, tcp->u_arg[3]);
2218a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards	}
2228a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards
22335be58119e3727a694d8c9f9b83f372401fcc4c0H.J. Lu	if (
22435be58119e3727a694d8c9f9b83f372401fcc4c0H.J. Lu	    s->sys_func == sys_old_mmap ||
2251ba85436def7da80971aeb902fbc6e52997a46faDenys Vlasenko#if defined(S390)
2261ba85436def7da80971aeb902fbc6e52997a46faDenys Vlasenko	    s->sys_func == sys_old_mmap_pgoff ||
2271ba85436def7da80971aeb902fbc6e52997a46faDenys Vlasenko#endif
2281ba85436def7da80971aeb902fbc6e52997a46faDenys Vlasenko	    s->sys_func == sys_mmap ||
2291ba85436def7da80971aeb902fbc6e52997a46faDenys Vlasenko	    s->sys_func == sys_mmap_pgoff ||
2301ba85436def7da80971aeb902fbc6e52997a46faDenys Vlasenko	    s->sys_func == sys_mmap_4koff
2311ba85436def7da80971aeb902fbc6e52997a46faDenys Vlasenko	) {
2328a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards		/* x, x, x, x, fd */
2338a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards		return fdmatch(tcp, tcp->u_arg[4]);
2348a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards	}
2358a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards
2367b609d5ba0852e6c56ba311350ebd4412361777bDenys Vlasenko	if (s->sys_func == sys_symlinkat) {
2378a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards		/* path, fd, path */
2388a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards		return fdmatch(tcp, tcp->u_arg[1]) ||
2398a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards			upathmatch(tcp, tcp->u_arg[0]) ||
240b63256e69bf3f1a74aadb0e14556490bc8f4ef95Denys Vlasenko			upathmatch(tcp, tcp->u_arg[2]);
2418a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards	}
2428a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards
243ad178c0dc050a0bd68179ca60f3aebe0ff6ddd89Dmitry V. Levin	if (s->sys_func == sys_splice) {
2448a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards		/* fd, x, fd, x, x */
2458a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards		return fdmatch(tcp, tcp->u_arg[0]) ||
2468a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards			fdmatch(tcp, tcp->u_arg[2]);
2478a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards	}
2488a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards
2497b609d5ba0852e6c56ba311350ebd4412361777bDenys Vlasenko	if (s->sys_func == sys_epoll_ctl) {
2508a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards		/* x, x, fd, x */
2518a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards		return fdmatch(tcp, tcp->u_arg[2]);
2528a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards	}
2538a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards
25499db95dd039d8fd700e7759bf4969abb55b979b9Dmitry V. Levin	if (s->sys_func == sys_fanotify_mark) {
25599db95dd039d8fd700e7759bf4969abb55b979b9Dmitry V. Levin		/* x, x, x, fd, path */
25699db95dd039d8fd700e7759bf4969abb55b979b9Dmitry V. Levin		return fdmatch(tcp, tcp->u_arg[3]) ||
25799db95dd039d8fd700e7759bf4969abb55b979b9Dmitry V. Levin			upathmatch(tcp, tcp->u_arg[4]);
25899db95dd039d8fd700e7759bf4969abb55b979b9Dmitry V. Levin	}
25999db95dd039d8fd700e7759bf4969abb55b979b9Dmitry V. Levin
2608a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards	if (s->sys_func == sys_select ||
2618a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards	    s->sys_func == sys_oldselect ||
262b63256e69bf3f1a74aadb0e14556490bc8f4ef95Denys Vlasenko	    s->sys_func == sys_pselect6)
2638a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards	{
26479a79ea851250a30005d6323b20a9e30d9f336c1Denys Vlasenko		int     i, j;
26564778cbfa62d9584c59d3a48b69ba1ebf8bd24e9Denys Vlasenko		int     nfds;
2668a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards		long   *args, oldargs[5];
2678a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards		unsigned fdsize;
2688a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards		fd_set *fds;
2698a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards
27064778cbfa62d9584c59d3a48b69ba1ebf8bd24e9Denys Vlasenko		args = tcp->u_arg;
2717b609d5ba0852e6c56ba311350ebd4412361777bDenys Vlasenko		if (s->sys_func == sys_oldselect) {
2728a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards			if (umoven(tcp, tcp->u_arg[0], sizeof oldargs,
2737e69ed98cdd3c8ee0bba783927bc95b895c60160Denys Vlasenko				   oldargs) < 0)
2748a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards			{
2758a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards				fprintf(stderr, "umoven() failed\n");
2768a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards				return 0;
2778a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards			}
2788a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards			args = oldargs;
27964778cbfa62d9584c59d3a48b69ba1ebf8bd24e9Denys Vlasenko		}
2808a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards
28164778cbfa62d9584c59d3a48b69ba1ebf8bd24e9Denys Vlasenko		/* Kernel truncates arg[0] to int, we do the same. */
28264778cbfa62d9584c59d3a48b69ba1ebf8bd24e9Denys Vlasenko		nfds = (int) args[0];
28364778cbfa62d9584c59d3a48b69ba1ebf8bd24e9Denys Vlasenko		/* Kernel rejects negative nfds, so we don't parse it either. */
28464778cbfa62d9584c59d3a48b69ba1ebf8bd24e9Denys Vlasenko		if (nfds <= 0)
28564778cbfa62d9584c59d3a48b69ba1ebf8bd24e9Denys Vlasenko			return 0;
28679a79ea851250a30005d6323b20a9e30d9f336c1Denys Vlasenko		/* Beware of select(2^31-1, NULL, NULL, NULL) and similar... */
28764778cbfa62d9584c59d3a48b69ba1ebf8bd24e9Denys Vlasenko		if (nfds > 1024*1024)
28879a79ea851250a30005d6323b20a9e30d9f336c1Denys Vlasenko			nfds = 1024*1024;
28964778cbfa62d9584c59d3a48b69ba1ebf8bd24e9Denys Vlasenko		fdsize = (((nfds + 7) / 8) + current_wordsize-1) & -current_wordsize;
2908a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards		fds = malloc(fdsize);
2911d46ba57a8ab16b353b531f2bbefe2ad7f354ca9Denys Vlasenko		if (!fds)
2921d46ba57a8ab16b353b531f2bbefe2ad7f354ca9Denys Vlasenko			die_out_of_memory();
2938a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards
2947b609d5ba0852e6c56ba311350ebd4412361777bDenys Vlasenko		for (i = 1; i <= 3; ++i) {
2958a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards			if (args[i] == 0)
2968a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards				continue;
2977e69ed98cdd3c8ee0bba783927bc95b895c60160Denys Vlasenko			if (umoven(tcp, args[i], fdsize, fds) < 0) {
2988a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards				fprintf(stderr, "umoven() failed\n");
2998a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards				continue;
3008a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards			}
30164778cbfa62d9584c59d3a48b69ba1ebf8bd24e9Denys Vlasenko			for (j = 0;; j++) {
30264778cbfa62d9584c59d3a48b69ba1ebf8bd24e9Denys Vlasenko				j = next_set_bit(fds, j, nfds);
30364778cbfa62d9584c59d3a48b69ba1ebf8bd24e9Denys Vlasenko				if (j < 0)
30464778cbfa62d9584c59d3a48b69ba1ebf8bd24e9Denys Vlasenko					break;
30564778cbfa62d9584c59d3a48b69ba1ebf8bd24e9Denys Vlasenko				if (fdmatch(tcp, j)) {
3068a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards					free(fds);
3078a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards					return 1;
3088a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards				}
30964778cbfa62d9584c59d3a48b69ba1ebf8bd24e9Denys Vlasenko			}
3108a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards		}
3118a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards		free(fds);
3128a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards		return 0;
3138a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards	}
3148a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards
3158a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards	if (s->sys_func == sys_poll ||
316b63256e69bf3f1a74aadb0e14556490bc8f4ef95Denys Vlasenko	    s->sys_func == sys_ppoll)
3178a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards	{
3188a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards		struct pollfd fds;
3198a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards		unsigned nfds;
3208a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards		unsigned long start, cur, end;
3218a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards
3228a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards		start = tcp->u_arg[0];
3238a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards		nfds = tcp->u_arg[1];
3248a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards
3258a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards		end = start + sizeof(fds) * nfds;
3268a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards
3278a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards		if (nfds == 0 || end < start)
3288a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards			return 0;
3298a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards
3308a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards		for (cur = start; cur < end; cur += sizeof(fds))
3317e69ed98cdd3c8ee0bba783927bc95b895c60160Denys Vlasenko			if ((umoven(tcp, cur, sizeof fds, &fds) == 0)
3328a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards			    && fdmatch(tcp, fds.fd))
3338a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards				return 1;
3348a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards
3358a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards		return 0;
3368a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards	}
3378a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards
3388a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards	if (s->sys_func == printargs ||
3398a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards	    s->sys_func == sys_pipe ||
3408a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards	    s->sys_func == sys_pipe2 ||
3418a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards	    s->sys_func == sys_eventfd2 ||
3428a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards	    s->sys_func == sys_eventfd ||
3438a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards	    s->sys_func == sys_inotify_init1 ||
3448a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards	    s->sys_func == sys_timerfd_create ||
3458a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards	    s->sys_func == sys_timerfd_settime ||
3468a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards	    s->sys_func == sys_timerfd_gettime ||
3478a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards	    s->sys_func == sys_epoll_create ||
348894c7e3858ede412e37a1752c716bed7f30a9605Philippe Ombredanne	    s->sys_func == sys_socket ||
349894c7e3858ede412e37a1752c716bed7f30a9605Philippe Ombredanne	    s->sys_func == sys_socketpair ||
35099db95dd039d8fd700e7759bf4969abb55b979b9Dmitry V. Levin	    s->sys_func == sys_fanotify_init)
3518a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards	{
3528a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards		/*
353894c7e3858ede412e37a1752c716bed7f30a9605Philippe Ombredanne		 * These have TRACE_FILE or TRACE_DESCRIPTOR or TRACE_NETWORK set,
354894c7e3858ede412e37a1752c716bed7f30a9605Philippe Ombredanne		 * but they don't have any file descriptor or path args to test.
3558a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards		 */
3568a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards		return 0;
3578a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards	}
3588a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards
3598a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards	/*
3608a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards	 * Our fallback position for calls that haven't already
3618a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards	 * been handled is to just check arg[0].
3628a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards	 */
3638a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards
3648a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards	if (s->sys_flags & TRACE_FILE)
3658a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards		return upathmatch(tcp, tcp->u_arg[0]);
3668a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards
367894c7e3858ede412e37a1752c716bed7f30a9605Philippe Ombredanne	if (s->sys_flags & (TRACE_DESC | TRACE_NETWORK))
3688a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards		return fdmatch(tcp, tcp->u_arg[0]);
3698a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards
3708a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards	return 0;
3718a08277d13d0378c2ff3ce851534c3bf7e31defaGrant Edwards}
372