Lines Matching +full:1 +full:f

13  * 1. Redistributions of source code must retain the above copyright
58 #define AT_CWD -1
141 return -1; in compare_filemon_states()
143 return +1; in compare_filemon_states()
145 return -1; in compare_filemon_states()
147 return +1; in compare_filemon_states()
159 return -1; in compare_filemon_key()
161 return +1; in compare_filemon_key()
163 return -1; in compare_filemon_key()
165 return +1; in compare_filemon_key()
198 struct filemon *F; in filemon_open() local
203 F = calloc(1, sizeof *F); in filemon_open()
204 if (F == NULL) in filemon_open()
208 if (pipe2(ktrpipe, O_CLOEXEC|O_NONBLOCK) == -1) { in filemon_open()
214 if ((F->in = fdopen(ktrpipe[0], "r")) == NULL) { in filemon_open()
218 ktrpipe[0] = -1; /* claimed by fdopen */ in filemon_open()
224 F->ktrfd = ktrpipe[1]; in filemon_open()
225 rb_tree_init(&F->active, &filemon_rb_ops); in filemon_open()
228 return F; in filemon_open()
231 (void)close(ktrpipe[1]); in filemon_open()
232 fail0: free(F); in filemon_open()
238 * filemon_closefd(F)
241 * If F is not open for output, do nothing. Never leaves F open
243 * and return -1 on failure.
246 filemon_closefd(struct filemon *F) in filemon_closefd() argument
251 if (F->out == NULL) in filemon_closefd()
258 if (fflush(F->out) == EOF && error == 0) in filemon_closefd()
260 if (fclose(F->out) == EOF && error == 0) in filemon_closefd()
262 F->out = NULL; in filemon_closefd()
264 /* Set errno and return -1 if anything went wrong. */ in filemon_closefd()
267 return -1; in filemon_closefd()
275 * filemon_setfd(F, fd)
277 * Cause filemon activity on F to be sent to fd. Claims ownership
282 filemon_setfd(struct filemon *F, int fd) in filemon_setfd() argument
289 if ((filemon_closefd(F)) == -1) in filemon_setfd()
290 return -1; in filemon_setfd()
291 assert(F->out == NULL); in filemon_setfd()
294 if ((F->out = fdopen(fd, "a")) == NULL) in filemon_setfd()
295 return -1; in filemon_setfd()
302 fprintf(F->out, "# filemon version 4\n"); in filemon_setfd()
303 fprintf(F->out, "# Target pid %jd\n", (intmax_t)getpid()); in filemon_setfd()
304 fprintf(F->out, "V 4\n"); in filemon_setfd()
311 * filemon_setpid_parent(F, pid)
316 filemon_setpid_parent(struct filemon *F, pid_t pid) in filemon_setpid_parent() argument
319 F->child = pid; in filemon_setpid_parent()
323 * filemon_setpid_child(F, pid)
326 * errno and returns -1 on failure.
329 filemon_setpid_child(const struct filemon *F, pid_t pid) in filemon_setpid_child() argument
337 if (fktrace(F->ktrfd, ops, trpoints, pid) == -1) in filemon_setpid_child()
338 return -1; in filemon_setpid_child()
344 * filemon_close(F)
346 * Close F for output if necessary, and free a filemon descriptor.
347 * Returns 0 on success; sets errno and returns -1 on failure, but
351 filemon_close(struct filemon *F) in filemon_close() argument
357 if (filemon_closefd(F) == -1 && error == 0) in filemon_close()
361 if (fclose(F->in) == EOF && error == 0) in filemon_close()
363 if (close(F->ktrfd) == -1 && error == 0) in filemon_close()
367 while ((S = RB_TREE_MIN(&F->active)) != NULL) { in filemon_close()
368 rb_tree_remove_node(&F->active, S); in filemon_close()
373 free(F); in filemon_close()
375 /* Set errno and return -1 if anything went wrong. */ in filemon_close()
378 return -1; in filemon_close()
386 * filemon_readfd(F)
390 * filemon_process, or -1 if anything has gone wrong.
393 filemon_readfd(const struct filemon *F) in filemon_readfd() argument
396 if (F->state == FILEMON_ERROR) in filemon_readfd()
397 return -1; in filemon_readfd()
398 return fileno(F->in); in filemon_readfd()
402 * filemon_dispatch(F)
408 filemon_dispatch(struct filemon *F) in filemon_dispatch() argument
411 .pid = F->hdr.ktr_pid, in filemon_dispatch()
412 .lid = F->hdr.ktr_lid, in filemon_dispatch()
416 switch (F->hdr.ktr_type) { in filemon_dispatch()
418 struct ktr_syscall *call = &F->payload.syscall; in filemon_dispatch()
431 S = (*filemon_syscalls[call->ktr_code])(F, &key, call); in filemon_dispatch()
444 S1 = rb_tree_insert_node(&F->active, S); in filemon_dispatch()
454 S = rb_tree_find_node(&F->active, &key); in filemon_dispatch()
461 S->path[S->i++] = strndup(F->payload.namei, in filemon_dispatch()
462 sizeof F->payload.namei); in filemon_dispatch()
465 struct ktr_sysret *ret = &F->payload.sysret; in filemon_dispatch()
469 S = rb_tree_find_node(&F->active, &key); in filemon_dispatch()
472 rb_tree_remove_node(&F->active, S); in filemon_dispatch()
481 S->show(F, S, ret); in filemon_dispatch()
496 * filemon_process(F)
498 * Process all pending events after filemon_readfd(F) has
501 * Returns -1 on failure, 0 on end of events, and anything else if
505 * If we stop while there's events buffered in F->in, then select
507 * in the buffer of F->in, but if we don't stop then ktrace events
511 filemon_process(struct filemon *F) in filemon_process() argument
517 if (F->child == 0) in filemon_process()
521 if (F->resid > 0) { in filemon_process()
522 nread = fread(F->p, 1, F->resid, F->in); in filemon_process()
524 if (feof(F->in) != 0) in filemon_process()
526 assert(ferror(F->in) != 0); in filemon_process()
532 return 1; in filemon_process()
533 F->state = FILEMON_ERROR; in filemon_process()
534 F->p = NULL; in filemon_process()
535 F->resid = 0; in filemon_process()
536 return -1; in filemon_process()
538 assert(nread <= F->resid); in filemon_process()
539 F->p += nread; in filemon_process()
540 F->resid -= nread; in filemon_process()
541 if (F->resid > 0) /* may be more events */ in filemon_process()
542 return 1; in filemon_process()
546 switch (F->state) { in filemon_process()
548 F->state = FILEMON_HEADER; in filemon_process()
549 F->p = (void *)&F->hdr; in filemon_process()
550 F->resid = sizeof F->hdr; in filemon_process()
554 if (F->hdr.ktr_len < 0 || in filemon_process()
555 (size_t)F->hdr.ktr_len > sizeof F->payload) { in filemon_process()
556 F->state = FILEMON_ERROR; in filemon_process()
557 F->p = NULL; in filemon_process()
558 F->resid = 0; in filemon_process()
560 return -1; in filemon_process()
562 F->state = FILEMON_PAYLOAD; in filemon_process()
563 F->p = (void *)&F->payload; in filemon_process()
564 F->resid = (size_t)F->hdr.ktr_len; in filemon_process()
568 filemon_dispatch(F); in filemon_process()
569 F->state = FILEMON_HEADER; in filemon_process()
570 F->p = (void *)&F->hdr; in filemon_process()
571 F->resid = sizeof F->hdr; in filemon_process()
574 F->state = FILEMON_ERROR; in filemon_process()
577 F->p = NULL; in filemon_process()
578 F->resid = 0; in filemon_process()
580 return -1; in filemon_process()
594 S = calloc(1, offsetof(struct filemon_state, path[npath])); in syscall_enter()
609 show_paths(struct filemon *F, const struct filemon_state *S, in show_paths() argument
623 if (F->out == NULL) in show_paths()
630 fprintf(F->out, "%s %jd", prefix, (intmax_t)S->key.pid); in show_paths()
632 const char *q = S->npath > 1 ? "'" : ""; in show_paths()
633 fprintf(F->out, " %s%s%s", q, S->path[i], q); in show_paths()
635 fprintf(F->out, "\n"); in show_paths()
639 show_retval(struct filemon *F, const struct filemon_state *S, in show_retval() argument
649 if (F->out == NULL) in show_retval()
652 fprintf(F->out, "%s %jd %jd\n", prefix, (intmax_t)S->key.pid, in show_retval()
657 show_chdir(struct filemon *F, const struct filemon_state *S, in show_chdir() argument
660 show_paths(F, S, ret, "C"); in show_chdir()
664 show_execve(struct filemon *F, const struct filemon_state *S, in show_execve() argument
667 show_paths(F, S, ret, "E"); in show_execve()
671 show_fork(struct filemon *F, const struct filemon_state *S, in show_fork() argument
674 show_retval(F, S, ret, "F"); in show_fork()
678 show_link(struct filemon *F, const struct filemon_state *S, in show_link() argument
681 show_paths(F, S, ret, "L"); /* XXX same as symlink */ in show_link()
685 show_open_read(struct filemon *F, const struct filemon_state *S, in show_open_read() argument
688 show_paths(F, S, ret, "R"); in show_open_read()
692 show_open_write(struct filemon *F, const struct filemon_state *S, in show_open_write() argument
695 show_paths(F, S, ret, "W"); in show_open_write()
699 show_open_readwrite(struct filemon *F, const struct filemon_state *S, in show_open_readwrite() argument
702 show_paths(F, S, ret, "R"); in show_open_readwrite()
703 show_paths(F, S, ret, "W"); in show_open_readwrite()
707 show_openat_read(struct filemon *F, const struct filemon_state *S, in show_openat_read() argument
711 show_paths(F, S, ret, "A"); in show_openat_read()
712 show_paths(F, S, ret, "R"); in show_openat_read()
716 show_openat_write(struct filemon *F, const struct filemon_state *S, in show_openat_write() argument
720 show_paths(F, S, ret, "A"); in show_openat_write()
721 show_paths(F, S, ret, "W"); in show_openat_write()
725 show_openat_readwrite(struct filemon *F, const struct filemon_state *S, in show_openat_readwrite() argument
729 show_paths(F, S, ret, "A"); in show_openat_readwrite()
730 show_paths(F, S, ret, "R"); in show_openat_readwrite()
731 show_paths(F, S, ret, "W"); in show_openat_readwrite()
735 show_symlink(struct filemon *F, const struct filemon_state *S, in show_symlink() argument
738 show_paths(F, S, ret, "L"); /* XXX same as link */ in show_symlink()
742 show_unlink(struct filemon *F, const struct filemon_state *S, in show_unlink() argument
745 show_paths(F, S, ret, "D"); in show_unlink()
749 show_rename(struct filemon *F, const struct filemon_state *S, in show_rename() argument
752 show_paths(F, S, ret, "M"); in show_rename()
757 filemon_sys_chdir(struct filemon *F, const struct filemon_key *key, in filemon_sys_chdir() argument
760 return syscall_enter(key, call, 1, &show_chdir); in filemon_sys_chdir()
767 filemon_sys_execve(struct filemon *F, const struct filemon_key *key, in filemon_sys_execve() argument
770 return syscall_enter(key, call, 1, &show_execve); in filemon_sys_execve()
774 filemon_sys_exit(struct filemon *F, const struct filemon_key *key, in filemon_sys_exit() argument
777 const register_t *args = (const void *)&call[1]; in filemon_sys_exit()
780 if (F->out != NULL) { in filemon_sys_exit()
781 fprintf(F->out, "X %jd %d\n", (intmax_t)key->pid, status); in filemon_sys_exit()
782 if (key->pid == F->child) { in filemon_sys_exit()
783 fprintf(F->out, "# Bye bye\n"); in filemon_sys_exit()
784 F->child = 0; in filemon_sys_exit()
792 filemon_sys_fork(struct filemon *F, const struct filemon_key *key, in filemon_sys_fork() argument
800 filemon_sys_link(struct filemon *F, const struct filemon_key *key, in filemon_sys_link() argument
808 filemon_sys_open(struct filemon *F, const struct filemon_key *key, in filemon_sys_open() argument
811 const register_t *args = (const void *)&call[1]; in filemon_sys_open()
816 flags = (int)args[1]; in filemon_sys_open()
819 return syscall_enter(key, call, 1, &show_open_readwrite); in filemon_sys_open()
821 return syscall_enter(key, call, 1, &show_open_write); in filemon_sys_open()
823 return syscall_enter(key, call, 1, &show_open_read); in filemon_sys_open()
830 filemon_sys_openat(struct filemon *F, const struct filemon_key *key, in filemon_sys_openat() argument
833 const register_t *args = (const void *)&call[1]; in filemon_sys_openat()
848 return syscall_enter(key, call, 1, in filemon_sys_openat()
851 return syscall_enter(key, call, 1, &show_open_write); in filemon_sys_openat()
853 return syscall_enter(key, call, 1, &show_open_read); in filemon_sys_openat()
858 return syscall_enter(key, call, 1, in filemon_sys_openat()
861 return syscall_enter(key, call, 1, &show_openat_write); in filemon_sys_openat()
863 return syscall_enter(key, call, 1, &show_openat_read); in filemon_sys_openat()
873 filemon_sys_symlink(struct filemon *F, const struct filemon_key *key, in filemon_sys_symlink() argument
881 filemon_sys_unlink(struct filemon *F, const struct filemon_key *key, in filemon_sys_unlink() argument
884 return syscall_enter(key, call, 1, &show_unlink); in filemon_sys_unlink()
889 filemon_sys_rename(struct filemon *F, const struct filemon_key *key, in filemon_sys_rename() argument