Compare commits
6 Commits
fdd643c528
...
dc69f665eb
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
dc69f665eb | ||
|
|
ec6fac8537 | ||
|
|
4dd55da7e7 | ||
|
|
8fa0c4b259 | ||
|
|
84fffca8e5 | ||
|
|
e4d58ff94b |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -2,3 +2,4 @@ guestfs-inspect
|
||||
guestfs-inspectd
|
||||
*~
|
||||
*.qcow2
|
||||
venv/
|
||||
|
||||
8
Makefile
8
Makefile
@@ -2,9 +2,15 @@ build: daemon client
|
||||
debug: daemon-debug client-debug
|
||||
|
||||
daemon:
|
||||
gcc -ggdb3 -Wall guestfs-inspectd.c libguestfs-inspect.c -o guestfs-inspectd `pkg-config libguestfs libczmq --cflags --libs`
|
||||
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:
|
||||
|
||||
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()
|
||||
@@ -8,7 +8,7 @@
|
||||
#define STREQ(a, b) (strcmp((a), (b)) == 0)
|
||||
|
||||
void print_help(char *);
|
||||
enum guestfs_inspect_command_const parse_command(char *);
|
||||
enum guestfs_inspect_command_const parse_command(const char *);
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
if (argc == 1) {
|
||||
@@ -74,7 +74,7 @@ void print_help(char *name) {
|
||||
printf(" cat <path>\n");
|
||||
}
|
||||
|
||||
enum guestfs_inspect_command_const parse_command(char *input) {
|
||||
enum guestfs_inspect_command_const parse_command(const char *input) {
|
||||
if (STREQ(input, "ls")) {
|
||||
return GUESTFS_COMMAND_LS;
|
||||
} else if (STREQ(input, "cat")) {
|
||||
|
||||
@@ -5,12 +5,13 @@
|
||||
#include <zmsg.h>
|
||||
#include <zactor.h>
|
||||
#include <guestfs.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "libguestfs-inspect.h"
|
||||
|
||||
#define STREQ(a, b) (strcmp((a), (b)) == 0)
|
||||
|
||||
static void worker_task(zsock_t*, char*);
|
||||
static void worker_task(zsock_t*, const char*);
|
||||
|
||||
static int compare_key_len(const void*, const void*);
|
||||
static int count_mountpoints(char *const *argv);
|
||||
@@ -21,15 +22,32 @@ static int compare_key_len(const void *p1, const void *p2) {
|
||||
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) {
|
||||
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) {
|
||||
static void cat_file(guestfs_h *g, const char *file_path, char **file_content, size_t *file_size) {
|
||||
assert((*file_content) == NULL);
|
||||
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);
|
||||
@@ -38,7 +56,7 @@ static void cat_file(guestfs_h *g, char *file_path, char **file_content, size_t
|
||||
return;
|
||||
}
|
||||
|
||||
static guestfs_h* init_guestfs(char *disk_path) {
|
||||
static guestfs_h* init_guestfs(const char *disk_path) {
|
||||
guestfs_h *g = NULL;
|
||||
char **roots, **mountpoints;
|
||||
char *root;
|
||||
@@ -84,14 +102,11 @@ static guestfs_h* init_guestfs(char *disk_path) {
|
||||
free(root);
|
||||
}
|
||||
free(roots);
|
||||
|
||||
printf("Finished initializing guestfs\n");
|
||||
return g;
|
||||
}
|
||||
|
||||
static void worker_task(zsock_t *pipe, char *disk_path) {
|
||||
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");
|
||||
@@ -104,8 +119,14 @@ static void worker_task(zsock_t *pipe, char *disk_path) {
|
||||
if (!msg) {
|
||||
break;
|
||||
}
|
||||
printf("Received a message in the worker\n");
|
||||
|
||||
zframe_t *client_id = zmsg_pop(msg);
|
||||
// Checking for term message
|
||||
if (zframe_streq(client_id, "$TERM")) {
|
||||
zframe_destroy(&client_id);
|
||||
zmsg_destroy(&msg);
|
||||
break;
|
||||
}
|
||||
|
||||
struct guestfs_inpsect_command *command = guestfs_inspect_zmsg_to_command(msg);
|
||||
zmsg_t *reply = zmsg_new();
|
||||
@@ -115,17 +136,17 @@ static void worker_task(zsock_t *pipe, char *disk_path) {
|
||||
}
|
||||
|
||||
switch (command->command) {
|
||||
case GUESTFS_COMMAND_LS: printf("Enter ls command\n");
|
||||
case GUESTFS_COMMAND_LS:;
|
||||
zmsg_pushstr(reply, "From worker! -- not implemented");
|
||||
break;
|
||||
case GUESTFS_COMMAND_CAT: printf("Entering cat command\n");
|
||||
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:
|
||||
default:;
|
||||
zmsg_pushstr(reply, "Unknown command");
|
||||
break;
|
||||
}
|
||||
@@ -141,8 +162,9 @@ static void worker_task(zsock_t *pipe, char *disk_path) {
|
||||
zmsg_destroy(&reply);
|
||||
}
|
||||
|
||||
zsock_destroy(&worker);
|
||||
guestfs_shutdown(g);
|
||||
guestfs_close(g);
|
||||
zsock_destroy(&worker);
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
@@ -152,12 +174,10 @@ typedef struct {
|
||||
} zactor_worker_map;
|
||||
|
||||
char *strdup(const char *);
|
||||
void mapping_zactor_worker(char *name, zactor_t *worker, zactor_worker_map **map, size_t mapper_size) {
|
||||
|
||||
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;
|
||||
printf("Registering inside mapper %s :: %p\n", name, worker);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -165,99 +185,138 @@ zactor_t * zactor_worker_lookup(char *name, zactor_worker_map **map, size_t mapp
|
||||
size_t i = 0;
|
||||
for (i = 0; i < mapper_size; i++) {
|
||||
if (strcmp((*map[i]).name, name) == 0) {
|
||||
//printf("found %p\n", map[i]->worker);
|
||||
return map[i]->worker;
|
||||
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) {
|
||||
if (argc < 2) {
|
||||
printf("Usage: %s <disk-path>:<name> ...\n", argv[0]);
|
||||
return EXIT_FAILURE;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
print_intro();
|
||||
|
||||
// Zactor setup
|
||||
size_t worker_map_size = 0;
|
||||
zactor_worker_map **worker_map = NULL;
|
||||
|
||||
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 *name = strtok(NULL, ":");
|
||||
printf("--- Registering\n");
|
||||
printf("name: '%s'\n", name);
|
||||
printf("path: '%s'\n", path);
|
||||
mapping_zactor_worker(name, zactor_new((void *)worker_task, path), worker_map, worker_map_size);
|
||||
}
|
||||
|
||||
if (worker_map == NULL) {
|
||||
log_message("Worker map was empty");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
// Setup daemon ipc endpoint
|
||||
char *ep = guestfs_inspect_endpoint();
|
||||
printf("ep: %s\n", ep);
|
||||
// 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, ep);
|
||||
zsock_bind(frontend, "%s", ep);
|
||||
free(ep);
|
||||
|
||||
zsock_t *backend = zsock_new(ZMQ_ROUTER);
|
||||
zsock_bind(backend, "inproc://workers");
|
||||
|
||||
for (int i = 0; i < worker_map_size; i++) {
|
||||
printf("Found:: %s :: %p\n", worker_map[i]->name, worker_map[i]->worker);
|
||||
}
|
||||
|
||||
// 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) {
|
||||
void *sock = zpoller_wait(poller, -1);
|
||||
|
||||
if (!sock) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (sock == frontend) {
|
||||
zmsg_t *msg = zmsg_recv(frontend);
|
||||
// reply id
|
||||
zframe_t *identity = zmsg_pop(msg);
|
||||
// Null frame
|
||||
zmsg_pop(msg);
|
||||
struct guestfs_inpsect_command *c = guestfs_inspect_zmsg_to_command(msg);
|
||||
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);
|
||||
|
||||
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 {
|
||||
// add failure responce
|
||||
}
|
||||
struct guestfs_inpsect_command *c = guestfs_inspect_zmsg_to_command(msg);
|
||||
if (c == NULL) {
|
||||
log_message("Error creating inspect command");
|
||||
zmsg_destroy(&msg);
|
||||
zframe_destroy(&identity);
|
||||
continue;
|
||||
}
|
||||
|
||||
zactor_tmp = NULL;
|
||||
guestfs_inspect_command_destroy(&c);
|
||||
zmsg_destroy(&msg);
|
||||
zframe_destroy(&identity);
|
||||
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 {
|
||||
// add failure response
|
||||
}
|
||||
|
||||
zactor_tmp = NULL;
|
||||
guestfs_inspect_command_destroy(&c);
|
||||
zmsg_destroy(&msg);
|
||||
zframe_destroy(&identity);
|
||||
} else if (sock == backend) {
|
||||
zmsg_t *msg = zmsg_recv(backend);
|
||||
zmsg_pop(msg); // Removing backend id
|
||||
zmsg_send(&msg, frontend);
|
||||
zmsg_destroy(&msg);
|
||||
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 (int i = 0; i < worker_map_size; i++) {
|
||||
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);
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
free(worker_map);
|
||||
zpoller_destroy(&poller);
|
||||
zsock_destroy(&frontend);
|
||||
zsock_destroy(&backend);
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
|
||||
@@ -27,7 +27,7 @@ char *guestfs_inspect_endpoint() {
|
||||
return res;
|
||||
}
|
||||
|
||||
zmsg_t *guestfs_inspect_command_to_zmsg(struct guestfs_inpsect_command *command) {
|
||||
zmsg_t *guestfs_inspect_command_to_zmsg(const struct guestfs_inpsect_command *command) {
|
||||
zmsg_t *res = zmsg_new();
|
||||
|
||||
zmsg_addstr(res, command->name);
|
||||
@@ -58,11 +58,17 @@ struct guestfs_inpsect_command *guestfs_inspect_zmsg_to_command(zmsg_t *external
|
||||
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));
|
||||
free(next);
|
||||
|
||||
zframe_destroy(&next);
|
||||
|
||||
switch (res->command) {
|
||||
case GUESTFS_COMMAND_LS:
|
||||
next = zmsg_pop(msg);
|
||||
@@ -86,9 +92,11 @@ struct guestfs_inpsect_command *guestfs_inspect_zmsg_to_command(zmsg_t *external
|
||||
res->args.cat.paths[i] = zmsg_popstr(msg);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
printf("Command not found\n");
|
||||
}
|
||||
zmsg_destroy(&msg);
|
||||
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
@@ -108,7 +116,7 @@ void guestfs_inspect_command_destroy(struct guestfs_inpsect_command **c) {
|
||||
free((*c)->args.cat.paths);
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
free(*c);
|
||||
*c = NULL;
|
||||
}
|
||||
|
||||
@@ -29,7 +29,7 @@ struct guestfs_inpsect_command {
|
||||
};
|
||||
|
||||
char *guestfs_inspect_endpoint(void);
|
||||
zmsg_t *guestfs_inspect_command_to_zmsg(struct guestfs_inpsect_command *command);
|
||||
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);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user