summaryrefslogtreecommitdiff
path: root/src/dbus/server/restart.h
blob: 00a86333c515df43f5951a1e7a8d9c893579aacb (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
/*
 * Copyright (C) 2011 Intel Corporation
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) version 3.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301  USA
 */

#ifndef RESTART_H
#define RESTART_H

#include <vector>
#include <string>

#include <errno.h>

#include <boost/scoped_array.hpp>

#include <syncevo/LogRedirect.h>

#include <syncevo/declarations.h>
SE_BEGIN_CXX

/**
 * Encapsulates startup environment from main() and can do execve()
 * with it later on. Assumes that argv[0] is the executable to run.
 */
class Restart
{
    std::vector<std::string> m_argv;
    std::vector<std::string> m_env;

    void saveArray(std::vector<std::string> &array, char **p)
    {
        while(*p) {
            array.push_back(*p);
            p++;
        }
    }

    const char **createArray(const std::vector<std::string> &array)
    {
        const char **res = new const char *[(array.size() + 1)];
        size_t i;
        for (i = 0; i < array.size(); i++) {
            res[i] = array[i].c_str();
        }
        res[i] = NULL;
        return res;
    }

public:
    Restart(char **argv, char **env)
    {
        saveArray(m_argv, argv);
        saveArray(m_env, env);
    }

    void restart()
    {
        boost::scoped_array<const char *> argv(createArray(m_argv));
        boost::scoped_array<const char *> env(createArray(m_env));
        LogRedirect::reset();
        if (execve(argv[0], (char *const *)argv.get(), (char *const *)env.get())) {
            SE_THROW(StringPrintf("restarting syncevo-dbus-server failed: %s", strerror(errno)));
        }
    }
};

SE_END_CXX

#endif // RESTART_H