Skip to content

Instantly share code, notes, and snippets.

@xypron
Last active July 25, 2026 07:06
Show Gist options
  • Select an option

  • Save xypron/7cb8d217cfcdba792545a1595ced34de to your computer and use it in GitHub Desktop.

Select an option

Save xypron/7cb8d217cfcdba792545a1595ced34de to your computer and use it in GitHub Desktop.
Avoid poweroff on SpacemiT Com260
// SPDX-License-Identifier: MIT
// gcc -O2 -static -s -o pow.shutdown pow.c
// sudo cp pow /usr/lib/systemd/system-shutdown/pow.shutdown
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <linux/reboot.h>
#include <sys/syscall.h>
static int write_all(int fd, const char *buf, size_t len) {
size_t off = 0;
while (off < len) {
ssize_t n = write(fd, buf + off, len - off);
if (n < 0) {
if (errno == EINTR) continue;
return -1;
}
off += (size_t)n;
}
return 0;
}
int main(int argc, char **argv) {
const char *msg = "Power off now, or the system will reboot.\n";
int fd;
if (argc < 2 || strcmp(argv[1], "poweroff") != 0)
return 0;
fd = open("/dev/console", O_WRONLY | O_CLOEXEC);
if (fd < 0)
return 1;
for (int i = 0; i < 20; i++) {
if (write_all(fd, msg, strlen(msg)) != 0) break;
fsync(fd);
usleep(500000); // 500 ms
}
close(fd);
sync();
syscall(SYS_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2,
LINUX_REBOOT_CMD_RESTART, NULL);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment