imcplugin: Add launching sel_ldr
[nativeclient.git] / nonnacl_util / linux / sel_ldr_launcher_linux.cc
blobe9b7f730157edd056170ef77700f67fb9ba38921
1 /*
2 * Copyright 2008, Google Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
14 * distribution.
15 * * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include <sys/types.h>
34 #include <sys/wait.h>
36 #include <fcntl.h>
37 #include <libgen.h>
38 #include <signal.h>
39 #include <stdio.h>
40 #include <string.h>
41 #include <sys/param.h>
42 #include <unistd.h>
44 #include "native_client/nonnacl_util/sel_ldr_launcher.h"
46 namespace nacl {
48 SelLdrLauncher::~SelLdrLauncher() {
49 if (child_ != kInvalidHandle) {
50 int status;
51 // When used with the browser's invalidate functionality this
52 // causes occasional deadlocks in nacl modules that have threads that have
53 // not terminated when the main thread is being terminated.
54 // TODO: Implement a whole-module shutdown RPC.
55 // waitpid(child_, &status, 0);
59 SelLdrLauncher::SelLdrLauncher(const char* application_name,
60 int imc_fd,
61 int sel_ldr_argc,
62 const char* sel_ldr_argv[],
63 int application_argc,
64 const char* application_argv[])
65 : is_sel_ldr_(true),
66 application_name_(application_name) {
67 Handle pair[2];
68 const char* const kSelLdrBasename = "sel_ldr";
69 const char* const kNaclPtraceBasename = "sel_ldr.trace";
70 const char* plugin_dirname = GetPluginDirname();
71 char sel_ldr_path[MAXPATHLEN + 1];
73 child_ = kInvalidHandle;
74 channel_ = kInvalidHandle;
76 if (NULL == plugin_dirname) {
77 // TODO: better reporting that we couldn't find sel_ldr.
78 printf("GetPluginDirname returned NULL -- load failed\n");
79 return;
82 if (getenv("NACL_TRACE_SEL_LDR")) {
83 snprintf(sel_ldr_path,
84 sizeof(sel_ldr_path),
85 "%s/%s",
86 plugin_dirname,
87 kNaclPtraceBasename);
88 } else {
89 snprintf(sel_ldr_path,
90 sizeof(sel_ldr_path),
91 "%s/%s",
92 plugin_dirname,
93 kSelLdrBasename);
95 printf("starting: %s %s\n", sel_ldr_path, application_name);
97 if (SocketPair(pair) == -1) {
98 // TODO: better reporting that we couldn't create the socket pair.
99 return;
102 // Set environment variable to keep the Mac sel_ldr from stealing the focus.
103 // TODO: change this to use a command line parameter rather than env.
104 setenv("NACL_LAUNCHED_FROM_BROWSER", "1", 0);
105 // Fork the sel_ldr process.
106 child_ = fork();
107 if (child_ == -1) {
108 // TODO: better reporting that we couldn't fork.
109 printf("fork failed\n");
110 Close(pair[0]);
111 Close(pair[1]);
112 return;
115 if (child_ == 0) {
116 Close(pair[0]);
117 BuildArgv(sel_ldr_path,
118 application_name,
119 imc_fd,
120 pair[1],
121 sel_ldr_argc,
122 sel_ldr_argv,
123 application_argc,
124 application_argv);
125 execv(sel_ldr_path, const_cast<char **>(argv_));
126 perror("exec");
127 printf("exec failed\n");
128 _exit(EXIT_FAILURE);
130 Close(pair[1]);
131 channel_ = pair[0];
133 int flags = fcntl(channel_, F_GETFD);
134 if (flags != -1) {
135 flags |= FD_CLOEXEC;
136 fcntl(channel_, F_SETFD, flags);
140 } // namespace nacl