summaryrefslogtreecommitdiff
path: root/usr.bin/fstat/fuser.c
blob: 6713055c1dbbbc91e50b0773238710019bc9f541 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*	$OpenBSD: fuser.c,v 1.3 2013/10/22 16:40:28 guenther Exp $	*/

/*
 * Copyright (c) 2009 Todd C. Miller <Todd.Miller@courtesan.com>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

/*
 * Copyright (c) 2002 Peter Werner <peterw@ifost.org.au>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/param.h>
#include <sys/queue.h>
#include <sys/stat.h>
#include <sys/sysctl.h>
#define _KERNEL /* for DTYPE_VNODE */
#include <sys/file.h>
#undef _KERNEL

#include <err.h>
#include <fcntl.h>
#include <pwd.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "fstat.h"

/*
 * Returns 1 if the file watched (fa) is equivalent
 * to a file held by a process (kf), else 0.
 */
static int
match(struct filearg *fa, struct kinfo_file *kf)
{
	if (fa->dev == kf->va_fsid) {
		if (cflg)
			return (1);
		if (fa->ino == kf->va_fileid)
			return (1);
	}
	return (0);
}

/*
 * Examine kinfo_file struct and record the details if they
 * match a watched file.
 */
void
fuser_check(struct kinfo_file *kf)
{
	struct filearg *fa;
	struct fuser *fu;

	if (kf->f_type != DTYPE_VNODE)
		return;

	SLIST_FOREACH(fa, &fileargs, next) {
		if (!match(fa, kf))
			continue;

		/*
		 * This assumes that kinfo_files2 returns all files
		 * associated with a process in a contiguous block.
		 */
		if (TAILQ_EMPTY(&fa->fusers) || kf->p_pid !=
		    (fu = TAILQ_LAST(&fa->fusers, fuserhead))->pid) {
			fu = malloc(sizeof(*fu));
			if (fu == NULL)
				err(1, NULL);
			fu->pid = kf->p_pid;
			fu->uid = kf->p_uid;
			fu->flags = 0;
			TAILQ_INSERT_TAIL(&fa->fusers, fu, tq);
		}
		switch (kf->fd_fd) {
		case KERN_FILE_CDIR:
			fu->flags |= F_CWD;
			break;
		case KERN_FILE_RDIR:
			fu->flags |= F_ROOT;
			break;
		case KERN_FILE_TEXT:
			fu->flags |= F_TEXT;
			break;
		case KERN_FILE_TRACE:
			/* ignore */
			break;
		default:
			fu->flags |= F_OPEN;
			break;
		}
	}
}

/*
 * Print out the specfics for a given file/filesystem
 */
static void
printfu(struct fuser *fu)
{
	struct passwd *pwd;

	printf("%d", fu->pid);
	fflush(stdout);

	if (fu->flags & F_CWD)
		fprintf(stderr, "c");

	if (fu->flags & F_ROOT)
		fprintf(stderr, "r");

	if (fu->flags & F_TEXT)
		fprintf(stderr, "t");

	if (uflg) {
		pwd = getpwuid(fu->uid);
		if (pwd != NULL)
			fprintf(stderr, "(%s)", pwd->pw_name);
		else
			fprintf(stderr, "(%d)", fu->uid);
	}

	putchar(' ');
}

/*
 * For each file, print matching process info and optionally send a signal.
 */
void
fuser_run(void)
{
	struct filearg *fa;
	struct fuser *fu;
	pid_t mypid = getpid();

	SLIST_FOREACH(fa, &fileargs, next) {
		fprintf(stderr, "%s: ", fa->name);
		TAILQ_FOREACH(fu, &fa->fusers, tq) {
			printfu(fu);
			if (sflg && fu->pid != mypid) {
				kill(fu->pid, signo);
			}
		}
		fflush(stdout);
		fprintf(stderr, "\n");
	}
}