File: ext_io.c

package info (click to toggle)
tcng 10b-4
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 3,644 kB
  • sloc: ansic: 19,040; pascal: 4,640; yacc: 2,619; sh: 1,914; perl: 1,546; lex: 772; makefile: 751
file content (322 lines) | stat: -rw-r--r-- 6,779 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
/*
 * ext_io.c - Communication infrastructure for the external interface
 *
 * Written 2001,2004 by Werner Almesberger
 * Copyright 2001 Network Robots
 * Copyright 2004 Werner Almesberger
 */


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

#include "config.h"
#include "util.h"
#include "location.h"
#include "error.h"
#include "ext.h"


static FILE *stream = NULL;	/* NULL if "ext" subsystem is not active */
static pid_t pid = 0;		/* 0 if there is no process */
static char *in_path,*out_path;
static int no_exit_handler = 1; /* set to 1 in child process */


/* ----- Argument handling ------------------------------------------------- */


static char **my_argv;		/* undefined if pid != 0 */

typedef struct _ext_arg {
    const char *name;
    const char *arg;
    struct _ext_arg *next;
} EXT_ARG;

static EXT_ARG *ext_args = NULL,**last_arg = &ext_args;


static void copy_argv(char **argv,const char *name)
{
    const EXT_ARG *ext_arg;
    int argc,i;

    for (argc = 0; argv[argc]; argc++);
    for (ext_arg = ext_args; ext_arg; ext_arg = ext_arg->next)
	if (!*ext_arg->name || !strcmp(ext_arg->name,name)) argc++;
    my_argv = malloc(sizeof(char *)*(argc+1));
    for (i = 0; argv[i]; i++)
	my_argv[i] = stralloc(argv[i]);
    for (ext_arg = ext_args; ext_arg; ext_arg = ext_arg->next)
	if (!*ext_arg->name || !strcmp(ext_arg->name,name))
	    my_argv[i++] = stralloc(ext_arg->arg);
    my_argv[i] = NULL;
}


static void free_argv(void)
{
    int i;

    for (i = 0; my_argv[i]; i++)
	free(my_argv[i]);
    free(my_argv);
}


void add_tcc_external_arg(const char *name,const char *arg)
{
    *last_arg = alloc_t(EXT_ARG);
    (*last_arg)->name = stralloc(name);
    (*last_arg)->arg = stralloc(arg);
    (*last_arg)->next = NULL;
    last_arg = &(*last_arg)->next;
}


/* ----- Error formatter --------------------------------------------------- */


static void expand_errors(FILE *err_file)
{
    char line[MAX_EXT_LINE+2];

    while (fgets(line,MAX_EXT_LINE+2,err_file)) {
	char *start,*pos;

	start = pos = line;
	while (1) {
	    const LOCATION *loc;
	    char *end;

	    pos = strchr(pos,'[');
	    if (!pos) break;
	    if (pos[1] != '<') {
		pos++;
		continue;
	    }
	    end = pos+2;
	    while (1) {
		end = strchr(end,'>');
		if (!end) break;
		if (end[1] == ']') break;
		end++;
	    }
	    if (!end) break;
	    /*
	     * On 64 bit, ptrdiff_t may be larger than an int. Normally, gcc
	     * doesn't complain, but in a printf argument, it does. So we need
	     * a cast.
	     */
	    if (start != pos) fprintf(stderr,"%.*s",(int) (pos-start),start);
	    *end = 0;
	    loc = location_by_spec(pos+2);
	    *end = '>';
	    if (loc) print_location(stderr,*loc);
	    else fprintf(stderr,"%.*s",(int) (end-pos)+2,pos);
	    start = pos = end+2;
	}
	fprintf(stderr,"%s",start);
    }
}

/* ----- External program -------------------------------------------------- */


