1 |
/* Asynchronous subprocess control for GNU Emacs. |
/* Asynchronous subprocess control for GNU Emacs. |
2 |
Copyright (C) 1985, 86, 87, 88, 93, 94, 95, 96, 98, 1999, 2001 |
Copyright (C) 1985, 86, 87, 88, 93, 94, 95, 96, 98, 1999, |
3 |
Free Software Foundation, Inc. |
2001, 2002 Free Software Foundation, Inc. |
4 |
|
|
5 |
This file is part of GNU Emacs. |
This file is part of GNU Emacs. |
6 |
|
|
112 |
#include "atimer.h" |
#include "atimer.h" |
113 |
|
|
114 |
Lisp_Object Qprocessp; |
Lisp_Object Qprocessp; |
115 |
Lisp_Object Qrun, Qstop, Qsignal, Qopen, Qclosed; |
Lisp_Object Qrun, Qstop, Qsignal; |
116 |
|
Lisp_Object Qopen, Qclosed, Qconnect, Qfailed; |
117 |
Lisp_Object Qlast_nonmenu_event; |
Lisp_Object Qlast_nonmenu_event; |
118 |
/* Qexit is declared and initialized in eval.c. */ |
/* Qexit is declared and initialized in eval.c. */ |
119 |
|
|
174 |
/* Number of events for which the user or sentinel has been notified. */ |
/* Number of events for which the user or sentinel has been notified. */ |
175 |
int update_tick; |
int update_tick; |
176 |
|
|
177 |
|
/* Define NON_BLOCKING_CONNECT if we can support non-blocking connects. */ |
178 |
|
|
179 |
|
#ifdef BROKEN_NON_BLOCKING_CONNECT |
180 |
|
#undef NON_BLOCKING_CONNECT |
181 |
|
#else |
182 |
|
#ifndef NON_BLOCKING_CONNECT |
183 |
|
#ifdef HAVE_SOCKETS |
184 |
|
#ifdef HAVE_SELECT |
185 |
|
#if defined (HAVE_GETPEERNAME) || defined (GNU_LINUX) |
186 |
|
#if defined (O_NONBLOCK) || defined (O_NDELAY) |
187 |
|
#if defined (EWOULDBLOCK) || defined (EINPROGRESS) |
188 |
|
#define NON_BLOCKING_CONNECT |
189 |
|
#endif /* EWOULDBLOCK || EINPROGRESS */ |
190 |
|
#endif /* O_NONBLOCK || O_NDELAY */ |
191 |
|
#endif /* HAVE_GETPEERNAME || GNU_LINUX */ |
192 |
|
#endif /* HAVE_SELECT */ |
193 |
|
#endif /* HAVE_SOCKETS */ |
194 |
|
#endif /* NON_BLOCKING_CONNECT */ |
195 |
|
#endif /* BROKEN_NON_BLOCKING_CONNECT */ |
196 |
|
|
197 |
|
#ifdef TERM |
198 |
|
#undef NON_BLOCKING_CONNECT |
199 |
|
#endif |
200 |
|
|
201 |
#include "sysselect.h" |
#include "sysselect.h" |
202 |
|
|
203 |
extern int keyboard_bit_set P_ ((SELECT_TYPE *)); |
extern int keyboard_bit_set P_ ((SELECT_TYPE *)); |
220 |
|
|
221 |
static SELECT_TYPE non_process_wait_mask; |
static SELECT_TYPE non_process_wait_mask; |
222 |
|
|
223 |
|
/* Mask of bits indicating the descriptors that we wait for connect to |
224 |
|
complete on. Once they complete, they are removed from this mask |
225 |
|
and added to the input_wait_mask and non_keyboard_wait_mask. */ |
226 |
|
|
227 |
|
static SELECT_TYPE connect_wait_mask; |
228 |
|
|
229 |
|
/* Number of bits set in connect_wait_mask. */ |
230 |
|
static int num_pending_connects; |
231 |
|
|
232 |
/* The largest descriptor currently in use for a process object. */ |
/* The largest descriptor currently in use for a process object. */ |
233 |
static int max_process_desc; |
static int max_process_desc; |
234 |
|
|
258 |
static struct coding_system *proc_encode_coding_system[MAXDESC]; |
static struct coding_system *proc_encode_coding_system[MAXDESC]; |
259 |
|
|
260 |
static Lisp_Object get_process (); |
static Lisp_Object get_process (); |
261 |
|
static void exec_sentinel (); |
262 |
|
|
263 |
extern EMACS_TIME timer_check (); |
extern EMACS_TIME timer_check (); |
264 |
extern int timers_run; |
extern int timers_run; |
370 |
return concat2 (build_string ("exited abnormally with code "), |
return concat2 (build_string ("exited abnormally with code "), |
371 |
concat2 (string, string2)); |
concat2 (string, string2)); |
372 |
} |
} |
373 |
|
else if (EQ (symbol, Qfailed)) |
374 |
|
{ |
375 |
|
string = Fnumber_to_string (make_number (code)); |
376 |
|
string2 = build_string ("\n"); |
377 |
|
return concat2 (build_string ("failed with code "), |
378 |
|
concat2 (string, string2)); |
379 |
|
} |
380 |
else |
else |
381 |
return Fcopy_sequence (Fsymbol_name (symbol)); |
return Fcopy_sequence (Fsymbol_name (symbol)); |
382 |
} |
} |
1783 |
deactivate and close it via delete-process */ |
deactivate and close it via delete-process */ |
1784 |
|
|
1785 |
DEFUN ("open-network-stream", Fopen_network_stream, Sopen_network_stream, |
DEFUN ("open-network-stream", Fopen_network_stream, Sopen_network_stream, |
1786 |
4, 4, 0, |
4, 7, 0, |
1787 |
doc: /* Open a TCP connection for a service to a host. |
doc: /* Open a TCP connection for a service to a host. |
1788 |
Returns a subprocess-object to represent the connection. |
Returns a subprocess-object to represent the connection. |
1789 |
|
Returns nil if a non-blocking connect is attempted on a system which |
1790 |
|
cannot support that; in that case, the caller should attempt a |
1791 |
|
normal connect instead. |
1792 |
|
|
1793 |
Input and output work as for subprocesses; `delete-process' closes it. |
Input and output work as for subprocesses; `delete-process' closes it. |
1794 |
Args are NAME BUFFER HOST SERVICE. |
Args are NAME BUFFER HOST SERVICE FILTER SENTINEL NON-BLOCKING. |
1795 |
NAME is name for process. It is modified if necessary to make it unique. |
NAME is name for process. It is modified if necessary to make it unique. |
1796 |
BUFFER is the buffer (or buffer-name) to associate with the process. |
BUFFER is the buffer (or buffer-name) to associate with the process. |
1797 |
Process output goes at end of that buffer, unless you specify |
Process output goes at end of that buffer, unless you specify |
1798 |
an output stream or filter function to handle the output. |
an output stream or filter function to handle the output. |
1799 |
BUFFER may be also nil, meaning that this process is not associated |
BUFFER may be also nil, meaning that this process is not associated |
1800 |
with any buffer |
with any buffer. |
1801 |
Third arg is name of the host to connect to, or its IP address. |
HOST is name of the host to connect to, or its IP address. |
1802 |
Fourth arg SERVICE is name of the service desired, or an integer |
SERVICE is name of the service desired, or an integer specifying a |
1803 |
specifying a port number to connect to. */) |
port number to connect to. |
1804 |
(name, buffer, host, service) |
FILTER and SENTINEL are optional args specifying the filter and |
1805 |
Lisp_Object name, buffer, host, service; |
sentinel functions associated with the network stream. |
1806 |
|
NON-BLOCKING is optional arg requesting an non-blocking connect. |
1807 |
|
When non-nil, open-network-stream will return immediately without |
1808 |
|
waiting for the connection to be made. Instead, the sentinel function |
1809 |
|
will be called with second matching "open" (if successful) or |
1810 |
|
"failed" when the connect completes. */) |
1811 |
|
(name, buffer, host, service, filter, sentinel, non_blocking) |
1812 |
|
Lisp_Object name, buffer, host, service, filter, sentinel, non_blocking; |
1813 |
{ |
{ |
1814 |
Lisp_Object proc; |
Lisp_Object proc; |
1815 |
#ifdef HAVE_GETADDRINFO |
#ifdef HAVE_GETADDRINFO |
1816 |
struct addrinfo hints, *res, *lres; |
struct addrinfo hints, *res, *lres; |
|
int ret = 0; |
|
|
int xerrno = 0; |
|
1817 |
char *portstring, portbuf[128]; |
char *portstring, portbuf[128]; |
1818 |
#else /* HAVE_GETADDRINFO */ |
#else /* HAVE_GETADDRINFO */ |
1819 |
struct sockaddr_in address; |
struct sockaddr_in address; |
1822 |
char *(addr_list[2]); |
char *(addr_list[2]); |
1823 |
IN_ADDR numeric_addr; |
IN_ADDR numeric_addr; |
1824 |
int port; |
int port; |
1825 |
|
struct _emacs_addrinfo |
1826 |
|
{ |
1827 |
|
int ai_family; |
1828 |
|
int ai_socktype; |
1829 |
|
int ai_protocol; |
1830 |
|
int ai_addrlen; |
1831 |
|
struct sockaddr *ai_addr; |
1832 |
|
struct _emacs_addrinfo *ai_next; |
1833 |
|
} ai, *res, *lres; |
1834 |
#endif /* HAVE_GETADDRINFO */ |
#endif /* HAVE_GETADDRINFO */ |
1835 |
|
int ret = 0; |
1836 |
|
int xerrno = 0; |
1837 |
int s = -1, outch, inch; |
int s = -1, outch, inch; |
1838 |
struct gcpro gcpro1, gcpro2, gcpro3, gcpro4; |
struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5; |
1839 |
int retry = 0; |
int retry = 0; |
1840 |
int count = specpdl_ptr - specpdl; |
int count = specpdl_ptr - specpdl; |
1841 |
int count1; |
int count1; |
1842 |
|
int is_non_blocking = 0; |
1843 |
|
|
1844 |
|
if (!NILP (non_blocking)) |
1845 |
|
{ |
1846 |
|
#ifndef NON_BLOCKING_CONNECT |
1847 |
|
return Qnil; |
1848 |
|
#else |
1849 |
|
non_blocking = Qt; /* Instead of GCPRO */ |
1850 |
|
is_non_blocking = 1; |
1851 |
|
#endif |
1852 |
|
} |
1853 |
|
|
1854 |
#ifdef WINDOWSNT |
#ifdef WINDOWSNT |
1855 |
/* Ensure socket support is loaded if available. */ |
/* Ensure socket support is loaded if available. */ |
1856 |
init_winsock (TRUE); |
init_winsock (TRUE); |
1857 |
#endif |
#endif |
1858 |
|
|
1859 |
GCPRO4 (name, buffer, host, service); |
/* Can only GCPRO 5 variables */ |
1860 |
|
sentinel = Fcons (sentinel, filter); |
1861 |
|
GCPRO5 (name, buffer, host, service, sentinel); |
1862 |
CHECK_STRING (name); |
CHECK_STRING (name); |
1863 |
CHECK_STRING (host); |
CHECK_STRING (host); |
1864 |
|
|
1916 |
#endif |
#endif |
1917 |
immediate_quit = 0; |
immediate_quit = 0; |
1918 |
|
|
|
/* Do this in case we never enter the for-loop below. */ |
|
|
count1 = specpdl_ptr - specpdl; |
|
|
s = -1; |
|
|
|
|
|
for (lres = res; lres; lres = lres->ai_next) |
|
|
{ |
|
|
s = socket (lres->ai_family, lres->ai_socktype, lres->ai_protocol); |
|
|
if (s < 0) |
|
|
{ |
|
|
xerrno = errno; |
|
|
continue; |
|
|
} |
|
|
|
|
|
/* Kernel bugs (on Ultrix at least) cause lossage (not just EINTR) |
|
|
when connect is interrupted. So let's not let it get interrupted. |
|
|
Note we do not turn off polling, because polling is only used |
|
|
when not interrupt_input, and thus not normally used on the systems |
|
|
which have this bug. On systems which use polling, there's no way |
|
|
to quit if polling is turned off. */ |
|
|
if (interrupt_input) |
|
|
unrequest_sigio (); |
|
|
|
|
|
/* Make us close S if quit. */ |
|
|
count1 = specpdl_ptr - specpdl; |
|
|
record_unwind_protect (close_file_unwind, make_number (s)); |
|
|
|
|
|
loop: |
|
|
|
|
|
immediate_quit = 1; |
|
|
QUIT; |
|
|
|
|
|
/* This turns off all alarm-based interrupts; the |
|
|
bind_polling_period call above doesn't always turn all the |
|
|
short-interval ones off, especially if interrupt_input is |
|
|
set. |
|
|
|
|
|
It'd be nice to be able to control the connect timeout |
|
|
though. Would non-blocking connect calls be portable? */ |
|
|
turn_on_atimers (0); |
|
|
ret = connect (s, lres->ai_addr, lres->ai_addrlen); |
|
|
xerrno = errno; |
|
|
turn_on_atimers (1); |
|
|
|
|
|
if (ret == 0 || xerrno == EISCONN) |
|
|
/* The unwind-protect will be discarded afterwards. |
|
|
Likewise for immediate_quit. */ |
|
|
break; |
|
|
|
|
|
immediate_quit = 0; |
|
|
|
|
|
if (xerrno == EINTR) |
|
|
goto loop; |
|
|
if (xerrno == EADDRINUSE && retry < 20) |
|
|
{ |
|
|
/* A delay here is needed on some FreeBSD systems, |
|
|
and it is harmless, since this retrying takes time anyway |
|
|
and should be infrequent. */ |
|
|
Fsleep_for (make_number (1), Qnil); |
|
|
retry++; |
|
|
goto loop; |
|
|
} |
|
|
|
|
|
/* Discard the unwind protect closing S. */ |
|
|
specpdl_ptr = specpdl + count1; |
|
|
count1 = specpdl_ptr - specpdl; |
|
|
|
|
|
emacs_close (s); |
|
|
s = -1; |
|
|
} |
|
|
|
|
|
freeaddrinfo (res); |
|
|
if (s < 0) |
|
|
{ |
|
|
if (interrupt_input) |
|
|
request_sigio (); |
|
|
|
|
|
errno = xerrno; |
|
|
report_file_error ("connection failed", |
|
|
Fcons (host, Fcons (name, Qnil))); |
|
|
} |
|
|
|
|
1919 |
#else /* not HAVE_GETADDRINFO */ |
#else /* not HAVE_GETADDRINFO */ |
1920 |
|
|
1921 |
while (1) |
while (1) |
1967 |
address.sin_family = host_info_ptr->h_addrtype; |
address.sin_family = host_info_ptr->h_addrtype; |
1968 |
address.sin_port = port; |
address.sin_port = port; |
1969 |
|
|
1970 |
s = socket (host_info_ptr->h_addrtype, SOCK_STREAM, 0); |
/* Emulate HAVE_GETADDRINFO for the loop over `res' below. */ |
1971 |
if (s < 0) |
ai.ai_family = host_info_ptr->h_addrtype; |
1972 |
report_file_error ("error creating socket", Fcons (name, Qnil)); |
ai.ai_socktype = SOCK_STREAM; |
1973 |
|
ai.ai_protocol = 0; |
1974 |
|
ai.ai_addr = (struct sockaddr *) &address; |
1975 |
|
ai.ai_addrlen = sizeof address; |
1976 |
|
ai.ai_next = NULL; |
1977 |
|
res = &ai; |
1978 |
|
#endif /* not HAVE_GETADDRINFO */ |
1979 |
|
|
1980 |
|
/* Do this in case we never enter the for-loop below. */ |
1981 |
count1 = specpdl_ptr - specpdl; |
count1 = specpdl_ptr - specpdl; |
1982 |
record_unwind_protect (close_file_unwind, make_number (s)); |
s = -1; |
1983 |
|
|
1984 |
/* Kernel bugs (on Ultrix at least) cause lossage (not just EINTR) |
for (lres = res; lres; lres = lres->ai_next) |
1985 |
when connect is interrupted. So let's not let it get interrupted. |
{ |
1986 |
Note we do not turn off polling, because polling is only used |
s = socket (lres->ai_family, lres->ai_socktype, lres->ai_protocol); |
1987 |
when not interrupt_input, and thus not normally used on the systems |
if (s < 0) |
1988 |
which have this bug. On systems which use polling, there's no way |
{ |
1989 |
to quit if polling is turned off. */ |
xerrno = errno; |
1990 |
if (interrupt_input) |
continue; |
1991 |
unrequest_sigio (); |
} |
1992 |
|
|
1993 |
|
#ifdef NON_BLOCKING_CONNECT |
1994 |
|
if (is_non_blocking) |
1995 |
|
{ |
1996 |
|
#ifdef O_NONBLOCK |
1997 |
|
ret = fcntl (s, F_SETFL, O_NONBLOCK); |
1998 |
|
#else |
1999 |
|
ret = fcntl (s, F_SETFL, O_NDELAY); |
2000 |
|
#endif |
2001 |
|
if (ret < 0) |
2002 |
|
{ |
2003 |
|
xerrno = errno; |
2004 |
|
emacs_close (s); |
2005 |
|
s = -1; |
2006 |
|
continue; |
2007 |
|
} |
2008 |
|
} |
2009 |
|
#endif |
2010 |
|
|
2011 |
loop: |
/* Kernel bugs (on Ultrix at least) cause lossage (not just EINTR) |
2012 |
|
when connect is interrupted. So let's not let it get interrupted. |
2013 |
|
Note we do not turn off polling, because polling is only used |
2014 |
|
when not interrupt_input, and thus not normally used on the systems |
2015 |
|
which have this bug. On systems which use polling, there's no way |
2016 |
|
to quit if polling is turned off. */ |
2017 |
|
if (interrupt_input) |
2018 |
|
unrequest_sigio (); |
2019 |
|
|
2020 |
immediate_quit = 1; |
/* Make us close S if quit. */ |
2021 |
QUIT; |
count1 = specpdl_ptr - specpdl; |
2022 |
|
record_unwind_protect (close_file_unwind, make_number (s)); |
2023 |
|
|
2024 |
if (connect (s, (struct sockaddr *) &address, sizeof address) == -1 |
loop: |
2025 |
&& errno != EISCONN) |
|
2026 |
{ |
immediate_quit = 1; |
2027 |
int xerrno = errno; |
QUIT; |
2028 |
|
|
2029 |
|
/* This turns off all alarm-based interrupts; the |
2030 |
|
bind_polling_period call above doesn't always turn all the |
2031 |
|
short-interval ones off, especially if interrupt_input is |
2032 |
|
set. |
2033 |
|
|
2034 |
|
It'd be nice to be able to control the connect timeout |
2035 |
|
though. Would non-blocking connect calls be portable? |
2036 |
|
|
2037 |
|
This used to be conditioned by HAVE_GETADDRINFO. Why? */ |
2038 |
|
|
2039 |
|
if (!is_non_blocking) |
2040 |
|
turn_on_atimers (0); |
2041 |
|
|
2042 |
|
ret = connect (s, lres->ai_addr, lres->ai_addrlen); |
2043 |
|
xerrno = errno; |
2044 |
|
|
2045 |
|
if (!is_non_blocking) |
2046 |
|
turn_on_atimers (1); |
2047 |
|
|
2048 |
|
if (ret == 0 || xerrno == EISCONN) |
2049 |
|
{ |
2050 |
|
is_non_blocking = 0; |
2051 |
|
/* The unwind-protect will be discarded afterwards. |
2052 |
|
Likewise for immediate_quit. */ |
2053 |
|
break; |
2054 |
|
} |
2055 |
|
|
2056 |
|
#ifdef NON_BLOCKING_CONNECT |
2057 |
|
#ifdef EINPROGRESS |
2058 |
|
if (is_non_blocking && xerrno == EINPROGRESS) |
2059 |
|
break; |
2060 |
|
#else |
2061 |
|
#ifdef EWOULDBLOCK |
2062 |
|
if (is_non_blocking && xerrno == EWOULDBLOCK) |
2063 |
|
break; |
2064 |
|
#endif |
2065 |
|
#endif |
2066 |
|
#endif |
2067 |
|
|
2068 |
immediate_quit = 0; |
immediate_quit = 0; |
2069 |
|
|
2070 |
if (errno == EINTR) |
if (xerrno == EINTR) |
2071 |
goto loop; |
goto loop; |
2072 |
if (errno == EADDRINUSE && retry < 20) |
if (xerrno == EADDRINUSE && retry < 20) |
2073 |
{ |
{ |
2074 |
/* A delay here is needed on some FreeBSD systems, |
/* A delay here is needed on some FreeBSD systems, |
2075 |
and it is harmless, since this retrying takes time anyway |
and it is harmless, since this retrying takes time anyway |
2079 |
goto loop; |
goto loop; |
2080 |
} |
} |
2081 |
|
|
2082 |
/* Discard the unwind protect. */ |
/* Discard the unwind protect closing S. */ |
2083 |
specpdl_ptr = specpdl + count1; |
specpdl_ptr = specpdl + count1; |
2084 |
|
count1 = specpdl_ptr - specpdl; |
2085 |
|
|
2086 |
emacs_close (s); |
emacs_close (s); |
2087 |
|
s = -1; |
2088 |
|
} |
2089 |
|
|
2090 |
|
#ifdef HAVE_GETADDRINFO |
2091 |
|
freeaddrinfo (res); |
2092 |
|
#endif |
2093 |
|
|
2094 |
|
if (s < 0) |
2095 |
|
{ |
2096 |
if (interrupt_input) |
if (interrupt_input) |
2097 |
request_sigio (); |
request_sigio (); |
2098 |
|
|
2099 |
|
/* If non-blocking got this far - and failed - assume non-blocking is |
2100 |
|
not supported after all. This is probably a wrong assumption, but |
2101 |
|
the normal blocking calls to open-network-stream handles this error |
2102 |
|
better. */ |
2103 |
|
if (is_non_blocking) |
2104 |
|
{ |
2105 |
|
#ifdef POLL_FOR_INPUT |
2106 |
|
unbind_to (count, Qnil); |
2107 |
|
#endif |
2108 |
|
return Qnil; |
2109 |
|
} |
2110 |
|
|
2111 |
errno = xerrno; |
errno = xerrno; |
2112 |
report_file_error ("connection failed", |
report_file_error ("connection failed", |
2113 |
Fcons (host, Fcons (name, Qnil))); |
Fcons (host, Fcons (name, Qnil))); |
2114 |
} |
} |
2115 |
|
|
|
#endif /* not HAVE_GETADDRINFO */ |
|
|
|
|
2116 |
immediate_quit = 0; |
immediate_quit = 0; |
2117 |
|
|
2118 |
/* Discard the unwind protect, if any. */ |
/* Discard the unwind protect, if any. */ |
2153 |
XPROCESS (proc)->childp = Fcons (host, Fcons (service, Qnil)); |
XPROCESS (proc)->childp = Fcons (host, Fcons (service, Qnil)); |
2154 |
XPROCESS (proc)->command_channel_p = Qnil; |
XPROCESS (proc)->command_channel_p = Qnil; |
2155 |
XPROCESS (proc)->buffer = buffer; |
XPROCESS (proc)->buffer = buffer; |
2156 |
XPROCESS (proc)->sentinel = Qnil; |
XPROCESS (proc)->sentinel = XCAR (sentinel); |
2157 |
XPROCESS (proc)->filter = Qnil; |
XPROCESS (proc)->filter = XCDR (sentinel); |
2158 |
XPROCESS (proc)->command = Qnil; |
XPROCESS (proc)->command = Qnil; |
2159 |
XPROCESS (proc)->pid = Qnil; |
XPROCESS (proc)->pid = Qnil; |
2160 |
XSETINT (XPROCESS (proc)->infd, inch); |
XSETINT (XPROCESS (proc)->infd, inch); |
2161 |
XSETINT (XPROCESS (proc)->outfd, outch); |
XSETINT (XPROCESS (proc)->outfd, outch); |
2162 |
XPROCESS (proc)->status = Qrun; |
XPROCESS (proc)->status = Qrun; |
2163 |
FD_SET (inch, &input_wait_mask); |
|
2164 |
FD_SET (inch, &non_keyboard_wait_mask); |
#ifdef NON_BLOCKING_CONNECT |
2165 |
|
if (!NILP (non_blocking)) |
2166 |
|
{ |
2167 |
|
/* We may get here if connect did succeed immediately. However, |
2168 |
|
in that case, we still need to signal this like a non-blocking |
2169 |
|
connection. */ |
2170 |
|
XPROCESS (proc)->status = Qconnect; |
2171 |
|
if (!FD_ISSET (inch, &connect_wait_mask)) |
2172 |
|
{ |
2173 |
|
FD_SET (inch, &connect_wait_mask); |
2174 |
|
num_pending_connects++; |
2175 |
|
} |
2176 |
|
} |
2177 |
|
else |
2178 |
|
#endif |
2179 |
|
if (!EQ (XPROCESS (proc)->filter, Qt)) |
2180 |
|
{ |
2181 |
|
FD_SET (inch, &input_wait_mask); |
2182 |
|
FD_SET (inch, &non_keyboard_wait_mask); |
2183 |
|
} |
2184 |
|
|
2185 |
if (inch > max_process_desc) |
if (inch > max_process_desc) |
2186 |
max_process_desc = inch; |
max_process_desc = inch; |
2187 |
|
|
2299 |
chan_process[inchannel] = Qnil; |
chan_process[inchannel] = Qnil; |
2300 |
FD_CLR (inchannel, &input_wait_mask); |
FD_CLR (inchannel, &input_wait_mask); |
2301 |
FD_CLR (inchannel, &non_keyboard_wait_mask); |
FD_CLR (inchannel, &non_keyboard_wait_mask); |
2302 |
|
if (FD_ISSET (inchannel, &connect_wait_mask)) |
2303 |
|
{ |
2304 |
|
FD_CLR (inchannel, &connect_wait_mask); |
2305 |
|
if (--num_pending_connects < 0) |
2306 |
|
abort (); |
2307 |
|
} |
2308 |
if (inchannel == max_process_desc) |
if (inchannel == max_process_desc) |
2309 |
{ |
{ |
2310 |
int i; |
int i; |
2469 |
{ |
{ |
2470 |
register int channel, nfds; |
register int channel, nfds; |
2471 |
static SELECT_TYPE Available; |
static SELECT_TYPE Available; |
2472 |
|
static SELECT_TYPE Connecting; |
2473 |
|
int check_connect, no_avail; |
2474 |
int xerrno; |
int xerrno; |
2475 |
Lisp_Object proc; |
Lisp_Object proc; |
2476 |
EMACS_TIME timeout, end_time; |
EMACS_TIME timeout, end_time; |
|
SELECT_TYPE Atemp; |
|
2477 |
int wait_channel = -1; |
int wait_channel = -1; |
2478 |
struct Lisp_Process *wait_proc = 0; |
struct Lisp_Process *wait_proc = 0; |
2479 |
int got_some_input = 0; |
int got_some_input = 0; |
2482 |
Lisp_Object wait_for_cell = Qnil; |
Lisp_Object wait_for_cell = Qnil; |
2483 |
|
|
2484 |
FD_ZERO (&Available); |
FD_ZERO (&Available); |
2485 |
|
FD_ZERO (&Connecting); |
2486 |
|
|
2487 |
/* If read_kbd is a process to watch, set wait_proc and wait_channel |
/* If read_kbd is a process to watch, set wait_proc and wait_channel |
2488 |
accordingly. */ |
accordingly. */ |
2624 |
timeout to get our attention. */ |
timeout to get our attention. */ |
2625 |
if (update_tick != process_tick && do_display) |
if (update_tick != process_tick && do_display) |
2626 |
{ |
{ |
2627 |
|
SELECT_TYPE Atemp, Ctemp; |
2628 |
|
|
2629 |
Atemp = input_wait_mask; |
Atemp = input_wait_mask; |
2630 |
|
Ctemp = connect_wait_mask; |
2631 |
EMACS_SET_SECS_USECS (timeout, 0, 0); |
EMACS_SET_SECS_USECS (timeout, 0, 0); |
2632 |
if ((select (max (max_process_desc, max_keyboard_desc) + 1, |
if ((select (max (max_process_desc, max_keyboard_desc) + 1, |
2633 |
&Atemp, (SELECT_TYPE *)0, (SELECT_TYPE *)0, |
&Atemp, |
2634 |
&timeout) |
(num_pending_connects > 0 ? &Ctemp : (SELECT_TYPE *)0), |
2635 |
|
(SELECT_TYPE *)0, &timeout) |
2636 |
<= 0)) |
<= 0)) |
2637 |
{ |
{ |
2638 |
/* It's okay for us to do this and then continue with |
/* It's okay for us to do this and then continue with |
2642 |
} |
} |
2643 |
} |
} |
2644 |
|
|
2645 |
/* Don't wait for output from a non-running process. */ |
/* Don't wait for output from a non-running process. Just |
2646 |
|
read whatever data has already been received. */ |
2647 |
if (wait_proc != 0 && !NILP (wait_proc->raw_status_low)) |
if (wait_proc != 0 && !NILP (wait_proc->raw_status_low)) |
2648 |
update_status (wait_proc); |
update_status (wait_proc); |
2649 |
if (wait_proc != 0 |
if (wait_proc != 0 |
2650 |
&& ! EQ (wait_proc->status, Qrun)) |
&& ! EQ (wait_proc->status, Qrun) |
2651 |
|
&& ! EQ (wait_proc->status, Qconnect)) |
2652 |
{ |
{ |
2653 |
int nread, total_nread = 0; |
int nread, total_nread = 0; |
2654 |
|
|
2687 |
/* Wait till there is something to do */ |
/* Wait till there is something to do */ |
2688 |
|
|
2689 |
if (!NILP (wait_for_cell)) |
if (!NILP (wait_for_cell)) |
2690 |
Available = non_process_wait_mask; |
{ |
2691 |
else if (! XINT (read_kbd)) |
Available = non_process_wait_mask; |
2692 |
Available = non_keyboard_wait_mask; |
check_connect = 0; |
2693 |
|
} |
2694 |
else |
else |
2695 |
Available = input_wait_mask; |
{ |
2696 |
|
if (! XINT (read_kbd)) |
2697 |
|
Available = non_keyboard_wait_mask; |
2698 |
|
else |
2699 |
|
Available = input_wait_mask; |
2700 |
|
check_connect = (num_pending_connects > 0); |
2701 |
|
} |
2702 |
|
|
2703 |
/* If frame size has changed or the window is newly mapped, |
/* If frame size has changed or the window is newly mapped, |
2704 |
redisplay now, before we start to wait. There is a race |
redisplay now, before we start to wait. There is a race |
2713 |
set_waiting_for_input (&timeout); |
set_waiting_for_input (&timeout); |
2714 |
} |
} |
2715 |
|
|
2716 |
|
no_avail = 0; |
2717 |
if (XINT (read_kbd) && detect_input_pending ()) |
if (XINT (read_kbd) && detect_input_pending ()) |
2718 |
{ |
{ |
2719 |
nfds = 0; |
nfds = 0; |
2720 |
FD_ZERO (&Available); |
no_avail = 1; |
2721 |
} |
} |
2722 |
else |
else |
2723 |
nfds = select (max (max_process_desc, max_keyboard_desc) + 1, |
{ |
2724 |
&Available, (SELECT_TYPE *)0, (SELECT_TYPE *)0, |
if (check_connect) |
2725 |
&timeout); |
Connecting = connect_wait_mask; |
2726 |
|
nfds = select (max (max_process_desc, max_keyboard_desc) + 1, |
2727 |
|
&Available, |
2728 |
|
(check_connect ? &Connecting : (SELECT_TYPE *)0), |
2729 |
|
(SELECT_TYPE *)0, &timeout); |
2730 |
|
} |
2731 |
|
|
2732 |
xerrno = errno; |
xerrno = errno; |
2733 |
|
|
2743 |
if (nfds < 0) |
if (nfds < 0) |
2744 |
{ |
{ |
2745 |
if (xerrno == EINTR) |
if (xerrno == EINTR) |
2746 |
FD_ZERO (&Available); |
no_avail = 1; |
2747 |
#ifdef ultrix |
#ifdef ultrix |
2748 |
/* Ultrix select seems to return ENOMEM when it is |
/* Ultrix select seems to return ENOMEM when it is |
2749 |
interrupted. Treat it just like EINTR. Bleah. Note |
interrupted. Treat it just like EINTR. Bleah. Note |
2751 |
"__ultrix__"; the latter is only defined under GCC, but |
"__ultrix__"; the latter is only defined under GCC, but |
2752 |
not by DEC's bundled CC. -JimB */ |
not by DEC's bundled CC. -JimB */ |
2753 |
else if (xerrno == ENOMEM) |
else if (xerrno == ENOMEM) |
2754 |
FD_ZERO (&Available); |
no_avail = 1; |
2755 |
#endif |
#endif |
2756 |
#ifdef ALLIANT |
#ifdef ALLIANT |
2757 |
/* This happens for no known reason on ALLIANT. |
/* This happens for no known reason on ALLIANT. |
2758 |
I am guessing that this is the right response. -- RMS. */ |
I am guessing that this is the right response. -- RMS. */ |
2759 |
else if (xerrno == EFAULT) |
else if (xerrno == EFAULT) |
2760 |
FD_ZERO (&Available); |
no_avail = 1; |
2761 |
#endif |
#endif |
2762 |
else if (xerrno == EBADF) |
else if (xerrno == EBADF) |
2763 |
{ |
{ |
2769 |
So, SIGHUP is ignored (see def of PTY_TTY_NAME_SPRINTF |
So, SIGHUP is ignored (see def of PTY_TTY_NAME_SPRINTF |
2770 |
in m/ibmrt-aix.h), and here we just ignore the select error. |
in m/ibmrt-aix.h), and here we just ignore the select error. |
2771 |
Cleanup occurs c/o status_notify after SIGCLD. */ |
Cleanup occurs c/o status_notify after SIGCLD. */ |
2772 |
FD_ZERO (&Available); /* Cannot depend on values returned */ |
no_avail = 1; /* Cannot depend on values returned */ |
2773 |
#else |
#else |
2774 |
abort (); |
abort (); |
2775 |
#endif |
#endif |
2777 |
else |
else |
2778 |
error ("select error: %s", emacs_strerror (xerrno)); |
error ("select error: %s", emacs_strerror (xerrno)); |
2779 |
} |
} |
2780 |
|
|
2781 |
|
if (no_avail) |
2782 |
|
{ |
2783 |
|
FD_ZERO (&Available); |
2784 |
|
check_connect = 0; |
2785 |
|
} |
2786 |
|
|
2787 |
#if defined(sun) && !defined(USG5_4) |
#if defined(sun) && !defined(USG5_4) |
2788 |
else if (nfds > 0 && keyboard_bit_set (&Available) |
if (nfds > 0 && keyboard_bit_set (&Available) |
2789 |
&& interrupt_input) |
&& interrupt_input) |
2790 |
/* System sometimes fails to deliver SIGIO. |
/* System sometimes fails to deliver SIGIO. |
2791 |
|
|
2792 |
David J. Mackenzie says that Emacs doesn't compile under |
David J. Mackenzie says that Emacs doesn't compile under |
2885 |
do_pending_window_change (0); |
do_pending_window_change (0); |
2886 |
|
|
2887 |
/* Check for data from a process. */ |
/* Check for data from a process. */ |
2888 |
|
if (no_avail || nfds == 0) |
2889 |
|
continue; |
2890 |
|
|
2891 |
/* Really FIRST_PROC_DESC should be 0 on Unix, |
/* Really FIRST_PROC_DESC should be 0 on Unix, |
2892 |
but this is safer in the short run. */ |
but this is safer in the short run. */ |
2893 |
for (channel = 0; channel <= max_process_desc; channel++) |
for (channel = 0; channel <= max_process_desc; channel++) |
2979 |
= Fcons (Qexit, Fcons (make_number (256), Qnil)); |
= Fcons (Qexit, Fcons (make_number (256), Qnil)); |
2980 |
} |
} |
2981 |
} |
} |
2982 |
|
#ifdef NON_BLOCKING_CONNECT |
2983 |
|
if (check_connect && FD_ISSET (channel, &Connecting)) |
2984 |
|
{ |
2985 |
|
struct Lisp_Process *p; |
2986 |
|
struct sockaddr pname; |
2987 |
|
socklen_t pnamelen = sizeof(pname); |
2988 |
|
|
2989 |
|
FD_CLR (channel, &connect_wait_mask); |
2990 |
|
if (--num_pending_connects < 0) |
2991 |
|
abort (); |
2992 |
|
|
2993 |
|
proc = chan_process[channel]; |
2994 |
|
if (NILP (proc)) |
2995 |
|
continue; |
2996 |
|
|
2997 |
|
p = XPROCESS (proc); |
2998 |
|
|
2999 |
|
#ifdef GNU_LINUX |
3000 |
|
/* getsockopt(,,SO_ERROR,,) is said to hang on some systems. |
3001 |
|
So only use it on systems where it is known to work. */ |
3002 |
|
{ |
3003 |
|
socklen_t xlen = sizeof(xerrno); |
3004 |
|
if (getsockopt(channel, SOL_SOCKET, SO_ERROR, &xerrno, &xlen)) |
3005 |
|
xerrno = errno; |
3006 |
|
} |
3007 |
|
#else |
3008 |
|
/* If connection failed, getpeername will fail. */ |
3009 |
|
xerrno = 0; |
3010 |
|
if (getpeername(channel, &pname, &pnamelen) < 0) |
3011 |
|
{ |
3012 |
|
/* Obtain connect failure code through error slippage. */ |
3013 |
|
char dummy; |
3014 |
|
xerrno = errno; |
3015 |
|
if (errno == ENOTCONN && read(channel, &dummy, 1) < 0) |
3016 |
|
xerrno = errno; |
3017 |
|
} |
3018 |
|
#endif |
3019 |
|
if (xerrno) |
3020 |
|
{ |
3021 |
|
XSETINT (p->tick, ++process_tick); |
3022 |
|
p->status = Fcons (Qfailed, Fcons (make_number (xerrno), Qnil)); |
3023 |
|
deactivate_process (proc); |
3024 |
|
} |
3025 |
|
else |
3026 |
|
{ |
3027 |
|
p->status = Qrun; |
3028 |
|
/* Execute the sentinel here. If we had relied on |
3029 |
|
status_notify to do it later, it will read input |
3030 |
|
from the process before calling the sentinel. */ |
3031 |
|
exec_sentinel (proc, build_string ("open\n")); |
3032 |
|
if (!EQ (p->filter, Qt)) |
3033 |
|
{ |
3034 |
|
FD_SET (XINT (p->infd), &input_wait_mask); |
3035 |
|
FD_SET (XINT (p->infd), &non_keyboard_wait_mask); |
3036 |
|
} |
3037 |
|
} |
3038 |
|
} |
3039 |
|
#endif /* NON_BLOCKING_CONNECT */ |
3040 |
} /* end for each file descriptor */ |
} /* end for each file descriptor */ |
3041 |
} /* end while exit conditions not met */ |
} /* end while exit conditions not met */ |
3042 |
|
|
4619 |
|
|
4620 |
/* If process is still active, read any output that remains. */ |
/* If process is still active, read any output that remains. */ |
4621 |
while (! EQ (p->filter, Qt) |
while (! EQ (p->filter, Qt) |
4622 |
|
&& ! EQ (p->status, Qconnect) |
4623 |
&& XINT (p->infd) >= 0 |
&& XINT (p->infd) >= 0 |
4624 |
&& read_process_output (proc, XINT (p->infd)) > 0); |
&& read_process_output (proc, XINT (p->infd)) > 0); |
4625 |
|
|
4854 |
staticpro (&Qopen); |
staticpro (&Qopen); |
4855 |
Qclosed = intern ("closed"); |
Qclosed = intern ("closed"); |
4856 |
staticpro (&Qclosed); |
staticpro (&Qclosed); |
4857 |
|
Qconnect = intern ("connect"); |
4858 |
|
staticpro (&Qconnect); |
4859 |
|
Qfailed = intern ("failed"); |
4860 |
|
staticpro (&Qfailed); |
4861 |
|
|
4862 |
Qlast_nonmenu_event = intern ("last-nonmenu-event"); |
Qlast_nonmenu_event = intern ("last-nonmenu-event"); |
4863 |
staticpro (&Qlast_nonmenu_event); |
staticpro (&Qlast_nonmenu_event); |