Introduce sigtimedwait() and sigwaitinfo()

This change also invents sigcountset() and strsignal_r() and improves
the quality of siginfo_t handling.
This commit is contained in:
Justine Tunney 2022-10-10 07:36:07 -07:00
parent 7ae556463a
commit 467a332e38
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
41 changed files with 887 additions and 345 deletions

View file

@ -0,0 +1,28 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2022 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/fmt/itoa.h"
#include "libc/intrin/describeflags.internal.h"
#include "libc/str/str.h"
const char *(DescribeErrno)(char buf[12], int x) {
const char *s;
if ((s = _strerrno(x))) return s;
FormatInt32(buf, x);
return buf;
}

View file

@ -16,6 +16,7 @@ const char *DescribeArchPrctlCode(char[12], int);
const char *DescribeCapability(char[20], int);
const char *DescribeClockName(char[32], int);
const char *DescribeDirfd(char[12], int);
const char *DescribeErrno(char[12], int);
const char *DescribeErrnoResult(char[12], int);
const char *DescribeFrame(char[32], int);
const char *DescribeFutexOp(char[64], int);
@ -50,6 +51,7 @@ const char *DescribeRemapFlags(char[48], int);
const char *DescribeRlimitName(char[20], int);
const char *DescribeSchedPolicy(char[48], int);
const char *DescribeSeccompOperation(int);
const char *DescribeSiCode(char[17], int, int);
const char *DescribeSleepFlags(char[16], int);
const char *DescribeSockLevel(char[12], int);
const char *DescribeSockOptname(char[32], int, int);
@ -63,6 +65,7 @@ const char *DescribeWhence(char[12], int);
#define DescribeCapability(x) DescribeCapability(alloca(20), x)
#define DescribeClockName(x) DescribeClockName(alloca(32), x)
#define DescribeDirfd(x) DescribeDirfd(alloca(12), x)
#define DescribeErrno(x) DescribeErrno(alloca(12), x)
#define DescribeErrnoResult(x) DescribeErrnoResult(alloca(12), x)
#define DescribeFrame(x) DescribeFrame(alloca(32), x)
#define DescribeFutexOp(x) DescribeFutexOp(alloca(64), x)
@ -94,6 +97,7 @@ const char *DescribeWhence(char[12], int);
#define DescribeRemapFlags(x) DescribeRemapFlags(alloca(48), x)
#define DescribeRlimitName(rl) DescribeRlimitName(alloca(20), rl)
#define DescribeSchedPolicy(x) DescribeSchedPolicy(alloca(48), x)
#define DescribeSiCode(x, y) DescribeSiCode(alloca(17), x, y)
#define DescribeSleepFlags(x) DescribeSleepFlags(alloca(16), x)
#define DescribeSockLevel(x) DescribeSockLevel(alloca(12), x)
#define DescribeSockOptname(x, y) DescribeSockOptname(alloca(32), x, y)

View file

