lxcpp: provisioning implementation (part 1)
[platform/core/security/vasum.git] / libs / lxcpp / guard / guard.cpp
1 /*
2  *  Copyright (C) 2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  This library is free software; you can redistribute it and/or
5  *  modify it under the terms of the GNU Lesser General Public
6  *  License version 2.1 as published by the Free Software Foundation.
7  *
8  *  This library is distributed in the hope that it will be useful,
9  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  *  Lesser General Public License for more details.
12  *
13  *  You should have received a copy of the GNU Lesser General Public
14  *  License along with this library; if not, write to the Free Software
15  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
16  */
17
18 /**
19  * @file
20  * @author  Lukasz Pawelczyk (l.pawelczyk@samsung.com)
21  * @brief   LXCPP guard process implementation
22  */
23
24 #include "lxcpp/utils.hpp"
25 #include "lxcpp/guard/guard.hpp"
26 #include "lxcpp/process.hpp"
27 #include "lxcpp/commands/prep-guest-terminal.hpp"
28 #include "lxcpp/commands/provision.hpp"
29
30 #include "config/manager.hpp"
31 #include "logger/logger.hpp"
32
33 #include <unistd.h>
34 #include <sys/wait.h>
35
36 namespace lxcpp {
37
38 namespace {
39
40 int startContainer(void* data)
41 {
42     ContainerConfig& config = *static_cast<ContainerConfig*>(data);
43
44     // TODO: container preparation part 2
45
46     Provisions provisions(config);
47     provisions.execute();
48
49     PrepGuestTerminal terminals(config.mTerminals);
50     terminals.execute();
51
52     lxcpp::execve(config.mInit);
53
54     return EXIT_FAILURE;
55 }
56
57 } // namespace
58
59
60 Guard::Guard(const int channelFD)
61     : mChannel(channelFD)
62 {
63     mChannel.setCloseOnExec(true);
64     config::loadFromFD(mChannel.getFD(), mConfig);
65
66     logger::setupLogger(mConfig.mLogger.getType(),
67                         mConfig.mLogger.getLevel(),
68                         mConfig.mLogger.getArg());
69
70     LOGD("Config & logging restored");
71
72     try {
73         LOGD("Setting the guard process title");
74         const std::string title = "[LXCPP] " + mConfig.mName + " " + mConfig.mRootPath;
75         setProcTitle(title);
76     } catch (std::exception &e) {
77         // Ignore, this is optional
78         LOGW("Failed to set the guard process title: " << e.what());
79     }
80 }
81
82 Guard::~Guard()
83 {
84 }
85
86 int Guard::execute()
87 {
88     // TODO: container preparation part 1
89
90     const pid_t initPid = lxcpp::clone(startContainer,
91                                        &mConfig,
92                                        mConfig.mNamespaces);
93
94     mConfig.mGuardPid = ::getpid();
95     mConfig.mInitPid = initPid;
96
97     mChannel.write(mConfig.mGuardPid);
98     mChannel.write(mConfig.mInitPid);
99     mChannel.shutdown();
100
101     int status = lxcpp::waitpid(initPid);
102     LOGD("Init exited with status: " << status);
103
104     // TODO: cleanup after child exits
105     Provisions provisions(mConfig);
106     provisions.revert();
107
108     return status;
109 }
110
111 } // namespace lxcpp