Skip to content

Instantly share code, notes, and snippets.

@ayosec
Created January 27, 2012 12:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ayosec/1688585 to your computer and use it in GitHub Desktop.
Save ayosec/1688585 to your computer and use it in GitHub Desktop.
Wrap ioctls
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <termios.h>
/*#include <sys/ioctl.h>*/
#include <assert.h>
#include <dlfcn.h>
#include <unistd.h>
/* Avoid load the sys/ioctl.h header since it will conflict with our ioctl() definition */
#define TIOCMGET 0x5415
typedef int (*type_ioctl)(int __fd, unsigned long int __request, int *x);
int ioctl (int __fd, unsigned long int __request, int *x) {
static type_ioctl original_ioctl = NULL;
if(original_ioctl == NULL) {
void* libc = dlopen("libc.so.6", RTLD_LAZY);
original_ioctl = (type_ioctl)dlsym(libc, "ioctl");
dlclose(libc);
}
if(__request == TIOCMGET) {
*x = 448;
return 0;
}
return original_ioctl(__fd, __request, x);
}
all: fakeioctl.so
CFLAGS=-D_GNU_SOURCE -g -Wall
fakeioctl.so: fakeioctl.c
gcc $(CFLAGS) -fPIC -shared -ldl -o fakeioctl.so fakeioctl.c
.PHONY: all clean
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment