This commit is contained in:
Pin
2024-03-02 23:50:20 -05:00
parent 991ffb5224
commit ac70e240c8

View File

@@ -9,7 +9,7 @@
#define STREQ(a, b) (strcmp((a), (b)) == 0) #define STREQ(a, b) (strcmp((a), (b)) == 0)
static void *worker_task(char*); static void *worker_task(zsock_t*, char*);
char *endpoint(void); char *endpoint(void);
@@ -25,84 +25,95 @@ char *endpoint() {
} }
static int compare_key_len(const void *p1, const void *p2) { static int compare_key_len(const void *p1, const void *p2) {
const char *key1 = *(char* const*) p1; const char *key1 = *(char* const*) p1;
const char *key2 = *(char* const*) p2; const char *key2 = *(char* const*) p2;
return strlen(key1) - strlen(key2); return strlen(key1) - strlen(key2);
} }
static int count_mountpoints(char *const *argv) { static int count_mountpoints(char *const *argv) {
size_t c; size_t c;
for (c = 0; argv[c]; c++) {} for (c = 0; argv[c]; c++) {}
return c; return c;
} }
static void *worker_task(char *disk_path) { static void *worker_task(zsock_t *pipe, char *disk_path) {
guestfs_h *g; guestfs_h *g;
char **roots, **mountpoints; char **roots, **mountpoints;
char *root; char *root;
size_t i, j; size_t i, j;
//char *file_content; //char *file_content;
//size_t file_size; //size_t file_size;
// Create a connection handle // Create a connection handle
g = guestfs_create(); g = guestfs_create();
if (g == NULL) { if (g == NULL) {
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
// Adding disk_path to connection handle // Adding disk_path to connection handle
if (guestfs_add_drive_opts(g, disk_path, if (guestfs_add_drive_opts(g, disk_path, GUESTFS_ADD_DRIVE_OPTS_READONLY, 1, -1) == -1) {
GUESTFS_ADD_DRIVE_OPTS_READONLY, 1, exit(EXIT_FAILURE);
-1) == -1) { }
exit(EXIT_FAILURE);
}
// Launching connection handle // Launching connection handle
if (guestfs_launch(g) == -1) { if (guestfs_launch(g) == -1) {
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
// Pulling rootfs information // Pulling rootfs information
roots = guestfs_inspect_os(g); roots = guestfs_inspect_os(g);
if (roots == NULL) { if (roots == NULL) {
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
// Looping through roots to mount mountpoints // Looping through roots to mount mountpoints
for (j = 0; roots[j] != NULL; j++) { for (j = 0; roots[j] != NULL; j++) {
root = roots[j]; root = roots[j];
mountpoints = guestfs_inspect_get_mountpoints(g, root); mountpoints = guestfs_inspect_get_mountpoints(g, root);
if (mountpoints == NULL) { if (mountpoints == NULL) {
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
// Sorting mountpoints to be in {'${device_path}', '${mount_path}'} format // Sorting mountpoints to be in {'${device_path}', '${mount_path}'} format
qsort(mountpoints, count_mountpoints(mountpoints) / 2, 2 * sizeof (char*), compare_key_len); qsort(mountpoints, count_mountpoints(mountpoints) / 2, 2 * sizeof (char*), compare_key_len);
for (i = 0; mountpoints[i] != NULL; i += 2) { 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]);
free(mountpoints[i+1]); free(mountpoints[i+1]);
} }
free(mountpoints); free(mountpoints);
free(root); free(root);
} }
// ZeroMQ Opens here // ZeroMQ Opens here
for (;;) { zsock_signal(pipe, 0);
while (true) {
zmsg_t *message = zmsg_recv(pipe);
if (message == NULL) {
break;
}
// Process message
} // do something here
/*if (guestfs_is_file_opts(g, file_path, GUESTFS_IS_FILE_OPTS_FOLLOWSYMLINKS, 1, -1) > 0) {
printf("--- %s ---\n", file_path); // Sending reply
file_content = guestfs_read_file(g, file_path, &file_size); zmsg_t *reply = zmsg_new();
if (file_content == NULL) { zmsg_addstr(reply, "Reply from worker");
exit(EXIT_FAILURE); zmsg_send(&reply, pipe);
} zmsg_destroy(&message);
printf("%s\n", file_content); }
free(file_content); /*if (guestfs_is_file_opts(g, file_path, GUESTFS_IS_FILE_OPTS_FOLLOWSYMLINKS, 1, -1) > 0) {
}*/ printf("--- %s ---\n", file_path);
free(roots); file_content = guestfs_read_file(g, file_path, &file_size);
guestfs_close(g); if (file_content == NULL) {
exit(EXIT_SUCCESS); 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) { int main(int argc, char **argv) {