142542f5fSchristos/***************************************************************************
242542f5fSchristos
342542f5fSchristos Copyright 2013 Intel Corporation.  All Rights Reserved.
442542f5fSchristos
542542f5fSchristos Permission is hereby granted, free of charge, to any person obtaining a
642542f5fSchristos copy of this software and associated documentation files (the
742542f5fSchristos "Software"), to deal in the Software without restriction, including
842542f5fSchristos without limitation the rights to use, copy, modify, merge, publish,
942542f5fSchristos distribute, sub license, and/or sell copies of the Software, and to
1042542f5fSchristos permit persons to whom the Software is furnished to do so, subject to
1142542f5fSchristos the following conditions:
1242542f5fSchristos
1342542f5fSchristos The above copyright notice and this permission notice (including the
1442542f5fSchristos next paragraph) shall be included in all copies or substantial portions
1542542f5fSchristos of the Software.
1642542f5fSchristos
1742542f5fSchristos THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
1842542f5fSchristos OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
1942542f5fSchristos MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
2042542f5fSchristos IN NO EVENT SHALL INTEL, AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
2142542f5fSchristos DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
2242542f5fSchristos OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
2342542f5fSchristos THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2442542f5fSchristos
2542542f5fSchristos **************************************************************************/
2642542f5fSchristos
2742542f5fSchristos#ifdef HAVE_CONFIG_H
2842542f5fSchristos#include "config.h"
2942542f5fSchristos#endif
3042542f5fSchristos
3142542f5fSchristos#include <fcntl.h>
3242542f5fSchristos#include <unistd.h>
3342542f5fSchristos
3442542f5fSchristos#include <misc.h> /* MAXCLIENTS */
3542542f5fSchristos
3642542f5fSchristos#include "fd.h"
3742542f5fSchristos
3842542f5fSchristosint fd_move_cloexec(int fd)
3942542f5fSchristos{
4042542f5fSchristos	int newfd;
4142542f5fSchristos
4242542f5fSchristos	newfd = fcntl(fd,
4342542f5fSchristos#ifdef F_DUPFD_CLOEXEC
4442542f5fSchristos		      F_DUPFD_CLOEXEC,
4542542f5fSchristos#else
4642542f5fSchristos		      F_DUPFD,
4742542f5fSchristos#endif
4842542f5fSchristos		      MAXCLIENTS);
4942542f5fSchristos	if (newfd < 0)
5042542f5fSchristos		return fd;
5142542f5fSchristos
5242542f5fSchristos#ifndef F_DUPFD_CLOEXEC
5342542f5fSchristos	newfd = fd_set_cloexec(newfd);
5442542f5fSchristos#endif
5542542f5fSchristos
5642542f5fSchristos	close(fd);
5742542f5fSchristos	return newfd;
5842542f5fSchristos}
5942542f5fSchristos
6042542f5fSchristosint fd_set_cloexec(int fd)
6142542f5fSchristos{
6242542f5fSchristos	int flags;
6342542f5fSchristos
6442542f5fSchristos	if (fd == -1)
6542542f5fSchristos		return fd;
6642542f5fSchristos
6742542f5fSchristos#ifdef FD_CLOEXEC
6842542f5fSchristos	flags = fcntl(fd, F_GETFD);
6942542f5fSchristos	if (flags != -1) {
7042542f5fSchristos		flags |= FD_CLOEXEC;
7142542f5fSchristos		fcntl(fd, F_SETFD, flags);
7242542f5fSchristos	}
7342542f5fSchristos#endif
7442542f5fSchristos
7542542f5fSchristos	return fd;
7642542f5fSchristos}
7742542f5fSchristos
7842542f5fSchristosint fd_set_nonblock(int fd)
7942542f5fSchristos{
8042542f5fSchristos	int flags;
8142542f5fSchristos
8242542f5fSchristos	if (fd == -1)
8342542f5fSchristos		return fd;
8442542f5fSchristos
8542542f5fSchristos	flags = fcntl(fd, F_GETFL);
8642542f5fSchristos	if (flags != -1) {
8742542f5fSchristos		flags |= O_NONBLOCK;
8842542f5fSchristos		fcntl(fd, F_SETFL, flags);
8942542f5fSchristos	}
9042542f5fSchristos
9142542f5fSchristos	return fd;
9242542f5fSchristos}
9342542f5fSchristos
94