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),每种类型在每个进程中也只能有一个。设置同类型的定时器会覆盖之前的设置。
相关推荐
黄华SJ520it6 分钟前
二二复制定点裂变双轨商城系统开发:原理、架构与实战指南
运维·小程序·架构·系统开发
生活爱好者!16 分钟前
NAS还能用来开视频会议?docker一键部署MiroTalk SFU
运维·docker·容器
土星云SaturnCloud28 分钟前
智慧温室精准环控:土星云边缘计算设备实现温光水肥本地闭环调节
服务器·人工智能·ai·边缘计算
深爱水瓶 血影S狂风1 小时前
函数调用过程探究
linux·运维·服务器
酷可达拉斯1 小时前
Linux操作系统-磁盘空间使用率100%如何处理?
linux·运维·服务器·云计算·bash
yuezhilangniao1 小时前
AI工具全家桶:从开发到运维,从数据库到产品经理-含魔塔社区简介
运维·数据库·人工智能
xiaoxiangsiyan2 小时前
LNMP + Redis Sentinel 高可用架构部署手册(续)
运维·网络·数据库·redis·缓存·架构·sentinel
AIgorithmGEEK2 小时前
Linux 权限管理:从「Permission Denied」到「畅行无阻」
linux·权限·chmod·粘滞位·umask·文件权限的构成
BullSmall2 小时前
scanoss-engine 本地最简部署 & 扫描命令(Linux/Anolis OS)
linux·运维·服务器
陈聪.3 小时前
Keepalived 高可用集群:从原理到企业级实践
运维