Windows core programming 2-kernel object 2

Source: Internet
Author: User
Continue ......

How can kernel objects be shared at process boundaries?
1. Inheritance of object handles
When a process has a parent-child relationship, the inheritance of the object handle can be used. The parent process has one or more object handles, and the parent process can decide to generate a child process to grant the child process access to the kernel objects of the parent process.
Specific implementation:
Specify in the security descriptor, for example:

Security_attrigbutes SA
SA. nlength = Sizeof (SA );
SA. lw.cuntydescriptor = Null ;
SA. binherithandle = True; // Indicates the kernel object that the child process can inherit from the parent process.
Handle hmutex = Createmutex (@ SA, false, null );


Then, use the CreateProcess function in the process:Bool CreateProcess (
Pctstr pszapplicationname,
Ptstr pszcommandline,
Psecurity_attributes psaprocess,
Psecurity_attributes psathread,
Bool binherithandles,
DWORD fdwcreale,
Pvoio pvenvironment,
Pctstr pszcurdir,
Pstartupinfo psistartinfo,
Pprocess_information ppiprocinfo );

If true is specified for binherithandles, the system is notified that the child process inherits the inherited handle from the parent process's handle table.

2. Change the HANDLE flag:
Because each process has a kernel object handle table that specifies the handle information of the process. Then, we can directly set the process handle table to obtain access to a kernel object.
Handle table style:

index pointer to the memory block of the kernel object access blocking (d w o r d of the Flag) flag (d w o r d of the Flag)
1 0x0 0 0 0 0 0 0 (none) (none)
2 0x0 0 0 0 0 0 0 (none) (none)
3 0 x F 0 0 0 0 0 1 0 0 x???????? 0x0 0 0 0 0 0 1

Set the HANDLE flag function:Bool sethandleinformation (
Handle hobject,
DWORD dwmask,
DWORD dwflags );

The function has three parameters. The first parameter H o B j e c t is used to identify a valid handle. The second parameter d w m a s k tells the function which or which signs it wants to change. Currently, two labels are associated with each handle:# DefineHandle flag_inherit 0x00000001
# DefineHANDLE flag protect from close 0x00000002

For example, to enable the inheritance flag of a kernel object, you can:
Sethandleinformation (hobj, handle_flag_inherit, handle_flag_inherit );
To disable this flag, you can:
Sethandleinformation (hobj, handle_flag_inherit, 0 );

3. Name the Kernel Object
You can name many kernel objects, such:

Handle createmutex (
Pslcurity_attributes PSA,
Bool binitialowner,
Pctstr pszname );

Handle createevent (
Psecurity_attributes PSA,
Bool bmanualreset,
Bool binitialstate,
Pctstr pszname );

Handle createsemaphore (
Psecurity_attributes PSA,
Long linitialcount,
Long lmaximumcount,
Pctstr psznarne );

Handle createwaitabletimer (
Pslcurity_attributes PSA,
Bool bmanualreset,
Pctstr pszname );

Handle createfilemapping (
Handle hfile,
Psecurity_attributes PSA,
DWORD flprotect,
DWORD dwmaximumsizehigh,
DWORD dwmaximumsizelow,
Pctstr pszname );

If the last parameter is set to null during creation, create a last-name kernel object. When creating an unnamed object, you can use inheritance (as described in the previous section) or d u p l I C A T E h a n d l e (which will be introduced in the next section) share objects across processes. To share an object by name, you must assign a name to the object.
Implementation: we create a kernel object in processa:
Handle hmutexpronessa = createmutex (null, false, "jeffmutex ");
Then, create a kernel object with the same name as processa in processb:
Handle hmutexprocessb = createmutex (null, false, "jeffmutex ");
What will happen next? The following is what is revealed in the book:
When process B calls C r e a t e m u t e x, the system first needs to check whether a kernel object named "j e ff m u t e x" already exists. Because there is indeed an object with this name, the kernel needs to check the object type. The system tries to create a mutex object and the object named "j e ff m u t e x" is also a mutex object. Therefore, the system performs a security check, to determine whether the caller has full access to the object. If you have this access permission, the system finds an empty project in the process B handle table and initializes the project so that the project points to the existing kernel object. If the object type does not match, or the caller is denied access, C r e a t e m u t e x will fail to run (return n u L ). When process B successfully calls C r e a t e m u t e x, it does not actually create a mutex object. On the contrary, process B is only assigned a process-related handle value to identify existing mutex objects in the kernel. Of course, since a new project in the process B handle table needs to reference this object, the Count of mutex objects will increase progressively. Before both process a and process B close their object handles, this object will not be undone. Note that the handle values of these two processes may be different. This is acceptable. Process a uses its handle value, while process B uses its own handle value to operate a mutually exclusive kernel object.

another way to share objects by name is that a process does not call the C r e a t e * function, instead, call one of the o p e n * functions shown below, such as handle openmutex (
DWORD dwdesiredaccess,
bool binherithandle,
pctstr pszname);
handle openevent (
DWORD dwdesiredaccess,
bool binherithandle,
pctstr pszname);
handle opensemaphore (
DWORD dwdesiredaccess,
bool binherithandle,
pctstr pszname );
handle openwaitabletimer (
DWORD dwdesiredaccess,
bool binherithandle,
pctstr pszname );
handle openfilemapping (
DWORD dwdesiredaccess,
bool binherithandle,
pctstr pszname);

What is the difference between calling CREATE * and calling open * to create an open kernel object?
The main difference between calling the C r e a t e * function and calling the o p e n * function is that if the object does not exist, then the C r e a t e * function creates this object, while the o p e n * function fails to run.

4. Copy the kernel object handle:
The last way to share the kernel objects that span process boundaries is to use the d u p l I C A T E h a n d l e function:Bool duplicatehandle (
Handle hsourceprocesshandle,
Handle hsourcehandle,
Handle htargetprocesshandle,
Phandle phtargethandle,
DWORD dwdesiredaccess,
Bool binherithandle,
DWORD dwoptions );

To put it simply, this function extracts the project from a process's handle table and copies the project to another process's handle table.

2. Process

1. What are processes and what are the characteristics of processes?
A process is a running process.Program. It consists of two parts:
• One is the kernel object used by the operating system to manage processes.
• The other is the address space, which contains all executable modules or d l modulesCodeAnd data. It also contains the space allocated by the dynamic memory. Such as thread stack and heap allocation space.

Note that the process is not lively, that is, the Operations completed by the process must be completed by the threads in the process. Each process must have one or more threads. The thread is responsible for executing the Code contained in the process address space. Multithreading means that multiple threads simultaneously execute the Code contained in the address space.
To allow the thread to execute code in the process, the operating system must allocate CPU time slices to the thread. It provides time slices in a loop, that is, how long each thread has to spend. After the time is completed, the CPU is reclaimed and transferred to another thread.
When a process is created, the system automatically creates a main thread for the process. Then, you can use this thread to create other threads, which are exactly multithreading.

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.