Add _android files for building DartVM on Android

Split directory_posix -> directory_(android|linux|macos)
  This allows us to cleanly make Android-specific changes to
  directory_android.cc

Copy all _linux files to _android files and edit as needed to account
for the differences between Linux and Android:

 + getcwd(0, NULL) doesn't work on Android, have to emulate
 + Android doesn't have a '/tmp' directory, have to emulate
 + Android doesn't provide mkdtemp(), have to emulate.
 + Small differences in the available system include files.
 + Use pthread_cond_timedwait_monotonic instead of
   pthread_condattr_setclock

Review URL: https://chromiumcodereview.appspot.com//10826233

git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@10613 260f80e4-7a28-3924-810f-c04153c831b5
This commit is contained in:
jackpal@google.com 2012-08-13 23:06:52 +00:00
parent f29e6bb659
commit e060b66a3e
32 changed files with 3745 additions and 10 deletions

View file

@ -10,6 +10,7 @@
'dartutils.h',
'dbg_connection.cc',
'dbg_connection.h',
'dbg_connection_android.cc',
'dbg_connection_linux.cc',
'dbg_connection_linux.h',
'dbg_connection_macos.cc',
@ -18,10 +19,13 @@
'dbg_connection_win.h',
'directory.cc',
'directory.h',
'directory_posix.cc',
'directory_android.cc',
'directory_linux.cc',
'directory_macos.cc',
'directory_win.cc',
'eventhandler.cc',
'eventhandler.h',
'eventhandler_android.cc',
'eventhandler_linux.cc',
'eventhandler_linux.h',
'eventhandler_macos.cc',
@ -30,32 +34,38 @@
'eventhandler_win.h',
'extensions.h',
'extensions.cc',
'extensions_android.cc',
'extensions_linux.cc',
'extensions_macos.cc',
'extensions_win.cc',
'file.cc',
'file.h',
'file_android.cc',
'file_linux.cc',
'file_macos.cc',
'file_win.cc',
'file_test.cc',
'fdutils.h',
'fdutils_android.cc',
'fdutils_linux.cc',
'fdutils_macos.cc',
'hashmap_test.cc',
'isolate_data.h',
'platform.cc',
'platform.h',
'platform_android.cc',
'platform_linux.cc',
'platform_macos.cc',
'platform_win.cc',
'process.cc',
'process.h',
'process_android.cc',
'process_linux.cc',
'process_macos.cc',
'process_win.cc',
'socket.cc',
'socket.h',
'socket_android.cc',
'socket_linux.cc',
'socket_macos.cc',
'socket_win.cc',
@ -63,6 +73,7 @@
'set_test.cc',
'thread.h',
'utils.h',
'utils_android.cc',
'utils_linux.cc',
'utils_macos.cc',
'utils_win.cc',

View file

@ -14,7 +14,9 @@
#include "platform/json.h"
#include "platform/thread.h"
// Declare the OS-specific types ahead of defining the generic class.
#if defined(TARGET_OS_LINUX)
#if defined(TARGET_OS_ANDROID)
#include "bin/dbg_connection_android.h"
#elif defined(TARGET_OS_LINUX)
#include "bin/dbg_connection_linux.h"
#elif defined(TARGET_OS_MACOS)
#include "bin/dbg_connection_macos.h"

View file

@ -0,0 +1,104 @@
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/epoll.h>
#include "bin/dbg_connection.h"
#include "bin/fdutils.h"
#include "bin/socket.h"
int DebuggerConnectionImpl::epoll_fd_ = -1;
int DebuggerConnectionImpl::wakeup_fds_[2] = {-1, -1};
void DebuggerConnectionImpl::HandleEvent(struct epoll_event* event) {
if (event->data.fd == DebuggerConnectionHandler::listener_fd_) {
if (DebuggerConnectionHandler::IsConnected()) {
FATAL("Cannot connect to more than one debugger.\n");
}
int fd = ServerSocket::Accept(event->data.fd);
if (fd < 0) {
FATAL("Accepting new debugger connection failed.\n");
}
FDUtils::SetBlocking(fd);
DebuggerConnectionHandler::AcceptDbgConnection(fd);
// TODO(hausner): add the debugger wire socket fd to the event poll queue
// once we poll the debugger connection.
} else if (event->data.fd == DebuggerConnectionHandler::debugger_fd_) {
printf("unexpected: receiving debugger connection event.\n");
UNIMPLEMENTED();
} else {
// Sync message. Not yet implemented.
UNIMPLEMENTED();
}
}
void DebuggerConnectionImpl::Handler(uword args) {
static const intptr_t kMaxEvents = 4;
struct epoll_event events[kMaxEvents];
while (1) {
const int no_timeout = -1;
intptr_t result = TEMP_FAILURE_RETRY(
epoll_wait(epoll_fd_, events, kMaxEvents, no_timeout));
ASSERT(EAGAIN == EWOULDBLOCK);
if (result == -1) {
if (errno != EWOULDBLOCK) {
perror("epoll_wait failed");
}
} else {
ASSERT(result <= kMaxEvents);
for (int i = 0; i < result; i++) {
HandleEvent(&events[i]);
}
}
}
}
void DebuggerConnectionImpl::SetupPollQueue() {
int result = TEMP_FAILURE_RETRY(pipe(wakeup_fds_));
if (result != 0) {
FATAL1("Pipe creation failed with error %d\n", result);
}
FDUtils::SetNonBlocking(wakeup_fds_[0]);
static const int kEpollInitialSize = 16;
epoll_fd_ = TEMP_FAILURE_RETRY(epoll_create(kEpollInitialSize));
if (epoll_fd_ == -1) {
FATAL("Failed creating epoll file descriptor");
}
// Register the wakeup _fd with the epoll instance.
struct epoll_event event;
event.events = EPOLLIN;
event.data.fd = wakeup_fds_[0];
int status = TEMP_FAILURE_RETRY(epoll_ctl(
epoll_fd_, EPOLL_CTL_ADD, wakeup_fds_[0], &event));
if (status == -1) {
FATAL("Failed adding wakeup fd to epoll instance");
}
// Register the listener_fd with the epoll instance.
event.events = EPOLLIN;
event.data.fd = DebuggerConnectionHandler::listener_fd_;
status = TEMP_FAILURE_RETRY(epoll_ctl(epoll_fd_, EPOLL_CTL_ADD,
DebuggerConnectionHandler::listener_fd_, &event));
if (status == -1) {
FATAL("Failed adding listener fd to epoll instance");
}
}
void DebuggerConnectionImpl::StartHandler(int port_number) {
ASSERT(DebuggerConnectionHandler::listener_fd_ != -1);
SetupPollQueue();
int result = dart::Thread::Start(&DebuggerConnectionImpl::Handler, 0);
if (result != 0) {
FATAL1("Failed to start debugger connection handler thread: %d\n", result);
}
}

View file

@ -0,0 +1,29 @@
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
#ifndef BIN_DBG_CONNECTION_ANDROID_H_
#define BIN_DBG_CONNECTION_ANDROID_H_
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/socket.h>
class DebuggerConnectionImpl {
public:
static void StartHandler(int port_number);
private:
static void SetupPollQueue();
static void HandleEvent(struct epoll_event* event);
static void Handler(uword args);
// File descriptors for pipes used to communicate with the debugger thread.
static int wakeup_fds_[2];
// File descriptor for the polling queue.
static int epoll_fd_;
};
#endif // BIN_DBG_CONNECTION_ANDROID_H_

View file

@ -0,0 +1,464 @@
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
#include "bin/directory.h"
#include <dirent.h>
#include <errno.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <unistd.h>
#include "bin/file.h"
#include "bin/platform.h"
static char* SafeStrNCpy(char* dest, const char* src, size_t n) {
strncpy(dest, src, n);
dest[n - 1] = '\0';
return dest;
}
// Forward declarations.
static bool ListRecursively(const char* dir_name,
bool recursive,
DirectoryListing* listing);
static bool DeleteRecursively(const char* dir_name);
static bool ComputeFullPath(const char* dir_name,
char* path,
int* path_length) {
char* abs_path;
do {
abs_path = realpath(dir_name, path);
} while (abs_path == NULL && errno == EINTR);
if (abs_path == NULL) {
return false;
}
*path_length = strlen(path);
size_t written = snprintf(path + *path_length,
PATH_MAX - *path_length,
"%s",
File::PathSeparator());
if (written != strlen(File::PathSeparator())) {
return false;
}
*path_length += written;
return true;
}
static bool HandleDir(char* dir_name,
char* path,
int path_length,
bool recursive,
DirectoryListing *listing) {
if (strcmp(dir_name, ".") != 0 &&
strcmp(dir_name, "..") != 0) {
size_t written = snprintf(path + path_length,
PATH_MAX - path_length,
"%s",
dir_name);
if (written != strlen(dir_name)) {
return false;
}
bool ok = listing->HandleDirectory(path);
if (!ok) return ok;
if (recursive) {
return ListRecursively(path, recursive, listing);
}
}
return true;
}
static bool HandleFile(char* file_name,
char* path,
int path_length,
DirectoryListing *listing) {
// TODO(sgjesse): Pass flags to indicate whether file responses are
// needed.
size_t written = snprintf(path + path_length,
PATH_MAX - path_length,
"%s",
file_name);
if (written != strlen(file_name)) {
return false;
}
return listing->HandleFile(path);
}
static void PostError(DirectoryListing *listing,
const char* dir_name) {
listing->HandleError(dir_name);
}
static bool ListRecursively(const char* dir_name,
bool recursive,
DirectoryListing *listing) {
DIR* dir_pointer;
do {
dir_pointer = opendir(dir_name);
} while (dir_pointer == NULL && errno == EINTR);
if (dir_pointer == NULL) {
PostError(listing, dir_name);
return false;
}
// Compute full path for the directory currently being listed. The
// path buffer will be used to construct the current path in the
// recursive traversal. path_length does not always equal
// strlen(path) but indicates the current prefix of path that is the
// path of the current directory in the traversal.
char *path = static_cast<char*>(malloc(PATH_MAX));
ASSERT(path != NULL);
int path_length = 0;
bool valid = ComputeFullPath(dir_name, path, &path_length);
if (!valid) {
free(path);
PostError(listing, dir_name);
return false;
}
// Iterated the directory and post the directories and files to the
// ports.
int read = 0;
bool success = true;
dirent entry;
dirent* result;
while ((read = TEMP_FAILURE_RETRY(readdir_r(dir_pointer,
&entry,
&result))) == 0 &&
result != NULL) {
switch (entry.d_type) {
case DT_DIR:
success = HandleDir(entry.d_name,
path,
path_length,
recursive,
listing) && success;
break;
case DT_REG:
success = HandleFile(entry.d_name,
path,
path_length,
listing) && success;
break;
case DT_LNK:
case DT_UNKNOWN: {
// On some file systems the entry type is not determined by
// readdir_r. For those and for links we use stat to determine
// the actual entry type. Notice that stat returns the type of
// the file pointed to.
struct stat entry_info;
size_t written = snprintf(path + path_length,
PATH_MAX - path_length,
"%s",
entry.d_name);
if (written != strlen(entry.d_name)) {
success = false;
break;
}
int stat_success = TEMP_FAILURE_RETRY(stat(path, &entry_info));
if (stat_success == -1) {
success = false;
PostError(listing, path);
break;
}
if (S_ISDIR(entry_info.st_mode)) {
success = HandleDir(entry.d_name,
path,
path_length,
recursive,
listing) && success;
} else if (S_ISREG(entry_info.st_mode)) {
success = HandleFile(entry.d_name,
path,
path_length,
listing) && success;
}
ASSERT(!S_ISLNK(entry_info.st_mode));
break;
}
default:
break;
}
}
if (read != 0) {
errno = read;
success = false;
PostError(listing, dir_name);
}
if (closedir(dir_pointer) == -1) {
success = false;
PostError(listing, dir_name);
}
free(path);
return success;
}
static bool DeleteFile(char* file_name,
char* path,
int path_length) {
size_t written = snprintf(path + path_length,
PATH_MAX - path_length,
"%s",
file_name);
if (written != strlen(file_name)) {
return false;
}
return (remove(path) == 0);
}
static bool DeleteDir(char* dir_name,
char* path,
int path_length) {
if (strcmp(dir_name, ".") != 0 &&
strcmp(dir_name, "..") != 0) {
size_t written = snprintf(path + path_length,
PATH_MAX - path_length,
"%s",
dir_name);
if (written != strlen(dir_name)) {
return false;
}
return DeleteRecursively(path);
}
return true;
}
static bool DeleteRecursively(const char* dir_name) {
// Do not recurse into links for deletion. Instead delete the link.
struct stat st;
if (TEMP_FAILURE_RETRY(lstat(dir_name, &st)) == -1) {
return false;
} else if (S_ISLNK(st.st_mode)) {
return (remove(dir_name) == 0);
}
// Not a link. Attempt to open as a directory and recurse into the
// directory.
DIR* dir_pointer;
do {
dir_pointer = opendir(dir_name);
} while (dir_pointer == NULL && errno == EINTR);
if (dir_pointer == NULL) {
return false;
}
// Compute full path for the directory currently being deleted. The
// path buffer will be used to construct the current path in the
// recursive traversal. path_length does not always equal
// strlen(path) but indicates the current prefix of path that is the
// path of the current directory in the traversal.
char *path = static_cast<char*>(malloc(PATH_MAX));
ASSERT(path != NULL);
int path_length = 0;
bool valid = ComputeFullPath(dir_name, path, &path_length);
if (!valid) {
free(path);
return false;
}
// Iterate the directory and delete all files and directories.
int read = 0;
bool success = true;
dirent entry;
dirent* result;
while ((read = TEMP_FAILURE_RETRY(readdir_r(dir_pointer,
&entry,
&result))) == 0 &&
result != NULL &&
success) {
switch (entry.d_type) {
case DT_DIR:
success = success && DeleteDir(entry.d_name, path, path_length);
break;
case DT_REG:
case DT_LNK:
// Treat all links as files. This will delete the link which
// is what we want no matter if the link target is a file or a
// directory.
success = success && DeleteFile(entry.d_name, path, path_length);
break;
case DT_UNKNOWN: {
// On some file systems the entry type is not determined by
// readdir_r. For those we use lstat to determine the entry
// type.
struct stat entry_info;
size_t written = snprintf(path + path_length,
PATH_MAX - path_length,
"%s",
entry.d_name);
if (written != strlen(entry.d_name)) {
success = false;
break;
}
int lstat_success = TEMP_FAILURE_RETRY(lstat(path, &entry_info));
if (lstat_success == -1) {
success = false;
break;
}
if (S_ISDIR(entry_info.st_mode)) {
success = success && DeleteDir(entry.d_name, path, path_length);
} else if (S_ISREG(entry_info.st_mode) || S_ISLNK(entry_info.st_mode)) {
// Treat links as files. This will delete the link which is
// what we want no matter if the link target is a file or a
// directory.
success = success && DeleteFile(entry.d_name, path, path_length);
}
break;
}
default:
break;
}
}
free(path);
if ((read != 0) ||
(closedir(dir_pointer) == -1) ||
(remove(dir_name) == -1)) {
return false;
}
return success;
}
bool Directory::List(const char* dir_name,
bool recursive,
DirectoryListing *listing) {
bool completed = ListRecursively(dir_name, recursive, listing);
return completed;
}
Directory::ExistsResult Directory::Exists(const char* dir_name) {
struct stat entry_info;
int success = TEMP_FAILURE_RETRY(stat(dir_name, &entry_info));
if (success == 0) {
if (S_ISDIR(entry_info.st_mode)) {
return EXISTS;
} else {
return DOES_NOT_EXIST;
}
} else {
if (errno == EACCES ||
errno == EBADF ||
errno == EFAULT ||
errno == ENOMEM ||
errno == EOVERFLOW) {
// Search permissions denied for one of the directories in the
// path or a low level error occured. We do not know if the
// directory exists.
return UNKNOWN;
}
ASSERT(errno == ELOOP ||
errno == ENAMETOOLONG ||
errno == ENOENT ||
errno == ENOTDIR);
return DOES_NOT_EXIST;
}
}
char* Directory::Current() {
// Android's getcwd adheres closely to the POSIX standard. It won't
// allocate memory. We need to make our own copy.
char buffer[PATH_MAX];
if (NULL == getcwd(buffer, PATH_MAX)) {
return NULL;
}
return strdup(buffer);
}
bool Directory::Create(const char* dir_name) {
// Create the directory with the permissions specified by the
// process umask.
return (TEMP_FAILURE_RETRY(mkdir(dir_name, 0777)) == 0);
}
// Android doesn't currently provide mkdtemp. Once Android provied mkdtemp,
// remove this function in favor of calling mkdtemp directly.
static char* MakeTempDirectory(char* path_template) {
if (mktemp(path_template) == NULL) {
return NULL;
}
if (mkdir(path_template, 0700) != 0) {
return NULL;
}
return path_template;
}
char* Directory::CreateTemp(const char* const_template) {
// Returns a new, unused directory name, modifying the contents of
// dir_template. Creates the directory with the permissions specified
// by the process umask.
// The return value must be freed by the caller.
char* path = static_cast<char*>(malloc(PATH_MAX + 1));
SafeStrNCpy(path, const_template, PATH_MAX + 1);
int path_length = strlen(path);
if (path_length > 0) {
if ((path)[path_length - 1] == '/') {
snprintf(path + path_length, PATH_MAX - path_length, "temp_dir_XXXXXX");
} else {
snprintf(path + path_length, PATH_MAX - path_length, "XXXXXX");
}
} else {
// Android does not have a /tmp directory. A partial substitute,
// suitable for bring-up work and tests, is to create a tmp
// directory in /data/local/tmp.
//
// TODO(4413): In the long run, when running in an application we should
// probably use android.content.Context.getCacheDir().
#define ANDROID_TEMP_DIR "/data/local/tmp"
struct stat st;
if (stat(ANDROID_TEMP_DIR, &st) != 0) {
mkdir(ANDROID_TEMP_DIR, 0777);
}
snprintf(path, PATH_MAX, ANDROID_TEMP_DIR "/temp_dir1_XXXXXX");
}
char* result;
do {
result = MakeTempDirectory(path);
} while (result == NULL && errno == EINTR);
if (result == NULL) {
free(path);
return NULL;
}
return path;
}
bool Directory::Delete(const char* dir_name, bool recursive) {
if (!recursive) {
return (TEMP_FAILURE_RETRY(remove(dir_name)) == 0);
} else {
return DeleteRecursively(dir_name);
}
}
bool Directory::Rename(const char* path, const char* new_path) {
ExistsResult exists = Exists(path);
if (exists != EXISTS) return false;
return (TEMP_FAILURE_RETRY(rename(path, new_path)) == 0);
}

