# #
#############################################################################*/
+#include <dirent.h>
#include <errno.h>
#include <linux/limits.h>
#include <linux/magic.h>
return 0;
}
+DIR* pakfire_cgroup_opendir(Pakfire pakfire, const char* group) {
+ // Make path
+ char path[PATH_MAX];
+ int r = pakfire_cgroup_make_path(pakfire, path, sizeof(path) - 1, group, NULL);
+ if (r < 0)
+ return NULL;
+
+ return opendir(path);
+}
+
int pakfire_cgroup_attach(Pakfire pakfire, const char* group, pid_t pid) {
int r = pakfire_cgroup_fprintf(pakfire, group, "cgroup.procs", "%d", pid);
if (r < 0) {
if (bytes_read < 0)
break;
- printf("%s", line);
-
for (const struct keyword* keyword = keywords; keyword->keyword; keyword++) {
if (pakfire_string_startswith(line, keyword->keyword)) {
const char* p = line + strlen(keyword->keyword) + 1;
# #
#############################################################################*/
+#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <linux/limits.h>
+#include <linux/sched.h>
#include <sched.h>
#include <stdlib.h>
#include <string.h>
#define __NR_clone3 435
#endif
-struct clone_args {
- uint64_t flags;
- uint64_t pidfd;
- uint64_t child_tid;
- uint64_t parent_tid;
- uint64_t exit_signal;
- uint64_t stack;
- uint64_t stack_size;
- uint64_t tls;
- uint64_t set_tid;
- uint64_t set_tid_size;
- uint64_t cgroup;
-};
-
static int pakfire_execute_buffer_is_full(const struct pakfire_execute_buffer* buffer) {
return (sizeof(buffer->data) == buffer->used);
}
PAKFIRE_EXPORT int pakfire_execute(Pakfire pakfire, const char* argv[], char* envp[],
int flags, pakfire_execute_logging_callback logging_callback, void* data) {
+ DIR* cgroupdir = NULL;
+
struct pakfire_execute env = {
.pakfire = pakfire,
.argv = argv,
if (r)
goto ERROR;
+ // Launch the new process straight into their cgroup
+ cgroupdir = pakfire_cgroup_opendir(pakfire, env.cgroup);
+ if (!cgroupdir) {
+ ERROR(pakfire, "Could not open cgroup %s: %s\n", env.cgroup, strerror(errno));
+ goto ERROR;
+ }
+
+ args.flags |= CLONE_INTO_CGROUP;
+ args.cgroup = dirfd(cgroupdir);
+
// Fork this process
pid_t pid = syscall(__NR_clone3, &args, sizeof(args));
if (pid < 0) {
exit(r);
}
- // Attach the process to the cgroup
- r = pakfire_cgroup_attach(pakfire, env.cgroup, pid);
- if (r)
- goto ERROR;
-
// Set some useful error code
int exit = -ESRCH;
int status = 0;
r = exit;
ERROR:
+ // Close the cgroup
+ if (cgroupdir)
+ closedir(cgroupdir);
+
// Destroy the cgroup
pakfire_cgroup_destroy(pakfire, env.cgroup);
#ifdef PAKFIRE_PRIVATE
+#include <dirent.h>
#include <sys/types.h>
#include <time.h>
int pakfire_cgroup_create(Pakfire pakfire, const char* group);
int pakfire_cgroup_destroy(Pakfire pakfire, const char* group);
+DIR* pakfire_cgroup_opendir(Pakfire pakfire, const char* group);
+
int pakfire_cgroup_attach(Pakfire pakfire, const char* group, pid_t pid);
int pakfire_cgroup_detach(Pakfire pakfire, const char* group, pid_t pid);