I know some user space resources (e.g. fd/socket) used by a process will be destroyed automatically by kernel when "kill -9". But I am still confused about how driver (in kernel space) resource destroyed in this situation? Let me show an example below:
- Driver code implemented like this:
static void *mm;
static long my_ioctl(unsigned int cmd, unsigned long arg, void *priv)
{
switch (cmd) {
case IOC_CREATE_RESOURCE:
mm = kmalloc();
break;
case IOC_DESTROY_RESOURCE:
kfree(mm);
break;
}
// ...
}
static struct file_operations myfops = {
.owner = THIS_MODULE,
.open = my_open,
.unlocked_ioctl = my_ioctl,
};
- A user process started like this:
fd = open();
ioctl(fd, IOC_CREATE_RESOURCE);
// do somethings with the resource
ioctl(fd, IOC_DESTROY_RESOURCE);
close(fd);
- However, just before the second
ioctl, the process is killed with "kill -9" command. That mean the secondioctlwill ever get chance to be executed as far as know.
Does it mean the recourse created by the first ioctl is leaked? And how to fix it? The driver doesn't know anything about kill -9 operation from my understanding.