Skip to content

Commit

Permalink
Add list unit files functions to node and agent
Browse files Browse the repository at this point in the history
Fixes: eclipse-bluechi#889

Signed-off-by: tallison <[email protected]>
  • Loading branch information
tallison committed Jul 30, 2024
1 parent 4884806 commit 333ef0c
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 0 deletions.
11 changes: 11 additions & 0 deletions data/org.eclipse.bluechi.Node.xml
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,17 @@
<arg name="units" type="a(ssssssouso)" direction="out" />
</method>

<!--
ListUnitFiles:
@unitfiles: A list of all unit files on the node:
- The unit file path as string
- The enabled state (i.e. whether the unit is currently enabled or not)
List all systemd unit files.
-->
<method name="ListUnitFiles">
<arg name="unitfiles" type="a(ss)" direction="out" />
</method>

<!--
Reload:
Expand Down
45 changes: 45 additions & 0 deletions src/agent/agent.c
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,50 @@ static int agent_method_list_units(sd_bus_message *m, void *userdata, UNUSED sd_
return 1;
}

/************************************************************************
********** org.eclipse.bluechi.internal.Agent.ListUnitFiles ************
************************************************************************/

static int list_unit_files_callback(sd_bus_message *m, void *userdata, UNUSED sd_bus_error *ret_error) {
_cleanup_systemd_request_ SystemdRequest *req = userdata;

if (sd_bus_message_is_method_error(m, NULL)) {
return sd_bus_reply_method_error(req->request_message, sd_bus_message_get_error(m));
}

_cleanup_sd_bus_message_ sd_bus_message *reply = NULL;

int r = sd_bus_message_new_method_return(req->request_message, &reply);
if (r < 0) {
return r;
}

r = sd_bus_message_copy(reply, m, true);
if (r < 0) {
return r;
}

return sd_bus_message_send(reply);
}

static int agent_method_list_unit_files(sd_bus_message *m, void *userdata, UNUSED sd_bus_error *ret_error) {
Agent *agent = userdata;

_cleanup_systemd_request_ SystemdRequest *req = agent_create_request(agent, m, "ListUnitFiles");
if (req == NULL) {
return sd_bus_reply_method_errorf(
m,
SD_BUS_ERROR_FAILED,
"Failed to create a systemd request for the ListUnitFiles method");
}

if (!systemd_request_start(req, list_unit_files_callback)) {
return sd_bus_reply_method_errorf(m, SD_BUS_ERROR_FAILED, "Failed to start systemd request");
}

return 1;
}

