2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2005-2009 Stanislav Sedov <stas@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/queue.h>
34 #include <sys/sysctl.h>
41 #include <libprocstat.h>
52 #include "functions.h"
55 * File access mode flags table.
61 {PS_FST_FFLAG_WRITE, 'w'},
62 {PS_FST_FFLAG_APPEND, 'a'},
63 {PS_FST_FFLAG_DIRECT, 'd'},
64 {PS_FST_FFLAG_SHLOCK, 's'},
65 {PS_FST_FFLAG_EXLOCK, 'e'}
67 #define NFFLAGS (sizeof(fflags) / sizeof(*fflags))
70 * Usage flags translation table.
76 {PS_FST_UFLAG_RDIR, 'r'},
77 {PS_FST_UFLAG_CDIR, 'c'},
78 {PS_FST_UFLAG_JAIL, 'j'},
79 {PS_FST_UFLAG_TRACE, 't'},
80 {PS_FST_UFLAG_TEXT, 'x'},
81 {PS_FST_UFLAG_MMAP, 'm'},
82 {PS_FST_UFLAG_CTTY, 'y'}
84 #define NUFLAGS (sizeof(uflags) / sizeof(*uflags))
92 STAILQ_ENTRY(consumer) next;
98 STAILQ_HEAD(, consumer) consumers;
104 #define UFLAG 0x01 /* -u flag: show users */
105 #define FFLAG 0x02 /* -f flag: specified files only */
106 #define CFLAG 0x04 /* -c flag: treat as mpoints */
107 #define MFLAG 0x10 /* -m flag: mmapped files too */
108 #define KFLAG 0x20 /* -k flag: send signal (SIGKILL by default) */
110 static int flags = 0; /* Option flags. */
112 static void printflags(struct consumer *consumer);
113 static int str2sig(const char *str);
114 static void usage(void) __dead2;
115 static int addfile(const char *path, struct reqfile *reqfile);
116 static void dofiles(struct procstat *procstat, struct kinfo_proc *kp,
117 struct reqfile *reqfiles, size_t nfiles);
124 "usage: fuser [-cfhkmu] [-M core] [-N system] [-s signal] file ...\n");
129 printflags(struct consumer *cons)
134 for (i = 0; i < NUFLAGS; i++)
135 if ((cons->uflags & uflags[i].flag) != 0)
136 fputc(uflags[i].ch, stderr);
137 for (i = 0; i < NFFLAGS; i++)
138 if ((cons->flags & fflags[i].flag) != 0)
139 fputc(fflags[i].ch, stderr);
143 * Add file to the list.
146 addfile(const char *path, struct reqfile *reqfile)
151 if (stat(path, &sb) != 0) {
155 reqfile->fileid = sb.st_ino;
156 reqfile->fsid = sb.st_dev;
157 reqfile->name = path;
158 STAILQ_INIT(&reqfile->consumers);
163 do_fuser(int argc, char *argv[])
165 struct consumer *consumer;
166 struct kinfo_proc *procs;
167 struct procstat *procstat;
168 struct reqfile *reqfiles;
169 char *ep, *nlistf, *memf;
171 unsigned int i, cnt, nfiles;
173 sig = SIGKILL; /* Default to kill. */
176 while ((ch = getopt(argc, argv, "M:N:cfhkms:u")) != -1)
179 if ((flags & CFLAG) != 0)
184 if ((flags & FFLAG) != 0)
204 if (isdigit(*optarg)) {
205 sig = strtol(optarg, &ep, 10);
206 if (*ep != '\0' || sig < 0 || sig >= sys_nsig)
207 errx(EX_USAGE, "illegal signal number" ": %s",
210 sig = str2sig(optarg);
212 errx(EX_USAGE, "illegal signal name: "
231 * Process named files.
233 reqfiles = malloc(argc * sizeof(struct reqfile));
234 if (reqfiles == NULL)
235 err(EX_OSERR, "malloc()");
238 if (!addfile(*(argv++), &reqfiles[nfiles]))
241 errx(EX_IOERR, "files not accessible");
244 procstat = procstat_open_kvm(nlistf, memf);
246 procstat = procstat_open_sysctl();
247 if (procstat == NULL)
248 errx(1, "procstat_open()");
249 procs = procstat_getprocs(procstat, KERN_PROC_PROC, 0, &cnt);
251 errx(1, "procstat_getprocs()");
254 * Walk through process table and look for matching files.
256 for (i = 0; i < cnt; i++)
257 if (procs[i].ki_stat != SZOMB)
258 dofiles(procstat, &procs[i], reqfiles, nfiles);
260 for (i = 0; i < nfiles; i++) {
261 fprintf(stderr, "%s:", reqfiles[i].name);
263 STAILQ_FOREACH(consumer, &reqfiles[i].consumers, next) {
264 if (consumer->flags != 0) {
265 fprintf(stdout, "%6d", consumer->pid);
267 printflags(consumer);
268 if ((flags & UFLAG) != 0)
269 fprintf(stderr, "(%s)",
270 user_from_uid(consumer->uid, 0));
271 if ((flags & KFLAG) != 0)
272 kill(consumer->pid, sig);
276 (void)fprintf(stderr, "\n");
278 procstat_freeprocs(procstat, procs);
279 procstat_close(procstat);
285 dofiles(struct procstat *procstat, struct kinfo_proc *kp,
286 struct reqfile *reqfiles, size_t nfiles)
289 struct consumer *cons;
290 struct filestat *fst;
291 struct filestat_list *head;
294 char errbuf[_POSIX2_LINE_MAX];
296 head = procstat_getfiles(procstat, kp, flags & MFLAG);
299 STAILQ_FOREACH(fst, head, next) {
300 if (fst->fs_type != PS_FST_TYPE_VNODE)
302 error = procstat_get_vnode_info(procstat, fst, &vn, errbuf);
305 for (i = 0; i < nfiles; i++) {
306 if (flags & CFLAG && reqfiles[i].fsid == vn.vn_fsid) {
309 else if (reqfiles[i].fsid == vn.vn_fsid &&
310 reqfiles[i].fileid == vn.vn_fileid) {
313 else if (!(flags & FFLAG) &&
314 (vn.vn_type == PS_FST_VTYPE_VCHR ||
315 vn.vn_type == PS_FST_VTYPE_VBLK) &&
316 vn.vn_fsid == reqfiles[i].fileid) {
321 continue; /* No match. */
324 * Look for existing entries.
327 STAILQ_FOREACH(cons, &reqfiles[i].consumers, next)
328 if (cons->pid == kp->ki_pid) {
332 if (match == 1) { /* Use old entry. */
333 cons->flags |= fst->fs_fflags;
334 cons->uflags |= fst->fs_uflags;
337 * Create new entry in the consumer chain.
339 cons = calloc(1, sizeof(struct consumer));
344 cons->uid = kp->ki_uid;
345 cons->pid = kp->ki_pid;
346 cons->uflags = fst->fs_uflags;
347 cons->flags = fst->fs_fflags;
348 STAILQ_INSERT_TAIL(&reqfiles[i].consumers, cons, next);
351 procstat_freefiles(procstat, head);
355 * Returns signal number for it's string representation.
358 str2sig(const char *str)
362 if (!strncasecmp(str, "SIG", 3))
364 for (i = 1; i < sys_nsig; i++) {
365 if (!strcasecmp(sys_signame[i], str))