aboutsummaryrefslogtreecommitdiff
path: root/paludis/util/system.cc
blob: 65a0188e57b2747d3c93d1fd6bee938353ed6de5 (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
/* vim: set sw=4 sts=4 et foldmethod=syntax : */

/*
 * Copyright (c) 2006 Ciaran McCreesh <ciaran.mccreesh@blueyonder.co.uk>
 *
 * 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
 */

#include <cstdlib>
#include <paludis/util/system.hh>
#include <paludis/util/log.hh>
#include <sys/utsname.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include "config.h"

/** \file
 * Implementation of various system utilities.
 *
 * \ingroup grpsystem
 */

using namespace paludis;

GetenvError::GetenvError(const std::string & key) throw () :
    Exception("Environment variable '" + key + "' not set")
{
}

std::string
paludis::getenv_with_default(const std::string & key, const std::string & def)
{
    const char * const e(std::getenv(key.c_str()));
    return e ? e : def;
}

std::string
paludis::getenv_or_error(const std::string & key)
{
    const char * const e(std::getenv(key.c_str()));
    if (! e)
        throw GetenvError(key);
    return e;
}

namespace
{
    /**
     * Fetch the kernel version, for paludis::kernel_version.
     *
     * \ingroup grpsystem
     */
    std::string get_kernel_version()
    {
        struct utsname u;
        if (0 != uname(&u))
            throw InternalError(PALUDIS_HERE, "uname call failed");
        return u.release;
    }
}

std::string
paludis::kernel_version()
{
    static const std::string result(get_kernel_version());
    return result;
}

int
paludis::run_command(const std::string & cmd)
{
    pid_t child(fork());
    if (0 == child)
    {
        Log::get_instance()->message(ll_debug, "execl /bin/sh -c " + cmd);
        execl("/bin/sh", "sh", "-c", cmd.c_str(), static_cast<char *>(0));
        throw InternalError(PALUDIS_HERE, "execl failed"); /// \todo fixme
    }
    else if (-1 == child)
        throw InternalError(PALUDIS_HERE, "fork failed"); /// \todo fixme
    else
    {
        int status(-1);
        if (-1 == wait(&status))
            throw InternalError(PALUDIS_HERE, "wait failed"); /// \todo fixme
        return status;
    }

    throw InternalError(PALUDIS_HERE, "should never be reached");
}

MakeEnvCommand::MakeEnvCommand(const std::string & c,
        const std::string & a) :
    cmd(c),
    args(a)
{
}

MakeEnvCommand
MakeEnvCommand::operator() (const std::string & k,
        const std::string & v) const
{
    std::string vv;
    for (std::string::size_type p(0) ; p < v.length() ; ++p)
        if ('\'' == v[p])
            vv.append("'\"'\"'");
        else
            vv.append(v.substr(p, 1));

    return MakeEnvCommand(cmd, args + k + "='" + vv + "' ");
}

MakeEnvCommand::operator std::string() const
{
    return "/usr/bin/env " + args + cmd;
}

const MakeEnvCommand
paludis::make_env_command(const std::string & cmd)
{
    return MakeEnvCommand(cmd, "");
}

const std::string
paludis::make_sandbox_command(const std::string & cmd)
{
#if HAVE_SANDBOX
    return "sandbox " + cmd;
#else
    return cmd;
#endif
}