Files
guestfs-inspect/guestfs-inspectd.c

191 lines
4.5 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <czmq.h>
#include <zmsg.h>
#include <zactor.h>
#include <guestfs.h>
#include "libguestfs-inspect.h"
#define STREQ(a, b) (strcmp((a), (b)) == 0)
static void worker_task(zsock_t*, char*);
static int compare_key_len(const void*, const void*);
static int count_mountpoints(char *const *argv);
static int compare_key_len(const void *p1, const void *p2) {
const char *key1 = *(char* const*) p1;
const char *key2 = *(char* const*) p2;
return strlen(key1) - strlen(key2);
}
static int count_mountpoints(char *const *argv) {
size_t c;
for (c = 0; argv[c]; c++) {}
return c;
}
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) {
printf("path:: %s\n", file_path);
(*file_content) = guestfs_read_file(g, file_path, file_size);
if ((*file_content) == NULL) {
exit(EXIT_FAILURE);
}
}
return;
}
static guestfs_h* init_guestfs(char *disk_path) {
guestfs_h *g = NULL;
char **roots, **mountpoints;
char *root;
size_t i, j;
// Create a connection handle
g = guestfs_create();
if (g == NULL) {
exit(EXIT_FAILURE);
}
// Adding disk_path to connection handle
if (guestfs_add_drive_opts(g, disk_path, GUESTFS_ADD_DRIVE_OPTS_READONLY, 1, -1) == -1) {
exit(EXIT_FAILURE);
}
// Launching connection handle
if (guestfs_launch(g) == -1) {
exit(EXIT_FAILURE);
}
// Pulling rootfs information
roots = guestfs_inspect_os(g);
if (roots == NULL) {
exit(EXIT_FAILURE);
}
// Looping through roots to mount mountpoints
for (j = 0; roots[j] != NULL; j++) {
root = roots[j];
mountpoints = guestfs_inspect_get_mountpoints(g, root);
if (mountpoints == NULL) {
exit(EXIT_FAILURE);
}
// Sorting mountpoints to be in {'${device_path}', '${mount_path}'} format
qsort(mountpoints, count_mountpoints(mountpoints) / 2, 2 * sizeof (char*), compare_key_len);
for (i = 0; mountpoints[i] != NULL; i += 2) {
guestfs_mount_ro(g, mountpoints[i+1], mountpoints[i]);
free(mountpoints[i]);
free(mountpoints[i+1]);
}
free(mountpoints);
free(root);
}
free(roots);
printf("Finished initializing guestfs\n");
return g;
}
static void worker_task(zsock_t *pipe, char *disk_path) {
guestfs_h *g;
g = init_guestfs(disk_path);
// ZeroMQ Opens here
zsock_signal(pipe, 0);
while (true) {
zmsg_t *msg = zmsg_recv(pipe);
if (!msg) {
break;
}
// Process message
// 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 = guestfs_inspect_zmsg_to_command(msg);
printf("Size OF:: %zu\n", (sizeof(struct guestfs_inpsect_command)));
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! -- not implemented");
break;
case GUESTFS_COMMAND_CAT:
char *res;
size_t s;
puts("catting file...");
cat_file(g, command->args.cat.paths[0], &res, &s);
puts("Done catting file contents...");
zmsg_addmem(reply, res, s);
free(res);
break;
}
// Sending reply
zmsg_send(&reply, pipe);
guestfs_inspect_command_destroy(&command);
zmsg_destroy(&msg);
zmsg_destroy(&reply);
}
guestfs_close(g);
exit(EXIT_SUCCESS);
}
int main(int argc, char **argv) {
if (argc < 2) {
printf("Usage: %s <disk-path>:<name> ...\n", argv[0]);
return EXIT_FAILURE;
}
char *path = strtok(argv[1], ":");
char *name = strtok(NULL, ":");
printf("name: '%s'\n", name);
zactor_t *worker = zactor_new(worker_task, path);
char *ep = guestfs_inspect_endpoint();
printf("ep: %s\n", ep);
zsock_t *frontend = zsock_new(ZMQ_ROUTER);
zsock_bind(frontend, ep);
free(ep);
while (true) {
zmsg_t *msg = zmsg_recv(frontend);
if (!msg) {
break;
}
// reply id
char *id = zmsg_popstr(msg);
// null frame
zmsg_pop(msg);
struct guestfs_inpsect_command *c = guestfs_inspect_zmsg_to_command(msg);
if (STREQ(name, c->name)) {
// send request to worker
printf("name equal\n");
zactor_send(worker, &msg);
} else {
printf("name not equal\n");
}
/* zmsg_t *reply = zmsg_recv(frontend); */
}
zactor_destroy(&worker);
return EXIT_SUCCESS;
}