2012-03-29 18:32:44 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-03-23 00:40:14 -07: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/. */
|
|
|
|
|
|
|
|
#include <unistd.h>
|
2012-04-02 02:59:29 -07:00
|
|
|
#include <pthread.h>
|
2012-03-23 00:40:14 -07:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#define NS_EXPORT __attribute__ ((visibility("default")))
|
|
|
|
|
|
|
|
/* Android doesn't have pthread_atfork(), so we need to use our own. */
|
|
|
|
struct AtForkFuncs {
|
|
|
|
void (*prepare)(void);
|
|
|
|
void (*parent)(void);
|
|
|
|
void (*child)(void);
|
|
|
|
};
|
|
|
|
static std::vector<AtForkFuncs> atfork;
|
|
|
|
|
|
|
|
extern "C" NS_EXPORT int
|
|
|
|
__wrap_pthread_atfork(void (*prepare)(void), void (*parent)(void), void (*child)(void))
|
|
|
|
{
|
|
|
|
AtForkFuncs funcs;
|
|
|
|
funcs.prepare = prepare;
|
|
|
|
funcs.parent = parent;
|
|
|
|
funcs.child = child;
|
|
|
|
atfork.push_back(funcs);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" NS_EXPORT pid_t
|
|
|
|
__wrap_fork(void)
|
|
|
|
{
|
|
|
|
pid_t pid;
|
|
|
|
for (std::vector<AtForkFuncs>::reverse_iterator it = atfork.rbegin();
|
|
|
|
it < atfork.rend(); ++it)
|
|
|
|
if (it->prepare)
|
|
|
|
it->prepare();
|
|
|
|
|
|
|
|
switch ((pid = fork())) {
|
|
|
|
case 0:
|
|
|
|
for (std::vector<AtForkFuncs>::iterator it = atfork.begin();
|
|
|
|
it < atfork.end(); ++it)
|
|
|
|
if (it->child)
|
|
|
|
it->child();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
for (std::vector<AtForkFuncs>::iterator it = atfork.begin();
|
|
|
|
it < atfork.end(); ++it)
|
|
|
|
if (it->parent)
|
|
|
|
it->parent();
|
|
|
|
}
|
|
|
|
return pid;
|
|
|
|
}
|
2012-04-02 02:59:29 -07:00
|
|
|
|
|
|
|
extern "C" NS_EXPORT int
|
|
|
|
__wrap_raise(int sig)
|
|
|
|
{
|
|
|
|
return pthread_kill(pthread_self(), sig);
|
|
|
|
}
|
|
|
|
|