From 83f62dc21789b01db98af0c6c57f877670d949ed Mon Sep 17 00:00:00 2001 From: Robby Zambito Date: Sun, 3 Mar 2024 15:22:36 -0500 Subject: [PATCH 01/11] Remove router loop --- guestfs-inspectd.c | 47 +++++----------------------------------------- 1 file changed, 5 insertions(+), 42 deletions(-) diff --git a/guestfs-inspectd.c b/guestfs-inspectd.c index 4bf816c..cf7d713 100644 --- a/guestfs-inspectd.c +++ b/guestfs-inspectd.c @@ -134,54 +134,17 @@ int main(int argc, char **argv) { return EXIT_FAILURE; } - const int worker_count = argc - 1; - - // One worker per disk image. - 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, ":"); - printf("name: %s\n", worker_map[i].name); - worker_map[i].worker = zactor_new(worker_task, path); - } + char *path = strtok(argv[1], ":"); + char *name = strtok(NULL, ":"); + printf("name: '%s'\n", name); char *ep = endpoint(); printf("ep: %s\n", ep); - zsock_t *frontend = zsock_new_router(ep); + zsock_t *worker = zsock_new_rep(ep); free(ep); - while (true) { - zmsg_t *msg = zmsg_recv(frontend); - printf("Received a message in the router\n"); - if (!msg) break; + worker_task(worker, path); - // 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; - } - } - - // TODO: REMOVE - if (!worker) { - worker = worker_map[0].worker; - } - // TODO: REMOVE - - 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; } From 859ee900489ef315533e34f5f02fee8f86b72b96 Mon Sep 17 00:00:00 2001 From: Robby Zambito Date: Sun, 3 Mar 2024 15:34:51 -0500 Subject: [PATCH 02/11] Send message from daemon to client --- guestfs-inspectd.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/guestfs-inspectd.c b/guestfs-inspectd.c index cf7d713..bc87f56 100644 --- a/guestfs-inspectd.c +++ b/guestfs-inspectd.c @@ -113,15 +113,18 @@ static void *worker_task(zsock_t *pipe, char *disk_path) { // Sending reply /* zmsg_send(&msg, pipe); */ - /* zstr_send(pipe, ); */ + zmsg_t *reply = zmsg_new(); if (!reply) { printf("wuddahec\n"); exit(EXIT_FAILURE); } + zmsg_pushstr(reply, "From worker!"); + + zmsg_send(&reply, pipe); zmsg_destroy(&msg); - + zmsg_destroy(&reply); } guestfs_close(g); From 9422f77119eef3a19d8cf017a225e0e3f31cbd21 Mon Sep 17 00:00:00 2001 From: Robby Zambito Date: Sun, 3 Mar 2024 15:41:10 -0500 Subject: [PATCH 03/11] Update struct to use pointers instead of flexible length structs --- libguestfs-inspect.h | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/libguestfs-inspect.h b/libguestfs-inspect.h index 9cd0870..c48ff7e 100644 --- a/libguestfs-inspect.h +++ b/libguestfs-inspect.h @@ -6,19 +6,17 @@ enum guestfs_inspect_command_const { }; struct guestfs_ls_args { - size_t path_length; - char path[]; + char **paths; }; struct guestfs_cat_args {}; struct guestfs_inpsect_command { + char *name; enum guestfs_inspect_command_const command; union { struct guestfs_ls_args ls; struct guestfs_cat_args cat; } args; - size_t name_length; - char name[]; }; static char *endpoint() { From 8a814b1d939e4a6bfb4da520075ae0475cb088c1 Mon Sep 17 00:00:00 2001 From: Robby Zambito Date: Sun, 3 Mar 2024 15:46:18 -0500 Subject: [PATCH 04/11] Add basic serialization of the message --- libguestfs-inspect.h | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/libguestfs-inspect.h b/libguestfs-inspect.h index c48ff7e..7f8c02a 100644 --- a/libguestfs-inspect.h +++ b/libguestfs-inspect.h @@ -1,3 +1,5 @@ +#include + enum guestfs_inspect_command_const { GUESTFS_COMMAND_LS, /* GUESTFS_COMMAND_TOUCH, */ @@ -6,6 +8,7 @@ enum guestfs_inspect_command_const { }; struct guestfs_ls_args { + size_t paths_length; char **paths; }; struct guestfs_cat_args {}; @@ -27,3 +30,22 @@ static char *endpoint() { return res; } +static zmsg_t *command_to_zmsg(struct guestfs_inpsect_command *command) { + zmsg_t *res = zmsg_new(); + + zmsg_pushstr(res, command->name); + zmsg_pushmem(res, &(command->command), sizeof(command->command)); + + switch (command->command) { + case GUESTFS_COMMAND_LS: + zmsg_pushmem(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_pushstr(res, command->args.ls.paths[i]); + } + break; + } + + return res; +} + From a4b56107aacd9acdc99b9d751f4d4f5499b43dfe Mon Sep 17 00:00:00 2001 From: Robby Zambito Date: Sun, 3 Mar 2024 15:46:18 -0500 Subject: [PATCH 05/11] Add deserialize function --- libguestfs-inspect.h | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/libguestfs-inspect.h b/libguestfs-inspect.h index 7f8c02a..e156b18 100644 --- a/libguestfs-inspect.h +++ b/libguestfs-inspect.h @@ -49,3 +49,26 @@ static zmsg_t *command_to_zmsg(struct guestfs_inpsect_command *command) { 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; + } + return res; +} From 16e95af02a64e00cafe1c465c265c4e8cde48d7c Mon Sep 17 00:00:00 2001 From: Robby Zambito Date: Sun, 3 Mar 2024 16:09:58 -0500 Subject: [PATCH 06/11] Serialize struct from the client --- guestfs-inspect.c | 28 +++++++++++++++++++++------- guestfs-inspectd.c | 3 +-- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/guestfs-inspect.c b/guestfs-inspect.c index d899d4e..1fd4a80 100644 --- a/guestfs-inspect.c +++ b/guestfs-inspect.c @@ -8,7 +8,7 @@ #define STREQ(a, b) (strcmp((a), (b)) == 0) void print_help(char *); -enum guestfs_inpsect_command_const parse_command(char *); +enum guestfs_inspect_command_const parse_command(char *); int main(int argc, char **argv) { if (argc == 1) { @@ -20,17 +20,23 @@ int main(int argc, char **argv) { zsock_t *daemon = zsock_new_req(ep); free(ep); - struct guestfs_inpsect_command *command = malloc(80); + 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]); */ - /* command->args.ls = 5; */ + command->command = parse_command(argv[2]); + 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]); - zsock_send(daemon, "b", command, 80); + zmsg_t *msg = command_to_zmsg(command); + zmsg_send(&msg, daemon); - zmsg_t *msg = zmsg_recv(daemon); + zmsg_t *rep = zmsg_recv(daemon); char *res = zmsg_popstr(msg); printf("Res:\n%s\n", res); - zmsg_destroy(msg); + + zmsg_destroy(&msg); free(res); return EXIT_SUCCESS; @@ -42,3 +48,11 @@ void print_help(char *name) { printf(" ls \n"); printf(" cat \n"); } + +enum guestfs_inspect_command_const parse_command(char *input) { + if (STREQ(input, "ls")) { + return GUESTFS_COMMAND_LS; + } + + exit(EXIT_FAILURE); +} diff --git a/guestfs-inspectd.c b/guestfs-inspectd.c index bc87f56..50a619a 100644 --- a/guestfs-inspectd.c +++ b/guestfs-inspectd.c @@ -105,8 +105,7 @@ static void *worker_task(zsock_t *pipe, char *disk_path) { 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 = malloc(80); - memcpy(command, zmsg_last(msg), 80); + struct guestfs_inpsect_command *command = zmsg_to_command(msg); printf("Name: %s\n", command->name); free(command); From 32a03a3895728dc499f94e39d63ec21feae770d8 Mon Sep 17 00:00:00 2001 From: Robby Zambito Date: Sun, 3 Mar 2024 16:18:11 -0500 Subject: [PATCH 07/11] Add message to show when the daemon is ready --- guestfs-inspectd.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/guestfs-inspectd.c b/guestfs-inspectd.c index 50a619a..452b56f 100644 --- a/guestfs-inspectd.c +++ b/guestfs-inspectd.c @@ -85,6 +85,8 @@ static void init_guestfs(guestfs_h *g, char *disk_path) { free(root); } free(roots); + + printf("Finished initializing guestfs\n"); } static void *worker_task(zsock_t *pipe, char *disk_path) { From b99513c8b3a1a052a95ac33981de5447f59578bb Mon Sep 17 00:00:00 2001 From: Robby Zambito Date: Sun, 3 Mar 2024 16:31:58 -0500 Subject: [PATCH 08/11] 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 --- guestfs-inspectd.c | 1 + libguestfs-inspect.h | 8 ++++---- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/guestfs-inspectd.c b/guestfs-inspectd.c index 452b56f..e5f74fc 100644 --- a/guestfs-inspectd.c +++ b/guestfs-inspectd.c @@ -109,6 +109,7 @@ static void *worker_task(zsock_t *pipe, char *disk_path) { printf("Size: %zu\tContent size: %zu\n", zmsg_size(msg), zmsg_content_size(msg)); struct guestfs_inpsect_command *command = zmsg_to_command(msg); printf("Name: %s\n", command->name); + printf("path: %s\n", command->args.ls.paths[0]); free(command); diff --git a/libguestfs-inspect.h b/libguestfs-inspect.h index e156b18..00df786 100644 --- a/libguestfs-inspect.h +++ b/libguestfs-inspect.h @@ -33,15 +33,15 @@ static char *endpoint() { static zmsg_t *command_to_zmsg(struct guestfs_inpsect_command *command) { zmsg_t *res = zmsg_new(); - zmsg_pushstr(res, command->name); - zmsg_pushmem(res, &(command->command), sizeof(command->command)); + zmsg_addstr(res, command->name); + zmsg_addmem(res, &(command->command), sizeof(command->command)); switch (command->command) { case GUESTFS_COMMAND_LS: - zmsg_pushmem(res, &(command->args.ls.paths_length), sizeof(command->args.ls.paths_length)); + 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_pushstr(res, command->args.ls.paths[i]); + zmsg_addstr(res, command->args.ls.paths[i]); } break; } From bdf9fc7718c5a4fe7ec1f6f3579a62da29ab9d56 Mon Sep 17 00:00:00 2001 From: Robby Zambito Date: Sun, 3 Mar 2024 16:35:14 -0500 Subject: [PATCH 09/11] Fix error reading from the wrong msg object --- guestfs-inspect.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/guestfs-inspect.c b/guestfs-inspect.c index 1fd4a80..736d65a 100644 --- a/guestfs-inspect.c +++ b/guestfs-inspect.c @@ -33,7 +33,7 @@ int main(int argc, char **argv) { zmsg_send(&msg, daemon); zmsg_t *rep = zmsg_recv(daemon); - char *res = zmsg_popstr(msg); + char *res = zmsg_popstr(rep); printf("Res:\n%s\n", res); zmsg_destroy(&msg); From 2b4aa5cbafab63d2d2a0b28c884dfabd0502ad0c Mon Sep 17 00:00:00 2001 From: Robby Zambito Date: Sun, 3 Mar 2024 16:35:14 -0500 Subject: [PATCH 10/11] Add message to show the number of paths sent to the worker to ls --- guestfs-inspectd.c | 1 + 1 file changed, 1 insertion(+) diff --git a/guestfs-inspectd.c b/guestfs-inspectd.c index e5f74fc..e94b899 100644 --- a/guestfs-inspectd.c +++ b/guestfs-inspectd.c @@ -109,6 +109,7 @@ static void *worker_task(zsock_t *pipe, char *disk_path) { printf("Size: %zu\tContent size: %zu\n", zmsg_size(msg), zmsg_content_size(msg)); struct guestfs_inpsect_command *command = zmsg_to_command(msg); printf("Name: %s\n", command->name); + printf("paths: %zu\n", command->args.ls.paths_length); printf("path: %s\n", command->args.ls.paths[0]); free(command); From e94211e5f4512dd0bc7e53444bd8e68a327597da Mon Sep 17 00:00:00 2001 From: Robby Zambito Date: Sun, 3 Mar 2024 16:51:03 -0500 Subject: [PATCH 11/11] Cleanup socket --- guestfs-inspect.c | 1 + 1 file changed, 1 insertion(+) diff --git a/guestfs-inspect.c b/guestfs-inspect.c index 736d65a..d30aab2 100644 --- a/guestfs-inspect.c +++ b/guestfs-inspect.c @@ -39,6 +39,7 @@ int main(int argc, char **argv) { zmsg_destroy(&msg); free(res); + zsock_destroy(&daemon); return EXIT_SUCCESS; }