Convert most remaining function definitions to standard C.
[bpt/emacs.git] / src / process.c
1 /* Asynchronous subprocess control for GNU Emacs.
2 Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994, 1995,
3 1996, 1998, 1999, 2001, 2002, 2003, 2004,
4 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
5
6 This file is part of GNU Emacs.
7
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20
21
22 #include <config.h>
23 #include <signal.h>
24
25 /* This file is split into two parts by the following preprocessor
26 conditional. The 'then' clause contains all of the support for
27 asynchronous subprocesses. The 'else' clause contains stub
28 versions of some of the asynchronous subprocess routines that are
29 often called elsewhere in Emacs, so we don't have to #ifdef the
30 sections that call them. */
31
32 \f
33 #ifdef subprocesses
34
35 #include <stdio.h>
36 #include <errno.h>
37 #include <setjmp.h>
38 #include <sys/types.h> /* some typedefs are used in sys/file.h */
39 #include <sys/file.h>
40 #include <sys/stat.h>
41 #include <setjmp.h>
42 #ifdef HAVE_INTTYPES_H
43 #include <inttypes.h>
44 #endif
45 #ifdef STDC_HEADERS
46 #include <stdlib.h>
47 #endif
48
49 #ifdef HAVE_UNISTD_H
50 #include <unistd.h>
51 #endif
52 #include <fcntl.h>
53
54 #ifdef HAVE_SOCKETS /* TCP connection support, if kernel can do it */
55 #include <sys/socket.h>
56 #include <netdb.h>
57 #include <netinet/in.h>
58 #include <arpa/inet.h>
59
60 /* Are local (unix) sockets supported? */
61 #if defined (HAVE_SYS_UN_H)
62 #if !defined (AF_LOCAL) && defined (AF_UNIX)
63 #define AF_LOCAL AF_UNIX
64 #endif
65 #ifdef AF_LOCAL
66 #define HAVE_LOCAL_SOCKETS
67 #include <sys/un.h>
68 #endif
69 #endif
70 #endif /* HAVE_SOCKETS */
71
72 #if defined(HAVE_SYS_IOCTL_H)
73 #include <sys/ioctl.h>
74 #if !defined (O_NDELAY) && defined (HAVE_PTYS) && !defined(USG5)
75 #include <fcntl.h>
76 #endif /* HAVE_PTYS and no O_NDELAY */
77 #endif /* HAVE_SYS_IOCTL_H */
78
79 #ifdef NEED_BSDTTY
80 #include <bsdtty.h>
81 #endif
82
83 /* Can we use SIOCGIFCONF and/or SIOCGIFADDR */
84 #ifdef HAVE_SOCKETS
85 #if defined(HAVE_SYS_IOCTL_H) && defined(HAVE_NET_IF_H)
86 /* sys/ioctl.h may have been included already */
87 #ifndef SIOCGIFADDR
88 #include <sys/ioctl.h>
89 #endif
90 #include <net/if.h>
91 #endif
92 #endif
93
94 #ifdef HAVE_SYS_WAIT
95 #include <sys/wait.h>
96 #endif
97
98 #ifdef HAVE_RES_INIT
99 #include <netinet/in.h>
100 #include <arpa/nameser.h>
101 #include <resolv.h>
102 #endif
103
104 #include "lisp.h"
105 #include "systime.h"
106 #include "systty.h"
107
108 #include "window.h"
109 #include "buffer.h"
110 #include "character.h"
111 #include "coding.h"
112 #include "process.h"
113 #include "frame.h"
114 #include "termhooks.h"
115 #include "termopts.h"
116 #include "commands.h"
117 #include "keyboard.h"
118 #include "blockinput.h"
119 #include "dispextern.h"
120 #include "composite.h"
121 #include "atimer.h"
122
123 #if defined (USE_GTK) || defined (HAVE_GCONF)
124 #include "xgselect.h"
125 #endif /* defined (USE_GTK) || defined (HAVE_GCONF) */
126
127 Lisp_Object Qprocessp;
128 Lisp_Object Qrun, Qstop, Qsignal;
129 Lisp_Object Qopen, Qclosed, Qconnect, Qfailed, Qlisten;
130 Lisp_Object Qlocal, Qipv4, Qdatagram, Qseqpacket;
131 Lisp_Object Qreal, Qnetwork, Qserial;
132 #ifdef AF_INET6
133 Lisp_Object Qipv6;
134 #endif
135 Lisp_Object QCport, QCspeed, QCprocess;
136 Lisp_Object QCbytesize, QCstopbits, QCparity, Qodd, Qeven;
137 Lisp_Object QCflowcontrol, Qhw, Qsw, QCsummary;
138 Lisp_Object QCname, QCbuffer, QChost, QCservice, QCtype;
139 Lisp_Object QClocal, QCremote, QCcoding;
140 Lisp_Object QCserver, QCnowait, QCnoquery, QCstop;
141 Lisp_Object QCsentinel, QClog, QCoptions, QCplist;
142 Lisp_Object Qlast_nonmenu_event;
143 /* QCfamily is declared and initialized in xfaces.c,
144 QCfilter in keyboard.c. */
145 extern Lisp_Object QCfamily, QCfilter;
146
147 /* Qexit is declared and initialized in eval.c. */
148
149 /* QCfamily is defined in xfaces.c. */
150 extern Lisp_Object QCfamily;
151 /* QCfilter is defined in keyboard.c. */
152 extern Lisp_Object QCfilter;
153
154 Lisp_Object Qeuid, Qegid, Qcomm, Qstate, Qppid, Qpgrp, Qsess, Qttname, Qtpgid;
155 Lisp_Object Qminflt, Qmajflt, Qcminflt, Qcmajflt, Qutime, Qstime, Qcstime;
156 Lisp_Object Qcutime, Qpri, Qnice, Qthcount, Qstart, Qvsize, Qrss, Qargs;
157 Lisp_Object Quser, Qgroup, Qetime, Qpcpu, Qpmem, Qtime, Qctime;
158
159 #ifdef HAVE_SOCKETS
160 #define NETCONN_P(p) (EQ (XPROCESS (p)->type, Qnetwork))
161 #define NETCONN1_P(p) (EQ ((p)->type, Qnetwork))
162 #define SERIALCONN_P(p) (EQ (XPROCESS (p)->type, Qserial))
163 #define SERIALCONN1_P(p) (EQ ((p)->type, Qserial))
164 #else
165 #define NETCONN_P(p) 0
166 #define NETCONN1_P(p) 0
167 #define SERIALCONN_P(p) 0
168 #define SERIALCONN1_P(p) 0
169 #endif /* HAVE_SOCKETS */
170
171 /* Define first descriptor number available for subprocesses. */
172 #define FIRST_PROC_DESC 3
173
174 /* Define SIGCHLD as an alias for SIGCLD. There are many conditionals
175 testing SIGCHLD. */
176
177 #if !defined (SIGCHLD) && defined (SIGCLD)
178 #define SIGCHLD SIGCLD
179 #endif /* SIGCLD */
180
181 #include "syssignal.h"
182
183 #include "syswait.h"
184
185 extern char *get_operating_system_release ();
186
187 /* Serial processes require termios or Windows. */
188 #if defined (HAVE_TERMIOS) || defined (WINDOWSNT)
189 #define HAVE_SERIAL
190 #endif
191
192 #ifdef HAVE_SERIAL
193 /* From sysdep.c or w32.c */
194 extern int serial_open (char *port);
195 extern void serial_configure (struct Lisp_Process *p, Lisp_Object contact);
196 #endif
197
198 #ifndef HAVE_H_ERRNO
199 extern int h_errno;
200 #endif
201
202 /* t means use pty, nil means use a pipe,
203 maybe other values to come. */
204 static Lisp_Object Vprocess_connection_type;
205
206 /* These next two vars are non-static since sysdep.c uses them in the
207 emulation of `select'. */
208 /* Number of events of change of status of a process. */
209 int process_tick;
210 /* Number of events for which the user or sentinel has been notified. */
211 int update_tick;
212
213 /* Define NON_BLOCKING_CONNECT if we can support non-blocking connects. */
214
215 #ifdef BROKEN_NON_BLOCKING_CONNECT
216 #undef NON_BLOCKING_CONNECT
217 #else
218 #ifndef NON_BLOCKING_CONNECT
219 #ifdef HAVE_SOCKETS
220 #ifdef HAVE_SELECT
221 #if defined (HAVE_GETPEERNAME) || defined (GNU_LINUX)
222 #if defined (O_NONBLOCK) || defined (O_NDELAY)
223 #if defined (EWOULDBLOCK) || defined (EINPROGRESS)
224 #define NON_BLOCKING_CONNECT
225 #endif /* EWOULDBLOCK || EINPROGRESS */
226 #endif /* O_NONBLOCK || O_NDELAY */
227 #endif /* HAVE_GETPEERNAME || GNU_LINUX */
228 #endif /* HAVE_SELECT */
229 #endif /* HAVE_SOCKETS */
230 #endif /* NON_BLOCKING_CONNECT */
231 #endif /* BROKEN_NON_BLOCKING_CONNECT */
232
233 /* Define DATAGRAM_SOCKETS if datagrams can be used safely on
234 this system. We need to read full packets, so we need a
235 "non-destructive" select. So we require either native select,
236 or emulation of select using FIONREAD. */
237
238 #ifdef BROKEN_DATAGRAM_SOCKETS
239 #undef DATAGRAM_SOCKETS
240 #else
241 #ifndef DATAGRAM_SOCKETS
242 #ifdef HAVE_SOCKETS
243 #if defined (HAVE_SELECT) || defined (FIONREAD)
244 #if defined (HAVE_SENDTO) && defined (HAVE_RECVFROM) && defined (EMSGSIZE)
245 #define DATAGRAM_SOCKETS
246 #endif /* HAVE_SENDTO && HAVE_RECVFROM && EMSGSIZE */
247 #endif /* HAVE_SELECT || FIONREAD */
248 #endif /* HAVE_SOCKETS */
249 #endif /* DATAGRAM_SOCKETS */
250 #endif /* BROKEN_DATAGRAM_SOCKETS */
251
252 #if defined HAVE_LOCAL_SOCKETS && defined DATAGRAM_SOCKETS
253 # define HAVE_SEQPACKET
254 #endif
255
256 #if !defined (ADAPTIVE_READ_BUFFERING) && !defined (NO_ADAPTIVE_READ_BUFFERING)
257 #ifdef EMACS_HAS_USECS
258 #define ADAPTIVE_READ_BUFFERING
259 #endif
260 #endif
261
262 #ifdef ADAPTIVE_READ_BUFFERING
263 #define READ_OUTPUT_DELAY_INCREMENT 10000
264 #define READ_OUTPUT_DELAY_MAX (READ_OUTPUT_DELAY_INCREMENT * 5)
265 #define READ_OUTPUT_DELAY_MAX_MAX (READ_OUTPUT_DELAY_INCREMENT * 7)
266
267 /* Number of processes which have a non-zero read_output_delay,
268 and therefore might be delayed for adaptive read buffering. */
269
270 static int process_output_delay_count;
271
272 /* Non-zero if any process has non-nil read_output_skip. */
273
274 static int process_output_skip;
275
276 /* Non-nil means to delay reading process output to improve buffering.
277 A value of t means that delay is reset after each send, any other
278 non-nil value does not reset the delay. A value of nil disables
279 adaptive read buffering completely. */
280 static Lisp_Object Vprocess_adaptive_read_buffering;
281 #else
282 #define process_output_delay_count 0
283 #endif
284
285
286 #include "sysselect.h"
287
288 static int keyboard_bit_set (SELECT_TYPE *);
289 static void deactivate_process (Lisp_Object);
290 static void status_notify (struct Lisp_Process *);
291 static int read_process_output (Lisp_Object, int);
292 static void create_pty (Lisp_Object);
293
294 /* If we support a window system, turn on the code to poll periodically
295 to detect C-g. It isn't actually used when doing interrupt input. */
296 #ifdef HAVE_WINDOW_SYSTEM
297 #define POLL_FOR_INPUT
298 #endif
299
300 static Lisp_Object get_process (register Lisp_Object name);
301 static void exec_sentinel (Lisp_Object proc, Lisp_Object reason);
302
303 extern int timers_run;
304 \f
305 /* Mask of bits indicating the descriptors that we wait for input on. */
306
307 static SELECT_TYPE input_wait_mask;
308
309 /* Mask that excludes keyboard input descriptor(s). */
310
311 static SELECT_TYPE non_keyboard_wait_mask;
312
313 /* Mask that excludes process input descriptor(s). */
314
315 static SELECT_TYPE non_process_wait_mask;
316
317 /* Mask for the gpm mouse input descriptor. */
318
319 static SELECT_TYPE gpm_wait_mask;
320
321 #ifdef NON_BLOCKING_CONNECT
322 /* Mask of bits indicating the descriptors that we wait for connect to
323 complete on. Once they complete, they are removed from this mask
324 and added to the input_wait_mask and non_keyboard_wait_mask. */
325
326 static SELECT_TYPE connect_wait_mask;
327
328 /* Number of bits set in connect_wait_mask. */
329 static int num_pending_connects;
330
331 #define IF_NON_BLOCKING_CONNECT(s) s
332 #else
333 #define IF_NON_BLOCKING_CONNECT(s)
334 #endif
335
336 /* The largest descriptor currently in use for a process object. */
337 static int max_process_desc;
338
339 /* The largest descriptor currently in use for keyboard input. */
340 static int max_keyboard_desc;
341
342 /* The largest descriptor currently in use for gpm mouse input. */
343 static int max_gpm_desc;
344
345 /* Nonzero means delete a process right away if it exits. */
346 static int delete_exited_processes;
347
348 /* Indexed by descriptor, gives the process (if any) for that descriptor */
349 Lisp_Object chan_process[MAXDESC];
350
351 /* Alist of elements (NAME . PROCESS) */
352 Lisp_Object Vprocess_alist;
353
354 /* Buffered-ahead input char from process, indexed by channel.
355 -1 means empty (no char is buffered).
356 Used on sys V where the only way to tell if there is any
357 output from the process is to read at least one char.
358 Always -1 on systems that support FIONREAD. */
359
360 /* Don't make static; need to access externally. */
361 int proc_buffered_char[MAXDESC];
362
363 /* Table of `struct coding-system' for each process. */
364 static struct coding_system *proc_decode_coding_system[MAXDESC];
365 static struct coding_system *proc_encode_coding_system[MAXDESC];
366
367 #ifdef DATAGRAM_SOCKETS
368 /* Table of `partner address' for datagram sockets. */
369 struct sockaddr_and_len {
370 struct sockaddr *sa;
371 int len;
372 } datagram_address[MAXDESC];
373 #define DATAGRAM_CHAN_P(chan) (datagram_address[chan].sa != 0)
374 #define DATAGRAM_CONN_P(proc) (PROCESSP (proc) && datagram_address[XPROCESS (proc)->infd].sa != 0)
375 #else
376 #define DATAGRAM_CHAN_P(chan) (0)
377 #define DATAGRAM_CONN_P(proc) (0)
378 #endif
379
380 /* Maximum number of bytes to send to a pty without an eof. */
381 static int pty_max_bytes;
382
383 /* Nonzero means don't run process sentinels. This is used
384 when exiting. */
385 int inhibit_sentinels;
386
387 #ifdef HAVE_PTYS
388 #ifdef HAVE_PTY_H
389 #include <pty.h>
390 #endif
391 /* The file name of the pty opened by allocate_pty. */
392
393 static char pty_name[24];
394 #endif
395 \f
396 /* Compute the Lisp form of the process status, p->status, from
397 the numeric status that was returned by `wait'. */
398
399 static Lisp_Object status_convert (int);
400
401 static void
402 update_status (struct Lisp_Process *p)
403 {
404 eassert (p->raw_status_new);
405 p->status = status_convert (p->raw_status);
406 p->raw_status_new = 0;
407 }
408
409 /* Convert a process status word in Unix format to
410 the list that we use internally. */
411
412 static Lisp_Object
413 status_convert (int w)
414 {
415 if (WIFSTOPPED (w))
416 return Fcons (Qstop, Fcons (make_number (WSTOPSIG (w)), Qnil));
417 else if (WIFEXITED (w))
418 return Fcons (Qexit, Fcons (make_number (WRETCODE (w)),
419 WCOREDUMP (w) ? Qt : Qnil));
420 else if (WIFSIGNALED (w))
421 return Fcons (Qsignal, Fcons (make_number (WTERMSIG (w)),
422 WCOREDUMP (w) ? Qt : Qnil));
423 else
424 return Qrun;
425 }
426
427 /* Given a status-list, extract the three pieces of information
428 and store them individually through the three pointers. */
429
430 static void
431 decode_status (Lisp_Object l, Lisp_Object *symbol, int *code, int *coredump)
432 {
433 Lisp_Object tem;
434
435 if (SYMBOLP (l))
436 {
437 *symbol = l;
438 *code = 0;
439 *coredump = 0;
440 }
441 else
442 {
443 *symbol = XCAR (l);
444 tem = XCDR (l);
445 *code = XFASTINT (XCAR (tem));
446 tem = XCDR (tem);
447 *coredump = !NILP (tem);
448 }
449 }
450
451 /* Return a string describing a process status list. */
452
453 static Lisp_Object
454 status_message (struct Lisp_Process *p)
455 {
456 Lisp_Object status = p->status;
457 Lisp_Object symbol;
458 int code, coredump;
459 Lisp_Object string, string2;
460
461 decode_status (status, &symbol, &code, &coredump);
462
463 if (EQ (symbol, Qsignal) || EQ (symbol, Qstop))
464 {
465 char *signame;
466 synchronize_system_messages_locale ();
467 signame = strsignal (code);
468 if (signame == 0)
469 string = build_string ("unknown");
470 else
471 {
472 int c1, c2;
473
474 string = make_unibyte_string (signame, strlen (signame));
475 if (! NILP (Vlocale_coding_system))
476 string = (code_convert_string_norecord
477 (string, Vlocale_coding_system, 0));
478 c1 = STRING_CHAR ((char *) SDATA (string));
479 c2 = DOWNCASE (c1);
480 if (c1 != c2)
481 Faset (string, make_number (0), make_number (c2));
482 }
483 string2 = build_string (coredump ? " (core dumped)\n" : "\n");
484 return concat2 (string, string2);
485 }
486 else if (EQ (symbol, Qexit))
487 {
488 if (NETCONN1_P (p))
489 return build_string (code == 0 ? "deleted\n" : "connection broken by remote peer\n");
490 if (code == 0)
491 return build_string ("finished\n");
492 string = Fnumber_to_string (make_number (code));
493 string2 = build_string (coredump ? " (core dumped)\n" : "\n");
494 return concat3 (build_string ("exited abnormally with code "),
495 string, string2);
496 }
497 else if (EQ (symbol, Qfailed))
498 {
499 string = Fnumber_to_string (make_number (code));
500 string2 = build_string ("\n");
501 return concat3 (build_string ("failed with code "),
502 string, string2);
503 }
504 else
505 return Fcopy_sequence (Fsymbol_name (symbol));
506 }
507 \f
508 #ifdef HAVE_PTYS
509
510 /* Open an available pty, returning a file descriptor.
511 Return -1 on failure.
512 The file name of the terminal corresponding to the pty
513 is left in the variable pty_name. */
514
515 static int
516 allocate_pty (void)
517 {
518 register int c, i;
519 int fd;
520
521 #ifdef PTY_ITERATION
522 PTY_ITERATION
523 #else
524 for (c = FIRST_PTY_LETTER; c <= 'z'; c++)
525 for (i = 0; i < 16; i++)
526 #endif
527 {
528 struct stat stb; /* Used in some PTY_OPEN. */
529 #ifdef PTY_NAME_SPRINTF
530 PTY_NAME_SPRINTF
531 #else
532 sprintf (pty_name, "/dev/pty%c%x", c, i);
533 #endif /* no PTY_NAME_SPRINTF */
534
535 #ifdef PTY_OPEN
536 PTY_OPEN;
537 #else /* no PTY_OPEN */
538 {
539 { /* Some systems name their pseudoterminals so that there are gaps in
540 the usual sequence - for example, on HP9000/S700 systems, there
541 are no pseudoterminals with names ending in 'f'. So we wait for
542 three failures in a row before deciding that we've reached the
543 end of the ptys. */
544 int failed_count = 0;
545
546 if (stat (pty_name, &stb) < 0)
547 {
548 failed_count++;
549 if (failed_count >= 3)
550 return -1;
551 }
552 else
553 failed_count = 0;
554 }
555 # ifdef O_NONBLOCK
556 fd = emacs_open (pty_name, O_RDWR | O_NONBLOCK, 0);
557 # else
558 fd = emacs_open (pty_name, O_RDWR | O_NDELAY, 0);
559 # endif
560 }
561 #endif /* no PTY_OPEN */
562
563 if (fd >= 0)
564 {
565 /* check to make certain that both sides are available
566 this avoids a nasty yet stupid bug in rlogins */
567 #ifdef PTY_TTY_NAME_SPRINTF
568 PTY_TTY_NAME_SPRINTF
569 #else
570 sprintf (pty_name, "/dev/tty%c%x", c, i);
571 #endif /* no PTY_TTY_NAME_SPRINTF */
572 if (access (pty_name, 6) != 0)
573 {
574 emacs_close (fd);
575 # ifndef __sgi
576 continue;
577 # else
578 return -1;
579 # endif /* __sgi */
580 }
581 setup_pty (fd);
582 return fd;
583 }
584 }
585 return -1;
586 }
587 #endif /* HAVE_PTYS */
588 \f
589 static Lisp_Object
590 make_process (Lisp_Object name)
591 {
592 register Lisp_Object val, tem, name1;
593 register struct Lisp_Process *p;
594 char suffix[10];
595 register int i;
596
597 p = allocate_process ();
598
599 p->infd = -1;
600 p->outfd = -1;
601 p->tick = 0;
602 p->update_tick = 0;
603 p->pid = 0;
604 p->pty_flag = 0;
605 p->raw_status_new = 0;
606 p->status = Qrun;
607 p->mark = Fmake_marker ();
608 p->kill_without_query = 0;
609
610 #ifdef ADAPTIVE_READ_BUFFERING
611 p->adaptive_read_buffering = 0;
612 p->read_output_delay = 0;
613 p->read_output_skip = 0;
614 #endif
615
616 /* If name is already in use, modify it until it is unused. */
617
618 name1 = name;
619 for (i = 1; ; i++)
620 {
621 tem = Fget_process (name1);
622 if (NILP (tem)) break;
623 sprintf (suffix, "<%d>", i);
624 name1 = concat2 (name, build_string (suffix));
625 }
626 name = name1;
627 p->name = name;
628 XSETPROCESS (val, p);
629 Vprocess_alist = Fcons (Fcons (name, val), Vprocess_alist);
630 return val;
631 }
632
633 static void
634 remove_process (register Lisp_Object proc)
635 {
636 register Lisp_Object pair;
637
638 pair = Frassq (proc, Vprocess_alist);
639 Vprocess_alist = Fdelq (pair, Vprocess_alist);
640
641 deactivate_process (proc);
642 }
643
644 /* Setup coding systems of PROCESS. */
645
646 void
647 setup_process_coding_systems (Lisp_Object process)
648 {
649 struct Lisp_Process *p = XPROCESS (process);
650 int inch = p->infd;
651 int outch = p->outfd;
652 Lisp_Object coding_system;
653
654 if (inch < 0 || outch < 0)
655 return;
656
657 if (!proc_decode_coding_system[inch])
658 proc_decode_coding_system[inch]
659 = (struct coding_system *) xmalloc (sizeof (struct coding_system));
660 coding_system = p->decode_coding_system;
661 if (! NILP (p->filter))
662 ;
663 else if (BUFFERP (p->buffer))
664 {
665 if (NILP (XBUFFER (p->buffer)->enable_multibyte_characters))
666 coding_system = raw_text_coding_system (coding_system);
667 }
668 setup_coding_system (coding_system, proc_decode_coding_system[inch]);
669
670 if (!proc_encode_coding_system[outch])
671 proc_encode_coding_system[outch]
672 = (struct coding_system *) xmalloc (sizeof (struct coding_system));
673 setup_coding_system (p->encode_coding_system,
674 proc_encode_coding_system[outch]);
675 }
676 \f
677 DEFUN ("processp", Fprocessp, Sprocessp, 1, 1, 0,
678 doc: /* Return t if OBJECT is a process. */)
679 (object)
680 Lisp_Object object;
681 {
682 return PROCESSP (object) ? Qt : Qnil;
683 }
684
685 DEFUN ("get-process", Fget_process, Sget_process, 1, 1, 0,
686 doc: /* Return the process named NAME, or nil if there is none. */)
687 (name)
688 register Lisp_Object name;
689 {
690 if (PROCESSP (name))
691 return name;
692 CHECK_STRING (name);
693 return Fcdr (Fassoc (name, Vprocess_alist));
694 }
695
696 DEFUN ("get-buffer-process", Fget_buffer_process, Sget_buffer_process, 1, 1, 0,
697 doc: /* Return the (or a) process associated with BUFFER.
698 BUFFER may be a buffer or the name of one. */)
699 (buffer)
700 register Lisp_Object buffer;
701 {
702 register Lisp_Object buf, tail, proc;
703
704 if (NILP (buffer)) return Qnil;
705 buf = Fget_buffer (buffer);
706 if (NILP (buf)) return Qnil;
707
708 for (tail = Vprocess_alist; CONSP (tail); tail = XCDR (tail))
709 {
710 proc = Fcdr (XCAR (tail));
711 if (PROCESSP (proc) && EQ (XPROCESS (proc)->buffer, buf))
712 return proc;
713 }
714 return Qnil;
715 }
716
717 /* This is how commands for the user decode process arguments. It
718 accepts a process, a process name, a buffer, a buffer name, or nil.
719 Buffers denote the first process in the buffer, and nil denotes the
720 current buffer. */
721
722 static Lisp_Object
723 get_process (register Lisp_Object name)
724 {
725 register Lisp_Object proc, obj;
726 if (STRINGP (name))
727 {
728 obj = Fget_process (name);
729 if (NILP (obj))
730 obj = Fget_buffer (name);
731 if (NILP (obj))
732 error ("Process %s does not exist", SDATA (name));
733 }
734 else if (NILP (name))
735 obj = Fcurrent_buffer ();
736 else
737 obj = name;
738
739 /* Now obj should be either a buffer object or a process object.
740 */
741 if (BUFFERP (obj))
742 {
743 proc = Fget_buffer_process (obj);
744 if (NILP (proc))
745 error ("Buffer %s has no process", SDATA (XBUFFER (obj)->name));
746 }
747 else
748 {
749 CHECK_PROCESS (obj);
750 proc = obj;
751 }
752 return proc;
753 }
754
755
756 #ifdef SIGCHLD
757 /* Fdelete_process promises to immediately forget about the process, but in
758 reality, Emacs needs to remember those processes until they have been
759 treated by sigchld_handler; otherwise this handler would consider the
760 process as being synchronous and say that the synchronous process is
761 dead. */
762 static Lisp_Object deleted_pid_list;
763 #endif
764
765 DEFUN ("delete-process", Fdelete_process, Sdelete_process, 1, 1, 0,
766 doc: /* Delete PROCESS: kill it and forget about it immediately.
767 PROCESS may be a process, a buffer, the name of a process or buffer, or
768 nil, indicating the current buffer's process. */)
769 (process)
770 register Lisp_Object process;
771 {
772 register struct Lisp_Process *p;
773
774 process = get_process (process);
775 p = XPROCESS (process);
776
777 p->raw_status_new = 0;
778 if (NETCONN1_P (p) || SERIALCONN1_P (p))
779 {
780 p->status = Fcons (Qexit, Fcons (make_number (0), Qnil));
781 p->tick = ++process_tick;
782 status_notify (p);
783 redisplay_preserve_echo_area (13);
784 }
785 else if (p->infd >= 0)
786 {
787 #ifdef SIGCHLD
788 Lisp_Object symbol;
789 /* Assignment to EMACS_INT stops GCC whining about limited range
790 of data type. */
791 EMACS_INT pid = p->pid;
792
793 /* No problem storing the pid here, as it is still in Vprocess_alist. */
794 deleted_pid_list = Fcons (make_fixnum_or_float (pid),
795 /* GC treated elements set to nil. */
796 Fdelq (Qnil, deleted_pid_list));
797 /* If the process has already signaled, remove it from the list. */
798 if (p->raw_status_new)
799 update_status (p);
800 symbol = p->status;
801 if (CONSP (p->status))
802 symbol = XCAR (p->status);
803 if (EQ (symbol, Qsignal) || EQ (symbol, Qexit))
804 deleted_pid_list
805 = Fdelete (make_fixnum_or_float (pid), deleted_pid_list);
806 else
807 #endif
808 {
809 Fkill_process (process, Qnil);
810 /* Do this now, since remove_process will make sigchld_handler do nothing. */
811 p->status
812 = Fcons (Qsignal, Fcons (make_number (SIGKILL), Qnil));
813 p->tick = ++process_tick;
814 status_notify (p);
815 redisplay_preserve_echo_area (13);
816 }
817 }
818 remove_process (process);
819 return Qnil;
820 }
821 \f
822 DEFUN ("process-status", Fprocess_status, Sprocess_status, 1, 1, 0,
823 doc: /* Return the status of PROCESS.
824 The returned value is one of the following symbols:
825 run -- for a process that is running.
826 stop -- for a process stopped but continuable.
827 exit -- for a process that has exited.
828 signal -- for a process that has got a fatal signal.
829 open -- for a network stream connection that is open.
830 listen -- for a network stream server that is listening.
831 closed -- for a network stream connection that is closed.
832 connect -- when waiting for a non-blocking connection to complete.
833 failed -- when a non-blocking connection has failed.
834 nil -- if arg is a process name and no such process exists.
835 PROCESS may be a process, a buffer, the name of a process, or
836 nil, indicating the current buffer's process. */)
837 (process)
838 register Lisp_Object process;
839 {
840 register struct Lisp_Process *p;
841 register Lisp_Object status;
842
843 if (STRINGP (process))
844 process = Fget_process (process);
845 else
846 process = get_process (process);
847
848 if (NILP (process))
849 return process;
850
851 p = XPROCESS (process);
852 if (p->raw_status_new)
853 update_status (p);
854 status = p->status;
855 if (CONSP (status))
856 status = XCAR (status);
857 if (NETCONN1_P (p) || SERIALCONN1_P (p))
858 {
859 if (EQ (status, Qexit))
860 status = Qclosed;
861 else if (EQ (p->command, Qt))
862 status = Qstop;
863 else if (EQ (status, Qrun))
864 status = Qopen;
865 }
866 return status;
867 }
868
869 DEFUN ("process-exit-status", Fprocess_exit_status, Sprocess_exit_status,
870 1, 1, 0,
871 doc: /* Return the exit status of PROCESS or the signal number that killed it.
872 If PROCESS has not yet exited or died, return 0. */)
873 (process)
874 register Lisp_Object process;
875 {
876 CHECK_PROCESS (process);
877 if (XPROCESS (process)->raw_status_new)
878 update_status (XPROCESS (process));
879 if (CONSP (XPROCESS (process)->status))
880 return XCAR (XCDR (XPROCESS (process)->status));
881 return make_number (0);
882 }
883
884 DEFUN ("process-id", Fprocess_id, Sprocess_id, 1, 1, 0,
885 doc: /* Return the process id of PROCESS.
886 This is the pid of the external process which PROCESS uses or talks to.
887 For a network connection, this value is nil. */)
888 (process)
889 register Lisp_Object process;
890 {
891 /* Assignment to EMACS_INT stops GCC whining about limited range of
892 data type. */
893 EMACS_INT pid;
894
895 CHECK_PROCESS (process);
896 pid = XPROCESS (process)->pid;
897 return (pid ? make_fixnum_or_float (pid) : Qnil);
898 }
899
900 DEFUN ("process-name", Fprocess_name, Sprocess_name, 1, 1, 0,
901 doc: /* Return the name of PROCESS, as a string.
902 This is the name of the program invoked in PROCESS,
903 possibly modified to make it unique among process names. */)
904 (process)
905 register Lisp_Object process;
906 {
907 CHECK_PROCESS (process);
908 return XPROCESS (process)->name;
909 }
910
911 DEFUN ("process-command", Fprocess_command, Sprocess_command, 1, 1, 0,
912 doc: /* Return the command that was executed to start PROCESS.
913 This is a list of strings, the first string being the program executed
914 and the rest of the strings being the arguments given to it.
915 For a network or serial process, this is nil (process is running) or t
916 \(process is stopped). */)
917 (process)
918 register Lisp_Object process;
919 {
920 CHECK_PROCESS (process);
921 return XPROCESS (process)->command;
922 }
923
924 DEFUN ("process-tty-name", Fprocess_tty_name, Sprocess_tty_name, 1, 1, 0,
925 doc: /* Return the name of the terminal PROCESS uses, or nil if none.
926 This is the terminal that the process itself reads and writes on,
927 not the name of the pty that Emacs uses to talk with that terminal. */)
928 (process)
929 register Lisp_Object process;
930 {
931 CHECK_PROCESS (process);
932 return XPROCESS (process)->tty_name;
933 }
934
935 DEFUN ("set-process-buffer", Fset_process_buffer, Sset_process_buffer,
936 2, 2, 0,
937 doc: /* Set buffer associated with PROCESS to BUFFER (a buffer, or nil). */)
938 (process, buffer)
939 register Lisp_Object process, buffer;
940 {
941 struct Lisp_Process *p;
942
943 CHECK_PROCESS (process);
944 if (!NILP (buffer))
945 CHECK_BUFFER (buffer);
946 p = XPROCESS (process);
947 p->buffer = buffer;
948 if (NETCONN1_P (p) || SERIALCONN1_P (p))
949 p->childp = Fplist_put (p->childp, QCbuffer, buffer);
950 setup_process_coding_systems (process);
951 return buffer;
952 }
953
954 DEFUN ("process-buffer", Fprocess_buffer, Sprocess_buffer,
955 1, 1, 0,
956 doc: /* Return the buffer PROCESS is associated with.
957 Output from PROCESS is inserted in this buffer unless PROCESS has a filter. */)
958 (process)
959 register Lisp_Object process;
960 {
961 CHECK_PROCESS (process);
962 return XPROCESS (process)->buffer;
963 }
964
965 DEFUN ("process-mark", Fprocess_mark, Sprocess_mark,
966 1, 1, 0,
967 doc: /* Return the marker for the end of the last output from PROCESS. */)
968 (process)
969 register Lisp_Object process;
970 {
971 CHECK_PROCESS (process);
972 return XPROCESS (process)->mark;
973 }
974
975 DEFUN ("set-process-filter", Fset_process_filter, Sset_process_filter,
976 2, 2, 0,
977 doc: /* Give PROCESS the filter function FILTER; nil means no filter.
978 A value of t means stop accepting output from the process.
979
980 When a process has a filter, its buffer is not used for output.
981 Instead, each time it does output, the entire string of output is
982 passed to the filter.
983
984 The filter gets two arguments: the process and the string of output.
985 The string argument is normally a multibyte string, except:
986 - if the process' input coding system is no-conversion or raw-text,
987 it is a unibyte string (the non-converted input), or else
988 - if `default-enable-multibyte-characters' is nil, it is a unibyte
989 string (the result of converting the decoded input multibyte
990 string to unibyte with `string-make-unibyte'). */)
991 (process, filter)
992 register Lisp_Object process, filter;
993 {
994 struct Lisp_Process *p;
995
996 CHECK_PROCESS (process);
997 p = XPROCESS (process);
998
999 /* Don't signal an error if the process' input file descriptor
1000 is closed. This could make debugging Lisp more difficult,
1001 for example when doing something like
1002
1003 (setq process (start-process ...))
1004 (debug)
1005 (set-process-filter process ...) */
1006
1007 if (p->infd >= 0)
1008 {
1009 if (EQ (filter, Qt) && !EQ (p->status, Qlisten))
1010 {
1011 FD_CLR (p->infd, &input_wait_mask);
1012 FD_CLR (p->infd, &non_keyboard_wait_mask);
1013 }
1014 else if (EQ (p->filter, Qt)
1015 /* Network or serial process not stopped: */
1016 && !EQ (p->command, Qt))
1017 {
1018 FD_SET (p->infd, &input_wait_mask);
1019 FD_SET (p->infd, &non_keyboard_wait_mask);
1020 }
1021 }
1022
1023 p->filter = filter;
1024 if (NETCONN1_P (p) || SERIALCONN1_P (p))
1025 p->childp = Fplist_put (p->childp, QCfilter, filter);
1026 setup_process_coding_systems (process);
1027 return filter;
1028 }
1029
1030 DEFUN ("process-filter", Fprocess_filter, Sprocess_filter,
1031 1, 1, 0,
1032 doc: /* Returns the filter function of PROCESS; nil if none.
1033 See `set-process-filter' for more info on filter functions. */)
1034 (process)
1035 register Lisp_Object process;
1036 {
1037 CHECK_PROCESS (process);
1038 return XPROCESS (process)->filter;
1039 }
1040
1041 DEFUN ("set-process-sentinel", Fset_process_sentinel, Sset_process_sentinel,
1042 2, 2, 0,
1043 doc: /* Give PROCESS the sentinel SENTINEL; nil for none.
1044 The sentinel is called as a function when the process changes state.
1045 It gets two arguments: the process, and a string describing the change. */)
1046 (process, sentinel)
1047 register Lisp_Object process, sentinel;
1048 {
1049 struct Lisp_Process *p;
1050
1051 CHECK_PROCESS (process);
1052 p = XPROCESS (process);
1053
1054 p->sentinel = sentinel;
1055 if (NETCONN1_P (p) || SERIALCONN1_P (p))
1056 p->childp = Fplist_put (p->childp, QCsentinel, sentinel);
1057 return sentinel;
1058 }
1059
1060 DEFUN ("process-sentinel", Fprocess_sentinel, Sprocess_sentinel,
1061 1, 1, 0,
1062 doc: /* Return the sentinel of PROCESS; nil if none.
1063 See `set-process-sentinel' for more info on sentinels. */)
1064 (process)
1065 register Lisp_Object process;
1066 {
1067 CHECK_PROCESS (process);
1068 return XPROCESS (process)->sentinel;
1069 }
1070
1071 DEFUN ("set-process-window-size", Fset_process_window_size,
1072 Sset_process_window_size, 3, 3, 0,
1073 doc: /* Tell PROCESS that it has logical window size HEIGHT and WIDTH. */)
1074 (process, height, width)
1075 register Lisp_Object process, height, width;
1076 {
1077 CHECK_PROCESS (process);
1078 CHECK_NATNUM (height);
1079 CHECK_NATNUM (width);
1080
1081 if (XPROCESS (process)->infd < 0
1082 || set_window_size (XPROCESS (process)->infd,
1083 XINT (height), XINT (width)) <= 0)
1084 return Qnil;
1085 else
1086 return Qt;
1087 }
1088
1089 DEFUN ("set-process-inherit-coding-system-flag",
1090 Fset_process_inherit_coding_system_flag,
1091 Sset_process_inherit_coding_system_flag, 2, 2, 0,
1092 doc: /* Determine whether buffer of PROCESS will inherit coding-system.
1093 If the second argument FLAG is non-nil, then the variable
1094 `buffer-file-coding-system' of the buffer associated with PROCESS
1095 will be bound to the value of the coding system used to decode
1096 the process output.
1097
1098 This is useful when the coding system specified for the process buffer
1099 leaves either the character code conversion or the end-of-line conversion
1100 unspecified, or if the coding system used to decode the process output
1101 is more appropriate for saving the process buffer.
1102
1103 Binding the variable `inherit-process-coding-system' to non-nil before
1104 starting the process is an alternative way of setting the inherit flag
1105 for the process which will run. */)
1106 (process, flag)
1107 register Lisp_Object process, flag;
1108 {
1109 CHECK_PROCESS (process);
1110 XPROCESS (process)->inherit_coding_system_flag = !NILP (flag);
1111 return flag;
1112 }
1113
1114 DEFUN ("process-inherit-coding-system-flag",
1115 Fprocess_inherit_coding_system_flag, Sprocess_inherit_coding_system_flag,
1116 1, 1, 0,
1117 doc: /* Return the value of inherit-coding-system flag for PROCESS.
1118 If this flag is t, `buffer-file-coding-system' of the buffer
1119 associated with PROCESS will inherit the coding system used to decode
1120 the process output. */)
1121 (process)
1122 register Lisp_Object process;
1123 {
1124 CHECK_PROCESS (process);
1125 return XPROCESS (process)->inherit_coding_system_flag ? Qt : Qnil;
1126 }
1127
1128 DEFUN ("set-process-query-on-exit-flag",
1129 Fset_process_query_on_exit_flag, Sset_process_query_on_exit_flag,
1130 2, 2, 0,
1131 doc: /* Specify if query is needed for PROCESS when Emacs is exited.
1132 If the second argument FLAG is non-nil, Emacs will query the user before
1133 exiting or killing a buffer if PROCESS is running. */)
1134 (process, flag)
1135 register Lisp_Object process, flag;
1136 {
1137 CHECK_PROCESS (process);
1138 XPROCESS (process)->kill_without_query = NILP (flag);
1139 return flag;
1140 }
1141
1142 DEFUN ("process-query-on-exit-flag",
1143 Fprocess_query_on_exit_flag, Sprocess_query_on_exit_flag,
1144 1, 1, 0,
1145 doc: /* Return the current value of query-on-exit flag for PROCESS. */)
1146 (process)
1147 register Lisp_Object process;
1148 {
1149 CHECK_PROCESS (process);
1150 return (XPROCESS (process)->kill_without_query ? Qnil : Qt);
1151 }
1152
1153 #ifdef DATAGRAM_SOCKETS
1154 Lisp_Object Fprocess_datagram_address (Lisp_Object process);
1155 #endif
1156
1157 DEFUN ("process-contact", Fprocess_contact, Sprocess_contact,
1158 1, 2, 0,
1159 doc: /* Return the contact info of PROCESS; t for a real child.
1160 For a network or serial connection, the value depends on the optional
1161 KEY arg. If KEY is nil, value is a cons cell of the form (HOST
1162 SERVICE) for a network connection or (PORT SPEED) for a serial
1163 connection. If KEY is t, the complete contact information for the
1164 connection is returned, else the specific value for the keyword KEY is
1165 returned. See `make-network-process' or `make-serial-process' for a
1166 list of keywords. */)
1167 (process, key)
1168 register Lisp_Object process, key;
1169 {
1170 Lisp_Object contact;
1171
1172 CHECK_PROCESS (process);
1173 contact = XPROCESS (process)->childp;
1174
1175 #ifdef DATAGRAM_SOCKETS
1176 if (DATAGRAM_CONN_P (process)
1177 && (EQ (key, Qt) || EQ (key, QCremote)))
1178 contact = Fplist_put (contact, QCremote,
1179 Fprocess_datagram_address (process));
1180 #endif
1181
1182 if ((!NETCONN_P (process) && !SERIALCONN_P (process)) || EQ (key, Qt))
1183 return contact;
1184 if (NILP (key) && NETCONN_P (process))
1185 return Fcons (Fplist_get (contact, QChost),
1186 Fcons (Fplist_get (contact, QCservice), Qnil));
1187 if (NILP (key) && SERIALCONN_P (process))
1188 return Fcons (Fplist_get (contact, QCport),
1189 Fcons (Fplist_get (contact, QCspeed), Qnil));
1190 return Fplist_get (contact, key);
1191 }
1192
1193 DEFUN ("process-plist", Fprocess_plist, Sprocess_plist,
1194 1, 1, 0,
1195 doc: /* Return the plist of PROCESS. */)
1196 (process)
1197 register Lisp_Object process;
1198 {
1199 CHECK_PROCESS (process);
1200 return XPROCESS (process)->plist;
1201 }
1202
1203 DEFUN ("set-process-plist", Fset_process_plist, Sset_process_plist,
1204 2, 2, 0,
1205 doc: /* Replace the plist of PROCESS with PLIST. Returns PLIST. */)
1206 (process, plist)
1207 register Lisp_Object process, plist;
1208 {
1209 CHECK_PROCESS (process);
1210 CHECK_LIST (plist);
1211
1212 XPROCESS (process)->plist = plist;
1213 return plist;
1214 }
1215
1216 #if 0 /* Turned off because we don't currently record this info
1217 in the process. Perhaps add it. */
1218 DEFUN ("process-connection", Fprocess_connection, Sprocess_connection, 1, 1, 0,
1219 doc: /* Return the connection type of PROCESS.
1220 The value is nil for a pipe, t or `pty' for a pty, or `stream' for
1221 a socket connection. */)
1222 (process)
1223 Lisp_Object process;
1224 {
1225 return XPROCESS (process)->type;
1226 }
1227 #endif
1228
1229 DEFUN ("process-type", Fprocess_type, Sprocess_type, 1, 1, 0,
1230 doc: /* Return the connection type of PROCESS.
1231 The value is either the symbol `real', `network', or `serial'.
1232 PROCESS may be a process, a buffer, the name of a process or buffer, or
1233 nil, indicating the current buffer's process. */)
1234 (process)
1235 Lisp_Object process;
1236 {
1237 Lisp_Object proc;
1238 proc = get_process (process);
1239 return XPROCESS (proc)->type;
1240 }
1241
1242 #ifdef HAVE_SOCKETS
1243 DEFUN ("format-network-address", Fformat_network_address, Sformat_network_address,
1244 1, 2, 0,
1245 doc: /* Convert network ADDRESS from internal format to a string.
1246 A 4 or 5 element vector represents an IPv4 address (with port number).
1247 An 8 or 9 element vector represents an IPv6 address (with port number).
1248 If optional second argument OMIT-PORT is non-nil, don't include a port
1249 number in the string, even when present in ADDRESS.
1250 Returns nil if format of ADDRESS is invalid. */)
1251 (address, omit_port)
1252 Lisp_Object address, omit_port;
1253 {
1254 if (NILP (address))
1255 return Qnil;
1256
1257 if (STRINGP (address)) /* AF_LOCAL */
1258 return address;
1259
1260 if (VECTORP (address)) /* AF_INET or AF_INET6 */
1261 {
1262 register struct Lisp_Vector *p = XVECTOR (address);
1263 Lisp_Object args[10];
1264 int nargs, i;
1265
1266 if (p->size == 4 || (p->size == 5 && !NILP (omit_port)))
1267 {
1268 args[0] = build_string ("%d.%d.%d.%d");
1269 nargs = 4;
1270 }
1271 else if (p->size == 5)
1272 {
1273 args[0] = build_string ("%d.%d.%d.%d:%d");
1274 nargs = 5;
1275 }
1276 else if (p->size == 8 || (p->size == 9 && !NILP (omit_port)))
1277 {
1278 args[0] = build_string ("%x:%x:%x:%x:%x:%x:%x:%x");
1279 nargs = 8;
1280 }
1281 else if (p->size == 9)
1282 {
1283 args[0] = build_string ("[%x:%x:%x:%x:%x:%x:%x:%x]:%d");
1284 nargs = 9;
1285 }
1286 else
1287 return Qnil;
1288
1289 for (i = 0; i < nargs; i++)
1290 {
1291 EMACS_INT element = XINT (p->contents[i]);
1292
1293 if (element < 0 || element > 65535)
1294 return Qnil;
1295
1296 if (nargs <= 5 /* IPv4 */
1297 && i < 4 /* host, not port */
1298 && element > 255)
1299 return Qnil;
1300
1301 args[i+1] = p->contents[i];
1302 }
1303
1304 return Fformat (nargs+1, args);
1305 }
1306
1307 if (CONSP (address))
1308 {
1309 Lisp_Object args[2];
1310 args[0] = build_string ("<Family %d>");
1311 args[1] = Fcar (address);
1312 return Fformat (2, args);
1313 }
1314
1315 return Qnil;
1316 }
1317 #endif
1318 \f
1319 static Lisp_Object
1320 list_processes_1 (Lisp_Object query_only)
1321 {
1322 register Lisp_Object tail, tem;
1323 Lisp_Object proc, minspace, tem1;
1324 register struct Lisp_Process *p;
1325 char tembuf[300];
1326 int w_proc, w_buffer, w_tty;
1327 int exited = 0;
1328 Lisp_Object i_status, i_buffer, i_tty, i_command;
1329
1330 w_proc = 4; /* Proc */
1331 w_buffer = 6; /* Buffer */
1332 w_tty = 0; /* Omit if no ttys */
1333
1334 for (tail = Vprocess_alist; CONSP (tail); tail = XCDR (tail))
1335 {
1336 int i;
1337
1338 proc = Fcdr (XCAR (tail));
1339 p = XPROCESS (proc);
1340 if (NILP (p->type))
1341 continue;
1342 if (!NILP (query_only) && p->kill_without_query)
1343 continue;
1344 if (STRINGP (p->name)
1345 && ( i = SCHARS (p->name), (i > w_proc)))
1346 w_proc = i;
1347 if (!NILP (p->buffer))
1348 {
1349 if (NILP (XBUFFER (p->buffer)->name))
1350 {
1351 if (w_buffer < 8)
1352 w_buffer = 8; /* (Killed) */
1353 }
1354 else if ((i = SCHARS (XBUFFER (p->buffer)->name), (i > w_buffer)))
1355 w_buffer = i;
1356 }
1357 if (STRINGP (p->tty_name)
1358 && (i = SCHARS (p->tty_name), (i > w_tty)))
1359 w_tty = i;
1360 }
1361
1362 XSETFASTINT (i_status, w_proc + 1);
1363 XSETFASTINT (i_buffer, XFASTINT (i_status) + 9);
1364 if (w_tty)
1365 {
1366 XSETFASTINT (i_tty, XFASTINT (i_buffer) + w_buffer + 1);
1367 XSETFASTINT (i_command, XFASTINT (i_tty) + w_tty + 1);
1368 }
1369 else
1370 {
1371 i_tty = Qnil;
1372 XSETFASTINT (i_command, XFASTINT (i_buffer) + w_buffer + 1);
1373 }
1374
1375 XSETFASTINT (minspace, 1);
1376
1377 set_buffer_internal (XBUFFER (Vstandard_output));
1378 current_buffer->undo_list = Qt;
1379
1380 current_buffer->truncate_lines = Qt;
1381
1382 write_string ("Proc", -1);
1383 Findent_to (i_status, minspace); write_string ("Status", -1);
1384 Findent_to (i_buffer, minspace); write_string ("Buffer", -1);
1385 if (!NILP (i_tty))
1386 {
1387 Findent_to (i_tty, minspace); write_string ("Tty", -1);
1388 }
1389 Findent_to (i_command, minspace); write_string ("Command", -1);
1390 write_string ("\n", -1);
1391
1392 write_string ("----", -1);
1393 Findent_to (i_status, minspace); write_string ("------", -1);
1394 Findent_to (i_buffer, minspace); write_string ("------", -1);
1395 if (!NILP (i_tty))
1396 {
1397 Findent_to (i_tty, minspace); write_string ("---", -1);
1398 }
1399 Findent_to (i_command, minspace); write_string ("-------", -1);
1400 write_string ("\n", -1);
1401
1402 for (tail = Vprocess_alist; CONSP (tail); tail = XCDR (tail))
1403 {
1404 Lisp_Object symbol;
1405
1406 proc = Fcdr (XCAR (tail));
1407 p = XPROCESS (proc);
1408 if (NILP (p->type))
1409 continue;
1410 if (!NILP (query_only) && p->kill_without_query)
1411 continue;
1412
1413 Finsert (1, &p->name);
1414 Findent_to (i_status, minspace);
1415
1416 if (p->raw_status_new)
1417 update_status (p);
1418 symbol = p->status;
1419 if (CONSP (p->status))
1420 symbol = XCAR (p->status);
1421
1422 if (EQ (symbol, Qsignal))
1423 {
1424 Lisp_Object tem;
1425 tem = Fcar (Fcdr (p->status));
1426 Fprinc (symbol, Qnil);
1427 }
1428 else if (NETCONN1_P (p) || SERIALCONN1_P (p))
1429 {
1430 if (EQ (symbol, Qexit))
1431 write_string ("closed", -1);
1432 else if (EQ (p->command, Qt))
1433 write_string ("stopped", -1);
1434 else if (EQ (symbol, Qrun))
1435 write_string ("open", -1);
1436 else
1437 Fprinc (symbol, Qnil);
1438 }
1439 else if (SERIALCONN1_P (p))
1440 {
1441 write_string ("running", -1);
1442 }
1443 else
1444 Fprinc (symbol, Qnil);
1445
1446 if (EQ (symbol, Qexit))
1447 {
1448 Lisp_Object tem;
1449 tem = Fcar (Fcdr (p->status));
1450 if (XFASTINT (tem))
1451 {
1452 sprintf (tembuf, " %d", (int) XFASTINT (tem));
1453 write_string (tembuf, -1);
1454 }
1455 }
1456
1457 if (EQ (symbol, Qsignal) || EQ (symbol, Qexit) || EQ (symbol, Qclosed))
1458 exited++;
1459
1460 Findent_to (i_buffer, minspace);
1461 if (NILP (p->buffer))
1462 insert_string ("(none)");
1463 else if (NILP (XBUFFER (p->buffer)->name))
1464 insert_string ("(Killed)");
1465 else
1466 Finsert (1, &XBUFFER (p->buffer)->name);
1467
1468 if (!NILP (i_tty))
1469 {
1470 Findent_to (i_tty, minspace);
1471 if (STRINGP (p->tty_name))
1472 Finsert (1, &p->tty_name);
1473 }
1474
1475 Findent_to (i_command, minspace);
1476
1477 if (EQ (p->status, Qlisten))
1478 {
1479 Lisp_Object port = Fplist_get (p->childp, QCservice);
1480 if (INTEGERP (port))
1481 port = Fnumber_to_string (port);
1482 if (NILP (port))
1483 port = Fformat_network_address (Fplist_get (p->childp, QClocal), Qnil);
1484 sprintf (tembuf, "(network %s server on %s)\n",
1485 (DATAGRAM_CHAN_P (p->infd) ? "datagram" : "stream"),
1486 (STRINGP (port) ? (char *)SDATA (port) : "?"));
1487 insert_string (tembuf);
1488 }
1489 else if (NETCONN1_P (p))
1490 {
1491 /* For a local socket, there is no host name,
1492 so display service instead. */
1493 Lisp_Object host = Fplist_get (p->childp, QChost);
1494 if (!STRINGP (host))
1495 {
1496 host = Fplist_get (p->childp, QCservice);
1497 if (INTEGERP (host))
1498 host = Fnumber_to_string (host);
1499 }
1500 if (NILP (host))
1501 host = Fformat_network_address (Fplist_get (p->childp, QCremote), Qnil);
1502 sprintf (tembuf, "(network %s connection to %s)\n",
1503 (DATAGRAM_CHAN_P (p->infd) ? "datagram" : "stream"),
1504 (STRINGP (host) ? (char *)SDATA (host) : "?"));
1505 insert_string (tembuf);
1506 }
1507 else if (SERIALCONN1_P (p))
1508 {
1509 Lisp_Object port = Fplist_get (p->childp, QCport);
1510 Lisp_Object speed = Fplist_get (p->childp, QCspeed);
1511 insert_string ("(serial port ");
1512 if (STRINGP (port))
1513 insert_string (SDATA (port));
1514 else
1515 insert_string ("?");
1516 if (INTEGERP (speed))
1517 {
1518 sprintf (tembuf, " at %ld b/s", (long) XINT (speed));
1519 insert_string (tembuf);
1520 }
1521 insert_string (")\n");
1522 }
1523 else
1524 {
1525 tem = p->command;
1526 while (1)
1527 {
1528 tem1 = Fcar (tem);
1529 if (NILP (tem1))
1530 break;
1531 Finsert (1, &tem1);
1532 tem = Fcdr (tem);
1533 if (NILP (tem))
1534 break;
1535 insert_string (" ");
1536 }
1537 insert_string ("\n");
1538 }
1539 }
1540 if (exited)
1541 {
1542 status_notify (NULL);
1543 redisplay_preserve_echo_area (13);
1544 }
1545 return Qnil;
1546 }
1547
1548 DEFUN ("list-processes", Flist_processes, Slist_processes, 0, 1, "P",
1549 doc: /* Display a list of all processes.
1550 If optional argument QUERY-ONLY is non-nil, only processes with
1551 the query-on-exit flag set will be listed.
1552 Any process listed as exited or signaled is actually eliminated
1553 after the listing is made. */)
1554 (query_only)
1555 Lisp_Object query_only;
1556 {
1557 internal_with_output_to_temp_buffer ("*Process List*",
1558 list_processes_1, query_only);
1559 return Qnil;
1560 }
1561
1562 DEFUN ("process-list", Fprocess_list, Sprocess_list, 0, 0, 0,
1563 doc: /* Return a list of all processes. */)
1564 ()
1565 {
1566 return Fmapcar (Qcdr, Vprocess_alist);
1567 }
1568 \f
1569 /* Starting asynchronous inferior processes. */
1570
1571 static Lisp_Object start_process_unwind (Lisp_Object proc);
1572
1573 DEFUN ("start-process", Fstart_process, Sstart_process, 3, MANY, 0,
1574 doc: /* Start a program in a subprocess. Return the process object for it.
1575 NAME is name for process. It is modified if necessary to make it unique.
1576 BUFFER is the buffer (or buffer name) to associate with the process.
1577
1578 Process output (both standard output and standard error streams) goes
1579 at end of BUFFER, unless you specify an output stream or filter
1580 function to handle the output. BUFFER may also be nil, meaning that
1581 this process is not associated with any buffer.
1582
1583 PROGRAM is the program file name. It is searched for in PATH. If
1584 nil, just associate a pty with the buffer. Remaining arguments are
1585 strings to give program as arguments.
1586
1587 If you want to separate standard output from standard error, invoke
1588 the command through a shell and redirect one of them using the shell
1589 syntax.
1590
1591 usage: (start-process NAME BUFFER PROGRAM &rest PROGRAM-ARGS) */)
1592 (nargs, args)
1593 int nargs;
1594 register Lisp_Object *args;
1595 {
1596 Lisp_Object buffer, name, program, proc, current_dir, tem;
1597 register unsigned char **new_argv;
1598 register int i;
1599 int count = SPECPDL_INDEX ();
1600
1601 buffer = args[1];
1602 if (!NILP (buffer))
1603 buffer = Fget_buffer_create (buffer);
1604
1605 /* Make sure that the child will be able to chdir to the current
1606 buffer's current directory, or its unhandled equivalent. We
1607 can't just have the child check for an error when it does the
1608 chdir, since it's in a vfork.
1609
1610 We have to GCPRO around this because Fexpand_file_name and
1611 Funhandled_file_name_directory might call a file name handling
1612 function. The argument list is protected by the caller, so all
1613 we really have to worry about is buffer. */
1614 {
1615 struct gcpro gcpro1, gcpro2;
1616
1617 current_dir = current_buffer->directory;
1618
1619 GCPRO2 (buffer, current_dir);
1620
1621 current_dir = Funhandled_file_name_directory (current_dir);
1622 if (NILP (current_dir))
1623 /* If the file name handler says that current_dir is unreachable, use
1624 a sensible default. */
1625 current_dir = build_string ("~/");
1626 current_dir = expand_and_dir_to_file (current_dir, Qnil);
1627 if (NILP (Ffile_accessible_directory_p (current_dir)))
1628 report_file_error ("Setting current directory",
1629 Fcons (current_buffer->directory, Qnil));
1630
1631 UNGCPRO;
1632 }
1633
1634 name = args[0];
1635 CHECK_STRING (name);
1636
1637 program = args[2];
1638
1639 if (!NILP (program))
1640 CHECK_STRING (program);
1641
1642 proc = make_process (name);
1643 /* If an error occurs and we can't start the process, we want to
1644 remove it from the process list. This means that each error
1645 check in create_process doesn't need to call remove_process
1646 itself; it's all taken care of here. */
1647 record_unwind_protect (start_process_unwind, proc);
1648
1649 XPROCESS (proc)->childp = Qt;
1650 XPROCESS (proc)->plist = Qnil;
1651 XPROCESS (proc)->type = Qreal;
1652 XPROCESS (proc)->buffer = buffer;
1653 XPROCESS (proc)->sentinel = Qnil;
1654 XPROCESS (proc)->filter = Qnil;
1655 XPROCESS (proc)->command = Flist (nargs - 2, args + 2);
1656
1657 #ifdef ADAPTIVE_READ_BUFFERING
1658 XPROCESS (proc)->adaptive_read_buffering
1659 = (NILP (Vprocess_adaptive_read_buffering) ? 0
1660 : EQ (Vprocess_adaptive_read_buffering, Qt) ? 1 : 2);
1661 #endif
1662
1663 /* Make the process marker point into the process buffer (if any). */
1664 if (BUFFERP (buffer))
1665 set_marker_both (XPROCESS (proc)->mark, buffer,
1666 BUF_ZV (XBUFFER (buffer)),
1667 BUF_ZV_BYTE (XBUFFER (buffer)));
1668
1669 {
1670 /* Decide coding systems for communicating with the process. Here
1671 we don't setup the structure coding_system nor pay attention to
1672 unibyte mode. They are done in create_process. */
1673
1674 /* Qt denotes we have not yet called Ffind_operation_coding_system. */
1675 Lisp_Object coding_systems = Qt;
1676 Lisp_Object val, *args2;
1677 struct gcpro gcpro1, gcpro2;
1678
1679 val = Vcoding_system_for_read;
1680 if (NILP (val))
1681 {
1682 args2 = (Lisp_Object *) alloca ((nargs + 1) * sizeof *args2);
1683 args2[0] = Qstart_process;
1684 for (i = 0; i < nargs; i++) args2[i + 1] = args[i];
1685 GCPRO2 (proc, current_dir);
1686 if (!NILP (program))
1687 coding_systems = Ffind_operation_coding_system (nargs + 1, args2);
1688 UNGCPRO;
1689 if (CONSP (coding_systems))
1690 val = XCAR (coding_systems);
1691 else if (CONSP (Vdefault_process_coding_system))
1692 val = XCAR (Vdefault_process_coding_system);
1693 }
1694 XPROCESS (proc)->decode_coding_system = val;
1695
1696 val = Vcoding_system_for_write;
1697 if (NILP (val))
1698 {
1699 if (EQ (coding_systems, Qt))
1700 {
1701 args2 = (Lisp_Object *) alloca ((nargs + 1) * sizeof args2);
1702 args2[0] = Qstart_process;
1703 for (i = 0; i < nargs; i++) args2[i + 1] = args[i];
1704 GCPRO2 (proc, current_dir);
1705 if (!NILP (program))
1706 coding_systems = Ffind_operation_coding_system (nargs + 1, args2);
1707 UNGCPRO;
1708 }
1709 if (CONSP (coding_systems))
1710 val = XCDR (coding_systems);
1711 else if (CONSP (Vdefault_process_coding_system))
1712 val = XCDR (Vdefault_process_coding_system);
1713 }
1714 XPROCESS (proc)->encode_coding_system = val;
1715 }
1716
1717
1718 XPROCESS (proc)->decoding_buf = make_uninit_string (0);
1719 XPROCESS (proc)->decoding_carryover = 0;
1720 XPROCESS (proc)->encoding_buf = make_uninit_string (0);
1721
1722 XPROCESS (proc)->inherit_coding_system_flag
1723 = !(NILP (buffer) || !inherit_process_coding_system);
1724
1725 if (!NILP (program))
1726 {
1727 /* If program file name is not absolute, search our path for it.
1728 Put the name we will really use in TEM. */
1729 if (!IS_DIRECTORY_SEP (SREF (program, 0))
1730 && !(SCHARS (program) > 1
1731 && IS_DEVICE_SEP (SREF (program, 1))))
1732 {
1733 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
1734
1735 tem = Qnil;
1736 GCPRO4 (name, program, buffer, current_dir);
1737 openp (Vexec_path, program, Vexec_suffixes, &tem, make_number (X_OK));
1738 UNGCPRO;
1739 if (NILP (tem))
1740 report_file_error ("Searching for program", Fcons (program, Qnil));
1741 tem = Fexpand_file_name (tem, Qnil);
1742 }
1743 else
1744 {
1745 if (!NILP (Ffile_directory_p (program)))
1746 error ("Specified program for new process is a directory");
1747 tem = program;
1748 }
1749
1750 /* If program file name starts with /: for quoting a magic name,
1751 discard that. */
1752 if (SBYTES (tem) > 2 && SREF (tem, 0) == '/'
1753 && SREF (tem, 1) == ':')
1754 tem = Fsubstring (tem, make_number (2), Qnil);
1755
1756 {
1757 struct gcpro gcpro1;
1758 GCPRO1 (tem);
1759
1760 /* Encode the file name and put it in NEW_ARGV.
1761 That's where the child will use it to execute the program. */
1762 tem = Fcons (ENCODE_FILE (tem), Qnil);
1763
1764 /* Here we encode arguments by the coding system used for sending
1765 data to the process. We don't support using different coding
1766 systems for encoding arguments and for encoding data sent to the
1767 process. */
1768
1769 for (i = 3; i < nargs; i++)
1770 {
1771 tem = Fcons (args[i], tem);
1772 CHECK_STRING (XCAR (tem));
1773 if (STRING_MULTIBYTE (XCAR (tem)))
1774 XSETCAR (tem,
1775 code_convert_string_norecord
1776 (XCAR (tem), XPROCESS (proc)->encode_coding_system, 1));
1777 }
1778
1779 UNGCPRO;
1780 }
1781
1782 /* Now that everything is encoded we can collect the strings into
1783 NEW_ARGV. */
1784 new_argv = (unsigned char **) alloca ((nargs - 1) * sizeof (char *));
1785 new_argv[nargs - 2] = 0;
1786
1787 for (i = nargs - 3; i >= 0; i--)
1788 {
1789 new_argv[i] = SDATA (XCAR (tem));
1790 tem = XCDR (tem);
1791 }
1792
1793 create_process (proc, (char **) new_argv, current_dir);
1794 }
1795 else
1796 create_pty (proc);
1797
1798 return unbind_to (count, proc);
1799 }
1800
1801 /* This function is the unwind_protect form for Fstart_process. If
1802 PROC doesn't have its pid set, then we know someone has signaled
1803 an error and the process wasn't started successfully, so we should
1804 remove it from the process list. */
1805 static Lisp_Object
1806 start_process_unwind (Lisp_Object proc)
1807 {
1808 if (!PROCESSP (proc))
1809 abort ();
1810
1811 /* Was PROC started successfully? */
1812 if (XPROCESS (proc)->pid == -1)
1813 remove_process (proc);
1814
1815 return Qnil;
1816 }
1817
1818 static void
1819 create_process_1 (struct atimer *timer)
1820 {
1821 /* Nothing to do. */
1822 }
1823
1824
1825 #if 0 /* This doesn't work; see the note before sigchld_handler. */
1826 #ifdef USG
1827 #ifdef SIGCHLD
1828 /* Mimic blocking of signals on system V, which doesn't really have it. */
1829
1830 /* Nonzero means we got a SIGCHLD when it was supposed to be blocked. */
1831 int sigchld_deferred;
1832
1833 SIGTYPE
1834 create_process_sigchld ()
1835 {
1836 signal (SIGCHLD, create_process_sigchld);
1837
1838 sigchld_deferred = 1;
1839 }
1840 #endif
1841 #endif
1842 #endif
1843
1844 void
1845 create_process (Lisp_Object process, char **new_argv, Lisp_Object current_dir)
1846 {
1847 int inchannel, outchannel;
1848 pid_t pid;
1849 int sv[2];
1850 #if !defined (WINDOWSNT) && defined (FD_CLOEXEC)
1851 int wait_child_setup[2];
1852 #endif
1853 sigset_t procmask;
1854 sigset_t blocked;
1855 struct sigaction sigint_action;
1856 struct sigaction sigquit_action;
1857 #ifdef AIX
1858 struct sigaction sighup_action;
1859 #endif
1860 /* Use volatile to protect variables from being clobbered by longjmp. */
1861 volatile int forkin, forkout;
1862 volatile int pty_flag = 0;
1863 #ifndef USE_CRT_DLL
1864 extern char **environ;
1865 #endif
1866
1867 inchannel = outchannel = -1;
1868
1869 #ifdef HAVE_PTYS
1870 if (!NILP (Vprocess_connection_type))
1871 outchannel = inchannel = allocate_pty ();
1872
1873 if (inchannel >= 0)
1874 {
1875 #if ! defined (USG) || defined (USG_SUBTTY_WORKS)
1876 /* On most USG systems it does not work to open the pty's tty here,
1877 then close it and reopen it in the child. */
1878 #ifdef O_NOCTTY
1879 /* Don't let this terminal become our controlling terminal
1880 (in case we don't have one). */
1881 forkout = forkin = emacs_open (pty_name, O_RDWR | O_NOCTTY, 0);
1882 #else
1883 forkout = forkin = emacs_open (pty_name, O_RDWR, 0);
1884 #endif
1885 if (forkin < 0)
1886 report_file_error ("Opening pty", Qnil);
1887 #else
1888 forkin = forkout = -1;
1889 #endif /* not USG, or USG_SUBTTY_WORKS */
1890 pty_flag = 1;
1891 }
1892 else
1893 #endif /* HAVE_PTYS */
1894 {
1895 int tem;
1896 tem = pipe (sv);
1897 if (tem < 0)
1898 report_file_error ("Creating pipe", Qnil);
1899 inchannel = sv[0];
1900 forkout = sv[1];
1901 tem = pipe (sv);
1902 if (tem < 0)
1903 {
1904 emacs_close (inchannel);
1905 emacs_close (forkout);
1906 report_file_error ("Creating pipe", Qnil);
1907 }
1908 outchannel = sv[1];
1909 forkin = sv[0];
1910 }
1911
1912 #if !defined (WINDOWSNT) && defined (FD_CLOEXEC)
1913 {
1914 int tem;
1915
1916 tem = pipe (wait_child_setup);
1917 if (tem < 0)
1918 report_file_error ("Creating pipe", Qnil);
1919 tem = fcntl (wait_child_setup[1], F_GETFD, 0);
1920 if (tem >= 0)
1921 tem = fcntl (wait_child_setup[1], F_SETFD, tem | FD_CLOEXEC);
1922 if (tem < 0)
1923 {
1924 emacs_close (wait_child_setup[0]);
1925 emacs_close (wait_child_setup[1]);
1926 report_file_error ("Setting file descriptor flags", Qnil);
1927 }
1928 }
1929 #endif
1930
1931 #if 0
1932 /* Replaced by close_process_descs */
1933 set_exclusive_use (inchannel);
1934 set_exclusive_use (outchannel);
1935 #endif
1936
1937 #ifdef O_NONBLOCK
1938 fcntl (inchannel, F_SETFL, O_NONBLOCK);
1939 fcntl (outchannel, F_SETFL, O_NONBLOCK);
1940 #else
1941 #ifdef O_NDELAY
1942 fcntl (inchannel, F_SETFL, O_NDELAY);
1943 fcntl (outchannel, F_SETFL, O_NDELAY);
1944 #endif
1945 #endif
1946
1947 /* Record this as an active process, with its channels.
1948 As a result, child_setup will close Emacs's side of the pipes. */
1949 chan_process[inchannel] = process;
1950 XPROCESS (process)->infd = inchannel;
1951 XPROCESS (process)->outfd = outchannel;
1952
1953 /* Previously we recorded the tty descriptor used in the subprocess.
1954 It was only used for getting the foreground tty process, so now
1955 we just reopen the device (see emacs_get_tty_pgrp) as this is
1956 more portable (see USG_SUBTTY_WORKS above). */
1957
1958 XPROCESS (process)->pty_flag = pty_flag;
1959 XPROCESS (process)->status = Qrun;
1960 setup_process_coding_systems (process);
1961
1962 /* Delay interrupts until we have a chance to store
1963 the new fork's pid in its process structure */
1964 sigemptyset (&blocked);
1965 #ifdef SIGCHLD
1966 sigaddset (&blocked, SIGCHLD);
1967 #endif
1968 #ifdef HAVE_WORKING_VFORK
1969 /* On many hosts (e.g. Solaris 2.4), if a vforked child calls `signal',
1970 this sets the parent's signal handlers as well as the child's.
1971 So delay all interrupts whose handlers the child might munge,
1972 and record the current handlers so they can be restored later. */
1973 sigaddset (&blocked, SIGINT ); sigaction (SIGINT , 0, &sigint_action );
1974 sigaddset (&blocked, SIGQUIT); sigaction (SIGQUIT, 0, &sigquit_action);
1975 #ifdef AIX
1976 sigaddset (&blocked, SIGHUP ); sigaction (SIGHUP , 0, &sighup_action );
1977 #endif
1978 #endif /* HAVE_WORKING_VFORK */
1979 sigprocmask (SIG_BLOCK, &blocked, &procmask);
1980
1981 FD_SET (inchannel, &input_wait_mask);
1982 FD_SET (inchannel, &non_keyboard_wait_mask);
1983 if (inchannel > max_process_desc)
1984 max_process_desc = inchannel;
1985
1986 /* Until we store the proper pid, enable sigchld_handler
1987 to recognize an unknown pid as standing for this process.
1988 It is very important not to let this `marker' value stay
1989 in the table after this function has returned; if it does
1990 it might cause call-process to hang and subsequent asynchronous
1991 processes to get their return values scrambled. */
1992 XPROCESS (process)->pid = -1;
1993
1994 BLOCK_INPUT;
1995
1996 {
1997 /* child_setup must clobber environ on systems with true vfork.
1998 Protect it from permanent change. */
1999 char **save_environ = environ;
2000
2001 current_dir = ENCODE_FILE (current_dir);
2002
2003 #ifndef WINDOWSNT
2004 pid = vfork ();
2005 if (pid == 0)
2006 #endif /* not WINDOWSNT */
2007 {
2008 int xforkin = forkin;
2009 int xforkout = forkout;
2010
2011 #if 0 /* This was probably a mistake--it duplicates code later on,
2012 but fails to handle all the cases. */
2013 /* Make sure SIGCHLD is not blocked in the child. */
2014 sigsetmask (SIGEMPTYMASK);
2015 #endif
2016
2017 /* Make the pty be the controlling terminal of the process. */
2018 #ifdef HAVE_PTYS
2019 /* First, disconnect its current controlling terminal. */
2020 #ifdef HAVE_SETSID
2021 /* We tried doing setsid only if pty_flag, but it caused
2022 process_set_signal to fail on SGI when using a pipe. */
2023 setsid ();
2024 /* Make the pty's terminal the controlling terminal. */
2025 if (pty_flag && xforkin >= 0)
2026 {
2027 #ifdef TIOCSCTTY
2028 /* We ignore the return value
2029 because faith@cs.unc.edu says that is necessary on Linux. */
2030 ioctl (xforkin, TIOCSCTTY, 0);
2031 #endif
2032 }
2033 #else /* not HAVE_SETSID */
2034 #ifdef USG
2035 /* It's very important to call setpgrp here and no time
2036 afterwards. Otherwise, we lose our controlling tty which
2037 is set when we open the pty. */
2038 setpgrp ();
2039 #endif /* USG */
2040 #endif /* not HAVE_SETSID */
2041 #if defined (HAVE_TERMIOS) && defined (LDISC1)
2042 if (pty_flag && xforkin >= 0)
2043 {
2044 struct termios t;
2045 tcgetattr (xforkin, &t);
2046 t.c_lflag = LDISC1;
2047 if (tcsetattr (xforkin, TCSANOW, &t) < 0)
2048 emacs_write (1, "create_process/tcsetattr LDISC1 failed\n", 39);
2049 }
2050 #else
2051 #if defined (NTTYDISC) && defined (TIOCSETD)
2052 if (pty_flag && xforkin >= 0)
2053 {
2054 /* Use new line discipline. */
2055 int ldisc = NTTYDISC;
2056 ioctl (xforkin, TIOCSETD, &ldisc);
2057 }
2058 #endif
2059 #endif
2060 #ifdef TIOCNOTTY
2061 /* In 4.3BSD, the TIOCSPGRP bug has been fixed, and now you
2062 can do TIOCSPGRP only to the process's controlling tty. */
2063 if (pty_flag)
2064 {
2065 /* I wonder: would just ioctl (0, TIOCNOTTY, 0) work here?
2066 I can't test it since I don't have 4.3. */
2067 int j = emacs_open ("/dev/tty", O_RDWR, 0);
2068 if (j >= 0)
2069 {
2070 ioctl (j, TIOCNOTTY, 0);
2071 emacs_close (j);
2072 }
2073 #ifndef USG
2074 /* In order to get a controlling terminal on some versions
2075 of BSD, it is necessary to put the process in pgrp 0
2076 before it opens the terminal. */
2077 #ifdef HAVE_SETPGID
2078 setpgid (0, 0);
2079 #else
2080 setpgrp (0, 0);
2081 #endif
2082 #endif
2083 }
2084 #endif /* TIOCNOTTY */
2085
2086 #if !defined (DONT_REOPEN_PTY)
2087 /*** There is a suggestion that this ought to be a
2088 conditional on TIOCSPGRP,
2089 or !(defined (HAVE_SETSID) && defined (TIOCSCTTY)).
2090 Trying the latter gave the wrong results on Debian GNU/Linux 1.1;
2091 that system does seem to need this code, even though
2092 both HAVE_SETSID and TIOCSCTTY are defined. */
2093 /* Now close the pty (if we had it open) and reopen it.
2094 This makes the pty the controlling terminal of the subprocess. */
2095 if (pty_flag)
2096 {
2097
2098 /* I wonder if emacs_close (emacs_open (pty_name, ...))
2099 would work? */
2100 if (xforkin >= 0)
2101 emacs_close (xforkin);
2102 xforkout = xforkin = emacs_open (pty_name, O_RDWR, 0);
2103
2104 if (xforkin < 0)
2105 {
2106 emacs_write (1, "Couldn't open the pty terminal ", 31);
2107 emacs_write (1, pty_name, strlen (pty_name));
2108 emacs_write (1, "\n", 1);
2109 _exit (1);
2110 }
2111
2112 }
2113 #endif /* not DONT_REOPEN_PTY */
2114
2115 #ifdef SETUP_SLAVE_PTY
2116 if (pty_flag)
2117 {
2118 SETUP_SLAVE_PTY;
2119 }
2120 #endif /* SETUP_SLAVE_PTY */
2121 #ifdef AIX
2122 /* On AIX, we've disabled SIGHUP above once we start a child on a pty.
2123 Now reenable it in the child, so it will die when we want it to. */
2124 if (pty_flag)
2125 signal (SIGHUP, SIG_DFL);
2126 #endif
2127 #endif /* HAVE_PTYS */
2128
2129 signal (SIGINT, SIG_DFL);
2130 signal (SIGQUIT, SIG_DFL);
2131
2132 /* Stop blocking signals in the child. */
2133 sigprocmask (SIG_SETMASK, &procmask, 0);
2134
2135 if (pty_flag)
2136 child_setup_tty (xforkout);
2137 #ifdef WINDOWSNT
2138 pid = child_setup (xforkin, xforkout, xforkout,
2139 new_argv, 1, current_dir);
2140 #else /* not WINDOWSNT */
2141 #ifdef FD_CLOEXEC
2142 emacs_close (wait_child_setup[0]);
2143 #endif
2144 child_setup (xforkin, xforkout, xforkout,
2145 new_argv, 1, current_dir);
2146 #endif /* not WINDOWSNT */
2147 }
2148 environ = save_environ;
2149 }
2150
2151 UNBLOCK_INPUT;
2152
2153 /* This runs in the Emacs process. */
2154 if (pid < 0)
2155 {
2156 if (forkin >= 0)
2157 emacs_close (forkin);
2158 if (forkin != forkout && forkout >= 0)
2159 emacs_close (forkout);
2160 }
2161 else
2162 {
2163 /* vfork succeeded. */
2164 XPROCESS (process)->pid = pid;
2165
2166 #ifdef WINDOWSNT
2167 register_child (pid, inchannel);
2168 #endif /* WINDOWSNT */
2169
2170 /* If the subfork execv fails, and it exits,
2171 this close hangs. I don't know why.
2172 So have an interrupt jar it loose. */
2173 {
2174 struct atimer *timer;
2175 EMACS_TIME offset;
2176
2177 stop_polling ();
2178 EMACS_SET_SECS_USECS (offset, 1, 0);
2179 timer = start_atimer (ATIMER_RELATIVE, offset, create_process_1, 0);
2180
2181 if (forkin >= 0)
2182 emacs_close (forkin);
2183
2184 cancel_atimer (timer);
2185 start_polling ();
2186 }
2187
2188 if (forkin != forkout && forkout >= 0)
2189 emacs_close (forkout);
2190
2191 #ifdef HAVE_PTYS
2192 if (pty_flag)
2193 XPROCESS (process)->tty_name = build_string (pty_name);
2194 else
2195 #endif
2196 XPROCESS (process)->tty_name = Qnil;
2197
2198 #if !defined (WINDOWSNT) && defined (FD_CLOEXEC)
2199 /* Wait for child_setup to complete in case that vfork is
2200 actually defined as fork. The descriptor wait_child_setup[1]
2201 of a pipe is closed at the child side either by close-on-exec
2202 on successful execvp or the _exit call in child_setup. */
2203 {
2204 char dummy;
2205
2206 emacs_close (wait_child_setup[1]);
2207 emacs_read (wait_child_setup[0], &dummy, 1);
2208 emacs_close (wait_child_setup[0]);
2209 }
2210 #endif
2211 }
2212
2213 /* Restore the signal state whether vfork succeeded or not.
2214 (We will signal an error, below, if it failed.) */
2215 #ifdef HAVE_WORKING_VFORK
2216 /* Restore the parent's signal handlers. */
2217 sigaction (SIGINT, &sigint_action, 0);
2218 sigaction (SIGQUIT, &sigquit_action, 0);
2219 #ifdef AIX
2220 sigaction (SIGHUP, &sighup_action, 0);
2221 #endif
2222 #endif /* HAVE_WORKING_VFORK */
2223 /* Stop blocking signals in the parent. */
2224 sigprocmask (SIG_SETMASK, &procmask, 0);
2225
2226 /* Now generate the error if vfork failed. */
2227 if (pid < 0)
2228 report_file_error ("Doing vfork", Qnil);
2229 }
2230
2231 void
2232 create_pty (Lisp_Object process)
2233 {
2234 int inchannel, outchannel;
2235
2236 /* Use volatile to protect variables from being clobbered by longjmp. */
2237 volatile int forkin, forkout;
2238 volatile int pty_flag = 0;
2239
2240 inchannel = outchannel = -1;
2241
2242 #ifdef HAVE_PTYS
2243 if (!NILP (Vprocess_connection_type))
2244 outchannel = inchannel = allocate_pty ();
2245
2246 if (inchannel >= 0)
2247 {
2248 #if ! defined (USG) || defined (USG_SUBTTY_WORKS)
2249 /* On most USG systems it does not work to open the pty's tty here,
2250 then close it and reopen it in the child. */
2251 #ifdef O_NOCTTY
2252 /* Don't let this terminal become our controlling terminal
2253 (in case we don't have one). */
2254 forkout = forkin = emacs_open (pty_name, O_RDWR | O_NOCTTY, 0);
2255 #else
2256 forkout = forkin = emacs_open (pty_name, O_RDWR, 0);
2257 #endif
2258 if (forkin < 0)
2259 report_file_error ("Opening pty", Qnil);
2260 #if defined (DONT_REOPEN_PTY)
2261 /* In the case that vfork is defined as fork, the parent process
2262 (Emacs) may send some data before the child process completes
2263 tty options setup. So we setup tty before forking. */
2264 child_setup_tty (forkout);
2265 #endif /* DONT_REOPEN_PTY */
2266 #else
2267 forkin = forkout = -1;
2268 #endif /* not USG, or USG_SUBTTY_WORKS */
2269 pty_flag = 1;
2270 }
2271 #endif /* HAVE_PTYS */
2272
2273 #ifdef O_NONBLOCK
2274 fcntl (inchannel, F_SETFL, O_NONBLOCK);
2275 fcntl (outchannel, F_SETFL, O_NONBLOCK);
2276 #else
2277 #ifdef O_NDELAY
2278 fcntl (inchannel, F_SETFL, O_NDELAY);
2279 fcntl (outchannel, F_SETFL, O_NDELAY);
2280 #endif
2281 #endif
2282
2283 /* Record this as an active process, with its channels.
2284 As a result, child_setup will close Emacs's side of the pipes. */
2285 chan_process[inchannel] = process;
2286 XPROCESS (process)->infd = inchannel;
2287 XPROCESS (process)->outfd = outchannel;
2288
2289 /* Previously we recorded the tty descriptor used in the subprocess.
2290 It was only used for getting the foreground tty process, so now
2291 we just reopen the device (see emacs_get_tty_pgrp) as this is
2292 more portable (see USG_SUBTTY_WORKS above). */
2293
2294 XPROCESS (process)->pty_flag = pty_flag;
2295 XPROCESS (process)->status = Qrun;
2296 setup_process_coding_systems (process);
2297
2298 FD_SET (inchannel, &input_wait_mask);
2299 FD_SET (inchannel, &non_keyboard_wait_mask);
2300 if (inchannel > max_process_desc)
2301 max_process_desc = inchannel;
2302
2303 XPROCESS (process)->pid = -2;
2304 #ifdef HAVE_PTYS
2305 if (pty_flag)
2306 XPROCESS (process)->tty_name = build_string (pty_name);
2307 else
2308 #endif
2309 XPROCESS (process)->tty_name = Qnil;
2310 }
2311
2312 \f
2313 #ifdef HAVE_SOCKETS
2314
2315 /* Convert an internal struct sockaddr to a lisp object (vector or string).
2316 The address family of sa is not included in the result. */
2317
2318 static Lisp_Object
2319 conv_sockaddr_to_lisp (struct sockaddr *sa, int len)
2320 {
2321 Lisp_Object address;
2322 int i;
2323 unsigned char *cp;
2324 register struct Lisp_Vector *p;
2325
2326 /* Workaround for a bug in getsockname on BSD: Names bound to
2327 sockets in the UNIX domain are inaccessible; getsockname returns
2328 a zero length name. */
2329 if (len < OFFSETOF (struct sockaddr, sa_family) + sizeof (sa->sa_family))
2330 return empty_unibyte_string;
2331
2332 switch (sa->sa_family)
2333 {
2334 case AF_INET:
2335 {
2336 struct sockaddr_in *sin = (struct sockaddr_in *) sa;
2337 len = sizeof (sin->sin_addr) + 1;
2338 address = Fmake_vector (make_number (len), Qnil);
2339 p = XVECTOR (address);
2340 p->contents[--len] = make_number (ntohs (sin->sin_port));
2341 cp = (unsigned char *) &sin->sin_addr;
2342 break;
2343 }
2344 #ifdef AF_INET6
2345 case AF_INET6:
2346 {
2347 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) sa;
2348 uint16_t *ip6 = (uint16_t *) &sin6->sin6_addr;
2349 len = sizeof (sin6->sin6_addr)/2 + 1;
2350 address = Fmake_vector (make_number (len), Qnil);
2351 p = XVECTOR (address);
2352 p->contents[--len] = make_number (ntohs (sin6->sin6_port));
2353 for (i = 0; i < len; i++)
2354 p->contents[i] = make_number (ntohs (ip6[i]));
2355 return address;
2356 }
2357 #endif
2358 #ifdef HAVE_LOCAL_SOCKETS
2359 case AF_LOCAL:
2360 {
2361 struct sockaddr_un *sockun = (struct sockaddr_un *) sa;
2362 for (i = 0; i < sizeof (sockun->sun_path); i++)
2363 if (sockun->sun_path[i] == 0)
2364 break;
2365 return make_unibyte_string (sockun->sun_path, i);
2366 }
2367 #endif
2368 default:
2369 len -= OFFSETOF (struct sockaddr, sa_family) + sizeof (sa->sa_family);
2370 address = Fcons (make_number (sa->sa_family),
2371 Fmake_vector (make_number (len), Qnil));
2372 p = XVECTOR (XCDR (address));
2373 cp = (unsigned char *) &sa->sa_family + sizeof (sa->sa_family);
2374 break;
2375 }
2376
2377 i = 0;
2378 while (i < len)
2379 p->contents[i++] = make_number (*cp++);
2380
2381 return address;
2382 }
2383
2384
2385 /* Get family and required size for sockaddr structure to hold ADDRESS. */
2386
2387 static int
2388 get_lisp_to_sockaddr_size (Lisp_Object address, int *familyp)
2389 {
2390 register struct Lisp_Vector *p;
2391
2392 if (VECTORP (address))
2393 {
2394 p = XVECTOR (address);
2395 if (p->size == 5)
2396 {
2397 *familyp = AF_INET;
2398 return sizeof (struct sockaddr_in);
2399 }
2400 #ifdef AF_INET6
2401 else if (p->size == 9)
2402 {
2403 *familyp = AF_INET6;
2404 return sizeof (struct sockaddr_in6);
2405 }
2406 #endif
2407 }
2408 #ifdef HAVE_LOCAL_SOCKETS
2409 else if (STRINGP (address))
2410 {
2411 *familyp = AF_LOCAL;
2412 return sizeof (struct sockaddr_un);
2413 }
2414 #endif
2415 else if (CONSP (address) && INTEGERP (XCAR (address)) && VECTORP (XCDR (address)))
2416 {
2417 struct sockaddr *sa;
2418 *familyp = XINT (XCAR (address));
2419 p = XVECTOR (XCDR (address));
2420 return p->size + sizeof (sa->sa_family);
2421 }
2422 return 0;
2423 }
2424
2425 /* Convert an address object (vector or string) to an internal sockaddr.
2426
2427 The address format has been basically validated by
2428 get_lisp_to_sockaddr_size, but this does not mean FAMILY is valid;
2429 it could have come from user data. So if FAMILY is not valid,
2430 we return after zeroing *SA. */
2431
2432 static void
2433 conv_lisp_to_sockaddr (int family, Lisp_Object address, struct sockaddr *sa, int len)
2434 {
2435 register struct Lisp_Vector *p;
2436 register unsigned char *cp = NULL;
2437 register int i;
2438
2439 bzero (sa, len);
2440
2441 if (VECTORP (address))
2442 {
2443 p = XVECTOR (address);
2444 if (family == AF_INET)
2445 {
2446 struct sockaddr_in *sin = (struct sockaddr_in *) sa;
2447 len = sizeof (sin->sin_addr) + 1;
2448 i = XINT (p->contents[--len]);
2449 sin->sin_port = htons (i);
2450 cp = (unsigned char *)&sin->sin_addr;
2451 sa->sa_family = family;
2452 }
2453 #ifdef AF_INET6
2454 else if (family == AF_INET6)
2455 {
2456 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) sa;
2457 uint16_t *ip6 = (uint16_t *)&sin6->sin6_addr;
2458 len = sizeof (sin6->sin6_addr) + 1;
2459 i = XINT (p->contents[--len]);
2460 sin6->sin6_port = htons (i);
2461 for (i = 0; i < len; i++)
2462 if (INTEGERP (p->contents[i]))
2463 {
2464 int j = XFASTINT (p->contents[i]) & 0xffff;
2465 ip6[i] = ntohs (j);
2466 }
2467 sa->sa_family = family;
2468 return;
2469 }
2470 #endif
2471 else
2472 return;
2473 }
2474 else if (STRINGP (address))
2475 {
2476 #ifdef HAVE_LOCAL_SOCKETS
2477 if (family == AF_LOCAL)
2478 {
2479 struct sockaddr_un *sockun = (struct sockaddr_un *) sa;
2480 cp = SDATA (address);
2481 for (i = 0; i < sizeof (sockun->sun_path) && *cp; i++)
2482 sockun->sun_path[i] = *cp++;
2483 sa->sa_family = family;
2484 }
2485 #endif
2486 return;
2487 }
2488 else
2489 {
2490 p = XVECTOR (XCDR (address));
2491 cp = (unsigned char *)sa + sizeof (sa->sa_family);
2492 }
2493
2494 for (i = 0; i < len; i++)
2495 if (INTEGERP (p->contents[i]))
2496 *cp++ = XFASTINT (p->contents[i]) & 0xff;
2497 }
2498
2499 #ifdef DATAGRAM_SOCKETS
2500 DEFUN ("process-datagram-address", Fprocess_datagram_address, Sprocess_datagram_address,
2501 1, 1, 0,
2502 doc: /* Get the current datagram address associated with PROCESS. */)
2503 (process)
2504 Lisp_Object process;
2505 {
2506 int channel;
2507
2508 CHECK_PROCESS (process);
2509
2510 if (!DATAGRAM_CONN_P (process))
2511 return Qnil;
2512
2513 channel = XPROCESS (process)->infd;
2514 return conv_sockaddr_to_lisp (datagram_address[channel].sa,
2515 datagram_address[channel].len);
2516 }
2517
2518 DEFUN ("set-process-datagram-address", Fset_process_datagram_address, Sset_process_datagram_address,
2519 2, 2, 0,
2520 doc: /* Set the datagram address for PROCESS to ADDRESS.
2521 Returns nil upon error setting address, ADDRESS otherwise. */)
2522 (process, address)
2523 Lisp_Object process, address;
2524 {
2525 int channel;
2526 int family, len;
2527
2528 CHECK_PROCESS (process);
2529
2530 if (!DATAGRAM_CONN_P (process))
2531 return Qnil;
2532
2533 channel = XPROCESS (process)->infd;
2534
2535 len = get_lisp_to_sockaddr_size (address, &family);
2536 if (datagram_address[channel].len != len)
2537 return Qnil;
2538 conv_lisp_to_sockaddr (family, address, datagram_address[channel].sa, len);
2539 return address;
2540 }
2541 #endif
2542 \f
2543
2544 static const struct socket_options {
2545 /* The name of this option. Should be lowercase version of option
2546 name without SO_ prefix. */
2547 char *name;
2548 /* Option level SOL_... */
2549 int optlevel;
2550 /* Option number SO_... */
2551 int optnum;
2552 enum { SOPT_UNKNOWN, SOPT_BOOL, SOPT_INT, SOPT_IFNAME, SOPT_LINGER } opttype;
2553 enum { OPIX_NONE=0, OPIX_MISC=1, OPIX_REUSEADDR=2 } optbit;
2554 } socket_options[] =
2555 {
2556 #ifdef SO_BINDTODEVICE
2557 { ":bindtodevice", SOL_SOCKET, SO_BINDTODEVICE, SOPT_IFNAME, OPIX_MISC },
2558 #endif
2559 #ifdef SO_BROADCAST
2560 { ":broadcast", SOL_SOCKET, SO_BROADCAST, SOPT_BOOL, OPIX_MISC },
2561 #endif
2562 #ifdef SO_DONTROUTE
2563 { ":dontroute", SOL_SOCKET, SO_DONTROUTE, SOPT_BOOL, OPIX_MISC },
2564 #endif
2565 #ifdef SO_KEEPALIVE
2566 { ":keepalive", SOL_SOCKET, SO_KEEPALIVE, SOPT_BOOL, OPIX_MISC },
2567 #endif
2568 #ifdef SO_LINGER
2569 { ":linger", SOL_SOCKET, SO_LINGER, SOPT_LINGER, OPIX_MISC },
2570 #endif
2571 #ifdef SO_OOBINLINE
2572 { ":oobinline", SOL_SOCKET, SO_OOBINLINE, SOPT_BOOL, OPIX_MISC },
2573 #endif
2574 #ifdef SO_PRIORITY
2575 { ":priority", SOL_SOCKET, SO_PRIORITY, SOPT_INT, OPIX_MISC },
2576 #endif
2577 #ifdef SO_REUSEADDR
2578 { ":reuseaddr", SOL_SOCKET, SO_REUSEADDR, SOPT_BOOL, OPIX_REUSEADDR },
2579 #endif
2580 { 0, 0, 0, SOPT_UNKNOWN, OPIX_NONE }
2581 };
2582
2583 /* Set option OPT to value VAL on socket S.
2584
2585 Returns (1<<socket_options[OPT].optbit) if option is known, 0 otherwise.
2586 Signals an error if setting a known option fails.
2587 */
2588
2589 static int
2590 set_socket_option (int s, Lisp_Object opt, Lisp_Object val)
2591 {
2592 char *name;
2593 const struct socket_options *sopt;
2594 int ret = 0;
2595
2596 CHECK_SYMBOL (opt);
2597
2598 name = (char *) SDATA (SYMBOL_NAME (opt));
2599 for (sopt = socket_options; sopt->name; sopt++)
2600 if (strcmp (name, sopt->name) == 0)
2601 break;
2602
2603 switch (sopt->opttype)
2604 {
2605 case SOPT_BOOL:
2606 {
2607 int optval;
2608 optval = NILP (val) ? 0 : 1;
2609 ret = setsockopt (s, sopt->optlevel, sopt->optnum,
2610 &optval, sizeof (optval));
2611 break;
2612 }
2613
2614 case SOPT_INT:
2615 {
2616 int optval;
2617 if (INTEGERP (val))
2618 optval = XINT (val);
2619 else
2620 error ("Bad option value for %s", name);
2621 ret = setsockopt (s, sopt->optlevel, sopt->optnum,
2622 &optval, sizeof (optval));
2623 break;
2624 }
2625
2626 #ifdef SO_BINDTODEVICE
2627 case SOPT_IFNAME:
2628 {
2629 char devname[IFNAMSIZ+1];
2630
2631 /* This is broken, at least in the Linux 2.4 kernel.
2632 To unbind, the arg must be a zero integer, not the empty string.
2633 This should work on all systems. KFS. 2003-09-23. */
2634 bzero (devname, sizeof devname);
2635 if (STRINGP (val))
2636 {
2637 char *arg = (char *) SDATA (val);
2638 int len = min (strlen (arg), IFNAMSIZ);
2639 bcopy (arg, devname, len);
2640 }
2641 else if (!NILP (val))
2642 error ("Bad option value for %s", name);
2643 ret = setsockopt (s, sopt->optlevel, sopt->optnum,
2644 devname, IFNAMSIZ);
2645 break;
2646 }
2647 #endif
2648
2649 #ifdef SO_LINGER
2650 case SOPT_LINGER:
2651 {
2652 struct linger linger;
2653
2654 linger.l_onoff = 1;
2655 linger.l_linger = 0;
2656 if (INTEGERP (val))
2657 linger.l_linger = XINT (val);
2658 else
2659 linger.l_onoff = NILP (val) ? 0 : 1;
2660 ret = setsockopt (s, sopt->optlevel, sopt->optnum,
2661 &linger, sizeof (linger));
2662 break;
2663 }
2664 #endif
2665
2666 default:
2667 return 0;
2668 }
2669
2670 if (ret < 0)
2671 report_file_error ("Cannot set network option",
2672 Fcons (opt, Fcons (val, Qnil)));
2673 return (1 << sopt->optbit);
2674 }
2675
2676
2677 DEFUN ("set-network-process-option",
2678 Fset_network_process_option, Sset_network_process_option,
2679 3, 4, 0,
2680 doc: /* For network process PROCESS set option OPTION to value VALUE.
2681 See `make-network-process' for a list of options and values.
2682 If optional fourth arg NO-ERROR is non-nil, don't signal an error if
2683 OPTION is not a supported option, return nil instead; otherwise return t. */)
2684 (process, option, value, no_error)
2685 Lisp_Object process, option, value;
2686 Lisp_Object no_error;
2687 {
2688 int s;
2689 struct Lisp_Process *p;
2690
2691 CHECK_PROCESS (process);
2692 p = XPROCESS (process);
2693 if (!NETCONN1_P (p))
2694 error ("Process is not a network process");
2695
2696 s = p->infd;
2697 if (s < 0)
2698 error ("Process is not running");
2699
2700 if (set_socket_option (s, option, value))
2701 {
2702 p->childp = Fplist_put (p->childp, option, value);
2703 return Qt;
2704 }
2705
2706 if (NILP (no_error))
2707 error ("Unknown or unsupported option");
2708
2709 return Qnil;
2710 }
2711
2712 \f
2713 #ifdef HAVE_SERIAL
2714 DEFUN ("serial-process-configure",
2715 Fserial_process_configure,
2716 Sserial_process_configure,
2717 0, MANY, 0,
2718 doc: /* Configure speed, bytesize, etc. of a serial process.
2719
2720 Arguments are specified as keyword/argument pairs. Attributes that
2721 are not given are re-initialized from the process's current
2722 configuration (available via the function `process-contact') or set to
2723 reasonable default values. The following arguments are defined:
2724
2725 :process PROCESS
2726 :name NAME
2727 :buffer BUFFER
2728 :port PORT
2729 -- Any of these arguments can be given to identify the process that is
2730 to be configured. If none of these arguments is given, the current
2731 buffer's process is used.
2732
2733 :speed SPEED -- SPEED is the speed of the serial port in bits per
2734 second, also called baud rate. Any value can be given for SPEED, but
2735 most serial ports work only at a few defined values between 1200 and
2736 115200, with 9600 being the most common value. If SPEED is nil, the
2737 serial port is not configured any further, i.e., all other arguments
2738 are ignored. This may be useful for special serial ports such as
2739 Bluetooth-to-serial converters which can only be configured through AT
2740 commands. A value of nil for SPEED can be used only when passed
2741 through `make-serial-process' or `serial-term'.
2742
2743 :bytesize BYTESIZE -- BYTESIZE is the number of bits per byte, which
2744 can be 7 or 8. If BYTESIZE is not given or nil, a value of 8 is used.
2745
2746 :parity PARITY -- PARITY can be nil (don't use parity), the symbol
2747 `odd' (use odd parity), or the symbol `even' (use even parity). If
2748 PARITY is not given, no parity is used.
2749
2750 :stopbits STOPBITS -- STOPBITS is the number of stopbits used to
2751 terminate a byte transmission. STOPBITS can be 1 or 2. If STOPBITS
2752 is not given or nil, 1 stopbit is used.
2753
2754 :flowcontrol FLOWCONTROL -- FLOWCONTROL determines the type of
2755 flowcontrol to be used, which is either nil (don't use flowcontrol),
2756 the symbol `hw' (use RTS/CTS hardware flowcontrol), or the symbol `sw'
2757 \(use XON/XOFF software flowcontrol). If FLOWCONTROL is not given, no
2758 flowcontrol is used.
2759
2760 `serial-process-configure' is called by `make-serial-process' for the
2761 initial configuration of the serial port.
2762
2763 Examples:
2764
2765 \(serial-process-configure :process "/dev/ttyS0" :speed 1200)
2766
2767 \(serial-process-configure
2768 :buffer "COM1" :stopbits 1 :parity 'odd :flowcontrol 'hw)
2769
2770 \(serial-process-configure :port "\\\\.\\COM13" :bytesize 7)
2771
2772 usage: (serial-process-configure &rest ARGS) */)
2773 (nargs, args)
2774 int nargs;
2775 Lisp_Object *args;
2776 {
2777 struct Lisp_Process *p;
2778 Lisp_Object contact = Qnil;
2779 Lisp_Object proc = Qnil;
2780 struct gcpro gcpro1;
2781
2782 contact = Flist (nargs, args);
2783 GCPRO1 (contact);
2784
2785 proc = Fplist_get (contact, QCprocess);
2786 if (NILP (proc))
2787 proc = Fplist_get (contact, QCname);
2788 if (NILP (proc))
2789 proc = Fplist_get (contact, QCbuffer);
2790 if (NILP (proc))
2791 proc = Fplist_get (contact, QCport);
2792 proc = get_process (proc);
2793 p = XPROCESS (proc);
2794 if (!EQ (p->type, Qserial))
2795 error ("Not a serial process");
2796
2797 if (NILP (Fplist_get (p->childp, QCspeed)))
2798 {
2799 UNGCPRO;
2800 return Qnil;
2801 }
2802
2803 serial_configure (p, contact);
2804
2805 UNGCPRO;
2806 return Qnil;
2807 }
2808
2809 /* Used by make-serial-process to recover from errors. */
2810 Lisp_Object make_serial_process_unwind (Lisp_Object proc)
2811 {
2812 if (!PROCESSP (proc))
2813 abort ();
2814 remove_process (proc);
2815 return Qnil;
2816 }
2817
2818 DEFUN ("make-serial-process", Fmake_serial_process, Smake_serial_process,
2819 0, MANY, 0,
2820 doc: /* Create and return a serial port process.
2821
2822 In Emacs, serial port connections are represented by process objects,
2823 so input and output work as for subprocesses, and `delete-process'
2824 closes a serial port connection. However, a serial process has no
2825 process id, it cannot be signaled, and the status codes are different
2826 from normal processes.
2827
2828 `make-serial-process' creates a process and a buffer, on which you
2829 probably want to use `process-send-string'. Try \\[serial-term] for
2830 an interactive terminal. See below for examples.
2831
2832 Arguments are specified as keyword/argument pairs. The following
2833 arguments are defined:
2834
2835 :port PORT -- (mandatory) PORT is the path or name of the serial port.
2836 For example, this could be "/dev/ttyS0" on Unix. On Windows, this
2837 could be "COM1", or "\\\\.\\COM10" for ports higher than COM9 (double
2838 the backslashes in strings).
2839
2840 :speed SPEED -- (mandatory) is handled by `serial-process-configure',
2841 which is called by `make-serial-process'.
2842
2843 :name NAME -- NAME is the name of the process. If NAME is not given,
2844 the value of PORT is used.
2845
2846 :buffer BUFFER -- BUFFER is the buffer (or buffer-name) to associate
2847 with the process. Process output goes at the end of that buffer,
2848 unless you specify an output stream or filter function to handle the
2849 output. If BUFFER is not given, the value of NAME is used.
2850
2851 :coding CODING -- If CODING is a symbol, it specifies the coding
2852 system used for both reading and writing for this process. If CODING
2853 is a cons (DECODING . ENCODING), DECODING is used for reading, and
2854 ENCODING is used for writing.
2855
2856 :noquery BOOL -- When exiting Emacs, query the user if BOOL is nil and
2857 the process is running. If BOOL is not given, query before exiting.
2858
2859 :stop BOOL -- Start process in the `stopped' state if BOOL is non-nil.
2860 In the stopped state, a serial process does not accept incoming data,
2861 but you can send outgoing data. The stopped state is cleared by
2862 `continue-process' and set by `stop-process'.
2863
2864 :filter FILTER -- Install FILTER as the process filter.
2865
2866 :sentinel SENTINEL -- Install SENTINEL as the process sentinel.
2867
2868 :plist PLIST -- Install PLIST as the initial plist of the process.
2869
2870 :speed
2871 :bytesize
2872 :parity
2873 :stopbits
2874 :flowcontrol
2875 -- These arguments are handled by `serial-process-configure', which is
2876 called by `make-serial-process'.
2877
2878 The original argument list, possibly modified by later configuration,
2879 is available via the function `process-contact'.
2880
2881 Examples:
2882
2883 \(make-serial-process :port "/dev/ttyS0" :speed 9600)
2884
2885 \(make-serial-process :port "COM1" :speed 115200 :stopbits 2)
2886
2887 \(make-serial-process :port "\\\\.\\COM13" :speed 1200 :bytesize 7 :parity 'odd)
2888
2889 \(make-serial-process :port "/dev/tty.BlueConsole-SPP-1" :speed nil)
2890
2891 usage: (make-serial-process &rest ARGS) */)
2892 (nargs, args)
2893 int nargs;
2894 Lisp_Object *args;
2895 {
2896 int fd = -1;
2897 Lisp_Object proc, contact, port;
2898 struct Lisp_Process *p;
2899 struct gcpro gcpro1;
2900 Lisp_Object name, buffer;
2901 Lisp_Object tem, val;
2902 int specpdl_count = -1;
2903
2904 if (nargs == 0)
2905 return Qnil;
2906
2907 contact = Flist (nargs, args);
2908 GCPRO1 (contact);
2909
2910 port = Fplist_get (contact, QCport);
2911 if (NILP (port))
2912 error ("No port specified");
2913 CHECK_STRING (port);
2914
2915 if (NILP (Fplist_member (contact, QCspeed)))
2916 error (":speed not specified");
2917 if (!NILP (Fplist_get (contact, QCspeed)))
2918 CHECK_NUMBER (Fplist_get (contact, QCspeed));
2919
2920 name = Fplist_get (contact, QCname);
2921 if (NILP (name))
2922 name = port;
2923 CHECK_STRING (name);
2924 proc = make_process (name);
2925 specpdl_count = SPECPDL_INDEX ();
2926 record_unwind_protect (make_serial_process_unwind, proc);
2927 p = XPROCESS (proc);
2928
2929 fd = serial_open ((char*) SDATA (port));
2930 p->infd = fd;
2931 p->outfd = fd;
2932 if (fd > max_process_desc)
2933 max_process_desc = fd;
2934 chan_process[fd] = proc;
2935
2936 buffer = Fplist_get (contact, QCbuffer);
2937 if (NILP (buffer))
2938 buffer = name;
2939 buffer = Fget_buffer_create (buffer);
2940 p->buffer = buffer;
2941
2942 p->childp = contact;
2943 p->plist = Fcopy_sequence (Fplist_get (contact, QCplist));
2944 p->type = Qserial;
2945 p->sentinel = Fplist_get (contact, QCsentinel);
2946 p->filter = Fplist_get (contact, QCfilter);
2947 p->log = Qnil;
2948 if (tem = Fplist_get (contact, QCnoquery), !NILP (tem))
2949 p->kill_without_query = 1;
2950 if (tem = Fplist_get (contact, QCstop), !NILP (tem))
2951 p->command = Qt;
2952 p->pty_flag = 0;
2953
2954 if (!EQ (p->command, Qt))
2955 {
2956 FD_SET (fd, &input_wait_mask);
2957 FD_SET (fd, &non_keyboard_wait_mask);
2958 }
2959
2960 if (BUFFERP (buffer))
2961 {
2962 set_marker_both (p->mark, buffer,
2963 BUF_ZV (XBUFFER (buffer)),
2964 BUF_ZV_BYTE (XBUFFER (buffer)));
2965 }
2966
2967 tem = Fplist_member (contact, QCcoding);
2968 if (!NILP (tem) && (!CONSP (tem) || !CONSP (XCDR (tem))))
2969 tem = Qnil;
2970
2971 val = Qnil;
2972 if (!NILP (tem))
2973 {
2974 val = XCAR (XCDR (tem));
2975 if (CONSP (val))
2976 val = XCAR (val);
2977 }
2978 else if (!NILP (Vcoding_system_for_read))
2979 val = Vcoding_system_for_read;
2980 else if ((!NILP (buffer) && NILP (XBUFFER (buffer)->enable_multibyte_characters))
2981 || (NILP (buffer) && NILP (buffer_defaults.enable_multibyte_characters)))
2982 val = Qnil;
2983 p->decode_coding_system = val;
2984
2985 val = Qnil;
2986 if (!NILP (tem))
2987 {
2988 val = XCAR (XCDR (tem));
2989 if (CONSP (val))
2990 val = XCDR (val);
2991 }
2992 else if (!NILP (Vcoding_system_for_write))
2993 val = Vcoding_system_for_write;
2994 else if ((!NILP (buffer) && NILP (XBUFFER (buffer)->enable_multibyte_characters))
2995 || (NILP (buffer) && NILP (buffer_defaults.enable_multibyte_characters)))
2996 val = Qnil;
2997 p->encode_coding_system = val;
2998
2999 setup_process_coding_systems (proc);
3000 p->decoding_buf = make_uninit_string (0);
3001 p->decoding_carryover = 0;
3002 p->encoding_buf = make_uninit_string (0);
3003 p->inherit_coding_system_flag
3004 = !(!NILP (tem) || NILP (buffer) || !inherit_process_coding_system);
3005
3006 Fserial_process_configure(nargs, args);
3007
3008 specpdl_ptr = specpdl + specpdl_count;
3009
3010 UNGCPRO;
3011 return proc;
3012 }
3013 #endif /* HAVE_SERIAL */
3014
3015 /* Create a network stream/datagram client/server process. Treated
3016 exactly like a normal process when reading and writing. Primary
3017 differences are in status display and process deletion. A network
3018 connection has no PID; you cannot signal it. All you can do is
3019 stop/continue it and deactivate/close it via delete-process */
3020
3021 DEFUN ("make-network-process", Fmake_network_process, Smake_network_process,
3022 0, MANY, 0,
3023 doc: /* Create and return a network server or client process.
3024
3025 In Emacs, network connections are represented by process objects, so
3026 input and output work as for subprocesses and `delete-process' closes
3027 a network connection. However, a network process has no process id,
3028 it cannot be signaled, and the status codes are different from normal
3029 processes.
3030
3031 Arguments are specified as keyword/argument pairs. The following
3032 arguments are defined:
3033
3034 :name NAME -- NAME is name for process. It is modified if necessary
3035 to make it unique.
3036
3037 :buffer BUFFER -- BUFFER is the buffer (or buffer-name) to associate
3038 with the process. Process output goes at end of that buffer, unless
3039 you specify an output stream or filter function to handle the output.
3040 BUFFER may be also nil, meaning that this process is not associated
3041 with any buffer.
3042
3043 :host HOST -- HOST is name of the host to connect to, or its IP
3044 address. The symbol `local' specifies the local host. If specified
3045 for a server process, it must be a valid name or address for the local
3046 host, and only clients connecting to that address will be accepted.
3047
3048 :service SERVICE -- SERVICE is name of the service desired, or an
3049 integer specifying a port number to connect to. If SERVICE is t,
3050 a random port number is selected for the server. (If Emacs was
3051 compiled with getaddrinfo, a port number can also be specified as a
3052 string, e.g. "80", as well as an integer. This is not portable.)
3053
3054 :type TYPE -- TYPE is the type of connection. The default (nil) is a
3055 stream type connection, `datagram' creates a datagram type connection,
3056 `seqpacket' creates a reliable datagram connection.
3057
3058 :family FAMILY -- FAMILY is the address (and protocol) family for the
3059 service specified by HOST and SERVICE. The default (nil) is to use
3060 whatever address family (IPv4 or IPv6) that is defined for the host
3061 and port number specified by HOST and SERVICE. Other address families
3062 supported are:
3063 local -- for a local (i.e. UNIX) address specified by SERVICE.
3064 ipv4 -- use IPv4 address family only.
3065 ipv6 -- use IPv6 address family only.
3066
3067 :local ADDRESS -- ADDRESS is the local address used for the connection.
3068 This parameter is ignored when opening a client process. When specified
3069 for a server process, the FAMILY, HOST and SERVICE args are ignored.
3070
3071 :remote ADDRESS -- ADDRESS is the remote partner's address for the
3072 connection. This parameter is ignored when opening a stream server
3073 process. For a datagram server process, it specifies the initial
3074 setting of the remote datagram address. When specified for a client
3075 process, the FAMILY, HOST, and SERVICE args are ignored.
3076
3077 The format of ADDRESS depends on the address family:
3078 - An IPv4 address is represented as an vector of integers [A B C D P]
3079 corresponding to numeric IP address A.B.C.D and port number P.
3080 - A local address is represented as a string with the address in the
3081 local address space.
3082 - An "unsupported family" address is represented by a cons (F . AV)
3083 where F is the family number and AV is a vector containing the socket
3084 address data with one element per address data byte. Do not rely on
3085 this format in portable code, as it may depend on implementation
3086 defined constants, data sizes, and data structure alignment.
3087
3088 :coding CODING -- If CODING is a symbol, it specifies the coding
3089 system used for both reading and writing for this process. If CODING
3090 is a cons (DECODING . ENCODING), DECODING is used for reading, and
3091 ENCODING is used for writing.
3092
3093 :nowait BOOL -- If BOOL is non-nil for a stream type client process,
3094 return without waiting for the connection to complete; instead, the
3095 sentinel function will be called with second arg matching "open" (if
3096 successful) or "failed" when the connect completes. Default is to use
3097 a blocking connect (i.e. wait) for stream type connections.
3098
3099 :noquery BOOL -- Query the user unless BOOL is non-nil, and process is
3100 running when Emacs is exited.
3101
3102 :stop BOOL -- Start process in the `stopped' state if BOOL non-nil.
3103 In the stopped state, a server process does not accept new
3104 connections, and a client process does not handle incoming traffic.
3105 The stopped state is cleared by `continue-process' and set by
3106 `stop-process'.
3107
3108 :filter FILTER -- Install FILTER as the process filter.
3109
3110 :filter-multibyte BOOL -- If BOOL is non-nil, strings given to the
3111 process filter are multibyte, otherwise they are unibyte.
3112 If this keyword is not specified, the strings are multibyte if
3113 `default-enable-multibyte-characters' is non-nil.
3114
3115 :sentinel SENTINEL -- Install SENTINEL as the process sentinel.
3116
3117 :log LOG -- Install LOG as the server process log function. This
3118 function is called when the server accepts a network connection from a
3119 client. The arguments are SERVER, CLIENT, and MESSAGE, where SERVER
3120 is the server process, CLIENT is the new process for the connection,
3121 and MESSAGE is a string.
3122
3123 :plist PLIST -- Install PLIST as the new process' initial plist.
3124
3125 :server QLEN -- if QLEN is non-nil, create a server process for the
3126 specified FAMILY, SERVICE, and connection type (stream or datagram).
3127 If QLEN is an integer, it is used as the max. length of the server's
3128 pending connection queue (also known as the backlog); the default
3129 queue length is 5. Default is to create a client process.
3130
3131 The following network options can be specified for this connection:
3132
3133 :broadcast BOOL -- Allow send and receive of datagram broadcasts.
3134 :dontroute BOOL -- Only send to directly connected hosts.
3135 :keepalive BOOL -- Send keep-alive messages on network stream.
3136 :linger BOOL or TIMEOUT -- Send queued messages before closing.
3137 :oobinline BOOL -- Place out-of-band data in receive data stream.
3138 :priority INT -- Set protocol defined priority for sent packets.
3139 :reuseaddr BOOL -- Allow reusing a recently used local address
3140 (this is allowed by default for a server process).
3141 :bindtodevice NAME -- bind to interface NAME. Using this may require
3142 special privileges on some systems.
3143
3144 Consult the relevant system programmer's manual pages for more
3145 information on using these options.
3146
3147
3148 A server process will listen for and accept connections from clients.
3149 When a client connection is accepted, a new network process is created
3150 for the connection with the following parameters:
3151
3152 - The client's process name is constructed by concatenating the server
3153 process' NAME and a client identification string.
3154 - If the FILTER argument is non-nil, the client process will not get a
3155 separate process buffer; otherwise, the client's process buffer is a newly
3156 created buffer named after the server process' BUFFER name or process
3157 NAME concatenated with the client identification string.
3158 - The connection type and the process filter and sentinel parameters are
3159 inherited from the server process' TYPE, FILTER and SENTINEL.
3160 - The client process' contact info is set according to the client's
3161 addressing information (typically an IP address and a port number).
3162 - The client process' plist is initialized from the server's plist.
3163
3164 Notice that the FILTER and SENTINEL args are never used directly by
3165 the server process. Also, the BUFFER argument is not used directly by
3166 the server process, but via the optional :log function, accepted (and
3167 failed) connections may be logged in the server process' buffer.
3168
3169 The original argument list, modified with the actual connection
3170 information, is available via the `process-contact' function.
3171
3172 usage: (make-network-process &rest ARGS) */)
3173 (nargs, args)
3174 int nargs;
3175 Lisp_Object *args;
3176 {
3177 Lisp_Object proc;
3178 Lisp_Object contact;
3179 struct Lisp_Process *p;
3180 #ifdef HAVE_GETADDRINFO
3181 struct addrinfo ai, *res, *lres;
3182 struct addrinfo hints;
3183 char *portstring, portbuf[128];
3184 #else /* HAVE_GETADDRINFO */
3185 struct _emacs_addrinfo
3186 {
3187 int ai_family;
3188 int ai_socktype;
3189 int ai_protocol;
3190 int ai_addrlen;
3191 struct sockaddr *ai_addr;
3192 struct _emacs_addrinfo *ai_next;
3193 } ai, *res, *lres;
3194 #endif /* HAVE_GETADDRINFO */
3195 struct sockaddr_in address_in;
3196 #ifdef HAVE_LOCAL_SOCKETS
3197 struct sockaddr_un address_un;
3198 #endif
3199 int port;
3200 int ret = 0;
3201 int xerrno = 0;
3202 int s = -1, outch, inch;
3203 struct gcpro gcpro1;
3204 int count = SPECPDL_INDEX ();
3205 int count1;
3206 Lisp_Object QCaddress; /* one of QClocal or QCremote */
3207 Lisp_Object tem;
3208 Lisp_Object name, buffer, host, service, address;
3209 Lisp_Object filter, sentinel;
3210 int is_non_blocking_client = 0;
3211 int is_server = 0, backlog = 5;
3212 int socktype;
3213 int family = -1;
3214
3215 if (nargs == 0)
3216 return Qnil;
3217
3218 /* Save arguments for process-contact and clone-process. */
3219 contact = Flist (nargs, args);
3220 GCPRO1 (contact);
3221
3222 #ifdef WINDOWSNT
3223 /* Ensure socket support is loaded if available. */
3224 init_winsock (TRUE);
3225 #endif
3226
3227 /* :type TYPE (nil: stream, datagram */
3228 tem = Fplist_get (contact, QCtype);
3229 if (NILP (tem))
3230 socktype = SOCK_STREAM;
3231 #ifdef DATAGRAM_SOCKETS
3232 else if (EQ (tem, Qdatagram))
3233 socktype = SOCK_DGRAM;
3234 #endif
3235 #ifdef HAVE_SEQPACKET
3236 else if (EQ (tem, Qseqpacket))
3237 socktype = SOCK_SEQPACKET;
3238 #endif
3239 else
3240 error ("Unsupported connection type");
3241
3242 /* :server BOOL */
3243 tem = Fplist_get (contact, QCserver);
3244 if (!NILP (tem))
3245 {
3246 /* Don't support network sockets when non-blocking mode is
3247 not available, since a blocked Emacs is not useful. */
3248 #if !defined(O_NONBLOCK) && !defined(O_NDELAY)
3249 error ("Network servers not supported");
3250 #else
3251 is_server = 1;
3252 if (INTEGERP (tem))
3253 backlog = XINT (tem);
3254 #endif
3255 }
3256
3257 /* Make QCaddress an alias for :local (server) or :remote (client). */
3258 QCaddress = is_server ? QClocal : QCremote;
3259
3260 /* :nowait BOOL */
3261 if (!is_server && socktype != SOCK_DGRAM
3262 && (tem = Fplist_get (contact, QCnowait), !NILP (tem)))
3263 {
3264 #ifndef NON_BLOCKING_CONNECT
3265 error ("Non-blocking connect not supported");
3266 #else
3267 is_non_blocking_client = 1;
3268 #endif
3269 }
3270
3271 name = Fplist_get (contact, QCname);
3272 buffer = Fplist_get (contact, QCbuffer);
3273 filter = Fplist_get (contact, QCfilter);
3274 sentinel = Fplist_get (contact, QCsentinel);
3275
3276 CHECK_STRING (name);
3277
3278 /* Initialize addrinfo structure in case we don't use getaddrinfo. */
3279 ai.ai_socktype = socktype;
3280 ai.ai_protocol = 0;
3281 ai.ai_next = NULL;
3282 res = &ai;
3283
3284 /* :local ADDRESS or :remote ADDRESS */
3285 address = Fplist_get (contact, QCaddress);
3286 if (!NILP (address))
3287 {
3288 host = service = Qnil;
3289
3290 if (!(ai.ai_addrlen = get_lisp_to_sockaddr_size (address, &family)))
3291 error ("Malformed :address");
3292 ai.ai_family = family;
3293 ai.ai_addr = alloca (ai.ai_addrlen);
3294 conv_lisp_to_sockaddr (family, address, ai.ai_addr, ai.ai_addrlen);
3295 goto open_socket;
3296 }
3297
3298 /* :family FAMILY -- nil (for Inet), local, or integer. */
3299 tem = Fplist_get (contact, QCfamily);
3300 if (NILP (tem))
3301 {
3302 #if defined(HAVE_GETADDRINFO) && defined(AF_INET6)
3303 family = AF_UNSPEC;
3304 #else
3305 family = AF_INET;
3306 #endif
3307 }
3308 #ifdef HAVE_LOCAL_SOCKETS
3309 else if (EQ (tem, Qlocal))
3310 family = AF_LOCAL;
3311 #endif
3312 #ifdef AF_INET6
3313 else if (EQ (tem, Qipv6))
3314 family = AF_INET6;
3315 #endif
3316 else if (EQ (tem, Qipv4))
3317 family = AF_INET;
3318 else if (INTEGERP (tem))
3319 family = XINT (tem);
3320 else
3321 error ("Unknown address family");
3322
3323 ai.ai_family = family;
3324
3325 /* :service SERVICE -- string, integer (port number), or t (random port). */
3326 service = Fplist_get (contact, QCservice);
3327
3328 /* :host HOST -- hostname, ip address, or 'local for localhost. */
3329 host = Fplist_get (contact, QChost);
3330 if (!NILP (host))
3331 {
3332 if (EQ (host, Qlocal))
3333 host = build_string ("localhost");
3334 CHECK_STRING (host);
3335 }
3336
3337 #ifdef HAVE_LOCAL_SOCKETS
3338 if (family == AF_LOCAL)
3339 {
3340 if (!NILP (host))
3341 {
3342 message (":family local ignores the :host \"%s\" property",
3343 SDATA (host));
3344 contact = Fplist_put (contact, QChost, Qnil);
3345 host = Qnil;
3346 }
3347 CHECK_STRING (service);
3348 bzero (&address_un, sizeof address_un);
3349 address_un.sun_family = AF_LOCAL;
3350 strncpy (address_un.sun_path, SDATA (service), sizeof address_un.sun_path);
3351 ai.ai_addr = (struct sockaddr *) &address_un;
3352 ai.ai_addrlen = sizeof address_un;
3353 goto open_socket;
3354 }
3355 #endif
3356
3357 /* Slow down polling to every ten seconds.
3358 Some kernels have a bug which causes retrying connect to fail
3359 after a connect. Polling can interfere with gethostbyname too. */
3360 #ifdef POLL_FOR_INPUT
3361 if (socktype != SOCK_DGRAM)
3362 {
3363 record_unwind_protect (unwind_stop_other_atimers, Qnil);
3364 bind_polling_period (10);
3365 }
3366 #endif
3367
3368 #ifdef HAVE_GETADDRINFO
3369 /* If we have a host, use getaddrinfo to resolve both host and service.
3370 Otherwise, use getservbyname to lookup the service. */
3371 if (!NILP (host))
3372 {
3373
3374 /* SERVICE can either be a string or int.
3375 Convert to a C string for later use by getaddrinfo. */
3376 if (EQ (service, Qt))
3377 portstring = "0";
3378 else if (INTEGERP (service))
3379 {
3380 sprintf (portbuf, "%ld", (long) XINT (service));
3381 portstring = portbuf;
3382 }
3383 else
3384 {
3385 CHECK_STRING (service);
3386 portstring = SDATA (service);
3387 }
3388
3389 immediate_quit = 1;
3390 QUIT;
3391 memset (&hints, 0, sizeof (hints));
3392 hints.ai_flags = 0;
3393 hints.ai_family = family;
3394 hints.ai_socktype = socktype;
3395 hints.ai_protocol = 0;
3396
3397 #ifdef HAVE_RES_INIT
3398 res_init ();
3399 #endif
3400
3401 ret = getaddrinfo (SDATA (host), portstring, &hints, &res);
3402 if (ret)
3403 #ifdef HAVE_GAI_STRERROR
3404 error ("%s/%s %s", SDATA (host), portstring, gai_strerror(ret));
3405 #else
3406 error ("%s/%s getaddrinfo error %d", SDATA (host), portstring, ret);
3407 #endif
3408 immediate_quit = 0;
3409
3410 goto open_socket;
3411 }
3412 #endif /* HAVE_GETADDRINFO */
3413
3414 /* We end up here if getaddrinfo is not defined, or in case no hostname
3415 has been specified (e.g. for a local server process). */
3416
3417 if (EQ (service, Qt))
3418 port = 0;
3419 else if (INTEGERP (service))
3420 port = htons ((unsigned short) XINT (service));
3421 else
3422 {
3423 struct servent *svc_info;
3424 CHECK_STRING (service);
3425 svc_info = getservbyname (SDATA (service),
3426 (socktype == SOCK_DGRAM ? "udp" : "tcp"));
3427 if (svc_info == 0)
3428 error ("Unknown service: %s", SDATA (service));
3429 port = svc_info->s_port;
3430 }
3431
3432 bzero (&address_in, sizeof address_in);
3433 address_in.sin_family = family;
3434 address_in.sin_addr.s_addr = INADDR_ANY;
3435 address_in.sin_port = port;
3436
3437 #ifndef HAVE_GETADDRINFO
3438 if (!NILP (host))
3439 {
3440 struct hostent *host_info_ptr;
3441
3442 /* gethostbyname may fail with TRY_AGAIN, but we don't honour that,
3443 as it may `hang' Emacs for a very long time. */
3444 immediate_quit = 1;
3445 QUIT;
3446
3447 #ifdef HAVE_RES_INIT
3448 res_init ();
3449 #endif
3450
3451 host_info_ptr = gethostbyname (SDATA (host));
3452 immediate_quit = 0;
3453
3454 if (host_info_ptr)
3455 {
3456 bcopy (host_info_ptr->h_addr, (char *) &address_in.sin_addr,
3457 host_info_ptr->h_length);
3458 family = host_info_ptr->h_addrtype;
3459 address_in.sin_family = family;
3460 }
3461 else
3462 /* Attempt to interpret host as numeric inet address */
3463 {
3464 unsigned long numeric_addr;
3465 numeric_addr = inet_addr ((char *) SDATA (host));
3466 if (numeric_addr == -1)
3467 error ("Unknown host \"%s\"", SDATA (host));
3468
3469 bcopy ((char *)&numeric_addr, (char *) &address_in.sin_addr,
3470 sizeof (address_in.sin_addr));
3471 }
3472
3473 }
3474 #endif /* not HAVE_GETADDRINFO */
3475
3476 ai.ai_family = family;
3477 ai.ai_addr = (struct sockaddr *) &address_in;
3478 ai.ai_addrlen = sizeof address_in;
3479
3480 open_socket:
3481
3482 /* Do this in case we never enter the for-loop below. */
3483 count1 = SPECPDL_INDEX ();
3484 s = -1;
3485
3486 for (lres = res; lres; lres = lres->ai_next)
3487 {
3488 int optn, optbits;
3489
3490 retry_connect:
3491
3492 s = socket (lres->ai_family, lres->ai_socktype, lres->ai_protocol);
3493 if (s < 0)
3494 {
3495 xerrno = errno;
3496 continue;
3497 }
3498
3499 #ifdef DATAGRAM_SOCKETS
3500 if (!is_server && socktype == SOCK_DGRAM)
3501 break;
3502 #endif /* DATAGRAM_SOCKETS */
3503
3504 #ifdef NON_BLOCKING_CONNECT
3505 if (is_non_blocking_client)
3506 {
3507 #ifdef O_NONBLOCK
3508 ret = fcntl (s, F_SETFL, O_NONBLOCK);
3509 #else
3510 ret = fcntl (s, F_SETFL, O_NDELAY);
3511 #endif
3512 if (ret < 0)
3513 {
3514 xerrno = errno;
3515 emacs_close (s);
3516 s = -1;
3517 continue;
3518 }
3519 }
3520 #endif
3521
3522 /* Make us close S if quit. */
3523 record_unwind_protect (close_file_unwind, make_number (s));
3524
3525 /* Parse network options in the arg list.
3526 We simply ignore anything which isn't a known option (including other keywords).
3527 An error is signaled if setting a known option fails. */
3528 for (optn = optbits = 0; optn < nargs-1; optn += 2)
3529 optbits |= set_socket_option (s, args[optn], args[optn+1]);
3530
3531 if (is_server)
3532 {
3533 /* Configure as a server socket. */
3534
3535 /* SO_REUSEADDR = 1 is default for server sockets; must specify
3536 explicit :reuseaddr key to override this. */
3537 #ifdef HAVE_LOCAL_SOCKETS
3538 if (family != AF_LOCAL)
3539 #endif
3540 if (!(optbits & (1 << OPIX_REUSEADDR)))
3541 {
3542 int optval = 1;
3543 if (setsockopt (s, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof optval))
3544 report_file_error ("Cannot set reuse option on server socket", Qnil);
3545 }
3546
3547 if (bind (s, lres->ai_addr, lres->ai_addrlen))
3548 report_file_error ("Cannot bind server socket", Qnil);
3549
3550 #ifdef HAVE_GETSOCKNAME
3551 if (EQ (service, Qt))
3552 {
3553 struct sockaddr_in sa1;
3554 int len1 = sizeof (sa1);
3555 if (getsockname (s, (struct sockaddr *)&sa1, &len1) == 0)
3556 {
3557 ((struct sockaddr_in *)(lres->ai_addr))->sin_port = sa1.sin_port;
3558 service = make_number (ntohs (sa1.sin_port));
3559 contact = Fplist_put (contact, QCservice, service);
3560 }
3561 }
3562 #endif
3563
3564 if (socktype != SOCK_DGRAM && listen (s, backlog))
3565 report_file_error ("Cannot listen on server socket", Qnil);
3566
3567 break;
3568 }
3569
3570 immediate_quit = 1;
3571 QUIT;
3572
3573 ret = connect (s, lres->ai_addr, lres->ai_addrlen);
3574 xerrno = errno;
3575
3576 if (ret == 0 || xerrno == EISCONN)
3577 {
3578 /* The unwind-protect will be discarded afterwards.
3579 Likewise for immediate_quit. */
3580 break;
3581 }
3582
3583 #ifdef NON_BLOCKING_CONNECT
3584 #ifdef EINPROGRESS
3585 if (is_non_blocking_client && xerrno == EINPROGRESS)
3586 break;
3587 #else
3588 #ifdef EWOULDBLOCK
3589 if (is_non_blocking_client && xerrno == EWOULDBLOCK)
3590 break;
3591 #endif
3592 #endif
3593 #endif
3594
3595 #ifndef WINDOWSNT
3596 if (xerrno == EINTR)
3597 {
3598 /* Unlike most other syscalls connect() cannot be called
3599 again. (That would return EALREADY.) The proper way to
3600 wait for completion is select(). */
3601 int sc, len;
3602 SELECT_TYPE fdset;
3603 retry_select:
3604 FD_ZERO (&fdset);
3605 FD_SET (s, &fdset);
3606 QUIT;
3607 sc = select (s + 1, (SELECT_TYPE *)0, &fdset, (SELECT_TYPE *)0,
3608 (EMACS_TIME *)0);
3609 if (sc == -1)
3610 {
3611 if (errno == EINTR)
3612 goto retry_select;
3613 else
3614 report_file_error ("select failed", Qnil);
3615 }
3616 eassert (sc > 0);
3617
3618 len = sizeof xerrno;
3619 eassert (FD_ISSET (s, &fdset));
3620 if (getsockopt (s, SOL_SOCKET, SO_ERROR, &xerrno, &len) == -1)
3621 report_file_error ("getsockopt failed", Qnil);
3622 if (xerrno)
3623 errno = xerrno, report_file_error ("error during connect", Qnil);
3624 else
3625 break;
3626 }
3627 #endif /* !WINDOWSNT */
3628
3629 immediate_quit = 0;
3630
3631 /* Discard the unwind protect closing S. */
3632 specpdl_ptr = specpdl + count1;
3633 emacs_close (s);
3634 s = -1;
3635
3636 #ifdef WINDOWSNT
3637 if (xerrno == EINTR)
3638 goto retry_connect;
3639 #endif
3640 }
3641
3642 if (s >= 0)
3643 {
3644 #ifdef DATAGRAM_SOCKETS
3645 if (socktype == SOCK_DGRAM)
3646 {
3647 if (datagram_address[s].sa)
3648 abort ();
3649 datagram_address[s].sa = (struct sockaddr *) xmalloc (lres->ai_addrlen);
3650 datagram_address[s].len = lres->ai_addrlen;
3651 if (is_server)
3652 {
3653 Lisp_Object remote;
3654 bzero (datagram_address[s].sa, lres->ai_addrlen);
3655 if (remote = Fplist_get (contact, QCremote), !NILP (remote))
3656 {
3657 int rfamily, rlen;
3658 rlen = get_lisp_to_sockaddr_size (remote, &rfamily);
3659 if (rfamily == lres->ai_family && rlen == lres->ai_addrlen)
3660 conv_lisp_to_sockaddr (rfamily, remote,
3661 datagram_address[s].sa, rlen);
3662 }
3663 }
3664 else
3665 bcopy (lres->ai_addr, datagram_address[s].sa, lres->ai_addrlen);
3666 }
3667 #endif
3668 contact = Fplist_put (contact, QCaddress,
3669 conv_sockaddr_to_lisp (lres->ai_addr, lres->ai_addrlen));
3670 #ifdef HAVE_GETSOCKNAME
3671 if (!is_server)
3672 {
3673 struct sockaddr_in sa1;
3674 int len1 = sizeof (sa1);
3675 if (getsockname (s, (struct sockaddr *)&sa1, &len1) == 0)
3676 contact = Fplist_put (contact, QClocal,
3677 conv_sockaddr_to_lisp ((struct sockaddr *)&sa1, len1));
3678 }
3679 #endif
3680 }
3681
3682 immediate_quit = 0;
3683
3684 #ifdef HAVE_GETADDRINFO
3685 if (res != &ai)
3686 {
3687 BLOCK_INPUT;
3688 freeaddrinfo (res);
3689 UNBLOCK_INPUT;
3690 }
3691 #endif
3692
3693 /* Discard the unwind protect for closing S, if any. */
3694 specpdl_ptr = specpdl + count1;
3695
3696 /* Unwind bind_polling_period and request_sigio. */
3697 unbind_to (count, Qnil);
3698
3699 if (s < 0)
3700 {
3701 /* If non-blocking got this far - and failed - assume non-blocking is
3702 not supported after all. This is probably a wrong assumption, but
3703 the normal blocking calls to open-network-stream handles this error
3704 better. */
3705 if (is_non_blocking_client)
3706 return Qnil;
3707
3708 errno = xerrno;
3709 if (is_server)
3710 report_file_error ("make server process failed", contact);
3711 else
3712 report_file_error ("make client process failed", contact);
3713 }
3714
3715 inch = s;
3716 outch = s;
3717
3718 if (!NILP (buffer))
3719 buffer = Fget_buffer_create (buffer);
3720 proc = make_process (name);
3721
3722 chan_process[inch] = proc;
3723
3724 #ifdef O_NONBLOCK
3725 fcntl (inch, F_SETFL, O_NONBLOCK);
3726 #else
3727 #ifdef O_NDELAY
3728 fcntl (inch, F_SETFL, O_NDELAY);
3729 #endif
3730 #endif
3731
3732 p = XPROCESS (proc);
3733
3734 p->childp = contact;
3735 p->plist = Fcopy_sequence (Fplist_get (contact, QCplist));
3736 p->type = Qnetwork;
3737
3738 p->buffer = buffer;
3739 p->sentinel = sentinel;
3740 p->filter = filter;
3741 p->log = Fplist_get (contact, QClog);
3742 if (tem = Fplist_get (contact, QCnoquery), !NILP (tem))
3743 p->kill_without_query = 1;
3744 if ((tem = Fplist_get (contact, QCstop), !NILP (tem)))
3745 p->command = Qt;
3746 p->pid = 0;
3747 p->infd = inch;
3748 p->outfd = outch;
3749 if (is_server && socktype != SOCK_DGRAM)
3750 p->status = Qlisten;
3751
3752 /* Make the process marker point into the process buffer (if any). */
3753 if (BUFFERP (buffer))
3754 set_marker_both (p->mark, buffer,
3755 BUF_ZV (XBUFFER (buffer)),
3756 BUF_ZV_BYTE (XBUFFER (buffer)));
3757
3758 #ifdef NON_BLOCKING_CONNECT
3759 if (is_non_blocking_client)
3760 {
3761 /* We may get here if connect did succeed immediately. However,
3762 in that case, we still need to signal this like a non-blocking
3763 connection. */
3764 p->status = Qconnect;
3765 if (!FD_ISSET (inch, &connect_wait_mask))
3766 {
3767 FD_SET (inch, &connect_wait_mask);
3768 num_pending_connects++;
3769 }
3770 }
3771 else
3772 #endif
3773 /* A server may have a client filter setting of Qt, but it must
3774 still listen for incoming connects unless it is stopped. */
3775 if ((!EQ (p->filter, Qt) && !EQ (p->command, Qt))
3776 || (EQ (p->status, Qlisten) && NILP (p->command)))
3777 {
3778 FD_SET (inch, &input_wait_mask);
3779 FD_SET (inch, &non_keyboard_wait_mask);
3780 }
3781
3782 if (inch > max_process_desc)
3783 max_process_desc = inch;
3784
3785 tem = Fplist_member (contact, QCcoding);
3786 if (!NILP (tem) && (!CONSP (tem) || !CONSP (XCDR (tem))))
3787 tem = Qnil; /* No error message (too late!). */
3788
3789 {
3790 /* Setup coding systems for communicating with the network stream. */
3791 struct gcpro gcpro1;
3792 /* Qt denotes we have not yet called Ffind_operation_coding_system. */
3793 Lisp_Object coding_systems = Qt;
3794 Lisp_Object args[5], val;
3795
3796 if (!NILP (tem))
3797 {
3798 val = XCAR (XCDR (tem));
3799 if (CONSP (val))
3800 val = XCAR (val);
3801 }
3802 else if (!NILP (Vcoding_system_for_read))
3803 val = Vcoding_system_for_read;
3804 else if ((!NILP (buffer) && NILP (XBUFFER (buffer)->enable_multibyte_characters))
3805 || (NILP (buffer) && NILP (buffer_defaults.enable_multibyte_characters)))
3806 /* We dare not decode end-of-line format by setting VAL to
3807 Qraw_text, because the existing Emacs Lisp libraries
3808 assume that they receive bare code including a sequene of
3809 CR LF. */
3810 val = Qnil;
3811 else
3812 {
3813 if (NILP (host) || NILP (service))
3814 coding_systems = Qnil;
3815 else
3816 {
3817 args[0] = Qopen_network_stream, args[1] = name,
3818 args[2] = buffer, args[3] = host, args[4] = service;
3819 GCPRO1 (proc);
3820 coding_systems = Ffind_operation_coding_system (5, args);
3821 UNGCPRO;
3822 }
3823 if (CONSP (coding_systems))
3824 val = XCAR (coding_systems);
3825 else if (CONSP (Vdefault_process_coding_system))
3826 val = XCAR (Vdefault_process_coding_system);
3827 else
3828 val = Qnil;
3829 }
3830 p->decode_coding_system = val;
3831
3832 if (!NILP (tem))
3833 {
3834 val = XCAR (XCDR (tem));
3835 if (CONSP (val))
3836 val = XCDR (val);
3837 }
3838 else if (!NILP (Vcoding_system_for_write))
3839 val = Vcoding_system_for_write;
3840 else if (NILP (current_buffer->enable_multibyte_characters))
3841 val = Qnil;
3842 else
3843 {
3844 if (EQ (coding_systems, Qt))
3845 {
3846 if (NILP (host) || NILP (service))
3847 coding_systems = Qnil;
3848 else
3849 {
3850 args[0] = Qopen_network_stream, args[1] = name,
3851 args[2] = buffer, args[3] = host, args[4] = service;
3852 GCPRO1 (proc);
3853 coding_systems = Ffind_operation_coding_system (5, args);
3854 UNGCPRO;
3855 }
3856 }
3857 if (CONSP (coding_systems))
3858 val = XCDR (coding_systems);
3859 else if (CONSP (Vdefault_process_coding_system))
3860 val = XCDR (Vdefault_process_coding_system);
3861 else
3862 val = Qnil;
3863 }
3864 p->encode_coding_system = val;
3865 }
3866 setup_process_coding_systems (proc);
3867
3868 p->decoding_buf = make_uninit_string (0);
3869 p->decoding_carryover = 0;
3870 p->encoding_buf = make_uninit_string (0);
3871
3872 p->inherit_coding_system_flag
3873 = !(!NILP (tem) || NILP (buffer) || !inherit_process_coding_system);
3874
3875 UNGCPRO;
3876 return proc;
3877 }
3878 #endif /* HAVE_SOCKETS */
3879
3880 \f
3881 #if defined(HAVE_SOCKETS) && defined(HAVE_NET_IF_H) && defined(HAVE_SYS_IOCTL_H)
3882
3883 #ifdef SIOCGIFCONF
3884 DEFUN ("network-interface-list", Fnetwork_interface_list, Snetwork_interface_list, 0, 0, 0,
3885 doc: /* Return an alist of all network interfaces and their network address.
3886 Each element is a cons, the car of which is a string containing the
3887 interface name, and the cdr is the network address in internal
3888 format; see the description of ADDRESS in `make-network-process'. */)
3889 ()
3890 {
3891 struct ifconf ifconf;
3892 struct ifreq *ifreqs = NULL;
3893 int ifaces = 0;
3894 int buf_size, s;
3895 Lisp_Object res;
3896
3897 s = socket (AF_INET, SOCK_STREAM, 0);
3898 if (s < 0)
3899 return Qnil;
3900
3901 again:
3902 ifaces += 25;
3903 buf_size = ifaces * sizeof(ifreqs[0]);
3904 ifreqs = (struct ifreq *)xrealloc(ifreqs, buf_size);
3905 if (!ifreqs)
3906 {
3907 close (s);
3908 return Qnil;
3909 }
3910
3911 ifconf.ifc_len = buf_size;
3912 ifconf.ifc_req = ifreqs;
3913 if (ioctl (s, SIOCGIFCONF, &ifconf))
3914 {
3915 close (s);
3916 return Qnil;
3917 }
3918
3919 if (ifconf.ifc_len == buf_size)
3920 goto again;
3921
3922 close (s);
3923 ifaces = ifconf.ifc_len / sizeof (ifreqs[0]);
3924
3925 res = Qnil;
3926 while (--ifaces >= 0)
3927 {
3928 struct ifreq *ifq = &ifreqs[ifaces];
3929 char namebuf[sizeof (ifq->ifr_name) + 1];
3930 if (ifq->ifr_addr.sa_family != AF_INET)
3931 continue;
3932 bcopy (ifq->ifr_name, namebuf, sizeof (ifq->ifr_name));
3933 namebuf[sizeof (ifq->ifr_name)] = 0;
3934 res = Fcons (Fcons (build_string (namebuf),
3935 conv_sockaddr_to_lisp (&ifq->ifr_addr,
3936 sizeof (struct sockaddr))),
3937 res);
3938 }
3939
3940 return res;
3941 }
3942 #endif /* SIOCGIFCONF */
3943
3944 #if defined(SIOCGIFADDR) || defined(SIOCGIFHWADDR) || defined(SIOCGIFFLAGS)
3945
3946 struct ifflag_def {
3947 int flag_bit;
3948 const char *flag_sym;
3949 };
3950
3951 static const struct ifflag_def ifflag_table[] = {
3952 #ifdef IFF_UP
3953 { IFF_UP, "up" },
3954 #endif
3955 #ifdef IFF_BROADCAST
3956 { IFF_BROADCAST, "broadcast" },
3957 #endif
3958 #ifdef IFF_DEBUG
3959 { IFF_DEBUG, "debug" },
3960 #endif
3961 #ifdef IFF_LOOPBACK
3962 { IFF_LOOPBACK, "loopback" },
3963 #endif
3964 #ifdef IFF_POINTOPOINT
3965 { IFF_POINTOPOINT, "pointopoint" },
3966 #endif
3967 #ifdef IFF_RUNNING
3968 { IFF_RUNNING, "running" },
3969 #endif
3970 #ifdef IFF_NOARP
3971 { IFF_NOARP, "noarp" },
3972 #endif
3973 #ifdef IFF_PROMISC
3974 { IFF_PROMISC, "promisc" },
3975 #endif
3976 #ifdef IFF_NOTRAILERS
3977 { IFF_NOTRAILERS, "notrailers" },
3978 #endif
3979 #ifdef IFF_ALLMULTI
3980 { IFF_ALLMULTI, "allmulti" },
3981 #endif
3982 #ifdef IFF_MASTER
3983 { IFF_MASTER, "master" },
3984 #endif
3985 #ifdef IFF_SLAVE
3986 { IFF_SLAVE, "slave" },
3987 #endif
3988 #ifdef IFF_MULTICAST
3989 { IFF_MULTICAST, "multicast" },
3990 #endif
3991 #ifdef IFF_PORTSEL
3992 { IFF_PORTSEL, "portsel" },
3993 #endif
3994 #ifdef IFF_AUTOMEDIA
3995 { IFF_AUTOMEDIA, "automedia" },
3996 #endif
3997 #ifdef IFF_DYNAMIC
3998 { IFF_DYNAMIC, "dynamic" },
3999 #endif
4000 #ifdef IFF_OACTIVE
4001 { IFF_OACTIVE, "oactive" }, /* OpenBSD: transmission in progress */
4002 #endif
4003 #ifdef IFF_SIMPLEX
4004 { IFF_SIMPLEX, "simplex" }, /* OpenBSD: can't hear own transmissions */
4005 #endif
4006 #ifdef IFF_LINK0
4007 { IFF_LINK0, "link0" }, /* OpenBSD: per link layer defined bit */
4008 #endif
4009 #ifdef IFF_LINK1
4010 { IFF_LINK1, "link1" }, /* OpenBSD: per link layer defined bit */
4011 #endif
4012 #ifdef IFF_LINK2
4013 { IFF_LINK2, "link2" }, /* OpenBSD: per link layer defined bit */
4014 #endif
4015 { 0, 0 }
4016 };
4017
4018 DEFUN ("network-interface-info", Fnetwork_interface_info, Snetwork_interface_info, 1, 1, 0,
4019 doc: /* Return information about network interface named IFNAME.
4020 The return value is a list (ADDR BCAST NETMASK HWADDR FLAGS),
4021 where ADDR is the layer 3 address, BCAST is the layer 3 broadcast address,
4022 NETMASK is the layer 3 network mask, HWADDR is the layer 2 addres, and
4023 FLAGS is the current flags of the interface. */)
4024 (ifname)
4025 Lisp_Object ifname;
4026 {
4027 struct ifreq rq;
4028 Lisp_Object res = Qnil;
4029 Lisp_Object elt;
4030 int s;
4031 int any = 0;
4032
4033 CHECK_STRING (ifname);
4034
4035 bzero (rq.ifr_name, sizeof rq.ifr_name);
4036 strncpy (rq.ifr_name, SDATA (ifname), sizeof (rq.ifr_name));
4037
4038 s = socket (AF_INET, SOCK_STREAM, 0);
4039 if (s < 0)
4040 return Qnil;
4041
4042 elt = Qnil;
4043 #if defined(SIOCGIFFLAGS) && defined(HAVE_STRUCT_IFREQ_IFR_FLAGS)
4044 if (ioctl (s, SIOCGIFFLAGS, &rq) == 0)
4045 {
4046 int flags = rq.ifr_flags;
4047 const struct ifflag_def *fp;
4048 int fnum;
4049
4050 any++;
4051 for (fp = ifflag_table; flags != 0 && fp->flag_sym; fp++)
4052 {
4053 if (flags & fp->flag_bit)
4054 {
4055 elt = Fcons (intern (fp->flag_sym), elt);
4056 flags -= fp->flag_bit;
4057 }
4058 }
4059 for (fnum = 0; flags && fnum < 32; fnum++)
4060 {
4061 if (flags & (1 << fnum))
4062 {
4063 elt = Fcons (make_number (fnum), elt);
4064 }
4065 }
4066 }
4067 #endif
4068 res = Fcons (elt, res);
4069
4070 elt = Qnil;
4071 #if defined(SIOCGIFHWADDR) && defined(HAVE_STRUCT_IFREQ_IFR_HWADDR)
4072 if (ioctl (s, SIOCGIFHWADDR, &rq) == 0)
4073 {
4074 Lisp_Object hwaddr = Fmake_vector (make_number (6), Qnil);
4075 register struct Lisp_Vector *p = XVECTOR (hwaddr);
4076 int n;
4077
4078 any++;
4079 for (n = 0; n < 6; n++)
4080 p->contents[n] = make_number (((unsigned char *)&rq.ifr_hwaddr.sa_data[0])[n]);
4081 elt = Fcons (make_number (rq.ifr_hwaddr.sa_family), hwaddr);
4082 }
4083 #endif
4084 res = Fcons (elt, res);
4085
4086 elt = Qnil;
4087 #if defined(SIOCGIFNETMASK) && (defined(HAVE_STRUCT_IFREQ_IFR_NETMASK) || defined(HAVE_STRUCT_IFREQ_IFR_ADDR))
4088 if (ioctl (s, SIOCGIFNETMASK, &rq) == 0)
4089 {
4090 any++;
4091 #ifdef HAVE_STRUCT_IFREQ_IFR_NETMASK
4092 elt = conv_sockaddr_to_lisp (&rq.ifr_netmask, sizeof (rq.ifr_netmask));
4093 #else
4094 elt = conv_sockaddr_to_lisp (&rq.ifr_addr, sizeof (rq.ifr_addr));
4095 #endif
4096 }
4097 #endif
4098 res = Fcons (elt, res);
4099
4100 elt = Qnil;
4101 #if defined(SIOCGIFBRDADDR) && defined(HAVE_STRUCT_IFREQ_IFR_BROADADDR)
4102 if (ioctl (s, SIOCGIFBRDADDR, &rq) == 0)
4103 {
4104 any++;
4105 elt = conv_sockaddr_to_lisp (&rq.ifr_broadaddr, sizeof (rq.ifr_broadaddr));
4106 }
4107 #endif
4108 res = Fcons (elt, res);
4109
4110 elt = Qnil;
4111 #if defined(SIOCGIFADDR) && defined(HAVE_STRUCT_IFREQ_IFR_ADDR)
4112 if (ioctl (s, SIOCGIFADDR, &rq) == 0)
4113 {
4114 any++;
4115 elt = conv_sockaddr_to_lisp (&rq.ifr_addr, sizeof (rq.ifr_addr));
4116 }
4117 #endif
4118 res = Fcons (elt, res);
4119
4120 close (s);
4121
4122 return any ? res : Qnil;
4123 }
4124 #endif
4125 #endif /* HAVE_SOCKETS */
4126
4127 /* Turn off input and output for process PROC. */
4128
4129 void
4130 deactivate_process (Lisp_Object proc)
4131 {
4132 register int inchannel, outchannel;
4133 register struct Lisp_Process *p = XPROCESS (proc);
4134
4135 inchannel = p->infd;
4136 outchannel = p->outfd;
4137
4138 #ifdef ADAPTIVE_READ_BUFFERING
4139 if (p->read_output_delay > 0)
4140 {
4141 if (--process_output_delay_count < 0)
4142 process_output_delay_count = 0;
4143 p->read_output_delay = 0;
4144 p->read_output_skip = 0;
4145 }
4146 #endif
4147
4148 if (inchannel >= 0)
4149 {
4150 /* Beware SIGCHLD hereabouts. */
4151 flush_pending_output (inchannel);
4152 emacs_close (inchannel);
4153 if (outchannel >= 0 && outchannel != inchannel)
4154 emacs_close (outchannel);
4155
4156 p->infd = -1;
4157 p->outfd = -1;
4158 #ifdef DATAGRAM_SOCKETS
4159 if (DATAGRAM_CHAN_P (inchannel))
4160 {
4161 xfree (datagram_address[inchannel].sa);
4162 datagram_address[inchannel].sa = 0;
4163 datagram_address[inchannel].len = 0;
4164 }
4165 #endif
4166 chan_process[inchannel] = Qnil;
4167 FD_CLR (inchannel, &input_wait_mask);
4168 FD_CLR (inchannel, &non_keyboard_wait_mask);
4169 #ifdef NON_BLOCKING_CONNECT
4170 if (FD_ISSET (inchannel, &connect_wait_mask))
4171 {
4172 FD_CLR (inchannel, &connect_wait_mask);
4173 if (--num_pending_connects < 0)
4174 abort ();
4175 }
4176 #endif
4177 if (inchannel == max_process_desc)
4178 {
4179 int i;
4180 /* We just closed the highest-numbered process input descriptor,
4181 so recompute the highest-numbered one now. */
4182 max_process_desc = 0;
4183 for (i = 0; i < MAXDESC; i++)
4184 if (!NILP (chan_process[i]))
4185 max_process_desc = i;
4186 }
4187 }
4188 }
4189
4190 /* Close all descriptors currently in use for communication
4191 with subprocess. This is used in a newly-forked subprocess
4192 to get rid of irrelevant descriptors. */
4193
4194 void
4195 close_process_descs (void)
4196 {
4197 #ifndef WINDOWSNT
4198 int i;
4199 for (i = 0; i < MAXDESC; i++)
4200 {
4201 Lisp_Object process;
4202 process = chan_process[i];
4203 if (!NILP (process))
4204 {
4205 int in = XPROCESS (process)->infd;
4206 int out = XPROCESS (process)->outfd;
4207 if (in >= 0)
4208 emacs_close (in);
4209 if (out >= 0 && in != out)
4210 emacs_close (out);
4211 }
4212 }
4213 #endif
4214 }
4215 \f
4216 DEFUN ("accept-process-output", Faccept_process_output, Saccept_process_output,
4217 0, 4, 0,
4218 doc: /* Allow any pending output from subprocesses to be read by Emacs.
4219 It is read into the process' buffers or given to their filter functions.
4220 Non-nil arg PROCESS means do not return until some output has been received
4221 from PROCESS.
4222
4223 Non-nil second arg SECONDS and third arg MILLISEC are number of seconds
4224 and milliseconds to wait; return after that much time whether or not
4225 there is any subprocess output. If SECONDS is a floating point number,
4226 it specifies a fractional number of seconds to wait.
4227 The MILLISEC argument is obsolete and should be avoided.
4228
4229 If optional fourth arg JUST-THIS-ONE is non-nil, only accept output
4230 from PROCESS, suspending reading output from other processes.
4231 If JUST-THIS-ONE is an integer, don't run any timers either.
4232 Return non-nil if we received any output before the timeout expired. */)
4233 (process, seconds, millisec, just_this_one)
4234 register Lisp_Object process, seconds, millisec, just_this_one;
4235 {
4236 int secs, usecs = 0;
4237
4238 if (! NILP (process))
4239 CHECK_PROCESS (process);
4240 else
4241 just_this_one = Qnil;
4242
4243 if (!NILP (millisec))
4244 { /* Obsolete calling convention using integers rather than floats. */
4245 CHECK_NUMBER (millisec);
4246 if (NILP (seconds))
4247 seconds = make_float (XINT (millisec) / 1000.0);
4248 else
4249 {
4250 CHECK_NUMBER (seconds);
4251 seconds = make_float (XINT (millisec) / 1000.0 + XINT (seconds));
4252 }
4253 }
4254
4255 if (!NILP (seconds))
4256 {
4257 if (INTEGERP (seconds))
4258 secs = XINT (seconds);
4259 else if (FLOATP (seconds))
4260 {
4261 double timeout = XFLOAT_DATA (seconds);
4262 secs = (int) timeout;
4263 usecs = (int) ((timeout - (double) secs) * 1000000);
4264 }
4265 else
4266 wrong_type_argument (Qnumberp, seconds);
4267
4268 if (secs < 0 || (secs == 0 && usecs == 0))
4269 secs = -1, usecs = 0;
4270 }
4271 else
4272 secs = NILP (process) ? -1 : 0;
4273
4274 return
4275 (wait_reading_process_output (secs, usecs, 0, 0,
4276 Qnil,
4277 !NILP (process) ? XPROCESS (process) : NULL,
4278 NILP (just_this_one) ? 0 :
4279 !INTEGERP (just_this_one) ? 1 : -1)
4280 ? Qt : Qnil);
4281 }
4282
4283 /* Accept a connection for server process SERVER on CHANNEL. */
4284
4285 static int connect_counter = 0;
4286
4287 static void
4288 server_accept_connection (Lisp_Object server, int channel)
4289 {
4290 Lisp_Object proc, caller, name, buffer;
4291 Lisp_Object contact, host, service;
4292 struct Lisp_Process *ps= XPROCESS (server);
4293 struct Lisp_Process *p;
4294 int s;
4295 union u_sockaddr {
4296 struct sockaddr sa;
4297 struct sockaddr_in in;
4298 #ifdef AF_INET6
4299 struct sockaddr_in6 in6;
4300 #endif
4301 #ifdef HAVE_LOCAL_SOCKETS
4302 struct sockaddr_un un;
4303 #endif
4304 } saddr;
4305 int len = sizeof saddr;
4306
4307 s = accept (channel, &saddr.sa, &len);
4308
4309 if (s < 0)
4310 {
4311 int code = errno;
4312
4313 if (code == EAGAIN)
4314 return;
4315 #ifdef EWOULDBLOCK
4316 if (code == EWOULDBLOCK)
4317 return;
4318 #endif
4319
4320 if (!NILP (ps->log))
4321 call3 (ps->log, server, Qnil,
4322 concat3 (build_string ("accept failed with code"),
4323 Fnumber_to_string (make_number (code)),
4324 build_string ("\n")));
4325 return;
4326 }
4327
4328 connect_counter++;
4329
4330 /* Setup a new process to handle the connection. */
4331
4332 /* Generate a unique identification of the caller, and build contact
4333 information for this process. */
4334 host = Qt;
4335 service = Qnil;
4336 switch (saddr.sa.sa_family)
4337 {
4338 case AF_INET:
4339 {
4340 Lisp_Object args[5];
4341 unsigned char *ip = (unsigned char *)&saddr.in.sin_addr.s_addr;
4342 args[0] = build_string ("%d.%d.%d.%d");
4343 args[1] = make_number (*ip++);
4344 args[2] = make_number (*ip++);
4345 args[3] = make_number (*ip++);
4346 args[4] = make_number (*ip++);
4347 host = Fformat (5, args);
4348 service = make_number (ntohs (saddr.in.sin_port));
4349
4350 args[0] = build_string (" <%s:%d>");
4351 args[1] = host;
4352 args[2] = service;
4353 caller = Fformat (3, args);
4354 }
4355 break;
4356
4357 #ifdef AF_INET6
4358 case AF_INET6:
4359 {
4360 Lisp_Object args[9];
4361 uint16_t *ip6 = (uint16_t *)&saddr.in6.sin6_addr;
4362 int i;
4363 args[0] = build_string ("%x:%x:%x:%x:%x:%x:%x:%x");
4364 for (i = 0; i < 8; i++)
4365 args[i+1] = make_number (ntohs(ip6[i]));
4366 host = Fformat (9, args);
4367 service = make_number (ntohs (saddr.in.sin_port));
4368
4369 args[0] = build_string (" <[%s]:%d>");
4370 args[1] = host;
4371 args[2] = service;
4372 caller = Fformat (3, args);
4373 }
4374 break;
4375 #endif
4376
4377 #ifdef HAVE_LOCAL_SOCKETS
4378 case AF_LOCAL:
4379 #endif
4380 default:
4381 caller = Fnumber_to_string (make_number (connect_counter));
4382 caller = concat3 (build_string (" <"), caller, build_string (">"));
4383 break;
4384 }
4385
4386 /* Create a new buffer name for this process if it doesn't have a
4387 filter. The new buffer name is based on the buffer name or
4388 process name of the server process concatenated with the caller
4389 identification. */
4390
4391 if (!NILP (ps->filter) && !EQ (ps->filter, Qt))
4392 buffer = Qnil;
4393 else
4394 {
4395 buffer = ps->buffer;
4396 if (!NILP (buffer))
4397 buffer = Fbuffer_name (buffer);
4398 else
4399 buffer = ps->name;
4400 if (!NILP (buffer))
4401 {
4402 buffer = concat2 (buffer, caller);
4403 buffer = Fget_buffer_create (buffer);
4404 }
4405 }
4406
4407 /* Generate a unique name for the new server process. Combine the
4408 server process name with the caller identification. */
4409
4410 name = concat2 (ps->name, caller);
4411 proc = make_process (name);
4412
4413 chan_process[s] = proc;
4414
4415 #ifdef O_NONBLOCK
4416 fcntl (s, F_SETFL, O_NONBLOCK);
4417 #else
4418 #ifdef O_NDELAY
4419 fcntl (s, F_SETFL, O_NDELAY);
4420 #endif
4421 #endif
4422
4423 p = XPROCESS (proc);
4424
4425 /* Build new contact information for this setup. */
4426 contact = Fcopy_sequence (ps->childp);
4427 contact = Fplist_put (contact, QCserver, Qnil);
4428 contact = Fplist_put (contact, QChost, host);
4429 if (!NILP (service))
4430 contact = Fplist_put (contact, QCservice, service);
4431 contact = Fplist_put (contact, QCremote,
4432 conv_sockaddr_to_lisp (&saddr.sa, len));
4433 #ifdef HAVE_GETSOCKNAME
4434 len = sizeof saddr;
4435 if (getsockname (s, &saddr.sa, &len) == 0)
4436 contact = Fplist_put (contact, QClocal,
4437 conv_sockaddr_to_lisp (&saddr.sa, len));
4438 #endif
4439
4440 p->childp = contact;
4441 p->plist = Fcopy_sequence (ps->plist);
4442 p->type = Qnetwork;
4443
4444 p->buffer = buffer;
4445 p->sentinel = ps->sentinel;
4446 p->filter = ps->filter;
4447 p->command = Qnil;
4448 p->pid = 0;
4449 p->infd = s;
4450 p->outfd = s;
4451 p->status = Qrun;
4452
4453 /* Client processes for accepted connections are not stopped initially. */
4454 if (!EQ (p->filter, Qt))
4455 {
4456 FD_SET (s, &input_wait_mask);
4457 FD_SET (s, &non_keyboard_wait_mask);
4458 }
4459
4460 if (s > max_process_desc)
4461 max_process_desc = s;
4462
4463 /* Setup coding system for new process based on server process.
4464 This seems to be the proper thing to do, as the coding system
4465 of the new process should reflect the settings at the time the
4466 server socket was opened; not the current settings. */
4467
4468 p->decode_coding_system = ps->decode_coding_system;
4469 p->encode_coding_system = ps->encode_coding_system;
4470 setup_process_coding_systems (proc);
4471
4472 p->decoding_buf = make_uninit_string (0);
4473 p->decoding_carryover = 0;
4474 p->encoding_buf = make_uninit_string (0);
4475
4476 p->inherit_coding_system_flag
4477 = (NILP (buffer) ? 0 : ps->inherit_coding_system_flag);
4478
4479 if (!NILP (ps->log))
4480 call3 (ps->log, server, proc,
4481 concat3 (build_string ("accept from "),
4482 (STRINGP (host) ? host : build_string ("-")),
4483 build_string ("\n")));
4484
4485 if (!NILP (p->sentinel))
4486 exec_sentinel (proc,
4487 concat3 (build_string ("open from "),
4488 (STRINGP (host) ? host : build_string ("-")),
4489 build_string ("\n")));
4490 }
4491
4492 /* This variable is different from waiting_for_input in keyboard.c.
4493 It is used to communicate to a lisp process-filter/sentinel (via the
4494 function Fwaiting_for_user_input_p below) whether Emacs was waiting
4495 for user-input when that process-filter was called.
4496 waiting_for_input cannot be used as that is by definition 0 when
4497 lisp code is being evalled.
4498 This is also used in record_asynch_buffer_change.
4499 For that purpose, this must be 0
4500 when not inside wait_reading_process_output. */
4501 static int waiting_for_user_input_p;
4502
4503 static Lisp_Object
4504 wait_reading_process_output_unwind (Lisp_Object data)
4505 {
4506 waiting_for_user_input_p = XINT (data);
4507 return Qnil;
4508 }
4509
4510 /* This is here so breakpoints can be put on it. */
4511 static void
4512 wait_reading_process_output_1 (void)
4513 {
4514 }
4515
4516 /* Use a wrapper around select to work around a bug in gdb 5.3.
4517 Normally, the wrapper is optimized away by inlining.
4518
4519 If emacs is stopped inside select, the gdb backtrace doesn't
4520 show the function which called select, so it is practically
4521 impossible to step through wait_reading_process_output. */
4522
4523 #ifndef select
4524 static INLINE int
4525 select_wrapper (int n, fd_set *rfd, fd_set *wfd, fd_set *xfd, struct timeval *tmo)
4526 {
4527 return select (n, rfd, wfd, xfd, tmo);
4528 }
4529 #define select select_wrapper
4530 #endif
4531
4532 /* Read and dispose of subprocess output while waiting for timeout to
4533 elapse and/or keyboard input to be available.
4534
4535 TIME_LIMIT is:
4536 timeout in seconds, or
4537 zero for no limit, or
4538 -1 means gobble data immediately available but don't wait for any.
4539
4540 MICROSECS is:
4541 an additional duration to wait, measured in microseconds.
4542 If this is nonzero and time_limit is 0, then the timeout
4543 consists of MICROSECS only.
4544
4545 READ_KBD is a lisp value:
4546 0 to ignore keyboard input, or
4547 1 to return when input is available, or
4548 -1 meaning caller will actually read the input, so don't throw to
4549 the quit handler, or
4550
4551 DO_DISPLAY != 0 means redisplay should be done to show subprocess
4552 output that arrives.
4553
4554 If WAIT_FOR_CELL is a cons cell, wait until its car is non-nil
4555 (and gobble terminal input into the buffer if any arrives).
4556
4557 If WAIT_PROC is specified, wait until something arrives from that
4558 process. The return value is true if we read some input from
4559 that process.
4560
4561 If JUST_WAIT_PROC is non-nil, handle only output from WAIT_PROC
4562 (suspending output from other processes). A negative value
4563 means don't run any timers either.
4564
4565 If WAIT_PROC is specified, then the function returns true if we
4566 received input from that process before the timeout elapsed.
4567 Otherwise, return true if we received input from any process. */
4568
4569 int
4570 wait_reading_process_output (time_limit, microsecs, read_kbd, do_display,
4571 wait_for_cell, wait_proc, just_wait_proc)
4572 int time_limit, microsecs, read_kbd, do_display;
4573 Lisp_Object wait_for_cell;
4574 struct Lisp_Process *wait_proc;
4575 int just_wait_proc;
4576 {
4577 register int channel, nfds;
4578 SELECT_TYPE Available;
4579 #ifdef NON_BLOCKING_CONNECT
4580 SELECT_TYPE Connecting;
4581 int check_connect;
4582 #endif
4583 int check_delay, no_avail;
4584 int xerrno;
4585 Lisp_Object proc;
4586 EMACS_TIME timeout, end_time;
4587 int wait_channel = -1;
4588 int got_some_input = 0;
4589 int count = SPECPDL_INDEX ();
4590
4591 FD_ZERO (&Available);
4592 #ifdef NON_BLOCKING_CONNECT
4593 FD_ZERO (&Connecting);
4594 #endif
4595
4596 if (time_limit == 0 && wait_proc && !NILP (Vinhibit_quit)
4597 && !(CONSP (wait_proc->status) && EQ (XCAR (wait_proc->status), Qexit)))
4598 message ("Blocking call to accept-process-output with quit inhibited!!");
4599
4600 /* If wait_proc is a process to watch, set wait_channel accordingly. */
4601 if (wait_proc != NULL)
4602 wait_channel = wait_proc->infd;
4603
4604 record_unwind_protect (wait_reading_process_output_unwind,
4605 make_number (waiting_for_user_input_p));
4606 waiting_for_user_input_p = read_kbd;
4607
4608 /* Since we may need to wait several times,
4609 compute the absolute time to return at. */
4610 if (time_limit || microsecs)
4611 {
4612 EMACS_GET_TIME (end_time);
4613 EMACS_SET_SECS_USECS (timeout, time_limit, microsecs);
4614 EMACS_ADD_TIME (end_time, end_time, timeout);
4615 }
4616
4617 while (1)
4618 {
4619 int timeout_reduced_for_timers = 0;
4620
4621 /* If calling from keyboard input, do not quit
4622 since we want to return C-g as an input character.
4623 Otherwise, do pending quit if requested. */
4624 if (read_kbd >= 0)
4625 QUIT;
4626 #ifdef SYNC_INPUT
4627 else
4628 process_pending_signals ();
4629 #endif
4630
4631 /* Exit now if the cell we're waiting for became non-nil. */
4632 if (! NILP (wait_for_cell) && ! NILP (XCAR (wait_for_cell)))
4633 break;
4634
4635 /* Compute time from now till when time limit is up */
4636 /* Exit if already run out */
4637 if (time_limit == -1)
4638 {
4639 /* -1 specified for timeout means
4640 gobble output available now
4641 but don't wait at all. */
4642
4643 EMACS_SET_SECS_USECS (timeout, 0, 0);
4644 }
4645 else if (time_limit || microsecs)
4646 {
4647 EMACS_GET_TIME (timeout);
4648 EMACS_SUB_TIME (timeout, end_time, timeout);
4649 if (EMACS_TIME_NEG_P (timeout))
4650 break;
4651 }
4652 else
4653 {
4654 EMACS_SET_SECS_USECS (timeout, 100000, 0);
4655 }
4656
4657 /* Normally we run timers here.
4658 But not if wait_for_cell; in those cases,
4659 the wait is supposed to be short,
4660 and those callers cannot handle running arbitrary Lisp code here. */
4661 if (NILP (wait_for_cell)
4662 && just_wait_proc >= 0)
4663 {
4664 EMACS_TIME timer_delay;
4665
4666 do
4667 {
4668 int old_timers_run = timers_run;
4669 struct buffer *old_buffer = current_buffer;
4670 Lisp_Object old_window = selected_window;
4671
4672 timer_delay = timer_check (1);
4673
4674 /* If a timer has run, this might have changed buffers
4675 an alike. Make read_key_sequence aware of that. */
4676 if (timers_run != old_timers_run
4677 && (old_buffer != current_buffer
4678 || !EQ (old_window, selected_window))
4679 && waiting_for_user_input_p == -1)
4680 record_asynch_buffer_change ();
4681
4682 if (timers_run != old_timers_run && do_display)
4683 /* We must retry, since a timer may have requeued itself
4684 and that could alter the time_delay. */
4685 redisplay_preserve_echo_area (9);
4686 else
4687 break;
4688 }
4689 while (!detect_input_pending ());
4690
4691 /* If there is unread keyboard input, also return. */
4692 if (read_kbd != 0
4693 && requeued_events_pending_p ())
4694 break;
4695
4696 if (! EMACS_TIME_NEG_P (timer_delay) && time_limit != -1)
4697 {
4698 EMACS_TIME difference;
4699 EMACS_SUB_TIME (difference, timer_delay, timeout);
4700 if (EMACS_TIME_NEG_P (difference))
4701 {
4702 timeout = timer_delay;
4703 timeout_reduced_for_timers = 1;
4704 }
4705 }
4706 /* If time_limit is -1, we are not going to wait at all. */
4707 else if (time_limit != -1)
4708 {
4709 /* This is so a breakpoint can be put here. */
4710 wait_reading_process_output_1 ();
4711 }
4712 }
4713
4714 /* Cause C-g and alarm signals to take immediate action,
4715 and cause input available signals to zero out timeout.
4716
4717 It is important that we do this before checking for process
4718 activity. If we get a SIGCHLD after the explicit checks for
4719 process activity, timeout is the only way we will know. */
4720 if (read_kbd < 0)
4721 set_waiting_for_input (&timeout);
4722
4723 /* If status of something has changed, and no input is
4724 available, notify the user of the change right away. After
4725 this explicit check, we'll let the SIGCHLD handler zap
4726 timeout to get our attention. */
4727 if (update_tick != process_tick)
4728 {
4729 SELECT_TYPE Atemp;
4730 #ifdef NON_BLOCKING_CONNECT
4731 SELECT_TYPE Ctemp;
4732 #endif
4733
4734 Atemp = input_wait_mask;
4735 IF_NON_BLOCKING_CONNECT (Ctemp = connect_wait_mask);
4736
4737 EMACS_SET_SECS_USECS (timeout, 0, 0);
4738 if ((select (max (max (max_process_desc, max_keyboard_desc),
4739 max_gpm_desc) + 1,
4740 &Atemp,
4741 #ifdef NON_BLOCKING_CONNECT
4742 (num_pending_connects > 0 ? &Ctemp : (SELECT_TYPE *)0),
4743 #else
4744 (SELECT_TYPE *)0,
4745 #endif
4746 (SELECT_TYPE *)0, &timeout)
4747 <= 0))
4748 {
4749 /* It's okay for us to do this and then continue with
4750 the loop, since timeout has already been zeroed out. */
4751 clear_waiting_for_input ();
4752 status_notify (NULL);
4753 if (do_display) redisplay_preserve_echo_area (13);
4754 }
4755 }
4756
4757 /* Don't wait for output from a non-running process. Just
4758 read whatever data has already been received. */
4759 if (wait_proc && wait_proc->raw_status_new)
4760 update_status (wait_proc);
4761 if (wait_proc
4762 && ! EQ (wait_proc->status, Qrun)
4763 && ! EQ (wait_proc->status, Qconnect))
4764 {
4765 int nread, total_nread = 0;
4766
4767 clear_waiting_for_input ();
4768 XSETPROCESS (proc, wait_proc);
4769
4770 /* Read data from the process, until we exhaust it. */
4771 while (wait_proc->infd >= 0)
4772 {
4773 nread = read_process_output (proc, wait_proc->infd);
4774
4775 if (nread == 0)
4776 break;
4777
4778 if (0 < nread)
4779 {
4780 total_nread += nread;
4781 got_some_input = 1;
4782 }
4783 #ifdef EIO
4784 else if (nread == -1 && EIO == errno)
4785 break;
4786 #endif
4787 #ifdef EAGAIN
4788 else if (nread == -1 && EAGAIN == errno)
4789 break;
4790 #endif
4791 #ifdef EWOULDBLOCK
4792 else if (nread == -1 && EWOULDBLOCK == errno)
4793 break;
4794 #endif
4795 }
4796 if (total_nread > 0 && do_display)
4797 redisplay_preserve_echo_area (10);
4798
4799 break;
4800 }
4801
4802 /* Wait till there is something to do */
4803
4804 if (wait_proc && just_wait_proc)
4805 {
4806 if (wait_proc->infd < 0) /* Terminated */
4807 break;
4808 FD_SET (wait_proc->infd, &Available);
4809 check_delay = 0;
4810 IF_NON_BLOCKING_CONNECT (check_connect = 0);
4811 }
4812 else if (!NILP (wait_for_cell))
4813 {
4814 Available = non_process_wait_mask;
4815 check_delay = 0;
4816 IF_NON_BLOCKING_CONNECT (check_connect = 0);
4817 }
4818 else
4819 {
4820 if (! read_kbd)
4821 Available = non_keyboard_wait_mask;
4822 else
4823 Available = input_wait_mask;
4824 IF_NON_BLOCKING_CONNECT (check_connect = (num_pending_connects > 0));
4825 check_delay = wait_channel >= 0 ? 0 : process_output_delay_count;
4826 }
4827
4828 /* If frame size has changed or the window is newly mapped,
4829 redisplay now, before we start to wait. There is a race
4830 condition here; if a SIGIO arrives between now and the select
4831 and indicates that a frame is trashed, the select may block
4832 displaying a trashed screen. */
4833 if (frame_garbaged && do_display)
4834 {
4835 clear_waiting_for_input ();
4836 redisplay_preserve_echo_area (11);
4837 if (read_kbd < 0)
4838 set_waiting_for_input (&timeout);
4839 }
4840
4841 no_avail = 0;
4842 if (read_kbd && detect_input_pending ())
4843 {
4844 nfds = 0;
4845 no_avail = 1;
4846 }
4847 else
4848 {
4849 #ifdef NON_BLOCKING_CONNECT
4850 if (check_connect)
4851 Connecting = connect_wait_mask;
4852 #endif
4853
4854 #ifdef ADAPTIVE_READ_BUFFERING
4855 /* Set the timeout for adaptive read buffering if any
4856 process has non-zero read_output_skip and non-zero
4857 read_output_delay, and we are not reading output for a
4858 specific wait_channel. It is not executed if
4859 Vprocess_adaptive_read_buffering is nil. */
4860 if (process_output_skip && check_delay > 0)
4861 {
4862 int usecs = EMACS_USECS (timeout);
4863 if (EMACS_SECS (timeout) > 0 || usecs > READ_OUTPUT_DELAY_MAX)
4864 usecs = READ_OUTPUT_DELAY_MAX;
4865 for (channel = 0; check_delay > 0 && channel <= max_process_desc; channel++)
4866 {
4867 proc = chan_process[channel];
4868 if (NILP (proc))
4869 continue;
4870 /* Find minimum non-zero read_output_delay among the
4871 processes with non-zero read_output_skip. */
4872 if (XPROCESS (proc)->read_output_delay > 0)
4873 {
4874 check_delay--;
4875 if (!XPROCESS (proc)->read_output_skip)
4876 continue;
4877 FD_CLR (channel, &Available);
4878 XPROCESS (proc)->read_output_skip = 0;
4879 if (XPROCESS (proc)->read_output_delay < usecs)
4880 usecs = XPROCESS (proc)->read_output_delay;
4881 }
4882 }
4883 EMACS_SET_SECS_USECS (timeout, 0, usecs);
4884 process_output_skip = 0;
4885 }
4886 #endif
4887 #if defined (USE_GTK) || defined (HAVE_GCONF)
4888 nfds = xg_select
4889 #elif defined (HAVE_NS)
4890 nfds = ns_select
4891 #else
4892 nfds = select
4893 #endif
4894 (max (max (max_process_desc, max_keyboard_desc),
4895 max_gpm_desc) + 1,
4896 &Available,
4897 #ifdef NON_BLOCKING_CONNECT
4898 (check_connect ? &Connecting : (SELECT_TYPE *)0),
4899 #else
4900 (SELECT_TYPE *)0,
4901 #endif
4902 (SELECT_TYPE *)0, &timeout);
4903 }
4904
4905 xerrno = errno;
4906
4907 /* Make C-g and alarm signals set flags again */
4908 clear_waiting_for_input ();
4909
4910 /* If we woke up due to SIGWINCH, actually change size now. */
4911 do_pending_window_change (0);
4912
4913 if (time_limit && nfds == 0 && ! timeout_reduced_for_timers)
4914 /* We wanted the full specified time, so return now. */
4915 break;
4916 if (nfds < 0)
4917 {
4918 if (xerrno == EINTR)
4919 no_avail = 1;
4920 else if (xerrno == EBADF)
4921 {
4922 #ifdef AIX
4923 /* AIX doesn't handle PTY closure the same way BSD does. On AIX,
4924 the child's closure of the pts gives the parent a SIGHUP, and
4925 the ptc file descriptor is automatically closed,
4926 yielding EBADF here or at select() call above.
4927 So, SIGHUP is ignored (see def of PTY_TTY_NAME_SPRINTF
4928 in m/ibmrt-aix.h), and here we just ignore the select error.
4929 Cleanup occurs c/o status_notify after SIGCLD. */
4930 no_avail = 1; /* Cannot depend on values returned */
4931 #else
4932 abort ();
4933 #endif
4934 }
4935 else
4936 error ("select error: %s", emacs_strerror (xerrno));
4937 }
4938
4939 if (no_avail)
4940 {
4941 FD_ZERO (&Available);
4942 IF_NON_BLOCKING_CONNECT (check_connect = 0);
4943 }
4944
4945 #if 0 /* When polling is used, interrupt_input is 0,
4946 so get_input_pending should read the input.
4947 So this should not be needed. */
4948 /* If we are using polling for input,
4949 and we see input available, make it get read now.
4950 Otherwise it might not actually get read for a second.
4951 And on hpux, since we turn off polling in wait_reading_process_output,
4952 it might never get read at all if we don't spend much time
4953 outside of wait_reading_process_output. */
4954 if (read_kbd && interrupt_input
4955 && keyboard_bit_set (&Available)
4956 && input_polling_used ())
4957 kill (getpid (), SIGALRM);
4958 #endif
4959
4960 /* Check for keyboard input */
4961 /* If there is any, return immediately
4962 to give it higher priority than subprocesses */
4963
4964 if (read_kbd != 0)
4965 {
4966 int old_timers_run = timers_run;
4967 struct buffer *old_buffer = current_buffer;
4968 Lisp_Object old_window = selected_window;
4969 int leave = 0;
4970
4971 if (detect_input_pending_run_timers (do_display))
4972 {
4973 swallow_events (do_display);
4974 if (detect_input_pending_run_timers (do_display))
4975 leave = 1;
4976 }
4977
4978 /* If a timer has run, this might have changed buffers
4979 an alike. Make read_key_sequence aware of that. */
4980 if (timers_run != old_timers_run
4981 && waiting_for_user_input_p == -1
4982 && (old_buffer != current_buffer
4983 || !EQ (old_window, selected_window)))
4984 record_asynch_buffer_change ();
4985
4986 if (leave)
4987 break;
4988 }
4989
4990 /* If there is unread keyboard input, also return. */
4991 if (read_kbd != 0
4992 && requeued_events_pending_p ())
4993 break;
4994
4995 /* If we are not checking for keyboard input now,
4996 do process events (but don't run any timers).
4997 This is so that X events will be processed.
4998 Otherwise they may have to wait until polling takes place.
4999 That would causes delays in pasting selections, for example.
5000
5001 (We used to do this only if wait_for_cell.) */
5002 if (read_kbd == 0 && detect_input_pending ())
5003 {
5004 swallow_events (do_display);
5005 #if 0 /* Exiting when read_kbd doesn't request that seems wrong, though. */
5006 if (detect_input_pending ())
5007 break;
5008 #endif
5009 }
5010
5011 /* Exit now if the cell we're waiting for became non-nil. */
5012 if (! NILP (wait_for_cell) && ! NILP (XCAR (wait_for_cell)))
5013 break;
5014
5015 #ifdef SIGIO
5016 /* If we think we have keyboard input waiting, but didn't get SIGIO,
5017 go read it. This can happen with X on BSD after logging out.
5018 In that case, there really is no input and no SIGIO,
5019 but select says there is input. */
5020
5021 if (read_kbd && interrupt_input
5022 && keyboard_bit_set (&Available) && ! noninteractive)
5023 kill (getpid (), SIGIO);
5024 #endif
5025
5026 if (! wait_proc)
5027 got_some_input |= nfds > 0;
5028
5029 /* If checking input just got us a size-change event from X,
5030 obey it now if we should. */
5031 if (read_kbd || ! NILP (wait_for_cell))
5032 do_pending_window_change (0);
5033
5034 /* Check for data from a process. */
5035 if (no_avail || nfds == 0)
5036 continue;
5037
5038 /* Really FIRST_PROC_DESC should be 0 on Unix,
5039 but this is safer in the short run. */
5040 for (channel = 0; channel <= max_process_desc; channel++)
5041 {
5042 if (FD_ISSET (channel, &Available)
5043 && FD_ISSET (channel, &non_keyboard_wait_mask))
5044 {
5045 int nread;
5046
5047 /* If waiting for this channel, arrange to return as
5048 soon as no more input to be processed. No more
5049 waiting. */
5050 if (wait_channel == channel)
5051 {
5052 wait_channel = -1;
5053 time_limit = -1;
5054 got_some_input = 1;
5055 }
5056 proc = chan_process[channel];
5057 if (NILP (proc))
5058 continue;
5059
5060 /* If this is a server stream socket, accept connection. */
5061 if (EQ (XPROCESS (proc)->status, Qlisten))
5062 {
5063 server_accept_connection (proc, channel);
5064 continue;
5065 }
5066
5067 /* Read data from the process, starting with our
5068 buffered-ahead character if we have one. */
5069
5070 nread = read_process_output (proc, channel);
5071 if (nread > 0)
5072 {
5073 /* Since read_process_output can run a filter,
5074 which can call accept-process-output,
5075 don't try to read from any other processes
5076 before doing the select again. */
5077 FD_ZERO (&Available);
5078
5079 if (do_display)
5080 redisplay_preserve_echo_area (12);
5081 }
5082 #ifdef EWOULDBLOCK
5083 else if (nread == -1 && errno == EWOULDBLOCK)
5084 ;
5085 #endif
5086 /* ISC 4.1 defines both EWOULDBLOCK and O_NONBLOCK,
5087 and Emacs uses O_NONBLOCK, so what we get is EAGAIN. */
5088 #ifdef O_NONBLOCK
5089 else if (nread == -1 && errno == EAGAIN)
5090 ;
5091 #else
5092 #ifdef O_NDELAY
5093 else if (nread == -1 && errno == EAGAIN)
5094 ;
5095 /* Note that we cannot distinguish between no input
5096 available now and a closed pipe.
5097 With luck, a closed pipe will be accompanied by
5098 subprocess termination and SIGCHLD. */
5099 else if (nread == 0 && !NETCONN_P (proc) && !SERIALCONN_P (proc))
5100 ;
5101 #endif /* O_NDELAY */
5102 #endif /* O_NONBLOCK */
5103 #ifdef HAVE_PTYS
5104 /* On some OSs with ptys, when the process on one end of
5105 a pty exits, the other end gets an error reading with
5106 errno = EIO instead of getting an EOF (0 bytes read).
5107 Therefore, if we get an error reading and errno =
5108 EIO, just continue, because the child process has
5109 exited and should clean itself up soon (e.g. when we
5110 get a SIGCHLD).
5111
5112 However, it has been known to happen that the SIGCHLD
5113 got lost. So raise the signal again just in case.
5114 It can't hurt. */
5115 else if (nread == -1 && errno == EIO)
5116 {
5117 /* Clear the descriptor now, so we only raise the
5118 signal once. Don't do this if `process' is only
5119 a pty. */
5120 if (XPROCESS (proc)->pid != -2)
5121 {
5122 FD_CLR (channel, &input_wait_mask);
5123 FD_CLR (channel, &non_keyboard_wait_mask);
5124
5125 kill (getpid (), SIGCHLD);
5126 }
5127 }
5128 #endif /* HAVE_PTYS */
5129 /* If we can detect process termination, don't consider the process
5130 gone just because its pipe is closed. */
5131 #ifdef SIGCHLD
5132 else if (nread == 0 && !NETCONN_P (proc) && !SERIALCONN_P (proc))
5133 ;
5134 #endif
5135 else
5136 {
5137 /* Preserve status of processes already terminated. */
5138 XPROCESS (proc)->tick = ++process_tick;
5139 deactivate_process (proc);
5140 if (XPROCESS (proc)->raw_status_new)
5141 update_status (XPROCESS (proc));
5142 if (EQ (XPROCESS (proc)->status, Qrun))
5143 XPROCESS (proc)->status
5144 = Fcons (Qexit, Fcons (make_number (256), Qnil));
5145 }
5146 }
5147 #ifdef NON_BLOCKING_CONNECT
5148 if (check_connect && FD_ISSET (channel, &Connecting)
5149 && FD_ISSET (channel, &connect_wait_mask))
5150 {
5151 struct Lisp_Process *p;
5152
5153 FD_CLR (channel, &connect_wait_mask);
5154 if (--num_pending_connects < 0)
5155 abort ();
5156
5157 proc = chan_process[channel];
5158 if (NILP (proc))
5159 continue;
5160
5161 p = XPROCESS (proc);
5162
5163 #ifdef GNU_LINUX
5164 /* getsockopt(,,SO_ERROR,,) is said to hang on some systems.
5165 So only use it on systems where it is known to work. */
5166 {
5167 int xlen = sizeof(xerrno);
5168 if (getsockopt(channel, SOL_SOCKET, SO_ERROR, &xerrno, &xlen))
5169 xerrno = errno;
5170 }
5171 #else
5172 {
5173 struct sockaddr pname;
5174 int pnamelen = sizeof(pname);
5175
5176 /* If connection failed, getpeername will fail. */
5177 xerrno = 0;
5178 if (getpeername(channel, &pname, &pnamelen) < 0)
5179 {
5180 /* Obtain connect failure code through error slippage. */
5181 char dummy;
5182 xerrno = errno;
5183 if (errno == ENOTCONN && read(channel, &dummy, 1) < 0)
5184 xerrno = errno;
5185 }
5186 }
5187 #endif
5188 if (xerrno)
5189 {
5190 p->tick = ++process_tick;
5191 p->status = Fcons (Qfailed, Fcons (make_number (xerrno), Qnil));
5192 deactivate_process (proc);
5193 }
5194 else
5195 {
5196 p->status = Qrun;
5197 /* Execute the sentinel here. If we had relied on
5198 status_notify to do it later, it will read input
5199 from the process before calling the sentinel. */
5200 exec_sentinel (proc, build_string ("open\n"));
5201 if (!EQ (p->filter, Qt) && !EQ (p->command, Qt))
5202 {
5203 FD_SET (p->infd, &input_wait_mask);
5204 FD_SET (p->infd, &non_keyboard_wait_mask);
5205 }
5206 }
5207 }
5208 #endif /* NON_BLOCKING_CONNECT */
5209 } /* end for each file descriptor */
5210 } /* end while exit conditions not met */
5211
5212 unbind_to (count, Qnil);
5213
5214 /* If calling from keyboard input, do not quit
5215 since we want to return C-g as an input character.
5216 Otherwise, do pending quit if requested. */
5217 if (read_kbd >= 0)
5218 {
5219 /* Prevent input_pending from remaining set if we quit. */
5220 clear_input_pending ();
5221 QUIT;
5222 }
5223
5224 return got_some_input;
5225 }
5226 \f
5227 /* Given a list (FUNCTION ARGS...), apply FUNCTION to the ARGS. */
5228
5229 static Lisp_Object
5230 read_process_output_call (Lisp_Object fun_and_args)
5231 {
5232 return apply1 (XCAR (fun_and_args), XCDR (fun_and_args));
5233 }
5234
5235 static Lisp_Object
5236 read_process_output_error_handler (Lisp_Object error)
5237 {
5238 cmd_error_internal (error, "error in process filter: ");
5239 Vinhibit_quit = Qt;
5240 update_echo_area ();
5241 Fsleep_for (make_number (2), Qnil);
5242 return Qt;
5243 }
5244
5245 /* Read pending output from the process channel,
5246 starting with our buffered-ahead character if we have one.
5247 Yield number of decoded characters read.
5248
5249 This function reads at most 4096 characters.
5250 If you want to read all available subprocess output,
5251 you must call it repeatedly until it returns zero.
5252
5253 The characters read are decoded according to PROC's coding-system
5254 for decoding. */
5255
5256 static int
5257 read_process_output (Lisp_Object proc, register int channel)
5258 {
5259 register int nbytes;
5260 char *chars;
5261 register Lisp_Object outstream;
5262 register struct Lisp_Process *p = XPROCESS (proc);
5263 register int opoint;
5264 struct coding_system *coding = proc_decode_coding_system[channel];
5265 int carryover = p->decoding_carryover;
5266 int readmax = 4096;
5267 int count = SPECPDL_INDEX ();
5268 Lisp_Object odeactivate;
5269
5270 chars = (char *) alloca (carryover + readmax);
5271 if (carryover)
5272 /* See the comment above. */
5273 bcopy (SDATA (p->decoding_buf), chars, carryover);
5274
5275 #ifdef DATAGRAM_SOCKETS
5276 /* We have a working select, so proc_buffered_char is always -1. */
5277 if (DATAGRAM_CHAN_P (channel))
5278 {
5279 int len = datagram_address[channel].len;
5280 nbytes = recvfrom (channel, chars + carryover, readmax,
5281 0, datagram_address[channel].sa, &len);
5282 }
5283 else
5284 #endif
5285 if (proc_buffered_char[channel] < 0)
5286 {
5287 nbytes = emacs_read (channel, chars + carryover, readmax);
5288 #ifdef ADAPTIVE_READ_BUFFERING
5289 if (nbytes > 0 && p->adaptive_read_buffering)
5290 {
5291 int delay = p->read_output_delay;
5292 if (nbytes < 256)
5293 {
5294 if (delay < READ_OUTPUT_DELAY_MAX_MAX)
5295 {
5296 if (delay == 0)
5297 process_output_delay_count++;
5298 delay += READ_OUTPUT_DELAY_INCREMENT * 2;
5299 }
5300 }
5301 else if (delay > 0 && (nbytes == readmax))
5302 {
5303 delay -= READ_OUTPUT_DELAY_INCREMENT;
5304 if (delay == 0)
5305 process_output_delay_count--;
5306 }
5307 p->read_output_delay = delay;
5308 if (delay)
5309 {
5310 p->read_output_skip = 1;
5311 process_output_skip = 1;
5312 }
5313 }
5314 #endif
5315 }
5316 else
5317 {
5318 chars[carryover] = proc_buffered_char[channel];
5319 proc_buffered_char[channel] = -1;
5320 nbytes = emacs_read (channel, chars + carryover + 1, readmax - 1);
5321 if (nbytes < 0)
5322 nbytes = 1;
5323 else
5324 nbytes = nbytes + 1;
5325 }
5326
5327 p->decoding_carryover = 0;
5328
5329 /* At this point, NBYTES holds number of bytes just received
5330 (including the one in proc_buffered_char[channel]). */
5331 if (nbytes <= 0)
5332 {
5333 if (nbytes < 0 || coding->mode & CODING_MODE_LAST_BLOCK)
5334 return nbytes;
5335 coding->mode |= CODING_MODE_LAST_BLOCK;
5336 }
5337
5338 /* Now set NBYTES how many bytes we must decode. */
5339 nbytes += carryover;
5340
5341 odeactivate = Vdeactivate_mark;
5342 /* There's no good reason to let process filters change the current
5343 buffer, and many callers of accept-process-output, sit-for, and
5344 friends don't expect current-buffer to be changed from under them. */
5345 record_unwind_protect (set_buffer_if_live, Fcurrent_buffer ());
5346
5347 /* Read and dispose of the process output. */
5348 outstream = p->filter;
5349 if (!NILP (outstream))
5350 {
5351 Lisp_Object obuffer, okeymap;
5352 Lisp_Object text;
5353 int outer_running_asynch_code = running_asynch_code;
5354 int waiting = waiting_for_user_input_p;
5355
5356 /* No need to gcpro these, because all we do with them later
5357 is test them for EQness, and none of them should be a string. */
5358 XSETBUFFER (obuffer, current_buffer);
5359 okeymap = current_buffer->keymap;
5360
5361 /* We inhibit quit here instead of just catching it so that
5362 hitting ^G when a filter happens to be running won't screw
5363 it up. */
5364 specbind (Qinhibit_quit, Qt);
5365 specbind (Qlast_nonmenu_event, Qt);
5366
5367 /* In case we get recursively called,
5368 and we already saved the match data nonrecursively,
5369 save the same match data in safely recursive fashion. */
5370 if (outer_running_asynch_code)
5371 {
5372 Lisp_Object tem;
5373 /* Don't clobber the CURRENT match data, either! */
5374 tem = Fmatch_data (Qnil, Qnil, Qnil);
5375 restore_search_regs ();
5376 record_unwind_save_match_data ();
5377 Fset_match_data (tem, Qt);
5378 }
5379
5380 /* For speed, if a search happens within this code,
5381 save the match data in a special nonrecursive fashion. */
5382 running_asynch_code = 1;
5383
5384 decode_coding_c_string (coding, chars, nbytes, Qt);
5385 text = coding->dst_object;
5386 Vlast_coding_system_used = CODING_ID_NAME (coding->id);
5387 /* A new coding system might be found. */
5388 if (!EQ (p->decode_coding_system, Vlast_coding_system_used))
5389 {
5390 p->decode_coding_system = Vlast_coding_system_used;
5391
5392 /* Don't call setup_coding_system for
5393 proc_decode_coding_system[channel] here. It is done in
5394 detect_coding called via decode_coding above. */
5395
5396 /* If a coding system for encoding is not yet decided, we set
5397 it as the same as coding-system for decoding.
5398
5399 But, before doing that we must check if
5400 proc_encode_coding_system[p->outfd] surely points to a
5401 valid memory because p->outfd will be changed once EOF is
5402 sent to the process. */
5403 if (NILP (p->encode_coding_system)
5404 && proc_encode_coding_system[p->outfd])
5405 {
5406 p->encode_coding_system
5407 = coding_inherit_eol_type (Vlast_coding_system_used, Qnil);
5408 setup_coding_system (p->encode_coding_system,
5409 proc_encode_coding_system[p->outfd]);
5410 }
5411 }
5412
5413 if (coding->carryover_bytes > 0)
5414 {
5415 if (SCHARS (p->decoding_buf) < coding->carryover_bytes)
5416 p->decoding_buf = make_uninit_string (coding->carryover_bytes);
5417 bcopy (coding->carryover, SDATA (p->decoding_buf),
5418 coding->carryover_bytes);
5419 p->decoding_carryover = coding->carryover_bytes;
5420 }
5421 if (SBYTES (text) > 0)
5422 internal_condition_case_1 (read_process_output_call,
5423 Fcons (outstream,
5424 Fcons (proc, Fcons (text, Qnil))),
5425 !NILP (Vdebug_on_error) ? Qnil : Qerror,
5426 read_process_output_error_handler);
5427
5428 /* If we saved the match data nonrecursively, restore it now. */
5429 restore_search_regs ();
5430 running_asynch_code = outer_running_asynch_code;
5431
5432 /* Restore waiting_for_user_input_p as it was
5433 when we were called, in case the filter clobbered it. */
5434 waiting_for_user_input_p = waiting;
5435
5436 #if 0 /* Call record_asynch_buffer_change unconditionally,
5437 because we might have changed minor modes or other things
5438 that affect key bindings. */
5439 if (! EQ (Fcurrent_buffer (), obuffer)
5440 || ! EQ (current_buffer->keymap, okeymap))
5441 #endif
5442 /* But do it only if the caller is actually going to read events.
5443 Otherwise there's no need to make him wake up, and it could
5444 cause trouble (for example it would make sit_for return). */
5445 if (waiting_for_user_input_p == -1)
5446 record_asynch_buffer_change ();
5447 }
5448
5449 /* If no filter, write into buffer if it isn't dead. */
5450 else if (!NILP (p->buffer) && !NILP (XBUFFER (p->buffer)->name))
5451 {
5452 Lisp_Object old_read_only;
5453 int old_begv, old_zv;
5454 int old_begv_byte, old_zv_byte;
5455 int before, before_byte;
5456 int opoint_byte;
5457 Lisp_Object text;
5458 struct buffer *b;
5459
5460 Fset_buffer (p->buffer);
5461 opoint = PT;
5462 opoint_byte = PT_BYTE;
5463 old_read_only = current_buffer->read_only;
5464 old_begv = BEGV;
5465 old_zv = ZV;
5466 old_begv_byte = BEGV_BYTE;
5467 old_zv_byte = ZV_BYTE;
5468
5469 current_buffer->read_only = Qnil;
5470
5471 /* Insert new output into buffer
5472 at the current end-of-output marker,
5473 thus preserving logical ordering of input and output. */
5474 if (XMARKER (p->mark)->buffer)
5475 SET_PT_BOTH (clip_to_bounds (BEGV, marker_position (p->mark), ZV),
5476 clip_to_bounds (BEGV_BYTE, marker_byte_position (p->mark),
5477 ZV_BYTE));
5478 else
5479 SET_PT_BOTH (ZV, ZV_BYTE);
5480 before = PT;
5481 before_byte = PT_BYTE;
5482
5483 /* If the output marker is outside of the visible region, save
5484 the restriction and widen. */
5485 if (! (BEGV <= PT && PT <= ZV))
5486 Fwiden ();
5487
5488 decode_coding_c_string (coding, chars, nbytes, Qt);
5489 text = coding->dst_object;
5490 Vlast_coding_system_used = CODING_ID_NAME (coding->id);
5491 /* A new coding system might be found. See the comment in the
5492 similar code in the previous `if' block. */
5493 if (!EQ (p->decode_coding_system, Vlast_coding_system_used))
5494 {
5495 p->decode_coding_system = Vlast_coding_system_used;
5496 if (NILP (p->encode_coding_system)
5497 && proc_encode_coding_system[p->outfd])
5498 {
5499 p->encode_coding_system
5500 = coding_inherit_eol_type (Vlast_coding_system_used, Qnil);
5501 setup_coding_system (p->encode_coding_system,
5502 proc_encode_coding_system[p->outfd]);
5503 }
5504 }
5505 if (coding->carryover_bytes > 0)
5506 {
5507 if (SCHARS (p->decoding_buf) < coding->carryover_bytes)
5508 p->decoding_buf = make_uninit_string (coding->carryover_bytes);
5509 bcopy (coding->carryover, SDATA (p->decoding_buf),
5510 coding->carryover_bytes);
5511 p->decoding_carryover = coding->carryover_bytes;
5512 }
5513 /* Adjust the multibyteness of TEXT to that of the buffer. */
5514 if (NILP (current_buffer->enable_multibyte_characters)
5515 != ! STRING_MULTIBYTE (text))
5516 text = (STRING_MULTIBYTE (text)
5517 ? Fstring_as_unibyte (text)
5518 : Fstring_to_multibyte (text));
5519 /* Insert before markers in case we are inserting where
5520 the buffer's mark is, and the user's next command is Meta-y. */
5521 insert_from_string_before_markers (text, 0, 0,
5522 SCHARS (text), SBYTES (text), 0);
5523
5524 /* Make sure the process marker's position is valid when the
5525 process buffer is changed in the signal_after_change above.
5526 W3 is known to do that. */
5527 if (BUFFERP (p->buffer)
5528 && (b = XBUFFER (p->buffer), b != current_buffer))
5529 set_marker_both (p->mark, p->buffer, BUF_PT (b), BUF_PT_BYTE (b));
5530 else
5531 set_marker_both (p->mark, p->buffer, PT, PT_BYTE);
5532
5533 update_mode_lines++;
5534
5535 /* Make sure opoint and the old restrictions
5536 float ahead of any new text just as point would. */
5537 if (opoint >= before)
5538 {
5539 opoint += PT - before;
5540 opoint_byte += PT_BYTE - before_byte;
5541 }
5542 if (old_begv > before)
5543 {
5544 old_begv += PT - before;
5545 old_begv_byte += PT_BYTE - before_byte;
5546 }
5547 if (old_zv >= before)
5548 {
5549 old_zv += PT - before;
5550 old_zv_byte += PT_BYTE - before_byte;
5551 }
5552
5553 /* If the restriction isn't what it should be, set it. */
5554 if (old_begv != BEGV || old_zv != ZV)
5555 Fnarrow_to_region (make_number (old_begv), make_number (old_zv));
5556
5557
5558 current_buffer->read_only = old_read_only;
5559 SET_PT_BOTH (opoint, opoint_byte);
5560 }
5561 /* Handling the process output should not deactivate the mark. */
5562 Vdeactivate_mark = odeactivate;
5563
5564 unbind_to (count, Qnil);
5565 return nbytes;
5566 }
5567
5568 DEFUN ("waiting-for-user-input-p", Fwaiting_for_user_input_p, Swaiting_for_user_input_p,
5569 0, 0, 0,
5570 doc: /* Returns non-nil if Emacs is waiting for input from the user.
5571 This is intended for use by asynchronous process output filters and sentinels. */)
5572 ()
5573 {
5574 return (waiting_for_user_input_p ? Qt : Qnil);
5575 }
5576 \f
5577 /* Sending data to subprocess */
5578
5579 jmp_buf send_process_frame;
5580 Lisp_Object process_sent_to;
5581
5582 SIGTYPE
5583 send_process_trap (int ignore)
5584 {
5585 SIGNAL_THREAD_CHECK (SIGPIPE);
5586 sigunblock (sigmask (SIGPIPE));
5587 longjmp (send_process_frame, 1);
5588 }
5589
5590 /* Send some data to process PROC.
5591 BUF is the beginning of the data; LEN is the number of characters.
5592 OBJECT is the Lisp object that the data comes from. If OBJECT is
5593 nil or t, it means that the data comes from C string.
5594
5595 If OBJECT is not nil, the data is encoded by PROC's coding-system
5596 for encoding before it is sent.
5597
5598 This function can evaluate Lisp code and can garbage collect. */
5599
5600 static void
5601 send_process (volatile Lisp_Object proc, unsigned char *volatile buf,
5602 volatile int len, volatile Lisp_Object object)
5603 {
5604 /* Use volatile to protect variables from being clobbered by longjmp. */
5605 struct Lisp_Process *p = XPROCESS (proc);
5606 int rv;
5607 struct coding_system *coding;
5608 struct gcpro gcpro1;
5609 SIGTYPE (*volatile old_sigpipe) ();
5610
5611 GCPRO1 (object);
5612
5613 if (p->raw_status_new)
5614 update_status (p);
5615 if (! EQ (p->status, Qrun))
5616 error ("Process %s not running", SDATA (p->name));
5617 if (p->outfd < 0)
5618 error ("Output file descriptor of %s is closed", SDATA (p->name));
5619
5620 coding = proc_encode_coding_system[p->outfd];
5621 Vlast_coding_system_used = CODING_ID_NAME (coding->id);
5622
5623 if ((STRINGP (object) && STRING_MULTIBYTE (object))
5624 || (BUFFERP (object)
5625 && !NILP (XBUFFER (object)->enable_multibyte_characters))
5626 || EQ (object, Qt))
5627 {
5628 if (!EQ (Vlast_coding_system_used, p->encode_coding_system))
5629 /* The coding system for encoding was changed to raw-text
5630 because we sent a unibyte text previously. Now we are
5631 sending a multibyte text, thus we must encode it by the
5632 original coding system specified for the current process. */
5633 setup_coding_system (p->encode_coding_system, coding);
5634 coding->src_multibyte = 1;
5635 }
5636 else
5637 {
5638 /* For sending a unibyte text, character code conversion should
5639 not take place but EOL conversion should. So, setup raw-text
5640 or one of the subsidiary if we have not yet done it. */
5641 if (CODING_REQUIRE_ENCODING (coding))
5642 {
5643 if (CODING_REQUIRE_FLUSHING (coding))
5644 {
5645 /* But, before changing the coding, we must flush out data. */
5646 coding->mode |= CODING_MODE_LAST_BLOCK;
5647 send_process (proc, "", 0, Qt);
5648 coding->mode &= CODING_MODE_LAST_BLOCK;
5649 }
5650 setup_coding_system (raw_text_coding_system
5651 (Vlast_coding_system_used),
5652 coding);
5653 coding->src_multibyte = 0;
5654 }
5655 }
5656 coding->dst_multibyte = 0;
5657
5658 if (CODING_REQUIRE_ENCODING (coding))
5659 {
5660 coding->dst_object = Qt;
5661 if (BUFFERP (object))
5662 {
5663 int from_byte, from, to;
5664 int save_pt, save_pt_byte;
5665 struct buffer *cur = current_buffer;
5666
5667 set_buffer_internal (XBUFFER (object));
5668 save_pt = PT, save_pt_byte = PT_BYTE;
5669
5670 from_byte = PTR_BYTE_POS (buf);
5671 from = BYTE_TO_CHAR (from_byte);
5672 to = BYTE_TO_CHAR (from_byte + len);
5673 TEMP_SET_PT_BOTH (from, from_byte);
5674 encode_coding_object (coding, object, from, from_byte,
5675 to, from_byte + len, Qt);
5676 TEMP_SET_PT_BOTH (save_pt, save_pt_byte);
5677 set_buffer_internal (cur);
5678 }
5679 else if (STRINGP (object))
5680 {
5681 encode_coding_object (coding, object, 0, 0, SCHARS (object),
5682 SBYTES (object), Qt);
5683 }
5684 else
5685 {
5686 coding->dst_object = make_unibyte_string (buf, len);
5687 coding->produced = len;
5688 }
5689
5690 len = coding->produced;
5691 object = coding->dst_object;
5692 buf = SDATA (object);
5693 }
5694
5695 if (pty_max_bytes == 0)
5696 {
5697 #if defined (HAVE_FPATHCONF) && defined (_PC_MAX_CANON)
5698 pty_max_bytes = fpathconf (p->outfd, _PC_MAX_CANON);
5699 if (pty_max_bytes < 0)
5700 pty_max_bytes = 250;
5701 #else
5702 pty_max_bytes = 250;
5703 #endif
5704 /* Deduct one, to leave space for the eof. */
5705 pty_max_bytes--;
5706 }
5707
5708 /* 2000-09-21: Emacs 20.7, sparc-sun-solaris-2.6, GCC 2.95.2,
5709 CFLAGS="-g -O": The value of the parameter `proc' is clobbered
5710 when returning with longjmp despite being declared volatile. */
5711 if (!setjmp (send_process_frame))
5712 {
5713 process_sent_to = proc;
5714 while (len > 0)
5715 {
5716 int this = len;
5717
5718 /* Send this batch, using one or more write calls. */
5719 while (this > 0)
5720 {
5721 int outfd = p->outfd;
5722 old_sigpipe = (SIGTYPE (*) ()) signal (SIGPIPE, send_process_trap);
5723 #ifdef DATAGRAM_SOCKETS
5724 if (DATAGRAM_CHAN_P (outfd))
5725 {
5726 rv = sendto (outfd, (char *) buf, this,
5727 0, datagram_address[outfd].sa,
5728 datagram_address[outfd].len);
5729 if (rv < 0 && errno == EMSGSIZE)
5730 {
5731 signal (SIGPIPE, old_sigpipe);
5732 report_file_error ("sending datagram",
5733 Fcons (proc, Qnil));
5734 }
5735 }
5736 else
5737 #endif
5738 {
5739 rv = emacs_write (outfd, (char *) buf, this);
5740 #ifdef ADAPTIVE_READ_BUFFERING
5741 if (p->read_output_delay > 0
5742 && p->adaptive_read_buffering == 1)
5743 {
5744 p->read_output_delay = 0;
5745 process_output_delay_count--;
5746 p->read_output_skip = 0;
5747 }
5748 #endif
5749 }
5750 signal (SIGPIPE, old_sigpipe);
5751
5752 if (rv < 0)
5753 {
5754 if (0
5755 #ifdef EWOULDBLOCK
5756 || errno == EWOULDBLOCK
5757 #endif
5758 #ifdef EAGAIN
5759 || errno == EAGAIN
5760 #endif
5761 )
5762 /* Buffer is full. Wait, accepting input;
5763 that may allow the program
5764 to finish doing output and read more. */
5765 {
5766 int offset = 0;
5767
5768 #ifdef BROKEN_PTY_READ_AFTER_EAGAIN
5769 /* A gross hack to work around a bug in FreeBSD.
5770 In the following sequence, read(2) returns
5771 bogus data:
5772
5773 write(2) 1022 bytes
5774 write(2) 954 bytes, get EAGAIN
5775 read(2) 1024 bytes in process_read_output
5776 read(2) 11 bytes in process_read_output
5777
5778 That is, read(2) returns more bytes than have
5779 ever been written successfully. The 1033 bytes
5780 read are the 1022 bytes written successfully
5781 after processing (for example with CRs added if
5782 the terminal is set up that way which it is
5783 here). The same bytes will be seen again in a
5784 later read(2), without the CRs. */
5785
5786 if (errno == EAGAIN)
5787 {
5788 int flags = FWRITE;
5789 ioctl (p->outfd, TIOCFLUSH, &flags);
5790 }
5791 #endif /* BROKEN_PTY_READ_AFTER_EAGAIN */
5792
5793 /* Running filters might relocate buffers or strings.
5794 Arrange to relocate BUF. */
5795 if (BUFFERP (object))
5796 offset = BUF_PTR_BYTE_POS (XBUFFER (object), buf);
5797 else if (STRINGP (object))
5798 offset = buf - SDATA (object);
5799
5800 #ifdef EMACS_HAS_USECS
5801 wait_reading_process_output (0, 20000, 0, 0, Qnil, NULL, 0);
5802 #else
5803 wait_reading_process_output (1, 0, 0, 0, Qnil, NULL, 0);
5804 #endif
5805
5806 if (BUFFERP (object))
5807 buf = BUF_BYTE_ADDRESS (XBUFFER (object), offset);
5808 else if (STRINGP (object))
5809 buf = offset + SDATA (object);
5810
5811 rv = 0;
5812 }
5813 else
5814 /* This is a real error. */
5815 report_file_error ("writing to process", Fcons (proc, Qnil));
5816 }
5817 buf += rv;
5818 len -= rv;
5819 this -= rv;
5820 }
5821 }
5822 }
5823 else
5824 {
5825 signal (SIGPIPE, old_sigpipe);
5826 proc = process_sent_to;
5827 p = XPROCESS (proc);
5828 p->raw_status_new = 0;
5829 p->status = Fcons (Qexit, Fcons (make_number (256), Qnil));
5830 p->tick = ++process_tick;
5831 deactivate_process (proc);
5832 error ("SIGPIPE raised on process %s; closed it", SDATA (p->name));
5833 }
5834
5835 UNGCPRO;
5836 }
5837
5838 DEFUN ("process-send-region", Fprocess_send_region, Sprocess_send_region,
5839 3, 3, 0,
5840 doc: /* Send current contents of region as input to PROCESS.
5841 PROCESS may be a process, a buffer, the name of a process or buffer, or
5842 nil, indicating the current buffer's process.
5843 Called from program, takes three arguments, PROCESS, START and END.
5844 If the region is more than 500 characters long,
5845 it is sent in several bunches. This may happen even for shorter regions.
5846 Output from processes can arrive in between bunches. */)
5847 (process, start, end)
5848 Lisp_Object process, start, end;
5849 {
5850 Lisp_Object proc;
5851 int start1, end1;
5852
5853 proc = get_process (process);
5854 validate_region (&start, &end);
5855
5856 if (XINT (start) < GPT && XINT (end) > GPT)
5857 move_gap (XINT (start));
5858
5859 start1 = CHAR_TO_BYTE (XINT (start));
5860 end1 = CHAR_TO_BYTE (XINT (end));
5861 send_process (proc, BYTE_POS_ADDR (start1), end1 - start1,
5862 Fcurrent_buffer ());
5863
5864 return Qnil;
5865 }
5866
5867 DEFUN ("process-send-string", Fprocess_send_string, Sprocess_send_string,
5868 2, 2, 0,
5869 doc: /* Send PROCESS the contents of STRING as input.
5870 PROCESS may be a process, a buffer, the name of a process or buffer, or
5871 nil, indicating the current buffer's process.
5872 If STRING is more than 500 characters long,
5873 it is sent in several bunches. This may happen even for shorter strings.
5874 Output from processes can arrive in between bunches. */)
5875 (process, string)
5876 Lisp_Object process, string;
5877 {
5878 Lisp_Object proc;
5879 CHECK_STRING (string);
5880 proc = get_process (process);
5881 send_process (proc, SDATA (string),
5882 SBYTES (string), string);
5883 return Qnil;
5884 }
5885 \f
5886 /* Return the foreground process group for the tty/pty that
5887 the process P uses. */
5888 static int
5889 emacs_get_tty_pgrp (struct Lisp_Process *p)
5890 {
5891 int gid = -1;
5892
5893 #ifdef TIOCGPGRP
5894 if (ioctl (p->infd, TIOCGPGRP, &gid) == -1 && ! NILP (p->tty_name))
5895 {
5896 int fd;
5897 /* Some OS:es (Solaris 8/9) does not allow TIOCGPGRP from the
5898 master side. Try the slave side. */
5899 fd = emacs_open (SDATA (p->tty_name), O_RDONLY, 0);
5900
5901 if (fd != -1)
5902 {
5903 ioctl (fd, TIOCGPGRP, &gid);
5904 emacs_close (fd);
5905 }
5906 }
5907 #endif /* defined (TIOCGPGRP ) */
5908
5909 return gid;
5910 }
5911
5912 DEFUN ("process-running-child-p", Fprocess_running_child_p,
5913 Sprocess_running_child_p, 0, 1, 0,
5914 doc: /* Return t if PROCESS has given the terminal to a child.
5915 If the operating system does not make it possible to find out,
5916 return t unconditionally. */)
5917 (process)
5918 Lisp_Object process;
5919 {
5920 /* Initialize in case ioctl doesn't exist or gives an error,
5921 in a way that will cause returning t. */
5922 int gid;
5923 Lisp_Object proc;
5924 struct Lisp_Process *p;
5925
5926 proc = get_process (process);
5927 p = XPROCESS (proc);
5928
5929 if (!EQ (p->type, Qreal))
5930 error ("Process %s is not a subprocess",
5931 SDATA (p->name));
5932 if (p->infd < 0)
5933 error ("Process %s is not active",
5934 SDATA (p->name));
5935
5936 gid = emacs_get_tty_pgrp (p);
5937
5938 if (gid == p->pid)
5939 return Qnil;
5940 return Qt;
5941 }
5942 \f
5943 /* send a signal number SIGNO to PROCESS.
5944 If CURRENT_GROUP is t, that means send to the process group
5945 that currently owns the terminal being used to communicate with PROCESS.
5946 This is used for various commands in shell mode.
5947 If CURRENT_GROUP is lambda, that means send to the process group
5948 that currently owns the terminal, but only if it is NOT the shell itself.
5949
5950 If NOMSG is zero, insert signal-announcements into process's buffers
5951 right away.
5952
5953 If we can, we try to signal PROCESS by sending control characters
5954 down the pty. This allows us to signal inferiors who have changed
5955 their uid, for which killpg would return an EPERM error. */
5956
5957 static void
5958 process_send_signal (Lisp_Object process, int signo, Lisp_Object current_group,
5959 int nomsg)
5960 {
5961 Lisp_Object proc;
5962 register struct Lisp_Process *p;
5963 int gid;
5964 int no_pgrp = 0;
5965
5966 proc = get_process (process);
5967 p = XPROCESS (proc);
5968
5969 if (!EQ (p->type, Qreal))
5970 error ("Process %s is not a subprocess",
5971 SDATA (p->name));
5972 if (p->infd < 0)
5973 error ("Process %s is not active",
5974 SDATA (p->name));
5975
5976 if (!p->pty_flag)
5977 current_group = Qnil;
5978
5979 /* If we are using pgrps, get a pgrp number and make it negative. */
5980 if (NILP (current_group))
5981 /* Send the signal to the shell's process group. */
5982 gid = p->pid;
5983 else
5984 {
5985 #ifdef SIGNALS_VIA_CHARACTERS
5986 /* If possible, send signals to the entire pgrp
5987 by sending an input character to it. */
5988
5989 /* TERMIOS is the latest and bestest, and seems most likely to
5990 work. If the system has it, use it. */
5991 #ifdef HAVE_TERMIOS
5992 struct termios t;
5993 cc_t *sig_char = NULL;
5994
5995 tcgetattr (p->infd, &t);
5996
5997 switch (signo)
5998 {
5999 case SIGINT:
6000 sig_char = &t.c_cc[VINTR];
6001 break;
6002
6003 case SIGQUIT:
6004 sig_char = &t.c_cc[VQUIT];
6005 break;
6006
6007 case SIGTSTP:
6008 #if defined (VSWTCH) && !defined (PREFER_VSUSP)
6009 sig_char = &t.c_cc[VSWTCH];
6010 #else
6011 sig_char = &t.c_cc[VSUSP];
6012 #endif
6013 break;
6014 }
6015
6016 if (sig_char && *sig_char != CDISABLE)
6017 {
6018 send_process (proc, sig_char, 1, Qnil);
6019 return;
6020 }
6021 /* If we can't send the signal with a character,
6022 fall through and send it another way. */
6023 #else /* ! HAVE_TERMIOS */
6024
6025 /* On Berkeley descendants, the following IOCTL's retrieve the
6026 current control characters. */
6027 #if defined (TIOCGLTC) && defined (TIOCGETC)
6028
6029 struct tchars c;
6030 struct ltchars lc;
6031
6032 switch (signo)
6033 {
6034 case SIGINT:
6035 ioctl (p->infd, TIOCGETC, &c);
6036 send_process (proc, &c.t_intrc, 1, Qnil);
6037 return;
6038 case SIGQUIT:
6039 ioctl (p->infd, TIOCGETC, &c);
6040 send_process (proc, &c.t_quitc, 1, Qnil);
6041 return;
6042 #ifdef SIGTSTP
6043 case SIGTSTP:
6044 ioctl (p->infd, TIOCGLTC, &lc);
6045 send_process (proc, &lc.t_suspc, 1, Qnil);
6046 return;
6047 #endif /* ! defined (SIGTSTP) */
6048 }
6049
6050 #else /* ! defined (TIOCGLTC) && defined (TIOCGETC) */
6051
6052 /* On SYSV descendants, the TCGETA ioctl retrieves the current control
6053 characters. */
6054 #ifdef TCGETA
6055 struct termio t;
6056 switch (signo)
6057 {
6058 case SIGINT:
6059 ioctl (p->infd, TCGETA, &t);
6060 send_process (proc, &t.c_cc[VINTR], 1, Qnil);
6061 return;
6062 case SIGQUIT:
6063 ioctl (p->infd, TCGETA, &t);
6064 send_process (proc, &t.c_cc[VQUIT], 1, Qnil);
6065 return;
6066 #ifdef SIGTSTP
6067 case SIGTSTP:
6068 ioctl (p->infd, TCGETA, &t);
6069 send_process (proc, &t.c_cc[VSWTCH], 1, Qnil);
6070 return;
6071 #endif /* ! defined (SIGTSTP) */
6072 }
6073 #else /* ! defined (TCGETA) */
6074 Your configuration files are messed up.
6075 /* If your system configuration files define SIGNALS_VIA_CHARACTERS,
6076 you'd better be using one of the alternatives above! */
6077 #endif /* ! defined (TCGETA) */
6078 #endif /* ! defined (TIOCGLTC) && defined (TIOCGETC) */
6079 /* In this case, the code above should alway return. */
6080 abort ();
6081 #endif /* ! defined HAVE_TERMIOS */
6082
6083 /* The code above may fall through if it can't
6084 handle the signal. */
6085 #endif /* defined (SIGNALS_VIA_CHARACTERS) */
6086
6087 #ifdef TIOCGPGRP
6088 /* Get the current pgrp using the tty itself, if we have that.
6089 Otherwise, use the pty to get the pgrp.
6090 On pfa systems, saka@pfu.fujitsu.co.JP writes:
6091 "TIOCGPGRP symbol defined in sys/ioctl.h at E50.
6092 But, TIOCGPGRP does not work on E50 ;-P works fine on E60"
6093 His patch indicates that if TIOCGPGRP returns an error, then
6094 we should just assume that p->pid is also the process group id. */
6095
6096 gid = emacs_get_tty_pgrp (p);
6097
6098 if (gid == -1)
6099 /* If we can't get the information, assume
6100 the shell owns the tty. */
6101 gid = p->pid;
6102
6103 /* It is not clear whether anything really can set GID to -1.
6104 Perhaps on some system one of those ioctls can or could do so.
6105 Or perhaps this is vestigial. */
6106 if (gid == -1)
6107 no_pgrp = 1;
6108 #else /* ! defined (TIOCGPGRP ) */
6109 /* Can't select pgrps on this system, so we know that
6110 the child itself heads the pgrp. */
6111 gid = p->pid;
6112 #endif /* ! defined (TIOCGPGRP ) */
6113
6114 /* If current_group is lambda, and the shell owns the terminal,
6115 don't send any signal. */
6116 if (EQ (current_group, Qlambda) && gid == p->pid)
6117 return;
6118 }
6119
6120 switch (signo)
6121 {
6122 #ifdef SIGCONT
6123 case SIGCONT:
6124 p->raw_status_new = 0;
6125 p->status = Qrun;
6126 p->tick = ++process_tick;
6127 if (!nomsg)
6128 {
6129 status_notify (NULL);
6130 redisplay_preserve_echo_area (13);
6131 }
6132 break;
6133 #endif /* ! defined (SIGCONT) */
6134 case SIGINT:
6135 case SIGQUIT:
6136 case SIGKILL:
6137 flush_pending_output (p->infd);
6138 break;
6139 }
6140
6141 /* If we don't have process groups, send the signal to the immediate
6142 subprocess. That isn't really right, but it's better than any
6143 obvious alternative. */
6144 if (no_pgrp)
6145 {
6146 kill (p->pid, signo);
6147 return;
6148 }
6149
6150 /* gid may be a pid, or minus a pgrp's number */
6151 #ifdef TIOCSIGSEND
6152 if (!NILP (current_group))
6153 {
6154 if (ioctl (p->infd, TIOCSIGSEND, signo) == -1)
6155 EMACS_KILLPG (gid, signo);
6156 }
6157 else
6158 {
6159 gid = - p->pid;
6160 kill (gid, signo);
6161 }
6162 #else /* ! defined (TIOCSIGSEND) */
6163 EMACS_KILLPG (gid, signo);
6164 #endif /* ! defined (TIOCSIGSEND) */
6165 }
6166
6167 DEFUN ("interrupt-process", Finterrupt_process, Sinterrupt_process, 0, 2, 0,
6168 doc: /* Interrupt process PROCESS.
6169 PROCESS may be a process, a buffer, or the name of a process or buffer.
6170 No arg or nil means current buffer's process.
6171 Second arg CURRENT-GROUP non-nil means send signal to
6172 the current process-group of the process's controlling terminal
6173 rather than to the process's own process group.
6174 If the process is a shell, this means interrupt current subjob
6175 rather than the shell.
6176
6177 If CURRENT-GROUP is `lambda', and if the shell owns the terminal,
6178 don't send the signal. */)
6179 (process, current_group)
6180 Lisp_Object process, current_group;
6181 {
6182 process_send_signal (process, SIGINT, current_group, 0);
6183 return process;
6184 }
6185
6186 DEFUN ("kill-process", Fkill_process, Skill_process, 0, 2, 0,
6187 doc: /* Kill process PROCESS. May be process or name of one.
6188 See function `interrupt-process' for more details on usage. */)
6189 (process, current_group)
6190 Lisp_Object process, current_group;
6191 {
6192 process_send_signal (process, SIGKILL, current_group, 0);
6193 return process;
6194 }
6195
6196 DEFUN ("quit-process", Fquit_process, Squit_process, 0, 2, 0,
6197 doc: /* Send QUIT signal to process PROCESS. May be process or name of one.
6198 See function `interrupt-process' for more details on usage. */)
6199 (process, current_group)
6200 Lisp_Object process, current_group;
6201 {
6202 process_send_signal (process, SIGQUIT, current_group, 0);
6203 return process;
6204 }
6205
6206 DEFUN ("stop-process", Fstop_process, Sstop_process, 0, 2, 0,
6207 doc: /* Stop process PROCESS. May be process or name of one.
6208 See function `interrupt-process' for more details on usage.
6209 If PROCESS is a network or serial process, inhibit handling of incoming
6210 traffic. */)
6211 (process, current_group)
6212 Lisp_Object process, current_group;
6213 {
6214 #ifdef HAVE_SOCKETS
6215 if (PROCESSP (process) && (NETCONN_P (process) || SERIALCONN_P (process)))
6216 {
6217 struct Lisp_Process *p;
6218
6219 p = XPROCESS (process);
6220 if (NILP (p->command)
6221 && p->infd >= 0)
6222 {
6223 FD_CLR (p->infd, &input_wait_mask);
6224 FD_CLR (p->infd, &non_keyboard_wait_mask);
6225 }
6226 p->command = Qt;
6227 return process;
6228 }
6229 #endif
6230 #ifndef SIGTSTP
6231 error ("No SIGTSTP support");
6232 #else
6233 process_send_signal (process, SIGTSTP, current_group, 0);
6234 #endif
6235 return process;
6236 }
6237
6238 DEFUN ("continue-process", Fcontinue_process, Scontinue_process, 0, 2, 0,
6239 doc: /* Continue process PROCESS. May be process or name of one.
6240 See function `interrupt-process' for more details on usage.
6241 If PROCESS is a network or serial process, resume handling of incoming
6242 traffic. */)
6243 (process, current_group)
6244 Lisp_Object process, current_group;
6245 {
6246 #ifdef HAVE_SOCKETS
6247 if (PROCESSP (process) && (NETCONN_P (process) || SERIALCONN_P (process)))
6248 {
6249 struct Lisp_Process *p;
6250
6251 p = XPROCESS (process);
6252 if (EQ (p->command, Qt)
6253 && p->infd >= 0
6254 && (!EQ (p->filter, Qt) || EQ (p->status, Qlisten)))
6255 {
6256 FD_SET (p->infd, &input_wait_mask);
6257 FD_SET (p->infd, &non_keyboard_wait_mask);
6258 #ifdef WINDOWSNT
6259 if (fd_info[ p->infd ].flags & FILE_SERIAL)
6260 PurgeComm (fd_info[ p->infd ].hnd, PURGE_RXABORT | PURGE_RXCLEAR);
6261 #endif
6262 #ifdef HAVE_TERMIOS
6263 tcflush (p->infd, TCIFLUSH);
6264 #endif
6265 }
6266 p->command = Qnil;
6267 return process;
6268 }
6269 #endif
6270 #ifdef SIGCONT
6271 process_send_signal (process, SIGCONT, current_group, 0);
6272 #else
6273 error ("No SIGCONT support");
6274 #endif
6275 return process;
6276 }
6277
6278 DEFUN ("signal-process", Fsignal_process, Ssignal_process,
6279 2, 2, "sProcess (name or number): \nnSignal code: ",
6280 doc: /* Send PROCESS the signal with code SIGCODE.
6281 PROCESS may also be a number specifying the process id of the
6282 process to signal; in this case, the process need not be a child of
6283 this Emacs.
6284 SIGCODE may be an integer, or a symbol whose name is a signal name. */)
6285 (process, sigcode)
6286 Lisp_Object process, sigcode;
6287 {
6288 pid_t pid;
6289
6290 if (INTEGERP (process))
6291 {
6292 pid = XINT (process);
6293 goto got_it;
6294 }
6295
6296 if (FLOATP (process))
6297 {
6298 pid = (pid_t) XFLOAT_DATA (process);
6299 goto got_it;
6300 }
6301
6302 if (STRINGP (process))
6303 {
6304 Lisp_Object tem;
6305 if (tem = Fget_process (process), NILP (tem))
6306 {
6307 pid = XINT (Fstring_to_number (process, make_number (10)));
6308 if (pid > 0)
6309 goto got_it;
6310 }
6311 process = tem;
6312 }
6313 else
6314 process = get_process (process);
6315
6316 if (NILP (process))
6317 return process;
6318
6319 CHECK_PROCESS (process);
6320 pid = XPROCESS (process)->pid;
6321 if (pid <= 0)
6322 error ("Cannot signal process %s", SDATA (XPROCESS (process)->name));
6323
6324 got_it:
6325
6326 #define parse_signal(NAME, VALUE) \
6327 else if (!xstrcasecmp (name, NAME)) \
6328 XSETINT (sigcode, VALUE)
6329
6330 if (INTEGERP (sigcode))
6331 ;
6332 else
6333 {
6334 unsigned char *name;
6335
6336 CHECK_SYMBOL (sigcode);
6337 name = SDATA (SYMBOL_NAME (sigcode));
6338
6339 if (!strncmp(name, "SIG", 3) || !strncmp(name, "sig", 3))
6340 name += 3;
6341
6342 if (0)
6343 ;
6344 #ifdef SIGUSR1
6345 parse_signal ("usr1", SIGUSR1);
6346 #endif
6347 #ifdef SIGUSR2
6348 parse_signal ("usr2", SIGUSR2);
6349 #endif
6350 #ifdef SIGTERM
6351 parse_signal ("term", SIGTERM);
6352 #endif
6353 #ifdef SIGHUP
6354 parse_signal ("hup", SIGHUP);
6355 #endif
6356 #ifdef SIGINT
6357 parse_signal ("int", SIGINT);
6358 #endif
6359 #ifdef SIGQUIT
6360 parse_signal ("quit", SIGQUIT);
6361 #endif
6362 #ifdef SIGILL
6363 parse_signal ("ill", SIGILL);
6364 #endif
6365 #ifdef SIGABRT
6366 parse_signal ("abrt", SIGABRT);
6367 #endif
6368 #ifdef SIGEMT
6369 parse_signal ("emt", SIGEMT);
6370 #endif
6371 #ifdef SIGKILL
6372 parse_signal ("kill", SIGKILL);
6373 #endif
6374 #ifdef SIGFPE
6375 parse_signal ("fpe", SIGFPE);
6376 #endif
6377 #ifdef SIGBUS
6378 parse_signal ("bus", SIGBUS);
6379 #endif
6380 #ifdef SIGSEGV
6381 parse_signal ("segv", SIGSEGV);
6382 #endif
6383 #ifdef SIGSYS
6384 parse_signal ("sys", SIGSYS);
6385 #endif
6386 #ifdef SIGPIPE
6387 parse_signal ("pipe", SIGPIPE);
6388 #endif
6389 #ifdef SIGALRM
6390 parse_signal ("alrm", SIGALRM);
6391 #endif
6392 #ifdef SIGURG
6393 parse_signal ("urg", SIGURG);
6394 #endif
6395 #ifdef SIGSTOP
6396 parse_signal ("stop", SIGSTOP);
6397 #endif
6398 #ifdef SIGTSTP
6399 parse_signal ("tstp", SIGTSTP);
6400 #endif
6401 #ifdef SIGCONT
6402 parse_signal ("cont", SIGCONT);
6403 #endif
6404 #ifdef SIGCHLD
6405 parse_signal ("chld", SIGCHLD);
6406 #endif
6407 #ifdef SIGTTIN
6408 parse_signal ("ttin", SIGTTIN);
6409 #endif
6410 #ifdef SIGTTOU
6411 parse_signal ("ttou", SIGTTOU);
6412 #endif
6413 #ifdef SIGIO
6414 parse_signal ("io", SIGIO);
6415 #endif
6416 #ifdef SIGXCPU
6417 parse_signal ("xcpu", SIGXCPU);
6418 #endif
6419 #ifdef SIGXFSZ
6420 parse_signal ("xfsz", SIGXFSZ);
6421 #endif
6422 #ifdef SIGVTALRM
6423 parse_signal ("vtalrm", SIGVTALRM);
6424 #endif
6425 #ifdef SIGPROF
6426 parse_signal ("prof", SIGPROF);
6427 #endif
6428 #ifdef SIGWINCH
6429 parse_signal ("winch", SIGWINCH);
6430 #endif
6431 #ifdef SIGINFO
6432 parse_signal ("info", SIGINFO);
6433 #endif
6434 else
6435 error ("Undefined signal name %s", name);
6436 }
6437
6438 #undef parse_signal
6439
6440 return make_number (kill (pid, XINT (sigcode)));
6441 }
6442
6443 DEFUN ("process-send-eof", Fprocess_send_eof, Sprocess_send_eof, 0, 1, 0,
6444 doc: /* Make PROCESS see end-of-file in its input.
6445 EOF comes after any text already sent to it.
6446 PROCESS may be a process, a buffer, the name of a process or buffer, or
6447 nil, indicating the current buffer's process.
6448 If PROCESS is a network connection, or is a process communicating
6449 through a pipe (as opposed to a pty), then you cannot send any more
6450 text to PROCESS after you call this function.
6451 If PROCESS is a serial process, wait until all output written to the
6452 process has been transmitted to the serial port. */)
6453 (process)
6454 Lisp_Object process;
6455 {
6456 Lisp_Object proc;
6457 struct coding_system *coding;
6458
6459 if (DATAGRAM_CONN_P (process))
6460 return process;
6461
6462 proc = get_process (process);
6463 coding = proc_encode_coding_system[XPROCESS (proc)->outfd];
6464
6465 /* Make sure the process is really alive. */
6466 if (XPROCESS (proc)->raw_status_new)
6467 update_status (XPROCESS (proc));
6468 if (! EQ (XPROCESS (proc)->status, Qrun))
6469 error ("Process %s not running", SDATA (XPROCESS (proc)->name));
6470
6471 if (CODING_REQUIRE_FLUSHING (coding))
6472 {
6473 coding->mode |= CODING_MODE_LAST_BLOCK;
6474 send_process (proc, "", 0, Qnil);
6475 }
6476
6477 if (XPROCESS (proc)->pty_flag)
6478 send_process (proc, "\004", 1, Qnil);
6479 else if (EQ (XPROCESS (proc)->type, Qserial))
6480 {
6481 #ifdef HAVE_TERMIOS
6482 if (tcdrain (XPROCESS (proc)->outfd) != 0)
6483 error ("tcdrain() failed: %s", emacs_strerror (errno));
6484 #endif
6485 /* Do nothing on Windows because writes are blocking. */
6486 }
6487 else
6488 {
6489 int old_outfd, new_outfd;
6490
6491 #ifdef HAVE_SHUTDOWN
6492 /* If this is a network connection, or socketpair is used
6493 for communication with the subprocess, call shutdown to cause EOF.
6494 (In some old system, shutdown to socketpair doesn't work.
6495 Then we just can't win.) */
6496 if (EQ (XPROCESS (proc)->type, Qnetwork)
6497 || XPROCESS (proc)->outfd == XPROCESS (proc)->infd)
6498 shutdown (XPROCESS (proc)->outfd, 1);
6499 /* In case of socketpair, outfd == infd, so don't close it. */
6500 if (XPROCESS (proc)->outfd != XPROCESS (proc)->infd)
6501 emacs_close (XPROCESS (proc)->outfd);
6502 #else /* not HAVE_SHUTDOWN */
6503 emacs_close (XPROCESS (proc)->outfd);
6504 #endif /* not HAVE_SHUTDOWN */
6505 new_outfd = emacs_open (NULL_DEVICE, O_WRONLY, 0);
6506 if (new_outfd < 0)
6507 abort ();
6508 old_outfd = XPROCESS (proc)->outfd;
6509
6510 if (!proc_encode_coding_system[new_outfd])
6511 proc_encode_coding_system[new_outfd]
6512 = (struct coding_system *) xmalloc (sizeof (struct coding_system));
6513 bcopy (proc_encode_coding_system[old_outfd],
6514 proc_encode_coding_system[new_outfd],
6515 sizeof (struct coding_system));
6516 bzero (proc_encode_coding_system[old_outfd],
6517 sizeof (struct coding_system));
6518
6519 XPROCESS (proc)->outfd = new_outfd;
6520 }
6521 return process;
6522 }
6523
6524 /* Kill all processes associated with `buffer'.
6525 If `buffer' is nil, kill all processes */
6526
6527 void
6528 kill_buffer_processes (Lisp_Object buffer)
6529 {
6530 Lisp_Object tail, proc;
6531
6532 for (tail = Vprocess_alist; CONSP (tail); tail = XCDR (tail))
6533 {
6534 proc = XCDR (XCAR (tail));
6535 if (PROCESSP (proc)
6536 && (NILP (buffer) || EQ (XPROCESS (proc)->buffer, buffer)))
6537 {
6538 if (NETCONN_P (proc) || SERIALCONN_P (proc))
6539 Fdelete_process (proc);
6540 else if (XPROCESS (proc)->infd >= 0)
6541 process_send_signal (proc, SIGHUP, Qnil, 1);
6542 }
6543 }
6544 }
6545 \f
6546 /* On receipt of a signal that a child status has changed, loop asking
6547 about children with changed statuses until the system says there
6548 are no more.
6549
6550 All we do is change the status; we do not run sentinels or print
6551 notifications. That is saved for the next time keyboard input is
6552 done, in order to avoid timing errors.
6553
6554 ** WARNING: this can be called during garbage collection.
6555 Therefore, it must not be fooled by the presence of mark bits in
6556 Lisp objects.
6557
6558 ** USG WARNING: Although it is not obvious from the documentation
6559 in signal(2), on a USG system the SIGCLD handler MUST NOT call
6560 signal() before executing at least one wait(), otherwise the
6561 handler will be called again, resulting in an infinite loop. The
6562 relevant portion of the documentation reads "SIGCLD signals will be
6563 queued and the signal-catching function will be continually
6564 reentered until the queue is empty". Invoking signal() causes the
6565 kernel to reexamine the SIGCLD queue. Fred Fish, UniSoft Systems
6566 Inc.
6567
6568 ** Malloc WARNING: This should never call malloc either directly or
6569 indirectly; if it does, that is a bug */
6570
6571 #ifdef SIGCHLD
6572 SIGTYPE
6573 sigchld_handler (int signo)
6574 {
6575 int old_errno = errno;
6576 Lisp_Object proc;
6577 register struct Lisp_Process *p;
6578 extern EMACS_TIME *input_available_clear_time;
6579
6580 SIGNAL_THREAD_CHECK (signo);
6581
6582 while (1)
6583 {
6584 pid_t pid;
6585 int w;
6586 Lisp_Object tail;
6587
6588 #ifdef WNOHANG
6589 #ifndef WUNTRACED
6590 #define WUNTRACED 0
6591 #endif /* no WUNTRACED */
6592 /* Keep trying to get a status until we get a definitive result. */
6593 do
6594 {
6595 errno = 0;
6596 pid = wait3 (&w, WNOHANG | WUNTRACED, 0);
6597 }
6598 while (pid < 0 && errno == EINTR);
6599
6600 if (pid <= 0)
6601 {
6602 /* PID == 0 means no processes found, PID == -1 means a real
6603 failure. We have done all our job, so return. */
6604
6605 errno = old_errno;
6606 return;
6607 }
6608 #else
6609 pid = wait (&w);
6610 #endif /* no WNOHANG */
6611
6612 /* Find the process that signaled us, and record its status. */
6613
6614 /* The process can have been deleted by Fdelete_process. */
6615 for (tail = deleted_pid_list; CONSP (tail); tail = XCDR (tail))
6616 {
6617 Lisp_Object xpid = XCAR (tail);
6618 if ((INTEGERP (xpid) && pid == (pid_t) XINT (xpid))
6619 || (FLOATP (xpid) && pid == (pid_t) XFLOAT_DATA (xpid)))
6620 {
6621 XSETCAR (tail, Qnil);
6622 goto sigchld_end_of_loop;
6623 }
6624 }
6625
6626 /* Otherwise, if it is asynchronous, it is in Vprocess_alist. */
6627 p = 0;
6628 for (tail = Vprocess_alist; CONSP (tail); tail = XCDR (tail))
6629 {
6630 proc = XCDR (XCAR (tail));
6631 p = XPROCESS (proc);
6632 if (EQ (p->type, Qreal) && p->pid == pid)
6633 break;
6634 p = 0;
6635 }
6636
6637 /* Look for an asynchronous process whose pid hasn't been filled
6638 in yet. */
6639 if (p == 0)
6640 for (tail = Vprocess_alist; CONSP (tail); tail = XCDR (tail))
6641 {
6642 proc = XCDR (XCAR (tail));
6643 p = XPROCESS (proc);
6644 if (p->pid == -1)
6645 break;
6646 p = 0;
6647 }
6648
6649 /* Change the status of the process that was found. */
6650 if (p != 0)
6651 {
6652 int clear_desc_flag = 0;
6653
6654 p->tick = ++process_tick;
6655 p->raw_status = w;
6656 p->raw_status_new = 1;
6657
6658 /* If process has terminated, stop waiting for its output. */
6659 if ((WIFSIGNALED (w) || WIFEXITED (w))
6660 && p->infd >= 0)
6661 clear_desc_flag = 1;
6662
6663 /* We use clear_desc_flag to avoid a compiler bug in Microsoft C. */
6664 if (clear_desc_flag)
6665 {
6666 FD_CLR (p->infd, &input_wait_mask);
6667 FD_CLR (p->infd, &non_keyboard_wait_mask);
6668 }
6669
6670 /* Tell wait_reading_process_output that it needs to wake up and
6671 look around. */
6672 if (input_available_clear_time)
6673 EMACS_SET_SECS_USECS (*input_available_clear_time, 0, 0);
6674 }
6675
6676 /* There was no asynchronous process found for that pid: we have
6677 a synchronous process. */
6678 else
6679 {
6680 synch_process_alive = 0;
6681
6682 /* Report the status of the synchronous process. */
6683 if (WIFEXITED (w))
6684 synch_process_retcode = WRETCODE (w);
6685 else if (WIFSIGNALED (w))
6686 synch_process_termsig = WTERMSIG (w);
6687
6688 /* Tell wait_reading_process_output that it needs to wake up and
6689 look around. */
6690 if (input_available_clear_time)
6691 EMACS_SET_SECS_USECS (*input_available_clear_time, 0, 0);
6692 }
6693
6694 sigchld_end_of_loop:
6695 ;
6696
6697 /* On some systems, we must return right away.
6698 If any more processes want to signal us, we will
6699 get another signal.
6700 Otherwise (on systems that have WNOHANG), loop around
6701 to use up all the processes that have something to tell us. */
6702 #if (defined WINDOWSNT \
6703 || (defined USG && !defined GNU_LINUX \
6704 && !(defined HPUX && defined WNOHANG)))
6705 errno = old_errno;
6706 return;
6707 #endif /* USG, but not HPUX with WNOHANG */
6708 }
6709 }
6710 #endif /* SIGCHLD */
6711 \f
6712
6713 static Lisp_Object
6714 exec_sentinel_unwind (Lisp_Object data)
6715 {
6716 XPROCESS (XCAR (data))->sentinel = XCDR (data);
6717 return Qnil;
6718 }
6719
6720 static Lisp_Object
6721 exec_sentinel_error_handler (Lisp_Object error)
6722 {
6723 cmd_error_internal (error, "error in process sentinel: ");
6724 Vinhibit_quit = Qt;
6725 update_echo_area ();
6726 Fsleep_for (make_number (2), Qnil);
6727 return Qt;
6728 }
6729
6730 static void
6731 exec_sentinel (Lisp_Object proc, Lisp_Object reason)
6732 {
6733 Lisp_Object sentinel, obuffer, odeactivate, okeymap;
6734 register struct Lisp_Process *p = XPROCESS (proc);
6735 int count = SPECPDL_INDEX ();
6736 int outer_running_asynch_code = running_asynch_code;
6737 int waiting = waiting_for_user_input_p;
6738
6739 if (inhibit_sentinels)
6740 return;
6741
6742 /* No need to gcpro these, because all we do with them later
6743 is test them for EQness, and none of them should be a string. */
6744 odeactivate = Vdeactivate_mark;
6745 XSETBUFFER (obuffer, current_buffer);
6746 okeymap = current_buffer->keymap;
6747
6748 /* There's no good reason to let sentinels change the current
6749 buffer, and many callers of accept-process-output, sit-for, and
6750 friends don't expect current-buffer to be changed from under them. */
6751 record_unwind_protect (set_buffer_if_live, Fcurrent_buffer ());
6752
6753 sentinel = p->sentinel;
6754 if (NILP (sentinel))
6755 return;
6756
6757 /* Zilch the sentinel while it's running, to avoid recursive invocations;
6758 assure that it gets restored no matter how the sentinel exits. */
6759 p->sentinel = Qnil;
6760 record_unwind_protect (exec_sentinel_unwind, Fcons (proc, sentinel));
6761 /* Inhibit quit so that random quits don't screw up a running filter. */
6762 specbind (Qinhibit_quit, Qt);
6763 specbind (Qlast_nonmenu_event, Qt); /* Why? --Stef */
6764
6765 /* In case we get recursively called,
6766 and we already saved the match data nonrecursively,
6767 save the same match data in safely recursive fashion. */
6768 if (outer_running_asynch_code)
6769 {
6770 Lisp_Object tem;
6771 tem = Fmatch_data (Qnil, Qnil, Qnil);
6772 restore_search_regs ();
6773 record_unwind_save_match_data ();
6774 Fset_match_data (tem, Qt);
6775 }
6776
6777 /* For speed, if a search happens within this code,
6778 save the match data in a special nonrecursive fashion. */
6779 running_asynch_code = 1;
6780
6781 internal_condition_case_1 (read_process_output_call,
6782 Fcons (sentinel,
6783 Fcons (proc, Fcons (reason, Qnil))),
6784 !NILP (Vdebug_on_error) ? Qnil : Qerror,
6785 exec_sentinel_error_handler);
6786
6787 /* If we saved the match data nonrecursively, restore it now. */
6788 restore_search_regs ();
6789 running_asynch_code = outer_running_asynch_code;
6790
6791 Vdeactivate_mark = odeactivate;
6792
6793 /* Restore waiting_for_user_input_p as it was
6794 when we were called, in case the filter clobbered it. */
6795 waiting_for_user_input_p = waiting;
6796
6797 #if 0
6798 if (! EQ (Fcurrent_buffer (), obuffer)
6799 || ! EQ (current_buffer->keymap, okeymap))
6800 #endif
6801 /* But do it only if the caller is actually going to read events.
6802 Otherwise there's no need to make him wake up, and it could
6803 cause trouble (for example it would make sit_for return). */
6804 if (waiting_for_user_input_p == -1)
6805 record_asynch_buffer_change ();
6806
6807 unbind_to (count, Qnil);
6808 }
6809
6810 /* Report all recent events of a change in process status
6811 (either run the sentinel or output a message).
6812 This is usually done while Emacs is waiting for keyboard input
6813 but can be done at other times. */
6814
6815 static void
6816 status_notify (struct Lisp_Process *deleting_process)
6817 {
6818 register Lisp_Object proc, buffer;
6819 Lisp_Object tail, msg;
6820 struct gcpro gcpro1, gcpro2;
6821
6822 tail = Qnil;
6823 msg = Qnil;
6824 /* We need to gcpro tail; if read_process_output calls a filter
6825 which deletes a process and removes the cons to which tail points
6826 from Vprocess_alist, and then causes a GC, tail is an unprotected
6827 reference. */
6828 GCPRO2 (tail, msg);
6829
6830 /* Set this now, so that if new processes are created by sentinels
6831 that we run, we get called again to handle their status changes. */
6832 update_tick = process_tick;
6833
6834 for (tail = Vprocess_alist; CONSP (tail); tail = XCDR (tail))
6835 {
6836 Lisp_Object symbol;
6837 register struct Lisp_Process *p;
6838
6839 proc = Fcdr (XCAR (tail));
6840 p = XPROCESS (proc);
6841
6842 if (p->tick != p->update_tick)
6843 {
6844 p->update_tick = p->tick;
6845
6846 /* If process is still active, read any output that remains. */
6847 while (! EQ (p->filter, Qt)
6848 && ! EQ (p->status, Qconnect)
6849 && ! EQ (p->status, Qlisten)
6850 /* Network or serial process not stopped: */
6851 && ! EQ (p->command, Qt)
6852 && p->infd >= 0
6853 && p != deleting_process
6854 && read_process_output (proc, p->infd) > 0);
6855
6856 buffer = p->buffer;
6857
6858 /* Get the text to use for the message. */
6859 if (p->raw_status_new)
6860 update_status (p);
6861 msg = status_message (p);
6862
6863 /* If process is terminated, deactivate it or delete it. */
6864 symbol = p->status;
6865 if (CONSP (p->status))
6866 symbol = XCAR (p->status);
6867
6868 if (EQ (symbol, Qsignal) || EQ (symbol, Qexit)
6869 || EQ (symbol, Qclosed))
6870 {
6871 if (delete_exited_processes)
6872 remove_process (proc);
6873 else
6874 deactivate_process (proc);
6875 }
6876
6877 /* The actions above may have further incremented p->tick.
6878 So set p->update_tick again
6879 so that an error in the sentinel will not cause
6880 this code to be run again. */
6881 p->update_tick = p->tick;
6882 /* Now output the message suitably. */
6883 if (!NILP (p->sentinel))
6884 exec_sentinel (proc, msg);
6885 /* Don't bother with a message in the buffer
6886 when a process becomes runnable. */
6887 else if (!EQ (symbol, Qrun) && !NILP (buffer))
6888 {
6889 Lisp_Object tem;
6890 struct buffer *old = current_buffer;
6891 int opoint, opoint_byte;
6892 int before, before_byte;
6893
6894 /* Avoid error if buffer is deleted
6895 (probably that's why the process is dead, too) */
6896 if (NILP (XBUFFER (buffer)->name))
6897 continue;
6898 Fset_buffer (buffer);
6899
6900 opoint = PT;
6901 opoint_byte = PT_BYTE;
6902 /* Insert new output into buffer
6903 at the current end-of-output marker,
6904 thus preserving logical ordering of input and output. */
6905 if (XMARKER (p->mark)->buffer)
6906 Fgoto_char (p->mark);
6907 else
6908 SET_PT_BOTH (ZV, ZV_BYTE);
6909
6910 before = PT;
6911 before_byte = PT_BYTE;
6912
6913 tem = current_buffer->read_only;
6914 current_buffer->read_only = Qnil;
6915 insert_string ("\nProcess ");
6916 Finsert (1, &p->name);
6917 insert_string (" ");
6918 Finsert (1, &msg);
6919 current_buffer->read_only = tem;
6920 set_marker_both (p->mark, p->buffer, PT, PT_BYTE);
6921
6922 if (opoint >= before)
6923 SET_PT_BOTH (opoint + (PT - before),
6924 opoint_byte + (PT_BYTE - before_byte));
6925 else
6926 SET_PT_BOTH (opoint, opoint_byte);
6927
6928 set_buffer_internal (old);
6929 }
6930 }
6931 } /* end for */
6932
6933 update_mode_lines++; /* in case buffers use %s in mode-line-format */
6934 UNGCPRO;
6935 }
6936
6937 \f
6938 DEFUN ("set-process-coding-system", Fset_process_coding_system,
6939 Sset_process_coding_system, 1, 3, 0,
6940 doc: /* Set coding systems of PROCESS to DECODING and ENCODING.
6941 DECODING will be used to decode subprocess output and ENCODING to
6942 encode subprocess input. */)
6943 (process, decoding, encoding)
6944 register Lisp_Object process, decoding, encoding;
6945 {
6946 register struct Lisp_Process *p;
6947
6948 CHECK_PROCESS (process);
6949 p = XPROCESS (process);
6950 if (p->infd < 0)
6951 error ("Input file descriptor of %s closed", SDATA (p->name));
6952 if (p->outfd < 0)
6953 error ("Output file descriptor of %s closed", SDATA (p->name));
6954 Fcheck_coding_system (decoding);
6955 Fcheck_coding_system (encoding);
6956 encoding = coding_inherit_eol_type (encoding, Qnil);
6957 p->decode_coding_system = decoding;
6958 p->encode_coding_system = encoding;
6959 setup_process_coding_systems (process);
6960
6961 return Qnil;
6962 }
6963
6964 DEFUN ("process-coding-system",
6965 Fprocess_coding_system, Sprocess_coding_system, 1, 1, 0,
6966 doc: /* Return a cons of coding systems for decoding and encoding of PROCESS. */)
6967 (process)
6968 register Lisp_Object process;
6969 {
6970 CHECK_PROCESS (process);
6971 return Fcons (XPROCESS (process)->decode_coding_system,
6972 XPROCESS (process)->encode_coding_system);
6973 }
6974
6975 DEFUN ("set-process-filter-multibyte", Fset_process_filter_multibyte,
6976 Sset_process_filter_multibyte, 2, 2, 0,
6977 doc: /* Set multibyteness of the strings given to PROCESS's filter.
6978 If FLAG is non-nil, the filter is given multibyte strings.
6979 If FLAG is nil, the filter is given unibyte strings. In this case,
6980 all character code conversion except for end-of-line conversion is
6981 suppressed. */)
6982 (process, flag)
6983 Lisp_Object process, flag;
6984 {
6985 register struct Lisp_Process *p;
6986
6987 CHECK_PROCESS (process);
6988 p = XPROCESS (process);
6989 if (NILP (flag))
6990 p->decode_coding_system = raw_text_coding_system (p->decode_coding_system);
6991 setup_process_coding_systems (process);
6992
6993 return Qnil;
6994 }
6995
6996 DEFUN ("process-filter-multibyte-p", Fprocess_filter_multibyte_p,
6997 Sprocess_filter_multibyte_p, 1, 1, 0,
6998 doc: /* Return t if a multibyte string is given to PROCESS's filter.*/)
6999 (process)
7000 Lisp_Object process;
7001 {
7002 register struct Lisp_Process *p;
7003 struct coding_system *coding;
7004
7005 CHECK_PROCESS (process);
7006 p = XPROCESS (process);
7007 coding = proc_decode_coding_system[p->infd];
7008 return (CODING_FOR_UNIBYTE (coding) ? Qnil : Qt);
7009 }
7010
7011
7012 \f
7013 /* Add DESC to the set of keyboard input descriptors. */
7014
7015 void
7016 add_keyboard_wait_descriptor (int desc)
7017 {
7018 FD_SET (desc, &input_wait_mask);
7019 FD_SET (desc, &non_process_wait_mask);
7020 if (desc > max_keyboard_desc)
7021 max_keyboard_desc = desc;
7022 }
7023
7024 static int add_gpm_wait_descriptor_called_flag;
7025
7026 void
7027 add_gpm_wait_descriptor (int desc)
7028 {
7029 if (! add_gpm_wait_descriptor_called_flag)
7030 FD_CLR (0, &input_wait_mask);
7031 add_gpm_wait_descriptor_called_flag = 1;
7032 FD_SET (desc, &input_wait_mask);
7033 FD_SET (desc, &gpm_wait_mask);
7034 if (desc > max_gpm_desc)
7035 max_gpm_desc = desc;
7036 }
7037
7038 /* From now on, do not expect DESC to give keyboard input. */
7039
7040 void
7041 delete_keyboard_wait_descriptor (int desc)
7042 {
7043 int fd;
7044 int lim = max_keyboard_desc;
7045
7046 FD_CLR (desc, &input_wait_mask);
7047 FD_CLR (desc, &non_process_wait_mask);
7048
7049 if (desc == max_keyboard_desc)
7050 for (fd = 0; fd < lim; fd++)
7051 if (FD_ISSET (fd, &input_wait_mask)
7052 && !FD_ISSET (fd, &non_keyboard_wait_mask)
7053 && !FD_ISSET (fd, &gpm_wait_mask))
7054 max_keyboard_desc = fd;
7055 }
7056
7057 void
7058 delete_gpm_wait_descriptor (int desc)
7059 {
7060 int fd;
7061 int lim = max_gpm_desc;
7062
7063 FD_CLR (desc, &input_wait_mask);
7064 FD_CLR (desc, &non_process_wait_mask);
7065
7066 if (desc == max_gpm_desc)
7067 for (fd = 0; fd < lim; fd++)
7068 if (FD_ISSET (fd, &input_wait_mask)
7069 && !FD_ISSET (fd, &non_keyboard_wait_mask)
7070 && !FD_ISSET (fd, &non_process_wait_mask))
7071 max_gpm_desc = fd;
7072 }
7073
7074 /* Return nonzero if *MASK has a bit set
7075 that corresponds to one of the keyboard input descriptors. */
7076
7077 static int
7078 keyboard_bit_set (fd_set *mask)
7079 {
7080 int fd;
7081
7082 for (fd = 0; fd <= max_keyboard_desc; fd++)
7083 if (FD_ISSET (fd, mask) && FD_ISSET (fd, &input_wait_mask)
7084 && !FD_ISSET (fd, &non_keyboard_wait_mask))
7085 return 1;
7086
7087 return 0;
7088 }
7089 \f
7090 /* Enumeration of and access to system processes a-la ps(1). */
7091
7092 DEFUN ("list-system-processes", Flist_system_processes, Slist_system_processes,
7093 0, 0, 0,
7094 doc: /* Return a list of numerical process IDs of all running processes.
7095 If this functionality is unsupported, return nil.
7096
7097 See `process-attributes' for getting attributes of a process given its ID. */)
7098 ()
7099 {
7100 return list_system_processes ();
7101 }
7102
7103 DEFUN ("process-attributes", Fprocess_attributes,
7104 Sprocess_attributes, 1, 1, 0,
7105 doc: /* Return attributes of the process given by its PID, a number.
7106
7107 Value is an alist where each element is a cons cell of the form
7108
7109 \(KEY . VALUE)
7110
7111 If this functionality is unsupported, the value is nil.
7112
7113 See `list-system-processes' for getting a list of all process IDs.
7114
7115 The KEYs of the attributes that this function may return are listed
7116 below, together with the type of the associated VALUE (in parentheses).
7117 Not all platforms support all of these attributes; unsupported
7118 attributes will not appear in the returned alist.
7119 Unless explicitly indicated otherwise, numbers can have either
7120 integer or floating point values.
7121
7122 euid -- Effective user User ID of the process (number)
7123 user -- User name corresponding to euid (string)
7124 egid -- Effective user Group ID of the process (number)
7125 group -- Group name corresponding to egid (string)
7126 comm -- Command name (executable name only) (string)
7127 state -- Process state code, such as "S", "R", or "T" (string)
7128 ppid -- Parent process ID (number)
7129 pgrp -- Process group ID (number)
7130 sess -- Session ID, i.e. process ID of session leader (number)
7131 ttname -- Controlling tty name (string)
7132 tpgid -- ID of foreground process group on the process's tty (number)
7133 minflt -- number of minor page faults (number)
7134 majflt -- number of major page faults (number)
7135 cminflt -- cumulative number of minor page faults (number)
7136 cmajflt -- cumulative number of major page faults (number)
7137 utime -- user time used by the process, in the (HIGH LOW USEC) format
7138 stime -- system time used by the process, in the (HIGH LOW USEC) format
7139 time -- sum of utime and stime, in the (HIGH LOW USEC) format
7140 cutime -- user time used by the process and its children, (HIGH LOW USEC)
7141 cstime -- system time used by the process and its children, (HIGH LOW USEC)
7142 ctime -- sum of cutime and cstime, in the (HIGH LOW USEC) format
7143 pri -- priority of the process (number)
7144 nice -- nice value of the process (number)
7145 thcount -- process thread count (number)
7146 start -- time the process started, in the (HIGH LOW USEC) format
7147 vsize -- virtual memory size of the process in KB's (number)
7148 rss -- resident set size of the process in KB's (number)
7149 etime -- elapsed time the process is running, in (HIGH LOW USEC) format
7150 pcpu -- percents of CPU time used by the process (floating-point number)
7151 pmem -- percents of total physical memory used by process's resident set
7152 (floating-point number)
7153 args -- command line which invoked the process (string). */)
7154 (pid)
7155
7156 Lisp_Object pid;
7157 {
7158 return system_process_attributes (pid);
7159 }
7160 \f
7161 void
7162 init_process (void)
7163 {
7164 register int i;
7165
7166 inhibit_sentinels = 0;
7167
7168 #ifdef SIGCHLD
7169 #ifndef CANNOT_DUMP
7170 if (! noninteractive || initialized)
7171 #endif
7172 signal (SIGCHLD, sigchld_handler);
7173 #endif
7174
7175 FD_ZERO (&input_wait_mask);
7176 FD_ZERO (&non_keyboard_wait_mask);
7177 FD_ZERO (&non_process_wait_mask);
7178 max_process_desc = 0;
7179
7180 #ifdef NON_BLOCKING_CONNECT
7181 FD_ZERO (&connect_wait_mask);
7182 num_pending_connects = 0;
7183 #endif
7184
7185 #ifdef ADAPTIVE_READ_BUFFERING
7186 process_output_delay_count = 0;
7187 process_output_skip = 0;
7188 #endif
7189
7190 /* Don't do this, it caused infinite select loops. The display
7191 method should call add_keyboard_wait_descriptor on stdin if it
7192 needs that. */
7193 #if 0
7194 FD_SET (0, &input_wait_mask);
7195 #endif
7196
7197 Vprocess_alist = Qnil;
7198 #ifdef SIGCHLD
7199 deleted_pid_list = Qnil;
7200 #endif
7201 for (i = 0; i < MAXDESC; i++)
7202 {
7203 chan_process[i] = Qnil;
7204 proc_buffered_char[i] = -1;
7205 }
7206 bzero (proc_decode_coding_system, sizeof proc_decode_coding_system);
7207 bzero (proc_encode_coding_system, sizeof proc_encode_coding_system);
7208 #ifdef DATAGRAM_SOCKETS
7209 bzero (datagram_address, sizeof datagram_address);
7210 #endif
7211
7212 #ifdef HAVE_SOCKETS
7213 {
7214 Lisp_Object subfeatures = Qnil;
7215 const struct socket_options *sopt;
7216
7217 #define ADD_SUBFEATURE(key, val) \
7218 subfeatures = pure_cons (pure_cons (key, pure_cons (val, Qnil)), subfeatures)
7219
7220 #ifdef NON_BLOCKING_CONNECT
7221 ADD_SUBFEATURE (QCnowait, Qt);
7222 #endif
7223 #ifdef DATAGRAM_SOCKETS
7224 ADD_SUBFEATURE (QCtype, Qdatagram);
7225 #endif
7226 #ifdef HAVE_SEQPACKET
7227 ADD_SUBFEATURE (QCtype, Qseqpacket);
7228 #endif
7229 #ifdef HAVE_LOCAL_SOCKETS
7230 ADD_SUBFEATURE (QCfamily, Qlocal);
7231 #endif
7232 ADD_SUBFEATURE (QCfamily, Qipv4);
7233 #ifdef AF_INET6
7234 ADD_SUBFEATURE (QCfamily, Qipv6);
7235 #endif
7236 #ifdef HAVE_GETSOCKNAME
7237 ADD_SUBFEATURE (QCservice, Qt);
7238 #endif
7239 #if defined(O_NONBLOCK) || defined(O_NDELAY)
7240 ADD_SUBFEATURE (QCserver, Qt);
7241 #endif
7242
7243 for (sopt = socket_options; sopt->name; sopt++)
7244 subfeatures = pure_cons (intern_c_string (sopt->name), subfeatures);
7245
7246 Fprovide (intern_c_string ("make-network-process"), subfeatures);
7247 }
7248 #endif /* HAVE_SOCKETS */
7249
7250 #if defined (DARWIN_OS)
7251 /* PTYs are broken on Darwin < 6, but are sometimes useful for interactive
7252 processes. As such, we only change the default value. */
7253 if (initialized)
7254 {
7255 char *release = get_operating_system_release();
7256 if (!release || !release[0] || (release[0] < MIN_PTY_KERNEL_VERSION
7257 && release[1] == '.')) {
7258 Vprocess_connection_type = Qnil;
7259 }
7260 }
7261 #endif
7262 }
7263
7264 void
7265 syms_of_process (void)
7266 {
7267 Qprocessp = intern_c_string ("processp");
7268 staticpro (&Qprocessp);
7269 Qrun = intern_c_string ("run");
7270 staticpro (&Qrun);
7271 Qstop = intern_c_string ("stop");
7272 staticpro (&Qstop);
7273 Qsignal = intern_c_string ("signal");
7274 staticpro (&Qsignal);
7275
7276 /* Qexit is already staticpro'd by syms_of_eval; don't staticpro it
7277 here again.
7278
7279 Qexit = intern_c_string ("exit");
7280 staticpro (&Qexit); */
7281
7282 Qopen = intern_c_string ("open");
7283 staticpro (&Qopen);
7284 Qclosed = intern_c_string ("closed");
7285 staticpro (&Qclosed);
7286 Qconnect = intern_c_string ("connect");
7287 staticpro (&Qconnect);
7288 Qfailed = intern_c_string ("failed");
7289 staticpro (&Qfailed);
7290 Qlisten = intern_c_string ("listen");
7291 staticpro (&Qlisten);
7292 Qlocal = intern_c_string ("local");
7293 staticpro (&Qlocal);
7294 Qipv4 = intern_c_string ("ipv4");
7295 staticpro (&Qipv4);
7296 #ifdef AF_INET6
7297 Qipv6 = intern_c_string ("ipv6");
7298 staticpro (&Qipv6);
7299 #endif
7300 Qdatagram = intern_c_string ("datagram");
7301 staticpro (&Qdatagram);
7302 Qseqpacket = intern_c_string ("seqpacket");
7303 staticpro (&Qseqpacket);
7304
7305 QCport = intern_c_string (":port");
7306 staticpro (&QCport);
7307 QCspeed = intern_c_string (":speed");
7308 staticpro (&QCspeed);
7309 QCprocess = intern_c_string (":process");
7310 staticpro (&QCprocess);
7311
7312 QCbytesize = intern_c_string (":bytesize");
7313 staticpro (&QCbytesize);
7314 QCstopbits = intern_c_string (":stopbits");
7315 staticpro (&QCstopbits);
7316 QCparity = intern_c_string (":parity");
7317 staticpro (&QCparity);
7318 Qodd = intern_c_string ("odd");
7319 staticpro (&Qodd);
7320 Qeven = intern_c_string ("even");
7321 staticpro (&Qeven);
7322 QCflowcontrol = intern_c_string (":flowcontrol");
7323 staticpro (&QCflowcontrol);
7324 Qhw = intern_c_string ("hw");
7325 staticpro (&Qhw);
7326 Qsw = intern_c_string ("sw");
7327 staticpro (&Qsw);
7328 QCsummary = intern_c_string (":summary");
7329 staticpro (&QCsummary);
7330
7331 Qreal = intern_c_string ("real");
7332 staticpro (&Qreal);
7333 Qnetwork = intern_c_string ("network");
7334 staticpro (&Qnetwork);
7335 Qserial = intern_c_string ("serial");
7336 staticpro (&Qserial);
7337
7338 QCname = intern_c_string (":name");
7339 staticpro (&QCname);
7340 QCbuffer = intern_c_string (":buffer");
7341 staticpro (&QCbuffer);
7342 QChost = intern_c_string (":host");
7343 staticpro (&QChost);
7344 QCservice = intern_c_string (":service");
7345 staticpro (&QCservice);
7346 QCtype = intern_c_string (":type");
7347 staticpro (&QCtype);
7348 QClocal = intern_c_string (":local");
7349 staticpro (&QClocal);
7350 QCremote = intern_c_string (":remote");
7351 staticpro (&QCremote);
7352 QCcoding = intern_c_string (":coding");
7353 staticpro (&QCcoding);
7354 QCserver = intern_c_string (":server");
7355 staticpro (&QCserver);
7356 QCnowait = intern_c_string (":nowait");
7357 staticpro (&QCnowait);
7358 QCsentinel = intern_c_string (":sentinel");
7359 staticpro (&QCsentinel);
7360 QClog = intern_c_string (":log");
7361 staticpro (&QClog);
7362 QCnoquery = intern_c_string (":noquery");
7363 staticpro (&QCnoquery);
7364 QCstop = intern_c_string (":stop");
7365 staticpro (&QCstop);
7366 QCoptions = intern_c_string (":options");
7367 staticpro (&QCoptions);
7368 QCplist = intern_c_string (":plist");
7369 staticpro (&QCplist);
7370
7371 Qlast_nonmenu_event = intern_c_string ("last-nonmenu-event");
7372 staticpro (&Qlast_nonmenu_event);
7373
7374 staticpro (&Vprocess_alist);
7375 #ifdef SIGCHLD
7376 staticpro (&deleted_pid_list);
7377 #endif
7378
7379 Qeuid = intern_c_string ("euid");
7380 staticpro (&Qeuid);
7381 Qegid = intern_c_string ("egid");
7382 staticpro (&Qegid);
7383 Quser = intern_c_string ("user");
7384 staticpro (&Quser);
7385 Qgroup = intern_c_string ("group");
7386 staticpro (&Qgroup);
7387 Qcomm = intern_c_string ("comm");
7388 staticpro (&Qcomm);
7389 Qstate = intern_c_string ("state");
7390 staticpro (&Qstate);
7391 Qppid = intern_c_string ("ppid");
7392 staticpro (&Qppid);
7393 Qpgrp = intern_c_string ("pgrp");
7394 staticpro (&Qpgrp);
7395 Qsess = intern_c_string ("sess");
7396 staticpro (&Qsess);
7397 Qttname = intern_c_string ("ttname");
7398 staticpro (&Qttname);
7399 Qtpgid = intern_c_string ("tpgid");
7400 staticpro (&Qtpgid);
7401 Qminflt = intern_c_string ("minflt");
7402 staticpro (&Qminflt);
7403 Qmajflt = intern_c_string ("majflt");
7404 staticpro (&Qmajflt);
7405 Qcminflt = intern_c_string ("cminflt");
7406 staticpro (&Qcminflt);
7407 Qcmajflt = intern_c_string ("cmajflt");
7408 staticpro (&Qcmajflt);
7409 Qutime = intern_c_string ("utime");
7410 staticpro (&Qutime);
7411 Qstime = intern_c_string ("stime");
7412 staticpro (&Qstime);
7413 Qtime = intern_c_string ("time");
7414 staticpro (&Qtime);
7415 Qcutime = intern_c_string ("cutime");
7416 staticpro (&Qcutime);
7417 Qcstime = intern_c_string ("cstime");
7418 staticpro (&Qcstime);
7419 Qctime = intern_c_string ("ctime");
7420 staticpro (&Qctime);
7421 Qpri = intern_c_string ("pri");
7422 staticpro (&Qpri);
7423 Qnice = intern_c_string ("nice");
7424 staticpro (&Qnice);
7425 Qthcount = intern_c_string ("thcount");
7426 staticpro (&Qthcount);
7427 Qstart = intern_c_string ("start");
7428 staticpro (&Qstart);
7429 Qvsize = intern_c_string ("vsize");
7430 staticpro (&Qvsize);
7431 Qrss = intern_c_string ("rss");
7432 staticpro (&Qrss);
7433 Qetime = intern_c_string ("etime");
7434 staticpro (&Qetime);
7435 Qpcpu = intern_c_string ("pcpu");
7436 staticpro (&Qpcpu);
7437 Qpmem = intern_c_string ("pmem");
7438 staticpro (&Qpmem);
7439 Qargs = intern_c_string ("args");
7440 staticpro (&Qargs);
7441
7442 DEFVAR_BOOL ("delete-exited-processes", &delete_exited_processes,
7443 doc: /* *Non-nil means delete processes immediately when they exit.
7444 A value of nil means don't delete them until `list-processes' is run. */);
7445
7446 delete_exited_processes = 1;
7447
7448 DEFVAR_LISP ("process-connection-type", &Vprocess_connection_type,
7449 doc: /* Control type of device used to communicate with subprocesses.
7450 Values are nil to use a pipe, or t or `pty' to use a pty.
7451 The value has no effect if the system has no ptys or if all ptys are busy:
7452 then a pipe is used in any case.
7453 The value takes effect when `start-process' is called. */);
7454 Vprocess_connection_type = Qt;
7455
7456 #ifdef ADAPTIVE_READ_BUFFERING
7457 DEFVAR_LISP ("process-adaptive-read-buffering", &Vprocess_adaptive_read_buffering,
7458 doc: /* If non-nil, improve receive buffering by delaying after short reads.
7459 On some systems, when Emacs reads the output from a subprocess, the output data
7460 is read in very small blocks, potentially resulting in very poor performance.
7461 This behavior can be remedied to some extent by setting this variable to a
7462 non-nil value, as it will automatically delay reading from such processes, to
7463 allow them to produce more output before Emacs tries to read it.
7464 If the value is t, the delay is reset after each write to the process; any other
7465 non-nil value means that the delay is not reset on write.
7466 The variable takes effect when `start-process' is called. */);
7467 Vprocess_adaptive_read_buffering = Qt;
7468 #endif
7469
7470 defsubr (&Sprocessp);
7471 defsubr (&Sget_process);
7472 defsubr (&Sget_buffer_process);
7473 defsubr (&Sdelete_process);
7474 defsubr (&Sprocess_status);
7475 defsubr (&Sprocess_exit_status);
7476 defsubr (&Sprocess_id);
7477 defsubr (&Sprocess_name);
7478 defsubr (&Sprocess_tty_name);
7479 defsubr (&Sprocess_command);
7480 defsubr (&Sset_process_buffer);
7481 defsubr (&Sprocess_buffer);
7482 defsubr (&Sprocess_mark);
7483 defsubr (&Sset_process_filter);
7484 defsubr (&Sprocess_filter);
7485 defsubr (&Sset_process_sentinel);
7486 defsubr (&Sprocess_sentinel);
7487 defsubr (&Sset_process_window_size);
7488 defsubr (&Sset_process_inherit_coding_system_flag);
7489 defsubr (&Sprocess_inherit_coding_system_flag);
7490 defsubr (&Sset_process_query_on_exit_flag);
7491 defsubr (&Sprocess_query_on_exit_flag);
7492 defsubr (&Sprocess_contact);
7493 defsubr (&Sprocess_plist);
7494 defsubr (&Sset_process_plist);
7495 defsubr (&Slist_processes);
7496 defsubr (&Sprocess_list);
7497 defsubr (&Sstart_process);
7498 #ifdef HAVE_SERIAL
7499 defsubr (&Sserial_process_configure);
7500 defsubr (&Smake_serial_process);
7501 #endif /* HAVE_SERIAL */
7502 #ifdef HAVE_SOCKETS
7503 defsubr (&Sset_network_process_option);
7504 defsubr (&Smake_network_process);
7505 defsubr (&Sformat_network_address);
7506 #endif /* HAVE_SOCKETS */
7507 #if defined(HAVE_SOCKETS) && defined(HAVE_NET_IF_H) && defined(HAVE_SYS_IOCTL_H)
7508 #ifdef SIOCGIFCONF
7509 defsubr (&Snetwork_interface_list);
7510 #endif
7511 #if defined(SIOCGIFADDR) || defined(SIOCGIFHWADDR) || defined(SIOCGIFFLAGS)
7512 defsubr (&Snetwork_interface_info);
7513 #endif
7514 #endif /* HAVE_SOCKETS ... */
7515 #ifdef DATAGRAM_SOCKETS
7516 defsubr (&Sprocess_datagram_address);
7517 defsubr (&Sset_process_datagram_address);
7518 #endif
7519 defsubr (&Saccept_process_output);
7520 defsubr (&Sprocess_send_region);
7521 defsubr (&Sprocess_send_string);
7522 defsubr (&Sinterrupt_process);
7523 defsubr (&Skill_process);
7524 defsubr (&Squit_process);
7525 defsubr (&Sstop_process);
7526 defsubr (&Scontinue_process);
7527 defsubr (&Sprocess_running_child_p);
7528 defsubr (&Sprocess_send_eof);
7529 defsubr (&Ssignal_process);
7530 defsubr (&Swaiting_for_user_input_p);
7531 defsubr (&Sprocess_type);
7532 defsubr (&Sset_process_coding_system);
7533 defsubr (&Sprocess_coding_system);
7534 defsubr (&Sset_process_filter_multibyte);
7535 defsubr (&Sprocess_filter_multibyte_p);
7536 defsubr (&Slist_system_processes);
7537 defsubr (&Sprocess_attributes);
7538 }
7539
7540 \f
7541 #else /* not subprocesses */
7542
7543 #include <sys/types.h>
7544 #include <errno.h>
7545 #include <sys/stat.h>
7546 #include <stdlib.h>
7547 #include <fcntl.h>
7548 #include <setjmp.h>
7549 #ifdef HAVE_UNISTD_H
7550 #include <unistd.h>
7551 #endif
7552
7553 #include "lisp.h"
7554 #include "systime.h"
7555 #include "character.h"
7556 #include "coding.h"
7557 #include "termopts.h"
7558 #include "sysselect.h"
7559
7560 extern int frame_garbaged;
7561
7562 extern EMACS_TIME timer_check ();
7563 extern int timers_run;
7564
7565 Lisp_Object QCtype, QCname;
7566
7567 Lisp_Object Qeuid, Qegid, Qcomm, Qstate, Qppid, Qpgrp, Qsess, Qttname, Qtpgid;
7568 Lisp_Object Qminflt, Qmajflt, Qcminflt, Qcmajflt, Qutime, Qstime, Qcstime;
7569 Lisp_Object Qcutime, Qpri, Qnice, Qthcount, Qstart, Qvsize, Qrss, Qargs;
7570 Lisp_Object Quser, Qgroup, Qetime, Qpcpu, Qpmem, Qtime, Qctime;
7571
7572 /* As described above, except assuming that there are no subprocesses:
7573
7574 Wait for timeout to elapse and/or keyboard input to be available.
7575
7576 time_limit is:
7577 timeout in seconds, or
7578 zero for no limit, or
7579 -1 means gobble data immediately available but don't wait for any.
7580
7581 read_kbd is a Lisp_Object:
7582 0 to ignore keyboard input, or
7583 1 to return when input is available, or
7584 -1 means caller will actually read the input, so don't throw to
7585 the quit handler.
7586
7587 see full version for other parameters. We know that wait_proc will
7588 always be NULL, since `subprocesses' isn't defined.
7589
7590 do_display != 0 means redisplay should be done to show subprocess
7591 output that arrives.
7592
7593 Return true if we received input from any process. */
7594
7595 int
7596 wait_reading_process_output (time_limit, microsecs, read_kbd, do_display,
7597 wait_for_cell, wait_proc, just_wait_proc)
7598 int time_limit, microsecs, read_kbd, do_display;
7599 Lisp_Object wait_for_cell;
7600 struct Lisp_Process *wait_proc;
7601 int just_wait_proc;
7602 {
7603 register int nfds;
7604 EMACS_TIME end_time, timeout;
7605 SELECT_TYPE waitchannels;
7606 int xerrno;
7607
7608 /* What does time_limit really mean? */
7609 if (time_limit || microsecs)
7610 {
7611 EMACS_GET_TIME (end_time);
7612 EMACS_SET_SECS_USECS (timeout, time_limit, microsecs);
7613 EMACS_ADD_TIME (end_time, end_time, timeout);
7614 }
7615
7616 /* Turn off periodic alarms (in case they are in use)
7617 and then turn off any other atimers,
7618 because the select emulator uses alarms. */
7619 stop_polling ();
7620 turn_on_atimers (0);
7621
7622 while (1)
7623 {
7624 int timeout_reduced_for_timers = 0;
7625
7626 /* If calling from keyboard input, do not quit
7627 since we want to return C-g as an input character.
7628 Otherwise, do pending quit if requested. */
7629 if (read_kbd >= 0)
7630 QUIT;
7631
7632 /* Exit now if the cell we're waiting for became non-nil. */
7633 if (! NILP (wait_for_cell) && ! NILP (XCAR (wait_for_cell)))
7634 break;
7635
7636 /* Compute time from now till when time limit is up */
7637 /* Exit if already run out */
7638 if (time_limit == -1)
7639 {
7640 /* -1 specified for timeout means
7641 gobble output available now
7642 but don't wait at all. */
7643
7644 EMACS_SET_SECS_USECS (timeout, 0, 0);
7645 }
7646 else if (time_limit || microsecs)
7647 {
7648 EMACS_GET_TIME (timeout);
7649 EMACS_SUB_TIME (timeout, end_time, timeout);
7650 if (EMACS_TIME_NEG_P (timeout))
7651 break;
7652 }
7653 else
7654 {
7655 EMACS_SET_SECS_USECS (timeout, 100000, 0);
7656 }
7657
7658 /* If our caller will not immediately handle keyboard events,
7659 run timer events directly.
7660 (Callers that will immediately read keyboard events
7661 call timer_delay on their own.) */
7662 if (NILP (wait_for_cell))
7663 {
7664 EMACS_TIME timer_delay;
7665
7666 do
7667 {
7668 int old_timers_run = timers_run;
7669 timer_delay = timer_check (1);
7670 if (timers_run != old_timers_run && do_display)
7671 /* We must retry, since a timer may have requeued itself
7672 and that could alter the time delay. */
7673 redisplay_preserve_echo_area (14);
7674 else
7675 break;
7676 }
7677 while (!detect_input_pending ());
7678
7679 /* If there is unread keyboard input, also return. */
7680 if (read_kbd != 0
7681 && requeued_events_pending_p ())
7682 break;
7683
7684 if (! EMACS_TIME_NEG_P (timer_delay) && time_limit != -1)
7685 {
7686 EMACS_TIME difference;
7687 EMACS_SUB_TIME (difference, timer_delay, timeout);
7688 if (EMACS_TIME_NEG_P (difference))
7689 {
7690 timeout = timer_delay;
7691 timeout_reduced_for_timers = 1;
7692 }
7693 }
7694 }
7695
7696 /* Cause C-g and alarm signals to take immediate action,
7697 and cause input available signals to zero out timeout. */
7698 if (read_kbd < 0)
7699 set_waiting_for_input (&timeout);
7700
7701 /* Wait till there is something to do. */
7702
7703 if (! read_kbd && NILP (wait_for_cell))
7704 FD_ZERO (&waitchannels);
7705 else
7706 FD_SET (0, &waitchannels);
7707
7708 /* If a frame has been newly mapped and needs updating,
7709 reprocess its display stuff. */
7710 if (frame_garbaged && do_display)
7711 {
7712 clear_waiting_for_input ();
7713 redisplay_preserve_echo_area (15);
7714 if (read_kbd < 0)
7715 set_waiting_for_input (&timeout);
7716 }
7717
7718 if (read_kbd && detect_input_pending ())
7719 {
7720 nfds = 0;
7721 FD_ZERO (&waitchannels);
7722 }
7723 else
7724 nfds = select (1, &waitchannels, (SELECT_TYPE *)0, (SELECT_TYPE *)0,
7725 &timeout);
7726
7727 xerrno = errno;
7728
7729 /* Make C-g and alarm signals set flags again */
7730 clear_waiting_for_input ();
7731
7732 /* If we woke up due to SIGWINCH, actually change size now. */
7733 do_pending_window_change (0);
7734
7735 if (time_limit && nfds == 0 && ! timeout_reduced_for_timers)
7736 /* We waited the full specified time, so return now. */
7737 break;
7738
7739 if (nfds == -1)
7740 {
7741 /* If the system call was interrupted, then go around the
7742 loop again. */
7743 if (xerrno == EINTR)
7744 FD_ZERO (&waitchannels);
7745 else
7746 error ("select error: %s", emacs_strerror (xerrno));
7747 }
7748 #ifdef SOLARIS2
7749 else if (nfds > 0 && (waitchannels & 1) && interrupt_input)
7750 /* System sometimes fails to deliver SIGIO. */
7751 kill (getpid (), SIGIO);
7752 #endif
7753 #ifdef SIGIO
7754 if (read_kbd && interrupt_input && (waitchannels & 1))
7755 kill (getpid (), SIGIO);
7756 #endif
7757
7758 /* Check for keyboard input */
7759
7760 if (read_kbd
7761 && detect_input_pending_run_timers (do_display))
7762 {
7763 swallow_events (do_display);
7764 if (detect_input_pending_run_timers (do_display))
7765 break;
7766 }
7767
7768 /* If there is unread keyboard input, also return. */
7769 if (read_kbd
7770 && requeued_events_pending_p ())
7771 break;
7772
7773 /* If wait_for_cell. check for keyboard input
7774 but don't run any timers.
7775 ??? (It seems wrong to me to check for keyboard
7776 input at all when wait_for_cell, but the code
7777 has been this way since July 1994.
7778 Try changing this after version 19.31.) */
7779 if (! NILP (wait_for_cell)
7780 && detect_input_pending ())
7781 {
7782 swallow_events (do_display);
7783 if (detect_input_pending ())
7784 break;
7785 }
7786
7787 /* Exit now if the cell we're waiting for became non-nil. */
7788 if (! NILP (wait_for_cell) && ! NILP (XCAR (wait_for_cell)))
7789 break;
7790 }
7791
7792 start_polling ();
7793
7794 return 0;
7795 }
7796
7797
7798 /* Don't confuse make-docfile by having two doc strings for this function.
7799 make-docfile does not pay attention to #if, for good reason! */
7800 DEFUN ("get-buffer-process", Fget_buffer_process, Sget_buffer_process, 1, 1, 0,
7801 0)
7802 (name)
7803 register Lisp_Object name;
7804 {
7805 return Qnil;
7806 }
7807
7808 /* Don't confuse make-docfile by having two doc strings for this function.
7809 make-docfile does not pay attention to #if, for good reason! */
7810 DEFUN ("process-inherit-coding-system-flag",
7811 Fprocess_inherit_coding_system_flag, Sprocess_inherit_coding_system_flag,
7812 1, 1, 0,
7813 0)
7814 (process)
7815 register Lisp_Object process;
7816 {
7817 /* Ignore the argument and return the value of
7818 inherit-process-coding-system. */
7819 return inherit_process_coding_system ? Qt : Qnil;
7820 }
7821
7822 /* Kill all processes associated with `buffer'.
7823 If `buffer' is nil, kill all processes.
7824 Since we have no subprocesses, this does nothing. */
7825
7826 void
7827 kill_buffer_processes (buffer)
7828 Lisp_Object buffer;
7829 {
7830 }
7831
7832 DEFUN ("list-system-processes", Flist_system_processes, Slist_system_processes,
7833 0, 0, 0,
7834 doc: /* Return a list of numerical process IDs of all running processes.
7835 If this functionality is unsupported, return nil.
7836
7837 See `process-attributes' for getting attributes of a process given its ID. */)
7838 ()
7839 {
7840 return list_system_processes ();
7841 }
7842
7843 DEFUN ("process-attributes", Fprocess_attributes,
7844 Sprocess_attributes, 1, 1, 0,
7845 doc: /* Return attributes of the process given by its PID, a number.
7846
7847 Value is an alist where each element is a cons cell of the form
7848
7849 \(KEY . VALUE)
7850
7851 If this functionality is unsupported, the value is nil.
7852
7853 See `list-system-processes' for getting a list of all process IDs.
7854
7855 The KEYs of the attributes that this function may return are listed
7856 below, together with the type of the associated VALUE (in parentheses).
7857 Not all platforms support all of these attributes; unsupported
7858 attributes will not appear in the returned alist.
7859 Unless explicitly indicated otherwise, numbers can have either
7860 integer or floating point values.
7861
7862 euid -- Effective user User ID of the process (number)
7863 user -- User name corresponding to euid (string)
7864 egid -- Effective user Group ID of the process (number)
7865 group -- Group name corresponding to egid (string)
7866 comm -- Command name (executable name only) (string)
7867 state -- Process state code, such as "S", "R", or "T" (string)
7868 ppid -- Parent process ID (number)
7869 pgrp -- Process group ID (number)
7870 sess -- Session ID, i.e. process ID of session leader (number)
7871 ttname -- Controlling tty name (string)
7872 tpgid -- ID of foreground process group on the process's tty (number)
7873 minflt -- number of minor page faults (number)
7874 majflt -- number of major page faults (number)
7875 cminflt -- cumulative number of minor page faults (number)
7876 cmajflt -- cumulative number of major page faults (number)
7877 utime -- user time used by the process, in the (HIGH LOW USEC) format
7878 stime -- system time used by the process, in the (HIGH LOW USEC) format
7879 time -- sum of utime and stime, in the (HIGH LOW USEC) format
7880 cutime -- user time used by the process and its children, (HIGH LOW USEC)
7881 cstime -- system time used by the process and its children, (HIGH LOW USEC)
7882 ctime -- sum of cutime and cstime, in the (HIGH LOW USEC) format
7883 pri -- priority of the process (number)
7884 nice -- nice value of the process (number)
7885 thcount -- process thread count (number)
7886 start -- time the process started, in the (HIGH LOW USEC) format
7887 vsize -- virtual memory size of the process in KB's (number)
7888 rss -- resident set size of the process in KB's (number)
7889 etime -- elapsed time the process is running, in (HIGH LOW USEC) format
7890 pcpu -- percents of CPU time used by the process (floating-point number)
7891 pmem -- percents of total physical memory used by process's resident set
7892 (floating-point number)
7893 args -- command line which invoked the process (string). */)
7894 (pid)
7895
7896 Lisp_Object pid;
7897 {
7898 return system_process_attributes (pid);
7899 }
7900
7901 void
7902 init_process ()
7903 {
7904 }
7905
7906 void
7907 syms_of_process ()
7908 {
7909 QCtype = intern_c_string (":type");
7910 staticpro (&QCtype);
7911 QCname = intern_c_string (":name");
7912 staticpro (&QCname);
7913 QCtype = intern_c_string (":type");
7914 staticpro (&QCtype);
7915 QCname = intern_c_string (":name");
7916 staticpro (&QCname);
7917 Qeuid = intern_c_string ("euid");
7918 staticpro (&Qeuid);
7919 Qegid = intern_c_string ("egid");
7920 staticpro (&Qegid);
7921 Quser = intern_c_string ("user");
7922 staticpro (&Quser);
7923 Qgroup = intern_c_string ("group");
7924 staticpro (&Qgroup);
7925 Qcomm = intern_c_string ("comm");
7926 staticpro (&Qcomm);
7927 Qstate = intern_c_string ("state");
7928 staticpro (&Qstate);
7929 Qppid = intern_c_string ("ppid");
7930 staticpro (&Qppid);
7931 Qpgrp = intern_c_string ("pgrp");
7932 staticpro (&Qpgrp);
7933 Qsess = intern_c_string ("sess");
7934 staticpro (&Qsess);
7935 Qttname = intern_c_string ("ttname");
7936 staticpro (&Qttname);
7937 Qtpgid = intern_c_string ("tpgid");
7938 staticpro (&Qtpgid);
7939 Qminflt = intern_c_string ("minflt");
7940 staticpro (&Qminflt);
7941 Qmajflt = intern_c_string ("majflt");
7942 staticpro (&Qmajflt);
7943 Qcminflt = intern_c_string ("cminflt");
7944 staticpro (&Qcminflt);
7945 Qcmajflt = intern_c_string ("cmajflt");
7946 staticpro (&Qcmajflt);
7947 Qutime = intern_c_string ("utime");
7948 staticpro (&Qutime);
7949 Qstime = intern_c_string ("stime");
7950 staticpro (&Qstime);
7951 Qtime = intern_c_string ("time");
7952 staticpro (&Qtime);
7953 Qcutime = intern_c_string ("cutime");
7954 staticpro (&Qcutime);
7955 Qcstime = intern_c_string ("cstime");
7956 staticpro (&Qcstime);
7957 Qctime = intern_c_string ("ctime");
7958 staticpro (&Qctime);
7959 Qpri = intern_c_string ("pri");
7960 staticpro (&Qpri);
7961 Qnice = intern_c_string ("nice");
7962 staticpro (&Qnice);
7963 Qthcount = intern_c_string ("thcount");
7964 staticpro (&Qthcount);
7965 Qstart = intern_c_string ("start");
7966 staticpro (&Qstart);
7967 Qvsize = intern_c_string ("vsize");
7968 staticpro (&Qvsize);
7969 Qrss = intern_c_string ("rss");
7970 staticpro (&Qrss);
7971 Qetime = intern_c_string ("etime");
7972 staticpro (&Qetime);
7973 Qpcpu = intern_c_string ("pcpu");
7974 staticpro (&Qpcpu);
7975 Qpmem = intern_c_string ("pmem");
7976 staticpro (&Qpmem);
7977 Qargs = intern_c_string ("args");
7978 staticpro (&Qargs);
7979
7980 defsubr (&Sget_buffer_process);
7981 defsubr (&Sprocess_inherit_coding_system_flag);
7982 defsubr (&Slist_system_processes);
7983 defsubr (&Sprocess_attributes);
7984 }
7985
7986 \f
7987 #endif /* not subprocesses */
7988
7989 /* arch-tag: 3706c011-7b9a-4117-bd4f-59e7f701a4c4
7990 (do not change this comment) */