embedded linux, tty not working during boot - linux

I'm writing a software to test some peripherals on my device.
I have to check some tty devices, so I writed a C binary that writes and reads from a given tty. It works perfectly called from command line, but it doesn't work if called from an init.d script.
Any ideas?

Login prompt appears after start-up scripts are executed - that is why they are called start-up scripts.
I am not sure changing the order would be wise and safe. I would probably wait (sleep) for a while. Just don't forget to run your program in the background mode, otherwise it'll block boot process:
my_test_tty_program &

Related

Monitor commands that are executed in a shell in Linux and block some of them, using a user-mode app or kernel module?

In windows, we can monitor executed command with various ways, such as monitoring process creations with a kernel driver callback and checking if the parent is CMD or powershell, then parsing its command line to see what command has been executed.
My question is, how to do this in linux? meaning how can i write a program that monitors every executed command in most of the common shells such as bin/sh, and blocks certain commands from getting executed via command line?
Is this possible with a user-mode app? if not, then how about a kernel module?
Edit 1:
Also note that it is really important to find the parent process that executed the command in this case, for example if a benign process executed command "X" it can be completely fine, but that same executable getting executed by something else might be extremely suspicious and needs to get blocked/reviewed. Basically what is the equivalent of PsSetCreateProcessNotifyRoutineEx in Linux?

linux option to detect keyboard interrupt when making system call

Usually when I run a program in terminal I am able to stop it with control C.
When I wrote a program which invoke an operating system command inside a while loop, the rest of the code executes so fast that there is no way to stop the program other than to kill the pid. The way I have been doing is adding a sleep for 1 second but it is not a good practice to fix it.
while(!phrasespotted){
invoke an operating system command to arecord for 5 secs, during which control C can not stop the program
process the audio for phrase spotting
sleep(1); //during this I can stop the program in terminal
}
What do I add to my code so that it can pick up the keyboard interrupt while making the system call, or even register it to stop it immediately after?
Is threading the only option based on catching the signal? https://www.thegeekstuff.com/2012/03/catch-signals-sample-c-code/

QEMU no terminal output after execvp

QEMU is used by me as an emulator with PetaLinux kernel (terminal-only with -nographic option).
At the beginning QEMU initializes itself and once it's ready it boots-up the system. Usually, boot messages are printed in a terminal during the process. With that scenario everything works fine, system starts and one can login and use the system.
Then, another simple peripheral device is implemented, which similarly is initialized (printing control message to the output) and the rest goes on the same way.
The problem rises when there is a child process spawned from inside the peripheral implementation ( fork() followed by execvp() ). The main QEMU process (parent) no longer prints to the terminal output, the new process (child) only can do it. It seems like the parent process is probably still executing but loses the terminal. Without it one cannot see login prompt, so basically cannot use the system at all.
What is the reason?
I found the solution. The problem was because the child process was using SIGUSR1 to signal some event to the parent process. Apparently, QEMU itself uses SIGUSR1 so there was a collision of handlers, maybe both of them had been invoked, and somehow the parent process was freezing. Everything works fine after changing my signal to SIGUSR2. Hopefully there is no more collision with QEMU internals.

Child processes won't die in Jenkins environment

I'm developing code for Linux, and cannot seem to kill processes when running in a Jenkins environment.
I have test script that spawns processes and cleans them up as it goes through the tests. One of the processes also spawns and cleans up one of its own subprocesses. All of the "cleanup" is done by sending a SIGINT, followed by a wait. Everything works fine with this when run from a terminal, except when running through Jenkins.
When the same exact thing is run in Jenkins, processes killed with SIGINT do not die, and the call to wait blocks forever. This wreaks havoc on my test. I could update the logic to not do a blocking wait, but I don't feel I should have to change my production code to accommodate Jenkins.
Any ideas?
Process tree killer may be your answer - https://wiki.jenkins-ci.org/display/JENKINS/ProcessTreeKiller
In testing, this would usually work when I ran the tests from the command line, but would almost always fail when that unit test script was called from another script. Frankly, it was bizarre....
Then I realized that when I had stray processes, they would indeed go away when I killed them with SIGTERM. But WHY?????
I didn't find a 100%-definitive answer. But thinking about it logically, if the process is not attached to a terminal, then maybe the "terminal interrupt" signal (SIGINT), wouldn't work...?
In doing some reading, what I learned is that, basically, when it's a shell that executes a process, the SIGINT action may be set to 'ignore'. That make sense (to me, anyway) because you wouldn't want CTRL-C at the command line to kill all of your background processes:
When the shell executes a process “in the background” (or when another background process executes another process), the newly executed process should ignore the interrupt and quit characters. Thus, before a shell executes a background process, it should set SIGINT and SIGQUIT to SIG_IGN.
Our production code isn't a shell, but it is started from a shell, and Jenkins uses /bin/sh to run stuff. So, this would add up.
So, since there is an implied association between SIGINT and the existence of a TTY, SIGTERM is a better option for killing your own background processes:
It should be noted that SIGINT is nearly identical to SIGTERM. (1)
I've changed the code that kills the proxyserver processes, and the Python unit test code, to use the SIGTERM signal. Now everything runs at the terminal and in Jenkins.

What's the difference between a foreground and background program?

I tried to run a program in background mode and foreground mode.
While in background mode, it failed to run.
But in foreground mode, it succeeded.
So what's the difference between background and foreground mode that might cause this issue?
A foreground process has access to the terminal (standard input and output).
You can try to fix the issue by adding <> /dev/null to the commandline. This will tell the program not to use stdin. Some programs take this into accound and "behave" as soon as you don't give them a terminal anymore.
Another solution is the nohup program which does basically the same plus some more things.
Background processes typically run with little or no user interaction, they interact with the system instead. Forground processes are what the user interacts with. Background processes, unless explicitly ran so, run with non-admin permissions. If you ran it under your user-context, then it would likely have the permissions to do whatever it is the app does.

Resources