lxccontainer: ongoing_create()

parent ef1ab8f1
...@@ -127,48 +127,48 @@ static bool config_file_exists(const char *lxcpath, const char *cname) ...@@ -127,48 +127,48 @@ static bool config_file_exists(const char *lxcpath, const char *cname)
return file_exists(fname); return file_exists(fname);
} }
/* /* A few functions to help detect when a container creation failed. If a
* A few functions to help detect when a container creation failed. * container creation was killed partway through, then trying to actually start
* If a container creation was killed partway through, then trying * that container could harm the host. We detect this by creating a 'partial'
* to actually start that container could harm the host. We detect * file under the container directory, and keeping an advisory lock. When
* this by creating a 'partial' file under the container directory, * container creation completes, we remove that file. When we load or try to
* and keeping an advisory lock. When container creation completes, * start a container, if we find that file, without a flock, we remove the
* we remove that file. When we load or try to start a container, if * container.
* we find that file, without a flock, we remove the container.
*/ */
static int ongoing_create(struct lxc_container *c) static int ongoing_create(struct lxc_container *c)
{ {
int len = strlen(c->config_path) + strlen(c->name) + 10;
char *path = alloca(len);
int fd, ret; int fd, ret;
size_t len;
char *path;
struct flock lk; struct flock lk;
len = strlen(c->config_path) + strlen(c->name) + 10;
path = alloca(len);
ret = snprintf(path, len, "%s/%s/partial", c->config_path, c->name); ret = snprintf(path, len, "%s/%s/partial", c->config_path, c->name);
if (ret < 0 || ret >= len) { if (ret < 0 || (size_t)ret >= len)
ERROR("Error writing partial pathname");
return -1; return -1;
}
if (!file_exists(path)) if (!file_exists(path))
return 0; return 0;
fd = open(path, O_RDWR); fd = open(path, O_RDWR);
if (fd < 0) { if (fd < 0)
/* give benefit of the doubt */
SYSERROR("Error opening partial file");
return 0; return 0;
}
lk.l_type = F_WRLCK; lk.l_type = F_WRLCK;
lk.l_whence = SEEK_SET; lk.l_whence = SEEK_SET;
lk.l_start = 0; lk.l_start = 0;
lk.l_len = 0; lk.l_len = 0;
lk.l_pid = -1; lk.l_pid = -1;
if (fcntl(fd, F_GETLK, &lk) == 0 && lk.l_pid != -1) {
ret = fcntl(fd, F_GETLK, &lk);
close(fd);
if (ret == 0 && lk.l_pid != -1) {
/* create is still ongoing */ /* create is still ongoing */
close(fd);
return 1; return 1;
} }
/* create completed but partial is still there. */
close(fd); /* Create completed but partial is still there. */
return 2; return 2;
} }
......
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