Commit 5b5f1e16 by Christian Seiler Committed by Stéphane Graber

Factor out capability parsing logic

Currently, setup_caps and dropcaps_except both use the same parsing logic for parsing capabilities (try to identify by name, but allow numerical specification). Since this is a common routine, separate it out to improve maintainability and reuseability. Signed-off-by: 's avatarChristian Seiler <christian@iwakd.de> Signed-off-by: 's avatarSerge Hallyn <serge.hallyn@ubuntu.com>
parent 258eb399
...@@ -2160,22 +2160,14 @@ static int setup_mount_entries(const struct lxc_rootfs *rootfs, struct lxc_list ...@@ -2160,22 +2160,14 @@ static int setup_mount_entries(const struct lxc_rootfs *rootfs, struct lxc_list
return ret; return ret;
} }
static int setup_caps(struct lxc_list *caps) static int parse_cap(const char *cap)
{ {
struct lxc_list *iterator; char *ptr = NULL;
char *drop_entry; int i, capid = -1;
char *ptr;
int i, capid;
lxc_list_for_each(iterator, caps) {
drop_entry = iterator->elem;
capid = -1;
for (i = 0; i < sizeof(caps_opt)/sizeof(caps_opt[0]); i++) { for (i = 0; i < sizeof(caps_opt)/sizeof(caps_opt[0]); i++) {
if (strcmp(drop_entry, caps_opt[i].name)) if (strcmp(cap, caps_opt[i].name))
continue; continue;
capid = caps_opt[i].value; capid = caps_opt[i].value;
...@@ -2187,7 +2179,7 @@ static int setup_caps(struct lxc_list *caps) ...@@ -2187,7 +2179,7 @@ static int setup_caps(struct lxc_list *caps)
* capabilities that the running kernel knows about but * capabilities that the running kernel knows about but
* we don't */ * we don't */
errno = 0; errno = 0;
capid = strtol(drop_entry, &ptr, 10); capid = strtol(cap, &ptr, 10);
if (!ptr || *ptr != '\0' || errno != 0) if (!ptr || *ptr != '\0' || errno != 0)
/* not a valid number */ /* not a valid number */
capid = -1; capid = -1;
...@@ -2197,6 +2189,21 @@ static int setup_caps(struct lxc_list *caps) ...@@ -2197,6 +2189,21 @@ static int setup_caps(struct lxc_list *caps)
capid = -1; capid = -1;
} }
return capid;
}
static int setup_caps(struct lxc_list *caps)
{
struct lxc_list *iterator;
char *drop_entry;
int capid;
lxc_list_for_each(iterator, caps) {
drop_entry = iterator->elem;
capid = parse_cap(drop_entry);
if (capid < 0) { if (capid < 0) {
ERROR("unknown capability %s", drop_entry); ERROR("unknown capability %s", drop_entry);
return -1; return -1;
...@@ -2220,7 +2227,6 @@ static int dropcaps_except(struct lxc_list *caps) ...@@ -2220,7 +2227,6 @@ static int dropcaps_except(struct lxc_list *caps)
{ {
struct lxc_list *iterator; struct lxc_list *iterator;
char *keep_entry; char *keep_entry;
char *ptr;
int i, capid; int i, capid;
int numcaps = lxc_caps_last_cap() + 1; int numcaps = lxc_caps_last_cap() + 1;
INFO("found %d capabilities", numcaps); INFO("found %d capabilities", numcaps);
...@@ -2236,31 +2242,7 @@ static int dropcaps_except(struct lxc_list *caps) ...@@ -2236,31 +2242,7 @@ static int dropcaps_except(struct lxc_list *caps)
keep_entry = iterator->elem; keep_entry = iterator->elem;
capid = -1; capid = parse_cap(keep_entry);
for (i = 0; i < sizeof(caps_opt)/sizeof(caps_opt[0]); i++) {
if (strcmp(keep_entry, caps_opt[i].name))
continue;
capid = caps_opt[i].value;
break;
}
if (capid < 0) {
/* try to see if it's numeric, so the user may specify
* capabilities that the running kernel knows about but
* we don't */
capid = strtol(keep_entry, &ptr, 10);
if (!ptr || *ptr != '\0' ||
capid == INT_MIN || capid == INT_MAX)
/* not a valid number */
capid = -1;
else if (capid > lxc_caps_last_cap())
/* we have a number but it's not a valid
* capability */
capid = -1;
}
if (capid < 0) { if (capid < 0) {
ERROR("unknown capability %s", keep_entry); ERROR("unknown capability %s", keep_entry);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment