mirror of
https://github.com/apache/nuttx.git
synced 2026-06-06 16:50:55 +08:00
fcntl: add O_CLOEXEC/FD_CLOEXEC support
This commit is contained in:
+32
-10
@@ -110,21 +110,43 @@ int psock_vfcntl(FAR struct socket *psock, int cmd, va_list ap)
|
||||
break;
|
||||
|
||||
case F_GETFD:
|
||||
/* Get the file descriptor flags defined in <fcntl.h> that are associated
|
||||
* with the file descriptor fd. File descriptor flags are associated
|
||||
* with a single file descriptor and do not affect other file descriptors
|
||||
* that refer to the same file.
|
||||
/* Get the file descriptor flags defined in <fcntl.h> that are
|
||||
* associated with the file descriptor fd. File descriptor flags
|
||||
* are associated with a single file descriptor and do not affect
|
||||
* other file descriptors that refer to the same file.
|
||||
*/
|
||||
|
||||
{
|
||||
ret = _SS_ISCLOEXEC(psock->s_flags) ? FD_CLOEXEC : 0;
|
||||
}
|
||||
break;
|
||||
|
||||
case F_SETFD:
|
||||
/* Set the file descriptor flags defined in <fcntl.h>, that are associated
|
||||
* with fd, to the third argument, arg, taken as type int. If the
|
||||
* FD_CLOEXEC flag in the third argument is 0, the file shall remain open
|
||||
* across the exec functions; otherwise, the file shall be closed upon
|
||||
* successful execution of one of the exec functions.
|
||||
/* Set the file descriptor flags defined in <fcntl.h>, that are
|
||||
* associated with fd, to the third argument, arg, taken as type int.
|
||||
* If the FD_CLOEXEC flag in the third argument is 0, the file shall
|
||||
* remain open across the exec functions; otherwise, the file shall
|
||||
* be closed upon successful execution of one of the exec functions.
|
||||
*/
|
||||
|
||||
ret = -ENOSYS; /* F_GETFD and F_SETFD not implemented */
|
||||
{
|
||||
int oflags = va_arg(ap, int);
|
||||
|
||||
if (oflags & ~FD_CLOEXEC)
|
||||
{
|
||||
ret = -ENOSYS;
|
||||
break;
|
||||
}
|
||||
|
||||
if (oflags & FD_CLOEXEC)
|
||||
{
|
||||
psock->s_flags |= _SF_CLOEXEC;
|
||||
}
|
||||
else
|
||||
{
|
||||
psock->s_flags &= ~_SF_CLOEXEC;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case F_GETFL:
|
||||
|
||||
@@ -57,6 +57,7 @@
|
||||
|
||||
/* Definitions of 8-bit socket flags */
|
||||
|
||||
#define _SF_CLOEXEC 0x04 /* Bit 2: Close on execute */
|
||||
#define _SF_NONBLOCK 0x08 /* Bit 3: Don't block if no data (TCP/READ only) */
|
||||
#define _SF_LISTENING 0x10 /* Bit 4: SOCK_STREAM is listening */
|
||||
#define _SF_BOUND 0x20 /* Bit 5: SOCK_STREAM is bound to an address */
|
||||
@@ -73,6 +74,7 @@
|
||||
|
||||
/* Macro to manage the socket state and flags */
|
||||
|
||||
#define _SS_ISCLOEXEC(s) (((s) & _SF_CLOEXEC) != 0)
|
||||
#define _SS_ISNONBLOCK(s) (((s) & _SF_NONBLOCK) != 0)
|
||||
#define _SS_ISLISTENING(s) (((s) & _SF_LISTENING) != 0)
|
||||
#define _SS_ISBOUND(s) (((s) & _SF_BOUND) != 0)
|
||||
|
||||
Reference in New Issue
Block a user