Cannot catch SIGINT while serial break condition occurs

I setup termios structure with IGNBRK is not set and BRKINT is set.
To allow the process to receive signals I call: fcntl(fd, F_SETOWN, getpid());
I have made a signal handler to catch all signals. I can catch SIGINT when pressing ctrl+c but when I send break signal over serial then it cannot catch SIGINT.
Is there anything I'm doing wrong?
Any suggestions and code samples would be appreciated.

Thanks

fcntl has nothing to do with the break key. BRKINT only works with the controlling tty. The INT character, which you have mapped to CNTL-C, also only works with the controlling TTY. So the same keyboard that is sending the cntl-c character should be able to send the break signal. If that keyboard is a PC instead of a true ascii terminal, the program in the pc make need a special operation to send a break signal out the serial port. As one example see: SUMMARY: Send Break Signal Through HyperTerminal

I use another computer to send data and break signal over serial. When my program receives data then i get SIGIO signal and i can use read to get the received data. But when I send break signal from the other computer then i receive no signal.
In manpage i can read that :" BRKINT If IGNBRK is set, a BREAK is ignored. If it is not set but RKINT is set, then a BREAK causes the input and output queues to be flushed, and if the terminal is the controlling terminal of a foreground process group, it will cause a SIGINT to be sent to this foreground process group.", but I'm not sure how to become controlling terminal of a foreground process.
I have no problem seting break condition on the serial line, but the problem is how to detect the break condition, without polling serial break counters or other things that stores information about break conditions.

Like I said: "BRKINT only works with the controlling tty."

Any specific functions to call to become a controlling terminal of ttyS0 for example? I have no idea how to achive that condition. Can ioctl(fd, TIOCSCTTY, (char *)NULL) or ioctl(fd, TIOCSCTTY, (char *)1) be used to become a controlling tty of ttyS0. If you have any specific suggestions then go a head.

ioctl TIOCSCTTY always returns -1 (Operation not permitted) even if I am super user.
Is it even possible to receive SIGINT from serial driver without being super user?

maybe this thread should be moevd to programming forum, but if I started this here I wont start another thread for the same pbroblem.
-------------------------------------------------------------------------------------------------------
What am I missing?
What needs to be added to receive SIGINT from serial driver?

#include <termios.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/signal.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#define BAUDRATE B38400
#define MODEMDEVICE "/dev/ttyS0"
#define _POSIX_SOURCE 1 /* POSIX compliant source /
#include <unistd.h>
#include <errno.h>
#include <string.h>
void signal_handler_SIGINT (int status); /
definition of signal handler */

main()
{
int fd,c, res;
struct termios options;
char buf[255];
int pid;

fd = open\(MODEMDEVICE, O_RDWR | O_NOCTTY | O_NONBLOCK\);
if \(fd &lt;0\) \{perror\(MODEMDEVICE\); exit\(-1\); \}

signal\(SIGINT,signal\_handler_SIGINT\);

fcntl\(fd, F_SETOWN, getpid\(\)\);
/* Make the file descriptor asynchronous \(the manual page says only
   O_APPEND and O_NONBLOCK, will work with F_SETFL...\) */
fcntl\(fd, F_SETFL, FASYNC\);

memset \(&options, 0x00, sizeof \(options\)\);
options.c_cflag |= \(CREAD\);
options.c_cflag |= CLOCAL;
options.c_cflag |= CS8; // Select 8 l2_data bits
options.c_iflag |= \(BRKINT\);

// options.c_cc[VMIN] = 1;
options.c_cc[VTIME] = 1;
if(ioctl(fd,TIOCSCTTY, 1) == -1)
printf("eh %s\n",strerror(errno));

\}

/***************************************************************************

  • signal handler. sets wait_flag to FALSE, to indicate above loop that *
  • characters have been received. *
    ***************************************************************************/

void signal_handler_SIGINT (int status)
{
printf("received SIGINT signal.\n");
exit(0);
}

Where have you got the double fork and setting of the controlling terminal?

Thanks for the tip. With this code I can receive SIGINT while break synchronization condition occurs.
Is there any suggestions that I should or should not do?

#include <sys/ioctl.h>
#include <termios.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/signal.h>
#include <errno.h>
#include <sys/types.h>
#include <unistd.h>
void signal_handler(int status);

int main(void)
{
int fd;

switch (fork()) {
case 0:
break;
case -1:
// Error
printf("Error demonizing (fork)! %d - %s\n", errno, strerror(errno));
exit(0);
break;
default:
_exit(0);
}

if (setsid() == -1)
{
printf("Error demonizing (setsid)! %d - %s\n", errno, strerror(errno));
exit(0);
}
fd = open ("/dev/ttyS0", O_RDWR, 0 );

struct sigaction saio; /* definition of signal action */
saio.sa_handler = signal_handler;
// saio.sa_mask = 0;
saio.sa_flags = 0;
saio.sa_restorer = NULL;
sigaction(SIGINT,&saio,NULL);
struct termios options;
memset (&options, 0x00, sizeof (options));
options.c_cflag |= (CREAD);
options.c_cflag |= CLOCAL;
options.c_cflag |= CS8; // Select 8 l2_data bits
options.c_iflag |= (BRKINT);
// options.c_cc[VMIN] = 1;
options.c_cc[VTIME] = 1;
if (tcsetattr (fd, TCSAFLUSH, &options) == -1)
{
printf("port setup failure\n");
return -1;
}
ioctl(fd, TIOCSCTTY, (char *)NULL);
getchar();
}

void signal_handler(int status)
{
printf("received SIGINT %d signal.\n", status);
exit(0);
}

Is it possible to detect when the serial port is not pending on a serial break any more?

Hmm. This is a VERY good guide to programming serial ports. Instead of thrashing about, use some of the examples:

http://www.easysw.com/~mike/serial/serial.html

Thanks for the link but I have rad that site about 10 times and I cant find anywhere how to detect serial break condition. Can you be more specific and tell how to implement this kind of functionality without using fork() or double fork(). I admit that my posted code is not good but I wanted to show how I archived required functionality. Any better solution would be appreciated.

What platform/operating system does this have to run on?

on linux platform