Commit 9123e471 by Serge Hallyn Committed by Stéphane Graber

lxc_monitor_open: prepend lxcpath

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: 's avatarSerge Hallyn <serge.hallyn@ubuntu.com> Acked-by: 's avatarStéphane Graber <stgraber@ubuntu.com>
parent fbf5de31
...@@ -40,7 +40,7 @@ ...@@ -40,7 +40,7 @@
lxc_log_define(lxc_freezer, lxc); 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 *nsgroup;
char freezer[MAXPATHLEN], *f; char freezer[MAXPATHLEN], *f;
...@@ -98,7 +98,7 @@ static int freeze_unfreeze(const char *name, int freeze) ...@@ -98,7 +98,7 @@ static int freeze_unfreeze(const char *name, int freeze)
ret = strncmp(f, tmpf, strlen(f)); ret = strncmp(f, tmpf, strlen(f));
if (!ret) if (!ret)
{ {
lxc_monitor_send_state(name, freeze ? FROZEN : THAWED); lxc_monitor_send_state(name, freeze ? FROZEN : THAWED, lxcpath);
break; /* Success */ break; /* Success */
} }
...@@ -122,14 +122,14 @@ out: ...@@ -122,14 +122,14 @@ out:
return ret; return ret;
} }
int lxc_freeze(const char *name) int lxc_freeze(const char *name, const char *lxcpath)
{ {
lxc_monitor_send_state(name, FREEZING); lxc_monitor_send_state(name, FREEZING, lxcpath);
return freeze_unfreeze(name, 1); 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);
} }
...@@ -74,7 +74,7 @@ extern int lxc_execute(const char *name, char *const argv[], int quiet, ...@@ -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 * The function will return an fd corresponding to the events
* Returns a file descriptor on success, < 0 otherwise * 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 * Read the state of the container if this one has changed
...@@ -108,14 +108,14 @@ extern int lxc_console(const char *name, int ttynum, int *fd, const char *lxcpat ...@@ -108,14 +108,14 @@ extern int lxc_console(const char *name, int ttynum, int *fd, const char *lxcpat
* @name : the container name * @name : the container name
* Returns 0 on success, < 0 otherwise * 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. * Unfreeze all previously frozen tasks.
* @name : the name of the container * @name : the name of the container
* Return 0 on sucess, < 0 otherwise * 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 * Retrieve the container state
......
...@@ -58,6 +58,6 @@ int main(int argc, char *argv[]) ...@@ -58,6 +58,6 @@ int main(int argc, char *argv[])
my_args.progname, my_args.quiet)) my_args.progname, my_args.quiet))
return -1; return -1;
return lxc_freeze(my_args.name); return lxc_freeze(my_args.name, my_args.lxcpath);
} }
...@@ -87,7 +87,7 @@ int main(int argc, char *argv[]) ...@@ -87,7 +87,7 @@ int main(int argc, char *argv[])
return -1; return -1;
} }
fd = lxc_monitor_open(); fd = lxc_monitor_open(my_args.lxcpath);
if (fd < 0) if (fd < 0)
return -1; return -1;
......
...@@ -57,6 +57,6 @@ int main(int argc, char *argv[]) ...@@ -57,6 +57,6 @@ int main(int argc, char *argv[])
my_args.progname, my_args.quiet)) my_args.progname, my_args.quiet))
return -1; return -1;
return lxc_unfreeze(my_args.name); return lxc_unfreeze(my_args.name, my_args.lxcpath);
} }
...@@ -195,7 +195,7 @@ static bool lxcapi_freeze(struct lxc_container *c) ...@@ -195,7 +195,7 @@ static bool lxcapi_freeze(struct lxc_container *c)
if (lxclock(c->slock, 0)) if (lxclock(c->slock, 0))
return false; return false;
ret = lxc_freeze(c->name); ret = lxc_freeze(c->name, c->config_path);
lxcunlock(c->slock); lxcunlock(c->slock);
if (ret) if (ret)
return false; return false;
...@@ -210,7 +210,7 @@ static bool lxcapi_unfreeze(struct lxc_container *c) ...@@ -210,7 +210,7 @@ static bool lxcapi_unfreeze(struct lxc_container *c)
if (lxclock(c->slock, 0)) if (lxclock(c->slock, 0))
return false; return false;
ret = lxc_unfreeze(c->name); ret = lxc_unfreeze(c->name, c->config_path);
lxcunlock(c->slock); lxcunlock(c->slock);
if (ret) if (ret)
return false; return false;
......
...@@ -47,13 +47,23 @@ lxc_log_define(lxc_monitor, lxc); ...@@ -47,13 +47,23 @@ lxc_log_define(lxc_monitor, lxc);
#define UNIX_PATH_MAX 108 #define UNIX_PATH_MAX 108
#endif #endif
static void lxc_monitor_send(struct lxc_msg *msg) static void lxc_monitor_send(struct lxc_msg *msg, const char *lxcpath)
{ {
int fd; int fd;
struct sockaddr_un addr = { .sun_family = AF_UNIX }; struct sockaddr_un addr = { .sun_family = AF_UNIX };
char *offset = &addr.sun_path[1]; char *offset = &addr.sun_path[1];
size_t ret, len;
strcpy(offset, "lxc-monitor");
/*
* 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); fd = socket(PF_UNIX, SOCK_DGRAM, 0);
if (fd < 0) if (fd < 0)
...@@ -65,23 +75,33 @@ static void lxc_monitor_send(struct lxc_msg *msg) ...@@ -65,23 +75,33 @@ static void lxc_monitor_send(struct lxc_msg *msg)
close(fd); 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, struct lxc_msg msg = { .type = lxc_msg_state,
.value = state }; .value = state };
strncpy(msg.name, name, sizeof(msg.name)); strncpy(msg.name, name, sizeof(msg.name));
msg.name[sizeof(msg.name) - 1] = 0; 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 }; struct sockaddr_un addr = { .sun_family = AF_UNIX };
char *offset = &addr.sun_path[1]; char *offset = &addr.sun_path[1];
int fd; int fd;
size_t ret, len;
strcpy(offset, "lxc-monitor");
/*
* 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); fd = socket(PF_UNIX, SOCK_DGRAM, 0);
if (fd < 0) { if (fd < 0) {
......
...@@ -36,8 +36,7 @@ struct lxc_msg { ...@@ -36,8 +36,7 @@ struct lxc_msg {
int value; 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); const char *lxcpath);
void lxc_monitor_cleanup(const char *name);
#endif #endif
...@@ -311,7 +311,7 @@ int lxc_clone_flags_callback(int fd, struct lxc_request *request, ...@@ -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) int lxc_set_state(const char *name, struct lxc_handler *handler, lxc_state_t state)
{ {
handler->state = state; handler->state = state;
lxc_monitor_send_state(name, state); lxc_monitor_send_state(name, state, handler->lxcpath);
return 0; return 0;
} }
...@@ -379,6 +379,7 @@ struct lxc_handler *lxc_init(const char *name, struct lxc_conf *conf, const char ...@@ -379,6 +379,7 @@ struct lxc_handler *lxc_init(const char *name, struct lxc_conf *conf, const char
memset(handler, 0, sizeof(*handler)); memset(handler, 0, sizeof(*handler));
handler->conf = conf; handler->conf = conf;
handler->lxcpath = lxcpath;
apparmor_handler_init(handler); apparmor_handler_init(handler);
handler->name = strdup(name); handler->name = strdup(name);
......
...@@ -50,6 +50,7 @@ struct lxc_handler { ...@@ -50,6 +50,7 @@ struct lxc_handler {
int aa_enabled; int aa_enabled;
#endif #endif
int pinfd; int pinfd;
const char *lxcpath;
}; };
extern struct lxc_handler *lxc_init(const char *name, struct lxc_conf *, const char *); extern struct lxc_handler *lxc_init(const char *name, struct lxc_conf *, const char *);
......
...@@ -200,7 +200,7 @@ extern int lxc_wait(const char *lxcname, const char *states, int timeout, const ...@@ -200,7 +200,7 @@ extern int lxc_wait(const char *lxcname, const char *states, int timeout, const
if (fillwaitedstates(states, s)) if (fillwaitedstates(states, s))
return -1; return -1;
fd = lxc_monitor_open(); fd = lxc_monitor_open(lxcpath);
if (fd < 0) if (fd < 0)
return -1; return -1;
......
...@@ -85,7 +85,7 @@ extern int lxc_stop_callback(int fd, struct lxc_request *request, ...@@ -85,7 +85,7 @@ extern int lxc_stop_callback(int fd, struct lxc_request *request,
answer.ret = kill(handler->pid, SIGKILL); answer.ret = kill(handler->pid, SIGKILL);
if (!answer.ret) { if (!answer.ret) {
ret = lxc_unfreeze(handler->name); ret = lxc_unfreeze(handler->name, handler->lxcpath);
if (!ret) if (!ret)
return 0; return 0;
......
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