comparison src/process-unix.c @ 442:abe6d1db359e r21-2-36

Import from CVS: tag r21-2-36
author cvs
date Mon, 13 Aug 2007 11:35:02 +0200
parents 8de8e3f6228a
children 576fb035e263
comparison
equal deleted inserted replaced
441:72a7cfa4a488 442:abe6d1db359e
59 #include "systime.h" 59 #include "systime.h"
60 #include "syssignal.h" /* Always include before systty.h */ 60 #include "syssignal.h" /* Always include before systty.h */
61 #include "systty.h" 61 #include "systty.h"
62 #include "syswait.h" 62 #include "syswait.h"
63 63
64 #ifdef HPUX
65 #include <grp.h> /* See grantpt fixups for HPUX below. */
66 #endif
64 67
65 /* 68 /*
66 * Implementation-specific data. Pointed to by Lisp_Process->process_data 69 * Implementation-specific data. Pointed to by Lisp_Process->process_data
67 */ 70 */
68 71
81 char pty_flag; 84 char pty_flag;
82 }; 85 };
83 86
84 #define UNIX_DATA(p) ((struct unix_process_data*)((p)->process_data)) 87 #define UNIX_DATA(p) ((struct unix_process_data*)((p)->process_data))
85 88
86 #ifdef HAVE_PTYS
87 /* The file name of the pty opened by allocate_pty. */
88
89 static char pty_name[24];
90 #endif
91
92 89
93 90
94 /**********************************************************************/ 91 /**********************************************************************/
95 /* Static helper routines */ 92 /* Static helper routines */
96 /**********************************************************************/ 93 /**********************************************************************/
125 /* Close all descriptors currently in use for communication 122 /* Close all descriptors currently in use for communication
126 with subprocess. This is used in a newly-forked subprocess 123 with subprocess. This is used in a newly-forked subprocess
127 to get rid of irrelevant descriptors. */ 124 to get rid of irrelevant descriptors. */
128 125
129 static int 126 static int
130 close_process_descs_mapfun (CONST void* key, void* contents, void* arg) 127 close_process_descs_mapfun (const void* key, void* contents, void* arg)
131 { 128 {
132 Lisp_Object proc; 129 Lisp_Object proc;
133 CVOID_TO_LISP (proc, contents); 130 CVOID_TO_LISP (proc, contents);
134 event_stream_delete_stream_pair (XPROCESS(proc)->pipe_instream, 131 event_stream_delete_stream_pair (XPROCESS(proc)->pipe_instream,
135 XPROCESS(proc)->pipe_outstream); 132 XPROCESS(proc)->pipe_outstream);
150 communication. */ 147 communication. */
151 148
152 /* This function used to be visible on the Lisp level, but there is no 149 /* This function used to be visible on the Lisp level, but there is no
153 real point in doing that. Here is the doc string: 150 real point in doing that. Here is the doc string:
154 151
155 "Connect to an existing file descriptor.\n\ 152 "Connect to an existing file descriptor.
156 Returns a subprocess-object to represent the connection.\n\ 153 Return a subprocess-object to represent the connection.
157 Input and output work as for subprocesses; `delete-process' closes it.\n\ 154 Input and output work as for subprocesses; `delete-process' closes it.
158 Args are NAME BUFFER INFD OUTFD.\n\ 155 Args are NAME BUFFER INFD OUTFD.
159 NAME is name for process. It is modified if necessary to make it unique.\n\ 156 NAME is name for process. It is modified if necessary to make it unique.
160 BUFFER is the buffer (or buffer-name) to associate with the process.\n\ 157 BUFFER is the buffer (or buffer-name) to associate with the process.
161 Process output goes at end of that buffer, unless you specify\n\ 158 Process output goes at end of that buffer, unless you specify
162 an output stream or filter function to handle the output.\n\ 159 an output stream or filter function to handle the output.
163 BUFFER may be also nil, meaning that this process is not associated\n\ 160 BUFFER may also be nil, meaning that this process is not associated
164 with any buffer\n\ 161 with any buffer.
165 INFD and OUTFD specify the file descriptors to use for input and\n\ 162 INFD and OUTFD specify the file descriptors to use for input and
166 output, respectively." 163 output, respectively."
167 */ 164 */
168 165
169 Lisp_Object 166 Lisp_Object
170 connect_to_file_descriptor (Lisp_Object name, Lisp_Object buffer, 167 connect_to_file_descriptor (Lisp_Object name, Lisp_Object buffer,
177 CHECK_STRING (name); 174 CHECK_STRING (name);
178 CHECK_INT (infd); 175 CHECK_INT (infd);
179 CHECK_INT (outfd); 176 CHECK_INT (outfd);
180 177
181 inch = XINT (infd); 178 inch = XINT (infd);
182 if (get_process_from_usid (FD_TO_USID(inch))) 179 if (get_process_from_usid (FD_TO_USID (inch)))
183 error ("There is already a process connected to fd %d", inch); 180 invalid_operation ("There is already a process connected to fd", infd);
184 if (!NILP (buffer)) 181 if (!NILP (buffer))
185 buffer = Fget_buffer_create (buffer); 182 buffer = Fget_buffer_create (buffer);
186 proc = make_process_internal (name); 183 proc = make_process_internal (name);
187 184
188 XPROCESS (proc)->pid = Fcons (infd, name); 185 XPROCESS (proc)->pid = Fcons (infd, name);
189 XPROCESS (proc)->buffer = buffer; 186 XPROCESS (proc)->buffer = buffer;
190 init_process_io_handles (XPROCESS (proc), (void*)inch, (void*)XINT (outfd), 0); 187 init_process_io_handles (XPROCESS (proc), (void*)inch, (void*)XINT (outfd),
188 0);
191 UNIX_DATA (XPROCESS (proc))->connected_via_filedesc_p = 1; 189 UNIX_DATA (XPROCESS (proc))->connected_via_filedesc_p = 1;
192 190
193 event_stream_select_process (XPROCESS (proc)); 191 event_stream_select_process (XPROCESS (proc));
194 192
195 return proc; 193 return proc;
196 } 194 }
197 195
198 #ifdef HAVE_PTYS 196 #ifdef HAVE_PTYS
197 static int allocate_pty_the_old_fashioned_way (void);
198
199 /* The file name of the (slave) pty opened by allocate_pty(). */
200 #ifndef MAX_PTYNAME_LEN
201 #define MAX_PTYNAME_LEN 64
202 #endif
203 static char pty_name[MAX_PTYNAME_LEN];
199 204
200 /* Open an available pty, returning a file descriptor. 205 /* Open an available pty, returning a file descriptor.
201 Return -1 on failure. 206 Return -1 on failure.
202 The file name of the terminal corresponding to the pty 207 The file name of the terminal corresponding to the pty
203 is left in the variable pty_name. */ 208 is left in the variable `pty_name'. */
204 209
205 static int 210 static int
206 allocate_pty (void) 211 allocate_pty (void)
207 { 212 {
208 #ifndef PTY_OPEN 213 /* Unix98 standardized grantpt, unlockpt, and ptsname, but not the
214 functions required to open a master pty in the first place :-(
215
216 Modern Unix systems all seems to have convenience methods to open
217 a master pty fd in one function call, but there is little
218 agreement on how to do it.
219
220 allocate_pty() tries all the different known easy ways of opening
221 a pty. In case of failure, we resort to the old BSD-style pty
222 grovelling code in allocate_pty_the_old_fashioned_way(). */
223 int master_fd = -1;
224 const char *slave_name = NULL;
225 const char *clone = NULL;
226 static const char * const clones[] = /* Different pty master clone devices */
227 {
228 "/dev/ptmx", /* Various systems */
229 "/dev/ptm/clone", /* HPUX */
230 "/dev/ptc", /* AIX */
231 "/dev/ptmx_bsd" /* Tru64 */
232 };
233
234 #ifdef HAVE_GETPT /* glibc */
235 master_fd = getpt ();
236 if (master_fd >= 0)
237 goto have_master;
238 #endif /* HAVE_GETPT */
239
240
241 #if defined(HAVE_OPENPTY) /* BSD, Tru64, glibc */
242 {
243 int slave_fd = -1;
244 int rc;
245 EMACS_BLOCK_SIGNAL (SIGCHLD);
246 rc = openpty (&master_fd, &slave_fd, NULL, NULL, NULL);
247 EMACS_UNBLOCK_SIGNAL (SIGCHLD);
248 if (rc == 0)
249 {
250 slave_name = ttyname (slave_fd);
251 close (slave_fd);
252 goto have_slave_name;
253 }
254 else
255 {
256 if (master_fd >= 0)
257 close (master_fd);
258 if (slave_fd >= 0)
259 close (slave_fd);
260 }
261 }
262 #endif /* HAVE_OPENPTY */
263
264 #if defined(HAVE__GETPTY) && defined (O_NDELAY) /* SGI */
265 master_fd = -1;
266 EMACS_BLOCK_SIGNAL (SIGCHLD);
267 slave_name = _getpty (&master_fd, O_RDWR | O_NDELAY, 0600, 0);
268 EMACS_UNBLOCK_SIGNAL (SIGCHLD);
269 if (master_fd >= 0 && slave_name != NULL)
270 goto have_slave_name;
271 #endif /* HAVE__GETPTY */
272
273 /* Master clone devices are available on most systems */
274 {
275 int i;
276 for (i = 0; i < countof (clones); i++)
277 {
278 clone = clones[i];
279 master_fd = open (clone, O_RDWR | O_NONBLOCK | OPEN_BINARY, 0);
280 if (master_fd >= 0)
281 goto have_master;
282 }
283 clone = NULL;
284 }
285
286 goto lose;
287
288 have_master:
289
290 #if defined (HAVE_PTSNAME)
291 slave_name = ptsname (master_fd);
292 if (slave_name)
293 goto have_slave_name;
294 #endif
295
296 /* AIX docs say to use ttyname, not ptsname, to get slave_name */
297 if (clone
298 && !strcmp (clone, "/dev/ptc")
299 && (slave_name = ttyname (master_fd)) != NULL)
300 goto have_slave_name;
301
302 goto lose;
303
304 have_slave_name:
305 strncpy (pty_name, slave_name, sizeof (pty_name));
306 pty_name[sizeof (pty_name) - 1] = '\0';
307 setup_pty (master_fd);
308
309 /* We jump through some hoops to frob the pty.
310 It's not obvious that checking the return code here is useful. */
311
312 /* "The grantpt() function will fail if it is unable to successfully
313 invoke the setuid root program. It may also fail if the
314 application has installed a signal handler to catch SIGCHLD
315 signals." */
316 #if defined (HAVE_GRANTPT) || defined (HAVE_UNLOCKPT)
317 EMACS_BLOCK_SIGNAL (SIGCHLD);
318
319 #if defined (HAVE_GRANTPT)
320 grantpt (master_fd);
321 #ifdef HPUX
322 /* grantpt() behavior on some versions of HP-UX differs from what's
323 specified in the man page: the group of the slave PTY is set to
324 the user's primary group, and we fix that. */
325 {
326 struct group *tty_group = getgrnam ("tty");
327 if (tty_group != NULL)
328 chown (pty_name, (uid_t) -1, tty_group->gr_gid);
329 }
330 #endif /* HPUX has broken grantpt() */
331 #endif /* HAVE_GRANTPT */
332
333 #if defined (HAVE_UNLOCKPT)
334 unlockpt (master_fd);
335 #endif
336
337 EMACS_UNBLOCK_SIGNAL (SIGCHLD);
338 #endif /* HAVE_GRANTPT || HAVE_UNLOCKPT */
339
340 return master_fd;
341
342 lose:
343 if (master_fd >= 0)
344 close (master_fd);
345 return allocate_pty_the_old_fashioned_way ();
346 }
347
348 /* This function tries to allocate a pty by iterating through file
349 pairs with names like /dev/ptyp1 and /dev/ttyp1. */
350 static int
351 allocate_pty_the_old_fashioned_way (void)
352 {
209 struct stat stb; 353 struct stat stb;
210 354
211 /* Some systems name their pseudoterminals so that there are gaps in 355 /* Some systems name their pseudoterminals so that there are gaps in
212 the usual sequence - for example, on HP9000/S700 systems, there 356 the usual sequence - for example, on HP9000/S700 systems, there
213 are no pseudoterminals with names ending in 'f'. So we wait for 357 are no pseudoterminals with names ending in 'f'. So we wait for
214 three failures in a row before deciding that we've reached the 358 three failures in a row before deciding that we've reached the
215 end of the ptys. */ 359 end of the ptys. */
216 int failed_count = 0; 360 int failed_count = 0;
217 #endif
218 int fd; 361 int fd;
219 #ifndef HAVE_GETPT
220 int i; 362 int i;
221 int c; 363 int c;
222 #endif
223 364
224 #ifdef PTY_ITERATION 365 #ifdef PTY_ITERATION
225 PTY_ITERATION 366 PTY_ITERATION
226 #else 367 #else
368 # ifndef FIRST_PTY_LETTER
369 # define FIRST_PTY_LETTER 'p'
370 # endif
227 for (c = FIRST_PTY_LETTER; c <= 'z'; c++) 371 for (c = FIRST_PTY_LETTER; c <= 'z'; c++)
228 for (i = 0; i < 16; i++) 372 for (i = 0; i < 16; i++)
229 #endif 373 #endif /* PTY_ITERATION */
374
230 { 375 {
231 #ifdef PTY_NAME_SPRINTF 376 #ifdef PTY_NAME_SPRINTF
232 PTY_NAME_SPRINTF 377 PTY_NAME_SPRINTF
233 #else 378 #else
234 sprintf (pty_name, "/dev/pty%c%x", c, i); 379 sprintf (pty_name, "/dev/pty%c%x", c, i);
235 #endif /* no PTY_NAME_SPRINTF */ 380 #endif /* no PTY_NAME_SPRINTF */
236 381
237 #ifdef PTY_OPEN 382 if (xemacs_stat (pty_name, &stb) < 0)
238 PTY_OPEN;
239 #else /* no PTY_OPEN */
240 #ifdef IRIS
241 /* Unusual IRIS code */
242 *ptyv = open ("/dev/ptc", O_RDWR | O_NONBLOCK | OPEN_BINARY, 0);
243 if (fd < 0)
244 return -1;
245 if (fstat (fd, &stb) < 0)
246 return -1;
247 #else /* not IRIS */
248 if (stat (pty_name, &stb) < 0)
249 { 383 {
250 failed_count++; 384 if (++failed_count >= 3)
251 if (failed_count >= 3)
252 return -1; 385 return -1;
253 } 386 }
254 else 387 else
255 failed_count = 0; 388 failed_count = 0;
256 fd = open (pty_name, O_RDWR | O_NONBLOCK | OPEN_BINARY, 0); 389 fd = open (pty_name, O_RDWR | O_NONBLOCK | OPEN_BINARY, 0);
257 #endif /* not IRIS */
258 #endif /* no PTY_OPEN */
259 390
260 if (fd >= 0) 391 if (fd >= 0)
261 { 392 {
262 /* check to make certain that both sides are available
263 this avoids a nasty yet stupid bug in rlogins */
264 #ifdef PTY_TTY_NAME_SPRINTF 393 #ifdef PTY_TTY_NAME_SPRINTF
265 PTY_TTY_NAME_SPRINTF 394 PTY_TTY_NAME_SPRINTF
266 #else 395 #else
267 sprintf (pty_name, "/dev/tty%c%x", c, i); 396 sprintf (pty_name, "/dev/tty%c%x", c, i);
268 #endif /* no PTY_TTY_NAME_SPRINTF */ 397 #endif /* no PTY_TTY_NAME_SPRINTF */
269 #if !defined(UNIPLUS) && !defined(HAVE_GETPT) 398 if (access (pty_name, R_OK | W_OK) == 0)
270 if (access (pty_name, 6) != 0)
271 { 399 {
272 close (fd); 400 setup_pty (fd);
273 #if !defined(IRIS) && !defined(__sgi) 401 return fd;
274 continue;
275 #else
276 return -1;
277 #endif /* IRIS */
278 } 402 }
279 #endif /* not UNIPLUS */ 403 close (fd);
280 setup_pty (fd);
281 return fd;
282 } 404 }
283 } 405 } /* iteration */
284 return -1; 406 return -1;
285 } 407 }
286 #endif /* HAVE_PTYS */ 408 #endif /* HAVE_PTYS */
287 409
288 static int 410 static int
370 return 1; 492 return 1;
371 } 493 }
372 #endif /* !(HAVE_GETADDRINFO && HAVE_GETNAMEINFO) */ 494 #endif /* !(HAVE_GETADDRINFO && HAVE_GETNAMEINFO) */
373 495
374 static void 496 static void
375 set_socket_nonblocking_maybe (int fd, int port, CONST char* proto) 497 set_socket_nonblocking_maybe (int fd, int port, const char* proto)
376 { 498 {
377 #ifdef PROCESS_IO_BLOCKING 499 #ifdef PROCESS_IO_BLOCKING
378 Lisp_Object tail; 500 Lisp_Object tail;
379 501
380 for (tail = network_stream_blocking_port_list; CONSP (tail); tail = XCDR (tail)) 502 for (tail = network_stream_blocking_port_list; CONSP (tail); tail = XCDR (tail))
573 695
574 static int 696 static int
575 process_signal_char (int tty_fd, int signo) 697 process_signal_char (int tty_fd, int signo)
576 { 698 {
577 /* If it's not a tty, pray that these default values work */ 699 /* If it's not a tty, pray that these default values work */
578 if (!isatty(tty_fd)) { 700 if (! isatty (tty_fd)) {
579 #define CNTL(ch) (037 & (ch)) 701 #define CNTL(ch) (037 & (ch))
580 switch (signo) 702 switch (signo)
581 { 703 {
582 case SIGINT: return CNTL('C'); 704 case SIGINT: return CNTL ('C');
583 case SIGQUIT: return CNTL('\\'); 705 case SIGQUIT: return CNTL ('\\');
584 #ifdef SIGTSTP 706 #ifdef SIGTSTP
585 case SIGTSTP: return CNTL('Z'); 707 case SIGTSTP: return CNTL ('Z');
586 #endif 708 #endif
587 } 709 }
588 } 710 }
589 711
590 #ifdef HAVE_TERMIOS 712 #ifdef HAVE_TERMIOS
692 814
693 /* 815 /*
694 * Initialize any process local data. This is called when newly 816 * Initialize any process local data. This is called when newly
695 * created process is connected to real OS file handles. The 817 * created process is connected to real OS file handles. The
696 * handles are generally represented by void* type, but are 818 * handles are generally represented by void* type, but are
697 * of type int (file descriptors) for UNIX 819 * of type int (file descriptors) for UNIX.
698 */ 820 */
699 821
700 static void 822 static void
701 unix_init_process_io_handles (Lisp_Process *p, void* in, void* out, int flags) 823 unix_init_process_io_handles (Lisp_Process *p, void* in, void* out, int flags)
702 { 824 {
716 static int 838 static int
717 unix_create_process (Lisp_Process *p, 839 unix_create_process (Lisp_Process *p,
718 Lisp_Object *argv, int nargv, 840 Lisp_Object *argv, int nargv,
719 Lisp_Object program, Lisp_Object cur_dir) 841 Lisp_Object program, Lisp_Object cur_dir)
720 { 842 {
721 /* This function rewritten by ben@xemacs.org. */
722
723 int pid; 843 int pid;
724 int inchannel = -1; 844 int inchannel = -1;
725 int outchannel = -1; 845 int outchannel = -1;
726 /* Use volatile to protect variables from being clobbered by longjmp. */ 846 /* Use volatile to protect variables from being clobbered by longjmp. */
727 volatile int forkin = -1; 847 volatile int forkin = -1;
777 pty_flag ? STREAM_PTY_FLUSHING : 0); 897 pty_flag ? STREAM_PTY_FLUSHING : 0);
778 /* Record the tty descriptor used in the subprocess. */ 898 /* Record the tty descriptor used in the subprocess. */
779 UNIX_DATA(p)->subtty = forkin; 899 UNIX_DATA(p)->subtty = forkin;
780 900
781 { 901 {
782 #if !defined(__CYGWIN32__) 902 #if !defined(CYGWIN)
783 /* child_setup must clobber environ on systems with true vfork. 903 /* child_setup must clobber environ on systems with true vfork.
784 Protect it from permanent change. */ 904 Protect it from permanent change. */
785 char **save_environ = environ; 905 char **save_environ = environ;
786 #endif 906 #endif
787 907
790 { 910 {
791 /**** Now we're in the child process ****/ 911 /**** Now we're in the child process ****/
792 int xforkin = forkin; 912 int xforkin = forkin;
793 int xforkout = forkout; 913 int xforkout = forkout;
794 914
795 if (!pty_flag) 915 /* Disconnect the current controlling terminal, pursuant to
796 EMACS_SEPARATE_PROCESS_GROUP (); 916 making the pty be the controlling terminal of the process.
917 Also put us in our own process group. */
918
919 disconnect_controlling_terminal ();
920
797 #ifdef HAVE_PTYS 921 #ifdef HAVE_PTYS
798 else 922 if (pty_flag)
799 { 923 {
800 /* Disconnect the current controlling terminal, pursuant to
801 making the pty be the controlling terminal of the process.
802 Also put us in our own process group. */
803
804 disconnect_controlling_terminal ();
805
806 /* Open the pty connection and make the pty's terminal 924 /* Open the pty connection and make the pty's terminal
807 our controlling terminal. 925 our controlling terminal.
808 926
809 On systems with TIOCSCTTY, we just use it to set 927 On systems with TIOCSCTTY, we just use it to set
810 the controlling terminal. On other systems, the 928 the controlling terminal. On other systems, the
853 971
854 /* Miscellaneous setup required for some systems. 972 /* Miscellaneous setup required for some systems.
855 Must be done before using tc* functions on xforkin. 973 Must be done before using tc* functions on xforkin.
856 This guarantees that isatty(xforkin) is true. */ 974 This guarantees that isatty(xforkin) is true. */
857 975
858 # ifdef SETUP_SLAVE_PTY 976 # if defined (HAVE_ISASTREAM) && defined (I_PUSH)
859 SETUP_SLAVE_PTY; 977 if (isastream (xforkin))
860 # endif /* SETUP_SLAVE_PTY */ 978 {
979 # if defined (I_FIND)
980 # define stream_module_pushed(fd, module) (ioctl (fd, I_FIND, module) == 1)
981 # else
982 # define stream_module_pushed(fd, module) 0
983 # endif
984 if (! stream_module_pushed (xforkin, "ptem"))
985 ioctl (xforkin, I_PUSH, "ptem");
986 if (! stream_module_pushed (xforkin, "ldterm"))
987 ioctl (xforkin, I_PUSH, "ldterm");
988 if (! stream_module_pushed (xforkin, "ttcompat"))
989 ioctl (xforkin, I_PUSH, "ttcompat");
990 }
991 # endif /* HAVE_ISASTREAM */
861 992
862 # ifdef TIOCSCTTY 993 # ifdef TIOCSCTTY
863 /* We ignore the return value 994 /* We ignore the return value
864 because faith@cs.unc.edu says that is necessary on Linux. */ 995 because faith@cs.unc.edu says that is necessary on Linux. */
865 assert (isatty (xforkin)); 996 assert (isatty (xforkin));
889 1020
890 /* Make our process group be the foreground group 1021 /* Make our process group be the foreground group
891 of our new controlling terminal. */ 1022 of our new controlling terminal. */
892 1023
893 { 1024 {
894 int piddly = EMACS_GET_PROCESS_GROUP (); 1025 pid_t piddly = EMACS_GET_PROCESS_GROUP ();
895 EMACS_SET_TTY_PROCESS_GROUP (xforkin, &piddly); 1026 EMACS_SET_TTY_PROCESS_GROUP (xforkin, &piddly);
896 } 1027 }
897 1028
898 /* On AIX, we've disabled SIGHUP above once we start a 1029 /* On AIX, we've disabled SIGHUP above once we start a
899 child on a pty. Now reenable it in the child, so it 1030 child on a pty. Now reenable it in the child, so it
926 CHECK_STRING (argv[i]); 1057 CHECK_STRING (argv[i]);
927 new_argv[i + 1] = (char *) XSTRING_DATA (argv[i]); 1058 new_argv[i + 1] = (char *) XSTRING_DATA (argv[i]);
928 } 1059 }
929 new_argv[i + 1] = 0; 1060 new_argv[i + 1] = 0;
930 1061
931 TO_EXTERNAL_FORMAT (LISP_STRING, cur_dir, 1062 LISP_STRING_TO_EXTERNAL (cur_dir, current_dir, Qfile_name);
932 C_STRING_ALLOCA, current_dir,
933 Qfile_name);
934 1063
935 child_setup (xforkin, xforkout, xforkout, new_argv, current_dir); 1064 child_setup (xforkin, xforkout, xforkout, new_argv, current_dir);
936 } 1065 }
937 1066
938 } /**** End of child code ****/ 1067 } /**** End of child code ****/
939 1068
940 /**** Back in parent process ****/ 1069 /**** Back in parent process ****/
941 #if !defined(__CYGWIN32__) 1070 #if !defined(CYGWIN)
942 environ = save_environ; 1071 environ = save_environ;
943 #endif 1072 #endif
944 } 1073 }
945 1074
946 if (pid < 0) 1075 if (pid < 0)
947 { 1076 {
1077 int save_errno = errno;
948 close_descriptor_pair (forkin, forkout); 1078 close_descriptor_pair (forkin, forkout);
1079 errno = save_errno;
949 report_file_error ("Doing fork", Qnil); 1080 report_file_error ("Doing fork", Qnil);
950 } 1081 }
951 1082
952 /* #### dmoore - why is this commented out, otherwise we leave 1083 /* #### dmoore - why is this commented out, otherwise we leave
953 subtty = forkin, but then we close forkin just below. */ 1084 subtty = forkin, but then we close forkin just below. */
1137 /* Use volatile to protect variables from being clobbered by longjmp. */ 1268 /* Use volatile to protect variables from being clobbered by longjmp. */
1138 SIGTYPE (*volatile old_sigpipe) (int) = 0; 1269 SIGTYPE (*volatile old_sigpipe) (int) = 0;
1139 volatile Lisp_Object vol_proc = proc; 1270 volatile Lisp_Object vol_proc = proc;
1140 Lisp_Process *volatile p = XPROCESS (proc); 1271 Lisp_Process *volatile p = XPROCESS (proc);
1141 1272
1273 /* #### JV: layering violation?
1274
1275 This function knows too much about the relation between the encoding
1276 stream (DATA_OUTSTREAM) and the actual output stream p->output_stream.
1277
1278 If encoding streams properly forwarded all calls, we could simply
1279 use DATA_OUTSTREAM everywhere. */
1280
1142 if (!SETJMP (send_process_frame)) 1281 if (!SETJMP (send_process_frame))
1143 { 1282 {
1144 /* use a reasonable-sized buffer (somewhere around the size of the 1283 /* use a reasonable-sized buffer (somewhere around the size of the
1145 stream buffer) so as to avoid inundating the stream with blocked 1284 stream buffer) so as to avoid inundating the stream with blocked
1146 data. */ 1285 data. */
1171 { 1310 {
1172 /* Buffer is full. Wait, accepting input; 1311 /* Buffer is full. Wait, accepting input;
1173 that may allow the program 1312 that may allow the program
1174 to finish doing output and read more. */ 1313 to finish doing output and read more. */
1175 Faccept_process_output (Qnil, make_int (1), Qnil); 1314 Faccept_process_output (Qnil, make_int (1), Qnil);
1315 /* It could have *really* finished, deleting the process */
1316 if (NILP(p->pipe_outstream))
1317 return;
1176 old_sigpipe = 1318 old_sigpipe =
1177 (SIGTYPE (*) (int)) signal (SIGPIPE, send_process_trap); 1319 (SIGTYPE (*) (int)) signal (SIGPIPE, send_process_trap);
1178 Lstream_flush (XLSTREAM (p->pipe_outstream)); 1320 Lstream_flush (XLSTREAM (p->pipe_outstream));
1179 signal (SIGPIPE, old_sigpipe); 1321 signal (SIGPIPE, old_sigpipe);
1180 } 1322 }
1191 p->exit_code = 256; /* #### SIGPIPE ??? */ 1333 p->exit_code = 256; /* #### SIGPIPE ??? */
1192 p->core_dumped = 0; 1334 p->core_dumped = 0;
1193 p->tick++; 1335 p->tick++;
1194 process_tick++; 1336 process_tick++;
1195 deactivate_process (*((Lisp_Object *) (&vol_proc))); 1337 deactivate_process (*((Lisp_Object *) (&vol_proc)));
1196 error ("SIGPIPE raised on process %s; closed it", 1338 invalid_operation ("SIGPIPE raised on process; closed it", p->name);
1197 XSTRING_DATA (p->name));
1198 } 1339 }
1199 1340
1200 old_sigpipe = (SIGTYPE (*) (int)) signal (SIGPIPE, send_process_trap); 1341 old_sigpipe = (SIGTYPE (*) (int)) signal (SIGPIPE, send_process_trap);
1201 Lstream_flush (XLSTREAM (DATA_OUTSTREAM(p))); 1342 Lstream_flush (XLSTREAM (DATA_OUTSTREAM(p)));
1202 signal (SIGPIPE, old_sigpipe); 1343 signal (SIGPIPE, old_sigpipe);
1221 things up. */ 1362 things up. */
1222 #if 0 1363 #if 0
1223 Bufbyte eof_char = get_eof_char (XPROCESS (proc)); 1364 Bufbyte eof_char = get_eof_char (XPROCESS (proc));
1224 send_process (proc, Qnil, &eof_char, 0, 1); 1365 send_process (proc, Qnil, &eof_char, 0, 1);
1225 #else 1366 #else
1226 send_process (proc, Qnil, (CONST Bufbyte *) "\004", 0, 1); 1367 send_process (proc, Qnil, (const Bufbyte *) "\004", 0, 1);
1227 #endif 1368 #endif
1228 return 1; 1369 return 1;
1229 } 1370 }
1230 1371
1231 /* 1372 /*
1259 UNIX_DATA(p)->infd = -1; 1400 UNIX_DATA(p)->infd = -1;
1260 1401
1261 return usid; 1402 return usid;
1262 } 1403 }
1263 1404
1264 /* send a signal number SIGNO to PROCESS. 1405 /* If the subtty field of the process data is not filled in, do so now. */
1406 static void
1407 try_to_initialize_subtty (struct unix_process_data *upd)
1408 {
1409 if (upd->pty_flag
1410 && (upd->subtty = -1 || ! isatty (upd->subtty))
1411 && STRINGP (upd->tty_name))
1412 upd->subtty = open (XSTRING_DATA (upd->tty_name), O_RDWR, 0);
1413 }
1414
1415 /* Send signal number SIGNO to PROCESS.
1265 CURRENT_GROUP means send to the process group that currently owns 1416 CURRENT_GROUP means send to the process group that currently owns
1266 the terminal being used to communicate with PROCESS. 1417 the terminal being used to communicate with PROCESS.
1267 This is used for various commands in shell mode. 1418 This is used for various commands in shell mode.
1268 If NOMSG is zero, insert signal-announcements into process's buffers 1419 If NOMSG is zero, insert signal-announcements into process's buffers
1269 right away. 1420 right away.
1270 1421
1271 If we can, we try to signal PROCESS by sending control characters 1422 If we can, we try to signal PROCESS by sending control characters
1272 down the pty. This allows us to signal inferiors who have changed 1423 down the pty. This allows us to signal inferiors who have changed
1273 their uid, for which killpg would return an EPERM error. 1424 their uid, for which killpg would return an EPERM error,
1274 1425 or processes running on other machines via remote login.
1275 The method signals an error if the given SIGNO is not valid 1426
1276 */ 1427 The method signals an error if the given SIGNO is not valid. */
1277 1428
1278 static void 1429 static void
1279 unix_kill_child_process (Lisp_Object proc, int signo, 1430 unix_kill_child_process (Lisp_Object proc, int signo,
1280 int current_group, int nomsg) 1431 int current_group, int nomsg)
1281 { 1432 {
1282 int gid; 1433 pid_t pgid = -1;
1283 int no_pgrp = 0;
1284 int kill_retval;
1285 Lisp_Process *p = XPROCESS (proc); 1434 Lisp_Process *p = XPROCESS (proc);
1286 1435 struct unix_process_data *d = UNIX_DATA (p);
1287 if (!UNIX_DATA(p)->pty_flag)
1288 current_group = 0;
1289
1290 /* If we are using pgrps, get a pgrp number and make it negative. */
1291 if (current_group)
1292 {
1293 #ifdef SIGNALS_VIA_CHARACTERS
1294 /* If possible, send signals to the entire pgrp
1295 by sending an input character to it. */
1296 {
1297 char sigchar = process_signal_char(UNIX_DATA(p)->subtty, signo);
1298 if (sigchar) {
1299 send_process (proc, Qnil, (Bufbyte *) &sigchar, 0, 1);
1300 return;
1301 }
1302 }
1303 #endif /* ! defined (SIGNALS_VIA_CHARACTERS) */
1304
1305 #ifdef TIOCGPGRP
1306 /* Get the pgrp using the tty itself, if we have that.
1307 Otherwise, use the pty to get the pgrp.
1308 On pfa systems, saka@pfu.fujitsu.co.JP writes:
1309 "TIOCGPGRP symbol defined in sys/ioctl.h at E50.
1310 But, TIOCGPGRP does not work on E50 ;-P works fine on E60"
1311 His patch indicates that if TIOCGPGRP returns an error, then
1312 we should just assume that p->pid is also the process group id. */
1313 {
1314 int err;
1315
1316 err = ioctl ( (UNIX_DATA(p)->subtty != -1
1317 ? UNIX_DATA(p)->subtty
1318 : UNIX_DATA(p)->infd), TIOCGPGRP, &gid);
1319
1320 #ifdef pfa
1321 if (err == -1)
1322 gid = - XINT (p->pid);
1323 #endif /* ! defined (pfa) */
1324 }
1325 if (gid == -1)
1326 no_pgrp = 1;
1327 else
1328 gid = - gid;
1329 #else /* ! defined (TIOCGPGRP ) */
1330 /* Can't select pgrps on this system, so we know that
1331 the child itself heads the pgrp. */
1332 gid = - XINT (p->pid);
1333 #endif /* ! defined (TIOCGPGRP ) */
1334 }
1335 else
1336 gid = - XINT (p->pid);
1337 1436
1338 switch (signo) 1437 switch (signo)
1339 { 1438 {
1340 #ifdef SIGCONT 1439 #ifdef SIGCONT
1341 case SIGCONT: 1440 case SIGCONT:
1348 break; 1447 break;
1349 #endif /* ! defined (SIGCONT) */ 1448 #endif /* ! defined (SIGCONT) */
1350 case SIGINT: 1449 case SIGINT:
1351 case SIGQUIT: 1450 case SIGQUIT:
1352 case SIGKILL: 1451 case SIGKILL:
1353 flush_pending_output (UNIX_DATA(p)->infd); 1452 flush_pending_output (d->infd);
1354 break; 1453 break;
1355 } 1454 }
1356 1455
1357 /* If we don't have process groups, send the signal to the immediate 1456 if (! d->pty_flag)
1358 subprocess. That isn't really right, but it's better than any 1457 current_group = 0;
1359 obvious alternative. */ 1458
1360 if (no_pgrp) 1459 /* If current_group is true, we want to send a signal to the
1361 { 1460 foreground process group of the terminal our child process is
1362 kill_retval = kill (XINT (p->pid), signo) ? errno : 0; 1461 running on. You would think that would be easy.
1363 } 1462
1364 else 1463 The BSD people invented the TIOCPGRP ioctl to get the foreground
1365 { 1464 process group of a tty. That, combined with killpg, gives us
1366 /* gid may be a pid, or minus a pgrp's number */ 1465 what we want.
1367 #if defined (TIOCSIGNAL) || defined (TIOCSIGSEND) 1466
1368 if (current_group) 1467 However, the POSIX standards people, in their infinite wisdom,
1468 have seen fit to only allow this for processes which have the
1469 terminal as controlling terminal, which doesn't apply to us.
1470
1471 Sooo..., we have to do something non-standard. The ioctls
1472 TIOCSIGNAL, TIOCSIG, and TIOCSIGSEND send the signal directly on
1473 many systems. POSIX tcgetpgrp(), since it is *documented* as not
1474 doing what we want, is actually less likely to work than the BSD
1475 ioctl TIOCGPGRP it is supposed to obsolete. Sometimes we have to
1476 use TIOCGPGRP on the master end, sometimes the slave end
1477 (probably an AIX bug). So we better get a fd for the slave if we
1478 haven't got it yet. On some systems none of these work, so then
1479 we just fall back to the non-current_group behavior and kill the
1480 process group of the child. */
1481 if (current_group)
1482 {
1483 try_to_initialize_subtty (d);
1484
1485 #ifdef SIGNALS_VIA_CHARACTERS
1486 /* If possible, send signals to the entire pgrp
1487 by sending an input character to it. */
1488 {
1489 char sigchar = process_signal_char (d->subtty, signo);
1490 if (sigchar)
1491 {
1492 send_process (proc, Qnil, (Bufbyte *) &sigchar, 0, 1);
1493 return;
1494 }
1495 }
1496 #endif /* SIGNALS_VIA_CHARACTERS */
1497
1498 #ifdef TIOCGPGRP
1499 if (pgid == -1)
1500 ioctl (d->infd, TIOCGPGRP, &pgid); /* BSD */
1501 if (pgid == -1 && d->subtty != -1)
1502 ioctl (d->subtty, TIOCGPGRP, &pgid); /* Only this works on AIX! */
1503 #endif /* TIOCGPGRP */
1504
1505 if (pgid == -1)
1369 { 1506 {
1370 #ifdef TIOCSIGNAL 1507 /* Many systems provide an ioctl to send a signal directly */
1371 kill_retval = ioctl (UNIX_DATA(p)->infd, TIOCSIGNAL, signo); 1508 #ifdef TIOCSIGNAL /* Solaris, HP-UX */
1372 #else /* ! defined (TIOCSIGNAL) */ 1509 if (ioctl (d->infd, TIOCSIGNAL, signo) != -1)
1373 kill_retval = ioctl (UNIX_DATA(p)->infd, TIOCSIGSEND, signo); 1510 return;
1374 #endif /* ! defined (TIOCSIGNAL) */ 1511 #endif /* TIOCSIGNAL */
1512
1513 #ifdef TIOCSIG /* BSD */
1514 if (ioctl (d->infd, TIOCSIG, signo) != -1)
1515 return;
1516 #endif /* TIOCSIG */
1375 } 1517 }
1376 else 1518 } /* current_group */
1377 kill_retval = kill (- XINT (p->pid), signo) ? errno : 0; 1519
1378 #else /* ! (defined (TIOCSIGNAL) || defined (TIOCSIGSEND)) */ 1520 if (pgid == -1)
1379 kill_retval = EMACS_KILLPG (-gid, signo) ? errno : 0; 1521 /* Either current_group is 0, or we failed to get the foreground
1380 #endif /* ! (defined (TIOCSIGNAL) || defined (TIOCSIGSEND)) */ 1522 process group using the trickery above. So we fall back to
1381 } 1523 sending the signal to the process group of our child process.
1382 1524 Since this is often a shell that ignores signals like SIGINT,
1383 if (kill_retval < 0 && errno == EINVAL) 1525 the shell's subprocess is killed, which is the desired effect.
1384 error ("Signal number %d is invalid for this system", signo); 1526 The process group of p->pid is always p->pid, since it was
1385 } 1527 created as a process group leader. */
1386 1528 pgid = XINT (p->pid);
1387 /* 1529
1388 * Kill any process in the system given its PID. 1530 /* Finally send the signal. */
1389 * 1531 if (EMACS_KILLPG (pgid, signo) == -1)
1390 * Returns zero if a signal successfully sent, or 1532 error ("kill (%ld, %ld) failed: %s",
1391 * negative number upon failure 1533 (long) pgid, (long) signo, strerror (errno));
1392 */ 1534 }
1535
1536 /* Send signal SIGCODE to any process in the system given its PID.
1537 Return zero if successful, a negative number upon failure. */
1393 1538
1394 static int 1539 static int
1395 unix_kill_process_by_pid (int pid, int sigcode) 1540 unix_kill_process_by_pid (int pid, int sigcode)
1396 { 1541 {
1397 return kill (pid, sigcode); 1542 return kill (pid, sigcode);
1398 } 1543 }
1399 1544
1400 /* 1545 /* Return TTY name used to communicate with subprocess. */
1401 * Return TTY name used to communicate with subprocess
1402 */
1403 1546
1404 static Lisp_Object 1547 static Lisp_Object
1405 unix_get_tty_name (Lisp_Process *p) 1548 unix_get_tty_name (Lisp_Process *p)
1406 { 1549 {
1407 return UNIX_DATA (p)->tty_name; 1550 return UNIX_DATA (p)->tty_name;
1408 } 1551 }
1409 1552
1410 /* 1553 /* Canonicalize host name HOST, and return its canonical form.
1411 * Canonicalize host name HOST, and return its canonical form 1554 The default implementation just takes HOST for a canonical name. */
1412 *
1413 * The default implementation just takes HOST for a canonical name.
1414 */
1415 1555
1416 #ifdef HAVE_SOCKETS 1556 #ifdef HAVE_SOCKETS
1417 static Lisp_Object 1557 static Lisp_Object
1418 unix_canonicalize_host_name (Lisp_Object host) 1558 unix_canonicalize_host_name (Lisp_Object host)
1419 { 1559 {
1427 xzero (hints); 1567 xzero (hints);
1428 hints.ai_flags = AI_CANONNAME; 1568 hints.ai_flags = AI_CANONNAME;
1429 hints.ai_family = AF_UNSPEC; 1569 hints.ai_family = AF_UNSPEC;
1430 hints.ai_socktype = SOCK_STREAM; 1570 hints.ai_socktype = SOCK_STREAM;
1431 hints.ai_protocol = 0; 1571 hints.ai_protocol = 0;
1432 TO_EXTERNAL_FORMAT (LISP_STRING, host, C_STRING_ALLOCA, ext_host, Qnative); 1572 LISP_STRING_TO_EXTERNAL (host, ext_host, Qnative);
1433 retval = getaddrinfo (ext_host, NULL, &hints, &res); 1573 retval = getaddrinfo (ext_host, NULL, &hints, &res);
1434 if (retval != 0) 1574 if (retval != 0)
1435 { 1575 {
1436 char *gai_error; 1576 char *gai_error;
1437 1577
1438 TO_INTERNAL_FORMAT (C_STRING, gai_strerror (retval), 1578 EXTERNAL_TO_C_STRING (gai_strerror (retval), gai_error, Qnative);
1439 C_STRING_ALLOCA, gai_error,
1440 Qnative);
1441 maybe_error (Qprocess, ERROR_ME_NOT, 1579 maybe_error (Qprocess, ERROR_ME_NOT,
1442 "%s \"%s\"", gai_error, XSTRING_DATA (host)); 1580 "%s \"%s\"", gai_error, XSTRING_DATA (host));
1443 canonname = host; 1581 canonname = host;
1444 } 1582 }
1445 else 1583 else
1465 /* #### any clue what to do here? */ 1603 /* #### any clue what to do here? */
1466 return host; 1604 return host;
1467 #endif /* ! HAVE_GETADDRINFO */ 1605 #endif /* ! HAVE_GETADDRINFO */
1468 } 1606 }
1469 1607
1470 /* open a TCP network connection to a given HOST/SERVICE. Treated 1608 /* Open a TCP network connection to a given HOST/SERVICE.
1471 exactly like a normal process when reading and writing. Only 1609 Treated exactly like a normal process when reading and writing.
1472 differences are in status display and process deletion. A network 1610 Only differences are in status display and process deletion.
1473 connection has no PID; you cannot signal it. All you can do is 1611 A network connection has no PID; you cannot signal it. All you can
1474 deactivate and close it via delete-process */ 1612 do is deactivate and close it via delete-process. */
1475 1613
1476 static void 1614 static void
1477 unix_open_network_stream (Lisp_Object name, Lisp_Object host, Lisp_Object service, 1615 unix_open_network_stream (Lisp_Object name, Lisp_Object host, Lisp_Object service,
1478 Lisp_Object protocol, void** vinfd, void** voutfd) 1616 Lisp_Object protocol, void** vinfd, void** voutfd)
1479 { 1617 {
1485 int retval; 1623 int retval;
1486 1624
1487 CHECK_STRING (host); 1625 CHECK_STRING (host);
1488 1626
1489 if (!EQ (protocol, Qtcp) && !EQ (protocol, Qudp)) 1627 if (!EQ (protocol, Qtcp) && !EQ (protocol, Qudp))
1490 error ("Unsupported protocol \"%s\"", 1628 invalid_argument ("Unsupported protocol", protocol);
1491 string_data (symbol_name (XSYMBOL (protocol))));
1492 1629
1493 { 1630 {
1494 #if defined(HAVE_GETADDRINFO) && defined(HAVE_GETNAMEINFO) 1631 #if defined(HAVE_GETADDRINFO) && defined(HAVE_GETNAMEINFO)
1495 struct addrinfo hints, *res; 1632 struct addrinfo hints, *res;
1496 struct addrinfo * volatile lres; 1633 struct addrinfo * volatile lres;
1510 port = htons ((unsigned short) XINT (service)); 1647 port = htons ((unsigned short) XINT (service));
1511 } 1648 }
1512 else 1649 else
1513 { 1650 {
1514 CHECK_STRING (service); 1651 CHECK_STRING (service);
1515 TO_EXTERNAL_FORMAT (LISP_STRING, service, 1652 LISP_STRING_TO_EXTERNAL (service, portstring, Qnative);
1516 C_STRING_ALLOCA, portstring,
1517 Qnative);
1518 port = 0; 1653 port = 0;
1519 } 1654 }
1520 1655
1521 xzero (hints); 1656 xzero (hints);
1522 hints.ai_flags = 0; 1657 hints.ai_flags = 0;
1524 if (EQ (protocol, Qtcp)) 1659 if (EQ (protocol, Qtcp))
1525 hints.ai_socktype = SOCK_STREAM; 1660 hints.ai_socktype = SOCK_STREAM;
1526 else /* EQ (protocol, Qudp) */ 1661 else /* EQ (protocol, Qudp) */
1527 hints.ai_socktype = SOCK_DGRAM; 1662 hints.ai_socktype = SOCK_DGRAM;
1528 hints.ai_protocol = 0; 1663 hints.ai_protocol = 0;
1529 TO_EXTERNAL_FORMAT (LISP_STRING, host, C_STRING_ALLOCA, ext_host, Qnative); 1664 LISP_STRING_TO_EXTERNAL (host, ext_host, Qnative);
1530 retval = getaddrinfo (ext_host, portstring, &hints, &res); 1665 retval = getaddrinfo (ext_host, portstring, &hints, &res);
1531 if (retval != 0) 1666 if (retval != 0)
1532 { 1667 {
1533 char *gai_error; 1668 char *gai_error;
1534 1669
1535 TO_INTERNAL_FORMAT (C_STRING, gai_strerror (retval), 1670 EXTERNAL_TO_C_STRING (gai_strerror (retval), gai_error, Qnative);
1536 C_STRING_ALLOCA, gai_error,
1537 Qnative);
1538 error ("%s/%s %s", XSTRING_DATA (host), portstring, gai_error); 1671 error ("%s/%s %s", XSTRING_DATA (host), portstring, gai_error);
1539 } 1672 }
1540 1673
1541 /* address loop */ 1674 /* address loop */
1542 for (lres = res; lres ; lres = lres->ai_next) 1675 for (lres = res; lres ; lres = lres->ai_next)
1663 svc_info = getservbyname ((char *) XSTRING_DATA (service), "tcp"); 1796 svc_info = getservbyname ((char *) XSTRING_DATA (service), "tcp");
1664 else /* EQ (protocol, Qudp) */ 1797 else /* EQ (protocol, Qudp) */
1665 svc_info = getservbyname ((char *) XSTRING_DATA (service), "udp"); 1798 svc_info = getservbyname ((char *) XSTRING_DATA (service), "udp");
1666 1799
1667 if (svc_info == 0) 1800 if (svc_info == 0)
1668 error ("Unknown service \"%s\"", XSTRING_DATA (service)); 1801 invalid_argument ("Unknown service", service);
1669 port = svc_info->s_port; 1802 port = svc_info->s_port;
1670 } 1803 }
1671 1804
1672 get_internet_address (host, &address, ERROR_ME); 1805 get_internet_address (host, &address, ERROR_ME);
1673 address.sin_port = port; 1806 address.sin_port = port;
1763 } 1896 }
1764 1897
1765 1898
1766 #ifdef HAVE_MULTICAST 1899 #ifdef HAVE_MULTICAST
1767 1900
1768 /* Didier Verna <verna@inf.enst.fr> Nov. 28 1997. 1901 /* Didier Verna <didier@xemacs.org> Nov. 28 1997.
1769 1902
1770 This function is similar to open-network-stream-internal, but provides a 1903 This function is similar to open-network-stream-internal, but provides a
1771 mean to open an UDP multicast connection instead of a TCP one. Like in the 1904 mean to open an UDP multicast connection instead of a TCP one. Like in the
1772 TCP case, the multicast connection will be seen as a sub-process, 1905 TCP case, the multicast connection will be seen as a sub-process,
1773 1906
1782 about the kind of connection we have. However, this is not such an 1915 about the kind of connection we have. However, this is not such an
1783 important issue. 1916 important issue.
1784 */ 1917 */
1785 1918
1786 static void 1919 static void
1787 unix_open_multicast_group (Lisp_Object name, Lisp_Object dest, Lisp_Object port, 1920 unix_open_multicast_group (Lisp_Object name, Lisp_Object dest,
1788 Lisp_Object ttl, void** vinfd, void** voutfd) 1921 Lisp_Object port, Lisp_Object ttl, void** vinfd,
1922 void** voutfd)
1789 { 1923 {
1790 struct ip_mreq imr; 1924 struct ip_mreq imr;
1791 struct sockaddr_in sa; 1925 struct sockaddr_in sa;
1792 struct protoent *udp; 1926 struct protoent *udp;
1793 int ws, rs; 1927 int ws, rs;
1804 1938
1805 CHECK_NATNUM (ttl); 1939 CHECK_NATNUM (ttl);
1806 thettl = (unsigned char) XINT (ttl); 1940 thettl = (unsigned char) XINT (ttl);
1807 1941
1808 if ((udp = getprotobyname ("udp")) == NULL) 1942 if ((udp = getprotobyname ("udp")) == NULL)
1809 error ("No info available for UDP protocol"); 1943 type_error (Qinvalid_operation, "No info available for UDP protocol");
1810 1944
1811 /* Init the sockets. Yes, I need 2 sockets. I couldn't duplicate one. */ 1945 /* Init the sockets. Yes, I need 2 sockets. I couldn't duplicate one. */
1812 if ((rs = socket (PF_INET, SOCK_DGRAM, udp->p_proto)) < 0) 1946 if ((rs = socket (PF_INET, SOCK_DGRAM, udp->p_proto)) < 0)
1813 report_file_error ("error creating socket", list1(name)); 1947 report_file_error ("error creating socket", list1(name));
1814 if ((ws = socket (PF_INET, SOCK_DGRAM, udp->p_proto)) < 0) 1948 if ((ws = socket (PF_INET, SOCK_DGRAM, udp->p_proto)) < 0)
1842 1976
1843 /* join multicast group */ 1977 /* join multicast group */
1844 imr.imr_multiaddr.s_addr = htonl (inet_addr ((char *) XSTRING_DATA (dest))); 1978 imr.imr_multiaddr.s_addr = htonl (inet_addr ((char *) XSTRING_DATA (dest)));
1845 imr.imr_interface.s_addr = htonl (INADDR_ANY); 1979 imr.imr_interface.s_addr = htonl (INADDR_ANY);
1846 if (setsockopt (rs, IPPROTO_IP, IP_ADD_MEMBERSHIP, 1980 if (setsockopt (rs, IPPROTO_IP, IP_ADD_MEMBERSHIP,
1847 (char *) &imr, sizeof (struct ip_mreq)) < 0) 1981 &imr, sizeof (struct ip_mreq)) < 0)
1848 { 1982 {
1849 close (ws); 1983 close (ws);
1850 close (rs); 1984 close (rs);
1851 report_file_error ("error adding membership", list2(name, dest)); 1985 report_file_error ("error adding membership", list2(name, dest));
1852 } 1986 }
1908 2042
1909 speed_up_interrupts (); 2043 speed_up_interrupts ();
1910 2044
1911 /* scope */ 2045 /* scope */
1912 if (setsockopt (ws, IPPROTO_IP, IP_MULTICAST_TTL, 2046 if (setsockopt (ws, IPPROTO_IP, IP_MULTICAST_TTL,
1913 (char *) &thettl, sizeof (thettl)) < 0) 2047 &thettl, sizeof (thettl)) < 0)
1914 { 2048 {
1915 close (rs); 2049 close (rs);
1916 close (ws); 2050 close (ws);
1917 report_file_error ("error setting ttl", list2(name, ttl)); 2051 report_file_error ("error setting ttl", list2(name, ttl));
1918 } 2052 }