Skip to content

Commit

Permalink
Merge pull request #3328 from brauner/2020-03-25/fixes
Browse files Browse the repository at this point in the history
tree-wide: fixes
  • Loading branch information
stgraber committed Mar 26, 2020
2 parents 7568890 + 65146c9 commit 45d6d89
Show file tree
Hide file tree
Showing 12 changed files with 228 additions and 224 deletions.
12 changes: 7 additions & 5 deletions src/include/strlcat.c
Expand Up @@ -27,11 +27,13 @@
#include "strlcpy.h"
#endif

size_t strlcat(char *d, const char *s, size_t n)
size_t strlcat(char *src, const char *append, size_t len)
{
size_t l = strnlen(d, n);
if (l == n)
return l + strlen(s);
size_t src_len;

return l + strlcpy(d + l, s, n - l);
src_len = strnlen(src, len);
if (src_len == len)
return src_len + strlen(append);

return src_len + strlcpy(src + src_len, append, len - src_len);
}
2 changes: 1 addition & 1 deletion src/include/strlcat.h
Expand Up @@ -24,6 +24,6 @@

#include <stdio.h>

extern size_t strlcat(char *d, const char *s, size_t n);
extern size_t strlcat(char *src, const char *append, size_t len);

#endif
195 changes: 96 additions & 99 deletions src/lxc/cmd/lxc_init.c
Expand Up @@ -70,9 +70,6 @@ struct arguments {
int argc;
};

static int arguments_parse(struct arguments *my_args, int argc,
char *const argv[]);

static struct arguments my_args = {
.options = long_options,
.shortopts = short_options
Expand All @@ -90,7 +87,8 @@ static void prevent_forking(void)
return;

while (getline(&line, &len, f) != -1) {
int fd, ret;
__do_close int fd = -EBADF;
int ret;
char *p, *p2;

p = strchr(line, ':');
Expand Down Expand Up @@ -121,7 +119,7 @@ static void prevent_forking(void)
return;
}

fd = open(path, O_WRONLY);
fd = open(path, O_WRONLY | O_CLOEXEC);
if (fd < 0) {
if (my_args.quiet)
fprintf(stderr, "Failed to open \"%s\"\n", path);
Expand All @@ -132,7 +130,6 @@ static void prevent_forking(void)
if (ret != 1 && !my_args.quiet)
fprintf(stderr, "Failed to write to \"%s\"\n", path);

close(fd);
return;
}
}
Expand Down Expand Up @@ -191,6 +188,99 @@ static void remove_self(void)
return;
}

__noreturn static void print_usage_exit(const struct option longopts[])

{
fprintf(stderr, "Usage: lxc-init [-n|--name=NAME] [-h|--help] [--usage] [--version]\n\
[-q|--quiet] [-P|--lxcpath=LXCPATH]\n");
exit(EXIT_SUCCESS);
}

__noreturn static void print_version_exit(void)
{
printf("%s\n", LXC_VERSION);
exit(EXIT_SUCCESS);
}

static void print_help(void)
{
fprintf(stderr, "\
Usage: lxc-init --name=NAME -- COMMAND\n\
\n\
lxc-init start a COMMAND as PID 2 inside a container\n\
\n\
Options :\n\
-n, --name=NAME NAME of the container\n\
-q, --quiet Don't produce any output\n\
-P, --lxcpath=PATH Use specified container path\n\
-?, --help Give this help list\n\
--usage Give a short usage message\n\
--version Print the version number\n\
\n\
Mandatory or optional arguments to long options are also mandatory or optional\n\
for any corresponding short options.\n\
\n\
See the lxc-init man page for further information.\n\n");
}

