How to reboot Linux using a C programming language code ?
Hello,
Do you have maybe a reboot.c to reboot linux, using C? gcc or clang. Kind regards |
https://github.com/torvalds/linux/bl...ernel/reboot.c <--- this is NOT LIGHT
|
Quote:
|
Code:
man execl Code:
#include <unistd.h> |
|
Moderator Response
Moved: This thread is more suitable in <Programming> and has been moved accordingly to help your thread/question get the exposure it deserves.
|
Quote:
|
|
Quote:
|
Quote:
cc -static reboot.c -o myreboot |
BTW, what is the actual problem to solve?
|
Did you try RB_AUTOBOOT ?
|
Hello this is Gulshan Negi
Well, the reboot function provided by the Linux system call interface can be used to reboot a Linux system using the C programming language. Example: #include <unistd.h> #include <sys/reboot.h> int main() { int result = reboot(RB_AUTOBOOT); // initiate a system reboot if (result == -1) { perror("Reboot failed"); return 1; } else { printf("Rebooting...\n"); return 0; } } I hope it will help you. Thanks |
As others have said, usually the best approach is to "exec" the reboot command. (Remember to handle the case where the user doesn't have the privilege to run it.)
And, "this being open-source Linux," you can examine the actual source code of this command to see precisely how it works. |
Quote:
Could it be done without reboot.h ? #include <sys/reboot.h> look shutdown.c does not have reboot.h #include <stdio.h> #include <fcntl.h> #include <unistd.h> #include <stdlib.h> #include <utmp.h> #include <time.h> #include <string.h> #include <ctype.h> #include <signal.h> #include <sys/param.h> #include <termios.h> #include <mntent.h> #include <sys/mount.h> #include <sys/wait.h> #include <syslog.h> #include <sys/resource.h> #include "pathnames.h" |
All times are GMT -5. The time now is 09:42 AM. |