Merge pull request #2493 from brauner/2018-07-26/bugfixes

utils: add lxc_iterate_parts(), compile with -Wvla and -std=gnu11
This commit is contained in:
Stéphane Graber 2018-07-26 10:56:46 -04:00 committed by GitHub
commit 38a8a5e5fa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 104 additions and 141 deletions

View File

@ -656,11 +656,8 @@ AC_PROG_SED
# See if we support thread-local storage. # See if we support thread-local storage.
LXC_CHECK_TLS LXC_CHECK_TLS
if test "x$GCC" = "xyes"; then if test "x$enable_werror" = "xyes"; then
CFLAGS="$CFLAGS -Wall" CFLAGS="$CFLAGS -Werror -Wvla -std=gnu11"
if test "x$enable_werror" = "xyes"; then
CFLAGS="$CFLAGS -Werror"
fi
fi fi
# Files requiring some variable expansion # Files requiring some variable expansion

View File

@ -575,7 +575,15 @@ int getifaddrs(struct ifaddrs **ifap)
} }
unsigned l_numLinks = countLinks(l_socket, l_linkResults) + countLinks(l_socket, l_addrResults); unsigned l_numLinks = countLinks(l_socket, l_linkResults) + countLinks(l_socket, l_addrResults);
struct ifaddrs *l_links[l_numLinks]; struct ifaddrs **l_links;
l_links = malloc(l_numLinks * sizeof(struct ifaddrs *));
if (!l_links)
{
close(l_socket);
freeResultList(l_linkResults);
return -1;
}
memset(l_links, 0, l_numLinks * sizeof(struct ifaddrs *)); memset(l_links, 0, l_numLinks * sizeof(struct ifaddrs *));
interpret(l_socket, l_linkResults, l_links, ifap); interpret(l_socket, l_linkResults, l_links, ifap);
@ -583,6 +591,7 @@ int getifaddrs(struct ifaddrs **ifap)
freeResultList(l_linkResults); freeResultList(l_linkResults);
freeResultList(l_addrResults); freeResultList(l_addrResults);
free(l_links);
close(l_socket); close(l_socket);
return 0; return 0;
} }

View File

@ -18,6 +18,7 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/ */
#include <alloca.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <mntent.h> #include <mntent.h>
@ -153,7 +154,10 @@ FILE *setmntent (const char *file, const char *mode)
/* Extend the mode parameter with "c" to disable cancellation in the /* Extend the mode parameter with "c" to disable cancellation in the
I/O functions and "e" to set FD_CLOEXEC. */ I/O functions and "e" to set FD_CLOEXEC. */
size_t modelen = strlen (mode); size_t modelen = strlen (mode);
char newmode[modelen + 3]; char *newmode;
newmode = alloca(modelen + 3);
memcpy (newmode, mode, modelen); memcpy (newmode, mode, modelen);
memcpy (newmode + modelen, "ce", 3); memcpy (newmode + modelen, "ce", 3);
FILE *result = fopen (file, newmode); FILE *result = fopen (file, newmode);

View File

