Compare commits

...

11 Commits

Author SHA1 Message Date
8be9dfc093 Add a clean target 2024-03-30 16:18:47 -04:00
f11569691f Move build target to first target
This makes it so you can just run make to build both the client and the daemon.
2024-03-30 16:18:33 -04:00
873ed765f4 Do not modify the argument we are given.
By duplicating it, we make it so popping frames off the message does not modify the argument.
This make sure the value remains useful to the caller.
2024-03-07 18:50:47 -05:00
2493f8916a Update usage
Tell users they should specify the name for an image.
2024-03-07 18:42:05 -05:00
bc7ccbd805 Move function implementations out of the header file. 2024-03-06 23:32:07 -05:00
5f98d369fa Prefix libguestfs-inspect functions with guestfs_inspect_.
The functions are also made non-static.
This is in preparation to moving the header function implementations to a .c file.
2024-03-06 23:32:07 -05:00
1d17e047b7 Add header guard to libguestfs-inspect.h 2024-03-06 23:22:29 -05:00
3c9564cb3e Use environment variable for the socket.
The default behavior is to create a socket file at /tmp/guestfs-inspect.sock.
If XDG_RUNTIME_DIR is set, that will be used instead of /tmp.

One could instead specify GUESTFS_INSPECT_ENDPOINT, which requires the full endpoint, as specified by: https://libzmq.readthedocs.io/en/zeromq3-x/zmq_connect.html

You will likely want to use ipc://, but you can probably use tcp:// or (e)pgm:// if that is appropriate.
2024-03-06 23:22:29 -05:00
ba3ac50325 Remove unused include 2024-03-06 19:25:05 -05:00
465d6f0cd4 Set workdir in the Dockerfile 2024-03-06 19:24:34 -05:00
1378a5a31a Free the worker 2024-03-06 19:24:34 -05:00
6 changed files with 138 additions and 99 deletions

View File

@@ -2,3 +2,5 @@ FROM debian
RUN apt update && apt upgrade -y
RUN apt install -y libczmq-dev libguestfs-dev gcc valgrind gdb make pkg-config
WORKDIR /opt

View File

@@ -1,7 +1,10 @@
build: daemon client
daemon:
gcc -ggdb3 -Wall guestfs-inspectd.c -o guestfs-inspectd `pkg-config libguestfs libczmq --cflags --libs`
gcc -ggdb3 -Wall guestfs-inspectd.c libguestfs-inspect.c -o guestfs-inspectd `pkg-config libguestfs libczmq --cflags --libs`
client:
gcc -ggdb3 -Wall guestfs-inspect.c -o guestfs-inspect `pkg-config libguestfs libczmq --cflags --libs`
gcc -ggdb3 -Wall guestfs-inspect.c libguestfs-inspect.c -o guestfs-inspect `pkg-config libguestfs libczmq --cflags --libs`
build: daemon client
clean:
rm guestfs-inspect guestfs-inspectd

View File

@@ -16,7 +16,7 @@ int main(int argc, char **argv) {
return EXIT_SUCCESS;
}
char *ep = endpoint();
char *ep = guestfs_inspect_endpoint();
zsock_t *daemon = zsock_new_req(ep);
free(ep);
@@ -40,7 +40,7 @@ int main(int argc, char **argv) {
break;
}
zmsg_t *msg = command_to_zmsg(command);
zmsg_t *msg = guestfs_inspect_command_to_zmsg(command);
zmsg_send(&msg, daemon);
zmsg_t *rep = zmsg_recv(daemon);
@@ -62,7 +62,7 @@ int main(int argc, char **argv) {
zmsg_destroy(&msg);
zmsg_destroy(&rep);
zsock_destroy(&daemon);
command_destroy(&command);
guestfs_inspect_command_destroy(&command);
return EXIT_SUCCESS;
}

View File

