Pseudo-terminals, and what tmux and htop needed around them

- /dev/ptmx and /dev/pts/N: Unix98 pairs with a full line discipline,
  window sizes, job control and hangup (kernel/dev/pty.c).
- The console speaks scroll regions, insert/delete line, SU/SD, RI/IND/NEL,
  DECSC/DECRC; swallows charset designations and private CSI sequences;
  and LF only returns the carriage under ONLCR.
- setsid() drops the controlling terminal, so a pane's pty becomes its ctty.
- pipe2() honours O_NONBLOCK and O_CLOEXEC (libevent's signal pipe).
- File modes are stored and enforced: chmod, fchmod(at), umask, creation
  modes; /tmp is 1777.
- /proc/stat has real user/system/idle; /proc/<pid>/task; kthread flag.
- SO_PEERCRED, IOV_MAX 1024.
- /proc/strace: name a program and its syscalls go to the kernel log.
- World gains htop, btop, tmux, mc, ncdu, fastfetch, games, X toys, NetSurf.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01S1giNvrHTrPMPw7V6x1kQ4
This commit is contained in:
Nexoniarz 2026-10-25 02:11:10 +01:00
commit fd9d392416
28 changed files with 1577 additions and 47 deletions

View file

@ -61,6 +61,10 @@ int pciuser_mmio_ok(uint64 phys, uint64 len);
void pciuser_release_all(struct proc*);
int pciuser_owner_pid(int idx);
int proc_reincarnate(int status);
void proc_cputime(uint64 *user, uint64 *sys);
// kernel/dev/procfs.c: /proc/strace
int strace_wanted(const char *name);
int proc_write_strace(int user_src, uint64 src, uint n);
void proc_kill_init(void);
extern uint init_generation;
void cprintf(char*, ...);
@ -512,6 +516,7 @@ int socksendto(struct socket*, char *buf, int n,
int sockbind_in(struct socket*, uint addr, ushort port);
int sockconnect_in(struct socket*, uint addr, ushort port);
int sock_domain(struct socket*);
int sock_peercred(struct socket*, int*, uint*, uint*);
// Socket options. The syscall layer does the user-memory marshalling and these
// do the work, because struct socket's fields live in kernel/net/unix.c and the
// syscall layer deliberately cannot see them.

View file

@ -29,7 +29,7 @@
#define NOFILE 128 // open files per process
#define NFILE 512 // open files per system
#define NINODE 200 // maximum number of active i-nodes
#define NDEV 13 // maximum major device number
#define NDEV 16 // maximum major device number
#define RAMDISK_DEV 2 // memory-backed block device (kernel/dev/ramdisk.c)
#define USBDISK_DEV 3 // USB mass storage (kernel/drivers/usb_storage.c)

View file

@ -174,6 +174,7 @@ struct procinfo {
uint64 nvcsw, nivcsw; // voluntary / involuntary context switches
char name[16];
char exe[64]; // full path, for /proc/<pid>/exe
int kthread; // a kernel thread, not a program
};
struct proc {
@ -321,6 +322,14 @@ struct proc {
// chroot(2): where an absolute path starts, and where ".." stops. 0 is the
// system's root. Inherited across fork, held as a reference like cwd.
struct inode *root;
// Syscall tracing: every call and its result go to the log. Set by exec
// when the program's name is the one written to /proc/strace, and kept
// across fork so that what a traced program starts is traced too.
int strace;
// umask(2). Inherited across fork; 022 for the first process.
uint umask;
};
#define FD_CLOEXEC 1

41
include/dev/pty.h Normal file
View file

@ -0,0 +1,41 @@
#pragma once
// Pseudo-terminals, Unix98 style: open /dev/ptmx and get the master side of a
// new pair; the slave is /dev/pts/N. What sits between the two is the same
// line discipline a console has. See kernel/dev/pty.c.
#include "core/types.h"
#define PTMX_MAJOR 13 // /dev/ptmx: opening it allocates a pair
#define PTM_MAJOR 14 // a master, minor = pair number; never in a listing
#define PTS_MAJOR 15 // /dev/pts/N
#define NPTY 32
// A controlling terminal that is a pty, in proc->ctty: PTY_CTTY + N. The
// console's are 1..NVT.
#define PTY_CTTY 64
struct inode;
struct file;
int pty_alloc(void); // the new pair's number, or -1
int pty_used(int n);
int pty_ioctl(struct inode *ip, uint request, addr_t arg);
int pty_poll(struct inode *ip, int events, int readable, int writable);
struct inode *ptmx_open(struct inode *ip); // ip locked; returns the master, locked
void pty_init(void);
// Lines the devsw table needs.
int ptm_read(struct inode*, uint64, char*, int);
int ptm_write(struct inode*, uint64, char*, int);
int ptm_ready(struct inode*);
void ptm_open(struct inode*);
void ptm_close(struct inode*);
int pts_read(struct inode*, uint64, char*, int);
int pts_write(struct inode*, uint64, char*, int);
int pts_ready(struct inode*);
void pts_open(struct inode*);
void pts_close(struct inode*);
// From kernel/fs/devfs.c: the inode of master N.
struct inode *devfs_pty_master(int n);

View file

@ -82,7 +82,7 @@ struct vtcell {
// Escape parser states. E_OSC and E_OSC_ESC exist for hyperlinks: a terminal
// that does not follow OSC 8 must still consume it, or the URL is printed as
// text. See the comment in kernel/core/console.c that this came from.
enum { VE_NORMAL, VE_ESC, VE_CSI, VE_OSC, VE_OSC_ESC };
enum { VE_NORMAL, VE_ESC, VE_CSI, VE_OSC, VE_OSC_ESC, VE_CHARSET };
struct vt {
int index; // 0-based; /dev/tty<index+1>
@ -98,6 +98,10 @@ struct vt {
// CR, LF, backspace, an escape sequence -- clears it.
int wrap_pending;
int saved_cx, saved_cy; // DECSC/DECRC
// The scrolling region, DECSTBM: rows scroll_top..scroll_bot inclusive.
// scroll_bot 0 means unset, the whole screen -- which keeps a grid resized
// underneath it from holding a stale bottom.
int scroll_top, scroll_bot;
uint8 fg, bg; // current SGR colours
int bold;

View file

@ -130,6 +130,10 @@ struct inode {
uint64 size;
uint rwmode;
// POSIX permission bits from a filesystem that keeps them, or 0 for one
// that does not, in which case stat makes some up from the type. Bit 18 set
// alone means a mode of 0000, which chmod can ask for.
uint mode;
uint supermode;
uint showmode;
uint atime;

View file

@ -355,6 +355,12 @@ struct socket {
// the only side that can say when they have been delivered.
struct scm_fds scm[NSCM];
int nscm;
// Who is at this end, for SO_PEERCRED on the other: the process that made
// the socket, or for the server's end of a connection, the one that made
// the listener -- which is what Linux records too.
int cred_pid;
uint cred_uid, cred_gid;
};
#endif