The parse_mode() function will be used by cgconfigparser, so move it there.
In addition, the cgconfigparser will need uid:gid parsing routines from
cgcreate.c, so move it to common code as well.

Signed-off-by: Jan Safranek <jsafr...@redhat.com>
Acked-by: Dhaval Giani <dhaval.gi...@gmail.com>
---

 src/tools/cgcreate.c     |  109 ++--------------------------------------------
 src/tools/tools-common.c |   74 +++++++++++++++++++++++++++++++
 src/tools/tools-common.h |   18 ++++++++
 3 files changed, 96 insertions(+), 105 deletions(-)

diff --git a/src/tools/cgcreate.c b/src/tools/cgcreate.c
index d470b94..2579072 100644
--- a/src/tools/cgcreate.c
+++ b/src/tools/cgcreate.c
@@ -57,38 +57,6 @@ static void usage(int status, const char *program_name)
        }
 }
 
-/* allowed mode strings are octal version: "755" */
-
-int parse_mode(char *string, mode_t *pmode, const char *program_name)
-{
-       mode_t mode = 0;
-       int pos = 0; /* position of the number iin string */
-       int i;
-       int j = 64;
-
-       while (pos < 3) {
-               if ('0' <= string[pos] && string[pos] < '8') {
-                       i = (int)string[pos] - (int)'0';
-                       /* parse the permission triple*/
-                       mode = mode + i*j;
-                       j = j / 8;
-               } else {
-                       fprintf(stdout, "%s wrong mode format %s",
-                               program_name, string);
-                       return -1;
-               }
-               pos++;
-       }
-
-       /* the string have contains three characters */
-       if (string[pos] != '\0') {
-               fprintf(stdout, "%s wrong mode format %s",
-                       program_name, string);
-               return -1;
-       }
-       *pmode = mode;
-       return 0;
-}
 
 int main(int argc, char *argv[])
 {
@@ -106,14 +74,6 @@ int main(int argc, char *argv[])
                {0, 0, 0, 0},
        };
 
-       /* Structure to get GID from group name */
-       struct group *grp = NULL;
-       char *grp_string = NULL;
-
-       /* Structure to get UID from user name */
-       struct passwd *pwd = NULL;
-       char *pwd_string = NULL;
-
        uid_t tuid = CGRULE_INVALID, auid = CGRULE_INVALID;
        gid_t tgid = CGRULE_INVALID, agid = CGRULE_INVALID;
 
@@ -152,74 +112,13 @@ int main(int argc, char *argv[])
                        goto err;
                case 'a':
                        /* set admin uid/gid */
-                       if (optarg[0] == ':')
-                               grp_string = strtok(optarg, ":");
-                       else {
-                               pwd_string = strtok(optarg, ":");
-                               if (pwd_string != NULL)
-                                       grp_string = strtok(NULL, ":");
-                       }
-
-                       if (pwd_string != NULL) {
-                               pwd = getpwnam(pwd_string);
-                               if (pwd != NULL) {
-                                       auid = pwd->pw_uid;
-                               } else {
-                                       fprintf(stderr, "%s: "
-                                               "can't find uid of user %s.\n",
-                                               argv[0], pwd_string);
-                                       ret = -1;
-                                       goto err;
-                               }
-                       }
-                       if (grp_string != NULL) {
-                               grp = getgrnam(grp_string);
-                               if (grp != NULL)
-                                       agid = grp->gr_gid;
-                               else {
-                                       fprintf(stderr, "%s: "
-                                               "can't find gid of group %s.\n",
-                                               argv[0], grp_string);
-                                       ret = -1;
-                                       goto err;
-                               }
-                       }
-
+                       if (parse_uid_gid(optarg, &auid, &agid, argv[0]))
+                               goto err;
                        break;
                case 't':
                        /* set task uid/gid */
-                       if (optarg[0] == ':')
-                               grp_string = strtok(optarg, ":");
-                       else {
-                               pwd_string = strtok(optarg, ":");
-                               if (pwd_string != NULL)
-                                       grp_string = strtok(NULL, ":");
-                       }
-
-                       if (pwd_string != NULL) {
-                               pwd = getpwnam(pwd_string);
-                               if (pwd != NULL) {
-                                       tuid = pwd->pw_uid;
-                               } else {
-                                       fprintf(stderr, "%s: "
-                                               "can't find uid of user %s.\n",
-                                               argv[0], pwd_string);
-                                       ret = -1;
-                                       goto err;
-                               }
-                       }
-                       if (grp_string != NULL) {
-                               grp = getgrnam(grp_string);
-                               if (grp != NULL)
-                                       tgid = grp->gr_gid;
-                               else {
-                                       fprintf(stderr, "%s: "
-                                               "can't find gid of group %s.\n",
-                                               argv[0], grp_string);
-                                       ret = -1;
-                                       goto err;
-                               }
-                       }
+                       if (parse_uid_gid(optarg, &tuid, &tgid, argv[0]))
+                               goto err;
                        break;
                case 'g':
                        ret = parse_cgroup_spec(cgroup_list, optarg, capacity);
