aboutsummaryrefslogtreecommitdiff
path: root/syslog.c
blob: 024428a7ecd979c95cfc2884c95af15ddf2d04c5 (plain)
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
174
175
176
177
178
179
180
181
182
183
184
/* SPDX-License-Identifier: ISC */
#include <getopt.h>
#include <syslog.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>

#include "syslogd.h"

static int facility = 1;
static int level = LOG_INFO;
static int flags = LOG_NDELAY | LOG_NOWAIT;
static const char *ident = "(shell)";

static const struct option options[] = {
	{ "help", no_argument, NULL, 'h' },
	{ "version", no_argument, NULL, 'V' },
	{ "console", required_argument, NULL, 'c' },
	{ "facility", required_argument, NULL, 'f' },
	{ "level", required_argument, NULL, 'l' },
	{ "ident", required_argument, NULL, 'i' },
	{ NULL, 0, NULL, 0 },
};

static const char *shortopt = "hVcf:l:i:";

static const char *helptext =
"Usage: syslog [OPTION]... [STRING]...\n\n"
"Concatenate the given STRINGs and send a log message to the syslog daemon.\n"
"\n"
"The following OPTIONSs can be used:\n"
"  -f, --facility <facility>  Logging facilty name or numeric identifier.\n"
"  -l, --level <level>        Log level name or numeric identifier.\n"
"  -i, --ident <name>         Program name for log syslog message.\n"
"                             Default is \"%s\".\n\n"
"  -c, --console              Write to the console if opening the syslog\n"
"                             socket fails.\n\n"
"  -h, --help                 Print this help text and exit\n"
"  -V, --version              Print version information and exit\n\n";

static const char *version_string =
"syslog (usyslog) " PACKAGE_VERSION "\n"
"Copyright (C) 2018 David Oberhollenzer\n\n"
"This is free software: you are free to change and redistribute it.\n"
"There is NO WARRANTY, to the extent permitted by law.\n";

static void usage(int status)
{
	const char *str;
	int i;

	if (status != EXIT_SUCCESS) {
		fputs("Try `syslog --help' for more information\n", stderr);
	} else {
		printf(helptext, ident);

		fputs("The following values can be used for --level:\n",
		      stdout);

		i = 0;
		while ((str = level_id_to_string(i)) != NULL) {
			printf("  %s (=%d)%s\n", str, i,
			       i == level ? ", set as default" : "");
			++i;
		}

		fputs("\nThe following values can be used for --facility:\n",
		      stdout);

		i = 0;
		while ((str = facility_id_to_string(i)) != NULL) {
			printf("  %s (=%d)%s\n", str, i,
			       i == facility ? ", set as default" : "");
			++i;
		}
	}

	exit(status);
}

static int readint(const char *str)
{
	int x = 0;

	if (!isdigit(*str))
		return -1;

	while (isdigit(*str))
		x = x * 10 + (*(str++)) - '0';

	return (*str == '\0') ? x : -1;
}

static void process_options(int argc, char **argv)
{
	int c;

	for (;;) {
		c = getopt_long(argc, argv, shortopt, options, NULL);
		if (c == -1)
			break;

		switch (c) {
		case 'f':
			facility = readint(optarg);
			if (facility >= 0)
				break;
			facility = facility_id_from_string(optarg);
			if (facility < 0) {
				fprintf(stderr, "Unknown facility name '%s'\n",
					optarg);
				usage(EXIT_FAILURE);
			}
			break;
		case 'l':
			level = readint(optarg);
			if (level >= 0)
				break;
			level = level_id_from_string(optarg);
			if (level < 0) {
				fprintf(stderr, "Unknown log level '%s'\n",
					optarg);
				usage(EXIT_FAILURE);
			}
			break;
		case 'i':
			ident = optarg;
			break;
		case 'c':
			flags |= LOG_CONS;
			break;
		case 'V':
			fputs(version_string, stdout);
			exit(EXIT_SUCCESS);
			break;
		case 'h':
			usage(EXIT_SUCCESS);
			break;
		default:
			usage(EXIT_FAILURE);
			break;
		}
	}
}


int main(int argc, char **argv)
{
	size_t len = 0;
	char *str;
	int i;

	process_options(argc, argv);

	if (optind >= argc) {
		fputs("Error: no log string provided.\n", stderr);
		usage(EXIT_FAILURE);
	}

	for (i = optind; i < argc; ++i)
		len += strlen(argv[i]);

	len += argc - optind - 1;

	str = calloc(1, len + 1);
	if (str == NULL) {
		fputs("syslog: out of memory\n", stderr);
		return EXIT_FAILURE;
	}

	for (i = optind; i < argc; ++i) {
		if (i > optind)
			strcat(str, " ");
		strcat(str, argv[i]);
	}

	openlog(ident, flags, facility << 3);
	syslog(level, "%s", str);
	closelog();

	free(str);
	return EXIT_SUCCESS;
}