diff --git a/src/include/netns_ifaddrs.c b/src/include/netns_ifaddrs.c index fc0ffc167..1c8b92b66 100644 --- a/src/include/netns_ifaddrs.c +++ b/src/include/netns_ifaddrs.c @@ -373,12 +373,7 @@ static int __ifaddrs_netlink_recv(int fd, unsigned int seq, int type, int af, struct nlmsghdr *h), void *ctx) { - char getlink_buf[__NETLINK_ALIGN(sizeof(struct nlmsghdr)) + - __NETLINK_ALIGN(sizeof(struct ifinfomsg)) + - __NETLINK_ALIGN(1024)]; - char getaddr_buf[__NETLINK_ALIGN(sizeof(struct nlmsghdr)) + - __NETLINK_ALIGN(sizeof(struct ifaddrmsg)) + - __NETLINK_ALIGN(1024)]; + int r, property, ret; char *buf; struct nlmsghdr *hdr; struct ifinfomsg *ifi_msg; @@ -391,47 +386,43 @@ static int __ifaddrs_netlink_recv(int fd, unsigned int seq, int type, int af, } req; struct nlmsghdr reply; } u; - int r, property, ret; - - if (type == RTM_GETLINK) - buf = getlink_buf; - else if (type == RTM_GETADDR) - buf = getaddr_buf; - else - return -1; - - memset(buf, 0, sizeof(*buf)); + char getlink_buf[__NETLINK_ALIGN(sizeof(struct nlmsghdr)) + + __NETLINK_ALIGN(sizeof(struct ifinfomsg)) + + __NETLINK_ALIGN(1024)] = {0}; + char getaddr_buf[__NETLINK_ALIGN(sizeof(struct nlmsghdr)) + + __NETLINK_ALIGN(sizeof(struct ifaddrmsg)) + + __NETLINK_ALIGN(1024)] = {0}; #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wcast-align" - hdr = (struct nlmsghdr *)buf; - if (type == RTM_GETLINK) - ifi_msg = (struct ifinfomsg *)__NLMSG_DATA(hdr); - else - ifa_msg = (struct ifaddrmsg *)__NLMSG_DATA(hdr); - - if (type == RTM_GETLINK) + if (type == RTM_GETLINK) { + buf = getlink_buf; + hdr = (struct nlmsghdr *)buf; hdr->nlmsg_len = NLMSG_LENGTH(sizeof(*ifi_msg)); - else + + ifi_msg = (struct ifinfomsg *)__NLMSG_DATA(hdr); + ifi_msg->ifi_family = af; + + property = IFLA_TARGET_NETNSID; + } else if (type == RTM_GETADDR) { + buf = getaddr_buf; + hdr = (struct nlmsghdr *)buf; hdr->nlmsg_len = NLMSG_LENGTH(sizeof(*ifa_msg)); + + ifa_msg = (struct ifaddrmsg *)__NLMSG_DATA(hdr); + ifa_msg->ifa_family = af; + + property = IFA_TARGET_NETNSID; + } else { + errno = EINVAL; + return -1; + } #pragma GCC diagnostic pop hdr->nlmsg_type = type; hdr->nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST; hdr->nlmsg_pid = 0; hdr->nlmsg_seq = seq; - if (type == RTM_GETLINK) - ifi_msg->ifi_family = af; - else - ifa_msg->ifa_family = af; - - errno = EINVAL; - if (type == RTM_GETLINK) - property = IFLA_TARGET_NETNSID; - else if (type == RTM_GETADDR) - property = IFA_TARGET_NETNSID; - else - return -1; if (netns_id >= 0) addattr(hdr, 1024, property, &netns_id, sizeof(netns_id)); diff --git a/src/lxc/cgroups/cgfsng.c b/src/lxc/cgroups/cgfsng.c index 1a722ae2f..43403ce42 100644 --- a/src/lxc/cgroups/cgfsng.c +++ b/src/lxc/cgroups/cgfsng.c @@ -2133,7 +2133,8 @@ static int __cg_unified_attach(const struct hierarchy *h, const char *name, if (errno != EBUSY) goto on_error; - } while (++idx > 0 && idx < 1000); + idx++; + } while (idx < 1000); on_success: if (idx < 1000) diff --git a/src/lxc/cmd/lxc_init.c b/src/lxc/cmd/lxc_init.c index d50868a13..e7daa7863 100644 --- a/src/lxc/cmd/lxc_init.c +++ b/src/lxc/cmd/lxc_init.c @@ -182,17 +182,17 @@ static void kill_children(pid_t pid) } while (!feof(f)) { - pid_t pid; + pid_t find_pid; - if (fscanf(f, "%d ", &pid) != 1) { + if (fscanf(f, "%d ", &find_pid) != 1) { if (my_args.quiet) fprintf(stderr, "Failed to retrieve pid\n"); fclose(f); return; } - kill_children(pid); - kill(pid, SIGKILL); + (void)kill_children(find_pid); + (void)kill(find_pid, SIGKILL); } fclose(f); diff --git a/src/lxc/cmd/lxc_monitord.c b/src/lxc/cmd/lxc_monitord.c index 13158d7b2..f8312998b 100644 --- a/src/lxc/cmd/lxc_monitord.c +++ b/src/lxc/cmd/lxc_monitord.c @@ -80,7 +80,7 @@ struct lxc_monitor { struct lxc_epoll_descr descr; }; -static struct lxc_monitor mon; +static struct lxc_monitor monitor; static int quit; static int lxc_monitord_fifo_create(struct lxc_monitor *mon) @@ -349,7 +349,7 @@ static int lxc_monitord_mainloop_add(struct lxc_monitor *mon) static void lxc_monitord_cleanup(void) { - lxc_monitord_delete(&mon); + lxc_monitord_delete(&monitor); } static void lxc_monitord_sig_handler(int sig) @@ -415,15 +415,15 @@ int main(int argc, char *argv[]) ret = EXIT_FAILURE; - memset(&mon, 0, sizeof(mon)); - mon.lxcpath = lxcpath; - if (lxc_mainloop_open(&mon.descr)) { + memset(&monitor, 0, sizeof(monitor)); + monitor.lxcpath = lxcpath; + if (lxc_mainloop_open(&monitor.descr)) { ERROR("Failed to create mainloop"); goto on_error; } mainloop_opened = true; - if (lxc_monitord_create(&mon)) + if (lxc_monitord_create(&monitor)) goto on_error; monitord_created = true; @@ -437,22 +437,22 @@ int main(int argc, char *argv[]) ; close(pipefd); - if (lxc_monitord_mainloop_add(&mon)) { + if (lxc_monitord_mainloop_add(&monitor)) { ERROR("Failed to add mainloop handlers"); goto on_error; } NOTICE("lxc-monitord with pid %d is now monitoring lxcpath %s", - lxc_raw_getpid(), mon.lxcpath); + lxc_raw_getpid(), monitor.lxcpath); for (;;) { - ret = lxc_mainloop(&mon.descr, 1000 * 30); + ret = lxc_mainloop(&monitor.descr, 1000 * 30); if (ret) { ERROR("mainloop returned an error"); break; } - if (mon.clientfds_cnt <= 0) { + if (monitor.clientfds_cnt <= 0) { NOTICE("No remaining clients. lxc-monitord is exiting"); break; } @@ -471,7 +471,7 @@ on_error: lxc_monitord_cleanup(); if (mainloop_opened) - lxc_mainloop_close(&mon.descr); + lxc_mainloop_close(&monitor.descr); exit(ret); } diff --git a/src/lxc/commands.c b/src/lxc/commands.c index 5c2ce04fe..1d3c2eaef 100644 --- a/src/lxc/commands.c +++ b/src/lxc/commands.c @@ -299,14 +299,9 @@ static int lxc_cmd(const char *name, struct lxc_cmd_rr *cmd, int *stopped, SYSTRACE("Command \"%s\" failed to connect command socket", lxc_cmd_str(cmd->req.cmd)); - if (errno == ECONNREFUSED) + if (errno == ECONNREFUSED || errno == EPIPE) *stopped = 1; - if (errno == EPIPE) { - *stopped = 1; - client_fd = 0; - } - return -1; } @@ -314,12 +309,12 @@ static int lxc_cmd(const char *name, struct lxc_cmd_rr *cmd, int *stopped, if (ret < 0 && errno == ECONNRESET) *stopped = 1; - if (!stay_connected || ret <= 0) - if (client_fd >= 0) { - saved_errno = errno; - close(client_fd); - errno = saved_errno; - } + if (!stay_connected || ret <= 0) { + saved_errno = errno; + close(client_fd); + errno = saved_errno; + return ret; + } if (stay_connected && ret > 0) cmd->rsp.ret = client_fd; diff --git a/src/lxc/conf.c b/src/lxc/conf.c index e822e90d3..7307770a8 100644 --- a/src/lxc/conf.c +++ b/src/lxc/conf.c @@ -1494,15 +1494,16 @@ int lxc_chroot(const struct lxc_rootfs *rootfs) */ static int lxc_pivot_root(const char *rootfs) { - int newroot = -1, oldroot = -1, ret = -1; + int oldroot; + int newroot = -1, ret = -1; - oldroot = open("/", O_DIRECTORY | O_RDONLY); + oldroot = open("/", O_DIRECTORY | O_RDONLY | O_CLOEXEC); if (oldroot < 0) { SYSERROR("Failed to open old root directory"); return -1; } - newroot = open(rootfs, O_DIRECTORY | O_RDONLY); + newroot = open(rootfs, O_DIRECTORY | O_RDONLY | O_CLOEXEC); if (newroot < 0) { SYSERROR("Failed to open new root directory"); goto on_error; @@ -1564,9 +1565,9 @@ static int lxc_pivot_root(const char *rootfs) TRACE("pivot_root(\"%s\") successful", rootfs); on_error: - if (oldroot != -1) - close(oldroot); - if (newroot != -1) + close(oldroot); + + if (newroot >= 0) close(newroot); return ret; @@ -2419,10 +2420,6 @@ FILE *make_anonymous_mount_file(struct lxc_list *mount, TRACE("Created temporary mount file"); } - if (fd < 0) { - SYSERROR("Could not create temporary mount file"); - return NULL; - } lxc_list_for_each (iterator, mount) { size_t len; diff --git a/src/lxc/confile.c b/src/lxc/confile.c index 5f1485a79..e1f4266b6 100644 --- a/src/lxc/confile.c +++ b/src/lxc/confile.c @@ -156,7 +156,7 @@ lxc_config_define(uts_name); lxc_config_define(sysctl); lxc_config_define(proc); -static struct lxc_config_t config[] = { +static struct lxc_config_t config_jump_table[] = { { "lxc.arch", set_config_personality, get_config_personality, clr_config_personality, }, { "lxc.apparmor.profile", set_config_apparmor_profile, get_config_apparmor_profile, clr_config_apparmor_profile, }, { "lxc.apparmor.allow_incomplete", set_config_apparmor_allow_incomplete, get_config_apparmor_allow_incomplete, clr_config_apparmor_allow_incomplete, }, @@ -246,15 +246,15 @@ static struct lxc_config_t config[] = { { "lxc.proc", set_config_proc, get_config_proc, clr_config_proc, }, }; -static const size_t config_size = sizeof(config) / sizeof(struct lxc_config_t); +static const size_t config_jump_table_size = sizeof(config_jump_table) / sizeof(struct lxc_config_t); struct lxc_config_t *lxc_get_config(const char *key) { size_t i; - for (i = 0; i < config_size; i++) - if (!strncmp(config[i].name, key, strlen(config[i].name))) - return &config[i]; + for (i = 0; i < config_jump_table_size; i++) + if (!strncmp(config_jump_table[i].name, key, strlen(config_jump_table[i].name))) + return &config_jump_table[i]; return NULL; } @@ -3489,8 +3489,10 @@ static int get_config_hooks(const char *key, char *retv, int inlen, return -1; subkey = strchr(subkey + 1, '.'); + if (!subkey) + return -1; subkey++; - if (!*subkey) + if (*subkey == '\0') return -1; for (i = 0; i < NUM_LXC_HOOKS; i++) { @@ -5190,8 +5192,8 @@ int lxc_list_config_items(char *retv, int inlen) else memset(retv, 0, inlen); - for (i = 0; i < config_size; i++) { - char *s = config[i].name; + for (i = 0; i < config_jump_table_size; i++) { + char *s = config_jump_table[i].name; if (s[strlen(s) - 1] == '.') continue; diff --git a/src/lxc/criu.c b/src/lxc/criu.c index c8cd449de..60cdb17e8 100644 --- a/src/lxc/criu.c +++ b/src/lxc/criu.c @@ -695,7 +695,7 @@ bool __criu_check_feature(uint64_t *features_to_check) return false; } - while (current_bit < sizeof(uint64_t) * 8) { + while (current_bit < (sizeof(uint64_t) * 8 - 1)) { /* only test requested features */ if (!(features & (1ULL << current_bit))) { /* skip this */ diff --git a/src/lxc/lxccontainer.c b/src/lxc/lxccontainer.c index 9941d646a..8c26769b4 100644 --- a/src/lxc/lxccontainer.c +++ b/src/lxc/lxccontainer.c @@ -1935,8 +1935,7 @@ static bool do_lxcapi_create(struct lxc_container *c, const char *t, out_unlock: umask(mask); - if (partial_fd >= 0) - remove_partial(c, partial_fd); + remove_partial(c, partial_fd); out: if (!ret) diff --git a/src/lxc/start.c b/src/lxc/start.c index ab035507e..55c2e0e99 100644 --- a/src/lxc/start.c +++ b/src/lxc/start.c @@ -1467,10 +1467,10 @@ int resolve_clone_flags(struct lxc_handler *handler) struct lxc_conf *conf = handler->conf; for (i = 0; i < LXC_NS_MAX; i++) { - if (conf->ns_keep != 0) { + if (conf->ns_keep > 0) { if ((conf->ns_keep & ns_info[i].clone_flag) == 0) handler->ns_clone_flags |= ns_info[i].clone_flag; - } else if (conf->ns_clone != 0) { + } else if (conf->ns_clone > 0) { if ((conf->ns_clone & ns_info[i].clone_flag) > 0) handler->ns_clone_flags |= ns_info[i].clone_flag; } else { diff --git a/src/lxc/state.c b/src/lxc/state.c index b3e6f83f8..0fb0e2d64 100644 --- a/src/lxc/state.c +++ b/src/lxc/state.c @@ -135,11 +135,6 @@ int lxc_wait(const char *lxcname, const char *states, int timeout, (void)nanosleep(&onesec, NULL); } - if (state < 0) { - ERROR("Failed to retrieve state from monitor"); - return -1; - } - TRACE("Retrieved state of container %s", lxc_state2str(state)); if (!s[state]) return -1; diff --git a/src/lxc/tools/lxc_copy.c b/src/lxc/tools/lxc_copy.c index 3492be99f..76af22646 100644 --- a/src/lxc/tools/lxc_copy.c +++ b/src/lxc/tools/lxc_copy.c @@ -87,7 +87,7 @@ static const struct option my_longopts[] = { }; /* mount keys */ -static char *const keys[] = { +static char *const mount_keys[] = { [LXC_MNT_BIND] = "bind", [LXC_MNT_OVL] = "overlay", NULL @@ -568,7 +568,7 @@ static int my_parser(struct lxc_arguments *args, int c, char *arg) break; case 'm': subopts = optarg; - if (parse_mntsubopts(subopts, keys, mntparameters) < 0) + if (parse_mntsubopts(subopts, mount_keys, mntparameters) < 0) return -1; break; case 'B': diff --git a/src/lxc/tools/lxc_info.c b/src/lxc/tools/lxc_info.c index 07303366c..138a3060b 100644 --- a/src/lxc/tools/lxc_info.c +++ b/src/lxc/tools/lxc_info.c @@ -48,7 +48,7 @@ static bool pid; static bool stats; static bool humanize = true; static char **key = NULL; -static int keys = 0; +static int nr_keys = 0; static int filter_count = 0; static int my_parser(struct lxc_arguments *args, int c, char *arg) @@ -57,13 +57,13 @@ static int my_parser(struct lxc_arguments *args, int c, char *arg) switch (c) { case 'c': - newk = realloc(key, (keys + 1) * sizeof(key[0])); + newk = realloc(key, (nr_keys + 1) * sizeof(key[0])); if (!newk) return -1; key = newk; - key[keys] = arg; - keys++; + key[nr_keys] = arg; + nr_keys++; break; case 'i': ips = true; filter_count += 1; break; case 's': state = true; filter_count += 1; break; @@ -341,7 +341,7 @@ static int print_info(const char *name, const char *lxcpath) return -1; } - if (!state && !pid && !ips && !stats && keys <= 0) { + if (!state && !pid && !ips && !stats && nr_keys <= 0) { state = pid = ips = stats = true; print_info_msg_str("Name:", c->name); } @@ -380,7 +380,7 @@ static int print_info(const char *name, const char *lxcpath) print_net_stats(c); } - for(i = 0; i < keys; i++) { + for(i = 0; i < nr_keys; i++) { int len = c->get_config_item(c, key[i], NULL, 0); if (len > 0) { @@ -389,7 +389,7 @@ static int print_info(const char *name, const char *lxcpath) if (c->get_config_item(c, key[i], val, len + 1) != len) { fprintf(stderr, "unable to read %s from configuration\n", key[i]); } else { - if (!humanize && keys == 1) + if (!humanize && nr_keys == 1) printf("%s\n", val); else printf("%s = %s\n", key[i], val); @@ -397,7 +397,7 @@ static int print_info(const char *name, const char *lxcpath) free(val); } else if (len == 0) { - if (!humanize && keys == 1) + if (!humanize && nr_keys == 1) printf("\n"); else printf("%s =\n", key[i]); diff --git a/src/lxc/tools/lxc_stop.c b/src/lxc/tools/lxc_stop.c index 1267fd11e..62ca465b8 100644 --- a/src/lxc/tools/lxc_stop.c +++ b/src/lxc/tools/lxc_stop.c @@ -205,8 +205,7 @@ int main(int argc, char *argv[]) /* reboot */ if (my_args.reboot) { - ret = c->reboot2(c, my_args.timeout); - if (ret < 0) + if (!c->reboot2(c, my_args.timeout)) ret = EXIT_FAILURE; else ret = EXIT_SUCCESS; diff --git a/src/lxc/tools/lxc_top.c b/src/lxc/tools/lxc_top.c index 9fad3a433..41ff10139 100644 --- a/src/lxc/tools/lxc_top.c +++ b/src/lxc/tools/lxc_top.c @@ -73,7 +73,7 @@ struct stats { struct blkio_stats io_serviced; }; -struct ct { +struct container_stats { struct lxc_container *c; struct stats *stats; }; @@ -84,7 +84,7 @@ static int delay = 3; static char sort_by = 'n'; static int sort_reverse = 0; static struct termios oldtios; -static struct ct *ct = NULL; +static struct container_stats *container_stats = NULL; static int ct_alloc_cnt = 0; static int my_parser(struct lxc_arguments *args, int c, char *arg) @@ -336,7 +336,7 @@ out: return; } -static void stats_get(struct lxc_container *c, struct ct *ct, struct stats *total) +static void stats_get(struct lxc_container *c, struct container_stats *ct, struct stats *total) { ct->c = c; ct->stats->mem_used = stat_get_int(c, "memory.usage_in_bytes"); @@ -448,8 +448,8 @@ static void stats_print(const char *name, const struct stats *stats, static int cmp_name(const void *sct1, const void *sct2) { - const struct ct *ct1 = sct1; - const struct ct *ct2 = sct2; + const struct container_stats *ct1 = sct1; + const struct container_stats *ct2 = sct2; if (sort_reverse) return strncmp(ct2->c->name, ct1->c->name, strlen(ct2->c->name)); @@ -459,8 +459,8 @@ static int cmp_name(const void *sct1, const void *sct2) static int cmp_cpuuse(const void *sct1, const void *sct2) { - const struct ct *ct1 = sct1; - const struct ct *ct2 = sct2; + const struct container_stats *ct1 = sct1; + const struct container_stats *ct2 = sct2; if (sort_reverse) return ct2->stats->cpu_use_nanos < ct1->stats->cpu_use_nanos; @@ -470,8 +470,8 @@ static int cmp_cpuuse(const void *sct1, const void *sct2) static int cmp_blkio(const void *sct1, const void *sct2) { - const struct ct *ct1 = sct1; - const struct ct *ct2 = sct2; + const struct container_stats *ct1 = sct1; + const struct container_stats *ct2 = sct2; if (sort_reverse) return ct2->stats->io_service_bytes.total < ct1->stats->io_service_bytes.total; @@ -481,8 +481,8 @@ static int cmp_blkio(const void *sct1, const void *sct2) static int cmp_memory(const void *sct1, const void *sct2) { - const struct ct *ct1 = sct1; - const struct ct *ct2 = sct2; + const struct container_stats *ct1 = sct1; + const struct container_stats *ct2 = sct2; if (sort_reverse) return ct2->stats->mem_used < ct1->stats->mem_used; @@ -492,8 +492,8 @@ static int cmp_memory(const void *sct1, const void *sct2) static int cmp_memorysw(const void *sct1, const void *sct2) { - const struct ct *ct1 = sct1; - const struct ct *ct2 = sct2; + const struct container_stats *ct1 = sct1; + const struct container_stats *ct2 = sct2; if (sort_reverse) return ct2->stats->memsw_used < ct1->stats->memsw_used; @@ -503,8 +503,8 @@ static int cmp_memorysw(const void *sct1, const void *sct2) static int cmp_kmemory(const void *sct1, const void *sct2) { - const struct ct *ct1 = sct1; - const struct ct *ct2 = sct2; + const struct container_stats *ct1 = sct1; + const struct container_stats *ct2 = sct2; if (sort_reverse) return ct2->stats->kmem_used < ct1->stats->kmem_used; @@ -526,7 +526,7 @@ static void ct_sort(int active) case 'k': cmp_func = cmp_kmemory; break; } - qsort(ct, active, sizeof(*ct), (int (*)(const void *,const void *))cmp_func); + qsort(container_stats, active, sizeof(*container_stats), (int (*)(const void *,const void *))cmp_func); } static void ct_free(void) @@ -534,13 +534,13 @@ static void ct_free(void) int i; for (i = 0; i < ct_alloc_cnt; i++) { - if (ct[i].c) { - lxc_container_put(ct[i].c); - ct[i].c = NULL; + if (container_stats[i].c) { + lxc_container_put(container_stats[i].c); + container_stats[i].c = NULL; } - free(ct[i].stats); - ct[i].stats = NULL; + free(container_stats[i].stats); + container_stats[i].stats = NULL; } } @@ -551,15 +551,15 @@ static void ct_realloc(int active_cnt) ct_free(); - ct = realloc(ct, sizeof(*ct) * active_cnt); - if (!ct) { + container_stats = realloc(container_stats, sizeof(*container_stats) * active_cnt); + if (!container_stats) { fprintf(stderr, "Cannot alloc mem\n"); exit(EXIT_FAILURE); } for (i = 0; i < active_cnt; i++) { - ct[i].stats = malloc(sizeof(*ct[0].stats)); - if (!ct[i].stats) { + container_stats[i].stats = malloc(sizeof(*container_stats[0].stats)); + if (!container_stats[i].stats) { fprintf(stderr, "Cannot alloc mem\n"); exit(EXIT_FAILURE); } @@ -640,7 +640,7 @@ int main(int argc, char *argv[]) memset(&total, 0, sizeof(total)); for (i = 0; i < active_cnt; i++) - stats_get(active[i], &ct[i], &total); + stats_get(active[i], &container_stats[i], &total); ct_sort(active_cnt); @@ -650,7 +650,7 @@ int main(int argc, char *argv[]) } for (i = 0; i < active_cnt && i < ct_print_cnt; i++) { - stats_print(ct[i].c->name, ct[i].stats, &total); + stats_print(container_stats[i].c->name, container_stats[i].stats, &total); printf("\n"); } @@ -661,8 +661,8 @@ int main(int argc, char *argv[]) fflush(stdout); for (i = 0; i < active_cnt; i++) { - lxc_container_put(ct[i].c); - ct[i].c = NULL; + lxc_container_put(container_stats[i].c); + container_stats[i].c = NULL; } in_char = '\0';