Commit af256970 by Christian Brauner

lxc-user-nic: non-functional changes

parent a055595c
...@@ -73,12 +73,13 @@ static void usage(char *me, bool fail) ...@@ -73,12 +73,13 @@ static void usage(char *me, bool fail)
static int open_and_lock(char *path) static int open_and_lock(char *path)
{ {
int fd; int fd, ret;
struct flock lk; struct flock lk;
fd = open(path, O_RDWR|O_CREAT, S_IWUSR | S_IRUSR); fd = open(path, O_RDWR | O_CREAT, S_IWUSR | S_IRUSR);
if (fd < 0) { if (fd < 0) {
usernic_error("Failed to open %s: %s\n", path, strerror(errno)); usernic_error("Failed to open \"%s\": %s\n", path,
strerror(errno));
return -1; return -1;
} }
...@@ -86,8 +87,11 @@ static int open_and_lock(char *path) ...@@ -86,8 +87,11 @@ static int open_and_lock(char *path)
lk.l_whence = SEEK_SET; lk.l_whence = SEEK_SET;
lk.l_start = 0; lk.l_start = 0;
lk.l_len = 0; lk.l_len = 0;
if (fcntl(fd, F_SETLKW, &lk) < 0) {
usernic_error("Failed to lock %s: %s\n", path, strerror(errno)); ret = fcntl(fd, F_SETLKW, &lk);
if (ret < 0) {
usernic_error("Failed to lock \"%s\": %s\n", path,
strerror(errno));
close(fd); close(fd);
return -1; return -1;
} }
...@@ -95,14 +99,13 @@ static int open_and_lock(char *path) ...@@ -95,14 +99,13 @@ static int open_and_lock(char *path)
return fd; return fd;
} }
static char *get_username(void) static char *get_username(void)
{ {
struct passwd *pwd; struct passwd *pwd;
pwd = getpwuid(getuid()); pwd = getpwuid(getuid());
if (!pwd) { if (!pwd) {
usernic_error("Failed to call get username: %s\n", strerror(errno)); usernic_error("Failed to get username: %s\n", strerror(errno));
return NULL; return NULL;
} }
...@@ -132,9 +135,8 @@ static char **get_groupnames(void) ...@@ -132,9 +135,8 @@ static char **get_groupnames(void)
ngroups = getgroups(0, NULL); ngroups = getgroups(0, NULL);
if (ngroups < 0) { if (ngroups < 0) {
usernic_error( usernic_error("Failed to get number of groups the user "
"Failed to get number of groups the user belongs to: %s\n", "belongs to: %s\n", strerror(errno));
strerror(errno));
return NULL; return NULL;
} }
if (ngroups == 0) if (ngroups == 0)
...@@ -208,19 +210,21 @@ struct alloted_s { ...@@ -208,19 +210,21 @@ struct alloted_s {
struct alloted_s *next; struct alloted_s *next;
}; };
static struct alloted_s *append_alloted(struct alloted_s **head, char *name, int n) static struct alloted_s *append_alloted(struct alloted_s **head, char *name,
int n)
{ {
struct alloted_s *cur, *al; struct alloted_s *cur, *al;
if (!head || !name) { if (!head || !name) {
/* sanity check. parameters should not be null */ /* Sanity check. Parameters should not be null. */
usernic_error("%s\n", "Unexpected NULL argument"); usernic_error("%s\n", "Unexpected NULL argument");
return NULL; return NULL;
} }
al = malloc(sizeof(struct alloted_s)); al = malloc(sizeof(struct alloted_s));
if (!al) { if (!al) {
usernic_error("Failed to allocate memory: %s\n", strerror(errno)); usernic_error("Failed to allocate memory: %s\n",
strerror(errno));
return NULL; return NULL;
} }
...@@ -271,7 +275,8 @@ static void free_alloted(struct alloted_s **head) ...@@ -271,7 +275,8 @@ static void free_alloted(struct alloted_s **head)
* Return the count entry for the calling user if there is one. Else * Return the count entry for the calling user if there is one. Else
* return -1. * return -1.
*/ */
static int get_alloted(char *me, char *intype, char *link, struct alloted_s **alloted) static int get_alloted(char *me, char *intype, char *link,
struct alloted_s **alloted)
{ {
int n, ret; int n, ret;
char name[100], type[100], br[100]; char name[100], type[100], br[100];
...@@ -284,13 +289,15 @@ static int get_alloted(char *me, char *intype, char *link, struct alloted_s **al ...@@ -284,13 +289,15 @@ static int get_alloted(char *me, char *intype, char *link, struct alloted_s **al
fin = fopen(LXC_USERNIC_CONF, "r"); fin = fopen(LXC_USERNIC_CONF, "r");
if (!fin) { if (!fin) {
usernic_error("Failed to open \"%s\": %s\n", LXC_USERNIC_CONF, strerror(errno)); usernic_error("Failed to open \"%s\": %s\n", LXC_USERNIC_CONF,
strerror(errno));
return -1; return -1;
} }
groups = get_groupnames(); groups = get_groupnames();
while ((getline(&line, &len, fin)) != -1) { while ((getline(&line, &len, fin)) != -1) {
ret = sscanf(line, "%99[^ \t] %99[^ \t] %99[^ \t] %d", name, type, br, &n); ret = sscanf(line, "%99[^ \t] %99[^ \t] %99[^ \t] %d", name,
type, br, &n);
if (ret != 4) if (ret != 4)
continue; continue;
...@@ -363,7 +370,8 @@ static char *find_line(char *p, char *e, char *u, char *t, char *l) ...@@ -363,7 +370,8 @@ 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) || strncmp(p, u, strlen(u))) if (!p2 || ((size_t)(p2 - p)) != strlen(u) ||
strncmp(p, u, strlen(u)))
goto next; goto next;
p = p2 + 1; p = p2 + 1;
...@@ -371,7 +379,8 @@ static char *find_line(char *p, char *e, char *u, char *t, char *l) ...@@ -371,7 +379,8 @@ 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(t) || strncmp(p, t, strlen(t))) if (!p2 || ((size_t)(p2 - p)) != strlen(t) ||
strncmp(p, t, strlen(t)))
goto next; goto next;
p = p2 + 1; p = p2 + 1;
...@@ -379,11 +388,12 @@ static char *find_line(char *p, char *e, char *u, char *t, char *l) ...@@ -379,11 +388,12 @@ 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(l) || strncmp(p, l, strlen(l))) if (!p2 || ((size_t)(p2 - p)) != strlen(l) ||
strncmp(p, l, strlen(l)))
goto next; goto next;
return ret; return ret;
next: next:
p = p1 + 1; p = p1 + 1;
} }
...@@ -422,7 +432,8 @@ static int instantiate_veth(char *n1, char **n2) ...@@ -422,7 +432,8 @@ static int instantiate_veth(char *n1, char **n2)
err = lxc_veth_create(n1, *n2); err = lxc_veth_create(n1, *n2);
if (err) { if (err) {
usernic_error("Failed to create %s-%s : %s.\n", n1, *n2, strerror(-err)); usernic_error("Failed to create %s-%s : %s.\n", n1, *n2,
strerror(-err));
return -1; return -1;
} }
...@@ -432,8 +443,7 @@ static int instantiate_veth(char *n1, char **n2) ...@@ -432,8 +443,7 @@ static int instantiate_veth(char *n1, char **n2)
err = setup_private_host_hw_addr(n1); err = setup_private_host_hw_addr(n1);
if (err) if (err)
usernic_error("Failed to change mac address of host interface " usernic_error("Failed to change mac address of host interface "
"%s : %s\n", "%s : %s\n", n1, strerror(-err));
n1, strerror(-err));
return netdev_set_flag(n1, IFF_UP); return netdev_set_flag(n1, IFF_UP);
} }
...@@ -476,13 +486,15 @@ static bool create_nic(char *nic, char *br, int pid, char **cnic) ...@@ -476,13 +486,15 @@ static bool create_nic(char *nic, char *br, int pid, char **cnic)
if (mtu > 0) { if (mtu > 0) {
ret = lxc_netdev_set_mtu(veth1buf, mtu); ret = lxc_netdev_set_mtu(veth1buf, mtu);
if (ret < 0) { if (ret < 0) {
usernic_error("Failed to set mtu to %d on %s\n", mtu, veth1buf); usernic_error("Failed to set mtu to %d on %s\n",
mtu, veth1buf);
goto out_del; goto out_del;
} }
ret = lxc_netdev_set_mtu(veth2buf, mtu); ret = lxc_netdev_set_mtu(veth2buf, mtu);
if (ret < 0) { if (ret < 0) {
usernic_error("Failed to set mtu to %d on %s\n", mtu, veth2buf); usernic_error("Failed to set mtu to %d on %s\n",
mtu, veth2buf);
goto out_del; goto out_del;
} }
} }
...@@ -498,7 +510,8 @@ static bool create_nic(char *nic, char *br, int pid, char **cnic) ...@@ -498,7 +510,8 @@ static bool create_nic(char *nic, char *br, int pid, char **cnic)
/* pass veth2 to target netns */ /* pass veth2 to target netns */
ret = lxc_netdev_move_by_name(veth2buf, pid, NULL); ret = lxc_netdev_move_by_name(veth2buf, pid, NULL);
if (ret < 0) { if (ret < 0) {
usernic_error("Error moving %s to network namespace of %d\n", veth2buf, pid); usernic_error("Error moving %s to network namespace of %d\n",
veth2buf, pid);
goto out_del; goto out_del;
} }
...@@ -545,7 +558,8 @@ static bool get_nic_from_line(char *p, char **nic) ...@@ -545,7 +558,8 @@ static bool get_nic_from_line(char *p, char **nic)
int ret; int ret;
char user[100], type[100], br[100]; char user[100], type[100], br[100];
ret = sscanf(p, "%99[^ \t\n] %99[^ \t\n] %99[^ \t\n] %99[^ \t\n]", user, type, br, *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)
return false; return false;
...@@ -579,9 +593,10 @@ static bool cull_entries(int fd, char *me, char *t, char *br) ...@@ -579,9 +593,10 @@ static bool cull_entries(int fd, char *me, char *t, char *br)
if (len == 0) if (len == 0)
return true; return true;
buf = mmap(NULL, len, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); buf = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (buf == MAP_FAILED) { if (buf == MAP_FAILED) {
usernic_error("Failed to establish shared memory mapping: %s\n", strerror(errno)); usernic_error("Failed to establish shared memory mapping: %s\n",
strerror(errno));
return false; return false;
} }
...@@ -626,7 +641,8 @@ static bool cull_entries(int fd, char *me, char *t, char *br) ...@@ -626,7 +641,8 @@ static bool cull_entries(int fd, char *me, char *t, char *br)
munmap(buf, sb.st_size); munmap(buf, sb.st_size);
if (ftruncate(fd, p - buf)) if (ftruncate(fd, p - buf))
usernic_error("Failed to set new file size: %s\n", strerror(errno)); usernic_error("Failed to set new file size: %s\n",
strerror(errno));
return true; return true;
} }
...@@ -647,10 +663,7 @@ static int count_entries(char *buf, off_t len, char *me, char *t, char *br) ...@@ -647,10 +663,7 @@ static int count_entries(char *buf, off_t len, char *me, char *t, char *br)
return count; return count;
} }
/* /* The dbfile has lines of the format: user type bridge nicname. */
* The dbfile has lines of the format:
* user type bridge nicname
*/
static char *get_nic_if_avail(int fd, struct alloted_s *names, int pid, static char *get_nic_if_avail(int fd, struct alloted_s *names, int pid,
char *intype, char *br, int allowed, char **cnic) char *intype, char *br, int allowed, char **cnic)
{ {
...@@ -677,9 +690,11 @@ static char *get_nic_if_avail(int fd, struct alloted_s *names, int pid, ...@@ -677,9 +690,11 @@ static char *get_nic_if_avail(int fd, struct alloted_s *names, int pid,
len = sb.st_size; len = sb.st_size;
if (len > 0) { if (len > 0) {
buf = mmap(NULL, len, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); buf =
mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (buf == MAP_FAILED) { if (buf == MAP_FAILED) {
usernic_error("Failed to establish shared memory mapping: %s\n", strerror(errno)); usernic_error("Failed to establish shared memory mapping: %s\n",
strerror(errno));
return NULL; return NULL;
} }
...@@ -724,11 +739,13 @@ static char *get_nic_if_avail(int fd, struct alloted_s *names, int pid, ...@@ -724,11 +739,13 @@ static char *get_nic_if_avail(int fd, struct alloted_s *names, int pid,
munmap(buf, len); munmap(buf, len);
if (ftruncate(fd, len + slen)) if (ftruncate(fd, len + slen))
usernic_error("Failed to set new file size: %s\n", strerror(errno)); usernic_error("Failed to set new file size: %s\n",
strerror(errno));
buf = mmap(NULL, len + slen, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); buf = mmap(NULL, len + slen, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (buf == MAP_FAILED) { if (buf == MAP_FAILED) {
usernic_error("Failed to establish shared memory mapping: %s\n", strerror(errno)); usernic_error("Failed to establish shared memory mapping: %s\n",
strerror(errno));
if (lxc_netdev_delete_by_name(nicname) != 0) if (lxc_netdev_delete_by_name(nicname) != 0)
usernic_error("Error unlinking %s\n", nicname); usernic_error("Error unlinking %s\n", nicname);
free(nicname); free(nicname);
...@@ -743,6 +760,7 @@ static char *get_nic_if_avail(int fd, struct alloted_s *names, int pid, ...@@ -743,6 +760,7 @@ static char *get_nic_if_avail(int fd, struct alloted_s *names, int pid,
static bool create_db_dir(char *fnam) static bool create_db_dir(char *fnam)
{ {
int ret;
char *p; char *p;
p = alloca(strlen(fnam) + 1); p = alloca(strlen(fnam) + 1);
...@@ -757,8 +775,11 @@ again: ...@@ -757,8 +775,11 @@ again:
return true; return true;
*p = '\0'; *p = '\0';
if (mkdir(fnam, 0755) && errno != EEXIST) {
usernic_error("Failed to create %s: %s\n", fnam, strerror(errno)); ret = mkdir(fnam, 0755);
if (ret < 0 && errno != EEXIST) {
usernic_error("Failed to create %s: %s\n", fnam,
strerror(errno));
*p = '/'; *p = '/';
return false; return false;
} }
......
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