Menu

#9 daemonize properly [with patch]

Unstable_(example)
open
nobody
None
5
2015-04-12
2015-04-12
No

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)

2 Attachments

Discussion

  • Nick Guenther

    Nick Guenther - 2015-04-12

    I screwed up the other ticket. Sorry. I haven't used sf in years.

     
  • Peter Åstrand

    Peter Åstrand - 2015-04-12

    Thanks for your contribution. Wrt the patch, I'm a bit worried that the use of the daemon() function will limit portability. According to my man page of CentOS6:

    "Not in POSIX.1-2001. A similar function appears on the BSDs. The daemon() function first appeared in 4.4BSD"

    Perhaps a more minimal solution could simply close the problematic fd?