1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296
|
/* Part of SWI-Prolog
Author: Jan Wielemaker
E-mail: J.Wielemaker@vu.nl
WWW: http://www.swi-prolog.org
Copyright (c) 2000-2022, University of Amsterdam
VU University Amsterdam
CWI, Amsterdam
SWI-Prolog Solutions b.v.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
#include <config.h>
/*#define O_DEBUG 1*/
#undef O_DEBUG
#define _GNU_SOURCE /* get pipe2() */
#include <SWI-Prolog.h>
#include <SWI-Stream.h>
#include "error.h"
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <errno.h>
#include <signal.h>
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_SYS_WAIT_H
#include <sys/wait.h>
#endif
#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_PRCTL
#include <sys/prctl.h>
#endif
#ifdef _REENTRANT
#include <pthread.h>
#endif
#ifdef HAVE_CRT_EXTERNS_H
#include <crt_externs.h> /* _NSGetEnviron() on MacOS */
#endif
static atom_t ATOM_stdin;
static atom_t ATOM_stdout;
static atom_t ATOM_stderr;
static atom_t ATOM_std;
static atom_t ATOM_null;
static atom_t ATOM_process;
static atom_t ATOM_detached;
static atom_t ATOM_cwd;
static atom_t ATOM_env;
static atom_t ATOM_environment;
static atom_t ATOM_priority;
static atom_t ATOM_window;
static atom_t ATOM_timeout;
static atom_t ATOM_release;
static atom_t ATOM_infinite;
static atom_t ATOM_text;
static atom_t ATOM_binary;
static atom_t ATOM_octet;
static atom_t ATOM_utf8;
static atom_t ATOM_ascii;
static atom_t ATOM_iso_latin_1;
static atom_t ATOM_unicode_be;
static atom_t ATOM_unicode_le;
static functor_t FUNCTOR_error2;
static functor_t FUNCTOR_process_error2;
static functor_t FUNCTOR_system_error2;
static functor_t FUNCTOR_pipe1;
static functor_t FUNCTOR_pipe2;
static functor_t FUNCTOR_stream1;
static functor_t FUNCTOR_exit1;
static functor_t FUNCTOR_killed1;
static functor_t FUNCTOR_eq2; /* =/2 */
static functor_t FUNCTOR_type1;
static functor_t FUNCTOR_encoding1;
#define MAYBE 2
#if O_DEBUG
#define DEBUG(g) g
#else
#define DEBUG(g) (void)0
#endif
#ifdef __WINDOWS__
#include <windows.h>
#include <stdio.h>
#include <fcntl.h>
#include <io.h>
#if !defined(__MINGW32__)
typedef DWORD pid_t;
#endif
typedef wchar_t echar; /* environment character */
#define ecslen(s) wcslen(s)
#ifndef CREATE_BREAKAWAY_FROM_JOB
#define CREATE_BREAKAWAY_FROM_JOB 0x1000000
#endif
#else
typedef char echar;
#define ecslen(s) strlen(s)
#endif
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
ISSUES:
- Deal with child errors (no cwd, cannot execute, etc.)
- Complete test suite
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/*******************************
* ADMIN *
*******************************/
typedef enum std_type
{ std_std,
std_null,
std_pipe,
std_stream
} std_type;
typedef struct p_stream
{ term_t term; /* P in pipe(P) */
std_type type; /* type of stream */
IOENC encoding; /* Encoding for the stream */
#ifdef __WINDOWS__
HANDLE fd[2]; /* pipe handles */
#else
int fd[2]; /* pipe handles */
#endif
int cloexec; /* close on exec activated */
} p_stream;
typedef struct ecbuf
{ echar *buffer;
size_t size;
size_t allocated;
} ecbuf;
typedef struct p_options
{ atom_t exe_name; /* exe as atom */
#ifdef __WINDOWS__
wchar_t *exe; /* Executable */
wchar_t *cmdline; /* Command line */
wchar_t *cwd; /* CWD of new process */
#else
char *exe; /* Executable */
char **argv; /* argument vector */
char *cwd; /* CWD of new process */
char **envp; /* New environment */
#endif
ecbuf envbuf; /* environment buffer */
term_t pid; /* process(PID) */
int pipes; /* #pipes found */
p_stream streams[3];
int detached; /* create as detached */
int window; /* Show a window? */
int priority; /* Process priority */
} p_options;
typedef struct wait_options
{ double timeout;
int has_timeout;
int release;
} wait_options;
typedef enum create_method
{ PCREATE_SPAWN,
PCREATE_VFORK,
PCREATE_FORK
} create_method;
static create_method create_process_method = PCREATE_SPAWN;
#ifdef __WINDOWS__
static int win_command_line(term_t t, int arity,
const wchar_t *exepath, wchar_t **cmdline);
#endif
/*******************************
* STRING BUFFER *
*******************************/
static void
free_ecbuf(ecbuf *b)
{ if ( b->buffer )
{ PL_free(b->buffer);
b->buffer = NULL;
}
}
static int
add_ecbuf(ecbuf *b, echar *data, size_t len)
{ if ( b->size + len > b->allocated )
{ size_t newsize = (b->allocated ? b->allocated * 2 : 2048);
while( b->size + len > newsize )
newsize *= 2;
if ( b->buffer )
{ b->buffer = PL_realloc(b->buffer, newsize*sizeof(echar));
} else
{ b->buffer = PL_malloc(newsize*sizeof(echar));
}
b->allocated = newsize;
}
memcpy(b->buffer+b->size, data, len*sizeof(echar));
b->size += len;
return TRUE;
}
/*******************************
* ENVIRONMENT PARSING *
*******************************/
static int
get_echars_arg_ex(int i, term_t from, term_t arg, echar **sp, size_t *lenp)
{ const echar *s, *e;
if ( !PL_get_arg(i, from, arg) )
return FALSE;
#ifdef __WINDOWS__
if ( !PL_get_wchars(arg, lenp, sp,
CVT_ATOMIC|CVT_EXCEPTION) )
#else
if ( !PL_get_nchars(arg, lenp, sp,
CVT_ATOMIC|CVT_EXCEPTION|REP_FN) )
#endif
return FALSE;
for(s = *sp, e = s+*lenp; s<e; s++)
{ if ( !*s )
return PL_domain_error("text_non_zero_code", arg);
}
return TRUE;
}
#ifdef __WINDOWS__
#define ECHARS(s) L##s
#else
#define ECHARS(s) s
#endif
static int
already_in_env(const echar *env, int count, const echar *e)
{ for(; count-- > 0; env += ecslen(env)+1)
{ const echar *s, *q;
for(s=env, q=e; *s && *q && *s == *q && *s != '=' && *q != '='; s++,q++)
;
if (*s == *q && *s == '=' )
return TRUE;
}
return FALSE;
}
static int
parse_environment(term_t t, p_options *info, int pass)
{ term_t tail = PL_copy_term_ref(t);
term_t head = PL_new_term_ref();
term_t tmp = PL_new_term_ref();
ecbuf *eb = &info->envbuf;
int count = 0;
#ifndef __WINDOWS__
echar *q;
char **ep;
int c = 0;
#endif
if ( eb->buffer )
return PL_permission_error("redefine", "environment", t);
while( PL_get_list(tail, head, tail) )
{ echar *s;
size_t len;
if ( !PL_is_functor(head, FUNCTOR_eq2) )
return PL_type_error("environment_variable", head);
if ( !get_echars_arg_ex(1, head, tmp, &s, &len) )
return FALSE;
add_ecbuf(eb, s, len);
add_ecbuf(eb, ECHARS("="), 1);
if ( !get_echars_arg_ex(2, head, tmp, &s, &len) )
return FALSE;
add_ecbuf(eb, s, len);
add_ecbuf(eb, ECHARS("\0"), 1);
count++;
}
if ( !PL_get_nil_ex(tail) )
return FALSE;
if ( pass && count == 0 )
return TRUE; /* environment([]) is a no-op */
if ( pass )
{
#ifndef __WINDOWS__
#ifdef HAVE__NSGETENVIRON
char **environ = *_NSGetEnviron();
#else
extern char **environ;
#endif
char **e;
int count0 = count;
for(e=environ; e && *e; e++)
{ if ( !already_in_env(eb->buffer, count0, *e) )
{ add_ecbuf(eb, *e, strlen(*e));
add_ecbuf(eb, ECHARS("\0"), 1);
count++;
}
}
#else /*__WINDOWS__*/
echar *env = GetEnvironmentStringsW();
if ( env )
{ int count0 = count;
while(*env)
{ size_t len = wcslen(env);
if ( !already_in_env(eb->buffer, count0, env) )
{ add_ecbuf(eb, env, len+1);
count++;
}
env += len+1;
}
}
#endif /*__WINDOWS__*/
}
#ifdef __WINDOWS__
add_ecbuf(eb, ECHARS("\0"), 1);
if ( count == 0 )
add_ecbuf(eb, ECHARS("\0"), 1);
#else
info->envp = PL_malloc((count+1)*sizeof(char*));
for(ep=info->envp, c=0, q=eb->buffer; c<count; c++, ep++)
{ *ep = q;
q += strlen(q)+1;
}
assert((size_t)(q-eb->buffer) == eb->size);
*ep = NULL;
#endif
return TRUE;
}
static int
get_type(term_t head, IOENC *enc)
{ atom_t a;
if ( PL_get_atom_ex(head, &a) )
{ if ( a == ATOM_text )
*enc = ENC_ANSI;
else if ( a == ATOM_binary )
*enc = ENC_OCTET;
else
return PL_domain_error("stream_type", head);
return TRUE;
}
return FALSE;
}
/* TBD: provide a public API for translating encoding names to
* the IOENC enum
*/
static int
get_encoding(term_t head, IOENC *enc)
{ atom_t a;
if ( PL_get_atom_ex(head, &a) )
{ IOENC e;
if ( (e=PL_atom_to_encoding(a)) != ENC_UNKNOWN )
{ *enc = e;
return TRUE;
}
return PL_domain_error("encoding", head);
}
return FALSE;
}
static int
get_stream(term_t t, p_options *info, p_stream *stream, atom_t name)
{ atom_t a;
int i;
if ( PL_get_atom(t, &a) )
{ if ( a == ATOM_null )
{ stream->type = std_null;
return TRUE;
} else if ( a == ATOM_std )
{ stream->type = std_std;
return TRUE;
} else
{ return PL_domain_error("process_stream", t);
}
} else if ( PL_is_functor(t, FUNCTOR_pipe1) ||
PL_is_functor(t, FUNCTOR_pipe2) )
{ stream->term = PL_new_term_ref();
stream->encoding = ENC_ANSI;
_PL_get_arg(1, t, stream->term);
if ( !PL_is_variable(stream->term) )
{ for (i = 0; i < info->pipes; i++)
{ if (PL_compare(info->streams[i].term, t) == 0)
break;
}
if (i == info->pipes)
return PL_uninstantiation_error(stream->term);
}
if ( PL_is_functor(t, FUNCTOR_pipe2) )
{ term_t tail = PL_new_term_ref();
term_t head = PL_new_term_ref();
_PL_get_arg(2, t, tail);
while(PL_get_list_ex(tail, head, tail))
{ if ( PL_is_functor(head, FUNCTOR_type1) )
{ _PL_get_arg(1, head, head);
if ( !get_type(head, &stream->encoding) )
return FALSE;
} else if ( PL_is_functor(head, FUNCTOR_encoding1) )
{ _PL_get_arg(1, head, head);
if ( !get_encoding(head, &stream->encoding) )
return FALSE;
} else
return PL_domain_error("pipe_option", head);
}
if ( !PL_get_nil_ex(tail) )
return FALSE;
}
stream->type = std_pipe;
info->pipes++;
return TRUE;
} else if ( PL_is_functor(t, FUNCTOR_stream1) )
{ IOSTREAM *s;
int fd;
stream->term = PL_new_term_ref();
_PL_get_arg(1, t, stream->term);
if ( !PL_get_stream(stream->term, &s,
name == ATOM_stdin ? SIO_INPUT : SIO_OUTPUT) )
return FALSE;
stream->type = std_stream;
if ( (fd = Sfileno(s)) > 0 )
{
#ifdef __WINDOWS__
stream->fd[0] = stream->fd[1] = (HANDLE)_get_osfhandle(fd);
#else
stream->fd[0] = stream->fd[1] = fd;
#endif
} else
{ return PL_domain_error("file_stream", stream->term);
}
return TRUE;
} else
return PL_type_error("process_stream", t);
}
static int
parse_options(term_t options, p_options *info)
{ term_t tail = PL_copy_term_ref(options);
term_t head = PL_new_term_ref();
term_t arg = PL_new_term_ref();
info->window = MAYBE;
while(PL_get_list(tail, head, tail))
{ atom_t name;
size_t arity;
if ( !PL_get_name_arity(head, &name, &arity) || arity != 1 )
return PL_type_error("option", head);
_PL_get_arg(1, head, arg);
if ( name == ATOM_stdin )
{ if ( !get_stream(arg, info, &info->streams[0], name) )
return FALSE;
} else if ( name == ATOM_stdout )
{ if ( !get_stream(arg, info, &info->streams[1], name) )
return FALSE;
} else if ( name == ATOM_stderr )
{ if ( !get_stream(arg, info, &info->streams[2], name) )
return FALSE;
} else if ( name == ATOM_process )
{ info->pid = PL_copy_term_ref(arg);
} else if ( name == ATOM_detached )
{ if ( !PL_get_bool_ex(arg, &info->detached) )
return FALSE;
} else if ( name == ATOM_cwd )
{
#ifdef __WINDOWS__
if ( !PL_get_wchars(arg, NULL, &info->cwd,
CVT_ATOM|CVT_STRING|CVT_EXCEPTION|BUF_MALLOC) )
return FALSE;
#else
if ( !PL_get_chars(arg, &info->cwd,
CVT_ATOM|CVT_STRING|CVT_EXCEPTION|BUF_MALLOC|REP_FN) )
return FALSE;
#endif
} else if ( name == ATOM_window )
{ if ( !PL_get_bool_ex(arg, &info->window) )
return FALSE;
} else if ( name == ATOM_env )
{ if ( !parse_environment(arg, info, FALSE) )
return FALSE;
} else if ( name == ATOM_environment )
{ if ( !parse_environment(arg, info, TRUE) )
return FALSE;
} else if ( name == ATOM_priority )
{ int tmp;
if ( !PL_get_integer_ex(arg, &tmp) )
return FALSE;
if ( tmp < -20 || tmp > 19 )
return PL_domain_error("priority_option", arg);
info->priority = tmp;
} else
return PL_domain_error("process_option", head);
}
if ( !PL_get_nil_ex(tail) )
return FALSE;
return TRUE;
}
static int
get_exe(term_t exe, p_options *info)
{ size_t arity;
term_t arg = PL_new_term_ref();
if ( !PL_get_name_arity(exe, &info->exe_name, &arity) )
return PL_type_error("callable", exe);
PL_put_atom(arg, info->exe_name);
#ifdef __WINDOWS__
if ( !PL_get_wchars(arg, NULL, &info->exe, CVT_ATOM|CVT_EXCEPTION|BUF_MALLOC) )
return FALSE;
if ( !win_command_line(exe, arity, info->exe, &info->cmdline) )
return FALSE;
#else /*__WINDOWS__*/
if ( !PL_get_chars(arg, &info->exe, CVT_ATOM|CVT_EXCEPTION|BUF_MALLOC|REP_FN) )
return FALSE;
if ( !(info->argv = PL_malloc((arity+2)*sizeof(char*))) )
return PL_resource_error("memory");
memset(info->argv, 0, (arity+2)*sizeof(char*));
if ( !(info->argv[0] = PL_malloc(strlen(info->exe)+1)) )
return PL_resource_error("memory");
strcpy(info->argv[0], info->exe);
{ size_t i;
for(i=1; i<=arity; i++)
{ _PL_get_arg(i, exe, arg);
if ( !PL_get_chars(arg, &info->argv[i],
CVT_ATOMIC|CVT_EXCEPTION|BUF_MALLOC|REP_FN) )
return FALSE;
}
info->argv[i] = NULL;
}
#endif /*__WINDOWS__*/
return TRUE;
}
static void
free_options(p_options *info) /* TBD: close streams */
{ if ( info->exe )
{ PL_free(info->exe);
info->exe = NULL;
}
if ( info->cwd )
{ PL_free(info->cwd);
info->cwd = NULL;
}
#ifndef __WINDOWS__
if ( info->envp )
{ PL_free(info->envp);
info->envp = NULL;
}
#endif
free_ecbuf(&info->envbuf);
#ifdef __WINDOWS__
if ( info->cmdline )
{ PL_free(info->cmdline);
info->cmdline = NULL;
}
#else /*__WINDOWS__*/
if ( info->argv )
{ char **a;
for(a=info->argv; *a; a++)
{ if ( *a )
PL_free(*a);
}
PL_free(info->argv);
info->argv = NULL;
}
#endif /*__WINDOWS__*/
}
/*******************************
* PROCESS READS *
*******************************/
#define PROCESS_MAGIC 0x29498001
typedef struct process_context
{ int magic; /* PROCESS_MAGIC */
#ifdef __WINDOWS__
HANDLE handle; /* process handle */
#else
pid_t pid; /* the process id */
#endif
int open_mask; /* Open streams */
int pipes[3]; /* stdin/stdout/stderr */
atom_t exe_name; /* exe as atom */
} process_context;
static int wait_for_process(process_context *pc);
static int
process_fd(void *handle, process_context **PC)
{ process_context *pc = (process_context*) ((uintptr_t)handle&~(uintptr_t)0x3);
int pipe = (int)(uintptr_t)handle & 0x3;
if ( pc->magic == PROCESS_MAGIC )
{ if ( PC )
*PC = pc;
return pc->pipes[pipe];
}
return -1;
}
static ssize_t
Sread_process(void *handle, char *buf, size_t size)
{ int fd = process_fd(handle, NULL);
return (*Sfilefunctions.read)((void*)(uintptr_t)fd, buf, size);
}
static ssize_t
Swrite_process(void *handle, char *buf, size_t size)
{ int fd = process_fd(handle, NULL);
return (*Sfilefunctions.write)((void*)(uintptr_t)fd, buf, size);
}
static int
Sclose_process(void *handle)
{ process_context *pc;
int fd = process_fd(handle, &pc);
if ( fd >= 0 )
{ int which = (int)(uintptr_t)handle & 0x3;
int rc;
rc = (*Sfilefunctions.close)((void*)(uintptr_t)fd);
pc->open_mask &= ~(1<<which);
DEBUG(Sdprintf("Closing fd[%d]; mask = 0x%x\n", which, pc->open_mask));
if ( !pc->open_mask )
{ int rcw = wait_for_process(pc);
return rcw ? 0 : -1;
}
return rc;
}
return -1;
}
static int
Scontrol_process(void *handle, int action, void *arg)
{ process_context *pc;
int fd = process_fd(handle, &pc);
switch(action)
{ case SIO_GETFILENO:
{ int *fdp = arg;
*fdp = fd;
return 0;
}
default:
return (*Sfilefunctions.control)((void*)(uintptr_t)fd, action, arg);
}
}
static IOFUNCTIONS Sprocessfunctions =
{ Sread_process,
Swrite_process,
NULL, /* seek */
Sclose_process,
Scontrol_process,
NULL /* seek64 */
};
static IOSTREAM *
open_process_pipe(process_context *pc, p_options *info, int which, int fdn)
{ void *handle;
#ifdef __WINDOWS__
HANDLE fd = info->streams[which].fd[fdn];
#else
int fd = info->streams[which].fd[fdn];
#endif
int flags = SIO_RECORDPOS|SIO_FBUF;
IOSTREAM *s;
pc->open_mask |= (1<<which);
#ifdef __WINDOWS__
pc->pipes[which] = _open_osfhandle((intptr_t)fd, _O_BINARY);
#else
pc->pipes[which] = fd;
#endif
if ( info->streams[which].encoding != ENC_OCTET )
flags |= SIO_TEXT;
if ( which == 0 )
flags |= SIO_OUTPUT;
else
flags |= SIO_INPUT;
handle = (void *)((uintptr_t)pc | (uintptr_t)which);
if ( (s=Snew(handle, flags, &Sprocessfunctions)) )
s->encoding = info->streams[which].encoding;
return s;
}
/*******************************
* OS STUFF *
*******************************/
#ifdef __WINDOWS__
CRITICAL_SECTION process_lock;
#define LOCK() EnterCriticalSection(&process_lock);
#define UNLOCK() LeaveCriticalSection(&process_lock);
/* We need a root job to put non-detached processes into */
static HANDLE rootJob = (HANDLE)0;
static IOFUNCTIONS Sprocessfilefunctions = { 0, };
static void
win_init(void)
{ InitializeCriticalSection(&process_lock);
JOBOBJECT_EXTENDED_LIMIT_INFORMATION jeli;
rootJob = CreateJobObject(NULL, NULL);
if (rootJob == NULL) /* Already in a job */
return;
memset(&jeli, 0, sizeof(jeli));
jeli.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE;
/* This will fail (silently) if we dont have permission to manage processes */
SetInformationJobObject(rootJob, JobObjectExtendedLimitInformation,
&jeli, sizeof(jeli));
memcpy(&Sprocessfilefunctions, &Sfilefunctions, sizeof(IOFUNCTIONS));
Sprocessfilefunctions.seek = NULL;
Sprocessfilefunctions.seek64 = NULL;
}
#include "win_error.c"
typedef struct arg_string
{ size_t len;
wchar_t *text;
wchar_t quote;
} arg_string;
#define QMISC 0x1
#define QDBLQ 0x2
#define QSBLQ 0x4
static int
set_quote(arg_string *as)
{ int needq = 0;
const wchar_t *s = as->text;
for(; *s; s++)
{ if ( !iswalnum(*s) )
{ if ( *s == '"' )
needq |= QDBLQ;
else if ( *s == '\'' )
needq |= QSBLQ;
else
needq |= QMISC;
}
}
if ( !needq )
{ as->quote = 0;
return TRUE;
}
needq &= ~QMISC;
switch( needq )
{ case QDBLQ:
as->quote = '\'';
return TRUE;
case 0:
case QSBLQ:
as->quote = '"';
return TRUE;
default:
return FALSE;
}
}
static int
win_command_line(term_t t, int arity, const wchar_t *exe, wchar_t **cline)
{ if ( arity > 0 )
{ arg_string *av = PL_malloc((arity+1)*sizeof(*av));
term_t arg = PL_new_term_ref();
size_t cmdlen;
wchar_t *cmdline, *o;
const wchar_t *b;
int i;
if ( (b=wcsrchr(exe, '\\')) )
b++;
else
b = exe;
av[0].text = (wchar_t*)b;
av[0].len = wcslen(av[0].text);
set_quote(&av[0]);
cmdlen = av[0].len+(av[0].quote?2:0)+1;
for( i=1; i<=arity; i++)
{ _PL_get_arg(i, t, arg);
if ( !PL_get_wchars(arg, &av[i].len, &av[i].text,
CVT_ATOMIC|CVT_EXCEPTION|BUF_MALLOC) )
return FALSE;
if ( wcslen(av[i].text) != av[i].len )
return PL_domain_error("no_zero_code_atom", arg);
if ( !set_quote(&av[i]) )
return PL_domain_error("dos_quotable_atom", arg);
cmdlen += av[i].len+(av[i].quote?2:0)+1;
}
cmdline = PL_malloc(cmdlen*sizeof(wchar_t));
for( o=cmdline,i=0; i<=arity; )
{ wchar_t *s = av[i].text;
if ( av[i].quote )
*o++ = av[i].quote;
wcsncpy(o, s, av[i].len);
o += av[i].len;
if ( i > 0 )
PL_free(s); /* do not free shared exename */
if ( av[i].quote )
*o++ = av[i].quote;
if (++i <= arity)
*o++ = ' ';
}
*o = 0;
PL_free(av);
*cline = cmdline;
} else
{ *cline = NULL;
}
return TRUE;
}
typedef struct win_process
{ DWORD pid;
HANDLE handle;
HANDLE job;
struct win_process *next;
} win_process;
static win_process *processes;
static void
register_process(DWORD pid, HANDLE h, HANDLE job)
{ win_process *wp = PL_malloc(sizeof(*wp));
wp->pid = pid;
wp->handle = h;
wp->job = job;
LOCK();
wp->next = processes;
processes = wp;
UNLOCK();
}
static int
unregister_process(DWORD pid)
{ win_process **wpp, *wp;
LOCK();
for(wpp=&processes, wp=*wpp; wp; wpp=&wp->next, wp=*wpp)
{ if ( wp->pid == pid )
{ *wpp = wp->next;
PL_free(wp);
UNLOCK();
return TRUE;
}
}
UNLOCK();
return FALSE;
}
static HANDLE
find_process_from_pid(DWORD pid, const char *pred)
{ win_process *wp;
LOCK();
for(wp=processes; wp; wp=wp->next)
{ if ( wp->pid == pid )
{ HANDLE h = wp->handle;
UNLOCK();
return h;
}
}
UNLOCK();
if ( pred )
{ term_t ex = PL_new_term_ref();
if ( PL_put_integer(ex, pid) )
PL_existence_error("process", ex);
}
return (HANDLE)0;
}
static HANDLE
find_job_from_pid(DWORD pid, const char *pred)
{ win_process *wp;
LOCK();
for(wp=processes; wp; wp=wp->next)
{ if ( wp->pid == pid )
{ HANDLE h = wp->job;
UNLOCK();
return h;
}
}
UNLOCK();
if ( pred )
{ term_t ex = PL_new_term_ref();
if ( PL_put_integer(ex, pid) )
PL_existence_error("process", ex);
}
return (HANDLE)0;
}
#define WP_TIMEOUT 2
static int
wait_process_handle(HANDLE process, ULONG *rc, DWORD timeout)
{ DWORD wc;
retry:
wc = MsgWaitForMultipleObjects(1,
&process,
FALSE, /* return on any event */
timeout,
QS_ALLINPUT);
switch(wc)
{ case WAIT_OBJECT_0:
if ( !GetExitCodeProcess(process, rc) )
{ win_error("GetExitCodeProcess");
CloseHandle(process);
return FALSE;
}
CloseHandle(process);
return TRUE;
case WAIT_OBJECT_0+1:
{ MSG msg;
while( PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) )
{ TranslateMessage(&msg);
DispatchMessage(&msg);
if ( PL_handle_signals() < 0 )
return FALSE;
}
goto retry;
}
case WAIT_TIMEOUT:
return WP_TIMEOUT;
default:
win_error("WaitForSingleObject");
CloseHandle(process);
return FALSE;
}
}
static int
wait_for_pid(pid_t pid, term_t code, wait_options *opts)
{ HANDLE *h;
if ( (h=find_process_from_pid(pid, "process_wait")) )
{ ULONG rc;
DWORD timeout;
int wc;
if ( opts->has_timeout )
timeout = (DWORD)(opts->timeout * 1000.0);
else
timeout = INFINITE;
if ( !(wc=wait_process_handle(h, &rc, timeout)) )
return FALSE;
if ( wc == WP_TIMEOUT )
return PL_unify_atom(code, ATOM_timeout);
unregister_process(pid);
return PL_unify_term(code,
PL_FUNCTOR, FUNCTOR_exit1,
PL_LONG, rc);
} else
{ return FALSE;
}
}
static int
win_wait_success(atom_t exe, HANDLE process)
{ ULONG rc;
if ( !wait_process_handle(process, &rc, INFINITE) )
return FALSE;
if ( rc != 0 )
{ term_t ex = PL_new_term_ref();
if ( PL_unify_term(ex,
PL_FUNCTOR, FUNCTOR_error2,
PL_FUNCTOR, FUNCTOR_process_error2,
PL_ATOM, exe,
PL_FUNCTOR, FUNCTOR_exit1,
PL_LONG, rc,
PL_VARIABLE) )
return PL_raise_exception(ex);
return FALSE;
}
return TRUE;
}
static int
wait_for_process(process_context *pc)
{ int rc = win_wait_success(pc->exe_name, pc->handle);
PL_unregister_atom(pc->exe_name);
PL_free(pc);
return rc;
}
static int
create_pipes(p_options *info)
{ int i;
SECURITY_ATTRIBUTES sa;
sa.nLength = sizeof(sa); /* Length in bytes */
sa.bInheritHandle = 1; /* the child must inherit these handles */
sa.lpSecurityDescriptor = NULL;
for(i=0; i<3; i++)
{ p_stream *s = &info->streams[i];
if ( s->term && s->type == std_pipe )
{ if ( i == 2 && info->streams[1].term &&
PL_compare(info->streams[1].term, info->streams[2].term) == 0 )
{ s->fd[0] = info->streams[1].fd[0];
s->fd[1] = info->streams[1].fd[1];
} else
{ if ( !CreatePipe(&s->fd[0], &s->fd[1], &sa, 1<<13) )
{ return win_error("CreatePipe");
}
}
}
}
return TRUE;
}
static IOSTREAM *
Sopen_handle(HANDLE h, const char *mode)
{ IOSTREAM *s = Sfdopen(_open_osfhandle((intptr_t)h, _O_BINARY), mode);
if ( s->functions == &Sfilefunctions )
{ s->functions = &Sprocessfilefunctions;
}
return s;
}
static HANDLE
open_null_stream(DWORD access)
{ SECURITY_ATTRIBUTES sa;
sa.nLength = sizeof(sa); /* Length in bytes */
sa.bInheritHandle = 1; /* the child must inherit these handles */
sa.lpSecurityDescriptor = NULL;
return CreateFile("nul",
access,
FILE_SHARE_READ|FILE_SHARE_WRITE,
&sa, /* security */
OPEN_EXISTING,
0,
NULL);
}
static int
console_app(void)
{ HANDLE h;
if ( (h = GetStdHandle(STD_OUTPUT_HANDLE)) != INVALID_HANDLE_VALUE )
{ DWORD mode;
if ( GetConsoleMode(h, &mode) )
return TRUE;
}
return FALSE;
}
static int
do_create_process(p_options *info)
{ int flags = 0;
PROCESS_INFORMATION pi;
STARTUPINFOW si;
switch(info->window)
{ case MAYBE:
if ( !console_app() )
flags |= CREATE_NO_WINDOW;
break;
case TRUE:
break;
case FALSE:
flags |= CREATE_NO_WINDOW;
break;
}
if ( info->detached )
flags |= CREATE_BREAKAWAY_FROM_JOB;
if ( info->envbuf.buffer )
flags |= CREATE_UNICODE_ENVIRONMENT;
memset(&si, 0, sizeof(si));
si.cb = sizeof(si);
si.dwFlags = STARTF_USESTDHANDLES;
/* stdin */
switch( info->streams[0].type )
{ case std_pipe:
case std_stream:
si.hStdInput = info->streams[0].fd[0];
SetHandleInformation(info->streams[0].fd[1],
HANDLE_FLAG_INHERIT, FALSE);
break;
case std_null:
si.hStdInput = open_null_stream(GENERIC_READ);
break;
case std_std:
si.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
break;
}
/* stdout */
switch( info->streams[1].type )
{ case std_pipe:
case std_stream:
si.hStdOutput = info->streams[1].fd[1];
SetHandleInformation(info->streams[1].fd[0],
HANDLE_FLAG_INHERIT, FALSE);
break;
case std_null:
si.hStdOutput = open_null_stream(GENERIC_WRITE);
break;
case std_std:
si.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
break;
}
/* stderr */
switch( info->streams[2].type )
{ case std_pipe:
case std_stream:
si.hStdError = info->streams[2].fd[1];
SetHandleInformation(info->streams[2].fd[0],
HANDLE_FLAG_INHERIT, FALSE);
break;
case std_null:
si.hStdError = open_null_stream(GENERIC_WRITE);
break;
case std_std:
si.hStdError = GetStdHandle(STD_ERROR_HANDLE);
break;
}
if ( CreateProcessW(info->exe,
info->cmdline,
NULL, /* Process security */
NULL, /* Thread security */
TRUE, /* Inherit handles */
flags, /* Creation flags */
info->envbuf.buffer, /* Environment */
info->cwd, /* Directory */
&si, /* Startup info */
&pi) ) /* Process information */
{ int rc = TRUE;
HANDLE hJob = (HANDLE)0;
if ( !info->detached )
{
hJob = rootJob;
}
CloseHandle(pi.hThread);
if ( info->pipes > 0 && info->pid == 0 )
{ IOSTREAM *s;
process_context *pc = PL_malloc(sizeof(*pc));
DEBUG(Sdprintf("Wait on pipes\n"));
memset(pc, 0, sizeof(*pc));
pc->magic = PROCESS_MAGIC;
pc->handle = pi.hProcess;
pc->exe_name = info->exe_name;
PL_register_atom(pc->exe_name);
if ( info->streams[0].type == std_pipe )
{ CloseHandle(info->streams[0].fd[0]);
if ( (s = open_process_pipe(pc, info, 0, 1)) )
rc = PL_unify_stream(info->streams[0].term, s);
else
CloseHandle(info->streams[0].fd[1]);
}
if ( info->streams[1].type == std_pipe )
{ CloseHandle(info->streams[1].fd[1]);
if ( rc && (s = open_process_pipe(pc, info, 1, 0)) )
PL_unify_stream(info->streams[1].term, s);
else
CloseHandle(info->streams[1].fd[0]);
}
if ( info->streams[2].type == std_pipe &&
( !info->streams[1].term || PL_compare(info->streams[1].term, info->streams[2].term) != 0 ) )
{ CloseHandle(info->streams[2].fd[1]);
if ( rc && (s = open_process_pipe(pc, info, 2, 0)) )
rc = PL_unify_stream(info->streams[2].term, s);
else
CloseHandle(info->streams[2].fd[0]);
}
return rc;
} else if ( info->pipes > 0 )
{ IOSTREAM *s;
if ( info->streams[0].type == std_pipe )
{ CloseHandle(info->streams[0].fd[0]);
if ( (s = Sopen_handle(info->streams[0].fd[1], "w")) )
rc = PL_unify_stream(info->streams[0].term, s);
else
CloseHandle(info->streams[0].fd[1]);
}
if ( info->streams[1].type == std_pipe )
{ CloseHandle(info->streams[1].fd[1]);
if ( rc && (s = Sopen_handle(info->streams[1].fd[0], "r")) )
rc = PL_unify_stream(info->streams[1].term, s);
else
CloseHandle(info->streams[1].fd[0]);
}
if ( info->streams[2].type == std_pipe &&
( !info->streams[1].term || PL_compare(info->streams[1].term, info->streams[2].term) != 0 ) )
{ CloseHandle(info->streams[2].fd[1]);
if ( rc && (s = Sopen_handle(info->streams[2].fd[0], "r")) )
rc = PL_unify_stream(info->streams[2].term, s);
else
CloseHandle(info->streams[2].fd[0]);
}
}
if ( !rc )
{ Sdprintf("FATAL ERROR: create_process/3\n");
PL_halt(1);
}
if ( info->detached )
{ hJob = CreateJobObject(NULL, NULL);
AssignProcessToJobObject(hJob, pi.hProcess);
}
else
{ AssignProcessToJobObject(rootJob, pi.hProcess);
}
if ( info->pid )
{ register_process(pi.dwProcessId, pi.hProcess, hJob);
return PL_unify_integer(info->pid, pi.dwProcessId);
}
return win_wait_success(info->exe_name, pi.hProcess);
} else
{ return win_error("CreateProcess");
}
}
#else /*__WINDOWS__*/
#ifndef HAVE_PRCTL
#ifdef _REENTRANT
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
#define LOCK() pthread_mutex_lock(&mutex)
#define UNLOCK() pthread_mutex_unlock(&mutex)
#else
#define LOCK() {}
#define UNLOCK() {}
#endif
typedef struct posix_process
{ pid_t pid;
struct posix_process *next;
} posix_process;
static posix_process *processes = NULL;
static void
register_process(pid_t pid)
{ posix_process *pp = PL_malloc(sizeof(*pp));
pp->pid = pid;
LOCK();
pp->next = processes;
processes = pp;
UNLOCK();
}
static int
unregister_process(pid_t pid)
{ posix_process **ppp, *pp;
LOCK();
for(ppp=&processes, pp=*ppp; pp; ppp=&pp->next, pp=*ppp)
{ if ( pp->pid == pid )
{ *ppp = pp->next;
PL_free(pp);
UNLOCK();
return TRUE;
}
}
UNLOCK();
return FALSE;
}
static int
kill_all_processes(int rc, void* arg)
{ posix_process* pp;
for(pp=processes; pp; pp=pp->next)
kill(pp->pid, SIGTERM);
return 0;
}
static void
posix_init()
{ PL_exit_hook(kill_all_processes, NULL);
}
#endif
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Note the descriptors created using pipe() are inherited by the child.
This implies that if two threads call process_create/3 using pipes, the
pipe created for one thread may also end up in the child created by
another thread. We can avoid this using FD_CLOEXEC on the pipe's
descriptor. Note that we can do that on both because the flag will be
cleared on the duplicated descriptor after dup2 (which is executed
before the exec, so the descriptors are still valid).
This can be implemented safely on systems that have pipe2(). On other
systems, safety can be enhanced by reducing the window using fcntl(),
but this needs to be synced with fork() in other threads.
(*) It seems that some systems implement the function, but the function
returns ENOSYS to indicate it is not implemented. Hence, we need to go
for a runtime solution.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
static int
create_pipes(p_options *info)
{ int i;
for(i=0; i<3; i++)
{ p_stream *s = &info->streams[i];
if ( s->term && s->type == std_pipe )
{ if ( i == 2 && info->streams[1].term &&
PL_compare(info->streams[1].term, info->streams[2].term) == 0 )
{ s->fd[0] = info->streams[1].fd[0];
s->fd[1] = info->streams[1].fd[1];
} else
{ int my_side;
#ifdef HAVE_PIPE2
if ( pipe2(s->fd, O_CLOEXEC) == 0 )
{ s->cloexec = TRUE;
continue;
} else if ( errno != ENOSYS ) /* See (*) */
{ if ( errno != EMFILE )
Sdprintf("pipe2(): unexpected error: %s\n", strerror(errno));
return PL_resource_error("open_files");
}
#endif
if ( pipe(s->fd) )
{ if ( errno != EMFILE )
Sdprintf("pipe(): unexpected error: %s\n", strerror(errno));
return PL_resource_error("open_files");
}
my_side = (i == 0 ? s->fd[1] : s->fd[0]);
#ifdef F_SETFD
if ( fcntl(my_side, F_SETFD, FD_CLOEXEC) == 0 )
s->cloexec = TRUE;
#endif
}
} else if ( s->term && s->type == std_stream )
{ if ( fcntl(s->fd[0], F_SETFD, FD_CLOEXEC) == 0 )
s->cloexec = TRUE;
}
}
return TRUE;
}
static int
unify_exit_status(term_t code, int status)
{ if ( WIFEXITED(status) )
{ return PL_unify_term(code,
PL_FUNCTOR, FUNCTOR_exit1,
PL_INT, (int)WEXITSTATUS(status));
} else if ( WIFSIGNALED(status) )
{ return PL_unify_term(code,
PL_FUNCTOR, FUNCTOR_killed1,
PL_INT, (int)WTERMSIG(status));
} else
{ assert(0);
return FALSE;
}
}
static int
wait_for_pid(pid_t pid, term_t code, wait_options *opts)
{ pid_t p2;
int status;
if ( opts->has_timeout && opts->timeout == 0.0 )
{ if ( (p2=waitpid(pid, &status, WNOHANG)) == pid )
{
#ifndef HAVE_PRCTL
unregister_process(pid);
#endif
return unify_exit_status(code, status);
}
else if ( p2 == 0 )
return PL_unify_atom(code, ATOM_timeout);
else
{ term_t PID;
error:
return ((PID = PL_new_term_ref()) &&
PL_put_integer(PID, pid) &&
pl_error(NULL, 0, "waitpid", ERR_ERRNO,
errno, "wait", "process", PID));
}
}
for(;;)
{ if ( (p2=waitpid(pid, &status, 0)) == pid )
return unify_exit_status(code, status);
if ( p2 == -1 && errno == EINTR )
{ if ( PL_handle_signals() < 0 )
return FALSE;
} else
{ goto error;
}
}
}
static int
wait_success(atom_t name, pid_t pid)
{ pid_t p2;
for(;;)
{ int status;
if ( (p2=waitpid(pid, &status, 0)) == pid )
{ if ( WIFEXITED(status) && WEXITSTATUS(status) == 0 )
{
#ifndef HAVE_PRCTL
unregister_process(pid);
#endif
return TRUE;
} else
{ term_t code, ex;
if ( (code = PL_new_term_ref()) &&
(ex = PL_new_term_ref()) &&
unify_exit_status(code, status) &&
PL_unify_term(ex,
PL_FUNCTOR, FUNCTOR_error2,
PL_FUNCTOR, FUNCTOR_process_error2,
PL_ATOM, name,
PL_TERM, code,
PL_VARIABLE) )
return PL_raise_exception(ex);
return FALSE;
}
}
if ( p2 == -1 && errno == EINTR )
{ if ( PL_handle_signals() < 0 )
return FALSE;
}
}
}
static int
wait_for_process(process_context *pc)
{ int rc = wait_success(pc->exe_name, pc->pid);
PL_unregister_atom(pc->exe_name);
PL_free(pc);
return rc;
}
static int
close_ok(int fd)
{ int rc;
do
{ rc = close(fd);
} while ( rc == -1 && errno == EINTR );
DEBUG(if ( rc == -1 )
perror("close"));
return rc;
}
static IOSTREAM *
p_fdopen(p_options *info, int which, int fdn, char *mode)
{ IOSTREAM *s;
char m[10];
char *mp = m;
*mp++ = mode[0];
if ( info->streams[which].encoding == ENC_OCTET )
*mp++ = 'b';
*mp = 0;
if ( (s=Sfdopen(info->streams[which].fd[fdn], m)) )
s->encoding = info->streams[which].encoding;
return s;
}
static int
process_parent_side(p_options *info, int pid)
{ int rc = TRUE;
if ( info->pipes > 0 && info->pid == 0 )
{ IOSTREAM *s; /* no pid(Pid): wait */
process_context *pc = PL_malloc(sizeof(*pc));
DEBUG(Sdprintf("Wait on pipes\n"));
memset(pc, 0, sizeof(*pc));
pc->magic = PROCESS_MAGIC;
pc->pid = pid;
pc->exe_name = info->exe_name;
PL_register_atom(pc->exe_name);
if ( info->streams[0].type == std_pipe )
{ close_ok(info->streams[0].fd[0]);
if ( (s = open_process_pipe(pc, info, 0, 1)) )
rc = PL_unify_stream(info->streams[0].term, s);
else
close_ok(info->streams[0].fd[1]);
}
if ( info->streams[1].type == std_pipe )
{ close_ok(info->streams[1].fd[1]);
if ( rc && (s = open_process_pipe(pc, info, 1, 0)) )
rc = PL_unify_stream(info->streams[1].term, s);
else
close_ok(info->streams[1].fd[0]);
}
if ( info->streams[2].type == std_pipe &&
( !info->streams[1].term || PL_compare(info->streams[1].term, info->streams[2].term) != 0 ) )
{ close_ok(info->streams[2].fd[1]);
if ( rc && (s = open_process_pipe(pc, info, 2, 0)) )
rc = PL_unify_stream(info->streams[2].term, s);
else
close_ok(info->streams[2].fd[0]);
}
return rc;
} else if ( info->pipes > 0 )
{ IOSTREAM *s;
if ( info->streams[0].type == std_pipe )
{ close_ok(info->streams[0].fd[0]);
if ( (s = p_fdopen(info, 0, 1, "w")) )
rc = PL_unify_stream(info->streams[0].term, s);
else
close_ok(info->streams[0].fd[1]);
}
if ( info->streams[1].type == std_pipe )
{ close_ok(info->streams[1].fd[1]);
if ( rc && (s = p_fdopen(info, 1, 0, "r")) )
rc = PL_unify_stream(info->streams[1].term, s);
else
close_ok(info->streams[1].fd[0]);
}
if ( info->streams[2].type == std_pipe &&
( !info->streams[1].term || PL_compare(info->streams[1].term, info->streams[2].term) != 0 ) )
{ close_ok(info->streams[2].fd[1]);
if ( rc && (s = p_fdopen(info, 2, 0, "r")) )
PL_unify_stream(info->streams[2].term, s);
else
close_ok(info->streams[2].fd[0]);
}
}
assert(rc); /* What else? */
#ifndef HAVE_PRCTL /* Then we must do this manually */
if ( !info->detached )
register_process(pid);
#endif
if ( info->pid )
return PL_unify_integer(info->pid, pid);
return wait_success(info->exe_name, pid);
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
If profiling is used, fork() may not work succeed as it is interrupted
before completion. This problem was diagnosed by Edward Schwartz. It
seems to affect at least Ubuntu 18.04, but not later versions.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
#if defined(SIGPROF) && defined(HAVE_SIGPROCMASK)
static void
blockSignal(int sig, sigset_t *old)
{ sigset_t set;
sigemptyset(&set);
sigaddset(&set, sig);
sigprocmask(SIG_BLOCK, &set, old);
}
static void
restoreSignals(sigset_t *old)
{ sigprocmask(SIG_SETMASK, old, NULL);
}
#else
#define blockSignal(sig, old) (void)set
#define restoreSignals(set) (void)0
#endif
#ifndef HAVE_VFORK
#define vfork fork
#endif
static int
do_create_process_fork(p_options *info, create_method method)
{ int pid;
sigset_t set;
blockSignal(SIGPROF, &set);
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
if ( method == PCREATE_VFORK )
pid = vfork(); /* As long as it is there we use it */
else
pid = fork();
if ( pid != 0 ) /* parent */
restoreSignals(&set);
#pragma GCC diagnostic pop
if ( pid == 0 ) /* child */
{ int fd;
PL_cleanup_fork();
#if defined(HAVE_SYS_RESOURCE_H) && defined(PRIO_PROCESS)
if ( info->priority != 255 )
setpriority(PRIO_PROCESS, pid, info->priority);
#endif
if ( info->detached )
{ setsid();
#ifdef HAVE_SETPGID
setpgid(pid, pid);
#elif defined(HAVE_SETPGRP)
#ifdef SETPGRP_VOID
setpgrp();
#else
setpgrp(pid, pid);
#endif
#endif
} else
{
#ifdef HAVE_PRCTL
prctl(PR_SET_PDEATHSIG, SIGTERM);
#endif
}
if ( info->cwd )
{ if ( chdir(info->cwd) )
{ perror(info->cwd);
exit(1);
}
}
/* stdin */
switch( info->streams[0].type )
{ case std_pipe:
case std_stream:
dup2(info->streams[0].fd[0], 0);
if ( !info->streams[0].cloexec )
close(info->streams[0].fd[1]);
break;
case std_null:
if ( (fd = open("/dev/null", O_RDONLY)) >= 0 )
dup2(fd, 0);
break;
case std_std:
{ int fd = Sfileno(Suser_input);
if ( fd > 0 )
dup2(fd, 0);
break;
}
}
/* stdout */
switch( info->streams[1].type )
{ case std_pipe:
case std_stream:
dup2(info->streams[1].fd[1], 1);
if ( !info->streams[1].cloexec )
close(info->streams[1].fd[0]);
break;
case std_null:
if ( (fd = open("/dev/null", O_WRONLY)) >= 0 )
dup2(fd, 1);
break;
case std_std:
{ int fd = Sfileno(Suser_output);
if ( fd >= 0 && fd != 1 )
dup2(fd, 1);
break;
}
}
/* stderr */
switch( info->streams[2].type )
{ case std_pipe:
case std_stream:
dup2(info->streams[2].fd[1], 2);
if ( !info->streams[2].cloexec )
close(info->streams[2].fd[0]);
break;
case std_null:
if ( (fd = open("/dev/null", O_WRONLY)) >= 0 )
dup2(fd, 2);
break;
case std_std:
{ int fd = Sfileno(Suser_error);
if ( fd >= 0 && fd != 2 )
dup2(fd, 2);
break;
}
}
if ( info->envp )
{ execve(info->exe, info->argv, info->envp);
} else
{
#ifdef HAVE__NSGETENVIRON
char **environ = *_NSGetEnviron();
#else
extern char **environ;
#endif
execve(info->exe, info->argv, environ);
}
perror(info->exe);
exit(1);
} else if ( pid < 0 ) /* parent */
{ term_t exe = PL_new_term_ref();
PL_put_atom_chars(exe, info->exe);
return pl_error(NULL, 0, "fork", ERR_ERRNO, errno, "fork", "process", exe);
} else
{ return process_parent_side(info, pid);
}
}
#ifdef HAVE_POSIX_SPAWN
#include <spawn.h>
static int
do_create_process(p_options *info)
{ pid_t pid;
posix_spawn_file_actions_t file_actions;
posix_spawnattr_t attr;
int rc;
if ( info->cwd || create_process_method != PCREATE_SPAWN )
return do_create_process_fork(info, create_process_method);
posix_spawn_file_actions_init(&file_actions);
posix_spawnattr_init(&attr);
if ( info->detached )
posix_spawnattr_setpgroup(&attr, 0);
switch( info->streams[0].type ) /* stdin */
{ case std_pipe:
case std_stream:
posix_spawn_file_actions_adddup2(&file_actions, info->streams[0].fd[0], 0);
if ( !info->streams[0].cloexec )
posix_spawn_file_actions_addclose(&file_actions, info->streams[0].fd[1]);
break;
case std_null:
posix_spawn_file_actions_addopen(&file_actions, 0,
"/dev/null", O_RDONLY, 0);
break;
case std_std:
break;
}
/* stdout */
switch( info->streams[1].type )
{ case std_pipe:
case std_stream:
posix_spawn_file_actions_adddup2(&file_actions, info->streams[1].fd[1], 1);
if ( !info->streams[1].cloexec )
posix_spawn_file_actions_addclose(&file_actions, info->streams[1].fd[0]);
break;
case std_null:
posix_spawn_file_actions_addopen(&file_actions, 1,
"/dev/null", O_WRONLY, 0);
break;
case std_std:
break;
}
/* stderr */
switch( info->streams[2].type )
{ case std_pipe:
case std_stream:
posix_spawn_file_actions_adddup2(&file_actions, info->streams[2].fd[1], 2);
if ( !info->streams[2].cloexec )
posix_spawn_file_actions_addclose(&file_actions, info->streams[2].fd[0]);
break;
case std_null:
posix_spawn_file_actions_addopen(&file_actions, 2,
"/dev/null", O_WRONLY, 0);
break;
case std_std:
break;
}
rc = posix_spawn(&pid, info->exe,
&file_actions, &attr,
info->argv, info->envp);
posix_spawn_file_actions_destroy(&file_actions);
posix_spawnattr_destroy(&attr);
if ( rc == 0 )
{ return process_parent_side(info, pid);
} else
{ term_t exe = PL_new_term_ref();
PL_put_atom_chars(exe, info->exe);
return pl_error(NULL, 0, "spawn", ERR_ERRNO, errno, "fork", "process", exe);
}
}
#else /*HAVE_POSIX_SPAWN*/
static int
do_create_process(p_options *info)
{ return do_create_process_fork(info, create_process_method);
}
#endif /*HAVE_POSIX_SPAWN*/
#endif /*__WINDOWS__*/
/*******************************
* BINDING *
*******************************/
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Running out of resources after we created the process is really hard to
handle gracefully, so we first make a list to ensure we have enough
resources to bind the return value.
Ideally, we need a call to ask for sufficient resources.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
static int
ensure_stack_resources(int count)
{ fid_t fid = PL_open_foreign_frame();
term_t list = PL_new_term_ref();
term_t tail = PL_copy_term_ref(list);
while ( count-- > 0 )
{ term_t head;
if ( !(head = PL_new_term_ref()) ||
!PL_unify_list(tail, head, tail) )
{ PL_close_foreign_frame(fid);
return FALSE;
}
}
PL_discard_foreign_frame(fid);
return TRUE;
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Basic process creation interface takes
* Exe file
* List of arguments
* standard streams % std, null, pipe(S)
* Working directory
* detached
* window % Windows
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
static foreign_t
process_create(term_t exe, term_t options)
{ p_options info;
int rc = FALSE;
if ( !ensure_stack_resources(10) ) /* max 3 stream structures */
return FALSE;
memset(&info, 0, sizeof(info));
info.priority = 255; /* zero is a valid priority */
if ( !get_exe(exe, &info) )
goto out;
if ( !parse_options(options, &info) )
goto out;
if ( !create_pipes(&info) )
goto out;
rc = do_create_process(&info);
out:
free_options(&info);
return rc;
}
static int
get_pid(term_t pid, pid_t *p)
{ int n;
if ( !PL_get_integer_ex(pid, &n) )
return FALSE;
if ( n < 0 )
return PL_domain_error("not_less_than_zero", pid),FALSE;
*p = n;
return TRUE;
}
static foreign_t
process_wait(term_t pid, term_t code, term_t options)
{ pid_t p;
wait_options opts;
term_t tail = PL_copy_term_ref(options);
term_t head = PL_new_term_ref();
term_t arg = PL_new_term_ref();
if ( !get_pid(pid, &p) )
return FALSE;
memset(&opts, 0, sizeof(opts));
while(PL_get_list(tail, head, tail))
{ atom_t name;
size_t arity;
if ( !PL_get_name_arity(head, &name, &arity) || arity != 1 )
return PL_type_error("option", head);
_PL_get_arg(1, head, arg);
if ( name == ATOM_timeout )
{ atom_t a;
if ( !(PL_get_atom(arg, &a) && a == ATOM_infinite) )
{ if ( !PL_get_float(arg, &opts.timeout) )
return PL_type_error("timeout", arg);
opts.has_timeout = TRUE;
}
} else if ( name == ATOM_release )
{ if ( !PL_get_bool_ex(arg, &opts.release) )
return FALSE;
if ( opts.release == FALSE )
return PL_domain_error("true", arg);
} else
return PL_domain_error("process_wait_option", head);
}
if ( !PL_get_nil_ex(tail) )
return FALSE;
return wait_for_pid(p, code, &opts);
}
#ifndef __WINDOWS__
static foreign_t
process_kill_posix(term_t pid_term, pid_t pid, int sig)
{ if ( kill(pid, sig) == 0 )
return TRUE;
switch(errno)
{ case EPERM:
return pl_error("process_kill", 2, NULL, ERR_PERMISSION,
pid_term, "kill", "process");
case ESRCH:
return pl_error("process_kill", 2, NULL, ERR_EXISTENCE,
"process", pid_term);
default:
return pl_error("process_kill", 2, "kill", ERR_ERRNO, errno,
"kill", "process", pid_term);
}
}
#endif
static foreign_t
process_kill(term_t pid, term_t signal)
{ pid_t p;
if ( !get_pid(pid, &p) )
return FALSE;
{
#ifdef __WINDOWS__
HANDLE h;
if ( !(h=find_process_from_pid(p, "process_kill")) )
return FALSE;
if ( TerminateProcess(h, 255) )
return TRUE;
return win_error("TerminateProcess");
#else /*__WINDOWS__*/
int sig;
if ( !PL_get_signum_ex(signal, &sig) )
return FALSE;
return process_kill_posix(pid, p, sig);
#endif /*__WINDOWS__*/
}
}
static foreign_t
process_group_kill(term_t pid, term_t signal)
{ pid_t p;
if ( !get_pid(pid, &p) )
return FALSE;
{
#ifdef __WINDOWS__
HANDLE hJob;
hJob = find_job_from_pid(p, "process_group_kill");
TerminateJobObject(hJob, 255);
/* For consistency, if you try to kill the process group of the
root process then exit. Windows does not do this for us.
*/
if (hJob == rootJob)
exit(255);
return TRUE;
#else /*__WINDOWS__*/
int sig;
if ( !PL_get_signum_ex(signal, &sig) )
return FALSE;
return process_kill_posix(pid, -p, sig);
#endif /*__WINDOWS__*/
}
}
static foreign_t
process_set_method(term_t how)
{ char *s;
if ( PL_get_chars(how, &s, CVT_ATOM|CVT_EXCEPTION) )
{ if ( strcmp(s, "fork") == 0 )
create_process_method = PCREATE_FORK;
else if ( strcmp(s, "vfork") == 0 )
create_process_method = PCREATE_VFORK;
else if ( strcmp(s, "spawn") == 0 )
create_process_method = PCREATE_SPAWN;
else
return PL_domain_error("process_create_method", how);
return TRUE;
}
return FALSE;
}
#define MKATOM(n) ATOM_ ## n = PL_new_atom(#n)
#define MKFUNCTOR(n,a) FUNCTOR_ ## n ## a = PL_new_functor(PL_new_atom(#n), a)
install_t
install_process()
{
#ifdef __WINDOWS__
win_init();
#elif !defined(HAVE_PRCTL)
posix_init();
#endif
MKATOM(stdin);
MKATOM(stdout);
MKATOM(stderr);
MKATOM(std);
MKATOM(null);
MKATOM(process);
MKATOM(detached);
MKATOM(cwd);
MKATOM(env);
MKATOM(environment);
MKATOM(priority);
MKATOM(window);
MKATOM(timeout);
MKATOM(release);
MKATOM(infinite);
MKATOM(text);
MKATOM(binary);
MKATOM(octet);
MKATOM(utf8);
MKATOM(ascii);
MKATOM(iso_latin_1);
MKATOM(unicode_be);
MKATOM(unicode_le);
MKFUNCTOR(pipe, 1);
MKFUNCTOR(pipe, 2);
MKFUNCTOR(type, 1);
MKFUNCTOR(encoding, 1);
MKFUNCTOR(stream, 1);
MKFUNCTOR(error, 2);
MKFUNCTOR(process_error, 2);
MKFUNCTOR(system_error, 2);
MKFUNCTOR(exit, 1);
MKFUNCTOR(killed, 1);
FUNCTOR_eq2 = PL_new_functor(PL_new_atom("="), 2);
PL_register_foreign("process_create", 2, process_create, 0);
PL_register_foreign("process_wait", 3, process_wait, 0);
PL_register_foreign("process_kill", 2, process_kill, 0);
PL_register_foreign("process_group_kill", 2, process_group_kill, 0);
PL_register_foreign("process_set_method", 1, process_set_method, 0);
}
|