static int arguments_parse(struct arguments *args, int argc,
char *const argv[])
{
for (;;) {
int c;
int index = 0;

c = getopt_long(argc, argv, args->shortopts, args->options, &index);
if (c == -1)
break;
switch (c) {
case 'n':
args->name = optarg;
break;
case 'o':
break;
case 'l':
break;
case 'q':
args->quiet = true;
break;
case 'P':
remove_trailing_slashes(optarg);
args->lxcpath = optarg;
break;
case OPT_USAGE:
print_usage_exit(args->options);
case OPT_VERSION:
print_version_exit();
case '?':
print_help();
exit(EXIT_FAILURE);
case 'h':
print_help();
exit(EXIT_SUCCESS);
}
}

/*
* Reclaim the remaining command arguments
*/
args->argv = &argv[optind];
args->argc = argc - optind;

/* If no lxcpath was given, use default */
if (!args->lxcpath)
args->lxcpath = lxc_global_config_value("lxc.lxcpath");

/* Check the command options */
if (!args->name) {
if (!args->quiet)
fprintf(stderr, "lxc-init: missing container name, use --name option\n");
return -1;
}

return 0;
}

int main(int argc, char *argv[])
{
int i, logfd, ret;
Expand Down Expand Up @@ -426,96 +516,3 @@ int main(int argc, char *argv[])
exit(EXIT_FAILURE);
exit(exit_with);
}

__noreturn static void print_usage_exit(const struct option longopts[])

{
fprintf(stderr, "Usage: lxc-init [-n|--name=NAME] [-h|--help] [--usage] [--version]\n\
[-q|--quiet] [-P|--lxcpath=LXCPATH]\n");
exit(EXIT_SUCCESS);
}

__noreturn static void print_version_exit(void)
{
printf("%s\n", LXC_VERSION);
exit(EXIT_SUCCESS);
}

static void print_help(void)
{
fprintf(stderr, "\
Usage: lxc-init --name=NAME -- COMMAND\n\
\n\
lxc-init start a COMMAND as PID 2 inside a container\n\
\n\
Options :\n\
-n, --name=NAME NAME of the container\n\
-q, --quiet Don't produce any output\n\
-P, --lxcpath=PATH Use specified container path\n\
-?, --help Give this help list\n\
--usage Give a short usage message\n\
--version Print the version number\n\
\n\
Mandatory or optional arguments to long options are also mandatory or optional\n\
for any corresponding short options.\n\
\n\
See the lxc-init man page for further information.\n\n");
}

static int arguments_parse(struct arguments *args, int argc,
char *const argv[])
{
for (;;) {
int c;
int index = 0;

c = getopt_long(argc, argv, args->shortopts, args->options, &index);
if (c == -1)
break;
switch (c) {
case 'n':
args->name = optarg;
break;
case 'o':
break;
case 'l':
break;
case 'q':
args->quiet = true;
break;
case 'P':
remove_trailing_slashes(optarg);
args->lxcpath = optarg;
break;
case OPT_USAGE:
print_usage_exit(args->options);
case OPT_VERSION:
print_version_exit();
case '?':
print_help();
exit(EXIT_FAILURE);
case 'h':
print_help();
exit(EXIT_SUCCESS);
}
}

/*
* Reclaim the remaining command arguments
*/
args->argv = &argv[optind];
args->argc = argc - optind;

/* If no lxcpath was given, use default */
if (!args->lxcpath)
args->lxcpath = lxc_global_config_value("lxc.lxcpath");

/* Check the command options */
if (!args->name) {
if (!args->quiet)
fprintf(stderr, "lxc-init: missing container name, use --name option\n");
return -1;
}

return 0;
}
68 changes: 38 additions & 30 deletions src/lxc/conf.c
Expand Up @@ -1694,61 +1694,70 @@ static int lxc_setup_console(const struct lxc_rootfs *rootfs,
return lxc_setup_ttydir_console(rootfs, console, ttydir);
}

