]>
Commit | Line | Data |
---|---|---|
92e35018 CB |
1 | /* liblxcapi |
2 | * | |
3 | * Copyright © 2017 Christian Brauner <christian.brauner@ubuntu.com>. | |
4 | * Copyright © 2017 Canonical Ltd. | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License version 2, as | |
8 | * published by the Free Software Foundation. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License along | |
16 | * with this program; if not, write to the Free Software Foundation, Inc., | |
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
18 | */ | |
19 | ||
20 | #define _GNU_SOURCE | |
21 | #include <errno.h> | |
22 | #include <stdio.h> | |
23 | #include <string.h> | |
24 | #include <unistd.h> | |
25 | #include <sys/socket.h> | |
26 | #include <sys/un.h> | |
27 | ||
28 | #include "commands.h" | |
29 | #include "commands_utils.h" | |
30 | #include "log.h" | |
31 | #include "monitor.h" | |
32 | #include "state.h" | |
33 | ||
34 | lxc_log_define(lxc_commands_utils, lxc); | |
35 | ||
36 | int lxc_cmd_sock_rcv_state(int state_client_fd, int timeout) | |
37 | { | |
38 | int ret; | |
39 | struct lxc_msg msg; | |
40 | struct timeval out; | |
41 | ||
42 | memset(&out, 0, sizeof(out)); | |
43 | out.tv_sec = timeout; | |
44 | ret = setsockopt(state_client_fd, SOL_SOCKET, SO_RCVTIMEO, | |
45 | (const void *)&out, sizeof(out)); | |
46 | if (ret < 0) { | |
47 | SYSERROR("Failed to set %ds timeout on containter state socket", timeout); | |
48 | return -1; | |
49 | } | |
50 | ||
51 | memset(&msg, 0, sizeof(msg)); | |
52 | ||
53 | again: | |
54 | ret = recv(state_client_fd, &msg, sizeof(msg), 0); | |
55 | if (ret < 0) { | |
56 | if (errno == EINTR) | |
57 | goto again; | |
58 | ||
59 | ERROR("failed to receive message: %s", strerror(errno)); | |
60 | return -1; | |
61 | } | |
62 | ||
63 | if (ret == 0) { | |
64 | ERROR("length of message was 0"); | |
65 | return -1; | |
66 | } | |
67 | ||
68 | TRACE("received state %s from state client %d", | |
69 | lxc_state2str(msg.value), state_client_fd); | |
70 | ||
71 | return msg.value; | |
72 | } | |
73 | ||
74 | /* Register a new state client and retrieve state from command socket. */ | |
75 | int lxc_cmd_sock_get_state(const char *name, const char *lxcpath, | |
76 | lxc_state_t states[MAX_STATE], int timeout) | |
77 | { | |
78 | int ret; | |
79 | int state_client_fd; | |
80 | ||
81 | ret = lxc_cmd_add_state_client(name, lxcpath, states, &state_client_fd); | |
82 | if (ret < 0) | |
83 | return -1; | |
84 | ||
85 | if (ret < MAX_STATE) | |
86 | return ret; | |
87 | ||
88 | ret = lxc_cmd_sock_rcv_state(state_client_fd, timeout); | |
89 | close(state_client_fd); | |
90 | return ret; | |
91 | } |