• source navigation  • diff markup  • identifier search  • freetext search  • 

Sources/procd/utils/cgroup.c

  1 /*
  2  * Copyright (C) 2026 Daniel Golle <daniel@makrotopia.org>
  3  *
  4  * This program is free software; you can redistribute it and/or modify
  5  * it under the terms of the GNU Lesser General Public License version 2.1
  6  * as published by the Free Software Foundation
  7  *
  8  * This program is distributed in the hope that it will be useful,
  9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 11  * GNU General Public License for more details.
 12  */
 13 
 14 #include <sys/stat.h>
 15 #include <errno.h>
 16 #include <fcntl.h>
 17 #include <stdio.h>
 18 #include <string.h>
 19 #include <unistd.h>
 20 
 21 #include <libubox/utils.h>
 22 
 23 #include "cgroup.h"
 24 
 25 int cgroup_join(const char *path)
 26 {
 27         char procs[256], dir[256];
 28         struct stat sb;
 29         int fd, ret;
 30 
 31         if (stat("/sys/fs/cgroup/cgroup.subtree_control", &sb))
 32                 return 0;
 33 
 34         ret = snprintf(procs, sizeof(procs), "%s/cgroup.procs", path);
 35         if (ret < 0 || (size_t)ret >= sizeof(procs))
 36                 return -ENAMETOOLONG;
 37 
 38         snprintf(dir, sizeof(dir), "%s", path);
 39         if (mkdir_p(dir, 0700))
 40                 return -EPERM;
 41 
 42         fd = open(procs, O_WRONLY);
 43         if (fd == -1)
 44                 return -EIO;
 45 
 46         ret = dprintf(fd, "%d", getpid());
 47         close(fd);
 48         if (ret < 0)
 49                 return -EIO;
 50 
 51         return 0;
 52 }
 53 
 54 int cgroup_kill(const char *path)
 55 {
 56         char kill_path[256];
 57         int fd, ret;
 58 
 59         ret = snprintf(kill_path, sizeof(kill_path), "%s/cgroup.kill", path);
 60         if (ret < 0 || (size_t)ret >= sizeof(kill_path))
 61                 return -ENAMETOOLONG;
 62 
 63         fd = open(kill_path, O_WRONLY);
 64         if (fd < 0)
 65                 return 0;
 66 
 67         ret = (write(fd, "1", 1) < 0) ? -errno : 0;
 68         close(fd);
 69 
 70         return ret;
 71 }
 72 
 73 bool cgroup_populated(int fd)
 74 {
 75         char buf[128];
 76         ssize_t len;
 77         char *val;
 78 
 79         if (lseek(fd, 0, SEEK_SET) < 0)
 80                 return false;
 81 
 82         len = read(fd, buf, sizeof(buf) - 1);
 83         if (len <= 0)
 84                 return false;
 85 
 86         buf[len] = '\0';
 87         val = strstr(buf, "populated ");
 88         if (!val)
 89                 return false;
 90 
 91         return val[strlen("populated ")] == '1';
 92 }
 93 

This page was automatically generated by LXR 0.3.1.  •  OpenWrt