1
0
mirror of https://github.com/rofl0r/proxychains-ng synced 2025-01-11 00:03:12 +08:00
proxychains-ng/src/allocator_thread.c
rofl0r 25afe98b20 failed attempt to use shared memory for the ip <-> dns mapping
this is in order to get irssi, which forks for DNS lookups,
and similar programs, to work as intended.

in a previous attempt i learned that shared memory created in a
child process is not visible to the parent;
in this attempt i spin off a thread from the parent which listens
on a pipe and manages the shared memory allocation from the parent
address-space. however this doesnt work as expected:
memory allocated in the parent after the child forked is not visi-
ble to the child as well.

so what happens is: irssi starts a child process, the thread allocs
memory and hands it to the child, the child attempts to write and
segfaults. however irssi doesnt crash. since now the memory is
already allocated, doing the dns lookup again will succeed.

i.e. the dns lookup works now in irssi by luck.
all but the first dns lookups will suceed.

however this is not good enough for me to be satisfied, i commit
this only for documentation purposes.
2012-11-07 16:49:14 +01:00

127 lines
2.7 KiB
C

#include <pthread.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/select.h>
#include <assert.h>
#include "shm.h"
#include "debug.h"
enum at_msgtype {
ATM_REALLOC,
ATM_STRINGDUMP,
ATM_EXIT,
};
static pthread_t allocator_thread;
static pthread_attr_t allocator_thread_attr;
static int req_pipefd[2];
static int resp_pipefd[2];
static size_t *at_oldsize;
static size_t *at_newsize;
static void **at_data;
struct stringpool mem;
static void* threadfunc(void* x) {
(void) x;
int readfd = req_pipefd[0];
int writefd = resp_pipefd[1];
fd_set fds;
FD_ZERO(&fds);
FD_SET(readfd, &fds);
int ret;
int msg;
while((ret = select(readfd+1, &fds, NULL, NULL, NULL)) != -1) {
assert(ret);
if(read(readfd, &msg, sizeof(int)) != sizeof(int)) {
perror("read");
} else {
void *nu;
switch(msg) {
case ATM_REALLOC:
nu = shm_realloc(*at_data, *at_oldsize, *at_newsize);
break;
case ATM_STRINGDUMP:
nu = stringpool_add(&mem, *at_data, *at_newsize);
break;
case ATM_EXIT:
return 0;
default:
abort();
}
*at_data = nu;
write(writefd, &msg, sizeof(int));
}
}
return 0;
}
static void initpipe(int* fds) {
if(pipe2(fds, 0/*O_CLOEXEC*/) == -1) {
perror("pipe");
exit(1);
}
}
/* initialize with pointers to shared memory. these will
* be used to place responses and arguments */
void at_init(void **data, size_t *oldsize, size_t *newsize) {
PFUNC();
initpipe(req_pipefd);
initpipe(resp_pipefd);
at_oldsize = oldsize;
at_newsize = newsize;
at_data = data;
stringpool_init(&mem);
pthread_attr_init(&allocator_thread_attr);
pthread_attr_setstacksize(&allocator_thread_attr, 16 * 1024);
pthread_create(&allocator_thread, &allocator_thread_attr, threadfunc, 0);
}
void at_close(void) {
PFUNC();
const int msg = ATM_EXIT;
write(req_pipefd[1], &msg, sizeof(int));
pthread_join(allocator_thread, NULL);
close(req_pipefd[0]);
close(req_pipefd[1]);
close(resp_pipefd[0]);
close(resp_pipefd[1]);
}
static int wait_reply(void) {
PFUNC();
int readfd = resp_pipefd[0];
fd_set fds;
FD_ZERO(&fds);
FD_SET(readfd, &fds);
int ret;
while((ret = select(readfd+1, &fds, NULL, NULL, NULL)) <= 0) {
if(ret < 0) perror("select2");
}
read(readfd, &ret, sizeof(int));
return ret;
}
void *at_realloc(void* old, size_t oldsize, size_t newsize) {
PFUNC();
*at_data = old;
*at_oldsize = oldsize;
*at_newsize = newsize;
const int msg = ATM_REALLOC;
write(req_pipefd[1], &msg, sizeof(int));
assert(wait_reply() == msg);
return *at_data;
}
char *at_dumpstring(char* s, size_t len) {
PFUNC();
*at_data = s;
*at_newsize = len;
const int msg = ATM_STRINGDUMP;
write(req_pipefd[1], &msg, sizeof(int));
assert(wait_reply() == msg);
return *at_data;
}