File: ripperX_plugin_tester.c

package info (click to toggle)
ripperx 2.7.2-3
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 2,256 kB
  • ctags: 609
  • sloc: sh: 10,579; ansic: 6,939; makefile: 145
file content (364 lines) | stat: -rw-r--r-- 8,029 bytes parent folder | download | duplicates (3)
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <signal.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>

#define TRUE  1
#define FALSE 0

#define PLUGIN_MSG_PARSE_ERR        -1	// also interpreted as 'no more output'
#define PLUGIN_PROGRESS_MSG         0
#define PLUGIN_WARN_MSG             1
#define PLUGIN_ERR_MSG              2

#define CHECK_INTERVAL              0.475

int add_argv (char **dest, char *content);
int open_a_pty (int *pty, int *tty);
char **create_argv_for_execution_using_shell (char *command);
int execute_ripper_encoder_with_plugin (char **program_argv,
                                        char **plugin_argv,
                                        pid_t * program_pid, pid_t * plugin_pid,
                                        int *read_fd);
void err_handler (char *msg);
int read_and_process_plugin_output (int read_fd, double *progress, char *msg);
int parse_plugin_output (char *out, double *progress, char *msg);

int
add_argv (char **dest, char *content)
{
	size_t i;

	i = 0;

	while (content[i++] != '\0');

	if ((*dest = malloc (i)) == NULL) {
		err_handler ("Cannot allocate memory");
		return FALSE;
	}

	strcpy (*dest, content);
	return TRUE;
}

void
err_handler (char *msg)
{
	fprintf (stderr, "Error : %s\n", msg);
}

int
open_a_pty (int *pty, int *tty)
{
	char line[12];
	char *p1, *p2;
	char *cp;
	int i;

	sprintf (line, "/dev/ptyXX");
	p1 = &line[8];
	p2 = &line[9];

	for (cp = "pqrstuvwxyzPQRST"; *cp; cp++) {
		struct stat stb;

		*p1 = *cp;
		*p2 = '0';
		/*
		 * This stat() check is just to keep us from
		 * looping through all 256 combinations if there
		 * aren't that many ptys available.
		 */
		if (stat (line, &stb) < 0)
			break;
		for (i = 0; i < 16; i++) {
			*p2 = "0123456789abcdef"[i];
			*pty = open (line, O_RDWR);
			if (*pty > 0) {
				line[5] = 't';
				/* Now open appropriate tty */
				if ((*tty = open (line, O_RDWR)) < 0) {
					line[5] = 'p';
					close (*pty);
					continue;
				}
				return 0;
			}
		}
	}
	return -1;
}

char **
create_argv_for_execution_using_shell (char *command)
{
	char *shell;
	char **argv;

	shell = "/bin/bash";

	if ((argv = (char **) malloc (sizeof (char *) * 4)) == NULL) {
		err_handler ("malloc failed");
		return NULL;
	}
	argv[0] = NULL;
	argv[1] = NULL;
	argv[2] = NULL;
	argv[3] = NULL;

	if (add_argv (&(argv[0]), shell) == FALSE)
		return NULL;
	if (add_argv (&(argv[1]), "-c") == FALSE)
		return NULL;
	if (add_argv (&(argv[2]), command) == FALSE)
		return NULL;
	return argv;
}

int
execute_ripper_encoder_with_plugin (char **program_argv,
                                    char **plugin_argv,
                                    pid_t * program_pid, pid_t * plugin_pid,
                                    int *read_fd)
{
	int pty_fd0, tty_fd0, pty_fd1, tty_fd1;
	pid_t pid;

	/* Open two pty/tty pairs */
	if (open_a_pty (&pty_fd0, &tty_fd0) < 0) {
		err_handler ("Cannot open pty/tty pair");
		return -1;
	}
	if (open_a_pty (&pty_fd1, &tty_fd1) < 0) {
		close (pty_fd0);
		close (tty_fd0);
		err_handler ("Cannot open pty/tty pair");
		return -1;
	}

	// fork & exec & link plugin
	if ((pid = fork ()) < 0) {
		err_handler ("Cannot fork");
		return -1;
	}
	*plugin_pid = pid;

	if (pid == 0) {
		// We're in the child process
		// save stderr
		int stderr_fd;
		stderr_fd = dup (2);

		dup2 (pty_fd0, 0);
		dup2 (tty_fd1, 1);

		execvp (plugin_argv[0], plugin_argv);

		dup2 (stderr_fd, 2);
		perror ("Failed to exec plugin");
		_exit (127);
	}

	// we're in the parent process
	close (pty_fd0);
	close (tty_fd1);
	*read_fd = pty_fd1;

	// fork the real program
	if ((pid = fork ()) < 0) {
		err_handler ("Cannot fork");
		return -1;
	}
	*program_pid = pid;

	if (pid == 0) {
		// We're in the child process
		// save stderr
		int stderr_fd;
		stderr_fd = dup (2);

		dup2 (tty_fd0, 1);
		dup2 (tty_fd0, 2);

		execvp (program_argv[0], program_argv);

		dup2 (stderr_fd, 2);
		perror ("Failed to exec the specified program");
		_exit (127);
	}
	close (tty_fd0);

	return 0;
}

