Move function implementations out of the header file.
This commit is contained in:
4
Makefile
4
Makefile
@@ -1,7 +1,7 @@
|
|||||||
daemon:
|
daemon:
|
||||||
gcc -ggdb3 -Wall guestfs-inspectd.c -o guestfs-inspectd `pkg-config libguestfs libczmq --cflags --libs`
|
gcc -ggdb3 -Wall guestfs-inspectd.c libguestfs-inspect.c -o guestfs-inspectd `pkg-config libguestfs libczmq --cflags --libs`
|
||||||
|
|
||||||
client:
|
client:
|
||||||
gcc -ggdb3 -Wall guestfs-inspect.c -o guestfs-inspect `pkg-config libguestfs libczmq --cflags --libs`
|
gcc -ggdb3 -Wall guestfs-inspect.c libguestfs-inspect.c -o guestfs-inspect `pkg-config libguestfs libczmq --cflags --libs`
|
||||||
|
|
||||||
build: daemon client
|
build: daemon client
|
||||||
|
|||||||
110
libguestfs-inspect.c
Normal file
110
libguestfs-inspect.c
Normal file
@@ -0,0 +1,110 @@
|
|||||||
|
#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(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 *msg) {
|
||||||
|
struct guestfs_inpsect_command *res = malloc(sizeof(struct guestfs_inpsect_command));
|
||||||
|
|
||||||
|
res->name = zmsg_popstr(msg);
|
||||||
|
zframe_t *next = zmsg_pop(msg);
|
||||||
|
res->command = *(zframe_data(next));
|
||||||
|
free(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;
|
||||||
|
}
|
||||||
|
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;
|
||||||
|
}
|
||||||
@@ -28,111 +28,9 @@ struct guestfs_inpsect_command {
|
|||||||
} args;
|
} args;
|
||||||
};
|
};
|
||||||
|
|
||||||
char *guestfs_inspect_endpoint() {
|
char *guestfs_inspect_endpoint(void);
|
||||||
const char *guestfs_inspect_endpoint = getenv("GUESTFS_INSPECT_ENDPOINT");
|
zmsg_t *guestfs_inspect_command_to_zmsg(struct guestfs_inpsect_command *command);
|
||||||
const char *socket_file_name = "/guestfs-inspect.sock";
|
struct guestfs_inpsect_command *guestfs_inspect_zmsg_to_command(zmsg_t *msg);
|
||||||
char *res = NULL;
|
void guestfs_inspect_command_destroy(struct guestfs_inpsect_command **c);
|
||||||
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(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 *msg) {
|
|
||||||
struct guestfs_inpsect_command *res = malloc(sizeof(struct guestfs_inpsect_command));
|
|
||||||
|
|
||||||
res->name = zmsg_popstr(msg);
|
|
||||||
zframe_t *next = zmsg_pop(msg);
|
|
||||||
res->command = *(zframe_data(next));
|
|
||||||
free(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;
|
|
||||||
}
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif // LIBGUESTFS_INSPECT_H
|
#endif // LIBGUESTFS_INSPECT_H
|
||||||
|
|||||||
Reference in New Issue
Block a user