kbd: three layouts, and the arrow keys were sitting on top of UTF-8
`∞`, `→`, `≤` and `'` could not be typed. `ą` and `ę` could. That pairing is
the whole diagnosis: KBD_UP was 0xE2 and its neighbours filled 0xE0..0xE9,
which are exactly the UTF-8 lead bytes for three-byte characters. Every
character in U+0800..U+FFFF -- most punctuation, every arrow, every maths
symbol, the whole of CJK -- arrived as a value the console read as an arrow key
and helpfully turned into `ESC [ A`. Two-byte sequences lead with 0xC4 and
nothing claimed 0xC4, so Polish came through and the rest did not.
The chord codes already knew this rule and said so in a comment. The navigation
keys predate them and were never brought in line. Stated once, in kbd.h: a key
produces either a Unicode codepoint or a value above 0x10FFFF, and nothing in
between means anything. KEY_* -> KBD_*, which also settles a silent collision
-- include/input.h defines its own KEY_HOME, KEY_UP and KEY_END as evdev
numbers, and kbd.c includes both headers, so three macros were being quietly
redefined in the one file that used them.
Then the layouts, which is what the codepoint-sized keycodes were for.
kernel/dev/keymap.c: us, pl, de. A layout is four tables -- plain, Shift,
AltGr, Shift+AltGr -- of uint32 codepoints. uint32 and not uchar is the reason
the file exists: `ó` fits in a byte and `ą` does not, and half a Polish
alphabet is not a Polish layout.
Two things deliberately are *not* tables:
- **Ctrl is derived from the plain codepoint.** The table it replaces was
indexed by scancode, which quietly assumed US -- on QWERTZ the Y and Z keys
swap, so Ctrl+Z would have suspended a job only on some layouts.
- **Caps Lock is a case toggle on the result**, so AltGr+a with Caps Lock is
`Ą`. No second table could say that, because Caps Lock combines with AltGr
rather than replacing it. Latin Extended-A alternates upper/lower by
codepoint but not all the same way round -- Ł is U+0141 (odd) and Ą is
U+0104 (even) -- so Polish lands in both halves and the parity matters.
kbdgetc() encodes to UTF-8 and returns the bytes one call at a time.
consoleintr()'s contract is bytes: the serial line hands it UTF-8 already and
it could not tell an encoded codepoint from a byte of the same value.
The layouts live in the kernel. Linux's loadkeys compiles a text keymap and
uploads it a key at a time through KDSKBENT, which puts the data in userspace
-- and a machine whose /usr has not mounted then has no layout at all. Here the
kernel always has a working keyboard and `loadkeys` only names which one.
KB_GETLAYOUT/KB_SETLAYOUT/KB_LISTLAYOUT; /etc/vconsole.conf and an
os.schos.keymap job for the boot-time choice. No dead keys, so French and
Spanish are absent rather than wrong.
/etc/locale.conf and LANG=C.UTF-8 in profile and zshenv. Only the charset half
of that is real -- musl decodes UTF-8 whatever the locale says and ships no
locale data -- but an unset LANG reads as "ASCII only" to everything that asks,
which is its own way of producing `?`.
And two symlink bugs found while chasing
zsh: locking failed for /root/.zsh_history: operation not permitted
on every boot. symlink() is the classic atomic lock -- create the link, and if
it exists someone else holds it -- and zsh distinguishes EEXIST ("contended,
retry") from anything else ("this cannot work, give up"). sys_symlink returned
a bare -1, which is -EPERM, so every zsh on the machine took the second branch
on a filesystem that supports symlinks perfectly well. Same errno class as
commit 75ccf98.
The second one the first was hiding: **divesymlink() has been in fs.c since the
fork and nothing ever called it.** A symlink behaved as a small file containing
its own target, so `cat link` printed the target's *name*. open() and openat()
follow the final component now (O_NOFOLLOW gives ELOOP), and stat() and lstat()
stop being the same function, since which one follows is the only difference
between them. Mid-path symlinks are still not resolved -- that belongs in
namex(), which would re-enter lookup holding a directory lock.
Verified on the emulated keyboard, not just over serial: AltGr+a/e/s gives
ąęś, Shift+AltGr gives ĄĘŚ, Caps Lock+AltGr gives ĄĘŚ, de types zy for the
physical y/z keys and @€µ on AltGr. `∞ 'x' → ≤ • — …` typed over serial now
survives. Three fresh boots: 0 lock failures, 0 faults.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
parent
5618164754
commit
4e3e345b41
15 changed files with 994 additions and 208 deletions
|
|
@ -18,6 +18,7 @@
|
|||
#include "gfx.h" /* gfx_cols/gfx_rows — TIOCGWINSZ reports them */
|
||||
#include "vt.h" /* the terminals TIOC*/VT_* act on */
|
||||
#include "pciuser.h"
|
||||
#include "keymap.h" /* KB_*LAYOUT — the keyboard layout catalogue */
|
||||
#include "netuser.h"
|
||||
|
||||
// -----------------------------------------------------------------------
|
||||
|
|
@ -459,6 +460,35 @@ addr_t sys_pipe2(void)
|
|||
// Filesystem ops
|
||||
// -----------------------------------------------------------------------
|
||||
|
||||
// Resolve a symlink opened as the final path component.
|
||||
//
|
||||
// divesymlink() has been in kernel/fs/fs.c since the fork and nothing ever
|
||||
// called it, so a symlink behaved as a small file containing its own target:
|
||||
// `cat link` printed the target's *name* rather than its contents, and every
|
||||
// program that follows a symlink to find something followed it nowhere.
|
||||
//
|
||||
// Only the last component. A symlink in the middle of a path -- /a/link/b --
|
||||
// is still not resolved; that belongs in namex(), which would have to re-enter
|
||||
// path lookup while holding a directory lock, and getting that wrong is a
|
||||
// deadlock rather than a wrong answer. Final-component resolution is what
|
||||
// open(), exec() and stat() need, which is nearly everything that matters.
|
||||
//
|
||||
// Takes a locked inode and returns a locked inode, or 0 with everything
|
||||
// released -- the same contract divesymlink() already had.
|
||||
static struct inode *
|
||||
follow_final(struct inode *ip, int omode)
|
||||
{
|
||||
if (ip->type != T_SYMLINK)
|
||||
return ip;
|
||||
if (omode & O_NOFOLLOW) {
|
||||
// POSIX: opening a symlink itself is an error unless O_PATH, which
|
||||
// this kernel does not have. Saying so beats handing back the link.
|
||||
iunlockput(ip);
|
||||
return 0;
|
||||
}
|
||||
return divesymlink(ip);
|
||||
}
|
||||
|
||||
addr_t sys_open(void)
|
||||
{
|
||||
char *path;
|
||||
|
|
@ -486,6 +516,7 @@ addr_t sys_open(void)
|
|||
} else {
|
||||
if (!(ip = namei(path))) { end_op(); return -ENOENT; }
|
||||
ilock(ip);
|
||||
if (!(ip = follow_final(ip, omode))) { end_op(); return -ELOOP; }
|
||||
// Directories may only be opened for reading, but callers (e.g.
|
||||
// opendir()) legitimately OR in flags like O_DIRECTORY/O_CLOEXEC/
|
||||
// O_NONBLOCK alongside O_RDONLY — only the access-mode bits matter.
|
||||
|
|
@ -530,6 +561,7 @@ addr_t sys_openat(void)
|
|||
} else {
|
||||
if (!(ip = at_namei(dirfd, path))) { end_op(); return -ENOENT; }
|
||||
ilock(ip);
|
||||
if (!(ip = follow_final(ip, omode))) { end_op(); return -ELOOP; }
|
||||
// Directories may only be opened for reading, but callers (e.g.
|
||||
// opendir()) legitimately OR in flags like O_DIRECTORY/O_CLOEXEC/
|
||||
// O_NONBLOCK alongside O_RDONLY — only the access-mode bits matter.
|
||||
|
|
@ -572,16 +604,21 @@ addr_t sys_fstat(void)
|
|||
return 0;
|
||||
}
|
||||
|
||||
addr_t sys_stat(void)
|
||||
// stat/lstat differ in exactly one thing: whether a final symlink is followed.
|
||||
// They used to be the same function, which was invisible while nothing
|
||||
// followed symlinks anywhere and becomes wrong the moment something does.
|
||||
static addr_t
|
||||
do_stat(int dirfd, char *path, char *st, int follow)
|
||||
{
|
||||
char *path;
|
||||
char *st;
|
||||
struct inode *ip;
|
||||
int size = myproc()->syscall_map ? sizeof(struct linux_stat) : sizeof(struct stat);
|
||||
if (argstr(0, &path) < 0 || argptr(1, &st, size) < 0) return -1;
|
||||
|
||||
begin_op();
|
||||
if (!(ip = namei(path))) { end_op(); return -ENOENT; }
|
||||
if (!(ip = at_namei(dirfd, path))) { end_op(); return -ENOENT; }
|
||||
ilock(ip);
|
||||
if (follow && ip->type == T_SYMLINK && !(ip = divesymlink(ip))) {
|
||||
end_op();
|
||||
return -ELOOP;
|
||||
}
|
||||
if (myproc()->syscall_map) stati_linux(ip, (struct linux_stat*)st);
|
||||
else stati(ip, (struct stat*)st);
|
||||
iunlockput(ip);
|
||||
|
|
@ -589,7 +626,23 @@ addr_t sys_stat(void)
|
|||
return 0;
|
||||
}
|
||||
|
||||
addr_t sys_lstat(void) { return sys_stat(); }
|
||||
addr_t sys_stat(void)
|
||||
{
|
||||
char *path, *st;
|
||||
int size = myproc()->syscall_map ? sizeof(struct linux_stat) : sizeof(struct stat);
|
||||
if (argstr(0, &path) < 0) return -EFAULT;
|
||||
if (argptr(1, &st, size) < 0) return -EFAULT;
|
||||
return do_stat(AT_FDCWD, path, st, 1);
|
||||
}
|
||||
|
||||
addr_t sys_lstat(void)
|
||||
{
|
||||
char *path, *st;
|
||||
int size = myproc()->syscall_map ? sizeof(struct linux_stat) : sizeof(struct stat);
|
||||
if (argstr(0, &path) < 0) return -EFAULT;
|
||||
if (argptr(1, &st, size) < 0) return -EFAULT;
|
||||
return do_stat(AT_FDCWD, path, st, 0);
|
||||
}
|
||||
|
||||
addr_t sys_access(void)
|
||||
{
|
||||
|
|
@ -865,31 +918,71 @@ bad_rename:
|
|||
ilock(ip); ip->nlink--; iupdate(ip); iunlockput(ip); end_op(); return -err;
|
||||
}
|
||||
|
||||
// symlink(2).
|
||||
//
|
||||
// The errno matters more here than anywhere else in this file, because the one
|
||||
// widespread use of symlink() is as an atomic lock: create a link, and if it
|
||||
// already exists someone else holds the lock. zsh's history locking is exactly
|
||||
// that, and it distinguishes EEXIST ("contended, wait and retry") from
|
||||
// anything else ("this cannot work, give up"). Returning a bare -1 -- which is
|
||||
// -EPERM -- put every zsh on the machine in the second bucket, and printed
|
||||
//
|
||||
// zsh: locking failed for /root/.zsh_history: operation not permitted
|
||||
//
|
||||
// on every boot, on a filesystem that supports symlinks perfectly well.
|
||||
static addr_t
|
||||
do_symlink(int newdirfd, char *target, char *linkpath)
|
||||
{
|
||||
struct inode *ip;
|
||||
int len;
|
||||
|
||||
if (*target == 0)
|
||||
return -ENOENT; // an empty target resolves to nothing
|
||||
|
||||
begin_op();
|
||||
// Distinguishing the two ways createat() returns 0 -- the name is taken,
|
||||
// or the path to it does not exist -- has to be done before the create,
|
||||
// because createat() does not say which.
|
||||
if ((ip = at_namei(newdirfd, linkpath)) != 0) {
|
||||
iput(ip);
|
||||
end_op();
|
||||
return -EEXIST;
|
||||
}
|
||||
if (!(ip = createat(newdirfd, linkpath, T_SYMLINK, 0, 0))) {
|
||||
end_op();
|
||||
return -ENOENT;
|
||||
}
|
||||
len = strlen(target);
|
||||
if (writei(ip, 0, (uint64)target, 0, len) != len) {
|
||||
iunlockput(ip);
|
||||
end_op();
|
||||
return -EIO;
|
||||
}
|
||||
iunlockput(ip);
|
||||
end_op();
|
||||
return 0;
|
||||
}
|
||||
|
||||
addr_t sys_symlink(void)
|
||||
{
|
||||
char *target, *linkpath;
|
||||
if (argstr(0, &target) < 0 || argstr(1, &linkpath) < 0) return -1;
|
||||
begin_op();
|
||||
struct inode *ip = create(linkpath, T_SYMLINK, 0, 0);
|
||||
if (!ip) { end_op(); return -1; }
|
||||
int len = strlen(target);
|
||||
if (writei(ip, 0, (uint64)target, 0, len) != len) { iunlockput(ip); end_op(); return -1; }
|
||||
iunlockput(ip); end_op();
|
||||
return 0;
|
||||
if (argstr(0, &target) < 0 || argstr(1, &linkpath) < 0) return -EFAULT;
|
||||
return do_symlink(AT_FDCWD, target, linkpath);
|
||||
}
|
||||
|
||||
addr_t sys_readlink(void)
|
||||
{
|
||||
char *path, *buf;
|
||||
int bufsiz;
|
||||
if (argstr(0, &path) < 0 || argint(2, &bufsiz) < 0 || bufsiz <= 0) return -1;
|
||||
if (argptr(1, &buf, bufsiz) < 0) return -1;
|
||||
if (argstr(0, &path) < 0) return -EFAULT;
|
||||
if (argint(2, &bufsiz) < 0 || bufsiz <= 0) return -EINVAL;
|
||||
if (argptr(1, &buf, bufsiz) < 0) return -EFAULT;
|
||||
struct inode *ip;
|
||||
if (!(ip = namei(path))) return -ENOENT;
|
||||
ilock(ip);
|
||||
if (ip->type != T_SYMLINK) { iunlockput(ip); return -EINVAL; }
|
||||
int n = (int)ip->size < bufsiz ? (int)ip->size : bufsiz;
|
||||
if (readi(ip, 0, (uint64)buf, 0, n) != n) { iunlockput(ip); return -1; }
|
||||
if (readi(ip, 0, (uint64)buf, 0, n) != n) { iunlockput(ip); return -EIO; }
|
||||
iunlockput(ip);
|
||||
return n;
|
||||
}
|
||||
|
|
@ -1255,6 +1348,32 @@ addr_t sys_ioctl(void)
|
|||
return 0;
|
||||
}
|
||||
|
||||
// Which keyboard layout the console uses. One layout for the machine, not
|
||||
// one per terminal: there is one keyboard, and a user who switches to
|
||||
// tty3 has not changed which keys their fingers are on.
|
||||
if (request == KB_SETLAYOUT) {
|
||||
struct kb_layout *k = (struct kb_layout*)user_ptr(argp, sizeof(*k));
|
||||
if (!k) return -EFAULT;
|
||||
k->name[sizeof(k->name) - 1] = 0;
|
||||
if (keymap_set(k->name) < 0)
|
||||
return -EINVAL;
|
||||
return 0;
|
||||
}
|
||||
if (request == KB_GETLAYOUT || request == KB_LISTLAYOUT) {
|
||||
struct kb_layout *k = (struct kb_layout*)user_ptr(argp, sizeof(*k));
|
||||
if (!k) return -EFAULT;
|
||||
const struct keylayout *l;
|
||||
if (request == KB_GETLAYOUT) {
|
||||
l = keymap_active();
|
||||
} else {
|
||||
l = keymap_by_index(k->index);
|
||||
if (!l) return -EINVAL; // the documented end of the list
|
||||
}
|
||||
safestrcpy(k->name, l->name, sizeof(k->name));
|
||||
safestrcpy(k->desc, l->desc, sizeof(k->desc));
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (request == VT_GETMODE) {
|
||||
struct vt_mode *m = (struct vt_mode*)user_ptr(argp, sizeof(*m));
|
||||
if (!m) return -EFAULT;
|
||||
|
|
@ -1596,18 +1715,10 @@ addr_t sys_symlinkat(void)
|
|||
{
|
||||
char *target, *linkpath;
|
||||
int newdirfd;
|
||||
if (argstr(0, &target) < 0) return -1;
|
||||
if (argstr(0, &target) < 0) return -EFAULT;
|
||||
argint(1, &newdirfd);
|
||||
if (argstr(2, &linkpath) < 0) return -1;
|
||||
begin_op();
|
||||
struct inode *ip = createat(newdirfd, linkpath, T_SYMLINK, 0, 0);
|
||||
if (!ip) { end_op(); return -1; }
|
||||
int len = strlen(target);
|
||||
if (writei(ip, 0, (uint64)target, 0, len) != len) {
|
||||
iunlockput(ip); end_op(); return -1;
|
||||
}
|
||||
iunlockput(ip); end_op();
|
||||
return 0;
|
||||
if (argstr(2, &linkpath) < 0) return -EFAULT;
|
||||
return do_symlink(newdirfd, target, linkpath);
|
||||
}
|
||||
|
||||
addr_t sys_readlinkat(void)
|
||||
|
|
@ -1616,15 +1727,15 @@ addr_t sys_readlinkat(void)
|
|||
char *path, *buf;
|
||||
int bufsiz;
|
||||
argint(0, &dirfd);
|
||||
if (argstr(1, &path) < 0) return -1;
|
||||
if (argint(3, &bufsiz) < 0 || bufsiz <= 0) return -1;
|
||||
if (argptr(2, &buf, bufsiz) < 0) return -1;
|
||||
if (argstr(1, &path) < 0) return -EFAULT;
|
||||
if (argint(3, &bufsiz) < 0 || bufsiz <= 0) return -EINVAL;
|
||||
if (argptr(2, &buf, bufsiz) < 0) return -EFAULT;
|
||||
struct inode *ip;
|
||||
if (!(ip = at_namei(dirfd, path))) return -ENOENT;
|
||||
ilock(ip);
|
||||
if (ip->type != T_SYMLINK) { iunlockput(ip); return -EINVAL; }
|
||||
int n = (int)ip->size < bufsiz ? (int)ip->size : bufsiz;
|
||||
if (readi(ip, 0, (uint64)buf, 0, n) != n) { iunlockput(ip); return -1; }
|
||||
if (readi(ip, 0, (uint64)buf, 0, n) != n) { iunlockput(ip); return -EIO; }
|
||||
iunlockput(ip);
|
||||
return n;
|
||||
}
|
||||
|
|
@ -1670,15 +1781,7 @@ addr_t sys_fstatat(void)
|
|||
return 0;
|
||||
}
|
||||
|
||||
begin_op();
|
||||
struct inode *ip = at_namei(dirfd, path);
|
||||
if (!ip) { end_op(); return -ENOENT; }
|
||||
ilock(ip);
|
||||
if (myproc()->syscall_map) stati_linux(ip, (struct linux_stat*)st);
|
||||
else stati(ip, (struct stat*)st);
|
||||
iunlockput(ip);
|
||||
end_op();
|
||||
return 0;
|
||||
return do_stat(dirfd, path, st, !(flags & AT_SYMLINK_NOFOLLOW));
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------
|
||||
|
|
|
|||
Loading…
Reference in a new issue