1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include "c_synonyms.h"
30 #include <sys/types.h>
31 #include <errno.h>
32 #include <fcntl.h>
33 #include <limits.h>
34 #include <signal.h>
35 #include <stdio.h>
36 #include <stdio_ext.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <unistd.h>
40 
41 #define	_FILE_FD_MAX 255
42 
43 /*
44  * This 32-bit only preloadable library enables extended fd FILE's.
45  */
46 
47 #pragma	init(init_STDIO_bad_fd)
48 
49 void
50 init_STDIO_bad_fd(void)
51 {
52 	int action = -1;	/* default signal */
53 	int closed_fd = -1;	/* default fd */
54 	char *ptr;
55 	int signal;
56 	int retval;
57 
58 	/*
59 	 * user specified badfd
60 	 */
61 	if ((ptr = getenv("_STDIO_BADFD")) != NULL) {
62 		closed_fd = atoi(ptr);
63 		if (closed_fd < 3 || closed_fd > _FILE_FD_MAX) {
64 			(void) fprintf(stderr, "File descriptor must be"
65 			    " in the range 3-%d inclusive.\n", _FILE_FD_MAX);
66 			exit(1);
67 		}
68 	}
69 
70 	/*
71 	 * user specified action
72 	 */
73 	if ((ptr = getenv("_STDIO_BADFD_SIGNAL")) != NULL) {
74 		/* accept numbers or symbolic names */
75 		if (strncmp(ptr, "SIG", 3) == 0)	/* begins with "SIG"? */
76 			ptr = ptr + 3;
77 		retval = str2sig(ptr, &signal);
78 		if (retval == -1) {
79 			(void) fprintf(stderr,
80 			    "Invalid signal name or number.\n");
81 			exit(1);
82 		}
83 		action = signal;
84 	}
85 
86 	if ((closed_fd = enable_extended_FILE_stdio(closed_fd, action)) == -1) {
87 		perror("enable_extended_FILE_stdio(3C)");
88 		exit(1);
89 	}
90 }
91