| 1 |
/**************************************************************************** |
| 2 |
* * |
| 3 |
* GNAT RUN-TIME COMPONENTS * |
| 4 |
* * |
| 5 |
* E X P E C T * |
| 6 |
* * |
| 7 |
* C Implementation File * |
| 8 |
* * |
| 9 |
* Copyright (C) 2001-2025, 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 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. * |
| 17 |
* * |
| 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. * |
| 21 |
* * |
| 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/>. * |
| 26 |
* * |
| 27 |
* GNAT was originally developed by the GNAT team at New York University. * |
| 28 |
* Extensive contributions were provided by Ada Core Technologies Inc. * |
| 29 |
* * |
| 30 |
****************************************************************************/ |
| 31 |
|
| 32 |
#ifdef IN_RTS |
| 33 |
#define POSIX |
| 34 |
#include "runtime.h" |
| 35 |
#include <unistd.h> |
| 36 |
|
| 37 |
#else |
| 38 |
#include "config.h" |
| 39 |
#include "system.h" |
| 40 |
#endif |
| 41 |
|
| 42 |
#include "adaint.h" |
| 43 |
#include <sys/types.h> |
| 44 |
#include <string.h> |
| 45 |
|
| 46 |
#if defined (__vxworks) && defined (__RTP__) |
| 47 |
# include <wait.h> |
| 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 */ |
| 54 |
#else |
| 55 |
#include <sys/wait.h> |
| 56 |
#endif |
| 57 |
|
| 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. */ |
| 62 |
|
| 63 |
#ifdef _AIX |
| 64 |
/* Work around the fact that gcc/cpp does not define "__unix__" under AiX. */ |
| 65 |
#define __unix__ |
| 66 |
#endif |
| 67 |
|
| 68 |
#ifdef __APPLE__ |
| 69 |
/* Work around the fact that gcc/cpp does not define "__unix__" on Darwin. */ |
| 70 |
#define __unix__ |
| 71 |
#endif |
| 72 |
|
| 73 |
#ifdef _WIN32 |
| 74 |
|
| 75 |
#define WIN32_LEAN_AND_MEAN |
| 76 |
#include <windows.h> |
| 77 |
#include <process.h> |
| 78 |
#include <signal.h> |
| 79 |
#include <io.h> |
| 80 |
#include "mingw32.h" |
| 81 |
|
| 82 |
int |
| 83 |
__gnat_waitpid (int pid) |
| 84 |
{ |
| 85 |
HANDLE h = OpenProcess (PROCESS_ALL_ACCESS, FALSE, pid); |
| 86 |
DWORD exitcode = 1; |
| 87 |
|
| 88 |
if (h != NULL) |
| 89 |
{ |
| 90 |
(void) WaitForSingleObject (h, INFINITE); |
| 91 |
GetExitCodeProcess (h, &exitcode); |
| 92 |
CloseHandle (h); |
| 93 |
} |
| 94 |
|
| 95 |
__gnat_win32_remove_handle (NULL, pid); |
| 96 |
return (int) exitcode; |
| 97 |
} |
| 98 |
|
| 99 |
int |
| 100 |
__gnat_expect_fork (void) |
| 101 |
{ |
| 102 |
return 0; |
| 103 |
} |
| 104 |
|
| 105 |
void |
| 106 |
__gnat_expect_portable_execvp (int *pid, char *cmd ATTRIBUTE_UNUSED, |
| 107 |
char *argv[]) |
| 108 |
{ |
| 109 |
*pid = __gnat_portable_no_block_spawn (argv); |
| 110 |
} |
| 111 |
|
| 112 |
int |
| 113 |
__gnat_pipe (int *fd) |
| 114 |
{ |
| 115 |
HANDLE read, write; |
| 116 |
|
| 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 */ |
| 121 |
} |
| 122 |
|
| 123 |
int |
| 124 |
__gnat_expect_poll (int *fd, |
| 125 |
int num_fd, |
| 126 |
int timeout, |
| 127 |
int *dead_process, |
| 128 |
int *is_set) |
| 129 |
{ |
| 130 |
#define MAX_DELAY 100 |
| 131 |
|
| 132 |
int i, delay, infinite = 0; |
| 133 |
DWORD avail; |
| 134 |
HANDLE handles[num_fd]; |
| 135 |
|
| 136 |
*dead_process = 0; |
| 137 |
|
| 138 |
for (i = 0; i < num_fd; i++) |
| 139 |
is_set[i] = 0; |
| 140 |
|
| 141 |
for (i = 0; i < num_fd; i++) |
| 142 |
handles[i] = (HANDLE) _get_osfhandle (fd [i]); |
| 143 |
|
| 144 |
/* Start with small delays, and then increase them, to avoid polling too |
| 145 |
much when waiting a long time */ |
| 146 |
delay = 5; |
| 147 |
|
| 148 |
if (timeout < 0) |
| 149 |
infinite = 1; |
| 150 |
|
| 151 |
while (1) |
| 152 |
{ |
| 153 |
for (i = 0; i < num_fd; i++) |
| 154 |
{ |
| 155 |
if (!PeekNamedPipe (handles [i], NULL, 0, NULL, &avail, NULL)) |
| 156 |
{ |
| 157 |
*dead_process = i + 1; |
| 158 |
return -1; |
| 159 |
} |
| 160 |
if (avail > 0) |
| 161 |
{ |
| 162 |
is_set[i] = 1; |
| 163 |
return 1; |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
if (!infinite && timeout <= 0) |
| 168 |
return 0; |
| 169 |
|
| 170 |
Sleep (delay); |
| 171 |
timeout -= delay; |
| 172 |
|
| 173 |
if (delay < MAX_DELAY) |
| 174 |
delay += 10; |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
#elif defined (VMS) |
| 179 |
#include <unistd.h> |
| 180 |
#include <stdio.h> |
| 181 |
#include <unixio.h> |
| 182 |
#include <stdlib.h> |
| 183 |
#include <string.h> |
| 184 |
#include <vms/descrip.h> |
| 185 |
#include <stdio.h> |
| 186 |
#include <vms/stsdef.h> |
| 187 |
#include <vms/iodef.h> |
| 188 |
#include <signal.h> |
| 189 |
|
| 190 |
int |
| 191 |
__gnat_waitpid (int pid) |
| 192 |
{ |
| 193 |
int status = 0; |
| 194 |
|
| 195 |
waitpid (pid, &status, 0); |
| 196 |
status = WEXITSTATUS (status); |
| 197 |
|
| 198 |
return status; |
| 199 |
} |
| 200 |
|
| 201 |
int |
| 202 |
__gnat_pipe (int *fd) |
| 203 |
{ |
| 204 |
return pipe (fd); |
| 205 |
} |
| 206 |
|
| 207 |
int |
| 208 |
__gnat_expect_fork (void) |
| 209 |
{ |
| 210 |
return -1; |
| 211 |
} |
| 212 |
|
| 213 |
void |
| 214 |
__gnat_expect_portable_execvp (int *pid, char *cmd, char *argv[]) |
| 215 |
{ |
| 216 |
*pid = (int) getpid (); |
| 217 |
/* Since cmd is fully qualified, it is incorrect to call execvp */ |
| 218 |
execv (cmd, argv); |
| 219 |
_exit (1); |
| 220 |
} |
| 221 |
|
| 222 |
int |
| 223 |
__gnat_expect_poll (int *fd, |
| 224 |
int num_fd, |
| 225 |
int timeout, |
| 226 |
int *dead_process, |
| 227 |
int *is_set) |
| 228 |
{ |
| 229 |
int i, num, ready = 0; |
| 230 |
unsigned int status; |
| 231 |
int mbxchans [num_fd]; |
| 232 |
struct dsc$descriptor_s mbxname; |
| 233 |
struct io_status_block { |
| 234 |
short int condition; |
| 235 |
short int count; |
| 236 |
int dev; |
| 237 |
} iosb; |
| 238 |
char buf [256]; |
| 239 |
|
| 240 |
*dead_process = 0; |
| 241 |
|
| 242 |
for (i = 0; i < num_fd; i++) |
| 243 |
is_set[i] = 0; |
| 244 |
|
| 245 |
for (i = 0; i < num_fd; i++) |
| 246 |
{ |
| 247 |
|
| 248 |
/* Get name of the mailbox used in the pipe */ |
| 249 |
getname (fd [i], buf); |
| 250 |
|
| 251 |
/* Assign a channel to the mailbox */ |
| 252 |
if (strlen (buf) > 0) |
| 253 |
{ |
| 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; |
| 258 |
|
| 259 |
status = SYS$ASSIGN (&mbxname, &mbxchans[i], 0, 0, 0); |
| 260 |
|
| 261 |
if ((status & 1) != 1) |
| 262 |
{ |
| 263 |
ready = -1; |
| 264 |
*dead_process = i + 1; |
| 265 |
return ready; |
| 266 |
} |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
num = timeout / 100; |
| 271 |
|
| 272 |
while (1) |
| 273 |
{ |
| 274 |
for (i = 0; i < num_fd; i++) |
| 275 |
{ |
| 276 |
if (mbxchans[i] > 0) |
| 277 |
{ |
| 278 |
|
| 279 |
/* Peek in the mailbox to see if there's data */ |
| 280 |
status = SYS$QIOW |
| 281 |
(0, mbxchans[i], IO$_SENSEMODE|IO$M_READERCHECK, |
| 282 |
&iosb, 0, 0, 0, 0, 0, 0, 0, 0); |
| 283 |
|
| 284 |
if ((status & 1) != 1) |
| 285 |
{ |
| 286 |
ready = -1; |
| 287 |
goto deassign; |
| 288 |
} |
| 289 |
|
| 290 |
if (iosb.count > 0) |
| 291 |
{ |
| 292 |
is_set[i] = 1; |
| 293 |
ready = 1; |
| 294 |
goto deassign; |
| 295 |
} |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
if (timeout > 0 && num == 0) |
| 300 |
{ |
| 301 |
ready = 0; |
| 302 |
goto deassign; |
| 303 |
} |
| 304 |
|
| 305 |
usleep (100000); |
| 306 |
num--; |
| 307 |
} |
| 308 |
|
| 309 |
deassign: |
| 310 |
|
| 311 |
/* Deassign channels assigned above */ |
| 312 |
for (i = 0; i < num_fd; i++) |
| 313 |
{ |
| 314 |
if (mbxchans[i] > 0) |
| 315 |
status = SYS$DASSGN (mbxchans[i]); |
| 316 |
} |
| 317 |
|
| 318 |
return ready; |
| 319 |
} |
| 320 |
#elif defined (__unix__) |
| 321 |
|
| 322 |
#ifdef __hpux__ |
| 323 |
#include <sys/ptyio.h> |
| 324 |
#endif |
| 325 |
|
| 326 |
#include <sys/time.h> |
| 327 |
|
| 328 |
#ifndef NO_FD_SET |
| 329 |
#define SELECT_MASK fd_set |
| 330 |
#else /* !NO_FD_SET */ |
| 331 |
#ifndef _AIX |
| 332 |
typedef long fd_mask; |
| 333 |
#endif /* _AIX */ |
| 334 |
#ifdef _IBMR2 |
| 335 |
#define SELECT_MASK void |
| 336 |
#else /* !_IBMR2 */ |
| 337 |
#define SELECT_MASK int |
| 338 |
#endif /* !_IBMR2 */ |
| 339 |
#endif /* !NO_FD_SET */ |
| 340 |
|
| 341 |
int |
| 342 |
__gnat_waitpid (int pid) |
| 343 |
{ |
| 344 |
int status = 0; |
| 345 |
|
| 346 |
if (waitpid (pid, &status, 0) == -1) { |
| 347 |
return -1; |
| 348 |
} |
| 349 |
|
| 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); |
| 356 |
} |
| 357 |
|
| 358 |
return status; |
| 359 |
} |
| 360 |
|
| 361 |
int |
| 362 |
__gnat_pipe (int *fd) |
| 363 |
{ |
| 364 |
return pipe (fd); |
| 365 |
} |
| 366 |
|
| 367 |
int |
| 368 |
__gnat_expect_fork (void) |
| 369 |
{ |
| 370 |
int pid = fork(); |
| 371 |
if (pid == 0) { |
| 372 |
__gnat_in_child_after_fork = 1; |
| 373 |
} |
| 374 |
return pid; |
| 375 |
} |
| 376 |
|
| 377 |
void |
| 378 |
__gnat_expect_portable_execvp (int *pid, char *cmd, char *argv[]) |
| 379 |
{ |
| 380 |
*pid = (int) getpid (); |
| 381 |
/* Since cmd is fully qualified, it is incorrect to call execvp */ |
| 382 |
execv (cmd, argv); |
| 383 |
_exit (1); |
| 384 |
} |
| 385 |
|
| 386 |
int |
| 387 |
__gnat_expect_poll (int *fd, |
| 388 |
int num_fd, |
| 389 |
int timeout, |
| 390 |
int *dead_process, |
| 391 |
int *is_set) |
| 392 |
{ |
| 393 |
struct timeval tv; |
| 394 |
SELECT_MASK rset; |
| 395 |
SELECT_MASK eset; |
| 396 |
|
| 397 |
int max_fd = 0; |
| 398 |
int ready; |
| 399 |
int i; |
| 400 |
#ifdef __hpux__ |
| 401 |
int received; |
| 402 |
#endif |
| 403 |
|
| 404 |
*dead_process = 0; |
| 405 |
|
| 406 |
tv.tv_sec = timeout / 1000; |
| 407 |
tv.tv_usec = (timeout % 1000) * 1000; |
| 408 |
|
| 409 |
do { |
| 410 |
FD_ZERO (&rset); |
| 411 |
FD_ZERO (&eset); |
| 412 |
|
| 413 |
for (i = 0; i < num_fd; i++) |
| 414 |
{ |
| 415 |
FD_SET (fd[i], &rset); |
| 416 |
FD_SET (fd[i], &eset); |
| 417 |
|
| 418 |
if (fd[i] > max_fd) |
| 419 |
max_fd = fd[i]; |
| 420 |
} |
| 421 |
|
| 422 |
ready = |
| 423 |
select (max_fd + 1, &rset, NULL, &eset, timeout == -1 ? NULL : &tv); |
| 424 |
|
| 425 |
if (ready > 0) |
| 426 |
{ |
| 427 |
#ifdef __hpux__ |
| 428 |
received = 0; |
| 429 |
#endif |
| 430 |
|
| 431 |
for (i = 0; i < num_fd; i++) |
| 432 |
{ |
| 433 |
if (FD_ISSET (fd[i], &rset)) |
| 434 |
{ |
| 435 |
is_set[i] = 1; |
| 436 |
#ifdef __hpux__ |
| 437 |
received = 1; |
| 438 |
#endif |
| 439 |
} |
| 440 |
else |
| 441 |
is_set[i] = 0; |
| 442 |
} |
| 443 |
|
| 444 |
#ifdef __hpux__ |
| 445 |
for (i = 0; i < num_fd; i++) |
| 446 |
{ |
| 447 |
if (FD_ISSET (fd[i], &eset)) |
| 448 |
{ |
| 449 |
struct request_info ei; |
| 450 |
|
| 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 */ |
| 454 |
|
| 455 |
if (!received) |
| 456 |
{ |
| 457 |
ioctl (fd[i], TIOCREQCHECK, &ei); |
| 458 |
|
| 459 |
if (ei.request == TIOCCLOSE) |
| 460 |
{ |
| 461 |
ioctl (fd[i], TIOCREQSET, &ei); |
| 462 |
*dead_process = i + 1; |
| 463 |
return -1; |
| 464 |
} |
| 465 |
|
| 466 |
ioctl (fd[i], TIOCREQSET, &ei); |
| 467 |
} |
| 468 |
ready--; |
| 469 |
} |
| 470 |
} |
| 471 |
#endif |
| 472 |
} |
| 473 |
} while (timeout == -1 && ready == 0); |
| 474 |
|
| 475 |
return ready; |
| 476 |
} |
| 477 |
|
| 478 |
#else |
| 479 |
|
| 480 |
int |
| 481 |
__gnat_waitpid (int pid ATTRIBUTE_UNUSED, int sig ATTRIBUTE_UNUSED) |
| 482 |
{ |
| 483 |
return 0; |
| 484 |
} |
| 485 |
|
| 486 |
int |
| 487 |
__gnat_pipe (int *fd ATTRIBUTE_UNUSED) |
| 488 |
{ |
| 489 |
return -1; |
| 490 |
} |
| 491 |
|
| 492 |
int |
| 493 |
__gnat_expect_fork (void) |
| 494 |
{ |
| 495 |
return -1; |
| 496 |
} |
| 497 |
|
| 498 |
void |
| 499 |
__gnat_expect_portable_execvp (int *pid ATTRIBUTE_UNUSED, |
| 500 |
char *cmd ATTRIBUTE_UNUSED, |
| 501 |
char *argv[] ATTRIBUTE_UNUSED) |
| 502 |
{ |
| 503 |
*pid = 0; |
| 504 |
} |
| 505 |
|
| 506 |
int |
| 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) |
| 512 |
{ |
| 513 |
*dead_process = 0; |
| 514 |
return -1; |
| 515 |
} |
| 516 |
#endif |