Mercurial > hg > xemacs-beta
comparison src/process-unix.c @ 371:cc15677e0335 r21-2b1
Import from CVS: tag r21-2b1
author | cvs |
---|---|
date | Mon, 13 Aug 2007 11:03:08 +0200 |
parents | 1d62742628b6 |
children | 6240c7796c7a |
comparison
equal
deleted
inserted
replaced
370:bd866891f083 | 371:cc15677e0335 |
---|---|
733 | 733 |
734 inchannel = outchannel = forkin = forkout = -1; | 734 inchannel = outchannel = forkin = forkout = -1; |
735 | 735 |
736 /* Nothing below here GCs so our string pointers shouldn't move. */ | 736 /* Nothing below here GCs so our string pointers shouldn't move. */ |
737 new_argv = alloca_array (char *, nargv + 2); | 737 new_argv = alloca_array (char *, nargv + 2); |
738 GET_C_STRING_FILENAME_DATA_ALLOCA (program, new_argv[0]); | 738 new_argv[0] = (char *) XSTRING_DATA (program); |
739 for (i = 0; i < nargv; i++) | 739 for (i = 0; i < nargv; i++) |
740 { | 740 { |
741 Lisp_Object tem = argv[i]; | 741 Lisp_Object tem = argv[i]; |
742 CHECK_STRING (tem); | 742 CHECK_STRING (tem); |
743 new_argv[i + 1] = (char *) XSTRING_DATA (tem); | 743 new_argv[i + 1] = (char *) XSTRING_DATA (tem); |
744 } | 744 } |
745 new_argv[i + 1] = 0; | 745 new_argv[i + 1] = 0; |
746 GET_C_STRING_FILENAME_DATA_ALLOCA (cur_dir, current_dir); | 746 current_dir = (char *) XSTRING_DATA (cur_dir); |
747 | 747 |
748 #ifdef HAVE_PTYS | 748 #ifdef HAVE_PTYS |
749 if (!NILP (Vprocess_connection_type)) | 749 if (!NILP (Vprocess_connection_type)) |
750 { | 750 { |
751 /* find a new pty, open the master side, return the opened | 751 /* find a new pty, open the master side, return the opened |
815 { | 815 { |
816 /**** Now we're in the child process ****/ | 816 /**** Now we're in the child process ****/ |
817 int xforkin = forkin; | 817 int xforkin = forkin; |
818 int xforkout = forkout; | 818 int xforkout = forkout; |
819 | 819 |
820 if (!pty_flag) | |
821 EMACS_SEPARATE_PROCESS_GROUP (); | |
822 #ifdef HAVE_PTYS | |
823 else | |
824 { | |
820 /* Disconnect the current controlling terminal, pursuant to | 825 /* Disconnect the current controlling terminal, pursuant to |
821 making the pty be the controlling terminal of the process. | 826 making the pty be the controlling terminal of the process. |
822 Also put us in our own process group. */ | 827 Also put us in our own process group. */ |
823 | 828 |
824 disconnect_controlling_terminal (); | 829 disconnect_controlling_terminal (); |
825 | 830 |
826 #ifdef HAVE_PTYS | |
827 if (pty_flag) | |
828 { | |
829 /* Open the pty connection and make the pty's terminal | 831 /* Open the pty connection and make the pty's terminal |
830 our controlling terminal. | 832 our controlling terminal. |
831 | 833 |
832 On systems with TIOCSCTTY, we just use it to set | 834 On systems with TIOCSCTTY, we just use it to set |
833 the controlling terminal. On other systems, the | 835 the controlling terminal. On other systems, the |
916 { | 918 { |
917 int piddly = EMACS_GET_PROCESS_GROUP (); | 919 int piddly = EMACS_GET_PROCESS_GROUP (); |
918 EMACS_SET_TTY_PROCESS_GROUP (xforkin, &piddly); | 920 EMACS_SET_TTY_PROCESS_GROUP (xforkin, &piddly); |
919 } | 921 } |
920 | 922 |
923 # ifdef AIX | |
921 /* On AIX, we've disabled SIGHUP above once we start a | 924 /* On AIX, we've disabled SIGHUP above once we start a |
922 child on a pty. Now reenable it in the child, so it | 925 child on a pty. Now reenable it in the child, so it |
923 will die when we want it to. | 926 will die when we want it to. */ |
924 JV: This needs to be done ALWAYS as we might have inherited | |
925 a SIG_IGN handling from our parent (nohup) and we are in new | |
926 process group. | |
927 */ | |
928 signal (SIGHUP, SIG_DFL); | 927 signal (SIGHUP, SIG_DFL); |
928 # endif /* AIX */ | |
929 } | 929 } |
930 #endif /* HAVE_PTYS */ | 930 #endif /* HAVE_PTYS */ |
931 | 931 |
932 signal (SIGINT, SIG_DFL); | 932 signal (SIGINT, SIG_DFL); |
933 signal (SIGQUIT, SIG_DFL); | 933 signal (SIGQUIT, SIG_DFL); |
950 #endif | 950 #endif |
951 } | 951 } |
952 | 952 |
953 if (pid < 0) | 953 if (pid < 0) |
954 { | 954 { |
955 int save_errno = errno; | |
956 close_descriptor_pair (forkin, forkout); | 955 close_descriptor_pair (forkin, forkout); |
957 errno = save_errno; | |
958 report_file_error ("Doing fork", Qnil); | 956 report_file_error ("Doing fork", Qnil); |
959 } | 957 } |
960 | 958 |
961 /* #### dmoore - why is this commented out, otherwise we leave | 959 /* #### dmoore - why is this commented out, otherwise we leave |
962 subtty = forkin, but then we close forkin just below. */ | 960 subtty = forkin, but then we close forkin just below. */ |
1151 /* Use volatile to protect variables from being clobbered by longjmp. */ | 1149 /* Use volatile to protect variables from being clobbered by longjmp. */ |
1152 SIGTYPE (*volatile old_sigpipe) (int) = 0; | 1150 SIGTYPE (*volatile old_sigpipe) (int) = 0; |
1153 volatile Lisp_Object vol_proc = proc; | 1151 volatile Lisp_Object vol_proc = proc; |
1154 struct Lisp_Process *volatile p = XPROCESS (proc); | 1152 struct Lisp_Process *volatile p = XPROCESS (proc); |
1155 | 1153 |
1156 /* #### JV: layering violation? | |
1157 | |
1158 This function knows too much about the relation between the encodingstream | |
1159 (DATA_OUTSTREAM) and te actual output stream p->output_stream. | |
1160 | |
1161 If encoding streams properly forwarded all calls, we could simply | |
1162 use DATA_OUTSTREAM everywhere. */ | |
1163 | |
1164 if (!SETJMP (send_process_frame)) | 1154 if (!SETJMP (send_process_frame)) |
1165 { | 1155 { |
1166 /* use a reasonable-sized buffer (somewhere around the size of the | 1156 /* use a reasonable-sized buffer (somewhere around the size of the |
1167 stream buffer) so as to avoid inundating the stream with blocked | 1157 stream buffer) so as to avoid inundating the stream with blocked |
1168 data. */ | 1158 data. */ |
1194 { | 1184 { |
1195 /* Buffer is full. Wait, accepting input; | 1185 /* Buffer is full. Wait, accepting input; |
1196 that may allow the program | 1186 that may allow the program |
1197 to finish doing output and read more. */ | 1187 to finish doing output and read more. */ |
1198 Faccept_process_output (Qnil, make_int (1), Qnil); | 1188 Faccept_process_output (Qnil, make_int (1), Qnil); |
1199 /* It could have *really* finished, deleting the process */ | |
1200 if (NILP(p->pipe_outstream)) | |
1201 return; | |
1202 old_sigpipe = | 1189 old_sigpipe = |
1203 (SIGTYPE (*) (int)) signal (SIGPIPE, send_process_trap); | 1190 (SIGTYPE (*) (int)) signal (SIGPIPE, send_process_trap); |
1204 Lstream_flush (XLSTREAM (p->pipe_outstream)); | 1191 Lstream_flush (XLSTREAM (p->pipe_outstream)); |
1205 signal (SIGPIPE, old_sigpipe); | 1192 signal (SIGPIPE, old_sigpipe); |
1206 } | 1193 } |
1207 } | 1194 } |
1208 } | 1195 } |
1209 else | 1196 else |
1210 { /* We got here from a longjmp() from the SIGPIPE handler */ | 1197 { /* We got here from a longjmp() from the SIGPIPE handler */ |
1211 signal (SIGPIPE, old_sigpipe); | 1198 signal (SIGPIPE, old_sigpipe); |
1212 /* Close the file lstream so we don't attempt to write to it further */ | |
1213 /* #### There is controversy over whether this might cause fd leakage */ | |
1214 /* my tests say no. -slb */ | |
1215 XLSTREAM (p->pipe_outstream)->flags &= ~LSTREAM_FL_IS_OPEN; | |
1216 p->status_symbol = Qexit; | 1199 p->status_symbol = Qexit; |
1217 p->exit_code = 256; /* #### SIGPIPE ??? */ | 1200 p->exit_code = 256; /* #### SIGPIPE ??? */ |
1218 p->core_dumped = 0; | 1201 p->core_dumped = 0; |
1219 p->tick++; | 1202 p->tick++; |
1220 process_tick++; | 1203 process_tick++; |
1285 UNIX_DATA(p)->infd = -1; | 1268 UNIX_DATA(p)->infd = -1; |
1286 | 1269 |
1287 return usid; | 1270 return usid; |
1288 } | 1271 } |
1289 | 1272 |
1290 /* If the subtty field of the process data is not filled in, do so now. */ | 1273 /* send a signal number SIGNO to PROCESS. |
1291 static void | |
1292 try_to_initialize_subtty (struct unix_process_data *upd) | |
1293 { | |
1294 if (upd->pty_flag | |
1295 && (upd->subtty == -1 || ! isatty (upd->subtty)) | |
1296 && STRINGP (upd->tty_name)) | |
1297 upd->subtty = open ((char *) XSTRING_DATA (upd->tty_name), O_RDWR, 0); | |
1298 } | |
1299 | |
1300 /* Send signal number SIGNO to PROCESS. | |
1301 CURRENT_GROUP means send to the process group that currently owns | 1274 CURRENT_GROUP means send to the process group that currently owns |
1302 the terminal being used to communicate with PROCESS. | 1275 the terminal being used to communicate with PROCESS. |
1303 This is used for various commands in shell mode. | 1276 This is used for various commands in shell mode. |
1304 If NOMSG is zero, insert signal-announcements into process's buffers | 1277 If NOMSG is zero, insert signal-announcements into process's buffers |
1305 right away. | 1278 right away. |
1306 | 1279 |
1307 If we can, we try to signal PROCESS by sending control characters | 1280 If we can, we try to signal PROCESS by sending control characters |
1308 down the pty. This allows us to signal inferiors who have changed | 1281 down the pty. This allows us to signal inferiors who have changed |
1309 their uid, for which killpg would return an EPERM error, | 1282 their uid, for which killpg would return an EPERM error. |
1310 or processes running on other machines via remote login. | 1283 |
1311 | 1284 The method signals an error if the given SIGNO is not valid |
1312 The method signals an error if the given SIGNO is not valid. */ | 1285 */ |
1313 | 1286 |
1314 static void | 1287 static void |
1315 unix_kill_child_process (Lisp_Object proc, int signo, | 1288 unix_kill_child_process (Lisp_Object proc, int signo, |
1316 int current_group, int nomsg) | 1289 int current_group, int nomsg) |
1317 { | 1290 { |
1318 pid_t pgid = -1; | 1291 int gid; |
1292 int no_pgrp = 0; | |
1293 int kill_retval; | |
1319 struct Lisp_Process *p = XPROCESS (proc); | 1294 struct Lisp_Process *p = XPROCESS (proc); |
1320 struct unix_process_data *d = UNIX_DATA (p); | 1295 |
1296 if (!UNIX_DATA(p)->pty_flag) | |
1297 current_group = 0; | |
1298 | |
1299 /* If we are using pgrps, get a pgrp number and make it negative. */ | |
1300 if (current_group) | |
1301 { | |
1302 #ifdef SIGNALS_VIA_CHARACTERS | |
1303 /* If possible, send signals to the entire pgrp | |
1304 by sending an input character to it. */ | |
1305 { | |
1306 char sigchar = process_signal_char(UNIX_DATA(p)->subtty, signo); | |
1307 if (sigchar) { | |
1308 send_process (proc, Qnil, (Bufbyte *) &sigchar, 0, 1); | |
1309 return; | |
1310 } | |
1311 } | |
1312 #endif /* ! defined (SIGNALS_VIA_CHARACTERS) */ | |
1313 | |
1314 #ifdef TIOCGPGRP | |
1315 /* Get the pgrp using the tty itself, if we have that. | |
1316 Otherwise, use the pty to get the pgrp. | |
1317 On pfa systems, saka@pfu.fujitsu.co.JP writes: | |
1318 "TIOCGPGRP symbol defined in sys/ioctl.h at E50. | |
1319 But, TIOCGPGRP does not work on E50 ;-P works fine on E60" | |
1320 His patch indicates that if TIOCGPGRP returns an error, then | |
1321 we should just assume that p->pid is also the process group id. */ | |
1322 { | |
1323 int err; | |
1324 | |
1325 err = ioctl ( (UNIX_DATA(p)->subtty != -1 | |
1326 ? UNIX_DATA(p)->subtty | |
1327 : UNIX_DATA(p)->infd), TIOCGPGRP, &gid); | |
1328 | |
1329 #ifdef pfa | |
1330 if (err == -1) | |
1331 gid = - XINT (p->pid); | |
1332 #endif /* ! defined (pfa) */ | |
1333 } | |
1334 if (gid == -1) | |
1335 no_pgrp = 1; | |
1336 else | |
1337 gid = - gid; | |
1338 #else /* ! defined (TIOCGPGRP ) */ | |
1339 /* Can't select pgrps on this system, so we know that | |
1340 the child itself heads the pgrp. */ | |
1341 gid = - XINT (p->pid); | |
1342 #endif /* ! defined (TIOCGPGRP ) */ | |
1343 } | |
1344 else | |
1345 gid = - XINT (p->pid); | |
1321 | 1346 |
1322 switch (signo) | 1347 switch (signo) |
1323 { | 1348 { |
1324 #ifdef SIGCONT | 1349 #ifdef SIGCONT |
1325 case SIGCONT: | 1350 case SIGCONT: |
1332 break; | 1357 break; |
1333 #endif /* ! defined (SIGCONT) */ | 1358 #endif /* ! defined (SIGCONT) */ |
1334 case SIGINT: | 1359 case SIGINT: |
1335 case SIGQUIT: | 1360 case SIGQUIT: |
1336 case SIGKILL: | 1361 case SIGKILL: |
1337 flush_pending_output (d->infd); | 1362 flush_pending_output (UNIX_DATA(p)->infd); |
1338 break; | 1363 break; |
1339 } | 1364 } |
1340 | 1365 |
1341 if (! d->pty_flag) | 1366 /* If we don't have process groups, send the signal to the immediate |
1342 current_group = 0; | 1367 subprocess. That isn't really right, but it's better than any |
1343 | 1368 obvious alternative. */ |
1344 /* If current_group is true, we want to send a signal to the | 1369 if (no_pgrp) |
1345 foreground process group of the terminal our child process is | 1370 { |
1346 running on. You would think that would be easy. | 1371 kill_retval = kill (XINT (p->pid), signo) ? errno : 0; |
1347 | 1372 } |
1348 The BSD people invented the TIOCPGRP ioctl to get the foreground | 1373 else |
1349 process group of a tty. That, combined with killpg, gives us | 1374 { |
1350 what we want. | 1375 /* gid may be a pid, or minus a pgrp's number */ |
1351 | 1376 #if defined (TIOCSIGNAL) || defined (TIOCSIGSEND) |
1352 However, the POSIX standards people, in their infinite wisdom, | 1377 if (current_group) |
1353 have seen fit to only allow this for processes which have the | |
1354 terminal as controlling terminal, which doesn't apply to us. | |
1355 | |
1356 Sooo..., we have to do something non-standard. The ioctls | |
1357 TIOCSIGNAL, TIOCSIG, and TIOCSIGSEND send the signal directly on | |
1358 many systems. POSIX tcgetpgrp(), since it is *documented* as not | |
1359 doing what we want, is actually less likely to work than the BSD | |
1360 ioctl TIOCGPGRP it is supposed to obsolete. Sometimes we have to | |
1361 use TIOCGPGRP on the master end, sometimes the slave end | |
1362 (probably an AIX bug). So we better get a fd for the slave if we | |
1363 haven't got it yet. | |
1364 | |
1365 Anal operating systems like SGI Irix and Compaq Tru64 adhere | |
1366 strictly to the letter of the law, so our hack doesn't work. | |
1367 The following fragment from an Irix header file is suggestive: | |
1368 | |
1369 #ifdef __notdef__ | |
1370 // this is not currently supported | |
1371 #define TIOCSIGNAL (tIOC|31) // pty: send signal to slave | |
1372 #endif | |
1373 | |
1374 On those systems where none of our tricks work, we just fall back | |
1375 to the non-current_group behavior and kill the process group of | |
1376 the child. | |
1377 */ | |
1378 if (current_group) | |
1379 { | |
1380 try_to_initialize_subtty (d); | |
1381 | |
1382 #ifdef SIGNALS_VIA_CHARACTERS | |
1383 /* If possible, send signals to the entire pgrp | |
1384 by sending an input character to it. */ | |
1385 { | |
1386 char sigchar = process_signal_char (d->subtty, signo); | |
1387 if (sigchar) | |
1388 { | 1378 { |
1389 send_process (proc, Qnil, (Bufbyte *) &sigchar, 0, 1); | 1379 #ifdef TIOCSIGNAL |
1390 return; | 1380 kill_retval = ioctl (UNIX_DATA(p)->infd, TIOCSIGNAL, signo); |
1381 #else /* ! defined (TIOCSIGNAL) */ | |
1382 kill_retval = ioctl (UNIX_DATA(p)->infd, TIOCSIGSEND, signo); | |
1383 #endif /* ! defined (TIOCSIGNAL) */ | |
1391 } | 1384 } |
1392 } | 1385 else |
1393 #endif /* SIGNALS_VIA_CHARACTERS */ | 1386 kill_retval = kill (- XINT (p->pid), signo) ? errno : 0; |
1394 | 1387 #else /* ! (defined (TIOCSIGNAL) || defined (TIOCSIGSEND)) */ |
1395 #ifdef TIOCGPGRP | 1388 kill_retval = EMACS_KILLPG (-gid, signo) ? errno : 0; |
1396 if (pgid == -1) | 1389 #endif /* ! (defined (TIOCSIGNAL) || defined (TIOCSIGSEND)) */ |
1397 ioctl (d->infd, TIOCGPGRP, &pgid); /* BSD */ | 1390 } |
1398 if (pgid == -1 && d->subtty != -1) | 1391 |
1399 ioctl (d->subtty, TIOCGPGRP, &pgid); /* Only this works on AIX! */ | 1392 if (kill_retval < 0 && errno == EINVAL) |
1400 #endif /* TIOCGPGRP */ | 1393 error ("Signal number %d is invalid for this system", signo); |
1401 | |
1402 if (pgid == -1) | |
1403 { | |
1404 /* Many systems provide an ioctl to send a signal directly */ | |
1405 #ifdef TIOCSIGNAL /* Solaris, HP-UX */ | |
1406 if (ioctl (d->infd, TIOCSIGNAL, signo) != -1) | |
1407 return; | |
1408 #endif /* TIOCSIGNAL */ | |
1409 | |
1410 #ifdef TIOCSIG /* BSD */ | |
1411 if (ioctl (d->infd, TIOCSIG, signo) != -1) | |
1412 return; | |
1413 #endif /* TIOCSIG */ | |
1414 } | |
1415 } /* current_group */ | |
1416 | |
1417 if (pgid == -1) | |
1418 /* Either current_group is 0, or we failed to get the foreground | |
1419 process group using the trickery above. So we fall back to | |
1420 sending the signal to the process group of our child process. | |
1421 Since this is often a shell that ignores signals like SIGINT, | |
1422 the shell's subprocess is killed, which is the desired effect. | |
1423 The process group of p->pid is always p->pid, since it was | |
1424 created as a process group leader. */ | |
1425 pgid = XINT (p->pid); | |
1426 | |
1427 /* Finally send the signal. */ | |
1428 if (EMACS_KILLPG (pgid, signo) == -1) | |
1429 error ("kill (%ld, %ld) failed: %s", | |
1430 (long) pgid, (long) signo, strerror (errno)); | |
1431 } | 1394 } |
1432 | 1395 |
1433 /* | 1396 /* |
1434 * Kill any process in the system given its PID. | 1397 * Kill any process in the system given its PID. |
1435 * | 1398 * |