I have a child process which runs in a pseudo terminal. The parent process does not run as root, but the child process does, through su or sudo. Because of this it is not possible to send a signal to the child process to force it to exit. I want to force it to exit by one of these means:
How do I do either of these? I already have a pty master fd, and I've tried something like this:
write(master, &termios.c_cc[VINTR], 1)
but it doesn't do anything.
It seems to me that if you truly have a pty (unless you mean something else by pseudo terminal), that all you have to do is send a Control-C to that FD. As evidence of this, I submit the following code in Python (but fairly close to the C required to do it):
import pty, os, sys, time
pid, fd = pty.fork()
if pid == 0:
os.execv('/bin/sh', ['/bin/sh', '-c',
'while true; do date; sleep 1; done'])
sys.exit(0)
time.sleep(3)
os.write(fd, '^C')
print 'results:', os.read(fd, 1024)
This forks a process under a pty, which runs an infinite loop printing the date. Then the parent waits 3 seconds and sends a control-C.
This results in the following output:
guin:/tmp$ time python /tmp/foo
results: Fri Feb 5 08:28:09 MST 2010
Fri Feb 5 08:28:10 MST 2010
Fri Feb 5 08:28:11 MST 2010
python /tmp/foo 0.02s user 0.01s system 1% cpu 3.042 total
guin:/tmp$
It ran just over 3 seconds, printed out the date 3 times, and exited.
I eventually went with the following solution:
After forking, instead of exec'ing sudo immediately, I exec() a helper child process instead, which in turn forks and execs sudo and calls waitpid on it. So the process hierarchy looks like this:
original process <---- runs as user
|
+-- helper process <---- runs as user, session leader,
| has own pty, in pty's foreground process group
|
+--- sudo <---- runs as root
By killing the helper process, the pty does not have a foreground process anymore. This will cause the OS to send SIGHUP to the entire foreground process group, regardless of the user, so sudo is SIGHUP'ed too.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With