【问题标题】:SIGACTION handler to repeat loading after interruptionSIGACTION 处理程序在中断后重复加载
【发布时间】:2021-02-15 07:01:42
【问题描述】:

你好 stackoverflow 家族,

我正在做一个 uni 任务来制作一个 linux 程序来读取密码并保护它,如果用户在此期间中断密码,则再次“重新读取”​​密码。

这是我用于捕获处理程序的代码。

void catch_suspend(int sig_num)
{
    printf("\nSuspending execution...\n");
    fflush(stdout);

    echo_on(YES);           // re-enable echo mode

    raise(SIGSTOP); // stop self

    // we'll get back here when the process is resumed
    printf("Resuming execution...\n");

    echo_on(NO);            // disable echo mode again

    printf("Password: ");       // reproduce the prompt
    fflush(stdout);
}

这里是主程序

int main(int argc, char *argv[])
{
#define MAX_SIZE 30
    char user[MAX_SIZE];    // user name supplied by the user
    char passwd[MAX_SIZE];  // password supplied by the user

    sigset_t sigs;
    struct sigaction sa_new;

    memset(&sa_new, 0, sizeof(sa_new)); // initialization to zeros
    sa_new.sa_handler = catch_suspend;      // set handler
    sigemptyset(&sa_new.sa_mask);       // mask: empty set
    sigaddset(&sa_new.sa_mask, SIGINT); // mask: add SIGINT
    sigaddset(&sa_new.sa_mask, SIGQUIT);    // mask: add SIGQUIT
    sa_new.sa_flags = 0;            // no flags

    printf("Username: ");       // prompt the user for a user name
    fflush(stdout);

    fgets(user, MAX_SIZE, stdin);   // wait for input

    sigaction(SIGTSTP, &sa_new, &sa_old);   // set the handler for SIGTSTP and get old handler setting

    printf("Password: ");       // prompt the user for a password
    fflush(stdout);

    echo_on(NO);            // set input to no-echo mode
    fgets(passwd, MAX_SIZE, stdin); // get the user input
    echo_on(YES);           // re-enable echo on input

    printf("\n");           // the Enter pressed by the user was not echoed
    fflush(stdout);

    // verify the password (\n is stored, don't compare it)
    etc...

    return 0;

我还应该阻止除 SIGINT 和 SIGQUIT 之外的所有信号,不确定我是否正确使用了掩码。我现在遇到的问题是:在密码读取过程中中断后,进程停止。没关系。但是在我使用“fg”命令继续之后,我的处理程序只写输出并且程序结束,但我需要它再次运行 fgets(passwd, MAX_SIZE, stdin) (它停止的地方) .

我想我可能没有正确设置 old_action,但我读过的手册并没有真正让我更清楚。谁能伸出援手?

【问题讨论】:

    标签: linux signals interrupt-handling sigaction


    【解决方案1】:

    好的,我终于想通了,我会把它留在这里给遇到同样问题的人。

    我没有设置sigaction标志如下:

    sa_new.sa_flags = SA_RESTART;
    

    此标志控制在某些原语(例如打开、读取或写入)期间传递信号时发生的情况,并且信号处理程序正常返回。有两种选择:库函数可以恢复,或者它可以返回失败并返回错误代码 EINTR。

    选择由所传递的特定类型信号的 SA_RESTART 标志控制。如果设置了标志,则从处理程序返回恢复库函数。如果标志是明确的,从处理程序返回会使函数失败

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-11
      • 2013-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-26
      • 2019-11-10
      • 1970-01-01
      相关资源
      最近更新 更多