Optionally, kill subprocess when parent dies
[folly.git] / folly / test / SubprocessTestParentDeathHelper.cpp
1 /*
2  * Copyright 2013 Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 // This is a helper for the parentDeathSignal test in SubprocessTest.cpp.
18 //
19 // Basically, we create two processes, a parent and a child, and set the
20 // child to receive SIGUSR1 when the parent exits.  We set the child to
21 // create a file when that happens.  The child then kills the parent; the test
22 // will verify that the file actually gets created, which means that everything
23 // worked as intended.
24
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <fcntl.h>
28 #include <signal.h>
29 #include <unistd.h>
30
31 #include <gflags/gflags.h>
32 #include <glog/logging.h>
33
34 #include "folly/Conv.h"
35 #include "folly/Subprocess.h"
36
37 using folly::Subprocess;
38
39 DEFINE_bool(child, false, "");
40
41 namespace {
42 constexpr int kSignal = SIGUSR1;
43 volatile bool caught = false;
44
45 void signalHandler(int sig) {
46   if (sig != kSignal) {
47     abort();
48   }
49   caught = true;
50 }
51
52 }  // namespace
53
54 void runChild(const char* file) {
55   struct sigaction sa;
56   sa.sa_handler = signalHandler;
57   sigemptyset(&sa.sa_mask);
58   sa.sa_flags = 0;
59   CHECK_ERR(sigaction(kSignal, &sa, nullptr));
60
61   // Kill the parent, wait for our signal.
62   CHECK_ERR(kill(getppid(), SIGKILL));
63
64   while (!caught) {
65     pause();
66   }
67
68   // Signal completion by creating the file
69   CHECK_ERR(creat(file, 0600));
70 }
71
72 void runParent(const char* file) {
73   std::vector<std::string> args {"/proc/self/exe", "--child", file};
74   Subprocess proc(
75       args,
76       Subprocess::Options().parentDeathSignal(kSignal));
77   CHECK(proc.poll().running());
78
79   // The child will kill us.
80   for (;;) {
81     pause();
82   }
83 }
84
85 int main(int argc, char *argv[]) {
86   google::ParseCommandLineFlags(&argc, &argv, true);
87   CHECK_EQ(argc, 2);
88   if (FLAGS_child) {
89     runChild(argv[1]);
90   } else {
91     runParent(argv[1]);
92   }
93   return 0;
94 }
95