File: ofl.c

package info (click to toggle)
hxtools 20251011-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,468 kB
  • sloc: ansic: 4,384; perl: 3,467; sh: 1,664; cpp: 353; makefile: 90
file content (335 lines) | stat: -rw-r--r-- 7,582 bytes parent folder | download
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
// SPDX-License-Identifier: MIT
// SPDX-FileCopyrightText: 2008-2010 Jan Engelhardt
/*
 *	Show processes using directories/files/mountpoints
 *
 *	(While it says mountpoint in the source, any directory is acceptable,
 *	as are files.)
 */
#ifdef HAVE_CONFIG_H
#	include "config.h"
#endif
#include <sys/stat.h>
#include <sys/types.h>
#include <errno.h>
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <libHX.h>

/**
 * @sb:		just space
 * @pid:	pid for current process
 * @signal:	signal to send
 * @check:	check for symlink
 * @found:	found something (used for exit value)
 */
struct ofl_compound {
	struct stat sb;
	pid_t pid;
	unsigned char signal;
	bool check, found;
};

static bool pids_only;

static const char *ofl_comm(pid_t pid, char *buf, size_t size)
{
	char src[64], dst[512];
	const char *p;
	ssize_t ret;

	snprintf(src, sizeof(src), COMPAT_PROC "/proc/%u/exe", (unsigned int)pid);
	ret = readlink(src, dst, sizeof(dst) - 1);
	if (ret < 0) {
		*buf = '\0';
		return buf;
	}
	dst[ret] = '\0';
	p = HX_basename(dst);
	strncpy(buf, p, size);
	return buf;
}

/**
 * ofl_file - check if file is within directory
 * @mnt:	mountpoint
 * @file:	file that is supposed to be within @mnt
 *
 * Returns true if that seems so.
 * We do not check for the existence of @file using lstat() or so - it is
 * assumed this exists if it is found through procfs. In fact,
 * /proc/<pid>/task/<tid>/fd/<n> might point to the ominous
 * "/foo/bar (deleted)" which almost never exists, but it shows us anyway that
 * the file is still in use.
 */
static bool ofl_file(const char *mnt, const char *file, const char *ll_entry,
    struct ofl_compound *data)
{
	ssize_t mnt_len;
	const char *p;

	/* Strip extra slashes at the end */
	mnt_len = strlen(mnt);
	for (p = mnt + mnt_len - 1; p >= mnt && *p == '/'; --p)
		--mnt_len;

	if (strncmp(file, mnt, mnt_len) != 0)
		return false;
	if (file[mnt_len] != '\0' && file[mnt_len] != '/')
		return false;

	data->found = true;
	if (pids_only) {
		printf("%u ", data->pid);
	} else if (data->signal == 0) {
		char buf[24];
		printf("%u(%s): %s -> %s\n", data->pid,
		       ofl_comm(data->pid, buf, sizeof(buf)), ll_entry, file);
		return false; /* so that more FDs will be inspected */
	}

	if (kill(data->pid, data->signal) < 0) {
		if (errno == ESRCH)
			return true;
		return false;
	}
	return true;
}

/**
 * ofl_pmap - read process mappings
 * @mnt:	mountpoint
 * @map_file:	/proc/<pid>/maps
 */
static bool ofl_pmap(const char *mnt, const char *map_file,
    struct ofl_compound *data)
{
	hxmc_t *line = NULL;
	bool ret = false;
	unsigned int i;
	const char *p;
	FILE *fp;

	if ((fp = fopen(map_file, "r")) == NULL)
		return false;

	while (HX_getl(&line, fp) != NULL) {
		HX_chomp(line);
		p = line;
		for (i = 0; i < 5; ++i) {
			while (!HX_isspace(*p) && *p != '\0')
				++p;
			while (HX_isspace(*p))
				++p;
		}
		if (*p == '\0')
			continue;
		ret = ofl_file(mnt, p, map_file, data);
		if (ret)
			break;
	}

	HXmc_free(line);
	fclose(fp);
	return ret;
}

/**
 * ofl_one - check a symlink
 * @mnt:	Mountpoint that is to be removed.
 * @entry:	Path to a symlink.
 *
 * Returns true if the process does not exist anymore or has been signalled.
 */
static bool ofl_one(const char *mnt, const char *entry,
    struct ofl_compound *data)
{
	ssize_t lnk_len;
	char tmp[512];

	if (data->check)
		if (lstat(entry, &data->sb) < 0 || !S_ISLNK(data->sb.st_mode))
			return false;

	lnk_len = readlink(entry, tmp, sizeof(tmp) - 1);
	if (lnk_len < 0)
		return false;
	tmp[lnk_len] = '\0';

	return ofl_file(mnt, tmp, entry, data);
}

/**
 * ofl_taskfd - iterate through /proc/<pid>/task/<tid>/fd/
 */
static bool ofl_taskfd(const char *mnt, const char *path,
    struct ofl_compound *data)
{
	const char *de;
	char tmp[256];
	struct HXdir *dir;
	bool ret = false;

