source: trunk/kernel/syscalls/sys_kill.c @ 436

Last change on this file since 436 was 436, checked in by alain, 4 years ago

1) improve the threads and process destruction mechanism.
2) introduce FIFOs in the soclib_tty driver.

File size: 6.1 KB
Line 
1/*
2 * sys_kill.c - Kernel function implementing the "kill" system call.
3 *
4 * Author    Alain Greiner (2016,2017)
5 *
6 * Copyright (c)  UPMC Sorbonne Universites
7 *
8 * This file is part of ALMOS-MKH.
9 *
10 * ALMOS-MKH is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; version 2.0 of the License.
13 *
14 * ALMOS-MKH is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with ALMOS-MKH; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24#include <kernel_config.h>
25#include <hal_types.h>
26#include <hal_irqmask.h>
27#include <errno.h>
28#include <thread.h>
29#include <printk.h>
30#include <process.h>
31#include <shared_syscalls.h>
32#include <cluster.h>
33#include <rpc.h>
34
35///////////////////////////
36int sys_kill( pid_t    pid,
37              uint32_t sig_id )
38{
39    xptr_t      owner_xp;      // extended pointer on target process in owner cluster
40    cxy_t       owner_cxy;     // target process owner cluster
41    process_t * owner_ptr;     // local pointer on target process in owner cluster
42    xptr_t      parent_xp;     // extended pointer on parent process
43    cxy_t       parent_cxy;    // parent process cluster
44    process_t * parent_ptr;    // local pointer on parent process
45    pid_t       ppid;          // parent process PID
46    uint32_t    retval;        // return value for the switch
47
48    thread_t  * this    = CURRENT_THREAD;
49    process_t * process = this->process;
50    trdid_t     trdid   = this->trdid;
51
52#if CONFIG_DEBUG_SYS_KILL
53uint64_t    tm_start;
54uint64_t    tm_end;
55tm_start = hal_get_cycles();
56if( CONFIG_DEBUG_SYS_KILL < tm_start )
57printk("\n[DBG] %s : thread %x enter / process %x / sig %d / cycle %d\n",
58__FUNCTION__ , this, pid, sig_id, (uint32_t)tm_start );
59#endif
60
61    // get pointers on process descriptor in owner cluster
62    owner_xp  = cluster_get_owner_process_from_pid( pid );
63    owner_cxy = GET_CXY( owner_xp );
64    owner_ptr = GET_PTR( owner_xp );
65   
66    // check process found
67    if( owner_xp == XPTR_NULL)
68    {
69
70#if CONFIG_DEBUG_SYSCALLS_ERROR
71printk("\n[ERROR] in %s : process %x not found\n", __FUNCTION__, pid );
72#endif
73        this->errno = EINVAL;
74        return -1;
75    }
76
77    // process can kill itself only when calling thread is the main thread
78    if( (pid == process->pid) && ((owner_cxy != local_cxy) || (LTID_FROM_TRDID( trdid ))) ) 
79    {
80
81#if CONFIG_DEBUG_SYSCALLS_ERROR
82printk("\n[ERROR] in %s : only main thread can kill itself\n", __FUNCTION__ );
83#endif
84        this->errno = EINVAL;
85        return -1;
86    }
87
88    // get parent process PID
89    parent_xp  = hal_remote_lwd( XPTR( owner_cxy , &owner_ptr->parent_xp ) );
90    parent_cxy = GET_CXY( parent_xp );
91    parent_ptr = GET_PTR( parent_xp );
92    ppid       = hal_remote_lw( XPTR( parent_cxy , &parent_ptr->pid ) );
93
94    // check processe INIT
95    if( pid == 1 )
96    {
97
98#if CONFIG_DEBUG_SYSCALLS_ERROR
99printk("\n[ERROR] in %s : process_init cannot be killed\n", __FUNCTION__ );
100#endif
101                this->errno = EINVAL;
102        return -1;
103    }
104
105    // analyse signal type / supported values are : 0, SIGSTOP, SIGCONT, SIGKILL
106    switch( sig_id )
107    {
108        case 0 :          // does nothing
109        {
110            retval = 0;
111            break;
112        }
113        case SIGSTOP:     // block all target process threads
114        {
115            // transfer TXT ownership
116            process_txt_transfer_ownership( owner_xp );
117
118            // block all threads in all clusters, but the main thread
119            process_sigaction( pid , BLOCK_ALL_THREADS );
120
121            // get pointer on target process main thread
122            xptr_t main_xp = XPTR( owner_cxy , &owner_ptr->th_tbl[0] );
123
124            // block main thread
125            thread_block( main_xp , THREAD_BLOCKED_GLOBAL );
126
127            // atomically update owner process termination state
128            hal_remote_atomic_or( XPTR( owner_cxy , &owner_ptr->term_state ) ,
129                                  PROCESS_TERM_STOP );
130            retval = 0;
131            break;
132        }
133        case SIGCONT:     // unblock all target process threads
134        {
135            // unblock all threads in all clusters
136            process_sigaction( pid , UNBLOCK_ALL_THREADS );
137
138            // atomically update owner process termination state
139            hal_remote_atomic_and( XPTR( owner_cxy , &owner_ptr->term_state ) ,
140                                   ~PROCESS_TERM_STOP );
141            retval = 0;
142            break;
143        }
144        break;
145        case SIGKILL:
146        {
147            // remove process from TXT list
148            process_txt_detach( owner_xp );
149
150            // mark for delete all process threads in all clusters, but the main
151            process_sigaction( pid , DELETE_ALL_THREADS );
152
153            // get pointer on target process main thread
154            xptr_t main_xp = XPTR( owner_cxy , &owner_ptr->th_tbl[0] );
155
156            // block main thread
157            thread_block( main_xp , THREAD_BLOCKED_GLOBAL );
158
159            // atomically update owner process descriptor term_state to ask
160            // the parent process sys_wait() function to delete this main thread
161            hal_remote_atomic_or( XPTR( owner_cxy , &owner_ptr->term_state ) ,
162                                  PROCESS_TERM_KILL );
163            retval = 0;
164            break;
165        }
166        default:
167        {
168
169#if CONFIG_DEBUG_SYSCALLS_ERROR
170printk("\n[ERROR] in %s : illegal signal %d / process %x\n", __FUNCTION__, sig_id, pid );
171#endif
172            this->errno = EINVAL;
173            retval = -1;
174            break;
175        }
176    }
177   
178    hal_fence();
179
180#if CONFIG_DEBUG_SYS_KILL
181tm_end = hal_get_cycles();
182if( CONFIG_DEBUG_SYS_KILL < tm_end )
183printk("\n[DBG] %s : thread %x exit / process %x / sig %d / cost = %d / cycle %d\n",
184__FUNCTION__ , this, pid, sig_id, (uint32_t)(tm_end - tm_start), (uint32_t)tm_end );
185#endif
186
187        return retval;
188
189}  // end sys_kill()
190
Note: See TracBrowser for help on using the repository browser.