aboutsummaryrefslogtreecommitdiffstats
path: root/makewrappers
blob: 11ef4075b1237747671f965b1dace91aec0a90a4 (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
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
#!/bin/sh
#
# makewrappers, script to auto-generate wrapper functions
#
# Copyright (c) 2008-2010 Wind River Systems, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the Lesser GNU General Public License version 2.1 as
# published by the Free Software Foundation.
#
# This program 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 Lesser GNU General Public License for more details.
#
# You should have received a copy of the Lesser GNU General Public License
# version 2.1 along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
#

case $# in
0)	echo >&2 "Usage: makewrappers file [...]"
	exit 1
	;;
esac

# save old versions
test -f "pseudo_wrappers.c" && mv pseudo_wrappers.c pseudo_wrappers.c.old
test -f "pseudo_wrappers.h" && mv pseudo_wrappers.h pseudo_wrappers.h.old

# create files
exec 5>pseudo_wrappers.c
exec 6>pseudo_wrappers.h
exec 7>pseudo_wrapper_table.c

# "cat >&N <<EOF" populates the file on &N with the here-document.

# pseudo_wrappers.c has to have all the hunks used by the wrapper functions,
# including guts/*.c.
cat >&5 <<EOF
`cat guts/COPYRIGHT`
/* wrapper functions. generated automatically. */

/* This file is generated and should not be modified.  See the makewrappers
 * script if you want to modify this. */

#include <stdlib.h>
#include <limits.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <pthread.h>

#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <dlfcn.h>

#include "pseudo.h"
#include "pseudo_wrappers.h"
#include "pseudo_ipc.h"
#include "pseudo_client.h"

static void pseudo_enosys(const char *);
static int pseudo_populate_wrappers(void);
static volatile int antimagic = 0;
static pthread_mutex_t pseudo_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_t pseudo_mutex_holder;
static int pseudo_mutex_recursion = 0;

int
pseudo_getlock() {
	if (pthread_equal(pseudo_mutex_holder, pthread_self())) {
		++pseudo_mutex_recursion;
		return 0;
	} else {
		if (pthread_mutex_lock(&pseudo_mutex) == 0) {
			pseudo_mutex_recursion = 1;
			pseudo_mutex_holder = pthread_self();
			return 0;
		} else {
			return -1;
		}
	}
}

void
pseudo_droplock() {
	if (--pseudo_mutex_recursion == 0) {
		pseudo_mutex_holder = 0;
		pthread_mutex_unlock(&pseudo_mutex);
	}
}

void
pseudo_antimagic() {
	++antimagic;
}

void
pseudo_magic() {
	if (antimagic > 0)
		--antimagic;
}

EOF

cat >&6 <<EOF
`cat guts/COPYRIGHT`
EOF

# the wrapper function table is also #included, it is a separate file so it
# can be written as we go, but still be a single table.
cat >&7 <<EOF
`cat guts/COPYRIGHT`
/* The table of wrapper functions to populate */

/* This file is generated and should not be modified.  See the makewrappers
 * script if you want to modify this. */
static struct {
	char *name;		/* the name */
	int (**real)(void);	/* the underlying syscall */
	int (*dummy)(void);	/* the always-fails form */
	int (*wrapper)(void);	/* the wrapper from guts/name.c */
} pseudo_functions[] = {
EOF

printf >&2 'Reading signatures...\n'
for file in "$@"
do
	# read lines containing wrappable functions, write wrapper
	# declarations, definitions, and so on.
	printf >&2 '[%s]' "$file"
	while read signature
	do
		# skip comments
		case $signature in
		\#*)	continue;;
		esac
		# obtain return type, name, and arguments
		args=`expr "$signature" : '.*(\(.*\));'`
		return_and_name=`expr "$signature" : '\(.*\)('`
		name=`expr "$return_and_name" : '.*[^a-zA-Z0-9_]\([a-zA-Z0-9_]*\)$'`
		type=`expr "$return_and_name" : '\(.*[^ ]\) *'"$name"'$'`
		printf >&2 ' %s' "$name"
		argnames=''
		save_IFS=$IFS
		IFS=,
		set -- $args
		IFS=$save_ifs
		args=''
		optional_arg=false
		for arg
		do
			# strip whitespace
			arg=${arg# }
			arg=${arg% }

			# handle optional arguments, like the third arg
			# to open()
			case $arg in
			...*)
				optional_arg=true
				args="$args${args:+, }..."
				arg=`expr "$arg" : '\.\.\.{\(.*\)}'`
				argname=`expr "$arg" : '.*[^a-zA-Z0-9_]\([a-zA-Z0-9_]*\)$'`
				argnames="$argnames${argnames:+, }$argname"

				# we need this to extract and pass the argument
				optional_decl=$arg
				optional_prev=$prev_argname
				optional_name=$argname
				optional_type=`expr "$arg" : '\(.*[^ ]\) *'"$argname"'$'`
				;;
			*)
				argname=`expr "$arg" : '.*[^a-zA-Z0-9_]\([a-zA-Z0-9_]*\)$'`
				args="$args${args:+, }$arg"
				argnames="$argnames${argnames:+, }$argname"
				prev_argname=$argname
				;;
			esac
		done

		# determine default return value.
		case $type in
		int)
			default_value=-1;;
		uid_t|gid_t)
			default_value=0;;
		'FILE *')
			default_value=NULL;;
		*)	echo >&2 "Unknown type '$type'." ; exit 1 ;;
		esac
		# create the wrappers
		# first the dummy, and the function pointer:
		cat >&5 <<EOF
