posix main: some cleanup & fixes

- argv[0] can have the form ./px4-commander
- create eeprom directory
- fix fpe signal handler
This commit is contained in:
Beat Küng 2018-08-04 11:46:48 +02:00 committed by Lorenz Meier
parent 6a49d78c4b
commit 413c09e20e
1 changed files with 18 additions and 11 deletions

View File

@ -118,9 +118,18 @@ int main(int argc, char **argv)
/* Symlinks point to all commands that can be used as a client with a prefix. */ /* Symlinks point to all commands that can be used as a client with a prefix. */
const char prefix[] = PX4_BASH_PREFIX; const char prefix[] = PX4_BASH_PREFIX;
int path_length = 0;
if (argc > 0) {
/* The executed binary name could start with a path, so strip it away */
const std::string full_binary_name = argv[0];
const std::string binary_name = file_basename(full_binary_name);
if (binary_name.compare(0, strlen(prefix), prefix) == 0) {
is_client = true;
}
path_length = full_binary_name.length() - binary_name.length();
if (strstr(argv[0], prefix)) {
is_client = true;
} }
if (is_client) { if (is_client) {
@ -130,8 +139,8 @@ int main(int argc, char **argv)
return -1; return -1;
} }
/* Remove the prefix. */ /* Remove the path and prefix. */
argv[0] += strlen(prefix); argv[0] += path_length + strlen(prefix);
px4_daemon::Client client; px4_daemon::Client client;
client.generate_uuid(); client.generate_uuid();
@ -310,11 +319,9 @@ int create_dirs()
{ {
std::string current_path = pwd(); std::string current_path = pwd();
std::vector<std::string> dirs; std::vector<std::string> dirs{"log", "eeprom"};
dirs.push_back("log");
for (int i = 0; i < dirs.size(); i++) { for (const auto &dir : dirs) {
std::string dir = dirs[i];
PX4_DEBUG("mkdir: %s", dir.c_str());; PX4_DEBUG("mkdir: %s", dir.c_str());;
std::string dir_path = current_path + "/" + dir; std::string dir_path = current_path + "/" + dir;
@ -343,12 +350,12 @@ void register_sig_handler()
// SIGINT // SIGINT
struct sigaction sig_int {}; struct sigaction sig_int {};
sig_int.sa_handler = sig_int_handler; sig_int.sa_handler = sig_int_handler;
sig_int.sa_flags = 0;// not SA_RESTART!; sig_int.sa_flags = 0;// not SA_RESTART!
// SIGFPE // SIGFPE
struct sigaction sig_fpe {}; struct sigaction sig_fpe {};
sig_int.sa_handler = sig_fpe_handler; sig_fpe.sa_handler = sig_fpe_handler;
sig_int.sa_flags = 0;// not SA_RESTART!; sig_fpe.sa_flags = 0;// not SA_RESTART!
// SIGPIPE // SIGPIPE
// We want to ignore if a PIPE has been closed. // We want to ignore if a PIPE has been closed.