# # # patch "cmd_netsync.cc" # from [073c470b8682f791dc005e7b79eaa9e645a5aa92] # to [81921e8b5294c2b6a568b782e6de2432609c9368] # ============================================================ --- cmd_netsync.cc 073c470b8682f791dc005e7b79eaa9e645a5aa92 +++ cmd_netsync.cc 81921e8b5294c2b6a568b782e6de2432609c9368 @@ -449,8 +449,6 @@ struct pid_file require_path_is_nonexistent(path, F("pid file '%s' already exists") % path); file.open(path.as_external().c_str()); E(file.is_open(), F("failed to create pid file '%s'") % path); - file << get_process_id() << '\n'; - file.flush(); } ~pid_file() @@ -465,6 +463,12 @@ struct pid_file } } + void set_pid() + { + file << get_process_id() << '\n'; + file.flush(); + } + private: ofstream file; system_path path; @@ -480,6 +484,15 @@ CMD_NO_WORKSPACE(serve, "serve", "", CMD if (!args.empty()) throw usage(execid); + //this is only a requirement so that the testsuite can always depend + //on having a pid file when spawning mtn --daemon, since working with + //the pid of the original process wouldn't do much good. + //i think it makes some amount of sense even outside of this... + N(app.opts.daemon && !app.opts.pidfile.empty(), + F("When using --daemon, you must supply --pid-file also")); + + //this ensures that the specified pid file will work and opens it...doing + //this before daemonizing ensures better user warnings in case of failure. pid_file pid(app.opts.pidfile); if (app.opts.use_transport_auth) @@ -498,10 +511,18 @@ CMD_NO_WORKSPACE(serve, "serve", "", CMD app.db.ensure_open(); + //this must happen after the above require_password so that the user + //has a chance to do manual/interactive entry of the passphrase if they + //so desire. if (app.opts.daemon) E(daemon(0, 0) == 0, F("call to daemon failed!")); + //do this after the call to daemon (if the option is used) so that we + //record the pid of the backgrounded/detached process instead of the + //pre-fork process. + pid.set_pid(); + run_netsync_protocol(server_voice, source_and_sink_role, app.opts.bind_uris, globish("*"), globish(""), app); }