]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/commands.c
commands: add LXC_CMD_SERVE_STATE_CLIENTS
[mirror_lxc.git] / src / lxc / commands.c
1 /*
2 * lxc: linux Container library
3 *
4 * (C) Copyright IBM Corp. 2007, 2009
5 *
6 * Authors:
7 * Daniel Lezcano <daniel.lezcano at free.fr>
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24 #include "config.h"
25
26 #include <caps.h>
27 #include <errno.h>
28 #include <fcntl.h>
29 #include <malloc.h>
30 #include <poll.h>
31 #include <signal.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <unistd.h>
35 #include <sys/param.h>
36 #include <sys/socket.h>
37 #include <sys/un.h>
38
39 #include "af_unix.h"
40 #include "cgroup.h"
41 #include "commands.h"
42 #include "commands_utils.h"
43 #include "conf.h"
44 #include "confile.h"
45 #include "console.h"
46 #include "log.h"
47 #include "lxc.h"
48 #include "lxclock.h"
49 #include "mainloop.h"
50 #include "monitor.h"
51 #include "start.h"
52 #include "utils.h"
53
54 /*
55 * This file provides the different functions for clients to query/command the
56 * server. The client is typically some lxc tool and the server is typically the
57 * container (ie. lxc-start).
58 *
59 * Each command is transactional, the clients send a request to the server and
60 * the server answers the request with a message giving the request's status
61 * (zero or a negative errno value). Both the request and response may contain
62 * additional data.
63 *
64 * Each command is wrapped in a ancillary message in order to pass a credential
65 * making possible to the server to check if the client is allowed to ask for
66 * this command or not.
67 *
68 * IMPORTANTLY: Note that semantics for current commands are fixed. If you wish
69 * to make any changes to how, say, LXC_CMD_GET_CONFIG_ITEM works by adding
70 * information to the end of cmd.data, then you must introduce a new
71 * LXC_CMD_GET_CONFIG_ITEM_V2 define with a new number. You may wish to also
72 * mark LXC_CMD_GET_CONFIG_ITEM deprecated in commands.h.
73 *
74 * This is necessary in order to avoid having a newly compiled lxc command
75 * communicating with a running (old) monitor from crashing the running
76 * container.
77 */
78
79 lxc_log_define(lxc_commands, lxc);
80
81 static const char *lxc_cmd_str(lxc_cmd_t cmd)
82 {
83 static const char *const cmdname[LXC_CMD_MAX] = {
84 [LXC_CMD_CONSOLE] = "console",
85 [LXC_CMD_CONSOLE_WINCH] = "console_winch",
86 [LXC_CMD_STOP] = "stop",
87 [LXC_CMD_GET_STATE] = "get_state",
88 [LXC_CMD_GET_INIT_PID] = "get_init_pid",
89 [LXC_CMD_GET_CLONE_FLAGS] = "get_clone_flags",
90 [LXC_CMD_GET_CGROUP] = "get_cgroup",
91 [LXC_CMD_GET_CONFIG_ITEM] = "get_config_item",
92 [LXC_CMD_GET_NAME] = "get_name",
93 [LXC_CMD_GET_LXCPATH] = "get_lxcpath",
94 [LXC_CMD_ADD_STATE_CLIENT] = "add_state_client",
95 [LXC_CMD_CONSOLE_LOG] = "console_log",
96 [LXC_CMD_SERVE_STATE_CLIENTS] = "serve_state_clients",
97 };
98
99 if (cmd >= LXC_CMD_MAX)
100 return "Unknown cmd";
101
102 return cmdname[cmd];
103 }
104
105 /*
106 * lxc_cmd_rsp_recv: Receive a response to a command
107 *
108 * @sock : the socket connected to the container
109 * @cmd : command to put response in
110 *
111 * Returns the size of the response message or < 0 on failure
112 *
113 * Note that if the command response datalen > 0, then data is
114 * a malloc()ed buffer and should be free()ed by the caller. If
115 * the response data is <= a void * worth of data, it will be
116 * stored directly in data and datalen will be 0.
117 *
118 * As a special case, the response for LXC_CMD_CONSOLE is created
119 * here as it contains an fd for the master pty passed through the
120 * unix socket.
121 */
122 static int lxc_cmd_rsp_recv(int sock, struct lxc_cmd_rr *cmd)
123 {
124 int ret, rspfd;
125 struct lxc_cmd_rsp *rsp = &cmd->rsp;
126
127 ret = lxc_abstract_unix_recv_fds(sock, &rspfd, 1, rsp, sizeof(*rsp));
128 if (ret < 0) {
129 WARN("%s - Failed to receive response for command \"%s\"",
130 strerror(errno), lxc_cmd_str(cmd->req.cmd));
131 if (errno == ECONNRESET)
132 return -ECONNRESET;
133
134 return -1;
135 }
136 TRACE("Command \"%s\" received response", lxc_cmd_str(cmd->req.cmd));
137
138 if (cmd->req.cmd == LXC_CMD_CONSOLE) {
139 struct lxc_cmd_console_rsp_data *rspdata;
140
141 /* recv() returns 0 bytes when a tty cannot be allocated,
142 * rsp->ret is < 0 when the peer permission check failed
143 */
144 if (ret == 0 || rsp->ret < 0)
145 return 0;
146
147 rspdata = malloc(sizeof(*rspdata));
148 if (!rspdata) {
149 ERROR("Failed to allocate response buffer for command \"%s\"",
150 lxc_cmd_str(cmd->req.cmd));
151 return -ENOMEM;
152 }
153 rspdata->masterfd = rspfd;
154 rspdata->ttynum = PTR_TO_INT(rsp->data);
155 rsp->data = rspdata;
156 }
157
158 if (rsp->datalen == 0) {
159 DEBUG("Response data length for command \"%s\" is 0",
160 lxc_cmd_str(cmd->req.cmd));
161 return ret;
162 }
163
164 if ((rsp->datalen > LXC_CMD_DATA_MAX) &&
165 (cmd->req.cmd != LXC_CMD_CONSOLE_LOG)) {
166 errno = EFBIG;
167 ERROR("%s - Response data for command \"%s\" is too long: %d "
168 "bytes > %d", strerror(errno), lxc_cmd_str(cmd->req.cmd),
169 rsp->datalen, LXC_CMD_DATA_MAX);
170 return -EFBIG;
171 }
172
173 if (cmd->req.cmd == LXC_CMD_CONSOLE_LOG) {
174 rsp->data = malloc(rsp->datalen + 1);
175 ((char *)rsp->data)[rsp->datalen] = '\0';
176 } else {
177 rsp->data = malloc(rsp->datalen);
178 }
179 if (!rsp->data) {
180 errno = ENOMEM;
181 ERROR("%s - Failed to allocate response buffer for command "
182 "\"%s\"", strerror(errno), lxc_cmd_str(cmd->req.cmd));
183 return -ENOMEM;
184 }
185
186 ret = recv(sock, rsp->data, rsp->datalen, 0);
187 if (ret != rsp->datalen) {
188 ERROR("%s - Failed to receive response data for command \"%s\"",
189 lxc_cmd_str(cmd->req.cmd), strerror(errno));
190 if (ret >= 0)
191 ret = -1;
192 }
193
194 return ret;
195 }
196
197 /*
198 * lxc_cmd_rsp_send: Send a command response
199 *
200 * @fd : file descriptor of socket to send response on
201 * @rsp : response to send
202 *
203 * Returns 0 on success, < 0 on failure
204 */
205 static int lxc_cmd_rsp_send(int fd, struct lxc_cmd_rsp *rsp)
206 {
207 ssize_t ret;
208
209 ret = send(fd, rsp, sizeof(*rsp), 0);
210 if (ret < 0 || (size_t)ret != sizeof(*rsp)) {
211 ERROR("%s - Failed to send command response %zd",
212 strerror(errno), ret);
213 return -1;
214 }
215
216 if (rsp->datalen <= 0)
217 return 0;
218
219 ret = send(fd, rsp->data, rsp->datalen, 0);
220 if (ret < 0 || ret != (ssize_t)rsp->datalen) {
221 WARN("%s - Failed to send command response data %zd",
222 strerror(errno), ret);
223 return -1;
224 }
225
226 return 0;
227 }
228
229 static int lxc_cmd_send(const char *name, struct lxc_cmd_rr *cmd,
230 const char *lxcpath, const char *hashed_sock_name)
231 {
232 int client_fd;
233 ssize_t ret = -1;
234
235 client_fd = lxc_cmd_connect(name, lxcpath, hashed_sock_name, "command");
236 if (client_fd < 0) {
237 if (client_fd == -ECONNREFUSED)
238 return -ECONNREFUSED;
239
240 return -1;
241 }
242
243 ret = lxc_abstract_unix_send_credential(client_fd, &cmd->req,
244 sizeof(cmd->req));
245 if (ret < 0 || (size_t)ret != sizeof(cmd->req)) {
246 close(client_fd);
247
248 if (errno == EPIPE)
249 return -EPIPE;
250
251 if (ret >= 0)
252 return -EMSGSIZE;
253
254 return -1;
255 }
256
257 if (cmd->req.datalen <= 0)
258 return client_fd;
259
260 ret = send(client_fd, cmd->req.data, cmd->req.datalen, MSG_NOSIGNAL);
261 if (ret < 0 || ret != (ssize_t)cmd->req.datalen) {
262 close(client_fd);
263
264 if (errno == EPIPE)
265 return -EPIPE;
266
267 if (ret >= 0)
268 return -EMSGSIZE;
269
270 return -1;
271 }
272
273 return client_fd;
274 }
275
276 /*
277 * lxc_cmd: Connect to the specified running container, send it a command
278 * request and collect the response
279 *
280 * @name : name of container to connect to
281 * @cmd : command with initialized request to send
282 * @stopped : output indicator if the container was not running
283 * @lxcpath : the lxcpath in which the container is running
284 *
285 * Returns the size of the response message on success, < 0 on failure
286 *
287 * Note that there is a special case for LXC_CMD_CONSOLE. For this command
288 * the fd cannot be closed because it is used as a placeholder to indicate
289 * that a particular tty slot is in use. The fd is also used as a signal to
290 * the container that when the caller dies or closes the fd, the container
291 * will notice the fd on its side of the socket in its mainloop select and
292 * then free the slot with lxc_cmd_fd_cleanup(). The socket fd will be
293 * returned in the cmd response structure.
294 */
295 static int lxc_cmd(const char *name, struct lxc_cmd_rr *cmd, int *stopped,
296 const char *lxcpath, const char *hashed_sock_name)
297 {
298 int client_fd;
299 int ret = -1;
300 bool stay_connected = false;
301
302 if (cmd->req.cmd == LXC_CMD_CONSOLE ||
303 cmd->req.cmd == LXC_CMD_ADD_STATE_CLIENT)
304 stay_connected = true;
305
306 *stopped = 0;
307
308 client_fd = lxc_cmd_send(name, cmd, lxcpath, hashed_sock_name);
309 if (client_fd < 0) {
310 TRACE("%s - Command \"%s\" failed to connect command socket",
311 strerror(errno), lxc_cmd_str(cmd->req.cmd));
312 if (client_fd == -ECONNREFUSED) {
313 *stopped = 1;
314 return -1;
315 }
316
317 if (client_fd == -EPIPE)
318 goto epipe;
319
320 goto out;
321 }
322
323 ret = lxc_cmd_rsp_recv(client_fd, cmd);
324 if (ret == -ECONNRESET)
325 *stopped = 1;
326 out:
327 if (!stay_connected || ret <= 0)
328 if (client_fd >= 0)
329 close(client_fd);
330
331 if (stay_connected && ret > 0)
332 cmd->rsp.ret = client_fd;
333
334 return ret;
335
336 epipe:
337 *stopped = 1;
338 return 0;
339 }
340
341 int lxc_try_cmd(const char *name, const char *lxcpath)
342 {
343 int stopped, ret;
344 struct lxc_cmd_rr cmd = {
345 .req = { .cmd = LXC_CMD_GET_INIT_PID },
346 };
347
348 ret = lxc_cmd(name, &cmd, &stopped, lxcpath, NULL);
349 if (stopped)
350 return 0;
351 if (ret > 0 && cmd.rsp.ret < 0) {
352 errno = cmd.rsp.ret;
353 return -1;
354 }
355 if (ret > 0)
356 return 0;
357
358 /* At this point we weren't denied access, and the container *was*
359 * started. There was some inexplicable error in the protocol. I'm not
360 * clear on whether we should return -1 here, but we didn't receive a
361 * -EACCES, so technically it's not that we're not allowed to control
362 * the container - it's just not behaving.
363 */
364 return 0;
365 }
366
367 /* Implentations of the commands and their callbacks */
368
369 /*
370 * lxc_cmd_get_init_pid: Get pid of the container's init process
371 *
372 * @name : name of container to connect to
373 * @lxcpath : the lxcpath in which the container is running
374 *
375 * Returns the pid on success, < 0 on failure
376 */
377 pid_t lxc_cmd_get_init_pid(const char *name, const char *lxcpath)
378 {
379 int ret, stopped;
380 struct lxc_cmd_rr cmd = {
381 .req = { .cmd = LXC_CMD_GET_INIT_PID },
382 };
383
384 ret = lxc_cmd(name, &cmd, &stopped, lxcpath, NULL);
385 if (ret < 0)
386 return ret;
387
388 return PTR_TO_INT(cmd.rsp.data);
389 }
390
391 static int lxc_cmd_get_init_pid_callback(int fd, struct lxc_cmd_req *req,
392 struct lxc_handler *handler)
393 {
394 struct lxc_cmd_rsp rsp = { .data = INT_TO_PTR(handler->pid) };
395
396 return lxc_cmd_rsp_send(fd, &rsp);
397 }
398
399 /*
400 * lxc_cmd_get_clone_flags: Get clone flags container was spawned with
401 *
402 * @name : name of container to connect to
403 * @lxcpath : the lxcpath in which the container is running
404 *
405 * Returns the clone flags on success, < 0 on failure
406 */
407 int lxc_cmd_get_clone_flags(const char *name, const char *lxcpath)
408 {
409 int ret, stopped;
410 struct lxc_cmd_rr cmd = {
411 .req = { .cmd = LXC_CMD_GET_CLONE_FLAGS },
412 };
413
414 ret = lxc_cmd(name, &cmd, &stopped, lxcpath, NULL);
415 if (ret < 0)
416 return ret;
417
418 return PTR_TO_INT(cmd.rsp.data);
419 }
420
421 static int lxc_cmd_get_clone_flags_callback(int fd, struct lxc_cmd_req *req,
422 struct lxc_handler *handler)
423 {
424 struct lxc_cmd_rsp rsp = { .data = INT_TO_PTR(handler->clone_flags) };
425
426 return lxc_cmd_rsp_send(fd, &rsp);
427 }
428
429 /*
430 * lxc_cmd_get_cgroup_path: Calculate a container's cgroup path for a
431 * particular subsystem. This is the cgroup path relative to the root
432 * of the cgroup filesystem.
433 *
434 * @name : name of container to connect to
435 * @lxcpath : the lxcpath in which the container is running
436 * @subsystem : the subsystem being asked about
437 *
438 * Returns the path on success, NULL on failure. The caller must free() the
439 * returned path.
440 */
441 char *lxc_cmd_get_cgroup_path(const char *name, const char *lxcpath,
442 const char *subsystem)
443 {
444 int ret, stopped;
445 struct lxc_cmd_rr cmd = {
446 .req = {
447 .cmd = LXC_CMD_GET_CGROUP,
448 .datalen = strlen(subsystem) + 1,
449 .data = subsystem,
450 },
451 };
452
453 ret = lxc_cmd(name, &cmd, &stopped, lxcpath, NULL);
454 if (ret < 0)
455 return NULL;
456
457 if (ret == 0)
458 return NULL;
459
460 if (cmd.rsp.ret < 0 || cmd.rsp.datalen < 0)
461 return NULL;
462
463 return cmd.rsp.data;
464 }
465
466 static int lxc_cmd_get_cgroup_callback(int fd, struct lxc_cmd_req *req,
467 struct lxc_handler *handler)
468 {
469 const char *path;
470 struct lxc_cmd_rsp rsp;
471
472 if (req->datalen < 1)
473 return -1;
474
475 path = cgroup_get_cgroup(handler, req->data);
476 if (!path)
477 return -1;
478
479 rsp.ret = 0;
480 rsp.datalen = strlen(path) + 1;
481 rsp.data = (char *)path;
482
483 return lxc_cmd_rsp_send(fd, &rsp);
484 }
485
486 /*
487 * lxc_cmd_get_config_item: Get config item the running container
488 *
489 * @name : name of container to connect to
490 * @item : the configuration item to retrieve (ex: lxc.net.0.veth.pair)
491 * @lxcpath : the lxcpath in which the container is running
492 *
493 * Returns the item on success, NULL on failure. The caller must free() the
494 * returned item.
495 */
496 char *lxc_cmd_get_config_item(const char *name, const char *item,
497 const char *lxcpath)
498 {
499 int ret, stopped;
500 struct lxc_cmd_rr cmd = {
501 .req = { .cmd = LXC_CMD_GET_CONFIG_ITEM,
502 .data = item,
503 .datalen = strlen(item) + 1,
504 },
505 };
506
507 ret = lxc_cmd(name, &cmd, &stopped, lxcpath, NULL);
508 if (ret < 0)
509 return NULL;
510
511 if (cmd.rsp.ret == 0)
512 return cmd.rsp.data;
513
514 return NULL;
515 }
516
517 static int lxc_cmd_get_config_item_callback(int fd, struct lxc_cmd_req *req,
518 struct lxc_handler *handler)
519 {
520 int cilen;
521 char *cidata;
522 struct lxc_config_t *item;
523 struct lxc_cmd_rsp rsp;
524
525 memset(&rsp, 0, sizeof(rsp));
526 item = lxc_get_config(req->data);
527 if (!item)
528 goto err1;
529
530 cilen = item->get(req->data, NULL, 0, handler->conf, NULL);
531 if (cilen <= 0)
532 goto err1;
533
534 cidata = alloca(cilen + 1);
535 if (item->get(req->data, cidata, cilen + 1, handler->conf, NULL) != cilen)
536 goto err1;
537
538 cidata[cilen] = '\0';
539 rsp.data = cidata;
540 rsp.datalen = cilen + 1;
541 rsp.ret = 0;
542 goto out;
543
544 err1:
545 rsp.ret = -1;
546 out:
547 return lxc_cmd_rsp_send(fd, &rsp);
548 }
549
550 /*
551 * lxc_cmd_get_state: Get current state of the container
552 *
553 * @name : name of container to connect to
554 * @lxcpath : the lxcpath in which the container is running
555 *
556 * Returns the state on success, < 0 on failure
557 */
558 int lxc_cmd_get_state(const char *name, const char *lxcpath)
559 {
560 int ret, stopped;
561 struct lxc_cmd_rr cmd = {
562 .req = { .cmd = LXC_CMD_GET_STATE }
563 };
564
565 ret = lxc_cmd(name, &cmd, &stopped, lxcpath, NULL);
566 if (ret < 0 && stopped)
567 return STOPPED;
568
569 if (ret < 0)
570 return -1;
571
572 if (!ret) {
573 WARN("Container \"%s\" has stopped before sending its state", name);
574 return -1;
575 }
576
577 DEBUG("Container \"%s\" is in \"%s\" state", name,
578 lxc_state2str(PTR_TO_INT(cmd.rsp.data)));
579
580 return PTR_TO_INT(cmd.rsp.data);
581 }
582
583 static int lxc_cmd_get_state_callback(int fd, struct lxc_cmd_req *req,
584 struct lxc_handler *handler)
585 {
586 struct lxc_cmd_rsp rsp = { .data = INT_TO_PTR(handler->state) };
587
588 return lxc_cmd_rsp_send(fd, &rsp);
589 }
590
591 /*
592 * lxc_cmd_stop: Stop the container previously started with lxc_start. All
593 * the processes running inside this container will be killed.
594 *
595 * @name : name of container to connect to
596 * @lxcpath : the lxcpath in which the container is running
597 *
598 * Returns 0 on success, < 0 on failure
599 */
600 int lxc_cmd_stop(const char *name, const char *lxcpath)
601 {
602 int ret, stopped;
603 struct lxc_cmd_rr cmd = {
604 .req = { .cmd = LXC_CMD_STOP },
605 };
606
607 ret = lxc_cmd(name, &cmd, &stopped, lxcpath, NULL);
608 if (ret < 0) {
609 if (stopped) {
610 INFO("Container \"%s\" is already stopped", name);
611 return 0;
612 }
613
614 return -1;
615 }
616
617 /* We do not expect any answer, because we wait for the connection to be
618 * closed.
619 */
620 if (ret > 0) {
621 ERROR("%s - Failed to stop container \"%s\"",
622 strerror(-cmd.rsp.ret), name);
623 return -1;
624 }
625
626 INFO("Container \"%s\" has stopped", name);
627 return 0;
628 }
629
630 static int lxc_cmd_stop_callback(int fd, struct lxc_cmd_req *req,
631 struct lxc_handler *handler)
632 {
633 struct lxc_cmd_rsp rsp;
634 int stopsignal = SIGKILL;
635
636 if (handler->conf->stopsignal)
637 stopsignal = handler->conf->stopsignal;
638 memset(&rsp, 0, sizeof(rsp));
639 rsp.ret = kill(handler->pid, stopsignal);
640 if (!rsp.ret) {
641 /* We can't just use lxc_unfreeze() since we are already in the
642 * context of handling the STOP cmd in lxc-start, and calling
643 * lxc_unfreeze() would do another cmd (GET_CGROUP) which would
644 * deadlock us.
645 */
646 if (cgroup_unfreeze(handler))
647 return 0;
648
649 ERROR("Failed to unfreeze container \"%s\"", handler->name);
650 rsp.ret = -1;
651 }
652
653 return lxc_cmd_rsp_send(fd, &rsp);
654 }
655
656 /*
657 * lxc_cmd_console_winch: To process as if a SIGWINCH were received
658 *
659 * @name : name of container to connect to
660 * @lxcpath : the lxcpath in which the container is running
661 *
662 * Returns 0 on success, < 0 on failure
663 */
664 int lxc_cmd_console_winch(const char *name, const char *lxcpath)
665 {
666 int ret, stopped;
667 struct lxc_cmd_rr cmd = {
668 .req = { .cmd = LXC_CMD_CONSOLE_WINCH },
669 };
670
671 ret = lxc_cmd(name, &cmd, &stopped, lxcpath, NULL);
672 if (ret < 0)
673 return ret;
674
675 return 0;
676 }
677
678 static int lxc_cmd_console_winch_callback(int fd, struct lxc_cmd_req *req,
679 struct lxc_handler *handler)
680 {
681 struct lxc_cmd_rsp rsp = { .data = 0 };
682
683 lxc_console_sigwinch(SIGWINCH);
684
685 return lxc_cmd_rsp_send(fd, &rsp);
686 }
687
688 /*
689 * lxc_cmd_console: Open an fd to a tty in the container
690 *
691 * @name : name of container to connect to
692 * @ttynum : in: the tty to open or -1 for next available
693 * : out: the tty allocated
694 * @fd : out: file descriptor for master side of pty
695 * @lxcpath : the lxcpath in which the container is running
696 *
697 * Returns fd holding tty allocated on success, < 0 on failure
698 */
699 int lxc_cmd_console(const char *name, int *ttynum, int *fd, const char *lxcpath)
700 {
701 int ret, stopped;
702 struct lxc_cmd_console_rsp_data *rspdata;
703 struct lxc_cmd_rr cmd = {
704 .req = { .cmd = LXC_CMD_CONSOLE, .data = INT_TO_PTR(*ttynum) },
705 };
706
707 ret = lxc_cmd(name, &cmd, &stopped, lxcpath, NULL);
708 if (ret < 0)
709 return ret;
710
711 if (cmd.rsp.ret < 0) {
712 ERROR("%s - Denied access to tty", strerror(-cmd.rsp.ret));
713 ret = -1;
714 goto out;
715 }
716
717 if (ret == 0) {
718 ERROR("tty number %d invalid, busy or all ttys busy", *ttynum);
719 ret = -1;
720 goto out;
721 }
722
723 rspdata = cmd.rsp.data;
724 if (rspdata->masterfd < 0) {
725 ERROR("Unable to allocate fd for tty %d", rspdata->ttynum);
726 goto out;
727 }
728
729 ret = cmd.rsp.ret; /* socket fd */
730 *fd = rspdata->masterfd;
731 *ttynum = rspdata->ttynum;
732 INFO("Alloced fd %d for tty %d via socket %d", *fd, rspdata->ttynum, ret);
733
734 out:
735 free(cmd.rsp.data);
736 return ret;
737 }
738
739 static int lxc_cmd_console_callback(int fd, struct lxc_cmd_req *req,
740 struct lxc_handler *handler)
741 {
742 int masterfd, ret;
743 struct lxc_cmd_rsp rsp;
744 int ttynum = PTR_TO_INT(req->data);
745
746 masterfd = lxc_console_allocate(handler->conf, fd, &ttynum);
747 if (masterfd < 0)
748 goto out_close;
749
750 memset(&rsp, 0, sizeof(rsp));
751 rsp.data = INT_TO_PTR(ttynum);
752 ret = lxc_abstract_unix_send_fds(fd, &masterfd, 1, &rsp, sizeof(rsp));
753 if (ret < 0) {
754 ERROR("Failed to send tty to client");
755 lxc_console_free(handler->conf, fd);
756 goto out_close;
757 }
758
759 return 0;
760
761 out_close:
762 /* Special indicator to lxc_cmd_handler() to close the fd and do
763 * related cleanup.
764 */
765 return 1;
766 }
767
768 /*
769 * lxc_cmd_get_name: Returns the name of the container
770 *
771 * @hashed_sock_name: hashed socket name
772 *
773 * Returns the name on success, NULL on failure.
774 */
775 char *lxc_cmd_get_name(const char *hashed_sock_name)
776 {
777 int ret, stopped;
778 struct lxc_cmd_rr cmd = {
779 .req = { .cmd = LXC_CMD_GET_NAME},
780 };
781
782 ret = lxc_cmd(NULL, &cmd, &stopped, NULL, hashed_sock_name);
783 if (ret < 0)
784 return NULL;
785
786 if (cmd.rsp.ret == 0)
787 return cmd.rsp.data;
788
789 return NULL;
790 }
791
792 static int lxc_cmd_get_name_callback(int fd, struct lxc_cmd_req *req,
793 struct lxc_handler *handler)
794 {
795 struct lxc_cmd_rsp rsp;
796
797 memset(&rsp, 0, sizeof(rsp));
798
799 rsp.data = (char *)handler->name;
800 rsp.datalen = strlen(handler->name) + 1;
801 rsp.ret = 0;
802
803 return lxc_cmd_rsp_send(fd, &rsp);
804 }
805
806 /*
807 * lxc_cmd_get_lxcpath: Returns the lxcpath of the container
808 *
809 * @hashed_sock_name: hashed socket name
810 *
811 * Returns the lxcpath on success, NULL on failure.
812 */
813 char *lxc_cmd_get_lxcpath(const char *hashed_sock_name)
814 {
815 int ret, stopped;
816 struct lxc_cmd_rr cmd = {
817 .req = { .cmd = LXC_CMD_GET_LXCPATH},
818 };
819
820 ret = lxc_cmd(NULL, &cmd, &stopped, NULL, hashed_sock_name);
821 if (ret < 0)
822 return NULL;
823
824 if (cmd.rsp.ret == 0)
825 return cmd.rsp.data;
826
827 return NULL;
828 }
829
830 static int lxc_cmd_get_lxcpath_callback(int fd, struct lxc_cmd_req *req,
831 struct lxc_handler *handler)
832 {
833 struct lxc_cmd_rsp rsp;
834
835 memset(&rsp, 0, sizeof(rsp));
836
837 rsp.ret = 0;
838 rsp.data = (char *)handler->lxcpath;
839 rsp.datalen = strlen(handler->lxcpath) + 1;
840
841 return lxc_cmd_rsp_send(fd, &rsp);
842 }
843
844 int lxc_cmd_add_state_client(const char *name, const char *lxcpath,
845 lxc_state_t states[MAX_STATE],
846 int *state_client_fd)
847 {
848 int state, stopped;
849 ssize_t ret;
850 struct lxc_cmd_rr cmd = {
851 .req = {
852 .cmd = LXC_CMD_ADD_STATE_CLIENT,
853 .data = states,
854 .datalen = (sizeof(lxc_state_t) * MAX_STATE)
855 },
856 };
857
858 ret = lxc_cmd(name, &cmd, &stopped, lxcpath, NULL);
859 if (states[STOPPED] != 0 && stopped != 0)
860 return STOPPED;
861
862 if (ret < 0) {
863 if (errno != ECONNREFUSED)
864 ERROR("%s - Failed to execute command", strerror(errno));
865 return -1;
866 }
867
868 /* We should now be guaranteed to get an answer from the state sending
869 * function.
870 */
871 if (cmd.rsp.ret < 0) {
872 ERROR("%s - Failed to receive socket fd", strerror(-cmd.rsp.ret));
873 return -1;
874 }
875
876 state = PTR_TO_INT(cmd.rsp.data);
877 if (state < MAX_STATE) {
878 TRACE("Container is already in requested state %s", lxc_state2str(state));
879 close(cmd.rsp.ret);
880 return state;
881 }
882
883 *state_client_fd = cmd.rsp.ret;
884 TRACE("Added state client %d to state client list", cmd.rsp.ret);
885 return MAX_STATE;
886 }
887
888 static int lxc_cmd_add_state_client_callback(int fd, struct lxc_cmd_req *req,
889 struct lxc_handler *handler)
890 {
891 int ret;
892 struct lxc_cmd_rsp rsp = {0};
893
894 if (req->datalen < 0)
895 goto reap_client_fd;
896
897 if (req->datalen > (sizeof(lxc_state_t) * MAX_STATE))
898 goto reap_client_fd;
899
900 if (!req->data)
901 goto reap_client_fd;
902
903 rsp.ret = lxc_add_state_client(fd, handler, (lxc_state_t *)req->data);
904 if (rsp.ret < 0)
905 goto reap_client_fd;
906
907 rsp.data = INT_TO_PTR(rsp.ret);
908
909 ret = lxc_cmd_rsp_send(fd, &rsp);
910 if (ret < 0)
911 goto reap_client_fd;
912
913 return 0;
914
915 reap_client_fd:
916 /* Special indicator to lxc_cmd_handler() to close the fd and do related
917 * cleanup.
918 */
919 return 1;
920 }
921
922 int lxc_cmd_console_log(const char *name, const char *lxcpath,
923 struct lxc_console_log *log)
924 {
925 int ret, stopped;
926 struct lxc_cmd_console_log data;
927 struct lxc_cmd_rr cmd;
928
929 data.clear = log->clear;
930 data.read = log->read;
931 data.read_max = *log->read_max;
932 data.write_logfile = log->write_logfile;
933
934 cmd.req.cmd = LXC_CMD_CONSOLE_LOG;
935 cmd.req.data = &data;
936 cmd.req.datalen = sizeof(struct lxc_cmd_console_log);
937
938 ret = lxc_cmd(name, &cmd, &stopped, lxcpath, NULL);
939 if (ret < 0)
940 return ret;
941
942 /* There is nothing to be read from the buffer. So clear any values we
943 * where passed to clearly indicate to the user that nothing went wrong.
944 */
945 if (cmd.rsp.ret == -ENODATA || cmd.rsp.ret == -EFAULT || cmd.rsp.ret == -ENOENT) {
946 *log->read_max = 0;
947 log->data = NULL;
948 }
949
950 /* This is a proper error so don't touch any values we were passed. */
951 if (cmd.rsp.ret < 0)
952 return cmd.rsp.ret;
953
954 *log->read_max = cmd.rsp.datalen;
955 log->data = cmd.rsp.data;
956
957 return 0;
958 }
959
960 static int lxc_cmd_console_log_callback(int fd, struct lxc_cmd_req *req,
961 struct lxc_handler *handler)
962 {
963 struct lxc_cmd_rsp rsp;
964 uint64_t buffer_size = handler->conf->console.buffer_size;
965 const struct lxc_cmd_console_log *log = req->data;
966 struct lxc_console *console = &handler->conf->console;
967 struct lxc_ringbuf *buf = &handler->conf->console.ringbuf;
968
969 rsp.ret = -EFAULT;
970 rsp.datalen = 0;
971 rsp.data = NULL;
972 if (buffer_size <= 0)
973 goto out;
974
975 if (log->read || log->write_logfile)
976 rsp.datalen = lxc_ringbuf_used(buf);
977
978 if (log->read)
979 rsp.data = lxc_ringbuf_get_read_addr(buf);
980
981 if (log->read_max > 0 && (log->read_max <= rsp.datalen))
982 rsp.datalen = log->read_max;
983
984 /* there's nothing to read */
985 rsp.ret = -ENODATA;
986 if (log->read && (buf->r_off == buf->w_off))
987 goto out;
988
989 if (log->write_logfile && rsp.datalen > 0) {
990 rsp.ret = -ENOENT;
991 if (!console->buffer_log_file)
992 goto out;
993
994 rsp.ret = lxc_console_write_ringbuffer(console);
995 if (rsp.ret < 0)
996 goto out;
997 }
998
999 rsp.ret = 0;
1000 if (log->clear) {
1001 int ret;
1002 size_t len;
1003 char *tmp;
1004
1005 /* clear the ringbuffer */
1006 lxc_ringbuf_clear(buf);
1007
1008 /* truncate the ringbuffer log file */
1009 if (console->buffer_log_file) {
1010 rsp.ret = -ENOENT;
1011 if (!file_exists(console->buffer_log_file))
1012 goto out;
1013
1014 /* be very certain things are kosher */
1015 rsp.ret = -EBADF;
1016 if (console->buffer_log_file_fd < 0)
1017 goto out;
1018
1019 rsp.ret = lxc_unpriv(ftruncate(console->buffer_log_file_fd, 0));
1020 if (rsp.ret < 0) {
1021 ERROR("%s - Failed to truncate console "
1022 "ringbuffer log file \"%s\"",
1023 strerror(errno), console->buffer_log_file);
1024 goto out;
1025 }
1026 }
1027
1028 /* rotate the console log file */
1029 if (!console->log_path || console->log_rotate == 0)
1030 goto out;
1031
1032 /* be very certain things are kosher */
1033 rsp.ret = -EBADF;
1034 if (console->log_fd < 0)
1035 goto out;
1036
1037 len = strlen(console->log_path) + sizeof(".1");
1038 tmp = alloca(len);
1039
1040 rsp.ret = -EFBIG;
1041 ret = snprintf(tmp, len, "%s.1", console->log_path);
1042 if (ret < 0 || (size_t)ret >= len)
1043 goto out;
1044
1045 close(console->log_fd);
1046 console->log_fd = -1;
1047 rsp.ret = lxc_unpriv(rename(console->log_path, tmp));
1048 if (rsp.ret < 0)
1049 goto out;
1050
1051 rsp.ret = lxc_console_create_log_file(console);
1052 } else if (rsp.datalen > 0) {
1053 lxc_ringbuf_move_read_addr(buf, rsp.datalen);
1054 }
1055
1056 out:
1057 return lxc_cmd_rsp_send(fd, &rsp);
1058 }
1059
1060 int lxc_cmd_serve_state_clients(const char *name, const char *lxcpath,
1061 lxc_state_t state)
1062 {
1063 int stopped;
1064 ssize_t ret;
1065 struct lxc_cmd_rr cmd = {
1066 .req = {
1067 .cmd = LXC_CMD_SERVE_STATE_CLIENTS,
1068 .data = INT_TO_PTR(state)
1069 },
1070 };
1071
1072 ret = lxc_cmd(name, &cmd, &stopped, lxcpath, NULL);
1073 if (ret < 0) {
1074 ERROR("%s - Failed to execute command", strerror(errno));
1075 return -1;
1076 }
1077
1078 return 0;
1079 }
1080
1081 static int lxc_cmd_serve_state_clients_callback(int fd, struct lxc_cmd_req *req,
1082 struct lxc_handler *handler)
1083 {
1084 int ret;
1085 lxc_state_t state = PTR_TO_INT(req->data);
1086 struct lxc_cmd_rsp rsp = {0};
1087
1088 ret = lxc_serve_state_clients(handler->name, handler, state);
1089 if (ret < 0)
1090 goto reap_client_fd;
1091
1092 ret = lxc_cmd_rsp_send(fd, &rsp);
1093 if (ret < 0)
1094 goto reap_client_fd;
1095
1096 return 0;
1097
1098 reap_client_fd:
1099 /* Special indicator to lxc_cmd_handler() to close the fd and do related
1100 * cleanup.
1101 */
1102 return 1;
1103 }
1104
1105 static int lxc_cmd_process(int fd, struct lxc_cmd_req *req,
1106 struct lxc_handler *handler)
1107 {
1108 typedef int (*callback)(int, struct lxc_cmd_req *, struct lxc_handler *);
1109
1110 callback cb[LXC_CMD_MAX] = {
1111 [LXC_CMD_CONSOLE] = lxc_cmd_console_callback,
1112 [LXC_CMD_CONSOLE_WINCH] = lxc_cmd_console_winch_callback,
1113 [LXC_CMD_STOP] = lxc_cmd_stop_callback,
1114 [LXC_CMD_GET_STATE] = lxc_cmd_get_state_callback,
1115 [LXC_CMD_GET_INIT_PID] = lxc_cmd_get_init_pid_callback,
1116 [LXC_CMD_GET_CLONE_FLAGS] = lxc_cmd_get_clone_flags_callback,
1117 [LXC_CMD_GET_CGROUP] = lxc_cmd_get_cgroup_callback,
1118 [LXC_CMD_GET_CONFIG_ITEM] = lxc_cmd_get_config_item_callback,
1119 [LXC_CMD_GET_NAME] = lxc_cmd_get_name_callback,
1120 [LXC_CMD_GET_LXCPATH] = lxc_cmd_get_lxcpath_callback,
1121 [LXC_CMD_ADD_STATE_CLIENT] = lxc_cmd_add_state_client_callback,
1122 [LXC_CMD_CONSOLE_LOG] = lxc_cmd_console_log_callback,
1123 [LXC_CMD_SERVE_STATE_CLIENTS] = lxc_cmd_serve_state_clients_callback,
1124 };
1125
1126 if (req->cmd >= LXC_CMD_MAX) {
1127 ERROR("Undefined command id %d", req->cmd);
1128 return -1;
1129 }
1130 return cb[req->cmd](fd, req, handler);
1131 }
1132
1133 static void lxc_cmd_fd_cleanup(int fd, struct lxc_handler *handler,
1134 struct lxc_epoll_descr *descr,
1135 const lxc_cmd_t cmd)
1136 {
1137 struct lxc_state_client *client;
1138 struct lxc_list *cur, *next;
1139
1140 lxc_console_free(handler->conf, fd);
1141 lxc_mainloop_del_handler(descr, fd);
1142 if (cmd != LXC_CMD_ADD_STATE_CLIENT) {
1143 close(fd);
1144 return;
1145 }
1146
1147 process_lock();
1148 lxc_list_for_each_safe(cur, &handler->conf->state_clients, next) {
1149 client = cur->elem;
1150 if (client->clientfd != fd)
1151 continue;
1152
1153 /* kick client from list */
1154 close(client->clientfd);
1155 lxc_list_del(cur);
1156 free(cur->elem);
1157 free(cur);
1158 /* No need to walk the whole list. If we found the state client
1159 * fd there can't be a second one.
1160 */
1161 break;
1162 }
1163 process_unlock();
1164 }
1165
1166 static int lxc_cmd_handler(int fd, uint32_t events, void *data,
1167 struct lxc_epoll_descr *descr)
1168 {
1169 int ret;
1170 struct lxc_cmd_req req;
1171 void *reqdata = NULL;
1172 struct lxc_handler *handler = data;
1173
1174 ret = lxc_abstract_unix_rcv_credential(fd, &req, sizeof(req));
1175 if (ret == -EACCES) {
1176 /* We don't care for the peer, just send and close. */
1177 struct lxc_cmd_rsp rsp = {.ret = ret};
1178
1179 lxc_cmd_rsp_send(fd, &rsp);
1180 goto out_close;
1181 }
1182
1183 if (ret < 0) {
1184 SYSERROR("Failed to receive data on command socket for command "
1185 "\"%s\"", lxc_cmd_str(req.cmd));
1186 goto out_close;
1187 }
1188
1189 if (ret == 0)
1190 goto out_close;
1191
1192 if (ret != sizeof(req)) {
1193 WARN("Failed to receive full command request. Ignoring request "
1194 "for \"%s\"", lxc_cmd_str(req.cmd));
1195 ret = -1;
1196 goto out_close;
1197 }
1198
1199 if ((req.datalen > LXC_CMD_DATA_MAX) &&
1200 (req.cmd != LXC_CMD_CONSOLE_LOG)) {
1201 ERROR("Received command data length %d is too large for "
1202 "command \"%s\"", req.datalen, lxc_cmd_str(req.cmd));
1203 errno = EFBIG;
1204 ret = -EFBIG;
1205 goto out_close;
1206 }
1207
1208 if (req.datalen > 0) {
1209 /* LXC_CMD_CONSOLE_LOG needs to be able to allocate data
1210 * that exceeds LXC_CMD_DATA_MAX: use malloc() for that.
1211 */
1212 if (req.cmd == LXC_CMD_CONSOLE_LOG)
1213 reqdata = malloc(req.datalen);
1214 else
1215 reqdata = alloca(req.datalen);
1216 if (!reqdata) {
1217 ERROR("Failed to allocate memory for \"%s\" command",
1218 lxc_cmd_str(req.cmd));
1219 errno = ENOMEM;
1220 ret = -ENOMEM;
1221 goto out_close;
1222 }
1223
1224 ret = recv(fd, reqdata, req.datalen, 0);
1225 if (ret != req.datalen) {
1226 WARN("Failed to receive full command request. Ignoring "
1227 "request for \"%s\"", lxc_cmd_str(req.cmd));
1228 ret = -1;
1229 goto out_close;
1230 }
1231
1232 req.data = reqdata;
1233 }
1234
1235 ret = lxc_cmd_process(fd, &req, handler);
1236 if (ret) {
1237 /* This is not an error, but only a request to close fd. */
1238 ret = 0;
1239 goto out_close;
1240 }
1241
1242 out:
1243 if (req.cmd == LXC_CMD_CONSOLE_LOG && reqdata)
1244 free(reqdata);
1245
1246 return ret;
1247
1248 out_close:
1249 lxc_cmd_fd_cleanup(fd, handler, descr, req.cmd);
1250 goto out;
1251 }
1252
1253 static int lxc_cmd_accept(int fd, uint32_t events, void *data,
1254 struct lxc_epoll_descr *descr)
1255 {
1256 int connection;
1257 int opt = 1, ret = -1;
1258
1259 connection = accept(fd, NULL, 0);
1260 if (connection < 0) {
1261 SYSERROR("Failed to accept connection to run command.");
1262 return -1;
1263 }
1264
1265 ret = fcntl(connection, F_SETFD, FD_CLOEXEC);
1266 if (ret < 0) {
1267 SYSERROR("Failed to set close-on-exec on incoming command connection");
1268 goto out_close;
1269 }
1270
1271 ret = setsockopt(connection, SOL_SOCKET, SO_PASSCRED, &opt, sizeof(opt));
1272 if (ret < 0) {
1273 SYSERROR("Failed to enable necessary credentials on command socket");
1274 goto out_close;
1275 }
1276
1277 ret = lxc_mainloop_add_handler(descr, connection, lxc_cmd_handler, data);
1278 if (ret) {
1279 ERROR("Failed to add command handler");
1280 goto out_close;
1281 }
1282
1283 out:
1284 return ret;
1285
1286 out_close:
1287 close(connection);
1288 goto out;
1289 }
1290
1291 int lxc_cmd_init(const char *name, const char *lxcpath, const char *suffix)
1292 {
1293 int fd, len, ret;
1294 char path[sizeof(((struct sockaddr_un *)0)->sun_path)] = {0};
1295 char *offset = &path[1];
1296
1297 /* -2 here because this is an abstract unix socket so it needs a
1298 * leading \0, and we null terminate, so it needs a trailing \0.
1299 * Although null termination isn't required by the API, we do it anyway
1300 * because we print the sockname out sometimes.
1301 */
1302 len = sizeof(path) - 2;
1303 ret = lxc_make_abstract_socket_name(offset, len, name, lxcpath, NULL, suffix);
1304 if (ret < 0)
1305 return -1;
1306 TRACE("Creating abstract unix socket \"%s\"", offset);
1307
1308 fd = lxc_abstract_unix_open(path, SOCK_STREAM, 0);
1309 if (fd < 0) {
1310 ERROR("%s - Failed to create command socket %s",
1311 strerror(errno), offset);
1312 if (errno == EADDRINUSE)
1313 ERROR("Container \"%s\" appears to be already running", name);
1314 return -1;
1315 }
1316
1317 ret = fcntl(fd, F_SETFD, FD_CLOEXEC);
1318 if (ret < 0) {
1319 SYSERROR("Failed to set FD_CLOEXEC on command socket file descriptor");
1320 close(fd);
1321 return -1;
1322 }
1323
1324 return fd;
1325 }
1326
1327 int lxc_cmd_mainloop_add(const char *name, struct lxc_epoll_descr *descr,
1328 struct lxc_handler *handler)
1329 {
1330 int ret;
1331 int fd = handler->conf->maincmd_fd;
1332
1333 ret = lxc_mainloop_add_handler(descr, fd, lxc_cmd_accept, handler);
1334 if (ret < 0) {
1335 ERROR("Failed to add handler for command socket");
1336 close(fd);
1337 }
1338
1339 return ret;
1340 }