aboutsummaryrefslogtreecommitdiff
path: root/tests/process-term/driver.cxx
blob: 90afc96d5754f1215bc60026e7253177ace3d46d (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
405
406
407
408
409
410
411
412
413
414
415
416
417
// file      : tests/process-term/driver.cxx -*- C++ -*-
// license   : MIT; see accompanying LICENSE file

#ifndef _WIN32
#  include <time.h>
#  include <signal.h>
#  include <unistd.h>
#  include <sys/types.h>
#else
#  include <libbutl/win32-utility.hxx>
#endif

#ifndef __cpp_lib_modules_ts
#include <string>
#include <cerrno>   // ERANGE
#include <utility>  // move()
#include <cstdlib>  // atexit(), exit(), strtoull()
#include <cstring>  // memset()
#include <cstdint>  // uint64_t
#include <iostream>
#ifndef _WIN32
#  include <chrono>
#endif
#endif

// Other includes.

#ifdef __cpp_modules_ts
#ifdef __cpp_lib_modules_ts
import std.core;
import std.io;
#endif
import butl.process;
import butl.optional;
import butl.fdstream;
#else
#include <libbutl/process.mxx>
#include <libbutl/optional.mxx>
#include <libbutl/fdstream.mxx>
#endif

#undef NDEBUG
#include <cassert>

using namespace std;
using namespace butl;

void
atexit_func ()
{
  cout << "exiting";
}

#ifndef _WIN32

volatile sig_atomic_t term_sig = 0;

static void
term (int sig)
{
  term_sig = sig;
}
#endif

// Usages:
//
// argv[0]
// argv[0] -s <sec> [-t (ignore|exit|default)] [-e] [-c <num>]
//
// In the first form run some basic process termination tests, running its
// child in the second form.
//
// In the second form optionally register the SIGTERM signal handler
// (POSIX-only) and the atexit function, then sleep for the requested number
// of seconds and exit with the specified status.
//
// -s <sec>
//    Sleep for the specified timeout.
//
// -t (ignore|exit|default)
//    Register the SIGTERM signal handler. If the signal is received than
//    either ignore it, interrupt the sleep and exit, or call the default
//    handler.
//
// -e
//    Register the function with atexit() that prints the 'exiting' string to
//    stdout.
//
// -c <num>
//    Exit with the specified status (zero by default).
//
int
main (int argc, const char* argv[])
{
  using butl::optional;

  auto num = [] (const string& s)
  {
    assert (!s.empty ());

    char* e (nullptr);
    errno = 0; // We must clear it according to POSIX.
    uint64_t r (strtoull (s.c_str (), &e, 10)); // Can't throw.
    assert (errno != ERANGE && e == s.c_str () + s.size ());

    return r;
  };

  int ec (0);
  optional<uint64_t> sec;

#ifndef _WIN32
  enum class sig_action
  {
    ignore,
    exit,
    default_
  };

  optional<sig_action> term_action;

  struct sigaction def_handler;
#endif

  for (int i (1); i != argc; ++i)
  {
    string o (argv[i]);

    if (o == "-s")
    {
      assert (++i != argc);
      sec = num (argv[i]);
    }
    else if (o == "-c")
    {
      assert (++i != argc);
      ec = static_cast<int> (num (argv[i]));
    }
    else if (o == "-e")
    {
      assert (atexit (atexit_func) == 0);
    }
    else if (o == "-t")
    {
      assert (++i != argc);

#ifndef _WIN32
      string v (argv[i]);

      if (v == "ignore")
        term_action = sig_action::ignore;
      else if (v == "exit")
        term_action = sig_action::exit;
      else if (v == "default")
        term_action = sig_action::default_;
      else
        assert (false);

      struct sigaction action;
      memset (&action, 0, sizeof (action));
      action.sa_handler = term;
      assert (sigaction (SIGTERM, &action, &def_handler) == 0);
#endif
    }
    else
      assert (false);
  }

#ifndef _WIN32
  auto sleep = [&term_action, &def_handler] (uint64_t sec)
  {
    // Wait until timeout expires or SIGTERM is received and is not ignored.
    //
    for (timespec tm {static_cast<time_t> (sec), 0};
         nanosleep (&tm, &tm) == -1; )
    {
      assert (term_action && errno == EINTR && term_sig == SIGTERM);

      if (*term_action == sig_action::ignore)
        continue;

      if (*term_action == sig_action::default_)
      {
        assert (sigaction (term_sig, &def_handler, nullptr) == 0);
        kill (getpid (), term_sig);
      }

      break;
    }
  };
#else
  auto sleep = [] (uint64_t sec)
  {
    Sleep (static_cast<DWORD> (sec) * 1000);
  };
#endif

  // Child process.
  //
  if (sec)
  {
    if (*sec != 0)
      sleep (*sec);

    return ec;
  }

  // Main process.
  //

  // Return true if the child process has written the specified string to
  // stdout, represented by the reading end of the specified pipe.
  //
  auto test_out = [] (fdpipe&& pipe, const char* out)
  {
    pipe.out.close ();

    ifdstream is (move (pipe.in));
    bool r (is.read_text () == out);
    is.close ();
    return r;
  };

#ifndef _WIN32
  // Terminate a process with the default SIGTERM handler.
  //
  {
    fdpipe pipe (fdopen_pipe ());
    process p (process_start (0, pipe, 2, argv[0], "-s", 60, "-e"));

    sleep (3); // Give the child some time to initialize.
    p.term ();

    assert (test_out (move (pipe), ""));

    assert (!p.wait ());
    assert (p.exit);
    assert (!p.exit->normal ());
    assert (p.exit->signal () == SIGTERM);
  }

  // Terminate a process that exits on SIGTERM. Make sure it exits normally
  // and atexit function is called.
  //
  {
    fdpipe pipe (fdopen_pipe ());
    process p (process_start (0, pipe, 2,
                              argv[0], "-s", 60, "-t", "exit", "-e", "-c", 5));

    sleep (3); // Give the child some time to initialize.
    p.term ();

    assert (test_out (move (pipe), "exiting"));

    assert (!p.wait ());
    assert (p.exit);
    assert (p.exit->normal ());
    assert (p.exit->code () == 5);
  }

  // Terminate a process that calls the default handler on SIGTERM.
  //
  {
    fdpipe pipe (fdopen_pipe ());
    process p (
      process_start (0, pipe, 2,
                     argv[0], "-s", 60, "-t", "default", "-e", "-c", 5));

    sleep (3); // Give the child some time to initialize.
    p.term ();

    assert (test_out (move (pipe), ""));

    assert (!p.wait ());
    assert (p.exit);
    assert (!p.exit->normal ());
    assert (p.exit->signal () == SIGTERM);
  }

  // Terminate and then kill still running process.
  //
  {
    fdpipe pipe (fdopen_pipe ());
    process p (process_start (0, pipe, 2,
                              argv[0], "-s", 60, "-t", "ignore", "-e"));

    sleep (3); // Give the child some time to initialize.
    p.term ();

    assert (!p.timed_wait (chrono::seconds (1)));

    p.kill ();

    assert (test_out (move (pipe), ""));

    assert (!p.wait ());
    assert (p.exit);
    assert (!p.exit->normal ());
    assert (p.exit->signal () == SIGKILL);
  }

  // Terminate an already terminated process.
  //
  {
    fdpipe pipe (fdopen_pipe ());
    process p (process_start (0, pipe, 2, argv[0], "-s", 0, "-c", 5));

    sleep (4); // Give the child some time to terminate.
    p.term ();

    assert (test_out (move (pipe), ""));

    assert (!p.wait ());
    assert (p.exit);
    assert (p.exit->normal ());
    assert (p.exit->code () == 5);
  }

  // Terminate a process being terminated.
  //
  {
    fdpipe pipe (fdopen_pipe ());
    process p (process_start (0, pipe, 2, argv[0], "-s", 60));

    p.term ();
    p.term ();

    assert (test_out (move (pipe), ""));

    assert (!p.wait ());
    assert (p.exit);
    assert (!p.exit->normal ());
  }

  // Kill a process being terminated.
  //
  {
    fdpipe pipe (fdopen_pipe ());
    process p (process_start (0, pipe, 2, argv[0], "-s", 60));

    p.term ();
    p.kill ();

    assert (test_out (move (pipe), ""));

    assert (!p.wait ());
    assert (p.exit);
    assert (!p.exit->normal ());
    assert (p.exit->signal () == SIGTERM || p.exit->signal () == SIGKILL);
  }

  // Kill a process being killed.
  //
  {
    fdpipe pipe (fdopen_pipe ());
    process p (process_start (0, pipe, 2, argv[0], "-s", 60));

    p.kill ();
    p.kill ();

    assert (test_out (move (pipe), ""));

    assert (!p.wait ());
    assert (p.exit);
    assert (!p.exit->normal ());
  }
#endif

  // Terminate and wait a process.
  //
  {
    fdpipe pipe (fdopen_pipe ());
    process p (process_start (0, pipe, 2, argv[0], "-s", 60, "-e"));

    sleep (3); // Give the child some time to initialize.
    p.term ();

    assert (test_out (move (pipe), ""));

    assert (!p.wait ());
    assert (p.exit);
    assert (!p.exit->normal ());
  }

  // Kill and wait a process.
  //
  {
    fdpipe pipe (fdopen_pipe ());
    process p (process_start (0, pipe, 2, argv[0], "-s", 60, "-e"));

    sleep (3); // Give the child some time to initialize.
    p.kill ();

    assert (test_out (move (pipe), ""));

    assert (!p.wait ());
    assert (p.exit);
    assert (!p.exit->normal ());
  }

  // Kill a terminated process.
  //
  {
    fdpipe pipe (fdopen_pipe ());
    process p (process_start (0, pipe, 2, argv[0], "-s", 0, "-c", 5));

    sleep (4); // Give the child some time to terminate.
    p.kill ();

    assert (test_out (move (pipe), ""));

    assert (!p.wait ());
    assert (p.exit);
    assert (p.exit->normal ());
    assert (p.exit->code () == 5);
  }
}