What happens if printf is not handled - c

I'm current supporting some legacy software written aeon ago, compiled in gcc 2.95.3. The program is ran in an SBC, occasionally I had to telnet into it to fix things. I notice that when the SBC is running on its own, it display some unexpected behavior e.g. missed executions.
When I do telnet in, slay the process and execute it manually, it stays fine that way. I'm suspecting that this has to do with performing printf without a proper terminal/console for it to print on.
If printf are not handled, what would be the effect of it?

Could it be that the system is trying to print to a hardware serial port (which are really slow) and when you telnet in its replaced by a TCP port instead?

It depends on the stdio library you use, most will attempt to write to console anyway, so...
You should redirecting your STDOUT and STDERR to NULL or to a file, this should be quiet easy.
gcc 2.95 was very stable so we can pretty much rule out any problems there.

Related

Are commands supplied to system() visible to other users?

I need to run a single curl command. The powers that be have decided this has to be done in C to avoid anyone being able to see the source code.
I don't know anything about C and have only little programming experience but I found you can use system() to execute a shell command like this:
int main()
{
system("/usr/bin/curl http://192.168.1.1");
return 0;
}
However, will there be a record/log anywhere in Linux (Ubuntu) that shows the full command my program executed?
Anyone with access to your account will easily find out whatever you are trying to hide (using strings or strace).
Someone without said access (hopefully the whole world except you and your sysadmin) can still use ps. It won't make any difference whether you use a C wrapper or run the command directly: in both cases ps will show the command and its arguments in all their glory.
Some commands, like sqlplus, manipulate their command line immediately after (although not exactly at) startup to hide e.g. password from prying eyes. curl does the same with usernames and passwords, but certainly not with URLs (which can be easily spied upon by network monitoring tools anyway)
On (post-3.2) linux, your sysadmin can (re-)mount /proc such that only root has access to your command lines, using the hidepid mount option
This will go a long way towards protecting your command lines - a C wrapper will only be useful for its placebo effect on your boss.
I need to run a single curl command. The powers that be have decided this has to be done in C to avoid anyone being able to see the source code.
You probably need to make a single HTTP request. I recommend taking time to read some HTTP specification, like RFC 2616 or newer. Read also about HTTP/2
Consider using (from your C code) the libcurl library for that (without using system(3) to run any curl(1) command). You need spend several days in reading about HTTP and libcurl.
Of course, someone could strace(1) your software (and find all details about your HTTP request and response), and understand the involved syscalls(2). See also credentials(7) and read Advanced Linux Programming and the documentation of GCC.
Learn also more about C, e.g. by reading n1570 and this C reference
Read also (and study documentation and source code) about the GDB debugger and ptrace(2).
Don't forget that the Linux kernel, the GCC compiler, the GDB debugger are all free software: you are allowed to download their source code, study it (it could take months), recompile that code (see LinuxFromScratch), and improve them.
There is no record/log of commands being executed by default. If you use bash, it has a history feature you can disable (with HISTSIZE=0). cron logs commands to syslog. ps will show processes that are currently running. If you run your c-program with ltrace or strace it will trivially tell you what it is doing. As will strings of the (stripped) binary.

Stop other processes from writing to tty

I am interested in stopping all processes other than the one I am writing from printing to the tty in which my process is running. It can be assumed that my application is only to be ran in a real tty.
I would prefer a POSIX compliant solution, but ubuntu or freeBSD specific solutions would be appreciated as well. The system I am coding on is Ubuntu 14.
I am writing in C, but portability is not so much of a concern that I would need to avoid system calls, nonstandard headers (like POSIX headers), etc.
I am having a hard time seeing where to start with this. I thought maybe stty would have a command for it, but I couldn't find one in its man page.
To illustrate my issue, my application uses the terminal as a character grid rather than a sequence of lines, so when a keyboard is plugged in, I get output regarding configuration, which overwrites the characters I have in place. I am about to resort to regular reprinting of the entire screen, but I'd really like to avoid that. So, to do that, I really need to stop other processes from writing to the tty.
Thanks in advance!

Redirect printf to Console I/O on IAR Embedded Workbench

