Skip to content

Commit

Permalink
fix: couldn't find binary in container
Browse files Browse the repository at this point in the history
improve parsing args

Signed-off-by: ComixHe <heyuming@deepin.org>
  • Loading branch information
ComixHe authored and dengbo11 committed May 22, 2024
1 parent 66cd16b commit 448d0f5
Showing 1 changed file with 23 additions and 12 deletions.
35 changes: 23 additions & 12 deletions apps/ll-box/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,22 +148,30 @@ int exec(struct arg_exec *arg, int argc, char **argv) noexcept
}

auto wdns = linglong::util::format("--wdns=%s", arg->cwd.c_str());
const char *nsenterArg[] = { "nsenter", "-t", boxPidStr.c_str(), "-U", "-m",
"-p", wdns.c_str(), "--preserve-credentials", "bash", "-c" };
auto newArgc = 10 + argc + 1; // except argv[0] and add '&'
const char *nsenterArgv[] = {
"nsenter", "-t", boxPidStr.c_str(), "-U", "-m",
"-p", wdns.c_str(), "--preserve-credentials", "bash", "--login",
"-c", nullptr
};

int nsenterArgc{ 0 };
while (nsenterArgv[nsenterArgc] != nullptr) {
++nsenterArgc;
}
auto newArgc = nsenterArgc + argc + 1; // except argv[0] and add '&'
const char **newArgv = (const char **)malloc(sizeof(char *) * newArgc);

if (newArgv == nullptr) {
logErr() << "malloc error";
return errno;
}

for (int i = 0; i < 10; ++i) {
newArgv[i] = nsenterArg[i];
for (int i = 0; i < nsenterArgc; ++i) {
newArgv[i] = nsenterArgv[i];
}

for (int i = 0; i < argc; ++i) {
newArgv[i + 10] = argv[i + 1];
newArgv[i + nsenterArgc] = argv[i + 1];
}

newArgv[newArgc - 2] = "&";
Expand Down Expand Up @@ -461,19 +469,22 @@ int cmd_kill(struct argp_state *state)

::free(argv[0]);
argv[0] = argv0;
state->next += 1;
state->next += argc - 1;

if (argv[1] == nullptr) {
if (state->argv[state->next] == nullptr) {
logErr() << "container id must be set.";
return EINVAL;
}

std::string container{ argv[1] };
std::string container{ state->argv[state->next++] };
std::string signal;
if (argv[2] != nullptr) {
signal = argv[2];
if (state->argv[state->next] != nullptr) {
signal = state->argv[state->next++];
}

while (state->argv[state->next] != nullptr) {
++(state->next);
}
state->next += 2;

auto *global = (struct arg_global *)state->input;
global->exitCode = kill(container, signal);
Expand Down

0 comments on commit 448d0f5

Please sign in to comment.