1 /* Asynchronous subprocess control for GNU Emacs.
3 Copyright (C) 1985-1988, 1993-1996, 1998-1999, 2001-2013 Free Software
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/>. */
24 #define PROCESS_INLINE EXTERN_INLINE
28 #include <sys/types.h> /* Some typedefs are used in sys/file.h. */
36 /* Only MS-DOS does not define `subprocesses'. */
39 #include <sys/socket.h>
41 #include <netinet/in.h>
42 #include <arpa/inet.h>
44 /* Are local (unix) sockets supported? */
45 #if defined (HAVE_SYS_UN_H)
46 #if !defined (AF_LOCAL) && defined (AF_UNIX)
47 #define AF_LOCAL AF_UNIX
50 #define HAVE_LOCAL_SOCKETS
55 #include <sys/ioctl.h>
56 #if defined (HAVE_NET_IF_H)
58 #endif /* HAVE_NET_IF_H */
60 #if defined (HAVE_IFADDRS_H)
61 /* Must be after net/if.h */
64 /* We only use structs from this header when we use getifaddrs. */
65 #if defined (HAVE_NET_IF_DL_H)
66 #include <net/if_dl.h>
76 # include <sys/stream.h>
77 # include <sys/stropts.h>
81 #include <arpa/nameser.h>
96 #endif /* subprocesses */
102 #include "character.h"
107 #include "termhooks.h"
108 #include "termopts.h"
109 #include "commands.h"
110 #include "keyboard.h"
111 #include "blockinput.h"
112 #include "dispextern.h"
113 #include "composite.h"
115 #include "sysselect.h"
116 #include "syssignal.h"
122 #ifdef HAVE_WINDOW_SYSTEM
124 #endif /* HAVE_WINDOW_SYSTEM */
127 #include "xgselect.h"
134 extern int sys_select (int, SELECT_TYPE
*, SELECT_TYPE
*, SELECT_TYPE
*,
135 EMACS_TIME
*, void *);
138 /* Work around GCC 4.7.0 bug with strict overflow checking; see
139 <http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52904>.
140 These lines can be removed once the GCC bug is fixed. */
141 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)
142 # pragma GCC diagnostic ignored "-Wstrict-overflow"
145 Lisp_Object Qeuid
, Qegid
, Qcomm
, Qstate
, Qppid
, Qpgrp
, Qsess
, Qttname
, Qtpgid
;
146 Lisp_Object Qminflt
, Qmajflt
, Qcminflt
, Qcmajflt
, Qutime
, Qstime
, Qcstime
;
147 Lisp_Object Qcutime
, Qpri
, Qnice
, Qthcount
, Qstart
, Qvsize
, Qrss
, Qargs
;
148 Lisp_Object Quser
, Qgroup
, Qetime
, Qpcpu
, Qpmem
, Qtime
, Qctime
;
149 Lisp_Object QCname
, QCtype
;
151 /* True if keyboard input is on hold, zero otherwise. */
153 static bool kbd_is_on_hold
;
155 /* Nonzero means don't run process sentinels. This is used
157 bool inhibit_sentinels
;
161 Lisp_Object Qprocessp
;
162 static Lisp_Object Qrun
, Qstop
, Qsignal
;
163 static Lisp_Object Qopen
, Qclosed
, Qconnect
, Qfailed
, Qlisten
;
165 static Lisp_Object Qipv4
, Qdatagram
, Qseqpacket
;
166 static Lisp_Object Qreal
, Qnetwork
, Qserial
;
168 static Lisp_Object Qipv6
;
170 static Lisp_Object QCport
, QCprocess
;
172 Lisp_Object QCbytesize
, QCstopbits
, QCparity
, Qodd
, Qeven
;
173 Lisp_Object QCflowcontrol
, Qhw
, Qsw
, QCsummary
;
174 static Lisp_Object QCbuffer
, QChost
, QCservice
;
175 static Lisp_Object QClocal
, QCremote
, QCcoding
;
176 static Lisp_Object QCserver
, QCnowait
, QCnoquery
, QCstop
;
177 static Lisp_Object QCsentinel
, QClog
, QCoptions
, QCplist
;
178 static Lisp_Object Qlast_nonmenu_event
;
179 static Lisp_Object Qinternal_default_process_sentinel
;
180 static Lisp_Object Qinternal_default_process_filter
;
182 #define NETCONN_P(p) (EQ (XPROCESS (p)->type, Qnetwork))
183 #define NETCONN1_P(p) (EQ (p->type, Qnetwork))
184 #define SERIALCONN_P(p) (EQ (XPROCESS (p)->type, Qserial))
185 #define SERIALCONN1_P(p) (EQ (p->type, Qserial))
187 /* Number of events of change of status of a process. */
188 static EMACS_INT process_tick
;
189 /* Number of events for which the user or sentinel has been notified. */
190 static EMACS_INT update_tick
;
192 /* Define NON_BLOCKING_CONNECT if we can support non-blocking connects. */
194 /* Only W32 has this, it really means that select can't take write mask. */
195 #ifdef BROKEN_NON_BLOCKING_CONNECT
196 #undef NON_BLOCKING_CONNECT
197 #define SELECT_CANT_DO_WRITE_MASK
199 #ifndef NON_BLOCKING_CONNECT
201 #if defined (HAVE_GETPEERNAME) || defined (GNU_LINUX)
202 #if defined (EWOULDBLOCK) || defined (EINPROGRESS)
203 #define NON_BLOCKING_CONNECT
204 #endif /* EWOULDBLOCK || EINPROGRESS */
205 #endif /* HAVE_GETPEERNAME || GNU_LINUX */
206 #endif /* HAVE_SELECT */
207 #endif /* NON_BLOCKING_CONNECT */
208 #endif /* BROKEN_NON_BLOCKING_CONNECT */
210 /* Define DATAGRAM_SOCKETS if datagrams can be used safely on
211 this system. We need to read full packets, so we need a
212 "non-destructive" select. So we require either native select,
213 or emulation of select using FIONREAD. */
215 #ifndef BROKEN_DATAGRAM_SOCKETS
216 # if defined HAVE_SELECT || defined USABLE_FIONREAD
217 # if defined HAVE_SENDTO && defined HAVE_RECVFROM && defined EMSGSIZE
218 # define DATAGRAM_SOCKETS
223 #if defined HAVE_LOCAL_SOCKETS && defined DATAGRAM_SOCKETS
224 # define HAVE_SEQPACKET
227 #if !defined (ADAPTIVE_READ_BUFFERING) && !defined (NO_ADAPTIVE_READ_BUFFERING)
228 #define ADAPTIVE_READ_BUFFERING
231 #ifdef ADAPTIVE_READ_BUFFERING
232 #define READ_OUTPUT_DELAY_INCREMENT (EMACS_TIME_RESOLUTION / 100)
233 #define READ_OUTPUT_DELAY_MAX (READ_OUTPUT_DELAY_INCREMENT * 5)
234 #define READ_OUTPUT_DELAY_MAX_MAX (READ_OUTPUT_DELAY_INCREMENT * 7)
236 /* Number of processes which have a non-zero read_output_delay,
237 and therefore might be delayed for adaptive read buffering. */
239 static int process_output_delay_count
;
241 /* True if any process has non-nil read_output_skip. */
243 static bool process_output_skip
;
246 #define process_output_delay_count 0
249 static void create_process (Lisp_Object
, char **, Lisp_Object
);
251 static bool keyboard_bit_set (SELECT_TYPE
*);
253 static void deactivate_process (Lisp_Object
);
254 static void status_notify (struct Lisp_Process
*);
255 static int read_process_output (Lisp_Object
, int);
256 static void handle_child_signal (int);
257 static void create_pty (Lisp_Object
);
259 /* If we support a window system, turn on the code to poll periodically
260 to detect C-g. It isn't actually used when doing interrupt input. */
261 #ifdef HAVE_WINDOW_SYSTEM
262 #define POLL_FOR_INPUT
265 static Lisp_Object
get_process (register Lisp_Object name
);
266 static void exec_sentinel (Lisp_Object proc
, Lisp_Object reason
);
268 #ifdef NON_BLOCKING_CONNECT
269 /* Number of bits set in connect_wait_mask. */
270 static int num_pending_connects
;
271 #endif /* NON_BLOCKING_CONNECT */
273 /* The largest descriptor currently in use for input. */
276 /* Indexed by descriptor, gives the process (if any) for that descriptor */
277 static Lisp_Object chan_process
[MAXDESC
];
279 /* Alist of elements (NAME . PROCESS) */
280 static Lisp_Object Vprocess_alist
;
282 /* Buffered-ahead input char from process, indexed by channel.
283 -1 means empty (no char is buffered).
284 Used on sys V where the only way to tell if there is any
285 output from the process is to read at least one char.
286 Always -1 on systems that support FIONREAD. */
288 static int proc_buffered_char
[MAXDESC
];
290 /* Table of `struct coding-system' for each process. */
291 static struct coding_system
*proc_decode_coding_system
[MAXDESC
];
292 static struct coding_system
*proc_encode_coding_system
[MAXDESC
];
294 #ifdef DATAGRAM_SOCKETS
295 /* Table of `partner address' for datagram sockets. */
296 static struct sockaddr_and_len
{
299 } datagram_address
[MAXDESC
];
300 #define DATAGRAM_CHAN_P(chan) (datagram_address[chan].sa != 0)
301 #define DATAGRAM_CONN_P(proc) (PROCESSP (proc) && datagram_address[XPROCESS (proc)->infd].sa != 0)
303 #define DATAGRAM_CHAN_P(chan) (0)
304 #define DATAGRAM_CONN_P(proc) (0)
307 /* These setters are used only in this file, so they can be private. */
309 pset_buffer (struct Lisp_Process
*p
, Lisp_Object val
)
314 pset_command (struct Lisp_Process
*p
, Lisp_Object val
)
319 pset_decode_coding_system (struct Lisp_Process
*p
, Lisp_Object val
)
321 p
->decode_coding_system
= val
;
324 pset_decoding_buf (struct Lisp_Process
*p
, Lisp_Object val
)
326 p
->decoding_buf
= val
;
329 pset_encode_coding_system (struct Lisp_Process
*p
, Lisp_Object val
)
331 p
->encode_coding_system
= val
;
334 pset_encoding_buf (struct Lisp_Process
*p
, Lisp_Object val
)
336 p
->encoding_buf
= val
;
339 pset_filter (struct Lisp_Process
*p
, Lisp_Object val
)
341 p
->filter
= NILP (val
) ? Qinternal_default_process_filter
: val
;
344 pset_log (struct Lisp_Process
*p
, Lisp_Object val
)
349 pset_mark (struct Lisp_Process
*p
, Lisp_Object val
)
354 pset_thread (struct Lisp_Process
*p
, Lisp_Object val
)
359 pset_name (struct Lisp_Process
*p
, Lisp_Object val
)
364 pset_plist (struct Lisp_Process
*p
, Lisp_Object val
)
369 pset_sentinel (struct Lisp_Process
*p
, Lisp_Object val
)
371 p
->sentinel
= NILP (val
) ? Qinternal_default_process_sentinel
: val
;
374 pset_status (struct Lisp_Process
*p
, Lisp_Object val
)
379 pset_tty_name (struct Lisp_Process
*p
, Lisp_Object val
)
384 pset_type (struct Lisp_Process
*p
, Lisp_Object val
)
389 pset_write_queue (struct Lisp_Process
*p
, Lisp_Object val
)
391 p
->write_queue
= val
;
398 /* Read from file descriptor. */
400 /* Write to file descriptor. */
402 /* This descriptor refers to a keyboard. Only valid if FOR_READ is
405 /* This descriptor refers to a process. */
407 /* A non-blocking connect. Only valid if FOR_WRITE is set. */
408 NON_BLOCKING_CONNECT_FD
= 16
411 static struct fd_callback_data
415 /* Flags from enum fd_bits. */
417 /* If this fd is locked to a certain thread, this points to it.
418 Otherwise, this is NULL. If an fd is locked to a thread, then
419 only that thread is permitted to wait on it. */
420 struct thread_state
*thread
;
421 /* If this fd is currently being selected on by a thread, this
422 points to the thread. Otherwise it is NULL. */
423 struct thread_state
*waiting_thread
;
424 } fd_callback_info
[MAXDESC
];
427 /* Add a file descriptor FD to be monitored for when read is possible.
428 When read is possible, call FUNC with argument DATA. */
431 add_read_fd (int fd
, fd_callback func
, void *data
)
433 eassert (fd
< MAXDESC
);
434 add_keyboard_wait_descriptor (fd
);
436 fd_callback_info
[fd
].func
= func
;
437 fd_callback_info
[fd
].data
= data
;
441 add_non_keyboard_read_fd (int fd
)
443 eassert (fd
>= 0 && fd
< MAXDESC
);
444 eassert (fd_callback_info
[fd
].func
== NULL
);
445 fd_callback_info
[fd
].flags
|= FOR_READ
;
451 add_process_read_fd (int fd
)
453 add_non_keyboard_read_fd (fd
);
454 fd_callback_info
[fd
].flags
|= PROCESS_FD
;
457 /* Stop monitoring file descriptor FD for when read is possible. */
460 delete_read_fd (int fd
)
462 eassert (fd
< MAXDESC
);
463 eassert (fd
<= max_desc
);
464 delete_keyboard_wait_descriptor (fd
);
466 if (fd_callback_info
[fd
].flags
== 0)
468 fd_callback_info
[fd
].func
= 0;
469 fd_callback_info
[fd
].data
= 0;
473 /* Add a file descriptor FD to be monitored for when write is possible.
474 When write is possible, call FUNC with argument DATA. */
477 add_write_fd (int fd
, fd_callback func
, void *data
)
479 eassert (fd
< MAXDESC
);
483 fd_callback_info
[fd
].func
= func
;
484 fd_callback_info
[fd
].data
= data
;
485 fd_callback_info
[fd
].flags
|= FOR_WRITE
;
489 add_non_blocking_write_fd (int fd
)
491 eassert (fd
>= 0 && fd
< MAXDESC
);
492 eassert (fd_callback_info
[fd
].func
== NULL
);
494 fd_callback_info
[fd
].flags
|= FOR_WRITE
| NON_BLOCKING_CONNECT_FD
;
497 ++num_pending_connects
;
501 recompute_max_desc (void)
505 for (fd
= max_desc
; fd
>= 0; --fd
)
507 if (fd_callback_info
[fd
].flags
!= 0)
515 /* Stop monitoring file descriptor FD for when write is possible. */
518 delete_write_fd (int fd
)
522 eassert (fd
< MAXDESC
);
523 eassert (fd
<= max_desc
);
525 if ((fd_callback_info
[fd
].flags
& NON_BLOCKING_CONNECT_FD
) != 0)
527 if (--num_pending_connects
< 0)
530 fd_callback_info
[fd
].flags
&= ~(FOR_WRITE
| NON_BLOCKING_CONNECT_FD
);
531 if (fd_callback_info
[fd
].flags
== 0)
533 fd_callback_info
[fd
].func
= 0;
534 fd_callback_info
[fd
].data
= 0;
537 recompute_max_desc ();
542 compute_input_wait_mask (SELECT_TYPE
*mask
)
547 for (fd
= 0; fd
<= max_desc
; ++fd
)
549 if (fd_callback_info
[fd
].thread
!= NULL
550 && fd_callback_info
[fd
].thread
!= current_thread
)
552 if (fd_callback_info
[fd
].waiting_thread
!= NULL
553 && fd_callback_info
[fd
].waiting_thread
!= current_thread
)
555 if ((fd_callback_info
[fd
].flags
& FOR_READ
) != 0)
558 fd_callback_info
[fd
].waiting_thread
= current_thread
;
564 compute_non_process_wait_mask (SELECT_TYPE
*mask
)
569 for (fd
= 0; fd
<= max_desc
; ++fd
)
571 if (fd_callback_info
[fd
].thread
!= NULL
572 && fd_callback_info
[fd
].thread
!= current_thread
)
574 if (fd_callback_info
[fd
].waiting_thread
!= NULL
575 && fd_callback_info
[fd
].waiting_thread
!= current_thread
)
577 if ((fd_callback_info
[fd
].flags
& FOR_READ
) != 0
578 && (fd_callback_info
[fd
].flags
& PROCESS_FD
) == 0)
581 fd_callback_info
[fd
].waiting_thread
= current_thread
;
587 compute_non_keyboard_wait_mask (SELECT_TYPE
*mask
)
592 for (fd
= 0; fd
<= max_desc
; ++fd
)
594 if (fd_callback_info
[fd
].thread
!= NULL
595 && fd_callback_info
[fd
].thread
!= current_thread
)
597 if (fd_callback_info
[fd
].waiting_thread
!= NULL
598 && fd_callback_info
[fd
].waiting_thread
!= current_thread
)
600 if ((fd_callback_info
[fd
].flags
& FOR_READ
) != 0
601 && (fd_callback_info
[fd
].flags
& KEYBOARD_FD
) == 0)
604 fd_callback_info
[fd
].waiting_thread
= current_thread
;
610 compute_write_mask (SELECT_TYPE
*mask
)
615 for (fd
= 0; fd
<= max_desc
; ++fd
)
617 if (fd_callback_info
[fd
].thread
!= NULL
618 && fd_callback_info
[fd
].thread
!= current_thread
)
620 if (fd_callback_info
[fd
].waiting_thread
!= NULL
621 && fd_callback_info
[fd
].waiting_thread
!= current_thread
)
623 if ((fd_callback_info
[fd
].flags
& FOR_WRITE
) != 0)
626 fd_callback_info
[fd
].waiting_thread
= current_thread
;
632 clear_waiting_thread_info (void)
636 for (fd
= 0; fd
<= max_desc
; ++fd
)
638 if (fd_callback_info
[fd
].waiting_thread
== current_thread
)
639 fd_callback_info
[fd
].waiting_thread
= NULL
;
644 /* Compute the Lisp form of the process status, p->status, from
645 the numeric status that was returned by `wait'. */
647 static Lisp_Object
status_convert (int);
650 update_status (struct Lisp_Process
*p
)
652 eassert (p
->raw_status_new
);
653 pset_status (p
, status_convert (p
->raw_status
));
654 p
->raw_status_new
= 0;
657 /* Convert a process status word in Unix format to
658 the list that we use internally. */
661 status_convert (int w
)
664 return Fcons (Qstop
, Fcons (make_number (WSTOPSIG (w
)), Qnil
));
665 else if (WIFEXITED (w
))
666 return Fcons (Qexit
, Fcons (make_number (WEXITSTATUS (w
)),
667 WCOREDUMP (w
) ? Qt
: Qnil
));
668 else if (WIFSIGNALED (w
))
669 return Fcons (Qsignal
, Fcons (make_number (WTERMSIG (w
)),
670 WCOREDUMP (w
) ? Qt
: Qnil
));
675 /* Given a status-list, extract the three pieces of information
676 and store them individually through the three pointers. */
679 decode_status (Lisp_Object l
, Lisp_Object
*symbol
, int *code
, bool *coredump
)
693 *code
= XFASTINT (XCAR (tem
));
695 *coredump
= !NILP (tem
);
699 /* Return a string describing a process status list. */
702 status_message (struct Lisp_Process
*p
)
704 Lisp_Object status
= p
->status
;
708 Lisp_Object string
, string2
;
710 decode_status (status
, &symbol
, &code
, &coredump
);
712 if (EQ (symbol
, Qsignal
) || EQ (symbol
, Qstop
))
715 synchronize_system_messages_locale ();
716 signame
= strsignal (code
);
718 string
= build_string ("unknown");
723 string
= build_unibyte_string (signame
);
724 if (! NILP (Vlocale_coding_system
))
725 string
= (code_convert_string_norecord
726 (string
, Vlocale_coding_system
, 0));
727 c1
= STRING_CHAR (SDATA (string
));
730 Faset (string
, make_number (0), make_number (c2
));
732 string2
= build_string (coredump
? " (core dumped)\n" : "\n");
733 return concat2 (string
, string2
);
735 else if (EQ (symbol
, Qexit
))
738 return build_string (code
== 0 ? "deleted\n" : "connection broken by remote peer\n");
740 return build_string ("finished\n");
741 string
= Fnumber_to_string (make_number (code
));
742 string2
= build_string (coredump
? " (core dumped)\n" : "\n");
743 return concat3 (build_string ("exited abnormally with code "),
746 else if (EQ (symbol
, Qfailed
))
748 string
= Fnumber_to_string (make_number (code
));
749 string2
= build_string ("\n");
750 return concat3 (build_string ("failed with code "),
754 return Fcopy_sequence (Fsymbol_name (symbol
));
759 /* The file name of the pty opened by allocate_pty. */
760 static char pty_name
[24];
762 /* Open an available pty, returning a file descriptor.
763 Return -1 on failure.
764 The file name of the terminal corresponding to the pty
765 is left in the variable pty_name. */
776 for (c
= FIRST_PTY_LETTER
; c
<= 'z'; c
++)
777 for (i
= 0; i
< 16; i
++)
780 #ifdef PTY_NAME_SPRINTF
783 sprintf (pty_name
, "/dev/pty%c%x", c
, i
);
784 #endif /* no PTY_NAME_SPRINTF */
788 #else /* no PTY_OPEN */
789 fd
= emacs_open (pty_name
, O_RDWR
| O_NONBLOCK
, 0);
790 #endif /* no PTY_OPEN */
794 /* check to make certain that both sides are available
795 this avoids a nasty yet stupid bug in rlogins */
796 #ifdef PTY_TTY_NAME_SPRINTF
799 sprintf (pty_name
, "/dev/tty%c%x", c
, i
);
800 #endif /* no PTY_TTY_NAME_SPRINTF */
801 if (faccessat (AT_FDCWD
, pty_name
, R_OK
| W_OK
, AT_EACCESS
) != 0)
816 #endif /* HAVE_PTYS */
819 make_process (Lisp_Object name
)
821 register Lisp_Object val
, tem
, name1
;
822 register struct Lisp_Process
*p
;
823 char suffix
[sizeof "<>" + INT_STRLEN_BOUND (printmax_t
)];
826 p
= allocate_process ();
827 /* Initialize Lisp data. Note that allocate_process initializes all
828 Lisp data to nil, so do it only for slots which should not be nil. */
829 pset_status (p
, Qrun
);
830 pset_mark (p
, Fmake_marker ());
831 pset_thread (p
, Fcurrent_thread ());
833 /* Initialize non-Lisp data. Note that allocate_process zeroes out all
834 non-Lisp data, so do it only for slots which should not be zero. */
839 p
->gnutls_initstage
= GNUTLS_STAGE_EMPTY
;
842 /* If name is already in use, modify it until it is unused. */
847 tem
= Fget_process (name1
);
848 if (NILP (tem
)) break;
849 name1
= concat2 (name
, make_formatted_string (suffix
, "<%"pMd
">", i
));
853 pset_sentinel (p
, Qinternal_default_process_sentinel
);
854 pset_filter (p
, Qinternal_default_process_filter
);
855 XSETPROCESS (val
, p
);
856 Vprocess_alist
= Fcons (Fcons (name
, val
), Vprocess_alist
);
861 remove_process (register Lisp_Object proc
)
863 register Lisp_Object pair
;
865 pair
= Frassq (proc
, Vprocess_alist
);
866 Vprocess_alist
= Fdelq (pair
, Vprocess_alist
);
868 deactivate_process (proc
);
872 update_processes_for_thread_death (Lisp_Object dying_thread
)
876 for (pair
= Vprocess_alist
; !NILP (pair
); pair
= XCDR (pair
))
878 Lisp_Object process
= XCDR (XCAR (pair
));
879 if (EQ (XPROCESS (process
)->thread
, dying_thread
))
881 struct Lisp_Process
*proc
= XPROCESS (process
);
885 fd_callback_info
[proc
->infd
].thread
= NULL
;
886 if (proc
->outfd
>= 0)
887 fd_callback_info
[proc
->outfd
].thread
= NULL
;
893 DEFUN ("processp", Fprocessp
, Sprocessp
, 1, 1, 0,
894 doc
: /* Return t if OBJECT is a process. */)
897 return PROCESSP (object
) ? Qt
: Qnil
;
900 DEFUN ("get-process", Fget_process
, Sget_process
, 1, 1, 0,
901 doc
: /* Return the process named NAME, or nil if there is none. */)
902 (register Lisp_Object name
)
907 return Fcdr (Fassoc (name
, Vprocess_alist
));
910 /* This is how commands for the user decode process arguments. It
911 accepts a process, a process name, a buffer, a buffer name, or nil.
912 Buffers denote the first process in the buffer, and nil denotes the
916 get_process (register Lisp_Object name
)
918 register Lisp_Object proc
, obj
;
921 obj
= Fget_process (name
);
923 obj
= Fget_buffer (name
);
925 error ("Process %s does not exist", SDATA (name
));
927 else if (NILP (name
))
928 obj
= Fcurrent_buffer ();
932 /* Now obj should be either a buffer object or a process object.
936 proc
= Fget_buffer_process (obj
);
938 error ("Buffer %s has no process", SDATA (BVAR (XBUFFER (obj
), name
)));
949 /* Fdelete_process promises to immediately forget about the process, but in
950 reality, Emacs needs to remember those processes until they have been
951 treated by the SIGCHLD handler and waitpid has been invoked on them;
952 otherwise they might fill up the kernel's process table.
954 Some processes created by call-process are also put onto this list. */
955 static Lisp_Object deleted_pid_list
;
958 record_deleted_pid (pid_t pid
)
960 deleted_pid_list
= Fcons (make_fixnum_or_float (pid
),
961 /* GC treated elements set to nil. */
962 Fdelq (Qnil
, deleted_pid_list
));
966 DEFUN ("delete-process", Fdelete_process
, Sdelete_process
, 1, 1, 0,
967 doc
: /* Delete PROCESS: kill it and forget about it immediately.
968 PROCESS may be a process, a buffer, the name of a process or buffer, or
969 nil, indicating the current buffer's process. */)
970 (register Lisp_Object process
)
972 register struct Lisp_Process
*p
;
974 process
= get_process (process
);
975 p
= XPROCESS (process
);
977 p
->raw_status_new
= 0;
978 if (NETCONN1_P (p
) || SERIALCONN1_P (p
))
980 pset_status (p
, Fcons (Qexit
, Fcons (make_number (0), Qnil
)));
981 p
->tick
= ++process_tick
;
983 redisplay_preserve_echo_area (13);
988 record_kill_process (p
);
992 /* Update P's status, since record_kill_process will make the
993 SIGCHLD handler update deleted_pid_list, not *P. */
995 if (p
->raw_status_new
)
997 symbol
= CONSP (p
->status
) ? XCAR (p
->status
) : p
->status
;
998 if (! (EQ (symbol
, Qsignal
) || EQ (symbol
, Qexit
)))
999 pset_status (p
, list2 (Qsignal
, make_number (SIGKILL
)));
1001 p
->tick
= ++process_tick
;
1003 redisplay_preserve_echo_area (13);
1006 remove_process (process
);
1010 DEFUN ("process-status", Fprocess_status
, Sprocess_status
, 1, 1, 0,
1011 doc
: /* Return the status of PROCESS.
1012 The returned value is one of the following symbols:
1013 run -- for a process that is running.
1014 stop -- for a process stopped but continuable.
1015 exit -- for a process that has exited.
1016 signal -- for a process that has got a fatal signal.
1017 open -- for a network stream connection that is open.
1018 listen -- for a network stream server that is listening.
1019 closed -- for a network stream connection that is closed.
1020 connect -- when waiting for a non-blocking connection to complete.
1021 failed -- when a non-blocking connection has failed.
1022 nil -- if arg is a process name and no such process exists.
1023 PROCESS may be a process, a buffer, the name of a process, or
1024 nil, indicating the current buffer's process. */)
1025 (register Lisp_Object process
)
1027 register struct Lisp_Process
*p
;
1028 register Lisp_Object status
;
1030 if (STRINGP (process
))
1031 process
= Fget_process (process
);
1033 process
= get_process (process
);
1038 p
= XPROCESS (process
);
1039 if (p
->raw_status_new
)
1043 status
= XCAR (status
);
1044 if (NETCONN1_P (p
) || SERIALCONN1_P (p
))
1046 if (EQ (status
, Qexit
))
1048 else if (EQ (p
->command
, Qt
))
1050 else if (EQ (status
, Qrun
))
1056 DEFUN ("process-exit-status", Fprocess_exit_status
, Sprocess_exit_status
,
1058 doc
: /* Return the exit status of PROCESS or the signal number that killed it.
1059 If PROCESS has not yet exited or died, return 0. */)
1060 (register Lisp_Object process
)
1062 CHECK_PROCESS (process
);
1063 if (XPROCESS (process
)->raw_status_new
)
1064 update_status (XPROCESS (process
));
1065 if (CONSP (XPROCESS (process
)->status
))
1066 return XCAR (XCDR (XPROCESS (process
)->status
));
1067 return make_number (0);
1070 DEFUN ("process-id", Fprocess_id
, Sprocess_id
, 1, 1, 0,
1071 doc
: /* Return the process id of PROCESS.
1072 This is the pid of the external process which PROCESS uses or talks to.
1073 For a network connection, this value is nil. */)
1074 (register Lisp_Object process
)
1078 CHECK_PROCESS (process
);
1079 pid
= XPROCESS (process
)->pid
;
1080 return (pid
? make_fixnum_or_float (pid
) : Qnil
);
1083 DEFUN ("process-name", Fprocess_name
, Sprocess_name
, 1, 1, 0,
1084 doc
: /* Return the name of PROCESS, as a string.
1085 This is the name of the program invoked in PROCESS,
1086 possibly modified to make it unique among process names. */)
1087 (register Lisp_Object process
)
1089 CHECK_PROCESS (process
);
1090 return XPROCESS (process
)->name
;
1093 DEFUN ("process-command", Fprocess_command
, Sprocess_command
, 1, 1, 0,
1094 doc
: /* Return the command that was executed to start PROCESS.
1095 This is a list of strings, the first string being the program executed
1096 and the rest of the strings being the arguments given to it.
1097 For a network or serial process, this is nil (process is running) or t
1098 \(process is stopped). */)
1099 (register Lisp_Object process
)
1101 CHECK_PROCESS (process
);
1102 return XPROCESS (process
)->command
;
1105 DEFUN ("process-tty-name", Fprocess_tty_name
, Sprocess_tty_name
, 1, 1, 0,
1106 doc
: /* Return the name of the terminal PROCESS uses, or nil if none.
1107 This is the terminal that the process itself reads and writes on,
1108 not the name of the pty that Emacs uses to talk with that terminal. */)
1109 (register Lisp_Object process
)
1111 CHECK_PROCESS (process
);
1112 return XPROCESS (process
)->tty_name
;
1115 DEFUN ("set-process-buffer", Fset_process_buffer
, Sset_process_buffer
,
1117 doc
: /* Set buffer associated with PROCESS to BUFFER (a buffer, or nil).
1119 (register Lisp_Object process
, Lisp_Object buffer
)
1121 struct Lisp_Process
*p
;
1123 CHECK_PROCESS (process
);
1125 CHECK_BUFFER (buffer
);
1126 p
= XPROCESS (process
);
1127 pset_buffer (p
, buffer
);
1128 if (NETCONN1_P (p
) || SERIALCONN1_P (p
))
1129 pset_childp (p
, Fplist_put (p
->childp
, QCbuffer
, buffer
));
1130 setup_process_coding_systems (process
);
1134 DEFUN ("process-buffer", Fprocess_buffer
, Sprocess_buffer
,
1136 doc
: /* Return the buffer PROCESS is associated with.
1137 Output from PROCESS is inserted in this buffer unless PROCESS has a filter. */)
1138 (register Lisp_Object process
)
1140 CHECK_PROCESS (process
);
1141 return XPROCESS (process
)->buffer
;
1144 DEFUN ("process-mark", Fprocess_mark
, Sprocess_mark
,
1146 doc
: /* Return the marker for the end of the last output from PROCESS. */)
1147 (register Lisp_Object process
)
1149 CHECK_PROCESS (process
);
1150 return XPROCESS (process
)->mark
;
1153 DEFUN ("set-process-filter", Fset_process_filter
, Sset_process_filter
,
1155 doc
: /* Give PROCESS the filter function FILTER; nil means default.
1156 A value of t means stop accepting output from the process.
1158 When a process has a non-default filter, its buffer is not used for output.
1159 Instead, each time it does output, the entire string of output is
1160 passed to the filter.
1162 The filter gets two arguments: the process and the string of output.
1163 The string argument is normally a multibyte string, except:
1164 - if the process' input coding system is no-conversion or raw-text,
1165 it is a unibyte string (the non-converted input), or else
1166 - if `default-enable-multibyte-characters' is nil, it is a unibyte
1167 string (the result of converting the decoded input multibyte
1168 string to unibyte with `string-make-unibyte'). */)
1169 (register Lisp_Object process
, Lisp_Object filter
)
1171 struct Lisp_Process
*p
;
1173 CHECK_PROCESS (process
);
1174 p
= XPROCESS (process
);
1176 /* Don't signal an error if the process' input file descriptor
1177 is closed. This could make debugging Lisp more difficult,
1178 for example when doing something like
1180 (setq process (start-process ...))
1182 (set-process-filter process ...) */
1185 filter
= Qinternal_default_process_filter
;
1189 if (EQ (filter
, Qt
) && !EQ (p
->status
, Qlisten
))
1190 delete_read_fd (p
->infd
);
1191 else if (EQ (p
->filter
, Qt
)
1192 /* Network or serial process not stopped: */
1193 && !EQ (p
->command
, Qt
))
1194 delete_read_fd (p
->infd
);
1197 pset_filter (p
, filter
);
1198 if (NETCONN1_P (p
) || SERIALCONN1_P (p
))
1199 pset_childp (p
, Fplist_put (p
->childp
, QCfilter
, filter
));
1200 setup_process_coding_systems (process
);
1204 DEFUN ("process-filter", Fprocess_filter
, Sprocess_filter
,
1206 doc
: /* Return the filter function of PROCESS.
1207 See `set-process-filter' for more info on filter functions. */)
1208 (register Lisp_Object process
)
1210 CHECK_PROCESS (process
);
1211 return XPROCESS (process
)->filter
;
1214 DEFUN ("set-process-sentinel", Fset_process_sentinel
, Sset_process_sentinel
,
1216 doc
: /* Give PROCESS the sentinel SENTINEL; nil for default.
1217 The sentinel is called as a function when the process changes state.
1218 It gets two arguments: the process, and a string describing the change. */)
1219 (register Lisp_Object process
, Lisp_Object sentinel
)
1221 struct Lisp_Process
*p
;
1223 CHECK_PROCESS (process
);
1224 p
= XPROCESS (process
);
1226 if (NILP (sentinel
))
1227 sentinel
= Qinternal_default_process_sentinel
;
1229 pset_sentinel (p
, sentinel
);
1230 if (NETCONN1_P (p
) || SERIALCONN1_P (p
))
1231 pset_childp (p
, Fplist_put (p
->childp
, QCsentinel
, sentinel
));
1235 DEFUN ("process-sentinel", Fprocess_sentinel
, Sprocess_sentinel
,
1237 doc
: /* Return the sentinel of PROCESS.
1238 See `set-process-sentinel' for more info on sentinels. */)
1239 (register Lisp_Object process
)
1241 CHECK_PROCESS (process
);
1242 return XPROCESS (process
)->sentinel
;
1245 DEFUN ("set-process-thread", Fset_process_thread
, Sset_process_thread
,
1248 (Lisp_Object process
, Lisp_Object thread
)
1250 struct Lisp_Process
*proc
;
1251 struct thread_state
*tstate
;
1253 CHECK_PROCESS (process
);
1258 CHECK_THREAD (thread
);
1259 tstate
= XTHREAD (thread
);
1262 proc
= XPROCESS (process
);
1263 proc
->thread
= thread
;
1264 if (proc
->infd
>= 0)
1265 fd_callback_info
[proc
->infd
].thread
= tstate
;
1266 if (proc
->outfd
>= 0)
1267 fd_callback_info
[proc
->outfd
].thread
= tstate
;
1272 DEFUN ("process-thread", Fprocess_thread
, Sprocess_thread
,
1275 (Lisp_Object process
)
1277 CHECK_PROCESS (process
);
1278 return XPROCESS (process
)->thread
;
1281 DEFUN ("set-process-window-size", Fset_process_window_size
,
1282 Sset_process_window_size
, 3, 3, 0,
1283 doc
: /* Tell PROCESS that it has logical window size HEIGHT and WIDTH. */)
1284 (register Lisp_Object process
, Lisp_Object height
, Lisp_Object width
)
1286 CHECK_PROCESS (process
);
1287 CHECK_RANGED_INTEGER (height
, 0, INT_MAX
);
1288 CHECK_RANGED_INTEGER (width
, 0, INT_MAX
);
1290 if (XPROCESS (process
)->infd
< 0
1291 || set_window_size (XPROCESS (process
)->infd
,
1292 XINT (height
), XINT (width
)) <= 0)
1298 DEFUN ("set-process-inherit-coding-system-flag",
1299 Fset_process_inherit_coding_system_flag
,
1300 Sset_process_inherit_coding_system_flag
, 2, 2, 0,
1301 doc
: /* Determine whether buffer of PROCESS will inherit coding-system.
1302 If the second argument FLAG is non-nil, then the variable
1303 `buffer-file-coding-system' of the buffer associated with PROCESS
1304 will be bound to the value of the coding system used to decode
1307 This is useful when the coding system specified for the process buffer
1308 leaves either the character code conversion or the end-of-line conversion
1309 unspecified, or if the coding system used to decode the process output
1310 is more appropriate for saving the process buffer.
1312 Binding the variable `inherit-process-coding-system' to non-nil before
1313 starting the process is an alternative way of setting the inherit flag
1314 for the process which will run.
1316 This function returns FLAG. */)
1317 (register Lisp_Object process
, Lisp_Object flag
)
1319 CHECK_PROCESS (process
);
1320 XPROCESS (process
)->inherit_coding_system_flag
= !NILP (flag
);
1324 DEFUN ("set-process-query-on-exit-flag",
1325 Fset_process_query_on_exit_flag
, Sset_process_query_on_exit_flag
,
1327 doc
: /* Specify if query is needed for PROCESS when Emacs is exited.
1328 If the second argument FLAG is non-nil, Emacs will query the user before
1329 exiting or killing a buffer if PROCESS is running. This function
1331 (register Lisp_Object process
, Lisp_Object flag
)
1333 CHECK_PROCESS (process
);
1334 XPROCESS (process
)->kill_without_query
= NILP (flag
);
1338 DEFUN ("process-query-on-exit-flag",
1339 Fprocess_query_on_exit_flag
, Sprocess_query_on_exit_flag
,
1341 doc
: /* Return the current value of query-on-exit flag for PROCESS. */)
1342 (register Lisp_Object process
)
1344 CHECK_PROCESS (process
);
1345 return (XPROCESS (process
)->kill_without_query
? Qnil
: Qt
);
1348 DEFUN ("process-contact", Fprocess_contact
, Sprocess_contact
,
1350 doc
: /* Return the contact info of PROCESS; t for a real child.
1351 For a network or serial connection, the value depends on the optional
1352 KEY arg. If KEY is nil, value is a cons cell of the form (HOST
1353 SERVICE) for a network connection or (PORT SPEED) for a serial
1354 connection. If KEY is t, the complete contact information for the
1355 connection is returned, else the specific value for the keyword KEY is
1356 returned. See `make-network-process' or `make-serial-process' for a
1357 list of keywords. */)
1358 (register Lisp_Object process
, Lisp_Object key
)
1360 Lisp_Object contact
;
1362 CHECK_PROCESS (process
);
1363 contact
= XPROCESS (process
)->childp
;
1365 #ifdef DATAGRAM_SOCKETS
1366 if (DATAGRAM_CONN_P (process
)
1367 && (EQ (key
, Qt
) || EQ (key
, QCremote
)))
1368 contact
= Fplist_put (contact
, QCremote
,
1369 Fprocess_datagram_address (process
));
1372 if ((!NETCONN_P (process
) && !SERIALCONN_P (process
)) || EQ (key
, Qt
))
1374 if (NILP (key
) && NETCONN_P (process
))
1375 return Fcons (Fplist_get (contact
, QChost
),
1376 Fcons (Fplist_get (contact
, QCservice
), Qnil
));
1377 if (NILP (key
) && SERIALCONN_P (process
))
1378 return Fcons (Fplist_get (contact
, QCport
),
1379 Fcons (Fplist_get (contact
, QCspeed
), Qnil
));
1380 return Fplist_get (contact
, key
);
1383 DEFUN ("process-plist", Fprocess_plist
, Sprocess_plist
,
1385 doc
: /* Return the plist of PROCESS. */)
1386 (register Lisp_Object process
)
1388 CHECK_PROCESS (process
);
1389 return XPROCESS (process
)->plist
;
1392 DEFUN ("set-process-plist", Fset_process_plist
, Sset_process_plist
,
1394 doc
: /* Replace the plist of PROCESS with PLIST. Returns PLIST. */)
1395 (register Lisp_Object process
, Lisp_Object plist
)
1397 CHECK_PROCESS (process
);
1400 pset_plist (XPROCESS (process
), plist
);
1404 #if 0 /* Turned off because we don't currently record this info
1405 in the process. Perhaps add it. */
1406 DEFUN ("process-connection", Fprocess_connection
, Sprocess_connection
, 1, 1, 0,
1407 doc
: /* Return the connection type of PROCESS.
1408 The value is nil for a pipe, t or `pty' for a pty, or `stream' for
1409 a socket connection. */)
1410 (Lisp_Object process
)
1412 return XPROCESS (process
)->type
;
1416 DEFUN ("process-type", Fprocess_type
, Sprocess_type
, 1, 1, 0,
1417 doc
: /* Return the connection type of PROCESS.
1418 The value is either the symbol `real', `network', or `serial'.
1419 PROCESS may be a process, a buffer, the name of a process or buffer, or
1420 nil, indicating the current buffer's process. */)
1421 (Lisp_Object process
)
1424 proc
= get_process (process
);
1425 return XPROCESS (proc
)->type
;
1428 DEFUN ("format-network-address", Fformat_network_address
, Sformat_network_address
,
1430 doc
: /* Convert network ADDRESS from internal format to a string.
1431 A 4 or 5 element vector represents an IPv4 address (with port number).
1432 An 8 or 9 element vector represents an IPv6 address (with port number).
1433 If optional second argument OMIT-PORT is non-nil, don't include a port
1434 number in the string, even when present in ADDRESS.
1435 Returns nil if format of ADDRESS is invalid. */)
1436 (Lisp_Object address
, Lisp_Object omit_port
)
1441 if (STRINGP (address
)) /* AF_LOCAL */
1444 if (VECTORP (address
)) /* AF_INET or AF_INET6 */
1446 register struct Lisp_Vector
*p
= XVECTOR (address
);
1447 ptrdiff_t size
= p
->header
.size
;
1448 Lisp_Object args
[10];
1451 if (size
== 4 || (size
== 5 && !NILP (omit_port
)))
1453 args
[0] = build_string ("%d.%d.%d.%d");
1458 args
[0] = build_string ("%d.%d.%d.%d:%d");
1461 else if (size
== 8 || (size
== 9 && !NILP (omit_port
)))
1463 args
[0] = build_string ("%x:%x:%x:%x:%x:%x:%x:%x");
1468 args
[0] = build_string ("[%x:%x:%x:%x:%x:%x:%x:%x]:%d");
1474 for (i
= 0; i
< nargs
; i
++)
1476 if (! RANGED_INTEGERP (0, p
->contents
[i
], 65535))
1479 if (nargs
<= 5 /* IPv4 */
1480 && i
< 4 /* host, not port */
1481 && XINT (p
->contents
[i
]) > 255)
1484 args
[i
+1] = p
->contents
[i
];
1487 return Fformat (nargs
+1, args
);
1490 if (CONSP (address
))
1492 Lisp_Object args
[2];
1493 args
[0] = build_string ("<Family %d>");
1494 args
[1] = Fcar (address
);
1495 return Fformat (2, args
);
1501 DEFUN ("process-list", Fprocess_list
, Sprocess_list
, 0, 0, 0,
1502 doc
: /* Return a list of all processes. */)
1505 return Fmapcar (Qcdr
, Vprocess_alist
);
1508 /* Starting asynchronous inferior processes. */
1510 static Lisp_Object
start_process_unwind (Lisp_Object proc
);
1512 DEFUN ("start-process", Fstart_process
, Sstart_process
, 3, MANY
, 0,
1513 doc
: /* Start a program in a subprocess. Return the process object for it.
1514 NAME is name for process. It is modified if necessary to make it unique.
1515 BUFFER is the buffer (or buffer name) to associate with the process.
1517 Process output (both standard output and standard error streams) goes
1518 at end of BUFFER, unless you specify an output stream or filter
1519 function to handle the output. BUFFER may also be nil, meaning that
1520 this process is not associated with any buffer.
1522 PROGRAM is the program file name. It is searched for in `exec-path'
1523 (which see). If nil, just associate a pty with the buffer. Remaining
1524 arguments are strings to give program as arguments.
1526 If you want to separate standard output from standard error, invoke
1527 the command through a shell and redirect one of them using the shell
1530 usage: (start-process NAME BUFFER PROGRAM &rest PROGRAM-ARGS) */)
1531 (ptrdiff_t nargs
, Lisp_Object
*args
)
1533 Lisp_Object buffer
, name
, program
, proc
, current_dir
, tem
;
1534 register unsigned char **new_argv
;
1536 ptrdiff_t count
= SPECPDL_INDEX ();
1540 buffer
= Fget_buffer_create (buffer
);
1542 /* Make sure that the child will be able to chdir to the current
1543 buffer's current directory, or its unhandled equivalent. We
1544 can't just have the child check for an error when it does the
1545 chdir, since it's in a vfork.
1547 We have to GCPRO around this because Fexpand_file_name and
1548 Funhandled_file_name_directory might call a file name handling
1549 function. The argument list is protected by the caller, so all
1550 we really have to worry about is buffer. */
1552 struct gcpro gcpro1
, gcpro2
;
1554 current_dir
= BVAR (current_buffer
, directory
);
1556 GCPRO2 (buffer
, current_dir
);
1558 current_dir
= Funhandled_file_name_directory (current_dir
);
1559 if (NILP (current_dir
))
1560 /* If the file name handler says that current_dir is unreachable, use
1561 a sensible default. */
1562 current_dir
= build_string ("~/");
1563 current_dir
= expand_and_dir_to_file (current_dir
, Qnil
);
1564 if (NILP (Ffile_accessible_directory_p (current_dir
)))
1565 report_file_error ("Setting current directory",
1566 Fcons (BVAR (current_buffer
, directory
), Qnil
));
1572 CHECK_STRING (name
);
1576 if (!NILP (program
))
1577 CHECK_STRING (program
);
1579 proc
= make_process (name
);
1580 /* If an error occurs and we can't start the process, we want to
1581 remove it from the process list. This means that each error
1582 check in create_process doesn't need to call remove_process
1583 itself; it's all taken care of here. */
1584 record_unwind_protect (start_process_unwind
, proc
);
1586 pset_childp (XPROCESS (proc
), Qt
);
1587 pset_plist (XPROCESS (proc
), Qnil
);
1588 pset_type (XPROCESS (proc
), Qreal
);
1589 pset_buffer (XPROCESS (proc
), buffer
);
1590 pset_sentinel (XPROCESS (proc
), Qinternal_default_process_sentinel
);
1591 pset_filter (XPROCESS (proc
), Qinternal_default_process_filter
);
1592 pset_command (XPROCESS (proc
), Flist (nargs
- 2, args
+ 2));
1595 /* AKA GNUTLS_INITSTAGE(proc). */
1596 XPROCESS (proc
)->gnutls_initstage
= GNUTLS_STAGE_EMPTY
;
1597 pset_gnutls_cred_type (XPROCESS (proc
), Qnil
);
1600 #ifdef ADAPTIVE_READ_BUFFERING
1601 XPROCESS (proc
)->adaptive_read_buffering
1602 = (NILP (Vprocess_adaptive_read_buffering
) ? 0
1603 : EQ (Vprocess_adaptive_read_buffering
, Qt
) ? 1 : 2);
1606 /* Make the process marker point into the process buffer (if any). */
1607 if (BUFFERP (buffer
))
1608 set_marker_both (XPROCESS (proc
)->mark
, buffer
,
1609 BUF_ZV (XBUFFER (buffer
)),
1610 BUF_ZV_BYTE (XBUFFER (buffer
)));
1613 /* Decide coding systems for communicating with the process. Here
1614 we don't setup the structure coding_system nor pay attention to
1615 unibyte mode. They are done in create_process. */
1617 /* Qt denotes we have not yet called Ffind_operation_coding_system. */
1618 Lisp_Object coding_systems
= Qt
;
1619 Lisp_Object val
, *args2
;
1620 struct gcpro gcpro1
, gcpro2
;
1622 val
= Vcoding_system_for_read
;
1625 args2
= alloca ((nargs
+ 1) * sizeof *args2
);
1626 args2
[0] = Qstart_process
;
1627 for (i
= 0; i
< nargs
; i
++) args2
[i
+ 1] = args
[i
];
1628 GCPRO2 (proc
, current_dir
);
1629 if (!NILP (program
))
1630 coding_systems
= Ffind_operation_coding_system (nargs
+ 1, args2
);
1632 if (CONSP (coding_systems
))
1633 val
= XCAR (coding_systems
);
1634 else if (CONSP (Vdefault_process_coding_system
))
1635 val
= XCAR (Vdefault_process_coding_system
);
1637 pset_decode_coding_system (XPROCESS (proc
), val
);
1639 val
= Vcoding_system_for_write
;
1642 if (EQ (coding_systems
, Qt
))
1644 args2
= alloca ((nargs
+ 1) * sizeof *args2
);
1645 args2
[0] = Qstart_process
;
1646 for (i
= 0; i
< nargs
; i
++) args2
[i
+ 1] = args
[i
];
1647 GCPRO2 (proc
, current_dir
);
1648 if (!NILP (program
))
1649 coding_systems
= Ffind_operation_coding_system (nargs
+ 1, args2
);
1652 if (CONSP (coding_systems
))
1653 val
= XCDR (coding_systems
);
1654 else if (CONSP (Vdefault_process_coding_system
))
1655 val
= XCDR (Vdefault_process_coding_system
);
1657 pset_encode_coding_system (XPROCESS (proc
), val
);
1658 /* Note: At this moment, the above coding system may leave
1659 text-conversion or eol-conversion unspecified. They will be
1660 decided after we read output from the process and decode it by
1661 some coding system, or just before we actually send a text to
1666 pset_decoding_buf (XPROCESS (proc
), empty_unibyte_string
);
1667 XPROCESS (proc
)->decoding_carryover
= 0;
1668 pset_encoding_buf (XPROCESS (proc
), empty_unibyte_string
);
1670 XPROCESS (proc
)->inherit_coding_system_flag
1671 = !(NILP (buffer
) || !inherit_process_coding_system
);
1673 if (!NILP (program
))
1675 /* If program file name is not absolute, search our path for it.
1676 Put the name we will really use in TEM. */
1677 if (!IS_DIRECTORY_SEP (SREF (program
, 0))
1678 && !(SCHARS (program
) > 1
1679 && IS_DEVICE_SEP (SREF (program
, 1))))
1681 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
;
1684 GCPRO4 (name
, program
, buffer
, current_dir
);
1685 openp (Vexec_path
, program
, Vexec_suffixes
, &tem
, make_number (X_OK
));
1688 report_file_error ("Searching for program", Fcons (program
, Qnil
));
1689 tem
= Fexpand_file_name (tem
, Qnil
);
1693 if (!NILP (Ffile_directory_p (program
)))
1694 error ("Specified program for new process is a directory");
1698 /* If program file name starts with /: for quoting a magic name,
1700 if (SBYTES (tem
) > 2 && SREF (tem
, 0) == '/'
1701 && SREF (tem
, 1) == ':')
1702 tem
= Fsubstring (tem
, make_number (2), Qnil
);
1705 Lisp_Object arg_encoding
= Qnil
;
1706 struct gcpro gcpro1
;
1709 /* Encode the file name and put it in NEW_ARGV.
1710 That's where the child will use it to execute the program. */
1711 tem
= Fcons (ENCODE_FILE (tem
), Qnil
);
1713 /* Here we encode arguments by the coding system used for sending
1714 data to the process. We don't support using different coding
1715 systems for encoding arguments and for encoding data sent to the
1718 for (i
= 3; i
< nargs
; i
++)
1720 tem
= Fcons (args
[i
], tem
);
1721 CHECK_STRING (XCAR (tem
));
1722 if (STRING_MULTIBYTE (XCAR (tem
)))
1724 if (NILP (arg_encoding
))
1725 arg_encoding
= (complement_process_encoding_system
1726 (XPROCESS (proc
)->encode_coding_system
));
1728 code_convert_string_norecord
1729 (XCAR (tem
), arg_encoding
, 1));
1736 /* Now that everything is encoded we can collect the strings into
1738 new_argv
= alloca ((nargs
- 1) * sizeof *new_argv
);
1739 new_argv
[nargs
- 2] = 0;
1741 for (i
= nargs
- 2; i
-- != 0; )
1743 new_argv
[i
] = SDATA (XCAR (tem
));
1747 create_process (proc
, (char **) new_argv
, current_dir
);
1752 return unbind_to (count
, proc
);
1755 /* This function is the unwind_protect form for Fstart_process. If
1756 PROC doesn't have its pid set, then we know someone has signaled
1757 an error and the process wasn't started successfully, so we should
1758 remove it from the process list. */
1760 start_process_unwind (Lisp_Object proc
)
1762 if (!PROCESSP (proc
))
1765 /* Was PROC started successfully?
1766 -2 is used for a pty with no process, eg for gdb. */
1767 if (XPROCESS (proc
)->pid
<= 0 && XPROCESS (proc
)->pid
!= -2)
1768 remove_process (proc
);
1774 create_process_1 (struct atimer
*timer
)
1776 /* Nothing to do. */
1781 create_process (Lisp_Object process
, char **new_argv
, Lisp_Object current_dir
)
1783 int inchannel
, outchannel
;
1787 int wait_child_setup
[2];
1789 int forkin
, forkout
;
1791 Lisp_Object lisp_pty_name
= Qnil
;
1792 Lisp_Object encoded_current_dir
;
1794 inchannel
= outchannel
= -1;
1797 if (!NILP (Vprocess_connection_type
))
1798 outchannel
= inchannel
= allocate_pty ();
1802 #if ! defined (USG) || defined (USG_SUBTTY_WORKS)
1803 /* On most USG systems it does not work to open the pty's tty here,
1804 then close it and reopen it in the child. */
1805 /* Don't let this terminal become our controlling terminal
1806 (in case we don't have one). */
1807 forkout
= forkin
= emacs_open (pty_name
, O_RDWR
| O_NOCTTY
, 0);
1809 report_file_error ("Opening pty", Qnil
);
1811 forkin
= forkout
= -1;
1812 #endif /* not USG, or USG_SUBTTY_WORKS */
1814 lisp_pty_name
= build_string (pty_name
);
1817 #endif /* HAVE_PTYS */
1822 report_file_error ("Creating pipe", Qnil
);
1828 emacs_close (inchannel
);
1829 emacs_close (forkout
);
1830 report_file_error ("Creating pipe", Qnil
);
1840 tem
= pipe (wait_child_setup
);
1842 report_file_error ("Creating pipe", Qnil
);
1843 tem
= fcntl (wait_child_setup
[1], F_GETFD
, 0);
1845 tem
= fcntl (wait_child_setup
[1], F_SETFD
, tem
| FD_CLOEXEC
);
1848 emacs_close (wait_child_setup
[0]);
1849 emacs_close (wait_child_setup
[1]);
1850 report_file_error ("Setting file descriptor flags", Qnil
);
1855 fcntl (inchannel
, F_SETFL
, O_NONBLOCK
);
1856 fcntl (outchannel
, F_SETFL
, O_NONBLOCK
);
1858 /* Record this as an active process, with its channels.
1859 As a result, child_setup will close Emacs's side of the pipes. */
1860 chan_process
[inchannel
] = process
;
1861 XPROCESS (process
)->infd
= inchannel
;
1862 XPROCESS (process
)->outfd
= outchannel
;
1864 /* Previously we recorded the tty descriptor used in the subprocess.
1865 It was only used for getting the foreground tty process, so now
1866 we just reopen the device (see emacs_get_tty_pgrp) as this is
1867 more portable (see USG_SUBTTY_WORKS above). */
1869 XPROCESS (process
)->pty_flag
= pty_flag
;
1870 pset_status (XPROCESS (process
), Qrun
);
1872 add_process_read_fd (inchannel
);
1874 /* This may signal an error. */
1875 setup_process_coding_systems (process
);
1877 encoded_current_dir
= ENCODE_FILE (current_dir
);
1880 block_child_signal ();
1883 /* vfork, and prevent local vars from being clobbered by the vfork. */
1885 Lisp_Object
volatile encoded_current_dir_volatile
= encoded_current_dir
;
1886 Lisp_Object
volatile lisp_pty_name_volatile
= lisp_pty_name
;
1887 Lisp_Object
volatile process_volatile
= process
;
1888 bool volatile pty_flag_volatile
= pty_flag
;
1889 char **volatile new_argv_volatile
= new_argv
;
1890 int volatile forkin_volatile
= forkin
;
1891 int volatile forkout_volatile
= forkout
;
1892 int volatile wait_child_setup_0_volatile
= wait_child_setup
[0];
1893 int volatile wait_child_setup_1_volatile
= wait_child_setup
[1];
1897 encoded_current_dir
= encoded_current_dir_volatile
;
1898 lisp_pty_name
= lisp_pty_name_volatile
;
1899 process
= process_volatile
;
1900 pty_flag
= pty_flag_volatile
;
1901 new_argv
= new_argv_volatile
;
1902 forkin
= forkin_volatile
;
1903 forkout
= forkout_volatile
;
1904 wait_child_setup
[0] = wait_child_setup_0_volatile
;
1905 wait_child_setup
[1] = wait_child_setup_1_volatile
;
1909 #endif /* not WINDOWSNT */
1911 int xforkin
= forkin
;
1912 int xforkout
= forkout
;
1914 /* Make the pty be the controlling terminal of the process. */
1916 /* First, disconnect its current controlling terminal. */
1917 /* We tried doing setsid only if pty_flag, but it caused
1918 process_set_signal to fail on SGI when using a pipe. */
1920 /* Make the pty's terminal the controlling terminal. */
1921 if (pty_flag
&& xforkin
>= 0)
1924 /* We ignore the return value
1925 because faith@cs.unc.edu says that is necessary on Linux. */
1926 ioctl (xforkin
, TIOCSCTTY
, 0);
1929 #if defined (LDISC1)
1930 if (pty_flag
&& xforkin
>= 0)
1933 tcgetattr (xforkin
, &t
);
1935 if (tcsetattr (xforkin
, TCSANOW
, &t
) < 0)
1936 emacs_write (1, "create_process/tcsetattr LDISC1 failed\n", 39);
1939 #if defined (NTTYDISC) && defined (TIOCSETD)
1940 if (pty_flag
&& xforkin
>= 0)
1942 /* Use new line discipline. */
1943 int ldisc
= NTTYDISC
;
1944 ioctl (xforkin
, TIOCSETD
, &ldisc
);
1949 /* In 4.3BSD, the TIOCSPGRP bug has been fixed, and now you
1950 can do TIOCSPGRP only to the process's controlling tty. */
1953 /* I wonder: would just ioctl (0, TIOCNOTTY, 0) work here?
1954 I can't test it since I don't have 4.3. */
1955 int j
= emacs_open ("/dev/tty", O_RDWR
, 0);
1958 ioctl (j
, TIOCNOTTY
, 0);
1962 #endif /* TIOCNOTTY */
1964 #if !defined (DONT_REOPEN_PTY)
1965 /*** There is a suggestion that this ought to be a
1966 conditional on TIOCSPGRP, or !defined TIOCSCTTY.
1967 Trying the latter gave the wrong results on Debian GNU/Linux 1.1;
1968 that system does seem to need this code, even though
1969 both TIOCSCTTY is defined. */
1970 /* Now close the pty (if we had it open) and reopen it.
1971 This makes the pty the controlling terminal of the subprocess. */
1975 /* I wonder if emacs_close (emacs_open (pty_name, ...))
1978 emacs_close (xforkin
);
1979 xforkout
= xforkin
= emacs_open (pty_name
, O_RDWR
, 0);
1983 emacs_write (1, "Couldn't open the pty terminal ", 31);
1984 emacs_write (1, pty_name
, strlen (pty_name
));
1985 emacs_write (1, "\n", 1);
1990 #endif /* not DONT_REOPEN_PTY */
1992 #ifdef SETUP_SLAVE_PTY
1997 #endif /* SETUP_SLAVE_PTY */
1999 /* On AIX, we've disabled SIGHUP above once we start a child on a pty.
2000 Now reenable it in the child, so it will die when we want it to. */
2002 signal (SIGHUP
, SIG_DFL
);
2004 #endif /* HAVE_PTYS */
2006 signal (SIGINT
, SIG_DFL
);
2007 signal (SIGQUIT
, SIG_DFL
);
2009 /* Emacs ignores SIGPIPE, but the child should not. */
2010 signal (SIGPIPE
, SIG_DFL
);
2012 /* Stop blocking SIGCHLD in the child. */
2013 unblock_child_signal ();
2016 child_setup_tty (xforkout
);
2018 pid
= child_setup (xforkin
, xforkout
, xforkout
,
2019 new_argv
, 1, encoded_current_dir
);
2020 #else /* not WINDOWSNT */
2021 emacs_close (wait_child_setup
[0]);
2022 child_setup (xforkin
, xforkout
, xforkout
,
2023 new_argv
, 1, encoded_current_dir
);
2024 #endif /* not WINDOWSNT */
2027 /* Back in the parent process. */
2029 XPROCESS (process
)->pid
= pid
;
2031 XPROCESS (process
)->alive
= 1;
2033 /* Stop blocking in the parent. */
2034 unblock_child_signal ();
2040 emacs_close (forkin
);
2041 if (forkin
!= forkout
&& forkout
>= 0)
2042 emacs_close (forkout
);
2046 /* vfork succeeded. */
2049 register_child (pid
, inchannel
);
2050 #endif /* WINDOWSNT */
2052 /* If the subfork execv fails, and it exits,
2053 this close hangs. I don't know why.
2054 So have an interrupt jar it loose. */
2056 struct atimer
*timer
;
2057 EMACS_TIME offset
= make_emacs_time (1, 0);
2060 timer
= start_atimer (ATIMER_RELATIVE
, offset
, create_process_1
, 0);
2063 emacs_close (forkin
);
2065 cancel_atimer (timer
);
2069 if (forkin
!= forkout
&& forkout
>= 0)
2070 emacs_close (forkout
);
2072 pset_tty_name (XPROCESS (process
), lisp_pty_name
);
2075 /* Wait for child_setup to complete in case that vfork is
2076 actually defined as fork. The descriptor wait_child_setup[1]
2077 of a pipe is closed at the child side either by close-on-exec
2078 on successful execve or the _exit call in child_setup. */
2082 emacs_close (wait_child_setup
[1]);
2083 emacs_read (wait_child_setup
[0], &dummy
, 1);
2084 emacs_close (wait_child_setup
[0]);
2089 /* Now generate the error if vfork failed. */
2091 report_file_error ("Doing vfork", Qnil
);
2095 create_pty (Lisp_Object process
)
2097 int inchannel
, outchannel
;
2100 inchannel
= outchannel
= -1;
2103 if (!NILP (Vprocess_connection_type
))
2104 outchannel
= inchannel
= allocate_pty ();
2108 #if ! defined (USG) || defined (USG_SUBTTY_WORKS)
2109 /* On most USG systems it does not work to open the pty's tty here,
2110 then close it and reopen it in the child. */
2111 /* Don't let this terminal become our controlling terminal
2112 (in case we don't have one). */
2113 int forkout
= emacs_open (pty_name
, O_RDWR
| O_NOCTTY
, 0);
2115 report_file_error ("Opening pty", Qnil
);
2116 #if defined (DONT_REOPEN_PTY)
2117 /* In the case that vfork is defined as fork, the parent process
2118 (Emacs) may send some data before the child process completes
2119 tty options setup. So we setup tty before forking. */
2120 child_setup_tty (forkout
);
2121 #endif /* DONT_REOPEN_PTY */
2122 #endif /* not USG, or USG_SUBTTY_WORKS */
2125 #endif /* HAVE_PTYS */
2127 fcntl (inchannel
, F_SETFL
, O_NONBLOCK
);
2128 fcntl (outchannel
, F_SETFL
, O_NONBLOCK
);
2130 /* Record this as an active process, with its channels.
2131 As a result, child_setup will close Emacs's side of the pipes. */
2132 chan_process
[inchannel
] = process
;
2133 XPROCESS (process
)->infd
= inchannel
;
2134 XPROCESS (process
)->outfd
= outchannel
;
2136 /* Previously we recorded the tty descriptor used in the subprocess.
2137 It was only used for getting the foreground tty process, so now
2138 we just reopen the device (see emacs_get_tty_pgrp) as this is
2139 more portable (see USG_SUBTTY_WORKS above). */
2141 XPROCESS (process
)->pty_flag
= pty_flag
;
2142 pset_status (XPROCESS (process
), Qrun
);
2143 setup_process_coding_systems (process
);
2145 add_process_read_fd (inchannel
);
2147 XPROCESS (process
)->pid
= -2;
2150 pset_tty_name (XPROCESS (process
), build_string (pty_name
));
2153 pset_tty_name (XPROCESS (process
), Qnil
);
2157 /* Convert an internal struct sockaddr to a lisp object (vector or string).
2158 The address family of sa is not included in the result. */
2161 conv_sockaddr_to_lisp (struct sockaddr
*sa
, int len
)
2163 Lisp_Object address
;
2166 register struct Lisp_Vector
*p
;
2168 /* Workaround for a bug in getsockname on BSD: Names bound to
2169 sockets in the UNIX domain are inaccessible; getsockname returns
2170 a zero length name. */
2171 if (len
< offsetof (struct sockaddr
, sa_family
) + sizeof (sa
->sa_family
))
2172 return empty_unibyte_string
;
2174 switch (sa
->sa_family
)
2178 struct sockaddr_in
*sin
= (struct sockaddr_in
*) sa
;
2179 len
= sizeof (sin
->sin_addr
) + 1;
2180 address
= Fmake_vector (make_number (len
), Qnil
);
2181 p
= XVECTOR (address
);
2182 p
->contents
[--len
] = make_number (ntohs (sin
->sin_port
));
2183 cp
= (unsigned char *) &sin
->sin_addr
;
2189 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*) sa
;
2190 uint16_t *ip6
= (uint16_t *) &sin6
->sin6_addr
;
2191 len
= sizeof (sin6
->sin6_addr
)/2 + 1;
2192 address
= Fmake_vector (make_number (len
), Qnil
);
2193 p
= XVECTOR (address
);
2194 p
->contents
[--len
] = make_number (ntohs (sin6
->sin6_port
));
2195 for (i
= 0; i
< len
; i
++)
2196 p
->contents
[i
] = make_number (ntohs (ip6
[i
]));
2200 #ifdef HAVE_LOCAL_SOCKETS
2203 struct sockaddr_un
*sockun
= (struct sockaddr_un
*) sa
;
2204 for (i
= 0; i
< sizeof (sockun
->sun_path
); i
++)
2205 if (sockun
->sun_path
[i
] == 0)
2207 return make_unibyte_string (sockun
->sun_path
, i
);
2211 len
-= offsetof (struct sockaddr
, sa_family
) + sizeof (sa
->sa_family
);
2212 address
= Fcons (make_number (sa
->sa_family
),
2213 Fmake_vector (make_number (len
), Qnil
));
2214 p
= XVECTOR (XCDR (address
));
2215 cp
= (unsigned char *) &sa
->sa_family
+ sizeof (sa
->sa_family
);
2221 p
->contents
[i
++] = make_number (*cp
++);
2227 /* Get family and required size for sockaddr structure to hold ADDRESS. */
2230 get_lisp_to_sockaddr_size (Lisp_Object address
, int *familyp
)
2232 register struct Lisp_Vector
*p
;
2234 if (VECTORP (address
))
2236 p
= XVECTOR (address
);
2237 if (p
->header
.size
== 5)
2240 return sizeof (struct sockaddr_in
);
2243 else if (p
->header
.size
== 9)
2245 *familyp
= AF_INET6
;
2246 return sizeof (struct sockaddr_in6
);
2250 #ifdef HAVE_LOCAL_SOCKETS
2251 else if (STRINGP (address
))
2253 *familyp
= AF_LOCAL
;
2254 return sizeof (struct sockaddr_un
);
2257 else if (CONSP (address
) && TYPE_RANGED_INTEGERP (int, XCAR (address
))
2258 && VECTORP (XCDR (address
)))
2260 struct sockaddr
*sa
;
2261 *familyp
= XINT (XCAR (address
));
2262 p
= XVECTOR (XCDR (address
));
2263 return p
->header
.size
+ sizeof (sa
->sa_family
);
2268 /* Convert an address object (vector or string) to an internal sockaddr.
2270 The address format has been basically validated by
2271 get_lisp_to_sockaddr_size, but this does not mean FAMILY is valid;
2272 it could have come from user data. So if FAMILY is not valid,
2273 we return after zeroing *SA. */
2276 conv_lisp_to_sockaddr (int family
, Lisp_Object address
, struct sockaddr
*sa
, int len
)
2278 register struct Lisp_Vector
*p
;
2279 register unsigned char *cp
= NULL
;
2283 memset (sa
, 0, len
);
2285 if (VECTORP (address
))
2287 p
= XVECTOR (address
);
2288 if (family
== AF_INET
)
2290 struct sockaddr_in
*sin
= (struct sockaddr_in
*) sa
;
2291 len
= sizeof (sin
->sin_addr
) + 1;
2292 hostport
= XINT (p
->contents
[--len
]);
2293 sin
->sin_port
= htons (hostport
);
2294 cp
= (unsigned char *)&sin
->sin_addr
;
2295 sa
->sa_family
= family
;
2298 else if (family
== AF_INET6
)
2300 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*) sa
;
2301 uint16_t *ip6
= (uint16_t *)&sin6
->sin6_addr
;
2302 len
= sizeof (sin6
->sin6_addr
) + 1;
2303 hostport
= XINT (p
->contents
[--len
]);
2304 sin6
->sin6_port
= htons (hostport
);
2305 for (i
= 0; i
< len
; i
++)
2306 if (INTEGERP (p
->contents
[i
]))
2308 int j
= XFASTINT (p
->contents
[i
]) & 0xffff;
2311 sa
->sa_family
= family
;
2318 else if (STRINGP (address
))
2320 #ifdef HAVE_LOCAL_SOCKETS
2321 if (family
== AF_LOCAL
)
2323 struct sockaddr_un
*sockun
= (struct sockaddr_un
*) sa
;
2324 cp
= SDATA (address
);
2325 for (i
= 0; i
< sizeof (sockun
->sun_path
) && *cp
; i
++)
2326 sockun
->sun_path
[i
] = *cp
++;
2327 sa
->sa_family
= family
;
2334 p
= XVECTOR (XCDR (address
));
2335 cp
= (unsigned char *)sa
+ sizeof (sa
->sa_family
);
2338 for (i
= 0; i
< len
; i
++)
2339 if (INTEGERP (p
->contents
[i
]))
2340 *cp
++ = XFASTINT (p
->contents
[i
]) & 0xff;
2343 #ifdef DATAGRAM_SOCKETS
2344 DEFUN ("process-datagram-address", Fprocess_datagram_address
, Sprocess_datagram_address
,
2346 doc
: /* Get the current datagram address associated with PROCESS. */)
2347 (Lisp_Object process
)
2351 CHECK_PROCESS (process
);
2353 if (!DATAGRAM_CONN_P (process
))
2356 channel
= XPROCESS (process
)->infd
;
2357 return conv_sockaddr_to_lisp (datagram_address
[channel
].sa
,
2358 datagram_address
[channel
].len
);
2361 DEFUN ("set-process-datagram-address", Fset_process_datagram_address
, Sset_process_datagram_address
,
2363 doc
: /* Set the datagram address for PROCESS to ADDRESS.
2364 Returns nil upon error setting address, ADDRESS otherwise. */)
2365 (Lisp_Object process
, Lisp_Object address
)
2370 CHECK_PROCESS (process
);
2372 if (!DATAGRAM_CONN_P (process
))
2375 channel
= XPROCESS (process
)->infd
;
2377 len
= get_lisp_to_sockaddr_size (address
, &family
);
2378 if (len
== 0 || datagram_address
[channel
].len
!= len
)
2380 conv_lisp_to_sockaddr (family
, address
, datagram_address
[channel
].sa
, len
);
2386 static const struct socket_options
{
2387 /* The name of this option. Should be lowercase version of option
2388 name without SO_ prefix. */
2390 /* Option level SOL_... */
2392 /* Option number SO_... */
2394 enum { SOPT_UNKNOWN
, SOPT_BOOL
, SOPT_INT
, SOPT_IFNAME
, SOPT_LINGER
} opttype
;
2395 enum { OPIX_NONE
=0, OPIX_MISC
=1, OPIX_REUSEADDR
=2 } optbit
;
2396 } socket_options
[] =
2398 #ifdef SO_BINDTODEVICE
2399 { ":bindtodevice", SOL_SOCKET
, SO_BINDTODEVICE
, SOPT_IFNAME
, OPIX_MISC
},
2402 { ":broadcast", SOL_SOCKET
, SO_BROADCAST
, SOPT_BOOL
, OPIX_MISC
},
2405 { ":dontroute", SOL_SOCKET
, SO_DONTROUTE
, SOPT_BOOL
, OPIX_MISC
},
2408 { ":keepalive", SOL_SOCKET
, SO_KEEPALIVE
, SOPT_BOOL
, OPIX_MISC
},
2411 { ":linger", SOL_SOCKET
, SO_LINGER
, SOPT_LINGER
, OPIX_MISC
},
2414 { ":oobinline", SOL_SOCKET
, SO_OOBINLINE
, SOPT_BOOL
, OPIX_MISC
},
2417 { ":priority", SOL_SOCKET
, SO_PRIORITY
, SOPT_INT
, OPIX_MISC
},
2420 { ":reuseaddr", SOL_SOCKET
, SO_REUSEADDR
, SOPT_BOOL
, OPIX_REUSEADDR
},
2422 { 0, 0, 0, SOPT_UNKNOWN
, OPIX_NONE
}
2425 /* Set option OPT to value VAL on socket S.
2427 Returns (1<<socket_options[OPT].optbit) if option is known, 0 otherwise.
2428 Signals an error if setting a known option fails.
2432 set_socket_option (int s
, Lisp_Object opt
, Lisp_Object val
)
2435 const struct socket_options
*sopt
;
2440 name
= SSDATA (SYMBOL_NAME (opt
));
2441 for (sopt
= socket_options
; sopt
->name
; sopt
++)
2442 if (strcmp (name
, sopt
->name
) == 0)
2445 switch (sopt
->opttype
)
2450 optval
= NILP (val
) ? 0 : 1;
2451 ret
= setsockopt (s
, sopt
->optlevel
, sopt
->optnum
,
2452 &optval
, sizeof (optval
));
2459 if (TYPE_RANGED_INTEGERP (int, val
))
2460 optval
= XINT (val
);
2462 error ("Bad option value for %s", name
);
2463 ret
= setsockopt (s
, sopt
->optlevel
, sopt
->optnum
,
2464 &optval
, sizeof (optval
));
2468 #ifdef SO_BINDTODEVICE
2471 char devname
[IFNAMSIZ
+1];
2473 /* This is broken, at least in the Linux 2.4 kernel.
2474 To unbind, the arg must be a zero integer, not the empty string.
2475 This should work on all systems. KFS. 2003-09-23. */
2476 memset (devname
, 0, sizeof devname
);
2479 char *arg
= SSDATA (val
);
2480 int len
= min (strlen (arg
), IFNAMSIZ
);
2481 memcpy (devname
, arg
, len
);
2483 else if (!NILP (val
))
2484 error ("Bad option value for %s", name
);
2485 ret
= setsockopt (s
, sopt
->optlevel
, sopt
->optnum
,
2494 struct linger linger
;
2497 linger
.l_linger
= 0;
2498 if (TYPE_RANGED_INTEGERP (int, val
))
2499 linger
.l_linger
= XINT (val
);
2501 linger
.l_onoff
= NILP (val
) ? 0 : 1;
2502 ret
= setsockopt (s
, sopt
->optlevel
, sopt
->optnum
,
2503 &linger
, sizeof (linger
));
2513 report_file_error ("Cannot set network option",
2514 Fcons (opt
, Fcons (val
, Qnil
)));
2515 return (1 << sopt
->optbit
);
2519 DEFUN ("set-network-process-option",
2520 Fset_network_process_option
, Sset_network_process_option
,
2522 doc
: /* For network process PROCESS set option OPTION to value VALUE.
2523 See `make-network-process' for a list of options and values.
2524 If optional fourth arg NO-ERROR is non-nil, don't signal an error if
2525 OPTION is not a supported option, return nil instead; otherwise return t. */)
2526 (Lisp_Object process
, Lisp_Object option
, Lisp_Object value
, Lisp_Object no_error
)
2529 struct Lisp_Process
*p
;
2531 CHECK_PROCESS (process
);
2532 p
= XPROCESS (process
);
2533 if (!NETCONN1_P (p
))
2534 error ("Process is not a network process");
2538 error ("Process is not running");
2540 if (set_socket_option (s
, option
, value
))
2542 pset_childp (p
, Fplist_put (p
->childp
, option
, value
));
2546 if (NILP (no_error
))
2547 error ("Unknown or unsupported option");
2553 DEFUN ("serial-process-configure",
2554 Fserial_process_configure
,
2555 Sserial_process_configure
,
2557 doc
: /* Configure speed, bytesize, etc. of a serial process.
2559 Arguments are specified as keyword/argument pairs. Attributes that
2560 are not given are re-initialized from the process's current
2561 configuration (available via the function `process-contact') or set to
2562 reasonable default values. The following arguments are defined:
2568 -- Any of these arguments can be given to identify the process that is
2569 to be configured. If none of these arguments is given, the current
2570 buffer's process is used.
2572 :speed SPEED -- SPEED is the speed of the serial port in bits per
2573 second, also called baud rate. Any value can be given for SPEED, but
2574 most serial ports work only at a few defined values between 1200 and
2575 115200, with 9600 being the most common value. If SPEED is nil, the
2576 serial port is not configured any further, i.e., all other arguments
2577 are ignored. This may be useful for special serial ports such as
2578 Bluetooth-to-serial converters which can only be configured through AT
2579 commands. A value of nil for SPEED can be used only when passed
2580 through `make-serial-process' or `serial-term'.
2582 :bytesize BYTESIZE -- BYTESIZE is the number of bits per byte, which
2583 can be 7 or 8. If BYTESIZE is not given or nil, a value of 8 is used.
2585 :parity PARITY -- PARITY can be nil (don't use parity), the symbol
2586 `odd' (use odd parity), or the symbol `even' (use even parity). If
2587 PARITY is not given, no parity is used.
2589 :stopbits STOPBITS -- STOPBITS is the number of stopbits used to
2590 terminate a byte transmission. STOPBITS can be 1 or 2. If STOPBITS
2591 is not given or nil, 1 stopbit is used.
2593 :flowcontrol FLOWCONTROL -- FLOWCONTROL determines the type of
2594 flowcontrol to be used, which is either nil (don't use flowcontrol),
2595 the symbol `hw' (use RTS/CTS hardware flowcontrol), or the symbol `sw'
2596 \(use XON/XOFF software flowcontrol). If FLOWCONTROL is not given, no
2597 flowcontrol is used.
2599 `serial-process-configure' is called by `make-serial-process' for the
2600 initial configuration of the serial port.
2604 \(serial-process-configure :process "/dev/ttyS0" :speed 1200)
2606 \(serial-process-configure
2607 :buffer "COM1" :stopbits 1 :parity 'odd :flowcontrol 'hw)
2609 \(serial-process-configure :port "\\\\.\\COM13" :bytesize 7)
2611 usage: (serial-process-configure &rest ARGS) */)
2612 (ptrdiff_t nargs
, Lisp_Object
*args
)
2614 struct Lisp_Process
*p
;
2615 Lisp_Object contact
= Qnil
;
2616 Lisp_Object proc
= Qnil
;
2617 struct gcpro gcpro1
;
2619 contact
= Flist (nargs
, args
);
2622 proc
= Fplist_get (contact
, QCprocess
);
2624 proc
= Fplist_get (contact
, QCname
);
2626 proc
= Fplist_get (contact
, QCbuffer
);
2628 proc
= Fplist_get (contact
, QCport
);
2629 proc
= get_process (proc
);
2630 p
= XPROCESS (proc
);
2631 if (!EQ (p
->type
, Qserial
))
2632 error ("Not a serial process");
2634 if (NILP (Fplist_get (p
->childp
, QCspeed
)))
2640 serial_configure (p
, contact
);
2646 /* Used by make-serial-process to recover from errors. */
2648 make_serial_process_unwind (Lisp_Object proc
)
2650 if (!PROCESSP (proc
))
2652 remove_process (proc
);
2656 DEFUN ("make-serial-process", Fmake_serial_process
, Smake_serial_process
,
2658 doc
: /* Create and return a serial port process.
2660 In Emacs, serial port connections are represented by process objects,
2661 so input and output work as for subprocesses, and `delete-process'
2662 closes a serial port connection. However, a serial process has no
2663 process id, it cannot be signaled, and the status codes are different
2664 from normal processes.
2666 `make-serial-process' creates a process and a buffer, on which you
2667 probably want to use `process-send-string'. Try \\[serial-term] for
2668 an interactive terminal. See below for examples.
2670 Arguments are specified as keyword/argument pairs. The following
2671 arguments are defined:
2673 :port PORT -- (mandatory) PORT is the path or name of the serial port.
2674 For example, this could be "/dev/ttyS0" on Unix. On Windows, this
2675 could be "COM1", or "\\\\.\\COM10" for ports higher than COM9 (double
2676 the backslashes in strings).
2678 :speed SPEED -- (mandatory) is handled by `serial-process-configure',
2679 which this function calls.
2681 :name NAME -- NAME is the name of the process. If NAME is not given,
2682 the value of PORT is used.
2684 :buffer BUFFER -- BUFFER is the buffer (or buffer-name) to associate
2685 with the process. Process output goes at the end of that buffer,
2686 unless you specify an output stream or filter function to handle the
2687 output. If BUFFER is not given, the value of NAME is used.
2689 :coding CODING -- If CODING is a symbol, it specifies the coding
2690 system used for both reading and writing for this process. If CODING
2691 is a cons (DECODING . ENCODING), DECODING is used for reading, and
2692 ENCODING is used for writing.
2694 :noquery BOOL -- When exiting Emacs, query the user if BOOL is nil and
2695 the process is running. If BOOL is not given, query before exiting.
2697 :stop BOOL -- Start process in the `stopped' state if BOOL is non-nil.
2698 In the stopped state, a serial process does not accept incoming data,
2699 but you can send outgoing data. The stopped state is cleared by
2700 `continue-process' and set by `stop-process'.
2702 :filter FILTER -- Install FILTER as the process filter.
2704 :sentinel SENTINEL -- Install SENTINEL as the process sentinel.
2706 :plist PLIST -- Install PLIST as the initial plist of the process.
2712 -- This function calls `serial-process-configure' to handle these
2715 The original argument list, possibly modified by later configuration,
2716 is available via the function `process-contact'.
2720 \(make-serial-process :port "/dev/ttyS0" :speed 9600)
2722 \(make-serial-process :port "COM1" :speed 115200 :stopbits 2)
2724 \(make-serial-process :port "\\\\.\\COM13" :speed 1200 :bytesize 7 :parity 'odd)
2726 \(make-serial-process :port "/dev/tty.BlueConsole-SPP-1" :speed nil)
2728 usage: (make-serial-process &rest ARGS) */)
2729 (ptrdiff_t nargs
, Lisp_Object
*args
)
2732 Lisp_Object proc
, contact
, port
;
2733 struct Lisp_Process
*p
;
2734 struct gcpro gcpro1
;
2735 Lisp_Object name
, buffer
;
2736 Lisp_Object tem
, val
;
2737 ptrdiff_t specpdl_count
;
2742 contact
= Flist (nargs
, args
);
2745 port
= Fplist_get (contact
, QCport
);
2747 error ("No port specified");
2748 CHECK_STRING (port
);
2750 if (NILP (Fplist_member (contact
, QCspeed
)))
2751 error (":speed not specified");
2752 if (!NILP (Fplist_get (contact
, QCspeed
)))
2753 CHECK_NUMBER (Fplist_get (contact
, QCspeed
));
2755 name
= Fplist_get (contact
, QCname
);
2758 CHECK_STRING (name
);
2759 proc
= make_process (name
);
2760 specpdl_count
= SPECPDL_INDEX ();
2761 record_unwind_protect (make_serial_process_unwind
, proc
);
2762 p
= XPROCESS (proc
);
2764 fd
= serial_open (SSDATA (port
));
2769 chan_process
[fd
] = proc
;
2771 buffer
= Fplist_get (contact
, QCbuffer
);
2774 buffer
= Fget_buffer_create (buffer
);
2775 pset_buffer (p
, buffer
);
2777 pset_childp (p
, contact
);
2778 pset_plist (p
, Fcopy_sequence (Fplist_get (contact
, QCplist
)));
2779 pset_type (p
, Qserial
);
2780 pset_sentinel (p
, Fplist_get (contact
, QCsentinel
));
2781 pset_filter (p
, Fplist_get (contact
, QCfilter
));
2783 if (tem
= Fplist_get (contact
, QCnoquery
), !NILP (tem
))
2784 p
->kill_without_query
= 1;
2785 if (tem
= Fplist_get (contact
, QCstop
), !NILP (tem
))
2786 pset_command (p
, Qt
);
2789 if (!EQ (p
->command
, Qt
))
2790 add_non_keyboard_read_fd (fd
);
2792 if (BUFFERP (buffer
))
2794 set_marker_both (p
->mark
, buffer
,
2795 BUF_ZV (XBUFFER (buffer
)),
2796 BUF_ZV_BYTE (XBUFFER (buffer
)));
2799 tem
= Fplist_member (contact
, QCcoding
);
2800 if (!NILP (tem
) && (!CONSP (tem
) || !CONSP (XCDR (tem
))))
2806 val
= XCAR (XCDR (tem
));
2810 else if (!NILP (Vcoding_system_for_read
))
2811 val
= Vcoding_system_for_read
;
2812 else if ((!NILP (buffer
) && NILP (BVAR (XBUFFER (buffer
), enable_multibyte_characters
)))
2813 || (NILP (buffer
) && NILP (BVAR (&buffer_defaults
, enable_multibyte_characters
))))
2815 pset_decode_coding_system (p
, val
);
2820 val
= XCAR (XCDR (tem
));
2824 else if (!NILP (Vcoding_system_for_write
))
2825 val
= Vcoding_system_for_write
;
2826 else if ((!NILP (buffer
) && NILP (BVAR (XBUFFER (buffer
), enable_multibyte_characters
)))
2827 || (NILP (buffer
) && NILP (BVAR (&buffer_defaults
, enable_multibyte_characters
))))
2829 pset_encode_coding_system (p
, val
);
2831 setup_process_coding_systems (proc
);
2832 pset_decoding_buf (p
, empty_unibyte_string
);
2833 p
->decoding_carryover
= 0;
2834 pset_encoding_buf (p
, empty_unibyte_string
);
2835 p
->inherit_coding_system_flag
2836 = !(!NILP (tem
) || NILP (buffer
) || !inherit_process_coding_system
);
2838 Fserial_process_configure (nargs
, args
);
2840 specpdl_ptr
= specpdl
+ specpdl_count
;
2846 /* Create a network stream/datagram client/server process. Treated
2847 exactly like a normal process when reading and writing. Primary
2848 differences are in status display and process deletion. A network
2849 connection has no PID; you cannot signal it. All you can do is
2850 stop/continue it and deactivate/close it via delete-process */
2852 DEFUN ("make-network-process", Fmake_network_process
, Smake_network_process
,
2854 doc
: /* Create and return a network server or client process.
2856 In Emacs, network connections are represented by process objects, so
2857 input and output work as for subprocesses and `delete-process' closes
2858 a network connection. However, a network process has no process id,
2859 it cannot be signaled, and the status codes are different from normal
2862 Arguments are specified as keyword/argument pairs. The following
2863 arguments are defined:
2865 :name NAME -- NAME is name for process. It is modified if necessary
2868 :buffer BUFFER -- BUFFER is the buffer (or buffer-name) to associate
2869 with the process. Process output goes at end of that buffer, unless
2870 you specify an output stream or filter function to handle the output.
2871 BUFFER may be also nil, meaning that this process is not associated
2874 :host HOST -- HOST is name of the host to connect to, or its IP
2875 address. The symbol `local' specifies the local host. If specified
2876 for a server process, it must be a valid name or address for the local
2877 host, and only clients connecting to that address will be accepted.
2879 :service SERVICE -- SERVICE is name of the service desired, or an
2880 integer specifying a port number to connect to. If SERVICE is t,
2881 a random port number is selected for the server. (If Emacs was
2882 compiled with getaddrinfo, a port number can also be specified as a
2883 string, e.g. "80", as well as an integer. This is not portable.)
2885 :type TYPE -- TYPE is the type of connection. The default (nil) is a
2886 stream type connection, `datagram' creates a datagram type connection,
2887 `seqpacket' creates a reliable datagram connection.
2889 :family FAMILY -- FAMILY is the address (and protocol) family for the
2890 service specified by HOST and SERVICE. The default (nil) is to use
2891 whatever address family (IPv4 or IPv6) that is defined for the host
2892 and port number specified by HOST and SERVICE. Other address families
2894 local -- for a local (i.e. UNIX) address specified by SERVICE.
2895 ipv4 -- use IPv4 address family only.
2896 ipv6 -- use IPv6 address family only.
2898 :local ADDRESS -- ADDRESS is the local address used for the connection.
2899 This parameter is ignored when opening a client process. When specified
2900 for a server process, the FAMILY, HOST and SERVICE args are ignored.
2902 :remote ADDRESS -- ADDRESS is the remote partner's address for the
2903 connection. This parameter is ignored when opening a stream server
2904 process. For a datagram server process, it specifies the initial
2905 setting of the remote datagram address. When specified for a client
2906 process, the FAMILY, HOST, and SERVICE args are ignored.
2908 The format of ADDRESS depends on the address family:
2909 - An IPv4 address is represented as an vector of integers [A B C D P]
2910 corresponding to numeric IP address A.B.C.D and port number P.
2911 - A local address is represented as a string with the address in the
2912 local address space.
2913 - An "unsupported family" address is represented by a cons (F . AV)
2914 where F is the family number and AV is a vector containing the socket
2915 address data with one element per address data byte. Do not rely on
2916 this format in portable code, as it may depend on implementation
2917 defined constants, data sizes, and data structure alignment.
2919 :coding CODING -- If CODING is a symbol, it specifies the coding
2920 system used for both reading and writing for this process. If CODING
2921 is a cons (DECODING . ENCODING), DECODING is used for reading, and
2922 ENCODING is used for writing.
2924 :nowait BOOL -- If BOOL is non-nil for a stream type client process,
2925 return without waiting for the connection to complete; instead, the
2926 sentinel function will be called with second arg matching "open" (if
2927 successful) or "failed" when the connect completes. Default is to use
2928 a blocking connect (i.e. wait) for stream type connections.
2930 :noquery BOOL -- Query the user unless BOOL is non-nil, and process is
2931 running when Emacs is exited.
2933 :stop BOOL -- Start process in the `stopped' state if BOOL non-nil.
2934 In the stopped state, a server process does not accept new
2935 connections, and a client process does not handle incoming traffic.
2936 The stopped state is cleared by `continue-process' and set by
2939 :filter FILTER -- Install FILTER as the process filter.
2941 :filter-multibyte BOOL -- If BOOL is non-nil, strings given to the
2942 process filter are multibyte, otherwise they are unibyte.
2943 If this keyword is not specified, the strings are multibyte if
2944 the default value of `enable-multibyte-characters' is non-nil.
2946 :sentinel SENTINEL -- Install SENTINEL as the process sentinel.
2948 :log LOG -- Install LOG as the server process log function. This
2949 function is called when the server accepts a network connection from a
2950 client. The arguments are SERVER, CLIENT, and MESSAGE, where SERVER
2951 is the server process, CLIENT is the new process for the connection,
2952 and MESSAGE is a string.
2954 :plist PLIST -- Install PLIST as the new process' initial plist.
2956 :server QLEN -- if QLEN is non-nil, create a server process for the
2957 specified FAMILY, SERVICE, and connection type (stream or datagram).
2958 If QLEN is an integer, it is used as the max. length of the server's
2959 pending connection queue (also known as the backlog); the default
2960 queue length is 5. Default is to create a client process.
2962 The following network options can be specified for this connection:
2964 :broadcast BOOL -- Allow send and receive of datagram broadcasts.
2965 :dontroute BOOL -- Only send to directly connected hosts.
2966 :keepalive BOOL -- Send keep-alive messages on network stream.
2967 :linger BOOL or TIMEOUT -- Send queued messages before closing.
2968 :oobinline BOOL -- Place out-of-band data in receive data stream.
2969 :priority INT -- Set protocol defined priority for sent packets.
2970 :reuseaddr BOOL -- Allow reusing a recently used local address
2971 (this is allowed by default for a server process).
2972 :bindtodevice NAME -- bind to interface NAME. Using this may require
2973 special privileges on some systems.
2975 Consult the relevant system programmer's manual pages for more
2976 information on using these options.
2979 A server process will listen for and accept connections from clients.
2980 When a client connection is accepted, a new network process is created
2981 for the connection with the following parameters:
2983 - The client's process name is constructed by concatenating the server
2984 process' NAME and a client identification string.
2985 - If the FILTER argument is non-nil, the client process will not get a
2986 separate process buffer; otherwise, the client's process buffer is a newly
2987 created buffer named after the server process' BUFFER name or process
2988 NAME concatenated with the client identification string.
2989 - The connection type and the process filter and sentinel parameters are
2990 inherited from the server process' TYPE, FILTER and SENTINEL.
2991 - The client process' contact info is set according to the client's
2992 addressing information (typically an IP address and a port number).
2993 - The client process' plist is initialized from the server's plist.
2995 Notice that the FILTER and SENTINEL args are never used directly by
2996 the server process. Also, the BUFFER argument is not used directly by
2997 the server process, but via the optional :log function, accepted (and
2998 failed) connections may be logged in the server process' buffer.
3000 The original argument list, modified with the actual connection
3001 information, is available via the `process-contact' function.
3003 usage: (make-network-process &rest ARGS) */)
3004 (ptrdiff_t nargs
, Lisp_Object
*args
)
3007 Lisp_Object contact
;
3008 struct Lisp_Process
*p
;
3009 #ifdef HAVE_GETADDRINFO
3010 struct addrinfo ai
, *res
, *lres
;
3011 struct addrinfo hints
;
3012 const char *portstring
;
3014 #else /* HAVE_GETADDRINFO */
3015 struct _emacs_addrinfo
3021 struct sockaddr
*ai_addr
;
3022 struct _emacs_addrinfo
*ai_next
;
3024 #endif /* HAVE_GETADDRINFO */
3025 struct sockaddr_in address_in
;
3026 #ifdef HAVE_LOCAL_SOCKETS
3027 struct sockaddr_un address_un
;
3032 int s
= -1, outch
, inch
;
3033 struct gcpro gcpro1
;
3034 ptrdiff_t count
= SPECPDL_INDEX ();
3036 Lisp_Object QCaddress
; /* one of QClocal or QCremote */
3038 Lisp_Object name
, buffer
, host
, service
, address
;
3039 Lisp_Object filter
, sentinel
;
3040 bool is_non_blocking_client
= 0;
3049 /* Save arguments for process-contact and clone-process. */
3050 contact
= Flist (nargs
, args
);
3054 /* Ensure socket support is loaded if available. */
3055 init_winsock (TRUE
);
3058 /* :type TYPE (nil: stream, datagram */
3059 tem
= Fplist_get (contact
, QCtype
);
3061 socktype
= SOCK_STREAM
;
3062 #ifdef DATAGRAM_SOCKETS
3063 else if (EQ (tem
, Qdatagram
))
3064 socktype
= SOCK_DGRAM
;
3066 #ifdef HAVE_SEQPACKET
3067 else if (EQ (tem
, Qseqpacket
))
3068 socktype
= SOCK_SEQPACKET
;
3071 error ("Unsupported connection type");
3074 tem
= Fplist_get (contact
, QCserver
);
3077 /* Don't support network sockets when non-blocking mode is
3078 not available, since a blocked Emacs is not useful. */
3080 if (TYPE_RANGED_INTEGERP (int, tem
))
3081 backlog
= XINT (tem
);
3084 /* Make QCaddress an alias for :local (server) or :remote (client). */
3085 QCaddress
= is_server
? QClocal
: QCremote
;
3088 if (!is_server
&& socktype
!= SOCK_DGRAM
3089 && (tem
= Fplist_get (contact
, QCnowait
), !NILP (tem
)))
3091 #ifndef NON_BLOCKING_CONNECT
3092 error ("Non-blocking connect not supported");
3094 is_non_blocking_client
= 1;
3098 name
= Fplist_get (contact
, QCname
);
3099 buffer
= Fplist_get (contact
, QCbuffer
);
3100 filter
= Fplist_get (contact
, QCfilter
);
3101 sentinel
= Fplist_get (contact
, QCsentinel
);
3103 CHECK_STRING (name
);
3105 /* Initialize addrinfo structure in case we don't use getaddrinfo. */
3106 ai
.ai_socktype
= socktype
;
3111 /* :local ADDRESS or :remote ADDRESS */
3112 address
= Fplist_get (contact
, QCaddress
);
3113 if (!NILP (address
))
3115 host
= service
= Qnil
;
3117 if (!(ai
.ai_addrlen
= get_lisp_to_sockaddr_size (address
, &family
)))
3118 error ("Malformed :address");
3119 ai
.ai_family
= family
;
3120 ai
.ai_addr
= alloca (ai
.ai_addrlen
);
3121 conv_lisp_to_sockaddr (family
, address
, ai
.ai_addr
, ai
.ai_addrlen
);
3125 /* :family FAMILY -- nil (for Inet), local, or integer. */
3126 tem
= Fplist_get (contact
, QCfamily
);
3129 #if defined (HAVE_GETADDRINFO) && defined (AF_INET6)
3135 #ifdef HAVE_LOCAL_SOCKETS
3136 else if (EQ (tem
, Qlocal
))
3140 else if (EQ (tem
, Qipv6
))
3143 else if (EQ (tem
, Qipv4
))
3145 else if (TYPE_RANGED_INTEGERP (int, tem
))
3146 family
= XINT (tem
);
3148 error ("Unknown address family");
3150 ai
.ai_family
= family
;
3152 /* :service SERVICE -- string, integer (port number), or t (random port). */
3153 service
= Fplist_get (contact
, QCservice
);
3155 /* :host HOST -- hostname, ip address, or 'local for localhost. */
3156 host
= Fplist_get (contact
, QChost
);
3159 if (EQ (host
, Qlocal
))
3160 /* Depending on setup, "localhost" may map to different IPv4 and/or
3161 IPv6 addresses, so it's better to be explicit. (Bug#6781) */
3162 host
= build_string ("127.0.0.1");
3163 CHECK_STRING (host
);
3166 #ifdef HAVE_LOCAL_SOCKETS
3167 if (family
== AF_LOCAL
)
3171 message (":family local ignores the :host \"%s\" property",
3173 contact
= Fplist_put (contact
, QChost
, Qnil
);
3176 CHECK_STRING (service
);
3177 memset (&address_un
, 0, sizeof address_un
);
3178 address_un
.sun_family
= AF_LOCAL
;
3179 if (sizeof address_un
.sun_path
<= SBYTES (service
))
3180 error ("Service name too long");
3181 strcpy (address_un
.sun_path
, SSDATA (service
));
3182 ai
.ai_addr
= (struct sockaddr
*) &address_un
;
3183 ai
.ai_addrlen
= sizeof address_un
;
3188 /* Slow down polling to every ten seconds.
3189 Some kernels have a bug which causes retrying connect to fail
3190 after a connect. Polling can interfere with gethostbyname too. */
3191 #ifdef POLL_FOR_INPUT
3192 if (socktype
!= SOCK_DGRAM
)
3194 record_unwind_protect (unwind_stop_other_atimers
, Qnil
);
3195 bind_polling_period (10);
3199 #ifdef HAVE_GETADDRINFO
3200 /* If we have a host, use getaddrinfo to resolve both host and service.
3201 Otherwise, use getservbyname to lookup the service. */
3205 /* SERVICE can either be a string or int.
3206 Convert to a C string for later use by getaddrinfo. */
3207 if (EQ (service
, Qt
))
3209 else if (INTEGERP (service
))
3211 sprintf (portbuf
, "%"pI
"d", XINT (service
));
3212 portstring
= portbuf
;
3216 CHECK_STRING (service
);
3217 portstring
= SSDATA (service
);
3222 memset (&hints
, 0, sizeof (hints
));
3224 hints
.ai_family
= family
;
3225 hints
.ai_socktype
= socktype
;
3226 hints
.ai_protocol
= 0;
3228 #ifdef HAVE_RES_INIT
3232 ret
= getaddrinfo (SSDATA (host
), portstring
, &hints
, &res
);
3234 #ifdef HAVE_GAI_STRERROR
3235 error ("%s/%s %s", SSDATA (host
), portstring
, gai_strerror (ret
));
3237 error ("%s/%s getaddrinfo error %d", SSDATA (host
), portstring
, ret
);
3243 #endif /* HAVE_GETADDRINFO */
3245 /* We end up here if getaddrinfo is not defined, or in case no hostname
3246 has been specified (e.g. for a local server process). */
3248 if (EQ (service
, Qt
))
3250 else if (INTEGERP (service
))
3251 port
= htons ((unsigned short) XINT (service
));
3254 struct servent
*svc_info
;
3255 CHECK_STRING (service
);
3256 svc_info
= getservbyname (SSDATA (service
),
3257 (socktype
== SOCK_DGRAM
? "udp" : "tcp"));
3259 error ("Unknown service: %s", SDATA (service
));
3260 port
= svc_info
->s_port
;
3263 memset (&address_in
, 0, sizeof address_in
);
3264 address_in
.sin_family
= family
;
3265 address_in
.sin_addr
.s_addr
= INADDR_ANY
;
3266 address_in
.sin_port
= port
;
3268 #ifndef HAVE_GETADDRINFO
3271 struct hostent
*host_info_ptr
;
3273 /* gethostbyname may fail with TRY_AGAIN, but we don't honor that,
3274 as it may `hang' Emacs for a very long time. */
3278 #ifdef HAVE_RES_INIT
3282 host_info_ptr
= gethostbyname (SDATA (host
));
3287 memcpy (&address_in
.sin_addr
, host_info_ptr
->h_addr
,
3288 host_info_ptr
->h_length
);
3289 family
= host_info_ptr
->h_addrtype
;
3290 address_in
.sin_family
= family
;
3293 /* Attempt to interpret host as numeric inet address */
3295 unsigned long numeric_addr
;
3296 numeric_addr
= inet_addr (SSDATA (host
));
3297 if (numeric_addr
== -1)
3298 error ("Unknown host \"%s\"", SDATA (host
));
3300 memcpy (&address_in
.sin_addr
, &numeric_addr
,
3301 sizeof (address_in
.sin_addr
));
3305 #endif /* not HAVE_GETADDRINFO */
3307 ai
.ai_family
= family
;
3308 ai
.ai_addr
= (struct sockaddr
*) &address_in
;
3309 ai
.ai_addrlen
= sizeof address_in
;
3313 /* Do this in case we never enter the for-loop below. */
3314 count1
= SPECPDL_INDEX ();
3317 for (lres
= res
; lres
; lres
= lres
->ai_next
)
3326 s
= socket (lres
->ai_family
, lres
->ai_socktype
, lres
->ai_protocol
);
3333 #ifdef DATAGRAM_SOCKETS
3334 if (!is_server
&& socktype
== SOCK_DGRAM
)
3336 #endif /* DATAGRAM_SOCKETS */
3338 #ifdef NON_BLOCKING_CONNECT
3339 if (is_non_blocking_client
)
3341 ret
= fcntl (s
, F_SETFL
, O_NONBLOCK
);
3352 /* Make us close S if quit. */
3353 record_unwind_protect (close_file_unwind
, make_number (s
));
3355 /* Parse network options in the arg list.
3356 We simply ignore anything which isn't a known option (including other keywords).
3357 An error is signaled if setting a known option fails. */
3358 for (optn
= optbits
= 0; optn
< nargs
-1; optn
+= 2)
3359 optbits
|= set_socket_option (s
, args
[optn
], args
[optn
+1]);
3363 /* Configure as a server socket. */
3365 /* SO_REUSEADDR = 1 is default for server sockets; must specify
3366 explicit :reuseaddr key to override this. */
3367 #ifdef HAVE_LOCAL_SOCKETS
3368 if (family
!= AF_LOCAL
)
3370 if (!(optbits
& (1 << OPIX_REUSEADDR
)))
3373 if (setsockopt (s
, SOL_SOCKET
, SO_REUSEADDR
, &optval
, sizeof optval
))
3374 report_file_error ("Cannot set reuse option on server socket", Qnil
);
3377 if (bind (s
, lres
->ai_addr
, lres
->ai_addrlen
))
3378 report_file_error ("Cannot bind server socket", Qnil
);
3380 #ifdef HAVE_GETSOCKNAME
3381 if (EQ (service
, Qt
))
3383 struct sockaddr_in sa1
;
3384 socklen_t len1
= sizeof (sa1
);
3385 if (getsockname (s
, (struct sockaddr
*)&sa1
, &len1
) == 0)
3387 ((struct sockaddr_in
*)(lres
->ai_addr
))->sin_port
= sa1
.sin_port
;
3388 service
= make_number (ntohs (sa1
.sin_port
));
3389 contact
= Fplist_put (contact
, QCservice
, service
);
3394 if (socktype
!= SOCK_DGRAM
&& listen (s
, backlog
))
3395 report_file_error ("Cannot listen on server socket", Qnil
);
3403 ret
= connect (s
, lres
->ai_addr
, lres
->ai_addrlen
);
3406 if (ret
== 0 || xerrno
== EISCONN
)
3408 /* The unwind-protect will be discarded afterwards.
3409 Likewise for immediate_quit. */
3413 #ifdef NON_BLOCKING_CONNECT
3415 if (is_non_blocking_client
&& xerrno
== EINPROGRESS
)
3419 if (is_non_blocking_client
&& xerrno
== EWOULDBLOCK
)
3426 if (xerrno
== EINTR
)
3428 /* Unlike most other syscalls connect() cannot be called
3429 again. (That would return EALREADY.) The proper way to
3430 wait for completion is pselect(). */
3438 sc
= pselect (s
+ 1, NULL
, &fdset
, NULL
, NULL
, NULL
);
3444 report_file_error ("select failed", Qnil
);
3448 len
= sizeof xerrno
;
3449 eassert (FD_ISSET (s
, &fdset
));
3450 if (getsockopt (s
, SOL_SOCKET
, SO_ERROR
, &xerrno
, &len
) == -1)
3451 report_file_error ("getsockopt failed", Qnil
);
3453 errno
= xerrno
, report_file_error ("error during connect", Qnil
);
3457 #endif /* !WINDOWSNT */
3461 /* Discard the unwind protect closing S. */
3462 specpdl_ptr
= specpdl
+ count1
;
3467 if (xerrno
== EINTR
)
3474 #ifdef DATAGRAM_SOCKETS
3475 if (socktype
== SOCK_DGRAM
)
3477 if (datagram_address
[s
].sa
)
3479 datagram_address
[s
].sa
= xmalloc (lres
->ai_addrlen
);
3480 datagram_address
[s
].len
= lres
->ai_addrlen
;
3484 memset (datagram_address
[s
].sa
, 0, lres
->ai_addrlen
);
3485 if (remote
= Fplist_get (contact
, QCremote
), !NILP (remote
))
3488 rlen
= get_lisp_to_sockaddr_size (remote
, &rfamily
);
3489 if (rlen
!= 0 && rfamily
== lres
->ai_family
3490 && rlen
== lres
->ai_addrlen
)
3491 conv_lisp_to_sockaddr (rfamily
, remote
,
3492 datagram_address
[s
].sa
, rlen
);
3496 memcpy (datagram_address
[s
].sa
, lres
->ai_addr
, lres
->ai_addrlen
);
3499 contact
= Fplist_put (contact
, QCaddress
,
3500 conv_sockaddr_to_lisp (lres
->ai_addr
, lres
->ai_addrlen
));
3501 #ifdef HAVE_GETSOCKNAME
3504 struct sockaddr_in sa1
;
3505 socklen_t len1
= sizeof (sa1
);
3506 if (getsockname (s
, (struct sockaddr
*)&sa1
, &len1
) == 0)
3507 contact
= Fplist_put (contact
, QClocal
,
3508 conv_sockaddr_to_lisp ((struct sockaddr
*)&sa1
, len1
));
3515 #ifdef HAVE_GETADDRINFO
3524 /* Discard the unwind protect for closing S, if any. */
3525 specpdl_ptr
= specpdl
+ count1
;
3527 /* Unwind bind_polling_period and request_sigio. */
3528 unbind_to (count
, Qnil
);
3532 /* If non-blocking got this far - and failed - assume non-blocking is
3533 not supported after all. This is probably a wrong assumption, but
3534 the normal blocking calls to open-network-stream handles this error
3536 if (is_non_blocking_client
)
3541 report_file_error ("make server process failed", contact
);
3543 report_file_error ("make client process failed", contact
);
3550 buffer
= Fget_buffer_create (buffer
);
3551 proc
= make_process (name
);
3553 chan_process
[inch
] = proc
;
3555 fcntl (inch
, F_SETFL
, O_NONBLOCK
);
3557 p
= XPROCESS (proc
);
3559 pset_childp (p
, contact
);
3560 pset_plist (p
, Fcopy_sequence (Fplist_get (contact
, QCplist
)));
3561 pset_type (p
, Qnetwork
);
3563 pset_buffer (p
, buffer
);
3564 pset_sentinel (p
, sentinel
);
3565 pset_filter (p
, filter
);
3566 pset_log (p
, Fplist_get (contact
, QClog
));
3567 if (tem
= Fplist_get (contact
, QCnoquery
), !NILP (tem
))
3568 p
->kill_without_query
= 1;
3569 if ((tem
= Fplist_get (contact
, QCstop
), !NILP (tem
)))
3570 pset_command (p
, Qt
);
3574 if (is_server
&& socktype
!= SOCK_DGRAM
)
3575 pset_status (p
, Qlisten
);
3577 /* Make the process marker point into the process buffer (if any). */
3578 if (BUFFERP (buffer
))
3579 set_marker_both (p
->mark
, buffer
,
3580 BUF_ZV (XBUFFER (buffer
)),
3581 BUF_ZV_BYTE (XBUFFER (buffer
)));
3583 #ifdef NON_BLOCKING_CONNECT
3584 if (is_non_blocking_client
)
3586 /* We may get here if connect did succeed immediately. However,
3587 in that case, we still need to signal this like a non-blocking
3589 pset_status (p
, Qconnect
);
3590 if ((fd_callback_info
[inch
].flags
& NON_BLOCKING_CONNECT_FD
) == 0)
3591 add_non_blocking_write_fd (inch
);
3595 /* A server may have a client filter setting of Qt, but it must
3596 still listen for incoming connects unless it is stopped. */
3597 if ((!EQ (p
->filter
, Qt
) && !EQ (p
->command
, Qt
))
3598 || (EQ (p
->status
, Qlisten
) && NILP (p
->command
)))
3599 add_non_keyboard_read_fd (inch
);
3601 if (inch
> max_desc
)
3604 tem
= Fplist_member (contact
, QCcoding
);
3605 if (!NILP (tem
) && (!CONSP (tem
) || !CONSP (XCDR (tem
))))
3606 tem
= Qnil
; /* No error message (too late!). */
3609 /* Setup coding systems for communicating with the network stream. */
3610 struct gcpro gcpro1
;
3611 /* Qt denotes we have not yet called Ffind_operation_coding_system. */
3612 Lisp_Object coding_systems
= Qt
;
3613 Lisp_Object fargs
[5], val
;
3617 val
= XCAR (XCDR (tem
));
3621 else if (!NILP (Vcoding_system_for_read
))
3622 val
= Vcoding_system_for_read
;
3623 else if ((!NILP (buffer
) && NILP (BVAR (XBUFFER (buffer
), enable_multibyte_characters
)))
3624 || (NILP (buffer
) && NILP (BVAR (&buffer_defaults
, enable_multibyte_characters
))))
3625 /* We dare not decode end-of-line format by setting VAL to
3626 Qraw_text, because the existing Emacs Lisp libraries
3627 assume that they receive bare code including a sequence of
3632 if (NILP (host
) || NILP (service
))
3633 coding_systems
= Qnil
;
3636 fargs
[0] = Qopen_network_stream
, fargs
[1] = name
,
3637 fargs
[2] = buffer
, fargs
[3] = host
, fargs
[4] = service
;
3639 coding_systems
= Ffind_operation_coding_system (5, fargs
);
3642 if (CONSP (coding_systems
))
3643 val
= XCAR (coding_systems
);
3644 else if (CONSP (Vdefault_process_coding_system
))
3645 val
= XCAR (Vdefault_process_coding_system
);
3649 pset_decode_coding_system (p
, val
);
3653 val
= XCAR (XCDR (tem
));
3657 else if (!NILP (Vcoding_system_for_write
))
3658 val
= Vcoding_system_for_write
;
3659 else if (NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
3663 if (EQ (coding_systems
, Qt
))
3665 if (NILP (host
) || NILP (service
))
3666 coding_systems
= Qnil
;
3669 fargs
[0] = Qopen_network_stream
, fargs
[1] = name
,
3670 fargs
[2] = buffer
, fargs
[3] = host
, fargs
[4] = service
;
3672 coding_systems
= Ffind_operation_coding_system (5, fargs
);
3676 if (CONSP (coding_systems
))
3677 val
= XCDR (coding_systems
);
3678 else if (CONSP (Vdefault_process_coding_system
))
3679 val
= XCDR (Vdefault_process_coding_system
);
3683 pset_encode_coding_system (p
, val
);
3685 setup_process_coding_systems (proc
);
3687 pset_decoding_buf (p
, empty_unibyte_string
);
3688 p
->decoding_carryover
= 0;
3689 pset_encoding_buf (p
, empty_unibyte_string
);
3691 p
->inherit_coding_system_flag
3692 = !(!NILP (tem
) || NILP (buffer
) || !inherit_process_coding_system
);
3699 #if defined (HAVE_NET_IF_H)
3702 DEFUN ("network-interface-list", Fnetwork_interface_list
, Snetwork_interface_list
, 0, 0, 0,
3703 doc
: /* Return an alist of all network interfaces and their network address.
3704 Each element is a cons, the car of which is a string containing the
3705 interface name, and the cdr is the network address in internal
3706 format; see the description of ADDRESS in `make-network-process'. */)
3709 struct ifconf ifconf
;
3710 struct ifreq
*ifreq
;
3712 ptrdiff_t buf_size
= 512;
3716 s
= socket (AF_INET
, SOCK_STREAM
, 0);
3722 buf
= xpalloc (buf
, &buf_size
, 1, INT_MAX
, 1);
3723 ifconf
.ifc_buf
= buf
;
3724 ifconf
.ifc_len
= buf_size
;
3725 if (ioctl (s
, SIOCGIFCONF
, &ifconf
))
3732 while (ifconf
.ifc_len
== buf_size
);
3737 ifreq
= ifconf
.ifc_req
;
3738 while ((char *) ifreq
< (char *) ifconf
.ifc_req
+ ifconf
.ifc_len
)
3740 struct ifreq
*ifq
= ifreq
;
3741 #ifdef HAVE_STRUCT_IFREQ_IFR_ADDR_SA_LEN
3742 #define SIZEOF_IFREQ(sif) \
3743 ((sif)->ifr_addr.sa_len < sizeof (struct sockaddr) \
3744 ? sizeof (*(sif)) : sizeof ((sif)->ifr_name) + (sif)->ifr_addr.sa_len)
3746 int len
= SIZEOF_IFREQ (ifq
);
3748 int len
= sizeof (*ifreq
);
3750 char namebuf
[sizeof (ifq
->ifr_name
) + 1];
3751 ifreq
= (struct ifreq
*) ((char *) ifreq
+ len
);
3753 if (ifq
->ifr_addr
.sa_family
!= AF_INET
)
3756 memcpy (namebuf
, ifq
->ifr_name
, sizeof (ifq
->ifr_name
));
3757 namebuf
[sizeof (ifq
->ifr_name
)] = 0;
3758 res
= Fcons (Fcons (build_string (namebuf
),
3759 conv_sockaddr_to_lisp (&ifq
->ifr_addr
,
3760 sizeof (struct sockaddr
))),
3767 #endif /* SIOCGIFCONF */
3769 #if defined (SIOCGIFADDR) || defined (SIOCGIFHWADDR) || defined (SIOCGIFFLAGS)
3773 const char *flag_sym
;
3776 static const struct ifflag_def ifflag_table
[] = {
3780 #ifdef IFF_BROADCAST
3781 { IFF_BROADCAST
, "broadcast" },
3784 { IFF_DEBUG
, "debug" },
3787 { IFF_LOOPBACK
, "loopback" },
3789 #ifdef IFF_POINTOPOINT
3790 { IFF_POINTOPOINT
, "pointopoint" },
3793 { IFF_RUNNING
, "running" },
3796 { IFF_NOARP
, "noarp" },
3799 { IFF_PROMISC
, "promisc" },
3801 #ifdef IFF_NOTRAILERS
3802 #ifdef NS_IMPL_COCOA
3803 /* Really means smart, notrailers is obsolete */
3804 { IFF_NOTRAILERS
, "smart" },
3806 { IFF_NOTRAILERS
, "notrailers" },
3810 { IFF_ALLMULTI
, "allmulti" },
3813 { IFF_MASTER
, "master" },
3816 { IFF_SLAVE
, "slave" },
3818 #ifdef IFF_MULTICAST
3819 { IFF_MULTICAST
, "multicast" },
3822 { IFF_PORTSEL
, "portsel" },
3824 #ifdef IFF_AUTOMEDIA
3825 { IFF_AUTOMEDIA
, "automedia" },
3828 { IFF_DYNAMIC
, "dynamic" },
3831 { IFF_OACTIVE
, "oactive" }, /* OpenBSD: transmission in progress */
3834 { IFF_SIMPLEX
, "simplex" }, /* OpenBSD: can't hear own transmissions */
3837 { IFF_LINK0
, "link0" }, /* OpenBSD: per link layer defined bit */
3840 { IFF_LINK1
, "link1" }, /* OpenBSD: per link layer defined bit */
3843 { IFF_LINK2
, "link2" }, /* OpenBSD: per link layer defined bit */
3848 DEFUN ("network-interface-info", Fnetwork_interface_info
, Snetwork_interface_info
, 1, 1, 0,
3849 doc
: /* Return information about network interface named IFNAME.
3850 The return value is a list (ADDR BCAST NETMASK HWADDR FLAGS),
3851 where ADDR is the layer 3 address, BCAST is the layer 3 broadcast address,
3852 NETMASK is the layer 3 network mask, HWADDR is the layer 2 address, and
3853 FLAGS is the current flags of the interface. */)
3854 (Lisp_Object ifname
)
3857 Lisp_Object res
= Qnil
;
3861 #if (! (defined SIOCGIFHWADDR && defined HAVE_STRUCT_IFREQ_IFR_HWADDR) \
3862 && defined HAVE_GETIFADDRS && defined LLADDR)
3863 struct ifaddrs
*ifap
;
3866 CHECK_STRING (ifname
);
3868 if (sizeof rq
.ifr_name
<= SBYTES (ifname
))
3869 error ("interface name too long");
3870 strcpy (rq
.ifr_name
, SSDATA (ifname
));
3872 s
= socket (AF_INET
, SOCK_STREAM
, 0);
3877 #if defined (SIOCGIFFLAGS) && defined (HAVE_STRUCT_IFREQ_IFR_FLAGS)
3878 if (ioctl (s
, SIOCGIFFLAGS
, &rq
) == 0)
3880 int flags
= rq
.ifr_flags
;
3881 const struct ifflag_def
*fp
;
3884 /* If flags is smaller than int (i.e. short) it may have the high bit set
3885 due to IFF_MULTICAST. In that case, sign extending it into
3887 if (flags
< 0 && sizeof (rq
.ifr_flags
) < sizeof (flags
))
3888 flags
= (unsigned short) rq
.ifr_flags
;
3891 for (fp
= ifflag_table
; flags
!= 0 && fp
->flag_sym
; fp
++)
3893 if (flags
& fp
->flag_bit
)
3895 elt
= Fcons (intern (fp
->flag_sym
), elt
);
3896 flags
-= fp
->flag_bit
;
3899 for (fnum
= 0; flags
&& fnum
< 32; flags
>>= 1, fnum
++)
3903 elt
= Fcons (make_number (fnum
), elt
);
3908 res
= Fcons (elt
, res
);
3911 #if defined (SIOCGIFHWADDR) && defined (HAVE_STRUCT_IFREQ_IFR_HWADDR)
3912 if (ioctl (s
, SIOCGIFHWADDR
, &rq
) == 0)
3914 Lisp_Object hwaddr
= Fmake_vector (make_number (6), Qnil
);
3915 register struct Lisp_Vector
*p
= XVECTOR (hwaddr
);
3919 for (n
= 0; n
< 6; n
++)
3920 p
->contents
[n
] = make_number (((unsigned char *)&rq
.ifr_hwaddr
.sa_data
[0])[n
]);
3921 elt
= Fcons (make_number (rq
.ifr_hwaddr
.sa_family
), hwaddr
);
3923 #elif defined (HAVE_GETIFADDRS) && defined (LLADDR)
3924 if (getifaddrs (&ifap
) != -1)
3926 Lisp_Object hwaddr
= Fmake_vector (make_number (6), Qnil
);
3927 register struct Lisp_Vector
*p
= XVECTOR (hwaddr
);
3930 for (it
= ifap
; it
!= NULL
; it
= it
->ifa_next
)
3932 struct sockaddr_dl
*sdl
= (struct sockaddr_dl
*) it
->ifa_addr
;
3933 unsigned char linkaddr
[6];
3936 if (it
->ifa_addr
->sa_family
!= AF_LINK
3937 || strcmp (it
->ifa_name
, SSDATA (ifname
)) != 0
3938 || sdl
->sdl_alen
!= 6)
3941 memcpy (linkaddr
, LLADDR (sdl
), sdl
->sdl_alen
);
3942 for (n
= 0; n
< 6; n
++)
3943 p
->contents
[n
] = make_number (linkaddr
[n
]);
3945 elt
= Fcons (make_number (it
->ifa_addr
->sa_family
), hwaddr
);
3949 #ifdef HAVE_FREEIFADDRS
3953 #endif /* HAVE_GETIFADDRS && LLADDR */
3955 res
= Fcons (elt
, res
);
3958 #if defined (SIOCGIFNETMASK) && (defined (HAVE_STRUCT_IFREQ_IFR_NETMASK) || defined (HAVE_STRUCT_IFREQ_IFR_ADDR))
3959 if (ioctl (s
, SIOCGIFNETMASK
, &rq
) == 0)
3962 #ifdef HAVE_STRUCT_IFREQ_IFR_NETMASK
3963 elt
= conv_sockaddr_to_lisp (&rq
.ifr_netmask
, sizeof (rq
.ifr_netmask
));
3965 elt
= conv_sockaddr_to_lisp (&rq
.ifr_addr
, sizeof (rq
.ifr_addr
));
3969 res
= Fcons (elt
, res
);
3972 #if defined (SIOCGIFBRDADDR) && defined (HAVE_STRUCT_IFREQ_IFR_BROADADDR)
3973 if (ioctl (s
, SIOCGIFBRDADDR
, &rq
) == 0)
3976 elt
= conv_sockaddr_to_lisp (&rq
.ifr_broadaddr
, sizeof (rq
.ifr_broadaddr
));
3979 res
= Fcons (elt
, res
);
3982 #if defined (SIOCGIFADDR) && defined (HAVE_STRUCT_IFREQ_IFR_ADDR)
3983 if (ioctl (s
, SIOCGIFADDR
, &rq
) == 0)
3986 elt
= conv_sockaddr_to_lisp (&rq
.ifr_addr
, sizeof (rq
.ifr_addr
));
3989 res
= Fcons (elt
, res
);
3993 return any
? res
: Qnil
;
3996 #endif /* defined (HAVE_NET_IF_H) */
3998 /* Turn off input and output for process PROC. */
4001 deactivate_process (Lisp_Object proc
)
4003 register int inchannel
, outchannel
;
4004 register struct Lisp_Process
*p
= XPROCESS (proc
);
4007 /* Delete GnuTLS structures in PROC, if any. */
4008 emacs_gnutls_deinit (proc
);
4009 #endif /* HAVE_GNUTLS */
4011 inchannel
= p
->infd
;
4012 outchannel
= p
->outfd
;
4014 #ifdef ADAPTIVE_READ_BUFFERING
4015 if (p
->read_output_delay
> 0)
4017 if (--process_output_delay_count
< 0)
4018 process_output_delay_count
= 0;
4019 p
->read_output_delay
= 0;
4020 p
->read_output_skip
= 0;
4026 /* Beware SIGCHLD hereabouts. */
4027 flush_pending_output (inchannel
);
4028 emacs_close (inchannel
);
4029 if (outchannel
>= 0 && outchannel
!= inchannel
)
4030 emacs_close (outchannel
);
4034 #ifdef DATAGRAM_SOCKETS
4035 if (DATAGRAM_CHAN_P (inchannel
))
4037 xfree (datagram_address
[inchannel
].sa
);
4038 datagram_address
[inchannel
].sa
= 0;
4039 datagram_address
[inchannel
].len
= 0;
4042 chan_process
[inchannel
] = Qnil
;
4043 delete_read_fd (inchannel
);
4044 #ifdef NON_BLOCKING_CONNECT
4045 if ((fd_callback_info
[inchannel
].flags
& NON_BLOCKING_CONNECT_FD
) != 0)
4046 delete_write_fd (inchannel
);
4048 if (inchannel
== max_desc
)
4049 recompute_max_desc ();
4054 DEFUN ("accept-process-output", Faccept_process_output
, Saccept_process_output
,
4056 doc
: /* Allow any pending output from subprocesses to be read by Emacs.
4057 It is read into the process' buffers or given to their filter functions.
4058 Non-nil arg PROCESS means do not return until some output has been received
4061 Non-nil second arg SECONDS and third arg MILLISEC are number of seconds
4062 and milliseconds to wait; return after that much time whether or not
4063 there is any subprocess output. If SECONDS is a floating point number,
4064 it specifies a fractional number of seconds to wait.
4065 The MILLISEC argument is obsolete and should be avoided.
4067 If optional fourth arg JUST-THIS-ONE is non-nil, only accept output
4068 from PROCESS, suspending reading output from other processes.
4069 If JUST-THIS-ONE is an integer, don't run any timers either.
4070 Return non-nil if we received any output before the timeout expired. */)
4071 (register Lisp_Object process
, Lisp_Object seconds
, Lisp_Object millisec
, Lisp_Object just_this_one
)
4076 if (! NILP (process
))
4078 struct Lisp_Process
*procp
;
4080 CHECK_PROCESS (process
);
4081 procp
= XPROCESS (process
);
4083 /* Can't wait for a process that is dedicated to a different
4085 if (!EQ (procp
->thread
, Qnil
) && !EQ (procp
->thread
, Fcurrent_thread ()))
4089 just_this_one
= Qnil
;
4091 if (!NILP (millisec
))
4092 { /* Obsolete calling convention using integers rather than floats. */
4093 CHECK_NUMBER (millisec
);
4095 seconds
= make_float (XINT (millisec
) / 1000.0);
4098 CHECK_NUMBER (seconds
);
4099 seconds
= make_float (XINT (millisec
) / 1000.0 + XINT (seconds
));
4106 if (!NILP (seconds
))
4108 if (INTEGERP (seconds
))
4110 if (XINT (seconds
) > 0)
4112 secs
= XINT (seconds
);
4116 else if (FLOATP (seconds
))
4118 if (XFLOAT_DATA (seconds
) > 0)
4120 EMACS_TIME t
= EMACS_TIME_FROM_DOUBLE (XFLOAT_DATA (seconds
));
4121 secs
= min (EMACS_SECS (t
), WAIT_READING_MAX
);
4122 nsecs
= EMACS_NSECS (t
);
4126 wrong_type_argument (Qnumberp
, seconds
);
4128 else if (! NILP (process
))
4132 (wait_reading_process_output (secs
, nsecs
, 0, 0,
4134 !NILP (process
) ? XPROCESS (process
) : NULL
,
4135 NILP (just_this_one
) ? 0 :
4136 !INTEGERP (just_this_one
) ? 1 : -1)
4140 /* Accept a connection for server process SERVER on CHANNEL. */
4142 static EMACS_INT connect_counter
= 0;
4145 server_accept_connection (Lisp_Object server
, int channel
)
4147 Lisp_Object proc
, caller
, name
, buffer
;
4148 Lisp_Object contact
, host
, service
;
4149 struct Lisp_Process
*ps
= XPROCESS (server
);
4150 struct Lisp_Process
*p
;
4154 struct sockaddr_in in
;
4156 struct sockaddr_in6 in6
;
4158 #ifdef HAVE_LOCAL_SOCKETS
4159 struct sockaddr_un un
;
4162 socklen_t len
= sizeof saddr
;
4164 s
= accept (channel
, &saddr
.sa
, &len
);
4173 if (code
== EWOULDBLOCK
)
4177 if (!NILP (ps
->log
))
4178 call3 (ps
->log
, server
, Qnil
,
4179 concat3 (build_string ("accept failed with code"),
4180 Fnumber_to_string (make_number (code
)),
4181 build_string ("\n")));
4187 /* Setup a new process to handle the connection. */
4189 /* Generate a unique identification of the caller, and build contact
4190 information for this process. */
4193 switch (saddr
.sa
.sa_family
)
4197 Lisp_Object args
[5];
4198 unsigned char *ip
= (unsigned char *)&saddr
.in
.sin_addr
.s_addr
;
4199 args
[0] = build_string ("%d.%d.%d.%d");
4200 args
[1] = make_number (*ip
++);
4201 args
[2] = make_number (*ip
++);
4202 args
[3] = make_number (*ip
++);
4203 args
[4] = make_number (*ip
++);
4204 host
= Fformat (5, args
);
4205 service
= make_number (ntohs (saddr
.in
.sin_port
));
4207 args
[0] = build_string (" <%s:%d>");
4210 caller
= Fformat (3, args
);
4217 Lisp_Object args
[9];
4218 uint16_t *ip6
= (uint16_t *)&saddr
.in6
.sin6_addr
;
4220 args
[0] = build_string ("%x:%x:%x:%x:%x:%x:%x:%x");
4221 for (i
= 0; i
< 8; i
++)
4222 args
[i
+1] = make_number (ntohs (ip6
[i
]));
4223 host
= Fformat (9, args
);
4224 service
= make_number (ntohs (saddr
.in
.sin_port
));
4226 args
[0] = build_string (" <[%s]:%d>");
4229 caller
= Fformat (3, args
);
4234 #ifdef HAVE_LOCAL_SOCKETS
4238 caller
= Fnumber_to_string (make_number (connect_counter
));
4239 caller
= concat3 (build_string (" <"), caller
, build_string (">"));
4243 /* Create a new buffer name for this process if it doesn't have a
4244 filter. The new buffer name is based on the buffer name or
4245 process name of the server process concatenated with the caller
4248 if (!(EQ (ps
->filter
, Qinternal_default_process_filter
)
4249 || EQ (ps
->filter
, Qt
)))
4253 buffer
= ps
->buffer
;
4255 buffer
= Fbuffer_name (buffer
);
4260 buffer
= concat2 (buffer
, caller
);
4261 buffer
= Fget_buffer_create (buffer
);
4265 /* Generate a unique name for the new server process. Combine the
4266 server process name with the caller identification. */
4268 name
= concat2 (ps
->name
, caller
);
4269 proc
= make_process (name
);
4271 chan_process
[s
] = proc
;
4273 fcntl (s
, F_SETFL
, O_NONBLOCK
);
4275 p
= XPROCESS (proc
);
4277 /* Build new contact information for this setup. */
4278 contact
= Fcopy_sequence (ps
->childp
);
4279 contact
= Fplist_put (contact
, QCserver
, Qnil
);
4280 contact
= Fplist_put (contact
, QChost
, host
);
4281 if (!NILP (service
))
4282 contact
= Fplist_put (contact
, QCservice
, service
);
4283 contact
= Fplist_put (contact
, QCremote
,
4284 conv_sockaddr_to_lisp (&saddr
.sa
, len
));
4285 #ifdef HAVE_GETSOCKNAME
4287 if (getsockname (s
, &saddr
.sa
, &len
) == 0)
4288 contact
= Fplist_put (contact
, QClocal
,
4289 conv_sockaddr_to_lisp (&saddr
.sa
, len
));
4292 pset_childp (p
, contact
);
4293 pset_plist (p
, Fcopy_sequence (ps
->plist
));
4294 pset_type (p
, Qnetwork
);
4296 pset_buffer (p
, buffer
);
4297 pset_sentinel (p
, ps
->sentinel
);
4298 pset_filter (p
, ps
->filter
);
4299 pset_command (p
, Qnil
);
4303 pset_status (p
, Qrun
);
4305 /* Client processes for accepted connections are not stopped initially. */
4306 if (!EQ (p
->filter
, Qt
))
4307 add_non_keyboard_read_fd (s
);
4309 /* Setup coding system for new process based on server process.
4310 This seems to be the proper thing to do, as the coding system
4311 of the new process should reflect the settings at the time the
4312 server socket was opened; not the current settings. */
4314 pset_decode_coding_system (p
, ps
->decode_coding_system
);
4315 pset_encode_coding_system (p
, ps
->encode_coding_system
);
4316 setup_process_coding_systems (proc
);
4318 pset_decoding_buf (p
, empty_unibyte_string
);
4319 p
->decoding_carryover
= 0;
4320 pset_encoding_buf (p
, empty_unibyte_string
);
4322 p
->inherit_coding_system_flag
4323 = (NILP (buffer
) ? 0 : ps
->inherit_coding_system_flag
);
4325 if (!NILP (ps
->log
))
4326 call3 (ps
->log
, server
, proc
,
4327 concat3 (build_string ("accept from "),
4328 (STRINGP (host
) ? host
: build_string ("-")),
4329 build_string ("\n")));
4331 exec_sentinel (proc
,
4332 concat3 (build_string ("open from "),
4333 (STRINGP (host
) ? host
: build_string ("-")),
4334 build_string ("\n")));
4338 wait_reading_process_output_unwind (Lisp_Object data
)
4340 clear_waiting_thread_info ();
4341 waiting_for_user_input_p
= XINT (data
);
4345 /* This is here so breakpoints can be put on it. */
4347 wait_reading_process_output_1 (void)
4351 /* Read and dispose of subprocess output while waiting for timeout to
4352 elapse and/or keyboard input to be available.
4356 If negative, gobble data immediately available but don't wait for any.
4359 an additional duration to wait, measured in nanoseconds
4360 If TIME_LIMIT is zero, then:
4361 If NSECS == 0, there is no limit.
4362 If NSECS > 0, the timeout consists of NSECS only.
4363 If NSECS < 0, gobble data immediately, as if TIME_LIMIT were negative.
4366 0 to ignore keyboard input, or
4367 1 to return when input is available, or
4368 -1 meaning caller will actually read the input, so don't throw to
4369 the quit handler, or
4371 DO_DISPLAY means redisplay should be done to show subprocess
4372 output that arrives.
4374 If WAIT_FOR_CELL is a cons cell, wait until its car is non-nil
4375 (and gobble terminal input into the buffer if any arrives).
4377 If WAIT_PROC is specified, wait until something arrives from that
4378 process. The return value is true if we read some input from
4381 If JUST_WAIT_PROC is nonzero, handle only output from WAIT_PROC
4382 (suspending output from other processes). A negative value
4383 means don't run any timers either.
4385 If WAIT_PROC is specified, then the function returns true if we
4386 received input from that process before the timeout elapsed.
4387 Otherwise, return true if we received input from any process. */
4390 wait_reading_process_output (intmax_t time_limit
, int nsecs
, int read_kbd
,
4392 Lisp_Object wait_for_cell
,
4393 struct Lisp_Process
*wait_proc
, int just_wait_proc
)
4396 SELECT_TYPE Available
;
4397 SELECT_TYPE Writeok
;
4403 EMACS_TIME timeout
, end_time
;
4404 int wait_channel
= -1;
4405 bool got_some_input
= 0;
4406 ptrdiff_t count
= SPECPDL_INDEX ();
4408 eassert (wait_proc
== NULL
4409 || EQ (wait_proc
->thread
, Qnil
)
4410 || XTHREAD (wait_proc
->thread
) == current_thread
);
4412 FD_ZERO (&Available
);
4415 if (time_limit
== 0 && nsecs
== 0 && wait_proc
&& !NILP (Vinhibit_quit
)
4416 && !(CONSP (wait_proc
->status
)
4417 && EQ (XCAR (wait_proc
->status
), Qexit
)))
4418 message1 ("Blocking call to accept-process-output with quit inhibited!!");
4420 /* If wait_proc is a process to watch, set wait_channel accordingly. */
4421 if (wait_proc
!= NULL
)
4422 wait_channel
= wait_proc
->infd
;
4424 record_unwind_protect (wait_reading_process_output_unwind
,
4425 make_number (waiting_for_user_input_p
));
4426 waiting_for_user_input_p
= read_kbd
;
4433 else if (TYPE_MAXIMUM (time_t) < time_limit
)
4434 time_limit
= TYPE_MAXIMUM (time_t);
4436 /* Since we may need to wait several times,
4437 compute the absolute time to return at. */
4438 if (time_limit
|| nsecs
> 0)
4440 timeout
= make_emacs_time (time_limit
, nsecs
);
4441 end_time
= add_emacs_time (current_emacs_time (), timeout
);
4446 bool timeout_reduced_for_timers
= 0;
4448 /* If calling from keyboard input, do not quit
4449 since we want to return C-g as an input character.
4450 Otherwise, do pending quit if requested. */
4453 else if (pending_signals
)
4454 process_pending_signals ();
4456 /* Exit now if the cell we're waiting for became non-nil. */
4457 if (! NILP (wait_for_cell
) && ! NILP (XCAR (wait_for_cell
)))
4460 /* Compute time from now till when time limit is up. */
4461 /* Exit if already run out. */
4464 /* A negative timeout means
4465 gobble output available now
4466 but don't wait at all. */
4468 timeout
= make_emacs_time (0, 0);
4470 else if (time_limit
|| nsecs
> 0)
4472 EMACS_TIME now
= current_emacs_time ();
4473 if (EMACS_TIME_LE (end_time
, now
))
4475 timeout
= sub_emacs_time (end_time
, now
);
4479 timeout
= make_emacs_time (100000, 0);
4482 /* Normally we run timers here.
4483 But not if wait_for_cell; in those cases,
4484 the wait is supposed to be short,
4485 and those callers cannot handle running arbitrary Lisp code here. */
4486 if (NILP (wait_for_cell
)
4487 && just_wait_proc
>= 0)
4489 EMACS_TIME timer_delay
;
4493 unsigned old_timers_run
= timers_run
;
4494 struct buffer
*old_buffer
= current_buffer
;
4495 Lisp_Object old_window
= selected_window
;
4497 timer_delay
= timer_check ();
4499 /* If a timer has run, this might have changed buffers
4500 an alike. Make read_key_sequence aware of that. */
4501 if (timers_run
!= old_timers_run
4502 && (old_buffer
!= current_buffer
4503 || !EQ (old_window
, selected_window
))
4504 && waiting_for_user_input_p
== -1)
4505 record_asynch_buffer_change ();
4507 if (timers_run
!= old_timers_run
&& do_display
)
4508 /* We must retry, since a timer may have requeued itself
4509 and that could alter the time_delay. */
4510 redisplay_preserve_echo_area (9);
4514 while (!detect_input_pending ());
4516 /* If there is unread keyboard input, also return. */
4518 && requeued_events_pending_p ())
4521 /* A negative timeout means do not wait at all. */
4524 if (EMACS_TIME_VALID_P (timer_delay
))
4526 if (EMACS_TIME_LT (timer_delay
, timeout
))
4528 timeout
= timer_delay
;
4529 timeout_reduced_for_timers
= 1;
4534 /* This is so a breakpoint can be put here. */
4535 wait_reading_process_output_1 ();
4540 /* Cause C-g and alarm signals to take immediate action,
4541 and cause input available signals to zero out timeout.
4543 It is important that we do this before checking for process
4544 activity. If we get a SIGCHLD after the explicit checks for
4545 process activity, timeout is the only way we will know. */
4547 set_waiting_for_input (&timeout
);
4549 /* If status of something has changed, and no input is
4550 available, notify the user of the change right away. After
4551 this explicit check, we'll let the SIGCHLD handler zap
4552 timeout to get our attention. */
4553 if (update_tick
!= process_tick
)
4558 if (kbd_on_hold_p ())
4561 compute_input_wait_mask (&Atemp
);
4562 compute_write_mask (&Ctemp
);
4564 timeout
= make_emacs_time (0, 0);
4565 if ((thread_select (pselect
, max_desc
+ 1,
4567 #ifdef NON_BLOCKING_CONNECT
4568 (num_pending_connects
> 0 ? &Ctemp
: NULL
),
4572 NULL
, &timeout
, NULL
)
4575 /* It's okay for us to do this and then continue with
4576 the loop, since timeout has already been zeroed out. */
4577 clear_waiting_for_input ();
4578 status_notify (NULL
);
4579 if (do_display
) redisplay_preserve_echo_area (13);
4583 /* Don't wait for output from a non-running process. Just
4584 read whatever data has already been received. */
4585 if (wait_proc
&& wait_proc
->raw_status_new
)
4586 update_status (wait_proc
);
4588 && ! EQ (wait_proc
->status
, Qrun
)
4589 && ! EQ (wait_proc
->status
, Qconnect
))
4591 bool read_some_bytes
= 0;
4593 clear_waiting_for_input ();
4594 XSETPROCESS (proc
, wait_proc
);
4596 /* Read data from the process, until we exhaust it. */
4597 while (wait_proc
->infd
>= 0)
4599 int nread
= read_process_output (proc
, wait_proc
->infd
);
4605 got_some_input
= read_some_bytes
= 1;
4606 else if (nread
== -1 && (errno
== EIO
|| errno
== EAGAIN
))
4609 else if (nread
== -1 && EWOULDBLOCK
== errno
)
4613 if (read_some_bytes
&& do_display
)
4614 redisplay_preserve_echo_area (10);
4619 /* Wait till there is something to do */
4621 if (wait_proc
&& just_wait_proc
)
4623 if (wait_proc
->infd
< 0) /* Terminated */
4625 FD_SET (wait_proc
->infd
, &Available
);
4629 else if (!NILP (wait_for_cell
))
4631 compute_non_process_wait_mask (&Available
);
4638 compute_non_keyboard_wait_mask (&Available
);
4640 compute_input_wait_mask (&Available
);
4641 compute_write_mask (&Writeok
);
4642 #ifdef SELECT_CANT_DO_WRITE_MASK
4647 check_delay
= wait_channel
>= 0 ? 0 : process_output_delay_count
;
4650 /* If frame size has changed or the window is newly mapped,
4651 redisplay now, before we start to wait. There is a race
4652 condition here; if a SIGIO arrives between now and the select
4653 and indicates that a frame is trashed, the select may block
4654 displaying a trashed screen. */
4655 if (frame_garbaged
&& do_display
)
4657 clear_waiting_for_input ();
4658 redisplay_preserve_echo_area (11);
4660 set_waiting_for_input (&timeout
);
4663 /* Skip the `select' call if input is available and we're
4664 waiting for keyboard input or a cell change (which can be
4665 triggered by processing X events). In the latter case, set
4666 nfds to 1 to avoid breaking the loop. */
4668 if ((read_kbd
|| !NILP (wait_for_cell
))
4669 && detect_input_pending ())
4671 nfds
= read_kbd
? 0 : 1;
4678 #ifdef ADAPTIVE_READ_BUFFERING
4679 /* Set the timeout for adaptive read buffering if any
4680 process has non-zero read_output_skip and non-zero
4681 read_output_delay, and we are not reading output for a
4682 specific wait_channel. It is not executed if
4683 Vprocess_adaptive_read_buffering is nil. */
4684 if (process_output_skip
&& check_delay
> 0)
4686 int nsecs
= EMACS_NSECS (timeout
);
4687 if (EMACS_SECS (timeout
) > 0 || nsecs
> READ_OUTPUT_DELAY_MAX
)
4688 nsecs
= READ_OUTPUT_DELAY_MAX
;
4689 for (channel
= 0; check_delay
> 0 && channel
<= max_desc
; channel
++)
4691 proc
= chan_process
[channel
];
4694 /* Find minimum non-zero read_output_delay among the
4695 processes with non-zero read_output_skip. */
4696 if (XPROCESS (proc
)->read_output_delay
> 0)
4699 if (!XPROCESS (proc
)->read_output_skip
)
4701 FD_CLR (channel
, &Available
);
4702 XPROCESS (proc
)->read_output_skip
= 0;
4703 if (XPROCESS (proc
)->read_output_delay
< nsecs
)
4704 nsecs
= XPROCESS (proc
)->read_output_delay
;
4707 timeout
= make_emacs_time (0, nsecs
);
4708 process_output_skip
= 0;
4711 nfds
= thread_select (
4712 #if defined (HAVE_NS)
4714 #elif defined (HAVE_GLIB)
4721 (check_write
? &Writeok
: (SELECT_TYPE
*)0),
4722 NULL
, &timeout
, NULL
);
4725 /* GnuTLS buffers data internally. In lowat mode it leaves
4726 some data in the TCP buffers so that select works, but
4727 with custom pull/push functions we need to check if some
4728 data is available in the buffers manually. */
4733 /* We're not waiting on a specific process, so loop
4734 through all the channels and check for data.
4735 This is a workaround needed for some versions of
4736 the gnutls library -- 2.12.14 has been confirmed
4738 http://comments.gmane.org/gmane.emacs.devel/145074 */
4739 for (channel
= 0; channel
< MAXDESC
; ++channel
)
4740 if (! NILP (chan_process
[channel
]))
4742 struct Lisp_Process
*p
=
4743 XPROCESS (chan_process
[channel
]);
4744 if (p
&& p
->gnutls_p
&& p
->infd
4745 && ((emacs_gnutls_record_check_pending
4750 FD_SET (p
->infd
, &Available
);
4756 /* Check this specific channel. */
4757 if (wait_proc
->gnutls_p
/* Check for valid process. */
4758 /* Do we have pending data? */
4759 && ((emacs_gnutls_record_check_pending
4760 (wait_proc
->gnutls_state
))
4764 /* Set to Available. */
4765 FD_SET (wait_proc
->infd
, &Available
);
4774 /* Make C-g and alarm signals set flags again */
4775 clear_waiting_for_input ();
4777 /* If we woke up due to SIGWINCH, actually change size now. */
4778 do_pending_window_change (0);
4780 if ((time_limit
|| nsecs
) && nfds
== 0 && ! timeout_reduced_for_timers
)
4781 /* We waited the full specified time, so return now. */
4785 if (xerrno
== EINTR
)
4787 else if (xerrno
== EBADF
)
4790 /* AIX doesn't handle PTY closure the same way BSD does. On AIX,
4791 the child's closure of the pts gives the parent a SIGHUP, and
4792 the ptc file descriptor is automatically closed,
4793 yielding EBADF here or at select() call above.
4794 So, SIGHUP is ignored (see def of PTY_TTY_NAME_SPRINTF
4795 in m/ibmrt-aix.h), and here we just ignore the select error.
4796 Cleanup occurs c/o status_notify after SIGCHLD. */
4797 no_avail
= 1; /* Cannot depend on values returned */
4803 error ("select error: %s", emacs_strerror (xerrno
));
4808 FD_ZERO (&Available
);
4812 /* Check for keyboard input */
4813 /* If there is any, return immediately
4814 to give it higher priority than subprocesses */
4818 unsigned old_timers_run
= timers_run
;
4819 struct buffer
*old_buffer
= current_buffer
;
4820 Lisp_Object old_window
= selected_window
;
4823 if (detect_input_pending_run_timers (do_display
))
4825 swallow_events (do_display
);
4826 if (detect_input_pending_run_timers (do_display
))
4830 /* If a timer has run, this might have changed buffers
4831 an alike. Make read_key_sequence aware of that. */
4832 if (timers_run
!= old_timers_run
4833 && waiting_for_user_input_p
== -1
4834 && (old_buffer
!= current_buffer
4835 || !EQ (old_window
, selected_window
)))
4836 record_asynch_buffer_change ();
4842 /* If there is unread keyboard input, also return. */
4844 && requeued_events_pending_p ())
4847 /* If we are not checking for keyboard input now,
4848 do process events (but don't run any timers).
4849 This is so that X events will be processed.
4850 Otherwise they may have to wait until polling takes place.
4851 That would causes delays in pasting selections, for example.
4853 (We used to do this only if wait_for_cell.) */
4854 if (read_kbd
== 0 && detect_input_pending ())
4856 swallow_events (do_display
);
4857 #if 0 /* Exiting when read_kbd doesn't request that seems wrong, though. */
4858 if (detect_input_pending ())
4863 /* Exit now if the cell we're waiting for became non-nil. */
4864 if (! NILP (wait_for_cell
) && ! NILP (XCAR (wait_for_cell
)))
4868 /* If we think we have keyboard input waiting, but didn't get SIGIO,
4869 go read it. This can happen with X on BSD after logging out.
4870 In that case, there really is no input and no SIGIO,
4871 but select says there is input. */
4873 if (read_kbd
&& interrupt_input
4874 && keyboard_bit_set (&Available
) && ! noninteractive
)
4875 handle_input_available_signal (SIGIO
);
4879 got_some_input
|= nfds
> 0;
4881 /* If checking input just got us a size-change event from X,
4882 obey it now if we should. */
4883 if (read_kbd
|| ! NILP (wait_for_cell
))
4884 do_pending_window_change (0);
4886 /* Check for data from a process. */
4887 if (no_avail
|| nfds
== 0)
4890 for (channel
= 0; channel
<= max_desc
; ++channel
)
4892 struct fd_callback_data
*d
= &fd_callback_info
[channel
];
4894 && ((d
->flags
& FOR_READ
4895 && FD_ISSET (channel
, &Available
))
4896 || (d
->flags
& FOR_WRITE
4897 && FD_ISSET (channel
, &Writeok
))))
4898 d
->func (channel
, d
->data
);
4901 for (channel
= 0; channel
<= max_desc
; channel
++)
4903 if (FD_ISSET (channel
, &Available
)
4904 && ((fd_callback_info
[channel
].flags
& (KEYBOARD_FD
| PROCESS_FD
))
4909 /* If waiting for this channel, arrange to return as
4910 soon as no more input to be processed. No more
4912 if (wait_channel
== channel
)
4918 proc
= chan_process
[channel
];
4922 /* If this is a server stream socket, accept connection. */
4923 if (EQ (XPROCESS (proc
)->status
, Qlisten
))
4925 server_accept_connection (proc
, channel
);
4929 /* Read data from the process, starting with our
4930 buffered-ahead character if we have one. */
4932 nread
= read_process_output (proc
, channel
);
4935 /* Since read_process_output can run a filter,
4936 which can call accept-process-output,
4937 don't try to read from any other processes
4938 before doing the select again. */
4939 FD_ZERO (&Available
);
4942 redisplay_preserve_echo_area (12);
4945 else if (nread
== -1 && errno
== EWOULDBLOCK
)
4948 else if (nread
== -1 && errno
== EAGAIN
)
4951 /* FIXME: Is this special case still needed? */
4952 /* Note that we cannot distinguish between no input
4953 available now and a closed pipe.
4954 With luck, a closed pipe will be accompanied by
4955 subprocess termination and SIGCHLD. */
4956 else if (nread
== 0 && !NETCONN_P (proc
) && !SERIALCONN_P (proc
))
4960 /* On some OSs with ptys, when the process on one end of
4961 a pty exits, the other end gets an error reading with
4962 errno = EIO instead of getting an EOF (0 bytes read).
4963 Therefore, if we get an error reading and errno =
4964 EIO, just continue, because the child process has
4965 exited and should clean itself up soon (e.g. when we
4967 else if (nread
== -1 && errno
== EIO
)
4969 struct Lisp_Process
*p
= XPROCESS (proc
);
4971 /* Clear the descriptor now, so we only raise the
4973 delete_read_fd (channel
);
4977 /* If the EIO occurs on a pty, the SIGCHLD handler's
4978 waitpid call will not find the process object to
4979 delete. Do it here. */
4980 p
->tick
= ++process_tick
;
4981 pset_status (p
, Qfailed
);
4984 #endif /* HAVE_PTYS */
4985 /* If we can detect process termination, don't consider the
4986 process gone just because its pipe is closed. */
4987 else if (nread
== 0 && !NETCONN_P (proc
) && !SERIALCONN_P (proc
))
4991 /* Preserve status of processes already terminated. */
4992 XPROCESS (proc
)->tick
= ++process_tick
;
4993 deactivate_process (proc
);
4994 if (XPROCESS (proc
)->raw_status_new
)
4995 update_status (XPROCESS (proc
));
4996 if (EQ (XPROCESS (proc
)->status
, Qrun
))
4997 pset_status (XPROCESS (proc
),
4998 list2 (Qexit
, make_number (256)));
5001 #ifdef NON_BLOCKING_CONNECT
5002 if (FD_ISSET (channel
, &Writeok
)
5003 && (fd_callback_info
[channel
].flags
5004 & NON_BLOCKING_CONNECT_FD
) != 0)
5006 struct Lisp_Process
*p
;
5008 delete_write_fd (channel
);
5010 proc
= chan_process
[channel
];
5014 p
= XPROCESS (proc
);
5017 /* getsockopt(,,SO_ERROR,,) is said to hang on some systems.
5018 So only use it on systems where it is known to work. */
5020 socklen_t xlen
= sizeof (xerrno
);
5021 if (getsockopt (channel
, SOL_SOCKET
, SO_ERROR
, &xerrno
, &xlen
))
5026 struct sockaddr pname
;
5027 int pnamelen
= sizeof (pname
);
5029 /* If connection failed, getpeername will fail. */
5031 if (getpeername (channel
, &pname
, &pnamelen
) < 0)
5033 /* Obtain connect failure code through error slippage. */
5036 if (errno
== ENOTCONN
&& read (channel
, &dummy
, 1) < 0)
5043 p
->tick
= ++process_tick
;
5044 pset_status (p
, list2 (Qfailed
, make_number (xerrno
)));
5045 deactivate_process (proc
);
5049 pset_status (p
, Qrun
);
5050 /* Execute the sentinel here. If we had relied on
5051 status_notify to do it later, it will read input
5052 from the process before calling the sentinel. */
5053 exec_sentinel (proc
, build_string ("open\n"));
5054 if (!EQ (p
->filter
, Qt
) && !EQ (p
->command
, Qt
))
5055 delete_read_fd (p
->infd
);
5058 #endif /* NON_BLOCKING_CONNECT */
5059 } /* End for each file descriptor. */
5060 } /* End while exit conditions not met. */
5062 unbind_to (count
, Qnil
);
5064 /* If calling from keyboard input, do not quit
5065 since we want to return C-g as an input character.
5066 Otherwise, do pending quit if requested. */
5069 /* Prevent input_pending from remaining set if we quit. */
5070 clear_input_pending ();
5074 return got_some_input
;
5077 /* Given a list (FUNCTION ARGS...), apply FUNCTION to the ARGS. */
5080 read_process_output_call (Lisp_Object fun_and_args
)
5082 return apply1 (XCAR (fun_and_args
), XCDR (fun_and_args
));
5086 read_process_output_error_handler (Lisp_Object error_val
)
5088 cmd_error_internal (error_val
, "error in process filter: ");
5090 update_echo_area ();
5091 Fsleep_for (make_number (2), Qnil
);
5096 read_and_dispose_of_process_output (struct Lisp_Process
*p
, char *chars
,
5098 struct coding_system
*coding
);
5100 /* Read pending output from the process channel,
5101 starting with our buffered-ahead character if we have one.
5102 Yield number of decoded characters read.
5104 This function reads at most 4096 characters.
5105 If you want to read all available subprocess output,
5106 you must call it repeatedly until it returns zero.
5108 The characters read are decoded according to PROC's coding-system
5112 read_process_output (Lisp_Object proc
, register int channel
)
5114 register ssize_t nbytes
;
5116 register struct Lisp_Process
*p
= XPROCESS (proc
);
5117 struct coding_system
*coding
= proc_decode_coding_system
[channel
];
5118 int carryover
= p
->decoding_carryover
;
5120 ptrdiff_t count
= SPECPDL_INDEX ();
5121 Lisp_Object odeactivate
;
5123 chars
= alloca (carryover
+ readmax
);
5125 /* See the comment above. */
5126 memcpy (chars
, SDATA (p
->decoding_buf
), carryover
);
5128 #ifdef DATAGRAM_SOCKETS
5129 /* We have a working select, so proc_buffered_char is always -1. */
5130 if (DATAGRAM_CHAN_P (channel
))
5132 socklen_t len
= datagram_address
[channel
].len
;
5133 nbytes
= recvfrom (channel
, chars
+ carryover
, readmax
,
5134 0, datagram_address
[channel
].sa
, &len
);
5139 bool buffered
= proc_buffered_char
[channel
] >= 0;
5142 chars
[carryover
] = proc_buffered_char
[channel
];
5143 proc_buffered_char
[channel
] = -1;
5147 nbytes
= emacs_gnutls_read (p
, chars
+ carryover
+ buffered
,
5148 readmax
- buffered
);
5151 nbytes
= emacs_read (channel
, chars
+ carryover
+ buffered
,
5152 readmax
- buffered
);
5153 #ifdef ADAPTIVE_READ_BUFFERING
5154 if (nbytes
> 0 && p
->adaptive_read_buffering
)
5156 int delay
= p
->read_output_delay
;
5159 if (delay
< READ_OUTPUT_DELAY_MAX_MAX
)
5162 process_output_delay_count
++;
5163 delay
+= READ_OUTPUT_DELAY_INCREMENT
* 2;
5166 else if (delay
> 0 && nbytes
== readmax
- buffered
)
5168 delay
-= READ_OUTPUT_DELAY_INCREMENT
;
5170 process_output_delay_count
--;
5172 p
->read_output_delay
= delay
;
5175 p
->read_output_skip
= 1;
5176 process_output_skip
= 1;
5181 nbytes
+= buffered
&& nbytes
<= 0;
5184 p
->decoding_carryover
= 0;
5186 /* At this point, NBYTES holds number of bytes just received
5187 (including the one in proc_buffered_char[channel]). */
5190 if (nbytes
< 0 || coding
->mode
& CODING_MODE_LAST_BLOCK
)
5192 coding
->mode
|= CODING_MODE_LAST_BLOCK
;
5195 /* Now set NBYTES how many bytes we must decode. */
5196 nbytes
+= carryover
;
5198 odeactivate
= Vdeactivate_mark
;
5199 /* There's no good reason to let process filters change the current
5200 buffer, and many callers of accept-process-output, sit-for, and
5201 friends don't expect current-buffer to be changed from under them. */
5202 record_unwind_current_buffer ();
5204 read_and_dispose_of_process_output (p
, chars
, nbytes
, coding
);
5206 /* Handling the process output should not deactivate the mark. */
5207 Vdeactivate_mark
= odeactivate
;
5209 unbind_to (count
, Qnil
);
5214 read_and_dispose_of_process_output (struct Lisp_Process
*p
, char *chars
,
5216 struct coding_system
*coding
)
5218 Lisp_Object outstream
= p
->filter
;
5220 bool outer_running_asynch_code
= running_asynch_code
;
5221 int waiting
= waiting_for_user_input_p
;
5223 /* No need to gcpro these, because all we do with them later
5224 is test them for EQness, and none of them should be a string. */
5226 Lisp_Object obuffer
, okeymap
;
5227 XSETBUFFER (obuffer
, current_buffer
);
5228 okeymap
= BVAR (current_buffer
, keymap
);
5231 /* We inhibit quit here instead of just catching it so that
5232 hitting ^G when a filter happens to be running won't screw
5234 specbind (Qinhibit_quit
, Qt
);
5235 specbind (Qlast_nonmenu_event
, Qt
);
5237 /* In case we get recursively called,
5238 and we already saved the match data nonrecursively,
5239 save the same match data in safely recursive fashion. */
5240 if (outer_running_asynch_code
)
5243 /* Don't clobber the CURRENT match data, either! */
5244 tem
= Fmatch_data (Qnil
, Qnil
, Qnil
);
5245 restore_search_regs ();
5246 record_unwind_save_match_data ();
5247 Fset_match_data (tem
, Qt
);
5250 /* For speed, if a search happens within this code,
5251 save the match data in a special nonrecursive fashion. */
5252 running_asynch_code
= 1;
5254 decode_coding_c_string (coding
, (unsigned char *) chars
, nbytes
, Qt
);
5255 text
= coding
->dst_object
;
5256 Vlast_coding_system_used
= CODING_ID_NAME (coding
->id
);
5257 /* A new coding system might be found. */
5258 if (!EQ (p
->decode_coding_system
, Vlast_coding_system_used
))
5260 pset_decode_coding_system (p
, Vlast_coding_system_used
);
5262 /* Don't call setup_coding_system for
5263 proc_decode_coding_system[channel] here. It is done in
5264 detect_coding called via decode_coding above. */
5266 /* If a coding system for encoding is not yet decided, we set
5267 it as the same as coding-system for decoding.
5269 But, before doing that we must check if
5270 proc_encode_coding_system[p->outfd] surely points to a
5271 valid memory because p->outfd will be changed once EOF is
5272 sent to the process. */
5273 if (NILP (p
->encode_coding_system
)
5274 && proc_encode_coding_system
[p
->outfd
])
5276 pset_encode_coding_system
5277 (p
, coding_inherit_eol_type (Vlast_coding_system_used
, Qnil
));
5278 setup_coding_system (p
->encode_coding_system
,
5279 proc_encode_coding_system
[p
->outfd
]);
5283 if (coding
->carryover_bytes
> 0)
5285 if (SCHARS (p
->decoding_buf
) < coding
->carryover_bytes
)
5286 pset_decoding_buf (p
, make_uninit_string (coding
->carryover_bytes
));
5287 memcpy (SDATA (p
->decoding_buf
), coding
->carryover
,
5288 coding
->carryover_bytes
);
5289 p
->decoding_carryover
= coding
->carryover_bytes
;
5291 if (SBYTES (text
) > 0)
5292 /* FIXME: It's wrong to wrap or not based on debug-on-error, and
5293 sometimes it's simply wrong to wrap (e.g. when called from
5294 accept-process-output). */
5295 internal_condition_case_1 (read_process_output_call
,
5297 Fcons (make_lisp_proc (p
),
5298 Fcons (text
, Qnil
))),
5299 !NILP (Vdebug_on_error
) ? Qnil
: Qerror
,
5300 read_process_output_error_handler
);
5302 /* If we saved the match data nonrecursively, restore it now. */
5303 restore_search_regs ();
5304 running_asynch_code
= outer_running_asynch_code
;
5306 /* Restore waiting_for_user_input_p as it was
5307 when we were called, in case the filter clobbered it. */
5308 waiting_for_user_input_p
= waiting
;
5310 #if 0 /* Call record_asynch_buffer_change unconditionally,
5311 because we might have changed minor modes or other things
5312 that affect key bindings. */
5313 if (! EQ (Fcurrent_buffer (), obuffer
)
5314 || ! EQ (current_buffer
->keymap
, okeymap
))
5316 /* But do it only if the caller is actually going to read events.
5317 Otherwise there's no need to make him wake up, and it could
5318 cause trouble (for example it would make sit_for return). */
5319 if (waiting_for_user_input_p
== -1)
5320 record_asynch_buffer_change ();
5323 DEFUN ("internal-default-process-filter", Finternal_default_process_filter
,
5324 Sinternal_default_process_filter
, 2, 2, 0,
5325 doc
: /* Function used as default process filter. */)
5326 (Lisp_Object proc
, Lisp_Object text
)
5328 struct Lisp_Process
*p
;
5331 CHECK_PROCESS (proc
);
5332 p
= XPROCESS (proc
);
5333 CHECK_STRING (text
);
5335 if (!NILP (p
->buffer
) && BUFFER_LIVE_P (XBUFFER (p
->buffer
)))
5337 Lisp_Object old_read_only
;
5338 ptrdiff_t old_begv
, old_zv
;
5339 ptrdiff_t old_begv_byte
, old_zv_byte
;
5340 ptrdiff_t before
, before_byte
;
5341 ptrdiff_t opoint_byte
;
5344 Fset_buffer (p
->buffer
);
5346 opoint_byte
= PT_BYTE
;
5347 old_read_only
= BVAR (current_buffer
, read_only
);
5350 old_begv_byte
= BEGV_BYTE
;
5351 old_zv_byte
= ZV_BYTE
;
5353 bset_read_only (current_buffer
, Qnil
);
5355 /* Insert new output into buffer
5356 at the current end-of-output marker,
5357 thus preserving logical ordering of input and output. */
5358 if (XMARKER (p
->mark
)->buffer
)
5359 SET_PT_BOTH (clip_to_bounds (BEGV
,
5360 marker_position (p
->mark
), ZV
),
5361 clip_to_bounds (BEGV_BYTE
,
5362 marker_byte_position (p
->mark
),
5365 SET_PT_BOTH (ZV
, ZV_BYTE
);
5367 before_byte
= PT_BYTE
;
5369 /* If the output marker is outside of the visible region, save
5370 the restriction and widen. */
5371 if (! (BEGV
<= PT
&& PT
<= ZV
))
5374 /* Adjust the multibyteness of TEXT to that of the buffer. */
5375 if (NILP (BVAR (current_buffer
, enable_multibyte_characters
))
5376 != ! STRING_MULTIBYTE (text
))
5377 text
= (STRING_MULTIBYTE (text
)
5378 ? Fstring_as_unibyte (text
)
5379 : Fstring_to_multibyte (text
));
5380 /* Insert before markers in case we are inserting where
5381 the buffer's mark is, and the user's next command is Meta-y. */
5382 insert_from_string_before_markers (text
, 0, 0,
5383 SCHARS (text
), SBYTES (text
), 0);
5385 /* Make sure the process marker's position is valid when the
5386 process buffer is changed in the signal_after_change above.
5387 W3 is known to do that. */
5388 if (BUFFERP (p
->buffer
)
5389 && (b
= XBUFFER (p
->buffer
), b
!= current_buffer
))
5390 set_marker_both (p
->mark
, p
->buffer
, BUF_PT (b
), BUF_PT_BYTE (b
));
5392 set_marker_both (p
->mark
, p
->buffer
, PT
, PT_BYTE
);
5394 update_mode_lines
++;
5396 /* Make sure opoint and the old restrictions
5397 float ahead of any new text just as point would. */
5398 if (opoint
>= before
)
5400 opoint
+= PT
- before
;
5401 opoint_byte
+= PT_BYTE
- before_byte
;
5403 if (old_begv
> before
)
5405 old_begv
+= PT
- before
;
5406 old_begv_byte
+= PT_BYTE
- before_byte
;
5408 if (old_zv
>= before
)
5410 old_zv
+= PT
- before
;
5411 old_zv_byte
+= PT_BYTE
- before_byte
;
5414 /* If the restriction isn't what it should be, set it. */
5415 if (old_begv
!= BEGV
|| old_zv
!= ZV
)
5416 Fnarrow_to_region (make_number (old_begv
), make_number (old_zv
));
5418 bset_read_only (current_buffer
, old_read_only
);
5419 SET_PT_BOTH (opoint
, opoint_byte
);
5424 /* Sending data to subprocess. */
5426 /* In send_process, when a write fails temporarily,
5427 wait_reading_process_output is called. It may execute user code,
5428 e.g. timers, that attempts to write new data to the same process.
5429 We must ensure that data is sent in the right order, and not
5430 interspersed half-completed with other writes (Bug#10815). This is
5431 handled by the write_queue element of struct process. It is a list
5432 with each entry having the form
5434 (string . (offset . length))
5436 where STRING is a lisp string, OFFSET is the offset into the
5437 string's byte sequence from which we should begin to send, and
5438 LENGTH is the number of bytes left to send. */
5440 /* Create a new entry in write_queue.
5441 INPUT_OBJ should be a buffer, string Qt, or Qnil.
5442 BUF is a pointer to the string sequence of the input_obj or a C
5443 string in case of Qt or Qnil. */
5446 write_queue_push (struct Lisp_Process
*p
, Lisp_Object input_obj
,
5447 const char *buf
, ptrdiff_t len
, bool front
)
5450 Lisp_Object entry
, obj
;
5452 if (STRINGP (input_obj
))
5454 offset
= buf
- SSDATA (input_obj
);
5460 obj
= make_unibyte_string (buf
, len
);
5463 entry
= Fcons (obj
, Fcons (make_number (offset
), make_number (len
)));
5466 pset_write_queue (p
, Fcons (entry
, p
->write_queue
));
5468 pset_write_queue (p
, nconc2 (p
->write_queue
, Fcons (entry
, Qnil
)));
5471 /* Remove the first element in the write_queue of process P, put its
5472 contents in OBJ, BUF and LEN, and return true. If the
5473 write_queue is empty, return false. */
5476 write_queue_pop (struct Lisp_Process
*p
, Lisp_Object
*obj
,
5477 const char **buf
, ptrdiff_t *len
)
5479 Lisp_Object entry
, offset_length
;
5482 if (NILP (p
->write_queue
))
5485 entry
= XCAR (p
->write_queue
);
5486 pset_write_queue (p
, XCDR (p
->write_queue
));
5488 *obj
= XCAR (entry
);
5489 offset_length
= XCDR (entry
);
5491 *len
= XINT (XCDR (offset_length
));
5492 offset
= XINT (XCAR (offset_length
));
5493 *buf
= SSDATA (*obj
) + offset
;
5498 /* Send some data to process PROC.
5499 BUF is the beginning of the data; LEN is the number of characters.
5500 OBJECT is the Lisp object that the data comes from. If OBJECT is
5501 nil or t, it means that the data comes from C string.
5503 If OBJECT is not nil, the data is encoded by PROC's coding-system
5504 for encoding before it is sent.
5506 This function can evaluate Lisp code and can garbage collect. */
5509 send_process (Lisp_Object proc
, const char *buf
, ptrdiff_t len
,
5512 struct Lisp_Process
*p
= XPROCESS (proc
);
5514 struct coding_system
*coding
;
5516 if (p
->raw_status_new
)
5518 if (! EQ (p
->status
, Qrun
))
5519 error ("Process %s not running", SDATA (p
->name
));
5521 error ("Output file descriptor of %s is closed", SDATA (p
->name
));
5523 coding
= proc_encode_coding_system
[p
->outfd
];
5524 Vlast_coding_system_used
= CODING_ID_NAME (coding
->id
);
5526 if ((STRINGP (object
) && STRING_MULTIBYTE (object
))
5527 || (BUFFERP (object
)
5528 && !NILP (BVAR (XBUFFER (object
), enable_multibyte_characters
)))
5531 pset_encode_coding_system
5532 (p
, complement_process_encoding_system (p
->encode_coding_system
));
5533 if (!EQ (Vlast_coding_system_used
, p
->encode_coding_system
))
5535 /* The coding system for encoding was changed to raw-text
5536 because we sent a unibyte text previously. Now we are
5537 sending a multibyte text, thus we must encode it by the
5538 original coding system specified for the current process.
5540 Another reason we come here is that the coding system
5541 was just complemented and a new one was returned by
5542 complement_process_encoding_system. */
5543 setup_coding_system (p
->encode_coding_system
, coding
);
5544 Vlast_coding_system_used
= p
->encode_coding_system
;
5546 coding
->src_multibyte
= 1;
5550 coding
->src_multibyte
= 0;
5551 /* For sending a unibyte text, character code conversion should
5552 not take place but EOL conversion should. So, setup raw-text
5553 or one of the subsidiary if we have not yet done it. */
5554 if (CODING_REQUIRE_ENCODING (coding
))
5556 if (CODING_REQUIRE_FLUSHING (coding
))
5558 /* But, before changing the coding, we must flush out data. */
5559 coding
->mode
|= CODING_MODE_LAST_BLOCK
;
5560 send_process (proc
, "", 0, Qt
);
5561 coding
->mode
&= CODING_MODE_LAST_BLOCK
;
5563 setup_coding_system (raw_text_coding_system
5564 (Vlast_coding_system_used
),
5566 coding
->src_multibyte
= 0;
5569 coding
->dst_multibyte
= 0;
5571 if (CODING_REQUIRE_ENCODING (coding
))
5573 coding
->dst_object
= Qt
;
5574 if (BUFFERP (object
))
5576 ptrdiff_t from_byte
, from
, to
;
5577 ptrdiff_t save_pt
, save_pt_byte
;
5578 struct buffer
*cur
= current_buffer
;
5580 set_buffer_internal (XBUFFER (object
));
5581 save_pt
= PT
, save_pt_byte
= PT_BYTE
;
5583 from_byte
= PTR_BYTE_POS ((unsigned char *) buf
);
5584 from
= BYTE_TO_CHAR (from_byte
);
5585 to
= BYTE_TO_CHAR (from_byte
+ len
);
5586 TEMP_SET_PT_BOTH (from
, from_byte
);
5587 encode_coding_object (coding
, object
, from
, from_byte
,
5588 to
, from_byte
+ len
, Qt
);
5589 TEMP_SET_PT_BOTH (save_pt
, save_pt_byte
);
5590 set_buffer_internal (cur
);
5592 else if (STRINGP (object
))
5594 encode_coding_object (coding
, object
, 0, 0, SCHARS (object
),
5595 SBYTES (object
), Qt
);
5599 coding
->dst_object
= make_unibyte_string (buf
, len
);
5600 coding
->produced
= len
;
5603 len
= coding
->produced
;
5604 object
= coding
->dst_object
;
5605 buf
= SSDATA (object
);
5608 /* If there is already data in the write_queue, put the new data
5609 in the back of queue. Otherwise, ignore it. */
5610 if (!NILP (p
->write_queue
))
5611 write_queue_push (p
, object
, buf
, len
, 0);
5613 do /* while !NILP (p->write_queue) */
5615 ptrdiff_t cur_len
= -1;
5616 const char *cur_buf
;
5617 Lisp_Object cur_object
;
5619 /* If write_queue is empty, ignore it. */
5620 if (!write_queue_pop (p
, &cur_object
, &cur_buf
, &cur_len
))
5624 cur_object
= object
;
5629 /* Send this batch, using one or more write calls. */
5630 ptrdiff_t written
= 0;
5631 int outfd
= p
->outfd
;
5632 #ifdef DATAGRAM_SOCKETS
5633 if (DATAGRAM_CHAN_P (outfd
))
5635 rv
= sendto (outfd
, cur_buf
, cur_len
,
5636 0, datagram_address
[outfd
].sa
,
5637 datagram_address
[outfd
].len
);
5640 else if (errno
== EMSGSIZE
)
5641 report_file_error ("sending datagram", Fcons (proc
, Qnil
));
5648 written
= emacs_gnutls_write (p
, cur_buf
, cur_len
);
5651 written
= emacs_write (outfd
, cur_buf
, cur_len
);
5652 rv
= (written
? 0 : -1);
5653 #ifdef ADAPTIVE_READ_BUFFERING
5654 if (p
->read_output_delay
> 0
5655 && p
->adaptive_read_buffering
== 1)
5657 p
->read_output_delay
= 0;
5658 process_output_delay_count
--;
5659 p
->read_output_skip
= 0;
5668 || errno
== EWOULDBLOCK
5671 /* Buffer is full. Wait, accepting input;
5672 that may allow the program
5673 to finish doing output and read more. */
5675 #ifdef BROKEN_PTY_READ_AFTER_EAGAIN
5676 /* A gross hack to work around a bug in FreeBSD.
5677 In the following sequence, read(2) returns
5681 write(2) 954 bytes, get EAGAIN
5682 read(2) 1024 bytes in process_read_output
5683 read(2) 11 bytes in process_read_output
5685 That is, read(2) returns more bytes than have
5686 ever been written successfully. The 1033 bytes
5687 read are the 1022 bytes written successfully
5688 after processing (for example with CRs added if
5689 the terminal is set up that way which it is
5690 here). The same bytes will be seen again in a
5691 later read(2), without the CRs. */
5693 if (errno
== EAGAIN
)
5696 ioctl (p
->outfd
, TIOCFLUSH
, &flags
);
5698 #endif /* BROKEN_PTY_READ_AFTER_EAGAIN */
5700 /* Put what we should have written in wait_queue. */
5701 write_queue_push (p
, cur_object
, cur_buf
, cur_len
, 1);
5702 wait_reading_process_output (0, 20 * 1000 * 1000,
5703 0, 0, Qnil
, NULL
, 0);
5704 /* Reread queue, to see what is left. */
5707 else if (errno
== EPIPE
)
5709 p
->raw_status_new
= 0;
5710 pset_status (p
, list2 (Qexit
, make_number (256)));
5711 p
->tick
= ++process_tick
;
5712 deactivate_process (proc
);
5713 error ("process %s no longer connected to pipe; closed it",
5717 /* This is a real error. */
5718 report_file_error ("writing to process", Fcons (proc
, Qnil
));
5724 while (!NILP (p
->write_queue
));
5727 DEFUN ("process-send-region", Fprocess_send_region
, Sprocess_send_region
,
5729 doc
: /* Send current contents of region as input to PROCESS.
5730 PROCESS may be a process, a buffer, the name of a process or buffer, or
5731 nil, indicating the current buffer's process.
5732 Called from program, takes three arguments, PROCESS, START and END.
5733 If the region is more than 500 characters long,
5734 it is sent in several bunches. This may happen even for shorter regions.
5735 Output from processes can arrive in between bunches. */)
5736 (Lisp_Object process
, Lisp_Object start
, Lisp_Object end
)
5738 Lisp_Object proc
= get_process (process
);
5739 ptrdiff_t start_byte
, end_byte
;
5741 validate_region (&start
, &end
);
5743 start_byte
= CHAR_TO_BYTE (XINT (start
));
5744 end_byte
= CHAR_TO_BYTE (XINT (end
));
5746 if (XINT (start
) < GPT
&& XINT (end
) > GPT
)
5747 move_gap_both (XINT (start
), start_byte
);
5749 send_process (proc
, (char *) BYTE_POS_ADDR (start_byte
),
5750 end_byte
- start_byte
, Fcurrent_buffer ());
5755 DEFUN ("process-send-string", Fprocess_send_string
, Sprocess_send_string
,
5757 doc
: /* Send PROCESS the contents of STRING as input.
5758 PROCESS may be a process, a buffer, the name of a process or buffer, or
5759 nil, indicating the current buffer's process.
5760 If STRING is more than 500 characters long,
5761 it is sent in several bunches. This may happen even for shorter strings.
5762 Output from processes can arrive in between bunches. */)
5763 (Lisp_Object process
, Lisp_Object string
)
5766 CHECK_STRING (string
);
5767 proc
= get_process (process
);
5768 send_process (proc
, SSDATA (string
),
5769 SBYTES (string
), string
);
5773 /* Return the foreground process group for the tty/pty that
5774 the process P uses. */
5776 emacs_get_tty_pgrp (struct Lisp_Process
*p
)
5781 if (ioctl (p
->infd
, TIOCGPGRP
, &gid
) == -1 && ! NILP (p
->tty_name
))
5784 /* Some OS:es (Solaris 8/9) does not allow TIOCGPGRP from the
5785 master side. Try the slave side. */
5786 fd
= emacs_open (SSDATA (p
->tty_name
), O_RDONLY
, 0);
5790 ioctl (fd
, TIOCGPGRP
, &gid
);
5794 #endif /* defined (TIOCGPGRP ) */
5799 DEFUN ("process-running-child-p", Fprocess_running_child_p
,
5800 Sprocess_running_child_p
, 0, 1, 0,
5801 doc
: /* Return t if PROCESS has given the terminal to a child.
5802 If the operating system does not make it possible to find out,
5803 return t unconditionally. */)
5804 (Lisp_Object process
)
5806 /* Initialize in case ioctl doesn't exist or gives an error,
5807 in a way that will cause returning t. */
5810 struct Lisp_Process
*p
;
5812 proc
= get_process (process
);
5813 p
= XPROCESS (proc
);
5815 if (!EQ (p
->type
, Qreal
))
5816 error ("Process %s is not a subprocess",
5819 error ("Process %s is not active",
5822 gid
= emacs_get_tty_pgrp (p
);
5829 /* send a signal number SIGNO to PROCESS.
5830 If CURRENT_GROUP is t, that means send to the process group
5831 that currently owns the terminal being used to communicate with PROCESS.
5832 This is used for various commands in shell mode.
5833 If CURRENT_GROUP is lambda, that means send to the process group
5834 that currently owns the terminal, but only if it is NOT the shell itself.
5836 If NOMSG is false, insert signal-announcements into process's buffers
5839 If we can, we try to signal PROCESS by sending control characters
5840 down the pty. This allows us to signal inferiors who have changed
5841 their uid, for which kill would return an EPERM error. */
5844 process_send_signal (Lisp_Object process
, int signo
, Lisp_Object current_group
,
5848 struct Lisp_Process
*p
;
5852 proc
= get_process (process
);
5853 p
= XPROCESS (proc
);
5855 if (!EQ (p
->type
, Qreal
))
5856 error ("Process %s is not a subprocess",
5859 error ("Process %s is not active",
5863 current_group
= Qnil
;
5865 /* If we are using pgrps, get a pgrp number and make it negative. */
5866 if (NILP (current_group
))
5867 /* Send the signal to the shell's process group. */
5871 #ifdef SIGNALS_VIA_CHARACTERS
5872 /* If possible, send signals to the entire pgrp
5873 by sending an input character to it. */
5876 cc_t
*sig_char
= NULL
;
5878 tcgetattr (p
->infd
, &t
);
5883 sig_char
= &t
.c_cc
[VINTR
];
5887 sig_char
= &t
.c_cc
[VQUIT
];
5891 #if defined (VSWTCH) && !defined (PREFER_VSUSP)
5892 sig_char
= &t
.c_cc
[VSWTCH
];
5894 sig_char
= &t
.c_cc
[VSUSP
];
5899 if (sig_char
&& *sig_char
!= CDISABLE
)
5901 send_process (proc
, (char *) sig_char
, 1, Qnil
);
5904 /* If we can't send the signal with a character,
5905 fall through and send it another way. */
5907 /* The code above may fall through if it can't
5908 handle the signal. */
5909 #endif /* defined (SIGNALS_VIA_CHARACTERS) */
5912 /* Get the current pgrp using the tty itself, if we have that.
5913 Otherwise, use the pty to get the pgrp.
5914 On pfa systems, saka@pfu.fujitsu.co.JP writes:
5915 "TIOCGPGRP symbol defined in sys/ioctl.h at E50.
5916 But, TIOCGPGRP does not work on E50 ;-P works fine on E60"
5917 His patch indicates that if TIOCGPGRP returns an error, then
5918 we should just assume that p->pid is also the process group id. */
5920 gid
= emacs_get_tty_pgrp (p
);
5923 /* If we can't get the information, assume
5924 the shell owns the tty. */
5927 /* It is not clear whether anything really can set GID to -1.
5928 Perhaps on some system one of those ioctls can or could do so.
5929 Or perhaps this is vestigial. */
5932 #else /* ! defined (TIOCGPGRP ) */
5933 /* Can't select pgrps on this system, so we know that
5934 the child itself heads the pgrp. */
5936 #endif /* ! defined (TIOCGPGRP ) */
5938 /* If current_group is lambda, and the shell owns the terminal,
5939 don't send any signal. */
5940 if (EQ (current_group
, Qlambda
) && gid
== p
->pid
)
5948 p
->raw_status_new
= 0;
5949 pset_status (p
, Qrun
);
5950 p
->tick
= ++process_tick
;
5953 status_notify (NULL
);
5954 redisplay_preserve_echo_area (13);
5957 #endif /* ! defined (SIGCONT) */
5961 flush_pending_output (p
->infd
);
5965 /* If we don't have process groups, send the signal to the immediate
5966 subprocess. That isn't really right, but it's better than any
5967 obvious alternative. */
5970 kill (p
->pid
, signo
);
5974 /* gid may be a pid, or minus a pgrp's number */
5976 if (!NILP (current_group
))
5978 if (ioctl (p
->infd
, TIOCSIGSEND
, signo
) == -1)
5986 #else /* ! defined (TIOCSIGSEND) */
5988 #endif /* ! defined (TIOCSIGSEND) */
5991 DEFUN ("interrupt-process", Finterrupt_process
, Sinterrupt_process
, 0, 2, 0,
5992 doc
: /* Interrupt process PROCESS.
5993 PROCESS may be a process, a buffer, or the name of a process or buffer.
5994 No arg or nil means current buffer's process.
5995 Second arg CURRENT-GROUP non-nil means send signal to
5996 the current process-group of the process's controlling terminal
5997 rather than to the process's own process group.
5998 If the process is a shell, this means interrupt current subjob
5999 rather than the shell.
6001 If CURRENT-GROUP is `lambda', and if the shell owns the terminal,
6002 don't send the signal. */)
6003 (Lisp_Object process
, Lisp_Object current_group
)
6005 process_send_signal (process
, SIGINT
, current_group
, 0);
6009 DEFUN ("kill-process", Fkill_process
, Skill_process
, 0, 2, 0,
6010 doc
: /* Kill process PROCESS. May be process or name of one.
6011 See function `interrupt-process' for more details on usage. */)
6012 (Lisp_Object process
, Lisp_Object current_group
)
6014 process_send_signal (process
, SIGKILL
, current_group
, 0);
6018 DEFUN ("quit-process", Fquit_process
, Squit_process
, 0, 2, 0,
6019 doc
: /* Send QUIT signal to process PROCESS. May be process or name of one.
6020 See function `interrupt-process' for more details on usage. */)
6021 (Lisp_Object process
, Lisp_Object current_group
)
6023 process_send_signal (process
, SIGQUIT
, current_group
, 0);
6027 DEFUN ("stop-process", Fstop_process
, Sstop_process
, 0, 2, 0,
6028 doc
: /* Stop process PROCESS. May be process or name of one.
6029 See function `interrupt-process' for more details on usage.
6030 If PROCESS is a network or serial process, inhibit handling of incoming
6032 (Lisp_Object process
, Lisp_Object current_group
)
6034 if (PROCESSP (process
) && (NETCONN_P (process
) || SERIALCONN_P (process
)))
6036 struct Lisp_Process
*p
;
6038 p
= XPROCESS (process
);
6039 if (NILP (p
->command
)
6041 delete_read_fd (p
->infd
);
6042 pset_command (p
, Qt
);
6046 error ("No SIGTSTP support");
6048 process_send_signal (process
, SIGTSTP
, current_group
, 0);
6053 DEFUN ("continue-process", Fcontinue_process
, Scontinue_process
, 0, 2, 0,
6054 doc
: /* Continue process PROCESS. May be process or name of one.
6055 See function `interrupt-process' for more details on usage.
6056 If PROCESS is a network or serial process, resume handling of incoming
6058 (Lisp_Object process
, Lisp_Object current_group
)
6060 if (PROCESSP (process
) && (NETCONN_P (process
) || SERIALCONN_P (process
)))
6062 struct Lisp_Process
*p
;
6064 p
= XPROCESS (process
);
6065 if (EQ (p
->command
, Qt
)
6067 && (!EQ (p
->filter
, Qt
) || EQ (p
->status
, Qlisten
)))
6069 add_non_keyboard_read_fd (p
->infd
);
6071 if (fd_info
[ p
->infd
].flags
& FILE_SERIAL
)
6072 PurgeComm (fd_info
[ p
->infd
].hnd
, PURGE_RXABORT
| PURGE_RXCLEAR
);
6073 #else /* not WINDOWSNT */
6074 tcflush (p
->infd
, TCIFLUSH
);
6075 #endif /* not WINDOWSNT */
6077 pset_command (p
, Qnil
);
6081 process_send_signal (process
, SIGCONT
, current_group
, 0);
6083 error ("No SIGCONT support");
6088 /* Return the integer value of the signal whose abbreviation is ABBR,
6089 or a negative number if there is no such signal. */
6091 abbr_to_signal (char const *name
)
6094 char sigbuf
[20]; /* Large enough for all valid signal abbreviations. */
6096 if (!strncmp (name
, "SIG", 3) || !strncmp (name
, "sig", 3))
6099 for (i
= 0; i
< sizeof sigbuf
; i
++)
6101 sigbuf
[i
] = c_toupper (name
[i
]);
6103 return str2sig (sigbuf
, &signo
) == 0 ? signo
: -1;
6109 DEFUN ("signal-process", Fsignal_process
, Ssignal_process
,
6110 2, 2, "sProcess (name or number): \nnSignal code: ",
6111 doc
: /* Send PROCESS the signal with code SIGCODE.
6112 PROCESS may also be a number specifying the process id of the
6113 process to signal; in this case, the process need not be a child of
6115 SIGCODE may be an integer, or a symbol whose name is a signal name. */)
6116 (Lisp_Object process
, Lisp_Object sigcode
)
6121 if (STRINGP (process
))
6123 Lisp_Object tem
= Fget_process (process
);
6126 Lisp_Object process_number
=
6127 string_to_number (SSDATA (process
), 10, 1);
6128 if (INTEGERP (process_number
) || FLOATP (process_number
))
6129 tem
= process_number
;
6133 else if (!NUMBERP (process
))
6134 process
= get_process (process
);
6139 if (NUMBERP (process
))
6140 CONS_TO_INTEGER (process
, pid_t
, pid
);
6143 CHECK_PROCESS (process
);
6144 pid
= XPROCESS (process
)->pid
;
6146 error ("Cannot signal process %s", SDATA (XPROCESS (process
)->name
));
6149 if (INTEGERP (sigcode
))
6151 CHECK_TYPE_RANGED_INTEGER (int, sigcode
);
6152 signo
= XINT (sigcode
);
6158 CHECK_SYMBOL (sigcode
);
6159 name
= SSDATA (SYMBOL_NAME (sigcode
));
6161 signo
= abbr_to_signal (name
);
6163 error ("Undefined signal name %s", name
);
6166 return make_number (kill (pid
, signo
));
6169 DEFUN ("process-send-eof", Fprocess_send_eof
, Sprocess_send_eof
, 0, 1, 0,
6170 doc
: /* Make PROCESS see end-of-file in its input.
6171 EOF comes after any text already sent to it.
6172 PROCESS may be a process, a buffer, the name of a process or buffer, or
6173 nil, indicating the current buffer's process.
6174 If PROCESS is a network connection, or is a process communicating
6175 through a pipe (as opposed to a pty), then you cannot send any more
6176 text to PROCESS after you call this function.
6177 If PROCESS is a serial process, wait until all output written to the
6178 process has been transmitted to the serial port. */)
6179 (Lisp_Object process
)
6182 struct coding_system
*coding
;
6184 if (DATAGRAM_CONN_P (process
))
6187 proc
= get_process (process
);
6188 coding
= proc_encode_coding_system
[XPROCESS (proc
)->outfd
];
6190 /* Make sure the process is really alive. */
6191 if (XPROCESS (proc
)->raw_status_new
)
6192 update_status (XPROCESS (proc
));
6193 if (! EQ (XPROCESS (proc
)->status
, Qrun
))
6194 error ("Process %s not running", SDATA (XPROCESS (proc
)->name
));
6196 if (CODING_REQUIRE_FLUSHING (coding
))
6198 coding
->mode
|= CODING_MODE_LAST_BLOCK
;
6199 send_process (proc
, "", 0, Qnil
);
6202 if (XPROCESS (proc
)->pty_flag
)
6203 send_process (proc
, "\004", 1, Qnil
);
6204 else if (EQ (XPROCESS (proc
)->type
, Qserial
))
6207 if (tcdrain (XPROCESS (proc
)->outfd
) != 0)
6208 error ("tcdrain() failed: %s", emacs_strerror (errno
));
6209 #endif /* not WINDOWSNT */
6210 /* Do nothing on Windows because writes are blocking. */
6214 int old_outfd
, new_outfd
;
6216 #ifdef HAVE_SHUTDOWN
6217 /* If this is a network connection, or socketpair is used
6218 for communication with the subprocess, call shutdown to cause EOF.
6219 (In some old system, shutdown to socketpair doesn't work.
6220 Then we just can't win.) */
6221 if (EQ (XPROCESS (proc
)->type
, Qnetwork
)
6222 || XPROCESS (proc
)->outfd
== XPROCESS (proc
)->infd
)
6223 shutdown (XPROCESS (proc
)->outfd
, 1);
6224 /* In case of socketpair, outfd == infd, so don't close it. */
6225 if (XPROCESS (proc
)->outfd
!= XPROCESS (proc
)->infd
)
6226 emacs_close (XPROCESS (proc
)->outfd
);
6227 #else /* not HAVE_SHUTDOWN */
6228 emacs_close (XPROCESS (proc
)->outfd
);
6229 #endif /* not HAVE_SHUTDOWN */
6230 new_outfd
= emacs_open (NULL_DEVICE
, O_WRONLY
, 0);
6233 old_outfd
= XPROCESS (proc
)->outfd
;
6235 if (!proc_encode_coding_system
[new_outfd
])
6236 proc_encode_coding_system
[new_outfd
]
6237 = xmalloc (sizeof (struct coding_system
));
6238 *proc_encode_coding_system
[new_outfd
]
6239 = *proc_encode_coding_system
[old_outfd
];
6240 memset (proc_encode_coding_system
[old_outfd
], 0,
6241 sizeof (struct coding_system
));
6243 XPROCESS (proc
)->outfd
= new_outfd
;
6248 /* The main Emacs thread records child processes in three places:
6250 - Vprocess_alist, for asynchronous subprocesses, which are child
6251 processes visible to Lisp.
6253 - deleted_pid_list, for child processes invisible to Lisp,
6254 typically because of delete-process. These are recorded so that
6255 the processes can be reaped when they exit, so that the operating
6256 system's process table is not cluttered by zombies.
6258 - the local variable PID in Fcall_process, call_process_cleanup and
6259 call_process_kill, for synchronous subprocesses.
6260 record_unwind_protect is used to make sure this process is not
6261 forgotten: if the user interrupts call-process and the child
6262 process refuses to exit immediately even with two C-g's,
6263 call_process_kill adds PID's contents to deleted_pid_list before
6266 The main Emacs thread invokes waitpid only on child processes that
6267 it creates and that have not been reaped. This avoid races on
6268 platforms such as GTK, where other threads create their own
6269 subprocesses which the main thread should not reap. For example,
6270 if the main thread attempted to reap an already-reaped child, it
6271 might inadvertently reap a GTK-created process that happened to
6272 have the same process ID. */
6274 /* LIB_CHILD_HANDLER is a SIGCHLD handler that Emacs calls while doing
6275 its own SIGCHLD handling. On POSIXish systems, glib needs this to
6276 keep track of its own children. GNUstep is similar. */
6278 static void dummy_handler (int sig
) {}
6279 static signal_handler_t
volatile lib_child_handler
;
6281 /* Handle a SIGCHLD signal by looking for known child processes of
6282 Emacs whose status have changed. For each one found, record its
6285 All we do is change the status; we do not run sentinels or print
6286 notifications. That is saved for the next time keyboard input is
6287 done, in order to avoid timing errors.
6289 ** WARNING: this can be called during garbage collection.
6290 Therefore, it must not be fooled by the presence of mark bits in
6293 ** USG WARNING: Although it is not obvious from the documentation
6294 in signal(2), on a USG system the SIGCLD handler MUST NOT call
6295 signal() before executing at least one wait(), otherwise the
6296 handler will be called again, resulting in an infinite loop. The
6297 relevant portion of the documentation reads "SIGCLD signals will be
6298 queued and the signal-catching function will be continually
6299 reentered until the queue is empty". Invoking signal() causes the
6300 kernel to reexamine the SIGCLD queue. Fred Fish, UniSoft Systems
6303 ** Malloc WARNING: This should never call malloc either directly or
6304 indirectly; if it does, that is a bug */
6307 handle_child_signal (int sig
)
6311 /* Find the process that signaled us, and record its status. */
6313 /* The process can have been deleted by Fdelete_process, or have
6314 been started asynchronously by Fcall_process. */
6315 for (tail
= deleted_pid_list
; CONSP (tail
); tail
= XCDR (tail
))
6317 bool all_pids_are_fixnums
6318 = (MOST_NEGATIVE_FIXNUM
<= TYPE_MINIMUM (pid_t
)
6319 && TYPE_MAXIMUM (pid_t
) <= MOST_POSITIVE_FIXNUM
);
6320 Lisp_Object xpid
= XCAR (tail
);
6321 if (all_pids_are_fixnums
? INTEGERP (xpid
) : NUMBERP (xpid
))
6324 if (INTEGERP (xpid
))
6325 deleted_pid
= XINT (xpid
);
6327 deleted_pid
= XFLOAT_DATA (xpid
);
6328 if (child_status_changed (deleted_pid
, 0, 0))
6329 XSETCAR (tail
, Qnil
);
6333 /* Otherwise, if it is asynchronous, it is in Vprocess_alist. */
6334 for (tail
= Vprocess_alist
; CONSP (tail
); tail
= XCDR (tail
))
6336 Lisp_Object proc
= XCDR (XCAR (tail
));
6337 struct Lisp_Process
*p
= XPROCESS (proc
);
6341 && child_status_changed (p
->pid
, &status
, WUNTRACED
| WCONTINUED
))
6343 /* Change the status of the process that was found. */
6344 p
->tick
= ++process_tick
;
6345 p
->raw_status
= status
;
6346 p
->raw_status_new
= 1;
6348 /* If process has terminated, stop waiting for its output. */
6349 if (WIFSIGNALED (status
) || WIFEXITED (status
))
6351 bool clear_desc_flag
= 0;
6354 clear_desc_flag
= 1;
6356 /* clear_desc_flag avoids a compiler bug in Microsoft C. */
6357 if (clear_desc_flag
)
6358 delete_read_fd (p
->infd
);
6363 lib_child_handler (sig
);
6364 #ifdef NS_IMPL_GNUSTEP
6365 /* NSTask in GNUStep sets its child handler each time it is called.
6366 So we must re-set ours. */
6367 catch_child_signal();
6372 deliver_child_signal (int sig
)
6374 deliver_process_signal (sig
, handle_child_signal
);
6379 exec_sentinel_error_handler (Lisp_Object error_val
)
6381 cmd_error_internal (error_val
, "error in process sentinel: ");
6383 update_echo_area ();
6384 Fsleep_for (make_number (2), Qnil
);
6389 exec_sentinel (Lisp_Object proc
, Lisp_Object reason
)
6391 Lisp_Object sentinel
, odeactivate
;
6392 struct Lisp_Process
*p
= XPROCESS (proc
);
6393 ptrdiff_t count
= SPECPDL_INDEX ();
6394 bool outer_running_asynch_code
= running_asynch_code
;
6395 int waiting
= waiting_for_user_input_p
;
6397 if (inhibit_sentinels
)
6400 /* No need to gcpro these, because all we do with them later
6401 is test them for EQness, and none of them should be a string. */
6402 odeactivate
= Vdeactivate_mark
;
6404 Lisp_Object obuffer
, okeymap
;
6405 XSETBUFFER (obuffer
, current_buffer
);
6406 okeymap
= BVAR (current_buffer
, keymap
);
6409 /* There's no good reason to let sentinels change the current
6410 buffer, and many callers of accept-process-output, sit-for, and
6411 friends don't expect current-buffer to be changed from under them. */
6412 record_unwind_current_buffer ();
6414 sentinel
= p
->sentinel
;
6416 /* Inhibit quit so that random quits don't screw up a running filter. */
6417 specbind (Qinhibit_quit
, Qt
);
6418 specbind (Qlast_nonmenu_event
, Qt
); /* Why? --Stef */
6420 /* In case we get recursively called,
6421 and we already saved the match data nonrecursively,
6422 save the same match data in safely recursive fashion. */
6423 if (outer_running_asynch_code
)
6426 tem
= Fmatch_data (Qnil
, Qnil
, Qnil
);
6427 restore_search_regs ();
6428 record_unwind_save_match_data ();
6429 Fset_match_data (tem
, Qt
);
6432 /* For speed, if a search happens within this code,
6433 save the match data in a special nonrecursive fashion. */
6434 running_asynch_code
= 1;
6436 internal_condition_case_1 (read_process_output_call
,
6438 Fcons (proc
, Fcons (reason
, Qnil
))),
6439 !NILP (Vdebug_on_error
) ? Qnil
: Qerror
,
6440 exec_sentinel_error_handler
);
6442 /* If we saved the match data nonrecursively, restore it now. */
6443 restore_search_regs ();
6444 running_asynch_code
= outer_running_asynch_code
;
6446 Vdeactivate_mark
= odeactivate
;
6448 /* Restore waiting_for_user_input_p as it was
6449 when we were called, in case the filter clobbered it. */
6450 waiting_for_user_input_p
= waiting
;
6453 if (! EQ (Fcurrent_buffer (), obuffer
)
6454 || ! EQ (current_buffer
->keymap
, okeymap
))
6456 /* But do it only if the caller is actually going to read events.
6457 Otherwise there's no need to make him wake up, and it could
6458 cause trouble (for example it would make sit_for return). */
6459 if (waiting_for_user_input_p
== -1)
6460 record_asynch_buffer_change ();
6462 unbind_to (count
, Qnil
);
6465 /* Report all recent events of a change in process status
6466 (either run the sentinel or output a message).
6467 This is usually done while Emacs is waiting for keyboard input
6468 but can be done at other times. */
6471 status_notify (struct Lisp_Process
*deleting_process
)
6473 register Lisp_Object proc
;
6474 Lisp_Object tail
, msg
;
6475 struct gcpro gcpro1
, gcpro2
;
6479 /* We need to gcpro tail; if read_process_output calls a filter
6480 which deletes a process and removes the cons to which tail points
6481 from Vprocess_alist, and then causes a GC, tail is an unprotected
6485 /* Set this now, so that if new processes are created by sentinels
6486 that we run, we get called again to handle their status changes. */
6487 update_tick
= process_tick
;
6489 for (tail
= Vprocess_alist
; CONSP (tail
); tail
= XCDR (tail
))
6492 register struct Lisp_Process
*p
;
6494 proc
= Fcdr (XCAR (tail
));
6495 p
= XPROCESS (proc
);
6497 if (p
->tick
!= p
->update_tick
)
6499 p
->update_tick
= p
->tick
;
6501 /* If process is still active, read any output that remains. */
6502 while (! EQ (p
->filter
, Qt
)
6503 && ! EQ (p
->status
, Qconnect
)
6504 && ! EQ (p
->status
, Qlisten
)
6505 /* Network or serial process not stopped: */
6506 && ! EQ (p
->command
, Qt
)
6508 && p
!= deleting_process
6509 && read_process_output (proc
, p
->infd
) > 0);
6511 /* Get the text to use for the message. */
6512 if (p
->raw_status_new
)
6514 msg
= status_message (p
);
6516 /* If process is terminated, deactivate it or delete it. */
6518 if (CONSP (p
->status
))
6519 symbol
= XCAR (p
->status
);
6521 if (EQ (symbol
, Qsignal
) || EQ (symbol
, Qexit
)
6522 || EQ (symbol
, Qclosed
))
6524 if (delete_exited_processes
)
6525 remove_process (proc
);
6527 deactivate_process (proc
);
6530 /* The actions above may have further incremented p->tick.
6531 So set p->update_tick again so that an error in the sentinel will
6532 not cause this code to be run again. */
6533 p
->update_tick
= p
->tick
;
6534 /* Now output the message suitably. */
6535 exec_sentinel (proc
, msg
);
6539 update_mode_lines
++; /* In case buffers use %s in mode-line-format. */
6543 DEFUN ("internal-default-process-sentinel", Finternal_default_process_sentinel
,
6544 Sinternal_default_process_sentinel
, 2, 2, 0,
6545 doc
: /* Function used as default sentinel for processes. */)
6546 (Lisp_Object proc
, Lisp_Object msg
)
6548 Lisp_Object buffer
, symbol
;
6549 struct Lisp_Process
*p
;
6550 CHECK_PROCESS (proc
);
6551 p
= XPROCESS (proc
);
6555 symbol
= XCAR (symbol
);
6557 if (!EQ (symbol
, Qrun
) && !NILP (buffer
))
6560 struct buffer
*old
= current_buffer
;
6561 ptrdiff_t opoint
, opoint_byte
;
6562 ptrdiff_t before
, before_byte
;
6564 /* Avoid error if buffer is deleted
6565 (probably that's why the process is dead, too). */
6566 if (!BUFFER_LIVE_P (XBUFFER (buffer
)))
6568 Fset_buffer (buffer
);
6570 if (NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
6571 msg
= (code_convert_string_norecord
6572 (msg
, Vlocale_coding_system
, 1));
6575 opoint_byte
= PT_BYTE
;
6576 /* Insert new output into buffer
6577 at the current end-of-output marker,
6578 thus preserving logical ordering of input and output. */
6579 if (XMARKER (p
->mark
)->buffer
)
6580 Fgoto_char (p
->mark
);
6582 SET_PT_BOTH (ZV
, ZV_BYTE
);
6585 before_byte
= PT_BYTE
;
6587 tem
= BVAR (current_buffer
, read_only
);
6588 bset_read_only (current_buffer
, Qnil
);
6589 insert_string ("\nProcess ");
6590 { /* FIXME: temporary kludge. */
6591 Lisp_Object tem2
= p
->name
; Finsert (1, &tem2
); }
6592 insert_string (" ");
6594 bset_read_only (current_buffer
, tem
);
6595 set_marker_both (p
->mark
, p
->buffer
, PT
, PT_BYTE
);
6597 if (opoint
>= before
)
6598 SET_PT_BOTH (opoint
+ (PT
- before
),
6599 opoint_byte
+ (PT_BYTE
- before_byte
));
6601 SET_PT_BOTH (opoint
, opoint_byte
);
6603 set_buffer_internal (old
);
6609 DEFUN ("set-process-coding-system", Fset_process_coding_system
,
6610 Sset_process_coding_system
, 1, 3, 0,
6611 doc
: /* Set coding systems of PROCESS to DECODING and ENCODING.
6612 DECODING will be used to decode subprocess output and ENCODING to
6613 encode subprocess input. */)
6614 (register Lisp_Object process
, Lisp_Object decoding
, Lisp_Object encoding
)
6616 register struct Lisp_Process
*p
;
6618 CHECK_PROCESS (process
);
6619 p
= XPROCESS (process
);
6621 error ("Input file descriptor of %s closed", SDATA (p
->name
));
6623 error ("Output file descriptor of %s closed", SDATA (p
->name
));
6624 Fcheck_coding_system (decoding
);
6625 Fcheck_coding_system (encoding
);
6626 encoding
= coding_inherit_eol_type (encoding
, Qnil
);
6627 pset_decode_coding_system (p
, decoding
);
6628 pset_encode_coding_system (p
, encoding
);
6629 setup_process_coding_systems (process
);
6634 DEFUN ("process-coding-system",
6635 Fprocess_coding_system
, Sprocess_coding_system
, 1, 1, 0,
6636 doc
: /* Return a cons of coding systems for decoding and encoding of PROCESS. */)
6637 (register Lisp_Object process
)
6639 CHECK_PROCESS (process
);
6640 return Fcons (XPROCESS (process
)->decode_coding_system
,
6641 XPROCESS (process
)->encode_coding_system
);
6644 DEFUN ("set-process-filter-multibyte", Fset_process_filter_multibyte
,
6645 Sset_process_filter_multibyte
, 2, 2, 0,
6646 doc
: /* Set multibyteness of the strings given to PROCESS's filter.
6647 If FLAG is non-nil, the filter is given multibyte strings.
6648 If FLAG is nil, the filter is given unibyte strings. In this case,
6649 all character code conversion except for end-of-line conversion is
6651 (Lisp_Object process
, Lisp_Object flag
)
6653 register struct Lisp_Process
*p
;
6655 CHECK_PROCESS (process
);
6656 p
= XPROCESS (process
);
6658 pset_decode_coding_system
6659 (p
, raw_text_coding_system (p
->decode_coding_system
));
6660 setup_process_coding_systems (process
);
6665 DEFUN ("process-filter-multibyte-p", Fprocess_filter_multibyte_p
,
6666 Sprocess_filter_multibyte_p
, 1, 1, 0,
6667 doc
: /* Return t if a multibyte string is given to PROCESS's filter.*/)
6668 (Lisp_Object process
)
6670 register struct Lisp_Process
*p
;
6671 struct coding_system
*coding
;
6673 CHECK_PROCESS (process
);
6674 p
= XPROCESS (process
);
6675 coding
= proc_decode_coding_system
[p
->infd
];
6676 return (CODING_FOR_UNIBYTE (coding
) ? Qnil
: Qt
);
6685 add_gpm_wait_descriptor (int desc
)
6687 add_keyboard_wait_descriptor (desc
);
6691 delete_gpm_wait_descriptor (int desc
)
6693 delete_keyboard_wait_descriptor (desc
);
6698 # ifdef USABLE_SIGIO
6700 /* Return true if *MASK has a bit set
6701 that corresponds to one of the keyboard input descriptors. */
6704 keyboard_bit_set (fd_set
*mask
)
6708 for (fd
= 0; fd
<= max_desc
; fd
++)
6709 if (FD_ISSET (fd
, mask
)
6710 && ((fd_callback_info
[fd
].flags
& KEYBOARD_FD
) != 0))
6717 #else /* not subprocesses */
6719 /* Defined on msdos.c. */
6720 extern int sys_select (int, SELECT_TYPE
*, SELECT_TYPE
*, SELECT_TYPE
*,
6721 EMACS_TIME
*, void *);
6723 /* Implementation of wait_reading_process_output, assuming that there
6724 are no subprocesses. Used only by the MS-DOS build.
6726 Wait for timeout to elapse and/or keyboard input to be available.
6730 If negative, gobble data immediately available but don't wait for any.
6733 an additional duration to wait, measured in nanoseconds
6734 If TIME_LIMIT is zero, then:
6735 If NSECS == 0, there is no limit.
6736 If NSECS > 0, the timeout consists of NSECS only.
6737 If NSECS < 0, gobble data immediately, as if TIME_LIMIT were negative.
6740 0 to ignore keyboard input, or
6741 1 to return when input is available, or
6742 -1 means caller will actually read the input, so don't throw to
6745 see full version for other parameters. We know that wait_proc will
6746 always be NULL, since `subprocesses' isn't defined.
6748 DO_DISPLAY means redisplay should be done to show subprocess
6749 output that arrives.
6751 Return true if we received input from any process. */
6754 wait_reading_process_output (intmax_t time_limit
, int nsecs
, int read_kbd
,
6756 Lisp_Object wait_for_cell
,
6757 struct Lisp_Process
*wait_proc
, int just_wait_proc
)
6760 EMACS_TIME end_time
, timeout
;
6767 else if (TYPE_MAXIMUM (time_t) < time_limit
)
6768 time_limit
= TYPE_MAXIMUM (time_t);
6770 /* What does time_limit really mean? */
6771 if (time_limit
|| nsecs
> 0)
6773 timeout
= make_emacs_time (time_limit
, nsecs
);
6774 end_time
= add_emacs_time (current_emacs_time (), timeout
);
6777 /* Turn off periodic alarms (in case they are in use)
6778 and then turn off any other atimers,
6779 because the select emulator uses alarms. */
6781 turn_on_atimers (0);
6785 bool timeout_reduced_for_timers
= 0;
6786 SELECT_TYPE waitchannels
;
6789 /* If calling from keyboard input, do not quit
6790 since we want to return C-g as an input character.
6791 Otherwise, do pending quit if requested. */
6795 /* Exit now if the cell we're waiting for became non-nil. */
6796 if (! NILP (wait_for_cell
) && ! NILP (XCAR (wait_for_cell
)))
6799 /* Compute time from now till when time limit is up. */
6800 /* Exit if already run out. */
6803 /* A negative timeout means
6804 gobble output available now
6805 but don't wait at all. */
6807 timeout
= make_emacs_time (0, 0);
6809 else if (time_limit
|| nsecs
> 0)
6811 EMACS_TIME now
= current_emacs_time ();
6812 if (EMACS_TIME_LE (end_time
, now
))
6814 timeout
= sub_emacs_time (end_time
, now
);
6818 timeout
= make_emacs_time (100000, 0);
6821 /* If our caller will not immediately handle keyboard events,
6822 run timer events directly.
6823 (Callers that will immediately read keyboard events
6824 call timer_delay on their own.) */
6825 if (NILP (wait_for_cell
))
6827 EMACS_TIME timer_delay
;
6831 unsigned old_timers_run
= timers_run
;
6832 timer_delay
= timer_check ();
6833 if (timers_run
!= old_timers_run
&& do_display
)
6834 /* We must retry, since a timer may have requeued itself
6835 and that could alter the time delay. */
6836 redisplay_preserve_echo_area (14);
6840 while (!detect_input_pending ());
6842 /* If there is unread keyboard input, also return. */
6844 && requeued_events_pending_p ())
6847 if (EMACS_TIME_VALID_P (timer_delay
) && nsecs
>= 0)
6849 if (EMACS_TIME_LT (timer_delay
, timeout
))
6851 timeout
= timer_delay
;
6852 timeout_reduced_for_timers
= 1;
6857 /* Cause C-g and alarm signals to take immediate action,
6858 and cause input available signals to zero out timeout. */
6860 set_waiting_for_input (&timeout
);
6862 /* If a frame has been newly mapped and needs updating,
6863 reprocess its display stuff. */
6864 if (frame_garbaged
&& do_display
)
6866 clear_waiting_for_input ();
6867 redisplay_preserve_echo_area (15);
6869 set_waiting_for_input (&timeout
);
6872 /* Wait till there is something to do. */
6873 FD_ZERO (&waitchannels
);
6874 if (read_kbd
&& detect_input_pending ())
6878 if (read_kbd
|| !NILP (wait_for_cell
))
6879 FD_SET (0, &waitchannels
);
6880 nfds
= pselect (1, &waitchannels
, NULL
, NULL
, &timeout
, NULL
);
6885 /* Make C-g and alarm signals set flags again */
6886 clear_waiting_for_input ();
6888 /* If we woke up due to SIGWINCH, actually change size now. */
6889 do_pending_window_change (0);
6891 if ((time_limit
|| nsecs
) && nfds
== 0 && ! timeout_reduced_for_timers
)
6892 /* We waited the full specified time, so return now. */
6897 /* If the system call was interrupted, then go around the
6899 if (xerrno
== EINTR
)
6900 FD_ZERO (&waitchannels
);
6902 error ("select error: %s", emacs_strerror (xerrno
));
6905 /* Check for keyboard input */
6908 && detect_input_pending_run_timers (do_display
))
6910 swallow_events (do_display
);
6911 if (detect_input_pending_run_timers (do_display
))
6915 /* If there is unread keyboard input, also return. */
6917 && requeued_events_pending_p ())
6920 /* If wait_for_cell. check for keyboard input
6921 but don't run any timers.
6922 ??? (It seems wrong to me to check for keyboard
6923 input at all when wait_for_cell, but the code
6924 has been this way since July 1994.
6925 Try changing this after version 19.31.) */
6926 if (! NILP (wait_for_cell
)
6927 && detect_input_pending ())
6929 swallow_events (do_display
);
6930 if (detect_input_pending ())
6934 /* Exit now if the cell we're waiting for became non-nil. */
6935 if (! NILP (wait_for_cell
) && ! NILP (XCAR (wait_for_cell
)))
6944 #endif /* not subprocesses */
6946 /* The following functions are needed even if async subprocesses are
6947 not supported. Some of them are no-op stubs in that case. */
6949 /* Add DESC to the set of keyboard input descriptors. */
6952 add_keyboard_wait_descriptor (int desc
)
6954 #ifdef subprocesses /* actually means "not MSDOS" */
6955 eassert (desc
>= 0 && desc
< MAXDESC
);
6956 fd_callback_info
[desc
].flags
|= FOR_READ
| KEYBOARD_FD
;
6957 if (desc
> max_desc
)
6962 /* From now on, do not expect DESC to give keyboard input. */
6965 delete_keyboard_wait_descriptor (int desc
)
6971 eassert (desc
>= 0 && desc
< MAXDESC
);
6972 eassert (desc
<= max_desc
);
6974 fd_callback_info
[desc
].flags
&= ~(FOR_READ
| KEYBOARD_FD
| PROCESS_FD
);
6976 if (desc
== max_desc
)
6977 recompute_max_desc ();
6981 /* Setup coding systems of PROCESS. */
6984 setup_process_coding_systems (Lisp_Object process
)
6987 struct Lisp_Process
*p
= XPROCESS (process
);
6989 int outch
= p
->outfd
;
6990 Lisp_Object coding_system
;
6992 if (inch
< 0 || outch
< 0)
6995 if (!proc_decode_coding_system
[inch
])
6996 proc_decode_coding_system
[inch
] = xmalloc (sizeof (struct coding_system
));
6997 coding_system
= p
->decode_coding_system
;
6998 if (EQ (p
->filter
, Qinternal_default_process_filter
)
6999 && BUFFERP (p
->buffer
))
7001 if (NILP (BVAR (XBUFFER (p
->buffer
), enable_multibyte_characters
)))
7002 coding_system
= raw_text_coding_system (coding_system
);
7004 setup_coding_system (coding_system
, proc_decode_coding_system
[inch
]);
7006 if (!proc_encode_coding_system
[outch
])
7007 proc_encode_coding_system
[outch
] = xmalloc (sizeof (struct coding_system
));
7008 setup_coding_system (p
->encode_coding_system
,
7009 proc_encode_coding_system
[outch
]);
7013 /* Close all descriptors currently in use for communication
7014 with subprocess. This is used in a newly-forked subprocess
7015 to get rid of irrelevant descriptors. */
7018 close_process_descs (void)
7022 for (i
= 0; i
< MAXDESC
; i
++)
7024 Lisp_Object process
;
7025 process
= chan_process
[i
];
7026 if (!NILP (process
))
7028 int in
= XPROCESS (process
)->infd
;
7029 int out
= XPROCESS (process
)->outfd
;
7032 if (out
>= 0 && in
!= out
)
7039 DEFUN ("get-buffer-process", Fget_buffer_process
, Sget_buffer_process
, 1, 1, 0,
7040 doc
: /* Return the (or a) process associated with BUFFER.
7041 BUFFER may be a buffer or the name of one. */)
7042 (register Lisp_Object buffer
)
7045 register Lisp_Object buf
, tail
, proc
;
7047 if (NILP (buffer
)) return Qnil
;
7048 buf
= Fget_buffer (buffer
);
7049 if (NILP (buf
)) return Qnil
;
7051 for (tail
= Vprocess_alist
; CONSP (tail
); tail
= XCDR (tail
))
7053 proc
= Fcdr (XCAR (tail
));
7054 if (PROCESSP (proc
) && EQ (XPROCESS (proc
)->buffer
, buf
))
7057 #endif /* subprocesses */
7061 DEFUN ("process-inherit-coding-system-flag",
7062 Fprocess_inherit_coding_system_flag
, Sprocess_inherit_coding_system_flag
,
7064 doc
: /* Return the value of inherit-coding-system flag for PROCESS.
7065 If this flag is t, `buffer-file-coding-system' of the buffer
7066 associated with PROCESS will inherit the coding system used to decode
7067 the process output. */)
7068 (register Lisp_Object process
)
7071 CHECK_PROCESS (process
);
7072 return XPROCESS (process
)->inherit_coding_system_flag
? Qt
: Qnil
;
7074 /* Ignore the argument and return the value of
7075 inherit-process-coding-system. */
7076 return inherit_process_coding_system
? Qt
: Qnil
;
7080 /* Kill all processes associated with `buffer'.
7081 If `buffer' is nil, kill all processes */
7084 kill_buffer_processes (Lisp_Object buffer
)
7087 Lisp_Object tail
, proc
;
7089 for (tail
= Vprocess_alist
; CONSP (tail
); tail
= XCDR (tail
))
7091 proc
= XCDR (XCAR (tail
));
7093 && (NILP (buffer
) || EQ (XPROCESS (proc
)->buffer
, buffer
)))
7095 if (NETCONN_P (proc
) || SERIALCONN_P (proc
))
7096 Fdelete_process (proc
);
7097 else if (XPROCESS (proc
)->infd
>= 0)
7098 process_send_signal (proc
, SIGHUP
, Qnil
, 1);
7101 #else /* subprocesses */
7102 /* Since we have no subprocesses, this does nothing. */
7103 #endif /* subprocesses */
7106 DEFUN ("waiting-for-user-input-p", Fwaiting_for_user_input_p
,
7107 Swaiting_for_user_input_p
, 0, 0, 0,
7108 doc
: /* Return non-nil if Emacs is waiting for input from the user.
7109 This is intended for use by asynchronous process output filters and sentinels. */)
7113 return (waiting_for_user_input_p
? Qt
: Qnil
);
7119 /* Stop reading input from keyboard sources. */
7122 hold_keyboard_input (void)
7127 /* Resume reading input from keyboard sources. */
7130 unhold_keyboard_input (void)
7135 /* Return true if keyboard input is on hold, zero otherwise. */
7138 kbd_on_hold_p (void)
7140 return kbd_is_on_hold
;
7144 /* Enumeration of and access to system processes a-la ps(1). */
7146 DEFUN ("list-system-processes", Flist_system_processes
, Slist_system_processes
,
7148 doc
: /* Return a list of numerical process IDs of all running processes.
7149 If this functionality is unsupported, return nil.
7151 See `process-attributes' for getting attributes of a process given its ID. */)
7154 return list_system_processes ();
7157 DEFUN ("process-attributes", Fprocess_attributes
,
7158 Sprocess_attributes
, 1, 1, 0,
7159 doc
: /* Return attributes of the process given by its PID, a number.
7161 Value is an alist where each element is a cons cell of the form
7165 If this functionality is unsupported, the value is nil.
7167 See `list-system-processes' for getting a list of all process IDs.
7169 The KEYs of the attributes that this function may return are listed
7170 below, together with the type of the associated VALUE (in parentheses).
7171 Not all platforms support all of these attributes; unsupported
7172 attributes will not appear in the returned alist.
7173 Unless explicitly indicated otherwise, numbers can have either
7174 integer or floating point values.
7176 euid -- Effective user User ID of the process (number)
7177 user -- User name corresponding to euid (string)
7178 egid -- Effective user Group ID of the process (number)
7179 group -- Group name corresponding to egid (string)
7180 comm -- Command name (executable name only) (string)
7181 state -- Process state code, such as "S", "R", or "T" (string)
7182 ppid -- Parent process ID (number)
7183 pgrp -- Process group ID (number)
7184 sess -- Session ID, i.e. process ID of session leader (number)
7185 ttname -- Controlling tty name (string)
7186 tpgid -- ID of foreground process group on the process's tty (number)
7187 minflt -- number of minor page faults (number)
7188 majflt -- number of major page faults (number)
7189 cminflt -- cumulative number of minor page faults (number)
7190 cmajflt -- cumulative number of major page faults (number)
7191 utime -- user time used by the process, in (current-time) format,
7192 which is a list of integers (HIGH LOW USEC PSEC)
7193 stime -- system time used by the process (current-time)
7194 time -- sum of utime and stime (current-time)
7195 cutime -- user time used by the process and its children (current-time)
7196 cstime -- system time used by the process and its children (current-time)
7197 ctime -- sum of cutime and cstime (current-time)
7198 pri -- priority of the process (number)
7199 nice -- nice value of the process (number)
7200 thcount -- process thread count (number)
7201 start -- time the process started (current-time)
7202 vsize -- virtual memory size of the process in KB's (number)
7203 rss -- resident set size of the process in KB's (number)
7204 etime -- elapsed time the process is running, in (HIGH LOW USEC PSEC) format
7205 pcpu -- percents of CPU time used by the process (floating-point number)
7206 pmem -- percents of total physical memory used by process's resident set
7207 (floating-point number)
7208 args -- command line which invoked the process (string). */)
7211 return system_process_attributes (pid
);
7214 /* Arrange to catch SIGCHLD if this hasn't already been arranged.
7215 Invoke this after init_process_emacs, and after glib and/or GNUstep
7216 futz with the SIGCHLD handler, but before Emacs forks any children.
7217 This function's caller should block SIGCHLD. */
7219 #ifndef NS_IMPL_GNUSTEP
7223 catch_child_signal (void)
7225 struct sigaction action
, old_action
;
7226 emacs_sigaction_init (&action
, deliver_child_signal
);
7227 block_child_signal ();
7228 sigaction (SIGCHLD
, &action
, &old_action
);
7229 eassert (! (old_action
.sa_flags
& SA_SIGINFO
));
7231 if (old_action
.sa_handler
!= deliver_child_signal
)
7233 = (old_action
.sa_handler
== SIG_DFL
|| old_action
.sa_handler
== SIG_IGN
7235 : old_action
.sa_handler
);
7236 unblock_child_signal ();
7240 /* This is not called "init_process" because that is the name of a
7241 Mach system call, so it would cause problems on Darwin systems. */
7243 init_process_emacs (void)
7248 inhibit_sentinels
= 0;
7251 if (! noninteractive
|| initialized
)
7254 #if defined HAVE_GLIB && !defined WINDOWSNT
7255 /* Tickle glib's child-handling code. Ask glib to wait for Emacs itself;
7256 this should always fail, but is enough to initialize glib's
7257 private SIGCHLD handler, allowing catch_child_signal to copy
7258 it into lib_child_handler. */
7259 g_source_unref (g_child_watch_source_new (getpid ()));
7261 catch_child_signal ();
7265 memset (fd_callback_info
, 0, sizeof (fd_callback_info
));
7267 #ifdef NON_BLOCKING_CONNECT
7268 num_pending_connects
= 0;
7271 #ifdef ADAPTIVE_READ_BUFFERING
7272 process_output_delay_count
= 0;
7273 process_output_skip
= 0;
7276 /* Don't do this, it caused infinite select loops. The display
7277 method should call add_keyboard_wait_descriptor on stdin if it
7280 FD_SET (0, &input_wait_mask
);
7283 Vprocess_alist
= Qnil
;
7284 deleted_pid_list
= Qnil
;
7285 for (i
= 0; i
< MAXDESC
; i
++)
7287 chan_process
[i
] = Qnil
;
7288 proc_buffered_char
[i
] = -1;
7290 memset (proc_decode_coding_system
, 0, sizeof proc_decode_coding_system
);
7291 memset (proc_encode_coding_system
, 0, sizeof proc_encode_coding_system
);
7292 #ifdef DATAGRAM_SOCKETS
7293 memset (datagram_address
, 0, sizeof datagram_address
);
7297 Lisp_Object subfeatures
= Qnil
;
7298 const struct socket_options
*sopt
;
7300 #define ADD_SUBFEATURE(key, val) \
7301 subfeatures = pure_cons (pure_cons (key, pure_cons (val, Qnil)), subfeatures)
7303 #ifdef NON_BLOCKING_CONNECT
7304 ADD_SUBFEATURE (QCnowait
, Qt
);
7306 #ifdef DATAGRAM_SOCKETS
7307 ADD_SUBFEATURE (QCtype
, Qdatagram
);
7309 #ifdef HAVE_SEQPACKET
7310 ADD_SUBFEATURE (QCtype
, Qseqpacket
);
7312 #ifdef HAVE_LOCAL_SOCKETS
7313 ADD_SUBFEATURE (QCfamily
, Qlocal
);
7315 ADD_SUBFEATURE (QCfamily
, Qipv4
);
7317 ADD_SUBFEATURE (QCfamily
, Qipv6
);
7319 #ifdef HAVE_GETSOCKNAME
7320 ADD_SUBFEATURE (QCservice
, Qt
);
7322 ADD_SUBFEATURE (QCserver
, Qt
);
7324 for (sopt
= socket_options
; sopt
->name
; sopt
++)
7325 subfeatures
= pure_cons (intern_c_string (sopt
->name
), subfeatures
);
7327 Fprovide (intern_c_string ("make-network-process"), subfeatures
);
7330 #if defined (DARWIN_OS)
7331 /* PTYs are broken on Darwin < 6, but are sometimes useful for interactive
7332 processes. As such, we only change the default value. */
7335 char const *release
= (STRINGP (Voperating_system_release
)
7336 ? SSDATA (Voperating_system_release
)
7338 if (!release
|| !release
[0] || (release
[0] < '7' && release
[1] == '.')) {
7339 Vprocess_connection_type
= Qnil
;
7343 #endif /* subprocesses */
7348 syms_of_process (void)
7352 DEFSYM (Qprocessp
, "processp");
7353 DEFSYM (Qrun
, "run");
7354 DEFSYM (Qstop
, "stop");
7355 DEFSYM (Qsignal
, "signal");
7357 /* Qexit is already staticpro'd by syms_of_eval; don't staticpro it
7360 Qexit = intern_c_string ("exit");
7361 staticpro (&Qexit); */
7363 DEFSYM (Qopen
, "open");
7364 DEFSYM (Qclosed
, "closed");
7365 DEFSYM (Qconnect
, "connect");
7366 DEFSYM (Qfailed
, "failed");
7367 DEFSYM (Qlisten
, "listen");
7368 DEFSYM (Qlocal
, "local");
7369 DEFSYM (Qipv4
, "ipv4");
7371 DEFSYM (Qipv6
, "ipv6");
7373 DEFSYM (Qdatagram
, "datagram");
7374 DEFSYM (Qseqpacket
, "seqpacket");
7376 DEFSYM (QCport
, ":port");
7377 DEFSYM (QCspeed
, ":speed");
7378 DEFSYM (QCprocess
, ":process");
7380 DEFSYM (QCbytesize
, ":bytesize");
7381 DEFSYM (QCstopbits
, ":stopbits");
7382 DEFSYM (QCparity
, ":parity");
7383 DEFSYM (Qodd
, "odd");
7384 DEFSYM (Qeven
, "even");
7385 DEFSYM (QCflowcontrol
, ":flowcontrol");
7388 DEFSYM (QCsummary
, ":summary");
7390 DEFSYM (Qreal
, "real");
7391 DEFSYM (Qnetwork
, "network");
7392 DEFSYM (Qserial
, "serial");
7393 DEFSYM (QCbuffer
, ":buffer");
7394 DEFSYM (QChost
, ":host");
7395 DEFSYM (QCservice
, ":service");
7396 DEFSYM (QClocal
, ":local");
7397 DEFSYM (QCremote
, ":remote");
7398 DEFSYM (QCcoding
, ":coding");
7399 DEFSYM (QCserver
, ":server");
7400 DEFSYM (QCnowait
, ":nowait");
7401 DEFSYM (QCsentinel
, ":sentinel");
7402 DEFSYM (QClog
, ":log");
7403 DEFSYM (QCnoquery
, ":noquery");
7404 DEFSYM (QCstop
, ":stop");
7405 DEFSYM (QCoptions
, ":options");
7406 DEFSYM (QCplist
, ":plist");
7408 DEFSYM (Qlast_nonmenu_event
, "last-nonmenu-event");
7410 staticpro (&Vprocess_alist
);
7411 staticpro (&deleted_pid_list
);
7413 #endif /* subprocesses */
7415 DEFSYM (QCname
, ":name");
7416 DEFSYM (QCtype
, ":type");
7418 DEFSYM (Qeuid
, "euid");
7419 DEFSYM (Qegid
, "egid");
7420 DEFSYM (Quser
, "user");
7421 DEFSYM (Qgroup
, "group");
7422 DEFSYM (Qcomm
, "comm");
7423 DEFSYM (Qstate
, "state");
7424 DEFSYM (Qppid
, "ppid");
7425 DEFSYM (Qpgrp
, "pgrp");
7426 DEFSYM (Qsess
, "sess");
7427 DEFSYM (Qttname
, "ttname");
7428 DEFSYM (Qtpgid
, "tpgid");
7429 DEFSYM (Qminflt
, "minflt");
7430 DEFSYM (Qmajflt
, "majflt");
7431 DEFSYM (Qcminflt
, "cminflt");
7432 DEFSYM (Qcmajflt
, "cmajflt");
7433 DEFSYM (Qutime
, "utime");
7434 DEFSYM (Qstime
, "stime");
7435 DEFSYM (Qtime
, "time");
7436 DEFSYM (Qcutime
, "cutime");
7437 DEFSYM (Qcstime
, "cstime");
7438 DEFSYM (Qctime
, "ctime");
7439 DEFSYM (Qinternal_default_process_sentinel
,
7440 "internal-default-process-sentinel");
7441 DEFSYM (Qinternal_default_process_filter
,
7442 "internal-default-process-filter");
7443 DEFSYM (Qpri
, "pri");
7444 DEFSYM (Qnice
, "nice");
7445 DEFSYM (Qthcount
, "thcount");
7446 DEFSYM (Qstart
, "start");
7447 DEFSYM (Qvsize
, "vsize");
7448 DEFSYM (Qrss
, "rss");
7449 DEFSYM (Qetime
, "etime");
7450 DEFSYM (Qpcpu
, "pcpu");
7451 DEFSYM (Qpmem
, "pmem");
7452 DEFSYM (Qargs
, "args");
7454 DEFVAR_BOOL ("delete-exited-processes", delete_exited_processes
,
7455 doc
: /* Non-nil means delete processes immediately when they exit.
7456 A value of nil means don't delete them until `list-processes' is run. */);
7458 delete_exited_processes
= 1;
7461 DEFVAR_LISP ("process-connection-type", Vprocess_connection_type
,
7462 doc
: /* Control type of device used to communicate with subprocesses.
7463 Values are nil to use a pipe, or t or `pty' to use a pty.
7464 The value has no effect if the system has no ptys or if all ptys are busy:
7465 then a pipe is used in any case.
7466 The value takes effect when `start-process' is called. */);
7467 Vprocess_connection_type
= Qt
;
7469 #ifdef ADAPTIVE_READ_BUFFERING
7470 DEFVAR_LISP ("process-adaptive-read-buffering", Vprocess_adaptive_read_buffering
,
7471 doc
: /* If non-nil, improve receive buffering by delaying after short reads.
7472 On some systems, when Emacs reads the output from a subprocess, the output data
7473 is read in very small blocks, potentially resulting in very poor performance.
7474 This behavior can be remedied to some extent by setting this variable to a
7475 non-nil value, as it will automatically delay reading from such processes, to
7476 allow them to produce more output before Emacs tries to read it.
7477 If the value is t, the delay is reset after each write to the process; any other
7478 non-nil value means that the delay is not reset on write.
7479 The variable takes effect when `start-process' is called. */);
7480 Vprocess_adaptive_read_buffering
= Qt
;
7483 defsubr (&Sprocessp
);
7484 defsubr (&Sget_process
);
7485 defsubr (&Sdelete_process
);
7486 defsubr (&Sprocess_status
);
7487 defsubr (&Sprocess_exit_status
);
7488 defsubr (&Sprocess_id
);
7489 defsubr (&Sprocess_name
);
7490 defsubr (&Sprocess_tty_name
);
7491 defsubr (&Sprocess_command
);
7492 defsubr (&Sset_process_buffer
);
7493 defsubr (&Sprocess_buffer
);
7494 defsubr (&Sprocess_mark
);
7495 defsubr (&Sset_process_filter
);
7496 defsubr (&Sprocess_filter
);
7497 defsubr (&Sset_process_sentinel
);
7498 defsubr (&Sprocess_sentinel
);
7499 defsubr (&Sset_process_thread
);
7500 defsubr (&Sprocess_thread
);
7501 defsubr (&Sset_process_window_size
);
7502 defsubr (&Sset_process_inherit_coding_system_flag
);
7503 defsubr (&Sset_process_query_on_exit_flag
);
7504 defsubr (&Sprocess_query_on_exit_flag
);
7505 defsubr (&Sprocess_contact
);
7506 defsubr (&Sprocess_plist
);
7507 defsubr (&Sset_process_plist
);
7508 defsubr (&Sprocess_list
);
7509 defsubr (&Sstart_process
);
7510 defsubr (&Sserial_process_configure
);
7511 defsubr (&Smake_serial_process
);
7512 defsubr (&Sset_network_process_option
);
7513 defsubr (&Smake_network_process
);
7514 defsubr (&Sformat_network_address
);
7515 #if defined (HAVE_NET_IF_H)
7517 defsubr (&Snetwork_interface_list
);
7519 #if defined (SIOCGIFADDR) || defined (SIOCGIFHWADDR) || defined (SIOCGIFFLAGS)
7520 defsubr (&Snetwork_interface_info
);
7522 #endif /* defined (HAVE_NET_IF_H) */
7523 #ifdef DATAGRAM_SOCKETS
7524 defsubr (&Sprocess_datagram_address
);
7525 defsubr (&Sset_process_datagram_address
);
7527 defsubr (&Saccept_process_output
);
7528 defsubr (&Sprocess_send_region
);
7529 defsubr (&Sprocess_send_string
);
7530 defsubr (&Sinterrupt_process
);
7531 defsubr (&Skill_process
);
7532 defsubr (&Squit_process
);
7533 defsubr (&Sstop_process
);
7534 defsubr (&Scontinue_process
);
7535 defsubr (&Sprocess_running_child_p
);
7536 defsubr (&Sprocess_send_eof
);
7537 defsubr (&Ssignal_process
);
7538 defsubr (&Swaiting_for_user_input_p
);
7539 defsubr (&Sprocess_type
);
7540 defsubr (&Sinternal_default_process_sentinel
);
7541 defsubr (&Sinternal_default_process_filter
);
7542 defsubr (&Sset_process_coding_system
);
7543 defsubr (&Sprocess_coding_system
);
7544 defsubr (&Sset_process_filter_multibyte
);
7545 defsubr (&Sprocess_filter_multibyte_p
);
7547 #endif /* subprocesses */
7549 defsubr (&Sget_buffer_process
);
7550 defsubr (&Sprocess_inherit_coding_system_flag
);
7551 defsubr (&Slist_system_processes
);
7552 defsubr (&Sprocess_attributes
);