Skip to content

Commit

Permalink
lxc_monitor_open: prepend lxcpath
Browse files Browse the repository at this point in the history
This is needed for lxc_wait and lxc_monitor to handle lxcpath.  However,
the full path name is limited to 108 bytes.  Should we use a md5sum of
the lxcpath instead of the path itself?

In any case, with this patch, lxc-wait and lxc-monitor work right with
respect to multiple lxcpaths.

The lxcpath is added to the lxc_handler to make it available most of the
places we need it.

I also remove function prototypes in monitor.h for two functions which
are not defined or used anywhere.

TODO: make cgroups tolerate multiple same-named containers.

Signed-off-by: Serge Hallyn <serge.hallyn@ubuntu.com>
Acked-by: Stéphane Graber <stgraber@ubuntu.com>
  • Loading branch information
hallyn authored and stgraber committed Feb 19, 2013
1 parent fbf5de3 commit 9123e47
Show file tree
Hide file tree
Showing 12 changed files with 50 additions and 29 deletions.
14 changes: 7 additions & 7 deletions src/lxc/freezer.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@

lxc_log_define(lxc_freezer, lxc);

static int freeze_unfreeze(const char *name, int freeze)
static int freeze_unfreeze(const char *name, int freeze, const char *lxcpath)
{
char *nsgroup;
char freezer[MAXPATHLEN], *f;
Expand Down Expand Up @@ -98,7 +98,7 @@ static int freeze_unfreeze(const char *name, int freeze)
ret = strncmp(f, tmpf, strlen(f));
if (!ret)
{
lxc_monitor_send_state(name, freeze ? FROZEN : THAWED);
lxc_monitor_send_state(name, freeze ? FROZEN : THAWED, lxcpath);
break; /* Success */
}

Expand All @@ -122,14 +122,14 @@ static int freeze_unfreeze(const char *name, int freeze)
return ret;
}

int lxc_freeze(const char *name)
int lxc_freeze(const char *name, const char *lxcpath)
{
lxc_monitor_send_state(name, FREEZING);
return freeze_unfreeze(name, 1);
lxc_monitor_send_state(name, FREEZING, lxcpath);
return freeze_unfreeze(name, 1, lxcpath);
}

int lxc_unfreeze(const char *name)
int lxc_unfreeze(const char *name, const char *lxcpath)
{
return freeze_unfreeze(name, 0);
return freeze_unfreeze(name, 0, lxcpath);
}

6 changes: 3 additions & 3 deletions src/lxc/lxc.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ extern int lxc_execute(const char *name, char *const argv[], int quiet,
* The function will return an fd corresponding to the events
* Returns a file descriptor on success, < 0 otherwise
*/
extern int lxc_monitor_open(void);
extern int lxc_monitor_open(const char *lxcpath);

/*
* Read the state of the container if this one has changed
Expand Down Expand Up @@ -108,14 +108,14 @@ extern int lxc_console(const char *name, int ttynum, int *fd, const char *lxcpat
* @name : the container name
* Returns 0 on success, < 0 otherwise
*/
extern int lxc_freeze(const char *name);
extern int lxc_freeze(const char *name, const char *lxcpath);

/*
* Unfreeze all previously frozen tasks.
* @name : the name of the container
* Return 0 on sucess, < 0 otherwise
*/
extern int lxc_unfreeze(const char *name);
extern int lxc_unfreeze(const char *name, const char *lxcpath);

/*
* Retrieve the container state
Expand Down
2 changes: 1 addition & 1 deletion src/lxc/lxc_freeze.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,6 @@ int main(int argc, char *argv[])
my_args.progname, my_args.quiet))
return -1;

return lxc_freeze(my_args.name);
return lxc_freeze(my_args.name, my_args.lxcpath);
}

2 changes: 1 addition & 1 deletion src/lxc/lxc_monitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ int main(int argc, char *argv[])
return -1;
}

fd = lxc_monitor_open();
fd = lxc_monitor_open(my_args.lxcpath);
if (fd < 0)
return -1;

Expand Down
2 changes: 1 addition & 1 deletion src/lxc/lxc_unfreeze.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,6 @@ int main(int argc, char *argv[])
my_args.progname, my_args.quiet))
return -1;

return lxc_unfreeze(my_args.name);
return lxc_unfreeze(my_args.name, my_args.lxcpath);
}

4 changes: 2 additions & 2 deletions src/lxc/lxccontainer.c
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ static bool lxcapi_freeze(struct lxc_container *c)

if (lxclock(c->slock, 0))
return false;
ret = lxc_freeze(c->name);
ret = lxc_freeze(c->name, c->config_path);
lxcunlock(c->slock);
if (ret)
return false;
Expand All @@ -210,7 +210,7 @@ static bool lxcapi_unfreeze(struct lxc_container *c)

if (lxclock(c->slock, 0))
return false;
ret = lxc_unfreeze(c->name);
ret = lxc_unfreeze(c->name, c->config_path);
lxcunlock(c->slock);
if (ret)
return false;
Expand Down
36 changes: 28 additions & 8 deletions src/lxc/monitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,23 @@ lxc_log_define(lxc_monitor, lxc);
#define UNIX_PATH_MAX 108
#endif

static void lxc_monitor_send(struct lxc_msg *msg)
static void lxc_monitor_send(struct lxc_msg *msg, const char *lxcpath)
{
int fd;
struct sockaddr_un addr = { .sun_family = AF_UNIX };
char *offset = &addr.sun_path[1];

strcpy(offset, "lxc-monitor");
size_t ret, len;

/*
* addr.sun_path is only 108 bytes.
* should we take a hash of lxcpath? a subset of it?
*/
len = sizeof(addr.sun_path) - 1;
ret = snprintf(offset, len, "%s/lxc-monitor", lxcpath);
if (ret < 0 || ret >= len) {
ERROR("lxcpath too long to open monitor");
return;
}

fd = socket(PF_UNIX, SOCK_DGRAM, 0);
if (fd < 0)
Expand All @@ -65,23 +75,33 @@ static void lxc_monitor_send(struct lxc_msg *msg)
close(fd);
}

