Sending parameters through ssh. Are there NON-BLOCKING input functions? - c++

I am working with an embedded system in which a Linux-based OS is running. Let's say that an application is running and the behavior needs to change when some actions happen. I did it using buttons and switches on the board but I have no idea on how to do it via ssh where the output of the application is constantly prompt. Any suggestion? Is it possible to use the keyboard of the laptop to send parameters at runtime only if a keyboard-button is pressed? (let's imagine I want to press only the + and the -). Of course, the output of the application should continue to be prompt constantly.
Of course the solution cin << will not work because it stops the execution until the new parameter is set up. The aim should be "change behavior if and only if + or - are pressed".

Use signals. Register a signal handler in your program and signal it from the outside with pkill -<SIGNAL> -f <PROGRAM> or kill -<SIGNAL> <PID>. For instance:
ssh user#server pkill -USR1 -f my_program

Related

how to quit currently running Trace32 from command line

I am doing the automate regression with Trace32. Before the regression starts, if any Trace32 process is in use, I want to kill the process. The problem is, if I kill it with system OS, when regression starts, the GUI will pop up a dialog saying "TRACE32 device already in use. Reset device and connect?" I have to manually click yes to continue to regression. Is there any way to quit the currently running Trace32 properly from command line, such that the reset dialog will not show when Trace32 starts next time. Or any command I can add to the .cmm file in my regression to skip this question dialog. I have tried to put RESet initially in .cmm, which does not help.
First of all try to end all your automated test with TRACE32 command QUIT. This will close TRACE32. However something might go wrong in your tests and thus, the QUIT command might not get reached and TRACE32 is still running.
So secondly start TRACE32 with an open Remote-API port. Add to your TRACE32 config-file (by default this is c:\T32\config.t32) the following lines
RCL=NETASSIST
PORT=20000
Before and after the block there must be an empty line. You can also choose any other number for PORT, which specifies a UDP/IP port, which gets opened by TRACE32. (If more than one TRACE32 instance is active at the same time, use different a port number for each instance.)
If TRACE32 was started with open Remote-API port you can send a QUIT command to the still running application instead of terminating it via a kill-command. To send the QUIT command used command line tool t32rem.exe as follow:
t32rem localhost port=20000 QUIT
Finally we need a way to handle the (hopefully rarely happening) situation that TRACE32 somehow crashed and is not longer responsive. Then you have to kill it of course. For a proper reconnect use the following setting CONNECTIONMODE=AUTOCONNECT in the PBI= section of you TRACE32 config-file (by default this is c:\T32\config.t32). This setting does the "Reset device and connect" without asking you.
Putting all together you config-file should look somehow like that:
OS=
ID=myT32
SYS=C:\T32
PBI=
USB
CONNECTIONMODE=AUTOABORT
RCL=NETASSIST
PORT=20000

How to restart QtSingleApplication?

