1/*
2 * Copyright (C) 2011 Igalia S.L.
3 * Copyright (C) 2011 Apple Inc.
4 * Copyright (C) 2012 Samsung Electronics
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
17 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
19 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
25 * THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include "config.h"
29#include "PluginProcessProxy.h"
30
31#if ENABLE(PLUGIN_PROCESS)
32
33#include "PluginProcessCreationParameters.h"
34#include "ProcessExecutablePath.h"
35#include <WebCore/FileSystem.h>
36#include <wtf/text/CString.h>
37#include <wtf/text/WTFString.h>
38#if PLATFORM(GTK) || PLATFORM(EFL)
39#include <glib.h>
40#include <wtf/gobject/GUniquePtr.h>
41#endif
42
43#include <sys/wait.h>
44
45using namespace WebCore;
46
47namespace WebKit {
48
49void PluginProcessProxy::platformGetLaunchOptions(ProcessLauncher::LaunchOptions& launchOptions, const PluginProcessAttributes& pluginProcessAttributes)
50{
51#if PLATFORM(EFL) && !defined(NDEBUG)
52    const char* commandPrefix = getenv("PLUGIN_PROCESS_COMMAND_PREFIX");
53    if (commandPrefix && *commandPrefix)
54        launchOptions.processCmdPrefix = String::fromUTF8(commandPrefix);
55#endif
56
57    launchOptions.extraInitializationData.add("plugin-path", pluginProcessAttributes.moduleInfo.path);
58#if PLATFORM(GTK)
59    if (pluginProcessAttributes.moduleInfo.requiresGtk2)
60        launchOptions.extraInitializationData.add("requires-gtk2", emptyString());
61#endif
62}
63
64void PluginProcessProxy::platformInitializePluginProcess(PluginProcessCreationParameters&)
65{
66}
67
68#if PLUGIN_ARCHITECTURE(X11)
69bool PluginProcessProxy::scanPlugin(const String& pluginPath, RawPluginMetaData& result)
70{
71#if PLATFORM(GTK) || PLATFORM(EFL)
72    CString binaryPath = fileSystemRepresentation(executablePathOfPluginProcess());
73    CString pluginPathCString = fileSystemRepresentation(pluginPath);
74    char* argv[4];
75    argv[0] = const_cast<char*>(binaryPath.data());
76    argv[1] = const_cast<char*>("-scanPlugin");
77    argv[2] = const_cast<char*>(pluginPathCString.data());
78    argv[3] = 0;
79
80    int status;
81    GUniqueOutPtr<char> stdOut;
82
83    // If the disposition of SIGCLD signal is set to SIG_IGN (default)
84    // then the signal will be ignored and g_spawn_sync() will not be
85    // able to return the status.
86    // As a consequence, we make sure that the disposition is set to
87    // SIG_DFL before calling g_spawn_sync().
88#if defined(SIGCLD)
89    struct sigaction action;
90    sigaction(SIGCLD, 0, &action);
91    if (action.sa_handler == SIG_IGN) {
92        action.sa_handler = SIG_DFL;
93        sigaction(SIGCLD, &action, 0);
94    }
95#endif
96
97    if (!g_spawn_sync(0, argv, 0, G_SPAWN_STDERR_TO_DEV_NULL, 0, 0, &stdOut.outPtr(), 0, &status, 0))
98        return false;
99
100    if (!WIFEXITED(status) || WEXITSTATUS(status) != EXIT_SUCCESS || !stdOut)
101        return false;
102
103    String stdOutString = String::fromUTF8(stdOut.get());
104
105    Vector<String> lines;
106    stdOutString.split(UChar('\n'), true, lines);
107
108    if (lines.size() < 3)
109        return false;
110
111    result.name.swap(lines[0]);
112    result.description.swap(lines[1]);
113    result.mimeDescription.swap(lines[2]);
114#if PLATFORM(GTK)
115    if (lines.size() > 3)
116        result.requiresGtk2 = lines[3] == "requires-gtk2";
117#endif
118    return !result.mimeDescription.isEmpty();
119#else // PLATFORM(GTK) || PLATFORM(EFL)
120    return false;
121#endif // PLATFORM(GTK) || PLATFORM(EFL)
122}
123#endif // PLUGIN_ARCHITECTURE(X11)
124
125} // namespace WebKit
126
127#endif // ENABLE(PLUGIN_PROCESS)
128