View file

@ -0,0 +1,432 @@
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
#include "bin/directory.h"
#include <dirent.h>
#include <errno.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <unistd.h>
#include "bin/file.h"
#include "bin/platform.h"
static char* SafeStrNCpy(char* dest, const char* src, size_t n) {
strncpy(dest, src, n);
dest[n - 1] = '\0';
return dest;
}
// Forward declarations.
static bool ListRecursively(const char* dir_name,
bool recursive,
DirectoryListing* listing);
static bool DeleteRecursively(const char* dir_name);
static bool ComputeFullPath(const char* dir_name,
char* path,
int* path_length) {
char* abs_path;
do {
abs_path = realpath(dir_name, path);
} while (abs_path == NULL && errno == EINTR);
if (abs_path == NULL) {
return false;
}
*path_length = strlen(path);
size_t written = snprintf(path + *path_length,
PATH_MAX - *path_length,
"%s",
File::PathSeparator());
if (written != strlen(File::PathSeparator())) {
return false;
}
*path_length += written;
return true;
}
static bool HandleDir(char* dir_name,
char* path,
int path_length,
bool recursive,
DirectoryListing *listing) {
if (strcmp(dir_name, ".") != 0 &&
strcmp(dir_name, "..") != 0) {
size_t written = snprintf(path + path_length,
PATH_MAX - path_length,
"%s",
dir_name);
if (written != strlen(dir_name)) {
return false;
}
bool ok = listing->HandleDirectory(path);
if (!ok) return ok;
if (recursive) {
return ListRecursively(path, recursive, listing);
}
}
return true;
}
static bool HandleFile(char* file_name,
char* path,
int path_length,
DirectoryListing *listing) {
// TODO(sgjesse): Pass flags to indicate whether file responses are
// needed.
size_t written = snprintf(path + path_length,
PATH_MAX - path_length,
"%s",
file_name);
if (written != strlen(file_name)) {
return false;
}
return listing->HandleFile(path);
}
static void PostError(DirectoryListing *listing,
const char* dir_name) {
listing->HandleError(dir_name);
}
static bool ListRecursively(const char* dir_name,
bool recursive,
DirectoryListing *listing) {
DIR* dir_pointer;
do {
dir_pointer = opendir(dir_name);
} while (dir_pointer == NULL && errno == EINTR);
if (dir_pointer == NULL) {
PostError(listing, dir_name);
return false;
}
// Compute full path for the directory currently being listed. The
// path buffer will be used to construct the current path in the
// recursive traversal. path_length does not always equal
// strlen(path) but indicates the current prefix of path that is the
// path of the current directory in the traversal.
char *path = static_cast<char*>(malloc(PATH_MAX));
ASSERT(path != NULL);
int path_length = 0;
bool valid = ComputeFullPath(dir_name, path, &path_length);
if (!valid) {
free(path);
PostError(listing, dir_name);
return false;
}
// Iterated the directory and post the directories and files to the
// ports.
int read = 0;
bool success = true;
dirent entry;
dirent* result;
while ((read = TEMP_FAILURE_RETRY(readdir_r(dir_pointer,
&entry,
&result))) == 0 &&
result != NULL) {
switch (entry.d_type) {
case DT_DIR:
success = HandleDir(entry.d_name,
path,
path_length,
recursive,
listing) && success;
break;
case DT_REG:
success = HandleFile(entry.d_name,
path,
path_length,
listing) && success;
break;
case DT_LNK:
case DT_UNKNOWN: {
// On some file systems the entry type is not determined by
// readdir_r. For those and for links we use stat to determine
// the actual entry type. Notice that stat returns the type of
// the file pointed to.
struct stat entry_info;
size_t written = snprintf(path + path_length,
PATH_MAX - path_length,
"%s",
entry.d_name);
if (written != strlen(entry.d_name)) {
success = false;
break;
}
int stat_success = TEMP_FAILURE_RETRY(stat(path, &entry_info));
if (stat_success == -1) {
success = false;
PostError(listing, path);
break;
}
if (S_ISDIR(entry_info.st_mode)) {
success = HandleDir(entry.d_name,
path,
path_length,
recursive,
listing) && success;
} else if (S_ISREG(entry_info.st_mode)) {
success = HandleFile(entry.d_name,
path,
path_length,
listing) && success;
}
ASSERT(!S_ISLNK(entry_info.st_mode));
break;
}
default:
break;
}
}
if (read != 0) {
errno = read;
success = false;
PostError(listing, dir_name);
}
if (closedir(dir_pointer) == -1) {
success = false;
PostError(listing, dir_name);
}
free(path);
return success;
}
static bool DeleteFile(char* file_name,
char* path,
int path_length) {
size_t written = snprintf(path + path_length,
PATH_MAX - path_length,
"%s",
file_name);
if (written != strlen(file_name)) {
return false;
}
return (remove(path) == 0);
}
static bool DeleteDir(char* dir_name,
char* path,
int path_length) {
if (strcmp(dir_name, ".") != 0 &&
strcmp(dir_name, "..") != 0) {
size_t written = snprintf(path + path_length,
PATH_MAX - path_length,
"%s",
dir_name);
if (written != strlen(dir_name)) {
return false;
}
return DeleteRecursively(path);
}
return true;
}
static bool DeleteRecursively(const char* dir_name) {
// Do not recurse into links for deletion. Instead delete the link.
struct stat st;
if (TEMP_FAILURE_RETRY(lstat(dir_name, &st)) == -1) {
return false;
} else if (S_ISLNK(st.st_mode)) {
return (remove(dir_name) == 0);
}
// Not a link. Attempt to open as a directory and recurse into the
// directory.
DIR* dir_pointer;
do {
dir_pointer = opendir(dir_name);
} while (dir_pointer == NULL && errno == EINTR);
if (dir_pointer == NULL) {
return false;
}
// Compute full path for the directory currently being deleted. The
// path buffer will be used to construct the current path in the
// recursive traversal. path_length does not always equal
// strlen(path) but indicates the current prefix of path that is the
// path of the current directory in the traversal.
char *path = static_cast<char*>(malloc(PATH_MAX));
ASSERT(path != NULL);
int path_length = 0;
bool valid = ComputeFullPath(dir_name, path, &path_length);
if (!valid) {
free(path);
return false;
}
// Iterate the directory and delete all files and directories.
int read = 0;
bool success = true;
dirent entry;
dirent* result;
while ((read = TEMP_FAILURE_RETRY(readdir_r(dir_pointer,
&entry,
&result))) == 0 &&
result != NULL &&
success) {
switch (entry.d_type) {
case DT_DIR:
success = success && DeleteDir(entry.d_name, path, path_length);
break;
case DT_REG:
case DT_LNK:
// Treat all links as files. This will delete the link which
// is what we want no matter if the link target is a file or a
// directory.
success = success && DeleteFile(entry.d_name, path, path_length);
break;
case DT_UNKNOWN: {
// On some file systems the entry type is not determined by
// readdir_r. For those we use lstat to determine the entry
// type.
struct stat entry_info;
size_t written = snprintf(path + path_length,
PATH_MAX - path_length,
"%s",
entry.d_name);
if (written != strlen(entry.d_name)) {
success = false;
break;
}
int lstat_success = TEMP_FAILURE_RETRY(lstat(path, &entry_info));
if (lstat_success == -1) {
success = false;
break;
}
if (S_ISDIR(entry_info.st_mode)) {
success = success && DeleteDir(entry.d_name, path, path_length);
} else if (S_ISREG(entry_info.st_mode) || S_ISLNK(entry_info.st_mode)) {
// Treat links as files. This will delete the link which is
// what we want no matter if the link target is a file or a
// directory.
success = success && DeleteFile(entry.d_name, path, path_length);
}
break;
}
default:
break;
}
}
free(path);
if ((read != 0) ||
(closedir(dir_pointer) == -1) ||
(remove(dir_name) == -1)) {
return false;
}
return success;
}
bool Directory::List(const char* dir_name,
bool recursive,
DirectoryListing *listing) {
bool completed = ListRecursively(dir_name, recursive, listing);
return completed;
}
Directory::ExistsResult Directory::Exists(const char* dir_name) {
struct stat entry_info;
int success = TEMP_FAILURE_RETRY(stat(dir_name, &entry_info));
if (success == 0) {
if (S_ISDIR(entry_info.st_mode)) {
return EXISTS;
} else {
return DOES_NOT_EXIST;
}
} else {
if (errno == EACCES ||
errno == EBADF ||
errno == EFAULT ||
errno == ENOMEM ||
errno == EOVERFLOW) {
// Search permissions denied for one of the directories in the
// path or a low level error occured. We do not know if the
// directory exists.
return UNKNOWN;
}
ASSERT(errno == ELOOP ||
errno == ENAMETOOLONG ||
errno == ENOENT ||
errno == ENOTDIR);
return DOES_NOT_EXIST;
}
}
char* Directory::Current() {
return getcwd(NULL, 0);
}
bool Directory::Create(const char* dir_name) {
// Create the directory with the permissions specified by the
// process umask.
return (TEMP_FAILURE_RETRY(mkdir(dir_name, 0777)) == 0);
}
char* Directory::CreateTemp(const char* const_template) {
// Returns a new, unused directory name, modifying the contents of
// dir_template. Creates the directory with the permissions specified
// by the process umask.
// The return value must be freed by the caller.
char* path = static_cast<char*>(malloc(PATH_MAX + 1));
SafeStrNCpy(path, const_template, PATH_MAX + 1);
int path_length = strlen(path);
if (path_length > 0) {
if ((path)[path_length - 1] == '/') {
snprintf(path + path_length, PATH_MAX - path_length, "temp_dir_XXXXXX");
} else {
snprintf(path + path_length, PATH_MAX - path_length, "XXXXXX");
}
} else {
snprintf(path, PATH_MAX, "/tmp/temp_dir1_XXXXXX");
}
char* result;
do {
result = mkdtemp(path);
} while (result == NULL && errno == EINTR);
if (result == NULL) {
free(path);
return NULL;
}
return path;
}
bool Directory::Delete(const char* dir_name, bool recursive) {
if (!recursive) {
return (TEMP_FAILURE_RETRY(remove(dir_name)) == 0);
} else {
return DeleteRecursively(dir_name);
}
}
bool Directory::Rename(const char* path, const char* new_path) {
ExistsResult exists = Exists(path);
if (exists != EXISTS) return false;
return (TEMP_FAILURE_RETRY(rename(path, new_path)) == 0);
}

