2022-2023-1 Student ID 20222820 "Linux Kernel Principles and Analysis" Fifth Week Assignment

Experimental content: Use the same system call in two ways: using library function API and embedding assembly code in C code

1. Use library function API to call getuid

1. Select a system call. For the list of system calls, see torvalds/linux.


Enter the /LinuxKernel/linux-3.18.6/arch/x86/syscalls directory and select the getuid function

 2. Create a new file 2820.c and use the library function API to call the function. The code is as follows

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>

int main(int argc, char const *agrv[])
{
    uid_t uid;
    uid=getuid();
    printf("The current user ID:%d\n",uid);

    return 0;
}

 3. Execute file 2820.c. System calls are a set of interfaces provided by the operating system for user-mode processes to interact with hardware devices. The system call sends a clear request to the kernel through a soft interrupt, uses a package routine to complete the corresponding function, and returns the result to the user process.

2. Implement system call getuid by embedding assembly code in C language

1. Modify the code in the 2820.c file into assembly code

code show as below:

#include <unistd.h>
#include <sys/types.h>
#include <stdio.h>
int main()
{
  uid_t uid;
  asm volatile(
          "mov $0,%%ebx\n\t"
          "mov $0x18,%%eax\n\t"
          "int $0x80\n\t"
          "mov %%eax,%0\n\t"
          : "=m" (uid)
          );

          printf("The current user ID:%d\n",uid);

          return 0;
}

2. The execution results are as follows

 

Guess you like

Origin blog.csdn.net/weixin_44226607/article/details/127350379