aboutsummaryrefslogtreecommitdiff
path: root/paludis/util/system.hh
blob: 1ab5745e02fd371c459484dc68dec14e29f4ef0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/* vim: set sw=4 sts=4 et foldmethod=syntax : */

/*
 * Copyright (c) 2006, 2007, 2008, 2009 Ciaran McCreesh
 *
 * This file is part of the Paludis package manager. Paludis is free software;
 * you can redistribute it and/or modify it under the terms of the GNU General
 * Public License version 2, as published by the Free Software Foundation.
 *
 * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA  02111-1307  USA
 */

#ifndef PALUDIS_GUARD_PALUDIS_UTIL_SYSTEM_HH
#define PALUDIS_GUARD_PALUDIS_UTIL_SYSTEM_HH 1

#include <paludis/util/exception.hh>
#include <paludis/util/private_implementation_pattern.hh>
#include <paludis/util/wrapped_forward_iterator-fwd.hh>
#include <tr1/memory>
#include <tr1/functional>
#include <string>
#include <sys/types.h>

/** \file
 * Various system utilities.
 *
 * \ingroup g_system
 *
 * \section Examples
 *
 * - None at this time.
 */

namespace paludis
{
    class FSEntry;

    /**
     * Thrown if getenv_or_error fails.
     *
     * \ingroup g_exceptions
     * \ingroup g_system
     * \nosubgrouping
     */
    class PALUDIS_VISIBLE GetenvError : public Exception
    {
        public:
            ///\name Basic operations
            ///\{

            GetenvError(const std::string & key) throw ();

            ///\}
    };

    /**
     * Thrown if fork, wait or chdir fail when running a command.
     *
     * \ingroup g_exceptions
     * \ingroup g_system
     * \nosubgrouping
     */
    class PALUDIS_VISIBLE RunCommandError : public Exception
    {
        public:
            /**
             * Constructor.
             */
            RunCommandError(const std::string & message) throw ();
    };

    /**
     * Fetch the value of environment variable key, or def if the variable is
     * not defined.
     *
     * \ingroup g_system
     */
    std::string getenv_with_default(const std::string & key, const std::string & def) PALUDIS_VISIBLE;

    /**
     * Fetch the value of environment variable key, or throw a GetenvError if
     * the variable is not defined.
     *
     * \ingroup g_system
     */
    std::string getenv_or_error(const std::string & key) PALUDIS_VISIBLE;

    /**
     * Fetch the kernel version, for $KV.
     *
     * \ingroup g_system
     */
    std::string kernel_version() PALUDIS_VISIBLE;

