3 Copyright 1996, 1997, 1998, 1999, 2000, 2001 Red Hat, Inc.
5 This file is part of Cygwin.
7 This software is a copyrighted work licensed under the terms of the
8 Cygwin license. Please consult the file "CYGWIN_LICENSE" for
27 #include "child_info.h"
29 #include "perthread.h"
30 #include "perprocess.h"
36 static pid_t fork_pids[100];
39 /* Timeout to wait for child to start, parent to init child, etc. */
40 /* FIXME: Once things stabilize, bump up to a few minutes. */
41 #define FORK_WAIT_TIMEOUT (300 * 1000) /* 300 seconds */
43 #define dll_data_start &_data_start__
44 #define dll_data_end &_data_end__
45 #define dll_bss_start &_bss_start__
46 #define dll_bss_end &_bss_end__
49 per_thread::set (void *s)
51 if (s == PER_THREAD_FORK_CLEAR)
56 TlsSetValue (get_tls (), s);
60 stack_base (child_info_fork &ch)
62 MEMORY_BASIC_INFORMATION m;
63 memset (&m, 0, sizeof m);
64 if (!VirtualQuery ((LPCVOID) &m, &m, sizeof m))
65 system_printf ("couldn't get memory info, %E");
67 ch.stacktop = m.AllocationBase;
68 ch.stackbottom = (LPBYTE) m.BaseAddress + m.RegionSize;
69 ch.stacksize = (DWORD) ch.stackbottom - (DWORD) &m;
70 debug_printf ("bottom %p, top %p, stack %p, size %d, reserve %d",
71 ch.stackbottom, ch.stacktop, &m, ch.stacksize,
72 (DWORD) ch.stackbottom - (DWORD) ch.stacktop);
75 /* Copy memory from parent to child.
76 The result is a boolean indicating success. */
79 fork_copy (PROCESS_INFORMATION &pi, const char *what, ...)
85 va_start (args, what);
87 while ((low = va_arg (args, char *)))
89 char *high = va_arg (args, char *);
90 DWORD todo = wincap.chunksize () ?: high - low;
93 for (here = low; here < high; here += todo)
96 if (here + todo > high)
98 int res = WriteProcessMemory (pi.hProcess, here, here, todo, &done);
99 debug_printf ("child handle %p, low %p, high %p, res %d", pi.hProcess,
101 if (!res || todo != done)
105 /* If this happens then there is a bug in our fork
106 implementation somewhere. */
107 system_printf ("%s pass %d failed, %p..%p, done %d, windows pid %u, %E",
108 what, pass, low, high, done, pi.dwProcessId);
116 debug_printf ("done");
120 TerminateProcess (pi.hProcess, 1);
125 /* Wait for child to finish what it's doing and signal us.
126 We don't want to wait forever here.If there's a problem somewhere
127 it'll hang the entire system (since all forks are mutex'd). If we
128 time out, set errno = EAGAIN and hope the app tries again. */
130 sync_with_child (PROCESS_INFORMATION &pi, HANDLE subproc_ready,
131 BOOL hang_child, const char *s)
133 /* We also add the child process handle to the wait. If the child fails
134 to initialize (eg. because of a missing dll). Then this
135 handle will become signalled. This stops a *looong* timeout wait.
139 debug_printf ("waiting for child. reason: %s, hang_child %d", s,
142 w4[0] = subproc_ready;
143 DWORD rc = WaitForMultipleObjects (2, w4, FALSE, FORK_WAIT_TIMEOUT);
145 if (rc == WAIT_OBJECT_0 ||
146 WaitForSingleObject (subproc_ready, 0) == WAIT_OBJECT_0)
148 else if (rc == WAIT_FAILED || rc == WAIT_TIMEOUT)
150 if (rc != WAIT_FAILED)
151 system_printf ("WaitForMultipleObjects timed out");
153 system_printf ("WaitForMultipleObjects failed, %E");
155 syscall_printf ("-1 = fork(), WaitForMultipleObjects failed");
156 TerminateProcess (pi.hProcess, 1);
161 /* Child died. Clean up and exit. */
163 GetExitCodeProcess (pi.hProcess, &errcode);
164 /* Fix me. This is not enough. The fork should not be considered
165 * to have failed if the process was essentially killed by a signal.
167 if (errcode != STATUS_CONTROL_C_EXIT)
169 system_printf ("child %d(%p) died before initialization with status code %p",
170 pi.dwProcessId, pi.hProcess, errcode);
171 system_printf ("*** child state %s", s);
177 syscall_printf ("Child died before subproc_ready signalled");
181 debug_printf ("child signalled me");
186 resume_child (PROCESS_INFORMATION &pi, HANDLE forker_finished)
188 SetEvent (forker_finished);
189 debug_printf ("signalled child");
193 /* Notify parent that it is time for the next step.
194 Note that this has to be a macro since the parent may be messing with
196 static void __stdcall
197 sync_with_parent(const char *s, bool hang_self)
199 debug_printf ("signalling parent: %s", s);
200 /* Tell our parent we're waiting. */
201 if (!SetEvent (child_proc_info->subproc_ready))
202 api_fatal ("fork child - SetEvent for %s failed, %E", s);
205 HANDLE h = child_proc_info->forker_finished;
206 /* Wait for the parent to fill in our stack and heap.
207 Don't wait forever here. If our parent dies we don't want to clog
208 the system. If the wait fails, we really can't continue so exit. */
209 DWORD psync_rc = WaitForSingleObject (h, FORK_WAIT_TIMEOUT);
210 debug_printf ("awake");
214 api_fatal ("WFSO timed out for %s", s);
217 if (GetLastError () == ERROR_INVALID_HANDLE &&
218 WaitForSingleObject (child_proc_info->forker_finished, 1) != WAIT_FAILED)
220 api_fatal ("WFSO failed for %s, fork_finished %p, %E", s,
221 child_proc_info->forker_finished);
224 debug_printf ("no problems");
231 fork_child (HANDLE& hParent, dll *&first_dll, bool& load_dlls)
233 debug_printf ("child is running. pid %d, ppid %d, stack here %p",
234 myself->pid, myself->ppid, __builtin_frame_address (0));
236 /* Restore the inheritance state as in parent
237 Don't call setuid here! The flags are already set. */
238 if (cygheap->user.impersonated)
240 debug_printf ("Impersonation of child, token: %d", cygheap->user.token);
241 if (cygheap->user.token == INVALID_HANDLE_VALUE)
242 RevertToSelf (); // probably not needed
243 else if (!ImpersonateLoggedOnUser (cygheap->user.token))
244 system_printf ("Impersonate for forked child failed: %E");
247 sync_with_parent ("after longjmp.", TRUE);
248 ProtectHandle (hParent);
249 sigproc_printf ("hParent %p, child 1 first_dll %p, load_dlls %d\n", hParent,
250 first_dll, load_dlls);
254 if (GetEnvironmentVariable ("FORKDEBUG", &c, 1))
257 /* This is useful for debugging fork problems. Use gdb to attach to
258 the pid reported here. */
259 if (GetEnvironmentVariable ("CYGWIN_FORK_SLEEP", buf, sizeof (buf)))
261 small_printf ("Sleeping %d after fork, pid %u\n", atoi (buf), GetCurrentProcessId ());
266 /* If we've played with the stack, stacksize != 0. That means that
267 fork() was invoked from other than the main thread. Make sure that
268 when the "main" thread exits it calls do_exit, like a normal process.
269 Exit with a status code of 0. */
270 if (child_proc_info->stacksize)
272 ((DWORD *)child_proc_info->stackbottom)[-17] = (DWORD)do_exit;
273 ((DWORD *)child_proc_info->stackbottom)[-15] = (DWORD)0;
276 set_file_api_mode (current_codepage);
280 debug_fixup_after_fork ();
281 pinfo_fixup_after_fork ();
282 cygheap->fdtab.fixup_after_fork (hParent);
283 signal_fixup_after_fork ();
287 /* If we haven't dynamically loaded any dlls, just signal
288 the parent. Otherwise, load all the dlls, tell the parent
289 that we're done, and wait for the parent to fill in the.
290 loaded dlls' data/bss. */
292 sync_with_parent ("performed fork fixup.", FALSE);
295 dlls.load_after_fork (hParent, first_dll);
296 sync_with_parent ("loaded dlls", TRUE);
299 ForceCloseHandle (hParent);
300 (void) ForceCloseHandle (child_proc_info->subproc_ready);
301 (void) ForceCloseHandle (child_proc_info->forker_finished);
303 if (fixup_mmaps_after_fork ())
304 api_fatal ("recreate_mmaps_after_fork_failed");
306 /* Set thread local stuff to zero. Under Windows 95/98 this is sometimes
307 non-zero, for some reason.
308 FIXME: There is a memory leak here after a fork. */
309 for (per_thread **t = threadstuff; *t; t++)
310 if ((*t)->clear_on_fork ())
313 user_data->threadinterface->fixup_after_fork ();
315 /* Initialize signal/process handling */
317 __pthread_atforkchild ();
318 cygbench ("fork-child");
323 slow_pid_reuse (HANDLE h)
325 static NO_COPY HANDLE last_fork_procs[8] = {0};
326 static NO_COPY unsigned nfork_procs = 0;
328 if (nfork_procs >= (sizeof (last_fork_procs) / sizeof (last_fork_procs [0])))
330 /* Keep a list of handles to forked processes sitting around to prevent
331 Windows from reusing the same pid n times in a row. Having the same pids
332 close in succesion confuses bash. Keeping a handle open will stop
333 windows from reusing the same pid. */
334 if (last_fork_procs[nfork_procs])
335 ForceCloseHandle1 (last_fork_procs[nfork_procs], fork_stupidity);
336 if (DuplicateHandle (hMainProc, h, hMainProc, &last_fork_procs[nfork_procs],
337 0, FALSE, DUPLICATE_SAME_ACCESS))
338 ProtectHandle1 (last_fork_procs[nfork_procs], fork_stupidity);
341 last_fork_procs[nfork_procs] = NULL;
342 system_printf ("couldn't create last_fork_proc, %E");
348 fork_parent (HANDLE& hParent, dll *&first_dll,
349 bool& load_dlls, void *stack_here, child_info_fork &ch)
351 HANDLE subproc_ready, forker_finished;
353 PROCESS_INFORMATION pi = {0, NULL, 0, 0};
355 /* call the pthread_atfork prepare functions */
356 __pthread_atforkprepare ();
360 #ifdef DEBUGGING_NOTNEEDED
361 /* The ProtectHandle call allocates memory so we need to make sure
362 that enough is set aside here so that the sbrk pointer does not
363 move when ProtectHandle is called after the child is started.
364 Otherwise the sbrk pointers in the parent will not agree with
365 the child and when user_data is (regrettably) copied over,
366 the user_data->ptr field will not be accurate. */
367 free (malloc (4096));
370 int c_flags = GetPriorityClass (hMainProc) /*|
371 CREATE_NEW_PROCESS_GROUP*/;
372 STARTUPINFO si = {0, NULL, NULL, NULL, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL};
374 /* If we don't have a console, then don't create a console for the
376 HANDLE console_handle = CreateFileA ("CONOUT$", GENERIC_WRITE,
377 FILE_SHARE_WRITE, &sec_none_nih,
378 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
381 if (console_handle != INVALID_HANDLE_VALUE && console_handle != 0)
382 CloseHandle (console_handle);
384 c_flags |= DETACHED_PROCESS;
386 /* Some file types (currently only sockets) need extra effort in the
387 parent after CreateProcess and before copying the datastructures
388 to the child. So we have to start the child in suspend state,
389 unfortunately, to avoid a race condition. */
390 if (cygheap->fdtab.need_fixup_before ())
391 c_flags |= CREATE_SUSPENDED;
393 /* Create an inheritable handle to pass to the child process. This will
394 allow the child to duplicate handles from the parent to itself. */
396 if (!DuplicateHandle (hMainProc, hMainProc, hMainProc, &hParent, 0, 1,
397 DUPLICATE_SAME_ACCESS))
399 system_printf ("couldn't create handle to myself for child, %E");
403 /* Remember the address of the first loaded dll and decide
404 if we need to load dlls. We do this here so that this
405 information will be available in the parent and, when
406 the stack is copied, in the child. */
407 first_dll = dlls.start.next;
408 load_dlls = dlls.reload_on_fork && dlls.loaded_dlls;
410 /* This will help some of the confusion. */
413 subproc_ready = CreateEvent (&sec_all, FALSE, FALSE, NULL);
414 if (subproc_ready == NULL)
416 CloseHandle (hParent);
417 system_printf ("unable to allocate subproc_ready event, %E");
420 forker_finished = CreateEvent (&sec_all, FALSE, FALSE, NULL);
421 if (forker_finished == NULL)
423 CloseHandle (hParent);
424 CloseHandle (subproc_ready);
425 system_printf ("unable to allocate forker_finished event, %E");
429 ProtectHandle (subproc_ready);
430 ProtectHandle (forker_finished);
432 init_child_info (PROC_FORK, &ch, 1, subproc_ready);
434 ch.forker_finished = forker_finished;
438 si.cb = sizeof (STARTUPINFO);
439 si.lpReserved2 = (LPBYTE)&ch;
440 si.cbReserved2 = sizeof(ch);
442 /* Remove impersonation */
443 if (cygheap->user.impersonated && cygheap->user.token != INVALID_HANDLE_VALUE)
450 for (int pass = 0; pass < 2; pass++)
453 while ((pid = fork_pids[npid++]))
466 syscall_printf ("CreateProcess (%s, %s, 0, 0, 1, %x, 0, 0, %p, %p)",
467 myself->progname, myself->progname, c_flags, &si, &pi);
468 __malloc_lock (_reent_clib ());
470 newheap = cygheap_setup_for_child (&ch,cygheap->fdtab.need_fixup_before ());
471 rc = CreateProcess (myself->progname, /* image to run */
472 myself->progname, /* what we send in arg0 */
473 sec_user_nih (sa_buf),
474 sec_user_nih (sa_buf),
475 TRUE, /* inherit handles from parent */
477 NULL, /* environment filled in later */
478 0, /* use current drive/directory */
482 CloseHandle (hParent);
487 syscall_printf ("CreateProcessA failed, %E");
488 ForceCloseHandle(subproc_ready);
489 ForceCloseHandle(forker_finished);
490 /* Restore impersonation */
491 if (cygheap->user.impersonated
492 && cygheap->user.token != INVALID_HANDLE_VALUE)
493 ImpersonateLoggedOnUser (cygheap->user.token);
494 cygheap_setup_for_child_cleanup (newheap, &ch, 0);
498 /* Fixup the parent datastructure if needed and resume the child's
500 if (!cygheap->fdtab.need_fixup_before ())
501 cygheap_setup_for_child_cleanup (newheap, &ch, 0);
504 cygheap->fdtab.fixup_before_fork (pi.dwProcessId);
505 cygheap_setup_for_child_cleanup (newheap, &ch, 1);
506 ResumeThread (pi.hThread);
510 pinfo forked ((ch.cygpid != 1 ? ch.cygpid : cygwin_pid (pi.dwProcessId)), 1);
512 pinfo forked (cygwin_pid (pi.dwProcessId), 1);
515 /* Initialize things that are done later in dll_crt0_1 that aren't done
517 strcpy(forked->progname, myself->progname);
519 /* Restore impersonation */
520 if (cygheap->user.impersonated && cygheap->user.token != INVALID_HANDLE_VALUE)
521 ImpersonateLoggedOnUser (cygheap->user.token);
523 ProtectHandle (pi.hThread);
524 /* Protect the handle but name it similarly to the way it will
525 be called in subproc handling. */
526 ProtectHandle1 (pi.hProcess, childhProc);
528 /* Fill in fields in the child's process table entry. */
529 forked->hProcess = pi.hProcess;
530 forked->dwProcessId = pi.dwProcessId;
531 forked->copysigs(myself);
533 /* Hopefully, this will succeed. The alternative to doing things this
534 way is to reserve space prior to calling CreateProcess and then fill
535 it in afterwards. This requires more bookkeeping than I like, though,
536 so we'll just do it the easy way. So, terminate any child process if
537 we can't actually record the pid in the internal table. */
538 if (!forked.remember ())
540 TerminateProcess (pi.hProcess, 1);
545 slow_pid_reuse (pi.hProcess);
547 /* Wait for subproc to initialize itself. */
548 if (!sync_with_child (pi, subproc_ready, TRUE, "waiting for longjmp"))
551 /* CHILD IS STOPPED */
552 debug_printf ("child is alive (but stopped)");
554 /* Initialize, in order: data, bss, heap, stack, dll data, dll bss
555 Note: variables marked as NO_COPY will not be copied
556 since they are placed in a protected segment. */
560 rc = fork_copy (pi, "user/cygwin data",
561 user_data->data_start, user_data->data_end,
562 user_data->bss_start, user_data->bss_end,
563 cygheap->heapbase, cygheap->heapptr,
564 stack_here, ch.stackbottom,
565 dll_data_start, dll_data_end,
566 dll_bss_start, dll_bss_end, NULL);
568 __malloc_unlock (_reent_clib ());
573 /* Now fill data/bss of any DLLs that were linked into the program. */
574 for (dll *d = dlls.istart (DLL_LINK); d; d = dlls.inext ())
576 debug_printf ("copying data/bss of a linked dll");
577 if (!fork_copy (pi, "linked dll data/bss", d->p.data_start, d->p.data_end,
578 d->p.bss_start, d->p.bss_end,
583 /* Start thread, and wait for it to reload dlls. */
584 if (!resume_child (pi, forker_finished) ||
585 !sync_with_child (pi, subproc_ready, load_dlls, "child loading dlls"))
588 /* If DLLs were loaded in the parent, then the child has reloaded all
589 of them and is now waiting to have all of the individual data and
590 bss sections filled in. */
593 /* CHILD IS STOPPED */
594 /* write memory of reloaded dlls */
595 for (dll *d = dlls.istart (DLL_LOAD); d; d = dlls.inext ())
597 debug_printf ("copying data/bss for a loaded dll");
598 if (!fork_copy (pi, "loaded dll data/bss", d->p.data_start, d->p.data_end,
599 d->p.bss_start, d->p.bss_end,
603 /* Start the child up again. */
604 (void) resume_child (pi, forker_finished);
607 ForceCloseHandle (subproc_ready);
608 ForceCloseHandle (pi.hThread);
609 ForceCloseHandle (forker_finished);
610 forker_finished = NULL;
612 __pthread_atforkparent ();
616 /* Common cleanup code for failure cases */
618 /* Remember to de-allocate the fd table. */
620 ForceCloseHandle1 (pi.hProcess, childhProc);
622 ForceCloseHandle (pi.hThread);
624 ForceCloseHandle (subproc_ready);
626 ForceCloseHandle (forker_finished);
641 sigframe thisframe (mainthread);
643 debug_printf ("entering");
644 grouped.hParent = grouped.first_dll = NULL;
645 grouped.load_dlls = 0;
647 if (ISSTATE(myself, PID_SPLIT_HEAP))
649 system_printf ("The heap has been split, CYGWIN can't fork this process.");
650 system_printf ("Increase the heap_chunk_size in the registry and try again.");
652 syscall_printf ("-1 = fork (), split heap");
657 __asm__ volatile ("movl %%esp,%0": "=r" (esp));
659 myself->set_has_pgid_children ();
663 int res = setjmp (ch.jmp);
666 res = fork_child (grouped.hParent, grouped.first_dll, grouped.load_dlls);
668 res = fork_parent (grouped.hParent, grouped.first_dll, grouped.load_dlls, esp, ch);
671 syscall_printf ("%d = fork()", res);
679 if (!GetEnvironmentVariable ("CYGWIN_FORK_PIDS", buf, 1024))
683 for (p = buf; (pid = strtol (p, &pe, 10)); p = pe)
684 fork_pids[npid_max++] = pid;
689 /* Dummy function to force second assignment below to actually be
694 return vfork_storage.val ();
706 vfork_save *vf = get_vfork_val ();
710 vf = vfork_storage.create ();
717 __asm__ volatile ("movl %%esp,%0": "=r" (vf->vfork_esp):);
718 __asm__ volatile ("movl %%ebp,%0": "=r" (vf->vfork_ebp):);
719 for (pp = (char **)vf->frame, esp = vf->vfork_esp;
720 esp <= vf->vfork_ebp + 2; pp++, esp++)
722 int res = cygheap->fdtab.vfork_child_dup () ? 0 : -1;
723 debug_printf ("%d = vfork()", res);
724 debug_printf ("exiting vfork, res %d", res);
728 vf = get_vfork_val ();
730 for (pp = (char **)vf->frame, esp = vf->vfork_esp;
731 esp <= vf->vfork_ebp + 2; pp++, esp++)
734 thisframe.init (mainthread);
735 cygheap->fdtab.vfork_parent_restore ();
739 int exitval = -vf->pid;
741 if ((vf->pid = fork ()) == 0)
747 debug_printf ("exiting vfork, pid %d", pid);
748 sig_dispatch_pending ();