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