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