你可以修改内核源代码,比如添加个系统调用:
kernel/sys.c:SYSCALL_DEFINE0(hello) {
printk(KERN_EMERG "hello\n")
return 123456
}
然后再注册一下:
include/linux/syscalls.h:asmlinkage long sys_hello(void)
arch/x86/syscalls/syscall_64.tbl:
1025 64 hello sys_hello
编译下内核,安装并重新启动就可以使用这个系统调用了:(用syscall()或者嵌入汇编都可以)
#include <stdio.h>int main () {
//int a = syscall (1025)
long a=97
asm (
"movq $0x0401, %%rax\n"
"syscall\n"
"movq %%rax, %0\n"
: "=r"(a)
)
printf ("%d\n", a)
return 0
}
(以上代码在内核版本3.12.5 x86_64上通过)
以 bash shell 为例, 其它 shell 基本类似, 请自行学习.
bash 版本:GNU bash, 版本 4.2.37(1)-release (x86_64-pc-linux-gnu)
bash manual 中说明了 shell 命令执行之前经历的过程:
此命令名是否含有 "/", 如果有, 执行命令, 如果无, 进行下一步.
此命令名是否为 shell 函数名, 如果是, 执行函数, 如果不是, 进行下一步.
此命令名是否为 shell 内置命令名, 如果是, 执行内置命令, 如果不是, 进行下一步.
此命令名是否为环境变量 PATH 中的路径下的可执行文件名, 如果是, 执行命令, 如果不是, 进行下一步.
当前 shell 是否存在 shell 函数 "command_not_found_handle", 如果有, 它把原命令以及参数当作自己的参数然后执行, 如果没有, shell 输出错误信息, 然后返回退出状态 127.
所以, 要执行当前目录下的程序(当前目录不在环境变量 PATH 中), 直接输入文件名是不可以的, 唯一的方法是带上 "/", 而带有 "/" 的路径表示形式有很多, 其中最简单的就是 "./filename".
以下是 bash manual 中, 关于命令执行的一部分:
COMMAND EXECUTIONAfter a command has been split into words, if it results in a simple command and an optional list of argu‐
ments, the following actions are taken.
If the command name contains no slashes, the shell attempts to locate it. If there exists a shell function
by that name, that function is invoked as described above in FUNCTIONS. If the name does not match a func‐
tion, the shell searches for it in the list of shell builtins. If a match is found, that builtin is invoked.
If the name is neither a shell function nor a builtin, and contains no slashes, bash searches each element of
the PATH for a directory containing an executable file by that name. Bash uses a hash table to remember the
full pathnames of executable files (see hash under SHELL BUILTIN COMMANDS below). A full search of the
directories in PATH is performed only if the command is not found in the hash table. If the search is unsuc‐
cessful, the shell searches for a defined shell function named command_not_found_handle. If that function
exists, it is invoked with the original command and the original command's arguments as its arguments, and
the function's exit status becomes the exit status of the shell. If that function is not defined, the shell
prints an error message and returns an exit status of 127.
If the search is successful, or if the command name contains one or more slashes, the shell executes the
named program in a separate execution environment. Argument 0 is set to the name given, and the remaining
arguments to the command are set to the arguments given, if any.
If this execution fails because the file is not in executable format, and the file is not a directory, it is
assumed to be a shell script, a file containing shell commands. A subshell is spawned to execute it. This
subshell reinitializes itself, so that the effect is as if a new shell had been invoked to handle the script,
with the exception that the locations of commands remembered by the parent (see hash below under SHELL
BUILTIN COMMANDS) are retained by the child.
If the program is a file beginning with #!, the remainder of the first line specifies an interpreter for the
program. The shell executes the specified interpreter on operating systems that do not handle this exe‐
cutable format themselves. The arguments to the interpreter consist of a single optional argument following
the interpreter name on the first line of the program, followed by the name of the program, followed by the
command arguments, if any.
本人水平有限, 欢迎批评指正.
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)