/
start.c
2088 lines (1734 loc) · 52.7 KB
/
start.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* lxc: linux Container library
*
* (C) Copyright IBM Corp. 2007, 2008
*
* Authors:
* Daniel Lezcano <daniel.lezcano at free.fr>
* Serge Hallyn <serge@hallyn.com>
* Christian Brauner <christian.brauner@ubuntu.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef _GNU_SOURCE
#define _GNU_SOURCE 1
#endif
#include <alloca.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <grp.h>
#include <poll.h>
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/file.h>
#include <sys/mount.h>
#include <sys/param.h>
#include <sys/prctl.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <sys/un.h>
#include <sys/wait.h>
#include <unistd.h>
#include "af_unix.h"
#include "caps.h"
#include "cgroup.h"
#include "commands.h"
#include "commands_utils.h"
#include "conf.h"
#include "config.h"
#include "confile_utils.h"
#include "error.h"
#include "file_utils.h"
#include "list.h"
#include "log.h"
#include "lsm/lsm.h"
#include "lxccontainer.h"
#include "lxclock.h"
#include "lxcseccomp.h"
#include "macro.h"
#include "mainloop.h"
#include "monitor.h"
#include "namespace.h"
#include "network.h"
#include "raw_syscalls.h"
#include "start.h"
#include "storage/storage.h"
#include "storage/storage_utils.h"
#include "sync.h"
#include "syscall_wrappers.h"
#include "terminal.h"
#include "utils.h"
#if HAVE_LIBCAP
#include <sys/capability.h>
#endif
#ifndef HAVE_STRLCPY
#include "include/strlcpy.h"
#endif
lxc_log_define(start, lxc);
extern void mod_all_rdeps(struct lxc_container *c, bool inc);
static bool do_destroy_container(struct lxc_handler *handler);
static int lxc_rmdir_onedev_wrapper(void *data);
static void lxc_destroy_container_on_signal(struct lxc_handler *handler,
const char *name);
static void print_top_failing_dir(const char *path)
{
int ret;
size_t len;
char *copy, *e, *p, saved;
len = strlen(path);
copy = alloca(len + 1);
(void)strlcpy(copy, path, len + 1);
p = copy;
e = copy + len;
while (p < e) {
while (p < e && *p == '/')
p++;
while (p < e && *p != '/')
p++;
saved = *p;
*p = '\0';
ret = access(copy, X_OK);
if (ret != 0) {
SYSERROR("Could not access %s. Please grant it x "
"access, or add an ACL for the container "
"root", copy);
return;
}
*p = saved;
}
}
static void lxc_put_nsfds(struct lxc_handler *handler)
{
int i;
for (i = 0; i < LXC_NS_MAX; i++) {
if (handler->nsfd[i] < 0)
continue;
close(handler->nsfd[i]);
handler->nsfd[i] = -EBADF;
}
}
static int lxc_try_preserve_ns(const int pid, const char *ns)
{
int fd;
fd = lxc_preserve_ns(pid, ns);
if (fd < 0) {
if (errno != ENOENT) {
SYSERROR("Failed to preserve %s namespace", ns);
return -EINVAL;
}
SYSWARN("Kernel does not support preserving %s namespaces", ns);
return -EOPNOTSUPP;
}
return fd;
}
/* lxc_try_preserve_namespaces: open /proc/@pid/ns/@ns for each namespace
* specified in ns_clone_flags.
* Return true on success, false on failure.
*/
static bool lxc_try_preserve_namespaces(struct lxc_handler *handler,
int ns_clone_flags, pid_t pid)
{
int i;
for (i = 0; i < LXC_NS_MAX; i++)
handler->nsfd[i] = -EBADF;
for (i = 0; i < LXC_NS_MAX; i++) {
int fd;
if ((ns_clone_flags & ns_info[i].clone_flag) == 0)
continue;
fd = lxc_try_preserve_ns(pid, ns_info[i].proc_name);
if (fd < 0) {
/* Do not fail to start container on kernels that do
* not support interacting with namespaces through
* /proc.
*/
if (fd == -EOPNOTSUPP)
continue;
lxc_put_nsfds(handler);
return false;
}
handler->nsfd[i] = fd;
DEBUG("Preserved %s namespace via fd %d", ns_info[i].proc_name,
handler->nsfd[i]);
}
return true;
}
static inline bool match_stdfds(int fd)
{
return (fd == STDIN_FILENO || fd == STDOUT_FILENO || fd == STDERR_FILENO);
}
int lxc_check_inherited(struct lxc_conf *conf, bool closeall,
int *fds_to_ignore, size_t len_fds)
{
int fd, fddir;
size_t i;
DIR *dir;
struct dirent *direntp;
if (conf && conf->close_all_fds)
closeall = true;
restart:
dir = opendir("/proc/self/fd");
if (!dir) {
SYSWARN("Failed to open directory");
return -1;
}
fddir = dirfd(dir);
while ((direntp = readdir(dir))) {
int ret;
struct lxc_list *cur;
bool matched = false;
if (strcmp(direntp->d_name, ".") == 0)
continue;
if (strcmp(direntp->d_name, "..") == 0)
continue;
ret = lxc_safe_int(direntp->d_name, &fd);
if (ret < 0) {
INFO("Could not parse file descriptor for \"%s\"", direntp->d_name);
continue;
}
for (i = 0; i < len_fds; i++)
if (fds_to_ignore[i] == fd)
break;
if (fd == fddir || fd == lxc_log_fd ||
(i < len_fds && fd == fds_to_ignore[i]))
continue;
/* Keep state clients that wait on reboots. */
if (conf) {
lxc_list_for_each(cur, &conf->state_clients) {
struct lxc_state_client *client = cur->elem;
if (client->clientfd != fd)
continue;
matched = true;
break;
}
}
if (matched)
continue;
if (current_config && fd == current_config->logfd)
continue;
if (match_stdfds(fd))
continue;
if (closeall) {
close(fd);
closedir(dir);
INFO("Closed inherited fd %d", fd);
goto restart;
}
WARN("Inherited fd %d", fd);
}
/* Only enable syslog at this point to avoid the above logging function
* to open a new fd and make the check_inherited function enter an
* infinite loop.
*/
lxc_log_enable_syslog();
closedir(dir); /* cannot fail */
return 0;
}
static int setup_signal_fd(sigset_t *oldmask)
{
int ret;
sigset_t mask;
const int signals[] = {SIGBUS, SIGILL, SIGSEGV, SIGWINCH};
/* Block everything except serious error signals. */
ret = sigfillset(&mask);
if (ret < 0)
return -EBADF;
for (int sig = 0; sig < (sizeof(signals) / sizeof(signals[0])); sig++) {
ret = sigdelset(&mask, signals[sig]);
if (ret < 0)
return -EBADF;
}
ret = pthread_sigmask(SIG_BLOCK, &mask, oldmask);
if (ret < 0) {
SYSERROR("Failed to set signal mask");
return -EBADF;
}
ret = signalfd(-1, &mask, SFD_CLOEXEC);
if (ret < 0) {
SYSERROR("Failed to create signal file descriptor");
return -EBADF;
}
TRACE("Created signal file descriptor %d", ret);
return ret;
}
static int signal_handler(int fd, uint32_t events, void *data,
struct lxc_epoll_descr *descr)
{
int ret;
siginfo_t info;
struct signalfd_siginfo siginfo;
struct lxc_handler *hdlr = data;
ret = lxc_read_nointr(fd, &siginfo, sizeof(siginfo));
if (ret < 0) {
ERROR("Failed to read signal info from signal file descriptor %d", fd);
return LXC_MAINLOOP_ERROR;
}
if (ret != sizeof(siginfo)) {
ERROR("Unexpected size for struct signalfd_siginfo");
return -EINVAL;
}
/* Check whether init is running. */
info.si_pid = 0;
ret = waitid(P_PID, hdlr->pid, &info, WEXITED | WNOWAIT | WNOHANG);
if (ret == 0 && info.si_pid == hdlr->pid)
hdlr->init_died = true;
/* Try to figure out a reasonable exit status to report. */
if (hdlr->init_died) {
switch (info.si_code) {
case CLD_EXITED:
hdlr->exit_status = info.si_status << 8;
break;
case CLD_KILLED:
case CLD_DUMPED:
case CLD_STOPPED:
hdlr->exit_status = info.si_status << 8 | 0x7f;
break;
case CLD_CONTINUED:
/* Huh? The waitid() told us it's dead *and* continued? */
WARN("Init %d dead and continued?", hdlr->pid);
hdlr->exit_status = 1;
break;
default:
ERROR("Unknown si_code: %d", info.si_code);
hdlr->exit_status = 1;
}
}
if (siginfo.ssi_signo == SIGHUP) {
kill(hdlr->pid, SIGTERM);
INFO("Killing %d since terminal hung up", hdlr->pid);
return hdlr->init_died ? LXC_MAINLOOP_CLOSE
: LXC_MAINLOOP_CONTINUE;
}
if (siginfo.ssi_signo != SIGCHLD) {
kill(hdlr->pid, siginfo.ssi_signo);
INFO("Forwarded signal %d to pid %d", siginfo.ssi_signo, hdlr->pid);
return hdlr->init_died ? LXC_MAINLOOP_CLOSE
: LXC_MAINLOOP_CONTINUE;
}
/* More robustness, protect ourself from a SIGCHLD sent
* by a process different from the container init.
*/
if (siginfo.ssi_pid != hdlr->pid) {
NOTICE("Received %d from pid %d instead of container init %d",
siginfo.ssi_signo, siginfo.ssi_pid, hdlr->pid);
return hdlr->init_died ? LXC_MAINLOOP_CLOSE
: LXC_MAINLOOP_CONTINUE;
}
if (siginfo.ssi_code == CLD_STOPPED) {
INFO("Container init process was stopped");
return hdlr->init_died ? LXC_MAINLOOP_CLOSE
: LXC_MAINLOOP_CONTINUE;
}
if (siginfo.ssi_code == CLD_CONTINUED) {
INFO("Container init process was continued");
return hdlr->init_died ? LXC_MAINLOOP_CLOSE
: LXC_MAINLOOP_CONTINUE;
}
DEBUG("Container init process %d exited", hdlr->pid);
return LXC_MAINLOOP_CLOSE;
}
int lxc_serve_state_clients(const char *name, struct lxc_handler *handler,
lxc_state_t state)
{
size_t retlen;
ssize_t ret;
struct lxc_list *cur, *next;
struct lxc_state_client *client;
struct lxc_msg msg = {.type = lxc_msg_state, .value = state};
if (state == THAWED)
handler->state = RUNNING;
else
handler->state = state;
TRACE("Set container state to %s", lxc_state2str(state));
if (lxc_list_empty(&handler->conf->state_clients)) {
TRACE("No state clients registered");
return 0;
}
retlen = strlcpy(msg.name, name, sizeof(msg.name));
if (retlen >= sizeof(msg.name))
return -E2BIG;
lxc_list_for_each_safe(cur, &handler->conf->state_clients, next) {
client = cur->elem;
if (client->states[state] == 0) {
TRACE("State %s not registered for state client %d",
lxc_state2str(state), client->clientfd);
continue;
}
TRACE("Sending state %s to state client %d",
lxc_state2str(state), client->clientfd);
ret = lxc_send_nointr(client->clientfd, &msg, sizeof(msg), MSG_NOSIGNAL);
if (ret <= 0)
SYSERROR("Failed to send message to client");
/* kick client from list */
lxc_list_del(cur);
close(client->clientfd);
free(cur->elem);
free(cur);
}
return 0;
}
static int lxc_serve_state_socket_pair(const char *name,
struct lxc_handler *handler,
lxc_state_t state)
{
ssize_t ret;
if (!handler->daemonize ||
handler->state_socket_pair[1] < 0 ||
state == STARTING)
return 0;
/* Close read end of the socket pair. */
close(handler->state_socket_pair[0]);
handler->state_socket_pair[0] = -1;
again:
ret = lxc_abstract_unix_send_credential(handler->state_socket_pair[1],
&(int){state}, sizeof(int));
if (ret < 0) {
SYSERROR("Failed to send state to %d", handler->state_socket_pair[1]);
if (errno == EINTR)
goto again;
return -1;
}
if (ret != sizeof(int)) {
ERROR("Message too long : %d", handler->state_socket_pair[1]);
return -1;
}
TRACE("Sent container state \"%s\" to %d", lxc_state2str(state),
handler->state_socket_pair[1]);
/* Close write end of the socket pair. */
close(handler->state_socket_pair[1]);
handler->state_socket_pair[1] = -1;
return 0;
}
int lxc_set_state(const char *name, struct lxc_handler *handler,
lxc_state_t state)
{
int ret;
ret = lxc_serve_state_socket_pair(name, handler, state);
if (ret < 0) {
ERROR("Failed to synchronize via anonymous pair of unix sockets");
return -1;
}
ret = lxc_serve_state_clients(name, handler, state);
if (ret < 0)
return -1;
/* This function will try to connect to the legacy lxc-monitord state
* server and only exists for backwards compatibility.
*/
lxc_monitor_send_state(name, state, handler->lxcpath);
return 0;
}
int lxc_poll(const char *name, struct lxc_handler *handler)
{
int ret;
bool has_console = true;
struct lxc_epoll_descr descr, descr_console;
if (handler->conf->console.path &&
strcmp(handler->conf->console.path, "none") == 0)
has_console = false;
ret = lxc_mainloop_open(&descr);
if (ret < 0) {
ERROR("Failed to create mainloop");
goto out_sigfd;
}
if (has_console) {
ret = lxc_mainloop_open(&descr_console);
if (ret < 0) {
ERROR("Failed to create console mainloop");
goto out_mainloop;
}
}
ret = lxc_mainloop_add_handler(&descr, handler->sigfd, signal_handler, handler);
if (ret < 0) {
ERROR("Failed to add signal handler for %d to mainloop", handler->sigfd);
goto out_mainloop_console;
}
if (has_console) {
struct lxc_terminal *console = &handler->conf->console;
ret = lxc_terminal_mainloop_add(&descr, console);
if (ret < 0) {
ERROR("Failed to add console handlers to mainloop");
goto out_mainloop_console;
}
ret = lxc_terminal_mainloop_add(&descr_console, console);
if (ret < 0) {
ERROR("Failed to add console handlers to console mainloop");
goto out_mainloop_console;
}
handler->conf->console.descr = &descr;
}
ret = lxc_cmd_mainloop_add(name, &descr, handler);
if (ret < 0) {
ERROR("Failed to add command handler to mainloop");
goto out_mainloop_console;
}
TRACE("Mainloop is ready");
ret = lxc_mainloop(&descr, -1);
close(descr.epfd);
descr.epfd = -EBADF;
if (ret < 0 || !handler->init_died)
goto out_mainloop_console;
if (has_console)
ret = lxc_mainloop(&descr_console, 0);
out_mainloop_console:
if (has_console) {
lxc_mainloop_close(&descr_console);
TRACE("Closed console mainloop");
}
out_mainloop:
lxc_mainloop_close(&descr);
TRACE("Closed mainloop");
out_sigfd:
close(handler->sigfd);
TRACE("Closed signal file descriptor %d", handler->sigfd);
handler->sigfd = -EBADF;
return ret;
}
void lxc_zero_handler(struct lxc_handler *handler)
{
int i;
memset(handler, 0, sizeof(struct lxc_handler));
handler->pinfd = -1;
handler->sigfd = -1;
for (i = 0; i < LXC_NS_MAX; i++)
handler->nsfd[i] = -1;
handler->data_sock[0] = -1;
handler->data_sock[1] = -1;
handler->state_socket_pair[0] = -1;
handler->state_socket_pair[1] = -1;
handler->sync_sock[0] = -1;
handler->sync_sock[1] = -1;
}
void lxc_free_handler(struct lxc_handler *handler)
{
if (handler->pinfd >= 0)
close(handler->pinfd);
if (handler->sigfd >= 0)
close(handler->sigfd);
lxc_put_nsfds(handler);
if (handler->conf && handler->conf->reboot == REBOOT_NONE)
if (handler->conf->maincmd_fd >= 0)
lxc_abstract_unix_close(handler->conf->maincmd_fd);
if (handler->state_socket_pair[0] >= 0)
close(handler->state_socket_pair[0]);
if (handler->state_socket_pair[1] >= 0)
close(handler->state_socket_pair[1]);
if (handler->cgroup_ops)
cgroup_exit(handler->cgroup_ops);
handler->conf = NULL;
free(handler);
handler = NULL;
}
struct lxc_handler *lxc_init_handler(const char *name, struct lxc_conf *conf,
const char *lxcpath, bool daemonize)
{
int i, ret;
struct lxc_handler *handler;
handler = malloc(sizeof(*handler));
if (!handler)
return NULL;
memset(handler, 0, sizeof(*handler));
/* Note that am_guest_unpriv() checks the effective uid. We
* probably don't care if we are real root only if we are running
* as root so this should be fine.
*/
handler->am_root = !am_guest_unpriv();
handler->data_sock[0] = handler->data_sock[1] = -1;
handler->conf = conf;
handler->lxcpath = lxcpath;
handler->pinfd = -1;
handler->sigfd = -EBADF;
handler->init_died = false;
handler->state_socket_pair[0] = handler->state_socket_pair[1] = -1;
if (handler->conf->reboot == REBOOT_NONE)
lxc_list_init(&handler->conf->state_clients);
for (i = 0; i < LXC_NS_MAX; i++)
handler->nsfd[i] = -1;
handler->name = name;
if (daemonize && handler->conf->reboot == REBOOT_NONE) {
/* Create socketpair() to synchronize on daemonized startup.
* When the container reboots we don't need to synchronize
* again currently so don't open another socketpair().
*/
ret = socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0,
handler->state_socket_pair);
if (ret < 0) {
ERROR("Failed to create anonymous pair of unix sockets");
goto on_error;
}
TRACE("Created anonymous pair {%d,%d} of unix sockets",
handler->state_socket_pair[0],
handler->state_socket_pair[1]);
}
if (handler->conf->reboot == REBOOT_NONE) {
handler->conf->maincmd_fd = lxc_cmd_init(name, lxcpath, "command");
if (handler->conf->maincmd_fd < 0) {
ERROR("Failed to set up command socket");
goto on_error;
}
}
TRACE("Unix domain socket %d for command server is ready",
handler->conf->maincmd_fd);
return handler;
on_error:
lxc_free_handler(handler);
return NULL;
}
int lxc_init(const char *name, struct lxc_handler *handler)
{
int ret;
const char *loglevel;
struct lxc_conf *conf = handler->conf;
lsm_init();
TRACE("Initialized LSM");
ret = lxc_read_seccomp_config(conf);
if (ret < 0) {
ERROR("Failed loading seccomp policy");
goto out_close_maincmd_fd;
}
TRACE("Read seccomp policy");
/* Begin by setting the state to STARTING. */
ret = lxc_set_state(name, handler, STARTING);
if (ret < 0) {
ERROR("Failed to set state to \"%s\"", lxc_state2str(STARTING));
goto out_close_maincmd_fd;
}
TRACE("Set container state to \"STARTING\"");
/* Start of environment variable setup for hooks. */
ret = setenv("LXC_NAME", name, 1);
if (ret < 0)
SYSERROR("Failed to set environment variable: LXC_NAME=%s", name);
if (conf->rcfile) {
ret = setenv("LXC_CONFIG_FILE", conf->rcfile, 1);
if (ret < 0)
SYSERROR("Failed to set environment variable: "
"LXC_CONFIG_FILE=%s", conf->rcfile);
}
if (conf->rootfs.mount) {
ret = setenv("LXC_ROOTFS_MOUNT", conf->rootfs.mount, 1);
if (ret < 0)
SYSERROR("Failed to set environment variable: "
"LXC_ROOTFS_MOUNT=%s", conf->rootfs.mount);
}
if (conf->rootfs.path) {
ret = setenv("LXC_ROOTFS_PATH", conf->rootfs.path, 1);
if (ret < 0)
SYSERROR("Failed to set environment variable: "
"LXC_ROOTFS_PATH=%s", conf->rootfs.path);
}
if (conf->console.path) {
ret = setenv("LXC_CONSOLE", conf->console.path, 1);
if (ret < 0)
SYSERROR("Failed to set environment variable: "
"LXC_CONSOLE=%s", conf->console.path);
}
if (conf->console.log_path) {
ret = setenv("LXC_CONSOLE_LOGPATH", conf->console.log_path, 1);
if (ret < 0)
SYSERROR("Failed to set environment variable: "
"LXC_CONSOLE_LOGPATH=%s", conf->console.log_path);
}
if (cgns_supported()) {
ret = setenv("LXC_CGNS_AWARE", "1", 1);
if (ret < 0)
SYSERROR("Failed to set environment variable "
"LXC_CGNS_AWARE=1");
}
loglevel = lxc_log_priority_to_string(lxc_log_get_level());
ret = setenv("LXC_LOG_LEVEL", loglevel, 1);
if (ret < 0)
SYSERROR("Set environment variable LXC_LOG_LEVEL=%s",
loglevel);
if (conf->hooks_version == 0)
ret = setenv("LXC_HOOK_VERSION", "0", 1);
else
ret = setenv("LXC_HOOK_VERSION", "1", 1);
if (ret < 0)
SYSERROR("Failed to set environment variable LXC_HOOK_VERSION=%u", conf->hooks_version);
/* End of environment variable setup for hooks. */
TRACE("Set environment variables");
ret = run_lxc_hooks(name, "pre-start", conf, NULL);
if (ret < 0) {
ERROR("Failed to run lxc.hook.pre-start for container \"%s\"", name);
goto out_aborting;
}
TRACE("Ran pre-start hooks");
/* The signal fd has to be created before forking otherwise if the child
* process exits before we setup the signal fd, the event will be lost
* and the command will be stuck.
*/
handler->sigfd = setup_signal_fd(&handler->oldmask);
if (handler->sigfd < 0) {
ERROR("Failed to setup SIGCHLD fd handler.");
goto out_delete_tty;
}
TRACE("Set up signal fd");
/* Do this after setting up signals since it might unblock SIGWINCH. */
ret = lxc_terminal_setup(conf);
if (ret < 0) {
ERROR("Failed to create console");
goto out_restore_sigmask;
}
TRACE("Created console");
ret = lxc_terminal_map_ids(conf, &conf->console);
if (ret < 0) {
ERROR("Failed to chown console");
goto out_delete_terminal;
}
TRACE("Chowned console");
handler->cgroup_ops = cgroup_init(handler);
if (!handler->cgroup_ops) {
ERROR("Failed to initialize cgroup driver");
goto out_delete_terminal;
}
TRACE("Initialized cgroup driver");
INFO("Container \"%s\" is initialized", name);
return 0;
out_delete_terminal:
lxc_terminal_delete(&handler->conf->console);
out_restore_sigmask:
(void)pthread_sigmask(SIG_SETMASK, &handler->oldmask, NULL);
out_delete_tty:
lxc_delete_tty(&conf->ttys);
out_aborting:
(void)lxc_set_state(name, handler, ABORTING);
out_close_maincmd_fd:
lxc_abstract_unix_close(conf->maincmd_fd);
conf->maincmd_fd = -1;
return -1;
}
void lxc_fini(const char *name, struct lxc_handler *handler)
{
int i, ret;
pid_t self;
struct lxc_list *cur, *next;
char *namespaces[LXC_NS_MAX + 1];
size_t namespace_count = 0;
struct cgroup_ops *cgroup_ops = handler->cgroup_ops;
/* The STOPPING state is there for future cleanup code which can take
* awhile.
*/
lxc_set_state(name, handler, STOPPING);
self = lxc_raw_getpid();
for (i = 0; i < LXC_NS_MAX; i++) {
if (handler->nsfd[i] < 0)
continue;
if (handler->conf->hooks_version == 0)
ret = asprintf(&namespaces[namespace_count],
"%s:/proc/%d/fd/%d", ns_info[i].proc_name,
self, handler->nsfd[i]);
else
ret = asprintf(&namespaces[namespace_count],
"/proc/%d/fd/%d", self, handler->nsfd[i]);
if (ret == -1) {
SYSERROR("Failed to allocate memory");
break;
}
if (handler->conf->hooks_version == 0) {
namespace_count++;
continue;
}
ret = setenv(ns_info[i].env_name, namespaces[namespace_count], 1);
if (ret < 0)
SYSERROR("Failed to set environment variable %s=%s",
ns_info[i].env_name, namespaces[namespace_count]);
else
TRACE("Set environment variable %s=%s",
ns_info[i].env_name, namespaces[namespace_count]);
namespace_count++;
}
namespaces[namespace_count] = NULL;
if (handler->conf->reboot > REBOOT_NONE) {
ret = setenv("LXC_TARGET", "reboot", 1);
if (ret < 0)
SYSERROR("Failed to set environment variable: "
"LXC_TARGET=reboot");
}
if (handler->conf->reboot == REBOOT_NONE) {
ret = setenv("LXC_TARGET", "stop", 1);
if (ret < 0)
SYSERROR("Failed to set environment variable: "
"LXC_TARGET=stop");
}
if (handler->conf->hooks_version == 0)
ret = run_lxc_hooks(name, "stop", handler->conf, namespaces);
else
ret = run_lxc_hooks(name, "stop", handler->conf, NULL);
if (ret < 0)
ERROR("Failed to run \"lxc.hook.stop\" hook");
while (namespace_count--)
free(namespaces[namespace_count]);
cgroup_ops->destroy(cgroup_ops, handler);
cgroup_exit(cgroup_ops);
if (handler->conf->reboot == REBOOT_NONE) {
/* For all new state clients simply close the command socket.
* This will inform all state clients that the container is
* STOPPED and also prevents a race between a open()/close() on
* the command socket causing a new process to get ECONNREFUSED
* because we haven't yet closed the command socket.
*/
lxc_abstract_unix_close(handler->conf->maincmd_fd);
handler->conf->maincmd_fd = -1;
TRACE("Closed command socket");
/* This function will try to connect to the legacy lxc-monitord
* state server and only exists for backwards compatibility.
*/
lxc_monitor_send_state(name, STOPPED, handler->lxcpath);
/* The command socket is closed so no one can acces the command
* socket anymore so there's no need to lock it.
*/
handler->state = STOPPED;
TRACE("Set container state to \"STOPPED\"");
} else {
lxc_set_state(name, handler, STOPPED);
}
ret = run_lxc_hooks(name, "post-stop", handler->conf, NULL);
if (ret < 0) {
ERROR("Failed to run lxc.hook.post-stop for container \"%s\"", name);
if (handler->conf->reboot > REBOOT_NONE) {
WARN("Container will be stopped instead of rebooted");
handler->conf->reboot = REBOOT_NONE;
ret = setenv("LXC_TARGET", "stop", 1);
if (ret < 0)
WARN("Failed to set environment variable: "
"LXC_TARGET=stop");
}
}
/* Reset mask set by setup_signal_fd. */
ret = pthread_sigmask(SIG_SETMASK, &handler->oldmask, NULL);
if (ret < 0)
SYSWARN("Failed to restore signal mask");
lxc_terminal_delete(&handler->conf->console);