Skip to content

Commit

Permalink
Added C++ compatibility, change to libtool, improve monitoring
Browse files Browse the repository at this point in the history
  • Loading branch information
dlezcano committed Sep 2, 2008
1 parent 187d3a3 commit c2cc9f0
Show file tree
Hide file tree
Showing 26 changed files with 862 additions and 95 deletions.
1 change: 1 addition & 0 deletions configure.in
Expand Up @@ -11,6 +11,7 @@ AC_CANONICAL_HOST
AC_PROG_RANLIB
AM_PROG_CC_C_O
AC_GNU_SOURCE
AC_PROG_LIBTOOL
AC_CHECK_HEADERS([linux/netlink.h linux/genetlink.h],, AC_MSG_ERROR([netlink headers not found]), [[]])
AC_PROG_GCC_TRADITIONAL

Expand Down
5 changes: 5 additions & 0 deletions lxc.spec.in
Expand Up @@ -82,13 +82,18 @@ rm -rf %{buildroot}
%files
%defattr(-,root,root)
%{_sysconfdir}/%{name}/*
%{_libdir}/*.so*
%{_bindir}/*

%files devel
%defattr(-,root,root)
%{_includedir}/%{name}/*
%{_libdir}/*.a

%post devel
ln -sf %{_includedir}/%{name} %{_includedir}/liblxc


%changelog
* Sun Aug 3 2008 Daniel Lezcano <dlezcano@fr.ibm.com>
- Initial RPM release.
Expand Down
9 changes: 5 additions & 4 deletions src/liblxc/Makefile.am
@@ -1,4 +1,4 @@
lib_LIBRARIES = liblxc.a
lib_LTLIBRARIES = liblxc.la
pkginclude_HEADERS = \
lxc.h \
lxc_cgroup.h \
Expand All @@ -10,16 +10,15 @@ pkginclude_HEADERS = \
lxc_state.h \
lxc_utils.h

liblxc_a_SOURCES = \
liblxc_la_SOURCES = \
create.c \
destroy.c \
start.c \
stop.c \
execute.c \
monitor.c \
monitor.c monitor.h \
kill.c \
freezer.c \
lxc_state.c lxc_state.h \
lxc_cgroup.c lxc_cgroup.h \
lxc.h \
lxc_utils.h \
Expand All @@ -34,3 +33,5 @@ liblxc_a_SOURCES = \
nl.c nl.h \
rtnl.c rtnl.h \
genl.c genl.h

liblxc_la_LDFLAGS = -release @PACKAGE_VERSION@
4 changes: 2 additions & 2 deletions src/liblxc/create.c
Expand Up @@ -116,7 +116,7 @@ int lxc_create(const char *name, struct lxc_conf *conf)
goto err;
}

if (mkstate(name)) {
if (lxc_mkstate(name)) {
lxc_log_error("failed to create the state file for %s", name);
goto err;
}
Expand All @@ -139,7 +139,7 @@ int lxc_create(const char *name, struct lxc_conf *conf)
err_state:
lxc_unconfigure(name);

if (rmstate(name))
if (lxc_rmstate(name))
lxc_log_error("failed to remove state file for %s", name);
err:
if (remove_lxc_directory(name))
Expand Down
4 changes: 3 additions & 1 deletion src/liblxc/destroy.c
Expand Up @@ -94,10 +94,12 @@ int lxc_destroy(const char *name)
goto out;
}

if (rmstate(name)) {
if (lxc_rmstate(name)) {
lxc_log_error("failed to remove state file for %s", name);
goto out_lock;
}

lxc_monitor_cleanup(name);

if (lxc_unconfigure(name)) {
lxc_log_error("failed to cleanup %s", name);
Expand Down
16 changes: 8 additions & 8 deletions src/liblxc/execute.c
Expand Up @@ -64,10 +64,8 @@ int lxc_execute(const char *name, int argc, char *argv[],
return -1;
}

fcntl(lock, F_SETFD, FD_CLOEXEC);

if (lxc_setstate(name, STARTING)) {
lxc_log_error("failed to set state %s", state2str(STARTING));
lxc_log_error("failed to set state %s", lxc_state2str(STARTING));
goto out;
}

Expand Down Expand Up @@ -117,10 +115,12 @@ int lxc_execute(const char *name, int argc, char *argv[],
lxc_log_error("failed to setup the container");
goto error;
}

if (mount("proc", "/proc", "proc", 0, NULL)) {
lxc_log_error("failed to mount '/proc'");
lxc_log_syserror("failed to mount '/proc'");
goto error;
}

if (mount("sysfs", "/sys", "sysfs", 0, NULL)) {
lxc_log_syserror("failed to mount '/sys'");
/* continue: non fatal error until sysfs not per
Expand Down Expand Up @@ -213,7 +213,7 @@ int lxc_execute(const char *name, int argc, char *argv[],
lxc_log_warning("cgroupfs not found: cgroup disabled");

if (lxc_setstate(name, RUNNING)) {
lxc_log_error("failed to set state to %s", state2str(RUNNING));
lxc_log_error("failed to set state to %s", lxc_state2str(RUNNING));
goto err_state_failed;
}

Expand All @@ -226,15 +226,15 @@ int lxc_execute(const char *name, int argc, char *argv[],
}

if (lxc_setstate(name, STOPPING))
lxc_log_error("failed to set state %s", state2str(STOPPING));
lxc_log_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");

err = 0;
out:
if (lxc_setstate(name, STOPPED))
lxc_log_error("failed to set state %s", state2str(STOPPED));
lxc_log_error("failed to set state %s", lxc_state2str(STOPPED));

lxc_unlink_nsgroup(name);
unlink(init);
Expand All @@ -257,7 +257,7 @@ int lxc_execute(const char *name, int argc, char *argv[],
err_open:
err_waitpid_failed:
if (lxc_setstate(name, ABORTING))
lxc_log_error("failed to set state %s", state2str(STOPPED));
lxc_log_error("failed to set state %s", lxc_state2str(STOPPED));

kill(pid, SIGKILL);
err_fork_ns:
Expand Down
2 changes: 2 additions & 0 deletions src/liblxc/lock.c
Expand Up @@ -42,6 +42,8 @@ int lxc_get_lock(const char *name)
goto out;
}

fcntl(fd, F_SETFD, FD_CLOEXEC);

if (flock(fd, LOCK_EX|LOCK_NB)) {
ret = errno == EWOULDBLOCK ? 0 : -errno;
close(fd);
Expand Down
51 changes: 42 additions & 9 deletions src/liblxc/lxc.h
Expand Up @@ -23,20 +23,24 @@
#ifndef __lxc_h
#define __lxc_h

#ifdef __cplusplus
extern "C" {
#endif

/**
Following code is for liblxc.
liblxc/lxc.h will contain exports of liblxc
**/

