1 /* sigproc.cc: inter/intra signal and sub process handler
3 This file is part of Cygwin.
5 This software is a copyrighted work licensed under the terms of the
6 Cygwin license. Please consult the file "CYGWIN_LICENSE" for
10 #include "miscfuncs.h"
12 #include <sys/cygwin.h>
19 #include "child_info_magic.h"
20 #include "shared_info.h"
23 #include "exception.h"
28 #define WSSC 60000 // Wait for signal completion
29 #define WPSP 40000 // Wait for proc_subproc mutex
34 struct sigaction
*global_sigs
;
39 bool no_thread_exit_protect::flag
;
41 /* Flag to sig_send that signal goes to current process but no wait is
43 char NO_COPY myself_nowait_dummy
[1] = {'0'};
45 #define Static static NO_COPY
47 /* All my children info. Avoid expensive constructor ops at DLL
50 This class can allocate memory. But there's no need to free it
51 because only one instance of the class is created per process. */
54 static const int _NPROCS
= 256;
55 static const int _NPROCS_2
= 1023;
57 static const int _NPROCS
= 1024;
58 static const int _NPROCS_2
= 4095;
61 uint8_t _procs
[_NPROCS
* sizeof (pinfo
)] __attribute__ ((__aligned__
));
64 int count () const { return _count
; }
65 int add_one () { return ++_count
; }
66 int del_one () { return --_count
; }
67 int reset () { return _count
= 0; }
68 pinfo
&operator[] (int idx
)
74 /* Use HeapAlloc to avoid propagating this memory area
75 to the child processes. */
76 _procs_2
= (pinfo
*) HeapAlloc (GetProcessHeap (),
77 HEAP_GENERATE_EXCEPTIONS
79 (_NPROCS_2
+ 1) * sizeof (pinfo
));
81 return _procs_2
[idx
- _NPROCS
];
83 return ((pinfo
*) _procs
)[idx
];
85 int max_child_procs () const { return _NPROCS
+ _NPROCS_2
; }
87 Static child_procs chld_procs
;
89 /* Start of queue for waiting threads. */
90 Static waitq waitq_head
;
92 /* Controls access to subproc stuff. */
93 Static muto sync_proc_subproc
;
95 _cygtls NO_COPY
*_sig_tls
;
97 Static HANDLE my_sendsig
;
98 Static HANDLE my_readsig
;
100 /* Used in select if a signalfd is part of the read descriptor set */
101 HANDLE NO_COPY my_pendingsigs_evt
;
103 /* Function declarations */
104 static int __reg1
checkstate (waitq
*);
105 static __inline__
bool get_proc_lock (DWORD
, DWORD
);
106 static int remove_proc (int);
107 static bool stopped_or_terminated (waitq
*, _pinfo
*);
108 static void WINAPI
wait_sig (VOID
*arg
);
110 /* wait_sig bookkeeping */
112 class pending_signals
114 sigpacket sigs
[_NSIG
+ 1];
119 void add (sigpacket
&);
120 bool pending () {retry
= true; return !!start
.next
;}
121 void clear (int sig
) {sigs
[sig
].si
.si_signo
= 0;}
122 void clear (_cygtls
*tls
);
123 friend void __reg1
sig_dispatch_pending (bool);
124 friend void WINAPI
wait_sig (VOID
*arg
);
127 Static pending_signals sigq
;
133 cygheap
->sigs
= global_sigs
=
134 (struct sigaction
*) ccalloc_abort (HEAP_SIGS
, _NSIG
, sizeof (struct sigaction
));
135 global_sigs
[SIGSTOP
].sa_flags
= SA_RESTART
| SA_NODEFER
;
139 signal_fixup_after_exec ()
141 global_sigs
= cygheap
->sigs
;
142 /* Set up child's signal handlers */
143 for (int i
= 0; i
< _NSIG
; i
++)
145 global_sigs
[i
].sa_mask
= 0;
146 if (global_sigs
[i
].sa_handler
!= SIG_IGN
)
148 global_sigs
[i
].sa_handler
= SIG_DFL
;
149 global_sigs
[i
].sa_flags
&= ~ SA_SIGINFO
;
154 /* Get the sync_proc_subproc muto to control access to
155 * children, proc arrays.
156 * Attempt to handle case where process is exiting as we try to grab
160 get_proc_lock (DWORD what
, DWORD val
)
162 if (!cygwin_finished_initializing
)
164 Static
int lastwhat
= -1;
165 if (!sync_proc_subproc
)
167 sigproc_printf ("sync_proc_subproc is NULL");
170 if (sync_proc_subproc
.acquire (WPSP
))
175 system_printf ("Couldn't acquire %s for(%d,%d), last %d, %E",
176 sync_proc_subproc
.name
, what
, val
, lastwhat
);
181 proc_can_be_signalled (_pinfo
*p
)
183 if (!(p
->exitcode
& EXITCODE_SET
))
185 if (ISSTATE (p
, PID_INITIALIZING
) ||
186 (((p
)->process_state
& (PID_ACTIVE
| PID_IN_USE
)) ==
187 (PID_ACTIVE
| PID_IN_USE
)))
196 pid_exists (pid_t pid
)
199 return p
&& p
->exists ();
202 /* Return true if this is one of our children, false otherwise. */
206 for (int i
= 0; i
< chld_procs
.count (); i
++)
207 if (chld_procs
[i
]->pid
== pid
)
212 /* Handle all subprocess requests
215 proc_subproc (DWORD what
, uintptr_t val
)
223 #define wval ((waitq *) val)
224 #define vchild (*((pinfo *) val))
226 sigproc_printf ("args: %x, %d", what
, val
);
228 if (!get_proc_lock (what
, val
)) // Serialize access to this function
230 system_printf ("couldn't get proc lock. what %d, val %d", what
, val
);
236 /* Add a new subprocess to the children arrays.
237 * (usually called from the main thread)
240 /* Filled up process table? */
241 if (chld_procs
.count () >= chld_procs
.max_child_procs ())
243 sigproc_printf ("proc table overflow: hit %d processes, pid %d\n",
244 chld_procs
.count (), vchild
->pid
);
250 if (vchild
!= myself
)
252 vchild
->uid
= myself
->uid
;
253 vchild
->gid
= myself
->gid
;
254 vchild
->pgid
= myself
->pgid
;
255 vchild
->sid
= myself
->sid
;
256 vchild
->ctty
= myself
->ctty
;
257 vchild
->cygstarted
= true;
258 vchild
->process_state
|= PID_INITIALIZING
;
259 vchild
->ppid
= myself
->pid
; /* always set last */
263 case PROC_ATTACH_CHILD
:
264 slot
= chld_procs
.count ();
265 chld_procs
[slot
] = vchild
;
266 rc
= chld_procs
[slot
].wait ();
269 sigproc_printf ("added pid %d to proc table, slot %d", vchild
->pid
,
271 chld_procs
.add_one ();
275 /* Handle a wait4() operation. Allocates an event for the calling
276 * thread which is signaled when the appropriate pid exits or stops.
277 * (usually called from the main thread)
280 wval
->ev
= NULL
; // Don't know event flag yet
282 if (wval
->pid
!= -1 && wval
->pid
&& !mychild (wval
->pid
))
283 goto out
; // invalid pid. flag no such child
285 wval
->status
= 0; // Don't know status yet
286 sigproc_printf ("wval->pid %d, wval->options %d", wval
->pid
, wval
->options
);
288 /* If the first time for this thread, create a new event, otherwise
291 if ((wval
->ev
= wval
->thread_ev
) == NULL
)
293 wval
->ev
= wval
->thread_ev
= CreateEvent (&sec_none_nih
, TRUE
, FALSE
,
295 ProtectHandle1 (wval
->ev
, wq_ev
);
298 ResetEvent (wval
->ev
);
300 waitq_head
.next
= wval
; /* Add at the beginning. */
301 wval
->next
= w
; /* Link in rest of the list. */
305 case PROC_EXEC_CLEANUP
:
306 /* Cleanup backwards to eliminate redundant copying of chld_procs
307 array members inside remove_proc. */
308 while (chld_procs
.count ())
309 remove_proc (chld_procs
.count () - 1);
310 for (w
= &waitq_head
; w
->next
!= NULL
; w
= w
->next
)
311 CloseHandle (w
->next
->ev
);
314 /* Clear all waiting threads. Called from exceptions.cc prior to
315 the main thread's dispatch to a signal handler function.
316 (called from wait_sig thread) */
318 /* Clear all "wait"ing threads. */
320 sigproc_printf ("clear waiting threads");
322 sigproc_printf ("looking for processes to reap, count %d",
323 chld_procs
.count ());
327 /* Scan the linked list of wait()ing threads. If a wait's parameters
328 match this pid, then activate it. */
329 for (w
= &waitq_head
; w
->next
!= NULL
; w
= w
->next
)
331 if ((potential_match
= checkstate (w
)) > 0)
332 sigproc_printf ("released waiting thread");
333 else if (!clearing
&& !(w
->next
->options
& WNOHANG
) && potential_match
< 0)
334 sigproc_printf ("only found non-terminated children");
335 else if (potential_match
<= 0) // nothing matched
337 sigproc_printf ("waiting thread found no children");
338 HANDLE oldw
= w
->next
->ev
;
341 w
->next
->status
= -1; /* flag that a signal was received */
342 else if (!potential_match
|| !(w
->next
->options
& WNOHANG
))
344 if (!SetEvent (oldw
))
345 system_printf ("couldn't wake up wait event %p, %E", oldw
);
346 w
->next
= w
->next
->next
;
353 sigproc_printf ("finished processing terminated/stopped child");
356 waitq_head
.next
= NULL
;
357 sigproc_printf ("finished clearing");
360 if (global_sigs
[SIGCHLD
].sa_handler
== (void *) SIG_IGN
)
361 for (int i
= 0; i
< chld_procs
.count (); i
+= remove_proc (i
))
366 sync_proc_subproc
.release (); // Release the lock
368 sigproc_printf ("returning %d", rc
);
374 // FIXME: This is inelegant
376 _cygtls::remove_wq (DWORD wait
)
380 if (exit_state
< ES_FINAL
&& waitq_head
.next
&& sync_proc_subproc
381 && sync_proc_subproc
.acquire (wait
))
383 ForceCloseHandle1 (wq
.thread_ev
, wq_ev
);
385 for (waitq
*w
= &waitq_head
; w
->next
!= NULL
; w
= w
->next
)
391 sync_proc_subproc
.release ();
397 /* Terminate the wait_subproc thread.
398 Called on process exit.
399 Also called by spawn_guts to disassociate any subprocesses from this
400 process. Subprocesses will then know to clean up after themselves and
401 will not become chld_procs. */
405 sigproc_printf ("child_procs count %d", chld_procs
.count ());
406 if (chld_procs
.count ())
408 sync_proc_subproc
.acquire (WPSP
);
410 proc_subproc (PROC_CLEARWAIT
, 1);
412 /* Clean out proc processes from the pid list. */
413 for (int i
= 0; i
< chld_procs
.count (); i
++)
415 /* If we've execed then the execed process will handle setting ppid
416 to 1 iff it is a Cygwin process. */
417 if (!have_execed
|| !have_execed_cygwin
)
418 chld_procs
[i
]->ppid
= 1;
419 if (chld_procs
[i
].wait_thread
)
420 chld_procs
[i
].wait_thread
->terminate_thread ();
421 /* Release memory associated with this process unless it is 'myself'.
422 'myself' is only in the chld_procs table when we've execed. We
423 reach here when the next process has finished initializing but we
424 still can't free the memory used by 'myself' since it is used
425 later on during cygwin tear down. */
426 if (chld_procs
[i
] != myself
)
427 chld_procs
[i
].release ();
430 sync_proc_subproc
.release ();
432 sigproc_printf ("leaving");
435 /* Clear pending signal */
442 /* Clear pending signals of specific thread. Called under TLS lock from
443 _cygtls::remove_pending_sigs. */
445 pending_signals::clear (_cygtls
*tls
)
447 sigpacket
*q
= &start
, *qnext
;
449 while ((qnext
= q
->next
))
450 if (qnext
->sigtls
== tls
)
452 qnext
->si
.si_signo
= 0;
453 q
->next
= qnext
->next
;
459 /* Clear pending signals of specific thread. Called from _cygtls::remove */
461 _cygtls::remove_pending_sigs ()
467 sigpending (sigset_t
*mask
)
469 sigset_t outset
= sig_send (myself
, __SIGPENDING
, &_my_tls
);
470 if (outset
== SIG_BAD_MASK
)
476 /* Force the wait_sig thread to wake up and scan for pending signals */
478 sig_dispatch_pending (bool fast
)
480 /* Non-atomically test for any signals pending and wake up wait_sig if any are
481 found. It's ok if there's a race here since the next call to this function
483 if (sigq
.pending () && &_my_tls
!= _sig_tls
)
484 sig_send (myself
, fast
? __SIGFLUSHFAST
: __SIGFLUSH
);
487 /* Signal thread initialization. Called from dll_crt0_1.
488 This routine starts the signal handling thread. */
492 char char_sa_buf
[1024];
493 PSECURITY_ATTRIBUTES sa
= sec_user_nih ((PSECURITY_ATTRIBUTES
) char_sa_buf
, cygheap
->user
.sid());
494 DWORD err
= fhandler_pipe::create (sa
, &my_readsig
, &my_sendsig
,
495 _NSIG
* sizeof (sigpacket
), "sigwait",
500 api_fatal ("couldn't create signal pipe, %E");
502 ProtectHandle (my_readsig
);
503 myself
->sendsig
= my_sendsig
;
504 my_pendingsigs_evt
= CreateEvent (NULL
, TRUE
, FALSE
, NULL
);
505 if (!my_pendingsigs_evt
)
506 api_fatal ("couldn't create pending signal event, %E");
508 /* sync_proc_subproc is used by proc_subproc. It serializes
509 access to the children and proc arrays. */
510 sync_proc_subproc
.init ("sync_proc_subproc");
511 new cygthread (wait_sig
, cygself
, "sig");
514 /* Exit the current thread very carefully.
515 See cgf-000017 in DevNotes for more details on why this is
518 exit_thread (DWORD res
)
521 if (no_thread_exit_protect ())
523 sigfillset (&_my_tls
.sigmask
); /* No signals wanted */
525 /* CV 2014-11-21: Disable the code sending a signal. The problem with
526 this code is that it allows deadlocks under signal-rich multithreading
528 The original problem reported in 2012 couldn't be reproduced anymore,
529 even disabling this code. Tested on XP 32, Vista 32, W7 32, WOW64, 64,
532 lock_process for_now
; /* May block indefinitely when exiting. */
534 if (!DuplicateHandle (GetCurrentProcess (), GetCurrentThread (),
535 GetCurrentProcess (), &h
,
536 0, FALSE
, DUPLICATE_SAME_ACCESS
))
539 system_printf ("couldn't duplicate the current thread, %E");
544 ProtectHandle1 (h
, exit_thread
);
545 /* Tell wait_sig to wait for this thread to exit. It can then release
546 the lock below and close the above-opened handle. */
547 siginfo_t si
= {__SIGTHREADEXIT
, SI_KERNEL
};
549 sig_send (myself_nowait
, si
, &_my_tls
);
555 sig_send (_pinfo
*p
, int sig
, _cygtls
*tls
)
559 si
.si_code
= SI_KERNEL
;
560 return sig_send (p
, si
, tls
);
563 /* Send a signal to another process by raising its signal semaphore.
564 If pinfo *p == NULL, send to the current process.
565 If sending to this process, wait for notification that a signal has
566 completed before returning. */
568 sig_send (_pinfo
*p
, siginfo_t
& si
, _cygtls
*tls
)
574 bool communing
= si
.si_signo
== __SIGCOMMUNE
;
577 bool wait_for_completion
;
578 if (!(its_me
= p
== NULL
|| p
== myself
|| p
== myself_nowait
))
580 /* It is possible that the process is not yet ready to receive messages
581 * or that it has exited. Detect this.
583 if (!proc_can_be_signalled (p
)) /* Is the process accepting messages? */
585 sigproc_printf ("invalid pid %d(%x), signal %d",
586 p
->pid
, p
->process_state
, si
.si_signo
);
589 wait_for_completion
= false;
593 wait_for_completion
= p
!= myself_nowait
;
597 /* If myself is the stub process, send signal to the child process
598 rather than myself. The fact that myself->dwProcessId is not equal
599 to the current process id indicates myself is the stub process. */
600 if (its_me
&& myself
->dwProcessId
!= GetCurrentProcessId ())
602 wait_for_completion
= false;
607 sendsig
= my_sendsig
;
612 for (int i
= 0; !p
->sendsig
&& i
< 10000; i
++)
617 dwProcessId
= p
->dwProcessId
;
621 dupsig
= p
->exec_sendsig
;
622 dwProcessId
= p
->exec_dwProcessId
;
627 sigproc_printf ("sendsig handle never materialized");
630 HANDLE hp
= OpenProcess (PROCESS_DUP_HANDLE
, false, dwProcessId
);
634 sigproc_printf ("OpenProcess failed, %E");
638 if (!DuplicateHandle (hp
, dupsig
, GetCurrentProcess (), &sendsig
, 0,
639 false, DUPLICATE_SAME_ACCESS
) || !sendsig
)
642 sigproc_printf ("DuplicateHandle failed, %E");
646 VerifyHandle (sendsig
);
650 DWORD flag
= PIPE_NOWAIT
;
651 /* Set PIPE_NOWAIT here to avoid blocking when sending a signal.
652 (Yes, I know MSDN says not to use this)
653 We can't ever block here because it causes a deadlock when
654 debugging with gdb. */
655 BOOL res
= SetNamedPipeHandleState (sendsig
, &flag
, NULL
, NULL
);
656 sigproc_printf ("%d = SetNamedPipeHandleState (%y, PIPE_NOWAIT, NULL, NULL)", res
, sendsig
);
660 si
._si_commune
._si_process_handle
= hp
;
662 HANDLE
& tome
= si
._si_commune
._si_write_handle
;
663 HANDLE
& fromthem
= si
._si_commune
._si_read_handle
;
664 if (!CreatePipeOverlapped (&fromthem
, &tome
, &sec_all_nih
))
666 sigproc_printf ("CreatePipe for __SIGCOMMUNE failed, %E");
670 if (!DuplicateHandle (GetCurrentProcess (), tome
, hp
, &tome
, 0, false,
671 DUPLICATE_SAME_ACCESS
| DUPLICATE_CLOSE_SOURCE
))
673 sigproc_printf ("DuplicateHandle for __SIGCOMMUNE failed, %E");
680 sigproc_printf ("sendsig %p, pid %d, signal %d, its_me %d", sendsig
, p
->pid
,
681 si
.si_signo
, its_me
);
686 else if (si
.si_signo
== __SIGPENDING
)
687 pack
.mask
= &pending
;
688 else if (si
.si_signo
== __SIGFLUSH
|| si
.si_signo
> 0)
690 threadlist_t
*tl_entry
= cygheap
->find_tls (tls
? tls
: _main_tls
);
691 pack
.mask
= tls
? &tls
->sigmask
: &_main_tls
->sigmask
;
692 cygheap
->unlock_tls (tl_entry
);
699 pack
.si
.si_pid
= myself
->pid
;
701 pack
.si
.si_uid
= myself
->uid
;
702 pack
.pid
= myself
->pid
;
704 if (wait_for_completion
)
706 pack
.wakeup
= CreateEvent (&sec_none_nih
, FALSE
, FALSE
, NULL
);
707 sigproc_printf ("wakeup %p", pack
.wakeup
);
708 ProtectHandle (pack
.wakeup
);
713 if (!communing
|| !(si
._si_commune
._si_code
& PICOM_EXTRASTR
))
715 leader
= (char *) &pack
;
716 packsize
= sizeof (pack
);
720 size_t n
= strlen (si
._si_commune
._si_str
);
721 packsize
= sizeof (pack
) + sizeof (n
) + n
;
722 char *p
= leader
= (char *) alloca (packsize
);
723 memcpy (p
, &pack
, sizeof (pack
)); p
+= sizeof (pack
);
724 memcpy (p
, &n
, sizeof (n
)); p
+= sizeof (n
);
725 memcpy (p
, si
._si_commune
._si_str
, n
); p
+= n
;
730 /* Try multiple times to send if packsize != nb since that probably
731 means that the pipe buffer is full. */
732 for (int i
= 0; i
< 100; i
++)
734 res
= WriteFile (sendsig
, leader
, packsize
, &nb
, NULL
);
735 if (!res
|| packsize
== nb
)
743 /* Couldn't send to the pipe. This probably means that the
744 process is exiting. */
747 sigproc_printf ("WriteFile for pipe %p failed, %E", sendsig
);
748 ForceCloseHandle (sendsig
);
750 else if (!p
->exec_sendsig
&& !exit_state
)
751 system_printf ("error sending signal %d, pid %u, pipe handle %p, nb %u, packsize %u, %E",
752 si
.si_signo
, p
->pid
, sendsig
, nb
, packsize
);
753 if (GetLastError () == ERROR_BROKEN_PIPE
)
761 /* No need to wait for signal completion unless this was a signal to
764 If it was a signal to this process, wait for a dispatched signal.
765 Otherwise just wait for the wait_sig to signal that it has finished
766 processing the signal. */
767 if (wait_for_completion
)
769 sigproc_printf ("Waiting for pack.wakeup %p", pack
.wakeup
);
770 rc
= WaitForSingleObject (pack
.wakeup
, WSSC
);
771 ForceCloseHandle (pack
.wakeup
);
776 sigproc_printf ("Not waiting for sigcomplete. its_me %d signal %d",
777 its_me
, si
.si_signo
);
779 ForceCloseHandle (sendsig
);
783 if (rc
== WAIT_OBJECT_0
)
784 rc
= 0; // Successful exit
791 if (wait_for_completion
&& si
.si_signo
!= __SIGFLUSHFAST
)
792 _my_tls
.call_signal_handler ();
797 if (si
._si_commune
._si_process_handle
)
798 CloseHandle (si
._si_commune
._si_process_handle
);
799 if (si
._si_commune
._si_read_handle
)
800 CloseHandle (si
._si_commune
._si_read_handle
);
803 ForceCloseHandle (pack
.wakeup
);
804 if (si
.si_signo
!= __SIGPENDING
)
810 sigproc_printf ("returning %p from sending signal %d", rc
, si
.si_signo
);
814 int child_info::retry_count
= 0;
816 /* Initialize some of the memory block passed to child processes
817 by fork/spawn/exec. */
818 child_info::child_info (unsigned in_cb
, child_info_types chtype
,
819 bool need_subproc_ready
):
820 cb (in_cb
), intro (PROC_MAGIC_GENERIC
), magic (CHILD_INFO_MAGIC
),
821 type (chtype
), cygheap (::cygheap
), cygheap_max (::cygheap_max
),
822 flag (0), retry (child_info::retry_count
), rd_proc_pipe (NULL
),
825 /* It appears that when running under WOW64 on Vista 64, the first DWORD
826 value in the datastructure lpReserved2 is pointing to (msv_count in
827 Cygwin), has to reflect the size of that datastructure as used in the
828 Microsoft C runtime (a count value, counting the number of elements in
829 two subsequent arrays, BYTE[count and HANDLE[count]), even though the C
830 runtime isn't used. Otherwise, if msv_count is 0 or too small, the
831 datastructure gets overwritten.
833 This seems to be a bug in Vista's WOW64, which apparently copies the
834 lpReserved2 datastructure not using the cbReserved2 size information,
835 but using the information given in the first DWORD within lpReserved2
836 instead. However, it's not clear if a non-0 count doesn't result in
837 trying to evaluate the content, so we do this really only for Vista 64.
839 The value is sizeof (child_info_*) / 5 which results in a count which
840 covers the full datastructure, plus not more than 4 extra bytes. This
841 is ok as long as the child_info structure is cosily stored within a bigger
843 msv_count
= wincap
.needs_count_in_si_lpres2 () ? in_cb
/ 5 : 0;
845 fhandler_union_cb
= sizeof (fhandler_union
);
846 user_h
= cygwin_user_h
;
847 if (strace
.active ())
852 /* Only propagate _CI_STRACED to child if strace is actually tracing
853 child processes of this process. The undocumented ProcessDebugFlags
854 returns 0 if EPROCESS->NoDebugInherit is TRUE, 1 otherwise.
855 This avoids a hang when stracing a forking or spawning process
856 with the -f flag set to "don't follow fork". */
857 status
= NtQueryInformationProcess (GetCurrentProcess (),
858 ProcessDebugFlags
, &DebugFlags
,
859 sizeof (DebugFlags
), NULL
);
860 if (NT_SUCCESS (status
) && DebugFlags
)
863 if (need_subproc_ready
)
865 subproc_ready
= CreateEvent (&sec_all
, FALSE
, FALSE
, NULL
);
866 flag
|= _CI_ISCYGWIN
;
868 sigproc_printf ("subproc_ready %p", subproc_ready
);
869 /* Create an inheritable handle to pass to the child process. This will
870 allow the child to copy cygheap etc. from the parent to itself. If
871 we're forking, we also need handle duplicate access. */
873 DWORD perms
= PROCESS_QUERY_LIMITED_INFORMATION
| PROCESS_VM_READ
874 | PROCESS_VM_OPERATION
| SYNCHRONIZE
;
875 if (type
== _CH_FORK
)
877 perms
|= PROCESS_DUP_HANDLE
;
878 /* VirtualQueryEx is documented to require PROCESS_QUERY_INFORMATION.
879 That's true for Windows 7, but PROCESS_QUERY_LIMITED_INFORMATION
880 appears to be sufficient on Windows 8 and later. */
881 if (wincap
.needs_query_information ())
882 perms
|= PROCESS_QUERY_INFORMATION
;
885 if (!DuplicateHandle (GetCurrentProcess (), GetCurrentProcess (),
886 GetCurrentProcess (), &parent
, perms
, TRUE
, 0))
887 system_printf ("couldn't create handle to myself for child, %E");
890 child_info::~child_info ()
895 child_info_fork::child_info_fork () :
896 child_info (sizeof *this, _CH_FORK
, true),
897 forker_finished (NULL
)
901 child_info_spawn::child_info_spawn (child_info_types chtype
, bool need_subproc_ready
) :
902 child_info (sizeof *this, chtype
, need_subproc_ready
)
904 if (type
== _CH_EXEC
)
909 else if (!(ev
= CreateEvent (&sec_none_nih
, false, false, NULL
)))
910 api_fatal ("couldn't create signalling event for exec, %E");
912 get_proc_lock (PROC_EXECING
, 0);
913 /* exit with lock held */
918 cygheap_exec_info::alloc ()
920 cygheap_exec_info
*res
=
921 (cygheap_exec_info
*) ccalloc_abort (HEAP_1_EXEC
, 1,
922 sizeof (cygheap_exec_info
)
923 + (chld_procs
.count ()
924 * sizeof (children
[0])));
925 res
->sigmask
= _my_tls
.sigmask
;
930 child_info_spawn::wait_for_myself ()
933 if (myself
.remember ())
935 WaitForSingleObject (ev
, INFINITE
);
939 child_info::cleanup ()
943 CloseHandle (subproc_ready
);
944 subproc_ready
= NULL
;
948 CloseHandle (parent
);
953 ForceCloseHandle (rd_proc_pipe
);
958 ForceCloseHandle (wr_proc_pipe
);
964 child_info_spawn::cleanup ()
970 for (char **e
= moreinfo
->envp
; *e
; e
++)
972 cfree (moreinfo
->envp
);
974 if (type
!= _CH_SPAWN
&& moreinfo
->myself_pinfo
)
975 CloseHandle (moreinfo
->myself_pinfo
);
984 if (type
== _CH_EXEC
)
986 if (iscygwin () && hExeced
)
987 proc_subproc (PROC_EXEC_CLEANUP
, 0);
988 sync_proc_subproc
.release ();
991 child_info::cleanup ();
994 /* Record any non-reaped subprocesses to be passed to about-to-be-execed
995 process. FIXME: There is a race here if the process exits while we
998 cygheap_exec_info::record_children ()
1000 for (nchildren
= 0; nchildren
< chld_procs
.count (); nchildren
++)
1002 children
[nchildren
].pid
= chld_procs
[nchildren
]->pid
;
1003 children
[nchildren
].p
= chld_procs
[nchildren
];
1004 /* Set inheritance of required child handles for reattach_children
1005 in the about-to-be-execed process. */
1006 children
[nchildren
].p
.set_inheritance (true);
1011 child_info_spawn::record_children ()
1013 if (type
== _CH_EXEC
&& iscygwin ())
1014 moreinfo
->record_children ();
1017 /* Reattach non-reaped subprocesses passed in from the cygwin process
1018 which previously operated under this pid. FIXME: Is there a race here
1019 if the process exits during cygwin's exec handoff? */
1021 cygheap_exec_info::reattach_children (HANDLE parent
)
1023 for (int i
= 0; i
< nchildren
; i
++)
1025 pinfo
p (parent
, children
[i
].p
, children
[i
].pid
);
1027 debug_only_printf ("couldn't reattach child %d from previous process", children
[i
].pid
);
1028 else if (!p
.attach ())
1029 debug_only_printf ("attach of child process %d failed", children
[i
].pid
);
1031 debug_only_printf ("reattached pid %d<%u>, process handle %p, rd_proc_pipe %p->%p",
1032 p
->pid
, p
->dwProcessId
, p
.hProcess
,
1033 children
[i
].p
.rd_proc_pipe
, p
.rd_proc_pipe
);
1038 child_info_spawn::reattach_children ()
1040 moreinfo
->reattach_children (parent
);
1044 child_info::ready (bool execed
)
1048 sigproc_printf ("subproc_ready not set");
1052 if (dynamically_loaded
)
1053 sigproc_printf ("not really ready");
1054 else if (!SetEvent (subproc_ready
))
1055 api_fatal ("SetEvent failed, %E");
1057 sigproc_printf ("signalled %p that I was ready", subproc_ready
);
1061 CloseHandle (subproc_ready
);
1062 subproc_ready
= NULL
;
1067 child_info::sync (pid_t pid
, HANDLE
& hProcess
, DWORD howlong
)
1072 unsigned nsubproc_ready
;
1075 nsubproc_ready
= WAIT_OBJECT_0
+ 3;
1078 w4
[n
++] = subproc_ready
;
1083 sigproc_printf ("n %d, waiting for subproc_ready(%p) and child process(%p)", n
, w4
[0], w4
[1]);
1084 DWORD x
= WaitForMultipleObjects (n
, w4
, FALSE
, howlong
);
1088 system_printf ("wait failed, pid %u, %E", pid
);
1093 if (x
!= nsubproc_ready
)
1096 GetExitCodeProcess (hProcess
, &exit_code
);
1101 exit_code
= STILL_ACTIVE
;
1102 if (type
== _CH_EXEC
&& my_wr_proc_pipe
)
1104 ForceCloseHandle1 (hProcess
, childhProc
);
1108 sigproc_printf ("pid %u, WFMO returned %d, exit_code %y, res %d", pid
, x
,
1115 child_info::proc_retry (HANDLE h
)
1119 sigproc_printf ("exit_code %y", exit_code
);
1122 case STILL_ACTIVE
: /* shouldn't happen */
1123 sigproc_printf ("STILL_ACTIVE? How'd we get here?");
1125 case STATUS_DLL_NOT_FOUND
:
1126 case STATUS_ACCESS_VIOLATION
:
1127 case STATUS_ILLEGAL_INSTRUCTION
:
1128 case STATUS_ILLEGAL_DLL_PSEUDO_RELOCATION
: /* pseudo-reloc.c specific */
1130 case STATUS_CONTROL_C_EXIT
:
1134 case STATUS_DLL_INIT_FAILED
:
1135 case STATUS_DLL_INIT_FAILED_LOGOFF
:
1136 case EXITCODE_RETRY
:
1140 case EXITCODE_FORK_FAILED
: /* windows prevented us from forking */
1143 /* Count down non-recognized exit codes more quickly since they aren't
1144 due to known conditions. */
1146 if (!iscygwin () && (exit_code
& 0xffff0000) != 0xc0000000)
1148 if ((retry
-= 2) < 0)
1154 ForceCloseHandle1 (h
, childhProc
);
1159 child_info_fork::abort (const char *fmt
, ...)
1166 strace_vprintf (DEBUG
, fmt
, ap
);
1168 strace_vprintf (SYSTEM
, fmt
, ap
);
1169 TerminateProcess (GetCurrentProcess (), EXITCODE_FORK_FAILED
);
1172 TerminateProcess (GetCurrentProcess (), EXITCODE_RETRY
);
1176 /* Check the state of all of our children to see if any are stopped or
1180 checkstate (waitq
*parent_w
)
1182 int potential_match
= 0;
1184 sigproc_printf ("child_procs count %d", chld_procs
.count ());
1186 /* Check already dead processes first to see if they match the criteria
1187 * given in w->next. */
1189 for (int i
= 0; i
< chld_procs
.count (); i
++)
1190 if ((res
= stopped_or_terminated (parent_w
, chld_procs
[i
])))
1193 potential_match
= 1;
1197 sigproc_printf ("no matching terminated children found");
1198 potential_match
= -!!chld_procs
.count ();
1201 sigproc_printf ("returning %d", potential_match
);
1202 return potential_match
;
1205 /* Remove a proc from chld_procs by swapping it with the last child in the list.
1206 Also releases shared memory of exited processes. */
1208 remove_proc (int ci
)
1212 if (_my_tls
._ctinfo
!= chld_procs
[ci
].wait_thread
)
1213 chld_procs
[ci
].wait_thread
->terminate_thread ();
1215 else if (chld_procs
[ci
] && chld_procs
[ci
]->exists ())
1218 sigproc_printf ("removing chld_procs[%d], pid %d, child_procs count %d",
1219 ci
, chld_procs
[ci
]->pid
, chld_procs
.count ());
1220 if (chld_procs
[ci
] != myself
)
1221 chld_procs
[ci
].release ();
1222 if (ci
< chld_procs
.del_one ())
1224 /* Wait for proc_waiter thread to make a copy of this element before
1225 moving it or it may become confused. The chances are very high that
1226 the proc_waiter thread has already done this by the time we
1228 if (!have_execed
&& !exit_state
)
1229 while (!chld_procs
[chld_procs
.count ()].waiter_ready
)
1231 chld_procs
[ci
] = chld_procs
[chld_procs
.count ()];
1236 /* Check status of child process vs. waitq member.
1238 parent_w is the pointer to the parent of the waitq member in question.
1239 child is the subprocess being considered.
1241 Returns non-zero if waiting thread released. */
1243 stopped_or_terminated (waitq
*parent_w
, _pinfo
*child
)
1246 waitq
*w
= parent_w
->next
;
1248 sigproc_printf ("considering pid %d, pgid %d, w->pid %d", child
->pid
, child
->pgid
, w
->pid
);
1251 else if (w
->pid
== 0)
1252 might_match
= child
->pgid
== myself
->pgid
;
1253 else if (w
->pid
< 0)
1254 might_match
= child
->pgid
== -w
->pid
;
1256 might_match
= (w
->pid
== child
->pid
);
1263 if (!((terminated
= (child
->process_state
== PID_EXITED
))
1264 || ((w
->options
& WCONTINUED
) && child
->stopsig
== SIGCONT
)
1265 || ((w
->options
& WUNTRACED
) && child
->stopsig
&& child
->stopsig
!= SIGCONT
)))
1268 parent_w
->next
= w
->next
; /* successful wait. remove from wait queue */
1269 w
->pid
= child
->pid
;
1273 sigproc_printf ("stopped child, stop signal %d", child
->stopsig
);
1274 if (child
->stopsig
== SIGCONT
)
1275 w
->status
= __W_CONTINUED
;
1277 w
->status
= (child
->stopsig
<< 8) | 0x7f;
1282 child
->process_state
= PID_REAPED
;
1283 w
->status
= (__uint16_t
) child
->exitcode
;
1285 add_rusage (&myself
->rusage_children
, &child
->rusage_children
);
1286 add_rusage (&myself
->rusage_children
, &child
->rusage_self
);
1290 add_rusage ((struct rusage
*) w
->rusage
, &child
->rusage_children
);
1291 add_rusage ((struct rusage
*) w
->rusage
, &child
->rusage_self
);
1295 if (!SetEvent (w
->ev
)) /* wake up wait4 () immediately */
1296 system_printf ("couldn't wake up wait event %p, %E", w
->ev
);
1301 talktome (siginfo_t
*si
)
1303 unsigned size
= sizeof (*si
);
1304 sigproc_printf ("pid %d wants some information", si
->si_pid
);
1305 if (si
->_si_commune
._si_code
& PICOM_EXTRASTR
)
1309 if (!ReadFile (my_readsig
, &n
, sizeof (n
), &nb
, NULL
) || nb
!= sizeof (n
))
1311 siginfo_t
*newsi
= (siginfo_t
*) alloca (size
+= n
+ 1);
1313 newsi
->_si_commune
._si_str
= (char *) (newsi
+ 1);
1314 if (!ReadFile (my_readsig
, newsi
->_si_commune
._si_str
, n
, &nb
, NULL
) || nb
!= n
)
1316 newsi
->_si_commune
._si_str
[n
] = '\0';
1320 pinfo
pi (si
->si_pid
);
1322 new cygthread (commune_process
, size
, si
, "commune");
1325 /* Add a packet to the beginning of the queue.
1326 Should only be called from signal thread. */
1328 pending_signals::add (sigpacket
& pack
)
1332 se
= sigs
+ pack
.si
.si_signo
;
1333 if (se
->si
.si_signo
)
1336 se
->next
= start
.next
;
1340 /* Process signals by waiting for signal data to arrive in a pipe.
1341 Set a completion event if one was specified. */
1345 _sig_tls
= &_my_tls
;
1346 bool sig_held
= false;
1348 sigproc_printf ("entering ReadFile loop, my_readsig %p, my_sendsig %p",
1349 my_readsig
, my_sendsig
);
1351 hntdll
= GetModuleHandle ("ntdll.dll");
1356 sigpacket pack
= {};
1358 pack
.si
.si_signo
= __SIGFLUSH
;
1359 else if (!ReadFile (my_readsig
, &pack
, sizeof (pack
), &nb
, NULL
))
1360 Sleep (INFINITE
); /* Assume were exiting. Never exit this thread */
1361 else if (nb
!= sizeof (pack
) || !pack
.si
.si_signo
)
1363 system_printf ("garbled signal pipe data nb %u, sig %d", nb
, pack
.si
.si_signo
);
1368 /* Don't process signals when we start exiting */
1369 if (exit_state
> ES_EXIT_STARTING
&& pack
.si
.si_signo
> 0)
1370 goto skip_process_signal
;
1372 sigset_t dummy_mask
;
1373 threadlist_t
*tl_entry
;
1376 tl_entry
= cygheap
->find_tls (_main_tls
);
1377 dummy_mask
= _main_tls
->sigmask
;
1378 cygheap
->unlock_tls (tl_entry
);
1379 pack
.mask
= &dummy_mask
;
1386 switch (pack
.si
.si_signo
)
1389 talktome (&pack
.si
);
1392 strace
.activate (false);
1399 tl_entry
= cygheap
->find_tls (pack
.sigtls
);
1400 while ((q
= q
->next
))
1402 /* Skip thread-specific signals for other threads. */
1403 if (q
->sigtls
&& pack
.sigtls
!= q
->sigtls
)
1405 if (pack
.sigtls
->sigmask
& (bit
= SIGTOMASK (q
->si
.si_signo
)))
1408 cygheap
->unlock_tls (tl_entry
);
1415 init_console_handler (true);
1417 case __SIGTHREADEXIT
:
1419 /* Serialize thread exit as the thread exit code can be interpreted
1420 as the process exit code in some cases when racing with
1421 ExitProcess/TerminateProcess.
1422 So, wait for the thread which sent this signal to exit, then
1423 release the process lock which it held and close it's handle.
1424 See cgf-000017 in DevNotes for more details.
1426 HANDLE h
= (HANDLE
) pack
.si
.si_cyg
;
1427 DWORD res
= WaitForSingleObject (h
, 5000);
1428 lock_process::force_release (pack
.sigtls
);
1429 ForceCloseHandle1 (h
, exit_thread
);
1430 if (res
!= WAIT_OBJECT_0
)
1435 system_printf ("WaitForSingleObject(%p) for thread exit returned %u", h
, res
);
1439 default: /* Normal (positive) signal */
1440 if (pack
.si
.si_signo
< 0)
1441 sig_clear (-pack
.si
.si_signo
);
1449 case __SIGFLUSHFAST
:
1453 /* Check the queue for signals. There will always be at least one
1454 thing on the queue if this was a valid signal. */
1455 while ((qnext
= q
->next
))
1457 if (qnext
->si
.si_signo
&& qnext
->process () <= 0)
1461 q
->next
= qnext
->next
;
1462 qnext
->si
.si_signo
= 0;
1465 /* At least one signal still queued? The event is used in select
1466 only, and only to decide if WFMO should wake up in case a
1467 signalfd is waiting via select/poll for being ready to read a
1468 pending signal. This method wakes up all threads hanging in
1469 select and having a signalfd, as soon as a pending signal is
1470 available, but it's certainly better than constant polling. */
1471 if (sigq
.start
.next
)
1472 SetEvent (my_pendingsigs_evt
);
1474 ResetEvent (my_pendingsigs_evt
);
1475 if (pack
.si
.si_signo
== SIGCHLD
)
1480 if (clearwait
&& !have_execed
)
1481 proc_subproc (PROC_CLEARWAIT
, 0);
1482 skip_process_signal
:
1485 sigproc_printf ("signalling pack.wakeup %p", pack
.wakeup
);
1486 SetEvent (pack
.wakeup
);