应用程序如何直接调用一个系统调用, 不经过C库?

通常应用程序是怎么访问系统调用的?

应用程序通常不会直接访问系统调用, 一般都是通过C库来访问系统调用.

我们通常说的, 写个应用程序, 调某个系统调用, 其实也不是真的直接调用系统调用, 而是调用C库(一般是glbic)封装的函数, 而这个封装的函数的函数名跟系统调用名称相同, 比如写个程序,打开某个文件, 将其内容输出来, 这里面我们看似调用了open(), read(), write()这些"系统调用", 实际也是glibc封装过的, 跟Linux相应的系统调用, 同名的函数, 而非真的系统调用本身.

Linux操作系统的API接口通常是以C标准库的方式提供的, 比如Linux中的libc库. C库提供了POSIX的绝大部分的API的实现, 同时也为内核提供的每个系统调用封装了相应的函数, 并且系统调用和C库封装的函数名称通常是相同的. 例如, open系统调用在C库的函数也是open函数.

应用程序怎么直接访问系统调用?

直接访问的话, 可以通过调用syscall()来实现:

SYNOPSIS
       #define _GNU_SOURCE         /* See feature_test_macros(7) */
       #include <unistd.h>
       #include <sys/syscall.h>   /* For SYS_xxx definitions */

       long syscall(long number, ...);

DESCRIPTION
       syscall()  is  a  small library function that invokes the system call whose assembly language interface has
       the specified number with the specified arguments. Employing syscall() is useful, for example, when invok‐
       ing a system call that has no wrapper function in the C library.

       syscall()  saves  CPU  registers before making the system call, restores the registers upon return from the
       system call, and stores any error code returned by the system call in errno(3) if an error occurs.

       Symbolic constants for system call numbers can be found in the header file <sys/syscall.h>.

syscall()函数可以直接调用一个系统调用, 第一个参数是系统调用号码, "…"是可变参数, 用来传递参数到内核.

举例

以open为例, open系统调用的调用号是5

[me@ukylin runninglinuxkernel_4.0]$ grep open arch/arm/include/uapi/asm/unistd.h
#define __NR_open                       (__NR_SYSCALL_BASE+  5)
#define __NR_mq_open                    (__NR_SYSCALL_BASE+274)
#define __NR_openat                     (__NR_SYSCALL_BASE+322)
#define __NR_perf_event_open            (__NR_SYSCALL_BASE+364)
#define __NR_open_by_handle_at          (__NR_SYSCALL_BASE+371)
[me@ukylin runninglinuxkernel_4.0]$

那么, 可以在应用程序中, 用如下代码直接调用open系统调用

/* example of calling open system call directly, not through libc */
#define NR_OPEN 5
syscall(NR_OPEN, filename, flags, mode);

参考

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值