From 94545fecc107b8e41a1298a8404442f2d93ce7aa Mon Sep 17 00:00:00 2001 From: Krishna Chilleri Date: Tue, 14 May 2024 16:03:25 -0600 Subject: [PATCH] add json parser test file --- bin/ch-run.c | 1 - bin/cjson-parser.c | 106 +++++++++++++++++++++++++++ bin/config.json | 176 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 282 insertions(+), 1 deletion(-) create mode 100644 bin/cjson-parser.c create mode 100644 bin/config.json diff --git a/bin/ch-run.c b/bin/ch-run.c index 067432ee5..0df198ef5 100644 --- a/bin/ch-run.c +++ b/bin/ch-run.c @@ -209,7 +209,6 @@ int main(int argc, char *argv[]) break; case IMG_OCI_BUNDLE: /* Parse config.json file -> containerize */ - /* OR call ch-run-oci to create and start/run container */ args.c.newroot = path_join(args.c.img_ref, "/rootfs"); break; case IMG_SQUASH: diff --git a/bin/cjson-parser.c b/bin/cjson-parser.c new file mode 100644 index 000000000..8299b5991 --- /dev/null +++ b/bin/cjson-parser.c @@ -0,0 +1,106 @@ + +#include +#include +#include +#include + +void readJSONFile(const char *filename); +void iterJSONFile(cJSON *json); + +int main() { + const char *filename = "config.json"; + readJSONFile(filename); + return 0; +} + +void iterJSONFile(cJSON *json) { + + cJSON *proc_key = cJSON_GetObjectItemCaseSensitive(json, "process"); + cJSON *root_key = cJSON_GetObjectItemCaseSensitive(json, "root"); + + json = json->child; + + while (json != NULL) { + if (strcmp(json->string, "ociVersion") == 0) { + // validate oci Version + printf("ociVersion: %s\n", json->valuestring); + } else if (strcmp(json->string, "process") == 0) { + // child: env - set environment to PATH value, cwd corresponds to --cd + cJSON *env_key = cJSON_GetObjectItemCaseSensitive(proc_key, "env"); + printf("process env: %s\n", env_key->child->valuestring); + printf("process env: %s\n", env_key->child->next->valuestring); + cJSON *cwd_key = cJSON_GetObjectItemCaseSensitive(proc_key, "cwd"); + printf("process cwd: %s\n", cwd_key->valuestring); + } else if (strcmp(json->string, "root") == 0) { + // child: path (map to newroot in ch-run.c), readonly (ignore for now) + cJSON *path_key = cJSON_GetObjectItemCaseSensitive(root_key, "path"); + printf("root path: %s\n", path_key->valuestring); + cJSON *read_key = cJSON_GetObjectItemCaseSensitive(root_key, "readonly"); + //printf("root readonly: %s\n", read_key->valuestring); + } else if (strcmp(json->string, "hostname") == 0) { + printf("hostname key identified.\n"); + // ignore + } else if (strcmp(json->string, "mounts") == 0) { + printf("mounts key identified.\n"); + // check mount(2) + } else if (strcmp(json->string, "linux") == 0) { + printf("linux key identified.\n"); + } else { + printf("key name %s NOT identified.\n", json->string); + } + json = json->next; + } +} + +void readJSONFile(const char *filename) { + + FILE *file = fopen(filename, "r"); + long length = 0; + char *buffer = NULL; + + if (file == NULL) { + fprintf(stderr, "Error: could not open file %s\n", filename); + return; + } + + // Get the file length + fseek(file, 0, SEEK_END); + length = ftell(file); + fseek(file, 0, SEEK_SET); + // Allocate content buffer + buffer = (char *)malloc((size_t)length + sizeof("")); + fread(buffer, sizeof(char), (size_t)length, file); + // Null-terminate the string + buffer[length] = '\0'; + + fclose(file); + + // Parse the JSON data + cJSON *json = cJSON_Parse(buffer); + // Check if parsing was successful + if (json == NULL) { + const char *error_ptr = cJSON_GetErrorPtr(); + if (error_ptr != NULL) { + fprintf(stderr, "Error before: %s\n", error_ptr); + } + cJSON_Delete(json); + free(buffer); + return; + } + + // Print the JSON data + char *json_tree = cJSON_Print(json); + if (json_tree == NULL) + { + fprintf(stderr, "Failed to print JSON tree.\n"); + } + //printf("%s\n", json_tree); + + // Process the JSON data + iterJSONFile(json); + + // Clean up + cJSON_Delete(json); + free(buffer); + free(json_tree); +} diff --git a/bin/config.json b/bin/config.json new file mode 100644 index 000000000..754fae2d1 --- /dev/null +++ b/bin/config.json @@ -0,0 +1,176 @@ +{ + "ociVersion": "1.0.2-dev", + "process": { + "terminal": true, + "user": {}, + + "args": [ + "sh", "redis-server", "--bind", "0.0.0.0" + ], + "env": [ + "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", + "TERM=xterm" + ], + "cwd": "/", + "capabilities": { + "bounding": [ + "CAP_AUDIT_WRITE", + "CAP_KILL", + "CAP_NET_BIND_SERVICE" + ], + "effective": [ + "CAP_AUDIT_WRITE", + "CAP_KILL", + "CAP_NET_BIND_SERVICE" + ], + "permitted": [ + "CAP_AUDIT_WRITE", + "CAP_KILL", + "CAP_NET_BIND_SERVICE" + ], + "ambient": [ + "CAP_AUDIT_WRITE", + "CAP_KILL", + "CAP_NET_BIND_SERVICE" + ] + }, + "rlimits": [ + { + "type": "RLIMIT_NOFILE", + "hard": 1024, + "soft": 1024 + } + ], + "noNewPrivileges": true + }, + "root": { + "path": "rootfs", + "readonly": true + }, + "hostname": "runc", + "mounts": [ + { + "destination": "/proc", + "type": "proc", + "source": "proc" + }, + { + "destination": "/dev", + "type": "tmpfs", + "source": "tmpfs", + "options": [ + "nosuid", + "strictatime", + "mode=755", + "size=65536k" + ] + }, + { + "destination": "/dev/pts", + "type": "devpts", + "source": "devpts", + "options": [ + "nosuid", + "noexec", + "newinstance", + "ptmxmode=0666", + "mode=0620", + "gid=5" + ] + }, + { + "destination": "/dev/shm", + "type": "tmpfs", + "source": "shm", + "options": [ + "nosuid", + "noexec", + "nodev", + "mode=1777", + "size=65536k" + ] + }, + { + "destination": "/dev/mqueue", + "type": "mqueue", + "source": "mqueue", + "options": [ + "nosuid", + "noexec", + "nodev" + ] + }, + { + "destination": "/sys", + "type": "sysfs", + "source": "sysfs", + "options": [ + "nosuid", + "noexec", + "nodev", + "ro" + ] + }, + { + "destination": "/sys/fs/cgroup", + "type": "cgroup", + "source": "cgroup", + "options": [ + "nosuid", + "noexec", + "nodev", + "relatime", + "ro" + ] + } + ], + "linux": { + "resources": { + "devices": [ + { + "allow": false, + "access": "rwm" + } + ] + }, + "namespaces": [ + { + "type": "pid" + }, + { + "type": "network" + }, + { + "type": "ipc" + }, + { + "type": "uts" + }, + { + "type": "mount" + }, + { + "type": "cgroup" + } + ], + "maskedPaths": [ + "/proc/acpi", + "/proc/asound", + "/proc/kcore", + "/proc/keys", + "/proc/latency_stats", + "/proc/timer_list", + "/proc/timer_stats", + "/proc/sched_debug", + "/sys/firmware", + "/proc/scsi" + ], + "readonlyPaths": [ + "/proc/bus", + "/proc/fs", + "/proc/irq", + "/proc/sys", + "/proc/sysrq-trigger" + ] + } +}