xref: /freebsd/contrib/bmake/filemon/filemon_dev.c (revision a0409676120c1e558d0ade943019934e0f15118d)
1 /*	$NetBSD: filemon_dev.c,v 1.8 2021/02/01 21:09:25 rillig 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|O_CLOEXEC)) == -1; i++) {
74 		if (i == 5) {
75 			error = errno;
76 			goto fail0;
77 		}
78 	}
79 
80 	/* Success!  */
81 	return F;
82 
83 fail0:	free(F);
84 	errno = error;
85 	return NULL;
86 }
87 
88 int
89 filemon_setfd(struct filemon *F, int fd)
90 {
91 
92 	/* Point the kernel at this file descriptor.  */
93 	if (ioctl(F->fd, FILEMON_SET_FD, &fd) == -1)
94 		return -1;
95 
96 	/* No need for it in userland any more; close it.  */
97 	(void)close(fd);
98 
99 	/* Success!  */
100 	return 0;
101 }
102 
103 void
104 filemon_setpid_parent(struct filemon *F, pid_t pid)
105 {
106 	/* Nothing to do!  */
107 }
108 
109 int
110 filemon_setpid_child(const struct filemon *F, pid_t pid)
111 {
112 
113 	/* Just pass it on to the kernel.  */
114 	return ioctl(F->fd, FILEMON_SET_PID, &pid);
115 }
116 
117 int
118 filemon_close(struct filemon *F)
119 {
120 	int error = 0;
121 
122 	/* Close the filemon device fd.  */
123 	if (close(F->fd) == -1 && error == 0)
124 		error = errno;
125 
126 	/* Free the filemon descriptor.  */
127 	free(F);
128 
129 	/* Set errno and return -1 if anything went wrong.  */
130 	if (error != 0) {
131 		errno = error;
132 		return -1;
133 	}
134 
135 	/* Success!  */
136 	return 0;
137 }
138 
139 int
140 filemon_readfd(const struct filemon *F)
141 {
142 
143 	return -1;
144 }
145 
146 int
147 filemon_process(struct filemon *F)
148 {
149 
150 	return 0;
151 }
152