User avatar
mad-hatter
Posts: 419
Joined: Sun Feb 26, 2012 2:58 pm
Location: By the UK seaside

C/C++ Check if a program is being run locally or remotely

Fri Oct 18, 2013 10:49 pm

Hello,

'C' / 'C++'

I'm trying to find a way, for 'myProgram' to determine if its being run locally or from
a remote computer during run time, so I can run a different section of the program.

I tried :- 'netstat -a -n | grep ESTABLISHED'
but it only tells you if there is a connection.

I tried 'tty'.
From a remote terminal I get '/dev/pts/0'
From a local terminal I get '/dev/pts/1'

Would this be a reliable way to find out.

Any body have any comments or suggestions.

Regards

User avatar
topguy
Posts: 6491
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: C/C++ Check if a program is being run locally or remotel

Sat Oct 19, 2013 12:17 am

I havent tested this but was thinking that "pstree" which shows which process started another process could show if your program was started from a shell which was started by sshd.

User avatar
joan
Posts: 14935
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: C/C++ Check if a program is being run locally or remotel

Sat Oct 19, 2013 7:29 am

If you use ssh then the remote shell will have environment variables SSH_TTY, SSH_CLIENT, and SSH_CONNECTION.

Code: Select all

#include <stdlib.h>

if (getenv("SSH_CLIENT")) printf("running on remote machine"\n);
else                      printf("running locally\n");

User avatar
jojopi
Posts: 3268
Joined: Tue Oct 11, 2011 8:38 pm

Re: C/C++ Check if a program is being run locally or remotel

Sat Oct 19, 2013 8:21 am

mad-hatter wrote:I'm trying to find a way, for 'myProgram' to determine if its being run locally or from
a remote computer during run time, so I can run a different section of the program.
It is not generally a good idea for a program to work differently all of its own accord. What if the user is remote but really wants the local behaviour, or vice versa? If you are concerned that an option will not work in certain cases, then the best thing may be just to try it, and fall back to the alternative when it fails.

There are many different ways that an application could be run:
  • locally on the system console
  • on a serial line (which traditionally could be local or remote, but analogue modems are unlikely these days)
  • in a terminal emulator in a local X session
  • in a terminal emulator in a remote X session, connected using SSH
  • in a terminal emulator in a local X session, but the user is connected remotely by VNC
  • remotely over SSH, without X forwarding
  • automatically in the background via cron…
Without knowing how you want the behaviour to differ, and in which cases, it may be impossible to suggest the most reliable test.

MattOwnby
Posts: 58
Joined: Thu Aug 16, 2012 7:22 pm

Re: C/C++ Check if a program is being run locally or remotel

Sun Oct 20, 2013 5:29 am

mad-hatter wrote:Hello,

'C' / 'C++'

I'm trying to find a way, for 'myProgram' to determine if its being run locally or from
a remote computer during run time, so I can run a different section of the program.

I tried :- 'netstat -a -n | grep ESTABLISHED'
but it only tells you if there is a connection.

I tried 'tty'.
From a remote terminal I get '/dev/pts/0'
From a local terminal I get '/dev/pts/1'

Would this be a reliable way to find out.

Any body have any comments or suggestions.

Regards
I know that you really just want an answer to your question, so the following may be disappointing. Buuuut....

It's very good programming practice to make individual methods/classes/functions/etc be as abstract as possible, meaning that they have no concept of how they are used or by what/whom. This is known as "loose coupling" and the benefit is that when it comes time to maintain or change your program, you will have designed it in such a way that future changes are easy and fairly painless. When you design your software to expect (or even require) knowing how it is going to be used ("strong coupling") you are basically writing something that you will never be able to use again anywhere else and that no one after you is going to have a pleasant time maintaining.

In other words, in order to make your program be aware of whether it was run from a local console or a remote (ssh for example) console, you are coupling your program to linux, possibly a raspberry pi, and even more possibly, the current version of raspbian that exists today. This means that if raspbian changes in 6 months, your program may break and your hard work will be down the drain. Even if it never changes, writing software that can only run on a raspberry pi (or only on linux) kinda sucks if and when you have the desire to port it to another operating system.

So the conclusion is... ask yourself how knowing whether you are local or remote will help you, and then focus on the goal you are trying to achieve. There may be another way to achieve the goal.

User avatar
BluDragon
Posts: 83
Joined: Thu Sep 05, 2013 7:32 am
Location: 127.0.0.1

Re: C/C++ Check if a program is being run locally or remotel

Thu Oct 31, 2013 4:51 pm

if i understand what you mean, then you would use;

sudo netstat -p -n --tcp --udp
"Whenever a separation is made between liberty and justice, neither, in my opinion, is safe." - Edmund Burke
"Men make counterfeit money; in many more cases, money makes counterfeit men." - Sydney J. Harris

User avatar
mad-hatter
Posts: 419
Joined: Sun Feb 26, 2012 2:58 pm
Location: By the UK seaside

Re: C/C++ Check if a program is being run locally or remotel

Tue Nov 26, 2013 1:39 pm

Hello,

My apologies for ignoring the replies to this post for so long, I got involved in something that needed my attention.
BluDragon wrote:if i understand what you mean, then you would use;
sudo netstat -p -n --tcp --udp
This seems to do what I need.
Thanks to those that responded.

Regards

