Unverified Commit ff620677 by Stéphane Graber Committed by GitHub

Merge pull request #2297 from brauner/2018-04-29/bugfixes

coverity
parents ad38dca1 e62fd16f
...@@ -622,7 +622,7 @@ copy_parent: ...@@ -622,7 +622,7 @@ copy_parent:
*lastslash = oldv; *lastslash = oldv;
free(fpath); free(fpath);
fpath = must_make_path(path, "cpuset.cpus", NULL); fpath = must_make_path(path, "cpuset.cpus", NULL);
ret = lxc_write_to_file(fpath, cpulist, strlen(cpulist), false); ret = lxc_write_to_file(fpath, cpulist, strlen(cpulist), false, 0666);
if (ret < 0) { if (ret < 0) {
SYSERROR("Failed to write cpu list to \"%s\"", fpath); SYSERROR("Failed to write cpu list to \"%s\"", fpath);
goto on_error; goto on_error;
...@@ -673,7 +673,7 @@ static bool copy_parent_file(char *path, char *file) ...@@ -673,7 +673,7 @@ static bool copy_parent_file(char *path, char *file)
*lastslash = oldv; *lastslash = oldv;
fpath = must_make_path(path, file, NULL); fpath = must_make_path(path, file, NULL);
ret = lxc_write_to_file(fpath, value, len, false); ret = lxc_write_to_file(fpath, value, len, false, 0666);
if (ret < 0) if (ret < 0)
SYSERROR("Failed to write \"%s\" to file \"%s\"", value, fpath); SYSERROR("Failed to write \"%s\" to file \"%s\"", value, fpath);
free(fpath); free(fpath);
...@@ -762,7 +762,7 @@ static bool cg_legacy_handle_cpuset_hierarchy(struct hierarchy *h, char *cgname) ...@@ -762,7 +762,7 @@ static bool cg_legacy_handle_cpuset_hierarchy(struct hierarchy *h, char *cgname)
} }
free(cgpath); free(cgpath);
ret = lxc_write_to_file(clonechildrenpath, "1", 1, false); ret = lxc_write_to_file(clonechildrenpath, "1", 1, false, 0666);
if (ret < 0) { if (ret < 0) {
/* Set clone_children so children inherit our settings */ /* Set clone_children so children inherit our settings */
SYSERROR("Failed to write 1 to \"%s\"", clonechildrenpath); SYSERROR("Failed to write 1 to \"%s\"", clonechildrenpath);
...@@ -1712,7 +1712,7 @@ static bool cg_unified_create_cgroup(struct hierarchy *h, char *cgname) ...@@ -1712,7 +1712,7 @@ static bool cg_unified_create_cgroup(struct hierarchy *h, char *cgname)
cgroup = must_append_path(cgroup, parts[i], NULL); cgroup = must_append_path(cgroup, parts[i], NULL);
target = must_make_path(cgroup, "cgroup.subtree_control", NULL); target = must_make_path(cgroup, "cgroup.subtree_control", NULL);
ret = lxc_write_to_file(target, add_controllers, full_len, false); ret = lxc_write_to_file(target, add_controllers, full_len, false, 0666);
free(target); free(target);
if (ret < 0) { if (ret < 0) {
SYSERROR("Could not enable \"%s\" controllers in the " SYSERROR("Could not enable \"%s\" controllers in the "
...@@ -1858,7 +1858,7 @@ static bool cgfsng_enter(void *hdata, pid_t pid) ...@@ -1858,7 +1858,7 @@ static bool cgfsng_enter(void *hdata, pid_t pid)
fullpath = must_make_path(hierarchies[i]->fullcgpath, fullpath = must_make_path(hierarchies[i]->fullcgpath,
"cgroup.procs", NULL); "cgroup.procs", NULL);
ret = lxc_write_to_file(fullpath, pidstr, len, false); ret = lxc_write_to_file(fullpath, pidstr, len, false, 0666);
if (ret != 0) { if (ret != 0) {
SYSERROR("Failed to enter cgroup \"%s\"", fullpath); SYSERROR("Failed to enter cgroup \"%s\"", fullpath);
free(fullpath); free(fullpath);
...@@ -2027,7 +2027,8 @@ static int cg_legacy_mount_controllers(int type, struct hierarchy *h, ...@@ -2027,7 +2027,8 @@ static int cg_legacy_mount_controllers(int type, struct hierarchy *h,
controllerpath, controllerpath,
flags | MS_REMOUNT); flags | MS_REMOUNT);
ret = mount(controllerpath, controllerpath, "cgroup", ret = mount(controllerpath, controllerpath, "cgroup",
MS_REMOUNT | MS_BIND | MS_RDONLY, NULL); remount_flags | MS_REMOUNT | MS_BIND | MS_RDONLY,
NULL);
if (ret < 0) { if (ret < 0) {
SYSERROR("Failed to remount \"%s\" ro", controllerpath); SYSERROR("Failed to remount \"%s\" ro", controllerpath);
return -1; return -1;
...@@ -2305,7 +2306,7 @@ static bool cgfsng_escape() ...@@ -2305,7 +2306,7 @@ static bool cgfsng_escape()
fullpath = must_make_path(hierarchies[i]->mountpoint, fullpath = must_make_path(hierarchies[i]->mountpoint,
hierarchies[i]->base_cgroup, hierarchies[i]->base_cgroup,
"cgroup.procs", NULL); "cgroup.procs", NULL);
ret = lxc_write_to_file(fullpath, "0", 2, false); ret = lxc_write_to_file(fullpath, "0", 2, false, 0666);
if (ret != 0) { if (ret != 0) {
SYSERROR("Failed to escape to cgroup \"%s\"", fullpath); SYSERROR("Failed to escape to cgroup \"%s\"", fullpath);
free(fullpath); free(fullpath);
...@@ -2358,7 +2359,7 @@ static bool cgfsng_unfreeze(void *hdata) ...@@ -2358,7 +2359,7 @@ static bool cgfsng_unfreeze(void *hdata)
return false; return false;
fullpath = must_make_path(h->fullcgpath, "freezer.state", NULL); fullpath = must_make_path(h->fullcgpath, "freezer.state", NULL);
ret = lxc_write_to_file(fullpath, THAWED, THAWED_LEN, false); ret = lxc_write_to_file(fullpath, THAWED, THAWED_LEN, false, 0666);
free(fullpath); free(fullpath);
if (ret < 0) if (ret < 0)
return false; return false;
...@@ -2416,7 +2417,7 @@ static int __cg_unified_attach(const struct hierarchy *h, const char *name, ...@@ -2416,7 +2417,7 @@ static int __cg_unified_attach(const struct hierarchy *h, const char *name,
base_path = must_make_path(h->mountpoint, container_cgroup, NULL); base_path = must_make_path(h->mountpoint, container_cgroup, NULL);
full_path = must_make_path(base_path, "cgroup.procs", NULL); full_path = must_make_path(base_path, "cgroup.procs", NULL);
/* cgroup is populated */ /* cgroup is populated */
ret = lxc_write_to_file(full_path, pidstr, pidstr_len, false); ret = lxc_write_to_file(full_path, pidstr, pidstr_len, false, 0666);
if (ret < 0 && errno != EBUSY) if (ret < 0 && errno != EBUSY)
goto on_error; goto on_error;
...@@ -2442,7 +2443,7 @@ static int __cg_unified_attach(const struct hierarchy *h, const char *name, ...@@ -2442,7 +2443,7 @@ static int __cg_unified_attach(const struct hierarchy *h, const char *name,
goto on_error; goto on_error;
strcat(full_path, "/cgroup.procs"); strcat(full_path, "/cgroup.procs");
ret = lxc_write_to_file(full_path, pidstr, len, false); ret = lxc_write_to_file(full_path, pidstr, len, false, 0666);
if (ret == 0) if (ret == 0)
goto on_success; goto on_success;
...@@ -2494,7 +2495,7 @@ static bool cgfsng_attach(const char *name, const char *lxcpath, pid_t pid) ...@@ -2494,7 +2495,7 @@ static bool cgfsng_attach(const char *name, const char *lxcpath, pid_t pid)
fullpath = build_full_cgpath_from_monitorpath(h, path, "cgroup.procs"); fullpath = build_full_cgpath_from_monitorpath(h, path, "cgroup.procs");
free(path); free(path);
ret = lxc_write_to_file(fullpath, pidstr, len, false); ret = lxc_write_to_file(fullpath, pidstr, len, false, 0666);
if (ret < 0) { if (ret < 0) {
SYSERROR("Failed to attach %d to %s", (int)pid, fullpath); SYSERROR("Failed to attach %d to %s", (int)pid, fullpath);
free(fullpath); free(fullpath);
...@@ -2572,7 +2573,7 @@ static int cgfsng_set(const char *filename, const char *value, const char *name, ...@@ -2572,7 +2573,7 @@ static int cgfsng_set(const char *filename, const char *value, const char *name,
char *fullpath; char *fullpath;
fullpath = build_full_cgpath_from_monitorpath(h, path, filename); fullpath = build_full_cgpath_from_monitorpath(h, path, filename);
ret = lxc_write_to_file(fullpath, value, strlen(value), false); ret = lxc_write_to_file(fullpath, value, strlen(value), false, 0666);
free(fullpath); free(fullpath);
} }
free(path); free(path);
...@@ -2697,7 +2698,7 @@ static int cg_legacy_set_data(const char *filename, const char *value, ...@@ -2697,7 +2698,7 @@ static int cg_legacy_set_data(const char *filename, const char *value,
} }
fullpath = must_make_path(h->fullcgpath, filename, NULL); fullpath = must_make_path(h->fullcgpath, filename, NULL);
ret = lxc_write_to_file(fullpath, value, strlen(value), false); ret = lxc_write_to_file(fullpath, value, strlen(value), false, 0666);
free(fullpath); free(fullpath);
return ret; return ret;
} }
...@@ -2766,7 +2767,7 @@ static bool __cg_unified_setup_limits(void *hdata, ...@@ -2766,7 +2767,7 @@ static bool __cg_unified_setup_limits(void *hdata,
struct lxc_cgroup *cg = iterator->elem; struct lxc_cgroup *cg = iterator->elem;
fullpath = must_make_path(h->fullcgpath, cg->subsystem, NULL); fullpath = must_make_path(h->fullcgpath, cg->subsystem, NULL);
ret = lxc_write_to_file(fullpath, cg->value, strlen(cg->value), false); ret = lxc_write_to_file(fullpath, cg->value, strlen(cg->value), false, 0666);
free(fullpath); free(fullpath);
if (ret < 0) { if (ret < 0) {
SYSERROR("Failed to set \"%s\" to \"%s\"", SYSERROR("Failed to set \"%s\" to \"%s\"",
......
...@@ -562,10 +562,6 @@ int pin_rootfs(const char *rootfs) ...@@ -562,10 +562,6 @@ int pin_rootfs(const char *rootfs)
if (!realpath(rootfs, absrootfs)) if (!realpath(rootfs, absrootfs))
return -2; return -2;
ret = access(absrootfs, F_OK);
if (ret != 0)
return -1;
ret = stat(absrootfs, &s); ret = stat(absrootfs, &s);
if (ret < 0) if (ret < 0)
return -1; return -1;
...@@ -581,12 +577,12 @@ int pin_rootfs(const char *rootfs) ...@@ -581,12 +577,12 @@ int pin_rootfs(const char *rootfs)
if (fd < 0) if (fd < 0)
return fd; return fd;
if (fstatfs (fd, &sfs)) { ret = fstatfs (fd, &sfs);
return -1; if (ret < 0)
} return fd;
if (sfs.f_type == NFS_SUPER_MAGIC) { if (sfs.f_type == NFS_SUPER_MAGIC) {
DEBUG("rootfs on NFS, not unlinking pin file \"%s\".", absrootfspin); DEBUG("Rootfs on NFS, not unlinking pin file \"%s\"", absrootfspin);
return fd; return fd;
} }
...@@ -2564,7 +2560,7 @@ int setup_sysctl_parameters(struct lxc_list *sysctls) ...@@ -2564,7 +2560,7 @@ int setup_sysctl_parameters(struct lxc_list *sysctls)
} }
ret = lxc_write_to_file(filename, elem->value, ret = lxc_write_to_file(filename, elem->value,
strlen(elem->value), false); strlen(elem->value), false, 0666);
if (ret < 0) { if (ret < 0) {
ERROR("Failed to setup sysctl parameters %s to %s", ERROR("Failed to setup sysctl parameters %s to %s",
elem->key, elem->value); elem->key, elem->value);
...@@ -2599,7 +2595,7 @@ int setup_proc_filesystem(struct lxc_list *procs, pid_t pid) ...@@ -2599,7 +2595,7 @@ int setup_proc_filesystem(struct lxc_list *procs, pid_t pid)
} }
ret = lxc_write_to_file(filename, elem->value, ret = lxc_write_to_file(filename, elem->value,
strlen(elem->value), false); strlen(elem->value), false, 0666);
if (ret < 0) { if (ret < 0) {
ERROR("Failed to setup proc filesystem %s to %s", ERROR("Failed to setup proc filesystem %s to %s",
elem->filename, elem->value); elem->filename, elem->value);
......
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
*/ */
#define _GNU_SOURCE #define _GNU_SOURCE
#include <arpa/inet.h>
#include <dirent.h> #include <dirent.h>
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
...@@ -29,22 +30,22 @@ ...@@ -29,22 +30,22 @@
#include <stdarg.h> #include <stdarg.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <sys/file.h>
#include <arpa/inet.h>
#include <sys/sysmacros.h>
#include <sys/mman.h> #include <sys/mman.h>
#include <sys/mount.h> #include <sys/mount.h>
#include <sys/syscall.h> #include <sys/syscall.h>
#include <sys/sysmacros.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/wait.h> #include <sys/wait.h>
#include <unistd.h>
#include "af_unix.h" #include "af_unix.h"
#include "attach.h" #include "attach.h"
#include "cgroup.h" #include "cgroup.h"
#include "conf.h"
#include "config.h"
#include "commands.h" #include "commands.h"
#include "commands_utils.h" #include "commands_utils.h"
#include "conf.h"
#include "config.h"
#include "confile.h" #include "confile.h"
#include "confile_utils.h" #include "confile_utils.h"
#include "criu.h" #include "criu.h"
...@@ -61,9 +62,9 @@ ...@@ -61,9 +62,9 @@
#include "start.h" #include "start.h"
#include "state.h" #include "state.h"
#include "storage.h" #include "storage.h"
#include "storage_utils.h"
#include "storage/btrfs.h" #include "storage/btrfs.h"
#include "storage/overlay.h" #include "storage/overlay.h"
#include "storage_utils.h"
#include "sync.h" #include "sync.h"
#include "terminal.h" #include "terminal.h"
#include "utils.h" #include "utils.h"
...@@ -140,7 +141,7 @@ static int ongoing_create(struct lxc_container *c) ...@@ -140,7 +141,7 @@ static int ongoing_create(struct lxc_container *c)
int fd, ret; int fd, ret;
size_t len; size_t len;
char *path; char *path;
struct flock lk; struct flock lk = {0};
len = strlen(c->config_path) + strlen(c->name) + 10; len = strlen(c->config_path) + strlen(c->name) + 10;
path = alloca(len); path = alloca(len);
...@@ -157,11 +158,11 @@ static int ongoing_create(struct lxc_container *c) ...@@ -157,11 +158,11 @@ static int ongoing_create(struct lxc_container *c)
lk.l_type = F_WRLCK; lk.l_type = F_WRLCK;
lk.l_whence = SEEK_SET; lk.l_whence = SEEK_SET;
lk.l_start = 0;
lk.l_len = 0;
lk.l_pid = -1; lk.l_pid = -1;
ret = fcntl(fd, F_GETLK, &lk); ret = fcntl(fd, F_OFD_GETLK, &lk);
if (ret < 0 && errno == EINVAL)
ret = flock(fd, LOCK_EX | LOCK_NB);
close(fd); close(fd);
if (ret == 0 && lk.l_pid != -1) { if (ret == 0 && lk.l_pid != -1) {
/* create is still ongoing */ /* create is still ongoing */
...@@ -177,7 +178,7 @@ static int create_partial(struct lxc_container *c) ...@@ -177,7 +178,7 @@ static int create_partial(struct lxc_container *c)
int fd, ret; int fd, ret;
size_t len; size_t len;
char *path; char *path;
struct flock lk; struct flock lk = {0};
/* $lxcpath + '/' + $name + '/partial' + \0 */ /* $lxcpath + '/' + $name + '/partial' + \0 */
len = strlen(c->config_path) + strlen(c->name) + 10; len = strlen(c->config_path) + strlen(c->name) + 10;
...@@ -192,11 +193,15 @@ static int create_partial(struct lxc_container *c) ...@@ -192,11 +193,15 @@ static int create_partial(struct lxc_container *c)
lk.l_type = F_WRLCK; lk.l_type = F_WRLCK;
lk.l_whence = SEEK_SET; lk.l_whence = SEEK_SET;
lk.l_start = 0;
lk.l_len = 0;
ret = fcntl(fd, F_SETLKW, &lk); ret = fcntl(fd, F_OFD_SETLKW, &lk);
if (ret < 0) { if (ret < 0) {
if (errno == EINVAL) {
ret = flock(fd, LOCK_EX);
if (ret == 0)
return fd;
}
SYSERROR("Failed to lock partial file %s", path); SYSERROR("Failed to lock partial file %s", path);
close(fd); close(fd);
return -1; return -1;
...@@ -340,7 +345,9 @@ int lxc_container_put(struct lxc_container *c) ...@@ -340,7 +345,9 @@ int lxc_container_put(struct lxc_container *c)
if (container_mem_lock(c)) if (container_mem_lock(c))
return -1; return -1;
if (--c->numthreads < 1) { c->numthreads--;
if (c->numthreads < 1) {
container_mem_unlock(c); container_mem_unlock(c);
lxc_container_free(c); lxc_container_free(c);
return 1; return 1;
...@@ -475,16 +482,10 @@ static bool is_stopped(struct lxc_container *c) ...@@ -475,16 +482,10 @@ static bool is_stopped(struct lxc_container *c)
static bool do_lxcapi_is_running(struct lxc_container *c) static bool do_lxcapi_is_running(struct lxc_container *c)
{ {
const char *s;
if (!c) if (!c)
return false; return false;
s = do_lxcapi_state(c); return !is_stopped(c);
if (!s || strcmp(s, "STOPPED") == 0)
return false;
return true;
} }
WRAP_API(bool, lxcapi_is_running) WRAP_API(bool, lxcapi_is_running)
...@@ -497,7 +498,7 @@ static bool do_lxcapi_freeze(struct lxc_container *c) ...@@ -497,7 +498,7 @@ static bool do_lxcapi_freeze(struct lxc_container *c)
return false; return false;
ret = lxc_freeze(c->name, c->config_path); ret = lxc_freeze(c->name, c->config_path);
if (ret) if (ret < 0)
return false; return false;
return true; return true;
...@@ -513,7 +514,7 @@ static bool do_lxcapi_unfreeze(struct lxc_container *c) ...@@ -513,7 +514,7 @@ static bool do_lxcapi_unfreeze(struct lxc_container *c)
return false; return false;
ret = lxc_unfreeze(c->name, c->config_path); ret = lxc_unfreeze(c->name, c->config_path);
if (ret) if (ret < 0)
return false; return false;
return true; return true;
...@@ -643,6 +644,7 @@ static bool do_lxcapi_want_daemonize(struct lxc_container *c, bool state) ...@@ -643,6 +644,7 @@ static bool do_lxcapi_want_daemonize(struct lxc_container *c, bool state)
return false; return false;
c->daemonize = state; c->daemonize = state;
container_mem_unlock(c); container_mem_unlock(c);
return true; return true;
...@@ -659,6 +661,7 @@ static bool do_lxcapi_want_close_all_fds(struct lxc_container *c, bool state) ...@@ -659,6 +661,7 @@ static bool do_lxcapi_want_close_all_fds(struct lxc_container *c, bool state)
return false; return false;
c->lxc_conf->close_all_fds = state; c->lxc_conf->close_all_fds = state;
container_mem_unlock(c); container_mem_unlock(c);
return true; return true;
...@@ -682,8 +685,8 @@ WRAP_API_2(bool, lxcapi_wait, const char *, int) ...@@ -682,8 +685,8 @@ WRAP_API_2(bool, lxcapi_wait, const char *, int)
static bool am_single_threaded(void) static bool am_single_threaded(void)
{ {
struct dirent *direntp;
DIR *dir; DIR *dir;
struct dirent *direntp;
int count = 0; int count = 0;
dir = opendir("/proc/self/task"); dir = opendir("/proc/self/task");
...@@ -691,13 +694,14 @@ static bool am_single_threaded(void) ...@@ -691,13 +694,14 @@ static bool am_single_threaded(void)
return false; return false;
while ((direntp = readdir(dir))) { while ((direntp = readdir(dir))) {
if (!strcmp(direntp->d_name, ".")) if (strcmp(direntp->d_name, ".") == 0)
continue; continue;
if (!strcmp(direntp->d_name, "..")) if (strcmp(direntp->d_name, "..") == 0)
continue; continue;
if (++count > 1) count++;
if (count > 1)
break; break;
} }
closedir(dir); closedir(dir);
...@@ -710,9 +714,7 @@ static void push_arg(char ***argp, char *arg, int *nargs) ...@@ -710,9 +714,7 @@ static void push_arg(char ***argp, char *arg, int *nargs)
char *copy; char *copy;
char **argv; char **argv;
do { copy = must_copy_string(arg);
copy = strdup(arg);
} while (!copy);
do { do {
argv = realloc(*argp, (*nargs + 2) * sizeof(char *)); argv = realloc(*argp, (*nargs + 2) * sizeof(char *));
...@@ -834,8 +836,6 @@ static bool do_lxcapi_start(struct lxc_container *c, int useinit, char * const a ...@@ -834,8 +836,6 @@ static bool do_lxcapi_start(struct lxc_container *c, int useinit, char * const a
int ret; int ret;
struct lxc_handler *handler; struct lxc_handler *handler;
struct lxc_conf *conf; struct lxc_conf *conf;
bool daemonize = false;
FILE *pid_fp = NULL;
char *default_args[] = { char *default_args[] = {
"/sbin/init", "/sbin/init",
NULL, NULL,
...@@ -870,11 +870,12 @@ static bool do_lxcapi_start(struct lxc_container *c, int useinit, char * const a ...@@ -870,11 +870,12 @@ static bool do_lxcapi_start(struct lxc_container *c, int useinit, char * const a
if (container_mem_lock(c)) if (container_mem_lock(c))
return false; return false;
conf = c->lxc_conf; conf = c->lxc_conf;
daemonize = c->daemonize;
/* initialize handler */ /* initialize handler */
handler = lxc_init_handler(c->name, conf, c->config_path, daemonize); handler = lxc_init_handler(c->name, conf, c->config_path, c->daemonize);
container_mem_unlock(c); container_mem_unlock(c);
if (!handler) if (!handler)
return false; return false;
...@@ -900,7 +901,7 @@ static bool do_lxcapi_start(struct lxc_container *c, int useinit, char * const a ...@@ -900,7 +901,7 @@ static bool do_lxcapi_start(struct lxc_container *c, int useinit, char * const a
* here to protect the on disk container? We don't want to exclude * here to protect the on disk container? We don't want to exclude
* things like lxc_info while the container is running. * things like lxc_info while the container is running.
*/ */
if (daemonize) { if (c->daemonize) {
bool started; bool started;
char title[2048]; char title[2048];
pid_t pid; pid_t pid;
...@@ -935,9 +936,9 @@ static bool do_lxcapi_start(struct lxc_container *c, int useinit, char * const a ...@@ -935,9 +936,9 @@ static bool do_lxcapi_start(struct lxc_container *c, int useinit, char * const a
* characters. All that it means is that the proctitle will be * characters. All that it means is that the proctitle will be
* ugly. Similarly, we also don't care if setproctitle() fails. * ugly. Similarly, we also don't care if setproctitle() fails.
* */ * */
snprintf(title, sizeof(title), "[lxc monitor] %s %s", c->config_path, c->name); (void)snprintf(title, sizeof(title), "[lxc monitor] %s %s", c->config_path, c->name);
INFO("Attempting to set proc title to %s", title); INFO("Attempting to set proc title to %s", title);
setproctitle(title); (void)setproctitle(title);
/* We fork() a second time to be reparented to init. Like /* We fork() a second time to be reparented to init. Like
* POSIX's daemon() function we change to "/" and redirect * POSIX's daemon() function we change to "/" and redirect
...@@ -997,30 +998,34 @@ static bool do_lxcapi_start(struct lxc_container *c, int useinit, char * const a ...@@ -997,30 +998,34 @@ static bool do_lxcapi_start(struct lxc_container *c, int useinit, char * const a
* write the right PID. * write the right PID.
*/ */
if (c->pidfile) { if (c->pidfile) {
pid_fp = fopen(c->pidfile, "w"); int ret, w;
if (pid_fp == NULL) { char pidstr[LXC_NUMSTRLEN64];
SYSERROR("Failed to create pidfile '%s' for '%s'",
c->pidfile, c->name); w = snprintf(pidstr, LXC_NUMSTRLEN64, "%d", (int)lxc_raw_getpid());
if (w < 0 || (size_t)w >= LXC_NUMSTRLEN64) {
free_init_cmd(init_cmd); free_init_cmd(init_cmd);
lxc_free_handler(handler); lxc_free_handler(handler);
if (daemonize)
SYSERROR("Failed to write monitor pid to \"%s\"", c->pidfile);
if (c->daemonize)
_exit(EXIT_FAILURE); _exit(EXIT_FAILURE);
return false; return false;
} }
if (fprintf(pid_fp, "%d\n", lxc_raw_getpid()) < 0) { ret = lxc_write_to_file(c->pidfile, pidstr, w, false, 0600);
SYSERROR("Failed to write '%s'", c->pidfile); if (ret < 0) {
fclose(pid_fp);
pid_fp = NULL;
free_init_cmd(init_cmd); free_init_cmd(init_cmd);
lxc_free_handler(handler); lxc_free_handler(handler);
if (daemonize)
SYSERROR("Failed to write '%s'", c->pidfile);
if (c->daemonize)
_exit(EXIT_FAILURE); _exit(EXIT_FAILURE);
return false; return false;
} }
fclose(pid_fp);
pid_fp = NULL;
} }
conf->reboot = 0; conf->reboot = 0;
...@@ -1047,7 +1052,7 @@ static bool do_lxcapi_start(struct lxc_container *c, int useinit, char * const a ...@@ -1047,7 +1052,7 @@ static bool do_lxcapi_start(struct lxc_container *c, int useinit, char * const a
reboot: reboot:
if (conf->reboot == 2) { if (conf->reboot == 2) {
/* initialize handler */ /* initialize handler */
handler = lxc_init_handler(c->name, conf, c->config_path, daemonize); handler = lxc_init_handler(c->name, conf, c->config_path, c->daemonize);
if (!handler) { if (!handler) {
ret = 1; ret = 1;
goto on_error; goto on_error;
...@@ -1057,7 +1062,7 @@ reboot: ...@@ -1057,7 +1062,7 @@ reboot:
keepfds[0] = handler->conf->maincmd_fd; keepfds[0] = handler->conf->maincmd_fd;
keepfds[1] = handler->state_socket_pair[0]; keepfds[1] = handler->state_socket_pair[0];
keepfds[2] = handler->state_socket_pair[1]; keepfds[2] = handler->state_socket_pair[1];
ret = lxc_check_inherited(conf, daemonize, keepfds, ret = lxc_check_inherited(conf, c->daemonize, keepfds,
sizeof(keepfds) / sizeof(keepfds[0])); sizeof(keepfds) / sizeof(keepfds[0]));
if (ret < 0) { if (ret < 0) {
lxc_free_handler(handler); lxc_free_handler(handler);
...@@ -1066,9 +1071,11 @@ reboot: ...@@ -1066,9 +1071,11 @@ reboot:
} }
if (useinit) if (useinit)
ret = lxc_execute(c->name, argv, 1, handler, c->config_path, daemonize, &c->error_num); ret = lxc_execute(c->name, argv, 1, handler, c->config_path,
c->daemonize, &c->error_num);
else else
ret = lxc_start(c->name, argv, handler, c->config_path, daemonize, &c->error_num); ret = lxc_start(c->name, argv, handler, c->config_path,
c->daemonize, &c->error_num);
if (conf->reboot == 1) { if (conf->reboot == 1) {
INFO("Container requested reboot"); INFO("Container requested reboot");
...@@ -1084,9 +1091,9 @@ on_error: ...@@ -1084,9 +1091,9 @@ on_error:
} }
free_init_cmd(init_cmd); free_init_cmd(init_cmd);
if (daemonize && ret != 0) if (c->daemonize && ret != 0)
_exit(EXIT_FAILURE); _exit(EXIT_FAILURE);
else if (daemonize) else if (c->daemonize)
_exit(EXIT_SUCCESS); _exit(EXIT_SUCCESS);
if (ret != 0) if (ret != 0)
...@@ -1573,6 +1580,7 @@ static bool create_run_template(struct lxc_container *c, char *tpath, ...@@ -1573,6 +1580,7 @@ static bool create_run_template(struct lxc_container *c, char *tpath,
static bool prepend_lxc_header(char *path, const char *t, char *const argv[]) static bool prepend_lxc_header(char *path, const char *t, char *const argv[])
{ {
long flen; long flen;
size_t len;
char *contents; char *contents;
FILE *f; FILE *f;
int ret = -1; int ret = -1;
...@@ -1586,15 +1594,30 @@ static bool prepend_lxc_header(char *path, const char *t, char *const argv[]) ...@@ -1586,15 +1594,30 @@ static bool prepend_lxc_header(char *path, const char *t, char *const argv[])
if (f == NULL) if (f == NULL)
return false; return false;
if (fseek(f, 0, SEEK_END) < 0) ret = fseek(f, 0, SEEK_END);
if (ret < 0)
goto out_error; goto out_error;
if ((flen = ftell(f)) < 0)
ret = -1;
flen = ftell(f);
if (flen < 0)
goto out_error; goto out_error;
if (fseek(f, 0, SEEK_SET) < 0)
ret = fseek(f, 0, SEEK_SET);
if (ret < 0)
goto out_error;
ret = fseek(f, 0, SEEK_SET);
if (ret < 0)
goto out_error; goto out_error;
if ((contents = malloc(flen + 1)) == NULL)
ret = -1;
contents = malloc(flen + 1);
if (!contents)
goto out_error; goto out_error;
if (fread(contents, 1, flen, f) != flen)
len = fread(contents, 1, flen, f);
if (len != flen)
goto out_free_contents; goto out_free_contents;
contents[flen] = '\0'; contents[flen] = '\0';
...@@ -1606,25 +1629,25 @@ static bool prepend_lxc_header(char *path, const char *t, char *const argv[]) ...@@ -1606,25 +1629,25 @@ static bool prepend_lxc_header(char *path, const char *t, char *const argv[])
#if HAVE_LIBGNUTLS #if HAVE_LIBGNUTLS
tpath = get_template_path(t); tpath = get_template_path(t);
if (!tpath) { if (!tpath) {
ERROR("bad template: %s", t); ERROR("Invalid template \"%s\" specified", t);
goto out_free_contents; goto out_free_contents;
} }
ret = sha1sum_file(tpath, md_value); ret = sha1sum_file(tpath, md_value);
if (ret < 0) {
ERROR("Error getting sha1sum of %s", tpath);
free(tpath); free(tpath);
if (ret < 0) {
ERROR("Failed to get sha1sum of %s", tpath);
goto out_free_contents; goto out_free_contents;
} }
free(tpath);
#endif #endif
f = fopen(path, "w"); f = fopen(path, "w");
if (f == NULL) { if (f == NULL) {
SYSERROR("reopening config for writing"); SYSERROR("Reopening config for writing");
free(contents); free(contents);
return false; return false;
} }
fprintf(f, "# Template used to create this container: %s\n", t); fprintf(f, "# Template used to create this container: %s\n", t);
if (argv) { if (argv) {
fprintf(f, "# Parameters passed to the template:"); fprintf(f, "# Parameters passed to the template:");
...@@ -1650,9 +1673,12 @@ static bool prepend_lxc_header(char *path, const char *t, char *const argv[]) ...@@ -1650,9 +1673,12 @@ static bool prepend_lxc_header(char *path, const char *t, char *const argv[])
fclose(f); fclose(f);
return false; return false;
} }
ret = 0; ret = 0;
out_free_contents: out_free_contents:
free(contents); free(contents);
out_error: out_error:
if (f) { if (f) {
int newret; int newret;
...@@ -1660,21 +1686,22 @@ out_error: ...@@ -1660,21 +1686,22 @@ out_error:
if (ret == 0) if (ret == 0)
ret = newret; ret = newret;
} }
if (ret < 0) { if (ret < 0) {
SYSERROR("Error prepending header"); SYSERROR("Error prepending header");
return false; return false;
} }
return true; return true;
} }
static void lxcapi_clear_config(struct lxc_container *c) static void lxcapi_clear_config(struct lxc_container *c)
{ {
if (c) { if (!c || !c->lxc_conf)
if (c->lxc_conf) { return;
lxc_conf_free(c->lxc_conf); lxc_conf_free(c->lxc_conf);
c->lxc_conf = NULL; c->lxc_conf = NULL;
}
}
} }
#define do_lxcapi_clear_config(c) lxcapi_clear_config(c) #define do_lxcapi_clear_config(c) lxcapi_clear_config(c)
......
...@@ -19,13 +19,14 @@ ...@@ -19,13 +19,14 @@
*/ */
#define _GNU_SOURCE #define _GNU_SOURCE
#include <malloc.h>
#include <stdio.h>
#include <errno.h> #include <errno.h>
#include <unistd.h>
#include <fcntl.h> #include <fcntl.h>
#include <stdlib.h> #include <malloc.h>
#include <pthread.h> #include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/file.h>
#include <unistd.h>
#include <lxc/lxccontainer.h> #include <lxc/lxccontainer.h>
......
...@@ -83,8 +83,10 @@ int lxc_file_for_each_line_mmap(const char *file, lxc_file_cb callback, ...@@ -83,8 +83,10 @@ int lxc_file_for_each_line_mmap(const char *file, lxc_file_cb callback,
return -1; return -1;
} }
if (st.st_size == 0) if (st.st_size == 0) {
close(fd);
return 0; return 0;
}
buf = lxc_strmmap(NULL, st.st_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0); buf = lxc_strmmap(NULL, st.st_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
if (buf == MAP_FAILED) { if (buf == MAP_FAILED) {
......
...@@ -667,33 +667,23 @@ void lxc_terminal_free(struct lxc_conf *conf, int fd) ...@@ -667,33 +667,23 @@ void lxc_terminal_free(struct lxc_conf *conf, int fd)
static int lxc_terminal_peer_default(struct lxc_terminal *terminal) static int lxc_terminal_peer_default(struct lxc_terminal *terminal)
{ {
struct lxc_terminal_state *ts; struct lxc_terminal_state *ts;
const char *path = terminal->path; const char *path;
int fd;
int ret = 0; int ret = 0;
if (!path) { if (terminal->path)
ret = access("/dev/tty", F_OK); path = terminal->path;
if (ret == 0) { else
/* If no terminal was given, try current controlling
* terminal, there won't be one if we were started as a
* daemon (-d).
*/
fd = open("/dev/tty", O_RDWR);
if (fd >= 0) {
close(fd);
path = "/dev/tty"; path = "/dev/tty";
}
}
}
if (!path) { terminal->peer = lxc_unpriv(open(path, O_RDWR | O_CLOEXEC));
errno = ENOTTY; if (terminal->peer < 0) {
DEBUG("The process does not have a controlling terminal"); if (!terminal->path) {
errno = ENODEV;
DEBUG("%s - The process does not have a controlling "
"terminal", strerror(errno));
goto on_succes; goto on_succes;
} }
terminal->peer = lxc_unpriv(open(path, O_RDWR | O_CLOEXEC));
if (terminal->peer < 0) {
ERROR("%s - Failed to open proxy terminal \"%s\"", ERROR("%s - Failed to open proxy terminal \"%s\"",
strerror(errno), path); strerror(errno), path);
return -ENOTTY; return -ENOTTY;
......
...@@ -109,7 +109,7 @@ static void create_helpfn(const struct lxc_arguments *args) ...@@ -109,7 +109,7 @@ static void create_helpfn(const struct lxc_arguments *args)
pid = fork(); pid = fork();
if (pid) { if (pid) {
wait_for_pid(pid); (void)wait_for_pid(pid);
return; return;
} }
......
...@@ -337,7 +337,6 @@ static int lxc_monitor_open(const char *lxcpath) ...@@ -337,7 +337,6 @@ static int lxc_monitor_open(const char *lxcpath)
int fd; int fd;
size_t retry; size_t retry;
size_t len; size_t len;
int ret = -1;
int backoff_ms[] = {10, 50, 100}; int backoff_ms[] = {10, 50, 100};
if (lxc_monitor_sock_name(lxcpath, &addr) < 0) if (lxc_monitor_sock_name(lxcpath, &addr) < 0)
...@@ -352,9 +351,9 @@ static int lxc_monitor_open(const char *lxcpath) ...@@ -352,9 +351,9 @@ static int lxc_monitor_open(const char *lxcpath)
len = strlen(&addr.sun_path[1]); len = strlen(&addr.sun_path[1]);
if (len >= sizeof(addr.sun_path) - 1) { if (len >= sizeof(addr.sun_path) - 1) {
errno = ENAMETOOLONG; errno = ENAMETOOLONG;
ret = -errno; close(fd);
fprintf(stderr, "name of monitor socket too long (%zu bytes): %s\n", len, strerror(errno)); fprintf(stderr, "name of monitor socket too long (%zu bytes): %s\n", len, strerror(errno));
goto on_error; return -errno;
} }
for (retry = 0; retry < sizeof(backoff_ms) / sizeof(backoff_ms[0]); retry++) { for (retry = 0; retry < sizeof(backoff_ms) / sizeof(backoff_ms[0]); retry++) {
...@@ -366,16 +365,11 @@ static int lxc_monitor_open(const char *lxcpath) ...@@ -366,16 +365,11 @@ static int lxc_monitor_open(const char *lxcpath)
} }
if (fd < 0) { if (fd < 0) {
ret = -errno;
fprintf(stderr, "Failed to connect to monitor socket: %s\n", strerror(errno)); fprintf(stderr, "Failed to connect to monitor socket: %s\n", strerror(errno));
goto on_error; return -errno;
} }
return fd; return fd;
on_error:
close(fd);
return ret;
} }
static int lxc_monitor_read_fdset(struct pollfd *fds, nfds_t nfds, static int lxc_monitor_read_fdset(struct pollfd *fds, nfds_t nfds,
......
...@@ -1093,9 +1093,6 @@ int rm_r(char *dirname) ...@@ -1093,9 +1093,6 @@ int rm_r(char *dirname)
char *pathname; char *pathname;
struct stat mystat; struct stat mystat;
if (!direntp)
break;
if (!strcmp(direntp->d_name, ".") || if (!strcmp(direntp->d_name, ".") ||
!strcmp(direntp->d_name, "..")) !strcmp(direntp->d_name, ".."))
continue; continue;
......
...@@ -971,12 +971,13 @@ size_t lxc_array_len(void **array) ...@@ -971,12 +971,13 @@ size_t lxc_array_len(void **array)
return result; return result;
} }
int lxc_write_to_file(const char *filename, const void* buf, size_t count, bool add_newline) int lxc_write_to_file(const char *filename, const void *buf, size_t count,
bool add_newline, mode_t mode)
{ {
int fd, saved_errno; int fd, saved_errno;
ssize_t ret; ssize_t ret;
fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT | O_CLOEXEC, 0666); fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT | O_CLOEXEC, mode);
if (fd < 0) if (fd < 0)
return -1; return -1;
ret = lxc_write_nointr(fd, buf, count); ret = lxc_write_nointr(fd, buf, count);
......
...@@ -392,7 +392,7 @@ extern int sha1sum_file(char *fnam, unsigned char *md_value); ...@@ -392,7 +392,7 @@ extern int sha1sum_file(char *fnam, unsigned char *md_value);
/* read and write whole files */ /* read and write whole files */
extern int lxc_write_to_file(const char *filename, const void *buf, extern int lxc_write_to_file(const char *filename, const void *buf,
size_t count, bool add_newline); size_t count, bool add_newline, mode_t mode);
extern int lxc_read_from_file(const char *filename, void* buf, size_t count); extern int lxc_read_from_file(const char *filename, void* buf, size_t count);
/* convert variadic argument lists to arrays (for execl type argument lists) */ /* convert variadic argument lists to arrays (for execl type argument lists) */
......
...@@ -94,7 +94,7 @@ static int do_test_file_open(struct lxc_container *c, char *fnam) ...@@ -94,7 +94,7 @@ static int do_test_file_open(struct lxc_container *c, char *fnam)
fret = 0; fret = 0;
err2: err2:
wait_for_pid(pid); (void)wait_for_pid(pid);
err1: err1:
close(pipefd[0]); close(pipefd[0]);
close(pipefd[1]); close(pipefd[1]);
......
...@@ -124,7 +124,7 @@ static int test_attach_lsm_func(struct lxc_container *ct) ...@@ -124,7 +124,7 @@ static int test_attach_lsm_func(struct lxc_container *ct)
ret = 0; ret = 0;
err2: err2:
wait_for_pid(pid); (void)wait_for_pid(pid);
err1: err1:
close(pipefd[0]); close(pipefd[0]);
close(pipefd[1]); close(pipefd[1]);
...@@ -178,7 +178,7 @@ static int test_attach_lsm_cmd(struct lxc_container *ct) ...@@ -178,7 +178,7 @@ static int test_attach_lsm_cmd(struct lxc_container *ct)
ret = 0; ret = 0;
err2: err2:
wait_for_pid(pid); (void)wait_for_pid(pid);
err1: err1:
close(pipefd[0]); close(pipefd[0]);
close(pipefd[1]); close(pipefd[1]);
...@@ -240,7 +240,7 @@ static int test_attach_func(struct lxc_container *ct) ...@@ -240,7 +240,7 @@ static int test_attach_func(struct lxc_container *ct)
ret = 0; ret = 0;
err2: err2:
wait_for_pid(pid); (void)wait_for_pid(pid);
err1: err1:
close(pipefd[0]); close(pipefd[0]);
close(pipefd[1]); close(pipefd[1]);
......
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