Add ncval-stubout tool
[nativeclient.git] / nonnacl_util / sel_ldr_launcher.cc
blob1a14adddfd4d9b97f2dcdfe0a4b5c1bc02ed080f
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(channel_buf_, sizeof(channel_buf_), "%d:%u",
52 imc_fd, imc_channel_handle);
53 _snprintf(channel_number_, sizeof(channel_number_), "%d", imc_fd);
54 #else
55 snprintf(channel_buf_,
56 sizeof(channel_buf_),
57 "%d:%d",
58 imc_fd,
59 imc_channel_handle);
60 snprintf(channel_number_, sizeof(channel_number_), "%d", imc_fd);
61 #endif
62 // Fixed args are:
63 // .../sel_ldr -f <application_name> -i <NaCl fd>:<imcchannel#>
64 const char* kFixedArgs[] = {
65 const_cast<char*>(sel_ldr_pathname),
66 "-f",
67 const_cast<char*>(application_name),
68 "-i",
69 channel_buf_
71 const int kFixedArgc = sizeof(kFixedArgs) / sizeof(kFixedArgs[0]);
73 argv_ = new char const*[kFixedArgc + sel_ldr_argc
74 + (application_argc + 1) + 1];
75 // We use pre-increment everywhere, so we need to initialize to -1.
76 argc_ = -1;
78 // Copy the fixed arguments to argv_.
79 for (int i = 0; i < kFixedArgc; ++i) {
80 argv_[++argc_] = kFixedArgs[i];
82 // Copy the specified additional arguments (e.g., "-d" or "-P") to sel_ldr
83 for (int i = 0; i < sel_ldr_argc; ++i) {
84 argv_[++argc_] = sel_ldr_argv[i];
86 // Copy the specified arguments to the application
87 if (application_argc > 0) {
88 argv_[++argc_] = "--";
89 for (int i = 0; i < application_argc; ++i) {
90 if (strncmp("$CHAN", application_argv[i], 6) == 0) {
91 argv_[++argc_] = channel_number_;
92 } else {
93 argv_[++argc_] = application_argv[i];
97 // NULL terminate the argument list.
98 argv_[++argc_] = NULL;
101 } // namespace nacl