1 /****************************************************************************
3 * GNAT RUN-TIME COMPONENTS *
7 * C Implementation File *
9 * Copyright (C) 2001-2024, AdaCore *
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 3, 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. *
18 * As a special exception under Section 7 of GPL version 3, you are granted *
19 * additional permissions described in the GCC Runtime Library Exception, *
20 * version 3.1, as published by the Free Software Foundation. *
22 * You should have received a copy of the GNU General Public License and *
23 * a copy of the GCC Runtime Library Exception along with this program; *
24 * see the files COPYING3 and COPYING.RUNTIME respectively. If not, see *
25 * <http://www.gnu.org/licenses/>. *
27 * GNAT was originally developed by the GNAT team at New York University. *
28 * Extensive contributions were provided by Ada Core Technologies Inc. *
30 ****************************************************************************/
43 #include <sys/types.h>
46 #if defined (__vxworks) && defined (__RTP__)
48 #elif defined (__Lynx__)
49 /* ??? See comment in adaint.c. */
50 # define GCC_RESOURCE_H
51 # include <sys/wait.h>
52 #elif defined (__PikeOS__) || defined (__MINGW32__)
53 /* No wait.h available */
58 /* This file provides the low level functionalities needed to implement Expect
59 capabilities in GNAT.Expect.
60 Implementations for unix and windows systems is provided.
61 Dummy stubs are also provided for other systems. */
64 /* Work around the fact that gcc/cpp does not define "__unix__" under AiX. */
69 /* Work around the fact that gcc/cpp does not define "__unix__" on Darwin. */
75 #define WIN32_LEAN_AND_MEAN
83 __gnat_waitpid (int pid
)
85 HANDLE h
= OpenProcess (PROCESS_ALL_ACCESS
, FALSE
, pid
);
90 (void) WaitForSingleObject (h
, INFINITE
);
91 GetExitCodeProcess (h
, &exitcode
);
95 __gnat_win32_remove_handle (NULL
, pid
);
96 return (int) exitcode
;
100 __gnat_expect_fork (void)
106 __gnat_expect_portable_execvp (int *pid
, char *cmd ATTRIBUTE_UNUSED
,
109 *pid
= __gnat_portable_no_block_spawn (argv
);
113 __gnat_pipe (int *fd
)
117 CreatePipe (&read
, &write
, NULL
, 0);
118 fd
[0]=_open_osfhandle ((intptr_t)read
, 0);
119 fd
[1]=_open_osfhandle ((intptr_t)write
, 0);
120 return 0; /* always success */
124 __gnat_expect_poll (int *fd
,
130 #define MAX_DELAY 100
132 int i
, delay
, infinite
= 0;
134 HANDLE handles
[num_fd
];
138 for (i
= 0; i
< num_fd
; i
++)
141 for (i
= 0; i
< num_fd
; i
++)
142 handles
[i
] = (HANDLE
) _get_osfhandle (fd
[i
]);
144 /* Start with small delays, and then increase them, to avoid polling too
145 much when waiting a long time */
153 for (i
= 0; i
< num_fd
; i
++)
155 if (!PeekNamedPipe (handles
[i
], NULL
, 0, NULL
, &avail
, NULL
))
157 *dead_process
= i
+ 1;
167 if (!infinite
&& timeout
<= 0)
173 if (delay
< MAX_DELAY
)
184 #include <vms/descrip.h>
186 #include <vms/stsdef.h>
187 #include <vms/iodef.h>
191 __gnat_waitpid (int pid
)
195 waitpid (pid
, &status
, 0);
196 status
= WEXITSTATUS (status
);
202 __gnat_pipe (int *fd
)
208 __gnat_expect_fork (void)
214 __gnat_expect_portable_execvp (int *pid
, char *cmd
, char *argv
[])
216 *pid
= (int) getpid ();
217 /* Since cmd is fully qualified, it is incorrect to call execvp */
223 __gnat_expect_poll (int *fd
,
229 int i
, num
, ready
= 0;
231 int mbxchans
[num_fd
];
232 struct dsc$descriptor_s mbxname
;
233 struct io_status_block
{
242 for (i
= 0; i
< num_fd
; i
++)
245 for (i
= 0; i
< num_fd
; i
++)
248 /* Get name of the mailbox used in the pipe */
249 getname (fd
[i
], buf
);
251 /* Assign a channel to the mailbox */
252 if (strlen (buf
) > 0)
254 mbxname
.dsc$w_length
= strlen (buf
);
255 mbxname
.dsc$b_dtype
= DSC$K_DTYPE_T
;
256 mbxname
.dsc$b_class
= DSC$K_CLASS_S
;
257 mbxname
.dsc$a_pointer
= buf
;
259 status
= SYS$
ASSIGN (&mbxname
, &mbxchans
[i
], 0, 0, 0);
261 if ((status
& 1) != 1)
264 *dead_process
= i
+ 1;
274 for (i
= 0; i
< num_fd
; i
++)
279 /* Peek in the mailbox to see if there's data */
281 (0, mbxchans
[i
], IO$_SENSEMODE
|IO$M_READERCHECK
,
282 &iosb
, 0, 0, 0, 0, 0, 0, 0, 0);
284 if ((status
& 1) != 1)
299 if (timeout
> 0 && num
== 0)
311 /* Deassign channels assigned above */
312 for (i
= 0; i
< num_fd
; i
++)
315 status
= SYS$
DASSGN (mbxchans
[i
]);
320 #elif defined (__unix__)
323 #include <sys/ptyio.h>
326 #include <sys/time.h>
329 #define SELECT_MASK fd_set
330 #else /* !NO_FD_SET */
332 typedef long fd_mask
;
335 #define SELECT_MASK void
337 #define SELECT_MASK int
339 #endif /* !NO_FD_SET */
342 __gnat_waitpid (int pid
)
346 if (waitpid (pid
, &status
, 0) == -1) {
350 if (WIFEXITED (status
)) {
351 status
= WEXITSTATUS (status
);
352 } else if (WIFSIGNALED (status
)) {
353 status
= WTERMSIG (status
);
354 } else if (WIFSTOPPED (status
)) {
355 status
= WSTOPSIG (status
);
362 __gnat_pipe (int *fd
)
368 __gnat_expect_fork (void)
372 __gnat_in_child_after_fork
= 1;
378 __gnat_expect_portable_execvp (int *pid
, char *cmd
, char *argv
[])
380 *pid
= (int) getpid ();
381 /* Since cmd is fully qualified, it is incorrect to call execvp */
387 __gnat_expect_poll (int *fd
,
406 tv
.tv_sec
= timeout
/ 1000;
407 tv
.tv_usec
= (timeout
% 1000) * 1000;
413 for (i
= 0; i
< num_fd
; i
++)
415 FD_SET (fd
[i
], &rset
);
416 FD_SET (fd
[i
], &eset
);
423 select (max_fd
+ 1, &rset
, NULL
, &eset
, timeout
== -1 ? NULL
: &tv
);
431 for (i
= 0; i
< num_fd
; i
++)
433 if (FD_ISSET (fd
[i
], &rset
))
445 for (i
= 0; i
< num_fd
; i
++)
447 if (FD_ISSET (fd
[i
], &eset
))
449 struct request_info ei
;
451 /* Only query and reset error state if no file descriptor
452 is ready to be read, otherwise we will be signalling a
453 died process too early */
457 ioctl (fd
[i
], TIOCREQCHECK
, &ei
);
459 if (ei
.request
== TIOCCLOSE
)
461 ioctl (fd
[i
], TIOCREQSET
, &ei
);
462 *dead_process
= i
+ 1;
466 ioctl (fd
[i
], TIOCREQSET
, &ei
);
473 } while (timeout
== -1 && ready
== 0);
481 __gnat_waitpid (int pid ATTRIBUTE_UNUSED
, int sig ATTRIBUTE_UNUSED
)
487 __gnat_pipe (int *fd ATTRIBUTE_UNUSED
)
493 __gnat_expect_fork (void)
499 __gnat_expect_portable_execvp (int *pid ATTRIBUTE_UNUSED
,
500 char *cmd ATTRIBUTE_UNUSED
,
501 char *argv
[] ATTRIBUTE_UNUSED
)
507 __gnat_expect_poll (int *fd ATTRIBUTE_UNUSED
,
508 int num_fd ATTRIBUTE_UNUSED
,
509 int timeout ATTRIBUTE_UNUSED
,
510 int *dead_process ATTRIBUTE_UNUSED
,
511 int *is_set ATTRIBUTE_UNUSED
)