Removed/replaced deprecated commands (execute/schedule/schedule_remove) and removed global lock in ExecFile.

This commit is contained in:
Jari Sundell
2025-05-05 09:55:38 +02:00
committed by GitHub
parent 55615abe9e
commit 9d5b769d78
8 changed files with 121 additions and 218 deletions
+25 -38
View File
@@ -1,9 +1,9 @@
#include "config.h"
#include <cerrno>
#include <fcntl.h>
#include <string>
#include <unistd.h>
#include <rak/error_number.h>
#include <rak/path.h>
#include <sys/types.h>
#include <sys/wait.h>
@@ -14,32 +14,24 @@
namespace rpc {
const unsigned int ExecFile::max_args;
const unsigned int ExecFile::buffer_size;
const int ExecFile::flag_expand_tilde;
const int ExecFile::flag_throw;
const int ExecFile::flag_capture;
const int ExecFile::flag_background;
// Close m_logFd.
// TODO: Access fd through torrent logging?
int
ExecFile::execute(const char* file, char* const* argv, int flags) {
// Write the execued command and its parameters to the log fd.
int __UNUSED result;
if (m_logFd != -1) {
if (m_log_fd != -1) {
for (char* const* itr = argv; *itr != NULL; itr++) {
if (itr == argv)
result = write(m_logFd, "\n---\n", sizeof("\n---\n"));
result = write(m_log_fd, "\n---\n", sizeof("\n---\n"));
else
result = write(m_logFd, " ", 1);
result = write(m_log_fd, " ", 1);
result = write(m_logFd, *itr, std::strlen(*itr));
result = write(m_log_fd, *itr, std::strlen(*itr));
}
result = write(m_logFd, "\n---\n", sizeof("\n---\n"));
result = write(m_log_fd, "\n---\n", sizeof("\n---\n"));
}
int pipeFd[2];
@@ -60,17 +52,18 @@ ExecFile::execute(const char* file, char* const* argv, int flags) {
_exit(-1);
if (detached_pid != 0) {
if (m_logFd != -1)
result = write(m_logFd, "\n--- Background task ---\n", sizeof("\n--- Background task ---\n"));
if (m_log_fd != -1)
result = write(m_log_fd, "\n--- Background task ---\n", sizeof("\n--- Background task ---\n"));
_exit(0);
}
m_logFd = -1;
m_log_fd = -1;
flags &= ~flag_capture;
}
int devNull = open("/dev/null", O_RDWR);
if (devNull != -1)
dup2(devNull, 0);
else
@@ -78,15 +71,15 @@ ExecFile::execute(const char* file, char* const* argv, int flags) {
if (flags & flag_capture)
dup2(pipeFd[1], 1);
else if (m_logFd != -1)
dup2(m_logFd, 1);
else if (m_log_fd != -1)
dup2(m_log_fd, 1);
else if (devNull != -1)
dup2(devNull, 1);
else
::close(1);
if (m_logFd != -1)
dup2(m_logFd, 2);
if (m_log_fd != -1)
dup2(m_log_fd, 2);
else if (devNull != -1)
dup2(devNull, 2);
else
@@ -101,10 +94,6 @@ ExecFile::execute(const char* file, char* const* argv, int flags) {
_exit(result);
}
// We yield the global lock when waiting for the executed command to
// finish so that XMLRPC and other threads can continue working.
torrent::utils::Thread::release_global_lock();
if (flags & flag_capture) {
m_capture = std::string();
::close(pipeFd[1]);
@@ -121,9 +110,9 @@ ExecFile::execute(const char* file, char* const* argv, int flags) {
::close(pipeFd[0]);
if (m_logFd != -1) {
result = write(m_logFd, "Captured output:\n", sizeof("Captured output:\n"));
result = write(m_logFd, m_capture.data(), m_capture.length());
if (m_log_fd != -1) {
result = write(m_log_fd, "Captured output:\n", sizeof("Captured output:\n"));
result = write(m_log_fd, m_capture.data(), m_capture.length());
}
}
@@ -132,19 +121,17 @@ ExecFile::execute(const char* file, char* const* argv, int flags) {
do {
wpid = waitpid(childPid, &status, 0);
} while (wpid == -1 && rak::error_number::current().value() == rak::error_number::e_intr);
torrent::utils::Thread::acquire_global_lock();
} while (wpid == -1 && WIFEXITED(status) == 0);
if (wpid != childPid)
throw torrent::internal_error("ExecFile::execute(...) waitpid failed.");
// Check return value?
if (m_logFd != -1) {
if (m_log_fd != -1) {
if (status == 0)
result = write(m_logFd, "\n--- Success ---\n", sizeof("\n--- Success ---\n"));
result = write(m_log_fd, "\n--- Success ---\n", sizeof("\n--- Success ---\n"));
else
result = write(m_logFd, "\n--- Error ---\n", sizeof("\n--- Error ---\n"));
result = write(m_log_fd, "\n--- Error ---\n", sizeof("\n--- Error ---\n"));
}
return status;
@@ -156,7 +143,7 @@ ExecFile::execute_object(const torrent::Object& rawArgs, int flags) {
char** argsCurrent = argsBuffer;
// Size of value strings are less than 24.
char valueBuffer[buffer_size];
char valueBuffer[buffer_size+1];
char* valueCurrent = valueBuffer;
if (rawArgs.is_list()) {
@@ -178,12 +165,12 @@ ExecFile::execute_object(const torrent::Object& rawArgs, int flags) {
if (valueCurrent >= valueBuffer + buffer_size)
throw torrent::input_error("Overflowed execute arg buffer.");
}
}
}
} else {
const torrent::Object::string_type& args = rawArgs.as_string();
if ((flags & flag_expand_tilde) && args.c_str()[0] == '~') {
*argsCurrent = valueCurrent;
valueCurrent = print_object(valueCurrent, valueBuffer + buffer_size, &rawArgs, flags) + 1;