Skip to content

Commit

Permalink
dinitcheck: account for service working directory for commands
Browse files Browse the repository at this point in the history
  • Loading branch information
q66 committed Dec 10, 2023
1 parent d7d4d8a commit 0f9e525
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/dinitcheck.cc
Original file line number Diff line number Diff line change
Expand Up @@ -338,15 +338,18 @@ service_record *load_service(service_set_t &services, const std::string &name,
return found->second;
}

string service_wdir;
string service_filename;
ifstream service_file;
int dirfd;

int fail_load_errno = 0;
std::string fail_load_path;

// Couldn't find one. Have to load it.
for (auto &service_dir : service_dirs) {
service_filename = service_dir.get_dir();
service_wdir = service_filename;
if (*(service_filename.rbegin()) != '/') {
service_filename += '/';
}
Expand Down Expand Up @@ -429,9 +432,19 @@ service_record *load_service(service_set_t &services, const std::string &name,

settings.finalise(report_err, renvmap, report_err, resolve_var);

if (!settings.working_dir.empty()) {
service_wdir = settings.working_dir;
}
dirfd = open(service_wdir.c_str(), O_DIRECTORY | O_PATH);
if (dirfd < 0) {
report_service_description_err(name,
std::string("could not open service working directory: ") + strerror(errno));
dirfd = AT_FDCWD;
}

auto check_command = [&](const char *setting_name, const char *command) {
struct stat command_stat;
if (stat(command, &command_stat) == -1) {
if (fstatat(dirfd, command, &command_stat, 0) == -1) {
report_service_description_err(name,
std::string("could not stat ") + setting_name + " executable '" + command
+ "': " + strerror(errno));
Expand Down Expand Up @@ -460,5 +473,9 @@ service_record *load_service(service_set_t &services, const std::string &name,
settings.stop_command.substr(offset_start, offset_end - offset_start).c_str());
}

if (dirfd != AT_FDCWD) {
close(dirfd);
}

return new service_record(name, settings.chain_to_name, settings.depends, settings.before_svcs);
}

0 comments on commit 0f9e525

Please sign in to comment.