-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
implement the signal command #39 #43
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -321,6 +321,104 @@ command_stop(GString *out, int argc, char **argv) | |
return service_run_groupv(argc, argv, out, stop_process); | ||
} | ||
|
||
static void | ||
command_signal(GString *out, int argc, char **argv) | ||
{ | ||
char *str_signal; | ||
int signal; | ||
|
||
static struct signal_mapping_s { | ||
char *name; | ||
int value; | ||
} SIGNAL [] = { | ||
#ifdef __unix__ // the base signals | ||
{ "SIGABRT", SIGABRT}, | ||
{ "SIGALRM", SIGALRM }, | ||
{ "SIGBUS", SIGBUS }, | ||
{ "SIGCHLD", SIGCHLD }, | ||
{ "SIGCONT", SIGCONT }, | ||
{ "SIGEMT", SIGEMT }, | ||
{ "SIGFPE", SIGFPE }, | ||
{ "SIGHUP", SIGHUP }, | ||
{ "SIGILL", SIGILL }, | ||
{ "SIGINFO", SIGINFO }, | ||
{ "SIGINT", SIGINT }, | ||
{ "SIGIO", SIGIO }, | ||
{ "SIGKILL", SIGKILL }, | ||
{ "SIGPIPE", SIGPIPE }, | ||
{ "SIGPROF", SIGPROF }, | ||
{ "SIGQUIT", SIGQUIT }, | ||
{ "SIGSEGV", SIGSEGV }, | ||
{ "SIGSTOP", SIGSTOP }, | ||
{ "SIGSYS", SIGSYS }, | ||
{ "SIGTERM", SIGTERM }, | ||
{ "SIGTRAP", SIGTRAP }, | ||
{ "SIGTSTP", SIGTSTP }, | ||
{ "SIGTTIN", SIGTTIN }, | ||
{ "SIGTTOU", SIGTTOU }, | ||
{ "SIGURG", SIGURG }, | ||
{ "SIGUSR1", SIGUSR1 }, | ||
{ "SIGUSR2", SIGUSR2 }, | ||
{ "SIGVTALRM", SIGVTALRM }, | ||
{ "SIGWINCH", SIGWINCH }, | ||
{ "SIGXCPU", SIGXCPU }, | ||
{ "SIGXFSZ", SIGXFSZ }, | ||
#else | ||
#error Your plateform is not supported | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I prefer to have |
||
#endif | ||
|
||
#ifdef __FreeBSD__ // freebsd specific signals | ||
{ "SIGTHR", SIGTHR }, | ||
{ "SIGLIBRT", SIGLIBRT }, | ||
#endif | ||
#ifdef __linux__ // linux specific signals | ||
{ "SIGCLD", SIGCLD }, | ||
{ "SIGIOT", SIGIOT }, | ||
{ "SIGLOST", SIGLOST }, | ||
{ "SIGPOLL", SIGPOLL }, | ||
{ "SIGPWR", SIGPWR }, | ||
{ "SIGSTKFLT", SIGSTKFLT }, | ||
{ "SIGUNUSED", SIGUNUSED }, | ||
{ NULL, 0 }, | ||
#endif | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don’t think we should put this thing here. It’s not good for code readability. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 to have a dedicated file |
||
}; | ||
|
||
errno = 0; // this should not be needed | ||
|
||
if( argc <= 0) { | ||
g_string_append_printf(out, "No signal were specified\n"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should return an error here, but I have no idea on how to do this. |
||
return; // nothing to do | ||
} | ||
|
||
str_signal = argv[0]; | ||
argv++; | ||
argc--; | ||
|
||
// first we try to convert the string to an integer | ||
signal = strtol(str_signal, NULL, 10); // TODO should we autodetect the base? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we want to allow hexadecimal signals and things like that? |
||
if (errno != 0) { // conversion failed | ||
errno = 0; | ||
// we look into the of known signals | ||
for (struct signal_mapping_s* s = SIGNAL;; s++) { | ||
if (s->name == NULL) { // we did not find any signals | ||
g_string_append_printf(out, "Unable to decode the signal %s\n", str_signal); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. send an error |
||
return; | ||
} | ||
if (0 == strcasecmp(str_signal, s->name)) { | ||
signal = s->value; | ||
break; | ||
} | ||
} | ||
} | ||
|
||
void signal_process(void *u UNUSED, struct child_info_s *ci) { | ||
kill(ci->pid, signal); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMO we should emit a message with |
||
} | ||
|
||
g_assert_nonnull(out); | ||
return service_run_groupv(argc, argv, out, signal_process); | ||
} | ||
|
||
static void | ||
command_restart(GString *out, int argc, char **argv) | ||
{ | ||
|
@@ -427,6 +525,7 @@ __resolve_command(const gchar *n) | |
{"repair", command_repair }, | ||
{"start", command_start }, | ||
{"stop", command_stop }, | ||
{"signal", command_signal }, | ||
{"restart", command_restart }, | ||
{"reload", command_reload }, | ||
{NULL, NULL} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
First, lot of copy and paste, you should use a macro
and have