Update a log message I missed in 1.80.
[privoxy.git] / amiga.c
1 const char amiga_rcs[] = "$Id: amiga.c,v 1.11 2006/07/18 14:48:45 david__schmidt 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  *                Privoxy team. http://www.privoxy.org/
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.11  2006/07/18 14:48:45  david__schmidt
32  *    Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
33  *    with what was really the latest development (the v_3_0_branch branch)
34  *
35  *    Revision 1.9  2002/03/26 22:29:54  swa
36  *    we have a new homepage!
37  *
38  *    Revision 1.8  2002/03/25 19:32:15  joergs
39  *    Name in version string changed from junkbuster to Privoxy.
40  *
41  *    Revision 1.7  2002/03/24 13:25:43  swa
42  *    name change related issues
43  *
44  *    Revision 1.6  2002/03/09 20:03:52  jongfoster
45  *    - Making various functions return int rather than size_t.
46  *      (Undoing a recent change).  Since size_t is unsigned on
47  *      Windows, functions like read_socket that return -1 on
48  *      error cannot return a size_t.
49  *
50  *      THIS WAS A MAJOR BUG - it caused frequent, unpredictable
51  *      crashes, and also frequently caused JB to jump to 100%
52  *      CPU and stay there.  (Because it thought it had just
53  *      read ((unsigned)-1) == 4Gb of data...)
54  *
55  *    - The signature of write_socket has changed, it now simply
56  *      returns success=0/failure=nonzero.
57  *
58  *    - Trying to get rid of a few warnings --with-debug on
59  *      Windows, I've introduced a new type "jb_socket".  This is
60  *      used for the socket file descriptors.  On Windows, this
61  *      is SOCKET (a typedef for unsigned).  Everywhere else, it's
62  *      an int.  The error value can't be -1 any more, so it's
63  *      now JB_INVALID_SOCKET (which is -1 on UNIX, and in
64  *      Windows it maps to the #define INVALID_SOCKET.)
65  *
66  *    - The signature of bind_port has changed.
67  *
68  *    Revision 1.5  2002/03/03 09:18:03  joergs
69  *    Made jumbjuster work on AmigaOS again.
70  *
71  *    Revision 1.4  2001/10/07 15:35:13  oes
72  *    Replaced 6 boolean members of csp with one bitmap (csp->flags)
73  *
74  *    Revision 1.3  2001/09/12 22:54:51  joergs
75  *    Stacksize of main thread increased.
76  *
77  *    Revision 1.2  2001/05/23 00:13:58  joergs
78  *    AmigaOS support fixed.
79  *
80  *    Revision 1.1.1.1  2001/05/15 13:58:46  oes
81  *    Initial import of version 2.9.3 source tree
82  *
83  *
84  *********************************************************************/
85 \f
86
87 #include "config.h"
88
89 #ifdef AMIGA
90
91 #include <stdio.h>
92 #include <signal.h>
93
94 #include "project.h"
95
96 const char amiga_h_rcs[] = AMIGA_H_VERSION;
97
98 static char *ver USED = "$VER: Privoxy " __AMIGAVERSION__ " (" __AMIGADATE__ ")";
99 #ifdef __amigaos4__
100 static char *stack USED = "$STACK: 524288";
101 #else
102 unsigned long __stack = 100*1024;
103 #endif
104 struct Task *main_task = NULL;
105 int childs = 0;
106
107 void serve(struct client_state *csp);
108
109 SAVEDS ULONG server_thread(void)
110 {
111    struct client_state *local_csp;
112    struct UserData UserData;
113    struct Task *me=FindTask(NULL);
114 #ifdef __amigaos4__
115    struct Library *SocketBase;
116 #endif
117
118    Wait(SIGF_SINGLE);
119    local_csp=(struct client_state *)(me->tc_UserData);
120    me->tc_UserData=&UserData;
121    SocketBase=(APTR)OpenLibrary("bsdsocket.library",3);
122    if (SocketBase)
123 #ifdef __amigaos4__
124    {
125       ISocket = (struct SocketIFace *)GetInterface(SocketBase, "main", 1, NULL);
126    }
127    if (ISocket)
128 #endif
129    {
130       SetErrnoPtr(&(UserData.eno),sizeof(int));
131       local_csp->cfd=ObtainSocket(local_csp->cfd, AF_INET, SOCK_STREAM, 0);
132       if(JB_INVALID_SOCKET!=local_csp->cfd)
133       {
134          Signal(main_task,SIGF_SINGLE);
135          serve((struct client_state *) local_csp);
136       } else {
137          local_csp->flags &= ~CSP_FLAG_ACTIVE;
138          Signal(main_task,SIGF_SINGLE);
139       }
140 #ifdef __amigaos4__
141       DropInterface((struct Interface *)ISocket);
142 #endif
143       CloseLibrary(SocketBase);
144    } else {
145 #ifdef __amigaos4__
146       CloseLibrary(SocketBase);
147 #endif
148       local_csp->flags &= ~CSP_FLAG_ACTIVE;
149       Signal(main_task,SIGF_SINGLE);
150    }
151    childs--;
152    return 0;
153 }
154
155 static BPTR olddir;
156
157 void amiga_exit(void)
158 {
159 #ifdef __amigaos4__
160    if (ISocket)
161 #else
162    if (SocketBase)
163 #endif
164    {
165 #ifdef __amigaos4__
166       struct Library *SocketBase = ISocket->Data.LibBase;
167       DropInterface((struct Interface *)ISocket);
168 #endif
169       CloseLibrary(SocketBase);
170    }
171    CurrentDir(olddir);
172 }
173
174 #ifndef __amigaos4__
175 static struct SignalSemaphore memsem;
176 static struct SignalSemaphore *memsemptr = NULL;
177 #endif
178 static struct UserData GlobalUserData;
179
180 void InitAmiga(void)
181 {
182 #ifdef __amigaos4__
183    struct Library *SocketBase;
184 #endif
185
186    main_task = FindTask(NULL);
187    main_task->tc_UserData = &GlobalUserData;
188
189    if (((struct Library *)SysBase)->lib_Version < 39)
190    {
191       exit(RETURN_FAIL);
192    }
193
194    signal(SIGINT,SIG_IGN);
195    SocketBase = (APTR)OpenLibrary("bsdsocket.library",3);
196 #ifdef __amigaos4__
197    if (SocketBase)
198    {
199       ISocket = (struct SocketIFace *)GetInterface(SocketBase, "main", 1, NULL);
200    }
201    if (!ISocket)
202 #else
203    if (!SocketBase)
204 #endif
205    {
206 #ifdef __amigaos4__
207       CloseLibrary(SocketBase);
208 #endif
209       fprintf(stderr, "Can't open bsdsocket.library V3+\n");
210       exit(RETURN_ERROR);
211    }
212    SetErrnoPtr(&(GlobalUserData.eno),sizeof(int));
213 #ifndef __amigaos4__
214    InitSemaphore(&memsem);
215    memsemptr = &memsem;
216 #endif
217
218    olddir=CurrentDir(GetProgramDir());
219    atexit(amiga_exit);
220 }
221
222 #ifndef __amigaos4__
223 #ifdef __GNUC__
224 #ifdef libnix
225 /* multithreadingsafe libnix replacements */
226 static void *memPool=NULL;
227
228 void *malloc (size_t s)
229 {
230    ULONG *mem;
231    LONG size = s;
232
233    if (size<=0)
234    {
235       return NULL;
236    }
237    if (!memPool)
238    {
239       if (!(memPool=CreatePool(MEMF_ANY,32*1024,8*1024)))
240       {
241          return NULL;
242       }
243    }
244    size += sizeof(ULONG) + MEM_BLOCKMASK;
245    size &= ~MEM_BLOCKMASK;
246    if (memsemptr)
247    {
248       ObtainSemaphore(memsemptr);
249    }
250    if ((mem=AllocPooled(memPool,size)))
251    {
252       *mem++=size;
253    }
254    if (memsemptr)
255    {
256       ReleaseSemaphore(memsemptr);
257    }
258    return mem;
259 }
260
261 void free (void *m)
262 {
263    ULONG *mem = m;
264
265    if(mem && memPool)
266    {
267       ULONG size=*--mem;
268
269       if (memsemptr)
270       {
271          ObtainSemaphore(memsemptr);
272       }
273       FreePooled(memPool,mem,size);
274       if (memsemptr)
275       {
276          ReleaseSemaphore(memsemptr);
277       }
278    }
279 }
280
281 void *realloc (void *old, size_t ns)
282 {
283    void *new;
284    LONG osize, *o = old;
285    LONG nsize = ns;
286
287    if (!old)
288    {
289       return malloc(nsize);
290    }
291    osize = (*(o-1)) - sizeof(ULONG);
292    if (nsize <= osize)
293    {
294       return old;
295    }
296    if ((new = malloc(nsize)))
297    {
298       ULONG *n = new;
299
300       osize >>= 2;
301       while(osize--)
302       {
303          *n++ = *o++;
304       }
305       free(old);
306    }
307    return new;
308 }
309
310 void __memCleanUp (void)
311 {
312    if (memsemptr)
313    {
314       ObtainSemaphore(memsemptr);
315    }
316    if (memPool)
317    {
318       DeletePool(memPool);
319    }
320    if (memsemptr)
321    {
322       ReleaseSemaphore(memsemptr);
323    }
324 }
325
326 #define ADD2LIST(a,b,c) asm(".stabs \"_" #b "\"," #c ",0,0,_" #a )
327 #define ADD2EXIT(a,pri) ADD2LIST(a,__EXIT_LIST__,22); \
328                         asm(".stabs \"___EXIT_LIST__\",20,0,0," #pri "+128")
329 ADD2EXIT(__memCleanUp,-50);
330 #elif !defined(ixemul)
331 #error No libnix and no ixemul!?
332 #endif /* libnix */
333 #else
334 #error Only GCC is supported, multithreading safe malloc/free required.
335 #endif /* __GNUC__ */
336 #endif /* !__amigaos4__ */
337
338 #endif /* def AMIGA */