Skip to content

Instantly share code, notes, and snippets.

@nrdmn
Created January 26, 2019 12:27
Show Gist options
  • Save nrdmn/3cb7154f3a2f3c63f407a749cef62047 to your computer and use it in GitHub Desktop.
Save nrdmn/3cb7154f3a2f3c63f407a749cef62047 to your computer and use it in GitHub Desktop.
SIGTERM me!
#include <inttypes.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
char *name;
void term(int signum)
{
if (name[0] != 'T') {
strncpy(name, "This process has been SIGTERM'ed once!", 51);
} else {
uint32_t count = 2;
if (name[33] != 'o') {
sscanf(name+33, "%" SCNu32, &count);
count++;
}
snprintf(name+33, 18, "%" SCNu32 " times!", count);
}
}
int main(int argc, char **argv)
{
if (getppid() != 1) {
if (fork() == 0) {
char path[256];
ssize_t len;
if ((len = readlink("/proc/self/exe", path, sizeof(path)-1)) != -1) {
path[len] = 0;
execl(path, "SIGTERM me! ", NULL);
}
}
return 0;
}
name = argv[0];
struct sigaction action;
memset(&action, 0, sizeof(struct sigaction));
action.sa_handler = term;
sigaction(SIGTERM, &action, NULL);
for (;;) {
sleep(1);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment