]>
git.proxmox.com Git - ceph.git/blob - ceph/src/common/SubProcess.h
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph distributed storage system
6 * Copyright (C) 2015 Mirantis Inc
8 * Author: Mykola Golub <mgolub@mirantis.com>
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
20 #if defined(__FreeBSD__) || defined(__APPLE__)
30 #include "include/compat.h"
34 * A helper class to spawn a subprocess.
38 * SubProcess cat("cat", SubProcess::PIPE, SubProcess::PIPE);
39 * if (cat.spawn() != 0) {
40 * std::cerr << "cat failed: " << cat.err() << std::endl;
43 * write_to_fd(cat.get_stdout(), "hello world!\n");
45 * read_from_fd(cat.get_stdin(), buf);
46 * if (cat.join() != 0) {
47 * std::cerr << cat.err() << std::endl;
60 SubProcess(const char *cmd
,
61 std_fd_op stdin_op
= CLOSE
,
62 std_fd_op stdout_op
= CLOSE
,
63 std_fd_op stderr_op
= CLOSE
);
64 virtual ~SubProcess();
66 void add_cmd_args(const char *arg
, ...);
67 void add_cmd_arg(const char *arg
);
69 virtual int spawn(); // Returns 0 on success or -errno on failure.
70 virtual int join(); // Returns exit code (0 on success).
72 bool is_spawned() const { return pid
> 0; }
74 int get_stdin() const;
75 int get_stdout() const;
76 int get_stderr() const;
82 void kill(int signo
= SIGTERM
) const;
84 const std::string
err() const;
87 bool is_child() const { return pid
== 0; }
93 void close_h(HANDLE
&handle
);
98 std::vector
<std::string
> cmd_args
;
102 int stdin_pipe_out_fd
;
103 int stdout_pipe_in_fd
;
104 int stderr_pipe_in_fd
;
106 std::ostringstream errstr
;
109 HANDLE proc_handle
= INVALID_HANDLE_VALUE
;
113 class SubProcessTimed
: public SubProcess
{
115 SubProcessTimed(const char *cmd
, std_fd_op stdin_op
= CLOSE
,
116 std_fd_op stdout_op
= CLOSE
, std_fd_op stderr_op
= CLOSE
,
117 int timeout
= 0, int sigkill
= SIGKILL
);
120 int spawn() override
;
125 void exec() override
;
136 void timeout_sighandler(int sig
);