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