@@ -2,7 +2,6 @@
#include <stdlib.h>
#include <string.h>
#include <czmq.h>
#include <zactor.h>
#include <zmsg.h>
#include <guestfs.h>
@@ -107,7 +106,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 = zmsg_to_command(msg);
struct guestfs_inpsect_command *command = guestfs_inspect_zmsg_to_command(msg);
printf("Size OF:: %zu\n", (sizeof(struct guestfs_inpsect_command)));
zmsg_t *reply = zmsg_new();
if (!reply) {
@@ -133,7 +132,7 @@ static void *worker_task(zsock_t *pipe, char *disk_path) {
// Sending reply
zmsg_send(&reply, pipe);
command_destroy(&command);
guestfs_inspect_command_destroy(&command);
zmsg_destroy(&msg);
zmsg_destroy(&reply);
}
@@ -144,7 +143,7 @@ static void *worker_task(zsock_t *pipe, char *disk_path) {
int main(int argc, char **argv) {
if (argc < 2) {
printf("Usage: %s <disk-path> ...\n", argv[0]);
printf("Usage: %s <disk-path>:<name> ...\n", argv[0]);
return EXIT_FAILURE;
}
@@ -152,12 +151,13 @@ int main(int argc, char **argv) {
char *name = strtok(NULL, ":");
printf("name: '%s'\n", name);
char *ep = endpoint();
char *ep = guestfs_inspect_endpoint();
printf("ep: %s\n", ep);
zsock_t *worker = zsock_new_rep(ep);
free(ep);
worker_task(worker, path);
zsock_destroy(&worker);
zsock_destroy(&worker);
return EXIT_SUCCESS;

114
libguestfs-inspect.c Normal file
View File

@@ -0,0 +1,114 @@
#include <czmq.h>
#include "libguestfs-inspect.h"
char *guestfs_inspect_endpoint() {
const char *guestfs_inspect_endpoint = getenv("GUESTFS_INSPECT_ENDPOINT");
const char *socket_file_name = "/guestfs-inspect.sock";
char *res = NULL;
if (guestfs_inspect_endpoint) {
res = malloc(strlen(guestfs_inspect_endpoint) + 1);
strcpy(res, guestfs_inspect_endpoint);
} else {
const char *xdg_runtime_dir = getenv("XDG_RUNTIME_DIR");
if (xdg_runtime_dir) {
res = malloc(strlen(xdg_runtime_dir) + strlen(socket_file_name) + strlen("ipc://") + 1);
strcpy(res, "ipc://");
strcat(res, xdg_runtime_dir);
strcat(res, socket_file_name);
} else {
const char *tmp_dir = "/tmp";
res = malloc(strlen(tmp_dir) + strlen(socket_file_name) + strlen("ipc://") + 1);
strcpy(res, "ipc://");
strcat(res, tmp_dir);
strcat(res, socket_file_name);
}
}
return res;
}
zmsg_t *guestfs_inspect_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;
}
struct guestfs_inpsect_command *guestfs_inspect_zmsg_to_command(zmsg_t *external_msg) {
// This way we do not modify the argument we are given.
zmsg_t *msg = zmsg_dup(external_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;
}
zmsg_destroy(&msg);
return res;
}
void guestfs_inspect_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;
}

View File

@@ -1,3 +1,6 @@
#ifndef LIBGUESTFS_INSPECT_H
#define LIBGUESTFS_INSPECT_H
#include <czmq.h>
enum guestfs_inspect_command_const {
@@ -25,92 +28,9 @@ struct guestfs_inpsect_command {
} args;
};
static char *endpoint() {
// TODO: This should be based on GUESTFS_INSPECT_ENDPOINT, or XDG_RUNTIME_DIR if the former is not set, and default to a sensible path if neither are set.
const char* ep = "ipc:///tmp/guestfs-inspect.sock";
char *res = malloc(strlen(ep) + 1);
strcpy(res, ep);
return res;
}
char *guestfs_inspect_endpoint(void);
zmsg_t *guestfs_inspect_command_to_zmsg(struct guestfs_inpsect_command *command);
struct guestfs_inpsect_command *guestfs_inspect_zmsg_to_command(zmsg_t *msg);
void guestfs_inspect_command_destroy(struct guestfs_inpsect_command **c);
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;
}
#endif // LIBGUESTFS_INSPECT_H