Commit Graph

38 Commits

Author SHA1 Message Date
Bryan Drewery
6c37a3d469 Cleanup COMPAT_FREEBSD32 support.
This is a NOP.

The COMPAT_IA32 was renamed in r205014 to COMPAT_FREEBSD32 and
COMPAT_ARCH32 does not seem to have existed.  Also remove some
leftovers from the sysent rework in r301404.  Include
freebsd32_util.h for the freebsd32_sysent prototype.

X-MFC-With:	r301404
Reported by:	kib
MFC after:	3 days
Sponsored by:	EMC / Isilon Storage Division
2016-06-05 18:16:33 +00:00
Bryan Drewery
f738a972ec Fix build after r301404.
X-MFC-With:	r301404
MFC after:	1 week
2016-06-04 23:34:51 +00:00
Bryan Drewery
66afa4152f Support all architectures by just using sysent.
PowerPC64 has two different ABIs, neither of which is elf64_freebsd_sysvec.
Using sysent and freebsd32_sysent achieves the same effect.

X-MFC-With:	r301130
MFC after:	1 week
Sponsored by:	EMC / Isilon Storage Division
2016-06-04 17:39:42 +00:00
Bryan Drewery
b7622235d0 Don't truncate existing error when writing the log.
Suggested by:	markj
MFC after:	2 weeks
Sponsored by:	EMC / Isilon Storage Division
2016-05-27 23:58:00 +00:00
Bryan Drewery
7ae27ff963 Write to the log using the tracer's credentials.
MFC after:	2 weeks
Sponsored by:	EMC / Isilon Storage Division
2016-05-27 23:57:53 +00:00
Bryan Drewery
f14fbe72ec exec: Cease tracing if credentials will change with the new image.
This also prevents tracing to a P_INEXEC process since it could race
with other processes attaching to it in filemon_event_process_exec() due
to the filemon_get_proc() race of incrementing ref and then locking the
filemon.  With the no-P_INEXEC invariant in place the p_filemon may only
be the same or NULL when trying to drop it in
filemon_event_process_exec().

MFC after:	2 weeks
Sponsored by:	EMC / Isilon Storage Division
Differential Revision:	https://reviews.freebsd.org/D6545
2016-05-27 23:57:43 +00:00
Bryan Drewery
23ae5cd74e filemon exec: Use imgp->execpath rather than vn_fullpath(9).
This will be more accurate as the actual name is provided if ran
from an absolute path in do_execve().

MFC after:	2 weeks
Sponsored by:	EMC / Isilon Storage Division
2016-05-26 23:27:08 +00:00
Bryan Drewery
9b511ce96b Handle copyin failures.
Skip the log entry as there is nothing good to write out.  Don't fail
the syscall though since it already succeeded.  There's no reason
filemon's tracing failure should fail the already-succeeded syscall.

Record the error for later to return from close(2) on the filemon devfs
file descriptor.

Discussed with:	markj, sjg, kib (briefly with kib)
Reported by:	mjg
MFC after:	2 weeks
Sponsored by:	EMC / Isilon Storage Division
2016-03-22 22:41:14 +00:00
Bryan Drewery
eac5c9f26f Remove unused done argument to copyinstr(9).
MFC after:	2 weeks
Sponsored by:	EMC / Isilon Storage Division
2016-03-22 22:41:10 +00:00
Bryan Drewery
4177d9f7d3 Return any log write failure encountered when closing the filemon fd.
Discussed with:	sjg, markj
Reviewed by:	sjg
MFC after:	2 weeks
Sponsored by:	EMC / Isilon Storage Division
2016-03-22 22:41:07 +00:00
Bryan Drewery
d77f7e8c9d Consolidate common link(2) logic.
MFC after:	2 weeks
Sponsored by:	EMC / Isilon Storage Division
2016-03-21 23:22:19 +00:00
Bryan Drewery
8536d1b7e1 Attempt to use the namecache for openat(2) path resolution.
This finishes the work done in D2810.

MFC after:	2 weeks
Sponsored by:	EMC / Isilon Storage Division
2016-03-21 20:29:53 +00:00
Bryan Drewery
4f0d785142 Use curthread for vn_fullpath.
No functional change.

