Skip to content

Instantly share code, notes, and snippets.

@jaseg
Created May 2, 2014 05:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaseg/957e555ac1eaa592cc30 to your computer and use it in GitHub Desktop.
Save jaseg/957e555ac1eaa592cc30 to your computer and use it in GitHub Desktop.
How to write a signal handler in amd64 assembly
#include <time.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <limits.h>
extern int var;
extern void pew(int);
int main(int argc, char **argv){
if(signal(10, pew) == SIG_ERR){ /* sigusr1 */
fprintf(stderr, "Cannot register signal handler\n");
exit(2);
}
struct timespec st = {0, 100000}; // 100µs
for(;;){
if(nanosleep(&st, NULL))
printf("Interrupted.\n");
if(var == 1){
printf("Signal handler called.\n");
}
var = 0;
}
return 0;
}
all:
gcc -Wall -o foo main.c test.S
.text
.comm var,4
.global pew
pew:
push %rbp
movl $0x1, var
pop %rbp
ret
.data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment