Seastar
High performance C++ framework for concurrent servers
process.hh
1 /*
2  * This file is open source software, licensed to you under the terms
3  * of the Apache License, Version 2.0 (the "License"). See the NOTICE file
4  * distributed with this work for additional information regarding copyright
5  * ownership. You may not use this file except in compliance with the License.
6  *
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing,
12  * software distributed under the License is distributed on an
13  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14  * KIND, either express or implied. See the License for the
15  * specific language governing permissions and limitations
16  * under the License.
17  */
18 
19 /*
20  * Copyright (C) 2022 Kefu Chai ( tchaikov@gmail.com )
21  */
22 
23 #pragma once
24 
25 #ifndef SEASTAR_MODULE
26 #include <sys/types.h>
27 #include <algorithm>
28 #include <filesystem>
29 #include <initializer_list>
30 #include <iterator>
31 #include <string_view>
32 #include <utility>
33 #include <variant>
34 #include <vector>
35 #include <fmt/format.h>
36 #endif
37 #include <seastar/core/iostream.hh>
38 #include <seastar/core/posix.hh>
39 #include <seastar/core/sstring.hh>
40 
41 namespace seastar::experimental {
42 
46 SEASTAR_MODULE_EXPORT
49  std::vector<sstring> argv;
51  std::vector<sstring> env;
52 };
53 
59 SEASTAR_MODULE_EXPORT
60 class process {
61  struct create_tag {};
68  static future<process> spawn(const std::filesystem::path& pathname,
69  spawn_parameters params);
75  static future<process> spawn(const std::filesystem::path& pathname);
76 public:
77  process(create_tag, pid_t pid, file_desc&& cin, file_desc&& cout, file_desc&& cerr);
84  struct wait_exited {
85  int exit_code;
86  };
87  struct wait_signaled {
88  int terminating_signal;
89  };
90  using wait_status = std::variant<wait_exited, wait_signaled>;
96  void terminate();
98  void kill();
99 
100 private:
101  const pid_t _pid;
102  file_desc _stdin;
103  file_desc _stdout;
104  file_desc _stderr;
105 
106  friend future<process> spawn_process(const std::filesystem::path&,
108  friend future<process> spawn_process(const std::filesystem::path&);
109 };
110 }
Definition: process.hh:60
input_stream< char > cout()
Return an writable stream which provides stdout output from the child process.
output_stream< char > cin()
Return an writable stream which provides input from the child process.
void terminate()
Stop the process using SIGTERM.
void kill()
Force the process to exit using SIGKILL.
friend future< process > spawn_process(const std::filesystem::path &, spawn_parameters)
input_stream< char > cerr()
Return an writable stream which provides stderr output from the child process.
friend future< process > spawn_process(const std::filesystem::path &)
future< wait_status > wait()
Definition: posix.hh:85
A representation of a possibly not-yet-computed value.
Definition: future.hh:1238
std::vector< sstring > env
The environment variables for the program.
Definition: process.hh:51
std::vector< sstring > argv
The arguments passed to the program.
Definition: process.hh:49