void lxc_monitor_send_state(const char *name, lxc_state_t state)
void lxc_monitor_send_state(const char *name, lxc_state_t state, const char *lxcpath)
{
struct lxc_msg msg = { .type = lxc_msg_state,
.value = state };
strncpy(msg.name, name, sizeof(msg.name));
msg.name[sizeof(msg.name) - 1] = 0;

lxc_monitor_send(&msg);
lxc_monitor_send(&msg, lxcpath);
}

int lxc_monitor_open(void)
int lxc_monitor_open(const char *lxcpath)
{
struct sockaddr_un addr = { .sun_family = AF_UNIX };
char *offset = &addr.sun_path[1];
int fd;

strcpy(offset, "lxc-monitor");
size_t ret, len;

/*
* addr.sun_path is only 108 bytes.
* should we take a hash of lxcpath? a subset of it?
*/
len = sizeof(addr.sun_path) - 1;
ret = snprintf(offset, len, "%s/lxc-monitor", lxcpath);
if (ret < 0 || ret >= len) {
ERROR("lxcpath too long to open monitor");
return -1;
}

fd = socket(PF_UNIX, SOCK_DGRAM, 0);
if (fd < 0) {
Expand Down
5 changes: 2 additions & 3 deletions src/lxc/monitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ struct lxc_msg {
int value;
};

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_send_state(const char *name, lxc_state_t state,
const char *lxcpath);

#endif
3 changes: 2 additions & 1 deletion src/lxc/start.c
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ int lxc_clone_flags_callback(int fd, struct lxc_request *request,
int lxc_set_state(const char *name, struct lxc_handler *handler, lxc_state_t state)
{
handler->state = state;
lxc_monitor_send_state(name, state);
lxc_monitor_send_state(name, state, handler->lxcpath);
return 0;
}

Expand Down Expand Up @@ -379,6 +379,7 @@ struct lxc_handler *lxc_init(const char *name, struct lxc_conf *conf, const char
memset(handler, 0, sizeof(*handler));

handler->conf = conf;
handler->lxcpath = lxcpath;

apparmor_handler_init(handler);
handler->name = strdup(name);
Expand Down
1 change: 1 addition & 0 deletions src/lxc/start.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ struct lxc_handler {
int aa_enabled;
#endif
int pinfd;
const char *lxcpath;
};

extern struct lxc_handler *lxc_init(const char *name, struct lxc_conf *, const char *);
Expand Down
2 changes: 1 addition & 1 deletion src/lxc/state.c
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ extern int lxc_wait(const char *lxcname, const char *states, int timeout, const
if (fillwaitedstates(states, s))
return -1;

fd = lxc_monitor_open();
fd = lxc_monitor_open(lxcpath);
if (fd < 0)
return -1;

Expand Down
2 changes: 1 addition & 1 deletion src/lxc/stop.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ extern int lxc_stop_callback(int fd, struct lxc_request *request,

answer.ret = kill(handler->pid, SIGKILL);
if (!answer.ret) {
ret = lxc_unfreeze(handler->name);
ret = lxc_unfreeze(handler->name, handler->lxcpath);
if (!ret)
return 0;

Expand Down

0 comments on commit 9123e47

Please sign in to comment.