paludis Version 3.0.2
The one true package mangler
process.hh
1/* vim: set sw=4 sts=4 et foldmethod=syntax : */
2
3/*
4 * Copyright (c) 2010, 2011 Ciaran McCreesh
5 *
6 * This file is part of the Paludis package manager. Paludis is free software;
7 * you can redistribute it and/or modify it under the terms of the GNU General
8 * Public License version 2, as published by the Free Software Foundation.
9 *
10 * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
11 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 * details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
17 * Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20#ifndef PALUDIS_GUARD_PALUDIS_UTIL_PROCESS_HH
21#define PALUDIS_GUARD_PALUDIS_UTIL_PROCESS_HH 1
22
23#include <paludis/util/process-fwd.hh>
25#include <paludis/util/pimp.hh>
27#include <paludis/util/fs_path-fwd.hh>
28
29#include <string>
30#include <iosfwd>
31#include <memory>
32#include <functional>
33#include <initializer_list>
34#include <vector>
35
36#include <sys/types.h>
37#include <unistd.h>
38
39namespace paludis
40{
41 typedef std::function<std::string (const std::string &)> ProcessPipeCommandFunction;
42
43 class PALUDIS_VISIBLE ProcessError :
44 public Exception
45 {
46 public:
47 ProcessError(const std::string &) noexcept;
48 ProcessError(int error, const std::string &) noexcept;
49 };
50
52 {
53 private:
55
56 public:
57 /**
58 * List of arguments, one string per argv value.
59 **/
60 explicit ProcessCommand(const std::initializer_list<std::string> &);
61
62 /**
63 * List of arguments, passed to sh -c.
64 */
65 explicit ProcessCommand(const std::string &);
66
69
70 ProcessCommand(const ProcessCommand &) = delete;
71 ProcessCommand & operator= (const ProcessCommand &) = delete;
72
73 void prepend_args(const std::initializer_list<std::string> &);
74 void append_args(const std::initializer_list<std::string> &);
75
76 void echo_command_to(std::ostream &);
77
78 ProcessCommand & setenv(const std::string &, const std::string &);
79 ProcessCommand & clearenv();
80 ProcessCommand & chdir(const FSPath &);
81 ProcessCommand & setuid_setgid(uid_t, gid_t);
82
83 void exec_prepare();
84 void exec(int err_fd = -1) PALUDIS_ATTRIBUTE((noreturn));
85
86 const std::vector<std::string>& get_args();
87 const std::string& get_args_string();
88 };
89
90 class PALUDIS_VISIBLE Process
91 {
92 private:
93 Pimp<Process> _imp;
94
95 public:
96 explicit Process(ProcessCommand &&);
97 ~Process();
98
99 Process(const Process &) = delete;
100 Process & operator= (const Process &) = delete;
101
102 RunningProcessHandle run() PALUDIS_ATTRIBUTE((warn_unused_result));
103
104 Process & setenv(const std::string &, const std::string &);
105 Process & clearenv();
106 Process & chdir(const FSPath &);
107 Process & setuid_setgid(uid_t, gid_t);
108
109 Process & capture_stdout(std::ostream &);
110 Process & capture_stderr(std::ostream &);
111 Process & capture_output_to_fd(std::ostream &, int fd_or_minus_one, const std::string & env_var_with_fd);
112 Process & send_input_to_fd(std::istream &, int fd_or_minus_one, const std::string & env_var_with_fd);
113 Process & set_stdin_fd(int);
114 Process & pipe_command_handler(const std::string &, const ProcessPipeCommandFunction &);
115
116 Process & use_ptys();
117 Process & echo_command_to(std::ostream &);
118
119 Process & prefix_stdout(const std::string &);
120 Process & prefix_stderr(const std::string &);
121 Process & extra_newlines_if_any_output_exists();
122
123 Process & sandbox();
124 Process & sydbox(const std::string &,
125 const std::string &,
126 const uid_t,
127 const gid_t);
128
129 /* NOTE: Do not use this functionality together with a
130 * multi-threaded process. Not only will all but the
131 * executing thread disappear, but locks the other
132 * threads held may be still locked or data protected by
133 * them might be in an inconsistent state. */
134 Process & as_main_process();
135 };
136
137 class PALUDIS_VISIBLE RunningProcessHandle
138 {
139 private:
141
142 public:
143 RunningProcessHandle(
144 const pid_t,
145 std::unique_ptr<RunningProcessThread> &&);
146
147 ~RunningProcessHandle() noexcept(false);
148 RunningProcessHandle(RunningProcessHandle &&);
149
150 RunningProcessHandle(const RunningProcessHandle &) = delete;
151 RunningProcessHandle & operator= (const RunningProcessHandle &) = delete;
152
153 int wait() PALUDIS_ATTRIBUTE((warn_unused_result));
154 };
155}
156
157#endif
Definition fs_path.hh:34
Definition pimp.hh:52
Definition process.hh:52
ProcessCommand(const std::string &)
ProcessCommand(const std::initializer_list< std::string > &)
Definition process.hh:138
#define PALUDIS_ATTRIBUTE(x)
Definition attributes.hh:53
#define PALUDIS_VISIBLE
Definition attributes.hh:59
Definition about_metadata-fwd.hh:24