astrand / xclip

Command line interface to the X11 clipboard
GNU General Public License v2.0
1.07k stars 75 forks source link

daemonize properly #10

Closed astrand closed 8 years ago

astrand commented 9 years ago

xclip forks to the background, but it does not daemonize properly (e.g. as explained at http://stackoverflow.com/questions/3095566/linux-daemonize). This bit me while working on https://github.com/kousu/hashapass: if I try to builds script on my script, they hang.

I tracked down the problem to xclip. hang.sh contains a test case showing the problem. When I run it, I see

[kousu@galleon tests]$ ./hang.sh

Before
hang.sh: 455
total 0
lrwx------ 1 kousu kousu 64 Apr 12 03:17 0 -> /dev/pts/1
lrwx------ 1 kousu kousu 64 Apr 12 03:17 1 -> /dev/pts/1
lrwx------ 1 kousu kousu 64 Apr 12 03:17 2 -> /dev/pts/1
lr-x------ 1 kousu kousu 64 Apr 12 03:17 255 -> /home/kousu/pro/hashapass/tests/hang.sh
lr-x------ 1 kousu kousu 64 Apr 12 03:17 3 -> pipe:[302803]
hang's nested subshell: 456
total 0
lrwx------ 1 kousu kousu 64 Apr 12 03:17 0 -> /dev/pts/1
l-wx------ 1 kousu kousu 64 Apr 12 03:17 1 -> pipe:[302803]
lrwx------ 1 kousu kousu 64 Apr 12 03:17 2 -> /dev/pts/1
xclip: 461
total 0
lr-x------ 1 kousu kousu 64 Apr 12 03:17 0 -> pipe:[306902]
l-wx------ 1 kousu kousu 64 Apr 12 03:17 1 -> pipe:[302803]
lrwx------ 1 kousu kousu 64 Apr 12 03:17 2 -> /dev/pts/1
lrwx------ 1 kousu kousu 64 Apr 12 03:17 3 -> socket:[302804]
^C

with it hanging there until ctrl-c. If you pkill xclip or select something else to copy, the script continues and finishes.

I've played around with this and the culprit is pipe:[302803]: notice that xclip has a write end of it open, because it inherited it from its (now dead) parent which inherited it from the subshell, and the root script has the read end open. Since $(...) blocks until it has all the content, it blocks until all write ends are closed, which never happens because xclip doesn't touch its fds once it forks (or maybe it does, but they aren't going anywhere useful).

I like xclip and use very much--it's in my aliases. I'm happy to be able to contribute to it, if you'll accept my patch.

After the patch, hang.sh no longer hangs:

[kousu@galleon tests]$ PATH=xclip-code/:$PATH ./hang.sh

Before
hang.sh: 523
total 0
lrwx------ 1 kousu kousu 64 Apr 12 03:19 0 -> /dev/pts/1
lrwx------ 1 kousu kousu 64 Apr 12 03:19 1 -> /dev/pts/1
lrwx------ 1 kousu kousu 64 Apr 12 03:19 2 -> /dev/pts/1
lr-x------ 1 kousu kousu 64 Apr 12 03:19 255 -> /home/kousu/pro/hashapass/tests/hang.sh
lr-x------ 1 kousu kousu 64 Apr 12 03:19 3 -> pipe:[299983]
hang's nested subshell: 526
total 0
lrwx------ 1 kousu kousu 64 Apr 12 03:19 0 -> /dev/pts/1
l-wx------ 1 kousu kousu 64 Apr 12 03:19 1 -> pipe:[299983]
lrwx------ 1 kousu kousu 64 Apr 12 03:19 2 -> /dev/pts/1
xclip: 536
total 0
lrwx------ 1 kousu kousu 64 Apr 12 03:19 0 -> /dev/null
lrwx------ 1 kousu kousu 64 Apr 12 03:19 1 -> /dev/null
lrwx------ 1 kousu kousu 64 Apr 12 03:19 2 -> /dev/null
lrwx------ 1 kousu kousu 64 Apr 12 03:19 3 -> socket:[304510]

afteR. Z=||
[kousu@galleon tests]$ 

(by the way, your INSTALL instructions are incomplete: they don't mention "autoconf" which is needed before ./configure exists)

Reported by: kousu

Original Ticket: xclip/patches/8

astrand commented 9 years ago

I see that the markdown parser is different here. Hopefully the message got across.

Original comment by: kousu

astrand commented 9 years ago

Original comment by: astrand

astrand commented 9 years ago

Closed in favour of http://sourceforge.net/p/xclip/patches/9/

Original comment by: astrand