about summary refs log tree commit diff
path: root/vlogger.c
blob: 949fb5b0d3ec70fe91dbe092539ef1a6e05063b9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <unistd.h>
#include <limits.h>

static char pwd[PATH_MAX];

typedef struct {
  const char *const c_name;
  int c_val;
} CODE;

CODE prioritynames[] = {
	{ "alert", LOG_ALERT },
	{ "crit", LOG_CRIT },
	{ "debug", LOG_DEBUG },
	{ "emerg", LOG_EMERG },
	{ "err", LOG_ERR },
	{ "error", LOG_ERR },
	{ "info", LOG_INFO },
	{ "notice", LOG_NOTICE },
	{ "panic", LOG_EMERG },
	{ "warn", LOG_WARNING },
	{ "warning", LOG_WARNING },
	{ 0, -1 }
};

CODE facilitynames[] = {
	{ "auth", LOG_AUTH },
	{ "authpriv", LOG_AUTHPRIV },
	{ "cron", LOG_CRON },
	{ "daemon", LOG_DAEMON },
	{ "ftp", LOG_FTP },
	{ "kern", LOG_KERN },
	{ "lpr", LOG_LPR },
	{ "mail", LOG_MAIL },
	{ "news", LOG_NEWS },
	{ "security", LOG_AUTH },
	{ "syslog", LOG_SYSLOG },
	{ "user", LOG_USER },
	{ "uucp", LOG_UUCP },
	{ "local0", LOG_LOCAL0 },
	{ "local1", LOG_LOCAL1 },
	{ "local2", LOG_LOCAL2 },
	{ "local3", LOG_LOCAL3 },
	{ "local4", LOG_LOCAL4 },
	{ "local5", LOG_LOCAL5 },
	{ "local6", LOG_LOCAL6 },
	{ "local7", LOG_LOCAL7 },
	{ 0, -1 }
};

static void
strpriority(char *s, int *facility, int *level)
{
	char *p;
	CODE *cp;

	if ((p = strchr(s, '.'))) {
		*p++ = 0;
		for (cp = prioritynames; cp->c_name; cp++) {
			if (strcmp(cp->c_name, p) == 0)
				*level = cp->c_val;
		}
	}
	if (*s)
		for (cp = facilitynames; cp->c_name; cp++) {
			if (strcmp(cp->c_name, s) == 0)
				*facility = cp->c_val;
		}
}

int
main(int argc, char *argv[])
{
	char buf[1024];
	char *p, *argv0;
	char *tag = "vlogger";
	int c;
	int Sflag = 0;
	int logflags = 0;
	int facility = LOG_DAEMON;
	int level = LOG_INFO;

	argv0 = *argv;

	if (strcmp(argv0, "./run") == 0) {
		p = getcwd(pwd, sizeof (pwd));
		if (p != NULL && *pwd == '/') {
			if (*(p = pwd+(strlen(pwd)-1)) == '/')
				*p = '\0';
			if ((p = strrchr(pwd, '/')) && strncmp(p+1, "log", 3) == 0 &&
			    (*p = '\0', (p = strrchr(pwd, '/'))) && (*(p+1) != '\0')) {
				tag = strdup(p+1);
			}
		}
	}

	while ((c = getopt(argc, argv, "f:ip:Sst:")) != -1)
		switch (c) {
		case 'f':
			if (freopen(optarg, "r", stdin) == NULL) {
				fprintf(stderr, "vlogger: %s: %s\n", optarg, strerror(errno));
				return 1;
			}
			break;
		case 'i': logflags |= LOG_PID; break;
		case 'p': strpriority(optarg, &facility, &level); break;
		case 'S': Sflag++; break;
		case 's': logflags |= LOG_PERROR; break;
		case 't': tag = optarg; break;
		default:
			fprintf(stderr, "usage: vlogger [-isS] [-f file] [-p pri] [-t tag] [message ...]\n");
			exit(1);
		}
	argc -= optind;
	argv += optind;

	if (!Sflag && access("/etc/vlogger", X_OK) != -1) {
		CODE *cp;
		const char *sfacility, *slevel;
		for (cp = prioritynames; cp->c_name; cp++) {
			if (cp->c_val == level)
				slevel = cp->c_name;
		}
		for (cp = facilitynames; cp->c_name; cp++) {
			if (cp->c_val == facility)
				sfacility = cp->c_name;
		}
		execl("/etc/vlogger", argv0, tag, slevel, sfacility, (char *)0);
		fprintf(stderr, "vlogger: exec: %s\n", strerror(errno));
		exit(1);
	}

	openlog(tag, logflags, facility);

	if (argc > 0) {
		size_t len;
		char *p, *e;
		p = buf;
		*p = '\0';
		e = buf + sizeof buf - 2;
		for (; *argv;) {
			len = strlen(*argv);
			if (p + len > e && p > buf) {
				syslog(level|facility, "%s", buf);
				p = buf;
				*p = '\0';
			}
			if (len > sizeof buf - 1) {
				syslog(level|facility, "%s", *argv++);
			} else {
				if (p != buf) {
					*p++ = ' ';
					*p = '\0';
				}
				strncat(p, *argv++, e-p);
				p += len;
			}
		}
		if (p != buf)
			syslog(level|facility, "%s", buf);
		return 0;
	}

	while (fgets(buf, sizeof buf, stdin) != NULL)
		syslog(level|facility, "%s", buf);

	return 0;
}