The #include//Dispatcher header file defines the task structure task_struct, the initial task 0 data, and/or some embedded Assembler function macro statements about descriptor parameter setting and getting. 8 #include//kernel header file. Contains a prototype definition of some common kernel functions. 9 #include//segment operation header file. An embedded assembler function is defined for the segment register operation. 1011 #include//signal header file. Defines signal symbol constants, signal structures, and signal manipulation function prototypes. 1213 volatile void do_exit (int error_code); The preceding qualifier volatile requires the compiler not to optimize it. 14//gets the current task signal masking bitmap (shield code). The new signal masking bitmap is set in the Sys_sgetmask int () () (), current->blocked;18}19//. SIGKILL cannot be blocked. The return value is the original signal mask bitmap. int sys_ssetmask (int newmask) {$ int old=current->blocked;2324 current->blocked = newmask & ~ (1<< (S IGKILL-1)); old;26}27//Copy the sigaction data to the FS data segment to Where: The static inline void Save_old (char * from,char * to) invokes the corresponding function of the C function handler. The signal is pre-processed for identification. Call the Do_signal () function. Inserts a signal handler handle into the user stack. Iret Kernel system call Interrupt handler system call int 0x80 Next Statement signal handler user program more e-book tutorials download Please login Http://down.zzbaike.com/ebook This website offers e-book tutorials are online collection, If the tutorial involves or infringes your copyright, please contact us. The 5th Chapter kernel code linux/kernel/11329 {int i;3132 verify_area (to, sizeof (struct sigaction));//Verify that the memory to is sufficient. I=0, i< sizeof (struct sigaction), i++) {Put_fs_byte); Copy to FS segment. is typically a user data segment. from++; Put_fs_byte () in Include/asm/segment.h. The to++;37}38}39//copies the sigaction data from the FS data segment from location to where. The static inline void get_new (char * from,char * to) of $ {$ int i;4344 for (i=0; i< sizeof (struct sigaction); i++) 45 * (to++) = Get_fs_byte (from++);}47//signal () system call. Similar to Sigaction (). Installs a new signal handle (signal handler) for the specified signal. The signal handle can be either a user-specified function or a SIG_DFL (the default handle) or Sig_ign (ignored). Parameter Signum--the specified signal; handler--the specified handle; restorer– the address location where the original program is currently executing. The function returns the original signal handle. sys_signal Int (int signum, long handler, long restorer), {a struct sigaction tmp;5152 if (signum<1 signum= "" >32 || Signum==sigkill)//signal value to be within the range of (1-32), return-1; and must not be sigkill. Tmp.sa_handler = (void (*) (int)) handler; The specified signal processing handle. Tmp.sa_mask = 0; The signal Shield code at the time of execution. Tmp.sa_flags = Sa_oneshot | Sa_nomask; The handle is restored to the default value after only 1 times,//and allows the signal to be received in its own processing handle. Tmp.sa_restorer = (void (*) (void)) restorer; Save the return address. Handler = (long) current->sigaction[signum-1].sa_handler;59 current->sigaction[signum-1] = tmp;60 returnhandler;61}62//sigaction () system call. Changes the operation of a process when it receives a signal. Signum is any//signal other than Sigkill. If the new Action (action) is not empty, the new operation is installed. If the oldaction pointer is not empty, the original operation//is persisted to oldaction. Success returns 0, otherwise-1. sys_sigaction Int (int signum, const struct sigaction * action,64 struct sigaction * oldaction The mp;67//signal value is within the range of (1-32) and the processing handle of the signal Sigkill cannot be changed. if (signum<1 signum= "" >32 | | signum==sigkill) return-1;//set a new operation (action) in the sigaction structure of the signal. * tmp = current->sigaction[signum-1]; more e-book tutorials download please visit the Http://down.zzbaike.com/ebook website to provide e-book tutorials are online collection, If the tutorial involves or infringes your copyright, please contact us. The 5th Chapter Kernel code linux/kernel/11471 get_new ((char *) action,72 (char *) (signum-1+current->sigaction));//If the oldaction pointer is not empty , the original action pointer is saved to the location indicated by Oldaction. if (oldaction) save_old ((char *) &tmp, (char *) oldaction),//If the signal is allowed to be received in its own signal handle, the shield code is 0, otherwise the mask signal is set. if (Current->sigaction[signum-1].sa_flags & sa_nomask) Current->sigaction[signum-1].sa_mask = 0;77 else78 current->sigaction[signum-1].sa_mask |= (1<< (signum-1)); return 0;80}81The system calls the real signal handler in the interrupt handler (on the kernel/system_call.s,119 line). The main function of this code is to insert the processing handle of the signal into the user program stack and execute the signal handler immediately after the end//return of the system call, and then proceed to execute the user's program. do_signal void (Long signr,long eax, long ebx, long ecx, long edx,83 long FS, long es, long ds,84 long EIP, long cs, Lon G eflags,85 unsigned long * ESP, long ss) {unsigned long sa_handler;88 long old_eip=eip;89 struct sigaction * sa = CU Rrent->sigaction + signr-1; CURRENT->SIGACTION[SIGNU-1]. longs;91 int unsigned long * tmp_esp;9293 Sa_handler = (unsigned long) sa->sa_handler;//if the signal handle is sig_ign (ignored), returns if the handle is SIG_DFL (default processing), if the signal is//SIGCHLD is returned, otherwise the execution of the terminating process 94 if (sa_handler==1) return;96 if (!sa_handler) {if (SIGNR==SIGCHLD) 98 return;99 else100 do_exit (1<< (signr-1)); Why is the signal bitmap a parameter? No why!??] This should be Do_exit (1<sa_flags & sa_oneshot) 103 Sa->sa_handler = null;//The following code inserts a signal processing handle into the user stack, and also Sa_restorer, SIGNR, the Process Shield code (if//Sa_nomask not set), Eax,ecx,edx as a parameter and the original call system calls the program returns the pointer and the flag register value//pressed into the stack. Therefore, when the system call Interrupt (0x80) is returned to the user program, the user's signal handler is executed first, then//the user program is executed.。 The code pointer that the user calls the system call EIP points to the signal processing handle. 104 * (&EIP) = sa_handler;//If the signal itself is allowed to be signaled by its own processing handle, then the process's blocking code needs to be pressed into the stack. Longs = (Sa->sa_flags & sa_nomask)? 7:8;//extend the stack pointer of the user of the original calling program down 7 (or 8) long words (parameters used to hold the call signal handle, etc.), more e-book tutorials download please login/http Down.zzbaike.com/ebook This site provides e-book tutorials are online collection, if the tutorial involved or infringe on your copyright please contact us. The 5th chapter linux/kernel/115//the kernel code and checks memory usage (for example, if memory is unbounded, new pages are allocated, etc.). 106 * (&ESP)-= longs;107 Verify_area (esp,longs*4),//In the user stack from bottom to top of the sa_restorer, signal Signr, shield code blocked (if Sa_nomask set), EAX, ECX, edx, EFlags, and user program original code pointers. 108 tmp_esp=esp;109 Put_fs_long ((long) sa->sa_restorer,tmp_esp++); Put_fs_long (signr,tmp_esp++); 111 if (! ( Sa->sa_flags & Sa_nomask) Put_fs_long (current->blocked,tmp_esp++); 113 Put_fs_long (eax,tmp_esp++); 114 Put_fs_long (ecx,tmp_esp++); Put_fs_long (edx,tmp_esp++); Put_fs_long (eflags,tmp_esp++) 117 Put_fs_long (old_ eip,tmp_esp++); 118 current->blocked |= sa->sa_mask; The process block Code (Shield code) is added to the code bit in the Sa_mask. 119}1205.8.3 Other information 5.8.3.1 process signaling is a simple message for communication between processes, usually a label value in the following table, and no other information is carried. For example, when a child process eventuallyStop or end, a sigchild signal with a label of 17 is sent to the parent process to inform the parent process about the current state of the child process. On how a process handles received signals, there are generally two approaches: one is that the process of the program is not processed, and the signal is processed by the corresponding default signal handler of the system, and the second approach is that the process uses its own signal handler to process the signal.
Linux Kernel Learning-6 semaphores (Follow Sina Weibo: Lonely Years of erosion (more than 4,000 technology sharing)