Commit eb6229af authored by Adrian Reber's avatar Adrian Reber Committed by Andrei Vagin

opts: use xstrdup () to assign char * options

With the previous change to dynamically allocate memory for each
possible configuration source (three different configuration files, CLI,
RPC) the char * options can no longer directly point to the character
strings extracted by getopt() as the memory might be free'd at some
point. This introduces a macro to set the char * options which first
does a xfree() and then a xstrdup().
Signed-off-by: 's avatarAdrian Reber <areber@redhat.com>
Signed-off-by: 's avatarAndrei Vagin <avagin@virtuozzo.com>
parent b427927f
...@@ -1883,7 +1883,7 @@ int new_cg_root_add(char *controller, char *newroot) ...@@ -1883,7 +1883,7 @@ int new_cg_root_add(char *controller, char *newroot)
struct cg_root_opt *o; struct cg_root_opt *o;
if (!controller) { if (!controller) {
opts.new_global_cg_root = newroot; SET_CHAR_OPTS(new_global_cg_root, newroot);
return 0; return 0;
} }
......
...@@ -567,7 +567,7 @@ int parse_options(int argc, char **argv, bool *usage_error, bool *has_exec_cmd) ...@@ -567,7 +567,7 @@ int parse_options(int argc, char **argv, bool *usage_error, bool *has_exec_cmd)
opts.show_pages_content = true; opts.show_pages_content = true;
break; break;
case 'r': case 'r':
opts.root = optarg; SET_CHAR_OPTS(root, optarg);
break; break;
case 'd': case 'd':
opts.restore_detach = true; opts.restore_detach = true;
...@@ -576,13 +576,13 @@ int parse_options(int argc, char **argv, bool *usage_error, bool *has_exec_cmd) ...@@ -576,13 +576,13 @@ int parse_options(int argc, char **argv, bool *usage_error, bool *has_exec_cmd)
opts.restore_sibling = true; opts.restore_sibling = true;
break; break;
case 'D': case 'D':
opts.imgs_dir = optarg; SET_CHAR_OPTS(imgs_dir, optarg);
break; break;
case 'W': case 'W':
opts.work_dir = optarg; SET_CHAR_OPTS(work_dir, optarg);
break; break;
case 'o': case 'o':
opts.output = optarg; SET_CHAR_OPTS(output, optarg);
break; break;
case 'J': case 'J':
if (parse_join_ns(optarg)) if (parse_join_ns(optarg))
...@@ -607,7 +607,7 @@ int parse_options(int argc, char **argv, bool *usage_error, bool *has_exec_cmd) ...@@ -607,7 +607,7 @@ int parse_options(int argc, char **argv, bool *usage_error, bool *has_exec_cmd)
break; break;
} }
case 1046: case 1046:
opts.pidfile = optarg; SET_CHAR_OPTS(pidfile, optarg);
break; break;
case 1047: case 1047:
{ {
...@@ -627,7 +627,7 @@ int parse_options(int argc, char **argv, bool *usage_error, bool *has_exec_cmd) ...@@ -627,7 +627,7 @@ int parse_options(int argc, char **argv, bool *usage_error, bool *has_exec_cmd)
return 1; return 1;
break; break;
case 1051: case 1051:
opts.addr = optarg; SET_CHAR_OPTS(addr, optarg);
break; break;
case 1052: case 1052:
opts.port = atoi(optarg); opts.port = atoi(optarg);
...@@ -641,7 +641,7 @@ int parse_options(int argc, char **argv, bool *usage_error, bool *has_exec_cmd) ...@@ -641,7 +641,7 @@ int parse_options(int argc, char **argv, bool *usage_error, bool *has_exec_cmd)
opts.handle_file_locks = true; opts.handle_file_locks = true;
break; break;
case 1053: case 1053:
opts.img_parent = optarg; SET_CHAR_OPTS(img_parent, optarg);
break; break;
case 1057: case 1057:
if (parse_cpu_cap(&opts, optarg)) if (parse_cpu_cap(&opts, optarg))
...@@ -654,6 +654,7 @@ int parse_options(int argc, char **argv, bool *usage_error, bool *has_exec_cmd) ...@@ -654,6 +654,7 @@ int parse_options(int argc, char **argv, bool *usage_error, bool *has_exec_cmd)
pr_err("--ms is deprecated; see \"Check options\" of criu --help\n"); pr_err("--ms is deprecated; see \"Check options\" of criu --help\n");
return 1; return 1;
case 'L': case 'L':
SET_CHAR_OPTS(libdir, optarg);
opts.libdir = optarg; opts.libdir = optarg;
break; break;
case 1059: case 1059:
...@@ -701,7 +702,7 @@ int parse_options(int argc, char **argv, bool *usage_error, bool *has_exec_cmd) ...@@ -701,7 +702,7 @@ int parse_options(int argc, char **argv, bool *usage_error, bool *has_exec_cmd)
return 1; return 1;
break; break;
case 1068: case 1068:
opts.freeze_cgroup = optarg; SET_CHAR_OPTS(freeze_cgroup, optarg);
break; break;
case 1069: case 1069:
opts.ghost_limit = parse_size(optarg); opts.ghost_limit = parse_size(optarg);
...@@ -711,7 +712,7 @@ int parse_options(int argc, char **argv, bool *usage_error, bool *has_exec_cmd) ...@@ -711,7 +712,7 @@ int parse_options(int argc, char **argv, bool *usage_error, bool *has_exec_cmd)
return -1; return -1;
break; break;
case 1071: case 1071:
opts.lsm_profile = optarg; SET_CHAR_OPTS(lsm_profile, optarg);
opts.lsm_supplied = true; opts.lsm_supplied = true;
break; break;
case 1072: case 1072:
...@@ -756,10 +757,10 @@ int parse_options(int argc, char **argv, bool *usage_error, bool *has_exec_cmd) ...@@ -756,10 +757,10 @@ int parse_options(int argc, char **argv, bool *usage_error, bool *has_exec_cmd)
opts.check_experimental_features = true; opts.check_experimental_features = true;
break; break;
case 1080: case 1080:
opts.cgroup_props = optarg; SET_CHAR_OPTS(cgroup_props, optarg);
break; break;
case 1081: case 1081:
opts.cgroup_props_file = optarg; SET_CHAR_OPTS(cgroup_props_file, optarg);
break; break;
case 1082: case 1082:
if (!cgp_add_dump_controller(optarg)) if (!cgp_add_dump_controller(optarg))
......
...@@ -261,7 +261,7 @@ static int setup_opts_from_req(int sk, CriuOpts *req) ...@@ -261,7 +261,7 @@ static int setup_opts_from_req(int sk, CriuOpts *req)
sprintf(images_dir_path, "/proc/%d/fd/%d", ids.pid, req->images_dir_fd); sprintf(images_dir_path, "/proc/%d/fd/%d", ids.pid, req->images_dir_fd);
if (req->parent_img) if (req->parent_img)
opts.img_parent = req->parent_img; SET_CHAR_OPTS(img_parent, req->parent_img);
if (open_image_dir(images_dir_path) < 0) { if (open_image_dir(images_dir_path) < 0) {
pr_perror("Can't open images directory"); pr_perror("Can't open images directory");
...@@ -292,9 +292,9 @@ static int setup_opts_from_req(int sk, CriuOpts *req) ...@@ -292,9 +292,9 @@ static int setup_opts_from_req(int sk, CriuOpts *req)
goto err; goto err;
} }
opts.output = req->log_file; SET_CHAR_OPTS(output, req->log_file);
} else } else
opts.output = DEFAULT_LOG_FILENAME; SET_CHAR_OPTS(output, DEFAULT_LOG_FILENAME);
log_set_loglevel(req->log_level); log_set_loglevel(req->log_level);
if (log_init(opts.output) == -1) { if (log_init(opts.output) == -1) {
...@@ -325,7 +325,7 @@ static int setup_opts_from_req(int sk, CriuOpts *req) ...@@ -325,7 +325,7 @@ static int setup_opts_from_req(int sk, CriuOpts *req)
} }
if (req->root) if (req->root)
opts.root = req->root; SET_CHAR_OPTS(root, req->root);
if (req->has_rst_sibling) { if (req->has_rst_sibling) {
if (!opts.swrk_restore) { if (!opts.swrk_restore) {
...@@ -381,7 +381,7 @@ static int setup_opts_from_req(int sk, CriuOpts *req) ...@@ -381,7 +381,7 @@ static int setup_opts_from_req(int sk, CriuOpts *req)
if (!opts.lazy_pages) { if (!opts.lazy_pages) {
opts.use_page_server = true; opts.use_page_server = true;
opts.addr = req->ps->address; SET_CHAR_OPTS(addr, req->ps->address);
if (req->ps->has_fd) { if (req->ps->has_fd) {
if (!opts.swrk_restore) if (!opts.swrk_restore)
...@@ -485,16 +485,16 @@ static int setup_opts_from_req(int sk, CriuOpts *req) ...@@ -485,16 +485,16 @@ static int setup_opts_from_req(int sk, CriuOpts *req)
} }
if (req->freeze_cgroup) if (req->freeze_cgroup)
opts.freeze_cgroup = req->freeze_cgroup; SET_CHAR_OPTS(freeze_cgroup, req->freeze_cgroup);
if (req->has_timeout) if (req->has_timeout)
opts.timeout = req->timeout; opts.timeout = req->timeout;
if (req->cgroup_props) if (req->cgroup_props)
opts.cgroup_props = req->cgroup_props; SET_CHAR_OPTS(cgroup_props, req->cgroup_props);
if (req->cgroup_props_file) if (req->cgroup_props_file)
opts.cgroup_props_file = req->cgroup_props_file; SET_CHAR_OPTS(cgroup_props_file, req->cgroup_props_file);
for (i = 0; i < req->n_cgroup_dump_controller; i++) { for (i = 0; i < req->n_cgroup_dump_controller; i++) {
if (!cgp_add_dump_controller(req->cgroup_dump_controller[i])) if (!cgp_add_dump_controller(req->cgroup_dump_controller[i]))
...@@ -1158,7 +1158,7 @@ int cr_service(bool daemon_mode) ...@@ -1158,7 +1158,7 @@ int cr_service(bool daemon_mode)
if (opts.addr == NULL) { if (opts.addr == NULL) {
pr_warn("Binding to local dir address!\n"); pr_warn("Binding to local dir address!\n");
opts.addr = CR_DEFAULT_SERVICE_ADDRESS; SET_CHAR_OPTS(addr, CR_DEFAULT_SERVICE_ADDRESS);
} }
strcpy(server_addr.sun_path, opts.addr); strcpy(server_addr.sun_path, opts.addr);
......
...@@ -175,10 +175,10 @@ int main(int argc, char *argv[], char *envp[]) ...@@ -175,10 +175,10 @@ int main(int argc, char *argv[], char *envp[])
} }
if (opts.imgs_dir == NULL) if (opts.imgs_dir == NULL)
opts.imgs_dir = "."; SET_CHAR_OPTS(imgs_dir, ".");
if (opts.work_dir == NULL) if (opts.work_dir == NULL)
opts.work_dir = opts.imgs_dir; SET_CHAR_OPTS(work_dir, opts.imgs_dir);
if (optind >= argc) { if (optind >= argc) {
pr_msg("Error: command is required\n"); pr_msg("Error: command is required\n");
......
...@@ -6,6 +6,12 @@ ...@@ -6,6 +6,12 @@
#include "common/config.h" #include "common/config.h"
#include "common/list.h" #include "common/list.h"
#define SET_CHAR_OPTS(__dest, __src) \
do { \
free(opts.__dest); \
opts.__dest = xstrdup(__src); \
} while(0)
/* /*
* CPU capability options. * CPU capability options.
*/ */
......
...@@ -230,15 +230,16 @@ int lsm_check_opts(void) ...@@ -230,15 +230,16 @@ int lsm_check_opts(void)
return -1; return -1;
} }
opts.lsm_profile = aux; SET_CHAR_OPTS(lsm_profile, aux);
} else if (strcmp(opts.lsm_profile, "selinux") == 0) { } else if (strcmp(opts.lsm_profile, "selinux") == 0) {
if (kdat.lsm != LSMTYPE__SELINUX) { if (kdat.lsm != LSMTYPE__SELINUX) {
pr_err("selinux LSM specified but selinux not supported by kernel\n"); pr_err("selinux LSM specified but selinux not supported by kernel\n");
return -1; return -1;
} }
opts.lsm_profile = aux; SET_CHAR_OPTS(lsm_profile, aux);
} else if (strcmp(opts.lsm_profile, "none") == 0) { } else if (strcmp(opts.lsm_profile, "none") == 0) {
xfree(opts.lsm_profile);
opts.lsm_profile = NULL; opts.lsm_profile = NULL;
} else { } else {
pr_err("unknown lsm %s\n", opts.lsm_profile); pr_err("unknown lsm %s\n", opts.lsm_profile);
......
...@@ -203,12 +203,12 @@ int cr_plugin_init(int stage) ...@@ -203,12 +203,12 @@ int cr_plugin_init(int stage)
if (opts.libdir == NULL) { if (opts.libdir == NULL) {
path = getenv("CRIU_LIBS_DIR"); path = getenv("CRIU_LIBS_DIR");
if (path) if (path)
opts.libdir = path; SET_CHAR_OPTS(libdir, path);
else { else {
if (access(CR_PLUGIN_DEFAULT, F_OK)) if (access(CR_PLUGIN_DEFAULT, F_OK))
return 0; return 0;
opts.libdir = CR_PLUGIN_DEFAULT; SET_CHAR_OPTS(libdir, CR_PLUGIN_DEFAULT);
} }
} }
......
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