static int do_test (void) { pid_t pid; int status = 0; if (pthread_atfork (prepare1, parent1, child1) != 0) { puts ("1st atfork failed"); exit (1); } if (pthread_atfork (prepare2, parent2, child2) != 0) { puts ("2nd atfork failed"); exit (1); } pid = fork (); if (pid == -1) { puts ("fork failed"); exit (1); } if (pid != 0) { /* Parent. */ if (val != 24) { printf ("expected val=%d, got %d\n", 24, val); exit (1); } if (TEMP_FAILURE_RETRY (waitpid (pid, &status, 0)) != pid) { puts ("waitpid failed"); exit (1); } } else { /* Child. */ if (val != 80) { printf ("expected val=%d, got %d\n", 80, val); exit (2); } } return status; }
int main() { int err; pid_t pid; pthread_t tid; if((err = pthread_atfork(prepare,parent,child)) != 0) { perror("pthread_atfork error"); exit(-1); } if((err == pthread_create(&tid, NULL,thr_fn,0))< 0) { perror("pthread_create error"); exit(-2); } sleep(2); printf("parent about to fork...\n"); if((pid = fork()) < 0) { exit(-3); } else if(pid == 0) { printf("child return from fork\n"); } else { printf("parent return from fork\n"); } return 0; }
int main(int argc, char *argv[]) { int err; pid_t pid; pthread_t tid; if ((err = pthread_atfork(prepare, parent, child))) { fprintf(stderr, "pthread_atfork error\n"); exit(2); } err = pthread_create(&tid, NULL, thr_fn, NULL); if (err) { fprintf(stderr, "pthread_create error\n"); exit(3); } sleep(2); printf("parent about to fork...\n"); if ((pid = fork()) < 0) { fprintf(stderr, "fork error\n"); exit(4); } else if (pid == 0) { printf("child returned from fork\n"); } else { printf("parent returned from fork\n"); } exit(0); }
void uwsgi_python_enable_threads() { PyEval_InitThreads(); if (pthread_key_create(&up.upt_save_key, NULL)) { uwsgi_error("pthread_key_create()"); exit(1); } if (pthread_key_create(&up.upt_gil_key, NULL)) { uwsgi_error("pthread_key_create()"); exit(1); } pthread_setspecific(up.upt_save_key, (void *) PyThreadState_Get()); pthread_setspecific(up.upt_gil_key, (void *) PyThreadState_Get()); pthread_mutex_init(&up.lock_pyloaders, NULL); pthread_atfork(uwsgi_python_pthread_prepare, uwsgi_python_pthread_parent, uwsgi_python_pthread_child); up.gil_get = gil_real_get; up.gil_release = gil_real_release; up.swap_ts = simple_threaded_swap_ts; up.reset_ts = simple_threaded_reset_ts; if (uwsgi.threads > 1) { up.swap_ts = threaded_swap_ts; up.reset_ts = threaded_reset_ts; } uwsgi_log("python threads support enabled\n"); }
int main(void) { int err; pid_t pid; pthread_t tid; if((err = pthread_atfork(prepare,parent,child)) != 0){ err_exit(err,"can't install fork handlers"); } if((err = pthread_create(&tid,NULL,thr_fn,0)) != 0){ err_exit(err,"can't create thread"); } sleep(2); printf("parent about to fork...\n"); if((pid = fork()) < 0){ err_quit("fork failed"); }else if(pid == 0){ //child printf("child return from fork\n"); }else{ printf("parent return from fork\n"); } exit(0); }
XPC_RETURNS_RETAINED static xpc_pipe_t _od_xpc_pipe(bool resetPipe) { static dispatch_once_t once; xpc_pipe_t result = NULL; #ifdef __i386__ if (xpc_pipe_create == NULL) { _si_disable_opendirectory(); return NULL; } #endif dispatch_once(&once, ^(void) { char *rc_xbs; /* if this is a build environment we ignore opendirectoryd */ rc_xbs = getenv("RC_XBS"); if ((issetugid() == 0) && (rc_xbs != NULL) && (strcmp(rc_xbs, "YES") == 0)) { _si_opendirectory_disabled = 1; return; } pthread_atfork(_od_fork_prepare, _od_fork_parent, _od_fork_child); });
int main( void ) { int err; pid_t pid; pthread_t tid; #if defined( BSD ) || defined( MACOS ) printf( "pthread atfork is unsupported\n" ); #else if ( (err = pthread_atfork( prepare, parent, child )) != 0 ) { err_exit( err, "cannot install fork handlers" ); } err = pthread_create( &tid, NULL, thread_run, 0 ); if ( err != 0 ) { err_exit( err, "cannot create thread" ); } sleep( 2 ); /* TODO: why ? */ printf( "parent about to fork...\n" ); if ( (pid = fork()) < 0 ) { err_quit( "fork failed" ); } else if ( pid == 0 ) { /* child is running */ printf( "child returned from fork\n" ); } else { /* parent is running */ printf( "parent returned from fork\n" ); } #endif return 0; }
int slog_open(const char *dir, const char *name, enum slog_level level, uint64_t rotate_size) { if (_slog_mkdir_recursive(dir) == -1) return -1; char *filename = calloc(1, strlen(dir) + strlen(name) + 2); sprintf(filename, "%s/%s", dir, name); int fd = open(filename, O_WRONLY | O_APPEND | O_CREAT, 0644); if (fd < 0) { free(filename); return -1; } char *lockname = calloc(1, strlen(dir) + strlen(name) + 8); sprintf(lockname, "%s/.%s.lock", dir, name); int lockfd = open(lockname, O_RDONLY | O_CREAT, 0644); if (lockfd < 0) { free(lockname); free(filename); close(fd); return -1; } logger = calloc(1, sizeof(*logger)); logger->fd = fd; logger->filename = filename; logger->level = level; logger->lockfd = lockfd; logger->lockname = lockname; logger->rotate_size = rotate_size; fstat(fd, &logger->fdstat); pthread_mutex_init(&logger->mutex, NULL); /* fork是安全的, 子进程会保持mutex无锁, 同时会重新打开lock文件 */ pthread_atfork(_slog_atfork_prepare, _slog_atfork_parent, _slog_atfork_child); return 0; }
// register a monitor in our list of monitors // return 0 on success // return -ENOSPC if we're out of slots static int udev_monitor_register( struct udev_monitor* monitor ) { g_monitor_table_spinlock(); // find a free slot int i = 0; int rc = -ENOSPC; for( i = 0; i < UDEV_MAX_MONITORS; i++ ) { if( g_monitor_table[i] == NULL ) { g_monitor_table[i] = monitor; monitor->slot = i; rc = 0; break; } } if( g_pid == 0 ) { // first monitor ever. // register our fork handler. g_pid = getpid(); pthread_atfork( NULL, NULL, udev_monitor_atfork ); } g_monitor_table_unlock(); return rc; }
/* Test function -- calls pthread_setschedparam() and checks that EINTR is never returned. */ void * test(void * arg) { int ret = 0; /* We don't block the signals SIGUSR1 and SIGUSR2 for this THREAD */ ret = pthread_sigmask(SIG_UNBLOCK, &usersigs, NULL); if (ret != 0) { UNRESOLVED(ret, "Unable to unblock SIGUSR1 and SIGUSR2 in worker thread"); } while (do_it) { count_ope++; ret = pthread_atfork(prepare, parent, child); if (ret == EINTR) { FAILED("EINTR was returned"); } } return NULL; }
__END_DECLS // ------------------------------------------------------------------------ static void InitAtfork() { static pthread_once_t atfork_init = PTHREAD_ONCE_INIT; pthread_once(&atfork_init, [](){ pthread_atfork( [](){ if (g_debug != nullptr) { g_debug->PrepareFork(); } }, [](){ if (g_debug != nullptr) { g_debug->PostForkParent(); } }, [](){ if (g_debug != nullptr) { g_debug->PostForkChild(); } } ); }); }
static void s_once_proc(void) { int st; mccp_result_t r; if ((st = pthread_attr_init(&s_attr)) == 0) { if ((st = pthread_attr_setdetachstate(&s_attr, PTHREAD_CREATE_DETACHED)) != 0) { errno = st; perror("pthread_attr_setdetachstate"); mccp_exit_fatal("can't initialize detached thread attr.\n"); } } else { errno = st; perror("pthread_attr_init"); mccp_exit_fatal("can't initialize thread attr.\n"); } if ((r = mccp_hashmap_create(&s_thd_tbl, MCCP_HASHMAP_TYPE_ONE_WORD, NULL)) != MCCP_RESULT_OK) { mccp_perror(r, "mccp_hashmap_create()"); mccp_exit_fatal("can't initialize the thread table.\n"); } if ((r = mccp_hashmap_create(&s_alloc_tbl, MCCP_HASHMAP_TYPE_ONE_WORD, NULL)) != MCCP_RESULT_OK) { mccp_perror(r, "mccp_hashmap_create()"); mccp_exit_fatal("can't initialize the thread allocation table.\n"); } (void)pthread_atfork(NULL, NULL, s_child_at_fork); }
init_process(void) { assert(!process_inited); if (getenv("_RR_CHECK_PRELOAD")) { /* The tracer parent is just checking that we loaded. * We did, so return a success code. */ exit(0); } real_pthread_create = dlsym(RTLD_NEXT, "pthread_create"); real_pthread_mutex_timedlock = dlsym(RTLD_NEXT, "pthread_mutex_timedlock"); buffer_enabled = !!getenv(SYSCALLBUF_ENABLED_ENV_VAR); if (!buffer_enabled) { debug("Syscall buffering is disabled"); process_inited = 1; return; } pthread_atfork(NULL, NULL, post_fork_child); install_syscall_filter(); rrcall_monkeypatch_vdso(&_vsyscall_hook_trampoline); process_inited = 1; init_thread(); }
void __attribute__ ((constructor)) cop_init() { if (!cop_process_init_complete) { cop_init_cops(); /* set default memory pool values */ unsigned long cc_size = 16 * 1024 * 1024; int cc_bolt = 0; unsigned long rx_size = 16 * 1024 * 1024; int rx_bolt = 0; read_environment_variables(&cc_size, &cc_bolt, &rx_size, &rx_bolt); mpr_rx_pbic = malloc(sizeof(mapped_memory_pbic_root)); mpr_cc_pbic = malloc(sizeof(mapped_memory_pbic_root)); if (COP_NUMBER_COPS) { /* check for compatible */ call_cop_get_compatible(COP_SYM_CRYPTO); cop_init_rx_pbic(mpr_rx_pbic, pool_block_Size64K, rx_size, rx_bolt); cop_init_cc_pbic(mpr_cc_pbic, pool_block_Size64K, cc_size, cc_bolt); /* register fork handler */ pthread_atfork(NULL, cop_fork_parent, cop_fork_child); } cop_process_init_complete = 1; } }
int main(void) { int err; pid_t pid; pthread_t tid; #if defined(BSD) || defined(MACOS) printf("pthread_atfork is unsupported\n"); #else if((err = pthread_atfork(prepare,parent,child)) != 0) err_exit(err,"can't install fork handlers"); err = pthread_create(&tid,NULL,thr_fn,0); if(err != 0) err_exit(err,"can't create thread"); sleep(2); printf("parent about to fork ...\n"); if((pid = fork()) < 0) err_quit("fork failed"); else if(pid == 0) printf("child returned from fork\n"); else printf("parent returned from fork\n"); #endif exit(0); }
static int boilerplate_init(void) { pthread_atfork(NULL, NULL, __boilerplate_init); __boilerplate_init(); return 0; }
void opal_warn_fork(void) { if (opal_warn_on_fork && !atfork_called) { pthread_atfork(warn_fork_cb, NULL, NULL); atfork_called = true; } }
int main() { pid_t pid; puts("pocz±tek programu"); inicjalizacja_watkow(); /* rejestrowanie funkcji wykonywanej w procesie potomnym */ errno = pthread_atfork(NULL, NULL, inicjalizacja_watkow); test_errno("pthread_atfork"); sleep(1); pid = fork(); printf("fork => %d\n", pid); switch (pid) { case -1: test_errno("fork"); break; case 0: // proces potomny sleep(2); break; default: // proces nadrzêdny waitpid(pid, NULL, 0); test_errno("waitpid"); break; } /* koñczymy proces, bez ogl±dania siê na w±tki */ return EXIT_SUCCESS; }
/*! Initialize the static process data. */ void Process::initialize () { #ifdef HAVE_PTHREAD forkMutex.init (); pthread_atfork (forkPrepare, forkParent, forkChild); #endif }
/***************************************************************************** * th_task_initialize -- called to initialize the task API * * th_task_initialize spawns a single listener pthread * this thread is solely to listen for messages from child * processes, which will be the task_exit message * *****************************************************************************/ th_status th_task_initialize(void) { th_status status = TH_OK; int i = 0; assert(CONKER_MAX_TASKS < FD_SETSIZE); th_log_info("[TH_FORK] -- th_task_initialize, max tasks = %d\n", CONKER_MAX_TASKS); for (i = 0; i < CONKER_MAX_TASKS; i++) { task_pids[i] = -1; task_pipes[i].pipe_fds1[0] = task_pipes[i].pipe_fds1[1] = -1; task_pipes[i].pipe_fds2[0] = task_pipes[i].pipe_fds2[1] = -1; } listener_flag = 1; /* start the listener thread */ if (pthread_create(&listener_thread, NULL, listener_thread_body, NULL) != 0) { status = TH_ERROR; } /* register child 'atfork' function */ if (pthread_atfork(NULL, NULL, child_fork_handler) != 0) { status = TH_ERROR; } parent_pid = getpid(); return status; }
inline static void _init(void) { while (!at_forked) { pthread_atfork(NULL, NULL, _atfork_child); at_forked = true; } }
chk_expn_thd *chk_exp_get_thread_expn() { if(!_exception_st) { _exception_st = calloc(1,sizeof(*_exception_st)); list_init(&_exception_st->expstack); pthread_atfork(NULL,NULL,reset_perthread_exception_st); } return _exception_st; }
TEST(pthread, pthread_atfork) { ASSERT_EQ(0, pthread_atfork(AtForkPrepare1, AtForkParent1, AtForkChild1)); ASSERT_EQ(0, pthread_atfork(AtForkPrepare2, AtForkParent2, AtForkChild2)); int pid = fork(); ASSERT_NE(-1, pid) << strerror(errno); // Child and parent calls are made in the order they were registered. if (pid == 0) { ASSERT_EQ(0x12, g_atfork_child_calls); _exit(0); } ASSERT_EQ(0x12, g_atfork_parent_calls); // Prepare calls are made in the reverse order. ASSERT_EQ(0x21, g_atfork_prepare_calls); }
static bool set_after_fork_handler() { if (pthread_atfork(NULL, after_fork_parent, after_fork_child)) { log(LOG_ERR, "pthread_atfork() failed"); return false; } return true; }
LoggerInitializer(void) { if (sIsInitialized == false) { (void)pthread_atfork(NULL, NULL, sChildAfterFork); sIsInitialized = true; } }
void je_malloc_disable_init() { if (pthread_atfork(je_malloc_disable_prefork, je_malloc_disable_postfork_parent, je_malloc_disable_postfork_child) != 0) { malloc_write("<jemalloc>: Error in pthread_atfork()\n"); if (opt_abort) abort(); } }
/* Called once form pthread_once in timer_init. This initializes the module and ensures that reinit_after_fork will be executed in any child process. */ void __timer_init_once (void) { init_module (); #if !defined(_ELIX_LEVEL) || _ELIX_LEVEL >= 3 pthread_atfork (0, 0, reinit_after_fork); #endif }
TEST(pthread, pthread_atfork_with_dlclose) { ASSERT_EQ(0, pthread_atfork(AtForkPrepare1, AtForkParent1, AtForkChild1)); void* handle = dlopen("libtest_pthread_atfork.so", RTLD_NOW | RTLD_LOCAL); ASSERT_TRUE(handle != nullptr) << dlerror(); typedef int (*fn_t)(void (*)(void), void (*)(void), void (*)(void)); fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "proxy_pthread_atfork")); ASSERT_TRUE(fn != nullptr) << dlerror(); // the library registers 2 additional atfork handlers in a constructor ASSERT_EQ(0, fn(AtForkPrepare2, AtForkParent2, AtForkChild2)); ASSERT_EQ(0, fn(AtForkPrepare3, AtForkParent3, AtForkChild3)); ASSERT_EQ(0, pthread_atfork(AtForkPrepare4, AtForkParent4, AtForkChild4)); pid_t pid = fork(); ASSERT_NE(-1, pid) << strerror(errno); if (pid == 0) { ASSERT_EQ(1234, g_atfork_child_calls); _exit(0); } ASSERT_EQ(1234, g_atfork_parent_calls); ASSERT_EQ(4321, g_atfork_prepare_calls); EXPECT_EQ(0, dlclose(handle)); g_atfork_prepare_calls = g_atfork_parent_calls = g_atfork_child_calls = 0; AssertChildExited(pid, 0); pid = fork(); ASSERT_NE(-1, pid) << strerror(errno); if (pid == 0) { ASSERT_EQ(14, g_atfork_child_calls); _exit(0); } ASSERT_EQ(14, g_atfork_parent_calls); ASSERT_EQ(41, g_atfork_prepare_calls); AssertChildExited(pid, 0); }
pid_t thread_id() { if(!tid){ tid = gettidv1(); pthread_atfork(NULL,NULL,child); } return tid; }
void ompi_warn_fork(void) { #if OPAL_HAVE_POSIX_THREADS if (ompi_warn_on_fork && !atfork_called) { pthread_atfork(warn_fork_cb, NULL, NULL); atfork_called = true; } #endif }