summaryrefslogtreecommitdiffstats
path: root/lib/fs.c
blob: 3752931cf8f8c193dd6ee42f655cc46669bd261f (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
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/*
 * fs.c         filesystem APIs
 *
 *		This program is free software; you can redistribute it and/or
 *		modify it under the terms of the GNU General Public License
 *		as published by the Free Software Foundation; either version
 *		2 of the License, or (at your option) any later version.
 *
 * Authors:	David Ahern <dsa@cumulusnetworks.com>
 *
 */

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/mount.h>
#include <ctype.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <limits.h>

#include "utils.h"

#ifndef HAVE_HANDLE_AT
# include <sys/syscall.h>
#endif

#define CGROUP2_FS_NAME "cgroup2"

/* if not already mounted cgroup2 is mounted here for iproute2's use */
#define MNT_CGRP2_PATH  "/var/run/cgroup2"


#ifndef HAVE_HANDLE_AT
struct file_handle {
	unsigned handle_bytes;
	int handle_type;
	unsigned char f_handle[];
};

static int name_to_handle_at(int dirfd, const char *pathname,
	struct file_handle *handle, int *mount_id, int flags)
{
	return syscall(__NR_name_to_handle_at, dirfd, pathname, handle,
	               mount_id, flags);
}

static int open_by_handle_at(int mount_fd, struct file_handle *handle, int flags)
{
	return syscall(__NR_open_by_handle_at, mount_fd, handle, flags);
}
#endif

/* return mount path of first occurrence of given fstype */
static char *find_fs_mount(const char *fs_to_find)
{
	char path[4096];
	char fstype[128];    /* max length of any filesystem name */
	char *mnt = NULL;
	FILE *fp;

	fp = fopen("/proc/mounts", "r");
	if (!fp) {
		fprintf(stderr,
			"Failed to open mounts file: %s\n", strerror(errno));
		return NULL;
	}

	while (fscanf(fp, "%*s %4095s %127s %*s %*d %*d\n",
		      path, fstype) == 2) {
		if (strcmp(fstype, fs_to_find) == 0) {
			mnt = strdup(path);
			break;
		}
	}

	fclose(fp);

	return mnt;
}

/* caller needs to free string returned */
char *find_cgroup2_mount(bool do_mount)
{
	char *mnt = find_fs_mount(CGROUP2_FS_NAME);

	if (mnt)
		return mnt;

	if (!do_mount) {
		fprintf(stderr, "Failed to find cgroup2 mount\n");
		return NULL;
	}

	mnt = strdup(MNT_CGRP2_PATH);
	if (!mnt) {
		fprintf(stderr, "Failed to allocate memory for cgroup2 path\n");
		return NULL;

	}

	if (make_path(mnt, 0755)) {
		fprintf(stderr, "Failed to setup cgroup2 directory\n");
		free(mnt);
		return NULL;
	}

	if (mount("none", mnt, CGROUP2_FS_NAME, 0, NULL)) {
		/* EBUSY means already mounted */
		if (errno == EBUSY)
			goto out;

		if (errno == ENODEV) {
			fprintf(stderr,
				"Failed to mount cgroup2. Are CGROUPS enabled in your kernel?\n");
		} else {
			fprintf(stderr,
				"Failed to mount cgroup2: %s\n",
				strerror(errno));
		}
		free(mnt);
		return NULL;
	}
out:
	return mnt;
}

__u64 get_cgroup2_id(const char *path)
{
	char fh_buf[sizeof(struct file_handle) + sizeof(__u64)] = { 0 };
	struct file_handle *fhp = (struct file_handle *)fh_buf;
	union {
		__u64 id;
		unsigned char bytes[sizeof(__u64)];
	} cg_id = { .id = 0 };
	char *mnt = NULL;
	int mnt_fd = -1;
	int mnt_id;

	if (!path) {
		fprintf(stderr, "Invalid cgroup2 path\n");
		return 0;
	}

	fhp->handle_bytes = sizeof(__u64);
	if (name_to_handle_at(AT_FDCWD, path, fhp, &mnt_id, 0) < 0) {
		/* try at cgroup2 mount */

		while (*path == '/')
			path++;
		if (*path == '\0') {
			fprintf(stderr, "Invalid cgroup2 path\n");
			goto out;
		}

		mnt = find_cgroup2_mount(false);
		if (!mnt)
			goto out;

		mnt_fd = open(mnt, O_RDONLY);
		if (mnt_fd < 0) {
			fprintf(stderr, "Failed to open cgroup2 mount\n");
			goto out;
		}

		fhp->handle_bytes = sizeof(__u64);
		if (name_to_handle_at(mnt_fd, path, fhp, &mnt_id, 0) < 0) {
			fprintf(stderr, "Failed to get cgroup2 ID: %s\n",
					strerror(errno));
			goto out;
		}
	}
	if (fhp->handle_bytes != sizeof(__u64)) {
		fprintf(stderr, "Invalid size of cgroup2 ID\n");
		goto out;
	}

	memcpy(cg_id.bytes, fhp->f_handle, sizeof(__u64));

out:
	if (mnt_fd >= 0)
		close(mnt_fd);
	free(mnt);

	return cg_id.id;
}

#define FILEID_INO32_GEN 1

/* caller needs to free string returned */
char *get_cgroup2_path(__u64 id, bool full)
{
	char fh_buf[sizeof(struct file_handle) + sizeof(__u64)] = { 0 };
	struct file_handle *fhp = (struct file_handle *)fh_buf;
	union {
		__u64 id;
		unsigned char bytes[sizeof(__u64)];
	} cg_id = { .id = id };
	int mnt_fd = -1, fd = -1;
	char link_buf[PATH_MAX];
	char *path = NULL;
	char fd_path[64];
	int link_len;
	char *mnt = NULL;

	if (!id) {
		fprintf(stderr, "Invalid cgroup2 ID\n");
		goto out;
	}

	mnt = find_cgroup2_mount(false);
	if (!mnt)
		goto out;

	mnt_fd = open(mnt, O_RDONLY);
	if (mnt_fd < 0) {
		fprintf(stderr, "Failed to open cgroup2 mount\n");
		goto out;
	}

	fhp->handle_bytes = sizeof(__u64);
	fhp->handle_type = FILEID_INO32_GEN;
	memcpy(fhp->f_handle, cg_id.bytes, sizeof(__u64));

	fd = open_by_handle_at(mnt_fd, fhp, 0);
	if (fd < 0) {
		fprintf(stderr, "Failed to open cgroup2 by ID\n");
		goto out;
	}

	snprintf(fd_path, sizeof(fd_path), "/proc/self/fd/%d", fd);
	link_len = readlink(fd_path, link_buf, sizeof(link_buf) - 1);
	if (link_len < 0) {
		fprintf(stderr,
			"Failed to read value of symbolic link %s\n",
			fd_path);
		goto out;
	}
	link_buf[link_len] = '\0';

	if (full)
		path = strdup(link_buf);
	else
		path = strdup(link_buf + strlen(mnt));
	if (!path)
		fprintf(stderr,
			"Failed to allocate memory for cgroup2 path\n");

out:
	if (fd >= 0)
		close(fd);
	if (mnt_fd >= 0)
		close(mnt_fd);
	free(mnt);

	return path;
}

int make_path(const char *path, mode_t mode)
{
	char *dir, *delim;
	int rc = -1;

	delim = dir = strdup(path);
	if (dir == NULL) {
		fprintf(stderr, "strdup failed copying path");
		return -1;
	}

	/* skip '/' -- it had better exist */
	if (*delim == '/')
		delim++;

	while (1) {
		delim = strchr(delim, '/');
		if (delim)
			*delim = '\0';

		rc = mkdir(dir, mode);
		if (rc && errno != EEXIST) {
			fprintf(stderr, "mkdir failed for %s: %s\n",
				dir, strerror(errno));
			goto out;
		}

		if (delim == NULL)
			break;

		*delim = '/';
		delim++;
		if (*delim == '\0')
			break;
	}
	rc = 0;
out:
	free(dir);

	return rc;
}

int get_command_name(const char *pid, char *comm, size_t len)
{
	char path[PATH_MAX];
	char line[128];
	FILE *fp;

	if (snprintf(path, sizeof(path),
		     "/proc/%s/status", pid) >= sizeof(path)) {
		return -1;
	}

	fp = fopen(path, "r");
	if (!fp)
		return -1;

	comm[0] = '\0';
	while (fgets(line, sizeof(line), fp)) {
		char *nl, *name;

		name = strstr(line, "Name:");
		if (!name)
			continue;

		name += 5;
		while (isspace(*name))
			name++;

		nl = strchr(name, '\n');
		if (nl)
			*nl = '\0';

		strlcpy(comm, name, len);
		break;
	}

	fclose(fp);

	return 0;
}

int get_task_name(pid_t pid, char *name, size_t len)
{
	char path[PATH_MAX];
	FILE *f;

	if (!pid)
		return -1;

	if (snprintf(path, sizeof(path), "/proc/%d/comm", pid) >= sizeof(path))
		return -1;

	f = fopen(path, "r");
	if (!f)
		return -1;

	if (!fgets(name, len, f))
		return -1;

	/* comm ends in \n, get rid of it */
	name[strcspn(name, "\n")] = '\0';

	fclose(f);

	return 0;
}