bitset: properly use false/true instead of 0/1 for Booleans
[gnulib.git] / lib / priv-set.c
blob3f55a560ba94ddc264dfaf8712da8b8d241ec235
1 /* Query, remove, or restore a Solaris privilege.
3 Copyright (C) 2009-2018 Free Software Foundation, Inc.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>.
18 Written by David Bartley. */
20 #include <config.h>
22 #define PRIV_SET_INLINE _GL_EXTERN_INLINE
24 #include "priv-set.h"
26 #if HAVE_GETPPRIV && HAVE_PRIV_H
28 # include <errno.h>
29 # include <stdbool.h>
30 # include <priv.h>
32 /* Holds a (cached) copy of the effective set. */
33 static priv_set_t *eff_set;
35 /* Holds a set of privileges that we have removed. */
36 static priv_set_t *rem_set;
38 static bool initialized;
40 static int
41 priv_set_initialize (void)
43 if (! initialized)
45 eff_set = priv_allocset ();
46 if (!eff_set)
48 return -1;
50 rem_set = priv_allocset ();
51 if (!rem_set)
53 priv_freeset (eff_set);
54 return -1;
56 if (getppriv (PRIV_EFFECTIVE, eff_set) != 0)
58 priv_freeset (eff_set);
59 priv_freeset (rem_set);
60 return -1;
62 priv_emptyset (rem_set);
63 initialized = true;
66 return 0;
70 /* Check if priv is in the effective set.
71 Returns 1 if priv is a member and 0 if not.
72 Returns -1 on error with errno set appropriately. */
73 int
74 priv_set_ismember (const char *priv)
76 if (! initialized && priv_set_initialize () != 0)
77 return -1;
79 return priv_ismember (eff_set, priv);
83 /* Try to remove priv from the effective set.
84 Returns 0 if priv was removed.
85 Returns -1 on error with errno set appropriately. */
86 int
87 priv_set_remove (const char *priv)
89 if (! initialized && priv_set_initialize () != 0)
90 return -1;
92 if (priv_ismember (eff_set, priv))
94 /* priv_addset/priv_delset can only fail if priv is invalid, which is
95 checked above by the priv_ismember call. */
96 priv_delset (eff_set, priv);
97 if (setppriv (PRIV_SET, PRIV_EFFECTIVE, eff_set) != 0)
99 priv_addset (eff_set, priv);
100 return -1;
102 priv_addset (rem_set, priv);
104 else
106 errno = EINVAL;
107 return -1;
110 return 0;
114 /* Try to restore priv to the effective set.
115 Returns 0 if priv was re-added to the effective set (after being previously
116 removed by a call to priv_set_remove).
117 Returns -1 on error with errno set appropriately. */
119 priv_set_restore (const char *priv)
121 if (! initialized && priv_set_initialize () != 0)
122 return -1;
124 if (priv_ismember (rem_set, priv))
126 /* priv_addset/priv_delset can only fail if priv is invalid, which is
127 checked above by the priv_ismember call. */
128 priv_addset (eff_set, priv);
129 if (setppriv (PRIV_SET, PRIV_EFFECTIVE, eff_set) != 0)
131 priv_delset (eff_set, priv);
132 return -1;
134 priv_delset (rem_set, priv);
136 else
138 errno = EINVAL;
139 return -1;
142 return 0;
145 #endif