GCC Code Coverage Report


Directory: src/gate/
File: src/gate/tech/platform/gate_lxc.h
Date: 2026-03-20 22:56:14
Exec Total Coverage
Lines: 4 13 30.8%
Functions: 1 1 100.0%
Branches: 2 4 50.0%

Line Branch Exec Source
1
2 /* GATE PROJECT LICENSE:
3 +----------------------------------------------------------------------------+
4 | Copyright(c) 2018-2026, Stefan Meislinger |
5 | All rights reserved. |
6 | |
7 | Redistribution and use in source and binary forms, with or without |
8 | modification, are permitted provided that the following conditions are met:|
9 | |
10 | 1. Redistributions of source code must retain the above copyright notice, |
11 | this list of conditions and the following disclaimer. |
12 | 2. Redistributions in binary form must reproduce the above copyright |
13 | notice, this list of conditions and the following disclaimer in the |
14 | documentation and/or other materials provided with the distribution. |
15 | |
16 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"|
17 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
18 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
19 | ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
20 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
21 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
22 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
23 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
24 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
25 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
26 | THE POSSIBILITY OF SUCH DAMAGE. |
27 +----------------------------------------------------------------------------+
28 */
29
30 #ifndef GATE_TECH_PLATFORM_LXC_H_INCLUDED
31 #define GATE_TECH_PLATFORM_LXC_H_INCLUDED
32
33
34 #if defined(HAS_LXC_LXCCONTAINER_H)
35
36 #include <lxc/lxccontainer.h>
37
38 #else
39
40 #include <sys/types.h>
41 #include <stdint.h>
42
43 typedef int (*lxc_attach_exec_t)(void* payload);
44
45
46 typedef enum lxc_attach_env_policy_t {
47 LXC_ATTACH_KEEP_ENV = 0,
48 LXC_ATTACH_CLEAR_ENV = 1
49 } lxc_attach_env_policy_t;
50
51
52 typedef struct lxc_groups_t {
53 size_t size;
54 gid_t *list;
55 } lxc_groups_t;
56
57
58 typedef struct lxc_attach_options_t {
59 int attach_flags;
60 int namespaces;
61 long personality;
62 char *initial_cwd;
63 uid_t uid;
64 gid_t gid;
65 lxc_attach_env_policy_t env_policy;
66 char **extra_env_vars;
67 char **extra_keep_env;
68 int stdin_fd; /*!< stdin file descriptor */
69 int stdout_fd; /*!< stdout file descriptor */
70 int stderr_fd; /*!< stderr file descriptor */
71 int log_fd;
72 char *lsm_label;
73 lxc_groups_t groups;
74 } lxc_attach_options_t;
75
76
77 struct bdev_specs {
78 char * fstype;
79 uint64_t fssize;
80 struct {
81 char *zfsroot;
82 } zfs;
83 struct {
84 char * vg;
85 char * lv;
86 char * thinpool;
87 } lvm;
88 char * dir;
89 struct {
90 char * rbdname;
91 char * rbdpool;
92 } rbd;
93 };
94
95
96 struct lxc_snapshot {
97 char * name;
98 char * comment_pathname;
99 char * timestamp;
100 char * lxcpath;
101
102 void (*free) (struct lxc_snapshot *s);
103 };
104
105
106 struct migrate_opts {
107 char * directory;
108 bool verbose;
109 bool stop;
110 char * predump_dir;
111 char * pageserver_address;
112 char * pageserver_port;
113 bool preserves_inodes;
114 char * action_script;
115 bool disable_skip_in_flight;
116 uint64_t ghost_limit;
117 uint64_t features_to_check;
118 };
119
120
121 struct lxc_mount {
122 int version;
123 };
124
125
126 struct lxc_console_log {
127 bool clear;
128 bool read;
129 uint64_t *read_max;
130 char *data;
131 };
132
133
134 struct lxc_container {
135 char * name;
136 char * configfile;
137 char * pidfile;
138 struct lxc_lock * slock;
139 struct lxc_lock * privlock;
140 int numthreads;
141 struct lxc_conf * lxc_conf;
142 char * error_string;
143 int error_num;
144 bool daemonize;
145 char * config_path;
146
147 bool (*is_defined) (struct lxc_container *c);
148 const char* (*state) (struct lxc_container *c);
149 bool (*is_running) (struct lxc_container *c);
150 bool (*freeze) (struct lxc_container *c);
151 bool (*unfreeze) (struct lxc_container *c);
152 pid_t (*init_pid) (struct lxc_container *c);
153 bool (*load_config) (struct lxc_container *c, const char *alt_file);
154 bool (*start) (struct lxc_container *c, int useinit, char * const argv[]);
155 bool (*startl) (struct lxc_container *c, int useinit, ...);
156 bool (*stop) (struct lxc_container *c);
157 bool (*want_daemonize) (struct lxc_container *c, bool state);
158 bool (*want_close_all_fds) (struct lxc_container *c, bool state);
159 char * (*config_file_name) (struct lxc_container *c);
160 bool (*wait) (struct lxc_container *c, const char *state, int timeout);
161 bool (*set_config_item) (struct lxc_container *c, const char *key, const char *value);
162 bool (*destroy) (struct lxc_container *c);
163 bool (*save_config) (struct lxc_container *c, const char *alt_file);
164 bool (*create) (struct lxc_container *c, const char *t, const char *bdevtype, struct bdev_specs *specs, int flags, char *const argv[]);
165 bool (*createl) (struct lxc_container *c, const char *t, const char *bdevtype, struct bdev_specs *specs, int flags, ...);
166 bool (*rename) (struct lxc_container *c, const char *newname);
167 bool (*reboot) (struct lxc_container *c);
168 bool (*shutdown) (struct lxc_container *c, int timeout);
169 void (*clear_config) (struct lxc_container *c);
170 bool (*clear_config_item) (struct lxc_container *c, const char *key);
171 int (*get_config_item) (struct lxc_container *c, const char *key, char *retv, int inlen);
172 char* (*get_running_config_item)(struct lxc_container *c, const char *key);
173 int (*get_keys) (struct lxc_container *c, const char *key, char *retv, int inlen);
174 char** (*get_interfaces) (struct lxc_container *c);
175 char** (*get_ips) (struct lxc_container *c, const char* interface, const char* family, int scope);
176 int (*get_cgroup_item) (struct lxc_container *c, const char *subsys, char *retv, int inlen);
177 bool (*set_cgroup_item) (struct lxc_container *c, const char *subsys, const char *value);
178 const char* (*get_config_path) (struct lxc_container *c);
179 bool (*set_config_path) (struct lxc_container *c, const char *path);
180 struct lxc_container* (*clone) (struct lxc_container *c, const char *newname, const char *lxcpath, int flags, const char *bdevtype, const char *bdevdata, uint64_t newsize, char **hookargs);
181 int (*console_getfd) (struct lxc_container *c, int *ttynum, int *ptxfd);
182 int (*console) (struct lxc_container *c, int ttynum, int stdinfd, int stdoutfd, int stderrfd, int escape);
183 int (*attach) (struct lxc_container *c, lxc_attach_exec_t exec_function, void *exec_payload, lxc_attach_options_t *options, pid_t *attached_process);
184 int (*attach_run_wait) (struct lxc_container *c, lxc_attach_options_t *options, const char *program, const char * const argv[]);
185 int (*attach_run_waitl) (struct lxc_container *c, lxc_attach_options_t *options, const char *program, const char *arg, ...);
186 int (*snapshot) (struct lxc_container *c, const char *commentfile);
187 int (*snapshot_list) (struct lxc_container *c, struct lxc_snapshot **snapshots);
188 bool (*snapshot_restore) (struct lxc_container *c, const char *snapname, const char *newname);
189 bool (*snapshot_destroy) (struct lxc_container *c, const char *snapname);
190 bool (*may_control) (struct lxc_container *c);
191 bool (*add_device_node) (struct lxc_container *c, const char *src_path, const char *dest_path);
192 bool (*remove_device_node) (struct lxc_container *c, const char *src_path, const char *dest_path);
193
194 /* Post LXC-1.0 additions */
195 bool (*attach_interface) (struct lxc_container *c, const char *dev, const char *dst_dev);
196 bool (*detach_interface) (struct lxc_container *c, const char *dev, const char *dst_dev);
197 bool (*checkpoint) (struct lxc_container *c, char *directory, bool stop, bool verbose);
198 bool (*restore) (struct lxc_container *c, char *directory, bool verbose);
199 bool (*destroy_with_snapshots)(struct lxc_container *c);
200 bool (*snapshot_destroy_all) (struct lxc_container *c);
201 int (*migrate) (struct lxc_container *c, unsigned int cmd, struct migrate_opts *opts, unsigned int size);
202 int (*console_log) (struct lxc_container *c, struct lxc_console_log *log);
203 bool (*reboot2) (struct lxc_container *c, int timeout);
204 int (*mount) (struct lxc_container *c, const char *source, const char *target, const char *filesystemtype, unsigned long mountflags, const void *data, struct lxc_mount *mnt);
205 int (*umount) (struct lxc_container *c, const char *target, unsigned long mountflags, struct lxc_mount *mnt);
206 int (*seccomp_notify_fd) (struct lxc_container *c);
207 int (*seccomp_notify_fd_active)(struct lxc_container *c);
208 int (*init_pidfd) (struct lxc_container *c);
209 int (*devpts_fd) (struct lxc_container *c);
210 };
211
212 #endif /* HAS_LXC_LXCCONTAINER_H */
213
214 #define LXC_CALL
215
216 #include "gate/libraries.h"
217 #include "gate/results.h"
218
219
220 typedef struct gate_lxc_functions
221 {
222 struct lxc_container* (LXC_CALL *lxc_container_new) (const char* name, const char* configpath);
223 int (LXC_CALL *lxc_container_get) (struct lxc_container* c);
224 int (LXC_CALL *lxc_container_put) (struct lxc_container* c);
225 int (LXC_CALL *lxc_get_wait_states) (const char **states);
226
227 int (LXC_CALL *list_defined_containers) (const char *lxcpath, char ***names, struct lxc_container ***cret);
228 int (LXC_CALL *list_active_containers) (const char *lxcpath, char ***names, struct lxc_container ***cret);
229 int (LXC_CALL *list_all_containers) (const char *lxcpath, char ***names, struct lxc_container ***cret);
230 } gate_lxc_functions_t;
231
232
233 1 static gate_result_t gate_load_lxc_functions(gate_lxc_functions_t* lxcc)
234 {
235 static gate_library_t lxc_lib;
236 static gate_bool_t volatile lxc_lib_initialized;
237 static gate_string_t const lxc_lib_name = GATE_STRING_INIT_STATIC("liblxc.so");
238
239
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (!lxc_lib_initialized)
240 {
241 1 gate_result_t result = gate_library_open(&lxc_lib_name, &lxc_lib, GATE_LIBRARY_FLAG_DEFAULT);
242
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 GATE_RETURN_IF_FAILED(result);
243 lxc_lib_initialized = true;
244 }
245
246 gate_library_get_function_name(lxc_lib, "lxc_container_new", &lxcc->lxc_container_new);
247 gate_library_get_function_name(lxc_lib, "lxc_container_get", &lxcc->lxc_container_get);
248 gate_library_get_function_name(lxc_lib, "lxc_container_put", &lxcc->lxc_container_put);
249 gate_library_get_function_name(lxc_lib, "lxc_get_wait_states", &lxcc->lxc_get_wait_states);
250
251 gate_library_get_function_name(lxc_lib, "list_defined_containers", &lxcc->list_defined_containers);
252 gate_library_get_function_name(lxc_lib, "list_active_containers", &lxcc->list_active_containers);
253 gate_library_get_function_name(lxc_lib, "list_all_containers", &lxcc->list_all_containers);
254
255 return GATE_RESULT_OK;
256 }
257
258
259 #endif
260