haikuwebkit/Source/WTF/wtf/UniStdExtras.h

63 lines
2.2 KiB
C
Raw Permalink Normal View History

[WK2] Looping for EINTR on close() is incorrect for Linux, at least https://bugs.webkit.org/show_bug.cgi?id=117266 Patch by Sergio Correia <sergio.correia@openbossa.org> on 2013-06-20 Reviewed by Darin Adler. Source/WebKit2: Call closeWithRetry() to work around a difference in how the retry needs to be done on Linux. * Platform/CoreIPC/unix/AttachmentUnix.cpp: (CoreIPC::Attachment::dispose): * Platform/CoreIPC/unix/ConnectionUnix.cpp: (CoreIPC::Connection::platformInvalidate): * Platform/unix/SharedMemoryUnix.cpp: (WebKit::SharedMemory::Handle::~Handle): (WebKit::SharedMemory::create): (WebKit::SharedMemory::~SharedMemory): (WebKit::SharedMemory::createHandle): * PluginProcess/PluginProcess.cpp: (WebKit::PluginProcess::createWebProcessConnection): * SharedWorkerProcess/SharedWorkerProcess.cpp: (WebKit::SharedWorkerProcess::createWebProcessConnection): * UIProcess/Launcher/qt/ProcessLauncherQt.cpp: (WebKit::ProcessLauncher::launchProcess): All these places had ``close-followed-by-EINTR-handling'' replaced with the new closeWithRetry() function added in this commit. Source/WTF: Added file UniStdExtras with a closeWithRetry() function that works around the EINTR behavior on Linux during a close() call: it closes the descriptor unconditionally even when the call is interrupted. * wtf/UniStdExtras.h: Added. (WTF::closeWithRetry): Wrapper around POSIX close() that handles EINTR correctly. Canonical link: https://commits.webkit.org/136001@main git-svn-id: https://svn.webkit.org/repository/webkit/trunk@151825 268f45cc-cd09-0410-ab3c-d52691b4dbfc
2013-06-21 06:02:58 +00:00
/*
* Copyright (C) 2013 Nokia Corporation and/or its subsidiary(-ies).
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef UniStdExtras_h
#define UniStdExtras_h
#include <errno.h>
#include <unistd.h>
namespace WTF {
WTF_EXPORT_PRIVATE bool setCloseOnExec(int fileDescriptor);
WTF_EXPORT_PRIVATE int dupCloseOnExec(int fileDescriptor);
[GTK] WebKitGtk+ uses too many file descriptors https://bugs.webkit.org/show_bug.cgi?id=152316 Reviewed by Michael Catanzaro. Source/WebKit2: The problem is that we are keeping file descriptors open in SharedMemory objects. Those objects can be kept alive for a long time, for example in case of cached resources sent from the network process as shareable resources. The thing is that we keep the file descriptor in the SharedMemory object only to be able to create a Handle, duplicating the file descriptor. However, we are also assuming that we create handles for SharedMemory objects created by another handle which is wrong. SharedMemory objects are created to map a file or data and then a handle is created to send it to another process, or to map an existing file or data for a given handle received from another process. They can also be created to wrap another map, but in that case we don't own the file descritor nor the mapped data. So, after mapping from a handle, we no longer need the file descriptor, and it can be closed to release it, while the mapped memory data will still be alive until munmap() is called. This drastically reduces the amount of file descriptors used by WebKitGTK+. * Platform/IPC/unix/ConnectionUnix.cpp: (IPC::readBytesFromSocket): Use setCloseOnExec(). (IPC::Connection::createPlatformConnection): Ditto. * Platform/SharedMemory.h: * Platform/unix/SharedMemoryUnix.cpp: (WebKit::SharedMemory::map): Close the file descriptor right after mmap. (WebKit::SharedMemory::~SharedMemory): Close the file descriptor only if it hasn't been closed yet. (WebKit::SharedMemory::createHandle): Add an ASSERT to ensure we only try to create a handle for SharedMemory objects having a valid file descriptor. Use dupCloseOnExec() to duplicate the handle and set the close on exec flag. * UIProcess/Launcher/gtk/ProcessLauncherGtk.cpp: (WebKit::ProcessLauncher::launchProcess): Use setCloseOnExec(). Source/WTF: Add helper functions to duplicate a file descriptor setting close on exec flag, and also to set close on exec flag to an existing file descriptor. * wtf/UniStdExtras.h: * wtf/UniStdExtras.cpp (WTF::setCloseOnExec): (WTF::dupCloseOnExec): Canonical link: https://commits.webkit.org/177855@main git-svn-id: https://svn.webkit.org/repository/webkit/trunk@203155 268f45cc-cd09-0410-ab3c-d52691b4dbfc
2016-07-13 08:36:01 +00:00
[WK2] Looping for EINTR on close() is incorrect for Linux, at least https://bugs.webkit.org/show_bug.cgi?id=117266 Patch by Sergio Correia <sergio.correia@openbossa.org> on 2013-06-20 Reviewed by Darin Adler. Source/WebKit2: Call closeWithRetry() to work around a difference in how the retry needs to be done on Linux. * Platform/CoreIPC/unix/AttachmentUnix.cpp: (CoreIPC::Attachment::dispose): * Platform/CoreIPC/unix/ConnectionUnix.cpp: (CoreIPC::Connection::platformInvalidate): * Platform/unix/SharedMemoryUnix.cpp: (WebKit::SharedMemory::Handle::~Handle): (WebKit::SharedMemory::create): (WebKit::SharedMemory::~SharedMemory): (WebKit::SharedMemory::createHandle): * PluginProcess/PluginProcess.cpp: (WebKit::PluginProcess::createWebProcessConnection): * SharedWorkerProcess/SharedWorkerProcess.cpp: (WebKit::SharedWorkerProcess::createWebProcessConnection): * UIProcess/Launcher/qt/ProcessLauncherQt.cpp: (WebKit::ProcessLauncher::launchProcess): All these places had ``close-followed-by-EINTR-handling'' replaced with the new closeWithRetry() function added in this commit. Source/WTF: Added file UniStdExtras with a closeWithRetry() function that works around the EINTR behavior on Linux during a close() call: it closes the descriptor unconditionally even when the call is interrupted. * wtf/UniStdExtras.h: Added. (WTF::closeWithRetry): Wrapper around POSIX close() that handles EINTR correctly. Canonical link: https://commits.webkit.org/136001@main git-svn-id: https://svn.webkit.org/repository/webkit/trunk@151825 268f45cc-cd09-0410-ab3c-d52691b4dbfc
2013-06-21 06:02:58 +00:00
inline int closeWithRetry(int fileDescriptor)
{
int ret;
#if OS(LINUX)
// Workaround for the Linux behavior of closing the descriptor
// unconditionally, even if the close() call is interrupted.
// See https://bugs.webkit.org/show_bug.cgi?id=117266 for more
// details.
if ((ret = close(fileDescriptor)) == -1 && errno == EINTR)
return 0;
#else
while ((ret = close(fileDescriptor)) == -1 && errno == EINTR) { }
#endif
return ret;
}
WTF_EXPORT_PRIVATE bool setNonBlock(int fileDescriptor);
[WK2] Looping for EINTR on close() is incorrect for Linux, at least https://bugs.webkit.org/show_bug.cgi?id=117266 Patch by Sergio Correia <sergio.correia@openbossa.org> on 2013-06-20 Reviewed by Darin Adler. Source/WebKit2: Call closeWithRetry() to work around a difference in how the retry needs to be done on Linux. * Platform/CoreIPC/unix/AttachmentUnix.cpp: (CoreIPC::Attachment::dispose): * Platform/CoreIPC/unix/ConnectionUnix.cpp: (CoreIPC::Connection::platformInvalidate): * Platform/unix/SharedMemoryUnix.cpp: (WebKit::SharedMemory::Handle::~Handle): (WebKit::SharedMemory::create): (WebKit::SharedMemory::~SharedMemory): (WebKit::SharedMemory::createHandle): * PluginProcess/PluginProcess.cpp: (WebKit::PluginProcess::createWebProcessConnection): * SharedWorkerProcess/SharedWorkerProcess.cpp: (WebKit::SharedWorkerProcess::createWebProcessConnection): * UIProcess/Launcher/qt/ProcessLauncherQt.cpp: (WebKit::ProcessLauncher::launchProcess): All these places had ``close-followed-by-EINTR-handling'' replaced with the new closeWithRetry() function added in this commit. Source/WTF: Added file UniStdExtras with a closeWithRetry() function that works around the EINTR behavior on Linux during a close() call: it closes the descriptor unconditionally even when the call is interrupted. * wtf/UniStdExtras.h: Added. (WTF::closeWithRetry): Wrapper around POSIX close() that handles EINTR correctly. Canonical link: https://commits.webkit.org/136001@main git-svn-id: https://svn.webkit.org/repository/webkit/trunk@151825 268f45cc-cd09-0410-ab3c-d52691b4dbfc
2013-06-21 06:02:58 +00:00
} // namespace WTF
using WTF::closeWithRetry;
[GTK] WebKitGtk+ uses too many file descriptors https://bugs.webkit.org/show_bug.cgi?id=152316 Reviewed by Michael Catanzaro. Source/WebKit2: The problem is that we are keeping file descriptors open in SharedMemory objects. Those objects can be kept alive for a long time, for example in case of cached resources sent from the network process as shareable resources. The thing is that we keep the file descriptor in the SharedMemory object only to be able to create a Handle, duplicating the file descriptor. However, we are also assuming that we create handles for SharedMemory objects created by another handle which is wrong. SharedMemory objects are created to map a file or data and then a handle is created to send it to another process, or to map an existing file or data for a given handle received from another process. They can also be created to wrap another map, but in that case we don't own the file descritor nor the mapped data. So, after mapping from a handle, we no longer need the file descriptor, and it can be closed to release it, while the mapped memory data will still be alive until munmap() is called. This drastically reduces the amount of file descriptors used by WebKitGTK+. * Platform/IPC/unix/ConnectionUnix.cpp: (IPC::readBytesFromSocket): Use setCloseOnExec(). (IPC::Connection::createPlatformConnection): Ditto. * Platform/SharedMemory.h: * Platform/unix/SharedMemoryUnix.cpp: (WebKit::SharedMemory::map): Close the file descriptor right after mmap. (WebKit::SharedMemory::~SharedMemory): Close the file descriptor only if it hasn't been closed yet. (WebKit::SharedMemory::createHandle): Add an ASSERT to ensure we only try to create a handle for SharedMemory objects having a valid file descriptor. Use dupCloseOnExec() to duplicate the handle and set the close on exec flag. * UIProcess/Launcher/gtk/ProcessLauncherGtk.cpp: (WebKit::ProcessLauncher::launchProcess): Use setCloseOnExec(). Source/WTF: Add helper functions to duplicate a file descriptor setting close on exec flag, and also to set close on exec flag to an existing file descriptor. * wtf/UniStdExtras.h: * wtf/UniStdExtras.cpp (WTF::setCloseOnExec): (WTF::dupCloseOnExec): Canonical link: https://commits.webkit.org/177855@main git-svn-id: https://svn.webkit.org/repository/webkit/trunk@203155 268f45cc-cd09-0410-ab3c-d52691b4dbfc
2016-07-13 08:36:01 +00:00
using WTF::dupCloseOnExec;
using WTF::setCloseOnExec;
using WTF::setNonBlock;
[WK2] Looping for EINTR on close() is incorrect for Linux, at least https://bugs.webkit.org/show_bug.cgi?id=117266 Patch by Sergio Correia <sergio.correia@openbossa.org> on 2013-06-20 Reviewed by Darin Adler. Source/WebKit2: Call closeWithRetry() to work around a difference in how the retry needs to be done on Linux. * Platform/CoreIPC/unix/AttachmentUnix.cpp: (CoreIPC::Attachment::dispose): * Platform/CoreIPC/unix/ConnectionUnix.cpp: (CoreIPC::Connection::platformInvalidate): * Platform/unix/SharedMemoryUnix.cpp: (WebKit::SharedMemory::Handle::~Handle): (WebKit::SharedMemory::create): (WebKit::SharedMemory::~SharedMemory): (WebKit::SharedMemory::createHandle): * PluginProcess/PluginProcess.cpp: (WebKit::PluginProcess::createWebProcessConnection): * SharedWorkerProcess/SharedWorkerProcess.cpp: (WebKit::SharedWorkerProcess::createWebProcessConnection): * UIProcess/Launcher/qt/ProcessLauncherQt.cpp: (WebKit::ProcessLauncher::launchProcess): All these places had ``close-followed-by-EINTR-handling'' replaced with the new closeWithRetry() function added in this commit. Source/WTF: Added file UniStdExtras with a closeWithRetry() function that works around the EINTR behavior on Linux during a close() call: it closes the descriptor unconditionally even when the call is interrupted. * wtf/UniStdExtras.h: Added. (WTF::closeWithRetry): Wrapper around POSIX close() that handles EINTR correctly. Canonical link: https://commits.webkit.org/136001@main git-svn-id: https://svn.webkit.org/repository/webkit/trunk@151825 268f45cc-cd09-0410-ab3c-d52691b4dbfc
2013-06-21 06:02:58 +00:00
#endif // UniStdExtras_h