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