2012-03-30 05:32:44 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-03-23 11:40:14 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2012-07-18 12:51:44 +04:00
|
|
|
#include <pthread.h>
|
2012-07-13 00:28:14 +04:00
|
|
|
#include <string.h>
|
2012-10-26 03:34:06 +04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <time.h>
|
2012-07-13 00:28:14 +04:00
|
|
|
#include <unistd.h>
|
2012-10-26 03:34:06 +04:00
|
|
|
#include <android/log.h>
|
2013-12-03 02:08:27 +04:00
|
|
|
#include <sys/syscall.h>
|
2013-08-14 11:00:52 +04:00
|
|
|
|
|
|
|
#include "mozilla/Alignment.h"
|
2012-07-13 00:28:14 +04:00
|
|
|
|
2012-03-23 11:40:14 +04:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#define NS_EXPORT __attribute__ ((visibility("default")))
|
|
|
|
|
2013-07-19 18:10:00 +04:00
|
|
|
#if ANDROID_VERSION < 17 || defined(MOZ_WIDGET_ANDROID)
|
2012-03-23 11:40:14 +04:00
|
|
|
/* Android doesn't have pthread_atfork(), so we need to use our own. */
|
|
|
|
struct AtForkFuncs {
|
|
|
|
void (*prepare)(void);
|
|
|
|
void (*parent)(void);
|
|
|
|
void (*child)(void);
|
|
|
|
};
|
2013-03-17 11:03:05 +04:00
|
|
|
|
|
|
|
/* jemalloc's initialization calls pthread_atfork. When pthread_atfork (see
|
|
|
|
* further below) stores the corresponding data, it's going to allocate memory,
|
|
|
|
* which will loop back to jemalloc's initialization, leading to a dead-lock.
|
|
|
|
* So, for that specific vector, we use a special allocator that returns a
|
|
|
|
* static buffer for small sizes, and force the initial vector capacity to
|
|
|
|
* a size enough to store one atfork function table. */
|
|
|
|
template <typename T>
|
|
|
|
struct SpecialAllocator: public std::allocator<T>
|
|
|
|
{
|
|
|
|
SpecialAllocator(): bufUsed(false) {}
|
|
|
|
|
|
|
|
inline typename std::allocator<T>::pointer allocate(typename std::allocator<T>::size_type n, const void * = 0) {
|
|
|
|
if (!bufUsed && n == 1) {
|
|
|
|
bufUsed = true;
|
|
|
|
return buf.addr();
|
|
|
|
}
|
|
|
|
return reinterpret_cast<T *>(::operator new(sizeof(T) * n));
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void deallocate(typename std::allocator<T>::pointer p, typename std::allocator<T>::size_type n) {
|
|
|
|
if (p == buf.addr())
|
|
|
|
bufUsed = false;
|
|
|
|
else
|
|
|
|
::operator delete(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U>
|
|
|
|
struct rebind {
|
|
|
|
typedef SpecialAllocator<U> other;
|
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
|
|
|
mozilla::AlignedStorage2<T> buf;
|
|
|
|
bool bufUsed;
|
|
|
|
};
|
|
|
|
|
|
|
|
static std::vector<AtForkFuncs, SpecialAllocator<AtForkFuncs> > atfork;
|
2013-07-19 18:10:00 +04:00
|
|
|
#endif
|
2012-03-23 11:40:14 +04:00
|
|
|
|
2012-10-26 03:34:06 +04:00
|
|
|
#ifdef MOZ_WIDGET_GONK
|
|
|
|
#include "cpuacct.h"
|
|
|
|
|
2013-07-19 18:10:00 +04:00
|
|
|
#if ANDROID_VERSION < 17 || defined(MOZ_WIDGET_ANDROID)
|
2012-03-23 11:40:14 +04:00
|
|
|
extern "C" NS_EXPORT int
|
2012-10-26 03:34:06 +04:00
|
|
|
timer_create(clockid_t, struct sigevent*, timer_t*)
|
|
|
|
{
|
|
|
|
__android_log_print(ANDROID_LOG_ERROR, "BionicGlue", "timer_create not supported!");
|
|
|
|
abort();
|
|
|
|
return -1;
|
|
|
|
}
|
2013-07-19 18:10:00 +04:00
|
|
|
#endif
|
2012-10-26 03:34:06 +04:00
|
|
|
|
|
|
|
#else
|
|
|
|
#define cpuacct_add(x)
|
|
|
|
#endif
|
|
|
|
|
2013-07-19 18:10:00 +04:00
|
|
|
#if ANDROID_VERSION < 17 || defined(MOZ_WIDGET_ANDROID)
|
2012-10-26 03:34:06 +04:00
|
|
|
extern "C" NS_EXPORT int
|
2014-10-14 02:17:13 +04:00
|
|
|
pthread_atfork(void (*prepare)(void), void (*parent)(void), void (*child)(void))
|
2012-03-23 11:40:14 +04:00
|
|
|
{
|
|
|
|
AtForkFuncs funcs;
|
|
|
|
funcs.prepare = prepare;
|
|
|
|
funcs.parent = parent;
|
|
|
|
funcs.child = child;
|
2013-03-17 11:03:05 +04:00
|
|
|
if (!atfork.capacity())
|
|
|
|
atfork.reserve(1);
|
2012-03-23 11:40:14 +04:00
|
|
|
atfork.push_back(funcs);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-05-06 09:26:46 +04:00
|
|
|
extern "C" NS_EXPORT pid_t __fork(void);
|
|
|
|
|
2012-03-23 11:40:14 +04:00
|
|
|
extern "C" NS_EXPORT pid_t
|
2014-10-14 02:17:13 +04:00
|
|
|
fork(void)
|
2012-03-23 11:40:14 +04:00
|
|
|
{
|
|
|
|
pid_t pid;
|
2013-09-03 13:39:35 +04:00
|
|
|
for (auto it = atfork.rbegin();
|
2012-03-23 11:40:14 +04:00
|
|
|
it < atfork.rend(); ++it)
|
|
|
|
if (it->prepare)
|
|
|
|
it->prepare();
|
|
|
|
|
2014-04-28 19:21:22 +04:00
|
|
|
switch ((pid = syscall(__NR_clone, SIGCHLD, NULL, NULL, NULL, NULL))) {
|
2012-03-23 11:40:14 +04:00
|
|
|
case 0:
|
2012-10-26 03:34:06 +04:00
|
|
|
cpuacct_add(getuid());
|
2013-09-03 13:39:35 +04:00
|
|
|
for (auto it = atfork.begin();
|
2012-03-23 11:40:14 +04:00
|
|
|
it < atfork.end(); ++it)
|
|
|
|
if (it->child)
|
|
|
|
it->child();
|
|
|
|
break;
|
|
|
|
default:
|
2013-09-03 13:39:35 +04:00
|
|
|
for (auto it = atfork.begin();
|
2012-03-23 11:40:14 +04:00
|
|
|
it < atfork.end(); ++it)
|
|
|
|
if (it->parent)
|
|
|
|
it->parent();
|
|
|
|
}
|
|
|
|
return pid;
|
|
|
|
}
|
2013-07-19 18:10:00 +04:00
|
|
|
#endif
|
2012-04-02 13:59:29 +04:00
|
|
|
|
|
|
|
extern "C" NS_EXPORT int
|
2014-10-14 02:17:13 +04:00
|
|
|
raise(int sig)
|
2012-04-02 13:59:29 +04:00
|
|
|
{
|
2013-12-03 02:08:27 +04:00
|
|
|
// Bug 741272: Bionic incorrectly uses kill(), which signals the
|
|
|
|
// process, and thus could signal another thread (and let this one
|
|
|
|
// return "successfully" from raising a fatal signal).
|
|
|
|
//
|
|
|
|
// Bug 943170: POSIX specifies pthread_kill(pthread_self(), sig) as
|
|
|
|
// equivalent to raise(sig), but Bionic also has a bug with these
|
|
|
|
// functions, where a forked child will kill its parent instead.
|
|
|
|
|
|
|
|
extern pid_t gettid(void);
|
|
|
|
return syscall(__NR_tgkill, getpid(), gettid(), sig);
|
2012-04-02 13:59:29 +04:00
|
|
|
}
|
|
|
|
|
2014-10-14 02:18:01 +04:00
|
|
|
/* Flash plugin uses symbols that are not present in Android >= 4.4 */
|
|
|
|
#ifndef MOZ_WIDGET_GONK
|
|
|
|
namespace android {
|
|
|
|
namespace VectorImpl {
|
|
|
|
NS_EXPORT void reservedVectorImpl1(void) { }
|
|
|
|
NS_EXPORT void reservedVectorImpl2(void) { }
|
|
|
|
NS_EXPORT void reservedVectorImpl3(void) { }
|
|
|
|
NS_EXPORT void reservedVectorImpl4(void) { }
|
|
|
|
NS_EXPORT void reservedVectorImpl5(void) { }
|
|
|
|
NS_EXPORT void reservedVectorImpl6(void) { }
|
|
|
|
NS_EXPORT void reservedVectorImpl7(void) { }
|
|
|
|
NS_EXPORT void reservedVectorImpl8(void) { }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|