static char *get_username(void)
{
+ __do_free char *buf = NULL;
struct passwd pwent;
struct passwd *pwentp = NULL;
- char *buf;
- char *username;
size_t bufsize;
int ret;
usernic_error("%s", "Could not find matched password record\n");
CMD_SYSERROR("Failed to get username: %u\n", getuid());
- free(buf);
return NULL;
}
- username = strdup(pwent.pw_name);
- free(buf);
-
- return username;
+ return strdup(pwent.pw_name);
}
static void free_groupnames(char **groupnames)
static char **get_groupnames(void)
{
+ __do_free char *buf = NULL;
+ __do_free gid_t *group_ids = NULL;
int ngroups;
- gid_t *group_ids;
int ret, i;
char **groupnames;
struct group grent;
struct group *grentp = NULL;
- char *buf;
size_t bufsize;
ngroups = getgroups(0, NULL);
ret = getgroups(ngroups, group_ids);
if (ret < 0) {
- free(group_ids);
CMD_SYSERROR("Failed to get process groups\n");
return NULL;
}
groupnames = malloc(sizeof(char *) * (ngroups + 1));
if (!groupnames) {
- free(group_ids);
CMD_SYSERROR("Failed to allocate memory while getting group names\n");
return NULL;
}
buf = malloc(bufsize);
if (!buf) {
- free(group_ids);
free_groupnames(groupnames);
CMD_SYSERROR("Failed to allocate memory while getting group names\n");
return NULL;
usernic_error("%s", "Could not find matched group record\n");
CMD_SYSERROR("Failed to get group name: %u\n", group_ids[i]);
- free(buf);
- free(group_ids);
free_groupnames(groupnames);
return NULL;
}
groupnames[i] = strdup(grent.gr_name);
if (!groupnames[i]) {
usernic_error("Failed to copy group name \"%s\"", grent.gr_name);
- free(buf);
- free(group_ids);
free_groupnames(groupnames);
return NULL;
}
}
- free(buf);
- free(group_ids);
-
return groupnames;
}
static int get_alloted(char *me, char *intype, char *link,
struct alloted_s **alloted)
{
+ __do_free char *line = NULL;
+ __do_fclose FILE *fin = NULL;
int n, ret;
char name[100], type[100], br[100];
char **groups;
- FILE *fin;
int count = 0;
size_t len = 0;
- char *line = NULL;
fin = fopen(LXC_USERNIC_CONF, "r");
if (!fin) {
}
free_groupnames(groups);
- fclose(fin);
- free(line);
/* Now return the total number of nics that this user can create. */
return count;
static bool cull_entries(int fd, char *name, char *net_type, char *net_link,
char *net_dev, bool *found_nicname)
{
+ __do_free struct entry_line *entry_lines = NULL;
int i, ret;
char *buf, *buf_end, *buf_start;
struct stat sb;
int n = 0;
bool found, keep;
- struct entry_line *entry_lines = NULL;
ret = fstat(fd, &sb);
if (ret < 0) {
newe = realloc(entry_lines, sizeof(*entry_lines) * (n + 1));
if (!newe) {
- free(entry_lines);
lxc_strmunmap(buf, sb.st_size);
return false;
}
buf_start++;
}
- free(entry_lines);
-
ret = ftruncate(fd, buf_start - buf);
lxc_strmunmap(buf, sb.st_size);
if (ret < 0)
static char *get_nic_if_avail(int fd, struct alloted_s *names, int pid,
char *intype, char *br, int allowed, char **cnic)
{
+ __do_free char *newline = NULL;
int ret;
size_t slen;
- char *newline, *owner;
+ char *owner;
char nicname[IFNAMSIZ];
struct stat sb;
struct alloted_s *n;
slen = strlen(owner) + strlen(intype) + strlen(br) + strlen(nicname) + 4;
newline = malloc(slen + 1);
if (!newline) {
- free(newline);
CMD_SYSERROR("Failed allocate memory\n");
return NULL;
}
if (lxc_netdev_delete_by_name(nicname) != 0)
usernic_error("Error unlinking %s\n", nicname);
- free(newline);
return NULL;
}
if (lxc_netdev_delete_by_name(nicname) != 0)
usernic_error("Error unlinking %s\n", nicname);
- free(newline);
return NULL;
}
* \0 byte! Files are not \0-terminated!
*/
memmove(buf + sb.st_size, newline, slen);
- free(newline);
lxc_strmunmap(buf, sb.st_size + slen);
return strdup(nicname);
int main(int argc, char *argv[])
{
+ __do_free char *me = NULL, *newname = NULL, *nicname = NULL;
int fd, n, pid, request, ret;
- char *me, *newname;
struct user_nic_args args;
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;
if (argc < 7 || argc > 8) {
}
n = get_alloted(me, args.type, args.link, &alloted);
- free(me);
if (request == LXC_USERNIC_DELETE) {
struct alloted_s *it;
if (ret < 0)
usernic_error("Failed to delete \"%s\"\n", cnic);
- free(nicname);
_exit(EXIT_FAILURE);
}
host_veth_ifidx = if_nametoindex(nicname);
if (!host_veth_ifidx) {
- free(newname);
- free(nicname);
CMD_SYSERROR("Failed to get netdev index\n");
_exit(EXIT_FAILURE);
}
*/
fprintf(stdout, "%s:%d:%s:%d\n", newname, container_veth_ifidx, nicname,
host_veth_ifidx);
- free(newname);
- free(nicname);
fflush(stdout);
_exit(EXIT_SUCCESS);