static void child(char **argv,int err_fds[2])
{
    const char *path;
    int fd;

    no_exit_handler = 1;
    path = getenv("PATH");
    if (path) {
	const char *tcng_topdir;
	char *new_path;

	tcng_topdir = getenv("TCNG_TOPDIR");
	if (!tcng_topdir) tcng_topdir = DATA_DIR;
	new_path = alloc(strlen(path)+strlen(tcng_topdir)+strlen(TCNG_BIN_DIR)+
	  3);
	sprintf(new_path,"%s:%s/%s",path,tcng_topdir,TCNG_BIN_DIR);
	if (setenv("PATH",new_path,1) < 0) {
	    perror("setenv");
	    exit(1);
	}
    }
    fd = open(in_path,O_RDONLY);
    if (fd < 0) {
	perror(in_path);
	exit(1);
    }
    if (dup2(fd,0) < 0) {
	perror("dup2(0)");
	exit(1);
    }
    fd = creat(out_path,0644);
    if (fd < 0) {
	perror(out_path);
	exit(1);
    }
    if (dup2(fd,1) < 0) {
	perror("dup2(1)");
	exit(1);
    }
    if (close(err_fds[0]) < 0) {
	perror("close");
	exit(1);
    }
    if (dup2(err_fds[1],2) < 0) {
	perror("dup2(2)");
	exit(1);
    }
    if (execvp(*argv,argv) < 0) {
	perror(*argv);
	exit(1);
    }
    /* not reached */
}


/* ----- Cleanup ----------------------------------------------------------- */


static void do_ext_close(int from_signal)
{
    /*
     * We have a few race conditions here, e.g. signal arriving during normal
     * exit. Can be fixed, but that's not exactly a priority item.
     */
    if (no_exit_handler) return;
    assert(stream);
    if (!from_signal) {
	if (fclose(stream) == EOF) {
	    perror("fclose");
	    _exit(1);
	}
	stream = NULL;
    }
    if (unlink(in_path) < 0) {
	perror(in_path);
	_exit(1);
    }
    if (pid) {
	if (unlink(out_path) < 0) {
	    perror(out_path);
	    _exit(1);
	}
	pid = 0;
    }
    if (!from_signal) {
	free_argv();
	free(in_path);
	free(out_path);
    }
}


static void cleanup_external(void)
{
    if (stream) do_ext_close(0);
}


static void interrupted(int sig)
{
    if (stream) do_ext_close(1);
    _exit(1);
}


/* ----- API --------------------------------------------------------------- */


FILE *ext_open(char **argv,const char *name)
{
    assert(!stream);
    in_path = alloc_sprintf("__ext_%d.in",getpid());
    out_path = alloc_sprintf("__ext_%d.out",getpid());
    stream = fopen(in_path,"w");
    if (!stream) {
	perror(in_path);
	exit(1);
    }
    if (no_exit_handler) {
	no_exit_handler = 0;
	atexit(cleanup_external);
	signal(SIGINT,interrupted);
    }
    copy_argv(argv,name);
    return stream;
}


void ext_read(const char *name,const char *action)
{
    FILE *err_file;
    int err_fds[2];
    int status;

    assert(stream);
    (void) fflush(stream);
    if (ferror(stream)) error("error writing to sub-process");
    if (!freopen(DEV_NULL,"r",stream)) { /* close old stream */
	perror("/dev/null");
	exit(1);
    }
    fflush(stdout);
    fflush(stderr);
    if (pipe(err_fds) < 0) {
	perror("pipe");
	exit(1);
    }
    err_file = fdopen(err_fds[0],"r");
    if (!err_file) {
	perror("fdopen");
	exit(1);
    }
    pid = fork();
    if (!pid) child(my_argv,err_fds);
    if (close(err_fds[1]) < 0) {
	perror("close");
	exit(1);
    }
    expand_errors(err_file);
    if (fclose(err_file) == EOF) {
	perror("fclose");
	exit(1);
    }
    if (waitpid(pid,&status,0) < 0) {
	perror("waitpid");
	exit(1);
    }
    if (!freopen(out_path,"r",stream)) {
	perror(out_path);
	exit(1);
    }
    if (WIFEXITED(status)) {
	if (!WEXITSTATUS(status)) return;
	errorf("external program \"%s\" exited with status %d %s",name,
	  WEXITSTATUS(status),action);
    }
    if (WIFSIGNALED(status))
	errorf("external program \"%s\" received signal %d %s",name,
	  WTERMSIG(status),action);
    errorf("external program \"%s\" returned unrecognized status %d %s",name,
      status,action);
}


void ext_close(void)
{
    do_ext_close(0);
}