ioctl TIOCSCTTY errno -1

V

Vivek.M

Help! Aid! Assist! I get:
background 4 -1 16931
System returned -1
This is on a OpenBSD grex.cyberspace.org 3.8 GENERIC#0 i386. I ssh to
the box. I am trying to write a program that grabs "tell messages" to
my terminal /dev/ttypE (pseudo-terminal). /dev/tty should point to my
controlling terminal.

Why is ioctl failing? Why! Oh! Why! I'm rather new to Perl and Unix so
please tolerate my idiocy.


#!/usr/bin/perl
use POSIX;

open LOG, '>>/c/v/i/vivekm1234/log' or die "Error log file\n";
open TTY, "/dev/tty" or die "error opening tty\n";

defined(my $pid = fork) or die "Can't fork: $!";
exit if $pid;
sleep 5;

setsid or die "Can't start a new session: $!";

### DEBUG ###
$fileno = fileno(TTY);
$tpgrp = tcgetpgrp(fileno(TTY));
$pgrp = getpgrp();

if ($tpgrp == $pgrp) {
print "foreground $tpgrp $pgrp\n";
} else {
print "background $fileno $tpgrp $pgrp\n";
}
### END DEBUG ###


require '/usr/libdata/perl5/site_perl/i386-openbsd/sys/ioctl.ph';
$getp = &TIOCSCTTY or die "NO TIOCSCTTY";

my $null;
$retval=ioctl(TTY, $getp, $null) || -1;

printf "System returned %d\n", $retval;

close LOG;
close TTY;
 
X

xhoster

Vivek.M said:
Help! Aid! Assist! I get:
background 4 -1 16931
System returned -1
This is on a OpenBSD grex.cyberspace.org 3.8 GENERIC#0 i386. I ssh to
the box. I am trying to write a program that grabs "tell messages" to
my terminal /dev/ttypE (pseudo-terminal). /dev/tty should point to my
controlling terminal.

Why is ioctl failing? Why! Oh! Why! I'm rather new to Perl and Unix so
please tolerate my idiocy.

Did you try looking in '$!' ?

Xho
 
V

Vivek.M

Did you try looking in '$!' ?

Xho
Yup. "Input/output error" when i did a print $!. I suspect that this:
$retval=ioctl(TTY, $getp, $null) || -1; is more accurate since it
gives me the UNIX system error.
 
X

xhoster

Vivek.M said:
Yup. "Input/output error" when i did a print $!. I suspect that this:
$retval=ioctl(TTY, $getp, $null) || -1; is more accurate since it
gives me the UNIX system error.

According to man ioctl, all it does on error is give you -1 and tells you
to go look in errno (which in Perl, means to look in $!). So it looks like
Perl is giving you all the info that the system is giving it.

You probably have a systems problem rather than a Perl problem.

The first thing I'd do to debug it would either be strace -f the perl, or
to re-write your example code into the analogous C code, which shouldn't be
too hard, and see what happens there.

Xho
 
V

Vivek.M

The first thing I'd do to debug it would either be strace -f the perl, or
to re-write your example code into the analogous C code, which shouldn't be
too hard, and see what happens there.
Yup, you were right :( :) I did it in C and i get Input/Output error.
Error no 5 in errno.h. So what do i do now? What the heck is a IO
error on /dev/tty imply? I've googled and can't seem to figure it out!


#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>

int main(void) {

pid_t pid;
int fd;

fd = open("/dev/tty",O_RDWR, 0);
if(fd == -1)
fprintf(stderr,"can't acquire terminal");

if( (pid = fork() ) < 0) {
fprintf(stderr, "Unable to fork process \n");
}

if(pid == 0) {
sleep(5);
if(setsid() < 0)
fprintf(stderr, "setsid error \n");

if( (ioctl(fd, TIOCSCTTY, NULL)) < 0)
printf("Error number: %d\n", errno);
}

return 0;
 
V

Vivek.M

Okay figured it out but am not any closer to getting this to work :).
when i call setsid i create a new session with the child being session
leader of this new session unfortunately it looses it's old
controlling terminal association which is why ioctl bombs. The old
session still exists and still retains control over /dev/tty *sigh*

So what i need to do is kill off the old session and then call ioctl.
 
X

xhoster

Vivek.M said:
Yup, you were right :( :) I did it in C and i get Input/Output error.
Error no 5 in errno.h. So what do i do now? What the heck is a IO
error on /dev/tty imply? I've googled and can't seem to figure it out!

I don't even get an IO error with your C code, I get Operation not
permitted error (even in the parent), so presumably this is a highly system
depend thing.

Sorry I can't be more help, but once we isolate the problem to something
not under Perl's control and not reproducible on my system, I lose interest
in pursuing it further...But good luck.

Cheers,

Xho
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,270
Latest member
TopCryptoTwitterChannels_

Latest Threads

Top