Compare commits
61 Commits
cbb0a440e9
...
dev
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
dc69f665eb | ||
|
|
ec6fac8537 | ||
|
|
4dd55da7e7 | ||
|
|
8fa0c4b259 | ||
|
|
84fffca8e5 | ||
|
|
e4d58ff94b | ||
|
|
fdd643c528 | ||
|
|
81bc834b26 | ||
| e4d2076fa2 | |||
| 3f8bd314b6 | |||
| 8be9dfc093 | |||
| f11569691f | |||
| 873ed765f4 | |||
| 2493f8916a | |||
| bc7ccbd805 | |||
| 5f98d369fa | |||
| 1d17e047b7 | |||
| 3c9564cb3e | |||
| ba3ac50325 | |||
| 465d6f0cd4 | |||
| 1378a5a31a | |||
|
|
46d9500d3a | ||
|
|
20fc94fb35 | ||
| c6f9204014 | |||
| 6700d98ad6 | |||
| 49b9f43c76 | |||
| ebc07d0cf7 | |||
| 75139f709d | |||
| 1e5357c4cf | |||
| 7d9c451799 | |||
| a7992436b1 | |||
| e94211e5f4 | |||
| 2b4aa5cbaf | |||
| bdf9fc7718 | |||
| b99513c8b3 | |||
| 32a03a3895 | |||
| 16e95af02a | |||
| a4b56107aa | |||
| 8a814b1d93 | |||
| 9422f77119 | |||
| 859ee90048 | |||
| 83f62dc217 | |||
| ea58a6ebd3 | |||
| eaf8dde6c3 | |||
| 26af0a5a5d | |||
| 187acad82a | |||
| d6c57e9e32 | |||
| 07ee70b002 | |||
| d60c0d419b | |||
| ec9a78a459 | |||
| 2c4cb0e235 | |||
| fd2ee16f79 | |||
| 44a5f6773c | |||
|
|
d01a6e593a | ||
|
|
2505662cfb | ||
| d09b118ad7 | |||
| ce8d3843a3 | |||
| 2bdf450d65 | |||
| 73d8b6716b | |||
| 26c742f017 | |||
| 052aa99a6c |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -2,3 +2,4 @@ guestfs-inspect
|
|||||||
guestfs-inspectd
|
guestfs-inspectd
|
||||||
*~
|
*~
|
||||||
*.qcow2
|
*.qcow2
|
||||||
|
venv/
|
||||||
|
|||||||
12
Dockerfile
12
Dockerfile
@@ -1,3 +1,11 @@
|
|||||||
FROM debian
|
FROM debian as builder
|
||||||
|
|
||||||
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
|
||||||
|
|
||||||
|
COPY . /opt
|
||||||
|
WORKDIR /opt
|
||||||
|
|
||||||
|
FROM builder as compile
|
||||||
|
|
||||||
|
RUN make
|
||||||
|
|||||||
19
Makefile
19
Makefile
@@ -1,2 +1,17 @@
|
|||||||
build:
|
build: daemon client
|
||||||
gcc guestfs-inspectd.c -o guestfs-inspectd `pkg-config libguestfs libczmq --cflags --libs`
|
debug: daemon-debug client-debug
|
||||||
|
|
||||||
|
daemon:
|
||||||
|
gcc -Wall guestfs-inspectd.c libguestfs-inspect.c -o guestfs-inspectd `pkg-config libguestfs libczmq --cflags --libs`
|
||||||
|
|
||||||
|
client:
|
||||||
|
gcc -Wall guestfs-inspect.c libguestfs-inspect.c -o guestfs-inspect `pkg-config libguestfs libczmq --cflags --libs`
|
||||||
|
|
||||||
|
daemon-debug:
|
||||||
|
gcc -ggdb3 -Wall guestfs-inspectd.c libguestfs-inspect.c -o guestfs-inspectd `pkg-config libguestfs libczmq --cflags --libs`
|
||||||
|
|
||||||
|
client-debug:
|
||||||
|
gcc -ggdb3 -Wall guestfs-inspect.c libguestfs-inspect.c -o guestfs-inspect `pkg-config libguestfs libczmq --cflags --libs`
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm guestfs-inspect guestfs-inspectd
|
||||||
|
|||||||
17
README.md
Normal file
17
README.md
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
# Edda
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
,ggggggg,
|
||||||
|
,dP""""""Y8b 8I 8I
|
||||||
|
d8' a Y8 8I 8I
|
||||||
|
88 "Y8P' 8I 8I
|
||||||
|
`8baaaa 8I 8I
|
||||||
|
,d8P"""" ,gggg,8I ,gggg,8I ,gggg,gg
|
||||||
|
d8" dP" "Y8I dP" "Y8I dP" "Y8I
|
||||||
|
Y8, i8' ,8I i8' ,8I i8' ,8I
|
||||||
|
`Yba,,_____, ,d8, ,d8b,,d8, ,d8b,,d8, ,d8b,
|
||||||
|
`"Y8888888 P"Y8888P"`Y8P"Y8888P"`Y8P"Y8888P"`Y8
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
21
client.py
Normal file
21
client.py
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
import zmq
|
||||||
|
|
||||||
|
context = zmq.Context()
|
||||||
|
socket = context.socket(zmq.REQ)
|
||||||
|
socket.connect('ipc:///run/user/1000/guestfs-inspect.sock')
|
||||||
|
|
||||||
|
tt = 1
|
||||||
|
tt_size = 1
|
||||||
|
|
||||||
|
message = "test"
|
||||||
|
socket.send(message.encode('utf-8'), flags=zmq.SNDMORE)
|
||||||
|
socket.send(tt.to_bytes(4, 'little'), flags=zmq.SNDMORE)
|
||||||
|
socket.send(tt_size.to_bytes(8, 'little'), flags=zmq.SNDMORE)
|
||||||
|
socket.send('/etc/os-release'.encode('utf-8'), flags=zmq.SNDMORE)
|
||||||
|
socket.send(''.encode('utf-8'))
|
||||||
|
|
||||||
|
message = socket.recv()
|
||||||
|
print(message.decode('utf-8'))
|
||||||
|
|
||||||
|
socket.close()
|
||||||
|
context.destroy()
|
||||||
@@ -0,0 +1,85 @@
|
|||||||
|
#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 *);
|
||||||
|
enum guestfs_inspect_command_const parse_command(const char *);
|
||||||
|
|
||||||
|
int main(int argc, char **argv) {
|
||||||
|
if (argc == 1) {
|
||||||
|
print_help(argv[0]);
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
char *ep = guestfs_inspect_endpoint();
|
||||||
|
zsock_t *daemon = zsock_new_req(ep);
|
||||||
|
free(ep);
|
||||||
|
|
||||||
|
struct guestfs_inpsect_command *command = malloc(sizeof(struct guestfs_inpsect_command));
|
||||||
|
command->name = calloc(strlen(argv[1]) + 1, sizeof(char));
|
||||||
|
strcpy(command->name, argv[1]);
|
||||||
|
command->command = parse_command(argv[2]);
|
||||||
|
|
||||||
|
switch (command->command) {
|
||||||
|
case GUESTFS_COMMAND_LS:
|
||||||
|
command->args.ls.paths_length = 1;
|
||||||
|
command->args.ls.paths = calloc(1, sizeof(char *));
|
||||||
|
command->args.ls.paths[0] = calloc(strlen(argv[3] + 1), sizeof(char));
|
||||||
|
strcpy(command->args.ls.paths[0], argv[3]);
|
||||||
|
break;
|
||||||
|
case GUESTFS_COMMAND_CAT:
|
||||||
|
command->args.cat.paths_length = 1;
|
||||||
|
command->args.cat.paths = calloc(1, sizeof(char *));
|
||||||
|
command->args.cat.paths[0] = calloc(strlen(argv[3] + 1), sizeof(char));
|
||||||
|
strcpy(command->args.cat.paths[0], argv[3]);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
zmsg_t *msg = guestfs_inspect_command_to_zmsg(command);
|
||||||
|
zmsg_send(&msg, daemon);
|
||||||
|
zmsg_t *rep = zmsg_recv(daemon);
|
||||||
|
|
||||||
|
char *res = NULL;
|
||||||
|
// process reply
|
||||||
|
switch (command->command) {
|
||||||
|
case GUESTFS_COMMAND_LS:
|
||||||
|
res = zmsg_popstr(rep);
|
||||||
|
printf("Res:\n%s\n", res);
|
||||||
|
free(res);
|
||||||
|
break;
|
||||||
|
case GUESTFS_COMMAND_CAT:
|
||||||
|
res = zmsg_popstr(rep);
|
||||||
|
printf("Res:\n%s\n", res);
|
||||||
|
free(res);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
zmsg_destroy(&msg);
|
||||||
|
zmsg_destroy(&rep);
|
||||||
|
zsock_destroy(&daemon);
|
||||||
|
guestfs_inspect_command_destroy(&command);
|
||||||
|
|
||||||
|
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");
|
||||||
|
}
|
||||||
|
|
||||||
|
enum guestfs_inspect_command_const parse_command(const char *input) {
|
||||||
|
if (STREQ(input, "ls")) {
|
||||||
|
return GUESTFS_COMMAND_LS;
|
||||||
|
} else if (STREQ(input, "cat")) {
|
||||||
|
return GUESTFS_COMMAND_CAT;
|
||||||
|
}
|
||||||
|
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,49 +2,66 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <czmq.h>
|
#include <czmq.h>
|
||||||
|
#include <zmsg.h>
|
||||||
#include <zactor.h>
|
#include <zactor.h>
|
||||||
#include <guestfs.h>
|
#include <guestfs.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
#include "libguestfs-inspect.h"
|
#include "libguestfs-inspect.h"
|
||||||
|
|
||||||
#define STREQ(a, b) (strcmp((a), (b)) == 0)
|
#define STREQ(a, b) (strcmp((a), (b)) == 0)
|
||||||
|
|
||||||
static void *worker_task(zsock_t*, char*);
|
static void worker_task(zsock_t*, const char*);
|
||||||
|
|
||||||
char *endpoint(void);
|
|
||||||
|
|
||||||
static int compare_key_len(const void*, const void*);
|
static int compare_key_len(const void*, const void*);
|
||||||
static int count_mountpoints(char *const *argv);
|
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) {
|
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 void print_intro() {
|
||||||
|
printf("\n"
|
||||||
|
"\n"
|
||||||
|
" ,ggggggg,\n"
|
||||||
|
" ,dP\"\"\"\"\"\"Y8b 8I 8I\n"
|
||||||
|
" d8' a Y8 8I 8I\n"
|
||||||
|
" 88 \"Y8P\' 8I 8I\n"
|
||||||
|
" `8baaaa 8I 8I\n"
|
||||||
|
",d8P\"\"\"\" ,gggg,8I ,gggg,8I ,gggg,gg\n"
|
||||||
|
"d8\" dP\" \"Y8I dP\" \"Y8I dP\" \"Y8I\n"
|
||||||
|
"Y8, i8' ,8I i8' ,8I i8' ,8I\n"
|
||||||
|
"`Yba,,_____, ,d8, ,d8b,,d8, ,d8b,,d8, ,d8b,\n"
|
||||||
|
" `\"Y8888888 P\"Y8888P\"`Y8P\"Y8888P\"`Y8P\"Y8888P\"`Y8\n"
|
||||||
|
"\n");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
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(zsock_t *pipe, char *disk_path) {
|
static void cat_file(guestfs_h *g, const char *file_path, char **file_content, size_t *file_size) {
|
||||||
guestfs_h *g;
|
assert((*file_content) == NULL);
|
||||||
|
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 guestfs_h* init_guestfs(const char *disk_path) {
|
||||||
|
guestfs_h *g = NULL;
|
||||||
char **roots, **mountpoints;
|
char **roots, **mountpoints;
|
||||||
char *root;
|
char *root;
|
||||||
size_t i, j;
|
size_t i, j;
|
||||||
|
|
||||||
//char *file_content;
|
|
||||||
//size_t file_size;
|
|
||||||
|
|
||||||
// Create a connection handle
|
// Create a connection handle
|
||||||
g = guestfs_create();
|
g = guestfs_create();
|
||||||
if (g == NULL) {
|
if (g == NULL) {
|
||||||
@@ -84,76 +101,222 @@ static void *worker_task(zsock_t *pipe, char *disk_path) {
|
|||||||
free(mountpoints);
|
free(mountpoints);
|
||||||
free(root);
|
free(root);
|
||||||
}
|
}
|
||||||
|
free(roots);
|
||||||
|
return g;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void worker_task(zsock_t *pipe, const char *disk_path) {
|
||||||
|
guestfs_h *g;
|
||||||
|
g = init_guestfs(disk_path);
|
||||||
|
|
||||||
|
zsock_t *worker = zsock_new_dealer("inproc://workers");
|
||||||
|
|
||||||
// ZeroMQ Opens here
|
// ZeroMQ Opens here
|
||||||
zsock_signal(pipe, 0);
|
zsock_signal(pipe, 0);
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
zmsg_t *message = zmsg_recv(pipe);
|
zmsg_t *msg = zmsg_recv(pipe);
|
||||||
if (!message) {
|
if (!msg) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
// Process message
|
|
||||||
|
|
||||||
// do something here
|
zframe_t *client_id = zmsg_pop(msg);
|
||||||
|
// Checking for term message
|
||||||
// Sending reply
|
if (zframe_streq(client_id, "$TERM")) {
|
||||||
zmsg_send(&msg, pipe);
|
zframe_destroy(&client_id);
|
||||||
zmsg_destory(&msg);
|
zmsg_destroy(&msg);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
/*if (guestfs_is_file_opts(g, file_path, GUESTFS_IS_FILE_OPTS_FOLLOWSYMLINKS, 1, -1) > 0) {
|
|
||||||
printf("--- %s ---\n", file_path);
|
struct guestfs_inpsect_command *command = guestfs_inspect_zmsg_to_command(msg);
|
||||||
file_content = guestfs_read_file(g, file_path, &file_size);
|
zmsg_t *reply = zmsg_new();
|
||||||
if (file_content == NULL) {
|
if (!reply) {
|
||||||
|
printf("wuddahec\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
printf("%s\n", file_content);
|
|
||||||
free(file_content);
|
switch (command->command) {
|
||||||
}*/
|
case GUESTFS_COMMAND_LS:;
|
||||||
free(roots);
|
zmsg_pushstr(reply, "From worker! -- not implemented");
|
||||||
|
break;
|
||||||
|
case GUESTFS_COMMAND_CAT:;
|
||||||
|
char *res = NULL;
|
||||||
|
size_t s = 0;
|
||||||
|
cat_file(g, command->args.cat.paths[0], &res, &s);
|
||||||
|
zmsg_addmem(reply, res, s);
|
||||||
|
free(res);
|
||||||
|
break;
|
||||||
|
default:;
|
||||||
|
zmsg_pushstr(reply, "Unknown command");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sending reply
|
||||||
|
zmsg_pushstr(reply, "");
|
||||||
|
zmsg_prepend(reply, &client_id);
|
||||||
|
zmsg_send(&reply, worker);
|
||||||
|
|
||||||
|
guestfs_inspect_command_destroy(&command);
|
||||||
|
zframe_destroy(&client_id);
|
||||||
|
zmsg_destroy(&msg);
|
||||||
|
zmsg_destroy(&reply);
|
||||||
|
}
|
||||||
|
|
||||||
|
guestfs_shutdown(g);
|
||||||
guestfs_close(g);
|
guestfs_close(g);
|
||||||
|
zsock_destroy(&worker);
|
||||||
exit(EXIT_SUCCESS);
|
exit(EXIT_SUCCESS);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
char *name;
|
||||||
|
zactor_t *worker;
|
||||||
|
} zactor_worker_map;
|
||||||
|
|
||||||
|
char *strdup(const char *);
|
||||||
|
void mapping_zactor_worker(const char *name, zactor_t *worker, zactor_worker_map **map, size_t mapper_size) {
|
||||||
|
map[mapper_size - 1] = malloc(sizeof(zactor_worker_map));
|
||||||
|
(*map[mapper_size - 1]).name = strdup(name);
|
||||||
|
(*map[mapper_size - 1]).worker = worker;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
zactor_t * zactor_worker_lookup(char *name, zactor_worker_map **map, size_t mapper_size) {
|
||||||
|
size_t i = 0;
|
||||||
|
for (i = 0; i < mapper_size; i++) {
|
||||||
|
if (strcmp((*map[i]).name, name) == 0) {
|
||||||
|
return map[i]->worker;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void log_message(const char *message) {
|
||||||
|
time_t ltime;
|
||||||
|
struct tm *result;
|
||||||
|
char *stime;
|
||||||
|
|
||||||
|
ltime = time(NULL);
|
||||||
|
result = localtime(<ime);
|
||||||
|
stime = asctime(result);
|
||||||
|
strtok(stime, "\n");
|
||||||
|
printf("[%s] %s\n", stime, message);
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
if (argc < 2) {
|
if (argc < 2) {
|
||||||
printf("Usage: %s <disk-path> ...\n", argv[0]);
|
printf("Usage: %s <disk-path>:<name> ...\n", argv[0]);
|
||||||
return EXIT_FAILURE;
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
const int worker_count = argc - 1;
|
print_intro();
|
||||||
|
|
||||||
// One worker per disk image.
|
// Zactor setup
|
||||||
struct {
|
size_t worker_map_size = 0;
|
||||||
char *name;
|
zactor_worker_map **worker_map = NULL;
|
||||||
zactor_t *worker;
|
|
||||||
} worker_map[worker_count];
|
|
||||||
|
|
||||||
for (int i = 0; i < worker_count; i++) {
|
log_message("Starting Workers");
|
||||||
|
|
||||||
|
for (int i = 0; i < argc - 1; i++) {
|
||||||
|
worker_map_size++;
|
||||||
|
worker_map = realloc(worker_map, sizeof(zactor_worker_map *) * worker_map_size);
|
||||||
char *path = strtok(argv[i+1], ":");
|
char *path = strtok(argv[i+1], ":");
|
||||||
worker_map[i].name = strtok(NULL, ":");
|
char *name = strtok(NULL, ":");
|
||||||
worker_map[i].worker = zactor_new(worker_task, path);
|
mapping_zactor_worker(name, zactor_new((void *)worker_task, path), worker_map, worker_map_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
char *ep = endpoint();
|
if (worker_map == NULL) {
|
||||||
zsock_t *frontend = zsock_new_router(ep);
|
log_message("Worker map was empty");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Setup daemon ipc endpoint
|
||||||
|
char *ep = guestfs_inspect_endpoint();
|
||||||
|
// Setting up endpoint log message
|
||||||
|
char *ep_log = (char *)malloc((strlen(ep) * sizeof(char)) + 32);
|
||||||
|
|
||||||
|
// Sending endpoint log message
|
||||||
|
sprintf(ep_log, "Endpoint: %s", ep); // NOLINT
|
||||||
|
log_message(ep_log);
|
||||||
|
free(ep_log);
|
||||||
|
|
||||||
|
// Setup ZMQ routers
|
||||||
|
zsock_t *frontend = zsock_new(ZMQ_ROUTER);
|
||||||
|
zsock_bind(frontend, "%s", ep);
|
||||||
free(ep);
|
free(ep);
|
||||||
|
|
||||||
|
zsock_t *backend = zsock_new(ZMQ_ROUTER);
|
||||||
|
zsock_bind(backend, "inproc://workers");
|
||||||
|
|
||||||
|
// Connecting frontend to backend
|
||||||
|
zpoller_t *poller = zpoller_new(frontend, backend, NULL);
|
||||||
|
assert(poller);
|
||||||
|
|
||||||
|
log_message("Server listening for requests");
|
||||||
|
|
||||||
|
zframe_t *message_pop = NULL;
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
|
void *sock = zpoller_wait(poller, -1);
|
||||||
|
|
||||||
|
if (!sock) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (sock == frontend) {
|
||||||
zmsg_t *msg = zmsg_recv(frontend);
|
zmsg_t *msg = zmsg_recv(frontend);
|
||||||
|
// reply id
|
||||||
|
zframe_t *identity = zmsg_pop(msg);
|
||||||
|
// Null frame
|
||||||
|
message_pop = zmsg_pop(msg);
|
||||||
|
// Destroy immediately
|
||||||
|
zframe_destroy(&message_pop);
|
||||||
|
|
||||||
if (!msg) break;
|
struct guestfs_inpsect_command *c = guestfs_inspect_zmsg_to_command(msg);
|
||||||
|
if (c == NULL) {
|
||||||
// Find the worker with the given name.
|
log_message("Error creating inspect command");
|
||||||
zactor_t *worker = NULL;
|
zmsg_destroy(&msg);
|
||||||
for (int i = 0; i < worker_count; i++) {
|
zframe_destroy(&identity);
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
if (worker) {
|
|
||||||
zmsg_send(&msg, zactor_sock(worker));
|
zactor_t *zactor_tmp = zactor_worker_lookup(c->name, worker_map, worker_map_size);
|
||||||
|
if (zactor_tmp != NULL) {
|
||||||
|
// send request to worker
|
||||||
|
zmsg_prepend(msg, &identity);
|
||||||
|
zactor_send(zactor_tmp, &msg);
|
||||||
} else {
|
} else {
|
||||||
// The name specified does not exist.
|
// add failure response
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return EXIT_SUCCESS;
|
zactor_tmp = NULL;
|
||||||
|
guestfs_inspect_command_destroy(&c);
|
||||||
|
zmsg_destroy(&msg);
|
||||||
|
zframe_destroy(&identity);
|
||||||
|
} else if (sock == backend) {
|
||||||
|
zmsg_t *msg = zmsg_recv(backend);
|
||||||
|
message_pop = zmsg_pop(msg); // Removing backend id
|
||||||
|
// Destroy immediately
|
||||||
|
zframe_destroy(&message_pop);
|
||||||
|
zmsg_send(&msg, frontend);
|
||||||
|
zmsg_destroy(&msg);
|
||||||
|
} else {
|
||||||
|
log_message("Socket found from unknown zsock");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
log_message("Cleaning up workers");
|
||||||
|
// Cleanup
|
||||||
|
for (size_t i = 0; i < worker_map_size; i++) {
|
||||||
|
zactor_destroy(&worker_map[i]->worker);
|
||||||
|
free(worker_map[i]->name);
|
||||||
|
free(worker_map[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
free(worker_map);
|
||||||
|
zpoller_destroy(&poller);
|
||||||
|
zsock_destroy(&frontend);
|
||||||
|
zsock_destroy(&backend);
|
||||||
|
exit(EXIT_SUCCESS);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
122
libguestfs-inspect.c
Normal file
122
libguestfs-inspect.c
Normal file
@@ -0,0 +1,122 @@
|
|||||||
|
#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(const 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));
|
||||||
|
|
||||||
|
if (!(zmsg_size(msg) > 2)) {
|
||||||
|
free(res);
|
||||||
|
zmsg_destroy(&msg);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
res->name = zmsg_popstr(msg);
|
||||||
|
zframe_t *next = zmsg_pop(msg);
|
||||||
|
res->command = *(zframe_data(next));
|
||||||
|
zframe_destroy(&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;
|
||||||
|
default:
|
||||||
|
printf("Command not found\n");
|
||||||
|
}
|
||||||
|
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,4 +1,9 @@
|
|||||||
enum guestfs_inspect_command {
|
#ifndef LIBGUESTFS_INSPECT_H
|
||||||
|
#define LIBGUESTFS_INSPECT_H
|
||||||
|
|
||||||
|
#include <czmq.h>
|
||||||
|
|
||||||
|
enum guestfs_inspect_command_const {
|
||||||
GUESTFS_COMMAND_LS,
|
GUESTFS_COMMAND_LS,
|
||||||
/* GUESTFS_COMMAND_TOUCH, */
|
/* GUESTFS_COMMAND_TOUCH, */
|
||||||
/* GUESTFS_COMMAND_MKDIR, */
|
/* GUESTFS_COMMAND_MKDIR, */
|
||||||
@@ -6,18 +11,26 @@ enum guestfs_inspect_command {
|
|||||||
};
|
};
|
||||||
|
|
||||||
struct guestfs_ls_args {
|
struct guestfs_ls_args {
|
||||||
size_t path_length;
|
size_t paths_length;
|
||||||
char path[];
|
char **paths;
|
||||||
|
};
|
||||||
|
struct guestfs_cat_args {
|
||||||
|
size_t paths_length;
|
||||||
|
char **paths;
|
||||||
};
|
};
|
||||||
struct guestfs_cat_args {};
|
|
||||||
|
|
||||||
struct guestfs_inpsect_command {
|
struct guestfs_inpsect_command {
|
||||||
enum guestfs_inspect_command command;
|
char *name;
|
||||||
|
enum guestfs_inspect_command_const command;
|
||||||
union {
|
union {
|
||||||
struct guestfs_ls_args ls;
|
struct guestfs_ls_args ls;
|
||||||
struct guestfs_cat_args cat;
|
struct guestfs_cat_args cat;
|
||||||
} args;
|
} args;
|
||||||
size_t name_length;
|
|
||||||
char name[];
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
char *guestfs_inspect_endpoint(void);
|
||||||
|
zmsg_t *guestfs_inspect_command_to_zmsg(const 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);
|
||||||
|
|
||||||
|
#endif // LIBGUESTFS_INSPECT_H
|
||||||
|
|||||||
Reference in New Issue
Block a user