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
/
linux.c
143 lines (120 loc) · 3.33 KB
/
linux.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
/*
* 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.
*/
#include "spec_handler.h"
#include "namespace.h"
#include "util.h"
static struct oci_cfg_namespace *current_ns;
static bool error_detected = false;
/*!
* Add \ref current_ns to the specified \c GSList.
*
* \param ns_list \c GSList of \ref oci_cfg_namespace's.
*/
static void
save_current_ns (GSList **ns_list)
{
if (! current_ns) {
return;
}
*ns_list = g_slist_append ((*ns_list), current_ns);
current_ns = NULL;
}
static void
handle_namespaces_section (GNode *root, struct cc_oci_config *config)
{
if ((! root) || error_detected) {
return;
}
if (! root->data) {
save_current_ns (&config->oci.oci_linux.namespaces);
} else if (root->children) {
/* create and populate a new ns */
if (!current_ns) {
current_ns = g_new0 (struct oci_cfg_namespace, 1);
}
if (! g_strcmp0 (root->data, "type")) {
const char *type = (const gchar *)root->children->data;
if (! type) {
g_critical ("no namespace type specified");
goto err;
}
current_ns->type = cc_oci_str_to_ns (type);
if ((! type) || (! *type) || (current_ns->type == OCI_NS_INVALID)) {
g_critical ("invalid namespace type: %s",
type ? type : "");
goto err;
}
} else if (! g_strcmp0 (root->data, "path")) {
const char *path = (const gchar *)root->children->data;
/* Note that 'path' can also be "" or absent
* (denoting that a new namespace should be
* created.
*/
if (path && *path) {
current_ns->path = g_strdup (path);
}
}
}
return;
err:
cc_oci_ns_free (current_ns);
error_detected = true;
current_ns = NULL;
}
static void
handle_linux_section (GNode *root, struct cc_oci_config *config)
{
if (! (root && root->children)) {
return;
}
if (! g_strcmp0 (root->data, "namespaces")) {
g_node_children_foreach(root, G_TRAVERSE_ALL,
(GNodeForeachFunc)handle_namespaces_section,
config);
} else if (! g_strcmp0 (root->data, "cgroupsPath")) {
config->oci.oci_linux.cgroupsPath = g_strdup (root->children->data);
}
}
static bool
linux_handle_section (GNode *root, struct cc_oci_config *config)
{
gboolean ret = false;
error_detected = false;
if (! root) {
g_critical ("root node is NULL");
goto out;
}
if (! config) {
g_critical ("oci config is NULL");
goto out;
}
g_node_children_foreach (root, G_TRAVERSE_ALL,
(GNodeForeachFunc)handle_linux_section, config);
if (! error_detected) {
save_current_ns (&config->oci.oci_linux.namespaces);
}
ret = ! error_detected;
out:
return ret;
}
struct spec_handler linux_spec_handler = {
.name = "linux",
.handle_section = linux_handle_section
};