hurd: Only check for TLS initialization inside rtld or in static builds
[glibc.git] / sysdeps / mach / hurd / setresgid.c
blob506bc20513bcea3e71f358e01a751ec7134e46fd
1 /* setresgid -- set real group ID, effective group ID, and saved-set group ID
2 Copyright (C) 2002-2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library 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 GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
19 #include <errno.h>
20 #include <unistd.h>
21 #include <hurd.h>
22 #include <hurd/id.h>
24 /* Set the real group ID, effective group ID, and saved-set group ID,
25 of the calling process to RGID, EGID, and SGID, respectively. */
26 int
27 __setresgid (gid_t rgid, gid_t egid, gid_t sgid)
29 auth_t newauth;
30 error_t err;
32 retry:
33 HURD_CRITICAL_BEGIN;
34 __mutex_lock (&_hurd_id.lock);
35 err = _hurd_check_ids ();
37 if (!err)
39 /* Make a new auth handle which has EGID as the first element in the
40 list of effective gids. */
42 uid_t *newgen, *newaux;
43 uid_t auxs[2] = { rgid, sgid };
44 size_t ngen, naux;
46 newgen = _hurd_id.gen.gids;
47 ngen = _hurd_id.gen.ngids;
48 if (egid != -1)
50 if (_hurd_id.gen.ngids == 0)
52 /* No effective gids now. The new set will be just UID. */
53 newgen = &egid;
54 ngen = 1;
56 else
58 _hurd_id.gen.gids[0] = egid;
59 _hurd_id.valid = 0;
63 newaux = _hurd_id.aux.gids;
64 naux = _hurd_id.aux.ngids;
65 if (rgid != -1)
67 if (_hurd_id.aux.ngids == 0)
69 newaux = &rgid;
70 naux = 1;
72 else
74 _hurd_id.aux.gids[0] = rgid;
75 _hurd_id.valid = 0;
79 if (sgid != -1)
81 if (rgid == -1)
83 if (_hurd_id.aux.ngids >= 1)
84 auxs[0] = _hurd_id.aux.gids[0];
85 else if (_hurd_id.gen.ngids >= 1)
86 auxs[0] = _hurd_id.gen.gids[0];
87 else
88 /* Not even an effective GID.
89 Fall back to the only GID we have. */
90 auxs[0] = sgid;
92 if (_hurd_id.aux.ngids <= 1)
94 /* No saved gids now. The new set will be just UID. */
95 newaux = auxs;
96 naux = 2;
98 else
100 _hurd_id.aux.gids[1] = sgid;
101 _hurd_id.valid = 0;
105 err = __USEPORT (AUTH, __auth_makeauth
106 (port, NULL, MACH_MSG_TYPE_COPY_SEND, 0,
107 _hurd_id.gen.uids, _hurd_id.gen.nuids,
108 _hurd_id.aux.uids, _hurd_id.aux.nuids,
109 newgen, ngen, newaux, naux,
110 &newauth));
112 __mutex_unlock (&_hurd_id.lock);
113 HURD_CRITICAL_END;
114 if (err == EINTR)
115 /* Got a signal while inside an RPC of the critical section, retry again */
116 goto retry;
118 if (err)
119 return __hurd_fail (err);
121 /* Install the new handle and reauthenticate everything. */
122 err = __setauth (newauth);
123 __mach_port_deallocate (__mach_task_self (), newauth);
124 return err;
126 libc_hidden_def (__setresgid)
127 weak_alias (__setresgid, setresgid)