-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshutdown.c
More file actions
22 lines (18 loc) · 731 Bytes
/
shutdown.c
File metadata and controls
22 lines (18 loc) · 731 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <stdio.h> // for printf()
#include <unistd.h> // for sync()
#include <sys/reboot.h> // for reboot() and constants
#define LINUX_REBOOT_CMD_POWER_OFF 0x4321fedc
int main(void) {
// Step 1: Tell the user what’s happening
printf("Flushing filesystem buffers...\n");
sync(); // Flushes filesystem buffers
// Step 2: Print next action
printf("Requesting ACPI shutdown from kernel...\n");
// Step 3: Ask the kernel to power off safely
if (reboot(LINUX_REBOOT_CMD_POWER_OFF) == -1) {
perror("Shutdown failed, please use the command \"flush\" and force long press the power button.");
return 1;
}
// If it works, this line shouldn’t even run
return 0;
}