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