summaryrefslogtreecommitdiff
path: root/src/lib/evil/evil_main.c
blob: 803e47fc3024c74f3c519b9a4d78091cafb311c4 (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
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <stdio.h>

#include "evil_private.h"


static int      _evil_init_count = 0;

extern LONGLONG _evil_time_freq;
extern LONGLONG _evil_time_count;

extern DWORD    _evil_tls_index;

int
evil_init(void)
{
   LARGE_INTEGER freq;
   LARGE_INTEGER count;

   if (++_evil_init_count != 1)
     return _evil_init_count;

   QueryPerformanceFrequency(&freq);

   _evil_time_freq = freq.QuadPart;

   QueryPerformanceCounter(&count);

   _evil_time_count = count.QuadPart;

   if (!evil_sockets_init())
     return --_evil_init_count;

   return _evil_init_count;
}

int
evil_shutdown(void)
{
   /* _evil_init_count should not go below zero. */
   if (_evil_init_count < 1)
     {
        printf("Evil shutdown called without calling evil init.\n");
        return 0;
     }

   if (--_evil_init_count != 0)
     return _evil_init_count;

   evil_sockets_shutdown();

   return _evil_init_count;
}

BOOL WINAPI DllMain(HINSTANCE inst, DWORD reason, LPVOID reserved)
{
   LPVOID data;

   switch (reason)
     {
     case DLL_PROCESS_ATTACH:
       _evil_tls_index = TlsAlloc();
       if (_evil_tls_index == TLS_OUT_OF_INDEXES)
         return FALSE;
       /* No break: Initialize the index for first thread. */
       /* fall through */
     case DLL_THREAD_ATTACH:
       data = (LPVOID)LocalAlloc(LPTR, 4096);
       if (!data)
         return FALSE;
       if (!TlsSetValue(_evil_tls_index, data))
         return FALSE;
       break;
     case DLL_THREAD_DETACH:
       data = TlsGetValue(_evil_tls_index);
       if (data)
         LocalFree((HLOCAL)data);
       break;
     case DLL_PROCESS_DETACH:
       data = TlsGetValue(_evil_tls_index);
       if (data)
         LocalFree((HLOCAL)data);
       TlsFree(_evil_tls_index);
       break;
     default:
       break;
     }

   return TRUE;

   (void)inst;
   (void)reserved;
}