From 16e95af02a64e00cafe1c465c265c4e8cde48d7c Mon Sep 17 00:00:00 2001 From: Robby Zambito Date: Sun, 3 Mar 2024 16:09:58 -0500 Subject: [PATCH] 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);