Unverified Commit 8aa60255 by Stéphane Graber Committed by GitHub

Merge pull request #3762 from brauner/2021-03-31/fixes

fixes: Makefile, lxc-user-nic, simplify get_network_config_ops()
parents b405dec6 7707b0e0
...@@ -233,10 +233,10 @@ struct alloted_s { ...@@ -233,10 +233,10 @@ struct alloted_s {
struct alloted_s *next; struct alloted_s *next;
}; };
static struct alloted_s *append_alloted(struct alloted_s **head, char *name, static struct alloted_s *append_alloted(struct alloted_s **head, char *name, int n)
int n)
{ {
struct alloted_s *cur, *al; __do_free struct alloted_s *al = NULL;
struct alloted_s *cur;
if (!head || !name) { if (!head || !name) {
/* Sanity check. Parameters should not be null. */ /* Sanity check. Parameters should not be null. */
...@@ -244,32 +244,29 @@ static struct alloted_s *append_alloted(struct alloted_s **head, char *name, ...@@ -244,32 +244,29 @@ static struct alloted_s *append_alloted(struct alloted_s **head, char *name,
return NULL; return NULL;
} }
al = malloc(sizeof(struct alloted_s)); al = zalloc(sizeof(struct alloted_s));
if (!al) { if (!al) {
CMD_SYSERROR("Failed to allocate memory\n"); CMD_SYSERROR("Failed to allocate memory\n");
return NULL; return NULL;
} }
al->name = strdup(name); al->name = strdup(name);
if (!al->name) { if (!al->name)
free(al);
return NULL; return NULL;
}
al->allowed = n; al->allowed = n;
al->next = NULL; al->next = NULL;
if (!*head) { if (*head) {
*head = al;
return al;
}
cur = *head; cur = *head;
while (cur->next) while (cur->next)
cur = cur->next; cur = cur->next;
cur->next = al; cur->next = al;
} else {
*head = al;
}
return al; return move_ptr(al);
} }
static void free_alloted(struct alloted_s **head) static void free_alloted(struct alloted_s **head)
...@@ -321,10 +318,10 @@ static int get_alloted(char *me, char *intype, char *link, ...@@ -321,10 +318,10 @@ static int get_alloted(char *me, char *intype, char *link,
if (ret != 4) if (ret != 4)
continue; continue;
if (strlen(name) == 0) if (is_empty_string(name))
continue; continue;
if (strcmp(name, me)) { if (!strequal(name, me)) {
if (name[0] != '@') if (name[0] != '@')
continue; continue;
...@@ -332,17 +329,17 @@ static int get_alloted(char *me, char *intype, char *link, ...@@ -332,17 +329,17 @@ static int get_alloted(char *me, char *intype, char *link,
continue; continue;
} }
if (strcmp(type, intype)) if (!strequal(type, intype))
continue; continue;
if (strcmp(link, br)) if (!strequal(link, br))
continue; continue;
/* Found the user or group with the appropriate settings, /*
* therefore finish the search. What to do if there are more * Found the user or group with the appropriate settings,
* than one applicable lines? not specified in the docs. Since * therefore finish the search. What to do if there are is more
* getline is implemented with realloc, we don't need to free * than one applicable line? Currently this is not specified in
* line until exiting func. * the docs.
* *
* If append_alloted returns NULL, e.g. due to a malloc error, * If append_alloted returns NULL, e.g. due to a malloc error,
* we set count to 0 and break the loop, allowing cleanup and * we set count to 0 and break the loop, allowing cleanup and
......
...@@ -32,12 +32,15 @@ typedef int (*config_get_cb)(const char *key, char *value, int inlen, ...@@ -32,12 +32,15 @@ typedef int (*config_get_cb)(const char *key, char *value, int inlen,
typedef int (*config_clr_cb)(const char *key, struct lxc_conf *conf, typedef int (*config_clr_cb)(const char *key, struct lxc_conf *conf,
void *data); void *data);
#define LXC_CONFIG_MEMBERS \
char *name; \
bool strict; \
config_set_cb set; \
config_get_cb get; \
config_clr_cb clr
struct lxc_config_t { struct lxc_config_t {
char *name; LXC_CONFIG_MEMBERS;
bool strict;
config_set_cb set;
config_get_cb get;
config_clr_cb clr;
}; };
struct new_config_item { struct new_config_item {
......
...@@ -192,41 +192,6 @@ char *lxc_string_join(const char *sep, const char **parts, bool use_as_prefix) ...@@ -192,41 +192,6 @@ char *lxc_string_join(const char *sep, const char **parts, bool use_as_prefix)
return result; return result;
} }
char **lxc_normalize_path(const char *path)
{
char **components;
size_t components_len = 0;
size_t pos = 0;
components = lxc_string_split(path, '/');
if (!components)
return NULL;
/* resolve '.' and '..' */
for (pos = 0; pos < components_len;) {
if (strequal(components[pos], ".") ||
(strequal(components[pos], "..") && pos == 0)) {
/* eat this element */
free(components[pos]);
memmove(&components[pos], &components[pos + 1],
sizeof(char *) * (components_len - pos));
components_len--;
} else if (strequal(components[pos], "..")) {
/* eat this and the previous element */
free(components[pos - 1]);
free(components[pos]);
memmove(&components[pos - 1], &components[pos + 1],
sizeof(char *) * (components_len - pos));
components_len -= 2;
pos--;
} else {
pos++;
}
}
return components;
}
/* taken from systemd */ /* taken from systemd */
char *path_simplify(const char *path) char *path_simplify(const char *path)
{ {
...@@ -672,8 +637,9 @@ int lxc_safe_uint64(const char *numstr, uint64_t *converted, int base) ...@@ -672,8 +637,9 @@ int lxc_safe_uint64(const char *numstr, uint64_t *converted, int base)
return 0; return 0;
} }
int lxc_safe_int64_residual(const char *numstr, int64_t *converted, int base, char *residual, int lxc_safe_int64_residual(const char *restrict numstr,
size_t residual_len) int64_t *restrict converted, int base,
char *restrict residual, size_t residual_len)
{ {
char *remaining = NULL; char *remaining = NULL;
int64_t u; int64_t u;
...@@ -692,7 +658,7 @@ int lxc_safe_int64_residual(const char *numstr, int64_t *converted, int base, ch ...@@ -692,7 +658,7 @@ int lxc_safe_int64_residual(const char *numstr, int64_t *converted, int base, ch
errno = 0; errno = 0;
u = strtoll(numstr, &remaining, base); u = strtoll(numstr, &remaining, base);
if (errno == ERANGE && u == INT64_MAX) if (errno == ERANGE && u == INT64_MAX)
return -ERANGE; return ret_errno(ERANGE);
if (remaining == numstr) if (remaining == numstr)
return -EINVAL; return -EINVAL;
...@@ -705,11 +671,11 @@ int lxc_safe_int64_residual(const char *numstr, int64_t *converted, int base, ch ...@@ -705,11 +671,11 @@ int lxc_safe_int64_residual(const char *numstr, int64_t *converted, int base, ch
len = strlen(remaining); len = strlen(remaining);
if (len >= residual_len) if (len >= residual_len)
return -EINVAL; return ret_errno(EINVAL);
memcpy(residual, remaining, len); memcpy(residual, remaining, len);
} else if (*remaining != '\0') { } else if (*remaining != '\0') {
return -EINVAL; return ret_errno(EINVAL);
} }
out: out:
......
...@@ -30,21 +30,7 @@ __hidden extern char *lxc_string_replace(const char *needle, const char *replace ...@@ -30,21 +30,7 @@ __hidden extern char *lxc_string_replace(const char *needle, const char *replace
const char *haystack); const char *haystack);
__hidden extern bool lxc_string_in_array(const char *needle, const char **haystack); __hidden extern bool lxc_string_in_array(const char *needle, const char **haystack);
__hidden extern char *lxc_string_join(const char *sep, const char **parts, bool use_as_prefix); __hidden extern char *lxc_string_join(const char *sep, const char **parts, bool use_as_prefix);
/*
* Normalize and split path: Leading and trailing / are removed, multiple
* / are compactified, .. and . are resolved (.. on the top level is considered
* identical to .).
* Examples:
* / -> { NULL }
* foo/../bar -> { bar, NULL }
* ../../ -> { NULL }
* ./bar/baz/.. -> { bar, NULL }
* foo//bar -> { foo, bar, NULL }
*/
__hidden extern char **lxc_normalize_path(const char *path);
/* remove multiple slashes from the path, e.g. ///foo//bar -> /foo/bar */
__hidden extern char *lxc_deslashify(const char *path);
__hidden extern char *lxc_append_paths(const char *first, const char *second); __hidden extern char *lxc_append_paths(const char *first, const char *second);
/* /*
...@@ -78,8 +64,10 @@ __hidden extern int lxc_safe_long(const char *numstr, long int *converted); ...@@ -78,8 +64,10 @@ __hidden extern int lxc_safe_long(const char *numstr, long int *converted);
__hidden extern int lxc_safe_long_long(const char *numstr, long long int *converted); __hidden extern int lxc_safe_long_long(const char *numstr, long long int *converted);
__hidden extern int lxc_safe_ulong(const char *numstr, unsigned long *converted); __hidden extern int lxc_safe_ulong(const char *numstr, unsigned long *converted);
__hidden extern int lxc_safe_uint64(const char *numstr, uint64_t *converted, int base); __hidden extern int lxc_safe_uint64(const char *numstr, uint64_t *converted, int base);
__hidden extern int lxc_safe_int64_residual(const char *numstr, int64_t *converted, int base, __hidden extern int lxc_safe_int64_residual(const char *restrict numstr,
char *residual, size_t residual_len); int64_t *restrict converted,
int base, char *restrict residual,
size_t residual_len);
/* Handles B, kb, MB, GB. Detects overflows and reports -ERANGE. */ /* Handles B, kb, MB, GB. Detects overflows and reports -ERANGE. */
__hidden extern int parse_byte_size_string(const char *s, long long int *converted); __hidden extern int parse_byte_size_string(const char *s, long long int *converted);
......
...@@ -289,6 +289,41 @@ static int mk_rand_ovl_dirs(struct mnts *mnts, unsigned int num, struct lxc_argu ...@@ -289,6 +289,41 @@ static int mk_rand_ovl_dirs(struct mnts *mnts, unsigned int num, struct lxc_argu
return 0; return 0;
} }
static char **lxc_normalize_path(const char *path)
{
char **components;
size_t components_len = 0;
size_t pos = 0;
components = lxc_string_split(path, '/');
if (!components)
return NULL;
/* resolve '.' and '..' */
for (pos = 0; pos < components_len;) {
if (strequal(components[pos], ".") ||
(strequal(components[pos], "..") && pos == 0)) {
/* eat this element */
free(components[pos]);
memmove(&components[pos], &components[pos + 1],
sizeof(char *) * (components_len - pos));
components_len--;
} else if (strequal(components[pos], "..")) {
/* eat this and the previous element */
free(components[pos - 1]);
free(components[pos]);
memmove(&components[pos - 1], &components[pos + 1],
sizeof(char *) * (components_len - pos));
components_len -= 2;
pos--;
} else {
pos++;
}
}
return components;
}
static char *construct_path(char *path, bool as_prefix) static char *construct_path(char *path, bool as_prefix)
{ {
char **components = NULL; char **components = NULL;
......
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