lxc-user-nic: keep lines from other {users,links}

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: 's avatarChristian Brauner <christian.brauner@ubuntu.com>
parent a92028b2
...@@ -370,27 +370,24 @@ static char *find_line(char *p, char *e, char *u, char *t, char *l) ...@@ -370,27 +370,24 @@ static char *find_line(char *p, char *e, char *u, char *t, char *l)
p++; p++;
p2 = get_eow(p, e); p2 = get_eow(p, e);
if (!p2 || ((size_t)(p2 - p)) != strlen(u) || if (!p2 || ((size_t)(p2 - p)) != strlen(u) || strncmp(p, u, strlen(u)))
strncmp(p, u, strlen(u))) return ret;
goto next;
p = p2 + 1; p = p2 + 1;
while ((p < e) && isblank(*p)) while ((p < e) && isblank(*p))
p++; p++;
p2 = get_eow(p, e); p2 = get_eow(p, e);
if (!p2 || ((size_t)(p2 - p)) != strlen(t) || if (!p2 || ((size_t)(p2 - p)) != strlen(t) || strncmp(p, t, strlen(t)))
strncmp(p, t, strlen(t))) return ret;
goto next;
p = p2 + 1; p = p2 + 1;
while ((p < e) && isblank(*p)) while ((p < e) && isblank(*p))
p++; p++;
p2 = get_eow(p, e); p2 = get_eow(p, e);
if (!p2 || ((size_t)(p2 - p)) != strlen(l) || if (!p2 || ((size_t)(p2 - p)) != strlen(l) || strncmp(p, l, strlen(l)))
strncmp(p, l, strlen(l))) return ret;
goto next;
return ret; return ret;
next: next:
...@@ -410,7 +407,7 @@ static bool nic_exists(char *nic) ...@@ -410,7 +407,7 @@ static bool nic_exists(char *nic)
return true; return true;
ret = snprintf(path, MAXPATHLEN, "/sys/class/net/%s", nic); ret = snprintf(path, MAXPATHLEN, "/sys/class/net/%s", nic);
if (ret < 0 || ret >= MAXPATHLEN) if (ret < 0 || (size_t)ret >= MAXPATHLEN)
return false; return false;
ret = stat(path, &sb); ret = stat(path, &sb);
...@@ -562,8 +559,10 @@ static bool get_nic_from_line(char *p, char **nic) ...@@ -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, ret = sscanf(p, "%99[^ \t\n] %99[^ \t\n] %99[^ \t\n] %99[^ \t\n]", user,
type, br, *nic); type, br, *nic);
if (ret != 4) if (ret != 4) {
*nic[0] = '\0';
return false; return false;
}
return true; return true;
} }
...@@ -609,6 +608,7 @@ static bool cull_entries(int fd, char *me, char *t, char *br, char *nicname, ...@@ -609,6 +608,7 @@ static bool cull_entries(int fd, char *me, char *t, char *br, char *nicname,
e = buf + len; e = buf + len;
while ((p = find_line(p, e, me, t, br))) { while ((p = find_line(p, e, me, t, br))) {
struct entry_line *newe; struct entry_line *newe;
bool exists = false;
newe = realloc(entry_lines, sizeof(*entry_lines) * (n + 1)); newe = realloc(entry_lines, sizeof(*entry_lines) * (n + 1));
if (!newe) { if (!newe) {
...@@ -624,10 +624,13 @@ static bool cull_entries(int fd, char *me, char *t, char *br, char *nicname, ...@@ -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)) if (!get_nic_from_line(p, &nic))
continue; continue;
if (nic && !nic_exists(nic)) if (nic[0] != '\0')
exists = nic_exists(nic);
if (!exists)
entry_lines[n - 1].keep = false; entry_lines[n - 1].keep = false;
if (nicname) if (exists && nicname)
if (!strcmp(nic, nicname)) if (!strcmp(nic, nicname))
*found_nicname = true; *found_nicname = true;
...@@ -1206,8 +1209,8 @@ int main(int argc, char *argv[]) ...@@ -1206,8 +1209,8 @@ int main(int argc, char *argv[])
free_alloted(&alloted); free_alloted(&alloted);
if (!found_nicname) { if (!found_nicname) {
usernic_error("%s", "Caller is not allowed to delete " usernic_error("Caller is not allowed to delete network "
"network device\n"); "device \"%s\"\n", args.veth_name);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
......
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