aboutsummaryrefslogtreecommitdiff
path: root/t/rt.c
blob: 345ab8b5774c16125484e55b62b29d33ea4db8c7 (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
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
/* runcap - run program and capture its output
   Copyright (C) 2017 Sergey Poznyakoff

   Runcap is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by the
   Free Software Foundation; either version 3 of the License, or (at your
   option) any later version.

   Runcap is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License along
   with Runcap. If not, see <http://www.gnu.org/licenses/>. */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <errno.h>
#include "runcap.h"

static char *progname;

void
error(char const *fmt, ...)
{
	va_list ap;
	
	fprintf(stderr, "%s: ", progname);
	va_start(ap, fmt);
	vfprintf(stderr, fmt, ap);
	va_end(ap);
	fputc('\n', stderr);
}

void
usage(int code)
{
	FILE *fp = code ? stderr : stdout;
	fprintf(fp, "%s [OPTIONS] COMMAND [ARG...]\n", progname);
	fprintf(fp, "OPTIONS are:\n\n");
	fprintf(fp, "  -S all|stderr|stdout   selects capture for the next -m or -s option\n");
	fprintf(fp, "  -f FILE                reads stdin from FILE\n");
	fprintf(fp, "  -i                     inline read (use before -f)\n");
	fprintf(fp, "  -m                     monitors each line recevied from the program (see -S)\n");
	fprintf(fp, "  -p PROGNAME            sets program name to use instead of COMMAND\n");
	fprintf(fp, "  -s SIZE                sets capture size (see -S)\n");
	fprintf(fp, "  -t SECONDS             sets execution timeout\n");
	fputc('\n', fp);
	exit(code);
}

#define WA_STDOUT 0x01
#define WA_STDERR 0x02
#define WA_ALL (WA_STDOUT|WA_STDERR)

static int
whatarg(char const *arg)
{
	if (strcmp(arg, "all") == 0)
		return WA_ALL;
	else if (strcmp(arg, "stdout") == 0)
		return WA_STDOUT;
	else if (strcmp(arg, "stderr") == 0)
		return WA_STDERR;
	error("unreconginzed option argument: %s", arg);
	exit(1);
}

static void
linemon(const char *ptr, size_t len, void *data)
{
	fprintf(stdout, "[%s]: ", (char*) data);
	fwrite(ptr, len-1, 1, stdout);
	fputc('\n', stdout);
}

int
main(int argc, char **argv)
{
	struct runcap rc;
	int rcf = 0;
	int what = WA_ALL;
	int inopt = 0;

	int c;
	int fd;
	unsigned long size;
	
	progname = strrchr(argv[0], '/');
	if (progname)
		progname++;
	else
		progname = argv[0];
	while ((c = getopt(argc, argv, "?f:imp:S:s:t:")) != EOF) {
		switch (c) {
		case 'f':
			fd = open(optarg, O_RDONLY);
			if (fd == -1) {
				error("can't open \"%s\": %s",
				      optarg, strerror(errno));
				exit(1);
			}
			if (inopt) {
				struct stat st;
				char *buffer;
				if (fstat(fd, &st)) {
					error("can't fstat \"%s\": %s",
					      optarg, strerror(errno));
					exit(1);
				}
				size = st.st_size;
				buffer = malloc(size + 1);
				if (!buffer) {
					error("not enough memory");
					exit(1);
				}
				
				rc.rc_cap[RUNCAP_STDIN].fc_size = size;
				rc.rc_cap[RUNCAP_STDIN].fc_base = buffer;
				while (size) {
					ssize_t n = read(fd, buffer, size);
					if (n < 0) {
						error("error reading from \"%s\": %s",
						      optarg, strerror(errno));
						exit(1);
					}
					if (n == 0) {
						error("unexpected eof on \"%s\"",
						      optarg);
						exit(1);
					}
					size -= n;
					buffer += n;
				}
				close(fd);
				rc.rc_cap[RUNCAP_STDIN].fc_fd = -1;
			} else {				 
				rc.rc_cap[RUNCAP_STDIN].fc_fd = fd;
				rc.rc_cap[RUNCAP_STDIN].fc_size = 0;
			}
			rcf |= RCF_STDIN;
			break;
		case 'i':
			inopt = 1;
			break;
		case 'S':
			what = whatarg(optarg);
			break;
		case 'm':
			if (what & WA_STDOUT) {
				rc.rc_cap[RUNCAP_STDOUT].fc_linemon = linemon;
				rc.rc_cap[RUNCAP_STDOUT].fc_monarg = "stdout";
				rcf |= RCF_STDOUT_LINEMON;
			}
			if (what & WA_STDERR) {
				rc.rc_cap[RUNCAP_STDERR].fc_linemon = linemon;
				rc.rc_cap[RUNCAP_STDERR].fc_monarg = "stderr";
				rcf |= RCF_STDERR_LINEMON;
			}
			break;
		case 'p':
			rc.rc_program = optarg;
			rcf |= RCF_PROGRAM;
			break;
		case 's':
			size = strtoul(optarg, NULL, 10);
			if (what & WA_STDOUT) {
				rc.rc_cap[RUNCAP_STDOUT].fc_size = size;
				rcf |= RCF_STDOUT_SIZE;
			}
			if (what & WA_STDERR) {
				rc.rc_cap[RUNCAP_STDERR].fc_size = size;
				rcf |= RCF_STDERR_SIZE;
			}
			break;
		case 't':
			rc.rc_timeout = strtoul(optarg, NULL, 10);
			rcf |= RCF_TIMEOUT;
			break;
		default:
			usage(optopt != '?');
		}
	}

	if (argc == optind) {
		static char *xargv[2];
		if (rcf & RCF_PROGRAM) {
			xargv[0] = rc.rc_program;
			xargv[1] = NULL;
			rc.rc_argv = xargv;
		} else
			usage(1);
	} else
		rc.rc_argv = argv + optind;

	c = runcap(&rc, rcf);

	printf("res=%d\n", c);
	if (c) {
		error("system error: %s", strerror(rc.rc_errno));
		exit(1);
	}

	if (WIFEXITED(rc.rc_status)) {
                printf("exit code: %d\n", WEXITSTATUS(rc.rc_status));
        } else if (WIFSIGNALED(rc.rc_status)) {
                printf("got signal: %d\n", WTERMSIG(rc.rc_status));
        } else if (WIFSTOPPED(rc.rc_status)) {
                printf("stopped by signal %d\n", WSTOPSIG(rc.rc_status));
        } else
                printf("unrecognized status: %d\n", rc.rc_status);

	printf("stdout: %zu lines, %zu bytes\n",
               rc.rc_cap[RUNCAP_STDOUT].fc_nlines,
               rc.rc_cap[RUNCAP_STDOUT].fc_leng);
        printf("stderr: %zu lines, %zu bytes\n",
               rc.rc_cap[RUNCAP_STDERR].fc_nlines,
               rc.rc_cap[RUNCAP_STDERR].fc_leng);
	
	return 0;
}

Return to:

Send suggestions and report system problems to the System administrator.