fcntl: add O_CLOEXEC/FD_CLOEXEC support

This commit is contained in:
chao.an 2020-02-03 22:21:54 +08:00 committed by Gregory Nutt
parent c015e42602
commit d07afc934e
6 changed files with 71 additions and 18 deletions

View File

@ -113,16 +113,38 @@ int file_vfcntl(FAR struct file *filep, int cmd, va_list ap)
* that refer to the same file. * that refer to the same file.
*/ */
{
ret = filep->f_oflags & O_CLOEXEC ? FD_CLOEXEC : 0;
}
break;
case F_SETFD: case F_SETFD:
/* Set the file descriptor flags defined in <fcntl.h>, that are associated /* Set the file descriptor flags defined in <fcntl.h>, that are
* with fd, to the third argument, arg, taken as type int. If the * associated with fd, to the third argument, arg, taken as type int.
* FD_CLOEXEC flag in the third argument is 0, the file shall remain open * If the FD_CLOEXEC flag in the third argument is 0, the file shall
* across the exec functions; otherwise, the file shall be closed upon * remain open across the exec functions; otherwise, the file shall
* successful execution of one of the exec functions. * be closed upon successful execution of one of the exec functions.
*/ */
{
int oflags = va_arg(ap, int);
if (oflags & ~FD_CLOEXEC)
{
ret = -ENOSYS; ret = -ENOSYS;
break; break;
}
if (oflags & FD_CLOEXEC)
{
filep->f_oflags |= O_CLOEXEC;
}
else
{
filep->f_oflags &= ~O_CLOEXEC;
}
}
break;
case F_GETFL: case F_GETFL:
/* Get the file status flags and file access modes, defined in /* Get the file status flags and file access modes, defined in

View File

@ -66,6 +66,7 @@
#define O_DSYNC O_SYNC /* Equivalent to OSYNC in NuttX */ #define O_DSYNC O_SYNC /* Equivalent to OSYNC in NuttX */
#define O_BINARY (1 << 8) /* Open the file in binary (untranslated) mode. */ #define O_BINARY (1 << 8) /* Open the file in binary (untranslated) mode. */
#define O_DIRECT (1 << 9) /* Avoid caching, write directly to hardware */ #define O_DIRECT (1 << 9) /* Avoid caching, write directly to hardware */
#define O_CLOEXEC (1 << 10) /* Close on execute */
/* Unsupported, but required open flags */ /* Unsupported, but required open flags */

View File

@ -110,21 +110,43 @@ int psock_vfcntl(FAR struct socket *psock, int cmd, va_list ap)
break; break;
case F_GETFD: case F_GETFD:
/* Get the file descriptor flags defined in <fcntl.h> that are associated /* Get the file descriptor flags defined in <fcntl.h> that are
* with the file descriptor fd. File descriptor flags are associated * associated with the file descriptor fd. File descriptor flags
* with a single file descriptor and do not affect other file descriptors * are associated with a single file descriptor and do not affect
* that refer to the same file. * other file descriptors that refer to the same file.
*/ */
{
ret = _SS_ISCLOEXEC(psock->s_flags) ? FD_CLOEXEC : 0;
}
break;
case F_SETFD: case F_SETFD:
/* Set the file descriptor flags defined in <fcntl.h>, that are associated /* Set the file descriptor flags defined in <fcntl.h>, that are
* with fd, to the third argument, arg, taken as type int. If the * associated with fd, to the third argument, arg, taken as type int.
* FD_CLOEXEC flag in the third argument is 0, the file shall remain open * If the FD_CLOEXEC flag in the third argument is 0, the file shall
* across the exec functions; otherwise, the file shall be closed upon * remain open across the exec functions; otherwise, the file shall
* successful execution of one of the exec functions. * 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; break;
case F_GETFL: case F_GETFL:

View File

@ -57,6 +57,7 @@
/* Definitions of 8-bit socket flags */ /* 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_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_LISTENING 0x10 /* Bit 4: SOCK_STREAM is listening */
#define _SF_BOUND 0x20 /* Bit 5: SOCK_STREAM is bound to an address */ #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 */ /* 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_ISNONBLOCK(s) (((s) & _SF_NONBLOCK) != 0)
#define _SS_ISLISTENING(s) (((s) & _SF_LISTENING) != 0) #define _SS_ISLISTENING(s) (((s) & _SF_LISTENING) != 0)
#define _SS_ISBOUND(s) (((s) & _SF_BOUND) != 0) #define _SS_ISBOUND(s) (((s) & _SF_BOUND) != 0)

View File

@ -76,3 +76,5 @@ endif
DEPPATH += --dep-path group DEPPATH += --dep-path group
VPATH += :group VPATH += :group
CFLAGS += ${shell $(INCDIR) $(INCDIROPT) "$(CC)" $(TOPDIR)$(DELIM)net}

View File

@ -41,11 +41,13 @@
#include <sched.h> #include <sched.h>
#include <errno.h> #include <errno.h>
#include <fcntl.h>
#include <nuttx/fs/fs.h> #include <nuttx/fs/fs.h>
#include <nuttx/net/net.h> #include <nuttx/net/net.h>
#include "sched/sched.h" #include "sched/sched.h"
#include "socket/socket.h"
#include "group/group.h" #include "group/group.h"
/**************************************************************************** /****************************************************************************
@ -114,7 +116,8 @@ static inline void sched_dupfiles(FAR struct task_tcb_s *tcb)
* i-node structure. * i-node structure.
*/ */
if (parent[i].f_inode) if (parent[i].f_inode &&
(parent[i].f_oflags & O_CLOEXEC) == 0)
{ {
/* Yes... duplicate it for the child */ /* Yes... duplicate it for the child */
@ -170,7 +173,8 @@ static inline void sched_dupsockets(FAR struct task_tcb_s *tcb)
* reference count. * reference count.
*/ */
if (parent[i].s_crefs > 0) if (parent[i].s_crefs > 0 &&
!_SS_ISCLOEXEC(parent[i].s_flags))
{ {
/* Yes... duplicate it for the child */ /* Yes... duplicate it for the child */