imcplugin: Add launching sel_ldr
[nativeclient.git] / nonnacl_util / sel_ldr_launcher.cc
blob87486959be9b50573bd05927849457fde1752a39
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 "native_client/nonnacl_util/sel_ldr_launcher.h"
35 #include <stdio.h>
36 #include <sys/types.h>
38 #include "native_client/include/nacl_elf.h"
40 namespace nacl {
42 void SelLdrLauncher::BuildArgv(const char* sel_ldr_pathname,
43 const char* application_name,
44 int imc_fd,
45 Handle imc_channel_handle,
46 int sel_ldr_argc,
47 const char* sel_ldr_argv[],
48 int application_argc,
49 const char* application_argv[]) {
50 #if NACL_WINDOWS
51 _snprintf_s(channel_buf_, sizeof(channel_buf_), sizeof(channel_buf_),
52 "%d:%u", imc_fd, imc_channel_handle);
53 _snprintf_s(channel_number_, sizeof(channel_number_), sizeof(channel_number_),
54 "%d", imc_fd);
55 #else
56 snprintf(channel_buf_,
57 sizeof(channel_buf_),
58 "%d:%d",
59 imc_fd,
60 imc_channel_handle);
61 snprintf(channel_number_, sizeof(channel_number_), "%d", imc_fd);
62 #endif
63 // Fixed args are:
64 // .../sel_ldr -f <application_name> -i <NaCl fd>:<imcchannel#>
65 const char* kFixedArgs[] = {
66 const_cast<char*>(sel_ldr_pathname),
67 "-f",
68 const_cast<char*>(application_name),
69 "-i",
70 channel_buf_
72 const int kFixedArgc = sizeof(kFixedArgs) / sizeof(kFixedArgs[0]);
74 argv_ = new char const*[kFixedArgc + sel_ldr_argc
75 + (application_argc + 1) + 1];
76 // We use pre-increment everywhere, so we need to initialize to -1.
77 argc_ = -1;
79 // Copy the fixed arguments to argv_.
80 for (int i = 0; i < kFixedArgc; ++i) {
81 argv_[++argc_] = kFixedArgs[i];
83 // Copy the specified additional arguments (e.g., "-d" or "-P") to sel_ldr
84 for (int i = 0; i < sel_ldr_argc; ++i) {
85 argv_[++argc_] = sel_ldr_argv[i];
87 // Copy the specified arguments to the application
88 if (application_argc > 0) {
89 argv_[++argc_] = "--";
90 for (int i = 0; i < application_argc; ++i) {
91 if (strncmp("$CHAN", application_argv[i], 6) == 0) {
92 argv_[++argc_] = channel_number_;
93 } else {
94 argv_[++argc_] = application_argv[i];
98 // NULL terminate the argument list.
99 argv_[++argc_] = NULL;
102 } // namespace nacl