View file

@ -26,7 +26,9 @@ enum MessageFlags {
// The event handler delegation class is OS specific.
#if defined(TARGET_OS_LINUX)
#if defined(TARGET_OS_ANDROID)
#include "bin/eventhandler_android.h"
#elif defined(TARGET_OS_LINUX)
#include "bin/eventhandler_linux.h"
#elif defined(TARGET_OS_MACOS)
#include "bin/eventhandler_macos.h"

View file

@ -0,0 +1,429 @@
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
#include "bin/eventhandler.h"
#include <errno.h>
#include <pthread.h>
#include <stdio.h>
#include <string.h>
#include <sys/epoll.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <unistd.h>
#include "bin/dartutils.h"
#include "bin/fdutils.h"
#include "platform/hashmap.h"
#include "platform/thread.h"
#include "platform/utils.h"
int64_t GetCurrentTimeMilliseconds() {
struct timeval tv;
if (gettimeofday(&tv, NULL) < 0) {
UNREACHABLE();
return 0;
}
return ((static_cast<int64_t>(tv.tv_sec) * 1000000) + tv.tv_usec) / 1000;
}
static const int kInterruptMessageSize = sizeof(InterruptMessage);
static const int kInfinityTimeout = -1;
static const int kTimerId = -1;
static const int kShutdownId = -2;
intptr_t SocketData::GetPollEvents() {
// Do not ask for EPOLLERR and EPOLLHUP explicitly as they are
// triggered anyway.
intptr_t events = 0;
if (!IsClosedRead()) {
if ((mask_ & (1 << kInEvent)) != 0) {
events |= EPOLLIN;
}
}
if (!IsClosedWrite()) {
if ((mask_ & (1 << kOutEvent)) != 0) {
events |= EPOLLOUT;
}
}
return events;
}
// Unregister the file descriptor for a SocketData structure with epoll.
static void RemoveFromEpollInstance(intptr_t epoll_fd_, SocketData* sd) {
if (sd->tracked_by_epoll()) {
int status = TEMP_FAILURE_RETRY(epoll_ctl(epoll_fd_,
EPOLL_CTL_DEL,
sd->fd(),
NULL));
if (status == -1) {
FATAL("Failed unregistering events for file descriptor");
}
sd->set_tracked_by_epoll(false);
}
}
// Register the file descriptor for a SocketData structure with epoll
// if events are requested.
static void UpdateEpollInstance(intptr_t epoll_fd_, SocketData* sd) {
struct epoll_event event;
event.events = sd->GetPollEvents();
event.data.ptr = sd;
if (sd->port() != 0 && event.events != 0) {
int status = 0;
if (sd->tracked_by_epoll()) {
status = TEMP_FAILURE_RETRY(epoll_ctl(epoll_fd_,
EPOLL_CTL_MOD,
sd->fd(),
&event));
} else {
status = TEMP_FAILURE_RETRY(epoll_ctl(epoll_fd_,
EPOLL_CTL_ADD,
sd->fd(),
&event));
sd->set_tracked_by_epoll(true);
}
if (status == -1) {
FATAL1("Failed updating epoll instance: %s", strerror(errno));
}
}
}
EventHandlerImplementation::EventHandlerImplementation()
: socket_map_(&HashMap::SamePointerValue, 16) {
intptr_t result;
result = TEMP_FAILURE_RETRY(pipe(interrupt_fds_));
if (result != 0) {
FATAL("Pipe creation failed");
}
FDUtils::SetNonBlocking(interrupt_fds_[0]);
timeout_ = kInfinityTimeout;
timeout_port_ = 0;
shutdown_ = false;
// The initial size passed to epoll_create is ignore on newer (>=
// 2.6.8) Linux versions
static const int kEpollInitialSize = 64;
epoll_fd_ = TEMP_FAILURE_RETRY(epoll_create(kEpollInitialSize));
if (epoll_fd_ == -1) {
FATAL("Failed creating epoll file descriptor");
}
// Register the interrupt_fd with the epoll instance.
struct epoll_event event;
event.events = EPOLLIN;
event.data.ptr = NULL;
int status = TEMP_FAILURE_RETRY(epoll_ctl(epoll_fd_,
EPOLL_CTL_ADD,
interrupt_fds_[0],
&event));
if (status == -1) {
FATAL("Failed adding interrupt fd to epoll instance");
}
}
EventHandlerImplementation::~EventHandlerImplementation() {
TEMP_FAILURE_RETRY(close(interrupt_fds_[0]));
TEMP_FAILURE_RETRY(close(interrupt_fds_[1]));
}
SocketData* EventHandlerImplementation::GetSocketData(intptr_t fd) {
ASSERT(fd >= 0);
HashMap::Entry* entry = socket_map_.Lookup(
GetHashmapKeyFromFd(fd), GetHashmapHashFromFd(fd), true);
ASSERT(entry != NULL);
SocketData* sd = reinterpret_cast<SocketData*>(entry->value);
if (sd == NULL) {
// If there is no data in the hash map for this file descriptor a
// new SocketData for the file descriptor is inserted.
sd = new SocketData(fd);
entry->value = sd;
}
ASSERT(fd == sd->fd());
return sd;
}
void EventHandlerImplementation::WakeupHandler(intptr_t id,
Dart_Port dart_port,
int64_t data) {
InterruptMessage msg;
msg.id = id;
msg.dart_port = dart_port;
msg.data = data;
intptr_t result =
FDUtils::WriteToBlocking(interrupt_fds_[1], &msg, kInterruptMessageSize);
if (result != kInterruptMessageSize) {
if (result == -1) {
perror("Interrupt message failure:");
}
FATAL1("Interrupt message failure. Wrote %d bytes.", result);
}
}
bool EventHandlerImplementation::GetInterruptMessage(InterruptMessage* msg) {
char* dst = reinterpret_cast<char*>(msg);
int total_read = 0;
int bytes_read =
TEMP_FAILURE_RETRY(read(interrupt_fds_[0], dst, kInterruptMessageSize));
if (bytes_read < 0) {
return false;
}
total_read = bytes_read;
while (total_read < kInterruptMessageSize) {
bytes_read = TEMP_FAILURE_RETRY(read(interrupt_fds_[0],
dst + total_read,
kInterruptMessageSize - total_read));
if (bytes_read > 0) {
total_read = total_read + bytes_read;
}
}
return (total_read == kInterruptMessageSize) ? true : false;
}
void EventHandlerImplementation::HandleInterruptFd() {
InterruptMessage msg;
while (GetInterruptMessage(&msg)) {
if (msg.id == kTimerId) {
timeout_ = msg.data;
timeout_port_ = msg.dart_port;
} else if (msg.id == kShutdownId) {
shutdown_ = true;
} else {
SocketData* sd = GetSocketData(msg.id);
if ((msg.data & (1 << kShutdownReadCommand)) != 0) {
ASSERT(msg.data == (1 << kShutdownReadCommand));
// Close the socket for reading.
sd->ShutdownRead();
UpdateEpollInstance(epoll_fd_, sd);
} else if ((msg.data & (1 << kShutdownWriteCommand)) != 0) {
ASSERT(msg.data == (1 << kShutdownWriteCommand));
// Close the socket for writing.
sd->ShutdownWrite();
UpdateEpollInstance(epoll_fd_, sd);
} else if ((msg.data & (1 << kCloseCommand)) != 0) {
ASSERT(msg.data == (1 << kCloseCommand));
// Close the socket and free system resources and move on to
// next message.
RemoveFromEpollInstance(epoll_fd_, sd);
intptr_t fd = sd->fd();
sd->Close();
socket_map_.Remove(GetHashmapKeyFromFd(fd), GetHashmapHashFromFd(fd));
delete sd;
} else {
// Setup events to wait for.
sd->SetPortAndMask(msg.dart_port, msg.data);
UpdateEpollInstance(epoll_fd_, sd);
}
}
}
}
#ifdef DEBUG_POLL
static void PrintEventMask(intptr_t fd, intptr_t events) {
printf("%d ", fd);
if ((events & EPOLLIN) != 0) printf("EPOLLIN ");
if ((events & EPOLLPRI) != 0) printf("EPOLLPRI ");
if ((events & EPOLLOUT) != 0) printf("EPOLLOUT ");
if ((events & EPOLLERR) != 0) printf("EPOLLERR ");
if ((events & EPOLLHUP) != 0) printf("EPOLLHUP ");
if ((events & EPOLLRDHUP) != 0) printf("EPOLLRDHUP ");
int all_events = EPOLLIN | EPOLLPRI | EPOLLOUT |
EPOLLERR | EPOLLHUP | EPOLLRDHUP;
if ((events & ~all_events) != 0) {
printf("(and %08x) ", events & ~all_events);
}
printf("(available %d) ", FDUtils::AvailableBytes(fd));
printf("\n");
}
#endif
intptr_t EventHandlerImplementation::GetPollEvents(intptr_t events,
SocketData* sd) {
#ifdef DEBUG_POLL
PrintEventMask(sd->fd(), events);
#endif
intptr_t event_mask = 0;
if (sd->IsListeningSocket()) {
// For listening sockets the EPOLLIN event indicate that there are
// connections ready for accept unless accompanied with one of the
// other flags.
if ((events & EPOLLIN) != 0) {
if ((events & EPOLLHUP) != 0) event_mask |= (1 << kCloseEvent);
if ((events & EPOLLERR) != 0) event_mask |= (1 << kErrorEvent);
if (event_mask == 0) event_mask |= (1 << kInEvent);
}
} else {
// Prioritize data events over close and error events.
if ((events & EPOLLIN) != 0) {
if (FDUtils::AvailableBytes(sd->fd()) != 0) {
event_mask = (1 << kInEvent);
} else if ((events & EPOLLHUP) != 0) {
// If both EPOLLHUP and EPOLLERR are reported treat it as an
// error.
if ((events & EPOLLERR) != 0) {
event_mask = (1 << kErrorEvent);
} else {
event_mask = (1 << kCloseEvent);
}
sd->MarkClosedRead();
} else if ((events & EPOLLERR) != 0) {
event_mask = (1 << kErrorEvent);
} else {
if (sd->IsPipe()) {
// When reading from stdin (either from a terminal or piped
// input) treat EPOLLIN with 0 available bytes as
// end-of-file.
if (sd->fd() == STDIN_FILENO) {
event_mask = (1 << kCloseEvent);
sd->MarkClosedRead();
}
} else {
// If EPOLLIN is set with no available data and no EPOLLHUP use
// recv to peek for whether the other end of the socket
// actually closed.
char buffer;
ssize_t bytesPeeked =
TEMP_FAILURE_RETRY(recv(sd->fd(), &buffer, 1, MSG_PEEK));
ASSERT(EAGAIN == EWOULDBLOCK);
if (bytesPeeked == 0) {
event_mask = (1 << kCloseEvent);
sd->MarkClosedRead();
} else if (errno != EWOULDBLOCK) {
fprintf(stderr, "Error recv: %s\n", strerror(errno));
}
}
}
}
// On pipes EPOLLHUP is reported without EPOLLIN when there is no
// more data to read.
if (sd->IsPipe()) {
if (((events & EPOLLIN) == 0) &&
((events & EPOLLHUP) != 0)) {
event_mask = (1 << kCloseEvent);
sd->MarkClosedRead();
}
}
if ((events & EPOLLOUT) != 0) {
if ((events & EPOLLERR) != 0) {
event_mask = (1 << kErrorEvent);
sd->MarkClosedWrite();
} else {
event_mask |= (1 << kOutEvent);
}
}
}
return event_mask;
}
void EventHandlerImplementation::HandleEvents(struct epoll_event* events,
int size) {
for (int i = 0; i < size; i++) {
if (events[i].data.ptr != NULL) {
SocketData* sd = reinterpret_cast<SocketData*>(events[i].data.ptr);
intptr_t event_mask = GetPollEvents(events[i].events, sd);
if (event_mask != 0) {
// Unregister events for the file descriptor. Events will be
// registered again when the current event has been handled in
// Dart code.
RemoveFromEpollInstance(epoll_fd_, sd);
Dart_Port port = sd->port();
ASSERT(port != 0);
DartUtils::PostInt32(port, event_mask);
}
}
}
HandleInterruptFd();
}
intptr_t EventHandlerImplementation::GetTimeout() {
if (timeout_ == kInfinityTimeout) {
return kInfinityTimeout;
}
intptr_t millis = timeout_ - GetCurrentTimeMilliseconds();
return (millis < 0) ? 0 : millis;
}
void EventHandlerImplementation::HandleTimeout() {
if (timeout_ != kInfinityTimeout) {
intptr_t millis = timeout_ - GetCurrentTimeMilliseconds();
if (millis <= 0) {
DartUtils::PostNull(timeout_port_);
timeout_ = kInfinityTimeout;
timeout_port_ = 0;
}
}
}
void EventHandlerImplementation::Poll(uword args) {
static const intptr_t kMaxEvents = 16;
struct epoll_event events[kMaxEvents];
EventHandlerImplementation* handler =
reinterpret_cast<EventHandlerImplementation*>(args);
ASSERT(handler != NULL);
while (!handler->shutdown_) {
intptr_t millis = handler->GetTimeout();
intptr_t result = TEMP_FAILURE_RETRY(epoll_wait(handler->epoll_fd_,
events,
kMaxEvents,
millis));
ASSERT(EAGAIN == EWOULDBLOCK);
if (result == -1) {
if (errno != EWOULDBLOCK) {
perror("Poll failed");
}
} else {
handler->HandleTimeout();
handler->HandleEvents(events, result);
}
}
}
void EventHandlerImplementation::Start() {
int result = dart::Thread::Start(&EventHandlerImplementation::Poll,
reinterpret_cast<uword>(this));
if (result != 0) {
FATAL1("Failed to start event handler thread %d", result);
}
}
void EventHandlerImplementation::Shutdown() {
SendData(kShutdownId, 0, 0);
}
void EventHandlerImplementation::SendData(intptr_t id,
Dart_Port dart_port,
intptr_t data) {
WakeupHandler(id, dart_port, data);
}
void* EventHandlerImplementation::GetHashmapKeyFromFd(intptr_t fd) {
// The hashmap does not support keys with value 0.
return reinterpret_cast<void*>(fd + 1);
}
uint32_t EventHandlerImplementation::GetHashmapHashFromFd(intptr_t fd) {
// The hashmap does not support keys with value 0.
return dart::Utils::WordHash(fd + 1);
}

View file

@ -0,0 +1,122 @@
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
#ifndef BIN_EVENTHANDLER_ANDROID_H_
#define BIN_EVENTHANDLER_ANDROID_H_
#if !defined(BIN_EVENTHANDLER_H_)
#error Do not include eventhandler_android.h directly;
#error use eventhandler.h instead.
#endif
#include <unistd.h>
#include <sys/socket.h>
#include "platform/hashmap.h"
class InterruptMessage {
public:
intptr_t id;
Dart_Port dart_port;
int64_t data;
};
enum PortDataFlags {
kClosedRead = 0,
kClosedWrite = 1,
};
class SocketData {
public:
explicit SocketData(intptr_t fd)
: tracked_by_epoll_(false), fd_(fd), port_(0), mask_(0), flags_(0) {
ASSERT(fd_ != -1);
}
intptr_t GetPollEvents();
void ShutdownRead() {
shutdown(fd_, SHUT_RD);
MarkClosedRead();
}
void ShutdownWrite() {
shutdown(fd_, SHUT_WR);
MarkClosedWrite();
}
void Close() {
port_ = 0;
mask_ = 0;
flags_ = 0;
close(fd_);
fd_ = -1;
}
bool IsListeningSocket() { return (mask_ & (1 << kListeningSocket)) != 0; }
bool IsPipe() { return (mask_ & (1 << kPipe)) != 0; }
bool IsClosedRead() { return (flags_ & (1 << kClosedRead)) != 0; }
bool IsClosedWrite() { return (flags_ & (1 << kClosedWrite)) != 0; }
void MarkClosedRead() { flags_ |= (1 << kClosedRead); }
void MarkClosedWrite() { flags_ |= (1 << kClosedWrite); }
void SetPortAndMask(Dart_Port port, intptr_t mask) {
ASSERT(fd_ != -1);
port_ = port;
mask_ = mask;
}
intptr_t fd() { return fd_; }
Dart_Port port() { return port_; }
intptr_t mask() { return mask_; }
bool tracked_by_epoll() { return tracked_by_epoll_; }
void set_tracked_by_epoll(bool value) { tracked_by_epoll_ = value; }
private:
bool tracked_by_epoll_;
intptr_t fd_;
Dart_Port port_;
intptr_t mask_;
intptr_t flags_;
};
class EventHandlerImplementation {
public:
EventHandlerImplementation();
~EventHandlerImplementation();
// Gets the socket data structure for a given file
// descriptor. Creates a new one if one is not found.
SocketData* GetSocketData(intptr_t fd);
void SendData(intptr_t id, Dart_Port dart_port, intptr_t data);
void Start();
void Shutdown();
private:
intptr_t GetTimeout();
bool GetInterruptMessage(InterruptMessage* msg);
void HandleEvents(struct epoll_event* events, int size);
void HandleTimeout();
static void Poll(uword args);
void WakeupHandler(intptr_t id, Dart_Port dart_port, int64_t data);
void HandleInterruptFd();
void SetPort(intptr_t fd, Dart_Port dart_port, intptr_t mask);
intptr_t GetPollEvents(intptr_t events, SocketData* sd);
static void* GetHashmapKeyFromFd(intptr_t fd);
static uint32_t GetHashmapHashFromFd(intptr_t fd);
HashMap socket_map_;
int64_t timeout_; // Time for next timeout.
Dart_Port timeout_port_;
bool shutdown_;
int interrupt_fds_[2];
int epoll_fd_;
};
#endif // BIN_EVENTHANDLER_ANDROID_H_

View file

@ -0,0 +1,22 @@
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
#include "bin/extensions.h"
#include <dlfcn.h>
void* Extensions::LoadExtensionLibrary(const char* library_path,
const char* extension_name) {
const char* strings[] = { library_path, "/lib",
extension_name, ".so", NULL };
char* library_file = Concatenate(strings);
void* lib_handle = dlopen(library_file, RTLD_LAZY);
free(library_file);
return lib_handle;
}
void* Extensions::ResolveSymbol(void* lib_handle, const char* symbol) {
void* result = dlsym(lib_handle, symbol);
if (dlerror() != NULL) return NULL;
return result;
}

View file

@ -0,0 +1,118 @@
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include "bin/fdutils.h"
static bool SetBlockingHelper(intptr_t fd, bool blocking) {
intptr_t status;
status = TEMP_FAILURE_RETRY(fcntl(fd, F_GETFL));
if (status < 0) {
perror("fcntl F_GETFL failed");
return false;
}
status = blocking ? (status & ~O_NONBLOCK) : (status | O_NONBLOCK);
if (TEMP_FAILURE_RETRY(fcntl(fd, F_SETFL, status)) < 0) {
perror("fcntl F_SETFL failed");
return false;
}
return true;
}
bool FDUtils::SetNonBlocking(intptr_t fd) {
return SetBlockingHelper(fd, false);
}
bool FDUtils::SetBlocking(intptr_t fd) {
return SetBlockingHelper(fd, true);
}
bool FDUtils::IsBlocking(intptr_t fd, bool* is_blocking) {
intptr_t status;
status = TEMP_FAILURE_RETRY(fcntl(fd, F_GETFL));
if (status < 0) {
perror("fcntl F_GETFL failed");
return false;
}
*is_blocking = (status & O_NONBLOCK) == 0;
return true;
}
intptr_t FDUtils::AvailableBytes(intptr_t fd) {
int available; // ioctl for FIONREAD expects an 'int*' argument.
int result = TEMP_FAILURE_RETRY(ioctl(fd, FIONREAD, &available));
if (result < 0) {
return result;
}
#ifdef DEBUG
ASSERT(available >= 0);
#endif
return static_cast<intptr_t>(available);
}
ssize_t FDUtils::ReadFromBlocking(int fd, void* buffer, size_t count) {
#ifdef DEBUG
bool is_blocking = false;
ASSERT(FDUtils::IsBlocking(fd, &is_blocking));
ASSERT(is_blocking);
#endif
size_t remaining = count;
char* buffer_pos = reinterpret_cast<char*>(buffer);
while (remaining > 0) {
ssize_t bytes_read = TEMP_FAILURE_RETRY(read(fd, buffer_pos, remaining));
if (bytes_read == 0) {
return count - remaining;
} else if (bytes_read == -1) {
ASSERT(EAGAIN == EWOULDBLOCK);
// Error code EWOULDBLOCK should only happen for non blocking
// file descriptors.
ASSERT(errno != EWOULDBLOCK);
return -1;
} else {
ASSERT((bytes_read > 0));
remaining -= bytes_read;
buffer_pos += bytes_read;
}
}
return count;
}
ssize_t FDUtils::WriteToBlocking(int fd, const void* buffer, size_t count) {
#ifdef DEBUG
bool is_blocking = false;
ASSERT(FDUtils::IsBlocking(fd, &is_blocking));
ASSERT(is_blocking);
#endif
size_t remaining = count;
char* buffer_pos = const_cast<char*>(reinterpret_cast<const char*>(buffer));
while (remaining > 0) {
ssize_t bytes_written =
TEMP_FAILURE_RETRY(write(fd, buffer_pos, remaining));
if (bytes_written == 0) {
return count - remaining;
} else if (bytes_written == -1) {
ASSERT(EAGAIN == EWOULDBLOCK);
// Error code EWOULDBLOCK should only happen for non blocking
// file descriptors.
ASSERT(errno != EWOULDBLOCK);
return -1;
} else {
ASSERT(bytes_written > 0);
remaining -= bytes_written;
buffer_pos += bytes_written;
}
}
return count;
}

242
runtime/bin/file_android.cc Normal file
View file

@ -0,0 +1,242 @@
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
#include "bin/file.h"
#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
#include <libgen.h>
#include "bin/builtin.h"
class FileHandle {
public:
explicit FileHandle(int fd) : fd_(fd) { }
~FileHandle() { }
int fd() const { return fd_; }
void set_fd(int fd) { fd_ = fd; }
private:
int fd_;
DISALLOW_COPY_AND_ASSIGN(FileHandle);
};
File::~File() {
// Close the file (unless it's a standard stream).
if (handle_->fd() > STDERR_FILENO) {
Close();
}
delete handle_;
}
void File::Close() {
ASSERT(handle_->fd() >= 0);
int err = TEMP_FAILURE_RETRY(close(handle_->fd()));
if (err != 0) {
const int kBufferSize = 1024;
char error_message[kBufferSize];
strerror_r(errno, error_message, kBufferSize);
fprintf(stderr, "%s\n", error_message);
}
handle_->set_fd(kClosedFd);
}
bool File::IsClosed() {
return handle_->fd() == kClosedFd;
}
int64_t File::Read(void* buffer, int64_t num_bytes) {
ASSERT(handle_->fd() >= 0);
return TEMP_FAILURE_RETRY(read(handle_->fd(), buffer, num_bytes));
}
int64_t File::Write(const void* buffer, int64_t num_bytes) {
ASSERT(handle_->fd() >= 0);
return TEMP_FAILURE_RETRY(write(handle_->fd(), buffer, num_bytes));
}
off_t File::Position() {
ASSERT(handle_->fd() >= 0);
return TEMP_FAILURE_RETRY(lseek(handle_->fd(), 0, SEEK_CUR));
}
bool File::SetPosition(int64_t position) {
ASSERT(handle_->fd() >= 0);
return TEMP_FAILURE_RETRY(lseek(handle_->fd(), position, SEEK_SET) != -1);
}
bool File::Truncate(int64_t length) {
ASSERT(handle_->fd() >= 0);
return TEMP_FAILURE_RETRY(ftruncate(handle_->fd(), length) != -1);
}
bool File::Flush() {
ASSERT(handle_->fd() >= 0);
return TEMP_FAILURE_RETRY(fsync(handle_->fd()) != -1);
}
off_t File::Length() {
ASSERT(handle_->fd() >= 0);
struct stat st;
if (TEMP_FAILURE_RETRY(fstat(handle_->fd(), &st)) == 0) {
return st.st_size;
}
return -1;
}
File* File::Open(const char* name, FileOpenMode mode) {
// Report errors for non-regular files.
struct stat st;
if (TEMP_FAILURE_RETRY(stat(name, &st)) == 0) {
if (!S_ISREG(st.st_mode)) {
errno = (S_ISDIR(st.st_mode)) ? EISDIR : ENOENT;
return NULL;
}
}
int flags = O_RDONLY;
if ((mode & kWrite) != 0) {
flags = (O_RDWR | O_CREAT);
}
if ((mode & kTruncate) != 0) {
flags = flags | O_TRUNC;
}
int fd = TEMP_FAILURE_RETRY(open(name, flags, 0666));
if (fd < 0) {
return NULL;
}
if (((mode & kWrite) != 0) && ((mode & kTruncate) == 0)) {
int position = TEMP_FAILURE_RETRY(lseek(fd, 0, SEEK_END));
if (position < 0) {
return NULL;
}
}
return new File(name, new FileHandle(fd));
}
File* File::OpenStdio(int fd) {
if (fd < 0 || 2 < fd) return NULL;
return new File(NULL, new FileHandle(fd));
}
bool File::Exists(const char* name) {
struct stat st;
if (TEMP_FAILURE_RETRY(stat(name, &st)) == 0) {
return S_ISREG(st.st_mode);
} else {
return false;
}
}
bool File::Create(const char* name) {
int fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY | O_CREAT, 0666));
if (fd < 0) {
return false;
}
return (close(fd) == 0);
}
bool File::Delete(const char* name) {
int status = TEMP_FAILURE_RETRY(remove(name));
if (status == -1) {
return false;
}
return true;
}
off_t File::LengthFromName(const char* name) {
struct stat st;
if (TEMP_FAILURE_RETRY(stat(name, &st)) == 0) {
return st.st_size;
}
return -1;
}
time_t File::LastModified(const char* name) {
struct stat st;
if (TEMP_FAILURE_RETRY(stat(name, &st)) == 0) {
return st.st_mtime;
}
return -1;
}
bool File::IsAbsolutePath(const char* pathname) {
return (pathname != NULL && pathname[0] == '/');
}
char* File::GetCanonicalPath(const char* pathname) {
char* abs_path = NULL;
if (pathname != NULL) {
do {
abs_path = realpath(pathname, NULL);
} while (abs_path == NULL && errno == EINTR);
ASSERT(abs_path == NULL || IsAbsolutePath(abs_path));
}
return abs_path;
}
char* File::GetContainingDirectory(char* pathname) {
// Report errors for non-regular files.
struct stat st;
if (TEMP_FAILURE_RETRY(stat(pathname, &st)) == 0) {
if (!S_ISREG(st.st_mode)) {
errno = (S_ISDIR(st.st_mode)) ? EISDIR : ENOENT;
return NULL;
}
} else {
return NULL;
}
char* path = NULL;
do {
path = dirname(pathname);
} while (path == NULL && errno == EINTR);
return GetCanonicalPath(path);
}
const char* File::PathSeparator() {
return "/";
}
const char* File::StringEscapedPathSeparator() {
return "/";
}
File::StdioHandleType File::GetStdioHandleType(int fd) {
ASSERT(0 <= fd && fd <= 2);
struct stat buf;
int result = fstat(fd, &buf);
if (result == -1) {
FATAL2("Failed stat on file descriptor %d: %s", fd, strerror(errno));
}
if (S_ISCHR(buf.st_mode)) return kTerminal;
if (S_ISFIFO(buf.st_mode)) return kPipe;
if (S_ISSOCK(buf.st_mode)) return kSocket;
if (S_ISREG(buf.st_mode)) return kFile;
return kOther;
}

