How to know if an SSH connection failed? - c

I have to run a ssh command in a separate process (so by means of execlp) to connect the running machine to another machine in the same local network. The thing is, I have to establish the hostname entered is valid so the ssh connection succeeds.
Since, execlp replaces the calling process' image on a successful command run (which will be the case with ssh), there is, to the best of knowledge, no way of knowing in the calling process whether the ssh connection was successfully set up.
Hence, the sole solution to this incovenient behavior way I can think up is to assert the given hostname of the machine to connect to is valid. How can/should I about that?
(A valid hostname is simply one that exists and, of course, is reachable, be it an IP adress or an alias)

In order to spare myself all the pipe setting, I use
popen() to run ssh. So yes, if there is no
other way, I might have to go down the less lazy path. …
waitpid() …
You don't have to go down the less lazy path. man popen:
The pclose() function waits for the associated process to terminate
and
returns the exit status of the command as returned by wait4(2).

Related

Restarting inetd should effect instances of all inetd controlled processes

When I am sending HUP signal to inetd so that it rereads the new inetd.conf file, what I want is, the processes controlled by the inetd process should also restart, so that it can read the new command line parameters added to the inetd.conf file as part of the change.
I know I can search for the running process and kill it, but is there a standard way to do this. I could not find anything over the Internet.
The standard inetd included in NetBSD does not manage the processes it starts (except for single-threaded services, i.e. those with "wait" flags) -- it just starts them. Each child process services one active connection and then exits when done (i.e. when the connection is closed). In the general case it would be very unwise to kill such processes early without very good reason -- for example consider the case where your current login session (where you tell inetd to reload) was opened to a service controlled by inetd (e.g. sshd).
If you really want to kill processes handling active current connections then you will have to write some helper script of your own to do that, though perhaps pkill will suffice.

Determing established connection time

I'm trying to find a way to determine how long a connection as been established to any IP and any port and I want to use C.
So far, I can get the remote IP and port as well as the local IP and port and the program name and its PID by forking and calling netstat and extracting data from it byte-by-byte.
However, I prefer if theres a standard API thats included with linux that I can directly use in my program so I don't always have to call external programs to see if a connection is established and for how long.
Is there a way I can do this without the need for calling external programs and extracting output from them?
What I want to do later in my program is kill a process if the established connection time is way too long (I'm trying to defeat the slowloris DOS attack).

Can D-Bus connect to a particular PID when there is more than one instance of a program running?

I'm trying to write a program which will interact with VLC over D-Bus.
When an instance of VLC is running I can execute things like this in the shell
qdbus org.mpris.MediaPlayer2.vlc /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Pause
qdbus org.mpris.MediaPlayer2.vlc /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Play
VLC pauses and resumes as expected. Great.
What if there is more than one instance of VLC running, how do I choose which instance the command is sent to? I know its PID. The DBus client doesn't have to be qdebus.
no
every d-bus connection gets unique name and you can ask for other name later using org.freedesktop.DBus.RequestName call, but it has to be unique as well. See "message bus names" part of spec. Note that one process can create multiple connections to bus (and thus have multiple names associated with it)
When you make a d-bus function call you use service name, object path, interface name and method name. First one is used by bus daemon to route your message, and service itself decides how to interpret path/interface/method/parameters part of message.
You can get pid the other way: well behaving dbus clients have to support org.freedesktop.DBus interface and you have org.freedesktop.DBus.GetConnectionUnixProcessID. You could iterate over all connections ( ListNames method ) and compare connection pid with what you have. This does not guarantee one to one mapping.

C program that connects to remote host and then executes system commands

I've looked around online about executing system commands through a c program, but none of them touched on executing the command after connecting to a remote host such as (this connection prompts for a user password):
sprintf(buffer1,"ssh -l %s %s ",userName,hostName);
system((char*)buffer1);
//Nothing below this executes because the connection has been established
sprintf(buffer2,"shasum sfin.exe > t.sha");
system((char*)buffer2);
Once the connection is closed the program then continues to execute, is there a simple way to keep the execution going?
You'll want to use the function popen instead of system.
http://linux.die.net/man/3/popen
It runs a command, returning a file object that you can write to with functions like fprintf, fwrite, etc., and those commands will go through the ssh process to the remote computer.

Checking network status and controlling PPP in a program

So I'm running PPP under linux with a cellular modem. The program I'm writing needs to know if the link is active before sending any data.
What are my options to check
if the link is available
if it routes to a server I control (it doesn't go to the internet as I said earlier)
Also, what is the best way to restart it - I'd like to have program control over when it starts and stops, so I assume an init.d isn't appropriate. Using system() doesn't seem to give a PID, are there other options besides fork() and the gaggle of exec??() calls?
C on Linux on ARM (custom distribution using buildroot).
You can use the ip-up script functionality of pppd to have it execute a program when the IP interface is up and ready. Details are in the pppd(8) man page - search for "ip-up".
To restart pppd, use the linkname parameter to set a known pidfile name (/var/run/ppp-name.pid). Also use the persist option. When you want to restart pppd, send the SIGHUP signal. Again, this is described in the man page.
You could parse /proc/net/route.

Resources