summaryrefslogtreecommitdiffstats
path: root/src/api.c
diff options
context:
space:
mode:
authorKen'ichi Ohmichi <oomichi@mxs.nes.nec.co.jp>2009-06-08 17:18:43 +0900
committerDhaval Giani <dhaval@linux.vnet.ibm.com>2009-06-08 14:33:25 +0530
commit5620d31b78fda7d5db0b85fcceca9c2cabe8f155 (patch)
treee603474e7427afabd610f07ac5c8e4512e6732f4 /src/api.c
parent340feae163c4797a6cb1247b3812c1ccdc52fa41 (diff)
downloadlibcg-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/api.c')
-rw-r--r--src/api.c55
1 files changed, 55 insertions, 0 deletions
diff --git a/src/api.c b/src/api.c
index 9a379d6..ccea89c 100644
--- a/src/api.c
+++ b/src/api.c
@@ -2473,3 +2473,58 @@ int cgroup_get_task_begin(char *cgroup, char *controller, void **handle,
return ret;
}
+
+/**
+ * Get process data (euid and egid) from /proc/<pid>/status file.
+ * @param pid: The process id
+ * @param euid: The uid of param pid
+ * @param egid: The gid of param pid
+ * @return 0 on success, > 0 on error.
+ */
+int cgroup_get_uid_gid_from_procfs(pid_t pid, uid_t *euid, gid_t *egid)
+{
+ FILE *f;
+ char path[FILENAME_MAX];
+ char buf[4092];
+ uid_t ruid, suid, fsuid;
+ gid_t rgid, sgid, fsgid;
+ bool found_euid = false;
+ bool found_egid = false;
+
+ sprintf(path, "/proc/%d/status", pid);
+ f = fopen(path, "r");
+ if (!f)
+ return ECGROUPNOTEXIST;
+
+ while (fgets(buf, sizeof(buf), f)) {
+ if (!strncmp(buf, "Uid:", 4)) {
+ if (sscanf((buf + 5), "%d%d%d%d", &ruid, euid,
+ &suid, &fsuid) != 4)
+ break;
+ cgroup_dbg("Scanned proc values are %d %d %d %d\n",
+ ruid, *euid, suid, fsuid);
+ found_euid = true;
+ } else if (!strncmp(buf, "Gid:", 4)) {
+ if (sscanf((buf + 5), "%d%d%d%d", &rgid, egid,
+ &sgid, &fsgid) != 4)
+ break;
+ cgroup_dbg("Scanned proc values are %d %d %d %d\n",
+ rgid, *egid, sgid, fsgid);
+ found_egid = true;
+ }
+ if (found_euid && found_egid)
+ break;
+ }
+ fclose(f);
+ if (!found_euid || !found_egid) {
+ /*
+ * This method doesn't match the file format of
+ * /proc/<pid>/status. The format has been changed
+ * and we should catch up the change.
+ */
+ cgroup_dbg("The invlid file format of /proc/%d/status.\n", pid);
+ return ECGFAIL;
+ }
+ return 0;
+}
+