0
|
1 /* Asynchronous subprocess control for XEmacs.
|
|
2 Copyright (C) 1985, 1986, 1987, 1988, 1992, 1993, 1994, 1995
|
|
3 Free Software Foundation, Inc.
|
|
4 Copyright (C) 1995 Sun Microsystems, Inc.
|
|
5 Copyright (C) 1995, 1996 Ben Wing.
|
|
6
|
|
7 This file is part of XEmacs.
|
|
8
|
|
9 XEmacs is free software; you can redistribute it and/or modify it
|
|
10 under the terms of the GNU General Public License as published by the
|
|
11 Free Software Foundation; either version 2, or (at your option) any
|
|
12 later version.
|
|
13
|
|
14 XEmacs is distributed in the hope that it will be useful, but WITHOUT
|
|
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
17 for more details.
|
|
18
|
|
19 You should have received a copy of the GNU General Public License
|
|
20 along with XEmacs; see the file COPYING. If not, write to
|
|
21 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
22 Boston, MA 02111-1307, USA. */
|
|
23
|
|
24 /* Synched up with: Mule 2.0, FSF 19.30. */
|
|
25
|
259
|
26 /* This file has been Mule-ized except for `start-process-internal',
|
|
27 `open-network-stream-internal' and `open-multicast-group-internal'. */
|
0
|
28
|
|
29 #include <config.h>
|
|
30
|
|
31 #if !defined (NO_SUBPROCESSES)
|
|
32
|
|
33 /* The entire file is within this conditional */
|
|
34
|
|
35 #include "lisp.h"
|
|
36
|
|
37 #include "buffer.h"
|
|
38 #include "commands.h"
|
|
39 #include "events.h"
|
|
40 #include "frame.h"
|
|
41 #include "insdel.h"
|
|
42 #include "lstream.h"
|
|
43 #include "opaque.h"
|
|
44 #include "process.h"
|
|
45 #include "sysdep.h"
|
|
46 #include "window.h"
|
259
|
47 #ifdef FILE_CODING
|
|
48 #include "file-coding.h"
|
70
|
49 #endif
|
0
|
50
|
|
51 #include <setjmp.h>
|
|
52 #include "sysfile.h"
|
|
53 #include "sysproc.h"
|
|
54 #include "systime.h"
|
|
55 #include "syssignal.h" /* Always include before systty.h */
|
|
56
|
|
57 #include "systty.h"
|
|
58 #include "syswait.h"
|
|
59
|
|
60 /* a process object is a network connection when its pid field a cons
|
|
61 (name of name of port we are connected to . foreign host name) */
|
|
62
|
|
63 /* Valid values of process->status_symbol */
|
|
64 Lisp_Object Qrun, Qstop; /* Qexit from eval.c, Qsignal from data.c. */
|
|
65 /* Qrun => Qopen, Qexit => Qclosed for "network connection" processes */
|
|
66 Lisp_Object Qopen, Qclosed;
|
|
67
|
259
|
68 #ifdef HAVE_MULTICAST
|
|
69 Lisp_Object Qmulticast; /* Will be used for occasional warnings */
|
|
70 #endif
|
|
71
|
0
|
72 /* t means use pty, nil means use a pipe,
|
|
73 maybe other values to come. */
|
|
74 static Lisp_Object Vprocess_connection_type;
|
|
75
|
153
|
76 #ifdef PROCESS_IO_BLOCKING
|
|
77 /* List of port numbers or port names to set a blocking I/O mode.
|
|
78 Nil means set a non-blocking I/O mode [default]. */
|
|
79 static Lisp_Object network_stream_blocking_port_list;
|
|
80 #endif /* PROCESS_IO_BLOCKING */
|
|
81
|
0
|
82 /* FSFmacs says:
|
|
83
|
|
84 These next two vars are non-static since sysdep.c uses them in the
|
|
85 emulation of `select'. */
|
|
86 /* Number of events of change of status of a process. */
|
|
87 static volatile int process_tick;
|
|
88
|
|
89 /* Number of events for which the user or sentinel has been notified. */
|
|
90 static int update_tick;
|
|
91
|
|
92 /* Nonzero means delete a process right away if it exits. */
|
|
93 int delete_exited_processes;
|
|
94
|
|
95 /* Indexed by descriptor, gives the process (if any) for that descriptor */
|
|
96 Lisp_Object descriptor_to_process[MAXDESC];
|
|
97
|
|
98 /* List of process objects. */
|
|
99 Lisp_Object Vprocess_list;
|
|
100
|
|
101 Lisp_Object Qprocessp;
|
|
102
|
|
103 /* Buffered-ahead input char from process, indexed by channel.
|
|
104 -1 means empty (no char is buffered).
|
|
105 Used on sys V where the only way to tell if there is any
|
|
106 output from the process is to read at least one char.
|
|
107 Always -1 on systems that support FIONREAD. */
|
|
108
|
255
|
109 #if 0 /* FSFmacs */
|
0
|
110 /* FSFmacs says:
|
|
111 Don't make static; need to access externally. */
|
|
112 static int proc_buffered_char[MAXDESC];
|
255
|
113 #endif
|
0
|
114
|
|
115 #ifdef HAVE_PTYS
|
|
116 /* The file name of the pty opened by allocate_pty. */
|
|
117
|
|
118 static char pty_name[24];
|
|
119 #endif
|
|
120
|
|
121
|
|
122 /************************************************************************/
|
|
123 /* the process Lisp object */
|
|
124 /************************************************************************/
|
|
125
|
|
126 /*
|
|
127 * Structure records pertinent information about open channels.
|
|
128 * There is one channel associated with each process.
|
|
129 */
|
|
130
|
|
131 struct Lisp_Process
|
|
132 {
|
|
133 struct lcrecord_header header;
|
|
134 /* Name of this process */
|
|
135 Lisp_Object name;
|
|
136 /* List of command arguments that this process was run with */
|
|
137 Lisp_Object command;
|
|
138 /* (funcall FILTER PROC STRING) (if FILTER is non-nil)
|
|
139 to dispose of a bunch of chars from the process all at once */
|
|
140 Lisp_Object filter;
|
|
141 /* (funcall SENTINEL PROCESS) when process state changes */
|
|
142 Lisp_Object sentinel;
|
|
143 /* Buffer that output is going to */
|
|
144 Lisp_Object buffer;
|
|
145 /* Marker set to end of last buffer-inserted output from this process */
|
|
146 Lisp_Object mark;
|
|
147 /* Lisp_Int of subprocess' PID, or a cons of
|
|
148 service/host if this is really a network connection */
|
|
149 Lisp_Object pid;
|
|
150 /* Non-0 if this is really a ToolTalk channel. */
|
|
151 int connected_via_filedesc_p;
|
|
152 #if 0 /* FSFmacs */
|
|
153 /* Perhaps it's cleaner this way, but FSFmacs
|
|
154 provides no way of retrieving this value, so I'll
|
|
155 leave this info with PID. */
|
|
156 /* Non-nil if this is really a child process */
|
|
157 Lisp_Object childp;
|
|
158 #endif
|
|
159
|
|
160 /* Symbol indicating status of process.
|
|
161 This may be a symbol: run, stop, exit, signal */
|
|
162 Lisp_Object status_symbol;
|
|
163
|
|
164
|
|
165 /* Exit code if process has terminated,
|
|
166 signal which stopped/interrupted process
|
|
167 or 0 if process is running */
|
|
168 int exit_code;
|
|
169 /* Number of this process */
|
|
170 /* Non-false if process has exited and "dumped core" on its way down */
|
|
171 char core_dumped;
|
|
172 /* Descriptor by which we read from this process. -1 for dead process */
|
|
173 int infd;
|
|
174 /* Descriptor by which we write to this process. -1 for dead process */
|
|
175 int outfd;
|
|
176 /* Descriptor for the tty which this process is using.
|
|
177 -1 if we didn't record it (on some systems, there's no need). */
|
|
178 int subtty;
|
|
179 /* Name of subprocess terminal. */
|
|
180 Lisp_Object tty_name;
|
|
181 /* Non-false if communicating through a pty. */
|
|
182 char pty_flag;
|
|
183 /* This next field is only actually used #ifdef ENERGIZE */
|
|
184 /* if this flag is not NIL, then filter will do the read on the
|
|
185 channel, rather than having a call to make_string.
|
|
186 This only works if the filter is a subr. */
|
|
187 char filter_does_read;
|
|
188 /* Non-nil means kill silently if Emacs is exited. */
|
|
189 char kill_without_query;
|
|
190 char selected;
|
|
191 /* Event-count of last event in which this process changed status. */
|
|
192 volatile int tick;
|
|
193 /* Event-count of last such event reported. */
|
|
194 int update_tick;
|
|
195 /* streams used in input and output */
|
|
196 Lisp_Object instream;
|
|
197 Lisp_Object outstream;
|
|
198 /* The actual filedesc stream used for output; may be different
|
|
199 than OUTSTREAM under Mule */
|
|
200 Lisp_Object filedesc_stream;
|
|
201 };
|
|
202
|
|
203 static Lisp_Object mark_process (Lisp_Object, void (*) (Lisp_Object));
|
|
204 static void print_process (Lisp_Object, Lisp_Object, int);
|
|
205 static void finalize_process (void *, int);
|
|
206 DEFINE_LRECORD_IMPLEMENTATION ("process", process,
|
|
207 mark_process, print_process, finalize_process,
|
|
208 0, 0, struct Lisp_Process);
|
|
209
|
|
210 static Lisp_Object
|
|
211 mark_process (Lisp_Object obj, void (*markobj) (Lisp_Object))
|
|
212 {
|
|
213 struct Lisp_Process *proc = XPROCESS (obj);
|
|
214 ((markobj) (proc->name));
|
|
215 ((markobj) (proc->command));
|
|
216 ((markobj) (proc->filter));
|
|
217 ((markobj) (proc->sentinel));
|
|
218 ((markobj) (proc->buffer));
|
|
219 ((markobj) (proc->mark));
|
|
220 ((markobj) (proc->pid));
|
|
221 ((markobj) (proc->tty_name));
|
|
222 ((markobj) (proc->instream));
|
|
223 ((markobj) (proc->outstream));
|
|
224 ((markobj) (proc->filedesc_stream));
|
173
|
225 return proc->status_symbol;
|
0
|
226 }
|
|
227
|
|
228 static void
|
|
229 print_process (Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
|
|
230 {
|
|
231 struct Lisp_Process *proc = XPROCESS (obj);
|
185
|
232
|
0
|
233 if (print_readably)
|
|
234 error ("printing unreadable object #<process %s>",
|
16
|
235 XSTRING_DATA (proc->name));
|
185
|
236
|
0
|
237 if (!escapeflag)
|
|
238 {
|
|
239 print_internal (proc->name, printcharfun, 0);
|
|
240 }
|
|
241 else
|
|
242 {
|
|
243 int netp = network_connection_p (obj);
|
|
244 write_c_string (((netp) ? GETTEXT ("#<network connection ") :
|
|
245 GETTEXT ("#<process ")), printcharfun);
|
|
246 print_internal (proc->name, printcharfun, 1);
|
|
247 write_c_string (((netp) ? " " : " pid "), printcharfun);
|
|
248 print_internal (proc->pid, printcharfun, 1);
|
|
249 write_c_string (" state:", printcharfun);
|
|
250 print_internal (proc->status_symbol, printcharfun, 1);
|
|
251 write_c_string (">", printcharfun);
|
|
252 }
|
|
253 }
|
|
254
|
|
255 #ifdef HAVE_WINDOW_SYSTEM
|
|
256 extern void debug_process_finalization (struct Lisp_Process *p);
|
|
257 #endif /* HAVE_WINDOW_SYSTEM */
|
|
258
|
|
259 static void
|
|
260 finalize_process (void *header, int for_disksave)
|
|
261 {
|
|
262 if (for_disksave) return; /* hmm, what would this do anyway? */
|
|
263 /* #### this probably needs to be tied into the tty event loop */
|
|
264 /* #### when there is one */
|
|
265 #ifdef HAVE_WINDOW_SYSTEM
|
|
266 {
|
|
267 struct Lisp_Process *p = (struct Lisp_Process *) header;
|
|
268 debug_process_finalization (p);
|
|
269 }
|
|
270 #endif /* HAVE_WINDOW_SYSTEM */
|
|
271 }
|
|
272
|
|
273
|
|
274 /************************************************************************/
|
|
275 /* basic process accessors */
|
|
276 /************************************************************************/
|
|
277
|
|
278 static SIGTYPE
|
|
279 close_safely_handler (int signo)
|
|
280 {
|
|
281 EMACS_REESTABLISH_SIGNAL (signo, close_safely_handler);
|
|
282 SIGRETURN;
|
|
283 }
|
|
284
|
|
285 static void
|
|
286 close_safely (int fd)
|
|
287 {
|
|
288 stop_interrupts ();
|
|
289 signal (SIGALRM, close_safely_handler);
|
|
290 alarm (1);
|
|
291 close (fd);
|
|
292 alarm (0);
|
|
293 start_interrupts ();
|
|
294 }
|
|
295
|
|
296 static void
|
|
297 close_descriptor_pair (int in, int out)
|
|
298 {
|
|
299 if (in >= 0)
|
|
300 close (in);
|
|
301 if (out != in && out >= 0)
|
|
302 close (out);
|
|
303 }
|
|
304
|
|
305 /* Close all descriptors currently in use for communication
|
|
306 with subprocess. This is used in a newly-forked subprocess
|
|
307 to get rid of irrelevant descriptors. */
|
|
308
|
|
309 void
|
|
310 close_process_descs (void)
|
|
311 {
|
|
312 #ifndef WINDOWSNT
|
|
313 int i;
|
|
314 for (i = 0; i < MAXDESC; i++)
|
|
315 {
|
|
316 Lisp_Object process;
|
|
317 process = descriptor_to_process[i];
|
|
318 if (!NILP (process))
|
|
319 {
|
|
320 close_descriptor_pair (XPROCESS (process)->infd,
|
|
321 XPROCESS (process)->outfd);
|
|
322 }
|
|
323 }
|
|
324 #endif
|
|
325 }
|
|
326
|
|
327 void
|
|
328 get_process_file_descriptors (struct Lisp_Process *p, int *infd,
|
|
329 int *outfd)
|
|
330 {
|
|
331 if (! p) abort ();
|
|
332 /* the cast of MAXDESC is needed for some versions of Linux */
|
|
333 assert (p->infd >= -1 && p->infd < ((int) (MAXDESC)));
|
|
334 assert (p->outfd >= -1 && p->outfd < ((int) (MAXDESC)));
|
|
335 *infd = p->infd;
|
|
336 *outfd = p->outfd;
|
|
337 }
|
|
338
|
|
339 struct Lisp_Process *
|
|
340 get_process_from_input_descriptor (int infd)
|
|
341 {
|
|
342 Lisp_Object proc;
|
|
343
|
|
344 if ((infd < 0) || (infd >= ((int) (MAXDESC)))) abort ();
|
|
345 proc = descriptor_to_process[infd];
|
|
346 if (NILP (proc))
|
|
347 return 0;
|
|
348 else
|
|
349 return XPROCESS (proc);
|
|
350 }
|
|
351
|
|
352 int
|
|
353 get_process_selected_p (struct Lisp_Process *p)
|
|
354 {
|
|
355 return p->selected;
|
|
356 }
|
|
357
|
|
358 void
|
|
359 set_process_selected_p (struct Lisp_Process *p, int selected_p)
|
|
360 {
|
|
361 p->selected = !!selected_p;
|
|
362 }
|
|
363
|
|
364 #ifdef HAVE_SOCKETS
|
|
365 int
|
|
366 network_connection_p (Lisp_Object process)
|
|
367 {
|
173
|
368 return GC_CONSP (XPROCESS (process)->pid);
|
0
|
369 }
|
|
370 #endif
|
|
371
|
|
372 int
|
|
373 connected_via_filedesc_p (struct Lisp_Process *p)
|
|
374 {
|
|
375 return p->connected_via_filedesc_p;
|
|
376 }
|
|
377
|
20
|
378 DEFUN ("processp", Fprocessp, 1, 1, 0, /*
|
0
|
379 Return t if OBJECT is a process.
|
20
|
380 */
|
|
381 (obj))
|
0
|
382 {
|
173
|
383 return PROCESSP (obj) ? Qt : Qnil;
|
0
|
384 }
|
|
385
|
20
|
386 DEFUN ("process-list", Fprocess_list, 0, 0, 0, /*
|
0
|
387 Return a list of all processes.
|
20
|
388 */
|
|
389 ())
|
0
|
390 {
|
|
391 return Fcopy_sequence (Vprocess_list);
|
|
392 }
|
|
393
|
20
|
394 DEFUN ("get-process", Fget_process, 1, 1, 0, /*
|
0
|
395 Return the process named NAME, or nil if there is none.
|
20
|
396 */
|
|
397 (name))
|
0
|
398 {
|
|
399 Lisp_Object tail;
|
|
400
|
|
401 if (GC_PROCESSP (name))
|
173
|
402 return name;
|
0
|
403
|
|
404 if (!gc_in_progress)
|
|
405 /* this only gets called during GC when emacs is going away as a result
|
|
406 of a signal or crash. */
|
|
407 CHECK_STRING (name);
|
|
408
|
|
409 for (tail = Vprocess_list; GC_CONSP (tail); tail = XCDR (tail))
|
|
410 {
|
|
411 Lisp_Object proc = XCAR (tail);
|
|
412 QUIT;
|
195
|
413 if (internal_equal (name, XPROCESS (proc)->name, 0))
|
173
|
414 return XCAR (tail);
|
0
|
415 }
|
|
416 return Qnil;
|
|
417 }
|
|
418
|
20
|
419 DEFUN ("get-buffer-process", Fget_buffer_process, 1, 1, 0, /*
|
0
|
420 Return the (or, a) process associated with BUFFER.
|
|
421 BUFFER may be a buffer or the name of one.
|
20
|
422 */
|
|
423 (name))
|
0
|
424 {
|
|
425 Lisp_Object buf, tail, proc;
|
|
426
|
|
427 if (GC_NILP (name)) return Qnil;
|
|
428 buf = Fget_buffer (name);
|
|
429 if (GC_NILP (buf)) return Qnil;
|
|
430
|
|
431 for (tail = Vprocess_list; GC_CONSP (tail); tail = XCDR (tail))
|
|
432 {
|
|
433 /* jwz: do not quit here - it isn't necessary, as there is no way for
|
|
434 Vprocess_list to get circular or overwhelmingly long, and this
|
|
435 function is called from layout_mode_element under redisplay. */
|
|
436 /* QUIT; */
|
|
437 proc = XCAR (tail);
|
|
438 if (GC_PROCESSP (proc) && EQ (XPROCESS (proc)->buffer, buf))
|
|
439 return proc;
|
|
440 }
|
|
441 return Qnil;
|
|
442 }
|
|
443
|
|
444 /* This is how commands for the user decode process arguments. It
|
|
445 accepts a process, a process name, a buffer, a buffer name, or nil.
|
|
446 Buffers denote the first process in the buffer, and nil denotes the
|
|
447 current buffer. */
|
|
448
|
|
449 static Lisp_Object
|
|
450 get_process (Lisp_Object name)
|
|
451 {
|
136
|
452 Lisp_Object proc, obj;
|
0
|
453
|
|
454 #ifdef I18N3
|
|
455 /* #### Look more closely into translating process names. */
|
|
456 #endif
|
|
457
|
|
458 /* This may be called during a GC from process_send_signal() from
|
|
459 kill_buffer_processes() if emacs decides to abort(). */
|
|
460 if (GC_PROCESSP (name))
|
|
461 return name;
|
|
462
|
136
|
463 if (GC_STRINGP (name))
|
|
464 {
|
|
465 obj = Fget_process (name);
|
|
466 if (GC_NILP (obj))
|
|
467 obj = Fget_buffer (name);
|
|
468 if (GC_NILP (obj))
|
|
469 error ("Process %s does not exist", XSTRING_DATA (name));
|
|
470 }
|
|
471 else if (GC_NILP (name))
|
|
472 obj = Fcurrent_buffer ();
|
|
473 else
|
|
474 obj = name;
|
|
475
|
|
476 /* Now obj should be either a buffer object or a process object.
|
|
477 */
|
|
478 if (GC_BUFFERP (obj))
|
|
479 {
|
|
480 proc = Fget_buffer_process (obj);
|
|
481 if (GC_NILP (proc))
|
|
482 error ("Buffer %s has no process", XSTRING_DATA (XBUFFER(obj)->name));
|
|
483 }
|
0
|
484 else
|
|
485 {
|
136
|
486 /* fsf: CHECK_PROCESS (obj, 0); */
|
|
487 proc = obj;
|
0
|
488 }
|
136
|
489 return proc;
|
0
|
490 }
|
|
491
|
20
|
492 DEFUN ("process-id", Fprocess_id, 1, 1, 0, /*
|
0
|
493 Return the process id of PROCESS.
|
|
494 This is the pid of the Unix process which PROCESS uses or talks to.
|
|
495 For a network connection, this value is a cons of
|
|
496 (foreign-network-port . foreign-host-name).
|
20
|
497 */
|
|
498 (proc))
|
0
|
499 {
|
|
500 Lisp_Object pid;
|
|
501 CHECK_PROCESS (proc);
|
|
502
|
|
503 pid = XPROCESS (proc)->pid;
|
|
504 if (network_connection_p (proc))
|
173
|
505 /* return Qnil; */
|
|
506 return Fcons (Fcar (pid), Fcdr (pid));
|
0
|
507 else
|
173
|
508 return pid;
|
0
|
509 }
|
|
510
|
20
|
511 DEFUN ("process-name", Fprocess_name, 1, 1, 0, /*
|
0
|
512 Return the name of PROCESS, as a string.
|
|
513 This is the name of the program invoked in PROCESS,
|
|
514 possibly modified to make it unique among process names.
|
20
|
515 */
|
|
516 (proc))
|
0
|
517 {
|
|
518 CHECK_PROCESS (proc);
|
|
519 return XPROCESS (proc)->name;
|
|
520 }
|
|
521
|
20
|
522 DEFUN ("process-command", Fprocess_command, 1, 1, 0, /*
|
0
|
523 Return the command that was executed to start PROCESS.
|
|
524 This is a list of strings, the first string being the program executed
|
|
525 and the rest of the strings being the arguments given to it.
|
20
|
526 */
|
|
527 (proc))
|
0
|
528 {
|
|
529 CHECK_PROCESS (proc);
|
|
530 return XPROCESS (proc)->command;
|
|
531 }
|
|
532
|
|
533
|
|
534 /************************************************************************/
|
|
535 /* creating a process */
|
|
536 /************************************************************************/
|
|
537
|
|
538 static Lisp_Object
|
|
539 make_process_internal (Lisp_Object name)
|
|
540 {
|
|
541 Lisp_Object val, name1;
|
|
542 int i;
|
185
|
543 struct Lisp_Process *p =
|
|
544 alloc_lcrecord_type (struct Lisp_Process, lrecord_process);
|
0
|
545
|
|
546 /* If name is already in use, modify it until it is unused. */
|
|
547 name1 = name;
|
|
548 for (i = 1; ; i++)
|
|
549 {
|
|
550 char suffix[10];
|
|
551 Lisp_Object tem = Fget_process (name1);
|
185
|
552 if (NILP (tem))
|
0
|
553 break;
|
|
554 sprintf (suffix, "<%d>", i);
|
|
555 name1 = concat2 (name, build_string (suffix));
|
|
556 }
|
|
557 name = name1;
|
|
558 p->name = name;
|
|
559
|
|
560 p->command = Qnil;
|
|
561 p->filter = Qnil;
|
|
562 p->sentinel = Qnil;
|
|
563 p->buffer = Qnil;
|
|
564 p->mark = Fmake_marker ();
|
|
565 p->pid = Qnil;
|
|
566 p->status_symbol = Qrun;
|
|
567 p->connected_via_filedesc_p = 0;
|
|
568 p->exit_code = 0;
|
|
569 p->core_dumped = 0;
|
|
570 p->infd = -1;
|
|
571 p->outfd = -1;
|
|
572 p->subtty = -1;
|
|
573 p->tty_name = Qnil;
|
|
574 p->pty_flag = 0;
|
|
575 p->filter_does_read = 0;
|
|
576 p->kill_without_query = 0;
|
|
577 p->selected = 0;
|
|
578 p->tick = 0;
|
|
579 p->update_tick = 0;
|
|
580 p->instream = Qnil;
|
|
581 p->outstream = Qnil;
|
|
582
|
|
583 XSETPROCESS (val, p);
|
|
584
|
|
585 Vprocess_list = Fcons (val, Vprocess_list);
|
173
|
586 return val;
|
0
|
587 }
|
|
588
|
|
589 #ifdef HAVE_PTYS
|
|
590
|
|
591 /* Open an available pty, returning a file descriptor.
|
|
592 Return -1 on failure.
|
|
593 The file name of the terminal corresponding to the pty
|
|
594 is left in the variable pty_name. */
|
|
595
|
|
596 static int
|
|
597 allocate_pty (void)
|
|
598 {
|
|
599 struct stat stb;
|
|
600 int c, i;
|
|
601 int fd;
|
|
602
|
|
603 /* Some systems name their pseudoterminals so that there are gaps in
|
|
604 the usual sequence - for example, on HP9000/S700 systems, there
|
|
605 are no pseudoterminals with names ending in 'f'. So we wait for
|
|
606 three failures in a row before deciding that we've reached the
|
|
607 end of the ptys. */
|
|
608 int failed_count = 0;
|
|
609
|
|
610 #ifdef PTY_ITERATION
|
|
611 PTY_ITERATION
|
|
612 #else
|
|
613 for (c = FIRST_PTY_LETTER; c <= 'z'; c++)
|
|
614 for (i = 0; i < 16; i++)
|
|
615 #endif
|
|
616 {
|
|
617 #ifdef PTY_NAME_SPRINTF
|
|
618 PTY_NAME_SPRINTF
|
|
619 #else
|
|
620 sprintf (pty_name, "/dev/pty%c%x", c, i);
|
|
621 #endif /* no PTY_NAME_SPRINTF */
|
|
622
|
|
623 #ifdef PTY_OPEN
|
|
624 PTY_OPEN;
|
|
625 #else /* no PTY_OPEN */
|
|
626 #ifdef IRIS
|
|
627 /* Unusual IRIS code */
|
251
|
628 *ptyv = open ("/dev/ptc", O_RDWR | O_NDELAY | OPEN_BINARY, 0);
|
0
|
629 if (fd < 0)
|
|
630 return -1;
|
|
631 if (fstat (fd, &stb) < 0)
|
|
632 return -1;
|
|
633 #else /* not IRIS */
|
|
634 if (stat (pty_name, &stb) < 0)
|
|
635 {
|
|
636 failed_count++;
|
|
637 if (failed_count >= 3)
|
|
638 return -1;
|
|
639 }
|
|
640 else
|
|
641 failed_count = 0;
|
|
642 #ifdef O_NONBLOCK
|
251
|
643 fd = open (pty_name, O_RDWR | O_NONBLOCK | OPEN_BINARY, 0);
|
0
|
644 #else
|
251
|
645 fd = open (pty_name, O_RDWR | O_NDELAY | OPEN_BINARY, 0);
|
0
|
646 #endif
|
|
647 #endif /* not IRIS */
|
|
648 #endif /* no PTY_OPEN */
|
|
649
|
|
650 if (fd >= 0)
|
|
651 {
|
|
652 /* check to make certain that both sides are available
|
|
653 this avoids a nasty yet stupid bug in rlogins */
|
|
654 #ifdef PTY_TTY_NAME_SPRINTF
|
|
655 PTY_TTY_NAME_SPRINTF
|
|
656 #else
|
|
657 sprintf (pty_name, "/dev/tty%c%x", c, i);
|
|
658 #endif /* no PTY_TTY_NAME_SPRINTF */
|
|
659 #ifndef UNIPLUS
|
|
660 if (access (pty_name, 6) != 0)
|
|
661 {
|
|
662 close (fd);
|
|
663 #if !defined(IRIS) && !defined(__sgi)
|
|
664 continue;
|
|
665 #else
|
|
666 return -1;
|
|
667 #endif /* IRIS */
|
|
668 }
|
|
669 #endif /* not UNIPLUS */
|
|
670 setup_pty (fd);
|
|
671 return fd;
|
|
672 }
|
|
673 }
|
|
674 return -1;
|
|
675 }
|
|
676 #endif /* HAVE_PTYS */
|
|
677
|
|
678 static int
|
|
679 create_bidirectional_pipe (int *inchannel, int *outchannel,
|
|
680 volatile int *forkin, volatile int *forkout)
|
|
681 {
|
|
682 int sv[2];
|
|
683
|
|
684 #ifdef SKTPAIR
|
|
685 if (socketpair (AF_UNIX, SOCK_STREAM, 0, sv) < 0)
|
|
686 return -1;
|
|
687 *outchannel = *inchannel = sv[0];
|
|
688 *forkout = *forkin = sv[1];
|
|
689 #else /* not SKTPAIR */
|
|
690 int temp;
|
|
691 temp = pipe (sv);
|
|
692 if (temp < 0) return -1;
|
|
693 *inchannel = sv[0];
|
|
694 *forkout = sv[1];
|
|
695 temp = pipe (sv);
|
|
696 if (temp < 0) return -1;
|
|
697 *outchannel = sv[1];
|
|
698 *forkin = sv[0];
|
|
699 #endif /* not SKTPAIR */
|
|
700 return 0;
|
|
701 }
|
|
702
|
|
703
|
|
704 static Bufbyte
|
|
705 get_eof_char (struct Lisp_Process *p)
|
|
706 {
|
|
707 /* Figure out the eof character for the outfd of the given process.
|
185
|
708 * The following code is similar to that in process_send_signal, and
|
0
|
709 * should probably be merged with that code somehow. */
|
|
710
|
16
|
711 CONST Bufbyte ctrl_d = (Bufbyte) '\004';
|
185
|
712
|
16
|
713 if (!isatty (p->outfd))
|
|
714 return ctrl_d;
|
0
|
715 #ifdef HAVE_TERMIOS
|
16
|
716 {
|
|
717 struct termios t;
|
|
718 tcgetattr (p->outfd, &t);
|
|
719 #if 0
|
|
720 /* What is the following line designed to do??? -mrb */
|
|
721 if (strlen ((CONST char *) t.c_cc) < (unsigned int) (VEOF + 1))
|
|
722 return ctrl_d;
|
|
723 else
|
|
724 return (Bufbyte) t.c_cc[VEOF];
|
|
725 #endif
|
|
726 return t.c_cc[VEOF] == CDISABLE ? ctrl_d : (Bufbyte) t.c_cc[VEOF];
|
|
727 }
|
0
|
728 #else /* ! HAVE_TERMIOS */
|
|
729 /* On Berkeley descendants, the following IOCTL's retrieve the
|
|
730 current control characters. */
|
|
731 #if defined (TIOCGETC)
|
16
|
732 {
|
|
733 struct tchars c;
|
|
734 ioctl (p->outfd, TIOCGETC, &c);
|
|
735 return (Bufbyte) c.t_eofc;
|
|
736 }
|
0
|
737 #else /* ! defined (TIOCGLTC) && defined (TIOCGETC) */
|
|
738 /* On SYSV descendants, the TCGETA ioctl retrieves the current control
|
|
739 characters. */
|
|
740 #ifdef TCGETA
|
16
|
741 {
|
|
742 struct termio t;
|
|
743 ioctl (p->outfd, TCGETA, &t);
|
|
744 if (strlen ((CONST char *) t.c_cc) < (unsigned int) (VINTR + 1))
|
|
745 return ctrl_d;
|
|
746 else
|
|
747 return (Bufbyte) t.c_cc[VINTR];
|
|
748 }
|
0
|
749 #else /* ! defined (TCGETA) */
|
185
|
750 /* Rather than complain, we'll just guess ^D, which is what
|
0
|
751 * earlier emacsen always used. */
|
16
|
752 return ctrl_d;
|
0
|
753 #endif /* ! defined (TCGETA) */
|
|
754 #endif /* ! defined (TIOCGETC) */
|
|
755 #endif /* ! defined (HAVE_TERMIOS) */
|
|
756 }
|
|
757
|
|
758 static int
|
|
759 get_pty_max_bytes (struct Lisp_Process *p)
|
|
760 {
|
|
761 int pty_max_bytes;
|
|
762
|
|
763 #if defined (HAVE_FPATHCONF) && defined (_PC_MAX_CANON)
|
|
764 pty_max_bytes = fpathconf (p->outfd, _PC_MAX_CANON);
|
|
765 if (pty_max_bytes < 0)
|
|
766 pty_max_bytes = 250;
|
|
767 #else
|
|
768 pty_max_bytes = 250;
|
|
769 #endif
|
|
770 /* Deduct one, to leave space for the eof. */
|
|
771 pty_max_bytes--;
|
|
772
|
|
773 return pty_max_bytes;
|
|
774 }
|
|
775
|
|
776 static void
|
|
777 init_process_fds (struct Lisp_Process *p, int in, int out)
|
|
778 {
|
|
779 p->infd = in;
|
|
780 p->outfd = out;
|
|
781 p->instream = make_filedesc_input_stream (in, 0, -1, 0);
|
|
782 p->outstream = make_filedesc_output_stream (out, 0, -1,
|
|
783 LSTR_BLOCKED_OK
|
|
784 | (p->pty_flag ?
|
|
785 LSTR_PTY_FLUSHING : 0));
|
|
786 p->filedesc_stream = p->outstream;
|
|
787 if (p->pty_flag)
|
|
788 {
|
|
789 Bufbyte eof_char = get_eof_char (p);
|
|
790 int pty_max_bytes = get_pty_max_bytes (p);
|
|
791 filedesc_stream_set_pty_flushing (XLSTREAM (p->outstream),
|
|
792 pty_max_bytes, eof_char);
|
|
793 }
|
259
|
794
|
|
795 #ifdef FILE_CODING
|
|
796
|
70
|
797 p->instream = make_decoding_input_stream
|
|
798 (XLSTREAM (p->instream),
|
120
|
799 Fget_coding_system (Vcoding_system_for_read));
|
70
|
800 Lstream_set_character_mode (XLSTREAM (p->instream));
|
|
801 p->outstream = make_encoding_output_stream
|
|
802 (XLSTREAM (p->outstream),
|
120
|
803 Fget_coding_system (Vcoding_system_for_write));
|
70
|
804 /* CODE_CNTL (&out_state[outchannel]) |= CC_END; !!####
|
|
805 What's going on here? */
|
259
|
806 #endif
|
0
|
807 }
|
|
808
|
|
809 static void
|
185
|
810 create_process (Lisp_Object process,
|
0
|
811 char **new_argv, CONST char *current_dir)
|
|
812 {
|
|
813 /* This function rewritten by wing@666.com. */
|
|
814
|
|
815 int pid, inchannel, outchannel;
|
|
816 /* Use volatile to protect variables from being clobbered by longjmp. */
|
|
817 volatile int forkin, forkout;
|
|
818 volatile int pty_flag = 0;
|
|
819 char **env;
|
|
820 struct Lisp_Process *p = XPROCESS (process);
|
|
821
|
|
822 env = environ;
|
|
823
|
|
824 inchannel = outchannel = forkin = forkout = -1;
|
|
825
|
|
826 #ifdef HAVE_PTYS
|
|
827 if (!NILP (Vprocess_connection_type))
|
|
828 {
|
|
829 /* find a new pty, open the master side, return the opened
|
|
830 file handle, and store the name of the corresponding slave
|
|
831 side in global variable pty_name. */
|
|
832 outchannel = inchannel = allocate_pty ();
|
|
833 }
|
|
834
|
|
835 if (inchannel >= 0)
|
|
836 {
|
|
837 /* You're "supposed" to now open the slave in the child.
|
|
838 On some systems, we can open it here; this allows for
|
|
839 better error checking. */
|
251
|
840 #if !defined(USG)
|
0
|
841 /* On USG systems it does not work to open the pty's tty here
|
|
842 and then close and reopen it in the child. */
|
|
843 #ifdef O_NOCTTY
|
|
844 /* Don't let this terminal become our controlling terminal
|
|
845 (in case we don't have one). */
|
251
|
846 forkout = forkin = open (pty_name, O_RDWR | O_NOCTTY | OPEN_BINARY, 0);
|
0
|
847 #else
|
251
|
848 forkout = forkin = open (pty_name, O_RDWR | OPEN_BINARY, 0);
|
0
|
849 #endif
|
|
850 if (forkin < 0)
|
|
851 goto io_failure;
|
|
852 #endif /* not USG */
|
|
853 p->pty_flag = pty_flag = 1;
|
|
854 }
|
|
855 else
|
|
856 #endif /* HAVE_PTYS */
|
|
857 if (create_bidirectional_pipe (&inchannel, &outchannel,
|
|
858 &forkin, &forkout) < 0)
|
|
859 goto io_failure;
|
|
860
|
|
861 #if 0
|
|
862 /* Replaced by close_process_descs */
|
|
863 set_exclusive_use (inchannel);
|
|
864 set_exclusive_use (outchannel);
|
|
865 #endif
|
|
866
|
|
867 set_descriptor_non_blocking (inchannel);
|
|
868
|
|
869 /* Record this as an active process, with its channels.
|
|
870 As a result, child_setup will close Emacs's side of the pipes. */
|
|
871 descriptor_to_process[inchannel] = process;
|
|
872 init_process_fds (p, inchannel, outchannel);
|
|
873 /* Record the tty descriptor used in the subprocess. */
|
|
874 p->subtty = forkin;
|
|
875 p->status_symbol = Qrun;
|
|
876 p->exit_code = 0;
|
|
877
|
|
878 {
|
253
|
879 #if !defined(__CYGWIN32__)
|
0
|
880 /* child_setup must clobber environ on systems with true vfork.
|
|
881 Protect it from permanent change. */
|
253
|
882 char **save_environ = environ;
|
|
883 #endif
|
0
|
884
|
|
885 #ifdef EMACS_BTL
|
|
886 /* when performance monitoring is on, turn it off before the vfork(),
|
|
887 as the child has no handler for the signal -- when back in the
|
|
888 parent process, turn it back on if it was really on when you "turned
|
|
889 it off" */
|
|
890 int logging_on = cadillac_stop_logging (); /* #### rename me */
|
|
891 #endif
|
|
892
|
|
893 #ifndef WINDOWSNT
|
167
|
894 pid = fork ();
|
0
|
895 if (pid == 0)
|
|
896 #endif /* not WINDOWSNT */
|
|
897 {
|
|
898 /**** Now we're in the child process ****/
|
|
899 int xforkin = forkin;
|
|
900 int xforkout = forkout;
|
|
901
|
|
902 if (!pty_flag)
|
|
903 EMACS_SEPARATE_PROCESS_GROUP ();
|
|
904 #ifdef HAVE_PTYS
|
|
905 else
|
|
906 {
|
|
907 /* Disconnect the current controlling terminal, pursuant to
|
|
908 making the pty be the controlling terminal of the process.
|
|
909 Also put us in our own process group. */
|
|
910
|
|
911 disconnect_controlling_terminal ();
|
|
912
|
|
913 /* Open the pty connection and make the pty's terminal
|
|
914 our controlling terminal.
|
|
915
|
|
916 On systems with TIOCSCTTY, we just use it to set
|
|
917 the controlling terminal. On other systems, the
|
|
918 first TTY we open becomes the controlling terminal.
|
|
919 So, we end up with four possibilities:
|
|
920
|
|
921 (1) on USG and TIOCSCTTY systems, we open the pty
|
|
922 and use TIOCSCTTY.
|
|
923 (2) on other USG systems, we just open the pty.
|
|
924 (3) on non-USG systems with TIOCSCTTY, we
|
|
925 just use TIOCSCTTY. (On non-USG systems, we
|
|
926 already opened the pty in the parent process.)
|
|
927 (4) on non-USG systems without TIOCSCTTY, we
|
|
928 close the pty and reopen it.
|
|
929
|
|
930 This would be cleaner if we didn't open the pty
|
|
931 in the parent process, but doing it that way
|
|
932 makes it possible to trap error conditions.
|
|
933 It's harder to convey an error from the child
|
|
934 process, and I don't feel like messing with
|
|
935 this now. */
|
|
936
|
|
937 /* There was some weirdo, probably wrong,
|
|
938 conditionalization on RTU and UNIPLUS here.
|
|
939 I deleted it. So sue me. */
|
|
940
|
|
941 /* SunOS has TIOCSCTTY but the close/open method
|
|
942 also works. */
|
|
943
|
|
944 # if defined (USG) || !defined (TIOCSCTTY)
|
|
945 /* Now close the pty (if we had it open) and reopen it.
|
|
946 This makes the pty the controlling terminal of the
|
|
947 subprocess. */
|
|
948 /* I wonder if close (open (pty_name, ...)) would work? */
|
|
949 if (xforkin >= 0)
|
|
950 close (xforkin);
|
251
|
951 xforkout = xforkin = open (pty_name, O_RDWR | OPEN_BINARY, 0);
|
0
|
952 if (xforkin < 0)
|
|
953 {
|
|
954 write (1, "Couldn't open the pty terminal ", 31);
|
|
955 write (1, pty_name, strlen (pty_name));
|
|
956 write (1, "\n", 1);
|
|
957 _exit (1);
|
|
958 }
|
|
959 # endif /* USG or not TIOCSCTTY */
|
|
960
|
|
961 /* Miscellaneous setup required for some systems.
|
|
962 Must be done before using tc* functions on xforkin.
|
|
963 This guarantees that isatty(xforkin) is true. */
|
185
|
964
|
0
|
965 # ifdef SETUP_SLAVE_PTY
|
|
966 SETUP_SLAVE_PTY;
|
|
967 # endif /* SETUP_SLAVE_PTY */
|
185
|
968
|
0
|
969 # ifdef TIOCSCTTY
|
|
970 /* We ignore the return value
|
|
971 because faith@cs.unc.edu says that is necessary on Linux. */
|
|
972 assert (isatty (xforkin));
|
|
973 ioctl (xforkin, TIOCSCTTY, 0);
|
|
974 # endif /* TIOCSCTTY */
|
|
975
|
|
976 /* Change the line discipline. */
|
|
977
|
|
978 # if defined (HAVE_TERMIOS) && defined (LDISC1)
|
|
979 {
|
|
980 struct termios t;
|
|
981 assert (isatty (xforkin));
|
|
982 tcgetattr (xforkin, &t);
|
|
983 t.c_lflag = LDISC1;
|
|
984 if (tcsetattr (xforkin, TCSANOW, &t) < 0)
|
|
985 perror ("create_process/tcsetattr LDISC1 failed\n");
|
|
986 }
|
|
987 # elif defined (NTTYDISC) && defined (TIOCSETD)
|
|
988 {
|
|
989 /* Use new line discipline. TIOCSETD is accepted and
|
|
990 ignored on Sys5.4 systems with ttcompat. */
|
|
991 int ldisc = NTTYDISC;
|
|
992 assert (isatty (xforkin));
|
|
993 ioctl (xforkin, TIOCSETD, &ldisc);
|
|
994 }
|
|
995 # endif /* TIOCSETD & NTTYDISC */
|
|
996
|
|
997 /* Make our process group be the foreground group
|
|
998 of our new controlling terminal. */
|
|
999
|
|
1000 {
|
|
1001 int piddly = EMACS_GET_PROCESS_GROUP ();
|
|
1002 EMACS_SET_TTY_PROCESS_GROUP (xforkin, &piddly);
|
|
1003 }
|
|
1004
|
|
1005 # ifdef AIX
|
|
1006 /* On AIX, we've disabled SIGHUP above once we start a
|
|
1007 child on a pty. Now reenable it in the child, so it
|
|
1008 will die when we want it to. */
|
|
1009 signal (SIGHUP, SIG_DFL);
|
|
1010 # endif /* AIX */
|
|
1011 }
|
|
1012 #endif /* HAVE_PTYS */
|
|
1013
|
|
1014 signal (SIGINT, SIG_DFL);
|
|
1015 signal (SIGQUIT, SIG_DFL);
|
|
1016
|
100
|
1017 #if !defined(MSDOS) && !defined(WINDOWSNT)
|
0
|
1018 if (pty_flag)
|
|
1019 {
|
|
1020 /* Set up the terminal characteristics of the pty. */
|
|
1021 child_setup_tty (xforkout);
|
|
1022 }
|
|
1023
|
|
1024 #ifdef WINDOWSNT
|
|
1025 pid = child_setup (xforkin, xforkout, xforkout,
|
|
1026 new_argv, current_dir);
|
185
|
1027 #else /* not WINDOWSNT */
|
0
|
1028 child_setup (xforkin, xforkout, xforkout, new_argv, current_dir);
|
|
1029 #endif /* not WINDOWSNT */
|
|
1030 #endif /* not MSDOS */
|
|
1031 }
|
|
1032 #ifdef EMACS_BTL
|
|
1033 else if (logging_on)
|
|
1034 cadillac_start_logging (); /* #### rename me */
|
|
1035 #endif
|
|
1036
|
253
|
1037 #if !defined(__CYGWIN32__)
|
|
1038 environ = save_environ;
|
|
1039 #endif
|
0
|
1040 }
|
|
1041
|
|
1042 if (pid < 0)
|
|
1043 {
|
|
1044 close_descriptor_pair (forkin, forkout);
|
167
|
1045 report_file_error ("Doing fork", Qnil);
|
0
|
1046 }
|
|
1047
|
|
1048 p->pid = make_int (pid);
|
120
|
1049 /* #### dmoore - why is this commented out, otherwise we leave
|
|
1050 subtty = forkin, but then we close forkin just below. */
|
0
|
1051 /* p->subtty = -1; */
|
|
1052
|
|
1053 #ifdef WINDOWSNT
|
|
1054 register_child (pid, inchannel);
|
|
1055 #endif /* WINDOWSNT */
|
|
1056
|
|
1057 /* If the subfork execv fails, and it exits,
|
|
1058 this close hangs. I don't know why.
|
|
1059 So have an interrupt jar it loose. */
|
|
1060 if (forkin >= 0)
|
|
1061 close_safely (forkin);
|
|
1062 if (forkin != forkout && forkout >= 0)
|
|
1063 close (forkout);
|
|
1064
|
|
1065 #ifdef HAVE_PTYS
|
|
1066 if (pty_flag)
|
|
1067 XPROCESS (process)->tty_name = build_string (pty_name);
|
|
1068 else
|
|
1069 #endif
|
|
1070 XPROCESS (process)->tty_name = Qnil;
|
|
1071
|
|
1072 /* Notice that SIGCHLD was not blocked. (This is not possible on
|
|
1073 some systems.) No biggie if SIGCHLD occurs right around the
|
|
1074 time that this call happens, because SIGCHLD() does not actually
|
|
1075 deselect the process (that doesn't occur until the next time
|
|
1076 we're waiting for an event, when status_notify() is called). */
|
|
1077 event_stream_select_process (XPROCESS (process));
|
|
1078
|
|
1079 return;
|
|
1080
|
|
1081 io_failure:
|
|
1082 {
|
|
1083 int temp = errno;
|
|
1084 close_descriptor_pair (forkin, forkout);
|
|
1085 close_descriptor_pair (inchannel, outchannel);
|
|
1086 errno = temp;
|
|
1087 report_file_error ("Opening pty or pipe", Qnil);
|
|
1088 }
|
|
1089 }
|
|
1090
|
|
1091 /* This function is the unwind_protect form for Fstart_process_internal. If
|
|
1092 PROC doesn't have its pid set, then we know someone has signalled
|
|
1093 an error and the process wasn't started successfully, so we should
|
|
1094 remove it from the process list. */
|
|
1095 static void remove_process (Lisp_Object proc);
|
|
1096 static Lisp_Object
|
|
1097 start_process_unwind (Lisp_Object proc)
|
|
1098 {
|
|
1099 /* Was PROC started successfully? */
|
|
1100 if (EQ (XPROCESS (proc)->pid, Qnil))
|
|
1101 remove_process (proc);
|
|
1102 return Qnil;
|
|
1103 }
|
|
1104
|
20
|
1105 DEFUN ("start-process-internal", Fstart_process_internal, 3, MANY, 0, /*
|
0
|
1106 Start a program in a subprocess. Return the process object for it.
|
|
1107 Args are NAME BUFFER PROGRAM &rest PROGRAM-ARGS
|
|
1108 NAME is name for process. It is modified if necessary to make it unique.
|
|
1109 BUFFER is the buffer or (buffer-name) to associate with the process.
|
|
1110 Process output goes at end of that buffer, unless you specify
|
|
1111 an output stream or filter function to handle the output.
|
|
1112 BUFFER may be also nil, meaning that this process is not associated
|
|
1113 with any buffer
|
|
1114 Third arg is program file name. It is searched for as in the shell.
|
|
1115 Remaining arguments are strings to give program as arguments.
|
|
1116 INCODE and OUTCODE specify the coding-system objects used in input/output
|
|
1117 from/to the process.
|
20
|
1118 */
|
|
1119 (int nargs, Lisp_Object *args))
|
0
|
1120 {
|
120
|
1121 /* This function can call lisp */
|
0
|
1122 /* !!#### This function has not been Mule-ized */
|
|
1123 Lisp_Object buffer, name, program, proc, current_dir;
|
|
1124 Lisp_Object tem;
|
|
1125 int speccount = specpdl_depth ();
|
120
|
1126 struct gcpro gcpro1, gcpro2, gcpro3;
|
0
|
1127 char **new_argv;
|
|
1128 int i;
|
|
1129
|
120
|
1130 name = args[0];
|
0
|
1131 buffer = args[1];
|
120
|
1132 program = args[2];
|
|
1133 current_dir = Qnil;
|
|
1134
|
|
1135 /* Protect against various file handlers doing GCs below. */
|
|
1136 GCPRO3 (buffer, program, current_dir);
|
|
1137
|
0
|
1138 if (!NILP (buffer))
|
|
1139 buffer = Fget_buffer_create (buffer);
|
|
1140
|
120
|
1141 CHECK_STRING (name);
|
|
1142 CHECK_STRING (program);
|
0
|
1143
|
|
1144 /* Make sure that the child will be able to chdir to the current
|
|
1145 buffer's current directory, or its unhandled equivalent. We
|
|
1146 can't just have the child check for an error when it does the
|
|
1147 chdir, since it's in a vfork.
|
|
1148
|
120
|
1149 Note: these assignments and calls are like this in order to insure
|
|
1150 "caller protects args" GC semantics. */
|
|
1151 current_dir = current_buffer->directory;
|
|
1152 current_dir = Funhandled_file_name_directory (current_dir);
|
|
1153 current_dir = expand_and_dir_to_file (current_dir, Qnil);
|
|
1154
|
0
|
1155 #if 0 /* This loser breaks ange-ftp */
|
120
|
1156 /* dmoore - if you re-enable this code, you have to gcprotect
|
|
1157 current_buffer through the above calls. */
|
|
1158 if (NILP (Ffile_accessible_directory_p (current_dir)))
|
|
1159 report_file_error ("Setting current directory",
|
|
1160 list1 (current_buffer->directory));
|
0
|
1161 #endif /* 0 */
|
|
1162
|
|
1163 /* If program file name is not absolute, search our path for it */
|
82
|
1164 if (!IS_DIRECTORY_SEP (XSTRING_BYTE (program, 0))
|
16
|
1165 && !(XSTRING_LENGTH (program) > 1
|
82
|
1166 && IS_DEVICE_SEP (XSTRING_BYTE (program, 1))))
|
0
|
1167 {
|
120
|
1168 struct gcpro ngcpro1;
|
70
|
1169
|
0
|
1170 tem = Qnil;
|
120
|
1171 NGCPRO1 (tem);
|
0
|
1172 locate_file (Vexec_path, program, EXEC_SUFFIXES, &tem,
|
|
1173 X_OK);
|
|
1174 if (NILP (tem))
|
|
1175 report_file_error ("Searching for program", list1 (program));
|
120
|
1176 program = Fexpand_file_name (tem, Qnil);
|
|
1177 NUNGCPRO;
|
0
|
1178 }
|
|
1179 else
|
|
1180 {
|
|
1181 if (!NILP (Ffile_directory_p (program)))
|
|
1182 error ("Specified program for new process is a directory");
|
|
1183 }
|
|
1184
|
120
|
1185 /* Nothing below here GCs so our string pointers shouldn't move. */
|
185
|
1186 new_argv = alloca_array (char *, nargs - 1);
|
120
|
1187 new_argv[0] = (char *) XSTRING_DATA (program);
|
0
|
1188 for (i = 3; i < nargs; i++)
|
|
1189 {
|
|
1190 tem = args[i];
|
|
1191 CHECK_STRING (tem);
|
16
|
1192 new_argv[i - 2] = (char *) XSTRING_DATA (tem);
|
0
|
1193 }
|
|
1194 new_argv[i - 2] = 0;
|
|
1195
|
|
1196 proc = make_process_internal (name);
|
|
1197
|
|
1198 XPROCESS (proc)->buffer = buffer;
|
|
1199 XPROCESS (proc)->command = Flist (nargs - 2,
|
|
1200 args + 2);
|
|
1201
|
|
1202 /* Make the process marker point into the process buffer (if any). */
|
|
1203 if (!NILP (buffer))
|
|
1204 Fset_marker (XPROCESS (proc)->mark,
|
|
1205 make_int (BUF_ZV (XBUFFER (buffer))), buffer);
|
|
1206
|
|
1207 /* If an error occurs and we can't start the process, we want to
|
|
1208 remove it from the process list. This means that each error
|
|
1209 check in create_process doesn't need to call remove_process
|
|
1210 itself; it's all taken care of here. */
|
|
1211 record_unwind_protect (start_process_unwind, proc);
|
|
1212
|
16
|
1213 create_process (proc, new_argv, (char *) XSTRING_DATA (current_dir));
|
0
|
1214
|
120
|
1215 UNGCPRO;
|
0
|
1216 return unbind_to (speccount, proc);
|
|
1217 }
|
|
1218
|
|
1219
|
|
1220 /* connect to an existing file descriptor. This is very similar to
|
|
1221 open-network-stream except that it assumes that the connection has
|
|
1222 already been initialized. It is currently used for ToolTalk
|
|
1223 communication. */
|
|
1224
|
|
1225 /* This function used to be visible on the Lisp level, but there is no
|
|
1226 real point in doing that. Here is the doc string:
|
|
1227
|
|
1228 "Connect to an existing file descriptor.\n\
|
|
1229 Returns a subprocess-object to represent the connection.\n\
|
|
1230 Input and output work as for subprocesses; `delete-process' closes it.\n\
|
|
1231 Args are NAME BUFFER INFD OUTFD.\n\
|
|
1232 NAME is name for process. It is modified if necessary to make it unique.\n\
|
|
1233 BUFFER is the buffer (or buffer-name) to associate with the process.\n\
|
|
1234 Process output goes at end of that buffer, unless you specify\n\
|
|
1235 an output stream or filter function to handle the output.\n\
|
|
1236 BUFFER may be also nil, meaning that this process is not associated\n\
|
|
1237 with any buffer\n\
|
|
1238 INFD and OUTFD specify the file descriptors to use for input and\n\
|
|
1239 output, respectively."
|
|
1240 */
|
|
1241
|
|
1242 Lisp_Object
|
|
1243 connect_to_file_descriptor (Lisp_Object name, Lisp_Object buffer,
|
|
1244 Lisp_Object infd, Lisp_Object outfd)
|
|
1245 {
|
|
1246 /* This function can GC */
|
|
1247 Lisp_Object proc;
|
|
1248 int inch;
|
|
1249
|
|
1250 CHECK_STRING (name);
|
|
1251 CHECK_INT (infd);
|
|
1252 CHECK_INT (outfd);
|
|
1253
|
|
1254 inch = XINT (infd);
|
|
1255 if (!NILP (descriptor_to_process[inch]))
|
|
1256 error ("There is already a process connected to fd %d", inch);
|
|
1257 if (!NILP (buffer))
|
|
1258 buffer = Fget_buffer_create (buffer);
|
|
1259 proc = make_process_internal (name);
|
|
1260
|
|
1261 descriptor_to_process[inch] = proc;
|
|
1262
|
|
1263 XPROCESS (proc)->pid = Fcons (infd, name);
|
|
1264 XPROCESS (proc)->buffer = buffer;
|
|
1265 init_process_fds (XPROCESS (proc), inch, XINT (outfd));
|
|
1266 XPROCESS (proc)->connected_via_filedesc_p = 1;
|
|
1267
|
|
1268 event_stream_select_process (XPROCESS (proc));
|
|
1269
|
|
1270 return proc;
|
|
1271 }
|
|
1272
|
|
1273
|
|
1274 #ifdef HAVE_SOCKETS
|
|
1275
|
|
1276 static int
|
|
1277 get_internet_address (Lisp_Object host, struct sockaddr_in *address,
|
|
1278 Error_behavior errb)
|
|
1279 {
|
173
|
1280 struct hostent *host_info_ptr = NULL;
|
108
|
1281 #ifdef TRY_AGAIN
|
|
1282 int count = 0;
|
|
1283 #endif
|
0
|
1284
|
|
1285 memset (address, 0, sizeof (*address));
|
|
1286
|
|
1287 while (1)
|
|
1288 {
|
|
1289 #ifdef TRY_AGAIN
|
108
|
1290 if (count++ > 10) break;
|
251
|
1291 #ifndef BROKEN_CYGWIN
|
|
1292 h_errno = 0;
|
|
1293 #endif
|
0
|
1294 #endif
|
|
1295 /* Some systems can't handle SIGIO/SIGALARM in gethostbyname. */
|
|
1296 slow_down_interrupts ();
|
16
|
1297 host_info_ptr = gethostbyname ((char *) XSTRING_DATA (host));
|
0
|
1298 speed_up_interrupts ();
|
|
1299 #ifdef TRY_AGAIN
|
|
1300 if (! (host_info_ptr == 0 && h_errno == TRY_AGAIN))
|
|
1301 #endif
|
|
1302 break;
|
|
1303 Fsleep_for (make_int (1));
|
|
1304 }
|
|
1305 if (host_info_ptr)
|
|
1306 {
|
|
1307 address->sin_family = host_info_ptr->h_addrtype;
|
|
1308 memcpy (&address->sin_addr, host_info_ptr->h_addr, host_info_ptr->h_length);
|
|
1309 }
|
|
1310 else
|
|
1311 {
|
|
1312 IN_ADDR numeric_addr;
|
|
1313 /* Attempt to interpret host as numeric inet address */
|
16
|
1314 numeric_addr = inet_addr ((char *) XSTRING_DATA (host));
|
0
|
1315 if (NUMERIC_ADDR_ERROR)
|
|
1316 {
|
|
1317 maybe_error (Qprocess, errb,
|
16
|
1318 "Unknown host \"%s\"", XSTRING_DATA (host));
|
0
|
1319 return 0;
|
|
1320 }
|
|
1321
|
|
1322 /* There was some broken code here that called strlen() here
|
|
1323 on (char *) &numeric_addr and even sometimes accessed
|
|
1324 uninitialized data. */
|
|
1325 address->sin_family = AF_INET;
|
|
1326 * (IN_ADDR *) &address->sin_addr = numeric_addr;
|
|
1327 }
|
|
1328
|
|
1329 return 1;
|
|
1330 }
|
|
1331
|
|
1332 /* open a TCP network connection to a given HOST/SERVICE. Treated
|
|
1333 exactly like a normal process when reading and writing. Only
|
|
1334 differences are in status display and process deletion. A network
|
|
1335 connection has no PID; you cannot signal it. All you can do is
|
|
1336 deactivate and close it via delete-process */
|
|
1337
|
20
|
1338 DEFUN ("open-network-stream-internal", Fopen_network_stream_internal, 4, 4, 0, /*
|
0
|
1339 Open a TCP connection for a service to a host.
|
|
1340 Returns a subprocess-object to represent the connection.
|
|
1341 Input and output work as for subprocesses; `delete-process' closes it.
|
70
|
1342
|
0
|
1343 NAME is name for process. It is modified if necessary to make it unique.
|
|
1344 BUFFER is the buffer (or buffer-name) to associate with the process.
|
|
1345 Process output goes at end of that buffer, unless you specify
|
|
1346 an output stream or filter function to handle the output.
|
70
|
1347 BUFFER may also be nil, meaning that this process is not associated
|
|
1348 with any buffer.
|
0
|
1349 Third arg is name of the host to connect to, or its IP address.
|
|
1350 Fourth arg SERVICE is name of the service desired, or an integer
|
|
1351 specifying a port number to connect to.
|
20
|
1352 */
|
|
1353 (name, buffer, host, service))
|
0
|
1354 {
|
|
1355 /* !!#### This function has not been Mule-ized */
|
|
1356 /* This function can GC */
|
|
1357 Lisp_Object proc;
|
|
1358 struct sockaddr_in address;
|
|
1359 int s, outch, inch;
|
169
|
1360 volatile int port;
|
0
|
1361 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
|
16
|
1362 volatile int retry = 0;
|
0
|
1363 int retval;
|
|
1364
|
|
1365 GCPRO4 (name, buffer, host, service);
|
|
1366 CHECK_STRING (name);
|
|
1367 CHECK_STRING (host);
|
|
1368 if (INTP (service))
|
|
1369 port = htons ((unsigned short) XINT (service));
|
|
1370 else
|
|
1371 {
|
|
1372 struct servent *svc_info;
|
|
1373 CHECK_STRING (service);
|
16
|
1374 svc_info = getservbyname ((char *) XSTRING_DATA (service), "tcp");
|
0
|
1375 if (svc_info == 0)
|
|
1376 #ifdef WIN32
|
|
1377 error ("Unknown service \"%s\" (%d)",
|
16
|
1378 XSTRING_DATA (service), WSAGetLastError ());
|
0
|
1379 #else
|
16
|
1380 error ("Unknown service \"%s\"", XSTRING_DATA (service));
|
0
|
1381 #endif
|
|
1382 port = svc_info->s_port;
|
|
1383 }
|
|
1384
|
|
1385 get_internet_address (host, &address, ERROR_ME);
|
|
1386 address.sin_port = port;
|
|
1387
|
|
1388 s = socket (address.sin_family, SOCK_STREAM, 0);
|
185
|
1389 if (s < 0)
|
0
|
1390 report_file_error ("error creating socket", list1 (name));
|
|
1391
|
|
1392 /* Turn off interrupts here -- see comments below. There used to
|
|
1393 be code which called bind_polling_period() to slow the polling
|
|
1394 period down rather than turn it off, but that seems rather
|
|
1395 bogus to me. Best thing here is to use a non-blocking connect
|
|
1396 or something, to check for QUIT. */
|
|
1397
|
|
1398 /* Comments that are not quite valid: */
|
|
1399
|
|
1400 /* Kernel bugs (on Ultrix at least) cause lossage (not just EINTR)
|
|
1401 when connect is interrupted. So let's not let it get interrupted.
|
|
1402 Note we do not turn off polling, because polling is only used
|
|
1403 when not interrupt_input, and thus not normally used on the systems
|
|
1404 which have this bug. On systems which use polling, there's no way
|
|
1405 to quit if polling is turned off. */
|
|
1406
|
|
1407 /* Slow down polling. Some kernels have a bug which causes retrying
|
|
1408 connect to fail after a connect. */
|
|
1409
|
|
1410 slow_down_interrupts ();
|
|
1411
|
|
1412 loop:
|
|
1413
|
|
1414 /* A system call interrupted with a SIGALRM or SIGIO comes back
|
|
1415 here, with can_break_system_calls reset to 0. */
|
|
1416 SETJMP (break_system_call_jump);
|
|
1417 if (QUITP)
|
|
1418 {
|
|
1419 speed_up_interrupts ();
|
|
1420 REALLY_QUIT;
|
|
1421 /* In case something really weird happens ... */
|
|
1422 slow_down_interrupts ();
|
|
1423 }
|
|
1424
|
|
1425 /* Break out of connect with a signal (it isn't otherwise possible).
|
|
1426 Thus you don't get screwed with a hung network. */
|
|
1427 can_break_system_calls = 1;
|
|
1428 retval = connect (s, (struct sockaddr *) &address, sizeof (address));
|
|
1429 can_break_system_calls = 0;
|
|
1430 if (retval == -1 && errno != EISCONN)
|
|
1431 {
|
|
1432 int xerrno = errno;
|
|
1433 if (errno == EINTR)
|
|
1434 goto loop;
|
|
1435 if (errno == EADDRINUSE && retry < 20)
|
|
1436 {
|
|
1437 /* A delay here is needed on some FreeBSD systems,
|
|
1438 and it is harmless, since this retrying takes time anyway
|
104
|
1439 and should be infrequent.
|
|
1440 `sleep-for' allowed for quitting this loop with interrupts
|
|
1441 slowed down so it can't be used here. Async timers should
|
|
1442 already be disabled at this point so we can use `sleep'. */
|
|
1443 sleep (1);
|
0
|
1444 retry++;
|
|
1445 goto loop;
|
|
1446 }
|
|
1447
|
|
1448 close (s);
|
|
1449
|
|
1450 speed_up_interrupts ();
|
|
1451
|
|
1452 errno = xerrno;
|
|
1453 report_file_error ("connection failed", list2 (host, name));
|
|
1454 }
|
|
1455
|
|
1456 speed_up_interrupts ();
|
|
1457
|
|
1458 inch = s;
|
|
1459 outch = dup (s);
|
|
1460 if (outch < 0)
|
|
1461 {
|
|
1462 close (s); /* this used to be leaked; from Kyle Jones */
|
|
1463 report_file_error ("error duplicating socket", list1 (name));
|
|
1464 }
|
|
1465
|
|
1466 if (!NILP (buffer))
|
|
1467 buffer = Fget_buffer_create (buffer);
|
|
1468 proc = make_process_internal (name);
|
|
1469
|
|
1470 descriptor_to_process[inch] = proc;
|
|
1471
|
153
|
1472 #ifdef PROCESS_IO_BLOCKING
|
|
1473 {
|
|
1474 Lisp_Object tail;
|
|
1475
|
|
1476 for (tail = network_stream_blocking_port_list; CONSP (tail); tail = XCDR (tail))
|
|
1477 {
|
|
1478 Lisp_Object tail_port = XCAR (tail);
|
|
1479
|
|
1480 if (STRINGP (tail_port))
|
|
1481 {
|
|
1482 struct servent *svc_info;
|
|
1483 CHECK_STRING (tail_port);
|
|
1484 svc_info = getservbyname ((char *) XSTRING_DATA (tail_port), "tcp");
|
|
1485 if ((svc_info != 0) && (svc_info->s_port == port))
|
|
1486 break;
|
|
1487 else
|
|
1488 continue;
|
|
1489 }
|
|
1490 else if ((INTP (tail_port)) && (htons ((unsigned short) XINT (tail_port)) == port))
|
|
1491 break;
|
|
1492 }
|
|
1493
|
|
1494 if (!CONSP (tail))
|
|
1495 {
|
|
1496 #endif /* PROCESS_IO_BLOCKING */
|
0
|
1497 set_descriptor_non_blocking (inch);
|
153
|
1498 #ifdef PROCESS_IO_BLOCKING
|
|
1499 }
|
|
1500 }
|
|
1501 #endif /* PROCESS_IO_BLOCKING */
|
0
|
1502
|
|
1503 XPROCESS (proc)->pid = Fcons (service, host);
|
|
1504 XPROCESS (proc)->buffer = buffer;
|
|
1505 init_process_fds (XPROCESS (proc), inch, outch);
|
|
1506 XPROCESS (proc)->connected_via_filedesc_p = 0;
|
|
1507
|
|
1508 event_stream_select_process (XPROCESS (proc));
|
|
1509
|
|
1510 UNGCPRO;
|
|
1511 return proc;
|
|
1512 }
|
|
1513
|
259
|
1514 #ifdef HAVE_MULTICAST
|
|
1515 /* Didier Verna <verna@inf.enst.fr> Nov. 28 1997.
|
|
1516
|
|
1517 This function is similar to open-network-stream-internal, but provides a
|
|
1518 mean to open an UDP multicast connection instead of a TCP one. Like in the
|
|
1519 TCP case, the multicast connection will be seen as a sub-process,
|
|
1520
|
|
1521 Some notes:
|
|
1522 - Normaly, we should use sendto and recvfrom with non connected
|
|
1523 sockets. The current code doesn't allow us to do this. In the future, it
|
|
1524 would be a good idea to extend the process data structure in order to deal
|
|
1525 properly with the different types network connections.
|
|
1526 - For the same reason, when leaving a multicast group, it is better to make
|
|
1527 a setsockopt - IP_DROP_MEMBERSHIP before closing the descriptors.
|
|
1528 Unfortunately, this can't be done here because delete_process doesn't know
|
|
1529 about the kind of connection we have. However, this is not such an
|
|
1530 important issue.
|
|
1531 */
|
|
1532 DEFUN ("open-multicast-group-internal", Fopen_multicast_group_internal, 5, 5, 0, /*
|
|
1533 Open a multicast connection on the specified dest/port/ttl.
|
|
1534 Returns a subprocess-object to represent the connection.
|
|
1535 Input and output work as for subprocesses; `delete-process' closes it.
|
|
1536
|
|
1537 NAME is name for process. It is modified if necessary to make it unique.
|
|
1538 BUFFER is the buffer (or buffer-name) to associate with the process.
|
|
1539 Process output goes at end of that buffer, unless you specify
|
|
1540 an output stream or filter function to handle the output.
|
|
1541 BUFFER may also be nil, meaning that this process is not associated
|
|
1542 with any buffer.
|
|
1543 Third, fourth and fifth args are the multicast destination group, port and ttl.
|
|
1544 dest must be an internet address between 224.0.0.0 and 239.255.255.255
|
|
1545 port is a communication port like in traditional unicast
|
|
1546 ttl is the time-to-live (15 for site, 63 for region and 127 for world)
|
|
1547 */
|
|
1548 (name, buffer, dest, port, ttl))
|
|
1549 {
|
|
1550 /* !!#### This function has not been Mule-ized */
|
|
1551 /* This function can GC */
|
|
1552 Lisp_Object proc;
|
|
1553 struct ip_mreq imr;
|
|
1554 struct sockaddr_in sa;
|
|
1555 struct protoent *udp;
|
|
1556 int ws, rs;
|
|
1557 int theport;
|
|
1558 unsigned char thettl;
|
|
1559 int one = 1; /* For REUSEADDR */
|
|
1560 int ret;
|
|
1561 volatile int retry = 0;
|
|
1562 struct gcpro gcpro1;
|
|
1563
|
|
1564 CHECK_STRING (name);
|
|
1565 CHECK_STRING (dest);
|
|
1566
|
|
1567 CHECK_NATNUM (port);
|
|
1568 theport = htons ((unsigned short) XINT (port));
|
|
1569
|
|
1570 CHECK_NATNUM (ttl);
|
|
1571 thettl = (unsigned char) XINT (ttl);
|
|
1572
|
|
1573 if ((udp = getprotobyname ("udp")) == NULL)
|
|
1574 error ("No info available for UDP protocol");
|
|
1575
|
|
1576 /* Init the sockets. Yes, I need 2 sockets. I couldn't duplicate one. */
|
|
1577 if ((rs = socket (PF_INET, SOCK_DGRAM, udp->p_proto)) < 0)
|
|
1578 report_file_error ("error creating socket", list1(name));
|
|
1579 if ((ws = socket (PF_INET, SOCK_DGRAM, udp->p_proto)) < 0)
|
|
1580 {
|
|
1581 close (rs);
|
|
1582 report_file_error ("error creating socket", list1(name));
|
|
1583 }
|
|
1584
|
|
1585 /* This will be used for both sockets */
|
|
1586 bzero(&sa, sizeof(sa));
|
|
1587 sa.sin_family = AF_INET;
|
|
1588 sa.sin_port = theport;
|
|
1589 sa.sin_addr.s_addr = htonl (inet_addr ((char *) XSTRING_DATA (dest)));
|
|
1590
|
|
1591 /* Socket configuration for reading ------------------------ */
|
|
1592
|
|
1593 /* Multiple connections from the same machine. This must be done before
|
|
1594 bind. If it fails, it shouldn't be fatal. The only consequence is that
|
|
1595 people won't be able to connect twice from the same machine. */
|
|
1596 if (setsockopt (rs, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof (one))
|
|
1597 < 0)
|
|
1598 warn_when_safe (Qmulticast, Qwarning, "Cannot reuse socket address");
|
|
1599
|
|
1600 /* bind socket name */
|
|
1601 if (bind (rs, (struct sockaddr *)&sa, sizeof(sa)))
|
|
1602 {
|
|
1603 close (rs);
|
|
1604 close (ws);
|
|
1605 report_file_error ("error binding socket", list2(name, port));
|
|
1606 }
|
|
1607
|
|
1608 /* join multicast group */
|
|
1609 imr.imr_multiaddr.s_addr = htonl (inet_addr ((char *) XSTRING_DATA (dest)));
|
|
1610 imr.imr_interface.s_addr = htonl (INADDR_ANY);
|
|
1611 if (setsockopt (rs, IPPROTO_IP, IP_ADD_MEMBERSHIP,
|
|
1612 (char *) &imr, sizeof (struct ip_mreq)) < 0)
|
|
1613 {
|
|
1614 close (ws);
|
|
1615 close (rs);
|
|
1616 report_file_error ("error adding membership", list2(name, dest));
|
|
1617 }
|
|
1618
|
|
1619 /* Socket configuration for writing ----------------------- */
|
|
1620
|
|
1621 /* Normaly, there's no 'connect' in multicast, since we use preferentialy
|
|
1622 'sendto' and 'recvfrom'. However, in order to handle this connection in
|
|
1623 the process-like way it is done for TCP, we must be able to use 'write'
|
|
1624 instead of 'sendto'. Consequently, we 'connect' this socket. */
|
|
1625
|
|
1626 /* See open-network-stream-internal for comments on this part of the code */
|
|
1627 slow_down_interrupts ();
|
|
1628
|
|
1629 loop:
|
|
1630
|
|
1631 /* A system call interrupted with a SIGALRM or SIGIO comes back
|
|
1632 here, with can_break_system_calls reset to 0. */
|
|
1633 SETJMP (break_system_call_jump);
|
|
1634 if (QUITP)
|
|
1635 {
|
|
1636 speed_up_interrupts ();
|
|
1637 REALLY_QUIT;
|
|
1638 /* In case something really weird happens ... */
|
|
1639 slow_down_interrupts ();
|
|
1640 }
|
|
1641
|
|
1642 /* Break out of connect with a signal (it isn't otherwise possible).
|
|
1643 Thus you don't get screwed with a hung network. */
|
|
1644 can_break_system_calls = 1;
|
|
1645 ret = connect (ws, (struct sockaddr *) &sa, sizeof (sa));
|
|
1646 can_break_system_calls = 0;
|
|
1647 if (ret == -1 && errno != EISCONN)
|
|
1648 {
|
|
1649 int xerrno = errno;
|
|
1650
|
|
1651 if (errno == EINTR)
|
|
1652 goto loop;
|
|
1653 if (errno == EADDRINUSE && retry < 20)
|
|
1654 {
|
|
1655 /* A delay here is needed on some FreeBSD systems,
|
|
1656 and it is harmless, since this retrying takes time anyway
|
|
1657 and should be infrequent.
|
|
1658 `sleep-for' allowed for quitting this loop with interrupts
|
|
1659 slowed down so it can't be used here. Async timers should
|
|
1660 already be disabled at this point so we can use `sleep'. */
|
|
1661 sleep (1);
|
|
1662 retry++;
|
|
1663 goto loop;
|
|
1664 }
|
|
1665
|
|
1666 close (rs);
|
|
1667 close (ws);
|
|
1668 speed_up_interrupts ();
|
|
1669
|
|
1670 errno = xerrno;
|
|
1671 report_file_error ("error connecting socket", list2(name, port));
|
|
1672 }
|
|
1673
|
|
1674 speed_up_interrupts ();
|
|
1675
|
|
1676 /* scope */
|
|
1677 if (setsockopt (ws, IPPROTO_IP, IP_MULTICAST_TTL,
|
|
1678 (char *) &thettl, sizeof (thettl)) < 0)
|
|
1679 {
|
|
1680 close (rs);
|
|
1681 close (ws);
|
|
1682 report_file_error ("error setting ttl", list2(name, ttl));
|
|
1683 }
|
|
1684
|
|
1685 if (!NILP (buffer))
|
|
1686 buffer = Fget_buffer_create (buffer);
|
|
1687
|
|
1688 proc = make_process_internal (name);
|
|
1689 GCPRO1 (proc);
|
|
1690
|
|
1691 descriptor_to_process[rs] = proc;
|
|
1692
|
|
1693 #ifdef PROCESS_IO_BLOCKING
|
|
1694 {
|
|
1695 Lisp_Object tail;
|
|
1696
|
|
1697 for (tail = network_stream_blocking_port_list;
|
|
1698 CONSP (tail); tail = XCDR (tail))
|
|
1699 {
|
|
1700 Lisp_Object tail_port = XCAR (tail);
|
|
1701
|
|
1702 if (STRINGP (tail_port))
|
|
1703 {
|
|
1704 struct servent *svc_info;
|
|
1705
|
|
1706 svc_info =
|
|
1707 getservbyname ((char *) XSTRING_DATA (tail_port), "udp");
|
|
1708 if ((svc_info != 0) && (svc_info->s_port == theport))
|
|
1709 break;
|
|
1710 else
|
|
1711 continue;
|
|
1712 }
|
|
1713 else if ((INTP (tail_port)) &&
|
|
1714 (htons ((unsigned short) XINT (tail_port)) == theport))
|
|
1715 break;
|
|
1716 }
|
|
1717
|
|
1718 if (!CONSP (tail))
|
|
1719 {
|
|
1720 #endif /* PROCESS_IO_BLOCKING */
|
|
1721 set_descriptor_non_blocking (rs);
|
|
1722 #ifdef PROCESS_IO_BLOCKING
|
|
1723 }
|
|
1724 }
|
|
1725 #endif /* PROCESS_IO_BLOCKING */
|
|
1726
|
|
1727 XPROCESS (proc)->pid = Fcons (port, dest);
|
|
1728 XPROCESS (proc)->buffer = buffer;
|
|
1729 init_process_fds (XPROCESS (proc), rs, ws);
|
|
1730 XPROCESS (proc)->connected_via_filedesc_p = 0;
|
|
1731
|
|
1732 event_stream_select_process (XPROCESS (proc));
|
|
1733
|
|
1734 UNGCPRO;
|
|
1735 return proc;
|
|
1736 }
|
|
1737 #endif /* HAVE_MULTICAST */
|
|
1738
|
0
|
1739 #endif /* HAVE_SOCKETS */
|
|
1740
|
|
1741 Lisp_Object
|
|
1742 canonicalize_host_name (Lisp_Object host)
|
|
1743 {
|
|
1744 #ifdef HAVE_SOCKETS
|
|
1745 /* #### for HAVE_TERM, you probably have to do something else. */
|
|
1746 struct sockaddr_in address;
|
|
1747
|
|
1748 if (!get_internet_address (host, &address, ERROR_ME_NOT))
|
|
1749 return host;
|
|
1750
|
|
1751 if (address.sin_family == AF_INET)
|
|
1752 return build_string (inet_ntoa (address.sin_addr));
|
|
1753 else
|
|
1754 /* #### any clue what to do here? */
|
|
1755 return host;
|
|
1756 #else
|
|
1757 return host;
|
|
1758 #endif
|
|
1759 }
|
|
1760
|
|
1761
|
20
|
1762 DEFUN ("set-process-window-size", Fset_process_window_size, 3, 3, 0, /*
|
0
|
1763 Tell PROCESS that it has logical window size HEIGHT and WIDTH.
|
20
|
1764 */
|
|
1765 (proc, height, width))
|
0
|
1766 {
|
|
1767 CHECK_PROCESS (proc);
|
|
1768 CHECK_NATNUM (height);
|
|
1769 CHECK_NATNUM (width);
|
|
1770 if (set_window_size (XPROCESS (proc)->infd, XINT (height), XINT (width))
|
|
1771 <= 0)
|
|
1772 return Qnil;
|
|
1773 else
|
|
1774 return Qt;
|
|
1775 }
|
|
1776
|
|
1777
|
|
1778 /************************************************************************/
|
|
1779 /* Process I/O */
|
|
1780 /************************************************************************/
|
|
1781
|
|
1782 /* (Faccept_process_output is now in event-stream.c) */
|
|
1783
|
|
1784 /* Some FSFmacs error handlers here. We handle this
|
|
1785 in call2_trapping_errors(). */
|
|
1786
|
|
1787 /* Read pending output from the process channel,
|
|
1788 starting with our buffered-ahead character if we have one.
|
|
1789 Yield number of characters read.
|
|
1790
|
|
1791 This function reads at most 1024 bytes.
|
|
1792 If you want to read all available subprocess output,
|
|
1793 you must call it repeatedly until it returns zero. */
|
|
1794
|
|
1795 Charcount
|
|
1796 read_process_output (Lisp_Object proc)
|
|
1797 {
|
|
1798 /* This function can GC */
|
|
1799 Bytecount nbytes, nchars;
|
|
1800 Bufbyte chars[1024];
|
|
1801 Lisp_Object outstream;
|
|
1802 struct Lisp_Process *p = XPROCESS (proc);
|
|
1803
|
|
1804 /* If there is a lot of output from the subprocess, the loop in
|
|
1805 execute_internal_event() might call read_process_output() more
|
|
1806 than once. If the filter that was executed from one of these
|
|
1807 calls set the filter to t, we have to stop now. Return -1 rather
|
|
1808 than 0 so execute_internal_event() doesn't close the process.
|
|
1809 Really, the loop in execute_internal_event() should check itself
|
|
1810 for a process-filter change, like in status_notify(); but the
|
|
1811 struct Lisp_Process is not exported outside of this file. */
|
|
1812 if (p->infd < 0)
|
|
1813 return -1; /* already closed */
|
|
1814
|
|
1815 if (!NILP (p->filter) && (p->filter_does_read))
|
|
1816 {
|
|
1817 Lisp_Object filter_result;
|
|
1818
|
|
1819 /* Some weird FSFmacs crap here with
|
|
1820 Vdeactivate_mark and current_buffer->keymap */
|
|
1821 running_asynch_code = 1;
|
|
1822 filter_result = call2_trapping_errors ("Error in process filter",
|
|
1823 p->filter, proc, Qnil);
|
|
1824 running_asynch_code = 0;
|
|
1825 restore_match_data ();
|
|
1826 CHECK_INT (filter_result);
|
|
1827 return XINT (filter_result);
|
|
1828 }
|
|
1829
|
|
1830 #if 0 /* FSFmacs */
|
|
1831 /* #### equivalent code from FSFmacs. Would need some porting
|
|
1832 for Windows NT. */
|
|
1833 if (proc_buffered_char[channel] < 0)
|
|
1834 #ifdef WINDOWSNT
|
|
1835 nchars = read_child_output (channel, chars, sizeof (chars));
|
|
1836 #else
|
|
1837 nchars = read (channel, chars, sizeof chars);
|
|
1838 #endif
|
|
1839 else
|
|
1840 {
|
|
1841 chars[0] = proc_buffered_char[channel];
|
|
1842 proc_buffered_char[channel] = -1;
|
|
1843 #ifdef WINDOWSNT
|
|
1844 nchars = read_child_output (channel, chars + 1, sizeof (chars) - 1);
|
|
1845 #else
|
|
1846 nchars = read (channel, chars + 1, sizeof chars - 1);
|
|
1847 #endif
|
|
1848 if (nchars < 0)
|
|
1849 nchars = 1;
|
|
1850 else
|
|
1851 nchars = nchars + 1;
|
|
1852 }
|
|
1853 #endif /* FSFmacs */
|
|
1854
|
|
1855 nbytes = Lstream_read (XLSTREAM (p->instream), chars, sizeof (chars));
|
|
1856 if (nbytes <= 0) return nbytes;
|
|
1857
|
|
1858 nchars = bytecount_to_charcount (chars, nbytes);
|
|
1859 outstream = p->filter;
|
|
1860 if (!NILP (outstream))
|
|
1861 {
|
|
1862 /* We used to bind inhibit-quit to t here, but
|
|
1863 call2_trapping_errors() does that for us. */
|
|
1864 running_asynch_code = 1;
|
|
1865 call2_trapping_errors ("Error in process filter",
|
|
1866 outstream, proc, make_string (chars, nbytes));
|
|
1867 running_asynch_code = 0;
|
|
1868 restore_match_data ();
|
173
|
1869 return nchars;
|
0
|
1870 }
|
|
1871
|
|
1872 /* If no filter, write into buffer if it isn't dead. */
|
|
1873 if (!NILP (p->buffer) && BUFFER_LIVE_P (XBUFFER (p->buffer)))
|
|
1874 {
|
|
1875 Lisp_Object old_read_only = Qnil;
|
|
1876 Bufpos old_point;
|
|
1877 Bufpos old_begv;
|
|
1878 Bufpos old_zv;
|
|
1879 int old_zmacs_region_stays = zmacs_region_stays;
|
|
1880 struct gcpro gcpro1, gcpro2;
|
|
1881 struct buffer *buf = XBUFFER (p->buffer);
|
|
1882
|
|
1883 GCPRO2 (proc, old_read_only);
|
|
1884
|
|
1885 old_point = BUF_PT (buf);
|
|
1886 old_begv = BUF_BEGV (buf);
|
|
1887 old_zv = BUF_ZV (buf);
|
|
1888 old_read_only = buf->read_only;
|
|
1889 buf->read_only = Qnil;
|
|
1890
|
|
1891 /* Insert new output into buffer
|
|
1892 at the current end-of-output marker,
|
|
1893 thus preserving logical ordering of input and output. */
|
|
1894 if (XMARKER (p->mark)->buffer)
|
|
1895 BUF_SET_PT (buf,
|
|
1896 bufpos_clip_to_bounds (old_begv, marker_position (p->mark),
|
|
1897 old_zv));
|
|
1898 else
|
|
1899 BUF_SET_PT (buf, old_zv);
|
|
1900
|
|
1901 /* If the output marker is outside of the visible region, save
|
|
1902 the restriction and widen. */
|
|
1903 if (! (BUF_BEGV (buf) <= BUF_PT (buf) &&
|
|
1904 BUF_PT (buf) <= BUF_ZV (buf)))
|
|
1905 Fwiden (p->buffer);
|
|
1906
|
|
1907 /* Make sure opoint floats ahead of any new text, just as point
|
|
1908 would. */
|
|
1909 if (BUF_PT (buf) <= old_point)
|
|
1910 old_point += nchars;
|
|
1911
|
|
1912 /* Insert after old_begv, but before old_zv. */
|
|
1913 if (BUF_PT (buf) < old_begv)
|
|
1914 old_begv += nchars;
|
|
1915 if (BUF_PT (buf) <= old_zv)
|
|
1916 old_zv += nchars;
|
|
1917
|
|
1918 #if 0
|
|
1919 /* This screws up intial display of the window. jla */
|
|
1920
|
|
1921 /* Insert before markers in case we are inserting where
|
|
1922 the buffer's mark is, and the user's next command is Meta-y. */
|
|
1923 buffer_insert_raw_string_1 (buf, -1, chars,
|
|
1924 nbytes, INSDEL_BEFORE_MARKERS);
|
|
1925 #else
|
|
1926 buffer_insert_raw_string (buf, chars, nbytes);
|
|
1927 #endif
|
|
1928
|
|
1929 Fset_marker (p->mark, make_int (BUF_PT (buf)), p->buffer);
|
|
1930
|
|
1931 MARK_MODELINE_CHANGED;
|
|
1932
|
|
1933 /* If the restriction isn't what it should be, set it. */
|
|
1934 if (old_begv != BUF_BEGV (buf) || old_zv != BUF_ZV (buf))
|
161
|
1935 {
|
|
1936 Fwiden(p->buffer);
|
|
1937 old_begv = bufpos_clip_to_bounds (BUF_BEG (buf),
|
|
1938 old_begv,
|
|
1939 BUF_Z (buf));
|
|
1940 old_zv = bufpos_clip_to_bounds (BUF_BEG (buf),
|
|
1941 old_zv,
|
|
1942 BUF_Z (buf));
|
|
1943 Fnarrow_to_region (make_int (old_begv), make_int (old_zv),
|
|
1944 p->buffer);
|
|
1945 }
|
0
|
1946
|
|
1947 /* Handling the process output should not deactivate the mark. */
|
|
1948 zmacs_region_stays = old_zmacs_region_stays;
|
|
1949 buf->read_only = old_read_only;
|
161
|
1950 old_point = bufpos_clip_to_bounds (BUF_BEGV (buf),
|
|
1951 old_point,
|
|
1952 BUF_ZV (buf));
|
0
|
1953 BUF_SET_PT (buf, old_point);
|
|
1954
|
|
1955 UNGCPRO;
|
|
1956 }
|
173
|
1957 return nchars;
|
0
|
1958 }
|
|
1959
|
|
1960 /* Sending data to subprocess */
|
|
1961
|
|
1962 static JMP_BUF send_process_frame;
|
|
1963
|
|
1964 static SIGTYPE
|
|
1965 send_process_trap (int signum)
|
|
1966 {
|
|
1967 EMACS_REESTABLISH_SIGNAL (signum, send_process_trap);
|
|
1968 EMACS_UNBLOCK_SIGNAL (signum);
|
|
1969 LONGJMP (send_process_frame, 1);
|
|
1970 }
|
|
1971
|
|
1972 /* send some data to process PROC. If NONRELOCATABLE is non-NULL, it
|
|
1973 specifies the address of the data. Otherwise, the data comes from the
|
|
1974 object RELOCATABLE (either a string or a buffer). START and LEN
|
|
1975 specify the offset and length of the data to send.
|
|
1976
|
|
1977 Note that START and LEN are in Bufpos's if RELOCATABLE is a buffer,
|
|
1978 and in Bytecounts otherwise. */
|
|
1979
|
|
1980 static void
|
|
1981 send_process (volatile Lisp_Object proc,
|
|
1982 Lisp_Object relocatable, CONST Bufbyte *nonrelocatable,
|
|
1983 int start, int len)
|
|
1984 {
|
|
1985 /* This function can GC */
|
|
1986 /* Use volatile to protect variables from being clobbered by longjmp. */
|
|
1987 struct gcpro gcpro1, gcpro2;
|
16
|
1988 SIGTYPE (*volatile old_sigpipe) (int) = 0;
|
0
|
1989 Lisp_Object lstream = Qnil;
|
|
1990 volatile struct Lisp_Process *p = XPROCESS (proc);
|
|
1991 #if defined (NO_UNION_TYPE) /* || !defined (__GNUC__) GCC bug only??? */
|
|
1992 /* #### ugh! There must be a better solution. */
|
|
1993 Lisp_Object defeat_volatile_kludge = (Lisp_Object) proc;
|
|
1994 #else
|
|
1995 Lisp_Object defeat_volatile_kludge = proc;
|
|
1996 #endif
|
|
1997
|
|
1998 GCPRO2 (defeat_volatile_kludge, lstream);
|
|
1999
|
|
2000 if (p->outfd < 0)
|
|
2001 signal_simple_error ("Process not open for writing", proc);
|
|
2002
|
|
2003 if (nonrelocatable)
|
|
2004 lstream =
|
|
2005 make_fixed_buffer_input_stream (nonrelocatable + start, len);
|
|
2006 else if (GC_BUFFERP (relocatable))
|
|
2007 lstream = make_lisp_buffer_input_stream (XBUFFER (relocatable),
|
|
2008 start, start + len, 0);
|
|
2009 else
|
|
2010 lstream = make_lisp_string_input_stream (relocatable, start, len);
|
|
2011
|
|
2012 if (!SETJMP (send_process_frame))
|
|
2013 {
|
|
2014 /* use a reasonable-sized buffer (somewhere around the size of the
|
|
2015 stream buffer) so as to avoid inundating the stream with blocked
|
|
2016 data. */
|
|
2017 Bufbyte chunkbuf[512];
|
|
2018 Bytecount chunklen;
|
|
2019
|
|
2020 while (1)
|
|
2021 {
|
|
2022 int writeret;
|
|
2023
|
|
2024 chunklen = Lstream_read (XLSTREAM (lstream), chunkbuf, 512);
|
|
2025 if (chunklen <= 0)
|
|
2026 break; /* perhaps should abort() if < 0?
|
|
2027 This should never happen. */
|
|
2028 old_sigpipe =
|
|
2029 (SIGTYPE (*) (int)) signal (SIGPIPE, send_process_trap);
|
|
2030 /* Lstream_write() will never successfully write less than
|
|
2031 the amount sent in. In the worst case, it just buffers
|
|
2032 the unwritten data. */
|
|
2033 writeret = Lstream_write (XLSTREAM (p->outstream), chunkbuf,
|
|
2034 chunklen);
|
|
2035 signal (SIGPIPE, old_sigpipe);
|
|
2036 if (writeret < 0)
|
|
2037 /* This is a real error. Blocking errors are handled
|
|
2038 specially inside of the filedesc stream. */
|
|
2039 report_file_error ("writing to process",
|
|
2040 list1 (proc));
|
|
2041 while (filedesc_stream_was_blocked (XLSTREAM (p->filedesc_stream)))
|
|
2042 {
|
185
|
2043 /* Buffer is full. Wait, accepting input;
|
0
|
2044 that may allow the program
|
|
2045 to finish doing output and read more. */
|
|
2046 Faccept_process_output (Qnil, make_int (1), Qnil);
|
|
2047 old_sigpipe =
|
|
2048 (SIGTYPE (*) (int)) signal (SIGPIPE, send_process_trap);
|
|
2049 Lstream_flush (XLSTREAM (p->filedesc_stream));
|
|
2050 signal (SIGPIPE, old_sigpipe);
|
|
2051 }
|
|
2052 }
|
|
2053 }
|
|
2054 else
|
|
2055 { /* We got here from a longjmp() from the SIGPIPE handler */
|
|
2056 signal (SIGPIPE, old_sigpipe);
|
|
2057 p->status_symbol = Qexit;
|
|
2058 p->exit_code = 256; /* #### SIGPIPE ??? */
|
|
2059 p->core_dumped = 0;
|
|
2060 p->tick++;
|
|
2061 process_tick++;
|
|
2062 deactivate_process (proc);
|
|
2063 error ("SIGPIPE raised on process %s; closed it",
|
70
|
2064 XSTRING_DATA (p->name));
|
0
|
2065 }
|
207
|
2066 old_sigpipe = (SIGTYPE (*) (int)) signal (SIGPIPE, send_process_trap);
|
0
|
2067 Lstream_flush (XLSTREAM (p->outstream));
|
207
|
2068 signal (SIGPIPE, old_sigpipe);
|
0
|
2069 UNGCPRO;
|
185
|
2070 Lstream_delete (XLSTREAM (lstream));
|
0
|
2071 }
|
|
2072
|
20
|
2073 DEFUN ("process-tty-name", Fprocess_tty_name, 1, 1, 0, /*
|
0
|
2074 Return the name of the terminal PROCESS uses, or nil if none.
|
|
2075 This is the terminal that the process itself reads and writes on,
|
|
2076 not the name of the pty that Emacs uses to talk with that terminal.
|
20
|
2077 */
|
|
2078 (proc))
|
0
|
2079 {
|
|
2080 CHECK_PROCESS (proc);
|
|
2081 return XPROCESS (proc)->tty_name;
|
|
2082 }
|
|
2083
|
20
|
2084 DEFUN ("set-process-buffer", Fset_process_buffer, 2, 2, 0, /*
|
0
|
2085 Set buffer associated with PROCESS to BUFFER (a buffer, or nil).
|
20
|
2086 */
|
|
2087 (proc, buffer))
|
0
|
2088 {
|
|
2089 CHECK_PROCESS (proc);
|
|
2090 if (!NILP (buffer))
|
|
2091 CHECK_BUFFER (buffer);
|
|
2092 XPROCESS (proc)->buffer = buffer;
|
|
2093 return buffer;
|
|
2094 }
|
|
2095
|
20
|
2096 DEFUN ("process-buffer", Fprocess_buffer, 1, 1, 0, /*
|
0
|
2097 Return the buffer PROCESS is associated with.
|
|
2098 Output from PROCESS is inserted in this buffer
|
|
2099 unless PROCESS has a filter.
|
20
|
2100 */
|
|
2101 (proc))
|
0
|
2102 {
|
|
2103 CHECK_PROCESS (proc);
|
|
2104 return XPROCESS (proc)->buffer;
|
|
2105 }
|
|
2106
|
20
|
2107 DEFUN ("process-mark", Fprocess_mark, 1, 1, 0, /*
|
0
|
2108 Return the marker for the end of the last output from PROCESS.
|
20
|
2109 */
|
|
2110 (proc))
|
0
|
2111 {
|
|
2112 CHECK_PROCESS (proc);
|
|
2113 return XPROCESS (proc)->mark;
|
|
2114 }
|
|
2115
|
|
2116 void
|
|
2117 set_process_filter (Lisp_Object proc, Lisp_Object filter, int filter_does_read)
|
|
2118 {
|
|
2119 CHECK_PROCESS (proc);
|
233
|
2120 if (PROCESS_LIVE_P (proc)) {
|
0
|
2121 if (EQ (filter, Qt))
|
|
2122 event_stream_unselect_process (XPROCESS (proc));
|
|
2123 else
|
|
2124 event_stream_select_process (XPROCESS (proc));
|
233
|
2125 }
|
0
|
2126
|
|
2127 XPROCESS (proc)->filter = filter;
|
|
2128 XPROCESS (proc)->filter_does_read = filter_does_read;
|
|
2129 }
|
|
2130
|
20
|
2131 DEFUN ("set-process-filter", Fset_process_filter, 2, 2, 0, /*
|
0
|
2132 Give PROCESS the filter function FILTER; nil means no filter.
|
|
2133 t means stop accepting output from the process.
|
|
2134 When a process has a filter, each time it does output
|
|
2135 the entire string of output is passed to the filter.
|
|
2136 The filter gets two arguments: the process and the string of output.
|
|
2137 If the process has a filter, its buffer is not used for output.
|
20
|
2138 */
|
|
2139 (proc, filter))
|
0
|
2140 {
|
|
2141 set_process_filter (proc, filter, 0);
|
|
2142 return filter;
|
|
2143 }
|
|
2144
|
20
|
2145 DEFUN ("process-filter", Fprocess_filter, 1, 1, 0, /*
|
0
|
2146 Return the filter function of PROCESS; nil if none.
|
|
2147 See `set-process-filter' for more info on filter functions.
|
20
|
2148 */
|
|
2149 (proc))
|
0
|
2150 {
|
|
2151 CHECK_PROCESS (proc);
|
|
2152 return XPROCESS (proc)->filter;
|
|
2153 }
|
|
2154
|
20
|
2155 DEFUN ("process-send-region", Fprocess_send_region, 3, 3, 0, /*
|
0
|
2156 Send current contents of region as input to PROCESS.
|
|
2157 PROCESS may be a process name or an actual process.
|
|
2158 Called from program, takes three arguments, PROCESS, START and END.
|
|
2159 If the region is more than 500 or so characters long,
|
|
2160 it is sent in several bunches. This may happen even for shorter regions.
|
|
2161 Output from processes can arrive in between bunches.
|
20
|
2162 */
|
|
2163 (process, start, end))
|
0
|
2164 {
|
|
2165 /* This function can GC */
|
|
2166 Lisp_Object proc = get_process (process);
|
|
2167 Bufpos st, en;
|
|
2168
|
|
2169 get_buffer_range_char (current_buffer, start, end, &st, &en, 0);
|
|
2170
|
|
2171 send_process (proc, Fcurrent_buffer (), 0,
|
|
2172 st, en - st);
|
173
|
2173 return Qnil;
|
0
|
2174 }
|
|
2175
|
20
|
2176 DEFUN ("process-send-string", Fprocess_send_string, 2, 4, 0, /*
|
0
|
2177 Send PROCESS the contents of STRING as input.
|
|
2178 PROCESS may be a process name or an actual process.
|
|
2179 Optional arguments FROM and TO specify part of STRING, see `substring'.
|
|
2180 If STRING is more than 500 or so characters long,
|
|
2181 it is sent in several bunches. This may happen even for shorter strings.
|
|
2182 Output from processes can arrive in between bunches.
|
20
|
2183 */
|
|
2184 (process, string, from, to))
|
0
|
2185 {
|
|
2186 /* This function can GC */
|
|
2187 Lisp_Object proc;
|
|
2188 Bytecount len;
|
|
2189 Bytecount bfr, bto;
|
|
2190
|
|
2191 proc = get_process (process);
|
|
2192 CHECK_STRING (string);
|
|
2193 get_string_range_byte (string, from, to, &bfr, &bto,
|
|
2194 GB_HISTORICAL_STRING_BEHAVIOR);
|
|
2195 len = bto - bfr;
|
|
2196
|
|
2197 send_process (proc, string, 0, bfr, len);
|
173
|
2198 return Qnil;
|
0
|
2199 }
|
|
2200
|
259
|
2201 #ifdef FILE_CODING
|
70
|
2202
|
|
2203 DEFUN ("process-input-coding-system", Fprocess_input_coding_system, 1, 1, 0, /*
|
|
2204 Return PROCESS's input coding system.
|
|
2205 */
|
|
2206 (process))
|
|
2207 {
|
|
2208 process = get_process (process);
|
|
2209 return decoding_stream_coding_system (XLSTREAM ( XPROCESS (process)->instream) );
|
|
2210 }
|
|
2211
|
|
2212 DEFUN ("process-output-coding-system", Fprocess_output_coding_system, 1, 1, 0, /*
|
|
2213 Return PROCESS's output coding system.
|
|
2214 */
|
|
2215 (process))
|
|
2216 {
|
|
2217 process = get_process (process);
|
|
2218 return encoding_stream_coding_system (XLSTREAM (XPROCESS (process)->outstream));
|
|
2219 }
|
|
2220
|
120
|
2221 DEFUN ("process-coding-system", Fprocess_coding_system, 1, 1, 0, /*
|
|
2222 Return a pair of coding-system for decoding and encoding of PROCESS.
|
|
2223 */
|
|
2224 (process))
|
|
2225 {
|
|
2226 process = get_process (process);
|
175
|
2227 return Fcons (decoding_stream_coding_system
|
|
2228 (XLSTREAM (XPROCESS (process)->instream)),
|
|
2229 encoding_stream_coding_system
|
|
2230 (XLSTREAM (XPROCESS (process)->outstream)));
|
120
|
2231 }
|
|
2232
|
70
|
2233 DEFUN ("set-process-input-coding-system",
|
|
2234 Fset_process_input_coding_system, 2, 2, 0, /*
|
|
2235 Set PROCESS's input coding system to CODESYS.
|
|
2236 */
|
|
2237 (process, codesys))
|
|
2238 {
|
|
2239 codesys = Fget_coding_system (codesys);
|
|
2240 process = get_process (process);
|
|
2241 set_decoding_stream_coding_system ( XLSTREAM ( XPROCESS (process)->instream ), codesys);
|
|
2242 return Qnil;
|
|
2243 }
|
|
2244
|
|
2245 DEFUN ("set-process-output-coding-system",
|
|
2246 Fset_process_output_coding_system, 2, 2, 0, /*
|
|
2247 Set PROCESS's output coding system to CODESYS.
|
|
2248 */
|
|
2249 (process, codesys))
|
|
2250 {
|
|
2251 codesys = Fget_coding_system (codesys);
|
|
2252 process = get_process (process);
|
|
2253 set_encoding_stream_coding_system
|
|
2254 ( XLSTREAM ( XPROCESS (process)->outstream), codesys);
|
|
2255 return Qnil;
|
|
2256 }
|
|
2257
|
120
|
2258 DEFUN ("set-process-coding-system",
|
|
2259 Fset_process_coding_system, 1, 3, 0, /*
|
|
2260 Set coding-systems of PROCESS to DECODING and ENCODING.
|
|
2261 */
|
|
2262 (process, decoding, encoding))
|
|
2263 {
|
|
2264 if(!NILP(decoding)){
|
|
2265 Fset_process_input_coding_system(process, decoding);
|
|
2266 }
|
|
2267 if(!NILP(encoding)){
|
|
2268 Fset_process_output_coding_system(process, encoding);
|
|
2269 }
|
|
2270 return Qnil;
|
|
2271 }
|
|
2272
|
259
|
2273 #endif
|
70
|
2274
|
0
|
2275
|
|
2276 /************************************************************************/
|
|
2277 /* process status */
|
|
2278 /************************************************************************/
|
|
2279
|
|
2280 /* Some FSFmacs error handlers here. We handle this
|
|
2281 in call2_trapping_errors(). */
|
|
2282
|
|
2283 static Lisp_Object
|
|
2284 exec_sentinel_unwind (Lisp_Object datum)
|
|
2285 {
|
|
2286 struct Lisp_Cons *d = XCONS (datum);
|
|
2287 XPROCESS (d->car)->sentinel = d->cdr;
|
|
2288 free_cons (d);
|
|
2289 return Qnil;
|
|
2290 }
|
|
2291
|
|
2292 static void
|
|
2293 exec_sentinel (Lisp_Object proc, Lisp_Object reason)
|
|
2294 {
|
|
2295 /* This function can GC */
|
175
|
2296 int speccount = specpdl_depth ();
|
0
|
2297 struct Lisp_Process *p = XPROCESS (proc);
|
175
|
2298 Lisp_Object sentinel = p->sentinel;
|
|
2299
|
0
|
2300 if (NILP (sentinel))
|
|
2301 return;
|
|
2302
|
|
2303 /* Some weird FSFmacs crap here with
|
|
2304 Vdeactivate_mark and current_buffer->keymap */
|
|
2305
|
|
2306 /* Zilch the sentinel while it's running, to avoid recursive invocations;
|
|
2307 assure that it gets restored no matter how the sentinel exits. */
|
|
2308 p->sentinel = Qnil;
|
|
2309 record_unwind_protect (exec_sentinel_unwind, noseeum_cons (proc, sentinel));
|
|
2310 /* We used to bind inhibit-quit to t here, but call2_trapping_errors()
|
|
2311 does that for us. */
|
|
2312 running_asynch_code = 1;
|
175
|
2313 call2_trapping_errors ("Error in process sentinel", sentinel, proc, reason);
|
0
|
2314 running_asynch_code = 0;
|
|
2315 restore_match_data ();
|
|
2316 unbind_to (speccount, Qnil);
|
|
2317 }
|
|
2318
|
20
|
2319 DEFUN ("set-process-sentinel", Fset_process_sentinel, 2, 2, 0, /*
|
0
|
2320 Give PROCESS the sentinel SENTINEL; nil for none.
|
|
2321 The sentinel is called as a function when the process changes state.
|
|
2322 It gets two arguments: the process, and a string describing the change.
|
20
|
2323 */
|
|
2324 (proc, sentinel))
|
0
|
2325 {
|
|
2326 CHECK_PROCESS (proc);
|
|
2327 XPROCESS (proc)->sentinel = sentinel;
|
|
2328 return sentinel;
|
|
2329 }
|
|
2330
|
20
|
2331 DEFUN ("process-sentinel", Fprocess_sentinel, 1, 1, 0, /*
|
0
|
2332 Return the sentinel of PROCESS; nil if none.
|
|
2333 See `set-process-sentinel' for more info on sentinels.
|
20
|
2334 */
|
|
2335 (proc))
|
0
|
2336 {
|
|
2337 CHECK_PROCESS (proc);
|
|
2338 return XPROCESS (proc)->sentinel;
|
|
2339 }
|
|
2340
|
|
2341
|
|
2342 CONST char *
|
|
2343 signal_name (int signum)
|
|
2344 {
|
|
2345 if (signum >= 0 && signum < NSIG)
|
173
|
2346 return (CONST char *) sys_siglist[signum];
|
209
|
2347
|
173
|
2348 return (CONST char *) GETTEXT ("unknown signal");
|
0
|
2349 }
|
|
2350
|
|
2351 /* Compute the Lisp form of the process status from
|
|
2352 the numeric status that was returned by `wait'. */
|
|
2353
|
|
2354 static void
|
155
|
2355 update_status_from_wait_code (struct Lisp_Process *p, int *w_fmh)
|
0
|
2356 {
|
|
2357 /* C compiler lossage when attempting to pass w directly */
|
155
|
2358 int w = *w_fmh;
|
0
|
2359
|
|
2360 if (WIFSTOPPED (w))
|
|
2361 {
|
|
2362 p->status_symbol = Qstop;
|
|
2363 p->exit_code = WSTOPSIG (w);
|
|
2364 p->core_dumped = 0;
|
|
2365 }
|
|
2366 else if (WIFEXITED (w))
|
|
2367 {
|
|
2368 p->status_symbol = Qexit;
|
155
|
2369 p->exit_code = WEXITSTATUS (w);
|
|
2370 p->core_dumped = 0;
|
0
|
2371 }
|
|
2372 else if (WIFSIGNALED (w))
|
|
2373 {
|
|
2374 p->status_symbol = Qsignal;
|
155
|
2375 p->exit_code = WTERMSIG (w);
|
|
2376 p->core_dumped = WCOREDUMP (w);
|
0
|
2377 }
|
|
2378 else
|
|
2379 {
|
|
2380 p->status_symbol = Qrun;
|
|
2381 p->exit_code = 0;
|
|
2382 }
|
|
2383 }
|
|
2384
|
|
2385 void
|
|
2386 update_process_status (Lisp_Object p,
|
|
2387 Lisp_Object status_symbol,
|
|
2388 int exit_code,
|
|
2389 int core_dumped)
|
|
2390 {
|
|
2391 XPROCESS (p)->tick++;
|
|
2392 process_tick++;
|
|
2393 XPROCESS (p)->status_symbol = status_symbol;
|
|
2394 XPROCESS (p)->exit_code = exit_code;
|
|
2395 XPROCESS (p)->core_dumped = core_dumped;
|
|
2396 }
|
|
2397
|
|
2398 #ifdef SIGCHLD
|
|
2399
|
|
2400 #define MAX_EXITED_PROCESSES 1000
|
|
2401 static volatile pid_t exited_processes[MAX_EXITED_PROCESSES];
|
155
|
2402 static volatile int exited_processes_status[MAX_EXITED_PROCESSES];
|
0
|
2403 static volatile int exited_processes_index;
|
|
2404
|
|
2405 static volatile int sigchld_happened;
|
|
2406
|
|
2407 /* For any processes that have changed status and are recorded
|
|
2408 and such, update the corresponding struct Lisp_Process.
|
|
2409 We separate this from record_exited_processes() so that
|
|
2410 we never have to call this function from within a signal
|
|
2411 handler. We block SIGCHLD in case record_exited_processes()
|
|
2412 is called from a signal handler. */
|
|
2413
|
|
2414 static void
|
|
2415 reap_exited_processes (void)
|
|
2416 {
|
|
2417 int i;
|
|
2418 struct Lisp_Process *p;
|
|
2419
|
114
|
2420 if (exited_processes_index <= 0)
|
|
2421 {
|
|
2422 return;
|
|
2423 }
|
|
2424
|
100
|
2425 #ifdef EMACS_BLOCK_SIGNAL
|
0
|
2426 EMACS_BLOCK_SIGNAL (SIGCHLD);
|
100
|
2427 #endif
|
0
|
2428 for (i = 0; i < exited_processes_index; i++)
|
|
2429 {
|
|
2430 int pid = exited_processes[i];
|
155
|
2431 int w = exited_processes_status[i];
|
0
|
2432
|
|
2433 /* Find the process that signaled us, and record its status. */
|
|
2434
|
|
2435 p = 0;
|
|
2436 {
|
|
2437 Lisp_Object tail;
|
|
2438 LIST_LOOP (tail, Vprocess_list)
|
|
2439 {
|
|
2440 Lisp_Object proc = XCAR (tail);
|
|
2441 p = XPROCESS (proc);
|
|
2442 if (INTP (p->pid) && XINT (p->pid) == pid)
|
|
2443 break;
|
|
2444 p = 0;
|
|
2445 }
|
|
2446 }
|
|
2447
|
|
2448 if (p)
|
|
2449 {
|
|
2450 /* Change the status of the process that was found. */
|
|
2451 p->tick++;
|
|
2452 process_tick++;
|
|
2453 update_status_from_wait_code (p, &w);
|
185
|
2454
|
0
|
2455 /* If process has terminated, stop waiting for its output. */
|
|
2456 if (WIFSIGNALED (w) || WIFEXITED (w))
|
|
2457 {
|
|
2458 if (p->infd >= 0)
|
|
2459 {
|
|
2460 /* We can't just call event_stream->unselect_process_cb (p)
|
|
2461 here, because that calls XtRemoveInput, which is not
|
|
2462 necessarily reentrant, so we can't call this at interrupt
|
|
2463 level.
|
|
2464 */
|
|
2465 }
|
|
2466 }
|
|
2467 }
|
|
2468 else
|
|
2469 {
|
|
2470 /* There was no asynchronous process found for that id. Check
|
|
2471 if we have a synchronous process. Only set sync process status
|
|
2472 if there is one, so we work OK with the waitpid() call in
|
|
2473 wait_for_termination(). */
|
|
2474 if (synch_process_alive != 0)
|
|
2475 { /* Set the global sync process status variables. */
|
|
2476 synch_process_alive = 0;
|
|
2477
|
|
2478 /* Report the status of the synchronous process. */
|
|
2479 if (WIFEXITED (w))
|
155
|
2480 synch_process_retcode = WEXITSTATUS (w);
|
0
|
2481 else if (WIFSIGNALED (w))
|
|
2482 synch_process_death = signal_name (WTERMSIG (w));
|
|
2483 }
|
|
2484 }
|
|
2485 }
|
|
2486
|
|
2487 exited_processes_index = 0;
|
185
|
2488
|
0
|
2489 EMACS_UNBLOCK_SIGNAL (SIGCHLD);
|
|
2490 }
|
|
2491
|
|
2492 /* On receipt of a signal that a child status has changed,
|
|
2493 loop asking about children with changed statuses until
|
|
2494 the system says there are no more. All we do is record
|
|
2495 the processes and wait status.
|
|
2496
|
|
2497 This function could be called from within the SIGCHLD
|
|
2498 handler, so it must be completely reentrant. When
|
|
2499 not called from a SIGCHLD handler, BLOCK_SIGCHLD should
|
|
2500 be non-zero so that SIGCHLD is blocked while this
|
|
2501 function is running. (This is necessary so avoid
|
|
2502 race conditions with the SIGCHLD_HAPPENED flag). */
|
|
2503
|
|
2504 static void
|
|
2505 record_exited_processes (int block_sigchld)
|
|
2506 {
|
114
|
2507 if (!sigchld_happened)
|
|
2508 {
|
|
2509 return;
|
|
2510 }
|
|
2511
|
102
|
2512 #ifdef EMACS_BLOCK_SIGNAL
|
0
|
2513 if (block_sigchld)
|
|
2514 EMACS_BLOCK_SIGNAL (SIGCHLD);
|
100
|
2515 #endif
|
0
|
2516
|
|
2517 while (sigchld_happened)
|
|
2518 {
|
|
2519 int pid;
|
155
|
2520 int w;
|
185
|
2521
|
0
|
2522 /* Keep trying to get a status until we get a definitive result. */
|
185
|
2523 do
|
0
|
2524 {
|
|
2525 errno = 0;
|
|
2526 #ifdef WNOHANG
|
|
2527 # ifndef WUNTRACED
|
|
2528 # define WUNTRACED 0
|
|
2529 # endif /* not WUNTRACED */
|
|
2530 # ifdef HAVE_WAITPID
|
|
2531 pid = waitpid ((pid_t) -1, &w, WNOHANG | WUNTRACED);
|
|
2532 # else
|
|
2533 pid = wait3 (&w, WNOHANG | WUNTRACED, 0);
|
|
2534 # endif
|
|
2535 #else /* not WNOHANG */
|
|
2536 pid = wait (&w);
|
|
2537 #endif /* not WNOHANG */
|
|
2538 }
|
|
2539 while (pid <= 0 && errno == EINTR);
|
185
|
2540
|
0
|
2541 if (pid <= 0)
|
|
2542 break;
|
185
|
2543
|
0
|
2544 if (exited_processes_index < MAX_EXITED_PROCESSES)
|
|
2545 {
|
|
2546 exited_processes[exited_processes_index] = pid;
|
|
2547 exited_processes_status[exited_processes_index] = w;
|
|
2548 exited_processes_index++;
|
|
2549 }
|
185
|
2550
|
0
|
2551 /* On systems with WNOHANG, we just ignore the number
|
|
2552 of times that SIGCHLD was signalled, and keep looping
|
|
2553 until there are no more processes to wait on. If we
|
|
2554 don't have WNOHANG, we have to rely on the count in
|
|
2555 SIGCHLD_HAPPENED. */
|
|
2556 #ifndef WNOHANG
|
|
2557 sigchld_happened--;
|
|
2558 #endif /* not WNOHANG */
|
|
2559 }
|
|
2560
|
|
2561 sigchld_happened = 0;
|
|
2562
|
|
2563 if (block_sigchld)
|
|
2564 EMACS_UNBLOCK_SIGNAL (SIGCHLD);
|
|
2565 }
|
|
2566
|
|
2567 /** USG WARNING: Although it is not obvious from the documentation
|
|
2568 in signal(2), on a USG system the SIGCLD handler MUST NOT call
|
|
2569 signal() before executing at least one wait(), otherwise the handler
|
|
2570 will be called again, resulting in an infinite loop. The relevant
|
|
2571 portion of the documentation reads "SIGCLD signals will be queued
|
|
2572 and the signal-catching function will be continually reentered until
|
|
2573 the queue is empty". Invoking signal() causes the kernel to reexamine
|
|
2574 the SIGCLD queue. Fred Fish, UniSoft Systems Inc.
|
185
|
2575
|
0
|
2576 (Note that now this only applies in SYS V Release 2 and before.
|
|
2577 On SYS V Release 3, we use sigset() to set the signal handler for
|
|
2578 the first time, and so we don't have to reestablish the signal handler
|
|
2579 in the handler below. On SYS V Release 4, we don't get this weirdo
|
|
2580 behavior when we use sigaction(), which we do use.) */
|
|
2581
|
|
2582 static SIGTYPE
|
|
2583 sigchld_handler (int signo)
|
|
2584 {
|
|
2585 #ifdef OBNOXIOUS_SYSV_SIGCLD_BEHAVIOR
|
|
2586 int old_errno = errno;
|
|
2587
|
|
2588 sigchld_happened++;
|
|
2589 record_exited_processes (0);
|
|
2590 errno = old_errno;
|
|
2591 #else
|
|
2592 sigchld_happened++;
|
|
2593 #endif
|
223
|
2594 #ifdef HAVE_UNIXOID_EVENT_LOOP
|
0
|
2595 signal_fake_event ();
|
223
|
2596 #endif
|
0
|
2597 /* WARNING - must come after wait3() for USG systems */
|
|
2598 EMACS_REESTABLISH_SIGNAL (signo, sigchld_handler);
|
|
2599 SIGRETURN;
|
|
2600 }
|
|
2601
|
|
2602 #endif /* SIGCHLD */
|
|
2603
|
|
2604 /* Return a string describing a process status list. */
|
|
2605
|
185
|
2606 static Lisp_Object
|
0
|
2607 status_message (struct Lisp_Process *p)
|
|
2608 {
|
|
2609 Lisp_Object symbol = p->status_symbol;
|
|
2610 int code = p->exit_code;
|
|
2611 int coredump = p->core_dumped;
|
|
2612 Lisp_Object string, string2;
|
|
2613
|
|
2614 if (EQ (symbol, Qsignal) || EQ (symbol, Qstop))
|
|
2615 {
|
|
2616 string = build_string (signal_name (code));
|
|
2617 if (coredump)
|
|
2618 string2 = build_translated_string (" (core dumped)\n");
|
|
2619 else
|
|
2620 string2 = build_string ("\n");
|
|
2621 set_string_char (XSTRING (string), 0,
|
|
2622 DOWNCASE (current_buffer,
|
|
2623 string_char (XSTRING (string), 0)));
|
|
2624 return concat2 (string, string2);
|
|
2625 }
|
|
2626 else if (EQ (symbol, Qexit))
|
|
2627 {
|
|
2628 if (code == 0)
|
|
2629 return build_translated_string ("finished\n");
|
|
2630 string = Fnumber_to_string (make_int (code));
|
|
2631 if (coredump)
|
|
2632 string2 = build_translated_string (" (core dumped)\n");
|
|
2633 else
|
|
2634 string2 = build_string ("\n");
|
|
2635 return concat2 (build_translated_string ("exited abnormally with code "),
|
|
2636 concat2 (string, string2));
|
|
2637 }
|
|
2638 else
|
|
2639 return Fcopy_sequence (Fsymbol_name (symbol));
|
|
2640 }
|
|
2641
|
|
2642 /* Tell status_notify() to check for terminated processes. We do this
|
|
2643 because on some systems we sometimes miss SIGCHLD calls. (Not sure
|
|
2644 why.) */
|
|
2645
|
|
2646 void
|
|
2647 kick_status_notify (void)
|
|
2648 {
|
|
2649 process_tick++;
|
|
2650 }
|
|
2651
|
|
2652 /* Report all recent events of a change in process status
|
|
2653 (either run the sentinel or output a message).
|
|
2654 This is done while Emacs is waiting for keyboard input. */
|
|
2655
|
|
2656 void
|
|
2657 status_notify (void)
|
|
2658 {
|
|
2659 /* This function can GC */
|
|
2660 Lisp_Object tail = Qnil;
|
|
2661 Lisp_Object symbol = Qnil;
|
|
2662 Lisp_Object msg = Qnil;
|
|
2663 struct gcpro gcpro1, gcpro2, gcpro3;
|
|
2664 /* process_tick is volatile, so we have to remember it now.
|
|
2665 Otherwise, we get a race condition is SIGCHLD happens during
|
|
2666 this function.
|
|
2667
|
|
2668 (Actually, this is not the case anymore. The code to
|
|
2669 update the process structures has been moved out of the
|
|
2670 SIGCHLD handler. But for the moment I'm leaving this
|
|
2671 stuff in -- it can't hurt.) */
|
|
2672 int temp_process_tick;
|
|
2673
|
|
2674 #ifdef SIGCHLD
|
|
2675 #ifndef OBNOXIOUS_SYSV_SIGCLD_BEHAVIOR
|
|
2676 record_exited_processes (1);
|
|
2677 #endif
|
|
2678 reap_exited_processes ();
|
|
2679 #endif
|
|
2680
|
|
2681 temp_process_tick = process_tick;
|
|
2682
|
|
2683 if (update_tick == temp_process_tick)
|
|
2684 return;
|
|
2685
|
|
2686 /* We need to gcpro tail; if read_process_output calls a filter
|
|
2687 which deletes a process and removes the cons to which tail points
|
|
2688 from Vprocess_alist, and then causes a GC, tail is an unprotected
|
|
2689 reference. */
|
|
2690 GCPRO3 (tail, symbol, msg);
|
|
2691
|
|
2692 for (tail = Vprocess_list; CONSP (tail); tail = XCDR (tail))
|
|
2693 {
|
|
2694 Lisp_Object proc = XCAR (tail);
|
|
2695 struct Lisp_Process *p = XPROCESS (proc);
|
|
2696 /* p->tick is also volatile. Same thing as above applies. */
|
|
2697 int this_process_tick;
|
|
2698
|
|
2699 #ifdef HAVE_WAITPID
|
|
2700 /* #### extra check for terminated processes, in case a SIGCHLD
|
|
2701 got missed (this seems to happen sometimes, I'm not sure why).
|
|
2702 */
|
|
2703 {
|
155
|
2704 int w;
|
0
|
2705 #ifdef SIGCHLD
|
|
2706 EMACS_BLOCK_SIGNAL (SIGCHLD);
|
|
2707 #endif
|
|
2708 if (INTP (p->pid) &&
|
|
2709 waitpid (XINT (p->pid), &w, WNOHANG) == XINT (p->pid))
|
|
2710 {
|
|
2711 p->tick++;
|
|
2712 update_status_from_wait_code (p, &w);
|
|
2713 }
|
|
2714 #ifdef SIGCHLD
|
|
2715 EMACS_UNBLOCK_SIGNAL (SIGCHLD);
|
|
2716 #endif
|
|
2717 }
|
|
2718 #endif
|
|
2719 this_process_tick = p->tick;
|
|
2720 if (this_process_tick != p->update_tick)
|
|
2721 {
|
|
2722 p->update_tick = this_process_tick;
|
|
2723
|
|
2724 /* If process is still active, read any output that remains. */
|
|
2725 while (!EQ (p->filter, Qt)
|
|
2726 && read_process_output (proc) > 0)
|
|
2727 ;
|
|
2728
|
|
2729 /* Get the text to use for the message. */
|
|
2730 msg = status_message (p);
|
|
2731
|
|
2732 /* If process is terminated, deactivate it or delete it. */
|
|
2733 symbol = p->status_symbol;
|
|
2734
|
185
|
2735 if (EQ (symbol, Qsignal)
|
0
|
2736 || EQ (symbol, Qexit))
|
|
2737 {
|
|
2738 if (delete_exited_processes)
|
|
2739 remove_process (proc);
|
|
2740 else
|
|
2741 deactivate_process (proc);
|
|
2742 }
|
|
2743
|
|
2744 /* Now output the message suitably. */
|
|
2745 if (!NILP (p->sentinel))
|
|
2746 exec_sentinel (proc, msg);
|
|
2747 /* Don't bother with a message in the buffer
|
|
2748 when a process becomes runnable. */
|
|
2749 else if (!EQ (symbol, Qrun) && !NILP (p->buffer))
|
|
2750 {
|
161
|
2751 Lisp_Object old_read_only = Qnil;
|
|
2752 Lisp_Object old = Fcurrent_buffer ();
|
|
2753 Bufpos opoint;
|
|
2754 struct gcpro ngcpro1, ngcpro2;
|
0
|
2755
|
|
2756 /* Avoid error if buffer is deleted
|
|
2757 (probably that's why the process is dead, too) */
|
|
2758 if (!BUFFER_LIVE_P (XBUFFER (p->buffer)))
|
|
2759 continue;
|
|
2760
|
161
|
2761 NGCPRO2 (old, old_read_only);
|
0
|
2762 Fset_buffer (p->buffer);
|
161
|
2763 opoint = BUF_PT (current_buffer);
|
0
|
2764 /* Insert new output into buffer
|
|
2765 at the current end-of-output marker,
|
|
2766 thus preserving logical ordering of input and output. */
|
|
2767 if (XMARKER (p->mark)->buffer)
|
|
2768 BUF_SET_PT (current_buffer, marker_position (p->mark));
|
|
2769 else
|
|
2770 BUF_SET_PT (current_buffer, BUF_ZV (current_buffer));
|
161
|
2771 if (BUF_PT (current_buffer) <= opoint)
|
|
2772 opoint += (string_char_length (XSTRING (msg))
|
|
2773 + string_char_length (XSTRING (p->name))
|
|
2774 + 10);
|
|
2775
|
|
2776 old_read_only = current_buffer->read_only;
|
|
2777 current_buffer->read_only = Qnil;
|
0
|
2778 buffer_insert_c_string (current_buffer, "\nProcess ");
|
|
2779 Finsert (1, &p->name);
|
|
2780 buffer_insert_c_string (current_buffer, " ");
|
|
2781 Finsert (1, &msg);
|
161
|
2782 current_buffer->read_only = old_read_only;
|
0
|
2783 Fset_marker (p->mark, make_int (BUF_PT (current_buffer)),
|
|
2784 p->buffer);
|
161
|
2785
|
|
2786 opoint = bufpos_clip_to_bounds(BUF_BEGV (XBUFFER (p->buffer)),
|
|
2787 opoint,
|
|
2788 BUF_ZV (XBUFFER (p->buffer)));
|
|
2789 BUF_SET_PT (current_buffer, opoint);
|
|
2790 Fset_buffer (old);
|
|
2791 NUNGCPRO;
|
0
|
2792 }
|
|
2793 }
|
|
2794 } /* end for */
|
|
2795
|
|
2796 /* in case buffers use %s in modeline-format */
|
|
2797 MARK_MODELINE_CHANGED;
|
|
2798 redisplay ();
|
|
2799
|
|
2800 update_tick = temp_process_tick;
|
|
2801
|
|
2802 UNGCPRO;
|
|
2803 }
|
|
2804
|
20
|
2805 DEFUN ("process-status", Fprocess_status, 1, 1, 0, /*
|
0
|
2806 Return the status of PROCESS.
|
|
2807 This is a symbol, one of these:
|
|
2808
|
175
|
2809 run -- for a process that is running.
|
|
2810 stop -- for a process stopped but continuable.
|
|
2811 exit -- for a process that has exited.
|
0
|
2812 signal -- for a process that has got a fatal signal.
|
175
|
2813 open -- for a network stream connection that is open.
|
0
|
2814 closed -- for a network stream connection that is closed.
|
175
|
2815 nil -- if arg is a process name and no such process exists.
|
|
2816
|
0
|
2817 PROCESS may be a process, a buffer, the name of a process or buffer, or
|
|
2818 nil, indicating the current buffer's process.
|
20
|
2819 */
|
|
2820 (proc))
|
0
|
2821 {
|
175
|
2822 Lisp_Object status_symbol;
|
0
|
2823
|
|
2824 if (STRINGP (proc))
|
|
2825 proc = Fget_process (proc);
|
|
2826 else
|
|
2827 proc = get_process (proc);
|
|
2828
|
|
2829 if (NILP (proc))
|
175
|
2830 return Qnil;
|
|
2831
|
|
2832 status_symbol = XPROCESS (proc)->status_symbol;
|
0
|
2833 if (network_connection_p (proc))
|
|
2834 {
|
175
|
2835 if (EQ (status_symbol, Qrun))
|
|
2836 status_symbol = Qopen;
|
|
2837 else if (EQ (status_symbol, Qexit))
|
|
2838 status_symbol = Qclosed;
|
0
|
2839 }
|
175
|
2840 return status_symbol;
|
0
|
2841 }
|
|
2842
|
20
|
2843 DEFUN ("process-exit-status", Fprocess_exit_status, 1, 1, 0, /*
|
0
|
2844 Return the exit status of PROCESS or the signal number that killed it.
|
|
2845 If PROCESS has not yet exited or died, return 0.
|
20
|
2846 */
|
|
2847 (proc))
|
0
|
2848 {
|
|
2849 CHECK_PROCESS (proc);
|
173
|
2850 return make_int (XPROCESS (proc)->exit_code);
|
0
|
2851 }
|
|
2852
|
|
2853
|
|
2854 #ifdef SIGNALS_VIA_CHARACTERS
|
|
2855 /* Get signal character to send to process if SIGNALS_VIA_CHARACTERS */
|
|
2856
|
|
2857 static int
|
|
2858 process_signal_char (int tty_fd, int signo)
|
|
2859 {
|
|
2860 /* If it's not a tty, pray that these default values work */
|
|
2861 if (!isatty(tty_fd)) {
|
|
2862 #define CNTL(ch) (037 & (ch))
|
|
2863 switch (signo)
|
|
2864 {
|
|
2865 case SIGINT: return CNTL('C');
|
|
2866 case SIGQUIT: return CNTL('\\');
|
|
2867 #ifdef SIGTSTP
|
|
2868 case SIGTSTP: return CNTL('Z');
|
|
2869 #endif
|
|
2870 }
|
|
2871 }
|
|
2872
|
|
2873 #ifdef HAVE_TERMIOS
|
|
2874 /* TERMIOS is the latest and bestest, and seems most likely to work.
|
|
2875 If the system has it, use it. */
|
|
2876 {
|
|
2877 struct termios t;
|
|
2878 tcgetattr (tty_fd, &t);
|
|
2879 switch (signo)
|
|
2880 {
|
|
2881 case SIGINT: return t.c_cc[VINTR];
|
|
2882 case SIGQUIT: return t.c_cc[VQUIT];
|
|
2883 # if defined (VSWTCH) && !defined (PREFER_VSUSP)
|
|
2884 case SIGTSTP: return t.c_cc[VSWTCH];
|
|
2885 # else
|
|
2886 case SIGTSTP: return t.c_cc[VSUSP];
|
|
2887 # endif
|
|
2888 }
|
|
2889 }
|
|
2890
|
|
2891 # elif defined (TIOCGLTC) && defined (TIOCGETC) /* not HAVE_TERMIOS */
|
|
2892 {
|
|
2893 /* On Berkeley descendants, the following IOCTL's retrieve the
|
|
2894 current control characters. */
|
|
2895 struct tchars c;
|
|
2896 struct ltchars lc;
|
|
2897 switch (signo)
|
|
2898 {
|
|
2899 case SIGINT: ioctl (tty_fd, TIOCGETC, &c); return c.t_intrc;
|
|
2900 case SIGQUIT: ioctl (tty_fd, TIOCGETC, &c); return c.t_quitc;
|
|
2901 # ifdef SIGTSTP
|
|
2902 case SIGTSTP: ioctl (tty_fd, TIOCGLTC, &lc); return lc.t_suspc;
|
|
2903 # endif /* SIGTSTP */
|
|
2904 }
|
|
2905 }
|
|
2906
|
|
2907 # elif defined (TCGETA) /* ! defined (TIOCGLTC) && defined (TIOCGETC) */
|
|
2908 {
|
|
2909 /* On SYSV descendants, the TCGETA ioctl retrieves the current
|
|
2910 control characters. */
|
|
2911 struct termio t;
|
|
2912 ioctl (tty_fd, TCGETA, &t);
|
|
2913 switch (signo) {
|
|
2914 case SIGINT: return t.c_cc[VINTR];
|
|
2915 case SIGQUIT: return t.c_cc[VQUIT];
|
|
2916 # ifdef SIGTSTP
|
|
2917 case SIGTSTP: return t.c_cc[VSWTCH];
|
|
2918 # endif /* SIGTSTP */
|
|
2919 }
|
|
2920 }
|
|
2921 # else /* ! defined (TCGETA) */
|
175
|
2922 #error ERROR! Using SIGNALS_VIA_CHARACTERS, but not HAVE_TERMIOS || (TIOCGLTC && TIOCGETC) || TCGETA
|
0
|
2923 /* If your system configuration files define SIGNALS_VIA_CHARACTERS,
|
|
2924 you'd better be using one of the alternatives above! */
|
|
2925 # endif /* ! defined (TCGETA) */
|
|
2926 return '\0';
|
|
2927 }
|
|
2928 #endif /* SIGNALS_VIA_CHARACTERS */
|
|
2929
|
|
2930
|
|
2931 /* send a signal number SIGNO to PROCESS.
|
|
2932 CURRENT_GROUP means send to the process group that currently owns
|
|
2933 the terminal being used to communicate with PROCESS.
|
|
2934 This is used for various commands in shell mode.
|
|
2935 If NOMSG is zero, insert signal-announcements into process's buffers
|
|
2936 right away.
|
|
2937
|
|
2938 If we can, we try to signal PROCESS by sending control characters
|
|
2939 down the pty. This allows us to signal inferiors who have changed
|
|
2940 their uid, for which killpg would return an EPERM error. */
|
|
2941
|
|
2942 static void
|
|
2943 process_send_signal (Lisp_Object process0, int signo,
|
|
2944 int current_group, int nomsg)
|
|
2945 {
|
|
2946 /* This function can GC */
|
|
2947 Lisp_Object proc = get_process (process0);
|
|
2948 struct Lisp_Process *p = XPROCESS (proc);
|
|
2949 int gid;
|
|
2950 int no_pgrp = 0;
|
|
2951
|
|
2952 if (network_connection_p (proc))
|
|
2953 error ("Network connection %s is not a subprocess",
|
70
|
2954 XSTRING_DATA (p->name));
|
0
|
2955 if (p->infd < 0)
|
|
2956 error ("Process %s is not active",
|
70
|
2957 XSTRING_DATA (p->name));
|
0
|
2958
|
|
2959 if (!p->pty_flag)
|
|
2960 current_group = 0;
|
|
2961
|
|
2962 /* If we are using pgrps, get a pgrp number and make it negative. */
|
|
2963 if (current_group)
|
|
2964 {
|
|
2965 #ifdef SIGNALS_VIA_CHARACTERS
|
|
2966 /* If possible, send signals to the entire pgrp
|
|
2967 by sending an input character to it. */
|
|
2968 {
|
|
2969 char sigchar = process_signal_char(p->subtty, signo);
|
|
2970 if (sigchar) {
|
|
2971 send_process (proc, Qnil, (Bufbyte *) &sigchar, 0, 1);
|
|
2972 return;
|
|
2973 }
|
|
2974 }
|
|
2975 #endif /* ! defined (SIGNALS_VIA_CHARACTERS) */
|
|
2976
|
185
|
2977 #ifdef TIOCGPGRP
|
0
|
2978 /* Get the pgrp using the tty itself, if we have that.
|
|
2979 Otherwise, use the pty to get the pgrp.
|
|
2980 On pfa systems, saka@pfu.fujitsu.co.JP writes:
|
|
2981 "TIOCGPGRP symbol defined in sys/ioctl.h at E50.
|
|
2982 But, TIOCGPGRP does not work on E50 ;-P works fine on E60"
|
|
2983 His patch indicates that if TIOCGPGRP returns an error, then
|
|
2984 we should just assume that p->pid is also the process group id. */
|
|
2985 {
|
|
2986 int err;
|
|
2987
|
|
2988 err = ioctl ( (p->subtty != -1 ? p->subtty : p->infd), TIOCGPGRP, &gid);
|
|
2989
|
|
2990 #ifdef pfa
|
|
2991 if (err == -1)
|
|
2992 gid = - XINT (p->pid);
|
|
2993 #endif /* ! defined (pfa) */
|
|
2994 }
|
|
2995 if (gid == -1)
|
|
2996 no_pgrp = 1;
|
|
2997 else
|
|
2998 gid = - gid;
|
|
2999 #else /* ! defined (TIOCGPGRP ) */
|
|
3000 /* Can't select pgrps on this system, so we know that
|
|
3001 the child itself heads the pgrp. */
|
|
3002 gid = - XINT (p->pid);
|
|
3003 #endif /* ! defined (TIOCGPGRP ) */
|
|
3004 }
|
|
3005 else
|
|
3006 gid = - XINT (p->pid);
|
|
3007
|
|
3008 switch (signo)
|
|
3009 {
|
|
3010 #ifdef SIGCONT
|
|
3011 case SIGCONT:
|
|
3012 p->status_symbol = Qrun;
|
|
3013 p->exit_code = 0;
|
|
3014 p->tick++;
|
|
3015 process_tick++;
|
|
3016 if (!nomsg)
|
|
3017 status_notify ();
|
|
3018 break;
|
|
3019 #endif /* ! defined (SIGCONT) */
|
|
3020 case SIGINT:
|
|
3021 case SIGQUIT:
|
|
3022 case SIGKILL:
|
|
3023 flush_pending_output (p->infd);
|
|
3024 break;
|
|
3025 }
|
|
3026
|
|
3027 /* If we don't have process groups, send the signal to the immediate
|
|
3028 subprocess. That isn't really right, but it's better than any
|
|
3029 obvious alternative. */
|
|
3030 if (no_pgrp)
|
|
3031 {
|
|
3032 kill (XINT (p->pid), signo);
|
|
3033 return;
|
|
3034 }
|
|
3035
|
|
3036 /* gid may be a pid, or minus a pgrp's number */
|
|
3037 #ifdef TIOCSIGSEND
|
|
3038 if (current_group)
|
|
3039 ioctl (p->infd, TIOCSIGSEND, signo);
|
|
3040 else
|
|
3041 {
|
|
3042 gid = - XINT (p->pid);
|
|
3043 kill (gid, signo);
|
|
3044 }
|
|
3045 #else /* ! defined (TIOCSIGSEND) */
|
|
3046 EMACS_KILLPG (-gid, signo);
|
|
3047 #endif /* ! defined (TIOCSIGSEND) */
|
|
3048 }
|
|
3049
|
20
|
3050 DEFUN ("interrupt-process", Finterrupt_process, 0, 2, 0, /*
|
0
|
3051 Interrupt process PROCESS. May be process or name of one.
|
|
3052 Nil or no arg means current buffer's process.
|
|
3053 Second arg CURRENT-GROUP non-nil means send signal to
|
|
3054 the current process-group of the process's controlling terminal
|
|
3055 rather than to the process's own process group.
|
|
3056 If the process is a shell, this means interrupt current subjob
|
|
3057 rather than the shell.
|
20
|
3058 */
|
|
3059 (process, current_group))
|
0
|
3060 {
|
|
3061 /* This function can GC */
|
|
3062 process_send_signal (process, SIGINT, !NILP (current_group), 0);
|
|
3063 return process;
|
|
3064 }
|
|
3065
|
20
|
3066 DEFUN ("kill-process", Fkill_process, 0, 2, 0, /*
|
0
|
3067 Kill process PROCESS. May be process or name of one.
|
|
3068 See function `interrupt-process' for more details on usage.
|
20
|
3069 */
|
|
3070 (process, current_group))
|
0
|
3071 {
|
|
3072 /* This function can GC */
|
|
3073 process_send_signal (process, SIGKILL, !NILP (current_group),
|
|
3074 0);
|
|
3075 return process;
|
|
3076 }
|
|
3077
|
20
|
3078 DEFUN ("quit-process", Fquit_process, 0, 2, 0, /*
|
0
|
3079 Send QUIT signal to process PROCESS. May be process or name of one.
|
|
3080 See function `interrupt-process' for more details on usage.
|
20
|
3081 */
|
|
3082 (process, current_group))
|
0
|
3083 {
|
|
3084 /* This function can GC */
|
|
3085 process_send_signal (process, SIGQUIT, !NILP (current_group),
|
|
3086 0);
|
|
3087 return process;
|
|
3088 }
|
|
3089
|
20
|
3090 DEFUN ("stop-process", Fstop_process, 0, 2, 0, /*
|
0
|
3091 Stop process PROCESS. May be process or name of one.
|
|
3092 See function `interrupt-process' for more details on usage.
|
20
|
3093 */
|
|
3094 (process, current_group))
|
0
|
3095 {
|
|
3096 /* This function can GC */
|
|
3097 #ifndef SIGTSTP
|
|
3098 error ("no SIGTSTP support");
|
|
3099 #else
|
|
3100 process_send_signal (process, SIGTSTP, !NILP (current_group),
|
|
3101 0);
|
|
3102 #endif
|
|
3103 return process;
|
|
3104 }
|
|
3105
|
20
|
3106 DEFUN ("continue-process", Fcontinue_process, 0, 2, 0, /*
|
0
|
3107 Continue process PROCESS. May be process or name of one.
|
|
3108 See function `interrupt-process' for more details on usage.
|
20
|
3109 */
|
|
3110 (process, current_group))
|
0
|
3111 {
|
|
3112 /* This function can GC */
|
|
3113 #ifdef SIGCONT
|
|
3114 process_send_signal (process, SIGCONT, !NILP (current_group),
|
|
3115 0);
|
|
3116 #else
|
|
3117 error ("no SIGCONT support");
|
|
3118 #endif
|
|
3119 return process;
|
|
3120 }
|
|
3121
|
20
|
3122 DEFUN ("signal-process", Fsignal_process, 2, 2,
|
|
3123 "nProcess number: \nnSignal code: ", /*
|
0
|
3124 Send the process with process id PID the signal with code SIGCODE.
|
|
3125 PID must be an integer. The process need not be a child of this Emacs.
|
|
3126 SIGCODE may be an integer, or a symbol whose name is a signal name.
|
20
|
3127 */
|
|
3128 (pid, sigcode))
|
0
|
3129 {
|
|
3130 CHECK_INT (pid);
|
|
3131
|
|
3132 #define handle_signal(NAME, VALUE) \
|
|
3133 else if (!strcmp ((CONST char *) name, NAME)) \
|
|
3134 XSETINT (sigcode, VALUE)
|
|
3135
|
|
3136 if (INTP (sigcode))
|
|
3137 ;
|
|
3138 else
|
|
3139 {
|
|
3140 Bufbyte *name;
|
|
3141
|
|
3142 CHECK_SYMBOL (sigcode);
|
|
3143 name = string_data (XSYMBOL (sigcode)->name);
|
|
3144
|
|
3145 if (0)
|
|
3146 ;
|
|
3147 #ifdef SIGHUP
|
|
3148 handle_signal ("SIGHUP", SIGHUP);
|
|
3149 #endif
|
|
3150 #ifdef SIGINT
|
|
3151 handle_signal ("SIGINT", SIGINT);
|
|
3152 #endif
|
|
3153 #ifdef SIGQUIT
|
|
3154 handle_signal ("SIGQUIT", SIGQUIT);
|
|
3155 #endif
|
|
3156 #ifdef SIGILL
|
|
3157 handle_signal ("SIGILL", SIGILL);
|
|
3158 #endif
|
|
3159 #ifdef SIGABRT
|
|
3160 handle_signal ("SIGABRT", SIGABRT);
|
|
3161 #endif
|
|
3162 #ifdef SIGEMT
|
|
3163 handle_signal ("SIGEMT", SIGEMT);
|
|
3164 #endif
|
|
3165 #ifdef SIGKILL
|
|
3166 handle_signal ("SIGKILL", SIGKILL);
|
|
3167 #endif
|
|
3168 #ifdef SIGFPE
|
|
3169 handle_signal ("SIGFPE", SIGFPE);
|
|
3170 #endif
|
|
3171 #ifdef SIGBUS
|
|
3172 handle_signal ("SIGBUS", SIGBUS);
|
|
3173 #endif
|
|
3174 #ifdef SIGSEGV
|
|
3175 handle_signal ("SIGSEGV", SIGSEGV);
|
|
3176 #endif
|
|
3177 #ifdef SIGSYS
|
|
3178 handle_signal ("SIGSYS", SIGSYS);
|
|
3179 #endif
|
|
3180 #ifdef SIGPIPE
|
|
3181 handle_signal ("SIGPIPE", SIGPIPE);
|
|
3182 #endif
|
|
3183 #ifdef SIGALRM
|
|
3184 handle_signal ("SIGALRM", SIGALRM);
|
|
3185 #endif
|
|
3186 #ifdef SIGTERM
|
|
3187 handle_signal ("SIGTERM", SIGTERM);
|
|
3188 #endif
|
|
3189 #ifdef SIGURG
|
|
3190 handle_signal ("SIGURG", SIGURG);
|
|
3191 #endif
|
|
3192 #ifdef SIGSTOP
|
|
3193 handle_signal ("SIGSTOP", SIGSTOP);
|
|
3194 #endif
|
|
3195 #ifdef SIGTSTP
|
|
3196 handle_signal ("SIGTSTP", SIGTSTP);
|
|
3197 #endif
|
|
3198 #ifdef SIGCONT
|
|
3199 handle_signal ("SIGCONT", SIGCONT);
|
|
3200 #endif
|
|
3201 #ifdef SIGCHLD
|
|
3202 handle_signal ("SIGCHLD", SIGCHLD);
|
|
3203 #endif
|
|
3204 #ifdef SIGTTIN
|
|
3205 handle_signal ("SIGTTIN", SIGTTIN);
|
|
3206 #endif
|
|
3207 #ifdef SIGTTOU
|
|
3208 handle_signal ("SIGTTOU", SIGTTOU);
|
|
3209 #endif
|
|
3210 #ifdef SIGIO
|
|
3211 handle_signal ("SIGIO", SIGIO);
|
|
3212 #endif
|
|
3213 #ifdef SIGXCPU
|
|
3214 handle_signal ("SIGXCPU", SIGXCPU);
|
|
3215 #endif
|
|
3216 #ifdef SIGXFSZ
|
|
3217 handle_signal ("SIGXFSZ", SIGXFSZ);
|
|
3218 #endif
|
|
3219 #ifdef SIGVTALRM
|
|
3220 handle_signal ("SIGVTALRM", SIGVTALRM);
|
|
3221 #endif
|
|
3222 #ifdef SIGPROF
|
|
3223 handle_signal ("SIGPROF", SIGPROF);
|
|
3224 #endif
|
|
3225 #ifdef SIGWINCH
|
|
3226 handle_signal ("SIGWINCH", SIGWINCH);
|
|
3227 #endif
|
|
3228 #ifdef SIGINFO
|
|
3229 handle_signal ("SIGINFO", SIGINFO);
|
|
3230 #endif
|
|
3231 #ifdef SIGUSR1
|
|
3232 handle_signal ("SIGUSR1", SIGUSR1);
|
|
3233 #endif
|
|
3234 #ifdef SIGUSR2
|
|
3235 handle_signal ("SIGUSR2", SIGUSR2);
|
|
3236 #endif
|
|
3237 else
|
|
3238 error ("Undefined signal name %s", name);
|
|
3239 }
|
|
3240
|
|
3241 #undef handle_signal
|
|
3242
|
|
3243 return make_int (kill (XINT (pid), XINT (sigcode)));
|
|
3244 }
|
|
3245
|
20
|
3246 DEFUN ("process-send-eof", Fprocess_send_eof, 0, 1, 0, /*
|
0
|
3247 Make PROCESS see end-of-file in its input.
|
|
3248 PROCESS may be a process, a buffer, the name of a process or buffer, or
|
|
3249 nil, indicating the current buffer's process.
|
|
3250 If PROCESS is a network connection, or is a process communicating
|
|
3251 through a pipe (as opposed to a pty), then you cannot send any more
|
|
3252 text to PROCESS after you call this function.
|
20
|
3253 */
|
|
3254 (process))
|
0
|
3255 {
|
|
3256 /* This function can GC */
|
|
3257 Lisp_Object proc;
|
|
3258
|
|
3259 proc = get_process (process);
|
|
3260
|
|
3261 /* Make sure the process is really alive. */
|
|
3262 if (! EQ (XPROCESS (proc)->status_symbol, Qrun))
|
16
|
3263 error ("Process %s not running", XSTRING_DATA (XPROCESS (proc)->name));
|
0
|
3264
|
|
3265 if (XPROCESS (proc)->pty_flag)
|
|
3266 {
|
|
3267 /* #### get_eof_char simply doesn't return the correct character
|
|
3268 here. Maybe it is needed to determine the right eof
|
|
3269 character in init_process_fds but here it simply screws
|
|
3270 things up. */
|
|
3271 #if 0
|
|
3272 Bufbyte eof_char = get_eof_char (XPROCESS (proc));
|
|
3273 send_process (proc, Qnil, &eof_char, 0, 1);
|
|
3274 #else
|
|
3275 send_process (proc, Qnil, (CONST Bufbyte *) "\004", 0, 1);
|
|
3276 #endif
|
|
3277 }
|
|
3278 else
|
|
3279 {
|
|
3280 close (XPROCESS (proc)->outfd);
|
251
|
3281 XPROCESS (proc)->outfd = open (NULL_DEVICE, O_WRONLY | OPEN_BINARY, 0);
|
0
|
3282 }
|
209
|
3283
|
0
|
3284 return process;
|
|
3285 }
|
|
3286
|
|
3287
|
|
3288 /************************************************************************/
|
|
3289 /* deleting a process */
|
|
3290 /************************************************************************/
|
|
3291
|
|
3292 void
|
|
3293 deactivate_process (Lisp_Object proc)
|
|
3294 {
|
|
3295 int inchannel, outchannel;
|
|
3296 struct Lisp_Process *p = XPROCESS (proc);
|
|
3297 SIGTYPE (*old_sigpipe) (int) = 0;
|
|
3298
|
|
3299 inchannel = p->infd;
|
|
3300 outchannel = p->outfd;
|
|
3301
|
|
3302 /* closing the outstream could result in SIGPIPE, so ignore it. */
|
|
3303 old_sigpipe =
|
|
3304 (SIGTYPE (*) (int)) signal (SIGPIPE, SIG_IGN);
|
|
3305 if (!NILP (p->instream))
|
|
3306 Lstream_close (XLSTREAM (p->instream));
|
|
3307 if (!NILP (p->outstream))
|
|
3308 Lstream_close (XLSTREAM (p->outstream));
|
|
3309 signal (SIGPIPE, old_sigpipe);
|
|
3310
|
|
3311 if (inchannel >= 0)
|
|
3312 {
|
|
3313 /* Beware SIGCHLD hereabouts. */
|
|
3314 flush_pending_output (inchannel);
|
|
3315 close_descriptor_pair (inchannel, outchannel);
|
|
3316 if (!NILP (p->pid))
|
|
3317 {
|
|
3318 /* It's possible that we got as far in the process-creation
|
|
3319 process as creating the descriptors but didn't get so
|
|
3320 far as selecting the process for input. In this
|
|
3321 case, p->pid is nil: p->pid is set at the same time that
|
|
3322 the process is selected for input. */
|
|
3323 /* Must call this before setting the file descriptors to 0 */
|
|
3324 event_stream_unselect_process (p);
|
|
3325 }
|
|
3326
|
|
3327 p->infd = -1;
|
|
3328 p->outfd = -1;
|
|
3329 descriptor_to_process[inchannel] = Qnil;
|
|
3330 }
|
|
3331 }
|
|
3332
|
|
3333 static void
|
|
3334 remove_process (Lisp_Object proc)
|
|
3335 {
|
|
3336 Vprocess_list = delq_no_quit (proc, Vprocess_list);
|
|
3337 Fset_marker (XPROCESS (proc)->mark, Qnil, Qnil);
|
|
3338
|
|
3339 deactivate_process (proc);
|
|
3340 }
|
|
3341
|
20
|
3342 DEFUN ("delete-process", Fdelete_process, 1, 1, 0, /*
|
0
|
3343 Delete PROCESS: kill it and forget about it immediately.
|
|
3344 PROCESS may be a process or the name of one, or a buffer name.
|
20
|
3345 */
|
|
3346 (proc))
|
0
|
3347 {
|
|
3348 /* This function can GC */
|
|
3349 struct Lisp_Process *p;
|
|
3350 proc = get_process (proc);
|
|
3351 p = XPROCESS (proc);
|
|
3352 if (network_connection_p (proc))
|
|
3353 {
|
|
3354 p->status_symbol = Qexit;
|
|
3355 p->exit_code = 0;
|
|
3356 p->core_dumped = 0;
|
|
3357 p->tick++;
|
|
3358 process_tick++;
|
|
3359 }
|
|
3360 else if (p->infd >= 0)
|
|
3361 {
|
|
3362 Fkill_process (proc, Qnil);
|
|
3363 /* Do this now, since remove_process will make sigchld_handler do nothing. */
|
|
3364 p->status_symbol = Qsignal;
|
|
3365 p->exit_code = SIGKILL;
|
|
3366 p->core_dumped = 0;
|
|
3367 p->tick++;
|
|
3368 process_tick++;
|
|
3369 status_notify ();
|
|
3370 }
|
|
3371 remove_process (proc);
|
|
3372 return Qnil;
|
|
3373 }
|
|
3374
|
|
3375 /* Kill all processes associated with `buffer'.
|
|
3376 If `buffer' is nil, kill all processes */
|
|
3377
|
|
3378 void
|
|
3379 kill_buffer_processes (Lisp_Object buffer)
|
|
3380 {
|
|
3381 Lisp_Object tail;
|
|
3382
|
|
3383 for (tail = Vprocess_list; GC_CONSP (tail);
|
|
3384 tail = XCDR (tail))
|
|
3385 {
|
|
3386 Lisp_Object proc = XCAR (tail);
|
|
3387 if (GC_PROCESSP (proc)
|
|
3388 && (GC_NILP (buffer) || GC_EQ (XPROCESS (proc)->buffer, buffer)))
|
|
3389 {
|
|
3390 if (network_connection_p (proc))
|
|
3391 Fdelete_process (proc);
|
|
3392 else if (XPROCESS (proc)->infd >= 0)
|
|
3393 process_send_signal (proc, SIGHUP, 0, 1);
|
|
3394 }
|
|
3395 }
|
|
3396 }
|
|
3397
|
|
3398 #if 0 /* Unused */
|
|
3399 int
|
|
3400 count_active_processes (void)
|
|
3401 {
|
|
3402 Lisp_Object tail;
|
|
3403 int count = 0;
|
|
3404
|
|
3405 for (tail = Vprocess_list; CONSP (tail); tail = XCDR (tail))
|
|
3406 {
|
|
3407 Lisp_Object status = XPROCESS (XCAR (tail))->status_symbol;
|
|
3408 if ((EQ (status, Qrun) || EQ (status, Qstop)))
|
|
3409 count++;
|
|
3410 }
|
|
3411
|
|
3412 return count;
|
|
3413 }
|
|
3414 #endif /* Unused */
|
|
3415
|
20
|
3416 DEFUN ("process-kill-without-query", Fprocess_kill_without_query, 1, 2, 0, /*
|
0
|
3417 Say no query needed if PROCESS is running when Emacs is exited.
|
|
3418 Optional second argument if non-nil says to require a query.
|
|
3419 Value is t if a query was formerly required.
|
20
|
3420 */
|
|
3421 (proc, require_query_p))
|
0
|
3422 {
|
|
3423 int tem;
|
|
3424
|
|
3425 CHECK_PROCESS (proc);
|
|
3426 tem = XPROCESS (proc)->kill_without_query;
|
|
3427 XPROCESS (proc)->kill_without_query = NILP (require_query_p);
|
|
3428
|
173
|
3429 return tem ? Qnil : Qt;
|
0
|
3430 }
|
|
3431
|
20
|
3432 DEFUN ("process-kill-without-query-p", Fprocess_kill_without_query_p, 1, 1, 0, /*
|
0
|
3433 Whether PROC will be killed without query if running when emacs is exited.
|
20
|
3434 */
|
|
3435 (proc))
|
0
|
3436 {
|
|
3437 CHECK_PROCESS (proc);
|
173
|
3438 return XPROCESS (proc)->kill_without_query ? Qt : Qnil;
|
0
|
3439 }
|
|
3440
|
|
3441
|
|
3442 /* This is not named init_process in order to avoid a conflict with NS 3.3 */
|
|
3443 void
|
|
3444 init_xemacs_process (void)
|
|
3445 {
|
|
3446 int i;
|
|
3447
|
|
3448 #ifdef SIGCHLD
|
|
3449 # ifndef CANNOT_DUMP
|
|
3450 if (! noninteractive || initialized)
|
|
3451 # endif
|
|
3452 signal (SIGCHLD, sigchld_handler);
|
|
3453 #endif /* SIGCHLD */
|
|
3454
|
|
3455 Vprocess_list = Qnil;
|
|
3456 for (i = 0; i < MAXDESC; i++)
|
|
3457 {
|
|
3458 descriptor_to_process[i] = Qnil;
|
255
|
3459 #if 0 /* FSFmacs */
|
0
|
3460 proc_buffered_char[i] = -1;
|
255
|
3461 #endif
|
0
|
3462 }
|
|
3463 }
|
149
|
3464
|
0
|
3465 #if 0
|
|
3466
|
149
|
3467 xxDEFUN ("process-connection", Fprocess_connection, 0, 1, 0, /*
|
0
|
3468 Return the connection type of `PROCESS'. This can be nil (pipe),
|
|
3469 t or pty (pty) or stream (socket connection).
|
149
|
3470 */
|
|
3471 (process))
|
0
|
3472 {
|
|
3473 return XPROCESS (process)->type;
|
|
3474 }
|
|
3475
|
|
3476 #endif /* 0 */
|
|
3477
|
|
3478 void
|
|
3479 syms_of_process (void)
|
|
3480 {
|
|
3481 defsymbol (&Qprocessp, "processp");
|
|
3482 defsymbol (&Qrun, "run");
|
|
3483 defsymbol (&Qstop, "stop");
|
|
3484 defsymbol (&Qsignal, "signal");
|
|
3485 /* Qexit is already defined by syms_of_eval
|
185
|
3486 * defsymbol (&Qexit, "exit");
|
0
|
3487 */
|
|
3488 defsymbol (&Qopen, "open");
|
|
3489 defsymbol (&Qclosed, "closed");
|
|
3490
|
259
|
3491 #ifdef HAVE_MULTICAST
|
|
3492 defsymbol(&Qmulticast, "multicast"); /* Used for occasional warnings */
|
|
3493 #endif
|
|
3494
|
20
|
3495 DEFSUBR (Fprocessp);
|
|
3496 DEFSUBR (Fget_process);
|
|
3497 DEFSUBR (Fget_buffer_process);
|
|
3498 DEFSUBR (Fdelete_process);
|
|
3499 DEFSUBR (Fprocess_status);
|
|
3500 DEFSUBR (Fprocess_exit_status);
|
|
3501 DEFSUBR (Fprocess_id);
|
|
3502 DEFSUBR (Fprocess_name);
|
|
3503 DEFSUBR (Fprocess_tty_name);
|
|
3504 DEFSUBR (Fprocess_command);
|
|
3505 DEFSUBR (Fset_process_buffer);
|
|
3506 DEFSUBR (Fprocess_buffer);
|
|
3507 DEFSUBR (Fprocess_mark);
|
|
3508 DEFSUBR (Fset_process_filter);
|
|
3509 DEFSUBR (Fprocess_filter);
|
|
3510 DEFSUBR (Fset_process_window_size);
|
|
3511 DEFSUBR (Fset_process_sentinel);
|
|
3512 DEFSUBR (Fprocess_sentinel);
|
|
3513 DEFSUBR (Fprocess_kill_without_query);
|
|
3514 DEFSUBR (Fprocess_kill_without_query_p);
|
|
3515 DEFSUBR (Fprocess_list);
|
|
3516 DEFSUBR (Fstart_process_internal);
|
0
|
3517 #ifdef HAVE_SOCKETS
|
20
|
3518 DEFSUBR (Fopen_network_stream_internal);
|
259
|
3519 #ifdef HAVE_MULTICAST
|
|
3520 DEFSUBR (Fopen_multicast_group_internal);
|
|
3521 #endif /* HAVE_MULTICAST */
|
0
|
3522 #endif /* HAVE_SOCKETS */
|
20
|
3523 DEFSUBR (Fprocess_send_region);
|
|
3524 DEFSUBR (Fprocess_send_string);
|
|
3525 DEFSUBR (Finterrupt_process);
|
|
3526 DEFSUBR (Fkill_process);
|
|
3527 DEFSUBR (Fquit_process);
|
|
3528 DEFSUBR (Fstop_process);
|
|
3529 DEFSUBR (Fcontinue_process);
|
|
3530 DEFSUBR (Fprocess_send_eof);
|
|
3531 DEFSUBR (Fsignal_process);
|
|
3532 /* DEFSUBR (Fprocess_connection); */
|
259
|
3533 #ifdef FILE_CODING
|
70
|
3534 DEFSUBR (Fprocess_input_coding_system);
|
|
3535 DEFSUBR (Fprocess_output_coding_system);
|
|
3536 DEFSUBR (Fset_process_input_coding_system);
|
|
3537 DEFSUBR (Fset_process_output_coding_system);
|
120
|
3538 DEFSUBR (Fprocess_coding_system);
|
|
3539 DEFSUBR (Fset_process_coding_system);
|
259
|
3540 #endif
|
0
|
3541 }
|
|
3542
|
|
3543 void
|
|
3544 vars_of_process (void)
|
|
3545 {
|
|
3546 Fprovide (intern ("subprocesses"));
|
|
3547 #ifdef HAVE_SOCKETS
|
|
3548 Fprovide (intern ("network-streams"));
|
259
|
3549 #ifdef HAVE_MULTICAST
|
|
3550 Fprovide (intern ("multicast"));
|
|
3551 #endif /* HAVE_MULTICAST */
|
|
3552 #endif /* HAVE_SOCKETS */
|
0
|
3553 staticpro (&Vprocess_list);
|
|
3554
|
|
3555 DEFVAR_BOOL ("delete-exited-processes", &delete_exited_processes /*
|
|
3556 *Non-nil means delete processes immediately when they exit.
|
|
3557 nil means don't delete them until `list-processes' is run.
|
|
3558 */ );
|
|
3559
|
|
3560 delete_exited_processes = 1;
|
|
3561
|
|
3562 DEFVAR_LISP ("process-connection-type", &Vprocess_connection_type /*
|
|
3563 Control type of device used to communicate with subprocesses.
|
|
3564 Values are nil to use a pipe, or t or `pty' to use a pty.
|
|
3565 The value has no effect if the system has no ptys or if all ptys are busy:
|
|
3566 then a pipe is used in any case.
|
|
3567 The value takes effect when `start-process' is called.
|
|
3568 */ );
|
|
3569 Vprocess_connection_type = Qt;
|
153
|
3570
|
|
3571 #ifdef PROCESS_IO_BLOCKING
|
|
3572 DEFVAR_LISP ("network-stream-blocking-port-list", &network_stream_blocking_port_list /*
|
|
3573 List of port numbers or port names to set a blocking I/O mode with connection.
|
|
3574 Nil value means to set a default(non-blocking) I/O mode.
|
|
3575 The value takes effect when `open-network-stream-internal' is called.
|
|
3576 */ );
|
|
3577 network_stream_blocking_port_list = Qnil;
|
|
3578 #endif /* PROCESS_IO_BLOCKING */
|
0
|
3579 }
|
|
3580
|
|
3581 #endif /* not NO_SUBPROCESSES */
|