From fd1422a8aab1a350f008d40a85c10800b94d709f Mon Sep 17 00:00:00 2001 From: Christopher Brannon Date: Sun, 16 Apr 2017 01:51:00 -0700 Subject: 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. --- vlogger.c | 31 +++++++++++++++++++++++++------ 1 file 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 #include #include +#include + +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) -- cgit 1.4.1