summaryrefslogtreecommitdiffstats
path: root/utils/cgroup.c
blob: 1c8b07cc73fd910e717904f1d43e12e158c0d40c (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
/*
 * Copyright (C) 2026 Daniel Golle <daniel@makrotopia.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 2.1
 * as published by the Free Software Foundation
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 */

#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include <libubox/utils.h>

#include "cgroup.h"

int cgroup_join(const char *path)
{
	char procs[256], dir[256];
	struct stat sb;
	int fd, ret;

	if (stat("/sys/fs/cgroup/cgroup.subtree_control", &sb))
		return 0;

	ret = snprintf(procs, sizeof(procs), "%s/cgroup.procs", path);
	if (ret < 0 || (size_t)ret >= sizeof(procs))
		return -ENAMETOOLONG;

	snprintf(dir, sizeof(dir), "%s", path);
	if (mkdir_p(dir, 0700))
		return -EPERM;

	fd = open(procs, O_WRONLY);
	if (fd == -1)
		return -EIO;

	ret = dprintf(fd, "%d", getpid());
	close(fd);
	if (ret < 0)
		return -EIO;

	return 0;
}

int cgroup_kill(const char *path)
{
	char kill_path[256];
	int fd, ret;

	ret = snprintf(kill_path, sizeof(kill_path), "%s/cgroup.kill", path);
	if (ret < 0 || (size_t)ret >= sizeof(kill_path))
		return -ENAMETOOLONG;

	fd = open(kill_path, O_WRONLY);
	if (fd < 0)
		return 0;

	ret = (write(fd, "1", 1) < 0) ? -errno : 0;
	close(fd);

	return ret;
}

bool cgroup_populated(int fd)
{
	char buf[128];
	ssize_t len;
	char *val;

	if (lseek(fd, 0, SEEK_SET) < 0)
		return false;

	len = read(fd, buf, sizeof(buf) - 1);
	if (len <= 0)
		return false;

	buf[len] = '\0';
	val = strstr(buf, "populated ");
	if (!val)
		return false;

	return val[strlen("populated ")] == '1';
}