Debugging: Add code to print backtrace for guest on SIGSEGV
[nativeclient.git] / nonnacl_util / linux / sel_ldr_launcher_linux.cc
bloba7de3b5d32c030007aa94cce861e13340e9513b4
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() {
50 // When used with the browser's invalidate functionality this
51 // causes occasional deadlocks in nacl modules that have threads that have
52 // not terminated when the main thread is being terminated.
53 // TODO: Implement a whole-module shutdown RPC.
54 #if 0
55 if (child_ != kInvalidHandle) {
56 int status;
57 waitpid(child_, &status, 0);
59 #endif
62 bool SelLdrLauncher::Start(const char* application_name,
63 int imc_fd,
64 int sel_ldr_argc,
65 const char* sel_ldr_argv[],
66 int application_argc,
67 const char* application_argv[]) {
68 Handle pair[2];
69 const char* const kNaclSelLdrBasename = "sel_ldr";
70 const char* plugin_dirname = GetPluginDirname();
71 char sel_ldr_path[MAXPATHLEN + 1];
73 application_name_ = application_name;
75 if (NULL == plugin_dirname) {
76 printf("GetPluginDirname returned NULL -- load failed\n");
77 return false;
79 // Uncomment to turn on the sandbox, or set this in your environment
80 // TODO: Turn this on by default.
82 // setenv("NACL_ENABLE_OUTER_SANDBOX");
83 snprintf(sel_ldr_path,
84 sizeof(sel_ldr_path),
85 "%s/%s",
86 plugin_dirname,
87 kNaclSelLdrBasename);
88 printf("starting: %s %s\n", sel_ldr_path, application_name);
90 if (SocketPair(pair) == -1) {
91 return false;
94 // Set environment variable to keep the Mac sel_ldr from stealing the focus.
95 // TODO: change this to use a command line parameter rather than env.
96 setenv("NACL_LAUNCHED_FROM_BROWSER", "1", 0);
97 // Fork the sel_ldr process.
98 child_ = fork();
99 if (child_ == -1) {
100 printf("fork failed\n");
101 Close(pair[0]);
102 Close(pair[1]);
103 return false;
106 if (child_ == 0) {
107 Close(pair[0]);
108 BuildArgv(sel_ldr_path,
109 application_name,
110 imc_fd,
111 pair[1],
112 sel_ldr_argc,
113 sel_ldr_argv,
114 application_argc,
115 application_argv);
116 execv(sel_ldr_path, const_cast<char **>(argv_));
117 perror("exec");
118 printf("exec failed\n");
119 _exit(EXIT_FAILURE);
121 Close(pair[1]);
122 channel_ = pair[0];
124 int flags = fcntl(channel_, F_GETFD);
125 if (flags != -1) {
126 flags |= FD_CLOEXEC;
127 fcntl(channel_, F_SETFD, flags);
129 return true;
132 } // namespace nacl