xref: /freebsd/usr.bin/fstat/fuser.c (revision 7afb8adff33d47f10a11368ff54bb2eec5b30165)
1 /*-
2  * Copyright (c) 2005-2009 Stanislav Sedov <stas@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  */
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/queue.h>
31 #include <sys/stat.h>
32 #include <sys/sysctl.h>
33 #include <sys/user.h>
34 
35 #include <assert.h>
36 #include <ctype.h>
37 #include <err.h>
38 #include <fcntl.h>
39 #include <libprocstat.h>
40 #include <limits.h>
41 #include <paths.h>
42 #include <pwd.h>
43 #include <signal.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <sysexits.h>
48 #include <unistd.h>
49 
50 #include "functions.h"
51 
52 /*
53  * File access mode flags table.
54  */
55 static const struct {
56 	int	flag;
57 	char	ch;
58 } fflags[] = {
59 	{PS_FST_FFLAG_WRITE,	'w'},
60 	{PS_FST_FFLAG_APPEND,	'a'},
61 	{PS_FST_FFLAG_DIRECT,	'd'},
62 	{PS_FST_FFLAG_SHLOCK,	's'},
63 	{PS_FST_FFLAG_EXLOCK,	'e'}
64 };
65 #define	NFFLAGS	(sizeof(fflags) / sizeof(*fflags))
66 
67 /*
68  * Usage flags translation table.
69  */
70 static const struct {
71 	int	flag;
72 	char	ch;
73 } uflags[] = {
74 	{PS_FST_UFLAG_RDIR,	'r'},
75 	{PS_FST_UFLAG_CDIR,	'c'},
76 	{PS_FST_UFLAG_JAIL,	'j'},
77 	{PS_FST_UFLAG_TRACE,	't'},
78 	{PS_FST_UFLAG_TEXT,	'x'},
79 	{PS_FST_UFLAG_MMAP,	'm'},
80 	{PS_FST_UFLAG_CTTY,	'y'}
81 };
82 #define	NUFLAGS	(sizeof(uflags) / sizeof(*uflags))
83 
84 struct consumer {
85 	pid_t	pid;
86 	uid_t	uid;
87 	int	fd;
88 	int	flags;
89 	int	uflags;
90 	STAILQ_ENTRY(consumer)	next;
91 };
92 struct reqfile {
93 	uint32_t	fsid;
94 	uint64_t	fileid;
95 	const char	*name;
96 	STAILQ_HEAD(, consumer) consumers;
97 };
98 
99 /*
100  * Option flags.
101  */
102 #define	UFLAG	0x01	/* -u flag: show users				*/
103 #define	FFLAG	0x02	/* -f flag: specified files only		*/
104 #define	CFLAG	0x04	/* -c flag: treat as mpoints			*/
105 #define	MFLAG	0x10	/* -m flag: mmapped files too			*/
106 #define	KFLAG	0x20	/* -k flag: send signal (SIGKILL by default)	*/
107 
108 static int flags = 0;	/* Option flags. */
109 
110 static void	printflags(struct consumer *consumer);
111 static int	str2sig(const char *str);
112 static void	usage(void) __dead2;
113 static int	addfile(const char *path, struct reqfile *reqfile);
114 static void	dofiles(struct procstat *procstat, struct kinfo_proc *kp,
115     struct reqfile *reqfiles, size_t nfiles);
116 
117 static void
118 usage(void)
119 {
120 
121 	fprintf(stderr,
122 "usage: fuser [-cfhkmu] [-M core] [-N system] [-s signal] file ...\n");
123 	exit(EX_USAGE);
124 }
125 
126 static void
127 printflags(struct consumer *cons)
128 {
129 	unsigned int i;
130 
131 	assert(cons);
132 	for (i = 0; i < NUFLAGS; i++)
133 		if ((cons->uflags & uflags[i].flag) != 0)
134 			fputc(uflags[i].ch, stderr);
135 	for (i = 0; i < NFFLAGS; i++)
136 		if ((cons->flags & fflags[i].flag) != 0)
137 			fputc(fflags[i].ch, stderr);
138 }
139 
140 /*
141  * Add file to the list.
142  */
143 static int
144 addfile(const char *path, struct reqfile *reqfile)
145 {
146 	struct stat sb;
147 
148 	assert(path);
149 	if (stat(path, &sb) != 0) {
150 		warn("%s", path);
151 		return (1);
152 	}
153 	reqfile->fileid = sb.st_ino;
154 	reqfile->fsid = sb.st_dev;
155 	reqfile->name = path;
156 	STAILQ_INIT(&reqfile->consumers);
157 	return (0);
158 }
159 
160 int
161 do_fuser(int argc, char *argv[])
162 {
163 	struct consumer *consumer;
164 	struct kinfo_proc *p, *procs;
165 	struct procstat *procstat;
166 	struct reqfile *reqfiles;
167 	char *ep, *nlistf, *memf;
168 	int ch, cnt, sig;
169 	unsigned int i, nfiles;
170 
171 	sig = SIGKILL;	/* Default to kill. */
172 	nlistf = NULL;
173 	memf = NULL;
174 	while ((ch = getopt(argc, argv, "M:N:cfhkms:u")) != -1)
175 		switch(ch) {
176 		case 'f':
177 			if ((flags & CFLAG) != 0)
178 				usage();
179 			flags |= FFLAG;
180 			break;
181 		case 'c':
182 			if ((flags & FFLAG) != 0)
183 				usage();
184 			flags |= CFLAG;
185 			break;
186 		case 'N':
187 			nlistf = optarg;
188 			break;
189 		case 'M':
190 			memf = optarg;
191 			break;
192 		case 'u':
193 			flags |= UFLAG;
194 			break;
195 		case 'm':
196 			flags |= MFLAG;
197 			break;
198 		case 'k':
199 			flags |= KFLAG;
200 			break;
201 		case 's':
202 			if (isdigit(*optarg)) {
203 				sig = strtol(optarg, &ep, 10);
204 				if (*ep != '\0' || sig < 0 || sig >= sys_nsig)
205 					errx(EX_USAGE, "illegal signal number" ": %s",
206 					    optarg);
207 			} else {
208 				sig = str2sig(optarg);
209 				if (sig < 0)
210 					errx(EX_USAGE, "illegal signal name: "
211 					    "%s", optarg);
212 			}
213 			break;
214 		case 'h':
215 			/* PASSTHROUGH */
216 		default:
217 			usage();
218 			/* NORETURN */
219 		}
220 	argv += optind;
221 	argc -= optind;
222 
223 	assert(argc >= 0);
224 	if (argc == 0)
225 		usage();
226 		/* NORETURN */
227 
228 	/*
229 	 * Process named files.
230 	 */
231 	reqfiles = malloc(argc * sizeof(struct reqfile));
232 	if (reqfiles == NULL)
233 		err(EX_OSERR, "malloc()");
234 	nfiles = 0;
235 	while (argc--)
236 		if (!addfile(*(argv++), &reqfiles[nfiles]))
237 			nfiles++;
238 	if (nfiles == 0)
239 		errx(EX_IOERR, "files not accessible");
240 
241 	if (memf != NULL)
242 		procstat = procstat_open_kvm(nlistf, memf);
243 	else
244 		procstat = procstat_open_sysctl();
245 	if (procstat == NULL)
246 		errx(1, "procstat_open()");
247 	procs = procstat_getprocs(procstat, KERN_PROC_PROC, 0, &cnt);
248 	if (procs == NULL)
249 		 errx(1, "procstat_getprocs()");
250 
251 	/*
252 	 * Walk through process table and look for matching files.
253 	 */
254 	p = procs;
255 	while(cnt--)
256 		if (p->ki_stat != SZOMB)
257 			dofiles(procstat, p++, reqfiles, nfiles);
258 
259 	for (i = 0; i < nfiles; i++) {
260 		fprintf(stderr, "%s:", reqfiles[i].name);
261 		fflush(stderr);
262 		STAILQ_FOREACH(consumer, &reqfiles[i].consumers, next) {
263 			if (consumer->flags != 0) {
264 				fprintf(stdout, "%6d", consumer->pid);
265 				fflush(stdout);
266 				printflags(consumer);
267 				if ((flags & UFLAG) != 0)
268 					fprintf(stderr, "(%s)",
269 					    user_from_uid(consumer->uid, 0));
270 				if ((flags & KFLAG) != 0)
271 					kill(consumer->pid, sig);
272 				fflush(stderr);
273 			}
274 		}
275 		(void)fprintf(stderr, "\n");
276 	}
277 	procstat_freeprocs(procstat, procs);
278 	procstat_close(procstat);
279 	free(reqfiles);
280 	return (0);
281 }
282 
283 static void
284 dofiles(struct procstat *procstat, struct kinfo_proc *kp,
285     struct reqfile *reqfiles, size_t nfiles)
286 {
287 	struct vnstat vn;
288 	struct consumer *cons;
289 	struct filestat *fst;
290 	struct filestat_list *head;
291 	int error, match;
292 	unsigned int i;
293 	char errbuf[_POSIX2_LINE_MAX];
294 
295 	head = procstat_getfiles(procstat, kp, flags & MFLAG);
296 	if (head == NULL)
297 		return;
298 	STAILQ_FOREACH(fst, head, next) {
299 		if (fst->fs_type != PS_FST_TYPE_VNODE)
300 			continue;
301 		error = procstat_get_vnode_info(procstat, fst, &vn, errbuf);
302 		if (error != 0)
303 			continue;
304 		for (i = 0; i < nfiles; i++) {
305 			if (flags & CFLAG && reqfiles[i].fsid == vn.vn_fsid) {
306 				break;
307 			}
308 			else if (reqfiles[i].fsid == vn.vn_fsid &&
309 			    reqfiles[i].fileid == vn.vn_fileid) {
310 				break;
311 			}
312 			else if (!(flags & FFLAG) &&
313 			    (vn.vn_type == PS_FST_VTYPE_VCHR ||
314 			    vn.vn_type == PS_FST_VTYPE_VBLK) &&
315 			    vn.vn_fsid == reqfiles[i].fileid) {
316 				break;
317 			}
318 		}
319 		if (i == nfiles)
320 			continue;	/* No match. */
321 
322 		/*
323 		 * Look for existing entries.
324 		 */
325 		match = 0;
326 		STAILQ_FOREACH(cons, &reqfiles[i].consumers, next)
327 			if (cons->pid == kp->ki_pid) {
328 				match = 1;
329 				break;
330 			}
331 		if (match == 1) {	/* Use old entry. */
332 			cons->flags |= fst->fs_fflags;
333 			cons->uflags |= fst->fs_uflags;
334 		} else {
335 			/*
336 			 * Create new entry in the consumer chain.
337 			 */
338 			cons = calloc(1, sizeof(struct consumer));
339 			if (cons == NULL) {
340 				warn("malloc()");
341 				continue;
342 			}
343 			cons->uid = kp->ki_uid;
344 			cons->pid = kp->ki_pid;
345 			cons->uflags = fst->fs_uflags;
346 			cons->flags = fst->fs_fflags;
347 			STAILQ_INSERT_TAIL(&reqfiles[i].consumers, cons, next);
348 		}
349 	}
350 	procstat_freefiles(procstat, head);
351 }
352 
353 /*
354  * Returns signal number for it's string representation.
355  */
356 static int
357 str2sig(const char *str)
358 {
359 	int i;
360 
361 	if (!strncasecmp(str, "SIG", 3))
362 		str += 3;
363 	for (i = 1; i < sys_nsig; i++) {
364                 if (!strcasecmp(sys_signame[i], str))
365                         return (i);
366         }
367         return (-1);
368 }
369