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