Skip to content

Commit

Permalink
lxc-user-nic: keep lines from other {users,links}
Browse files Browse the repository at this point in the history
Assume the db contained the following entries:

    chb veth lxcbr0 veth1
    chb veth lxcbr0 veth2
    chb veth lxdbr0 veth3
    chb veth lxdbr0 veth2
    didi veth lxcbr0 veth4

And you request

    cull_entries("chb", "veth", "lxdbr0", "veth3");

lxc-user-nic would wipe any entries that did not match irrespective of whether
they existed or not. Let's fix that.

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
  • Loading branch information
Christian Brauner committed Sep 2, 2017
1 parent a92028b commit 3231134
Showing 1 changed file with 18 additions and 15 deletions.
33 changes: 18 additions & 15 deletions src/lxc/lxc_user_nic.c
Expand Up @@ -370,27 +370,24 @@ static char *find_line(char *p, char *e, char *u, char *t, char *l)
p++;

p2 = get_eow(p, e);
if (!p2 || ((size_t)(p2 - p)) != strlen(u) ||
strncmp(p, u, strlen(u)))
goto next;
if (!p2 || ((size_t)(p2 - p)) != strlen(u) || strncmp(p, u, strlen(u)))
return ret;

p = p2 + 1;
while ((p < e) && isblank(*p))
p++;

p2 = get_eow(p, e);
if (!p2 || ((size_t)(p2 - p)) != strlen(t) ||
strncmp(p, t, strlen(t)))
goto next;
if (!p2 || ((size_t)(p2 - p)) != strlen(t) || strncmp(p, t, strlen(t)))
return ret;

p = p2 + 1;
while ((p < e) && isblank(*p))
p++;

p2 = get_eow(p, e);
if (!p2 || ((size_t)(p2 - p)) != strlen(l) ||
strncmp(p, l, strlen(l)))
goto next;
if (!p2 || ((size_t)(p2 - p)) != strlen(l) || strncmp(p, l, strlen(l)))
return ret;

return ret;
next:
Expand All @@ -410,7 +407,7 @@ static bool nic_exists(char *nic)
return true;

ret = snprintf(path, MAXPATHLEN, "/sys/class/net/%s", nic);
if (ret < 0 || ret >= MAXPATHLEN)
if (ret < 0 || (size_t)ret >= MAXPATHLEN)
return false;

ret = stat(path, &sb);
Expand Down Expand Up @@ -562,8 +559,10 @@ static bool get_nic_from_line(char *p, char **nic)

ret = sscanf(p, "%99[^ \t\n] %99[^ \t\n] %99[^ \t\n] %99[^ \t\n]", user,
type, br, *nic);
if (ret != 4)
if (ret != 4) {
*nic[0] = '\0';
return false;
}

return true;
}
Expand Down Expand Up @@ -609,6 +608,7 @@ static bool cull_entries(int fd, char *me, char *t, char *br, char *nicname,
e = buf + len;
while ((p = find_line(p, e, me, t, br))) {
struct entry_line *newe;
bool exists = false;

newe = realloc(entry_lines, sizeof(*entry_lines) * (n + 1));
if (!newe) {
Expand All @@ -624,10 +624,13 @@ static bool cull_entries(int fd, char *me, char *t, char *br, char *nicname,
if (!get_nic_from_line(p, &nic))
continue;

if (nic && !nic_exists(nic))
if (nic[0] != '\0')
exists = nic_exists(nic);

if (!exists)
entry_lines[n - 1].keep = false;

if (nicname)
if (exists && nicname)
if (!strcmp(nic, nicname))
*found_nicname = true;

Expand Down Expand Up @@ -1206,8 +1209,8 @@ int main(int argc, char *argv[])
free_alloted(&alloted);

if (!found_nicname) {
usernic_error("%s", "Caller is not allowed to delete "
"network device\n");
usernic_error("Caller is not allowed to delete network "
"device \"%s\"\n", args.veth_name);
exit(EXIT_FAILURE);
}

Expand Down

0 comments on commit 3231134

Please sign in to comment.