View file

@ -0,0 +1,63 @@
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
#include "bin/platform.h"
#include <signal.h>
#include <string.h>
#include <unistd.h>
bool Platform::Initialize() {
// Turn off the signal handler for SIGPIPE as it causes the process
// to terminate on writing to a closed pipe. Without the signal
// handler error EPIPE is set instead.
struct sigaction act;
bzero(&act, sizeof(act));
act.sa_handler = SIG_IGN;
if (sigaction(SIGPIPE, &act, 0) != 0) {
perror("Setting signal handler failed");
return false;
}
return true;
}
int Platform::NumberOfProcessors() {
return sysconf(_SC_NPROCESSORS_ONLN);
}
const char* Platform::OperatingSystem() {
return "android";
}
bool Platform::LocalHostname(char *buffer, intptr_t buffer_length) {
return gethostname(buffer, buffer_length) == 0;
}
char** Platform::Environment(intptr_t* count) {
// Using environ directly is only safe as long as we do not
// provide access to modifying environment variables.
intptr_t i = 0;
char** tmp = environ;
while (*(tmp++) != NULL) i++;
*count = i;
char** result = new char*[i];
for (intptr_t current = 0; current < i; current++) {
result[current] = environ[current];
}
return result;
}
char* Platform::StrError(int error_code) {
static const int kBufferSize = 1024;
char* error = static_cast<char*>(malloc(kBufferSize));
error[0] = '\0';
strerror_r(error_code, error, kBufferSize);
return error;
}

