diff options
author | Ken'ichi Ohmichi <oomichi@mxs.nes.nec.co.jp> | 2009-06-08 17:18:43 +0900 |
---|---|---|
committer | Dhaval Giani <dhaval@linux.vnet.ibm.com> | 2009-06-08 14:33:25 +0530 |
commit | 5620d31b78fda7d5db0b85fcceca9c2cabe8f155 (patch) | |
tree | e603474e7427afabd610f07ac5c8e4512e6732f4 /src/tools | |
parent | 340feae163c4797a6cb1247b3812c1ccdc52fa41 (diff) | |
download | libcg-5620d31b78fda7d5db0b85fcceca9c2cabe8f155.tar.gz libcg-5620d31b78fda7d5db0b85fcceca9c2cabe8f155.tar.xz libcg-5620d31b78fda7d5db0b85fcceca9c2cabe8f155.zip |
Cleanup: Integrate similar code to cgroup_get_uid_gid_from_procfs().
CHANGELOG of v2.1:
================
* Rebase the patch for commit '340feae163c4797a6cb1247b3812c1ccdc52fa41'.
There are some similar functions for getting process's data (uid, gid) from
/proc/<pid>/status file, so this patch integrates these functions into one
cgroup_get_uid_gid_from_procfs().
Signed-off-by: Ken'ichi Ohmichi <oomichi@mxs.nes.nec.co.jp>
Reviewed-By: Jan Safranek <jsafrane@redhat.com>
Signed-off-by: Dhaval Giani <dhaval@linux.vnet.ibm.com>
Diffstat (limited to 'src/tools')
-rw-r--r-- | src/tools/cgclassify.c | 87 |
1 files changed, 3 insertions, 84 deletions
diff --git a/src/tools/cgclassify.c b/src/tools/cgclassify.c index de10510..ceaca34 100644 --- a/src/tools/cgclassify.c +++ b/src/tools/cgclassify.c @@ -18,6 +18,7 @@ #include <string.h> #include <errno.h> #include <libcgroup.h> +#include <libcgroup-internal.h> #include <limits.h> #include <pwd.h> #include <unistd.h> @@ -31,80 +32,6 @@ #define TEMP_BUF 81 /* - * Go through /proc/<pid>/status file to determine the euid of the - * process. - * It returns 0 on success and negative values on failure. - */ - -int euid_of_pid(pid_t pid) -{ - FILE *fp; - char path[FILENAME_MAX]; - char buf[TEMP_BUF]; - uid_t ruid, euid, suid, fsuid; - - sprintf(path, "/proc/%d/status", pid); - fp = fopen(path, "r"); - if (!fp) { - cgroup_dbg("Error in opening file %s:%s\n", path, - strerror(errno)); - return -1; - } - - while (fgets(buf, TEMP_BUF, fp)) { - if (!strncmp(buf, "Uid:", 4)) { - sscanf((buf + 5), "%d%d%d%d", (int *)&ruid, - (int *)&euid, (int *)&suid, (int *)&fsuid); - cgroup_dbg("Scanned proc values are %d %d %d %d\n", - ruid, euid, suid, fsuid); - fclose(fp); - return euid; - } - } - fclose(fp); - - /* If we are here, we could not find euid. Return error. */ - return -1; -} - -/* - * Go through /proc/<pid>/status file to determine the egid of the - * process. - * It returns 0 on success and negative values on failure. - */ - -int egid_of_pid(pid_t pid) -{ - FILE *fp; - char path[FILENAME_MAX]; - char buf[TEMP_BUF]; - gid_t rgid, egid, sgid, fsgid; - - sprintf(path, "/proc/%d/status", pid); - fp = fopen(path, "r"); - if (!fp) { - cgroup_dbg("Error in opening file %s:%s\n", path, - strerror(errno)); - return -1; - } - - while (fgets(buf, TEMP_BUF, fp)) { - if (!strncmp(buf, "Gid:", 4)) { - sscanf((buf + 5), "%d%d%d%d", (int *)&rgid, - (int *)&egid, (int *)&sgid, (int *)&fsgid); - cgroup_dbg("Scanned proc values are %d %d %d %d\n", - rgid, egid, sgid, fsgid); - fclose(fp); - return egid; - } - } - fclose(fp); - - /* If we are here, we could not find egid. Return error. */ - return -1; -} - -/* * Change process group as specified on command line. */ int change_group_path(pid_t pid, struct cgroup_group_spec *cgroup_list[]) @@ -137,16 +64,8 @@ int change_group_uid_gid(pid_t pid) int ret; /* Put pid into right cgroup as per rules in /etc/cgrules.conf */ - euid = euid_of_pid(pid); - if (euid == -1) { - fprintf(stderr, "Error in determining euid of" - " pid %d\n", pid); - return -1; - } - - egid = egid_of_pid(pid); - if (egid == -1) { - fprintf(stderr, "Error in determining egid of" + if (cgroup_get_uid_gid_from_procfs(pid, &euid, &egid)) { + fprintf(stderr, "Error in determining euid/egid of" " pid %d\n", pid); return -1; } |