Home | History | Annotate | Line # | Download | only in filemon
      1 /*	$NetBSD: filemon_dev.c,v 1.9 2022/03/04 23:17:16 sjg Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2020 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Taylor R. Campbell.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include "filemon.h"
     33 
     34 #include <sys/ioctl.h>
     35 
     36 #include <errno.h>
     37 #include <fcntl.h>
     38 #include <stdlib.h>
     39 #include <unistd.h>
     40 
     41 #ifdef HAVE_FILEMON_H
     42 # include <filemon.h>
     43 #endif
     44 
     45 #ifndef _PATH_FILEMON
     46 #define _PATH_FILEMON	"/dev/filemon"
     47 #endif
     48 
     49 #ifndef MAKE_ATTR_UNUSED
     50 #define MAKE_ATTR_UNUSED        __attribute__((__unused__))
     51 #endif
     52 
     53 struct filemon {
     54 	int	fd;
     55 };
     56 
     57 const char *
     58 filemon_path(void)
     59 {
     60 
     61 	return _PATH_FILEMON;
     62 }
     63 
     64 struct filemon *
     65 filemon_open(void)
     66 {
     67 	struct filemon *F;
     68 	unsigned i;
     69 	int error;
     70 
     71 	/* Allocate and zero a struct filemon object.  */
     72 	F = calloc(1, sizeof *F);
     73 	if (F == NULL)
     74 		return NULL;
     75 
     76 	/* Try opening /dev/filemon, up to six times (cargo cult!).  */
     77 	for (i = 0; (F->fd = open(_PATH_FILEMON, O_RDWR|O_CLOEXEC)) == -1; i++) {
     78 		if (i == 5) {
     79 			error = errno;
     80 			goto fail0;
     81 		}
     82 	}
     83 
     84 	/* Success!  */
     85 	return F;
     86 
     87 fail0:	free(F);
     88 	errno = error;
     89 	return NULL;
     90 }
     91 
     92 int
     93 filemon_setfd(struct filemon *F, int fd)
     94 {
     95 
     96 	/* Point the kernel at this file descriptor.  */
     97 	if (ioctl(F->fd, FILEMON_SET_FD, &fd) == -1)
     98 		return -1;
     99 
    100 	/* No need for it in userland any more; close it.  */
    101 	(void)close(fd);
    102 
    103 	/* Success!  */
    104 	return 0;
    105 }
    106 
    107 void
    108 filemon_setpid_parent(struct filemon *F MAKE_ATTR_UNUSED, pid_t pid MAKE_ATTR_UNUSED)
    109 {
    110 	/* Nothing to do!  */
    111 }
    112 
    113 int
    114 filemon_setpid_child(const struct filemon *F, pid_t pid)
    115 {
    116 
    117 	/* Just pass it on to the kernel.  */
    118 	return ioctl(F->fd, FILEMON_SET_PID, &pid);
    119 }
    120 
    121 int
    122 filemon_close(struct filemon *F)
    123 {
    124 	int error = 0;
    125 
    126 	/* Close the filemon device fd.  */
    127 	if (close(F->fd) == -1 && error == 0)
    128 		error = errno;
    129 
    130 	/* Free the filemon descriptor.  */
    131 	free(F);
    132 
    133 	/* Set errno and return -1 if anything went wrong.  */
    134 	if (error != 0) {
    135 		errno = error;
    136 		return -1;
    137 	}
    138 
    139 	/* Success!  */
    140 	return 0;
    141 }
    142 
    143 int
    144 filemon_readfd(const struct filemon *F MAKE_ATTR_UNUSED)
    145 {
    146 
    147 	return -1;
    148 }
    149 
    150 int
    151 filemon_process(struct filemon *F MAKE_ATTR_UNUSED)
    152 {
    153 
    154 	return 0;
    155 }
    156