summaryrefslogtreecommitdiff
path: root/server-tools/instance-manager/angel.cc
blob: a1892112dc830d187615c27dae5ed1918aab2895 (plain)
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/* Copyright (C) 2003-2006 MySQL AB

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; version 2 of the License.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

#ifndef __WIN__

#include "angel.h"

#include <sys/wait.h>
/*
  sys/wait.h is needed for waitpid(). Unfortunately, there is no MySQL
  include file, that can serve for this. Include it before MySQL system
  headers so that we can change system defines if needed.
*/

#include "my_global.h"
#include "my_alarm.h"
#include "my_dir.h"
#include "my_sys.h"

/* Include other IM files. */

#include "log.h"
#include "manager.h"
#include "options.h"
#include "priv.h"

/************************************************************************/

enum { CHILD_OK= 0, CHILD_NEED_RESPAWN, CHILD_EXIT_ANGEL };

static int log_fd;

static volatile sig_atomic_t child_status= CHILD_OK;
static volatile sig_atomic_t child_exit_code= 0;
static volatile sig_atomic_t shutdown_request_signo= 0;


/************************************************************************/
/**
  Open log file.

  @return
    TRUE  on error;
    FALSE on success.
*************************************************************************/

static bool open_log_file()
{
  log_info("Angel: opening log file '%s'...",
           (const char *) Options::Daemon::log_file_name);

  log_fd= open(Options::Daemon::log_file_name,
               O_WRONLY | O_CREAT | O_APPEND | O_NOCTTY,
               S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);

  if (log_fd < 0)
  {
    log_error("Can not open log file '%s': %s.",
              (const char *) Options::Daemon::log_file_name,
              (const char *) strerror(errno));

    return TRUE;
  }

  return FALSE;
}


/************************************************************************/
/**
  Detach the process from controlling tty.

  @return
    TRUE on error;
    FALSE on success.
*************************************************************************/

static bool detach_process()
{
  /*
    Become a session leader (the goal is not to have a controlling tty).

    setsid() must succeed because child is guaranteed not to be a process
    group leader (it belongs to the process group of the parent).

    NOTE: if we now don't have a controlling tty we will not receive
    tty-related signals - no need to ignore them.
  */

  if (setsid() < 0)
  {
    log_error("setsid() failed: %s.", (const char *) strerror(errno));
    return -1;
  }

  /* Close STDIN. */

  log_info("Angel: preparing standard streams.");

  if (close(STDIN_FILENO) < 0)
  {
    log_error("Warning: can not close stdin (%s)."
              "Trying to continue...",
              (const char *) strerror(errno));
  }

  /* Dup STDOUT and STDERR to the log file. */

  if (dup2(log_fd, STDOUT_FILENO) < 0 ||
      dup2(log_fd, STDERR_FILENO) < 0)
  {
    log_error("Can not redirect stdout and stderr to the log file: %s.",
              (const char *) strerror(errno));

    return TRUE;
  }

  if (log_fd != STDOUT_FILENO && log_fd != STDERR_FILENO)
  {
    if (close(log_fd) < 0)
    {
      log_error("Can not close original log file handler (%d): %s. "
                "Trying to continue...",
                (int) log_fd,
                (const char *) strerror(errno));
    }
  }

  return FALSE;
}


/************************************************************************/
/**
  Create PID file.

  @return
    TRUE  on error;
    FALSE on success.
*************************************************************************/

static bool create_pid_file()
{
  if (create_pid_file(Options::Daemon::angel_pid_file_name, getpid()))
  {
    log_error("Angel: can not create pid file (%s).",
              (const char *) Options::Daemon::angel_pid_file_name);

    return TRUE;
  }

  log_info("Angel: pid file (%s) created.",
           (const char *) Options::Daemon::angel_pid_file_name);

  return FALSE;
}


/************************************************************************/
/**
  SIGCHLD handler.

  Reap child, analyze child exit code, and set child_status
  appropriately.
*************************************************************************/

void reap_child(int __attribute__((unused)) signo)
{
  /* NOTE: As we have only one child, no need to cycle waitpid(). */

  int exit_code;

  if (waitpid(0, &exit_code, WNOHANG) > 0)
  {
    child_exit_code= exit_code;
    child_status= exit_code ? CHILD_NEED_RESPAWN : CHILD_EXIT_ANGEL;
  }
}


/************************************************************************/
/**
  SIGTERM, SIGHUP, SIGINT handler.

  Set termination status and return.
*************************************************************************/

void terminate(int signo)
{
  shutdown_request_signo= signo;
}


