Add missing declaration for make_bigz
[sxemacs] / src / process-unix.c
1 /* Asynchronous subprocess implementation for UNIX
2    Copyright (C) 1985, 1986, 1987, 1988, 1992, 1993, 1994, 1995
3    Free Software Foundation, Inc.
4    Copyright (C) 1995 Sun Microsystems, Inc.
5    Copyright (C) 1995, 1996 Ben Wing.
6
7 This file is part of SXEmacs
8
9 SXEmacs is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13
14 SXEmacs is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program.  If not, see <http://www.gnu.org/licenses/>. */
21
22
23 /* This file has been Mule-ized except for `start-process-internal',
24    `open-network-stream-internal' and `open-multicast-group-internal'. */
25
26 /* This file has been split into process.c and process-unix.c by
27    Kirill M. Katsnelson <kkm@kis.ru>, so please bash him and not
28    the original author(s) */
29
30 /* The IPv6 support is derived from the code for GNU Emacs-20.3
31    written by Wolfgang S. Rupprecht */
32
33 #include <config.h>
34
35 #if !defined (NO_SUBPROCESSES)
36
37 /* The entire file is within this conditional */
38
39 #include "lisp.h"
40
41 #include "buffer.h"
42 #include "events/events.h"
43 #include "ui/frame.h"
44 #include "hash.h"
45 #include "lstream.h"
46 #include "opaque.h"
47 #include "process.h"
48 #include "procimpl.h"
49 #include "sysdep.h"
50 #include "ui/window.h"
51 #ifdef FILE_CODING
52 #include "mule/file-coding.h"
53 #endif
54
55 #include <setjmp.h>
56 #include "sysfile.h"
57 #include "sysproc.h"
58 #include "systime.h"
59 #include "syssignal.h"          /* Always include before systty.h */
60 #include "ui/TTY/systty.h"
61 #include "syswait.h"
62
63 #ifdef HPUX
64 #include <grp.h>                /* See grantpt fixups for HPUX below. */
65 #endif
66
67 #if defined(HAVE_OPENSSL) && defined(OPENSSL_SSL)
68 #include "openssl.h"
69 #endif
70
71
72 /*
73  * Implementation-specific data. Pointed to by Lisp_Process->process_data
74  */
75
76 struct unix_process_data {
77         /* Always 0.  Used to be for tooltalk only. */
78         int connected_via_filedesc_p;
79         /* Descriptor by which we read from this process.  -1 for dead process */
80         int infd;
81         /* Descriptor for the tty which this process is using.
82            -1 if we didn't record it (on some systems, there's no need).  */
83         int subtty;
84         /* Name of subprocess terminal. */
85         Lisp_Object tty_name;
86         /* Non-false if communicating through a pty.  */
87         char pty_flag;
88 };
89
90
91 extern struct hash_table *usid_to_process;
92
93
94 #define UNIX_DATA(p) ((struct unix_process_data*)((p)->process_data))
95 \f
96 /**********************************************************************/
97 /*                    Static helper routines                          */
98 /**********************************************************************/
99
100 static SIGTYPE close_safely_handler(int signo)
101 {
102         EMACS_REESTABLISH_SIGNAL(signo, close_safely_handler);
103         SIGRETURN;
104 }
105
106 static void close_safely(int fd)
107 {
108         stop_interrupts();
109         signal(SIGALRM, close_safely_handler);
110         alarm(1);
111         close(fd);
112         alarm(0);
113         start_interrupts();
114 }
115
116 static void close_descriptor_pair(int in, int out)
117 {
118         if (in >= 0)
119                 close(in);
120         if (out != in && out >= 0)
121                 close(out);
122 }
123
124 /* Close all descriptors currently in use for communication
125    with subprocess.  This is used in a newly-forked subprocess
126    to get rid of irrelevant descriptors.  */
127
128 static int
129 close_process_descs_mapfun(const void *key, void *contents, void *arg)
130 {
131         Lisp_Object proc;
132         CVOID_TO_LISP(proc, contents);
133         event_stream_delete_stream_pair(XPROCESS(proc)->pipe_instream,
134                                         XPROCESS(proc)->pipe_outstream);
135         return 0;
136 }
137
138 /* #### This function is currently called from child_setup
139    in callproc.c. It should become static though - kkm */
140 void close_process_descs(void)
141 {
142         maphash(close_process_descs_mapfun, usid_to_process, 0);
143 }
144
145 /* connect to an existing file descriptor.  This is very similar to
146    open-network-stream except that it assumes that the connection has
147    already been initialized.  It is currently used for ToolTalk
148    communication. */
149
150 /* This function used to be visible on the Lisp level, but there is no
151    real point in doing that.  Here is the doc string:
152
153   "Connect to an existing file descriptor.
154 Return a subprocess-object to represent the connection.
155 Input and output work as for subprocesses; `delete-process' closes it.
156 Args are NAME BUFFER INFD OUTFD.
157 NAME is name for process.  It is modified if necessary to make it unique.
158 BUFFER is the buffer (or buffer-name) to associate with the process.
159  Process output goes at end of that buffer, unless you specify
160  an output stream or filter function to handle the output.
161  BUFFER may also be nil, meaning that this process is not associated
162  with any buffer.
163 INFD and OUTFD specify the file descriptors to use for input and
164  output, respectively."
165 */
166
167 Lisp_Object
168 connect_to_file_descriptor(Lisp_Object name, Lisp_Object buffer,
169                            Lisp_Object infd, Lisp_Object outfd)
170 {
171         /* This function can GC */
172         Lisp_Object proc;
173         EMACS_INT inch, outch;
174
175         CHECK_STRING(name);
176         CHECK_INT(infd);
177         CHECK_INT(outfd);
178
179         inch = XINT(infd);
180         outch = XINT(outfd);
181         if (get_process_from_usid(FD_TO_USID(inch)))
182                 invalid_operation("There is already a process connected to fd",
183                                   infd);
184         if (!NILP(buffer))
185                 buffer = Fget_buffer_create(buffer);
186         proc = make_process_internal(name);
187
188         XPROCESS(proc)->pid = Fcons(infd, name);
189         XPROCESS(proc)->buffer = buffer;
190         init_process_io_handles(XPROCESS(proc), (void*)inch, (void*)outch, 0);
191         UNIX_DATA(XPROCESS(proc))->connected_via_filedesc_p = 1;
192
193         event_stream_select_process(XPROCESS(proc));
194
195         return proc;
196 }
197
198 #ifdef HAVE_PTYS
199 static int allocate_pty_the_old_fashioned_way(void);
200
201 /* The file name of the (slave) pty opened by allocate_pty().  */
202 #ifndef MAX_PTYNAME_LEN
203 #define MAX_PTYNAME_LEN 64
204 #endif
205 static char pty_name[MAX_PTYNAME_LEN];
206
207 /* Open an available pty, returning a file descriptor.
208    Return -1 on failure.
209    The file name of the terminal corresponding to the pty
210    is left in the variable `pty_name'.  */
211
212 static int allocate_pty(void)
213 {
214         /* Unix98 standardized grantpt, unlockpt, and ptsname, but not the
215            functions required to open a master pty in the first place :-(
216
217            Modern Unix systems all seems to have convenience methods to open
218            a master pty fd in one function call, but there is little
219            agreement on how to do it.
220
221            allocate_pty() tries all the different known easy ways of opening
222            a pty.  In case of failure, we resort to the old BSD-style pty
223            grovelling code in allocate_pty_the_old_fashioned_way(). */
224 #ifndef FORCE_ALLOCATE_PTY_THE_OLD_FASHIONED_WAY
225         int master_fd = -1;
226         const char *slave_name = NULL;
227         const char *_clone_ = NULL;
228         static const char *const clones[] = {
229                 /* Different pty master clone devices */
230                 "/dev/ptmx",    /* Various systems */
231                 "/dev/ptm/clone",       /* HPUX */
232                 "/dev/ptc",     /* AIX */
233                 "/dev/ptmx_bsd" /* Tru64 */
234         };
235
236 #ifdef HAVE_GETPT               /* glibc */
237         master_fd = getpt();
238         if (master_fd >= 0)
239                 goto have_master;
240 #endif                          /* HAVE_GETPT */
241
242 #if defined(HAVE_OPENPTY)       /* BSD, Tru64, glibc */
243         {
244                 int slave_fd = -1;
245                 int rc;
246                 EMACS_BLOCK_SIGNAL(SIGCHLD);
247                 rc = openpty(&master_fd, &slave_fd, NULL, NULL, NULL);
248                 EMACS_UNBLOCK_SIGNAL(SIGCHLD);
249                 if (rc == 0) {
250                         slave_name = ttyname(slave_fd);
251                         close(slave_fd);
252                         goto have_slave_name;
253                 } else {
254                         if (master_fd >= 0)
255                                 close(master_fd);
256                         if (slave_fd >= 0)
257                                 close(slave_fd);
258                 }
259         }
260 #endif                          /* HAVE_OPENPTY */
261
262 #if defined(HAVE__GETPTY) && defined (O_NDELAY) /* SGI */
263         master_fd = -1;
264         EMACS_BLOCK_SIGNAL(SIGCHLD);
265         slave_name = _getpty(&master_fd, O_RDWR | O_NDELAY, 0600, 0);
266         EMACS_UNBLOCK_SIGNAL(SIGCHLD);
267         if (master_fd >= 0 && slave_name != NULL)
268                 goto have_slave_name;
269 #endif                          /* HAVE__GETPTY */
270
271         /* Master clone devices are available on most systems */
272         {
273                 int i;
274                 for (i = 0; i < countof(clones); i++) {
275                         _clone_ = clones[i];
276                         master_fd =
277                             open(_clone_, O_RDWR | O_NONBLOCK | OPEN_BINARY, 0);
278                         if (master_fd >= 0)
279                                 goto have_master;
280                 }
281                 _clone_ = NULL;
282         }
283
284         goto lose;
285
286       have_master:
287
288 #if defined (HAVE_PTSNAME)
289         slave_name = ptsname(master_fd);
290         if (slave_name)
291                 goto have_slave_name;
292 #endif
293
294         /* AIX docs say to use ttyname, not ptsname, to get slave_name */
295         if (_clone_ && !strcmp(_clone_, "/dev/ptc")
296             && (slave_name = ttyname(master_fd)) != NULL)
297                 goto have_slave_name;
298
299         goto lose;
300
301       have_slave_name:
302         if( slave_name != NULL )
303                 strncpy(pty_name, slave_name, sizeof(pty_name));
304         else
305                 strncpy(pty_name, "<NULL ttyname>", sizeof(pty_name));
306         pty_name[sizeof(pty_name) - 1] = '\0';
307         setup_pty(master_fd);
308
309         /* We jump through some hoops to frob the pty.
310            It's not obvious that checking the return code here is useful. */
311
312         /* "The grantpt() function will fail if it is unable to successfully
313            invoke the setuid root program.  It may also fail if the
314            application has installed a signal handler to catch SIGCHLD
315            signals." */
316 #if defined (HAVE_GRANTPT) || defined (HAVE_UNLOCKPT)
317         EMACS_BLOCK_SIGNAL(SIGCHLD);
318
319 #if defined (HAVE_GRANTPT)
320         grantpt(master_fd);
321 #ifdef HPUX
322         /* grantpt() behavior on some versions of HP-UX differs from what's
323            specified in the man page: the group of the slave PTY is set to
324            the user's primary group, and we fix that. */
325         {
326                 struct group *tty_group = getgrnam("tty");
327                 if (tty_group != NULL)
328                         chown(pty_name, (uid_t) - 1, tty_group->gr_gid);
329         }
330 #endif                          /* HPUX has broken grantpt() */
331 #endif                          /* HAVE_GRANTPT */
332
333 #if defined (HAVE_UNLOCKPT)
334         unlockpt(master_fd);
335 #endif
336
337         EMACS_UNBLOCK_SIGNAL(SIGCHLD);
338 #endif                          /* HAVE_GRANTPT || HAVE_UNLOCKPT */
339
340         return master_fd;
341
342       lose:
343         if (master_fd >= 0)
344                 close(master_fd);
345 #endif                          /* ndef FORCE_ALLOCATE_PTY_THE_OLD_FASHIONED_WAY */
346         return allocate_pty_the_old_fashioned_way();
347 }
348
349 /* This function tries to allocate a pty by iterating through file
350    pairs with names like /dev/ptyp1 and /dev/ttyp1. */
351 static int allocate_pty_the_old_fashioned_way(void)
352 {
353         struct stat stb;
354
355         /* Some systems name their pseudoterminals so that there are gaps in
356            the usual sequence - for example, on HP9000/S700 systems, there
357            are no pseudoterminals with names ending in 'f'.  So we wait for
358            three failures in a row before deciding that we've reached the
359            end of the ptys.  */
360         int failed_count = 0;
361         int fd;
362         int i;
363         int c;
364
365 #ifdef PTY_ITERATION
366         PTY_ITERATION
367 #else
368 # ifndef FIRST_PTY_LETTER
369 # define FIRST_PTY_LETTER 'p'
370 # endif
371         for (c = FIRST_PTY_LETTER; c <= 'z'; c++)
372                 for (i = 0; i < 16; i++)
373 #endif                          /* PTY_ITERATION */
374
375                 {
376                         int sz;
377
378 #ifdef PTY_NAME_SPRINTF
379                         PTY_NAME_SPRINTF;
380 #else
381                         sz = snprintf(pty_name, sizeof(pty_name), "/dev/pty%c%x", c, i);
382                         assert(sz >= 0 && (size_t)sz < sizeof(pty_name));
383 #endif                          /* no PTY_NAME_SPRINTF */
384
385                         if (sxemacs_stat(pty_name, &stb) < 0) {
386                                 if (++failed_count >= 3)
387                                         return -1;
388                         } else
389                                 failed_count = 0;
390                         fd = open(pty_name, O_RDWR | O_NONBLOCK | OPEN_BINARY,
391                                   0);
392
393                         if (fd >= 0) {
394 #ifdef PTY_TTY_NAME_SPRINTF
395                                 PTY_TTY_NAME_SPRINTF;
396 #else
397                                 sz = snprintf(pty_name, sizeof(pty_name),
398                                                   "/dev/tty%c%x", c, i);
399                                 assert(sz >= 0 && (size_t)sz < sizeof(pty_name));
400 #endif                          /* no PTY_TTY_NAME_SPRINTF */
401                                 if (access(pty_name, R_OK | W_OK) == 0) {
402                                         setup_pty(fd);
403                                         return fd;
404                                 }
405                                 close(fd);
406                         }
407                 }               /* iteration */
408         return -1;
409 }
410 #endif                          /* HAVE_PTYS */
411
412 static int
413 create_bidirectional_pipe(long int *inchannel, long int *outchannel,
414                           volatile int *forkin, volatile int *forkout)
415 {
416         int sv[2];
417
418 #ifdef SKTPAIR
419         if (socketpair(AF_UNIX, SOCK_STREAM, 0, sv) < 0)
420                 return -1;
421         *outchannel = *inchannel = sv[0];
422         *forkout = *forkin = sv[1];
423 #else                           /* not SKTPAIR */
424         int temp;
425         temp = pipe(sv);
426         if (temp < 0)
427                 return -1;
428         *inchannel = sv[0];
429         *forkout = sv[1];
430         temp = pipe(sv);
431         if (temp < 0)
432                 return -1;
433         *outchannel = sv[1];
434         *forkin = sv[0];
435 #endif                          /* not SKTPAIR */
436         return 0;
437 }
438
439 #ifdef HAVE_SOCKETS
440
441 #if !(defined(HAVE_GETADDRINFO) && defined(HAVE_GETNAMEINFO))
442 static int
443 get_internet_address(Lisp_Object host, struct sockaddr_in *address,
444                      Error_behavior errb)
445 {
446         struct hostent *host_info_ptr = NULL;
447 #ifdef TRY_AGAIN
448         int count = 0;
449 #endif
450
451         xzero(*address);
452
453         while (1) {
454 #ifdef TRY_AGAIN
455                 if (count++ > 10)
456                         break;
457                 h_errno = 0;
458 #endif
459                 /* Some systems can't handle SIGIO/SIGALARM in gethostbyname. */
460                 slow_down_interrupts();
461                 host_info_ptr = gethostbyname((char *)XSTRING_DATA(host));
462                 speed_up_interrupts();
463 #ifdef TRY_AGAIN
464                 if (!(host_info_ptr == 0 && h_errno == TRY_AGAIN))
465 #endif
466                         break;
467                 Fsleep_for(make_int(1));
468         }
469         if (host_info_ptr) {
470                 address->sin_family = host_info_ptr->h_addrtype;
471                 memcpy(&address->sin_addr, host_info_ptr->h_addr,
472                        host_info_ptr->h_length);
473         } else {
474                 IN_ADDR numeric_addr;
475                 /* Attempt to interpret host as numeric inet address */
476                 numeric_addr = inet_addr((char *)XSTRING_DATA(host));
477                 if (NUMERIC_ADDR_ERROR) {
478                         maybe_error(Qprocess, errb,
479                                     "Unknown host \"%s\"", XSTRING_DATA(host));
480                         return 0;
481                 }
482
483                 /* There was some broken code here that called strlen() here
484                    on (char *) &numeric_addr and even sometimes accessed
485                    uninitialized data. */
486                 address->sin_family = AF_INET;
487                 *(IN_ADDR *) & address->sin_addr = numeric_addr;
488         }
489
490         return 1;
491 }
492 #endif                          /*  !(HAVE_GETADDRINFO && HAVE_GETNAMEINFO) */
493
494 static void set_socket_nonblocking_maybe(int fd, int port, const char *proto)
495 {
496 #ifdef PROCESS_IO_BLOCKING
497         Lisp_Object tail;
498
499         for (tail = network_stream_blocking_port_list; CONSP(tail);
500              tail = XCDR(tail)) {
501                 Lisp_Object tail_port = XCAR(tail);
502
503                 if (STRINGP(tail_port)) {
504                         struct servent *svc_info;
505                         CHECK_STRING(tail_port);
506                         svc_info =
507                             getservbyname((char *)XSTRING_DATA(tail_port),
508                                           proto);
509                         if ((svc_info != 0) && (svc_info->s_port == port))
510                                 break;
511                         else
512                                 continue;
513                 } else if (INTP(tail_port)
514                            && (htons((unsigned short)XINT(tail_port)) == port))
515                         break;
516         }
517
518         if (!CONSP(tail)) {
519                 set_descriptor_non_blocking(fd);
520         }
521 #else
522         set_descriptor_non_blocking(fd);
523 #endif                          /* PROCESS_IO_BLOCKING */
524 }
525
526 #endif                          /* HAVE_SOCKETS */
527
528 /* Compute the Lisp form of the process status from
529    the numeric status that was returned by `wait'.  */
530
531 static void update_status_from_wait_code(Lisp_Process * p, int *w_fmh)
532 {
533         /* C compiler lossage when attempting to pass w directly */
534         int w = *w_fmh;
535
536         if (WIFSTOPPED(w)) {
537                 p->status_symbol = Qstop;
538                 p->exit_code = WSTOPSIG(w);
539                 p->core_dumped = 0;
540         } else if (WIFEXITED(w)) {
541                 p->status_symbol = Qexit;
542                 p->exit_code = WEXITSTATUS(w);
543                 p->core_dumped = 0;
544         } else if (WIFSIGNALED(w)) {
545                 p->status_symbol = Qsignal;
546                 p->exit_code = WTERMSIG(w);
547                 p->core_dumped = WCOREDUMP(w);
548         } else {
549                 p->status_symbol = Qrun;
550                 p->exit_code = 0;
551         }
552 }
553
554 #ifdef SIGCHLD
555
556 #define MAX_EXITED_PROCESSES 1000
557 static volatile pid_t exited_processes[MAX_EXITED_PROCESSES];
558 static volatile int exited_processes_status[MAX_EXITED_PROCESSES];
559 static volatile int exited_processes_index;
560
561 static volatile int sigchld_happened;
562
563 /* On receipt of a signal that a child status has changed,
564  loop asking about children with changed statuses until
565  the system says there are no more.  All we do is record
566  the processes and wait status.
567
568  This function could be called from within the SIGCHLD
569  handler, so it must be completely reentrant.  When
570  not called from a SIGCHLD handler, BLOCK_SIGCHLD should
571  be non-zero so that SIGCHLD is blocked while this
572  function is running. (This is necessary so avoid
573  race conditions with the SIGCHLD_HAPPENED flag). */
574
575 static void record_exited_processes(int block_sigchld)
576 {
577         if (!sigchld_happened) {
578                 return;
579         }
580 #ifdef EMACS_BLOCK_SIGNAL
581         if (block_sigchld)
582                 EMACS_BLOCK_SIGNAL(SIGCHLD);
583 #endif
584
585         while (sigchld_happened) {
586                 int pid;
587                 int w;
588
589                 /* Keep trying to get a status until we get a definitive result.  */
590                 do {
591                         errno = 0;
592 #ifdef WNOHANG
593 #  ifndef WUNTRACED
594 #    define WUNTRACED 0
595 #  endif                        /* not WUNTRACED */
596 #  ifdef HAVE_WAITPID
597                         pid = waitpid((pid_t) - 1, &w, WNOHANG | WUNTRACED);
598 #  else
599                         pid = wait3(&w, WNOHANG | WUNTRACED, 0);
600 #  endif
601 #else                           /* not WNOHANG */
602                         pid = wait(&w);
603 #endif                          /* not WNOHANG */
604                 }
605                 while (pid <= 0 && errno == EINTR);
606
607                 if (pid <= 0)
608                         break;
609
610                 if (exited_processes_index < MAX_EXITED_PROCESSES) {
611                         exited_processes[exited_processes_index] = pid;
612                         exited_processes_status[exited_processes_index] = w;
613                         exited_processes_index++;
614                 }
615
616                 /* On systems with WNOHANG, we just ignore the number
617                    of times that SIGCHLD was signalled, and keep looping
618                    until there are no more processes to wait on.  If we
619                    don't have WNOHANG, we have to rely on the count in
620                    SIGCHLD_HAPPENED. */
621 #ifndef WNOHANG
622                 sigchld_happened--;
623 #endif                          /* not WNOHANG */
624         }
625
626         sigchld_happened = 0;
627
628         if (block_sigchld)
629                 EMACS_UNBLOCK_SIGNAL(SIGCHLD);
630 }
631
632 /* For any processes that have changed status and are recorded
633    and such, update the corresponding Lisp_Process.
634    We separate this from record_exited_processes() so that
635    we never have to call this function from within a signal
636    handler.  We block SIGCHLD in case record_exited_processes()
637    is called from a signal handler. */
638
639 /** USG WARNING:  Although it is not obvious from the documentation
640  in signal(2), on a USG system the SIGCLD handler MUST NOT call
641  signal() before executing at least one wait(), otherwise the handler
642  will be called again, resulting in an infinite loop.  The relevant
643  portion of the documentation reads "SIGCLD signals will be queued
644  and the signal-catching function will be continually reentered until
645  the queue is empty".  Invoking signal() causes the kernel to reexamine
646  the SIGCLD queue.   Fred Fish, UniSoft Systems Inc.
647
648  (Note that now this only applies in SYS V Release 2 and before.
649  On SYS V Release 3, we use sigset() to set the signal handler for
650  the first time, and so we don't have to reestablish the signal handler
651  in the handler below.  On SYS V Release 4, we don't get this weirdo
652  behavior when we use sigaction(), which we do use.) */
653
654 static SIGTYPE sigchld_handler(int signo)
655 {
656 #ifdef OBNOXIOUS_SYSV_SIGCLD_BEHAVIOR
657         int old_errno = errno;
658
659         sigchld_happened++;
660         record_exited_processes(0);
661         errno = old_errno;
662 #else
663         sigchld_happened++;
664 #endif
665 #ifdef HAVE_UNIXOID_EVENT_LOOP
666         signal_fake_event();
667 #endif
668         /* WARNING - must come after wait3() for USG systems */
669         EMACS_REESTABLISH_SIGNAL(signo, sigchld_handler);
670         SIGRETURN;
671 }
672
673 #endif                          /* SIGCHLD */
674
675 #ifdef SIGNALS_VIA_CHARACTERS
676 /* Get signal character to send to process if SIGNALS_VIA_CHARACTERS */
677
678 static int process_signal_char(int tty_fd, int signo)
679 {
680         /* Invalid tty_fd ... */
681         if (tty_fd < 0)
682                 return '\0';
683
684         /* If it's not a tty, pray that these default values work */
685         if (!isatty(tty_fd)) {
686 #define CNTL(ch) (037 & (ch))
687                 switch (signo) {
688                 case SIGINT:
689                         return CNTL('C');
690                 case SIGQUIT:
691                         return CNTL('\\');
692 #ifdef SIGTSTP
693                 case SIGTSTP:
694                         return CNTL('Z');
695 #endif
696                 default:
697                         break;
698                 }
699         }
700 #ifdef HAVE_TERMIOS
701         /* TERMIOS is the latest and bestest, and seems most likely to work.
702            If the system has it, use it. */
703         {
704                 struct termios t;
705                 tcgetattr(tty_fd, &t);
706                 switch (signo) {
707                 case SIGINT:
708                         return t.c_cc[VINTR];
709                 case SIGQUIT:
710                         return t.c_cc[VQUIT];
711 #if defined(SIGTSTP) && defined(VSUSP)
712                 case SIGTSTP:
713                         return t.c_cc[VSUSP];
714 #endif
715                 default:
716                         break;
717                 }
718         }
719
720 # elif defined (TIOCGLTC) && defined (TIOCGETC) /* not HAVE_TERMIOS */
721         {
722                 /* On Berkeley descendants, the following IOCTL's retrieve the
723                    current control characters.  */
724                 struct tchars c;
725                 struct ltchars lc;
726                 switch (signo) {
727                 case SIGINT:
728                         ioctl(tty_fd, TIOCGETC, &c);
729                         return c.t_intrc;
730                 case SIGQUIT:
731                         ioctl(tty_fd, TIOCGETC, &c);
732                         return c.t_quitc;
733 #  ifdef SIGTSTP
734                 case SIGTSTP:
735                         ioctl(tty_fd, TIOCGLTC, &lc);
736                         return lc.t_suspc;
737 #  endif                        /* SIGTSTP */
738                 }
739         }
740
741 # elif defined (TCGETA)         /* ! defined (TIOCGLTC) && defined (TIOCGETC) */
742         {
743                 /* On SYSV descendants, the TCGETA ioctl retrieves the current
744                    control characters.  */
745                 struct termio t;
746                 ioctl(tty_fd, TCGETA, &t);
747                 switch (signo) {
748                 case SIGINT:
749                         return t.c_cc[VINTR];
750                 case SIGQUIT:
751                         return t.c_cc[VQUIT];
752 #  ifdef SIGTSTP
753                 case SIGTSTP:
754                         return t.c_cc[VSWTCH];
755 #  endif                        /* SIGTSTP */
756                 }
757         }
758 # else                          /* ! defined (TCGETA) */
759 #error ERROR! Using SIGNALS_VIA_CHARACTERS, but not HAVE_TERMIOS || (TIOCGLTC && TIOCGETC) || TCGETA
760         /* If your system configuration files define SIGNALS_VIA_CHARACTERS,
761            you'd better be using one of the alternatives above!  */
762 # endif                         /* ! defined (TCGETA) */
763         return '\0';
764 }
765 #endif                          /* SIGNALS_VIA_CHARACTERS */
766 \f
767 /**********************************************************************/
768 /*              Process implementation methods                        */
769 /**********************************************************************/
770
771 /*
772  * Allocate and initialize Lisp_Process->process_data
773  */
774
775 static void unix_alloc_process_data(Lisp_Process * p)
776 {
777         p->process_data = xnew(struct unix_process_data);
778
779         UNIX_DATA(p)->connected_via_filedesc_p = 0;
780         UNIX_DATA(p)->infd = -1;
781         UNIX_DATA(p)->subtty = -1;
782         UNIX_DATA(p)->tty_name = Qnil;
783         UNIX_DATA(p)->pty_flag = 0;
784 }
785
786 /*
787  * Mark any Lisp objects in Lisp_Process->process_data
788  */
789
790 static void unix_mark_process_data(Lisp_Process * proc)
791 {
792         mark_object(UNIX_DATA(proc)->tty_name);
793 }
794
795 /*
796  * Initialize SXEmacs process implementation once
797  */
798
799 #ifdef SIGCHLD
800 static void unix_init_process(void)
801 {
802 #ifndef CANNOT_DUMP
803         if (!noninteractive || initialized)
804 #endif
805                 signal(SIGCHLD, sigchld_handler);
806 }
807 #endif                          /* SIGCHLD */
808
809 /*
810  * Initialize any process local data. This is called when newly
811  * created process is connected to real OS file handles. The
812  * handles are generally represented by void* type, but are
813  * of type int (file descriptors) for UNIX.
814  */
815
816 static void
817 unix_init_process_io_handles(Lisp_Process * p, void *in, void *out, int flags)
818 {
819         Lisp_Object process = Qnil;
820         USID        usid = FD_TO_USID((EMACS_INT)in);
821         XSETPROCESS(process, p);
822         puthash((const void *)usid, LISP_TO_VOID(process),
823                 usid_to_process);
824         UNIX_DATA(p)->infd = (EMACS_INT)in;
825 }
826
827 /*
828  * Fork off a subprocess. P is a pointer to a newly created subprocess
829  * object. If this function signals, the caller is responsible for
830  * deleting (and finalizing) the process object.
831  *
832  * The method must return PID of the new process, a (positive??? ####) number
833  * which fits into Lisp_Int. No return value indicates an error, the method
834  * must signal an error instead.
835  */
836
837 static int
838 unix_create_process(Lisp_Process * p,
839                     Lisp_Object * argv, int nargv,
840                     Lisp_Object program, Lisp_Object cur_dir)
841 {
842         int pid;
843         long int inchannel = -1;
844         long int outchannel = -1;
845         /* Use volatile to protect variables from being clobbered by longjmp.  */
846         volatile int forkin = -1;
847         volatile int forkout = -1;
848         volatile int pty_flag = 0;
849
850 #ifdef HAVE_PTYS
851         if (!NILP(Vprocess_connection_type)) {
852                 /* find a new pty, open the master side, return the opened
853                    file handle, and store the name of the corresponding slave
854                    side in global variable pty_name. */
855                 outchannel = inchannel = allocate_pty();
856         }
857
858         if (inchannel >= 0) {
859                 /* You're "supposed" to now open the slave in the child.
860                    On some systems, we can open it here; this allows for
861                    better error checking. */
862 #if !defined(USG)
863                 /* On USG systems it does not work to open the pty's tty here
864                    and then close and reopen it in the child.  */
865 #ifdef O_NOCTTY
866                 /* Don't let this terminal become our controlling terminal
867                    (in case we don't have one).  */
868                 forkout = forkin =
869                     open(pty_name, O_RDWR | O_NOCTTY | OPEN_BINARY, 0);
870 #else
871                 forkout = forkin = open(pty_name, O_RDWR | OPEN_BINARY, 0);
872 #endif
873                 if (forkin < 0)
874                         goto io_failure;
875 #endif                          /* not USG */
876                 UNIX_DATA(p)->pty_flag = pty_flag = 1;
877         } else
878 #endif                          /* HAVE_PTYS */
879         if (create_bidirectional_pipe(
880                     (void*)&inchannel, (void*)&outchannel,
881                     &forkin, &forkout) < 0)
882                 goto io_failure;
883
884 #if 0
885         /* Replaced by close_process_descs */
886         set_exclusive_use(inchannel);
887         set_exclusive_use(outchannel);
888 #endif
889
890         set_descriptor_non_blocking(inchannel);
891         set_descriptor_non_blocking(outchannel);
892
893         /* Record this as an active process, with its channels.
894            As a result, child_setup will close Emacs's side of the pipes.  */
895         init_process_io_handles(p, (void *)inchannel, (void *)outchannel,
896                                 pty_flag ? STREAM_PTY_FLUSHING : 0);
897         /* Record the tty descriptor used in the subprocess.  */
898         UNIX_DATA(p)->subtty = forkin;
899
900         {
901                 /* child_setup must clobber environ on systems with true vfork.
902                    Protect it from permanent change.  */
903                 char **save_environ = environ;
904
905                 pid = fork();
906                 if (pid == 0) {
907         /**** Now we're in the child process ****/
908                         int xforkin = forkin;
909                         int xforkout = forkout;
910
911                         /* Checking for quit in the child is bad because that will
912                            cause I/O, and that, in turn, can confuse the X connection. */
913                         begin_dont_check_for_quit();
914
915                         /* Disconnect the current controlling terminal, pursuant to
916                            making the pty be the controlling terminal of the process.
917                            Also put us in our own process group. */
918
919                         disconnect_controlling_terminal();
920
921 #ifdef HAVE_PTYS
922                         if (pty_flag) {
923                                 /* Open the pty connection and make the pty's terminal
924                                    our controlling terminal.
925
926                                    On systems with TIOCSCTTY, we just use it to set
927                                    the controlling terminal.  On other systems, the
928                                    first TTY we open becomes the controlling terminal.
929                                    So, we end up with four possibilities:
930
931                                    (1) on USG and TIOCSCTTY systems, we open the pty
932                                    and use TIOCSCTTY.
933                                    (2) on other USG systems, we just open the pty.
934                                    (3) on non-USG systems with TIOCSCTTY, we
935                                    just use TIOCSCTTY. (On non-USG systems, we
936                                    already opened the pty in the parent process.)
937                                    (4) on non-USG systems without TIOCSCTTY, we
938                                    close the pty and reopen it.
939
940                                    This would be cleaner if we didn't open the pty
941                                    in the parent process, but doing it that way
942                                    makes it possible to trap error conditions.
943                                    It's harder to convey an error from the child
944                                    process, and I don't feel like messing with
945                                    this now. */
946
947                                 /* There was some weirdo, probably wrong,
948                                    conditionalization on RTU and UNIPLUS here.
949                                    I deleted it.  So sue me. */
950
951                                 /* SunOS has TIOCSCTTY but the close/open method
952                                    also works. */
953
954 #  if defined (USG) || !defined (TIOCSCTTY)
955                                 /* Now close the pty (if we had it open) and reopen it.
956                                    This makes the pty the controlling terminal of the
957                                    subprocess.  */
958                                 /* I wonder if close (open (pty_name, ...)) would work?  */
959                                 if (xforkin >= 0)
960                                         close(xforkin);
961                                 xforkout = xforkin =
962                                     open(pty_name, O_RDWR | OPEN_BINARY, 0);
963                                 if (xforkin < 0) {
964                                         write(1,
965                                               "Couldn't open the pty terminal ",
966                                               31);
967                                         write(1, pty_name, strlen(pty_name));
968                                         write(1, "\n", 1);
969                                         _exit(1);
970                                 }
971 #  endif                        /* USG or not TIOCSCTTY */
972
973                                 /* Miscellaneous setup required for some systems.
974                                    Must be done before using tc* functions on xforkin.
975                                    This guarantees that isatty(xforkin) is true. */
976
977 #  if defined (HAVE_ISASTREAM) && defined (I_PUSH)
978                                 if (isastream(xforkin)) {
979 #    if defined (I_FIND)
980 #      define stream_module_pushed(fd, module) (ioctl (fd, I_FIND, module) == 1)
981 #    else
982 #      define stream_module_pushed(fd, module) 0
983 #    endif
984                                         if (!stream_module_pushed
985                                             (xforkin, "ptem"))
986                                                 ioctl(xforkin, I_PUSH, "ptem");
987                                         if (!stream_module_pushed
988                                             (xforkin, "ldterm"))
989                                                 ioctl(xforkin, I_PUSH,
990                                                       "ldterm");
991                                         if (!stream_module_pushed
992                                             (xforkin, "ttcompat"))
993                                                 ioctl(xforkin, I_PUSH,
994                                                       "ttcompat");
995                                 }
996 #  endif                        /* HAVE_ISASTREAM */
997
998 #  ifdef TIOCSCTTY
999                                 /* We ignore the return value
1000                                    because faith@cs.unc.edu says that is necessary on Linux.  */
1001                                 assert(isatty(xforkin));
1002                                 ioctl(xforkin, TIOCSCTTY, 0);
1003 #  endif                        /* TIOCSCTTY */
1004
1005                                 /* Change the line discipline. */
1006
1007 # if defined (HAVE_TERMIOS) && defined (LDISC1)
1008                                 {
1009                                         struct termios t;
1010                                         assert(isatty(xforkin));
1011                                         tcgetattr(xforkin, &t);
1012                                         t.c_lflag = LDISC1;
1013                                         if (tcsetattr(xforkin, TCSANOW, &t) < 0)
1014                                                 perror
1015                                                     ("create_process/tcsetattr LDISC1 failed\n");
1016                                 }
1017 # elif defined (NTTYDISC) && defined (TIOCSETD)
1018                                 {
1019                                         /* Use new line discipline.  TIOCSETD is accepted and
1020                                            ignored on Sys5.4 systems with ttcompat. */
1021                                         int ldisc = NTTYDISC;
1022                                         assert(isatty(xforkin));
1023                                         ioctl(xforkin, TIOCSETD, &ldisc);
1024                                 }
1025 # endif                         /* TIOCSETD & NTTYDISC */
1026
1027                                 /* Make our process group be the foreground group
1028                                    of our new controlling terminal. */
1029
1030                                 {
1031                                         pid_t piddly =
1032                                             EMACS_GET_PROCESS_GROUP();
1033                                         EMACS_SET_TTY_PROCESS_GROUP(xforkin,
1034                                                                     &piddly);
1035                                 }
1036
1037                                 /* On AIX, we've disabled SIGHUP above once we start a
1038                                    child on a pty.  Now reenable it in the child, so it
1039                                    will die when we want it to.
1040                                    JV: This needs to be done ALWAYS as we might have inherited
1041                                    a SIG_IGN handling from our parent (nohup) and we are in new
1042                                    process group.
1043                                  */
1044                                 signal(SIGHUP, SIG_DFL);
1045                         }
1046
1047                         if (pty_flag)
1048                                 /* Set up the terminal characteristics of the pty. */
1049                                 child_setup_tty(xforkout);
1050
1051 #endif                          /* HAVE_PTYS */
1052
1053                         signal(SIGINT, SIG_DFL);
1054                         signal(SIGQUIT, SIG_DFL);
1055
1056                         {
1057                                 char *current_dir;
1058                                 char **new_argv =
1059                                     alloca_array(char *, nargv + 2);
1060                                 int i;
1061
1062                                 /* Nothing below here GCs so our string pointers shouldn't move. */
1063                                 new_argv[0] = (char *)XSTRING_DATA(program);
1064                                 for (i = 0; i < nargv; i++) {
1065                                         CHECK_STRING(argv[i]);
1066                                         new_argv[i + 1] =
1067                                             (char *)XSTRING_DATA(argv[i]);
1068                                 }
1069                                 new_argv[i + 1] = 0;
1070
1071                                 LISP_STRING_TO_EXTERNAL(cur_dir, current_dir,
1072                                                         Qfile_name);
1073
1074                                 child_setup(xforkin, xforkout, xforkout,
1075                                             new_argv, current_dir);
1076                         }
1077
1078                 }
1079
1080             /**** End of child code ****/
1081  /**** Back in parent process ****/
1082                 environ = save_environ;
1083         }
1084
1085         if (pid < 0) {
1086                 int save_errno = errno;
1087                 close_descriptor_pair(forkin, forkout);
1088                 errno = save_errno;
1089                 report_file_error("Doing fork", Qnil);
1090         }
1091
1092         /* #### dmoore - why is this commented out, otherwise we leave
1093            subtty = forkin, but then we close forkin just below. */
1094         /* UNIX_DATA(p)->subtty = -1; */
1095
1096         /* If the subfork execv fails, and it exits,
1097            this close hangs.  I don't know why.
1098            So have an interrupt jar it loose.  */
1099         if (forkin >= 0)
1100                 close_safely(forkin);
1101         if (forkin != forkout && forkout >= 0)
1102                 close(forkout);
1103
1104 #ifdef HAVE_PTYS
1105         if (pty_flag)
1106                 UNIX_DATA(p)->tty_name = build_string(pty_name);
1107         else
1108 #endif
1109                 UNIX_DATA(p)->tty_name = Qnil;
1110
1111         /* Notice that SIGCHLD was not blocked. (This is not possible on
1112            some systems.) No biggie if SIGCHLD occurs right around the
1113            time that this call happens, because SIGCHLD() does not actually
1114            deselect the process (that doesn't occur until the next time
1115            we're waiting for an event, when status_notify() is called). */
1116         return pid;
1117
1118       io_failure:
1119         {
1120                 int save_errno = errno;
1121                 close_descriptor_pair(forkin, forkout);
1122                 close_descriptor_pair(inchannel, outchannel);
1123                 errno = save_errno;
1124                 report_file_error("Opening pty or pipe", Qnil);
1125                 return 0;       /* not reached */
1126         }
1127 }
1128
1129 /* This is called to set process' virtual terminal size */
1130
1131 static int unix_set_window_size(Lisp_Process * p, int cols, int rows)
1132 {
1133         return set_window_size(UNIX_DATA(p)->infd, cols, rows);
1134 }
1135
1136 /*
1137  * This method is called to update status fields of the process
1138  * structure. If the process has not existed, this method is
1139  * expected to do nothing.
1140  *
1141  * The method is called only for real child processes.
1142  */
1143
1144 #ifdef HAVE_WAITPID
1145 static void unix_update_status_if_terminated(Lisp_Process * p)
1146 {
1147         int w;
1148 #ifdef SIGCHLD
1149         EMACS_BLOCK_SIGNAL(SIGCHLD);
1150 #endif
1151         if (waitpid(XINT(p->pid), &w, WNOHANG) == XINT(p->pid)) {
1152                 p->tick++;
1153                 update_status_from_wait_code(p, &w);
1154         }
1155 #ifdef SIGCHLD
1156         EMACS_UNBLOCK_SIGNAL(SIGCHLD);
1157 #endif
1158 }
1159 #endif
1160
1161 /*
1162  * Update status of all exited processes. Called when SIGCLD has signaled.
1163  */
1164
1165 #ifdef SIGCHLD
1166 static void unix_reap_exited_processes(void)
1167 {
1168         int i;
1169         Lisp_Process *p;
1170
1171 #ifndef OBNOXIOUS_SYSV_SIGCLD_BEHAVIOR
1172         record_exited_processes(1);
1173 #endif
1174
1175         if (exited_processes_index <= 0) {
1176                 return;
1177         }
1178 #ifdef  EMACS_BLOCK_SIGNAL
1179         EMACS_BLOCK_SIGNAL(SIGCHLD);
1180 #endif
1181         for (i = 0; i < exited_processes_index; i++) {
1182                 int pid = exited_processes[i];
1183                 int w = exited_processes_status[i];
1184
1185                 /* Find the process that signaled us, and record its status.  */
1186
1187                 p = 0;
1188                 {
1189                         Lisp_Object tail;
1190                         LIST_LOOP(tail, Vprocess_list) {
1191                                 Lisp_Object proc = XCAR(tail);
1192                                 p = XPROCESS(proc);
1193                                 if (INTP(p->pid) && XINT(p->pid) == pid)
1194                                         break;
1195                                 p = 0;
1196                         }
1197                 }
1198
1199                 if (p) {
1200                         /* Change the status of the process that was found.  */
1201                         p->tick++;
1202                         process_tick++;
1203                         update_status_from_wait_code(p, &w);
1204
1205                         /* If process has terminated, stop waiting for its output.  */
1206                         if (WIFSIGNALED(w) || WIFEXITED(w)) {
1207                                 if (!NILP(p->pipe_instream)) {
1208                                         /* We can't just call event_stream->unselect_process_cb (p)
1209                                            here, because that calls XtRemoveInput, which is not
1210                                            necessarily reentrant, so we can't call this at interrupt
1211                                            level.
1212                                          */
1213                                 }
1214                         }
1215                 } else {
1216                         /* There was no asynchronous process found for that id.  Check
1217                            if we have a synchronous process. Only set sync process status
1218                            if there is one, so we work OK with the waitpid() call in
1219                            wait_for_termination(). */
1220                         if (synch_process_alive != 0) { /* Set the global sync process status variables. */
1221                                 synch_process_alive = 0;
1222
1223                                 /* Report the status of the synchronous process.  */
1224                                 if (WIFEXITED(w))
1225                                         synch_process_retcode = WEXITSTATUS(w);
1226                                 else if (WIFSIGNALED(w))
1227                                         synch_process_death =
1228                                             signal_name(WTERMSIG(w));
1229                         }
1230                 }
1231         }
1232
1233         exited_processes_index = 0;
1234
1235         EMACS_UNBLOCK_SIGNAL(SIGCHLD);
1236 }
1237 #endif                          /* SIGCHLD */
1238
1239 /*
1240  * Stuff the entire contents of LSTREAM to the process output pipe
1241  */
1242
1243 static JMP_BUF send_process_frame;
1244
1245 static SIGTYPE send_process_trap(int signum)
1246 {
1247         EMACS_REESTABLISH_SIGNAL(signum, send_process_trap);
1248         EMACS_UNBLOCK_SIGNAL(signum);
1249         LONGJMP(send_process_frame, 1);
1250 }
1251
1252 static void
1253 unix_send_process(Lisp_Object proc, lstream_t lstream)
1254 {
1255         /* Use volatile to protect variables from being clobbered by longjmp.  */
1256         SIGTYPE(*volatile old_sigpipe) (int) = 0;
1257         volatile Lisp_Object vol_proc = proc;
1258         Lisp_Process *volatile p = XPROCESS(proc);
1259
1260         /* #### JV: layering violation?
1261
1262            This function knows too much about the relation between the encoding
1263            stream (DATA_OUTSTREAM) and the actual output stream p->output_stream.
1264
1265            If encoding streams properly forwarded all calls, we could simply
1266            use DATA_OUTSTREAM everywhere. */
1267
1268         if (!SETJMP(send_process_frame)) {
1269                 /* use a reasonable-sized buffer (somewhere around the size of the
1270                    stream buffer) so as to avoid inundating the stream with blocked
1271                    data. */
1272                 Bufbyte chunkbuf[512];
1273                 Bytecount chunklen = 0;
1274
1275                 do {
1276                         Lstream_data_count writeret;
1277                         if (p->process_type!=PROCESS_TYPE_NETWORK_SERVER_LISTEN) {
1278                                 chunklen = Lstream_read(lstream, chunkbuf, 512);
1279                         }
1280                         old_sigpipe =
1281                             (SIGTYPE(*)(int))signal(SIGPIPE, send_process_trap);
1282                         if (chunklen > 0) {
1283                                 int save_errno;
1284
1285                                 switch (p->process_type) {
1286                                 case PROCESS_TYPE_NETWORK_SERVER_LISTEN:
1287                                         report_file_error ("no writing to listen process possible",
1288                                                            list1 (proc));
1289                                         break;
1290                                 case PROCESS_TYPE_SSL:
1291                                 case PROCESS_TYPE_PROC:
1292                                 case PROCESS_TYPE_NETWORK:
1293                                 case PROCESS_TYPE_MULTICAST:
1294                                 default:
1295 /* Lstream_write() will never successfully write less than
1296  * the amount sent in.  In the worst case, it just buffers
1297  * the unwritten data. */
1298                                         writeret = Lstream_write
1299                                                 (XLSTREAM (DATA_OUTSTREAM(p)),
1300                                                  chunkbuf, chunklen);
1301                                         break;
1302                                 }
1303                                 save_errno = errno;
1304                                 signal (SIGPIPE, old_sigpipe);
1305                                 errno = save_errno;
1306                                 if (writeret < 0)
1307 /* This is a real error.  Blocking errors are handled
1308  * specially inside of the filedesc stream.
1309  */
1310                                         report_file_error ("writing to process",
1311                                                            list1 (proc));
1312                         } else {
1313 /* Need to make sure that everything up to and including the
1314  * last chunk is flushed, even when the pipe is currently
1315  * blocked. */
1316                                 Lstream_flush (XLSTREAM (DATA_OUTSTREAM(p)));
1317                                 signal (SIGPIPE, old_sigpipe);
1318                         }
1319                         while (Lstream_was_blocked_p
1320                                (XLSTREAM(p->pipe_outstream))) {
1321                                 /* Buffer is full.  Wait, accepting input;
1322                                  * that may allow the program
1323                                  * to finish doing output and read more.
1324                                  */
1325                                 Faccept_process_output(Qnil, make_int(1), Qnil);
1326                                 /* It could have *really* finished,
1327                                  * deleting the process */
1328                                 if (NILP(p->pipe_outstream))
1329                                         return;
1330                                 old_sigpipe = (SIGTYPE(*)(int))signal(
1331                                         SIGPIPE,
1332                                         send_process_trap);
1333                                 Lstream_flush(XLSTREAM(p->pipe_outstream));
1334                                 signal(SIGPIPE, old_sigpipe);
1335                         }
1336                         /* Perhaps should abort() if < 0?
1337                          * This should never happen.
1338                          */
1339                 }
1340                 while (chunklen > 0);
1341         } else {        /* We got here from a longjmp() from the SIGPIPE handler */
1342                 signal(SIGPIPE, old_sigpipe);
1343                 /* Close the file lstream so we don't attempt to write to it further */
1344                 /* #### There is controversy over whether this might cause fd leakage */
1345                 /*      my tests say no. -slb */
1346                 XLSTREAM(p->pipe_outstream)->flags &= ~LSTREAM_FL_IS_OPEN;
1347 #ifdef FILE_CODING
1348                 XLSTREAM(p->coding_outstream)->flags &= ~LSTREAM_FL_IS_OPEN;
1349 #endif
1350                 p->status_symbol = Qexit;
1351                 p->exit_code = 256;     /* #### SIGPIPE ??? */
1352                 p->core_dumped = 0;
1353                 p->tick++;
1354                 process_tick++;
1355                 deactivate_process(vol_proc);
1356                 invalid_operation("SIGPIPE raised on process; closed it",
1357                                   p->name);
1358         }
1359
1360         old_sigpipe = (SIGTYPE(*)(int))signal(SIGPIPE, send_process_trap);
1361         Lstream_flush(XLSTREAM(DATA_OUTSTREAM(p)));
1362         signal(SIGPIPE, old_sigpipe);
1363 }
1364
1365 /*
1366  * Send EOF to the process. The default implementation simply
1367  * closes the output stream. The method must return 0 to call
1368  * the default implementation, or 1 if it has taken all care about
1369  * sending EOF to the process.
1370  */
1371
1372 static int unix_process_send_eof(Lisp_Object proc)
1373 {
1374         if (!UNIX_DATA(XPROCESS(proc))->pty_flag)
1375                 return 0;
1376
1377         /* #### get_eof_char simply doesn't return the correct character
1378            here.  Maybe it is needed to determine the right eof
1379            character in init_process_io_handles but here it simply screws
1380            things up. */
1381 #if 0
1382         Bufbyte eof_char = get_eof_char(XPROCESS(proc));
1383         send_process(proc, Qnil, &eof_char, 0, 1);
1384 #else
1385         send_process(proc, Qnil, (const Bufbyte *)"\004", 0, 1);
1386 #endif
1387         return 1;
1388 }
1389
1390 /*
1391  * Called before the process is deactivated. The process object
1392  * is not immediately finalized, just undergoes a transition to
1393  * inactive state.
1394  *
1395  * The return value is a unique stream ID, as returned by
1396  * event_stream_delete_stream_pair
1397  *
1398  * In the lack of this method, only event_stream_delete_stream_pair
1399  * is called on both I/O streams of the process.
1400  *
1401  * The UNIX version guards this by ignoring possible SIGPIPE.
1402  */
1403
1404 static USID unix_deactivate_process(Lisp_Process * p)
1405 {
1406         SIGTYPE(*old_sigpipe) (int) = 0;
1407         USID usid;
1408
1409         if (UNIX_DATA(p)->infd >= 0)
1410                 flush_pending_output(UNIX_DATA(p)->infd);
1411
1412         /* closing the outstream could result in SIGPIPE, so ignore it. */
1413         old_sigpipe = (SIGTYPE(*)(int))signal(SIGPIPE, SIG_IGN);
1414         usid =  FD_TO_USID(UNIX_DATA(p)->infd);
1415         event_stream_delete_stream_pair(p->pipe_instream,
1416                                         p->pipe_outstream);
1417
1418         signal(SIGPIPE, old_sigpipe);
1419
1420         UNIX_DATA(p)->infd = -1;
1421
1422         return usid;
1423 }
1424
1425 /* If the subtty field of the process data is not filled in, do so now. */
1426 static void try_to_initialize_subtty(struct unix_process_data *upd)
1427 {
1428         if (upd->pty_flag && (upd->subtty == -1 || !isatty(upd->subtty))
1429             && STRINGP(upd->tty_name))
1430                 upd->subtty =
1431                     open((char *)XSTRING_DATA(upd->tty_name), O_RDWR, 0);
1432 }
1433
1434 /* Send signal number SIGNO to PROCESS.
1435    CURRENT_GROUP means send to the process group that currently owns
1436    the terminal being used to communicate with PROCESS.
1437    This is used for various commands in shell mode.
1438    If NOMSG is zero, insert signal-announcements into process's buffers
1439    right away.
1440
1441    If we can, we try to signal PROCESS by sending control characters
1442    down the pty.  This allows us to signal inferiors who have changed
1443    their uid, for which killpg would return an EPERM error,
1444    or processes running on other machines via remote login.
1445
1446    The method signals an error if the given SIGNO is not valid. */
1447
1448 static void
1449 unix_kill_child_process(Lisp_Object proc, int signo,
1450                         int current_group, int nomsg)
1451 {
1452         pid_t pgid = -1;
1453         Lisp_Process *p = XPROCESS(proc);
1454         struct unix_process_data *d = UNIX_DATA(p);
1455
1456         switch (signo) {
1457 #ifdef SIGCONT
1458         case SIGCONT:
1459                 p->status_symbol = Qrun;
1460                 p->exit_code = 0;
1461                 p->tick++;
1462                 process_tick++;
1463                 if (!nomsg)
1464                         status_notify();
1465                 break;
1466 #endif                          /* ! defined (SIGCONT) */
1467         case SIGINT:
1468         case SIGQUIT:
1469         case SIGKILL:
1470                 flush_pending_output(d->infd);
1471                 break;
1472         default:
1473                 break;
1474         }
1475
1476         if (!d->pty_flag)
1477                 current_group = 0;
1478
1479         /* If current_group is true, we want to send a signal to the
1480            foreground process group of the terminal our child process is
1481            running on.  You would think that would be easy.
1482
1483            The BSD people invented the TIOCPGRP ioctl to get the foreground
1484            process group of a tty.  That, combined with killpg, gives us
1485            what we want.
1486
1487            However, the POSIX standards people, in their infinite wisdom,
1488            have seen fit to only allow this for processes which have the
1489            terminal as controlling terminal, which doesn't apply to us.
1490
1491            Sooo..., we have to do something non-standard.  The ioctls
1492            TIOCSIGNAL, TIOCSIG, and TIOCSIGSEND send the signal directly on
1493            many systems.  POSIX tcgetpgrp(), since it is *documented* as not
1494            doing what we want, is actually less likely to work than the BSD
1495            ioctl TIOCGPGRP it is supposed to obsolete.  Sometimes we have to
1496            use TIOCGPGRP on the master end, sometimes the slave end
1497            (probably an AIX bug).  So we better get a fd for the slave if we
1498            haven't got it yet.
1499
1500            Anal operating systems like SGI Irix and Compaq Tru64 adhere
1501            strictly to the letter of the law, so our hack doesn't work.
1502            The following fragment from an Irix header file is suggestive:
1503
1504            #ifdef __notdef__
1505            // this is not currently supported
1506            #define TIOCSIGNAL      (tIOC|31)       // pty: send signal to slave
1507            #endif
1508
1509            On those systems where none of our tricks work, we just fall back
1510            to the non-current_group behavior and kill the process group of
1511            the child.
1512          */
1513         if (current_group) {
1514                 try_to_initialize_subtty(d);
1515
1516 #ifdef SIGNALS_VIA_CHARACTERS
1517                 /* If possible, send signals to the entire pgrp
1518                    by sending an input character to it.  */
1519                 if (d->subtty >= 0)
1520                 {
1521                         char sigchar = process_signal_char(d->subtty, signo);
1522                         if (sigchar) {
1523                                 send_process(proc, Qnil, (Bufbyte *) & sigchar,
1524                                              0, 1);
1525                                 return;
1526                         }
1527                 }
1528 #endif                          /* SIGNALS_VIA_CHARACTERS */
1529
1530 #ifdef TIOCGPGRP
1531                 if (pgid == -1)
1532                         ioctl(d->infd, TIOCGPGRP, &pgid);       /* BSD */
1533                 if (pgid == -1 && d->subtty >= 0)
1534                         /* Only this works on AIX! */
1535                         ioctl(d->subtty, TIOCGPGRP, &pgid);
1536 #endif                          /* TIOCGPGRP */
1537
1538                 if (pgid == -1) {
1539                         /* Many systems provide an ioctl to send a signal directly */
1540 #ifdef TIOCSIGNAL               /* Solaris, HP-UX */
1541                         if (ioctl(d->infd, TIOCSIGNAL, signo) != -1)
1542                                 return;
1543 #endif                          /* TIOCSIGNAL */
1544
1545 #ifdef TIOCSIG                  /* BSD */
1546                         if (ioctl(d->infd, TIOCSIG, signo) != -1)
1547                                 return;
1548 #endif                          /* TIOCSIG */
1549                 }
1550         }
1551         /* current_group */
1552         if (pgid == -1)
1553                 /* Either current_group is 0, or we failed to get the foreground
1554                    process group using the trickery above.  So we fall back to
1555                    sending the signal to the process group of our child process.
1556                    Since this is often a shell that ignores signals like SIGINT,
1557                    the shell's subprocess is killed, which is the desired effect.
1558                    The process group of p->pid is always p->pid, since it was
1559                    created as a process group leader. */
1560                 pgid = XINT(p->pid);
1561
1562         /* Finally send the signal. */
1563         if (EMACS_KILLPG(pgid, signo) == -1) {
1564                 /* It's not an error if our victim is already dead.
1565                    And we can't rely on the result of killing a zombie, since
1566                    XPG 4.2 requires that killing a zombie fail with ESRCH,
1567                    while FIPS 151-2 requires that it succeeds! */
1568 #ifdef ESRCH
1569                 if (errno != ESRCH)
1570 #endif
1571                         error("kill (%ld, %ld) failed: %s",
1572                               (long)pgid, (long)signo, strerror(errno));
1573         }
1574 }
1575
1576 /* Send signal SIGCODE to any process in the system given its PID.
1577    Return zero if successful, a negative number upon failure. */
1578
1579 static int unix_kill_process_by_pid(int pid, int sigcode)
1580 {
1581         return kill(pid, sigcode);
1582 }
1583
1584 /* Return TTY name used to communicate with subprocess. */
1585
1586 static Lisp_Object unix_get_tty_name(Lisp_Process * p)
1587 {
1588         return UNIX_DATA(p)->tty_name;
1589 }
1590
1591 /* Canonicalize host name HOST, and return its canonical form.
1592    The default implementation just takes HOST for a canonical name. */
1593
1594 #ifdef HAVE_SOCKETS
1595 static Lisp_Object unix_canonicalize_host_name(Lisp_Object host)
1596 {
1597 #if defined(HAVE_GETADDRINFO) && defined(HAVE_GETNAMEINFO)
1598         struct addrinfo hints, *res;
1599         static char addrbuf[NI_MAXHOST];
1600         Lisp_Object canonname;
1601         int retval;
1602         char *ext_host;
1603
1604         xzero(hints);
1605         hints.ai_flags = AI_CANONNAME;
1606 #ifdef IPV6_CANONICALIZE
1607         hints.ai_family = AF_UNSPEC;
1608 #else
1609         hints.ai_family = PF_INET;
1610 #endif
1611         hints.ai_socktype = SOCK_STREAM;
1612         hints.ai_protocol = 0;
1613         LISP_STRING_TO_EXTERNAL(host, ext_host, Qnative);
1614         retval = getaddrinfo(ext_host, NULL, &hints, &res);
1615         if (retval != 0) {
1616                 char *gai_error_l;
1617
1618                 EXTERNAL_TO_C_STRING(gai_strerror(retval),
1619                                      gai_error_l, Qnative);
1620                 maybe_error(Qprocess, ERROR_ME_NOT,
1621                             "%s \"%s\"", gai_error_l, XSTRING_DATA(host));
1622                 canonname = host;
1623         } else {
1624                 int gni = getnameinfo(res->ai_addr, res->ai_addrlen,
1625                                       addrbuf, sizeof(addrbuf),
1626                                       NULL, 0, NI_NUMERICHOST);
1627                 canonname = gni ? host : build_ext_string(addrbuf, Qnative);
1628
1629                 freeaddrinfo(res);
1630         }
1631
1632         return canonname;
1633 #else                           /* ! HAVE_GETADDRINFO */
1634         struct sockaddr_in address;
1635
1636         if (!get_internet_address(host, &address, ERROR_ME_NOT))
1637                 return host;
1638
1639         if (address.sin_family == AF_INET)
1640                 return build_string(inet_ntoa(address.sin_addr));
1641         else
1642                 /* #### any clue what to do here? */
1643                 return host;
1644 #endif                          /* ! HAVE_GETADDRINFO */
1645 }
1646
1647 /* Open a TCP network connection to a given HOST/SERVICE.
1648    Treated exactly like a normal process when reading and writing.
1649    Only differences are in status display and process deletion.
1650    A network connection has no PID; you cannot signal it.  All you can
1651    do is deactivate and close it via delete-process. */
1652
1653 static void
1654 unix_open_network_stream(Lisp_Object name, Lisp_Object host,
1655                          Lisp_Object service, Lisp_Object protocol,
1656                          void **vinfd, void **voutfd)
1657 {
1658         EMACS_INT inch;
1659         EMACS_INT outch;
1660         volatile int s;
1661         volatile int port;
1662         volatile int retry = 0;
1663         int retval;
1664
1665         CHECK_STRING(host);
1666
1667         if (!EQ(protocol, Qtcp) && !EQ(protocol, Qudp))
1668                 invalid_argument("Unsupported protocol", protocol);
1669
1670         {
1671 #if defined(HAVE_GETADDRINFO) && defined(HAVE_GETNAMEINFO)
1672                 struct addrinfo hints, *res;
1673                 struct addrinfo *volatile lres;
1674                 char *portstring;
1675                 volatile int xerrno = 0;
1676                 volatile int failed_connect = 0;
1677                 char *ext_host;
1678                 char portbuf[sizeof(long)*3 + 2];
1679                 /*
1680                  * Caution: service can either be a string or int.
1681                  * Convert to a C string for later use by getaddrinfo.
1682                  */
1683                 if (INTP(service)) {
1684                         int sz= snprintf(portbuf, sizeof(portbuf), "%ld",
1685                                          (long)XINT(service));
1686                         assert(sz >= 0 && (size_t)sz < sizeof(portbuf));
1687                         portstring = portbuf;
1688                         port = htons((unsigned short)XINT(service));
1689                 } else {
1690                         CHECK_STRING(service);
1691                         LISP_STRING_TO_EXTERNAL(service, portstring, Qnative);
1692                         port = 0;
1693                 }
1694
1695                 xzero(hints);
1696                 hints.ai_flags = 0;
1697                 hints.ai_family = AF_UNSPEC;
1698                 if (EQ(protocol, Qtcp))
1699                         hints.ai_socktype = SOCK_STREAM;
1700                 else            /* EQ (protocol, Qudp) */
1701                         hints.ai_socktype = SOCK_DGRAM;
1702                 hints.ai_protocol = 0;
1703                 LISP_STRING_TO_EXTERNAL(host, ext_host, Qnative);
1704                 retval = getaddrinfo(ext_host, portstring, &hints, &res);
1705                 if (retval != 0) {
1706                         char *gai_error_l;
1707
1708                         EXTERNAL_TO_C_STRING(gai_strerror(retval),
1709                                              gai_error_l, Qnative);
1710                         error("%s/%s %s", XSTRING_DATA(host), portstring,
1711                               gai_error_l);
1712                 }
1713
1714                 /* address loop */
1715                 for (lres = res; lres; lres = lres->ai_next) {
1716                         if (EQ(protocol, Qtcp))
1717                                 s = socket(lres->ai_family, SOCK_STREAM, 0);
1718                         else    /* EQ (protocol, Qudp) */
1719                                 s = socket(lres->ai_family, SOCK_DGRAM, 0);
1720
1721                         if (s < 0)
1722                                 continue;
1723
1724                         /* Turn off interrupts here -- see comments below.
1725                            There used to be code which called
1726                            bind_polling_period() to slow the polling period down
1727                            rather than turn it off, but that seems rather bogus
1728                            to me.  Best thing here is to use a non-blocking
1729                            connect or something, to check for QUIT. */
1730
1731                         /* Comments that are not quite valid: */
1732
1733                         /* Kernel bugs (on Ultrix at least) cause lossage (not
1734                            just EINTR) when connect is interrupted.  So let's
1735                            not let it get interrupted.  Note we do not turn off
1736                            polling, because polling is only used when not
1737                            interrupt_input, and thus not normally used on the
1738                            systems which have this bug.  On systems which use
1739                            polling, there's no way to quit if polling is turned
1740                            off.  */
1741
1742                         /* Slow down polling.  Some kernels have a bug which
1743                            causes retrying connect to fail after a connect.  */
1744
1745                         slow_down_interrupts();
1746
1747                       loop:
1748
1749                         /* A system call interrupted with a SIGALRM or SIGIO
1750                            comes back here, with can_break_system_calls reset to
1751                            0. */
1752                         SETJMP(break_system_call_jump);
1753                         if (QUITP) {
1754                                 speed_up_interrupts();
1755                                 REALLY_QUIT;
1756                                 /* In case something really weird happens ... */
1757                                 slow_down_interrupts();
1758                         }
1759
1760                         /* Break out of connect with a signal (it isn't
1761                            otherwise possible).  Thus you don't get screwed with
1762                            a hung network. */
1763                         can_break_system_calls = 1;
1764                         retval = connect(s, lres->ai_addr, lres->ai_addrlen);
1765                         can_break_system_calls = 0;
1766                         if (retval == -1) {
1767                                 xerrno = errno;
1768                                 if (errno != EISCONN) {
1769                                         if (errno == EINTR)
1770                                                 goto loop;
1771                                         if (errno == EADDRINUSE && retry < 20) {
1772                                                 /* A delay here is needed on
1773                                                    some FreeBSD systems, and
1774                                                    it is harmless, since this
1775                                                    retrying takes time anyway
1776                                                    and should be infrequent.
1777                                                    `sleep-for' allowed for
1778                                                    quitting this loop with
1779                                                    interrupts slowed down so
1780                                                    it can't be used here.
1781                                                    Async timers should already
1782                                                    be disabled at this point
1783                                                    so we can use `sleep'. */
1784                                                 retry++;
1785                                                 goto loop;
1786                                         }
1787                                 }
1788
1789                                 failed_connect = 1;
1790                                 close(s);
1791                                 s = -1;
1792
1793                                 speed_up_interrupts();
1794
1795                                 continue;
1796                         }
1797
1798                         if (port == 0) {
1799                                 int gni;
1800                                 char servbuf[NI_MAXSERV];
1801
1802                                 if (EQ(protocol, Qtcp))
1803                                         gni =
1804                                             getnameinfo(lres->ai_addr,
1805                                                         lres->ai_addrlen, NULL,
1806                                                         0, servbuf,
1807                                                         sizeof(servbuf),
1808                                                         NI_NUMERICSERV);
1809                                 else    /* EQ (protocol, Qudp) */
1810                                         gni =
1811                                             getnameinfo(lres->ai_addr,
1812                                                         lres->ai_addrlen, NULL,
1813                                                         0, servbuf,
1814                                                         sizeof(servbuf),
1815                                                         NI_NUMERICSERV |
1816                                                         NI_DGRAM);
1817
1818                                 if (gni == 0)
1819                                         port = strtol(servbuf, NULL, 10);
1820                         }
1821
1822                         break;
1823                 }               /* address loop */
1824
1825                 speed_up_interrupts();
1826
1827                 freeaddrinfo(res);
1828                 if (s < 0) {
1829                         errno = xerrno;
1830
1831                         if (failed_connect)
1832                                 report_file_error("connection failed",
1833                                                   list2(host, name));
1834                         else
1835                                 report_file_error("error creating socket",
1836                                                   list1(name));
1837                 }
1838 #else                           /* ! HAVE_GETADDRINFO */
1839                 struct sockaddr_in address;
1840
1841                 if (INTP(service))
1842                         port = htons((unsigned short)XINT(service));
1843                 else {
1844                         struct servent *svc_info;
1845                         CHECK_STRING(service);
1846
1847                         if (EQ(protocol, Qtcp))
1848                                 svc_info =
1849                                     getservbyname((char *)XSTRING_DATA(service),
1850                                                   "tcp");
1851                         else    /* EQ (protocol, Qudp) */
1852                                 svc_info =
1853                                     getservbyname((char *)XSTRING_DATA(service),
1854                                                   "udp");
1855
1856                         if (svc_info == 0)
1857                                 invalid_argument("Unknown service", service);
1858                         port = svc_info->s_port;
1859                 }
1860
1861                 get_internet_address(host, &address, ERROR_ME);
1862                 address.sin_port = port;
1863
1864                 if (EQ(protocol, Qtcp))
1865                         s = socket(address.sin_family, SOCK_STREAM, 0);
1866                 else            /* EQ (protocol, Qudp) */
1867                         s = socket(address.sin_family, SOCK_DGRAM, 0);
1868
1869                 if (s < 0)
1870                         report_file_error("error creating socket", list1(name));
1871
1872                 /* Turn off interrupts here -- see comments below.  There used to
1873                    be code which called bind_polling_period() to slow the polling
1874                    period down rather than turn it off, but that seems rather
1875                    bogus to me.  Best thing here is to use a non-blocking connect
1876                    or something, to check for QUIT. */
1877
1878                 /* Comments that are not quite valid: */
1879
1880                 /* Kernel bugs (on Ultrix at least) cause lossage (not just EINTR)
1881                    when connect is interrupted.  So let's not let it get interrupted.
1882                    Note we do not turn off polling, because polling is only used
1883                    when not interrupt_input, and thus not normally used on the systems
1884                    which have this bug.  On systems which use polling, there's no way
1885                    to quit if polling is turned off.  */
1886
1887                 /* Slow down polling.  Some kernels have a bug which causes retrying
1888                    connect to fail after a connect.  */
1889
1890                 slow_down_interrupts();
1891
1892               loop:
1893
1894                 /* A system call interrupted with a SIGALRM or SIGIO comes back
1895                    here, with can_break_system_calls reset to 0. */
1896                 SETJMP(break_system_call_jump);
1897                 if (QUITP) {
1898                         speed_up_interrupts();
1899                         REALLY_QUIT;
1900                         /* In case something really weird happens ... */
1901                         slow_down_interrupts();
1902                 }
1903
1904                 /* Break out of connect with a signal (it isn't otherwise possible).
1905                    Thus you don't get screwed with a hung network. */
1906                 can_break_system_calls = 1;
1907                 retval =
1908                     connect(s, (struct sockaddr *)&address, sizeof(address));
1909                 can_break_system_calls = 0;
1910                 if (retval == -1 && errno != EISCONN) {
1911                         int xerrno = errno;
1912                         if (errno == EINTR)
1913                                 goto loop;
1914                         if (errno == EADDRINUSE && retry < 20) {
1915                                 /* A delay here is needed on some FreeBSD
1916                                    systems, and it is harmless, since this
1917                                    retrying takes time anyway and should
1918                                    be infrequent.  `sleep-for' allowed for
1919                                    quitting this loop with interrupts
1920                                    slowed down so it can't be used here.
1921                                    Async timers should already be disabled
1922                                    at this point so we can use `sleep'. */
1923                                 retry++;
1924                                 goto loop;
1925                         }
1926
1927                         close(s);
1928
1929                         speed_up_interrupts();
1930
1931                         errno = xerrno;
1932                         report_file_error("connection failed",
1933                                           list2(host, name));
1934                 }
1935
1936                 speed_up_interrupts();
1937 #endif                          /* ! HAVE_GETADDRINFO */
1938         }
1939
1940         inch = s;
1941         outch = dup(s);
1942         if (outch < 0) {
1943                 close(s);       /* this used to be leaked; from Kyle Jones */
1944                 report_file_error("error duplicating socket", list1(name));
1945         }
1946
1947         set_socket_nonblocking_maybe(inch, port, "tcp");
1948
1949         *vinfd = (void *)inch;
1950         *voutfd = (void *)outch;
1951 }
1952
1953
1954 /*
1955   Return the listener process of the accepted listened process
1956 */
1957 static Lisp_Object unix_network_process_listener(Lisp_Object process)
1958 {
1959         Lisp_Process *listener = 0,
1960                      *p = XPROCESS(process);
1961         Lisp_Object   ret = Qnil;
1962         int           sock = 0;
1963         struct  gcpro ngcpro1;
1964
1965         if (!PROCESS_READABLE_P(p))
1966                 return Qnil;
1967
1968         if (!CONSP(p->pid) || NILP(XCDR(XCDR(p->pid))))
1969                 return Qnil;
1970
1971         NGCPRO1(ret);
1972         sock = XINT(XCAR(XCDR(p->pid)));
1973         listener = get_process_from_usid(FD_TO_USID(sock));
1974         ret = listener ? (Lisp_Object)listener : Qnil;
1975         NUNGCPRO;
1976         return ret;
1977 }
1978
1979 /*
1980   Unwind a call to the network server stream accept below
1981 */
1982 static Lisp_Object exec_acceptor_unwind(Lisp_Object datum)
1983 {
1984         Lisp_Cons *d = XCONS(datum);
1985         free_cons(d);
1986         return Qnil;
1987 }
1988
1989
1990 /*
1991   Accept a connection being listened in the given network server
1992   stream process.
1993   Create a new network stream for the accepted connection.
1994   Call the acceptor callback and setup the sentinel and filter functions
1995 */
1996 static void unix_network_server_accept(Lisp_Object process)
1997 {
1998         Lisp_Process *p = XPROCESS(process);
1999         Lisp_Object np = Qnil;
2000         Lisp_Object acceptor = Qnil, filter = Qnil, sentinel = Qnil;
2001         Lisp_Object bufname = Qnil;
2002         Lisp_Object buffer = Qnil;
2003         long int ns, inch, outch;
2004         struct sockaddr_in sa;
2005         int sa_size = sizeof(sa);
2006         struct gcpro ngcpro1, ngcpro2, ngcpro3, ngcpro4, ngcpro5;
2007
2008         if (!PROCESS_READABLE_P(p))
2009                 return;
2010
2011         /* Make sure the listen process is not disconnected
2012            afterwards.  We have to make this here because in process.c
2013            we should not have any knowledge we need to do this, and in
2014            unix_open_network_server_stream we have no access to the
2015            process struct. It works, so I'm not complaining...
2016          */
2017         UNIX_DATA(p)->connected_via_filedesc_p = 1;
2018
2019         errno = 0; /* if we got an error, let it be from the accept call */
2020         ns = accept((int)UNIX_DATA(p)->infd, (struct sockaddr*)(&sa),
2021                     (socklen_t*)&sa_size);
2022         if ( ns < 0 )
2023                 return;
2024
2025         NGCPRO5(np,bufname,acceptor,filter,sentinel);
2026         if (CONSP(p->process_type_data)) {
2027                 acceptor = XCAR(p->process_type_data);
2028                 filter = XCDR(p->process_type_data);
2029                 if (CONSP(filter)) {
2030                         sentinel = XCDR(filter);
2031                         filter   = XCAR(filter);
2032                 }
2033                 if (CONSP(sentinel)) {
2034                         bufname  = XCDR(sentinel);
2035                         sentinel = XCAR(sentinel);
2036                 }
2037                 if (CONSP(bufname)) {
2038                         bufname  = XCAR(bufname);
2039                 }
2040         }
2041         if (!NILP(bufname)) {
2042                 Lisp_Object args[] = {
2043                         build_string("<server port:%S listened_on:%S>"),
2044                         make_int(sa.sin_port), bufname
2045                 };
2046                 bufname = Fformat( 3, args );
2047         } else {
2048                 Lisp_Object args[] = {
2049                         build_string("<server proc:%S pid:%S service:%S>"),
2050                         p->name, p->pid, make_int(sa.sin_port)
2051                 };
2052                 bufname = Fformat( 5, args );
2053         }
2054         if (!NILP(bufname) ) {
2055                 bufname = Fgenerate_new_buffer_name(bufname,Qnil);
2056                 buffer  =  Fget_buffer_create(bufname);
2057         }
2058         np = make_process_internal(p->name);
2059         XPROCESS(np)->pid = Fcons( make_int(sa.sin_port),
2060                                    Fcons(make_int(UNIX_DATA(p)->infd),p->pid));
2061         XPROCESS(np)->process_type = PROCESS_TYPE_NETWORK;
2062         XPROCESS(np)->buffer = buffer;
2063         inch = ns;
2064         outch = dup(ns);
2065         set_socket_nonblocking_maybe(inch, sa.sin_port, "tcp");
2066         init_process_io_handles(XPROCESS(np), (void *)inch, (void *)outch,
2067                                 STREAM_NETWORK_CONNECTION);
2068         /* Process the call backs.. */
2069         if (CONSP(p->process_type_data)) {
2070                 if (!NILP(filter)) {
2071                         XPROCESS(np)->filter = filter;
2072                 }
2073                 if (!NILP(sentinel)) {
2074                         XPROCESS(np)->sentinel = sentinel;
2075                 }
2076                 if (!NILP(acceptor)) {
2077                         int speccount = specpdl_depth();
2078                         record_unwind_protect(exec_acceptor_unwind,
2079                                               noseeum_cons(process, acceptor));
2080                         running_asynch_code = 1;
2081                         call1_trapping_errors("Error in server stream acceptor",
2082                                               acceptor, np);
2083                         running_asynch_code = 0;
2084                         restore_match_data();
2085                         unbind_to(speccount, Qnil);
2086
2087                 }
2088         } else {
2089                 /* We have to log something here... */
2090         }
2091         event_stream_select_process(XPROCESS(np));
2092         NUNGCPRO;
2093 }
2094
2095 /* Open a TCP network connection to a given HOST/SERVICE.
2096    Treated exactly like a normal process when reading and writing.
2097    Only differences are in status display and process deletion.
2098    A network connection has no PID; you cannot signal it.  All you can
2099    do is deactivate and close it via delete-process. */
2100
2101 static void
2102 unix_open_network_server_stream(Lisp_Object name, Lisp_Object host,
2103                                 Lisp_Object service, Lisp_Object protocol,
2104                                 void **vinfd, void **voutfd)
2105 {
2106         EMACS_INT inch;
2107         EMACS_INT outch;
2108         volatile int s;
2109         volatile int port;
2110         volatile int retry = 0;
2111         int retval;
2112         /* FIXME: Limited to 5 since it is the maximum for several BSD
2113            based implementations of sockets, and it is an acceptable
2114            value for a low rate of service purpose like this facility
2115            was designed for. */
2116         int listenQ = 5;
2117
2118
2119         if (!EQ(protocol, Qtcp) && !EQ(protocol, Qudp))
2120                 invalid_argument("Unsupported protocol", protocol);
2121
2122         {
2123 #if defined(HAVE_GETADDRINFO) && defined(HAVE_GETNAMEINFO)
2124                 struct addrinfo hints, *res;
2125                 struct addrinfo *volatile lres;
2126                 char *portstring;
2127                 volatile int xerrno = 0;
2128                 volatile int failed_connect = 0;
2129                 char *ext_host;
2130                 char portbuf[sizeof(long)*3 + 2];
2131                 /*
2132                  * Caution: service can either be a string or int.
2133                  * Convert to a C string for later use by getaddrinfo.
2134                  */
2135                 if (INTP(service)) {
2136                         int sz = snprintf(portbuf, sizeof(portbuf), "%ld",
2137                                           (long)XINT(service));
2138                         assert(sz >= 0 && (size_t)sz < sizeof(portbuf));
2139                         portstring = portbuf;
2140                         port = htons((unsigned short)XINT(service));
2141                 } else {
2142                         CHECK_STRING(service);
2143                         LISP_STRING_TO_EXTERNAL(service, portstring, Qnative);
2144                         port = 0;
2145                 }
2146
2147                 xzero(hints);
2148                 hints.ai_flags = 0;
2149                 hints.ai_family = AF_UNSPEC;
2150                 if (EQ(protocol, Qtcp))
2151                         hints.ai_socktype = SOCK_STREAM;
2152                 else            /* EQ (protocol, Qudp) */
2153                         hints.ai_socktype = SOCK_DGRAM;
2154                 hints.ai_protocol = 0;
2155                 if (SYMBOLP(host) ) {
2156                         if (EQ(host,Qip_any)) {
2157                                 hints.ai_flags |= AI_PASSIVE;
2158                         } else if (!EQ(host,Qlocalhost)) {
2159                                 invalid_argument("invalid host ",host);
2160                         } else {
2161                                 /* If using localhost, not passing
2162                                    AI_PASSIVE will cause getaddrinfo
2163                                    to return a proper addr spec for
2164                                    listening only to local
2165                                    connections. */
2166                         }
2167                         retval = getaddrinfo(NULL, portstring, &hints, &res);
2168                 } else {
2169                         CHECK_STRING(host);
2170                         LISP_STRING_TO_EXTERNAL(host, ext_host, Qnative);
2171                         retval = getaddrinfo(ext_host, portstring, &hints, &res);
2172                 }
2173                 if (retval != 0) {
2174                         char *gai_error_l;
2175
2176                         EXTERNAL_TO_C_STRING(gai_strerror(retval),
2177                                              gai_error_l, Qnative);
2178                         error("%s/%s %s", XSTRING_DATA(host), portstring,
2179                               gai_error_l);
2180                 }
2181
2182                 /* address loop */
2183                 for (lres = res; lres; lres = lres->ai_next) {
2184                         if (EQ(protocol, Qtcp))
2185                                 s = socket(lres->ai_family, SOCK_STREAM, 0);
2186                         else    /* EQ (protocol, Qudp) */
2187                                 s = socket(lres->ai_family, SOCK_DGRAM, 0);
2188
2189                         if (s < 0)
2190                                 continue;
2191
2192                         /* Turn off interrupts here -- see comments below.
2193                            There used to be code which called
2194                            bind_polling_period() to slow the polling period down
2195                            rather than turn it off, but that seems rather bogus
2196                            to me.  Best thing here is to use a non-blocking
2197                            connect or something, to check for QUIT. */
2198
2199                         /* Comments that are not quite valid: */
2200
2201                         /* Kernel bugs (on Ultrix at least) cause lossage (not
2202                            just EINTR) when connect is interrupted.  So let's
2203                            not let it get interrupted.  Note we do not turn off
2204                            polling, because polling is only used when not
2205                            interrupt_input, and thus not normally used on the
2206                            systems which have this bug.  On systems which use
2207                            polling, there's no way to quit if polling is turned
2208                            off.  */
2209
2210                         /* Slow down polling.  Some kernels have a bug which
2211                            causes retrying connect to fail after a connect.  */
2212
2213                         slow_down_interrupts();
2214
2215                       loop:
2216
2217                         /* A system call interrupted with a SIGALRM or SIGIO
2218                            comes back here, with can_break_system_calls reset to
2219                            0. */
2220                         SETJMP(break_system_call_jump);
2221                         if (QUITP) {
2222                                 speed_up_interrupts();
2223                                 REALLY_QUIT;
2224                                 /* In case something really weird happens ... */
2225                                 slow_down_interrupts();
2226                         }
2227
2228                         /* Break out of connect with a signal (it isn't
2229                            otherwise possible).  Thus you don't get screwed with
2230                            a hung network. */
2231                         can_break_system_calls = 1;
2232                         retval = bind(s, lres->ai_addr, lres->ai_addrlen);
2233                         if (retval >= 0 )
2234                                 retval = listen(s, listenQ);
2235                         can_break_system_calls = 0;
2236                         if (retval == -1) {
2237                                 xerrno = errno;
2238                                 if (errno != EISCONN) {
2239                                         if (errno == EINTR)
2240                                                 goto loop;
2241                                         if (errno == EADDRINUSE && retry < 20) {
2242                                                 /* A delay here is needed on
2243                                                    some FreeBSD systems, and it
2244                                                    is harmless, since this
2245                                                    retrying takes time anyway
2246                                                    and should be infrequent.
2247                                                    `sleep-for' allowed for
2248                                                    quitting this loop with
2249                                                    interrupts slowed down so it
2250                                                    can't be used here.  Async
2251                                                    timers should already be
2252                                                    disabled at this point so we
2253                                                    can use `sleep'. */
2254                                                 retry++;
2255                                                 goto loop;
2256                                         }
2257                                 }
2258
2259                                 failed_connect = 1;
2260                                 close(s);
2261                                 s = -1;
2262
2263                                 speed_up_interrupts();
2264
2265                                 continue;
2266                         }
2267
2268                         if (port == 0) {
2269                                 int gni;
2270                                 char servbuf[NI_MAXSERV];
2271
2272                                 if (EQ(protocol, Qtcp))
2273                                         gni =
2274                                             getnameinfo(lres->ai_addr,
2275                                                         lres->ai_addrlen, NULL,
2276                                                         0, servbuf,
2277                                                         sizeof(servbuf),
2278                                                         NI_NUMERICSERV);
2279                                 else    /* EQ (protocol, Qudp) */
2280                                         gni =
2281                                             getnameinfo(lres->ai_addr,
2282                                                         lres->ai_addrlen, NULL,
2283                                                         0, servbuf,
2284                                                         sizeof(servbuf),
2285                                                         NI_NUMERICSERV |
2286                                                         NI_DGRAM);
2287
2288                                 if (gni == 0)
2289                                         port = strtol(servbuf, NULL, 10);
2290                         }
2291
2292                         break;
2293                 }               /* address loop */
2294
2295                 speed_up_interrupts();
2296
2297                 freeaddrinfo(res);
2298                 if (s < 0) {
2299                         errno = xerrno;
2300
2301                         if (failed_connect)
2302                                 report_file_error("bind failed",
2303                                                   list2(host, name));
2304                         else
2305                                 report_file_error("error creating socket",
2306                                                   list1(name));
2307                 }
2308 #else                           /* ! HAVE_GETADDRINFO */
2309                 struct sockaddr_in address;
2310
2311                 if (INTP(service))
2312                         port = htons((unsigned short)XINT(service));
2313                 else {
2314                         struct servent *svc_info;
2315                         CHECK_STRING(service);
2316
2317                         if (EQ(protocol, Qtcp))
2318                                 svc_info =
2319                                     getservbyname((char *)XSTRING_DATA(service),
2320                                                   "tcp");
2321                         else    /* EQ (protocol, Qudp) */
2322                                 svc_info =
2323                                     getservbyname((char *)XSTRING_DATA(service),
2324                                                   "udp");
2325
2326                         if (svc_info == 0)
2327                                 invalid_argument("Unknown service", service);
2328                         port = svc_info->s_port;
2329                 }
2330                 if (SYMBOLP(host)) {
2331                         if (EQ(host,Qip_any)) {
2332                                 address.sin_addr.s_host = htonl(INADDR_ANY);
2333                         } else if (EQ(host,Qlocalhost)) {
2334                                 address.sin_addr.s_host = htonl(INADDR_LOOPBACK);
2335                         } else {
2336                                 invalid_argument("invalid host ",host);
2337                         }
2338                 } else {
2339                         get_internet_address(host, &address, ERROR_ME);
2340                 }
2341                 address.sin_port = port;
2342
2343                 if (EQ(protocol, Qtcp))
2344                         s = socket(address.sin_family, SOCK_STREAM, 0);
2345                 else            /* EQ (protocol, Qudp) */
2346                         s = socket(address.sin_family, SOCK_DGRAM, 0);
2347
2348                 if (s < 0)
2349                         report_file_error("error creating socket", list1(name));
2350
2351                 /* Turn off interrupts here -- see comments below.  There used to
2352                    be code which called bind_polling_period() to slow the polling
2353                    period down rather than turn it off, but that seems rather
2354                    bogus to me.  Best thing here is to use a non-blocking connect
2355                    or something, to check for QUIT. */
2356
2357                 /* Comments that are not quite valid: */
2358
2359                 /* Kernel bugs (on Ultrix at least) cause lossage (not just EINTR)
2360                    when connect is interrupted.  So let's not let it get interrupted.
2361                    Note we do not turn off polling, because polling is only used
2362                    when not interrupt_input, and thus not normally used on the systems
2363                    which have this bug.  On systems which use polling, there's no way
2364                    to quit if polling is turned off.  */
2365
2366                 /* Slow down polling.  Some kernels have a bug which causes retrying
2367                    connect to fail after a connect.  */
2368
2369                 slow_down_interrupts();
2370
2371               loop:
2372
2373                 /* A system call interrupted with a SIGALRM or SIGIO comes back
2374                    here, with can_break_system_calls reset to 0. */
2375                 SETJMP(break_system_call_jump);
2376                 if (QUITP) {
2377                         speed_up_interrupts();
2378                         REALLY_QUIT;
2379                         /* In case something really weird happens ... */
2380                         slow_down_interrupts();
2381                 }
2382
2383                 /* Break out of connect with a signal (it isn't otherwise possible).
2384                    Thus you don't get screwed with a hung network. */
2385                 can_break_system_calls = 1;
2386                 retval =
2387                     bind(s, (struct sockaddr *)&address, sizeof(address));
2388                 if ( retval >= 0 )
2389                         listen( s, listenQ ); /* @@@ FIXME: This should be a parameter */
2390                 can_break_system_calls = 0;
2391                 if (retval == -1 && errno != EISCONN) {
2392                         int xerrno = errno;
2393                         if (errno == EINTR)
2394                                 goto loop;
2395                         if (errno == EADDRINUSE && retry < 20) {
2396                                 /* A delay here is needed on some FreeBSD
2397                                    systems, and it is harmless, since this
2398                                    retrying takes time anyway and should be
2399                                    infrequent.  `sleep-for' allowed for quitting
2400                                    this loop with interrupts slowed down so it
2401                                    can't be used here.  Async timers should
2402                                    already be disabled at this point so we can
2403                                    use `sleep'. */
2404                                 retry++;
2405                                 goto loop;
2406                         }
2407
2408                         close(s);
2409
2410                         speed_up_interrupts();
2411
2412                         errno = xerrno;
2413                         report_file_error("connection failed",
2414                                           list2(host, name));
2415                 }
2416
2417                 speed_up_interrupts();
2418 #endif                          /* ! HAVE_GETADDRINFO */
2419         }
2420
2421         inch = s;
2422         outch = dup(s);
2423         if (outch < 0) {
2424                 close(s);       /* this used to be leaked; from Kyle Jones */
2425                 report_file_error("error duplicating socket", list1(name));
2426         }
2427
2428         set_socket_nonblocking_maybe(inch, port, "tcp");
2429
2430         *vinfd = (void *)inch;
2431         *voutfd = (void *)outch;
2432 }
2433
2434
2435 #ifdef HAVE_MULTICAST
2436
2437 /* Didier Verna <didier@xemacs.org> Nov. 28 1997.
2438
2439    This function is similar to open-network-stream-internal, but provides a
2440    mean to open an UDP multicast connection instead of a TCP one. Like in the
2441    TCP case, the multicast connection will be seen as a sub-process,
2442
2443    Some notes:
2444    - Normally, we should use sendto and recvfrom with non connected
2445    sockets. The current code doesn't allow us to do this. In the future, it
2446    would be a good idea to extend the process data structure in order to deal
2447    properly with the different types network connections.
2448    - For the same reason, when leaving a multicast group, it is better to make
2449    a setsockopt - IP_DROP_MEMBERSHIP before closing the descriptors.
2450    Unfortunately, this can't be done here because delete_process doesn't know
2451    about the kind of connection we have. However, this is not such an
2452    important issue.
2453 */
2454
2455 static void
2456 unix_open_multicast_group(Lisp_Object name, Lisp_Object dest,
2457                           Lisp_Object port, Lisp_Object ttl, void **vinfd,
2458                           void **voutfd)
2459 {
2460         struct ip_mreq imr;
2461         struct sockaddr_in sa;
2462         struct protoent *udp;
2463         EMACS_INT ws, rs;
2464         int theport;
2465         unsigned char thettl;
2466         int one = 1;            /* For REUSEADDR */
2467         int ret;
2468         volatile int retry = 0;
2469
2470         CHECK_STRING(dest);
2471
2472         CHECK_NATNUM(port);
2473         theport = htons((unsigned short)XINT(port));
2474
2475         CHECK_NATNUM(ttl);
2476         thettl = (unsigned char)XINT(ttl);
2477
2478         if ((udp = getprotobyname("udp")) == NULL)
2479                 type_error(Qinvalid_operation,
2480                            "No info available for UDP protocol");
2481
2482         /* Init the sockets. Yes, I need 2 sockets. I couldn't duplicate one. */
2483         if ((rs = socket(PF_INET, SOCK_DGRAM, udp->p_proto)) < 0)
2484                 report_file_error("error creating socket", list1(name));
2485         if ((ws = socket(PF_INET, SOCK_DGRAM, udp->p_proto)) < 0) {
2486                 close(rs);
2487                 report_file_error("error creating socket", list1(name));
2488         }
2489
2490         /* This will be used for both sockets */
2491         memset(&sa, 0, sizeof(sa));
2492         sa.sin_family = AF_INET;
2493         sa.sin_port = theport;
2494         sa.sin_addr.s_addr = inet_addr((char *)XSTRING_DATA(dest));
2495
2496         /* Socket configuration for reading ------------------------ */
2497
2498         /* Multiple connections from the same machine. This must be done before
2499            bind. If it fails, it shouldn't be fatal. The only consequence is that
2500            people won't be able to connect twice from the same machine. */
2501         if (setsockopt(rs, SOL_SOCKET, SO_REUSEADDR, (char *)&one, sizeof(one))
2502             < 0)
2503                 warn_when_safe(Qmulticast, Qwarning,
2504                                "Cannot reuse socket address");
2505
2506         /* bind socket name */
2507         if (bind(rs, (struct sockaddr *)&sa, sizeof(sa))) {
2508                 close(rs);
2509                 close(ws);
2510                 report_file_error("error binding socket", list2(name, port));
2511         }
2512
2513         /* join multicast group */
2514         imr.imr_multiaddr.s_addr = inet_addr((char *)XSTRING_DATA(dest));
2515         imr.imr_interface.s_addr = htonl(INADDR_ANY);
2516         if (setsockopt(rs, IPPROTO_IP, IP_ADD_MEMBERSHIP,
2517                        &imr, sizeof(struct ip_mreq)) < 0) {
2518                 close(ws);
2519                 close(rs);
2520                 report_file_error("error adding membership", list2(name, dest));
2521         }
2522
2523         /* Socket configuration for writing ----------------------- */
2524
2525         /* Normally, there's no 'connect' in multicast, since we prefer to use
2526            'sendto' and 'recvfrom'. However, in order to handle this connection
2527            in the process-like way it is done for TCP, we must be able to use
2528            'write' instead of 'sendto'. Consequently, we 'connect' this
2529            socket. */
2530
2531         /* See open-network-stream-internal for comments on this part of the
2532            code */
2533         slow_down_interrupts();
2534
2535       loop:
2536
2537         /* A system call interrupted with a SIGALRM or SIGIO comes back
2538            here, with can_break_system_calls reset to 0. */
2539         SETJMP(break_system_call_jump);
2540         if (QUITP) {
2541                 speed_up_interrupts();
2542                 REALLY_QUIT;
2543                 /* In case something really weird happens ... */
2544                 slow_down_interrupts();
2545         }
2546
2547         /* Break out of connect with a signal (it isn't otherwise possible).
2548            Thus you don't get screwed with a hung network. */
2549         can_break_system_calls = 1;
2550         ret = connect(ws, (struct sockaddr *)&sa, sizeof(sa));
2551         can_break_system_calls = 0;
2552         if (ret == -1 && errno != EISCONN) {
2553                 int xerrno = errno;
2554
2555                 if (errno == EINTR)
2556                         goto loop;
2557                 if (errno == EADDRINUSE && retry < 20) {
2558                         /* A delay here is needed on some FreeBSD systems, and
2559                            it is harmless, since this retrying takes time anyway
2560                            and should be infrequent.  `sleep-for' allowed for
2561                            quitting this loop with interrupts slowed down so it
2562                            can't be used here.  Async timers should already be
2563                            disabled at this point so we can use `sleep'. */
2564                         retry++;
2565                         goto loop;
2566                 }
2567
2568                 close(rs);
2569                 close(ws);
2570                 speed_up_interrupts();
2571
2572                 errno = xerrno;
2573                 report_file_error("error connecting socket", list2(name, port));
2574         }
2575
2576         speed_up_interrupts();
2577
2578         /* scope */
2579         if (setsockopt(ws, IPPROTO_IP, IP_MULTICAST_TTL,
2580                        &thettl, sizeof(thettl)) < 0) {
2581                 close(rs);
2582                 close(ws);
2583                 report_file_error("error setting ttl", list2(name, ttl));
2584         }
2585
2586         set_socket_nonblocking_maybe(rs, theport, "udp");
2587
2588         *vinfd = (void *)rs;
2589         *voutfd = (void *)ws;
2590 }
2591
2592 #endif                          /* HAVE_MULTICAST */
2593
2594 #endif                          /* HAVE_SOCKETS */
2595 \f
2596 /**********************************************************************/
2597 /*                            Initialization                          */
2598 /**********************************************************************/
2599
2600 void process_type_create_unix(void)
2601 {
2602         PROCESS_HAS_METHOD(unix, alloc_process_data);
2603         PROCESS_HAS_METHOD(unix, mark_process_data);
2604 #ifdef SIGCHLD
2605         PROCESS_HAS_METHOD(unix, init_process);
2606         PROCESS_HAS_METHOD(unix, reap_exited_processes);
2607 #endif
2608         PROCESS_HAS_METHOD(unix, init_process_io_handles);
2609         PROCESS_HAS_METHOD(unix, create_process);
2610         PROCESS_HAS_METHOD(unix, set_window_size);
2611 #ifdef HAVE_WAITPID
2612         PROCESS_HAS_METHOD(unix, update_status_if_terminated);
2613 #endif
2614         PROCESS_HAS_METHOD(unix, send_process);
2615         PROCESS_HAS_METHOD(unix, process_send_eof);
2616         PROCESS_HAS_METHOD(unix, deactivate_process);
2617         PROCESS_HAS_METHOD(unix, kill_child_process);
2618         PROCESS_HAS_METHOD(unix, kill_process_by_pid);
2619         PROCESS_HAS_METHOD(unix, get_tty_name);
2620 #ifdef HAVE_SOCKETS
2621         PROCESS_HAS_METHOD(unix, canonicalize_host_name);
2622         PROCESS_HAS_METHOD(unix, open_network_stream);
2623         PROCESS_HAS_METHOD(unix, open_network_server_stream);
2624         PROCESS_HAS_METHOD(unix, network_server_accept);
2625         PROCESS_HAS_METHOD(unix, network_process_listener);
2626 #ifdef HAVE_MULTICAST
2627         PROCESS_HAS_METHOD(unix, open_multicast_group);
2628 #endif
2629 #endif
2630 }
2631
2632 void vars_of_process_unix(void)
2633 {
2634         Fprovide(intern("unix-processes"));
2635 }
2636
2637 #endif                          /* !defined (NO_SUBPROCESSES) */