Three ways to implement Linux system call _linux

Source: Internet
Author: User

System call is a set of interfaces that the operating system provides to interact with hardware devices such as CPUs, disks, printers, and so on, for processes running in user state. When a system call is required by a user process, the CPU switches to the kernel state through a soft interrupt to begin executing the kernel system call function. Here are three ways in which system calls occur under Linux:

Library functions provided through the GLIBC
glibc is the Open source standard C library used under Linux, which is the LIBC library, the runtime library, which is published by GNU. GLIBC provides programmers with a rich API (application programming Interface), in addition to user-state services such as string processing, mathematical operations, and most importantly encapsulates the system services provided by the operating system, the encapsulation of system calls. So what is the relationship between the system call API provided by GLIBC and kernel-specific system calls?

    • Typically, each particular system call corresponds to at least one GLIBC encapsulated library function, such as a system-supplied open file system call Sys_open corresponding to the open function in glibc;
    • Second, glibc a separate API may invoke multiple system calls, such as the glibc provided by the printf function, which invokes system calls such as Sys_open, Sys_mmap, Sys_write, Sys_close, and so on;
    • In addition, multiple APIs may only correspond to the same system call, such as GLIBC implemented under malloc, Calloc, free, and other functions to allocate and release memory, using the kernel's SYS_BRK system calls.

For example, we use the chmod function provided by glibc to change the property of the file etc/passwd to 444:

#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <stdio.h >

int Main ()
{
  int rc;

  rc = chmod ("/etc/passwd", 0444);
  if (rc = = 1)
    fprintf (stderr, "chmod failed, errno =%d\n", errno);
  else
    printf ("chmod success!\n");
  return 0;
}

Under the common user compiles the application, the output result is:

Chmod failed, errno = 1
The value returned by the system call above is-1, indicating that the system call failed with the error code of 1, with the following error code description in the/usr/include/asm-generic/errno-base.h file:

#define EPERM 1/* Operation not permitted * *
That is, no permission to do this operation, we are normal user rights can not modify the properties of the/etc/passwd file, the result is correct.

Second, use Syscall direct call
There are many benefits to using the method above, first you don't need to know more details, such as the chmod system call number, you only need to know the prototype of the API provided by glibc, and secondly, this method has better portability, you can easily transfer the program to other platforms, or replace the GLIBC library with other libraries , the program only needs to make a small change.
But a bit of a disadvantage is that if glibc does not encapsulate a system call from a kernel, I have no way to invoke the system call through the method above. If I added a system call by compiling the kernel myself, then glibc could not have the encapsulation API for your new system call, which we can call directly using the Syscall function provided by GLIBC. The function is defined in the Unistd.h header file, and the function prototype is as follows:

Long int syscall (long int sysno, ...)

    • The Sysno is the system call number, and each system call has a unique system call number to identify it. There is a possible macro definition of the system call number in the sys/syscall.h.
    • ... For the remaining variable length parameters, for system calls with the parameters, according to the system call different, can take 0~5 parameters, if more than the specific system calls can take parameters, redundant parameters are ignored.
    • return value The function returns the return value of a specific system call, after which you can convert the return value to a specific type, or 1 if the system call fails, and the error code is stored in errno.

Also, take the properties of the/etc/passwd file modified above as an example, this time using the Syscall direct call:

#include <stdio.h>
#include <unistd.h>
#include <sys/syscall.h>
#include <errno.h >

int Main ()
{
  int rc;
  rc = Syscall (Sys_chmod, "/etc/passwd", 0444);

  if (rc = = 1)
    fprintf (stderr, "chmod failed, errno =%d\n", errno);
  else
    printf ("chmod succeess!\n");
  return 0;
}

Compiled and executed under normal user, the output is the same as the previous example.

Three, through the int instruction into
If we know the whole process of the system call, we should know that the user state program gets into the kernel state through the soft interrupt instruction int 0x80 (the sysenter directive is introduced in Intel Pentium II), and the parameter passes through the register, EAX Pass the system call number, EBX, ecx, edx, ESI, and EDI to pass up to five parameters in turn, and the return value is stored in the EAX when the system call returns.

Still, as an example of the modified file attribute above, the call to the system call is written in inline assembly code:

#include <stdio.h>
#include <sys/types.h>
#include <sys/syscall.h>
#include < errno.h>

int main ()
{
  long RC;
  Char *file_name = "/etc/passwd";
  unsigned short mode = 0444;

  ASM (
    "int $0x80"
    : "=a" (RC)
    : "0" (Sys_chmod), "B" ((long) file_name), "C" ((long) mode)
  ;

  if (unsigned long) RC >= (unsigned long) -132 {
    errno =-RC;
    rc =-1;
  }

  if (rc = = 1)
    fprintf (stderr, "Chmode failed, errno =%d\n", errno);
  else
    printf ("success!\n");

  return 0;
}

If the return value stored by the EAX register (stored in the variable RC) is between -1~-132, it must be interpreted as an error code (the maximum error code defined in the/usr/include/asm-generic/errno.h file is 132), and the error code is written to the errno , the system call return value is-1, otherwise the value in EAX is returned.

The above program in 32-bit Linux under the normal user rights to compile and run the results of the same as the previous two!

The above is the detailed content of this article, I hope you like.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.