4
0

cloexec.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /* closexec.c - set or clear the close-on-exec descriptor flag
  2. Copyright (C) 1991, 2004 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2, or (at your option)
  6. any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software Foundation,
  13. Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. The code is taken from glibc/manual/llio.texi */
  15. #if HAVE_CONFIG_H
  16. # include <config.h>
  17. #endif
  18. #include "cloexec.h"
  19. #if HAVE_UNISTD_H
  20. # include <unistd.h>
  21. #endif
  22. #if HAVE_FCNTL_H
  23. # include <fcntl.h>
  24. #endif
  25. #ifndef FD_CLOEXEC
  26. # define FD_CLOEXEC 1
  27. #endif
  28. /* Set the `FD_CLOEXEC' flag of DESC if VALUE is true,
  29. or clear the flag if VALUE is false.
  30. Return true on success, or false on error with `errno' set. */
  31. bool
  32. set_cloexec_flag (int desc, bool value)
  33. {
  34. #if defined F_GETFD && defined F_SETFD
  35. int flags = fcntl (desc, F_GETFD, 0);
  36. int newflags;
  37. if (flags < 0)
  38. return false;
  39. newflags = (value ? flags | FD_CLOEXEC : flags & ~FD_CLOEXEC);
  40. return (flags == newflags
  41. || fcntl (desc, F_SETFD, newflags) != -1);
  42. #else
  43. return true;
  44. #endif
  45. }