lxc-user-nic: use cleanup macros

parent 95150175
...@@ -111,10 +111,9 @@ static int open_and_lock(char *path) ...@@ -111,10 +111,9 @@ static int open_and_lock(char *path)
static char *get_username(void) static char *get_username(void)
{ {
__do_free char *buf = NULL;
struct passwd pwent; struct passwd pwent;
struct passwd *pwentp = NULL; struct passwd *pwentp = NULL;
char *buf;
char *username;
size_t bufsize; size_t bufsize;
int ret; int ret;
...@@ -132,14 +131,10 @@ static char *get_username(void) ...@@ -132,14 +131,10 @@ static char *get_username(void)
usernic_error("%s", "Could not find matched password record\n"); usernic_error("%s", "Could not find matched password record\n");
CMD_SYSERROR("Failed to get username: %u\n", getuid()); CMD_SYSERROR("Failed to get username: %u\n", getuid());
free(buf);
return NULL; return NULL;
} }
username = strdup(pwent.pw_name); return strdup(pwent.pw_name);
free(buf);
return username;
} }
static void free_groupnames(char **groupnames) static void free_groupnames(char **groupnames)
...@@ -157,13 +152,13 @@ static void free_groupnames(char **groupnames) ...@@ -157,13 +152,13 @@ static void free_groupnames(char **groupnames)
static char **get_groupnames(void) static char **get_groupnames(void)
{ {
__do_free char *buf = NULL;
__do_free gid_t *group_ids = NULL;
int ngroups; int ngroups;
gid_t *group_ids;
int ret, i; int ret, i;
char **groupnames; char **groupnames;
struct group grent; struct group grent;
struct group *grentp = NULL; struct group *grentp = NULL;
char *buf;
size_t bufsize; size_t bufsize;
ngroups = getgroups(0, NULL); ngroups = getgroups(0, NULL);
...@@ -182,14 +177,12 @@ static char **get_groupnames(void) ...@@ -182,14 +177,12 @@ static char **get_groupnames(void)
ret = getgroups(ngroups, group_ids); ret = getgroups(ngroups, group_ids);
if (ret < 0) { if (ret < 0) {
free(group_ids);
CMD_SYSERROR("Failed to get process groups\n"); CMD_SYSERROR("Failed to get process groups\n");
return NULL; return NULL;
} }
groupnames = malloc(sizeof(char *) * (ngroups + 1)); groupnames = malloc(sizeof(char *) * (ngroups + 1));
if (!groupnames) { if (!groupnames) {
free(group_ids);
CMD_SYSERROR("Failed to allocate memory while getting group names\n"); CMD_SYSERROR("Failed to allocate memory while getting group names\n");
return NULL; return NULL;
} }
...@@ -202,7 +195,6 @@ static char **get_groupnames(void) ...@@ -202,7 +195,6 @@ static char **get_groupnames(void)
buf = malloc(bufsize); buf = malloc(bufsize);
if (!buf) { if (!buf) {
free(group_ids);
free_groupnames(groupnames); free_groupnames(groupnames);
CMD_SYSERROR("Failed to allocate memory while getting group names\n"); CMD_SYSERROR("Failed to allocate memory while getting group names\n");
return NULL; return NULL;
...@@ -215,8 +207,6 @@ static char **get_groupnames(void) ...@@ -215,8 +207,6 @@ static char **get_groupnames(void)
usernic_error("%s", "Could not find matched group record\n"); usernic_error("%s", "Could not find matched group record\n");
CMD_SYSERROR("Failed to get group name: %u\n", group_ids[i]); CMD_SYSERROR("Failed to get group name: %u\n", group_ids[i]);
free(buf);
free(group_ids);
free_groupnames(groupnames); free_groupnames(groupnames);
return NULL; return NULL;
} }
...@@ -224,16 +214,11 @@ static char **get_groupnames(void) ...@@ -224,16 +214,11 @@ static char **get_groupnames(void)
groupnames[i] = strdup(grent.gr_name); groupnames[i] = strdup(grent.gr_name);
if (!groupnames[i]) { if (!groupnames[i]) {
usernic_error("Failed to copy group name \"%s\"", grent.gr_name); usernic_error("Failed to copy group name \"%s\"", grent.gr_name);
free(buf);
free(group_ids);
free_groupnames(groupnames); free_groupnames(groupnames);
return NULL; return NULL;
} }
} }
free(buf);
free(group_ids);
return groupnames; return groupnames;
} }
...@@ -321,13 +306,13 @@ static void free_alloted(struct alloted_s **head) ...@@ -321,13 +306,13 @@ static void free_alloted(struct alloted_s **head)
static int get_alloted(char *me, char *intype, char *link, static int get_alloted(char *me, char *intype, char *link,
struct alloted_s **alloted) struct alloted_s **alloted)
{ {
__do_free char *line = NULL;
__do_fclose FILE *fin = NULL;
int n, ret; int n, ret;
char name[100], type[100], br[100]; char name[100], type[100], br[100];
char **groups; char **groups;
FILE *fin;
int count = 0; int count = 0;
size_t len = 0; size_t len = 0;
char *line = NULL;
fin = fopen(LXC_USERNIC_CONF, "r"); fin = fopen(LXC_USERNIC_CONF, "r");
if (!fin) { if (!fin) {
...@@ -378,8 +363,6 @@ static int get_alloted(char *me, char *intype, char *link, ...@@ -378,8 +363,6 @@ static int get_alloted(char *me, char *intype, char *link,
} }
free_groupnames(groups); free_groupnames(groups);
fclose(fin);
free(line);
/* Now return the total number of nics that this user can create. */ /* Now return the total number of nics that this user can create. */
return count; return count;
...@@ -610,12 +593,12 @@ struct entry_line { ...@@ -610,12 +593,12 @@ struct entry_line {
static bool cull_entries(int fd, char *name, char *net_type, char *net_link, static bool cull_entries(int fd, char *name, char *net_type, char *net_link,
char *net_dev, bool *found_nicname) char *net_dev, bool *found_nicname)
{ {
__do_free struct entry_line *entry_lines = NULL;
int i, ret; int i, ret;
char *buf, *buf_end, *buf_start; char *buf, *buf_end, *buf_start;
struct stat sb; struct stat sb;
int n = 0; int n = 0;
bool found, keep; bool found, keep;
struct entry_line *entry_lines = NULL;
ret = fstat(fd, &sb); ret = fstat(fd, &sb);
if (ret < 0) { if (ret < 0) {
...@@ -641,7 +624,6 @@ static bool cull_entries(int fd, char *name, char *net_type, char *net_link, ...@@ -641,7 +624,6 @@ static bool cull_entries(int fd, char *name, char *net_type, char *net_link,
newe = realloc(entry_lines, sizeof(*entry_lines) * (n + 1)); newe = realloc(entry_lines, sizeof(*entry_lines) * (n + 1));
if (!newe) { if (!newe) {
free(entry_lines);
lxc_strmunmap(buf, sb.st_size); lxc_strmunmap(buf, sb.st_size);
return false; return false;
} }
...@@ -672,8 +654,6 @@ static bool cull_entries(int fd, char *name, char *net_type, char *net_link, ...@@ -672,8 +654,6 @@ static bool cull_entries(int fd, char *name, char *net_type, char *net_link,
buf_start++; buf_start++;
} }
free(entry_lines);
ret = ftruncate(fd, buf_start - buf); ret = ftruncate(fd, buf_start - buf);
lxc_strmunmap(buf, sb.st_size); lxc_strmunmap(buf, sb.st_size);
if (ret < 0) if (ret < 0)
...@@ -706,9 +686,10 @@ static int count_entries(char *buf, off_t len, char *name, char *net_type, char ...@@ -706,9 +686,10 @@ static int count_entries(char *buf, off_t len, char *name, char *net_type, char
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)
{ {
__do_free char *newline = NULL;
int ret; int ret;
size_t slen; size_t slen;
char *newline, *owner; char *owner;
char nicname[IFNAMSIZ]; char nicname[IFNAMSIZ];
struct stat sb; struct stat sb;
struct alloted_s *n; struct alloted_s *n;
...@@ -795,7 +776,6 @@ static char *get_nic_if_avail(int fd, struct alloted_s *names, int pid, ...@@ -795,7 +776,6 @@ static char *get_nic_if_avail(int fd, struct alloted_s *names, int pid,
slen = strlen(owner) + strlen(intype) + strlen(br) + strlen(nicname) + 4; slen = strlen(owner) + strlen(intype) + strlen(br) + strlen(nicname) + 4;
newline = malloc(slen + 1); newline = malloc(slen + 1);
if (!newline) { if (!newline) {
free(newline);
CMD_SYSERROR("Failed allocate memory\n"); CMD_SYSERROR("Failed allocate memory\n");
return NULL; return NULL;
} }
...@@ -805,7 +785,6 @@ static char *get_nic_if_avail(int fd, struct alloted_s *names, int pid, ...@@ -805,7 +785,6 @@ static char *get_nic_if_avail(int fd, struct alloted_s *names, int pid,
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(newline);
return NULL; return NULL;
} }
...@@ -824,7 +803,6 @@ static char *get_nic_if_avail(int fd, struct alloted_s *names, int pid, ...@@ -824,7 +803,6 @@ static char *get_nic_if_avail(int fd, struct alloted_s *names, int pid,
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(newline);
return NULL; return NULL;
} }
...@@ -832,7 +810,6 @@ static char *get_nic_if_avail(int fd, struct alloted_s *names, int pid, ...@@ -832,7 +810,6 @@ static char *get_nic_if_avail(int fd, struct alloted_s *names, int pid,
* \0 byte! Files are not \0-terminated! * \0 byte! Files are not \0-terminated!
*/ */
memmove(buf + sb.st_size, newline, slen); memmove(buf + sb.st_size, newline, slen);
free(newline);
lxc_strmunmap(buf, sb.st_size + slen); lxc_strmunmap(buf, sb.st_size + slen);
return strdup(nicname); return strdup(nicname);
...@@ -1115,11 +1092,11 @@ do_partial_cleanup: ...@@ -1115,11 +1092,11 @@ do_partial_cleanup:
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
__do_free char *me = NULL, *newname = NULL, *nicname = NULL;
int fd, n, pid, request, ret; int fd, n, pid, request, ret;
char *me, *newname;
struct user_nic_args args; struct user_nic_args args;
int container_veth_ifidx = -1, host_veth_ifidx = -1, netns_fd = -1; int container_veth_ifidx = -1, host_veth_ifidx = -1, netns_fd = -1;
char *cnic = NULL, *nicname = NULL; char *cnic = NULL;
struct alloted_s *alloted = NULL; struct alloted_s *alloted = NULL;
if (argc < 7 || argc > 8) { if (argc < 7 || argc > 8) {
...@@ -1248,7 +1225,6 @@ int main(int argc, char *argv[]) ...@@ -1248,7 +1225,6 @@ int main(int argc, char *argv[])
} }
n = get_alloted(me, args.type, args.link, &alloted); n = get_alloted(me, args.type, args.link, &alloted);
free(me);
if (request == LXC_USERNIC_DELETE) { if (request == LXC_USERNIC_DELETE) {
struct alloted_s *it; struct alloted_s *it;
...@@ -1313,14 +1289,11 @@ int main(int argc, char *argv[]) ...@@ -1313,14 +1289,11 @@ int main(int argc, char *argv[])
if (ret < 0) if (ret < 0)
usernic_error("Failed to delete \"%s\"\n", cnic); usernic_error("Failed to delete \"%s\"\n", cnic);
free(nicname);
_exit(EXIT_FAILURE); _exit(EXIT_FAILURE);
} }
host_veth_ifidx = if_nametoindex(nicname); host_veth_ifidx = if_nametoindex(nicname);
if (!host_veth_ifidx) { if (!host_veth_ifidx) {
free(newname);
free(nicname);
CMD_SYSERROR("Failed to get netdev index\n"); CMD_SYSERROR("Failed to get netdev index\n");
_exit(EXIT_FAILURE); _exit(EXIT_FAILURE);
} }
...@@ -1330,8 +1303,6 @@ int main(int argc, char *argv[]) ...@@ -1330,8 +1303,6 @@ int main(int argc, char *argv[])
*/ */
fprintf(stdout, "%s:%d:%s:%d\n", newname, container_veth_ifidx, nicname, fprintf(stdout, "%s:%d:%s:%d\n", newname, container_veth_ifidx, nicname,
host_veth_ifidx); host_veth_ifidx);
free(newname);
free(nicname);
fflush(stdout); fflush(stdout);
_exit(EXIT_SUCCESS); _exit(EXIT_SUCCESS);
......
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