}
}
-/* 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[])
{
{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;
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);
#include <stdlib.h>
#include <sys/types.h>
#include <dirent.h>
+#include <pwd.h>
+#include <grp.h>
#include <libcgroup.h>
#include "tools-common.h"
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;
+}
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 */