Fix bugs in termios library and cleanup code

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.
This commit is contained in:
Justine Tunney 2023-06-14 17:02:57 -07:00
parent 06b749ae03
commit 4778cd4d27
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
187 changed files with 1025 additions and 1848 deletions

View file

@ -16,11 +16,77 @@
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.
*
@ -32,15 +98,10 @@
* @return 0 on success, -1 w/ errno
* @asyncsignalsafe
*/
int(tcsetattr)(int fd, int opt, const struct termios *tio) {
switch (opt) {
case TCSANOW:
return ioctl(fd, TCSETS, (void *)tio);
case TCSADRAIN:
return ioctl(fd, TCSETSW, (void *)tio);
case TCSAFLUSH:
return ioctl(fd, TCSETSF, (void *)tio);
default:
return einval();
}
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;
}