    /**
     * A command to be run.
     *
     * \see PStream
     * \see run_command
     * \ingroup g_system
     * \nosubgrouping
     */
    class PALUDIS_VISIBLE Command :
        private PrivateImplementationPattern<Command>
    {
        public:
            ///\name Basic operations
            ///\{

            Command(const std::string &);
            Command(const char * const);
            Command(const Command &);
            const Command & operator= (const Command &);
            ~Command();

            ///\}

            ///\name Change command execution options
            ///\{

            /**
             * Include a chdir before we run our command.
             */
            Command & with_chdir(const FSEntry &);

            /**
             * Add a setenv before we run our command.
             */
            Command & with_setenv(const std::string &, const std::string &);

            /**
             * Remove (most) existing environment variables before
             * setting those added with with_setenv.
             *
             * \since 0.36
             */
            Command & with_clearenv();

            /**
             * Run our command sandboxed.
             */
            Command & with_sandbox();

            /**
             * Run our command sydboxed
             *
             * \since 0.38
             */
            Command & with_sydbox();

            /**
             * Echo the command to be run to stderr before running it.
             */
            Command & with_echo_to_stderr();

            /**
             * Add a setuid and setgid before running our command.
             */
            Command & with_uid_gid(const uid_t, const gid_t);

            /**
             * Prefix stdout output with this.
             */
            Command & with_stdout_prefix(const std::string &);

            /**
             * Prefix stderr output with this.
             */
            Command & with_stderr_prefix(const std::string &);

            /**
             * If prefixing, and if the output contains only blanks, don't display
             * any output.
             */
            Command & with_prefix_discard_blank_output();

            /**
             * If prefixing, prefix blank lines too.
             */
            Command & with_prefix_blank_lines();

            /**
             * Specify a pipe command handler.
             */
            Command & with_pipe_command_handler(const std::tr1::function<std::string (const std::string &)> &);

            /**
             * Specify a stream to which stdout is captured and written.
             */
            Command & with_captured_stdout_stream(std::ostream * const);

            /**
             * Specify a stream to which stderr is captured and written.
             *
             * \since 0.30
             */
            Command & with_captured_stderr_stream(std::ostream * const);

            /**
             * Send the contents of a stream in via a particular FD.
             *
             * May only be called once.
             *
             * \param fd Send the stream to this FD. If -1, pick an unused FD.
             * \param env_var If not empty, put the FD chosen in this env var.
             *
             * \since 0.40
             */
            Command & with_input_stream(
                    std::istream * const,
                    int fd,
                    const std::string & env_var);

            /**
             * Use ptys instead of pipes to capture stdout and/or stderr.
             *
             * \since 0.38
             */
            Command & with_ptys();

            ///\}

            ///\name Fetch command execution options
            ///\{

            /**
             * Our command, as a string.
             */
            std::string command() const;

            /**
             * Where to chdir, as a string.
             */
            std::string chdir() const;

            /**
             * Echo ourself to stderr.
             */
            void echo_to_stderr() const;

            /**
             * The uid for setuid.
             */
            std::tr1::shared_ptr<const uid_t> uid() const;

            /**
             * The gid for setgid.
             */
            std::tr1::shared_ptr<const gid_t> gid() const;

            /**
             * The stdout prefix.
             */
            std::string stdout_prefix() const;

            /**
             * The stderr prefix.
             */
            std::string stderr_prefix() const;

            /**
             * If prefixing, and if the output contains only blanks, don't display
             * any output?
             */
            bool prefix_discard_blank_output() const;

            /**
             * Prefix blank lines?
             */
            bool prefix_blank_lines() const;

            /**
             * The pipe command handler.
             */
            const std::tr1::function<std::string (const std::string &)> & pipe_command_handler() const;

            /**
             * The captured stdout stream, or null.
             */
            std::ostream * captured_stdout_stream() const;

            /**
             * The captured stderr stream, or null.
             *
             * \since 0.30
             */
            std::ostream * captured_stderr_stream() const;

            /**
             * The input stream, or null.
             *
             * \since 0.40
             */
            std::istream * input_stream() const;

            /**
             * The input FD, if input_stream() not null.
             *
             * \since 0.40
             */
            int input_fd() const;

            /**
             * The input FD env var, if input_stream() not null.
             *
             * \since 0.40
             */
            const std::string input_fd_env_var() const;

            /**
             * Uses ptys instead of pipes?
             *
             * \since 0.38
             */
            bool ptys() const;

            /**
             * Should we clear existing environment variables?
             *
             * \since 0.36
             */
            bool clearenv() const;

            ///\}

            ///\name Iterate over our setenvs.
            ///\{

            struct ConstIteratorTag;
            typedef WrappedForwardIterator<ConstIteratorTag, const std::pair<const std::string, std::string> > ConstIterator;
            ConstIterator begin_setenvs() const;
            ConstIterator end_setenvs() const;

            ///\}
    };

    /**
     * Run a command, wait for it to terminate and return its exit status.
     *
     * \ingroup g_system
     */
    int run_command(const Command & cmd) PALUDIS_VISIBLE
        PALUDIS_ATTRIBUTE((warn_unused_result));

    /**
     * Become another command.
     *
     * Actions that change the initial state (uid / gid, env) work, as do input
     * streams, but output redirection does not. Pipe commands don't work, but
     * could be made to.
     *
     * \ingroup g_system
     * \since 0.40.1
     */
    void become_command(const Command & cmd) PALUDIS_VISIBLE PALUDIS_ATTRIBUTE((noreturn));

    /**
     * Set the stderr and close for stdout fds used by run_command and
     * run_command_in_directory.
     *
     * \ingroup g_system
     */
    void set_run_command_stdout_fds(const int, const int) PALUDIS_VISIBLE;

    /**
     * Set the stderr and close for stderr fds used by run_command and
     * run_command_in_directory.
     *
     * \ingroup g_system
     */
    void set_run_command_stderr_fds(const int, const int) PALUDIS_VISIBLE;

    /**
     * Fetch the username for a uid, or the uid as a string if not available.
     *
     * \ingroup g_system
     */
    std::string get_user_name(const uid_t) PALUDIS_VISIBLE;

    /**
     * Fetch the group name for a gid, or the gid as a string if not available.
     *
     * \ingroup g_system
     */
    std::string get_group_name(const gid_t) PALUDIS_VISIBLE;
}

#endif