Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ignore SIGKILL or force a process into 'D' sleep state?

Tags:

c++

python

linux

I am trying to figure out how to get a process to ignore SIGKILL. The way I understand it, this isn't normally possible. My idea is to get a process into the 'D' state permanently. I want to do this for testing purposes (the corner case isn't really reproducible). I'm not sure this is possible programatically (I don't want to go damage hardware). I'm working in C++ and Python, but any language should be fine. I have root access.

I don't have any code to show because I don't know how to get started with this, or if it's even possible. Could I possibly set up a bad NFS and try reading from it?

Apologies in advance if this is a duplicate question; I didn't find anyone else trying to induce the D state.

Many thanks.

like image 840
Erin Avatar asked Jul 23 '14 22:07

Erin


2 Answers

To get a process into the "D" state (uninterruptible sleep), you have to write kernel code which does that, and then call that code from user space via a system call.

In the Linux kernel, this is done by setting the current task state to uninterruptible, and invoking the scheduler:

set_current_state(TASK_UNINTERRUPTIBLE);
schedule();

Of course, these actions are normally wrapped with additional preparations so that the task has a way to wake up, such as registering on some wait queue or whatever.

Device drivers for low-latency devices such as mass storage use uninterruptible sleeps to simplify their logic. It should only be used when there is a sure-fire way that the process will wake up almost no matter what happens.

Kernel code to do little thing like performing an uninterruptible sleep can be put into a tiny module (start with a minimal driver skeleton) whose initialization function performs the code and then returns nonzero. You can then run the code using insmod, e.g.

 insmod my_uninterruptible_sleep_mod.ko

there is no need to rmmod because the function fails, and so the module is unloaded immediately.

like image 136
Kaz Avatar answered Nov 09 '22 22:11

Kaz


It is not possible to ignore SIGKILL or handle it in any way.

From man sigaction:

The sa_mask field specified in act is not allowed to block SIGKILL or SIGSTOP. Any attempt to do so will be silently ignored.

like image 34
Dietrich Epp Avatar answered Nov 09 '22 22:11

Dietrich Epp