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