What is magic number in linux

what is the purpose of the magic number in linux reboots? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.

This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.

On the Wikipedia page for Linus Torvalds, it states that: ‘The Linux kernel’s reboot system call accepts their dates of birth (written in hexadecimal) as magic values’. And documentation for the Linux reboot function shows that it needs two magic numbers, ‘magic’ and ‘magic2’ too reboot. What is the purpose of these magic numbers? Why can’t the function operate without them? Thanks

1 Answer 1

A quick google of your question yields the answer;

In order to prevent the reboot() system call being called inadvertently, you must pass it a pair of magic values in the first two parameters. The first magic value is 0xfee1dead. The second magic value is one of:

$ grep LINUX_REBOOT_MAGIC include/linux/*.h include/linux/reboot.h:#define LINUX_REBOOT_MAGIC1 0xfee1dead include/linux/reboot.h:#define LINUX_REBOOT_MAGIC2 672274793 include/linux/reboot.h:#define LINUX_REBOOT_MAGIC2A 85072278 include/linux/reboot.h:#define LINUX_REBOOT_MAGIC2B 369367448 include/linux/reboot.h:#define LINUX_REBOOT_MAGIC2C 537993216 

These are dates of significance to Linus; hint you can use perl to convert them into dates:

$ perl -e 'print localtime(672274793). "\n";' 

This is the date that Linus first started writing Linux. The significance of other dates is left as an exercise for the reader 😉

Источник

Linux magic numbers¶

This file is a registry of magic numbers which are in use. When you add a magic number to a structure, you should also add it to this file, since it is best if the magic numbers used by various structures are unique.

Читайте также:  Установка принтера pantum p2207 linux

It is a very good idea to protect kernel data structures with magic numbers. This allows you to check at run time whether (a) a structure has been clobbered, or (b) you’ve passed the wrong structure to a routine. This last is especially useful — particularly when you are passing pointers to structures via a void * pointer. The tty code, for example, does this frequently to pass driver-specific and line discipline-specific structures back and forth.

The way to use magic numbers is to declare them at the beginning of the structure, like so:

Please follow this discipline when you are adding future enhancements to the kernel! It has saved me countless hours of debugging, especially in the screwy cases where an array has been overrun and structures following the array have been overwritten. Using this discipline, these cases get detected quickly and safely.

Theodore Ts'o 31 Mar 94 The magic table is current to Linux 2.1.55. Michael Chastain 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 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 03 Nov 2002 Updated the magic table to Linux 2.5.74. Fabian Frederick 09 Jul 2003

Источник

Linux magic numbers¶

This file is a registry of magic numbers which are in use. When you add a magic number to a structure, you should also add it to this file, since it is best if the magic numbers used by various structures are unique.

Читайте также:  Exec with find in linux

It is a very good idea to protect kernel data structures with magic numbers. This allows you to check at run time whether (a) a structure has been clobbered, or (b) you’ve passed the wrong structure to a routine. This last is especially useful — particularly when you are passing pointers to structures via a void * pointer. The tty code, for example, does this frequently to pass driver-specific and line discipline-specific structures back and forth.

The way to use magic numbers is to declare them at the beginning of the structure, like so:

Please follow this discipline when you are adding future enhancements to the kernel! It has saved me countless hours of debugging, especially in the screwy cases where an array has been overrun and structures following the array have been overwritten. Using this discipline, these cases get detected quickly and safely.

Theodore Ts'o 31 Mar 94 The magic table is current to Linux 2.1.55. Michael Chastain 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 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 03 Nov 2002 Updated the magic table to Linux 2.5.74. Fabian Frederick 09 Jul 2003

Источник

Linux magic numbers¶

This file is a registry of magic numbers which are in use. When you add a magic number to a structure, you should also add it to this file, since it is best if the magic numbers used by various structures are unique.

Читайте также:  Linux fdisk отказано в доступе

It is a very good idea to protect kernel data structures with magic numbers. This allows you to check at run time whether (a) a structure has been clobbered, or (b) you’ve passed the wrong structure to a routine. This last is especially useful — particularly when you are passing pointers to structures via a void * pointer. The tty code, for example, does this frequently to pass driver-specific and line discipline-specific structures back and forth.

The way to use magic numbers is to declare them at the beginning of the structure, like so:

Please follow this discipline when you are adding future enhancements to the kernel! It has saved me countless hours of debugging, especially in the screwy cases where an array has been overrun and structures following the array have been overwritten. Using this discipline, these cases get detected quickly and safely.

Theodore Ts'o 31 Mar 94 The magic table is current to Linux 2.1.55. Michael Chastain 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 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 03 Nov 2002 Updated the magic table to Linux 2.5.74. Fabian Frederick 09 Jul 2003

Источник

Оцените статью
Adblock
detector