xref: /illumos-gate/usr/src/lib/libc/port/gen/sigsetops.c (revision 2983dda76a6d296fdb560c88114fe41caad1b84f)
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 /*	Copyright (c) 1988 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
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 "lint.h"
42 #include <sys/types.h>
43 #include <stdio.h>
44 #include <sys/param.h>
45 #include <sys/signal.h>
46 #include <errno.h>
47 #include "libc.h"
48 
49 #define	SIGSETSIZE	4
50 #define	MAXBITNO (NBPW*8)
51 
52 static sigset_t sigs;
53 static int sigsinit;
54 
55 #define	sigword(n) ((n-1)/MAXBITNO)
56 #define	bitmask(n) (1L<<((n-1)%MAXBITNO))
57 
58 static int
59 sigvalid(int sig)
60 {
61 	if (sig <= 0 || sig > (MAXBITNO * SIGSETSIZE))
62 		return (0);
63 
64 	if (!sigsinit) {
65 		(void) __sigfillset(&sigs);
66 		sigsinit++;
67 	}
68 
69 	return ((sigs.__sigbits[sigword(sig)] & bitmask(sig)) != 0);
70 }
71 
72 int
73 sigfillset(sigset_t *set)
74 {
75 	if (!sigsinit) {
76 		(void) __sigfillset(&sigs);
77 		sigsinit++;
78 	}
79 
80 	*set = sigs;
81 	return (0);
82 }
83 
84 int
85 sigemptyset(sigset_t *set)
86 {
87 	set->__sigbits[0] = 0;
88 	set->__sigbits[1] = 0;
89 	set->__sigbits[2] = 0;
90 	set->__sigbits[3] = 0;
91 	return (0);
92 }
93 
94 int
95 sigaddset(sigset_t *set, int sig)
96 {
97 	if (!sigvalid(sig)) {
98 		errno = EINVAL;
99 		return (-1);
100 	}
101 	set->__sigbits[sigword(sig)] |= bitmask(sig);
102 	return (0);
103 }
104 
105 int
106 sigdelset(sigset_t *set, int sig)
107 {
108 	if (!sigvalid(sig)) {
109 		errno = EINVAL;
110 		return (-1);
111 	}
112 	set->__sigbits[sigword(sig)] &= ~bitmask(sig);
113 	return (0);
114 }
115 
116 int
117 sigismember(const sigset_t *set, int sig)
118 {
119 	if (!sigvalid(sig)) {
120 		errno = EINVAL;
121 		return (-1);
122 	}
123 	return ((set->__sigbits[sigword(sig)] & bitmask(sig)) != 0);
124 }
125