MFC after:	2 weeks
Sponsored by:	EMC / Isilon Storage Division
2016-03-21 20:29:46 +00:00
Bryan Drewery
4d3cfa1d9c Consolidate open(2) and openat(2) code.
MFC after:	2 weeks
Sponsored by:	EMC / Isilon Storage Division
2016-03-21 20:29:43 +00:00
Bryan Drewery
6d2077c2be Stop tracking stat(2).
None of lstat(2), fstat(2), fstatat(2) were tracked either.

The other filemon implementations also do not track stat(2), nor
does bmake utilize it.  The act of opening a file for read should
be enough to decide that a file is a dependency.  There could be
rare cases where just having a file would cause a dependency but it
is unlikely.

MFC after:	2 weeks
Also noted by:	sjg
Sponsored by:	EMC / Isilon Storage Division
2016-03-21 20:29:39 +00:00
Bryan Drewery
e0d84b9ee9 Track filemon usage via a proc.p_filemon pointer rather than its own lists.
- proc.p_filemon is added which is protected by PROC_LOCK.  This improves
  performance and avoids double-fork issues, taking allproc_lock
  while in syscalls, and walking the process tree in syscalls.  A
  particular proc.p_filemon can only be changed to NULL or another
  filemon, or the filemon inherited, while the filemon->lock is held.
- Filemon are reference counted.  On the last reference the log will be closed.
- When closing the devfs file handle, the filemon will be detached from all
  processes and inheritance prevented.
- Disallow attaching to a process already being traced since filemon is
  typically intended to be used on children only.  This is allowed for
  curproc as bmake relies on this behavior for rare cases when combining
  .MAKE with .META.
- Detach any previously tracked process on ioctl(FILEMON_SET_PID).
- Handle error from devfs_set_cdevpriv() in filemon_open().
- The global filemon lock and lists are removed.
- A free list is no longer kept.  Previously this list was
  forever-expanding and never garbage cleaned.
- No longer loses track of double-forks.  If the process holding the filemon
  handle closes it will close the log rather than wait on a daemonized process,
  but it will log all activity until it closes its handle.  The filemon
  will be removed from the process and not inherited.
- A separate process count is kept only as an optimization for
  forced detachment to avoid taking allproc_lock and walking the entire
  process tree.
- struct filemon access is protected by sx(9) filemon->lock as it was before.
- Add more comments and KASSERTS.

MFC after:	2 weeks
Reviewed by:	kib, mjg, markj (all on previous versions)
Sponsored by:	EMC / Isilon Storage Division
Differential Revision:	https://reviews.freebsd.org/D5520
2016-03-21 20:29:27 +00:00
Bryan Drewery
8a81693ad5 Only call bwillwrite() for logging to vnodes, as other fo_write() calls do.
MFC after:	1 week
Sponsored by:	EMC / Isilon Storage Division
2016-03-07 21:10:19 +00:00
Bryan Drewery
e0dae8f1c0 Remove filemon->lock wrappers.
MFC after:	1 week
Sponsored by:	EMC / Isilon Storage Division
2016-03-02 00:13:13 +00:00
Bryan Drewery
5b83ad4bb4 Remove old compatibility checks.
MFC after:	3 days
Sponsored by:	EMC / Isilon Storage Division
2016-02-27 21:08:27 +00:00
Bryan Drewery
a2109e0056 Filemon: Fix panic when fork1() is called from kproc_create().
MFC after:	2 weeks
Sponsored by:	EMC / Isilon Storage Division
2016-02-16 02:14:25 +00:00
Bryan Drewery
d5e53f99be filemon: Track the process pointer rather than a pid.
The process is not held since the process_exit hook is called after the
exithold.  There is no need to hold the process since the hook will
always see it exiting via the process_exit event.

MFC after:	2 weeks
Sponsored by:	EMC / Isilon Storage Division
2016-01-29 00:44:32 +00:00
Bryan Drewery
f889a61ae5 filemon: Use process_exec EVENTHANDLER to capture sys_execve.
MFC after:	2 weeks
Sponsored by:	EMC / Isilon Storage Division
2016-01-28 21:45:25 +00:00
Bryan Drewery
e95c55e50d Follow-up r294967: Mark flags unused.
X-MFC-With:	r294967
MFC after:	2 weeks
Sponsored by:	EMC / Isilon Storage Division
2016-01-28 01:19:19 +00:00
Bryan Drewery
2f6009620f filemon: Trace fork via process_fork event.
This avoids needing ugly hooks and needing both a vfork and fork
handler.

