Accept udevmon configuration from multiple sources

Either from command line, configuration directory or both
This commit is contained in:
Francisco Lopes
2020-05-30 12:33:50 -03:00
parent 971e10f0f8
commit e11787ee04
3 changed files with 18 additions and 10 deletions

View File

@ -1,6 +1,7 @@
BasedOnStyle: Google BasedOnStyle: Google
Standard: Cpp11 Standard: Cpp11
SortIncludes: false SortIncludes: false
SortUsingDeclarations: false
AccessModifierOffset: -4 AccessModifierOffset: -4
PointerBindsToType: false PointerBindsToType: false
DerivePointerBinding: false DerivePointerBinding: false

View File

@ -17,7 +17,7 @@ options:
-h show this message and exit -h show this message and exit
-c configuration.yaml use configuration.yaml as configuration -c configuration.yaml use configuration.yaml as configuration
/etc/interception/udevmon.d/*.yaml is read if -c is not provided /etc/interception/udevmon.d/*.yaml is also read if present
``` ```
### intercept ### intercept

View File

@ -38,7 +38,7 @@ void print_usage(std::FILE *stream, const char *program) {
" -h show this message and exit\n" " -h show this message and exit\n"
" -c configuration.yaml use configuration.yaml as configuration\n" " -c configuration.yaml use configuration.yaml as configuration\n"
"\n" "\n"
"/etc/interception/udevmon.d/*.yaml is read if -c is not provided\n", "/etc/interception/udevmon.d/*.yaml is also read if present\n",
program); program);
// clang-format on // clang-format on
} }
@ -231,7 +231,7 @@ private:
std::strerror(errno)); std::strerror(errno));
break; break;
case 0: { case 0: {
char *command[] = {(char *)"sh", (char *)"-c", char *command[] = {(char *)"sh", (char *)"-c",
(char *)job.c_str(), nullptr}; (char *)job.c_str(), nullptr};
std::string variables = "DEVNODE=" + devnode; std::string variables = "DEVNODE=" + devnode;
char *environment[] = {(char *)variables.c_str(), nullptr}; char *environment[] = {(char *)variables.c_str(), nullptr};
@ -294,12 +294,18 @@ void kill_zombies(int /*signum*/) {
while (waitpid(-1, &status, WNOHANG) > 0) while (waitpid(-1, &status, WNOHANG) > 0)
; ;
} }
} } // namespace
int main(int argc, char *argv[]) try { int main(int argc, char *argv[]) try {
using std::perror; using std::perror;
std::vector<YAML::Node> configs; std::vector<YAML::Node> configs =
scan_config("/etc/interception/udevmon.d");
if (configs.size() > 0)
printf(
"%zu configuration files read from /etc/interception/udevmon.d\n",
configs.size());
int opt; int opt;
while ((opt = getopt(argc, argv, "hc:")) != -1) { while ((opt = getopt(argc, argv, "hc:")) != -1) {
@ -307,7 +313,11 @@ int main(int argc, char *argv[]) try {
case 'h': case 'h':
return print_usage(stdout, argv[0]), EXIT_SUCCESS; return print_usage(stdout, argv[0]), EXIT_SUCCESS;
case 'c': case 'c':
configs.push_back(YAML::LoadFile(optarg)); try {
configs.push_back(YAML::LoadFile(optarg));
} catch (const YAML::Exception &e) {
printf("ignoring %s, reason: %s\n", optarg, e.msg.c_str());
}
continue; continue;
} }
@ -315,10 +325,7 @@ int main(int argc, char *argv[]) try {
} }
if (configs.empty()) if (configs.empty())
configs = scan_config("/etc/interception/udevmon.d"); return perror("couldn't read any configuration"), EXIT_FAILURE;
if (configs.empty())
return print_usage(stderr, argv[0]), EXIT_FAILURE;
jobs_launcher launch_jobs_for_devnode(configs); jobs_launcher launch_jobs_for_devnode(configs);