int
read_and_process_plugin_output (int read_fd, double *progress, char *msg)
{
	int bytes_avail;
	char buf[1024];
	FILE *read_stream;

	ioctl (read_fd, FIONREAD, &bytes_avail);
	if (bytes_avail <= 0) {
		fprintf (stderr, "*** No report available from plugin. Ctrl-C to stop\n");
		// the plugin hasn't printed anything yet. return PLUGIN_MSG_PARSE_ERR
		// which the caller should ignore.
		return PLUGIN_MSG_PARSE_ERR;
	}

	// all the lines are terminated with '\n' and if the plugin started to
	// print something then it'll finish it soon. so using fgets is
	// reasonable

	read_stream = fdopen (read_fd, "r");
	if (fgets (buf, sizeof (buf), read_stream) == NULL)
		return PLUGIN_MSG_PARSE_ERR;
	fprintf (stderr, "*** Output read from plugin : %s", buf);
	return parse_plugin_output (buf, progress, msg);
}

int
parse_plugin_output (char *out, double *progress, char *msg)
{
	int pos, done, s, d;
	char ch;

	*progress = -1;
	msg[0] = '\0';

	pos = 0;
	while (out[pos] != '\0' && out[pos] != '[')
		pos++;

	// parse err point 0 : cannot find beginning '['
	if (out[pos] != '[') {
		fprintf (stderr, "*** parse err at : 0 : cannot find leading '['\n");
		return PLUGIN_MSG_PARSE_ERR;
	}
	pos++;

	// read the type character
	ch = out[pos++];

	if (ch == 'P')
		// if it's a msg reporting progess, read the percentage
		sscanf (out + pos, "%lf", progress);

	while (out[pos] != '\0' && out[pos] != '"' && out[pos] != ']')
		pos++;

	if (out[pos] == '"') {
		// we've got some message
		pos++;

		// copy the message
		done = 0;
		s = pos;
		d = 0;
		while (!done) {
			if (out[s] != '\\' && out[s] != '"' &&
				        out[s] != ']' && out[s] != '\0')
				msg[d++] = out[s++];
			else if (out[s] == '\\') {
				msg[d] = out[s + 1];
				s += 2;
				d++;
			} else {
				msg[d] = '\0';
				done = 1;
			}
		}
	}

	switch (ch) {
	case 'P':
		// parse err point 1 : invalid progress
		if (*progress < 0 || *progress > 1) {
			fprintf (stderr, "*** parse err at : 1 : invalid progress : %f\n",
			         *progress);
			return PLUGIN_MSG_PARSE_ERR;
		}
		return PLUGIN_PROGRESS_MSG;
	case 'W':
		// parse err point 2 : warning report without msg
		if (msg[0] == '\0') {
			fprintf (stderr, "*** parse err at : 2 : warning report w/o msg\n");
			return PLUGIN_MSG_PARSE_ERR;
		}
		return PLUGIN_WARN_MSG;
	case 'E':
		// parse err point 3 : error report without msg
		if (msg[0] == '\0') {
			fprintf (stderr, "*** parse err at : 3 : error report w/o msg\n");
			return PLUGIN_MSG_PARSE_ERR;
		}
		return PLUGIN_ERR_MSG;
	default:
		return PLUGIN_MSG_PARSE_ERR;
	}
}

int
main (int argc, char **argv)
{
	char *pg_com, *pi_com;
	char **pg_argv, **pi_argv;
	pid_t pg_pid, pi_pid;
	int read_fd;
	double progress;
	char msg[1024];
	int rs;

	if (argc != 3) {
		fprintf (stderr, "Syntax: ripperX_plugin_tester"
		         " \"program command with args\" \"plugin command\"\n");
		exit (1);
	}
	pg_com = argv[1];
	pi_com = argv[2];
	fprintf (stdout, "***Executing \"%s\"\n"
	         "with plugin \"%s\"\n", pg_com, pi_com);

	pg_argv = create_argv_for_execution_using_shell (pg_com);
	pi_argv = create_argv_for_execution_using_shell (pi_com);

	pg_pid = 0;
	pi_pid = 0;

	if (execute_ripper_encoder_with_plugin (pg_argv, pi_argv, &pg_pid, &pi_pid,
		                                        &read_fd) < 0)
		exit (1);

	while (1) {
		usleep (CHECK_INTERVAL * 1000000);
		printf ("\n");
		if ((rs = read_and_process_plugin_output (read_fd, &progress, msg))
			        == PLUGIN_MSG_PARSE_ERR) {
			//if (msg != NULL)
			//free(msg);
			fprintf (stderr, "*** PLUGIN_MSG_PARSE_ERR returned\n");
			continue;
		}
		switch (rs) {
		case PLUGIN_PROGRESS_MSG:
			printf ("Report type : progress, Progress : %f,\nMsg : %s\n",
			        progress, msg);
			break;
		case PLUGIN_WARN_MSG:
			printf ("Report type : warning,\nMsg : %s\n", msg);
			break;
		case PLUGIN_ERR_MSG:
			printf ("Report type : error,\nMsg : %s\n", msg);
			break;
		}
	}

	return 0;
}