2 * Copyright (c) 2011, David E. O'Brien.
3 * Copyright (c) 2009-2011, Juniper Networks, Inc.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY JUNIPER NETWORKS AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL JUNIPER NETWORKS OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
33 #include <sys/systm.h>
35 #include <sys/condvar.h>
37 #include <sys/fcntl.h>
38 #include <sys/ioccom.h>
39 #include <sys/kernel.h>
40 #include <sys/malloc.h>
41 #include <sys/module.h>
42 #include <sys/mutex.h>
45 #include <sys/queue.h>
46 #include <sys/syscall.h>
47 #include <sys/sysent.h>
48 #include <sys/sysproto.h>
51 #if __FreeBSD_version >= 900041
52 #include <sys/capability.h>
57 #if defined(COMPAT_IA32) || defined(COMPAT_FREEBSD32) || defined(COMPAT_ARCH32)
58 #include <compat/freebsd32/freebsd32_syscall.h>
59 #include <compat/freebsd32/freebsd32_proto.h>
61 extern struct sysentvec ia32_freebsd_sysvec;
64 extern struct sysentvec elf32_freebsd_sysvec;
65 extern struct sysentvec elf64_freebsd_sysvec;
67 static d_close_t filemon_close;
68 static d_ioctl_t filemon_ioctl;
69 static d_open_t filemon_open;
70 static int filemon_unload(void);
71 static void filemon_load(void *);
73 static struct cdevsw filemon_cdevsw = {
74 .d_version = D_VERSION,
75 .d_close = filemon_close,
76 .d_ioctl = filemon_ioctl,
77 .d_open = filemon_open,
81 MALLOC_DECLARE(M_FILEMON);
82 MALLOC_DEFINE(M_FILEMON, "filemon", "File access monitor");
85 TAILQ_ENTRY(filemon) link; /* Link into the in-use list. */
86 struct mtx mtx; /* Lock mutex for this filemon. */
87 struct cv cv; /* Lock condition variable for this
89 struct file *fp; /* Output file pointer. */
90 struct thread *locker; /* Ptr to the thread locking this
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 int n_readers = 0;
101 static struct mtx access_mtx;
102 static struct cv access_cv;
103 static struct thread *access_owner = NULL;
104 static struct thread *access_requester = NULL;
106 static struct cdev *filemon_dev;
108 #include "filemon_lock.c"
109 #include "filemon_wrapper.c"
112 filemon_dtr(void *data)
114 struct filemon *filemon = data;
116 if (filemon != NULL) {
117 struct file *fp = filemon->fp;
119 /* Get exclusive write access. */
120 filemon_lock_write();
122 /* Remove from the in-use list. */
123 TAILQ_REMOVE(&filemons_inuse, filemon, link);
128 /* Add to the free list. */
129 TAILQ_INSERT_TAIL(&filemons_free, filemon, link);
131 /* Give up write access. */
132 filemon_unlock_write();
135 fdrop(fp, curthread);
139 #if __FreeBSD_version < 900041
140 #define FGET_WRITE(a1, a2, a3) fget_write((a1), (a2), (a3))
142 #define FGET_WRITE(a1, a2, a3) fget_write((a1), (a2), CAP_WRITE | CAP_SEEK, (a3))
146 filemon_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag __unused,
150 struct filemon *filemon;
152 devfs_get_cdevpriv((void **) &filemon);
155 /* Set the output file descriptor. */
157 if ((error = FGET_WRITE(td, *(int *)data, &filemon->fp)) == 0)
158 /* Write the file header. */
159 filemon_comment(filemon);
162 /* Set the monitored process ID. */
163 case FILEMON_SET_PID:
164 filemon->pid = *((pid_t *)data);
176 filemon_open(struct cdev *dev, int oflags __unused, int devtype __unused,
177 struct thread *td __unused)
179 struct filemon *filemon;
181 /* Get exclusive write access. */
182 filemon_lock_write();
184 if ((filemon = TAILQ_FIRST(&filemons_free)) != NULL)
185 TAILQ_REMOVE(&filemons_free, filemon, link);
187 /* Give up write access. */
188 filemon_unlock_write();
190 if (filemon == NULL) {
191 filemon = malloc(sizeof(struct filemon), M_FILEMON,
196 mtx_init(&filemon->mtx, "filemon", "filemon", MTX_DEF);
197 cv_init(&filemon->cv, "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 mtx_init(&access_mtx, "filemon", "filemon", MTX_DEF);
228 cv_init(&access_cv, "filemon");
230 /* Install the syscall wrappers. */
231 filemon_wrapper_install();
233 filemon_dev = make_dev(&filemon_cdevsw, 0, UID_ROOT, GID_WHEEL, 0666,
240 struct filemon *filemon;
243 /* Get exclusive write access. */
244 filemon_lock_write();
246 if (TAILQ_FIRST(&filemons_inuse) != NULL)
249 destroy_dev(filemon_dev);
251 /* Deinstall the syscall wrappers. */
252 filemon_wrapper_deinstall();
255 /* Give up write access. */
256 filemon_unlock_write();
259 /* free() filemon structs free list. */
260 filemon_lock_write();
261 while ((filemon = TAILQ_FIRST(&filemons_free)) != NULL) {
262 TAILQ_REMOVE(&filemons_free, filemon, link);
263 mtx_destroy(&filemon->mtx);
264 cv_destroy(&filemon->cv);
265 free(filemon, M_FILEMON);
267 filemon_unlock_write();
269 mtx_destroy(&access_mtx);
270 cv_destroy(&access_cv);
277 filemon_modevent(module_t mod __unused, int type, void *data)
287 error = filemon_unload();
302 DEV_MODULE(filemon, filemon_modevent, NULL);
303 MODULE_VERSION(filemon, 1);