Linux: signal: SIGALRM; alarm: ITIMER_REAL

Openssh使用alarm接口实现登陆超时警告的功能;这个接口的功能非常单一,有一个全局的变量来记录这个ITIMER_REAL;

https://www.man7.org/linux/man-pages/man2/alarm.2.html

https://www.man7.org/linux/man-pages/man2/setitimer.2.html

c 复制代码
	/*
	 * We don't want to listen forever unless the other side
	 * successfully authenticates itself.  So we set up an alarm which is
	 * cleared after successful authentication.  A limit of zero
	 * indicates no limit. Note that we don't set the alarm in debugging
	 * mode; it is just annoying to have the server exit just when you
	 * are about to discover the bug.
	 */
	ssh_signal(SIGALRM, grace_alarm_handler);
	if (!debug_flag)
		alarm(options.login_grace_time);

如果登陆失败,就清理alarm

c 复制代码
	/*
	 * Cancel the alarm we set to limit the time taken for
	 * authentication.
	 */
	alarm(0);

glibc的实现

c 复制代码
/* Schedule an alarm.  In SECONDS seconds, the process will get a SIGALRM.
   If SECONDS is zero, any currently scheduled alarm will be cancelled.
   The function returns the number of seconds remaining until the last
   alarm scheduled would have signaled, or zero if there wasn't one.
   There is no return value to indicate an error, but you can set `errno'
   to 0 and check its value after calling `alarm', and this might tell you.
   The signal may come late due to processor scheduling.  */
unsigned int
alarm (unsigned int seconds)
{
  struct itimerval old, new;
  unsigned int retval;

  new.it_interval.tv_usec = 0;
  new.it_interval.tv_sec = 0;
  new.it_value.tv_usec = 0;
  new.it_value.tv_sec = (long int) seconds;
  if (__setitimer (ITIMER_REAL, &new, &old) < 0)

详细的解释:ITIMER_REAL

https://www.man7.org/linux/man-pages/man2/setitimer.2.html

  1. alarm() 和 setitimer()
    级别: 进程级别 (Process-level)。
    它们设置的定时器是针对整个进程的。当定时器到期时,内核会向整个进程发送相应的信号(SIGALRM, SIGVTALRM, SIGPROF)。
    如果进程是多线程的,这些信号会发送给进程中的任意一个线程(通常是主线程,除非有特定的信号处理配置)。
    数量: 每个进程只有一个。
    alarm() 只能设置一个实时定时器,新的 alarm() 调用会覆盖旧的。
    setitimer() 有三种类型 (ITIMER_REAL, ITIMER_VIRTUAL, ITIMER_PROF),每种类型在每个进程中也只能有一个。设置同类型的定时器会覆盖之前的设置。
相关推荐
齐齐大魔王11 分钟前
OpenSSL 原理
运维·网络·nginx·ssh·ssl
流浪00118 分钟前
Linux系统篇(一):从零入门操作系统:冯诺依曼体系到进程的完整理解
linux·运维·服务器
STDD24 分钟前
Node-RED 自托管部署指南:打造可视化 IoT 自动化平台
运维·物联网·自动化
大湿兄啊啊啊28 分钟前
MID360S调试
java·服务器·前端
土星云SaturnCloud29 分钟前
基于边缘计算的智慧停车场AI算力评估与SE110S-WA32部署方案
服务器·人工智能·ai·边缘计算
杜子不疼.30 分钟前
Agent Skills 的演进治理与 Swarm Skills 自演进
服务器·数据库·microsoft
hj2862511 小时前
Linux学习方法论 + 系统安全加固与性能优化 完整版笔记(含案例)
运维
刘某的Cloud1 小时前
硬链接 和 软链接 区别
运维·系统·硬链接·软链接
jiayong231 小时前
harness 与 hermes-agent 扩展性、安全与运维
运维·人工智能·安全·ai·架构·智能体·harness
老码观察1 小时前
数环通iPaaS架构设计的结构化与模块化方法论——从高内聚低耦合到工程落地的完整指南
java·服务器·网络