Linux reboot() 系统调用的幻数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4808748/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 02:38:43  来源:igfitidea点击:

Magic numbers of the Linux reboot() system call

linuxlinux-kernelsystem-calls

提问by Wei Hu

The Linux Programming Interfacehas an exercise in Chapter 3 that goes like this:

Linux 编程接口在第 3 章中有一个练习,如下所示:

When using the Linux-specific reboot() system call to reboot the system, the second argument, magic2, must be specified as one of a set of magic numbers (e.g., LINUX_REBOOT_MAGIC2). What is the significance of these numbers? (Converting them to hexadecimal provides a clue.)

当使用 Linux 特定的 reboot() 系统调用重新启动系统时,必须将第二个参数 magic2 指定为一组幻数之一(例如,LINUX_REBOOT_MAGIC2)。这些数字有什么意义?(将它们转换为十六进制提供了一个线索。)

The man page tells us magic2can be one of LINUX_REBOOT_MAGIC2 (672274793), LINUX_REBOOT_MAGIC2A (85072278), LINUX_REBOOT_MAGIC2B (369367448), or LINUX_REBOOT_MAGIC2C (537993216). I failed to decipher their meaning in hex. I also looked at /usr/include/linux/reboot.h, which didn't give any helpful comment either.

手册页告诉我们magic2可以是 LINUX_REBOOT_MAGIC2 (672274793)、LINUX_REBOOT_MAGIC2A (85072278)、LINUX_REBOOT_MAGIC2B (369367448) 或 LINUX_REBOOT53799C2 之一。我没能破译它们的十六进制含义。我也看了/usr/include/linux/reboot.h,也没有给出任何有用的评论。

I then searched in the kernel's source code for sys_reboot's definition. All I found was a declaration in a header file.

然后我在内核的源代码中搜索了sys_reboot的定义。我发现的只是头文件中的声明。

Therefore, my first question is, what is the significance of these numbers? My second question is, where's sys_reboot's definition, and how did you find it?

因此,我的第一个问题是,这些数字的意义是什么?我的第二个问题是,sys_reboot定义在哪里,你是怎么找到的?

EDIT: I found the definition in kernel/sys.c. I only grepped for sys_reboot, and forgot to grep for the MAGIC numbers. I figured the definition must be hidden behind some macro trick, so I looked at the System.mapfile under /boot, and found it next to ctrl_alt_del. I then grepped for that symbol, which led me to the correct file. If I had compiled the kernel from source code, I could try to find which object file defined the symbol, and go from there.

编辑:我在kernel/sys.c. 我只搜索了sys_reboot,忘记了搜索 MAGIC 数字。我想这个定义一定隐藏在一些宏技巧后面,所以我查看了System.map下的文件/boot,并在ctrl_alt_del. 然后我搜索了那个符号,这让我找到了正确的文件。如果我从源代码编译内核,我可以尝试找到定义符号的目标文件,然后从那里开始。

采纳答案by araqnid

Just a guess, but those numbers look more interesting in hex:

只是猜测,但这些数字在十六进制中看起来更有趣:

672274793 = 0x28121969
 85072278 = 0x05121996
369367448 = 0x16041998
537993216 = 0x20112000

Developers' or developers' children's birthdays?

开发者或开发者孩子的生日?

Regarding finding the syscall implementation, I did a git grep -n LINUX_REBOOT_MAGIC2and found the definition in kernel/sys.c. The symbol sys_rebootis generated by the SYSCALL_DEFINE4(reboot, ...gubbins, I suspect.

关于查找系统调用的实现,我做了一个git grep -n LINUX_REBOOT_MAGIC2并在kernel/sys.c中找到了定义。我怀疑这个符号sys_reboot是由SYSCALL_DEFINE4(reboot, ...gubbins生成的。

回答by Ritwik Dey

It's the birthday of Linus Torvalds (The developer of the Linux kernel and the Git version control) and his 3 daughters. works as magic numbers to reboot the system.

这是 Linus Torvalds(Linux 内核和 Git 版本控制的开发者)和他的 3 个女儿的生日。用作重新启动系统的幻数。

http://en.wikipedia.org/wiki/Linus_Torvalds

http://en.wikipedia.org/wiki/Linus_Torvalds