Skip to content

Commit fd1422a

Browse files
CMBDuncaen
authored andcommitted
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.
1 parent 0b85cd8 commit fd1422a

1 file changed

Lines changed: 25 additions & 6 deletions

File tree

vlogger.c

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
#include <string.h>
55
#include <syslog.h>
66
#include <unistd.h>
7+
#include <limits.h>
8+
9+
static char pwd[PATH_MAX + 1];
710

811
typedef struct {
912
const char *const c_name;
@@ -73,19 +76,35 @@ strpriority(char *s, int *facility, int *level)
7376
int
7477
main(int argc, char *argv[])
7578
{
76-
char c, *p, *argv0, *tag;
79+
char c, *p, *argv0;
80+
char *tag = "vlogger";
7781
int facility = LOG_DAEMON;
7882
int level = LOG_INFO;
83+
size_t pwdlen = 0;
7984

8085
argv0 = *argv;
8186

82-
if (((p = strrchr(*argv, '/')) && !strncmp(p+1, "run", 3)) &&
83-
(*p = 0, (p = strrchr(*argv, '/')) && !strncmp(p+1, "log", 3)) &&
84-
(*p = 0, (p = strrchr(*argv, '/'))) != 0) {
85-
if (!(tag = strdup(p+1))) {
86-
fprintf(stderr, "vlogger: strdup: %s\n", strerror(errno));
87+
if (!strcmp(argv0, "./run")) {
88+
p = getcwd(pwd, sizeof(pwd));
89+
if (p == NULL) {
90+
/* FIXME: roll our own getcwd that does dynamic allocation? */
91+
fprintf(stderr, "vlogger: getcwd: %s\n", strerror(errno));
92+
exit(1);
93+
}
94+
if (pwd[0] != '/') {
95+
fprintf(stderr, "Current working directory is not reachable or not absolute.\n");
8796
exit(1);
8897
}
98+
pwdlen = strlen(pwd);
99+
if (pwd[pwdlen - 1] == '/')
100+
pwd[pwdlen - 1] = '\0';
101+
if ((p = strrchr(pwd, '/')) && !strncmp(p+1, "log", 3) &&
102+
(*p = 0, (p = strrchr(pwd, '/'))) && (*(p + 1) != 0)) {
103+
if (!(tag = strdup(p+1))) {
104+
fprintf(stderr, "vlogger: strdup: %s\n", strerror(errno));
105+
exit(1);
106+
}
107+
}
89108
}
90109

91110
while ((c = getopt(argc, argv, "p:t:")) != -1)

0 commit comments

Comments
 (0)