@ -0,0 +1,167 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2022 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/dce.h"
#include "libc/fmt/itoa.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/sicode.h"
#include "libc/sysv/consts/sig.h"
static bool IsSiUser(int si_code) {
if (!IsOpenbsd()) {
return si_code == SI_USER;
} else {
return si_code <= 0;
}
}
static void NameIt(char p[17], const char *s, int si_code) {
p = stpcpy(p, s);
FormatInt32(p, si_code);
}
/**
* Returns symbolic name for siginfo::si_code value.
*/
const char *(DescribeSiCode)(char b[17], int sig, int si_code) {
NameIt(b, "SI_", si_code);
if (si_code == SI_QUEUE) {
strcpy(b + 3, "QUEUE"); /* sent by sigqueue(2) */
} else if (si_code == SI_TIMER) {
strcpy(b + 3, "TIMER"); /* sent by setitimer(2) or clock_settime(2) */
} else if (si_code == SI_TKILL) {
strcpy(b + 3, "TKILL"); /* sent by tkill(2), etc. */
} else if (si_code == SI_MESGQ) {
strcpy(b + 3, "MESGQ"); /* sent by mq_notify(2) */
} else if (si_code == SI_MESGQ) {
strcpy(b + 3, "MESGQ"); /* aio completion */
} else if (si_code == SI_ASYNCNL) {
strcpy(b + 3, "ASYNCNL"); /* aio completion for dns; the horror */
} else if (si_code == SI_NOINFO) {
strcpy(b + 3, "NOINFO"); /* no signal specific info available */
} else if (si_code == SI_KERNEL) {
strcpy(b + 3, "KERNEL"); /* sent by kernel */
} else if (IsSiUser(si_code)) {
strcpy(b + 3, "USER"); /* sent by kill(2) i.e. from userspace */
} else if (sig == SIGCHLD) {
NameIt(b, "CLD_", si_code);
if (si_code == CLD_EXITED) {
strcpy(b + 4, "EXITED"); /* child exited */
} else if (si_code == CLD_KILLED) {
strcpy(b + 4, "KILLED"); /* child terminated w/o core */
} else if (si_code == CLD_DUMPED) {
strcpy(b + 4, "DUMPED"); /* child terminated w/ core */
} else if (si_code == CLD_TRAPPED) {
strcpy(b + 4, "TRAPPED"); /* traced child trapped */
} else if (si_code == CLD_STOPPED) {
strcpy(b + 4, "STOPPED"); /* child stopped */
} else if (si_code == CLD_CONTINUED) {
strcpy(b + 4, "CONTINUED"); /* stopped child continued */
}
} else if (sig == SIGTRAP) {
NameIt(b, "TRAP_", si_code);
if (si_code == TRAP_BRKPT) {
strcpy(b + 5, "BRKPT"); /* process breakpoint */
} else if (si_code == TRAP_TRACE) {
strcpy(b + 5, "TRACE"); /* process trace trap */
}
} else if (sig == SIGSEGV) {
NameIt(b, "SEGV_", si_code);
if (si_code == SEGV_MAPERR) {
strcpy(b + 5, "MAPERR"); /* address not mapped to object */
} else if (si_code == SEGV_ACCERR) {
strcpy(b + 5, "ACCERR"); /* invalid permissions for mapped object */
} else if (si_code == SEGV_PKUERR) {
strcpy(b + 5, "PKUERR"); /* FreeBSD: x86: PKU violation */
}
} else if (sig == SIGFPE) {
NameIt(b, "FPE_???", si_code);
if (si_code == FPE_INTDIV) {
strcpy(b + 4, "INTDIV"); /* integer divide by zero */
} else if (si_code == FPE_INTOVF) {
strcpy(b + 4, "INTOVF"); /* integer overflow */
} else if (si_code == FPE_FLTDIV) {
strcpy(b + 4, "FLTDIV"); /* floating point divide by zero */
} else if (si_code == FPE_FLTOVF) {
strcpy(b + 4, "FLTOVF"); /* floating point overflow */
} else if (si_code == FPE_FLTUND) {
strcpy(b + 4, "FLTUND"); /* floating point underflow */
} else if (si_code == FPE_FLTRES) {
strcpy(b + 4, "FLTRES"); /* floating point inexact */
} else if (si_code == FPE_FLTINV) {
strcpy(b + 4, "FLTINV"); /* invalid floating point operation */
} else if (si_code == FPE_FLTSUB) {
strcpy(b + 4, "FLTSUB"); /* subscript out of range */
}
} else if (sig == SIGILL) {
NameIt(b, "ILL_", si_code);
if (si_code == ILL_ILLOPC) {
strcpy(b + 4, "ILLOPC"); /* illegal opcode */
} else if (si_code == ILL_ILLOPN) {
strcpy(b + 4, "ILLOPN"); /* illegal operand */
} else if (si_code == ILL_ILLADR) {
strcpy(b + 4, "ILLADR"); /* illegal addressing mode */
} else if (si_code == ILL_ILLTRP) {
strcpy(b + 4, "ILLTRP"); /* illegal trap */
} else if (si_code == ILL_PRVOPC) {
strcpy(b + 4, "PRVOPC"); /* privileged opcode */
} else if (si_code == ILL_PRVREG) {
strcpy(b + 4, "PRVREG"); /* privileged register */
} else if (si_code == ILL_COPROC) {
strcpy(b + 4, "COPROC"); /* coprocessor error */
} else if (si_code == ILL_BADSTK) {
strcpy(b + 4, "BADSTK"); /* internal stack error */
}
} else if (sig == SIGBUS) {
NameIt(b, "BUS_", si_code);
if (si_code == BUS_ADRALN) {
strcpy(b + 4, "ADRALN"); /* invalid address alignment */
} else if (si_code == BUS_ADRERR) {
strcpy(b + 4, "ADRERR"); /* non-existent physical address */
} else if (si_code == BUS_OBJERR) {
strcpy(b + 4, "OBJERR"); /* object specific hardware error */
} else if (si_code == BUS_OOMERR) {
strcpy(b + 4, "OOMERR"); /* FreeBSD */
} else if (si_code == BUS_MCEERR_AR) {
strcpy(b + 4, "MCEERR_AR"); /* Linux 2.6.32+ */
} else if (si_code == BUS_MCEERR_AO) {
strcpy(b + 4, "MCEERR_AO"); /* Linux 2.6.32+ */
}
} else if (sig == SIGIO) {
NameIt(b, "POLL_", si_code);
if (si_code == POLL_IN) {
strcpy(b + 5, "IN"); /* data input available */
} else if (si_code == POLL_OUT) {
strcpy(b + 5, "OUT"); /* output buffer available */
} else if (si_code == POLL_MSG) {
strcpy(b + 5, "MSG"); /* input message available */
} else if (si_code == POLL_ERR) {
strcpy(b + 5, "ERR"); /* i/o error */
} else if (si_code == POLL_PRI) {
strcpy(b + 5, "PRI"); /* high priority input available */
} else if (si_code == POLL_HUP) {
strcpy(b + 5, "HUP"); /* device disconnected */
}
} else if (sig == SIGSYS) {
NameIt(b, "SYS_", si_code);
if (si_code == SYS_SECCOMP) {
strcpy(b + 4, "SECCOMP");
}
}
return b;
}

