Execute user program "Go" in kernel state via Call_usermodehelper ()

Source: Internet
Author: User


Turn from: Http://edsionte.com/techblog/archives/category/linux%E5%86%85%E6%A0%B8%E7%BC%96%E7%A8%8B background


How do I perform certain user-state programs or system commands in the Linux kernel? In the user state, it can be implemented by EXECVE (), and in the kernel state, the function can be implemented by Call_usermodehelpere (). If you consult the source code implementation of the Call_usermodehelper () kernel function, you can see that the function will eventually execute DO_EXECVE (). The EXECVE system call will eventually call Do_execve () after it undergoes the system call flow of the kernel.


Examples of Use


1. No output executable test



The Load function demo looks like this:


1 staticint__init call_usermodehelper_init(void)
2 {
3     intret = -1;
4     charpath[] ="/bin/mkdir";
5     char*argv[] = {path,"-p","/home/tester/new/new_dir", NULL};
6
7     printk("call_usermodehelper module is starting..!\n");
8     ret = call_usermodehelper(path, argv, envp, UMH_WAIT_PROC);
9     printk("ret=%d\n", ret);
10     return0;
11 }


The Unload function demo looks like this:


1 staticvoid__exit call_usermodehelper_exit(void)
2 {
3     intret = -1;
4     charpath[] ="/bin/rm";
5     char*argv[] = {path,"-r","/home/tester/new", NULL};
6     char*envp[] = {NULL};
7
8     printk("call_usermodehelper module is starting..!\n");
9     ret = call_usermodehelper(path, argv, envp, UMH_WAIT_PROC);
10     printk("ret=%d\n", ret);
11 }


2. executable file test with output



If the executable has output, you can take advantage of the output redirection, but at this point the executable should be/bin/bash, and the actual executable file is called Bash's parameters. For example, if you want to execute the ls-la command in the kernel and redirect its output to Ls_output, then in the above argv[]={"/bin/bash", "-C", "LS", "-la", ">", "/home/tester/ls_output ", NULL};



Although this article explains how to invoke the user-state program in the kernel state, it is possible to abstract this approach as a way for the kernel to initiate communication to the user state proactively.



No Comments»



Published in Linux kernel programming



The Linux kernel obtains absolute paths through file descriptorsMarch 19, 2014Background


In the Linux kernel, how do I get the absolute path of a process that is known to have a PID and file descriptor for its open file, FD? The basic idea is to get the file structure in the kernel, and then get the absolute path to the whole file through D_path ().


Method One


This approach can be used if you understand the relationship between processes and file system data structures. The basic method is as follows:



1. Get process descriptor task_struct through process PID;



2. Get the process through task_struct to open the file structure files_struct, thus obtaining the file descriptor chart;



3. Take FD as index in the file descriptor table to obtain the corresponding file structure of the document;



4. Obtain the corresponding path structure through file, which encapsulates the corresponding dentry and mount points of the current file;



5. Obtain the absolute path of the file through the kernel function d_path ();



Get the process descriptor demo via the process PID:


1 structtask_struct *get_proc(pid_t pid)
2 {
3     structpid *pid_struct = NULL;
4     structtask_struct *mytask = NULL;
5
6     pid_struct = find_get_pid(pid);
7     if(!pid_struct)
8         returnNULL;
9     mytask = pid_task(pid_struct, PIDTYPE_PID);
10     returnmytask;
11 }


Get absolute path demo via FD and D_path ():


1 intget_path(structtask_struct *mytask,intfd)
2 {
3         structfile *myfile = NULL;
4         structfiles_struct *files = NULL;
5         charpath[100] = {‘\0‘};
6         char*ppath = path;
7
8         files = mytask->files;
9         if(!files) {
10                 printk("files is null..\n");
11                 return-1;
12         }
13         myfile = files->fdt->fd[fd];
14         if(!myfile) {
15                 printk("myfile is null..\n");
16                 return-1;
17         }
18         ppath = d_path(&(myfile->f_path), ppath, 100);
19
20         printk("path:%s\n", ppath);
21         return0;
22 }


As can be seen from the above code, the acquisition of the structure from FD to file is obtained through the pointing relationship between each data structure.


Method Two


This is the same idea as method one, but you can use the kernel-provided function fget () to obtain the FD-to-file directly. This method is relatively simple to use, the program is more secure, but is less on the structure of the relationship between the thinking process. In fact, the implementation process of the Fget () function can be used as a reference to appreciate the rigor of the code implementation in the kernel.






The Linux kernel gets the absolute path through the file descriptor closed Comments»



Published in Linux kernel programming



Tags: Kernel Programming File System file path


LIBC Libraries and system callsJune 2, 2012


Linux system calls this section often comes with two words: libc library and encapsulation function, do not know whether you know what they mean?


libc


LIBC is the abbreviation of standard C library, which conforms to the ANSI C standard. The LIBC library provides macros, type definitions, string manipulation functions, mathematical calculation functions, and input and output functions used in C. Just as ANSI C is the standard for C, libc is only a library standard, and each operating system will implement the standard library in accordance with that standard. Usually what we call libc is a standard library of an operating system, such as what we call libc under the Linux operating system, which is glibc. GLIBC is the most widely used libc library in Unix-like operating systems, and its full name is the GNU C Library.



UNIX-like operating systems typically use the LIBC library as part of the operating system, which is considered an interface between the operating system and the user program. The LIBC library not only implements the functions in the standard C language, but also contains the function interfaces to which it belongs. For example, in the GLIBC library, both fopen () in standard C and open () in Unix-like systems are included. In Unix-like operating systems, if a standard library is missing, the entire operating system will not function properly.



Unlike Unix-like operating systems, Windows systems do not use the LIBC library as part of the entire core operating system. Typically each compiler is attached to its own libc library, which can be statically compiled into a program or dynamically compiled into a program. This means that the application relies on the compiler instead of the operating system.


Encapsulation functions


In a Linux system, the GLIBC library contains many APIs, and most APIs correspond to one system call, such as the interface used in an application, open (), which calls open () on a system with the same name. The API and system calls are associated in the GLIBC library through the encapsulation routines (Wrapper Routine). The API is a function interface defined in the header file, and the encapsulation routines in GLIBC are specific implementations of the API's corresponding functionality. In fact, we know that the function to be done by the interface Open () is done through the system call to open (), so the wrapper routine does the job of copying the parameters in the interface open () to the appropriate registers, and then throws an exception so that the system enters the kernel to execute Sys_open (), Finally, when the system call finishes executing, the encapsulation routine also returns the error code to the application.



It is important to note that the API and system calls in the library do not have a one by one corresponding relationship. The application uses system calls to get the services provided by the kernel, and functions such as string manipulation do not need to be implemented by the kernel, so they do not have to be associated with a system call.



However, we do not have to pass the encapsulation routines to use system calls, Syscall () and _SYSCALLX () two functions can call system calls directly. How to use it is clear in the Man Handbook.



Reference:



1. http://en.wikipedia.org/wiki/Libc



2. Man Syscalls



Execute user Program "Go" in kernel state via Call_usermodehelper ()


Related Article

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.