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),每种类型在每个进程中也只能有一个。设置同类型的定时器会覆盖之前的设置。
相关推荐
Web3探索者4 小时前
可视化服务器管理和传统命令行区别是什么?新手教程:Linux 运维到底该用图形界面还是 SSH 命令行?
linux·ssh
zylyehuo6 小时前
Linux系统中网线与USB网络共享冲突
linux
荣--9 小时前
一键部署不是为了省时间 —— 它是把"买来的 PaaS"变成"自己的平台"的拐点
运维·zabbix·工程化·一键部署·平台化·边界设计
江华森9 小时前
动手实战学 Docker — 从零到集群编排完全指南
运维
Avan_菜菜1 天前
FRP 内网穿透完整实战:从 HTTP 映射到 HTTPS 自签代理
运维·nginx·https
Sokach10151 天前
Linux Shell 脚本从零到能用:一个新手的一天学习总结
linux
SelectDB2 天前
Litefuse 开源并推出单进程轻量模式,25 秒就能跑起来的 Agent 可观测与评估平台
运维·后端·自动化运维
AlfredZhao2 天前
Docker 容器时区不对,`timedatectl` 不存在怎么办?
linux·timezone
zzzzzz3103 天前
9K Star 炸裂开源!这个 C 语言写的代码知识图谱,把 Linux 内核索引压缩到了 3 分钟
linux·服务器·sql
XIAOHEZIcode3 天前
Linux系统鼠标偏移常见原因以及修复方案
linux·运维·游戏