-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
217 additions
and
0 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#include <sys/epoll.h> | ||
|
||
#include <re.h> | ||
#include "test.h" | ||
|
||
|
||
int __wrap_epoll_create(int size); | ||
int __real_epoll_create(int size); | ||
|
||
int __wrap_epoll_create(int size) | ||
{ | ||
int ret; | ||
|
||
int err = wrap_return_int("epoll_create", &ret); | ||
|
||
return err ? __real_epoll_create(size) : ret; | ||
} | ||
|
||
|
||
int __wrap_epoll_ctl(int epfd, int op, int fd, struct epoll_event *event); | ||
int __real_epoll_ctl(int epfd, int op, int fd, struct epoll_event *event); | ||
|
||
int __wrap_epoll_ctl(int epfd, int op, int fd, struct epoll_event *event) | ||
{ | ||
int ret; | ||
|
||
int err = wrap_return_int("epoll_create", &ret); | ||
|
||
return err ? __real_epoll_ctl(epfd, op, fd, event) : ret; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#include <netdb.h> | ||
|
||
#include <re.h> | ||
#include "test.h" | ||
|
||
|
||
int __wrap_getaddrinfo(const char *restrict node, const char *restrict service, | ||
const struct addrinfo *restrict hints, | ||
struct addrinfo **restrict res); | ||
int __real_getaddrinfo(const char *restrict node, const char *restrict service, | ||
const struct addrinfo *restrict hints, | ||
struct addrinfo **restrict res); | ||
int __wrap_getaddrinfo(const char *restrict node, const char *restrict service, | ||
const struct addrinfo *restrict hints, | ||
struct addrinfo **restrict res) | ||
{ | ||
return __real_getaddrinfo(node, service, hints, res); | ||
} | ||
|
||
|
||
void __wrap_freeaddrinfo(struct addrinfo *res); | ||
void __real_freeaddrinfo(struct addrinfo *res); | ||
void __wrap_freeaddrinfo(struct addrinfo *res) | ||
{ | ||
__real_freeaddrinfo(res); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
#include <re.h> | ||
#include "test.h" | ||
|
||
enum wrap_type { | ||
WRAP_INT, | ||
WRAP_DATA | ||
}; | ||
|
||
struct wrap_entry { | ||
struct le le; | ||
char func[64]; | ||
enum wrap_type type; | ||
union { | ||
int ret_int; | ||
void *ret_data; | ||
} u; | ||
}; | ||
|
||
static struct list wrapl = LIST_INIT; | ||
|
||
|
||
static void entry_destruct(void *arg) | ||
{ | ||
struct wrap_entry *e = arg; | ||
list_unlink(&e->le); | ||
} | ||
|
||
|
||
static struct wrap_entry *wrap_entry_alloc(const char *func) | ||
{ | ||
struct wrap_entry *e; | ||
|
||
e = mem_alloc(sizeof(struct wrap_entry), entry_destruct); | ||
if (!e) | ||
return NULL; | ||
|
||
str_ncpy(e->func, func, sizeof(e->func)); | ||
|
||
list_append(&wrapl, &e->le, e); | ||
|
||
return e; | ||
} | ||
|
||
|
||
int wrap_will_return_int(const char *func, int ret) | ||
{ | ||
struct wrap_entry *e; | ||
|
||
if (!func) | ||
return EINVAL; | ||
|
||
e = wrap_entry_alloc(func); | ||
if (!e) | ||
return ENOMEM; | ||
|
||
e->type = WRAP_INT; | ||
e->u.ret_int = ret; | ||
|
||
return 0; | ||
} | ||
|
||
|
||
int wrap_will_return_data(const char *func, void *data) | ||
{ | ||
struct wrap_entry *e; | ||
|
||
if (!func) | ||
return EINVAL; | ||
|
||
e = wrap_entry_alloc(func); | ||
if (!e) | ||
return ENOMEM; | ||
|
||
e->type = WRAP_DATA; | ||
e->u.ret_data = data; | ||
|
||
return 0; | ||
} | ||
|
||
|
||
int wrap_return_int(const char *func, int *ret) | ||
{ | ||
struct le *le; | ||
|
||
if (!func) | ||
return EINVAL; | ||
|
||
LIST_FOREACH(&wrapl, le) | ||
{ | ||
struct wrap_entry *e = le->data; | ||
|
||
if (e->type != WRAP_INT) | ||
continue; | ||
|
||
if (0 == str_casecmp(e->func, func)) { | ||
*ret = e->u.ret_int; | ||
mem_deref(e); | ||
return 0; | ||
} | ||
} | ||
|
||
return ENODATA; | ||
} | ||
|
||
|
||
void *wrap_return_data(const char *func) | ||
{ | ||
struct le *le; | ||
void *data; | ||
|
||
if (!func) | ||
return NULL; | ||
|
||
LIST_FOREACH(&wrapl, le) | ||
{ | ||
struct wrap_entry *e = le->data; | ||
|
||
if (e->type != WRAP_DATA) | ||
continue; | ||
|
||
if (0 == str_casecmp(e->func, func)) { | ||
data = e->u.ret_data; | ||
mem_deref(e); | ||
return data; | ||
} | ||
} | ||
|
||
return NULL; | ||
} |