static void parse_mntopt(char *opt, unsigned long *flags, char **data, size_t size)
static int parse_mntopt(char *opt, unsigned long *flags, char **data, size_t size)
{
struct mount_opt *mo;
ssize_t ret;

/* If '=' is contained in opt, the option must go into data. */
if (!strchr(opt, '=')) {

/* If opt is found in mount_opt, set or clear flags.
* Otherwise append it to data. */
/*
* If opt is found in mount_opt, set or clear flags.
* Otherwise append it to data.
*/
size_t opt_len = strlen(opt);
for (mo = &mount_opt[0]; mo->name != NULL; mo++) {
for (struct mount_opt *mo = &mount_opt[0]; mo->name != NULL; mo++) {
size_t mo_name_len = strlen(mo->name);

if (opt_len == mo_name_len && strncmp(opt, mo->name, mo_name_len) == 0) {
if (mo->clear)
*flags &= ~mo->flag;
else
*flags |= mo->flag;
return;
return 0;
}
}
}

if (strlen(*data))
(void)strlcat(*data, ",", size);
if (strlen(*data)) {
ret = strlcat(*data, ",", size);
if (ret < 0)
return log_error_errno(ret, errno, "Failed to append \",\" to %s", *data);
}

ret = strlcat(*data, opt, size);
if (ret < 0)
return log_error_errno(ret, errno, "Failed to append \"%s\" to %s", opt, *data);

(void)strlcat(*data, opt, size);
return 0;
}

int parse_mntopts(const char *mntopts, unsigned long *mntflags, char **mntdata)
{
__do_free char *data = NULL, *s = NULL;
char *p;
__do_free char *mntopts_new = NULL, *mntopts_dup = NULL;
char *mntopt_cur = NULL;
size_t size;

*mntdata = NULL;
*mntflags = 0L;
if (*mntdata || *mntflags)
return ret_errno(EINVAL);

if (!mntopts)
return 0;

s = strdup(mntopts);
if (!s)
return -1;
mntopts_dup = strdup(mntopts);
if (!mntopts_dup)
return ret_errno(ENOMEM);

size = strlen(s) + 1;
data = malloc(size);
if (!data)
return -1;
*data = 0;
size = strlen(mntopts_dup) + 1;
mntopts_new = zalloc(size);
if (!mntopts_new)
return ret_errno(ENOMEM);

lxc_iterate_parts(p, s, ",")
parse_mntopt(p, mntflags, &data, size);
lxc_iterate_parts(mntopt_cur, mntopts_dup, ",")
if (parse_mntopt(mntopt_cur, mntflags, &mntopts_new, size) < 0)
return ret_errno(EINVAL);

if (*data)
*mntdata = move_ptr(data);
if (*mntopts_new)
*mntdata = move_ptr(mntopts_new);

return 0;
}
Expand Down Expand Up @@ -2001,11 +2010,10 @@ static inline int mount_entry_on_generic(struct mntent *mntent,
const char *lxc_path)
{
__do_free char *mntdata = NULL;
unsigned long mntflags = 0, pflags = 0;
char *rootfs_path = NULL;
int ret;
unsigned long mntflags;
bool dev, optional, relative;
unsigned long pflags = 0;
char *rootfs_path = NULL;

optional = hasmntopt(mntent, "optional") != NULL;
dev = hasmntopt(mntent, "dev") != NULL;
Expand All @@ -2030,7 +2038,7 @@ static inline int mount_entry_on_generic(struct mntent *mntent,

ret = parse_mntopts(mntent->mnt_opts, &mntflags, &mntdata);
if (ret < 0)
return -1;
return ret;

ret = mount_entry(mntent->mnt_fsname, path, mntent->mnt_type, mntflags,
pflags, mntdata, optional, dev, relative, rootfs_path);
Expand Down
2 changes: 1 addition & 1 deletion src/lxc/confile.c
Expand Up @@ -2572,9 +2572,9 @@ static int set_config_rootfs_mount(const char *key, const char *value,
static int set_config_rootfs_options(const char *key, const char *value,
struct lxc_conf *lxc_conf, void *data)
{
int ret;
unsigned long mflags = 0, pflags = 0;
char *mdata = NULL, *opts = NULL;
int ret;
struct lxc_rootfs *rootfs = &lxc_conf->rootfs;

ret = parse_mntopts(value, &mflags, &mdata);
Expand Down

0 comments on commit 45d6d89

Please sign in to comment.