about summary refs log tree commit diff
diff options
context:
space:
mode:
authorChristopher Brannon <chris@the-brannons.com>2017-04-16 01:51:00 -0700
committerDuncaen <mail@duncano.de>2018-06-20 18:02:46 +0200
commitfd1422a8aab1a350f008d40a85c10800b94d709f (patch)
tree61a79b26693ba644d76fe79e8546088f73789f91
parent0b85cd8b88c92f6fe30a50ae46c796c68f4f77e9 (diff)
downloadrunit-void-fd1422a8aab1a350f008d40a85c10800b94d709f.tar.gz
runit-void-fd1422a8aab1a350f008d40a85c10800b94d709f.tar.xz
runit-void-fd1422a8aab1a350f008d40a85c10800b94d709f.zip
vlogger: Fix symlinked ./run handling, make sure tag is initialized.
If vlogger is symlinked to foo/log/run for some service foo,
it's invoked as ./run with $SERVICEDIR/foo/log as the cwd.
So extract the service name from cwd if argv[0] is ./run.
Also tag is initialized to the generic "vlogger" if it is
not given on the command line and servicename couldn't be extracted
from cwd.
-rw-r--r--vlogger.c31
1 files changed, 25 insertions, 6 deletions
diff --git a/vlogger.c b/vlogger.c
index 4fd407a..605ff37 100644
--- a/vlogger.c
+++ b/vlogger.c
@@ -4,6 +4,9 @@
 #include <string.h>
 #include <syslog.h>
 #include <unistd.h>
+#include <limits.h>
+
+static char pwd[PATH_MAX + 1];
 
 typedef struct {
   const char *const c_name;
@@ -73,19 +76,35 @@ strpriority(char *s, int *facility, int *level)
 int
 main(int argc, char *argv[])
 {
-	char c, *p, *argv0, *tag;
+	char c, *p, *argv0;
+	char *tag = "vlogger";
 	int facility = LOG_DAEMON;
 	int level = LOG_INFO;
+	size_t pwdlen = 0;
 
 	argv0 = *argv;
 
-	if (((p = strrchr(*argv, '/')) && !strncmp(p+1, "run", 3)) &&
-	    (*p = 0, (p = strrchr(*argv, '/')) && !strncmp(p+1, "log", 3)) &&
-	    (*p = 0, (p = strrchr(*argv, '/'))) != 0) {
-		if (!(tag = strdup(p+1))) {
-			fprintf(stderr, "vlogger: strdup: %s\n", strerror(errno));
+	if (!strcmp(argv0, "./run")) {
+		p = getcwd(pwd, sizeof(pwd));
+		if (p == NULL) {
+/* FIXME: roll our own getcwd that does dynamic allocation? */
+			fprintf(stderr, "vlogger: getcwd: %s\n", strerror(errno));
+			exit(1);
+		}
+		if (pwd[0] != '/') {
+			fprintf(stderr, "Current working directory is not reachable or not absolute.\n");
 			exit(1);
 		}
+		pwdlen = strlen(pwd);
+		if (pwd[pwdlen - 1] == '/')
+			pwd[pwdlen - 1] = '\0';
+		if ((p = strrchr(pwd, '/')) && !strncmp(p+1, "log", 3) &&
+		    (*p = 0, (p = strrchr(pwd, '/'))) && (*(p + 1) != 0)) {
+			if (!(tag = strdup(p+1))) {
+				fprintf(stderr, "vlogger: strdup: %s\n", strerror(errno));
+				exit(1);
+			}
+		}
 	}
 
 	while ((c = getopt(argc, argv, "p:t:")) != -1)