#include <lxc_state.h>
#include <lxc_list.h>
#include <lxc_conf.h>
#include <lxc_log.h>
#include <lxc_lock.h>
#include <lxc_cgroup.h>
#include <lxc_namespace.h>
#include <lxc_utils.h>
#include <liblxc/lxc_state.h>
#include <liblxc/lxc_list.h>
#include <liblxc/lxc_conf.h>
#include <liblxc/lxc_log.h>
#include <liblxc/lxc_lock.h>
#include <liblxc/lxc_cgroup.h>
#include <liblxc/lxc_namespace.h>
#include <liblxc/lxc_utils.h>

#define LXCPATH "/var/lxc"
#define MAXPIDLEN 20
Expand Down Expand Up @@ -104,12 +108,37 @@ extern int lxc_stop(const char *name);
* is changed, a state data is send through a file descriptor passed to
* the function with output_fd.
* The function will block until the container is destroyed.
* @name : the name of the contaier
* @name : the name of the container
* @output_fd : the file descriptor where to send the states
* Returns 0 on success, < 0 otherwise
*/
extern int lxc_monitor(const char *name, int output_fd);

/*
* Open the monitoring mechanism for a specific container
* The function will return an fd corresponding to the events
* @name : the name of the container
* Returns a file descriptor on success, < 0 otherwise
*/
extern int lxc_monitor_open(const char *name);

/*
* Read the state of the container if this one has changed
* The function will block until there is an event available
* @fd : the file descriptor provided by lxc_monitor_open
* @state : the variable which will be filled with the state
* Returns 0 if the monitored container has exited, > 0 if
* data was readen, < 0 otherwise
*/
extern int lxc_monitor_read(int fd, lxc_state_t *state);

/*
* Close the fd associated with the monitoring
* @fd : the file descriptor provided by lxc_monitor_open
* Returns 0 on success, < 0 otherwise
*/
extern int lxc_monitor_close(int fd);

/*
* Show the console of the container.
* @name : the name of container
Expand Down Expand Up @@ -220,4 +249,8 @@ extern int lxc_cgroup_get_cpuset(const char *name, long *cpumask,
*/
extern int lxc_cgroup_get_cpu_usage(const char *name, long long *usage);

#ifdef __cplusplus
}
#endif

#endif
4 changes: 2 additions & 2 deletions src/liblxc/lxc_cgroup.c
Expand Up @@ -101,7 +101,7 @@ int lxc_unlink_nsgroup(const char *name)
return ret;
}

