[fpc-pascal] The keyboard unit

Kevin Lyda kevin at ie.suberic.net
Sat May 11 10:46:33 CEST 2019


Ralf, thanks! That was the idea, a small easy to test program.

[NOTE: this email was a log of me debugging. If you want to skip to
the issue: it's because the keyboard unit dies when running under tmux
because /proc/PID/stat is harder to parse than the keyboard unit code
assumes.]

My setup is similar but slightly different: Ubuntu 18.04 and just
using a Makefile and the commandline compiler. Am I missing a flag?
When I run these:

strace -o argh ./test
strace -o argh ./test-without-keyboard

diff -u argh*
--- argh        2019-05-11 08:24:54.015836828 +0100
+++ argh2       2019-05-11 08:25:21.377836828 +0100
@@ -9,26 +9,6 @@
 ioctl(1, TCGETS, {B38400 opost isig icanon echo ...}) = 0
 ioctl(2, TCGETS, {B38400 opost isig icanon echo ...}) = 0
 readlink("/proc/self/exe", "/home/kevin/src/sandbox/older/fp"..., 255) = 45
-getpid()                                = 2158
-mmap(NULL, 32768, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS,
-1, 0) = 0x7bcbfb894000
-open("/proc/2158/stat", O_RDONLY|O_LARGEFILE) = 3
-read(3, "2158 (test) R 2156 2156 14867 34"..., 256) = 256
-close(3)                                = 0
-open("/proc/2156/stat", O_RDONLY|O_LARGEFILE) = 3
-read(3, "2156 (strace) S 14867 2156 14867"..., 256) = 256
-close(3)                                = 0
-mmap(NULL, 32768, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS,
-1, 0) = 0x7bcbfb88c000
-open("/proc/14867/stat", O_RDONLY|O_LARGEFILE) = 3
-read(3, "14867 (zsh) S 522 14867 14867 34"..., 256) = 256
-close(3)                                = 0
-open("/proc/522/stat", O_RDONLY|O_LARGEFILE) = 3
-read(3, "522 (tmux: server) S 131 522 522"..., 256) = 256
-write(2, "Runtime error 106 at $0000000000"..., 39) = 39
-write(2, "  $000000000042968D\n", 20)   = 20
-ioctl(2, TCGETS, {B38400 opost isig icanon echo ...}) = 0
-write(2, "  $000000000040018C\n", 20)   = 20
-write(2, "\n", 1)                       = 1
-munmap(0x7bcbfb88c000, 32768)           = 0
-munmap(0x7bcbfb894000, 32768)           = 0
-exit_group(106)                         = ?
-+++ exited with 106 +++
+write(1, "Hello World.\n", 13)          = 13
+exit_group(0)                           = ?
++++ exited with 0 +++

OK, so it seems to read through the ancestors of its process and dies
when it reads the /proc/PID/stat of the tmux process. So, what if I
run it without tmux: strace -o argh3 ./test which runs fine and exits
cleanly. So what's the difference? diff -u argh{,3}

--- argh        2019-05-11 08:24:54.015836828 +0100
+++ argh3       2019-05-11 08:40:16.456836828 +0100
@@ -9,26 +9,31 @@
 ioctl(1, TCGETS, {B38400 opost isig icanon echo ...}) = 0
 ioctl(2, TCGETS, {B38400 opost isig icanon echo ...}) = 0
 readlink("/proc/self/exe", "/home/kevin/src/sandbox/older/fp"..., 255) = 45
