Compare commits
8 Commits
cbb0a440e9
...
d01a6e593a
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d01a6e593a | ||
|
|
2505662cfb | ||
| d09b118ad7 | |||
| ce8d3843a3 | |||
| 2bdf450d65 | |||
| 73d8b6716b | |||
| 26c742f017 | |||
| 052aa99a6c |
@@ -1,3 +1,4 @@
|
||||
FROM debian
|
||||
|
||||
RUN apt update && apt upgrade -y && apt install -y libczmq-dev libguestfs-dev gcc valgrind gdb make
|
||||
RUN apt update && apt upgrade -y
|
||||
RUN apt install -y libczmq-dev libguestfs-dev gcc valgrind gdb make pkg-config
|
||||
|
||||
7
Makefile
7
Makefile
@@ -1,2 +1,7 @@
|
||||
build:
|
||||
daemon:
|
||||
gcc guestfs-inspectd.c -o guestfs-inspectd `pkg-config libguestfs libczmq --cflags --libs`
|
||||
|
||||
client:
|
||||
gcc guestfs-inspect.c -o guestfs-inspect `pkg-config libguestfs libczmq --cflags --libs`
|
||||
|
||||
build: daemon client
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <czmq.h>
|
||||
#include <guestfs.h>
|
||||
|
||||
#include "libguestfs-inspect.h"
|
||||
|
||||
#define STREQ(a, b) (strcmp((a), (b)) == 0)
|
||||
|
||||
void print_help(char *);
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
if (argc == 1) {
|
||||
print_help(argv[0]);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
char *ep = endpoint();
|
||||
zsock_t *daemon = zsock_new_req(ep);
|
||||
free(ep);
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
void print_help(char *name) {
|
||||
printf("Usage: %s [name] [command] <arguments>\n", name);
|
||||
printf("Commands:\n");
|
||||
printf(" ls <path>\n");
|
||||
printf(" cat <path>\n");
|
||||
}
|
||||
|
||||
@@ -11,19 +11,9 @@
|
||||
|
||||
static void *worker_task(zsock_t*, char*);
|
||||
|
||||
char *endpoint(void);
|
||||
|
||||
static int compare_key_len(const void*, const void*);
|
||||
static int count_mountpoints(char *const *argv);
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
static int compare_key_len(const void *p1, const void *p2) {
|
||||
const char *key1 = *(char* const*) p1;
|
||||
const char *key2 = *(char* const*) p2;
|
||||
@@ -36,8 +26,17 @@ static int count_mountpoints(char *const *argv) {
|
||||
return c;
|
||||
}
|
||||
|
||||
static void *worker_task(zsock_t *pipe, char *disk_path) {
|
||||
guestfs_h *g;
|
||||
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) {
|
||||
(*file_content) = guestfs_read_file(g, file_path, file_size);
|
||||
if ((*file_content) == NULL) {
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
static void init_guestfs(guestfs_h **g, char *disk_path) {
|
||||
char **roots, **mountpoints;
|
||||
char *root;
|
||||
size_t i, j;
|
||||
@@ -46,23 +45,23 @@ static void *worker_task(zsock_t *pipe, char *disk_path) {
|
||||
//size_t file_size;
|
||||
|
||||
// Create a connection handle
|
||||
g = guestfs_create();
|
||||
(*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) {
|
||||
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) {
|
||||
if (guestfs_launch((*g)) == -1) {
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
// Pulling rootfs information
|
||||
roots = guestfs_inspect_os(g);
|
||||
roots = guestfs_inspect_os((*g));
|
||||
if (roots == NULL) {
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
@@ -70,26 +69,34 @@ static void *worker_task(zsock_t *pipe, char *disk_path) {
|
||||
// Looping through roots to mount mountpoints
|
||||
for (j = 0; roots[j] != NULL; j++) {
|
||||
root = roots[j];
|
||||
mountpoints = guestfs_inspect_get_mountpoints(g, root);
|
||||
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]);
|
||||
guestfs_mount_ro((*g), mountpoints[i+1], mountpoints[i]);
|
||||
free(mountpoints[i]);
|
||||
free(mountpoints[i+1]);
|
||||
}
|
||||
free(mountpoints);
|
||||
free(root);
|
||||
}
|
||||
free(roots);
|
||||
}
|
||||
|
||||
static void *worker_task(zsock_t *pipe, char *disk_path) {
|
||||
guestfs_h *g = NULL;
|
||||
|
||||
init_guestfs(&g, disk_path);
|
||||
|
||||
/*
|
||||
// ZeroMQ Opens here
|
||||
zsock_signal(pipe, 0);
|
||||
while (true) {
|
||||
zmsg_t *message = zmsg_recv(pipe);
|
||||
if (!message) {
|
||||
zmsg_t *msg = zmsg_recv(pipe);
|
||||
if (!msg) {
|
||||
break;
|
||||
}
|
||||
// Process message
|
||||
@@ -98,18 +105,10 @@ static void *worker_task(zsock_t *pipe, char *disk_path) {
|
||||
|
||||
// Sending reply
|
||||
zmsg_send(&msg, pipe);
|
||||
zmsg_destory(&msg);
|
||||
zmsg_destroy(&msg);
|
||||
}
|
||||
/*if (guestfs_is_file_opts(g, file_path, GUESTFS_IS_FILE_OPTS_FOLLOWSYMLINKS, 1, -1) > 0) {
|
||||
printf("--- %s ---\n", file_path);
|
||||
file_content = guestfs_read_file(g, file_path, &file_size);
|
||||
if (file_content == NULL) {
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
printf("%s\n", file_content);
|
||||
free(file_content);
|
||||
}*/
|
||||
free(roots);
|
||||
*/
|
||||
|
||||
guestfs_close(g);
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
@@ -145,13 +144,18 @@ int main(int argc, char **argv) {
|
||||
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
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;
|
||||
|
||||
@@ -21,3 +21,11 @@ struct guestfs_inpsect_command {
|
||||
char name[];
|
||||
};
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user