4f56771771f04873b80f006568c274daa3d41a01
[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
29 #include "config/manager.hpp"
30 #include "logger/logger.hpp"
31
32 #include <unistd.h>
33 #include <sys/wait.h>
34
35 namespace lxcpp {
36
37 namespace {
38
39 int startContainer(void* data)
40 {
41     ContainerConfig& config = *static_cast<ContainerConfig*>(data);
42
43     // TODO: container preparation part 2
44
45     PrepGuestTerminal terminals(config.mTerminals);
46     terminals.execute();
47
48     lxcpp::execve(config.mInit);
49
50     return EXIT_FAILURE;
51 }
52
53 } // namespace
54
55
56 Guard::Guard(const int channelFD)
57     : mChannel(channelFD)
58 {
59     mChannel.setCloseOnExec(true);
60     config::loadFromFD(mChannel.getFD(), mConfig);
61
62     logger::setupLogger(mConfig.mLogger.getType(),
63                         mConfig.mLogger.getLevel(),
64                         mConfig.mLogger.getArg());
65
66     LOGD("Config & logging restored");
67
68     try {
69         LOGD("Setting the guard process title");
70         const std::string title = "[LXCPP] " + mConfig.mName + " " + mConfig.mRootPath;
71         setProcTitle(title);
72     } catch (std::exception &e) {
73         // Ignore, this is optional
74         LOGW("Failed to set the guard process title: " << e.what());
75     }
76 }
77
78 Guard::~Guard()
79 {
80 }
81
82 int Guard::execute()
83 {
84     // TODO: container preparation part 1
85
86     const pid_t initPid = lxcpp::clone(startContainer,
87                                        &mConfig,
88                                        mConfig.mNamespaces);
89
90     mConfig.mGuardPid = ::getpid();
91     mConfig.mInitPid = initPid;
92
93     mChannel.write(mConfig.mGuardPid);
94     mChannel.write(mConfig.mInitPid);
95     mChannel.shutdown();
96
97     int status = lxcpp::waitpid(initPid);
98     LOGD("Init exited with status: " << status);
99     return status;
100 }
101
102 } // namespace lxcpp