@ -274,14 +274,13 @@ static uint32_t *lxc_cpumask(char *buf, size_t nbits)
char *token; char *token;
size_t arrlen; size_t arrlen;
uint32_t *bitarr; uint32_t *bitarr;
char *saveptr = NULL;
arrlen = BITS_TO_LONGS(nbits); arrlen = BITS_TO_LONGS(nbits);
bitarr = calloc(arrlen, sizeof(uint32_t)); bitarr = calloc(arrlen, sizeof(uint32_t));
if (!bitarr) if (!bitarr)
return NULL; return NULL;
for (; (token = strtok_r(buf, ",", &saveptr)); buf = NULL) { lxc_iterate_parts(token, buf, ",") {
errno = 0; errno = 0;
unsigned end, start; unsigned end, start;
char *range; char *range;
@ -728,7 +727,7 @@ static char **cg_hybrid_get_controllers(char **klist, char **nlist, char *line,
*/ */
int i; int i;
char *dup, *p2, *tok; char *dup, *p2, *tok;
char *p = line, *saveptr = NULL, *sep = ","; char *p = line, *sep = ",";
char **aret = NULL; char **aret = NULL;
for (i = 0; i < 4; i++) { for (i = 0; i < 4; i++) {
@ -755,16 +754,17 @@ static char **cg_hybrid_get_controllers(char **klist, char **nlist, char *line,
*p2 = '\0'; *p2 = '\0';
if (type == CGROUP_SUPER_MAGIC) { if (type == CGROUP_SUPER_MAGIC) {
/* strdup() here for v1 hierarchies. Otherwise strtok_r() will /* strdup() here for v1 hierarchies. Otherwise
* destroy mountpoints such as "/sys/fs/cgroup/cpu,cpuacct". * lxc_iterate_parts() will destroy mountpoints such as
* "/sys/fs/cgroup/cpu,cpuacct".
*/ */
dup = strdup(p); dup = strdup(p);
if (!dup) if (!dup)
return NULL; return NULL;
for (tok = strtok_r(dup, sep, &saveptr); tok; lxc_iterate_parts(tok, dup, sep) {
tok = strtok_r(NULL, sep, &saveptr))
must_append_controller(klist, nlist, &aret, tok); must_append_controller(klist, nlist, &aret, tok);
}
free(dup); free(dup);
} }
@ -786,15 +786,14 @@ static char **cg_unified_make_empty_controller(void)
static char **cg_unified_get_controllers(const char *file) static char **cg_unified_get_controllers(const char *file)
{ {
char *buf, *tok; char *buf, *tok;
char *saveptr = NULL, *sep = " \t\n"; char *sep = " \t\n";
char **aret = NULL; char **aret = NULL;
buf = read_file(file); buf = read_file(file);
if (!buf) if (!buf)
return NULL; return NULL;
for (tok = strtok_r(buf, sep, &saveptr); tok; lxc_iterate_parts(tok, buf, sep) {
tok = strtok_r(NULL, sep, &saveptr)) {
int newentry; int newentry;
char *copy; char *copy;
@ -878,7 +877,7 @@ static char *copy_to_eol(char *p)
*/ */
static bool controller_in_clist(char *cgline, char *c) static bool controller_in_clist(char *cgline, char *c)
{ {
char *tok, *saveptr = NULL, *eol, *tmp; char *tok, *eol, *tmp;
size_t len; size_t len;
eol = strchr(cgline, ':'); eol = strchr(cgline, ':');
@ -890,8 +889,7 @@ static bool controller_in_clist(char *cgline, char *c)
memcpy(tmp, cgline, len); memcpy(tmp, cgline, len);
tmp[len] = '\0'; tmp[len] = '\0';
for (tok = strtok_r(tmp, ",", &saveptr); tok; lxc_iterate_parts(tok, tmp, ",") {
tok = strtok_r(NULL, ",", &saveptr)) {
if (strcmp(tok, c) == 0) if (strcmp(tok, c) == 0)
return true; return true;
} }
@ -955,7 +953,7 @@ static int get_existing_subsystems(char ***klist, char ***nlist)
return -1; return -1;
while (getline(&line, &len, f) != -1) { while (getline(&line, &len, f) != -1) {
char *p, *p2, *tok, *saveptr = NULL; char *p, *p2, *tok;
p = strchr(line, ':'); p = strchr(line, ':');
if (!p) if (!p)
continue; continue;
@ -977,8 +975,7 @@ static int get_existing_subsystems(char ***klist, char ***nlist)
continue; continue;
} }
for (tok = strtok_r(p, ",", &saveptr); tok; lxc_iterate_parts(tok, p, ",") {
tok = strtok_r(NULL, ",", &saveptr)) {
if (strncmp(tok, "name=", 5) == 0) if (strncmp(tok, "name=", 5) == 0)
must_append_string(nlist, tok); must_append_string(nlist, tok);
else else
@ -2531,13 +2528,13 @@ static bool cg_init(struct cgroup_ops *ops)
tmp = lxc_global_config_value("lxc.cgroup.use"); tmp = lxc_global_config_value("lxc.cgroup.use");
if (tmp) { if (tmp) {
char *chop, *cur, *pin; char *chop, *cur, *pin;
char *saveptr = NULL;
pin = must_copy_string(tmp); pin = must_copy_string(tmp);
chop = pin; chop = pin;
for (; (cur = strtok_r(chop, ",", &saveptr)); chop = NULL) lxc_iterate_parts(cur, chop, ",") {
must_append_string(&ops->cgroup_use, cur); must_append_string(&ops->cgroup_use, cur);
}
free(pin); free(pin);
} }

View File

@ -1888,7 +1888,6 @@ static void parse_mntopt(char *opt, unsigned long *flags, char **data, size_t si
int parse_mntopts(const char *mntopts, unsigned long *mntflags, char **mntdata) int parse_mntopts(const char *mntopts, unsigned long *mntflags, char **mntdata)
{ {
char *data, *p, *s; char *data, *p, *s;
char *saveptr = NULL;
size_t size; size_t size;
*mntdata = NULL; *mntdata = NULL;
@ -1909,7 +1908,7 @@ int parse_mntopts(const char *mntopts, unsigned long *mntflags, char **mntdata)
} }
*data = 0; *data = 0;
for (; (p = strtok_r(s, ",", &saveptr)); s = NULL) lxc_iterate_parts(p, s, ",")
parse_mntopt(p, mntflags, &data, size); parse_mntopt(p, mntflags, &data, size);
if (*data) if (*data)
@ -1942,7 +1941,6 @@ static void parse_propagationopt(char *opt, unsigned long *flags)
static int parse_propagationopts(const char *mntopts, unsigned long *pflags) static int parse_propagationopts(const char *mntopts, unsigned long *pflags)
{ {
char *p, *s; char *p, *s;
char *saveptr = NULL;
if (!mntopts) if (!mntopts)
return 0; return 0;
@ -1954,7 +1952,7 @@ static int parse_propagationopts(const char *mntopts, unsigned long *pflags)
} }
*pflags = 0L; *pflags = 0L;
for (; (p = strtok_r(s, ",", &saveptr)); s = NULL) lxc_iterate_parts(p, s, ",")
parse_propagationopt(p, pflags); parse_propagationopt(p, pflags);
free(s); free(s);

View File

@ -1007,9 +1007,9 @@ static int set_config_monitor_signal_pdeath(const char *key, const char *value,
static int set_config_group(const char *key, const char *value, static int set_config_group(const char *key, const char *value,
struct lxc_conf *lxc_conf, void *data) struct lxc_conf *lxc_conf, void *data)
{ {
char *groups, *groupptr, *sptr, *token; char *groups, *token;
struct lxc_list *grouplist; struct lxc_list *grouplist;
int ret = -1; int ret = 0;
if (lxc_config_value_empty(value)) if (lxc_config_value_empty(value))
return lxc_clear_groups(lxc_conf); return lxc_clear_groups(lxc_conf);
@ -1021,20 +1021,17 @@ static int set_config_group(const char *key, const char *value,
/* In case several groups are specified in a single line split these /* In case several groups are specified in a single line split these
* groups in a single element for the list. * groups in a single element for the list.
*/ */
for (groupptr = groups;; groupptr = NULL) { lxc_iterate_parts(token, groups, " \t") {
token = strtok_r(groupptr, " \t", &sptr); grouplist = malloc(sizeof(*grouplist));
if (!token) { if (!grouplist) {
ret = 0; ret = -1;
break; break;
} }
grouplist = malloc(sizeof(*grouplist));
if (!grouplist)
break;
grouplist->elem = strdup(token); grouplist->elem = strdup(token);
if (!grouplist->elem) { if (!grouplist->elem) {
free(grouplist); free(grouplist);
ret = -1;
break; break;
} }
@ -1678,7 +1675,7 @@ static int set_config_mount_fstab(const char *key, const char *value,
static int set_config_mount_auto(const char *key, const char *value, static int set_config_mount_auto(const char *key, const char *value,
struct lxc_conf *lxc_conf, void *data) struct lxc_conf *lxc_conf, void *data)
{ {
char *autos, *autoptr, *sptr, *token; char *autos, *token;
int i; int i;
int ret = -1; int ret = -1;
static struct { static struct {
@ -1726,15 +1723,9 @@ static int set_config_mount_auto(const char *key, const char *value,
if (!autos) if (!autos)
return -1; return -1;
for (autoptr = autos;; autoptr = NULL) { lxc_iterate_parts(token, autos, " \t") {
bool is_shmounts = false; bool is_shmounts = false;
token = strtok_r(autoptr, " \t", &sptr);
if (!token) {
ret = 0;
break;
}
for (i = 0; allowed_auto_mounts[i].token; i++) { for (i = 0; allowed_auto_mounts[i].token; i++) {
if (!strcmp(allowed_auto_mounts[i].token, token)) if (!strcmp(allowed_auto_mounts[i].token, token))
break; break;
@ -1748,7 +1739,7 @@ static int set_config_mount_auto(const char *key, const char *value,
if (!allowed_auto_mounts[i].token) { if (!allowed_auto_mounts[i].token) {
ERROR("Invalid filesystem to automount \"%s\"", token); ERROR("Invalid filesystem to automount \"%s\"", token);
break; goto on_error;
} }
lxc_conf->auto_mounts &= ~allowed_auto_mounts[i].mask; lxc_conf->auto_mounts &= ~allowed_auto_mounts[i].mask;
@ -1757,23 +1748,27 @@ static int set_config_mount_auto(const char *key, const char *value,
lxc_conf->shmount.path_host = strdup(token + (sizeof("shmounts:") - 1)); lxc_conf->shmount.path_host = strdup(token + (sizeof("shmounts:") - 1));
if (!lxc_conf->shmount.path_host) { if (!lxc_conf->shmount.path_host) {
SYSERROR("Failed to copy shmounts host path"); SYSERROR("Failed to copy shmounts host path");
break; goto on_error;
} }
if (strcmp(lxc_conf->shmount.path_host, "") == 0) { if (strcmp(lxc_conf->shmount.path_host, "") == 0) {
ERROR("Invalid shmounts path: empty"); ERROR("Invalid shmounts path: empty");
break; goto on_error;
} }
lxc_conf->shmount.path_cont = strdup("/dev/.lxc-mounts"); lxc_conf->shmount.path_cont = strdup("/dev/.lxc-mounts");
if(!lxc_conf->shmount.path_cont) { if(!lxc_conf->shmount.path_cont) {
SYSERROR("Failed to copy shmounts container path"); SYSERROR("Failed to copy shmounts container path");
break; goto on_error;
} }
} }
} }
ret = 0;
on_error:
free(autos); free(autos);
return ret; return ret;
} }
@ -1809,7 +1804,7 @@ int add_elem_to_mount_list(const char *value, struct lxc_conf *lxc_conf) {
static int set_config_cap_keep(const char *key, const char *value, static int set_config_cap_keep(const char *key, const char *value,
struct lxc_conf *lxc_conf, void *data) struct lxc_conf *lxc_conf, void *data)
{ {
char *keepcaps, *keepptr, *sptr, *token; char *keepcaps, *token;
struct lxc_list *keeplist; struct lxc_list *keeplist;
int ret = -1; int ret = -1;
@ -1823,29 +1818,26 @@ static int set_config_cap_keep(const char *key, const char *value,
/* In case several capability keep is specified in a single line /* In case several capability keep is specified in a single line
* split these caps in a single element for the list. * split these caps in a single element for the list.
*/ */
for (keepptr = keepcaps;; keepptr = NULL) { lxc_iterate_parts(token, keepcaps, " \t") {
token = strtok_r(keepptr, " \t", &sptr);
if (!token) {
ret = 0;
break;
}
if (!strcmp(token, "none")) if (!strcmp(token, "none"))
lxc_clear_config_keepcaps(lxc_conf); lxc_clear_config_keepcaps(lxc_conf);
keeplist = malloc(sizeof(*keeplist)); keeplist = malloc(sizeof(*keeplist));
if (!keeplist) if (!keeplist)
break; goto on_error;
keeplist->elem = strdup(token); keeplist->elem = strdup(token);
if (!keeplist->elem) { if (!keeplist->elem) {
free(keeplist); free(keeplist);
break; goto on_error;
} }
lxc_list_add_tail(&lxc_conf->keepcaps, keeplist); lxc_list_add_tail(&lxc_conf->keepcaps, keeplist);
} }
ret = 0;
on_error:
free(keepcaps); free(keepcaps);
return ret; return ret;
@ -1854,7 +1846,7 @@ static int set_config_cap_keep(const char *key, const char *value,
static int set_config_cap_drop(const char *key, const char *value, static int set_config_cap_drop(const char *key, const char *value,
struct lxc_conf *lxc_conf, void *data) struct lxc_conf *lxc_conf, void *data)
{ {
char *dropcaps, *dropptr, *sptr, *token; char *dropcaps, *token;
struct lxc_list *droplist; struct lxc_list *droplist;
int ret = -1; int ret = -1;
@ -1868,26 +1860,23 @@ static int set_config_cap_drop(const char *key, const char *value,
/* In case several capability drop is specified in a single line /* In case several capability drop is specified in a single line
* split these caps in a single element for the list. * split these caps in a single element for the list.
*/ */
for (dropptr = dropcaps;; dropptr = NULL) { lxc_iterate_parts(token, dropcaps, " \t") {
token = strtok_r(dropptr, " \t", &sptr);
if (!token) {
ret = 0;
break;
}
droplist = malloc(sizeof(*droplist)); droplist = malloc(sizeof(*droplist));
if (!droplist) if (!droplist)
break; goto on_error;
droplist->elem = strdup(token); droplist->elem = strdup(token);
if (!droplist->elem) { if (!droplist->elem) {
free(droplist); free(droplist);
break; goto on_error;
} }
lxc_list_add_tail(&lxc_conf->caps, droplist); lxc_list_add_tail(&lxc_conf->caps, droplist);
} }
ret = 0;
on_error:
free(dropcaps); free(dropcaps);
return ret; return ret;
@ -2186,7 +2175,7 @@ static int set_config_uts_name(const char *key, const char *value,
static int set_config_namespace_clone(const char *key, const char *value, static int set_config_namespace_clone(const char *key, const char *value,
struct lxc_conf *lxc_conf, void *data) struct lxc_conf *lxc_conf, void *data)
{ {
char *ns, *nsptr, *token; char *ns, *token;
int cloneflag = 0; int cloneflag = 0;
char *saveptr = NULL; char *saveptr = NULL;
@ -2203,9 +2192,8 @@ static int set_config_namespace_clone(const char *key, const char *value,
ns = strdup(value); ns = strdup(value);
if (!ns) if (!ns)
return -1; return -1;
nsptr = ns;
for (; (token = strtok_r(nsptr, " \t", &saveptr)); nsptr = NULL) { lxc_iterate_parts(token, ns, " \t") {
token += lxc_char_left_gc(token, strlen(token)); token += lxc_char_left_gc(token, strlen(token));
token[lxc_char_right_gc(token, strlen(token))] = '\0'; token[lxc_char_right_gc(token, strlen(token))] = '\0';
cloneflag = lxc_namespace_2_cloneflag(token); cloneflag = lxc_namespace_2_cloneflag(token);
@ -2223,7 +2211,7 @@ static int set_config_namespace_clone(const char *key, const char *value,
static int set_config_namespace_keep(const char *key, const char *value, static int set_config_namespace_keep(const char *key, const char *value,
struct lxc_conf *lxc_conf, void *data) struct lxc_conf *lxc_conf, void *data)
{ {
char *ns, *nsptr, *token; char *ns, *token;
int cloneflag = 0; int cloneflag = 0;
char *saveptr = NULL; char *saveptr = NULL;
@ -2240,9 +2228,8 @@ static int set_config_namespace_keep(const char *key, const char *value,
ns = strdup(value); ns = strdup(value);
if (!ns) if (!ns)
return -1; return -1;
nsptr = ns;
for (; (token = strtok_r(nsptr, " \t", &saveptr)); nsptr = NULL) { lxc_iterate_parts(token, ns, " \t") {
token += lxc_char_left_gc(token, strlen(token)); token += lxc_char_left_gc(token, strlen(token));
token[lxc_char_right_gc(token, strlen(token))] = '\0'; token[lxc_char_right_gc(token, strlen(token))] = '\0';
cloneflag = lxc_namespace_2_cloneflag(token); cloneflag = lxc_namespace_2_cloneflag(token);
@ -2541,7 +2528,7 @@ signed long lxc_config_parse_arch(const char *arch)
int lxc_fill_elevated_privileges(char *flaglist, int *flags) int lxc_fill_elevated_privileges(char *flaglist, int *flags)
{ {
char *token, *saveptr = NULL; char *token;
int i, aflag; int i, aflag;
struct { struct {
const char *token; const char *token;
@ -2563,8 +2550,7 @@ int lxc_fill_elevated_privileges(char *flaglist, int *flags)
return 0; return 0;
} }
token = strtok_r(flaglist, "|", &saveptr); lxc_iterate_parts(token, flaglist, "|") {
while (token) {
aflag = -1; aflag = -1;
for (i = 0; all_privs[i].token; i++) for (i = 0; all_privs[i].token; i++)
@ -2575,8 +2561,6 @@ int lxc_fill_elevated_privileges(char *flaglist, int *flags)
return -1; return -1;
*flags |= aflag; *flags |= aflag;
token = strtok_r(NULL, "|", &saveptr);
} }
return 0; return 0;

View File

@ -759,7 +759,6 @@ static char **split_init_cmd(const char *incmd)
char *copy, *p; char *copy, *p;
char **argv; char **argv;
int nargs = 0; int nargs = 0;
char *saveptr = NULL;
if (!incmd) if (!incmd)
return NULL; return NULL;
@ -775,7 +774,7 @@ static char **split_init_cmd(const char *incmd)
} while (!argv); } while (!argv);
argv[0] = NULL; argv[0] = NULL;
for (; (p = strtok_r(copy, " ", &saveptr)); copy = NULL) lxc_iterate_parts(p, copy, " ")
push_arg(&argv, p, &nargs); push_arg(&argv, p, &nargs);
if (nargs == 0) { if (nargs == 0) {

View File

@ -232,7 +232,7 @@ extern int lxc_namespace_2_std_identifiers(char *namespaces)
int lxc_fill_namespace_flags(char *flaglist, int *flags) int lxc_fill_namespace_flags(char *flaglist, int *flags)
{ {
char *token, *saveptr = NULL; char *token;
int aflag; int aflag;
if (!flaglist) { if (!flaglist) {
@ -240,15 +240,12 @@ int lxc_fill_namespace_flags(char *flaglist, int *flags)
return -1; return -1;
} }
token = strtok_r(flaglist, "|", &saveptr); lxc_iterate_parts(token, flaglist, "|") {
while (token) {
aflag = lxc_namespace_2_cloneflag(token); aflag = lxc_namespace_2_cloneflag(token);
if (aflag < 0) if (aflag < 0)
return -1; return -1;
*flags |= aflag; *flags |= aflag;
token = strtok_r(NULL, "|", &saveptr);
} }
return 0; return 0;

View File

@ -68,10 +68,9 @@ int lxc_file_for_each_line_mmap(const char *file, lxc_file_cb callback,
void *data) void *data)
{ {
int fd; int fd;
char *buf, *chop, *line; char *buf, *line;
struct stat st; struct stat st;
int ret = 0; int ret = 0;
char *saveptr = NULL;
fd = open(file, O_RDONLY | O_CLOEXEC); fd = open(file, O_RDONLY | O_CLOEXEC);
if (fd < 0) if (fd < 0)
@ -94,7 +93,7 @@ int lxc_file_for_each_line_mmap(const char *file, lxc_file_cb callback,
return -1; return -1;
} }
for (chop = buf; (line = strtok_r(chop, "\n\0", &saveptr)); chop = NULL) { lxc_iterate_parts(line, buf, "\n\0") {
ret = callback(line, data); ret = callback(line, data);
if (ret) { if (ret) {
/* Callback rv > 0 means stop here callback rv < 0 means /* Callback rv > 0 means stop here callback rv < 0 means

View File

@ -43,6 +43,7 @@
#include "lxc.h" #include "lxc.h"
#include "monitor.h" #include "monitor.h"
#include "start.h" #include "start.h"
#include "utils.h"
lxc_log_define(state, lxc); lxc_log_define(state, lxc);
@ -78,16 +79,15 @@ lxc_state_t lxc_getstate(const char *name, const char *lxcpath)
static int fillwaitedstates(const char *strstates, lxc_state_t *states) static int fillwaitedstates(const char *strstates, lxc_state_t *states)
{ {
char *token, *saveptr = NULL; char *token;
char *strstates_dup = strdup(strstates); char *strstates_dup;
int state; int state;
strstates_dup = strdup(strstates);
if (!strstates_dup) if (!strstates_dup)
return -1; return -1;
token = strtok_r(strstates_dup, "|", &saveptr); lxc_iterate_parts(token, strstates_dup, "|") {
while (token) {
state = lxc_str2state(token); state = lxc_str2state(token);
if (state < 0) { if (state < 0) {
free(strstates_dup); free(strstates_dup);
@ -95,8 +95,6 @@ static int fillwaitedstates(const char *strstates, lxc_state_t *states)
} }
states[state] = 1; states[state] = 1;
token = strtok_r(NULL, "|", &saveptr);
} }
free(strstates_dup); free(strstates_dup);
return 0; return 0;

View File

@ -567,15 +567,13 @@ struct lxc_storage *storage_create(const char *dest, const char *type,
/* -B lvm,dir */ /* -B lvm,dir */
if (strchr(type, ',')) { if (strchr(type, ',')) {
char *dup, *token; char *dup, *token;
char *saveptr = NULL;
size_t len; size_t len;
len = strlen(type); len = strlen(type);
dup = alloca(len + 1); dup = alloca(len + 1);
(void)strlcpy(dup, type, len + 1); (void)strlcpy(dup, type, len + 1);
for (token = strtok_r(dup, ",", &saveptr); token; lxc_iterate_parts(token, dup, ",") {
token = strtok_r(NULL, ",", &saveptr)) {
bdev = do_storage_create(dest, token, cname, specs); bdev = do_storage_create(dest, token, cname, specs);
if (bdev) if (bdev)
return bdev; return bdev;

View File

@ -194,8 +194,6 @@ static struct lxc_list *accumulate_list(char *input, char *delimiter,
static struct lxc_list *get_list(char *input, char *delimiter) static struct lxc_list *get_list(char *input, char *delimiter)
{ {
char *workstr = NULL; char *workstr = NULL;
char *workptr = NULL;
char *sptr = NULL;
char *token = NULL; char *token = NULL;
struct lxc_list *worklist; struct lxc_list *worklist;
struct lxc_list *workstr_list; struct lxc_list *workstr_list;
@ -212,11 +210,7 @@ static struct lxc_list *get_list(char *input, char *delimiter)
return NULL; return NULL;
} }
for (workptr = workstr;; workptr = NULL) { lxc_iterate_parts(token, workstr, delimiter) {
token = strtok_r(workptr, delimiter, &sptr);
if (!token)
break;
worklist = malloc(sizeof(*worklist)); worklist = malloc(sizeof(*worklist));
if (!worklist) if (!worklist)
break; break;

View File

@ -209,10 +209,8 @@ static bool do_destroy_with_snapshots(struct lxc_container *c)
char *buf = NULL; char *buf = NULL;
char *lxcpath = NULL; char *lxcpath = NULL;
char *lxcname = NULL; char *lxcname = NULL;
char *scratch = NULL;
int fd; int fd;
int ret; int ret;
int counter = 0;
/* Destroy clones. */ /* Destroy clones. */
ret = snprintf(path, MAXPATHLEN, "%s/%s/lxc_snapshots", c->config_path, c->name); ret = snprintf(path, MAXPATHLEN, "%s/%s/lxc_snapshots", c->config_path, c->name);
@ -244,15 +242,10 @@ static bool do_destroy_with_snapshots(struct lxc_container *c)
} }
close(fd); close(fd);
while ((lxcpath = strtok_r(!counter ? buf : NULL, "\n", &scratch))) { lxc_iterate_parts(lxcpath, buf, "\n") {
if (!(lxcname = strtok_r(NULL, "\n", &scratch)))
break;
c1 = lxc_container_new(lxcname, lxcpath); c1 = lxc_container_new(lxcname, lxcpath);
if (!c1) { if (!c1)
counter++;
continue; continue;
}
/* We do not destroy recursively. If a clone of a clone /* We do not destroy recursively. If a clone of a clone
* has clones or snapshots the user should remove it * has clones or snapshots the user should remove it
@ -264,7 +257,6 @@ static bool do_destroy_with_snapshots(struct lxc_container *c)
} }
lxc_container_put(c1); lxc_container_put(c1);
counter++;
} }
free(buf); free(buf);
} }

View File

@ -806,7 +806,7 @@ char *lxc_append_paths(const char *first, const char *second)
bool lxc_string_in_list(const char *needle, const char *haystack, char _sep) bool lxc_string_in_list(const char *needle, const char *haystack, char _sep)
{ {
char *token, *str, *saveptr = NULL; char *token, *str;
char sep[2] = { _sep, '\0' }; char sep[2] = { _sep, '\0' };
size_t len; size_t len;
@ -817,17 +817,16 @@ bool lxc_string_in_list(const char *needle, const char *haystack, char _sep)
str = alloca(len + 1); str = alloca(len + 1);
(void)strlcpy(str, haystack, len + 1); (void)strlcpy(str, haystack, len + 1);
for (; (token = strtok_r(str, sep, &saveptr)); str = NULL) { lxc_iterate_parts(token, str, sep)
if (strcmp(needle, token) == 0) if (strcmp(needle, token) == 0)
return 1; return 1;
}
return 0; return 0;
} }
char **lxc_string_split(const char *string, char _sep) char **lxc_string_split(const char *string, char _sep)
{ {
char *token, *str, *saveptr = NULL; char *token, *str;
char sep[2] = {_sep, '\0'}; char sep[2] = {_sep, '\0'};
char **tmp = NULL, **result = NULL; char **tmp = NULL, **result = NULL;
size_t result_capacity = 0; size_t result_capacity = 0;
@ -842,7 +841,7 @@ char **lxc_string_split(const char *string, char _sep)
str = alloca(len + 1); str = alloca(len + 1);
(void)strlcpy(str, string, len + 1); (void)strlcpy(str, string, len + 1);
for (; (token = strtok_r(str, sep, &saveptr)); str = NULL) { lxc_iterate_parts(token, str, sep) {
r = lxc_grow_array((void ***)&result, &result_capacity, result_count + 1, 16); r = lxc_grow_array((void ***)&result, &result_capacity, result_count + 1, 16);
if (r < 0) if (r < 0)
goto error_out; goto error_out;
@ -946,7 +945,7 @@ char **lxc_string_split_quoted(char *string)
char **lxc_string_split_and_trim(const char *string, char _sep) char **lxc_string_split_and_trim(const char *string, char _sep)
{ {
char *token, *str, *saveptr = NULL; char *token, *str;
char sep[2] = { _sep, '\0' }; char sep[2] = { _sep, '\0' };
char **result = NULL; char **result = NULL;
size_t result_capacity = 0; size_t result_capacity = 0;
@ -962,7 +961,7 @@ char **lxc_string_split_and_trim(const char *string, char _sep)
str = alloca(len + 1); str = alloca(len + 1);
(void)strlcpy(str, string, len + 1); (void)strlcpy(str, string, len + 1);
for (; (token = strtok_r(str, sep, &saveptr)); str = NULL) { lxc_iterate_parts(token, str, sep) {
while (token[0] == ' ' || token[0] == '\t') while (token[0] == ' ' || token[0] == '\t')
token++; token++;
@ -1342,9 +1341,7 @@ bool detect_ramfs_rootfs(void)
} }
char *on_path(const char *cmd, const char *rootfs) { char *on_path(const char *cmd, const char *rootfs) {
char *path = NULL; char *entry = NULL, *path = NULL;
char *entry = NULL;
char *saveptr = NULL;
char cmdpath[MAXPATHLEN]; char cmdpath[MAXPATHLEN];
int ret; int ret;
@ -1356,23 +1353,18 @@ char *on_path(const char *cmd, const char *rootfs) {
if (!path) if (!path)
return NULL; return NULL;
entry = strtok_r(path, ":", &saveptr); lxc_iterate_parts(entry, path, ":") {
while (entry) {
if (rootfs) if (rootfs)
ret = snprintf(cmdpath, MAXPATHLEN, "%s/%s/%s", rootfs, entry, cmd); ret = snprintf(cmdpath, MAXPATHLEN, "%s/%s/%s", rootfs, entry, cmd);
else else
ret = snprintf(cmdpath, MAXPATHLEN, "%s/%s", entry, cmd); ret = snprintf(cmdpath, MAXPATHLEN, "%s/%s", entry, cmd);
if (ret < 0 || ret >= MAXPATHLEN) if (ret < 0 || ret >= MAXPATHLEN)
goto next_loop; continue;
if (access(cmdpath, X_OK) == 0) { if (access(cmdpath, X_OK) == 0) {
free(path); free(path);
return strdup(cmdpath); return strdup(cmdpath);
} }
next_loop:
entry = strtok_r(NULL, ":", &saveptr);
} }
free(path); free(path);

View File

@ -620,4 +620,9 @@ extern int lxc_set_death_signal(int signal);
extern int fd_cloexec(int fd, bool cloexec); extern int fd_cloexec(int fd, bool cloexec);
extern int recursive_destroy(char *dirname); extern int recursive_destroy(char *dirname);
#define lxc_iterate_parts(__iterator, __splitme, __separators) \
for (char *__p = NULL, *__it = strtok_r(__splitme, __separators, &__p); \
(__iterator = __it); \
__iterator = __it = strtok_r(NULL, __separators, &__p))
#endif /* __LXC_UTILS_H */ #endif /* __LXC_UTILS_H */

View File

@ -33,7 +33,7 @@ lxc_test_state_server_SOURCES = state_server.c lxctest.h
lxc_test_share_ns_SOURCES = share_ns.c lxctest.h lxc_test_share_ns_SOURCES = share_ns.c lxctest.h
lxc_test_criu_check_feature_SOURCES = criu_check_feature.c lxctest.h lxc_test_criu_check_feature_SOURCES = criu_check_feature.c lxctest.h
lxc_test_raw_clone_SOURCES = lxc_raw_clone.c lxctest.h lxc_test_raw_clone_SOURCES = lxc_raw_clone.c lxctest.h
lxc_test_mount_injection_SOURCES = mount_injection.c lxc_test_mount_injection_SOURCES = mount_injection.c lxctest.h
AM_CFLAGS=-DLXCROOTFSMOUNT=\"$(LXCROOTFSMOUNT)\" \ AM_CFLAGS=-DLXCROOTFSMOUNT=\"$(LXCROOTFSMOUNT)\" \
-DLXCPATH=\"$(LXCPATH)\" \ -DLXCPATH=\"$(LXCPATH)\" \

View File

@ -17,7 +17,6 @@
*/ */
#define _GNU_SOURCE
#include <fcntl.h> #include <fcntl.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -30,6 +29,7 @@
#include <sys/types.h> #include <sys/types.h>
#include <unistd.h> #include <unistd.h>
#include "config.h"
#include "lxctest.h" #include "lxctest.h"
#include "utils.h" #include "utils.h"
@ -145,9 +145,9 @@ static int perform_container_test(const char *name, const char *config_items[])
int i; int i;
char *sret; char *sret;
char template_log[sizeof(TEMPLATE)], template_dir[sizeof(TEMPLATE)], char template_log[sizeof(TEMPLATE)], template_dir[sizeof(TEMPLATE)],
device_message[sizeof("Check urandom device injected into "" - ") - 1 + strlen(name) + 1], device_message[4096],
dir_message[sizeof("Check dir "" injected into "" - ") - 1 + sizeof(TEMPLATE) - 1 + strlen(name) + 1], dir_message[4096],
fs_message[sizeof("Check devtmpfs injected into "" - ") - 1 + strlen(name) + 1]; fs_message[4096];
struct lxc_container *c; struct lxc_container *c;
struct lxc_mount mnt; struct lxc_mount mnt;
struct lxc_log log; struct lxc_log log;