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
|
/* $XConsortium: connection.c /main/27 1996/12/26 18:53:34 rws $ */
/***********************************************************
Copyright (c) 1987, 1989 X Consortium
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Except as contained in this notice, the name of the X Consortium shall not be
used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization from the X Consortium.
Copyright 1987, 1989 by Digital Equipment Corporation, Maynard, Massachusetts.
All Rights Reserved
Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted,
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in
supporting documentation, and that the name of Digital not be
used in advertising or publicity pertaining to distribution of the
software without specific, written prior permission.
DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
SOFTWARE.
******************************************************************/
/* $XFree86: xc/programs/lbxproxy/os/connection.c,v 1.1.1.2.2.2 1998/02/01 22:08:23 robin Exp $ */
/*
*
* The connection code/ideas for SVR4/Intel environments was contributed by
* the following companies/groups:
*
* MetroLink Inc
* NCR
* Pittsburgh Powercomputing Corporation (PPc)/Quarterdeck Office Systems
* SGCS
* Unix System Laboratories (USL) / Novell
* XFree86
*
* The goal is to have common connection code among all SVR4/Intel vendors.
*
* ALL THE ABOVE COMPANIES DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
* SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS,
* IN NO EVENT SHALL THESE COMPANIES BE LIABLE FOR ANY SPECIAL, INDIRECT
* OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
* OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE
* OR PERFORMANCE OF THIS SOFTWARE.
*/
/*****************************************************************
* Stuff to create connections --- OS dependent
*
* EstablishNewConnections, CreateWellKnownSockets, ResetWellKnownSockets,
* CloseDownConnection, CheckConnections, AddEnabledDevice,
* RemoveEnabledDevice, OnlyListToOneClient,
* ListenToAllClients,
*
* (WaitForSomething is in its own file)
*
* In this implementation, a client socket table is not kept.
* Instead, what would be the index into the table is just the
* file descriptor of the socket. This won't work for if the
* socket ids aren't small nums (0 - 2^8)
*
*****************************************************************/
#ifdef STREAMSCONN
#define TCPCONN
#endif
#include "misc.h"
#include "Xos.h" /* for strings, file, time */
#include <sys/param.h>
#include <errno.h>
#ifdef X_NOT_STDC_ENV
extern int errno;
#endif
#include <sys/socket.h>
#include <signal.h>
#include <setjmp.h>
#ifdef hpux
#include <sys/utsname.h>
#include <sys/ioctl.h>
#endif
#ifdef AIXV3
#include <sys/ioctl.h>
#endif
#ifdef TCPCONN
# include <netinet/in.h>
# ifndef hpux
# ifdef apollo
# ifndef NO_TCP_H
# include <netinet/tcp.h>
# endif
# else
# include <netinet/tcp.h>
# endif
# endif
#endif
#if defined(SO_DONTLINGER) && defined(SO_LINGER)
#undef SO_DONTLINGER
#endif
#ifdef UNIXCONN
/*
* sites should be careful to have separate /tmp directories for diskless nodes
*/
#include <sys/un.h>
#include <sys/stat.h>
static int unixDomainConnection = -1;
#endif
#include <stdio.h>
#include <sys/uio.h>
#include <X11/Xpoll.h>
#include "osdep.h"
#include "os.h"
#include "lbx.h"
#include "util.h"
#ifdef DNETCONN
#include <netdnet/dn.h>
#endif /* DNETCONN */
typedef long CCID; /* mask of indices into client socket table */
#ifndef X_NOT_POSIX
#include <unistd.h>
#else
extern int read(), close();
#endif
#ifndef X_UNIX_PATH
#ifdef hpux
#define X_UNIX_DIR "/usr/spool/sockets/X11"
#define X_UNIX_PATH "/usr/spool/sockets/X11/"
#define OLD_UNIX_DIR "/tmp/.X11-unix"
#else
#define X_UNIX_DIR "/tmp/.X11-unix"
#define X_UNIX_PATH "/tmp/.X11-unix/X"
#endif
#endif
extern char *display; /* The display number */
int lastfdesc; /* maximum file descriptor */
fd_set WellKnownConnections; /* Listener mask */
fd_set AllSockets; /* select on this */
fd_set AllClients; /* available clients */
fd_set LastSelectMask; /* mask returned from last select call */
fd_set ClientsWithInput; /* clients with FULL requests in buffer */
fd_set ClientsWriteBlocked; /* clients who cannot receive output */
fd_set OutputPending; /* clients with reply/event data ready to go */
int MaxClients = MAXSOCKS;
Bool NewOutputPending; /* not yet attempted to write some new output */
Bool AnyClientsWriteBlocked; /* true if some client blocked on write */
Bool RunFromSmartParent; /* send SIGUSR1 to parent process */
Bool PartialNetwork; /* continue even if unable to bind all addrs */
static int ParentProcess;
static Bool debug_conns = FALSE;
static fd_set IgnoredClientsWithInput;
static fd_set GrabImperviousClients;
static fd_set SavedAllClients;
static fd_set SavedAllSockets;
static fd_set SavedClientsWithInput;
int GrabInProgress = 0;
int ConnectionTranslation[MAXSOCKS];
int ConnectionOutputTranslation[MAXSOCKS];
extern int auditTrailLevel;
unsigned long raw_stream_out; /* out to server, in from client */
unsigned long raw_stream_in; /* in from server, out to client */
extern unsigned long stream_out_plain;
extern unsigned long stream_in_plain;
extern void CloseServer();
static void ErrorConnMax(
#if NeedFunctionPrototypes
register int /*fd*/
#endif
);
static void
PickNewListenDisplay (displayP)
char **displayP;
{
static char newDisplay[16];
sprintf (newDisplay, "%d", atoi (*displayP) - 1);
*displayP = newDisplay;
}
#ifdef TCPCONN
static int
open_tcp_socket (retry)
int retry; /* boolean - retry if addr busy */
{
struct sockaddr_in insock;
int request;
int retryCount;
#ifndef SO_DONTLINGER
#ifdef SO_LINGER
static int linger[2] = { 0, 0 };
#endif /* SO_LINGER */
#endif /* SO_DONTLINGER */
if ((request = socket (AF_INET, SOCK_STREAM, 0)) < 0)
{
Error ("Creating TCP socket");
return -1;
}
#ifdef SO_REUSEADDR
/* Necesary to restart the server without a reboot */
{
int one = 1;
setsockopt(request, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(int));
}
#endif /* SO_REUSEADDR */
{
bzero ((char *)&insock, sizeof (insock));
#ifdef BSD44SOCKETS
insock.sin_len = sizeof(insock);
#endif
insock.sin_family = AF_INET;
insock.sin_port = htons ((unsigned short)(X_TCP_PORT + atoi (display)));
insock.sin_addr.s_addr = htonl(INADDR_ANY);
retryCount = retry ? 19 : 0;
while (bind(request, (struct sockaddr *) &insock, sizeof (insock)))
{
if (retryCount-- == 0) {
Error ("Binding TCP socket");
close (request);
return -1;
}
fprintf (stderr, "port %s is busy, retrying\n", display);
#ifdef SO_REUSEADDR
sleep (1);
#else
sleep (10);
#endif /* SO_REUSEDADDR */
}
}
#ifdef SO_DONTLINGER
if(setsockopt (request, SOL_SOCKET, SO_DONTLINGER, (char *)NULL, 0))
Error ("Setting TCP SO_DONTLINGER");
#else
#ifdef SO_LINGER
if(setsockopt (request, SOL_SOCKET, SO_LINGER,
(char *)linger, sizeof(linger)))
Error ("Setting TCP SO_LINGER");
#endif /* SO_LINGER */
#endif /* SO_DONTLINGER */
if (listen (request, 5)) {
Error ("TCP Listening");
close (request);
return -1;
}
return request;
}
#endif /* TCPCONN */
#if defined(UNIXCONN) && !defined(LOCALCONN)
static struct sockaddr_un unsock;
static int
open_unix_socket ()
{
int oldUmask;
int request;
bzero ((char *) &unsock, sizeof (unsock));
unsock.sun_family = AF_UNIX;
oldUmask = umask (0);
#ifdef X_UNIX_DIR
if (!mkdir (X_UNIX_DIR, 0777))
chmod (X_UNIX_DIR, 0777);
#endif
strcpy (unsock.sun_path, X_UNIX_PATH);
strcat (unsock.sun_path, display);
#if defined(BSD44SOCKETS) && !defined(Lynx)
unsock.sun_len = strlen(unsock.sun_path);
#endif
#ifdef hpux
{
/* The following is for backwards compatibility
* with old HP clients. This old scheme predates the use
* of the /usr/spool/sockets directory, and uses hostname:display
* in the /tmp/.X11-unix directory
*/
struct utsname systemName;
static char oldLinkName[256];
uname(&systemName);
strcpy(oldLinkName, OLD_UNIX_DIR);
if (!mkdir(oldLinkName, 0777))
chown(oldLinkName, 2, 3);
strcat(oldLinkName, "/");
strcat(oldLinkName, systemName.nodename);
strcat(oldLinkName, display);
unlink(oldLinkName);
symlink(unsock.sun_path, oldLinkName);
}
#endif /* hpux */
unlink (unsock.sun_path);
if ((request = socket (AF_UNIX, SOCK_STREAM, 0)) < 0)
{
Error ("Creating Unix socket");
return -1;
}
#ifdef BSD44SOCKETS
if (bind(request, (struct sockaddr *)&unsock, SUN_LEN(&unsock)))
#else
if (bind(request, (struct sockaddr *)&unsock, strlen(unsock.sun_path)+2))
#endif
{
Error ("Binding Unix socket");
close (request);
return -1;
}
if (listen (request, 5))
{
Error ("Unix Listening");
close (request);
return -1;
}
(void)umask(oldUmask);
return request;
}
#endif /*UNIXCONN */
#ifdef LOCALCONN
/*
* This code amply demonstrates why vendors need to talk to each other
* earlier rather than later.
*
* The following is an implementation of the X-server-half of a variety
* of SYSV386 local connection methods. This includes compatability
* with ISC STREAMS, SCO STREAMS, ATT pts connections, ATT/USL named-pipes,
* and SVR4.2 UNIX DOMAIN connections. To enable the output of various
* connection-related information messages to stderr, compile with
* -DXLOCAL_VERBOSE and set the environment variable XLOCAL_VERBOSE.
* XLOCAL_VERBOSE = 0 print connection availability
* = 1 also, print message for each connection
* = 2 also, print cleanup information
* = 3 even more...
*
* See note below about the conflict between ISC and UNIX nodes.
*/
#include <sys/stream.h>
#include <sys/stropts.h>
#include <sys/utsname.h>
#ifndef UNIXCONN
#include <sys/stat.h>
#endif
#define X_STREAMS_DIR "/dev/X"
#ifdef UNIXCONN
#define XLOCAL_UNIX
#define X_UNIX_DEVDIR "/dev/X/UNIXCON"
#define X_UNIX_DEVPATH "/dev/X/UNIXCON/X"
#endif
#if !defined(SVR4) || defined(SVR4_ACP)
#define XLOCAL_ISC
#define XLOCAL_SCO
#define X_ISC_DIR "/dev/X/ISCCONN"
#define X_ISC_PATH "/dev/X/ISCCONN/X"
#define X_SCO_PATH "/dev/X"
#define DEV_SPX "/dev/spx"
static int iscFd = -1;
static int scoFd = -1;
#endif
#ifdef unix
#define XLOCAL_PTS
#define X_PTS_PATH "/dev/X/server."
#define DEV_PTMX "/dev/ptmx"
extern char *ptsname();
static int ptsFd = -1;
#endif
#ifdef SVR4
#define XLOCAL_NAMED
static int namedFd = -1;
#define X_NAMED_PATH "/dev/X/Nserver."
#endif
static fd_set AllStreams; /* bitmap of STREAMS file descriptors */
#ifndef XLOCAL_VERBOSE
#define XLOCAL_MSG(x) /* as nothing */
#else
static void xlocalMsg();
#define XLOCAL_MSG(x) xlocalMsg x;
#endif
static int useSlashTmpForUNIX=0;
#ifdef XLOCAL_VERBOSE
/*PRINTFLIKE1*/
static void
xlocalMsg(lvl,str,a,b,c,d,e,f,g,h)
int lvl;
char *str;
int a,b,c,d,e,f,g,h;
{
static int xlocalMsgLvl = -2;
if (xlocalMsgLvl == -2) {
char *tmp;
if ((tmp = (char *)getenv("XLOCAL_VERBOSE")) != NULL) {
xlocalMsgLvl = atoi(tmp);
} else {
xlocalMsgLvl = -1;
}
}
if (xlocalMsgLvl >= lvl) {
fprintf(stderr,"X: XLOCAL - ");
fprintf(stderr,str,a,b,c,d,e,f,g,h);
}
}
#endif /* XLOCAL_VERBOSE */
/*
* We have a conflict between ISC and UNIX connections over the use
* of the /tmp/.X11-unix/Xn path. Therefore, whichever connection type
* is specified first in the XLOCAL environment variable gets to use this
* path for its own device nodes. The default is ISC.
*
* Note that both connection types are always available using their
* alternate paths at /dev/X/ISCCONN/Xn and /dev/X/UNIXCON/Xn respectively.
*
* To make an older client or library use these alternate paths, you
* need to edit the binary and replace /tmp/.X11-unix with either
* /dev/X/ISCCONN or /dev/X/UNIXCON depending on its preference.
*/
#define WHITE " :\t\n\r"
static void
ChooseLocalConnectionType()
{
char *name,*nameList;
XLOCAL_MSG((3,"Choosing ISC vs. UNIXDOMAIN connections...\n"));
useSlashTmpForUNIX=0;
if ((nameList = (char *)getenv("XLOCAL")) != NULL) {
nameList = (char *)strdup(nameList);
name = strtok(nameList,WHITE);
while (name) {
if (!strncmp(name,"SP",2) ||
!strncmp(name,"ISC",3) ||
!strncmp(name,"STREAMS",7)) {
break;
} else if (!strncmp(name,"UNIX",4)) {
useSlashTmpForUNIX=1;
break;
}
name = strtok(NULL,WHITE);
}
xfree(nameList);
} else {
XLOCAL_MSG((3,"XLOCAL not set in environment.\n"));
}
XLOCAL_MSG((3,"Using %s for local connections in /tmp/.X11-unix.\n",
useSlashTmpForUNIX ? "UNIXCONN" : "ISC"));
}
#undef WHITE
static int
xlocal_unlink(path)
char *path;
{
int ret;
ret = unlink(path);
if (ret == -1 && errno == EINTR)
ret = unlink(path);
if (ret == -1 && errno == ENOENT)
ret = 0;
return(ret);
}
#ifdef XLOCAL_UNIX
static struct sockaddr_un unsock;
/* UNIX: UNIX domain sockets as used in SVR4.2 */
static int
open_unix_local()
{
int oldUmask;
int request;
bzero((char *) &unsock, sizeof (unsock));
unsock.sun_family = AF_UNIX;
oldUmask = umask (0);
mkdir(X_STREAMS_DIR, 0777); /* "/dev/X" */
chmod(X_STREAMS_DIR, 0777);
if (!mkdir(X_UNIX_DEVDIR, 0777))
chmod(X_UNIX_DEVDIR, 0777);
strcpy(unsock.sun_path, X_UNIX_DEVPATH);
strcat(unsock.sun_path, display);
xlocal_unlink(unsock.sun_path);
if ((request = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
Error("Creating Unix socket");
return -1;
}
if (bind(request, (struct sockaddr *)&unsock, strlen(unsock.sun_path)+2)) {
Error("Binding Unix socket");
close(request);
return -1;
}
if (listen(request, 5)) {
Error("Unix Listening");
close(request);
return -1;
}
XLOCAL_MSG((0,"UNIX connections available at [%s]\n", unsock.sun_path));
if (useSlashTmpForUNIX) {
char tmpPath[64];
if (!mkdir(X_UNIX_DIR, 0777))
chmod(X_UNIX_DIR, 0777);
strcpy(tmpPath, X_UNIX_PATH);
strcat(tmpPath, display);
xlocal_unlink(tmpPath);
if (link(unsock.sun_path,tmpPath) == 0) {
XLOCAL_MSG((0,"UNIX connections available at [%s]\n", tmpPath));
}
}
(void)umask(oldUmask);
return request;
}
static void
close_unix_local()
{
char path[64];
if (unixDomainConnection != -1) {
close(unixDomainConnection);
FD_CLR (unixDomainConnection, &WellKnownConnections);
unixDomainConnection = -1;
}
strcpy(path, X_UNIX_DEVPATH);
strcat(path, display);
XLOCAL_MSG((2,"removing [%s] and [%s]\n",path,X_UNIX_DEVDIR));
xlocal_unlink(path);
xlocal_unlink(X_UNIX_DEVDIR);
if (useSlashTmpForUNIX) {
strcpy(path, X_UNIX_PATH);
strcat(path, display);
XLOCAL_MSG((2,"removing [%s] and [%s]\n",path,X_UNIX_DIR));
xlocal_unlink(path);
xlocal_unlink(X_UNIX_DIR);
}
}
static void
reset_unix_local()
{
char path[64];
struct stat statb;
int need_reset = 0;
if (unixDomainConnection != -1) {
strcpy(path, X_UNIX_DEVPATH);
strcat(path, display);
if ((stat(path, &statb) == -1) ||
((statb.st_mode & S_IFMT) !=
#if (defined (sun) && defined(SVR4)) || defined(NCR) || defined(sco)
S_IFIFO))
#else
S_IFSOCK))
#endif
need_reset = 1;
if (useSlashTmpForUNIX) {
strcpy(path, X_UNIX_PATH);
strcat(path, display);
if ((stat(path, &statb) == -1) ||
((statb.st_mode & S_IFMT) !=
#if (defined (sun) && defined(SVR4)) || defined(NCR) || defined(sco)
S_IFIFO))
#else
S_IFSOCK))
#endif
need_reset = 1;
}
if (need_reset) {
close_unix_local();
unixDomainConnection = open_unix_local();
if (unixDomainConnection != -1)
FD_SET (unixDomainConnection, &WellKnownConnections);
}
}
}
#endif /* XLOCAL_UNIX */
#if defined(XLOCAL_ISC) || defined(XLOCAL_SCO)
static int
connect_spipe(fd1, fd2)
int fd1, fd2;
{
long temp;
struct strfdinsert sbuf;
sbuf.databuf.maxlen = -1;
sbuf.databuf.len = -1;
sbuf.databuf.buf = NULL;
sbuf.ctlbuf.maxlen = sizeof(long);
sbuf.ctlbuf.len = sizeof(long);
sbuf.ctlbuf.buf = (caddr_t)&temp;
sbuf.offset = 0;
sbuf.fildes = fd2;
sbuf.flags = 0;
if (ioctl(fd1, I_FDINSERT, &sbuf) == -1) return (-1);
return(0);
}
static int
named_spipe(fd, path)
int fd;
char *path;
{
int oldUmask, ret;
struct stat sbuf;
oldUmask = umask(0);
(void) fstat(fd, &sbuf);
ret = mknod(path, 0020666, sbuf.st_rdev);
umask(oldUmask);
if (ret < 0) {
ret = -1;
} else {
ret = fd;
}
return(ret);
}
#endif /* defined(XLOCAL_ISC) || defined(XLOCAL_SCO) */
#ifdef XLOCAL_ISC
/*
* ISC: ISC UNIX V.3.2 compatible streams at /dev/X/ISCCONN/Xn.
* It will link this to /tmp/.X11-unix/Xn if there are no UNIXDOMAIN
* connection nodes required.
*/
static int
open_isc_local()
{
int fd = -1,fds = -1;
char pathISC[64],pathX11[64];
mkdir(X_STREAMS_DIR, 0777); /* "/dev/X" */
chmod(X_STREAMS_DIR, 0777);
mkdir(X_ISC_DIR, 0777); /* "/dev/X/ISCCONN" */
chmod(X_ISC_DIR, 0777);
strcpy(pathISC, X_ISC_PATH);
strcat(pathISC, display);
if (xlocal_unlink(pathISC) < 0) {
ErrorF("open_isc_local(): Can't unlink node (%s),\n", pathISC);
return(-1);
}
if ((fds = open(DEV_SPX, O_RDWR)) >= 0 &&
(fd = open(DEV_SPX, O_RDWR)) >= 0 ) {
if (connect_spipe(fds, fd) != -1 &&
named_spipe(fds, pathISC) != -1) {
XLOCAL_MSG((0,"ISC connections available at [%s]\n", pathISC));
if (!useSlashTmpForUNIX) {
mkdir(X_UNIX_DIR, 0777);
chmod(X_UNIX_DIR, 0777);
strcpy(pathX11, X_UNIX_PATH);
strcat(pathX11, display);
if (xlocal_unlink(pathX11) < 0) {
/*EMPTY*/
/* can't get the /tmp node, just return the good one...*/
} else {
#ifdef SVR4
/* we prefer symbolic links as hard links can't
cross filesystems */
if (symlink(pathISC,pathX11) == 0) {
XLOCAL_MSG((0,"ISC connections available at [%s]\n",
pathX11));
}
#else
if (link(pathISC,pathX11) == 0) {
XLOCAL_MSG((0,"ISC connections available at [%s]\n",
pathX11));
}
#endif
}
}
return(fd);
} else {
Error("open_isc_local(): Can't set up listener pipes");
}
} else {
XLOCAL_MSG((0,"open_isc_local(): can't open %s\n",DEV_SPX));
#ifndef SVR4
/*
* At this point, most SVR4 versions will fail on this, so leave out the
* warning
*/
ErrorF("open_isc_local(): can't open \"%s\"",DEV_SPX);
return(-1);
#endif
}
(void) close(fds);
(void) close(fd);
return(-1);
}
static int
accept_isc_local()
{
struct strrecvfd buf;
while (ioctl(iscFd, I_RECVFD, &buf) < 0)
if (errno != EAGAIN) {
Error("accept_isc_local(): Can't read fildes");
return(-1);
}
XLOCAL_MSG((1,"new ISC connection accepted (%d)\n",buf.fd));
FD_SET(buf.fd, &AllStreams);
return(buf.fd);
}
static void
close_isc_local()
{
char path[64];
if (iscFd != -1) {
close(iscFd);
FD_CLR (iscFd, &WellKnownConnections);
iscFd = -1;
}
strcpy(path, X_ISC_PATH);
strcat(path, display);
XLOCAL_MSG((2,"removing [%s] and [%s]\n",path,X_ISC_DIR));
xlocal_unlink(path);
xlocal_unlink(X_ISC_DIR);
if (!useSlashTmpForUNIX) {
strcpy(path, X_UNIX_PATH);
strcat(path, display);
XLOCAL_MSG((2,"removing [%s] and [%s]\n",path,X_UNIX_DIR));
xlocal_unlink(path);
xlocal_unlink(X_UNIX_DIR);
}
}
#endif /* XLOCAL_ISC */
#ifdef XLOCAL_SCO
/* SCO: SCO/XSIGHT style using /dev/spx */
static int
open_sco_local()
{
int fds = -1,fdr = -1;
char pathS[64], pathR[64];
sprintf(pathS, "%s%sS",X_SCO_PATH, display);
sprintf(pathR, "%s%sR",X_SCO_PATH, display);
if ((xlocal_unlink(pathS) < 0) || (xlocal_unlink(pathR) < 0)) {
ErrorF("open_sco_local(): can't unlink node (%s)\n",pathR);
return(-1);
}
if ((fds = open(DEV_SPX, O_RDWR)) >= 0 &&
(fdr = open(DEV_SPX, O_RDWR)) >= 0 ) {
if (connect_spipe(fds, fdr) != -1 &&
named_spipe(fds, pathS) != -1 &&
named_spipe(fdr, pathR) != -1) {
XLOCAL_MSG((0,"SCO connections available at [%s]\n",pathR));
return(fds);
} else {
Error("open_sco_local(): can't set up listener pipes");
}
} else {
XLOCAL_MSG((0,"open_sco_local(): can't open %s",DEV_SPX));
#ifndef SVR4
/*
* At this point, most SVR4 versions will fail on this, so
* leave out the warning
*/
ErrorF("open_sco_local(): can't open \"%s\"",DEV_SPX);
return(-1);
#endif
}
(void) close(fds);
(void) close(fdr);
return(-1);
}
static int
accept_sco_local()
{
char c;
int fd;
if (read(scoFd, &c, 1) < 0) {
Error("accept_sco_local(): can't read from client");
return(-1);
}
if ((fd = open(DEV_SPX, O_RDWR)) < 0) {
ErrorF("accept_sco_local(): can't open \"%s\"",DEV_SPX);
return(-1);
}
if (connect_spipe(scoFd, fd) < 0) {
Error("accept_sco_local(): can't connect pipes");
(void) close(fd);
return(-1);
}
XLOCAL_MSG((1,"new SCO connection accepted (%d)\n",fd));
FD_SET(fd, &AllStreams);
return(fd);
}
static void
close_sco_local()
{
char pathS[64], pathR[64];
if (scoFd != -1) {
close(scoFd);
FD_CLR (scoFd, &WellKnownConnections);
scoFd = -1;
}
sprintf(pathS, "%s%sS",X_SCO_PATH, display);
sprintf(pathR, "%s%sR",X_SCO_PATH, display);
XLOCAL_MSG((2,"removing [%s] and [%s]\n",pathS,pathR));
xlocal_unlink(pathS);
xlocal_unlink(pathR);
}
#endif /* XLOCAL_SCO */
#ifdef XLOCAL_PTS
/* PTS: AT&T style using /dev/ptmx */
static int
open_pts_local()
{
char *slave;
int fd;
char path[64];
mkdir(X_STREAMS_DIR, 0777);
chmod(X_STREAMS_DIR, 0777);
strcpy(path, X_PTS_PATH);
strcat(path, display);
if (open(path, O_RDWR) >= 0 || (xlocal_unlink(path) < 0)) {
ErrorF("open_pts_local(): server is already running (%s)\n", path);
return(-1);
}
if ((fd = open(DEV_PTMX, O_RDWR)) < 0) {
Error("open_pts_local(): open failed");
ErrorF("open_pts_local(): can't open \"%s\"",DEV_PTMX);
return(-1);
}
grantpt(fd);
unlockpt(fd);
slave = ptsname(fd);
if (link(slave, path) < 0 || chmod(path, 0666) < 0) {
Error("open_pts_local(): can't set up local listener");
return(-1);
}
if (open(path, O_RDWR) < 0) {
Error("open_pts_local(): open failed");
ErrorF("open_pts_local(): can't open %s\n", path);
close(fd);
return(-1);
}
XLOCAL_MSG((0,"PTS connections available at [%s]\n",path));
return(fd);
}
static int
accept_pts_local()
{
int newconn;
char length;
char path[64];
/*
* first get device-name
*/
if(read(ptsFd, &length, 1) <= 0 ) {
Error("accept_pts_local(): can't read slave name length");
return(-1);
}
if(read(ptsFd, path, length) <= 0 ) {
Error("accept_pts_local(): can't read slave name");
return(-1);
}
path[ length ] = '\0';
if((newconn = open(path,O_RDWR)) < 0) {
Error("accept_pts_local(): can't open slave");
return(-1);
}
(void) write(newconn, "1", 1); /* send an acknowledge to the client */
XLOCAL_MSG((1,"new PTS connection accepted (%d)\n",newconn));
FD_SET(newconn, &AllStreams);
return(newconn);
}
static void
close_pts_local()
{
char path[64];
if (ptsFd != -1) {
close(ptsFd);
FD_CLR (ptsFd, &WellKnownConnections);
ptsFd = -1;
}
strcpy(path, X_PTS_PATH);
strcat(path, display);
XLOCAL_MSG((2,"removing [%s]\n",path));
xlocal_unlink(path);
}
#endif /* XLOCAL_PTS */
#ifdef XLOCAL_NAMED
/* NAMED: USL style using bi-directional named pipes */
static int
open_named_local()
{
int fd,fld[2];
char path[64];
struct stat sbuf;
mkdir(X_STREAMS_DIR, 0777);
chmod(X_STREAMS_DIR, 0777);
strcpy(path, X_NAMED_PATH);
strcat(path, display);
if (stat(path, &sbuf) != 0) {
if (errno == ENOENT) {
if ((fd = creat(path, (mode_t)0666)) == -1) {
ErrorF("open_named_local(): can't create %s\n",path);
return(-1);
}
close(fd);
if (chmod(path, (mode_t)0666) < 0) {
ErrorF("open_named_local(): can't chmod %s\n",path);
return(-1);
}
} else {
ErrorF("open_named_local(): unknown stat error, %d\n",errno);
return(-1);
}
}
if (pipe(fld) != 0) {
ErrorF("open_named_local(): pipe failed, errno=%d\n",errno);
return(-1);
}
if (ioctl(fld[0], I_PUSH, "connld") != 0) {
ErrorF("open_named_local(): ioctl error %d\n",errno);
return(-1);
}
if (fattach(fld[0], path) != 0) {
ErrorF("open_named_local(): fattach failed, errno=%d\n",errno);
return(-1);
}
XLOCAL_MSG((0,"NAMED connections available at [%s]\n", path));
return(fld[1]);
}
static int
accept_named_local()
{
struct strrecvfd str;
if (ioctl(namedFd, I_RECVFD, &str) < 0) {
ErrorF("accept_named_local(): I_RECVFD failed\n");
return(-1);
}
XLOCAL_MSG((1,"new NAMED connection accepted (%d)\n",str.fd));
FD_SET(str.fd, &AllStreams);
return(str.fd);
}
static void
close_named_local()
{
char path[64];
if (namedFd != -1) {
close(namedFd);
FD_CLR (namedFd, &WellKnownConnections);
namedFd = -1;
}
strcpy(path, X_NAMED_PATH);
strcat(path, display);
XLOCAL_MSG((2,"removing [%s]\n",path));
xlocal_unlink(path);
}
#endif /* XLOCAL_NAMED */
static int
xlocal_create_sockets(findPort)
int findPort;
{
int request;
ChooseLocalConnectionType();
FD_ZERO(&AllStreams);
#ifdef XLOCAL_PTS
if ((ptsFd = open_pts_local()) == -1) {
XLOCAL_MSG((0,"open_pts_local(): failed.\n"));
} else {
FD_SET (ptsFd, &WellKnownConnections);
}
#endif
#ifdef XLOCAL_NAMED
if ((namedFd = open_named_local()) == -1) {
XLOCAL_MSG((0,"open_named_local(): failed.\n"));
} else {
FD_SET (namedFd, &WellKnownConnections);
}
#endif
#ifdef XLOCAL_ISC
if ((iscFd = open_isc_local()) == -1) {
XLOCAL_MSG((0,"open_isc_local(): failed.\n"));
} else {
FD_SET (iscFd, &WellKnownConnections);
}
#endif
#ifdef XLOCAL_SCO
if ((scoFd = open_sco_local()) == -1) {
XLOCAL_MSG((0,"open_sco_local(): failed.\n"));
} else {
FD_SET (scoFd, &WellKnownConnections);
}
#endif
#ifdef XLOCAL_UNIX
if ((request = open_unix_local()) == -1) {
XLOCAL_MSG((0,"open_unix_local(): failed.\n"));
} else {
FD_SET (request, &WellKnownConnections);
unixDomainConnection = request;
}
#endif
#ifdef TCPCONN
if ((request = open_tcp_socket(!findPort)) == -1) {
XLOCAL_MSG((0,"open_tcp_socket(): failed.\n"));
if (PartialNetwork == FALSE && !findPort) {
FatalError("Cannot establish tcp listening socket");
}
return -1;
} else {
XLOCAL_MSG((0,"TCP connections available at port %d\n",
X_TCP_PORT + atoi(display)));
FD_SET (request, &WellKnownConnections);
return 0;
}
#endif /* TCPCONN */
}
static void
xlocal_reset_sockets()
{
#ifdef XLOCAL_UNIX
reset_unix_local();
#endif
FD_ZERO(&AllStreams);
}
static int
xlocal_close_sockets()
{
#ifdef XLOCAL_UNIX
close_unix_local();
#endif /* XLOCAL_UNIX */
#ifdef XLOCAL_ISC
close_isc_local();
#endif /* XLOCAL_ISC */
#ifdef XLOCAL_SCO
close_sco_local();
#endif /* XLOCAL_SCO */
#ifdef XLOCAL_PTS
close_pts_local();
#endif /* XLOCAL_PTS */
#ifdef XLOCAL_NAMED
close_named_local();
#endif /* XLOCAL_NAMED */
return(0);
}
int
xlocal_getpeername(fd, from, fromlen)
int fd;
struct sockaddr *from;
int *fromlen;
{
/* special case for local connections ( ISC, SCO, PTS, NAMED... ) */
if (FD_SET(fd, &AllStreams)) {
from->sa_family = AF_UNSPEC;
*fromlen = 0;
return 0;
}
#if defined(TCPCONN) || defined(DNETCONN) || defined(UNIXCONN)
return getpeername(fd, from, fromlen);
#else
return(-1);
#endif
}
#define getpeername xlocal_getpeername
static int
xlocal_accept(fd, from, fromlen)
int fd;
struct sockaddr *from;
int *fromlen;
{
int ret;
#ifdef XLOCAL_PTS
if (fd == ptsFd) return accept_pts_local();
#endif
#ifdef XLOCAL_NAMED
if (fd == namedFd) return accept_named_local();
#endif
#ifdef XLOCAL_ISC
if (fd == iscFd) return accept_isc_local();
#endif
#ifdef XLOCAL_SCO
if (fd == scoFd) return accept_sco_local();
#endif
/*
* else we are handling the normal accept case
*/
#if defined(TCPCONN) || defined(DNETCONN) || defined(XLOCAL_UNIX)
ret = accept (fd, from, fromlen);
#ifdef XLOCAL_UNIX
if (fd == unixDomainConnection) {
XLOCAL_MSG((1,"new UNIX connection accepted (%d)\n",ret));
} else
#endif
{
XLOCAL_MSG((1,"new TCP connection accepted (%d)\n",ret));
}
#endif
return(ret);
}
#define accept xlocal_accept
#endif /* LOCALCONN */
#ifdef hpux
/*
* hpux returns EOPNOTSUPP when using getpeername on a unix-domain
* socket. In this case, smash the socket address with the address
* used to bind the connection socket and return success.
*/
hpux_getpeername(fd, from, fromlen)
int fd;
struct sockaddr *from;
int *fromlen;
{
int ret;
int len;
ret = getpeername(fd, from, fromlen);
if (ret == -1 && errno == EOPNOTSUPP)
{
ret = 0;
len = strlen(unsock.sun_path)+2;
if (len > *fromlen)
len = *fromlen;
memmove((char *) from, (char *) &unsock, len);
*fromlen = len;
}
return ret;
}
#define getpeername(fd, from, fromlen) hpux_getpeername(fd, from, fromlen)
#endif
#ifdef DNETCONN
static int
open_dnet_socket ()
{
int request;
struct sockaddr_dn dnsock;
if ((request = socket (AF_DECnet, SOCK_STREAM, 0)) < 0)
{
Error ("Creating DECnet socket");
return -1;
}
bzero ((char *)&dnsock, sizeof (dnsock));
dnsock.sdn_family = AF_DECnet;
sprintf(dnsock.sdn_objname, "X$X%d", atoi (display));
dnsock.sdn_objnamel = strlen(dnsock.sdn_objname);
if (bind (request, (struct sockaddr *) &dnsock, sizeof (dnsock)))
{
Error ("Binding DECnet socket");
close (request);
return -1;
}
if (listen (request, 5))
{
Error ("DECnet Listening");
close (request);
return -1;
}
return request;
}
#endif /* DNETCONN */
/*****************
* CreateWellKnownSockets
* At initialization, create the sockets to listen on for new clients.
*****************/
extern Bool proxyMngr;
void
CreateWellKnownSockets()
{
int tcp_request = -1, dnet_request = - 1, unix_request;
int i, done = 0;
int findPort = proxyMngr;
FD_ZERO(&AllSockets);
FD_ZERO(&AllClients);
FD_ZERO(&LastSelectMask);
FD_ZERO(&ClientsWithInput);
for (i=0; i<MAXSOCKS; i++) ConnectionTranslation[i] = 0;
for (i=0; i<MAXSOCKS; i++) ConnectionOutputTranslation[i] = 0;
#ifdef XNO_SYSCONF
#undef _SC_OPEN_MAX
#endif
#ifdef _SC_OPEN_MAX
lastfdesc = sysconf(_SC_OPEN_MAX) - 1;
#else
#ifdef hpux
lastfdesc = _NFILE - 1;
#else
lastfdesc = getdtablesize() - 1;
#endif
#endif
if (lastfdesc > MAXSOCKS)
{
lastfdesc = MAXSOCKS;
if (debug_conns)
ErrorF( "GOT TO END OF SOCKETS %d\n", MAXSOCKS);
}
while (!done)
{
FD_ZERO(&WellKnownConnections);
#ifdef LOCALCONN
if (xlocal_create_sockets() == -1 && findPort)
{
PickNewListenDisplay (&display);
continue;
}
#else /* LOCALCONN */
#ifdef TCPCONN
if ((tcp_request = open_tcp_socket (!findPort)) != -1) {
FD_SET (tcp_request, &WellKnownConnections);
}
else if (findPort)
{
PickNewListenDisplay (&display);
continue;
}
else if (!PartialNetwork)
{
FatalError ("Cannot establish tcp listening socket");
}
#endif /* TCPCONN */
#ifdef DNETCONN
if ((dnet_request = open_dnet_socket ()) != -1) {
FD_SET (dnet_request, &WellKnownConnections);
}
else if (findPort)
{
PickNewListenDisplay (&display);
if (tcp_request >= 0)
close (tcp_request);
continue;
}
else if (!PartialNetwork)
{
FatalError ("Cannot establish dnet listening socket");
}
#endif /* DNETCONN */
#ifdef UNIXCONN
if ((unix_request = open_unix_socket ()) != -1) {
FD_SET (unix_request, &WellKnownConnections);
unixDomainConnection = unix_request;
}
else if (findPort)
{
PickNewListenDisplay (&display);
if (tcp_request >= 0)
close (tcp_request);
if (dnet_request >= 0)
close (dnet_request);
continue;
}
else if (!PartialNetwork)
{
FatalError ("Cannot establish unix listening socket");
}
#endif /* UNIXCONN */
#endif /* LOCALCONN */
done = 1;
}
/*
* We will start listening on the well known connections
* after the proxy finishes connecting to the server.
*/
if (!XFD_ANYSET (&WellKnownConnections))
FatalError ("Cannot establish any listening sockets");
OsSignal (SIGPIPE, SIG_IGN);
OsSignal (SIGHUP, AutoResetServer);
OsSignal (SIGINT, GiveUp);
OsSignal (SIGTERM, GiveUp);
/*
* Magic: If SIGUSR1 was set to SIG_IGN when
* the server started, assume that either
*
* a- The parent process is ignoring SIGUSR1
*
* or
*
* b- The parent process is expecting a SIGUSR1
* when the server is ready to accept connections
*
* In the first case, the signal will be harmless,
* in the second case, the signal will be quite
* useful
*/
if (OsSignal (SIGUSR1, SIG_IGN) == SIG_IGN)
RunFromSmartParent = TRUE;
ParentProcess = getppid ();
if (RunFromSmartParent) {
if (ParentProcess > 0) {
kill (ParentProcess, SIGUSR1);
}
}
}
extern int proxy_manager_fd;
ListenToProxyManager ()
{
FD_SET(proxy_manager_fd, &AllSockets);
}
void
ListenWellKnownSockets ()
{
XFD_ORSET (&AllSockets, &AllSockets, &WellKnownConnections);
}
void
ResetWellKnownSockets ()
{
ResetOsBuffers();
#ifdef LOCALCONN
xlocal_reset_sockets();
#else /* LOCALCONN */
#if defined(UNIXCONN) && !defined(SVR4)
if (unixDomainConnection != -1)
{
/*
* see if the unix domain socket has disappeared
*/
struct stat statb;
if (stat (unsock.sun_path, &statb) == -1 ||
(statb.st_mode & S_IFMT) != S_IFSOCK)
{
ErrorF ("Unix domain socket %s trashed, recreating\n",
unsock.sun_path);
(void) unlink (unsock.sun_path);
(void) close (unixDomainConnection);
FD_CLR(unixDomainConnection, &WellKnownConnections);
unixDomainConnection = open_unix_socket ();
if (unixDomainConnection != -1)
FD_SET (unixDomainConnection, &WellKnownConnections);
}
}
#endif /* UNIXCONN */
#endif /* LOCALCONN */
/*
* See above in CreateWellKnownSockets about SIGUSR1
*/
if (RunFromSmartParent) {
if (ParentProcess > 0) {
kill (ParentProcess, SIGUSR1);
}
}
}
void
AvailableClientInput (client)
ClientPtr client;
{
OsCommPtr oc = (OsCommPtr)client->osPrivate;
if (FD_ISSET(oc->fd, &AllSockets))
FD_SET(oc->fd, &ClientsWithInput);
}
static int
ClientRead (fd, buf, len)
int fd;
char *buf;
int len;
{
int n;
n = read(fd, buf, len);
if (n > 0)
raw_stream_out += n;
return n;
}
static int
ClientWritev(fd, iov, iovcnt)
int fd;
struct iovec *iov;
int iovcnt;
{
int n;
n = writev(fd, iov, iovcnt);
if (n > 0)
raw_stream_in += n;
return n;
}
static int
ServerRead (fd, buf, len)
int fd;
char *buf;
int len;
{
int n;
n = read(fd, buf, len);
if (n > 0)
stream_in_plain += n;
return n;
}
static int
ServerWritev(fd, iov, iovcnt)
int fd;
struct iovec *iov;
int iovcnt;
{
int n;
n = writev(fd, iov, iovcnt);
if (n > 0)
stream_out_plain += n;
return n;
}
ClientPtr
AllocNewConnection (fd, to_server)
int fd;
Bool to_server;
{
OsCommPtr oc;
ClientPtr client;
if (fd >= lastfdesc)
return NullClient;
oc = (OsCommPtr)xalloc(sizeof(OsCommRec));
if (!oc)
return NullClient;
oc->fd = fd;
oc->input = (ConnectionInputPtr)NULL;
oc->output = (ConnectionOutputPtr)NULL;
if (to_server) {
oc->Read = ServerRead;
oc->Writev = ServerWritev;
oc->Close = CloseServer;
} else {
oc->Read = ClientRead;
oc->Writev = ClientWritev;
oc->Close = CloseDownFileDescriptor;
}
oc->flushClient = StandardFlushClient;
oc->ofirst = (ConnectionOutputPtr) NULL;
if (!(client = NextAvailableClient((pointer)oc)))
{
xfree (oc);
return NullClient;
}
if (!ConnectionTranslation[fd])
{
ConnectionTranslation[fd] = client->index;
ConnectionOutputTranslation[fd] = client->index;
if (GrabInProgress)
{
FD_SET(fd, &SavedAllClients);
FD_SET(fd, &SavedAllSockets);
}
else
{
FD_SET(fd, &AllClients);
FD_SET(fd, &AllSockets);
}
}
client->public.readRequest = StandardReadRequestFromClient;
client->public.writeToClient = StandardWriteToClient;
client->public.requestLength = StandardRequestLength;
return client;
}
void
SwitchConnectionFuncs (client, Read, Writev)
ClientPtr client;
int (*Read)();
int (*Writev)();
{
OsCommPtr oc = (OsCommPtr) client->osPrivate;
oc->Read = Read;
oc->Writev = Writev;
}
void
StartOutputCompression(client, CompressOn, CompressOff)
ClientPtr client;
void (*CompressOn)();
void (*CompressOff)();
{
OsCommPtr oc = (OsCommPtr) client->osPrivate;
oc->compressOn = CompressOn;
oc->compressOff = CompressOff;
oc->flushClient = LbxFlushClient;
}
/*****************
* EstablishNewConnections
* If anyone is waiting on listened sockets, accept them.
* Returns a mask with indices of new clients. Updates AllClients
* and AllSockets.
*****************/
/*ARGSUSED*/
Bool
EstablishNewConnections(clientUnused, closure)
ClientPtr clientUnused;
pointer closure;
{
fd_mask readyconnections; /* mask of listeners that are ready */
int curconn; /* fd of listener that's ready */
register int newconn; /* fd of new client */
register int i;
register ClientPtr client;
register OsCommPtr oc;
fd_set tmask;
#ifdef TCP_NODELAY
union {
struct sockaddr sa;
#ifdef UNIXCONN
struct sockaddr_un un;
#endif /* UNIXCONN */
#ifdef TCPCONN
struct sockaddr_in in;
#endif /* TCPCONN */
#ifdef DNETCONN
struct sockaddr_dn dn;
#endif /* DNETCONN */
} from;
int fromlen;
#endif /* TCP_NODELAY */
XFD_ANDSET (&tmask, (fd_set*)closure, &WellKnownConnections);
readyconnections = tmask.fds_bits[0];
if (!readyconnections)
return TRUE;
while (readyconnections)
{
curconn = ffs (readyconnections) - 1;
readyconnections &= ~(1 << curconn);
if ((newconn = accept (curconn,
(struct sockaddr *) NULL,
(int *)NULL)) < 0)
continue;
#ifdef TCP_NODELAY
fromlen = sizeof (from);
if (!getpeername (newconn, &from.sa, &fromlen))
{
if (fromlen && (from.sa.sa_family == AF_INET))
{
int mi = 1;
setsockopt (newconn, IPPROTO_TCP, TCP_NODELAY,
(char *)&mi, sizeof (int));
}
}
#endif /* TCP_NODELAY */
/* ultrix reads hang on Unix sockets, hpux reads fail, AIX fails too */
#if defined(O_NONBLOCK) && (!defined(ultrix) && !defined(hpux) && !defined(AIXV3) && !defined(uniosu))
(void) fcntl (newconn, F_SETFL, O_NONBLOCK);
#else
#ifdef FIOSNBIO
{
int arg;
arg = 1;
ioctl(newconn, FIOSNBIO, &arg);
}
#else
#if (defined(AIXV3) || defined(uniosu)) && defined(FIONBIO)
{
int arg;
arg = 1;
ioctl(newconn, FIONBIO, &arg);
}
#else
fcntl (newconn, F_SETFL, FNDELAY);
#endif
#endif
#endif
client = AllocNewConnection (newconn, FALSE);
if (!client)
{
ErrorConnMax(newconn);
close(newconn);
continue;
}
}
return TRUE;
}
#define NOROOM "Maximum number of clients reached"
/************
* ErrorConnMax
* Fail a connection due to lack of client or file descriptor space
************/
static void
ErrorConnMax(fd)
register int fd;
{
xConnSetupPrefix csp;
char pad[3];
struct iovec iov[3];
char byteOrder = 0;
int whichbyte = 1;
struct timeval waittime;
fd_set mask;
/* if these seems like a lot of trouble to go to, it probably is */
waittime.tv_sec = BOTIMEOUT / MILLI_PER_SECOND;
waittime.tv_usec = (BOTIMEOUT % MILLI_PER_SECOND) *
(1000000 / MILLI_PER_SECOND);
FD_ZERO(&mask);
FD_SET(fd, &mask);
(void)Select(fd + 1, &mask, NULL, NULL, &waittime);
/* try to read the byte-order of the connection */
(void)read(fd, &byteOrder, 1);
if ((byteOrder == 'l') || (byteOrder == 'B'))
{
csp.success = xFalse;
csp.lengthReason = sizeof(NOROOM) - 1;
csp.length = (sizeof(NOROOM) + 2) >> 2;
csp.majorVersion = X_PROTOCOL;
csp.minorVersion = X_PROTOCOL_REVISION;
if (((*(char *) &whichbyte) && (byteOrder == 'B')) ||
(!(*(char *) &whichbyte) && (byteOrder == 'l')))
{
swaps(&csp.majorVersion, whichbyte);
swaps(&csp.minorVersion, whichbyte);
swaps(&csp.length, whichbyte);
}
iov[0].iov_len = sz_xConnSetupPrefix;
iov[0].iov_base = (char *) &csp;
iov[1].iov_len = csp.lengthReason;
iov[1].iov_base = NOROOM;
iov[2].iov_len = (4 - (csp.lengthReason & 3)) & 3;
iov[2].iov_base = pad;
(void)ClientWritev(fd, iov, 3);
}
}
/************
* CloseDownFileDescriptor:
* Remove this file descriptor and it's I/O buffers, etc.
************/
void
CloseDownFileDescriptor(client)
ClientPtr client;
{
register OsCommPtr oc = (OsCommPtr) client->osPrivate;
int connection = oc->fd;
close(connection);
ConnectionTranslation[connection] = 0;
ConnectionOutputTranslation[connection] = 0;
FD_CLR(connection, &AllSockets);
FD_CLR(connection, &AllClients);
#ifdef LOCALCONN
FD_CLR(connection, &AllStreams);
#endif
FD_CLR(connection, &ClientsWithInput);
FD_CLR(connection, &GrabImperviousClients);
if (GrabInProgress)
{
FD_CLR(connection, &SavedAllSockets);
FD_CLR(connection, &SavedAllClients);
FD_CLR(connection, &SavedClientsWithInput);
}
FD_CLR(connection, &ClientsWriteBlocked);
if (!XFD_ANYSET(&ClientsWriteBlocked))
AnyClientsWriteBlocked = FALSE;
FD_CLR(connection, &OutputPending);
}
/*****************
* CheckConections
* Some connection has died, go find which one and shut it down
* The file descriptor has been closed, but is still in AllClients.
* If would truly be wonderful if select() would put the bogus
* file descriptors in the exception mask, but nooooo. So we have
* to check each and every socket individually.
*****************/
void
CheckConnections()
{
fd_mask mask;
fd_set tmask;
register int curclient, curoff;
int i;
struct timeval notime;
int r;
notime.tv_sec = 0;
notime.tv_usec = 0;
for (i=0; i<howmany(XFD_SETSIZE, NFDBITS); i++)
{
mask = AllClients.fds_bits[i];
while (mask)
{
curoff = ffs (mask) - 1;
curclient = curoff + (i << 5);
FD_ZERO(&tmask);
FD_SET(curclient, &tmask);
r = Select (curclient + 1, &tmask, NULL, NULL, ¬ime);
if (r < 0)
CloseDownClient(clients[ConnectionTranslation[curclient]]);
mask &= ~(1 << curoff);
}
}
}
/*****************
* CloseDownConnection
* Delete client from AllClients and free resources
*****************/
void
CloseDownConnection(client)
ClientPtr client;
{
OsCommPtr oc = (OsCommPtr)client->osPrivate;
if (oc->output && oc->output->count)
FlushClient(client, oc, (char *)NULL, 0);
ConnectionTranslation[oc->fd] = 0;
(*oc->Close) (client);
FreeOsBuffers(oc);
xfree(oc);
client->osPrivate = (pointer)NULL;
if (auditTrailLevel > 1)
AuditF("client %d disconnected\n", client->index);
}
/*****************
* OnlyListenToOneClient:
* Only accept requests from one client. Continue to handle new
* connections, but don't take any protocol requests from the new
* ones. Note that if GrabInProgress is set, EstablishNewConnections
* needs to put new clients into SavedAllSockets and SavedAllClients.
* Note also that there is no timeout for this in the protocol.
* This routine is "undone" by ListenToAllClients()
*****************/
void
OnlyListenToOneClient(client)
ClientPtr client;
{
OsCommPtr oc = (OsCommPtr)client->osPrivate;
int connection = oc->fd;
if (! GrabInProgress)
{
XFD_COPYSET(&ClientsWithInput, &SavedClientsWithInput);
XFD_ANDSET(&ClientsWithInput,
&ClientsWithInput, &GrabImperviousClients);
if (FD_ISSET(connection, &SavedClientsWithInput))
{
FD_CLR(connection, &SavedClientsWithInput);
FD_SET(connection, &ClientsWithInput);
}
XFD_UNSET(&SavedClientsWithInput, &GrabImperviousClients);
FD_CLR(connection, &AllSockets);
XFD_COPYSET(&AllSockets, &SavedAllSockets);
FD_CLR(connection, &AllClients);
XFD_COPYSET(&AllClients, &SavedAllClients);
XFD_UNSET(&AllSockets, &AllClients);
XFD_ANDSET(&AllClients, &AllClients, &GrabImperviousClients);
FD_SET(connection, &AllClients);
XFD_ORSET(&AllSockets, &AllSockets, &AllClients);
GrabInProgress = client->index;
}
}
/****************
* ListenToAllClients:
* Undoes OnlyListentToOneClient()
****************/
void
ListenToAllClients()
{
if (GrabInProgress)
{
XFD_ORSET(&AllSockets, &AllSockets, &SavedAllSockets);
XFD_ORSET(&AllClients, &AllClients, &SavedAllClients);
XFD_ORSET(&ClientsWithInput, &ClientsWithInput, &SavedClientsWithInput);
GrabInProgress = 0;
}
}
/****************
* IgnoreClient
* Removes one client from input masks.
* Must have cooresponding call to AttendClient.
****************/
void
IgnoreClient (client)
ClientPtr client;
{
OsCommPtr oc = (OsCommPtr)client->osPrivate;
int connection = oc->fd;
if (!GrabInProgress || FD_ISSET (connection, &AllClients))
{
if (FD_ISSET (connection, &ClientsWithInput))
FD_SET(connection, &IgnoredClientsWithInput);
else
FD_CLR(connection, &IgnoredClientsWithInput);
FD_CLR(connection, &ClientsWithInput);
FD_CLR(connection, &AllSockets);
FD_CLR(connection, &AllClients);
FD_CLR(connection, &LastSelectMask);
}
else
{
if (FD_ISSET (connection, &SavedClientsWithInput))
FD_SET(connection, &IgnoredClientsWithInput);
else
FD_CLR(connection, &IgnoredClientsWithInput);
FD_CLR(connection, &SavedClientsWithInput);
FD_CLR(connection, &SavedAllSockets);
FD_CLR(connection, &SavedAllClients);
}
isItTimeToYield = TRUE;
}
/****************
* AttendClient
* Adds one client back into the input masks.
****************/
void
AttendClient (client)
ClientPtr client;
{
OsCommPtr oc = (OsCommPtr)client->osPrivate;
int connection = oc->fd;
if (!GrabInProgress || GrabInProgress == client->index ||
FD_ISSET(connection, &GrabImperviousClients))
{
FD_SET(connection, &AllClients);
FD_SET(connection, &AllSockets);
FD_SET(connection, &LastSelectMask);
if (FD_ISSET (connection, &IgnoredClientsWithInput))
FD_SET(connection, &ClientsWithInput);
}
else
{
FD_SET(connection, &SavedAllClients);
FD_SET(connection, &SavedAllSockets);
if (FD_ISSET(connection, &IgnoredClientsWithInput))
FD_SET(connection, &SavedClientsWithInput);
}
}
/* make client impervious to grabs; assume only executing client calls this */
void
MakeClientGrabImpervious(client)
ClientPtr client;
{
OsCommPtr oc = (OsCommPtr)client->osPrivate;
int connection = oc->fd;
FD_SET(connection, &GrabImperviousClients);
}
/* make client pervious to grabs; assume only executing client calls this */
void
MakeClientGrabPervious(client)
ClientPtr client;
{
OsCommPtr oc = (OsCommPtr)client->osPrivate;
int connection = oc->fd;
FD_CLR(connection, &GrabImperviousClients);
if (GrabInProgress && (GrabInProgress != client->index))
{
if (FD_ISSET(connection, &ClientsWithInput))
{
FD_SET(connection, &SavedClientsWithInput);
FD_CLR(connection, &ClientsWithInput);
}
FD_CLR(connection, &AllSockets);
FD_CLR(connection, &AllClients);
isItTimeToYield = TRUE;
}
}
|