diff --git a/guestfs-inspectd.c b/guestfs-inspectd.c index 5692774..61f1ec8 100644 --- a/guestfs-inspectd.c +++ b/guestfs-inspectd.c @@ -1,15 +1,108 @@ #include #include +#include #include #include #include #define STREQ(a, b) (strcmp((a), (b)) == 0) -void *worker_task; +static void *worker_task(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; + 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 *worker_task(char *disk_path) { + guestfs_h *g; + char **roots, **mountpoints; + char *root; + size_t i, j; + + //char *file_content; + //size_t file_size; + + // 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); + } + + // ZeroMQ Opens here + for (;;) { + + } + /*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); +} + int main(int argc, char **argv) { if (argc < 2) { printf("Usage: %s ...\n", argv[0]); @@ -42,7 +135,7 @@ int main(int argc, char **argv) { // Find the worker with the given name. zactor_t *worker = NULL; for (int i = 0; i < worker_count; i++) { - + } if (worker) { zmsg_send(&msg, zactor_sock(worker)); @@ -53,10 +146,3 @@ int main(int argc, char **argv) { return EXIT_SUCCESS; } -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; -}