Forking daemon #8278

pull ChoHag wants to merge 3 commits into bitcoin:master from ChoHag:forking-daemon changing 2 files +24 −3
  1. ChoHag commented at 9:35 am on June 28, 2016: contributor

    When started with cron, the sendmail process hung around waiting for its stdin, bitcoind’s stdout/err, to close. Fix that using daemon(3) where possible and doing its work where not.

    Also makes the forking error messages more consistent and, by using strerror, more explanatory.

  2. [bitcoind] Daemonise using daemon(3) if available 2855409302
  3. [bitcoind] Act more like daemon(3) if unavailable
    [bitcoind] Make forking error messages consistent
    136908e94b
  4. laanwj commented at 9:37 am on June 28, 2016: member
    Concept ACK
  5. laanwj added the label Utils and libraries on Jun 28, 2016
  6. sipa commented at 9:39 am on June 28, 2016: member
    Concept ACK
  7. in configure.ac: in 136908e94b outdated
    513@@ -514,6 +514,9 @@ AC_SEARCH_LIBS([inet_pton], [nsl resolv], [AC_DEFINE(HAVE_INET_PTON, 1, [Define
    514 
    515 AC_CHECK_DECLS([strnlen])
    516 
    517+# Check for daemon(3), unrelated to --with-daemon (although used by it)
    518+AC_CHECK_DECLS([daemon])
    


    laanwj commented at 11:38 am on June 28, 2016:
    Nice use of the build system
  8. in src/bitcoind.cpp: in 136908e94b outdated
    158@@ -150,9 +159,20 @@ bool AppInit(int argc, char* argv[])
    159 
    160             pid_t sid = setsid();
    161             if (sid < 0)
    162-                fprintf(stderr, "Error: setsid() returned %d errno %d\n", sid, errno);
    163+                fprintf(stderr, "Error: setsid() failed: %s\n", strerror(errno));
    164+
    165+            (void)chdir("/");
    


    laanwj commented at 11:41 am on June 28, 2016:
    This may be a change of behavior: we should check whether there are arguments that take relative paths (e.g. -datadir, -conf, but also the -*notify options) that are affected by this.

    ChoHag commented at 6:39 pm on June 28, 2016:

    Having looked up alternative ways of dealing with absolute/relative paths, I’ve come up with these options:

    • Remove chdir and tell daemon() not to either
    • Remember PWD before changing and prepend it to all relative paths (alternatively open a fd to PWD before changing and cd back or openat when necessary, but that won’t work with exec and is probably a terrible idea even if it did)
    • Abort if -daemon and any paths in the listed options are relative but include a -nochdir option to disable the chdir
    • Don’t chdir by default and require a -chdir or similar option to do so; should probably still abort if relative paths are provided

    There are advantages to performing the chdir to / when daemonising especially if/when bitcoind becomes suitable to use as a system service so if it can be done with minimal fuss I think it should be.


    laanwj commented at 7:06 am on June 29, 2016:
    I think the first option for this PR makes sense. That keeps this a refactor, not a change of behavior (which would break compatibility for users). In some later change the chdir could be re-added after implementing one of the other options.

    ChoHag commented at 8:36 am on June 30, 2016:

    I like the chdir, myself, and think it will look neater than ‘cd / && bitcoind’ so may I propose two new command-line switches:

    • -chdir; CHange DIRectory to / (or C:) prior to daemonising, can perhaps later be changed into a noop.
    • -pwdir; Preserve Working DIRectory*, currently a noop, can perhaps later be changed into preserving current functionality.

    I considered giving -chdir an optional option to specify which directory, with / or C:\ as the default, but that seems like overkill and makes option parsing Magic; a dirty word.

    I think this fits well with bitcoind’s triple life as a system daemon, user daemon or user interface.

    Edit: One more thing, I’m aware that in the grand scheme of things this addition is largely unimportant and have no intention of starting a bike-shedding.


    [*] Not what it really stands for.


    laanwj commented at 8:41 am on June 30, 2016:
    Sure, I’m not against changing the behavior in the future, we can do that later, one step at a time. However if you can do this PR as a simple change to daemonize that keeps the behavior the same, it’s much easier to get it merged, even for 0.13. If you start adding command-line flags this is too late for the feature freeze, so will only be considered after the 0.14 branch (and you will have to document the changes in the release notes).
  9. jonasschnelli commented at 8:18 pm on June 28, 2016: contributor
    Concept ACK
  10. [bitcoind] Preserve daemonise behaviour; no chdir d2cc39a017
  11. ChoHag commented at 10:49 am on June 30, 2016: contributor
    I’m not overly concerned with making the feature freeze but I realised it’s a bad code smell to have two mutually exclusive command line arguments, so have simply removed the chdir for the time being.
  12. laanwj commented at 7:58 am on July 1, 2016: member
    Thanks. Will test.
  13. laanwj commented at 8:17 am on July 1, 2016: member

    I get the following compile error when I force HAVE_DECL_DAEMON off:

    0/home/user/src/bitcoin/src/bitcoind.cpp:164:27: error: use of undeclared identifier '_PATH_DEVNULL'
    1            int fd = open(_PATH_DEVNULL, 02, 0);
    

    Where is that definition supposed to come from?

  14. ChoHag commented at 9:26 am on July 1, 2016: contributor

    It’s from /usr/include/paths.h but now that I see the initial _ (and, of course, your error) I wonder how portable it is. The file’s there on OpenBSD and Linux. I don’t have anything else to hand to look in but notably the file’s copyright is 1993 to Berkely so if not officially portable it’s probably practically portable.

    According to gnulib’s manual: Portability problems not fixed by Gnulib: This header file is missing on some platforms: Minix 3.1.8, HP-UX 11, Solaris 11 2010-11, mingw, MSVC 9, BeOS. The set of PATH* macros is platform dependent.

    I’ll push a change shortly to be more thorough.

  15. sipa commented at 12:15 pm on August 25, 2016: member
    Anything holding this up?
  16. laanwj commented at 11:11 am on August 26, 2016: member
    Yes, it’s not compiling for me when pretending not to have daemonize(). If we can’t test that fallback code, we shouldn’t even include it. @ChoHag was working on a change. (probably an autotools detection for _PATH_DEVNULL)
  17. laanwj commented at 11:20 am on August 26, 2016: member

    Note that we do have some things that potentially print to stdout/stderr after daemonize():

    • util.cpp: PrintExceptionContinue
    • noui.cpp: noui_ThreadSafeMessageBox (used for reporting some errors during http initialization)
    • sync.cpp: AssertLockHeldInternal

    Should not hold up this pull, as this is bad behavior in the first place, but we should make sure that everything printed also ends up in the log.

  18. gmaxwell commented at 11:05 pm on September 22, 2016: contributor
    @ChoHag Around? If not then maybe someone else wants to pick this up?
  19. ChoHag commented at 10:40 am on September 23, 2016: contributor

    I’m not really, I have pressing personal matters to deal with and cannot devote time to hacking so I’m happy to hand this over to someone else. I was researching the best way to make the change fully portable but really the whole process is quite simple.

    Matthew

    @ChoHag Around? If not then maybe someone else wants to pick this up?

    You are receiving this because you were mentioned. Reply to this email directly or view it on GitHub: #8278 (comment)

  20. laanwj commented at 9:12 am on September 26, 2016: member

    Are there any POSIX-ish OSes that are supported by our build that don’t support daemon()? I’ve checked at least Linux and OpenBSD and they do. OSX does too (thanks @jonasschnelli).

    If not, we could just disable -daemon if that call is not available (like on WIndows). That makes it a lot easier to do this as well as maintain the code afterward as the fallback path is not needed.

  21. laanwj referenced this in commit 26a470aca4 on Sep 26, 2016
  22. laanwj referenced this in commit bca798790c on Sep 26, 2016
  23. laanwj referenced this in commit 365c2d9fea on Sep 26, 2016
  24. laanwj commented at 9:39 am on September 26, 2016: member
    Closing in favor of #8813
  25. laanwj closed this on Sep 26, 2016

  26. laanwj referenced this in commit ccf834602f on Sep 26, 2016
  27. laanwj referenced this in commit a7ca7c8f26 on Sep 26, 2016
  28. laanwj referenced this in commit dbd66f1a36 on Sep 26, 2016
  29. laanwj referenced this in commit a92bf4af66 on Sep 26, 2016
  30. luke-jr referenced this in commit 377ab84465 on Dec 21, 2016
  31. DrahtBot locked this on Sep 8, 2021

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2024-11-25 06:12 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me