Commit 36eb9bde by Cedric Le Goater Committed by Daniel Lezcano

lxc: use new logging system

This patch defines a new log category in each file using logging and replaced the former logging routines with the new ones. Signed-off-by: 's avatarCedric Le Goater <legoater@free.fr> Acked-by: 's avatarDaniel Lezcano <dlezcano@fr.ibm.com> Signed-off-by: 's avatarDaniel Lezcano <dlezcano@fr.ibm.com>
parent cb8c5720
......@@ -37,7 +37,11 @@
#include <net/if.h>
#include "error.h"
#include <lxc/lxc.h>
#include <lxc/log.h>
lxc_log_define(lxc_cgroup, lxc);
#define MTAB "/etc/mtab"
......@@ -49,7 +53,7 @@ static int get_cgroup_mount(const char *mtab, char *mnt)
file = setmntent(mtab, "r");
if (!file) {
lxc_log_syserror("failed to open %s", mtab);
SYSERROR("failed to open %s", mtab);
goto out;
}
......@@ -74,7 +78,7 @@ int lxc_link_nsgroup(const char *name, pid_t pid)
int ret;
if (get_cgroup_mount(MTAB, cgroup)) {
lxc_log_info("cgroup is not mounted");
INFO("cgroup is not mounted");
return -1;
}
......@@ -84,7 +88,7 @@ int lxc_link_nsgroup(const char *name, pid_t pid)
unlink(lxc);
ret = symlink(nsgroup, lxc);
if (ret)
lxc_log_syserror("failed to create symlink %s->%s",
SYSERROR("failed to create symlink %s->%s",
nsgroup, lxc);
return ret;
}
......
......@@ -40,6 +40,10 @@
#include "lxc_plugin.h"
#include <lxc.h>
#include <lxc/log.h>
lxc_log_define(lxc_checkpoint, lxc);
#define MAXPIDLEN 20
int lxc_checkpoint(const char *name, const char *statefile,
......@@ -62,19 +66,19 @@ int lxc_checkpoint(const char *name, const char *statefile,
snprintf(init, MAXPATHLEN, LXCPATH "/%s/init", name);
fd = open(init, O_RDONLY);
if (fd < 0) {
lxc_log_syserror("failed to open init file for %s", name);
SYSERROR("failed to open init file for %s", name);
goto out_close;
}
if (read(fd, val, sizeof(val)) < 0) {
lxc_log_syserror("failed to read %s", init);
SYSERROR("failed to read %s", init);
goto out_close;
}
pid = atoi(val);
if (lxc_plugin_checkpoint(pid, statefile, flags) < 0) {
lxc_log_syserror("failed to checkpoint %zd", pid);
SYSERROR("failed to checkpoint %zd", pid);
goto out_close;
}
......
......@@ -35,6 +35,9 @@
#include "parse.h"
#include <lxc/lxc.h>
#include <lxc/log.h>
lxc_log_define(lxc_confile, lxc);
static int config_pts(const char *, char *, struct lxc_conf *);
static int config_tty(const char *, char *, struct lxc_conf *);
......@@ -99,7 +102,7 @@ static int config_network_type(const char *key, char *value, struct lxc_conf *lx
network = malloc(sizeof(*network));
if (!network) {
lxc_log_syserror("failed to allocate memory");
SYSERROR("failed to allocate memory");
return -1;
}
......@@ -107,7 +110,7 @@ static int config_network_type(const char *key, char *value, struct lxc_conf *lx
netdev = malloc(sizeof(*netdev));
if (!netdev) {
lxc_log_syserror("failed to allocate memory");
SYSERROR("failed to allocate memory");
return -1;
}
......@@ -118,7 +121,7 @@ static int config_network_type(const char *key, char *value, struct lxc_conf *lx
ndlist = malloc(sizeof(*ndlist));
if (!ndlist) {
lxc_log_syserror("failed to allocate memory");
SYSERROR("failed to allocate memory");
return -1;
}
......@@ -128,7 +131,7 @@ static int config_network_type(const char *key, char *value, struct lxc_conf *lx
list = malloc(sizeof(*list));
if (!list) {
lxc_log_syserror("failed to allocate memory");
SYSERROR("failed to allocate memory");
return -1;
}
......@@ -146,7 +149,7 @@ static int config_network_type(const char *key, char *value, struct lxc_conf *lx
else if (!strcmp(value, "empty"))
network->type = EMPTY;
else {
lxc_log_error("invalid network type %s", value);
ERROR("invalid network type %s", value);
return -1;
}
return 0;
......@@ -159,13 +162,13 @@ static int config_network_flags(const char *key, char *value, struct lxc_conf *l
struct lxc_netdev *netdev;
if (lxc_list_empty(networks)) {
lxc_log_error("network is not created for '%s' option", value);
ERROR("network is not created for '%s' option", value);
return -1;
}
network = lxc_list_first_elem(networks);
if (!network) {
lxc_log_error("no network defined for '%s' option", value);
ERROR("no network defined for '%s' option", value);
return -1;
}
......@@ -181,18 +184,18 @@ static int config_network_link(const char *key, char *value, struct lxc_conf *lx
struct lxc_netdev *netdev;
if (lxc_list_empty(networks)) {
lxc_log_error("network is not created for %s", value);
ERROR("network is not created for %s", value);
return -1;
}
network = lxc_list_first_elem(networks);
if (!network) {
lxc_log_error("no network defined for %s", value);
ERROR("no network defined for %s", value);
return -1;
}
if (strlen(value) > IFNAMSIZ) {
lxc_log_error("invalid interface name: %s", value);
ERROR("invalid interface name: %s", value);
return -1;
}
......@@ -208,18 +211,18 @@ static int config_network_name(const char *key, char *value, struct lxc_conf *lx
struct lxc_netdev *netdev;
if (lxc_list_empty(networks)) {
lxc_log_error("network is not created for %s", value);
ERROR("network is not created for %s", value);
return -1;
}
network = lxc_list_first_elem(networks);
if (!network) {
lxc_log_error("no network defined for %s", value);
ERROR("no network defined for %s", value);
return -1;
}
if (strlen(value) > IFNAMSIZ) {
lxc_log_error("invalid interface name: %s", value);
ERROR("invalid interface name: %s", value);
return -1;
}
......@@ -235,13 +238,13 @@ static int config_network_hwaddr(const char *key, char *value, struct lxc_conf *
struct lxc_netdev *netdev;
if (lxc_list_empty(networks)) {
lxc_log_error("network is not created for %s", value);
ERROR("network is not created for %s", value);
return -1;
}
network = lxc_list_first_elem(networks);
if (!network) {
lxc_log_error("no network defined for %s", value);
ERROR("no network defined for %s", value);
return -1;
}
......@@ -257,13 +260,13 @@ static int config_network_mtu(const char *key, char *value, struct lxc_conf *lxc
struct lxc_netdev *netdev;
if (lxc_list_empty(networks)) {
lxc_log_error("network is not created for %s", value);
ERROR("network is not created for %s", value);
return -1;
}
network = lxc_list_first_elem(networks);
if (!network) {
lxc_log_error("no network defined for %s", value);
ERROR("no network defined for %s", value);
return -1;
}
......@@ -282,31 +285,31 @@ static int config_network_ipv4(const char *key, char *value, struct lxc_conf *lx
char *cursor, *slash, *addr = NULL, *bcast = NULL, *prefix = NULL;
if (lxc_list_empty(networks)) {
lxc_log_error("network is not created for '%s'", value);
ERROR("network is not created for '%s'", value);
return -1;
}
network = lxc_list_first_elem(networks);
if (!network) {
lxc_log_error("no network defined for '%s'", value);
ERROR("no network defined for '%s'", value);
return -1;
}
netdev = lxc_list_first_elem(&network->netdev);
if (!netdev) {
lxc_log_error("no netdev defined for '%s'", value);
ERROR("no netdev defined for '%s'", value);
}
inetdev = malloc(sizeof(*inetdev));
if (!inetdev) {
lxc_log_syserror("failed to allocate ipv4 address");
SYSERROR("failed to allocate ipv4 address");
return -1;
}
memset(inetdev, 0, sizeof(*inetdev));
list = malloc(sizeof(*list));
if (!list) {
lxc_log_syserror("failed to allocate memory");
SYSERROR("failed to allocate memory");
return -1;
}
......@@ -328,18 +331,18 @@ static int config_network_ipv4(const char *key, char *value, struct lxc_conf *lx
}
if (!addr) {
lxc_log_error("no address specified");
ERROR("no address specified");
return -1;
}
if (!inet_pton(AF_INET, addr, &inetdev->addr)) {
lxc_log_syserror("invalid ipv4 address: %s", value);
SYSERROR("invalid ipv4 address: %s", value);
return -1;
}
if (bcast)
if (!inet_pton(AF_INET, bcast, &inetdev->bcast)) {
lxc_log_syserror("invalid ipv4 address: %s", value);
SYSERROR("invalid ipv4 address: %s", value);
return -1;
}
......@@ -362,26 +365,26 @@ static int config_network_ipv6(const char *key, char *value, struct lxc_conf *lx
char *netmask;
if (lxc_list_empty(networks)) {
lxc_log_error("network is not created for %s", value);
ERROR("network is not created for %s", value);
return -1;
}
network = lxc_list_first_elem(networks);
if (!network) {
lxc_log_error("no network defined for %s", value);
ERROR("no network defined for %s", value);
return -1;
}
inet6dev = malloc(sizeof(*inet6dev));
if (!inet6dev) {
lxc_log_syserror("failed to allocate ipv6 address");
SYSERROR("failed to allocate ipv6 address");
return -1;
}
memset(inet6dev, 0, sizeof(*inet6dev));
list = malloc(sizeof(*list));
if (!list) {
lxc_log_syserror("failed to allocate memory");
SYSERROR("failed to allocate memory");
return -1;
}
......@@ -396,7 +399,7 @@ static int config_network_ipv6(const char *key, char *value, struct lxc_conf *lx
}
if (!inet_pton(AF_INET6, value, &inet6dev->addr)) {
lxc_log_syserror("invalid ipv6 address: %s", value);
SYSERROR("invalid ipv6 address: %s", value);
return -1;
}
......@@ -467,13 +470,13 @@ static int config_cgroup(const char *key, char *value, struct lxc_conf *lxc_conf
static int config_mount(const char *key, char *value, struct lxc_conf *lxc_conf)
{
if (strlen(value) >= MAXPATHLEN) {
lxc_log_error("%s path is too long", value);
ERROR("%s path is too long", value);
return -1;
}
lxc_conf->fstab = strdup(value);
if (!lxc_conf->fstab) {
lxc_log_syserror("failed to duplicate string %s", value);
SYSERROR("failed to duplicate string %s", value);
return -1;
}
......@@ -483,13 +486,13 @@ static int config_mount(const char *key, char *value, struct lxc_conf *lxc_conf)
static int config_rootfs(const char *key, char *value, struct lxc_conf *lxc_conf)
{
if (strlen(value) >= MAXPATHLEN) {
lxc_log_error("%s path is too long", value);
ERROR("%s path is too long", value);
return -1;
}
lxc_conf->rootfs = strdup(value);
if (!lxc_conf->rootfs) {
lxc_log_syserror("failed to duplicate string %s", value);
SYSERROR("failed to duplicate string %s", value);
return -1;
}
......@@ -502,12 +505,12 @@ static int config_utsname(const char *key, char *value, struct lxc_conf *lxc_con
utsname = malloc(sizeof(*utsname));
if (!utsname) {
lxc_log_syserror("failed to allocate memory");
SYSERROR("failed to allocate memory");
return -1;
}
if (strlen(value) >= sizeof(utsname->nodename)) {
lxc_log_error("node name '%s' is too long",
ERROR("node name '%s' is too long",
utsname->nodename);
return -1;
}
......@@ -535,7 +538,7 @@ static int parse_line(void *buffer, void *data)
dot = strstr(line, "=");
if (!dot) {
lxc_log_error("invalid configuration line: %s", line);
ERROR("invalid configuration line: %s", line);
return -1;
}
......@@ -550,7 +553,7 @@ static int parse_line(void *buffer, void *data)
config = getconfig(key);
if (!config) {
lxc_log_error("unknow key %s", key);
ERROR("unknow key %s", key);
return -1;
}
......
......@@ -31,6 +31,10 @@
#include "af_unix.h"
#include "error.h"
#include <lxc/log.h>
lxc_log_define(lxc_console, lxc);
extern int lxc_console(const char *name, int ttynum, int *fd)
{
struct sockaddr_un addr = { 0 };
......@@ -41,24 +45,24 @@ extern int lxc_console(const char *name, int ttynum, int *fd)
sock = lxc_af_unix_connect(addr.sun_path);
if (sock < 0) {
lxc_log_error("failed to connect to the tty service");
ERROR("failed to connect to the tty service");
goto out_err;
}
ret = lxc_af_unix_send_credential(sock, &ttynum, sizeof(ttynum));
if (ret < 0) {
lxc_log_syserror("failed to send credentials");
SYSERROR("failed to send credentials");
goto out_err;
}
ret = lxc_af_unix_recv_fd(sock, fd, NULL, 0);
if (ret < 0) {
lxc_log_error("failed to connect to the tty");
ERROR("failed to connect to the tty");
goto out_err;
}
if (!ret) {
lxc_log_error("tty%d denied by '%s'", ttynum, name);
ERROR("tty%d denied by '%s'", ttynum, name);
ret = -LXC_ERROR_TTY_DENIED;
goto out_err;
}
......
......@@ -36,6 +36,11 @@
#include "error.h"
#include <lxc.h>
#include <lxc/log.h>
lxc_log_define(lxc_columbia, lxc);
#if __i386__
# define __NR_checkpoint 333
......@@ -82,13 +87,13 @@ int lxc_plugin_checkpoint(pid_t pid, const char *statefile, unsigned long flags)
fd = open(statefile, O_RDWR);
if (fd < 0) {
lxc_log_syserror("failed to open init file for %s", statefile);
SYSERROR("failed to open init file for %s", statefile);
return -1;
}
ret = sys_checkpoint(pid, fd, flags);
if (ret < 0) {
lxc_log_syserror("failed to checkpoint %d", pid);
SYSERROR("failed to checkpoint %d", pid);
goto out_close;
}
......@@ -105,12 +110,12 @@ int lxc_plugin_restart(pid_t pid, const char *statefile, unsigned long flags)
fd = open(statefile, O_RDONLY);
if (fd < 0) {
lxc_log_syserror("failed to open init file for %s", statefile);
SYSERROR("failed to open init file for %s", statefile);
return -1;
}
sys_restart(pid, fd, flags);
lxc_log_syserror("failed to restart %d", pid);
SYSERROR("failed to restart %d", pid);
close(fd);
return -1;
}
......@@ -32,6 +32,9 @@
#include "error.h"
#include <lxc/lxc.h>
#include <lxc/log.h>
lxc_log_define(lxc_create, lxc);
static int dir_filter(const struct dirent *dirent)
{
......@@ -48,7 +51,7 @@ static int is_empty_directory(const char *dirname)
n = scandir(dirname, &namelist, dir_filter, alphasort);
if (n < 0)
lxc_log_syserror("failed to scan %s directory", dirname);
SYSERROR("failed to scan %s directory", dirname);
return n == 0;
}
......@@ -57,14 +60,14 @@ static int create_lxc_directory(const char *dirname)
char path[MAXPATHLEN];
if (mkdir(LXCPATH, 0755) && errno != EEXIST) {
lxc_log_syserror("failed to create %s directory", LXCPATH);
SYSERROR("failed to create %s directory", LXCPATH);
return -errno;
}
sprintf(path, LXCPATH "/%s", dirname);
if (mkdir(path, 0755)) {
lxc_log_syserror("failed to create %s directory", path);
SYSERROR("failed to create %s directory", path);
return -errno;
}
......@@ -78,13 +81,13 @@ static int remove_lxc_directory(const char *dirname)
sprintf(path, LXCPATH "/%s", dirname);
if (rmdir(path)) {
lxc_log_syserror("failed to remove %s directory", path);
SYSERROR("failed to remove %s directory", path);
return -1;
}
if (is_empty_directory(LXCPATH)) {
if (rmdir(LXCPATH)) {
lxc_log_syserror("failed to remove %s directory", LXCPATH);
SYSERROR("failed to remove %s directory", LXCPATH);
return -1;
}
}
......@@ -107,18 +110,18 @@ int lxc_create(const char *name, struct lxc_conf *conf)
err = LXC_ERROR_INTERNAL;
if (lxc_mkstate(name)) {
lxc_log_error("failed to create the state file for %s", name);
ERROR("failed to create the state file for %s", name);
goto err;
}
if (lxc_setstate(name, STOPPED)) {
lxc_log_error("failed to set state for %s", name);
ERROR("failed to set state for %s", name);
goto err_state;
}
err = lxc_configure(name, conf);
if (err) {
lxc_log_error("failed to set configuration for %s", name);
ERROR("failed to set configuration for %s", name);
goto err_state;
}
......@@ -131,9 +134,9 @@ err_state:
lxc_unconfigure(name);
if (lxc_rmstate(name))
lxc_log_error("failed to remove state file for %s", name);
ERROR("failed to remove state file for %s", name);
err:
if (remove_lxc_directory(name))
lxc_log_error("failed to cleanup lxc directory for %s", name);
ERROR("failed to cleanup lxc directory for %s", name);
goto out;
}
......@@ -31,6 +31,9 @@
#include "error.h"
#include <lxc/lxc.h>
#include <lxc/log.h>
lxc_log_define(lxc_destroy, lxc);
static int remove_lxc_directory(const char *dirname)
{
......@@ -39,7 +42,7 @@ static int remove_lxc_directory(const char *dirname)
sprintf(path, LXCPATH "/%s", dirname);
if (rmdir(path)) {
lxc_log_syserror("failed to remove %s directory", path);
SYSERROR("failed to remove %s directory", path);
return -1;
}
......@@ -56,7 +59,7 @@ int lxc_destroy(const char *name)
return lock;
if (lxc_rmstate(name)) {
lxc_log_error("failed to remove state file for %s", name);
ERROR("failed to remove state file for %s", name);
goto out_lock;
}
......@@ -65,12 +68,12 @@ int lxc_destroy(const char *name)
lxc_unlink_nsgroup(name);
if (lxc_unconfigure(name)) {
lxc_log_error("failed to cleanup %s", name);
ERROR("failed to cleanup %s", name);
goto out_lock;
}
if (remove_lxc_directory(name)) {
lxc_log_syserror("failed to remove '%s'", name);
SYSERROR("failed to remove '%s'", name);
goto out_lock;
}
......
......@@ -33,6 +33,9 @@
#include "error.h"
#include <lxc/lxc.h>
#include <lxc/log.h>
lxc_log_define(lxc_freezer, lxc);
static int freeze_unfreeze(const char *name, int freeze)
{
......@@ -44,7 +47,7 @@ static int freeze_unfreeze(const char *name, int freeze)
fd = open(freezer, O_WRONLY);
if (fd < 0) {
lxc_log_syserror("failed to open freezer for '%s'", name);
SYSERROR("failed to open freezer for '%s'", name);
return -1;
}
......@@ -64,7 +67,7 @@ static int freeze_unfreeze(const char *name, int freeze)
close(fd);
if (ret)
lxc_log_syserror("failed to write to '%s'", freezer);
SYSERROR("failed to write to '%s'", freezer);
return 0;
}
......
......@@ -39,6 +39,9 @@
#include "error.h"
#include "lxc.h"
#include <lxc/log.h>
lxc_log_define(lxc_console, lxc);
void usage(char *cmd)
{
......@@ -77,7 +80,7 @@ int main(int argc, char *argv[])
/* Get current termios */
if (tcgetattr(0, &tios)) {
lxc_log_error("failed to get current terminal settings");
ERROR("failed to get current terminal settings");
fprintf(stderr, "%s\n", lxc_strerror(err));
return 1;
}
......@@ -94,7 +97,7 @@ int main(int argc, char *argv[])
/* Set new attributes */
if (tcsetattr(0, TCSAFLUSH, &tios)) {
lxc_log_syserror("failed to set new terminal settings");
SYSERROR("failed to set new terminal settings");
fprintf(stderr, "%s\n", lxc_strerror(err));
return 1;
}
......@@ -126,7 +129,7 @@ int main(int argc, char *argv[])
if (poll(pfd, 2, -1) < 0) {
if (errno == EINTR)
continue;
lxc_log_syserror("failed to poll");
SYSERROR("failed to poll");
goto out_err;
}
......@@ -134,7 +137,7 @@ int main(int argc, char *argv[])
*/
if (pfd[0].revents & POLLIN) {
if (read(0, &c, 1) < 0) {
lxc_log_syserror("failed to read");
SYSERROR("failed to read");
goto out_err;
}
......@@ -149,7 +152,7 @@ int main(int argc, char *argv[])
wait4q = 0;
if (write(master, &c, 1) < 0) {
lxc_log_syserror("failed to write");
SYSERROR("failed to write");
goto out_err;
}
}
......@@ -161,7 +164,7 @@ int main(int argc, char *argv[])
/* read the master and write to "stdout" */
if (pfd[1].revents & POLLIN) {
if (read(master, &c, 1) < 0) {
lxc_log_syserror("failed to read");
SYSERROR("failed to read");
goto out_err;
}
printf("%c", c);
......
......@@ -35,6 +35,9 @@
#include <net/if.h>
#include <lxc/lxc.h>
#include <lxc/log.h>
lxc_log_define(lxc_start, lxc);
void usage(char *cmd)
{
......@@ -76,7 +79,7 @@ int main(int argc, char *argv[])
usage(argv[0]);
if (tcgetattr(0, &tios)) {
lxc_log_error("failed to get current terminal settings");
ERROR("failed to get current terminal settings");
fprintf(stderr, "%s\n", lxc_strerror(err));
return 1;
}
......@@ -88,7 +91,7 @@ int main(int argc, char *argv[])
}
if (tcsetattr(0, TCSAFLUSH, &tios))
lxc_log_syserror("failed to restore terminal attributes");
SYSERROR("failed to restore terminal attributes");
return err;
}
......
......@@ -38,6 +38,9 @@
#include "error.h"
#include <lxc/lxc.h>
#include <lxc/log.h>
lxc_log_define(lxc_monitor, lxc);
#ifndef UNIX_PATH_MAX
#define UNIX_PATH_MAX 108
......@@ -59,7 +62,7 @@ int lxc_monitor(const char *name, int output_fd)
nfd = inotify_init();
if (nfd < 0) {
lxc_log_syserror("failed to initialize inotify");
SYSERROR("failed to initialize inotify");
return -1;
}
......@@ -67,7 +70,7 @@ int lxc_monitor(const char *name, int output_fd)
wfd = inotify_add_watch(nfd, path, IN_DELETE_SELF|IN_CLOSE_WRITE);
if (wfd < 0) {
lxc_log_syserror("failed to add a watch on %s", path);
SYSERROR("failed to add a watch on %s", path);
goto out;
}
......@@ -75,7 +78,7 @@ int lxc_monitor(const char *name, int output_fd)
struct inotify_event evt;
if (read(nfd, &evt, sizeof(evt)) < 0) {
lxc_log_syserror("failed to read inotify event");
SYSERROR("failed to read inotify event");
goto out;
}
......@@ -83,13 +86,13 @@ int lxc_monitor(const char *name, int output_fd)
state = lxc_getstate(name);
if (state < 0) {
lxc_log_error("failed to get the state for %s",
ERROR("failed to get the state for %s",
name);
goto out;
}
if (write(output_fd, &state, sizeof(state)) < 0) {
lxc_log_syserror("failed to send state to %d",
SYSERROR("failed to send state to %d",
output_fd);
goto out;
}
......@@ -102,7 +105,7 @@ int lxc_monitor(const char *name, int output_fd)
goto out;
}
lxc_log_error("unknown evt for inotity (%d)", evt.mask);
ERROR("unknown evt for inotity (%d)", evt.mask);
goto out;
}
......@@ -119,7 +122,7 @@ static void lxc_monitor_send(struct lxc_msg *msg)
fd = socket(PF_NETLINK, SOCK_RAW, 0);
if (fd < 0) {
lxc_log_syserror("failed to create notification socket");
SYSERROR("failed to create notification socket");
return;
}
......@@ -151,7 +154,7 @@ int lxc_monitor_open(void)
fd = socket(PF_NETLINK, SOCK_RAW, 0);
if (fd < 0) {
lxc_log_syserror("failed to create notification socket");
SYSERROR("failed to create notification socket");
return -LXC_ERROR_INTERNAL;
}
......@@ -162,7 +165,7 @@ int lxc_monitor_open(void)
addr.nl_groups = MONITOR_MCGROUP;
if (bind(fd, (const struct sockaddr *)&addr, sizeof(addr))) {
lxc_log_syserror("failed to bind to multicast group '%d'",
SYSERROR("failed to bind to multicast group '%d'",
addr.nl_groups);
close(fd);
return -1;
......@@ -180,7 +183,7 @@ int lxc_monitor_read(int fd, struct lxc_msg *msg)
ret = recvfrom(fd, msg, sizeof(*msg), 0,
(struct sockaddr *)&from, &len);
if (ret < 0) {
lxc_log_syserror("failed to received state");
SYSERROR("failed to received state");
return -LXC_ERROR_INTERNAL;
}
......
......@@ -28,6 +28,9 @@
#include "parse.h"
#include "log.h"
#include <lxc/log.h>
lxc_log_define(lxc_parse, lxc);
static int dir_filter(const struct dirent *dirent)
{
......@@ -45,13 +48,13 @@ int lxc_dir_for_each(const char *name, const char *directory,
n = scandir(directory, &namelist, dir_filter, alphasort);
if (n < 0) {
lxc_log_syserror("failed to scan %s directory", directory);
SYSERROR("failed to scan %s directory", directory);
return -1;
}
while (n--) {
if (callback(name, directory, namelist[n]->d_name, data)) {
lxc_log_error("callback failed");
ERROR("callback failed");
free(namelist[n]);
return -1;
}
......@@ -69,7 +72,7 @@ int lxc_file_for_each_line(const char *file, lxc_file_cb callback,
f = fopen(file, "r");
if (!f) {
lxc_log_syserror("failed to open %s", file);
SYSERROR("failed to open %s", file);
return -1;
}
......
......@@ -40,6 +40,9 @@
#include "error.h"
#include "lxc_plugin.h"
#include <lxc/lxc.h>
#include <lxc/log.h>
lxc_log_define(lxc_restart, lxc);
LXC_TTY_HANDLER(SIGINT);
LXC_TTY_HANDLER(SIGQUIT);
......@@ -60,19 +63,19 @@ int lxc_restart(const char *name, const char *statefile,
/* Begin the set the state to STARTING*/
if (lxc_setstate(name, STARTING)) {
lxc_log_error("failed to set state %s",
ERROR("failed to set state %s",
lxc_state2str(STARTING));
goto out;
}
if (ttyname_r(0, tty, sizeof(tty))) {
tty[0] = '\0';
lxc_log_warning("failed to get tty name");
WARN("failed to get tty name");
}
/* Synchro socketpair */
if (socketpair(AF_LOCAL, SOCK_STREAM, 0, sv)) {
lxc_log_syserror("failed to create communication socketpair");
SYSERROR("failed to create communication socketpair");
goto out;
}
......@@ -89,7 +92,7 @@ int lxc_restart(const char *name, const char *statefile,
/* Create a process in a new set of namespaces */
pid = fork_ns(clone_flags);
if (pid < 0) {
lxc_log_syserror("failed to fork into a new namespace");
SYSERROR("failed to fork into a new namespace");
goto err_fork_ns;
}
......@@ -102,30 +105,30 @@ int lxc_restart(const char *name, const char *statefile,
/* Tell our father he can begin to configure the container */
if (write(sv[0], &sync, sizeof(sync)) < 0) {
lxc_log_syserror("failed to write socket");
SYSERROR("failed to write socket");
return 1;
}
/* Wait for the father to finish the configuration */
if (read(sv[0], &sync, sizeof(sync)) < 0) {
lxc_log_syserror("failed to read socket");
SYSERROR("failed to read socket");
return 1;
}
/* Setup the container, ip, names, utsname, ... */
if (lxc_setup(name, tty, &tty_info)) {
lxc_log_error("failed to setup the container");
ERROR("failed to setup the container");
if (write(sv[0], &sync, sizeof(sync)) < 0)
lxc_log_syserror("failed to write the socket");
SYSERROR("failed to write the socket");
return -1;
}
lxc_plugin_restart(getpid(), statefile, flags);
lxc_log_syserror("failed to restart");
SYSERROR("failed to restart");
/* If the exec fails, tell that to our father */
if (write(sv[0], &sync, sizeof(sync)) < 0)
lxc_log_syserror("failed to write the socket");
SYSERROR("failed to write the socket");
exit(1);
}
......@@ -134,62 +137,62 @@ int lxc_restart(const char *name, const char *statefile,
/* Wait for the child to be ready */
if (read(sv[1], &sync, sizeof(sync)) < 0) {
lxc_log_syserror("failed to read the socket");
SYSERROR("failed to read the socket");
goto err_pipe_read;
}
/* Create the network configuration */
if (clone_flags & CLONE_NEWNET && conf_create_network(name, pid)) {
lxc_log_error("failed to create the configured network");
ERROR("failed to create the configured network");
goto err_create_network;
}
/* Tell the child to continue its initialization */
if (write(sv[1], &sync, sizeof(sync)) < 0) {
lxc_log_syserror("failed to write the socket");
SYSERROR("failed to write the socket");
goto err_pipe_write;
}
/* Wait for the child to exec or returning an error */
err = read(sv[1], &sync, sizeof(sync));
if (err < 0) {
lxc_log_error("failed to read the socket");
ERROR("failed to read the socket");
goto err_pipe_read2;
}
if (err > 0) {
lxc_log_error("something went wrong with %d", pid);
ERROR("something went wrong with %d", pid);
/* TODO : check status etc ... */
waitpid(pid, NULL, 0);
goto err_child_failed;
}
if (!asprintf(&val, "%d\n", pid)) {
lxc_log_syserror("failed to allocate memory");
SYSERROR("failed to allocate memory");
goto err_child_failed;
}
if (!asprintf(&init, LXCPATH "/%s/init", name)) {
lxc_log_syserror("failed to allocate memory");
SYSERROR("failed to allocate memory");
goto err_child_failed;
}
fd = open(init, O_WRONLY|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR);
if (fd < 0) {
lxc_log_syserror("failed to open '%s'", init);
SYSERROR("failed to open '%s'", init);
goto err_write;
}
if (write(fd, val, strlen(val)) < 0) {
lxc_log_syserror("failed to write the init pid");
SYSERROR("failed to write the init pid");
goto err_write;
}
close(fd);
if (lxc_link_nsgroup(name, pid))
lxc_log_warning("cgroupfs not found: cgroup disabled");
WARN("cgroupfs not found: cgroup disabled");
if (lxc_setstate(name, RUNNING)) {
lxc_log_error("failed to set state to %s",
ERROR("failed to set state to %s",
lxc_state2str(RUNNING));
goto err_state_failed;
}
......@@ -198,20 +201,20 @@ wait_again:
if (waitpid(pid, NULL, 0) < 0) {
if (errno == EINTR)
goto wait_again;
lxc_log_syserror("failed to wait the pid %d", pid);
SYSERROR("failed to wait the pid %d", pid);
goto err_waitpid_failed;
}
if (lxc_setstate(name, STOPPING))
lxc_log_error("failed to set state %s", lxc_state2str(STOPPING));
ERROR("failed to set state %s", lxc_state2str(STOPPING));
if (clone_flags & CLONE_NEWNET && conf_destroy_network(name))
lxc_log_error("failed to destroy the network");
ERROR("failed to destroy the network");
err = 0;
out:
if (lxc_setstate(name, STOPPED))
lxc_log_error("failed to set state %s", lxc_state2str(STOPPED));
ERROR("failed to set state %s", lxc_state2str(STOPPED));
lxc_unlink_nsgroup(name);
unlink(init);
......@@ -234,7 +237,7 @@ err_create_network:
err_pipe_read:
err_waitpid_failed:
if (lxc_setstate(name, ABORTING))
lxc_log_error("failed to set state %s", lxc_state2str(STOPPED));
ERROR("failed to set state %s", lxc_state2str(STOPPED));
kill(pid, SIGKILL);
err_fork_ns:
......
......@@ -91,6 +91,10 @@ int signalfd(int fd, const sigset_t *mask, int flags)
#include "mainloop.h"
#include <lxc/lxc.h>
#include <lxc/log.h>
lxc_log_define(lxc_start, lxc);
LXC_TTY_HANDLER(SIGINT);
LXC_TTY_HANDLER(SIGQUIT);
......@@ -101,23 +105,23 @@ static int setup_sigchld_fd(sigset_t *oldmask)
int fd;
if (sigprocmask(SIG_BLOCK, NULL, &mask)) {
lxc_log_syserror("failed to get mask signal");
SYSERROR("failed to get mask signal");
return -1;
}
if (sigaddset(&mask, SIGCHLD) || sigprocmask(SIG_BLOCK, &mask, oldmask)) {
lxc_log_syserror("failed to set mask signal");
SYSERROR("failed to set mask signal");
return -1;
}
fd = signalfd(-1, &mask, 0);
if (fd < 0) {
lxc_log_syserror("failed to create the signal fd");
SYSERROR("failed to create the signal fd");
return -1;
}
if (fcntl(fd, F_SETFD, FD_CLOEXEC)) {
lxc_log_syserror("failed to set sigfd to close-on-exec");
SYSERROR("failed to set sigfd to close-on-exec");
close(fd);
return -1;
}
......@@ -139,7 +143,7 @@ static int setup_tty_service(const char *name, int *ttyfd)
return -1;
if (fcntl(fd, F_SETFD, FD_CLOEXEC)) {
lxc_log_syserror("failed to close-on-exec flag");
SYSERROR("failed to close-on-exec flag");
close(fd);
return -1;
}
......@@ -186,12 +190,12 @@ static int ttyservice_handler(int fd, void *data,
conn = accept(fd, NULL, 0);
if (conn < 0) {
lxc_log_syserror("failed to accept tty client");
SYSERROR("failed to accept tty client");
return -1;
}
if (setsockopt(conn, SOL_SOCKET, SO_PASSCRED, &val, sizeof(val))) {
lxc_log_syserror("failed to enable credential on socket");
SYSERROR("failed to enable credential on socket");
goto out_close;
}
......@@ -209,13 +213,13 @@ static int ttyservice_handler(int fd, void *data,
if (lxc_af_unix_send_fd(conn, tty_info->pty_info[ttynum].master,
NULL, 0) < 0) {
lxc_log_error("failed to send tty to client");
ERROR("failed to send tty to client");
goto out_close;
}
if (lxc_mainloop_add_handler(descr, conn,
ttyclient_handler, tty_info)) {
lxc_log_error("failed to add tty client handler");
ERROR("failed to add tty client handler");
goto out_close;
}
......@@ -237,7 +241,7 @@ static int mainloop(const char *name, pid_t pid, int sigfd,
struct lxc_epoll_descr descr;
if (tty_info->nbtty && setup_tty_service(name, &ttyfd)) {
lxc_log_error("failed to create the tty service point");
ERROR("failed to create the tty service point");
goto out_sigfd;
}
......@@ -246,12 +250,12 @@ static int mainloop(const char *name, pid_t pid, int sigfd,
nfds = tty_info->nbtty + 1 + tty_info->nbtty ? 1 : 0;
if (lxc_mainloop_open(nfds, &descr)) {
lxc_log_error("failed to create mainloop");
ERROR("failed to create mainloop");
goto out_ttyfd;
}
if (lxc_mainloop_add_handler(&descr, sigfd, sigchld_handler, &pid)) {
lxc_log_error("failed to add handler for the signal");
ERROR("failed to add handler for the signal");
goto out_mainloop_open;
}
......@@ -259,7 +263,7 @@ static int mainloop(const char *name, pid_t pid, int sigfd,
if (lxc_mainloop_add_handler(&descr, ttyfd,
ttyservice_handler,
(void *)tty_info)) {
lxc_log_error("failed to add handler for the tty");
ERROR("failed to add handler for the tty");
goto out_mainloop_open;
}
}
......@@ -295,7 +299,7 @@ int lxc_start(const char *name, char *argv[])
/* Begin the set the state to STARTING*/
if (lxc_setstate(name, STARTING)) {
lxc_log_error("failed to set state '%s'",
ERROR("failed to set state '%s'",
lxc_state2str(STARTING));
goto out;
}
......@@ -305,7 +309,7 @@ int lxc_start(const char *name, char *argv[])
tty[0] = '\0';
if (lxc_create_tty(name, &tty_info)) {
lxc_log_error("failed to create the ttys");
ERROR("failed to create the ttys");
goto out;
}
......@@ -314,13 +318,13 @@ int lxc_start(const char *name, char *argv[])
* the event will be lost and the command will be stuck */
sigfd = setup_sigchld_fd(&oldmask);
if (sigfd < 0) {
lxc_log_error("failed to set sigchild fd handler");
ERROR("failed to set sigchild fd handler");
return -1;
}
/* Synchro socketpair */
if (socketpair(AF_LOCAL, SOCK_STREAM, 0, sv)) {
lxc_log_syserror("failed to create communication socketpair");
SYSERROR("failed to create communication socketpair");
goto out;
}
......@@ -337,14 +341,14 @@ int lxc_start(const char *name, char *argv[])
/* Create a process in a new set of namespaces */
pid = fork_ns(clone_flags);
if (pid < 0) {
lxc_log_syserror("failed to fork into a new namespace");
SYSERROR("failed to fork into a new namespace");
goto err_fork_ns;
}
if (!pid) {
if (sigprocmask(SIG_SETMASK, &oldmask, NULL)) {
lxc_log_syserror("failed to set sigprocmask");
SYSERROR("failed to set sigprocmask");
return -1;
}
......@@ -355,37 +359,37 @@ int lxc_start(const char *name, char *argv[])
/* Tell our father he can begin to configure the container */
if (write(sv[0], &sync, sizeof(sync)) < 0) {
lxc_log_syserror("failed to write socket");
SYSERROR("failed to write socket");
goto out_child;
}
/* Wait for the father to finish the configuration */
if (read(sv[0], &sync, sizeof(sync)) < 0) {
lxc_log_syserror("failed to read socket");
SYSERROR("failed to read socket");
goto out_child;
}
/* Setup the container, ip, names, utsname, ... */
err = lxc_setup(name, tty, &tty_info);
if (err) {
lxc_log_error("failed to setup the container");
ERROR("failed to setup the container");
if (write(sv[0], &err, sizeof(err)) < 0)
lxc_log_syserror("failed to write the socket");
SYSERROR("failed to write the socket");
goto out_child;
}
if (prctl(PR_CAPBSET_DROP, CAP_SYS_BOOT, 0, 0, 0)) {
lxc_log_syserror("failed to remove CAP_SYS_BOOT capability");
SYSERROR("failed to remove CAP_SYS_BOOT capability");
goto out_child;
}
execvp(argv[0], argv);
lxc_log_syserror("failed to exec %s", argv[0]);
SYSERROR("failed to exec %s", argv[0]);
err = LXC_ERROR_WRONG_COMMAND;
/* If the exec fails, tell that to our father */
if (write(sv[0], &err, sizeof(err)) < 0)
lxc_log_syserror("failed to write the socket");
SYSERROR("failed to write the socket");
out_child:
exit(err);
......@@ -395,29 +399,29 @@ int lxc_start(const char *name, char *argv[])
/* Wait for the child to be ready */
if (read(sv[1], &sync, sizeof(sync)) < 0) {
lxc_log_syserror("failed to read the socket");
SYSERROR("failed to read the socket");
goto err_pipe_read;
}
if (lxc_link_nsgroup(name, pid))
lxc_log_warning("cgroupfs not found: cgroup disabled");
WARN("cgroupfs not found: cgroup disabled");
/* Create the network configuration */
if (clone_flags & CLONE_NEWNET && conf_create_network(name, pid)) {
lxc_log_error("failed to create the configured network");
ERROR("failed to create the configured network");
goto err_create_network;
}
/* Tell the child to continue its initialization */
if (write(sv[1], &sync, sizeof(sync)) < 0) {
lxc_log_syserror("failed to write the socket");
SYSERROR("failed to write the socket");
goto err_pipe_write;
}
/* Wait for the child to exec or returning an error */
err = read(sv[1], &sync, sizeof(sync));
if (err < 0) {
lxc_log_error("failed to read the socket");
ERROR("failed to read the socket");
goto err_pipe_read2;
}
......@@ -428,7 +432,7 @@ int lxc_start(const char *name, char *argv[])
}
if (!asprintf(&val, "%d\n", pid)) {
lxc_log_syserror("failed to allocate memory");
SYSERROR("failed to allocate memory");
goto err_child_failed;
}
......@@ -436,38 +440,38 @@ int lxc_start(const char *name, char *argv[])
fd = open(init, O_WRONLY|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR);
if (fd < 0) {
lxc_log_syserror("failed to open '%s'", init);
SYSERROR("failed to open '%s'", init);
goto err_write;
}
if (write(fd, val, strlen(val)) < 0) {
lxc_log_syserror("failed to write the init pid");
SYSERROR("failed to write the init pid");
goto err_write;
}
close(fd);
if (lxc_setstate(name, RUNNING)) {
lxc_log_error("failed to set state to %s",
ERROR("failed to set state to %s",
lxc_state2str(RUNNING));
goto err_state_failed;
}
if (mainloop(name, pid, sigfd, &tty_info)) {
lxc_log_error("mainloop exited with an error");
ERROR("mainloop exited with an error");
goto err_mailoop_failed;
}
if (lxc_setstate(name, STOPPING))
lxc_log_error("failed to set state %s", lxc_state2str(STOPPING));
ERROR("failed to set state %s", lxc_state2str(STOPPING));
if (clone_flags & CLONE_NEWNET && conf_destroy_network(name))
lxc_log_error("failed to destroy the network");
ERROR("failed to destroy the network");
err = 0;
out:
if (lxc_setstate(name, STOPPED))
lxc_log_error("failed to set state %s", lxc_state2str(STOPPED));
ERROR("failed to set state %s", lxc_state2str(STOPPED));
lxc_delete_tty(&tty_info);
lxc_unlink_nsgroup(name);
......@@ -492,7 +496,7 @@ err_create_network:
err_pipe_read:
err_mailoop_failed:
if (lxc_setstate(name, ABORTING))
lxc_log_error("failed to set state %s", lxc_state2str(STOPPED));
ERROR("failed to set state %s", lxc_state2str(STOPPED));
kill(pid, SIGKILL);
err_fork_ns:
......
......@@ -32,6 +32,9 @@
#include <sys/file.h>
#include <lxc/lxc.h>
#include <lxc/log.h>
lxc_log_define(lxc_state, lxc);
static char *strstate[] = {
"STOPPED", "STARTING", "RUNNING", "STOPPING",
......@@ -68,22 +71,22 @@ int lxc_setstate(const char *name, lxc_state_t state)
fd = open(file, O_WRONLY);
if (fd < 0) {
lxc_log_syserror("failed to open %s file", file);
SYSERROR("failed to open %s file", file);
return -1;
}
if (flock(fd, LOCK_EX)) {
lxc_log_syserror("failed to take the lock to %s", file);
SYSERROR("failed to take the lock to %s", file);
goto out;
}
if (ftruncate(fd, 0)) {
lxc_log_syserror("failed to truncate the file %s", file);
SYSERROR("failed to truncate the file %s", file);
goto out;
}
if (write(fd, str, strlen(str)) < 0) {
lxc_log_syserror("failed to write state to %s", file);
SYSERROR("failed to write state to %s", file);
goto out;
}
......@@ -104,7 +107,7 @@ int lxc_mkstate(const char *name)
snprintf(file, MAXPATHLEN, LXCPATH "/%s/state", name);
fd = creat(file, S_IRUSR|S_IWUSR);
if (fd < 0) {
lxc_log_syserror("failed to create file %s", file);
SYSERROR("failed to create file %s", file);
return -1;
}
close(fd);
......@@ -128,19 +131,19 @@ lxc_state_t lxc_getstate(const char *name)
fd = open(file, O_RDONLY);
if (fd < 0) {
lxc_log_syserror("failed to open %s", file);
SYSERROR("failed to open %s", file);
return -1;
}
if (flock(fd, LOCK_SH)) {
lxc_log_syserror("failed to take the lock to %s", file);
SYSERROR("failed to take the lock to %s", file);
close(fd);
return -1;
}
err = read(fd, file, strlen(file));
if (err < 0) {
lxc_log_syserror("failed to read file %s", file);
SYSERROR("failed to read file %s", file);
close(fd);
return -1;
}
......@@ -168,7 +171,7 @@ static int freezer_state(const char *name)
fclose(file);
if (err == EOF) {
lxc_log_syserror("failed to read %s", freezer);
SYSERROR("failed to read %s", freezer);
return -1;
}
......
......@@ -32,6 +32,9 @@
#include <fcntl.h>
#include <lxc/lxc.h>
#include <lxc/log.h>
lxc_log_define(lxc_stop, lxc);
#define MAXPIDLEN 20
......@@ -54,19 +57,19 @@ int lxc_stop(const char *name)
snprintf(init, MAXPATHLEN, LXCPATH "/%s/init", name);
fd = open(init, O_RDONLY);
if (fd < 0) {
lxc_log_syserror("failed to open init file for %s", name);
SYSERROR("failed to open init file for %s", name);
goto out_close;
}
if (read(fd, val, sizeof(val)) < 0) {
lxc_log_syserror("failed to read %s", init);
SYSERROR("failed to read %s", init);
goto out_close;
}
pid = atoi(val);
if (kill(pid, SIGKILL)) {
lxc_log_syserror("failed to kill %zd", pid);
SYSERROR("failed to kill %zd", pid);
goto out_close;
}
......
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