View file

@ -0,0 +1,84 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2021 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/calls/struct/siginfo.h"
#include "libc/calls/struct/siginfo.internal.h"
#include "libc/dce.h"
#include "libc/intrin/asan.internal.h"
#include "libc/intrin/describeflags.internal.h"
#include "libc/intrin/kprintf.h"
#include "libc/intrin/weaken.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/sig.h"
#define N 300
#define append(...) i += ksnprintf(buf + i, N - i, __VA_ARGS__)
const char *(DescribeSiginfo)(char buf[N], int rc, const siginfo_t *si) {
int i = 0;
if (rc == -1) return "n/a";
if (!si) return "NULL";
if ((!IsAsan() && kisdangerous(si)) ||
(IsAsan() && !__asan_is_valid(si, sizeof(*si)))) {
ksnprintf(buf, N, "%p", si);
return buf;
}
append("{.si_signo=%s, si_code=%s", strsignal(si->si_signo),
DescribeSiCode(si->si_signo, si->si_code));
if (si->si_errno) {
append(", .si_errno=%s", DescribeErrno(si->si_errno));
}
if (si->si_signo == SIGILL || //
si->si_signo == SIGFPE || //
si->si_signo == SIGSEGV || //
si->si_signo == SIGBUS || //
si->si_signo == SIGTRAP) {
append(", .si_addr=%p, .si_addr_lsb=%d, .si_pkey=%u", si->si_addr,
si->si_addr_lsb, si->si_pkey);
} else if (si->si_signo == SIGALRM) {
append(", .si_timerid=%d, .si_overrun=%d", si->si_timerid, si->si_overrun);
} else {
if (si->si_pid) {
append(", .si_pid=%d", si->si_pid);
}
if (si->si_uid) {
append(", .si_uid=%d", si->si_uid);
}
}
if (si->si_status) {
append(", .si_status=%d", si->si_status);
}
if (si->si_utime) {
append(", .si_utime=%d", si->si_utime);
}
if (si->si_stime) {
append(", .si_stime=%d", si->si_stime);
}
append("}");
return buf;
}

