LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How to reboot Linux using a C programming language code ? (https://www.linuxquestions.org/questions/programming-9/how-to-reboot-linux-using-a-c-programming-language-code-4175723171/)

Xeratul 03-18-2023 08:22 AM

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

Xeratul 03-18-2023 08:23 AM

https://github.com/torvalds/linux/bl...ernel/reboot.c <--- this is NOT LIGHT

Xeratul 03-18-2023 08:24 AM

Quote:

For a large number of reasons, it is better to power off the machine using e.g.

execl("/bin/shutdown", "shutdown", "-P", "now", (char *)0);
or reboot using

execl("/bin/shutdown", "shutdown", "-r", "now", (char *)0);
is really a highly crap answer ;)

teckk 03-18-2023 08:50 AM

Code:

man execl
mytest.c
Code:

#include <unistd.h>

int main()
{
    execl("/bin/ls", "ls", "-a", (char*) NULL);
    return 0;
}

//gcc mytest.c -o mytest


dugan 03-18-2023 11:05 AM

This is closer to what you're looking for:

https://kernel.googlesource.com/pub/...ils/shutdown.c

onebuck 03-18-2023 12:32 PM

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.

Xeratul 03-18-2023 02:33 PM

Quote:

Originally Posted by teckk (Post 6418447)
Code:

man execl
mytest.c
Code:

#include <unistd.h>

int main()
{
    execl("/bin/ls", "ls", "-a", (char*) NULL);
    return 0;
}

//gcc mytest.c -o mytest


it sucks totally, i.e. system( "/sbin/reboot" ) or execl isnt C programming...

iPad 03-20-2023 01:56 AM

https://man7.org/linux/man-pages/man2/reboot.2.html

Xeratul 03-20-2023 05:37 AM

Quote:

Originally Posted by teckk (Post 6418447)
Code:

man execl
mytest.c
Code:

#include <unistd.h>

int main()
{
    execl("/bin/ls", "ls", "-a", (char*) NULL);
    return 0;
}

//gcc mytest.c -o mytest


this above code is so much ugly man.

Xeratul 03-20-2023 05:39 AM

Quote:

#include <unistd.h>
#include <sys/reboot.h>
int main() {
reboot(RB_POWER_OFF);
}



Some attempts... however it will power off

cc -static reboot.c -o myreboot

NevemTeve 03-20-2023 06:16 AM

BTW, what is the actual problem to solve?

iPad 03-20-2023 11:42 PM

Did you try RB_AUTOBOOT ?

gulshan212 04-26-2023 03:13 AM

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

sundialsvcs 04-27-2023 08:54 AM

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.

Xeratul 04-30-2023 07:10 PM

Quote:

Originally Posted by gulshan212 (Post 6427043)
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



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.