Compare commits

..

31 Commits

Author SHA1 Message Date
Pin
20fc94fb35 testing errors 2024-03-03 18:46:41 -05:00
c6f9204014 Cat seems to hang forever 2024-03-03 17:43:58 -05:00
6700d98ad6 Fully branch cat and ls 2024-03-03 17:41:06 -05:00
49b9f43c76 Initial branching by command in the client 2024-03-03 17:23:54 -05:00
ebc07d0cf7 Add common support for cat command. 2024-03-03 17:19:11 -05:00
75139f709d Start branching based on the command type 2024-03-03 17:15:03 -05:00
1e5357c4cf Added destroy command function 2024-03-03 17:14:43 -05:00
7d9c451799 Resolve leaks in the client 2024-03-03 17:02:24 -05:00
a7992436b1 Replace multi-worker attempt with simple single worker
Get things working before scaling it up
2024-03-03 16:54:02 -05:00
e94211e5f4 Cleanup socket 2024-03-03 16:52:47 -05:00
2b4aa5cbaf Add message to show the number of paths sent to the worker to ls 2024-03-03 16:51:03 -05:00
bdf9fc7718 Fix error reading from the wrong msg object 2024-03-03 16:50:45 -05:00
b99513c8b3 Fix serialization
Was putting new data in the front of the message instead of the end.
This mismatched with how I was deserializing the messages
2024-03-03 16:50:45 -05:00
32a03a3895 Add message to show when the daemon is ready 2024-03-03 16:50:45 -05:00
16e95af02a Serialize struct from the client 2024-03-03 16:48:44 -05:00
a4b56107aa Add deserialize function 2024-03-03 16:09:58 -05:00
8a814b1d93 Add basic serialization of the message 2024-03-03 15:57:53 -05:00
9422f77119 Update struct to use pointers instead of flexible length structs 2024-03-03 15:46:18 -05:00
859ee90048 Send message from daemon to client 2024-03-03 15:41:10 -05:00
83f62dc217 Remove router loop 2024-03-03 15:35:11 -05:00
ea58a6ebd3 Do not use redundant pointer to pointer 2024-03-03 15:22:36 -05:00
eaf8dde6c3 Do not use draft API 2024-03-03 15:22:15 -05:00
26af0a5a5d trying to use the draft api to get router ids 2024-03-03 15:18:18 -05:00
187acad82a Trying to send a reply string from the worker 2024-03-03 15:18:18 -05:00
d6c57e9e32 Automatically select the first worker for development 2024-03-03 14:19:32 -05:00
07ee70b002 Add print statements for debugging 2024-03-03 14:16:51 -05:00
d60c0d419b Add debug flag 2024-03-03 02:05:33 -05:00
ec9a78a459 Send the message and get the reply 2024-03-03 02:05:33 -05:00
2c4cb0e235 Uncomment worker loop 2024-03-03 02:05:33 -05:00
fd2ee16f79 Renamed enum 2024-03-03 02:05:33 -05:00
44a5f6773c Build command object 2024-03-03 02:05:33 -05:00
4 changed files with 199 additions and 57 deletions

View File

@@ -2,6 +2,6 @@ daemon:
gcc guestfs-inspectd.c -o guestfs-inspectd `pkg-config libguestfs libczmq --cflags --libs` gcc guestfs-inspectd.c -o guestfs-inspectd `pkg-config libguestfs libczmq --cflags --libs`
client: client:
gcc guestfs-inspect.c -o guestfs-inspect `pkg-config libguestfs libczmq --cflags --libs` gcc -g guestfs-inspect.c -o guestfs-inspect `pkg-config libguestfs libczmq --cflags --libs`
build: daemon client build: daemon client

View File

