Skip to content
Permalink
Browse files
Pass the plug-in process shim in DYLD_INSERT_LIBRARIES when launching…
… the plug-in process

https://bugs.webkit.org/show_bug.cgi?id=50262

Reviewed by Sam Weinig.

* UIProcess/Launcher/mac/ProcessLauncherMac.mm:
Add an EnvironmentVariables class that allows for easy modification of the environment variables
passed to posix_spawn.

(WebKit::ProcessLauncher::launchProcess):
When spawning the plug-in process, insert the plug-in process shim.

Canonical link: https://commits.webkit.org/63412@main
git-svn-id: https://svn.webkit.org/repository/webkit/trunk@72957 268f45cc-cd09-0410-ab3c-d52691b4dbfc
  • Loading branch information
Anders Carlsson committed Nov 30, 2010
1 parent d9d22b5 commit 188383702c326fd814828b4eeb1c771f88b275c5
Showing with 146 additions and 1 deletion.
  1. +14 −0 WebKit2/ChangeLog
  2. +132 −1 WebKit2/UIProcess/Launcher/mac/ProcessLauncherMac.mm
@@ -1,3 +1,17 @@
2010-11-30 Anders Carlsson <andersca@apple.com>

Reviewed by Sam Weinig.

Pass the plug-in process shim in DYLD_INSERT_LIBRARIES when launching the plug-in process
https://bugs.webkit.org/show_bug.cgi?id=50262

* UIProcess/Launcher/mac/ProcessLauncherMac.mm:
Add an EnvironmentVariables class that allows for easy modification of the environment variables
passed to posix_spawn.

(WebKit::ProcessLauncher::launchProcess):
When spawning the plug-in process, insert the plug-in process shim.

2010-11-30 Anders Carlsson <andersca@apple.com>

Reviewed by Sam Weinig.
@@ -35,6 +35,7 @@
#include <servers/bootstrap.h>
#include <spawn.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <wtf/PassRefPtr.h>
#include <wtf/Threading.h>
#include <wtf/text/CString.h>
@@ -89,6 +90,122 @@ static void setUpTerminationNotificationHandler(pid_t pid)
#endif
}

class EnvironmentVariables {
WTF_MAKE_NONCOPYABLE(EnvironmentVariables);

public:
EnvironmentVariables()
: m_environmentPointer(*_NSGetEnviron())
{
}

~EnvironmentVariables()
{
deleteAllValues(m_allocatedStrings);
}

void set(const char* name, const char* value)
{
// Check if we need to copy the environment.
if (m_environmentPointer == *_NSGetEnviron())
copyEnvironmentVariables();

// Allocate a string for the name and value.
char* nameAndValue = createStringForVariable(name, value);

for (size_t i = 0; i < m_environmentVariables.size() - 1; ++i) {
char* environmentVariable = m_environmentVariables[i];

if (valueIfVariableHasName(environmentVariable, name)) {
// Just replace the environment variable.
m_environmentVariables[i] = nameAndValue;
return;
}
}

// Append the new string.
ASSERT(!m_environmentVariables.last());
m_environmentVariables.last() = nameAndValue;
m_environmentVariables.append(static_cast<char*>(0));

m_environmentPointer = m_environmentVariables.data();
}

char* get(const char* name) const
{
for (size_t i = 0; m_environmentPointer[i]; ++i) {
if (char* value = valueIfVariableHasName(m_environmentPointer[i], name))
return value;
}
return 0;
}

// Will append the value with the given separator if the environment variable already exists.
void appendValue(const char* name, const char* value, char separator)
{
char* existingValue = get(name);
if (!existingValue) {
set(name, value);
return;
}

Vector<char, 128> newValue;
newValue.append(existingValue, strlen(existingValue));
newValue.append(separator);
newValue.append(value, strlen(value) + 1);

set(name, newValue.data());
}

char** environmentPointer() const { return m_environmentPointer; }

private:
char *valueIfVariableHasName(const char* environmentVariable, const char* name) const
{
// Find the environment variable name.
char* equalsLocation = strchr(environmentVariable, '=');
ASSERT(equalsLocation);

size_t nameLength = equalsLocation - environmentVariable;
if (strncmp(environmentVariable, name, nameLength))
return 0;

return equalsLocation + 1;
}

char* createStringForVariable(const char* name, const char* value)
{
int nameLength = strlen(name);
int valueLength = strlen(value);

// Allocate enough room to hold 'name=value' and the null character.
char* string = static_cast<char*>(fastMalloc(nameLength + 1 + valueLength + 1));
memcpy(string, name, nameLength);
string[nameLength] = '=';
memcpy(string + nameLength + 1, value, valueLength);
string[nameLength + 1 + valueLength] = '\0';

m_allocatedStrings.append(string);

return string;
}

void copyEnvironmentVariables()
{
for (size_t i = 0; (*_NSGetEnviron())[i]; i++)
m_environmentVariables.append((*_NSGetEnviron())[i]);

// Null-terminate the array.
m_environmentVariables.append(static_cast<char*>(0));
}

char** m_environmentPointer;
Vector<char*> m_environmentVariables;

// These allocated strings will be freed in the destructor.
Vector<char*> m_allocatedStrings;
};

void ProcessLauncher::launchProcess()
{
// Create the listening port.
@@ -141,7 +258,21 @@ static void setUpTerminationNotificationHandler(pid_t pid)
posix_spawnattr_setflags(&attr, flags);

pid_t processIdentifier;
int result = posix_spawn(&processIdentifier, path, 0, &attr, (char *const*)args, *_NSGetEnviron());

EnvironmentVariables environmentVariables;

if (m_launchOptions.processType == ProcessLauncher::PluginProcess) {
// We need to insert the plug-in process shim.
NSString *pluginProcessShimPathNSString = [[webProcessAppExecutablePath stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"PluginProcessShim.dylib"];
const char *pluginProcessShimPath = [pluginProcessShimPathNSString fileSystemRepresentation];

// Make sure that the file exists.
struct stat statBuf;
if (stat(pluginProcessShimPath, &statBuf) == 0 && (statBuf.st_mode & S_IFMT) == S_IFREG)
environmentVariables.appendValue("DYLD_INSERT_LIBRARIES", pluginProcessShimPath, ':');
}

int result = posix_spawn(&processIdentifier, path, 0, &attr, (char *const*)args, environmentVariables.environmentPointer());

posix_spawnattr_destroy(&attr);

0 comments on commit 1883837

Please sign in to comment.