| 72 | namespace util |
| 73 | { |
| 74 | bool DaemonUnix::start() |
| 75 | { |
| 76 | if (isDaemon) |
| 77 | { |
| 78 | pid_t pid; |
| 79 | pid = fork(); |
| 80 | if (pid > 0) // parent |
| 81 | ::exit (EXIT_SUCCESS); |
| 82 | |
| 83 | if (pid < 0) // error |
| 84 | { |
| 85 | LogPrint(eLogError, "Daemon: Could not fork: ", strerror(errno)); |
| 86 | std::cerr << "i2pd: Could not fork: " << strerror(errno) << std::endl; |
| 87 | return false; |
| 88 | } |
| 89 | |
| 90 | // child |
| 91 | umask(S_IWGRP | S_IRWXO); // 0027 |
| 92 | int sid = setsid(); |
| 93 | if (sid < 0) |
| 94 | { |
| 95 | LogPrint(eLogError, "Daemon: Could not create process group."); |
| 96 | std::cerr << "i2pd: Could not create process group." << std::endl; |
| 97 | return false; |
| 98 | } |
| 99 | std::string d = i2p::fs::GetDataDir(); |
| 100 | if (chdir(d.c_str()) != 0) |
| 101 | { |
| 102 | LogPrint(eLogError, "Daemon: Could not chdir: ", strerror(errno)); |
| 103 | std::cerr << "i2pd: Could not chdir: " << strerror(errno) << std::endl; |
| 104 | return false; |
| 105 | } |
| 106 | |
| 107 | // point std{in,out,err} descriptors to /dev/null |
| 108 | freopen("/dev/null", "r", stdin); |
| 109 | freopen("/dev/null", "w", stdout); |
| 110 | freopen("/dev/null", "w", stderr); |
| 111 | } |
| 112 | |
| 113 | // set proc limits |
| 114 | struct rlimit limit; |
| 115 | uint16_t nfiles; i2p::config::GetOption("limits.openfiles", nfiles); |
| 116 | getrlimit(RLIMIT_NOFILE, &limit); |
| 117 | if (nfiles == 0) { |
| 118 | LogPrint(eLogInfo, "Daemon: Using system limit in ", limit.rlim_cur, " max open files"); |
| 119 | } else if (nfiles <= limit.rlim_max) { |
| 120 | limit.rlim_cur = nfiles; |
| 121 | if (setrlimit(RLIMIT_NOFILE, &limit) == 0) { |
| 122 | LogPrint(eLogInfo, "Daemon: Set max number of open files to ", |
| 123 | nfiles, " (system limit is ", limit.rlim_max, ")"); |
| 124 | } else { |
| 125 | LogPrint(eLogError, "Daemon: Can't set max number of open files: ", strerror(errno)); |
| 126 | } |
| 127 | } else { |
| 128 | LogPrint(eLogError, "Daemon: limits.openfiles exceeds system limit: ", limit.rlim_max); |
| 129 | } |
| 130 | uint32_t cfsize; i2p::config::GetOption("limits.coresize", cfsize); |
| 131 | if (cfsize) // core file size set |
nothing calls this directly
no test coverage detected