2 * lxc: linux Container library
4 * (C) Copyright IBM Corp. 2007, 2009
7 * Daniel Lezcano <daniel.lezcano at free.fr>
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.
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.
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
35 #include <sys/param.h>
36 #include <sys/socket.h>
42 #include "commands_utils.h"
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).
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
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.
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.
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
79 lxc_log_define(commands
, lxc
);
81 static const char *lxc_cmd_str(lxc_cmd_t cmd
)
83 static const char *const cmdname
[LXC_CMD_MAX
] = {
84 [LXC_CMD_CONSOLE
] = "console",
85 [LXC_CMD_TERMINAL_WINCH
] = "terminal_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",
99 if (cmd
>= LXC_CMD_MAX
)
100 return "Unknown cmd";
106 * lxc_cmd_rsp_recv: Receive a response to a command
108 * @sock : the socket connected to the container
109 * @cmd : command to put response in
111 * Returns the size of the response message or < 0 on failure
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.
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
122 static int lxc_cmd_rsp_recv(int sock
, struct lxc_cmd_rr
*cmd
)
125 struct lxc_cmd_rsp
*rsp
= &cmd
->rsp
;
127 ret
= lxc_abstract_unix_recv_fds(sock
, &rspfd
, 1, rsp
, sizeof(*rsp
));
129 SYSWARN("Failed to receive response for command \"%s\"",
130 lxc_cmd_str(cmd
->req
.cmd
));
132 if (errno
== ECONNRESET
)
137 TRACE("Command \"%s\" received response", lxc_cmd_str(cmd
->req
.cmd
));
139 if (cmd
->req
.cmd
== LXC_CMD_CONSOLE
) {
140 struct lxc_cmd_console_rsp_data
*rspdata
;
142 /* recv() returns 0 bytes when a tty cannot be allocated,
143 * rsp->ret is < 0 when the peer permission check failed
145 if (ret
== 0 || rsp
->ret
< 0)
148 rspdata
= malloc(sizeof(*rspdata
));
150 ERROR("Failed to allocate response buffer for command \"%s\"",
151 lxc_cmd_str(cmd
->req
.cmd
));
154 rspdata
->masterfd
= rspfd
;
155 rspdata
->ttynum
= PTR_TO_INT(rsp
->data
);
159 if (rsp
->datalen
== 0) {
160 DEBUG("Response data length for command \"%s\" is 0",
161 lxc_cmd_str(cmd
->req
.cmd
));
165 if ((rsp
->datalen
> LXC_CMD_DATA_MAX
) &&
166 (cmd
->req
.cmd
!= LXC_CMD_CONSOLE_LOG
)) {
168 SYSERROR("Response data for command \"%s\" is too long: %d bytes > %d",
169 lxc_cmd_str(cmd
->req
.cmd
), rsp
->datalen
, LXC_CMD_DATA_MAX
);
173 if (cmd
->req
.cmd
== LXC_CMD_CONSOLE_LOG
) {
174 rsp
->data
= malloc(rsp
->datalen
+ 1);
175 ((char *)rsp
->data
)[rsp
->datalen
] = '\0';
177 rsp
->data
= malloc(rsp
->datalen
);
181 SYSERROR("Failed to allocate response buffer for command \"%s\"",
182 lxc_cmd_str(cmd
->req
.cmd
));
186 ret
= recv(sock
, rsp
->data
, rsp
->datalen
, 0);
187 if (ret
!= rsp
->datalen
) {
188 SYSERROR("Failed to receive response data for command \"%s\"",
189 lxc_cmd_str(cmd
->req
.cmd
));
198 * lxc_cmd_rsp_send: Send a command response
200 * @fd : file descriptor of socket to send response on
201 * @rsp : response to send
203 * Returns 0 on success, < 0 on failure
205 static int lxc_cmd_rsp_send(int fd
, struct lxc_cmd_rsp
*rsp
)
209 ret
= send(fd
, rsp
, sizeof(*rsp
), MSG_NOSIGNAL
);
210 if (ret
< 0 || (size_t)ret
!= sizeof(*rsp
)) {
211 SYSERROR("Failed to send command response %zd", ret
);
215 if (!rsp
->data
|| rsp
->datalen
<= 0)
218 ret
= send(fd
, rsp
->data
, rsp
->datalen
, MSG_NOSIGNAL
);
219 if (ret
< 0 || ret
!= (ssize_t
)rsp
->datalen
) {
220 SYSWARN("Failed to send command response data %zd", ret
);
227 static int lxc_cmd_send(const char *name
, struct lxc_cmd_rr
*cmd
,
228 const char *lxcpath
, const char *hashed_sock_name
)
233 client_fd
= lxc_cmd_connect(name
, lxcpath
, hashed_sock_name
, "command");
235 if (client_fd
== -ECONNREFUSED
)
236 return -ECONNREFUSED
;
241 ret
= lxc_abstract_unix_send_credential(client_fd
, &cmd
->req
,
243 if (ret
< 0 || (size_t)ret
!= sizeof(cmd
->req
)) {
255 if (cmd
->req
.datalen
<= 0)
258 ret
= send(client_fd
, cmd
->req
.data
, cmd
->req
.datalen
, MSG_NOSIGNAL
);
259 if (ret
< 0 || ret
!= (ssize_t
)cmd
->req
.datalen
) {
275 * lxc_cmd: Connect to the specified running container, send it a command
276 * request and collect the response
278 * @name : name of container to connect to
279 * @cmd : command with initialized request to send
280 * @stopped : output indicator if the container was not running
281 * @lxcpath : the lxcpath in which the container is running
283 * Returns the size of the response message on success, < 0 on failure
285 * Note that there is a special case for LXC_CMD_CONSOLE. For this command
286 * the fd cannot be closed because it is used as a placeholder to indicate
287 * that a particular tty slot is in use. The fd is also used as a signal to
288 * the container that when the caller dies or closes the fd, the container
289 * will notice the fd on its side of the socket in its mainloop select and
290 * then free the slot with lxc_cmd_fd_cleanup(). The socket fd will be
291 * returned in the cmd response structure.
293 static int lxc_cmd(const char *name
, struct lxc_cmd_rr
*cmd
, int *stopped
,
294 const char *lxcpath
, const char *hashed_sock_name
)
298 bool stay_connected
= false;
300 if (cmd
->req
.cmd
== LXC_CMD_CONSOLE
||
301 cmd
->req
.cmd
== LXC_CMD_ADD_STATE_CLIENT
)
302 stay_connected
= true;
306 client_fd
= lxc_cmd_send(name
, cmd
, lxcpath
, hashed_sock_name
);
308 SYSTRACE("Command \"%s\" failed to connect command socket",
309 lxc_cmd_str(cmd
->req
.cmd
));
311 if (client_fd
== -ECONNREFUSED
)
314 if (client_fd
== -EPIPE
) {
322 ret
= lxc_cmd_rsp_recv(client_fd
, cmd
);
323 if (ret
== -ECONNRESET
)
326 if (!stay_connected
|| ret
<= 0)
330 if (stay_connected
&& ret
> 0)
331 cmd
->rsp
.ret
= client_fd
;
336 int lxc_try_cmd(const char *name
, const char *lxcpath
)
339 struct lxc_cmd_rr cmd
= {
340 .req
= { .cmd
= LXC_CMD_GET_INIT_PID
},
343 ret
= lxc_cmd(name
, &cmd
, &stopped
, lxcpath
, NULL
);
346 if (ret
> 0 && cmd
.rsp
.ret
< 0) {
353 /* At this point we weren't denied access, and the container *was*
354 * started. There was some inexplicable error in the protocol. I'm not
355 * clear on whether we should return -1 here, but we didn't receive a
356 * -EACCES, so technically it's not that we're not allowed to control
357 * the container - it's just not behaving.
362 /* Implentations of the commands and their callbacks */
365 * lxc_cmd_get_init_pid: Get pid of the container's init process
367 * @name : name of container to connect to
368 * @lxcpath : the lxcpath in which the container is running
370 * Returns the pid on success, < 0 on failure
372 pid_t
lxc_cmd_get_init_pid(const char *name
, const char *lxcpath
)
375 struct lxc_cmd_rr cmd
= {
376 .req
= { .cmd
= LXC_CMD_GET_INIT_PID
},
379 ret
= lxc_cmd(name
, &cmd
, &stopped
, lxcpath
, NULL
);
383 return PTR_TO_INT(cmd
.rsp
.data
);
386 static int lxc_cmd_get_init_pid_callback(int fd
, struct lxc_cmd_req
*req
,
387 struct lxc_handler
*handler
)
389 struct lxc_cmd_rsp rsp
= { .data
= INT_TO_PTR(handler
->pid
) };
391 return lxc_cmd_rsp_send(fd
, &rsp
);
395 * lxc_cmd_get_clone_flags: Get clone flags container was spawned with
397 * @name : name of container to connect to
398 * @lxcpath : the lxcpath in which the container is running
400 * Returns the clone flags on success, < 0 on failure
402 int lxc_cmd_get_clone_flags(const char *name
, const char *lxcpath
)
405 struct lxc_cmd_rr cmd
= {
406 .req
= { .cmd
= LXC_CMD_GET_CLONE_FLAGS
},
409 ret
= lxc_cmd(name
, &cmd
, &stopped
, lxcpath
, NULL
);
413 return PTR_TO_INT(cmd
.rsp
.data
);
416 static int lxc_cmd_get_clone_flags_callback(int fd
, struct lxc_cmd_req
*req
,
417 struct lxc_handler
*handler
)
419 struct lxc_cmd_rsp rsp
= { .data
= INT_TO_PTR(handler
->ns_clone_flags
) };
421 return lxc_cmd_rsp_send(fd
, &rsp
);
425 * lxc_cmd_get_cgroup_path: Calculate a container's cgroup path for a
426 * particular subsystem. This is the cgroup path relative to the root
427 * of the cgroup filesystem.
429 * @name : name of container to connect to
430 * @lxcpath : the lxcpath in which the container is running
431 * @subsystem : the subsystem being asked about
433 * Returns the path on success, NULL on failure. The caller must free() the
436 char *lxc_cmd_get_cgroup_path(const char *name
, const char *lxcpath
,
437 const char *subsystem
)
440 struct lxc_cmd_rr cmd
= {
442 .cmd
= LXC_CMD_GET_CGROUP
,
448 cmd
.req
.data
= subsystem
;
451 cmd
.req
.datalen
= strlen(subsystem
) + 1;
453 ret
= lxc_cmd(name
, &cmd
, &stopped
, lxcpath
, NULL
);
460 if (cmd
.rsp
.ret
< 0 || cmd
.rsp
.datalen
< 0)
466 static int lxc_cmd_get_cgroup_callback(int fd
, struct lxc_cmd_req
*req
,
467 struct lxc_handler
*handler
)
470 struct lxc_cmd_rsp rsp
;
471 struct cgroup_ops
*cgroup_ops
= handler
->cgroup_ops
;
473 if (req
->datalen
> 0)
474 path
= cgroup_ops
->get_cgroup(cgroup_ops
, req
->data
);
476 path
= cgroup_ops
->get_cgroup(cgroup_ops
, NULL
);
481 rsp
.datalen
= strlen(path
) + 1;
482 rsp
.data
= (char *)path
;
484 return lxc_cmd_rsp_send(fd
, &rsp
);
488 * lxc_cmd_get_config_item: Get config item the running container
490 * @name : name of container to connect to
491 * @item : the configuration item to retrieve (ex: lxc.net.0.veth.pair)
492 * @lxcpath : the lxcpath in which the container is running
494 * Returns the item on success, NULL on failure. The caller must free() the
497 char *lxc_cmd_get_config_item(const char *name
, const char *item
,
501 struct lxc_cmd_rr cmd
= {
502 .req
= { .cmd
= LXC_CMD_GET_CONFIG_ITEM
,
504 .datalen
= strlen(item
) + 1,
508 ret
= lxc_cmd(name
, &cmd
, &stopped
, lxcpath
, NULL
);
512 if (cmd
.rsp
.ret
== 0)
518 static int lxc_cmd_get_config_item_callback(int fd
, struct lxc_cmd_req
*req
,
519 struct lxc_handler
*handler
)
523 struct lxc_config_t
*item
;
524 struct lxc_cmd_rsp rsp
;
526 memset(&rsp
, 0, sizeof(rsp
));
527 item
= lxc_get_config(req
->data
);
531 cilen
= item
->get(req
->data
, NULL
, 0, handler
->conf
, NULL
);
535 cidata
= alloca(cilen
+ 1);
536 if (item
->get(req
->data
, cidata
, cilen
+ 1, handler
->conf
, NULL
) != cilen
)
539 cidata
[cilen
] = '\0';
541 rsp
.datalen
= cilen
+ 1;
548 return lxc_cmd_rsp_send(fd
, &rsp
);
552 * lxc_cmd_get_state: Get current state of the container
554 * @name : name of container to connect to
555 * @lxcpath : the lxcpath in which the container is running
557 * Returns the state on success, < 0 on failure
559 int lxc_cmd_get_state(const char *name
, const char *lxcpath
)
562 struct lxc_cmd_rr cmd
= {
563 .req
= { .cmd
= LXC_CMD_GET_STATE
}
566 ret
= lxc_cmd(name
, &cmd
, &stopped
, lxcpath
, NULL
);
567 if (ret
< 0 && stopped
)
574 WARN("Container \"%s\" has stopped before sending its state", name
);
578 DEBUG("Container \"%s\" is in \"%s\" state", name
,
579 lxc_state2str(PTR_TO_INT(cmd
.rsp
.data
)));
581 return PTR_TO_INT(cmd
.rsp
.data
);
584 static int lxc_cmd_get_state_callback(int fd
, struct lxc_cmd_req
*req
,
585 struct lxc_handler
*handler
)
587 struct lxc_cmd_rsp rsp
= { .data
= INT_TO_PTR(handler
->state
) };
589 return lxc_cmd_rsp_send(fd
, &rsp
);
593 * lxc_cmd_stop: Stop the container previously started with lxc_start. All
594 * the processes running inside this container will be killed.
596 * @name : name of container to connect to
597 * @lxcpath : the lxcpath in which the container is running
599 * Returns 0 on success, < 0 on failure
601 int lxc_cmd_stop(const char *name
, const char *lxcpath
)
604 struct lxc_cmd_rr cmd
= {
605 .req
= { .cmd
= LXC_CMD_STOP
},
608 ret
= lxc_cmd(name
, &cmd
, &stopped
, lxcpath
, NULL
);
611 INFO("Container \"%s\" is already stopped", name
);
618 /* We do not expect any answer, because we wait for the connection to be
622 errno
= -cmd
.rsp
.ret
;
623 SYSERROR("Failed to stop container \"%s\"", name
);
627 INFO("Container \"%s\" has stopped", name
);
631 static int lxc_cmd_stop_callback(int fd
, struct lxc_cmd_req
*req
,
632 struct lxc_handler
*handler
)
634 struct lxc_cmd_rsp rsp
;
635 int stopsignal
= SIGKILL
;
636 struct cgroup_ops
*cgroup_ops
= handler
->cgroup_ops
;
638 if (handler
->conf
->stopsignal
)
639 stopsignal
= handler
->conf
->stopsignal
;
640 memset(&rsp
, 0, sizeof(rsp
));
641 rsp
.ret
= kill(handler
->pid
, stopsignal
);
643 /* We can't just use lxc_unfreeze() since we are already in the
644 * context of handling the STOP cmd in lxc-start, and calling
645 * lxc_unfreeze() would do another cmd (GET_CGROUP) which would
648 if (cgroup_ops
->unfreeze(cgroup_ops
))
651 ERROR("Failed to unfreeze container \"%s\"", handler
->name
);
655 return lxc_cmd_rsp_send(fd
, &rsp
);
659 * lxc_cmd_terminal_winch: To process as if a SIGWINCH were received
661 * @name : name of container to connect to
662 * @lxcpath : the lxcpath in which the container is running
664 * Returns 0 on success, < 0 on failure
666 int lxc_cmd_terminal_winch(const char *name
, const char *lxcpath
)
669 struct lxc_cmd_rr cmd
= {
670 .req
= { .cmd
= LXC_CMD_TERMINAL_WINCH
},
673 ret
= lxc_cmd(name
, &cmd
, &stopped
, lxcpath
, NULL
);
680 static int lxc_cmd_terminal_winch_callback(int fd
, struct lxc_cmd_req
*req
,
681 struct lxc_handler
*handler
)
683 struct lxc_cmd_rsp rsp
= { .data
= 0 };
685 lxc_terminal_sigwinch(SIGWINCH
);
687 return lxc_cmd_rsp_send(fd
, &rsp
);
691 * lxc_cmd_console: Open an fd to a tty in the container
693 * @name : name of container to connect to
694 * @ttynum : in: the tty to open or -1 for next available
695 * : out: the tty allocated
696 * @fd : out: file descriptor for master side of pty
697 * @lxcpath : the lxcpath in which the container is running
699 * Returns fd holding tty allocated on success, < 0 on failure
701 int lxc_cmd_console(const char *name
, int *ttynum
, int *fd
, const char *lxcpath
)
704 struct lxc_cmd_console_rsp_data
*rspdata
;
705 struct lxc_cmd_rr cmd
= {
706 .req
= { .cmd
= LXC_CMD_CONSOLE
, .data
= INT_TO_PTR(*ttynum
) },
709 ret
= lxc_cmd(name
, &cmd
, &stopped
, lxcpath
, NULL
);
713 if (cmd
.rsp
.ret
< 0) {
714 errno
= -cmd
.rsp
.ret
;
715 SYSERROR("Denied access to tty");
721 ERROR("tty number %d invalid, busy or all ttys busy", *ttynum
);
726 rspdata
= cmd
.rsp
.data
;
727 if (rspdata
->masterfd
< 0) {
728 ERROR("Unable to allocate fd for tty %d", rspdata
->ttynum
);
732 ret
= cmd
.rsp
.ret
; /* socket fd */
733 *fd
= rspdata
->masterfd
;
734 *ttynum
= rspdata
->ttynum
;
735 INFO("Alloced fd %d for tty %d via socket %d", *fd
, rspdata
->ttynum
, ret
);
742 static int lxc_cmd_console_callback(int fd
, struct lxc_cmd_req
*req
,
743 struct lxc_handler
*handler
)
746 struct lxc_cmd_rsp rsp
;
747 int ttynum
= PTR_TO_INT(req
->data
);
749 masterfd
= lxc_terminal_allocate(handler
->conf
, fd
, &ttynum
);
753 memset(&rsp
, 0, sizeof(rsp
));
754 rsp
.data
= INT_TO_PTR(ttynum
);
755 ret
= lxc_abstract_unix_send_fds(fd
, &masterfd
, 1, &rsp
, sizeof(rsp
));
757 ERROR("Failed to send tty to client");
758 lxc_terminal_free(handler
->conf
, fd
);
765 /* Special indicator to lxc_cmd_handler() to close the fd and do
772 * lxc_cmd_get_name: Returns the name of the container
774 * @hashed_sock_name: hashed socket name
776 * Returns the name on success, NULL on failure.
778 char *lxc_cmd_get_name(const char *hashed_sock_name
)
781 struct lxc_cmd_rr cmd
= {
782 .req
= { .cmd
= LXC_CMD_GET_NAME
},
785 ret
= lxc_cmd(NULL
, &cmd
, &stopped
, NULL
, hashed_sock_name
);
789 if (cmd
.rsp
.ret
== 0)
795 static int lxc_cmd_get_name_callback(int fd
, struct lxc_cmd_req
*req
,
796 struct lxc_handler
*handler
)
798 struct lxc_cmd_rsp rsp
;
800 memset(&rsp
, 0, sizeof(rsp
));
802 rsp
.data
= (char *)handler
->name
;
803 rsp
.datalen
= strlen(handler
->name
) + 1;
806 return lxc_cmd_rsp_send(fd
, &rsp
);
810 * lxc_cmd_get_lxcpath: Returns the lxcpath of the container
812 * @hashed_sock_name: hashed socket name
814 * Returns the lxcpath on success, NULL on failure.
816 char *lxc_cmd_get_lxcpath(const char *hashed_sock_name
)
819 struct lxc_cmd_rr cmd
= {
820 .req
= { .cmd
= LXC_CMD_GET_LXCPATH
},
823 ret
= lxc_cmd(NULL
, &cmd
, &stopped
, NULL
, hashed_sock_name
);
827 if (cmd
.rsp
.ret
== 0)
833 static int lxc_cmd_get_lxcpath_callback(int fd
, struct lxc_cmd_req
*req
,
834 struct lxc_handler
*handler
)
836 struct lxc_cmd_rsp rsp
;
838 memset(&rsp
, 0, sizeof(rsp
));
841 rsp
.data
= (char *)handler
->lxcpath
;
842 rsp
.datalen
= strlen(handler
->lxcpath
) + 1;
844 return lxc_cmd_rsp_send(fd
, &rsp
);
847 int lxc_cmd_add_state_client(const char *name
, const char *lxcpath
,
848 lxc_state_t states
[MAX_STATE
],
849 int *state_client_fd
)
853 struct lxc_cmd_rr cmd
= {
855 .cmd
= LXC_CMD_ADD_STATE_CLIENT
,
857 .datalen
= (sizeof(lxc_state_t
) * MAX_STATE
)
861 ret
= lxc_cmd(name
, &cmd
, &stopped
, lxcpath
, NULL
);
862 if (states
[STOPPED
] != 0 && stopped
!= 0)
866 if (errno
!= ECONNREFUSED
)
867 SYSERROR("Failed to execute command");
872 /* We should now be guaranteed to get an answer from the state sending
875 if (cmd
.rsp
.ret
< 0) {
876 errno
= -cmd
.rsp
.ret
;
877 SYSERROR("Failed to receive socket fd");
881 state
= PTR_TO_INT(cmd
.rsp
.data
);
882 if (state
< MAX_STATE
) {
883 TRACE("Container is already in requested state %s", lxc_state2str(state
));
888 *state_client_fd
= cmd
.rsp
.ret
;
889 TRACE("Added state client %d to state client list", cmd
.rsp
.ret
);
893 static int lxc_cmd_add_state_client_callback(int fd
, struct lxc_cmd_req
*req
,
894 struct lxc_handler
*handler
)
897 struct lxc_cmd_rsp rsp
= {0};
899 if (req
->datalen
< 0)
902 if (req
->datalen
> (sizeof(lxc_state_t
) * MAX_STATE
))
908 rsp
.ret
= lxc_add_state_client(fd
, handler
, (lxc_state_t
*)req
->data
);
912 rsp
.data
= INT_TO_PTR(rsp
.ret
);
914 ret
= lxc_cmd_rsp_send(fd
, &rsp
);
921 /* Special indicator to lxc_cmd_handler() to close the fd and do related
927 int lxc_cmd_console_log(const char *name
, const char *lxcpath
,
928 struct lxc_console_log
*log
)
931 struct lxc_cmd_console_log data
;
932 struct lxc_cmd_rr cmd
;
934 data
.clear
= log
->clear
;
935 data
.read
= log
->read
;
936 data
.read_max
= *log
->read_max
;
938 cmd
.req
.cmd
= LXC_CMD_CONSOLE_LOG
;
939 cmd
.req
.data
= &data
;
940 cmd
.req
.datalen
= sizeof(struct lxc_cmd_console_log
);
942 ret
= lxc_cmd(name
, &cmd
, &stopped
, lxcpath
, NULL
);
946 /* There is nothing to be read from the buffer. So clear any values we
947 * where passed to clearly indicate to the user that nothing went wrong.
949 if (cmd
.rsp
.ret
== -ENODATA
|| cmd
.rsp
.ret
== -EFAULT
|| cmd
.rsp
.ret
== -ENOENT
) {
954 /* This is a proper error so don't touch any values we were passed. */
958 *log
->read_max
= cmd
.rsp
.datalen
;
959 log
->data
= cmd
.rsp
.data
;
964 static int lxc_cmd_console_log_callback(int fd
, struct lxc_cmd_req
*req
,
965 struct lxc_handler
*handler
)
967 struct lxc_cmd_rsp rsp
;
968 uint64_t buffer_size
= handler
->conf
->console
.buffer_size
;
969 const struct lxc_cmd_console_log
*log
= req
->data
;
970 struct lxc_ringbuf
*buf
= &handler
->conf
->console
.ringbuf
;
975 if (buffer_size
<= 0)
978 if (log
->read
|| log
->write_logfile
)
979 rsp
.datalen
= lxc_ringbuf_used(buf
);
982 rsp
.data
= lxc_ringbuf_get_read_addr(buf
);
984 if (log
->read_max
> 0 && (log
->read_max
<= rsp
.datalen
))
985 rsp
.datalen
= log
->read_max
;
987 /* there's nothing to read */
989 if (log
->read
&& (buf
->r_off
== buf
->w_off
))
994 lxc_ringbuf_clear(buf
); /* clear the ringbuffer */
995 else if (rsp
.datalen
> 0)
996 lxc_ringbuf_move_read_addr(buf
, rsp
.datalen
);
999 return lxc_cmd_rsp_send(fd
, &rsp
);
1002 int lxc_cmd_serve_state_clients(const char *name
, const char *lxcpath
,
1007 struct lxc_cmd_rr cmd
= {
1009 .cmd
= LXC_CMD_SERVE_STATE_CLIENTS
,
1010 .data
= INT_TO_PTR(state
)
1014 ret
= lxc_cmd(name
, &cmd
, &stopped
, lxcpath
, NULL
);
1016 SYSERROR("Failed to execute command");
1023 static int lxc_cmd_serve_state_clients_callback(int fd
, struct lxc_cmd_req
*req
,
1024 struct lxc_handler
*handler
)
1027 lxc_state_t state
= PTR_TO_INT(req
->data
);
1028 struct lxc_cmd_rsp rsp
= {0};
1030 ret
= lxc_serve_state_clients(handler
->name
, handler
, state
);
1032 goto reap_client_fd
;
1034 ret
= lxc_cmd_rsp_send(fd
, &rsp
);
1036 goto reap_client_fd
;
1041 /* Special indicator to lxc_cmd_handler() to close the fd and do related
1047 static int lxc_cmd_process(int fd
, struct lxc_cmd_req
*req
,
1048 struct lxc_handler
*handler
)
1050 typedef int (*callback
)(int, struct lxc_cmd_req
*, struct lxc_handler
*);
1052 callback cb
[LXC_CMD_MAX
] = {
1053 [LXC_CMD_CONSOLE
] = lxc_cmd_console_callback
,
1054 [LXC_CMD_TERMINAL_WINCH
] = lxc_cmd_terminal_winch_callback
,
1055 [LXC_CMD_STOP
] = lxc_cmd_stop_callback
,
1056 [LXC_CMD_GET_STATE
] = lxc_cmd_get_state_callback
,
1057 [LXC_CMD_GET_INIT_PID
] = lxc_cmd_get_init_pid_callback
,
1058 [LXC_CMD_GET_CLONE_FLAGS
] = lxc_cmd_get_clone_flags_callback
,
1059 [LXC_CMD_GET_CGROUP
] = lxc_cmd_get_cgroup_callback
,
1060 [LXC_CMD_GET_CONFIG_ITEM
] = lxc_cmd_get_config_item_callback
,
1061 [LXC_CMD_GET_NAME
] = lxc_cmd_get_name_callback
,
1062 [LXC_CMD_GET_LXCPATH
] = lxc_cmd_get_lxcpath_callback
,
1063 [LXC_CMD_ADD_STATE_CLIENT
] = lxc_cmd_add_state_client_callback
,
1064 [LXC_CMD_CONSOLE_LOG
] = lxc_cmd_console_log_callback
,
1065 [LXC_CMD_SERVE_STATE_CLIENTS
] = lxc_cmd_serve_state_clients_callback
,
1068 if (req
->cmd
>= LXC_CMD_MAX
) {
1069 ERROR("Undefined command id %d", req
->cmd
);
1072 return cb
[req
->cmd
](fd
, req
, handler
);
1075 static void lxc_cmd_fd_cleanup(int fd
, struct lxc_handler
*handler
,
1076 struct lxc_epoll_descr
*descr
,
1077 const lxc_cmd_t cmd
)
1079 struct lxc_state_client
*client
;
1080 struct lxc_list
*cur
, *next
;
1082 lxc_terminal_free(handler
->conf
, fd
);
1083 lxc_mainloop_del_handler(descr
, fd
);
1084 if (cmd
!= LXC_CMD_ADD_STATE_CLIENT
) {
1089 lxc_list_for_each_safe(cur
, &handler
->conf
->state_clients
, next
) {
1091 if (client
->clientfd
!= fd
)
1094 /* kick client from list */
1096 close(client
->clientfd
);
1099 /* No need to walk the whole list. If we found the state client
1100 * fd there can't be a second one.
1106 static int lxc_cmd_handler(int fd
, uint32_t events
, void *data
,
1107 struct lxc_epoll_descr
*descr
)
1110 struct lxc_cmd_req req
;
1111 void *reqdata
= NULL
;
1112 struct lxc_handler
*handler
= data
;
1114 ret
= lxc_abstract_unix_rcv_credential(fd
, &req
, sizeof(req
));
1115 if (ret
== -EACCES
) {
1116 /* We don't care for the peer, just send and close. */
1117 struct lxc_cmd_rsp rsp
= {.ret
= ret
};
1119 lxc_cmd_rsp_send(fd
, &rsp
);
1124 SYSERROR("Failed to receive data on command socket for command "
1125 "\"%s\"", lxc_cmd_str(req
.cmd
));
1132 if (ret
!= sizeof(req
)) {
1133 WARN("Failed to receive full command request. Ignoring request "
1134 "for \"%s\"", lxc_cmd_str(req
.cmd
));
1139 if ((req
.datalen
> LXC_CMD_DATA_MAX
) &&
1140 (req
.cmd
!= LXC_CMD_CONSOLE_LOG
)) {
1141 ERROR("Received command data length %d is too large for "
1142 "command \"%s\"", req
.datalen
, lxc_cmd_str(req
.cmd
));
1148 if (req
.datalen
> 0) {
1149 /* LXC_CMD_CONSOLE_LOG needs to be able to allocate data
1150 * that exceeds LXC_CMD_DATA_MAX: use malloc() for that.
1152 if (req
.cmd
== LXC_CMD_CONSOLE_LOG
)
1153 reqdata
= malloc(req
.datalen
);
1155 reqdata
= alloca(req
.datalen
);
1157 ERROR("Failed to allocate memory for \"%s\" command",
1158 lxc_cmd_str(req
.cmd
));
1164 ret
= recv(fd
, reqdata
, req
.datalen
, 0);
1165 if (ret
!= req
.datalen
) {
1166 WARN("Failed to receive full command request. Ignoring "
1167 "request for \"%s\"", lxc_cmd_str(req
.cmd
));
1168 ret
= LXC_MAINLOOP_ERROR
;
1175 ret
= lxc_cmd_process(fd
, &req
, handler
);
1177 /* This is not an error, but only a request to close fd. */
1178 ret
= LXC_MAINLOOP_CONTINUE
;
1183 if (req
.cmd
== LXC_CMD_CONSOLE_LOG
&& reqdata
)
1189 lxc_cmd_fd_cleanup(fd
, handler
, descr
, req
.cmd
);
1193 static int lxc_cmd_accept(int fd
, uint32_t events
, void *data
,
1194 struct lxc_epoll_descr
*descr
)
1197 int opt
= 1, ret
= -1;
1199 connection
= accept(fd
, NULL
, 0);
1200 if (connection
< 0) {
1201 SYSERROR("Failed to accept connection to run command.");
1202 return LXC_MAINLOOP_ERROR
;
1205 ret
= fcntl(connection
, F_SETFD
, FD_CLOEXEC
);
1207 SYSERROR("Failed to set close-on-exec on incoming command connection");
1211 ret
= setsockopt(connection
, SOL_SOCKET
, SO_PASSCRED
, &opt
, sizeof(opt
));
1213 SYSERROR("Failed to enable necessary credentials on command socket");
1217 ret
= lxc_mainloop_add_handler(descr
, connection
, lxc_cmd_handler
, data
);
1219 ERROR("Failed to add command handler");
1231 int lxc_cmd_init(const char *name
, const char *lxcpath
, const char *suffix
)
1234 char path
[sizeof(((struct sockaddr_un
*)0)->sun_path
)] = {0};
1235 char *offset
= &path
[1];
1237 /* -2 here because this is an abstract unix socket so it needs a
1238 * leading \0, and we null terminate, so it needs a trailing \0.
1239 * Although null termination isn't required by the API, we do it anyway
1240 * because we print the sockname out sometimes.
1242 len
= sizeof(path
) - 2;
1243 ret
= lxc_make_abstract_socket_name(offset
, len
, name
, lxcpath
, NULL
, suffix
);
1246 TRACE("Creating abstract unix socket \"%s\"", offset
);
1248 fd
= lxc_abstract_unix_open(path
, SOCK_STREAM
, 0);
1250 SYSERROR("Failed to create command socket %s", offset
);
1251 if (errno
== EADDRINUSE
)
1252 ERROR("Container \"%s\" appears to be already running", name
);
1257 ret
= fcntl(fd
, F_SETFD
, FD_CLOEXEC
);
1259 SYSERROR("Failed to set FD_CLOEXEC on command socket file descriptor");
1267 int lxc_cmd_mainloop_add(const char *name
, struct lxc_epoll_descr
*descr
,
1268 struct lxc_handler
*handler
)
1271 int fd
= handler
->conf
->maincmd_fd
;
1273 ret
= lxc_mainloop_add_handler(descr
, fd
, lxc_cmd_accept
, handler
);
1275 ERROR("Failed to add handler for command socket");