92 lines
3.9 KiB
C
Raw Normal View History

2026-01-29 00:38:21 +01:00
// retoor <retoor@molodetz.nl>
#include "tool.h"
2026-01-29 06:01:05 +01:00
#include "r_config.h"
2026-01-29 00:38:21 +01:00
#include "bash_executor.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static struct json_object *process_monitor_get_description(void);
static char *process_monitor_execute(tool_t *self, struct json_object *args);
static void process_monitor_print_action(const char *name, struct json_object *args);
static const tool_vtable_t process_monitor_vtable = {
.get_description = process_monitor_get_description,
.execute = process_monitor_execute,
.print_action = process_monitor_print_action
};
static tool_t process_monitor_tool = { .vtable = &process_monitor_vtable, .name = "process_monitor" };
tool_t *tool_process_monitor_create(void) { return &process_monitor_tool; }
static void process_monitor_print_action(const char *name, struct json_object *args) {
(void)name;
struct json_object *action;
if (json_object_object_get_ex(args, "action", &action)) {
fprintf(stderr, " -> Process monitor: %s\n", json_object_get_string(action));
}
}
static char *process_monitor_execute(tool_t *self, struct json_object *args) {
(void)self;
struct json_object *action_obj;
if (!json_object_object_get_ex(args, "action", &action_obj)) {
return strdup("Error: missing 'action' argument (list or kill)");
}
const char *action = json_object_get_string(action_obj);
if (strcmp(action, "list") == 0) {
2026-01-29 06:01:05 +01:00
return r_bash_execute("ps aux | head -n 50", false, 300);
2026-01-29 00:38:21 +01:00
} else if (strcmp(action, "kill") == 0) {
struct json_object *pid_obj;
if (!json_object_object_get_ex(args, "pid", &pid_obj)) {
return strdup("Error: missing 'pid' for kill action");
}
char cmd[256];
snprintf(cmd, sizeof(cmd), "kill -9 %d", json_object_get_int(pid_obj));
2026-01-29 06:01:05 +01:00
return r_bash_execute(cmd, false, 300);
2026-01-29 00:38:21 +01:00
}
return strdup("Error: unknown action");
}
static struct json_object *process_monitor_get_description(void) {
struct json_object *root = json_object_new_object();
json_object_object_add(root, "type", json_object_new_string("function"));
struct json_object *function = json_object_new_object();
json_object_object_add(function, "name", json_object_new_string("process_monitor"));
json_object_object_add(function, "description", json_object_new_string("Monitor and manage system processes."));
struct json_object *parameters = json_object_new_object();
json_object_object_add(parameters, "type", json_object_new_string("object"));
struct json_object *properties = json_object_new_object();
struct json_object *action = json_object_new_object();
json_object_object_add(action, "type", json_object_new_string("string"));
json_object_object_add(action, "description", json_object_new_string("Action to perform: 'list' or 'kill'."));
json_object_object_add(properties, "action", action);
struct json_object *pid = json_object_new_object();
json_object_object_add(pid, "type", json_object_new_string("integer"));
json_object_object_add(pid, "description", json_object_new_string("Process ID to kill (required for 'kill' action)."));
json_object_object_add(properties, "pid", pid);
json_object_object_add(parameters, "properties", properties);
struct json_object *required = json_object_new_array();
json_object_array_add(required, json_object_new_string("action"));
2026-01-29 06:01:05 +01:00
json_object_array_add(required, json_object_new_string("pid"));
2026-01-29 00:38:21 +01:00
json_object_object_add(parameters, "required", required);
2026-01-29 06:01:05 +01:00
json_object_object_add(parameters, "additionalProperties", json_object_new_boolean(0));
2026-01-29 00:38:21 +01:00
json_object_object_add(function, "parameters", parameters);
2026-01-29 06:01:05 +01:00
r_config_handle cfg = r_config_get_instance();
if (r_config_use_strict(cfg)) {
json_object_object_add(function, "strict", json_object_new_boolean(1));
}
2026-01-29 00:38:21 +01:00
json_object_object_add(root, "function", function);
return root;
}