comparison src/process-unix.c @ 369:1d62742628b6 r21-1-14

Import from CVS: tag r21-1-14
author cvs
date Mon, 13 Aug 2007 11:01:51 +0200
parents 30d2cfa1092a
children cc15677e0335
comparison
equal deleted inserted replaced
368:397a7324211a 369:1d62742628b6
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 {
825 /* Disconnect the current controlling terminal, pursuant to 820 /* Disconnect the current controlling terminal, pursuant to
826 making the pty be the controlling terminal of the process. 821 making the pty be the controlling terminal of the process.
827 Also put us in our own process group. */ 822 Also put us in our own process group. */
828 823
829 disconnect_controlling_terminal (); 824 disconnect_controlling_terminal ();
830 825
826 #ifdef HAVE_PTYS
827 if (pty_flag)
828 {
831 /* Open the pty connection and make the pty's terminal 829 /* Open the pty connection and make the pty's terminal
832 our controlling terminal. 830 our controlling terminal.
833 831
834 On systems with TIOCSCTTY, we just use it to set 832 On systems with TIOCSCTTY, we just use it to set
835 the controlling terminal. On other systems, the 833 the controlling terminal. On other systems, the
1287 UNIX_DATA(p)->infd = -1; 1285 UNIX_DATA(p)->infd = -1;
1288 1286
1289 return usid; 1287 return usid;
1290 } 1288 }
1291 1289
1292 /* send a signal number SIGNO to PROCESS. 1290 /* If the subtty field of the process data is not filled in, do so now. */
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.
1293 CURRENT_GROUP means send to the process group that currently owns 1301 CURRENT_GROUP means send to the process group that currently owns
1294 the terminal being used to communicate with PROCESS. 1302 the terminal being used to communicate with PROCESS.
1295 This is used for various commands in shell mode. 1303 This is used for various commands in shell mode.
1296 If NOMSG is zero, insert signal-announcements into process's buffers 1304 If NOMSG is zero, insert signal-announcements into process's buffers
1297 right away. 1305 right away.
1298 1306
1299 If we can, we try to signal PROCESS by sending control characters 1307 If we can, we try to signal PROCESS by sending control characters
1300 down the pty. This allows us to signal inferiors who have changed 1308 down the pty. This allows us to signal inferiors who have changed
1301 their uid, for which killpg would return an EPERM error. 1309 their uid, for which killpg would return an EPERM error,
1302 1310 or processes running on other machines via remote login.
1303 The method signals an error if the given SIGNO is not valid 1311
1304 */ 1312 The method signals an error if the given SIGNO is not valid. */
1305 1313
1306 static void 1314 static void
1307 unix_kill_child_process (Lisp_Object proc, int signo, 1315 unix_kill_child_process (Lisp_Object proc, int signo,
1308 int current_group, int nomsg) 1316 int current_group, int nomsg)
1309 { 1317 {
1310 int gid; 1318 pid_t pgid = -1;
1311 int no_pgrp = 0;
1312 int kill_retval;
1313 struct Lisp_Process *p = XPROCESS (proc); 1319 struct Lisp_Process *p = XPROCESS (proc);
1314 1320 struct unix_process_data *d = UNIX_DATA (p);
1315 if (!UNIX_DATA(p)->pty_flag)
1316 current_group = 0;
1317
1318 /* If we are using pgrps, get a pgrp number and make it negative. */
1319 if (current_group)
1320 {
1321 #ifdef SIGNALS_VIA_CHARACTERS
1322 /* If possible, send signals to the entire pgrp
1323 by sending an input character to it. */
1324 {
1325 char sigchar = process_signal_char(UNIX_DATA(p)->subtty, signo);
1326 if (sigchar) {
1327 send_process (proc, Qnil, (Bufbyte *) &sigchar, 0, 1);
1328 return;
1329 }
1330 }
1331 #endif /* ! defined (SIGNALS_VIA_CHARACTERS) */
1332
1333 #ifdef TIOCGPGRP
1334 /* Get the pgrp using the tty itself, if we have that.
1335 Otherwise, use the pty to get the pgrp.
1336 On pfa systems, saka@pfu.fujitsu.co.JP writes:
1337 "TIOCGPGRP symbol defined in sys/ioctl.h at E50.
1338 But, TIOCGPGRP does not work on E50 ;-P works fine on E60"
1339 His patch indicates that if TIOCGPGRP returns an error, then
1340 we should just assume that p->pid is also the process group id. */
1341 {
1342 int err;
1343
1344 err = ioctl ( (UNIX_DATA(p)->subtty != -1
1345 ? UNIX_DATA(p)->subtty
1346 : UNIX_DATA(p)->infd), TIOCGPGRP, &gid);
1347
1348 #ifdef pfa
1349 if (err == -1)
1350 gid = - XINT (p->pid);
1351 #endif /* ! defined (pfa) */
1352 }
1353 if (gid == -1)
1354 no_pgrp = 1;
1355 else
1356 gid = - gid;
1357 #else /* ! defined (TIOCGPGRP ) */
1358 /* Can't select pgrps on this system, so we know that
1359 the child itself heads the pgrp. */
1360 gid = - XINT (p->pid);
1361 #endif /* ! defined (TIOCGPGRP ) */
1362 }
1363 else
1364 gid = - XINT (p->pid);
1365 1321
1366 switch (signo) 1322 switch (signo)
1367 { 1323 {
1368 #ifdef SIGCONT 1324 #ifdef SIGCONT
1369 case SIGCONT: 1325 case SIGCONT:
1376 break; 1332 break;
1377 #endif /* ! defined (SIGCONT) */ 1333 #endif /* ! defined (SIGCONT) */
1378 case SIGINT: 1334 case SIGINT:
1379 case SIGQUIT: 1335 case SIGQUIT:
1380 case SIGKILL: 1336 case SIGKILL:
1381 flush_pending_output (UNIX_DATA(p)->infd); 1337 flush_pending_output (d->infd);
1382 break; 1338 break;
1383 } 1339 }
1384 1340
1385 /* If we don't have process groups, send the signal to the immediate 1341 if (! d->pty_flag)
1386 subprocess. That isn't really right, but it's better than any 1342 current_group = 0;
1387 obvious alternative. */ 1343
1388 if (no_pgrp) 1344 /* If current_group is true, we want to send a signal to the
1389 { 1345 foreground process group of the terminal our child process is
1390 kill_retval = kill (XINT (p->pid), signo) ? errno : 0; 1346 running on. You would think that would be easy.
1391 } 1347
1392 else 1348 The BSD people invented the TIOCPGRP ioctl to get the foreground
1393 { 1349 process group of a tty. That, combined with killpg, gives us
1394 /* gid may be a pid, or minus a pgrp's number */ 1350 what we want.
1395 #if defined (TIOCSIGNAL) || defined (TIOCSIGSEND) 1351
1396 if (current_group) 1352 However, the POSIX standards people, in their infinite wisdom,
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)
1397 { 1388 {
1398 #ifdef TIOCSIGNAL 1389 send_process (proc, Qnil, (Bufbyte *) &sigchar, 0, 1);
1399 kill_retval = ioctl (UNIX_DATA(p)->infd, TIOCSIGNAL, signo); 1390 return;
1400 #else /* ! defined (TIOCSIGNAL) */
1401 kill_retval = ioctl (UNIX_DATA(p)->infd, TIOCSIGSEND, signo);
1402 #endif /* ! defined (TIOCSIGNAL) */
1403 } 1391 }
1404 else 1392 }
1405 kill_retval = kill (- XINT (p->pid), signo) ? errno : 0; 1393 #endif /* SIGNALS_VIA_CHARACTERS */
1406 #else /* ! (defined (TIOCSIGNAL) || defined (TIOCSIGSEND)) */ 1394
1407 kill_retval = EMACS_KILLPG (-gid, signo) ? errno : 0; 1395 #ifdef TIOCGPGRP
1408 #endif /* ! (defined (TIOCSIGNAL) || defined (TIOCSIGSEND)) */ 1396 if (pgid == -1)
1409 } 1397 ioctl (d->infd, TIOCGPGRP, &pgid); /* BSD */
1410 1398 if (pgid == -1 && d->subtty != -1)
1411 if (kill_retval < 0 && errno == EINVAL) 1399 ioctl (d->subtty, TIOCGPGRP, &pgid); /* Only this works on AIX! */
1412 error ("Signal number %d is invalid for this system", signo); 1400 #endif /* TIOCGPGRP */
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));
1413 } 1431 }
1414 1432
1415 /* 1433 /*
1416 * Kill any process in the system given its PID. 1434 * Kill any process in the system given its PID.
1417 * 1435 *