Can C library functions (specifically sigaddset()) fail with an error not mentioned in standard?

StackOverflow https://stackoverflow.com/questions/18376091

  •  26-06-2022
  •  | 
  •  

質問

In my program,sigaddset() is failing with error:

sigaddset: Invalid Seek

which corresponds to ESPIPE in errno.h but this error is not mentioned in the standard definition of sigaddset().

The complete code is too long, I am posting the required parts:

The signal handling routine:

void* Ctrl_C_handler(void *arg)
{
    int *sock_fd_ptr = (int*)arg;
    sigset_t set;
    int err;

    err = sigemptyset(&set);                // Clear the signals holder set
    if(!err) perror("sigemptyset(&set)");

    err = sigaddset(&set, SIGINT);          // Add the SIGINT: Ctrl+C
    if(!err) perror("siaddset(&set, SIGINT)");

    err = sigthreadmask(SIG_UNBLOCK, &set, NULL);   // Set mask as to unblock SIGINT
    if(!err) perror("sigthreadmask(SIG_SETMASK, &set, NULL)");

    while(1)        // Ctrl+C
    {
        sig = sigwait(&set);            // Wait for signal to occur
        if(sig == SIGINT)
        {
            printf("Ctrl+C detected by server !!\n");
            printf("No more connections will be accepted!!");
            if(*sock_fd_ptr > 0)
            {
                close(server_sock_fd);
                *sock_fd_ptr = -1;
                break;
            }
        }

        else
        {
            printf("Error handling the signal, SigHandlerThread exiting..\n")
            perror("sigwait(&set)");
            break;
        }
    }
    return NULL;
}

Inside main():

/*********** Signal Handling *************/

sigset_t set;                       // declare a set to hold the signals 
err = sigfillset(&set);             // Fill the set with all the signals
if(!err) perror("sigfillset(&set)");

err = sigthreadmask(SIG_BLOCK, set, NULL);  // Block/Mask the signals in the set
if(!err) perror("sigthreadmask(SIG_BLOCK, &set, NULL)");

err = pthread_create(&sig_handler_tid, NULL, Ctrl_C_handler, (void *)&sock_fd);
                                    // Create a thread for handling signals
if(!err) perror("pthread_create");

What am I missing or doing wrong or understanding wrong?

P.S.: I have recently started programming using signals.

Operating Sys: SLES 10 SP3 x86-64

役に立ちましたか?

解決

Your error testing is wrong. These functions return 0 when they're successful, -1 when there's an error. Your test:

if (!err) perror(...);

is equivalent to:

if (err == 0) perror(...);

which means you call perror when the calls are successful. As a result, you're printing the value of errno left over from some past system call that failed (often this is something internal to the stdio functions).

You should do:

if (err == -1) perror(...);
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top