#############################################################################*/
#include <limits.h>
+#include <stdlib.h>
#include <systemd/sd-bus.h>
return r;
}
-// Dump
-
-static int networkctl_port_dump(sd_bus* bus, int argc, char* argv[]) {
+static int networkctl_port_describe(sd_bus* bus, const char* name, char** text) {
sd_bus_message* reply = NULL;
sd_bus_error error = SD_BUS_ERROR_NULL;
char path[PATH_MAX];
- const char* text = NULL;
+ const char* t = NULL;
int r;
- if (argc < 1) {
- fprintf(stderr, "Port required\n");
+ // Check input
+ if (!name || !text)
return -EINVAL;
- }
// Make port path
- r = nw_string_format(path, "/org/ipfire/network1/port/%s", argv[0]);
+ r = nw_string_format(path, "/org/ipfire/network1/port/%s", name);
if (r < 0)
goto ERROR;
}
// Read the text
- r = sd_bus_message_read(reply, "s", &text);
+ r = sd_bus_message_read(reply, "s", &t);
if (r < 0) {
fprintf(stderr, "Could not parse bus message: %s\n", strerror(-r));
goto ERROR;
}
- // Print the text
- if (text)
- printf("%s\n", text);
+ // Copy text to heap
+ *text = strdup(t);
+ if (!*text)
+ r = -errno;
ERROR:
if (reply)
return r;
}
+// Dump
+
+static int networkctl_port_dump(sd_bus* bus, int argc, char* argv[]) {
+ char* text = NULL;
+ int r;
+
+ if (argc < 1) {
+ fprintf(stderr, "Port required\n");
+ return -EINVAL;
+ }
+
+ // Describe the port
+ r = networkctl_port_describe(bus, argv[0], &text);
+ if (r < 0)
+ return r;
+
+ // Print the text
+ printf("%s\n", text);
+
+ if (text)
+ free(text);
+
+ return 0;
+}
+
// List
static int __networkctl_port_list(sd_bus* bus, const char* path, const char* name, void* data) {