User avatar
jojopi
Posts: 3268
Joined: Tue Oct 11, 2011 8:38 pm

Re: C/C++ Check if a program is being run locally or remotel

Tue Nov 26, 2013 9:39 pm

mad-hatter wrote:This seems to do what I need.
I do not see how. "myProgram" will not itself have any network connections, even if it is running remotely. (If it is an X11 client it will have a socket connection, but that will probably be a local UNIX-domain socket, even if it is being tunnelled to a remote display over SSH.)

"netstat -p" will only tell you that various processes have network connections, not whether your application is indirectly using them. To work that out you would have to trace to process hierarchy. It is probably easier to trace using your controlling terminal instead. In bash, compare the outputs of "tty" and "w". In C, ttyname() and getutxent().

All of these techniques are rather involved, however. For many disambiguations of "being run locally or from a remote computer" there will be an environment variable that you test easily with getenv().

User avatar
mad-hatter
Posts: 419
Joined: Sun Feb 26, 2012 2:58 pm
Location: By the UK seaside

Re: C/C++ Check if a program is being run locally or remotel

Wed Nov 27, 2013 8:44 pm

Hello jojopi,

I think my use of the term remote is wrong and misleading,
I'm operating on a local network when I access 'myProgram'.

The inclusions show that there are differences in the output,
as its 'myProgram' that is calling 'netstat', I would be able to tell if it is being
accessed from outside the local Pi.

There is a problem (for me) with 'netstat' in that, I don't know how to stop it running
and still obtain its output. I'm using QT4 and QProcess.

Had a look at 'getenv()' and 'printenv' with and without tightvnc or rdp, and could not
see any differences in the output for 'printenv'.

If I can't get something straight forward sorted out, I'll use password control.

Regards

Code: Select all

From remote under tightvnc or from the Pi running tightvnc on remote

root@mypi:/# netstat -p -n -tcp -udp
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 192.168.yy.yy:5901      192.168.yy.zz:10966     ESTABLISHED 3210/Xtightvnc

Code: Select all

From remote under rdp or from the Pi when runing rdp on remote

root@mypi:/# netstat -p -n -tcp -udp
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 127.xx.xx.xx:5910       127.xx.xx.xx:45304      ESTABLISHED 3559/Xvnc       
tcp        0      0 127.xx.xx.ww:45304      127.xx.xx.xx:5910       ESTABLISHED 3128/xrdp       
tcp        0     85 192.168.yy.yy:3389      192.168.yy.zz:2159      ESTABLISHED 3128/xrdp       
tcp        0      0 127.xx.xx.xx:3350       127.xx.xx.xx:53450      TIME_WAIT   - 

Code: Select all

From pi, no vnc, no rdp running on remote

root@mypi:/# netstat -p -n -tcp -udp
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 127.xx.xx.xx:45304      127.xx.xx.xx:5910       TIME_WAIT   - 

User avatar
jojopi
Posts: 3268
Joined: Tue Oct 11, 2011 8:38 pm

Re: C/C++ Check if a program is being run locally or remotel

Thu Nov 28, 2013 12:04 am

Those outputs only show that a VNC or RDP session is running, and that someone is connected to it. There is nothing to indicate that your specific program belongs to the remote session and not to a local one. They could even be running as completely different users. Also you cannot in general trace the process hierarchy as I previously suggested, because some of your ancestor processes may no longer be alive.

If you are using QT, then perhaps your application is graphical and it requires an X11 server. If it is running on the first-started local X display, then $DISPLAY will normally be set to ":0.0". If it is running in an Xvnc or Xrdp session, or tunnelled over SSH, then $DISPLAY will usually be some higher number.

Of course there are corner-cases with this test, including multiple local displays, remote sessions connected locally, and non-default display numbers. But finding out which display you are connected to is definitely the correct first step for an X11 client.

Your mention of passwords makes it sound that you want to use the test as a security restriction. That does not seem advisable, especially if you are not sure exactly what test you need.

netstat exits after it prints its output, so I do not see how you could read the output and still leave it running. Perhaps you are seeing a zombie process slot because you have not yet checked its exit status?

User avatar
mad-hatter
Posts: 419
Joined: Sun Feb 26, 2012 2:58 pm
Location: By the UK seaside

Re: C/C++ Check if a program is being run locally or remotel

Fri Nov 29, 2013 9:34 am

Hello jojopi,

Thanks for your response.

I got 'netstat' running and sending data to 'myProg', by mistake I had put a '-c' in the flags. Yes, I agree it does not give me the information I need.

Yes it is a graphical app, I will look into finding out the display reference.
If it is running on the first-started local X display, then $DISPLAY will normally be set to ":0.0".
I will keep playing. I thought there might be an easy way.

Regards

frodo
Posts: 63
Joined: Wed Dec 18, 2013 2:36 pm

Re: C/C++ Check if a program is being run locally or remotel

Wed Dec 18, 2013 4:29 pm

You are trying to break major UNIX design principles with your programm. It's gonna hurt a lot and it won't be secure. ;)
You might find a better way in conformance with the systems security mechanisms if you
  • have a closer look at user groups and their role within the filesystem,
  • read the section Users and Groups in the Libc reference manual,
  • man 2 chroot
  • man 7 capabilities.
Without knowing what exactly it is you want to restrict it's impossible to tell you the easiest way to do it.

Return to “C/C++”