Main Page | Report this Page
Linux Forum Index  »  Linux Development - Applications  »  how to get which terminal?...
Page 1 of 1    

how to get which terminal?...

Author Message
zix...
Posted: Fri Oct 09, 2009 3:34 am
Guest
Hi,
I have a program. Now, I have to figure out whether its been run
through serial console or through ssh, specifically serial? Is there
any API that I can use in linux?

Thanks and Regards,
Sudip
 
Rainer Weikusat...
Posted: Fri Oct 09, 2009 8:50 am
Guest
zix <zixenus at (no spam) gmail.com> writes:
Quote:
I have a program. Now, I have to figure out whether its been run
through serial console or through ssh, specifically serial? Is there
any API that I can use in linux?

You could use the 'tty' program (GNU coreutils). For a ssh-connection
(or anything 'going over the net'), the output will look like this:

[rw at (no spam) void]~ $tty
/dev/pts/2

(this indicates that 'stdin' is using a so-called 'pseudo terminal)

For a serial connection, it will look like this:

[rw at (no spam) void]~ $tty
/dev/ttyS0

As can be determined with the help of strace, the program works by
reading the /proc/self/fd/0 'virtual symlink' which yields the
pathname of the file this file descriptor refers to. This could be
extended to deal with other scenarios, eg, if stdin is connected
straight to a socket without a pty, the symlink will look like this:

lrwx------ 1 0 0 64 Oct 9 14:48 /proc/self/fd/0 -> socket:[45819]
 
Josef Moellers...
Posted: Mon Oct 12, 2009 1:22 am
Guest
Rainer Weikusat wrote:
Quote:
zix <zixenus at (no spam) gmail.com> writes:
I have a program. Now, I have to figure out whether its been run
through serial console or through ssh, specifically serial? Is there
any API that I can use in linux?

You could use the 'tty' program (GNU coreutils). For a ssh-connection
(or anything 'going over the net'), the output will look like this:

[rw at (no spam) void]~ $tty
/dev/pts/2

(this indicates that 'stdin' is using a so-called 'pseudo terminal)

Which is also used on a graphic desktop.

A number of enviroment variables have SSH in their name (SSH_CLIENT,
SSH_TTY, SSH_ASKPASS, SSH_CONNECTION, depending upon your system). This
may be used to determine whether it's a remote connection, but they can
easily be set/reset to fool the program, so I'd use that only if the
distinction is to the benefit of the user (i.e. if he fakes a local
connection, he'll lose some important functions).

However, if you run "who am i", it will tell you that you're connected
through the network rather than directly, so the OP could find out how
"who" does it and use that (strace shows that who reads /var/run/utmp).
It would require more to fake that.

Quote:
As can be determined with the help of strace, the program works by
reading the /proc/self/fd/0 'virtual symlink' which yields the
pathname of the file this file descriptor refers to. This could be
extended to deal with other scenarios, eg, if stdin is connected
straight to a socket without a pty, the symlink will look like this:

lrwx------ 1 0 0 64 Oct 9 14:48 /proc/self/fd/0 -> socket:[45819]

If you connected your shell directly to a socket, none of the tty ioctls
and tty related functions (tcgetattr(tcsetattr) will work.
So it won't help in determining whether you're local (graphical screen)
or remote (ssh):

josef at (no spam) bounty:~$ ls -l /proc/self/fd/0
lrwx------ 1 josef josef 64 2009-10-12 09:14 /proc/self/fd/0 -> /dev/pts/2
josef at (no spam) bounty:~$ ssh r200a
[josef at (no spam) r200a ~]$ ls -l /proc/self/fd/0
lrwx------ 1 josef josef 64 Oct 12 09:27 /proc/self/fd/0 -> /dev/pts/5

Where's the difference?

Josef
--
These are my personal views and not those of Fujitsu Technology Solutions!
Josef Möllers (Pinguinpfleger bei FTS)
If failure had no penalty success would not be a prize (T. Pratchett)
Company Details: http://de.ts.fujitsu.com/imprint.html
 
Joe Beanfish...
Posted: Mon Oct 12, 2009 10:57 am
Guest
zix wrote:
Quote:
Hi,
I have a program. Now, I have to figure out whether its been run
through serial console or through ssh, specifically serial? Is there
any API that I can use in linux?

Out of curiosity why do you care?
If it's a speed issue you might look at the connection speed with stty
command or related ioctl()s. Network connections typically have highest
speed set. Serial may not, but no guarantee.
Maybe try an ioctl() that only makes sense for serial and assume IP if
you get an error?
 
Rainer Weikusat...
Posted: Mon Oct 12, 2009 11:22 am
Guest
Josef Moellers <josef.moellers at (no spam) ts.fujitsu.com> writes:
Quote:
Rainer Weikusat wrote:
zix <zixenus at (no spam) gmail.com> writes:
I have a program. Now, I have to figure out whether its been run
through serial console or through ssh, specifically serial? Is there
any API that I can use in linux?

You could use the 'tty' program (GNU coreutils). For a ssh-connection
(or anything 'going over the net'), the output will look like this:

[rw at (no spam) void]~ $tty /dev/pts/2

(this indicates that 'stdin' is using a so-called 'pseudo terminal)

Which is also used on a graphic desktop.

I don't believe the OP has 'a graphic desktop' running in his device
environment :->. Pseudo-TTYs are used whenever 'tty functionality'
needs to be emulated for a 'transport connection' which isn't a tty and
which would, because of this, otherwise not have any 'terminal driver'
support from the kernel. I considered this to be too obvious to
specifically mention it.

[...]

Quote:
As can be determined with the help of strace, the program works by
reading the /proc/self/fd/0 'virtual symlink' which yields the
pathname of the file this file descriptor refers to. This could be
extended to deal with other scenarios, eg, if stdin is connected
straight to a socket without a pty, the symlink will look like this:

lrwx------ 1 0 0 64 Oct 9 14:48 /proc/self/fd/0 -> socket:[45819]

If you connected your shell directly to a socket, none of the tty
ioctls and tty related functions (tcgetattr(tcsetattr) will work.
So it won't help in determining whether you're local (graphical
screen) or remote (ssh):

Well, obviously. For such a case, the most likely guess would be that
this is a non-interactive session.
 
 
Page 1 of 1    
All times are GMT - 5 Hours
The time now is Mon Nov 30, 2009 12:19 am