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