/************************************************************************
******** org.eclipse.bluechi.internal.Agent.GetUnitProperties ************
************************************************************************/
Expand Down Expand Up @@ -1668,6 +1712,7 @@ static int agent_method_job_cancel(sd_bus_message *m, void *userdata, UNUSED sd_
static const sd_bus_vtable internal_agent_vtable[] = {
SD_BUS_VTABLE_START(0),
SD_BUS_METHOD("ListUnits", "", UNIT_INFO_STRUCT_ARRAY_TYPESTRING, agent_method_list_units, 0),
SD_BUS_METHOD("ListUnitFiles", "", UNIT_FILE_INFO_STRUCT_ARRAY_TYPESTRING, agent_method_list_unit_files, 0),
SD_BUS_METHOD("GetUnitProperties", "ss", "a{sv}", agent_method_get_unit_properties, 0),
SD_BUS_METHOD("GetUnitProperty", "sss", "v", agent_method_get_unit_property, 0),
SD_BUS_METHOD("SetUnitProperties", "sba(sv)", "", agent_method_set_unit_properties, 0),
Expand Down
75 changes: 75 additions & 0 deletions src/controller/node.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ static int node_run_unit_lifecycle_method(sd_bus_message *m, Node *node, const c
static int node_method_register(sd_bus_message *m, void *userdata, sd_bus_error *ret_error);
static int node_disconnected(sd_bus_message *message, void *userdata, sd_bus_error *error);
static int node_method_list_units(sd_bus_message *m, void *userdata, UNUSED sd_bus_error *ret_error);
static int node_method_list_unit_files(sd_bus_message *m, void *userdata, UNUSED sd_bus_error *ret_error);
static int node_method_set_unit_properties(sd_bus_message *m, void *userdata, UNUSED sd_bus_error *ret_error);
static int node_method_start_unit(sd_bus_message *m, void *userdata, UNUSED sd_bus_error *ret_error);
static int node_method_stop_unit(sd_bus_message *m, void *userdata, UNUSED sd_bus_error *ret_error);
Expand Down Expand Up @@ -65,6 +66,7 @@ static const sd_bus_vtable internal_controller_controller_vtable[] = {
static const sd_bus_vtable node_vtable[] = {
SD_BUS_VTABLE_START(0),
SD_BUS_METHOD("ListUnits", "", UNIT_INFO_STRUCT_ARRAY_TYPESTRING, node_method_list_units, 0),
SD_BUS_METHOD("ListUnitFiles", "", UNIT_FILE_INFO_STRUCT_ARRAY_TYPESTRING, node_method_list_unit_files, 0),
SD_BUS_METHOD("StartUnit", "ss", "o", node_method_start_unit, 0),
SD_BUS_METHOD("StopUnit", "ss", "o", node_method_stop_unit, 0),
SD_BUS_METHOD("FreezeUnit", "s", "", node_method_passthrough_to_agent, 0),
Expand Down Expand Up @@ -1184,6 +1186,25 @@ AgentRequest *node_request_list_units(
return steal_pointer(&req);
}

AgentRequest *node_request_list_unit_files(
Node *node, agent_request_response_t cb, void *userdata, free_func_t free_userdata) {
if (!node_has_agent(node)) {
return NULL;
}

_cleanup_agent_request_ AgentRequest *req = NULL;
node_create_request(&req, node, "ListUnitFiles", cb, userdata, free_userdata);
if (req == NULL) {
return NULL;
}

if (agent_request_start(req) < 0) {
return NULL;
}

return steal_pointer(&req);
}

/*************************************************************************
********** org.eclipse.bluechi.Node.ListUnits **************************
************************************************************************/
Expand Down Expand Up @@ -1218,6 +1239,7 @@ static int method_list_units_callback(AgentRequest *req, sd_bus_message *m, UNUS
return sd_bus_message_send(reply);
}


static int node_method_list_units(sd_bus_message *m, void *userdata, UNUSED sd_bus_error *ret_error) {
Node *node = userdata;

Expand All @@ -1238,6 +1260,59 @@ static int node_method_list_units(sd_bus_message *m, void *userdata, UNUSED sd_b
return 1;
}

/*************************************************************************
********** org.eclipse.bluechi.Node.ListUnitFiles ***********************
************************************************************************/

static int method_list_unit_files_callback(AgentRequest *req, sd_bus_message *m, UNUSED sd_bus_error *ret_error) {
sd_bus_message *request_message = req->userdata;

if (sd_bus_message_is_method_error(m, NULL)) {
/* Forward error */
return sd_bus_reply_method_error(request_message, sd_bus_message_get_error(m));
}

_cleanup_sd_bus_message_ sd_bus_message *reply = NULL;
int r = sd_bus_message_new_method_return(request_message, &reply);
if (r < 0) {
return sd_bus_reply_method_errorf(
request_message,
SD_BUS_ERROR_FAILED,
"Failed to create a reply message for ListUnitFiles request: %s",
strerror(-r));
}

r = sd_bus_message_copy(reply, m, true);
if (r < 0) {
return sd_bus_reply_method_errorf(
request_message,
SD_BUS_ERROR_FAILED,
"Failed to copy the bus message for ListUnitFiles request: %s",
strerror(-r));
}

return sd_bus_message_send(reply);
}

static int node_method_list_unit_files(sd_bus_message *m, void *userdata, UNUSED sd_bus_error *ret_error) {
Node *node = userdata;

if (node->is_shutdown) {
return sd_bus_reply_method_errorf(
m, SD_BUS_ERROR_FAILED, "Request not allowed: node is in shutdown state");
}

_cleanup_agent_request_ AgentRequest *agent_req = node_request_list_unit_files(
node,
method_list_unit_files_callback,
sd_bus_message_ref(m),
(free_func_t) sd_bus_message_unref);
if (agent_req == NULL) {
return sd_bus_reply_method_errorf(m, SD_BUS_ERROR_FAILED, "List unit files not found");
}
return 1;
}

/*************************************************************************
********** org.eclipse.bluechi.Node.SetUnitProperty ******************
************************************************************************/
Expand Down
7 changes: 7 additions & 0 deletions src/libbluechi/common/protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,13 @@
#define NODE_AND_UNIT_INFO_STRUCT_TYPESTRING "(" NODE_AND_UNIT_INFO_TYPESTRING ")"
#define NODE_AND_UNIT_INFO_STRUCT_ARRAY_TYPESTRING "a" NODE_AND_UNIT_INFO_STRUCT_TYPESTRING

#define UNIT_FILE_INFO_TYPESTRING "ss"
#define UNIT_FILE_INFO_STRUCT_TYPESTRING "(" UNIT_FILE_INFO_TYPESTRING ")"
#define UNIT_FILE_INFO_STRUCT_ARRAY_TYPESTRING "a" UNIT_FILE_INFO_STRUCT_TYPESTRING

#define NODE_AND_UNIT_FILE_INFO_TYPESTRING "s" UNIT_FILE_INFO_TYPESTRING
#define NODE_AND_UNIT_FILE_INFO_STRUCT_TYPESTRING "(" NODE_AND_UNIT_FILE_INFO_TYPESTRING ")"
#define NODE_AND_UNIT_FILE_INFO_STRUCT_ARRAY_TYPESTRING "a" NODE_AND_UNIT_FILE_INFO_STRUCT_TYPESTRING

typedef enum JobState {
JOB_WAITING,
Expand Down

0 comments on commit 333ef0c

Please sign in to comment.