1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
|
/* signal.cc
Written by Steve Chamberlain of Cygnus Support, sac@cygnus.com
Significant changes by Sergey Okhapkin <sos@prospect.com.ru>
This file is part of Cygwin.
This software is a copyrighted work licensed under the terms of the
Cygwin license. Please consult the file "CYGWIN_LICENSE" for
details. */
#include "winsup.h"
#include <stdlib.h>
#include <unistd.h>
#include <sys/cygwin.h>
#include <sys/signalfd.h>
#include <sys/reent.h> /* needed for __stdio_exit_handler declaration */
#include "pinfo.h"
#include "sigproc.h"
#include "cygtls.h"
#include "path.h"
#include "fhandler.h"
#include "dtable.h"
#include "cygheap.h"
#include "cygwait.h"
#include "posix_timer.h"
#define _SA_NORESTART 0x8000
static int sigaction_worker (int, const struct sigaction *, struct sigaction *, bool);
#define sigtrapped(func) ((func) != SIG_IGN && (func) != SIG_DFL)
extern "C" _sig_func_ptr
signal (int sig, _sig_func_ptr func)
{
sig_dispatch_pending ();
_sig_func_ptr prev;
/* check that sig is in right range */
if (sig <= 0 || sig >= _NSIG || sig == SIGKILL || sig == SIGSTOP)
{
set_errno (EINVAL);
syscall_printf ("SIG_ERR = signal (%d, %p)", sig, func);
return (_sig_func_ptr) SIG_ERR;
}
prev = global_sigs[sig].sa_handler;
struct sigaction& gs = global_sigs[sig];
if (gs.sa_flags & _SA_NORESTART)
gs.sa_flags &= ~SA_RESTART;
else
gs.sa_flags |= SA_RESTART;
gs.sa_mask = SIGTOMASK (sig);
gs.sa_handler = func;
gs.sa_flags &= ~SA_SIGINFO;
syscall_printf ("%p = signal (%d, %p)", prev, sig, func);
return prev;
}
extern "C" int
clock_nanosleep (clockid_t clk_id, int flags, const struct timespec *rqtp,
struct timespec *rmtp)
{
const bool abstime = (flags & TIMER_ABSTIME) ? true : false;
int res = 0;
sig_dispatch_pending ();
pthread_testcancel ();
__try
{
if (!valid_timespec (*rqtp))
return EINVAL;
}
__except (NO_ERROR)
{
return EFAULT;
}
__endtry
/* Explicitly disallowed by POSIX. Needs to be checked first to avoid
being caught by the following test. */
if (clk_id == CLOCK_THREAD_CPUTIME_ID)
return EINVAL;
/* support for CPU-time clocks is optional */
if (CLOCKID_IS_PROCESS (clk_id) || CLOCKID_IS_THREAD (clk_id))
return ENOTSUP;
/* All other valid clocks are valid */
if (clk_id >= MAX_CLOCKS)
return EINVAL;
LARGE_INTEGER timeout;
timeout.QuadPart = (LONGLONG) rqtp->tv_sec * NS100PERSEC
+ ((LONGLONG) rqtp->tv_nsec + (NSPERSEC/NS100PERSEC) - 1)
/ (NSPERSEC/NS100PERSEC);
if (abstime)
{
struct timespec tp;
clock_gettime (clk_id, &tp);
/* Check for immediate timeout */
if (tp.tv_sec > rqtp->tv_sec
|| (tp.tv_sec == rqtp->tv_sec && tp.tv_nsec > rqtp->tv_nsec))
return 0;
switch (clk_id)
{
case CLOCK_REALTIME_COARSE:
case CLOCK_REALTIME:
timeout.QuadPart += FACTOR;
break;
default:
/* other clocks need to be handled with a relative timeout */
timeout.QuadPart -= tp.tv_sec * NS100PERSEC
+ tp.tv_nsec / (NSPERSEC/NS100PERSEC);
timeout.QuadPart *= -1LL;
break;
}
}
else /* !abstime */
timeout.QuadPart *= -1LL;
syscall_printf ("clock_nanosleep (%ld.%09ld)", rqtp->tv_sec, rqtp->tv_nsec);
int rc = cygwait (NULL, &timeout, cw_sig_eintr | cw_cancel | cw_cancel_self);
if (rc == WAIT_SIGNALED)
res = EINTR;
/* according to POSIX, rmtp is used only if !abstime */
if (rmtp && !abstime)
{
__try
{
rmtp->tv_sec = (time_t) (timeout.QuadPart / NS100PERSEC);
rmtp->tv_nsec = (long) ((timeout.QuadPart % NS100PERSEC)
* (NSPERSEC/NS100PERSEC));
}
__except (NO_ERROR)
{
res = EFAULT;
}
__endtry
}
syscall_printf ("%d = clock_nanosleep(%lu, %d, %ld.%09ld, %ld.%09.ld)",
res, clk_id, flags, rqtp->tv_sec, rqtp->tv_nsec,
rmtp ? rmtp->tv_sec : 0, rmtp ? rmtp->tv_nsec : 0);
return res;
}
extern "C" int
nanosleep (const struct timespec *rqtp, struct timespec *rmtp)
{
int res = clock_nanosleep (CLOCK_REALTIME, 0, rqtp, rmtp);
if (res != 0)
{
set_errno (res);
return -1;
}
return 0;
}
extern "C" unsigned int
sleep (unsigned int seconds)
{
struct timespec req, rem;
req.tv_sec = seconds;
req.tv_nsec = 0;
if (clock_nanosleep (CLOCK_REALTIME, 0, &req, &rem))
return rem.tv_sec + (rem.tv_nsec > 0);
return 0;
}
extern "C" int
usleep (useconds_t useconds)
{
struct timespec req;
req.tv_sec = useconds / USPERSEC;
req.tv_nsec = (useconds % USPERSEC) * (NSPERSEC/USPERSEC);
int res = clock_nanosleep (CLOCK_REALTIME, 0, &req, NULL);
if (res != 0)
{
set_errno (res);
return -1;
}
return 0;
}
extern "C" int
sigprocmask (int how, const sigset_t *set, sigset_t *oldset)
{
int res = handle_sigprocmask (how, set, oldset, _my_tls.sigmask);
if (res)
{
set_errno (res);
res = -1;
}
syscall_printf ("%R = sigprocmask (%d, %p, %p)", res, how, set, oldset);
return res;
}
int
handle_sigprocmask (int how, const sigset_t *set, sigset_t *oldset, sigset_t& opmask)
{
/* check that how is in right range if set is not NULL */
if (set && how != SIG_BLOCK && how != SIG_UNBLOCK && how != SIG_SETMASK)
{
syscall_printf ("Invalid how value %d", how);
return EINVAL;
}
__try
{
if (oldset)
*oldset = opmask;
if (set)
{
sigset_t newmask = opmask;
switch (how)
{
case SIG_BLOCK:
/* add set to current mask */
newmask |= *set;
break;
case SIG_UNBLOCK:
/* remove set from current mask */
newmask &= ~*set;
break;
case SIG_SETMASK:
/* just set it */
newmask = *set;
break;
}
set_signal_mask (opmask, newmask);
}
}
__except (EFAULT)
{
return EFAULT;
}
__endtry
return 0;
}
int
_pinfo::kill (siginfo_t& si)
{
int res;
DWORD this_process_state;
pid_t this_pid;
sig_dispatch_pending ();
if (exists ())
{
bool sendSIGCONT;
this_process_state = process_state;
if ((sendSIGCONT = (si.si_signo < 0)))
si.si_signo = -si.si_signo;
if (si.si_signo == 0)
res = 0;
else if ((res = (int) sig_send (this, si)))
{
sigproc_printf ("%d = sig_send, %E ", res);
res = -1;
}
else if (sendSIGCONT)
{
siginfo_t si2 = {0};
si2.si_signo = SIGCONT;
si2.si_code = SI_KERNEL;
sig_send (this, si2);
}
this_pid = pid;
}
else if (process_state == PID_EXITED)
{
this_process_state = process_state;
this_pid = pid;
res = 0;
}
else
{
set_errno (ESRCH);
this_process_state = 0;
this_pid = 0;
res = -1;
}
syscall_printf ("%d = _pinfo::kill (%d), pid %d, process_state %y", res,
si.si_signo, this_pid, this_process_state);
return res;
}
extern "C" int
raise (int sig)
{
pthread *thread = _my_tls.tid;
if (!thread || !__isthreaded)
return kill (myself->pid, sig);
/* Make sure to return -1 and set errno, as on Linux. */
int err = pthread_kill (thread, sig);
if (err)
set_errno (err);
return err ? -1 : 0;
}
static int
kill0 (pid_t pid, siginfo_t& si)
{
syscall_printf ("kill (%d, %d)", pid, si.si_signo);
/* check that sig is in right range */
if (si.si_signo < 0 || si.si_signo >= _NSIG)
{
set_errno (EINVAL);
syscall_printf ("signal %d out of range", si.si_signo);
return -1;
}
if (pid > 0)
{
pinfo p (pid);
if (!p)
{
set_errno (ESRCH);
return -1;
}
return p->kill (si);
}
return kill_pgrp (-pid, si);
}
int
kill (pid_t pid, int sig)
{
siginfo_t si = {0};
si.si_signo = sig;
si.si_code = SI_USER;
return kill0 (pid, si);
}
int
kill_pgrp (pid_t pid, siginfo_t& si)
{
int res = 0;
int found = 0;
int killself = 0;
sigproc_printf ("pid %d, signal %d", pid, si.si_signo);
winpids pids ((DWORD) PID_MAP_RW);
for (unsigned i = 0; i < pids.npids; i++)
{
_pinfo *p = pids[i];
if (!p || !p->exists ())
continue;
/* Is it a process we want to kill? */
if ((pid == 0 && (p->pgid != myself->pgid || p->ctty != myself->ctty)) ||
(pid > 1 && p->pgid != pid) ||
(si.si_signo < 0 && NOTSTATE (p, PID_STOPPED)))
continue;
sigproc_printf ("killing pid %d, pgrp %d, p->%s, %s", p->pid, p->pgid,
p->__ctty (), myctty ());
if (p == myself)
killself++;
else if (p->kill (si))
res = -1;
found++;
}
if (killself && !exit_state && myself->kill (si))
res = -1;
if (!found)
{
set_errno (ESRCH);
res = -1;
}
syscall_printf ("%R = kill(%d, %d)", res, pid, si.si_signo);
return res;
}
extern "C" int
killpg (pid_t pgrp, int sig)
{
return kill (-pgrp, sig);
}
extern "C" void
abort (void)
{
_my_tls.incyg++;
sig_dispatch_pending ();
/* Ensure that SIGABRT can be caught regardless of blockage. */
sigset_t sig_mask;
sigfillset (&sig_mask);
sigdelset (&sig_mask, SIGABRT);
set_signal_mask (_my_tls.sigmask, sig_mask);
raise (SIGABRT);
_my_tls.call_signal_handler (); /* Call any signal handler */
/* Flush all streams as per SUSv2. */
if (__stdio_exit_handler)
(*__stdio_exit_handler) ();
do_exit (SIGABRT); /* signal handler didn't exit. Goodbye. */
}
static int
sigaction_worker (int sig, const struct sigaction *newact,
struct sigaction *oldact, bool isinternal)
{
int res = -1;
__try
{
sig_dispatch_pending ();
/* check that sig is in right range */
if (sig <= 0 || sig >= _NSIG)
set_errno (EINVAL);
else
{
struct sigaction oa = global_sigs[sig];
if (!newact)
sigproc_printf ("signal %d, newact %p, oa %p",
sig, newact, oa, oa.sa_handler);
else
{
sigproc_printf ("signal %d, newact %p (handler %p), oa %p",
sig, newact, newact->sa_handler, oa,
oa.sa_handler);
if (sig == SIGKILL || sig == SIGSTOP)
{
set_errno (EINVAL);
__leave;
}
struct sigaction na = *newact;
struct sigaction& gs = global_sigs[sig];
if (!isinternal)
na.sa_flags &= ~_SA_INTERNAL_MASK;
gs = na;
if (!(gs.sa_flags & SA_NODEFER))
gs.sa_mask |= SIGTOMASK(sig);
if (gs.sa_handler == SIG_IGN)
sig_clear (sig);
if (gs.sa_handler == SIG_DFL && sig == SIGCHLD)
sig_clear (sig);
if (sig == SIGCHLD)
{
myself->process_state &= ~PID_NOCLDSTOP;
if (gs.sa_flags & SA_NOCLDSTOP)
myself->process_state |= PID_NOCLDSTOP;
}
}
if (oldact)
{
*oldact = oa;
oa.sa_flags &= ~_SA_INTERNAL_MASK;
}
res = 0;
}
}
__except (EFAULT) {}
__endtry
return res;
}
extern "C" int
sigaction (int sig, const struct sigaction *newact, struct sigaction *oldact)
{
int res = sigaction_worker (sig, newact, oldact, false);
syscall_printf ("%R = sigaction(%d, %p, %p)", res, sig, newact, oldact);
return res;
}
extern "C" int
sigaddset (sigset_t *set, const int sig)
{
/* check that sig is in right range */
if (sig <= 0 || sig >= _NSIG)
{
set_errno (EINVAL);
syscall_printf ("SIG_ERR = sigaddset signal %d out of range", sig);
return -1;
}
*set |= SIGTOMASK (sig);
return 0;
}
extern "C" int
sigdelset (sigset_t *set, const int sig)
{
/* check that sig is in right range */
if (sig <= 0 || sig >= _NSIG)
{
set_errno (EINVAL);
syscall_printf ("SIG_ERR = sigdelset signal %d out of range", sig);
return -1;
}
*set &= ~SIGTOMASK (sig);
return 0;
}
extern "C" int
sigismember (const sigset_t *set, int sig)
{
/* check that sig is in right range */
if (sig <= 0 || sig >= _NSIG)
{
set_errno (EINVAL);
syscall_printf ("SIG_ERR = sigdelset signal %d out of range", sig);
return -1;
}
if (*set & SIGTOMASK (sig))
return 1;
else
return 0;
}
extern "C" int
sigemptyset (sigset_t *set)
{
*set = (sigset_t) 0;
return 0;
}
extern "C" int
sigfillset (sigset_t *set)
{
*set = ~((sigset_t) 0);
return 0;
}
extern "C" int
sigsuspend (const sigset_t *set)
{
int res = handle_sigsuspend (*set);
syscall_printf ("%R = sigsuspend(%p)", res, set);
return res;
}
extern "C" int
sigpause (int signal_mask)
{
int res = handle_sigsuspend ((sigset_t) signal_mask);
syscall_printf ("%R = sigpause(%y)", res, signal_mask);
return res;
}
extern "C" int
__xpg_sigpause (int sig)
{
int res;
sigset_t signal_mask;
sigprocmask (0, NULL, &signal_mask);
sigdelset (&signal_mask, sig);
res = handle_sigsuspend (signal_mask);
syscall_printf ("%R = __xpg_sigpause(%y)", res, sig);
return res;
}
extern "C" int
pause (void)
{
int res = handle_sigsuspend (_my_tls.sigmask);
syscall_printf ("%R = pause()", res);
return res;
}
extern "C" int
siginterrupt (int sig, int flag)
{
struct sigaction act;
int res = sigaction_worker (sig, NULL, &act, false);
if (res == 0)
{
if (flag)
{
act.sa_flags &= ~SA_RESTART;
act.sa_flags |= _SA_NORESTART;
}
else
{
act.sa_flags &= ~_SA_NORESTART;
act.sa_flags |= SA_RESTART;
}
res = sigaction_worker (sig, &act, NULL, true);
}
syscall_printf ("%R = siginterrupt(%d, %y)", sig, flag);
return res;
}
int
sigwait_common (const sigset_t *set, siginfo_t *info, PLARGE_INTEGER waittime)
{
int res = -1;
pthread_testcancel ();
__try
{
set_signal_mask (_my_tls.sigwait_mask, *set);
sig_dispatch_pending (true);
do_wait:
switch (cygwait (NULL, waittime,
cw_sig_eintr | cw_cancel | cw_cancel_self))
{
case WAIT_SIGNALED:
if (!sigismember (set, _my_tls.infodata.si_signo))
set_errno (EINTR);
else
{
_my_tls.lock ();
if (_my_tls.infodata.si_code == SI_TIMER)
{
timer_tracker *tt = (timer_tracker *)
_my_tls.infodata.si_tid;
_my_tls.infodata.si_overrun = tt->disarm_overrun_event ();
}
if (info)
*info = _my_tls.infodata;
res = _my_tls.infodata.si_signo;
_my_tls.current_sig = 0;
if (_my_tls.retaddr () == (__tlsstack_t) sigdelayed)
_my_tls.pop ();
_my_tls.unlock ();
}
break;
case WAIT_TIMEOUT:
_my_tls.lock ();
if (_my_tls.sigwait_mask == 0)
{
/* sigpacket::process() already started.
Will surely be signalled soon. */
waittime = cw_infinite;
_my_tls.unlock ();
goto do_wait;
}
_my_tls.sigwait_mask = 0;
_my_tls.unlock ();
set_errno (EAGAIN);
break;
default:
__seterrno ();
break;
}
}
__except (EFAULT)
{
res = -1;
}
__endtry
sigproc_printf ("returning signal %d", res);
return res;
}
extern "C" int
sigtimedwait (const sigset_t *set, siginfo_t *info, const timespec *timeout)
{
LARGE_INTEGER waittime;
if (timeout)
{
if (!valid_timespec (*timeout))
{
set_errno (EINVAL);
return -1;
}
/* convert timespec to 100ns units */
waittime.QuadPart = (LONGLONG) timeout->tv_sec * NS100PERSEC
+ ((LONGLONG) timeout->tv_nsec + (NSPERSEC/NS100PERSEC) - 1)
/ (NSPERSEC/NS100PERSEC);
/* negate waittime to code as duration for NtSetTimer() below cygwait() */
waittime.QuadPart = -waittime.QuadPart;
}
return sigwait_common (set, info, timeout ? &waittime : cw_infinite);
}
extern "C" int
sigwait (const sigset_t *set, int *sig_ptr)
{
int sig;
do
{
sig = sigwait_common (set, NULL, cw_infinite);
}
while (sig == -1 && get_errno () == EINTR);
if (sig > 0)
*sig_ptr = sig;
return sig > 0 ? 0 : get_errno ();
}
extern "C" int
sigwaitinfo (const sigset_t *set, siginfo_t *info)
{
return sigwait_common (set, info, cw_infinite);
}
/* FIXME: SUSv3 says that this function should block until the signal has
actually been delivered. Currently, this will only happen when sending
signals to the current process. It will not happen when sending signals
to other processes. */
extern "C" int
sigqueue (pid_t pid, int sig, const union sigval value)
{
siginfo_t si = {0};
pinfo dest (pid);
if (!dest)
{
set_errno (ESRCH);
return -1;
}
if (sig == 0)
return 0;
if (sig < 0 || sig >= _NSIG)
{
set_errno (EINVAL);
return -1;
}
si.si_signo = sig;
si.si_code = SI_QUEUE;
si.si_value = value;
return (int) sig_send (dest, si);
}
extern "C" int
sigaltstack (const stack_t *ss, stack_t *oss)
{
_cygtls& me = _my_tls;
__try
{
if (ss)
{
if (me.altstack.ss_flags == SS_ONSTACK)
{
/* An attempt was made to modify an active stack. */
set_errno (EPERM);
return -1;
}
if (ss->ss_flags == SS_DISABLE)
{
me.altstack.ss_sp = NULL;
me.altstack.ss_flags = 0;
me.altstack.ss_size = 0;
}
else
{
if (ss->ss_flags)
{
/* The ss argument is not a null pointer, and the ss_flags
member pointed to by ss contains flags other than
SS_DISABLE. */
set_errno (EINVAL);
return -1;
}
if (ss->ss_size < MINSIGSTKSZ)
{
/* The size of the alternate stack area is less than
MINSIGSTKSZ. */
set_errno (ENOMEM);
return -1;
}
memcpy (&me.altstack, ss, sizeof *ss);
}
}
if (oss)
{
char stack_marker;
memcpy (oss, &me.altstack, sizeof *oss);
/* Check if the current stack is the alternate signal stack. If so,
set ss_flags accordingly. We do this here rather than setting
ss_flags in _cygtls::call_signal_handler since the signal handler
calls longjmp, so we never return to reset the flag. */
if (!me.altstack.ss_flags && me.altstack.ss_sp)
{
if (&stack_marker >= (char *) me.altstack.ss_sp
&& &stack_marker < (char *) me.altstack.ss_sp
+ me.altstack.ss_size)
oss->ss_flags = SS_ONSTACK;
}
}
}
__except (EFAULT)
{
return EFAULT;
}
__endtry
return 0;
}
extern "C" int
signalfd (int fd_in, const sigset_t *mask, int flags)
{
int ret = -1;
fhandler_signalfd *fh;
debug_printf ("signalfd (%d, %p, %y)", fd_in, mask, flags);
if ((flags & ~(SFD_NONBLOCK | SFD_CLOEXEC)) != 0)
{
set_errno (EINVAL);
goto done;
}
if (fd_in != -1)
{
/* Change signal mask. */
cygheap_fdget fd (fd_in);
if (fd < 0)
goto done;
fh = fd->is_signalfd ();
if (!fh)
{
set_errno (EINVAL);
goto done;
}
__try
{
if (fh->signalfd (mask, flags) == 0)
ret = fd_in;
}
__except (EINVAL) {}
__endtry
}
else
{
/* Create new signalfd descriptor. */
cygheap_fdnew fd;
if (fd < 0)
goto done;
fh = (fhandler_signalfd *) build_fh_dev (*signalfd_dev);
if (fh && fh->signalfd (mask, flags) == 0)
{
fd = fh;
if (fd <= 2)
set_std_handle (fd);
ret = fd;
}
else
delete fh;
}
done:
syscall_printf ("%R = signalfd (%d, %p, %y)", ret, fd_in, mask, flags);
return ret;
}
|