considering being new to C language on Linux, I've walked through socket programing scenarios where you have to handle that SIGPIPE issues and I've faced the fllowing cases:
1- catch sigaction for the process then continue, which equals ignoring the signal.
struct sigaction sginal_action;
memset(&sginal_action, 0, sizeof (sginal_action));
sginal_action.sa_handler = SIG_IGN;
sginal_action.sa_flags = SA_RESTART;
if (sigaction(SIGPIPE, &sginal_action, null)) {
perror("signal action error");
}
2- ignore the signal over thread using
sigset_t sigpipe_mask;
sigemptyset(&sigpipe_mask);
sigaddset(&sigpipe_mask, SIGPIPE);
sigset_t saved_mask;
if (pthread_sigmask(SA_RESTART, &sigpipe_mask, &saved_mask) == -1) {
perror("pthread_sigmask");
}
-Now every thing shall work fine but I need an answer about this test case behavior:
1- server accepted socket from user A with file discriptor int 7
2- I run lengthy SQL statement that takes 25 secondes
3- at second 20 client closed socket
4- at second 21 Linux kernel sent SIGPIPE
5- at second 22 process ignored SIGPIPE and continued working with file decriptor 7, since there is no signal any thing went wrong
6- at second 23 server accepted socket from user B with file discriptor int 7, again!
7- user B authorization takes 2 seconds shall finish at second 25 of user A.
8- the core problem starts at second 25 write(file_dsciptor, buffer, buffer_size); from both threads will write data to both user A and user B channels
9- it happens that user B is not authorized to login to server but before server close file decriptor 7 at thread B, thread A of client A writes to file decriptor 7 and client B received vital data due to the reuse of socket file descriptor 7!
My question,
is that scenario wrong and will never happen?
or that is possible to happen but there is a correct method to apply?
and is there a C system call function to stop file_discriptor re-usage over socket connections?
or what shall be done to ensure that opened channel is not conflicted with another channel, like unique file discriptor I guess?