]> git.proxmox.com Git - mirror_lxc.git/blame - src/lxc/commands.c
lxc-execute: find lxc-init
[mirror_lxc.git] / src / lxc / commands.c
CommitLineData
724e753c
MN
1/*
2 * lxc: linux Container library
3 *
4 * (C) Copyright IBM Corp. 2007, 2009
5 *
6 * Authors:
7 * Daniel Lezcano <dlezcano at fr.ibm.com>
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23
24#include <stdio.h>
25#include <errno.h>
26#include <unistd.h>
27#include <signal.h>
91480a0f 28#include <fcntl.h>
724e753c
MN
29#include <sys/socket.h>
30#include <sys/un.h>
31#include <sys/poll.h>
32#include <sys/param.h>
33
00b3c2e2
CLG
34#include <lxc/log.h>
35#include <lxc/conf.h>
36#include <lxc/start.h> /* for struct lxc_handler */
724e753c
MN
37
38#include "commands.h"
39#include "mainloop.h"
40#include "af_unix.h"
adaeaa99 41#include "config.h"
724e753c 42
ded1d23f
DL
43/*
44 * This file provides the different functions to have the client
45 * and the server to communicate
46 *
47 * Each command is transactional, the client send a request to
48 * the server and the server answer the request with a message
49 * giving the request's status (zero or a negative errno value).
50 *
51 * Each command is wrapped in a ancillary message in order to pass
52 * a credential making possible to the server to check if the client
53 * is allowed to ask for this command or not.
54 *
55 */
56
724e753c
MN
57lxc_log_define(lxc_commands, lxc);
58
adaeaa99 59#define abstractname LXCPATH "/%s/command"
ded1d23f 60
724e753c
MN
61static int receive_answer(int sock, struct lxc_answer *answer)
62{
63 int ret;
64
65 ret = lxc_af_unix_recv_fd(sock, &answer->fd, answer, sizeof(*answer));
66 if (ret < 0)
67 ERROR("failed to receive answer for the command");
68
69 return ret;
70}
71
43eb6f29
DL
72static int __lxc_command(const char *name, struct lxc_command *command,
73 int *stopped, int stay_connected)
724e753c 74{
724e753c 75 int sock, ret = -1;
46968ea3
DL
76 char path[sizeof(((struct sockaddr_un *)0)->sun_path)] = { 0 };
77 char *offset = &path[1];
724e753c 78
ded1d23f 79 sprintf(offset, abstractname, name);
724e753c 80
46968ea3 81 sock = lxc_af_unix_connect(path);
d97b36f8
DL
82 if (sock < 0 && errno == ECONNREFUSED) {
83 *stopped = 1;
84 return -1;
85 }
86
724e753c 87 if (sock < 0) {
d97b36f8 88 SYSERROR("failed to connect to '@%s'", offset);
724e753c
MN
89 return -1;
90 }
91
92 ret = lxc_af_unix_send_credential(sock, &command->request,
93 sizeof(command->request));
94 if (ret < 0) {
d97b36f8 95 SYSERROR("failed to send request to '@%s'", offset);
1362f2eb 96 goto out;
724e753c
MN
97 }
98
99 if (ret != sizeof(command->request)) {
d97b36f8 100 SYSERROR("message partially sent to '@%s'", offset);
1362f2eb 101 goto out;
724e753c
MN
102 }
103
104 ret = receive_answer(sock, &command->answer);
724e753c 105out:
43eb6f29
DL
106 if (!stay_connected || ret < 0)
107 close(sock);
108
1362f2eb 109 return ret;
724e753c
MN
110}
111
43eb6f29
DL
112extern int lxc_command(const char *name,
113 struct lxc_command *command, int *stopped)
114{
115 return __lxc_command(name, command, stopped, 0);
116}
117
118extern int lxc_command_connected(const char *name,
119 struct lxc_command *command, int *stopped)
120{
121 return __lxc_command(name, command, stopped, 1);
122}
123
124
26b2d152
MN
125pid_t get_init_pid(const char *name)
126{
127 struct lxc_command command = {
128 .request = { .type = LXC_COMMAND_PID },
129 };
130
131 int ret, stopped = 0;
132
133 ret = lxc_command(name, &command, &stopped);
ebdd307d 134 if (ret < 0 && stopped)
26b2d152 135 return -1;
26b2d152
MN
136
137 if (ret < 0) {
138 ERROR("failed to send command");
139 return -1;
140 }
141
142 if (command.answer.ret) {
143 ERROR("failed to retrieve the init pid: %s",
144 strerror(-command.answer.ret));
145 return -1;
146 }
147
148 return command.answer.pid;
149}
150
ded1d23f
DL
151extern void lxc_console_remove_fd(int, struct lxc_tty_info *);
152extern int lxc_console_callback(int, struct lxc_request *, struct lxc_handler *);
153extern int lxc_stop_callback(int, struct lxc_request *, struct lxc_handler *);
154extern int lxc_state_callback(int, struct lxc_request *, struct lxc_handler *);
81c75799 155extern int lxc_pid_callback(int, struct lxc_request *, struct lxc_handler *);
724e753c
MN
156
157static int trigger_command(int fd, struct lxc_request *request,
ded1d23f 158 struct lxc_handler *handler)
724e753c 159{
ded1d23f 160 typedef int (*callback)(int, struct lxc_request *, struct lxc_handler *);
724e753c
MN
161
162 callback cb[LXC_COMMAND_MAX] = {
ded1d23f
DL
163 [LXC_COMMAND_TTY] = lxc_console_callback,
164 [LXC_COMMAND_STOP] = lxc_stop_callback,
e98fe68b 165 [LXC_COMMAND_STATE] = lxc_state_callback,
81c75799 166 [LXC_COMMAND_PID] = lxc_pid_callback,
724e753c
MN
167 };
168
169 if (request->type < 0 || request->type >= LXC_COMMAND_MAX)
170 return -1;
171
172 return cb[request->type](fd, request, handler);
173}
174
175static void command_fd_cleanup(int fd, struct lxc_handler *handler,
ded1d23f 176 struct lxc_epoll_descr *descr)
724e753c 177{
fae349da 178 lxc_console_remove_fd(fd, &handler->conf->tty_info);
724e753c
MN
179 lxc_mainloop_del_handler(descr, fd);
180 close(fd);
181}
182
ded1d23f 183static int command_handler(int fd, void *data, struct lxc_epoll_descr *descr)
724e753c
MN
184{
185 int ret;
186 struct lxc_request request;
187 struct lxc_handler *handler = data;
188
189 ret = lxc_af_unix_rcv_credential(fd, &request, sizeof(request));
0a3ec350 190 if (ret == -EACCES) {
3cc5de36
MN
191 /* we don't care for the peer, just send and close */
192 struct lxc_answer answer = { .ret = ret };
193 send(fd, &answer, sizeof(answer), 0);
194 goto out_close;
ded1d23f
DL
195 }
196
197 if (ret < 0) {
724e753c
MN
198 SYSERROR("failed to receive data on command socket");
199 goto out_close;
200 }
201
202 if (!ret) {
203 DEBUG("peer has disconnected");
204 goto out_close;
205 }
206
207 if (ret != sizeof(request)) {
208 WARN("partial request, ignored");
209 goto out_close;
210 }
211
212 ret = trigger_command(fd, &request, handler);
213 if (ret) {
214 /* this is not an error, but only a request to close fd */
215 ret = 0;
216 goto out_close;
217 }
218
219out:
220 return ret;
221out_close:
222 command_fd_cleanup(fd, handler, descr);
223 goto out;
224}
225
226static int incoming_command_handler(int fd, void *data,
227 struct lxc_epoll_descr *descr)
228{
ded1d23f 229 int opt = 1, ret = -1, connection;
724e753c
MN
230
231 connection = accept(fd, NULL, 0);
232 if (connection < 0) {
233 SYSERROR("failed to accept connection");
234 return -1;
235 }
236
9ccb2dbc
DL
237 if (fcntl(connection, F_SETFD, FD_CLOEXEC)) {
238 SYSERROR("failed to set close-on-exec on incoming connection");
239 goto out_close;
240 }
241
0a3ec350
DL
242 if (setsockopt(connection, SOL_SOCKET,
243 SO_PASSCRED, &opt, sizeof(opt))) {
724e753c
MN
244 SYSERROR("failed to enable credential on socket");
245 goto out_close;
246 }
247
248 ret = lxc_mainloop_add_handler(descr, connection, command_handler, data);
249 if (ret) {
250 ERROR("failed to add handler");
251 goto out_close;
252 }
253
254out:
255 return ret;
256
257out_close:
258 close(connection);
259 goto out;
260}
261
0a3ec350
DL
262extern int lxc_command_mainloop_add(const char *name,
263 struct lxc_epoll_descr *descr,
724e753c
MN
264 struct lxc_handler *handler)
265{
266 int ret, fd;
46968ea3
DL
267 char path[sizeof(((struct sockaddr_un *)0)->sun_path)] = { 0 };
268 char *offset = &path[1];
724e753c 269
ded1d23f 270 sprintf(offset, abstractname, name);
724e753c 271
46968ea3 272 fd = lxc_af_unix_open(path, SOCK_STREAM, 0);
724e753c 273 if (fd < 0) {
97d3756c
SH
274 ERROR("failed (%d) to create the command service point %s", errno, offset);
275 if (errno == EADDRINUSE) {
276 ERROR("##");
277 ERROR("# The container appears to be already running!");
278 ERROR("##");
279 }
724e753c
MN
280 return -1;
281 }
282
91480a0f
DL
283 if (fcntl(fd, F_SETFD, FD_CLOEXEC)) {
284 SYSERROR("failed to set sigfd to close-on-exec");
285 close(fd);
286 return -1;
287 }
288
0a3ec350
DL
289 ret = lxc_mainloop_add_handler(descr, fd, incoming_command_handler,
290 handler);
724e753c
MN
291 if (ret) {
292 ERROR("failed to add handler for command socket");
293 close(fd);
294 }
295
296 return ret;
297}