2018-11-02 12:50:12 +00:00
|
|
|
/*
|
2020-09-08 12:55:01 +00:00
|
|
|
Copyright (C) 2017-2020 Fredrik Öhrström
|
2018-11-02 12:50:12 +00:00
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "shell.h"
|
|
|
|
#include "util.h"
|
|
|
|
|
2019-02-25 21:03:20 +00:00
|
|
|
#include <assert.h>
|
2019-11-03 15:31:30 +00:00
|
|
|
#include <fcntl.h>
|
2018-11-02 12:50:12 +00:00
|
|
|
#include <memory.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <sys/types.h>
|
2019-02-23 20:26:45 +00:00
|
|
|
#include <sys/wait.h>
|
2018-11-02 12:50:12 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
void invokeShell(string program, vector<string> args, vector<string> envs)
|
|
|
|
{
|
2019-01-04 21:27:51 +00:00
|
|
|
vector<const char*> argv(args.size()+2);
|
2019-01-04 21:50:31 +00:00
|
|
|
char *p = new char[program.length()+1];
|
|
|
|
strcpy(p, program.c_str());
|
|
|
|
argv[0] = p;
|
2018-11-02 12:50:12 +00:00
|
|
|
int i = 1;
|
2019-04-03 15:31:51 +00:00
|
|
|
debug("(shell) exec \"%s\"\n", program.c_str());
|
2018-11-02 12:50:12 +00:00
|
|
|
for (auto &a : args) {
|
|
|
|
argv[i] = a.c_str();
|
|
|
|
i++;
|
2019-04-03 15:31:51 +00:00
|
|
|
debug("(shell) arg \"%s\"\n", a.c_str());
|
2018-11-02 12:50:12 +00:00
|
|
|
}
|
|
|
|
argv[i] = NULL;
|
|
|
|
|
2019-01-04 21:27:51 +00:00
|
|
|
vector<const char*> env(envs.size()+1);
|
2019-01-04 21:50:31 +00:00
|
|
|
env[0] = p;
|
2018-11-02 12:50:12 +00:00
|
|
|
i = 0;
|
|
|
|
for (auto &e : envs) {
|
|
|
|
env[i] = e.c_str();
|
|
|
|
i++;
|
2019-04-03 15:31:51 +00:00
|
|
|
debug("(shell) env \"%s\"\n", e.c_str());
|
2018-11-02 12:50:12 +00:00
|
|
|
}
|
|
|
|
env[i] = NULL;
|
|
|
|
|
|
|
|
pid_t pid = fork();
|
|
|
|
int status;
|
|
|
|
if (pid == 0) {
|
|
|
|
// I am the child!
|
|
|
|
close(0); // Close stdin
|
2020-04-21 16:07:46 +00:00
|
|
|
#if (defined(__APPLE__) && defined(__MACH__)) || defined(__FreeBSD__)
|
2019-02-23 20:26:45 +00:00
|
|
|
execve(program.c_str(), (char*const*)&argv[0], (char*const*)&env[0]);
|
|
|
|
#else
|
2021-11-12 14:51:06 +00:00
|
|
|
execvpe(program.c_str(), (char*const*)&argv[0], (char*const*)&env[0]);
|
2019-02-23 20:26:45 +00:00
|
|
|
#endif
|
|
|
|
|
2018-11-02 12:50:12 +00:00
|
|
|
perror("Execvp failed:");
|
2019-04-03 15:31:51 +00:00
|
|
|
error("(shell) invoking %s failed!\n", program.c_str());
|
2018-11-02 12:50:12 +00:00
|
|
|
} else {
|
|
|
|
if (pid == -1) {
|
2019-04-03 15:31:51 +00:00
|
|
|
error("(shell) could not fork!\n");
|
2018-11-02 12:50:12 +00:00
|
|
|
}
|
2019-04-03 15:31:51 +00:00
|
|
|
debug("(shell) waiting for child %d to complete.\n", pid);
|
2018-11-02 12:50:12 +00:00
|
|
|
// Wait for the child to finish!
|
|
|
|
waitpid(pid, &status, 0);
|
|
|
|
if (WIFEXITED(status)) {
|
|
|
|
// Child exited properly.
|
|
|
|
int rc = WEXITSTATUS(status);
|
2019-04-03 15:31:51 +00:00
|
|
|
debug("(shell) %s: return code %d\n", program.c_str(), rc);
|
2018-11-02 12:50:12 +00:00
|
|
|
if (rc != 0) {
|
2019-04-03 15:31:51 +00:00
|
|
|
warning("(shell) %s exited with non-zero return code: %d\n", program.c_str(), rc);
|
2018-11-02 12:50:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-01-04 21:50:31 +00:00
|
|
|
delete[] p;
|
2018-11-02 12:50:12 +00:00
|
|
|
}
|
2019-02-25 21:03:20 +00:00
|
|
|
|
2019-11-03 15:31:30 +00:00
|
|
|
bool invokeBackgroundShell(string program, vector<string> args, vector<string> envs, int *fd_out, int *pid)
|
2019-02-25 21:03:20 +00:00
|
|
|
{
|
|
|
|
int link[2];
|
|
|
|
vector<const char*> argv(args.size()+2);
|
|
|
|
char *p = new char[program.length()+1];
|
|
|
|
strcpy(p, program.c_str());
|
|
|
|
argv[0] = p;
|
|
|
|
int i = 1;
|
2019-04-03 15:31:51 +00:00
|
|
|
debug("(bgshell) exec background \"%s\"\n", program.c_str());
|
2019-02-25 21:03:20 +00:00
|
|
|
for (auto &a : args) {
|
|
|
|
argv[i] = a.c_str();
|
|
|
|
i++;
|
2019-04-03 15:31:51 +00:00
|
|
|
debug("(bgshell) arg \"%s\"\n", a.c_str());
|
2019-02-25 21:03:20 +00:00
|
|
|
}
|
|
|
|
argv[i] = NULL;
|
|
|
|
|
|
|
|
vector<const char*> env(envs.size()+1);
|
|
|
|
env[0] = p;
|
|
|
|
i = 0;
|
|
|
|
for (auto &e : envs) {
|
|
|
|
env[i] = e.c_str();
|
|
|
|
i++;
|
2019-04-03 15:31:51 +00:00
|
|
|
debug("(bgshell) env \"%s\"\n", e.c_str());
|
2019-02-25 21:03:20 +00:00
|
|
|
}
|
|
|
|
env[i] = NULL;
|
|
|
|
|
|
|
|
if (pipe(link) == -1) {
|
2019-04-03 15:31:51 +00:00
|
|
|
error("(bgshell) could not create pipe!\n");
|
2019-02-25 21:03:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
*pid = fork();
|
|
|
|
if (*pid == 0) {
|
|
|
|
// I am the child!
|
2020-08-10 18:22:58 +00:00
|
|
|
// Restore the handlers in the child.
|
|
|
|
restoreSignalHandlers();
|
|
|
|
|
|
|
|
// Make this child a process group leader,
|
|
|
|
// so that we can easily terminate it and all its
|
|
|
|
// subprocesses later one!
|
|
|
|
setpgid(0, 0);
|
2019-02-25 21:03:20 +00:00
|
|
|
// Redirect stdout and stderr to pipe
|
|
|
|
dup2 (link[1], STDOUT_FILENO);
|
|
|
|
dup2 (link[1], STDERR_FILENO);
|
|
|
|
// Close return pipe, not duped.
|
|
|
|
close(link[0]);
|
|
|
|
// Close old forward fd pipe.
|
|
|
|
close(link[1]);
|
|
|
|
close(0); // Close stdin
|
|
|
|
|
2020-04-21 16:07:46 +00:00
|
|
|
#if (defined(__APPLE__) && defined(__MACH__)) || defined(__FreeBSD__)
|
2019-02-25 21:03:20 +00:00
|
|
|
execve(program.c_str(), (char*const*)&argv[0], (char*const*)&env[0]);
|
|
|
|
#else
|
2021-11-12 14:51:06 +00:00
|
|
|
execvpe(program.c_str(), (char*const*)&argv[0], (char*const*)&env[0]);
|
2019-02-25 21:03:20 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
perror("Execvp failed:");
|
2019-04-03 15:31:51 +00:00
|
|
|
error("(bgshell) invoking %s failed!\n", program.c_str());
|
2019-02-25 21:03:20 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-11-03 15:31:30 +00:00
|
|
|
// Make reads from the pipe non-blocking.
|
|
|
|
int flags = fcntl(link[0], F_GETFL);
|
|
|
|
flags |= O_NONBLOCK;
|
|
|
|
fcntl(link[0], F_SETFL, flags);
|
|
|
|
|
|
|
|
*fd_out = link[0];
|
2019-02-25 21:03:20 +00:00
|
|
|
delete[] p;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-04-03 15:31:51 +00:00
|
|
|
bool stillRunning(int pid)
|
|
|
|
{
|
|
|
|
if (pid == 0) return false;
|
|
|
|
int status;
|
|
|
|
int p = waitpid(pid, &status, WNOHANG);
|
|
|
|
if (p == 0) {
|
|
|
|
// The pid has not exited yet.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (p < 0) {
|
|
|
|
// No pid to wait for.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (WIFEXITED(status)) {
|
|
|
|
// Child exited properly.
|
|
|
|
int rc = WEXITSTATUS(status);
|
|
|
|
debug("(bgshell) %d exited with return code %d\n", pid, rc);
|
|
|
|
}
|
|
|
|
else if (WIFSIGNALED(status)) {
|
|
|
|
// Child forcefully terminated
|
|
|
|
debug("(bgshell) %d terminated due to signal %d\n", pid, WTERMSIG(status));
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
// Exited for other reasons, whatever those may be.
|
|
|
|
debug("(bgshell) %d exited\n", pid);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-02-25 21:03:20 +00:00
|
|
|
void stopBackgroundShell(int pid)
|
|
|
|
{
|
|
|
|
assert(pid > 0);
|
|
|
|
|
2020-08-09 15:05:55 +00:00
|
|
|
// Sending SIGTERM to the pid will properly shut down the subshell
|
|
|
|
// and its contents.
|
2020-08-10 18:22:58 +00:00
|
|
|
// You can check process group ids for rtl_sdr|rtl_wmbus using this command,
|
|
|
|
// replace rtl with whatever it is that you start inside the subshell.
|
|
|
|
// ps -axcf -o pid,ppid,pgid,comm | grep -B 10 -A 10 rtl
|
|
|
|
debug("(shell) sending SIGTERM to process group %d\n", pid);
|
|
|
|
int rc = kill(-pid, SIGTERM);
|
2019-04-03 15:31:51 +00:00
|
|
|
if (rc < 0) {
|
2020-08-10 18:22:58 +00:00
|
|
|
debug("(bgshell) could not sigterm -%d, exited already?\n", pid);
|
2019-04-03 15:31:51 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-02-25 21:03:20 +00:00
|
|
|
// Wait for the child to finish!
|
2020-08-10 18:22:58 +00:00
|
|
|
debug("(bgshell) sent sigterm, now waiting for child %d to exit.\n", pid);
|
2019-04-03 15:31:51 +00:00
|
|
|
int status;
|
|
|
|
int p = waitpid(pid, &status, 0);
|
|
|
|
if (p < 0) {
|
|
|
|
debug("(bgshell) cannot stop pid %d, exited already?\n", pid);
|
|
|
|
return;
|
|
|
|
}
|
2019-02-25 21:03:20 +00:00
|
|
|
if (WIFEXITED(status)) {
|
|
|
|
// Child exited properly.
|
|
|
|
int rc = WEXITSTATUS(status);
|
2019-04-03 15:31:51 +00:00
|
|
|
debug("(bgshell) return code %d\n", rc);
|
2019-02-25 21:03:20 +00:00
|
|
|
if (rc != 0) {
|
2019-04-03 15:31:51 +00:00
|
|
|
warning("(bgshell) exited with non-zero return code: %d\n", rc);
|
2019-02-25 21:03:20 +00:00
|
|
|
}
|
|
|
|
}
|
2019-04-03 15:31:51 +00:00
|
|
|
if (WIFSIGNALED(status)) {
|
|
|
|
// Child forcefully terminated
|
|
|
|
debug("(bgshell) %d terminated due to signal %d\n", pid, WTERMSIG(status));
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
debug("(bgshell) %d exited\n", pid);
|
|
|
|
}
|
2019-02-25 21:03:20 +00:00
|
|
|
}
|
2020-05-30 16:45:29 +00:00
|
|
|
|
2020-12-05 10:33:47 +00:00
|
|
|
int invokeShellCaptureOutput(string program, vector<string> args, vector<string> envs, string *out, bool do_not_warn_if_fail)
|
2020-05-30 16:45:29 +00:00
|
|
|
{
|
2020-12-05 10:33:47 +00:00
|
|
|
int rc = 0;
|
2020-05-30 16:45:29 +00:00
|
|
|
int pid;
|
|
|
|
int link[2];
|
|
|
|
vector<const char*> argv(args.size()+2);
|
|
|
|
char *p = new char[program.length()+1];
|
|
|
|
strcpy(p, program.c_str());
|
|
|
|
argv[0] = p;
|
|
|
|
int i = 1;
|
|
|
|
|
|
|
|
debug("(shell) exec (capture output) \"%s\"\n", program.c_str());
|
|
|
|
for (auto &a : args) {
|
|
|
|
argv[i] = a.c_str();
|
|
|
|
i++;
|
|
|
|
debug("(shell) arg \"%s\"\n", a.c_str());
|
|
|
|
}
|
|
|
|
argv[i] = NULL;
|
|
|
|
|
|
|
|
vector<const char*> env(envs.size()+1);
|
|
|
|
env[0] = p;
|
|
|
|
i = 0;
|
|
|
|
for (auto &e : envs) {
|
|
|
|
env[i] = e.c_str();
|
|
|
|
i++;
|
|
|
|
debug("(shell) env \"%s\"\n", e.c_str());
|
|
|
|
}
|
|
|
|
env[i] = NULL;
|
|
|
|
|
|
|
|
if (pipe(link) == -1) {
|
|
|
|
error("(shell) could not create pipe!\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
pid = fork();
|
|
|
|
if (pid == 0) {
|
|
|
|
// I am the child!
|
|
|
|
// Redirect stdout and stderr to pipe
|
|
|
|
dup2 (link[1], STDOUT_FILENO);
|
|
|
|
dup2 (link[1], STDERR_FILENO);
|
|
|
|
// Close return pipe, not duped.
|
|
|
|
close(link[0]);
|
|
|
|
// Close old forward fd pipe.
|
|
|
|
close(link[1]);
|
|
|
|
close(0); // Close stdin
|
|
|
|
|
|
|
|
#if (defined(__APPLE__) && defined(__MACH__)) || defined(__FreeBSD__)
|
|
|
|
execve(program.c_str(), (char*const*)&argv[0], (char*const*)&env[0]);
|
|
|
|
#else
|
2021-11-12 14:51:06 +00:00
|
|
|
execvpe(program.c_str(), (char*const*)&argv[0], (char*const*)&env[0]);
|
2020-05-30 16:45:29 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
perror("Execvp failed:");
|
|
|
|
error("(shell) invoking %s failed!\n", program.c_str());
|
2020-12-05 10:33:47 +00:00
|
|
|
return 127;
|
2020-05-30 16:45:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
close(link[1]);
|
|
|
|
|
|
|
|
int fd_out = link[0];
|
|
|
|
delete[] p;
|
|
|
|
|
|
|
|
string data;
|
|
|
|
uchar buf[32768];
|
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
ssize_t n = read(fd_out, buf, sizeof(buf));
|
|
|
|
if (n <= 0)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
data.insert(data.end(), buf, buf+n);
|
|
|
|
}
|
|
|
|
|
|
|
|
debug("(shell) output: >>>%s<<<\n", data.c_str());
|
|
|
|
|
|
|
|
*out = data;
|
|
|
|
|
|
|
|
int status;
|
|
|
|
int pp = waitpid(pid, &status, 0);
|
|
|
|
if (pp < 0) {
|
|
|
|
debug("(shell) cannot stop pid %d, exited already?\n", pid);
|
2020-12-05 10:33:47 +00:00
|
|
|
return 127;
|
2020-05-30 16:45:29 +00:00
|
|
|
}
|
|
|
|
if (WIFEXITED(status)) {
|
|
|
|
// Child exited properly.
|
2020-12-05 10:33:47 +00:00
|
|
|
rc = WEXITSTATUS(status);
|
2020-05-30 16:45:29 +00:00
|
|
|
debug("(shell) return code %d\n", rc);
|
|
|
|
if (rc != 0) {
|
|
|
|
if (!do_not_warn_if_fail)
|
|
|
|
{
|
|
|
|
warning("(shell) exited with non-zero return code: %d\n", rc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (WIFSIGNALED(status)) {
|
|
|
|
// Child forcefully terminated
|
|
|
|
debug("(shell) %d terminated due to signal %d\n", pid, WTERMSIG(status));
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
debug("(shell) %d exited\n", pid);
|
|
|
|
}
|
|
|
|
|
2020-12-05 10:33:47 +00:00
|
|
|
return rc;
|
2020-05-30 16:45:29 +00:00
|
|
|
}
|
2020-09-18 18:05:59 +00:00
|
|
|
|
|
|
|
void detectProcesses(string cmd, vector<int> *pids)
|
|
|
|
{
|
|
|
|
vector<string> args;
|
|
|
|
vector<string> envs;
|
|
|
|
args.push_back(cmd);
|
|
|
|
string out;
|
|
|
|
invokeShellCaptureOutput("/bin/pidof", args, envs, &out, true);
|
|
|
|
|
|
|
|
char buf[out.size()+1];
|
|
|
|
strcpy(buf, out.c_str());
|
|
|
|
char *pch;
|
|
|
|
pch = strtok (buf," \n");
|
|
|
|
while (pch != NULL)
|
|
|
|
{
|
|
|
|
pids->push_back(atoi(pch));
|
|
|
|
pch = strtok (NULL, " \n");
|
|
|
|
}
|
|
|
|
}
|