#include "lxcpp/credentials.hpp"
#include "utils/exception.hpp"
+#include "utils/fd-utils.hpp"
+#include "logger/logger.hpp"
#include <unistd.h>
#include <sys/mount.h>
+#include <sys/types.h>
+#include <fcntl.h>
#include <functional>
*/
}
+bool setupControlTTY(const int ttyFD)
+{
+ if (!::isatty(ttyFD)) {
+ return false;
+ }
+
+ if (::setsid() < 0) {
+ return false;
+ }
+
+ if (::ioctl(ttyFD, TIOCSCTTY, NULL) < 0) {
+ return false;
+ }
+
+ if (::dup2(ttyFD, STDIN_FILENO) < 0) {
+ return false;
+ }
+
+ if (::dup2(ttyFD, STDOUT_FILENO) < 0) {
+ return false;
+ }
+
+ if (::dup2(ttyFD, STDERR_FILENO) < 0) {
+ return false;
+ }
+
+ return true;
+}
+
int execFunction(void* call)
{
try {
Container::AttachCall& userCall,
const uid_t uid,
const gid_t gid,
+ const std::string& ttyPath,
const std::vector<gid_t>& supplementaryGids,
const int capsToKeep,
const std::string& workDirInContainer,
mEnvToKeep(envToKeep),
mEnvToSet(envToSet)
{
+ mTTYFD = ::open(ttyPath.c_str(), O_RDWR | O_NOCTTY);
+ if (mTTYFD < 0) {
+ const std::string msg = "open() failed: " +
+ utils::getSystemErrorMessage();
+ LOGE(msg);
+ throw BadArgument(msg);
+ }
}
Attach::~Attach()
{
+ utils::close(mTTYFD);
}
void Attach::execute()
mUserCall,
mUid,
mGid,
+ mTTYFD,
mSupplementaryGids,
mCapsToKeep,
mEnvToKeep,
intermChannel.setRight();
interm(intermChannel, call);
intermChannel.shutdown();
- ::_exit(0);
+ ::_exit(EXIT_SUCCESS);
}
}
int Attach::child(const Container::AttachCall& call,
const uid_t uid,
const gid_t gid,
+ const int ttyFD,
const std::vector<gid_t>& supplementaryGids,
const int capsToKeep,
const std::vector<std::string>& envToKeep,
lxcpp::setuid(uid);
+ // Set control TTY
+ if(!setupControlTTY(ttyFD)) {
+ ::_exit(EXIT_FAILURE);
+ }
+
// Run user's code
return call();
}
Container::AttachCall& userCall,
const uid_t uid,
const gid_t gid,
+ const std::string& ttyPath,
const std::vector<gid_t>& supplementaryGids,
const int capsToKeep,
const std::string& workDirInContainer,
const Container::AttachCall& mUserCall;
const uid_t mUid;
const gid_t mGid;
+ int mTTYFD;
const std::vector<gid_t>& mSupplementaryGids;
const int mCapsToKeep;
const std::string& mWorkDirInContainer;
static int child(const Container::AttachCall& call,
const uid_t uid,
const gid_t gid,
+ const int ttyFD,
const std::vector<gid_t>& supplementaryGids,
const int capsToKeep,
const std::vector<std::string>& envToKeep,