util: allow clearing cloexec bit

* src/util/util.h (virSetInherit): New prototype.
* src/util/util.c (virSetCloseExec): Move guts...
(virSetInherit): ...to new function, and allow clearing.
* src/libvirt_private.syms (util.h): Export it.
This commit is contained in:
Eric Blake 2011-03-26 05:19:28 -06:00
parent 60dea30b7d
commit 296eb0bbe3
3 changed files with 13 additions and 3 deletions

View File

@ -913,6 +913,7 @@ virRun;
virRunWithHook; virRunWithHook;
virSetBlocking; virSetBlocking;
virSetCloseExec; virSetCloseExec;
virSetInherit;
virSetNonBlock; virSetNonBlock;
virSetUIDGID; virSetUIDGID;
virSkipSpaces; virSkipSpaces;

View File

@ -273,13 +273,21 @@ int virSetNonBlock(int fd) {
} }
int virSetCloseExec(int fd)
{
return virSetInherit(fd, false);
}
#ifndef WIN32 #ifndef WIN32
int virSetCloseExec(int fd) { int virSetInherit(int fd, bool inherit) {
int flags; int flags;
if ((flags = fcntl(fd, F_GETFD)) < 0) if ((flags = fcntl(fd, F_GETFD)) < 0)
return -1; return -1;
flags |= FD_CLOEXEC; if (inherit)
flags &= ~FD_CLOEXEC;
else
flags |= FD_CLOEXEC;
if ((fcntl(fd, F_SETFD, flags)) < 0) if ((fcntl(fd, F_SETFD, flags)) < 0)
return -1; return -1;
return 0; return 0;
@ -931,7 +939,7 @@ virRunWithHook(const char *const*argv,
#else /* WIN32 */ #else /* WIN32 */
int virSetCloseExec(int fd ATTRIBUTE_UNUSED) int virSetInherit(int fd ATTRIBUTE_UNUSED, bool inherit ATTRIBUTE_UNUSED)
{ {
return -1; return -1;
} }

View File

@ -51,6 +51,7 @@ enum {
int virSetBlocking(int fd, bool blocking) ATTRIBUTE_RETURN_CHECK; int virSetBlocking(int fd, bool blocking) ATTRIBUTE_RETURN_CHECK;
int virSetNonBlock(int fd) ATTRIBUTE_RETURN_CHECK; int virSetNonBlock(int fd) ATTRIBUTE_RETURN_CHECK;
int virSetInherit(int fd, bool inherit) ATTRIBUTE_RETURN_CHECK;
int virSetCloseExec(int fd) ATTRIBUTE_RETURN_CHECK; int virSetCloseExec(int fd) ATTRIBUTE_RETURN_CHECK;
/* This will execute in the context of the first child /* This will execute in the context of the first child