I have coded a project of mine in C on a Windows machine in the software IAR Embedded Workbench IDE. The project compiles and runs fine. I have a couple of printf functions in my code. But the thing is that the project is intended for a microcontroller AT91SAM7X256. I've successfully built my application to run on the sram of the microcontroller, and the application was loaded successfully. But the printf function is being directed to the USART port of the controller (I can only assume), and so I would like to redirect the printf to display the text on my terminal I/O. Does anyone have an idea on how I can do that?
I use the ARM, AVR32 and MSP430 processor versions of the IAR toolchain. In each of these you have to implement your own low level functionality to handle the stdin and stdout streams. The ARM compiler manual has a section on "Standard streams for input and output" which says that you need to write your own version of the __write() function, and it provides an example version where the data is written to a memory mapped LED display.
Knowing IAR I would expect they will have a similar example for your processor/toolchain combination.
I don't know why your printf is being redirected but to do what you want to do I did the following:
First show the console window. (while running debug go to menu View-> Terminal I/O
then stopping the application, go to Project->Options and in Linker-> Library check Include C-SPY debugging support
Be warned that this makes the running of the program considerably slower, than it should be. Comment out all printfs later when finishing your debugging
The standard answer is to select "Semihosted" and "Via semihosting" (Or via SWO -- but I have been using the other.) And "buffered terminal output" for speed under library options 1.
However, I am poking about here because I have a demo program from ST that won't printf to the console and I don't know why -- have many applications that work. So if anyone can remember some detail that gets left out of the standard information feel free to say something...
all you have to do is to #include <stdio.h> and use printf() in your code. For this to work with the debugger console you will have to go to Project options -> Linker -> Output (tab) and in the "Format" section where you have selected "Debug information for C-SPY" you need to have "With I/O emulation modules" checked (this requires "with runtime control modules"). This will make the linker use its special low-level I/O routines (putchar() , getchar() ) which will route it to/from the debug terminal I/O console.
When entering debug mode you get the debug console window displayed by selecting View -> Terminal I/O from the top menu. You will probably notice that if you try to printf too much text it will make the output lag a bit. But it can surely be a handy tool for debugging your application anyway.
(For the curious, you can see in the disassembler that the putchar() / getchar() functions linked into the code when using I/O emulation are just NOP instruction dummy functions. The C-SPY debugger will set a hardware breakpoint in this function, and whenever this breakpoint is reached it will read out the register to read/write the character. The communication overhead causes a small delay for each character. A side-effect of using I/O emulation is that the debugger will reserve one hardware breakpoint for each of these two functions (if you are using them), so you will get fewer hardware breakpoints available to use in your other application debugging.)
You can find some more information about "I/O emulation modules" in the IAR user manual: Help -> 8051 Embedded Workbench User Guide.

How to detach program from terminal and to attach it back?

I am working on an embedded project, where I need a program without external dependencies that works like screen or tmux. These two programs are not good because they need other libraries.
Since I only need to detach a program, being able to log-out and getting it back when I log-in again, I was wondering whether I can write a small program for that.
Do you know which calls (in C) I need to do to detach the program and to have it back?
If i understand your requirements correctly, you could theoretically use termios struct and ioctl to achieve this.
ioctl(0, TIOCNOTTY, NULL);
to detach and
ioctl(0, TIOCSCTTY, 1);
to attach back to the terminal. However, it doesn't actually perform the job.
The following solution describes a not so nice but practical work around
tty demulsified
The primary intention there is to attach program to another terminal but i believe that is the way you can achieve your goal too.
Regarding your mention of embedded platform, you might be able to find some help from busybox
It compiles for embedded linux with a pretty small binary and contains most of commonly used linux utilities such as getty, stty etc.

can anyone suggest a good program for debugging a C program?

I need to debug a C program that includes posix threads, socket programming (udp client, server). I use ubuntu 12.04 and as IDE/SDK, Qt Creator 2.4.1 and Netbeans IDE 7.1.2. I know they use gdb for debugging.
When I start to debug my program, the program stops running after 5 min or so and neither Qt Creator or Netbeans output any error or warning, although I use debugging feature and my program suppose to listen for a UDP port.
I use printf for all line of my code, and I can see that my program works as it suppose to and listen the UDP port and waits. I can not figure the problem out why it stops without any reason and since IDEs that I am using do not show any debug error, warning, I can not think any reason.
I wonder if anyone can suggest me a debug program that monitors all/some variables and threads during the run time. Thank you.
An old, but reliable tool is ddd, which is basically the gdb GUI wrapper. Although, I usually do debugging directly with Emacs, ddd is the tool that you'll be able to run on almost all *nix platforms.
gdb isn't too comfortable but always available.
To do runtime analysis of different types, especially checking memory access, Valgrind (see here for docs) might be the tool of choice.
Update: I'm referring to *IX systems. For Windows gbd also works in the cygwin enviroment. Nativly there is VC Express, which is free and includes IDE and debugger.
I'm not quite sure what debugger is used for your kind of application. The only debugger I know about on Linux is gdb. It along with printf statements is all I need.
gdb is simple, though not "too comfortable" as #alk said, but seems to be ubiquitous.
There is also Eclipse, and that's quite a nice development and debugging platform, too.

Resources