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

Last change on this file since 433 was 433, checked in by alain, 6 years ago

blip

File size: 5.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    uint32_t    save_sr;       // required to enable IRQs
40    xptr_t      owner_xp;      // extended pointer on target reference process
41    cxy_t       owner_cxy;     // target process cluster
42    process_t * owner_ptr;     // local pointer on target process
43    xptr_t      parent_xp;     // extended pointer on parent process
44    cxy_t       parent_cxy;    // parent process cluster
45    process_t * parent_ptr;    // local pointer on parent process
46    pid_t       ppid;          // parent process PID
47    uint32_t    retval;        // return value for the switch
48
49    thread_t  * this    = CURRENT_THREAD;
50
51#if CONFIG_DEBUG_SYS_KILL
52uint64_t    tm_start;
53uint64_t    tm_end;
54tm_start = hal_get_cycles();
55if( CONFIG_DEBUG_SYS_KILL < tm_start )
56printk("\n[DBG] %s : thread %x enter / process %x / sig %d / cycle %d\n",
57__FUNCTION__ , this, pid, sig_id, (uint32_t)tm_start );
58#endif
59
60    // get cluster and pointers on owner process
61    owner_xp  = cluster_get_owner_process_from_pid( pid );
62    owner_cxy = GET_CXY( owner_xp );
63    owner_ptr = GET_PTR( owner_xp );
64
65    // check process existence
66    if( owner_xp == XPTR_NULL )
67    {
68
69syscall_dmsg("\n[ERROR] in %s : process %x not found\n", __FUNCTION__ , pid );
70
71        this->errno = EINVAL;
72        return -1;
73    }
74   
75    // get parent process PID
76    parent_xp  = hal_remote_lwd( XPTR( owner_cxy , &owner_ptr->parent_xp ) );
77    parent_cxy = GET_CXY( parent_xp );
78    parent_ptr = GET_PTR( parent_xp );
79    ppid       = hal_remote_lw( XPTR( parent_cxy , &parent_ptr->pid ) );
80
81    // processes INIT and processes KSH cannot be stoped or killed
82    if( ppid < 2 )
83    {
84
85syscall_dmsg("\n[ERROR] in %s : process %x cannot be killed\n", __FUNCTION__ , pid );
86
87                this->errno = EINVAL;
88        return -1;
89    }
90
91    // enable IRQs
92    hal_enable_irq( &save_sr );
93
94    // analyse signal type
95    // supported values are : 0, SIGSTOP, SIGCONT, SIGKILL
96    switch( sig_id )
97    {
98        case 0 :
99        {
100            // does nothing
101            retval = 0;
102            break;
103        }
104        case SIGSTOP:     
105        {
106            // remove TXT ownership from target process
107            process_txt_reset_ownership( owner_xp );
108
109            // block all threads in all clusters
110            process_sigaction( owner_ptr , BLOCK_ALL_THREADS );
111
112            // atomically update reference process termination state
113            hal_remote_atomic_or( XPTR( owner_cxy , &owner_ptr->term_state ) ,
114                                  PROCESS_FLAG_BLOCK );
115 
116            retval = 0;
117            break;
118        }
119        case SIGCONT:
120        {
121            // unblock all threads in all clusters
122            process_sigaction( owner_ptr , UNBLOCK_ALL_THREADS );
123
124            // atomically update reference process termination state
125            hal_remote_atomic_and( XPTR( owner_cxy , &owner_ptr->term_state ) ,
126                                   ~PROCESS_FLAG_BLOCK );
127            retval = 0;
128            break;
129        }
130        break;
131        case SIGKILL:
132        {
133            // the process_make_kill() function must be executed
134            // by an RPC thread in process owner cluster
135            // It deletes all target process threads in all clusters,
136            // and updates the process termination state
137            rpc_process_make_kill_client( owner_cxy , owner_ptr , false , 0 );
138
139            retval = 0;
140            break;
141        }
142        default:
143        {
144
145syscall_dmsg("\n[ERROR] in %s : illegal signal type %d for process %x\n",
146__FUNCTION__ , sig_id , pid );
147
148            this->errno = EINVAL;
149            retval = -1;
150            break;
151        }
152    }
153   
154    // restore IRQs
155    hal_restore_irq( save_sr );
156
157    hal_fence();
158
159#if CONFIG_DEBUG_SYS_KILL
160tm_end = hal_get_cycles();
161if( CONFIG_DEBUG_SYS_KILL < tm_end )
162printk("\n[DBG] %s : thread %x enter / process %x / sig %d / cost = %d / cycle %d\n",
163__FUNCTION__ , this, pid, sig_id, (uint32_t)(tm_end - tm_start), (uint32_t)tm_end );
164#endif
165
166        return retval;
167
168}  // end sys_kill()
169
Note: See TracBrowser for help on using the repository browser.