-getpid()                                = 2158
-mmap(NULL, 32768, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS,
-1, 0) = 0x7bcbfb894000
-open("/proc/2158/stat", O_RDONLY|O_LARGEFILE) = 3
-read(3, "2158 (test) R 2156 2156 14867 34"..., 256) = 256
-close(3)                                = 0
-open("/proc/2156/stat", O_RDONLY|O_LARGEFILE) = 3
-read(3, "2156 (strace) S 14867 2156 14867"..., 256) = 256
-close(3)                                = 0
-mmap(NULL, 32768, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS,
-1, 0) = 0x7bcbfb88c000
-open("/proc/14867/stat", O_RDONLY|O_LARGEFILE) = 3
-read(3, "14867 (zsh) S 522 14867 14867 34"..., 256) = 256
-close(3)                                = 0
-open("/proc/522/stat", O_RDONLY|O_LARGEFILE) = 3
-read(3, "522 (tmux: server) S 131 522 522"..., 256) = 256
-write(2, "Runtime error 106 at $0000000000"..., 39) = 39
-write(2, "  $000000000042968D\n", 20)   = 20
-ioctl(2, TCGETS, {B38400 opost isig icanon echo ...}) = 0
-write(2, "  $000000000040018C\n", 20)   = 20
-write(2, "\n", 1)                       = 1
-munmap(0x7bcbfb88c000, 32768)           = 0
-munmap(0x7bcbfb894000, 32768)           = 0
-exit_group(106)                         = ?
-+++ exited with 106 +++
+getpid()                                = 2719
+mmap(NULL, 32768, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS,
-1, 0) = 0x7f8092c23000
+open("/proc/2719/stat", O_RDONLY|O_LARGEFILE) = 3
+read(3, "2719 (test) R 2717 2717 2429 348"..., 256) = 256
+close(3)                                = 0
+open("/proc/2717/stat", O_RDONLY|O_LARGEFILE) = 3
+read(3, "2717 (strace) S 2429 2717 2429 3"..., 256) = 256
+close(3)                                = 0
+open("/proc/2429/stat", O_RDONLY|O_LARGEFILE) = 3
+read(3, "2429 (zsh) S 2427 2429 2429 3481"..., 256) = 256
+close(3)                                = 0
+open("/proc/2427/stat", O_RDONLY|O_LARGEFILE) = 3
+read(3, "2427 (ld-linux-x86-64) S 364 242"..., 256) = 256
+close(3)                                = 0
+open("/etc/timezone", O_RDONLY|O_LARGEFILE) = 3
+read(3, "Etc/UTC\n", 255)               = 8
+close(3)                                = 0
+open("/usr/share/zoneinfo/Etc/UTC", O_RDONLY|O_LARGEFILE) = 3
+read(3, "TZif2\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\1\0\0\0\1\0\0\0\0"...,
2048) = 127
+mmap(NULL, 32768, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS,
-1, 0) = 0x7f8092c1b000
+close(3)                                = 0
+time([1557560416])                      = 1557560416
+ioctl(1, TCGETS, {B38400 opost isig icanon echo ...}) = 0
+write(1, "Hello World.\n", 13)          = 13
+munmap(0x7f8092c1b000, 32768)           = 0
+munmap(0x7f8092c23000, 32768)           = 0
+exit_group(0)                           = ?
++++ exited with 0 +++

So the code that does this is in this:
/usr/share/fpcsrc/3.0.4/rtl/linux/linuxvcs.pp

I instrumented it a bit
(https://gist.github.com/b110dd08847fbd54e08e8bef803ac5d9) and I see
the issue:

/proc/5200/stat: 5200  (kbd-test) R 14867 5200 14867 34821
/proc/14867/stat: 14867  (zsh) S 522 14867 14867 34821
/proc/522/stat: 522  (tmux: server) Runtime error 106 at $00000000004006D8
  $00000000004006D8
  $0000000000400967
  $000000000040018C

The problem is that /proc/*/stat is kind of hard to parse as this code
proves - tmux has rewritten its argv[0] to have a space which breaks
the parser here.  And don't think the ")" is a delimiter as argv[0]
can have a ")" in it.  The format seems to imply it needs to be read
to the end of the line and parsed backwards:
https://www.redhat.com/archives/axp-list/2001-January/msg00355.html

In addition ttyname(3) is a POSIX function that would avoid this issue.

Kevin

On Sat, May 11, 2019 at 6:08 AM Ralf Quint <freedos.la at gmail.com> wrote:
>
> On 5/10/2019 4:42 PM, Kevin Lyda wrote:
> > If I remove the uses clause it runs fine.  Why is the keyboard unit,
> > which isn't even being used, causing this error?
> >
> > Kevin
>
> Well, when I got home a short while ago, I tried that small test program
> on one of my Linux workstations and it runs just fine...
>
> FPC 3.04 as installed with Lazarus 2.02 (ok, installed the 3 .deb
> packages for fpc, fpc-source and lazarus 2.02 from the lazarus
> SourceForge repo), compiled and running on Linux Mint 19.2/64, tried
> both Cinnamon and Mate, on two different hosts...
>
> Ralf
>
>
> ---
> This email has been checked for viruses by Avast antivirus software.
> https://www.avast.com/antivirus
>
> _______________________________________________
> fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org
> http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal



-- 
Kevin Lyda
Galway, Ireland



More information about the fpc-pascal mailing list