diff --git a/src/tools/tools-common.c b/src/tools/tools-common.c
index 729b00c..8a739ce 100644
--- a/src/tools/tools-common.c
+++ b/src/tools/tools-common.c
@@ -23,6 +23,8 @@
 #include <stdlib.h>
 #include <sys/types.h>
 #include <dirent.h>
+#include <pwd.h>
+#include <grp.h>
 
 #include <libcgroup.h>
 #include "tools-common.h"
@@ -233,3 +235,75 @@ int cgroup_string_list_add_directory(struct 
cgroup_string_list *list,
        return 0;
 }
 
+
+/* allowed mode strings are octal version: "755" */
+int parse_mode(char *string, mode_t *pmode, const char *program_name)
+{
+       mode_t mode = 0;
+       int pos = 0; /* position of the number iin string */
+       int i;
+       int j = 64;
+
+       while (pos < 3) {
+               if ('0' <= string[pos] && string[pos] < '8') {
+                       i = (int)string[pos] - (int)'0';
+                       /* parse the permission triple*/
+                       mode = mode + i*j;
+                       j = j / 8;
+               } else {
+                       fprintf(stdout, "%s wrong mode format %s",
+                               program_name, string);
+                       return -1;
+               }
+               pos++;
+       }
+
+       /* the string have contains three characters */
+       if (string[pos] != '\0') {
+               fprintf(stdout, "%s wrong mode format %s",
+                       program_name, string);
+               return -1;
+       }
+       *pmode = mode;
+       return 0;
+}
+
+int parse_uid_gid(char *string, uid_t *uid, gid_t *gid,
+               const char *program_name)
+{
+       char *grp_string, *pwd_string;
+       struct passwd *pwd;
+       struct group *grp;
+
+       *uid = *gid = NO_UID_GID;
+
+       if (string[0] == ':')
+               grp_string = strtok(string, ":");
+       else {
+               pwd_string = strtok(string, ":");
+               if (pwd_string != NULL)
+                       grp_string = strtok(NULL, ":");
+       }
+
+       if (pwd_string != NULL) {
+               pwd = getpwnam(pwd_string);
+               if (pwd != NULL) {
+                       *uid = pwd->pw_uid;
+               } else {
+                       fprintf(stderr, "%s: can't find uid of user %s.\n",
+                                       program_name, pwd_string);
+                       return -1;
+               }
+       }
+       if (grp_string != NULL) {
+               grp = getgrnam(grp_string);
+               if (grp != NULL)
+                       *gid = grp->gr_gid;
+               else {
+                       fprintf(stderr, "%s: can't find gid of group %s.\n",
+                               program_name, grp_string);
+                       return -1;
+               }
+       }
+       return 0;
+}
diff --git a/src/tools/tools-common.h b/src/tools/tools-common.h
index 199444d..23bd35c 100644
--- a/src/tools/tools-common.h
+++ b/src/tools/tools-common.h
@@ -105,4 +105,22 @@ int cgroup_string_list_add_directory(struct 
cgroup_string_list *list,
                char *dirname, char *program_name);
 
 
+/**
+ * Parse file permissions as octal number.
+ * @param string A string to parse, must contain 3-4 characters '0'-'7'.
+ * @param pmode Parsed mode.
+ * @oaram program_name Argv[0] to show error messages.
+ */
+int parse_mode(char *string, mode_t *pmode, const char *program_name);
+
+/**
+ * Parse UID and GID from string in form "user:group".
+ * @param string A string to parse.
+ * @param uid Parsed UID (-1 if 'user' is missing in the string).
+ * @param gid Parsed GID (-1 if 'group' is missing in the string).
+ * @param program_name Argv[0] to show error messages.
+ */
+int parse_uid_gid(char *string, uid_t *uid, gid_t *gid,
+               const char *program_name);
+
 #endif /* TOOLS_COMMON */


------------------------------------------------------------------------------
All the data continuously generated in your IT infrastructure 
contains a definitive record of customers, application performance, 
security threats, fraudulent activity, and more. Splunk takes this 
data and makes sense of it. IT sense. And common sense.
http://p.sf.net/sfu/splunk-novd2d
_______________________________________________
Libcg-devel mailing list
Libcg-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/libcg-devel

Reply via email to