1 /****************************************************************************
3 * GNAT RUN-TIME COMPONENTS *
7 * C Implementation File *
9 * Copyright (C) 2001-2014, 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 ****************************************************************************/
32 #ifdef __alpha_vxworks
45 #include <sys/types.h>
49 # include <sys/wait.h>
51 #elif defined (__vxworks) && defined (__RTP__)
53 #elif defined (__Lynx__)
54 /* ??? See comment in adaint.c. */
55 # define GCC_RESOURCE_H
56 # include <sys/wait.h>
57 #elif defined (__nucleus__) || defined (__PikeOS__)
58 /* No wait.h available on Nucleus */
63 /* This file provides the low level functionalities needed to implement Expect
64 capabilities in GNAT.Expect.
65 Implementations for unix and windows systems is provided.
66 Dummy stubs are also provided for other systems. */
69 /* Work around the fact that gcc/cpp does not define "__unix__" under AiX. */
74 /* Work around the fact that gcc/cpp does not define "__unix__" on Darwin. */
87 __gnat_kill (int pid
, int sig
, int close
)
89 HANDLE h
= OpenProcess (PROCESS_ALL_ACCESS
, FALSE
, pid
);
94 TerminateProcess (h
, 0);
95 __gnat_win32_remove_handle (NULL
, pid
);
97 else if (sig
== SIGINT
)
98 GenerateConsoleCtrlEvent (CTRL_C_EVENT
, pid
);
99 else if (sig
== SIGBREAK
)
100 GenerateConsoleCtrlEvent (CTRL_BREAK_EVENT
, pid
);
101 /* ??? The last two alternatives don't really work. SIGBREAK requires setting
102 up process groups at start time which we don't do; treating SIGINT is just
103 not possible apparently. So we really only support signal 9. Fortunately
104 that's all we use in GNAT.Expect */
110 __gnat_waitpid (int pid
)
112 HANDLE h
= OpenProcess (PROCESS_ALL_ACCESS
, FALSE
, pid
);
118 res
= WaitForSingleObject (h
, INFINITE
);
119 GetExitCodeProcess (h
, &exitcode
);
123 __gnat_win32_remove_handle (NULL
, pid
);
124 return (int) exitcode
;
128 __gnat_expect_fork (void)
134 __gnat_expect_portable_execvp (int *pid
, char *cmd
, char *argv
[])
136 *pid
= __gnat_portable_no_block_spawn (argv
);
140 __gnat_pipe (int *fd
)
144 CreatePipe (&read
, &write
, NULL
, 0);
145 fd
[0]=_open_osfhandle ((intptr_t)read
, 0);
146 fd
[1]=_open_osfhandle ((intptr_t)write
, 0);
147 return 0; /* always success */
151 __gnat_expect_poll (int *fd
,
157 #define MAX_DELAY 100
159 int i
, delay
, infinite
= 0;
161 HANDLE handles
[num_fd
];
165 for (i
= 0; i
< num_fd
; i
++)
168 for (i
= 0; i
< num_fd
; i
++)
169 handles
[i
] = (HANDLE
) _get_osfhandle (fd
[i
]);
171 /* Start with small delays, and then increase them, to avoid polling too
172 much when waiting a long time */
180 for (i
= 0; i
< num_fd
; i
++)
182 if (!PeekNamedPipe (handles
[i
], NULL
, 0, NULL
, &avail
, NULL
))
184 *dead_process
= i
+ 1;
194 if (!infinite
&& timeout
<= 0)
200 if (delay
< MAX_DELAY
)
211 #include <vms/descrip.h>
213 #include <vms/stsdef.h>
214 #include <vms/iodef.h>
218 __gnat_kill (int pid
, int sig
, int close
)
224 __gnat_waitpid (int pid
)
228 waitpid (pid
, &status
, 0);
229 status
= WEXITSTATUS (status
);
235 __gnat_pipe (int *fd
)
241 __gnat_expect_fork (void)
247 __gnat_expect_portable_execvp (int *pid
, char *cmd
, char *argv
[])
249 *pid
= (int) getpid ();
250 /* Since cmd is fully qualified, it is incorrect to call execvp */
256 __gnat_expect_poll (int *fd
,
262 int i
, num
, ready
= 0;
264 int mbxchans
[num_fd
];
265 struct dsc$descriptor_s mbxname
;
266 struct io_status_block
{
275 for (i
= 0; i
< num_fd
; i
++)
278 for (i
= 0; i
< num_fd
; i
++)
281 /* Get name of the mailbox used in the pipe */
282 getname (fd
[i
], buf
);
284 /* Assign a channel to the mailbox */
285 if (strlen (buf
) > 0)
287 mbxname
.dsc$w_length
= strlen (buf
);
288 mbxname
.dsc$b_dtype
= DSC$K_DTYPE_T
;
289 mbxname
.dsc$b_class
= DSC$K_CLASS_S
;
290 mbxname
.dsc$a_pointer
= buf
;
292 status
= SYS$
ASSIGN (&mbxname
, &mbxchans
[i
], 0, 0, 0);
294 if ((status
& 1) != 1)
297 dead_process
= i
+ 1;
307 for (i
= 0; i
< num_fd
; i
++)
312 /* Peek in the mailbox to see if there's data */
314 (0, mbxchans
[i
], IO$_SENSEMODE
|IO$M_READERCHECK
,
315 &iosb
, 0, 0, 0, 0, 0, 0, 0, 0);
317 if ((status
& 1) != 1)
332 if (timeout
> 0 && num
== 0)
344 /* Deassign channels assigned above */
345 for (i
= 0; i
< num_fd
; i
++)
348 status
= SYS$
DASSGN (mbxchans
[i
]);
353 #elif defined (__unix__) && !defined (__nucleus__)
356 #include <sys/ptyio.h>
359 #include <sys/time.h>
362 #define SELECT_MASK fd_set
363 #else /* !NO_FD_SET */
365 typedef long fd_mask
;
368 #define SELECT_MASK void
370 #define SELECT_MASK int
372 #endif /* !NO_FD_SET */
375 __gnat_kill (int pid
, int sig
, int close
)
381 __gnat_waitpid (int pid
)
385 waitpid (pid
, &status
, 0);
386 status
= WEXITSTATUS (status
);
392 __gnat_pipe (int *fd
)
398 __gnat_expect_fork (void)
404 __gnat_expect_portable_execvp (int *pid
, char *cmd
, char *argv
[])
406 *pid
= (int) getpid ();
407 /* Since cmd is fully qualified, it is incorrect to call execvp */
413 __gnat_expect_poll (int *fd
,
430 tv
.tv_sec
= timeout
/ 1000;
431 tv
.tv_usec
= (timeout
% 1000) * 1000;
437 for (i
= 0; i
< num_fd
; i
++)
439 FD_SET (fd
[i
], &rset
);
440 FD_SET (fd
[i
], &eset
);
447 select (max_fd
+ 1, &rset
, NULL
, &eset
, timeout
== -1 ? NULL
: &tv
);
453 for (i
= 0; i
< num_fd
; i
++)
455 if (FD_ISSET (fd
[i
], &rset
))
465 for (i
= 0; i
< num_fd
; i
++)
467 if (FD_ISSET (fd
[i
], &eset
))
469 struct request_info ei
;
471 /* Only query and reset error state if no file descriptor
472 is ready to be read, otherwise we will be signalling a
473 died process too early */
477 ioctl (fd
[i
], TIOCREQCHECK
, &ei
);
479 if (ei
.request
== TIOCCLOSE
)
481 ioctl (fd
[i
], TIOCREQSET
, &ei
);
482 dead_process
= i
+ 1;
486 ioctl (fd
[i
], TIOCREQSET
, &ei
);
493 } while (timeout
== -1 && ready
== 0);
501 __gnat_kill (int pid ATTRIBUTE_UNUSED
,
502 int sig ATTRIBUTE_UNUSED
,
503 int close ATTRIBUTE_UNUSED
)
508 __gnat_waitpid (int pid ATTRIBUTE_UNUSED
, int sig ATTRIBUTE_UNUSED
)
514 __gnat_pipe (int *fd ATTRIBUTE_UNUSED
)
520 __gnat_expect_fork (void)
526 __gnat_expect_portable_execvp (int *pid ATTRIBUTE_UNUSED
,
527 char *cmd ATTRIBUTE_UNUSED
,
528 char *argv
[] ATTRIBUTE_UNUSED
)
534 __gnat_expect_poll (int *fd ATTRIBUTE_UNUSED
,
535 int num_fd ATTRIBUTE_UNUSED
,
536 int timeout ATTRIBUTE_UNUSED
,
537 int *dead_process ATTRIBUTE_UNUSED
,
538 int *is_set ATTRIBUTE_UNUSED
)