]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - source/Host/android/ProcessLauncherAndroid.cpp
Vendor import of lldb trunk r256945:
[FreeBSD/FreeBSD.git] / source / Host / android / ProcessLauncherAndroid.cpp
1 //===-- ProcessLauncherAndroid.cpp ------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "lldb/Host/FileSpec.h"
11 #include "lldb/Host/Host.h"
12 #include "lldb/Host/HostProcess.h"
13 #include "lldb/Host/android/ProcessLauncherAndroid.h"
14
15 #include "lldb/Target/ProcessLaunchInfo.h"
16
17 #include <limits.h>
18
19 using namespace lldb;
20 using namespace lldb_private;
21
22 static bool
23 DupDescriptor(const FileSpec &file_spec, int fd, int flags)
24 {
25     int target_fd = ::open(file_spec.GetCString(), flags, 0666);
26
27     if (target_fd == -1)
28         return false;
29
30     if (::dup2(target_fd, fd) == -1)
31         return false;
32
33     return (::close(target_fd) == -1) ? false : true;
34 }
35
36 // If there is no PATH variable specified inside the environment then set the path to /system/bin.
37 // It is required because the default path used by execve() is wrong on android.
38 static void
39 FixupEnvironment(Args& env)
40 {
41     static const char* path = "PATH=";
42     static const int path_len = ::strlen(path);
43     for (const char** args = env.GetConstArgumentVector(); *args; ++args)
44         if (::strncmp(path, *args, path_len) == 0)
45             return;
46     env.AppendArgument("PATH=/system/bin");
47 }
48
49 HostProcess
50 ProcessLauncherAndroid::LaunchProcess(const ProcessLaunchInfo &launch_info, Error &error)
51 {
52     // TODO: Handle other launch parameters specified in launc_info
53
54     char exe_path[PATH_MAX];
55     launch_info.GetExecutableFile().GetPath(exe_path, sizeof(exe_path));
56
57     lldb::pid_t pid = ::fork();
58     if (pid == static_cast<lldb::pid_t>(-1))
59     {
60         // Fork failed
61         error.SetErrorStringWithFormat("Fork failed with error message: %s", strerror(errno));
62         return HostProcess(LLDB_INVALID_PROCESS_ID);
63     }
64     else if (pid == 0)
65     {
66         if (const lldb_private::FileAction *file_action = launch_info.GetFileActionForFD(STDIN_FILENO)) {
67             FileSpec file_spec = file_action->GetFileSpec();
68             if (file_spec)
69                 if (!DupDescriptor(file_spec, STDIN_FILENO, O_RDONLY))
70                     exit(-1);
71         }
72
73         if (const lldb_private::FileAction *file_action = launch_info.GetFileActionForFD(STDOUT_FILENO)) {
74             FileSpec file_spec = file_action->GetFileSpec();
75             if (file_spec)
76                 if (!DupDescriptor(file_spec, STDOUT_FILENO, O_WRONLY | O_CREAT | O_TRUNC))
77                     exit(-1);
78         }
79
80         if (const lldb_private::FileAction *file_action = launch_info.GetFileActionForFD(STDERR_FILENO)) {
81             FileSpec file_spec = file_action->GetFileSpec();
82             if (file_spec)
83                 if (!DupDescriptor(file_spec, STDERR_FILENO, O_WRONLY | O_CREAT | O_TRUNC))
84                     exit(-1);
85         }
86
87         // Child process
88         const char **argv = launch_info.GetArguments().GetConstArgumentVector();
89
90         Args env = launch_info.GetEnvironmentEntries();
91         FixupEnvironment(env);
92         const char **envp = env.GetConstArgumentVector();
93
94         FileSpec working_dir = launch_info.GetWorkingDirectory();
95         if (working_dir)
96         {
97             if (::chdir(working_dir.GetCString()) != 0)
98                 exit(-1);
99         }
100
101         execve(argv[0],
102                const_cast<char *const *>(argv),
103                const_cast<char *const *>(envp));
104         exit(-1);
105     }
106    
107     return HostProcess(pid);
108 }