2 * Copyright (c) 2011, David E. O'Brien.
3 * Copyright (c) 2009-2011, Juniper Networks, Inc.
4 * Copyright (c) 2015, EMC Corp.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY JUNIPER NETWORKS AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL JUNIPER NETWORKS OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include "opt_compat.h"
34 #include <sys/param.h>
36 #include <sys/systm.h>
38 #include <sys/condvar.h>
40 #include <sys/fcntl.h>
41 #include <sys/ioccom.h>
42 #include <sys/kernel.h>
44 #include <sys/malloc.h>
45 #include <sys/module.h>
48 #include <sys/queue.h>
50 #include <sys/syscall.h>
51 #include <sys/sysent.h>
52 #include <sys/sysproto.h>
55 #if __FreeBSD_version >= 900041
56 #include <sys/capsicum.h>
61 #if defined(COMPAT_IA32) || defined(COMPAT_FREEBSD32) || defined(COMPAT_ARCH32)
62 #include <compat/freebsd32/freebsd32_syscall.h>
63 #include <compat/freebsd32/freebsd32_proto.h>
65 extern struct sysentvec ia32_freebsd_sysvec;
68 extern struct sysentvec elf32_freebsd_sysvec;
69 extern struct sysentvec elf64_freebsd_sysvec;
71 static d_close_t filemon_close;
72 static d_ioctl_t filemon_ioctl;
73 static d_open_t filemon_open;
74 static int filemon_unload(void);
75 static void filemon_load(void *);
77 static struct cdevsw filemon_cdevsw = {
78 .d_version = D_VERSION,
79 .d_close = filemon_close,
80 .d_ioctl = filemon_ioctl,
81 .d_open = filemon_open,
85 MALLOC_DECLARE(M_FILEMON);
86 MALLOC_DEFINE(M_FILEMON, "filemon", "File access monitor");
89 TAILQ_ENTRY(filemon) link; /* Link into the in-use list. */
90 struct sx lock; /* Lock mutex for this filemon. */
91 struct file *fp; /* Output file pointer. */
92 pid_t pid; /* The process ID being monitored. */
93 char fname1[MAXPATHLEN]; /* Temporary filename buffer. */
94 char fname2[MAXPATHLEN]; /* Temporary filename buffer. */
95 char msgbufr[1024]; /* Output message buffer. */
98 static TAILQ_HEAD(, filemon) filemons_inuse = TAILQ_HEAD_INITIALIZER(filemons_inuse);
99 static TAILQ_HEAD(, filemon) filemons_free = TAILQ_HEAD_INITIALIZER(filemons_free);
100 static struct sx access_lock;
102 static struct cdev *filemon_dev;
104 #include "filemon_lock.c"
105 #include "filemon_wrapper.c"
108 filemon_dtr(void *data)
110 struct filemon *filemon = data;
112 if (filemon != NULL) {
113 struct file *fp = filemon->fp;
115 /* Get exclusive write access. */
116 filemon_lock_write();
118 /* Remove from the in-use list. */
119 TAILQ_REMOVE(&filemons_inuse, filemon, link);
124 /* Add to the free list. */
125 TAILQ_INSERT_TAIL(&filemons_free, filemon, link);
127 /* Give up write access. */
128 filemon_unlock_write();
131 fdrop(fp, curthread);
136 filemon_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag __unused,
140 struct filemon *filemon;
142 #if __FreeBSD_version >= 900041
146 devfs_get_cdevpriv((void **) &filemon);
149 /* Set the output file descriptor. */
151 error = fget_write(td, *(int *)data,
152 #if __FreeBSD_version >= 900041
153 cap_rights_init(&rights, CAP_PWRITE),
157 /* Write the file header. */
158 filemon_comment(filemon);
161 /* Set the monitored process ID. */
162 case FILEMON_SET_PID:
163 error = pget(*((pid_t *)data), PGET_CANDEBUG | PGET_NOTWEXIT,
166 filemon->pid = p->p_pid;
180 filemon_open(struct cdev *dev, int oflags __unused, int devtype __unused,
181 struct thread *td __unused)
183 struct filemon *filemon;
185 /* Get exclusive write access. */
186 filemon_lock_write();
188 if ((filemon = TAILQ_FIRST(&filemons_free)) != NULL)
189 TAILQ_REMOVE(&filemons_free, filemon, link);
191 /* Give up write access. */
192 filemon_unlock_write();
194 if (filemon == NULL) {
195 filemon = malloc(sizeof(struct filemon), M_FILEMON,
197 sx_init(&filemon->lock, "filemon");
200 filemon->pid = curproc->p_pid;
202 devfs_set_cdevpriv(filemon, filemon_dtr);
204 /* Get exclusive write access. */
205 filemon_lock_write();
207 /* Add to the in-use list. */
208 TAILQ_INSERT_TAIL(&filemons_inuse, filemon, link);
210 /* Give up write access. */
211 filemon_unlock_write();
217 filemon_close(struct cdev *dev __unused, int flag __unused, int fmt __unused,
218 struct thread *td __unused)
225 filemon_load(void *dummy __unused)
227 sx_init(&access_lock, "filemons_inuse");
229 /* Install the syscall wrappers. */
230 filemon_wrapper_install();
232 filemon_dev = make_dev(&filemon_cdevsw, 0, UID_ROOT, GID_WHEEL, 0666,
239 struct filemon *filemon;
242 /* Get exclusive write access. */
243 filemon_lock_write();
245 if (TAILQ_FIRST(&filemons_inuse) != NULL)
248 destroy_dev(filemon_dev);
250 /* Deinstall the syscall wrappers. */
251 filemon_wrapper_deinstall();
254 /* Give up write access. */
255 filemon_unlock_write();
258 /* free() filemon structs free list. */
259 filemon_lock_write();
260 while ((filemon = TAILQ_FIRST(&filemons_free)) != NULL) {
261 TAILQ_REMOVE(&filemons_free, filemon, link);
262 sx_destroy(&filemon->lock);
263 free(filemon, M_FILEMON);
265 filemon_unlock_write();
267 sx_destroy(&access_lock);
274 filemon_modevent(module_t mod __unused, int type, void *data)
284 error = filemon_unload();
299 DEV_MODULE(filemon, filemon_modevent, NULL);
300 MODULE_VERSION(filemon, 1);