Android(安卓)系统调用
16lz
2021-01-26
另外: http://hamer-blog.logdown.com/posts/174690-android-find-kernel-headers-the-source-needs
http://blog.csdn.net/heqiangflytosky/article/details/8941466
http://www.eoeandroid.com/thread-4668-1-1.html
http://blog.chinaunix.net/uid-9078996-id-2010323.html
http://blog.csdn.net/andyhuabing/article/details/7183369
1、Syscall stub:
每个系统调用函数都由一个小的汇编代码段实现(称为”syscall stub”),这是由tools/gensyscalls.py工具自动生成的,它从SYSCALL.TXT中取得输入参数。SYSCALLS.TXT包含了一份要生成的系统调用块列表,和相应的系统调用数字标识符(ARM和X86不一样),以及它的签署。
如果你要修改这个文件,你可能要使用tools/checksyscalls.py工具,检查它里面是否包含官方linux核心头文件,如果有不合法的syscall ids,它会报告错误。
有时,C库函数其实就是一个包装,它内部调用相应的syscall名称,例如,exit函数由C库提供,它调用了_exit()这个syscall stub.
详细内容请参考SYSCALLS.TXT文件。
2、比如要增加消息队列的函数:
(1). 修改 SYSCALL.TXT
增加:
long ipc(unsigned int call, int first, unsigned long second, unsigned long third, void __user *ptr, long fifth) 117
(2). 执行 tools/gensyscalls.py
(3). 以下内容会由 tools/gensyscalls.py 自动产生:
bionic/libc/arch-mips/syscalls.mk
----------------------------------------------------------------------------------------------------------------------------
syscall_src += arch-mips/syscalls/ipc.S
----------------------------------------------------------------------------------------------------------------------------
bionic/libc/include/sys/linux-syscalls.h
----------------------------------------------------------------------------------------------------------------------------
#define _NR_ipc (_NR_SYSCALL_BASE + 117)
----------------------------------------------------------------------------------------------------------------------------
bionic/libc/include/sys/linux-unistd.h
----------------------------------------------------------------------------------------------------------------------------
int ipc (unsigned int call, int first, int second, int third, void* ptr, long fifth);
----------------------------------------------------------------------------------------------------------------------------
(4). 手动编写
bionic/libc/Android.mk
----------------------------------------------------------------------------------------------------------------------------
ifeq ($(TARGET_ARCH),mips)
libc_common_src_files += \
arch-mips/bionic/sys_ipc.c \
----------------------------------------------------------------------------------------------------------------------------
bionic/libc/arch-mips/bionic/sys_ipc.c
----------------------------------------------------------------------------------------------------------------------------
[cpp] view plain copy
- #include<stdarg.h>
- #include<sys/msg.h>
- externintipc(unsignedintcall,intfirst,intsecond,intthird,void*ptr,longfifth);
- intsemget(key_tkey,intnsems,intsemflg)
- {
- returnipc(SEMGET,key,nsems,semflg,(void*)NULL,(long)NULL);
- }
- intsemop(intsemid,structsembuf*sops,size_tnsops)
- {
- returnipc(SEMOP,semid,(int)nsops,0,sops,(long)NULL);
- }
- void*shmat(intshmid,constvoid*shmaddr,intshmflg)
- {
- intrval;
- unsignedlongraddr;
- rval=ipc(SHMAT,shmid,shmflg,(int)&raddr,(void*)shmaddr,(long)NULL);
- if(rval<0)
- return(char*)rval;
- return(char*)raddr;
- }
- intshmctl(intshmid,intcmd,structshmid_ds*buf)
- {
- returnipc(SHMCTL,shmid,cmd,0,buf,(long)NULL);
- }
- intshmget(key_tkey,size_tsize,intshmflg)
- {
- returnipc(SHMGET,key,size,shmflg,(void*)NULL,(long)NULL);
- }
- intmsgctl(intmsqid,intcmd,structmsqid_ds*buf)
- {
- returnipc(MSGCTL,msqid,cmd,0,buf,(long)NULL);
- }
- intshmdt(constvoid*shmaddr)
- {
- returnipc(SHMDT,0,0,0,(void*)shmaddr,(long)NULL);
- }
- intmsgget(key_tkey,intmsgflg)
- {
- returnipc(MSGGET,key,msgflg,0,NULL,(long)NULL);
- }
- ssize_tmsgrcv(intmsqid,void*msgp,size_tmsgsz,longintmsgtyp,intmsgflg)
- {
- structipc_kludgetmp;
- tmp.msgp=msgp;
- tmp.msgtyp=msgtyp;
- returnipc(MSGRCV,msqid,msgsz,msgflg,&tmp,(long)NULL);
- }
- intmsgsnd(intmsqid,constvoid*msgp,size_tmsgsz,intmsgflg)
- {
- returnipc(MSGSND,msqid,msgsz,msgflg,(void*)msgp,(long)NULL);
- }
bionic/libc/arch-mips/syscalls/ipc.S
----------------------------------------------------------------------------------------------------------------------------
/* autogenerated by gensyscalls.py */
#include <sys/linux-syscalls.h>
.text
.globl ipc
.align 4
.ent ipc
ipc:
.set noreorder
.cpload $t9
li $v0, __NR_ipc
syscall
bnez $a3, 1f
move $a0, $v0
j $ra
nop
1:
la $t9,__set_errno
j $t9
nop
.set reorder
.end ipc
----------------------------------------------------------------------------------------------------------------------------
bionic/libc/include/sys/msg.h
----------------------------------------------------------------------------------------------------------------------------
[cpp] view plain copy
- #ifndef_SYS_MSG_H
- #define_SYS_MSG_H
- #include<features.h>
- #define__need_size_t
- #include<stddef.h>
- /*GetcommondefinitionofSystemVstyleIPC.*/
- #include<sys/ipc.h>
- /*Getsystemdependentdefinitionof`structmsqid_ds'andmore.*/
- #include<msq.h>
- /*Definetypesrequiredbythestandard.*/
- #define__need_time_t
- #include<time.h>
- #ifndef__pid_t_defined
- typedef__pid_tpid_t;
- #define__pid_t_defined
- #endif
- #ifndef__ssize_t_defined
- typedef__ssize_tssize_t;
- #define__ssize_t_defined
- #endif
- #defineSEMOP1
- #defineSEMGET2
- #defineSEMCTL3
- #defineSEMTIMEDOP4
- #defineMSGSND11
- #defineMSGRCV12
- #defineMSGGET13
- #defineMSGCTL14
- #defineSHMAT21
- #defineSHMDT22
- #defineSHMGET23
- #defineSHMCTL24
- /*ThefollowingSystemVstyleIPCfunctionsimplementamessagequeue
- system.ThedefinitionisfoundinXPG2.*/
- #ifdef__USE_GNU
- /*Templateforstructtobeusedasargumentfor`msgsnd'and`msgrcv'.*/
- structmsgbuf
- {
- longintmtype;/*typeofreceived/sentmessage*/
- charmtext[1];/*textofthemessage*/
- };
- #endif
- structipc_kludge{
- structmsgbuf__user*msgp;
- longmsgtyp;
- };
- __BEGIN_DECLS
- /*Messagequeuecontroloperation.*/
- externintmsgctl(int__msqid,int__cmd,structmsqid_ds*__buf);
- /*Getmessagesqueue.*/
- externintmsgget(key_t__key,int__msgflg);
- /*Receivemessagefrommessagequeue.
- Thisfunctionisacancellationpointandthereforenotmarkedwith
- __THROW.*/
- externssize_tmsgrcv(int__msqid,void*__msgp,size_t__msgsz,longint__msgtyp,int__msgflg);
- /*Sendmessagetomessagequeue.
- Thisfunctionisacancellationpointandthereforenotmarkedwith
- __THROW.*/
- externintmsgsnd(int__msqid,__constvoid*__msgp,size_t__msgsz,int__msgflg);
- __END_DECLS
- #endif/*sys/msg.h*/
----------------------------------------------------------------------------------------------------------------------------
Create bionic/libc/include/msq.h
[cpp] view plain copy
- #ifndef_SYS_MSG_H
- #error"Neveruse<msq.h>directly;include<sys/msg.h>instead."
- #endif
- #include<sys/types.h>
- /*Defineoptionsformessagequeuefunctions.*/
- #defineMSG_NOERROR010000/*noerrorifmessageistoobig*/
- /*Typesusedinthestructuredefinition.*/
- typedefunsignedshortintmsgqnum_t;
- typedefunsignedshortintmsglen_t;
- /*Structureofrecordforonemessageinsidethekernel.
- Thetype`struct__msg'isopaque.*/
- structmsqid_ds
- {
- structipc_permmsg_perm;/*structuredescribingoperationpermission*/
- __time_tmsg_stime;/*timeoflastmsgsndcommand*/
- __time_tmsg_rtime;/*timeoflastmsgrcvcommand*/
- __time_tmsg_ctime;/*timeoflastchange*/
- msgqnum_tmsg_qnum;/*numberofmessagescurrentlyonqueue*/
- msglen_tmsg_qbytes;/*maxnumberofbytesallowedonqueue*/
- __pid_tmsg_lspid;/*pidoflastmsgsnd()*/
- __pid_tmsg_lrpid;/*pidoflastmsgrcv()*/
- };
更多相关文章
- android,webview增加自定义JS对象,调用过程追踪。
- 【黑马Android】(03)学生管理系统/动态刷新界面
- Java中,使用HTTP Basic进行验证
- 接收系统广播监听网络变化
- android、webService 天气预报demo
- OKhttp上传头像,调用相机相册进行裁剪
- Android调用邮件客户端
- Android(安卓)打开资源raw文件
- 模拟按键操作的几种方式