int lxc_set_priority(const char *name, int priority)
int lxc_cgroup_set_priority(const char *name, int priority)
{
int fd;
char *path = NULL, *prio = NULL;
Expand Down Expand Up @@ -129,7 +129,7 @@ int lxc_set_priority(const char *name, int priority)
return 0;
}

int lxc_get_priority(const char *name, int *priority)
int lxc_cgroup_get_priority(const char *name, int *priority)
{
int fd, ret = -1;
char *path, prio[MAXPRIOLEN];
Expand Down
12 changes: 6 additions & 6 deletions src/liblxc/lxc_list.h
Expand Up @@ -35,13 +35,13 @@ static inline int lxc_list_empty(struct lxc_list *list)
return list == list->next;
}

static inline void lxc_list_add(struct lxc_list *list, struct lxc_list *new)
static inline void lxc_list_add(struct lxc_list *head, struct lxc_list *list)
{
struct lxc_list *next = list->next;
next->prev = new;
new->next = next;
new->prev = list;
list->next = new;
struct lxc_list *next = head->next;
next->prev = list;
list->next = next;
list->prev = head;
head->next = list;
}

static inline void lxc_list_del(struct lxc_list *list)
Expand Down
19 changes: 11 additions & 8 deletions src/liblxc/lxc_state.c
Expand Up @@ -32,20 +32,21 @@
#include <sys/file.h>

#include <lxc.h>
#include "monitor.h"

static char *strstate[] = {
"STOPPED", "STARTING", "RUNNING", "STOPPING",
"ABORTING", "FREEZING", "FROZEN",
};

const char *state2str(lxc_state_t state)
const char *lxc_state2str(lxc_state_t state)
{
if (state < STOPPED || state > MAX_STATE - 1)
return NULL;
return strstate[state];
}

lxc_state_t str2state(const char *state)
lxc_state_t lxc_str2state(const char *state)
{
int i, len;
len = sizeof(strstate)/sizeof(strstate[0]);
Expand All @@ -59,7 +60,7 @@ int lxc_setstate(const char *name, lxc_state_t state)
{
int fd, err;
char file[MAXPATHLEN];
const char *str = state2str(state);
const char *str = lxc_state2str(state);

if (!str)
return -1;
Expand Down Expand Up @@ -91,6 +92,8 @@ int lxc_setstate(const char *name, lxc_state_t state)
out:
close(fd);

lxc_monitor_send_state(name, state);

/* let the event to be propagated, crappy but that works,
* otherwise the events will be folded into only one event,
* and I want to have them to be one by one in order
Expand All @@ -101,7 +104,7 @@ int lxc_setstate(const char *name, lxc_state_t state)
return -err;
}

int mkstate(const char *name)
int lxc_mkstate(const char *name)
{
int fd;
char file[MAXPATHLEN];
Expand All @@ -116,7 +119,7 @@ int mkstate(const char *name)
return 0;
}

int rmstate(const char *name)
int lxc_rmstate(const char *name)
{
char file[MAXPATHLEN];
snprintf(file, MAXPATHLEN, LXCPATH "/%s/state", name);
Expand Down Expand Up @@ -152,7 +155,7 @@ lxc_state_t lxc_getstate(const char *name)
file[err] = '\0';

close(fd);
return str2state(file);
return lxc_str2state(file);
}

static int freezer_state(const char *name)
Expand All @@ -166,7 +169,7 @@ static int freezer_state(const char *name)
LXCPATH "/%s/freezer.freeze", name);

file = fopen(freezer, "r");
if (file < 0) {
if (!file) {
lxc_log_syserror("failed to open %s", freezer);
return -1;
}
Expand All @@ -179,7 +182,7 @@ static int freezer_state(const char *name)
return -1;
}

return str2state(status);
return lxc_str2state(status);
}

lxc_state_t lxc_state(const char *name)
Expand Down
9 changes: 5 additions & 4 deletions src/liblxc/lxc_state.h
Expand Up @@ -28,11 +28,12 @@ typedef enum {
ABORTING, FREEZING, FROZEN, MAX_STATE,
} lxc_state_t;

extern const char *state2str(lxc_state_t state);
extern lxc_state_t str2state(const char *state);
extern int mkstate(const char *name);
extern int rmstate(const char *name);
extern int lxc_mkstate(const char *name);
extern int lxc_rmstate(const char *name);
extern int lxc_setstate(const char *name, lxc_state_t state);
extern lxc_state_t lxc_getstate(const char *name);

extern lxc_state_t lxc_str2state(const char *state);
extern const char *lxc_state2str(lxc_state_t state);

#endif

0 comments on commit c2cc9f0

Please sign in to comment.