cosmopolitan/third_party/musl/grp.c

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

267 lines
8.1 KiB
C
Raw Permalink Normal View History

Improve ZIP filesystem and change its prefix The ZIP filesystem has a breaking change. You now need to use /zip/ to open() / opendir() / etc. assets within the ZIP structure of your APE binary, instead of the previous convention of using zip: or zip! URIs. This is needed because Python likes to use absolute paths, and having ZIP paths encoded like URIs simply broke too many things. Many more system calls have been updated to be able to operate on ZIP files and file descriptors. In particular fcntl() and ioctl() since Python would do things like ask if a ZIP file is a terminal and get confused when the old implementation mistakenly said yes, because the fastest way to guarantee native file descriptors is to dup(2). This change also improves the async signal safety of zipos and ensures it doesn't maintain any open file descriptors beyond that which the user has opened. This change makes a lot of progress towards adding magic numbers that are specific to platforms other than Linux. The philosophy here is that, if you use an operating system like FreeBSD, then you should be able to take advantage of FreeBSD exclusive features, even if we don't polyfill them on other platforms. For example, you can now open() a file with the O_VERIFY flag. If your program runs on other platforms, then Cosmo will automatically set O_VERIFY to zero. This lets you safely use it without the need for #ifdef or ifstatements which detract from readability. One of the blindspots of the ASAN memory hardening we use to offer Rust like assurances has always been that memory passed to the kernel via system calls (e.g. writev) can't be checked automatically since the kernel wasn't built with MODE=asan. This change makes more progress ensuring that each system call will verify the soundness of memory before it's passed to the kernel. The code for doing these checks is fast, particularly for buffers, where it can verify 64 bytes a cycle. - Correct O_LOOP definition on NT - Introduce program_executable_name - Add ASAN guards to more system calls - Improve termios compatibility with BSDs - Fix bug in Windows auxiliary value encoding - Add BSD and XNU specific errnos and open flags - Add check to ensure build doesn't talk to internet
2021-08-22 08:04:18 +00:00
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│
vi: set et ft=c ts=8 sw=8 fenc=utf-8 :vi
2020-06-15 14:18:57 +00:00
Musl Libc
Copyright © 2005-2014 Rich Felker, et al.
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
2023-09-22 06:51:55 +00:00
#include "libc/calls/calls.h"
2020-06-15 14:18:57 +00:00
#include "libc/calls/weirdtypes.h"
#include "libc/errno.h"
#include "libc/mem/mem.h"
#include "libc/stdio/stdio.h"
#include "libc/str/str.h"
2023-09-22 06:51:55 +00:00
#include "libc/sysv/consts/limits.h"
2022-11-05 01:19:05 +00:00
#include "libc/thread/thread.h"
2020-06-15 14:18:57 +00:00
#include "third_party/musl/passwd.h"
Release Cosmopolitan v3.3 This change upgrades to GCC 12.3 and GNU binutils 2.42. The GNU linker appears to have changed things so that only a single de-duplicated str table is present in the binary, and it gets placed wherever the linker wants, regardless of what the linker script says. To cope with that we need to stop using .ident to embed licenses. As such, this change does significant work to revamp how third party licenses are defined in the codebase, using `.section .notice,"aR",@progbits`. This new GCC 12.3 toolchain has support for GNU indirect functions. It lets us support __target_clones__ for the first time. This is used for optimizing the performance of libc string functions such as strlen and friends so far on x86, by ensuring AVX systems favor a second codepath that uses VEX encoding. It shaves some latency off certain operations. It's a useful feature to have for scientific computing for the reasons explained by the test/libcxx/openmp_test.cc example which compiles for fifteen different microarchitectures. Thanks to the upgrades, it's now also possible to use newer instruction sets, such as AVX512FP16, VNNI. Cosmo now uses the %gs register on x86 by default for TLS. Doing it is helpful for any program that links `cosmo_dlopen()`. Such programs had to recompile their binaries at startup to change the TLS instructions. That's not great, since it means every page in the executable needs to be faulted. The work of rewriting TLS-related x86 opcodes, is moved to fixupobj.com instead. This is great news for MacOS x86 users, since we previously needed to morph the binary every time for that platform but now that's no longer necessary. The only platforms where we need fixup of TLS x86 opcodes at runtime are now Windows, OpenBSD, and NetBSD. On Windows we morph TLS to point deeper into the TIB, based on a TlsAlloc assignment, and on OpenBSD/NetBSD we morph %gs back into %fs since the kernels do not allow us to specify a value for the %gs register. OpenBSD users are now required to use APE Loader to run Cosmo binaries and assimilation is no longer possible. OpenBSD kernel needs to change to allow programs to specify a value for the %gs register, or it needs to stop marking executable pages loaded by the kernel as mimmutable(). This release fixes __constructor__, .ctor, .init_array, and lastly the .preinit_array so they behave the exact same way as glibc. We no longer use hex constants to define math.h symbols like M_PI.
2024-02-20 19:12:09 +00:00
__static_yoink("musl_libc_notice");
2020-06-15 14:18:57 +00:00
static unsigned atou(char **s) {
unsigned x;
for (x = 0; **s - '0' < 10U; ++*s) x = 10 * x + (**s - '0');
return x;
}
int __getgrent_a(FILE *f, struct group *gr, char **line, size_t *size,
char ***mem, size_t *nmem, struct group **res) {
2020-06-15 14:18:57 +00:00
ssize_t l;
char *s, *mems;
size_t i;
int rv = 0;
int cs;
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
for (;;) {
if ((l = getline(line, size, f)) < 0) {
rv = ferror(f) ? errno : 0;
free(*line);
*line = 0;
gr = 0;
goto end;
}
line[0][l - 1] = 0;
s = line[0];
gr->gr_name = s++;
if (!(s = strchr(s, ':'))) continue;
*s++ = 0;
gr->gr_passwd = s;
if (!(s = strchr(s, ':'))) continue;
*s++ = 0;
gr->gr_gid = atou(&s);
if (*s != ':') continue;
*s++ = 0;
mems = s;
break;
}
for (*nmem = !!*s; *s; s++)
if (*s == ',') ++*nmem;
free(*mem);
*mem = calloc(sizeof(char *), *nmem + 1);
if (!*mem) {
rv = errno;
free(*line);
*line = 0;
gr = 0;
goto end;
}
if (*mems) {
mem[0][0] = mems;
for (s = mems, i = 0; *s; s++)
if (*s == ',') *s++ = 0, mem[0][++i] = s;
mem[0][++i] = 0;
} else {
mem[0][0] = 0;
}
gr->gr_mem = *mem;
end:
pthread_setcancelstate(cs, 0);
*res = gr;
if (rv) errno = rv;
return rv;
}
int __getgr_a(const char *name, gid_t gid, struct group *gr, char **buf,
size_t *size, char ***mem, size_t *nmem,
struct group **res) {
2020-06-15 14:18:57 +00:00
FILE *f;
int rv = 0;
int cs;
*res = 0;
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
f = fopen("/etc/group", "rbe");
if (!f) {
rv = errno;
goto done;
}
while (!(rv = __getgrent_a(f, gr, buf, size, mem, nmem, res)) && *res) {
if ((name && !strcmp(name, (*res)->gr_name)) ||
(!name && (*res)->gr_gid == gid)) {
break;
}
}
done:
pthread_setcancelstate(cs, 0);
if (rv) errno = rv;
return rv;
}
static int getgr_r(const char *name, gid_t gid, struct group *gr, char *buf,
size_t size, struct group **res) {
#define FIX(x) (gr->gr_##x = gr->gr_##x - line + buf)
char *line = 0;
size_t len = 0;
char **mem = 0;
size_t nmem = 0;
int rv = 0;
size_t i;
int cs;
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
rv = __getgr_a(name, gid, gr, &line, &len, &mem, &nmem, res);
if (*res && size < len + (nmem + 1) * sizeof(char *) + 32) {
*res = 0;
rv = ERANGE;
}
if (*res) {
buf += (16 - (uintptr_t)buf) % 16;
gr->gr_mem = (void *)buf;
buf += (nmem + 1) * sizeof(char *);
memcpy(buf, line, len);
FIX(name);
FIX(passwd);
for (i = 0; mem[i]; i++) gr->gr_mem[i] = mem[i] - line + buf;
gr->gr_mem[i] = 0;
}
free(mem);
free(line);
pthread_setcancelstate(cs, 0);
if (rv) errno = rv;
return rv;
#undef FIX
}
int getgrouplist(const char *user, gid_t gid, gid_t *groups, int *ngroups) {
int rv, nlim, ret = -1;
ssize_t i, n = 1;
struct group gr;
struct group *res;
FILE *f;
char *buf = 0;
char **mem = 0;
size_t nmem = 0;
size_t size;
nlim = *ngroups;
if (nlim >= 1) *groups++ = gid;
f = fopen("/etc/group", "rbe");
if (!f && errno != ENOENT && errno != ENOTDIR) goto cleanup;
if (f) {
while (!(rv = __getgrent_a(f, &gr, &buf, &size, &mem, &nmem, &res)) &&
res) {
for (i = 0; gr.gr_mem[i] && strcmp(user, gr.gr_mem[i]); i++)
;
if (!gr.gr_mem[i]) continue;
if (++n <= nlim) *groups++ = gr.gr_gid;
}
if (rv) {
errno = rv;
goto cleanup;
}
}
ret = n > nlim ? -1 : n;
*ngroups = n;
cleanup:
if (f) fclose(f);
free(buf);
free(mem);
return ret;
}
int getgrnam_r(const char *name, struct group *gr, char *buf, size_t size,
struct group **res) {
return getgr_r(name, 0, gr, buf, size, res);
}
int getgrgid_r(gid_t gid, struct group *gr, char *buf, size_t size,
struct group **res) {
return getgr_r(0, gid, gr, buf, size, res);
}
static struct GetgrentState {
FILE *f;
char *line;
char **mem;
struct group gr;
} g_getgrent[1];
Make improvements - This change fixes a bug that allowed unbuffered printf() output (to streams like stderr) to be truncated. This regression was introduced some time between now and the last release. - POSIX specifies all functions as thread safe by default. This change works towards cleaning up our use of the @threadsafe / @threadunsafe documentation annotations to reflect that. The goal is (1) to use @threadunsafe to document functions which POSIX say needn't be thread safe, and (2) use @threadsafe to document functions that we chose to implement as thread safe even though POSIX didn't mandate it. - Tidy up the clock_gettime() implementation. We're now trying out a cleaner approach to system call support that aims to maintain the Linux errno convention as long as possible. This also fixes bugs that existed previously, where the vDSO errno wasn't being translated properly. The gettimeofday() system call is now a wrapper for clock_gettime(), which reduces bloat in apps that use both. - The recently-introduced improvements to the execute bit on Windows has had bugs fixed. access(X_OK) on a directory on Windows now succeeds. fstat() will now perform the MZ/#! ReadFile() operation correctly. - Windows.h is no longer included in libc/isystem/, because it confused PCRE's build system into thinking Cosmopolitan is a WIN32 platform. Cosmo's Windows.h polyfill was never even really that good, since it only defines a subset of the subset of WIN32 APIs that Cosmo defines. - The setlongerjmp() / longerjmp() APIs are removed. While they're nice APIs that are superior to the standardized setjmp / longjmp functions, they weren't superior enough to not be dead code in the monorepo. If you use these APIs, please file an issue and they'll be restored. - The .com appending magic has now been removed from APE Loader.
2023-10-03 02:25:19 +00:00
/**
* Closes group database.
* @threadunsafe
*/
2020-06-15 14:18:57 +00:00
void endgrent() {
setgrent();
}
Make improvements - This change fixes a bug that allowed unbuffered printf() output (to streams like stderr) to be truncated. This regression was introduced some time between now and the last release. - POSIX specifies all functions as thread safe by default. This change works towards cleaning up our use of the @threadsafe / @threadunsafe documentation annotations to reflect that. The goal is (1) to use @threadunsafe to document functions which POSIX say needn't be thread safe, and (2) use @threadsafe to document functions that we chose to implement as thread safe even though POSIX didn't mandate it. - Tidy up the clock_gettime() implementation. We're now trying out a cleaner approach to system call support that aims to maintain the Linux errno convention as long as possible. This also fixes bugs that existed previously, where the vDSO errno wasn't being translated properly. The gettimeofday() system call is now a wrapper for clock_gettime(), which reduces bloat in apps that use both. - The recently-introduced improvements to the execute bit on Windows has had bugs fixed. access(X_OK) on a directory on Windows now succeeds. fstat() will now perform the MZ/#! ReadFile() operation correctly. - Windows.h is no longer included in libc/isystem/, because it confused PCRE's build system into thinking Cosmopolitan is a WIN32 platform. Cosmo's Windows.h polyfill was never even really that good, since it only defines a subset of the subset of WIN32 APIs that Cosmo defines. - The setlongerjmp() / longerjmp() APIs are removed. While they're nice APIs that are superior to the standardized setjmp / longjmp functions, they weren't superior enough to not be dead code in the monorepo. If you use these APIs, please file an issue and they'll be restored. - The .com appending magic has now been removed from APE Loader.
2023-10-03 02:25:19 +00:00
/**
* Rewinds to beginning of group database.
* @threadunsafe
*/
2020-06-15 14:18:57 +00:00
void setgrent() {
if (g_getgrent->f) fclose(g_getgrent->f);
g_getgrent->f = 0;
}
Make improvements - This change fixes a bug that allowed unbuffered printf() output (to streams like stderr) to be truncated. This regression was introduced some time between now and the last release. - POSIX specifies all functions as thread safe by default. This change works towards cleaning up our use of the @threadsafe / @threadunsafe documentation annotations to reflect that. The goal is (1) to use @threadunsafe to document functions which POSIX say needn't be thread safe, and (2) use @threadsafe to document functions that we chose to implement as thread safe even though POSIX didn't mandate it. - Tidy up the clock_gettime() implementation. We're now trying out a cleaner approach to system call support that aims to maintain the Linux errno convention as long as possible. This also fixes bugs that existed previously, where the vDSO errno wasn't being translated properly. The gettimeofday() system call is now a wrapper for clock_gettime(), which reduces bloat in apps that use both. - The recently-introduced improvements to the execute bit on Windows has had bugs fixed. access(X_OK) on a directory on Windows now succeeds. fstat() will now perform the MZ/#! ReadFile() operation correctly. - Windows.h is no longer included in libc/isystem/, because it confused PCRE's build system into thinking Cosmopolitan is a WIN32 platform. Cosmo's Windows.h polyfill was never even really that good, since it only defines a subset of the subset of WIN32 APIs that Cosmo defines. - The setlongerjmp() / longerjmp() APIs are removed. While they're nice APIs that are superior to the standardized setjmp / longjmp functions, they weren't superior enough to not be dead code in the monorepo. If you use these APIs, please file an issue and they'll be restored. - The .com appending magic has now been removed from APE Loader.
2023-10-03 02:25:19 +00:00
/**
* Returns successive entries in /etc/group database.
* @threadunsafe
*/
2020-06-15 14:18:57 +00:00
struct group *getgrent() {
struct group *res;
size_t size = 0, nmem = 0;
if (!g_getgrent->f) g_getgrent->f = fopen("/etc/group", "rbe");
if (!g_getgrent->f) return 0;
__getgrent_a(g_getgrent->f, &g_getgrent->gr, &g_getgrent->line, &size,
&g_getgrent->mem, &nmem, &res);
return res;
}
struct group *getgrgid(gid_t gid) {
struct group *res;
size_t size = 0, nmem = 0;
__getgr_a(0, gid, &g_getgrent->gr, &g_getgrent->line, &size, &g_getgrent->mem,
&nmem, &res);
return res;
}
struct group *getgrnam(const char *name) {
struct group *res;
size_t size = 0, nmem = 0;
__getgr_a(name, 0, &g_getgrent->gr, &g_getgrent->line, &size,
&g_getgrent->mem, &nmem, &res);
return res;
}
2023-09-22 06:51:55 +00:00
int initgroups(const char *user, gid_t gid) {
gid_t groups[NGROUPS_MAX];
int count = NGROUPS_MAX;
if (getgrouplist(user, gid, groups, &count) < 0) return -1;
return setgroups(count, groups);
}