Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
platform_fuchsia.cc
Go to the documentation of this file.
1// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
2// for details. All rights reserved. Use of this source code is governed by a
3// BSD-style license that can be found in the LICENSE file.
4
5#include "platform/globals.h"
6#if defined(DART_HOST_OS_FUCHSIA)
7
8#include "bin/platform.h"
9
10#include <fuchsia/kernel/cpp/fidl.h>
11#include <lib/fdio/directory.h>
12#include <lib/zx/resource.h>
13#include <string.h>
14#include <sys/utsname.h>
15#include <unistd.h>
16#include <zircon/process.h>
17#include <zircon/status.h>
18#include <zircon/syscalls.h>
19
20#include "bin/console.h"
21#include "bin/dartutils.h"
22#include "bin/fdutils.h"
23#include "bin/file.h"
24
25namespace dart {
26namespace bin {
27
28const char* Platform::executable_name_ = nullptr;
29int Platform::script_index_ = 1;
30char** Platform::argv_ = nullptr;
31
33 return true;
34}
35
37 return sysconf(_SC_NPROCESSORS_CONF);
38}
39
41 struct utsname info;
42 int ret = uname(&info);
43 if (ret != 0) {
44 return nullptr;
45 }
46 const char* kFormat = "%s %s %s";
47 int len =
48 snprintf(nullptr, 0, kFormat, info.sysname, info.release, info.version);
49 if (len <= 0) {
50 return nullptr;
51 }
52 char* result = DartUtils::ScopedCString(len + 1);
53 ASSERT(result != nullptr);
54 len = snprintf(result, len + 1, kFormat, info.sysname, info.release,
55 info.version);
56 if (len <= 0) {
57 return nullptr;
58 }
59 return result;
60}
61
62const char* Platform::LibraryPrefix() {
63 return "lib";
64}
65
66const char* Platform::LibraryExtension() {
67 return "so";
68}
69
70const char* Platform::LocaleName() {
71 char* lang = getenv("LANG");
72 if (lang == nullptr) {
73 return "en_US";
74 }
75 return lang;
76}
77
78bool Platform::LocalHostname(char* buffer, intptr_t buffer_length) {
79 return gethostname(buffer, buffer_length) == 0;
80}
81
82char** Platform::Environment(intptr_t* count) {
83 // Using environ directly is only safe as long as we do not
84 // provide access to modifying environment variables.
85 intptr_t i = 0;
86 char** tmp = environ;
87 while (*(tmp++) != nullptr) {
88 i++;
89 }
90 *count = i;
91 char** result;
92 result = reinterpret_cast<char**>(Dart_ScopeAllocate(i * sizeof(*result)));
93 for (intptr_t current = 0; current < i; current++) {
94 result[current] = environ[current];
95 }
96 return result;
97}
98
99const char* Platform::GetExecutableName() {
100 if (executable_name_ != nullptr) {
101 return executable_name_;
102 }
103 char* name = DartUtils::ScopedCString(ZX_MAX_NAME_LEN);
104 zx_status_t status = zx_object_get_property(zx_process_self(), ZX_PROP_NAME,
105 name, ZX_MAX_NAME_LEN);
106 if (status != ZX_OK) {
107 return nullptr;
108 }
109 return name;
110}
111
113 const char* executable_name = Platform::GetExecutableName();
114 if (executable_name == nullptr) {
115 return nullptr;
116 }
117 if ((executable_name[0] == '/') && File::Exists(nullptr, executable_name)) {
118 return File::GetCanonicalPath(nullptr, executable_name);
119 }
120 if (strchr(executable_name, '/') != nullptr) {
121 const char* result = File::GetCanonicalPath(nullptr, executable_name);
122 if (File::Exists(nullptr, result)) {
123 return result;
124 }
125 } else {
126 const char* path = getenv("PATH");
127 if (path == nullptr) {
128 // If PATH isn't set, make some guesses about where we should look.
129 path = "/system/bin:/system/apps:/boot/bin";
130 }
131 char* pathcopy = DartUtils::ScopedCopyCString(path);
133 char* save = nullptr;
134 while ((pathcopy = strtok_r(pathcopy, ":", &save)) != nullptr) {
135 snprintf(result, PATH_MAX, "%s/%s", pathcopy, executable_name);
136 result[PATH_MAX] = '\0';
137 if (File::Exists(nullptr, result)) {
138 return File::GetCanonicalPath(nullptr, result);
139 }
140 pathcopy = nullptr;
141 }
142 }
143 // Couldn't find it. This causes null to be returned for
144 // Platform.resolvedExecutable.
145 return nullptr;
146}
147
148intptr_t Platform::ResolveExecutablePathInto(char* result, size_t result_size) {
149 return -1;
150}
151
152void Platform::SetProcessName(const char* name) {
153 zx_object_set_property(zx_process_self(), ZX_PROP_NAME, name,
154 Utils::Minimum(strlen(name), ZX_MAX_NAME_LEN));
155}
156
157void Platform::Exit(int exit_code) {
160 exit(exit_code);
161}
162
163void Platform::_Exit(int exit_code) {
166 _exit(exit_code);
167}
168
170 // Not supported.
171}
172
173zx_handle_t Platform::GetVMEXResource() {
174 zx::resource vmex_resource;
175 fuchsia::kernel::VmexResourceSyncPtr vmex_resource_svc;
176 zx_status_t status = fdio_service_connect(
177 "/svc/fuchsia.kernel.VmexResource",
178 vmex_resource_svc.NewRequest().TakeChannel().release());
179 ASSERT(status == ZX_OK);
180 status = vmex_resource_svc->Get(&vmex_resource);
181 ASSERT(status == ZX_OK);
182 return vmex_resource.release();
183}
184
185} // namespace bin
186} // namespace dart
187
188#endif // defined(DART_HOST_OS_FUCHSIA)
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition DM.cpp:213
int count
static T Minimum(T x, T y)
Definition utils.h:21
static void RestoreConfig()
static char * ScopedCopyCString(const char *str)
Definition dartutils.h:232
static char * ScopedCString(intptr_t length)
Definition dartutils.h:224
static const char * GetCanonicalPath(Namespace *namespc, const char *path, char *dest=nullptr, int dest_size=0)
static bool Exists(Namespace *namespc, const char *path)
static bool LocalHostname(char *buffer, intptr_t buffer_length)
static const char * GetExecutableName()
static const char * LibraryPrefix()
static DART_NORETURN void _Exit(int exit_code)
static void SetProcessName(const char *name)
static const char * LocaleName()
static DART_NORETURN void Exit(int exit_code)
static bool Initialize()
static char ** Environment(intptr_t *count)
static const char * OperatingSystemVersion()
static intptr_t ResolveExecutablePathInto(char *result, size_t result_size)
static int NumberOfProcessors()
static const char * LibraryExtension()
static const char * ResolveExecutablePath()
static void SetCoreDumpResourceLimit(int value)
#define ASSERT(E)
static const uint8_t buffer[]
GAsyncResult * result
exit(kErrorExitCode)
DART_EXPORT void Dart_PrepareToAbort()
const char *const name
DART_EXPORT uint8_t * Dart_ScopeAllocate(intptr_t size)
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir path
Definition switches.h:57
#define PATH_MAX
Definition globals.h:708