0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ServerLauncher.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007-2015 Hypertable, Inc.
3  *
4  * This file is part of Hypertable.
5  *
6  * Hypertable is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 3
9  * of the License, or any later version.
10  *
11  * Hypertable is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  * 02110-1301, USA.
20  */
21 
27 #ifndef Common_ServerLauncher_h
28 #define Common_ServerLauncher_h
29 
30 #include <chrono>
31 #include <iostream>
32 #include <thread>
33 
34 extern "C" {
35 #include <signal.h>
36 #include <sys/types.h>
37 #include <unistd.h>
38 #include <sys/types.h>
39 #include <sys/stat.h>
40 #include <fcntl.h>
41 }
42 
43 namespace Hypertable {
44 
55  public:
67  ServerLauncher(const char *path, char *const argv[],
68  const char *outfile = 0, bool append_output = false) {
69  int fd[2];
70  m_path = path;
71  if (pipe(fd) < 0) {
72  perror("pipe");
73  exit(EXIT_FAILURE);
74  }
75  if ((m_child_pid = fork()) == 0) {
76  if (outfile) {
77  int open_flags;
78  int outfd = -1;
79 
80  if (append_output)
81  open_flags = O_CREAT | O_APPEND | O_RDWR;
82  else
83  open_flags = O_CREAT | O_TRUNC | O_WRONLY,
84 
85  outfd = open(outfile, open_flags, 0644);
86  if (outfd < 0) {
87  perror("open");
88  exit(EXIT_FAILURE);
89  }
90  dup2(outfd, 1);
91  dup2(outfd, 2);
92  }
93  close(fd[1]);
94  dup2(fd[0], 0);
95  close(fd[0]);
96  execv(path, argv);
97  }
98  close(fd[0]);
99  m_write_fd = fd[1];
100  std::this_thread::sleep_for(std::chrono::milliseconds(2000));
101  }
102 
105  std::cerr << "Killing '" << m_path << "' pid=" << m_child_pid
106  << std::endl << std::flush;
107  close(m_write_fd);
108  if (kill(m_child_pid, 9) == -1)
109  perror("kill");
110  }
111 
113  int get_write_descriptor() const {
114  return m_write_fd;
115  }
116 
118  pid_t get_pid() const {
119  return m_child_pid;
120  }
121 
122  private:
124  const char *m_path;
125 
127  pid_t m_child_pid;
128 
131  };
132 
135 }
136 
137 #endif // Common_ServerLauncher_h
Launches external commands and redirects their output to a file; kills the external process when goin...
pid_t get_pid() const
Returns the pid of the external program.
~ServerLauncher()
Destructor; kills the external program.
Hypertable definitions
pid_t m_child_pid
The pid of the external program.
const char * m_path
The path of the external program.
int get_write_descriptor() const
Returns stdin of the external program.
ServerLauncher(const char *path, char *const argv[], const char *outfile=0, bool append_output=false)
Constructor; launches the external command, optionally redirects the output.
int m_write_fd
The file descriptor writing to stdin of the external program.