Compare commits
11 Commits
46d9500d3a
...
8be9dfc093
| Author | SHA1 | Date | |
|---|---|---|---|
| 8be9dfc093 | |||
| f11569691f | |||
| 873ed765f4 | |||
| 2493f8916a | |||
| bc7ccbd805 | |||
| 5f98d369fa | |||
| 1d17e047b7 | |||
| 3c9564cb3e | |||
| ba3ac50325 | |||
| 465d6f0cd4 | |||
| 1378a5a31a |
@@ -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
|
||||
9
Makefile
9
Makefile
@@ -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
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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
114
libguestfs-inspect.c
Normal 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;
|
||||
}
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user