Ejemplo n.º 1
0
int lxc_monitor_read(int fd, struct lxc_msg *msg)
{
	return lxc_monitor_read_timeout(fd, msg, -1);
}
Ejemplo n.º 2
0
extern int lxc_wait(const char *lxcname, const char *states, int timeout, const char *lxcpath)
{
	struct lxc_msg msg;
	int state, ret;
	int s[MAX_STATE] = { }, fd;

	if (fillwaitedstates(states, s))
		return -1;

	if (lxc_monitord_spawn(lxcpath))
		return -1;

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

	/*
	 * if container present,
	 * then check if already in requested state
	 */
	ret = -1;
	state = lxc_getstate(lxcname, lxcpath);
	if (state < 0) {
		goto out_close;
	} else if ((state >= 0) && (s[state])) {
		ret = 0;
		goto out_close;
	}

	for (;;) {
		int elapsed_time, curtime = 0;
		struct timeval tv;
		int stop = 0;
		int retval;

		if (timeout != -1) {
			retval = gettimeofday(&tv, NULL);
			if (retval)
				goto out_close;
			curtime = tv.tv_sec;
		}
		if (lxc_monitor_read_timeout(fd, &msg, timeout) < 0) {
			/* try again if select interrupted by signal */
			if (errno != EINTR)
				goto out_close;
		}

		if (timeout != -1) {
			retval = gettimeofday(&tv, NULL);
			if (retval)
				goto out_close;
			elapsed_time = tv.tv_sec - curtime;
			if (timeout - elapsed_time <= 0)
				stop = 1;
			timeout -= elapsed_time;
		}

		if (strcmp(lxcname, msg.name)) {
			if (stop) {
				ret = -2;
				goto out_close;
			}
			continue;
		}

		switch (msg.type) {
		case lxc_msg_state:
			if (msg.value < 0 || msg.value >= MAX_STATE) {
				ERROR("Receive an invalid state number '%d'",
					msg.value);
				goto out_close;
			}

			if (s[msg.value]) {
				ret = 0;
				goto out_close;
			}
			break;
		default:
			if (stop) {
				ret = -2;
				goto out_close;
			}
			/* just ignore garbage */
			break;
		}
	}

out_close:
	lxc_monitor_close(fd);
	return ret;
}