comparison src/process-unix.c @ 272:c5d627a313b1 r21-0b34

Import from CVS: tag r21-0b34
author cvs
date Mon, 13 Aug 2007 10:28:48 +0200
parents 727739f917cb
children ca9a9ec9c1c1
comparison
equal deleted inserted replaced
271:c7b7086b0a39 272:c5d627a313b1
318 struct hostent *host_info_ptr = NULL; 318 struct hostent *host_info_ptr = NULL;
319 #ifdef TRY_AGAIN 319 #ifdef TRY_AGAIN
320 int count = 0; 320 int count = 0;
321 #endif 321 #endif
322 322
323 memset (address, 0, sizeof (*address)); 323 xzero (*address);
324 324
325 while (1) 325 while (1)
326 { 326 {
327 #ifdef TRY_AGAIN 327 #ifdef TRY_AGAIN
328 if (count++ > 10) break; 328 if (count++ > 10) break;
329 #ifndef BROKEN_CYGWIN 329 #ifndef BROKEN_CYGWIN
330 h_errno = 0; 330 h_errno = 0;
331 #endif 331 #endif
332 #endif 332 #endif
333 /* Some systems can't handle SIGIO/SIGALARM in gethostbyname. */ 333 /* Some systems can't handle SIGIO/SIGALARM in gethostbyname. */
334 slow_down_interrupts (); 334 slow_down_interrupts ();
335 host_info_ptr = gethostbyname ((char *) XSTRING_DATA (host)); 335 host_info_ptr = gethostbyname ((char *) XSTRING_DATA (host));
645 645
646 /**********************************************************************/ 646 /**********************************************************************/
647 /* Process implementation methods */ 647 /* Process implementation methods */
648 /**********************************************************************/ 648 /**********************************************************************/
649 649
650 /* 650 /*
651 * Allocate and initialize Lisp_Process->process_data 651 * Allocate and initialize Lisp_Process->process_data
652 */ 652 */
653 653
654 static void 654 static void
655 unix_alloc_process_data (struct Lisp_Process *p) 655 unix_alloc_process_data (struct Lisp_Process *p)
703 } 703 }
704 704
705 /* 705 /*
706 * Fork off a subprocess. P is a pointer to newly created subprocess 706 * Fork off a subprocess. P is a pointer to newly created subprocess
707 * object. If this function signals, the caller is responsible for 707 * object. If this function signals, the caller is responsible for
708 * deleting (and finalizing) the process object. 708 * deleting (and finalizing) the process object.
709 * 709 *
710 * The method must return PID of the new proces, a (positive??? ####) number 710 * The method must return PID of the new proces, a (positive??? ####) number
711 * which fits into Lisp_Int. No return value indicates an error, the method 711 * which fits into Lisp_Int. No return value indicates an error, the method
712 * must signal an error instead. 712 * must signal an error instead.
713 */ 713 */
1124 EMACS_UNBLOCK_SIGNAL (signum); 1124 EMACS_UNBLOCK_SIGNAL (signum);
1125 LONGJMP (send_process_frame, 1); 1125 LONGJMP (send_process_frame, 1);
1126 } 1126 }
1127 1127
1128 static void 1128 static void
1129 unix_send_process (volatile Lisp_Object proc, struct lstream* lstream) 1129 unix_send_process (Lisp_Object proc, struct lstream* lstream)
1130 { 1130 {
1131 /* Use volatile to protect variables from being clobbered by longjmp. */ 1131 /* Use volatile to protect variables from being clobbered by longjmp. */
1132 SIGTYPE (*volatile old_sigpipe) (int) = 0; 1132 SIGTYPE (*volatile old_sigpipe) (int) = 0;
1133 volatile struct Lisp_Process *p = XPROCESS (proc); 1133 volatile Lisp_Object vol_proc = proc;
1134 1134 struct Lisp_Process *volatile p = XPROCESS (proc);
1135
1135 if (!SETJMP (send_process_frame)) 1136 if (!SETJMP (send_process_frame))
1136 { 1137 {
1137 /* use a reasonable-sized buffer (somewhere around the size of the 1138 /* use a reasonable-sized buffer (somewhere around the size of the
1138 stream buffer) so as to avoid inundating the stream with blocked 1139 stream buffer) so as to avoid inundating the stream with blocked
1139 data. */ 1140 data. */
1158 signal (SIGPIPE, old_sigpipe); 1159 signal (SIGPIPE, old_sigpipe);
1159 if (writeret < 0) 1160 if (writeret < 0)
1160 /* This is a real error. Blocking errors are handled 1161 /* This is a real error. Blocking errors are handled
1161 specially inside of the filedesc stream. */ 1162 specially inside of the filedesc stream. */
1162 report_file_error ("writing to process", 1163 report_file_error ("writing to process",
1163 list1 (proc)); 1164 list1 (vol_proc));
1164 while (Lstream_was_blocked_p (XLSTREAM (p->pipe_outstream))) 1165 while (Lstream_was_blocked_p (XLSTREAM (p->pipe_outstream)))
1165 { 1166 {
1166 /* Buffer is full. Wait, accepting input; 1167 /* Buffer is full. Wait, accepting input;
1167 that may allow the program 1168 that may allow the program
1168 to finish doing output and read more. */ 1169 to finish doing output and read more. */
1180 p->status_symbol = Qexit; 1181 p->status_symbol = Qexit;
1181 p->exit_code = 256; /* #### SIGPIPE ??? */ 1182 p->exit_code = 256; /* #### SIGPIPE ??? */
1182 p->core_dumped = 0; 1183 p->core_dumped = 0;
1183 p->tick++; 1184 p->tick++;
1184 process_tick++; 1185 process_tick++;
1185 deactivate_process (proc); 1186 deactivate_process (vol_proc);
1186 error ("SIGPIPE raised on process %s; closed it", 1187 error ("SIGPIPE raised on process %s; closed it",
1187 XSTRING_DATA (p->name)); 1188 XSTRING_DATA (p->name));
1188 } 1189 }
1189 1190
1190 old_sigpipe = (SIGTYPE (*) (int)) signal (SIGPIPE, send_process_trap); 1191 old_sigpipe = (SIGTYPE (*) (int)) signal (SIGPIPE, send_process_trap);
1191 Lstream_flush (XLSTREAM (DATA_OUTSTREAM(p))); 1192 Lstream_flush (XLSTREAM (DATA_OUTSTREAM(p)));
1192 signal (SIGPIPE, old_sigpipe); 1193 signal (SIGPIPE, old_sigpipe);
1193 } 1194 }
1194 1195
1243 1244
1244 /* closing the outstream could result in SIGPIPE, so ignore it. */ 1245 /* closing the outstream could result in SIGPIPE, so ignore it. */
1245 old_sigpipe = (SIGTYPE (*) (int)) signal (SIGPIPE, SIG_IGN); 1246 old_sigpipe = (SIGTYPE (*) (int)) signal (SIGPIPE, SIG_IGN);
1246 usid = event_stream_delete_stream_pair (p->pipe_instream, p->pipe_outstream); 1247 usid = event_stream_delete_stream_pair (p->pipe_instream, p->pipe_outstream);
1247 signal (SIGPIPE, old_sigpipe); 1248 signal (SIGPIPE, old_sigpipe);
1248 1249
1249 UNIX_DATA(p)->infd = -1; 1250 UNIX_DATA(p)->infd = -1;
1250 1251
1251 return usid; 1252 return usid;
1252 } 1253 }
1253 1254
1352 kill_retval = kill (XINT (p->pid), signo) ? errno : 0; 1353 kill_retval = kill (XINT (p->pid), signo) ? errno : 0;
1353 } 1354 }
1354 else 1355 else
1355 { 1356 {
1356 /* gid may be a pid, or minus a pgrp's number */ 1357 /* gid may be a pid, or minus a pgrp's number */
1357 #ifdef TIOCSIGSEND 1358 #if defined (TIOCSIGNAL) || defined (TIOCSIGSEND)
1358 if (current_group) 1359 if (current_group)
1359 kill_retval = ioctl (UNIX_DATA(p)->infd, TIOCSIGSEND, signo); 1360 {
1361 #ifdef (TIOCSIGNAL)
1362 kill_retval = ioctl (UNIX_DATA(p)->infd, TIOCSIGNAL, signo);
1363 #else /* ! defined (TIOCSIGNAL) */
1364 kill_retval = ioctl (UNIX_DATA(p)->infd, TIOCSIGSEND, signo);
1365 #endif /* ! defined (TIOCSIGNAL) */
1366 }
1360 else 1367 else
1361 kill_retval = kill (- XINT (p->pid), signo) ? errno : 0; 1368 kill_retval = kill (- XINT (p->pid), signo) ? errno : 0;
1362 #else /* ! defined (TIOCSIGSEND) */ 1369 #else /* ! (defined (TIOCSIGNAL) || defined (TIOCSIGSEND)) */
1363 kill_retval = EMACS_KILLPG (-gid, signo) ? errno : 0; 1370 kill_retval = EMACS_KILLPG (-gid, signo) ? errno : 0;
1364 #endif /* ! defined (TIOCSIGSEND) */ 1371 #endif /* ! (defined (TIOCSIGNAL) || defined (TIOCSIGSEND)) */
1365 } 1372 }
1366 1373
1367 if (kill_retval < 0 && errno == EINVAL) 1374 if (kill_retval < 0 && errno == EINVAL)
1368 error ("Signal number %d is invalid for this system", make_int (signo)); 1375 error ("Signal number %d is invalid for this system", signo);
1369 } 1376 }
1370 1377
1371 /* 1378 /*
1372 * Kill any process in the system given its PID. 1379 * Kill any process in the system given its PID.
1373 * 1380 *
1374 * Returns zero if a signal successfully sent, or 1381 * Returns zero if a signal successfully sent, or
1409 if (address.sin_family == AF_INET) 1416 if (address.sin_family == AF_INET)
1410 return build_string (inet_ntoa (address.sin_addr)); 1417 return build_string (inet_ntoa (address.sin_addr));
1411 else 1418 else
1412 /* #### any clue what to do here? */ 1419 /* #### any clue what to do here? */
1413 return host; 1420 return host;
1414 } 1421 }
1415 1422
1416 /* open a TCP network connection to a given HOST/SERVICE. Treated 1423 /* open a TCP network connection to a given HOST/SERVICE. Treated
1417 exactly like a normal process when reading and writing. Only 1424 exactly like a normal process when reading and writing. Only
1418 differences are in status display and process deletion. A network 1425 differences are in status display and process deletion. A network
1419 connection has no PID; you cannot signal it. All you can do is 1426 connection has no PID; you cannot signal it. All you can do is
1426 struct sockaddr_in address; 1433 struct sockaddr_in address;
1427 int s, inch, outch; 1434 int s, inch, outch;
1428 volatile int port; 1435 volatile int port;
1429 volatile int retry = 0; 1436 volatile int retry = 0;
1430 int retval; 1437 int retval;
1431 1438
1432 CHECK_STRING (host); 1439 CHECK_STRING (host);
1433 1440
1434 if (!EQ (family, Qtcpip)) 1441 if (!EQ (family, Qtcpip))
1435 error ("Unsupported protocol family \"%s\"", 1442 error ("Unsupported protocol family \"%s\"",
1436 string_data (symbol_name (XSYMBOL (family)))); 1443 string_data (symbol_name (XSYMBOL (family))));
1530 1537
1531 set_socket_nonblocking_maybe (inch, port, "tcp"); 1538 set_socket_nonblocking_maybe (inch, port, "tcp");
1532 1539
1533 *vinfd = (void*)inch; 1540 *vinfd = (void*)inch;
1534 *voutfd = (void*)outch; 1541 *voutfd = (void*)outch;
1535 } 1542 }
1536 1543
1537 1544
1538 #ifdef HAVE_MULTICAST 1545 #ifdef HAVE_MULTICAST
1539 1546
1540 /* Didier Verna <verna@inf.enst.fr> Nov. 28 1997. 1547 /* Didier Verna <verna@inf.enst.fr> Nov. 28 1997.
1568 int one = 1; /* For REUSEADDR */ 1575 int one = 1; /* For REUSEADDR */
1569 int ret; 1576 int ret;
1570 volatile int retry = 0; 1577 volatile int retry = 0;
1571 1578
1572 CHECK_STRING (dest); 1579 CHECK_STRING (dest);
1573 1580
1574 CHECK_NATNUM (port); 1581 CHECK_NATNUM (port);
1575 theport = htons ((unsigned short) XINT (port)); 1582 theport = htons ((unsigned short) XINT (port));
1576 1583
1577 CHECK_NATNUM (ttl); 1584 CHECK_NATNUM (ttl);
1578 thettl = (unsigned char) XINT (ttl); 1585 thettl = (unsigned char) XINT (ttl);
1579 1586
1580 if ((udp = getprotobyname ("udp")) == NULL) 1587 if ((udp = getprotobyname ("udp")) == NULL)
1581 error ("No info available for UDP protocol"); 1588 error ("No info available for UDP protocol");
1582 1589
1583 /* Init the sockets. Yes, I need 2 sockets. I couldn't duplicate one. */ 1590 /* Init the sockets. Yes, I need 2 sockets. I couldn't duplicate one. */
1584 if ((rs = socket (PF_INET, SOCK_DGRAM, udp->p_proto)) < 0) 1591 if ((rs = socket (PF_INET, SOCK_DGRAM, udp->p_proto)) < 0)
1585 report_file_error ("error creating socket", list1(name)); 1592 report_file_error ("error creating socket", list1(name));
1586 if ((ws = socket (PF_INET, SOCK_DGRAM, udp->p_proto)) < 0) 1593 if ((ws = socket (PF_INET, SOCK_DGRAM, udp->p_proto)) < 0)
1587 { 1594 {
1588 close (rs); 1595 close (rs);
1589 report_file_error ("error creating socket", list1(name)); 1596 report_file_error ("error creating socket", list1(name));
1590 } 1597 }
1591 1598
1592 /* This will be used for both sockets */ 1599 /* This will be used for both sockets */
1593 bzero(&sa, sizeof(sa)); 1600 bzero(&sa, sizeof(sa));
1594 sa.sin_family = AF_INET; 1601 sa.sin_family = AF_INET;
1595 sa.sin_port = theport; 1602 sa.sin_port = theport;
1596 sa.sin_addr.s_addr = htonl (inet_addr ((char *) XSTRING_DATA (dest))); 1603 sa.sin_addr.s_addr = htonl (inet_addr ((char *) XSTRING_DATA (dest)));
1597 1604
1598 /* Socket configuration for reading ------------------------ */ 1605 /* Socket configuration for reading ------------------------ */
1599 1606
1600 /* Multiple connections from the same machine. This must be done before 1607 /* Multiple connections from the same machine. This must be done before
1601 bind. If it fails, it shouldn't be fatal. The only consequence is that 1608 bind. If it fails, it shouldn't be fatal. The only consequence is that
1602 people won't be able to connect twice from the same machine. */ 1609 people won't be able to connect twice from the same machine. */
1603 if (setsockopt (rs, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof (one)) 1610 if (setsockopt (rs, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof (one))
1604 < 0) 1611 < 0)
1605 warn_when_safe (Qmulticast, Qwarning, "Cannot reuse socket address"); 1612 warn_when_safe (Qmulticast, Qwarning, "Cannot reuse socket address");
1606 1613
1607 /* bind socket name */ 1614 /* bind socket name */
1608 if (bind (rs, (struct sockaddr *)&sa, sizeof(sa))) 1615 if (bind (rs, (struct sockaddr *)&sa, sizeof(sa)))
1609 { 1616 {
1610 close (rs); 1617 close (rs);
1611 close (ws); 1618 close (ws);
1612 report_file_error ("error binding socket", list2(name, port)); 1619 report_file_error ("error binding socket", list2(name, port));
1613 } 1620 }
1614 1621
1615 /* join multicast group */ 1622 /* join multicast group */
1616 imr.imr_multiaddr.s_addr = htonl (inet_addr ((char *) XSTRING_DATA (dest))); 1623 imr.imr_multiaddr.s_addr = htonl (inet_addr ((char *) XSTRING_DATA (dest)));
1617 imr.imr_interface.s_addr = htonl (INADDR_ANY); 1624 imr.imr_interface.s_addr = htonl (INADDR_ANY);
1618 if (setsockopt (rs, IPPROTO_IP, IP_ADD_MEMBERSHIP, 1625 if (setsockopt (rs, IPPROTO_IP, IP_ADD_MEMBERSHIP,
1619 (char *) &imr, sizeof (struct ip_mreq)) < 0) 1626 (char *) &imr, sizeof (struct ip_mreq)) < 0)
1620 { 1627 {
1621 close (ws); 1628 close (ws);
1622 close (rs); 1629 close (rs);
1623 report_file_error ("error adding membership", list2(name, dest)); 1630 report_file_error ("error adding membership", list2(name, dest));
1624 } 1631 }
1625 1632
1626 /* Socket configuration for writing ----------------------- */ 1633 /* Socket configuration for writing ----------------------- */
1627 1634
1628 /* Normaly, there's no 'connect' in multicast, since we use preferentialy 1635 /* Normaly, there's no 'connect' in multicast, since we use preferentialy
1629 'sendto' and 'recvfrom'. However, in order to handle this connection in 1636 'sendto' and 'recvfrom'. However, in order to handle this connection in
1630 the process-like way it is done for TCP, we must be able to use 'write' 1637 the process-like way it is done for TCP, we must be able to use 'write'
1631 instead of 'sendto'. Consequently, we 'connect' this socket. */ 1638 instead of 'sendto'. Consequently, we 'connect' this socket. */
1632 1639
1633 /* See open-network-stream-internal for comments on this part of the code */ 1640 /* See open-network-stream-internal for comments on this part of the code */
1634 slow_down_interrupts (); 1641 slow_down_interrupts ();
1635 1642
1636 loop: 1643 loop:
1637 1644
1638 /* A system call interrupted with a SIGALRM or SIGIO comes back 1645 /* A system call interrupted with a SIGALRM or SIGIO comes back
1639 here, with can_break_system_calls reset to 0. */ 1646 here, with can_break_system_calls reset to 0. */
1640 SETJMP (break_system_call_jump); 1647 SETJMP (break_system_call_jump);
1641 if (QUITP) 1648 if (QUITP)
1642 { 1649 {
1643 speed_up_interrupts (); 1650 speed_up_interrupts ();
1644 REALLY_QUIT; 1651 REALLY_QUIT;
1645 /* In case something really weird happens ... */ 1652 /* In case something really weird happens ... */
1646 slow_down_interrupts (); 1653 slow_down_interrupts ();
1647 } 1654 }
1648 1655
1649 /* Break out of connect with a signal (it isn't otherwise possible). 1656 /* Break out of connect with a signal (it isn't otherwise possible).
1650 Thus you don't get screwed with a hung network. */ 1657 Thus you don't get screwed with a hung network. */
1651 can_break_system_calls = 1; 1658 can_break_system_calls = 1;
1652 ret = connect (ws, (struct sockaddr *) &sa, sizeof (sa)); 1659 ret = connect (ws, (struct sockaddr *) &sa, sizeof (sa));
1653 can_break_system_calls = 0; 1660 can_break_system_calls = 0;
1654 if (ret == -1 && errno != EISCONN) 1661 if (ret == -1 && errno != EISCONN)
1655 { 1662 {
1656 int xerrno = errno; 1663 int xerrno = errno;
1657 1664
1658 if (errno == EINTR) 1665 if (errno == EINTR)
1659 goto loop; 1666 goto loop;
1660 if (errno == EADDRINUSE && retry < 20) 1667 if (errno == EADDRINUSE && retry < 20)
1661 { 1668 {
1662 /* A delay here is needed on some FreeBSD systems, 1669 /* A delay here is needed on some FreeBSD systems,
1663 and it is harmless, since this retrying takes time anyway 1670 and it is harmless, since this retrying takes time anyway
1664 and should be infrequent. 1671 and should be infrequent.
1665 `sleep-for' allowed for quitting this loop with interrupts 1672 `sleep-for' allowed for quitting this loop with interrupts
1666 slowed down so it can't be used here. Async timers should 1673 slowed down so it can't be used here. Async timers should
1667 already be disabled at this point so we can use `sleep'. */ 1674 already be disabled at this point so we can use `sleep'. */
1668 sleep (1); 1675 sleep (1);
1669 retry++; 1676 retry++;
1670 goto loop; 1677 goto loop;
1671 } 1678 }
1672 1679
1673 close (rs); 1680 close (rs);
1674 close (ws); 1681 close (ws);
1675 speed_up_interrupts (); 1682 speed_up_interrupts ();
1676 1683
1677 errno = xerrno; 1684 errno = xerrno;
1678 report_file_error ("error connecting socket", list2(name, port)); 1685 report_file_error ("error connecting socket", list2(name, port));
1679 } 1686 }
1680 1687
1681 speed_up_interrupts (); 1688 speed_up_interrupts ();
1682 1689
1683 /* scope */ 1690 /* scope */
1684 if (setsockopt (ws, IPPROTO_IP, IP_MULTICAST_TTL, 1691 if (setsockopt (ws, IPPROTO_IP, IP_MULTICAST_TTL,
1685 (char *) &thettl, sizeof (thettl)) < 0) 1692 (char *) &thettl, sizeof (thettl)) < 0)
1686 { 1693 {
1687 close (rs); 1694 close (rs);
1688 close (ws); 1695 close (ws);
1689 report_file_error ("error setting ttl", list2(name, ttl)); 1696 report_file_error ("error setting ttl", list2(name, ttl));
1690 } 1697 }
1691 1698
1692 set_socket_nonblocking_maybe (rs, theport, "udp"); 1699 set_socket_nonblocking_maybe (rs, theport, "udp");
1693 1700
1694 *vinfd = (void*)rs; 1701 *vinfd = (void*)rs;
1695 *voutfd = (void*)ws; 1702 *voutfd = (void*)ws;
1696 } 1703 }