-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- seperate the `SIGUSR2` related API to `sig.c` file - add logic to distinguish the origin of `SIGUSR2` signal Fixes #1529
- Loading branch information
1 parent
efc54de
commit 6bd9acf
Showing
13 changed files
with
282 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
#define _GNU_SOURCE | ||
|
||
#include "sig.h" | ||
#include "errno.h" | ||
#include "scopestdlib.h" | ||
#include "fn.h" | ||
#include "dbg.h" | ||
|
||
static timer_t g_sigTimerId = 0; | ||
static bool g_sigScopeSignalHandler = FALSE; | ||
static bool g_sigAppSignalHandler = FALSE; | ||
static struct sigaction g_sigScopeSigAction; | ||
static struct sigaction g_sigAppAction; | ||
|
||
/* | ||
* Distinguish the SIGUSR2 generated by the AppScope library | ||
*/ | ||
#define SIGTIMER_SCOPE_ID (123456789) | ||
|
||
/* | ||
* Save application handler | ||
*/ | ||
void | ||
sigSaveAppAction(const struct sigaction *act) { | ||
g_sigAppAction.sa_flags = act->sa_flags; | ||
if (g_sigAppAction.sa_flags & SA_SIGINFO) { | ||
g_sigAppAction.sa_sigaction = act->sa_sigaction; | ||
} else { | ||
g_sigAppAction.sa_handler = act->sa_handler; | ||
} | ||
|
||
g_sigAppAction.sa_handler = act->sa_handler; | ||
g_sigAppAction.sa_mask = act->sa_mask; | ||
g_sigAppSignalHandler = TRUE; | ||
} | ||
|
||
/* | ||
* Call application handler | ||
*/ | ||
void | ||
sigCallAppAction(int sig, siginfo_t *info, void *secret) { | ||
if (g_sigAppAction.sa_flags & SA_SIGINFO) { | ||
g_sigAppAction.sa_sigaction(sig, info, secret); | ||
} else { | ||
g_sigAppAction.sa_handler(sig); | ||
} | ||
} | ||
|
||
/* | ||
* Check if AppScope Handler is active | ||
*/ | ||
bool | ||
sigIsAppcopeActionActive(void) { | ||
return g_sigScopeSignalHandler; | ||
} | ||
|
||
/* | ||
* Check if Application installed signal Handler | ||
*/ | ||
bool | ||
sigIsAppActionInstalled(void) { | ||
return g_sigAppSignalHandler; | ||
} | ||
|
||
/* | ||
* Check if siginfo comes from AppScope signal | ||
*/ | ||
bool | ||
sigIsSigFromAppscopeTimer(const siginfo_t *si) { | ||
if (!si) { | ||
return FALSE; | ||
} | ||
if (si->si_signo != SIGUSR2) { | ||
return FALSE; | ||
} | ||
if (si->si_code != SI_TIMER) { | ||
return FALSE; | ||
} | ||
|
||
if (si->_sifields._timer.si_sigval.sival_int != SIGTIMER_SCOPE_ID) { | ||
return FALSE; | ||
} | ||
|
||
return TRUE; | ||
} | ||
|
||
/* | ||
* Register the signal handler for Appscope | ||
* TODO: Consolidate this with other signals used by the Appscope | ||
*/ | ||
bool | ||
sigHandlerRegister(int sigNo, void(*handler)(int, siginfo_t *, void *)) { | ||
sigemptyset(&g_sigScopeSigAction.sa_mask); | ||
g_sigScopeSigAction.sa_handler = (void (*))handler; | ||
g_sigScopeSigAction.sa_flags = SA_SIGINFO | SA_RESTART; | ||
|
||
if (!g_fn.sigaction) { | ||
return FALSE; | ||
} | ||
|
||
struct sigaction oldact = { 0 }; | ||
if (g_fn.sigaction(sigNo, &g_sigScopeSigAction, &oldact) == -1) { | ||
DBG("errno %d", errno); | ||
return FALSE; | ||
} | ||
g_sigScopeSignalHandler = TRUE; | ||
|
||
return TRUE; | ||
} | ||
|
||
/* | ||
* Starts the "signal timer" | ||
* On timer expiration will generate specific signal | ||
*/ | ||
bool | ||
sigTimerStart(int sigNo, unsigned secInterval) | ||
{ | ||
struct sigevent sevent = {0}; | ||
struct itimerspec tspec; | ||
|
||
sevent.sigev_notify = SIGEV_SIGNAL; | ||
sevent.sigev_signo = sigNo; | ||
sevent.sigev_value.sival_int = SIGTIMER_SCOPE_ID; | ||
|
||
if (timer_create(CLOCK_MONOTONIC, &sevent, &g_sigTimerId) == -1) { | ||
DBG("errno %d", errno); | ||
return FALSE; | ||
} | ||
|
||
tspec.it_interval.tv_sec = 0; | ||
tspec.it_interval.tv_nsec = 0; | ||
tspec.it_value.tv_sec = secInterval; | ||
tspec.it_value.tv_nsec = 0; | ||
if (timer_settime(g_sigTimerId, 0, &tspec, NULL) == -1) { | ||
DBG("errno %d", errno); | ||
return FALSE; | ||
} | ||
return TRUE; | ||
} | ||
|
||
/* | ||
* Stops the "signal timer" | ||
*/ | ||
bool | ||
sigTimerStop(void) { | ||
if (g_sigTimerId) { | ||
timer_delete(g_sigTimerId); | ||
g_sigTimerId = 0; | ||
return TRUE; | ||
} | ||
|
||
return FALSE; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#ifndef __SIG_H__ | ||
#define __SIG_H__ | ||
|
||
#include <signal.h> | ||
#include "scopetypes.h" | ||
|
||
bool sigIsAppcopeActionActive(void); | ||
bool sigIsAppActionInstalled(void); | ||
void sigCallAppAction(int, siginfo_t *, void *); | ||
bool sigIsSigFromAppscopeTimer(const siginfo_t *); | ||
void sigSaveAppAction(const struct sigaction *); | ||
bool sigHandlerRegister(int, void(*handler)(int, siginfo_t *, void *)); | ||
bool sigTimerStart(int, unsigned); | ||
bool sigTimerStop(void); | ||
|
||
#endif // __SIG_H__ |
Oops, something went wrong.