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>
43 #include "commands_utils.h"
51 #include "memory_utils.h"
58 * This file provides the different functions for clients to query/command the
59 * server. The client is typically some lxc tool and the server is typically the
60 * container (ie. lxc-start).
62 * Each command is transactional, the clients send a request to the server and
63 * the server answers the request with a message giving the request's status
64 * (zero or a negative errno value). Both the request and response may contain
67 * Each command is wrapped in a ancillary message in order to pass a credential
68 * making possible to the server to check if the client is allowed to ask for
69 * this command or not.
71 * IMPORTANTLY: Note that semantics for current commands are fixed. If you wish
72 * to make any changes to how, say, LXC_CMD_GET_CONFIG_ITEM works by adding
73 * information to the end of cmd.data, then you must introduce a new
74 * LXC_CMD_GET_CONFIG_ITEM_V2 define with a new number. You may wish to also
75 * mark LXC_CMD_GET_CONFIG_ITEM deprecated in commands.h.
77 * This is necessary in order to avoid having a newly compiled lxc command
78 * communicating with a running (old) monitor from crashing the running
82 lxc_log_define(commands
, lxc
);
84 static const char *lxc_cmd_str(lxc_cmd_t cmd
)
86 static const char *const cmdname
[LXC_CMD_MAX
] = {
87 [LXC_CMD_CONSOLE
] = "console",
88 [LXC_CMD_TERMINAL_WINCH
] = "terminal_winch",
89 [LXC_CMD_STOP
] = "stop",
90 [LXC_CMD_GET_STATE
] = "get_state",
91 [LXC_CMD_GET_INIT_PID
] = "get_init_pid",
92 [LXC_CMD_GET_CLONE_FLAGS
] = "get_clone_flags",
93 [LXC_CMD_GET_CGROUP
] = "get_cgroup",
94 [LXC_CMD_GET_CONFIG_ITEM
] = "get_config_item",
95 [LXC_CMD_GET_NAME
] = "get_name",
96 [LXC_CMD_GET_LXCPATH
] = "get_lxcpath",
97 [LXC_CMD_ADD_STATE_CLIENT
] = "add_state_client",
98 [LXC_CMD_CONSOLE_LOG
] = "console_log",
99 [LXC_CMD_SERVE_STATE_CLIENTS
] = "serve_state_clients",
102 if (cmd
>= LXC_CMD_MAX
)
103 return "Invalid request";
109 * lxc_cmd_rsp_recv: Receive a response to a command
111 * @sock : the socket connected to the container
112 * @cmd : command to put response in
114 * Returns the size of the response message or < 0 on failure
116 * Note that if the command response datalen > 0, then data is
117 * a malloc()ed buffer and should be free()ed by the caller. If
118 * the response data is <= a void * worth of data, it will be
119 * stored directly in data and datalen will be 0.
121 * As a special case, the response for LXC_CMD_CONSOLE is created
122 * here as it contains an fd for the master pty passed through the
125 static int lxc_cmd_rsp_recv(int sock
, struct lxc_cmd_rr
*cmd
)
128 struct lxc_cmd_rsp
*rsp
= &cmd
->rsp
;
130 ret
= lxc_abstract_unix_recv_fds(sock
, &rspfd
, 1, rsp
, sizeof(*rsp
));
132 SYSWARN("Failed to receive response for command \"%s\"",
133 lxc_cmd_str(cmd
->req
.cmd
));
135 if (errno
== ECONNRESET
)
140 TRACE("Command \"%s\" received response", lxc_cmd_str(cmd
->req
.cmd
));
142 if (cmd
->req
.cmd
== LXC_CMD_CONSOLE
) {
143 struct lxc_cmd_console_rsp_data
*rspdata
;
145 /* recv() returns 0 bytes when a tty cannot be allocated,
146 * rsp->ret is < 0 when the peer permission check failed
148 if (ret
== 0 || rsp
->ret
< 0)
151 rspdata
= malloc(sizeof(*rspdata
));
154 ERROR("Failed to allocate response buffer for command \"%s\"",
155 lxc_cmd_str(cmd
->req
.cmd
));
159 rspdata
->masterfd
= rspfd
;
160 rspdata
->ttynum
= PTR_TO_INT(rsp
->data
);
164 if (rsp
->datalen
== 0) {
165 DEBUG("Response data length for command \"%s\" is 0",
166 lxc_cmd_str(cmd
->req
.cmd
));
170 if ((rsp
->datalen
> LXC_CMD_DATA_MAX
) &&
171 (cmd
->req
.cmd
!= LXC_CMD_CONSOLE_LOG
)) {
172 ERROR("Response data for command \"%s\" is too long: %d bytes > %d",
173 lxc_cmd_str(cmd
->req
.cmd
), rsp
->datalen
, LXC_CMD_DATA_MAX
);
177 if (cmd
->req
.cmd
== LXC_CMD_CONSOLE_LOG
) {
178 rsp
->data
= malloc(rsp
->datalen
+ 1);
179 ((char *)rsp
->data
)[rsp
->datalen
] = '\0';
181 rsp
->data
= malloc(rsp
->datalen
);
185 ERROR("Failed to allocate response buffer for command \"%s\"",
186 lxc_cmd_str(cmd
->req
.cmd
));
190 ret
= lxc_recv_nointr(sock
, rsp
->data
, rsp
->datalen
, 0);
191 if (ret
!= rsp
->datalen
) {
192 SYSERROR("Failed to receive response data for command \"%s\"",
193 lxc_cmd_str(cmd
->req
.cmd
));
201 * lxc_cmd_rsp_send: Send a command response
203 * @fd : file descriptor of socket to send response on
204 * @rsp : response to send
206 * Returns 0 on success, < 0 on failure
208 static int lxc_cmd_rsp_send(int fd
, struct lxc_cmd_rsp
*rsp
)
213 ret
= lxc_send_nointr(fd
, rsp
, sizeof(*rsp
), MSG_NOSIGNAL
);
214 if (ret
< 0 || (size_t)ret
!= sizeof(*rsp
)) {
215 SYSERROR("Failed to send command response %zd", ret
);
219 if (!rsp
->data
|| rsp
->datalen
<= 0)
223 ret
= lxc_send_nointr(fd
, rsp
->data
, rsp
->datalen
, MSG_NOSIGNAL
);
224 if (ret
< 0 || ret
!= (ssize_t
)rsp
->datalen
) {
225 SYSWARN("Failed to send command response data %zd", ret
);
232 static int lxc_cmd_send(const char *name
, struct lxc_cmd_rr
*cmd
,
233 const char *lxcpath
, const char *hashed_sock_name
)
235 int client_fd
, saved_errno
;
238 client_fd
= lxc_cmd_connect(name
, lxcpath
, hashed_sock_name
, "command");
242 ret
= lxc_abstract_unix_send_credential(client_fd
, &cmd
->req
,
244 if (ret
< 0 || (size_t)ret
!= sizeof(cmd
->req
))
247 if (cmd
->req
.datalen
<= 0)
251 ret
= lxc_send_nointr(client_fd
, (void *)cmd
->req
.data
,
252 cmd
->req
.datalen
, MSG_NOSIGNAL
);
253 if (ret
< 0 || ret
!= (ssize_t
)cmd
->req
.datalen
)
267 * lxc_cmd: Connect to the specified running container, send it a command
268 * request and collect the response
270 * @name : name of container to connect to
271 * @cmd : command with initialized request to send
272 * @stopped : output indicator if the container was not running
273 * @lxcpath : the lxcpath in which the container is running
275 * Returns the size of the response message on success, < 0 on failure
277 * Note that there is a special case for LXC_CMD_CONSOLE. For this command
278 * the fd cannot be closed because it is used as a placeholder to indicate
279 * that a particular tty slot is in use. The fd is also used as a signal to
280 * the container that when the caller dies or closes the fd, the container
281 * will notice the fd on its side of the socket in its mainloop select and
282 * then free the slot with lxc_cmd_fd_cleanup(). The socket fd will be
283 * returned in the cmd response structure.
285 static int lxc_cmd(const char *name
, struct lxc_cmd_rr
*cmd
, int *stopped
,
286 const char *lxcpath
, const char *hashed_sock_name
)
288 int client_fd
, saved_errno
;
290 bool stay_connected
= false;
292 if (cmd
->req
.cmd
== LXC_CMD_CONSOLE
||
293 cmd
->req
.cmd
== LXC_CMD_ADD_STATE_CLIENT
)
294 stay_connected
= true;
298 client_fd
= lxc_cmd_send(name
, cmd
, lxcpath
, hashed_sock_name
);
300 SYSTRACE("Command \"%s\" failed to connect command socket",
301 lxc_cmd_str(cmd
->req
.cmd
));
303 if (errno
== ECONNREFUSED
|| errno
== EPIPE
)
309 ret
= lxc_cmd_rsp_recv(client_fd
, cmd
);
310 if (ret
< 0 && errno
== ECONNRESET
)
313 if (!stay_connected
|| ret
<= 0) {
320 if (stay_connected
&& ret
> 0)
321 cmd
->rsp
.ret
= client_fd
;
326 int lxc_try_cmd(const char *name
, const char *lxcpath
)
329 struct lxc_cmd_rr cmd
= {
330 .req
= { .cmd
= LXC_CMD_GET_INIT_PID
},
333 ret
= lxc_cmd(name
, &cmd
, &stopped
, lxcpath
, NULL
);
336 if (ret
> 0 && cmd
.rsp
.ret
< 0) {
343 /* At this point we weren't denied access, and the container *was*
344 * started. There was some inexplicable error in the protocol. I'm not
345 * clear on whether we should return -1 here, but we didn't receive a
346 * -EACCES, so technically it's not that we're not allowed to control
347 * the container - it's just not behaving.
352 /* Implementations of the commands and their callbacks */
355 * lxc_cmd_get_init_pid: Get pid of the container's init process
357 * @name : name of container to connect to
358 * @lxcpath : the lxcpath in which the container is running
360 * Returns the pid on success, < 0 on failure
362 pid_t
lxc_cmd_get_init_pid(const char *name
, const char *lxcpath
)
366 struct lxc_cmd_rr cmd
= {
368 .cmd
= LXC_CMD_GET_INIT_PID
371 .data
= INTMAX_TO_PTR((intmax_t){-1})
375 ret
= lxc_cmd(name
, &cmd
, &stopped
, lxcpath
, NULL
);
379 pid
= PTR_TO_INTMAX(cmd
.rsp
.data
);
383 /* We need to assume that pid_t can actually hold any pid given to us
384 * by the kernel. If it can't it's a libc bug.
389 static int lxc_cmd_get_init_pid_callback(int fd
, struct lxc_cmd_req
*req
,
390 struct lxc_handler
*handler
)
392 intmax_t pid
= handler
->pid
;
394 struct lxc_cmd_rsp rsp
= {
395 .data
= INTMAX_TO_PTR(pid
)
398 return lxc_cmd_rsp_send(fd
, &rsp
);
402 * lxc_cmd_get_clone_flags: Get clone flags container was spawned with
404 * @name : name of container to connect to
405 * @lxcpath : the lxcpath in which the container is running
407 * Returns the clone flags on success, < 0 on failure
409 int lxc_cmd_get_clone_flags(const char *name
, const char *lxcpath
)
412 struct lxc_cmd_rr cmd
= {
413 .req
= { .cmd
= LXC_CMD_GET_CLONE_FLAGS
},
416 ret
= lxc_cmd(name
, &cmd
, &stopped
, lxcpath
, NULL
);
420 return PTR_TO_INT(cmd
.rsp
.data
);
423 static int lxc_cmd_get_clone_flags_callback(int fd
, struct lxc_cmd_req
*req
,
424 struct lxc_handler
*handler
)
426 struct lxc_cmd_rsp rsp
= { .data
= INT_TO_PTR(handler
->ns_clone_flags
) };
428 return lxc_cmd_rsp_send(fd
, &rsp
);
432 * lxc_cmd_get_cgroup_path: Calculate a container's cgroup path for a
433 * particular subsystem. This is the cgroup path relative to the root
434 * of the cgroup filesystem.
436 * @name : name of container to connect to
437 * @lxcpath : the lxcpath in which the container is running
438 * @subsystem : the subsystem being asked about
440 * Returns the path on success, NULL on failure. The caller must free() the
443 char *lxc_cmd_get_cgroup_path(const char *name
, const char *lxcpath
,
444 const char *subsystem
)
447 struct lxc_cmd_rr cmd
= {
449 .cmd
= LXC_CMD_GET_CGROUP
,
455 cmd
.req
.data
= subsystem
;
458 cmd
.req
.datalen
= strlen(subsystem
) + 1;
460 ret
= lxc_cmd(name
, &cmd
, &stopped
, lxcpath
, NULL
);
467 if (cmd
.rsp
.ret
< 0 || cmd
.rsp
.datalen
< 0)
473 static int lxc_cmd_get_cgroup_callback(int fd
, struct lxc_cmd_req
*req
,
474 struct lxc_handler
*handler
)
477 struct lxc_cmd_rsp rsp
;
478 struct cgroup_ops
*cgroup_ops
= handler
->cgroup_ops
;
480 if (req
->datalen
> 0)
481 path
= cgroup_ops
->get_cgroup(cgroup_ops
, req
->data
);
483 path
= cgroup_ops
->get_cgroup(cgroup_ops
, NULL
);
488 rsp
.datalen
= strlen(path
) + 1;
489 rsp
.data
= (char *)path
;
491 return lxc_cmd_rsp_send(fd
, &rsp
);
495 * lxc_cmd_get_config_item: Get config item the running container
497 * @name : name of container to connect to
498 * @item : the configuration item to retrieve (ex: lxc.net.0.veth.pair)
499 * @lxcpath : the lxcpath in which the container is running
501 * Returns the item on success, NULL on failure. The caller must free() the
504 char *lxc_cmd_get_config_item(const char *name
, const char *item
,
508 struct lxc_cmd_rr cmd
= {
509 .req
= { .cmd
= LXC_CMD_GET_CONFIG_ITEM
,
511 .datalen
= strlen(item
) + 1,
515 ret
= lxc_cmd(name
, &cmd
, &stopped
, lxcpath
, NULL
);
519 if (cmd
.rsp
.ret
== 0)
525 static int lxc_cmd_get_config_item_callback(int fd
, struct lxc_cmd_req
*req
,
526 struct lxc_handler
*handler
)
528 __do_free
char *cidata
= NULL
;
530 struct lxc_config_t
*item
;
531 struct lxc_cmd_rsp rsp
;
533 memset(&rsp
, 0, sizeof(rsp
));
534 item
= lxc_get_config(req
->data
);
538 cilen
= item
->get(req
->data
, NULL
, 0, handler
->conf
, NULL
);
542 cidata
= must_realloc(NULL
, cilen
+ 1);
543 if (item
->get(req
->data
, cidata
, cilen
+ 1, handler
->conf
, NULL
) != cilen
)
546 cidata
[cilen
] = '\0';
548 rsp
.datalen
= cilen
+ 1;
555 return lxc_cmd_rsp_send(fd
, &rsp
);
559 * lxc_cmd_get_state: Get current state of the container
561 * @name : name of container to connect to
562 * @lxcpath : the lxcpath in which the container is running
564 * Returns the state on success, < 0 on failure
566 int lxc_cmd_get_state(const char *name
, const char *lxcpath
)
569 struct lxc_cmd_rr cmd
= {
570 .req
= { .cmd
= LXC_CMD_GET_STATE
}
573 ret
= lxc_cmd(name
, &cmd
, &stopped
, lxcpath
, NULL
);
574 if (ret
< 0 && stopped
)
581 WARN("Container \"%s\" has stopped before sending its state", name
);
585 DEBUG("Container \"%s\" is in \"%s\" state", name
,
586 lxc_state2str(PTR_TO_INT(cmd
.rsp
.data
)));
588 return PTR_TO_INT(cmd
.rsp
.data
);
591 static int lxc_cmd_get_state_callback(int fd
, struct lxc_cmd_req
*req
,
592 struct lxc_handler
*handler
)
594 struct lxc_cmd_rsp rsp
= { .data
= INT_TO_PTR(handler
->state
) };
596 return lxc_cmd_rsp_send(fd
, &rsp
);
600 * lxc_cmd_stop: Stop the container previously started with lxc_start. All
601 * the processes running inside this container will be killed.
603 * @name : name of container to connect to
604 * @lxcpath : the lxcpath in which the container is running
606 * Returns 0 on success, < 0 on failure
608 int lxc_cmd_stop(const char *name
, const char *lxcpath
)
611 struct lxc_cmd_rr cmd
= {
612 .req
= { .cmd
= LXC_CMD_STOP
},
615 ret
= lxc_cmd(name
, &cmd
, &stopped
, lxcpath
, NULL
);
618 INFO("Container \"%s\" is already stopped", name
);
625 /* We do not expect any answer, because we wait for the connection to be
629 errno
= -cmd
.rsp
.ret
;
630 SYSERROR("Failed to stop container \"%s\"", name
);
634 INFO("Container \"%s\" has stopped", name
);
638 static int lxc_cmd_stop_callback(int fd
, struct lxc_cmd_req
*req
,
639 struct lxc_handler
*handler
)
641 struct lxc_cmd_rsp rsp
;
642 int stopsignal
= SIGKILL
;
643 struct cgroup_ops
*cgroup_ops
= handler
->cgroup_ops
;
645 if (handler
->conf
->stopsignal
)
646 stopsignal
= handler
->conf
->stopsignal
;
647 memset(&rsp
, 0, sizeof(rsp
));
648 rsp
.ret
= kill(handler
->pid
, stopsignal
);
650 /* We can't just use lxc_unfreeze() since we are already in the
651 * context of handling the STOP cmd in lxc-start, and calling
652 * lxc_unfreeze() would do another cmd (GET_CGROUP) which would
655 if (!cgroup_ops
->get_cgroup(cgroup_ops
, "freezer"))
658 if (cgroup_ops
->unfreeze(cgroup_ops
))
661 ERROR("Failed to unfreeze container \"%s\"", handler
->name
);
665 return lxc_cmd_rsp_send(fd
, &rsp
);
669 * lxc_cmd_terminal_winch: noop
671 * @name : name of container to connect to
672 * @lxcpath : the lxcpath in which the container is running
674 * Returns 0 on success, < 0 on failure
676 int lxc_cmd_terminal_winch(const char *name
, const char *lxcpath
)
681 static int lxc_cmd_terminal_winch_callback(int fd
, struct lxc_cmd_req
*req
,
682 struct lxc_handler
*handler
)
684 /* should never be called */
689 * lxc_cmd_console: Open an fd to a tty in the container
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
697 * Returns fd holding tty allocated on success, < 0 on failure
699 int lxc_cmd_console(const char *name
, int *ttynum
, int *fd
, const char *lxcpath
)
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
) },
707 ret
= lxc_cmd(name
, &cmd
, &stopped
, lxcpath
, NULL
);
711 if (cmd
.rsp
.ret
< 0) {
712 errno
= -cmd
.rsp
.ret
;
713 SYSERROR("Denied access to tty");
719 ERROR("tty number %d invalid, busy or all ttys busy", *ttynum
);
724 rspdata
= cmd
.rsp
.data
;
725 if (rspdata
->masterfd
< 0) {
726 ERROR("Unable to allocate fd for tty %d", rspdata
->ttynum
);
730 ret
= cmd
.rsp
.ret
; /* socket fd */
731 *fd
= rspdata
->masterfd
;
732 *ttynum
= rspdata
->ttynum
;
733 INFO("Alloced fd %d for tty %d via socket %d", *fd
, rspdata
->ttynum
, ret
);
740 static int lxc_cmd_console_callback(int fd
, struct lxc_cmd_req
*req
,
741 struct lxc_handler
*handler
)
744 struct lxc_cmd_rsp rsp
;
745 int ttynum
= PTR_TO_INT(req
->data
);
747 masterfd
= lxc_terminal_allocate(handler
->conf
, fd
, &ttynum
);
751 memset(&rsp
, 0, sizeof(rsp
));
752 rsp
.data
= INT_TO_PTR(ttynum
);
753 ret
= lxc_abstract_unix_send_fds(fd
, &masterfd
, 1, &rsp
, sizeof(rsp
));
755 SYSERROR("Failed to send tty to client");
756 lxc_terminal_free(handler
->conf
, fd
);
763 /* Special indicator to lxc_cmd_handler() to close the fd and do
770 * lxc_cmd_get_name: Returns the name of the container
772 * @hashed_sock_name: hashed socket name
774 * Returns the name on success, NULL on failure.
776 char *lxc_cmd_get_name(const char *hashed_sock_name
)
779 struct lxc_cmd_rr cmd
= {
780 .req
= { .cmd
= LXC_CMD_GET_NAME
},
783 ret
= lxc_cmd(NULL
, &cmd
, &stopped
, NULL
, hashed_sock_name
);
787 if (cmd
.rsp
.ret
== 0)
793 static int lxc_cmd_get_name_callback(int fd
, struct lxc_cmd_req
*req
,
794 struct lxc_handler
*handler
)
796 struct lxc_cmd_rsp rsp
;
798 memset(&rsp
, 0, sizeof(rsp
));
800 rsp
.data
= (char *)handler
->name
;
801 rsp
.datalen
= strlen(handler
->name
) + 1;
804 return lxc_cmd_rsp_send(fd
, &rsp
);
808 * lxc_cmd_get_lxcpath: Returns the lxcpath of the container
810 * @hashed_sock_name: hashed socket name
812 * Returns the lxcpath on success, NULL on failure.
814 char *lxc_cmd_get_lxcpath(const char *hashed_sock_name
)
817 struct lxc_cmd_rr cmd
= {
818 .req
= { .cmd
= LXC_CMD_GET_LXCPATH
},
821 ret
= lxc_cmd(NULL
, &cmd
, &stopped
, NULL
, hashed_sock_name
);
825 if (cmd
.rsp
.ret
== 0)
831 static int lxc_cmd_get_lxcpath_callback(int fd
, struct lxc_cmd_req
*req
,
832 struct lxc_handler
*handler
)
834 struct lxc_cmd_rsp rsp
;
836 memset(&rsp
, 0, sizeof(rsp
));
839 rsp
.data
= (char *)handler
->lxcpath
;
840 rsp
.datalen
= strlen(handler
->lxcpath
) + 1;
842 return lxc_cmd_rsp_send(fd
, &rsp
);
845 int lxc_cmd_add_state_client(const char *name
, const char *lxcpath
,
846 lxc_state_t states
[MAX_STATE
],
847 int *state_client_fd
)
851 struct lxc_cmd_rr cmd
= {
853 .cmd
= LXC_CMD_ADD_STATE_CLIENT
,
855 .datalen
= (sizeof(lxc_state_t
) * MAX_STATE
)
859 ret
= lxc_cmd(name
, &cmd
, &stopped
, lxcpath
, NULL
);
860 if (states
[STOPPED
] != 0 && stopped
!= 0)
864 if (errno
!= ECONNREFUSED
)
865 SYSERROR("Failed to execute command");
870 /* We should now be guaranteed to get an answer from the state sending
873 if (cmd
.rsp
.ret
< 0) {
874 errno
= -cmd
.rsp
.ret
;
875 SYSERROR("Failed to receive socket fd");
879 state
= PTR_TO_INT(cmd
.rsp
.data
);
880 if (state
< MAX_STATE
) {
881 TRACE("Container is already in requested state %s", lxc_state2str(state
));
886 *state_client_fd
= cmd
.rsp
.ret
;
887 TRACE("Added state client %d to state client list", cmd
.rsp
.ret
);
891 static int lxc_cmd_add_state_client_callback(int fd
, struct lxc_cmd_req
*req
,
892 struct lxc_handler
*handler
)
895 struct lxc_cmd_rsp rsp
= {0};
897 if (req
->datalen
< 0)
900 if (req
->datalen
!= (sizeof(lxc_state_t
) * MAX_STATE
))
906 rsp
.ret
= lxc_add_state_client(fd
, handler
, (lxc_state_t
*)req
->data
);
910 rsp
.data
= INT_TO_PTR(rsp
.ret
);
912 ret
= lxc_cmd_rsp_send(fd
, &rsp
);
919 /* Special indicator to lxc_cmd_handler() to close the fd and do related
925 int lxc_cmd_console_log(const char *name
, const char *lxcpath
,
926 struct lxc_console_log
*log
)
929 struct lxc_cmd_console_log data
;
930 struct lxc_cmd_rr cmd
;
932 data
.clear
= log
->clear
;
933 data
.read
= log
->read
;
934 data
.read_max
= *log
->read_max
;
936 cmd
.req
.cmd
= LXC_CMD_CONSOLE_LOG
;
937 cmd
.req
.data
= &data
;
938 cmd
.req
.datalen
= sizeof(struct lxc_cmd_console_log
);
940 ret
= lxc_cmd(name
, &cmd
, &stopped
, lxcpath
, NULL
);
944 /* There is nothing to be read from the buffer. So clear any values we
945 * where passed to clearly indicate to the user that nothing went wrong.
947 if (cmd
.rsp
.ret
== -ENODATA
|| cmd
.rsp
.ret
== -EFAULT
|| cmd
.rsp
.ret
== -ENOENT
) {
952 /* This is a proper error so don't touch any values we were passed. */
956 *log
->read_max
= cmd
.rsp
.datalen
;
957 log
->data
= cmd
.rsp
.data
;
962 static int lxc_cmd_console_log_callback(int fd
, struct lxc_cmd_req
*req
,
963 struct lxc_handler
*handler
)
965 struct lxc_cmd_rsp rsp
;
966 uint64_t buffer_size
= handler
->conf
->console
.buffer_size
;
967 const struct lxc_cmd_console_log
*log
= req
->data
;
968 struct lxc_ringbuf
*buf
= &handler
->conf
->console
.ringbuf
;
973 if (buffer_size
<= 0)
976 if (log
->read
|| log
->write_logfile
)
977 rsp
.datalen
= lxc_ringbuf_used(buf
);
980 rsp
.data
= lxc_ringbuf_get_read_addr(buf
);
982 if (log
->read_max
> 0 && (log
->read_max
<= rsp
.datalen
))
983 rsp
.datalen
= log
->read_max
;
985 /* there's nothing to read */
987 if (log
->read
&& (buf
->r_off
== buf
->w_off
))
992 lxc_ringbuf_clear(buf
); /* clear the ringbuffer */
993 else if (rsp
.datalen
> 0)
994 lxc_ringbuf_move_read_addr(buf
, rsp
.datalen
);
997 return lxc_cmd_rsp_send(fd
, &rsp
);
1000 int lxc_cmd_serve_state_clients(const char *name
, const char *lxcpath
,
1005 struct lxc_cmd_rr cmd
= {
1007 .cmd
= LXC_CMD_SERVE_STATE_CLIENTS
,
1008 .data
= INT_TO_PTR(state
)
1012 ret
= lxc_cmd(name
, &cmd
, &stopped
, lxcpath
, NULL
);
1014 SYSERROR("Failed to execute command");
1021 static int lxc_cmd_serve_state_clients_callback(int fd
, struct lxc_cmd_req
*req
,
1022 struct lxc_handler
*handler
)
1025 lxc_state_t state
= PTR_TO_INT(req
->data
);
1026 struct lxc_cmd_rsp rsp
= {0};
1028 ret
= lxc_serve_state_clients(handler
->name
, handler
, state
);
1030 goto reap_client_fd
;
1032 ret
= lxc_cmd_rsp_send(fd
, &rsp
);
1034 goto reap_client_fd
;
1039 /* Special indicator to lxc_cmd_handler() to close the fd and do related
1045 static int lxc_cmd_process(int fd
, struct lxc_cmd_req
*req
,
1046 struct lxc_handler
*handler
)
1048 typedef int (*callback
)(int, struct lxc_cmd_req
*, struct lxc_handler
*);
1050 callback cb
[LXC_CMD_MAX
] = {
1051 [LXC_CMD_CONSOLE
] = lxc_cmd_console_callback
,
1052 [LXC_CMD_TERMINAL_WINCH
] = lxc_cmd_terminal_winch_callback
,
1053 [LXC_CMD_STOP
] = lxc_cmd_stop_callback
,
1054 [LXC_CMD_GET_STATE
] = lxc_cmd_get_state_callback
,
1055 [LXC_CMD_GET_INIT_PID
] = lxc_cmd_get_init_pid_callback
,
1056 [LXC_CMD_GET_CLONE_FLAGS
] = lxc_cmd_get_clone_flags_callback
,
1057 [LXC_CMD_GET_CGROUP
] = lxc_cmd_get_cgroup_callback
,
1058 [LXC_CMD_GET_CONFIG_ITEM
] = lxc_cmd_get_config_item_callback
,
1059 [LXC_CMD_GET_NAME
] = lxc_cmd_get_name_callback
,
1060 [LXC_CMD_GET_LXCPATH
] = lxc_cmd_get_lxcpath_callback
,
1061 [LXC_CMD_ADD_STATE_CLIENT
] = lxc_cmd_add_state_client_callback
,
1062 [LXC_CMD_CONSOLE_LOG
] = lxc_cmd_console_log_callback
,
1063 [LXC_CMD_SERVE_STATE_CLIENTS
] = lxc_cmd_serve_state_clients_callback
,
1066 if (req
->cmd
>= LXC_CMD_MAX
) {
1067 ERROR("Undefined command id %d", req
->cmd
);
1070 return cb
[req
->cmd
](fd
, req
, handler
);
1073 static void lxc_cmd_fd_cleanup(int fd
, struct lxc_handler
*handler
,
1074 struct lxc_epoll_descr
*descr
,
1075 const lxc_cmd_t cmd
)
1077 struct lxc_state_client
*client
;
1078 struct lxc_list
*cur
, *next
;
1080 lxc_terminal_free(handler
->conf
, fd
);
1081 lxc_mainloop_del_handler(descr
, fd
);
1082 if (cmd
!= LXC_CMD_ADD_STATE_CLIENT
) {
1087 lxc_list_for_each_safe(cur
, &handler
->conf
->state_clients
, next
) {
1089 if (client
->clientfd
!= fd
)
1092 /* kick client from list */
1094 close(client
->clientfd
);
1097 /* No need to walk the whole list. If we found the state client
1098 * fd there can't be a second one.
1104 static int lxc_cmd_handler(int fd
, uint32_t events
, void *data
,
1105 struct lxc_epoll_descr
*descr
)
1107 __do_free
void *reqdata
= NULL
;
1109 struct lxc_cmd_req req
;
1110 struct lxc_handler
*handler
= data
;
1112 ret
= lxc_abstract_unix_rcv_credential(fd
, &req
, sizeof(req
));
1114 SYSERROR("Failed to receive data on command socket for command "
1115 "\"%s\"", lxc_cmd_str(req
.cmd
));
1117 if (errno
== EACCES
) {
1118 /* We don't care for the peer, just send and close. */
1119 struct lxc_cmd_rsp rsp
= {.ret
= ret
};
1121 lxc_cmd_rsp_send(fd
, &rsp
);
1130 if (ret
!= sizeof(req
)) {
1131 WARN("Failed to receive full command request. Ignoring request "
1132 "for \"%s\"", lxc_cmd_str(req
.cmd
));
1137 if ((req
.datalen
> LXC_CMD_DATA_MAX
) &&
1138 (req
.cmd
!= LXC_CMD_CONSOLE_LOG
)) {
1139 ERROR("Received command data length %d is too large for "
1140 "command \"%s\"", req
.datalen
, lxc_cmd_str(req
.cmd
));
1146 if (req
.datalen
> 0) {
1147 reqdata
= must_realloc(NULL
, req
.datalen
);
1148 ret
= lxc_recv_nointr(fd
, reqdata
, req
.datalen
, 0);
1149 if (ret
!= req
.datalen
) {
1150 WARN("Failed to receive full command request. Ignoring "
1151 "request for \"%s\"", lxc_cmd_str(req
.cmd
));
1152 ret
= LXC_MAINLOOP_ERROR
;
1159 ret
= lxc_cmd_process(fd
, &req
, handler
);
1161 /* This is not an error, but only a request to close fd. */
1162 ret
= LXC_MAINLOOP_CONTINUE
;
1170 lxc_cmd_fd_cleanup(fd
, handler
, descr
, req
.cmd
);
1174 static int lxc_cmd_accept(int fd
, uint32_t events
, void *data
,
1175 struct lxc_epoll_descr
*descr
)
1178 int opt
= 1, ret
= -1;
1180 connection
= accept(fd
, NULL
, 0);
1181 if (connection
< 0) {
1182 SYSERROR("Failed to accept connection to run command");
1183 return LXC_MAINLOOP_ERROR
;
1186 ret
= fcntl(connection
, F_SETFD
, FD_CLOEXEC
);
1188 SYSERROR("Failed to set close-on-exec on incoming command connection");
1192 ret
= setsockopt(connection
, SOL_SOCKET
, SO_PASSCRED
, &opt
, sizeof(opt
));
1194 SYSERROR("Failed to enable necessary credentials on command socket");
1198 ret
= lxc_mainloop_add_handler(descr
, connection
, lxc_cmd_handler
, data
);
1200 ERROR("Failed to add command handler");
1212 int lxc_cmd_init(const char *name
, const char *lxcpath
, const char *suffix
)
1215 char path
[LXC_AUDS_ADDR_LEN
] = {0};
1217 ret
= lxc_make_abstract_socket_name(path
, sizeof(path
), name
, lxcpath
, NULL
, suffix
);
1220 TRACE("Creating abstract unix socket \"%s\"", &path
[1]);
1222 fd
= lxc_abstract_unix_open(path
, SOCK_STREAM
, 0);
1224 SYSERROR("Failed to create command socket %s", &path
[1]);
1225 if (errno
== EADDRINUSE
)
1226 ERROR("Container \"%s\" appears to be already running", name
);
1231 ret
= fcntl(fd
, F_SETFD
, FD_CLOEXEC
);
1233 SYSERROR("Failed to set FD_CLOEXEC on command socket file descriptor");
1241 int lxc_cmd_mainloop_add(const char *name
, struct lxc_epoll_descr
*descr
,
1242 struct lxc_handler
*handler
)
1245 int fd
= handler
->conf
->maincmd_fd
;
1247 ret
= lxc_mainloop_add_handler(descr
, fd
, lxc_cmd_accept
, handler
);
1249 ERROR("Failed to add handler for command socket");