1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
|
#include <new>
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <new>
#include <stdexcept>
#include <memory>
extern "C"
{
#include <time.h>
#include <sys/wait.h>
#include <signal.h>
#include <sys/utsname.h>
}
#include <DrbdMon.h>
#include <MessageLog.h>
// LOG_CAPACITY must be >= 1
const size_t LOG_CAPACITY {10};
// Clear screen escape sequence
const char* ANSI_CLEAR = "\x1b[H\x1b[2J";
// Delay of 3 seconds for delayed restarts
static const time_t DELAY_SECS = 3;
static const long DELAY_NANOSECS = 0;
static void reset_delay(struct timespec& delay) noexcept;
static void clear_screen() noexcept;
static void cond_print_error_header(bool& error_header_printed, const std::string* const node_name) noexcept;
static bool adjust_ids(MessageLog* log, bool& ids_safe);
static void query_node_name(std::unique_ptr<std::string>& node_name);
int main(int argc, char* argv[])
{
int exit_code {0};
struct timespec delay;
struct timespec remaining;
reset_delay(delay);
reset_delay(remaining);
DrbdMon::fail_info fail_data {DrbdMon::fail_info::NONE};
DrbdMon::finish_action fin_action {DrbdMon::finish_action::RESTART_DELAYED};
std::unique_ptr<MessageLog> log;
std::unique_ptr<std::string> node_name;
bool ids_safe {false};
while (fin_action != DrbdMon::finish_action::TERMINATE &&
fin_action != DrbdMon::finish_action::TERMINATE_NO_CLEAR)
{
bool error_header_printed {false};
try
{
if (log == nullptr)
{
// std::out_of_range exception not handled, as it is
// only thrown if LOG_CAPACITY < 1
log = std::unique_ptr<MessageLog>(new MessageLog(LOG_CAPACITY));
}
if (node_name == nullptr)
{
query_node_name(node_name);
}
if (ids_safe || adjust_ids(log.get(), ids_safe))
{
const std::unique_ptr<DrbdMon> dm_instance(
new DrbdMon(argc, argv, *log, fail_data, node_name.get())
);
dm_instance->run();
fin_action = dm_instance->get_fin_action();
if (fin_action != DrbdMon::finish_action::TERMINATE_NO_CLEAR)
{
clear_screen();
}
}
else
{
fin_action = DrbdMon::finish_action::TERMINATE;
}
}
catch (std::bad_alloc& out_of_memory_exc)
{
clear_screen();
fin_action = DrbdMon::finish_action::RESTART_DELAYED;
fail_data = DrbdMon::fail_info::OUT_OF_MEMORY;
}
// Display any log messages
if (log != nullptr)
{
if (log->has_entries())
{
cond_print_error_header(error_header_printed, node_name.get());
std::fputs("** DrbdMon messages log\n\n", stdout);
log->display_messages(stderr);
fputc('\n', stdout);
}
}
if (fail_data == DrbdMon::fail_info::OUT_OF_MEMORY)
{
cond_print_error_header(error_header_printed, node_name.get());
std::fputs("** DrbdMon: Out of memory, trying to restart\n", stdout);
}
// Cleanup any zombie child processes
pid_t child_pid {0};
do
{
child_pid = waitpid(-1, nullptr, WNOHANG);
}
while (child_pid > 0);
if (fin_action == DrbdMon::finish_action::RESTART_DELAYED)
{
cond_print_error_header(error_header_printed, node_name.get());
std::fprintf(stdout, "** DrbdMon: Reinitializing in %u seconds\n",
static_cast<unsigned int> (delay.tv_sec));
// Attempt to unblock signals before waiting, so one can
// easily exit the program immediately by hitting Ctrl-C,
// closing the terminal or sending a TERM signal
sigset_t signal_mask;
if (sigemptyset(&signal_mask) == 0)
{
// These calls are unchecked; if adding a signal fails,
// it cannot be fixed anyway
sigaddset(&signal_mask, SIGTERM);
sigaddset(&signal_mask, SIGINT);
sigaddset(&signal_mask, SIGHUP);
sigprocmask(SIG_UNBLOCK, &signal_mask, nullptr);
}
// Suspend to delay the restart
while (nanosleep(&delay, &remaining) != 0)
{
delay = remaining;
}
reset_delay(delay);
}
else
if (fin_action == DrbdMon::finish_action::RESTART_IMMED)
{
cond_print_error_header(error_header_printed, node_name.get());
std::fputs("** DrbdMon: Reinitializing immediately\n", stdout);
}
}
return exit_code;
}
static void reset_delay(struct timespec& delay) noexcept
{
delay.tv_sec = DELAY_SECS;
delay.tv_nsec = DELAY_NANOSECS;
}
static void clear_screen() noexcept
{
std::fputs(ANSI_CLEAR, stdout);
std::fflush(stdout);
}
static void cond_print_error_header(bool& error_header_printed, const std::string* const node_name) noexcept
{
if (!error_header_printed)
{
std::fprintf(stdout, "** DrbdMon v%s\n", DrbdMon::VERSION.c_str());
if (node_name != nullptr)
{
std::fprintf(stdout, " Node %s\n", node_name->c_str());
}
error_header_printed = true;
}
}
// @throws std::bad_alloc
static bool adjust_ids(MessageLog* log, bool& ids_safe)
{
try
{
// Get the user ids of the current process
uid_t real_user {0};
uid_t eff_user {0};
uid_t saved_user {0};
if (getresuid(&real_user, &eff_user, &saved_user) != 0)
{
throw SysException();
}
// Get the group ids of the current process
gid_t real_group {0};
gid_t eff_group {0};
gid_t saved_group {0};
if (getresgid(&real_group, &eff_group, &saved_group) != 0)
{
throw SysException();
}
// Unless already equal, set effective user id = real user id, or
// if the real user id is root, then this program is probably setuid
// to some non-root account and being executed by root in an attempt
// to drop root privileges, therefore set the real user id to the
// effective user id in this case.
if (eff_user != real_user)
{
if (real_user == 0)
{
real_user = eff_user;
}
else
{
eff_user = real_user;
}
if (setresuid(real_user, eff_user, -1) != 0)
{
throw SysException();
}
}
// Set saved user id = real user id unless equal already
if (saved_user != real_user)
{
// Set saved user id = real user id
saved_user = real_user;
if (setresuid(-1, -1, saved_user) != 0)
{
throw SysException();
}
}
// Analogous to adjusting the user ids, adjust the group ids to
// non-root if real group id and effective group id do not match
if (eff_group != real_group)
{
if (real_group == 0)
{
real_group = eff_group;
}
else
{
eff_group = real_group;
}
if (setresgid(real_group, eff_group, -1) != 0)
{
throw SysException();
}
}
// Set saved group id = real group id unless equal already
if (saved_group != real_group)
{
// Set saved group id = real group id
saved_group = real_group;
if (setresgid(-1, -1, saved_group) != 0)
{
throw SysException();
}
}
// Cross-check adjusted set user & group ids
if (getresuid(&real_user, &eff_user, &saved_user) != 0 ||
getresgid(&real_group, &eff_group, &saved_group) != 0)
{
throw SysException();
}
if (real_user == eff_user && real_user == saved_user &&
real_group == eff_group && real_group == saved_group)
{
ids_safe = true;
}
else
{
throw SysException();
}
}
catch (SysException&)
{
log->add_entry(
MessageLog::log_level::ALERT,
"Adjusting the process' user/group ids failed"
);
}
return ids_safe;
}
// @throws std::bad_alloc
static void query_node_name(std::unique_ptr<std::string>& node_name)
{
std::unique_ptr<struct utsname> uname_buffer;
uname_buffer = std::unique_ptr<struct utsname>(new struct utsname);
if (uname(uname_buffer.get()) == 0)
{
node_name = std::unique_ptr<std::string>(new std::string(uname_buffer->nodename));
}
}
|