systemctl控制下Linux重启和关机的区别

goucqfw6  于 2023-08-03  发布在  Linux
关注(0)|答案(1)|浏览(90)

rebootpoweroff都Map到/bin/systemctlsystemctl如何控制关机和重启?systemctl在输入rebootpoweroff时,如何获取应该执行哪个命令?

看起来rebootpoweroff都链接到了/bin/systemctl

➜  /usr/bin file /usr/sbin/reboot
/usr/sbin/reboot: symbolic link to /bin/systemctl
➜  /usr/bin file /usr/sbin/poweroff 
/usr/sbin/poweroff: symbolic link to /bin/systemctl
➜  /usr/bin 
➜  /usr/bin 
➜  /usr/bin 
➜  /usr/bin uname -a
Linux mi-OptiPlex-7080 5.15.0-76-generic #83~20.04.1-Ubuntu SMP Wed Jun 21 20:23:31 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux
➜  /usr/bin

字符串

des4xlb0

des4xlb01#

systemctl如何控制关机和重启
当一个命令在Linux下执行时,它会收到参数。有一个特殊的,第一个或“零”参数,这是执行的进程的名称。参见man execve

$ bash -c 'echo $0'
bash
$ strace bash -c 'echo $0' 2>&1 | grep exec
execve("/usr/bin/bash", ["bash", "-c", "echo $0"], 0x7fff48f3f1d0 /* 115 vars */) = 0
                                        ^^^^^^^ - argument 2
                                  ^^ - argument 1
                          ^^^^^ - argument 0
        ^^^^^^^^^^^^^ -  process to execute

字符串
systemctl的C程序只检查第0个参数并比较字符串。在伪代码中:

int main(int argc, char *argv[]) {
    if (strcmp(argv[0], "poweroff") == 0) {
        I_am_poweroff();
    else if (strcmp(argv[0], "reboot") == 0) {
        reboot();
    } etc...
}


在真实的代码中,这里会发生这种情况:https://github.com/systemd/systemd/blob/main/src/systemctl/systemctl.c#L1083:。你可能也对busybox项目感兴趣。

相关问题