xref: /titanic_50/usr/src/lib/extendedFILE/common/extendedFILE.c (revision cd3e933325e68e23516a196a8fea7f49b1e497c3)
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 2008 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 <sys/types.h>
30 #include <errno.h>
31 #include <fcntl.h>
32 #include <limits.h>
33 #include <signal.h>
34 #include <stdio.h>
35 #include <stdio_ext.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <unistd.h>
39 
40 #define	_FILE_FD_MAX 255
41 
42 /*
43  * This 32-bit only preloadable library enables extended fd FILE's.
44  */
45 
46 #pragma	init(init_STDIO_bad_fd)
47 
48 void
49 init_STDIO_bad_fd(void)
50 {
51 	int action = -1;	/* default signal */
52 	int closed_fd = -1;	/* default fd */
53 	char *ptr;
54 	int signal;
55 	int retval;
56 
57 	/*
58 	 * user specified badfd
59 	 */
60 	if ((ptr = getenv("_STDIO_BADFD")) != NULL) {
61 		closed_fd = atoi(ptr);
62 		if (closed_fd < 3 || closed_fd > _FILE_FD_MAX) {
63 			(void) fprintf(stderr, "File descriptor must be"
64 			    " in the range 3-%d inclusive.\n", _FILE_FD_MAX);
65 			exit(1);
66 		}
67 	}
68 
69 	/*
70 	 * user specified action
71 	 */
72 	if ((ptr = getenv("_STDIO_BADFD_SIGNAL")) != NULL) {
73 		/* accept numbers or symbolic names */
74 		if (strncmp(ptr, "SIG", 3) == 0)	/* begins with "SIG"? */
75 			ptr = ptr + 3;
76 		retval = str2sig(ptr, &signal);
77 		if (retval == -1) {
78 			(void) fprintf(stderr,
79 			    "Invalid signal name or number.\n");
80 			exit(1);
81 		}
82 		action = signal;
83 	}
84 
85 	if ((closed_fd = enable_extended_FILE_stdio(closed_fd, action)) == -1) {
86 		perror("enable_extended_FILE_stdio(3C)");
87 		exit(1);
88 	}
89 }
90