summary refs log tree commit diff stats
path: root/plugins/dns/thread.c
blob: aa153137b9e55f929f4b88f39863eb7756b75201 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <stdlib.h>

#define USE_PTHREAD

#ifdef WIN32

#include <windows.h>
#define pthread_t DWORD
#define pipe(a) _pipe(a,4096,_O_BINARY)

#else
#ifdef USE_PTHREAD

#include <pthread.h>

#else

#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#define pthread_t int

#endif
#endif


typedef struct
{
	pthread_t threadid;
	int pipe_fd[2];
	void *userdata;
} thread;

thread *
thread_new (void)
{
	thread *th;

	th = calloc (1, sizeof (*th));
	if (!th)
		return NULL;

	if (pipe (th->pipe_fd) == -1)
	{
		free (th);
		return NULL;
	}

#ifdef __EMX__ /* os/2 */
	setmode (pipe_fd[0], O_BINARY);
	setmode (pipe_fd[1], O_BINARY);
#endif

	return th;
}

int
thread_start (thread *th, void *(*start_routine)(void *), void *arg)
{
	pthread_t id;

#ifdef WIN32
	CloseHandle (CreateThread (NULL, 0,
										(LPTHREAD_START_ROUTINE)start_routine,
										arg, 0, (DWORD *)&id));
#else
#ifdef USE_PTHREAD
	if (pthread_create (&id, NULL, start_routine, arg) != 0)
		return 0;
#else
	switch (id = fork ())
	{
	case -1:
		return 0;
	case 0:
		/* this is the child */
		setuid (getuid ());
		start_routine (arg);
		_exit (0);
	}
#endif
#endif

	th->threadid = id;

	return 1;
}

/*void
thread_kill (thread *th)
{
#ifdef WIN32
	PostThreadMessage (th->threadid, WM_QUIT, 0, 0);
#else
#ifdef USE_PTHREAD
	pthread_cancel (th->threadid);
	pthread_join (th->threadid, (void *)&th);
#else
	kill (th->threadid, SIGKILL);
	waitpid (th->threadid, NULL, 0);
#endif
#endif
}

void
thread_free (thread *th)
{
	close (th->pipe_fd[0]);
	close (th->pipe_fd[1]);
	free (th);
}*/