-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathreboot_notifier.c
More file actions
62 lines (53 loc) · 1.64 KB
/
reboot_notifier.c
File metadata and controls
62 lines (53 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// SPDX-License-Identifier: (GPL-2.0 OR MIT)
/**
* Test waiting for a critical job to finish before rebooting or powering down.
* Sparkled from: https://stackoverflow.com/questions/64670766
*/
#include <linux/init.h> // module_{init,exit}()
#include <linux/module.h> // THIS_MODULE, MODULE_VERSION, ...
#include <linux/kernel.h> // printk(), pr_*()
#include <linux/reboot.h> // register_reboot_notifier()
#include <linux/kthread.h> // kthread_{create,stop,...}()
#include <linux/delay.h> // msleep()
#include <linux/completion.h> // struct completion, complete(), ...
#ifdef pr_fmt
#undef pr_fmt
#endif
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
static DECLARE_COMPLETION(done_wasting_time);
int my_notifier(struct notifier_block *nb, unsigned long action, void *data) {
if (!completion_done(&done_wasting_time)) {
pr_info("Wait! I have some critical job to finish...\n");
wait_for_completion(&done_wasting_time);
pr_info("Done!\n");
}
return NOTIFY_OK;
}
static struct notifier_block notifier = {
.notifier_call = my_notifier,
.next = NULL,
.priority = 0
};
int waste_time(void *data) {
struct completion *cmp = data;
msleep(5000);
complete(cmp);
return 0;
}
static int __init modinit(void)
{
register_reboot_notifier(¬ifier);
kthread_run(waste_time, &done_wasting_time, "waste_time");
return 0;
}
static void __exit modexit(void)
{
unregister_reboot_notifier(¬ifier);
}
module_init(modinit);
module_exit(modexit);
MODULE_VERSION("0.1");
MODULE_DESCRIPTION("Test waiting for a critical job to finish before rebooting "
"or powering down.");
MODULE_AUTHOR("Marco Bonelli");
MODULE_LICENSE("Dual MIT/GPL");