mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-02-28 22:18:29 +00:00
This change fixes an issue with the tcflow() magic numbers that was causing bash to freeze up on Linux. While auditing termios polyfills, several other issues were identified with XNU/BSD compatibility. Out of an abundance of caution this change undefines as much surface area from libc/calls/struct/termios.h as possible, so that autoconf scripts are less likely to detect non-POSIX teletypewriter APIs that haven't been polyfilled by Cosmopolitan. This is a *breaking change* for your static archives in /opt/cosmos if you use the cosmocc toolchain. That's because this change disables the ioctl() undiamonding trick for code outside the monorepo, specifically because it'll lead to brittle ABI breakages like this. If you're using the cosmocc toolchain, you'll need to rebuild libraries like ncurses, readline, etc. Yes diamonds cause bloat. To work around that, consider using tcgetwinsize() instead of ioctl(TIOCGWINSZ) since it'll help you avoid pulling every single ioctl-related polyfill into the linkage. The cosmocc script was specifying -DNDEBUG for some reason. It's fixed.
107 lines
4.1 KiB
C
107 lines
4.1 KiB
C
/*-*- 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/internal.h"
|
|
#include "libc/calls/ioctl.h"
|
|
#include "libc/calls/struct/metatermios.internal.h"
|
|
#include "libc/calls/syscall-sysv.internal.h"
|
|
#include "libc/calls/termios.h"
|
|
#include "libc/calls/termios.internal.h"
|
|
#include "libc/dce.h"
|
|
#include "libc/fmt/itoa.h"
|
|
#include "libc/intrin/asan.internal.h"
|
|
#include "libc/intrin/strace.internal.h"
|
|
#include "libc/intrin/weaken.h"
|
|
#include "libc/mem/alloca.h"
|
|
#include "libc/sysv/consts/termios.h"
|
|
#include "libc/sysv/errfuns.h"
|
|
|
|
#define TCSETS 0x5402
|
|
#define TCSETSW 0x5403
|
|
#define TCSETSF 0x5404
|
|
#define TIOCSETA 0x80487414
|
|
#define TIOCSETAW 0x80487415
|
|
#define TIOCSETAF 0x80487416
|
|
|
|
void __on_tcsetattr(int);
|
|
int tcsetattr_nt(int, int, const struct termios *);
|
|
|
|
static const char *DescribeTcsa(char buf[12], int opt) {
|
|
if (opt == TCSANOW) return "TCSANOW";
|
|
if (opt == TCSADRAIN) return "TCSADRAIN";
|
|
if (opt == TCSAFLUSH) return "TCSAFLUSH";
|
|
FormatInt32(buf, opt);
|
|
return buf;
|
|
}
|
|
|
|
static int tcsetattr_impl(int fd, int opt, const struct termios *tio) {
|
|
if (fd < 0) {
|
|
return einval();
|
|
}
|
|
|
|
if (fd < g_fds.n && g_fds.p[fd].kind == kFdZip) {
|
|
return enotty();
|
|
}
|
|
|
|
if (0 <= fd && fd <= 2 && _weaken(__on_tcsetattr)) {
|
|
static bool once;
|
|
if (!once) {
|
|
_weaken(__on_tcsetattr)(fd);
|
|
once = true;
|
|
}
|
|
}
|
|
|
|
if (IsAsan() && !__asan_is_valid(tio, sizeof(*tio))) {
|
|
return efault();
|
|
}
|
|
|
|
if (IsMetal()) {
|
|
return 0;
|
|
}
|
|
|
|
if (IsWindows()) {
|
|
return tcsetattr_nt(fd, opt, tio);
|
|
}
|
|
|
|
if (IsLinux() || IsBsd()) {
|
|
union metatermios mt;
|
|
return sys_ioctl(fd, (IsLinux() ? TCSETS : TIOCSETA) + opt,
|
|
__termios2host(&mt, tio));
|
|
}
|
|
|
|
return enosys();
|
|
}
|
|
|
|
/**
|
|
* Sets struct on teletypewriter w/ drains and flushes.
|
|
*
|
|
* @param fd open file descriptor that isatty()
|
|
* @param opt can be:
|
|
* - TCSANOW: sets console settings
|
|
* - TCSADRAIN: drains output, and sets console settings
|
|
* - TCSAFLUSH: drops input, drains output, and sets console settings
|
|
* @return 0 on success, -1 w/ errno
|
|
* @asyncsignalsafe
|
|
*/
|
|
int tcsetattr(int fd, int opt, const struct termios *tio) {
|
|
int rc;
|
|
rc = tcsetattr_impl(fd, opt, tio);
|
|
STRACE("tcsetattr(%d, %s, %p) → %d% m", fd, DescribeTcsa(alloca(12), opt),
|
|
tio, rc);
|
|
return rc;
|
|
}
|