Lines Matching refs:filemon

30 __FBSDID("$FreeBSD: stable/11/sys/dev/filemon/filemon.c 343885 2019-02-07 23:55:11Z bdrewery $");
55 #include "filemon.h"
72 .d_name = "filemon",
76 MALLOC_DEFINE(M_FILEMON, "filemon", "File access monitor");
79 * The filemon->lock protects several things currently:
82 * - Serializing the filemon's log output.
83 * - Preventing inheritance or removal of the filemon into proc.p_filemon.
85 struct filemon {
86 struct sx lock; /* Lock for this filemon. */
98 static void filemon_output(struct filemon *filemon, char *msg, size_t len);
100 static __inline struct filemon *
101 filemon_acquire(struct filemon *filemon)
104 if (filemon != NULL)
105 refcount_acquire(&filemon->refcnt);
106 return (filemon);
113 filemon_release(struct filemon *filemon)
116 if (refcount_release(&filemon->refcnt) == 0)
123 sx_assert(&filemon->lock, SA_UNLOCKED);
125 if (filemon->cred != NULL)
126 crfree(filemon->cred);
127 sx_destroy(&filemon->lock);
128 free(filemon, M_FILEMON);
132 * Acquire the proc's p_filemon reference and lock the filemon.
133 * The proc's p_filemon may not match this filemon on return.
135 static struct filemon *
138 struct filemon *filemon;
143 filemon = filemon_acquire(p->p_filemon);
146 if (filemon == NULL)
152 sx_xlock(&filemon->lock);
153 return (filemon);
156 /* Remove and release the filemon on the given process. */
160 struct filemon *filemon;
166 filemon = p->p_filemon;
168 --filemon->proccnt;
172 * cannot be called with filemon locked, which the caller expects
175 KASSERT(filemon->refcnt > 1, ("%s: proc %p dropping filemon %p "
176 "with last reference", __func__, p, filemon));
177 filemon_release(filemon);
180 /* Unlock and release the filemon. */
182 filemon_drop(struct filemon *filemon)
185 sx_xunlock(&filemon->lock);
186 filemon_release(filemon);
192 filemon_write_header(struct filemon *filemon)
199 len = snprintf(filemon->msgbufr, sizeof(filemon->msgbufr),
200 "# filemon version %d\n# Target pid %d\n# Start %ju.%06ju\nV %d\n",
203 if (len < sizeof(filemon->msgbufr))
204 filemon_output(filemon, filemon->msgbufr, len);
208 * Invalidate the passed filemon in all processes.
211 filemon_untrack_processes(struct filemon *filemon)
215 sx_assert(&filemon->lock, SA_XLOCKED);
218 if (filemon->proccnt == 0)
223 * filemon_event_process_exit() will lock on filemon->lock
230 * guaranteed to not change since we have its filemon
234 if (p->p_filemon == filemon)
244 KASSERT(filemon->refcnt > 0, ("%s: filemon %p should have "
245 "references still.", __func__, filemon));
246 KASSERT(filemon->proccnt == 0, ("%s: filemon %p should not have "
247 "attached procs still.", __func__, filemon));
254 filemon_close_log(struct filemon *filemon)
260 sx_assert(&filemon->lock, SA_XLOCKED);
261 if (filemon->fp == NULL)
266 len = snprintf(filemon->msgbufr,
267 sizeof(filemon->msgbufr),
271 if (len < sizeof(filemon->msgbufr))
272 filemon_output(filemon, filemon->msgbufr, len);
273 fp = filemon->fp;
274 filemon->fp = NULL;
276 sx_xunlock(&filemon->lock);
278 sx_xlock(&filemon->lock);
288 struct filemon *filemon = data;
290 if (filemon == NULL)
293 sx_xlock(&filemon->lock);
295 * Detach the filemon. It cannot be inherited after this.
297 filemon_untrack_processes(filemon);
298 filemon_close_log(filemon);
299 filemon_drop(filemon);
302 /* Attach the filemon to the process. */
304 filemon_attach_proc(struct filemon *filemon, struct proc *p)
306 struct filemon *filemon2;
308 sx_assert(&filemon->lock, SA_XLOCKED);
311 ("%s: filemon %p attaching to exiting process %p",
312 __func__, filemon, p));
314 ("%s: filemon %p attaching to execing process %p",
315 __func__, filemon, p));
317 if (p->p_filemon == filemon)
327 * Historic behavior of filemon has been to let a child initiate
334 sx_xunlock(&filemon->lock);
341 sx_xlock(&filemon->lock);
350 ("%s: proc %p didn't detach filemon %p", __func__, p,
352 p->p_filemon = filemon_acquire(filemon);
353 ++filemon->proccnt;
363 struct filemon *filemon;
367 if ((error = devfs_get_cdevpriv((void **) &filemon)) != 0)
370 sx_xlock(&filemon->lock);
375 if (filemon->fp != NULL) {
382 &filemon->fp);
385 filemon_write_header(filemon);
391 filemon_untrack_processes(filemon);
396 KASSERT(p->p_filemon != filemon,
397 ("%s: proc %p didn't untrack filemon %p",
398 __func__, p, filemon));
399 error = filemon_attach_proc(filemon, p);
409 sx_xunlock(&filemon->lock);
418 struct filemon *filemon;
420 filemon = malloc(sizeof(*filemon), M_FILEMON,
422 sx_init(&filemon->lock, "filemon");
423 refcount_init(&filemon->refcnt, 1);
424 filemon->cred = crhold(td->td_ucred);
426 error = devfs_set_cdevpriv(filemon, filemon_dtr);
428 filemon_release(filemon);
438 struct filemon *filemon;
441 if ((error = devfs_get_cdevpriv((void **) &filemon)) != 0)
444 sx_xlock(&filemon->lock);
445 filemon_close_log(filemon);
446 error = filemon->error;
447 sx_xunlock(&filemon->lock);
465 "filemon");
512 DEV_MODULE(filemon, filemon_modevent, NULL);
513 MODULE_VERSION(filemon, 1);