extendedFILE.c revision a5f69788de7ac07553de47f7fec8c05a9a94c105
5cff782560a1c3cf913ba5574a5123a299f3315emh/*
5cff782560a1c3cf913ba5574a5123a299f3315emh * CDDL HEADER START
5cff782560a1c3cf913ba5574a5123a299f3315emh *
5cff782560a1c3cf913ba5574a5123a299f3315emh * The contents of this file are subject to the terms of the
5cff782560a1c3cf913ba5574a5123a299f3315emh * Common Development and Distribution License (the "License").
5cff782560a1c3cf913ba5574a5123a299f3315emh * You may not use this file except in compliance with the License.
5cff782560a1c3cf913ba5574a5123a299f3315emh *
5cff782560a1c3cf913ba5574a5123a299f3315emh * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
5cff782560a1c3cf913ba5574a5123a299f3315emh * or http://www.opensolaris.org/os/licensing.
5cff782560a1c3cf913ba5574a5123a299f3315emh * See the License for the specific language governing permissions
5cff782560a1c3cf913ba5574a5123a299f3315emh * and limitations under the License.
5cff782560a1c3cf913ba5574a5123a299f3315emh *
5cff782560a1c3cf913ba5574a5123a299f3315emh * When distributing Covered Code, include this CDDL HEADER in each
5cff782560a1c3cf913ba5574a5123a299f3315emh * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
5cff782560a1c3cf913ba5574a5123a299f3315emh * If applicable, add the following below this CDDL HEADER, with the
5cff782560a1c3cf913ba5574a5123a299f3315emh * fields enclosed by brackets "[]" replaced with your own identifying
5cff782560a1c3cf913ba5574a5123a299f3315emh * information: Portions Copyright [yyyy] [name of copyright owner]
5cff782560a1c3cf913ba5574a5123a299f3315emh *
5cff782560a1c3cf913ba5574a5123a299f3315emh * CDDL HEADER END
5cff782560a1c3cf913ba5574a5123a299f3315emh */
5cff782560a1c3cf913ba5574a5123a299f3315emh
4596d7e96cbde86cd5bd70b9cd44960693e88271Guoli Shu/*
5cff782560a1c3cf913ba5574a5123a299f3315emh * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
5cff782560a1c3cf913ba5574a5123a299f3315emh * Use is subject to license terms.
5cff782560a1c3cf913ba5574a5123a299f3315emh */
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh#pragma ident "%Z%%M% %I% %E% SMI"
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh#include "c_synonyms.h"
5cff782560a1c3cf913ba5574a5123a299f3315emh#include <sys/types.h>
5cff782560a1c3cf913ba5574a5123a299f3315emh#include <errno.h>
5cff782560a1c3cf913ba5574a5123a299f3315emh#include <fcntl.h>
5cff782560a1c3cf913ba5574a5123a299f3315emh#include <limits.h>
5cff782560a1c3cf913ba5574a5123a299f3315emh#include <signal.h>
2df1fe9ca32bb227b9158c67f5c00b54c20b10fdrandyf#include <stdio.h>
5cff782560a1c3cf913ba5574a5123a299f3315emh#include <stdio_ext.h>
5cff782560a1c3cf913ba5574a5123a299f3315emh#include <stdlib.h>
5cff782560a1c3cf913ba5574a5123a299f3315emh#include <string.h>
5cff782560a1c3cf913ba5574a5123a299f3315emh#include <unistd.h>
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh#define _FILE_FD_MAX 255
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh/*
5cff782560a1c3cf913ba5574a5123a299f3315emh * This 32-bit only preloadable library enables extended fd FILE's.
5cff782560a1c3cf913ba5574a5123a299f3315emh */
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh#pragma init(init_STDIO_bad_fd)
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emhvoid
5cff782560a1c3cf913ba5574a5123a299f3315emhinit_STDIO_bad_fd(void)
5cff782560a1c3cf913ba5574a5123a299f3315emh{
5cff782560a1c3cf913ba5574a5123a299f3315emh int action = -1; /* default signal */
5cff782560a1c3cf913ba5574a5123a299f3315emh int closed_fd = -1; /* default fd */
5cff782560a1c3cf913ba5574a5123a299f3315emh char *ptr;
5cff782560a1c3cf913ba5574a5123a299f3315emh int signal;
5cff782560a1c3cf913ba5574a5123a299f3315emh int retval;
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh /*
5cff782560a1c3cf913ba5574a5123a299f3315emh * user specified badfd
2df1fe9ca32bb227b9158c67f5c00b54c20b10fdrandyf */
5cff782560a1c3cf913ba5574a5123a299f3315emh if ((ptr = getenv("_STDIO_BADFD")) != NULL) {
5cff782560a1c3cf913ba5574a5123a299f3315emh closed_fd = atoi(ptr);
5cff782560a1c3cf913ba5574a5123a299f3315emh if (closed_fd < 3 || closed_fd > _FILE_FD_MAX) {
5cff782560a1c3cf913ba5574a5123a299f3315emh (void) fprintf(stderr, "File descriptor must be"
5cff782560a1c3cf913ba5574a5123a299f3315emh " in the range 3-%d inclusive.\n", _FILE_FD_MAX);
5cff782560a1c3cf913ba5574a5123a299f3315emh exit(1);
5cff782560a1c3cf913ba5574a5123a299f3315emh }
5cff782560a1c3cf913ba5574a5123a299f3315emh }
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh /*
2df1fe9ca32bb227b9158c67f5c00b54c20b10fdrandyf * user specified action
5cff782560a1c3cf913ba5574a5123a299f3315emh */
2df1fe9ca32bb227b9158c67f5c00b54c20b10fdrandyf if ((ptr = getenv("_STDIO_BADFD_SIGNAL")) != NULL) {
2df1fe9ca32bb227b9158c67f5c00b54c20b10fdrandyf /* accept numbers or symbolic names */
2df1fe9ca32bb227b9158c67f5c00b54c20b10fdrandyf if (strncmp(ptr, "SIG", 3) == 0) /* begins with "SIG"? */
5cff782560a1c3cf913ba5574a5123a299f3315emh ptr = ptr + 3;
5cff782560a1c3cf913ba5574a5123a299f3315emh retval = str2sig(ptr, &signal);
2df1fe9ca32bb227b9158c67f5c00b54c20b10fdrandyf if (retval == -1) {
2df1fe9ca32bb227b9158c67f5c00b54c20b10fdrandyf (void) fprintf(stderr,
2df1fe9ca32bb227b9158c67f5c00b54c20b10fdrandyf "Invalid signal name or number.\n");
2df1fe9ca32bb227b9158c67f5c00b54c20b10fdrandyf exit(1);
2df1fe9ca32bb227b9158c67f5c00b54c20b10fdrandyf }
2df1fe9ca32bb227b9158c67f5c00b54c20b10fdrandyf action = signal;
2df1fe9ca32bb227b9158c67f5c00b54c20b10fdrandyf }
2df1fe9ca32bb227b9158c67f5c00b54c20b10fdrandyf
2df1fe9ca32bb227b9158c67f5c00b54c20b10fdrandyf if ((closed_fd = enable_extended_FILE_stdio(closed_fd, action)) == -1) {
2df1fe9ca32bb227b9158c67f5c00b54c20b10fdrandyf perror("enable_extended_FILE_stdio(3C)");
2df1fe9ca32bb227b9158c67f5c00b54c20b10fdrandyf exit(1);
2df1fe9ca32bb227b9158c67f5c00b54c20b10fdrandyf }
2df1fe9ca32bb227b9158c67f5c00b54c20b10fdrandyf}
2df1fe9ca32bb227b9158c67f5c00b54c20b10fdrandyf