Made failure to bind() a fatal error.
[privoxy.git] / amiga.c
1 const char amiga_rcs[] = "$Id: amiga.c,v 1.1 2001/05/13 21:57:06 administrator Exp $";
2 /*********************************************************************
3  *
4  * File        :  $Source: /home/administrator/cvs/ijb/jcc.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: jcc.c,v $
31  *
32  *********************************************************************/
33 \f
34
35 #include "config.h"
36
37 #ifdef AMIGA
38
39 #include <stdio.h>
40 #include <signal.h>
41
42 #include "amiga.h"
43
44 chonst char amiga_h_rcs[] = AMIGA_H_VERSION;
45
46 unsigned long __stack = 20*1024;
47 /* static char ver[] = "$VER: junkbuster " __AMIGAVERSION__ " (" __AMIGADATE__ ")"; */
48 struct Task *main_task = NULL;
49 int childs = 0;
50
51 void serve(struct client_state *csp);
52
53 __saveds ULONG server_thread(void)
54 {
55    struct client_state *local_csp;
56    struct UserData UserData;
57    struct Task *me=FindTask(NULL);
58
59    Wait(SIGF_SINGLE);
60    local_csp=(struct client_state *)(me->tc_UserData);
61    me->tc_UserData=&UserData;
62    SocketBase=(APTR)OpenLibrary("bsdsocket.library",3);
63    if(SocketBase)
64    {
65       SetErrnoPtr(&(UserData.eno),sizeof(int));
66       local_csp->cfd=ObtainSocket(local_csp->cfd, AF_INET, SOCK_STREAM, 0);
67       if(-1!=local_csp->cfd)
68       {
69          Signal(main_task,SIGF_SINGLE);
70          serve((struct client_state *) local_csp);
71       } else {
72          local_csp->active = 0;
73          Signal(main_task,SIGF_SINGLE);
74       }
75       CloseLibrary(SocketBase);
76    } else {
77       local_csp->active = 0;
78       Signal(main_task,SIGF_SINGLE);
79    }
80    childs--;
81    return 0;
82 }
83
84 void amiga_exit(void)
85 {
86    if(SocketBase)
87    {
88       CloseLibrary(SocketBase);
89    }
90 }
91
92 static struct SignalSemaphore memsem;
93 static struct SignalSemaphore *memsemptr = NULL;
94 static struct UserData GlobalUserData;
95
96 void InitAmiga(void)
97 {
98    main_task = FindTask(NULL);
99    main_task->tc_UserData = &GlobalUserData;
100
101    if (((struct Library *)SysBase)->lib_Version < 39)
102    {
103       exit(RETURN_FAIL);
104    }
105
106    signal(SIGINT,SIG_IGN);
107    SocketBase = (APTR)OpenLibrary("bsdsocket.library",3);
108    if (!SocketBase)
109    {
110       fprintf(stderr, "Can't open bsdsocket.library V3+\n");
111       exit(RETURN_ERROR);
112    }
113    SetErrnoPtr(&(GlobalUserData.eno),sizeof(int));
114    InitSemaphore(&memsem);
115    memsemptr = &memsem;
116
117    atexit(amiga_exit);
118 }
119
120 #ifdef __GNUC__
121 #ifdef libnix
122 /* multitaskingsafe libnix replacements */
123 static void *memPool=NULL;
124
125 void *malloc (size_t s)
126 {
127    ULONG *mem;
128    LONG size = s;
129
130    if (size<=0)
131    {
132       return NULL;
133    }
134    if (!memPool)
135    {
136       if (!(memPool=CreatePool(MEMF_ANY,32*1024,8*1024)))
137       {
138          return NULL;
139       }
140    }
141    size += sizeof(ULONG) + MEM_BLOCKMASK;
142    size &= ~MEM_BLOCKMASK;
143    if (memsemptr)
144    {
145       ObtainSemaphore(memsemptr);
146    }
147    if ((mem=AllocPooled(memPool,size)))
148    {
149       *mem++=size;
150    }
151    if (memsemptr)
152    {
153       ReleaseSemaphore(memsemptr);
154    }
155    return mem;
156 }
157
158 void free (void *m)
159 {
160    ULONG *mem = m;
161
162    if(mem && memPool)
163    {
164       ULONG size=*--mem;
165
166       if (memsemptr)
167       {
168          ObtainSemaphore(memsemptr);
169       }
170       FreePooled(memPool,mem,size);
171       if (memsemptr)
172       {
173          ReleaseSemaphore(memsemptr);
174       }
175    }
176 }
177
178 void *realloc (void *old, size_t ns)
179 {
180    void *new;
181    LONG osize, *o = old;
182    LONG nsize = ns;
183
184    if (!old)
185    {
186       return malloc(nsize);
187    }
188    osize = (*(o-1)) - sizeof(ULONG);
189    if (nsize <= osize)
190    {
191       return old;
192    }
193    if ((new = malloc(nsize)))
194    {
195       ULONG *n = new;
196
197       osize >>= 2;
198       while(osize--)
199       {
200          *n++ = *o++;
201       }
202       free(old);
203    }
204    return new;
205 }
206
207 void __memCleanUp (void)
208 {
209    if (memsemptr)
210    {
211       ObtainSemaphore(memsemptr);
212    }
213    if (memPool)
214    {
215       DeletePool(memPool);
216    }
217    if (memsemptr)
218    {
219       ReleaseSemaphore(memsemptr);
220    }
221 }
222
223 #define ADD2LIST(a,b,c) asm(".stabs \"_" #b "\"," #c ",0,0,_" #a )
224 #define ADD2EXIT(a,pri) ADD2LIST(a,__EXIT_LIST__,22); \
225                         asm(".stabs \"___EXIT_LIST__\",20,0,0," #pri "+128")
226 ADD2EXIT(__memCleanUp,-50);
227 #elif !defined(ixemul)
228 #error No libnix and no ixemul!?
229 #endif /* libnix */
230 #else
231 #error Only GCC is supported, multitasking safe malloc/free required.
232 #endif /* __GNUC__ */
233
234 #endif /* def AMIGA */