1428d7b3dSmrg/*************************************************************************** 2428d7b3dSmrg 3428d7b3dSmrg Copyright 2013 Intel Corporation. All Rights Reserved. 4428d7b3dSmrg 5428d7b3dSmrg Permission is hereby granted, free of charge, to any person obtaining a 6428d7b3dSmrg copy of this software and associated documentation files (the 7428d7b3dSmrg "Software"), to deal in the Software without restriction, including 8428d7b3dSmrg without limitation the rights to use, copy, modify, merge, publish, 9428d7b3dSmrg distribute, sub license, and/or sell copies of the Software, and to 10428d7b3dSmrg permit persons to whom the Software is furnished to do so, subject to 11428d7b3dSmrg the following conditions: 12428d7b3dSmrg 13428d7b3dSmrg The above copyright notice and this permission notice (including the 14428d7b3dSmrg next paragraph) shall be included in all copies or substantial portions 15428d7b3dSmrg of the Software. 16428d7b3dSmrg 17428d7b3dSmrg THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 18428d7b3dSmrg OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19428d7b3dSmrg MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. 20428d7b3dSmrg IN NO EVENT SHALL INTEL, AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, 21428d7b3dSmrg DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 22428d7b3dSmrg OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR 23428d7b3dSmrg THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24428d7b3dSmrg 25428d7b3dSmrg **************************************************************************/ 26428d7b3dSmrg 27428d7b3dSmrg#ifdef HAVE_CONFIG_H 28428d7b3dSmrg#include "config.h" 29428d7b3dSmrg#endif 30428d7b3dSmrg 31428d7b3dSmrg#include <fcntl.h> 32428d7b3dSmrg#include <unistd.h> 33428d7b3dSmrg 34428d7b3dSmrg#include <misc.h> /* MAXCLIENTS */ 35428d7b3dSmrg 36428d7b3dSmrg#include "fd.h" 37428d7b3dSmrg 38428d7b3dSmrgint fd_move_cloexec(int fd) 39428d7b3dSmrg{ 40428d7b3dSmrg int newfd; 41428d7b3dSmrg 42428d7b3dSmrg newfd = fcntl(fd, 43428d7b3dSmrg#ifdef F_DUPFD_CLOEXEC 44428d7b3dSmrg F_DUPFD_CLOEXEC, 45428d7b3dSmrg#else 46428d7b3dSmrg F_DUPFD, 47428d7b3dSmrg#endif 48428d7b3dSmrg MAXCLIENTS); 49428d7b3dSmrg if (newfd < 0) 50428d7b3dSmrg return fd; 51428d7b3dSmrg 52428d7b3dSmrg#ifndef F_DUPFD_CLOEXEC 53428d7b3dSmrg newfd = fd_set_cloexec(newfd); 54428d7b3dSmrg#endif 55428d7b3dSmrg 56428d7b3dSmrg close(fd); 57428d7b3dSmrg return newfd; 58428d7b3dSmrg} 59428d7b3dSmrg 60428d7b3dSmrgint fd_set_cloexec(int fd) 61428d7b3dSmrg{ 62428d7b3dSmrg int flags; 63428d7b3dSmrg 64428d7b3dSmrg if (fd == -1) 65428d7b3dSmrg return fd; 66428d7b3dSmrg 67428d7b3dSmrg#ifdef FD_CLOEXEC 68428d7b3dSmrg flags = fcntl(fd, F_GETFD); 69428d7b3dSmrg if (flags != -1) { 70428d7b3dSmrg flags |= FD_CLOEXEC; 71428d7b3dSmrg fcntl(fd, F_SETFD, flags); 72428d7b3dSmrg } 73428d7b3dSmrg#endif 74428d7b3dSmrg 75428d7b3dSmrg return fd; 76428d7b3dSmrg} 77428d7b3dSmrg 78428d7b3dSmrgint fd_set_nonblock(int fd) 79428d7b3dSmrg{ 80428d7b3dSmrg int flags; 81428d7b3dSmrg 82428d7b3dSmrg if (fd == -1) 83428d7b3dSmrg return fd; 84428d7b3dSmrg 85428d7b3dSmrg flags = fcntl(fd, F_GETFL); 86428d7b3dSmrg if (flags != -1) { 87428d7b3dSmrg flags |= O_NONBLOCK; 88428d7b3dSmrg fcntl(fd, F_SETFL, flags); 89428d7b3dSmrg } 90428d7b3dSmrg 91428d7b3dSmrg return fd; 92428d7b3dSmrg} 93428d7b3dSmrg 94