Page MenuHomeFreeBSD
Authored By
vova_fbsd.ru
Oct 13 2026, 10:00 PM
Size
3 KB
Referenced Files
None
Subscribers
None

ctty-last-close-linux.c

/* SPDX-License-Identifier: BSD-2-Clause */
/* Standalone Linux x86-64 ABI reproducer, no headers or libc required.
* Build on FreeBSD:
* clang --target=x86_64-linux-gnu -fuse-ld=lld -nostdlib -static -fno-stack-protector -fno-builtin -O2 -Wall -Wextra -Werror -Wl,-e,_start -o ctty-last-close-linux ctty-last-close-linux.c
* brandelf -t Linux ctty-last-close-linux
* Exit: 0 retained controlling tty; 1 regression; 2 setup/runtime error.
*/
#ifndef __x86_64__
#error This test requires x86-64
#endif
/* Linux x86-64 syscall numbers and ioctl constants, not FreeBSD values. */
enum { SYS_write=1, SYS_open=2, SYS_close=3, SYS_ioctl=16,
SYS_fork=57, SYS_wait4=61, SYS_setsid=112,
SYS_exit_group=231, O_RDWR_NOCTTY=0402,
TIOCSCTTY=0x540e,
TIOCGPTN=0x80045430U, TIOCSPTLCK=0x40045431U };
static long call(long n, long a, long b, long c, long d)
{
register long r10 __asm__("r10") = d;
long result;
__asm__ volatile("syscall" : "=a"(result)
: "a"(n), "D"(a), "S"(b), "d"(c), "r"(r10)
: "rcx", "r11", "memory");
return result;
}
static void print(const char *s)
{
long len=0;
while (s[len]) ++len;
while (len) {
long n=call(SYS_write, 1, (long)s, len, 0);
if (n == -4) continue;
if (n <= 0) return;
s+=n; len-=n;
}
}
static void number(unsigned long n)
{
char buf[32]; int i=31;
buf[i]=0;
do { buf[--i]='0'+n%10; n/=10; } while (n);
print(buf+i);
}
static __attribute__((noreturn)) void quit(int status)
{
call(SYS_exit_group, status, 0, 0, 0);
for (;;) {}
}
static long checked(const char *label, long result, int status)
{
if (result < 0) {
print(status == 1 ? "FAIL: " : "SETUP ERROR: ");
print(label); print(" errno="); number(-result); print("\n");
quit(status);
}
return result;
}
static int open_tty(const char *path, int status)
{
return checked(path, call(SYS_open, (long)path, O_RDWR_NOCTTY, 0, 0), status);
}
static void close_fd(int fd)
{
checked("close", call(SYS_close, fd, 0, 0, 0), 2);
}
void test_main(void)
{
int master=open_tty("/dev/ptmx", 2), ptn=-1, unlock=0;
checked("TIOCGPTN", call(SYS_ioctl, master, TIOCGPTN, (long)&ptn, 0), 2);
checked("TIOCSPTLCK", call(SYS_ioctl, master, TIOCSPTLCK, (long)&unlock, 0), 2);
if (ptn < 0) { print("Invalid PTY number\n"); quit(2); }
char path[32]="/dev/pts/", digits[16];
unsigned int value=(unsigned int)ptn;
int count=0, pos=9;
do { digits[count++]='0'+value%10; value/=10; } while (value);
while (count) path[pos++]=digits[--count];
path[pos]=0;
long pid=checked("fork", call(SYS_fork, 0, 0, 0, 0), 2);
if (pid == 0) {
close_fd(master); /* Parent retains master until child exits. */
checked("setsid", call(SYS_setsid, 0, 0, 0, 0), 2);
int slave=open_tty(path, 2);
checked("TIOCSCTTY", call(SYS_ioctl, slave, TIOCSCTTY, 0, 0), 2);
int tty=open_tty("/dev/tty", 2);
print("before last close: /dev/tty works\n");
close_fd(tty);
close_fd(slave); /* No other descriptors reference this slave. */
tty=open_tty("/dev/tty", 1);
print("PASS: /dev/tty still opens after last slave close\n");
close_fd(tty);
quit(0);
}
int status=0; long result;
do { result=call(SYS_wait4, pid, (long)&status, 0, 0); } while (result == -4);
checked("wait4", result, 2);
close_fd(master);
if (status & 127) {
print("Child terminated by signal "); number(status & 127); print("\n");
quit(2);
}
quit((status >> 8) & 255);
}
__asm__(".global _start\n_start:\n xor %ebp,%ebp\n andq $-16,%rsp\n call test_main\n ud2\n");

File Metadata

Mime Type
text/x-c
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
40821277
Default Alt Text
ctty-last-close-linux.c (3 KB)

Event Timeline