mirror of
https://github.com/MariaDB/server.git
synced 2025-08-08 11:22:35 +03:00
Add close-on-exec flag to open(), socket(), accept() & fopen().
This commit is contained in:
@@ -315,6 +315,7 @@ ENDIF()
|
|||||||
#
|
#
|
||||||
# Tests for functions
|
# Tests for functions
|
||||||
#
|
#
|
||||||
|
CHECK_FUNCTION_EXISTS (accept4 HAVE_ACCEPT4)
|
||||||
CHECK_FUNCTION_EXISTS (access HAVE_ACCESS)
|
CHECK_FUNCTION_EXISTS (access HAVE_ACCESS)
|
||||||
#CHECK_FUNCTION_EXISTS (aiowait HAVE_AIOWAIT)
|
#CHECK_FUNCTION_EXISTS (aiowait HAVE_AIOWAIT)
|
||||||
CHECK_FUNCTION_EXISTS (aio_read HAVE_AIO_READ)
|
CHECK_FUNCTION_EXISTS (aio_read HAVE_AIO_READ)
|
||||||
|
@@ -609,6 +609,12 @@ typedef SOCKET_SIZE_TYPE size_socket;
|
|||||||
#ifndef O_NOFOLLOW
|
#ifndef O_NOFOLLOW
|
||||||
#define O_NOFOLLOW 0
|
#define O_NOFOLLOW 0
|
||||||
#endif
|
#endif
|
||||||
|
#ifndef O_CLOEXEC
|
||||||
|
#define O_CLOEXEC 0
|
||||||
|
#endif
|
||||||
|
#ifndef SOCK_CLOEXEC
|
||||||
|
#define SOCK_CLOEXEC 0
|
||||||
|
#endif
|
||||||
|
|
||||||
/* additional file share flags for win32 */
|
/* additional file share flags for win32 */
|
||||||
#ifdef __WIN__
|
#ifdef __WIN__
|
||||||
|
@@ -553,7 +553,7 @@ inline_mysql_socket_socket
|
|||||||
int domain, int type, int protocol)
|
int domain, int type, int protocol)
|
||||||
{
|
{
|
||||||
MYSQL_SOCKET mysql_socket= MYSQL_INVALID_SOCKET;
|
MYSQL_SOCKET mysql_socket= MYSQL_INVALID_SOCKET;
|
||||||
mysql_socket.fd= socket(domain, type, protocol);
|
mysql_socket.fd= socket(domain, type | SOCK_CLOEXEC, protocol);
|
||||||
|
|
||||||
#ifdef HAVE_PSI_SOCKET_INTERFACE
|
#ifdef HAVE_PSI_SOCKET_INTERFACE
|
||||||
if (likely(mysql_socket.fd != INVALID_SOCKET))
|
if (likely(mysql_socket.fd != INVALID_SOCKET))
|
||||||
@@ -1013,6 +1013,8 @@ inline_mysql_socket_accept
|
|||||||
#endif
|
#endif
|
||||||
MYSQL_SOCKET socket_listen, struct sockaddr *addr, socklen_t *addr_len)
|
MYSQL_SOCKET socket_listen, struct sockaddr *addr, socklen_t *addr_len)
|
||||||
{
|
{
|
||||||
|
int flags;
|
||||||
|
|
||||||
MYSQL_SOCKET socket_accept= MYSQL_INVALID_SOCKET;
|
MYSQL_SOCKET socket_accept= MYSQL_INVALID_SOCKET;
|
||||||
socklen_t addr_length= (addr_len != NULL) ? *addr_len : 0;
|
socklen_t addr_length= (addr_len != NULL) ? *addr_len : 0;
|
||||||
|
|
||||||
@@ -1026,7 +1028,17 @@ inline_mysql_socket_accept
|
|||||||
(&state, socket_listen.m_psi, PSI_SOCKET_CONNECT, (size_t)0, src_file, src_line);
|
(&state, socket_listen.m_psi, PSI_SOCKET_CONNECT, (size_t)0, src_file, src_line);
|
||||||
|
|
||||||
/* Instrumented code */
|
/* Instrumented code */
|
||||||
|
#ifdef HAVE_ACCEPT4
|
||||||
|
socket_accept.fd= accept4(socket_listen.fd, addr, &addr_length,
|
||||||
|
SOCK_CLOEXEC);
|
||||||
|
#else
|
||||||
socket_accept.fd= accept(socket_listen.fd, addr, &addr_length);
|
socket_accept.fd= accept(socket_listen.fd, addr, &addr_length);
|
||||||
|
flags= fcntl(socket_accept.fd, F_GETFD);
|
||||||
|
if (flags != -1) {
|
||||||
|
flags |= FD_CLOEXEC;
|
||||||
|
fcntl(socket_accept.fd, F_SETFD, flags);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
/* Instrumentation end */
|
/* Instrumentation end */
|
||||||
if (locker != NULL)
|
if (locker != NULL)
|
||||||
@@ -1036,7 +1048,17 @@ inline_mysql_socket_accept
|
|||||||
#endif
|
#endif
|
||||||
{
|
{
|
||||||
/* Non instrumented code */
|
/* Non instrumented code */
|
||||||
|
#ifdef HAVE_ACCEPT4
|
||||||
|
socket_accept.fd= accept4(socket_listen.fd, addr, &addr_length,
|
||||||
|
SOCK_CLOEXEC);
|
||||||
|
#else
|
||||||
socket_accept.fd= accept(socket_listen.fd, addr, &addr_length);
|
socket_accept.fd= accept(socket_listen.fd, addr, &addr_length);
|
||||||
|
flags= fcntl(socket_accept.fd, F_GETFD);
|
||||||
|
if (flags != -1) {
|
||||||
|
flags |= FD_CLOEXEC;
|
||||||
|
fcntl(socket_accept.fd, F_SETFD, flags);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef HAVE_PSI_SOCKET_INTERFACE
|
#ifdef HAVE_PSI_SOCKET_INTERFACE
|
||||||
|
@@ -43,7 +43,7 @@ File my_create(const char *FileName, int CreateFlags, int access_flags,
|
|||||||
#if defined(_WIN32)
|
#if defined(_WIN32)
|
||||||
fd= my_win_open(FileName, access_flags | O_CREAT);
|
fd= my_win_open(FileName, access_flags | O_CREAT);
|
||||||
#else
|
#else
|
||||||
fd= open((char *) FileName, access_flags | O_CREAT,
|
fd= open((char *) FileName, access_flags | O_CREAT | O_CLOEXEC,
|
||||||
CreateFlags ? CreateFlags : my_umask);
|
CreateFlags ? CreateFlags : my_umask);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@@ -42,7 +42,7 @@ static void make_ftype(char * to,int flag);
|
|||||||
FILE *my_fopen(const char *filename, int flags, myf MyFlags)
|
FILE *my_fopen(const char *filename, int flags, myf MyFlags)
|
||||||
{
|
{
|
||||||
FILE *fd;
|
FILE *fd;
|
||||||
char type[5];
|
char type[10];
|
||||||
DBUG_ENTER("my_fopen");
|
DBUG_ENTER("my_fopen");
|
||||||
DBUG_PRINT("my",("Name: '%s' flags: %d MyFlags: %lu",
|
DBUG_PRINT("my",("Name: '%s' flags: %d MyFlags: %lu",
|
||||||
filename, flags, MyFlags));
|
filename, flags, MyFlags));
|
||||||
@@ -351,9 +351,11 @@ static void make_ftype(register char * to, register int flag)
|
|||||||
else
|
else
|
||||||
*to++= 'r';
|
*to++= 'r';
|
||||||
|
|
||||||
#if FILE_BINARY /* If we have binary-files */
|
|
||||||
if (flag & FILE_BINARY)
|
if (flag & FILE_BINARY)
|
||||||
*to++='b';
|
*to++='b';
|
||||||
#endif
|
|
||||||
|
if (O_CLOEXEC)
|
||||||
|
*to++= 'e';
|
||||||
|
|
||||||
*to='\0';
|
*to='\0';
|
||||||
} /* make_ftype */
|
} /* make_ftype */
|
||||||
|
@@ -46,9 +46,9 @@ File my_open(const char *FileName, int Flags, myf MyFlags)
|
|||||||
#if defined(_WIN32)
|
#if defined(_WIN32)
|
||||||
fd= my_win_open(FileName, Flags);
|
fd= my_win_open(FileName, Flags);
|
||||||
#elif !defined(NO_OPEN_3)
|
#elif !defined(NO_OPEN_3)
|
||||||
fd = open(FileName, Flags, my_umask); /* Normal unix */
|
fd = open(FileName, Flags | O_CLOEXEC, my_umask); /* Normal unix */
|
||||||
#else
|
#else
|
||||||
fd = open((char *) FileName, Flags);
|
fd = open((char *) FileName, Flags | O_CLOEXEC);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
fd= my_register_filename(fd, FileName, FILE_BY_OPEN,
|
fd= my_register_filename(fd, FileName, FILE_BY_OPEN,
|
||||||
|
Reference in New Issue
Block a user