Skip to content

Commit 879b0a1

Browse files
committed
In python bindings, make sure duped fd have cloexec
1 parent 9ec514f commit 879b0a1

1 file changed

Lines changed: 13 additions & 2 deletions

File tree

bindings/python/auparse_python.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "structmember.h"
44

55
#include <errno.h>
6+
#include <fcntl.h>
67
#include <time.h>
78
#include <stdint.h>
89
#include <unistd.h>
@@ -53,6 +54,16 @@ int PyFile_Check(PyObject *f) {
5354
static int debug = 0;
5455
static PyObject *NoParserError = NULL;
5556

57+
/*
58+
* duplicate_fd_cloexec - duplicate a file descriptor with close-on-exec set
59+
* fd: file descriptor to duplicate
60+
* returns: duplicated descriptor on success, -1 on error with errno set
61+
*/
62+
static inline int duplicate_fd_cloexec(int fd)
63+
{
64+
return fcntl(fd, F_DUPFD_CLOEXEC, 0);
65+
}
66+
5667
/*===========================================================================
5768
* AuEvent
5869
*===========================================================================*/
@@ -451,7 +462,7 @@ AuParser_init(AuParser *self, PyObject *args, PyObject *kwds)
451462
PyErr_SetString(PyExc_ValueError, "source must be resolvable to a file descriptor when source_type is AUSOURCE_DESCRIPTOR");
452463
return -1;
453464
}
454-
dup_fd = dup(fd);
465+
dup_fd = duplicate_fd_cloexec(fd);
455466
if (dup_fd < 0) {
456467
PyErr_SetFromErrno(PyExc_EnvironmentError);
457468
return -1;
@@ -477,7 +488,7 @@ AuParser_init(AuParser *self, PyObject *args, PyObject *kwds)
477488
PyErr_SetString(PyExc_TypeError, "source must be open file when source_type is AUSOURCE_FILE_POINTER");
478489
return -1;
479490
}
480-
dup_fd = dup(fd);
491+
dup_fd = duplicate_fd_cloexec(fd);
481492
if (dup_fd < 0) {
482493
PyErr_SetFromErrno(PyExc_EnvironmentError);
483494
return -1;

0 commit comments

Comments
 (0)