-
Notifications
You must be signed in to change notification settings - Fork 305
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes: #1177 Signed-off-by: Sohan Kunkerkar <sohank2602@gmail.com>
- Loading branch information
1 parent
7da99fb
commit 33cb00b
Showing
8 changed files
with
378 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
/* | ||
* crun - OCI runtime written in C | ||
* | ||
* crun 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. | ||
* | ||
* crun 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 crun. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#include <config.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <argp.h> | ||
#include <string.h> | ||
#include <unistd.h> | ||
#include <errno.h> | ||
|
||
#include <yajl/yajl_tree.h> | ||
#include <yajl/yajl_gen.h> | ||
|
||
#include "crun.h" | ||
#include "libcrun/container.h" | ||
#include "libcrun/utils.h" | ||
|
||
static char doc[] = "OCI runtime"; | ||
|
||
static struct argp_option options[] = { {0} }; | ||
|
||
static char args_doc[] = "features"; | ||
|
||
static error_t | ||
parse_opt (int key, char *arg arg_unused, struct argp_state *state arg_unused) | ||
{ | ||
if (key != ARGP_KEY_NO_ARGS) { | ||
return ARGP_ERR_UNKNOWN; | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
static struct argp run_argp = { options, parse_opt, args_doc, doc, NULL, NULL, NULL }; | ||
|
||
void | ||
addStringToJSON(yajl_gen jsonGen, const char *key, const char *value) { | ||
yajl_gen_string(jsonGen, (const unsigned char *) key, strlen(key)); | ||
yajl_gen_string(jsonGen, (const unsigned char *) value, strlen(value)); | ||
} | ||
|
||
void | ||
addBoolToJSON(yajl_gen jsonGen, const char *key, int value) { | ||
yajl_gen_string(jsonGen, (const unsigned char *) key, strlen(key)); | ||
yajl_gen_bool(jsonGen, value); | ||
} | ||
|
||
void | ||
addArrayToJSON(yajl_gen jsonGen, const char *key, const char **array) { | ||
yajl_gen_string(jsonGen, (const unsigned char *) key, strlen(key)); | ||
yajl_gen_array_open(jsonGen); | ||
|
||
for (size_t i = 0; array[i] != NULL; i++) { | ||
yajl_gen_string(jsonGen, (const unsigned char *) array[i], strlen(array[i])); | ||
} | ||
|
||
yajl_gen_array_close(jsonGen); | ||
} | ||
|
||
int | ||
crun_command_features(struct crun_global_arguments *global_args, int argc, char **argv, libcrun_error_t *err) { | ||
argp_parse(&run_argp, argc, argv, 0, 0, &options); | ||
|
||
// Call the function in features.c to gather the feature information | ||
struct features_info_s info; | ||
int result = libcrun_container_print_features(&info); | ||
if (result != 0) { | ||
libcrun_make_error(err, result, "Failed to gather features information."); | ||
return result; | ||
} | ||
|
||
// Prepare the JSON output | ||
yajl_gen jsonGen = yajl_gen_alloc(NULL); | ||
yajl_gen_status status; | ||
|
||
yajl_gen_config(jsonGen, yajl_gen_beautify, 1); // Optional: Enable pretty formatting | ||
|
||
// Start building the JSON | ||
yajl_gen_map_open(jsonGen); | ||
|
||
// Add ociVersionMin field | ||
yajl_gen_string(jsonGen, (const unsigned char *) "ociVersionMin", strlen("ociVersionMin")); | ||
yajl_gen_string(jsonGen, (const unsigned char *) info.ociVersionMin, strlen(info.ociVersionMin)); | ||
|
||
// Add ociVersionMax field | ||
yajl_gen_string(jsonGen, (const unsigned char *) "ociVersionMax", strlen("ociVersionMax")); | ||
yajl_gen_string(jsonGen, (const unsigned char *) info.ociVersionMax, strlen(info.ociVersionMax)); | ||
|
||
// Add hooks array | ||
addArrayToJSON(jsonGen, "hooks", info.hooks); | ||
// Add mountOptions array | ||
addArrayToJSON(jsonGen, "mountOptions", info.mountOptions); | ||
|
||
yajl_gen_string(jsonGen, (const unsigned char *) "linux", strlen("linux")); | ||
yajl_gen_map_open(jsonGen); | ||
|
||
// Add namespaces array | ||
addArrayToJSON(jsonGen, "namespaces", info.linux.namespaces); | ||
// Add capabilities array | ||
addArrayToJSON(jsonGen, "capabilities", info.linux.capabilities); | ||
|
||
// Generate the "cgroup" field | ||
yajl_gen_string(jsonGen, (const unsigned char *) "cgroup", strlen("cgroup")); | ||
yajl_gen_map_open(jsonGen); | ||
|
||
addBoolToJSON(jsonGen, "v1", info.linux.cgroup.v1); | ||
addBoolToJSON(jsonGen, "v2", info.linux.cgroup.v2); | ||
addBoolToJSON(jsonGen, "systemd", info.linux.cgroup.systemd); | ||
addBoolToJSON(jsonGen, "systemdUser", info.linux.cgroup.systemdUser); | ||
|
||
// Close the map for cgroup | ||
yajl_gen_map_close(jsonGen); | ||
|
||
// Add seccomp field with TODO comment | ||
yajl_gen_string(jsonGen, (const unsigned char *) "seccomp", strlen("seccomp")); | ||
yajl_gen_map_open(jsonGen); | ||
|
||
yajl_gen_string(jsonGen, (const unsigned char *) "/* TODO: */", strlen("/* TODO: */")); | ||
|
||
// Close the map for seccomp | ||
yajl_gen_map_close(jsonGen); | ||
|
||
// Generate "apparmor" field | ||
yajl_gen_string(jsonGen, (const unsigned char *) "apparmor", strlen("apparmor")); | ||
yajl_gen_map_open(jsonGen); | ||
|
||
addBoolToJSON(jsonGen, "enabled", info.linux.apparmor.enabled); | ||
|
||
// Close the map for apparmor | ||
yajl_gen_map_close(jsonGen); | ||
|
||
// Generate "selinux" field | ||
yajl_gen_string(jsonGen, (const unsigned char *) "selinux", strlen("selinux")); | ||
yajl_gen_map_open(jsonGen); | ||
|
||
addBoolToJSON(jsonGen, "enabled", info.linux.selinux.enabled); | ||
|
||
// Close the map for selinux | ||
yajl_gen_map_close(jsonGen); | ||
|
||
// Close the map for linux | ||
yajl_gen_map_close(jsonGen); | ||
|
||
// End building the JSON | ||
yajl_gen_map_close(jsonGen); | ||
|
||
const unsigned char *jsonString; | ||
size_t jsonLength; | ||
yajl_gen_get_buf(jsonGen, &jsonString, &jsonLength); | ||
|
||
printf("%s", (const char *) jsonString); | ||
|
||
yajl_gen_free(jsonGen); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* crun - OCI runtime written in C | ||
* | ||
* Copyright (C) 2017, 2018, 2019 Giuseppe Scrivano <giuseppe@scrivano.org> | ||
* crun 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. | ||
* | ||
* crun 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 crun. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
#ifndef FEATURES_H | ||
#define FEATURES_H | ||
|
||
#include "crun.h" | ||
|
||
int crun_command_features (struct crun_global_arguments *global_args, int argc, char **argv, libcrun_error_t *error); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.