lxcpp: Command implementation
[platform/core/security/vasum.git] / libs / lxcpp / commands / attach.hpp
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  Jan Olszak (j.olszak@samsung.com)
21  * @brief   Implementation of attaching to a container
22  */
23
24 #ifndef LXCPP_COMMANDS_ATTACH_HPP
25 #define LXCPP_COMMANDS_ATTACH_HPP
26
27 #include "lxcpp/commands/command.hpp"
28 #include "lxcpp/container-impl.hpp"
29 #include "utils/channel.hpp"
30
31 #include <string>
32
33 namespace lxcpp {
34
35 class Attach final: Command {
36 public:
37     typedef std::function<int(void)> Call;
38
39     /**
40      * Runs call in the container's context
41      *
42      * Object attach should be used immediately after creation.
43      * It will not be stored for future use like most other commands.
44      *
45      * @param container container to which it attaches
46      * @param userCall user's function to run
47      * @param capsToKeep capabilities that will be kept
48      * @param workDirInContainer work directory set for the new process
49      * @param envToKeep environment variables that will be kept
50      * @param envToSet new environment variables that will be set
51      */
52     Attach(lxcpp::ContainerImpl& container,
53            Container::AttachCall& userCall,
54            const int capsToKeep,
55            const std::string& workDirInContainer,
56            const std::vector<std::string>& envToKeep,
57            const std::vector<std::pair<std::string, std::string>>& envToSet);
58     ~Attach();
59
60     void execute();
61
62 private:
63     const lxcpp::ContainerImpl& mContainer;
64     const Container::AttachCall& mUserCall;
65     const int mCapsToKeep;
66     const std::string& mWorkDirInContainer;
67     const std::vector<std::string>& mEnvToKeep;
68     const std::vector<std::pair<std::string, std::string>>& mEnvToSet;
69
70     // Methods for different stages of setting up the attachment
71     static int child(const Container::AttachCall& call,
72                      const int capsToKeep,
73                      const std::vector<std::string>& envToKeep,
74                      const std::vector<std::pair<std::string, std::string>>& envToSet);
75
76     void parent(utils::Channel& intermChannel,
77                 const pid_t pid);
78
79     void interm(utils::Channel& intermChannel,
80                 Call& call);
81 };
82
83 } // namespace lxcpp
84
85 #endif // LXCPP_COMMANDS_ATTACH_HPP