View file

@ -0,0 +1,570 @@
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
#include "bin/process.h"
#include <errno.h>
#include <fcntl.h>
#include <poll.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <unistd.h>
#include "bin/fdutils.h"
#include "bin/thread.h"
// ProcessInfo is used to map a process id to the file descriptor for
// the pipe used to communicate the exit code of the process to Dart.
// ProcessInfo objects are kept in the static singly-linked
// ProcessInfoList.
class ProcessInfo {
public:
ProcessInfo(pid_t pid, intptr_t fd) : pid_(pid), fd_(fd) { }
~ProcessInfo() {
int closed = TEMP_FAILURE_RETRY(close(fd_));
if (closed != 0) {
FATAL("Failed to close process exit code pipe");
}
}
pid_t pid() { return pid_; }
intptr_t fd() { return fd_; }
ProcessInfo* next() { return next_; }
void set_next(ProcessInfo* info) { next_ = info; }
private:
pid_t pid_;
intptr_t fd_;
ProcessInfo* next_;
};
// Singly-linked list of ProcessInfo objects for all active processes
// started from Dart.
class ProcessInfoList {
public:
static void AddProcess(pid_t pid, intptr_t fd) {
MutexLocker locker(&mutex_);
ProcessInfo* info = new ProcessInfo(pid, fd);
info->set_next(active_processes_);
active_processes_ = info;
}
static intptr_t LookupProcessExitFd(pid_t pid) {
MutexLocker locker(&mutex_);
ProcessInfo* current = active_processes_;
while (current != NULL) {
if (current->pid() == pid) {
return current->fd();
}
current = current->next();
}
return 0;
}
static void RemoveProcess(pid_t pid) {
MutexLocker locker(&mutex_);
ProcessInfo* prev = NULL;
ProcessInfo* current = active_processes_;
while (current != NULL) {
if (current->pid() == pid) {
if (prev == NULL) {
active_processes_ = current->next();
} else {
prev->set_next(current->next());
}
delete current;
return;
}
prev = current;
current = current->next();
}
}
private:
// Linked list of ProcessInfo objects for all active processes
// started from Dart code.
static ProcessInfo* active_processes_;
// Mutex protecting all accesses to the linked list of active
// processes.
static dart::Mutex mutex_;
};
ProcessInfo* ProcessInfoList::active_processes_ = NULL;
dart::Mutex ProcessInfoList::mutex_;
// The exit code handler sets up a separate thread which is signalled
// on SIGCHLD. That separate thread can then get the exit code from
// processes that have exited and communicate it to Dart through the
// event loop.
class ExitCodeHandler {
public:
// Ensure that the ExitCodeHandler has been initialized.
static bool EnsureInitialized() {
// Multiple isolates could be starting processes at the same
// time. Make sure that only one of them initializes the
// ExitCodeHandler.
MutexLocker locker(&mutex_);
if (initialized_) {
return true;
}
// Allocate a pipe that the signal handler can write a byte to and
// that the exit handler thread can poll.
int result = TEMP_FAILURE_RETRY(pipe(sig_chld_fds_));
if (result < 0) {
return false;
}
// Start thread that polls the pipe and handles process exits when
// data is received on the pipe.
result = dart::Thread::Start(ExitCodeHandlerEntry, sig_chld_fds_[0]);
if (result != 0) {
FATAL1("Failed to start exit code handler worker thread %d", result);
}
// Mark write end non-blocking.
FDUtils::SetNonBlocking(sig_chld_fds_[1]);
// Thread started and the ExitCodeHandler is initialized.
initialized_ = true;
return true;
}
// Get the write end of the pipe.
static int WakeUpFd() {
ASSERT(initialized_);
return sig_chld_fds_[1];
}
static void TerminateExitCodeThread() {
MutexLocker locker(&mutex_);
if (!initialized_) {
return;
}
uint8_t data = kThreadTerminateByte;
ssize_t result =
TEMP_FAILURE_RETRY(write(ExitCodeHandler::WakeUpFd(), &data, 1));
if (result < 1) {
perror("Failed to write to wake-up fd to terminate exit code thread");
}
{
MonitorLocker terminate_locker(&thread_terminate_monitor_);
while (!thread_terminated_) {
terminate_locker.Wait();
}
}
}
static void ExitCodeThreadTerminated() {
MonitorLocker locker(&thread_terminate_monitor_);
thread_terminated_ = true;
locker.Notify();
}
private:
static const uint8_t kThreadTerminateByte = 1;
// GetProcessExitCodes is called on a separate thread when a SIGCHLD
// signal is received to retrieve the exit codes and post them to
// dart.
static void GetProcessExitCodes() {
pid_t pid = 0;
int status = 0;
while ((pid = TEMP_FAILURE_RETRY(waitpid(-1, &status, WNOHANG))) > 0) {
int exit_code = 0;
int negative = 0;
if (WIFEXITED(status)) {
exit_code = WEXITSTATUS(status);
}
if (WIFSIGNALED(status)) {
exit_code = WTERMSIG(status);
negative = 1;
}
intptr_t exit_code_fd = ProcessInfoList::LookupProcessExitFd(pid);
if (exit_code_fd != 0) {
int message[2] = { exit_code, negative };
ssize_t result =
FDUtils::WriteToBlocking(exit_code_fd, &message, sizeof(message));
// If the process has been closed, the read end of the exit
// pipe has been closed. It is therefore not a problem that
// write fails with a broken pipe error. Other errors should
// not happen.
if (result != -1 && result != sizeof(message)) {
FATAL("Failed to write entire process exit message");
} else if (result == -1 && errno != EPIPE) {
FATAL1("Failed to write exit code: %d", errno);
}
ProcessInfoList::RemoveProcess(pid);
}
}
}
// Entry point for the separate exit code handler thread started by
// the ExitCodeHandler.
static void ExitCodeHandlerEntry(uword param) {
struct pollfd pollfds;
pollfds.fd = param;
pollfds.events = POLLIN;
while (true) {
int result = TEMP_FAILURE_RETRY(poll(&pollfds, 1, -1));
if (result == -1) {
ASSERT(EAGAIN == EWOULDBLOCK);
if (errno != EWOULDBLOCK) {
perror("ExitCodeHandler poll failed");
}
} else {
// Read the byte from the wake-up fd.
ASSERT(result = 1);
intptr_t data = 0;
ssize_t read_bytes = FDUtils::ReadFromBlocking(pollfds.fd, &data, 1);
if (read_bytes < 1) {
perror("Failed to read from wake-up fd in exit-code handler");
}
if (data == ExitCodeHandler::kThreadTerminateByte) {
ExitCodeThreadTerminated();
return;
}
// Get the exit code from all processes that have died.
GetProcessExitCodes();
}
}
}
static dart::Mutex mutex_;
static bool initialized_;
static int sig_chld_fds_[2];
static bool thread_terminated_;
static dart::Monitor thread_terminate_monitor_;
};
dart::Mutex ExitCodeHandler::mutex_;
bool ExitCodeHandler::initialized_ = false;
int ExitCodeHandler::sig_chld_fds_[2] = { 0, 0 };
bool ExitCodeHandler::thread_terminated_ = false;
dart::Monitor ExitCodeHandler::thread_terminate_monitor_;
static char* SafeStrNCpy(char* dest, const char* src, size_t n) {
strncpy(dest, src, n);
dest[n - 1] = '\0';
return dest;
}
static void SetChildOsErrorMessage(char* os_error_message,
int os_error_message_len) {
SafeStrNCpy(os_error_message, strerror(errno), os_error_message_len);
}
static void SigChldHandler(int process_signal, siginfo_t* siginfo, void* tmp) {
// Save errno so it can be restored at the end.
int entry_errno = errno;
// Signal the exit code handler where the actual processing takes
// place.
ssize_t result =
TEMP_FAILURE_RETRY(write(ExitCodeHandler::WakeUpFd(), "", 1));
if (result < 1) {
perror("Failed to write to wake-up fd in SIGCHLD handler");
}
// Restore errno.
errno = entry_errno;
}
static void ReportChildError(int exec_control_fd) {
// In the case of failure in the child process write the errno and
// the OS error message to the exec control pipe and exit.
int child_errno = errno;
char* os_error_message = strerror(errno);
ASSERT(sizeof(child_errno) == sizeof(errno));
int bytes_written =
FDUtils::WriteToBlocking(
exec_control_fd, &child_errno, sizeof(child_errno));
if (bytes_written == sizeof(child_errno)) {
FDUtils::WriteToBlocking(
exec_control_fd, os_error_message, strlen(os_error_message) + 1);
}
TEMP_FAILURE_RETRY(close(exec_control_fd));
exit(1);
}
int Process::Start(const char* path,
char* arguments[],
intptr_t arguments_length,
const char* working_directory,
char* environment[],
intptr_t environment_length,
intptr_t* in,
intptr_t* out,
intptr_t* err,
intptr_t* id,
intptr_t* exit_event,
char* os_error_message,
int os_error_message_len) {
pid_t pid;
int read_in[2]; // Pipe for stdout to child process.
int read_err[2]; // Pipe for stderr to child process.
int write_out[2]; // Pipe for stdin to child process.
int exec_control[2]; // Pipe to get the result from exec.
int result;
bool initialized = ExitCodeHandler::EnsureInitialized();
if (!initialized) {
SetChildOsErrorMessage(os_error_message, os_error_message_len);
fprintf(stderr,
"Error initializing exit code handler: %s\n",
os_error_message);
return errno;
}
result = TEMP_FAILURE_RETRY(pipe(read_in));
if (result < 0) {
SetChildOsErrorMessage(os_error_message, os_error_message_len);
fprintf(stderr, "Error pipe creation failed: %s\n", os_error_message);
return errno;
}
result = TEMP_FAILURE_RETRY(pipe(read_err));
if (result < 0) {
SetChildOsErrorMessage(os_error_message, os_error_message_len);
TEMP_FAILURE_RETRY(close(read_in[0]));
TEMP_FAILURE_RETRY(close(read_in[1]));
fprintf(stderr, "Error pipe creation failed: %s\n", os_error_message);
return errno;
}
result = TEMP_FAILURE_RETRY(pipe(write_out));
if (result < 0) {
SetChildOsErrorMessage(os_error_message, os_error_message_len);
TEMP_FAILURE_RETRY(close(read_in[0]));
TEMP_FAILURE_RETRY(close(read_in[1]));
TEMP_FAILURE_RETRY(close(read_err[0]));
TEMP_FAILURE_RETRY(close(read_err[1]));
fprintf(stderr, "Error pipe creation failed: %s\n", os_error_message);
return errno;
}
result = TEMP_FAILURE_RETRY(pipe(exec_control));
if (result < 0) {
SetChildOsErrorMessage(os_error_message, os_error_message_len);
TEMP_FAILURE_RETRY(close(read_in[0]));
TEMP_FAILURE_RETRY(close(read_in[1]));
TEMP_FAILURE_RETRY(close(read_err[0]));
TEMP_FAILURE_RETRY(close(read_err[1]));
TEMP_FAILURE_RETRY(close(write_out[0]));
TEMP_FAILURE_RETRY(close(write_out[1]));
fprintf(stderr, "Error pipe creation failed: %s\n", os_error_message);
return errno;
}
// Set close on exec on the write file descriptor of the exec control pipe.
result = TEMP_FAILURE_RETRY(
fcntl(exec_control[1],
F_SETFD,
TEMP_FAILURE_RETRY(fcntl(exec_control[1], F_GETFD)) | FD_CLOEXEC));
if (result < 0) {
SetChildOsErrorMessage(os_error_message, os_error_message_len);
TEMP_FAILURE_RETRY(close(read_in[0]));
TEMP_FAILURE_RETRY(close(read_in[1]));
TEMP_FAILURE_RETRY(close(read_err[0]));
TEMP_FAILURE_RETRY(close(read_err[1]));
TEMP_FAILURE_RETRY(close(write_out[0]));
TEMP_FAILURE_RETRY(close(write_out[1]));
TEMP_FAILURE_RETRY(close(exec_control[0]));
TEMP_FAILURE_RETRY(close(exec_control[1]));
fprintf(stderr, "fcntl failed: %s\n", os_error_message);
return errno;
}
char** program_arguments = new char*[arguments_length + 2];
program_arguments[0] = const_cast<char*>(path);
for (int i = 0; i < arguments_length; i++) {
program_arguments[i + 1] = arguments[i];
}
program_arguments[arguments_length + 1] = NULL;
char** program_environment = NULL;
if (environment != NULL) {
program_environment = new char*[environment_length + 1];
for (int i = 0; i < environment_length; i++) {
program_environment[i] = environment[i];
}
program_environment[environment_length] = NULL;
}
struct sigaction act;
bzero(&act, sizeof(act));
act.sa_sigaction = SigChldHandler;
act.sa_flags = SA_NOCLDSTOP | SA_SIGINFO;
if (sigaction(SIGCHLD, &act, 0) != 0) {
perror("Process start: setting signal handler failed");
}
pid = TEMP_FAILURE_RETRY(fork());
if (pid < 0) {
SetChildOsErrorMessage(os_error_message, os_error_message_len);
delete[] program_arguments;
TEMP_FAILURE_RETRY(close(read_in[0]));
TEMP_FAILURE_RETRY(close(read_in[1]));
TEMP_FAILURE_RETRY(close(read_err[0]));
TEMP_FAILURE_RETRY(close(read_err[1]));
TEMP_FAILURE_RETRY(close(write_out[0]));
TEMP_FAILURE_RETRY(close(write_out[1]));
TEMP_FAILURE_RETRY(close(exec_control[0]));
TEMP_FAILURE_RETRY(close(exec_control[1]));
return errno;
} else if (pid == 0) {
// Wait for parent process before setting up the child process.
char msg;
int bytes_read = FDUtils::ReadFromBlocking(read_in[0], &msg, sizeof(msg));
if (bytes_read != sizeof(msg)) {
perror("Failed receiving notification message");
exit(1);
}
TEMP_FAILURE_RETRY(close(write_out[1]));
TEMP_FAILURE_RETRY(close(read_in[0]));
TEMP_FAILURE_RETRY(close(read_err[0]));
TEMP_FAILURE_RETRY(close(exec_control[0]));
if (TEMP_FAILURE_RETRY(dup2(write_out[0], STDIN_FILENO)) == -1) {
ReportChildError(exec_control[1]);
}
TEMP_FAILURE_RETRY(close(write_out[0]));
if (TEMP_FAILURE_RETRY(dup2(read_in[1], STDOUT_FILENO)) == -1) {
ReportChildError(exec_control[1]);
}
TEMP_FAILURE_RETRY(close(read_in[1]));
if (TEMP_FAILURE_RETRY(dup2(read_err[1], STDERR_FILENO)) == -1) {
ReportChildError(exec_control[1]);
}
TEMP_FAILURE_RETRY(close(read_err[1]));
if (working_directory != NULL &&
TEMP_FAILURE_RETRY(chdir(working_directory)) == -1) {
ReportChildError(exec_control[1]);
}
if (environment != NULL) {
TEMP_FAILURE_RETRY(
execve(path,
const_cast<char* const*>(program_arguments),
program_environment));
} else {
TEMP_FAILURE_RETRY(
execvp(path, const_cast<char* const*>(program_arguments)));
}
ReportChildError(exec_control[1]);
}
// The arguments and environment for the spawned process are not needed
// any longer.
delete[] program_arguments;
delete[] program_environment;
int event_fds[2];
result = TEMP_FAILURE_RETRY(pipe(event_fds));
if (result < 0) {
SetChildOsErrorMessage(os_error_message, os_error_message_len);
TEMP_FAILURE_RETRY(close(read_in[0]));
TEMP_FAILURE_RETRY(close(read_in[1]));
TEMP_FAILURE_RETRY(close(read_err[0]));
TEMP_FAILURE_RETRY(close(read_err[1]));
TEMP_FAILURE_RETRY(close(write_out[0]));
TEMP_FAILURE_RETRY(close(write_out[1]));
fprintf(stderr, "Error pipe creation failed: %s\n", os_error_message);
return errno;
}
ProcessInfoList::AddProcess(pid, event_fds[1]);
*exit_event = event_fds[0];
FDUtils::SetNonBlocking(event_fds[0]);
// Notify child process to start.
char msg = '1';
result = FDUtils::WriteToBlocking(read_in[1], &msg, sizeof(msg));
if (result != sizeof(msg)) {
perror("Failed sending notification message");
}
// Read exec result from child. If no data is returned the exec was
// successful and the exec call closed the pipe. Otherwise the errno
// is written to the pipe.
TEMP_FAILURE_RETRY(close(exec_control[1]));
int child_errno;
int bytes_read = -1;
ASSERT(sizeof(child_errno) == sizeof(errno));
bytes_read =
FDUtils::ReadFromBlocking(
exec_control[0], &child_errno, sizeof(child_errno));
if (bytes_read == sizeof(child_errno)) {
bytes_read = FDUtils::ReadFromBlocking(exec_control[0],
os_error_message,
os_error_message_len);
os_error_message[os_error_message_len - 1] = '\0';
}
TEMP_FAILURE_RETRY(close(exec_control[0]));
// Return error code if any failures.
if (bytes_read != 0) {
TEMP_FAILURE_RETRY(close(read_in[0]));
TEMP_FAILURE_RETRY(close(read_in[1]));
TEMP_FAILURE_RETRY(close(read_err[0]));
TEMP_FAILURE_RETRY(close(read_err[1]));
TEMP_FAILURE_RETRY(close(write_out[0]));
TEMP_FAILURE_RETRY(close(write_out[1]));
if (bytes_read == -1) {
return errno; // Read failed.
} else {
return child_errno; // Exec failed.
}
}
FDUtils::SetNonBlocking(read_in[0]);
*in = read_in[0];
TEMP_FAILURE_RETRY(close(read_in[1]));
FDUtils::SetNonBlocking(write_out[1]);
*out = write_out[1];
TEMP_FAILURE_RETRY(close(write_out[0]));
FDUtils::SetNonBlocking(read_err[0]);
*err = read_err[0];
TEMP_FAILURE_RETRY(close(read_err[1]));
*id = pid;
return 0;
}
bool Process::Kill(intptr_t id, int signal) {
int result = TEMP_FAILURE_RETRY(kill(id, signal));
if (result == -1) {
return false;
}
return true;
}
void Process::TerminateExitCodeHandler() {
ExitCodeHandler::TerminateExitCodeThread();
}
intptr_t Process::CurrentProcessId() {
return static_cast<intptr_t>(getpid());
}

