Sigtimedwait () returns EAGAIN before timeout

I am trying to learn how to use sigtimedwait (), but I believe that it does not wait for the timeout to complete. Below, it seems EAGAIN 4s returns faster than necessary (1 s faster in 1 minute timeout):

#include <signal.h>
#include <syslog.h>
#include <stdarg.h>
#include <stddef.h>
#include <errno.h>


int main(int argc, char* argv[]) {
    setlogmask(LOG_UPTO(LOG_NOTICE));
    openlog ("SIG_TIMED_WAITER", LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL1);
    syslog (LOG_NOTICE, "Started");

    sigset_t set;
    sigemptyset(&set);
    sigaddset(&set, SIGUSR1);

    struct timespec to;
    to.tv_sec = 240;
    to.tv_nsec = 0;

    int ret = sigtimedwait(&set, NULL, &to);
    if(ret < 0) {
        if (errno == EAGAIN) {
            syslog (LOG_NOTICE, "EAGAIN: TimedWait complete...");
        } else {
            syslog (LOG_NOTICE, "ERROR!");
        }
    }   else {
        syslog (LOG_NOTICE, "Interrupted by signum: %d.", ret);
    }

    syslog (LOG_NOTICE, "Terminated.");
    closelog();
}

And here is the conclusion:

$ tail -f /var/log/syslog|grep "SIG_TIMED_WAITER"
Jan  7 15:39:41 localhost SIG_TIMED_WAITER[13275]: Started
Jan  7 15:43:36 localhost SIG_TIMED_WAITER[13275]: EAGAIN: TimedWait complete...
Jan  7 15:43:36 localhost SIG_TIMED_WAITER[13275]: Terminated.

I expected to see "EAGAIN: TimedWait complete ..." recorded four seconds later.

Is something wrong with my code, or is it for some other reason? Note that I do not see this, for example, select (), which waits four minutes.

+4
source share
1 answer

LOG_LOCAL1- reserved item. I.E. do not use it

Use instead LOG_USER

It’s easier to follow the action if the options parameter also has LOG_PERROR

stderr.

/ .

#define _GNU_SOURCE

#include <signal.h>
#include <syslog.h>
#include <stdarg.h>
#include <stddef.h>
#include <errno.h>


int main( void )
{
    setlogmask(LOG_UPTO(LOG_NOTICE));
    openlog ("SIG_TIMED_WAITER", LOG_CONS | LOG_PID | LOG_NDELAY | LOG_PERROR | LOG_PID, LOG_USER);
    syslog (LOG_NOTICE, "Started");

    sigset_t set;
    sigemptyset(&set);
    sigaddset(&set, SIGUSR1);

    struct timespec to;
    to.tv_sec = 240;
    to.tv_nsec = 0;

    int ret = sigtimedwait(&set, NULL, &to);
    if(ret < 0)
    {
        if (errno == EAGAIN)
        {
            syslog (LOG_NOTICE, "EAGAIN: TimedWait complete...");
        }

        else
        {
            syslog (LOG_NOTICE, "ERROR!");
        }
    }

    else
    {
        syslog (LOG_NOTICE, "Interrupted by signum: %d.", ret);
    }

    syslog (LOG_NOTICE, "Terminated.");
    closelog();

    return 0;
} // end function: main

ubuntu linux 14.04

( 15212 PID /, .)

/ ( stderr)

SIG_TIMED_WAITER[15212]: Started
SIG_TIMED_WAITER[15212]: EAGAIN: TimedWait complete...
SIG_TIMED_WAITER[15212]: Terminated.

/var/log/syslog:

Jan  7 05:50:07 rkwill-desktop SIG_TIMED_WAITER[15212]: Started
Jan  7 05:54:07 rkwill-desktop SIG_TIMED_WAITER[15212]: EAGAIN: TimedWait complete...
Jan  7 05:54:07 rkwill-desktop SIG_TIMED_WAITER[15212]: Terminated.

: EAGAIN 4 (240 )

+1

Source: https://habr.com/ru/post/1623265/


All Articles