Debian Codegraph @

lxc-binding.c

raw symbols SCIP
1 // Copyright © 2013, 2014, The Go-LXC Authors. All rights reserved.
2 // Use of this source code is governed by a LGPLv2.1
3 // license that can be found in the LICENSE file.
4
5 // +build linux,cgo
6
7 #include <errno.h>
8 #include <stdbool.h>
9 #include <string.h>
10 #include <sys/types.h>
11 #include <sys/wait.h>
12 #include <errno.h>
13
14 #include <lxc/lxccontainer.h>
15 #include <lxc/attach_options.h>
16 #include <lxc/version.h>
17
18 #include "lxc-binding.h"
19
20 #ifndef LXC_DEVEL
21 #define LXC_DEVEL 0
22 #endif
23
24 #define ret_errno(__errno__) \
25 ({ \
26 errno = (__errno__); \
27 -(__errno__); \
28 })
29
30 bool go_lxc_defined(struct lxc_container *c) {
31 return c->is_defined(c);
32 }
33
34 const char* go_lxc_state(struct lxc_container *c) {
35 return c->state(c);
36 }
37
38 bool go_lxc_running(struct lxc_container *c) {
39 return c->is_running(c);
40 }
41
42 bool go_lxc_freeze(struct lxc_container *c) {
43 return c->freeze(c);
44 }
45
46 bool go_lxc_unfreeze(struct lxc_container *c) {
47 return c->unfreeze(c);
48 }
49
50 pid_t go_lxc_init_pid(struct lxc_container *c) {
51 return c->init_pid(c);
52 }
53
54 int go_lxc_init_pidfd(struct lxc_container *c) {
55 #if VERSION_AT_LEAST(4, 0, 0)
56 return c->init_pidfd(c);
57 #else
58 return ret_errno(ENOSYS);
59 #endif
60 }
61
62 int go_lxc_seccomp_notify_fd(struct lxc_container *c) {
63 #if VERSION_AT_LEAST(4, 0, 0)
64 return c->seccomp_notify_fd(c);
65 #else
66 return ret_errno(ENOSYS);
67 #endif
68 }
69
70 int go_lxc_seccomp_notify_fd_active(struct lxc_container *c) {
71 #if VERSION_AT_LEAST(4, 0, 5)
72 return c->seccomp_notify_fd_active(c);
73 #else
74 return ret_errno(ENOSYS);
75 #endif
76 }
77
78 int go_lxc_set_timeout(struct lxc_container *c, int timeout) {
79 #if VERSION_AT_LEAST(5, 0, 4)
80 if (!c->set_timeout(c, timeout))
81 return ret_errno(EINVAL);
82
83 return 0;
84 #else
85 return ret_errno(ENOSYS);
86 #endif
87 }
88
89 int go_lxc_devpts_fd(struct lxc_container *c) {
90 #if VERSION_AT_LEAST(4, 0, 5)
91 return c->devpts_fd(c);
92 #else
93 return ret_errno(ENOSYS);
94 #endif
95 }
96
97 bool go_lxc_want_daemonize(struct lxc_container *c, bool state) {
98 return c->want_daemonize(c, state);
99 }
100
101 bool go_lxc_want_close_all_fds(struct lxc_container *c, bool state) {
102 return c->want_close_all_fds(c, state);
103 }
104
105 bool go_lxc_create(struct lxc_container *c, const char *t, const char *bdevtype, struct bdev_specs *specs, int flags, char * const argv[]) {
106 if (strncmp(t, "none", strlen(t)) == 0) {
107 return c->create(c, NULL, bdevtype, specs, !!(flags & LXC_CREATE_QUIET), argv);
108 }
109 return c->create(c, t, bdevtype, specs, !!(flags & LXC_CREATE_QUIET), argv);
110 }
111
112 bool go_lxc_start(struct lxc_container *c, int useinit, char * const argv[]) {
113 return c->start(c, useinit, argv);
114 }
115
116 bool go_lxc_stop(struct lxc_container *c) {
117 return c->stop(c);
118 }
119
120 bool go_lxc_reboot(struct lxc_container *c) {
121 return c->reboot(c);
122 }
123
124 bool go_lxc_shutdown(struct lxc_container *c, int timeout) {
125 return c->shutdown(c, timeout);
126 }
127
128 char* go_lxc_config_file_name(struct lxc_container *c) {
129 return c->config_file_name(c);
130 }
131
132 bool go_lxc_destroy(struct lxc_container *c) {
133 return c->destroy(c);
134 }
135
136 bool go_lxc_destroy_with_snapshots(struct lxc_container *c) {
137 #if VERSION_AT_LEAST(1, 1, 0)
138 return c->destroy_with_snapshots(c);
139 #else
140 return false;
141 #endif
142 }
143
144 bool go_lxc_wait(struct lxc_container *c, const char *state, int timeout) {
145 return c->wait(c, state, timeout);
146 }
147
148 char *go_lxc_get_config_item(struct lxc_container *c, const char *key)
149 {
150 char *value = NULL;
151
152 int len = c->get_config_item(c, key, NULL, 0);
153 if (len <= 0)
154 return NULL;
155
156 again:
157 value = (char *)malloc(sizeof(char) * len + 1);
158 if (value == NULL)
159 goto again;
160
161 if (c->get_config_item(c, key, value, len + 1) != len) {
162 free(value);
163 return NULL;
164 }
165
166 return value;
167 }
168
169 bool go_lxc_set_config_item(struct lxc_container *c, const char *key, const char *value) {
170 return c->set_config_item(c, key, value);
171 }
172
173 void go_lxc_clear_config(struct lxc_container *c) {
174 c->clear_config(c);
175 }
176
177 bool go_lxc_clear_config_item(struct lxc_container *c, const char *key) {
178 return c->clear_config_item(c, key);
179 }
180
181 char* go_lxc_get_running_config_item(struct lxc_container *c, const char *key) {
182 return c->get_running_config_item(c, key);
183 }
184
185 char *go_lxc_get_keys(struct lxc_container *c, const char *key)
186 {
187 char *value = NULL;
188
189 int len = c->get_keys(c, key, NULL, 0);
190 if (len <= 0)
191 return NULL;
192
193 again:
194 value = (char *)malloc(sizeof(char) * len + 1);
195 if (value == NULL)
196 goto again;
197
198 if (c->get_keys(c, key, value, len + 1) != len) {
199 free(value);
200 return NULL;
201 }
202
203 return value;
204 }
205
206 char *go_lxc_get_cgroup_item(struct lxc_container *c, const char *key)
207 {
208 char *value = NULL;
209
210 int len = c->get_cgroup_item(c, key, NULL, 0);
211 if (len <= 0)
212 return NULL;
213
214 again:
215 value = (char *)malloc(sizeof(char) * len + 1);
216 if (value == NULL)
217 goto again;
218
219 if (c->get_cgroup_item(c, key, value, len + 1) != len) {
220 free(value);
221 return NULL;
222 }
223
224 return value;
225 }
226
227 bool go_lxc_set_cgroup_item(struct lxc_container *c, const char *key, const char *value) {
228 return c->set_cgroup_item(c, key, value);
229 }
230
231 const char* go_lxc_get_config_path(struct lxc_container *c) {
232 return c->get_config_path(c);
233 }
234
235 bool go_lxc_set_config_path(struct lxc_container *c, const char *path) {
236 return c->set_config_path(c, path);
237 }
238
239 bool go_lxc_load_config(struct lxc_container *c, const char *alt_file) {
240 return c->load_config(c, alt_file);
241 }
242
243 bool go_lxc_save_config(struct lxc_container *c, const char *alt_file) {
244 return c->save_config(c, alt_file);
245 }
246
247 bool go_lxc_clone(struct lxc_container *c, const char *newname, const char *lxcpath, int flags, const char *bdevtype) {
248 struct lxc_container *c2 = c->clone(c, newname, lxcpath, flags, bdevtype, NULL, 0, NULL);
249 if (c2 == NULL) {
250 return false;
251 }
252 lxc_container_put(c2);
253 return true;
254 }
255
256 int go_lxc_console_getfd(struct lxc_container *c, int ttynum) {
257 int mainfd;
258 int ret = 0;
259
260 ret = c->console_getfd(c, &ttynum, &mainfd);
261 if (ret < 0)
262 return ret;
263
264 return mainfd;
265 }
266
267 bool go_lxc_console(struct lxc_container *c, int ttynum, int stdinfd, int stdoutfd, int stderrfd, int escape) {
268
269 if (c->console(c, ttynum, stdinfd, stdoutfd, stderrfd, escape) == 0) {
270 return true;
271 }
272 return false;
273 }
274
275 char** go_lxc_get_interfaces(struct lxc_container *c) {
276 return c->get_interfaces(c);
277 }
278
279 char** go_lxc_get_ips(struct lxc_container *c, const char *interface, const char *family, int scope) {
280 return c->get_ips(c, interface, family, scope);
281 }
282
283 int wait_for_pid_status(pid_t pid)
284 {
285 int status, ret;
286
287 again:
288 ret = waitpid(pid, &status, 0);
289 if (ret == -1) {
290 if (errno == EINTR)
291 goto again;
292 return -1;
293 }
294 if (ret != pid)
295 goto again;
296 return status;
297 }
298
299 int go_lxc_attach_no_wait(struct lxc_container *c,
300 bool clear_env,
301 int namespaces,
302 long personality,
303 uid_t uid, gid_t gid, lxc_groups_t groups,
304 int stdinfd, int stdoutfd, int stderrfd,
305 char *initial_cwd,
306 char **extra_env_vars,
307 char **extra_keep_env,
308 const char * const argv[],
309 pid_t *attached_pid,
310 int attach_flags) {
311 int ret;
312
313 lxc_attach_options_t attach_options = LXC_ATTACH_OPTIONS_DEFAULT;
314 attach_options.attach_flags = attach_flags;
315
316 lxc_attach_command_t command = (lxc_attach_command_t){.program = NULL};
317
318 attach_options.env_policy = LXC_ATTACH_KEEP_ENV;
319 if (clear_env) {
320 attach_options.env_policy = LXC_ATTACH_CLEAR_ENV;
321 }
322
323 attach_options.namespaces = namespaces;
324 attach_options.personality = personality;
325
326 attach_options.uid = uid;
327 attach_options.gid = gid;
328 #if VERSION_AT_LEAST(4, 0, 9)
329 if ( groups.size > 0 ) {
330 attach_options.groups = groups;
331 attach_options.attach_flags &= LXC_ATTACH_SETGROUPS;
332 }
333 #endif
334
335 attach_options.stdin_fd = stdinfd;
336 attach_options.stdout_fd = stdoutfd;
337 attach_options.stderr_fd = stderrfd;
338
339 attach_options.initial_cwd = initial_cwd;
340 attach_options.extra_env_vars = extra_env_vars;
341 attach_options.extra_keep_env = extra_keep_env;
342
343 command.program = (char *)argv[0];
344 command.argv = (char **)argv;
345
346 ret = c->attach(c, lxc_attach_run_command, &command, &attach_options, attached_pid);
347 if (ret < 0)
348 return ret;
349
350 return 0;
351 }
352
353 int go_lxc_attach(struct lxc_container *c,
354 bool clear_env,
355 int namespaces,
356 long personality,
357 uid_t uid, gid_t gid, lxc_groups_t groups,
358 int stdinfd, int stdoutfd, int stderrfd,
359 char *initial_cwd,
360 char **extra_env_vars,
361 char **extra_keep_env,
362 int attach_flags) {
363 int ret;
364 pid_t pid;
365
366 lxc_attach_options_t attach_options = LXC_ATTACH_OPTIONS_DEFAULT;
367 attach_options.attach_flags = attach_flags;
368
369 attach_options.env_policy = LXC_ATTACH_KEEP_ENV;
370 if (clear_env) {
371 attach_options.env_policy = LXC_ATTACH_CLEAR_ENV;
372 }
373
374 attach_options.namespaces = namespaces;
375 attach_options.personality = personality;
376
377 attach_options.uid = uid;
378 attach_options.gid = gid;
379 #if VERSION_AT_LEAST(4, 0, 9)
380 if ( groups.size > 0 ) {
381 attach_options.groups = groups;
382 attach_options.attach_flags &= LXC_ATTACH_SETGROUPS;
383 }
384 #endif
385
386 attach_options.stdin_fd = stdinfd;
387 attach_options.stdout_fd = stdoutfd;
388 attach_options.stderr_fd = stderrfd;
389
390 attach_options.initial_cwd = initial_cwd;
391 attach_options.extra_env_vars = extra_env_vars;
392 attach_options.extra_keep_env = extra_keep_env;
393
394 ret = c->attach(c, lxc_attach_run_shell, NULL, &attach_options, &pid);
395 if (ret < 0)
396 return ret;
397
398 ret = wait_for_pid_status(pid);
399 if (ret < 0)
400 return ret;
401
402 if (WIFEXITED(ret))
403 return WEXITSTATUS(ret);
404
405 return ret;
406 }
407
408 int go_lxc_attach_run_wait(struct lxc_container *c,
409 bool clear_env,
410 int namespaces,
411 long personality,
412 uid_t uid, gid_t gid, lxc_groups_t groups,
413 int stdinfd, int stdoutfd, int stderrfd,
414 char *initial_cwd,
415 char **extra_env_vars,
416 char **extra_keep_env,
417 const char * const argv[],
418 int attach_flags) {
419 int ret;
420
421 lxc_attach_options_t attach_options = LXC_ATTACH_OPTIONS_DEFAULT;
422 attach_options.attach_flags = attach_flags;
423
424 attach_options.env_policy = LXC_ATTACH_KEEP_ENV;
425 if (clear_env) {
426 attach_options.env_policy = LXC_ATTACH_CLEAR_ENV;
427 }
428
429 attach_options.namespaces = namespaces;
430 attach_options.personality = personality;
431
432 attach_options.uid = uid;
433 attach_options.gid = gid;
434 #if VERSION_AT_LEAST(4, 0, 9)
435 if ( groups.size > 0 ) {
436 attach_options.groups = groups;
437 attach_options.attach_flags &= LXC_ATTACH_SETGROUPS;
438 }
439 #endif
440
441 attach_options.stdin_fd = stdinfd;
442 attach_options.stdout_fd = stdoutfd;
443 attach_options.stderr_fd = stderrfd;
444
445 attach_options.initial_cwd = initial_cwd;
446 attach_options.extra_env_vars = extra_env_vars;
447 attach_options.extra_keep_env = extra_keep_env;
448
449 ret = c->attach_run_wait(c, &attach_options, argv[0], argv);
450 if (WIFEXITED(ret) && WEXITSTATUS(ret) == 255)
451 return -1;
452 return ret;
453 }
454
455 bool go_lxc_may_control(struct lxc_container *c) {
456 return c->may_control(c);
457 }
458
459 int go_lxc_snapshot(struct lxc_container *c) {
460 return c->snapshot(c, NULL);
461 }
462
463 int go_lxc_snapshot_list(struct lxc_container *c, struct lxc_snapshot **ret) {
464 return c->snapshot_list(c, ret);
465 }
466
467 bool go_lxc_snapshot_restore(struct lxc_container *c, const char *snapname, const char *newname) {
468 return c->snapshot_restore(c, snapname, newname);
469 }
470
471 bool go_lxc_snapshot_destroy(struct lxc_container *c, const char *snapname) {
472 return c->snapshot_destroy(c, snapname);
473 }
474
475 bool go_lxc_snapshot_destroy_all(struct lxc_container *c) {
476 #if VERSION_AT_LEAST(1, 1, 0)
477 return c->snapshot_destroy_all(c);
478 #else
479 return false;
480 #endif
481
482 }
483
484 bool go_lxc_add_device_node(struct lxc_container *c, const char *src_path, const char *dest_path) {
485 return c->add_device_node(c, src_path, dest_path);
486 }
487
488 bool go_lxc_remove_device_node(struct lxc_container *c, const char *src_path, const char *dest_path) {
489 return c->remove_device_node(c, src_path, dest_path);
490 }
491
492 bool go_lxc_rename(struct lxc_container *c, const char *newname) {
493 return c->rename(c, newname);
494 }
495
496 bool go_lxc_checkpoint(struct lxc_container *c, char *directory, bool stop, bool verbose) {
497 #if VERSION_AT_LEAST(1, 1, 0)
498 return c->checkpoint(c, directory, stop, verbose);
499 #else
500 return false;
501 #endif
502 }
503
504 bool go_lxc_restore(struct lxc_container *c, char *directory, bool verbose) {
505 #if VERSION_AT_LEAST(1, 1, 0)
506 return c->restore(c, directory, verbose);
507 #else
508 return false;
509 #endif
510 }
511
512 int go_lxc_migrate(struct lxc_container *c, unsigned int cmd, struct migrate_opts *opts, struct extra_migrate_opts *extras) {
513 #if VERSION_AT_LEAST(3, 0, 0)
514 opts->features_to_check = extras->features_to_check;
515 #endif
516 #if VERSION_AT_LEAST(2, 0, 4)
517 opts->action_script = extras->action_script;
518 opts->ghost_limit = extras->ghost_limit;
519 #endif
520
521 #if VERSION_AT_LEAST(2, 0, 1)
522 opts->preserves_inodes = extras->preserves_inodes;
523 #endif
524
525 #if VERSION_AT_LEAST(2, 0, 0)
526 return c->migrate(c, cmd, opts, sizeof(*opts));
527 #else
528 return -EINVAL;
529 #endif
530 }
531
532 bool go_lxc_attach_interface(struct lxc_container *c, const char *dev, const char *dst_dev) {
533 #if VERSION_AT_LEAST(1, 1, 0)
534 return c->attach_interface(c, dev, dst_dev);
535 #else
536 return false;
537 #endif
538 }
539
540 bool go_lxc_detach_interface(struct lxc_container *c, const char *dev, const char *dst_dev) {
541 #if VERSION_AT_LEAST(1, 1, 0)
542 return c->detach_interface(c, dev, dst_dev);
543 #else
544 return false;
545 #endif
546 }
547
548 bool go_lxc_config_item_is_supported(const char *key)
549 {
550 #if VERSION_AT_LEAST(2, 1, 0)
551 return lxc_config_item_is_supported(key);
552 #else
553 return false;
554 #endif
555 }
556
557 int go_lxc_error_num(struct lxc_container *c)
558 {
559 return c->error_num;
560 }
561
562 int go_lxc_console_log(struct lxc_container *c, struct lxc_console_log *log) {
563 #if VERSION_AT_LEAST(3, 0, 0)
564 return c->console_log(c, log);
565 #else
566 return false;
567 #endif
568 }
569
570 bool go_lxc_has_api_extension(const char *extension)
571 {
572 #if VERSION_AT_LEAST(3, 1, 0)
573 return lxc_has_api_extension(extension);
574 #else
575 return false;
576 #endif
577 }