I can restart my application with
QProcess::startDetached(QApplication::applicationFilePath());
exit(12);
But if I have QtSingleApplication new instance will not run. Is there way to trick QtSingleApplication to allow it to run new instance when isRunning() returns true maybe by some command line arguments or another aspects?
I'll be fully fine if I will add Sleep(5000); in the beginning but it doesn't look alike good design, I think I just need an additional mutex to detect if my application is gone :S what do you think?
When you want to start your app as new running instance, pass a specific argument to signal it should close any existing rather the usual opposite.
This can be handled as
//code to send to instances
if(myturn)
{
if (a.isRunning()) {
QString rep("Another instance is running, so I will ask to leave.");
bool sentok = a.sendMessage(message_gtfo,1000);
if(!sentok)
return "some error";
}
}
//code to receive message from instance
connect(&a, SIGNAL(messageReceived(const QString&)),
&this, SLOT(handlMessage(const QString &))
);
...
void instancecomhandler::handlMessage(const QString & msg)
{
if(msg == message_gtfo)
//proceed to exit
}
edit:
You still need to pass the argument to modify the behavior. Default behavior quit when another instance is running. New behavior waits till the other instance terminate.
One way to do such a thing is to use a little helper application (or script). The helper application is started detached and receives an identifier (ie PID) of the currently running program as a command line argument. It then waits until that identifier has disappeared from the process table, then restarts the application and exits.
This is a bit hacky/racy, but it works okay for me in practise. I use this in an update system, where I need to replace the running program with a new version of itself, which on Windows cannot be done while the program is running.
If you are restarting your application from inside itself, it is by definition no more a single-process application (since for a short time at least, two processes running the same application exist).
I would recommend using some shell tricks to restart it.
On Linux with a X11 desktop, you might e.g. popen(3) some at (or batch) command which would restart your application. You'll probably need to explicitly set the DISPLAY environment variable.
On Linux or other POSIX systems, a possible way would be to wrap the application in a shell script (for example mywrapper) like e.g.
#! /bin/sh
# script file mywrapper
aftersource=$(tempfile -p app)
myapp --aftersource $aftersource "$#"
source $aftersource
rm $aftersource
You'll need to improve the above script to handle failures and trap some signals... I guess you might adapt that script to Windows if so needed
Then your application would process the --aftersource argument to write some shell commands in it. If it want to restart itself it would e.g. write some command into that file to do that, etc.
Feel free to improve the script (trapping some signals and exit, looping, etc...)

Netcat on remote machine using ssh

I was trying to run netcat by using ssh and seems that my code always fails in my C code. Here are the commands which I am executing using system() in this order.
system("ssh machine 'nc -l 61001|dd bs=64M of=temp' &")
system("/bin/dd if=filename bs=64M|nc IP_address 61001")
I noticed that the first command works correctly as the temp file is created on the remote machine. The second dd command fails and states that 0 bytes has been written on the remote machine. These commands work correctly when executed from the terminal, but fail as soon as I put it in system() calls in C.
Short answer:
a & disown (instead of just &) should do.
Explanation:
system() spawns a shell that just executes one command and then exits. The & tells the shell to fork the command into background (means, it doesn't wait for its completion), but it's still part of the session and process group of the shell. When the group leader (the shell) exits, all children are killed. The disown causes the shell to start new process group, the child process is now owned by init (the first process in the system).
"Real" answer:
This is about programming. You are forking processes like crazy to accomplish something a C program could easily do using builtin library calls (except for the ssh but there are better ways, too). Go read on BSD sockets.

Program doesn't stop when "ctrl c" is used to terminate program

I have a programme (C++ compiled) that using tcp socket to communicated. The programme is configured in two mode. Let's say mode A and mode B.
Start the programme mode A, it will give some prints like:
waiting connections on port 1234
local endpoint : 0.0.0.0:1234
//I think it is using boost for TCP socket
Then start mode B. They will find each other and run perfect.
Question is if I start mode A, and then use "ctrl c" to terminate the application with mode A. It will left the port open there.
When I start the mode B, it will also find the connection and runs with error due to A is not there.
I have a bash to run the application, I want to ask how I can force that port to close? (In bash or other possible way)
Thanks
Use this in the bash script (before calling your binary):
trap "fuser -k -n tcp 1234 && exit" SIGINT SIGTERM

GenerateConsoleCtrlEvent() won't due for termination signaling to process run by cmd.exe

When one is interactively using cmd.exe to run all sort of windows CLI application, one can easily stop them by pressing CTRL+C or CTRL+BREAK . this is implemented by signaling the process as can be read here. As for cmd.exe itself, it does not terminate in these conditions as can be explained in a comment of this question.
Now, consider the following scenario. My application open a cmd.exe using CreateProcess(), and the user has started another application b.exe through it. Say that my application want to fold before b.exe has ended , and it doesn't really care about the graceful termination of it. optimally, I'd like to mimic the user pressing CTRL+C and then send exit to the cmd.exe (let's say I can do it IO-wise). the windows api offers GenerateConsoleCtrlEvent() for that (almost) exact purpose, but it can be ignored by the process (cmd.exe in that case) and in particular , it won't forward the signal to b.exe.