英语

Linux 魔数

此文件是正在使用的魔数的注册表。当您向结构体添加魔数时,您也应该将其添加到此文件中,因为如果各种结构体使用的魔数是唯一的,那将是最好的。

使用魔数保护内核数据结构是一个非常好的主意。 这使您可以运行时检查(a)结构是否已被破坏,或者(b)您是否将错误的结构传递给例程。 后者特别有用——尤其是在您通过 void * 指针传递指向结构的指针时。 例如,tty 代码经常这样做来来回回传递驱动程序特定的和线路规程特定的结构。

使用魔数的方法是在结构体的开头声明它们,就像这样

struct tty_ldisc {
        int     magic;
        ...
};

在您将来增强内核时,请遵循此原则! 它为我节省了无数的调试时间,尤其是在数组被溢出并且数组后面的结构体被覆盖的糟糕情况下。 使用此原则,这些情况可以快速安全地被检测到。

更新日志

                                      Theodore Ts'o
                                      31 Mar 94

The magic table is current to Linux 2.1.55.

                                      Michael Chastain
                                      <mailto:mec@shout.net>
                                      22 Sep 1997

Now it should be up to date with Linux 2.1.112. Because
we are in feature freeze time it is very unlikely that
something will change before 2.2.x. The entries are
sorted by number field.

                                      Krzysztof G. Baranowski
                                      <mailto: kgb@knm.org.pl>
                                      29 Jul 1998

Updated the magic table to Linux 2.5.45. Right over the feature freeze,
but it is possible that some new magic numbers will sneak into the
kernel before 2.6.x yet.

                                      Petr Baudis
                                      <pasky@ucw.cz>
                                      03 Nov 2002

Updated the magic table to Linux 2.5.74.

                                      Fabian Frederick
                                      <ffrederick@users.sourceforge.net>
                                      09 Jul 2003

魔数名称

数字

结构体

文件

PG_MAGIC

‘P’

pg_{read,write}_hdr

include/uapi/linux/pg.h

APM_BIOS_MAGIC

0x4101

apm_user

arch/x86/kernel/apm_32.c

FASYNC_MAGIC

0x4601

fasync_struct

include/linux/fs.h

SLIP_MAGIC

0x5302

slip

drivers/net/slip/slip.h

BAYCOM_MAGIC

19730510

baycom_state

drivers/net/hamradio/baycom_epp.c

HDLCDRV_MAGIC

0x5ac6e778

hdlcdrv_state

include/linux/hdlcdrv.h

KV_MAGIC

0x5f4b565f

kernel_vars_s

arch/mips/include/asm/sn/klkernvars.h

CODA_MAGIC

0xC0DAC0DA

coda_file_info

fs/coda/coda_fs_i.h

YAM_MAGIC

0xF10A7654

yam_port

drivers/net/hamradio/yam.c

CCB_MAGIC

0xf2691ad2

ccb

drivers/scsi/ncr53c8xx.c

QUEUE_MAGIC_FREE

0xf7e1c9a3

queue_entry

drivers/scsi/arm/queue.c

QUEUE_MAGIC_USED

0xf7e1cc33

queue_entry

drivers/scsi/arm/queue.c

NMI_MAGIC

0x48414d4d455201

nmi_s

arch/mips/include/asm/sn/nmi.h