1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2012 The Bitcoin developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
7 #include "bitcoinrpc.h"
8 #include <boost/algorithm/string/predicate.hpp>
10 void DetectShutdownThread(boost::thread_group* threadGroup)
12 bool shutdown = ShutdownRequested();
13 // Tell the main threads to shutdown.
17 shutdown = ShutdownRequested();
20 threadGroup->interrupt_all();
23 //////////////////////////////////////////////////////////////////////////////
27 bool AppInit(int argc, char* argv[])
29 boost::thread_group threadGroup;
30 boost::thread* detectShutdownThread = NULL;
38 // If Qt is used, parameters/bitcoin.conf are parsed in qt/bitcoin.cpp's main()
39 ParseParameters(argc, argv);
40 if (!boost::filesystem::is_directory(GetDataDir(false)))
42 fprintf(stderr, "Error: Specified directory does not exist\n");
45 ReadConfigFile(mapArgs, mapMultiArgs);
47 if (mapArgs.count("-?") || mapArgs.count("--help"))
49 // First part of help message is specific to bitcoind / RPC client
50 std::string strUsage = _("Bitcoin version") + " " + FormatFullVersion() + "\n\n" +
52 " bitcoind [options] " + "\n" +
53 " bitcoind [options] <command> [params] " + _("Send command to -server or bitcoind") + "\n" +
54 " bitcoind [options] help " + _("List commands") + "\n" +
55 " bitcoind [options] help <command> " + _("Get help for a command") + "\n";
57 strUsage += "\n" + HelpMessage();
59 fprintf(stdout, "%s", strUsage.c_str());
64 for (int i = 1; i < argc; i++)
65 if (!IsSwitchChar(argv[i][0]) && !boost::algorithm::istarts_with(argv[i], "bitcoin:"))
70 if (!SelectParamsFromCommandLine()) {
71 fprintf(stderr, "Error: invalid combination of -regtest and -testnet.\n");
74 int ret = CommandLineRPC(argc, argv);
78 fDaemon = GetBoolArg("-daemon", false);
85 fprintf(stderr, "Error: fork() returned %d errno %d\n", pid, errno);
88 if (pid > 0) // Parent process, pid is child process id
90 CreatePidFile(GetPidFile(), pid);
93 // Child process falls through to rest of initialization
97 fprintf(stderr, "Error: setsid() returned %d errno %d\n", sid, errno);
101 detectShutdownThread = new boost::thread(boost::bind(&DetectShutdownThread, &threadGroup));
102 fRet = AppInit2(threadGroup);
104 catch (std::exception& e) {
105 PrintExceptionContinue(&e, "AppInit()");
107 PrintExceptionContinue(NULL, "AppInit()");
110 if (detectShutdownThread)
111 detectShutdownThread->interrupt();
112 threadGroup.interrupt_all();
115 if (detectShutdownThread)
117 detectShutdownThread->join();
118 delete detectShutdownThread;
119 detectShutdownThread = NULL;
126 extern void noui_connect();
127 int main(int argc, char* argv[])
132 // Connect bitcoind signal handlers
135 fRet = AppInit(argc, argv);
140 return (fRet ? 0 : 1);