]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - sys/dev/filemon/filemon.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / sys / dev / filemon / filemon.c
1 /*-
2  * Copyright (c) 2011, David E. O'Brien.
3  * Copyright (c) 2009-2011, Juniper Networks, Inc.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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.
14  *
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
25  * SUCH DAMAGE.
26  */
27
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30
31 #include "opt_compat.h"
32
33 #include <sys/param.h>
34 #include <sys/file.h>
35 #include <sys/systm.h>
36 #include <sys/buf.h>
37 #include <sys/condvar.h>
38 #include <sys/conf.h>
39 #include <sys/fcntl.h>
40 #include <sys/ioccom.h>
41 #include <sys/kernel.h>
42 #include <sys/malloc.h>
43 #include <sys/module.h>
44 #include <sys/mutex.h>
45 #include <sys/poll.h>
46 #include <sys/proc.h>
47 #include <sys/queue.h>
48 #include <sys/syscall.h>
49 #include <sys/sysent.h>
50 #include <sys/sysproto.h>
51 #include <sys/uio.h>
52
53 #if __FreeBSD_version >= 900041
54 #include <sys/capability.h>
55 #endif
56
57 #include "filemon.h"
58
59 #if defined(COMPAT_IA32) || defined(COMPAT_FREEBSD32) || defined(COMPAT_ARCH32)
60 #include <compat/freebsd32/freebsd32_syscall.h>
61 #include <compat/freebsd32/freebsd32_proto.h>
62
63 extern struct sysentvec ia32_freebsd_sysvec;
64 #endif
65
66 extern struct sysentvec elf32_freebsd_sysvec;
67 extern struct sysentvec elf64_freebsd_sysvec;
68
69 static d_close_t        filemon_close;
70 static d_ioctl_t        filemon_ioctl;
71 static d_open_t         filemon_open;
72 static int              filemon_unload(void);
73 static void             filemon_load(void *);
74
75 static struct cdevsw filemon_cdevsw = {
76         .d_version      = D_VERSION,
77         .d_close        = filemon_close,
78         .d_ioctl        = filemon_ioctl,
79         .d_open         = filemon_open,
80         .d_name         = "filemon",
81 };
82
83 MALLOC_DECLARE(M_FILEMON);
84 MALLOC_DEFINE(M_FILEMON, "filemon", "File access monitor");
85
86 struct filemon {
87         TAILQ_ENTRY(filemon) link;      /* Link into the in-use list. */
88         struct mtx      mtx;            /* Lock mutex for this filemon. */
89         struct cv       cv;             /* Lock condition variable for this
90                                            filemon. */
91         struct file     *fp;            /* Output file pointer. */
92         struct thread   *locker;        /* Ptr to the thread locking this
93                                            filemon. */
94         pid_t           pid;            /* The process ID being monitored. */
95         char            fname1[MAXPATHLEN]; /* Temporary filename buffer. */
96         char            fname2[MAXPATHLEN]; /* Temporary filename buffer. */
97         char            msgbufr[1024];  /* Output message buffer. */
98 };
99
100 static TAILQ_HEAD(, filemon) filemons_inuse = TAILQ_HEAD_INITIALIZER(filemons_inuse);
101 static TAILQ_HEAD(, filemon) filemons_free = TAILQ_HEAD_INITIALIZER(filemons_free);
102 static int n_readers = 0;
103 static struct mtx access_mtx;
104 static struct cv access_cv;
105 static struct thread *access_owner = NULL;
106 static struct thread *access_requester = NULL;
107
108 #if __FreeBSD_version < 701000
109 static struct clonedevs *filemon_clones;
110 static eventhandler_tag eh_tag;
111 #else
112 static struct cdev *filemon_dev;
113 #endif
114
115 #include "filemon_lock.c"
116 #include "filemon_wrapper.c"
117
118 #if __FreeBSD_version < 701000
119 static void
120 filemon_clone(void *arg, struct ucred *cred, char *name, int namelen,
121     struct cdev **dev)
122 {
123         int u = -1;
124         size_t len;
125
126         if (*dev != NULL)
127                 return;
128
129         len = strlen(name);
130
131         if (len != 7)
132                 return;
133
134         if (bcmp(name,"filemon", 7) != 0)
135                 return;
136
137         /* Clone the device to the new minor number. */
138         if (clone_create(&filemon_clones, &filemon_cdevsw, &u, dev, 0) != 0)
139                 /* Create the /dev/filemonNN entry. */
140                 *dev = make_dev_cred(&filemon_cdevsw, u, cred, UID_ROOT,
141                     GID_WHEEL, 0666, "filemon%d", u);
142         if (*dev != NULL) {
143                 dev_ref(*dev);
144                 (*dev)->si_flags |= SI_CHEAPCLONE;
145         }
146 }
147 #endif
148
149 static void
150 filemon_dtr(void *data)
151 {
152         struct filemon *filemon = data;
153
154         if (filemon != NULL) {
155                 struct file *fp = filemon->fp;
156
157                 /* Get exclusive write access. */
158                 filemon_lock_write();
159
160                 /* Remove from the in-use list. */
161                 TAILQ_REMOVE(&filemons_inuse, filemon, link);
162
163                 filemon->fp = NULL;
164                 filemon->pid = -1;
165
166                 /* Add to the free list. */
167                 TAILQ_INSERT_TAIL(&filemons_free, filemon, link);
168
169                 /* Give up write access. */
170                 filemon_unlock_write();
171
172                 if (fp != NULL)
173                         fdrop(fp, curthread);
174         }
175 }
176
177 static int
178 filemon_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag __unused,
179     struct thread *td)
180 {
181         int error = 0;
182         struct filemon *filemon;
183
184 #if __FreeBSD_version < 701000
185         filemon = dev->si_drv1;
186 #else
187         devfs_get_cdevpriv((void **) &filemon);
188 #endif
189
190         switch (cmd) {
191         /* Set the output file descriptor. */
192         case FILEMON_SET_FD:
193 #if __FreeBSD_version < 900041
194 #define FGET_WRITE(a1, a2, a3) fget_write((a1), (a2), (a3))
195 #else
196 #define FGET_WRITE(a1, a2, a3) fget_write((a1), (a2), CAP_WRITE | CAP_SEEK, (a3))
197 #endif
198                 if ((error = FGET_WRITE(td, *(int *)data, &filemon->fp)) == 0)
199                         /* Write the file header. */
200                         filemon_comment(filemon);
201                 break;
202
203         /* Set the monitored process ID. */
204         case FILEMON_SET_PID:
205                 filemon->pid = *((pid_t *) data);
206                 break;
207
208         default:
209                 error = EINVAL;
210                 break;
211         }
212
213         return (error);
214 }
215
216 static int
217 filemon_open(struct cdev *dev, int oflags __unused, int devtype __unused,
218     struct thread *td __unused)
219 {
220         struct filemon *filemon;
221
222         /* Get exclusive write access. */
223         filemon_lock_write();
224
225         if ((filemon = TAILQ_FIRST(&filemons_free)) != NULL)
226                 TAILQ_REMOVE(&filemons_free, filemon, link);
227
228         /* Give up write access. */
229         filemon_unlock_write();
230
231         if (filemon == NULL) {
232                 filemon = malloc(sizeof(struct filemon), M_FILEMON,
233                     M_WAITOK | M_ZERO);
234
235                 filemon->fp = NULL;
236
237                 mtx_init(&filemon->mtx, "filemon", "filemon", MTX_DEF);
238                 cv_init(&filemon->cv, "filemon");
239         }
240
241         filemon->pid = curproc->p_pid;
242
243 #if __FreeBSD_version < 701000
244         dev->si_drv1 = filemon;
245 #else
246         devfs_set_cdevpriv(filemon, filemon_dtr);
247 #endif
248
249         /* Get exclusive write access. */
250         filemon_lock_write();
251
252         /* Add to the in-use list. */
253         TAILQ_INSERT_TAIL(&filemons_inuse, filemon, link);
254
255         /* Give up write access. */
256         filemon_unlock_write();
257
258         return (0);
259 }
260
261 static int
262 filemon_close(struct cdev *dev __unused, int flag __unused, int fmt __unused,
263     struct thread *td __unused)
264 {
265 #if __FreeBSD_version < 701000
266         filemon_dtr(dev->si_drv1);
267
268         dev->si_drv1 = NULL;
269
270         /* Schedule this cloned device to be destroyed. */
271         destroy_dev_sched(dev);
272 #endif
273
274         return (0);
275 }
276
277 static void
278 filemon_load(void *dummy __unused)
279 {
280         mtx_init(&access_mtx, "filemon", "filemon", MTX_DEF);
281         cv_init(&access_cv, "filemon");
282
283         /* Install the syscall wrappers. */
284         filemon_wrapper_install();
285
286 #if __FreeBSD_version < 701000
287         /* Enable device cloning. */
288         clone_setup(&filemon_clones);
289
290         /* Setup device cloning events. */
291         eh_tag = EVENTHANDLER_REGISTER(dev_clone, filemon_clone, 0, 1000);
292 #else
293         filemon_dev = make_dev(&filemon_cdevsw, 0, UID_ROOT, GID_WHEEL, 0666,
294             "filemon");
295 #endif
296 }
297
298 static int
299 filemon_unload(void)
300 {
301         struct filemon *filemon;
302         int error = 0;
303
304         /* Get exclusive write access. */
305         filemon_lock_write();
306
307         if (TAILQ_FIRST(&filemons_inuse) != NULL)
308                 error = EBUSY;
309         else {
310 #if __FreeBSD_version >= 701000
311                 destroy_dev(filemon_dev);
312 #endif
313
314                 /* Deinstall the syscall wrappers. */
315                 filemon_wrapper_deinstall();
316         }
317
318         /* Give up write access. */
319         filemon_unlock_write();
320
321         if (error == 0) {
322 #if __FreeBSD_version < 701000
323                 /*
324                  * Check if there is still an event handler callback registered.
325                 */
326                 if (eh_tag != 0) {
327                         /* De-register the device cloning event handler. */
328                         EVENTHANDLER_DEREGISTER(dev_clone, eh_tag);
329                         eh_tag = 0;
330
331                         /* Stop device cloning. */
332                         clone_cleanup(&filemon_clones);
333                 }
334 #endif
335                 /* free() filemon structs free list. */
336                 filemon_lock_write();
337                 while ((filemon = TAILQ_FIRST(&filemons_free)) != NULL) {
338                         TAILQ_REMOVE(&filemons_free, filemon, link);
339                         mtx_destroy(&filemon->mtx);
340                         cv_destroy(&filemon->cv);
341                         free(filemon, M_FILEMON);
342                 }
343                 filemon_unlock_write();
344
345                 mtx_destroy(&access_mtx);
346                 cv_destroy(&access_cv);
347         }
348
349         return (error);
350 }
351
352 static int
353 filemon_modevent(module_t mod __unused, int type, void *data)
354 {
355         int error = 0;
356
357         switch (type) {
358         case MOD_LOAD:
359                 filemon_load(data);
360                 break;
361
362         case MOD_UNLOAD:
363                 error = filemon_unload();
364                 break;
365
366         case MOD_SHUTDOWN:
367                 break;
368
369         default:
370                 error = EOPNOTSUPP;
371                 break;
372
373         }
374
375         return (error);
376 }
377
378 DEV_MODULE(filemon, filemon_modevent, NULL);
379 MODULE_VERSION(filemon, 1);