1 /****************************************************************************
3 * GNAT RUN-TIME COMPONENTS *
7 * C Implementation File *
9 * Copyright (C) 2001-2021, 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>
47 # include <sys/wait.h>
49 #elif defined (__vxworks) && defined (__RTP__)
51 #elif defined (__Lynx__)
52 /* ??? See comment in adaint.c. */
53 # define GCC_RESOURCE_H
54 # include <sys/wait.h>
55 #elif defined (__PikeOS__)
56 /* No wait.h available */
61 /* This file provides the low level functionalities needed to implement Expect
62 capabilities in GNAT.Expect.
63 Implementations for unix and windows systems is provided.
64 Dummy stubs are also provided for other systems. */
67 /* Work around the fact that gcc/cpp does not define "__unix__" under AiX. */
72 /* Work around the fact that gcc/cpp does not define "__unix__" on Darwin. */
85 __gnat_waitpid (int pid
)
87 HANDLE h
= OpenProcess (PROCESS_ALL_ACCESS
, FALSE
, pid
);
92 (void) WaitForSingleObject (h
, INFINITE
);
93 GetExitCodeProcess (h
, &exitcode
);
97 __gnat_win32_remove_handle (NULL
, pid
);
98 return (int) exitcode
;
102 __gnat_expect_fork (void)
108 __gnat_expect_portable_execvp (int *pid
, char *cmd ATTRIBUTE_UNUSED
,
111 *pid
= __gnat_portable_no_block_spawn (argv
);
115 __gnat_pipe (int *fd
)
119 CreatePipe (&read
, &write
, NULL
, 0);
120 fd
[0]=_open_osfhandle ((intptr_t)read
, 0);
121 fd
[1]=_open_osfhandle ((intptr_t)write
, 0);
122 return 0; /* always success */
126 __gnat_expect_poll (int *fd
,
132 #define MAX_DELAY 100
134 int i
, delay
, infinite
= 0;
136 HANDLE handles
[num_fd
];
140 for (i
= 0; i
< num_fd
; i
++)
143 for (i
= 0; i
< num_fd
; i
++)
144 handles
[i
] = (HANDLE
) _get_osfhandle (fd
[i
]);
146 /* Start with small delays, and then increase them, to avoid polling too
147 much when waiting a long time */
155 for (i
= 0; i
< num_fd
; i
++)
157 if (!PeekNamedPipe (handles
[i
], NULL
, 0, NULL
, &avail
, NULL
))
159 *dead_process
= i
+ 1;
169 if (!infinite
&& timeout
<= 0)
175 if (delay
< MAX_DELAY
)
186 #include <vms/descrip.h>
188 #include <vms/stsdef.h>
189 #include <vms/iodef.h>
193 __gnat_waitpid (int pid
)
197 waitpid (pid
, &status
, 0);
198 status
= WEXITSTATUS (status
);
204 __gnat_pipe (int *fd
)
210 __gnat_expect_fork (void)
216 __gnat_expect_portable_execvp (int *pid
, char *cmd
, char *argv
[])
218 *pid
= (int) getpid ();
219 /* Since cmd is fully qualified, it is incorrect to call execvp */
225 __gnat_expect_poll (int *fd
,
231 int i
, num
, ready
= 0;
233 int mbxchans
[num_fd
];
234 struct dsc$descriptor_s mbxname
;
235 struct io_status_block
{
244 for (i
= 0; i
< num_fd
; i
++)
247 for (i
= 0; i
< num_fd
; i
++)
250 /* Get name of the mailbox used in the pipe */
251 getname (fd
[i
], buf
);
253 /* Assign a channel to the mailbox */
254 if (strlen (buf
) > 0)
256 mbxname
.dsc$w_length
= strlen (buf
);
257 mbxname
.dsc$b_dtype
= DSC$K_DTYPE_T
;
258 mbxname
.dsc$b_class
= DSC$K_CLASS_S
;
259 mbxname
.dsc$a_pointer
= buf
;
261 status
= SYS$
ASSIGN (&mbxname
, &mbxchans
[i
], 0, 0, 0);
263 if ((status
& 1) != 1)
266 *dead_process
= i
+ 1;
276 for (i
= 0; i
< num_fd
; i
++)
281 /* Peek in the mailbox to see if there's data */
283 (0, mbxchans
[i
], IO$_SENSEMODE
|IO$M_READERCHECK
,
284 &iosb
, 0, 0, 0, 0, 0, 0, 0, 0);
286 if ((status
& 1) != 1)
301 if (timeout
> 0 && num
== 0)
313 /* Deassign channels assigned above */
314 for (i
= 0; i
< num_fd
; i
++)
317 status
= SYS$
DASSGN (mbxchans
[i
]);
322 #elif defined (__unix__)
325 #include <sys/ptyio.h>
328 #include <sys/time.h>
331 #define SELECT_MASK fd_set
332 #else /* !NO_FD_SET */
334 typedef long fd_mask
;
337 #define SELECT_MASK void
339 #define SELECT_MASK int
341 #endif /* !NO_FD_SET */
344 __gnat_waitpid (int pid
)
348 waitpid (pid
, &status
, 0);
349 status
= WEXITSTATUS (status
);
355 __gnat_pipe (int *fd
)
361 __gnat_expect_fork (void)
365 __gnat_in_child_after_fork
= 1;
371 __gnat_expect_portable_execvp (int *pid
, char *cmd
, char *argv
[])
373 *pid
= (int) getpid ();
374 /* Since cmd is fully qualified, it is incorrect to call execvp */
380 __gnat_expect_poll (int *fd
,
399 tv
.tv_sec
= timeout
/ 1000;
400 tv
.tv_usec
= (timeout
% 1000) * 1000;
406 for (i
= 0; i
< num_fd
; i
++)
408 FD_SET (fd
[i
], &rset
);
409 FD_SET (fd
[i
], &eset
);
416 select (max_fd
+ 1, &rset
, NULL
, &eset
, timeout
== -1 ? NULL
: &tv
);
424 for (i
= 0; i
< num_fd
; i
++)
426 if (FD_ISSET (fd
[i
], &rset
))
438 for (i
= 0; i
< num_fd
; i
++)
440 if (FD_ISSET (fd
[i
], &eset
))
442 struct request_info ei
;
444 /* Only query and reset error state if no file descriptor
445 is ready to be read, otherwise we will be signalling a
446 died process too early */
450 ioctl (fd
[i
], TIOCREQCHECK
, &ei
);
452 if (ei
.request
== TIOCCLOSE
)
454 ioctl (fd
[i
], TIOCREQSET
, &ei
);
455 *dead_process
= i
+ 1;
459 ioctl (fd
[i
], TIOCREQSET
, &ei
);
466 } while (timeout
== -1 && ready
== 0);
474 __gnat_waitpid (int pid ATTRIBUTE_UNUSED
, int sig ATTRIBUTE_UNUSED
)
480 __gnat_pipe (int *fd ATTRIBUTE_UNUSED
)
486 __gnat_expect_fork (void)
492 __gnat_expect_portable_execvp (int *pid ATTRIBUTE_UNUSED
,
493 char *cmd ATTRIBUTE_UNUSED
,
494 char *argv
[] ATTRIBUTE_UNUSED
)
500 __gnat_expect_poll (int *fd ATTRIBUTE_UNUSED
,
501 int num_fd ATTRIBUTE_UNUSED
,
502 int timeout ATTRIBUTE_UNUSED
,
503 int *dead_process ATTRIBUTE_UNUSED
,
504 int *is_set ATTRIBUTE_UNUSED
)