	dir = HXdir_open(path);
	if (dir == NULL)
		return false;
	while ((de = HXdir_read(dir)) != NULL) {
		if (*de == '.')
			continue;
		snprintf(tmp, sizeof(tmp), "%s/%s", path, de);
		if (lstat(tmp, &data->sb) < 0 || !S_ISLNK(data->sb.st_mode))
			continue;
		ret = ofl_one(mnt, tmp, data);
		if (ret)
			break;
	}
	HXdir_close(dir);
	return ret;
}

/**
 * ofl_task - iterate through /proc/<pid>/task/
 */
static void ofl_task(const char *mnt, const char *path,
    struct ofl_compound *data)
{
	const char *de;
	char tmp[256];
	struct HXdir *dir;

	dir = HXdir_open(path);
	if (dir == NULL)
		return;
	while ((de = HXdir_read(dir)) != NULL) {
		if (*de == '.')
			continue;
		snprintf(tmp, sizeof(tmp), "%s/%s/fd", path, de);
		if (lstat(tmp, &data->sb) < 0 || !S_ISDIR(data->sb.st_mode))
			continue;
		ofl_taskfd(mnt, tmp, data);
	}
	HXdir_close(dir);
}

/**
 * ofl - filesystem use checker
 * @mnt:	mountpoint to search for
 * @action:	action to take
 */
static bool ofl(const char *mnt, unsigned int signum)
{
	struct ofl_compound data = {.signal = signum};
	const char *de;
	char tmp[256];
	struct HXdir *dir;

	dir = HXdir_open(COMPAT_PROC "/proc");
	if (dir == NULL)
		return false;
	while ((de = HXdir_read(dir)) != NULL) {
		if (*de == '.')
			continue;
		data.pid = strtoul(de, NULL, 0);
		if (data.pid == 0)
			continue;
		snprintf(tmp, sizeof(tmp), COMPAT_PROC "/proc/%s", de);
		if (lstat(tmp, &data.sb) < 0 || !S_ISDIR(data.sb.st_mode))
			continue;

		/* Program map */
		snprintf(tmp, sizeof(tmp), COMPAT_PROC "/proc/%s/maps", de);
		if (ofl_pmap(mnt, tmp, &data))
			continue;

		/* Basic links */
		data.check = true;
		snprintf(tmp, sizeof(tmp), COMPAT_PROC "/proc/%s/root", de);
		if (ofl_one(mnt, tmp, &data))
			continue;
		snprintf(tmp, sizeof(tmp), COMPAT_PROC "/proc/%s/cwd", de);
		if (ofl_one(mnt, tmp, &data))
			continue;
		snprintf(tmp, sizeof(tmp), COMPAT_PROC "/proc/%s/exe", de);
		if (ofl_one(mnt, tmp, &data))
			continue;

		/* All file descriptors */
		data.check = false;
		snprintf(tmp, sizeof(tmp), COMPAT_PROC "/proc/%s/task", de);
		ofl_task(mnt, tmp, &data);
	}

	return data.found;
}

static unsigned int parse_signal(const char *str)
{
	static const char *signames[] = {
		[SIGHUP]  = "HUP",	[SIGINT]  = "INT",
		[SIGQUIT] = "QUIT",	[SIGKILL] = "KILL",
		[SIGTERM] = "TERM",	[SIGALRM] = "ALRM",
		[SIGPIPE] = "PIPE",
	};
	unsigned int ret;
	char *end;

	if (HX_isdigit(*str)) {
		ret = strtoul(str, &end, 10);
		if (*end == '\0')
			return ret;
	}

	for (ret = 0; ret < ARRAY_SIZE(signames); ++ret)
		if (signames[ret] != NULL && strcmp(str, signames[ret]) == 0)
			return ret;
	return 0;
}

int main(int argc, char **argv)
{
	unsigned int signum = 0;
	const char *signum_str = NULL;
	struct HXoption options_table[] = {
		{.sh = 'P', .type = HXTYPE_NONE, .ptr = &pids_only,
		 .help = "Show only PIDs"},
		{.sh = 'k', .type = HXTYPE_STRP, .ptr = &signum_str,
		 .help = "Signal to send (if any)", .htyp = "NUM/NAME"},
		HXOPT_AUTOHELP,
		HXOPT_TABLEEND,
	};
	struct HXopt6_result argp;
	int ret;

	if ((ret = HX_init()) <= 0) {
		fprintf(stderr, "HX_init: %s\n", strerror(-ret));
		abort();
	}
	if (HX_getopt6(options_table, argc, argv, &argp,
	    HXOPT_USAGEONERR | HXOPT_ITER_ARGS) != HXOPT_ERR_SUCCESS)
		goto out;
	if (argp.nargs == 0) {
		fprintf(stderr, "You need to supply at least a path\n");
		goto out;
	}

	if (signum_str != NULL)
		signum = parse_signal(signum_str);
	ret = false;
	for (int i = 0; i < argp.nargs; ++i)
		ret |= ofl(argp.uarg[i], signum);

	if (pids_only)
		printf("\n");

	HX_getopt6_clean(&argp);
	HX_exit();
	return ret ? EXIT_SUCCESS : EXIT_FAILURE;
 out:
	HX_getopt6_clean(&argp);
	HX_exit();
	return EXIT_FAILURE + 1;
}