/************************************************************************/
/**
  Angel main loop.

  @return
    The function returns exit status for global main():
      0  -- program completed successfully;
      !0 -- error occurred.
*************************************************************************/

static int angel_main_loop()
{
  /*
    Install signal handlers.

    NOTE: Although signal handlers are needed only for parent process
    (IM-angel), we should install them before fork() in order to avoid race
    condition (i.e. to be sure, that IM-angel will receive SIGCHLD in any
    case).
  */

  sigset_t wait_for_signals_mask;

  struct sigaction sa_chld;
  struct sigaction sa_term;
  struct sigaction sa_chld_orig;
  struct sigaction sa_term_orig;
  struct sigaction sa_int_orig;
  struct sigaction sa_hup_orig;

  log_info("Angel: setting necessary signal actions...");

  sigemptyset(&wait_for_signals_mask);

  sigemptyset(&sa_chld.sa_mask);
  sa_chld.sa_handler= reap_child;
  sa_chld.sa_flags= SA_NOCLDSTOP;

  sigemptyset(&sa_term.sa_mask);
  sa_term.sa_handler= terminate;
  sa_term.sa_flags= 0;

  /* NOTE: sigaction() fails only if arguments are wrong. */

  sigaction(SIGCHLD, &sa_chld, &sa_chld_orig);
  sigaction(SIGTERM, &sa_term, &sa_term_orig);
  sigaction(SIGINT, &sa_term, &sa_int_orig);
  sigaction(SIGHUP, &sa_term, &sa_hup_orig);

  /* The main Angel loop. */

  while (true)
  {
    /* Spawn a new Manager. */

    log_info("Angel: forking Manager process...");

    switch (fork()) {
    case -1:
      log_error("Angel: can not fork IM-main: %s.",
                (const char *) strerror(errno));

      return -1;

    case 0:
      /*
        We are in child process, which will be IM-main:
          - Restore default signal actions to let the IM-main work with
            signals as he wishes;
          - Call Manager::main();
      */

      log_info("Angel: Manager process created successfully.");

      /* NOTE: sigaction() fails only if arguments are wrong. */

      sigaction(SIGCHLD, &sa_chld_orig, NULL);
      sigaction(SIGTERM, &sa_term_orig, NULL);
      sigaction(SIGINT, &sa_int_orig, NULL);
      sigaction(SIGHUP, &sa_hup_orig, NULL);

      log_info("Angel: executing Manager...");

      return Manager::main();
    }

    /* Wait for signals. */

    log_info("Angel: waiting for signals...");

    while (child_status == CHILD_OK && shutdown_request_signo == 0)
      sigsuspend(&wait_for_signals_mask);

    /* Exit if one of shutdown signals has been caught. */

    if (shutdown_request_signo)
    {
      log_info("Angel: received shutdown signal (%d). Exiting...",
               (int) shutdown_request_signo);

      return 0;
    }

    /* Manager process died. Respawn it if it was a failure. */

    if (child_status == CHILD_NEED_RESPAWN)
    {
      child_status= CHILD_OK;

      log_error("Angel: Manager exited abnormally (exit code: %d).",
                (int) child_exit_code);

      log_info("Angel: sleeping 1 second...");

      sleep(1); /* don't respawn too fast */

      log_info("Angel: respawning Manager...");

      continue;
    }

    /* Delete IM-angel PID file. */

    my_delete(Options::Daemon::angel_pid_file_name, MYF(0));

    /* IM-angel finished. */

    log_info("Angel: Manager exited normally. Exiting...");

    return 0;
  }
}


/************************************************************************/
/**
  Angel main function.

  @return
    The function returns exit status for global main():
      0  -- program completed successfully;
      !0 -- error occurred.
*************************************************************************/

int Angel::main()
{
  log_info("Angel: started.");

  /* Open log file. */

  if (open_log_file())
    return -1;

  /* Fork a new process. */

  log_info("Angel: daemonizing...");

  switch (fork()) {
  case -1:
    /*
      This is the main Instance Manager process, fork() failed.
      Log an error and bail out with error code.
    */

    log_error("fork() failed: %s.", (const char *) strerror(errno));
    return -1;

  case 0:
    /* We are in child process. Continue Angel::main() execution. */

    break;

  default:
    /*
      We are in the parent process. Return 0 so that parent exits
      successfully.
    */

    log_info("Angel: exiting from the original process...");

    return 0;
  }

  /* Detach child from controlling tty. */

  if (detach_process())
    return -1;

  /* Create PID file. */

  if (create_pid_file())
    return -1;

  /* Start Angel main loop. */

  return angel_main_loop();
}

#endif // __WIN__