2 * Copyright (c) 2005-2009 Stanislav Sedov <stas@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
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 *p, *procs;
165 struct procstat *procstat;
166 struct reqfile *reqfiles;
167 char *ep, *nlistf, *memf;
169 unsigned int i, 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.
256 if (p->ki_stat != SZOMB)
257 dofiles(procstat, p++, reqfiles, nfiles);
259 for (i = 0; i < nfiles; i++) {
260 fprintf(stderr, "%s:", reqfiles[i].name);
262 STAILQ_FOREACH(consumer, &reqfiles[i].consumers, next) {
263 if (consumer->flags != 0) {
264 fprintf(stdout, "%6d", consumer->pid);
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);
275 (void)fprintf(stderr, "\n");
277 procstat_freeprocs(procstat, procs);
278 procstat_close(procstat);
284 dofiles(struct procstat *procstat, struct kinfo_proc *kp,
285 struct reqfile *reqfiles, size_t nfiles)
288 struct consumer *cons;
289 struct filestat *fst;
290 struct filestat_list *head;
293 char errbuf[_POSIX2_LINE_MAX];
295 head = procstat_getfiles(procstat, kp, flags & MFLAG);
298 STAILQ_FOREACH(fst, head, next) {
299 if (fst->fs_type != PS_FST_TYPE_VNODE)
301 error = procstat_get_vnode_info(procstat, fst, &vn, errbuf);
304 for (i = 0; i < nfiles; i++) {
305 if (flags & CFLAG && reqfiles[i].fsid == vn.vn_fsid) {
308 else if (reqfiles[i].fsid == vn.vn_fsid &&
309 reqfiles[i].fileid == vn.vn_fileid) {
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) {
320 continue; /* No match. */
323 * Look for existing entries.
326 STAILQ_FOREACH(cons, &reqfiles[i].consumers, next)
327 if (cons->pid == kp->ki_pid) {
331 if (match == 1) { /* Use old entry. */
332 cons->flags |= fst->fs_fflags;
333 cons->uflags |= fst->fs_uflags;
336 * Create new entry in the consumer chain.
338 cons = calloc(1, sizeof(struct consumer));
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);
350 procstat_freefiles(procstat, head);
354 * Returns signal number for it's string representation.
357 str2sig(const char *str)
361 if (!strncasecmp(str, "SIG", 3))
363 for (i = 1; i < sys_nsig; i++) {
364 if (!strcasecmp(sys_signame[i], str))