MFC after:	2 weeks
Sponsored by:	EMC / Isilon Storage Division
2016-01-28 01:17:55 +00:00
Bryan Drewery
79d699139b filemon: Use process_exit EVENTHANDLER to capture process exit.
This fixes some cases where a process could exit without being untracked
by filemon.

Reported by:	mjg
MFC after:	2 weeks
Sponsored by:	EMC / Isilon Storage Division
2016-01-28 00:51:17 +00:00
Bryan Drewery
2b198fe92f filemon_comment has nothing to do with wrappers so move it out of filemon_wrapper.c.
It only prints the header from filemon_ioctl.  Keep the name though to stay
closer to other implementations.

MFC after:	2 weeks
Sponsored by:	EMC / Isilon Storage Division
2016-01-27 21:17:43 +00:00
Bryan Drewery
64c368a488 filemon_ioctl: Lock the associated filemon handle before writing to it.
Reported by:	mjg
MFC after:	2 weeks
Sponsored by:	EMC / Isilon Storage Division
2016-01-27 21:14:09 +00:00
Bryan Drewery
8b64fa1e12 Avoid taking proctree_lock and searching parents in wrappers if not needed.
This should help the case where filemon is loaded but not in use.

Sponsored by:	EMC / Isilon Storage Division
MFC after:	2 weeks
2015-08-26 03:37:18 +00:00
Bryan Drewery
9fa45e4864 Remove unneeded inuse list locking in filemon_comment().
Sponsored by:	EMC / Isilon Storage Division
MFC after:	2 weeks
2015-08-26 03:33:34 +00:00
Bryan Drewery
1ab9f216d2 Move common locking for filemon_inuse and struct filemon* to filemon_pid_check().
This keeps the lock for the filemon_inuse list held only while reading
the list.

Sponsored by:	EMC / Isilon Storage Division
MFC after:	2 weeks
2015-08-26 03:32:47 +00:00
Simon J. Gerraty
3df2b1571b sx_sunlock for sx_slock 2015-06-19 17:34:59 +00:00
Simon J. Gerraty
c849fda850 filemon_pid_check needs to hold proctree_lock
Reviewed by:	kib
MFC after:	few days
2015-06-19 17:19:20 +00:00
Simon J. Gerraty
f859e95660 Latest clang uses openat(2).
If the pathname is absolute or dirfd is AT_FDCWD we can
handle it exactly like open(2).
Otherwise we output an A record to indicate that the path of
an open directory needs to be used (earlier in the trace).

Differential Revision:	D2810
Reviewed by: jhb
MFC after: a bit
2015-06-14 16:31:06 +00:00
Hiroki Sato
89cac24e48 - Use pget(PGET_CANDEBUG | PGET_NOTWEXIT) to determine if the specified
PID is valid for monitoring in FILEMON_SET_PID ioctl.

- Set the monitored PID to -1 when the process exits.

Suggested by:	jilles
Tested by:	sjg
MFC after:	3 days
2013-08-06 02:14:30 +00:00
David E. O'Brien
f9d4b3926a Match the options of the kernel. 2013-06-04 06:38:01 +00:00
David E. O'Brien
93665dfffb Iterate rather than use recursion. We can blow out the kernel stack if there
is a long chain of fork(2)s.
2012-10-26 15:44:29 +00:00
Marcel Moolenaar
097f09bb98 There's no need to make filemon specific to i386 and amd64. All
LP64 architectures define elf64_freebsd_sysvec and all ILP32
architectures define elf32_freebsd_sysvec.
2012-07-02 20:36:26 +00:00
David E. O'Brien
eb9aea5ac0 Add the 'filemon' device. 'filemon' is a kernel module that provides a device
interface for processes to record system calls of its children.

Submitted by:	Juniper Networks.
2012-06-04 22:54:19 +00:00