remove user junkbuster after de-installation.
[privoxy.git] / amiga.c
1 const char amiga_rcs[] = "$Id: amiga.c,v 1.4 2001/10/07 15:35:13 oes Exp $";
2 /*********************************************************************
3  *
4  * File        :  $Source: /cvsroot/ijbswa/current/amiga.c,v $
5  *
6  * Purpose     :  Amiga-specific declarations.
7  *
8  * Copyright   :  Written by and Copyright (C) 2001 the SourceForge
9  *                IJBSWA team.  http://ijbswa.sourceforge.net
10  *
11  *                This program is free software; you can redistribute it 
12  *                and/or modify it under the terms of the GNU General
13  *                Public License as published by the Free Software
14  *                Foundation; either version 2 of the License, or (at
15  *                your option) any later version.
16  *
17  *                This program is distributed in the hope that it will
18  *                be useful, but WITHOUT ANY WARRANTY; without even the
19  *                implied warranty of MERCHANTABILITY or FITNESS FOR A
20  *                PARTICULAR PURPOSE.  See the GNU General Public
21  *                License for more details.
22  *
23  *                The GNU General Public License should be included with
24  *                this file.  If not, you can view it at
25  *                http://www.gnu.org/copyleft/gpl.html
26  *                or write to the Free Software Foundation, Inc., 59
27  *                Temple Place - Suite 330, Boston, MA  02111-1307, USA.
28  *
29  * Revisions   :
30  *    $Log: amiga.c,v $
31  *    Revision 1.4  2001/10/07 15:35:13  oes
32  *    Replaced 6 boolean members of csp with one bitmap (csp->flags)
33  *
34  *    Revision 1.3  2001/09/12 22:54:51  joergs
35  *    Stacksize of main thread increased.
36  *
37  *    Revision 1.2  2001/05/23 00:13:58  joergs
38  *    AmigaOS support fixed.
39  *
40  *    Revision 1.1.1.1  2001/05/15 13:58:46  oes
41  *    Initial import of version 2.9.3 source tree
42  *
43  *
44  *********************************************************************/
45 \f
46
47 #include "config.h"
48
49 #ifdef AMIGA
50
51 #include <stdio.h>
52 #include <signal.h>
53
54 #include "project.h"
55
56 const char amiga_h_rcs[] = AMIGA_H_VERSION;
57
58 unsigned long __stack = 100*1024;
59 static char ver[] = "$VER: junkbuster " __AMIGAVERSION__ " (" __AMIGADATE__ ")";
60 struct Task *main_task = NULL;
61 int childs = 0;
62
63 void serve(struct client_state *csp);
64
65 __saveds ULONG server_thread(void)
66 {
67    struct client_state *local_csp;
68    struct UserData UserData;
69    struct Task *me=FindTask(NULL);
70
71    Wait(SIGF_SINGLE);
72    local_csp=(struct client_state *)(me->tc_UserData);
73    me->tc_UserData=&UserData;
74    SocketBase=(APTR)OpenLibrary("bsdsocket.library",3);
75    if(SocketBase)
76    {
77       SetErrnoPtr(&(UserData.eno),sizeof(int));
78       local_csp->cfd=ObtainSocket(local_csp->cfd, AF_INET, SOCK_STREAM, 0);
79       if(-1!=local_csp->cfd)
80       {
81          Signal(main_task,SIGF_SINGLE);
82          serve((struct client_state *) local_csp);
83       } else {
84          local_csp->flags &= ~CSP_FLAG_ACTIVE;
85          Signal(main_task,SIGF_SINGLE);
86       }
87       CloseLibrary(SocketBase);
88    } else {
89       local_csp->flags &= ~CSP_FLAG_ACTIVE;
90       Signal(main_task,SIGF_SINGLE);
91    }
92    childs--;
93    return 0;
94 }
95
96 static BPTR olddir;
97
98 void amiga_exit(void)
99 {
100    if(SocketBase)
101    {
102       CloseLibrary(SocketBase);
103    }
104    CurrentDir(olddir);
105 }
106
107 static struct SignalSemaphore memsem;
108 static struct SignalSemaphore *memsemptr = NULL;
109 static struct UserData GlobalUserData;
110
111 void InitAmiga(void)
112 {
113    main_task = FindTask(NULL);
114    main_task->tc_UserData = &GlobalUserData;
115
116    if (((struct Library *)SysBase)->lib_Version < 39)
117    {
118       exit(RETURN_FAIL);
119    }
120
121    signal(SIGINT,SIG_IGN);
122    SocketBase = (APTR)OpenLibrary("bsdsocket.library",3);
123    if (!SocketBase)
124    {
125       fprintf(stderr, "Can't open bsdsocket.library V3+\n");
126       exit(RETURN_ERROR);
127    }
128    SetErrnoPtr(&(GlobalUserData.eno),sizeof(int));
129    InitSemaphore(&memsem);
130    memsemptr = &memsem;
131
132    olddir=CurrentDir(GetProgramDir());
133    atexit(amiga_exit);
134 }
135
136 #ifdef __GNUC__
137 #ifdef libnix
138 /* multithreadingsafe libnix replacements */
139 static void *memPool=NULL;
140
141 void *malloc (size_t s)
142 {
143    ULONG *mem;
144    LONG size = s;
145
146    if (size<=0)
147    {
148       return NULL;
149    }
150    if (!memPool)
151    {
152       if (!(memPool=CreatePool(MEMF_ANY,32*1024,8*1024)))
153       {
154          return NULL;
155       }
156    }
157    size += sizeof(ULONG) + MEM_BLOCKMASK;
158    size &= ~MEM_BLOCKMASK;
159    if (memsemptr)
160    {
161       ObtainSemaphore(memsemptr);
162    }
163    if ((mem=AllocPooled(memPool,size)))
164    {
165       *mem++=size;
166    }
167    if (memsemptr)
168    {
169       ReleaseSemaphore(memsemptr);
170    }
171    return mem;
172 }
173
174 void free (void *m)
175 {
176    ULONG *mem = m;
177
178    if(mem && memPool)
179    {
180       ULONG size=*--mem;
181
182       if (memsemptr)
183       {
184          ObtainSemaphore(memsemptr);
185       }
186       FreePooled(memPool,mem,size);
187       if (memsemptr)
188       {
189          ReleaseSemaphore(memsemptr);
190       }
191    }
192 }
193
194 void *realloc (void *old, size_t ns)
195 {
196    void *new;
197    LONG osize, *o = old;
198    LONG nsize = ns;
199
200    if (!old)
201    {
202       return malloc(nsize);
203    }
204    osize = (*(o-1)) - sizeof(ULONG);
205    if (nsize <= osize)
206    {
207       return old;
208    }
209    if ((new = malloc(nsize)))
210    {
211       ULONG *n = new;
212
213       osize >>= 2;
214       while(osize--)
215       {
216          *n++ = *o++;
217       }
218       free(old);
219    }
220    return new;
221 }
222
223 void __memCleanUp (void)
224 {
225    if (memsemptr)
226    {
227       ObtainSemaphore(memsemptr);
228    }
229    if (memPool)
230    {
231       DeletePool(memPool);
232    }
233    if (memsemptr)
234    {
235       ReleaseSemaphore(memsemptr);
236    }
237 }
238
239 #define ADD2LIST(a,b,c) asm(".stabs \"_" #b "\"," #c ",0,0,_" #a )
240 #define ADD2EXIT(a,pri) ADD2LIST(a,__EXIT_LIST__,22); \
241                         asm(".stabs \"___EXIT_LIST__\",20,0,0," #pri "+128")
242 ADD2EXIT(__memCleanUp,-50);
243 #elif !defined(ixemul)
244 #error No libnix and no ixemul!?
245 #endif /* libnix */
246 #else
247 #error Only GCC is supported, multithreading safe malloc/free required.
248 #endif /* __GNUC__ */
249
250 #endif /* def AMIGA */