-
Notifications
You must be signed in to change notification settings - Fork 0
/
tscat.c
230 lines (191 loc) · 6.13 KB
/
tscat.c
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/* Copyright (c) 2020-2023, Michael Santos <[email protected]>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/resource.h>
#include <time.h>
#include <unistd.h>
#include "getnline.h"
#include "restrict_process.h"
#include "strtonum.h"
#define TS_VERSION "0.3.4"
enum { TS_WR_BLOCK = 0, TS_WR_DROP, TS_WR_EXIT };
typedef struct {
int output;
char *label;
char *format;
int write_error;
int print_timestamp;
} ts_state_t;
static int tscatin(ts_state_t *s);
static int tscatout(ts_state_t *s, char *buf, size_t buflen);
static void usage(void);
extern char *__progname;
static const struct option long_options[] = {
{"format", required_argument, NULL, 'f'},
{"output", required_argument, NULL, 'o'},
{"write-error", required_argument, NULL, 'W'},
{"help", no_argument, NULL, 'h'},
{NULL, 0, NULL, 0}};
int main(int argc, char *argv[]) {
int ch;
ts_state_t s = {0};
time_t now;
const char *errstr = NULL;
now = time(NULL);
if (now == -1)
err(EXIT_FAILURE, "time");
/* Initialize timezone before enabling process restrictions.
*
* localtime(3) reads /etc/timezone.
*/
(void)localtime(&now);
if (restrict_process_init() < 0)
err(EXIT_FAILURE, "restrict_process_init");
if (setvbuf(stdout, NULL, _IOLBF, 0) < 0)
err(EXIT_FAILURE, "setvbuf");
s.output = STDOUT_FILENO;
s.print_timestamp = 1;
while ((ch = getopt_long(argc, argv, "f:ho:W:", long_options, NULL)) != -1) {
switch (ch) {
case 'f':
s.format = optarg;
break;
case 'o':
s.output = strtonum(optarg, 0, 3, &errstr);
if (errstr != NULL)
errx(2, "strtonum: %s", errstr);
break;
case 'W':
if (strcmp(optarg, "block") == 0)
s.write_error = TS_WR_BLOCK;
else if (strcmp(optarg, "drop") == 0)
s.write_error = TS_WR_DROP;
else if (strcmp(optarg, "exit") == 0)
s.write_error = TS_WR_EXIT;
else
errx(2, "invalid option: %s: block|drop|exit", optarg);
break;
case 'h':
usage();
exit(0);
default:
usage();
exit(2);
}
}
argc -= optind;
argv += optind;
s.label = (argc == 0) ? "" : argv[0];
if (s.format == NULL)
s.format = "%FT%T%z";
if ((s.write_error != TS_WR_BLOCK) && (s.output & STDOUT_FILENO) &&
(fcntl(fileno(stdout), F_SETFL, O_NONBLOCK) < 0))
err(EXIT_FAILURE, "fcntl");
if ((s.write_error != TS_WR_BLOCK) && (s.output & STDERR_FILENO) &&
(fcntl(fileno(stderr), F_SETFL, O_NONBLOCK) < 0))
err(EXIT_FAILURE, "fcntl");
if (restrict_process_stdin() < 0)
err(EXIT_FAILURE, "restrict_process_stdin");
if (tscatin(&s) < 0)
err(EXIT_FAILURE, "tscatin");
return 0;
}
static int tscatin(ts_state_t *s) {
char *buf = NULL;
size_t buflen = 0;
ssize_t n;
while ((n = getnline(&buf, &buflen, 4096, stdin)) != -1) {
if (tscatout(s, buf, n) < 0) {
if (errno == EAGAIN && s->write_error == TS_WR_DROP)
continue;
return -1;
}
}
free(buf);
if (ferror(stdin))
return -1;
return 0;
}
static int tscatout(ts_state_t *s, char *buf, size_t n) {
char timestamp[64] = {0};
time_t now;
struct tm *tm;
int nl;
if (n == 0)
return 0;
nl = (buf[n - 1] == '\n');
now = time(NULL);
if (now == -1)
return -1;
tm = localtime(&now);
/* Linux:
* If the length of the result string (including the terminating
* null byte) would exceed max bytes, then strftime() returns 0, and the
* contents of the array are undefined.
*
* Note that the return value 0 does not necessarily indicate an error.
* For example, in many locales %p yields an empty string. An empty
* format string will likewise yield an empty string.
*
* OpenBSD:
* Note that while this implementation of strftime() will always NUL
* terminate buf, other implementations may not do so when maxsize is not
* large enough to store the entire time string. The contents of buf are
* implementation specific in this case.
*/
if (strftime(timestamp, sizeof(timestamp) - 1, s->format, tm) == 0)
timestamp[0] = '\0';
if (s->print_timestamp) {
if (s->output & STDOUT_FILENO)
if (fprintf(stdout, "%s%s%s%s", timestamp,
timestamp[0] == '\0' ? "" : " ", s->label,
s->label[0] == '\0' ? "" : " ") < 0)
return -1;
if (s->output & STDERR_FILENO)
if (fprintf(stderr, "%s%s%s%s", timestamp,
timestamp[0] == '\0' ? "" : " ", s->label,
s->label[0] == '\0' ? "" : " ") < 0)
return -1;
}
if (s->output & STDOUT_FILENO)
if (fprintf(stdout, "%s", buf) < 0)
return -1;
if (s->output & STDERR_FILENO)
if (fprintf(stderr, "%s", buf) < 0)
return -1;
s->print_timestamp = nl;
return 0;
}
static void usage() {
(void)fprintf(
stderr,
"%s: [OPTION] [<LABEL>]\n"
"Timestamp stdin to stdout/stderr\n"
"version: %s (using %s mode process restriction)\n\n"
"-o, --output <1|2|3> stdout=1, stderr=2, both=3 (default: 1)\n"
"-f, --format <fmt> timestamp format (see strftime(3)) (default: "
"%%F%%T%%z)\n"
"-W, --write-error <exit|drop|block>\n"
" behaviour if write buffer is full (default: "
"block)\n"
"-h, --help usage summary\n",
__progname, TS_VERSION, RESTRICT_PROCESS);
}