Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

add signal handler to adu-shell for restart race #699

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/adu-shell/inc/adushell.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,16 @@ typedef struct tagADUShell_LaunchArguments
} ADUShell_LaunchArguments;

/**
* @brief A exit code from an ADUShell child process.
* @brief An exit code from an ADUShell child process.
* We're using EXIT_SUCCESS (0) for success case and EXIT_FAILURE (1) for general errors.
*/
#define ADUSHELL_EXIT_UNSUPPORTED 3

/**
* @brief An exit code for early exit of adu-shell process due to SIGTERM/SIGINT signal handling.
*/
#define ADUSHELL_EXIT_SIGNAL_HANDLING 4

/**
* @brief A result from ADUShell task.
*/
Expand Down
29 changes: 28 additions & 1 deletion src/adu-shell/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Licensed under the MIT License.
*/
#include <getopt.h>

#include <signal.h> // signal
#include <string.h>

#include <aducpal/grp.h> // getgrnam
Expand Down Expand Up @@ -43,6 +43,11 @@ namespace ScriptTasks = Adu::Shell::Tasks::Script;

namespace adushconst = Adu::Shell::Const;

/**
* @brief Indicates whether a task is in progress or not. SIGTERM/SIGINT signal handling should not exit process if true.
*/
static bool s_task_in_progress = false;

/**
* @brief Parse command-line arguments.
* @param argc arguments count.
Expand Down Expand Up @@ -301,6 +306,24 @@ bool ADUShell_PermissionCheck()
return isTrusted;
}

/**
* @brief Called when a terminate (SIGINT, SIGTERM) signal is detected.
*
* @param sig Signal value.
*/
void OnSignal(int sig)
{
if (s_task_in_progress)
{
Log_Warn("Task already in progress. Ignoring SIG: %d", sig);
return;
}

// Assume handling SIGINT or SIGTERM at this point.
Log_Warn("Exit process for SIG: %d", sig);
exit(ADUSHELL_EXIT_SIGNAL_HANDLING);
}

/**
* @brief Main method.
*
Expand Down Expand Up @@ -333,6 +356,9 @@ int main(int argc, char** argv)
goto done;
}

signal(SIGINT, OnSignal);
signal(SIGTERM, OnSignal);

ADUCPAL_setenv(ADUC_CONFIG_FOLDER_ENV, launchArgs.configFolder, 1);
config = ADUC_ConfigInfo_GetInstance();
if (config == NULL)
Expand Down Expand Up @@ -371,6 +397,7 @@ int main(int argc, char** argv)
effectiveUserId,
ADUCPAL_getegid());

s_task_in_progress = true;
ret = ADUShell_Dowork(launchArgs);

ADUC_Logging_Uninit();
Expand Down