Skip to content

[Feature] sigaction support siginfo #10056

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wycwyhwyq opened this issue Feb 28, 2025 · 0 comments
Open

[Feature] sigaction support siginfo #10056

wycwyhwyq opened this issue Feb 28, 2025 · 0 comments

Comments

@wycwyhwyq
Copy link
Contributor

Describe problem solved by the proposed feature

sigaction信号处理支持siginfo。多个软件timer就可以使用一个信号处理函数,通过sigval进行区分。同时某些可能使用到信号的设备驱动也能传递更多的参数到用户层。

Describe your preferred solution

比如下面的例子

#include <signal.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>

#define CLOCKID CLOCK_REALTIME
#define SIG SIGRTMIN

#define errExit(msg)        \
    do {                    \
        perror(msg);        \
        exit(EXIT_FAILURE); \
    } while (0)

static void print_siginfo(siginfo_t* si)
{
    timer_t* tidp;

    tidp = si->si_value.sival_ptr;

    printf("si_signo = %d\n", si->si_signo);
    printf("si_code = %d\n", si->si_code);
    printf("si_errno = %d\n", si->si_errno);
    printf("timer ID is %#lx\n", (uintmax_t)*tidp);
}

static void handler(int sig, siginfo_t* si, void* uc)
{
    printf("Caught signal %d\n", sig);
    print_siginfo(si);
}

int main(int argc, char* argv[])
{
    timer_t timerid;
    struct sigevent sev;
    struct sigaction sa;
    struct itimerspec its;

    /* Establish handler for timer signal. */
    printf("Establishing handler for signal %d\n", SIG);
    sa.sa_flags = SA_SIGINFO;
    sa.sa_sigaction = handler;
    sigemptyset(&sa.sa_mask);
    if (sigaction(SIG, &sa, NULL) == -1)
        errExit("sigaction");

    /* Create the timer. */
    sev.sigev_notify = SIGEV_SIGNAL;
    sev.sigev_signo = SIG;
    sev.sigev_value.sival_ptr = &timerid;
    if (timer_create(CLOCKID, &sev, &timerid) == -1)
        errExit("timer_create");

    printf("timer ID is %#lx\n", (uintmax_t)timerid);

    /* Start the timer. */
    its.it_value.tv_sec = 1;
    its.it_value.tv_nsec = 0;
    its.it_interval.tv_sec = 0;
    its.it_interval.tv_nsec = 0;
    if (timer_settime(timerid, 0, &its, NULL) == -1)
        errExit("timer_settime");

    sleep(2);

    exit(EXIT_SUCCESS);
}

Describe possible alternatives

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant