Merge branch 'master' of gitlabwh.uniontech.com:wuhan/se/v23/linglong/linglong

This commit is contained in:
Heysion Y 2021-09-07 20:42:36 +08:00
commit eaa008aae0
10 changed files with 168 additions and 7 deletions

View File

@ -496,14 +496,16 @@ int Container::start()
// write uid map
std::ofstream uidMapFile(util::format("/proc/%d/uid_map", entryPid));
for (auto const &idMap : dd_ptr->r.linux.uidMappings) {
uidMapFile << util::format("{} {} {}\n", idMap.containerID, idMap.hostID, idMap.size);
uidMapFile << util::format("%lu %lu %lu\n", idMap.containerID, idMap.hostID, idMap.size);
}
uidMapFile.close();
// write gid map
std::ofstream gidMapFile(util::format("/proc/%d/gid_map", entryPid));
for (auto const &idMap : dd_ptr->r.linux.gidMappings) {
gidMapFile << util::format("{} {} {}\n", idMap.containerID, idMap.hostID, idMap.size);
gidMapFile << util::format("%lu %lu %lu\n", idMap.containerID, idMap.hostID, idMap.size);
}
gidMapFile.close();
logDbg() << "finish write uid_map and pid_map" << std::flush;
s.vrijgeven();

View File

@ -84,8 +84,8 @@ static T *fromVariant(const QVariant &v)
auto mo = m->metaObject();
for (int i = mo->propertyOffset(); i < mo->propertyCount(); ++i) {
auto k = mo->property(i).name();
auto t = mo->property(i).userType();
if (t >= QVariant::UserType) {
auto t = mo->property(i).type();
if (QVariant::UserType == t) {
switch (map[k].type()) {
case QVariant::Map: {
auto value = map[k].toMap();
@ -146,7 +146,7 @@ public:
template<class T>
QJsonValue toJson() const
{
toVariant<T>(this).toJsonValue();
return toVariant<T>(this).toJsonValue();
}
};

View File

@ -130,10 +130,10 @@ T *formYaml(YAML::Node &doc)
auto mo = m->metaObject();
for (int i = mo->propertyOffset(); i < mo->propertyCount(); ++i) {
auto k = mo->property(i).name();
auto t = mo->property(i).userType();
auto t = mo->property(i).type();
QVariant v = doc[k].template as<QVariant>();
// set parent
if (t >= QVariant::UserType) {
if (QVariant::UserType == t) {
switch (v.type()) {
case QVariant::Map: {
auto map = v.toMap();

View File

@ -1,5 +1,8 @@
enable_testing()
set(CMAKE_C_FLAGS --coverage)
set(CMAKE_CXX_FLAGS --coverage)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

View File

@ -43,3 +43,9 @@ TEST(OCI, Runtime)
EXPECT_EQ(r.linux.gidMappings.at(0).containerID, 0);
EXPECT_EQ(r.linux.gidMappings.at(0).size, 1);
}
TEST(OCI, Util)
{
EXPECT_EQ(util::format("%d %d %d\n", 1, 1, 1), "1 1 1\n");
EXPECT_EQ(util::format("%d %d %d\n", uint64_t(1), 1u, 1), "1 1 1\n");
}

View File

@ -56,4 +56,6 @@ TEST(OCI, QtJson)
EXPECT_EQ(r->linux->gidMappings.at(0)->hostID, 65534);
EXPECT_EQ(r->linux->gidMappings.at(0)->containerID, 0);
EXPECT_EQ(r->linux->gidMappings.at(0)->size, 1);
r->deleteLater();
}

View File

@ -37,6 +37,8 @@ TEST(LL, YAML)
EXPECT_EQ(app->root->readonly, false);
EXPECT_EQ(app->root->path, "/run/user/1000/linglong/ab24ae64edff4ddfa8e6922eb29e2baf");
app->deleteLater();
TestApp app2;
app2.root = new Root;
@ -48,4 +50,6 @@ TEST(LL, YAML)
EXPECT_EQ(doc2["version"].as<QString>(), "2");
EXPECT_EQ(doc2["root"]["readonly"].as<QString>(), "true");
EXPECT_EQ(doc2["root"]["readonly"].as<bool>(), true);
app2.root->deleteLater();
}

16
test/setns/CMakeLists.txt Normal file
View File

@ -0,0 +1,16 @@
set(LL_SETNS_SOURCES
container_bash.cpp)
add_executable(ll-test-setns-srv
${LL_SETNS_SOURCES})
target_link_libraries(ll-test-setns-srv PRIVATE ${LINK_LIBS})
set(LL_SETNS_CLI_SOURCES
container_bash_cli.cpp)
add_executable(ll-test-setns-cli
${LL_SETNS_CLI_SOURCES})
target_link_libraries(ll-test-setns-cli PRIVATE ${LINK_LIBS})

View File

@ -0,0 +1,83 @@
// listen /tmp/rstdin
// write /tmp/rstdout
#include <unistd.h>
#include <sys/wait.h>
#include <QtConcurrent/QtConcurrentRun>
#include <QFile>
int main()
{
pid_t parent2child[2], child2parent[2];
pipe(parent2child);
pipe(child2parent);
int ppid = fork();
if (ppid < 0) {
return -1;
}
if (ppid == 0) {
// child
printf("child %d\n", ppid);
close(parent2child[1]);
close(child2parent[0]);
dup2(parent2child[0], STDIN_FILENO);
dup2(child2parent[1], STDOUT_FILENO);
char *argv[] = {(char *)"/bin/bash", nullptr};
printf("child getpid %d\n", getpid());
int ret = execv("/bin/bash", argv);
printf("execve %d %d %s\n", ret, errno, strerror(errno));
return 0;
}
// parent
printf("parent %d\n", ppid);
close(parent2child[0]);
close(child2parent[1]);
// write to parent2child[1], read from child2parent[0]
QtConcurrent::run([=]() {
QFile in("/tmp/rstdin");
in.open(QIODevice::ReadOnly);
printf("in.isOpen() %d\n", in.isOpen());
char buf[1024] = {0};
do {
auto sz = in.read(buf, 1024);
if (sz < 0) {
printf("read exit %lld\n", sz);
break;
}
if (sz > 0) {
printf("read size %lld\n", sz);
write(parent2child[1], buf, sz);
}
} while (true);
});
QtConcurrent::run([=]() {
QFile out("/tmp/rstdout");
out.open(QIODevice::WriteOnly | QIODevice::Append);
printf("out.isOpen() %d\n", out.isOpen());
char buf[1024] = {0};
do {
auto sz = read(child2parent[0], buf, 1024);
if (sz < 0) {
printf("child read exit %ld\n", sz);
break;
}
if (sz > 0) {
printf("write size %ld %s\n", sz, buf);
sz = out.write(buf, sz);
out.flush();
printf("write size %ld\n", sz);
}
} while (true);
});
waitpid(-1, nullptr, 0);
}

View File

@ -0,0 +1,45 @@
// listen /tmp/rstdin
// write /tmp/rstdout
#include <QtConcurrent/QtConcurrentRun>
#include <QFile>
#include <QCoreApplication>
#include <iostream>
int main(int argc, char **argv)
{
QCoreApplication app(argc, argv);
QtConcurrent::run([=]() {
QFile inRedirect("/tmp/rstdin");
inRedirect.open(QIODevice::WriteOnly | QIODevice::Append);
printf("inRedirect.isOpen() %d\n", inRedirect.isOpen());
for (std::string line; std::getline(std::cin, line);) {
line += "\n";
auto sz = inRedirect.write(line.data(), line.size());
printf("inRedirect.write() %d\n", sz);
inRedirect.flush();
}
});
QtConcurrent::run([=]() {
QFile out("/tmp/rstdout");
out.open(QIODevice::ReadOnly);
printf("out.isOpen() %d\n", out.isOpen());
char buf[1024] = {0};
do {
auto sz = out.read(buf, 1024);
if (sz < 0) {
printf("child read exit %lld\n", sz);
break;
}
if (sz > 0) {
printf("write size %lld %s\n", sz, buf);
std::cout << buf;
}
} while (true);
});
return QCoreApplication::exec();
}