]> git.proxmox.com Git - mirror_lxc.git/blame - src/lxc/commands.c
confile: fix jump table order
[mirror_lxc.git] / src / lxc / commands.c
CommitLineData
cc73685d 1/* SPDX-License-Identifier: LGPL-2.1+ */
724e753c 2
d38dd64a
CB
3#ifndef _GNU_SOURCE
4#define _GNU_SOURCE 1
5#endif
cf685555 6#include <caps.h>
724e753c 7#include <errno.h>
91480a0f 8#include <fcntl.h>
fe84a562 9#include <malloc.h>
37515ebd 10#include <poll.h>
fe84a562
CB
11#include <signal.h>
12#include <stdio.h>
13#include <stdlib.h>
fe84a562 14#include <sys/param.h>
724e753c
MN
15#include <sys/socket.h>
16#include <sys/un.h>
d38dd64a 17#include <unistd.h>
724e753c 18
fe84a562 19#include "af_unix.h"
f2363e38 20#include "cgroup.h"
2a63b5cb 21#include "cgroups/cgroup2_devices.h"
724e753c 22#include "commands.h"
bbf5cf35 23#include "commands_utils.h"
fe84a562 24#include "conf.h"
d38dd64a 25#include "config.h"
ef6e34ee 26#include "confile.h"
fe84a562
CB
27#include "log.h"
28#include "lxc.h"
dbc9832d 29#include "lxclock.h"
cdb2a47f 30#include "lxcseccomp.h"
724e753c 31#include "mainloop.h"
5265a60c 32#include "memory_utils.h"
dbc9832d 33#include "monitor.h"
fe84a562 34#include "start.h"
0ed9b1bc 35#include "terminal.h"
fe84a562 36#include "utils.h"
724e753c 37
ded1d23f 38/*
fe84a562
CB
39 * This file provides the different functions for clients to query/command the
40 * server. The client is typically some lxc tool and the server is typically the
41 * container (ie. lxc-start).
ded1d23f 42 *
fe84a562
CB
43 * Each command is transactional, the clients send a request to the server and
44 * the server answers the request with a message giving the request's status
45 * (zero or a negative errno value). Both the request and response may contain
46 * additional data.
ded1d23f 47 *
fe84a562
CB
48 * Each command is wrapped in a ancillary message in order to pass a credential
49 * making possible to the server to check if the client is allowed to ask for
50 * this command or not.
80bcb053 51 *
fe84a562
CB
52 * IMPORTANTLY: Note that semantics for current commands are fixed. If you wish
53 * to make any changes to how, say, LXC_CMD_GET_CONFIG_ITEM works by adding
54 * information to the end of cmd.data, then you must introduce a new
55 * LXC_CMD_GET_CONFIG_ITEM_V2 define with a new number. You may wish to also
56 * mark LXC_CMD_GET_CONFIG_ITEM deprecated in commands.h.
80bcb053
SH
57 *
58 * This is necessary in order to avoid having a newly compiled lxc command
59 * communicating with a running (old) monitor from crashing the running
60 * container.
ded1d23f
DL
61 */
62
ac2cecc4 63lxc_log_define(commands, lxc);
724e753c 64
ef6e34ee 65static const char *lxc_cmd_str(lxc_cmd_t cmd)
724e753c 66{
fe84a562 67 static const char *const cmdname[LXC_CMD_MAX] = {
2a63b5cb
CB
68 [LXC_CMD_CONSOLE] = "console",
69 [LXC_CMD_TERMINAL_WINCH] = "terminal_winch",
70 [LXC_CMD_STOP] = "stop",
71 [LXC_CMD_GET_STATE] = "get_state",
72 [LXC_CMD_GET_INIT_PID] = "get_init_pid",
73 [LXC_CMD_GET_CLONE_FLAGS] = "get_clone_flags",
74 [LXC_CMD_GET_CGROUP] = "get_cgroup",
75 [LXC_CMD_GET_CONFIG_ITEM] = "get_config_item",
76 [LXC_CMD_GET_NAME] = "get_name",
77 [LXC_CMD_GET_LXCPATH] = "get_lxcpath",
78 [LXC_CMD_ADD_STATE_CLIENT] = "add_state_client",
79 [LXC_CMD_CONSOLE_LOG] = "console_log",
80 [LXC_CMD_SERVE_STATE_CLIENTS] = "serve_state_clients",
81 [LXC_CMD_SECCOMP_NOTIFY_ADD_LISTENER] = "seccomp_notify_add_listener",
82 [LXC_CMD_ADD_BPF_DEVICE_CGROUP] = "add_bpf_device_cgroup",
018051e3
CB
83 [LXC_CMD_FREEZE] = "freeze",
84 [LXC_CMD_UNFREEZE] = "unfreeze",
bad788b0 85 [LXC_CMD_GET_CGROUP2_FD] = "get_cgroup2_fd",
746aab51 86 [LXC_CMD_GET_INIT_PIDFD] = "get_init_pidfd",
a900cbaf
WB
87 [LXC_CMD_GET_LIMITING_CGROUP] = "get_limiting_cgroup",
88 [LXC_CMD_GET_LIMITING_CGROUP2_FD] = "get_limiting_cgroup2_fd",
ef6e34ee 89 };
724e753c 90
f371aca9 91 if (cmd >= LXC_CMD_MAX)
a8007512 92 return "Invalid request";
fe84a562 93
ef6e34ee
DE
94 return cmdname[cmd];
95}
96
97/*
98 * lxc_cmd_rsp_recv: Receive a response to a command
99 *
100 * @sock : the socket connected to the container
101 * @cmd : command to put response in
102 *
103 * Returns the size of the response message or < 0 on failure
104 *
105 * Note that if the command response datalen > 0, then data is
106 * a malloc()ed buffer and should be free()ed by the caller. If
107 * the response data is <= a void * worth of data, it will be
108 * stored directly in data and datalen will be 0.
109 *
110 * As a special case, the response for LXC_CMD_CONSOLE is created
0115f8fd
DE
111 * here as it contains an fd for the master pty passed through the
112 * unix socket.
ef6e34ee
DE
113 */
114static int lxc_cmd_rsp_recv(int sock, struct lxc_cmd_rr *cmd)
115{
f62cf1d4 116 __do_close int fd_rsp = -EBADF;
23a917e5 117 int ret;
ef6e34ee
DE
118 struct lxc_cmd_rsp *rsp = &cmd->rsp;
119
23a917e5
CB
120 ret = lxc_abstract_unix_recv_fds(sock, &fd_rsp, 1, rsp, sizeof(*rsp));
121 if (ret < 0)
122 return log_warn_errno(-1,
123 errno, "Failed to receive response for command \"%s\"",
124 lxc_cmd_str(cmd->req.cmd));
fe84a562 125 TRACE("Command \"%s\" received response", lxc_cmd_str(cmd->req.cmd));
ef6e34ee
DE
126
127 if (cmd->req.cmd == LXC_CMD_CONSOLE) {
128 struct lxc_cmd_console_rsp_data *rspdata;
129
0115f8fd
DE
130 /* recv() returns 0 bytes when a tty cannot be allocated,
131 * rsp->ret is < 0 when the peer permission check failed
132 */
133 if (ret == 0 || rsp->ret < 0)
134 return 0;
135
ef6e34ee 136 rspdata = malloc(sizeof(*rspdata));
23a917e5
CB
137 if (!rspdata)
138 return log_warn_errno(-1,
139 ENOMEM, "Failed to receive response for command \"%s\"",
140 lxc_cmd_str(cmd->req.cmd));
2a850b2c 141
23a917e5 142 rspdata->masterfd = move_fd(fd_rsp);
ef6e34ee
DE
143 rspdata->ttynum = PTR_TO_INT(rsp->data);
144 rsp->data = rspdata;
145 }
146
a900cbaf
WB
147 if (cmd->req.cmd == LXC_CMD_GET_CGROUP2_FD ||
148 cmd->req.cmd == LXC_CMD_GET_LIMITING_CGROUP2_FD)
149 {
23a917e5
CB
150 int cgroup2_fd = move_fd(fd_rsp);
151 rsp->data = INT_TO_PTR(cgroup2_fd);
860e7c43 152 }
fe84a562 153
746aab51
CB
154 if (cmd->req.cmd == LXC_CMD_GET_INIT_PIDFD) {
155 int init_pidfd = move_fd(fd_rsp);
156 rsp->data = INT_TO_PTR(init_pidfd);
157 }
158
23a917e5
CB
159 if (rsp->datalen == 0)
160 return log_debug(ret,
161 "Response data length for command \"%s\" is 0",
162 lxc_cmd_str(cmd->req.cmd));
163
191d43cc 164 if ((rsp->datalen > LXC_CMD_DATA_MAX) &&
23a917e5
CB
165 (cmd->req.cmd != LXC_CMD_CONSOLE_LOG))
166 return log_error(-1, "Response data for command \"%s\" is too long: %d bytes > %d",
167 lxc_cmd_str(cmd->req.cmd), rsp->datalen,
168 LXC_CMD_DATA_MAX);
ef6e34ee 169
191d43cc
CB
170 if (cmd->req.cmd == LXC_CMD_CONSOLE_LOG) {
171 rsp->data = malloc(rsp->datalen + 1);
172 ((char *)rsp->data)[rsp->datalen] = '\0';
173 } else {
174 rsp->data = malloc(rsp->datalen);
175 }
23a917e5
CB
176 if (!rsp->data)
177 return log_error_errno(-1,
178 ENOMEM, "Failed to allocate response buffer for command \"%s\"",
179 lxc_cmd_str(cmd->req.cmd));
fe84a562 180
2a850b2c 181 ret = lxc_recv_nointr(sock, rsp->data, rsp->datalen, 0);
23a917e5
CB
182 if (ret != rsp->datalen)
183 return log_error_errno(-1,
184 errno, "Failed to receive response data for command \"%s\"",
185 lxc_cmd_str(cmd->req.cmd));
724e753c
MN
186
187 return ret;
188}
189
ef6e34ee
DE
190/*
191 * lxc_cmd_rsp_send: Send a command response
192 *
193 * @fd : file descriptor of socket to send response on
194 * @rsp : response to send
195 *
196 * Returns 0 on success, < 0 on failure
197 */
198static int lxc_cmd_rsp_send(int fd, struct lxc_cmd_rsp *rsp)
199{
fe84a562 200 ssize_t ret;
ef6e34ee 201
7fbb15ec
CB
202 errno = EMSGSIZE;
203 ret = lxc_send_nointr(fd, rsp, sizeof(*rsp), MSG_NOSIGNAL);
6c6497ea
CB
204 if (ret < 0 || (size_t)ret != sizeof(*rsp))
205 return log_error_errno(-1, errno, "Failed to send command response %zd", ret);
ef6e34ee 206
a674dfe1 207 if (!rsp->data || rsp->datalen <= 0)
fe84a562
CB
208 return 0;
209
7fbb15ec
CB
210 errno = EMSGSIZE;
211 ret = lxc_send_nointr(fd, rsp->data, rsp->datalen, MSG_NOSIGNAL);
6c6497ea
CB
212 if (ret < 0 || ret != (ssize_t)rsp->datalen)
213 return log_warn_errno(-1, errno, "Failed to send command response data %zd", ret);
fe84a562 214
ef6e34ee
DE
215 return 0;
216}
217
c01c2be6 218static int lxc_cmd_send(const char *name, struct lxc_cmd_rr *cmd,
fe84a562 219 const char *lxcpath, const char *hashed_sock_name)
c01c2be6 220{
f62cf1d4 221 __do_close int client_fd = -EBADF;
fe84a562 222 ssize_t ret = -1;
c01c2be6 223
9dfa4041 224 client_fd = lxc_cmd_connect(name, lxcpath, hashed_sock_name, "command");
2a850b2c 225 if (client_fd < 0)
fe84a562 226 return -1;
c01c2be6 227
fe84a562
CB
228 ret = lxc_abstract_unix_send_credential(client_fd, &cmd->req,
229 sizeof(cmd->req));
2a850b2c 230 if (ret < 0 || (size_t)ret != sizeof(cmd->req))
e96f9291 231 return -1;
9044b79e 232
cdb2a47f
CB
233 if (cmd->req.cmd == LXC_CMD_SECCOMP_NOTIFY_ADD_LISTENER) {
234 int notify_fd = PTR_TO_INT(cmd->req.data);
235 ret = lxc_abstract_unix_send_fds(client_fd, &notify_fd, 1, NULL, 0);
236 if (ret <= 0)
237 return -1;
238 } else {
239 if (cmd->req.datalen <= 0)
240 return move_fd(client_fd);
c01c2be6 241
cdb2a47f
CB
242 errno = EMSGSIZE;
243 ret = lxc_send_nointr(client_fd, (void *)cmd->req.data,
244 cmd->req.datalen, MSG_NOSIGNAL);
245 if (ret < 0 || ret != (ssize_t)cmd->req.datalen)
246 return -1;
247 }
c01c2be6 248
240fecd0 249 return move_fd(client_fd);
c01c2be6
CB
250}
251
ef6e34ee
DE
252/*
253 * lxc_cmd: Connect to the specified running container, send it a command
254 * request and collect the response
255 *
256 * @name : name of container to connect to
1e8cfdf6 257 * @cmd : command with initialized request to send
ef6e34ee
DE
258 * @stopped : output indicator if the container was not running
259 * @lxcpath : the lxcpath in which the container is running
260 *
261 * Returns the size of the response message on success, < 0 on failure
262 *
263 * Note that there is a special case for LXC_CMD_CONSOLE. For this command
264 * the fd cannot be closed because it is used as a placeholder to indicate
265 * that a particular tty slot is in use. The fd is also used as a signal to
266 * the container that when the caller dies or closes the fd, the container
0115f8fd
DE
267 * will notice the fd on its side of the socket in its mainloop select and
268 * then free the slot with lxc_cmd_fd_cleanup(). The socket fd will be
269 * returned in the cmd response structure.
ef6e34ee
DE
270 */
271static int lxc_cmd(const char *name, struct lxc_cmd_rr *cmd, int *stopped,
88556fd7 272 const char *lxcpath, const char *hashed_sock_name)
724e753c 273{
f62cf1d4 274 __do_close int client_fd = -EBADF;
fe84a562 275 int ret = -1;
dbc9832d
CB
276 bool stay_connected = false;
277
278 if (cmd->req.cmd == LXC_CMD_CONSOLE ||
54446942 279 cmd->req.cmd == LXC_CMD_ADD_STATE_CLIENT)
dbc9832d 280 stay_connected = true;
724e753c 281
0ecf64b5
SH
282 *stopped = 0;
283
c01c2be6
CB
284 client_fd = lxc_cmd_send(name, cmd, lxcpath, hashed_sock_name);
285 if (client_fd < 0) {
00e5ca13 286 if (errno == ECONNREFUSED || errno == EPIPE)
ef6e34ee 287 *stopped = 1;
3f903c04 288
6c6497ea
CB
289 return log_trace_errno(-1, errno, "Command \"%s\" failed to connect command socket",
290 lxc_cmd_str(cmd->req.cmd));
724e753c
MN
291 }
292
c01c2be6 293 ret = lxc_cmd_rsp_recv(client_fd, cmd);
2a850b2c 294 if (ret < 0 && errno == ECONNRESET)
6b7f85cb 295 *stopped = 1;
6a93ae77 296
ea2a070b
CB
297 TRACE("Opened new command socket connection fd %d for command \"%s\"",
298 client_fd, lxc_cmd_str(cmd->req.cmd));
299
c34ff119 300 if (stay_connected && ret > 0)
240fecd0 301 cmd->rsp.ret = move_fd(client_fd);
43eb6f29 302
1362f2eb 303 return ret;
724e753c
MN
304}
305
b494d2dd
SH
306int lxc_try_cmd(const char *name, const char *lxcpath)
307{
0ecf64b5 308 int stopped, ret;
b494d2dd
SH
309 struct lxc_cmd_rr cmd = {
310 .req = { .cmd = LXC_CMD_GET_INIT_PID },
311 };
312
88556fd7 313 ret = lxc_cmd(name, &cmd, &stopped, lxcpath, NULL);
b494d2dd
SH
314 if (stopped)
315 return 0;
316 if (ret > 0 && cmd.rsp.ret < 0) {
317 errno = cmd.rsp.ret;
318 return -1;
319 }
320 if (ret > 0)
321 return 0;
322
fe84a562
CB
323 /* At this point we weren't denied access, and the container *was*
324 * started. There was some inexplicable error in the protocol. I'm not
325 * clear on whether we should return -1 here, but we didn't receive a
326 * -EACCES, so technically it's not that we're not allowed to control
327 * the container - it's just not behaving.
b494d2dd
SH
328 */
329 return 0;
330}
331
e6bc68d6
WB
332/*
333 * Validate that the input is a proper string parameter. If not,
334 * send an EINVAL response and return -1.
335 *
336 * Precondition: there is non-zero-length data available.
337 */
338static int validate_string_request(int fd, const struct lxc_cmd_req *req)
339{
340 int ret;
341 size_t maxlen = req->datalen - 1;
342 const char *data = req->data;
343
344 if (data[maxlen] == 0 && strnlen(data, maxlen) == maxlen)
345 return 0;
346
347 struct lxc_cmd_rsp rsp = {
348 .ret = -EINVAL,
349 .datalen = 0,
350 .data = NULL,
351 };
352
353 ret = lxc_cmd_rsp_send(fd, &rsp);
354 if (ret < 0)
355 return LXC_CMD_REAP_CLIENT_FD;
356
357 return -1;
358}
359
cc4c0832 360/* Implementations of the commands and their callbacks */
ef6e34ee
DE
361
362/*
363 * lxc_cmd_get_init_pid: Get pid of the container's init process
364 *
365 * @name : name of container to connect to
366 * @lxcpath : the lxcpath in which the container is running
367 *
368 * Returns the pid on success, < 0 on failure
369 */
370pid_t lxc_cmd_get_init_pid(const char *name, const char *lxcpath)
43eb6f29 371{
0ecf64b5 372 int ret, stopped;
565eb353 373 pid_t pid = -1;
ef6e34ee 374 struct lxc_cmd_rr cmd = {
e8cd1208
CB
375 .req = {
376 .cmd = LXC_CMD_GET_INIT_PID
377 },
378 .rsp = {
565eb353 379 .data = PID_TO_PTR(pid)
e8cd1208 380 }
ef6e34ee
DE
381 };
382
88556fd7 383 ret = lxc_cmd(name, &cmd, &stopped, lxcpath, NULL);
ef6e34ee 384 if (ret < 0)
8ed8a626 385 return -1;
ef6e34ee 386
565eb353 387 pid = PTR_TO_PID(cmd.rsp.data);
9234406b
CB
388 if (pid < 0)
389 return -1;
390
391 /* We need to assume that pid_t can actually hold any pid given to us
392 * by the kernel. If it can't it's a libc bug.
393 */
394 return (pid_t)pid;
43eb6f29
DL
395}
396
ef6e34ee 397static int lxc_cmd_get_init_pid_callback(int fd, struct lxc_cmd_req *req,
cdb2a47f
CB
398 struct lxc_handler *handler,
399 struct lxc_epoll_descr *descr)
43eb6f29 400{
ea2a070b 401 int ret;
9234406b 402 struct lxc_cmd_rsp rsp = {
565eb353 403 .data = PID_TO_PTR(handler->pid)
9234406b 404 };
ef6e34ee 405
ea2a070b
CB
406 ret = lxc_cmd_rsp_send(fd, &rsp);
407 if (ret < 0)
408 return LXC_CMD_REAP_CLIENT_FD;
409
410 return 0;
43eb6f29
DL
411}
412
746aab51
CB
413int lxc_cmd_get_init_pidfd(const char *name, const char *lxcpath)
414{
415 int ret, stopped;
416 struct lxc_cmd_rr cmd = {
417 .req = {
418 .cmd = LXC_CMD_GET_INIT_PIDFD,
419 },
420 };
421
422 ret = lxc_cmd(name, &cmd, &stopped, lxcpath, NULL);
423 if (ret < 0)
424 return log_debug_errno(-1, errno, "Failed to process init pidfd command");
425
426 if (cmd.rsp.ret < 0)
427 return log_debug_errno(-EBADF, errno, "Failed to receive init pidfd");
428
429 return PTR_TO_INT(cmd.rsp.data);
430}
431
432static int lxc_cmd_get_init_pidfd_callback(int fd, struct lxc_cmd_req *req,
433 struct lxc_handler *handler,
434 struct lxc_epoll_descr *descr)
435{
436 struct lxc_cmd_rsp rsp = {
437 .ret = 0,
438 };
439 int ret;
440
441 if (handler->pidfd < 0)
442 rsp.ret = -EBADF;
443 ret = lxc_abstract_unix_send_fds(fd, &handler->pidfd, 1, &rsp, sizeof(rsp));
444 if (ret < 0)
445 return log_error(LXC_CMD_REAP_CLIENT_FD, "Failed to send init pidfd");
446
447 return 0;
448}
449
ef6e34ee
DE
450/*
451 * lxc_cmd_get_clone_flags: Get clone flags container was spawned with
452 *
453 * @name : name of container to connect to
454 * @lxcpath : the lxcpath in which the container is running
455 *
456 * Returns the clone flags on success, < 0 on failure
457 */
458int lxc_cmd_get_clone_flags(const char *name, const char *lxcpath)
459{
0ecf64b5 460 int ret, stopped;
ef6e34ee 461 struct lxc_cmd_rr cmd = {
6c6497ea
CB
462 .req = {
463 .cmd = LXC_CMD_GET_CLONE_FLAGS,
464 },
ef6e34ee
DE
465 };
466
88556fd7 467 ret = lxc_cmd(name, &cmd, &stopped, lxcpath, NULL);
ef6e34ee
DE
468 if (ret < 0)
469 return ret;
43eb6f29 470
ef6e34ee
DE
471 return PTR_TO_INT(cmd.rsp.data);
472}
473
474static int lxc_cmd_get_clone_flags_callback(int fd, struct lxc_cmd_req *req,
cdb2a47f
CB
475 struct lxc_handler *handler,
476 struct lxc_epoll_descr *descr)
26b2d152 477{
ea2a070b 478 int ret;
6c6497ea
CB
479 struct lxc_cmd_rsp rsp = {
480 .data = INT_TO_PTR(handler->ns_clone_flags),
481 };
ef6e34ee 482
ea2a070b
CB
483 ret = lxc_cmd_rsp_send(fd, &rsp);
484 if (ret < 0)
485 return LXC_CMD_REAP_CLIENT_FD;
486
487 return 0;
ef6e34ee
DE
488}
489
a900cbaf
WB
490static char *lxc_cmd_get_cgroup_path_do(const char *name, const char *lxcpath,
491 const char *subsystem,
492 lxc_cmd_t command)
ef6e34ee 493{
0ecf64b5 494 int ret, stopped;
ef6e34ee 495 struct lxc_cmd_rr cmd = {
b98f7d6e 496 .req = {
a900cbaf 497 .cmd = command,
b98f7d6e 498 .data = subsystem,
c2aed66d 499 .datalen = 0,
b98f7d6e 500 },
26b2d152
MN
501 };
502
c2aed66d
CB
503 cmd.req.data = subsystem;
504 cmd.req.datalen = 0;
505 if (subsystem)
506 cmd.req.datalen = strlen(subsystem) + 1;
507
88556fd7 508 ret = lxc_cmd(name, &cmd, &stopped, lxcpath, NULL);
fe84a562 509 if (ret < 0)
ef6e34ee
DE
510 return NULL;
511
fe84a562 512 if (ret == 0)
ef6e34ee 513 return NULL;
ef6e34ee 514
fe84a562 515 if (cmd.rsp.ret < 0 || cmd.rsp.datalen < 0)
ef6e34ee 516 return NULL;
3f903c04 517
ef6e34ee
DE
518 return cmd.rsp.data;
519}
520
a900cbaf
WB
521/*
522 * lxc_cmd_get_cgroup_path: Calculate a container's cgroup path for a
523 * particular subsystem. This is the cgroup path relative to the root
524 * of the cgroup filesystem.
525 *
526 * @name : name of container to connect to
527 * @lxcpath : the lxcpath in which the container is running
528 * @subsystem : the subsystem being asked about
529 *
530 * Returns the path on success, NULL on failure. The caller must free() the
531 * returned path.
532 */
533char *lxc_cmd_get_cgroup_path(const char *name, const char *lxcpath,
534 const char *subsystem)
535{
536 return lxc_cmd_get_cgroup_path_do(name, lxcpath, subsystem,
537 LXC_CMD_GET_CGROUP);
538}
539
540/*
541 * lxc_cmd_get_limiting_cgroup_path: Calculate a container's limiting cgroup
542 * path for a particular subsystem. This is the cgroup path relative to the
543 * root of the cgroup filesystem. This may be the same as the path returned by
544 * lxc_cmd_get_cgroup_path if the container doesn't have a limiting path prefix
545 * set.
546 *
547 * @name : name of container to connect to
548 * @lxcpath : the lxcpath in which the container is running
549 * @subsystem : the subsystem being asked about
550 *
551 * Returns the path on success, NULL on failure. The caller must free() the
552 * returned path.
553 */
554char *lxc_cmd_get_limiting_cgroup_path(const char *name, const char *lxcpath,
555 const char *subsystem)
556{
557 return lxc_cmd_get_cgroup_path_do(name, lxcpath, subsystem,
558 LXC_CMD_GET_LIMITING_CGROUP);
559}
560
561static int lxc_cmd_get_cgroup_callback_do(int fd, struct lxc_cmd_req *req,
562 struct lxc_handler *handler,
563 struct lxc_epoll_descr *descr,
564 bool limiting_cgroup)
ef6e34ee 565{
ea2a070b 566 int ret;
4fb3cba5 567 const char *path;
a900cbaf 568 const void *reqdata;
fe84a562 569 struct lxc_cmd_rsp rsp;
2202afc9 570 struct cgroup_ops *cgroup_ops = handler->cgroup_ops;
a900cbaf 571 const char *(*get_fn)(struct cgroup_ops *ops, const char *controller);
b98f7d6e 572
e6bc68d6
WB
573 if (req->datalen > 0) {
574 ret = validate_string_request(fd, req);
575 if (ret != 0)
576 return ret;
a900cbaf 577 reqdata = req->data;
e6bc68d6 578 } else {
a900cbaf 579 reqdata = NULL;
e6bc68d6 580 }
a900cbaf
WB
581
582 get_fn = (limiting_cgroup ? cgroup_ops->get_cgroup
583 : cgroup_ops->get_limiting_cgroup);
584
585 path = get_fn(cgroup_ops, reqdata);
586
b98f7d6e
SH
587 if (!path)
588 return -1;
fe84a562 589
4f17323e 590 rsp.ret = 0;
fe84a562
CB
591 rsp.datalen = strlen(path) + 1;
592 rsp.data = (char *)path;
ef6e34ee 593
ea2a070b
CB
594 ret = lxc_cmd_rsp_send(fd, &rsp);
595 if (ret < 0)
596 return LXC_CMD_REAP_CLIENT_FD;
597
598 return 0;
ef6e34ee
DE
599}
600
a900cbaf
WB
601static int lxc_cmd_get_cgroup_callback(int fd, struct lxc_cmd_req *req,
602 struct lxc_handler *handler,
603 struct lxc_epoll_descr *descr)
604{
605 return lxc_cmd_get_cgroup_callback_do(fd, req, handler, descr, false);
606}
607
608static int lxc_cmd_get_limiting_cgroup_callback(int fd, struct lxc_cmd_req *req,
609 struct lxc_handler *handler,
610 struct lxc_epoll_descr *descr)
611{
612 return lxc_cmd_get_cgroup_callback_do(fd, req, handler, descr, true);
613}
614
ef6e34ee
DE
615/*
616 * lxc_cmd_get_config_item: Get config item the running container
617 *
618 * @name : name of container to connect to
7fa3f2e9 619 * @item : the configuration item to retrieve (ex: lxc.net.0.veth.pair)
ef6e34ee
DE
620 * @lxcpath : the lxcpath in which the container is running
621 *
622 * Returns the item on success, NULL on failure. The caller must free() the
623 * returned item.
624 */
625char *lxc_cmd_get_config_item(const char *name, const char *item,
626 const char *lxcpath)
627{
0ecf64b5 628 int ret, stopped;
ef6e34ee
DE
629 struct lxc_cmd_rr cmd = {
630 .req = { .cmd = LXC_CMD_GET_CONFIG_ITEM,
631 .data = item,
fe84a562 632 .datalen = strlen(item) + 1,
ef6e34ee
DE
633 },
634 };
635
88556fd7 636 ret = lxc_cmd(name, &cmd, &stopped, lxcpath, NULL);
ef6e34ee
DE
637 if (ret < 0)
638 return NULL;
639
640 if (cmd.rsp.ret == 0)
641 return cmd.rsp.data;
fe84a562 642
ef6e34ee
DE
643 return NULL;
644}
645
646static int lxc_cmd_get_config_item_callback(int fd, struct lxc_cmd_req *req,
cdb2a47f
CB
647 struct lxc_handler *handler,
648 struct lxc_epoll_descr *descr)
ef6e34ee 649{
5265a60c 650 __do_free char *cidata = NULL;
ef6e34ee 651 int cilen;
30aec088 652 struct lxc_config_t *item;
fe84a562 653 struct lxc_cmd_rsp rsp;
ef6e34ee
DE
654
655 memset(&rsp, 0, sizeof(rsp));
300df83e 656 item = lxc_get_config(req->data);
30aec088
CB
657 if (!item)
658 goto err1;
fe84a562 659
cccd2219 660 cilen = item->get(req->data, NULL, 0, handler->conf, NULL);
ef6e34ee
DE
661 if (cilen <= 0)
662 goto err1;
663
5265a60c 664 cidata = must_realloc(NULL, cilen + 1);
cccd2219 665 if (item->get(req->data, cidata, cilen + 1, handler->conf, NULL) != cilen)
ef6e34ee 666 goto err1;
fe84a562 667
ef6e34ee
DE
668 cidata[cilen] = '\0';
669 rsp.data = cidata;
670 rsp.datalen = cilen + 1;
671 rsp.ret = 0;
672 goto out;
673
674err1:
675 rsp.ret = -1;
676out:
ea2a070b
CB
677 cilen = lxc_cmd_rsp_send(fd, &rsp);
678 if (cilen < 0)
679 return LXC_CMD_REAP_CLIENT_FD;
680
681 return 0;
ef6e34ee
DE
682}
683
684/*
685 * lxc_cmd_get_state: Get current state of the container
686 *
687 * @name : name of container to connect to
688 * @lxcpath : the lxcpath in which the container is running
689 *
690 * Returns the state on success, < 0 on failure
691 */
dbc9832d 692int lxc_cmd_get_state(const char *name, const char *lxcpath)
ef6e34ee 693{
0ecf64b5 694 int ret, stopped;
ef6e34ee 695 struct lxc_cmd_rr cmd = {
6c6497ea
CB
696 .req = {
697 .cmd = LXC_CMD_GET_STATE,
698 },
ef6e34ee 699 };
26b2d152 700
88556fd7 701 ret = lxc_cmd(name, &cmd, &stopped, lxcpath, NULL);
ebdd307d 702 if (ret < 0 && stopped)
ef6e34ee
DE
703 return STOPPED;
704
705 if (ret < 0)
26b2d152 706 return -1;
26b2d152 707
6c6497ea
CB
708 if (!ret)
709 return log_warn(-1, "Container \"%s\" has stopped before sending its state", name);
fe84a562 710
6c6497ea
CB
711 return log_debug(PTR_TO_INT(cmd.rsp.data),
712 "Container \"%s\" is in \"%s\" state", name,
713 lxc_state2str(PTR_TO_INT(cmd.rsp.data)));
ef6e34ee
DE
714}
715
716static int lxc_cmd_get_state_callback(int fd, struct lxc_cmd_req *req,
cdb2a47f
CB
717 struct lxc_handler *handler,
718 struct lxc_epoll_descr *descr)
ef6e34ee 719{
ea2a070b 720 int ret;
6c6497ea
CB
721 struct lxc_cmd_rsp rsp = {
722 .data = INT_TO_PTR(handler->state),
723 };
ef6e34ee 724
ea2a070b
CB
725 ret = lxc_cmd_rsp_send(fd, &rsp);
726 if (ret < 0)
727 return LXC_CMD_REAP_CLIENT_FD;
728
729 return 0;
ef6e34ee
DE
730}
731
732/*
733 * lxc_cmd_stop: Stop the container previously started with lxc_start. All
734 * the processes running inside this container will be killed.
735 *
736 * @name : name of container to connect to
737 * @lxcpath : the lxcpath in which the container is running
738 *
739 * Returns 0 on success, < 0 on failure
740 */
741int lxc_cmd_stop(const char *name, const char *lxcpath)
742{
0ecf64b5 743 int ret, stopped;
ef6e34ee 744 struct lxc_cmd_rr cmd = {
6c6497ea
CB
745 .req = {
746 .cmd = LXC_CMD_STOP,
747 },
ef6e34ee
DE
748 };
749
88556fd7 750 ret = lxc_cmd(name, &cmd, &stopped, lxcpath, NULL);
26b2d152 751 if (ret < 0) {
6c6497ea
CB
752 if (stopped)
753 return log_info(0, "Container \"%s\" is already stopped", name);
fe84a562 754
26b2d152
MN
755 return -1;
756 }
757
fe84a562
CB
758 /* We do not expect any answer, because we wait for the connection to be
759 * closed.
95d5b147 760 */
6c6497ea
CB
761 if (ret > 0)
762 return log_error_errno(-1, -cmd.rsp.ret, "Failed to stop container \"%s\"", name);
26b2d152 763
6c6497ea 764 return log_info(0, "Container \"%s\" has stopped", name);
26b2d152
MN
765}
766
ef6e34ee 767static int lxc_cmd_stop_callback(int fd, struct lxc_cmd_req *req,
cdb2a47f
CB
768 struct lxc_handler *handler,
769 struct lxc_epoll_descr *descr)
d5088cf2 770{
ef6e34ee 771 struct lxc_cmd_rsp rsp;
ef6e34ee 772 int stopsignal = SIGKILL;
2202afc9 773 struct cgroup_ops *cgroup_ops = handler->cgroup_ops;
ea2a070b 774 int ret;
ef6e34ee
DE
775
776 if (handler->conf->stopsignal)
777 stopsignal = handler->conf->stopsignal;
778 memset(&rsp, 0, sizeof(rsp));
9837ee46 779
8ad4fa68 780 if (handler->pidfd >= 0)
9837ee46
CB
781 rsp.ret = lxc_raw_pidfd_send_signal(handler->pidfd, stopsignal, NULL, 0);
782 else
783 rsp.ret = kill(handler->pid, stopsignal);
ef6e34ee 784 if (!rsp.ret) {
9837ee46
CB
785 if (handler->pidfd >= 0)
786 TRACE("Sent signal %d to pidfd %d", stopsignal, handler->pidfd);
787 else
788 TRACE("Sent signal %d to pidfd %d", stopsignal, handler->pid);
789
018051e3
CB
790 rsp.ret = cgroup_ops->unfreeze(cgroup_ops, -1);
791 if (!rsp.ret)
95d5b147 792 return 0;
fe84a562
CB
793
794 ERROR("Failed to unfreeze container \"%s\"", handler->name);
018051e3
CB
795 rsp.ret = -errno;
796 } else {
797 rsp.ret = -errno;
ef6e34ee
DE
798 }
799
ea2a070b
CB
800 ret = lxc_cmd_rsp_send(fd, &rsp);
801 if (ret < 0)
802 return LXC_CMD_REAP_CLIENT_FD;
803
804 return 0;
ef6e34ee 805}
d5088cf2 806
b5159817 807/*
2bd158cc 808 * lxc_cmd_terminal_winch: noop
b5159817
DE
809 *
810 * @name : name of container to connect to
811 * @lxcpath : the lxcpath in which the container is running
812 *
813 * Returns 0 on success, < 0 on failure
814 */
8ccbbf94 815int lxc_cmd_terminal_winch(const char *name, const char *lxcpath)
b5159817 816{
b5159817
DE
817 return 0;
818}
819
8ccbbf94 820static int lxc_cmd_terminal_winch_callback(int fd, struct lxc_cmd_req *req,
cdb2a47f
CB
821 struct lxc_handler *handler,
822 struct lxc_epoll_descr *descr)
b5159817 823{
2bd158cc 824 /* should never be called */
6c6497ea 825 return log_error_errno(-1, ENOSYS, "Called lxc_cmd_terminal_winch_callback()");
b5159817
DE
826}
827
ef6e34ee
DE
828/*
829 * lxc_cmd_console: Open an fd to a tty in the container
830 *
831 * @name : name of container to connect to
832 * @ttynum : in: the tty to open or -1 for next available
833 * : out: the tty allocated
834 * @fd : out: file descriptor for master side of pty
835 * @lxcpath : the lxcpath in which the container is running
836 *
0115f8fd 837 * Returns fd holding tty allocated on success, < 0 on failure
ef6e34ee
DE
838 */
839int lxc_cmd_console(const char *name, int *ttynum, int *fd, const char *lxcpath)
840{
8259d86d 841 __do_free struct lxc_cmd_console_rsp_data *rspdata = NULL;
0ecf64b5 842 int ret, stopped;
ef6e34ee 843 struct lxc_cmd_rr cmd = {
6c6497ea
CB
844 .req = {
845 .cmd = LXC_CMD_CONSOLE,
846 .data = INT_TO_PTR(*ttynum),
847 },
ef6e34ee 848 };
d5088cf2 849
88556fd7 850 ret = lxc_cmd(name, &cmd, &stopped, lxcpath, NULL);
ef6e34ee
DE
851 if (ret < 0)
852 return ret;
0115f8fd 853
8259d86d 854 rspdata = cmd.rsp.data;
6c6497ea
CB
855 if (cmd.rsp.ret < 0)
856 return log_error_errno(-1, -cmd.rsp.ret, "Denied access to tty");
d5088cf2 857
6c6497ea
CB
858 if (ret == 0)
859 return log_error(-1, "tty number %d invalid, busy or all ttys busy", *ttynum);
ef6e34ee 860
6c6497ea
CB
861 if (rspdata->masterfd < 0)
862 return log_error(-1, "Unable to allocate fd for tty %d", rspdata->ttynum);
ef6e34ee 863
fe84a562 864 ret = cmd.rsp.ret; /* socket fd */
0115f8fd 865 *fd = rspdata->masterfd;
ef6e34ee 866 *ttynum = rspdata->ttynum;
fe84a562 867
6c6497ea 868 return log_info(ret, "Alloced fd %d for tty %d via socket %d", *fd, rspdata->ttynum, ret);
ef6e34ee
DE
869}
870
871static int lxc_cmd_console_callback(int fd, struct lxc_cmd_req *req,
cdb2a47f
CB
872 struct lxc_handler *handler,
873 struct lxc_epoll_descr *descr)
ef6e34ee 874{
fe84a562 875 int masterfd, ret;
ef6e34ee 876 struct lxc_cmd_rsp rsp;
fe84a562 877 int ttynum = PTR_TO_INT(req->data);
ef6e34ee 878
c1ee47cd 879 masterfd = lxc_terminal_allocate(handler->conf, fd, &ttynum);
b5159817 880 if (masterfd < 0)
ea2a070b 881 return LXC_CMD_REAP_CLIENT_FD;
ef6e34ee 882
ef6e34ee
DE
883 memset(&rsp, 0, sizeof(rsp));
884 rsp.data = INT_TO_PTR(ttynum);
fe84a562
CB
885 ret = lxc_abstract_unix_send_fds(fd, &masterfd, 1, &rsp, sizeof(rsp));
886 if (ret < 0) {
3dfe6f8d 887 lxc_terminal_free(handler->conf, fd);
ea2a070b
CB
888 return log_error_errno(LXC_CMD_REAP_CLIENT_FD, errno,
889 "Failed to send tty to client");
ef6e34ee
DE
890 }
891
ef6e34ee 892 return 0;
d5088cf2
CS
893}
894
88556fd7
ÇO
895/*
896 * lxc_cmd_get_name: Returns the name of the container
897 *
898 * @hashed_sock_name: hashed socket name
899 *
900 * Returns the name on success, NULL on failure.
901 */
902char *lxc_cmd_get_name(const char *hashed_sock_name)
903{
904 int ret, stopped;
905 struct lxc_cmd_rr cmd = {
6c6497ea
CB
906 .req = {
907 .cmd = LXC_CMD_GET_NAME,
908 },
88556fd7
ÇO
909 };
910
911 ret = lxc_cmd(NULL, &cmd, &stopped, NULL, hashed_sock_name);
fe84a562 912 if (ret < 0)
88556fd7 913 return NULL;
88556fd7
ÇO
914
915 if (cmd.rsp.ret == 0)
916 return cmd.rsp.data;
fe84a562 917
88556fd7
ÇO
918 return NULL;
919}
920
921static int lxc_cmd_get_name_callback(int fd, struct lxc_cmd_req *req,
cdb2a47f
CB
922 struct lxc_handler *handler,
923 struct lxc_epoll_descr *descr)
88556fd7 924{
ea2a070b 925 int ret;
88556fd7
ÇO
926 struct lxc_cmd_rsp rsp;
927
928 memset(&rsp, 0, sizeof(rsp));
929
f0ecc19d 930 rsp.data = (char *)handler->name;
88556fd7
ÇO
931 rsp.datalen = strlen(handler->name) + 1;
932 rsp.ret = 0;
933
ea2a070b
CB
934 ret = lxc_cmd_rsp_send(fd, &rsp);
935 if (ret < 0)
936 return LXC_CMD_REAP_CLIENT_FD;
937
938 return 0;
88556fd7
ÇO
939}
940
941/*
942 * lxc_cmd_get_lxcpath: Returns the lxcpath of the container
943 *
944 * @hashed_sock_name: hashed socket name
945 *
946 * Returns the lxcpath on success, NULL on failure.
947 */
948char *lxc_cmd_get_lxcpath(const char *hashed_sock_name)
949{
950 int ret, stopped;
951 struct lxc_cmd_rr cmd = {
6c6497ea
CB
952 .req = {
953 .cmd = LXC_CMD_GET_LXCPATH,
954 },
88556fd7 955 };
724e753c 956
88556fd7 957 ret = lxc_cmd(NULL, &cmd, &stopped, NULL, hashed_sock_name);
fe84a562 958 if (ret < 0)
88556fd7 959 return NULL;
88556fd7
ÇO
960
961 if (cmd.rsp.ret == 0)
962 return cmd.rsp.data;
fe84a562 963
88556fd7
ÇO
964 return NULL;
965}
966
967static int lxc_cmd_get_lxcpath_callback(int fd, struct lxc_cmd_req *req,
cdb2a47f
CB
968 struct lxc_handler *handler,
969 struct lxc_epoll_descr *descr)
88556fd7 970{
ea2a070b 971 int ret;
6c6497ea
CB
972 struct lxc_cmd_rsp rsp = {
973 .ret = 0,
974 .data = (char *)handler->lxcpath,
975 .datalen = strlen(handler->lxcpath) + 1,
976 };
88556fd7 977
ea2a070b
CB
978 ret = lxc_cmd_rsp_send(fd, &rsp);
979 if (ret < 0)
980 return LXC_CMD_REAP_CLIENT_FD;
981
982 return 0;
88556fd7 983}
ef6e34ee 984
54446942 985int lxc_cmd_add_state_client(const char *name, const char *lxcpath,
92e35018
CB
986 lxc_state_t states[MAX_STATE],
987 int *state_client_fd)
dbc9832d 988{
f62cf1d4 989 __do_close int clientfd = -EBADF;
bc631984 990 int state, stopped;
dbc9832d 991 ssize_t ret;
dbc9832d 992 struct lxc_cmd_rr cmd = {
6c6497ea
CB
993 .req = {
994 .cmd = LXC_CMD_ADD_STATE_CLIENT,
995 .data = states,
996 .datalen = (sizeof(lxc_state_t) * MAX_STATE)
997 },
dbc9832d
CB
998 };
999
dbc9832d 1000 ret = lxc_cmd(name, &cmd, &stopped, lxcpath, NULL);
bc631984
CB
1001 if (states[STOPPED] != 0 && stopped != 0)
1002 return STOPPED;
1003
dbc9832d 1004 if (ret < 0) {
f577e061 1005 if (errno != ECONNREFUSED)
6d1400b5 1006 SYSERROR("Failed to execute command");
1007
dbc9832d
CB
1008 return -1;
1009 }
fe84a562 1010
dbc9832d
CB
1011 /* We should now be guaranteed to get an answer from the state sending
1012 * function.
1013 */
cd889e57 1014 clientfd = cmd.rsp.ret;
6c6497ea
CB
1015 if (clientfd < 0)
1016 return log_error_errno(-1, -clientfd, "Failed to receive socket fd");
dbc9832d 1017
bc631984 1018 state = PTR_TO_INT(cmd.rsp.data);
6c6497ea
CB
1019 if (state < MAX_STATE)
1020 return log_trace(state, "Container is already in requested state %s", lxc_state2str(state));
bc631984 1021
240fecd0 1022 *state_client_fd = move_fd(clientfd);
ea2a070b
CB
1023 TRACE("State connection fd %d ready to listen for container state changes", *state_client_fd);
1024 return MAX_STATE;
dbc9832d
CB
1025}
1026
ebbca852 1027static int lxc_cmd_add_state_client_callback(__owns int fd, struct lxc_cmd_req *req,
cdb2a47f
CB
1028 struct lxc_handler *handler,
1029 struct lxc_epoll_descr *descr)
dbc9832d 1030{
44552fb2 1031 int ret;
dbc9832d 1032 struct lxc_cmd_rsp rsp = {0};
dbc9832d 1033
fe84a562 1034 if (req->datalen < 0)
ea2a070b 1035 return LXC_CMD_REAP_CLIENT_FD;
dbc9832d 1036
71fc9c04 1037 if (req->datalen != (sizeof(lxc_state_t) * MAX_STATE))
ea2a070b 1038 return LXC_CMD_REAP_CLIENT_FD;
dbc9832d 1039
fe84a562 1040 if (!req->data)
ea2a070b 1041 return LXC_CMD_REAP_CLIENT_FD;
dbc9832d 1042
c01c2be6 1043 rsp.ret = lxc_add_state_client(fd, handler, (lxc_state_t *)req->data);
44552fb2 1044 if (rsp.ret < 0)
ea2a070b 1045 return LXC_CMD_REAP_CLIENT_FD;
44552fb2 1046
bc631984 1047 rsp.data = INT_TO_PTR(rsp.ret);
dbc9832d 1048
44552fb2
CB
1049 ret = lxc_cmd_rsp_send(fd, &rsp);
1050 if (ret < 0)
ea2a070b 1051 return LXC_CMD_REAP_CLIENT_FD;
44552fb2
CB
1052
1053 return 0;
dbc9832d
CB
1054}
1055
2a63b5cb
CB
1056int lxc_cmd_add_bpf_device_cgroup(const char *name, const char *lxcpath,
1057 struct device_item *device)
1058{
1059#ifdef HAVE_STRUCT_BPF_CGROUP_DEV_CTX
1060 int stopped = 0;
1061 struct lxc_cmd_rr cmd = {
6c6497ea
CB
1062 .req = {
1063 .cmd = LXC_CMD_ADD_BPF_DEVICE_CGROUP,
1064 .data = device,
1065 .datalen = sizeof(struct device_item),
1066 },
2a63b5cb
CB
1067 };
1068 int ret;
1069
1070 if (strlen(device->access) > STRLITERALLEN("rwm"))
3d0327ed 1071 return log_error_errno(-1, EINVAL, "Invalid access mode specified %s",
2a63b5cb
CB
1072 device->access);
1073
1074 ret = lxc_cmd(name, &cmd, &stopped, lxcpath, NULL);
1075 if (ret < 0 || cmd.rsp.ret < 0)
3d0327ed 1076 return log_error_errno(-1, errno, "Failed to add new bpf device cgroup rule");
2a63b5cb
CB
1077
1078 return 0;
1079#else
3d0327ed 1080 return ret_set_errno(-1, ENOSYS);
2a63b5cb
CB
1081#endif
1082}
1083
1084static int lxc_cmd_add_bpf_device_cgroup_callback(int fd, struct lxc_cmd_req *req,
1085 struct lxc_handler *handler,
1086 struct lxc_epoll_descr *descr)
1087{
1088#ifdef HAVE_STRUCT_BPF_CGROUP_DEV_CTX
1089 __do_bpf_program_free struct bpf_program *devices = NULL;
1090 struct lxc_cmd_rsp rsp = {0};
1091 struct lxc_conf *conf = handler->conf;
1092 struct hierarchy *unified = handler->cgroup_ops->unified;
2a63b5cb
CB
1093 int ret;
1094 struct lxc_list *it;
1095 struct device_item *device;
1096 struct bpf_program *devices_old;
1097
1098 if (req->datalen <= 0)
ea2a070b 1099 return LXC_CMD_REAP_CLIENT_FD;
2a63b5cb
CB
1100
1101 if (req->datalen != sizeof(struct device_item))
ea2a070b 1102 return LXC_CMD_REAP_CLIENT_FD;
2a63b5cb
CB
1103
1104 if (!req->data)
ea2a070b 1105 return LXC_CMD_REAP_CLIENT_FD;
2a63b5cb
CB
1106 device = (struct device_item *)req->data;
1107
1108 rsp.ret = -1;
1109 if (!unified)
1110 goto respond;
1111
1112 ret = bpf_list_add_device(conf, device);
1113 if (ret < 0)
1114 goto respond;
1115
1116 devices = bpf_program_new(BPF_PROG_TYPE_CGROUP_DEVICE);
1117 if (!devices)
1118 goto respond;
1119
1120 ret = bpf_program_init(devices);
1121 if (ret)
1122 goto respond;
1123
1124 lxc_list_for_each(it, &conf->devices) {
1125 struct device_item *cur = it->elem;
1126
1127 ret = bpf_program_append_device(devices, cur);
1128 if (ret)
1129 goto respond;
1130 }
1131
1132 ret = bpf_program_finalize(devices);
1133 if (ret)
1134 goto respond;
1135
1136 ret = bpf_program_cgroup_attach(devices, BPF_CGROUP_DEVICE,
1137 unified->container_full_path,
1138 BPF_F_ALLOW_MULTI);
1139 if (ret)
1140 goto respond;
1141
1142 /* Replace old bpf program. */
1143 devices_old = move_ptr(conf->cgroup2_devices);
1144 conf->cgroup2_devices = move_ptr(devices);
1145 devices = move_ptr(devices_old);
1146
1147 rsp.ret = 0;
1148
1149respond:
1150 ret = lxc_cmd_rsp_send(fd, &rsp);
1151 if (ret < 0)
ea2a070b 1152 return LXC_CMD_REAP_CLIENT_FD;
2a63b5cb
CB
1153
1154 return 0;
2a63b5cb 1155#else
3d0327ed 1156 return ret_set_errno(-1, ENOSYS);
2a63b5cb
CB
1157#endif
1158}
1159
191d43cc
CB
1160int lxc_cmd_console_log(const char *name, const char *lxcpath,
1161 struct lxc_console_log *log)
1162{
1163 int ret, stopped;
1164 struct lxc_cmd_console_log data;
1165 struct lxc_cmd_rr cmd;
1166
1167 data.clear = log->clear;
1168 data.read = log->read;
1169 data.read_max = *log->read_max;
1170
1171 cmd.req.cmd = LXC_CMD_CONSOLE_LOG;
1172 cmd.req.data = &data;
1173 cmd.req.datalen = sizeof(struct lxc_cmd_console_log);
1174
1175 ret = lxc_cmd(name, &cmd, &stopped, lxcpath, NULL);
1176 if (ret < 0)
1177 return ret;
1178
1179 /* There is nothing to be read from the buffer. So clear any values we
1180 * where passed to clearly indicate to the user that nothing went wrong.
1181 */
63b74cda 1182 if (cmd.rsp.ret == -ENODATA || cmd.rsp.ret == -EFAULT || cmd.rsp.ret == -ENOENT) {
191d43cc
CB
1183 *log->read_max = 0;
1184 log->data = NULL;
1185 }
1186
1187 /* This is a proper error so don't touch any values we were passed. */
1188 if (cmd.rsp.ret < 0)
1189 return cmd.rsp.ret;
1190
1191 *log->read_max = cmd.rsp.datalen;
1192 log->data = cmd.rsp.data;
1193
1194 return 0;
1195}
1196
1197static int lxc_cmd_console_log_callback(int fd, struct lxc_cmd_req *req,
cdb2a47f
CB
1198 struct lxc_handler *handler,
1199 struct lxc_epoll_descr *descr)
191d43cc
CB
1200{
1201 struct lxc_cmd_rsp rsp;
28f3b1cd 1202 uint64_t buffer_size = handler->conf->console.buffer_size;
191d43cc
CB
1203 const struct lxc_cmd_console_log *log = req->data;
1204 struct lxc_ringbuf *buf = &handler->conf->console.ringbuf;
1205
1206 rsp.ret = -EFAULT;
1207 rsp.datalen = 0;
1208 rsp.data = NULL;
28f3b1cd 1209 if (buffer_size <= 0)
191d43cc
CB
1210 goto out;
1211
5928191e
CB
1212 if (log->read || log->write_logfile)
1213 rsp.datalen = lxc_ringbuf_used(buf);
1214
191d43cc
CB
1215 if (log->read)
1216 rsp.data = lxc_ringbuf_get_read_addr(buf);
1217
1218 if (log->read_max > 0 && (log->read_max <= rsp.datalen))
1219 rsp.datalen = log->read_max;
1220
1221 /* there's nothing to read */
63b74cda 1222 rsp.ret = -ENODATA;
191d43cc 1223 if (log->read && (buf->r_off == buf->w_off))
63b74cda
CB
1224 goto out;
1225
63b74cda 1226 rsp.ret = 0;
23e0d9af 1227 if (log->clear)
43366ca2 1228 lxc_ringbuf_clear(buf); /* clear the ringbuffer */
23e0d9af 1229 else if (rsp.datalen > 0)
191d43cc
CB
1230 lxc_ringbuf_move_read_addr(buf, rsp.datalen);
1231
1232out:
1233 return lxc_cmd_rsp_send(fd, &rsp);
1234}
1235
974a8aba
CB
1236int lxc_cmd_serve_state_clients(const char *name, const char *lxcpath,
1237 lxc_state_t state)
1238{
1239 int stopped;
1240 ssize_t ret;
1241 struct lxc_cmd_rr cmd = {
6c6497ea
CB
1242 .req = {
1243 .cmd = LXC_CMD_SERVE_STATE_CLIENTS,
1244 .data = INT_TO_PTR(state)
1245 },
974a8aba
CB
1246 };
1247
1248 ret = lxc_cmd(name, &cmd, &stopped, lxcpath, NULL);
6c6497ea
CB
1249 if (ret < 0)
1250 return log_error_errno(-1, errno, "Failed to serve state clients");
974a8aba
CB
1251
1252 return 0;
1253}
1254
1255static int lxc_cmd_serve_state_clients_callback(int fd, struct lxc_cmd_req *req,
cdb2a47f
CB
1256 struct lxc_handler *handler,
1257 struct lxc_epoll_descr *descr)
974a8aba
CB
1258{
1259 int ret;
1260 lxc_state_t state = PTR_TO_INT(req->data);
1261 struct lxc_cmd_rsp rsp = {0};
1262
1263 ret = lxc_serve_state_clients(handler->name, handler, state);
1264 if (ret < 0)
ea2a070b 1265 return LXC_CMD_REAP_CLIENT_FD;
974a8aba
CB
1266
1267 ret = lxc_cmd_rsp_send(fd, &rsp);
1268 if (ret < 0)
ea2a070b 1269 return LXC_CMD_REAP_CLIENT_FD;
974a8aba
CB
1270
1271 return 0;
974a8aba
CB
1272}
1273
cdb2a47f
CB
1274int lxc_cmd_seccomp_notify_add_listener(const char *name, const char *lxcpath,
1275 int fd,
1276 /* unused */ unsigned int command,
1277 /* unused */ unsigned int flags)
1278{
1279
c3e3c21a 1280#ifdef HAVE_SECCOMP_NOTIFY
cdb2a47f
CB
1281 int ret, stopped;
1282 struct lxc_cmd_rr cmd = {
1283 .req = {
1284 .cmd = LXC_CMD_SECCOMP_NOTIFY_ADD_LISTENER,
1285 .data = INT_TO_PTR(fd),
1286 },
1287 };
1288
1289 ret = lxc_cmd(name, &cmd, &stopped, lxcpath, NULL);
6c6497ea
CB
1290 if (ret < 0)
1291 return log_error_errno(-1, errno, "Failed to add seccomp listener");
cdb2a47f
CB
1292
1293 return cmd.rsp.ret;
1294#else
3d0327ed 1295 return ret_set_errno(-1, ENOSYS);
cdb2a47f
CB
1296#endif
1297}
1298
1299static int lxc_cmd_seccomp_notify_add_listener_callback(int fd,
1300 struct lxc_cmd_req *req,
1301 struct lxc_handler *handler,
1302 struct lxc_epoll_descr *descr)
1303{
1304 struct lxc_cmd_rsp rsp = {0};
cdb2a47f 1305
c3e3c21a
CB
1306#ifdef HAVE_SECCOMP_NOTIFY
1307 int ret;
f62cf1d4 1308 __do_close int recv_fd = -EBADF;
cdb2a47f 1309
c3e3c21a
CB
1310 ret = lxc_abstract_unix_recv_fds(fd, &recv_fd, 1, NULL, 0);
1311 if (ret <= 0) {
1312 rsp.ret = -errno;
1313 goto out;
cdb2a47f
CB
1314 }
1315
c3e3c21a
CB
1316 if (!handler->conf->seccomp.notifier.wants_supervision ||
1317 handler->conf->seccomp.notifier.proxy_fd < 0) {
1318 SYSERROR("No seccomp proxy fd specified");
1319 rsp.ret = -EINVAL;
1320 goto out;
1321 }
cdb2a47f 1322
c3e3c21a 1323 ret = lxc_mainloop_add_handler(descr, recv_fd, seccomp_notify_handler,
cdb2a47f 1324 handler);
c3e3c21a
CB
1325 if (ret < 0) {
1326 rsp.ret = -errno;
1327 goto out;
1328 }
68a9e3eb 1329 move_fd(recv_fd);
cdb2a47f 1330
c3e3c21a 1331out:
cdb2a47f
CB
1332#else
1333 rsp.ret = -ENOSYS;
cdb2a47f 1334
c3e3c21a
CB
1335#endif
1336 return lxc_cmd_rsp_send(fd, &rsp);
cdb2a47f
CB
1337}
1338
018051e3
CB
1339int lxc_cmd_freeze(const char *name, const char *lxcpath, int timeout)
1340{
1341 int ret, stopped;
1342 struct lxc_cmd_rr cmd = {
1343 .req = {
1344 .cmd = LXC_CMD_FREEZE,
1345 .data = INT_TO_PTR(timeout),
1346 },
1347 };
1348
1349 ret = lxc_cmd(name, &cmd, &stopped, lxcpath, NULL);
1350 if (ret <= 0 || cmd.rsp.ret < 0)
3d0327ed 1351 return log_error_errno(-1, errno, "Failed to freeze container");
018051e3
CB
1352
1353 return cmd.rsp.ret;
1354}
1355
1356static int lxc_cmd_freeze_callback(int fd, struct lxc_cmd_req *req,
1357 struct lxc_handler *handler,
1358 struct lxc_epoll_descr *descr)
1359{
1360 int timeout = PTR_TO_INT(req->data);
1361 struct lxc_cmd_rsp rsp = {
6c6497ea 1362 .ret = -ENOENT,
018051e3
CB
1363 };
1364 struct cgroup_ops *ops = handler->cgroup_ops;
1365
1973b62a 1366 if (pure_unified_layout(ops))
018051e3
CB
1367 rsp.ret = ops->freeze(ops, timeout);
1368
1369 return lxc_cmd_rsp_send(fd, &rsp);
1370}
1371
1372int lxc_cmd_unfreeze(const char *name, const char *lxcpath, int timeout)
1373{
1374 int ret, stopped;
1375 struct lxc_cmd_rr cmd = {
1376 .req = {
1377 .cmd = LXC_CMD_UNFREEZE,
1378 .data = INT_TO_PTR(timeout),
1379 },
1380 };
1381
1382 ret = lxc_cmd(name, &cmd, &stopped, lxcpath, NULL);
1383 if (ret <= 0 || cmd.rsp.ret < 0)
3d0327ed 1384 return log_error_errno(-1, errno, "Failed to unfreeze container");
018051e3
CB
1385
1386 return cmd.rsp.ret;
1387}
1388
1389static int lxc_cmd_unfreeze_callback(int fd, struct lxc_cmd_req *req,
1390 struct lxc_handler *handler,
1391 struct lxc_epoll_descr *descr)
1392{
1393 int timeout = PTR_TO_INT(req->data);
1394 struct lxc_cmd_rsp rsp = {
6c6497ea 1395 .ret = -ENOENT,
018051e3
CB
1396 };
1397 struct cgroup_ops *ops = handler->cgroup_ops;
1398
1973b62a 1399 if (pure_unified_layout(ops))
018051e3
CB
1400 rsp.ret = ops->unfreeze(ops, timeout);
1401
1402 return lxc_cmd_rsp_send(fd, &rsp);
1403}
1404
bad788b0
CB
1405int lxc_cmd_get_cgroup2_fd(const char *name, const char *lxcpath)
1406{
1407 int ret, stopped;
1408 struct lxc_cmd_rr cmd = {
1409 .req = {
1410 .cmd = LXC_CMD_GET_CGROUP2_FD,
1411 },
1412 };
1413
1414 ret = lxc_cmd(name, &cmd, &stopped, lxcpath, NULL);
23a917e5
CB
1415 if (ret < 0)
1416 return -1;
1417
1418 if (cmd.rsp.ret < 0)
a5263e59 1419 return log_debug_errno(cmd.rsp.ret, -cmd.rsp.ret, "Failed to receive cgroup2 fd");
bad788b0
CB
1420
1421 return PTR_TO_INT(cmd.rsp.data);
1422}
1423
a900cbaf
WB
1424static int lxc_cmd_get_cgroup2_fd_callback_do(int fd, struct lxc_cmd_req *req,
1425 struct lxc_handler *handler,
1426 struct lxc_epoll_descr *descr,
1427 bool limiting_cgroup)
bad788b0
CB
1428{
1429 struct lxc_cmd_rsp rsp = {
1430 .ret = -EINVAL,
1431 };
1432 struct cgroup_ops *ops = handler->cgroup_ops;
a900cbaf 1433 int ret, send_fd;
bad788b0 1434
1973b62a 1435 if (!pure_unified_layout(ops) || !ops->unified)
bad788b0
CB
1436 return lxc_cmd_rsp_send(fd, &rsp);
1437
a900cbaf
WB
1438 send_fd = limiting_cgroup ? ops->unified->cgfd_limit
1439 : ops->unified->cgfd_con;
1440
bad788b0 1441 rsp.ret = 0;
a900cbaf 1442 ret = lxc_abstract_unix_send_fds(fd, &send_fd, 1, &rsp, sizeof(rsp));
bad788b0 1443 if (ret < 0)
a804c19b 1444 return log_error(LXC_CMD_REAP_CLIENT_FD, "Failed to send cgroup2 fd");
bad788b0
CB
1445
1446 return 0;
1447}
1448
a900cbaf
WB
1449static int lxc_cmd_get_cgroup2_fd_callback(int fd, struct lxc_cmd_req *req,
1450 struct lxc_handler *handler,
1451 struct lxc_epoll_descr *descr)
1452{
1453 return lxc_cmd_get_cgroup2_fd_callback_do(fd, req, handler, descr,
1454 false);
1455}
1456
1457static int lxc_cmd_get_limiting_cgroup2_fd_callback(int fd,
1458 struct lxc_cmd_req *req,
1459 struct lxc_handler *handler,
1460 struct lxc_epoll_descr *descr)
1461{
1462 return lxc_cmd_get_cgroup2_fd_callback_do(fd, req, handler, descr,
1463 true);
1464}
1465
ef6e34ee 1466static int lxc_cmd_process(int fd, struct lxc_cmd_req *req,
cdb2a47f
CB
1467 struct lxc_handler *handler,
1468 struct lxc_epoll_descr *descr)
724e753c 1469{
cdb2a47f
CB
1470 typedef int (*callback)(int, struct lxc_cmd_req *, struct lxc_handler *,
1471 struct lxc_epoll_descr *);
ef6e34ee
DE
1472
1473 callback cb[LXC_CMD_MAX] = {
2a63b5cb
CB
1474 [LXC_CMD_CONSOLE] = lxc_cmd_console_callback,
1475 [LXC_CMD_TERMINAL_WINCH] = lxc_cmd_terminal_winch_callback,
1476 [LXC_CMD_STOP] = lxc_cmd_stop_callback,
1477 [LXC_CMD_GET_STATE] = lxc_cmd_get_state_callback,
1478 [LXC_CMD_GET_INIT_PID] = lxc_cmd_get_init_pid_callback,
1479 [LXC_CMD_GET_CLONE_FLAGS] = lxc_cmd_get_clone_flags_callback,
1480 [LXC_CMD_GET_CGROUP] = lxc_cmd_get_cgroup_callback,
1481 [LXC_CMD_GET_CONFIG_ITEM] = lxc_cmd_get_config_item_callback,
1482 [LXC_CMD_GET_NAME] = lxc_cmd_get_name_callback,
1483 [LXC_CMD_GET_LXCPATH] = lxc_cmd_get_lxcpath_callback,
1484 [LXC_CMD_ADD_STATE_CLIENT] = lxc_cmd_add_state_client_callback,
1485 [LXC_CMD_CONSOLE_LOG] = lxc_cmd_console_log_callback,
1486 [LXC_CMD_SERVE_STATE_CLIENTS] = lxc_cmd_serve_state_clients_callback,
1487 [LXC_CMD_SECCOMP_NOTIFY_ADD_LISTENER] = lxc_cmd_seccomp_notify_add_listener_callback,
1488 [LXC_CMD_ADD_BPF_DEVICE_CGROUP] = lxc_cmd_add_bpf_device_cgroup_callback,
018051e3
CB
1489 [LXC_CMD_FREEZE] = lxc_cmd_freeze_callback,
1490 [LXC_CMD_UNFREEZE] = lxc_cmd_unfreeze_callback,
bad788b0 1491 [LXC_CMD_GET_CGROUP2_FD] = lxc_cmd_get_cgroup2_fd_callback,
746aab51 1492 [LXC_CMD_GET_INIT_PIDFD] = lxc_cmd_get_init_pidfd_callback,
a900cbaf
WB
1493 [LXC_CMD_GET_LIMITING_CGROUP] = lxc_cmd_get_limiting_cgroup_callback,
1494 [LXC_CMD_GET_LIMITING_CGROUP2_FD] = lxc_cmd_get_limiting_cgroup2_fd_callback,
724e753c
MN
1495 };
1496
23a917e5
CB
1497 if (req->cmd >= LXC_CMD_MAX)
1498 return log_error_errno(-1, ENOENT, "Undefined command id %d", req->cmd);
1499
cdb2a47f 1500 return cb[req->cmd](fd, req, handler, descr);
724e753c
MN
1501}
1502
ef6e34ee 1503static void lxc_cmd_fd_cleanup(int fd, struct lxc_handler *handler,
cdb2a47f 1504 struct lxc_epoll_descr *descr, const lxc_cmd_t cmd)
724e753c 1505{
3dfe6f8d 1506 lxc_terminal_free(handler->conf, fd);
724e753c 1507 lxc_mainloop_del_handler(descr, fd);
f6fc1565 1508
cd5369b0 1509 if (cmd == LXC_CMD_ADD_STATE_CLIENT) {
ab92468c
CB
1510 struct lxc_list *cur, *next;
1511
cdb2a47f
CB
1512 lxc_list_for_each_safe(cur, &handler->conf->state_clients, next) {
1513 struct lxc_state_client *client = cur->elem;
1514
1515 if (client->clientfd != fd)
1516 continue;
1517
ab92468c
CB
1518 /*
1519 * Only kick client from list so it can't be found
1520 * anymore. The actual close happens, as for all other
1521 * file descriptors, below.
1522 */
cdb2a47f 1523 lxc_list_del(cur);
cdb2a47f
CB
1524 free(cur->elem);
1525 free(cur);
ab92468c 1526
39e2a438
CB
1527 /*
1528 * No need to walk the whole list. If we found the state
cdb2a47f
CB
1529 * client fd there can't be a second one.
1530 */
ea2a070b 1531 TRACE("Found state client fd %d in state client list for command \"%s\"", fd, lxc_cmd_str(cmd));
ab92468c 1532 break;
cdb2a47f 1533 }
39e2a438
CB
1534
1535 /*
1536 * We didn't add the state client to the list. Either because
1537 * we failed to allocate memory (unlikely) or because the state
1538 * was already reached by the time we were ready to add it. So
1539 * fallthrough and clean it up.
1540 */
ea2a070b 1541 TRACE("Closing state client fd %d for command \"%s\"", fd, lxc_cmd_str(cmd));
f6fc1565 1542 }
cd5369b0 1543
ea2a070b 1544 TRACE("Closing client fd %d for command \"%s\"", fd, lxc_cmd_str(cmd));
cd5369b0 1545 close(fd);
724e753c
MN
1546}
1547
84c92abd
DE
1548static int lxc_cmd_handler(int fd, uint32_t events, void *data,
1549 struct lxc_epoll_descr *descr)
724e753c 1550{
5265a60c 1551 __do_free void *reqdata = NULL;
724e753c 1552 int ret;
ef6e34ee 1553 struct lxc_cmd_req req;
724e753c
MN
1554 struct lxc_handler *handler = data;
1555
aae93dd3 1556 ret = lxc_abstract_unix_rcv_credential(fd, &req, sizeof(req));
ded1d23f 1557 if (ret < 0) {
ea2a070b 1558 SYSERROR("Failed to receive data on command socket for command \"%s\"", lxc_cmd_str(req.cmd));
9044b79e 1559
1560 if (errno == EACCES) {
1561 /* We don't care for the peer, just send and close. */
ea2a070b 1562 struct lxc_cmd_rsp rsp = {
f7a97743 1563 .ret = -EPERM,
ea2a070b 1564 };
9044b79e 1565
1566 lxc_cmd_rsp_send(fd, &rsp);
1567 }
1568
724e753c
MN
1569 goto out_close;
1570 }
1571
fe84a562 1572 if (ret == 0)
724e753c 1573 goto out_close;
724e753c 1574
ef6e34ee 1575 if (ret != sizeof(req)) {
ea2a070b 1576 WARN("Failed to receive full command request. Ignoring request for \"%s\"", lxc_cmd_str(req.cmd));
ef6e34ee
DE
1577 goto out_close;
1578 }
1579
ea2a070b
CB
1580 if ((req.datalen > LXC_CMD_DATA_MAX) && (req.cmd != LXC_CMD_CONSOLE_LOG)) {
1581 ERROR("Received command data length %d is too large for command \"%s\"", req.datalen, lxc_cmd_str(req.cmd));
724e753c
MN
1582 goto out_close;
1583 }
1584
ef6e34ee 1585 if (req.datalen > 0) {
5265a60c 1586 reqdata = must_realloc(NULL, req.datalen);
e3233f26 1587 ret = lxc_recv_nointr(fd, reqdata, req.datalen, 0);
ef6e34ee 1588 if (ret != req.datalen) {
ea2a070b 1589 WARN("Failed to receive full command request. Ignoring request for \"%s\"", lxc_cmd_str(req.cmd));
ef6e34ee
DE
1590 goto out_close;
1591 }
fe84a562 1592
ef6e34ee
DE
1593 req.data = reqdata;
1594 }
1595
cdb2a47f 1596 ret = lxc_cmd_process(fd, &req, handler, descr);
724e753c 1597 if (ret) {
fe84a562 1598 /* This is not an error, but only a request to close fd. */
724e753c
MN
1599 goto out_close;
1600 }
1601
1602out:
f7a97743 1603 return LXC_MAINLOOP_CONTINUE;
fe84a562 1604
724e753c 1605out_close:
f6fc1565 1606 lxc_cmd_fd_cleanup(fd, handler, descr, req.cmd);
724e753c
MN
1607 goto out;
1608}
1609
84c92abd
DE
1610static int lxc_cmd_accept(int fd, uint32_t events, void *data,
1611 struct lxc_epoll_descr *descr)
724e753c 1612{
f62cf1d4 1613 __do_close int connection = -EBADF;
fe84a562 1614 int opt = 1, ret = -1;
724e753c
MN
1615
1616 connection = accept(fd, NULL, 0);
6c6497ea
CB
1617 if (connection < 0)
1618 return log_error_errno(LXC_MAINLOOP_ERROR, errno, "Failed to accept connection to run command");
724e753c 1619
fe84a562 1620 ret = fcntl(connection, F_SETFD, FD_CLOEXEC);
6c6497ea
CB
1621 if (ret < 0)
1622 return log_error_errno(ret, errno, "Failed to set close-on-exec on incoming command connection");
9ccb2dbc 1623
fe84a562 1624 ret = setsockopt(connection, SOL_SOCKET, SO_PASSCRED, &opt, sizeof(opt));
6c6497ea
CB
1625 if (ret < 0)
1626 return log_error_errno(ret, errno, "Failed to enable necessary credentials on command socket");
724e753c 1627
ef6e34ee 1628 ret = lxc_mainloop_add_handler(descr, connection, lxc_cmd_handler, data);
6c6497ea
CB
1629 if (ret)
1630 return log_error(ret, "Failed to add command handler");
724e753c 1631
ea2a070b 1632 TRACE("Accepted new client as fd %d on command server fd %d", connection, fd);
240fecd0 1633 move_fd(connection);
724e753c 1634 return ret;
724e753c
MN
1635}
1636
9dfa4041 1637int lxc_cmd_init(const char *name, const char *lxcpath, const char *suffix)
724e753c 1638{
f62cf1d4 1639 __do_close int fd = -EBADF;
c13e7111 1640 int ret;
b1234129 1641 char path[LXC_AUDS_ADDR_LEN] = {0};
724e753c 1642
5b46db1a 1643 ret = lxc_make_abstract_socket_name(path, sizeof(path), name, lxcpath, NULL, suffix);
fe84a562 1644 if (ret < 0)
9ba8130c 1645 return -1;
724e753c 1646
aae93dd3 1647 fd = lxc_abstract_unix_open(path, SOCK_STREAM, 0);
724e753c 1648 if (fd < 0) {
08aa08fe 1649 if (errno == EADDRINUSE)
fe84a562 1650 ERROR("Container \"%s\" appears to be already running", name);
6d1400b5 1651
6c6497ea 1652 return log_error_errno(-1, errno, "Failed to create command socket %s", &path[1]);
724e753c
MN
1653 }
1654
fe84a562 1655 ret = fcntl(fd, F_SETFD, FD_CLOEXEC);
6c6497ea
CB
1656 if (ret < 0)
1657 return log_error_errno(-1, errno, "Failed to set FD_CLOEXEC on command socket file descriptor");
91480a0f 1658
6c6497ea 1659 return log_trace(move_fd(fd), "Created abstract unix socket \"%s\"", &path[1]);
d2e30e99
DE
1660}
1661
fe84a562 1662int lxc_cmd_mainloop_add(const char *name, struct lxc_epoll_descr *descr,
ef6e34ee 1663 struct lxc_handler *handler)
d2e30e99 1664{
fe84a562 1665 int ret;
d2e30e99 1666
ea2a070b 1667 ret = lxc_mainloop_add_handler(descr, handler->conf->maincmd_fd, lxc_cmd_accept, handler);
6c6497ea 1668 if (ret < 0)
ea2a070b 1669 return log_error(ret, "Failed to add handler for command socket fd %d", handler->conf->maincmd_fd);
724e753c
MN
1670
1671 return ret;
1672}