Skip to content

Instantly share code, notes, and snippets.

@staybuzz
Created November 12, 2015 05:03
Show Gist options
  • Save staybuzz/9068559db706e89c7bb7 to your computer and use it in GitHub Desktop.
Save staybuzz/9068559db706e89c7bb7 to your computer and use it in GitHub Desktop.
シグナルハンドラとexecveを使ってみる
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
static int *p = NULL;
static void execute_command(){
char *argv[] = {"/bin/sh", "-c", "'date'", NULL};
execve(argv[0], argv, NULL);
}
static void print_message(int x){ /* シグナルハンドラ */
printf("Baz\n");
execute_command();
exit(1);
}
int main(void){
struct sigaction sa;
sa.sa_handler = print_message;
sigemptyset(&sa.sa_mask); /* おなじない */
sa.sa_flags = 0; /* おまじない */
sigaction(SIGSEGV, &sa, NULL); /* シグナルハンドラの登録 */
printf("Foo\n");
*p = 99;
printf("Bar\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment