Created
May 2, 2014 05:43
-
-
Save jaseg/957e555ac1eaa592cc30 to your computer and use it in GitHub Desktop.
How to write a signal handler in amd64 assembly
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
#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; | |
} |
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
all: | |
gcc -Wall -o foo main.c test.S |
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
.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