paludis  Version 2.6.0
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 
35 #include <sys/types.h>
36 #include <unistd.h>
37 
38 namespace paludis
39 {
40  typedef std::function<std::string (const std::string &)> ProcessPipeCommandFunction;
41 
43  public Exception
44  {
45  public:
46  ProcessError(const std::string &) noexcept;
47  };
48 
50  {
51  private:
53 
54  public:
55  /**
56  * List of arguments, one string per argv value.
57  **/
58  explicit ProcessCommand(const std::initializer_list<std::string> &);
59 
60  /**
61  * List of arguments, passed to sh -c.
62  */
63  explicit ProcessCommand(const std::string &);
64 
66  ~ProcessCommand();
67 
68  ProcessCommand(const ProcessCommand &) = delete;
69  ProcessCommand & operator= (const ProcessCommand &) = delete;
70 
71  void prepend_args(const std::initializer_list<std::string> &);
72  void append_args(const std::initializer_list<std::string> &);
73 
74  void echo_command_to(std::ostream &);
75 
76  void exec() PALUDIS_ATTRIBUTE((noreturn));
77  };
78 
80  {
81  private:
82  Pimp<Process> _imp;
83 
84  public:
85  explicit Process(ProcessCommand &&);
86  ~Process();
87 
88  Process(const Process &) = delete;
89  Process & operator= (const Process &) = delete;
90 
91  RunningProcessHandle run() PALUDIS_ATTRIBUTE((warn_unused_result));
92 
93  Process & capture_stdout(std::ostream &);
94  Process & capture_stderr(std::ostream &);
95  Process & capture_output_to_fd(std::ostream &, int fd_or_minus_one, const std::string & env_var_with_fd);
96  Process & send_input_to_fd(std::istream &, int fd_or_minus_one, const std::string & env_var_with_fd);
97  Process & set_stdin_fd(int);
98  Process & pipe_command_handler(const std::string &, const ProcessPipeCommandFunction &);
99 
100  Process & setenv(const std::string &, const std::string &);
101  Process & clearenv();
102 
103  Process & chdir(const FSPath &);
104  Process & use_ptys();
105  Process & setuid_setgid(uid_t, gid_t);
106  Process & echo_command_to(std::ostream &);
107 
108  Process & prefix_stdout(const std::string &);
109  Process & prefix_stderr(const std::string &);
110  Process & extra_newlines_if_any_output_exists();
111 
112  Process & sandbox();
113  Process & sydbox();
114 
115  Process & as_main_process();
116  };
117 
119  {
120  private:
122 
123  public:
125  const pid_t,
126  std::unique_ptr<RunningProcessThread> &&);
127 
128  ~RunningProcessHandle() noexcept(false);
130 
131  RunningProcessHandle(const RunningProcessHandle &) = delete;
132  RunningProcessHandle & operator= (const RunningProcessHandle &) = delete;
133 
134  int wait() PALUDIS_ATTRIBUTE((warn_unused_result));
135  };
136 }
137 
138 #endif
Definition: pimp.hh:51
Definition: about_metadata-fwd.hh:23
Definition: process.hh:79
Definition: exception.hh:74
Definition: process.hh:42
Definition: process.hh:49
Definition: fs_path.hh:33
Definition: process.hh:118
#define PALUDIS_ATTRIBUTE(x)
Definition: attributes.hh:53
#define PALUDIS_VISIBLE
Definition: attributes.hh:59