proj: Implement sigaction
This commit is contained in:
@@ -25,7 +25,7 @@ main: bin/main.o
|
||||
$(CC) -o $@ $^ $(CFLAGS) -lc -lpthread
|
||||
|
||||
main_intercept: bin/main.o src/intercept.c
|
||||
$(CC) -o $@ $^ $(CFLAGS) -lc -Wl,--wrap=malloc,--wrap=free,--wrap=calloc,--wrap=realloc,--wrap=reallocarray,--wrap=getopt,--wrap=exit,--wrap=close,\
|
||||
$(CC) -o $@ $^ $(CFLAGS) -lc -Wl,--wrap=malloc,--wrap=free,--wrap=calloc,--wrap=realloc,--wrap=reallocarray,--wrap=getopt,--wrap=exit,--wrap=close,--wrap=sigaction,\
|
||||
--wrap=sem_init,--wrap=sem_open,--wrap=sem_post,--wrap=sem_wait,--wrap=sem_trywait,--wrap=sem_timedwait,--wrap=sem_getvalue,--wrap=sem_close,--wrap=sem_unlink,--wrap=sem_destroy
|
||||
|
||||
clean:
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
#include <time.h>
|
||||
#include <stdarg.h>
|
||||
#include <semaphore.h>
|
||||
#include <signal.h>
|
||||
|
||||
#define BUFFER_SIZE 256
|
||||
|
||||
@@ -28,6 +29,7 @@ static void (*__real_free)(void *);
|
||||
static int (*__real_getopt)(int, char *const [], const char *);
|
||||
static void (*__real_exit)(int);
|
||||
static int (*__real_close)(int);
|
||||
static int (*__real_sigaction)(int, const struct sigaction *, struct sigaction *);
|
||||
static int (*__real_sem_init)(sem_t *, int, unsigned int);
|
||||
static sem_t *(*__real_sem_open)(const char *, int, ...);
|
||||
static int (*__real_sem_post)(sem_t *);
|
||||
@@ -38,9 +40,9 @@ static int (*__real_sem_getvalue)(sem_t *restrict, int *restrict);
|
||||
static int (*__real_sem_close)(sem_t *);
|
||||
static int (*__real_sem_unlink)(const char *);
|
||||
static int (*__real_sem_destroy)(sem_t *);
|
||||
#define load(var, name) \
|
||||
if (((var) = dlsym(RTLD_NEXT, name)) == NULL) { \
|
||||
fprintf(stderr, "intercept: unable to load symbol '%s': %s", name, strerror(errno)); \
|
||||
#define load(name) \
|
||||
if (((__real_ ## name) = dlsym(RTLD_NEXT, #name)) == NULL) { \
|
||||
fprintf(stderr, "intercept: unable to load symbol '%s': %s", #name, strerror(errno)); \
|
||||
return; \
|
||||
}
|
||||
#define sym(name) name
|
||||
@@ -53,6 +55,7 @@ extern void __real_free(void *);
|
||||
extern int __real_getopt(int, char *const [], const char *);
|
||||
extern void __real_exit(int);
|
||||
extern int __real_close(int);
|
||||
extern int __real_sigaction(int, const struct sigaction *, struct sigaction *);
|
||||
extern int __real_sem_init(sem_t *, int, unsigned int);
|
||||
extern sem_t *__real_sem_open(const char *, int, ...);
|
||||
extern int __real_sem_post(sem_t *);
|
||||
@@ -434,24 +437,25 @@ static void fin(void) {
|
||||
static void init(void) {
|
||||
if (mode) return;
|
||||
#ifdef INTERCEPT_PRELOAD
|
||||
load(__real_malloc, "malloc");
|
||||
load(__real_calloc, "calloc");
|
||||
load(__real_realloc, "realloc");
|
||||
load(__real_reallocarray, "reallocarray");
|
||||
load(__real_free, "free");
|
||||
load(__real_getopt, "getopt");
|
||||
load(__real_exit, "exit");
|
||||
load(__real_close, "close");
|
||||
load(__real_sem_init, "sem_init");
|
||||
load(__real_sem_open, "sem_open");
|
||||
load(__real_sem_post, "sem_post");
|
||||
load(__real_sem_wait, "sem_wait");
|
||||
load(__real_sem_wait, "sem_trywait");
|
||||
load(__real_sem_wait, "sem_timedwait");
|
||||
load(__real_sem_getvalue, "sem_getvalue");
|
||||
load(__real_sem_close, "sem_close");
|
||||
load(__real_sem_unlink, "sem_unlink");
|
||||
load(__real_sem_destroy, "sem_destroy");
|
||||
load(malloc);
|
||||
load(calloc);
|
||||
load(realloc);
|
||||
load(reallocarray);
|
||||
load(free);
|
||||
load(getopt);
|
||||
load(exit);
|
||||
load(close);
|
||||
load(sigaction);
|
||||
load(sem_init);
|
||||
load(sem_open);
|
||||
load(sem_post);
|
||||
load(sem_wait);
|
||||
load(sem_trywait);
|
||||
load(sem_timedwait);
|
||||
load(sem_getvalue);
|
||||
load(sem_close);
|
||||
load(sem_unlink);
|
||||
load(sem_destroy);
|
||||
#endif
|
||||
atexit(fin);
|
||||
const char *val = getenv("INTERCEPT");
|
||||
@@ -493,7 +497,7 @@ static void init(void) {
|
||||
msg("PID:%li", getpid());
|
||||
} else if (val && strncmp(val, "tcp://", 6) == 0) {
|
||||
mode = 5;
|
||||
// TODO
|
||||
// TODO socket/tcp mode
|
||||
} else {
|
||||
mode = -1;
|
||||
fprintf(stderr, "intercept: not logging or manipulating function/system calls\n");
|
||||
@@ -624,6 +628,106 @@ int sym(close)(int fildes) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
static const char *getsigstr(int sig) {
|
||||
if (sig == SIGINT) return "SIGINT";
|
||||
if (sig == SIGILL) return "SIGILL";
|
||||
if (sig == SIGABRT) return "SIGABRT";
|
||||
if (sig == SIGFPE) return "SIGFPE";
|
||||
if (sig == SIGSEGV) return "SIGSEGV";
|
||||
if (sig == SIGTERM) return "SIGTERM";
|
||||
if (sig == SIGHUP) return "SIGHUP";
|
||||
if (sig == SIGQUIT) return "SIGQUIT";
|
||||
if (sig == SIGTRAP) return "SIGTRAP";
|
||||
if (sig == SIGKILL) return "SIGKILL";
|
||||
if (sig == SIGPIPE) return "SIGPIPE";
|
||||
if (sig == SIGALRM) return "SIGALRM";
|
||||
if (sig == SIGSTKFLT) return "SIGSTKFLT";
|
||||
if (sig == SIGPWR) return "SIGPWR";
|
||||
if (sig == SIGBUS) return "SIGBUS";
|
||||
if (sig == SIGSYS) return "SIGSYS";
|
||||
if (sig == SIGURG) return "SIGURG";
|
||||
if (sig == SIGSTOP) return "SIGSTOP";
|
||||
if (sig == SIGTSTP) return "SIGTSTP";
|
||||
if (sig == SIGCONT) return "SIGCONT";
|
||||
if (sig == SIGCHLD) return "SIGCHLD";
|
||||
if (sig == SIGTTIN) return "SIGTTIN";
|
||||
if (sig == SIGTTOU) return "SIGTTOU";
|
||||
if (sig == SIGPOLL) return "SIGPOLL";
|
||||
if (sig == SIGXFSZ) return "SIGXFSZ";
|
||||
if (sig == SIGXCPU) return "SIGXCPU";
|
||||
if (sig == SIGVTALRM) return "SIGVTALRM";
|
||||
if (sig == SIGPROF) return "SIGPROF";
|
||||
if (sig == SIGUSR1) return "SIGUSR1";
|
||||
if (sig == SIGUSR2) return "SIGUSR2";
|
||||
if (sig == SIGWINCH) return "SIGWINCH";
|
||||
return "?";
|
||||
}
|
||||
|
||||
int sym(sigaction)(int sig, const struct sigaction *restrict act, struct sigaction *restrict oact) {
|
||||
init();
|
||||
const char *sigstr = getsigstr(sig);
|
||||
if (act != NULL) {
|
||||
char *name = "sa_handler";
|
||||
void *ptr = (void *)act->sa_handler;
|
||||
if (act->sa_flags & SA_SIGINFO) {
|
||||
name = "sa_sigaction";
|
||||
ptr = (void *)act->sa_sigaction;
|
||||
}
|
||||
char flgstr[64] = "|";
|
||||
if (act->sa_flags & SA_NOCLDSTOP) strcat(flgstr, "SA_NOCLDSTOP|");
|
||||
if (act->sa_flags & SA_NOCLDWAIT) strcat(flgstr, "SA_NOCLDWAIT|");
|
||||
if (act->sa_flags & SA_SIGINFO) strcat(flgstr, "SA_SIGINFO|");
|
||||
if (act->sa_flags & ~(SA_NOCLDSTOP | SA_NOCLDWAIT | SA_SIGINFO)) strcat(flgstr, "?|");
|
||||
char maskstr[512] = "";
|
||||
for (int i = 0; i < 64; i++) {
|
||||
if (sigismember(&act->sa_mask, i) != 1)
|
||||
continue;
|
||||
if (maskstr[0] != 0) strcat(maskstr, ",");
|
||||
strcat(maskstr, getsigstr(i));
|
||||
}
|
||||
msg("sigaction(%i:%s, %p:{sa_flags: 0x%x:%s, %s: %p, sa_mask: [%s]}, %p): %p", sig, sigstr, act, act->sa_flags, flgstr, name, ptr, maskstr, oact, __builtin_return_address(0));
|
||||
} else {
|
||||
msg("sigaction(%i:%s, %p:{}, %p): %p", sig, sigstr, act, oact, __builtin_return_address(0));
|
||||
}
|
||||
if (mode >= 4) {
|
||||
char buf[BUFFER_SIZE];
|
||||
rcv(buf, sizeof(buf));
|
||||
if (strncmp(buf, "modify ", 7) == 0) {
|
||||
// TODO sigaction modify
|
||||
fprintf(stderr, "intercept: sigaction: modify command not implemented\n");
|
||||
} else if (strncmp(buf, "return ", 7) == 0) {
|
||||
// TODO sigaction return
|
||||
fprintf(stderr, "intercept: sigaction: return command not implemented\n");
|
||||
} else if_error_1_int_errno(sigaction, EINVAL)
|
||||
else if_invalid(sigaction)
|
||||
}
|
||||
const int ret = __real_sigaction(sig, act, oact);
|
||||
if (oact != NULL) {
|
||||
char *name = "sa_handler";
|
||||
void *ptr = (void *)oact->sa_handler;
|
||||
if (oact->sa_flags & SA_SIGINFO) {
|
||||
name = "sa_sigaction";
|
||||
ptr = (void *)oact->sa_sigaction;
|
||||
}
|
||||
char flgstr[64] = "|";
|
||||
if (oact->sa_flags & SA_NOCLDSTOP) strcat(flgstr, "SA_NOCLDSTOP|");
|
||||
if (oact->sa_flags & SA_NOCLDWAIT) strcat(flgstr, "SA_NOCLDWAIT|");
|
||||
if (oact->sa_flags & SA_SIGINFO) strcat(flgstr, "SA_SIGINFO|");
|
||||
if (oact->sa_flags & ~(SA_NOCLDSTOP | SA_NOCLDWAIT | SA_SIGINFO)) strcat(flgstr, "?|");
|
||||
char maskstr[512] = "";
|
||||
for (int i = 0; i < 64; i++) {
|
||||
if (sigismember(&oact->sa_mask, i) != 1)
|
||||
continue;
|
||||
if (maskstr[0] != 0) strcat(maskstr, ",");
|
||||
strcat(maskstr, getsigstr(i));
|
||||
}
|
||||
msg("return %i; errno %s; oact={sa_flags: 0x%x:%s, %s: %p, sa-mask: [%s]}", ret, strerrorname_np(errno), oact->sa_flags, flgstr, name, ptr, maskstr);
|
||||
} else {
|
||||
msg("return %i; errno %s", ret, strerrorname_np(errno));
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int sym(sem_init)(sem_t *sem, int pshared, unsigned int value) {
|
||||
init();
|
||||
msg("sem_init(%p, %i, %u): %p", sem, pshared, value, __builtin_return_address(0));
|
||||
@@ -655,7 +759,7 @@ sem_t *sym(sem_open)(const char *name, int oflag, ...) {
|
||||
mode_arg = va_arg(args, mode_t);
|
||||
value = va_arg(args, unsigned int);
|
||||
va_end(args);
|
||||
msg("sem_open(%es, 0%o:|%s, 0%03o, %u): %p", name, oflag, ostr, mode_arg, value, __builtin_return_address(0));
|
||||
msg("sem_open(%es, 0%o:%s, 0%03o, %u): %p", name, oflag, ostr, mode_arg, value, __builtin_return_address(0));
|
||||
} else {
|
||||
msg("sem_open(%es, 0%o:|%s): %p", name, oflag, ostr, __builtin_return_address(0));
|
||||
}
|
||||
@@ -710,8 +814,6 @@ sem_t *sym(sem_open)(const char *name, int oflag, ...) {
|
||||
} else {
|
||||
fprintf(stderr, "intercept: sem_open: invalid args in modify command: '%s'\n", buf + 7); \
|
||||
}
|
||||
|
||||
fprintf(stderr, "intercept: sem_open: not implemented\n");
|
||||
} else if_return_ptr_errno(sem_open)
|
||||
else if_error_8_ptr_errno(sem_open, EACCES, EEXIST, EINVAL, EMFILE, ENAMETOOLONG, ENFILE, ENOENT, ENOMEM)
|
||||
else if_invalid(sem_open)
|
||||
@@ -832,7 +934,11 @@ int sym(sem_getvalue)(sem_t *restrict sem, int *restrict value) {
|
||||
else if_invalid(sem_getvalue)
|
||||
}
|
||||
const int ret = __real_sem_getvalue(sem, value);
|
||||
if (value != NULL) {
|
||||
msg("return %i; errno %s; value=%i", ret, strerrorname_np(errno), *value);
|
||||
} else {
|
||||
msg("return %i; errno %s", ret, strerrorname_np(errno));
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user