summaryrefslogtreecommitdiffstats
path: root/src/common/fork_function.h
blob: 3a4f2f29c08a274d13c9e92c54a6f5f58a2326f7 (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
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab

// Run a function in a forked child, with a timeout.

#pragma once

#include <functional>
#include <iostream>
#include <ostream>

#include <signal.h>
#ifndef _WIN32
#include <sys/wait.h>
#endif
#include <sys/types.h>

#include "include/ceph_assert.h"
#include "common/errno.h"

#ifndef _WIN32
static void _fork_function_dummy_sighandler(int sig) {}

// Run a function post-fork, with a timeout.  Function can return
// int8_t only due to unix exit code limitations.  Returns -ETIMEDOUT
// if timeout is reached.
static inline int fork_function(
  int timeout,
  std::ostream& errstr,
  std::function<int8_t(void)> f)
{
  // first fork the forker.
  pid_t forker_pid = fork();
  if (forker_pid) {
    // just wait
    int status;
    while (waitpid(forker_pid, &status, 0) == -1) {
      ceph_assert(errno == EINTR);
    }
    if (WIFSIGNALED(status)) {
      errstr << ": got signal: " << WTERMSIG(status) << "\n";
      return 128 + WTERMSIG(status);
    }
    if (WIFEXITED(status)) {
      int8_t r = WEXITSTATUS(status);
      errstr << ": exit status: " << (int)r << "\n";
      return r;
    }
    errstr << ": waitpid: unknown status returned\n";
    return -1;
  }

  // we are forker (first child)

  // close all fds
  int maxfd = sysconf(_SC_OPEN_MAX);
  if (maxfd == -1)
    maxfd = 16384;
  for (int fd = 0; fd <= maxfd; fd++) {
    if (fd == STDIN_FILENO)
      continue;
    if (fd == STDOUT_FILENO)
      continue;
    if (fd == STDERR_FILENO)
      continue;
    ::close(fd);
  }

  sigset_t mask, oldmask;
  int pid;

  // Restore default action for SIGTERM in case the parent process decided
  // to ignore it.
  if (signal(SIGTERM, SIG_DFL) == SIG_ERR) {
    std::cerr << ": signal failed: " << cpp_strerror(errno) << "\n";
    goto fail_exit;
  }
  // Because SIGCHLD is ignored by default, setup dummy handler for it,
  // so we can mask it.
  if (signal(SIGCHLD, _fork_function_dummy_sighandler) == SIG_ERR) {
    std::cerr << ": signal failed: " << cpp_strerror(errno) << "\n";
    goto fail_exit;
  }
  // Setup timeout handler.
  if (signal(SIGALRM, timeout_sighandler) == SIG_ERR) {
    std::cerr << ": signal failed: " << cpp_strerror(errno) << "\n";
    goto fail_exit;
  }
  // Block interesting signals.
  sigemptyset(&mask);
  sigaddset(&mask, SIGINT);
  sigaddset(&mask, SIGTERM);
  sigaddset(&mask, SIGCHLD);
  sigaddset(&mask, SIGALRM);
  if (sigprocmask(SIG_SETMASK, &mask, &oldmask) == -1) {
    std::cerr << ": sigprocmask failed: "
	      << cpp_strerror(errno) << "\n";
    goto fail_exit;
  }

  pid = fork();

  if (pid == -1) {
    std::cerr << ": fork failed: " << cpp_strerror(errno) << "\n";
    goto fail_exit;
  }

  if (pid == 0) { // we are second child
    // Restore old sigmask.
    if (sigprocmask(SIG_SETMASK, &oldmask, NULL) == -1) {
      std::cerr << ": sigprocmask failed: "
		<< cpp_strerror(errno) << "\n";
      goto fail_exit;
    }
    (void)setpgid(0, 0); // Become process group leader.
    int8_t r = f();
    _exit((uint8_t)r);
  }

  // Parent
  (void)alarm(timeout);

  for (;;) {
    int signo;
    if (sigwait(&mask, &signo) == -1) {
      std::cerr << ": sigwait failed: " << cpp_strerror(errno) << "\n";
      goto fail_exit;
    }
    switch (signo) {
    case SIGCHLD:
      int status;
      if (waitpid(pid, &status, WNOHANG) == -1) {
	std::cerr << ": waitpid failed: " << cpp_strerror(errno) << "\n";
	goto fail_exit;
      }
      if (WIFEXITED(status))
	_exit(WEXITSTATUS(status));
      if (WIFSIGNALED(status))
	_exit(128 + WTERMSIG(status));
      std::cerr << ": unknown status returned\n";
      goto fail_exit;
    case SIGINT:
    case SIGTERM:
      // Pass SIGINT and SIGTERM, which are usually used to terminate
      // a process, to the child.
      if (::kill(pid, signo) == -1) {
	std::cerr << ": kill failed: " << cpp_strerror(errno) << "\n";
	goto fail_exit;
      }
      continue;
    case SIGALRM:
      std::cerr << ": timed out (" << timeout << " sec)\n";
      if (::killpg(pid, SIGKILL) == -1) {
	std::cerr << ": kill failed: " << cpp_strerror(errno) << "\n";
	goto fail_exit;
      }
      _exit(-ETIMEDOUT);
    default:
      std::cerr << ": sigwait: invalid signal: " << signo << "\n";
      goto fail_exit;
    }
  }
  return 0;
fail_exit:
  _exit(EXIT_FAILURE);
}
#else
static inline int fork_function(
  int timeout,
  std::ostream& errstr,
  std::function<int8_t(void)> f)
{
  errstr << "Forking is not available on Windows.\n";
  return -1;
}
#endif