2 * SPDX-License-Identifier: BSD-2-Clause
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 #include <sys/queue.h>
32 #include <sys/sysctl.h>
39 #include <libprocstat.h>
50 #include "functions.h"
53 * File access mode flags table.
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'}
65 #define NFFLAGS (sizeof(fflags) / sizeof(*fflags))
68 * Usage flags translation table.
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'}
82 #define NUFLAGS (sizeof(uflags) / sizeof(*uflags))
90 STAILQ_ENTRY(consumer) next;
96 STAILQ_HEAD(, consumer) consumers;
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) */
108 static int flags = 0; /* Option flags. */
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);
122 "usage: fuser [-cfhkmu] [-M core] [-N system] [-s signal] file ...\n");
127 printflags(struct consumer *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);
141 * Add file to the list.
144 addfile(const char *path, struct reqfile *reqfile)
149 if (stat(path, &sb) != 0) {
153 reqfile->fileid = sb.st_ino;
154 reqfile->fsid = sb.st_dev;
155 reqfile->name = path;
156 STAILQ_INIT(&reqfile->consumers);
161 do_fuser(int argc, char *argv[])
163 struct consumer *consumer;
164 struct kinfo_proc *procs;
165 struct procstat *procstat;
166 struct reqfile *reqfiles;
167 char *ep, *nlistf, *memf;
169 unsigned int i, cnt, nfiles;
171 sig = SIGKILL; /* Default to kill. */
174 while ((ch = getopt(argc, argv, "M:N:cfhkms:u")) != -1)
177 if ((flags & CFLAG) != 0)
182 if ((flags & FFLAG) != 0)
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",
208 sig = str2sig(optarg);
210 errx(EX_USAGE, "illegal signal name: "
229 * Process named files.
231 reqfiles = malloc(argc * sizeof(struct reqfile));
232 if (reqfiles == NULL)
233 err(EX_OSERR, "malloc()");
236 if (!addfile(*(argv++), &reqfiles[nfiles]))
239 errx(EX_IOERR, "files not accessible");
242 procstat = procstat_open_kvm(nlistf, memf);
244 procstat = procstat_open_sysctl();
245 if (procstat == NULL)
246 errx(1, "procstat_open()");
247 procs = procstat_getprocs(procstat, KERN_PROC_PROC, 0, &cnt);
249 errx(1, "procstat_getprocs()");
252 * Walk through process table and look for matching files.
254 for (i = 0; i < cnt; i++)
255 if (procs[i].ki_stat != SZOMB)
256 dofiles(procstat, &procs[i], reqfiles, nfiles);
258 for (i = 0; i < nfiles; i++) {
259 fprintf(stderr, "%s:", reqfiles[i].name);
261 STAILQ_FOREACH(consumer, &reqfiles[i].consumers, next) {
262 if (consumer->flags != 0) {
263 fprintf(stdout, "%6d", consumer->pid);
265 printflags(consumer);
266 if ((flags & UFLAG) != 0)
267 fprintf(stderr, "(%s)",
268 user_from_uid(consumer->uid, 0));
269 if ((flags & KFLAG) != 0)
270 kill(consumer->pid, sig);
274 (void)fprintf(stderr, "\n");
276 procstat_freeprocs(procstat, procs);
277 procstat_close(procstat);
283 dofiles(struct procstat *procstat, struct kinfo_proc *kp,
284 struct reqfile *reqfiles, size_t nfiles)
287 struct consumer *cons;
288 struct filestat *fst;
289 struct filestat_list *head;
292 char errbuf[_POSIX2_LINE_MAX];
294 head = procstat_getfiles(procstat, kp, flags & MFLAG);
297 STAILQ_FOREACH(fst, head, next) {
298 if (fst->fs_type != PS_FST_TYPE_VNODE)
300 error = procstat_get_vnode_info(procstat, fst, &vn, errbuf);
303 for (i = 0; i < nfiles; i++) {
304 if (flags & CFLAG && reqfiles[i].fsid == vn.vn_fsid) {
307 else if (reqfiles[i].fsid == vn.vn_fsid &&
308 reqfiles[i].fileid == vn.vn_fileid) {
311 else if (!(flags & FFLAG) &&
312 (vn.vn_type == PS_FST_VTYPE_VCHR ||
313 vn.vn_type == PS_FST_VTYPE_VBLK) &&
314 vn.vn_fsid == reqfiles[i].fileid) {
319 continue; /* No match. */
322 * Look for existing entries.
325 STAILQ_FOREACH(cons, &reqfiles[i].consumers, next)
326 if (cons->pid == kp->ki_pid) {
330 if (match == 1) { /* Use old entry. */
331 cons->flags |= fst->fs_fflags;
332 cons->uflags |= fst->fs_uflags;
335 * Create new entry in the consumer chain.
337 cons = calloc(1, sizeof(struct consumer));
342 cons->uid = kp->ki_uid;
343 cons->pid = kp->ki_pid;
344 cons->uflags = fst->fs_uflags;
345 cons->flags = fst->fs_fflags;
346 STAILQ_INSERT_TAIL(&reqfiles[i].consumers, cons, next);
349 procstat_freefiles(procstat, head);
353 * Returns signal number for it's string representation.
356 str2sig(const char *str)
360 if (!strncasecmp(str, "SIG", 3))
362 for (i = 1; i < sys_nsig; i++) {
363 if (!strcasecmp(sys_signame[i], str))