1 /* Asynchronous subprocess control for GNU Emacs.
2 Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994, 1995,
3 1996, 1998, 1999, 2001, 2002, 2003, 2004,
4 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
25 /* This file is split into two parts by the following preprocessor
26 conditional. The 'then' clause contains all of the support for
27 asynchronous subprocesses. The 'else' clause contains stub
28 versions of some of the asynchronous subprocess routines that are
29 often called elsewhere in Emacs, so we don't have to #ifdef the
30 sections that call them. */
40 #include <sys/types.h> /* some typedefs are used in sys/file.h */
43 #ifdef HAVE_INTTYPES_H
57 #ifdef HAVE_SOCKETS /* TCP connection support, if kernel can do it */
58 #include <sys/socket.h>
60 #include <netinet/in.h>
61 #include <arpa/inet.h>
63 /* Are local (unix) sockets supported? */
64 #if defined (HAVE_SYS_UN_H)
65 #if !defined (AF_LOCAL) && defined (AF_UNIX)
66 #define AF_LOCAL AF_UNIX
69 #define HAVE_LOCAL_SOCKETS
73 #endif /* HAVE_SOCKETS */
75 #if defined(BSD_SYSTEM)
76 #include <sys/ioctl.h>
77 #if !defined (O_NDELAY) && defined (HAVE_PTYS) && !defined(USG5)
79 #endif /* HAVE_PTYS and no O_NDELAY */
80 #endif /* BSD_SYSTEM */
86 /* Can we use SIOCGIFCONF and/or SIOCGIFADDR */
88 #if defined(HAVE_SYS_IOCTL_H) && defined(HAVE_NET_IF_H)
89 /* sys/ioctl.h may have been included already */
91 #include <sys/ioctl.h>
102 #include <netinet/in.h>
103 #include <arpa/nameser.h>
113 #include "character.h"
117 #include "termhooks.h"
118 #include "termopts.h"
119 #include "commands.h"
120 #include "keyboard.h"
121 #include "blockinput.h"
122 #include "dispextern.h"
123 #include "composite.h"
126 Lisp_Object Qprocessp
;
127 Lisp_Object Qrun
, Qstop
, Qsignal
;
128 Lisp_Object Qopen
, Qclosed
, Qconnect
, Qfailed
, Qlisten
;
129 Lisp_Object Qlocal
, Qipv4
, Qdatagram
;
130 Lisp_Object Qreal
, Qnetwork
, Qserial
;
134 Lisp_Object QCport
, QCspeed
, QCprocess
;
135 Lisp_Object QCbytesize
, QCstopbits
, QCparity
, Qodd
, Qeven
;
136 Lisp_Object QCflowcontrol
, Qhw
, Qsw
, QCsummary
;
137 Lisp_Object QCname
, QCbuffer
, QChost
, QCservice
, QCtype
;
138 Lisp_Object QClocal
, QCremote
, QCcoding
;
139 Lisp_Object QCserver
, QCnowait
, QCnoquery
, QCstop
;
140 Lisp_Object QCsentinel
, QClog
, QCoptions
, QCplist
;
141 Lisp_Object Qlast_nonmenu_event
;
142 /* QCfamily is declared and initialized in xfaces.c,
143 QCfilter in keyboard.c. */
144 extern Lisp_Object QCfamily
, QCfilter
;
146 /* Qexit is declared and initialized in eval.c. */
148 /* QCfamily is defined in xfaces.c. */
149 extern Lisp_Object QCfamily
;
150 /* QCfilter is defined in keyboard.c. */
151 extern Lisp_Object QCfilter
;
153 Lisp_Object Qeuid
, Qegid
, Qcomm
, Qstate
, Qppid
, Qpgrp
, Qsess
, Qttname
, Qtpgid
;
154 Lisp_Object Qminflt
, Qmajflt
, Qcminflt
, Qcmajflt
, Qutime
, Qstime
, Qcstime
;
155 Lisp_Object Qcutime
, Qpri
, Qnice
, Qthcount
, Qstart
, Qvsize
, Qrss
, Qargs
;
156 Lisp_Object Quser
, Qgroup
, Qetime
, Qpcpu
, Qpmem
;
159 #define NETCONN_P(p) (EQ (XPROCESS (p)->type, Qnetwork))
160 #define NETCONN1_P(p) (EQ ((p)->type, Qnetwork))
161 #define SERIALCONN_P(p) (EQ (XPROCESS (p)->type, Qserial))
162 #define SERIALCONN1_P(p) (EQ ((p)->type, Qserial))
164 #define NETCONN_P(p) 0
165 #define NETCONN1_P(p) 0
166 #define SERIALCONN_P(p) 0
167 #define SERIALCONN1_P(p) 0
168 #endif /* HAVE_SOCKETS */
170 /* Define first descriptor number available for subprocesses. */
171 #define FIRST_PROC_DESC 3
173 /* Define SIGCHLD as an alias for SIGCLD. There are many conditionals
176 #if !defined (SIGCHLD) && defined (SIGCLD)
177 #define SIGCHLD SIGCLD
180 #include "syssignal.h"
184 extern char *get_operating_system_release ();
186 /* Serial processes require termios or Windows. */
187 #if defined (HAVE_TERMIOS) || defined (WINDOWSNT)
192 /* From sysdep.c or w32.c */
193 extern int serial_open (char *port
);
194 extern void serial_configure (struct Lisp_Process
*p
, Lisp_Object contact
);
205 /* t means use pty, nil means use a pipe,
206 maybe other values to come. */
207 static Lisp_Object Vprocess_connection_type
;
209 /* These next two vars are non-static since sysdep.c uses them in the
210 emulation of `select'. */
211 /* Number of events of change of status of a process. */
213 /* Number of events for which the user or sentinel has been notified. */
216 /* Define NON_BLOCKING_CONNECT if we can support non-blocking connects. */
218 #ifdef BROKEN_NON_BLOCKING_CONNECT
219 #undef NON_BLOCKING_CONNECT
221 #ifndef NON_BLOCKING_CONNECT
224 #if defined (HAVE_GETPEERNAME) || defined (GNU_LINUX)
225 #if defined (O_NONBLOCK) || defined (O_NDELAY)
226 #if defined (EWOULDBLOCK) || defined (EINPROGRESS)
227 #define NON_BLOCKING_CONNECT
228 #endif /* EWOULDBLOCK || EINPROGRESS */
229 #endif /* O_NONBLOCK || O_NDELAY */
230 #endif /* HAVE_GETPEERNAME || GNU_LINUX */
231 #endif /* HAVE_SELECT */
232 #endif /* HAVE_SOCKETS */
233 #endif /* NON_BLOCKING_CONNECT */
234 #endif /* BROKEN_NON_BLOCKING_CONNECT */
236 /* Define DATAGRAM_SOCKETS if datagrams can be used safely on
237 this system. We need to read full packets, so we need a
238 "non-destructive" select. So we require either native select,
239 or emulation of select using FIONREAD. */
241 #ifdef BROKEN_DATAGRAM_SOCKETS
242 #undef DATAGRAM_SOCKETS
244 #ifndef DATAGRAM_SOCKETS
246 #if defined (HAVE_SELECT) || defined (FIONREAD)
247 #if defined (HAVE_SENDTO) && defined (HAVE_RECVFROM) && defined (EMSGSIZE)
248 #define DATAGRAM_SOCKETS
249 #endif /* HAVE_SENDTO && HAVE_RECVFROM && EMSGSIZE */
250 #endif /* HAVE_SELECT || FIONREAD */
251 #endif /* HAVE_SOCKETS */
252 #endif /* DATAGRAM_SOCKETS */
253 #endif /* BROKEN_DATAGRAM_SOCKETS */
255 #if !defined (ADAPTIVE_READ_BUFFERING) && !defined (NO_ADAPTIVE_READ_BUFFERING)
256 #ifdef EMACS_HAS_USECS
257 #define ADAPTIVE_READ_BUFFERING
261 #ifdef ADAPTIVE_READ_BUFFERING
262 #define READ_OUTPUT_DELAY_INCREMENT 10000
263 #define READ_OUTPUT_DELAY_MAX (READ_OUTPUT_DELAY_INCREMENT * 5)
264 #define READ_OUTPUT_DELAY_MAX_MAX (READ_OUTPUT_DELAY_INCREMENT * 7)
266 /* Number of processes which have a non-zero read_output_delay,
267 and therefore might be delayed for adaptive read buffering. */
269 static int process_output_delay_count
;
271 /* Non-zero if any process has non-nil read_output_skip. */
273 static int process_output_skip
;
275 /* Non-nil means to delay reading process output to improve buffering.
276 A value of t means that delay is reset after each send, any other
277 non-nil value does not reset the delay. A value of nil disables
278 adaptive read buffering completely. */
279 static Lisp_Object Vprocess_adaptive_read_buffering
;
281 #define process_output_delay_count 0
285 #include "sysselect.h"
287 static int keyboard_bit_set
P_ ((SELECT_TYPE
*));
288 static void deactivate_process
P_ ((Lisp_Object
));
289 static void status_notify
P_ ((struct Lisp_Process
*));
290 static int read_process_output
P_ ((Lisp_Object
, int));
292 /* If we support a window system, turn on the code to poll periodically
293 to detect C-g. It isn't actually used when doing interrupt input. */
294 #ifdef HAVE_WINDOW_SYSTEM
295 #define POLL_FOR_INPUT
298 static Lisp_Object
get_process ();
299 static void exec_sentinel ();
301 extern EMACS_TIME
timer_check ();
302 extern int timers_run
;
304 /* Mask of bits indicating the descriptors that we wait for input on. */
306 static SELECT_TYPE input_wait_mask
;
308 /* Mask that excludes keyboard input descriptor(s). */
310 static SELECT_TYPE non_keyboard_wait_mask
;
312 /* Mask that excludes process input descriptor(s). */
314 static SELECT_TYPE non_process_wait_mask
;
316 /* Mask for the gpm mouse input descriptor. */
318 static SELECT_TYPE gpm_wait_mask
;
320 #ifdef NON_BLOCKING_CONNECT
321 /* Mask of bits indicating the descriptors that we wait for connect to
322 complete on. Once they complete, they are removed from this mask
323 and added to the input_wait_mask and non_keyboard_wait_mask. */
325 static SELECT_TYPE connect_wait_mask
;
327 /* Number of bits set in connect_wait_mask. */
328 static int num_pending_connects
;
330 #define IF_NON_BLOCKING_CONNECT(s) s
332 #define IF_NON_BLOCKING_CONNECT(s)
335 /* The largest descriptor currently in use for a process object. */
336 static int max_process_desc
;
338 /* The largest descriptor currently in use for keyboard input. */
339 static int max_keyboard_desc
;
341 /* The largest descriptor currently in use for gpm mouse input. */
342 static int max_gpm_desc
;
344 /* Nonzero means delete a process right away if it exits. */
345 static int delete_exited_processes
;
347 /* Indexed by descriptor, gives the process (if any) for that descriptor */
348 Lisp_Object chan_process
[MAXDESC
];
350 /* Alist of elements (NAME . PROCESS) */
351 Lisp_Object Vprocess_alist
;
353 /* Buffered-ahead input char from process, indexed by channel.
354 -1 means empty (no char is buffered).
355 Used on sys V where the only way to tell if there is any
356 output from the process is to read at least one char.
357 Always -1 on systems that support FIONREAD. */
359 /* Don't make static; need to access externally. */
360 int proc_buffered_char
[MAXDESC
];
362 /* Table of `struct coding-system' for each process. */
363 static struct coding_system
*proc_decode_coding_system
[MAXDESC
];
364 static struct coding_system
*proc_encode_coding_system
[MAXDESC
];
366 #ifdef DATAGRAM_SOCKETS
367 /* Table of `partner address' for datagram sockets. */
368 struct sockaddr_and_len
{
371 } datagram_address
[MAXDESC
];
372 #define DATAGRAM_CHAN_P(chan) (datagram_address[chan].sa != 0)
373 #define DATAGRAM_CONN_P(proc) (PROCESSP (proc) && datagram_address[XPROCESS (proc)->infd].sa != 0)
375 #define DATAGRAM_CHAN_P(chan) (0)
376 #define DATAGRAM_CONN_P(proc) (0)
379 /* Maximum number of bytes to send to a pty without an eof. */
380 static int pty_max_bytes
;
382 /* Nonzero means don't run process sentinels. This is used
384 int inhibit_sentinels
;
390 /* The file name of the pty opened by allocate_pty. */
392 static char pty_name
[24];
395 /* Compute the Lisp form of the process status, p->status, from
396 the numeric status that was returned by `wait'. */
398 static Lisp_Object
status_convert (int);
402 struct Lisp_Process
*p
;
404 eassert (p
->raw_status_new
);
405 p
->status
= status_convert (p
->raw_status
);
406 p
->raw_status_new
= 0;
409 /* Convert a process status word in Unix format to
410 the list that we use internally. */
413 status_convert (int w
)
416 return Fcons (Qstop
, Fcons (make_number (WSTOPSIG (w
)), Qnil
));
417 else if (WIFEXITED (w
))
418 return Fcons (Qexit
, Fcons (make_number (WRETCODE (w
)),
419 WCOREDUMP (w
) ? Qt
: Qnil
));
420 else if (WIFSIGNALED (w
))
421 return Fcons (Qsignal
, Fcons (make_number (WTERMSIG (w
)),
422 WCOREDUMP (w
) ? Qt
: Qnil
));
427 /* Given a status-list, extract the three pieces of information
428 and store them individually through the three pointers. */
431 decode_status (l
, symbol
, code
, coredump
)
449 *code
= XFASTINT (XCAR (tem
));
451 *coredump
= !NILP (tem
);
455 /* Return a string describing a process status list. */
459 struct Lisp_Process
*p
;
461 Lisp_Object status
= p
->status
;
464 Lisp_Object string
, string2
;
466 decode_status (status
, &symbol
, &code
, &coredump
);
468 if (EQ (symbol
, Qsignal
) || EQ (symbol
, Qstop
))
471 synchronize_system_messages_locale ();
472 signame
= strsignal (code
);
475 string
= build_string (signame
);
476 string2
= build_string (coredump
? " (core dumped)\n" : "\n");
477 SSET (string
, 0, DOWNCASE (SREF (string
, 0)));
478 return concat2 (string
, string2
);
480 else if (EQ (symbol
, Qexit
))
483 return build_string (code
== 0 ? "deleted\n" : "connection broken by remote peer\n");
485 return build_string ("finished\n");
486 string
= Fnumber_to_string (make_number (code
));
487 string2
= build_string (coredump
? " (core dumped)\n" : "\n");
488 return concat3 (build_string ("exited abnormally with code "),
491 else if (EQ (symbol
, Qfailed
))
493 string
= Fnumber_to_string (make_number (code
));
494 string2
= build_string ("\n");
495 return concat3 (build_string ("failed with code "),
499 return Fcopy_sequence (Fsymbol_name (symbol
));
504 /* Open an available pty, returning a file descriptor.
505 Return -1 on failure.
506 The file name of the terminal corresponding to the pty
507 is left in the variable pty_name. */
518 for (c
= FIRST_PTY_LETTER
; c
<= 'z'; c
++)
519 for (i
= 0; i
< 16; i
++)
522 struct stat stb
; /* Used in some PTY_OPEN. */
523 #ifdef PTY_NAME_SPRINTF
526 sprintf (pty_name
, "/dev/pty%c%x", c
, i
);
527 #endif /* no PTY_NAME_SPRINTF */
531 #else /* no PTY_OPEN */
533 { /* Some systems name their pseudoterminals so that there are gaps in
534 the usual sequence - for example, on HP9000/S700 systems, there
535 are no pseudoterminals with names ending in 'f'. So we wait for
536 three failures in a row before deciding that we've reached the
538 int failed_count
= 0;
540 if (stat (pty_name
, &stb
) < 0)
543 if (failed_count
>= 3)
550 fd
= emacs_open (pty_name
, O_RDWR
| O_NONBLOCK
, 0);
552 fd
= emacs_open (pty_name
, O_RDWR
| O_NDELAY
, 0);
555 #endif /* no PTY_OPEN */
559 /* check to make certain that both sides are available
560 this avoids a nasty yet stupid bug in rlogins */
561 #ifdef PTY_TTY_NAME_SPRINTF
564 sprintf (pty_name
, "/dev/tty%c%x", c
, i
);
565 #endif /* no PTY_TTY_NAME_SPRINTF */
566 if (access (pty_name
, 6) != 0)
581 #endif /* HAVE_PTYS */
587 register Lisp_Object val
, tem
, name1
;
588 register struct Lisp_Process
*p
;
592 p
= allocate_process ();
600 p
->raw_status_new
= 0;
602 p
->mark
= Fmake_marker ();
604 #ifdef ADAPTIVE_READ_BUFFERING
605 p
->adaptive_read_buffering
= 0;
606 p
->read_output_delay
= 0;
607 p
->read_output_skip
= 0;
610 /* If name is already in use, modify it until it is unused. */
615 tem
= Fget_process (name1
);
616 if (NILP (tem
)) break;
617 sprintf (suffix
, "<%d>", i
);
618 name1
= concat2 (name
, build_string (suffix
));
622 XSETPROCESS (val
, p
);
623 Vprocess_alist
= Fcons (Fcons (name
, val
), Vprocess_alist
);
628 remove_process (proc
)
629 register Lisp_Object proc
;
631 register Lisp_Object pair
;
633 pair
= Frassq (proc
, Vprocess_alist
);
634 Vprocess_alist
= Fdelq (pair
, Vprocess_alist
);
636 deactivate_process (proc
);
639 /* Setup coding systems of PROCESS. */
642 setup_process_coding_systems (process
)
645 struct Lisp_Process
*p
= XPROCESS (process
);
647 int outch
= p
->outfd
;
648 Lisp_Object coding_system
;
650 if (inch
< 0 || outch
< 0)
653 if (!proc_decode_coding_system
[inch
])
654 proc_decode_coding_system
[inch
]
655 = (struct coding_system
*) xmalloc (sizeof (struct coding_system
));
656 coding_system
= p
->decode_coding_system
;
657 if (! NILP (p
->filter
))
659 else if (BUFFERP (p
->buffer
))
661 if (NILP (XBUFFER (p
->buffer
)->enable_multibyte_characters
))
662 coding_system
= raw_text_coding_system (coding_system
);
664 setup_coding_system (coding_system
, proc_decode_coding_system
[inch
]);
666 if (!proc_encode_coding_system
[outch
])
667 proc_encode_coding_system
[outch
]
668 = (struct coding_system
*) xmalloc (sizeof (struct coding_system
));
669 setup_coding_system (p
->encode_coding_system
,
670 proc_encode_coding_system
[outch
]);
673 DEFUN ("processp", Fprocessp
, Sprocessp
, 1, 1, 0,
674 doc
: /* Return t if OBJECT is a process. */)
678 return PROCESSP (object
) ? Qt
: Qnil
;
681 DEFUN ("get-process", Fget_process
, Sget_process
, 1, 1, 0,
682 doc
: /* Return the process named NAME, or nil if there is none. */)
684 register Lisp_Object name
;
689 return Fcdr (Fassoc (name
, Vprocess_alist
));
692 DEFUN ("get-buffer-process", Fget_buffer_process
, Sget_buffer_process
, 1, 1, 0,
693 doc
: /* Return the (or a) process associated with BUFFER.
694 BUFFER may be a buffer or the name of one. */)
696 register Lisp_Object buffer
;
698 register Lisp_Object buf
, tail
, proc
;
700 if (NILP (buffer
)) return Qnil
;
701 buf
= Fget_buffer (buffer
);
702 if (NILP (buf
)) return Qnil
;
704 for (tail
= Vprocess_alist
; CONSP (tail
); tail
= XCDR (tail
))
706 proc
= Fcdr (XCAR (tail
));
707 if (PROCESSP (proc
) && EQ (XPROCESS (proc
)->buffer
, buf
))
713 /* This is how commands for the user decode process arguments. It
714 accepts a process, a process name, a buffer, a buffer name, or nil.
715 Buffers denote the first process in the buffer, and nil denotes the
720 register Lisp_Object name
;
722 register Lisp_Object proc
, obj
;
725 obj
= Fget_process (name
);
727 obj
= Fget_buffer (name
);
729 error ("Process %s does not exist", SDATA (name
));
731 else if (NILP (name
))
732 obj
= Fcurrent_buffer ();
736 /* Now obj should be either a buffer object or a process object.
740 proc
= Fget_buffer_process (obj
);
742 error ("Buffer %s has no process", SDATA (XBUFFER (obj
)->name
));
754 /* Fdelete_process promises to immediately forget about the process, but in
755 reality, Emacs needs to remember those processes until they have been
756 treated by sigchld_handler; otherwise this handler would consider the
757 process as being synchronous and say that the synchronous process is
759 static Lisp_Object deleted_pid_list
;
762 DEFUN ("delete-process", Fdelete_process
, Sdelete_process
, 1, 1, 0,
763 doc
: /* Delete PROCESS: kill it and forget about it immediately.
764 PROCESS may be a process, a buffer, the name of a process or buffer, or
765 nil, indicating the current buffer's process. */)
767 register Lisp_Object process
;
769 register struct Lisp_Process
*p
;
771 process
= get_process (process
);
772 p
= XPROCESS (process
);
774 p
->raw_status_new
= 0;
775 if (NETCONN1_P (p
) || SERIALCONN1_P (p
))
777 p
->status
= Fcons (Qexit
, Fcons (make_number (0), Qnil
));
778 p
->tick
= ++process_tick
;
781 else if (p
->infd
>= 0)
785 /* Assignment to EMACS_INT stops GCC whining about limited range
787 EMACS_INT pid
= p
->pid
;
789 /* No problem storing the pid here, as it is still in Vprocess_alist. */
790 deleted_pid_list
= Fcons (make_fixnum_or_float (pid
),
791 /* GC treated elements set to nil. */
792 Fdelq (Qnil
, deleted_pid_list
));
793 /* If the process has already signaled, remove it from the list. */
794 if (p
->raw_status_new
)
797 if (CONSP (p
->status
))
798 symbol
= XCAR (p
->status
);
799 if (EQ (symbol
, Qsignal
) || EQ (symbol
, Qexit
))
801 = Fdelete (make_fixnum_or_float (pid
), deleted_pid_list
);
805 Fkill_process (process
, Qnil
);
806 /* Do this now, since remove_process will make sigchld_handler do nothing. */
808 = Fcons (Qsignal
, Fcons (make_number (SIGKILL
), Qnil
));
809 p
->tick
= ++process_tick
;
813 remove_process (process
);
817 DEFUN ("process-status", Fprocess_status
, Sprocess_status
, 1, 1, 0,
818 doc
: /* Return the status of PROCESS.
819 The returned value is one of the following symbols:
820 run -- for a process that is running.
821 stop -- for a process stopped but continuable.
822 exit -- for a process that has exited.
823 signal -- for a process that has got a fatal signal.
824 open -- for a network stream connection that is open.
825 listen -- for a network stream server that is listening.
826 closed -- for a network stream connection that is closed.
827 connect -- when waiting for a non-blocking connection to complete.
828 failed -- when a non-blocking connection has failed.
829 nil -- if arg is a process name and no such process exists.
830 PROCESS may be a process, a buffer, the name of a process, or
831 nil, indicating the current buffer's process. */)
833 register Lisp_Object process
;
835 register struct Lisp_Process
*p
;
836 register Lisp_Object status
;
838 if (STRINGP (process
))
839 process
= Fget_process (process
);
841 process
= get_process (process
);
846 p
= XPROCESS (process
);
847 if (p
->raw_status_new
)
851 status
= XCAR (status
);
852 if (NETCONN1_P (p
) || SERIALCONN1_P (p
))
854 if (EQ (status
, Qexit
))
856 else if (EQ (p
->command
, Qt
))
858 else if (EQ (status
, Qrun
))
864 DEFUN ("process-exit-status", Fprocess_exit_status
, Sprocess_exit_status
,
866 doc
: /* Return the exit status of PROCESS or the signal number that killed it.
867 If PROCESS has not yet exited or died, return 0. */)
869 register Lisp_Object process
;
871 CHECK_PROCESS (process
);
872 if (XPROCESS (process
)->raw_status_new
)
873 update_status (XPROCESS (process
));
874 if (CONSP (XPROCESS (process
)->status
))
875 return XCAR (XCDR (XPROCESS (process
)->status
));
876 return make_number (0);
879 DEFUN ("process-id", Fprocess_id
, Sprocess_id
, 1, 1, 0,
880 doc
: /* Return the process id of PROCESS.
881 This is the pid of the external process which PROCESS uses or talks to.
882 For a network connection, this value is nil. */)
884 register Lisp_Object process
;
886 /* Assignment to EMACS_INT stops GCC whining about limited range of
890 CHECK_PROCESS (process
);
891 pid
= XPROCESS (process
)->pid
;
892 return (pid
? make_fixnum_or_float (pid
) : Qnil
);
895 DEFUN ("process-name", Fprocess_name
, Sprocess_name
, 1, 1, 0,
896 doc
: /* Return the name of PROCESS, as a string.
897 This is the name of the program invoked in PROCESS,
898 possibly modified to make it unique among process names. */)
900 register Lisp_Object process
;
902 CHECK_PROCESS (process
);
903 return XPROCESS (process
)->name
;
906 DEFUN ("process-command", Fprocess_command
, Sprocess_command
, 1, 1, 0,
907 doc
: /* Return the command that was executed to start PROCESS.
908 This is a list of strings, the first string being the program executed
909 and the rest of the strings being the arguments given to it.
910 For a network or serial process, this is nil (process is running) or t
911 \(process is stopped). */)
913 register Lisp_Object process
;
915 CHECK_PROCESS (process
);
916 return XPROCESS (process
)->command
;
919 DEFUN ("process-tty-name", Fprocess_tty_name
, Sprocess_tty_name
, 1, 1, 0,
920 doc
: /* Return the name of the terminal PROCESS uses, or nil if none.
921 This is the terminal that the process itself reads and writes on,
922 not the name of the pty that Emacs uses to talk with that terminal. */)
924 register Lisp_Object process
;
926 CHECK_PROCESS (process
);
927 return XPROCESS (process
)->tty_name
;
930 DEFUN ("set-process-buffer", Fset_process_buffer
, Sset_process_buffer
,
932 doc
: /* Set buffer associated with PROCESS to BUFFER (a buffer, or nil). */)
934 register Lisp_Object process
, buffer
;
936 struct Lisp_Process
*p
;
938 CHECK_PROCESS (process
);
940 CHECK_BUFFER (buffer
);
941 p
= XPROCESS (process
);
943 if (NETCONN1_P (p
) || SERIALCONN1_P (p
))
944 p
->childp
= Fplist_put (p
->childp
, QCbuffer
, buffer
);
945 setup_process_coding_systems (process
);
949 DEFUN ("process-buffer", Fprocess_buffer
, Sprocess_buffer
,
951 doc
: /* Return the buffer PROCESS is associated with.
952 Output from PROCESS is inserted in this buffer unless PROCESS has a filter. */)
954 register Lisp_Object process
;
956 CHECK_PROCESS (process
);
957 return XPROCESS (process
)->buffer
;
960 DEFUN ("process-mark", Fprocess_mark
, Sprocess_mark
,
962 doc
: /* Return the marker for the end of the last output from PROCESS. */)
964 register Lisp_Object process
;
966 CHECK_PROCESS (process
);
967 return XPROCESS (process
)->mark
;
970 DEFUN ("set-process-filter", Fset_process_filter
, Sset_process_filter
,
972 doc
: /* Give PROCESS the filter function FILTER; nil means no filter.
973 A value of t means stop accepting output from the process.
975 When a process has a filter, its buffer is not used for output.
976 Instead, each time it does output, the entire string of output is
977 passed to the filter.
979 The filter gets two arguments: the process and the string of output.
980 The string argument is normally a multibyte string, except:
981 - if the process' input coding system is no-conversion or raw-text,
982 it is a unibyte string (the non-converted input), or else
983 - if `default-enable-multibyte-characters' is nil, it is a unibyte
984 string (the result of converting the decoded input multibyte
985 string to unibyte with `string-make-unibyte'). */)
987 register Lisp_Object process
, filter
;
989 struct Lisp_Process
*p
;
991 CHECK_PROCESS (process
);
992 p
= XPROCESS (process
);
994 /* Don't signal an error if the process' input file descriptor
995 is closed. This could make debugging Lisp more difficult,
996 for example when doing something like
998 (setq process (start-process ...))
1000 (set-process-filter process ...) */
1004 if (EQ (filter
, Qt
) && !EQ (p
->status
, Qlisten
))
1006 FD_CLR (p
->infd
, &input_wait_mask
);
1007 FD_CLR (p
->infd
, &non_keyboard_wait_mask
);
1009 else if (EQ (p
->filter
, Qt
)
1010 /* Network or serial process not stopped: */
1011 && !EQ (p
->command
, Qt
))
1013 FD_SET (p
->infd
, &input_wait_mask
);
1014 FD_SET (p
->infd
, &non_keyboard_wait_mask
);
1019 if (NETCONN1_P (p
) || SERIALCONN1_P (p
))
1020 p
->childp
= Fplist_put (p
->childp
, QCfilter
, filter
);
1021 setup_process_coding_systems (process
);
1025 DEFUN ("process-filter", Fprocess_filter
, Sprocess_filter
,
1027 doc
: /* Returns the filter function of PROCESS; nil if none.
1028 See `set-process-filter' for more info on filter functions. */)
1030 register Lisp_Object process
;
1032 CHECK_PROCESS (process
);
1033 return XPROCESS (process
)->filter
;
1036 DEFUN ("set-process-sentinel", Fset_process_sentinel
, Sset_process_sentinel
,
1038 doc
: /* Give PROCESS the sentinel SENTINEL; nil for none.
1039 The sentinel is called as a function when the process changes state.
1040 It gets two arguments: the process, and a string describing the change. */)
1042 register Lisp_Object process
, sentinel
;
1044 struct Lisp_Process
*p
;
1046 CHECK_PROCESS (process
);
1047 p
= XPROCESS (process
);
1049 p
->sentinel
= sentinel
;
1050 if (NETCONN1_P (p
) || SERIALCONN1_P (p
))
1051 p
->childp
= Fplist_put (p
->childp
, QCsentinel
, sentinel
);
1055 DEFUN ("process-sentinel", Fprocess_sentinel
, Sprocess_sentinel
,
1057 doc
: /* Return the sentinel of PROCESS; nil if none.
1058 See `set-process-sentinel' for more info on sentinels. */)
1060 register Lisp_Object process
;
1062 CHECK_PROCESS (process
);
1063 return XPROCESS (process
)->sentinel
;
1066 DEFUN ("set-process-window-size", Fset_process_window_size
,
1067 Sset_process_window_size
, 3, 3, 0,
1068 doc
: /* Tell PROCESS that it has logical window size HEIGHT and WIDTH. */)
1069 (process
, height
, width
)
1070 register Lisp_Object process
, height
, width
;
1072 CHECK_PROCESS (process
);
1073 CHECK_NATNUM (height
);
1074 CHECK_NATNUM (width
);
1076 if (XPROCESS (process
)->infd
< 0
1077 || set_window_size (XPROCESS (process
)->infd
,
1078 XINT (height
), XINT (width
)) <= 0)
1084 DEFUN ("set-process-inherit-coding-system-flag",
1085 Fset_process_inherit_coding_system_flag
,
1086 Sset_process_inherit_coding_system_flag
, 2, 2, 0,
1087 doc
: /* Determine whether buffer of PROCESS will inherit coding-system.
1088 If the second argument FLAG is non-nil, then the variable
1089 `buffer-file-coding-system' of the buffer associated with PROCESS
1090 will be bound to the value of the coding system used to decode
1093 This is useful when the coding system specified for the process buffer
1094 leaves either the character code conversion or the end-of-line conversion
1095 unspecified, or if the coding system used to decode the process output
1096 is more appropriate for saving the process buffer.
1098 Binding the variable `inherit-process-coding-system' to non-nil before
1099 starting the process is an alternative way of setting the inherit flag
1100 for the process which will run. */)
1102 register Lisp_Object process
, flag
;
1104 CHECK_PROCESS (process
);
1105 XPROCESS (process
)->inherit_coding_system_flag
= !NILP (flag
);
1109 DEFUN ("process-inherit-coding-system-flag",
1110 Fprocess_inherit_coding_system_flag
, Sprocess_inherit_coding_system_flag
,
1112 doc
: /* Return the value of inherit-coding-system flag for PROCESS.
1113 If this flag is t, `buffer-file-coding-system' of the buffer
1114 associated with PROCESS will inherit the coding system used to decode
1115 the process output. */)
1117 register Lisp_Object process
;
1119 CHECK_PROCESS (process
);
1120 return XPROCESS (process
)->inherit_coding_system_flag
? Qt
: Qnil
;
1123 DEFUN ("set-process-query-on-exit-flag",
1124 Fset_process_query_on_exit_flag
, Sset_process_query_on_exit_flag
,
1126 doc
: /* Specify if query is needed for PROCESS when Emacs is exited.
1127 If the second argument FLAG is non-nil, Emacs will query the user before
1128 exiting if PROCESS is running. */)
1130 register Lisp_Object process
, flag
;
1132 CHECK_PROCESS (process
);
1133 XPROCESS (process
)->kill_without_query
= NILP (flag
);
1137 DEFUN ("process-query-on-exit-flag",
1138 Fprocess_query_on_exit_flag
, Sprocess_query_on_exit_flag
,
1140 doc
: /* Return the current value of query-on-exit flag for PROCESS. */)
1142 register Lisp_Object process
;
1144 CHECK_PROCESS (process
);
1145 return (XPROCESS (process
)->kill_without_query
? Qnil
: Qt
);
1148 #ifdef DATAGRAM_SOCKETS
1149 Lisp_Object
Fprocess_datagram_address ();
1152 DEFUN ("process-contact", Fprocess_contact
, Sprocess_contact
,
1154 doc
: /* Return the contact info of PROCESS; t for a real child.
1155 For a network or serial connection, the value depends on the optional
1156 KEY arg. If KEY is nil, value is a cons cell of the form (HOST
1157 SERVICE) for a network connection or (PORT SPEED) for a serial
1158 connection. If KEY is t, the complete contact information for the
1159 connection is returned, else the specific value for the keyword KEY is
1160 returned. See `make-network-process' or `make-serial-process' for a
1161 list of keywords. */)
1163 register Lisp_Object process
, key
;
1165 Lisp_Object contact
;
1167 CHECK_PROCESS (process
);
1168 contact
= XPROCESS (process
)->childp
;
1170 #ifdef DATAGRAM_SOCKETS
1171 if (DATAGRAM_CONN_P (process
)
1172 && (EQ (key
, Qt
) || EQ (key
, QCremote
)))
1173 contact
= Fplist_put (contact
, QCremote
,
1174 Fprocess_datagram_address (process
));
1177 if ((!NETCONN_P (process
) && !SERIALCONN_P (process
)) || EQ (key
, Qt
))
1179 if (NILP (key
) && NETCONN_P (process
))
1180 return Fcons (Fplist_get (contact
, QChost
),
1181 Fcons (Fplist_get (contact
, QCservice
), Qnil
));
1182 if (NILP (key
) && SERIALCONN_P (process
))
1183 return Fcons (Fplist_get (contact
, QCport
),
1184 Fcons (Fplist_get (contact
, QCspeed
), Qnil
));
1185 return Fplist_get (contact
, key
);
1188 DEFUN ("process-plist", Fprocess_plist
, Sprocess_plist
,
1190 doc
: /* Return the plist of PROCESS. */)
1192 register Lisp_Object process
;
1194 CHECK_PROCESS (process
);
1195 return XPROCESS (process
)->plist
;
1198 DEFUN ("set-process-plist", Fset_process_plist
, Sset_process_plist
,
1200 doc
: /* Replace the plist of PROCESS with PLIST. Returns PLIST. */)
1202 register Lisp_Object process
, plist
;
1204 CHECK_PROCESS (process
);
1207 XPROCESS (process
)->plist
= plist
;
1211 #if 0 /* Turned off because we don't currently record this info
1212 in the process. Perhaps add it. */
1213 DEFUN ("process-connection", Fprocess_connection
, Sprocess_connection
, 1, 1, 0,
1214 doc
: /* Return the connection type of PROCESS.
1215 The value is nil for a pipe, t or `pty' for a pty, or `stream' for
1216 a socket connection. */)
1218 Lisp_Object process
;
1220 return XPROCESS (process
)->type
;
1224 DEFUN ("process-type", Fprocess_type
, Sprocess_type
, 1, 1, 0,
1225 doc
: /* Return the connection type of PROCESS.
1226 The value is either the symbol `real', `network', or `serial'.
1227 PROCESS may be a process, a buffer, the name of a process or buffer, or
1228 nil, indicating the current buffer's process. */)
1230 Lisp_Object process
;
1233 proc
= get_process (process
);
1234 return XPROCESS (proc
)->type
;
1238 DEFUN ("format-network-address", Fformat_network_address
, Sformat_network_address
,
1240 doc
: /* Convert network ADDRESS from internal format to a string.
1241 A 4 or 5 element vector represents an IPv4 address (with port number).
1242 An 8 or 9 element vector represents an IPv6 address (with port number).
1243 If optional second argument OMIT-PORT is non-nil, don't include a port
1244 number in the string, even when present in ADDRESS.
1245 Returns nil if format of ADDRESS is invalid. */)
1246 (address
, omit_port
)
1247 Lisp_Object address
, omit_port
;
1252 if (STRINGP (address
)) /* AF_LOCAL */
1255 if (VECTORP (address
)) /* AF_INET or AF_INET6 */
1257 register struct Lisp_Vector
*p
= XVECTOR (address
);
1258 Lisp_Object args
[10];
1261 if (p
->size
== 4 || (p
->size
== 5 && !NILP (omit_port
)))
1263 args
[0] = build_string ("%d.%d.%d.%d");
1266 else if (p
->size
== 5)
1268 args
[0] = build_string ("%d.%d.%d.%d:%d");
1271 else if (p
->size
== 8 || (p
->size
== 9 && !NILP (omit_port
)))
1273 args
[0] = build_string ("%x:%x:%x:%x:%x:%x:%x:%x");
1276 else if (p
->size
== 9)
1278 args
[0] = build_string ("[%x:%x:%x:%x:%x:%x:%x:%x]:%d");
1284 for (i
= 0; i
< nargs
; i
++)
1286 EMACS_INT element
= XINT (p
->contents
[i
]);
1288 if (element
< 0 || element
> 65535)
1291 if (nargs
<= 5 /* IPv4 */
1292 && i
< 4 /* host, not port */
1296 args
[i
+1] = p
->contents
[i
];
1299 return Fformat (nargs
+1, args
);
1302 if (CONSP (address
))
1304 Lisp_Object args
[2];
1305 args
[0] = build_string ("<Family %d>");
1306 args
[1] = Fcar (address
);
1307 return Fformat (2, args
);
1315 list_processes_1 (query_only
)
1316 Lisp_Object query_only
;
1318 register Lisp_Object tail
, tem
;
1319 Lisp_Object proc
, minspace
, tem1
;
1320 register struct Lisp_Process
*p
;
1322 int w_proc
, w_buffer
, w_tty
;
1324 Lisp_Object i_status
, i_buffer
, i_tty
, i_command
;
1326 w_proc
= 4; /* Proc */
1327 w_buffer
= 6; /* Buffer */
1328 w_tty
= 0; /* Omit if no ttys */
1330 for (tail
= Vprocess_alist
; CONSP (tail
); tail
= XCDR (tail
))
1334 proc
= Fcdr (XCAR (tail
));
1335 p
= XPROCESS (proc
);
1338 if (!NILP (query_only
) && p
->kill_without_query
)
1340 if (STRINGP (p
->name
)
1341 && ( i
= SCHARS (p
->name
), (i
> w_proc
)))
1343 if (!NILP (p
->buffer
))
1345 if (NILP (XBUFFER (p
->buffer
)->name
))
1348 w_buffer
= 8; /* (Killed) */
1350 else if ((i
= SCHARS (XBUFFER (p
->buffer
)->name
), (i
> w_buffer
)))
1353 if (STRINGP (p
->tty_name
)
1354 && (i
= SCHARS (p
->tty_name
), (i
> w_tty
)))
1358 XSETFASTINT (i_status
, w_proc
+ 1);
1359 XSETFASTINT (i_buffer
, XFASTINT (i_status
) + 9);
1362 XSETFASTINT (i_tty
, XFASTINT (i_buffer
) + w_buffer
+ 1);
1363 XSETFASTINT (i_command
, XFASTINT (i_tty
) + w_tty
+ 1);
1368 XSETFASTINT (i_command
, XFASTINT (i_buffer
) + w_buffer
+ 1);
1371 XSETFASTINT (minspace
, 1);
1373 set_buffer_internal (XBUFFER (Vstandard_output
));
1374 current_buffer
->undo_list
= Qt
;
1376 current_buffer
->truncate_lines
= Qt
;
1378 write_string ("Proc", -1);
1379 Findent_to (i_status
, minspace
); write_string ("Status", -1);
1380 Findent_to (i_buffer
, minspace
); write_string ("Buffer", -1);
1383 Findent_to (i_tty
, minspace
); write_string ("Tty", -1);
1385 Findent_to (i_command
, minspace
); write_string ("Command", -1);
1386 write_string ("\n", -1);
1388 write_string ("----", -1);
1389 Findent_to (i_status
, minspace
); write_string ("------", -1);
1390 Findent_to (i_buffer
, minspace
); write_string ("------", -1);
1393 Findent_to (i_tty
, minspace
); write_string ("---", -1);
1395 Findent_to (i_command
, minspace
); write_string ("-------", -1);
1396 write_string ("\n", -1);
1398 for (tail
= Vprocess_alist
; CONSP (tail
); tail
= XCDR (tail
))
1402 proc
= Fcdr (XCAR (tail
));
1403 p
= XPROCESS (proc
);
1406 if (!NILP (query_only
) && p
->kill_without_query
)
1409 Finsert (1, &p
->name
);
1410 Findent_to (i_status
, minspace
);
1412 if (p
->raw_status_new
)
1415 if (CONSP (p
->status
))
1416 symbol
= XCAR (p
->status
);
1418 if (EQ (symbol
, Qsignal
))
1421 tem
= Fcar (Fcdr (p
->status
));
1422 Fprinc (symbol
, Qnil
);
1424 else if (NETCONN1_P (p
) || SERIALCONN1_P (p
))
1426 if (EQ (symbol
, Qexit
))
1427 write_string ("closed", -1);
1428 else if (EQ (p
->command
, Qt
))
1429 write_string ("stopped", -1);
1430 else if (EQ (symbol
, Qrun
))
1431 write_string ("open", -1);
1433 Fprinc (symbol
, Qnil
);
1435 else if (SERIALCONN1_P (p
))
1437 write_string ("running", -1);
1440 Fprinc (symbol
, Qnil
);
1442 if (EQ (symbol
, Qexit
))
1445 tem
= Fcar (Fcdr (p
->status
));
1448 sprintf (tembuf
, " %d", (int) XFASTINT (tem
));
1449 write_string (tembuf
, -1);
1453 if (EQ (symbol
, Qsignal
) || EQ (symbol
, Qexit
) || EQ (symbol
, Qclosed
))
1456 Findent_to (i_buffer
, minspace
);
1457 if (NILP (p
->buffer
))
1458 insert_string ("(none)");
1459 else if (NILP (XBUFFER (p
->buffer
)->name
))
1460 insert_string ("(Killed)");
1462 Finsert (1, &XBUFFER (p
->buffer
)->name
);
1466 Findent_to (i_tty
, minspace
);
1467 if (STRINGP (p
->tty_name
))
1468 Finsert (1, &p
->tty_name
);
1471 Findent_to (i_command
, minspace
);
1473 if (EQ (p
->status
, Qlisten
))
1475 Lisp_Object port
= Fplist_get (p
->childp
, QCservice
);
1476 if (INTEGERP (port
))
1477 port
= Fnumber_to_string (port
);
1479 port
= Fformat_network_address (Fplist_get (p
->childp
, QClocal
), Qnil
);
1480 sprintf (tembuf
, "(network %s server on %s)\n",
1481 (DATAGRAM_CHAN_P (p
->infd
) ? "datagram" : "stream"),
1482 (STRINGP (port
) ? (char *)SDATA (port
) : "?"));
1483 insert_string (tembuf
);
1485 else if (NETCONN1_P (p
))
1487 /* For a local socket, there is no host name,
1488 so display service instead. */
1489 Lisp_Object host
= Fplist_get (p
->childp
, QChost
);
1490 if (!STRINGP (host
))
1492 host
= Fplist_get (p
->childp
, QCservice
);
1493 if (INTEGERP (host
))
1494 host
= Fnumber_to_string (host
);
1497 host
= Fformat_network_address (Fplist_get (p
->childp
, QCremote
), Qnil
);
1498 sprintf (tembuf
, "(network %s connection to %s)\n",
1499 (DATAGRAM_CHAN_P (p
->infd
) ? "datagram" : "stream"),
1500 (STRINGP (host
) ? (char *)SDATA (host
) : "?"));
1501 insert_string (tembuf
);
1503 else if (SERIALCONN1_P (p
))
1505 Lisp_Object port
= Fplist_get (p
->childp
, QCport
);
1506 Lisp_Object speed
= Fplist_get (p
->childp
, QCspeed
);
1507 insert_string ("(serial port ");
1509 insert_string (SDATA (port
));
1511 insert_string ("?");
1512 if (INTEGERP (speed
))
1514 sprintf (tembuf
, " at %d b/s", XINT (speed
));
1515 insert_string (tembuf
);
1517 insert_string (")\n");
1529 insert_string (" ");
1531 insert_string ("\n");
1535 status_notify (NULL
);
1539 DEFUN ("list-processes", Flist_processes
, Slist_processes
, 0, 1, "P",
1540 doc
: /* Display a list of all processes.
1541 If optional argument QUERY-ONLY is non-nil, only processes with
1542 the query-on-exit flag set will be listed.
1543 Any process listed as exited or signaled is actually eliminated
1544 after the listing is made. */)
1546 Lisp_Object query_only
;
1548 internal_with_output_to_temp_buffer ("*Process List*",
1549 list_processes_1
, query_only
);
1553 DEFUN ("process-list", Fprocess_list
, Sprocess_list
, 0, 0, 0,
1554 doc
: /* Return a list of all processes. */)
1557 return Fmapcar (Qcdr
, Vprocess_alist
);
1560 /* Starting asynchronous inferior processes. */
1562 static Lisp_Object
start_process_unwind ();
1564 DEFUN ("start-process", Fstart_process
, Sstart_process
, 3, MANY
, 0,
1565 doc
: /* Start a program in a subprocess. Return the process object for it.
1566 NAME is name for process. It is modified if necessary to make it unique.
1567 BUFFER is the buffer (or buffer name) to associate with the process.
1569 Process output (both standard output and standard error streams) goes
1570 at end of BUFFER, unless you specify an output stream or filter
1571 function to handle the output. BUFFER may also be nil, meaning that
1572 this process is not associated with any buffer.
1574 PROGRAM is the program file name. It is searched for in PATH.
1575 Remaining arguments are strings to give program as arguments.
1577 If you want to separate standard output from standard error, invoke
1578 the command through a shell and redirect one of them using the shell
1581 usage: (start-process NAME BUFFER PROGRAM &rest PROGRAM-ARGS) */)
1584 register Lisp_Object
*args
;
1586 Lisp_Object buffer
, name
, program
, proc
, current_dir
, tem
;
1587 register unsigned char **new_argv
;
1589 int count
= SPECPDL_INDEX ();
1593 buffer
= Fget_buffer_create (buffer
);
1595 /* Make sure that the child will be able to chdir to the current
1596 buffer's current directory, or its unhandled equivalent. We
1597 can't just have the child check for an error when it does the
1598 chdir, since it's in a vfork.
1600 We have to GCPRO around this because Fexpand_file_name and
1601 Funhandled_file_name_directory might call a file name handling
1602 function. The argument list is protected by the caller, so all
1603 we really have to worry about is buffer. */
1605 struct gcpro gcpro1
, gcpro2
;
1607 current_dir
= current_buffer
->directory
;
1609 GCPRO2 (buffer
, current_dir
);
1611 current_dir
= Funhandled_file_name_directory (current_dir
);
1612 if (NILP (current_dir
))
1613 /* If the file name handler says that current_dir is unreachable, use
1614 a sensible default. */
1615 current_dir
= build_string ("~/");
1616 current_dir
= expand_and_dir_to_file (current_dir
, Qnil
);
1617 if (NILP (Ffile_accessible_directory_p (current_dir
)))
1618 report_file_error ("Setting current directory",
1619 Fcons (current_buffer
->directory
, Qnil
));
1625 CHECK_STRING (name
);
1629 CHECK_STRING (program
);
1631 proc
= make_process (name
);
1632 /* If an error occurs and we can't start the process, we want to
1633 remove it from the process list. This means that each error
1634 check in create_process doesn't need to call remove_process
1635 itself; it's all taken care of here. */
1636 record_unwind_protect (start_process_unwind
, proc
);
1638 XPROCESS (proc
)->childp
= Qt
;
1639 XPROCESS (proc
)->plist
= Qnil
;
1640 XPROCESS (proc
)->type
= Qreal
;
1641 XPROCESS (proc
)->buffer
= buffer
;
1642 XPROCESS (proc
)->sentinel
= Qnil
;
1643 XPROCESS (proc
)->filter
= Qnil
;
1644 XPROCESS (proc
)->command
= Flist (nargs
- 2, args
+ 2);
1646 #ifdef ADAPTIVE_READ_BUFFERING
1647 XPROCESS (proc
)->adaptive_read_buffering
1648 = (NILP (Vprocess_adaptive_read_buffering
) ? 0
1649 : EQ (Vprocess_adaptive_read_buffering
, Qt
) ? 1 : 2);
1652 /* Make the process marker point into the process buffer (if any). */
1653 if (BUFFERP (buffer
))
1654 set_marker_both (XPROCESS (proc
)->mark
, buffer
,
1655 BUF_ZV (XBUFFER (buffer
)),
1656 BUF_ZV_BYTE (XBUFFER (buffer
)));
1659 /* Decide coding systems for communicating with the process. Here
1660 we don't setup the structure coding_system nor pay attention to
1661 unibyte mode. They are done in create_process. */
1663 /* Qt denotes we have not yet called Ffind_operation_coding_system. */
1664 Lisp_Object coding_systems
= Qt
;
1665 Lisp_Object val
, *args2
;
1666 struct gcpro gcpro1
, gcpro2
;
1668 val
= Vcoding_system_for_read
;
1671 args2
= (Lisp_Object
*) alloca ((nargs
+ 1) * sizeof *args2
);
1672 args2
[0] = Qstart_process
;
1673 for (i
= 0; i
< nargs
; i
++) args2
[i
+ 1] = args
[i
];
1674 GCPRO2 (proc
, current_dir
);
1675 coding_systems
= Ffind_operation_coding_system (nargs
+ 1, args2
);
1677 if (CONSP (coding_systems
))
1678 val
= XCAR (coding_systems
);
1679 else if (CONSP (Vdefault_process_coding_system
))
1680 val
= XCAR (Vdefault_process_coding_system
);
1682 XPROCESS (proc
)->decode_coding_system
= val
;
1684 val
= Vcoding_system_for_write
;
1687 if (EQ (coding_systems
, Qt
))
1689 args2
= (Lisp_Object
*) alloca ((nargs
+ 1) * sizeof args2
);
1690 args2
[0] = Qstart_process
;
1691 for (i
= 0; i
< nargs
; i
++) args2
[i
+ 1] = args
[i
];
1692 GCPRO2 (proc
, current_dir
);
1693 coding_systems
= Ffind_operation_coding_system (nargs
+ 1, args2
);
1696 if (CONSP (coding_systems
))
1697 val
= XCDR (coding_systems
);
1698 else if (CONSP (Vdefault_process_coding_system
))
1699 val
= XCDR (Vdefault_process_coding_system
);
1701 XPROCESS (proc
)->encode_coding_system
= val
;
1704 new_argv
= (unsigned char **) alloca ((nargs
- 1) * sizeof (char *));
1706 /* If program file name is not absolute, search our path for it.
1707 Put the name we will really use in TEM. */
1708 if (!IS_DIRECTORY_SEP (SREF (program
, 0))
1709 && !(SCHARS (program
) > 1
1710 && IS_DEVICE_SEP (SREF (program
, 1))))
1712 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
;
1715 GCPRO4 (name
, program
, buffer
, current_dir
);
1716 openp (Vexec_path
, program
, Vexec_suffixes
, &tem
, make_number (X_OK
));
1719 report_file_error ("Searching for program", Fcons (program
, Qnil
));
1720 tem
= Fexpand_file_name (tem
, Qnil
);
1724 if (!NILP (Ffile_directory_p (program
)))
1725 error ("Specified program for new process is a directory");
1729 /* If program file name starts with /: for quoting a magic name,
1731 if (SBYTES (tem
) > 2 && SREF (tem
, 0) == '/'
1732 && SREF (tem
, 1) == ':')
1733 tem
= Fsubstring (tem
, make_number (2), Qnil
);
1735 /* Encode the file name and put it in NEW_ARGV.
1736 That's where the child will use it to execute the program. */
1737 tem
= ENCODE_FILE (tem
);
1738 new_argv
[0] = SDATA (tem
);
1740 /* Here we encode arguments by the coding system used for sending
1741 data to the process. We don't support using different coding
1742 systems for encoding arguments and for encoding data sent to the
1745 for (i
= 3; i
< nargs
; i
++)
1749 if (STRING_MULTIBYTE (tem
))
1750 tem
= (code_convert_string_norecord
1751 (tem
, XPROCESS (proc
)->encode_coding_system
, 1));
1752 new_argv
[i
- 2] = SDATA (tem
);
1754 new_argv
[i
- 2] = 0;
1756 XPROCESS (proc
)->decoding_buf
= make_uninit_string (0);
1757 XPROCESS (proc
)->decoding_carryover
= 0;
1758 XPROCESS (proc
)->encoding_buf
= make_uninit_string (0);
1760 XPROCESS (proc
)->inherit_coding_system_flag
1761 = !(NILP (buffer
) || !inherit_process_coding_system
);
1763 create_process (proc
, (char **) new_argv
, current_dir
);
1765 return unbind_to (count
, proc
);
1768 /* This function is the unwind_protect form for Fstart_process. If
1769 PROC doesn't have its pid set, then we know someone has signaled
1770 an error and the process wasn't started successfully, so we should
1771 remove it from the process list. */
1773 start_process_unwind (proc
)
1776 if (!PROCESSP (proc
))
1779 /* Was PROC started successfully? */
1780 if (XPROCESS (proc
)->pid
<= 0)
1781 remove_process (proc
);
1787 create_process_1 (timer
)
1788 struct atimer
*timer
;
1790 /* Nothing to do. */
1794 #if 0 /* This doesn't work; see the note before sigchld_handler. */
1797 /* Mimic blocking of signals on system V, which doesn't really have it. */
1799 /* Nonzero means we got a SIGCHLD when it was supposed to be blocked. */
1800 int sigchld_deferred
;
1803 create_process_sigchld ()
1805 signal (SIGCHLD
, create_process_sigchld
);
1807 sigchld_deferred
= 1;
1814 create_process (process
, new_argv
, current_dir
)
1815 Lisp_Object process
;
1817 Lisp_Object current_dir
;
1819 int inchannel
, outchannel
;
1822 #if !defined (WINDOWSNT) && defined (FD_CLOEXEC)
1823 int wait_child_setup
[2];
1825 #ifdef POSIX_SIGNALS
1828 struct sigaction sigint_action
;
1829 struct sigaction sigquit_action
;
1831 struct sigaction sighup_action
;
1833 #else /* !POSIX_SIGNALS */
1836 SIGTYPE (*sigchld
)();
1839 #endif /* !POSIX_SIGNALS */
1840 /* Use volatile to protect variables from being clobbered by longjmp. */
1841 volatile int forkin
, forkout
;
1842 volatile int pty_flag
= 0;
1844 extern char **environ
;
1847 inchannel
= outchannel
= -1;
1850 if (!NILP (Vprocess_connection_type
))
1851 outchannel
= inchannel
= allocate_pty ();
1855 #if ! defined (USG) || defined (USG_SUBTTY_WORKS)
1856 /* On most USG systems it does not work to open the pty's tty here,
1857 then close it and reopen it in the child. */
1859 /* Don't let this terminal become our controlling terminal
1860 (in case we don't have one). */
1861 forkout
= forkin
= emacs_open (pty_name
, O_RDWR
| O_NOCTTY
, 0);
1863 forkout
= forkin
= emacs_open (pty_name
, O_RDWR
, 0);
1866 report_file_error ("Opening pty", Qnil
);
1867 #if defined (DONT_REOPEN_PTY)
1868 /* In the case that vfork is defined as fork, the parent process
1869 (Emacs) may send some data before the child process completes
1870 tty options setup. So we setup tty before forking. */
1871 child_setup_tty (forkout
);
1872 #endif /* DONT_REOPEN_PTY */
1874 forkin
= forkout
= -1;
1875 #endif /* not USG, or USG_SUBTTY_WORKS */
1879 #endif /* HAVE_PTYS */
1884 report_file_error ("Creating pipe", Qnil
);
1890 emacs_close (inchannel
);
1891 emacs_close (forkout
);
1892 report_file_error ("Creating pipe", Qnil
);
1898 #if !defined (WINDOWSNT) && defined (FD_CLOEXEC)
1902 tem
= pipe (wait_child_setup
);
1904 report_file_error ("Creating pipe", Qnil
);
1905 tem
= fcntl (wait_child_setup
[1], F_GETFD
, 0);
1907 tem
= fcntl (wait_child_setup
[1], F_SETFD
, tem
| FD_CLOEXEC
);
1910 emacs_close (wait_child_setup
[0]);
1911 emacs_close (wait_child_setup
[1]);
1912 report_file_error ("Setting file descriptor flags", Qnil
);
1918 /* Replaced by close_process_descs */
1919 set_exclusive_use (inchannel
);
1920 set_exclusive_use (outchannel
);
1924 fcntl (inchannel
, F_SETFL
, O_NONBLOCK
);
1925 fcntl (outchannel
, F_SETFL
, O_NONBLOCK
);
1928 fcntl (inchannel
, F_SETFL
, O_NDELAY
);
1929 fcntl (outchannel
, F_SETFL
, O_NDELAY
);
1933 /* Record this as an active process, with its channels.
1934 As a result, child_setup will close Emacs's side of the pipes. */
1935 chan_process
[inchannel
] = process
;
1936 XPROCESS (process
)->infd
= inchannel
;
1937 XPROCESS (process
)->outfd
= outchannel
;
1939 /* Previously we recorded the tty descriptor used in the subprocess.
1940 It was only used for getting the foreground tty process, so now
1941 we just reopen the device (see emacs_get_tty_pgrp) as this is
1942 more portable (see USG_SUBTTY_WORKS above). */
1944 XPROCESS (process
)->pty_flag
= pty_flag
;
1945 XPROCESS (process
)->status
= Qrun
;
1946 setup_process_coding_systems (process
);
1948 /* Delay interrupts until we have a chance to store
1949 the new fork's pid in its process structure */
1950 #ifdef POSIX_SIGNALS
1951 sigemptyset (&blocked
);
1953 sigaddset (&blocked
, SIGCHLD
);
1955 #ifdef HAVE_WORKING_VFORK
1956 /* On many hosts (e.g. Solaris 2.4), if a vforked child calls `signal',
1957 this sets the parent's signal handlers as well as the child's.
1958 So delay all interrupts whose handlers the child might munge,
1959 and record the current handlers so they can be restored later. */
1960 sigaddset (&blocked
, SIGINT
); sigaction (SIGINT
, 0, &sigint_action
);
1961 sigaddset (&blocked
, SIGQUIT
); sigaction (SIGQUIT
, 0, &sigquit_action
);
1963 sigaddset (&blocked
, SIGHUP
); sigaction (SIGHUP
, 0, &sighup_action
);
1965 #endif /* HAVE_WORKING_VFORK */
1966 sigprocmask (SIG_BLOCK
, &blocked
, &procmask
);
1967 #else /* !POSIX_SIGNALS */
1969 #if defined (BSD_SYSTEM) || defined (HPUX)
1970 sigsetmask (sigmask (SIGCHLD
));
1971 #else /* ordinary USG */
1973 sigchld_deferred
= 0;
1974 sigchld
= signal (SIGCHLD
, create_process_sigchld
);
1976 #endif /* ordinary USG */
1977 #endif /* SIGCHLD */
1978 #endif /* !POSIX_SIGNALS */
1980 FD_SET (inchannel
, &input_wait_mask
);
1981 FD_SET (inchannel
, &non_keyboard_wait_mask
);
1982 if (inchannel
> max_process_desc
)
1983 max_process_desc
= inchannel
;
1985 /* Until we store the proper pid, enable sigchld_handler
1986 to recognize an unknown pid as standing for this process.
1987 It is very important not to let this `marker' value stay
1988 in the table after this function has returned; if it does
1989 it might cause call-process to hang and subsequent asynchronous
1990 processes to get their return values scrambled. */
1991 XPROCESS (process
)->pid
= -1;
1996 /* child_setup must clobber environ on systems with true vfork.
1997 Protect it from permanent change. */
1998 char **save_environ
= environ
;
2000 current_dir
= ENCODE_FILE (current_dir
);
2005 #endif /* not WINDOWSNT */
2007 int xforkin
= forkin
;
2008 int xforkout
= forkout
;
2010 #if 0 /* This was probably a mistake--it duplicates code later on,
2011 but fails to handle all the cases. */
2012 /* Make sure SIGCHLD is not blocked in the child. */
2013 sigsetmask (SIGEMPTYMASK
);
2016 /* Make the pty be the controlling terminal of the process. */
2018 /* First, disconnect its current controlling terminal. */
2020 /* We tried doing setsid only if pty_flag, but it caused
2021 process_set_signal to fail on SGI when using a pipe. */
2023 /* Make the pty's terminal the controlling terminal. */
2027 /* We ignore the return value
2028 because faith@cs.unc.edu says that is necessary on Linux. */
2029 ioctl (xforkin
, TIOCSCTTY
, 0);
2032 #else /* not HAVE_SETSID */
2034 /* It's very important to call setpgrp here and no time
2035 afterwards. Otherwise, we lose our controlling tty which
2036 is set when we open the pty. */
2039 #endif /* not HAVE_SETSID */
2040 #if defined (HAVE_TERMIOS) && defined (LDISC1)
2041 if (pty_flag
&& xforkin
>= 0)
2044 tcgetattr (xforkin
, &t
);
2046 if (tcsetattr (xforkin
, TCSANOW
, &t
) < 0)
2047 emacs_write (1, "create_process/tcsetattr LDISC1 failed\n", 39);
2050 #if defined (NTTYDISC) && defined (TIOCSETD)
2051 if (pty_flag
&& xforkin
>= 0)
2053 /* Use new line discipline. */
2054 int ldisc
= NTTYDISC
;
2055 ioctl (xforkin
, TIOCSETD
, &ldisc
);
2060 /* In 4.3BSD, the TIOCSPGRP bug has been fixed, and now you
2061 can do TIOCSPGRP only to the process's controlling tty. */
2064 /* I wonder: would just ioctl (0, TIOCNOTTY, 0) work here?
2065 I can't test it since I don't have 4.3. */
2066 int j
= emacs_open ("/dev/tty", O_RDWR
, 0);
2067 ioctl (j
, TIOCNOTTY
, 0);
2070 /* In order to get a controlling terminal on some versions
2071 of BSD, it is necessary to put the process in pgrp 0
2072 before it opens the terminal. */
2080 #endif /* TIOCNOTTY */
2082 #if !defined (DONT_REOPEN_PTY)
2083 /*** There is a suggestion that this ought to be a
2084 conditional on TIOCSPGRP,
2085 or !(defined (HAVE_SETSID) && defined (TIOCSCTTY)).
2086 Trying the latter gave the wrong results on Debian GNU/Linux 1.1;
2087 that system does seem to need this code, even though
2088 both HAVE_SETSID and TIOCSCTTY are defined. */
2089 /* Now close the pty (if we had it open) and reopen it.
2090 This makes the pty the controlling terminal of the subprocess. */
2094 /* I wonder if emacs_close (emacs_open (pty_name, ...))
2097 emacs_close (xforkin
);
2098 xforkout
= xforkin
= emacs_open (pty_name
, O_RDWR
, 0);
2102 emacs_write (1, "Couldn't open the pty terminal ", 31);
2103 emacs_write (1, pty_name
, strlen (pty_name
));
2104 emacs_write (1, "\n", 1);
2109 #endif /* not DONT_REOPEN_PTY */
2111 #ifdef SETUP_SLAVE_PTY
2116 #endif /* SETUP_SLAVE_PTY */
2118 /* On AIX, we've disabled SIGHUP above once we start a child on a pty.
2119 Now reenable it in the child, so it will die when we want it to. */
2121 signal (SIGHUP
, SIG_DFL
);
2123 #endif /* HAVE_PTYS */
2125 signal (SIGINT
, SIG_DFL
);
2126 signal (SIGQUIT
, SIG_DFL
);
2128 /* Stop blocking signals in the child. */
2129 #ifdef POSIX_SIGNALS
2130 sigprocmask (SIG_SETMASK
, &procmask
, 0);
2131 #else /* !POSIX_SIGNALS */
2133 #if defined (BSD_SYSTEM) || defined (HPUX)
2134 sigsetmask (SIGEMPTYMASK
);
2135 #else /* ordinary USG */
2137 signal (SIGCHLD
, sigchld
);
2139 #endif /* ordinary USG */
2140 #endif /* SIGCHLD */
2141 #endif /* !POSIX_SIGNALS */
2143 #if !defined (DONT_REOPEN_PTY)
2145 child_setup_tty (xforkout
);
2146 #endif /* not DONT_REOPEN_PTY */
2148 pid
= child_setup (xforkin
, xforkout
, xforkout
,
2149 new_argv
, 1, current_dir
);
2150 #else /* not WINDOWSNT */
2152 emacs_close (wait_child_setup
[0]);
2154 child_setup (xforkin
, xforkout
, xforkout
,
2155 new_argv
, 1, current_dir
);
2156 #endif /* not WINDOWSNT */
2158 environ
= save_environ
;
2163 /* This runs in the Emacs process. */
2167 emacs_close (forkin
);
2168 if (forkin
!= forkout
&& forkout
>= 0)
2169 emacs_close (forkout
);
2173 /* vfork succeeded. */
2174 XPROCESS (process
)->pid
= pid
;
2177 register_child (pid
, inchannel
);
2178 #endif /* WINDOWSNT */
2180 /* If the subfork execv fails, and it exits,
2181 this close hangs. I don't know why.
2182 So have an interrupt jar it loose. */
2184 struct atimer
*timer
;
2188 EMACS_SET_SECS_USECS (offset
, 1, 0);
2189 timer
= start_atimer (ATIMER_RELATIVE
, offset
, create_process_1
, 0);
2192 emacs_close (forkin
);
2194 cancel_atimer (timer
);
2198 if (forkin
!= forkout
&& forkout
>= 0)
2199 emacs_close (forkout
);
2203 XPROCESS (process
)->tty_name
= build_string (pty_name
);
2206 XPROCESS (process
)->tty_name
= Qnil
;
2208 #if !defined (WINDOWSNT) && defined (FD_CLOEXEC)
2209 /* Wait for child_setup to complete in case that vfork is
2210 actually defined as fork. The descriptor wait_child_setup[1]
2211 of a pipe is closed at the child side either by close-on-exec
2212 on successful execvp or the _exit call in child_setup. */
2216 emacs_close (wait_child_setup
[1]);
2217 emacs_read (wait_child_setup
[0], &dummy
, 1);
2218 emacs_close (wait_child_setup
[0]);
2223 /* Restore the signal state whether vfork succeeded or not.
2224 (We will signal an error, below, if it failed.) */
2225 #ifdef POSIX_SIGNALS
2226 #ifdef HAVE_WORKING_VFORK
2227 /* Restore the parent's signal handlers. */
2228 sigaction (SIGINT
, &sigint_action
, 0);
2229 sigaction (SIGQUIT
, &sigquit_action
, 0);
2231 sigaction (SIGHUP
, &sighup_action
, 0);
2233 #endif /* HAVE_WORKING_VFORK */
2234 /* Stop blocking signals in the parent. */
2235 sigprocmask (SIG_SETMASK
, &procmask
, 0);
2236 #else /* !POSIX_SIGNALS */
2238 #if defined (BSD_SYSTEM) || defined (HPUX)
2239 sigsetmask (SIGEMPTYMASK
);
2240 #else /* ordinary USG */
2242 signal (SIGCHLD
, sigchld
);
2243 /* Now really handle any of these signals
2244 that came in during this function. */
2245 if (sigchld_deferred
)
2246 kill (getpid (), SIGCHLD
);
2248 #endif /* ordinary USG */
2249 #endif /* SIGCHLD */
2250 #endif /* !POSIX_SIGNALS */
2252 /* Now generate the error if vfork failed. */
2254 report_file_error ("Doing vfork", Qnil
);
2260 /* Convert an internal struct sockaddr to a lisp object (vector or string).
2261 The address family of sa is not included in the result. */
2264 conv_sockaddr_to_lisp (sa
, len
)
2265 struct sockaddr
*sa
;
2268 Lisp_Object address
;
2271 register struct Lisp_Vector
*p
;
2273 switch (sa
->sa_family
)
2277 struct sockaddr_in
*sin
= (struct sockaddr_in
*) sa
;
2278 len
= sizeof (sin
->sin_addr
) + 1;
2279 address
= Fmake_vector (make_number (len
), Qnil
);
2280 p
= XVECTOR (address
);
2281 p
->contents
[--len
] = make_number (ntohs (sin
->sin_port
));
2282 cp
= (unsigned char *)&sin
->sin_addr
;
2288 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*) sa
;
2289 uint16_t *ip6
= (uint16_t *)&sin6
->sin6_addr
;
2290 len
= sizeof (sin6
->sin6_addr
)/2 + 1;
2291 address
= Fmake_vector (make_number (len
), Qnil
);
2292 p
= XVECTOR (address
);
2293 p
->contents
[--len
] = make_number (ntohs (sin6
->sin6_port
));
2294 for (i
= 0; i
< len
; i
++)
2295 p
->contents
[i
] = make_number (ntohs (ip6
[i
]));
2299 #ifdef HAVE_LOCAL_SOCKETS
2302 struct sockaddr_un
*sockun
= (struct sockaddr_un
*) sa
;
2303 for (i
= 0; i
< sizeof (sockun
->sun_path
); i
++)
2304 if (sockun
->sun_path
[i
] == 0)
2306 return make_unibyte_string (sockun
->sun_path
, i
);
2310 len
-= sizeof (sa
->sa_family
);
2311 address
= Fcons (make_number (sa
->sa_family
),
2312 Fmake_vector (make_number (len
), Qnil
));
2313 p
= XVECTOR (XCDR (address
));
2314 cp
= (unsigned char *) sa
+ sizeof (sa
->sa_family
);
2320 p
->contents
[i
++] = make_number (*cp
++);
2326 /* Get family and required size for sockaddr structure to hold ADDRESS. */
2329 get_lisp_to_sockaddr_size (address
, familyp
)
2330 Lisp_Object address
;
2333 register struct Lisp_Vector
*p
;
2335 if (VECTORP (address
))
2337 p
= XVECTOR (address
);
2341 return sizeof (struct sockaddr_in
);
2344 else if (p
->size
== 9)
2346 *familyp
= AF_INET6
;
2347 return sizeof (struct sockaddr_in6
);
2351 #ifdef HAVE_LOCAL_SOCKETS
2352 else if (STRINGP (address
))
2354 *familyp
= AF_LOCAL
;
2355 return sizeof (struct sockaddr_un
);
2358 else if (CONSP (address
) && INTEGERP (XCAR (address
)) && VECTORP (XCDR (address
)))
2360 struct sockaddr
*sa
;
2361 *familyp
= XINT (XCAR (address
));
2362 p
= XVECTOR (XCDR (address
));
2363 return p
->size
+ sizeof (sa
->sa_family
);
2368 /* Convert an address object (vector or string) to an internal sockaddr.
2370 The address format has been basically validated by
2371 get_lisp_to_sockaddr_size, but this does not mean FAMILY is valid;
2372 it could have come from user data. So if FAMILY is not valid,
2373 we return after zeroing *SA. */
2376 conv_lisp_to_sockaddr (family
, address
, sa
, len
)
2378 Lisp_Object address
;
2379 struct sockaddr
*sa
;
2382 register struct Lisp_Vector
*p
;
2383 register unsigned char *cp
= NULL
;
2388 if (VECTORP (address
))
2390 p
= XVECTOR (address
);
2391 if (family
== AF_INET
)
2393 struct sockaddr_in
*sin
= (struct sockaddr_in
*) sa
;
2394 len
= sizeof (sin
->sin_addr
) + 1;
2395 i
= XINT (p
->contents
[--len
]);
2396 sin
->sin_port
= htons (i
);
2397 cp
= (unsigned char *)&sin
->sin_addr
;
2398 sa
->sa_family
= family
;
2401 else if (family
== AF_INET6
)
2403 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*) sa
;
2404 uint16_t *ip6
= (uint16_t *)&sin6
->sin6_addr
;
2405 len
= sizeof (sin6
->sin6_addr
) + 1;
2406 i
= XINT (p
->contents
[--len
]);
2407 sin6
->sin6_port
= htons (i
);
2408 for (i
= 0; i
< len
; i
++)
2409 if (INTEGERP (p
->contents
[i
]))
2411 int j
= XFASTINT (p
->contents
[i
]) & 0xffff;
2414 sa
->sa_family
= family
;
2419 else if (STRINGP (address
))
2421 #ifdef HAVE_LOCAL_SOCKETS
2422 if (family
== AF_LOCAL
)
2424 struct sockaddr_un
*sockun
= (struct sockaddr_un
*) sa
;
2425 cp
= SDATA (address
);
2426 for (i
= 0; i
< sizeof (sockun
->sun_path
) && *cp
; i
++)
2427 sockun
->sun_path
[i
] = *cp
++;
2428 sa
->sa_family
= family
;
2435 p
= XVECTOR (XCDR (address
));
2436 cp
= (unsigned char *)sa
+ sizeof (sa
->sa_family
);
2439 for (i
= 0; i
< len
; i
++)
2440 if (INTEGERP (p
->contents
[i
]))
2441 *cp
++ = XFASTINT (p
->contents
[i
]) & 0xff;
2444 #ifdef DATAGRAM_SOCKETS
2445 DEFUN ("process-datagram-address", Fprocess_datagram_address
, Sprocess_datagram_address
,
2447 doc
: /* Get the current datagram address associated with PROCESS. */)
2449 Lisp_Object process
;
2453 CHECK_PROCESS (process
);
2455 if (!DATAGRAM_CONN_P (process
))
2458 channel
= XPROCESS (process
)->infd
;
2459 return conv_sockaddr_to_lisp (datagram_address
[channel
].sa
,
2460 datagram_address
[channel
].len
);
2463 DEFUN ("set-process-datagram-address", Fset_process_datagram_address
, Sset_process_datagram_address
,
2465 doc
: /* Set the datagram address for PROCESS to ADDRESS.
2466 Returns nil upon error setting address, ADDRESS otherwise. */)
2468 Lisp_Object process
, address
;
2473 CHECK_PROCESS (process
);
2475 if (!DATAGRAM_CONN_P (process
))
2478 channel
= XPROCESS (process
)->infd
;
2480 len
= get_lisp_to_sockaddr_size (address
, &family
);
2481 if (datagram_address
[channel
].len
!= len
)
2483 conv_lisp_to_sockaddr (family
, address
, datagram_address
[channel
].sa
, len
);
2489 static struct socket_options
{
2490 /* The name of this option. Should be lowercase version of option
2491 name without SO_ prefix. */
2493 /* Option level SOL_... */
2495 /* Option number SO_... */
2497 enum { SOPT_UNKNOWN
, SOPT_BOOL
, SOPT_INT
, SOPT_IFNAME
, SOPT_LINGER
} opttype
;
2498 enum { OPIX_NONE
=0, OPIX_MISC
=1, OPIX_REUSEADDR
=2 } optbit
;
2499 } socket_options
[] =
2501 #ifdef SO_BINDTODEVICE
2502 { ":bindtodevice", SOL_SOCKET
, SO_BINDTODEVICE
, SOPT_IFNAME
, OPIX_MISC
},
2505 { ":broadcast", SOL_SOCKET
, SO_BROADCAST
, SOPT_BOOL
, OPIX_MISC
},
2508 { ":dontroute", SOL_SOCKET
, SO_DONTROUTE
, SOPT_BOOL
, OPIX_MISC
},
2511 { ":keepalive", SOL_SOCKET
, SO_KEEPALIVE
, SOPT_BOOL
, OPIX_MISC
},
2514 { ":linger", SOL_SOCKET
, SO_LINGER
, SOPT_LINGER
, OPIX_MISC
},
2517 { ":oobinline", SOL_SOCKET
, SO_OOBINLINE
, SOPT_BOOL
, OPIX_MISC
},
2520 { ":priority", SOL_SOCKET
, SO_PRIORITY
, SOPT_INT
, OPIX_MISC
},
2523 { ":reuseaddr", SOL_SOCKET
, SO_REUSEADDR
, SOPT_BOOL
, OPIX_REUSEADDR
},
2525 { 0, 0, 0, SOPT_UNKNOWN
, OPIX_NONE
}
2528 /* Set option OPT to value VAL on socket S.
2530 Returns (1<<socket_options[OPT].optbit) if option is known, 0 otherwise.
2531 Signals an error if setting a known option fails.
2535 set_socket_option (s
, opt
, val
)
2537 Lisp_Object opt
, val
;
2540 struct socket_options
*sopt
;
2545 name
= (char *) SDATA (SYMBOL_NAME (opt
));
2546 for (sopt
= socket_options
; sopt
->name
; sopt
++)
2547 if (strcmp (name
, sopt
->name
) == 0)
2550 switch (sopt
->opttype
)
2555 optval
= NILP (val
) ? 0 : 1;
2556 ret
= setsockopt (s
, sopt
->optlevel
, sopt
->optnum
,
2557 &optval
, sizeof (optval
));
2565 optval
= XINT (val
);
2567 error ("Bad option value for %s", name
);
2568 ret
= setsockopt (s
, sopt
->optlevel
, sopt
->optnum
,
2569 &optval
, sizeof (optval
));
2573 #ifdef SO_BINDTODEVICE
2576 char devname
[IFNAMSIZ
+1];
2578 /* This is broken, at least in the Linux 2.4 kernel.
2579 To unbind, the arg must be a zero integer, not the empty string.
2580 This should work on all systems. KFS. 2003-09-23. */
2581 bzero (devname
, sizeof devname
);
2584 char *arg
= (char *) SDATA (val
);
2585 int len
= min (strlen (arg
), IFNAMSIZ
);
2586 bcopy (arg
, devname
, len
);
2588 else if (!NILP (val
))
2589 error ("Bad option value for %s", name
);
2590 ret
= setsockopt (s
, sopt
->optlevel
, sopt
->optnum
,
2599 struct linger linger
;
2602 linger
.l_linger
= 0;
2604 linger
.l_linger
= XINT (val
);
2606 linger
.l_onoff
= NILP (val
) ? 0 : 1;
2607 ret
= setsockopt (s
, sopt
->optlevel
, sopt
->optnum
,
2608 &linger
, sizeof (linger
));
2618 report_file_error ("Cannot set network option",
2619 Fcons (opt
, Fcons (val
, Qnil
)));
2620 return (1 << sopt
->optbit
);
2624 DEFUN ("set-network-process-option",
2625 Fset_network_process_option
, Sset_network_process_option
,
2627 doc
: /* For network process PROCESS set option OPTION to value VALUE.
2628 See `make-network-process' for a list of options and values.
2629 If optional fourth arg NO-ERROR is non-nil, don't signal an error if
2630 OPTION is not a supported option, return nil instead; otherwise return t. */)
2631 (process
, option
, value
, no_error
)
2632 Lisp_Object process
, option
, value
;
2633 Lisp_Object no_error
;
2636 struct Lisp_Process
*p
;
2638 CHECK_PROCESS (process
);
2639 p
= XPROCESS (process
);
2640 if (!NETCONN1_P (p
))
2641 error ("Process is not a network process");
2645 error ("Process is not running");
2647 if (set_socket_option (s
, option
, value
))
2649 p
->childp
= Fplist_put (p
->childp
, option
, value
);
2653 if (NILP (no_error
))
2654 error ("Unknown or unsupported option");
2661 DEFUN ("serial-process-configure",
2662 Fserial_process_configure
,
2663 Sserial_process_configure
,
2665 doc
: /* Configure speed, bytesize, etc. of a serial process.
2667 Arguments are specified as keyword/argument pairs. Attributes that
2668 are not given are re-initialized from the process's current
2669 configuration (available via the function `process-contact') or set to
2670 reasonable default values. The following arguments are defined:
2676 -- Any of these arguments can be given to identify the process that is
2677 to be configured. If none of these arguments is given, the current
2678 buffer's process is used.
2680 :speed SPEED -- SPEED is the speed of the serial port in bits per
2681 second, also called baud rate. Any value can be given for SPEED, but
2682 most serial ports work only at a few defined values between 1200 and
2683 115200, with 9600 being the most common value. If SPEED is nil, the
2684 serial port is not configured any further, i.e., all other arguments
2685 are ignored. This may be useful for special serial ports such as
2686 Bluetooth-to-serial converters which can only be configured through AT
2687 commands. A value of nil for SPEED can be used only when passed
2688 through `make-serial-process' or `serial-term'.
2690 :bytesize BYTESIZE -- BYTESIZE is the number of bits per byte, which
2691 can be 7 or 8. If BYTESIZE is not given or nil, a value of 8 is used.
2693 :parity PARITY -- PARITY can be nil (don't use parity), the symbol
2694 `odd' (use odd parity), or the symbol `even' (use even parity). If
2695 PARITY is not given, no parity is used.
2697 :stopbits STOPBITS -- STOPBITS is the number of stopbits used to
2698 terminate a byte transmission. STOPBITS can be 1 or 2. If STOPBITS
2699 is not given or nil, 1 stopbit is used.
2701 :flowcontrol FLOWCONTROL -- FLOWCONTROL determines the type of
2702 flowcontrol to be used, which is either nil (don't use flowcontrol),
2703 the symbol `hw' (use RTS/CTS hardware flowcontrol), or the symbol `sw'
2704 \(use XON/XOFF software flowcontrol). If FLOWCONTROL is not given, no
2705 flowcontrol is used.
2707 `serial-process-configure' is called by `make-serial-process' for the
2708 initial configuration of the serial port.
2712 \(serial-process-configure :process "/dev/ttyS0" :speed 1200)
2714 \(serial-process-configure
2715 :buffer "COM1" :stopbits 1 :parity 'odd :flowcontrol 'hw)
2717 \(serial-process-configure :port "\\\\.\\COM13" :bytesize 7)
2719 usage: (serial-process-configure &rest ARGS) */)
2724 struct Lisp_Process
*p
;
2725 Lisp_Object contact
= Qnil
;
2726 Lisp_Object proc
= Qnil
;
2727 struct gcpro gcpro1
;
2729 contact
= Flist (nargs
, args
);
2732 proc
= Fplist_get (contact
, QCprocess
);
2734 proc
= Fplist_get (contact
, QCname
);
2736 proc
= Fplist_get (contact
, QCbuffer
);
2738 proc
= Fplist_get (contact
, QCport
);
2739 proc
= get_process (proc
);
2740 p
= XPROCESS (proc
);
2741 if (!EQ (p
->type
, Qserial
))
2742 error ("Not a serial process");
2744 if (NILP (Fplist_get (p
->childp
, QCspeed
)))
2750 serial_configure (p
, contact
);
2756 /* Used by make-serial-process to recover from errors. */
2757 Lisp_Object
make_serial_process_unwind (Lisp_Object proc
)
2759 if (!PROCESSP (proc
))
2761 remove_process (proc
);
2765 DEFUN ("make-serial-process", Fmake_serial_process
, Smake_serial_process
,
2767 doc
: /* Create and return a serial port process.
2769 In Emacs, serial port connections are represented by process objects,
2770 so input and output work as for subprocesses, and `delete-process'
2771 closes a serial port connection. However, a serial process has no
2772 process id, it cannot be signaled, and the status codes are different
2773 from normal processes.
2775 `make-serial-process' creates a process and a buffer, on which you
2776 probably want to use `process-send-string'. Try \\[serial-term] for
2777 an interactive terminal. See below for examples.
2779 Arguments are specified as keyword/argument pairs. The following
2780 arguments are defined:
2782 :port PORT -- (mandatory) PORT is the path or name of the serial port.
2783 For example, this could be "/dev/ttyS0" on Unix. On Windows, this
2784 could be "COM1", or "\\\\.\\COM10" for ports higher than COM9 (double
2785 the backslashes in strings).
2787 :speed SPEED -- (mandatory) is handled by `serial-process-configure',
2788 which is called by `make-serial-process'.
2790 :name NAME -- NAME is the name of the process. If NAME is not given,
2791 the value of PORT is used.
2793 :buffer BUFFER -- BUFFER is the buffer (or buffer-name) to associate
2794 with the process. Process output goes at the end of that buffer,
2795 unless you specify an output stream or filter function to handle the
2796 output. If BUFFER is not given, the value of NAME is used.
2798 :coding CODING -- If CODING is a symbol, it specifies the coding
2799 system used for both reading and writing for this process. If CODING
2800 is a cons (DECODING . ENCODING), DECODING is used for reading, and
2801 ENCODING is used for writing.
2803 :noquery BOOL -- When exiting Emacs, query the user if BOOL is nil and
2804 the process is running. If BOOL is not given, query before exiting.
2806 :stop BOOL -- Start process in the `stopped' state if BOOL is non-nil.
2807 In the stopped state, a serial process does not accept incoming data,
2808 but you can send outgoing data. The stopped state is cleared by
2809 `continue-process' and set by `stop-process'.
2811 :filter FILTER -- Install FILTER as the process filter.
2813 :sentinel SENTINEL -- Install SENTINEL as the process sentinel.
2815 :plist PLIST -- Install PLIST as the initial plist of the process.
2822 -- These arguments are handled by `serial-process-configure', which is
2823 called by `make-serial-process'.
2825 The original argument list, possibly modified by later configuration,
2826 is available via the function `process-contact'.
2830 \(make-serial-process :port "/dev/ttyS0" :speed 9600)
2832 \(make-serial-process :port "COM1" :speed 115200 :stopbits 2)
2834 \(make-serial-process :port "\\\\.\\COM13" :speed 1200 :bytesize 7 :parity 'odd)
2836 \(make-serial-process :port "/dev/tty.BlueConsole-SPP-1" :speed nil)
2838 usage: (make-serial-process &rest ARGS) */)
2844 Lisp_Object proc
, contact
, port
;
2845 struct Lisp_Process
*p
;
2846 struct gcpro gcpro1
;
2847 Lisp_Object name
, buffer
;
2848 Lisp_Object tem
, val
;
2849 int specpdl_count
= -1;
2854 contact
= Flist (nargs
, args
);
2857 port
= Fplist_get (contact
, QCport
);
2859 error ("No port specified");
2860 CHECK_STRING (port
);
2862 if (NILP (Fplist_member (contact
, QCspeed
)))
2863 error (":speed not specified");
2864 if (!NILP (Fplist_get (contact
, QCspeed
)))
2865 CHECK_NUMBER (Fplist_get (contact
, QCspeed
));
2867 name
= Fplist_get (contact
, QCname
);
2870 CHECK_STRING (name
);
2871 proc
= make_process (name
);
2872 specpdl_count
= SPECPDL_INDEX ();
2873 record_unwind_protect (make_serial_process_unwind
, proc
);
2874 p
= XPROCESS (proc
);
2876 fd
= serial_open ((char*) SDATA (port
));
2879 if (fd
> max_process_desc
)
2880 max_process_desc
= fd
;
2881 chan_process
[fd
] = proc
;
2883 buffer
= Fplist_get (contact
, QCbuffer
);
2886 buffer
= Fget_buffer_create (buffer
);
2889 p
->childp
= contact
;
2890 p
->plist
= Fcopy_sequence (Fplist_get (contact
, QCplist
));
2892 p
->sentinel
= Fplist_get (contact
, QCsentinel
);
2893 p
->filter
= Fplist_get (contact
, QCfilter
);
2895 if (tem
= Fplist_get (contact
, QCnoquery
), !NILP (tem
))
2896 p
->kill_without_query
= 1;
2897 if (tem
= Fplist_get (contact
, QCstop
), !NILP (tem
))
2901 if (!EQ (p
->command
, Qt
))
2903 FD_SET (fd
, &input_wait_mask
);
2904 FD_SET (fd
, &non_keyboard_wait_mask
);
2907 if (BUFFERP (buffer
))
2909 set_marker_both (p
->mark
, buffer
,
2910 BUF_ZV (XBUFFER (buffer
)),
2911 BUF_ZV_BYTE (XBUFFER (buffer
)));
2914 tem
= Fplist_member (contact
, QCcoding
);
2915 if (!NILP (tem
) && (!CONSP (tem
) || !CONSP (XCDR (tem
))))
2921 val
= XCAR (XCDR (tem
));
2925 else if (!NILP (Vcoding_system_for_read
))
2926 val
= Vcoding_system_for_read
;
2927 else if ((!NILP (buffer
) && NILP (XBUFFER (buffer
)->enable_multibyte_characters
))
2928 || (NILP (buffer
) && NILP (buffer_defaults
.enable_multibyte_characters
)))
2930 p
->decode_coding_system
= val
;
2935 val
= XCAR (XCDR (tem
));
2939 else if (!NILP (Vcoding_system_for_write
))
2940 val
= Vcoding_system_for_write
;
2941 else if ((!NILP (buffer
) && NILP (XBUFFER (buffer
)->enable_multibyte_characters
))
2942 || (NILP (buffer
) && NILP (buffer_defaults
.enable_multibyte_characters
)))
2944 p
->encode_coding_system
= val
;
2946 setup_process_coding_systems (proc
);
2947 p
->decoding_buf
= make_uninit_string (0);
2948 p
->decoding_carryover
= 0;
2949 p
->encoding_buf
= make_uninit_string (0);
2950 p
->inherit_coding_system_flag
2951 = !(!NILP (tem
) || NILP (buffer
) || !inherit_process_coding_system
);
2953 Fserial_process_configure(nargs
, args
);
2955 specpdl_ptr
= specpdl
+ specpdl_count
;
2960 #endif /* HAVE_SERIAL */
2962 /* Create a network stream/datagram client/server process. Treated
2963 exactly like a normal process when reading and writing. Primary
2964 differences are in status display and process deletion. A network
2965 connection has no PID; you cannot signal it. All you can do is
2966 stop/continue it and deactivate/close it via delete-process */
2968 DEFUN ("make-network-process", Fmake_network_process
, Smake_network_process
,
2970 doc
: /* Create and return a network server or client process.
2972 In Emacs, network connections are represented by process objects, so
2973 input and output work as for subprocesses and `delete-process' closes
2974 a network connection. However, a network process has no process id,
2975 it cannot be signaled, and the status codes are different from normal
2978 Arguments are specified as keyword/argument pairs. The following
2979 arguments are defined:
2981 :name NAME -- NAME is name for process. It is modified if necessary
2984 :buffer BUFFER -- BUFFER is the buffer (or buffer-name) to associate
2985 with the process. Process output goes at end of that buffer, unless
2986 you specify an output stream or filter function to handle the output.
2987 BUFFER may be also nil, meaning that this process is not associated
2990 :host HOST -- HOST is name of the host to connect to, or its IP
2991 address. The symbol `local' specifies the local host. If specified
2992 for a server process, it must be a valid name or address for the local
2993 host, and only clients connecting to that address will be accepted.
2995 :service SERVICE -- SERVICE is name of the service desired, or an
2996 integer specifying a port number to connect to. If SERVICE is t,
2997 a random port number is selected for the server. (If Emacs was
2998 compiled with getaddrinfo, a port number can also be specified as a
2999 string, e.g. "80", as well as an integer. This is not portable.)
3001 :type TYPE -- TYPE is the type of connection. The default (nil) is a
3002 stream type connection, `datagram' creates a datagram type connection.
3004 :family FAMILY -- FAMILY is the address (and protocol) family for the
3005 service specified by HOST and SERVICE. The default (nil) is to use
3006 whatever address family (IPv4 or IPv6) that is defined for the host
3007 and port number specified by HOST and SERVICE. Other address families
3009 local -- for a local (i.e. UNIX) address specified by SERVICE.
3010 ipv4 -- use IPv4 address family only.
3011 ipv6 -- use IPv6 address family only.
3013 :local ADDRESS -- ADDRESS is the local address used for the connection.
3014 This parameter is ignored when opening a client process. When specified
3015 for a server process, the FAMILY, HOST and SERVICE args are ignored.
3017 :remote ADDRESS -- ADDRESS is the remote partner's address for the
3018 connection. This parameter is ignored when opening a stream server
3019 process. For a datagram server process, it specifies the initial
3020 setting of the remote datagram address. When specified for a client
3021 process, the FAMILY, HOST, and SERVICE args are ignored.
3023 The format of ADDRESS depends on the address family:
3024 - An IPv4 address is represented as an vector of integers [A B C D P]
3025 corresponding to numeric IP address A.B.C.D and port number P.
3026 - A local address is represented as a string with the address in the
3027 local address space.
3028 - An "unsupported family" address is represented by a cons (F . AV)
3029 where F is the family number and AV is a vector containing the socket
3030 address data with one element per address data byte. Do not rely on
3031 this format in portable code, as it may depend on implementation
3032 defined constants, data sizes, and data structure alignment.
3034 :coding CODING -- If CODING is a symbol, it specifies the coding
3035 system used for both reading and writing for this process. If CODING
3036 is a cons (DECODING . ENCODING), DECODING is used for reading, and
3037 ENCODING is used for writing.
3039 :nowait BOOL -- If BOOL is non-nil for a stream type client process,
3040 return without waiting for the connection to complete; instead, the
3041 sentinel function will be called with second arg matching "open" (if
3042 successful) or "failed" when the connect completes. Default is to use
3043 a blocking connect (i.e. wait) for stream type connections.
3045 :noquery BOOL -- Query the user unless BOOL is non-nil, and process is
3046 running when Emacs is exited.
3048 :stop BOOL -- Start process in the `stopped' state if BOOL non-nil.
3049 In the stopped state, a server process does not accept new
3050 connections, and a client process does not handle incoming traffic.
3051 The stopped state is cleared by `continue-process' and set by
3054 :filter FILTER -- Install FILTER as the process filter.
3056 :filter-multibyte BOOL -- If BOOL is non-nil, strings given to the
3057 process filter are multibyte, otherwise they are unibyte.
3058 If this keyword is not specified, the strings are multibyte if
3059 `default-enable-multibyte-characters' is non-nil.
3061 :sentinel SENTINEL -- Install SENTINEL as the process sentinel.
3063 :log LOG -- Install LOG as the server process log function. This
3064 function is called when the server accepts a network connection from a
3065 client. The arguments are SERVER, CLIENT, and MESSAGE, where SERVER
3066 is the server process, CLIENT is the new process for the connection,
3067 and MESSAGE is a string.
3069 :plist PLIST -- Install PLIST as the new process' initial plist.
3071 :server QLEN -- if QLEN is non-nil, create a server process for the
3072 specified FAMILY, SERVICE, and connection type (stream or datagram).
3073 If QLEN is an integer, it is used as the max. length of the server's
3074 pending connection queue (also known as the backlog); the default
3075 queue length is 5. Default is to create a client process.
3077 The following network options can be specified for this connection:
3079 :broadcast BOOL -- Allow send and receive of datagram broadcasts.
3080 :dontroute BOOL -- Only send to directly connected hosts.
3081 :keepalive BOOL -- Send keep-alive messages on network stream.
3082 :linger BOOL or TIMEOUT -- Send queued messages before closing.
3083 :oobinline BOOL -- Place out-of-band data in receive data stream.
3084 :priority INT -- Set protocol defined priority for sent packets.
3085 :reuseaddr BOOL -- Allow reusing a recently used local address
3086 (this is allowed by default for a server process).
3087 :bindtodevice NAME -- bind to interface NAME. Using this may require
3088 special privileges on some systems.
3090 Consult the relevant system programmer's manual pages for more
3091 information on using these options.
3094 A server process will listen for and accept connections from clients.
3095 When a client connection is accepted, a new network process is created
3096 for the connection with the following parameters:
3098 - The client's process name is constructed by concatenating the server
3099 process' NAME and a client identification string.
3100 - If the FILTER argument is non-nil, the client process will not get a
3101 separate process buffer; otherwise, the client's process buffer is a newly
3102 created buffer named after the server process' BUFFER name or process
3103 NAME concatenated with the client identification string.
3104 - The connection type and the process filter and sentinel parameters are
3105 inherited from the server process' TYPE, FILTER and SENTINEL.
3106 - The client process' contact info is set according to the client's
3107 addressing information (typically an IP address and a port number).
3108 - The client process' plist is initialized from the server's plist.
3110 Notice that the FILTER and SENTINEL args are never used directly by
3111 the server process. Also, the BUFFER argument is not used directly by
3112 the server process, but via the optional :log function, accepted (and
3113 failed) connections may be logged in the server process' buffer.
3115 The original argument list, modified with the actual connection
3116 information, is available via the `process-contact' function.
3118 usage: (make-network-process &rest ARGS) */)
3124 Lisp_Object contact
;
3125 struct Lisp_Process
*p
;
3126 #ifdef HAVE_GETADDRINFO
3127 struct addrinfo ai
, *res
, *lres
;
3128 struct addrinfo hints
;
3129 char *portstring
, portbuf
[128];
3130 #else /* HAVE_GETADDRINFO */
3131 struct _emacs_addrinfo
3137 struct sockaddr
*ai_addr
;
3138 struct _emacs_addrinfo
*ai_next
;
3140 #endif /* HAVE_GETADDRINFO */
3141 struct sockaddr_in address_in
;
3142 #ifdef HAVE_LOCAL_SOCKETS
3143 struct sockaddr_un address_un
;
3148 int s
= -1, outch
, inch
;
3149 struct gcpro gcpro1
;
3150 int count
= SPECPDL_INDEX ();
3152 Lisp_Object QCaddress
; /* one of QClocal or QCremote */
3154 Lisp_Object name
, buffer
, host
, service
, address
;
3155 Lisp_Object filter
, sentinel
;
3156 int is_non_blocking_client
= 0;
3157 int is_server
= 0, backlog
= 5;
3164 /* Save arguments for process-contact and clone-process. */
3165 contact
= Flist (nargs
, args
);
3169 /* Ensure socket support is loaded if available. */
3170 init_winsock (TRUE
);
3173 /* :type TYPE (nil: stream, datagram */
3174 tem
= Fplist_get (contact
, QCtype
);
3176 socktype
= SOCK_STREAM
;
3177 #ifdef DATAGRAM_SOCKETS
3178 else if (EQ (tem
, Qdatagram
))
3179 socktype
= SOCK_DGRAM
;
3182 error ("Unsupported connection type");
3185 tem
= Fplist_get (contact
, QCserver
);
3188 /* Don't support network sockets when non-blocking mode is
3189 not available, since a blocked Emacs is not useful. */
3190 #if !defined(O_NONBLOCK) && !defined(O_NDELAY)
3191 error ("Network servers not supported");
3195 backlog
= XINT (tem
);
3199 /* Make QCaddress an alias for :local (server) or :remote (client). */
3200 QCaddress
= is_server
? QClocal
: QCremote
;
3203 if (!is_server
&& socktype
== SOCK_STREAM
3204 && (tem
= Fplist_get (contact
, QCnowait
), !NILP (tem
)))
3206 #ifndef NON_BLOCKING_CONNECT
3207 error ("Non-blocking connect not supported");
3209 is_non_blocking_client
= 1;
3213 name
= Fplist_get (contact
, QCname
);
3214 buffer
= Fplist_get (contact
, QCbuffer
);
3215 filter
= Fplist_get (contact
, QCfilter
);
3216 sentinel
= Fplist_get (contact
, QCsentinel
);
3218 CHECK_STRING (name
);
3220 /* Initialize addrinfo structure in case we don't use getaddrinfo. */
3221 ai
.ai_socktype
= socktype
;
3226 /* :local ADDRESS or :remote ADDRESS */
3227 address
= Fplist_get (contact
, QCaddress
);
3228 if (!NILP (address
))
3230 host
= service
= Qnil
;
3232 if (!(ai
.ai_addrlen
= get_lisp_to_sockaddr_size (address
, &family
)))
3233 error ("Malformed :address");
3234 ai
.ai_family
= family
;
3235 ai
.ai_addr
= alloca (ai
.ai_addrlen
);
3236 conv_lisp_to_sockaddr (family
, address
, ai
.ai_addr
, ai
.ai_addrlen
);
3240 /* :family FAMILY -- nil (for Inet), local, or integer. */
3241 tem
= Fplist_get (contact
, QCfamily
);
3244 #if defined(HAVE_GETADDRINFO) && defined(AF_INET6)
3250 #ifdef HAVE_LOCAL_SOCKETS
3251 else if (EQ (tem
, Qlocal
))
3255 else if (EQ (tem
, Qipv6
))
3258 else if (EQ (tem
, Qipv4
))
3260 else if (INTEGERP (tem
))
3261 family
= XINT (tem
);
3263 error ("Unknown address family");
3265 ai
.ai_family
= family
;
3267 /* :service SERVICE -- string, integer (port number), or t (random port). */
3268 service
= Fplist_get (contact
, QCservice
);
3270 #ifdef HAVE_LOCAL_SOCKETS
3271 if (family
== AF_LOCAL
)
3273 /* Host is not used. */
3275 CHECK_STRING (service
);
3276 bzero (&address_un
, sizeof address_un
);
3277 address_un
.sun_family
= AF_LOCAL
;
3278 strncpy (address_un
.sun_path
, SDATA (service
), sizeof address_un
.sun_path
);
3279 ai
.ai_addr
= (struct sockaddr
*) &address_un
;
3280 ai
.ai_addrlen
= sizeof address_un
;
3285 /* :host HOST -- hostname, ip address, or 'local for localhost. */
3286 host
= Fplist_get (contact
, QChost
);
3289 if (EQ (host
, Qlocal
))
3290 host
= build_string ("localhost");
3291 CHECK_STRING (host
);
3294 /* Slow down polling to every ten seconds.
3295 Some kernels have a bug which causes retrying connect to fail
3296 after a connect. Polling can interfere with gethostbyname too. */
3297 #ifdef POLL_FOR_INPUT
3298 if (socktype
== SOCK_STREAM
)
3300 record_unwind_protect (unwind_stop_other_atimers
, Qnil
);
3301 bind_polling_period (10);
3305 #ifdef HAVE_GETADDRINFO
3306 /* If we have a host, use getaddrinfo to resolve both host and service.
3307 Otherwise, use getservbyname to lookup the service. */
3311 /* SERVICE can either be a string or int.
3312 Convert to a C string for later use by getaddrinfo. */
3313 if (EQ (service
, Qt
))
3315 else if (INTEGERP (service
))
3317 sprintf (portbuf
, "%ld", (long) XINT (service
));
3318 portstring
= portbuf
;
3322 CHECK_STRING (service
);
3323 portstring
= SDATA (service
);
3328 memset (&hints
, 0, sizeof (hints
));
3330 hints
.ai_family
= family
;
3331 hints
.ai_socktype
= socktype
;
3332 hints
.ai_protocol
= 0;
3334 #ifdef HAVE_RES_INIT
3338 ret
= getaddrinfo (SDATA (host
), portstring
, &hints
, &res
);
3340 #ifdef HAVE_GAI_STRERROR
3341 error ("%s/%s %s", SDATA (host
), portstring
, gai_strerror(ret
));
3343 error ("%s/%s getaddrinfo error %d", SDATA (host
), portstring
, ret
);
3349 #endif /* HAVE_GETADDRINFO */
3351 /* We end up here if getaddrinfo is not defined, or in case no hostname
3352 has been specified (e.g. for a local server process). */
3354 if (EQ (service
, Qt
))
3356 else if (INTEGERP (service
))
3357 port
= htons ((unsigned short) XINT (service
));
3360 struct servent
*svc_info
;
3361 CHECK_STRING (service
);
3362 svc_info
= getservbyname (SDATA (service
),
3363 (socktype
== SOCK_DGRAM
? "udp" : "tcp"));
3365 error ("Unknown service: %s", SDATA (service
));
3366 port
= svc_info
->s_port
;
3369 bzero (&address_in
, sizeof address_in
);
3370 address_in
.sin_family
= family
;
3371 address_in
.sin_addr
.s_addr
= INADDR_ANY
;
3372 address_in
.sin_port
= port
;
3374 #ifndef HAVE_GETADDRINFO
3377 struct hostent
*host_info_ptr
;
3379 /* gethostbyname may fail with TRY_AGAIN, but we don't honour that,
3380 as it may `hang' Emacs for a very long time. */
3384 #ifdef HAVE_RES_INIT
3388 host_info_ptr
= gethostbyname (SDATA (host
));
3393 bcopy (host_info_ptr
->h_addr
, (char *) &address_in
.sin_addr
,
3394 host_info_ptr
->h_length
);
3395 family
= host_info_ptr
->h_addrtype
;
3396 address_in
.sin_family
= family
;
3399 /* Attempt to interpret host as numeric inet address */
3401 unsigned long numeric_addr
;
3402 numeric_addr
= inet_addr ((char *) SDATA (host
));
3403 if (numeric_addr
== -1)
3404 error ("Unknown host \"%s\"", SDATA (host
));
3406 bcopy ((char *)&numeric_addr
, (char *) &address_in
.sin_addr
,
3407 sizeof (address_in
.sin_addr
));
3411 #endif /* not HAVE_GETADDRINFO */
3413 ai
.ai_family
= family
;
3414 ai
.ai_addr
= (struct sockaddr
*) &address_in
;
3415 ai
.ai_addrlen
= sizeof address_in
;
3419 /* Do this in case we never enter the for-loop below. */
3420 count1
= SPECPDL_INDEX ();
3423 for (lres
= res
; lres
; lres
= lres
->ai_next
)
3429 s
= socket (lres
->ai_family
, lres
->ai_socktype
, lres
->ai_protocol
);
3436 #ifdef DATAGRAM_SOCKETS
3437 if (!is_server
&& socktype
== SOCK_DGRAM
)
3439 #endif /* DATAGRAM_SOCKETS */
3441 #ifdef NON_BLOCKING_CONNECT
3442 if (is_non_blocking_client
)
3445 ret
= fcntl (s
, F_SETFL
, O_NONBLOCK
);
3447 ret
= fcntl (s
, F_SETFL
, O_NDELAY
);
3459 /* Make us close S if quit. */
3460 record_unwind_protect (close_file_unwind
, make_number (s
));
3462 /* Parse network options in the arg list.
3463 We simply ignore anything which isn't a known option (including other keywords).
3464 An error is signaled if setting a known option fails. */
3465 for (optn
= optbits
= 0; optn
< nargs
-1; optn
+= 2)
3466 optbits
|= set_socket_option (s
, args
[optn
], args
[optn
+1]);
3470 /* Configure as a server socket. */
3472 /* SO_REUSEADDR = 1 is default for server sockets; must specify
3473 explicit :reuseaddr key to override this. */
3474 #ifdef HAVE_LOCAL_SOCKETS
3475 if (family
!= AF_LOCAL
)
3477 if (!(optbits
& (1 << OPIX_REUSEADDR
)))
3480 if (setsockopt (s
, SOL_SOCKET
, SO_REUSEADDR
, &optval
, sizeof optval
))
3481 report_file_error ("Cannot set reuse option on server socket", Qnil
);
3484 if (bind (s
, lres
->ai_addr
, lres
->ai_addrlen
))
3485 report_file_error ("Cannot bind server socket", Qnil
);
3487 #ifdef HAVE_GETSOCKNAME
3488 if (EQ (service
, Qt
))
3490 struct sockaddr_in sa1
;
3491 int len1
= sizeof (sa1
);
3492 if (getsockname (s
, (struct sockaddr
*)&sa1
, &len1
) == 0)
3494 ((struct sockaddr_in
*)(lres
->ai_addr
))->sin_port
= sa1
.sin_port
;
3495 service
= make_number (ntohs (sa1
.sin_port
));
3496 contact
= Fplist_put (contact
, QCservice
, service
);
3501 if (socktype
== SOCK_STREAM
&& listen (s
, backlog
))
3502 report_file_error ("Cannot listen on server socket", Qnil
);
3510 /* This turns off all alarm-based interrupts; the
3511 bind_polling_period call above doesn't always turn all the
3512 short-interval ones off, especially if interrupt_input is
3515 It'd be nice to be able to control the connect timeout
3516 though. Would non-blocking connect calls be portable?
3518 This used to be conditioned by HAVE_GETADDRINFO. Why? */
3520 turn_on_atimers (0);
3522 ret
= connect (s
, lres
->ai_addr
, lres
->ai_addrlen
);
3525 turn_on_atimers (1);
3527 if (ret
== 0 || xerrno
== EISCONN
)
3529 /* The unwind-protect will be discarded afterwards.
3530 Likewise for immediate_quit. */
3534 #ifdef NON_BLOCKING_CONNECT
3536 if (is_non_blocking_client
&& xerrno
== EINPROGRESS
)
3540 if (is_non_blocking_client
&& xerrno
== EWOULDBLOCK
)
3548 /* Discard the unwind protect closing S. */
3549 specpdl_ptr
= specpdl
+ count1
;
3553 if (xerrno
== EINTR
)
3559 #ifdef DATAGRAM_SOCKETS
3560 if (socktype
== SOCK_DGRAM
)
3562 if (datagram_address
[s
].sa
)
3564 datagram_address
[s
].sa
= (struct sockaddr
*) xmalloc (lres
->ai_addrlen
);
3565 datagram_address
[s
].len
= lres
->ai_addrlen
;
3569 bzero (datagram_address
[s
].sa
, lres
->ai_addrlen
);
3570 if (remote
= Fplist_get (contact
, QCremote
), !NILP (remote
))
3573 rlen
= get_lisp_to_sockaddr_size (remote
, &rfamily
);
3574 if (rfamily
== lres
->ai_family
&& rlen
== lres
->ai_addrlen
)
3575 conv_lisp_to_sockaddr (rfamily
, remote
,
3576 datagram_address
[s
].sa
, rlen
);
3580 bcopy (lres
->ai_addr
, datagram_address
[s
].sa
, lres
->ai_addrlen
);
3583 contact
= Fplist_put (contact
, QCaddress
,
3584 conv_sockaddr_to_lisp (lres
->ai_addr
, lres
->ai_addrlen
));
3585 #ifdef HAVE_GETSOCKNAME
3588 struct sockaddr_in sa1
;
3589 int len1
= sizeof (sa1
);
3590 if (getsockname (s
, (struct sockaddr
*)&sa1
, &len1
) == 0)
3591 contact
= Fplist_put (contact
, QClocal
,
3592 conv_sockaddr_to_lisp (&sa1
, len1
));
3599 #ifdef HAVE_GETADDRINFO
3608 /* Discard the unwind protect for closing S, if any. */
3609 specpdl_ptr
= specpdl
+ count1
;
3611 /* Unwind bind_polling_period and request_sigio. */
3612 unbind_to (count
, Qnil
);
3616 /* If non-blocking got this far - and failed - assume non-blocking is
3617 not supported after all. This is probably a wrong assumption, but
3618 the normal blocking calls to open-network-stream handles this error
3620 if (is_non_blocking_client
)
3625 report_file_error ("make server process failed", contact
);
3627 report_file_error ("make client process failed", contact
);
3634 buffer
= Fget_buffer_create (buffer
);
3635 proc
= make_process (name
);
3637 chan_process
[inch
] = proc
;
3640 fcntl (inch
, F_SETFL
, O_NONBLOCK
);
3643 fcntl (inch
, F_SETFL
, O_NDELAY
);
3647 p
= XPROCESS (proc
);
3649 p
->childp
= contact
;
3650 p
->plist
= Fcopy_sequence (Fplist_get (contact
, QCplist
));
3654 p
->sentinel
= sentinel
;
3656 p
->log
= Fplist_get (contact
, QClog
);
3657 if (tem
= Fplist_get (contact
, QCnoquery
), !NILP (tem
))
3658 p
->kill_without_query
= 1;
3659 if ((tem
= Fplist_get (contact
, QCstop
), !NILP (tem
)))
3664 if (is_server
&& socktype
== SOCK_STREAM
)
3665 p
->status
= Qlisten
;
3667 /* Make the process marker point into the process buffer (if any). */
3668 if (BUFFERP (buffer
))
3669 set_marker_both (p
->mark
, buffer
,
3670 BUF_ZV (XBUFFER (buffer
)),
3671 BUF_ZV_BYTE (XBUFFER (buffer
)));
3673 #ifdef NON_BLOCKING_CONNECT
3674 if (is_non_blocking_client
)
3676 /* We may get here if connect did succeed immediately. However,
3677 in that case, we still need to signal this like a non-blocking
3679 p
->status
= Qconnect
;
3680 if (!FD_ISSET (inch
, &connect_wait_mask
))
3682 FD_SET (inch
, &connect_wait_mask
);
3683 num_pending_connects
++;
3688 /* A server may have a client filter setting of Qt, but it must
3689 still listen for incoming connects unless it is stopped. */
3690 if ((!EQ (p
->filter
, Qt
) && !EQ (p
->command
, Qt
))
3691 || (EQ (p
->status
, Qlisten
) && NILP (p
->command
)))
3693 FD_SET (inch
, &input_wait_mask
);
3694 FD_SET (inch
, &non_keyboard_wait_mask
);
3697 if (inch
> max_process_desc
)
3698 max_process_desc
= inch
;
3700 tem
= Fplist_member (contact
, QCcoding
);
3701 if (!NILP (tem
) && (!CONSP (tem
) || !CONSP (XCDR (tem
))))
3702 tem
= Qnil
; /* No error message (too late!). */
3705 /* Setup coding systems for communicating with the network stream. */
3706 struct gcpro gcpro1
;
3707 /* Qt denotes we have not yet called Ffind_operation_coding_system. */
3708 Lisp_Object coding_systems
= Qt
;
3709 Lisp_Object args
[5], val
;
3713 val
= XCAR (XCDR (tem
));
3717 else if (!NILP (Vcoding_system_for_read
))
3718 val
= Vcoding_system_for_read
;
3719 else if ((!NILP (buffer
) && NILP (XBUFFER (buffer
)->enable_multibyte_characters
))
3720 || (NILP (buffer
) && NILP (buffer_defaults
.enable_multibyte_characters
)))
3721 /* We dare not decode end-of-line format by setting VAL to
3722 Qraw_text, because the existing Emacs Lisp libraries
3723 assume that they receive bare code including a sequene of
3728 if (NILP (host
) || NILP (service
))
3729 coding_systems
= Qnil
;
3732 args
[0] = Qopen_network_stream
, args
[1] = name
,
3733 args
[2] = buffer
, args
[3] = host
, args
[4] = service
;
3735 coding_systems
= Ffind_operation_coding_system (5, args
);
3738 if (CONSP (coding_systems
))
3739 val
= XCAR (coding_systems
);
3740 else if (CONSP (Vdefault_process_coding_system
))
3741 val
= XCAR (Vdefault_process_coding_system
);
3745 p
->decode_coding_system
= val
;
3749 val
= XCAR (XCDR (tem
));
3753 else if (!NILP (Vcoding_system_for_write
))
3754 val
= Vcoding_system_for_write
;
3755 else if (NILP (current_buffer
->enable_multibyte_characters
))
3759 if (EQ (coding_systems
, Qt
))
3761 if (NILP (host
) || NILP (service
))
3762 coding_systems
= Qnil
;
3765 args
[0] = Qopen_network_stream
, args
[1] = name
,
3766 args
[2] = buffer
, args
[3] = host
, args
[4] = service
;
3768 coding_systems
= Ffind_operation_coding_system (5, args
);
3772 if (CONSP (coding_systems
))
3773 val
= XCDR (coding_systems
);
3774 else if (CONSP (Vdefault_process_coding_system
))
3775 val
= XCDR (Vdefault_process_coding_system
);
3779 p
->encode_coding_system
= val
;
3781 setup_process_coding_systems (proc
);
3783 p
->decoding_buf
= make_uninit_string (0);
3784 p
->decoding_carryover
= 0;
3785 p
->encoding_buf
= make_uninit_string (0);
3787 p
->inherit_coding_system_flag
3788 = !(!NILP (tem
) || NILP (buffer
) || !inherit_process_coding_system
);
3793 #endif /* HAVE_SOCKETS */
3796 #if defined(HAVE_SOCKETS) && defined(HAVE_NET_IF_H) && defined(HAVE_SYS_IOCTL_H)
3799 DEFUN ("network-interface-list", Fnetwork_interface_list
, Snetwork_interface_list
, 0, 0, 0,
3800 doc
: /* Return an alist of all network interfaces and their network address.
3801 Each element is a cons, the car of which is a string containing the
3802 interface name, and the cdr is the network address in internal
3803 format; see the description of ADDRESS in `make-network-process'. */)
3806 struct ifconf ifconf
;
3807 struct ifreq
*ifreqs
= NULL
;
3812 s
= socket (AF_INET
, SOCK_STREAM
, 0);
3818 buf_size
= ifaces
* sizeof(ifreqs
[0]);
3819 ifreqs
= (struct ifreq
*)xrealloc(ifreqs
, buf_size
);
3826 ifconf
.ifc_len
= buf_size
;
3827 ifconf
.ifc_req
= ifreqs
;
3828 if (ioctl (s
, SIOCGIFCONF
, &ifconf
))
3834 if (ifconf
.ifc_len
== buf_size
)
3838 ifaces
= ifconf
.ifc_len
/ sizeof (ifreqs
[0]);
3841 while (--ifaces
>= 0)
3843 struct ifreq
*ifq
= &ifreqs
[ifaces
];
3844 char namebuf
[sizeof (ifq
->ifr_name
) + 1];
3845 if (ifq
->ifr_addr
.sa_family
!= AF_INET
)
3847 bcopy (ifq
->ifr_name
, namebuf
, sizeof (ifq
->ifr_name
));
3848 namebuf
[sizeof (ifq
->ifr_name
)] = 0;
3849 res
= Fcons (Fcons (build_string (namebuf
),
3850 conv_sockaddr_to_lisp (&ifq
->ifr_addr
,
3851 sizeof (struct sockaddr
))),
3857 #endif /* SIOCGIFCONF */
3859 #if defined(SIOCGIFADDR) || defined(SIOCGIFHWADDR) || defined(SIOCGIFFLAGS)
3866 static struct ifflag_def ifflag_table
[] = {
3870 #ifdef IFF_BROADCAST
3871 { IFF_BROADCAST
, "broadcast" },
3874 { IFF_DEBUG
, "debug" },
3877 { IFF_LOOPBACK
, "loopback" },
3879 #ifdef IFF_POINTOPOINT
3880 { IFF_POINTOPOINT
, "pointopoint" },
3883 { IFF_RUNNING
, "running" },
3886 { IFF_NOARP
, "noarp" },
3889 { IFF_PROMISC
, "promisc" },
3891 #ifdef IFF_NOTRAILERS
3892 { IFF_NOTRAILERS
, "notrailers" },
3895 { IFF_ALLMULTI
, "allmulti" },
3898 { IFF_MASTER
, "master" },
3901 { IFF_SLAVE
, "slave" },
3903 #ifdef IFF_MULTICAST
3904 { IFF_MULTICAST
, "multicast" },
3907 { IFF_PORTSEL
, "portsel" },
3909 #ifdef IFF_AUTOMEDIA
3910 { IFF_AUTOMEDIA
, "automedia" },
3913 { IFF_DYNAMIC
, "dynamic" },
3916 { IFF_OACTIVE
, "oactive" }, /* OpenBSD: transmission in progress */
3919 { IFF_SIMPLEX
, "simplex" }, /* OpenBSD: can't hear own transmissions */
3922 { IFF_LINK0
, "link0" }, /* OpenBSD: per link layer defined bit */
3925 { IFF_LINK1
, "link1" }, /* OpenBSD: per link layer defined bit */
3928 { IFF_LINK2
, "link2" }, /* OpenBSD: per link layer defined bit */
3933 DEFUN ("network-interface-info", Fnetwork_interface_info
, Snetwork_interface_info
, 1, 1, 0,
3934 doc
: /* Return information about network interface named IFNAME.
3935 The return value is a list (ADDR BCAST NETMASK HWADDR FLAGS),
3936 where ADDR is the layer 3 address, BCAST is the layer 3 broadcast address,
3937 NETMASK is the layer 3 network mask, HWADDR is the layer 2 addres, and
3938 FLAGS is the current flags of the interface. */)
3943 Lisp_Object res
= Qnil
;
3948 CHECK_STRING (ifname
);
3950 bzero (rq
.ifr_name
, sizeof rq
.ifr_name
);
3951 strncpy (rq
.ifr_name
, SDATA (ifname
), sizeof (rq
.ifr_name
));
3953 s
= socket (AF_INET
, SOCK_STREAM
, 0);
3958 #if defined(SIOCGIFFLAGS) && defined(HAVE_STRUCT_IFREQ_IFR_FLAGS)
3959 if (ioctl (s
, SIOCGIFFLAGS
, &rq
) == 0)
3961 int flags
= rq
.ifr_flags
;
3962 struct ifflag_def
*fp
;
3966 for (fp
= ifflag_table
; flags
!= 0 && fp
->flag_sym
; fp
++)
3968 if (flags
& fp
->flag_bit
)
3970 elt
= Fcons (intern (fp
->flag_sym
), elt
);
3971 flags
-= fp
->flag_bit
;
3974 for (fnum
= 0; flags
&& fnum
< 32; fnum
++)
3976 if (flags
& (1 << fnum
))
3978 elt
= Fcons (make_number (fnum
), elt
);
3983 res
= Fcons (elt
, res
);
3986 #if defined(SIOCGIFHWADDR) && defined(HAVE_STRUCT_IFREQ_IFR_HWADDR)
3987 if (ioctl (s
, SIOCGIFHWADDR
, &rq
) == 0)
3989 Lisp_Object hwaddr
= Fmake_vector (make_number (6), Qnil
);
3990 register struct Lisp_Vector
*p
= XVECTOR (hwaddr
);
3994 for (n
= 0; n
< 6; n
++)
3995 p
->contents
[n
] = make_number (((unsigned char *)&rq
.ifr_hwaddr
.sa_data
[0])[n
]);
3996 elt
= Fcons (make_number (rq
.ifr_hwaddr
.sa_family
), hwaddr
);
3999 res
= Fcons (elt
, res
);
4002 #if defined(SIOCGIFNETMASK) && (defined(HAVE_STRUCT_IFREQ_IFR_NETMASK) || defined(HAVE_STRUCT_IFREQ_IFR_ADDR))
4003 if (ioctl (s
, SIOCGIFNETMASK
, &rq
) == 0)
4006 #ifdef HAVE_STRUCT_IFREQ_IFR_NETMASK
4007 elt
= conv_sockaddr_to_lisp (&rq
.ifr_netmask
, sizeof (rq
.ifr_netmask
));
4009 elt
= conv_sockaddr_to_lisp (&rq
.ifr_addr
, sizeof (rq
.ifr_addr
));
4013 res
= Fcons (elt
, res
);
4016 #if defined(SIOCGIFBRDADDR) && defined(HAVE_STRUCT_IFREQ_IFR_BROADADDR)
4017 if (ioctl (s
, SIOCGIFBRDADDR
, &rq
) == 0)
4020 elt
= conv_sockaddr_to_lisp (&rq
.ifr_broadaddr
, sizeof (rq
.ifr_broadaddr
));
4023 res
= Fcons (elt
, res
);
4026 #if defined(SIOCGIFADDR) && defined(HAVE_STRUCT_IFREQ_IFR_ADDR)
4027 if (ioctl (s
, SIOCGIFADDR
, &rq
) == 0)
4030 elt
= conv_sockaddr_to_lisp (&rq
.ifr_addr
, sizeof (rq
.ifr_addr
));
4033 res
= Fcons (elt
, res
);
4037 return any
? res
: Qnil
;
4040 #endif /* HAVE_SOCKETS */
4042 /* Turn off input and output for process PROC. */
4045 deactivate_process (proc
)
4048 register int inchannel
, outchannel
;
4049 register struct Lisp_Process
*p
= XPROCESS (proc
);
4051 inchannel
= p
->infd
;
4052 outchannel
= p
->outfd
;
4054 #ifdef ADAPTIVE_READ_BUFFERING
4055 if (p
->read_output_delay
> 0)
4057 if (--process_output_delay_count
< 0)
4058 process_output_delay_count
= 0;
4059 p
->read_output_delay
= 0;
4060 p
->read_output_skip
= 0;
4066 /* Beware SIGCHLD hereabouts. */
4067 flush_pending_output (inchannel
);
4068 emacs_close (inchannel
);
4069 if (outchannel
>= 0 && outchannel
!= inchannel
)
4070 emacs_close (outchannel
);
4074 #ifdef DATAGRAM_SOCKETS
4075 if (DATAGRAM_CHAN_P (inchannel
))
4077 xfree (datagram_address
[inchannel
].sa
);
4078 datagram_address
[inchannel
].sa
= 0;
4079 datagram_address
[inchannel
].len
= 0;
4082 chan_process
[inchannel
] = Qnil
;
4083 FD_CLR (inchannel
, &input_wait_mask
);
4084 FD_CLR (inchannel
, &non_keyboard_wait_mask
);
4085 #ifdef NON_BLOCKING_CONNECT
4086 if (FD_ISSET (inchannel
, &connect_wait_mask
))
4088 FD_CLR (inchannel
, &connect_wait_mask
);
4089 if (--num_pending_connects
< 0)
4093 if (inchannel
== max_process_desc
)
4096 /* We just closed the highest-numbered process input descriptor,
4097 so recompute the highest-numbered one now. */
4098 max_process_desc
= 0;
4099 for (i
= 0; i
< MAXDESC
; i
++)
4100 if (!NILP (chan_process
[i
]))
4101 max_process_desc
= i
;
4106 /* Close all descriptors currently in use for communication
4107 with subprocess. This is used in a newly-forked subprocess
4108 to get rid of irrelevant descriptors. */
4111 close_process_descs ()
4115 for (i
= 0; i
< MAXDESC
; i
++)
4117 Lisp_Object process
;
4118 process
= chan_process
[i
];
4119 if (!NILP (process
))
4121 int in
= XPROCESS (process
)->infd
;
4122 int out
= XPROCESS (process
)->outfd
;
4125 if (out
>= 0 && in
!= out
)
4132 DEFUN ("accept-process-output", Faccept_process_output
, Saccept_process_output
,
4134 doc
: /* Allow any pending output from subprocesses to be read by Emacs.
4135 It is read into the process' buffers or given to their filter functions.
4136 Non-nil arg PROCESS means do not return until some output has been received
4139 Non-nil second arg SECONDS and third arg MILLISEC are number of
4140 seconds and milliseconds to wait; return after that much time whether
4141 or not there is input. If SECONDS is a floating point number,
4142 it specifies a fractional number of seconds to wait.
4143 The MILLISEC argument is obsolete and should be avoided.
4145 If optional fourth arg JUST-THIS-ONE is non-nil, only accept output
4146 from PROCESS, suspending reading output from other processes.
4147 If JUST-THIS-ONE is an integer, don't run any timers either.
4148 Return non-nil if we received any output before the timeout expired. */)
4149 (process
, seconds
, millisec
, just_this_one
)
4150 register Lisp_Object process
, seconds
, millisec
, just_this_one
;
4152 int secs
, usecs
= 0;
4154 if (! NILP (process
))
4155 CHECK_PROCESS (process
);
4157 just_this_one
= Qnil
;
4159 if (!NILP (millisec
))
4160 { /* Obsolete calling convention using integers rather than floats. */
4161 CHECK_NUMBER (millisec
);
4163 seconds
= make_float (XINT (millisec
) / 1000.0);
4166 CHECK_NUMBER (seconds
);
4167 seconds
= make_float (XINT (millisec
) / 1000.0 + XINT (seconds
));
4171 if (!NILP (seconds
))
4173 if (INTEGERP (seconds
))
4174 secs
= XINT (seconds
);
4175 else if (FLOATP (seconds
))
4177 double timeout
= XFLOAT_DATA (seconds
);
4178 secs
= (int) timeout
;
4179 usecs
= (int) ((timeout
- (double) secs
) * 1000000);
4182 wrong_type_argument (Qnumberp
, seconds
);
4184 if (secs
< 0 || (secs
== 0 && usecs
== 0))
4185 secs
= -1, usecs
= 0;
4188 secs
= NILP (process
) ? -1 : 0;
4191 (wait_reading_process_output (secs
, usecs
, 0, 0,
4193 !NILP (process
) ? XPROCESS (process
) : NULL
,
4194 NILP (just_this_one
) ? 0 :
4195 !INTEGERP (just_this_one
) ? 1 : -1)
4199 /* Accept a connection for server process SERVER on CHANNEL. */
4201 static int connect_counter
= 0;
4204 server_accept_connection (server
, channel
)
4208 Lisp_Object proc
, caller
, name
, buffer
;
4209 Lisp_Object contact
, host
, service
;
4210 struct Lisp_Process
*ps
= XPROCESS (server
);
4211 struct Lisp_Process
*p
;
4215 struct sockaddr_in in
;
4217 struct sockaddr_in6 in6
;
4219 #ifdef HAVE_LOCAL_SOCKETS
4220 struct sockaddr_un un
;
4223 int len
= sizeof saddr
;
4225 s
= accept (channel
, &saddr
.sa
, &len
);
4234 if (code
== EWOULDBLOCK
)
4238 if (!NILP (ps
->log
))
4239 call3 (ps
->log
, server
, Qnil
,
4240 concat3 (build_string ("accept failed with code"),
4241 Fnumber_to_string (make_number (code
)),
4242 build_string ("\n")));
4248 /* Setup a new process to handle the connection. */
4250 /* Generate a unique identification of the caller, and build contact
4251 information for this process. */
4254 switch (saddr
.sa
.sa_family
)
4258 Lisp_Object args
[5];
4259 unsigned char *ip
= (unsigned char *)&saddr
.in
.sin_addr
.s_addr
;
4260 args
[0] = build_string ("%d.%d.%d.%d");
4261 args
[1] = make_number (*ip
++);
4262 args
[2] = make_number (*ip
++);
4263 args
[3] = make_number (*ip
++);
4264 args
[4] = make_number (*ip
++);
4265 host
= Fformat (5, args
);
4266 service
= make_number (ntohs (saddr
.in
.sin_port
));
4268 args
[0] = build_string (" <%s:%d>");
4271 caller
= Fformat (3, args
);
4278 Lisp_Object args
[9];
4279 uint16_t *ip6
= (uint16_t *)&saddr
.in6
.sin6_addr
;
4281 args
[0] = build_string ("%x:%x:%x:%x:%x:%x:%x:%x");
4282 for (i
= 0; i
< 8; i
++)
4283 args
[i
+1] = make_number (ntohs(ip6
[i
]));
4284 host
= Fformat (9, args
);
4285 service
= make_number (ntohs (saddr
.in
.sin_port
));
4287 args
[0] = build_string (" <[%s]:%d>");
4290 caller
= Fformat (3, args
);
4295 #ifdef HAVE_LOCAL_SOCKETS
4299 caller
= Fnumber_to_string (make_number (connect_counter
));
4300 caller
= concat3 (build_string (" <"), caller
, build_string (">"));
4304 /* Create a new buffer name for this process if it doesn't have a
4305 filter. The new buffer name is based on the buffer name or
4306 process name of the server process concatenated with the caller
4309 if (!NILP (ps
->filter
) && !EQ (ps
->filter
, Qt
))
4313 buffer
= ps
->buffer
;
4315 buffer
= Fbuffer_name (buffer
);
4320 buffer
= concat2 (buffer
, caller
);
4321 buffer
= Fget_buffer_create (buffer
);
4325 /* Generate a unique name for the new server process. Combine the
4326 server process name with the caller identification. */
4328 name
= concat2 (ps
->name
, caller
);
4329 proc
= make_process (name
);
4331 chan_process
[s
] = proc
;
4334 fcntl (s
, F_SETFL
, O_NONBLOCK
);
4337 fcntl (s
, F_SETFL
, O_NDELAY
);
4341 p
= XPROCESS (proc
);
4343 /* Build new contact information for this setup. */
4344 contact
= Fcopy_sequence (ps
->childp
);
4345 contact
= Fplist_put (contact
, QCserver
, Qnil
);
4346 contact
= Fplist_put (contact
, QChost
, host
);
4347 if (!NILP (service
))
4348 contact
= Fplist_put (contact
, QCservice
, service
);
4349 contact
= Fplist_put (contact
, QCremote
,
4350 conv_sockaddr_to_lisp (&saddr
.sa
, len
));
4351 #ifdef HAVE_GETSOCKNAME
4353 if (getsockname (s
, &saddr
.sa
, &len
) == 0)
4354 contact
= Fplist_put (contact
, QClocal
,
4355 conv_sockaddr_to_lisp (&saddr
.sa
, len
));
4358 p
->childp
= contact
;
4359 p
->plist
= Fcopy_sequence (ps
->plist
);
4363 p
->sentinel
= ps
->sentinel
;
4364 p
->filter
= ps
->filter
;
4371 /* Client processes for accepted connections are not stopped initially. */
4372 if (!EQ (p
->filter
, Qt
))
4374 FD_SET (s
, &input_wait_mask
);
4375 FD_SET (s
, &non_keyboard_wait_mask
);
4378 if (s
> max_process_desc
)
4379 max_process_desc
= s
;
4381 /* Setup coding system for new process based on server process.
4382 This seems to be the proper thing to do, as the coding system
4383 of the new process should reflect the settings at the time the
4384 server socket was opened; not the current settings. */
4386 p
->decode_coding_system
= ps
->decode_coding_system
;
4387 p
->encode_coding_system
= ps
->encode_coding_system
;
4388 setup_process_coding_systems (proc
);
4390 p
->decoding_buf
= make_uninit_string (0);
4391 p
->decoding_carryover
= 0;
4392 p
->encoding_buf
= make_uninit_string (0);
4394 p
->inherit_coding_system_flag
4395 = (NILP (buffer
) ? 0 : ps
->inherit_coding_system_flag
);
4397 if (!NILP (ps
->log
))
4398 call3 (ps
->log
, server
, proc
,
4399 concat3 (build_string ("accept from "),
4400 (STRINGP (host
) ? host
: build_string ("-")),
4401 build_string ("\n")));
4403 if (!NILP (p
->sentinel
))
4404 exec_sentinel (proc
,
4405 concat3 (build_string ("open from "),
4406 (STRINGP (host
) ? host
: build_string ("-")),
4407 build_string ("\n")));
4410 /* This variable is different from waiting_for_input in keyboard.c.
4411 It is used to communicate to a lisp process-filter/sentinel (via the
4412 function Fwaiting_for_user_input_p below) whether Emacs was waiting
4413 for user-input when that process-filter was called.
4414 waiting_for_input cannot be used as that is by definition 0 when
4415 lisp code is being evalled.
4416 This is also used in record_asynch_buffer_change.
4417 For that purpose, this must be 0
4418 when not inside wait_reading_process_output. */
4419 static int waiting_for_user_input_p
;
4422 wait_reading_process_output_unwind (data
)
4425 waiting_for_user_input_p
= XINT (data
);
4429 /* This is here so breakpoints can be put on it. */
4431 wait_reading_process_output_1 ()
4435 /* Use a wrapper around select to work around a bug in gdb 5.3.
4436 Normally, the wrapper is optimzed away by inlining.
4438 If emacs is stopped inside select, the gdb backtrace doesn't
4439 show the function which called select, so it is practically
4440 impossible to step through wait_reading_process_output. */
4444 select_wrapper (n
, rfd
, wfd
, xfd
, tmo
)
4446 SELECT_TYPE
*rfd
, *wfd
, *xfd
;
4449 return select (n
, rfd
, wfd
, xfd
, tmo
);
4451 #define select select_wrapper
4454 /* Read and dispose of subprocess output while waiting for timeout to
4455 elapse and/or keyboard input to be available.
4458 timeout in seconds, or
4459 zero for no limit, or
4460 -1 means gobble data immediately available but don't wait for any.
4463 an additional duration to wait, measured in microseconds.
4464 If this is nonzero and time_limit is 0, then the timeout
4465 consists of MICROSECS only.
4467 READ_KBD is a lisp value:
4468 0 to ignore keyboard input, or
4469 1 to return when input is available, or
4470 -1 meaning caller will actually read the input, so don't throw to
4471 the quit handler, or
4473 DO_DISPLAY != 0 means redisplay should be done to show subprocess
4474 output that arrives.
4476 If WAIT_FOR_CELL is a cons cell, wait until its car is non-nil
4477 (and gobble terminal input into the buffer if any arrives).
4479 If WAIT_PROC is specified, wait until something arrives from that
4480 process. The return value is true if we read some input from
4483 If JUST_WAIT_PROC is non-nil, handle only output from WAIT_PROC
4484 (suspending output from other processes). A negative value
4485 means don't run any timers either.
4487 If WAIT_PROC is specified, then the function returns true if we
4488 received input from that process before the timeout elapsed.
4489 Otherwise, return true if we received input from any process. */
4492 wait_reading_process_output (time_limit
, microsecs
, read_kbd
, do_display
,
4493 wait_for_cell
, wait_proc
, just_wait_proc
)
4494 int time_limit
, microsecs
, read_kbd
, do_display
;
4495 Lisp_Object wait_for_cell
;
4496 struct Lisp_Process
*wait_proc
;
4499 register int channel
, nfds
;
4500 SELECT_TYPE Available
;
4501 #ifdef NON_BLOCKING_CONNECT
4502 SELECT_TYPE Connecting
;
4505 int check_delay
, no_avail
;
4508 EMACS_TIME timeout
, end_time
;
4509 int wait_channel
= -1;
4510 int got_some_input
= 0;
4511 int count
= SPECPDL_INDEX ();
4513 FD_ZERO (&Available
);
4514 #ifdef NON_BLOCKING_CONNECT
4515 FD_ZERO (&Connecting
);
4518 /* If wait_proc is a process to watch, set wait_channel accordingly. */
4519 if (wait_proc
!= NULL
)
4520 wait_channel
= wait_proc
->infd
;
4522 record_unwind_protect (wait_reading_process_output_unwind
,
4523 make_number (waiting_for_user_input_p
));
4524 waiting_for_user_input_p
= read_kbd
;
4526 /* Since we may need to wait several times,
4527 compute the absolute time to return at. */
4528 if (time_limit
|| microsecs
)
4530 EMACS_GET_TIME (end_time
);
4531 EMACS_SET_SECS_USECS (timeout
, time_limit
, microsecs
);
4532 EMACS_ADD_TIME (end_time
, end_time
, timeout
);
4537 int timeout_reduced_for_timers
= 0;
4539 /* If calling from keyboard input, do not quit
4540 since we want to return C-g as an input character.
4541 Otherwise, do pending quit if requested. */
4547 if (interrupt_input_pending
)
4548 handle_async_input ();
4549 if (pending_atimers
)
4550 do_pending_atimers ();
4554 /* Exit now if the cell we're waiting for became non-nil. */
4555 if (! NILP (wait_for_cell
) && ! NILP (XCAR (wait_for_cell
)))
4558 /* Compute time from now till when time limit is up */
4559 /* Exit if already run out */
4560 if (time_limit
== -1)
4562 /* -1 specified for timeout means
4563 gobble output available now
4564 but don't wait at all. */
4566 EMACS_SET_SECS_USECS (timeout
, 0, 0);
4568 else if (time_limit
|| microsecs
)
4570 EMACS_GET_TIME (timeout
);
4571 EMACS_SUB_TIME (timeout
, end_time
, timeout
);
4572 if (EMACS_TIME_NEG_P (timeout
))
4577 EMACS_SET_SECS_USECS (timeout
, 100000, 0);
4580 /* Normally we run timers here.
4581 But not if wait_for_cell; in those cases,
4582 the wait is supposed to be short,
4583 and those callers cannot handle running arbitrary Lisp code here. */
4584 if (NILP (wait_for_cell
)
4585 && just_wait_proc
>= 0)
4587 EMACS_TIME timer_delay
;
4591 int old_timers_run
= timers_run
;
4592 struct buffer
*old_buffer
= current_buffer
;
4593 Lisp_Object old_window
= selected_window
;
4595 timer_delay
= timer_check (1);
4597 /* If a timer has run, this might have changed buffers
4598 an alike. Make read_key_sequence aware of that. */
4599 if (timers_run
!= old_timers_run
4600 && (old_buffer
!= current_buffer
4601 || !EQ (old_window
, selected_window
))
4602 && waiting_for_user_input_p
== -1)
4603 record_asynch_buffer_change ();
4605 if (timers_run
!= old_timers_run
&& do_display
)
4606 /* We must retry, since a timer may have requeued itself
4607 and that could alter the time_delay. */
4608 redisplay_preserve_echo_area (9);
4612 while (!detect_input_pending ());
4614 /* If there is unread keyboard input, also return. */
4616 && requeued_events_pending_p ())
4619 if (! EMACS_TIME_NEG_P (timer_delay
) && time_limit
!= -1)
4621 EMACS_TIME difference
;
4622 EMACS_SUB_TIME (difference
, timer_delay
, timeout
);
4623 if (EMACS_TIME_NEG_P (difference
))
4625 timeout
= timer_delay
;
4626 timeout_reduced_for_timers
= 1;
4629 /* If time_limit is -1, we are not going to wait at all. */
4630 else if (time_limit
!= -1)
4632 /* This is so a breakpoint can be put here. */
4633 wait_reading_process_output_1 ();
4637 /* Cause C-g and alarm signals to take immediate action,
4638 and cause input available signals to zero out timeout.
4640 It is important that we do this before checking for process
4641 activity. If we get a SIGCHLD after the explicit checks for
4642 process activity, timeout is the only way we will know. */
4644 set_waiting_for_input (&timeout
);
4646 /* If status of something has changed, and no input is
4647 available, notify the user of the change right away. After
4648 this explicit check, we'll let the SIGCHLD handler zap
4649 timeout to get our attention. When Emacs is run
4650 interactively, only do this with a nonzero DO_DISPLAY
4651 argument, because status_notify triggers redisplay. */
4652 if (update_tick
!= process_tick
4653 && (do_display
|| noninteractive
))
4656 #ifdef NON_BLOCKING_CONNECT
4660 Atemp
= input_wait_mask
;
4661 IF_NON_BLOCKING_CONNECT (Ctemp
= connect_wait_mask
);
4663 EMACS_SET_SECS_USECS (timeout
, 0, 0);
4664 if ((select (max (max (max_process_desc
, max_keyboard_desc
),
4667 #ifdef NON_BLOCKING_CONNECT
4668 (num_pending_connects
> 0 ? &Ctemp
: (SELECT_TYPE
*)0),
4672 (SELECT_TYPE
*)0, &timeout
)
4675 /* It's okay for us to do this and then continue with
4676 the loop, since timeout has already been zeroed out. */
4677 clear_waiting_for_input ();
4678 status_notify (NULL
);
4682 /* Don't wait for output from a non-running process. Just
4683 read whatever data has already been received. */
4684 if (wait_proc
&& wait_proc
->raw_status_new
)
4685 update_status (wait_proc
);
4687 && ! EQ (wait_proc
->status
, Qrun
)
4688 && ! EQ (wait_proc
->status
, Qconnect
))
4690 int nread
, total_nread
= 0;
4692 clear_waiting_for_input ();
4693 XSETPROCESS (proc
, wait_proc
);
4695 /* Read data from the process, until we exhaust it. */
4696 while (wait_proc
->infd
>= 0)
4698 nread
= read_process_output (proc
, wait_proc
->infd
);
4705 total_nread
+= nread
;
4709 else if (nread
== -1 && EIO
== errno
)
4713 else if (nread
== -1 && EAGAIN
== errno
)
4717 else if (nread
== -1 && EWOULDBLOCK
== errno
)
4721 if (total_nread
> 0 && do_display
)
4722 redisplay_preserve_echo_area (10);
4727 /* Wait till there is something to do */
4729 if (wait_proc
&& just_wait_proc
)
4731 if (wait_proc
->infd
< 0) /* Terminated */
4733 FD_SET (wait_proc
->infd
, &Available
);
4735 IF_NON_BLOCKING_CONNECT (check_connect
= 0);
4737 else if (!NILP (wait_for_cell
))
4739 Available
= non_process_wait_mask
;
4741 IF_NON_BLOCKING_CONNECT (check_connect
= 0);
4746 Available
= non_keyboard_wait_mask
;
4748 Available
= input_wait_mask
;
4749 IF_NON_BLOCKING_CONNECT (check_connect
= (num_pending_connects
> 0));
4750 check_delay
= wait_channel
>= 0 ? 0 : process_output_delay_count
;
4753 /* If frame size has changed or the window is newly mapped,
4754 redisplay now, before we start to wait. There is a race
4755 condition here; if a SIGIO arrives between now and the select
4756 and indicates that a frame is trashed, the select may block
4757 displaying a trashed screen. */
4758 if (frame_garbaged
&& do_display
)
4760 clear_waiting_for_input ();
4761 redisplay_preserve_echo_area (11);
4763 set_waiting_for_input (&timeout
);
4767 if (read_kbd
&& detect_input_pending ())
4774 #ifdef NON_BLOCKING_CONNECT
4776 Connecting
= connect_wait_mask
;
4779 #ifdef ADAPTIVE_READ_BUFFERING
4780 /* Set the timeout for adaptive read buffering if any
4781 process has non-zero read_output_skip and non-zero
4782 read_output_delay, and we are not reading output for a
4783 specific wait_channel. It is not executed if
4784 Vprocess_adaptive_read_buffering is nil. */
4785 if (process_output_skip
&& check_delay
> 0)
4787 int usecs
= EMACS_USECS (timeout
);
4788 if (EMACS_SECS (timeout
) > 0 || usecs
> READ_OUTPUT_DELAY_MAX
)
4789 usecs
= READ_OUTPUT_DELAY_MAX
;
4790 for (channel
= 0; check_delay
> 0 && channel
<= max_process_desc
; channel
++)
4792 proc
= chan_process
[channel
];
4795 /* Find minimum non-zero read_output_delay among the
4796 processes with non-zero read_output_skip. */
4797 if (XPROCESS (proc
)->read_output_delay
> 0)
4800 if (!XPROCESS (proc
)->read_output_skip
)
4802 FD_CLR (channel
, &Available
);
4803 XPROCESS (proc
)->read_output_skip
= 0;
4804 if (XPROCESS (proc
)->read_output_delay
< usecs
)
4805 usecs
= XPROCESS (proc
)->read_output_delay
;
4808 EMACS_SET_SECS_USECS (timeout
, 0, usecs
);
4809 process_output_skip
= 0;
4817 (max (max (max_process_desc
, max_keyboard_desc
),
4820 #ifdef NON_BLOCKING_CONNECT
4821 (check_connect
? &Connecting
: (SELECT_TYPE
*)0),
4825 (SELECT_TYPE
*)0, &timeout
);
4830 /* Make C-g and alarm signals set flags again */
4831 clear_waiting_for_input ();
4833 /* If we woke up due to SIGWINCH, actually change size now. */
4834 do_pending_window_change (0);
4836 if (time_limit
&& nfds
== 0 && ! timeout_reduced_for_timers
)
4837 /* We wanted the full specified time, so return now. */
4841 if (xerrno
== EINTR
)
4843 else if (xerrno
== EBADF
)
4846 /* AIX doesn't handle PTY closure the same way BSD does. On AIX,
4847 the child's closure of the pts gives the parent a SIGHUP, and
4848 the ptc file descriptor is automatically closed,
4849 yielding EBADF here or at select() call above.
4850 So, SIGHUP is ignored (see def of PTY_TTY_NAME_SPRINTF
4851 in m/ibmrt-aix.h), and here we just ignore the select error.
4852 Cleanup occurs c/o status_notify after SIGCLD. */
4853 no_avail
= 1; /* Cannot depend on values returned */
4859 error ("select error: %s", emacs_strerror (xerrno
));
4864 FD_ZERO (&Available
);
4865 IF_NON_BLOCKING_CONNECT (check_connect
= 0);
4868 #if 0 /* When polling is used, interrupt_input is 0,
4869 so get_input_pending should read the input.
4870 So this should not be needed. */
4871 /* If we are using polling for input,
4872 and we see input available, make it get read now.
4873 Otherwise it might not actually get read for a second.
4874 And on hpux, since we turn off polling in wait_reading_process_output,
4875 it might never get read at all if we don't spend much time
4876 outside of wait_reading_process_output. */
4877 if (read_kbd
&& interrupt_input
4878 && keyboard_bit_set (&Available
)
4879 && input_polling_used ())
4880 kill (getpid (), SIGALRM
);
4883 /* Check for keyboard input */
4884 /* If there is any, return immediately
4885 to give it higher priority than subprocesses */
4889 int old_timers_run
= timers_run
;
4890 struct buffer
*old_buffer
= current_buffer
;
4891 Lisp_Object old_window
= selected_window
;
4894 if (detect_input_pending_run_timers (do_display
))
4896 swallow_events (do_display
);
4897 if (detect_input_pending_run_timers (do_display
))
4901 /* If a timer has run, this might have changed buffers
4902 an alike. Make read_key_sequence aware of that. */
4903 if (timers_run
!= old_timers_run
4904 && waiting_for_user_input_p
== -1
4905 && (old_buffer
!= current_buffer
4906 || !EQ (old_window
, selected_window
)))
4907 record_asynch_buffer_change ();
4913 /* If there is unread keyboard input, also return. */
4915 && requeued_events_pending_p ())
4918 /* If we are not checking for keyboard input now,
4919 do process events (but don't run any timers).
4920 This is so that X events will be processed.
4921 Otherwise they may have to wait until polling takes place.
4922 That would causes delays in pasting selections, for example.
4924 (We used to do this only if wait_for_cell.) */
4925 if (read_kbd
== 0 && detect_input_pending ())
4927 swallow_events (do_display
);
4928 #if 0 /* Exiting when read_kbd doesn't request that seems wrong, though. */
4929 if (detect_input_pending ())
4934 /* Exit now if the cell we're waiting for became non-nil. */
4935 if (! NILP (wait_for_cell
) && ! NILP (XCAR (wait_for_cell
)))
4939 /* If we think we have keyboard input waiting, but didn't get SIGIO,
4940 go read it. This can happen with X on BSD after logging out.
4941 In that case, there really is no input and no SIGIO,
4942 but select says there is input. */
4944 if (read_kbd
&& interrupt_input
4945 && keyboard_bit_set (&Available
) && ! noninteractive
)
4946 kill (getpid (), SIGIO
);
4950 got_some_input
|= nfds
> 0;
4952 /* If checking input just got us a size-change event from X,
4953 obey it now if we should. */
4954 if (read_kbd
|| ! NILP (wait_for_cell
))
4955 do_pending_window_change (0);
4957 /* Check for data from a process. */
4958 if (no_avail
|| nfds
== 0)
4961 /* Really FIRST_PROC_DESC should be 0 on Unix,
4962 but this is safer in the short run. */
4963 for (channel
= 0; channel
<= max_process_desc
; channel
++)
4965 if (FD_ISSET (channel
, &Available
)
4966 && FD_ISSET (channel
, &non_keyboard_wait_mask
))
4970 /* If waiting for this channel, arrange to return as
4971 soon as no more input to be processed. No more
4973 if (wait_channel
== channel
)
4979 proc
= chan_process
[channel
];
4983 /* If this is a server stream socket, accept connection. */
4984 if (EQ (XPROCESS (proc
)->status
, Qlisten
))
4986 server_accept_connection (proc
, channel
);
4990 /* Read data from the process, starting with our
4991 buffered-ahead character if we have one. */
4993 nread
= read_process_output (proc
, channel
);
4996 /* Since read_process_output can run a filter,
4997 which can call accept-process-output,
4998 don't try to read from any other processes
4999 before doing the select again. */
5000 FD_ZERO (&Available
);
5003 redisplay_preserve_echo_area (12);
5006 else if (nread
== -1 && errno
== EWOULDBLOCK
)
5009 /* ISC 4.1 defines both EWOULDBLOCK and O_NONBLOCK,
5010 and Emacs uses O_NONBLOCK, so what we get is EAGAIN. */
5012 else if (nread
== -1 && errno
== EAGAIN
)
5016 else if (nread
== -1 && errno
== EAGAIN
)
5018 /* Note that we cannot distinguish between no input
5019 available now and a closed pipe.
5020 With luck, a closed pipe will be accompanied by
5021 subprocess termination and SIGCHLD. */
5022 else if (nread
== 0 && !NETCONN_P (proc
) && !SERIALCONN_P (proc
))
5024 #endif /* O_NDELAY */
5025 #endif /* O_NONBLOCK */
5027 /* On some OSs with ptys, when the process on one end of
5028 a pty exits, the other end gets an error reading with
5029 errno = EIO instead of getting an EOF (0 bytes read).
5030 Therefore, if we get an error reading and errno =
5031 EIO, just continue, because the child process has
5032 exited and should clean itself up soon (e.g. when we
5035 However, it has been known to happen that the SIGCHLD
5036 got lost. So raise the signal again just in case.
5038 else if (nread
== -1 && errno
== EIO
)
5040 /* Clear the descriptor now, so we only raise the signal once. */
5041 FD_CLR (channel
, &input_wait_mask
);
5042 FD_CLR (channel
, &non_keyboard_wait_mask
);
5044 kill (getpid (), SIGCHLD
);
5046 #endif /* HAVE_PTYS */
5047 /* If we can detect process termination, don't consider the process
5048 gone just because its pipe is closed. */
5050 else if (nread
== 0 && !NETCONN_P (proc
) && !SERIALCONN_P (proc
))
5055 /* Preserve status of processes already terminated. */
5056 XPROCESS (proc
)->tick
= ++process_tick
;
5057 deactivate_process (proc
);
5058 if (XPROCESS (proc
)->raw_status_new
)
5059 update_status (XPROCESS (proc
));
5060 if (EQ (XPROCESS (proc
)->status
, Qrun
))
5061 XPROCESS (proc
)->status
5062 = Fcons (Qexit
, Fcons (make_number (256), Qnil
));
5065 #ifdef NON_BLOCKING_CONNECT
5066 if (check_connect
&& FD_ISSET (channel
, &Connecting
)
5067 && FD_ISSET (channel
, &connect_wait_mask
))
5069 struct Lisp_Process
*p
;
5071 FD_CLR (channel
, &connect_wait_mask
);
5072 if (--num_pending_connects
< 0)
5075 proc
= chan_process
[channel
];
5079 p
= XPROCESS (proc
);
5082 /* getsockopt(,,SO_ERROR,,) is said to hang on some systems.
5083 So only use it on systems where it is known to work. */
5085 int xlen
= sizeof(xerrno
);
5086 if (getsockopt(channel
, SOL_SOCKET
, SO_ERROR
, &xerrno
, &xlen
))
5091 struct sockaddr pname
;
5092 int pnamelen
= sizeof(pname
);
5094 /* If connection failed, getpeername will fail. */
5096 if (getpeername(channel
, &pname
, &pnamelen
) < 0)
5098 /* Obtain connect failure code through error slippage. */
5101 if (errno
== ENOTCONN
&& read(channel
, &dummy
, 1) < 0)
5108 p
->tick
= ++process_tick
;
5109 p
->status
= Fcons (Qfailed
, Fcons (make_number (xerrno
), Qnil
));
5110 deactivate_process (proc
);
5115 /* Execute the sentinel here. If we had relied on
5116 status_notify to do it later, it will read input
5117 from the process before calling the sentinel. */
5118 exec_sentinel (proc
, build_string ("open\n"));
5119 if (!EQ (p
->filter
, Qt
) && !EQ (p
->command
, Qt
))
5121 FD_SET (p
->infd
, &input_wait_mask
);
5122 FD_SET (p
->infd
, &non_keyboard_wait_mask
);
5126 #endif /* NON_BLOCKING_CONNECT */
5127 } /* end for each file descriptor */
5128 } /* end while exit conditions not met */
5130 unbind_to (count
, Qnil
);
5132 /* If calling from keyboard input, do not quit
5133 since we want to return C-g as an input character.
5134 Otherwise, do pending quit if requested. */
5137 /* Prevent input_pending from remaining set if we quit. */
5138 clear_input_pending ();
5142 return got_some_input
;
5145 /* Given a list (FUNCTION ARGS...), apply FUNCTION to the ARGS. */
5148 read_process_output_call (fun_and_args
)
5149 Lisp_Object fun_and_args
;
5151 return apply1 (XCAR (fun_and_args
), XCDR (fun_and_args
));
5155 read_process_output_error_handler (error
)
5158 cmd_error_internal (error
, "error in process filter: ");
5160 update_echo_area ();
5161 Fsleep_for (make_number (2), Qnil
);
5165 /* Read pending output from the process channel,
5166 starting with our buffered-ahead character if we have one.
5167 Yield number of decoded characters read.
5169 This function reads at most 4096 characters.
5170 If you want to read all available subprocess output,
5171 you must call it repeatedly until it returns zero.
5173 The characters read are decoded according to PROC's coding-system
5177 read_process_output (proc
, channel
)
5179 register int channel
;
5181 register int nbytes
;
5183 register Lisp_Object outstream
;
5184 register struct buffer
*old
= current_buffer
;
5185 register struct Lisp_Process
*p
= XPROCESS (proc
);
5186 register int opoint
;
5187 struct coding_system
*coding
= proc_decode_coding_system
[channel
];
5188 int carryover
= p
->decoding_carryover
;
5191 chars
= (char *) alloca (carryover
+ readmax
);
5193 /* See the comment above. */
5194 bcopy (SDATA (p
->decoding_buf
), chars
, carryover
);
5196 #ifdef DATAGRAM_SOCKETS
5197 /* We have a working select, so proc_buffered_char is always -1. */
5198 if (DATAGRAM_CHAN_P (channel
))
5200 int len
= datagram_address
[channel
].len
;
5201 nbytes
= recvfrom (channel
, chars
+ carryover
, readmax
,
5202 0, datagram_address
[channel
].sa
, &len
);
5206 if (proc_buffered_char
[channel
] < 0)
5208 nbytes
= emacs_read (channel
, chars
+ carryover
, readmax
);
5209 #ifdef ADAPTIVE_READ_BUFFERING
5210 if (nbytes
> 0 && p
->adaptive_read_buffering
)
5212 int delay
= p
->read_output_delay
;
5215 if (delay
< READ_OUTPUT_DELAY_MAX_MAX
)
5218 process_output_delay_count
++;
5219 delay
+= READ_OUTPUT_DELAY_INCREMENT
* 2;
5222 else if (delay
> 0 && (nbytes
== readmax
))
5224 delay
-= READ_OUTPUT_DELAY_INCREMENT
;
5226 process_output_delay_count
--;
5228 p
->read_output_delay
= delay
;
5231 p
->read_output_skip
= 1;
5232 process_output_skip
= 1;
5239 chars
[carryover
] = proc_buffered_char
[channel
];
5240 proc_buffered_char
[channel
] = -1;
5241 nbytes
= emacs_read (channel
, chars
+ carryover
+ 1, readmax
- 1);
5245 nbytes
= nbytes
+ 1;
5248 p
->decoding_carryover
= 0;
5250 /* At this point, NBYTES holds number of bytes just received
5251 (including the one in proc_buffered_char[channel]). */
5254 if (nbytes
< 0 || coding
->mode
& CODING_MODE_LAST_BLOCK
)
5256 coding
->mode
|= CODING_MODE_LAST_BLOCK
;
5259 /* Now set NBYTES how many bytes we must decode. */
5260 nbytes
+= carryover
;
5262 /* Read and dispose of the process output. */
5263 outstream
= p
->filter
;
5264 if (!NILP (outstream
))
5266 /* We inhibit quit here instead of just catching it so that
5267 hitting ^G when a filter happens to be running won't screw
5269 int count
= SPECPDL_INDEX ();
5270 Lisp_Object odeactivate
;
5271 Lisp_Object obuffer
, okeymap
;
5273 int outer_running_asynch_code
= running_asynch_code
;
5274 int waiting
= waiting_for_user_input_p
;
5276 /* No need to gcpro these, because all we do with them later
5277 is test them for EQness, and none of them should be a string. */
5278 odeactivate
= Vdeactivate_mark
;
5279 XSETBUFFER (obuffer
, current_buffer
);
5280 okeymap
= current_buffer
->keymap
;
5282 specbind (Qinhibit_quit
, Qt
);
5283 specbind (Qlast_nonmenu_event
, Qt
);
5285 /* In case we get recursively called,
5286 and we already saved the match data nonrecursively,
5287 save the same match data in safely recursive fashion. */
5288 if (outer_running_asynch_code
)
5291 /* Don't clobber the CURRENT match data, either! */
5292 tem
= Fmatch_data (Qnil
, Qnil
, Qnil
);
5293 restore_search_regs ();
5294 record_unwind_save_match_data ();
5295 Fset_match_data (tem
, Qt
);
5298 /* For speed, if a search happens within this code,
5299 save the match data in a special nonrecursive fashion. */
5300 running_asynch_code
= 1;
5302 decode_coding_c_string (coding
, chars
, nbytes
, Qt
);
5303 text
= coding
->dst_object
;
5304 Vlast_coding_system_used
= CODING_ID_NAME (coding
->id
);
5305 /* A new coding system might be found. */
5306 if (!EQ (p
->decode_coding_system
, Vlast_coding_system_used
))
5308 p
->decode_coding_system
= Vlast_coding_system_used
;
5310 /* Don't call setup_coding_system for
5311 proc_decode_coding_system[channel] here. It is done in
5312 detect_coding called via decode_coding above. */
5314 /* If a coding system for encoding is not yet decided, we set
5315 it as the same as coding-system for decoding.
5317 But, before doing that we must check if
5318 proc_encode_coding_system[p->outfd] surely points to a
5319 valid memory because p->outfd will be changed once EOF is
5320 sent to the process. */
5321 if (NILP (p
->encode_coding_system
)
5322 && proc_encode_coding_system
[p
->outfd
])
5324 p
->encode_coding_system
5325 = coding_inherit_eol_type (Vlast_coding_system_used
, Qnil
);
5326 setup_coding_system (p
->encode_coding_system
,
5327 proc_encode_coding_system
[p
->outfd
]);
5331 if (coding
->carryover_bytes
> 0)
5333 if (SCHARS (p
->decoding_buf
) < coding
->carryover_bytes
)
5334 p
->decoding_buf
= make_uninit_string (coding
->carryover_bytes
);
5335 bcopy (coding
->carryover
, SDATA (p
->decoding_buf
),
5336 coding
->carryover_bytes
);
5337 p
->decoding_carryover
= coding
->carryover_bytes
;
5339 if (SBYTES (text
) > 0)
5340 internal_condition_case_1 (read_process_output_call
,
5342 Fcons (proc
, Fcons (text
, Qnil
))),
5343 !NILP (Vdebug_on_error
) ? Qnil
: Qerror
,
5344 read_process_output_error_handler
);
5346 /* If we saved the match data nonrecursively, restore it now. */
5347 restore_search_regs ();
5348 running_asynch_code
= outer_running_asynch_code
;
5350 /* Handling the process output should not deactivate the mark. */
5351 Vdeactivate_mark
= odeactivate
;
5353 /* Restore waiting_for_user_input_p as it was
5354 when we were called, in case the filter clobbered it. */
5355 waiting_for_user_input_p
= waiting
;
5357 #if 0 /* Call record_asynch_buffer_change unconditionally,
5358 because we might have changed minor modes or other things
5359 that affect key bindings. */
5360 if (! EQ (Fcurrent_buffer (), obuffer
)
5361 || ! EQ (current_buffer
->keymap
, okeymap
))
5363 /* But do it only if the caller is actually going to read events.
5364 Otherwise there's no need to make him wake up, and it could
5365 cause trouble (for example it would make sit_for return). */
5366 if (waiting_for_user_input_p
== -1)
5367 record_asynch_buffer_change ();
5369 unbind_to (count
, Qnil
);
5373 /* If no filter, write into buffer if it isn't dead. */
5374 if (!NILP (p
->buffer
) && !NILP (XBUFFER (p
->buffer
)->name
))
5376 Lisp_Object old_read_only
;
5377 int old_begv
, old_zv
;
5378 int old_begv_byte
, old_zv_byte
;
5379 Lisp_Object odeactivate
;
5380 int before
, before_byte
;
5385 odeactivate
= Vdeactivate_mark
;
5387 Fset_buffer (p
->buffer
);
5389 opoint_byte
= PT_BYTE
;
5390 old_read_only
= current_buffer
->read_only
;
5393 old_begv_byte
= BEGV_BYTE
;
5394 old_zv_byte
= ZV_BYTE
;
5396 current_buffer
->read_only
= Qnil
;
5398 /* Insert new output into buffer
5399 at the current end-of-output marker,
5400 thus preserving logical ordering of input and output. */
5401 if (XMARKER (p
->mark
)->buffer
)
5402 SET_PT_BOTH (clip_to_bounds (BEGV
, marker_position (p
->mark
), ZV
),
5403 clip_to_bounds (BEGV_BYTE
, marker_byte_position (p
->mark
),
5406 SET_PT_BOTH (ZV
, ZV_BYTE
);
5408 before_byte
= PT_BYTE
;
5410 /* If the output marker is outside of the visible region, save
5411 the restriction and widen. */
5412 if (! (BEGV
<= PT
&& PT
<= ZV
))
5415 decode_coding_c_string (coding
, chars
, nbytes
, Qt
);
5416 text
= coding
->dst_object
;
5417 Vlast_coding_system_used
= CODING_ID_NAME (coding
->id
);
5418 /* A new coding system might be found. See the comment in the
5419 similar code in the previous `if' block. */
5420 if (!EQ (p
->decode_coding_system
, Vlast_coding_system_used
))
5422 p
->decode_coding_system
= Vlast_coding_system_used
;
5423 if (NILP (p
->encode_coding_system
)
5424 && proc_encode_coding_system
[p
->outfd
])
5426 p
->encode_coding_system
5427 = coding_inherit_eol_type (Vlast_coding_system_used
, Qnil
);
5428 setup_coding_system (p
->encode_coding_system
,
5429 proc_encode_coding_system
[p
->outfd
]);
5432 if (coding
->carryover_bytes
> 0)
5434 if (SCHARS (p
->decoding_buf
) < coding
->carryover_bytes
)
5435 p
->decoding_buf
= make_uninit_string (coding
->carryover_bytes
);
5436 bcopy (coding
->carryover
, SDATA (p
->decoding_buf
),
5437 coding
->carryover_bytes
);
5438 p
->decoding_carryover
= coding
->carryover_bytes
;
5440 /* Adjust the multibyteness of TEXT to that of the buffer. */
5441 if (NILP (current_buffer
->enable_multibyte_characters
)
5442 != ! STRING_MULTIBYTE (text
))
5443 text
= (STRING_MULTIBYTE (text
)
5444 ? Fstring_as_unibyte (text
)
5445 : Fstring_to_multibyte (text
));
5446 /* Insert before markers in case we are inserting where
5447 the buffer's mark is, and the user's next command is Meta-y. */
5448 insert_from_string_before_markers (text
, 0, 0,
5449 SCHARS (text
), SBYTES (text
), 0);
5451 /* Make sure the process marker's position is valid when the
5452 process buffer is changed in the signal_after_change above.
5453 W3 is known to do that. */
5454 if (BUFFERP (p
->buffer
)
5455 && (b
= XBUFFER (p
->buffer
), b
!= current_buffer
))
5456 set_marker_both (p
->mark
, p
->buffer
, BUF_PT (b
), BUF_PT_BYTE (b
));
5458 set_marker_both (p
->mark
, p
->buffer
, PT
, PT_BYTE
);
5460 update_mode_lines
++;
5462 /* Make sure opoint and the old restrictions
5463 float ahead of any new text just as point would. */
5464 if (opoint
>= before
)
5466 opoint
+= PT
- before
;
5467 opoint_byte
+= PT_BYTE
- before_byte
;
5469 if (old_begv
> before
)
5471 old_begv
+= PT
- before
;
5472 old_begv_byte
+= PT_BYTE
- before_byte
;
5474 if (old_zv
>= before
)
5476 old_zv
+= PT
- before
;
5477 old_zv_byte
+= PT_BYTE
- before_byte
;
5480 /* If the restriction isn't what it should be, set it. */
5481 if (old_begv
!= BEGV
|| old_zv
!= ZV
)
5482 Fnarrow_to_region (make_number (old_begv
), make_number (old_zv
));
5484 /* Handling the process output should not deactivate the mark. */
5485 Vdeactivate_mark
= odeactivate
;
5487 current_buffer
->read_only
= old_read_only
;
5488 SET_PT_BOTH (opoint
, opoint_byte
);
5489 set_buffer_internal (old
);
5494 DEFUN ("waiting-for-user-input-p", Fwaiting_for_user_input_p
, Swaiting_for_user_input_p
,
5496 doc
: /* Returns non-nil if Emacs is waiting for input from the user.
5497 This is intended for use by asynchronous process output filters and sentinels. */)
5500 return (waiting_for_user_input_p
? Qt
: Qnil
);
5503 /* Sending data to subprocess */
5505 jmp_buf send_process_frame
;
5506 Lisp_Object process_sent_to
;
5509 send_process_trap ()
5511 SIGNAL_THREAD_CHECK (SIGPIPE
);
5512 sigunblock (sigmask (SIGPIPE
));
5513 longjmp (send_process_frame
, 1);
5516 /* Send some data to process PROC.
5517 BUF is the beginning of the data; LEN is the number of characters.
5518 OBJECT is the Lisp object that the data comes from. If OBJECT is
5519 nil or t, it means that the data comes from C string.
5521 If OBJECT is not nil, the data is encoded by PROC's coding-system
5522 for encoding before it is sent.
5524 This function can evaluate Lisp code and can garbage collect. */
5527 send_process (proc
, buf
, len
, object
)
5528 volatile Lisp_Object proc
;
5529 unsigned char *volatile buf
;
5531 volatile Lisp_Object object
;
5533 /* Use volatile to protect variables from being clobbered by longjmp. */
5534 struct Lisp_Process
*p
= XPROCESS (proc
);
5536 struct coding_system
*coding
;
5537 struct gcpro gcpro1
;
5538 SIGTYPE (*volatile old_sigpipe
) ();
5542 if (p
->raw_status_new
)
5544 if (! EQ (p
->status
, Qrun
))
5545 error ("Process %s not running", SDATA (p
->name
));
5547 error ("Output file descriptor of %s is closed", SDATA (p
->name
));
5549 coding
= proc_encode_coding_system
[p
->outfd
];
5550 Vlast_coding_system_used
= CODING_ID_NAME (coding
->id
);
5552 if ((STRINGP (object
) && STRING_MULTIBYTE (object
))
5553 || (BUFFERP (object
)
5554 && !NILP (XBUFFER (object
)->enable_multibyte_characters
))
5557 if (!EQ (Vlast_coding_system_used
, p
->encode_coding_system
))
5558 /* The coding system for encoding was changed to raw-text
5559 because we sent a unibyte text previously. Now we are
5560 sending a multibyte text, thus we must encode it by the
5561 original coding system specified for the current process. */
5562 setup_coding_system (p
->encode_coding_system
, coding
);
5563 coding
->src_multibyte
= 1;
5567 /* For sending a unibyte text, character code conversion should
5568 not take place but EOL conversion should. So, setup raw-text
5569 or one of the subsidiary if we have not yet done it. */
5570 if (CODING_REQUIRE_ENCODING (coding
))
5572 if (CODING_REQUIRE_FLUSHING (coding
))
5574 /* But, before changing the coding, we must flush out data. */
5575 coding
->mode
|= CODING_MODE_LAST_BLOCK
;
5576 send_process (proc
, "", 0, Qt
);
5577 coding
->mode
&= CODING_MODE_LAST_BLOCK
;
5579 setup_coding_system (raw_text_coding_system
5580 (Vlast_coding_system_used
),
5582 coding
->src_multibyte
= 0;
5585 coding
->dst_multibyte
= 0;
5587 if (CODING_REQUIRE_ENCODING (coding
))
5589 coding
->dst_object
= Qt
;
5590 if (BUFFERP (object
))
5592 int from_byte
, from
, to
;
5593 int save_pt
, save_pt_byte
;
5594 struct buffer
*cur
= current_buffer
;
5596 set_buffer_internal (XBUFFER (object
));
5597 save_pt
= PT
, save_pt_byte
= PT_BYTE
;
5599 from_byte
= PTR_BYTE_POS (buf
);
5600 from
= BYTE_TO_CHAR (from_byte
);
5601 to
= BYTE_TO_CHAR (from_byte
+ len
);
5602 TEMP_SET_PT_BOTH (from
, from_byte
);
5603 encode_coding_object (coding
, object
, from
, from_byte
,
5604 to
, from_byte
+ len
, Qt
);
5605 TEMP_SET_PT_BOTH (save_pt
, save_pt_byte
);
5606 set_buffer_internal (cur
);
5608 else if (STRINGP (object
))
5610 encode_coding_string (coding
, object
, 1);
5614 coding
->dst_object
= make_unibyte_string (buf
, len
);
5615 coding
->produced
= len
;
5618 len
= coding
->produced
;
5619 buf
= SDATA (coding
->dst_object
);
5622 if (pty_max_bytes
== 0)
5624 #if defined (HAVE_FPATHCONF) && defined (_PC_MAX_CANON)
5625 pty_max_bytes
= fpathconf (p
->outfd
, _PC_MAX_CANON
);
5626 if (pty_max_bytes
< 0)
5627 pty_max_bytes
= 250;
5629 pty_max_bytes
= 250;
5631 /* Deduct one, to leave space for the eof. */
5635 /* 2000-09-21: Emacs 20.7, sparc-sun-solaris-2.6, GCC 2.95.2,
5636 CFLAGS="-g -O": The value of the parameter `proc' is clobbered
5637 when returning with longjmp despite being declared volatile. */
5638 if (!setjmp (send_process_frame
))
5640 process_sent_to
= proc
;
5645 /* Decide how much data we can send in one batch.
5646 Long lines need to be split into multiple batches. */
5649 /* Starting this at zero is always correct when not the first
5650 iteration because the previous iteration ended by sending C-d.
5651 It may not be correct for the first iteration
5652 if a partial line was sent in a separate send_process call.
5653 If that proves worth handling, we need to save linepos
5654 in the process object. */
5656 unsigned char *ptr
= (unsigned char *) buf
;
5657 unsigned char *end
= (unsigned char *) buf
+ len
;
5659 /* Scan through this text for a line that is too long. */
5660 while (ptr
!= end
&& linepos
< pty_max_bytes
)
5668 /* If we found one, break the line there
5669 and put in a C-d to force the buffer through. */
5673 /* Send this batch, using one or more write calls. */
5676 int outfd
= p
->outfd
;
5677 old_sigpipe
= (SIGTYPE (*) ()) signal (SIGPIPE
, send_process_trap
);
5678 #ifdef DATAGRAM_SOCKETS
5679 if (DATAGRAM_CHAN_P (outfd
))
5681 rv
= sendto (outfd
, (char *) buf
, this,
5682 0, datagram_address
[outfd
].sa
,
5683 datagram_address
[outfd
].len
);
5684 if (rv
< 0 && errno
== EMSGSIZE
)
5686 signal (SIGPIPE
, old_sigpipe
);
5687 report_file_error ("sending datagram",
5688 Fcons (proc
, Qnil
));
5694 rv
= emacs_write (outfd
, (char *) buf
, this);
5695 #ifdef ADAPTIVE_READ_BUFFERING
5696 if (p
->read_output_delay
> 0
5697 && p
->adaptive_read_buffering
== 1)
5699 p
->read_output_delay
= 0;
5700 process_output_delay_count
--;
5701 p
->read_output_skip
= 0;
5705 signal (SIGPIPE
, old_sigpipe
);
5711 || errno
== EWOULDBLOCK
5717 /* Buffer is full. Wait, accepting input;
5718 that may allow the program
5719 to finish doing output and read more. */
5723 #ifdef BROKEN_PTY_READ_AFTER_EAGAIN
5724 /* A gross hack to work around a bug in FreeBSD.
5725 In the following sequence, read(2) returns
5729 write(2) 954 bytes, get EAGAIN
5730 read(2) 1024 bytes in process_read_output
5731 read(2) 11 bytes in process_read_output
5733 That is, read(2) returns more bytes than have
5734 ever been written successfully. The 1033 bytes
5735 read are the 1022 bytes written successfully
5736 after processing (for example with CRs added if
5737 the terminal is set up that way which it is
5738 here). The same bytes will be seen again in a
5739 later read(2), without the CRs. */
5741 if (errno
== EAGAIN
)
5744 ioctl (p
->outfd
, TIOCFLUSH
, &flags
);
5746 #endif /* BROKEN_PTY_READ_AFTER_EAGAIN */
5748 /* Running filters might relocate buffers or strings.
5749 Arrange to relocate BUF. */
5750 if (BUFFERP (object
))
5751 offset
= BUF_PTR_BYTE_POS (XBUFFER (object
), buf
);
5752 else if (STRINGP (object
))
5753 offset
= buf
- SDATA (object
);
5755 #ifdef EMACS_HAS_USECS
5756 wait_reading_process_output (0, 20000, 0, 0, Qnil
, NULL
, 0);
5758 wait_reading_process_output (1, 0, 0, 0, Qnil
, NULL
, 0);
5761 if (BUFFERP (object
))
5762 buf
= BUF_BYTE_ADDRESS (XBUFFER (object
), offset
);
5763 else if (STRINGP (object
))
5764 buf
= offset
+ SDATA (object
);
5769 /* This is a real error. */
5770 report_file_error ("writing to process", Fcons (proc
, Qnil
));
5777 /* If we sent just part of the string, put in an EOF (C-d)
5778 to force it through, before we send the rest. */
5780 Fprocess_send_eof (proc
);
5785 signal (SIGPIPE
, old_sigpipe
);
5786 proc
= process_sent_to
;
5787 p
= XPROCESS (proc
);
5788 p
->raw_status_new
= 0;
5789 p
->status
= Fcons (Qexit
, Fcons (make_number (256), Qnil
));
5790 p
->tick
= ++process_tick
;
5791 deactivate_process (proc
);
5792 error ("SIGPIPE raised on process %s; closed it", SDATA (p
->name
));
5798 DEFUN ("process-send-region", Fprocess_send_region
, Sprocess_send_region
,
5800 doc
: /* Send current contents of region as input to PROCESS.
5801 PROCESS may be a process, a buffer, the name of a process or buffer, or
5802 nil, indicating the current buffer's process.
5803 Called from program, takes three arguments, PROCESS, START and END.
5804 If the region is more than 500 characters long,
5805 it is sent in several bunches. This may happen even for shorter regions.
5806 Output from processes can arrive in between bunches. */)
5807 (process
, start
, end
)
5808 Lisp_Object process
, start
, end
;
5813 proc
= get_process (process
);
5814 validate_region (&start
, &end
);
5816 if (XINT (start
) < GPT
&& XINT (end
) > GPT
)
5817 move_gap (XINT (start
));
5819 start1
= CHAR_TO_BYTE (XINT (start
));
5820 end1
= CHAR_TO_BYTE (XINT (end
));
5821 send_process (proc
, BYTE_POS_ADDR (start1
), end1
- start1
,
5822 Fcurrent_buffer ());
5827 DEFUN ("process-send-string", Fprocess_send_string
, Sprocess_send_string
,
5829 doc
: /* Send PROCESS the contents of STRING as input.
5830 PROCESS may be a process, a buffer, the name of a process or buffer, or
5831 nil, indicating the current buffer's process.
5832 If STRING is more than 500 characters long,
5833 it is sent in several bunches. This may happen even for shorter strings.
5834 Output from processes can arrive in between bunches. */)
5836 Lisp_Object process
, string
;
5839 CHECK_STRING (string
);
5840 proc
= get_process (process
);
5841 send_process (proc
, SDATA (string
),
5842 SBYTES (string
), string
);
5846 /* Return the foreground process group for the tty/pty that
5847 the process P uses. */
5849 emacs_get_tty_pgrp (p
)
5850 struct Lisp_Process
*p
;
5855 if (ioctl (p
->infd
, TIOCGPGRP
, &gid
) == -1 && ! NILP (p
->tty_name
))
5858 /* Some OS:es (Solaris 8/9) does not allow TIOCGPGRP from the
5859 master side. Try the slave side. */
5860 fd
= emacs_open (SDATA (p
->tty_name
), O_RDONLY
, 0);
5864 ioctl (fd
, TIOCGPGRP
, &gid
);
5868 #endif /* defined (TIOCGPGRP ) */
5873 DEFUN ("process-running-child-p", Fprocess_running_child_p
,
5874 Sprocess_running_child_p
, 0, 1, 0,
5875 doc
: /* Return t if PROCESS has given the terminal to a child.
5876 If the operating system does not make it possible to find out,
5877 return t unconditionally. */)
5879 Lisp_Object process
;
5881 /* Initialize in case ioctl doesn't exist or gives an error,
5882 in a way that will cause returning t. */
5885 struct Lisp_Process
*p
;
5887 proc
= get_process (process
);
5888 p
= XPROCESS (proc
);
5890 if (!EQ (p
->type
, Qreal
))
5891 error ("Process %s is not a subprocess",
5894 error ("Process %s is not active",
5897 gid
= emacs_get_tty_pgrp (p
);
5904 /* send a signal number SIGNO to PROCESS.
5905 If CURRENT_GROUP is t, that means send to the process group
5906 that currently owns the terminal being used to communicate with PROCESS.
5907 This is used for various commands in shell mode.
5908 If CURRENT_GROUP is lambda, that means send to the process group
5909 that currently owns the terminal, but only if it is NOT the shell itself.
5911 If NOMSG is zero, insert signal-announcements into process's buffers
5914 If we can, we try to signal PROCESS by sending control characters
5915 down the pty. This allows us to signal inferiors who have changed
5916 their uid, for which killpg would return an EPERM error. */
5919 process_send_signal (process
, signo
, current_group
, nomsg
)
5920 Lisp_Object process
;
5922 Lisp_Object current_group
;
5926 register struct Lisp_Process
*p
;
5930 proc
= get_process (process
);
5931 p
= XPROCESS (proc
);
5933 if (!EQ (p
->type
, Qreal
))
5934 error ("Process %s is not a subprocess",
5937 error ("Process %s is not active",
5941 current_group
= Qnil
;
5943 /* If we are using pgrps, get a pgrp number and make it negative. */
5944 if (NILP (current_group
))
5945 /* Send the signal to the shell's process group. */
5949 #ifdef SIGNALS_VIA_CHARACTERS
5950 /* If possible, send signals to the entire pgrp
5951 by sending an input character to it. */
5953 /* TERMIOS is the latest and bestest, and seems most likely to
5954 work. If the system has it, use it. */
5957 cc_t
*sig_char
= NULL
;
5959 tcgetattr (p
->infd
, &t
);
5964 sig_char
= &t
.c_cc
[VINTR
];
5968 sig_char
= &t
.c_cc
[VQUIT
];
5972 #if defined (VSWTCH) && !defined (PREFER_VSUSP)
5973 sig_char
= &t
.c_cc
[VSWTCH
];
5975 sig_char
= &t
.c_cc
[VSUSP
];
5980 if (sig_char
&& *sig_char
!= CDISABLE
)
5982 send_process (proc
, sig_char
, 1, Qnil
);
5985 /* If we can't send the signal with a character,
5986 fall through and send it another way. */
5987 #else /* ! HAVE_TERMIOS */
5989 /* On Berkeley descendants, the following IOCTL's retrieve the
5990 current control characters. */
5991 #if defined (TIOCGLTC) && defined (TIOCGETC)
5999 ioctl (p
->infd
, TIOCGETC
, &c
);
6000 send_process (proc
, &c
.t_intrc
, 1, Qnil
);
6003 ioctl (p
->infd
, TIOCGETC
, &c
);
6004 send_process (proc
, &c
.t_quitc
, 1, Qnil
);
6008 ioctl (p
->infd
, TIOCGLTC
, &lc
);
6009 send_process (proc
, &lc
.t_suspc
, 1, Qnil
);
6011 #endif /* ! defined (SIGTSTP) */
6014 #else /* ! defined (TIOCGLTC) && defined (TIOCGETC) */
6016 /* On SYSV descendants, the TCGETA ioctl retrieves the current control
6023 ioctl (p
->infd
, TCGETA
, &t
);
6024 send_process (proc
, &t
.c_cc
[VINTR
], 1, Qnil
);
6027 ioctl (p
->infd
, TCGETA
, &t
);
6028 send_process (proc
, &t
.c_cc
[VQUIT
], 1, Qnil
);
6032 ioctl (p
->infd
, TCGETA
, &t
);
6033 send_process (proc
, &t
.c_cc
[VSWTCH
], 1, Qnil
);
6035 #endif /* ! defined (SIGTSTP) */
6037 #else /* ! defined (TCGETA) */
6038 Your configuration files are messed up
.
6039 /* If your system configuration files define SIGNALS_VIA_CHARACTERS,
6040 you'd better be using one of the alternatives above! */
6041 #endif /* ! defined (TCGETA) */
6042 #endif /* ! defined (TIOCGLTC) && defined (TIOCGETC) */
6043 /* In this case, the code above should alway return. */
6045 #endif /* ! defined HAVE_TERMIOS */
6047 /* The code above may fall through if it can't
6048 handle the signal. */
6049 #endif /* defined (SIGNALS_VIA_CHARACTERS) */
6052 /* Get the current pgrp using the tty itself, if we have that.
6053 Otherwise, use the pty to get the pgrp.
6054 On pfa systems, saka@pfu.fujitsu.co.JP writes:
6055 "TIOCGPGRP symbol defined in sys/ioctl.h at E50.
6056 But, TIOCGPGRP does not work on E50 ;-P works fine on E60"
6057 His patch indicates that if TIOCGPGRP returns an error, then
6058 we should just assume that p->pid is also the process group id. */
6060 gid
= emacs_get_tty_pgrp (p
);
6063 /* If we can't get the information, assume
6064 the shell owns the tty. */
6067 /* It is not clear whether anything really can set GID to -1.
6068 Perhaps on some system one of those ioctls can or could do so.
6069 Or perhaps this is vestigial. */
6072 #else /* ! defined (TIOCGPGRP ) */
6073 /* Can't select pgrps on this system, so we know that
6074 the child itself heads the pgrp. */
6076 #endif /* ! defined (TIOCGPGRP ) */
6078 /* If current_group is lambda, and the shell owns the terminal,
6079 don't send any signal. */
6080 if (EQ (current_group
, Qlambda
) && gid
== p
->pid
)
6088 p
->raw_status_new
= 0;
6090 p
->tick
= ++process_tick
;
6092 status_notify (NULL
);
6094 #endif /* ! defined (SIGCONT) */
6098 flush_pending_output (p
->infd
);
6102 /* If we don't have process groups, send the signal to the immediate
6103 subprocess. That isn't really right, but it's better than any
6104 obvious alternative. */
6107 kill (p
->pid
, signo
);
6111 /* gid may be a pid, or minus a pgrp's number */
6113 if (!NILP (current_group
))
6115 if (ioctl (p
->infd
, TIOCSIGSEND
, signo
) == -1)
6116 EMACS_KILLPG (gid
, signo
);
6123 #else /* ! defined (TIOCSIGSEND) */
6124 EMACS_KILLPG (gid
, signo
);
6125 #endif /* ! defined (TIOCSIGSEND) */
6128 DEFUN ("interrupt-process", Finterrupt_process
, Sinterrupt_process
, 0, 2, 0,
6129 doc
: /* Interrupt process PROCESS.
6130 PROCESS may be a process, a buffer, or the name of a process or buffer.
6131 No arg or nil means current buffer's process.
6132 Second arg CURRENT-GROUP non-nil means send signal to
6133 the current process-group of the process's controlling terminal
6134 rather than to the process's own process group.
6135 If the process is a shell, this means interrupt current subjob
6136 rather than the shell.
6138 If CURRENT-GROUP is `lambda', and if the shell owns the terminal,
6139 don't send the signal. */)
6140 (process
, current_group
)
6141 Lisp_Object process
, current_group
;
6143 process_send_signal (process
, SIGINT
, current_group
, 0);
6147 DEFUN ("kill-process", Fkill_process
, Skill_process
, 0, 2, 0,
6148 doc
: /* Kill process PROCESS. May be process or name of one.
6149 See function `interrupt-process' for more details on usage. */)
6150 (process
, current_group
)
6151 Lisp_Object process
, current_group
;
6153 process_send_signal (process
, SIGKILL
, current_group
, 0);
6157 DEFUN ("quit-process", Fquit_process
, Squit_process
, 0, 2, 0,
6158 doc
: /* Send QUIT signal to process PROCESS. May be process or name of one.
6159 See function `interrupt-process' for more details on usage. */)
6160 (process
, current_group
)
6161 Lisp_Object process
, current_group
;
6163 process_send_signal (process
, SIGQUIT
, current_group
, 0);
6167 DEFUN ("stop-process", Fstop_process
, Sstop_process
, 0, 2, 0,
6168 doc
: /* Stop process PROCESS. May be process or name of one.
6169 See function `interrupt-process' for more details on usage.
6170 If PROCESS is a network or serial process, inhibit handling of incoming
6172 (process
, current_group
)
6173 Lisp_Object process
, current_group
;
6176 if (PROCESSP (process
) && (NETCONN_P (process
) || SERIALCONN_P (process
)))
6178 struct Lisp_Process
*p
;
6180 p
= XPROCESS (process
);
6181 if (NILP (p
->command
)
6184 FD_CLR (p
->infd
, &input_wait_mask
);
6185 FD_CLR (p
->infd
, &non_keyboard_wait_mask
);
6192 error ("No SIGTSTP support");
6194 process_send_signal (process
, SIGTSTP
, current_group
, 0);
6199 DEFUN ("continue-process", Fcontinue_process
, Scontinue_process
, 0, 2, 0,
6200 doc
: /* Continue process PROCESS. May be process or name of one.
6201 See function `interrupt-process' for more details on usage.
6202 If PROCESS is a network or serial process, resume handling of incoming
6204 (process
, current_group
)
6205 Lisp_Object process
, current_group
;
6208 if (PROCESSP (process
) && (NETCONN_P (process
) || SERIALCONN_P (process
)))
6210 struct Lisp_Process
*p
;
6212 p
= XPROCESS (process
);
6213 if (EQ (p
->command
, Qt
)
6215 && (!EQ (p
->filter
, Qt
) || EQ (p
->status
, Qlisten
)))
6217 FD_SET (p
->infd
, &input_wait_mask
);
6218 FD_SET (p
->infd
, &non_keyboard_wait_mask
);
6220 if (fd_info
[ p
->infd
].flags
& FILE_SERIAL
)
6221 PurgeComm (fd_info
[ p
->infd
].hnd
, PURGE_RXABORT
| PURGE_RXCLEAR
);
6224 tcflush (p
->infd
, TCIFLUSH
);
6232 process_send_signal (process
, SIGCONT
, current_group
, 0);
6234 error ("No SIGCONT support");
6239 DEFUN ("signal-process", Fsignal_process
, Ssignal_process
,
6240 2, 2, "sProcess (name or number): \nnSignal code: ",
6241 doc
: /* Send PROCESS the signal with code SIGCODE.
6242 PROCESS may also be a number specifying the process id of the
6243 process to signal; in this case, the process need not be a child of
6245 SIGCODE may be an integer, or a symbol whose name is a signal name. */)
6247 Lisp_Object process
, sigcode
;
6251 if (INTEGERP (process
))
6253 pid
= XINT (process
);
6257 if (FLOATP (process
))
6259 pid
= (pid_t
) XFLOAT_DATA (process
);
6263 if (STRINGP (process
))
6266 if (tem
= Fget_process (process
), NILP (tem
))
6268 pid
= XINT (Fstring_to_number (process
, make_number (10)));
6275 process
= get_process (process
);
6280 CHECK_PROCESS (process
);
6281 pid
= XPROCESS (process
)->pid
;
6283 error ("Cannot signal process %s", SDATA (XPROCESS (process
)->name
));
6287 #define parse_signal(NAME, VALUE) \
6288 else if (!xstrcasecmp (name, NAME)) \
6289 XSETINT (sigcode, VALUE)
6291 if (INTEGERP (sigcode
))
6295 unsigned char *name
;
6297 CHECK_SYMBOL (sigcode
);
6298 name
= SDATA (SYMBOL_NAME (sigcode
));
6300 if (!strncmp(name
, "SIG", 3) || !strncmp(name
, "sig", 3))
6306 parse_signal ("usr1", SIGUSR1
);
6309 parse_signal ("usr2", SIGUSR2
);
6312 parse_signal ("term", SIGTERM
);
6315 parse_signal ("hup", SIGHUP
);
6318 parse_signal ("int", SIGINT
);
6321 parse_signal ("quit", SIGQUIT
);
6324 parse_signal ("ill", SIGILL
);
6327 parse_signal ("abrt", SIGABRT
);
6330 parse_signal ("emt", SIGEMT
);
6333 parse_signal ("kill", SIGKILL
);
6336 parse_signal ("fpe", SIGFPE
);
6339 parse_signal ("bus", SIGBUS
);
6342 parse_signal ("segv", SIGSEGV
);
6345 parse_signal ("sys", SIGSYS
);
6348 parse_signal ("pipe", SIGPIPE
);
6351 parse_signal ("alrm", SIGALRM
);
6354 parse_signal ("urg", SIGURG
);
6357 parse_signal ("stop", SIGSTOP
);
6360 parse_signal ("tstp", SIGTSTP
);
6363 parse_signal ("cont", SIGCONT
);
6366 parse_signal ("chld", SIGCHLD
);
6369 parse_signal ("ttin", SIGTTIN
);
6372 parse_signal ("ttou", SIGTTOU
);
6375 parse_signal ("io", SIGIO
);
6378 parse_signal ("xcpu", SIGXCPU
);
6381 parse_signal ("xfsz", SIGXFSZ
);
6384 parse_signal ("vtalrm", SIGVTALRM
);
6387 parse_signal ("prof", SIGPROF
);
6390 parse_signal ("winch", SIGWINCH
);
6393 parse_signal ("info", SIGINFO
);
6396 error ("Undefined signal name %s", name
);
6401 return make_number (kill (pid
, XINT (sigcode
)));
6404 DEFUN ("process-send-eof", Fprocess_send_eof
, Sprocess_send_eof
, 0, 1, 0,
6405 doc
: /* Make PROCESS see end-of-file in its input.
6406 EOF comes after any text already sent to it.
6407 PROCESS may be a process, a buffer, the name of a process or buffer, or
6408 nil, indicating the current buffer's process.
6409 If PROCESS is a network connection, or is a process communicating
6410 through a pipe (as opposed to a pty), then you cannot send any more
6411 text to PROCESS after you call this function.
6412 If PROCESS is a serial process, wait until all output written to the
6413 process has been transmitted to the serial port. */)
6415 Lisp_Object process
;
6418 struct coding_system
*coding
;
6420 if (DATAGRAM_CONN_P (process
))
6423 proc
= get_process (process
);
6424 coding
= proc_encode_coding_system
[XPROCESS (proc
)->outfd
];
6426 /* Make sure the process is really alive. */
6427 if (XPROCESS (proc
)->raw_status_new
)
6428 update_status (XPROCESS (proc
));
6429 if (! EQ (XPROCESS (proc
)->status
, Qrun
))
6430 error ("Process %s not running", SDATA (XPROCESS (proc
)->name
));
6432 if (CODING_REQUIRE_FLUSHING (coding
))
6434 coding
->mode
|= CODING_MODE_LAST_BLOCK
;
6435 send_process (proc
, "", 0, Qnil
);
6438 if (XPROCESS (proc
)->pty_flag
)
6439 send_process (proc
, "\004", 1, Qnil
);
6440 else if (EQ (XPROCESS (proc
)->type
, Qserial
))
6443 if (tcdrain (XPROCESS (proc
)->outfd
) != 0)
6444 error ("tcdrain() failed: %s", emacs_strerror (errno
));
6446 /* Do nothing on Windows because writes are blocking. */
6450 int old_outfd
, new_outfd
;
6452 #ifdef HAVE_SHUTDOWN
6453 /* If this is a network connection, or socketpair is used
6454 for communication with the subprocess, call shutdown to cause EOF.
6455 (In some old system, shutdown to socketpair doesn't work.
6456 Then we just can't win.) */
6457 if (EQ (XPROCESS (proc
)->type
, Qnetwork
)
6458 || XPROCESS (proc
)->outfd
== XPROCESS (proc
)->infd
)
6459 shutdown (XPROCESS (proc
)->outfd
, 1);
6460 /* In case of socketpair, outfd == infd, so don't close it. */
6461 if (XPROCESS (proc
)->outfd
!= XPROCESS (proc
)->infd
)
6462 emacs_close (XPROCESS (proc
)->outfd
);
6463 #else /* not HAVE_SHUTDOWN */
6464 emacs_close (XPROCESS (proc
)->outfd
);
6465 #endif /* not HAVE_SHUTDOWN */
6466 new_outfd
= emacs_open (NULL_DEVICE
, O_WRONLY
, 0);
6469 old_outfd
= XPROCESS (proc
)->outfd
;
6471 if (!proc_encode_coding_system
[new_outfd
])
6472 proc_encode_coding_system
[new_outfd
]
6473 = (struct coding_system
*) xmalloc (sizeof (struct coding_system
));
6474 bcopy (proc_encode_coding_system
[old_outfd
],
6475 proc_encode_coding_system
[new_outfd
],
6476 sizeof (struct coding_system
));
6477 bzero (proc_encode_coding_system
[old_outfd
],
6478 sizeof (struct coding_system
));
6480 XPROCESS (proc
)->outfd
= new_outfd
;
6485 /* Kill all processes associated with `buffer'.
6486 If `buffer' is nil, kill all processes */
6489 kill_buffer_processes (buffer
)
6492 Lisp_Object tail
, proc
;
6494 for (tail
= Vprocess_alist
; CONSP (tail
); tail
= XCDR (tail
))
6496 proc
= XCDR (XCAR (tail
));
6498 && (NILP (buffer
) || EQ (XPROCESS (proc
)->buffer
, buffer
)))
6500 if (NETCONN_P (proc
) || SERIALCONN_P (proc
))
6501 Fdelete_process (proc
);
6502 else if (XPROCESS (proc
)->infd
>= 0)
6503 process_send_signal (proc
, SIGHUP
, Qnil
, 1);
6508 /* On receipt of a signal that a child status has changed, loop asking
6509 about children with changed statuses until the system says there
6512 All we do is change the status; we do not run sentinels or print
6513 notifications. That is saved for the next time keyboard input is
6514 done, in order to avoid timing errors.
6516 ** WARNING: this can be called during garbage collection.
6517 Therefore, it must not be fooled by the presence of mark bits in
6520 ** USG WARNING: Although it is not obvious from the documentation
6521 in signal(2), on a USG system the SIGCLD handler MUST NOT call
6522 signal() before executing at least one wait(), otherwise the
6523 handler will be called again, resulting in an infinite loop. The
6524 relevant portion of the documentation reads "SIGCLD signals will be
6525 queued and the signal-catching function will be continually
6526 reentered until the queue is empty". Invoking signal() causes the
6527 kernel to reexamine the SIGCLD queue. Fred Fish, UniSoft Systems
6530 ** Malloc WARNING: This should never call malloc either directly or
6531 indirectly; if it does, that is a bug */
6535 sigchld_handler (signo
)
6538 int old_errno
= errno
;
6540 register struct Lisp_Process
*p
;
6541 extern EMACS_TIME
*input_available_clear_time
;
6543 SIGNAL_THREAD_CHECK (signo
);
6554 #endif /* no WUNTRACED */
6555 /* Keep trying to get a status until we get a definitive result. */
6559 pid
= wait3 (&w
, WNOHANG
| WUNTRACED
, 0);
6561 while (pid
< 0 && errno
== EINTR
);
6565 /* PID == 0 means no processes found, PID == -1 means a real
6566 failure. We have done all our job, so return. */
6568 /* USG systems forget handlers when they are used;
6569 must reestablish each time */
6570 #if defined (USG) && !defined (POSIX_SIGNALS)
6571 signal (signo
, sigchld_handler
); /* WARNING - must come after wait3() */
6578 #endif /* no WNOHANG */
6580 /* Find the process that signaled us, and record its status. */
6582 /* The process can have been deleted by Fdelete_process. */
6583 for (tail
= deleted_pid_list
; CONSP (tail
); tail
= XCDR (tail
))
6585 Lisp_Object xpid
= XCAR (tail
);
6586 if ((INTEGERP (xpid
) && pid
== (pid_t
) XINT (xpid
))
6587 || (FLOATP (xpid
) && pid
== (pid_t
) XFLOAT_DATA (xpid
)))
6589 XSETCAR (tail
, Qnil
);
6590 goto sigchld_end_of_loop
;
6594 /* Otherwise, if it is asynchronous, it is in Vprocess_alist. */
6596 for (tail
= Vprocess_alist
; CONSP (tail
); tail
= XCDR (tail
))
6598 proc
= XCDR (XCAR (tail
));
6599 p
= XPROCESS (proc
);
6600 if (EQ (p
->type
, Qreal
) && p
->pid
== pid
)
6605 /* Look for an asynchronous process whose pid hasn't been filled
6608 for (tail
= Vprocess_alist
; CONSP (tail
); tail
= XCDR (tail
))
6610 proc
= XCDR (XCAR (tail
));
6611 p
= XPROCESS (proc
);
6617 /* Change the status of the process that was found. */
6620 int clear_desc_flag
= 0;
6622 p
->tick
= ++process_tick
;
6624 p
->raw_status_new
= 1;
6626 /* If process has terminated, stop waiting for its output. */
6627 if ((WIFSIGNALED (w
) || WIFEXITED (w
))
6629 clear_desc_flag
= 1;
6631 /* We use clear_desc_flag to avoid a compiler bug in Microsoft C. */
6632 if (clear_desc_flag
)
6634 FD_CLR (p
->infd
, &input_wait_mask
);
6635 FD_CLR (p
->infd
, &non_keyboard_wait_mask
);
6638 /* Tell wait_reading_process_output that it needs to wake up and
6640 if (input_available_clear_time
)
6641 EMACS_SET_SECS_USECS (*input_available_clear_time
, 0, 0);
6644 /* There was no asynchronous process found for that pid: we have
6645 a synchronous process. */
6648 synch_process_alive
= 0;
6650 /* Report the status of the synchronous process. */
6652 synch_process_retcode
= WRETCODE (w
);
6653 else if (WIFSIGNALED (w
))
6654 synch_process_termsig
= WTERMSIG (w
);
6656 /* Tell wait_reading_process_output that it needs to wake up and
6658 if (input_available_clear_time
)
6659 EMACS_SET_SECS_USECS (*input_available_clear_time
, 0, 0);
6662 sigchld_end_of_loop
:
6665 /* On some systems, we must return right away.
6666 If any more processes want to signal us, we will
6668 Otherwise (on systems that have WNOHANG), loop around
6669 to use up all the processes that have something to tell us. */
6670 #if (defined WINDOWSNT \
6671 || (defined USG && !defined GNU_LINUX \
6672 && !(defined HPUX && defined WNOHANG)))
6673 #if defined (USG) && ! defined (POSIX_SIGNALS)
6674 signal (signo
, sigchld_handler
);
6678 #endif /* USG, but not HPUX with WNOHANG */
6681 #endif /* SIGCHLD */
6685 exec_sentinel_unwind (data
)
6688 XPROCESS (XCAR (data
))->sentinel
= XCDR (data
);
6693 exec_sentinel_error_handler (error
)
6696 cmd_error_internal (error
, "error in process sentinel: ");
6698 update_echo_area ();
6699 Fsleep_for (make_number (2), Qnil
);
6704 exec_sentinel (proc
, reason
)
6705 Lisp_Object proc
, reason
;
6707 Lisp_Object sentinel
, obuffer
, odeactivate
, okeymap
;
6708 register struct Lisp_Process
*p
= XPROCESS (proc
);
6709 int count
= SPECPDL_INDEX ();
6710 int outer_running_asynch_code
= running_asynch_code
;
6711 int waiting
= waiting_for_user_input_p
;
6713 if (inhibit_sentinels
)
6716 /* No need to gcpro these, because all we do with them later
6717 is test them for EQness, and none of them should be a string. */
6718 odeactivate
= Vdeactivate_mark
;
6719 XSETBUFFER (obuffer
, current_buffer
);
6720 okeymap
= current_buffer
->keymap
;
6722 sentinel
= p
->sentinel
;
6723 if (NILP (sentinel
))
6726 /* Zilch the sentinel while it's running, to avoid recursive invocations;
6727 assure that it gets restored no matter how the sentinel exits. */
6729 record_unwind_protect (exec_sentinel_unwind
, Fcons (proc
, sentinel
));
6730 /* Inhibit quit so that random quits don't screw up a running filter. */
6731 specbind (Qinhibit_quit
, Qt
);
6732 specbind (Qlast_nonmenu_event
, Qt
);
6734 /* In case we get recursively called,
6735 and we already saved the match data nonrecursively,
6736 save the same match data in safely recursive fashion. */
6737 if (outer_running_asynch_code
)
6740 tem
= Fmatch_data (Qnil
, Qnil
, Qnil
);
6741 restore_search_regs ();
6742 record_unwind_save_match_data ();
6743 Fset_match_data (tem
, Qt
);
6746 /* For speed, if a search happens within this code,
6747 save the match data in a special nonrecursive fashion. */
6748 running_asynch_code
= 1;
6750 internal_condition_case_1 (read_process_output_call
,
6752 Fcons (proc
, Fcons (reason
, Qnil
))),
6753 !NILP (Vdebug_on_error
) ? Qnil
: Qerror
,
6754 exec_sentinel_error_handler
);
6756 /* If we saved the match data nonrecursively, restore it now. */
6757 restore_search_regs ();
6758 running_asynch_code
= outer_running_asynch_code
;
6760 Vdeactivate_mark
= odeactivate
;
6762 /* Restore waiting_for_user_input_p as it was
6763 when we were called, in case the filter clobbered it. */
6764 waiting_for_user_input_p
= waiting
;
6767 if (! EQ (Fcurrent_buffer (), obuffer
)
6768 || ! EQ (current_buffer
->keymap
, okeymap
))
6770 /* But do it only if the caller is actually going to read events.
6771 Otherwise there's no need to make him wake up, and it could
6772 cause trouble (for example it would make sit_for return). */
6773 if (waiting_for_user_input_p
== -1)
6774 record_asynch_buffer_change ();
6776 unbind_to (count
, Qnil
);
6779 /* Report all recent events of a change in process status
6780 (either run the sentinel or output a message).
6781 This is usually done while Emacs is waiting for keyboard input
6782 but can be done at other times. */
6785 status_notify (deleting_process
)
6786 struct Lisp_Process
*deleting_process
;
6788 register Lisp_Object proc
, buffer
;
6789 Lisp_Object tail
, msg
;
6790 struct gcpro gcpro1
, gcpro2
;
6794 /* We need to gcpro tail; if read_process_output calls a filter
6795 which deletes a process and removes the cons to which tail points
6796 from Vprocess_alist, and then causes a GC, tail is an unprotected
6800 /* Set this now, so that if new processes are created by sentinels
6801 that we run, we get called again to handle their status changes. */
6802 update_tick
= process_tick
;
6804 for (tail
= Vprocess_alist
; CONSP (tail
); tail
= XCDR (tail
))
6807 register struct Lisp_Process
*p
;
6809 proc
= Fcdr (XCAR (tail
));
6810 p
= XPROCESS (proc
);
6812 if (p
->tick
!= p
->update_tick
)
6814 p
->update_tick
= p
->tick
;
6816 /* If process is still active, read any output that remains. */
6817 while (! EQ (p
->filter
, Qt
)
6818 && ! EQ (p
->status
, Qconnect
)
6819 && ! EQ (p
->status
, Qlisten
)
6820 /* Network or serial process not stopped: */
6821 && ! EQ (p
->command
, Qt
)
6823 && p
!= deleting_process
6824 && read_process_output (proc
, p
->infd
) > 0);
6828 /* Get the text to use for the message. */
6829 if (p
->raw_status_new
)
6831 msg
= status_message (p
);
6833 /* If process is terminated, deactivate it or delete it. */
6835 if (CONSP (p
->status
))
6836 symbol
= XCAR (p
->status
);
6838 if (EQ (symbol
, Qsignal
) || EQ (symbol
, Qexit
)
6839 || EQ (symbol
, Qclosed
))
6841 if (delete_exited_processes
)
6842 remove_process (proc
);
6844 deactivate_process (proc
);
6847 /* The actions above may have further incremented p->tick.
6848 So set p->update_tick again
6849 so that an error in the sentinel will not cause
6850 this code to be run again. */
6851 p
->update_tick
= p
->tick
;
6852 /* Now output the message suitably. */
6853 if (!NILP (p
->sentinel
))
6854 exec_sentinel (proc
, msg
);
6855 /* Don't bother with a message in the buffer
6856 when a process becomes runnable. */
6857 else if (!EQ (symbol
, Qrun
) && !NILP (buffer
))
6859 Lisp_Object ro
, tem
;
6860 struct buffer
*old
= current_buffer
;
6861 int opoint
, opoint_byte
;
6862 int before
, before_byte
;
6864 ro
= XBUFFER (buffer
)->read_only
;
6866 /* Avoid error if buffer is deleted
6867 (probably that's why the process is dead, too) */
6868 if (NILP (XBUFFER (buffer
)->name
))
6870 Fset_buffer (buffer
);
6873 opoint_byte
= PT_BYTE
;
6874 /* Insert new output into buffer
6875 at the current end-of-output marker,
6876 thus preserving logical ordering of input and output. */
6877 if (XMARKER (p
->mark
)->buffer
)
6878 Fgoto_char (p
->mark
);
6880 SET_PT_BOTH (ZV
, ZV_BYTE
);
6883 before_byte
= PT_BYTE
;
6885 tem
= current_buffer
->read_only
;
6886 current_buffer
->read_only
= Qnil
;
6887 insert_string ("\nProcess ");
6888 Finsert (1, &p
->name
);
6889 insert_string (" ");
6891 current_buffer
->read_only
= tem
;
6892 set_marker_both (p
->mark
, p
->buffer
, PT
, PT_BYTE
);
6894 if (opoint
>= before
)
6895 SET_PT_BOTH (opoint
+ (PT
- before
),
6896 opoint_byte
+ (PT_BYTE
- before_byte
));
6898 SET_PT_BOTH (opoint
, opoint_byte
);
6900 set_buffer_internal (old
);
6905 update_mode_lines
++; /* in case buffers use %s in mode-line-format */
6906 redisplay_preserve_echo_area (13);
6912 DEFUN ("set-process-coding-system", Fset_process_coding_system
,
6913 Sset_process_coding_system
, 1, 3, 0,
6914 doc
: /* Set coding systems of PROCESS to DECODING and ENCODING.
6915 DECODING will be used to decode subprocess output and ENCODING to
6916 encode subprocess input. */)
6917 (process
, decoding
, encoding
)
6918 register Lisp_Object process
, decoding
, encoding
;
6920 register struct Lisp_Process
*p
;
6922 CHECK_PROCESS (process
);
6923 p
= XPROCESS (process
);
6925 error ("Input file descriptor of %s closed", SDATA (p
->name
));
6927 error ("Output file descriptor of %s closed", SDATA (p
->name
));
6928 Fcheck_coding_system (decoding
);
6929 Fcheck_coding_system (encoding
);
6930 encoding
= coding_inherit_eol_type (encoding
, Qnil
);
6931 p
->decode_coding_system
= decoding
;
6932 p
->encode_coding_system
= encoding
;
6933 setup_process_coding_systems (process
);
6938 DEFUN ("process-coding-system",
6939 Fprocess_coding_system
, Sprocess_coding_system
, 1, 1, 0,
6940 doc
: /* Return a cons of coding systems for decoding and encoding of PROCESS. */)
6942 register Lisp_Object process
;
6944 CHECK_PROCESS (process
);
6945 return Fcons (XPROCESS (process
)->decode_coding_system
,
6946 XPROCESS (process
)->encode_coding_system
);
6949 DEFUN ("set-process-filter-multibyte", Fset_process_filter_multibyte
,
6950 Sset_process_filter_multibyte
, 2, 2, 0,
6951 doc
: /* Set multibyteness of the strings given to PROCESS's filter.
6952 If FLAG is non-nil, the filter is given multibyte strings.
6953 If FLAG is nil, the filter is given unibyte strings. In this case,
6954 all character code conversion except for end-of-line conversion is
6957 Lisp_Object process
, flag
;
6959 register struct Lisp_Process
*p
;
6961 CHECK_PROCESS (process
);
6962 p
= XPROCESS (process
);
6964 p
->decode_coding_system
= raw_text_coding_system (p
->decode_coding_system
);
6965 setup_process_coding_systems (process
);
6970 DEFUN ("process-filter-multibyte-p", Fprocess_filter_multibyte_p
,
6971 Sprocess_filter_multibyte_p
, 1, 1, 0,
6972 doc
: /* Return t if a multibyte string is given to PROCESS's filter.*/)
6974 Lisp_Object process
;
6976 register struct Lisp_Process
*p
;
6977 struct coding_system
*coding
;
6979 CHECK_PROCESS (process
);
6980 p
= XPROCESS (process
);
6981 coding
= proc_decode_coding_system
[p
->infd
];
6982 return (CODING_FOR_UNIBYTE (coding
) ? Qnil
: Qt
);
6987 /* Add DESC to the set of keyboard input descriptors. */
6990 add_keyboard_wait_descriptor (desc
)
6993 FD_SET (desc
, &input_wait_mask
);
6994 FD_SET (desc
, &non_process_wait_mask
);
6995 if (desc
> max_keyboard_desc
)
6996 max_keyboard_desc
= desc
;
6999 static int add_gpm_wait_descriptor_called_flag
;
7002 add_gpm_wait_descriptor (desc
)
7005 if (! add_gpm_wait_descriptor_called_flag
)
7006 FD_CLR (0, &input_wait_mask
);
7007 add_gpm_wait_descriptor_called_flag
= 1;
7008 FD_SET (desc
, &input_wait_mask
);
7009 FD_SET (desc
, &gpm_wait_mask
);
7010 if (desc
> max_gpm_desc
)
7011 max_gpm_desc
= desc
;
7014 /* From now on, do not expect DESC to give keyboard input. */
7017 delete_keyboard_wait_descriptor (desc
)
7021 int lim
= max_keyboard_desc
;
7023 FD_CLR (desc
, &input_wait_mask
);
7024 FD_CLR (desc
, &non_process_wait_mask
);
7026 if (desc
== max_keyboard_desc
)
7027 for (fd
= 0; fd
< lim
; fd
++)
7028 if (FD_ISSET (fd
, &input_wait_mask
)
7029 && !FD_ISSET (fd
, &non_keyboard_wait_mask
)
7030 && !FD_ISSET (fd
, &gpm_wait_mask
))
7031 max_keyboard_desc
= fd
;
7035 delete_gpm_wait_descriptor (desc
)
7039 int lim
= max_gpm_desc
;
7041 FD_CLR (desc
, &input_wait_mask
);
7042 FD_CLR (desc
, &non_process_wait_mask
);
7044 if (desc
== max_gpm_desc
)
7045 for (fd
= 0; fd
< lim
; fd
++)
7046 if (FD_ISSET (fd
, &input_wait_mask
)
7047 && !FD_ISSET (fd
, &non_keyboard_wait_mask
)
7048 && !FD_ISSET (fd
, &non_process_wait_mask
))
7052 /* Return nonzero if *MASK has a bit set
7053 that corresponds to one of the keyboard input descriptors. */
7056 keyboard_bit_set (mask
)
7061 for (fd
= 0; fd
<= max_keyboard_desc
; fd
++)
7062 if (FD_ISSET (fd
, mask
) && FD_ISSET (fd
, &input_wait_mask
)
7063 && !FD_ISSET (fd
, &non_keyboard_wait_mask
))
7069 /* Enumeration of and access to system processes a-la ps(1). */
7073 /* Process enumeration and access via /proc. */
7076 procfs_list_system_processes ()
7078 Lisp_Object procdir
, match
, proclist
, next
;
7079 struct gcpro gcpro1
, gcpro2
;
7080 register Lisp_Object tail
;
7082 GCPRO2 (procdir
, match
);
7083 /* For every process on the system, there's a directory in the
7084 "/proc" pseudo-directory whose name is the numeric ID of that
7086 procdir
= build_string ("/proc");
7087 match
= build_string ("[0-9]+");
7088 proclist
= directory_files_internal (procdir
, Qnil
, match
, Qt
, 0, Qnil
);
7090 /* `proclist' gives process IDs as strings. Destructively convert
7091 each string into a number. */
7092 for (tail
= proclist
; CONSP (tail
); tail
= next
)
7095 XSETCAR (tail
, Fstring_to_number (XCAR (tail
), Qnil
));
7099 /* directory_files_internal returns the files in reverse order; undo
7101 proclist
= Fnreverse (proclist
);
7106 time_from_jiffies (unsigned long long tval
, long hz
,
7107 time_t *sec
, unsigned *usec
)
7109 unsigned long long ullsec
;
7113 tval
-= ullsec
* hz
;
7114 /* Careful: if HZ > 1 million, then integer division by it yields zero. */
7116 *usec
= tval
* 1000000 / hz
;
7118 *usec
= tval
/ (hz
/ 1000000);
7122 ltime_from_jiffies (unsigned long long tval
, long hz
)
7127 time_from_jiffies (tval
, hz
, &sec
, &usec
);
7129 return list3 (make_number ((sec
>> 16) & 0xffff),
7130 make_number (sec
& 0xffff),
7131 make_number (usec
));
7135 get_up_time (time_t *sec
, unsigned *usec
)
7142 fup
= fopen ("/proc/uptime", "r");
7146 double uptime
, idletime
;
7148 /* The numbers in /proc/uptime use C-locale decimal point, but
7149 we already set ourselves to the C locale (see `fixup_locale'
7151 if (2 <= fscanf (fup
, "%lf %lf", &uptime
, &idletime
))
7154 *usec
= (uptime
- *sec
) * 1000000;
7161 #define MAJOR(d) (((unsigned)(d) >> 8) & 0xfff)
7162 #define MINOR(d) (((unsigned)(d) & 0xff) | (((unsigned)(d) & 0xfff00000) >> 12))
7165 procfs_ttyname (rdev
)
7168 char name
[PATH_MAX
];
7171 fdev
= fopen ("/proc/tty/drivers", "r");
7176 unsigned long minor_beg
, minor_end
;
7177 char minor
[25]; /* 2 32-bit numbers + dash */
7180 while (!feof (fdev
) && !ferror (fdev
))
7182 if (3 <= fscanf (fdev
, "%*s %s %u %s %*s\n", name
, &major
, minor
)
7183 && major
== MAJOR (rdev
))
7185 minor_beg
= strtoul (minor
, &endp
, 0);
7187 minor_end
= minor_beg
;
7188 else if (*endp
== '-')
7189 minor_end
= strtoul (endp
+ 1, &endp
, 0);
7193 if (MINOR (rdev
) >= minor_beg
&& MINOR (rdev
) <= minor_end
)
7195 sprintf (name
+ strlen (name
), "%lu", MINOR (rdev
));
7203 return build_string (name
);
7206 static unsigned long
7207 procfs_get_total_memory (void)
7210 unsigned long retval
= 2 * 1024 * 1024; /* default: 2GB */
7213 fmem
= fopen ("/proc/meminfo", "r");
7217 unsigned long entry_value
;
7218 char entry_name
[20]; /* the longest I saw is 13+1 */
7220 while (!feof (fmem
) && !ferror (fmem
))
7222 if (2 <= fscanf (fmem
, "%s %lu kB\n", entry_name
, &entry_value
)
7223 && strcmp (entry_name
, "MemTotal:") == 0)
7225 retval
= entry_value
;
7236 procfs_system_process_attributes (pid
)
7239 char procfn
[PATH_MAX
], fn
[PATH_MAX
];
7243 long clocks_per_sec
;
7245 char procbuf
[1025], *p
, *q
;
7249 char *cmdline
= NULL
;
7252 int proc_id
, ppid
, uid
, gid
, pgrp
, sess
, tty
, tpgid
, thcount
;
7253 unsigned long long utime
, stime
, cutime
, cstime
, start
;
7254 long priority
, nice
, rss
;
7255 unsigned long minflt
, majflt
, cminflt
, cmajflt
, vsize
;
7258 EMACS_TIME tnow
, tstart
, tboot
, telapsed
,ttotal
;
7260 Lisp_Object attrs
= Qnil
;
7261 Lisp_Object cmd_str
, decoded_cmd
, tem
;
7262 struct gcpro gcpro1
, gcpro2
;
7263 EMACS_INT uid_eint
, gid_eint
;
7265 CHECK_NUMBER_OR_FLOAT (pid
);
7266 proc_id
= FLOATP (pid
) ? XFLOAT_DATA (pid
) : XINT (pid
);
7267 sprintf (procfn
, "/proc/%lu", proc_id
);
7268 if (stat (procfn
, &st
) < 0)
7271 GCPRO2 (attrs
, decoded_cmd
);
7275 /* Use of EMACS_INT stops GCC whining about limited range of data type. */
7277 attrs
= Fcons (Fcons (Qeuid
, make_fixnum_or_float (uid_eint
)), attrs
);
7279 pw
= (struct passwd
*) getpwuid (uid
);
7282 attrs
= Fcons (Fcons (Quser
, build_string (pw
->pw_name
)), attrs
);
7286 attrs
= Fcons (Fcons (Qegid
, make_fixnum_or_float (gid_eint
)), attrs
);
7288 gr
= (struct group
*) getgrgid (gid
);
7291 attrs
= Fcons (Fcons (Qgroup
, build_string (gr
->gr_name
)), attrs
);
7293 strcpy (fn
, procfn
);
7294 procfn_end
= fn
+ strlen (fn
);
7295 strcpy (procfn_end
, "/stat");
7296 fd
= emacs_open (fn
, O_RDONLY
, 0);
7297 if (fd
>= 0 && (nread
= emacs_read (fd
, procbuf
, sizeof(procbuf
) - 1)) > 0)
7299 procbuf
[nread
] = '\0';
7302 p
= strchr (p
, '(') + 1;
7303 q
= strchr (p
, ')');
7307 memcpy (cmd
, p
, q
- p
);
7311 strcpy (cmd
, "???");
7312 /* Command name is encoded in locale-coding-system; decode it. */
7313 cmd_str
= make_unibyte_string (cmd
, q
? q
- p
: 3);
7314 decoded_cmd
= code_convert_string_norecord (cmd_str
,
7315 Vlocale_coding_system
, 0);
7316 attrs
= Fcons (Fcons (Qcomm
, decoded_cmd
), attrs
);
7320 EMACS_INT ppid_eint
, pgrp_eint
, sess_eint
, tpgid_eint
, thcount_eint
;
7322 /* state ppid pgrp sess tty tpgid . minflt cminflt majflt cmajflt utime stime cutime cstime priority nice thcount . start vsize rss */
7323 sscanf (p
, "%c %d %d %d %d %d %*u %lu %lu %lu %lu %Lu %Lu %Lu %Lu %ld %ld %d %*d %Lu %lu %ld",
7324 &c
, &ppid
, &pgrp
, &sess
, &tty
, &tpgid
,
7325 &minflt
, &cminflt
, &majflt
, &cmajflt
,
7326 &utime
, &stime
, &cutime
, &cstime
,
7327 &priority
, &nice
, &thcount
, &start
, &vsize
, &rss
);
7332 state_str
[1] = '\0';
7333 tem
= build_string (state_str
);
7334 attrs
= Fcons (Fcons (Qstate
, tem
), attrs
);
7336 /* Stops GCC whining about limited range of data type. */
7341 thcount_eint
= thcount
;
7342 attrs
= Fcons (Fcons (Qppid
, make_fixnum_or_float (ppid_eint
)), attrs
);
7343 attrs
= Fcons (Fcons (Qpgrp
, make_fixnum_or_float (pgrp_eint
)), attrs
);
7344 attrs
= Fcons (Fcons (Qsess
, make_fixnum_or_float (sess_eint
)), attrs
);
7345 attrs
= Fcons (Fcons (Qttname
, procfs_ttyname (tty
)), attrs
);
7346 attrs
= Fcons (Fcons (Qtpgid
, make_fixnum_or_float (tpgid_eint
)), attrs
);
7347 attrs
= Fcons (Fcons (Qminflt
, make_fixnum_or_float (minflt
)), attrs
);
7348 attrs
= Fcons (Fcons (Qmajflt
, make_fixnum_or_float (majflt
)), attrs
);
7349 attrs
= Fcons (Fcons (Qcminflt
, make_fixnum_or_float (cminflt
)), attrs
);
7350 attrs
= Fcons (Fcons (Qcmajflt
, make_fixnum_or_float (cmajflt
)), attrs
);
7351 clocks_per_sec
= sysconf (_SC_CLK_TCK
);
7352 if (clocks_per_sec
< 0)
7353 clocks_per_sec
= 100;
7354 attrs
= Fcons (Fcons (Qutime
,
7355 ltime_from_jiffies (utime
, clocks_per_sec
)),
7357 attrs
= Fcons (Fcons (Qstime
,
7358 ltime_from_jiffies (stime
, clocks_per_sec
)),
7360 attrs
= Fcons (Fcons (Qcutime
,
7361 ltime_from_jiffies (cutime
, clocks_per_sec
)),
7363 attrs
= Fcons (Fcons (Qcstime
,
7364 ltime_from_jiffies (cstime
, clocks_per_sec
)),
7366 attrs
= Fcons (Fcons (Qpri
, make_number (priority
)), attrs
);
7367 attrs
= Fcons (Fcons (Qnice
, make_number (nice
)), attrs
);
7368 attrs
= Fcons (Fcons (Qthcount
, make_fixnum_or_float (thcount_eint
)), attrs
);
7369 EMACS_GET_TIME (tnow
);
7370 get_up_time (&sec
, &usec
);
7371 EMACS_SET_SECS (telapsed
, sec
);
7372 EMACS_SET_USECS (telapsed
, usec
);
7373 EMACS_SUB_TIME (tboot
, tnow
, telapsed
);
7374 time_from_jiffies (start
, clocks_per_sec
, &sec
, &usec
);
7375 EMACS_SET_SECS (tstart
, sec
);
7376 EMACS_SET_USECS (tstart
, usec
);
7377 EMACS_ADD_TIME (tstart
, tboot
, tstart
);
7378 attrs
= Fcons (Fcons (Qstart
,
7380 ((EMACS_SECS (tstart
) >> 16) & 0xffff),
7382 (EMACS_SECS (tstart
) & 0xffff),
7384 (EMACS_USECS (tstart
)))),
7386 attrs
= Fcons (Fcons (Qvsize
, make_fixnum_or_float (vsize
/1024)), attrs
);
7387 attrs
= Fcons (Fcons (Qrss
, make_fixnum_or_float (4*rss
)), attrs
);
7388 EMACS_SUB_TIME (telapsed
, tnow
, tstart
);
7389 attrs
= Fcons (Fcons (Qetime
,
7391 ((EMACS_SECS (telapsed
) >> 16) & 0xffff),
7393 (EMACS_SECS (telapsed
) & 0xffff),
7395 (EMACS_USECS (telapsed
)))),
7397 time_from_jiffies (utime
+ stime
, clocks_per_sec
, &sec
, &usec
);
7398 pcpu
= (sec
+ usec
/ 1000000.0) / (EMACS_SECS (telapsed
) + EMACS_USECS (telapsed
) / 1000000.0);
7399 attrs
= Fcons (Fcons (Qpcpu
, make_float (pcpu
)), attrs
);
7400 pmem
= 4.0 * 100 * rss
/ procfs_get_total_memory ();
7403 attrs
= Fcons (Fcons (Qpmem
, make_float (pmem
)), attrs
);
7410 strcpy (procfn_end
, "/cmdline");
7411 fd
= emacs_open (fn
, O_RDONLY
, 0);
7414 for (cmdsize
= 0; emacs_read (fd
, (char *)&c
, 1) == 1; cmdsize
++)
7416 if (isspace (c
) || c
== '\\')
7417 cmdsize
++; /* for later quoting, see below */
7421 cmdline
= xmalloc (cmdsize
+ 1);
7422 lseek (fd
, 0L, SEEK_SET
);
7424 if ((nread
= read (fd
, cmdline
, cmdsize
)) >= 0)
7425 cmdline
[nread
++] = '\0';
7426 /* We don't want trailing null characters. */
7427 for (p
= cmdline
+ nread
- 1; p
> cmdline
&& !*p
; p
--)
7429 for (p
= cmdline
; p
< cmdline
+ nread
; p
++)
7431 /* Escape-quote whitespace and backslashes. */
7432 if (isspace (*p
) || *p
== '\\')
7434 memmove (p
+ 1, p
, nread
- (p
- cmdline
));
7438 else if (*p
== '\0')
7445 cmdsize
= strlen (cmd
) + 2;
7446 cmdline
= xmalloc (cmdsize
+ 1);
7447 strcpy (cmdline
, "[");
7448 strcat (strcat (cmdline
, cmd
), "]");
7451 /* Command line is encoded in locale-coding-system; decode it. */
7452 cmd_str
= make_unibyte_string (cmdline
, cmdsize
);
7453 decoded_cmd
= code_convert_string_norecord (cmd_str
,
7454 Vlocale_coding_system
, 0);
7456 attrs
= Fcons (Fcons (Qargs
, decoded_cmd
), attrs
);
7463 #endif /* !HAVE_PROCFS */
7465 DEFUN ("list-system-processes", Flist_system_processes
, Slist_system_processes
,
7467 doc
: /* Return a list of numerical process IDs of all running processes.
7468 If this functionality is unsupported, return nil.
7470 See `system-process-attributes' for getting attributes of a process
7481 DEFUN ("system-process-attributes", Fsystem_process_attributes
,
7482 Ssystem_process_attributeses
, 1, 1, 0,
7483 doc
: /* Return attributes of the process given by its PID, a number.
7485 Value is an alist where each element is a cons cell of the form
7489 If this functionality is unsupported, the value is nil.
7491 See `list-system-processes' for getting a list of all process IDs.
7493 The attributes that this function may return are listed below,
7494 together with the type of the associated value (in parentheses).
7495 Not all platforms support all of these attributes; unsupported
7496 attributes will not appear in the returned alist.
7497 Unless explicitly indicated otherwise, numbers can have either
7498 integer or floating point values.
7500 euid -- Effective user User ID of the process (number)
7501 user -- User name corresponding to euid (string)
7502 egid -- Effective user Group ID of the process (number)
7503 group -- Group name corresponding to egid (string)
7504 comm -- Command name (executable name only) (string)
7505 state -- Process state code, such as "S", "R", or "T" (string)
7506 ppid -- Parent process ID (number)
7507 pgrp -- Process group ID (number)
7508 sess -- Session ID, i.e. process ID of session leader (number)
7509 ttname -- Controlling tty name (string)
7510 tpgid -- ID of foreground process group on the process's tty (number)
7511 minflt -- number of minor page faults (number)
7512 majflt -- number of major page faults (number)
7513 cminflt -- cumulative number of minor page faults (number)
7514 cmajflt -- cumulative number of major page faults (number)
7515 utime -- user time used by the process, in the (HIGH LOW USEC) format
7516 stime -- system time used by the process, in the (HIGH LOW USEC) format
7517 cutime -- user time used by the process and its children, (HIGH LOW USEC)
7518 cstime -- system time used by the process and its children, (HIGH LOW USEC)
7519 pri -- priority of the process (number)
7520 nice -- nice value of the process (number)
7521 thcount -- process thread count (number)
7522 start -- time the process started, in the (HIGH LOW USEC) format
7523 vsize -- virtual memory size of the process in KB's (number)
7524 rss -- resident set size of the process in KB's (number)
7525 etime -- elapsed time the process is running, in (HIGH LOW USEC) format
7526 pcpu -- percents of CPU time used by the process (floating-point number)
7527 pmem -- percents of total physical memory used by process's resident set
7528 (floating-point number)
7529 args -- command line which invoked the process (string). */)
7535 return PROCATTR (pid
);
7546 inhibit_sentinels
= 0;
7550 if (! noninteractive
|| initialized
)
7552 signal (SIGCHLD
, sigchld_handler
);
7555 FD_ZERO (&input_wait_mask
);
7556 FD_ZERO (&non_keyboard_wait_mask
);
7557 FD_ZERO (&non_process_wait_mask
);
7558 max_process_desc
= 0;
7560 #ifdef NON_BLOCKING_CONNECT
7561 FD_ZERO (&connect_wait_mask
);
7562 num_pending_connects
= 0;
7565 #ifdef ADAPTIVE_READ_BUFFERING
7566 process_output_delay_count
= 0;
7567 process_output_skip
= 0;
7570 /* Don't do this, it caused infinite select loops. The display
7571 method should call add_keyboard_wait_descriptor on stdin if it
7574 FD_SET (0, &input_wait_mask
);
7577 Vprocess_alist
= Qnil
;
7579 deleted_pid_list
= Qnil
;
7581 for (i
= 0; i
< MAXDESC
; i
++)
7583 chan_process
[i
] = Qnil
;
7584 proc_buffered_char
[i
] = -1;
7586 bzero (proc_decode_coding_system
, sizeof proc_decode_coding_system
);
7587 bzero (proc_encode_coding_system
, sizeof proc_encode_coding_system
);
7588 #ifdef DATAGRAM_SOCKETS
7589 bzero (datagram_address
, sizeof datagram_address
);
7594 Lisp_Object subfeatures
= Qnil
;
7595 struct socket_options
*sopt
;
7597 #define ADD_SUBFEATURE(key, val) \
7598 subfeatures = Fcons (Fcons (key, Fcons (val, Qnil)), subfeatures)
7600 #ifdef NON_BLOCKING_CONNECT
7601 ADD_SUBFEATURE (QCnowait
, Qt
);
7603 #ifdef DATAGRAM_SOCKETS
7604 ADD_SUBFEATURE (QCtype
, Qdatagram
);
7606 #ifdef HAVE_LOCAL_SOCKETS
7607 ADD_SUBFEATURE (QCfamily
, Qlocal
);
7609 ADD_SUBFEATURE (QCfamily
, Qipv4
);
7611 ADD_SUBFEATURE (QCfamily
, Qipv6
);
7613 #ifdef HAVE_GETSOCKNAME
7614 ADD_SUBFEATURE (QCservice
, Qt
);
7616 #if defined(O_NONBLOCK) || defined(O_NDELAY)
7617 ADD_SUBFEATURE (QCserver
, Qt
);
7620 for (sopt
= socket_options
; sopt
->name
; sopt
++)
7621 subfeatures
= Fcons (intern (sopt
->name
), subfeatures
);
7623 Fprovide (intern ("make-network-process"), subfeatures
);
7625 #endif /* HAVE_SOCKETS */
7627 #if defined (DARWIN_OS)
7628 /* PTYs are broken on Darwin < 6, but are sometimes useful for interactive
7629 processes. As such, we only change the default value. */
7632 char *release
= get_operating_system_release();
7633 if (!release
|| !release
[0] || (release
[0] < MIN_PTY_KERNEL_VERSION
7634 && release
[1] == '.')) {
7635 Vprocess_connection_type
= Qnil
;
7644 Qprocessp
= intern ("processp");
7645 staticpro (&Qprocessp
);
7646 Qrun
= intern ("run");
7648 Qstop
= intern ("stop");
7650 Qsignal
= intern ("signal");
7651 staticpro (&Qsignal
);
7653 /* Qexit is already staticpro'd by syms_of_eval; don't staticpro it
7656 Qexit = intern ("exit");
7657 staticpro (&Qexit); */
7659 Qopen
= intern ("open");
7661 Qclosed
= intern ("closed");
7662 staticpro (&Qclosed
);
7663 Qconnect
= intern ("connect");
7664 staticpro (&Qconnect
);
7665 Qfailed
= intern ("failed");
7666 staticpro (&Qfailed
);
7667 Qlisten
= intern ("listen");
7668 staticpro (&Qlisten
);
7669 Qlocal
= intern ("local");
7670 staticpro (&Qlocal
);
7671 Qipv4
= intern ("ipv4");
7674 Qipv6
= intern ("ipv6");
7677 Qdatagram
= intern ("datagram");
7678 staticpro (&Qdatagram
);
7680 QCport
= intern (":port");
7681 staticpro (&QCport
);
7682 QCspeed
= intern (":speed");
7683 staticpro (&QCspeed
);
7684 QCprocess
= intern (":process");
7685 staticpro (&QCprocess
);
7687 QCbytesize
= intern (":bytesize");
7688 staticpro (&QCbytesize
);
7689 QCstopbits
= intern (":stopbits");
7690 staticpro (&QCstopbits
);
7691 QCparity
= intern (":parity");
7692 staticpro (&QCparity
);
7693 Qodd
= intern ("odd");
7695 Qeven
= intern ("even");
7697 QCflowcontrol
= intern (":flowcontrol");
7698 staticpro (&QCflowcontrol
);
7699 Qhw
= intern ("hw");
7701 Qsw
= intern ("sw");
7703 QCsummary
= intern (":summary");
7704 staticpro (&QCsummary
);
7706 Qreal
= intern ("real");
7708 Qnetwork
= intern ("network");
7709 staticpro (&Qnetwork
);
7710 Qserial
= intern ("serial");
7711 staticpro (&Qserial
);
7713 QCname
= intern (":name");
7714 staticpro (&QCname
);
7715 QCbuffer
= intern (":buffer");
7716 staticpro (&QCbuffer
);
7717 QChost
= intern (":host");
7718 staticpro (&QChost
);
7719 QCservice
= intern (":service");
7720 staticpro (&QCservice
);
7721 QCtype
= intern (":type");
7722 staticpro (&QCtype
);
7723 QClocal
= intern (":local");
7724 staticpro (&QClocal
);
7725 QCremote
= intern (":remote");
7726 staticpro (&QCremote
);
7727 QCcoding
= intern (":coding");
7728 staticpro (&QCcoding
);
7729 QCserver
= intern (":server");
7730 staticpro (&QCserver
);
7731 QCnowait
= intern (":nowait");
7732 staticpro (&QCnowait
);
7733 QCsentinel
= intern (":sentinel");
7734 staticpro (&QCsentinel
);
7735 QClog
= intern (":log");
7737 QCnoquery
= intern (":noquery");
7738 staticpro (&QCnoquery
);
7739 QCstop
= intern (":stop");
7740 staticpro (&QCstop
);
7741 QCoptions
= intern (":options");
7742 staticpro (&QCoptions
);
7743 QCplist
= intern (":plist");
7744 staticpro (&QCplist
);
7746 Qlast_nonmenu_event
= intern ("last-nonmenu-event");
7747 staticpro (&Qlast_nonmenu_event
);
7749 staticpro (&Vprocess_alist
);
7751 staticpro (&deleted_pid_list
);
7754 Qeuid
= intern ("euid");
7756 Qegid
= intern ("egid");
7758 Quser
= intern ("user");
7760 Qgroup
= intern ("group");
7761 staticpro (&Qgroup
);
7762 Qcomm
= intern ("comm");
7764 Qstate
= intern ("state");
7765 staticpro (&Qstate
);
7766 Qppid
= intern ("ppid");
7768 Qpgrp
= intern ("pgrp");
7770 Qsess
= intern ("sess");
7772 Qttname
= intern ("ttname");
7773 staticpro (&Qttname
);
7774 Qtpgid
= intern ("tpgid");
7775 staticpro (&Qtpgid
);
7776 Qminflt
= intern ("minflt");
7777 staticpro (&Qminflt
);
7778 Qmajflt
= intern ("majflt");
7779 staticpro (&Qmajflt
);
7780 Qcminflt
= intern ("cminflt");
7781 staticpro (&Qcminflt
);
7782 Qcmajflt
= intern ("cmajflt");
7783 staticpro (&Qcmajflt
);
7784 Qutime
= intern ("utime");
7785 staticpro (&Qutime
);
7786 Qstime
= intern ("stime");
7787 staticpro (&Qstime
);
7788 Qcutime
= intern ("cutime");
7789 staticpro (&Qcutime
);
7790 Qcstime
= intern ("cstime");
7791 staticpro (&Qcstime
);
7792 Qpri
= intern ("pri");
7794 Qnice
= intern ("nice");
7796 Qthcount
= intern ("thcount");
7797 staticpro (&Qthcount
);
7798 Qstart
= intern ("start");
7799 staticpro (&Qstart
);
7800 Qvsize
= intern ("vsize");
7801 staticpro (&Qvsize
);
7802 Qrss
= intern ("rss");
7804 Qetime
= intern ("etime");
7805 staticpro (&Qetime
);
7806 Qpcpu
= intern ("pcpu");
7808 Qpmem
= intern ("pmem");
7810 Qargs
= intern ("args");
7813 DEFVAR_BOOL ("delete-exited-processes", &delete_exited_processes
,
7814 doc
: /* *Non-nil means delete processes immediately when they exit.
7815 A value of nil means don't delete them until `list-processes' is run. */);
7817 delete_exited_processes
= 1;
7819 DEFVAR_LISP ("process-connection-type", &Vprocess_connection_type
,
7820 doc
: /* Control type of device used to communicate with subprocesses.
7821 Values are nil to use a pipe, or t or `pty' to use a pty.
7822 The value has no effect if the system has no ptys or if all ptys are busy:
7823 then a pipe is used in any case.
7824 The value takes effect when `start-process' is called. */);
7825 Vprocess_connection_type
= Qt
;
7827 #ifdef ADAPTIVE_READ_BUFFERING
7828 DEFVAR_LISP ("process-adaptive-read-buffering", &Vprocess_adaptive_read_buffering
,
7829 doc
: /* If non-nil, improve receive buffering by delaying after short reads.
7830 On some systems, when Emacs reads the output from a subprocess, the output data
7831 is read in very small blocks, potentially resulting in very poor performance.
7832 This behavior can be remedied to some extent by setting this variable to a
7833 non-nil value, as it will automatically delay reading from such processes, to
7834 allow them to produce more output before Emacs tries to read it.
7835 If the value is t, the delay is reset after each write to the process; any other
7836 non-nil value means that the delay is not reset on write.
7837 The variable takes effect when `start-process' is called. */);
7838 Vprocess_adaptive_read_buffering
= Qt
;
7841 defsubr (&Sprocessp
);
7842 defsubr (&Sget_process
);
7843 defsubr (&Sget_buffer_process
);
7844 defsubr (&Sdelete_process
);
7845 defsubr (&Sprocess_status
);
7846 defsubr (&Sprocess_exit_status
);
7847 defsubr (&Sprocess_id
);
7848 defsubr (&Sprocess_name
);
7849 defsubr (&Sprocess_tty_name
);
7850 defsubr (&Sprocess_command
);
7851 defsubr (&Sset_process_buffer
);
7852 defsubr (&Sprocess_buffer
);
7853 defsubr (&Sprocess_mark
);
7854 defsubr (&Sset_process_filter
);
7855 defsubr (&Sprocess_filter
);
7856 defsubr (&Sset_process_sentinel
);
7857 defsubr (&Sprocess_sentinel
);
7858 defsubr (&Sset_process_window_size
);
7859 defsubr (&Sset_process_inherit_coding_system_flag
);
7860 defsubr (&Sprocess_inherit_coding_system_flag
);
7861 defsubr (&Sset_process_query_on_exit_flag
);
7862 defsubr (&Sprocess_query_on_exit_flag
);
7863 defsubr (&Sprocess_contact
);
7864 defsubr (&Sprocess_plist
);
7865 defsubr (&Sset_process_plist
);
7866 defsubr (&Slist_processes
);
7867 defsubr (&Sprocess_list
);
7868 defsubr (&Sstart_process
);
7870 defsubr (&Sserial_process_configure
);
7871 defsubr (&Smake_serial_process
);
7872 #endif /* HAVE_SERIAL */
7874 defsubr (&Sset_network_process_option
);
7875 defsubr (&Smake_network_process
);
7876 defsubr (&Sformat_network_address
);
7877 #endif /* HAVE_SOCKETS */
7878 #if defined(HAVE_SOCKETS) && defined(HAVE_NET_IF_H) && defined(HAVE_SYS_IOCTL_H)
7880 defsubr (&Snetwork_interface_list
);
7882 #if defined(SIOCGIFADDR) || defined(SIOCGIFHWADDR) || defined(SIOCGIFFLAGS)
7883 defsubr (&Snetwork_interface_info
);
7885 #endif /* HAVE_SOCKETS ... */
7886 #ifdef DATAGRAM_SOCKETS
7887 defsubr (&Sprocess_datagram_address
);
7888 defsubr (&Sset_process_datagram_address
);
7890 defsubr (&Saccept_process_output
);
7891 defsubr (&Sprocess_send_region
);
7892 defsubr (&Sprocess_send_string
);
7893 defsubr (&Sinterrupt_process
);
7894 defsubr (&Skill_process
);
7895 defsubr (&Squit_process
);
7896 defsubr (&Sstop_process
);
7897 defsubr (&Scontinue_process
);
7898 defsubr (&Sprocess_running_child_p
);
7899 defsubr (&Sprocess_send_eof
);
7900 defsubr (&Ssignal_process
);
7901 defsubr (&Swaiting_for_user_input_p
);
7902 defsubr (&Sprocess_type
);
7903 defsubr (&Sset_process_coding_system
);
7904 defsubr (&Sprocess_coding_system
);
7905 defsubr (&Sset_process_filter_multibyte
);
7906 defsubr (&Sprocess_filter_multibyte_p
);
7907 defsubr (&Slist_system_processes
);
7908 defsubr (&Ssystem_process_attributeses
);
7912 #else /* not subprocesses */
7914 #include <sys/types.h>
7916 #include <sys/stat.h>
7919 #ifdef HAVE_UNISTD_H
7924 #include "systime.h"
7925 #include "character.h"
7927 #include "termopts.h"
7928 #include "sysselect.h"
7930 extern int frame_garbaged
;
7932 extern EMACS_TIME
timer_check ();
7933 extern int timers_run
;
7937 /* As described above, except assuming that there are no subprocesses:
7939 Wait for timeout to elapse and/or keyboard input to be available.
7942 timeout in seconds, or
7943 zero for no limit, or
7944 -1 means gobble data immediately available but don't wait for any.
7946 read_kbd is a Lisp_Object:
7947 0 to ignore keyboard input, or
7948 1 to return when input is available, or
7949 -1 means caller will actually read the input, so don't throw to
7952 see full version for other parameters. We know that wait_proc will
7953 always be NULL, since `subprocesses' isn't defined.
7955 do_display != 0 means redisplay should be done to show subprocess
7956 output that arrives.
7958 Return true if we received input from any process. */
7961 wait_reading_process_output (time_limit
, microsecs
, read_kbd
, do_display
,
7962 wait_for_cell
, wait_proc
, just_wait_proc
)
7963 int time_limit
, microsecs
, read_kbd
, do_display
;
7964 Lisp_Object wait_for_cell
;
7965 struct Lisp_Process
*wait_proc
;
7969 EMACS_TIME end_time
, timeout
;
7970 SELECT_TYPE waitchannels
;
7973 /* What does time_limit really mean? */
7974 if (time_limit
|| microsecs
)
7976 EMACS_GET_TIME (end_time
);
7977 EMACS_SET_SECS_USECS (timeout
, time_limit
, microsecs
);
7978 EMACS_ADD_TIME (end_time
, end_time
, timeout
);
7981 /* Turn off periodic alarms (in case they are in use)
7982 and then turn off any other atimers,
7983 because the select emulator uses alarms. */
7985 turn_on_atimers (0);
7989 int timeout_reduced_for_timers
= 0;
7991 /* If calling from keyboard input, do not quit
7992 since we want to return C-g as an input character.
7993 Otherwise, do pending quit if requested. */
7997 /* Exit now if the cell we're waiting for became non-nil. */
7998 if (! NILP (wait_for_cell
) && ! NILP (XCAR (wait_for_cell
)))
8001 /* Compute time from now till when time limit is up */
8002 /* Exit if already run out */
8003 if (time_limit
== -1)
8005 /* -1 specified for timeout means
8006 gobble output available now
8007 but don't wait at all. */
8009 EMACS_SET_SECS_USECS (timeout
, 0, 0);
8011 else if (time_limit
|| microsecs
)
8013 EMACS_GET_TIME (timeout
);
8014 EMACS_SUB_TIME (timeout
, end_time
, timeout
);
8015 if (EMACS_TIME_NEG_P (timeout
))
8020 EMACS_SET_SECS_USECS (timeout
, 100000, 0);
8023 /* If our caller will not immediately handle keyboard events,
8024 run timer events directly.
8025 (Callers that will immediately read keyboard events
8026 call timer_delay on their own.) */
8027 if (NILP (wait_for_cell
))
8029 EMACS_TIME timer_delay
;
8033 int old_timers_run
= timers_run
;
8034 timer_delay
= timer_check (1);
8035 if (timers_run
!= old_timers_run
&& do_display
)
8036 /* We must retry, since a timer may have requeued itself
8037 and that could alter the time delay. */
8038 redisplay_preserve_echo_area (14);
8042 while (!detect_input_pending ());
8044 /* If there is unread keyboard input, also return. */
8046 && requeued_events_pending_p ())
8049 if (! EMACS_TIME_NEG_P (timer_delay
) && time_limit
!= -1)
8051 EMACS_TIME difference
;
8052 EMACS_SUB_TIME (difference
, timer_delay
, timeout
);
8053 if (EMACS_TIME_NEG_P (difference
))
8055 timeout
= timer_delay
;
8056 timeout_reduced_for_timers
= 1;
8061 /* Cause C-g and alarm signals to take immediate action,
8062 and cause input available signals to zero out timeout. */
8064 set_waiting_for_input (&timeout
);
8066 /* Wait till there is something to do. */
8068 if (! read_kbd
&& NILP (wait_for_cell
))
8069 FD_ZERO (&waitchannels
);
8071 FD_SET (0, &waitchannels
);
8073 /* If a frame has been newly mapped and needs updating,
8074 reprocess its display stuff. */
8075 if (frame_garbaged
&& do_display
)
8077 clear_waiting_for_input ();
8078 redisplay_preserve_echo_area (15);
8080 set_waiting_for_input (&timeout
);
8083 if (read_kbd
&& detect_input_pending ())
8086 FD_ZERO (&waitchannels
);
8089 nfds
= select (1, &waitchannels
, (SELECT_TYPE
*)0, (SELECT_TYPE
*)0,
8094 /* Make C-g and alarm signals set flags again */
8095 clear_waiting_for_input ();
8097 /* If we woke up due to SIGWINCH, actually change size now. */
8098 do_pending_window_change (0);
8100 if (time_limit
&& nfds
== 0 && ! timeout_reduced_for_timers
)
8101 /* We waited the full specified time, so return now. */
8106 /* If the system call was interrupted, then go around the
8108 if (xerrno
== EINTR
)
8109 FD_ZERO (&waitchannels
);
8111 error ("select error: %s", emacs_strerror (xerrno
));
8114 else if (nfds
> 0 && (waitchannels
& 1) && interrupt_input
)
8115 /* System sometimes fails to deliver SIGIO. */
8116 kill (getpid (), SIGIO
);
8119 if (read_kbd
&& interrupt_input
&& (waitchannels
& 1))
8120 kill (getpid (), SIGIO
);
8123 /* Check for keyboard input */
8126 && detect_input_pending_run_timers (do_display
))
8128 swallow_events (do_display
);
8129 if (detect_input_pending_run_timers (do_display
))
8133 /* If there is unread keyboard input, also return. */
8135 && requeued_events_pending_p ())
8138 /* If wait_for_cell. check for keyboard input
8139 but don't run any timers.
8140 ??? (It seems wrong to me to check for keyboard
8141 input at all when wait_for_cell, but the code
8142 has been this way since July 1994.
8143 Try changing this after version 19.31.) */
8144 if (! NILP (wait_for_cell
)
8145 && detect_input_pending ())
8147 swallow_events (do_display
);
8148 if (detect_input_pending ())
8152 /* Exit now if the cell we're waiting for became non-nil. */
8153 if (! NILP (wait_for_cell
) && ! NILP (XCAR (wait_for_cell
)))
8163 /* Don't confuse make-docfile by having two doc strings for this function.
8164 make-docfile does not pay attention to #if, for good reason! */
8165 DEFUN ("get-buffer-process", Fget_buffer_process
, Sget_buffer_process
, 1, 1, 0,
8168 register Lisp_Object name
;
8173 /* Don't confuse make-docfile by having two doc strings for this function.
8174 make-docfile does not pay attention to #if, for good reason! */
8175 DEFUN ("process-inherit-coding-system-flag",
8176 Fprocess_inherit_coding_system_flag
, Sprocess_inherit_coding_system_flag
,
8180 register Lisp_Object process
;
8182 /* Ignore the argument and return the value of
8183 inherit-process-coding-system. */
8184 return inherit_process_coding_system
? Qt
: Qnil
;
8187 /* Kill all processes associated with `buffer'.
8188 If `buffer' is nil, kill all processes.
8189 Since we have no subprocesses, this does nothing. */
8192 kill_buffer_processes (buffer
)
8197 DEFUN ("list-system-processes", Flist_system_processes
, Slist_system_processes
,
8199 doc
: /* Return a list of numerical process IDs of all running processes.
8200 If this functionality is unsupported, return nil.
8202 See `system-process-attributes' for getting attributes of a process
8209 DEFUN ("system-process-attributes", Fsystem_process_attributes
,
8210 Ssystem_process_attributeses
, 1, 1, 0,
8211 doc
: /* Return attributes of the process given by its PID, a number.
8213 Value is an alist where each element is a cons cell of the form
8217 If this functionality is unsupported, the value is nil.
8219 See `list-system-processes' for getting a list of all process IDs.
8221 The attributes that this function may return are listed below,
8222 together with the type of the associated value (in parentheses).
8223 Not all platforms support all of these attributes; unsupported
8224 attributes will not appear in the returned alist.
8225 Unless explicitly indicated otherwise, numbers can have either
8226 integer or floating point values.
8228 euid -- Effective user User ID of the process (number)
8229 user -- User name corresponding to euid (string)
8230 egid -- Effective user Group ID of the process (number)
8231 group -- Group name corresponding to egid (string)
8232 comm -- Command name (executable name only) (string)
8233 state -- Process state code, such as "S", "R", or "T" (string)
8234 ppid -- Parent process ID (number)
8235 pgrp -- Process group ID (number)
8236 sess -- Session ID, i.e. process ID of session leader (number)
8237 ttname -- Controlling tty name (string)
8238 tpgid -- ID of foreground process group on the process's tty (number)
8239 minflt -- number of minor page faults (number)
8240 majflt -- number of major page faults (number)
8241 cminflt -- cumulative number of minor page faults (number)
8242 cmajflt -- cumulative number of major page faults (number)
8243 utime -- user time used by the process, in the (HIGH LOW USEC) format
8244 stime -- system time used by the process, in the (HIGH LOW USEC) format
8245 cutime -- user time used by the process and its children, (HIGH LOW USEC)
8246 cstime -- system time used by the process and its children, (HIGH LOW USEC)
8247 pri -- priority of the process (number)
8248 nice -- nice value of the process (number)
8249 thcount -- process thread count (number)
8250 start -- time the process started, in the (HIGH LOW USEC) format
8251 vsize -- virtual memory size of the process in KB's (number)
8252 rss -- resident set size of the process in KB's (number)
8253 etime -- elapsed time the process is running, in (HIGH LOW USEC) format
8254 pcpu -- percents of CPU time used by the process (floating-point number)
8255 pmem -- percents of total physical memory used by process's resident set
8256 (floating-point number)
8257 args -- command line which invoked the process (string). */)
8273 QCtype
= intern (":type");
8274 staticpro (&QCtype
);
8276 defsubr (&Sget_buffer_process
);
8277 defsubr (&Sprocess_inherit_coding_system_flag
);
8278 defsubr (&Slist_system_processes
);
8279 defsubr (&Ssystem_process_attributeses
);
8283 #endif /* not subprocesses */
8285 /* arch-tag: 3706c011-7b9a-4117-bd4f-59e7f701a4c4
8286 (do not change this comment) */