LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Freeing ttyS0 for loopback test - application hangs/blocks (https://www.linuxquestions.org/questions/programming-9/freeing-ttys0-for-loopback-test-application-hangs-blocks-4175467900/)

leonp 06-30-2013 09:03 AM

Freeing ttyS0 for loopback test - application hangs/blocks
 
I run Montavista 2.6.18 on ARM/TI.
I need to use external loopback on ttyS0, which is linux console by default.
The application will test the whole serial track via loopback.

It is started from initd (script in rc3.d) and getty start in iniittab is allowed as "once" only (not respawn).
The reduced initialization code looks like:

system("killall getty");
out = open("/dev/null", O_WRONLY);
dup2(out, fileno(stdout))
fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NONBLOCK);
tcgetattr(fd, &Opts);
cfmakeraw(&Opts);
Opts.c_cflag &= ~CRTSCTS;
Opts.c_cflag |= CLOCAL | CREAD;
tcsetattr(fd,TCSANOW,&Opts);

Now, the questions/problems:
1. When the first line of the initialization code system("killall getty") is active, application hangs at this point (never exits system() call), if the loopback is closed. Otherwise it passes OK, the test starts, and even passes, when I close the loopback later.

2. Removing system() call allows the application to initialize and pass to the first write(fd,"Test text",10), which hangs.
Again the same - starting with loopback opened and closing it later after makes everything working as required.

Any hints/recommendations/help will be highly appreciated...

NevemTeve 06-30-2013 10:19 AM

remove these lines, they make no sense:

Code:

out = open("/dev/null", O_WRONLY);
dup2(out, fileno(stdout));

You don't have to kill getty from your program, simply don't run it from inittab.
Also add error-checks to your code:

Code:

const char *device= "/dev/ttyS0";
fd = open(device, O_RDWR | O_NOCTTY);
if (fd==EOF) {
    fprintf (stderr, "*** Error opening %s errno=%d: %s\n",
        device, errno, strerror (errno));
    exit (12);
);


leonp 07-01-2013 01:57 AM

Thank you for reply.

Quote:

remove these lines, they make no sense:

Code:

out = open("/dev/null", O_WRONLY);
dup2(out, fileno(stdout));


I wanted to supress my application stdout/printf, because I received it back together with my test output. This worked - I now receive only the test sequence.

Quote:

You don't have to kill getty from your program, simply don't run it from inittab.
The cause of this kill was, that ttyS0 testing is the exceptional case. Otherwise I wanted to preserve the opportunity to login minimum once. Therefore I substituted "respawn" to "once" in inittab and inserted kill getty.

But you are right - my test without getty at all shows that everything is working fine.
So, the question now is why I cannot remain getty with "once" option and kill it from within the application!


Quote:

Also add error-checks to your code:
Thanks again! There is error checking, I just removed it to simplify the code in the post. But you are absolutely right.

NevemTeve 07-01-2013 02:50 AM

(you don't have to stop every getty, only the one listening on /dev/ttyS0)


All times are GMT -5. The time now is 01:15 AM.