2008-05-30 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / gcc / ada / g-socthi-vms.adb
blobf71bb2387ded17097987dbd57986d34c72149b08
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- G N A T . S O C K E T S . T H I N --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 2001-2008, AdaCore --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 2, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
21 -- --
22 -- As a special exception, if other files instantiate generics from this --
23 -- unit, or you link this unit with other files to produce an executable, --
24 -- this unit does not by itself cause the resulting executable to be --
25 -- covered by the GNU General Public License. This exception does not --
26 -- however invalidate any other reasons why the executable file might be --
27 -- covered by the GNU Public License. --
28 -- --
29 -- GNAT was originally developed by the GNAT team at New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc. --
31 -- --
32 ------------------------------------------------------------------------------
34 -- Temporary version for Alpha/VMS
36 with GNAT.OS_Lib; use GNAT.OS_Lib;
37 with GNAT.Sockets.Constants;
38 with GNAT.Task_Lock;
40 with Interfaces.C; use Interfaces.C;
42 package body GNAT.Sockets.Thin is
44 Non_Blocking_Sockets : constant Fd_Set_Access :=
45 New_Socket_Set (No_Fd_Set_Access);
46 -- When this package is initialized with Process_Blocking_IO set
47 -- to True, sockets are set in non-blocking mode to avoid blocking
48 -- the whole process when a thread wants to perform a blocking IO
49 -- operation. But the user can also set a socket in non-blocking
50 -- mode by purpose. In order to make a difference between these
51 -- two situations, we track the origin of non-blocking mode in
52 -- Non_Blocking_Sockets. If S is in Non_Blocking_Sockets, it has
53 -- been set in non-blocking mode by the user.
55 Quantum : constant Duration := 0.2;
56 -- When Constants.Thread_Blocking_IO is False, we set sockets in
57 -- non-blocking mode and we spend a period of time Quantum between
58 -- two attempts on a blocking operation.
60 Unknown_System_Error : constant C.Strings.chars_ptr :=
61 C.Strings.New_String ("Unknown system error");
63 function Syscall_Accept
64 (S : C.int;
65 Addr : System.Address;
66 Addrlen : not null access C.int) return C.int;
67 pragma Import (C, Syscall_Accept, "accept");
69 function Syscall_Connect
70 (S : C.int;
71 Name : System.Address;
72 Namelen : C.int) return C.int;
73 pragma Import (C, Syscall_Connect, "connect");
75 function Syscall_Ioctl
76 (S : C.int;
77 Req : C.int;
78 Arg : Int_Access) return C.int;
79 pragma Import (C, Syscall_Ioctl, "ioctl");
81 function Syscall_Recv
82 (S : C.int;
83 Msg : System.Address;
84 Len : C.int;
85 Flags : C.int) return C.int;
86 pragma Import (C, Syscall_Recv, "recv");
88 function Syscall_Recvfrom
89 (S : C.int;
90 Msg : System.Address;
91 Len : C.int;
92 Flags : C.int;
93 From : Sockaddr_In_Access;
94 Fromlen : not null access C.int) return C.int;
95 pragma Import (C, Syscall_Recvfrom, "recvfrom");
97 function Syscall_Send
98 (S : C.int;
99 Msg : System.Address;
100 Len : C.int;
101 Flags : C.int) return C.int;
102 pragma Import (C, Syscall_Send, "send");
104 function Syscall_Sendto
105 (S : C.int;
106 Msg : System.Address;
107 Len : C.int;
108 Flags : C.int;
109 To : Sockaddr_In_Access;
110 Tolen : C.int) return C.int;
111 pragma Import (C, Syscall_Sendto, "sendto");
113 function Syscall_Socket
114 (Domain, Typ, Protocol : C.int) return C.int;
115 pragma Import (C, Syscall_Socket, "socket");
117 function Non_Blocking_Socket (S : C.int) return Boolean;
118 procedure Set_Non_Blocking_Socket (S : C.int; V : Boolean);
120 --------------
121 -- C_Accept --
122 --------------
124 function C_Accept
125 (S : C.int;
126 Addr : System.Address;
127 Addrlen : not null access C.int) return C.int
129 R : C.int;
130 Val : aliased C.int := 1;
132 Discard : C.int;
133 pragma Warnings (Off, Discard);
135 begin
136 loop
137 R := Syscall_Accept (S, Addr, Addrlen);
138 exit when Constants.Thread_Blocking_IO
139 or else R /= Failure
140 or else Non_Blocking_Socket (S)
141 or else Errno /= Constants.EWOULDBLOCK;
142 delay Quantum;
143 end loop;
145 if not Constants.Thread_Blocking_IO
146 and then R /= Failure
147 then
148 -- A socket inherits the properties of its server, especially
149 -- the FIONBIO flag. Do not use C_Ioctl as this subprogram
150 -- tracks sockets set in non-blocking mode by user.
152 Set_Non_Blocking_Socket (R, Non_Blocking_Socket (S));
153 Discard := Syscall_Ioctl (R, Constants.FIONBIO, Val'Unchecked_Access);
154 end if;
156 return R;
157 end C_Accept;
159 ---------------
160 -- C_Connect --
161 ---------------
163 function C_Connect
164 (S : C.int;
165 Name : System.Address;
166 Namelen : C.int) return C.int
168 Res : C.int;
170 begin
171 Res := Syscall_Connect (S, Name, Namelen);
173 if Constants.Thread_Blocking_IO
174 or else Res /= Failure
175 or else Non_Blocking_Socket (S)
176 or else Errno /= Constants.EINPROGRESS
177 then
178 return Res;
179 end if;
181 declare
182 WSet : Fd_Set_Access;
183 Now : aliased Timeval;
185 begin
186 WSet := New_Socket_Set (No_Fd_Set_Access);
187 loop
188 Insert_Socket_In_Set (WSet, S);
189 Now := Immediat;
190 Res := C_Select
191 (S + 1,
192 No_Fd_Set_Access,
193 WSet,
194 No_Fd_Set_Access,
195 Now'Unchecked_Access);
197 exit when Res > 0;
199 if Res = Failure then
200 Free_Socket_Set (WSet);
201 return Res;
202 end if;
204 delay Quantum;
205 end loop;
207 Free_Socket_Set (WSet);
208 end;
210 Res := Syscall_Connect (S, Name, Namelen);
212 if Res = Failure and then Errno = Constants.EISCONN then
213 return Thin_Common.Success;
215 else
216 return Res;
217 end if;
218 end C_Connect;
220 -------------
221 -- C_Ioctl --
222 -------------
224 function C_Ioctl
225 (S : C.int;
226 Req : C.int;
227 Arg : Int_Access) return C.int
229 begin
230 if not Constants.Thread_Blocking_IO
231 and then Req = Constants.FIONBIO
232 then
233 if Arg.all /= 0 then
234 Set_Non_Blocking_Socket (S, True);
235 end if;
236 end if;
238 return Syscall_Ioctl (S, Req, Arg);
239 end C_Ioctl;
241 ------------
242 -- C_Recv --
243 ------------
245 function C_Recv
246 (S : C.int;
247 Msg : System.Address;
248 Len : C.int;
249 Flags : C.int) return C.int
251 Res : C.int;
253 begin
254 loop
255 Res := Syscall_Recv (S, Msg, Len, Flags);
256 exit when Constants.Thread_Blocking_IO
257 or else Res /= Failure
258 or else Non_Blocking_Socket (S)
259 or else Errno /= Constants.EWOULDBLOCK;
260 delay Quantum;
261 end loop;
263 return Res;
264 end C_Recv;
266 ----------------
267 -- C_Recvfrom --
268 ----------------
270 function C_Recvfrom
271 (S : C.int;
272 Msg : System.Address;
273 Len : C.int;
274 Flags : C.int;
275 From : Sockaddr_In_Access;
276 Fromlen : not null access C.int) return C.int
278 Res : C.int;
280 begin
281 loop
282 Res := Syscall_Recvfrom (S, Msg, Len, Flags, From, Fromlen);
283 exit when Constants.Thread_Blocking_IO
284 or else Res /= Failure
285 or else Non_Blocking_Socket (S)
286 or else Errno /= Constants.EWOULDBLOCK;
287 delay Quantum;
288 end loop;
290 return Res;
291 end C_Recvfrom;
293 ------------
294 -- C_Send --
295 ------------
297 function C_Send
298 (S : C.int;
299 Msg : System.Address;
300 Len : C.int;
301 Flags : C.int) return C.int
303 Res : C.int;
305 begin
306 loop
307 Res := Syscall_Send (S, Msg, Len, Flags);
308 exit when Constants.Thread_Blocking_IO
309 or else Res /= Failure
310 or else Non_Blocking_Socket (S)
311 or else Errno /= Constants.EWOULDBLOCK;
312 delay Quantum;
313 end loop;
315 return Res;
316 end C_Send;
318 --------------
319 -- C_Sendto --
320 --------------
322 function C_Sendto
323 (S : C.int;
324 Msg : System.Address;
325 Len : C.int;
326 Flags : C.int;
327 To : Sockaddr_In_Access;
328 Tolen : C.int) return C.int
330 Res : C.int;
332 begin
333 loop
334 Res := Syscall_Sendto (S, Msg, Len, Flags, To, Tolen);
335 exit when Constants.Thread_Blocking_IO
336 or else Res /= Failure
337 or else Non_Blocking_Socket (S)
338 or else Errno /= Constants.EWOULDBLOCK;
339 delay Quantum;
340 end loop;
342 return Res;
343 end C_Sendto;
345 --------------
346 -- C_Socket --
347 --------------
349 function C_Socket
350 (Domain : C.int;
351 Typ : C.int;
352 Protocol : C.int) return C.int
354 R : C.int;
355 Val : aliased C.int := 1;
357 Discard : C.int;
358 pragma Unreferenced (Discard);
360 begin
361 R := Syscall_Socket (Domain, Typ, Protocol);
363 if not Constants.Thread_Blocking_IO
364 and then R /= Failure
365 then
366 -- Do not use C_Ioctl as this subprogram tracks sockets set
367 -- in non-blocking mode by user.
369 Discard := Syscall_Ioctl (R, Constants.FIONBIO, Val'Unchecked_Access);
370 Set_Non_Blocking_Socket (R, False);
371 end if;
373 return R;
374 end C_Socket;
376 --------------
377 -- Finalize --
378 --------------
380 procedure Finalize is
381 begin
382 null;
383 end Finalize;
385 -------------------------
386 -- Host_Error_Messages --
387 -------------------------
389 package body Host_Error_Messages is separate;
391 ----------------
392 -- Initialize --
393 ----------------
395 procedure Initialize is
396 begin
397 null;
398 end Initialize;
400 -------------------------
401 -- Non_Blocking_Socket --
402 -------------------------
404 function Non_Blocking_Socket (S : C.int) return Boolean is
405 R : Boolean;
406 begin
407 Task_Lock.Lock;
408 R := (Is_Socket_In_Set (Non_Blocking_Sockets, S) /= 0);
409 Task_Lock.Unlock;
410 return R;
411 end Non_Blocking_Socket;
413 -----------------------------
414 -- Set_Non_Blocking_Socket --
415 -----------------------------
417 procedure Set_Non_Blocking_Socket (S : C.int; V : Boolean) is
418 begin
419 Task_Lock.Lock;
421 if V then
422 Insert_Socket_In_Set (Non_Blocking_Sockets, S);
423 else
424 Remove_Socket_From_Set (Non_Blocking_Sockets, S);
425 end if;
427 Task_Lock.Unlock;
428 end Set_Non_Blocking_Socket;
430 --------------------
431 -- Signalling_Fds --
432 --------------------
434 package body Signalling_Fds is separate;
436 --------------------------
437 -- Socket_Error_Message --
438 --------------------------
440 function Socket_Error_Message
441 (Errno : Integer) return C.Strings.chars_ptr
443 use type Interfaces.C.Strings.chars_ptr;
445 C_Msg : C.Strings.chars_ptr;
447 begin
448 C_Msg := C_Strerror (C.int (Errno));
450 if C_Msg = C.Strings.Null_Ptr then
451 return Unknown_System_Error;
452 else
453 return C_Msg;
454 end if;
455 end Socket_Error_Message;
457 -------------
458 -- C_Readv --
459 -------------
461 function C_Readv
462 (Fd : C.int;
463 Iov : System.Address;
464 Iovcnt : C.int) return C.int
466 Res : C.int;
467 Count : C.int := 0;
469 Iovec : array (0 .. Iovcnt - 1) of Vector_Element;
470 for Iovec'Address use Iov;
471 pragma Import (Ada, Iovec);
473 begin
474 for J in Iovec'Range loop
475 Res := C_Recv
476 (Fd,
477 Iovec (J).Base.all'Address,
478 Interfaces.C.int (Iovec (J).Length),
481 if Res < 0 then
482 return Res;
483 else
484 Count := Count + Res;
485 end if;
486 end loop;
487 return Count;
488 end C_Readv;
490 --------------
491 -- C_Writev --
492 --------------
494 function C_Writev
495 (Fd : C.int;
496 Iov : System.Address;
497 Iovcnt : C.int) return C.int
499 Res : C.int;
500 Count : C.int := 0;
502 Iovec : array (0 .. Iovcnt - 1) of Vector_Element;
503 for Iovec'Address use Iov;
504 pragma Import (Ada, Iovec);
506 begin
507 for J in Iovec'Range loop
508 Res := C_Send
509 (Fd,
510 Iovec (J).Base.all'Address,
511 Interfaces.C.int (Iovec (J).Length),
512 Constants.MSG_Forced_Flags);
514 if Res < 0 then
515 return Res;
516 else
517 Count := Count + Res;
518 end if;
519 end loop;
520 return Count;
521 end C_Writev;
523 end GNAT.Sockets.Thin;