This repository has been archived by the owner on Mar 28, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathoci.c
1788 lines (1515 loc) · 40.8 KB
/
oci.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
/*
* This file is part of cc-oci-runtime.
*
* Copyright (C) 2016 Intel Corporation
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/**
* \file
*
* Open Container Initiative (OCI) routines.
*
* \see https://www.opencontainers.org/
*/
#include <string.h>
#include <stdbool.h>
#include <errno.h>
#include <sys/types.h>
#include <stdio.h>
#include <sys/types.h>
#include <pwd.h>
#include <fcntl.h>
#include <sys/file.h>
#include <glib.h>
#include <glib/gstdio.h>
#include <glib/gprintf.h>
#include <gio/gunixsocketaddress.h>
#include <json-glib/json-glib.h>
#include <json-glib/json-gobject.h>
#include <sys/stat.h>
#include "common.h"
#include "oci.h"
#include "util.h"
#include "process.h"
#include "network.h"
#include "json.h"
#include "mount.h"
#include "state.h"
#include "oci-config.h"
#include "runtime.h"
#include "spec_handler.h"
#include "command.h"
#include "proxy.h"
#include "pod.h"
#include "namespace.h"
extern struct start_data start_data;
private gboolean cc_oci_container_running (const struct oci_state *state);
/** Format options for VM fields to display. */
struct format_options
{
/** If \c true, output in JSON format. */
gboolean use_json;
/** Used for JSON formatting. */
JsonArray *array;
/* If \c true, show hypervisor, image and kernel details. */
gboolean show_all;
int id_width;
int pid_width;
int status_width;
int bundle_width;
int created_width;
int hypervisor_width;
int image_width;
int kernel_width;
};
/** used by stdin and stdout socket watchers */
struct socket_watcher_data
{
GMainLoop* loop;
GIOChannel* socket_io;
struct oci_state *state;
gboolean setup_success;
};
/**
* List of spec handlers used to process config on start
*/
static struct spec_handler* start_spec_handlers[] = {
&annotations_spec_handler,
&hooks_spec_handler,
&mounts_spec_handler,
&platform_spec_handler,
&process_spec_handler,
&root_spec_handler,
&vm_spec_handler,
&linux_spec_handler,
/* terminator */
NULL
};
/*!
* Get the path of the specified file below the bundle path.
*
* \param bundle_path Full path to containers bundle path.
* \param file Full path to file to find below \p bundle_path.
*
* \return Newly-allocated path string on success, else \c NULL.
*/
gchar *
cc_oci_get_bundlepath_file (const gchar *bundle_path,
const gchar *file)
{
if ((!bundle_path) || (!(*bundle_path)) ||
(!file) || (!(*file))) {
return NULL;
}
return g_build_path ("/", bundle_path, file, NULL);
}
/**
* Get the workload directory for a given container.
* For pod sandboxes, this is the sandbox workloads directory,
* while for regular containers this is the OCI root path.
*
* \param config Container configuration.
*
* \return The container workload full path.
*/
gchar *
cc_oci_get_workload_dir (struct cc_oci_config *config)
{
if (! config) {
return NULL;
}
if (config->pod) {
return config->pod->sandbox_workloads;
}
return config->workload_dir;
}
/*!
* Get the container PID.
*
* \param state Container state.
*
* \return Container PID on success, else \c -1.
*/
static GPid
cc_oci_container_pid (const struct oci_state *state)
{
if (! state) {
return -1;
}
if (state->vm && state->vm->pid) {
return state->vm->pid;
}
if (state->pod && ! state->pod->sandbox) {
return state->pid;
}
return -1;
}
/*!
* Determine the containers config file, its configuration
* and state.
*
* \param[out] config_file Dynamically-allocated path to containers
* config file.
* \param[out] config \ref cc_oci_config.
* \param[out] state \ref oci_state.
*
* \note Used by the "stop" command.
*
* \return \c true on success, else \c false.
*/
gboolean
cc_oci_get_config_and_state (gchar **config_file,
struct cc_oci_config *config,
struct oci_state **state)
{
if ((!config_file) || (!config) || (!state)) {
return false;
}
if (! cc_oci_runtime_path_get (config)) {
return false;
}
if (! cc_oci_state_file_get (config)) {
return false;
}
*state = cc_oci_state_file_read (config->state.state_file_path);
if (! (*state)) {
g_critical("failed to read state file for container %s",
config->optarg_container_id);
goto err;
}
/* Fill in further details to make the config valid */
config->bundle_path = g_strdup ((*state)->bundle_path);
config->state.workload_pid = (*state)->pid;
config->state.status = (*state)->status;
g_strlcpy (config->state.comms_path, (*state)->comms_path,
sizeof (config->state.comms_path));
g_strlcpy (config->state.procsock_path,
(*state)->procsock_path,
sizeof (config->state.procsock_path));
g_strlcpy (config->workload_dir,
(*state)->workload_dir,
sizeof (config->workload_dir));
if((*state)->block_fstype) {
config->state.block_fstype = g_strdup((*state)->block_fstype);
config->state.block_index = (*state)->block_index;
}
*config_file = cc_oci_config_file_path ((*state)->bundle_path);
if (! (*config_file)) {
goto err;
}
return true;
err:
cc_oci_state_free (*state);
g_free_if_set (*config_file);
return false;
}
/*!
* Create OCI cgroup files under a given directory.
* The actual cgroup creation routine (\ref cc_oci_create_cgroups)
* is a wrapper around that function with \ref CGROUP_MEM_DIR as
* the directory argument.
*
* \param config \ref cc_oci_config.
* \param directory The directory where cgroup files will be created.
*
* \return \c true on success, else \c false.
*/
gboolean
cc_oci_create_cgroup_files (struct cc_oci_config *config, const gchar *directory)
{
g_autofree gchar *cgroup_dir = NULL;
g_autofree gchar *cgroup_tasks = NULL;
g_autofree gchar *cgroup_procs = NULL;
g_autofree gchar *pid_str = NULL;
int fd_tasks = -1;
int fd_procs = -1;
gboolean ret = false;
if (! (config && directory)) {
return false;
}
if (!config->oci.oci_linux.cgroupsPath) {
return true;
}
cgroup_dir = g_strdup_printf ("%s/%s", directory,
config->oci.oci_linux.cgroupsPath);
if (g_mkdir_with_parents (cgroup_dir, CC_OCI_CGROUP_MODE) < 0) {
g_critical("Failed to create cgroup directory: %s", strerror(errno));
goto out;
}
pid_str = g_strdup_printf ("%d", config->state.workload_pid);
cgroup_tasks = g_strdup_printf ("%s/tasks", cgroup_dir);
fd_tasks = open (cgroup_tasks, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
if (fd_tasks < 0) {
g_critical("Failed to create cgroup tasks file: %s", strerror(errno));
goto out;
}
if (write (fd_tasks, pid_str, strlen(pid_str)) <= 0) {
g_critical ("failed to copy workload pid to cgroup tasks: %s",
strerror(errno));
goto out;
}
cgroup_procs = g_strdup_printf ("%s/cgroup.procs", cgroup_dir);
fd_procs = open (cgroup_procs, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
if (fd_procs < 0) {
g_critical("Failed to create cgroup procs file: %s", strerror(errno));
goto out;
}
if (write (fd_procs, pid_str, strlen(pid_str)) <= 0) {
g_critical ("failed to copy workload pid to cgroup procs: %s",
strerror(errno));
goto out;
}
ret = true;
out:
if (fd_procs != -1) close (fd_procs);
if (fd_tasks != -1) close (fd_tasks);
return ret;
}
/*!
* Create OCI cgroup files for a given container.
*
* \param config \ref cc_oci_config.
*
* \return \c true on success, else \c false.
*/
gboolean
cc_oci_create_cgroups (struct cc_oci_config *config)
{
return cc_oci_create_cgroup_files(config, CGROUP_MEM_DIR);
}
/*!
* Forcibly stop the Hypervisor.
*
* \param config \ref cc_oci_config.
* \param state \ref oci_state.
* \param signum Signal number to send to hypervisor.
* \param all_processes \c true to send a signal to all container's processes,
* \c false to send a signal to container's workload.
*
* \return \c true on success, else \c false.
*/
gboolean
cc_oci_kill (struct cc_oci_config *config,
struct oci_state *state,
int signum,
gboolean all_processes)
{
if (! (config && state)) {
return false;
}
if (! cc_oci_get_signame (signum)) {
g_critical ("signal %d is not supported", signum);
return false;
}
/* A pod sandbox is not a running container, nothing to kill here */
if (cc_pod_is_pod_sandbox(config)) {
config->state.status = OCI_STATUS_STOPPED;
/* update state file */
if (! cc_oci_state_file_create (config, state->create_time)) {
g_critical ("failed to recreate state file");
return false;
}
return true;
}
/* is cc-shim running ? */
if (kill (state->pid, 0) == 0) {
#ifndef UNIT_TESTING
if (all_processes || signum == SIGKILL) {
if (! cc_proxy_hyper_kill_container (config, signum, all_processes)) {
g_critical ("failed to kill container %s: %s",
config->optarg_container_id,
strerror (errno));
return false;
}
} else
#endif // UNIT_TESTING
{
if (kill (state->pid, signum) < 0 && errno != ESRCH) {
g_critical ("failed to stop cc-shim %u: %s",
(unsigned)state->pid,
strerror (errno));
return false;
}
}
/*
* According with
* https://github.com/docker/docker/blob/master/docs/reference/commandline/stop.md
*
* The main process inside the container will receive SIGTERM,
* and after a grace period, SIGKILL.
*
* SIGTERM and SIGKILL are stop signals
*/
if (signum == SIGKILL || signum == SIGTERM) {
config->state.status = OCI_STATUS_STOPPED;
/* update state file */
if (! cc_oci_state_file_create (config, state->create_time)) {
g_critical ("failed to recreate state file");
return false;
}
}
} else {
/* is VM running ?*/
if (kill (config->vm->pid, 0) == 0 &&
! cc_pod_is_pod_container(config)) {
if (! cc_proxy_hyper_destroy_pod(config)) {
return false;
}
}
config->state.status = OCI_STATUS_STOPPED;
/* update state file */
if (! cc_oci_state_file_create (config, state->create_time)) {
g_critical ("failed to recreate state file");
return false;
}
}
return true;
}
/*!
* Determine if the container is running.
*
* \param state \ref oci_state.
*
* \return \c true on success, else \c false.
*/
private gboolean
cc_oci_container_running (const struct oci_state *state)
{
GPid container_pid;
if (! state) {
return false;
}
container_pid = cc_oci_container_pid(state);
if (container_pid < 0) {
return false;
}
return kill (container_pid, 0) == 0;
}
/*!
* Get the home directory for the workload user
*
* \param config \ref cc_oci_config.
* \param passwd_path Path to the local passwd file
*
* \return Newly-allocated path string on success, else \c NULL.
*/
private gchar*
get_user_home_dir(struct cc_oci_config *config, gchar *passwd_path) {
gchar *user_home = NULL;
FILE *pw_file = NULL;
struct passwd *pw_entry;
if (! (config && passwd_path)) {
return NULL;
}
pw_file = g_fopen (passwd_path, "r");
if ( pw_file == NULL) {
g_warning("Could not open password file: %s\n", passwd_path);
return NULL;
}
while ((pw_entry = fgetpwent(pw_file)) != NULL) {
if (pw_entry->pw_uid == config->oci.process.user.uid) {
user_home = g_strdup(pw_entry->pw_dir);
break;
}
}
fclose(pw_file);
return user_home;
}
/*!
* Set the HOME environment variable
*
* \param config \ref cc_oci_config.
*
* returns early if HOME is present in the environment configuration in \p config
*/
void
set_env_home(struct cc_oci_config *config)
{
g_autofree gchar *user_home_dir = NULL;
g_autofree gchar *passwd_path = NULL;
if (! (config && config->oci.process.env)) {
return;
}
/* Check if HOME is set in the environment config */
for (gchar **var = config->oci.process.env; *var != NULL; var++) {
if (g_str_has_prefix (*var, "HOME=")) {
g_debug("Home is already set in the configuration\n");
return;
}
}
guint env_len = 1 + g_strv_length(config->oci.process.env);
gchar **new_env = g_new0(gchar*, env_len + 1);
passwd_path = g_strdup_printf ("%s/%s", config->oci.root.path, PASSWD_PATH);
user_home_dir = get_user_home_dir(config, passwd_path);
if (! user_home_dir) {
// Fallback to stateless path
g_free(passwd_path);
passwd_path = g_strdup_printf ("%s/%s", config->oci.root.path,
STATELESS_PASSWD_PATH);
user_home_dir = get_user_home_dir(config, passwd_path);
// If we are not able to retrieve the home dir, set the default as "/"
if (! user_home_dir) {
user_home_dir = g_strdup("/");
g_debug("No HOME found in environment, so setting HOME %s for user %d",
user_home_dir, config->oci.process.user.uid);
}
}
new_env[0] = g_strdup_printf("HOME=%s", user_home_dir);
for (int i = 0; i < env_len-1; i++) {
new_env[i+1] = g_strdup(config->oci.process.env[i]);
}
g_strfreev(config->oci.process.env);
config->oci.process.env = new_env;
}
/*!
* Clean up all resources (including unmounts) for
* the specified config.
*
* \param config \ref cc_oci_config.
*
* \return \c true on success, else \c false.
*/
static gboolean
cc_oci_cleanup (struct cc_oci_config *config)
{
g_assert (config);
if (! cc_oci_handle_unmounts (config)) {
return false;
}
/* Container rootfs unmount should happen after volume unmounts */
if (! cc_oci_handle_rootfs_unmount(config)) {
return false;
}
/* Pod unmounts should happen after the volume unmounts */
if (! cc_pod_handle_unmounts(config)) {
return false;
}
if (! cc_oci_state_file_delete (config)) {
return false;
}
if (! cc_oci_runtime_dir_delete (config)) {
return false;
}
return true;
}
/*!
* Parse the \c GNode representation of \ref CC_OCI_CONFIG_FILE
* and save values in the provided \ref cc_oci_config.
*
* \param config \ref cc_oci_config.
*
* \return \c true on success, else \c false.
*/
static gboolean
cc_oci_config_file_parse (struct cc_oci_config *config)
{
g_autofree gchar *config_file = NULL;
g_autofree gchar *cwd = NULL;
GNode *root = NULL;
gboolean ret = false;
if (! config || ! config->bundle_path) {
return false;
}
config_file = cc_oci_config_file_path (config->bundle_path);
if (! config_file) {
return false;
}
g_debug ("using config_file %s", config_file);
cwd = g_get_current_dir ();
if (! cwd) {
return false;
}
/* Set bundle directory as working directory. This is required
* to deal with relative paths (paths relative to the bundle
* directory) in CC_OCI_CONFIG_FILE which must
* be resolved to absolutes.
*/
if (g_chdir (config->bundle_path) != 0) {
g_critical ("Cannot chdir to %s: %s",
config->bundle_path,
strerror (errno));
return false;
}
/* convert json file to GNode */
if (! cc_oci_json_parse (&root, config_file)) {
goto out;
}
#ifdef DEBUG
/* show json file converted to GNode */
cc_oci_node_dump (root);
#endif /*DEBUG*/
/* parse the GNode representation of CC_OCI_CONFIG_FILE */
if (! cc_oci_process_config(root, config, start_spec_handlers)) {
g_critical ("failed to process config");
goto out;
}
/* Supplement the OCI config by determining VM configuration
* details.
*/
if (! get_spec_vm_from_cfg_file (config)) {
g_critical ("failed to find any sources of VM configuration");
goto out;
}
ret = true;
out:
g_free_node (root);
(void)g_chdir (cwd);
return ret;
}
/*!
* Create the state file, apply mounts and run hooks,
* but do not start the VM.
*
* \param config \ref cc_oci_config.
*
* \return \c true on success, else \c false.
*/
gboolean
cc_oci_create (struct cc_oci_config *config)
{
gboolean ret = false;
if (! config) {
return false;
}
if (! cc_oci_config_file_parse (config)) {
return false;
}
if (! cc_oci_config_check (config)) {
return false;
}
if (! cc_oci_runtime_dir_setup (config)) {
if (g_file_test (config->state.runtime_path,
G_FILE_TEST_EXISTS |
G_FILE_TEST_IS_DIR)) {
g_critical ("container %s already exists",
config->optarg_container_id);
} else {
g_critical ("failed to create runtime directory");
}
return false;
}
/**
* Bind mount container rootfs
*/
if (! config->pod) {
if (! cc_oci_rootfs_is_block_device(config)) {
if (! cc_oci_add_rootfs_mount(config)) {
g_critical("failed to add container rootfs bind mount");
return false;
}
if (! cc_handle_rootfs_mount(config)) {
g_critical("failed to mount container rootfs");
return false;
}
}
}
/**
* Pod mounts should happen on the host mount namespace.
*/
if (! cc_pod_handle_mounts(config)) {
g_critical ("failed to handle pod mounts");
return false;
}
/* The namespace setup occurs in the parent to ensure
* the hooks run successfully. The child will automatically
* inherit the namespaces.
*/
if (! cc_oci_ns_setup (config)) {
g_critical ("failed to setup namespaces");
return false;
}
if (! cc_oci_handle_mounts (config)) {
g_critical ("failed to handle mounts");
return false;
}
// FIXME: consider dry-run mode.
if (config->dry_run_mode) {
g_debug ("dry-run mode: not launching VM");
return true;
}
/* Either start a standalone container or a pod sandbox */
if (cc_pod_is_vm(config)) {
if (! cc_oci_vm_launch (config)) {
g_critical ("failed to launch VM");
goto out;
}
} else {
/* We want to start a container within a pod */
if (! cc_pod_container_create (config)) {
g_critical ("failed to launch pod container");
goto out;
}
}
ret = true;
out:
return ret;
}
/**
* Determine when \ref CC_OCI_PROCESS_SOCKET is created.
*
* \param monitor \c GFileMonitor.
* \param file \c GFile (unused).
* \param other_file \c GFile (unused).
* \param event_type \c GFileMonitorEvent (unused).
* \param loop GMainLoop.
*/
static void
cc_oci_procsock_monitor_callback(
GFileMonitor *monitor,
GFile *file,
GFile *other_file,
GFileMonitorEvent event_type,
GMainLoop **loop)
{
(void)file;
(void)other_file;
(void)event_type;
g_assert (loop);
/* CC_OCI_PROCESS_SOCKET has now been created, so delete the
* monitor.
*/
g_object_unref (monitor);
g_main_loop_quit (*loop);
g_main_loop_unref (*loop);
*loop = NULL;
}
/*!
* Start a VM previously setup by a call to cc_oci_create().
*
* \param config \ref cc_oci_config.
* \param state \ref oci_state.
*
* \return \c true on success, else \c false.
*/
gboolean
cc_oci_start (struct cc_oci_config *config,
struct oci_state *state)
{
gboolean ret = false;
GFileMonitor *monitor = NULL;
GFile *file = NULL;
GError *error = NULL;
gboolean wait = false;
gchar *config_file = NULL;
struct stat st;
int shim_flock_fd = -1;
char *shim_flock_path = NULL;
GMainLoop *loop = NULL;
if (! config || ! state) {
return false;
}
if (state->status == OCI_STATUS_RUNNING) {
if (cc_oci_container_running (state)) {
g_critical ("container %s is already running",
config->optarg_container_id);
} else {
/* pid from state file is not / no longer valid */
g_critical ("container no longer running");
}
return false;
} else if (state->status != OCI_STATUS_CREATED) {
g_critical ("unexpected state for container %s: %s",
config->optarg_container_id,
cc_oci_status_to_str (state->status));
return false;
}
/* FIXME: how can we handle a "start --bundle=..." override? */
if (start_data.bundle) {
if (config->bundle_path) {
g_free (config->bundle_path);
}
config->bundle_path = cc_oci_resolve_path (start_data.bundle);
g_free (start_data.bundle);
start_data.bundle = NULL;
}
/* XXX: If running stand-alone, wait for the hypervisor to
* finish. But if running under containerd, don't wait.
*
* A simple way to determine if we're being called
* under containerd is to check if stdin is closed.
*
* Do not wait when console is empty.
*/
if ((isatty (STDIN_FILENO) && ! config->detached_mode && ! config->pod)) {
wait = true;
}
if (wait) {
/* Create a file monitor if CC_OCI_PROCESS_SOCKET does not exist */
if (stat(config->state.procsock_path, &st)) {
loop = g_main_loop_new (NULL, 0);
if (! loop) {
g_critical ("cannot create main loop for client");
return false;
}
file = g_file_new_for_path (config->state.procsock_path);
if (! file) {
g_main_loop_unref (loop);
return false;
}
/* create inotify watch on file */
monitor = g_file_monitor(file, G_FILE_MONITOR_NONE, NULL, &error);
if (! monitor) {
g_critical ("failed to monitor %s: %s",
g_file_get_path (file),
error->message);
g_error_free (error);
g_object_unref (file);
g_main_loop_unref (loop);
return false;
}
g_signal_connect (monitor, "changed",
G_CALLBACK (cc_oci_procsock_monitor_callback),
&loop);
}
}
if (! config->pod) {
if (! cc_proxy_hyper_new_container (config)) {
ret = false;
goto out;
}
} else if (cc_pod_is_pod_sandbox(config)) {
cc_proxy_hyper_new_pod_container(config,
config->optarg_container_id,
config->optarg_container_id,
"rootfs", config->optarg_container_id);
} else if (! cc_pod_is_pod_sandbox(config)) {
if (! cc_pod_container_start (config)) {
ret = false;
goto out;
}
}
/* The shim was left in stopped state after 'create', so now let it
* continue after container has started running inside the pod.
*
* This way the shim sends/receives I/O only after the container
* has started.
*
* This is to accomodate change introduced with docker 1.12.4 to attach the stdio streams
* before create: https://github.com/docker/docker/pull/26744
*/
kill(state->pid, SIGCONT);
/* Now the VM is running */
config->state.status = OCI_STATUS_RUNNING;
/* update state file after run container */
if (! cc_oci_state_file_create (config, state->create_time)) {
g_critical ("failed to recreate state file");
ret = false;
goto out;
}
/* If a hook returns a non-zero exit code, then an error is
logged and the remaining hooks are executed. */
cc_run_hooks (config->oci.hooks.poststart,
config->state.state_file_path, false);
if (wait) {
if (loop) {
/* waiting for CC_OCI_PROCESS_SOCKET
* this socket indicates that VM is running
*/
g_main_loop_run (loop);
}
/* try to lock shim flock file
* when flock returns means that shim finished
*/
shim_flock_path = g_strdup_printf ("%s/%s", config->state.runtime_path,
CC_OCI_SHIM_LOCK_FILE);
shim_flock_fd = open (shim_flock_path, O_RDONLY);
if (shim_flock_fd < 0) {
g_critical ("failed to open shim flock file: %s", strerror(errno));
goto out;
}
if (flock (shim_flock_fd, LOCK_EX) < 0) {
g_critical ("failed to flock shim file: %s", strerror(errno));
goto out;
}
/* Read state file to detect if the VM was stopped */
ret = cc_oci_get_config_and_state (&config_file, config,
&state);
if (! ret) {
goto out;
}
/*FIXME: should start delete the container? */
/* If the VM was stopped then *do not* cleanup */
if (config->state.status != OCI_STATUS_STOPPED) {
ret = cc_oci_cleanup (config);
}
} else {
ret = true;
}
out:
if (wait) {
if (file) {
g_object_unref (file);
}
if (loop) {
g_main_loop_unref (loop);
loop = NULL;
}
g_free_if_set (config_file);
}