Make futexes cancellable by pthreads

This commit is contained in:
Justine Tunney 2022-11-04 18:19:05 -07:00
parent 2278327eba
commit 022536cab6
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
101 changed files with 627 additions and 391 deletions

View file

@ -16,6 +16,7 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/calls/blockcancel.internal.h"
#include "libc/calls/calls.h"
#include "libc/calls/dprintf.h"
#include "libc/calls/struct/timespec.h"
@ -124,16 +125,13 @@ void vsyslog(int priority, const char *message, va_list ap) {
int l, l2;
int hlen; /* If LOG_CONS is specified, use to store the point in
* the header message after the timestamp */
BLOCK_CANCELLATIONS;
if (log_fd < 0) __openlog();
if (!(priority & LOG_FACMASK)) priority |= log_facility;
/* Build the time string */
now = Time(NULL);
gmtime_r(&now, &tm);
strftime(timebuf, sizeof(timebuf), "%b %e %T", &tm);
pid = (log_opt & LOG_PID) ? getpid() : 0;
/* This is a clever trick to optionally include "[<pid>]"
* only if pid != 0. When pid==0, the while "[%.0d]" is skipped:
@ -149,7 +147,6 @@ void vsyslog(int priority, const char *message, va_list ap) {
l += snprintf(buf + l, sizeof(buf) - l, "%hs%s%.0d%s: ", log_ident,
"[" + !pid, pid, "]" + !pid);
errno = errno_save;
/* Append user message */
l2 = vsnprintf(buf + l, sizeof(buf) - l, message, ap);
if (l2 >= 0) {
@ -166,37 +163,17 @@ void vsyslog(int priority, const char *message, va_list ap) {
* - First try to send it to syslogd
* - If fails and LOG_CONS is provided, writes to /dev/console
*/
#if 0
if (send(log_fd, buf, l, 0) < 0 && (!is_lost_conn(errno)
|| connect(log_fd, (void *)&log_addr, sizeof(log_addr)) < 0
|| send(log_fd, buf, l, 0) < 0)
&& (log_opt & LOG_CONS)) {
int fd = open("/dev/console", O_WRONLY|O_NOCTTY);
if (send(log_fd, buf, l, 0) < 0 &&
(!is_lost_conn(errno) ||
connect(log_fd, (void *)&log_addr, sizeof(log_addr)) < 0 ||
send(log_fd, buf, l, 0) < 0) &&
(log_opt & LOG_CONS)) {
int fd = open("/dev/console", O_WRONLY | O_NOCTTY);
if (fd >= 0) {
dprintf(fd, "%.*s", l-hlen, buf+hlen);
dprintf(fd, "%.*s", l - hlen, buf + hlen);
close(fd);
}
}
#else
int rc = send(log_fd, buf, l, 0);
if (rc < 0) {
printf("ERR: send(1) failed: %s (errno=%d)\n", strerror(errno), errno);
if (!is_lost_conn(errno)) {
rc = connect(log_fd, (void *)&log_addr, sizeof(log_addr));
if (rc < 0) {
printf("ERR: connect(syslog) failed: %s (errno=%d)\n",
strerror(errno), errno);
} else {
rc = send(log_fd, buf, l, 0);
if (rc < 0) {
printf("ERR: send(2) failed: %s (errno=%d)\n", strerror(errno),
errno);
}
}
}
}
#endif
} else {
uint16_t evtType;
uint32_t evtID;
@ -229,11 +206,11 @@ void vsyslog(int priority, const char *message, va_list ap) {
NULL /* Arguments */);
++log_id;
}
if (log_opt & LOG_PERROR) {
dprintf(2, "%.*s", l - hlen, buf + hlen);
}
}
ALLOW_CANCELLATIONS;
}
/**
@ -250,9 +227,7 @@ void vsyslog(int priority, const char *message, va_list ap) {
*/
int setlogmask(int maskpri) {
int ret;
if (log_facility == -1) {
__initlog();
}
if (log_facility == -1) __initlog();
ret = log_mask;
if (maskpri) log_mask = LOG_PRI(maskpri);
return ret;
@ -287,19 +262,15 @@ int setlogmask(int maskpri) {
*/
void openlog(const char *ident, int opt, int facility) {
size_t n;
if (log_facility == -1) {
__initlog();
}
if (!ident) {
ident = firstnonnull(program_invocation_short_name, "unknown");
}
BLOCK_CANCELLATIONS;
if (log_facility == -1) __initlog();
if (!ident) ident = firstnonnull(program_invocation_short_name, "unknown");
tprecode8to16(log_ident, ARRAYLEN(log_ident), ident);
log_opt = opt;
log_facility = facility;
log_id = 0;
if ((opt & LOG_NDELAY) && log_fd < 0) __openlog();
ALLOW_CANCELLATIONS;
}
/**
@ -339,13 +310,12 @@ void closelog(void) {
if (log_facility == -1) {
__initlog();
}
if (log_fd == -1) {
return;
if (log_fd != -1) {
if (IsWindows()) {
DeregisterEventSource(log_fd);
} else {
close(log_fd);
}
log_fd = -1;
}
if (IsWindows()) {
DeregisterEventSource(log_fd);
} else {
close(log_fd);
}
log_fd = -1;
}