Skip to content
This repository has been archived by the owner on Oct 2, 2024. It is now read-only.

Commit

Permalink
add json parser test file
Browse files Browse the repository at this point in the history
  • Loading branch information
kchilleri committed May 14, 2024
1 parent 2c39438 commit 94545fe
Show file tree
Hide file tree
Showing 3 changed files with 282 additions and 1 deletion.
1 change: 0 additions & 1 deletion bin/ch-run.c
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
106 changes: 106 additions & 0 deletions bin/cjson-parser.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cjson/cJSON.h>

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);
}
176 changes: 176 additions & 0 deletions bin/config.json
Original file line number Diff line number Diff line change
@@ -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"
]
}
}

0 comments on commit 94545fe

Please sign in to comment.