static $type
dummy_$name($args) {
	pseudo_enosys("$name");
	errno = ENOSYS;
	return $default_value;
}

static $type (*real_$name)($args) = dummy_$name;

EOF
		# then the wrapper signature and args:
		if $optional_arg; then
			cat >&5 <<EOF
$type
$name($args) {
	$optional_decl;
	va_list ap;
	va_start(ap, $optional_prev);
	$optional_name = va_arg(ap, $optional_type);
	va_end(ap);

EOF
		else
			cat >&5 <<EOF
$type
$name($args) {
EOF
		fi
		# and now the body of the wrapper:
		cat >&5 <<EOF
	if (pseudo_getlock()) {
		errno = EBUSY;
		return $default_value;
	}
	if (pseudo_populate_wrappers()) {
		$type rc = $default_value;
		int save_errno;
		if (antimagic > 0) {
			if (real_$name) {
				rc = (*real_$name)($argnames);
			} else {
				rc = dummy_$name($argnames);
			}
		} else {
			rc = wrap_$name($argnames);
		}
		save_errno = errno;
		pseudo_droplock();
		errno = save_errno;
		return rc;
	} else {
		pseudo_droplock();
		return dummy_$name($argnames);
	}
}

EOF
		# and now the signature part for the actual implementation:
		# and the guts include file.

		# the wrapper function is actually declared in
		# pseudo_wrapper.c, with guts implemented in a separate
		# file with comments indicating the signature.
		guts="guts/$name.c"

		# the actual wrapper function, including argument setup
		if $optional_arg; then
			cat >&5 << EOF
static $type
wrap_$name($args) {
	$type rc = $default_value;
	$optional_decl;

	va_list ap;
	va_start(ap, $optional_prev);
	$optional_name = va_arg(ap, $optional_type);
	va_end(ap);

#include "$guts"

	return rc;
}
EOF
		else
			cat >&5 << EOF
static $type
wrap_$name($args) {
	$type rc = $default_value;

#include "$guts"

	return rc;
}
EOF
		fi

		# if the guts file didn't already exist, create a default.
		if test ! -f "$guts"; then
			if $optional_arg; then
				cat > "$guts" <<EOF
/* 
 * static $type
 * wrap_$name($args$optional_decl) {
 *	$type rc = $default_value;
 */

	rc = real_$name($argnames);

/*	return rc;
 * }
 */
EOF
			else
				cat > "$guts" <<EOF
/* 
 * static $type
 * wrap_$name($args) {
 *	$type rc = $default_value;
 */

	rc = real_$name($argnames);

/*	return rc;
 * }
 */
EOF
			fi
		fi
		# prototypes for pseudo_wrappers.h
		cat >&6 <<EOF
/* $type $name($args); */
static $type dummy_$name($args);
static $type wrap_$name($args);
static $type (*real_$name)($args);

EOF

		# and entries in the Big Table
		cat >&7 <<EOF
	{ /* $type $name($args); */
		"$name",
		(int (**)(void)) &real_$name,
		(int (*)(void)) dummy_$name,
		(int (*)(void)) wrap_$name
	},
EOF
	done < $file
done
printf >&2 '.\n'

# sentinel values
cat >&7 <<EOF
	{ NULL, NULL, NULL, NULL },
};
EOF

# and now the actual populate_wrappers function.
cat >&5 <<EOF
#include "pseudo_wrapper_table.c"

extern char *program_invocation_short_name;

static void
pseudo_enosys(const char *func) {
	pseudo_diag("pseudo: ENOSYS for '%s'.\n", func ? func : "<nil>");
	if (getenv("PSEUDO_ENOSYS_ABORT"))
		abort();
}

static int
pseudo_populate_wrappers(void) {
	int i;
	char *debug;
	static int done = 0;
	char *pseudo_path = 0;

	if (done)
		return done;
	pseudo_getlock();
	pseudo_antimagic();
	for (i = 0; pseudo_functions[i].name; ++i) {
		if (*pseudo_functions[i].real == pseudo_functions[i].dummy) {
			int (*f)(void);
			char *e;
			dlerror();
			f = dlsym(RTLD_NEXT, pseudo_functions[i].name);
			if ((e = dlerror()) != NULL) {
				/* leave it pointed to dummy */
				pseudo_diag("No wrapper for %s: %s\n", pseudo_functions[i].name, e);
			} else {
				if (f)
					*pseudo_functions[i].real = f;
			}
		}
	}
	done = 1;
	debug = getenv("PSEUDO_DEBUG");
	if (debug) {
		int level = atoi(debug);
		for (i = 0; i < level; ++i) {
			pseudo_debug_verbose();
		}
	}
	/* must happen after wrappers are set up, because it can call
	 * getcwd(), which needs wrappers, but must happen here so that
	 * any attempt to use a path in a wrapper function will have a
	 * value for cwd.
	 */
	pseudo_client_reset();
	pseudo_path = pseudo_prefix_path(NULL);
	if (pseudo_dir_fd == -1) {
		if (pseudo_path) {
			pseudo_dir_fd = open(pseudo_path, O_RDONLY);
			pseudo_dir_fd = pseudo_fd(pseudo_dir_fd, MOVE_FD);
			free(pseudo_path);
		} else {
			pseudo_diag("No prefix available to to find server.\n");
			exit(1);
		}
		if (pseudo_dir_fd == -1) {
			pseudo_diag("Can't open prefix path (%s) for server.\n",
				strerror(errno));
			exit(1);
		}
	}
	pseudo_debug(2, "(%s) set up wrappers\n", program_invocation_short_name);
	pseudo_magic();
	pseudo_droplock();
	return done;
}
EOF