/* SPDX-License-Identifier: GPL-2.0 */
#include <stdbool.h>
#include <linux/limits.h>
#include <sys/ptrace.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <poll.h>
#include <stdlib.h>
#include <sys/inotify.h>
#include <string.h>
#include <sys/wait.h>
#include "../kselftest.h"
#include "cgroup_util.h"
#define DEBUG
#ifdef DEBUG
#define debug(args...) fprintf(stderr, args)
#else
#define debug(args...)
#endif
/*
* Check if the cgroup is frozen by looking at the cgroup.events::frozen value.
*/
static int cg_check_frozen(const char *cgroup, bool frozen)
{
if (frozen) {
if (cg_read_strstr(cgroup, "cgroup.events", "frozen 1") != 0) {
debug("Cgroup %s isn't frozen\n", cgroup);
return -1;
}
} else {
/*
* Check the cgroup.events::frozen value.
*/
if (cg_read_strstr(cgroup, "cgroup.events", "frozen 0") != 0) {
debug("Cgroup %s is frozen\n", cgroup);
return -1;
}
}
return 0;
}
/*
* Freeze the given cgroup.
*/
static int cg_freeze_nowait(const char *cgroup, bool freeze)
{
return cg_write(cgroup, "cgroup.freeze", freeze ? "1" : "0");
}
/*
* Prepare for waiting on cgroup.events file.
*/
static int cg_prepare_for_wait(const char *cgroup)
{
int fd, ret = -1;
fd = inotify_init1(0);
if (fd == -1) {
debug("Error: inotify_init1() failed\n");
return fd;
}
ret = inotify_add_watch(fd, cg_control(cgroup, "cgroup.events"),
IN_MODIFY);
if (ret == -1) {
debug("Error: inotify_add_watch() failed\n");
close(fd);
fd = -1;
}
return fd;
}
/*
* Wait for an event. If there are no events for 10 seconds,
* treat this an error.
*/
static int cg_wait_for(int fd)
{
int ret = -1;
struct pollfd fds = {
.fd = fd,
.events = POLLIN,
};
while (true) {
ret = poll(&fds, 1, 10000);
if (ret == -1) {
if (errno == EINTR)
continue;
debug("Error: poll() failed\n");
break;
}
if (ret > 0 && fds.revents & POLLIN) {
ret = 0;
break;
}
}
return ret;
}
/*
* Attach a task to the given cgroup and wait for a cgroup frozen event.
* All transient events (e.g. populated) are ignored.
*/
static int cg_enter_and_wait_for_frozen(const char *cgroup, int pid,
bool frozen)
{
int fd, ret = -1;
int attempts;
fd = cg_prepare_for_wait(cgroup);
if (fd < 0)
return fd;
ret = cg_enter(cgroup, pid);
if (ret)
goto out;
for (attempts = 0; attempts < 10; attempts++) {
ret = cg_wait_for(fd);
if (ret)
break;
ret = cg_check_frozen(cgroup, frozen);
if (ret)
continue;
}
out:
close(fd);
return ret;
}
/*
* Freeze the given cgroup and wait for the inotify signal.
* If there are no events in 10 seconds, treat this as an error.
* Then check that the cgroup is in the desired state.
*/
static int cg_freeze_wait(const char *cgroup, bool freeze)
{
int fd, ret = -1;
fd = cg_prepare_for_wait(cgroup);
if (fd < 0)
return fd;
ret = cg_freeze_nowait(cgroup, freeze);
if (ret) {
debug("Error: cg_freeze_nowait() failed\n");
goto out;
}
ret = cg_wait_for(fd);
if (ret)
goto out;
ret = cg_check_frozen(cgroup, freeze);
out:
close(fd);
return ret;
}
/*
* A simple process running in a sleep loop until being
* re-parented.
*/
static int child_fn(const char *cgroup, void *arg)
{
int ppid = getppid();
while (getppid() == ppid)
usleep(1000);
return getppid() == ppid;
}
/*
* A simple test for the cgroup freezer: populated the cgroup with 100
* running processes and freeze it. Then unfreeze it. Then it kills all
* processes and destroys the cgroup.
*/
static int test_cgfreezer_simple(const char *root)
{
int ret = KSFT_FAIL;
char *cgroup = NULL;
int i;
cgroup = cg_name(root, "cg_test_simple");
if (!cgroup)
goto cleanup;
if (cg_create(cgroup