]> git.ipfire.org Git - thirdparty/libcgroup.git/commitdiff
libcg: scan running tasks at start time to classify existing pids
authorJohn Fastabend <john.fastabend@gmail.com>
Wed, 20 Feb 2013 17:08:19 +0000 (09:08 -0800)
committerJan Safranek <jsafrane@redhat.com>
Mon, 25 Feb 2013 13:58:06 +0000 (14:58 +0100)
Add routine to scan rules.conf file and move matching running tasks
in /proc/pid/* into configured control groups. Then at init time
we can move running tasks into the correct control group.

Expose this routine via libcg so other applications can use it
to classify existing applications after creating control groups.

CC: Jan Safranek <jsafrane@redhat.com>
Signed-off-by: John Fastabend <john.r.fastabend@intel.com>
Signed-off-by: Jan Safranek <jsafrane@redhat.com>
include/libcgroup/tasks.h
src/api.c
src/daemon/cgrulesengd.c
src/libcgroup.map

index 0f79220ff5431983ec58eb9611c78fe11c0ed199..aad438a22e07bfea960c6907048c867778d55b07 100644 (file)
@@ -120,6 +120,17 @@ void cgroup_print_rules_config(FILE *fp);
  * following functions.
  */
 
+/**
+ * Changes the cgroup of all running PIDs based on the rules in the config
+ * file. If a rules exists for a PID, then the PID is placed in the correct
+ * group.
+ *
+ * This function may be called after creating new control groups to move
+ * running PIDs into the newly created control groups.
+ *     @return 0 on success, < 0 on error
+ */
+int cgroup_change_all_cgroups(void);
+
 /**
  * Changes the cgroup of a program based on the rules in the config file.
  * If a rule exists for the given UID, GID or PROCESS NAME, then the given
index 11cd1b45651d94c7d0475cb26885916d29c012e0..2851efd0c38c2682132d1a485fa38d1e52e13870 100644 (file)
--- a/src/api.c
+++ b/src/api.c
@@ -3055,6 +3055,52 @@ int cgroup_change_cgroup_path(const char *dest, pid_t pid,
        return ret;
 }
 
+/**
+ * Changes the cgroup of all running PIDs based on the rules in the config
+ * file. If a rules exists for a PID, then the PID is placed in the correct
+ * group.
+ *
+ * This function may be called after creating new control groups to move
+ * running PIDs into the newly created control groups.
+ *     @return 0 on success, < 0 on error
+ */
+int cgroup_change_all_cgroups(void)
+{
+       DIR *dir;
+       struct dirent *pid_dir = NULL;
+       char *path = "/proc/";
+
+       dir = opendir(path);
+       if (!dir)
+               return -ECGOTHER;
+
+       while ((pid_dir = readdir(dir)) != NULL) {
+               int err, pid;
+               uid_t euid;
+               gid_t egid;
+               char *procname = NULL;
+
+               err = sscanf(pid_dir->d_name, "%i", &pid);
+               if (err < 1)
+                       continue;
+
+               err = cgroup_get_uid_gid_from_procfs(pid, &euid, &egid);
+               if (err)
+                       continue;
+
+               err = cgroup_get_procname_from_procfs(pid, &procname);
+               if (err)
+                       continue;
+
+               err = cgroup_change_cgroup_flags(euid, egid, procname, pid, 0);
+               if (err)
+                       cgroup_dbg("cgroup change pid %i failed\n", pid);
+       }
+
+       closedir(dir);
+       return 0;
+}
+
 /**
  * Print the cached rules table.  This function should be called only after
  * first calling cgroup_parse_config(), but it will work with an empty rule
index f12db4546264fdc70af247f89dd1d9e82f968cdd..42feaccf1ab77c7f31c3e5fd66a612544395f151 100644 (file)
@@ -1171,6 +1171,11 @@ int main(int argc, char *argv[])
        if (logfile && loglevel >= LOG_INFO)
                cgroup_print_rules_config(logfile);
 
+       /* Scan for running applications with rules */
+       ret = cgroup_change_all_cgroups();
+       if (ret)
+               flog(LOG_WARNING, "Failed to initialize running tasks.");
+
        flog(LOG_NOTICE, "Started the CGroup Rules Engine Daemon.");
 
        /* We loop endlesly in this function, unless we encounter an error. */
index b550a58ff5ea9fc23dd9ffc204b8e4385177675e..7dce2dfcf28dbbbbb4803cefc8f4615986d2574d 100644 (file)
@@ -110,4 +110,5 @@ CGROUP_0.39 {
        cgroup_reload_cached_templates;
        cgroup_init_templates_cache;
        cgroup_config_create_template_group;
+       cgroup_change_all_cgroups;
 } CGROUP_0.38;