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 /* All my children info. Avoid expensive constructor ops at DLL
48 This class can allocate memory. But there's no need to free it
49 because only one instance of the class is created per process. */
51 static const int _NPROCS
= 1024;
52 static const int _NPROCS_2
= 4095;
54 uint8_t _procs
[_NPROCS
* sizeof (pinfo
)] __attribute__ ((__aligned__
));
57 int count () const { return _count
; }
58 int add_one () { return ++_count
; }
59 int del_one () { return --_count
; }
60 int reset () { return _count
= 0; }
61 pinfo
&operator[] (int idx
)
67 /* Use HeapAlloc to avoid propagating this memory area
68 to the child processes. */
69 _procs_2
= (pinfo
*) HeapAlloc (GetProcessHeap (),
70 HEAP_GENERATE_EXCEPTIONS
72 (_NPROCS_2
+ 1) * sizeof (pinfo
));
74 return _procs_2
[idx
- _NPROCS
];
76 return ((pinfo
*) _procs
)[idx
];
78 int max_child_procs () const { return _NPROCS
+ _NPROCS_2
; }
80 static NO_COPY child_procs chld_procs
;
82 /* Start of queue for waiting threads. */
83 static NO_COPY waitq waitq_head
;
85 /* Controls access to subproc stuff. */
86 static NO_COPY muto sync_proc_subproc
;
88 _cygtls NO_COPY
*_sig_tls
;
90 static NO_COPY HANDLE my_sendsig
;
91 static NO_COPY HANDLE my_readsig
;
93 /* Used in select if a signalfd is part of the read descriptor set */
94 HANDLE NO_COPY my_pendingsigs_evt
;
96 /* Function declarations */
97 static int checkstate (waitq
*);
98 static __inline__
bool get_proc_lock (DWORD
, DWORD
);
99 static int remove_proc (int);
100 static bool stopped_or_terminated (waitq
*, _pinfo
*);
101 static void wait_sig (VOID
*arg
);
103 /* wait_sig bookkeeping */
105 class pending_signals
107 sigpacket sigs
[_NSIG
+ 1];
112 void add (sigpacket
&);
113 bool pending () {retry
= true; return !!start
.next
;}
114 void clear (int sig
) {sigs
[sig
].si
.si_signo
= 0;}
115 void clear (_cygtls
*tls
);
116 friend void sig_dispatch_pending (bool);
117 friend void wait_sig (VOID
*arg
);
120 static NO_COPY pending_signals sigq
;
126 cygheap
->sigs
= global_sigs
=
127 (struct sigaction
*) ccalloc_abort (HEAP_SIGS
, _NSIG
, sizeof (struct sigaction
));
128 global_sigs
[SIGSTOP
].sa_flags
= SA_RESTART
| SA_NODEFER
;
132 signal_fixup_after_exec ()
134 global_sigs
= cygheap
->sigs
;
135 /* Set up child's signal handlers */
136 for (int i
= 0; i
< _NSIG
; i
++)
138 global_sigs
[i
].sa_mask
= 0;
139 if (global_sigs
[i
].sa_handler
!= SIG_IGN
)
141 global_sigs
[i
].sa_handler
= SIG_DFL
;
142 global_sigs
[i
].sa_flags
&= ~ SA_SIGINFO
;
147 /* Get the sync_proc_subproc muto to control access to
148 * children, proc arrays.
149 * Attempt to handle case where process is exiting as we try to grab
153 get_proc_lock (DWORD what
, DWORD val
)
155 if (!cygwin_finished_initializing
)
157 static NO_COPY
int lastwhat
= -1;
158 if (!sync_proc_subproc
)
160 sigproc_printf ("sync_proc_subproc is NULL");
163 if (sync_proc_subproc
.acquire (WPSP
))
168 system_printf ("Couldn't acquire %s for(%d,%d), last %d, %E",
169 sync_proc_subproc
.name
, what
, val
, lastwhat
);
174 proc_can_be_signalled (_pinfo
*p
)
176 if (!(p
->exitcode
& EXITCODE_SET
))
178 if (ISSTATE (p
, PID_INITIALIZING
) ||
179 (((p
)->process_state
& (PID_ACTIVE
| PID_IN_USE
)) ==
180 (PID_ACTIVE
| PID_IN_USE
)))
189 pid_exists (pid_t pid
)
192 return p
&& p
->exists ();
195 /* Return true if this is one of our children, false otherwise. */
199 for (int i
= 0; i
< chld_procs
.count (); i
++)
200 if (chld_procs
[i
]->pid
== pid
)
205 /* Handle all subprocess requests
208 proc_subproc (DWORD what
, uintptr_t val
)
216 #define wval ((waitq *) val)
217 #define vchild (*((pinfo *) val))
219 sigproc_printf ("args: %x, %d", what
, val
);
221 if (!get_proc_lock (what
, val
)) // Serialize access to this function
223 system_printf ("couldn't get proc lock. what %d, val %d", what
, val
);
229 /* Add a new subprocess to the children arrays.
230 * (usually called from the main thread)
233 /* Filled up process table? */
234 if (chld_procs
.count () >= chld_procs
.max_child_procs ())
236 sigproc_printf ("proc table overflow: hit %d processes, pid %d\n",
237 chld_procs
.count (), vchild
->pid
);
243 if (vchild
!= myself
)
245 vchild
->uid
= myself
->uid
;
246 vchild
->gid
= myself
->gid
;
247 vchild
->pgid
= myself
->pgid
;
248 vchild
->sid
= myself
->sid
;
249 vchild
->ctty
= myself
->ctty
;
250 vchild
->cygstarted
= true;
251 vchild
->process_state
|= PID_INITIALIZING
;
252 vchild
->ppid
= myself
->pid
; /* always set last */
256 case PROC_ATTACH_CHILD
:
257 slot
= chld_procs
.count ();
258 chld_procs
[slot
] = vchild
;
259 rc
= chld_procs
[slot
].wait ();
262 sigproc_printf ("added pid %d to proc table, slot %d", vchild
->pid
,
264 chld_procs
.add_one ();
268 /* Handle a wait4() operation. Allocates an event for the calling
269 * thread which is signaled when the appropriate pid exits or stops.
270 * (usually called from the main thread)
273 wval
->ev
= NULL
; // Don't know event flag yet
275 if (wval
->pid
!= -1 && wval
->pid
&& !mychild (wval
->pid
))
276 goto out
; // invalid pid. flag no such child
278 wval
->status
= 0; // Don't know status yet
279 sigproc_printf ("wval->pid %d, wval->options %d", wval
->pid
, wval
->options
);
281 /* If the first time for this thread, create a new event, otherwise
284 if ((wval
->ev
= wval
->thread_ev
) == NULL
)
286 wval
->ev
= wval
->thread_ev
= CreateEvent (&sec_none_nih
, TRUE
, FALSE
,
288 ProtectHandle1 (wval
->ev
, wq_ev
);
291 ResetEvent (wval
->ev
);
293 waitq_head
.next
= wval
; /* Add at the beginning. */
294 wval
->next
= w
; /* Link in rest of the list. */
298 case PROC_EXEC_CLEANUP
:
299 /* Cleanup backwards to eliminate redundant copying of chld_procs
300 array members inside remove_proc. */
301 while (chld_procs
.count ())
302 remove_proc (chld_procs
.count () - 1);
303 for (w
= &waitq_head
; w
->next
!= NULL
; w
= w
->next
)
304 CloseHandle (w
->next
->ev
);
307 /* Clear all waiting threads. Called from exceptions.cc prior to
308 the main thread's dispatch to a signal handler function.
309 (called from wait_sig thread) */
311 /* Clear all "wait"ing threads. */
313 sigproc_printf ("clear waiting threads");
315 sigproc_printf ("looking for processes to reap, count %d",
316 chld_procs
.count ());
320 /* Scan the linked list of wait()ing threads. If a wait's parameters
321 match this pid, then activate it. */
322 for (w
= &waitq_head
; w
->next
!= NULL
; w
= w
->next
)
324 if ((potential_match
= checkstate (w
)) > 0)
325 sigproc_printf ("released waiting thread");
326 else if (!clearing
&& !(w
->next
->options
& WNOHANG
) && potential_match
< 0)
327 sigproc_printf ("only found non-terminated children");
328 else if (potential_match
<= 0) // nothing matched
330 sigproc_printf ("waiting thread found no children");
331 HANDLE oldw
= w
->next
->ev
;
334 w
->next
->status
= -1; /* flag that a signal was received */
335 else if (!potential_match
|| !(w
->next
->options
& WNOHANG
))
337 if (!SetEvent (oldw
))
338 system_printf ("couldn't wake up wait event %p, %E", oldw
);
339 w
->next
= w
->next
->next
;
346 sigproc_printf ("finished processing terminated/stopped child");
349 waitq_head
.next
= NULL
;
350 sigproc_printf ("finished clearing");
353 if (global_sigs
[SIGCHLD
].sa_handler
== (void *) SIG_IGN
)
354 for (int i
= 0; i
< chld_procs
.count (); i
+= remove_proc (i
))
359 sync_proc_subproc
.release (); // Release the lock
361 sigproc_printf ("returning %d", rc
);
367 // FIXME: This is inelegant
369 _cygtls::remove_wq (DWORD wait
)
373 if (exit_state
< ES_FINAL
&& waitq_head
.next
&& sync_proc_subproc
374 && sync_proc_subproc
.acquire (wait
))
376 ForceCloseHandle1 (wq
.thread_ev
, wq_ev
);
378 for (waitq
*w
= &waitq_head
; w
->next
!= NULL
; w
= w
->next
)
384 sync_proc_subproc
.release ();
390 /* Terminate the wait_subproc thread.
391 Called on process exit.
392 Also called by spawn_guts to disassociate any subprocesses from this
393 process. Subprocesses will then know to clean up after themselves and
394 will not become chld_procs. */
398 sigproc_printf ("child_procs count %d", chld_procs
.count ());
399 if (chld_procs
.count ())
401 sync_proc_subproc
.acquire (WPSP
);
403 proc_subproc (PROC_CLEARWAIT
, 1);
405 /* Clean out proc processes from the pid list. */
406 for (int i
= 0; i
< chld_procs
.count (); i
++)
408 /* If we've execed then the execed process will handle setting ppid
409 to 1 iff it is a Cygwin process. */
410 if (!have_execed
|| !have_execed_cygwin
)
411 chld_procs
[i
]->ppid
= 1;
412 if (chld_procs
[i
].wait_thread
)
413 chld_procs
[i
].wait_thread
->terminate_thread ();
414 /* Release memory associated with this process unless it is 'myself'.
415 'myself' is only in the chld_procs table when we've execed. We
416 reach here when the next process has finished initializing but we
417 still can't free the memory used by 'myself' since it is used
418 later on during cygwin tear down. */
419 if (chld_procs
[i
] != myself
)
420 chld_procs
[i
].release ();
423 sync_proc_subproc
.release ();
425 sigproc_printf ("leaving");
428 /* Clear pending signal */
435 /* Clear pending signals of specific thread. Called under TLS lock from
436 _cygtls::remove_pending_sigs. */
438 pending_signals::clear (_cygtls
*tls
)
440 sigpacket
*q
= &start
, *qnext
;
442 while ((qnext
= q
->next
))
443 if (qnext
->sigtls
== tls
)
445 qnext
->si
.si_signo
= 0;
446 q
->next
= qnext
->next
;
452 /* Clear pending signals of specific thread. Called from _cygtls::remove */
454 _cygtls::remove_pending_sigs ()
460 sigpending (sigset_t
*mask
)
462 sigset_t outset
= sig_send (myself
, __SIGPENDING
, &_my_tls
);
463 if (outset
== SIG_BAD_MASK
)
469 /* Force the wait_sig thread to wake up and scan for pending signals */
471 sig_dispatch_pending (bool fast
)
473 /* Non-atomically test for any signals pending and wake up wait_sig if any are
474 found. It's ok if there's a race here since the next call to this function
476 if (sigq
.pending () && &_my_tls
!= _sig_tls
)
477 sig_send (myself
, fast
? __SIGFLUSHFAST
: __SIGFLUSH
);
480 /* Signal thread initialization. Called from dll_crt0_1.
481 This routine starts the signal handling thread. */
485 char char_sa_buf
[1024];
486 PSECURITY_ATTRIBUTES sa
= sec_user_nih ((PSECURITY_ATTRIBUTES
) char_sa_buf
, cygheap
->user
.sid());
487 DWORD err
= fhandler_pipe::create (sa
, &my_readsig
, &my_sendsig
,
488 _NSIG
* sizeof (sigpacket
), "sigwait",
493 api_fatal ("couldn't create signal pipe, %E");
495 ProtectHandle (my_readsig
);
496 myself
->sendsig
= my_sendsig
;
497 my_pendingsigs_evt
= CreateEvent (NULL
, TRUE
, FALSE
, NULL
);
498 if (!my_pendingsigs_evt
)
499 api_fatal ("couldn't create pending signal event, %E");
501 /* sync_proc_subproc is used by proc_subproc. It serializes
502 access to the children and proc arrays. */
503 sync_proc_subproc
.init ("sync_proc_subproc");
504 new cygthread (wait_sig
, cygself
, "sig");
507 /* Exit the current thread very carefully.
508 See cgf-000017 in DevNotes for more details on why this is
511 exit_thread (DWORD res
)
514 if (no_thread_exit_protect ())
516 sigfillset (&_my_tls
.sigmask
); /* No signals wanted */
518 /* CV 2014-11-21: Disable the code sending a signal. The problem with
519 this code is that it allows deadlocks under signal-rich multithreading
521 The original problem reported in 2012 couldn't be reproduced anymore,
522 even disabling this code. Tested on XP 32, Vista 32, W7 32, WOW64, 64,
525 lock_process for_now
; /* May block indefinitely when exiting. */
527 if (!DuplicateHandle (GetCurrentProcess (), GetCurrentThread (),
528 GetCurrentProcess (), &h
,
529 0, FALSE
, DUPLICATE_SAME_ACCESS
))
532 system_printf ("couldn't duplicate the current thread, %E");
537 ProtectHandle1 (h
, exit_thread
);
538 /* Tell wait_sig to wait for this thread to exit. It can then release
539 the lock below and close the above-opened handle. */
540 siginfo_t si
= {__SIGTHREADEXIT
, SI_KERNEL
};
542 sig_send (myself_nowait
, si
, &_my_tls
);
548 sig_send (_pinfo
*p
, int sig
, _cygtls
*tls
)
552 si
.si_code
= SI_KERNEL
;
553 return sig_send (p
, si
, tls
);
556 /* Send a signal to another process by raising its signal semaphore.
557 If pinfo *p == NULL, send to the current process.
558 If sending to this process, wait for notification that a signal has
559 completed before returning. */
561 sig_send (_pinfo
*p
, siginfo_t
& si
, _cygtls
*tls
)
567 bool communing
= si
.si_signo
== __SIGCOMMUNE
;
570 bool wait_for_completion
;
571 if (!(its_me
= p
== NULL
|| p
== myself
|| p
== myself_nowait
))
573 /* It is possible that the process is not yet ready to receive messages
574 * or that it has exited. Detect this.
576 if (!proc_can_be_signalled (p
)) /* Is the process accepting messages? */
578 sigproc_printf ("invalid pid %d(%x), signal %d",
579 p
->pid
, p
->process_state
, si
.si_signo
);
582 wait_for_completion
= false;
586 wait_for_completion
= p
!= myself_nowait
;
590 /* If myself is the stub process, send signal to the child process
591 rather than myself. The fact that myself->dwProcessId is not equal
592 to the current process id indicates myself is the stub process. */
593 if (its_me
&& myself
->dwProcessId
!= GetCurrentProcessId ())
595 wait_for_completion
= false;
600 sendsig
= my_sendsig
;
605 for (int i
= 0; !p
->sendsig
&& i
< 10000; i
++)
610 dwProcessId
= p
->dwProcessId
;
614 dupsig
= p
->exec_sendsig
;
615 dwProcessId
= p
->exec_dwProcessId
;
620 sigproc_printf ("sendsig handle never materialized");
623 HANDLE hp
= OpenProcess (PROCESS_DUP_HANDLE
, false, dwProcessId
);
627 sigproc_printf ("OpenProcess failed, %E");
631 if (!DuplicateHandle (hp
, dupsig
, GetCurrentProcess (), &sendsig
, 0,
632 false, DUPLICATE_SAME_ACCESS
) || !sendsig
)
635 sigproc_printf ("DuplicateHandle failed, %E");
639 VerifyHandle (sendsig
);
643 DWORD flag
= PIPE_NOWAIT
;
644 /* Set PIPE_NOWAIT here to avoid blocking when sending a signal.
645 (Yes, I know MSDN says not to use this)
646 We can't ever block here because it causes a deadlock when
647 debugging with gdb. */
648 BOOL res
= SetNamedPipeHandleState (sendsig
, &flag
, NULL
, NULL
);
649 sigproc_printf ("%d = SetNamedPipeHandleState (%y, PIPE_NOWAIT, NULL, NULL)", res
, sendsig
);
653 si
._si_commune
._si_process_handle
= hp
;
655 HANDLE
& tome
= si
._si_commune
._si_write_handle
;
656 HANDLE
& fromthem
= si
._si_commune
._si_read_handle
;
657 if (!CreatePipeOverlapped (&fromthem
, &tome
, &sec_all_nih
))
659 sigproc_printf ("CreatePipe for __SIGCOMMUNE failed, %E");
663 if (!DuplicateHandle (GetCurrentProcess (), tome
, hp
, &tome
, 0, false,
664 DUPLICATE_SAME_ACCESS
| DUPLICATE_CLOSE_SOURCE
))
666 sigproc_printf ("DuplicateHandle for __SIGCOMMUNE failed, %E");
673 sigproc_printf ("sendsig %p, pid %d, signal %d, its_me %d", sendsig
, p
->pid
,
674 si
.si_signo
, its_me
);
679 else if (si
.si_signo
== __SIGPENDING
|| si
.si_signo
== __SIGPENDINGALL
)
680 pack
.mask
= &pending
;
681 else if (si
.si_signo
== __SIGFLUSH
|| si
.si_signo
> 0)
683 threadlist_t
*tl_entry
= cygheap
->find_tls (tls
? tls
: _main_tls
);
684 pack
.mask
= tls
? &tls
->sigmask
: &_main_tls
->sigmask
;
685 cygheap
->unlock_tls (tl_entry
);
692 pack
.si
.si_pid
= myself
->pid
;
694 pack
.si
.si_uid
= myself
->uid
;
695 pack
.pid
= myself
->pid
;
697 if (wait_for_completion
)
699 pack
.wakeup
= CreateEvent (&sec_none_nih
, FALSE
, FALSE
, NULL
);
700 sigproc_printf ("wakeup %p", pack
.wakeup
);
701 ProtectHandle (pack
.wakeup
);
706 if (!communing
|| !(si
._si_commune
._si_code
& PICOM_EXTRASTR
))
708 leader
= (char *) &pack
;
709 packsize
= sizeof (pack
);
713 size_t n
= strlen (si
._si_commune
._si_str
);
714 packsize
= sizeof (pack
) + sizeof (n
) + n
;
715 char *p
= leader
= (char *) alloca (packsize
);
716 memcpy (p
, &pack
, sizeof (pack
)); p
+= sizeof (pack
);
717 memcpy (p
, &n
, sizeof (n
)); p
+= sizeof (n
);
718 memcpy (p
, si
._si_commune
._si_str
, n
); p
+= n
;
723 /* Try multiple times to send if packsize != nb since that probably
724 means that the pipe buffer is full. */
725 for (int i
= 0; i
< 100; i
++)
727 res
= WriteFile (sendsig
, leader
, packsize
, &nb
, NULL
);
728 if (!res
|| packsize
== nb
)
736 /* Couldn't send to the pipe. This probably means that the
737 process is exiting. */
740 sigproc_printf ("WriteFile for pipe %p failed, %E", sendsig
);
741 ForceCloseHandle (sendsig
);
743 else if (!p
->exec_sendsig
&& !exit_state
)
744 system_printf ("error sending signal %d, pid %u, pipe handle %p, nb %u, packsize %u, %E",
745 si
.si_signo
, p
->pid
, sendsig
, nb
, packsize
);
746 if (GetLastError () == ERROR_BROKEN_PIPE
)
754 /* No need to wait for signal completion unless this was a signal to
757 If it was a signal to this process, wait for a dispatched signal.
758 Otherwise just wait for the wait_sig to signal that it has finished
759 processing the signal. */
760 if (wait_for_completion
)
762 sigproc_printf ("Waiting for pack.wakeup %p", pack
.wakeup
);
763 rc
= WaitForSingleObject (pack
.wakeup
, WSSC
);
764 ForceCloseHandle (pack
.wakeup
);
769 sigproc_printf ("Not waiting for sigcomplete. its_me %d signal %d",
770 its_me
, si
.si_signo
);
772 ForceCloseHandle (sendsig
);
776 if (rc
== WAIT_OBJECT_0
)
777 rc
= 0; // Successful exit
784 if (wait_for_completion
&& si
.si_signo
!= __SIGFLUSHFAST
)
785 _my_tls
.call_signal_handler ();
790 if (si
._si_commune
._si_process_handle
)
791 CloseHandle (si
._si_commune
._si_process_handle
);
792 if (si
._si_commune
._si_read_handle
)
793 CloseHandle (si
._si_commune
._si_read_handle
);
796 ForceCloseHandle (pack
.wakeup
);
797 if (si
.si_signo
!= __SIGPENDING
&& si
.si_signo
!= __SIGPENDINGALL
)
803 sigproc_printf ("returning %p from sending signal %d", rc
, si
.si_signo
);
807 int child_info::retry_count
= 0;
809 /* Initialize some of the memory block passed to child processes
810 by fork/spawn/exec. */
811 child_info::child_info (unsigned in_cb
, child_info_types chtype
,
812 bool need_subproc_ready
):
813 msv_count (0), cb (in_cb
), intro (PROC_MAGIC_GENERIC
),
814 magic (CHILD_INFO_MAGIC
), type (chtype
), cygheap (::cygheap
),
815 cygheap_max (::cygheap_max
), flag (0), retry (child_info::retry_count
),
816 rd_proc_pipe (NULL
), wr_proc_pipe (NULL
), sigmask (_my_tls
.sigmask
)
818 fhandler_union_cb
= sizeof (fhandler_union
);
819 user_h
= cygwin_user_h
;
820 if (strace
.active ())
825 /* Only propagate _CI_STRACED to child if strace is actually tracing
826 child processes of this process. The undocumented ProcessDebugFlags
827 returns 0 if EPROCESS->NoDebugInherit is TRUE, 1 otherwise.
828 This avoids a hang when stracing a forking or spawning process
829 with the -f flag set to "don't follow fork". */
830 status
= NtQueryInformationProcess (GetCurrentProcess (),
831 ProcessDebugFlags
, &DebugFlags
,
832 sizeof (DebugFlags
), NULL
);
833 if (NT_SUCCESS (status
) && DebugFlags
)
836 if (need_subproc_ready
)
838 subproc_ready
= CreateEvent (&sec_all
, FALSE
, FALSE
, NULL
);
839 flag
|= _CI_ISCYGWIN
;
841 sigproc_printf ("subproc_ready %p", subproc_ready
);
842 /* Create an inheritable handle to pass to the child process. This will
843 allow the child to copy cygheap etc. from the parent to itself. If
844 we're forking, we also need handle duplicate access. */
846 DWORD perms
= PROCESS_QUERY_LIMITED_INFORMATION
| PROCESS_VM_READ
847 | PROCESS_VM_OPERATION
| SYNCHRONIZE
;
848 if (type
== _CH_FORK
)
849 perms
|= PROCESS_DUP_HANDLE
;
851 if (!DuplicateHandle (GetCurrentProcess (), GetCurrentProcess (),
852 GetCurrentProcess (), &parent
, perms
, TRUE
, 0))
853 system_printf ("couldn't create handle to myself for child, %E");
856 child_info::~child_info ()
861 child_info_fork::child_info_fork () :
862 child_info (sizeof *this, _CH_FORK
, true),
863 forker_finished (NULL
)
867 child_info_spawn::child_info_spawn (child_info_types chtype
, bool need_subproc_ready
) :
868 child_info (sizeof *this, chtype
, need_subproc_ready
)
870 if (type
== _CH_EXEC
)
875 else if (!(ev
= CreateEvent (&sec_none_nih
, false, false, NULL
)))
876 api_fatal ("couldn't create signalling event for exec, %E");
878 get_proc_lock (PROC_EXECING
, 0);
879 /* exit with lock held */
884 cygheap_exec_info::alloc ()
886 cygheap_exec_info
*res
=
887 (cygheap_exec_info
*) ccalloc_abort (HEAP_1_EXEC
, 1,
888 sizeof (cygheap_exec_info
)
889 + (chld_procs
.count ()
890 * sizeof (children
[0])));
895 child_info_spawn::wait_for_myself ()
898 if (myself
.remember ())
900 WaitForSingleObject (ev
, INFINITE
);
904 child_info::cleanup ()
908 CloseHandle (subproc_ready
);
909 subproc_ready
= NULL
;
913 CloseHandle (parent
);
918 ForceCloseHandle (rd_proc_pipe
);
923 ForceCloseHandle (wr_proc_pipe
);
929 child_info_spawn::cleanup ()
935 for (char **e
= moreinfo
->envp
; *e
; e
++)
937 cfree (moreinfo
->envp
);
939 if (type
!= _CH_SPAWN
&& moreinfo
->myself_pinfo
)
940 CloseHandle (moreinfo
->myself_pinfo
);
949 if (type
== _CH_EXEC
)
951 if (iscygwin () && hExeced
)
952 proc_subproc (PROC_EXEC_CLEANUP
, 0);
953 sync_proc_subproc
.release ();
956 child_info::cleanup ();
959 /* Record any non-reaped subprocesses to be passed to about-to-be-execed
960 process. FIXME: There is a race here if the process exits while we
963 cygheap_exec_info::record_children ()
965 for (nchildren
= 0; nchildren
< chld_procs
.count (); nchildren
++)
967 children
[nchildren
].pid
= chld_procs
[nchildren
]->pid
;
968 children
[nchildren
].p
= chld_procs
[nchildren
];
969 /* Set inheritance of required child handles for reattach_children
970 in the about-to-be-execed process. */
971 children
[nchildren
].p
.set_inheritance (true);
976 child_info_spawn::record_children ()
978 if (type
== _CH_EXEC
&& iscygwin ())
979 moreinfo
->record_children ();
982 /* Reattach non-reaped subprocesses passed in from the cygwin process
983 which previously operated under this pid. FIXME: Is there a race here
984 if the process exits during cygwin's exec handoff? */
986 cygheap_exec_info::reattach_children (HANDLE parent
)
988 for (int i
= 0; i
< nchildren
; i
++)
990 pinfo
p (parent
, children
[i
].p
, children
[i
].pid
);
992 debug_only_printf ("couldn't reattach child %d from previous process", children
[i
].pid
);
993 else if (!p
.attach ())
994 debug_only_printf ("attach of child process %d failed", children
[i
].pid
);
996 debug_only_printf ("reattached pid %d<%u>, process handle %p, rd_proc_pipe %p->%p",
997 p
->pid
, p
->dwProcessId
, p
.hProcess
,
998 children
[i
].p
.rd_proc_pipe
, p
.rd_proc_pipe
);
1003 child_info_spawn::reattach_children ()
1005 moreinfo
->reattach_children (parent
);
1009 child_info::ready (bool execed
)
1013 sigproc_printf ("subproc_ready not set");
1017 if (dynamically_loaded
)
1018 sigproc_printf ("not really ready");
1019 else if (!SetEvent (subproc_ready
))
1020 api_fatal ("SetEvent failed, %E");
1022 sigproc_printf ("signalled %p that I was ready", subproc_ready
);
1026 CloseHandle (subproc_ready
);
1027 subproc_ready
= NULL
;
1032 child_info::sync (pid_t pid
, HANDLE
& hProcess
, DWORD howlong
)
1037 unsigned nsubproc_ready
;
1040 nsubproc_ready
= WAIT_OBJECT_0
+ 3;
1043 w4
[n
++] = subproc_ready
;
1048 sigproc_printf ("n %d, waiting for subproc_ready(%p) and child process(%p)", n
, w4
[0], w4
[1]);
1049 DWORD x
= WaitForMultipleObjects (n
, w4
, FALSE
, howlong
);
1053 system_printf ("wait failed, pid %u, %E", pid
);
1058 if (x
!= nsubproc_ready
)
1061 GetExitCodeProcess (hProcess
, &exit_code
);
1066 exit_code
= STILL_ACTIVE
;
1067 if (type
== _CH_EXEC
&& my_wr_proc_pipe
)
1069 ForceCloseHandle1 (hProcess
, childhProc
);
1073 sigproc_printf ("pid %u, WFMO returned %d, exit_code %y, res %d", pid
, x
,
1080 child_info::proc_retry (HANDLE h
)
1084 sigproc_printf ("exit_code %y", exit_code
);
1087 case STILL_ACTIVE
: /* shouldn't happen */
1088 sigproc_printf ("STILL_ACTIVE? How'd we get here?");
1090 case STATUS_DLL_NOT_FOUND
:
1091 case STATUS_ACCESS_VIOLATION
:
1092 case STATUS_ILLEGAL_INSTRUCTION
:
1093 case STATUS_ILLEGAL_DLL_PSEUDO_RELOCATION
: /* pseudo-reloc.c specific */
1095 case STATUS_CONTROL_C_EXIT
:
1099 case STATUS_DLL_INIT_FAILED
:
1100 case STATUS_DLL_INIT_FAILED_LOGOFF
:
1101 case EXITCODE_RETRY
:
1105 case EXITCODE_FORK_FAILED
: /* windows prevented us from forking */
1108 /* Count down non-recognized exit codes more quickly since they aren't
1109 due to known conditions. */
1111 if (!iscygwin () && (exit_code
& 0xffff0000) != 0xc0000000)
1113 if ((retry
-= 2) < 0)
1119 ForceCloseHandle1 (h
, childhProc
);
1124 child_info_fork::abort (const char *fmt
, ...)
1131 strace_vprintf (DEBUG
, fmt
, ap
);
1133 strace_vprintf (SYSTEM
, fmt
, ap
);
1134 TerminateProcess (GetCurrentProcess (), EXITCODE_FORK_FAILED
);
1137 TerminateProcess (GetCurrentProcess (), EXITCODE_RETRY
);
1141 /* Check the state of all of our children to see if any are stopped or
1145 checkstate (waitq
*parent_w
)
1147 int potential_match
= 0;
1149 sigproc_printf ("child_procs count %d", chld_procs
.count ());
1151 /* Check already dead processes first to see if they match the criteria
1152 * given in w->next. */
1154 for (int i
= 0; i
< chld_procs
.count (); i
++)
1155 if ((res
= stopped_or_terminated (parent_w
, chld_procs
[i
])))
1158 potential_match
= 1;
1162 sigproc_printf ("no matching terminated children found");
1163 potential_match
= -!!chld_procs
.count ();
1166 sigproc_printf ("returning %d", potential_match
);
1167 return potential_match
;
1170 /* Remove a proc from chld_procs by swapping it with the last child in the list.
1171 Also releases shared memory of exited processes. */
1173 remove_proc (int ci
)
1177 if (_my_tls
._ctinfo
!= chld_procs
[ci
].wait_thread
)
1178 chld_procs
[ci
].wait_thread
->terminate_thread ();
1180 else if (chld_procs
[ci
] && chld_procs
[ci
]->exists ())
1183 sigproc_printf ("removing chld_procs[%d], pid %d, child_procs count %d",
1184 ci
, chld_procs
[ci
]->pid
, chld_procs
.count ());
1185 if (chld_procs
[ci
] != myself
)
1186 chld_procs
[ci
].release ();
1187 if (ci
< chld_procs
.del_one ())
1189 /* Wait for proc_waiter thread to make a copy of this element before
1190 moving it or it may become confused. The chances are very high that
1191 the proc_waiter thread has already done this by the time we
1193 if (!have_execed
&& !exit_state
)
1194 while (!chld_procs
[chld_procs
.count ()].waiter_ready
)
1196 chld_procs
[ci
] = chld_procs
[chld_procs
.count ()];
1201 /* Check status of child process vs. waitq member.
1203 parent_w is the pointer to the parent of the waitq member in question.
1204 child is the subprocess being considered.
1206 Returns non-zero if waiting thread released. */
1208 stopped_or_terminated (waitq
*parent_w
, _pinfo
*child
)
1211 waitq
*w
= parent_w
->next
;
1213 sigproc_printf ("considering pid %d, pgid %d, w->pid %d", child
->pid
, child
->pgid
, w
->pid
);
1216 else if (w
->pid
== 0)
1217 might_match
= child
->pgid
== myself
->pgid
;
1218 else if (w
->pid
< 0)
1219 might_match
= child
->pgid
== -w
->pid
;
1221 might_match
= (w
->pid
== child
->pid
);
1228 if (!((terminated
= (child
->process_state
== PID_EXITED
))
1229 || ((w
->options
& WCONTINUED
) && child
->stopsig
== SIGCONT
)
1230 || ((w
->options
& WUNTRACED
) && child
->stopsig
&& child
->stopsig
!= SIGCONT
)))
1233 parent_w
->next
= w
->next
; /* successful wait. remove from wait queue */
1234 w
->pid
= child
->pid
;
1238 sigproc_printf ("stopped child, stop signal %d", child
->stopsig
);
1239 if (child
->stopsig
== SIGCONT
)
1240 w
->status
= __W_CONTINUED
;
1242 w
->status
= (child
->stopsig
<< 8) | 0x7f;
1247 child
->process_state
= PID_REAPED
;
1248 w
->status
= (__uint16_t
) child
->exitcode
;
1250 add_rusage (&myself
->rusage_children
, &child
->rusage_children
);
1251 add_rusage (&myself
->rusage_children
, &child
->rusage_self
);
1255 add_rusage ((struct rusage
*) w
->rusage
, &child
->rusage_children
);
1256 add_rusage ((struct rusage
*) w
->rusage
, &child
->rusage_self
);
1260 if (!SetEvent (w
->ev
)) /* wake up wait4 () immediately */
1261 system_printf ("couldn't wake up wait event %p, %E", w
->ev
);
1266 talktome (siginfo_t
*si
)
1268 unsigned size
= sizeof (*si
);
1269 sigproc_printf ("pid %d wants some information", si
->si_pid
);
1270 if (si
->_si_commune
._si_code
& PICOM_EXTRASTR
)
1274 if (!ReadFile (my_readsig
, &n
, sizeof (n
), &nb
, NULL
) || nb
!= sizeof (n
))
1276 siginfo_t
*newsi
= (siginfo_t
*) alloca (size
+= n
+ 1);
1278 newsi
->_si_commune
._si_str
= (char *) (newsi
+ 1);
1279 if (!ReadFile (my_readsig
, newsi
->_si_commune
._si_str
, n
, &nb
, NULL
) || nb
!= n
)
1281 newsi
->_si_commune
._si_str
[n
] = '\0';
1285 pinfo
pi (si
->si_pid
);
1287 new cygthread (commune_process
, size
, si
, "commune");
1290 /* Add a packet to the beginning of the queue.
1291 Should only be called from signal thread. */
1293 pending_signals::add (sigpacket
& pack
)
1297 se
= sigs
+ pack
.si
.si_signo
;
1298 if (se
->si
.si_signo
)
1301 se
->next
= start
.next
;
1305 /* Process signals by waiting for signal data to arrive in a pipe.
1306 Set a completion event if one was specified. */
1310 _sig_tls
= &_my_tls
;
1311 bool sig_held
= false;
1313 sigproc_printf ("entering ReadFile loop, my_readsig %p, my_sendsig %p",
1314 my_readsig
, my_sendsig
);
1316 hntdll
= GetModuleHandle ("ntdll.dll");
1321 sigpacket pack
= {};
1323 pack
.si
.si_signo
= __SIGFLUSH
;
1324 else if (!ReadFile (my_readsig
, &pack
, sizeof (pack
), &nb
, NULL
))
1325 Sleep (INFINITE
); /* Assume were exiting. Never exit this thread */
1326 else if (nb
!= sizeof (pack
) || !pack
.si
.si_signo
)
1328 system_printf ("garbled signal pipe data nb %u, sig %d", nb
, pack
.si
.si_signo
);
1333 /* Don't process signals when we start exiting */
1334 if (exit_state
> ES_EXIT_STARTING
&& pack
.si
.si_signo
> 0)
1335 goto skip_process_signal
;
1337 sigset_t dummy_mask
;
1338 threadlist_t
*tl_entry
;
1341 /* There's a short time at process startup of a forked process,
1342 when _main_tls points to the system-allocated stack, not to
1343 the parent thread. In that case find_tls fails, and we fetch
1344 the sigmask from the child_info passed from the parent. */
1345 if (cygwin_finished_initializing
)
1347 tl_entry
= cygheap
->find_tls (_main_tls
);
1348 dummy_mask
= _main_tls
->sigmask
;
1349 cygheap
->unlock_tls (tl_entry
);
1351 else if (child_proc_info
)
1352 dummy_mask
= child_proc_info
->sigmask
;
1355 pack
.mask
= &dummy_mask
;
1362 switch (pack
.si
.si_signo
)
1365 talktome (&pack
.si
);
1368 strace
.activate (false);
1370 case __SIGPENDINGALL
:
1376 while ((q
= q
->next
))
1378 _cygtls
*sigtls
= q
->sigtls
?: _main_tls
;
1379 if (sigtls
->sigmask
& (bit
= SIGTOMASK (q
->si
.si_signo
)))
1381 tl_entry
= cygheap
->find_tls (q
->si
.si_signo
, issig_wait
);
1385 cygheap
->unlock_tls (tl_entry
);
1396 tl_entry
= cygheap
->find_tls (pack
.sigtls
);
1397 while ((q
= q
->next
))
1399 /* Skip thread-specific signals for other threads. */
1400 if (q
->sigtls
&& pack
.sigtls
!= q
->sigtls
)
1402 if (pack
.sigtls
->sigmask
& (bit
= SIGTOMASK (q
->si
.si_signo
)))
1405 cygheap
->unlock_tls (tl_entry
);
1412 init_console_handler (::cygheap
->ctty
1413 && ::cygheap
->ctty
->is_console ());
1415 case __SIGTHREADEXIT
:
1417 /* Serialize thread exit as the thread exit code can be interpreted
1418 as the process exit code in some cases when racing with
1419 ExitProcess/TerminateProcess.
1420 So, wait for the thread which sent this signal to exit, then
1421 release the process lock which it held and close it's handle.
1422 See cgf-000017 in DevNotes for more details.
1424 HANDLE h
= (HANDLE
) pack
.si
.si_cyg
;
1425 DWORD res
= WaitForSingleObject (h
, 5000);
1426 lock_process::force_release (pack
.sigtls
);
1427 ForceCloseHandle1 (h
, exit_thread
);
1428 if (res
!= WAIT_OBJECT_0
)
1433 system_printf ("WaitForSingleObject(%p) for thread exit returned %u", h
, res
);
1437 default: /* Normal (positive) signal */
1438 if (pack
.si
.si_signo
< 0)
1439 sig_clear (-pack
.si
.si_signo
);
1447 case __SIGFLUSHFAST
:
1451 /* Check the queue for signals. There will always be at least one
1452 thing on the queue if this was a valid signal. */
1453 while ((qnext
= q
->next
))
1455 if (qnext
->si
.si_signo
&& qnext
->process () <= 0)
1459 q
->next
= qnext
->next
;
1460 qnext
->si
.si_signo
= 0;
1463 /* At least one signal still queued? The event is used in select
1464 only, and only to decide if WFMO should wake up in case a
1465 signalfd is waiting via select/poll for being ready to read a
1466 pending signal. This method wakes up all threads hanging in
1467 select and having a signalfd, as soon as a pending signal is
1468 available, but it's certainly better than constant polling. */
1469 if (sigq
.start
.next
)
1470 SetEvent (my_pendingsigs_evt
);
1472 ResetEvent (my_pendingsigs_evt
);
1473 if (pack
.si
.si_signo
== SIGCHLD
)
1477 case __SIGNONCYGCHLD
:
1478 cygheap_fdenum
cfd (false);
1479 while (cfd
.next () >= 0)
1480 if (cfd
->get_dev () == FH_PIPEW
)
1482 fhandler_pipe
*pipe
= (fhandler_pipe
*)(fhandler_base
*) cfd
;
1483 if (pipe
->need_close_query_hdl ())
1484 pipe
->close_query_handle ();
1488 if (clearwait
&& !have_execed
)
1489 proc_subproc (PROC_CLEARWAIT
, 0);
1490 skip_process_signal
:
1493 sigproc_printf ("signalling pack.wakeup %p", pack
.wakeup
);
1494 SetEvent (pack
.wakeup
);