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 Free Software Foundation, Inc.
6 This file is part of GNU Emacs.
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.
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.
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/>. */
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. */
38 #include <sys/types.h> /* some typedefs are used in sys/file.h */
41 #ifdef HAVE_INTTYPES_H
53 #ifdef HAVE_SOCKETS /* TCP connection support, if kernel can do it */
54 #include <sys/socket.h>
56 #include <netinet/in.h>
57 #include <arpa/inet.h>
59 /* Are local (unix) sockets supported? */
60 #if defined (HAVE_SYS_UN_H)
61 #if !defined (AF_LOCAL) && defined (AF_UNIX)
62 #define AF_LOCAL AF_UNIX
65 #define HAVE_LOCAL_SOCKETS
69 #endif /* HAVE_SOCKETS */
71 #if defined(BSD_SYSTEM)
72 #include <sys/ioctl.h>
73 #if !defined (O_NDELAY) && defined (HAVE_PTYS) && !defined(USG5)
75 #endif /* HAVE_PTYS and no O_NDELAY */
76 #endif /* BSD_SYSTEM */
82 /* Can we use SIOCGIFCONF and/or SIOCGIFADDR */
84 #if defined(HAVE_SYS_IOCTL_H) && defined(HAVE_NET_IF_H)
85 /* sys/ioctl.h may have been included already */
87 #include <sys/ioctl.h>
98 #include <netinet/in.h>
99 #include <arpa/nameser.h>
109 #include "character.h"
113 #include "termhooks.h"
114 #include "termopts.h"
115 #include "commands.h"
116 #include "keyboard.h"
117 #include "blockinput.h"
118 #include "dispextern.h"
119 #include "composite.h"
122 Lisp_Object Qprocessp
;
123 Lisp_Object Qrun
, Qstop
, Qsignal
;
124 Lisp_Object Qopen
, Qclosed
, Qconnect
, Qfailed
, Qlisten
;
125 Lisp_Object Qlocal
, Qipv4
, Qdatagram
;
126 Lisp_Object Qreal
, Qnetwork
, Qserial
;
130 Lisp_Object QCport
, QCspeed
, QCprocess
;
131 Lisp_Object QCbytesize
, QCstopbits
, QCparity
, Qodd
, Qeven
;
132 Lisp_Object QCflowcontrol
, Qhw
, Qsw
, QCsummary
;
133 Lisp_Object QCname
, QCbuffer
, QChost
, QCservice
, QCtype
;
134 Lisp_Object QClocal
, QCremote
, QCcoding
;
135 Lisp_Object QCserver
, QCnowait
, QCnoquery
, QCstop
;
136 Lisp_Object QCsentinel
, QClog
, QCoptions
, QCplist
;
137 Lisp_Object Qlast_nonmenu_event
;
138 /* QCfamily is declared and initialized in xfaces.c,
139 QCfilter in keyboard.c. */
140 extern Lisp_Object QCfamily
, QCfilter
;
142 /* Qexit is declared and initialized in eval.c. */
144 /* QCfamily is defined in xfaces.c. */
145 extern Lisp_Object QCfamily
;
146 /* QCfilter is defined in keyboard.c. */
147 extern Lisp_Object QCfilter
;
149 Lisp_Object Qeuid
, Qegid
, Qcomm
, Qstate
, Qppid
, Qpgrp
, Qsess
, Qttname
, Qtpgid
;
150 Lisp_Object Qminflt
, Qmajflt
, Qcminflt
, Qcmajflt
, Qutime
, Qstime
, Qcstime
;
151 Lisp_Object Qcutime
, Qpri
, Qnice
, Qthcount
, Qstart
, Qvsize
, Qrss
, Qargs
;
152 Lisp_Object Quser
, Qgroup
, Qetime
, Qpcpu
, Qpmem
, Qtime
, Qctime
;
155 #define NETCONN_P(p) (EQ (XPROCESS (p)->type, Qnetwork))
156 #define NETCONN1_P(p) (EQ ((p)->type, Qnetwork))
157 #define SERIALCONN_P(p) (EQ (XPROCESS (p)->type, Qserial))
158 #define SERIALCONN1_P(p) (EQ ((p)->type, Qserial))
160 #define NETCONN_P(p) 0
161 #define NETCONN1_P(p) 0
162 #define SERIALCONN_P(p) 0
163 #define SERIALCONN1_P(p) 0
164 #endif /* HAVE_SOCKETS */
166 /* Define first descriptor number available for subprocesses. */
167 #define FIRST_PROC_DESC 3
169 /* Define SIGCHLD as an alias for SIGCLD. There are many conditionals
172 #if !defined (SIGCHLD) && defined (SIGCLD)
173 #define SIGCHLD SIGCLD
176 #include "syssignal.h"
180 extern char *get_operating_system_release ();
182 /* Serial processes require termios or Windows. */
183 #if defined (HAVE_TERMIOS) || defined (WINDOWSNT)
188 /* From sysdep.c or w32.c */
189 extern int serial_open (char *port
);
190 extern void serial_configure (struct Lisp_Process
*p
, Lisp_Object contact
);
201 /* t means use pty, nil means use a pipe,
202 maybe other values to come. */
203 static Lisp_Object Vprocess_connection_type
;
205 /* These next two vars are non-static since sysdep.c uses them in the
206 emulation of `select'. */
207 /* Number of events of change of status of a process. */
209 /* Number of events for which the user or sentinel has been notified. */
212 /* Define NON_BLOCKING_CONNECT if we can support non-blocking connects. */
214 #ifdef BROKEN_NON_BLOCKING_CONNECT
215 #undef NON_BLOCKING_CONNECT
217 #ifndef NON_BLOCKING_CONNECT
220 #if defined (HAVE_GETPEERNAME) || defined (GNU_LINUX)
221 #if defined (O_NONBLOCK) || defined (O_NDELAY)
222 #if defined (EWOULDBLOCK) || defined (EINPROGRESS)
223 #define NON_BLOCKING_CONNECT
224 #endif /* EWOULDBLOCK || EINPROGRESS */
225 #endif /* O_NONBLOCK || O_NDELAY */
226 #endif /* HAVE_GETPEERNAME || GNU_LINUX */
227 #endif /* HAVE_SELECT */
228 #endif /* HAVE_SOCKETS */
229 #endif /* NON_BLOCKING_CONNECT */
230 #endif /* BROKEN_NON_BLOCKING_CONNECT */
232 /* Define DATAGRAM_SOCKETS if datagrams can be used safely on
233 this system. We need to read full packets, so we need a
234 "non-destructive" select. So we require either native select,
235 or emulation of select using FIONREAD. */
237 #ifdef BROKEN_DATAGRAM_SOCKETS
238 #undef DATAGRAM_SOCKETS
240 #ifndef DATAGRAM_SOCKETS
242 #if defined (HAVE_SELECT) || defined (FIONREAD)
243 #if defined (HAVE_SENDTO) && defined (HAVE_RECVFROM) && defined (EMSGSIZE)
244 #define DATAGRAM_SOCKETS
245 #endif /* HAVE_SENDTO && HAVE_RECVFROM && EMSGSIZE */
246 #endif /* HAVE_SELECT || FIONREAD */
247 #endif /* HAVE_SOCKETS */
248 #endif /* DATAGRAM_SOCKETS */
249 #endif /* BROKEN_DATAGRAM_SOCKETS */
251 #if !defined (ADAPTIVE_READ_BUFFERING) && !defined (NO_ADAPTIVE_READ_BUFFERING)
252 #ifdef EMACS_HAS_USECS
253 #define ADAPTIVE_READ_BUFFERING
257 #ifdef ADAPTIVE_READ_BUFFERING
258 #define READ_OUTPUT_DELAY_INCREMENT 10000
259 #define READ_OUTPUT_DELAY_MAX (READ_OUTPUT_DELAY_INCREMENT * 5)
260 #define READ_OUTPUT_DELAY_MAX_MAX (READ_OUTPUT_DELAY_INCREMENT * 7)
262 /* Number of processes which have a non-zero read_output_delay,
263 and therefore might be delayed for adaptive read buffering. */
265 static int process_output_delay_count
;
267 /* Non-zero if any process has non-nil read_output_skip. */
269 static int process_output_skip
;
271 /* Non-nil means to delay reading process output to improve buffering.
272 A value of t means that delay is reset after each send, any other
273 non-nil value does not reset the delay. A value of nil disables
274 adaptive read buffering completely. */
275 static Lisp_Object Vprocess_adaptive_read_buffering
;
277 #define process_output_delay_count 0
281 #include "sysselect.h"
283 static int keyboard_bit_set
P_ ((SELECT_TYPE
*));
284 static void deactivate_process
P_ ((Lisp_Object
));
285 static void status_notify
P_ ((struct Lisp_Process
*));
286 static int read_process_output
P_ ((Lisp_Object
, int));
287 static void create_pty
P_ ((Lisp_Object
));
289 /* If we support a window system, turn on the code to poll periodically
290 to detect C-g. It isn't actually used when doing interrupt input. */
291 #ifdef HAVE_WINDOW_SYSTEM
292 #define POLL_FOR_INPUT
295 static Lisp_Object
get_process ();
296 static void exec_sentinel ();
298 extern EMACS_TIME
timer_check ();
299 extern int timers_run
;
301 /* Mask of bits indicating the descriptors that we wait for input on. */
303 static SELECT_TYPE input_wait_mask
;
305 /* Mask that excludes keyboard input descriptor(s). */
307 static SELECT_TYPE non_keyboard_wait_mask
;
309 /* Mask that excludes process input descriptor(s). */
311 static SELECT_TYPE non_process_wait_mask
;
313 /* Mask for the gpm mouse input descriptor. */
315 static SELECT_TYPE gpm_wait_mask
;
317 #ifdef NON_BLOCKING_CONNECT
318 /* Mask of bits indicating the descriptors that we wait for connect to
319 complete on. Once they complete, they are removed from this mask
320 and added to the input_wait_mask and non_keyboard_wait_mask. */
322 static SELECT_TYPE connect_wait_mask
;
324 /* Number of bits set in connect_wait_mask. */
325 static int num_pending_connects
;
327 #define IF_NON_BLOCKING_CONNECT(s) s
329 #define IF_NON_BLOCKING_CONNECT(s)
332 /* The largest descriptor currently in use for a process object. */
333 static int max_process_desc
;
335 /* The largest descriptor currently in use for keyboard input. */
336 static int max_keyboard_desc
;
338 /* The largest descriptor currently in use for gpm mouse input. */
339 static int max_gpm_desc
;
341 /* Nonzero means delete a process right away if it exits. */
342 static int delete_exited_processes
;
344 /* Indexed by descriptor, gives the process (if any) for that descriptor */
345 Lisp_Object chan_process
[MAXDESC
];
347 /* Alist of elements (NAME . PROCESS) */
348 Lisp_Object Vprocess_alist
;
350 /* Buffered-ahead input char from process, indexed by channel.
351 -1 means empty (no char is buffered).
352 Used on sys V where the only way to tell if there is any
353 output from the process is to read at least one char.
354 Always -1 on systems that support FIONREAD. */
356 /* Don't make static; need to access externally. */
357 int proc_buffered_char
[MAXDESC
];
359 /* Table of `struct coding-system' for each process. */
360 static struct coding_system
*proc_decode_coding_system
[MAXDESC
];
361 static struct coding_system
*proc_encode_coding_system
[MAXDESC
];
363 #ifdef DATAGRAM_SOCKETS
364 /* Table of `partner address' for datagram sockets. */
365 struct sockaddr_and_len
{
368 } datagram_address
[MAXDESC
];
369 #define DATAGRAM_CHAN_P(chan) (datagram_address[chan].sa != 0)
370 #define DATAGRAM_CONN_P(proc) (PROCESSP (proc) && datagram_address[XPROCESS (proc)->infd].sa != 0)
372 #define DATAGRAM_CHAN_P(chan) (0)
373 #define DATAGRAM_CONN_P(proc) (0)
376 /* Maximum number of bytes to send to a pty without an eof. */
377 static int pty_max_bytes
;
379 /* Nonzero means don't run process sentinels. This is used
381 int inhibit_sentinels
;
387 /* The file name of the pty opened by allocate_pty. */
389 static char pty_name
[24];
392 /* Compute the Lisp form of the process status, p->status, from
393 the numeric status that was returned by `wait'. */
395 static Lisp_Object
status_convert (int);
399 struct Lisp_Process
*p
;
401 eassert (p
->raw_status_new
);
402 p
->status
= status_convert (p
->raw_status
);
403 p
->raw_status_new
= 0;
406 /* Convert a process status word in Unix format to
407 the list that we use internally. */
410 status_convert (int w
)
413 return Fcons (Qstop
, Fcons (make_number (WSTOPSIG (w
)), Qnil
));
414 else if (WIFEXITED (w
))
415 return Fcons (Qexit
, Fcons (make_number (WRETCODE (w
)),
416 WCOREDUMP (w
) ? Qt
: Qnil
));
417 else if (WIFSIGNALED (w
))
418 return Fcons (Qsignal
, Fcons (make_number (WTERMSIG (w
)),
419 WCOREDUMP (w
) ? Qt
: Qnil
));
424 /* Given a status-list, extract the three pieces of information
425 and store them individually through the three pointers. */
428 decode_status (l
, symbol
, code
, coredump
)
446 *code
= XFASTINT (XCAR (tem
));
448 *coredump
= !NILP (tem
);
452 /* Return a string describing a process status list. */
456 struct Lisp_Process
*p
;
458 Lisp_Object status
= p
->status
;
461 Lisp_Object string
, string2
;
463 decode_status (status
, &symbol
, &code
, &coredump
);
465 if (EQ (symbol
, Qsignal
) || EQ (symbol
, Qstop
))
468 synchronize_system_messages_locale ();
469 signame
= strsignal (code
);
471 string
= build_string ("unknown");
476 string
= make_unibyte_string (signame
, strlen (signame
));
477 if (! NILP (Vlocale_coding_system
))
478 string
= (code_convert_string_norecord
479 (string
, Vlocale_coding_system
, 0));
480 c1
= STRING_CHAR ((char *) SDATA (string
), 0);
483 Faset (string
, make_number (0), make_number (c2
));
485 string2
= build_string (coredump
? " (core dumped)\n" : "\n");
486 return concat2 (string
, string2
);
488 else if (EQ (symbol
, Qexit
))
491 return build_string (code
== 0 ? "deleted\n" : "connection broken by remote peer\n");
493 return build_string ("finished\n");
494 string
= Fnumber_to_string (make_number (code
));
495 string2
= build_string (coredump
? " (core dumped)\n" : "\n");
496 return concat3 (build_string ("exited abnormally with code "),
499 else if (EQ (symbol
, Qfailed
))
501 string
= Fnumber_to_string (make_number (code
));
502 string2
= build_string ("\n");
503 return concat3 (build_string ("failed with code "),
507 return Fcopy_sequence (Fsymbol_name (symbol
));
512 /* Open an available pty, returning a file descriptor.
513 Return -1 on failure.
514 The file name of the terminal corresponding to the pty
515 is left in the variable pty_name. */
526 for (c
= FIRST_PTY_LETTER
; c
<= 'z'; c
++)
527 for (i
= 0; i
< 16; i
++)
530 struct stat stb
; /* Used in some PTY_OPEN. */
531 #ifdef PTY_NAME_SPRINTF
534 sprintf (pty_name
, "/dev/pty%c%x", c
, i
);
535 #endif /* no PTY_NAME_SPRINTF */
539 #else /* no PTY_OPEN */
541 { /* Some systems name their pseudoterminals so that there are gaps in
542 the usual sequence - for example, on HP9000/S700 systems, there
543 are no pseudoterminals with names ending in 'f'. So we wait for
544 three failures in a row before deciding that we've reached the
546 int failed_count
= 0;
548 if (stat (pty_name
, &stb
) < 0)
551 if (failed_count
>= 3)
558 fd
= emacs_open (pty_name
, O_RDWR
| O_NONBLOCK
, 0);
560 fd
= emacs_open (pty_name
, O_RDWR
| O_NDELAY
, 0);
563 #endif /* no PTY_OPEN */
567 /* check to make certain that both sides are available
568 this avoids a nasty yet stupid bug in rlogins */
569 #ifdef PTY_TTY_NAME_SPRINTF
572 sprintf (pty_name
, "/dev/tty%c%x", c
, i
);
573 #endif /* no PTY_TTY_NAME_SPRINTF */
574 if (access (pty_name
, 6) != 0)
589 #endif /* HAVE_PTYS */
595 register Lisp_Object val
, tem
, name1
;
596 register struct Lisp_Process
*p
;
600 p
= allocate_process ();
608 p
->raw_status_new
= 0;
610 p
->mark
= Fmake_marker ();
611 p
->kill_without_query
= 0;
613 #ifdef ADAPTIVE_READ_BUFFERING
614 p
->adaptive_read_buffering
= 0;
615 p
->read_output_delay
= 0;
616 p
->read_output_skip
= 0;
619 /* If name is already in use, modify it until it is unused. */
624 tem
= Fget_process (name1
);
625 if (NILP (tem
)) break;
626 sprintf (suffix
, "<%d>", i
);
627 name1
= concat2 (name
, build_string (suffix
));
631 XSETPROCESS (val
, p
);
632 Vprocess_alist
= Fcons (Fcons (name
, val
), Vprocess_alist
);
637 remove_process (proc
)
638 register Lisp_Object proc
;
640 register Lisp_Object pair
;
642 pair
= Frassq (proc
, Vprocess_alist
);
643 Vprocess_alist
= Fdelq (pair
, Vprocess_alist
);
645 deactivate_process (proc
);
648 /* Setup coding systems of PROCESS. */
651 setup_process_coding_systems (process
)
654 struct Lisp_Process
*p
= XPROCESS (process
);
656 int outch
= p
->outfd
;
657 Lisp_Object coding_system
;
659 if (inch
< 0 || outch
< 0)
662 if (!proc_decode_coding_system
[inch
])
663 proc_decode_coding_system
[inch
]
664 = (struct coding_system
*) xmalloc (sizeof (struct coding_system
));
665 coding_system
= p
->decode_coding_system
;
666 if (! NILP (p
->filter
))
668 else if (BUFFERP (p
->buffer
))
670 if (NILP (XBUFFER (p
->buffer
)->enable_multibyte_characters
))
671 coding_system
= raw_text_coding_system (coding_system
);
673 setup_coding_system (coding_system
, proc_decode_coding_system
[inch
]);
675 if (!proc_encode_coding_system
[outch
])
676 proc_encode_coding_system
[outch
]
677 = (struct coding_system
*) xmalloc (sizeof (struct coding_system
));
678 setup_coding_system (p
->encode_coding_system
,
679 proc_encode_coding_system
[outch
]);
682 DEFUN ("processp", Fprocessp
, Sprocessp
, 1, 1, 0,
683 doc
: /* Return t if OBJECT is a process. */)
687 return PROCESSP (object
) ? Qt
: Qnil
;
690 DEFUN ("get-process", Fget_process
, Sget_process
, 1, 1, 0,
691 doc
: /* Return the process named NAME, or nil if there is none. */)
693 register Lisp_Object name
;
698 return Fcdr (Fassoc (name
, Vprocess_alist
));
701 DEFUN ("get-buffer-process", Fget_buffer_process
, Sget_buffer_process
, 1, 1, 0,
702 doc
: /* Return the (or a) process associated with BUFFER.
703 BUFFER may be a buffer or the name of one. */)
705 register Lisp_Object buffer
;
707 register Lisp_Object buf
, tail
, proc
;
709 if (NILP (buffer
)) return Qnil
;
710 buf
= Fget_buffer (buffer
);
711 if (NILP (buf
)) return Qnil
;
713 for (tail
= Vprocess_alist
; CONSP (tail
); tail
= XCDR (tail
))
715 proc
= Fcdr (XCAR (tail
));
716 if (PROCESSP (proc
) && EQ (XPROCESS (proc
)->buffer
, buf
))
722 /* This is how commands for the user decode process arguments. It
723 accepts a process, a process name, a buffer, a buffer name, or nil.
724 Buffers denote the first process in the buffer, and nil denotes the
729 register Lisp_Object name
;
731 register Lisp_Object proc
, obj
;
734 obj
= Fget_process (name
);
736 obj
= Fget_buffer (name
);
738 error ("Process %s does not exist", SDATA (name
));
740 else if (NILP (name
))
741 obj
= Fcurrent_buffer ();
745 /* Now obj should be either a buffer object or a process object.
749 proc
= Fget_buffer_process (obj
);
751 error ("Buffer %s has no process", SDATA (XBUFFER (obj
)->name
));
763 /* Fdelete_process promises to immediately forget about the process, but in
764 reality, Emacs needs to remember those processes until they have been
765 treated by sigchld_handler; otherwise this handler would consider the
766 process as being synchronous and say that the synchronous process is
768 static Lisp_Object deleted_pid_list
;
771 DEFUN ("delete-process", Fdelete_process
, Sdelete_process
, 1, 1, 0,
772 doc
: /* Delete PROCESS: kill it and forget about it immediately.
773 PROCESS may be a process, a buffer, the name of a process or buffer, or
774 nil, indicating the current buffer's process. */)
776 register Lisp_Object process
;
778 register struct Lisp_Process
*p
;
780 process
= get_process (process
);
781 p
= XPROCESS (process
);
783 p
->raw_status_new
= 0;
784 if (NETCONN1_P (p
) || SERIALCONN1_P (p
))
786 p
->status
= Fcons (Qexit
, Fcons (make_number (0), Qnil
));
787 p
->tick
= ++process_tick
;
789 redisplay_preserve_echo_area (13);
791 else if (p
->infd
>= 0)
795 /* Assignment to EMACS_INT stops GCC whining about limited range
797 EMACS_INT pid
= p
->pid
;
799 /* No problem storing the pid here, as it is still in Vprocess_alist. */
800 deleted_pid_list
= Fcons (make_fixnum_or_float (pid
),
801 /* GC treated elements set to nil. */
802 Fdelq (Qnil
, deleted_pid_list
));
803 /* If the process has already signaled, remove it from the list. */
804 if (p
->raw_status_new
)
807 if (CONSP (p
->status
))
808 symbol
= XCAR (p
->status
);
809 if (EQ (symbol
, Qsignal
) || EQ (symbol
, Qexit
))
811 = Fdelete (make_fixnum_or_float (pid
), deleted_pid_list
);
815 Fkill_process (process
, Qnil
);
816 /* Do this now, since remove_process will make sigchld_handler do nothing. */
818 = Fcons (Qsignal
, Fcons (make_number (SIGKILL
), Qnil
));
819 p
->tick
= ++process_tick
;
821 redisplay_preserve_echo_area (13);
824 remove_process (process
);
828 DEFUN ("process-status", Fprocess_status
, Sprocess_status
, 1, 1, 0,
829 doc
: /* Return the status of PROCESS.
830 The returned value is one of the following symbols:
831 run -- for a process that is running.
832 stop -- for a process stopped but continuable.
833 exit -- for a process that has exited.
834 signal -- for a process that has got a fatal signal.
835 open -- for a network stream connection that is open.
836 listen -- for a network stream server that is listening.
837 closed -- for a network stream connection that is closed.
838 connect -- when waiting for a non-blocking connection to complete.
839 failed -- when a non-blocking connection has failed.
840 nil -- if arg is a process name and no such process exists.
841 PROCESS may be a process, a buffer, the name of a process, or
842 nil, indicating the current buffer's process. */)
844 register Lisp_Object process
;
846 register struct Lisp_Process
*p
;
847 register Lisp_Object status
;
849 if (STRINGP (process
))
850 process
= Fget_process (process
);
852 process
= get_process (process
);
857 p
= XPROCESS (process
);
858 if (p
->raw_status_new
)
862 status
= XCAR (status
);
863 if (NETCONN1_P (p
) || SERIALCONN1_P (p
))
865 if (EQ (status
, Qexit
))
867 else if (EQ (p
->command
, Qt
))
869 else if (EQ (status
, Qrun
))
875 DEFUN ("process-exit-status", Fprocess_exit_status
, Sprocess_exit_status
,
877 doc
: /* Return the exit status of PROCESS or the signal number that killed it.
878 If PROCESS has not yet exited or died, return 0. */)
880 register Lisp_Object process
;
882 CHECK_PROCESS (process
);
883 if (XPROCESS (process
)->raw_status_new
)
884 update_status (XPROCESS (process
));
885 if (CONSP (XPROCESS (process
)->status
))
886 return XCAR (XCDR (XPROCESS (process
)->status
));
887 return make_number (0);
890 DEFUN ("process-id", Fprocess_id
, Sprocess_id
, 1, 1, 0,
891 doc
: /* Return the process id of PROCESS.
892 This is the pid of the external process which PROCESS uses or talks to.
893 For a network connection, this value is nil. */)
895 register Lisp_Object process
;
897 /* Assignment to EMACS_INT stops GCC whining about limited range of
901 CHECK_PROCESS (process
);
902 pid
= XPROCESS (process
)->pid
;
903 return (pid
? make_fixnum_or_float (pid
) : Qnil
);
906 DEFUN ("process-name", Fprocess_name
, Sprocess_name
, 1, 1, 0,
907 doc
: /* Return the name of PROCESS, as a string.
908 This is the name of the program invoked in PROCESS,
909 possibly modified to make it unique among process names. */)
911 register Lisp_Object process
;
913 CHECK_PROCESS (process
);
914 return XPROCESS (process
)->name
;
917 DEFUN ("process-command", Fprocess_command
, Sprocess_command
, 1, 1, 0,
918 doc
: /* Return the command that was executed to start PROCESS.
919 This is a list of strings, the first string being the program executed
920 and the rest of the strings being the arguments given to it.
921 For a network or serial process, this is nil (process is running) or t
922 \(process is stopped). */)
924 register Lisp_Object process
;
926 CHECK_PROCESS (process
);
927 return XPROCESS (process
)->command
;
930 DEFUN ("process-tty-name", Fprocess_tty_name
, Sprocess_tty_name
, 1, 1, 0,
931 doc
: /* Return the name of the terminal PROCESS uses, or nil if none.
932 This is the terminal that the process itself reads and writes on,
933 not the name of the pty that Emacs uses to talk with that terminal. */)
935 register Lisp_Object process
;
937 CHECK_PROCESS (process
);
938 return XPROCESS (process
)->tty_name
;
941 DEFUN ("set-process-buffer", Fset_process_buffer
, Sset_process_buffer
,
943 doc
: /* Set buffer associated with PROCESS to BUFFER (a buffer, or nil). */)
945 register Lisp_Object process
, buffer
;
947 struct Lisp_Process
*p
;
949 CHECK_PROCESS (process
);
951 CHECK_BUFFER (buffer
);
952 p
= XPROCESS (process
);
954 if (NETCONN1_P (p
) || SERIALCONN1_P (p
))
955 p
->childp
= Fplist_put (p
->childp
, QCbuffer
, buffer
);
956 setup_process_coding_systems (process
);
960 DEFUN ("process-buffer", Fprocess_buffer
, Sprocess_buffer
,
962 doc
: /* Return the buffer PROCESS is associated with.
963 Output from PROCESS is inserted in this buffer unless PROCESS has a filter. */)
965 register Lisp_Object process
;
967 CHECK_PROCESS (process
);
968 return XPROCESS (process
)->buffer
;
971 DEFUN ("process-mark", Fprocess_mark
, Sprocess_mark
,
973 doc
: /* Return the marker for the end of the last output from PROCESS. */)
975 register Lisp_Object process
;
977 CHECK_PROCESS (process
);
978 return XPROCESS (process
)->mark
;
981 DEFUN ("set-process-filter", Fset_process_filter
, Sset_process_filter
,
983 doc
: /* Give PROCESS the filter function FILTER; nil means no filter.
984 A value of t means stop accepting output from the process.
986 When a process has a filter, its buffer is not used for output.
987 Instead, each time it does output, the entire string of output is
988 passed to the filter.
990 The filter gets two arguments: the process and the string of output.
991 The string argument is normally a multibyte string, except:
992 - if the process' input coding system is no-conversion or raw-text,
993 it is a unibyte string (the non-converted input), or else
994 - if `default-enable-multibyte-characters' is nil, it is a unibyte
995 string (the result of converting the decoded input multibyte
996 string to unibyte with `string-make-unibyte'). */)
998 register Lisp_Object process
, filter
;
1000 struct Lisp_Process
*p
;
1002 CHECK_PROCESS (process
);
1003 p
= XPROCESS (process
);
1005 /* Don't signal an error if the process' input file descriptor
1006 is closed. This could make debugging Lisp more difficult,
1007 for example when doing something like
1009 (setq process (start-process ...))
1011 (set-process-filter process ...) */
1015 if (EQ (filter
, Qt
) && !EQ (p
->status
, Qlisten
))
1017 FD_CLR (p
->infd
, &input_wait_mask
);
1018 FD_CLR (p
->infd
, &non_keyboard_wait_mask
);
1020 else if (EQ (p
->filter
, Qt
)
1021 /* Network or serial process not stopped: */
1022 && !EQ (p
->command
, Qt
))
1024 FD_SET (p
->infd
, &input_wait_mask
);
1025 FD_SET (p
->infd
, &non_keyboard_wait_mask
);
1030 if (NETCONN1_P (p
) || SERIALCONN1_P (p
))
1031 p
->childp
= Fplist_put (p
->childp
, QCfilter
, filter
);
1032 setup_process_coding_systems (process
);
1036 DEFUN ("process-filter", Fprocess_filter
, Sprocess_filter
,
1038 doc
: /* Returns the filter function of PROCESS; nil if none.
1039 See `set-process-filter' for more info on filter functions. */)
1041 register Lisp_Object process
;
1043 CHECK_PROCESS (process
);
1044 return XPROCESS (process
)->filter
;
1047 DEFUN ("set-process-sentinel", Fset_process_sentinel
, Sset_process_sentinel
,
1049 doc
: /* Give PROCESS the sentinel SENTINEL; nil for none.
1050 The sentinel is called as a function when the process changes state.
1051 It gets two arguments: the process, and a string describing the change. */)
1053 register Lisp_Object process
, sentinel
;
1055 struct Lisp_Process
*p
;
1057 CHECK_PROCESS (process
);
1058 p
= XPROCESS (process
);
1060 p
->sentinel
= sentinel
;
1061 if (NETCONN1_P (p
) || SERIALCONN1_P (p
))
1062 p
->childp
= Fplist_put (p
->childp
, QCsentinel
, sentinel
);
1066 DEFUN ("process-sentinel", Fprocess_sentinel
, Sprocess_sentinel
,
1068 doc
: /* Return the sentinel of PROCESS; nil if none.
1069 See `set-process-sentinel' for more info on sentinels. */)
1071 register Lisp_Object process
;
1073 CHECK_PROCESS (process
);
1074 return XPROCESS (process
)->sentinel
;
1077 DEFUN ("set-process-window-size", Fset_process_window_size
,
1078 Sset_process_window_size
, 3, 3, 0,
1079 doc
: /* Tell PROCESS that it has logical window size HEIGHT and WIDTH. */)
1080 (process
, height
, width
)
1081 register Lisp_Object process
, height
, width
;
1083 CHECK_PROCESS (process
);
1084 CHECK_NATNUM (height
);
1085 CHECK_NATNUM (width
);
1087 if (XPROCESS (process
)->infd
< 0
1088 || set_window_size (XPROCESS (process
)->infd
,
1089 XINT (height
), XINT (width
)) <= 0)
1095 DEFUN ("set-process-inherit-coding-system-flag",
1096 Fset_process_inherit_coding_system_flag
,
1097 Sset_process_inherit_coding_system_flag
, 2, 2, 0,
1098 doc
: /* Determine whether buffer of PROCESS will inherit coding-system.
1099 If the second argument FLAG is non-nil, then the variable
1100 `buffer-file-coding-system' of the buffer associated with PROCESS
1101 will be bound to the value of the coding system used to decode
1104 This is useful when the coding system specified for the process buffer
1105 leaves either the character code conversion or the end-of-line conversion
1106 unspecified, or if the coding system used to decode the process output
1107 is more appropriate for saving the process buffer.
1109 Binding the variable `inherit-process-coding-system' to non-nil before
1110 starting the process is an alternative way of setting the inherit flag
1111 for the process which will run. */)
1113 register Lisp_Object process
, flag
;
1115 CHECK_PROCESS (process
);
1116 XPROCESS (process
)->inherit_coding_system_flag
= !NILP (flag
);
1120 DEFUN ("process-inherit-coding-system-flag",
1121 Fprocess_inherit_coding_system_flag
, Sprocess_inherit_coding_system_flag
,
1123 doc
: /* Return the value of inherit-coding-system flag for PROCESS.
1124 If this flag is t, `buffer-file-coding-system' of the buffer
1125 associated with PROCESS will inherit the coding system used to decode
1126 the process output. */)
1128 register Lisp_Object process
;
1130 CHECK_PROCESS (process
);
1131 return XPROCESS (process
)->inherit_coding_system_flag
? Qt
: Qnil
;
1134 DEFUN ("set-process-query-on-exit-flag",
1135 Fset_process_query_on_exit_flag
, Sset_process_query_on_exit_flag
,
1137 doc
: /* Specify if query is needed for PROCESS when Emacs is exited.
1138 If the second argument FLAG is non-nil, Emacs will query the user before
1139 exiting or killing a buffer if PROCESS is running. */)
1141 register Lisp_Object process
, flag
;
1143 CHECK_PROCESS (process
);
1144 XPROCESS (process
)->kill_without_query
= NILP (flag
);
1148 DEFUN ("process-query-on-exit-flag",
1149 Fprocess_query_on_exit_flag
, Sprocess_query_on_exit_flag
,
1151 doc
: /* Return the current value of query-on-exit flag for PROCESS. */)
1153 register Lisp_Object process
;
1155 CHECK_PROCESS (process
);
1156 return (XPROCESS (process
)->kill_without_query
? Qnil
: Qt
);
1159 #ifdef DATAGRAM_SOCKETS
1160 Lisp_Object
Fprocess_datagram_address ();
1163 DEFUN ("process-contact", Fprocess_contact
, Sprocess_contact
,
1165 doc
: /* Return the contact info of PROCESS; t for a real child.
1166 For a network or serial connection, the value depends on the optional
1167 KEY arg. If KEY is nil, value is a cons cell of the form (HOST
1168 SERVICE) for a network connection or (PORT SPEED) for a serial
1169 connection. If KEY is t, the complete contact information for the
1170 connection is returned, else the specific value for the keyword KEY is
1171 returned. See `make-network-process' or `make-serial-process' for a
1172 list of keywords. */)
1174 register Lisp_Object process
, key
;
1176 Lisp_Object contact
;
1178 CHECK_PROCESS (process
);
1179 contact
= XPROCESS (process
)->childp
;
1181 #ifdef DATAGRAM_SOCKETS
1182 if (DATAGRAM_CONN_P (process
)
1183 && (EQ (key
, Qt
) || EQ (key
, QCremote
)))
1184 contact
= Fplist_put (contact
, QCremote
,
1185 Fprocess_datagram_address (process
));
1188 if ((!NETCONN_P (process
) && !SERIALCONN_P (process
)) || EQ (key
, Qt
))
1190 if (NILP (key
) && NETCONN_P (process
))
1191 return Fcons (Fplist_get (contact
, QChost
),
1192 Fcons (Fplist_get (contact
, QCservice
), Qnil
));
1193 if (NILP (key
) && SERIALCONN_P (process
))
1194 return Fcons (Fplist_get (contact
, QCport
),
1195 Fcons (Fplist_get (contact
, QCspeed
), Qnil
));
1196 return Fplist_get (contact
, key
);
1199 DEFUN ("process-plist", Fprocess_plist
, Sprocess_plist
,
1201 doc
: /* Return the plist of PROCESS. */)
1203 register Lisp_Object process
;
1205 CHECK_PROCESS (process
);
1206 return XPROCESS (process
)->plist
;
1209 DEFUN ("set-process-plist", Fset_process_plist
, Sset_process_plist
,
1211 doc
: /* Replace the plist of PROCESS with PLIST. Returns PLIST. */)
1213 register Lisp_Object process
, plist
;
1215 CHECK_PROCESS (process
);
1218 XPROCESS (process
)->plist
= plist
;
1222 #if 0 /* Turned off because we don't currently record this info
1223 in the process. Perhaps add it. */
1224 DEFUN ("process-connection", Fprocess_connection
, Sprocess_connection
, 1, 1, 0,
1225 doc
: /* Return the connection type of PROCESS.
1226 The value is nil for a pipe, t or `pty' for a pty, or `stream' for
1227 a socket connection. */)
1229 Lisp_Object process
;
1231 return XPROCESS (process
)->type
;
1235 DEFUN ("process-type", Fprocess_type
, Sprocess_type
, 1, 1, 0,
1236 doc
: /* Return the connection type of PROCESS.
1237 The value is either the symbol `real', `network', or `serial'.
1238 PROCESS may be a process, a buffer, the name of a process or buffer, or
1239 nil, indicating the current buffer's process. */)
1241 Lisp_Object process
;
1244 proc
= get_process (process
);
1245 return XPROCESS (proc
)->type
;
1249 DEFUN ("format-network-address", Fformat_network_address
, Sformat_network_address
,
1251 doc
: /* Convert network ADDRESS from internal format to a string.
1252 A 4 or 5 element vector represents an IPv4 address (with port number).
1253 An 8 or 9 element vector represents an IPv6 address (with port number).
1254 If optional second argument OMIT-PORT is non-nil, don't include a port
1255 number in the string, even when present in ADDRESS.
1256 Returns nil if format of ADDRESS is invalid. */)
1257 (address
, omit_port
)
1258 Lisp_Object address
, omit_port
;
1263 if (STRINGP (address
)) /* AF_LOCAL */
1266 if (VECTORP (address
)) /* AF_INET or AF_INET6 */
1268 register struct Lisp_Vector
*p
= XVECTOR (address
);
1269 Lisp_Object args
[10];
1272 if (p
->size
== 4 || (p
->size
== 5 && !NILP (omit_port
)))
1274 args
[0] = build_string ("%d.%d.%d.%d");
1277 else if (p
->size
== 5)
1279 args
[0] = build_string ("%d.%d.%d.%d:%d");
1282 else if (p
->size
== 8 || (p
->size
== 9 && !NILP (omit_port
)))
1284 args
[0] = build_string ("%x:%x:%x:%x:%x:%x:%x:%x");
1287 else if (p
->size
== 9)
1289 args
[0] = build_string ("[%x:%x:%x:%x:%x:%x:%x:%x]:%d");
1295 for (i
= 0; i
< nargs
; i
++)
1297 EMACS_INT element
= XINT (p
->contents
[i
]);
1299 if (element
< 0 || element
> 65535)
1302 if (nargs
<= 5 /* IPv4 */
1303 && i
< 4 /* host, not port */
1307 args
[i
+1] = p
->contents
[i
];
1310 return Fformat (nargs
+1, args
);
1313 if (CONSP (address
))
1315 Lisp_Object args
[2];
1316 args
[0] = build_string ("<Family %d>");
1317 args
[1] = Fcar (address
);
1318 return Fformat (2, args
);
1326 list_processes_1 (query_only
)
1327 Lisp_Object query_only
;
1329 register Lisp_Object tail
, tem
;
1330 Lisp_Object proc
, minspace
, tem1
;
1331 register struct Lisp_Process
*p
;
1333 int w_proc
, w_buffer
, w_tty
;
1335 Lisp_Object i_status
, i_buffer
, i_tty
, i_command
;
1337 w_proc
= 4; /* Proc */
1338 w_buffer
= 6; /* Buffer */
1339 w_tty
= 0; /* Omit if no ttys */
1341 for (tail
= Vprocess_alist
; CONSP (tail
); tail
= XCDR (tail
))
1345 proc
= Fcdr (XCAR (tail
));
1346 p
= XPROCESS (proc
);
1349 if (!NILP (query_only
) && p
->kill_without_query
)
1351 if (STRINGP (p
->name
)
1352 && ( i
= SCHARS (p
->name
), (i
> w_proc
)))
1354 if (!NILP (p
->buffer
))
1356 if (NILP (XBUFFER (p
->buffer
)->name
))
1359 w_buffer
= 8; /* (Killed) */
1361 else if ((i
= SCHARS (XBUFFER (p
->buffer
)->name
), (i
> w_buffer
)))
1364 if (STRINGP (p
->tty_name
)
1365 && (i
= SCHARS (p
->tty_name
), (i
> w_tty
)))
1369 XSETFASTINT (i_status
, w_proc
+ 1);
1370 XSETFASTINT (i_buffer
, XFASTINT (i_status
) + 9);
1373 XSETFASTINT (i_tty
, XFASTINT (i_buffer
) + w_buffer
+ 1);
1374 XSETFASTINT (i_command
, XFASTINT (i_tty
) + w_tty
+ 1);
1379 XSETFASTINT (i_command
, XFASTINT (i_buffer
) + w_buffer
+ 1);
1382 XSETFASTINT (minspace
, 1);
1384 set_buffer_internal (XBUFFER (Vstandard_output
));
1385 current_buffer
->undo_list
= Qt
;
1387 current_buffer
->truncate_lines
= Qt
;
1389 write_string ("Proc", -1);
1390 Findent_to (i_status
, minspace
); write_string ("Status", -1);
1391 Findent_to (i_buffer
, minspace
); write_string ("Buffer", -1);
1394 Findent_to (i_tty
, minspace
); write_string ("Tty", -1);
1396 Findent_to (i_command
, minspace
); write_string ("Command", -1);
1397 write_string ("\n", -1);
1399 write_string ("----", -1);
1400 Findent_to (i_status
, minspace
); write_string ("------", -1);
1401 Findent_to (i_buffer
, minspace
); write_string ("------", -1);
1404 Findent_to (i_tty
, minspace
); write_string ("---", -1);
1406 Findent_to (i_command
, minspace
); write_string ("-------", -1);
1407 write_string ("\n", -1);
1409 for (tail
= Vprocess_alist
; CONSP (tail
); tail
= XCDR (tail
))
1413 proc
= Fcdr (XCAR (tail
));
1414 p
= XPROCESS (proc
);
1417 if (!NILP (query_only
) && p
->kill_without_query
)
1420 Finsert (1, &p
->name
);
1421 Findent_to (i_status
, minspace
);
1423 if (p
->raw_status_new
)
1426 if (CONSP (p
->status
))
1427 symbol
= XCAR (p
->status
);
1429 if (EQ (symbol
, Qsignal
))
1432 tem
= Fcar (Fcdr (p
->status
));
1433 Fprinc (symbol
, Qnil
);
1435 else if (NETCONN1_P (p
) || SERIALCONN1_P (p
))
1437 if (EQ (symbol
, Qexit
))
1438 write_string ("closed", -1);
1439 else if (EQ (p
->command
, Qt
))
1440 write_string ("stopped", -1);
1441 else if (EQ (symbol
, Qrun
))
1442 write_string ("open", -1);
1444 Fprinc (symbol
, Qnil
);
1446 else if (SERIALCONN1_P (p
))
1448 write_string ("running", -1);
1451 Fprinc (symbol
, Qnil
);
1453 if (EQ (symbol
, Qexit
))
1456 tem
= Fcar (Fcdr (p
->status
));
1459 sprintf (tembuf
, " %d", (int) XFASTINT (tem
));
1460 write_string (tembuf
, -1);
1464 if (EQ (symbol
, Qsignal
) || EQ (symbol
, Qexit
) || EQ (symbol
, Qclosed
))
1467 Findent_to (i_buffer
, minspace
);
1468 if (NILP (p
->buffer
))
1469 insert_string ("(none)");
1470 else if (NILP (XBUFFER (p
->buffer
)->name
))
1471 insert_string ("(Killed)");
1473 Finsert (1, &XBUFFER (p
->buffer
)->name
);
1477 Findent_to (i_tty
, minspace
);
1478 if (STRINGP (p
->tty_name
))
1479 Finsert (1, &p
->tty_name
);
1482 Findent_to (i_command
, minspace
);
1484 if (EQ (p
->status
, Qlisten
))
1486 Lisp_Object port
= Fplist_get (p
->childp
, QCservice
);
1487 if (INTEGERP (port
))
1488 port
= Fnumber_to_string (port
);
1490 port
= Fformat_network_address (Fplist_get (p
->childp
, QClocal
), Qnil
);
1491 sprintf (tembuf
, "(network %s server on %s)\n",
1492 (DATAGRAM_CHAN_P (p
->infd
) ? "datagram" : "stream"),
1493 (STRINGP (port
) ? (char *)SDATA (port
) : "?"));
1494 insert_string (tembuf
);
1496 else if (NETCONN1_P (p
))
1498 /* For a local socket, there is no host name,
1499 so display service instead. */
1500 Lisp_Object host
= Fplist_get (p
->childp
, QChost
);
1501 if (!STRINGP (host
))
1503 host
= Fplist_get (p
->childp
, QCservice
);
1504 if (INTEGERP (host
))
1505 host
= Fnumber_to_string (host
);
1508 host
= Fformat_network_address (Fplist_get (p
->childp
, QCremote
), Qnil
);
1509 sprintf (tembuf
, "(network %s connection to %s)\n",
1510 (DATAGRAM_CHAN_P (p
->infd
) ? "datagram" : "stream"),
1511 (STRINGP (host
) ? (char *)SDATA (host
) : "?"));
1512 insert_string (tembuf
);
1514 else if (SERIALCONN1_P (p
))
1516 Lisp_Object port
= Fplist_get (p
->childp
, QCport
);
1517 Lisp_Object speed
= Fplist_get (p
->childp
, QCspeed
);
1518 insert_string ("(serial port ");
1520 insert_string (SDATA (port
));
1522 insert_string ("?");
1523 if (INTEGERP (speed
))
1525 sprintf (tembuf
, " at %d b/s", XINT (speed
));
1526 insert_string (tembuf
);
1528 insert_string (")\n");
1542 insert_string (" ");
1544 insert_string ("\n");
1549 status_notify (NULL
);
1550 redisplay_preserve_echo_area (13);
1555 DEFUN ("list-processes", Flist_processes
, Slist_processes
, 0, 1, "P",
1556 doc
: /* Display a list of all processes.
1557 If optional argument QUERY-ONLY is non-nil, only processes with
1558 the query-on-exit flag set will be listed.
1559 Any process listed as exited or signaled is actually eliminated
1560 after the listing is made. */)
1562 Lisp_Object query_only
;
1564 internal_with_output_to_temp_buffer ("*Process List*",
1565 list_processes_1
, query_only
);
1569 DEFUN ("process-list", Fprocess_list
, Sprocess_list
, 0, 0, 0,
1570 doc
: /* Return a list of all processes. */)
1573 return Fmapcar (Qcdr
, Vprocess_alist
);
1576 /* Starting asynchronous inferior processes. */
1578 static Lisp_Object
start_process_unwind ();
1580 DEFUN ("start-process", Fstart_process
, Sstart_process
, 3, MANY
, 0,
1581 doc
: /* Start a program in a subprocess. Return the process object for it.
1582 NAME is name for process. It is modified if necessary to make it unique.
1583 BUFFER is the buffer (or buffer name) to associate with the process.
1585 Process output (both standard output and standard error streams) goes
1586 at end of BUFFER, unless you specify an output stream or filter
1587 function to handle the output. BUFFER may also be nil, meaning that
1588 this process is not associated with any buffer.
1590 PROGRAM is the program file name. It is searched for in PATH. If
1591 nil, just associate a pty with the buffer. Remaining arguments are
1592 strings to give program as arguments.
1594 If you want to separate standard output from standard error, invoke
1595 the command through a shell and redirect one of them using the shell
1598 usage: (start-process NAME BUFFER PROGRAM &rest PROGRAM-ARGS) */)
1601 register Lisp_Object
*args
;
1603 Lisp_Object buffer
, name
, program
, proc
, current_dir
, tem
;
1604 register unsigned char **new_argv
;
1606 int count
= SPECPDL_INDEX ();
1610 buffer
= Fget_buffer_create (buffer
);
1612 /* Make sure that the child will be able to chdir to the current
1613 buffer's current directory, or its unhandled equivalent. We
1614 can't just have the child check for an error when it does the
1615 chdir, since it's in a vfork.
1617 We have to GCPRO around this because Fexpand_file_name and
1618 Funhandled_file_name_directory might call a file name handling
1619 function. The argument list is protected by the caller, so all
1620 we really have to worry about is buffer. */
1622 struct gcpro gcpro1
, gcpro2
;
1624 current_dir
= current_buffer
->directory
;
1626 GCPRO2 (buffer
, current_dir
);
1628 current_dir
= Funhandled_file_name_directory (current_dir
);
1629 if (NILP (current_dir
))
1630 /* If the file name handler says that current_dir is unreachable, use
1631 a sensible default. */
1632 current_dir
= build_string ("~/");
1633 current_dir
= expand_and_dir_to_file (current_dir
, Qnil
);
1634 if (NILP (Ffile_accessible_directory_p (current_dir
)))
1635 report_file_error ("Setting current directory",
1636 Fcons (current_buffer
->directory
, Qnil
));
1642 CHECK_STRING (name
);
1646 if (!NILP (program
))
1647 CHECK_STRING (program
);
1649 proc
= make_process (name
);
1650 /* If an error occurs and we can't start the process, we want to
1651 remove it from the process list. This means that each error
1652 check in create_process doesn't need to call remove_process
1653 itself; it's all taken care of here. */
1654 record_unwind_protect (start_process_unwind
, proc
);
1656 XPROCESS (proc
)->childp
= Qt
;
1657 XPROCESS (proc
)->plist
= Qnil
;
1658 XPROCESS (proc
)->type
= Qreal
;
1659 XPROCESS (proc
)->buffer
= buffer
;
1660 XPROCESS (proc
)->sentinel
= Qnil
;
1661 XPROCESS (proc
)->filter
= Qnil
;
1662 XPROCESS (proc
)->command
= Flist (nargs
- 2, args
+ 2);
1664 #ifdef ADAPTIVE_READ_BUFFERING
1665 XPROCESS (proc
)->adaptive_read_buffering
1666 = (NILP (Vprocess_adaptive_read_buffering
) ? 0
1667 : EQ (Vprocess_adaptive_read_buffering
, Qt
) ? 1 : 2);
1670 /* Make the process marker point into the process buffer (if any). */
1671 if (BUFFERP (buffer
))
1672 set_marker_both (XPROCESS (proc
)->mark
, buffer
,
1673 BUF_ZV (XBUFFER (buffer
)),
1674 BUF_ZV_BYTE (XBUFFER (buffer
)));
1677 /* Decide coding systems for communicating with the process. Here
1678 we don't setup the structure coding_system nor pay attention to
1679 unibyte mode. They are done in create_process. */
1681 /* Qt denotes we have not yet called Ffind_operation_coding_system. */
1682 Lisp_Object coding_systems
= Qt
;
1683 Lisp_Object val
, *args2
;
1684 struct gcpro gcpro1
, gcpro2
;
1686 val
= Vcoding_system_for_read
;
1689 args2
= (Lisp_Object
*) alloca ((nargs
+ 1) * sizeof *args2
);
1690 args2
[0] = Qstart_process
;
1691 for (i
= 0; i
< nargs
; i
++) args2
[i
+ 1] = args
[i
];
1692 GCPRO2 (proc
, current_dir
);
1693 if (!NILP (program
))
1694 coding_systems
= Ffind_operation_coding_system (nargs
+ 1, args2
);
1696 if (CONSP (coding_systems
))
1697 val
= XCAR (coding_systems
);
1698 else if (CONSP (Vdefault_process_coding_system
))
1699 val
= XCAR (Vdefault_process_coding_system
);
1701 XPROCESS (proc
)->decode_coding_system
= val
;
1703 val
= Vcoding_system_for_write
;
1706 if (EQ (coding_systems
, Qt
))
1708 args2
= (Lisp_Object
*) alloca ((nargs
+ 1) * sizeof args2
);
1709 args2
[0] = Qstart_process
;
1710 for (i
= 0; i
< nargs
; i
++) args2
[i
+ 1] = args
[i
];
1711 GCPRO2 (proc
, current_dir
);
1712 if (!NILP (program
))
1713 coding_systems
= Ffind_operation_coding_system (nargs
+ 1, args2
);
1716 if (CONSP (coding_systems
))
1717 val
= XCDR (coding_systems
);
1718 else if (CONSP (Vdefault_process_coding_system
))
1719 val
= XCDR (Vdefault_process_coding_system
);
1721 XPROCESS (proc
)->encode_coding_system
= val
;
1725 XPROCESS (proc
)->decoding_buf
= make_uninit_string (0);
1726 XPROCESS (proc
)->decoding_carryover
= 0;
1727 XPROCESS (proc
)->encoding_buf
= make_uninit_string (0);
1729 XPROCESS (proc
)->inherit_coding_system_flag
1730 = !(NILP (buffer
) || !inherit_process_coding_system
);
1732 if (!NILP (program
))
1734 /* If program file name is not absolute, search our path for it.
1735 Put the name we will really use in TEM. */
1736 if (!IS_DIRECTORY_SEP (SREF (program
, 0))
1737 && !(SCHARS (program
) > 1
1738 && IS_DEVICE_SEP (SREF (program
, 1))))
1740 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
;
1743 GCPRO4 (name
, program
, buffer
, current_dir
);
1744 openp (Vexec_path
, program
, Vexec_suffixes
, &tem
, make_number (X_OK
));
1747 report_file_error ("Searching for program", Fcons (program
, Qnil
));
1748 tem
= Fexpand_file_name (tem
, Qnil
);
1752 if (!NILP (Ffile_directory_p (program
)))
1753 error ("Specified program for new process is a directory");
1757 /* If program file name starts with /: for quoting a magic name,
1759 if (SBYTES (tem
) > 2 && SREF (tem
, 0) == '/'
1760 && SREF (tem
, 1) == ':')
1761 tem
= Fsubstring (tem
, make_number (2), Qnil
);
1764 struct gcpro gcpro1
;
1767 /* Encode the file name and put it in NEW_ARGV.
1768 That's where the child will use it to execute the program. */
1769 tem
= Fcons (ENCODE_FILE (tem
), Qnil
);
1771 /* Here we encode arguments by the coding system used for sending
1772 data to the process. We don't support using different coding
1773 systems for encoding arguments and for encoding data sent to the
1776 for (i
= 3; i
< nargs
; i
++)
1778 tem
= Fcons (args
[i
], tem
);
1779 CHECK_STRING (XCAR (tem
));
1780 if (STRING_MULTIBYTE (XCAR (tem
)))
1782 code_convert_string_norecord
1783 (XCAR (tem
), XPROCESS (proc
)->encode_coding_system
, 1));
1789 /* Now that everything is encoded we can collect the strings into
1791 new_argv
= (unsigned char **) alloca ((nargs
- 1) * sizeof (char *));
1792 new_argv
[nargs
- 2] = 0;
1794 for (i
= nargs
- 3; i
>= 0; i
--)
1796 new_argv
[i
] = SDATA (XCAR (tem
));
1800 create_process (proc
, (char **) new_argv
, current_dir
);
1805 return unbind_to (count
, proc
);
1808 /* This function is the unwind_protect form for Fstart_process. If
1809 PROC doesn't have its pid set, then we know someone has signaled
1810 an error and the process wasn't started successfully, so we should
1811 remove it from the process list. */
1813 start_process_unwind (proc
)
1816 if (!PROCESSP (proc
))
1819 /* Was PROC started successfully? */
1820 if (XPROCESS (proc
)->pid
== -1)
1821 remove_process (proc
);
1827 create_process_1 (timer
)
1828 struct atimer
*timer
;
1830 /* Nothing to do. */
1834 #if 0 /* This doesn't work; see the note before sigchld_handler. */
1837 /* Mimic blocking of signals on system V, which doesn't really have it. */
1839 /* Nonzero means we got a SIGCHLD when it was supposed to be blocked. */
1840 int sigchld_deferred
;
1843 create_process_sigchld ()
1845 signal (SIGCHLD
, create_process_sigchld
);
1847 sigchld_deferred
= 1;
1854 create_process (process
, new_argv
, current_dir
)
1855 Lisp_Object process
;
1857 Lisp_Object current_dir
;
1859 int inchannel
, outchannel
;
1862 #if !defined (WINDOWSNT) && defined (FD_CLOEXEC)
1863 int wait_child_setup
[2];
1865 #ifdef POSIX_SIGNALS
1868 struct sigaction sigint_action
;
1869 struct sigaction sigquit_action
;
1871 struct sigaction sighup_action
;
1873 #else /* !POSIX_SIGNALS */
1876 SIGTYPE (*sigchld
)();
1879 #endif /* !POSIX_SIGNALS */
1880 /* Use volatile to protect variables from being clobbered by longjmp. */
1881 volatile int forkin
, forkout
;
1882 volatile int pty_flag
= 0;
1884 extern char **environ
;
1887 inchannel
= outchannel
= -1;
1890 if (!NILP (Vprocess_connection_type
))
1891 outchannel
= inchannel
= allocate_pty ();
1895 #if ! defined (USG) || defined (USG_SUBTTY_WORKS)
1896 /* On most USG systems it does not work to open the pty's tty here,
1897 then close it and reopen it in the child. */
1899 /* Don't let this terminal become our controlling terminal
1900 (in case we don't have one). */
1901 forkout
= forkin
= emacs_open (pty_name
, O_RDWR
| O_NOCTTY
, 0);
1903 forkout
= forkin
= emacs_open (pty_name
, O_RDWR
, 0);
1906 report_file_error ("Opening pty", Qnil
);
1908 forkin
= forkout
= -1;
1909 #endif /* not USG, or USG_SUBTTY_WORKS */
1913 #endif /* HAVE_PTYS */
1918 report_file_error ("Creating pipe", Qnil
);
1924 emacs_close (inchannel
);
1925 emacs_close (forkout
);
1926 report_file_error ("Creating pipe", Qnil
);
1932 #if !defined (WINDOWSNT) && defined (FD_CLOEXEC)
1936 tem
= pipe (wait_child_setup
);
1938 report_file_error ("Creating pipe", Qnil
);
1939 tem
= fcntl (wait_child_setup
[1], F_GETFD
, 0);
1941 tem
= fcntl (wait_child_setup
[1], F_SETFD
, tem
| FD_CLOEXEC
);
1944 emacs_close (wait_child_setup
[0]);
1945 emacs_close (wait_child_setup
[1]);
1946 report_file_error ("Setting file descriptor flags", Qnil
);
1952 /* Replaced by close_process_descs */
1953 set_exclusive_use (inchannel
);
1954 set_exclusive_use (outchannel
);
1958 fcntl (inchannel
, F_SETFL
, O_NONBLOCK
);
1959 fcntl (outchannel
, F_SETFL
, O_NONBLOCK
);
1962 fcntl (inchannel
, F_SETFL
, O_NDELAY
);
1963 fcntl (outchannel
, F_SETFL
, O_NDELAY
);
1967 /* Record this as an active process, with its channels.
1968 As a result, child_setup will close Emacs's side of the pipes. */
1969 chan_process
[inchannel
] = process
;
1970 XPROCESS (process
)->infd
= inchannel
;
1971 XPROCESS (process
)->outfd
= outchannel
;
1973 /* Previously we recorded the tty descriptor used in the subprocess.
1974 It was only used for getting the foreground tty process, so now
1975 we just reopen the device (see emacs_get_tty_pgrp) as this is
1976 more portable (see USG_SUBTTY_WORKS above). */
1978 XPROCESS (process
)->pty_flag
= pty_flag
;
1979 XPROCESS (process
)->status
= Qrun
;
1980 setup_process_coding_systems (process
);
1982 /* Delay interrupts until we have a chance to store
1983 the new fork's pid in its process structure */
1984 #ifdef POSIX_SIGNALS
1985 sigemptyset (&blocked
);
1987 sigaddset (&blocked
, SIGCHLD
);
1989 #ifdef HAVE_WORKING_VFORK
1990 /* On many hosts (e.g. Solaris 2.4), if a vforked child calls `signal',
1991 this sets the parent's signal handlers as well as the child's.
1992 So delay all interrupts whose handlers the child might munge,
1993 and record the current handlers so they can be restored later. */
1994 sigaddset (&blocked
, SIGINT
); sigaction (SIGINT
, 0, &sigint_action
);
1995 sigaddset (&blocked
, SIGQUIT
); sigaction (SIGQUIT
, 0, &sigquit_action
);
1997 sigaddset (&blocked
, SIGHUP
); sigaction (SIGHUP
, 0, &sighup_action
);
1999 #endif /* HAVE_WORKING_VFORK */
2000 sigprocmask (SIG_BLOCK
, &blocked
, &procmask
);
2001 #else /* !POSIX_SIGNALS */
2003 #if defined (BSD_SYSTEM) || defined (HPUX)
2004 sigsetmask (sigmask (SIGCHLD
));
2005 #else /* ordinary USG */
2007 sigchld_deferred
= 0;
2008 sigchld
= signal (SIGCHLD
, create_process_sigchld
);
2010 #endif /* ordinary USG */
2011 #endif /* SIGCHLD */
2012 #endif /* !POSIX_SIGNALS */
2014 FD_SET (inchannel
, &input_wait_mask
);
2015 FD_SET (inchannel
, &non_keyboard_wait_mask
);
2016 if (inchannel
> max_process_desc
)
2017 max_process_desc
= inchannel
;
2019 /* Until we store the proper pid, enable sigchld_handler
2020 to recognize an unknown pid as standing for this process.
2021 It is very important not to let this `marker' value stay
2022 in the table after this function has returned; if it does
2023 it might cause call-process to hang and subsequent asynchronous
2024 processes to get their return values scrambled. */
2025 XPROCESS (process
)->pid
= -1;
2030 /* child_setup must clobber environ on systems with true vfork.
2031 Protect it from permanent change. */
2032 char **save_environ
= environ
;
2034 current_dir
= ENCODE_FILE (current_dir
);
2039 #endif /* not WINDOWSNT */
2041 int xforkin
= forkin
;
2042 int xforkout
= forkout
;
2044 #if 0 /* This was probably a mistake--it duplicates code later on,
2045 but fails to handle all the cases. */
2046 /* Make sure SIGCHLD is not blocked in the child. */
2047 sigsetmask (SIGEMPTYMASK
);
2050 /* Make the pty be the controlling terminal of the process. */
2052 /* First, disconnect its current controlling terminal. */
2054 /* We tried doing setsid only if pty_flag, but it caused
2055 process_set_signal to fail on SGI when using a pipe. */
2057 /* Make the pty's terminal the controlling terminal. */
2061 /* We ignore the return value
2062 because faith@cs.unc.edu says that is necessary on Linux. */
2063 ioctl (xforkin
, TIOCSCTTY
, 0);
2066 #else /* not HAVE_SETSID */
2068 /* It's very important to call setpgrp here and no time
2069 afterwards. Otherwise, we lose our controlling tty which
2070 is set when we open the pty. */
2073 #endif /* not HAVE_SETSID */
2074 #if defined (HAVE_TERMIOS) && defined (LDISC1)
2075 if (pty_flag
&& xforkin
>= 0)
2078 tcgetattr (xforkin
, &t
);
2080 if (tcsetattr (xforkin
, TCSANOW
, &t
) < 0)
2081 emacs_write (1, "create_process/tcsetattr LDISC1 failed\n", 39);
2084 #if defined (NTTYDISC) && defined (TIOCSETD)
2085 if (pty_flag
&& xforkin
>= 0)
2087 /* Use new line discipline. */
2088 int ldisc
= NTTYDISC
;
2089 ioctl (xforkin
, TIOCSETD
, &ldisc
);
2094 /* In 4.3BSD, the TIOCSPGRP bug has been fixed, and now you
2095 can do TIOCSPGRP only to the process's controlling tty. */
2098 /* I wonder: would just ioctl (0, TIOCNOTTY, 0) work here?
2099 I can't test it since I don't have 4.3. */
2100 int j
= emacs_open ("/dev/tty", O_RDWR
, 0);
2101 ioctl (j
, TIOCNOTTY
, 0);
2104 /* In order to get a controlling terminal on some versions
2105 of BSD, it is necessary to put the process in pgrp 0
2106 before it opens the terminal. */
2114 #endif /* TIOCNOTTY */
2116 #if !defined (DONT_REOPEN_PTY)
2117 /*** There is a suggestion that this ought to be a
2118 conditional on TIOCSPGRP,
2119 or !(defined (HAVE_SETSID) && defined (TIOCSCTTY)).
2120 Trying the latter gave the wrong results on Debian GNU/Linux 1.1;
2121 that system does seem to need this code, even though
2122 both HAVE_SETSID and TIOCSCTTY are defined. */
2123 /* Now close the pty (if we had it open) and reopen it.
2124 This makes the pty the controlling terminal of the subprocess. */
2128 /* I wonder if emacs_close (emacs_open (pty_name, ...))
2131 emacs_close (xforkin
);
2132 xforkout
= xforkin
= emacs_open (pty_name
, O_RDWR
, 0);
2136 emacs_write (1, "Couldn't open the pty terminal ", 31);
2137 emacs_write (1, pty_name
, strlen (pty_name
));
2138 emacs_write (1, "\n", 1);
2143 #endif /* not DONT_REOPEN_PTY */
2145 #ifdef SETUP_SLAVE_PTY
2150 #endif /* SETUP_SLAVE_PTY */
2152 /* On AIX, we've disabled SIGHUP above once we start a child on a pty.
2153 Now reenable it in the child, so it will die when we want it to. */
2155 signal (SIGHUP
, SIG_DFL
);
2157 #endif /* HAVE_PTYS */
2159 signal (SIGINT
, SIG_DFL
);
2160 signal (SIGQUIT
, SIG_DFL
);
2162 /* Stop blocking signals in the child. */
2163 #ifdef POSIX_SIGNALS
2164 sigprocmask (SIG_SETMASK
, &procmask
, 0);
2165 #else /* !POSIX_SIGNALS */
2167 #if defined (BSD_SYSTEM) || defined (HPUX)
2168 sigsetmask (SIGEMPTYMASK
);
2169 #else /* ordinary USG */
2171 signal (SIGCHLD
, sigchld
);
2173 #endif /* ordinary USG */
2174 #endif /* SIGCHLD */
2175 #endif /* !POSIX_SIGNALS */
2178 child_setup_tty (xforkout
);
2180 pid
= child_setup (xforkin
, xforkout
, xforkout
,
2181 new_argv
, 1, current_dir
);
2182 #else /* not WINDOWSNT */
2184 emacs_close (wait_child_setup
[0]);
2186 child_setup (xforkin
, xforkout
, xforkout
,
2187 new_argv
, 1, current_dir
);
2188 #endif /* not WINDOWSNT */
2190 environ
= save_environ
;
2195 /* This runs in the Emacs process. */
2199 emacs_close (forkin
);
2200 if (forkin
!= forkout
&& forkout
>= 0)
2201 emacs_close (forkout
);
2205 /* vfork succeeded. */
2206 XPROCESS (process
)->pid
= pid
;
2209 register_child (pid
, inchannel
);
2210 #endif /* WINDOWSNT */
2212 /* If the subfork execv fails, and it exits,
2213 this close hangs. I don't know why.
2214 So have an interrupt jar it loose. */
2216 struct atimer
*timer
;
2220 EMACS_SET_SECS_USECS (offset
, 1, 0);
2221 timer
= start_atimer (ATIMER_RELATIVE
, offset
, create_process_1
, 0);
2224 emacs_close (forkin
);
2226 cancel_atimer (timer
);
2230 if (forkin
!= forkout
&& forkout
>= 0)
2231 emacs_close (forkout
);
2235 XPROCESS (process
)->tty_name
= build_string (pty_name
);
2238 XPROCESS (process
)->tty_name
= Qnil
;
2240 #if !defined (WINDOWSNT) && defined (FD_CLOEXEC)
2241 /* Wait for child_setup to complete in case that vfork is
2242 actually defined as fork. The descriptor wait_child_setup[1]
2243 of a pipe is closed at the child side either by close-on-exec
2244 on successful execvp or the _exit call in child_setup. */
2248 emacs_close (wait_child_setup
[1]);
2249 emacs_read (wait_child_setup
[0], &dummy
, 1);
2250 emacs_close (wait_child_setup
[0]);
2255 /* Restore the signal state whether vfork succeeded or not.
2256 (We will signal an error, below, if it failed.) */
2257 #ifdef POSIX_SIGNALS
2258 #ifdef HAVE_WORKING_VFORK
2259 /* Restore the parent's signal handlers. */
2260 sigaction (SIGINT
, &sigint_action
, 0);
2261 sigaction (SIGQUIT
, &sigquit_action
, 0);
2263 sigaction (SIGHUP
, &sighup_action
, 0);
2265 #endif /* HAVE_WORKING_VFORK */
2266 /* Stop blocking signals in the parent. */
2267 sigprocmask (SIG_SETMASK
, &procmask
, 0);
2268 #else /* !POSIX_SIGNALS */
2270 #if defined (BSD_SYSTEM) || defined (HPUX)
2271 sigsetmask (SIGEMPTYMASK
);
2272 #else /* ordinary USG */
2274 signal (SIGCHLD
, sigchld
);
2275 /* Now really handle any of these signals
2276 that came in during this function. */
2277 if (sigchld_deferred
)
2278 kill (getpid (), SIGCHLD
);
2280 #endif /* ordinary USG */
2281 #endif /* SIGCHLD */
2282 #endif /* !POSIX_SIGNALS */
2284 /* Now generate the error if vfork failed. */
2286 report_file_error ("Doing vfork", Qnil
);
2290 create_pty (process
)
2291 Lisp_Object process
;
2293 int inchannel
, outchannel
;
2295 /* Use volatile to protect variables from being clobbered by longjmp. */
2296 volatile int forkin
, forkout
;
2297 volatile int pty_flag
= 0;
2299 inchannel
= outchannel
= -1;
2302 if (!NILP (Vprocess_connection_type
))
2303 outchannel
= inchannel
= allocate_pty ();
2307 #if ! defined (USG) || defined (USG_SUBTTY_WORKS)
2308 /* On most USG systems it does not work to open the pty's tty here,
2309 then close it and reopen it in the child. */
2311 /* Don't let this terminal become our controlling terminal
2312 (in case we don't have one). */
2313 forkout
= forkin
= emacs_open (pty_name
, O_RDWR
| O_NOCTTY
, 0);
2315 forkout
= forkin
= emacs_open (pty_name
, O_RDWR
, 0);
2318 report_file_error ("Opening pty", Qnil
);
2319 #if defined (RTU) || defined (UNIPLUS) || defined (DONT_REOPEN_PTY)
2320 /* In the case that vfork is defined as fork, the parent process
2321 (Emacs) may send some data before the child process completes
2322 tty options setup. So we setup tty before forking. */
2323 child_setup_tty (forkout
);
2324 #endif /* RTU or UNIPLUS or DONT_REOPEN_PTY */
2326 forkin
= forkout
= -1;
2327 #endif /* not USG, or USG_SUBTTY_WORKS */
2330 #endif /* HAVE_PTYS */
2333 fcntl (inchannel
, F_SETFL
, O_NONBLOCK
);
2334 fcntl (outchannel
, F_SETFL
, O_NONBLOCK
);
2337 fcntl (inchannel
, F_SETFL
, O_NDELAY
);
2338 fcntl (outchannel
, F_SETFL
, O_NDELAY
);
2342 /* Record this as an active process, with its channels.
2343 As a result, child_setup will close Emacs's side of the pipes. */
2344 chan_process
[inchannel
] = process
;
2345 XPROCESS (process
)->infd
= inchannel
;
2346 XPROCESS (process
)->outfd
= outchannel
;
2348 /* Previously we recorded the tty descriptor used in the subprocess.
2349 It was only used for getting the foreground tty process, so now
2350 we just reopen the device (see emacs_get_tty_pgrp) as this is
2351 more portable (see USG_SUBTTY_WORKS above). */
2353 XPROCESS (process
)->pty_flag
= pty_flag
;
2354 XPROCESS (process
)->status
= Qrun
;
2355 setup_process_coding_systems (process
);
2357 FD_SET (inchannel
, &input_wait_mask
);
2358 FD_SET (inchannel
, &non_keyboard_wait_mask
);
2359 if (inchannel
> max_process_desc
)
2360 max_process_desc
= inchannel
;
2362 XPROCESS (process
)->pid
= -2;
2365 XPROCESS (process
)->tty_name
= build_string (pty_name
);
2368 XPROCESS (process
)->tty_name
= Qnil
;
2374 /* Convert an internal struct sockaddr to a lisp object (vector or string).
2375 The address family of sa is not included in the result. */
2378 conv_sockaddr_to_lisp (sa
, len
)
2379 struct sockaddr
*sa
;
2382 Lisp_Object address
;
2385 register struct Lisp_Vector
*p
;
2387 /* Workaround for a bug in getsockname on BSD: Names bound to
2388 sockets in the UNIX domain are inaccessible; getsockname returns
2389 a zero length name. */
2390 if (len
< OFFSETOF (struct sockaddr
, sa_family
) + sizeof (sa
->sa_family
))
2391 return empty_unibyte_string
;
2393 switch (sa
->sa_family
)
2397 struct sockaddr_in
*sin
= (struct sockaddr_in
*) sa
;
2398 len
= sizeof (sin
->sin_addr
) + 1;
2399 address
= Fmake_vector (make_number (len
), Qnil
);
2400 p
= XVECTOR (address
);
2401 p
->contents
[--len
] = make_number (ntohs (sin
->sin_port
));
2402 cp
= (unsigned char *) &sin
->sin_addr
;
2408 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*) sa
;
2409 uint16_t *ip6
= (uint16_t *) &sin6
->sin6_addr
;
2410 len
= sizeof (sin6
->sin6_addr
)/2 + 1;
2411 address
= Fmake_vector (make_number (len
), Qnil
);
2412 p
= XVECTOR (address
);
2413 p
->contents
[--len
] = make_number (ntohs (sin6
->sin6_port
));
2414 for (i
= 0; i
< len
; i
++)
2415 p
->contents
[i
] = make_number (ntohs (ip6
[i
]));
2419 #ifdef HAVE_LOCAL_SOCKETS
2422 struct sockaddr_un
*sockun
= (struct sockaddr_un
*) sa
;
2423 for (i
= 0; i
< sizeof (sockun
->sun_path
); i
++)
2424 if (sockun
->sun_path
[i
] == 0)
2426 return make_unibyte_string (sockun
->sun_path
, i
);
2430 len
-= OFFSETOF (struct sockaddr
, sa_family
) + sizeof (sa
->sa_family
);
2431 address
= Fcons (make_number (sa
->sa_family
),
2432 Fmake_vector (make_number (len
), Qnil
));
2433 p
= XVECTOR (XCDR (address
));
2434 cp
= (unsigned char *) &sa
->sa_family
+ sizeof (sa
->sa_family
);
2440 p
->contents
[i
++] = make_number (*cp
++);
2446 /* Get family and required size for sockaddr structure to hold ADDRESS. */
2449 get_lisp_to_sockaddr_size (address
, familyp
)
2450 Lisp_Object address
;
2453 register struct Lisp_Vector
*p
;
2455 if (VECTORP (address
))
2457 p
= XVECTOR (address
);
2461 return sizeof (struct sockaddr_in
);
2464 else if (p
->size
== 9)
2466 *familyp
= AF_INET6
;
2467 return sizeof (struct sockaddr_in6
);
2471 #ifdef HAVE_LOCAL_SOCKETS
2472 else if (STRINGP (address
))
2474 *familyp
= AF_LOCAL
;
2475 return sizeof (struct sockaddr_un
);
2478 else if (CONSP (address
) && INTEGERP (XCAR (address
)) && VECTORP (XCDR (address
)))
2480 struct sockaddr
*sa
;
2481 *familyp
= XINT (XCAR (address
));
2482 p
= XVECTOR (XCDR (address
));
2483 return p
->size
+ sizeof (sa
->sa_family
);
2488 /* Convert an address object (vector or string) to an internal sockaddr.
2490 The address format has been basically validated by
2491 get_lisp_to_sockaddr_size, but this does not mean FAMILY is valid;
2492 it could have come from user data. So if FAMILY is not valid,
2493 we return after zeroing *SA. */
2496 conv_lisp_to_sockaddr (family
, address
, sa
, len
)
2498 Lisp_Object address
;
2499 struct sockaddr
*sa
;
2502 register struct Lisp_Vector
*p
;
2503 register unsigned char *cp
= NULL
;
2508 if (VECTORP (address
))
2510 p
= XVECTOR (address
);
2511 if (family
== AF_INET
)
2513 struct sockaddr_in
*sin
= (struct sockaddr_in
*) sa
;
2514 len
= sizeof (sin
->sin_addr
) + 1;
2515 i
= XINT (p
->contents
[--len
]);
2516 sin
->sin_port
= htons (i
);
2517 cp
= (unsigned char *)&sin
->sin_addr
;
2518 sa
->sa_family
= family
;
2521 else if (family
== AF_INET6
)
2523 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*) sa
;
2524 uint16_t *ip6
= (uint16_t *)&sin6
->sin6_addr
;
2525 len
= sizeof (sin6
->sin6_addr
) + 1;
2526 i
= XINT (p
->contents
[--len
]);
2527 sin6
->sin6_port
= htons (i
);
2528 for (i
= 0; i
< len
; i
++)
2529 if (INTEGERP (p
->contents
[i
]))
2531 int j
= XFASTINT (p
->contents
[i
]) & 0xffff;
2534 sa
->sa_family
= family
;
2539 else if (STRINGP (address
))
2541 #ifdef HAVE_LOCAL_SOCKETS
2542 if (family
== AF_LOCAL
)
2544 struct sockaddr_un
*sockun
= (struct sockaddr_un
*) sa
;
2545 cp
= SDATA (address
);
2546 for (i
= 0; i
< sizeof (sockun
->sun_path
) && *cp
; i
++)
2547 sockun
->sun_path
[i
] = *cp
++;
2548 sa
->sa_family
= family
;
2555 p
= XVECTOR (XCDR (address
));
2556 cp
= (unsigned char *)sa
+ sizeof (sa
->sa_family
);
2559 for (i
= 0; i
< len
; i
++)
2560 if (INTEGERP (p
->contents
[i
]))
2561 *cp
++ = XFASTINT (p
->contents
[i
]) & 0xff;
2564 #ifdef DATAGRAM_SOCKETS
2565 DEFUN ("process-datagram-address", Fprocess_datagram_address
, Sprocess_datagram_address
,
2567 doc
: /* Get the current datagram address associated with PROCESS. */)
2569 Lisp_Object process
;
2573 CHECK_PROCESS (process
);
2575 if (!DATAGRAM_CONN_P (process
))
2578 channel
= XPROCESS (process
)->infd
;
2579 return conv_sockaddr_to_lisp (datagram_address
[channel
].sa
,
2580 datagram_address
[channel
].len
);
2583 DEFUN ("set-process-datagram-address", Fset_process_datagram_address
, Sset_process_datagram_address
,
2585 doc
: /* Set the datagram address for PROCESS to ADDRESS.
2586 Returns nil upon error setting address, ADDRESS otherwise. */)
2588 Lisp_Object process
, address
;
2593 CHECK_PROCESS (process
);
2595 if (!DATAGRAM_CONN_P (process
))
2598 channel
= XPROCESS (process
)->infd
;
2600 len
= get_lisp_to_sockaddr_size (address
, &family
);
2601 if (datagram_address
[channel
].len
!= len
)
2603 conv_lisp_to_sockaddr (family
, address
, datagram_address
[channel
].sa
, len
);
2609 static struct socket_options
{
2610 /* The name of this option. Should be lowercase version of option
2611 name without SO_ prefix. */
2613 /* Option level SOL_... */
2615 /* Option number SO_... */
2617 enum { SOPT_UNKNOWN
, SOPT_BOOL
, SOPT_INT
, SOPT_IFNAME
, SOPT_LINGER
} opttype
;
2618 enum { OPIX_NONE
=0, OPIX_MISC
=1, OPIX_REUSEADDR
=2 } optbit
;
2619 } socket_options
[] =
2621 #ifdef SO_BINDTODEVICE
2622 { ":bindtodevice", SOL_SOCKET
, SO_BINDTODEVICE
, SOPT_IFNAME
, OPIX_MISC
},
2625 { ":broadcast", SOL_SOCKET
, SO_BROADCAST
, SOPT_BOOL
, OPIX_MISC
},
2628 { ":dontroute", SOL_SOCKET
, SO_DONTROUTE
, SOPT_BOOL
, OPIX_MISC
},
2631 { ":keepalive", SOL_SOCKET
, SO_KEEPALIVE
, SOPT_BOOL
, OPIX_MISC
},
2634 { ":linger", SOL_SOCKET
, SO_LINGER
, SOPT_LINGER
, OPIX_MISC
},
2637 { ":oobinline", SOL_SOCKET
, SO_OOBINLINE
, SOPT_BOOL
, OPIX_MISC
},
2640 { ":priority", SOL_SOCKET
, SO_PRIORITY
, SOPT_INT
, OPIX_MISC
},
2643 { ":reuseaddr", SOL_SOCKET
, SO_REUSEADDR
, SOPT_BOOL
, OPIX_REUSEADDR
},
2645 { 0, 0, 0, SOPT_UNKNOWN
, OPIX_NONE
}
2648 /* Set option OPT to value VAL on socket S.
2650 Returns (1<<socket_options[OPT].optbit) if option is known, 0 otherwise.
2651 Signals an error if setting a known option fails.
2655 set_socket_option (s
, opt
, val
)
2657 Lisp_Object opt
, val
;
2660 struct socket_options
*sopt
;
2665 name
= (char *) SDATA (SYMBOL_NAME (opt
));
2666 for (sopt
= socket_options
; sopt
->name
; sopt
++)
2667 if (strcmp (name
, sopt
->name
) == 0)
2670 switch (sopt
->opttype
)
2675 optval
= NILP (val
) ? 0 : 1;
2676 ret
= setsockopt (s
, sopt
->optlevel
, sopt
->optnum
,
2677 &optval
, sizeof (optval
));
2685 optval
= XINT (val
);
2687 error ("Bad option value for %s", name
);
2688 ret
= setsockopt (s
, sopt
->optlevel
, sopt
->optnum
,
2689 &optval
, sizeof (optval
));
2693 #ifdef SO_BINDTODEVICE
2696 char devname
[IFNAMSIZ
+1];
2698 /* This is broken, at least in the Linux 2.4 kernel.
2699 To unbind, the arg must be a zero integer, not the empty string.
2700 This should work on all systems. KFS. 2003-09-23. */
2701 bzero (devname
, sizeof devname
);
2704 char *arg
= (char *) SDATA (val
);
2705 int len
= min (strlen (arg
), IFNAMSIZ
);
2706 bcopy (arg
, devname
, len
);
2708 else if (!NILP (val
))
2709 error ("Bad option value for %s", name
);
2710 ret
= setsockopt (s
, sopt
->optlevel
, sopt
->optnum
,
2719 struct linger linger
;
2722 linger
.l_linger
= 0;
2724 linger
.l_linger
= XINT (val
);
2726 linger
.l_onoff
= NILP (val
) ? 0 : 1;
2727 ret
= setsockopt (s
, sopt
->optlevel
, sopt
->optnum
,
2728 &linger
, sizeof (linger
));
2738 report_file_error ("Cannot set network option",
2739 Fcons (opt
, Fcons (val
, Qnil
)));
2740 return (1 << sopt
->optbit
);
2744 DEFUN ("set-network-process-option",
2745 Fset_network_process_option
, Sset_network_process_option
,
2747 doc
: /* For network process PROCESS set option OPTION to value VALUE.
2748 See `make-network-process' for a list of options and values.
2749 If optional fourth arg NO-ERROR is non-nil, don't signal an error if
2750 OPTION is not a supported option, return nil instead; otherwise return t. */)
2751 (process
, option
, value
, no_error
)
2752 Lisp_Object process
, option
, value
;
2753 Lisp_Object no_error
;
2756 struct Lisp_Process
*p
;
2758 CHECK_PROCESS (process
);
2759 p
= XPROCESS (process
);
2760 if (!NETCONN1_P (p
))
2761 error ("Process is not a network process");
2765 error ("Process is not running");
2767 if (set_socket_option (s
, option
, value
))
2769 p
->childp
= Fplist_put (p
->childp
, option
, value
);
2773 if (NILP (no_error
))
2774 error ("Unknown or unsupported option");
2781 DEFUN ("serial-process-configure",
2782 Fserial_process_configure
,
2783 Sserial_process_configure
,
2785 doc
: /* Configure speed, bytesize, etc. of a serial process.
2787 Arguments are specified as keyword/argument pairs. Attributes that
2788 are not given are re-initialized from the process's current
2789 configuration (available via the function `process-contact') or set to
2790 reasonable default values. The following arguments are defined:
2796 -- Any of these arguments can be given to identify the process that is
2797 to be configured. If none of these arguments is given, the current
2798 buffer's process is used.
2800 :speed SPEED -- SPEED is the speed of the serial port in bits per
2801 second, also called baud rate. Any value can be given for SPEED, but
2802 most serial ports work only at a few defined values between 1200 and
2803 115200, with 9600 being the most common value. If SPEED is nil, the
2804 serial port is not configured any further, i.e., all other arguments
2805 are ignored. This may be useful for special serial ports such as
2806 Bluetooth-to-serial converters which can only be configured through AT
2807 commands. A value of nil for SPEED can be used only when passed
2808 through `make-serial-process' or `serial-term'.
2810 :bytesize BYTESIZE -- BYTESIZE is the number of bits per byte, which
2811 can be 7 or 8. If BYTESIZE is not given or nil, a value of 8 is used.
2813 :parity PARITY -- PARITY can be nil (don't use parity), the symbol
2814 `odd' (use odd parity), or the symbol `even' (use even parity). If
2815 PARITY is not given, no parity is used.
2817 :stopbits STOPBITS -- STOPBITS is the number of stopbits used to
2818 terminate a byte transmission. STOPBITS can be 1 or 2. If STOPBITS
2819 is not given or nil, 1 stopbit is used.
2821 :flowcontrol FLOWCONTROL -- FLOWCONTROL determines the type of
2822 flowcontrol to be used, which is either nil (don't use flowcontrol),
2823 the symbol `hw' (use RTS/CTS hardware flowcontrol), or the symbol `sw'
2824 \(use XON/XOFF software flowcontrol). If FLOWCONTROL is not given, no
2825 flowcontrol is used.
2827 `serial-process-configure' is called by `make-serial-process' for the
2828 initial configuration of the serial port.
2832 \(serial-process-configure :process "/dev/ttyS0" :speed 1200)
2834 \(serial-process-configure
2835 :buffer "COM1" :stopbits 1 :parity 'odd :flowcontrol 'hw)
2837 \(serial-process-configure :port "\\\\.\\COM13" :bytesize 7)
2839 usage: (serial-process-configure &rest ARGS) */)
2844 struct Lisp_Process
*p
;
2845 Lisp_Object contact
= Qnil
;
2846 Lisp_Object proc
= Qnil
;
2847 struct gcpro gcpro1
;
2849 contact
= Flist (nargs
, args
);
2852 proc
= Fplist_get (contact
, QCprocess
);
2854 proc
= Fplist_get (contact
, QCname
);
2856 proc
= Fplist_get (contact
, QCbuffer
);
2858 proc
= Fplist_get (contact
, QCport
);
2859 proc
= get_process (proc
);
2860 p
= XPROCESS (proc
);
2861 if (!EQ (p
->type
, Qserial
))
2862 error ("Not a serial process");
2864 if (NILP (Fplist_get (p
->childp
, QCspeed
)))
2870 serial_configure (p
, contact
);
2876 /* Used by make-serial-process to recover from errors. */
2877 Lisp_Object
make_serial_process_unwind (Lisp_Object proc
)
2879 if (!PROCESSP (proc
))
2881 remove_process (proc
);
2885 DEFUN ("make-serial-process", Fmake_serial_process
, Smake_serial_process
,
2887 doc
: /* Create and return a serial port process.
2889 In Emacs, serial port connections are represented by process objects,
2890 so input and output work as for subprocesses, and `delete-process'
2891 closes a serial port connection. However, a serial process has no
2892 process id, it cannot be signaled, and the status codes are different
2893 from normal processes.
2895 `make-serial-process' creates a process and a buffer, on which you
2896 probably want to use `process-send-string'. Try \\[serial-term] for
2897 an interactive terminal. See below for examples.
2899 Arguments are specified as keyword/argument pairs. The following
2900 arguments are defined:
2902 :port PORT -- (mandatory) PORT is the path or name of the serial port.
2903 For example, this could be "/dev/ttyS0" on Unix. On Windows, this
2904 could be "COM1", or "\\\\.\\COM10" for ports higher than COM9 (double
2905 the backslashes in strings).
2907 :speed SPEED -- (mandatory) is handled by `serial-process-configure',
2908 which is called by `make-serial-process'.
2910 :name NAME -- NAME is the name of the process. If NAME is not given,
2911 the value of PORT is used.
2913 :buffer BUFFER -- BUFFER is the buffer (or buffer-name) to associate
2914 with the process. Process output goes at the end of that buffer,
2915 unless you specify an output stream or filter function to handle the
2916 output. If BUFFER is not given, the value of NAME is used.
2918 :coding CODING -- If CODING is a symbol, it specifies the coding
2919 system used for both reading and writing for this process. If CODING
2920 is a cons (DECODING . ENCODING), DECODING is used for reading, and
2921 ENCODING is used for writing.
2923 :noquery BOOL -- When exiting Emacs, query the user if BOOL is nil and
2924 the process is running. If BOOL is not given, query before exiting.
2926 :stop BOOL -- Start process in the `stopped' state if BOOL is non-nil.
2927 In the stopped state, a serial process does not accept incoming data,
2928 but you can send outgoing data. The stopped state is cleared by
2929 `continue-process' and set by `stop-process'.
2931 :filter FILTER -- Install FILTER as the process filter.
2933 :sentinel SENTINEL -- Install SENTINEL as the process sentinel.
2935 :plist PLIST -- Install PLIST as the initial plist of the process.
2942 -- These arguments are handled by `serial-process-configure', which is
2943 called by `make-serial-process'.
2945 The original argument list, possibly modified by later configuration,
2946 is available via the function `process-contact'.
2950 \(make-serial-process :port "/dev/ttyS0" :speed 9600)
2952 \(make-serial-process :port "COM1" :speed 115200 :stopbits 2)
2954 \(make-serial-process :port "\\\\.\\COM13" :speed 1200 :bytesize 7 :parity 'odd)
2956 \(make-serial-process :port "/dev/tty.BlueConsole-SPP-1" :speed nil)
2958 usage: (make-serial-process &rest ARGS) */)
2964 Lisp_Object proc
, contact
, port
;
2965 struct Lisp_Process
*p
;
2966 struct gcpro gcpro1
;
2967 Lisp_Object name
, buffer
;
2968 Lisp_Object tem
, val
;
2969 int specpdl_count
= -1;
2974 contact
= Flist (nargs
, args
);
2977 port
= Fplist_get (contact
, QCport
);
2979 error ("No port specified");
2980 CHECK_STRING (port
);
2982 if (NILP (Fplist_member (contact
, QCspeed
)))
2983 error (":speed not specified");
2984 if (!NILP (Fplist_get (contact
, QCspeed
)))
2985 CHECK_NUMBER (Fplist_get (contact
, QCspeed
));
2987 name
= Fplist_get (contact
, QCname
);
2990 CHECK_STRING (name
);
2991 proc
= make_process (name
);
2992 specpdl_count
= SPECPDL_INDEX ();
2993 record_unwind_protect (make_serial_process_unwind
, proc
);
2994 p
= XPROCESS (proc
);
2996 fd
= serial_open ((char*) SDATA (port
));
2999 if (fd
> max_process_desc
)
3000 max_process_desc
= fd
;
3001 chan_process
[fd
] = proc
;
3003 buffer
= Fplist_get (contact
, QCbuffer
);
3006 buffer
= Fget_buffer_create (buffer
);
3009 p
->childp
= contact
;
3010 p
->plist
= Fcopy_sequence (Fplist_get (contact
, QCplist
));
3012 p
->sentinel
= Fplist_get (contact
, QCsentinel
);
3013 p
->filter
= Fplist_get (contact
, QCfilter
);
3015 if (tem
= Fplist_get (contact
, QCnoquery
), !NILP (tem
))
3016 p
->kill_without_query
= 1;
3017 if (tem
= Fplist_get (contact
, QCstop
), !NILP (tem
))
3021 if (!EQ (p
->command
, Qt
))
3023 FD_SET (fd
, &input_wait_mask
);
3024 FD_SET (fd
, &non_keyboard_wait_mask
);
3027 if (BUFFERP (buffer
))
3029 set_marker_both (p
->mark
, buffer
,
3030 BUF_ZV (XBUFFER (buffer
)),
3031 BUF_ZV_BYTE (XBUFFER (buffer
)));
3034 tem
= Fplist_member (contact
, QCcoding
);
3035 if (!NILP (tem
) && (!CONSP (tem
) || !CONSP (XCDR (tem
))))
3041 val
= XCAR (XCDR (tem
));
3045 else if (!NILP (Vcoding_system_for_read
))
3046 val
= Vcoding_system_for_read
;
3047 else if ((!NILP (buffer
) && NILP (XBUFFER (buffer
)->enable_multibyte_characters
))
3048 || (NILP (buffer
) && NILP (buffer_defaults
.enable_multibyte_characters
)))
3050 p
->decode_coding_system
= val
;
3055 val
= XCAR (XCDR (tem
));
3059 else if (!NILP (Vcoding_system_for_write
))
3060 val
= Vcoding_system_for_write
;
3061 else if ((!NILP (buffer
) && NILP (XBUFFER (buffer
)->enable_multibyte_characters
))
3062 || (NILP (buffer
) && NILP (buffer_defaults
.enable_multibyte_characters
)))
3064 p
->encode_coding_system
= val
;
3066 setup_process_coding_systems (proc
);
3067 p
->decoding_buf
= make_uninit_string (0);
3068 p
->decoding_carryover
= 0;
3069 p
->encoding_buf
= make_uninit_string (0);
3070 p
->inherit_coding_system_flag
3071 = !(!NILP (tem
) || NILP (buffer
) || !inherit_process_coding_system
);
3073 Fserial_process_configure(nargs
, args
);
3075 specpdl_ptr
= specpdl
+ specpdl_count
;
3080 #endif /* HAVE_SERIAL */
3082 /* Create a network stream/datagram client/server process. Treated
3083 exactly like a normal process when reading and writing. Primary
3084 differences are in status display and process deletion. A network
3085 connection has no PID; you cannot signal it. All you can do is
3086 stop/continue it and deactivate/close it via delete-process */
3088 DEFUN ("make-network-process", Fmake_network_process
, Smake_network_process
,
3090 doc
: /* Create and return a network server or client process.
3092 In Emacs, network connections are represented by process objects, so
3093 input and output work as for subprocesses and `delete-process' closes
3094 a network connection. However, a network process has no process id,
3095 it cannot be signaled, and the status codes are different from normal
3098 Arguments are specified as keyword/argument pairs. The following
3099 arguments are defined:
3101 :name NAME -- NAME is name for process. It is modified if necessary
3104 :buffer BUFFER -- BUFFER is the buffer (or buffer-name) to associate
3105 with the process. Process output goes at end of that buffer, unless
3106 you specify an output stream or filter function to handle the output.
3107 BUFFER may be also nil, meaning that this process is not associated
3110 :host HOST -- HOST is name of the host to connect to, or its IP
3111 address. The symbol `local' specifies the local host. If specified
3112 for a server process, it must be a valid name or address for the local
3113 host, and only clients connecting to that address will be accepted.
3115 :service SERVICE -- SERVICE is name of the service desired, or an
3116 integer specifying a port number to connect to. If SERVICE is t,
3117 a random port number is selected for the server. (If Emacs was
3118 compiled with getaddrinfo, a port number can also be specified as a
3119 string, e.g. "80", as well as an integer. This is not portable.)
3121 :type TYPE -- TYPE is the type of connection. The default (nil) is a
3122 stream type connection, `datagram' creates a datagram type connection.
3124 :family FAMILY -- FAMILY is the address (and protocol) family for the
3125 service specified by HOST and SERVICE. The default (nil) is to use
3126 whatever address family (IPv4 or IPv6) that is defined for the host
3127 and port number specified by HOST and SERVICE. Other address families
3129 local -- for a local (i.e. UNIX) address specified by SERVICE.
3130 ipv4 -- use IPv4 address family only.
3131 ipv6 -- use IPv6 address family only.
3133 :local ADDRESS -- ADDRESS is the local address used for the connection.
3134 This parameter is ignored when opening a client process. When specified
3135 for a server process, the FAMILY, HOST and SERVICE args are ignored.
3137 :remote ADDRESS -- ADDRESS is the remote partner's address for the
3138 connection. This parameter is ignored when opening a stream server
3139 process. For a datagram server process, it specifies the initial
3140 setting of the remote datagram address. When specified for a client
3141 process, the FAMILY, HOST, and SERVICE args are ignored.
3143 The format of ADDRESS depends on the address family:
3144 - An IPv4 address is represented as an vector of integers [A B C D P]
3145 corresponding to numeric IP address A.B.C.D and port number P.
3146 - A local address is represented as a string with the address in the
3147 local address space.
3148 - An "unsupported family" address is represented by a cons (F . AV)
3149 where F is the family number and AV is a vector containing the socket
3150 address data with one element per address data byte. Do not rely on
3151 this format in portable code, as it may depend on implementation
3152 defined constants, data sizes, and data structure alignment.
3154 :coding CODING -- If CODING is a symbol, it specifies the coding
3155 system used for both reading and writing for this process. If CODING
3156 is a cons (DECODING . ENCODING), DECODING is used for reading, and
3157 ENCODING is used for writing.
3159 :nowait BOOL -- If BOOL is non-nil for a stream type client process,
3160 return without waiting for the connection to complete; instead, the
3161 sentinel function will be called with second arg matching "open" (if
3162 successful) or "failed" when the connect completes. Default is to use
3163 a blocking connect (i.e. wait) for stream type connections.
3165 :noquery BOOL -- Query the user unless BOOL is non-nil, and process is
3166 running when Emacs is exited.
3168 :stop BOOL -- Start process in the `stopped' state if BOOL non-nil.
3169 In the stopped state, a server process does not accept new
3170 connections, and a client process does not handle incoming traffic.
3171 The stopped state is cleared by `continue-process' and set by
3174 :filter FILTER -- Install FILTER as the process filter.
3176 :filter-multibyte BOOL -- If BOOL is non-nil, strings given to the
3177 process filter are multibyte, otherwise they are unibyte.
3178 If this keyword is not specified, the strings are multibyte if
3179 `default-enable-multibyte-characters' is non-nil.
3181 :sentinel SENTINEL -- Install SENTINEL as the process sentinel.
3183 :log LOG -- Install LOG as the server process log function. This
3184 function is called when the server accepts a network connection from a
3185 client. The arguments are SERVER, CLIENT, and MESSAGE, where SERVER
3186 is the server process, CLIENT is the new process for the connection,
3187 and MESSAGE is a string.
3189 :plist PLIST -- Install PLIST as the new process' initial plist.
3191 :server QLEN -- if QLEN is non-nil, create a server process for the
3192 specified FAMILY, SERVICE, and connection type (stream or datagram).
3193 If QLEN is an integer, it is used as the max. length of the server's
3194 pending connection queue (also known as the backlog); the default
3195 queue length is 5. Default is to create a client process.
3197 The following network options can be specified for this connection:
3199 :broadcast BOOL -- Allow send and receive of datagram broadcasts.
3200 :dontroute BOOL -- Only send to directly connected hosts.
3201 :keepalive BOOL -- Send keep-alive messages on network stream.
3202 :linger BOOL or TIMEOUT -- Send queued messages before closing.
3203 :oobinline BOOL -- Place out-of-band data in receive data stream.
3204 :priority INT -- Set protocol defined priority for sent packets.
3205 :reuseaddr BOOL -- Allow reusing a recently used local address
3206 (this is allowed by default for a server process).
3207 :bindtodevice NAME -- bind to interface NAME. Using this may require
3208 special privileges on some systems.
3210 Consult the relevant system programmer's manual pages for more
3211 information on using these options.
3214 A server process will listen for and accept connections from clients.
3215 When a client connection is accepted, a new network process is created
3216 for the connection with the following parameters:
3218 - The client's process name is constructed by concatenating the server
3219 process' NAME and a client identification string.
3220 - If the FILTER argument is non-nil, the client process will not get a
3221 separate process buffer; otherwise, the client's process buffer is a newly
3222 created buffer named after the server process' BUFFER name or process
3223 NAME concatenated with the client identification string.
3224 - The connection type and the process filter and sentinel parameters are
3225 inherited from the server process' TYPE, FILTER and SENTINEL.
3226 - The client process' contact info is set according to the client's
3227 addressing information (typically an IP address and a port number).
3228 - The client process' plist is initialized from the server's plist.
3230 Notice that the FILTER and SENTINEL args are never used directly by
3231 the server process. Also, the BUFFER argument is not used directly by
3232 the server process, but via the optional :log function, accepted (and
3233 failed) connections may be logged in the server process' buffer.
3235 The original argument list, modified with the actual connection
3236 information, is available via the `process-contact' function.
3238 usage: (make-network-process &rest ARGS) */)
3244 Lisp_Object contact
;
3245 struct Lisp_Process
*p
;
3246 #ifdef HAVE_GETADDRINFO
3247 struct addrinfo ai
, *res
, *lres
;
3248 struct addrinfo hints
;
3249 char *portstring
, portbuf
[128];
3250 #else /* HAVE_GETADDRINFO */
3251 struct _emacs_addrinfo
3257 struct sockaddr
*ai_addr
;
3258 struct _emacs_addrinfo
*ai_next
;
3260 #endif /* HAVE_GETADDRINFO */
3261 struct sockaddr_in address_in
;
3262 #ifdef HAVE_LOCAL_SOCKETS
3263 struct sockaddr_un address_un
;
3268 int s
= -1, outch
, inch
;
3269 struct gcpro gcpro1
;
3270 int count
= SPECPDL_INDEX ();
3272 Lisp_Object QCaddress
; /* one of QClocal or QCremote */
3274 Lisp_Object name
, buffer
, host
, service
, address
;
3275 Lisp_Object filter
, sentinel
;
3276 int is_non_blocking_client
= 0;
3277 int is_server
= 0, backlog
= 5;
3284 /* Save arguments for process-contact and clone-process. */
3285 contact
= Flist (nargs
, args
);
3289 /* Ensure socket support is loaded if available. */
3290 init_winsock (TRUE
);
3293 /* :type TYPE (nil: stream, datagram */
3294 tem
= Fplist_get (contact
, QCtype
);
3296 socktype
= SOCK_STREAM
;
3297 #ifdef DATAGRAM_SOCKETS
3298 else if (EQ (tem
, Qdatagram
))
3299 socktype
= SOCK_DGRAM
;
3302 error ("Unsupported connection type");
3305 tem
= Fplist_get (contact
, QCserver
);
3308 /* Don't support network sockets when non-blocking mode is
3309 not available, since a blocked Emacs is not useful. */
3310 #if !defined(O_NONBLOCK) && !defined(O_NDELAY)
3311 error ("Network servers not supported");
3315 backlog
= XINT (tem
);
3319 /* Make QCaddress an alias for :local (server) or :remote (client). */
3320 QCaddress
= is_server
? QClocal
: QCremote
;
3323 if (!is_server
&& socktype
== SOCK_STREAM
3324 && (tem
= Fplist_get (contact
, QCnowait
), !NILP (tem
)))
3326 #ifndef NON_BLOCKING_CONNECT
3327 error ("Non-blocking connect not supported");
3329 is_non_blocking_client
= 1;
3333 name
= Fplist_get (contact
, QCname
);
3334 buffer
= Fplist_get (contact
, QCbuffer
);
3335 filter
= Fplist_get (contact
, QCfilter
);
3336 sentinel
= Fplist_get (contact
, QCsentinel
);
3338 CHECK_STRING (name
);
3340 /* Initialize addrinfo structure in case we don't use getaddrinfo. */
3341 ai
.ai_socktype
= socktype
;
3346 /* :local ADDRESS or :remote ADDRESS */
3347 address
= Fplist_get (contact
, QCaddress
);
3348 if (!NILP (address
))
3350 host
= service
= Qnil
;
3352 if (!(ai
.ai_addrlen
= get_lisp_to_sockaddr_size (address
, &family
)))
3353 error ("Malformed :address");
3354 ai
.ai_family
= family
;
3355 ai
.ai_addr
= alloca (ai
.ai_addrlen
);
3356 conv_lisp_to_sockaddr (family
, address
, ai
.ai_addr
, ai
.ai_addrlen
);
3360 /* :family FAMILY -- nil (for Inet), local, or integer. */
3361 tem
= Fplist_get (contact
, QCfamily
);
3364 #if defined(HAVE_GETADDRINFO) && defined(AF_INET6)
3370 #ifdef HAVE_LOCAL_SOCKETS
3371 else if (EQ (tem
, Qlocal
))
3375 else if (EQ (tem
, Qipv6
))
3378 else if (EQ (tem
, Qipv4
))
3380 else if (INTEGERP (tem
))
3381 family
= XINT (tem
);
3383 error ("Unknown address family");
3385 ai
.ai_family
= family
;
3387 /* :service SERVICE -- string, integer (port number), or t (random port). */
3388 service
= Fplist_get (contact
, QCservice
);
3390 #ifdef HAVE_LOCAL_SOCKETS
3391 if (family
== AF_LOCAL
)
3393 /* Host is not used. */
3395 CHECK_STRING (service
);
3396 bzero (&address_un
, sizeof address_un
);
3397 address_un
.sun_family
= AF_LOCAL
;
3398 strncpy (address_un
.sun_path
, SDATA (service
), sizeof address_un
.sun_path
);
3399 ai
.ai_addr
= (struct sockaddr
*) &address_un
;
3400 ai
.ai_addrlen
= sizeof address_un
;
3405 /* :host HOST -- hostname, ip address, or 'local for localhost. */
3406 host
= Fplist_get (contact
, QChost
);
3409 if (EQ (host
, Qlocal
))
3410 host
= build_string ("localhost");
3411 CHECK_STRING (host
);
3414 /* Slow down polling to every ten seconds.
3415 Some kernels have a bug which causes retrying connect to fail
3416 after a connect. Polling can interfere with gethostbyname too. */
3417 #ifdef POLL_FOR_INPUT
3418 if (socktype
== SOCK_STREAM
)
3420 record_unwind_protect (unwind_stop_other_atimers
, Qnil
);
3421 bind_polling_period (10);
3425 #ifdef HAVE_GETADDRINFO
3426 /* If we have a host, use getaddrinfo to resolve both host and service.
3427 Otherwise, use getservbyname to lookup the service. */
3431 /* SERVICE can either be a string or int.
3432 Convert to a C string for later use by getaddrinfo. */
3433 if (EQ (service
, Qt
))
3435 else if (INTEGERP (service
))
3437 sprintf (portbuf
, "%ld", (long) XINT (service
));
3438 portstring
= portbuf
;
3442 CHECK_STRING (service
);
3443 portstring
= SDATA (service
);
3448 memset (&hints
, 0, sizeof (hints
));
3450 hints
.ai_family
= family
;
3451 hints
.ai_socktype
= socktype
;
3452 hints
.ai_protocol
= 0;
3454 #ifdef HAVE_RES_INIT
3458 ret
= getaddrinfo (SDATA (host
), portstring
, &hints
, &res
);
3460 #ifdef HAVE_GAI_STRERROR
3461 error ("%s/%s %s", SDATA (host
), portstring
, gai_strerror(ret
));
3463 error ("%s/%s getaddrinfo error %d", SDATA (host
), portstring
, ret
);
3469 #endif /* HAVE_GETADDRINFO */
3471 /* We end up here if getaddrinfo is not defined, or in case no hostname
3472 has been specified (e.g. for a local server process). */
3474 if (EQ (service
, Qt
))
3476 else if (INTEGERP (service
))
3477 port
= htons ((unsigned short) XINT (service
));
3480 struct servent
*svc_info
;
3481 CHECK_STRING (service
);
3482 svc_info
= getservbyname (SDATA (service
),
3483 (socktype
== SOCK_DGRAM
? "udp" : "tcp"));
3485 error ("Unknown service: %s", SDATA (service
));
3486 port
= svc_info
->s_port
;
3489 bzero (&address_in
, sizeof address_in
);
3490 address_in
.sin_family
= family
;
3491 address_in
.sin_addr
.s_addr
= INADDR_ANY
;
3492 address_in
.sin_port
= port
;
3494 #ifndef HAVE_GETADDRINFO
3497 struct hostent
*host_info_ptr
;
3499 /* gethostbyname may fail with TRY_AGAIN, but we don't honour that,
3500 as it may `hang' Emacs for a very long time. */
3504 #ifdef HAVE_RES_INIT
3508 host_info_ptr
= gethostbyname (SDATA (host
));
3513 bcopy (host_info_ptr
->h_addr
, (char *) &address_in
.sin_addr
,
3514 host_info_ptr
->h_length
);
3515 family
= host_info_ptr
->h_addrtype
;
3516 address_in
.sin_family
= family
;
3519 /* Attempt to interpret host as numeric inet address */
3521 unsigned long numeric_addr
;
3522 numeric_addr
= inet_addr ((char *) SDATA (host
));
3523 if (numeric_addr
== -1)
3524 error ("Unknown host \"%s\"", SDATA (host
));
3526 bcopy ((char *)&numeric_addr
, (char *) &address_in
.sin_addr
,
3527 sizeof (address_in
.sin_addr
));
3531 #endif /* not HAVE_GETADDRINFO */
3533 ai
.ai_family
= family
;
3534 ai
.ai_addr
= (struct sockaddr
*) &address_in
;
3535 ai
.ai_addrlen
= sizeof address_in
;
3539 /* Do this in case we never enter the for-loop below. */
3540 count1
= SPECPDL_INDEX ();
3543 for (lres
= res
; lres
; lres
= lres
->ai_next
)
3549 s
= socket (lres
->ai_family
, lres
->ai_socktype
, lres
->ai_protocol
);
3556 #ifdef DATAGRAM_SOCKETS
3557 if (!is_server
&& socktype
== SOCK_DGRAM
)
3559 #endif /* DATAGRAM_SOCKETS */
3561 #ifdef NON_BLOCKING_CONNECT
3562 if (is_non_blocking_client
)
3565 ret
= fcntl (s
, F_SETFL
, O_NONBLOCK
);
3567 ret
= fcntl (s
, F_SETFL
, O_NDELAY
);
3579 /* Make us close S if quit. */
3580 record_unwind_protect (close_file_unwind
, make_number (s
));
3582 /* Parse network options in the arg list.
3583 We simply ignore anything which isn't a known option (including other keywords).
3584 An error is signaled if setting a known option fails. */
3585 for (optn
= optbits
= 0; optn
< nargs
-1; optn
+= 2)
3586 optbits
|= set_socket_option (s
, args
[optn
], args
[optn
+1]);
3590 /* Configure as a server socket. */
3592 /* SO_REUSEADDR = 1 is default for server sockets; must specify
3593 explicit :reuseaddr key to override this. */
3594 #ifdef HAVE_LOCAL_SOCKETS
3595 if (family
!= AF_LOCAL
)
3597 if (!(optbits
& (1 << OPIX_REUSEADDR
)))
3600 if (setsockopt (s
, SOL_SOCKET
, SO_REUSEADDR
, &optval
, sizeof optval
))
3601 report_file_error ("Cannot set reuse option on server socket", Qnil
);
3604 if (bind (s
, lres
->ai_addr
, lres
->ai_addrlen
))
3605 report_file_error ("Cannot bind server socket", Qnil
);
3607 #ifdef HAVE_GETSOCKNAME
3608 if (EQ (service
, Qt
))
3610 struct sockaddr_in sa1
;
3611 int len1
= sizeof (sa1
);
3612 if (getsockname (s
, (struct sockaddr
*)&sa1
, &len1
) == 0)
3614 ((struct sockaddr_in
*)(lres
->ai_addr
))->sin_port
= sa1
.sin_port
;
3615 service
= make_number (ntohs (sa1
.sin_port
));
3616 contact
= Fplist_put (contact
, QCservice
, service
);
3621 if (socktype
== SOCK_STREAM
&& listen (s
, backlog
))
3622 report_file_error ("Cannot listen on server socket", Qnil
);
3630 /* This turns off all alarm-based interrupts; the
3631 bind_polling_period call above doesn't always turn all the
3632 short-interval ones off, especially if interrupt_input is
3635 It'd be nice to be able to control the connect timeout
3636 though. Would non-blocking connect calls be portable?
3638 This used to be conditioned by HAVE_GETADDRINFO. Why? */
3640 turn_on_atimers (0);
3642 ret
= connect (s
, lres
->ai_addr
, lres
->ai_addrlen
);
3645 turn_on_atimers (1);
3647 if (ret
== 0 || xerrno
== EISCONN
)
3649 /* The unwind-protect will be discarded afterwards.
3650 Likewise for immediate_quit. */
3654 #ifdef NON_BLOCKING_CONNECT
3656 if (is_non_blocking_client
&& xerrno
== EINPROGRESS
)
3660 if (is_non_blocking_client
&& xerrno
== EWOULDBLOCK
)
3668 /* Discard the unwind protect closing S. */
3669 specpdl_ptr
= specpdl
+ count1
;
3673 if (xerrno
== EINTR
)
3679 #ifdef DATAGRAM_SOCKETS
3680 if (socktype
== SOCK_DGRAM
)
3682 if (datagram_address
[s
].sa
)
3684 datagram_address
[s
].sa
= (struct sockaddr
*) xmalloc (lres
->ai_addrlen
);
3685 datagram_address
[s
].len
= lres
->ai_addrlen
;
3689 bzero (datagram_address
[s
].sa
, lres
->ai_addrlen
);
3690 if (remote
= Fplist_get (contact
, QCremote
), !NILP (remote
))
3693 rlen
= get_lisp_to_sockaddr_size (remote
, &rfamily
);
3694 if (rfamily
== lres
->ai_family
&& rlen
== lres
->ai_addrlen
)
3695 conv_lisp_to_sockaddr (rfamily
, remote
,
3696 datagram_address
[s
].sa
, rlen
);
3700 bcopy (lres
->ai_addr
, datagram_address
[s
].sa
, lres
->ai_addrlen
);
3703 contact
= Fplist_put (contact
, QCaddress
,
3704 conv_sockaddr_to_lisp (lres
->ai_addr
, lres
->ai_addrlen
));
3705 #ifdef HAVE_GETSOCKNAME
3708 struct sockaddr_in sa1
;
3709 int len1
= sizeof (sa1
);
3710 if (getsockname (s
, (struct sockaddr
*)&sa1
, &len1
) == 0)
3711 contact
= Fplist_put (contact
, QClocal
,
3712 conv_sockaddr_to_lisp (&sa1
, len1
));
3719 #ifdef HAVE_GETADDRINFO
3728 /* Discard the unwind protect for closing S, if any. */
3729 specpdl_ptr
= specpdl
+ count1
;
3731 /* Unwind bind_polling_period and request_sigio. */
3732 unbind_to (count
, Qnil
);
3736 /* If non-blocking got this far - and failed - assume non-blocking is
3737 not supported after all. This is probably a wrong assumption, but
3738 the normal blocking calls to open-network-stream handles this error
3740 if (is_non_blocking_client
)
3745 report_file_error ("make server process failed", contact
);
3747 report_file_error ("make client process failed", contact
);
3754 buffer
= Fget_buffer_create (buffer
);
3755 proc
= make_process (name
);
3757 chan_process
[inch
] = proc
;
3760 fcntl (inch
, F_SETFL
, O_NONBLOCK
);
3763 fcntl (inch
, F_SETFL
, O_NDELAY
);
3767 p
= XPROCESS (proc
);
3769 p
->childp
= contact
;
3770 p
->plist
= Fcopy_sequence (Fplist_get (contact
, QCplist
));
3774 p
->sentinel
= sentinel
;
3776 p
->log
= Fplist_get (contact
, QClog
);
3777 if (tem
= Fplist_get (contact
, QCnoquery
), !NILP (tem
))
3778 p
->kill_without_query
= 1;
3779 if ((tem
= Fplist_get (contact
, QCstop
), !NILP (tem
)))
3784 if (is_server
&& socktype
== SOCK_STREAM
)
3785 p
->status
= Qlisten
;
3787 /* Make the process marker point into the process buffer (if any). */
3788 if (BUFFERP (buffer
))
3789 set_marker_both (p
->mark
, buffer
,
3790 BUF_ZV (XBUFFER (buffer
)),
3791 BUF_ZV_BYTE (XBUFFER (buffer
)));
3793 #ifdef NON_BLOCKING_CONNECT
3794 if (is_non_blocking_client
)
3796 /* We may get here if connect did succeed immediately. However,
3797 in that case, we still need to signal this like a non-blocking
3799 p
->status
= Qconnect
;
3800 if (!FD_ISSET (inch
, &connect_wait_mask
))
3802 FD_SET (inch
, &connect_wait_mask
);
3803 num_pending_connects
++;
3808 /* A server may have a client filter setting of Qt, but it must
3809 still listen for incoming connects unless it is stopped. */
3810 if ((!EQ (p
->filter
, Qt
) && !EQ (p
->command
, Qt
))
3811 || (EQ (p
->status
, Qlisten
) && NILP (p
->command
)))
3813 FD_SET (inch
, &input_wait_mask
);
3814 FD_SET (inch
, &non_keyboard_wait_mask
);
3817 if (inch
> max_process_desc
)
3818 max_process_desc
= inch
;
3820 tem
= Fplist_member (contact
, QCcoding
);
3821 if (!NILP (tem
) && (!CONSP (tem
) || !CONSP (XCDR (tem
))))
3822 tem
= Qnil
; /* No error message (too late!). */
3825 /* Setup coding systems for communicating with the network stream. */
3826 struct gcpro gcpro1
;
3827 /* Qt denotes we have not yet called Ffind_operation_coding_system. */
3828 Lisp_Object coding_systems
= Qt
;
3829 Lisp_Object args
[5], val
;
3833 val
= XCAR (XCDR (tem
));
3837 else if (!NILP (Vcoding_system_for_read
))
3838 val
= Vcoding_system_for_read
;
3839 else if ((!NILP (buffer
) && NILP (XBUFFER (buffer
)->enable_multibyte_characters
))
3840 || (NILP (buffer
) && NILP (buffer_defaults
.enable_multibyte_characters
)))
3841 /* We dare not decode end-of-line format by setting VAL to
3842 Qraw_text, because the existing Emacs Lisp libraries
3843 assume that they receive bare code including a sequene of
3848 if (NILP (host
) || NILP (service
))
3849 coding_systems
= Qnil
;
3852 args
[0] = Qopen_network_stream
, args
[1] = name
,
3853 args
[2] = buffer
, args
[3] = host
, args
[4] = service
;
3855 coding_systems
= Ffind_operation_coding_system (5, args
);
3858 if (CONSP (coding_systems
))
3859 val
= XCAR (coding_systems
);
3860 else if (CONSP (Vdefault_process_coding_system
))
3861 val
= XCAR (Vdefault_process_coding_system
);
3865 p
->decode_coding_system
= val
;
3869 val
= XCAR (XCDR (tem
));
3873 else if (!NILP (Vcoding_system_for_write
))
3874 val
= Vcoding_system_for_write
;
3875 else if (NILP (current_buffer
->enable_multibyte_characters
))
3879 if (EQ (coding_systems
, Qt
))
3881 if (NILP (host
) || NILP (service
))
3882 coding_systems
= Qnil
;
3885 args
[0] = Qopen_network_stream
, args
[1] = name
,
3886 args
[2] = buffer
, args
[3] = host
, args
[4] = service
;
3888 coding_systems
= Ffind_operation_coding_system (5, args
);
3892 if (CONSP (coding_systems
))
3893 val
= XCDR (coding_systems
);
3894 else if (CONSP (Vdefault_process_coding_system
))
3895 val
= XCDR (Vdefault_process_coding_system
);
3899 p
->encode_coding_system
= val
;
3901 setup_process_coding_systems (proc
);
3903 p
->decoding_buf
= make_uninit_string (0);
3904 p
->decoding_carryover
= 0;
3905 p
->encoding_buf
= make_uninit_string (0);
3907 p
->inherit_coding_system_flag
3908 = !(!NILP (tem
) || NILP (buffer
) || !inherit_process_coding_system
);
3913 #endif /* HAVE_SOCKETS */
3916 #if defined(HAVE_SOCKETS) && defined(HAVE_NET_IF_H) && defined(HAVE_SYS_IOCTL_H)
3919 DEFUN ("network-interface-list", Fnetwork_interface_list
, Snetwork_interface_list
, 0, 0, 0,
3920 doc
: /* Return an alist of all network interfaces and their network address.
3921 Each element is a cons, the car of which is a string containing the
3922 interface name, and the cdr is the network address in internal
3923 format; see the description of ADDRESS in `make-network-process'. */)
3926 struct ifconf ifconf
;
3927 struct ifreq
*ifreqs
= NULL
;
3932 s
= socket (AF_INET
, SOCK_STREAM
, 0);
3938 buf_size
= ifaces
* sizeof(ifreqs
[0]);
3939 ifreqs
= (struct ifreq
*)xrealloc(ifreqs
, buf_size
);
3946 ifconf
.ifc_len
= buf_size
;
3947 ifconf
.ifc_req
= ifreqs
;
3948 if (ioctl (s
, SIOCGIFCONF
, &ifconf
))
3954 if (ifconf
.ifc_len
== buf_size
)
3958 ifaces
= ifconf
.ifc_len
/ sizeof (ifreqs
[0]);
3961 while (--ifaces
>= 0)
3963 struct ifreq
*ifq
= &ifreqs
[ifaces
];
3964 char namebuf
[sizeof (ifq
->ifr_name
) + 1];
3965 if (ifq
->ifr_addr
.sa_family
!= AF_INET
)
3967 bcopy (ifq
->ifr_name
, namebuf
, sizeof (ifq
->ifr_name
));
3968 namebuf
[sizeof (ifq
->ifr_name
)] = 0;
3969 res
= Fcons (Fcons (build_string (namebuf
),
3970 conv_sockaddr_to_lisp (&ifq
->ifr_addr
,
3971 sizeof (struct sockaddr
))),
3977 #endif /* SIOCGIFCONF */
3979 #if defined(SIOCGIFADDR) || defined(SIOCGIFHWADDR) || defined(SIOCGIFFLAGS)
3986 static struct ifflag_def ifflag_table
[] = {
3990 #ifdef IFF_BROADCAST
3991 { IFF_BROADCAST
, "broadcast" },
3994 { IFF_DEBUG
, "debug" },
3997 { IFF_LOOPBACK
, "loopback" },
3999 #ifdef IFF_POINTOPOINT
4000 { IFF_POINTOPOINT
, "pointopoint" },
4003 { IFF_RUNNING
, "running" },
4006 { IFF_NOARP
, "noarp" },
4009 { IFF_PROMISC
, "promisc" },
4011 #ifdef IFF_NOTRAILERS
4012 { IFF_NOTRAILERS
, "notrailers" },
4015 { IFF_ALLMULTI
, "allmulti" },
4018 { IFF_MASTER
, "master" },
4021 { IFF_SLAVE
, "slave" },
4023 #ifdef IFF_MULTICAST
4024 { IFF_MULTICAST
, "multicast" },
4027 { IFF_PORTSEL
, "portsel" },
4029 #ifdef IFF_AUTOMEDIA
4030 { IFF_AUTOMEDIA
, "automedia" },
4033 { IFF_DYNAMIC
, "dynamic" },
4036 { IFF_OACTIVE
, "oactive" }, /* OpenBSD: transmission in progress */
4039 { IFF_SIMPLEX
, "simplex" }, /* OpenBSD: can't hear own transmissions */
4042 { IFF_LINK0
, "link0" }, /* OpenBSD: per link layer defined bit */
4045 { IFF_LINK1
, "link1" }, /* OpenBSD: per link layer defined bit */
4048 { IFF_LINK2
, "link2" }, /* OpenBSD: per link layer defined bit */
4053 DEFUN ("network-interface-info", Fnetwork_interface_info
, Snetwork_interface_info
, 1, 1, 0,
4054 doc
: /* Return information about network interface named IFNAME.
4055 The return value is a list (ADDR BCAST NETMASK HWADDR FLAGS),
4056 where ADDR is the layer 3 address, BCAST is the layer 3 broadcast address,
4057 NETMASK is the layer 3 network mask, HWADDR is the layer 2 addres, and
4058 FLAGS is the current flags of the interface. */)
4063 Lisp_Object res
= Qnil
;
4068 CHECK_STRING (ifname
);
4070 bzero (rq
.ifr_name
, sizeof rq
.ifr_name
);
4071 strncpy (rq
.ifr_name
, SDATA (ifname
), sizeof (rq
.ifr_name
));
4073 s
= socket (AF_INET
, SOCK_STREAM
, 0);
4078 #if defined(SIOCGIFFLAGS) && defined(HAVE_STRUCT_IFREQ_IFR_FLAGS)
4079 if (ioctl (s
, SIOCGIFFLAGS
, &rq
) == 0)
4081 int flags
= rq
.ifr_flags
;
4082 struct ifflag_def
*fp
;
4086 for (fp
= ifflag_table
; flags
!= 0 && fp
->flag_sym
; fp
++)
4088 if (flags
& fp
->flag_bit
)
4090 elt
= Fcons (intern (fp
->flag_sym
), elt
);
4091 flags
-= fp
->flag_bit
;
4094 for (fnum
= 0; flags
&& fnum
< 32; fnum
++)
4096 if (flags
& (1 << fnum
))
4098 elt
= Fcons (make_number (fnum
), elt
);
4103 res
= Fcons (elt
, res
);
4106 #if defined(SIOCGIFHWADDR) && defined(HAVE_STRUCT_IFREQ_IFR_HWADDR)
4107 if (ioctl (s
, SIOCGIFHWADDR
, &rq
) == 0)
4109 Lisp_Object hwaddr
= Fmake_vector (make_number (6), Qnil
);
4110 register struct Lisp_Vector
*p
= XVECTOR (hwaddr
);
4114 for (n
= 0; n
< 6; n
++)
4115 p
->contents
[n
] = make_number (((unsigned char *)&rq
.ifr_hwaddr
.sa_data
[0])[n
]);
4116 elt
= Fcons (make_number (rq
.ifr_hwaddr
.sa_family
), hwaddr
);
4119 res
= Fcons (elt
, res
);
4122 #if defined(SIOCGIFNETMASK) && (defined(HAVE_STRUCT_IFREQ_IFR_NETMASK) || defined(HAVE_STRUCT_IFREQ_IFR_ADDR))
4123 if (ioctl (s
, SIOCGIFNETMASK
, &rq
) == 0)
4126 #ifdef HAVE_STRUCT_IFREQ_IFR_NETMASK
4127 elt
= conv_sockaddr_to_lisp (&rq
.ifr_netmask
, sizeof (rq
.ifr_netmask
));
4129 elt
= conv_sockaddr_to_lisp (&rq
.ifr_addr
, sizeof (rq
.ifr_addr
));
4133 res
= Fcons (elt
, res
);
4136 #if defined(SIOCGIFBRDADDR) && defined(HAVE_STRUCT_IFREQ_IFR_BROADADDR)
4137 if (ioctl (s
, SIOCGIFBRDADDR
, &rq
) == 0)
4140 elt
= conv_sockaddr_to_lisp (&rq
.ifr_broadaddr
, sizeof (rq
.ifr_broadaddr
));
4143 res
= Fcons (elt
, res
);
4146 #if defined(SIOCGIFADDR) && defined(HAVE_STRUCT_IFREQ_IFR_ADDR)
4147 if (ioctl (s
, SIOCGIFADDR
, &rq
) == 0)
4150 elt
= conv_sockaddr_to_lisp (&rq
.ifr_addr
, sizeof (rq
.ifr_addr
));
4153 res
= Fcons (elt
, res
);
4157 return any
? res
: Qnil
;
4160 #endif /* HAVE_SOCKETS */
4162 /* Turn off input and output for process PROC. */
4165 deactivate_process (proc
)
4168 register int inchannel
, outchannel
;
4169 register struct Lisp_Process
*p
= XPROCESS (proc
);
4171 inchannel
= p
->infd
;
4172 outchannel
= p
->outfd
;
4174 #ifdef ADAPTIVE_READ_BUFFERING
4175 if (p
->read_output_delay
> 0)
4177 if (--process_output_delay_count
< 0)
4178 process_output_delay_count
= 0;
4179 p
->read_output_delay
= 0;
4180 p
->read_output_skip
= 0;
4186 /* Beware SIGCHLD hereabouts. */
4187 flush_pending_output (inchannel
);
4188 emacs_close (inchannel
);
4189 if (outchannel
>= 0 && outchannel
!= inchannel
)
4190 emacs_close (outchannel
);
4194 #ifdef DATAGRAM_SOCKETS
4195 if (DATAGRAM_CHAN_P (inchannel
))
4197 xfree (datagram_address
[inchannel
].sa
);
4198 datagram_address
[inchannel
].sa
= 0;
4199 datagram_address
[inchannel
].len
= 0;
4202 chan_process
[inchannel
] = Qnil
;
4203 FD_CLR (inchannel
, &input_wait_mask
);
4204 FD_CLR (inchannel
, &non_keyboard_wait_mask
);
4205 #ifdef NON_BLOCKING_CONNECT
4206 if (FD_ISSET (inchannel
, &connect_wait_mask
))
4208 FD_CLR (inchannel
, &connect_wait_mask
);
4209 if (--num_pending_connects
< 0)
4213 if (inchannel
== max_process_desc
)
4216 /* We just closed the highest-numbered process input descriptor,
4217 so recompute the highest-numbered one now. */
4218 max_process_desc
= 0;
4219 for (i
= 0; i
< MAXDESC
; i
++)
4220 if (!NILP (chan_process
[i
]))
4221 max_process_desc
= i
;
4226 /* Close all descriptors currently in use for communication
4227 with subprocess. This is used in a newly-forked subprocess
4228 to get rid of irrelevant descriptors. */
4231 close_process_descs ()
4235 for (i
= 0; i
< MAXDESC
; i
++)
4237 Lisp_Object process
;
4238 process
= chan_process
[i
];
4239 if (!NILP (process
))
4241 int in
= XPROCESS (process
)->infd
;
4242 int out
= XPROCESS (process
)->outfd
;
4245 if (out
>= 0 && in
!= out
)
4252 DEFUN ("accept-process-output", Faccept_process_output
, Saccept_process_output
,
4254 doc
: /* Allow any pending output from subprocesses to be read by Emacs.
4255 It is read into the process' buffers or given to their filter functions.
4256 Non-nil arg PROCESS means do not return until some output has been received
4259 Non-nil second arg SECONDS and third arg MILLISEC are number of seconds
4260 and milliseconds to wait; return after that much time whether or not
4261 there is any subprocess output. If SECONDS is a floating point number,
4262 it specifies a fractional number of seconds to wait.
4263 The MILLISEC argument is obsolete and should be avoided.
4265 If optional fourth arg JUST-THIS-ONE is non-nil, only accept output
4266 from PROCESS, suspending reading output from other processes.
4267 If JUST-THIS-ONE is an integer, don't run any timers either.
4268 Return non-nil if we received any output before the timeout expired. */)
4269 (process
, seconds
, millisec
, just_this_one
)
4270 register Lisp_Object process
, seconds
, millisec
, just_this_one
;
4272 int secs
, usecs
= 0;
4274 if (! NILP (process
))
4275 CHECK_PROCESS (process
);
4277 just_this_one
= Qnil
;
4279 if (!NILP (millisec
))
4280 { /* Obsolete calling convention using integers rather than floats. */
4281 CHECK_NUMBER (millisec
);
4283 seconds
= make_float (XINT (millisec
) / 1000.0);
4286 CHECK_NUMBER (seconds
);
4287 seconds
= make_float (XINT (millisec
) / 1000.0 + XINT (seconds
));
4291 if (!NILP (seconds
))
4293 if (INTEGERP (seconds
))
4294 secs
= XINT (seconds
);
4295 else if (FLOATP (seconds
))
4297 double timeout
= XFLOAT_DATA (seconds
);
4298 secs
= (int) timeout
;
4299 usecs
= (int) ((timeout
- (double) secs
) * 1000000);
4302 wrong_type_argument (Qnumberp
, seconds
);
4304 if (secs
< 0 || (secs
== 0 && usecs
== 0))
4305 secs
= -1, usecs
= 0;
4308 secs
= NILP (process
) ? -1 : 0;
4311 (wait_reading_process_output (secs
, usecs
, 0, 0,
4313 !NILP (process
) ? XPROCESS (process
) : NULL
,
4314 NILP (just_this_one
) ? 0 :
4315 !INTEGERP (just_this_one
) ? 1 : -1)
4319 /* Accept a connection for server process SERVER on CHANNEL. */
4321 static int connect_counter
= 0;
4324 server_accept_connection (server
, channel
)
4328 Lisp_Object proc
, caller
, name
, buffer
;
4329 Lisp_Object contact
, host
, service
;
4330 struct Lisp_Process
*ps
= XPROCESS (server
);
4331 struct Lisp_Process
*p
;
4335 struct sockaddr_in in
;
4337 struct sockaddr_in6 in6
;
4339 #ifdef HAVE_LOCAL_SOCKETS
4340 struct sockaddr_un un
;
4343 int len
= sizeof saddr
;
4345 s
= accept (channel
, &saddr
.sa
, &len
);
4354 if (code
== EWOULDBLOCK
)
4358 if (!NILP (ps
->log
))
4359 call3 (ps
->log
, server
, Qnil
,
4360 concat3 (build_string ("accept failed with code"),
4361 Fnumber_to_string (make_number (code
)),
4362 build_string ("\n")));
4368 /* Setup a new process to handle the connection. */
4370 /* Generate a unique identification of the caller, and build contact
4371 information for this process. */
4374 switch (saddr
.sa
.sa_family
)
4378 Lisp_Object args
[5];
4379 unsigned char *ip
= (unsigned char *)&saddr
.in
.sin_addr
.s_addr
;
4380 args
[0] = build_string ("%d.%d.%d.%d");
4381 args
[1] = make_number (*ip
++);
4382 args
[2] = make_number (*ip
++);
4383 args
[3] = make_number (*ip
++);
4384 args
[4] = make_number (*ip
++);
4385 host
= Fformat (5, args
);
4386 service
= make_number (ntohs (saddr
.in
.sin_port
));
4388 args
[0] = build_string (" <%s:%d>");
4391 caller
= Fformat (3, args
);
4398 Lisp_Object args
[9];
4399 uint16_t *ip6
= (uint16_t *)&saddr
.in6
.sin6_addr
;
4401 args
[0] = build_string ("%x:%x:%x:%x:%x:%x:%x:%x");
4402 for (i
= 0; i
< 8; i
++)
4403 args
[i
+1] = make_number (ntohs(ip6
[i
]));
4404 host
= Fformat (9, args
);
4405 service
= make_number (ntohs (saddr
.in
.sin_port
));
4407 args
[0] = build_string (" <[%s]:%d>");
4410 caller
= Fformat (3, args
);
4415 #ifdef HAVE_LOCAL_SOCKETS
4419 caller
= Fnumber_to_string (make_number (connect_counter
));
4420 caller
= concat3 (build_string (" <"), caller
, build_string (">"));
4424 /* Create a new buffer name for this process if it doesn't have a
4425 filter. The new buffer name is based on the buffer name or
4426 process name of the server process concatenated with the caller
4429 if (!NILP (ps
->filter
) && !EQ (ps
->filter
, Qt
))
4433 buffer
= ps
->buffer
;
4435 buffer
= Fbuffer_name (buffer
);
4440 buffer
= concat2 (buffer
, caller
);
4441 buffer
= Fget_buffer_create (buffer
);
4445 /* Generate a unique name for the new server process. Combine the
4446 server process name with the caller identification. */
4448 name
= concat2 (ps
->name
, caller
);
4449 proc
= make_process (name
);
4451 chan_process
[s
] = proc
;
4454 fcntl (s
, F_SETFL
, O_NONBLOCK
);
4457 fcntl (s
, F_SETFL
, O_NDELAY
);
4461 p
= XPROCESS (proc
);
4463 /* Build new contact information for this setup. */
4464 contact
= Fcopy_sequence (ps
->childp
);
4465 contact
= Fplist_put (contact
, QCserver
, Qnil
);
4466 contact
= Fplist_put (contact
, QChost
, host
);
4467 if (!NILP (service
))
4468 contact
= Fplist_put (contact
, QCservice
, service
);
4469 contact
= Fplist_put (contact
, QCremote
,
4470 conv_sockaddr_to_lisp (&saddr
.sa
, len
));
4471 #ifdef HAVE_GETSOCKNAME
4473 if (getsockname (s
, &saddr
.sa
, &len
) == 0)
4474 contact
= Fplist_put (contact
, QClocal
,
4475 conv_sockaddr_to_lisp (&saddr
.sa
, len
));
4478 p
->childp
= contact
;
4479 p
->plist
= Fcopy_sequence (ps
->plist
);
4483 p
->sentinel
= ps
->sentinel
;
4484 p
->filter
= ps
->filter
;
4491 /* Client processes for accepted connections are not stopped initially. */
4492 if (!EQ (p
->filter
, Qt
))
4494 FD_SET (s
, &input_wait_mask
);
4495 FD_SET (s
, &non_keyboard_wait_mask
);
4498 if (s
> max_process_desc
)
4499 max_process_desc
= s
;
4501 /* Setup coding system for new process based on server process.
4502 This seems to be the proper thing to do, as the coding system
4503 of the new process should reflect the settings at the time the
4504 server socket was opened; not the current settings. */
4506 p
->decode_coding_system
= ps
->decode_coding_system
;
4507 p
->encode_coding_system
= ps
->encode_coding_system
;
4508 setup_process_coding_systems (proc
);
4510 p
->decoding_buf
= make_uninit_string (0);
4511 p
->decoding_carryover
= 0;
4512 p
->encoding_buf
= make_uninit_string (0);
4514 p
->inherit_coding_system_flag
4515 = (NILP (buffer
) ? 0 : ps
->inherit_coding_system_flag
);
4517 if (!NILP (ps
->log
))
4518 call3 (ps
->log
, server
, proc
,
4519 concat3 (build_string ("accept from "),
4520 (STRINGP (host
) ? host
: build_string ("-")),
4521 build_string ("\n")));
4523 if (!NILP (p
->sentinel
))
4524 exec_sentinel (proc
,
4525 concat3 (build_string ("open from "),
4526 (STRINGP (host
) ? host
: build_string ("-")),
4527 build_string ("\n")));
4530 /* This variable is different from waiting_for_input in keyboard.c.
4531 It is used to communicate to a lisp process-filter/sentinel (via the
4532 function Fwaiting_for_user_input_p below) whether Emacs was waiting
4533 for user-input when that process-filter was called.
4534 waiting_for_input cannot be used as that is by definition 0 when
4535 lisp code is being evalled.
4536 This is also used in record_asynch_buffer_change.
4537 For that purpose, this must be 0
4538 when not inside wait_reading_process_output. */
4539 static int waiting_for_user_input_p
;
4542 wait_reading_process_output_unwind (data
)
4545 waiting_for_user_input_p
= XINT (data
);
4549 /* This is here so breakpoints can be put on it. */
4551 wait_reading_process_output_1 ()
4555 /* Use a wrapper around select to work around a bug in gdb 5.3.
4556 Normally, the wrapper is optimzed away by inlining.
4558 If emacs is stopped inside select, the gdb backtrace doesn't
4559 show the function which called select, so it is practically
4560 impossible to step through wait_reading_process_output. */
4564 select_wrapper (n
, rfd
, wfd
, xfd
, tmo
)
4566 SELECT_TYPE
*rfd
, *wfd
, *xfd
;
4569 return select (n
, rfd
, wfd
, xfd
, tmo
);
4571 #define select select_wrapper
4574 /* Read and dispose of subprocess output while waiting for timeout to
4575 elapse and/or keyboard input to be available.
4578 timeout in seconds, or
4579 zero for no limit, or
4580 -1 means gobble data immediately available but don't wait for any.
4583 an additional duration to wait, measured in microseconds.
4584 If this is nonzero and time_limit is 0, then the timeout
4585 consists of MICROSECS only.
4587 READ_KBD is a lisp value:
4588 0 to ignore keyboard input, or
4589 1 to return when input is available, or
4590 -1 meaning caller will actually read the input, so don't throw to
4591 the quit handler, or
4593 DO_DISPLAY != 0 means redisplay should be done to show subprocess
4594 output that arrives.
4596 If WAIT_FOR_CELL is a cons cell, wait until its car is non-nil
4597 (and gobble terminal input into the buffer if any arrives).
4599 If WAIT_PROC is specified, wait until something arrives from that
4600 process. The return value is true if we read some input from
4603 If JUST_WAIT_PROC is non-nil, handle only output from WAIT_PROC
4604 (suspending output from other processes). A negative value
4605 means don't run any timers either.
4607 If WAIT_PROC is specified, then the function returns true if we
4608 received input from that process before the timeout elapsed.
4609 Otherwise, return true if we received input from any process. */
4612 wait_reading_process_output (time_limit
, microsecs
, read_kbd
, do_display
,
4613 wait_for_cell
, wait_proc
, just_wait_proc
)
4614 int time_limit
, microsecs
, read_kbd
, do_display
;
4615 Lisp_Object wait_for_cell
;
4616 struct Lisp_Process
*wait_proc
;
4619 register int channel
, nfds
;
4620 SELECT_TYPE Available
;
4621 #ifdef NON_BLOCKING_CONNECT
4622 SELECT_TYPE Connecting
;
4625 int check_delay
, no_avail
;
4628 EMACS_TIME timeout
, end_time
;
4629 int wait_channel
= -1;
4630 int got_some_input
= 0;
4631 int count
= SPECPDL_INDEX ();
4633 FD_ZERO (&Available
);
4634 #ifdef NON_BLOCKING_CONNECT
4635 FD_ZERO (&Connecting
);
4638 /* If wait_proc is a process to watch, set wait_channel accordingly. */
4639 if (wait_proc
!= NULL
)
4640 wait_channel
= wait_proc
->infd
;
4642 record_unwind_protect (wait_reading_process_output_unwind
,
4643 make_number (waiting_for_user_input_p
));
4644 waiting_for_user_input_p
= read_kbd
;
4646 /* Since we may need to wait several times,
4647 compute the absolute time to return at. */
4648 if (time_limit
|| microsecs
)
4650 EMACS_GET_TIME (end_time
);
4651 EMACS_SET_SECS_USECS (timeout
, time_limit
, microsecs
);
4652 EMACS_ADD_TIME (end_time
, end_time
, timeout
);
4657 int timeout_reduced_for_timers
= 0;
4659 /* If calling from keyboard input, do not quit
4660 since we want to return C-g as an input character.
4661 Otherwise, do pending quit if requested. */
4666 process_pending_signals ();
4669 /* Exit now if the cell we're waiting for became non-nil. */
4670 if (! NILP (wait_for_cell
) && ! NILP (XCAR (wait_for_cell
)))
4673 /* Compute time from now till when time limit is up */
4674 /* Exit if already run out */
4675 if (time_limit
== -1)
4677 /* -1 specified for timeout means
4678 gobble output available now
4679 but don't wait at all. */
4681 EMACS_SET_SECS_USECS (timeout
, 0, 0);
4683 else if (time_limit
|| microsecs
)
4685 EMACS_GET_TIME (timeout
);
4686 EMACS_SUB_TIME (timeout
, end_time
, timeout
);
4687 if (EMACS_TIME_NEG_P (timeout
))
4692 EMACS_SET_SECS_USECS (timeout
, 100000, 0);
4695 /* Normally we run timers here.
4696 But not if wait_for_cell; in those cases,
4697 the wait is supposed to be short,
4698 and those callers cannot handle running arbitrary Lisp code here. */
4699 if (NILP (wait_for_cell
)
4700 && just_wait_proc
>= 0)
4702 EMACS_TIME timer_delay
;
4706 int old_timers_run
= timers_run
;
4707 struct buffer
*old_buffer
= current_buffer
;
4708 Lisp_Object old_window
= selected_window
;
4710 timer_delay
= timer_check (1);
4712 /* If a timer has run, this might have changed buffers
4713 an alike. Make read_key_sequence aware of that. */
4714 if (timers_run
!= old_timers_run
4715 && (old_buffer
!= current_buffer
4716 || !EQ (old_window
, selected_window
))
4717 && waiting_for_user_input_p
== -1)
4718 record_asynch_buffer_change ();
4720 if (timers_run
!= old_timers_run
&& do_display
)
4721 /* We must retry, since a timer may have requeued itself
4722 and that could alter the time_delay. */
4723 redisplay_preserve_echo_area (9);
4727 while (!detect_input_pending ());
4729 /* If there is unread keyboard input, also return. */
4731 && requeued_events_pending_p ())
4734 if (! EMACS_TIME_NEG_P (timer_delay
) && time_limit
!= -1)
4736 EMACS_TIME difference
;
4737 EMACS_SUB_TIME (difference
, timer_delay
, timeout
);
4738 if (EMACS_TIME_NEG_P (difference
))
4740 timeout
= timer_delay
;
4741 timeout_reduced_for_timers
= 1;
4744 /* If time_limit is -1, we are not going to wait at all. */
4745 else if (time_limit
!= -1)
4747 /* This is so a breakpoint can be put here. */
4748 wait_reading_process_output_1 ();
4752 /* Cause C-g and alarm signals to take immediate action,
4753 and cause input available signals to zero out timeout.
4755 It is important that we do this before checking for process
4756 activity. If we get a SIGCHLD after the explicit checks for
4757 process activity, timeout is the only way we will know. */
4759 set_waiting_for_input (&timeout
);
4761 /* If status of something has changed, and no input is
4762 available, notify the user of the change right away. After
4763 this explicit check, we'll let the SIGCHLD handler zap
4764 timeout to get our attention. */
4765 if (update_tick
!= process_tick
)
4768 #ifdef NON_BLOCKING_CONNECT
4772 Atemp
= input_wait_mask
;
4773 IF_NON_BLOCKING_CONNECT (Ctemp
= connect_wait_mask
);
4775 EMACS_SET_SECS_USECS (timeout
, 0, 0);
4776 if ((select (max (max (max_process_desc
, max_keyboard_desc
),
4779 #ifdef NON_BLOCKING_CONNECT
4780 (num_pending_connects
> 0 ? &Ctemp
: (SELECT_TYPE
*)0),
4784 (SELECT_TYPE
*)0, &timeout
)
4787 /* It's okay for us to do this and then continue with
4788 the loop, since timeout has already been zeroed out. */
4789 clear_waiting_for_input ();
4790 status_notify (NULL
);
4791 if (do_display
) redisplay_preserve_echo_area (13);
4795 /* Don't wait for output from a non-running process. Just
4796 read whatever data has already been received. */
4797 if (wait_proc
&& wait_proc
->raw_status_new
)
4798 update_status (wait_proc
);
4800 && ! EQ (wait_proc
->status
, Qrun
)
4801 && ! EQ (wait_proc
->status
, Qconnect
))
4803 int nread
, total_nread
= 0;
4805 clear_waiting_for_input ();
4806 XSETPROCESS (proc
, wait_proc
);
4808 /* Read data from the process, until we exhaust it. */
4809 while (wait_proc
->infd
>= 0)
4811 nread
= read_process_output (proc
, wait_proc
->infd
);
4818 total_nread
+= nread
;
4822 else if (nread
== -1 && EIO
== errno
)
4826 else if (nread
== -1 && EAGAIN
== errno
)
4830 else if (nread
== -1 && EWOULDBLOCK
== errno
)
4834 if (total_nread
> 0 && do_display
)
4835 redisplay_preserve_echo_area (10);
4840 /* Wait till there is something to do */
4842 if (wait_proc
&& just_wait_proc
)
4844 if (wait_proc
->infd
< 0) /* Terminated */
4846 FD_SET (wait_proc
->infd
, &Available
);
4848 IF_NON_BLOCKING_CONNECT (check_connect
= 0);
4850 else if (!NILP (wait_for_cell
))
4852 Available
= non_process_wait_mask
;
4854 IF_NON_BLOCKING_CONNECT (check_connect
= 0);
4859 Available
= non_keyboard_wait_mask
;
4861 Available
= input_wait_mask
;
4862 IF_NON_BLOCKING_CONNECT (check_connect
= (num_pending_connects
> 0));
4863 check_delay
= wait_channel
>= 0 ? 0 : process_output_delay_count
;
4866 /* If frame size has changed or the window is newly mapped,
4867 redisplay now, before we start to wait. There is a race
4868 condition here; if a SIGIO arrives between now and the select
4869 and indicates that a frame is trashed, the select may block
4870 displaying a trashed screen. */
4871 if (frame_garbaged
&& do_display
)
4873 clear_waiting_for_input ();
4874 redisplay_preserve_echo_area (11);
4876 set_waiting_for_input (&timeout
);
4880 if (read_kbd
&& detect_input_pending ())
4887 #ifdef NON_BLOCKING_CONNECT
4889 Connecting
= connect_wait_mask
;
4892 #ifdef ADAPTIVE_READ_BUFFERING
4893 /* Set the timeout for adaptive read buffering if any
4894 process has non-zero read_output_skip and non-zero
4895 read_output_delay, and we are not reading output for a
4896 specific wait_channel. It is not executed if
4897 Vprocess_adaptive_read_buffering is nil. */
4898 if (process_output_skip
&& check_delay
> 0)
4900 int usecs
= EMACS_USECS (timeout
);
4901 if (EMACS_SECS (timeout
) > 0 || usecs
> READ_OUTPUT_DELAY_MAX
)
4902 usecs
= READ_OUTPUT_DELAY_MAX
;
4903 for (channel
= 0; check_delay
> 0 && channel
<= max_process_desc
; channel
++)
4905 proc
= chan_process
[channel
];
4908 /* Find minimum non-zero read_output_delay among the
4909 processes with non-zero read_output_skip. */
4910 if (XPROCESS (proc
)->read_output_delay
> 0)
4913 if (!XPROCESS (proc
)->read_output_skip
)
4915 FD_CLR (channel
, &Available
);
4916 XPROCESS (proc
)->read_output_skip
= 0;
4917 if (XPROCESS (proc
)->read_output_delay
< usecs
)
4918 usecs
= XPROCESS (proc
)->read_output_delay
;
4921 EMACS_SET_SECS_USECS (timeout
, 0, usecs
);
4922 process_output_skip
= 0;
4930 (max (max (max_process_desc
, max_keyboard_desc
),
4933 #ifdef NON_BLOCKING_CONNECT
4934 (check_connect
? &Connecting
: (SELECT_TYPE
*)0),
4938 (SELECT_TYPE
*)0, &timeout
);
4943 /* Make C-g and alarm signals set flags again */
4944 clear_waiting_for_input ();
4946 /* If we woke up due to SIGWINCH, actually change size now. */
4947 do_pending_window_change (0);
4949 if (time_limit
&& nfds
== 0 && ! timeout_reduced_for_timers
)
4950 /* We wanted the full specified time, so return now. */
4954 if (xerrno
== EINTR
)
4956 else if (xerrno
== EBADF
)
4959 /* AIX doesn't handle PTY closure the same way BSD does. On AIX,
4960 the child's closure of the pts gives the parent a SIGHUP, and
4961 the ptc file descriptor is automatically closed,
4962 yielding EBADF here or at select() call above.
4963 So, SIGHUP is ignored (see def of PTY_TTY_NAME_SPRINTF
4964 in m/ibmrt-aix.h), and here we just ignore the select error.
4965 Cleanup occurs c/o status_notify after SIGCLD. */
4966 no_avail
= 1; /* Cannot depend on values returned */
4972 error ("select error: %s", emacs_strerror (xerrno
));
4977 FD_ZERO (&Available
);
4978 IF_NON_BLOCKING_CONNECT (check_connect
= 0);
4981 #if 0 /* When polling is used, interrupt_input is 0,
4982 so get_input_pending should read the input.
4983 So this should not be needed. */
4984 /* If we are using polling for input,
4985 and we see input available, make it get read now.
4986 Otherwise it might not actually get read for a second.
4987 And on hpux, since we turn off polling in wait_reading_process_output,
4988 it might never get read at all if we don't spend much time
4989 outside of wait_reading_process_output. */
4990 if (read_kbd
&& interrupt_input
4991 && keyboard_bit_set (&Available
)
4992 && input_polling_used ())
4993 kill (getpid (), SIGALRM
);
4996 /* Check for keyboard input */
4997 /* If there is any, return immediately
4998 to give it higher priority than subprocesses */
5002 int old_timers_run
= timers_run
;
5003 struct buffer
*old_buffer
= current_buffer
;
5004 Lisp_Object old_window
= selected_window
;
5007 if (detect_input_pending_run_timers (do_display
))
5009 swallow_events (do_display
);
5010 if (detect_input_pending_run_timers (do_display
))
5014 /* If a timer has run, this might have changed buffers
5015 an alike. Make read_key_sequence aware of that. */
5016 if (timers_run
!= old_timers_run
5017 && waiting_for_user_input_p
== -1
5018 && (old_buffer
!= current_buffer
5019 || !EQ (old_window
, selected_window
)))
5020 record_asynch_buffer_change ();
5026 /* If there is unread keyboard input, also return. */
5028 && requeued_events_pending_p ())
5031 /* If we are not checking for keyboard input now,
5032 do process events (but don't run any timers).
5033 This is so that X events will be processed.
5034 Otherwise they may have to wait until polling takes place.
5035 That would causes delays in pasting selections, for example.
5037 (We used to do this only if wait_for_cell.) */
5038 if (read_kbd
== 0 && detect_input_pending ())
5040 swallow_events (do_display
);
5041 #if 0 /* Exiting when read_kbd doesn't request that seems wrong, though. */
5042 if (detect_input_pending ())
5047 /* Exit now if the cell we're waiting for became non-nil. */
5048 if (! NILP (wait_for_cell
) && ! NILP (XCAR (wait_for_cell
)))
5052 /* If we think we have keyboard input waiting, but didn't get SIGIO,
5053 go read it. This can happen with X on BSD after logging out.
5054 In that case, there really is no input and no SIGIO,
5055 but select says there is input. */
5057 if (read_kbd
&& interrupt_input
5058 && keyboard_bit_set (&Available
) && ! noninteractive
)
5059 kill (getpid (), SIGIO
);
5063 got_some_input
|= nfds
> 0;
5065 /* If checking input just got us a size-change event from X,
5066 obey it now if we should. */
5067 if (read_kbd
|| ! NILP (wait_for_cell
))
5068 do_pending_window_change (0);
5070 /* Check for data from a process. */
5071 if (no_avail
|| nfds
== 0)
5074 /* Really FIRST_PROC_DESC should be 0 on Unix,
5075 but this is safer in the short run. */
5076 for (channel
= 0; channel
<= max_process_desc
; channel
++)
5078 if (FD_ISSET (channel
, &Available
)
5079 && FD_ISSET (channel
, &non_keyboard_wait_mask
))
5083 /* If waiting for this channel, arrange to return as
5084 soon as no more input to be processed. No more
5086 if (wait_channel
== channel
)
5092 proc
= chan_process
[channel
];
5096 /* If this is a server stream socket, accept connection. */
5097 if (EQ (XPROCESS (proc
)->status
, Qlisten
))
5099 server_accept_connection (proc
, channel
);
5103 /* Read data from the process, starting with our
5104 buffered-ahead character if we have one. */
5106 nread
= read_process_output (proc
, channel
);
5109 /* Since read_process_output can run a filter,
5110 which can call accept-process-output,
5111 don't try to read from any other processes
5112 before doing the select again. */
5113 FD_ZERO (&Available
);
5116 redisplay_preserve_echo_area (12);
5119 else if (nread
== -1 && errno
== EWOULDBLOCK
)
5122 /* ISC 4.1 defines both EWOULDBLOCK and O_NONBLOCK,
5123 and Emacs uses O_NONBLOCK, so what we get is EAGAIN. */
5125 else if (nread
== -1 && errno
== EAGAIN
)
5129 else if (nread
== -1 && errno
== EAGAIN
)
5131 /* Note that we cannot distinguish between no input
5132 available now and a closed pipe.
5133 With luck, a closed pipe will be accompanied by
5134 subprocess termination and SIGCHLD. */
5135 else if (nread
== 0 && !NETCONN_P (proc
) && !SERIALCONN_P (proc
))
5137 #endif /* O_NDELAY */
5138 #endif /* O_NONBLOCK */
5140 /* On some OSs with ptys, when the process on one end of
5141 a pty exits, the other end gets an error reading with
5142 errno = EIO instead of getting an EOF (0 bytes read).
5143 Therefore, if we get an error reading and errno =
5144 EIO, just continue, because the child process has
5145 exited and should clean itself up soon (e.g. when we
5148 However, it has been known to happen that the SIGCHLD
5149 got lost. So raise the signal again just in case.
5151 else if (nread
== -1 && errno
== EIO
)
5153 /* Clear the descriptor now, so we only raise the
5154 signal once. Don't do this if `process' is only
5156 if (XPROCESS (proc
)->pid
!= -2)
5158 FD_CLR (channel
, &input_wait_mask
);
5159 FD_CLR (channel
, &non_keyboard_wait_mask
);
5161 kill (getpid (), SIGCHLD
);
5164 #endif /* HAVE_PTYS */
5165 /* If we can detect process termination, don't consider the process
5166 gone just because its pipe is closed. */
5168 else if (nread
== 0 && !NETCONN_P (proc
) && !SERIALCONN_P (proc
))
5173 /* Preserve status of processes already terminated. */
5174 XPROCESS (proc
)->tick
= ++process_tick
;
5175 deactivate_process (proc
);
5176 if (XPROCESS (proc
)->raw_status_new
)
5177 update_status (XPROCESS (proc
));
5178 if (EQ (XPROCESS (proc
)->status
, Qrun
))
5179 XPROCESS (proc
)->status
5180 = Fcons (Qexit
, Fcons (make_number (256), Qnil
));
5183 #ifdef NON_BLOCKING_CONNECT
5184 if (check_connect
&& FD_ISSET (channel
, &Connecting
)
5185 && FD_ISSET (channel
, &connect_wait_mask
))
5187 struct Lisp_Process
*p
;
5189 FD_CLR (channel
, &connect_wait_mask
);
5190 if (--num_pending_connects
< 0)
5193 proc
= chan_process
[channel
];
5197 p
= XPROCESS (proc
);
5200 /* getsockopt(,,SO_ERROR,,) is said to hang on some systems.
5201 So only use it on systems where it is known to work. */
5203 int xlen
= sizeof(xerrno
);
5204 if (getsockopt(channel
, SOL_SOCKET
, SO_ERROR
, &xerrno
, &xlen
))
5209 struct sockaddr pname
;
5210 int pnamelen
= sizeof(pname
);
5212 /* If connection failed, getpeername will fail. */
5214 if (getpeername(channel
, &pname
, &pnamelen
) < 0)
5216 /* Obtain connect failure code through error slippage. */
5219 if (errno
== ENOTCONN
&& read(channel
, &dummy
, 1) < 0)
5226 p
->tick
= ++process_tick
;
5227 p
->status
= Fcons (Qfailed
, Fcons (make_number (xerrno
), Qnil
));
5228 deactivate_process (proc
);
5233 /* Execute the sentinel here. If we had relied on
5234 status_notify to do it later, it will read input
5235 from the process before calling the sentinel. */
5236 exec_sentinel (proc
, build_string ("open\n"));
5237 if (!EQ (p
->filter
, Qt
) && !EQ (p
->command
, Qt
))
5239 FD_SET (p
->infd
, &input_wait_mask
);
5240 FD_SET (p
->infd
, &non_keyboard_wait_mask
);
5244 #endif /* NON_BLOCKING_CONNECT */
5245 } /* end for each file descriptor */
5246 } /* end while exit conditions not met */
5248 unbind_to (count
, Qnil
);
5250 /* If calling from keyboard input, do not quit
5251 since we want to return C-g as an input character.
5252 Otherwise, do pending quit if requested. */
5255 /* Prevent input_pending from remaining set if we quit. */
5256 clear_input_pending ();
5260 return got_some_input
;
5263 /* Given a list (FUNCTION ARGS...), apply FUNCTION to the ARGS. */
5266 read_process_output_call (fun_and_args
)
5267 Lisp_Object fun_and_args
;
5269 return apply1 (XCAR (fun_and_args
), XCDR (fun_and_args
));
5273 read_process_output_error_handler (error
)
5276 cmd_error_internal (error
, "error in process filter: ");
5278 update_echo_area ();
5279 Fsleep_for (make_number (2), Qnil
);
5283 /* Read pending output from the process channel,
5284 starting with our buffered-ahead character if we have one.
5285 Yield number of decoded characters read.
5287 This function reads at most 4096 characters.
5288 If you want to read all available subprocess output,
5289 you must call it repeatedly until it returns zero.
5291 The characters read are decoded according to PROC's coding-system
5295 read_process_output (proc
, channel
)
5297 register int channel
;
5299 register int nbytes
;
5301 register Lisp_Object outstream
;
5302 register struct Lisp_Process
*p
= XPROCESS (proc
);
5303 register int opoint
;
5304 struct coding_system
*coding
= proc_decode_coding_system
[channel
];
5305 int carryover
= p
->decoding_carryover
;
5308 chars
= (char *) alloca (carryover
+ readmax
);
5310 /* See the comment above. */
5311 bcopy (SDATA (p
->decoding_buf
), chars
, carryover
);
5313 #ifdef DATAGRAM_SOCKETS
5314 /* We have a working select, so proc_buffered_char is always -1. */
5315 if (DATAGRAM_CHAN_P (channel
))
5317 int len
= datagram_address
[channel
].len
;
5318 nbytes
= recvfrom (channel
, chars
+ carryover
, readmax
,
5319 0, datagram_address
[channel
].sa
, &len
);
5323 if (proc_buffered_char
[channel
] < 0)
5325 nbytes
= emacs_read (channel
, chars
+ carryover
, readmax
);
5326 #ifdef ADAPTIVE_READ_BUFFERING
5327 if (nbytes
> 0 && p
->adaptive_read_buffering
)
5329 int delay
= p
->read_output_delay
;
5332 if (delay
< READ_OUTPUT_DELAY_MAX_MAX
)
5335 process_output_delay_count
++;
5336 delay
+= READ_OUTPUT_DELAY_INCREMENT
* 2;
5339 else if (delay
> 0 && (nbytes
== readmax
))
5341 delay
-= READ_OUTPUT_DELAY_INCREMENT
;
5343 process_output_delay_count
--;
5345 p
->read_output_delay
= delay
;
5348 p
->read_output_skip
= 1;
5349 process_output_skip
= 1;
5356 chars
[carryover
] = proc_buffered_char
[channel
];
5357 proc_buffered_char
[channel
] = -1;
5358 nbytes
= emacs_read (channel
, chars
+ carryover
+ 1, readmax
- 1);
5362 nbytes
= nbytes
+ 1;
5365 p
->decoding_carryover
= 0;
5367 /* At this point, NBYTES holds number of bytes just received
5368 (including the one in proc_buffered_char[channel]). */
5371 if (nbytes
< 0 || coding
->mode
& CODING_MODE_LAST_BLOCK
)
5373 coding
->mode
|= CODING_MODE_LAST_BLOCK
;
5376 /* Now set NBYTES how many bytes we must decode. */
5377 nbytes
+= carryover
;
5379 /* Read and dispose of the process output. */
5380 outstream
= p
->filter
;
5381 if (!NILP (outstream
))
5383 /* We inhibit quit here instead of just catching it so that
5384 hitting ^G when a filter happens to be running won't screw
5386 int count
= SPECPDL_INDEX ();
5387 Lisp_Object odeactivate
;
5388 Lisp_Object obuffer
, okeymap
;
5390 int outer_running_asynch_code
= running_asynch_code
;
5391 int waiting
= waiting_for_user_input_p
;
5393 /* No need to gcpro these, because all we do with them later
5394 is test them for EQness, and none of them should be a string. */
5395 odeactivate
= Vdeactivate_mark
;
5396 XSETBUFFER (obuffer
, current_buffer
);
5397 okeymap
= current_buffer
->keymap
;
5399 specbind (Qinhibit_quit
, Qt
);
5400 specbind (Qlast_nonmenu_event
, Qt
);
5402 /* In case we get recursively called,
5403 and we already saved the match data nonrecursively,
5404 save the same match data in safely recursive fashion. */
5405 if (outer_running_asynch_code
)
5408 /* Don't clobber the CURRENT match data, either! */
5409 tem
= Fmatch_data (Qnil
, Qnil
, Qnil
);
5410 restore_search_regs ();
5411 record_unwind_save_match_data ();
5412 Fset_match_data (tem
, Qt
);
5415 /* For speed, if a search happens within this code,
5416 save the match data in a special nonrecursive fashion. */
5417 running_asynch_code
= 1;
5419 decode_coding_c_string (coding
, chars
, nbytes
, Qt
);
5420 text
= coding
->dst_object
;
5421 Vlast_coding_system_used
= CODING_ID_NAME (coding
->id
);
5422 /* A new coding system might be found. */
5423 if (!EQ (p
->decode_coding_system
, Vlast_coding_system_used
))
5425 p
->decode_coding_system
= Vlast_coding_system_used
;
5427 /* Don't call setup_coding_system for
5428 proc_decode_coding_system[channel] here. It is done in
5429 detect_coding called via decode_coding above. */
5431 /* If a coding system for encoding is not yet decided, we set
5432 it as the same as coding-system for decoding.
5434 But, before doing that we must check if
5435 proc_encode_coding_system[p->outfd] surely points to a
5436 valid memory because p->outfd will be changed once EOF is
5437 sent to the process. */
5438 if (NILP (p
->encode_coding_system
)
5439 && proc_encode_coding_system
[p
->outfd
])
5441 p
->encode_coding_system
5442 = coding_inherit_eol_type (Vlast_coding_system_used
, Qnil
);
5443 setup_coding_system (p
->encode_coding_system
,
5444 proc_encode_coding_system
[p
->outfd
]);
5448 if (coding
->carryover_bytes
> 0)
5450 if (SCHARS (p
->decoding_buf
) < coding
->carryover_bytes
)
5451 p
->decoding_buf
= make_uninit_string (coding
->carryover_bytes
);
5452 bcopy (coding
->carryover
, SDATA (p
->decoding_buf
),
5453 coding
->carryover_bytes
);
5454 p
->decoding_carryover
= coding
->carryover_bytes
;
5456 if (SBYTES (text
) > 0)
5457 internal_condition_case_1 (read_process_output_call
,
5459 Fcons (proc
, Fcons (text
, Qnil
))),
5460 !NILP (Vdebug_on_error
) ? Qnil
: Qerror
,
5461 read_process_output_error_handler
);
5463 /* If we saved the match data nonrecursively, restore it now. */
5464 restore_search_regs ();
5465 running_asynch_code
= outer_running_asynch_code
;
5467 /* Handling the process output should not deactivate the mark. */
5468 Vdeactivate_mark
= odeactivate
;
5470 /* Restore waiting_for_user_input_p as it was
5471 when we were called, in case the filter clobbered it. */
5472 waiting_for_user_input_p
= waiting
;
5474 #if 0 /* Call record_asynch_buffer_change unconditionally,
5475 because we might have changed minor modes or other things
5476 that affect key bindings. */
5477 if (! EQ (Fcurrent_buffer (), obuffer
)
5478 || ! EQ (current_buffer
->keymap
, okeymap
))
5480 /* But do it only if the caller is actually going to read events.
5481 Otherwise there's no need to make him wake up, and it could
5482 cause trouble (for example it would make sit_for return). */
5483 if (waiting_for_user_input_p
== -1)
5484 record_asynch_buffer_change ();
5486 unbind_to (count
, Qnil
);
5490 /* If no filter, write into buffer if it isn't dead. */
5491 if (!NILP (p
->buffer
) && !NILP (XBUFFER (p
->buffer
)->name
))
5493 Lisp_Object old_read_only
;
5494 int old_begv
, old_zv
;
5495 int old_begv_byte
, old_zv_byte
;
5496 Lisp_Object odeactivate
;
5497 int before
, before_byte
;
5501 int count
= SPECPDL_INDEX ();
5503 odeactivate
= Vdeactivate_mark
;
5505 record_unwind_protect (Fset_buffer
, Fcurrent_buffer ());
5506 Fset_buffer (p
->buffer
);
5508 opoint_byte
= PT_BYTE
;
5509 old_read_only
= current_buffer
->read_only
;
5512 old_begv_byte
= BEGV_BYTE
;
5513 old_zv_byte
= ZV_BYTE
;
5515 current_buffer
->read_only
= Qnil
;
5517 /* Insert new output into buffer
5518 at the current end-of-output marker,
5519 thus preserving logical ordering of input and output. */
5520 if (XMARKER (p
->mark
)->buffer
)
5521 SET_PT_BOTH (clip_to_bounds (BEGV
, marker_position (p
->mark
), ZV
),
5522 clip_to_bounds (BEGV_BYTE
, marker_byte_position (p
->mark
),
5525 SET_PT_BOTH (ZV
, ZV_BYTE
);
5527 before_byte
= PT_BYTE
;
5529 /* If the output marker is outside of the visible region, save
5530 the restriction and widen. */
5531 if (! (BEGV
<= PT
&& PT
<= ZV
))
5534 decode_coding_c_string (coding
, chars
, nbytes
, Qt
);
5535 text
= coding
->dst_object
;
5536 Vlast_coding_system_used
= CODING_ID_NAME (coding
->id
);
5537 /* A new coding system might be found. See the comment in the
5538 similar code in the previous `if' block. */
5539 if (!EQ (p
->decode_coding_system
, Vlast_coding_system_used
))
5541 p
->decode_coding_system
= Vlast_coding_system_used
;
5542 if (NILP (p
->encode_coding_system
)
5543 && proc_encode_coding_system
[p
->outfd
])
5545 p
->encode_coding_system
5546 = coding_inherit_eol_type (Vlast_coding_system_used
, Qnil
);
5547 setup_coding_system (p
->encode_coding_system
,
5548 proc_encode_coding_system
[p
->outfd
]);
5551 if (coding
->carryover_bytes
> 0)
5553 if (SCHARS (p
->decoding_buf
) < coding
->carryover_bytes
)
5554 p
->decoding_buf
= make_uninit_string (coding
->carryover_bytes
);
5555 bcopy (coding
->carryover
, SDATA (p
->decoding_buf
),
5556 coding
->carryover_bytes
);
5557 p
->decoding_carryover
= coding
->carryover_bytes
;
5559 /* Adjust the multibyteness of TEXT to that of the buffer. */
5560 if (NILP (current_buffer
->enable_multibyte_characters
)
5561 != ! STRING_MULTIBYTE (text
))
5562 text
= (STRING_MULTIBYTE (text
)
5563 ? Fstring_as_unibyte (text
)
5564 : Fstring_to_multibyte (text
));
5565 /* Insert before markers in case we are inserting where
5566 the buffer's mark is, and the user's next command is Meta-y. */
5567 insert_from_string_before_markers (text
, 0, 0,
5568 SCHARS (text
), SBYTES (text
), 0);
5570 /* Make sure the process marker's position is valid when the
5571 process buffer is changed in the signal_after_change above.
5572 W3 is known to do that. */
5573 if (BUFFERP (p
->buffer
)
5574 && (b
= XBUFFER (p
->buffer
), b
!= current_buffer
))
5575 set_marker_both (p
->mark
, p
->buffer
, BUF_PT (b
), BUF_PT_BYTE (b
));
5577 set_marker_both (p
->mark
, p
->buffer
, PT
, PT_BYTE
);
5579 update_mode_lines
++;
5581 /* Make sure opoint and the old restrictions
5582 float ahead of any new text just as point would. */
5583 if (opoint
>= before
)
5585 opoint
+= PT
- before
;
5586 opoint_byte
+= PT_BYTE
- before_byte
;
5588 if (old_begv
> before
)
5590 old_begv
+= PT
- before
;
5591 old_begv_byte
+= PT_BYTE
- before_byte
;
5593 if (old_zv
>= before
)
5595 old_zv
+= PT
- before
;
5596 old_zv_byte
+= PT_BYTE
- before_byte
;
5599 /* If the restriction isn't what it should be, set it. */
5600 if (old_begv
!= BEGV
|| old_zv
!= ZV
)
5601 Fnarrow_to_region (make_number (old_begv
), make_number (old_zv
));
5603 /* Handling the process output should not deactivate the mark. */
5604 Vdeactivate_mark
= odeactivate
;
5606 current_buffer
->read_only
= old_read_only
;
5607 SET_PT_BOTH (opoint
, opoint_byte
);
5608 unbind_to (count
, Qnil
);
5613 DEFUN ("waiting-for-user-input-p", Fwaiting_for_user_input_p
, Swaiting_for_user_input_p
,
5615 doc
: /* Returns non-nil if Emacs is waiting for input from the user.
5616 This is intended for use by asynchronous process output filters and sentinels. */)
5619 return (waiting_for_user_input_p
? Qt
: Qnil
);
5622 /* Sending data to subprocess */
5624 jmp_buf send_process_frame
;
5625 Lisp_Object process_sent_to
;
5628 send_process_trap ()
5630 SIGNAL_THREAD_CHECK (SIGPIPE
);
5631 sigunblock (sigmask (SIGPIPE
));
5632 longjmp (send_process_frame
, 1);
5635 /* Send some data to process PROC.
5636 BUF is the beginning of the data; LEN is the number of characters.
5637 OBJECT is the Lisp object that the data comes from. If OBJECT is
5638 nil or t, it means that the data comes from C string.
5640 If OBJECT is not nil, the data is encoded by PROC's coding-system
5641 for encoding before it is sent.
5643 This function can evaluate Lisp code and can garbage collect. */
5646 send_process (proc
, buf
, len
, object
)
5647 volatile Lisp_Object proc
;
5648 unsigned char *volatile buf
;
5650 volatile Lisp_Object object
;
5652 /* Use volatile to protect variables from being clobbered by longjmp. */
5653 struct Lisp_Process
*p
= XPROCESS (proc
);
5655 struct coding_system
*coding
;
5656 struct gcpro gcpro1
;
5657 SIGTYPE (*volatile old_sigpipe
) ();
5661 if (p
->raw_status_new
)
5663 if (! EQ (p
->status
, Qrun
))
5664 error ("Process %s not running", SDATA (p
->name
));
5666 error ("Output file descriptor of %s is closed", SDATA (p
->name
));
5668 coding
= proc_encode_coding_system
[p
->outfd
];
5669 Vlast_coding_system_used
= CODING_ID_NAME (coding
->id
);
5671 if ((STRINGP (object
) && STRING_MULTIBYTE (object
))
5672 || (BUFFERP (object
)
5673 && !NILP (XBUFFER (object
)->enable_multibyte_characters
))
5676 if (!EQ (Vlast_coding_system_used
, p
->encode_coding_system
))
5677 /* The coding system for encoding was changed to raw-text
5678 because we sent a unibyte text previously. Now we are
5679 sending a multibyte text, thus we must encode it by the
5680 original coding system specified for the current process. */
5681 setup_coding_system (p
->encode_coding_system
, coding
);
5682 coding
->src_multibyte
= 1;
5686 /* For sending a unibyte text, character code conversion should
5687 not take place but EOL conversion should. So, setup raw-text
5688 or one of the subsidiary if we have not yet done it. */
5689 if (CODING_REQUIRE_ENCODING (coding
))
5691 if (CODING_REQUIRE_FLUSHING (coding
))
5693 /* But, before changing the coding, we must flush out data. */
5694 coding
->mode
|= CODING_MODE_LAST_BLOCK
;
5695 send_process (proc
, "", 0, Qt
);
5696 coding
->mode
&= CODING_MODE_LAST_BLOCK
;
5698 setup_coding_system (raw_text_coding_system
5699 (Vlast_coding_system_used
),
5701 coding
->src_multibyte
= 0;
5704 coding
->dst_multibyte
= 0;
5706 if (CODING_REQUIRE_ENCODING (coding
))
5708 coding
->dst_object
= Qt
;
5709 if (BUFFERP (object
))
5711 int from_byte
, from
, to
;
5712 int save_pt
, save_pt_byte
;
5713 struct buffer
*cur
= current_buffer
;
5715 set_buffer_internal (XBUFFER (object
));
5716 save_pt
= PT
, save_pt_byte
= PT_BYTE
;
5718 from_byte
= PTR_BYTE_POS (buf
);
5719 from
= BYTE_TO_CHAR (from_byte
);
5720 to
= BYTE_TO_CHAR (from_byte
+ len
);
5721 TEMP_SET_PT_BOTH (from
, from_byte
);
5722 encode_coding_object (coding
, object
, from
, from_byte
,
5723 to
, from_byte
+ len
, Qt
);
5724 TEMP_SET_PT_BOTH (save_pt
, save_pt_byte
);
5725 set_buffer_internal (cur
);
5727 else if (STRINGP (object
))
5729 encode_coding_object (coding
, object
, 0, 0, SCHARS (object
),
5730 SBYTES (object
), Qt
);
5734 coding
->dst_object
= make_unibyte_string (buf
, len
);
5735 coding
->produced
= len
;
5738 len
= coding
->produced
;
5739 object
= coding
->dst_object
;
5740 buf
= SDATA (object
);
5743 if (pty_max_bytes
== 0)
5745 #if defined (HAVE_FPATHCONF) && defined (_PC_MAX_CANON)
5746 pty_max_bytes
= fpathconf (p
->outfd
, _PC_MAX_CANON
);
5747 if (pty_max_bytes
< 0)
5748 pty_max_bytes
= 250;
5750 pty_max_bytes
= 250;
5752 /* Deduct one, to leave space for the eof. */
5756 /* 2000-09-21: Emacs 20.7, sparc-sun-solaris-2.6, GCC 2.95.2,
5757 CFLAGS="-g -O": The value of the parameter `proc' is clobbered
5758 when returning with longjmp despite being declared volatile. */
5759 if (!setjmp (send_process_frame
))
5761 process_sent_to
= proc
;
5766 /* Decide how much data we can send in one batch.
5767 Long lines need to be split into multiple batches. */
5770 /* Starting this at zero is always correct when not the first
5771 iteration because the previous iteration ended by sending C-d.
5772 It may not be correct for the first iteration
5773 if a partial line was sent in a separate send_process call.
5774 If that proves worth handling, we need to save linepos
5775 in the process object. */
5777 unsigned char *ptr
= (unsigned char *) buf
;
5778 unsigned char *end
= (unsigned char *) buf
+ len
;
5780 /* Scan through this text for a line that is too long. */
5781 while (ptr
!= end
&& linepos
< pty_max_bytes
)
5789 /* If we found one, break the line there
5790 and put in a C-d to force the buffer through. */
5794 /* Send this batch, using one or more write calls. */
5797 int outfd
= p
->outfd
;
5798 old_sigpipe
= (SIGTYPE (*) ()) signal (SIGPIPE
, send_process_trap
);
5799 #ifdef DATAGRAM_SOCKETS
5800 if (DATAGRAM_CHAN_P (outfd
))
5802 rv
= sendto (outfd
, (char *) buf
, this,
5803 0, datagram_address
[outfd
].sa
,
5804 datagram_address
[outfd
].len
);
5805 if (rv
< 0 && errno
== EMSGSIZE
)
5807 signal (SIGPIPE
, old_sigpipe
);
5808 report_file_error ("sending datagram",
5809 Fcons (proc
, Qnil
));
5815 rv
= emacs_write (outfd
, (char *) buf
, this);
5816 #ifdef ADAPTIVE_READ_BUFFERING
5817 if (p
->read_output_delay
> 0
5818 && p
->adaptive_read_buffering
== 1)
5820 p
->read_output_delay
= 0;
5821 process_output_delay_count
--;
5822 p
->read_output_skip
= 0;
5826 signal (SIGPIPE
, old_sigpipe
);
5832 || errno
== EWOULDBLOCK
5838 /* Buffer is full. Wait, accepting input;
5839 that may allow the program
5840 to finish doing output and read more. */
5844 #ifdef BROKEN_PTY_READ_AFTER_EAGAIN
5845 /* A gross hack to work around a bug in FreeBSD.
5846 In the following sequence, read(2) returns
5850 write(2) 954 bytes, get EAGAIN
5851 read(2) 1024 bytes in process_read_output
5852 read(2) 11 bytes in process_read_output
5854 That is, read(2) returns more bytes than have
5855 ever been written successfully. The 1033 bytes
5856 read are the 1022 bytes written successfully
5857 after processing (for example with CRs added if
5858 the terminal is set up that way which it is
5859 here). The same bytes will be seen again in a
5860 later read(2), without the CRs. */
5862 if (errno
== EAGAIN
)
5865 ioctl (p
->outfd
, TIOCFLUSH
, &flags
);
5867 #endif /* BROKEN_PTY_READ_AFTER_EAGAIN */
5869 /* Running filters might relocate buffers or strings.
5870 Arrange to relocate BUF. */
5871 if (BUFFERP (object
))
5872 offset
= BUF_PTR_BYTE_POS (XBUFFER (object
), buf
);
5873 else if (STRINGP (object
))
5874 offset
= buf
- SDATA (object
);
5876 #ifdef EMACS_HAS_USECS
5877 wait_reading_process_output (0, 20000, 0, 0, Qnil
, NULL
, 0);
5879 wait_reading_process_output (1, 0, 0, 0, Qnil
, NULL
, 0);
5882 if (BUFFERP (object
))
5883 buf
= BUF_BYTE_ADDRESS (XBUFFER (object
), offset
);
5884 else if (STRINGP (object
))
5885 buf
= offset
+ SDATA (object
);
5890 /* This is a real error. */
5891 report_file_error ("writing to process", Fcons (proc
, Qnil
));
5898 /* If we sent just part of the string, put in an EOF (C-d)
5899 to force it through, before we send the rest. */
5901 Fprocess_send_eof (proc
);
5906 signal (SIGPIPE
, old_sigpipe
);
5907 proc
= process_sent_to
;
5908 p
= XPROCESS (proc
);
5909 p
->raw_status_new
= 0;
5910 p
->status
= Fcons (Qexit
, Fcons (make_number (256), Qnil
));
5911 p
->tick
= ++process_tick
;
5912 deactivate_process (proc
);
5913 error ("SIGPIPE raised on process %s; closed it", SDATA (p
->name
));
5919 DEFUN ("process-send-region", Fprocess_send_region
, Sprocess_send_region
,
5921 doc
: /* Send current contents of region as input to PROCESS.
5922 PROCESS may be a process, a buffer, the name of a process or buffer, or
5923 nil, indicating the current buffer's process.
5924 Called from program, takes three arguments, PROCESS, START and END.
5925 If the region is more than 500 characters long,
5926 it is sent in several bunches. This may happen even for shorter regions.
5927 Output from processes can arrive in between bunches. */)
5928 (process
, start
, end
)
5929 Lisp_Object process
, start
, end
;
5934 proc
= get_process (process
);
5935 validate_region (&start
, &end
);
5937 if (XINT (start
) < GPT
&& XINT (end
) > GPT
)
5938 move_gap (XINT (start
));
5940 start1
= CHAR_TO_BYTE (XINT (start
));
5941 end1
= CHAR_TO_BYTE (XINT (end
));
5942 send_process (proc
, BYTE_POS_ADDR (start1
), end1
- start1
,
5943 Fcurrent_buffer ());
5948 DEFUN ("process-send-string", Fprocess_send_string
, Sprocess_send_string
,
5950 doc
: /* Send PROCESS the contents of STRING as input.
5951 PROCESS may be a process, a buffer, the name of a process or buffer, or
5952 nil, indicating the current buffer's process.
5953 If STRING is more than 500 characters long,
5954 it is sent in several bunches. This may happen even for shorter strings.
5955 Output from processes can arrive in between bunches. */)
5957 Lisp_Object process
, string
;
5960 CHECK_STRING (string
);
5961 proc
= get_process (process
);
5962 send_process (proc
, SDATA (string
),
5963 SBYTES (string
), string
);
5967 /* Return the foreground process group for the tty/pty that
5968 the process P uses. */
5970 emacs_get_tty_pgrp (p
)
5971 struct Lisp_Process
*p
;
5976 if (ioctl (p
->infd
, TIOCGPGRP
, &gid
) == -1 && ! NILP (p
->tty_name
))
5979 /* Some OS:es (Solaris 8/9) does not allow TIOCGPGRP from the
5980 master side. Try the slave side. */
5981 fd
= emacs_open (SDATA (p
->tty_name
), O_RDONLY
, 0);
5985 ioctl (fd
, TIOCGPGRP
, &gid
);
5989 #endif /* defined (TIOCGPGRP ) */
5994 DEFUN ("process-running-child-p", Fprocess_running_child_p
,
5995 Sprocess_running_child_p
, 0, 1, 0,
5996 doc
: /* Return t if PROCESS has given the terminal to a child.
5997 If the operating system does not make it possible to find out,
5998 return t unconditionally. */)
6000 Lisp_Object process
;
6002 /* Initialize in case ioctl doesn't exist or gives an error,
6003 in a way that will cause returning t. */
6006 struct Lisp_Process
*p
;
6008 proc
= get_process (process
);
6009 p
= XPROCESS (proc
);
6011 if (!EQ (p
->type
, Qreal
))
6012 error ("Process %s is not a subprocess",
6015 error ("Process %s is not active",
6018 gid
= emacs_get_tty_pgrp (p
);
6025 /* send a signal number SIGNO to PROCESS.
6026 If CURRENT_GROUP is t, that means send to the process group
6027 that currently owns the terminal being used to communicate with PROCESS.
6028 This is used for various commands in shell mode.
6029 If CURRENT_GROUP is lambda, that means send to the process group
6030 that currently owns the terminal, but only if it is NOT the shell itself.
6032 If NOMSG is zero, insert signal-announcements into process's buffers
6035 If we can, we try to signal PROCESS by sending control characters
6036 down the pty. This allows us to signal inferiors who have changed
6037 their uid, for which killpg would return an EPERM error. */
6040 process_send_signal (process
, signo
, current_group
, nomsg
)
6041 Lisp_Object process
;
6043 Lisp_Object current_group
;
6047 register struct Lisp_Process
*p
;
6051 proc
= get_process (process
);
6052 p
= XPROCESS (proc
);
6054 if (!EQ (p
->type
, Qreal
))
6055 error ("Process %s is not a subprocess",
6058 error ("Process %s is not active",
6062 current_group
= Qnil
;
6064 /* If we are using pgrps, get a pgrp number and make it negative. */
6065 if (NILP (current_group
))
6066 /* Send the signal to the shell's process group. */
6070 #ifdef SIGNALS_VIA_CHARACTERS
6071 /* If possible, send signals to the entire pgrp
6072 by sending an input character to it. */
6074 /* TERMIOS is the latest and bestest, and seems most likely to
6075 work. If the system has it, use it. */
6078 cc_t
*sig_char
= NULL
;
6080 tcgetattr (p
->infd
, &t
);
6085 sig_char
= &t
.c_cc
[VINTR
];
6089 sig_char
= &t
.c_cc
[VQUIT
];
6093 #if defined (VSWTCH) && !defined (PREFER_VSUSP)
6094 sig_char
= &t
.c_cc
[VSWTCH
];
6096 sig_char
= &t
.c_cc
[VSUSP
];
6101 if (sig_char
&& *sig_char
!= CDISABLE
)
6103 send_process (proc
, sig_char
, 1, Qnil
);
6106 /* If we can't send the signal with a character,
6107 fall through and send it another way. */
6108 #else /* ! HAVE_TERMIOS */
6110 /* On Berkeley descendants, the following IOCTL's retrieve the
6111 current control characters. */
6112 #if defined (TIOCGLTC) && defined (TIOCGETC)
6120 ioctl (p
->infd
, TIOCGETC
, &c
);
6121 send_process (proc
, &c
.t_intrc
, 1, Qnil
);
6124 ioctl (p
->infd
, TIOCGETC
, &c
);
6125 send_process (proc
, &c
.t_quitc
, 1, Qnil
);
6129 ioctl (p
->infd
, TIOCGLTC
, &lc
);
6130 send_process (proc
, &lc
.t_suspc
, 1, Qnil
);
6132 #endif /* ! defined (SIGTSTP) */
6135 #else /* ! defined (TIOCGLTC) && defined (TIOCGETC) */
6137 /* On SYSV descendants, the TCGETA ioctl retrieves the current control
6144 ioctl (p
->infd
, TCGETA
, &t
);
6145 send_process (proc
, &t
.c_cc
[VINTR
], 1, Qnil
);
6148 ioctl (p
->infd
, TCGETA
, &t
);
6149 send_process (proc
, &t
.c_cc
[VQUIT
], 1, Qnil
);
6153 ioctl (p
->infd
, TCGETA
, &t
);
6154 send_process (proc
, &t
.c_cc
[VSWTCH
], 1, Qnil
);
6156 #endif /* ! defined (SIGTSTP) */
6158 #else /* ! defined (TCGETA) */
6159 Your configuration files are messed up
.
6160 /* If your system configuration files define SIGNALS_VIA_CHARACTERS,
6161 you'd better be using one of the alternatives above! */
6162 #endif /* ! defined (TCGETA) */
6163 #endif /* ! defined (TIOCGLTC) && defined (TIOCGETC) */
6164 /* In this case, the code above should alway return. */
6166 #endif /* ! defined HAVE_TERMIOS */
6168 /* The code above may fall through if it can't
6169 handle the signal. */
6170 #endif /* defined (SIGNALS_VIA_CHARACTERS) */
6173 /* Get the current pgrp using the tty itself, if we have that.
6174 Otherwise, use the pty to get the pgrp.
6175 On pfa systems, saka@pfu.fujitsu.co.JP writes:
6176 "TIOCGPGRP symbol defined in sys/ioctl.h at E50.
6177 But, TIOCGPGRP does not work on E50 ;-P works fine on E60"
6178 His patch indicates that if TIOCGPGRP returns an error, then
6179 we should just assume that p->pid is also the process group id. */
6181 gid
= emacs_get_tty_pgrp (p
);
6184 /* If we can't get the information, assume
6185 the shell owns the tty. */
6188 /* It is not clear whether anything really can set GID to -1.
6189 Perhaps on some system one of those ioctls can or could do so.
6190 Or perhaps this is vestigial. */
6193 #else /* ! defined (TIOCGPGRP ) */
6194 /* Can't select pgrps on this system, so we know that
6195 the child itself heads the pgrp. */
6197 #endif /* ! defined (TIOCGPGRP ) */
6199 /* If current_group is lambda, and the shell owns the terminal,
6200 don't send any signal. */
6201 if (EQ (current_group
, Qlambda
) && gid
== p
->pid
)
6209 p
->raw_status_new
= 0;
6211 p
->tick
= ++process_tick
;
6214 status_notify (NULL
);
6215 redisplay_preserve_echo_area (13);
6218 #endif /* ! defined (SIGCONT) */
6222 flush_pending_output (p
->infd
);
6226 /* If we don't have process groups, send the signal to the immediate
6227 subprocess. That isn't really right, but it's better than any
6228 obvious alternative. */
6231 kill (p
->pid
, signo
);
6235 /* gid may be a pid, or minus a pgrp's number */
6237 if (!NILP (current_group
))
6239 if (ioctl (p
->infd
, TIOCSIGSEND
, signo
) == -1)
6240 EMACS_KILLPG (gid
, signo
);
6247 #else /* ! defined (TIOCSIGSEND) */
6248 EMACS_KILLPG (gid
, signo
);
6249 #endif /* ! defined (TIOCSIGSEND) */
6252 DEFUN ("interrupt-process", Finterrupt_process
, Sinterrupt_process
, 0, 2, 0,
6253 doc
: /* Interrupt process PROCESS.
6254 PROCESS may be a process, a buffer, or the name of a process or buffer.
6255 No arg or nil means current buffer's process.
6256 Second arg CURRENT-GROUP non-nil means send signal to
6257 the current process-group of the process's controlling terminal
6258 rather than to the process's own process group.
6259 If the process is a shell, this means interrupt current subjob
6260 rather than the shell.
6262 If CURRENT-GROUP is `lambda', and if the shell owns the terminal,
6263 don't send the signal. */)
6264 (process
, current_group
)
6265 Lisp_Object process
, current_group
;
6267 process_send_signal (process
, SIGINT
, current_group
, 0);
6271 DEFUN ("kill-process", Fkill_process
, Skill_process
, 0, 2, 0,
6272 doc
: /* Kill process PROCESS. May be process or name of one.
6273 See function `interrupt-process' for more details on usage. */)
6274 (process
, current_group
)
6275 Lisp_Object process
, current_group
;
6277 process_send_signal (process
, SIGKILL
, current_group
, 0);
6281 DEFUN ("quit-process", Fquit_process
, Squit_process
, 0, 2, 0,
6282 doc
: /* Send QUIT signal to process PROCESS. May be process or name of one.
6283 See function `interrupt-process' for more details on usage. */)
6284 (process
, current_group
)
6285 Lisp_Object process
, current_group
;
6287 process_send_signal (process
, SIGQUIT
, current_group
, 0);
6291 DEFUN ("stop-process", Fstop_process
, Sstop_process
, 0, 2, 0,
6292 doc
: /* Stop process PROCESS. May be process or name of one.
6293 See function `interrupt-process' for more details on usage.
6294 If PROCESS is a network or serial process, inhibit handling of incoming
6296 (process
, current_group
)
6297 Lisp_Object process
, current_group
;
6300 if (PROCESSP (process
) && (NETCONN_P (process
) || SERIALCONN_P (process
)))
6302 struct Lisp_Process
*p
;
6304 p
= XPROCESS (process
);
6305 if (NILP (p
->command
)
6308 FD_CLR (p
->infd
, &input_wait_mask
);
6309 FD_CLR (p
->infd
, &non_keyboard_wait_mask
);
6316 error ("No SIGTSTP support");
6318 process_send_signal (process
, SIGTSTP
, current_group
, 0);
6323 DEFUN ("continue-process", Fcontinue_process
, Scontinue_process
, 0, 2, 0,
6324 doc
: /* Continue process PROCESS. May be process or name of one.
6325 See function `interrupt-process' for more details on usage.
6326 If PROCESS is a network or serial process, resume handling of incoming
6328 (process
, current_group
)
6329 Lisp_Object process
, current_group
;
6332 if (PROCESSP (process
) && (NETCONN_P (process
) || SERIALCONN_P (process
)))
6334 struct Lisp_Process
*p
;
6336 p
= XPROCESS (process
);
6337 if (EQ (p
->command
, Qt
)
6339 && (!EQ (p
->filter
, Qt
) || EQ (p
->status
, Qlisten
)))
6341 FD_SET (p
->infd
, &input_wait_mask
);
6342 FD_SET (p
->infd
, &non_keyboard_wait_mask
);
6344 if (fd_info
[ p
->infd
].flags
& FILE_SERIAL
)
6345 PurgeComm (fd_info
[ p
->infd
].hnd
, PURGE_RXABORT
| PURGE_RXCLEAR
);
6348 tcflush (p
->infd
, TCIFLUSH
);
6356 process_send_signal (process
, SIGCONT
, current_group
, 0);
6358 error ("No SIGCONT support");
6363 DEFUN ("signal-process", Fsignal_process
, Ssignal_process
,
6364 2, 2, "sProcess (name or number): \nnSignal code: ",
6365 doc
: /* Send PROCESS the signal with code SIGCODE.
6366 PROCESS may also be a number specifying the process id of the
6367 process to signal; in this case, the process need not be a child of
6369 SIGCODE may be an integer, or a symbol whose name is a signal name. */)
6371 Lisp_Object process
, sigcode
;
6375 if (INTEGERP (process
))
6377 pid
= XINT (process
);
6381 if (FLOATP (process
))
6383 pid
= (pid_t
) XFLOAT_DATA (process
);
6387 if (STRINGP (process
))
6390 if (tem
= Fget_process (process
), NILP (tem
))
6392 pid
= XINT (Fstring_to_number (process
, make_number (10)));
6399 process
= get_process (process
);
6404 CHECK_PROCESS (process
);
6405 pid
= XPROCESS (process
)->pid
;
6407 error ("Cannot signal process %s", SDATA (XPROCESS (process
)->name
));
6411 #define parse_signal(NAME, VALUE) \
6412 else if (!xstrcasecmp (name, NAME)) \
6413 XSETINT (sigcode, VALUE)
6415 if (INTEGERP (sigcode
))
6419 unsigned char *name
;
6421 CHECK_SYMBOL (sigcode
);
6422 name
= SDATA (SYMBOL_NAME (sigcode
));
6424 if (!strncmp(name
, "SIG", 3) || !strncmp(name
, "sig", 3))
6430 parse_signal ("usr1", SIGUSR1
);
6433 parse_signal ("usr2", SIGUSR2
);
6436 parse_signal ("term", SIGTERM
);
6439 parse_signal ("hup", SIGHUP
);
6442 parse_signal ("int", SIGINT
);
6445 parse_signal ("quit", SIGQUIT
);
6448 parse_signal ("ill", SIGILL
);
6451 parse_signal ("abrt", SIGABRT
);
6454 parse_signal ("emt", SIGEMT
);
6457 parse_signal ("kill", SIGKILL
);
6460 parse_signal ("fpe", SIGFPE
);
6463 parse_signal ("bus", SIGBUS
);
6466 parse_signal ("segv", SIGSEGV
);
6469 parse_signal ("sys", SIGSYS
);
6472 parse_signal ("pipe", SIGPIPE
);
6475 parse_signal ("alrm", SIGALRM
);
6478 parse_signal ("urg", SIGURG
);
6481 parse_signal ("stop", SIGSTOP
);
6484 parse_signal ("tstp", SIGTSTP
);
6487 parse_signal ("cont", SIGCONT
);
6490 parse_signal ("chld", SIGCHLD
);
6493 parse_signal ("ttin", SIGTTIN
);
6496 parse_signal ("ttou", SIGTTOU
);
6499 parse_signal ("io", SIGIO
);
6502 parse_signal ("xcpu", SIGXCPU
);
6505 parse_signal ("xfsz", SIGXFSZ
);
6508 parse_signal ("vtalrm", SIGVTALRM
);
6511 parse_signal ("prof", SIGPROF
);
6514 parse_signal ("winch", SIGWINCH
);
6517 parse_signal ("info", SIGINFO
);
6520 error ("Undefined signal name %s", name
);
6525 return make_number (kill (pid
, XINT (sigcode
)));
6528 DEFUN ("process-send-eof", Fprocess_send_eof
, Sprocess_send_eof
, 0, 1, 0,
6529 doc
: /* Make PROCESS see end-of-file in its input.
6530 EOF comes after any text already sent to it.
6531 PROCESS may be a process, a buffer, the name of a process or buffer, or
6532 nil, indicating the current buffer's process.
6533 If PROCESS is a network connection, or is a process communicating
6534 through a pipe (as opposed to a pty), then you cannot send any more
6535 text to PROCESS after you call this function.
6536 If PROCESS is a serial process, wait until all output written to the
6537 process has been transmitted to the serial port. */)
6539 Lisp_Object process
;
6542 struct coding_system
*coding
;
6544 if (DATAGRAM_CONN_P (process
))
6547 proc
= get_process (process
);
6548 coding
= proc_encode_coding_system
[XPROCESS (proc
)->outfd
];
6550 /* Make sure the process is really alive. */
6551 if (XPROCESS (proc
)->raw_status_new
)
6552 update_status (XPROCESS (proc
));
6553 if (! EQ (XPROCESS (proc
)->status
, Qrun
))
6554 error ("Process %s not running", SDATA (XPROCESS (proc
)->name
));
6556 if (CODING_REQUIRE_FLUSHING (coding
))
6558 coding
->mode
|= CODING_MODE_LAST_BLOCK
;
6559 send_process (proc
, "", 0, Qnil
);
6562 if (XPROCESS (proc
)->pty_flag
)
6563 send_process (proc
, "\004", 1, Qnil
);
6564 else if (EQ (XPROCESS (proc
)->type
, Qserial
))
6567 if (tcdrain (XPROCESS (proc
)->outfd
) != 0)
6568 error ("tcdrain() failed: %s", emacs_strerror (errno
));
6570 /* Do nothing on Windows because writes are blocking. */
6574 int old_outfd
, new_outfd
;
6576 #ifdef HAVE_SHUTDOWN
6577 /* If this is a network connection, or socketpair is used
6578 for communication with the subprocess, call shutdown to cause EOF.
6579 (In some old system, shutdown to socketpair doesn't work.
6580 Then we just can't win.) */
6581 if (EQ (XPROCESS (proc
)->type
, Qnetwork
)
6582 || XPROCESS (proc
)->outfd
== XPROCESS (proc
)->infd
)
6583 shutdown (XPROCESS (proc
)->outfd
, 1);
6584 /* In case of socketpair, outfd == infd, so don't close it. */
6585 if (XPROCESS (proc
)->outfd
!= XPROCESS (proc
)->infd
)
6586 emacs_close (XPROCESS (proc
)->outfd
);
6587 #else /* not HAVE_SHUTDOWN */
6588 emacs_close (XPROCESS (proc
)->outfd
);
6589 #endif /* not HAVE_SHUTDOWN */
6590 new_outfd
= emacs_open (NULL_DEVICE
, O_WRONLY
, 0);
6593 old_outfd
= XPROCESS (proc
)->outfd
;
6595 if (!proc_encode_coding_system
[new_outfd
])
6596 proc_encode_coding_system
[new_outfd
]
6597 = (struct coding_system
*) xmalloc (sizeof (struct coding_system
));
6598 bcopy (proc_encode_coding_system
[old_outfd
],
6599 proc_encode_coding_system
[new_outfd
],
6600 sizeof (struct coding_system
));
6601 bzero (proc_encode_coding_system
[old_outfd
],
6602 sizeof (struct coding_system
));
6604 XPROCESS (proc
)->outfd
= new_outfd
;
6609 /* Kill all processes associated with `buffer'.
6610 If `buffer' is nil, kill all processes */
6613 kill_buffer_processes (buffer
)
6616 Lisp_Object tail
, proc
;
6618 for (tail
= Vprocess_alist
; CONSP (tail
); tail
= XCDR (tail
))
6620 proc
= XCDR (XCAR (tail
));
6622 && (NILP (buffer
) || EQ (XPROCESS (proc
)->buffer
, buffer
)))
6624 if (NETCONN_P (proc
) || SERIALCONN_P (proc
))
6625 Fdelete_process (proc
);
6626 else if (XPROCESS (proc
)->infd
>= 0)
6627 process_send_signal (proc
, SIGHUP
, Qnil
, 1);
6632 /* On receipt of a signal that a child status has changed, loop asking
6633 about children with changed statuses until the system says there
6636 All we do is change the status; we do not run sentinels or print
6637 notifications. That is saved for the next time keyboard input is
6638 done, in order to avoid timing errors.
6640 ** WARNING: this can be called during garbage collection.
6641 Therefore, it must not be fooled by the presence of mark bits in
6644 ** USG WARNING: Although it is not obvious from the documentation
6645 in signal(2), on a USG system the SIGCLD handler MUST NOT call
6646 signal() before executing at least one wait(), otherwise the
6647 handler will be called again, resulting in an infinite loop. The
6648 relevant portion of the documentation reads "SIGCLD signals will be
6649 queued and the signal-catching function will be continually
6650 reentered until the queue is empty". Invoking signal() causes the
6651 kernel to reexamine the SIGCLD queue. Fred Fish, UniSoft Systems
6654 ** Malloc WARNING: This should never call malloc either directly or
6655 indirectly; if it does, that is a bug */
6659 sigchld_handler (signo
)
6662 int old_errno
= errno
;
6664 register struct Lisp_Process
*p
;
6665 extern EMACS_TIME
*input_available_clear_time
;
6667 SIGNAL_THREAD_CHECK (signo
);
6678 #endif /* no WUNTRACED */
6679 /* Keep trying to get a status until we get a definitive result. */
6683 pid
= wait3 (&w
, WNOHANG
| WUNTRACED
, 0);
6685 while (pid
< 0 && errno
== EINTR
);
6689 /* PID == 0 means no processes found, PID == -1 means a real
6690 failure. We have done all our job, so return. */
6692 /* USG systems forget handlers when they are used;
6693 must reestablish each time */
6694 #if defined (USG) && !defined (POSIX_SIGNALS)
6695 signal (signo
, sigchld_handler
); /* WARNING - must come after wait3() */
6702 #endif /* no WNOHANG */
6704 /* Find the process that signaled us, and record its status. */
6706 /* The process can have been deleted by Fdelete_process. */
6707 for (tail
= deleted_pid_list
; CONSP (tail
); tail
= XCDR (tail
))
6709 Lisp_Object xpid
= XCAR (tail
);
6710 if ((INTEGERP (xpid
) && pid
== (pid_t
) XINT (xpid
))
6711 || (FLOATP (xpid
) && pid
== (pid_t
) XFLOAT_DATA (xpid
)))
6713 XSETCAR (tail
, Qnil
);
6714 goto sigchld_end_of_loop
;
6718 /* Otherwise, if it is asynchronous, it is in Vprocess_alist. */
6720 for (tail
= Vprocess_alist
; CONSP (tail
); tail
= XCDR (tail
))
6722 proc
= XCDR (XCAR (tail
));
6723 p
= XPROCESS (proc
);
6724 if (EQ (p
->type
, Qreal
) && p
->pid
== pid
)
6729 /* Look for an asynchronous process whose pid hasn't been filled
6732 for (tail
= Vprocess_alist
; CONSP (tail
); tail
= XCDR (tail
))
6734 proc
= XCDR (XCAR (tail
));
6735 p
= XPROCESS (proc
);
6741 /* Change the status of the process that was found. */
6744 int clear_desc_flag
= 0;
6746 p
->tick
= ++process_tick
;
6748 p
->raw_status_new
= 1;
6750 /* If process has terminated, stop waiting for its output. */
6751 if ((WIFSIGNALED (w
) || WIFEXITED (w
))
6753 clear_desc_flag
= 1;
6755 /* We use clear_desc_flag to avoid a compiler bug in Microsoft C. */
6756 if (clear_desc_flag
)
6758 FD_CLR (p
->infd
, &input_wait_mask
);
6759 FD_CLR (p
->infd
, &non_keyboard_wait_mask
);
6762 /* Tell wait_reading_process_output that it needs to wake up and
6764 if (input_available_clear_time
)
6765 EMACS_SET_SECS_USECS (*input_available_clear_time
, 0, 0);
6768 /* There was no asynchronous process found for that pid: we have
6769 a synchronous process. */
6772 synch_process_alive
= 0;
6774 /* Report the status of the synchronous process. */
6776 synch_process_retcode
= WRETCODE (w
);
6777 else if (WIFSIGNALED (w
))
6778 synch_process_termsig
= WTERMSIG (w
);
6780 /* Tell wait_reading_process_output that it needs to wake up and
6782 if (input_available_clear_time
)
6783 EMACS_SET_SECS_USECS (*input_available_clear_time
, 0, 0);
6786 sigchld_end_of_loop
:
6789 /* On some systems, we must return right away.
6790 If any more processes want to signal us, we will
6792 Otherwise (on systems that have WNOHANG), loop around
6793 to use up all the processes that have something to tell us. */
6794 #if (defined WINDOWSNT \
6795 || (defined USG && !defined GNU_LINUX \
6796 && !(defined HPUX && defined WNOHANG)))
6797 #if defined (USG) && ! defined (POSIX_SIGNALS)
6798 signal (signo
, sigchld_handler
);
6802 #endif /* USG, but not HPUX with WNOHANG */
6805 #endif /* SIGCHLD */
6809 exec_sentinel_unwind (data
)
6812 XPROCESS (XCAR (data
))->sentinel
= XCDR (data
);
6817 exec_sentinel_error_handler (error
)
6820 cmd_error_internal (error
, "error in process sentinel: ");
6822 update_echo_area ();
6823 Fsleep_for (make_number (2), Qnil
);
6828 exec_sentinel (proc
, reason
)
6829 Lisp_Object proc
, reason
;
6831 Lisp_Object sentinel
, obuffer
, odeactivate
, okeymap
;
6832 register struct Lisp_Process
*p
= XPROCESS (proc
);
6833 int count
= SPECPDL_INDEX ();
6834 int outer_running_asynch_code
= running_asynch_code
;
6835 int waiting
= waiting_for_user_input_p
;
6837 if (inhibit_sentinels
)
6840 /* No need to gcpro these, because all we do with them later
6841 is test them for EQness, and none of them should be a string. */
6842 odeactivate
= Vdeactivate_mark
;
6843 XSETBUFFER (obuffer
, current_buffer
);
6844 okeymap
= current_buffer
->keymap
;
6846 sentinel
= p
->sentinel
;
6847 if (NILP (sentinel
))
6850 /* Zilch the sentinel while it's running, to avoid recursive invocations;
6851 assure that it gets restored no matter how the sentinel exits. */
6853 record_unwind_protect (exec_sentinel_unwind
, Fcons (proc
, sentinel
));
6854 /* Inhibit quit so that random quits don't screw up a running filter. */
6855 specbind (Qinhibit_quit
, Qt
);
6856 specbind (Qlast_nonmenu_event
, Qt
);
6858 /* In case we get recursively called,
6859 and we already saved the match data nonrecursively,
6860 save the same match data in safely recursive fashion. */
6861 if (outer_running_asynch_code
)
6864 tem
= Fmatch_data (Qnil
, Qnil
, Qnil
);
6865 restore_search_regs ();
6866 record_unwind_save_match_data ();
6867 Fset_match_data (tem
, Qt
);
6870 /* For speed, if a search happens within this code,
6871 save the match data in a special nonrecursive fashion. */
6872 running_asynch_code
= 1;
6874 internal_condition_case_1 (read_process_output_call
,
6876 Fcons (proc
, Fcons (reason
, Qnil
))),
6877 !NILP (Vdebug_on_error
) ? Qnil
: Qerror
,
6878 exec_sentinel_error_handler
);
6880 /* If we saved the match data nonrecursively, restore it now. */
6881 restore_search_regs ();
6882 running_asynch_code
= outer_running_asynch_code
;
6884 Vdeactivate_mark
= odeactivate
;
6886 /* Restore waiting_for_user_input_p as it was
6887 when we were called, in case the filter clobbered it. */
6888 waiting_for_user_input_p
= waiting
;
6891 if (! EQ (Fcurrent_buffer (), obuffer
)
6892 || ! EQ (current_buffer
->keymap
, okeymap
))
6894 /* But do it only if the caller is actually going to read events.
6895 Otherwise there's no need to make him wake up, and it could
6896 cause trouble (for example it would make sit_for return). */
6897 if (waiting_for_user_input_p
== -1)
6898 record_asynch_buffer_change ();
6900 unbind_to (count
, Qnil
);
6903 /* Report all recent events of a change in process status
6904 (either run the sentinel or output a message).
6905 This is usually done while Emacs is waiting for keyboard input
6906 but can be done at other times. */
6909 status_notify (deleting_process
)
6910 struct Lisp_Process
*deleting_process
;
6912 register Lisp_Object proc
, buffer
;
6913 Lisp_Object tail
, msg
;
6914 struct gcpro gcpro1
, gcpro2
;
6918 /* We need to gcpro tail; if read_process_output calls a filter
6919 which deletes a process and removes the cons to which tail points
6920 from Vprocess_alist, and then causes a GC, tail is an unprotected
6924 /* Set this now, so that if new processes are created by sentinels
6925 that we run, we get called again to handle their status changes. */
6926 update_tick
= process_tick
;
6928 for (tail
= Vprocess_alist
; CONSP (tail
); tail
= XCDR (tail
))
6931 register struct Lisp_Process
*p
;
6933 proc
= Fcdr (XCAR (tail
));
6934 p
= XPROCESS (proc
);
6936 if (p
->tick
!= p
->update_tick
)
6938 p
->update_tick
= p
->tick
;
6940 /* If process is still active, read any output that remains. */
6941 while (! EQ (p
->filter
, Qt
)
6942 && ! EQ (p
->status
, Qconnect
)
6943 && ! EQ (p
->status
, Qlisten
)
6944 /* Network or serial process not stopped: */
6945 && ! EQ (p
->command
, Qt
)
6947 && p
!= deleting_process
6948 && read_process_output (proc
, p
->infd
) > 0);
6952 /* Get the text to use for the message. */
6953 if (p
->raw_status_new
)
6955 msg
= status_message (p
);
6957 /* If process is terminated, deactivate it or delete it. */
6959 if (CONSP (p
->status
))
6960 symbol
= XCAR (p
->status
);
6962 if (EQ (symbol
, Qsignal
) || EQ (symbol
, Qexit
)
6963 || EQ (symbol
, Qclosed
))
6965 if (delete_exited_processes
)
6966 remove_process (proc
);
6968 deactivate_process (proc
);
6971 /* The actions above may have further incremented p->tick.
6972 So set p->update_tick again
6973 so that an error in the sentinel will not cause
6974 this code to be run again. */
6975 p
->update_tick
= p
->tick
;
6976 /* Now output the message suitably. */
6977 if (!NILP (p
->sentinel
))
6978 exec_sentinel (proc
, msg
);
6979 /* Don't bother with a message in the buffer
6980 when a process becomes runnable. */
6981 else if (!EQ (symbol
, Qrun
) && !NILP (buffer
))
6983 Lisp_Object ro
, tem
;
6984 struct buffer
*old
= current_buffer
;
6985 int opoint
, opoint_byte
;
6986 int before
, before_byte
;
6988 ro
= XBUFFER (buffer
)->read_only
;
6990 /* Avoid error if buffer is deleted
6991 (probably that's why the process is dead, too) */
6992 if (NILP (XBUFFER (buffer
)->name
))
6994 Fset_buffer (buffer
);
6997 opoint_byte
= PT_BYTE
;
6998 /* Insert new output into buffer
6999 at the current end-of-output marker,
7000 thus preserving logical ordering of input and output. */
7001 if (XMARKER (p
->mark
)->buffer
)
7002 Fgoto_char (p
->mark
);
7004 SET_PT_BOTH (ZV
, ZV_BYTE
);
7007 before_byte
= PT_BYTE
;
7009 tem
= current_buffer
->read_only
;
7010 current_buffer
->read_only
= Qnil
;
7011 insert_string ("\nProcess ");
7012 Finsert (1, &p
->name
);
7013 insert_string (" ");
7015 current_buffer
->read_only
= tem
;
7016 set_marker_both (p
->mark
, p
->buffer
, PT
, PT_BYTE
);
7018 if (opoint
>= before
)
7019 SET_PT_BOTH (opoint
+ (PT
- before
),
7020 opoint_byte
+ (PT_BYTE
- before_byte
));
7022 SET_PT_BOTH (opoint
, opoint_byte
);
7024 set_buffer_internal (old
);
7029 update_mode_lines
++; /* in case buffers use %s in mode-line-format */
7034 DEFUN ("set-process-coding-system", Fset_process_coding_system
,
7035 Sset_process_coding_system
, 1, 3, 0,
7036 doc
: /* Set coding systems of PROCESS to DECODING and ENCODING.
7037 DECODING will be used to decode subprocess output and ENCODING to
7038 encode subprocess input. */)
7039 (process
, decoding
, encoding
)
7040 register Lisp_Object process
, decoding
, encoding
;
7042 register struct Lisp_Process
*p
;
7044 CHECK_PROCESS (process
);
7045 p
= XPROCESS (process
);
7047 error ("Input file descriptor of %s closed", SDATA (p
->name
));
7049 error ("Output file descriptor of %s closed", SDATA (p
->name
));
7050 Fcheck_coding_system (decoding
);
7051 Fcheck_coding_system (encoding
);
7052 encoding
= coding_inherit_eol_type (encoding
, Qnil
);
7053 p
->decode_coding_system
= decoding
;
7054 p
->encode_coding_system
= encoding
;
7055 setup_process_coding_systems (process
);
7060 DEFUN ("process-coding-system",
7061 Fprocess_coding_system
, Sprocess_coding_system
, 1, 1, 0,
7062 doc
: /* Return a cons of coding systems for decoding and encoding of PROCESS. */)
7064 register Lisp_Object process
;
7066 CHECK_PROCESS (process
);
7067 return Fcons (XPROCESS (process
)->decode_coding_system
,
7068 XPROCESS (process
)->encode_coding_system
);
7071 DEFUN ("set-process-filter-multibyte", Fset_process_filter_multibyte
,
7072 Sset_process_filter_multibyte
, 2, 2, 0,
7073 doc
: /* Set multibyteness of the strings given to PROCESS's filter.
7074 If FLAG is non-nil, the filter is given multibyte strings.
7075 If FLAG is nil, the filter is given unibyte strings. In this case,
7076 all character code conversion except for end-of-line conversion is
7079 Lisp_Object process
, flag
;
7081 register struct Lisp_Process
*p
;
7083 CHECK_PROCESS (process
);
7084 p
= XPROCESS (process
);
7086 p
->decode_coding_system
= raw_text_coding_system (p
->decode_coding_system
);
7087 setup_process_coding_systems (process
);
7092 DEFUN ("process-filter-multibyte-p", Fprocess_filter_multibyte_p
,
7093 Sprocess_filter_multibyte_p
, 1, 1, 0,
7094 doc
: /* Return t if a multibyte string is given to PROCESS's filter.*/)
7096 Lisp_Object process
;
7098 register struct Lisp_Process
*p
;
7099 struct coding_system
*coding
;
7101 CHECK_PROCESS (process
);
7102 p
= XPROCESS (process
);
7103 coding
= proc_decode_coding_system
[p
->infd
];
7104 return (CODING_FOR_UNIBYTE (coding
) ? Qnil
: Qt
);
7109 /* Add DESC to the set of keyboard input descriptors. */
7112 add_keyboard_wait_descriptor (desc
)
7115 FD_SET (desc
, &input_wait_mask
);
7116 FD_SET (desc
, &non_process_wait_mask
);
7117 if (desc
> max_keyboard_desc
)
7118 max_keyboard_desc
= desc
;
7121 static int add_gpm_wait_descriptor_called_flag
;
7124 add_gpm_wait_descriptor (desc
)
7127 if (! add_gpm_wait_descriptor_called_flag
)
7128 FD_CLR (0, &input_wait_mask
);
7129 add_gpm_wait_descriptor_called_flag
= 1;
7130 FD_SET (desc
, &input_wait_mask
);
7131 FD_SET (desc
, &gpm_wait_mask
);
7132 if (desc
> max_gpm_desc
)
7133 max_gpm_desc
= desc
;
7136 /* From now on, do not expect DESC to give keyboard input. */
7139 delete_keyboard_wait_descriptor (desc
)
7143 int lim
= max_keyboard_desc
;
7145 FD_CLR (desc
, &input_wait_mask
);
7146 FD_CLR (desc
, &non_process_wait_mask
);
7148 if (desc
== max_keyboard_desc
)
7149 for (fd
= 0; fd
< lim
; fd
++)
7150 if (FD_ISSET (fd
, &input_wait_mask
)
7151 && !FD_ISSET (fd
, &non_keyboard_wait_mask
)
7152 && !FD_ISSET (fd
, &gpm_wait_mask
))
7153 max_keyboard_desc
= fd
;
7157 delete_gpm_wait_descriptor (desc
)
7161 int lim
= max_gpm_desc
;
7163 FD_CLR (desc
, &input_wait_mask
);
7164 FD_CLR (desc
, &non_process_wait_mask
);
7166 if (desc
== max_gpm_desc
)
7167 for (fd
= 0; fd
< lim
; fd
++)
7168 if (FD_ISSET (fd
, &input_wait_mask
)
7169 && !FD_ISSET (fd
, &non_keyboard_wait_mask
)
7170 && !FD_ISSET (fd
, &non_process_wait_mask
))
7174 /* Return nonzero if *MASK has a bit set
7175 that corresponds to one of the keyboard input descriptors. */
7178 keyboard_bit_set (mask
)
7183 for (fd
= 0; fd
<= max_keyboard_desc
; fd
++)
7184 if (FD_ISSET (fd
, mask
) && FD_ISSET (fd
, &input_wait_mask
)
7185 && !FD_ISSET (fd
, &non_keyboard_wait_mask
))
7191 /* Enumeration of and access to system processes a-la ps(1). */
7193 DEFUN ("list-system-processes", Flist_system_processes
, Slist_system_processes
,
7195 doc
: /* Return a list of numerical process IDs of all running processes.
7196 If this functionality is unsupported, return nil.
7198 See `process-attributes' for getting attributes of a process given its ID. */)
7201 return list_system_processes ();
7204 DEFUN ("process-attributes", Fprocess_attributes
,
7205 Sprocess_attributes
, 1, 1, 0,
7206 doc
: /* Return attributes of the process given by its PID, a number.
7208 Value is an alist where each element is a cons cell of the form
7212 If this functionality is unsupported, the value is nil.
7214 See `list-system-processes' for getting a list of all process IDs.
7216 The KEYs of the attributes that this function may return are listed
7217 below, together with the type of the associated VALUE (in parentheses).
7218 Not all platforms support all of these attributes; unsupported
7219 attributes will not appear in the returned alist.
7220 Unless explicitly indicated otherwise, numbers can have either
7221 integer or floating point values.
7223 euid -- Effective user User ID of the process (number)
7224 user -- User name corresponding to euid (string)
7225 egid -- Effective user Group ID of the process (number)
7226 group -- Group name corresponding to egid (string)
7227 comm -- Command name (executable name only) (string)
7228 state -- Process state code, such as "S", "R", or "T" (string)
7229 ppid -- Parent process ID (number)
7230 pgrp -- Process group ID (number)
7231 sess -- Session ID, i.e. process ID of session leader (number)
7232 ttname -- Controlling tty name (string)
7233 tpgid -- ID of foreground process group on the process's tty (number)
7234 minflt -- number of minor page faults (number)
7235 majflt -- number of major page faults (number)
7236 cminflt -- cumulative number of minor page faults (number)
7237 cmajflt -- cumulative number of major page faults (number)
7238 utime -- user time used by the process, in the (HIGH LOW USEC) format
7239 stime -- system time used by the process, in the (HIGH LOW USEC) format
7240 time -- sum of utime and stime, in the (HIGH LOW USEC) format
7241 cutime -- user time used by the process and its children, (HIGH LOW USEC)
7242 cstime -- system time used by the process and its children, (HIGH LOW USEC)
7243 ctime -- sum of cutime and cstime, in the (HIGH LOW USEC) format
7244 pri -- priority of the process (number)
7245 nice -- nice value of the process (number)
7246 thcount -- process thread count (number)
7247 start -- time the process started, in the (HIGH LOW USEC) format
7248 vsize -- virtual memory size of the process in KB's (number)
7249 rss -- resident set size of the process in KB's (number)
7250 etime -- elapsed time the process is running, in (HIGH LOW USEC) format
7251 pcpu -- percents of CPU time used by the process (floating-point number)
7252 pmem -- percents of total physical memory used by process's resident set
7253 (floating-point number)
7254 args -- command line which invoked the process (string). */)
7259 return system_process_attributes (pid
);
7267 inhibit_sentinels
= 0;
7271 if (! noninteractive
|| initialized
)
7273 signal (SIGCHLD
, sigchld_handler
);
7276 FD_ZERO (&input_wait_mask
);
7277 FD_ZERO (&non_keyboard_wait_mask
);
7278 FD_ZERO (&non_process_wait_mask
);
7279 max_process_desc
= 0;
7281 #ifdef NON_BLOCKING_CONNECT
7282 FD_ZERO (&connect_wait_mask
);
7283 num_pending_connects
= 0;
7286 #ifdef ADAPTIVE_READ_BUFFERING
7287 process_output_delay_count
= 0;
7288 process_output_skip
= 0;
7291 /* Don't do this, it caused infinite select loops. The display
7292 method should call add_keyboard_wait_descriptor on stdin if it
7295 FD_SET (0, &input_wait_mask
);
7298 Vprocess_alist
= Qnil
;
7300 deleted_pid_list
= Qnil
;
7302 for (i
= 0; i
< MAXDESC
; i
++)
7304 chan_process
[i
] = Qnil
;
7305 proc_buffered_char
[i
] = -1;
7307 bzero (proc_decode_coding_system
, sizeof proc_decode_coding_system
);
7308 bzero (proc_encode_coding_system
, sizeof proc_encode_coding_system
);
7309 #ifdef DATAGRAM_SOCKETS
7310 bzero (datagram_address
, sizeof datagram_address
);
7315 Lisp_Object subfeatures
= Qnil
;
7316 struct socket_options
*sopt
;
7318 #define ADD_SUBFEATURE(key, val) \
7319 subfeatures = Fcons (Fcons (key, Fcons (val, Qnil)), subfeatures)
7321 #ifdef NON_BLOCKING_CONNECT
7322 ADD_SUBFEATURE (QCnowait
, Qt
);
7324 #ifdef DATAGRAM_SOCKETS
7325 ADD_SUBFEATURE (QCtype
, Qdatagram
);
7327 #ifdef HAVE_LOCAL_SOCKETS
7328 ADD_SUBFEATURE (QCfamily
, Qlocal
);
7330 ADD_SUBFEATURE (QCfamily
, Qipv4
);
7332 ADD_SUBFEATURE (QCfamily
, Qipv6
);
7334 #ifdef HAVE_GETSOCKNAME
7335 ADD_SUBFEATURE (QCservice
, Qt
);
7337 #if defined(O_NONBLOCK) || defined(O_NDELAY)
7338 ADD_SUBFEATURE (QCserver
, Qt
);
7341 for (sopt
= socket_options
; sopt
->name
; sopt
++)
7342 subfeatures
= Fcons (intern (sopt
->name
), subfeatures
);
7344 Fprovide (intern ("make-network-process"), subfeatures
);
7346 #endif /* HAVE_SOCKETS */
7348 #if defined (DARWIN_OS)
7349 /* PTYs are broken on Darwin < 6, but are sometimes useful for interactive
7350 processes. As such, we only change the default value. */
7353 char *release
= get_operating_system_release();
7354 if (!release
|| !release
[0] || (release
[0] < MIN_PTY_KERNEL_VERSION
7355 && release
[1] == '.')) {
7356 Vprocess_connection_type
= Qnil
;
7365 Qprocessp
= intern ("processp");
7366 staticpro (&Qprocessp
);
7367 Qrun
= intern ("run");
7369 Qstop
= intern ("stop");
7371 Qsignal
= intern ("signal");
7372 staticpro (&Qsignal
);
7374 /* Qexit is already staticpro'd by syms_of_eval; don't staticpro it
7377 Qexit = intern ("exit");
7378 staticpro (&Qexit); */
7380 Qopen
= intern ("open");
7382 Qclosed
= intern ("closed");
7383 staticpro (&Qclosed
);
7384 Qconnect
= intern ("connect");
7385 staticpro (&Qconnect
);
7386 Qfailed
= intern ("failed");
7387 staticpro (&Qfailed
);
7388 Qlisten
= intern ("listen");
7389 staticpro (&Qlisten
);
7390 Qlocal
= intern ("local");
7391 staticpro (&Qlocal
);
7392 Qipv4
= intern ("ipv4");
7395 Qipv6
= intern ("ipv6");
7398 Qdatagram
= intern ("datagram");
7399 staticpro (&Qdatagram
);
7401 QCport
= intern (":port");
7402 staticpro (&QCport
);
7403 QCspeed
= intern (":speed");
7404 staticpro (&QCspeed
);
7405 QCprocess
= intern (":process");
7406 staticpro (&QCprocess
);
7408 QCbytesize
= intern (":bytesize");
7409 staticpro (&QCbytesize
);
7410 QCstopbits
= intern (":stopbits");
7411 staticpro (&QCstopbits
);
7412 QCparity
= intern (":parity");
7413 staticpro (&QCparity
);
7414 Qodd
= intern ("odd");
7416 Qeven
= intern ("even");
7418 QCflowcontrol
= intern (":flowcontrol");
7419 staticpro (&QCflowcontrol
);
7420 Qhw
= intern ("hw");
7422 Qsw
= intern ("sw");
7424 QCsummary
= intern (":summary");
7425 staticpro (&QCsummary
);
7427 Qreal
= intern ("real");
7429 Qnetwork
= intern ("network");
7430 staticpro (&Qnetwork
);
7431 Qserial
= intern ("serial");
7432 staticpro (&Qserial
);
7434 QCname
= intern (":name");
7435 staticpro (&QCname
);
7436 QCbuffer
= intern (":buffer");
7437 staticpro (&QCbuffer
);
7438 QChost
= intern (":host");
7439 staticpro (&QChost
);
7440 QCservice
= intern (":service");
7441 staticpro (&QCservice
);
7442 QCtype
= intern (":type");
7443 staticpro (&QCtype
);
7444 QClocal
= intern (":local");
7445 staticpro (&QClocal
);
7446 QCremote
= intern (":remote");
7447 staticpro (&QCremote
);
7448 QCcoding
= intern (":coding");
7449 staticpro (&QCcoding
);
7450 QCserver
= intern (":server");
7451 staticpro (&QCserver
);
7452 QCnowait
= intern (":nowait");
7453 staticpro (&QCnowait
);
7454 QCsentinel
= intern (":sentinel");
7455 staticpro (&QCsentinel
);
7456 QClog
= intern (":log");
7458 QCnoquery
= intern (":noquery");
7459 staticpro (&QCnoquery
);
7460 QCstop
= intern (":stop");
7461 staticpro (&QCstop
);
7462 QCoptions
= intern (":options");
7463 staticpro (&QCoptions
);
7464 QCplist
= intern (":plist");
7465 staticpro (&QCplist
);
7467 Qlast_nonmenu_event
= intern ("last-nonmenu-event");
7468 staticpro (&Qlast_nonmenu_event
);
7470 staticpro (&Vprocess_alist
);
7472 staticpro (&deleted_pid_list
);
7475 Qeuid
= intern ("euid");
7477 Qegid
= intern ("egid");
7479 Quser
= intern ("user");
7481 Qgroup
= intern ("group");
7482 staticpro (&Qgroup
);
7483 Qcomm
= intern ("comm");
7485 Qstate
= intern ("state");
7486 staticpro (&Qstate
);
7487 Qppid
= intern ("ppid");
7489 Qpgrp
= intern ("pgrp");
7491 Qsess
= intern ("sess");
7493 Qttname
= intern ("ttname");
7494 staticpro (&Qttname
);
7495 Qtpgid
= intern ("tpgid");
7496 staticpro (&Qtpgid
);
7497 Qminflt
= intern ("minflt");
7498 staticpro (&Qminflt
);
7499 Qmajflt
= intern ("majflt");
7500 staticpro (&Qmajflt
);
7501 Qcminflt
= intern ("cminflt");
7502 staticpro (&Qcminflt
);
7503 Qcmajflt
= intern ("cmajflt");
7504 staticpro (&Qcmajflt
);
7505 Qutime
= intern ("utime");
7506 staticpro (&Qutime
);
7507 Qstime
= intern ("stime");
7508 staticpro (&Qstime
);
7509 Qtime
= intern ("time");
7511 Qcutime
= intern ("cutime");
7512 staticpro (&Qcutime
);
7513 Qcstime
= intern ("cstime");
7514 staticpro (&Qcstime
);
7515 Qctime
= intern ("ctime");
7516 staticpro (&Qctime
);
7517 Qpri
= intern ("pri");
7519 Qnice
= intern ("nice");
7521 Qthcount
= intern ("thcount");
7522 staticpro (&Qthcount
);
7523 Qstart
= intern ("start");
7524 staticpro (&Qstart
);
7525 Qvsize
= intern ("vsize");
7526 staticpro (&Qvsize
);
7527 Qrss
= intern ("rss");
7529 Qetime
= intern ("etime");
7530 staticpro (&Qetime
);
7531 Qpcpu
= intern ("pcpu");
7533 Qpmem
= intern ("pmem");
7535 Qargs
= intern ("args");
7538 DEFVAR_BOOL ("delete-exited-processes", &delete_exited_processes
,
7539 doc
: /* *Non-nil means delete processes immediately when they exit.
7540 A value of nil means don't delete them until `list-processes' is run. */);
7542 delete_exited_processes
= 1;
7544 DEFVAR_LISP ("process-connection-type", &Vprocess_connection_type
,
7545 doc
: /* Control type of device used to communicate with subprocesses.
7546 Values are nil to use a pipe, or t or `pty' to use a pty.
7547 The value has no effect if the system has no ptys or if all ptys are busy:
7548 then a pipe is used in any case.
7549 The value takes effect when `start-process' is called. */);
7550 Vprocess_connection_type
= Qt
;
7552 #ifdef ADAPTIVE_READ_BUFFERING
7553 DEFVAR_LISP ("process-adaptive-read-buffering", &Vprocess_adaptive_read_buffering
,
7554 doc
: /* If non-nil, improve receive buffering by delaying after short reads.
7555 On some systems, when Emacs reads the output from a subprocess, the output data
7556 is read in very small blocks, potentially resulting in very poor performance.
7557 This behavior can be remedied to some extent by setting this variable to a
7558 non-nil value, as it will automatically delay reading from such processes, to
7559 allow them to produce more output before Emacs tries to read it.
7560 If the value is t, the delay is reset after each write to the process; any other
7561 non-nil value means that the delay is not reset on write.
7562 The variable takes effect when `start-process' is called. */);
7563 Vprocess_adaptive_read_buffering
= Qt
;
7566 defsubr (&Sprocessp
);
7567 defsubr (&Sget_process
);
7568 defsubr (&Sget_buffer_process
);
7569 defsubr (&Sdelete_process
);
7570 defsubr (&Sprocess_status
);
7571 defsubr (&Sprocess_exit_status
);
7572 defsubr (&Sprocess_id
);
7573 defsubr (&Sprocess_name
);
7574 defsubr (&Sprocess_tty_name
);
7575 defsubr (&Sprocess_command
);
7576 defsubr (&Sset_process_buffer
);
7577 defsubr (&Sprocess_buffer
);
7578 defsubr (&Sprocess_mark
);
7579 defsubr (&Sset_process_filter
);
7580 defsubr (&Sprocess_filter
);
7581 defsubr (&Sset_process_sentinel
);
7582 defsubr (&Sprocess_sentinel
);
7583 defsubr (&Sset_process_window_size
);
7584 defsubr (&Sset_process_inherit_coding_system_flag
);
7585 defsubr (&Sprocess_inherit_coding_system_flag
);
7586 defsubr (&Sset_process_query_on_exit_flag
);
7587 defsubr (&Sprocess_query_on_exit_flag
);
7588 defsubr (&Sprocess_contact
);
7589 defsubr (&Sprocess_plist
);
7590 defsubr (&Sset_process_plist
);
7591 defsubr (&Slist_processes
);
7592 defsubr (&Sprocess_list
);
7593 defsubr (&Sstart_process
);
7595 defsubr (&Sserial_process_configure
);
7596 defsubr (&Smake_serial_process
);
7597 #endif /* HAVE_SERIAL */
7599 defsubr (&Sset_network_process_option
);
7600 defsubr (&Smake_network_process
);
7601 defsubr (&Sformat_network_address
);
7602 #endif /* HAVE_SOCKETS */
7603 #if defined(HAVE_SOCKETS) && defined(HAVE_NET_IF_H) && defined(HAVE_SYS_IOCTL_H)
7605 defsubr (&Snetwork_interface_list
);
7607 #if defined(SIOCGIFADDR) || defined(SIOCGIFHWADDR) || defined(SIOCGIFFLAGS)
7608 defsubr (&Snetwork_interface_info
);
7610 #endif /* HAVE_SOCKETS ... */
7611 #ifdef DATAGRAM_SOCKETS
7612 defsubr (&Sprocess_datagram_address
);
7613 defsubr (&Sset_process_datagram_address
);
7615 defsubr (&Saccept_process_output
);
7616 defsubr (&Sprocess_send_region
);
7617 defsubr (&Sprocess_send_string
);
7618 defsubr (&Sinterrupt_process
);
7619 defsubr (&Skill_process
);
7620 defsubr (&Squit_process
);
7621 defsubr (&Sstop_process
);
7622 defsubr (&Scontinue_process
);
7623 defsubr (&Sprocess_running_child_p
);
7624 defsubr (&Sprocess_send_eof
);
7625 defsubr (&Ssignal_process
);
7626 defsubr (&Swaiting_for_user_input_p
);
7627 defsubr (&Sprocess_type
);
7628 defsubr (&Sset_process_coding_system
);
7629 defsubr (&Sprocess_coding_system
);
7630 defsubr (&Sset_process_filter_multibyte
);
7631 defsubr (&Sprocess_filter_multibyte_p
);
7632 defsubr (&Slist_system_processes
);
7633 defsubr (&Sprocess_attributes
);
7637 #else /* not subprocesses */
7639 #include <sys/types.h>
7641 #include <sys/stat.h>
7644 #ifdef HAVE_UNISTD_H
7649 #include "systime.h"
7650 #include "character.h"
7652 #include "termopts.h"
7653 #include "sysselect.h"
7655 extern int frame_garbaged
;
7657 extern EMACS_TIME
timer_check ();
7658 extern int timers_run
;
7660 Lisp_Object QCtype
, QCname
;
7662 Lisp_Object Qeuid
, Qegid
, Qcomm
, Qstate
, Qppid
, Qpgrp
, Qsess
, Qttname
, Qtpgid
;
7663 Lisp_Object Qminflt
, Qmajflt
, Qcminflt
, Qcmajflt
, Qutime
, Qstime
, Qcstime
;
7664 Lisp_Object Qcutime
, Qpri
, Qnice
, Qthcount
, Qstart
, Qvsize
, Qrss
, Qargs
;
7665 Lisp_Object Quser
, Qgroup
, Qetime
, Qpcpu
, Qpmem
, Qtime
, Qctime
;
7667 /* As described above, except assuming that there are no subprocesses:
7669 Wait for timeout to elapse and/or keyboard input to be available.
7672 timeout in seconds, or
7673 zero for no limit, or
7674 -1 means gobble data immediately available but don't wait for any.
7676 read_kbd is a Lisp_Object:
7677 0 to ignore keyboard input, or
7678 1 to return when input is available, or
7679 -1 means caller will actually read the input, so don't throw to
7682 see full version for other parameters. We know that wait_proc will
7683 always be NULL, since `subprocesses' isn't defined.
7685 do_display != 0 means redisplay should be done to show subprocess
7686 output that arrives.
7688 Return true if we received input from any process. */
7691 wait_reading_process_output (time_limit
, microsecs
, read_kbd
, do_display
,
7692 wait_for_cell
, wait_proc
, just_wait_proc
)
7693 int time_limit
, microsecs
, read_kbd
, do_display
;
7694 Lisp_Object wait_for_cell
;
7695 struct Lisp_Process
*wait_proc
;
7699 EMACS_TIME end_time
, timeout
;
7700 SELECT_TYPE waitchannels
;
7703 /* What does time_limit really mean? */
7704 if (time_limit
|| microsecs
)
7706 EMACS_GET_TIME (end_time
);
7707 EMACS_SET_SECS_USECS (timeout
, time_limit
, microsecs
);
7708 EMACS_ADD_TIME (end_time
, end_time
, timeout
);
7711 /* Turn off periodic alarms (in case they are in use)
7712 and then turn off any other atimers,
7713 because the select emulator uses alarms. */
7715 turn_on_atimers (0);
7719 int timeout_reduced_for_timers
= 0;
7721 /* If calling from keyboard input, do not quit
7722 since we want to return C-g as an input character.
7723 Otherwise, do pending quit if requested. */
7727 /* Exit now if the cell we're waiting for became non-nil. */
7728 if (! NILP (wait_for_cell
) && ! NILP (XCAR (wait_for_cell
)))
7731 /* Compute time from now till when time limit is up */
7732 /* Exit if already run out */
7733 if (time_limit
== -1)
7735 /* -1 specified for timeout means
7736 gobble output available now
7737 but don't wait at all. */
7739 EMACS_SET_SECS_USECS (timeout
, 0, 0);
7741 else if (time_limit
|| microsecs
)
7743 EMACS_GET_TIME (timeout
);
7744 EMACS_SUB_TIME (timeout
, end_time
, timeout
);
7745 if (EMACS_TIME_NEG_P (timeout
))
7750 EMACS_SET_SECS_USECS (timeout
, 100000, 0);
7753 /* If our caller will not immediately handle keyboard events,
7754 run timer events directly.
7755 (Callers that will immediately read keyboard events
7756 call timer_delay on their own.) */
7757 if (NILP (wait_for_cell
))
7759 EMACS_TIME timer_delay
;
7763 int old_timers_run
= timers_run
;
7764 timer_delay
= timer_check (1);
7765 if (timers_run
!= old_timers_run
&& do_display
)
7766 /* We must retry, since a timer may have requeued itself
7767 and that could alter the time delay. */
7768 redisplay_preserve_echo_area (14);
7772 while (!detect_input_pending ());
7774 /* If there is unread keyboard input, also return. */
7776 && requeued_events_pending_p ())
7779 if (! EMACS_TIME_NEG_P (timer_delay
) && time_limit
!= -1)
7781 EMACS_TIME difference
;
7782 EMACS_SUB_TIME (difference
, timer_delay
, timeout
);
7783 if (EMACS_TIME_NEG_P (difference
))
7785 timeout
= timer_delay
;
7786 timeout_reduced_for_timers
= 1;
7791 /* Cause C-g and alarm signals to take immediate action,
7792 and cause input available signals to zero out timeout. */
7794 set_waiting_for_input (&timeout
);
7796 /* Wait till there is something to do. */
7798 if (! read_kbd
&& NILP (wait_for_cell
))
7799 FD_ZERO (&waitchannels
);
7801 FD_SET (0, &waitchannels
);
7803 /* If a frame has been newly mapped and needs updating,
7804 reprocess its display stuff. */
7805 if (frame_garbaged
&& do_display
)
7807 clear_waiting_for_input ();
7808 redisplay_preserve_echo_area (15);
7810 set_waiting_for_input (&timeout
);
7813 if (read_kbd
&& detect_input_pending ())
7816 FD_ZERO (&waitchannels
);
7819 nfds
= select (1, &waitchannels
, (SELECT_TYPE
*)0, (SELECT_TYPE
*)0,
7824 /* Make C-g and alarm signals set flags again */
7825 clear_waiting_for_input ();
7827 /* If we woke up due to SIGWINCH, actually change size now. */
7828 do_pending_window_change (0);
7830 if (time_limit
&& nfds
== 0 && ! timeout_reduced_for_timers
)
7831 /* We waited the full specified time, so return now. */
7836 /* If the system call was interrupted, then go around the
7838 if (xerrno
== EINTR
)
7839 FD_ZERO (&waitchannels
);
7841 error ("select error: %s", emacs_strerror (xerrno
));
7844 else if (nfds
> 0 && (waitchannels
& 1) && interrupt_input
)
7845 /* System sometimes fails to deliver SIGIO. */
7846 kill (getpid (), SIGIO
);
7849 if (read_kbd
&& interrupt_input
&& (waitchannels
& 1))
7850 kill (getpid (), SIGIO
);
7853 /* Check for keyboard input */
7856 && detect_input_pending_run_timers (do_display
))
7858 swallow_events (do_display
);
7859 if (detect_input_pending_run_timers (do_display
))
7863 /* If there is unread keyboard input, also return. */
7865 && requeued_events_pending_p ())
7868 /* If wait_for_cell. check for keyboard input
7869 but don't run any timers.
7870 ??? (It seems wrong to me to check for keyboard
7871 input at all when wait_for_cell, but the code
7872 has been this way since July 1994.
7873 Try changing this after version 19.31.) */
7874 if (! NILP (wait_for_cell
)
7875 && detect_input_pending ())
7877 swallow_events (do_display
);
7878 if (detect_input_pending ())
7882 /* Exit now if the cell we're waiting for became non-nil. */
7883 if (! NILP (wait_for_cell
) && ! NILP (XCAR (wait_for_cell
)))
7893 /* Don't confuse make-docfile by having two doc strings for this function.
7894 make-docfile does not pay attention to #if, for good reason! */
7895 DEFUN ("get-buffer-process", Fget_buffer_process
, Sget_buffer_process
, 1, 1, 0,
7898 register Lisp_Object name
;
7903 /* Don't confuse make-docfile by having two doc strings for this function.
7904 make-docfile does not pay attention to #if, for good reason! */
7905 DEFUN ("process-inherit-coding-system-flag",
7906 Fprocess_inherit_coding_system_flag
, Sprocess_inherit_coding_system_flag
,
7910 register Lisp_Object process
;
7912 /* Ignore the argument and return the value of
7913 inherit-process-coding-system. */
7914 return inherit_process_coding_system
? Qt
: Qnil
;
7917 /* Kill all processes associated with `buffer'.
7918 If `buffer' is nil, kill all processes.
7919 Since we have no subprocesses, this does nothing. */
7922 kill_buffer_processes (buffer
)
7927 DEFUN ("list-system-processes", Flist_system_processes
, Slist_system_processes
,
7929 doc
: /* Return a list of numerical process IDs of all running processes.
7930 If this functionality is unsupported, return nil.
7932 See `process-attributes' for getting attributes of a process given its ID. */)
7935 return list_system_processes ();
7938 DEFUN ("process-attributes", Fprocess_attributes
,
7939 Sprocess_attributes
, 1, 1, 0,
7940 doc
: /* Return attributes of the process given by its PID, a number.
7942 Value is an alist where each element is a cons cell of the form
7946 If this functionality is unsupported, the value is nil.
7948 See `list-system-processes' for getting a list of all process IDs.
7950 The KEYs of the attributes that this function may return are listed
7951 below, together with the type of the associated VALUE (in parentheses).
7952 Not all platforms support all of these attributes; unsupported
7953 attributes will not appear in the returned alist.
7954 Unless explicitly indicated otherwise, numbers can have either
7955 integer or floating point values.
7957 euid -- Effective user User ID of the process (number)
7958 user -- User name corresponding to euid (string)
7959 egid -- Effective user Group ID of the process (number)
7960 group -- Group name corresponding to egid (string)
7961 comm -- Command name (executable name only) (string)
7962 state -- Process state code, such as "S", "R", or "T" (string)
7963 ppid -- Parent process ID (number)
7964 pgrp -- Process group ID (number)
7965 sess -- Session ID, i.e. process ID of session leader (number)
7966 ttname -- Controlling tty name (string)
7967 tpgid -- ID of foreground process group on the process's tty (number)
7968 minflt -- number of minor page faults (number)
7969 majflt -- number of major page faults (number)
7970 cminflt -- cumulative number of minor page faults (number)
7971 cmajflt -- cumulative number of major page faults (number)
7972 utime -- user time used by the process, in the (HIGH LOW USEC) format
7973 stime -- system time used by the process, in the (HIGH LOW USEC) format
7974 time -- sum of utime and stime, in the (HIGH LOW USEC) format
7975 cutime -- user time used by the process and its children, (HIGH LOW USEC)
7976 cstime -- system time used by the process and its children, (HIGH LOW USEC)
7977 ctime -- sum of cutime and cstime, in the (HIGH LOW USEC) format
7978 pri -- priority of the process (number)
7979 nice -- nice value of the process (number)
7980 thcount -- process thread count (number)
7981 start -- time the process started, in the (HIGH LOW USEC) format
7982 vsize -- virtual memory size of the process in KB's (number)
7983 rss -- resident set size of the process in KB's (number)
7984 etime -- elapsed time the process is running, in (HIGH LOW USEC) format
7985 pcpu -- percents of CPU time used by the process (floating-point number)
7986 pmem -- percents of total physical memory used by process's resident set
7987 (floating-point number)
7988 args -- command line which invoked the process (string). */)
7993 return system_process_attributes (pid
);
8004 QCtype
= intern (":type");
8005 staticpro (&QCtype
);
8006 QCname
= intern (":name");
8007 staticpro (&QCname
);
8008 QCtype
= intern (":type");
8009 staticpro (&QCtype
);
8010 QCname
= intern (":name");
8011 staticpro (&QCname
);
8012 Qeuid
= intern ("euid");
8014 Qegid
= intern ("egid");
8016 Quser
= intern ("user");
8018 Qgroup
= intern ("group");
8019 staticpro (&Qgroup
);
8020 Qcomm
= intern ("comm");
8022 Qstate
= intern ("state");
8023 staticpro (&Qstate
);
8024 Qppid
= intern ("ppid");
8026 Qpgrp
= intern ("pgrp");
8028 Qsess
= intern ("sess");
8030 Qttname
= intern ("ttname");
8031 staticpro (&Qttname
);
8032 Qtpgid
= intern ("tpgid");
8033 staticpro (&Qtpgid
);
8034 Qminflt
= intern ("minflt");
8035 staticpro (&Qminflt
);
8036 Qmajflt
= intern ("majflt");
8037 staticpro (&Qmajflt
);
8038 Qcminflt
= intern ("cminflt");
8039 staticpro (&Qcminflt
);
8040 Qcmajflt
= intern ("cmajflt");
8041 staticpro (&Qcmajflt
);
8042 Qutime
= intern ("utime");
8043 staticpro (&Qutime
);
8044 Qstime
= intern ("stime");
8045 staticpro (&Qstime
);
8046 Qtime
= intern ("time");
8048 Qcutime
= intern ("cutime");
8049 staticpro (&Qcutime
);
8050 Qcstime
= intern ("cstime");
8051 staticpro (&Qcstime
);
8052 Qctime
= intern ("ctime");
8053 staticpro (&Qctime
);
8054 Qpri
= intern ("pri");
8056 Qnice
= intern ("nice");
8058 Qthcount
= intern ("thcount");
8059 staticpro (&Qthcount
);
8060 Qstart
= intern ("start");
8061 staticpro (&Qstart
);
8062 Qvsize
= intern ("vsize");
8063 staticpro (&Qvsize
);
8064 Qrss
= intern ("rss");
8066 Qetime
= intern ("etime");
8067 staticpro (&Qetime
);
8068 Qpcpu
= intern ("pcpu");
8070 Qpmem
= intern ("pmem");
8072 Qargs
= intern ("args");
8075 defsubr (&Sget_buffer_process
);
8076 defsubr (&Sprocess_inherit_coding_system_flag
);
8077 defsubr (&Slist_system_processes
);
8078 defsubr (&Sprocess_attributes
);
8082 #endif /* not subprocesses */
8084 /* arch-tag: 3706c011-7b9a-4117-bd4f-59e7f701a4c4
8085 (do not change this comment) */