View file

@ -11,7 +11,9 @@
#include "platform/globals.h"
#include "platform/thread.h"
// Declare the OS-specific types ahead of defining the generic class.
#if defined(TARGET_OS_LINUX)
#if defined(TARGET_OS_ANDROID)
#include "bin/socket_android.h"
#elif defined(TARGET_OS_LINUX)
#include "bin/socket_linux.h"
#elif defined(TARGET_OS_MACOS)
#include "bin/socket_macos.h"

View file

@ -0,0 +1,241 @@
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "bin/fdutils.h"
#include "bin/socket.h"
bool Socket::Initialize() {
// Nothing to do on Android.
return true;
}
intptr_t Socket::CreateConnect(const char* host, const intptr_t port) {
intptr_t fd;
struct hostent* server;
struct sockaddr_in server_address;
fd = TEMP_FAILURE_RETRY(socket(AF_INET, SOCK_STREAM, 0));
if (fd < 0) {
fprintf(stderr, "Error CreateConnect: %s\n", strerror(errno));
return -1;
}
FDUtils::SetNonBlocking(fd);
server = gethostbyname(host);
if (server == NULL) {
TEMP_FAILURE_RETRY(close(fd));
fprintf(stderr, "Error CreateConnect: %s\n", strerror(errno));
return -1;
}
server_address.sin_family = AF_INET;
server_address.sin_port = htons(port);
bcopy(server->h_addr, &server_address.sin_addr.s_addr, server->h_length);
memset(&server_address.sin_zero, 0, sizeof(server_address.sin_zero));
intptr_t result = TEMP_FAILURE_RETRY(
connect(fd,
reinterpret_cast<struct sockaddr *>(&server_address),
sizeof(server_address)));
if (result == 0 || errno == EINPROGRESS) {
return fd;
}
return -1;
}
intptr_t Socket::Available(intptr_t fd) {
return FDUtils::AvailableBytes(fd);
}
int Socket::Read(intptr_t fd, void* buffer, intptr_t num_bytes) {
ASSERT(fd >= 0);
ssize_t read_bytes = TEMP_FAILURE_RETRY(read(fd, buffer, num_bytes));
ASSERT(EAGAIN == EWOULDBLOCK);
if (read_bytes == -1 && errno == EWOULDBLOCK) {
// If the read would block we need to retry and therefore return 0
// as the number of bytes written.
read_bytes = 0;
}
return read_bytes;
}
int Socket::Write(intptr_t fd, const void* buffer, intptr_t num_bytes) {
ASSERT(fd >= 0);
ssize_t written_bytes = TEMP_FAILURE_RETRY(write(fd, buffer, num_bytes));
ASSERT(EAGAIN == EWOULDBLOCK);
if (written_bytes == -1 && errno == EWOULDBLOCK) {
// If the would block we need to retry and therefore return 0 as
// the number of bytes written.
written_bytes = 0;
}
return written_bytes;
}
intptr_t Socket::GetPort(intptr_t fd) {
ASSERT(fd >= 0);
struct sockaddr_in socket_address;
socklen_t size = sizeof(socket_address);
if (TEMP_FAILURE_RETRY(
getsockname(fd,
reinterpret_cast<struct sockaddr *>(&socket_address),
&size))) {
fprintf(stderr, "Error getsockname: %s\n", strerror(errno));
return 0;
}
return ntohs(socket_address.sin_port);
}
bool Socket::GetRemotePeer(intptr_t fd, char *host, intptr_t *port) {
ASSERT(fd >= 0);
struct sockaddr_in socket_address;
socklen_t size = sizeof(socket_address);
if (TEMP_FAILURE_RETRY(
getpeername(fd,
reinterpret_cast<struct sockaddr *>(&socket_address),
&size))) {
fprintf(stderr, "Error getpeername: %s\n", strerror(errno));
return false;
}
if (inet_ntop(socket_address.sin_family,
reinterpret_cast<const void *>(&socket_address.sin_addr),
host,
INET_ADDRSTRLEN) == NULL) {
fprintf(stderr, "Error inet_ntop: %s\n", strerror(errno));
return false;
}
*port = ntohs(socket_address.sin_port);
return true;
}
void Socket::GetError(intptr_t fd, OSError* os_error) {
int errorNumber;
socklen_t len = sizeof(errorNumber);
getsockopt(fd,
SOL_SOCKET,
SO_ERROR,
reinterpret_cast<void*>(&errorNumber),
&len);
os_error->SetCodeAndMessage(OSError::kSystem, errorNumber);
}
intptr_t Socket::GetStdioHandle(int num) {
return static_cast<intptr_t>(num);
}
const char* Socket::LookupIPv4Address(char* host, OSError** os_error) {
// Perform a name lookup for an IPv4 address.
struct addrinfo hints;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
struct addrinfo* info = NULL;
int status = getaddrinfo(host, 0, &hints, &info);
if (status != 0) {
ASSERT(*os_error == NULL);
*os_error = new OSError(status,
gai_strerror(status),
OSError::kGetAddressInfo);
return NULL;
}
// Convert the address into IPv4 dotted decimal notation.
char* buffer = reinterpret_cast<char*>(malloc(INET_ADDRSTRLEN));
sockaddr_in *sockaddr = reinterpret_cast<sockaddr_in *>(info->ai_addr);
const char* result = inet_ntop(AF_INET,
reinterpret_cast<void *>(&sockaddr->sin_addr),
buffer,
INET_ADDRSTRLEN);
if (result == NULL) {
free(buffer);
return NULL;
}
ASSERT(result == buffer);
return buffer;
}
intptr_t ServerSocket::CreateBindListen(const char* host,
intptr_t port,
intptr_t backlog) {
intptr_t fd;
struct sockaddr_in server_address;
fd = TEMP_FAILURE_RETRY(socket(AF_INET, SOCK_STREAM, 0));
if (fd < 0) {
fprintf(stderr, "Error CreateBind: %s\n", strerror(errno));
return -1;
}
int optval = 1;
TEMP_FAILURE_RETRY(
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)));
server_address.sin_family = AF_INET;
server_address.sin_port = htons(port);
server_address.sin_addr.s_addr = inet_addr(host);
memset(&server_address.sin_zero, 0, sizeof(server_address.sin_zero));
if (TEMP_FAILURE_RETRY(
bind(fd,
reinterpret_cast<struct sockaddr *>(&server_address),
sizeof(server_address))) < 0) {
TEMP_FAILURE_RETRY(close(fd));
fprintf(stderr, "Error Bind: %s\n", strerror(errno));
return -1;
}
if (TEMP_FAILURE_RETRY(listen(fd, backlog)) != 0) {
fprintf(stderr, "Error Listen: %s\n", strerror(errno));
return -1;
}
FDUtils::SetNonBlocking(fd);
return fd;
}
static bool IsTemporaryAcceptError(int error) {
// On Android a number of protocol errors should be treated as EAGAIN.
// These are the ones for TCP/IP.
return (error == EAGAIN) || (error == ENETDOWN) || (error == EPROTO) ||
(error == ENOPROTOOPT) || (error == EHOSTDOWN) || (error == ENONET) ||
(error == EHOSTUNREACH) || (error == EOPNOTSUPP) ||
(error == ENETUNREACH);
}
intptr_t ServerSocket::Accept(intptr_t fd) {
intptr_t socket;
struct sockaddr clientaddr;
socklen_t addrlen = sizeof(clientaddr);
socket = TEMP_FAILURE_RETRY(accept(fd, &clientaddr, &addrlen));
if (socket == -1) {
if (IsTemporaryAcceptError(errno)) {
// We need to signal to the caller that this is actually not an
// error. We got woken up from the poll on the listening socket,
// but there is no connection ready to be accepted.
ASSERT(kTemporaryFailure != -1);
socket = kTemporaryFailure;
}
} else {
FDUtils::SetNonBlocking(socket);
}
return socket;
}

