/*#############################################################################
# #
# Pakfire - The IPFire package management system #
-# Copyright (C) 2021 Pakfire development team #
+# Copyright (C) 2022 Pakfire development team #
# #
# 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 #
# #
#############################################################################*/
-#include <signal.h>
-#include <sys/types.h>
#include <unistd.h>
#include <pakfire/cgroup.h>
#include "../testsuite.h"
static int test_create_and_destroy(const struct test* t) {
- ASSERT_SUCCESS(
- pakfire_cgroup_create(t->pakfire, "pakfire/test")
- );
+ struct pakfire_cgroup* cgroup = NULL;
+ int r = EXIT_FAILURE;
- ASSERT_SUCCESS(
- pakfire_cgroup_destroy(t->pakfire, "pakfire/test")
- );
+ // Open a new cgroup
+ ASSERT_SUCCESS(pakfire_cgroup_open(&cgroup, t->pakfire, "pakfire-test", 0));
- return EXIT_SUCCESS;
+ // Destroy the cgroup again
+ ASSERT_SUCCESS(pakfire_cgroup_destroy(cgroup));
-FAIL:
- return EXIT_FAILURE;
-}
-
-static int test_attach(const struct test* t) {
- ssize_t num_processes;
-
- // Fetch the PID of the test process
- pid_t pid = getpid();
-
- LOG("This process's PID: %d\n", pid);
-
- ASSERT_SUCCESS(
- pakfire_cgroup_create(t->pakfire, "pakfire/test")
- );
-
- // Check that there are no processes in this group
- num_processes = pakfire_cgroup_num_processes(t->pakfire, "pakfire/test");
- ASSERT(num_processes == 0);
-
- ASSERT_SUCCESS(
- pakfire_cgroup_attach(t->pakfire, "pakfire/test", pid)
- );
-
- // Check that there is exactly one process in this group
- num_processes = pakfire_cgroup_num_processes(t->pakfire, "pakfire/test");
- ASSERT(num_processes == 1);
-
- ASSERT_SUCCESS(
- pakfire_cgroup_detach(t->pakfire, "pakfire/test", pid)
- );
-
- // Check that there are no processes in this group
- num_processes = pakfire_cgroup_num_processes(t->pakfire, "pakfire/test");
- ASSERT(num_processes == 0);
-
- ASSERT_SUCCESS(
- pakfire_cgroup_destroy(t->pakfire, "pakfire/test")
- );
-
- return EXIT_SUCCESS;
-
-FAIL:
- return EXIT_FAILURE;
-}
-
-static void handle_signal(int signum) {
- pid_t pid = getpid();
-
- LOG("Process %d received signal %d\n", pid, signum);
-}
-
-static int child_process(void) {
- LOG("Child process started with PID %d\n", getpid());
-
- signal(SIGTERM, handle_signal);
-
- // Do nothing
- while (1) {
- sleep(1);
-
- LOG("Child process is still alive\n");
- }
-
- return 0;
-}
-
-static pid_t fork_child_process(void) {
- pid_t pid = fork();
- ASSERT(pid >= 0);
-
- // Jump into child function
- if (pid == 0)
- return child_process();
-
- return pid;
-
-FAIL:
- abort();
-}
-
-static int test_killall(const struct test* t) {
- ASSERT_SUCCESS(
- pakfire_cgroup_create(t->pakfire, "pakfire/test")
- );
-
- // Launch a background we can kill :)
- pid_t pid = fork_child_process();
-
- // Attach child to cgroup
- ASSERT_SUCCESS(
- pakfire_cgroup_attach(t->pakfire, "pakfire/test", pid)
- );
-
- // Kill everything
- ASSERT_SUCCESS(
- pakfire_cgroup_killall(t->pakfire, "pakfire/test")
- );
-
- ASSERT_SUCCESS(
- pakfire_cgroup_destroy(t->pakfire, "pakfire/test")
- );
-
- return EXIT_SUCCESS;
+ // Success
+ r = EXIT_SUCCESS;
FAIL:
- return EXIT_FAILURE;
-}
-
-static int test_cpustat(const struct test* t) {
- struct pakfire_cgroup_cpustat st;
-
- ASSERT_SUCCESS(
- pakfire_cgroup_create(t->pakfire, "pakfire/test")
- );
-
- // Launch a background we can kill :)
- pid_t pid = fork_child_process();
-
- // Attach child to cgroup
- ASSERT_SUCCESS(
- pakfire_cgroup_attach(t->pakfire, "pakfire/test", pid)
- );
-
- // Kill everything
- ASSERT_SUCCESS(
- pakfire_cgroup_killall(t->pakfire, "pakfire/test")
- );
-
- ASSERT_SUCCESS(
- pakfire_cgroup_cpustat(t->pakfire, "pakfire/test", &st)
- );
-
- ASSERT(st.usage.tv_sec > 0 || st.usage.tv_usec > 0);
-
- ASSERT_SUCCESS(
- pakfire_cgroup_destroy(t->pakfire, "pakfire/test")
- );
-
- return EXIT_SUCCESS;
+ if (cgroup)
+ pakfire_cgroup_unref(cgroup);
-FAIL:
- return EXIT_FAILURE;
-}
-
-static int test_nice(const struct test* t) {
- ASSERT_SUCCESS(
- pakfire_cgroup_create(t->pakfire, "pakfire/test")
- );
-
- // Set nice level
- ASSERT_SUCCESS(
- pakfire_cgroup_set_nice(t->pakfire, "pakfire/test", 5)
- );
-
- ASSERT_SUCCESS(
- pakfire_cgroup_destroy(t->pakfire, "pakfire/test")
- );
-
- return EXIT_SUCCESS;
-
-FAIL:
- return EXIT_FAILURE;
-}
-
-static int test_random_name(const struct test* t) {
- char name1[] = "pakfire/execute-XXXXXX";
- char name2[] = "pakfire/execute-XXXXXX";
-
- ASSERT_SUCCESS(pakfire_cgroup_random_name(name1));
- ASSERT_SUCCESS(pakfire_cgroup_random_name(name2));
-
- // Make sure we got different results
- ASSERT_STRING_NOT_EQUALS(name1, name2);
-
- return EXIT_SUCCESS;
-
-FAIL:
- return EXIT_FAILURE;
+ return r;
}
int main(int argc, const char* argv[]) {
testsuite_add_test(test_create_and_destroy);
- testsuite_add_test(test_attach);
- testsuite_add_test(test_killall);
- testsuite_add_test(test_cpustat);
- testsuite_add_test(test_random_name);
- testsuite_add_test(test_nice);
return testsuite_run(argc, argv);
}