@@ -8,6 +8,7 @@
#define STREQ(a, b) (strcmp((a), (b)) == 0) #define STREQ(a, b) (strcmp((a), (b)) == 0)
void print_help(char *); void print_help(char *);
enum guestfs_inspect_command_const parse_command(char *);
int main(int argc, char **argv) { int main(int argc, char **argv) {
if (argc == 1) { if (argc == 1) {
@@ -19,6 +20,50 @@ int main(int argc, char **argv) {
zsock_t *daemon = zsock_new_req(ep); zsock_t *daemon = zsock_new_req(ep);
free(ep); free(ep);
struct guestfs_inpsect_command *command = malloc(sizeof(struct guestfs_inpsect_command));
command->name = calloc(strlen(argv[1]) + 1, sizeof(char));
strcpy(command->name, argv[1]);
command->command = parse_command(argv[2]);
switch (command->command) {
case GUESTFS_COMMAND_LS:
command->args.ls.paths_length = 1;
command->args.ls.paths = calloc(1, sizeof(char *));
command->args.ls.paths[0] = calloc(strlen(argv[3] + 1), sizeof(char));
strcpy(command->args.ls.paths[0], argv[3]);
break;
case GUESTFS_COMMAND_CAT:
command->args.cat.paths_length = 1;
command->args.cat.paths = calloc(1, sizeof(char *));
command->args.cat.paths[0] = calloc(strlen(argv[3] + 1), sizeof(char));
strcpy(command->args.cat.paths[0], argv[3]);
break;
}
zmsg_t *msg = command_to_zmsg(command);
zmsg_send(&msg, daemon);
zmsg_t *rep = zmsg_recv(daemon);
char *res = NULL;
// process reply
switch (command->command) {
case GUESTFS_COMMAND_LS:
res = zmsg_popstr(rep);
printf("Res:\n%s\n", res);
free(res);
break;
case GUESTFS_COMMAND_CAT:
res = zmsg_popstr(rep);
printf("Res:\n%s\n", res);
free(res);
break;
}
zmsg_destroy(&msg);
zmsg_destroy(&rep);
zsock_destroy(&daemon);
command_destroy(&command);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
@@ -28,3 +73,13 @@ void print_help(char *name) {
printf(" ls <path>\n"); printf(" ls <path>\n");
printf(" cat <path>\n"); printf(" cat <path>\n");
} }
enum guestfs_inspect_command_const parse_command(char *input) {
if (STREQ(input, "ls")) {
return GUESTFS_COMMAND_LS;
} else if (STREQ(input, "cat")) {
return GUESTFS_COMMAND_CAT;
}
exit(EXIT_FAILURE);
}

View File

@@ -3,6 +3,7 @@
#include <string.h> #include <string.h>
#include <czmq.h> #include <czmq.h>
#include <zactor.h> #include <zactor.h>
#include <zmsg.h>
#include <guestfs.h> #include <guestfs.h>
#include "libguestfs-inspect.h" #include "libguestfs-inspect.h"
@@ -28,6 +29,7 @@ static int count_mountpoints(char *const *argv) {
static void cat_file(guestfs_h *g, char *file_path, char **file_content, size_t *file_size) { static void cat_file(guestfs_h *g, char *file_path, char **file_content, size_t *file_size) {
if (guestfs_is_file_opts(g, file_path, GUESTFS_IS_FILE_OPTS_FOLLOWSYMLINKS, 1, -1) > 0) { if (guestfs_is_file_opts(g, file_path, GUESTFS_IS_FILE_OPTS_FOLLOWSYMLINKS, 1, -1) > 0) {
printf("path:: %s\n", file_path);
(*file_content) = guestfs_read_file(g, file_path, file_size); (*file_content) = guestfs_read_file(g, file_path, file_size);
if ((*file_content) == NULL) { if ((*file_content) == NULL) {
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
@@ -36,32 +38,29 @@ static void cat_file(guestfs_h *g, char *file_path, char **file_content, size_t
return; return;
} }
static void init_guestfs(guestfs_h **g, char *disk_path) { static void init_guestfs(guestfs_h *g, char *disk_path) {
char **roots, **mountpoints; char **roots, **mountpoints;
char *root; char *root;
size_t i, j; size_t i, j;
//char *file_content;
//size_t file_size;
// Create a connection handle // Create a connection handle
(*g) = guestfs_create(); g = guestfs_create();
if (g == NULL) { if (g == NULL) {
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
// Adding disk_path to connection handle // Adding disk_path to connection handle
if (guestfs_add_drive_opts((*g), disk_path, GUESTFS_ADD_DRIVE_OPTS_READONLY, 1, -1) == -1) { if (guestfs_add_drive_opts(g, disk_path, GUESTFS_ADD_DRIVE_OPTS_READONLY, 1, -1) == -1) {
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
// Launching connection handle // Launching connection handle
if (guestfs_launch((*g)) == -1) { if (guestfs_launch(g) == -1) {
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
// Pulling rootfs information // Pulling rootfs information
roots = guestfs_inspect_os((*g)); roots = guestfs_inspect_os(g);
if (roots == NULL) { if (roots == NULL) {
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
@@ -69,14 +68,14 @@ static void init_guestfs(guestfs_h **g, char *disk_path) {
// Looping through roots to mount mountpoints // Looping through roots to mount mountpoints
for (j = 0; roots[j] != NULL; j++) { for (j = 0; roots[j] != NULL; j++) {
root = roots[j]; root = roots[j];
mountpoints = guestfs_inspect_get_mountpoints((*g), root); mountpoints = guestfs_inspect_get_mountpoints(g, root);
if (mountpoints == NULL) { if (mountpoints == NULL) {
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
// Sorting mountpoints to be in {'${device_path}', '${mount_path}'} format // Sorting mountpoints to be in {'${device_path}', '${mount_path}'} format
qsort(mountpoints, count_mountpoints(mountpoints) / 2, 2 * sizeof (char*), compare_key_len); qsort(mountpoints, count_mountpoints(mountpoints) / 2, 2 * sizeof (char*), compare_key_len);
for (i = 0; mountpoints[i] != NULL; i += 2) { for (i = 0; mountpoints[i] != NULL; i += 2) {
guestfs_mount_ro((*g), mountpoints[i+1], mountpoints[i]); guestfs_mount_ro(g, mountpoints[i+1], mountpoints[i]);
free(mountpoints[i]); free(mountpoints[i]);
free(mountpoints[i+1]); free(mountpoints[i+1]);
} }
@@ -84,14 +83,15 @@ static void init_guestfs(guestfs_h **g, char *disk_path) {
free(root); free(root);
} }
free(roots); free(roots);
printf("Finished initializing guestfs\n");
} }
static void *worker_task(zsock_t *pipe, char *disk_path) { static void *worker_task(zsock_t *pipe, char *disk_path) {
guestfs_h *g = NULL; guestfs_h *g = NULL;
init_guestfs(&g, disk_path); //init_guestfs(g, disk_path);
/*
// ZeroMQ Opens here // ZeroMQ Opens here
zsock_signal(pipe, 0); zsock_signal(pipe, 0);
while (true) { while (true) {
@@ -102,12 +102,42 @@ static void *worker_task(zsock_t *pipe, char *disk_path) {
// Process message // Process message
// do something here // do something here
printf("Received a message in the worker\n");
printf("Size: %zu\tContent size: %zu\n", zmsg_size(msg), zmsg_content_size(msg));
struct guestfs_inpsect_command *command = zmsg_to_command(msg);
zmsg_t *reply = zmsg_new();
if (!reply) {
printf("wuddahec\n");
exit(EXIT_FAILURE);
}
switch (command->command) {
case GUESTFS_COMMAND_LS:
zmsg_pushstr(reply, "From worker!");
break;
case GUESTFS_COMMAND_CAT:
char *res;
size_t s;
puts("catting file...");
//cat_file(g, command->args.cat.paths[0], &res, &s);
init_guestfs(g, disk_path);
printf("STATUS::%d\n", guestfs_get_state(g));
if(guestfs_is_file_opts(g, "/etc/os-release", GUESTFS_IS_FILE_OPTS_FOLLOWSYMLINKS, 1, -1) > 0) {
res = guestfs_read_file(g, "/etc/os-release", &s);
}
puts("Done catting file contents...");
zmsg_addmem(reply, res, s);
break;
}
// Sending reply // Sending reply
zmsg_send(&msg, pipe); zmsg_send(&reply, pipe);
command_destroy(&command);
zmsg_destroy(&msg); zmsg_destroy(&msg);
zmsg_destroy(&reply);
} }
*/
guestfs_close(g); guestfs_close(g);
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
@@ -119,45 +149,17 @@ int main(int argc, char **argv) {
return EXIT_FAILURE; return EXIT_FAILURE;
} }
const int worker_count = argc - 1; char *path = strtok(argv[1], ":");
char *name = strtok(NULL, ":");
// One worker per disk image. printf("name: '%s'\n", name);
struct {
char *name;
zactor_t *worker;
} worker_map[worker_count];
for (int i = 0; i < worker_count; i++) {
char *path = strtok(argv[i+1], ":");
worker_map[i].name = strtok(NULL, ":");
worker_map[i].worker = zactor_new(worker_task, path);
}
char *ep = endpoint(); char *ep = endpoint();
zsock_t *frontend = zsock_new_router(ep); printf("ep: %s\n", ep);
zsock_t *worker = zsock_new_rep(ep);
free(ep); free(ep);
while (true) { worker_task(worker, path);
zmsg_t *msg = zmsg_recv(frontend);
if (!msg) break;
// Find the worker with the given name.
zactor_t *worker = NULL;
struct guestfs_inpsect_command *cmd = (struct guestfs_inpsect_command *) zmsg_last(msg);
for (int i = 0; i < worker_count; i++) {
if (STREQ(cmd->name, worker_map[i].name)) {
worker = worker_map[i].name;
break;
}
}
if (worker) {
zmsg_send(&msg, zactor_sock(worker));
} else {
// The name specified does not exist.
printf("There is no drive with the name %s\n.", cmd->name);
}
}
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }

View File

@@ -1,4 +1,6 @@
enum guestfs_inspect_command { #include <czmq.h>
enum guestfs_inspect_command_const {
GUESTFS_COMMAND_LS, GUESTFS_COMMAND_LS,
/* GUESTFS_COMMAND_TOUCH, */ /* GUESTFS_COMMAND_TOUCH, */
/* GUESTFS_COMMAND_MKDIR, */ /* GUESTFS_COMMAND_MKDIR, */
@@ -6,19 +8,21 @@ enum guestfs_inspect_command {
}; };
struct guestfs_ls_args { struct guestfs_ls_args {
size_t path_length; size_t paths_length;
char path[]; char **paths;
};
struct guestfs_cat_args {
size_t paths_length;
char **paths;
}; };
struct guestfs_cat_args {};
struct guestfs_inpsect_command { struct guestfs_inpsect_command {
enum guestfs_inspect_command command; char *name;
enum guestfs_inspect_command_const command;
union { union {
struct guestfs_ls_args ls; struct guestfs_ls_args ls;
struct guestfs_cat_args cat; struct guestfs_cat_args cat;
} args; } args;
size_t name_length;
char name[];
}; };
static char *endpoint() { static char *endpoint() {
@@ -29,3 +33,84 @@ static char *endpoint() {
return res; return res;
} }
static zmsg_t *command_to_zmsg(struct guestfs_inpsect_command *command) {
zmsg_t *res = zmsg_new();
zmsg_addstr(res, command->name);
zmsg_addmem(res, &(command->command), sizeof(command->command));
switch (command->command) {
case GUESTFS_COMMAND_LS:
zmsg_addmem(res, &(command->args.ls.paths_length), sizeof(command->args.ls.paths_length));
for (size_t i = 0; i < (command->args.ls.paths_length); i++) {
zmsg_addstr(res, command->args.ls.paths[i]);
}
break;
case GUESTFS_COMMAND_CAT:
zmsg_addmem(res, &(command->args.cat.paths_length), sizeof(command->args.cat.paths_length));
for (size_t i = 0; i < (command->args.cat.paths_length); i++) {
zmsg_addstr(res, command->args.cat.paths[i]);
}
break;
}
return res;
}
static struct guestfs_inpsect_command *zmsg_to_command(zmsg_t *msg) {
struct guestfs_inpsect_command *res = malloc(sizeof(struct guestfs_inpsect_command));
res->name = zmsg_popstr(msg);
zframe_t *next = zmsg_pop(msg);
res->command = *(zframe_data(next));
free(next);
switch (res->command) {
case GUESTFS_COMMAND_LS:
next = zmsg_pop(msg);
res->args.ls.paths_length = *(zframe_data(next));
free(next);
res->args.ls.paths = calloc(res->args.ls.paths_length, sizeof(char *));
for (size_t i = 0; i < res->args.ls.paths_length; i++) {
res->args.ls.paths[i] = zmsg_popstr(msg);
}
break;
case GUESTFS_COMMAND_CAT:
next = zmsg_pop(msg);
res->args.cat.paths_length = *(zframe_data(next));
free(next);
res->args.cat.paths = calloc(res->args.cat.paths_length, sizeof(char *));
for (size_t i = 0; i < res->args.cat.paths_length; i++) {
res->args.cat.paths[i] = zmsg_popstr(msg);
}
break;
}
return res;
}
static void command_destroy(struct guestfs_inpsect_command **c) {
free((*c)->name);
switch ((*c)->command) {
case GUESTFS_COMMAND_LS:
for (size_t i = 0; i < (*c)->args.ls.paths_length; i++) {
free((*c)->args.ls.paths[i]);
}
free((*c)->args.ls.paths);
break;
case GUESTFS_COMMAND_CAT:
for (size_t i = 0; i < (*c)->args.cat.paths_length; i++) {
free((*c)->args.cat.paths[i]);
}
free((*c)->args.cat.paths);
break;
}
free(*c);
*c = NULL;
}