View file

@ -0,0 +1,12 @@
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
#ifndef BIN_SOCKET_ANDROID_H_
#define BIN_SOCKET_ANDROID_H_
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/socket.h>
#endif // BIN_SOCKET_ANDROID_H_

View file

@ -0,0 +1,28 @@
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
#include <errno.h>
#include <netdb.h>
#include "bin/utils.h"
#include "platform/assert.h"
OSError::OSError() : sub_system_(kSystem), code_(0), message_(NULL) {
set_sub_system(kSystem);
set_code(errno);
SetMessage(strerror(errno));
}
void OSError::SetCodeAndMessage(SubSystem sub_system, int code) {
set_sub_system(sub_system);
set_code(code);
if (sub_system == kSystem) {
SetMessage(strerror(code));
} else if (sub_system == kGetAddressInfo) {
SetMessage(gai_strerror(code));
} else {
UNREACHABLE();
}
}

View file

@ -52,7 +52,9 @@
// for more information on predefined macros:
// - http://msdn.microsoft.com/en-us/library/b0084kay.aspx
// - with gcc, run: "echo | gcc -E -dM -"
#if defined(__linux__) || defined(__FreeBSD__)
#if defined(__ANDROID__)
#define TARGET_OS_ANDROID
#elif defined(__linux__) || defined(__FreeBSD__)
#define TARGET_OS_LINUX 1
#elif defined(__APPLE__)
#define TARGET_OS_MACOS 1
@ -325,12 +327,13 @@ inline D bit_copy(const S& source) {
// to be used in error quite often. To avoid problems we disallow the direct
// use of memcpy here.
//
// On Windows the basic libraries use memcpy and therefore compilation will
// fail if memcpy is overwritten even if user code does not use memcpy.
// On Android and Windows the basic libraries use memcpy and therefore
// compilation will fail if memcpy is overwritten even if user code does not
// use memcpy.
#if defined(memcpy)
#undef memcpy
#endif
#if !defined(TARGET_OS_WINDOWS)
#if !( defined(TARGET_OS_ANDROID) || defined(TARGET_OS_WINDOWS) )
#define memcpy "Please use memmove instead of memcpy."
#endif

View file

@ -8,6 +8,7 @@
'assert.cc',
'hashmap.cc',
'json.cc',
'thread_android.cc',
'thread_linux.cc',
'thread_macos.cc',
'thread_win.cc',

View file

@ -8,7 +8,9 @@
#include "platform/globals.h"
// Declare the OS-specific types ahead of defining the generic classes.
#if defined(TARGET_OS_LINUX)
#if defined(TARGET_OS_ANDROID)
#include "platform/thread_android.h"
#elif defined(TARGET_OS_LINUX)
#include "platform/thread_linux.h"
#elif defined(TARGET_OS_MACOS)
#include "platform/thread_macos.h"

View file

@ -0,0 +1,279 @@
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
#include "platform/thread.h"
#include <errno.h>
#include <sys/time.h>
#include "platform/assert.h"
namespace dart {
#define VALIDATE_PTHREAD_RESULT(result) \
if (result != 0) { \
FATAL2("pthread error: %d (%s)", result, strerror(result)); \
}
#ifdef DEBUG
#define RETURN_ON_PTHREAD_FAILURE(result) \
if (result != 0) { \
fprintf(stderr, "%s:%d: pthread error: %d (%s)\n", \
__FILE__, __LINE__, result, strerror(result)); \
return result; \
}
#else
#define RETURN_ON_PTHREAD_FAILURE(result) \
if (result != 0) return result;
#endif
static void ComputeTimeSpec(struct timespec* ts, int64_t millis) {
int64_t secs = millis / kMillisecondsPerSecond;
int64_t nanos =
(millis - (secs * kMillisecondsPerSecond)) * kNanosecondsPerMillisecond;
int result = clock_gettime(CLOCK_MONOTONIC, ts);
ASSERT(result == 0);
ts->tv_sec += secs;
ts->tv_nsec += nanos;
if (ts->tv_nsec >= kNanosecondsPerSecond) {
ts->tv_sec += 1;
ts->tv_nsec -= kNanosecondsPerSecond;
}
}
class ThreadStartData {
public:
ThreadStartData(Thread::ThreadStartFunction function,
uword parameter)
: function_(function), parameter_(parameter) {}
Thread::ThreadStartFunction function() const { return function_; }
uword parameter() const { return parameter_; }
private:
Thread::ThreadStartFunction function_;
uword parameter_;
DISALLOW_COPY_AND_ASSIGN(ThreadStartData);
};
// Dispatch to the thread start function provided by the caller. This trampoline
// is used to ensure that the thread is properly destroyed if the thread just
// exits.
static void* ThreadStart(void* data_ptr) {
ThreadStartData* data = reinterpret_cast<ThreadStartData*>(data_ptr);
Thread::ThreadStartFunction function = data->function();
uword parameter = data->parameter();
delete data;
// Call the supplied thread start function handing it its parameters.
function(parameter);
return NULL;
}
int Thread::Start(ThreadStartFunction function, uword parameter) {
pthread_attr_t attr;
int result = pthread_attr_init(&attr);
RETURN_ON_PTHREAD_FAILURE(result);
result = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
RETURN_ON_PTHREAD_FAILURE(result);
result = pthread_attr_setstacksize(&attr, Thread::GetMaxStackSize());
RETURN_ON_PTHREAD_FAILURE(result);
ThreadStartData* data = new ThreadStartData(function, parameter);
pthread_t tid;
result = pthread_create(&tid, &attr, ThreadStart, data);
RETURN_ON_PTHREAD_FAILURE(result);
result = pthread_attr_destroy(&attr);
RETURN_ON_PTHREAD_FAILURE(result);
return 0;
}
ThreadLocalKey Thread::kUnsetThreadLocalKey = static_cast<pthread_key_t>(-1);
ThreadLocalKey Thread::CreateThreadLocal() {
pthread_key_t key = kUnsetThreadLocalKey;
int result = pthread_key_create(&key, NULL);
VALIDATE_PTHREAD_RESULT(result);
ASSERT(key != kUnsetThreadLocalKey);
return key;
}
void Thread::DeleteThreadLocal(ThreadLocalKey key) {
ASSERT(key != kUnsetThreadLocalKey);
int result = pthread_key_delete(key);
VALIDATE_PTHREAD_RESULT(result);
}
void Thread::SetThreadLocal(ThreadLocalKey key, uword value) {
ASSERT(key != kUnsetThreadLocalKey);
int result = pthread_setspecific(key, reinterpret_cast<void*>(value));
VALIDATE_PTHREAD_RESULT(result);
}
intptr_t Thread::GetMaxStackSize() {
const int kStackSize = (512 * KB);
return kStackSize;
}
Mutex::Mutex() {
pthread_mutexattr_t attr;
int result = pthread_mutexattr_init(&attr);
VALIDATE_PTHREAD_RESULT(result);
#if defined(DEBUG)
result = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);
VALIDATE_PTHREAD_RESULT(result);
#endif // defined(DEBUG)
result = pthread_mutex_init(data_.mutex(), &attr);
// Verify that creating a pthread_mutex succeeded.
VALIDATE_PTHREAD_RESULT(result);
result = pthread_mutexattr_destroy(&attr);
VALIDATE_PTHREAD_RESULT(result);
}
Mutex::~Mutex() {
int result = pthread_mutex_destroy(data_.mutex());
// Verify that the pthread_mutex was destroyed.
VALIDATE_PTHREAD_RESULT(result);
}
void Mutex::Lock() {
int result = pthread_mutex_lock(data_.mutex());
// Specifically check for dead lock to help debugging.
ASSERT(result != EDEADLK);
ASSERT(result == 0); // Verify no other errors.
// TODO(iposva): Do we need to track lock owners?
}
bool Mutex::TryLock() {
int result = pthread_mutex_trylock(data_.mutex());
// Return false if the lock is busy and locking failed.
if (result == EBUSY) {
return false;
}
ASSERT(result == 0); // Verify no other errors.
// TODO(iposva): Do we need to track lock owners?
return true;
}
void Mutex::Unlock() {
// TODO(iposva): Do we need to track lock owners?
int result = pthread_mutex_unlock(data_.mutex());
// Specifically check for wrong thread unlocking to aid debugging.
ASSERT(result != EPERM);
ASSERT(result == 0); // Verify no other errors.
}
Monitor::Monitor() {
pthread_mutexattr_t mutex_attr;
int result = pthread_mutexattr_init(&mutex_attr);
VALIDATE_PTHREAD_RESULT(result);
#if defined(DEBUG)
result = pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_ERRORCHECK);
VALIDATE_PTHREAD_RESULT(result);
#endif // defined(DEBUG)
result = pthread_mutex_init(data_.mutex(), &mutex_attr);
VALIDATE_PTHREAD_RESULT(result);
result = pthread_mutexattr_destroy(&mutex_attr);
VALIDATE_PTHREAD_RESULT(result);
pthread_condattr_t cond_attr;
result = pthread_condattr_init(&cond_attr);
VALIDATE_PTHREAD_RESULT(result);
result = pthread_cond_init(data_.cond(), &cond_attr);
VALIDATE_PTHREAD_RESULT(result);
result = pthread_condattr_destroy(&cond_attr);
VALIDATE_PTHREAD_RESULT(result);
}
Monitor::~Monitor() {
int result = pthread_mutex_destroy(data_.mutex());
VALIDATE_PTHREAD_RESULT(result);
result = pthread_cond_destroy(data_.cond());
VALIDATE_PTHREAD_RESULT(result);
}
void Monitor::Enter() {
int result = pthread_mutex_lock(data_.mutex());
VALIDATE_PTHREAD_RESULT(result);
// TODO(iposva): Do we need to track lock owners?
}
void Monitor::Exit() {
// TODO(iposva): Do we need to track lock owners?
int result = pthread_mutex_unlock(data_.mutex());
VALIDATE_PTHREAD_RESULT(result);
}
Monitor::WaitResult Monitor::Wait(int64_t millis) {
// TODO(iposva): Do we need to track lock owners?
Monitor::WaitResult retval = kNotified;
if (millis == 0) {
// Wait forever.
int result = pthread_cond_wait(data_.cond(), data_.mutex());
VALIDATE_PTHREAD_RESULT(result);
} else {
struct timespec ts;
ComputeTimeSpec(&ts, millis);
int result = pthread_cond_timedwait_monotonic(
data_.cond(), data_.mutex(), &ts);
ASSERT((result == 0) || (result == ETIMEDOUT));
if (result == ETIMEDOUT) {
retval = kTimedOut;
}
}
return retval;
}
void Monitor::Notify() {
// TODO(iposva): Do we need to track lock owners?
int result = pthread_cond_signal(data_.cond());
VALIDATE_PTHREAD_RESULT(result);
}
void Monitor::NotifyAll() {
// TODO(iposva): Do we need to track lock owners?
int result = pthread_cond_broadcast(data_.cond());
VALIDATE_PTHREAD_RESULT(result);
}
} // namespace dart

View file

@ -0,0 +1,74 @@
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
#ifndef PLATFORM_THREAD_ANDROID_H_
#define PLATFORM_THREAD_ANDROID_H_
#if !defined(PLATFORM_THREAD_H_)
#error Do not include thread_android.h directly; use thread.h instead.
#endif
#include <pthread.h>
#include "platform/assert.h"
#include "platform/globals.h"
namespace dart {
typedef pthread_key_t ThreadLocalKey;
class ThreadInlineImpl {
private:
ThreadInlineImpl() {}
~ThreadInlineImpl() {}
static uword GetThreadLocal(ThreadLocalKey key) {
static ThreadLocalKey kUnsetThreadLocalKey = static_cast<pthread_key_t>(-1);
ASSERT(key != kUnsetThreadLocalKey);
return reinterpret_cast<uword>(pthread_getspecific(key));
}
friend class Thread;
DISALLOW_ALLOCATION();
DISALLOW_COPY_AND_ASSIGN(ThreadInlineImpl);
};
class MutexData {
private:
MutexData() {}
~MutexData() {}
pthread_mutex_t* mutex() { return &mutex_; }
pthread_mutex_t mutex_;
friend class Mutex;
DISALLOW_ALLOCATION();
DISALLOW_COPY_AND_ASSIGN(MutexData);
};
class MonitorData {
private:
MonitorData() {}
~MonitorData() {}
pthread_mutex_t* mutex() { return &mutex_; }
pthread_cond_t* cond() { return &cond_; }
pthread_mutex_t mutex_;
pthread_cond_t cond_;
friend class Monitor;
DISALLOW_ALLOCATION();
DISALLOW_COPY_AND_ASSIGN(MonitorData);
};
} // namespace dart
#endif // PLATFORM_THREAD_ANDROID_H_

