1 /* $NetBSD: filemon_dev.c,v 1.2 2020/07/10 00:42:53 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 struct filemon { 50 int fd; 51 }; 52 53 const char * 54 filemon_path(void) 55 { 56 57 return _PATH_FILEMON; 58 } 59 60 struct filemon * 61 filemon_open(void) 62 { 63 struct filemon *F; 64 unsigned i; 65 int error; 66 67 /* Allocate and zero a struct filemon object. */ 68 F = calloc(1, sizeof(*F)); 69 if (F == NULL) 70 return NULL; 71 72 /* Try opening /dev/filemon, up to six times (cargo cult!). */ 73 for (i = 0; (F->fd = open(_PATH_FILEMON, O_RDWR)) == -1; i++) { 74 if (i == 5) { 75 error = errno; 76 goto fail0; 77 } 78 } 79 (void)fcntl(F->fd, F_SETFD, FD_CLOEXEC); 80 81 /* Success! */ 82 return F; 83 84 fail0: free(F); 85 errno = error; 86 return NULL; 87 } 88 89 int 90 filemon_setfd(struct filemon *F, int fd) 91 { 92 93 /* Point the kernel at this file descriptor. */ 94 if (ioctl(F->fd, FILEMON_SET_FD, &fd) == -1) 95 return -1; 96 97 /* No need for it in userland any more; close it. */ 98 (void)close(fd); 99 100 /* Success! */ 101 return 0; 102 } 103 104 void 105 filemon_setpid_parent(struct filemon *F, pid_t pid) 106 { 107 /* Nothing to do! */ 108 } 109 110 int 111 filemon_setpid_child(const struct filemon *F, pid_t pid) 112 { 113 114 /* Just pass it on to the kernel. */ 115 return ioctl(F->fd, FILEMON_SET_PID, &pid); 116 } 117 118 int 119 filemon_close(struct filemon *F) 120 { 121 int error = 0; 122 123 /* Close the filemon device fd. */ 124 if (close(F->fd) == -1 && error == 0) 125 error = errno; 126 127 /* Free the filemon descriptor. */ 128 free(F); 129 130 /* Set errno and return -1 if anything went wrong. */ 131 if (error) { 132 errno = error; 133 return -1; 134 } 135 136 /* Success! */ 137 return 0; 138 } 139 140 int 141 filemon_readfd(const struct filemon *F) 142 { 143 144 return -1; 145 } 146 147 int 148 filemon_process(struct filemon *F) 149 { 150 151 return 0; 152 } 153