xref: /illumos-gate/usr/src/lib/libc/port/gen/sigsetops.c (revision 8cd45542)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*	Copyright (c) 1988 AT&T	*/
30 /*	  All Rights Reserved  	*/
31 
32 /*
33  * POSIX signal manipulation functions.
34  */
35 #pragma weak sigfillset = _sigfillset
36 #pragma weak sigemptyset = _sigemptyset
37 #pragma weak sigaddset = _sigaddset
38 #pragma weak sigdelset = _sigdelset
39 #pragma weak sigismember = _sigismember
40 
41 #include "synonyms.h"
42 #include <sys/types.h>
43 #include <stdio.h>
44 #include <sys/param.h>
45 #include <sys/errno.h>
46 #include <sys/signal.h>
47 #include <errno.h>
48 #include "libc.h"
49 
50 #define	SIGSETSIZE	4
51 #define	MAXBITNO (NBPW*8)
52 
53 static sigset_t sigs;
54 static int sigsinit;
55 
56 #define	sigword(n) ((n-1)/MAXBITNO)
57 #define	bitmask(n) (1L<<((n-1)%MAXBITNO))
58 
59 static int
60 sigvalid(int sig)
61 {
62 	if (sig <= 0 || sig > (MAXBITNO * SIGSETSIZE))
63 		return (0);
64 
65 	if (!sigsinit) {
66 		(void) __sigfillset(&sigs);
67 		sigsinit++;
68 	}
69 
70 	return ((sigs.__sigbits[sigword(sig)] & bitmask(sig)) != 0);
71 }
72 
73 int
74 sigfillset(sigset_t *set)
75 {
76 	if (!sigsinit) {
77 		(void) __sigfillset(&sigs);
78 		sigsinit++;
79 	}
80 
81 	*set = sigs;
82 	return (0);
83 }
84 
85 int
86 sigemptyset(sigset_t *set)
87 {
88 	set->__sigbits[0] = 0;
89 	set->__sigbits[1] = 0;
90 	set->__sigbits[2] = 0;
91 	set->__sigbits[3] = 0;
92 	return (0);
93 }
94 
95 int
96 sigaddset(sigset_t *set, int sig)
97 {
98 	if (!sigvalid(sig)) {
99 		errno = EINVAL;
100 		return (-1);
101 	}
102 	set->__sigbits[sigword(sig)] |= bitmask(sig);
103 	return (0);
104 }
105 
106 int
107 sigdelset(sigset_t *set, int sig)
108 {
109 	if (!sigvalid(sig)) {
110 		errno = EINVAL;
111 		return (-1);
112 	}
113 	set->__sigbits[sigword(sig)] &= ~bitmask(sig);
114 	return (0);
115 }
116 
117 int
118 sigismember(const sigset_t *set, int sig)
119 {
120 	if (!sigvalid(sig)) {
121 		errno = EINVAL;
122 		return (-1);
123 	}
124 	return ((set->__sigbits[sigword(sig)] & bitmask(sig)) != 0);
125 }
126