1,什么是封装例程?
系统调用是内核实现的,内核向用户态提供服务,内核向用户态提供系统调用的接口是一个软中断。
也就是int 0x80.如果用户直接调用系统调用会很麻烦。下面就举个直接调用系统调用的例子。用汇编实现的。
- # hello.s ----> intel汇编的注释用的; 而ATT用的#
- # display a string "Hello, world."
- .section .rodata
- msg:
- .ascii "Hello, world.\n"
- .section .text
- .globl _start
- _start:
- movl $2, %eax #调用fork系统调用
- int $0x80
- movl $4, %eax # system call 系统调用号(sys_write)
- movl $1, %ebx # file descriptor 参数一:文件描述符(stdout)
- movl $msg, %ecx # string address 参数二:要显示的字符串
- movl $14, %edx # string length 参数三:字符串长度
- int $0x80 # 调用内核功能
- movl $1, %eax # 系统调用号(sys_exit)
- movl $0, %ebx # 参数一:退出代码
- int $0x80 # 调用内核功能
这就需要对系统调用进行封装,便于用户调用,比如说fork(),getpid(),这些函数都是libc库中的函数
都对系统调用进行了封装。而且都是遵循POSIX标准的。
---------------------------------------------------------------------------------------------
2,什么是POSIX?
POSIX是一套操作系统标准,它是随着UNIX标准化而诞生的。linux是遵循POSIX标准的操作系统,如果
linux遵循POSIX标准的话,UNIX上的应用程序就可以顺利移植到linux上来了。形象点说也就是linux和UNIX
很多函数名字及其参数都定义的是一样的。
---------------------------------------------------------------------------------------------
3,旧方式封装(淘汰了),__syscall0() ~ __syscall6()
这几个宏实际上是利用了系统调用参数的个数不能超个6个来进行分类的。为了内核的安全考虑。从内核2.6.18开始该宏就从头文件中删除了,其实完全也可以自己加入到 指定的头文件中去。该宏被syscall()取代了,syscall()是glibc中的函数。而上面的_syscall()宏是不依赖于库的。
- #define __syscall_return(type, res) \
- do { \
- if ((unsigned long)(res) >= (unsigned long)(-MAX_ERRNO)) { \
- res = -1; \
- } \
- return (type) (res); \
- } while (0)
- #define _syscall0(type,name) \
- type name(void) \
- { \
- long __res; \
- __asm__ volatile ("int $0x80" \
- : "=a" (__res) \
- : "0" (__NR_##name)); \
- __syscall_return(type,__res); \
- }
- #define _syscall1(type,name,type1,arg1) \
- type name(type1 arg1) \
- { \
- long __res; \
- __asm__ volatile ("push %%ebx ; movl %2,%%ebx ; int $0x80 ; pop %%ebx" \
- : "=a" (__res) \
- : "0" (__NR_##name),"ri" ((long)(arg1)) : "memory"); \
- __syscall_return(type,__res); \
- }
- #define _syscall2(type,name,type1,arg1,type2,arg2) \
- type name(type1 arg1,type2 arg2) \
- { \
- long __res; \
- __asm__ volatile ("push %%ebx ; movl %2,%%ebx ; int $0x80 ; pop %%ebx" \
- : "=a" (__res) \
- : "0" (__NR_##name),"ri" ((long)(arg1)),"c" ((long)(arg2)) \
- : "memory"); \
- __syscall_return(type,__res); \
- }
- #define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
- type name(type1 arg1,type2 arg2,type3 arg3) \
- { \
- long __res; \
- __asm__ volatile ("push %%ebx ; movl %2,%%ebx ; int $0x80 ; pop %%ebx" \
- : "=a" (__res) \
- : "0" (__NR_##name),"ri" ((long)(arg1)),"c" ((long)(arg2)), \
- "d" ((long)(arg3)) : "memory"); \
- __syscall_return(type,__res); \
- }
- #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
- type name (type1 arg1, type2 arg2, type3 arg3, type4 arg4) \
- { \
- long __res; \
- __asm__ volatile ("push %%ebx ; movl %2,%%ebx ; int $0x80 ; pop %%ebx" \
- : "=a" (__res) \
- : "0" (__NR_##name),"ri" ((long)(arg1)),"c" ((long)(arg2)), \
- "d" ((long)(arg3)),"S" ((long)(arg4)) : "memory"); \
- __syscall_return(type,__res); \
- }
- #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
- type5,arg5) \
- type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \
- { \
- long __res; \
- __asm__ volatile ("push %%ebx ; movl %2,%%ebx ; movl %1,%%eax ; " \
- "int $0x80 ; pop %%ebx" \
- : "=a" (__res) \
- : "i" (__NR_##name),"ri" ((long)(arg1)),"c" ((long)(arg2)), \
- "d" ((long)(arg3)),"S" ((long)(arg4)),"D" ((long)(arg5)) \
- : "memory"); \
- __syscall_return(type,__res); \
- }
- #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
- type5,arg5,type6,arg6) \
- type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5,type6 arg6) \
- { \
- long __res; \
- struct { long __a1; long __a6; } __s = { (long)arg1, (long)arg6 }; \
- __asm__ volatile ("push %%ebp ; push %%ebx ; movl 4(%2),%%ebp ; " \
- "movl 0(%2),%%ebx ; movl %1,%%eax ; int $0x80 ; " \
- "pop %%ebx ; pop %%ebp" \
- : "=a" (__res) \
- : "i" (__NR_##name),"0" ((long)(&__s)),"c" ((long)(arg2)), \
- "d" ((long)(arg3)),"S" ((long)(arg4)),"D" ((long)(arg5)) \
- : "memory"); \
- __syscall_return(type,__res); \
- }
---------------------------------------------------------------------------------------------
4,新方式封装。(现在的封装方式)
现在对系统调用的封装都是用的syscall函数,该函数是不定参数,很好用,可以调用所有的系统调用。
- 1 #include <linux/unistd.h>
- 2 #include <syscall.h>
- 3 #include <sys/types.h>
- 4 #include <stdio.h>
- 6
- 7
- 8 int main(void)
- 9 {
- 10 long pid1;
- 11 pid1 = syscall(SYS_getpid); //getpid
- 12 printf("pid = %ld\n",pid1);
- 13 return 0;
- 14 }
---------------------------------------------------------------------------------------------
5,问题:我自己添加了一个系统调用,想在用户态下测试下,代码如下:
- 1 #include <linux/unistd.h>
- 2 #include <syscall.h>
- 3 #include <sys/types.h>
- 4 #include <stdio.h>
- 5
- 6
- 7 int main(void)
- 8 {
- 9 long n = 0;
- 10 n = syscall(SYS_mysyscall,190);
- 11 return 0;
- 12 }
该程序会提示“SYS_mysyscall”没有定义。
解决办法:
1,/usr/include/bits/syscall.h文件中添加:#define SYS_mysyscall __NR_mysyscall
2,/usr/include/i386-linux-gnu/asm/unistd_32文件末尾添加:
#define __NR_mysyscall 345
这样,你添加的系统调用和系统本身自带的系统调用的用户态使用上就一模一样了。
---------------------------------------------------------------------------------------------
阅读(3707) | 评论(0) | 转发(0) |