Commit 0f447810 authored by Nirbhay Choubey's avatar Nirbhay Choubey

Add close-on-exec flag to open(), socket(), accept() & fopen().

parent 70714d35
......@@ -315,6 +315,7 @@ ENDIF()
#
# Tests for functions
#
CHECK_FUNCTION_EXISTS (accept4 HAVE_ACCEPT4)
CHECK_FUNCTION_EXISTS (access HAVE_ACCESS)
#CHECK_FUNCTION_EXISTS (aiowait HAVE_AIOWAIT)
CHECK_FUNCTION_EXISTS (aio_read HAVE_AIO_READ)
......
......@@ -609,6 +609,12 @@ typedef SOCKET_SIZE_TYPE size_socket;
#ifndef O_NOFOLLOW
#define O_NOFOLLOW 0
#endif
#ifndef O_CLOEXEC
#define O_CLOEXEC 0
#endif
#ifndef SOCK_CLOEXEC
#define SOCK_CLOEXEC 0
#endif
/* additional file share flags for win32 */
#ifdef __WIN__
......
......@@ -553,7 +553,7 @@ inline_mysql_socket_socket
int domain, int type, int protocol)
{
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
if (likely(mysql_socket.fd != INVALID_SOCKET))
......@@ -1013,6 +1013,8 @@ inline_mysql_socket_accept
#endif
MYSQL_SOCKET socket_listen, struct sockaddr *addr, socklen_t *addr_len)
{
int flags;
MYSQL_SOCKET socket_accept= MYSQL_INVALID_SOCKET;
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);
/* 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);
flags= fcntl(socket_accept.fd, F_GETFD);
if (flags != -1) {
flags |= FD_CLOEXEC;
fcntl(socket_accept.fd, F_SETFD, flags);
}
#endif
/* Instrumentation end */
if (locker != NULL)
......@@ -1036,7 +1048,17 @@ inline_mysql_socket_accept
#endif
{
/* 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);
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
......
......@@ -43,7 +43,7 @@ File my_create(const char *FileName, int CreateFlags, int access_flags,
#if defined(_WIN32)
fd= my_win_open(FileName, access_flags | O_CREAT);
#else
fd= open((char *) FileName, access_flags | O_CREAT,
fd= open((char *) FileName, access_flags | O_CREAT | O_CLOEXEC,
CreateFlags ? CreateFlags : my_umask);
#endif
......
......@@ -42,7 +42,7 @@ static void make_ftype(char * to,int flag);
FILE *my_fopen(const char *filename, int flags, myf MyFlags)
{
FILE *fd;
char type[5];
char type[10];
DBUG_ENTER("my_fopen");
DBUG_PRINT("my",("Name: '%s' flags: %d MyFlags: %lu",
filename, flags, MyFlags));
......@@ -351,9 +351,11 @@ static void make_ftype(register char * to, register int flag)
else
*to++= 'r';
#if FILE_BINARY /* If we have binary-files */
if (flag & FILE_BINARY)
*to++='b';
#endif
if (O_CLOEXEC)
*to++= 'e';
*to='\0';
} /* make_ftype */
......@@ -46,9 +46,9 @@ File my_open(const char *FileName, int Flags, myf MyFlags)
#if defined(_WIN32)
fd= my_win_open(FileName, Flags);
#elif !defined(NO_OPEN_3)
fd = open(FileName, Flags, my_umask); /* Normal unix */
fd = open(FileName, Flags | O_CLOEXEC, my_umask); /* Normal unix */
#else
fd = open((char *) FileName, Flags);
fd = open((char *) FileName, Flags | O_CLOEXEC);
#endif
fd= my_register_filename(fd, FileName, FILE_BY_OPEN,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment