about summary refs log tree commit diff
path: root/nitroctl.c
diff options
context:
space:
mode:
authorLeah Neukirchen <leah@vuxu.org>2023-12-31 22:15:53 +0100
committerLeah Neukirchen <leah@vuxu.org>2023-12-31 22:15:53 +0100
commit674f10d677f53ba8c1f9831f63c547d9b2957ea3 (patch)
treefafb54b3893c4c938447d7e174143b2b19d3d94b /nitroctl.c
parent687279c8128da6a3cf6d3d8475a4bae7577bef89 (diff)
downloadnitro-674f10d677f53ba8c1f9831f63c547d9b2957ea3.tar.gz
nitro-674f10d677f53ba8c1f9831f63c547d9b2957ea3.tar.xz
nitro-674f10d677f53ba8c1f9831f63c547d9b2957ea3.zip
nitroctl: allow passing paths as service names
Bit hacky still, but services can control themselves with '.' as
argument at least.
Diffstat (limited to 'nitroctl.c')
-rw-r--r--nitroctl.c40
1 files changed, 31 insertions, 9 deletions
diff --git a/nitroctl.c b/nitroctl.c
index 9dce603..09497c7 100644
--- a/nitroctl.c
+++ b/nitroctl.c
@@ -135,6 +135,27 @@ send_and_wait(char cmd, const char *service)
 	return 2;
 }
 
+char *
+normalize(char *service)
+{
+	if (!service || (service[0] != '/' && service[0] != '.'))
+		return service;
+
+	char *buf = realpath(service, 0);
+	if (!buf) {
+		fprintf(stderr, "nitroctl: no such service: %s: %m\n", service);
+		exit(1);
+	}
+
+	char *end = strrchr(buf, '/');
+	if (strcmp(end, "/log") == 0)
+		while(end > buf && *--end != '/')
+			;
+
+	return end + 1;
+}
+
+
 int
 main(int argc, char *argv[])
 {
@@ -174,19 +195,20 @@ main(int argc, char *argv[])
 	}
 
 	char cmd = argv[1][0];
-
-	if (strcmp(argv[1], "start") == 0 && argv[2])
-		return send_and_wait('u', argv[2]);
-	else if (strcmp(argv[1], "stop") == 0 && argv[2])
-		return send_and_wait('d', argv[2]);
-	else if (strcmp(argv[1], "restart") == 0 && argv[2])
-		return send_and_wait('r', argv[2]);
-	else if (strcmp(argv[1], "check") == 0 && argv[2])
+	char *service = normalize(argv[2]);
+
+	if (strcmp(argv[1], "start") == 0 && service)
+		return send_and_wait('u', service);
+	else if (strcmp(argv[1], "stop") == 0 && service)
+		return send_and_wait('d', service);
+	else if (strcmp(argv[1], "restart") == 0 && service)
+		return send_and_wait('r', service);
+	else if (strcmp(argv[1], "check") == 0 && service)
 		cmd = '?';
 
 	notifysock("");
 
-	dprintf(connfd, "%c%s", cmd, argv[2] ? argv[2] : "");
+	dprintf(connfd, "%c%s", cmd, service ? service : "");
 
 	int status = 1;