View file

@ -172,7 +172,9 @@ class Utils {
} // namespace dart
#if defined(TARGET_OS_LINUX)
#if defined(TARGET_OS_ANDROID)
#include "platform/utils_android.h"
#elif defined(TARGET_OS_LINUX)
#include "platform/utils_linux.h"
#elif defined(TARGET_OS_MACOS)
#include "platform/utils_macos.h"

View file

@ -0,0 +1,22 @@
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
#ifndef PLATFORM_UTILS_ANDROID_H_
#define PLATFORM_UTILS_ANDROID_H_
namespace dart {
inline int Utils::CountTrailingZeros(uword x) {
#if defined(ARCH_IS_32_BIT)
return __builtin_ctzl(x);
#elif defined(ARCH_IS_64_BIT)
return __builtin_ctzll(x);
#else
#error Architecture is not 32-bit or 64-bit.
#endif
};
} // namespace dart
#endif // PLATFORM_UTILS_ANDROID_H_

View file

@ -0,0 +1,51 @@
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
#include "vm/debuginfo.h"
namespace dart {
DebugInfo::DebugInfo() {
handle_ = NULL;
}
DebugInfo::~DebugInfo() {
}
void DebugInfo::AddCode(uword pc, intptr_t size) {
// Nothing to do as there is no support for this on Android.
}
void DebugInfo::AddCodeRegion(const char* name, uword pc, intptr_t size) {
// Nothing to do as there is no support for this on Android.
}
bool DebugInfo::WriteToMemory(ByteBuffer* region) {
// Nothing to do as there is no support for this on Android.
return false;
}
DebugInfo* DebugInfo::NewGenerator() {
return new DebugInfo();
}
void DebugInfo::RegisterSection(const char* name,
uword entry_point,
intptr_t size) {
// Nothing to do as there is no support for this on Android.
}
void DebugInfo::UnregisterAllSections() {
// Nothing to do as there is no support for this on Android.
}
} // namespace dart

View file

@ -0,0 +1,77 @@
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include "vm/gdbjit_android.h"
extern "C" {
typedef enum {
JIT_NOACTION = 0,
JIT_REGISTER_FN,
JIT_UNREGISTER_FN
} jit_actions_t;
struct jit_code_entry {
struct jit_code_entry *next_entry;
struct jit_code_entry *prev_entry;
const char *symfile_addr;
uint64_t symfile_size;
};
struct jit_descriptor {
uint32_t version;
/* This type should be jit_actions_t, but we use uint32_t
to be explicit about the bitwidth. */
uint32_t action_flag;
struct jit_code_entry *relevant_entry;
struct jit_code_entry *first_entry;
};
/* GDB puts a breakpoint in this function. */
void __attribute__((noinline)) __jit_debug_register_code() { }
/* Make sure to specify the version statically, because the
debugger may check the version before we can set it. */
struct jit_descriptor __jit_debug_descriptor = { 1, 0, 0, 0 };
static struct jit_code_entry* first_dynamic_region = NULL;
static struct jit_code_entry* last_dynamic_region = NULL;
void addDynamicSection(const char* symfile_addr, uint64_t symfile_size) {
jit_code_entry* new_entry = reinterpret_cast<jit_code_entry*>(
malloc(sizeof(jit_code_entry)));
if (new_entry != NULL) {
new_entry->symfile_addr = symfile_addr;
new_entry->symfile_size = symfile_size;
new_entry->next_entry = NULL;
new_entry->prev_entry = last_dynamic_region;
if (first_dynamic_region == NULL) {
first_dynamic_region = new_entry;
} else {
last_dynamic_region->next_entry = new_entry;
}
last_dynamic_region = new_entry;
}
__jit_debug_descriptor.action_flag = JIT_REGISTER_FN;
__jit_debug_descriptor.relevant_entry = new_entry;
__jit_debug_descriptor.first_entry = first_dynamic_region;
__jit_debug_register_code();
}
void deleteDynamicSections() {
struct jit_code_entry* iterator = last_dynamic_region;
while (iterator != NULL) {
__jit_debug_descriptor.action_flag = JIT_UNREGISTER_FN;
__jit_debug_descriptor.relevant_entry = iterator;
__jit_debug_descriptor.first_entry = first_dynamic_region;
__jit_debug_register_code();
iterator = iterator->prev_entry;
}
first_dynamic_region = NULL;
last_dynamic_region = NULL;
}
};

View file

@ -0,0 +1,13 @@
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
#ifndef VM_GDBJIT_ANDROID_H_
#define VM_GDBJIT_ANDROID_H_
extern "C" {
void addDynamicSection(const char* symfile_addr, uint64_t symfile_size);
void deleteDynamicSections();
};
#endif // VM_GDBJIT_ANDROID_H_

215
runtime/vm/os_android.cc Normal file
View file

@ -0,0 +1,215 @@
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
#include "vm/os.h"
#include <errno.h>
#include <limits.h>
#include <time.h>
#include <sys/resource.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include "platform/utils.h"
#include "vm/isolate.h"
namespace dart {
static bool LocalTime(int64_t seconds_since_epoch, tm* tm_result) {
time_t seconds = static_cast<time_t>(seconds_since_epoch);
if (seconds != seconds_since_epoch) return false;
struct tm* error_code = localtime_r(&seconds, tm_result);
return error_code != NULL;
}
const char* OS::GetTimeZoneName(int64_t seconds_since_epoch) {
tm decomposed;
bool succeeded = LocalTime(seconds_since_epoch, &decomposed);
ASSERT(succeeded);
return decomposed.tm_zone;
}
int OS::GetTimeZoneOffsetInSeconds(int64_t seconds_since_epoch) {
tm decomposed;
bool succeeded = LocalTime(seconds_since_epoch, &decomposed);
ASSERT(succeeded);
// Even if the offset was 24 hours it would still easily fit into 32 bits.
return static_cast<int>(decomposed.tm_gmtoff);
}
int OS::GetLocalTimeZoneAdjustmentInSeconds() {
// TODO(floitsch): avoid excessive calls to tzset?
tzset();
// Even if the offset was 24 hours it would still easily fit into 32 bits.
// Note that Unix and Dart disagree on the sign.
return static_cast<int>(-timezone);
}
int64_t OS::GetCurrentTimeMillis() {
return GetCurrentTimeMicros() / 1000;
}
int64_t OS::GetCurrentTimeMicros() {
// gettimeofday has microsecond resolution.
struct timeval tv;
if (gettimeofday(&tv, NULL) < 0) {
UNREACHABLE();
return 0;
}
return (static_cast<int64_t>(tv.tv_sec) * 1000000) + tv.tv_usec;
}
// TODO(5411554): May need to hoist these architecture dependent code
// into a architecture specific file e.g: os_ia32_linux.cc
word OS::ActivationFrameAlignment() {
#if defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64)
const int kMinimumAlignment = 16;
#elif defined(TARGET_ARCH_ARM)
const int kMinimumAlignment = 8;
#else
#error Unsupported architecture.
#endif
word alignment = kMinimumAlignment;
// TODO(5411554): Allow overriding default stack alignment for
// testing purposes.
// Flags::DebugIsInt("stackalign", &alignment);
ASSERT(Utils::IsPowerOfTwo(alignment));
ASSERT(alignment >= kMinimumAlignment);
return alignment;
}
word OS::PreferredCodeAlignment() {
#if defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64)
const int kMinimumAlignment = 16;
#elif defined(TARGET_ARCH_ARM)
const int kMinimumAlignment = 16;
#else
#error Unsupported architecture.
#endif
word alignment = kMinimumAlignment;
// TODO(5411554): Allow overriding default code alignment for
// testing purposes.
// Flags::DebugIsInt("codealign", &alignment);
ASSERT(Utils::IsPowerOfTwo(alignment));
ASSERT(alignment >= kMinimumAlignment);
ASSERT(alignment <= OS::kMaxPreferredCodeAlignment);
return alignment;
}
uword OS::GetStackSizeLimit() {
struct rlimit stack_limit;
int retval = getrlimit(RLIMIT_STACK, &stack_limit);
ASSERT(retval == 0);
if (stack_limit.rlim_cur > INT_MAX) {
retval = INT_MAX;
} else {
retval = stack_limit.rlim_cur;
}
return retval;
}
int OS::NumberOfAvailableProcessors() {
return sysconf(_SC_NPROCESSORS_ONLN);
}
void OS::Sleep(int64_t millis) {
// TODO(5411554): For now just use usleep we may have to revisit this.
usleep(millis * 1000);
}
void OS::Print(const char* format, ...) {
va_list args;
va_start(args, format);
VFPrint(stdout, format, args);
va_end(args);
}
void OS::VFPrint(FILE* stream, const char* format, va_list args) {
vfprintf(stream, format, args);
fflush(stream);
}
int OS::SNPrint(char* str, size_t size, const char* format, ...) {
va_list args;
va_start(args, format);
int retval = VSNPrint(str, size, format, args);
va_end(args);
return retval;
}
int OS::VSNPrint(char* str, size_t size, const char* format, va_list args) {
int retval = vsnprintf(str, size, format, args);
if (retval < 0) {
FATAL1("Fatal error in OS::VSNPrint with format '%s'", format);
}
return retval;
}
bool OS::StringToInt64(const char* str, int64_t* value) {
ASSERT(str != NULL && strlen(str) > 0 && value != NULL);
int32_t base = 10;
char* endptr;
int i = 0;
if (str[0] == '-') {
i = 1;
}
if ((str[i] == '0') &&
(str[i + 1] == 'x' || str[i + 1] == 'X') &&
(str[i + 2] != '\0')) {
base = 16;
}
errno = 0;
*value = strtoll(str, &endptr, base);
return ((errno == 0) && (endptr != str) && (*endptr == 0));
}
void OS::PrintErr(const char* format, ...) {
va_list args;
va_start(args, format);
VFPrint(stderr, format, args);
va_end(args);
}
void OS::InitOnce() {
// TODO(5411554): For now we check that initonce is called only once,
// Once there is more formal mechanism to call InitOnce we can move
// this check there.
static bool init_once_called = false;
ASSERT(init_once_called == false);
init_once_called = true;
}
void OS::Shutdown() {
}
void OS::Abort() {
abort();
}
void OS::Exit(int code) {
exit(code);
}
} // namespace dart

View file

@ -0,0 +1,98 @@
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
#include "vm/virtual_memory.h"
#include <sys/mman.h>
#include <unistd.h>
#include "platform/assert.h"
#include "platform/utils.h"
namespace dart {
// standard MAP_FAILED causes "error: use of old-style cast" as it
// defines MAP_FAILED as ((void *) -1)
#undef MAP_FAILED
#define MAP_FAILED reinterpret_cast<void*>(-1)
uword VirtualMemory::page_size_ = 0;
void VirtualMemory::InitOnce() {
page_size_ = getpagesize();
}
VirtualMemory* VirtualMemory::Reserve(intptr_t size) {
// TODO(4408): use ashmem instead of anonymous memory.
void* address = mmap(NULL, size, PROT_NONE,
MAP_PRIVATE | MAP_ANON | MAP_NORESERVE,
-1, 0);
if (address == MAP_FAILED) {
return NULL;
}
MemoryRegion region(address, size);
return new VirtualMemory(region, NULL);
}
static void unmap(void* address, intptr_t size) {
if (size == 0) {
return;
}
if (munmap(address, size) != 0) {
FATAL("munmap failed\n");
}
}
VirtualMemory::~VirtualMemory() {
unmap(address(), size());
}
void VirtualMemory::FreeSubSegment(void* address, intptr_t size) {
unmap(address, size);
}
bool VirtualMemory::Commit(uword addr, intptr_t size, bool executable) {
ASSERT(Contains(addr));
ASSERT(Contains(addr + size) || (addr + size == end()));
int prot = PROT_READ | PROT_WRITE | (executable ? PROT_EXEC : 0);
void* address = mmap(reinterpret_cast<void*>(addr), size, prot,
MAP_PRIVATE | MAP_ANON | MAP_FIXED,
-1, 0);
if (address == MAP_FAILED) {
return false;
}
return true;
}
bool VirtualMemory::Protect(Protection mode) {
int prot = 0;
switch (mode) {
case kNoAccess:
prot = PROT_NONE;
break;
case kReadOnly:
prot = PROT_READ;
break;
case kReadWrite:
prot = PROT_READ | PROT_WRITE;
break;
case kReadExecute:
prot = PROT_READ | PROT_EXEC;
break;
case kReadWriteExecute:
prot = PROT_READ | PROT_WRITE | PROT_EXEC;
break;
}
return (mprotect(address(), size(), prot) == 0);
}
} // namespace dart

View file

@ -106,6 +106,7 @@
'disassembler_arm.cc',
'disassembler_test.cc',
'debuginfo.h',
'debuginfo_android.cc',
'debuginfo_linux.cc',
'debuginfo_macos.cc',
'debuginfo_win.cc',
@ -139,6 +140,8 @@
'gc_marker.h',
'gc_sweeper.cc',
'gc_sweeper.h',
'gdbjit_android.cc',
'gdbjit_android.h',
'gdbjit_linux.cc',
'gdbjit_linux.h',
'globals.h',
@ -209,6 +212,7 @@
'object_store.cc',
'object_store.h',
'object_store_test.cc',
'os_android.cc',
'os_linux.cc',
'os_macos.cc',
'os_win.cc',
@ -284,6 +288,7 @@
'utils_test.cc',
'virtual_memory.cc',
'virtual_memory.h',
'virtual_memory_android.cc',
'virtual_memory_linux.cc',
'virtual_memory_macos.cc',
'virtual_memory_test.cc',