View file

@ -26,9 +26,11 @@
#define N 128
#define append(...) i += ksnprintf(buf + i, N - i, __VA_ARGS__)
const char *(DescribeSigset)(char buf[N], int rc, const sigset_t *ss) {
bool gotsome;
int i, sig;
bool gotsome;
sigset_t sigset;
if (rc == -1) return "n/a";
@ -43,22 +45,22 @@ const char *(DescribeSigset)(char buf[N], int rc, const sigset_t *ss) {
sigset = *ss;
gotsome = false;
if (popcnt(sigset.__bits[0] & 0xffffffff) > 16) {
i += ksnprintf(buf + i, N - i, "~");
append("~");
sigset.__bits[0] = ~sigset.__bits[0];
sigset.__bits[1] = ~sigset.__bits[1];
}
i += ksnprintf(buf + i, N - i, "{");
append("{");
for (sig = 1; sig < 32; ++sig) {
if (sigismember(&sigset, sig)) {
if (gotsome) {
i += ksnprintf(buf + i, N - i, ",");
append(",");
} else {
gotsome = true;
}
i += ksnprintf(buf + i, N - i, "%s", strsignal(sig) + 3);
append("%s", strsignal(sig) + 3);
}
}
i += ksnprintf(buf + i, N - i, "}");
append("}");
return buf;
}

View file

@ -22,67 +22,64 @@
#include "libc/intrin/asan.internal.h"
#include "libc/intrin/kprintf.h"
const char *(DescribeStat)(char buf[300], int rc, const struct stat *st) {
int i, n;
#define N 300
#define append(...) i += ksnprintf(buf + i, N - i, __VA_ARGS__)
const char *(DescribeStat)(char buf[N], int rc, const struct stat *st) {
int i = 0;
if (rc == -1) return "n/a";
if (!st) return "NULL";
if ((!IsAsan() && kisdangerous(st)) ||
(IsAsan() && !__asan_is_valid(st, sizeof(*st)))) {
ksnprintf(buf, 300, "%p", st);
ksnprintf(buf, N, "%p", st);
return buf;
}
i = 0;
n = 300;
i += ksnprintf(buf + i, n - i, "{.st_%s=%'ld", "size", st->st_size);
append("{.st_%s=%'ld", "size", st->st_size);
if (st->st_blocks) {
i +=
ksnprintf(buf + i, n - i, ", .st_blocks=%'lu/512", st->st_blocks * 512);
append(", .st_blocks=%'lu/512", st->st_blocks * 512);
}
if (st->st_mode) {
i += ksnprintf(buf + i, n - i, ", .st_%s=%#o", "mode", st->st_mode);
append(", .st_%s=%#o", "mode", st->st_mode);
}
if (st->st_nlink != 1) {
i += ksnprintf(buf + i, n - i, ", .st_%s=%'lu", "nlink", st->st_nlink);
append(", .st_%s=%'lu", "nlink", st->st_nlink);
}
if (st->st_uid) {
i += ksnprintf(buf + i, n - i, ", .st_%s=%lu", "uid", st->st_uid);
append(", .st_%s=%lu", "uid", st->st_uid);
}
if (st->st_gid) {
i += ksnprintf(buf + i, n - i, ", .st_%s=%lu", "gid", st->st_gid);
append(", .st_%s=%lu", "gid", st->st_gid);
}
if (st->st_ino) {
i += ksnprintf(buf + i, n - i, ", .st_%s=%lu", "ino", st->st_ino);
append(", .st_%s=%lu", "ino", st->st_ino);
}
if (st->st_gen) {
i += ksnprintf(buf + i, n - i, ", .st_%s=%'lu", "gen", st->st_gen);
append(", .st_%s=%'lu", "gen", st->st_gen);
}
if (st->st_flags) {
i += ksnprintf(buf + i, n - i, ", .st_%s=%lx", "flags", st->st_flags);
append(", .st_%s=%lx", "flags", st->st_flags);
}
if (st->st_rdev) {
i += ksnprintf(buf + i, n - i, ", .st_%s=%'lu", "rdev", st->st_rdev);
append(", .st_%s=%'lu", "rdev", st->st_rdev);
}
if (st->st_blksize != PAGESIZE) {
i += ksnprintf(buf + i, n - i, ", .st_%s=%'lu", "blksize", st->st_blksize);
append(", .st_%s=%'lu", "blksize", st->st_blksize);
}
if (n - i >= 2) {
buf[i + 0] = '}';
buf[i + 1] = 0;
}
append("}");
return buf;
}

View file

@ -24,8 +24,12 @@
#include "libc/intrin/kprintf.h"
#include "libc/sysv/consts/st.h"
const char *(DescribeStatfs)(char buf[300], int rc, const struct statfs *f) {
int i, n;
#define N 300
#define append(...) i += ksnprintf(buf + i, N - i, __VA_ARGS__)
const char *(DescribeStatfs)(char buf[N], int rc, const struct statfs *f) {
int i = 0;
char ibuf[21];
int64_t flags;
@ -33,90 +37,82 @@ const char *(DescribeStatfs)(char buf[300], int rc, const struct statfs *f) {
if (!f) return "NULL";
if ((!IsAsan() && kisdangerous(f)) ||
(IsAsan() && !__asan_is_valid(f, sizeof(*f)))) {
ksnprintf(buf, 300, "%p", f);
ksnprintf(buf, N, "%p", f);
return buf;
}
i = 0;
n = 300;
i += ksnprintf(buf + i, n - i, "{.f_type=%#lx /* %s */", f->f_type,
f->f_fstypename);
append("{.f_type=%#lx /* %s */", f->f_type, f->f_fstypename);
sizefmt(ibuf, f->f_bsize, 1024);
i += ksnprintf(buf + i, n - i, ", .f_%s=%sb", "bsize", ibuf);
append(", .f_%s=%sb", "bsize", ibuf);
sizefmt(ibuf, f->f_blocks * f->f_bsize, 1024);
i += ksnprintf(buf + i, n - i, ", .f_%s=%sb", "blocks", ibuf);
append(", .f_%s=%sb", "blocks", ibuf);
sizefmt(ibuf, f->f_bfree * f->f_bsize, 1024);
i += ksnprintf(buf + i, n - i, ", .f_%s=%sb", "bfree", ibuf);
append(", .f_%s=%sb", "bfree", ibuf);
sizefmt(ibuf, f->f_bavail * f->f_bsize, 1024);
i += ksnprintf(buf + i, n - i, ", .f_%s=%sb", "bavail", ibuf);
append(", .f_%s=%sb", "bavail", ibuf);
i += ksnprintf(buf + i, n - i, ", .f_%s=%'zu", "files", f->f_files);
i += ksnprintf(buf + i, n - i, ", .f_%s=%'zu", "ffree", f->f_ffree);
i += ksnprintf(buf + i, n - i, ", .f_fsid=%#lx", f->f_fsid);
i += ksnprintf(buf + i, n - i, ", .f_%s=%'zu", "namelen", f->f_namelen);
i += ksnprintf(buf + i, n - i, ", .f_%s=%d", "owner", f->f_owner);
append(", .f_%s=%'zu", "files", f->f_files);
append(", .f_%s=%'zu", "ffree", f->f_ffree);
append(", .f_fsid=%#lx", f->f_fsid);
append(", .f_%s=%'zu", "namelen", f->f_namelen);
append(", .f_%s=%d", "owner", f->f_owner);
flags = f->f_flags;
i += ksnprintf(buf + i, n - i, ", .f_flags=");
append(", .f_flags=");
if (ST_RDONLY && (flags & ST_RDONLY)) {
i += ksnprintf(buf + i, n - i, "ST_RDONLY|");
append("ST_RDONLY|");
flags &= ~ST_RDONLY;
}
if (ST_NOSUID && (flags & ST_NOSUID)) {
i += ksnprintf(buf + i, n - i, "ST_NOSUID|");
append("ST_NOSUID|");
flags &= ~ST_NOSUID;
}
if (ST_NODEV && (flags & ST_NODEV)) {
i += ksnprintf(buf + i, n - i, "ST_NODEV|");
append("ST_NODEV|");
flags &= ~ST_NODEV;
}
if (ST_NOEXEC && (flags & ST_NOEXEC)) {
i += ksnprintf(buf + i, n - i, "ST_NOEXEC|");
append("ST_NOEXEC|");
flags &= ~ST_NOEXEC;
}
if (ST_SYNCHRONOUS && (flags & ST_SYNCHRONOUS)) {
i += ksnprintf(buf + i, n - i, "ST_SYNCHRONOUS|");
append("ST_SYNCHRONOUS|");
flags &= ~ST_SYNCHRONOUS;
}
if (ST_MANDLOCK && (flags & ST_MANDLOCK)) {
i += ksnprintf(buf + i, n - i, "ST_MANDLOCK|");
append("ST_MANDLOCK|");
flags &= ~ST_MANDLOCK;
}
if (ST_WRITE && (flags & ST_WRITE)) {
i += ksnprintf(buf + i, n - i, "ST_WRITE|");
append("ST_WRITE|");
flags &= ~ST_WRITE;
}
if (ST_APPEND && (flags & ST_APPEND)) {
i += ksnprintf(buf + i, n - i, "ST_APPEND|");
append("ST_APPEND|");
flags &= ~ST_APPEND;
}
if (ST_IMMUTABLE && (flags & ST_IMMUTABLE)) {
i += ksnprintf(buf + i, n - i, "ST_IMMUTABLE|");
append("ST_IMMUTABLE|");
flags &= ~ST_IMMUTABLE;
}
if (ST_NOATIME && (flags & ST_NOATIME)) {
i += ksnprintf(buf + i, n - i, "ST_NOATIME|");
append("ST_NOATIME|");
flags &= ~ST_NOATIME;
}
if (ST_NODIRATIME && (flags & ST_NODIRATIME)) {
i += ksnprintf(buf + i, n - i, "ST_NODIRATIME|");
append("ST_NODIRATIME|");
flags &= ~ST_NODIRATIME;
}
if (ST_RELATIME && (flags & ST_RELATIME)) {
i += ksnprintf(buf + i, n - i, "ST_RELATIME|");
append("ST_RELATIME|");
flags &= ~ST_RELATIME;
}
i += ksnprintf(buf + i, n - i, "%#lx", flags);
if (n - i >= 2) {
buf[i + 0] = '}';
buf[i + 1] = 0;
}
append("%#lx}", flags);
return buf;
}

View file

@ -212,7 +212,7 @@ privileged static size_t kformat(char *b, size_t n, const char *fmt,
if (!kistextpointer(fmt)) fmt = "!!WONTFMT";
p = b;
f = fmt;
e = p + n;
e = p + n; // assume if n was negative e < p will be the case
for (;;) {
for (;;) {
if (!(c = *f++) || c == '%') break;
@ -502,7 +502,7 @@ privileged static size_t kformat(char *b, size_t n, const char *fmt,
case 'G':
x = va_arg(va, int);
if (_weaken(strsignal) && (s = _weaken(strsignal)(x))) {
if (_weaken(strsignal_r) && (s = _weaken(strsignal_r)(x, z))) {
goto FormatString;
} else {
goto FormatDecimal;

View file

@ -23,7 +23,7 @@
* Adds signal to set.
*
* @return 0 on success, or -1 w/ errno
* @raises EINVAL if `1 sig NSIG` isn't the case
* @raise EINVAL if `1 sig NSIG` isn't the case
* @asyncsignalsafe
*/
int sigaddset(sigset_t *set, int sig) {

35
libc/intrin/sigcountset.c Normal file
View file

@ -0,0 +1,35 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/calls/struct/sigset.h"
#include "libc/intrin/popcnt.h"
#include "libc/macros.internal.h"
/**
* Returns population count of signal set.
*
* @return value greater than or equal to zero
* @asyncsignalsafe
*/
int sigcountset(const sigset_t *set) {
int r, i;
for (r = i = 0; i < ARRAYLEN(set->__bits); ++i) {
r += popcnt(set->__bits[i]);
}
return r;
}

View file

@ -16,52 +16,26 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/fmt/magnumstrs.internal.h"
#include "libc/str/str.h"
static char g_strsignal[12];
static char g_strsignal[15];
/**
* Returns string describing signal code.
*
* This returns SIGZERO for 0 which is the empty value. Textual names
* should be available for signals 1 through 32. Signals in the range 33
* and 128 are returned as a `SIG%03d` string. Everything else is SIGWUT
* This returns `"0"` for 0 which is the empty value. Symbolic names
* should be available for signals 1 through 32. If the system supports
* real-time signals, they're returned as `SIGRTMIN+%d`. For all other
* 32-bit signed integer, a plain integer representation is returned.
*
* This function is thread safe when `sig` is a known signal magnum.
* Otherwise a pointer to static memory is returned which is unsafe.
*
* @param sig is signal number which should be in range 1 through 128
* @return pointer to static memory that mutates on subsequent calls
* @return string which is valid code describing signal
* @see strsignal_r() for better thread safety
* @see sigaction()
* @threadsafe
*/
char *strsignal(int sig) {
char *p;
const char *s;
if (!sig) return "0";
if ((s = GetMagnumStr(kSignalNames, sig))) return s;
p = g_strsignal;
p[0] = 'S';
p[1] = 'I';
p[2] = 'G';
p[3] = 0;
if (!sig) {
p[3] = 'Z';
p[4] = 'E';
p[5] = 'R';
p[6] = 'O';
p[7] = 0;
} else if (1 <= sig && sig <= 128) {
p[3] = '0' + sig / 100;
p[4] = '0' + sig / 10 % 10;
p[5] = '0' + sig % 10;
p[6] = 0;
} else {
p[3] = 'W';
p[4] = 'U';
p[5] = 'T';
p[6] = 0;
}
return g_strsignal;
return strsignal_r(sig, g_strsignal);
}

60
libc/intrin/strsignal_r.c Normal file
View file

@ -0,0 +1,60 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/fmt/itoa.h"
#include "libc/fmt/magnumstrs.internal.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/sig.h"
/**
* Returns string describing signal code.
*
* This returns `"0"` for 0 which is the empty value. Symbolic names
* should be available for signals 1 through 32. If the system supports
* real-time signals, they're returned as `SIGRTMIN+%d`. For all other
* 32-bit signed integer, a plain integer representation is returned.
*
* @param sig is signal number which should be in range 1 through 128
* @param buf may be used to store output having at least 15 bytes
* @return pointer to .rodata string, or to `buf` after mutating
* @see sigaction()
* @threadsafe
*/
char *strsignal_r(int sig, char buf[hasatleast 15]) {
int i;
char *p;
const char *s;
if (!sig) return "0";
if ((s = GetMagnumStr(kSignalNames, sig))) return s;
if (SIGRTMIN <= sig && sig <= SIGRTMAX) {
sig -= SIGRTMIN;
buf[0] = 'S';
buf[1] = 'I';
buf[2] = 'G';
buf[3] = 'R';
buf[4] = 'T';
buf[5] = 'M';
buf[6] = 'I';
buf[7] = 'N';
buf[8] = '+';
FormatInt32(buf + 9, sig);
} else {
FormatInt32(buf, sig);
}
return buf;
}