Skip to content

Commit

Permalink
cgfs: remove redundancy utils
Browse files Browse the repository at this point in the history
Signed-off-by: 2xsec <dh48.jeong@samsung.com>
  • Loading branch information
2xsec authored and Christian Brauner committed Nov 22, 2018
1 parent a64c59b commit bd8357e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 26 deletions.
20 changes: 7 additions & 13 deletions src/lxc/cgroups/cgfsng.c
Expand Up @@ -84,12 +84,6 @@ static void free_string_list(char **clist)
free(clist);
}

/* Allocate a pointer, do not fail. */
static void *must_alloc(size_t sz)
{
return must_realloc(NULL, sz);
}

/* Given a pointer to a null-terminated array of pointers, realloc to add one
* entry, and point the new entry to NULL. Do not fail. Return the index to the
* second-to-last entry - that is, the one which is now available for use
Expand Down Expand Up @@ -134,7 +128,7 @@ static char *cg_legacy_must_prefix_named(char *entry)
char *prefixed;

len = strlen(entry);
prefixed = must_alloc(len + 6);
prefixed = must_realloc(NULL, len + 6);

memcpy(prefixed, "name=", STRLITERALLEN("name="));
memcpy(prefixed + STRLITERALLEN("name="), entry, len);
Expand Down Expand Up @@ -541,7 +535,7 @@ static bool copy_parent_file(char *path, char *file)
if (len <= 0)
goto on_error;

value = must_alloc(len + 1);
value = must_realloc(NULL, len + 1);
ret = lxc_read_from_file(fpath, value, len);
if (ret != len)
goto on_error;
Expand Down Expand Up @@ -825,7 +819,7 @@ static struct hierarchy *add_hierarchy(struct hierarchy ***h, char **clist, char
struct hierarchy *new;
int newentry;

new = must_alloc(sizeof(*new));
new = must_realloc(NULL, sizeof(*new));
new->controllers = clist;
new->mountpoint = mountpoint;
new->container_base_path = container_base_path;
Expand Down Expand Up @@ -863,7 +857,7 @@ static char *cg_hybrid_get_mountpoint(char *line)
*p2 = '\0';

len = strlen(p);
sret = must_alloc(len + 1);
sret = must_realloc(NULL, len + 1);
memcpy(sret, p, len);
sret[len] = '\0';
return sret;
Expand All @@ -879,7 +873,7 @@ static char *copy_to_eol(char *p)
return NULL;

len = p2 - p;
sret = must_alloc(len + 1);
sret = must_realloc(NULL, len + 1);
memcpy(sret, p, len);
sret[len] = '\0';
return sret;
Expand Down Expand Up @@ -1300,7 +1294,7 @@ __cgfsng_ops static bool cgfsng_payload_create(struct cgroup_ops *ops,
}

len = strlen(tmp) + 5; /* leave room for -NNN\0 */
container_cgroup = must_alloc(len);
container_cgroup = must_realloc(NULL, len);
(void)strlcpy(container_cgroup, tmp, len);
free(tmp);
offset = container_cgroup + len - 5;
Expand Down Expand Up @@ -1928,7 +1922,7 @@ static int __cg_unified_attach(const struct hierarchy *h, const char *name,

len = strlen(base_path) + STRLITERALLEN("/lxc-1000") +
STRLITERALLEN("/cgroup-procs");
full_path = must_alloc(len + 1);
full_path = must_realloc(NULL, len + 1);
do {
if (idx)
ret = snprintf(full_path, len + 1, "%s/lxc-%d",
Expand Down
19 changes: 6 additions & 13 deletions src/lxc/pam/pam_cgfs.c
Expand Up @@ -119,7 +119,6 @@ static inline bool is_set(unsigned bit, uint32_t *bitarr)
static bool is_lxcfs(const char *line);
static bool is_cgv1(char *line);
static bool is_cgv2(char *line);
static void *must_alloc(size_t sz);
static void must_add_to_list(char ***clist, char *entry);
static void must_append_controller(char **klist, char **nlist, char ***clist,
char *entry);
Expand Down Expand Up @@ -388,12 +387,6 @@ static void trim(char *s)
s[--len] = '\0';
}

/* Allocate pointer; do not fail. */
static void *must_alloc(size_t sz)
{
return must_realloc(NULL, sz);
}

/* Make allocated copy of string. End of string is taken to be '\n'. */
static char *copy_to_eol(char *s)
{
Expand All @@ -405,7 +398,7 @@ static char *copy_to_eol(char *s)
return NULL;

len = newline - s;
sret = must_alloc(len + 1);
sret = must_realloc(NULL, len + 1);
memcpy(sret, s, len);
sret[len] = '\0';

Expand Down Expand Up @@ -603,7 +596,7 @@ static char *get_mountpoint(char *line)
*p2 = '\0';

len = strlen(p);
sret = must_alloc(len + 1);
sret = must_realloc(NULL, len + 1);
memcpy(sret, p, len);
sret[len] = '\0';

Expand Down Expand Up @@ -775,7 +768,7 @@ static char *cgv1_must_prefix_named(char *entry)
size_t len;

len = strlen(entry);
s = must_alloc(len + 6);
s = must_realloc(NULL, len + 6);

ret = snprintf(s, len + 6, "name=%s", entry);
if (ret < 0 || (size_t)ret >= (len + 6)) {
Expand Down Expand Up @@ -937,7 +930,7 @@ static void cgv1_add_controller(char **clist, char *mountpoint, char *base_cgrou
struct cgv1_hierarchy *new;
int newentry;

new = must_alloc(sizeof(*new));
new = must_realloc(NULL, sizeof(*new));

new->controllers = clist;
new->mountpoint = mountpoint;
Expand All @@ -964,7 +957,7 @@ static void cgv2_add_controller(char **clist, char *mountpoint, char *base_cgrou
struct cgv2_hierarchy *new;
int newentry;

new = must_alloc(sizeof(*new));
new = must_realloc(NULL, sizeof(*new));

new->controllers = clist;
new->mountpoint = mountpoint;
Expand Down Expand Up @@ -1905,7 +1898,7 @@ static bool cg_copy_parent_file(char *path, char *file)
goto bad;
}

value = must_alloc(len + 1);
value = must_realloc(NULL, len + 1);
if (lxc_read_from_file(fpath, value, len) != len) {
pam_cgfs_debug("Failed to read %s: %s", fpath, strerror(errno));
goto bad;
Expand Down

0 comments on commit bd8357e

Please sign in to comment.