Header code cleanup

This commit is contained in:
dlezcano 2008-09-05 14:32:09 +00:00
parent b113348ebd
commit eae6543da1
12 changed files with 96 additions and 75 deletions

View File

@ -2,6 +2,7 @@ INCLUDES= -I$(top_srcdir)/src
lib_LTLIBRARIES = liblxc.la lib_LTLIBRARIES = liblxc.la
pkginclude_HEADERS = \ pkginclude_HEADERS = \
monitor.h \
lxc.h \ lxc.h \
lxc_cgroup.h \ lxc_cgroup.h \
lxc_conf.h \ lxc_conf.h \

View File

@ -30,7 +30,6 @@
#include <sys/param.h> #include <sys/param.h>
#include <lxc/lxc.h> #include <lxc/lxc.h>
#include "monitor.h"
static int dir_filter(const struct dirent *dirent) static int dir_filter(const struct dirent *dirent)
{ {

View File

@ -41,6 +41,7 @@ extern "C" {
#include <lxc/lxc_cgroup.h> #include <lxc/lxc_cgroup.h>
#include <lxc/lxc_namespace.h> #include <lxc/lxc_namespace.h>
#include <lxc/lxc_utils.h> #include <lxc/lxc_utils.h>
#include <lxc/monitor.h>
#define LXCPATH "/var/lxc" #define LXCPATH "/var/lxc"
#define MAXPIDLEN 20 #define MAXPIDLEN 20
@ -130,7 +131,7 @@ extern int lxc_monitor_open(const char *name);
* Returns 0 if the monitored container has exited, > 0 if * Returns 0 if the monitored container has exited, > 0 if
* data was readen, < 0 otherwise * data was readen, < 0 otherwise
*/ */
extern int lxc_monitor_read(int fd, lxc_state_t *state); extern int lxc_monitor_read(int fd, struct lxc_msg *msg);
/* /*
* Close the fd associated with the monitoring * Close the fd associated with the monitoring

View File

@ -104,17 +104,21 @@ int lxc_unlink_nsgroup(const char *name)
int lxc_cgroup_set_priority(const char *name, int priority) int lxc_cgroup_set_priority(const char *name, int priority)
{ {
int fd; int fd;
char *path = NULL, *prio = NULL; char path[MAXPATHLEN], *prio = NULL;
asprintf(&path, LXCPATH "/%s/nsgroup/cpu.shares", name); snprintf(path, MAXPATHLEN,
LXCPATH "/%s/nsgroup/cpu.shares", name);
fd = open(path, O_WRONLY); fd = open(path, O_WRONLY);
if (fd < 0) { if (fd < 0) {
lxc_log_syserror("failed to open '%s'", path); lxc_log_syserror("failed to open '%s'", path);
goto out; return -1;
} }
asprintf(&prio, "%d", priority); if (!asprintf(&prio, "%d", priority)) {
lxc_log_syserror("not enough memory");
goto out;
}
if (write(fd, prio, strlen(prio) + 1) < 0) { if (write(fd, prio, strlen(prio) + 1) < 0) {
lxc_log_syserror("failed to write to '%s'", path); lxc_log_syserror("failed to write to '%s'", path);
@ -122,9 +126,10 @@ int lxc_cgroup_set_priority(const char *name, int priority)
goto out; goto out;
} }
lxc_monitor_send_priority(name, priority);
close(fd); close(fd);
out: out:
free(path);
free(prio); free(prio);
return 0; return 0;
} }

View File

@ -404,13 +404,13 @@ static int configure_cgroup(const char *name, struct lxc_cgroup *cgroup)
return 0; return 0;
} }
static int configure_chroot(const char *name, const char *chroot) static int configure_rootfs(const char *name, const char *rootfs)
{ {
char path[MAXPATHLEN]; char path[MAXPATHLEN];
snprintf(path, MAXPATHLEN, LXCPATH "/%s/chroot", name); snprintf(path, MAXPATHLEN, LXCPATH "/%s/rootfs", name);
return symlink(chroot, path); return symlink(rootfs, path);
} }
@ -524,12 +524,12 @@ static int unconfigure_cgroup(const char *name)
return 0; return 0;
} }
static int unconfigure_chroot(const char *name) static int unconfigure_rootfs(const char *name)
{ {
char path[MAXPATHLEN]; char path[MAXPATHLEN];
snprintf(path, MAXPATHLEN, LXCPATH "/%s", name); snprintf(path, MAXPATHLEN, LXCPATH "/%s", name);
delete_info(path, "chroot"); delete_info(path, "rootfs");
return 0; return 0;
} }
@ -578,11 +578,11 @@ static int setup_utsname(const char *name)
return 0; return 0;
} }
static int setup_chroot(const char *name) static int setup_rootfs(const char *name)
{ {
char path[MAXPATHLEN], chrt[MAXPATHLEN]; char path[MAXPATHLEN], chrt[MAXPATHLEN];
snprintf(path, MAXPATHLEN, LXCPATH "/%s/chroot", name); snprintf(path, MAXPATHLEN, LXCPATH "/%s/rootfs", name);
if (readlink(path, chrt, MAXPATHLEN) > 0) { if (readlink(path, chrt, MAXPATHLEN) > 0) {
@ -889,8 +889,8 @@ int lxc_configure(const char *name, struct lxc_conf *conf)
return -1; return -1;
} }
if (conf->chroot && configure_chroot(name, conf->chroot)) { if (conf->rootfs && configure_rootfs(name, conf->rootfs)) {
lxc_log_error("failed to configure the chroot"); lxc_log_error("failed to configure the rootfs");
return -1; return -1;
} }
@ -913,8 +913,8 @@ int lxc_unconfigure(const char *name)
if (unconfigure_cgroup(name)) if (unconfigure_cgroup(name))
lxc_log_error("failed to cleanup cgroup"); lxc_log_error("failed to cleanup cgroup");
if (conf_has_chroot(name) && unconfigure_chroot(name)) if (conf_has_rootfs(name) && unconfigure_rootfs(name))
lxc_log_error("failed to cleanup chroot"); lxc_log_error("failed to cleanup rootfs");
if (conf_has_fstab(name) && unconfigure_mount(name)) if (conf_has_fstab(name) && unconfigure_mount(name))
lxc_log_error("failed to cleanup mount"); lxc_log_error("failed to cleanup mount");
@ -1225,8 +1225,8 @@ int lxc_setup(const char *name)
return -1; return -1;
} }
if (conf_has_chroot(name) && setup_chroot(name)) { if (conf_has_rootfs(name) && setup_rootfs(name)) {
lxc_log_error("failed to set chroot for '%s'", name); lxc_log_error("failed to set rootfs for '%s'", name);
return -1; return -1;
} }

View File

@ -105,13 +105,13 @@ struct lxc_cgroup {
/* /*
* Defines the global container configuration * Defines the global container configuration
* @chroot : the root directory to run the container * @rootfs : the root directory to run the container
* @mount : the list of mount points * @mount : the list of mount points
* @network : the network configuration * @network : the network configuration
* @utsname : the container utsname * @utsname : the container utsname
*/ */
struct lxc_conf { struct lxc_conf {
char *chroot; char *rootfs;
char *fstab; char *fstab;
struct utsname *utsname; struct utsname *utsname;
struct lxc_cgroup *cgroup; struct lxc_cgroup *cgroup;
@ -140,7 +140,7 @@ extern int lxc_setup(const char *name);
extern int conf_has(const char *name, const char *info); extern int conf_has(const char *name, const char *info);
#define conf_has_fstab(__name) conf_has(__name, "fstab") #define conf_has_fstab(__name) conf_has(__name, "fstab")
#define conf_has_chroot(__name) conf_has(__name, "chroot") #define conf_has_rootfs(__name) conf_has(__name, "rootfs")
#define conf_has_utsname(__name) conf_has(__name, "utsname") #define conf_has_utsname(__name) conf_has(__name, "utsname")
#define conf_has_network(__name) conf_has(__name, "network") #define conf_has_network(__name) conf_has(__name, "network")

View File

@ -38,7 +38,7 @@ typedef int (*file_cb)(char* buffer, void *data);
typedef int (*config_cb)(char *value, struct lxc_conf *lxc_conf); typedef int (*config_cb)(char *value, struct lxc_conf *lxc_conf);
static int config_mount(char *, struct lxc_conf *); static int config_mount(char *, struct lxc_conf *);
static int config_chroot(char *, struct lxc_conf *); static int config_rootfs(char *, struct lxc_conf *);
static int config_utsname(char *, struct lxc_conf *); static int config_utsname(char *, struct lxc_conf *);
static int config_network_type(char *, struct lxc_conf *); static int config_network_type(char *, struct lxc_conf *);
static int config_network_flags(char *, struct lxc_conf *); static int config_network_flags(char *, struct lxc_conf *);
@ -54,12 +54,12 @@ struct config {
config_cb cb; config_cb cb;
}; };
enum { MOUNT, CHROOT, UTSNAME, NETTYPE, NETFLAGS, NETLINK, enum { MOUNT, ROOTFS, UTSNAME, NETTYPE, NETFLAGS, NETLINK,
NETNAME, NETHWADDR, NETIPV4, NETIPV6 }; NETNAME, NETHWADDR, NETIPV4, NETIPV6 };
struct config config[] = { struct config config[] = {
{ "lxc.mount", MOUNT, config_mount }, { "lxc.mount", MOUNT, config_mount },
{ "lxc.chroot", CHROOT, config_chroot }, { "lxc.rootfs", ROOTFS, config_rootfs },
{ "lxc.utsname", UTSNAME, config_utsname }, { "lxc.utsname", UTSNAME, config_utsname },
{ "lxc.network.type", NETTYPE, config_network_type }, { "lxc.network.type", NETTYPE, config_network_type },
{ "lxc.network.flags", NETFLAGS, config_network_flags }, { "lxc.network.flags", NETFLAGS, config_network_flags },
@ -434,15 +434,15 @@ static int config_mount(char *value, struct lxc_conf *lxc_conf)
return 0; return 0;
} }
static int config_chroot(char *value, struct lxc_conf *lxc_conf) static int config_rootfs(char *value, struct lxc_conf *lxc_conf)
{ {
if (strlen(value) >= MAXPATHLEN) { if (strlen(value) >= MAXPATHLEN) {
lxc_log_error("%s path is too long", value); lxc_log_error("%s path is too long", value);
return -1; return -1;
} }
lxc_conf->chroot = strdup(value); lxc_conf->rootfs = strdup(value);
if (!lxc_conf->chroot) { if (!lxc_conf->rootfs) {
lxc_log_syserror("failed to duplicate string %s", value); lxc_log_syserror("failed to duplicate string %s", value);
return -1; return -1;
} }
@ -539,7 +539,7 @@ int lxc_config_read(const char *file, struct lxc_conf *conf)
int lxc_config_init(struct lxc_conf *conf) int lxc_config_init(struct lxc_conf *conf)
{ {
conf->chroot = NULL; conf->rootfs = NULL;
conf->fstab = NULL; conf->fstab = NULL;
conf->utsname = NULL; conf->utsname = NULL;
conf->cgroup = NULL; conf->cgroup = NULL;

View File

@ -36,10 +36,9 @@ void usage(char *cmd)
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
char opt; char opt, *name = NULL;
char *name = NULL; struct lxc_msg msg;
int fds[2]; int fd;
pid_t pid;
while ((opt = getopt(argc, argv, "n:")) != -1) { while ((opt = getopt(argc, argv, "n:")) != -1) {
switch (opt) { switch (opt) {
@ -52,46 +51,35 @@ int main(int argc, char *argv[])
if (!name) if (!name)
usage(argv[0]); usage(argv[0]);
if (pipe(fds)) { fd = lxc_monitor_open(name);
perror("pipe"); if (fd < 0) {
return 1; fprintf(stderr, "failed to open monitor for '%s'\n", name);
return -1;
} }
pid = fork();
if (pid < 0) {
perror("fork");
return 1;
}
if (!pid) {
close(fds[0]);
if (lxc_monitor(name, fds[1])) {
fprintf(stderr, "failed to monitor %s\n", name);
return 1;
}
return 0;
}
close(fds[1]);
for (;;) { for (;;) {
int err, state; if (lxc_monitor_read(fd, &msg) < 0) {
fprintf(stderr,
err = read(fds[0], &state, sizeof(state)); "failed to read monitor's message for '%s'\n",
if (err < 0) { name);
perror("read"); return -1;
return 1;
} }
if (!err) { switch (msg.type) {
printf("container has been destroyed\n"); case lxc_msg_state:
return 0; printf("'%s' changed state to [%s]\n",
name, lxc_state2str(msg.value));
break;
case lxc_msg_priority:
printf("'%s' changed priority to [%d]\n",
name, msg.value);
break;
default:
printf("invalid msg format\n");
break;
} }
printf("container has changed the state to %d - %s\n",
state, lxc_state2str(state));
} }
return 0; return 0;
} }

View File

@ -32,7 +32,6 @@
#include <sys/file.h> #include <sys/file.h>
#include <lxc/lxc.h> #include <lxc/lxc.h>
#include "monitor.h"
static char *strstate[] = { static char *strstate[] = {
"STOPPED", "STARTING", "RUNNING", "STOPPING", "STOPPED", "STARTING", "RUNNING", "STOPPING",

View File

@ -101,25 +101,42 @@ out:
return err; return err;
} }
void lxc_monitor_send_state(const char *name, lxc_state_t state) static void lxc_monitor_send(const char *name, struct lxc_msg *msg)
{ {
int fd; int fd;
struct sockaddr_un addr; struct sockaddr_un addr;
fd = socket(PF_UNIX, SOCK_DGRAM, 0); fd = socket(PF_UNIX, SOCK_DGRAM, 0);
if (fd < 0) if (fd < 0) {
lxc_log_syserror("failed to create notification socket"); lxc_log_syserror("failed to create notification socket");
return;
}
memset(&addr, 0, sizeof(addr)); memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_UNIX; addr.sun_family = AF_UNIX;
snprintf(addr.sun_path, UNIX_PATH_MAX, LXCPATH "/%s/notification", name); snprintf(addr.sun_path, UNIX_PATH_MAX,
LXCPATH "/%s/notification", name);
sendto(fd, &state, sizeof(state), 0, sendto(fd, msg, sizeof(*msg), 0,
(const struct sockaddr *)&addr, sizeof(addr)); (const struct sockaddr *)&addr, sizeof(addr));
close(fd); close(fd);
} }
void lxc_monitor_send_priority(const char *name, int priority)
{
struct lxc_msg msg = { .type = lxc_msg_priority,
.value = priority };
lxc_monitor_send(name, &msg);
}
void lxc_monitor_send_state(const char *name, lxc_state_t state)
{
struct lxc_msg msg = { .type = lxc_msg_state,
.value = state };
lxc_monitor_send(name, &msg);
}
void lxc_monitor_cleanup(const char *name) void lxc_monitor_cleanup(const char *name)
{ {
char path[UNIX_PATH_MAX]; char path[UNIX_PATH_MAX];
@ -153,11 +170,11 @@ int lxc_monitor_open(const char *name)
return fd; return fd;
} }
int lxc_monitor_read(int fd, lxc_state_t *state) int lxc_monitor_read(int fd, struct lxc_msg *msg)
{ {
int ret; int ret;
ret = recv(fd, state, sizeof(*state), 0); ret = recv(fd, msg, sizeof(*msg), 0);
if (ret < 0) { if (ret < 0) {
lxc_log_syserror("failed to received state"); lxc_log_syserror("failed to received state");
return -1; return -1;

View File

@ -23,7 +23,18 @@
#ifndef __monitor_h #ifndef __monitor_h
#define __monitor_h #define __monitor_h
typedef enum {
lxc_msg_state,
lxc_msg_priority,
} lxc_msg_type_t;
struct lxc_msg {
lxc_msg_type_t type;
int value;
};
void lxc_monitor_send_state(const char *name, lxc_state_t state); void lxc_monitor_send_state(const char *name, lxc_state_t state);
void lxc_monitor_send_priority(const char *name, int priority);
void lxc_monitor_cleanup(const char *name); void lxc_monitor_cleanup(const char *name);
#endif #endif

View File

@ -142,7 +142,7 @@ int main(int argc, char *argv[])
struct lxc_conf lxc_conf = { struct lxc_conf lxc_conf = {
.networks = lxc_init_list(&lxc_conf.networks), .networks = lxc_init_list(&lxc_conf.networks),
.chroot = "/mnt/iso", .rootfs = "/mnt/iso",
}; };
lxc_list_add(&phys.netdev, &ndlist); lxc_list_add(&phys.netdev, &ndlist);