diff options
Diffstat (limited to 'src/sysdep.c')
| -rw-r--r-- | src/sysdep.c | 132 |
1 files changed, 15 insertions, 117 deletions
diff --git a/src/sysdep.c b/src/sysdep.c index 24cc5cb0b40..7158f38dba2 100644 --- a/src/sysdep.c +++ b/src/sysdep.c | |||
| @@ -110,6 +110,9 @@ along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */ | |||
| 110 | #define _P_WAIT 0 | 110 | #define _P_WAIT 0 |
| 111 | int _cdecl _spawnlp (int, const char *, const char *, ...); | 111 | int _cdecl _spawnlp (int, const char *, const char *, ...); |
| 112 | int _cdecl _getpid (void); | 112 | int _cdecl _getpid (void); |
| 113 | /* The following is needed for O_CLOEXEC, F_SETFD, FD_CLOEXEC, and | ||
| 114 | several prototypes of functions called below. */ | ||
| 115 | #include <sys/socket.h> | ||
| 113 | #endif | 116 | #endif |
| 114 | 117 | ||
| 115 | #include "syssignal.h" | 118 | #include "syssignal.h" |
| @@ -1406,29 +1409,19 @@ setup_pty (int fd) | |||
| 1406 | } | 1409 | } |
| 1407 | #endif /* HAVE_PTYS */ | 1410 | #endif /* HAVE_PTYS */ |
| 1408 | 1411 | ||
| 1409 | #ifdef HAVE_SOCKETS | ||
| 1410 | #include <sys/socket.h> | ||
| 1411 | #include <netdb.h> | ||
| 1412 | #endif /* HAVE_SOCKETS */ | ||
| 1413 | |||
| 1414 | #ifdef TRY_AGAIN | ||
| 1415 | #ifndef HAVE_H_ERRNO | ||
| 1416 | extern int h_errno; | ||
| 1417 | #endif | ||
| 1418 | #endif /* TRY_AGAIN */ | ||
| 1419 | |||
| 1420 | void | 1412 | void |
| 1421 | init_system_name (void) | 1413 | init_system_name (void) |
| 1422 | { | 1414 | { |
| 1415 | char *hostname_alloc = NULL; | ||
| 1416 | char *hostname; | ||
| 1423 | #ifndef HAVE_GETHOSTNAME | 1417 | #ifndef HAVE_GETHOSTNAME |
| 1424 | struct utsname uts; | 1418 | struct utsname uts; |
| 1425 | uname (&uts); | 1419 | uname (&uts); |
| 1426 | Vsystem_name = build_string (uts.nodename); | 1420 | hostname = uts.nodename; |
| 1427 | #else /* HAVE_GETHOSTNAME */ | 1421 | #else /* HAVE_GETHOSTNAME */ |
| 1428 | char *hostname_alloc = NULL; | ||
| 1429 | char hostname_buf[256]; | 1422 | char hostname_buf[256]; |
| 1430 | ptrdiff_t hostname_size = sizeof hostname_buf; | 1423 | ptrdiff_t hostname_size = sizeof hostname_buf; |
| 1431 | char *hostname = hostname_buf; | 1424 | hostname = hostname_buf; |
| 1432 | 1425 | ||
| 1433 | /* Try to get the host name; if the buffer is too short, try | 1426 | /* Try to get the host name; if the buffer is too short, try |
| 1434 | again. Apparently, the only indication gethostname gives of | 1427 | again. Apparently, the only indication gethostname gives of |
| @@ -1446,110 +1439,15 @@ init_system_name (void) | |||
| 1446 | hostname = hostname_alloc = xpalloc (hostname_alloc, &hostname_size, 1, | 1439 | hostname = hostname_alloc = xpalloc (hostname_alloc, &hostname_size, 1, |
| 1447 | min (PTRDIFF_MAX, SIZE_MAX), 1); | 1440 | min (PTRDIFF_MAX, SIZE_MAX), 1); |
| 1448 | } | 1441 | } |
| 1449 | #ifdef HAVE_SOCKETS | ||
| 1450 | /* Turn the hostname into the official, fully-qualified hostname. | ||
| 1451 | Don't do this if we're going to dump; this can confuse system | ||
| 1452 | libraries on some machines and make the dumped emacs core dump. */ | ||
| 1453 | #ifndef CANNOT_DUMP | ||
| 1454 | if (initialized) | ||
| 1455 | #endif /* not CANNOT_DUMP */ | ||
| 1456 | if (! strchr (hostname, '.')) | ||
| 1457 | { | ||
| 1458 | int count; | ||
| 1459 | #ifdef HAVE_GETADDRINFO | ||
| 1460 | struct addrinfo *res; | ||
| 1461 | struct addrinfo hints; | ||
| 1462 | int ret; | ||
| 1463 | |||
| 1464 | memset (&hints, 0, sizeof (hints)); | ||
| 1465 | hints.ai_socktype = SOCK_STREAM; | ||
| 1466 | hints.ai_flags = AI_CANONNAME; | ||
| 1467 | |||
| 1468 | for (count = 0;; count++) | ||
| 1469 | { | ||
| 1470 | if ((ret = getaddrinfo (hostname, NULL, &hints, &res)) == 0 | ||
| 1471 | || ret != EAI_AGAIN) | ||
| 1472 | break; | ||
| 1473 | |||
| 1474 | if (count >= 5) | ||
| 1475 | break; | ||
| 1476 | Fsleep_for (make_number (1), Qnil); | ||
| 1477 | } | ||
| 1478 | |||
| 1479 | if (ret == 0) | ||
| 1480 | { | ||
| 1481 | struct addrinfo *it = res; | ||
| 1482 | while (it) | ||
| 1483 | { | ||
| 1484 | char *fqdn = it->ai_canonname; | ||
| 1485 | if (fqdn && strchr (fqdn, '.') | ||
| 1486 | && strcmp (fqdn, "localhost.localdomain") != 0) | ||
| 1487 | break; | ||
| 1488 | it = it->ai_next; | ||
| 1489 | } | ||
| 1490 | if (it) | ||
| 1491 | { | ||
| 1492 | ptrdiff_t len = strlen (it->ai_canonname); | ||
| 1493 | if (hostname_size <= len) | ||
| 1494 | { | ||
| 1495 | hostname_size = len + 1; | ||
| 1496 | hostname = hostname_alloc = xrealloc (hostname_alloc, | ||
| 1497 | hostname_size); | ||
| 1498 | } | ||
| 1499 | strcpy (hostname, it->ai_canonname); | ||
| 1500 | } | ||
| 1501 | freeaddrinfo (res); | ||
| 1502 | } | ||
| 1503 | #else /* !HAVE_GETADDRINFO */ | ||
| 1504 | struct hostent *hp; | ||
| 1505 | for (count = 0;; count++) | ||
| 1506 | { | ||
| 1507 | |||
| 1508 | #ifdef TRY_AGAIN | ||
| 1509 | h_errno = 0; | ||
| 1510 | #endif | ||
| 1511 | hp = gethostbyname (hostname); | ||
| 1512 | #ifdef TRY_AGAIN | ||
| 1513 | if (! (hp == 0 && h_errno == TRY_AGAIN)) | ||
| 1514 | #endif | ||
| 1515 | |||
| 1516 | break; | ||
| 1517 | |||
| 1518 | if (count >= 5) | ||
| 1519 | break; | ||
| 1520 | Fsleep_for (make_number (1), Qnil); | ||
| 1521 | } | ||
| 1522 | |||
| 1523 | if (hp) | ||
| 1524 | { | ||
| 1525 | char *fqdn = (char *) hp->h_name; | ||
| 1526 | |||
| 1527 | if (!strchr (fqdn, '.')) | ||
| 1528 | { | ||
| 1529 | /* We still don't have a fully qualified domain name. | ||
| 1530 | Try to find one in the list of alternate names */ | ||
| 1531 | char **alias = hp->h_aliases; | ||
| 1532 | while (*alias | ||
| 1533 | && (!strchr (*alias, '.') | ||
| 1534 | || !strcmp (*alias, "localhost.localdomain"))) | ||
| 1535 | alias++; | ||
| 1536 | if (*alias) | ||
| 1537 | fqdn = *alias; | ||
| 1538 | } | ||
| 1539 | hostname = fqdn; | ||
| 1540 | } | ||
| 1541 | #endif /* !HAVE_GETADDRINFO */ | ||
| 1542 | } | ||
| 1543 | #endif /* HAVE_SOCKETS */ | ||
| 1544 | Vsystem_name = build_string (hostname); | ||
| 1545 | xfree (hostname_alloc); | ||
| 1546 | #endif /* HAVE_GETHOSTNAME */ | 1442 | #endif /* HAVE_GETHOSTNAME */ |
| 1547 | { | 1443 | char *p; |
| 1548 | char *p; | 1444 | for (p = hostname; *p; p++) |
| 1549 | for (p = SSDATA (Vsystem_name); *p; p++) | 1445 | if (*p == ' ' || *p == '\t') |
| 1550 | if (*p == ' ' || *p == '\t') | 1446 | *p = '-'; |
| 1551 | *p = '-'; | 1447 | if (! (STRINGP (Vsystem_name) && SBYTES (Vsystem_name) == p - hostname |
| 1552 | } | 1448 | && strcmp (SSDATA (Vsystem_name), hostname) == 0)) |
| 1449 | Vsystem_name = build_string (hostname); | ||
| 1450 | xfree (hostname_alloc); | ||
| 1553 | } | 1451 | } |
| 1554 | 1452 | ||
| 1555 | sigset_t empty_mask; | 1453 | sigset_t empty_mask; |