LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-18-2023, 08:22 AM   #1
Xeratul
Senior Member
 
Registered: Jun 2006
Location: UNIX
Distribution: FreeBSD
Posts: 2,657

Rep: Reputation: 255Reputation: 255Reputation: 255
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
 
Old 03-18-2023, 08:23 AM   #2
Xeratul
Senior Member
 
Registered: Jun 2006
Location: UNIX
Distribution: FreeBSD
Posts: 2,657

Original Poster
Rep: Reputation: 255Reputation: 255Reputation: 255
https://github.com/torvalds/linux/bl...ernel/reboot.c <--- this is NOT LIGHT
 
Old 03-18-2023, 08:24 AM   #3
Xeratul
Senior Member
 
Registered: Jun 2006
Location: UNIX
Distribution: FreeBSD
Posts: 2,657

Original Poster
Rep: Reputation: 255Reputation: 255Reputation: 255
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
 
1 members found this post helpful.
Old 03-18-2023, 08:50 AM   #4
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,137
Blog Entries: 6

Rep: Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826
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
 
Old 03-18-2023, 11:05 AM   #5
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,220

Rep: Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309
This is closer to what you're looking for:

https://kernel.googlesource.com/pub/...ils/shutdown.c
 
Old 03-18-2023, 12:32 PM   #6
onebuck
Moderator
 
Registered: Jan 2005
Location: Central Florida 20 minutes from Disney World
Distribution: SlackwareŽ
Posts: 13,923
Blog Entries: 44

Rep: Reputation: 3158Reputation: 3158Reputation: 3158Reputation: 3158Reputation: 3158Reputation: 3158Reputation: 3158Reputation: 3158Reputation: 3158Reputation: 3158Reputation: 3158
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.
 
Old 03-18-2023, 02:33 PM   #7
Xeratul
Senior Member
 
Registered: Jun 2006
Location: UNIX
Distribution: FreeBSD
Posts: 2,657

Original Poster
Rep: Reputation: 255Reputation: 255Reputation: 255
Quote:
Originally Posted by teckk View Post
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...
 
Old 03-20-2023, 01:56 AM   #8
iPad
Member
 
Registered: Oct 2016
Distribution: iPadLinux
Posts: 81
Blog Entries: 1

Rep: Reputation: 45
https://man7.org/linux/man-pages/man2/reboot.2.html
 
2 members found this post helpful.
Old 03-20-2023, 05:37 AM   #9
Xeratul
Senior Member
 
Registered: Jun 2006
Location: UNIX
Distribution: FreeBSD
Posts: 2,657

Original Poster
Rep: Reputation: 255Reputation: 255Reputation: 255
Quote:
Originally Posted by teckk View Post
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.
 
Old 03-20-2023, 05:39 AM   #10
Xeratul
Senior Member
 
Registered: Jun 2006
Location: UNIX
Distribution: FreeBSD
Posts: 2,657

Original Poster
Rep: Reputation: 255Reputation: 255Reputation: 255
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
 
Old 03-20-2023, 06:16 AM   #11
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
BTW, what is the actual problem to solve?
 
Old 03-20-2023, 11:42 PM   #12
iPad
Member
 
Registered: Oct 2016
Distribution: iPadLinux
Posts: 81
Blog Entries: 1

Rep: Reputation: 45
Did you try RB_AUTOBOOT ?
 
Old 04-26-2023, 03:13 AM   #13
gulshan212
LQ Newbie
 
Registered: Apr 2023
Location: Haldwani, Uttarakhand
Posts: 6

Rep: Reputation: 0
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
 
Old 04-27-2023, 08:54 AM   #14
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,649
Blog Entries: 4

Rep: Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934
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.
 
Old 04-30-2023, 07:10 PM   #15
Xeratul
Senior Member
 
Registered: Jun 2006
Location: UNIX
Distribution: FreeBSD
Posts: 2,657

Original Poster
Rep: Reputation: 255Reputation: 255Reputation: 255
Quote:
Originally Posted by gulshan212 View Post
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"

Last edited by Xeratul; 04-30-2023 at 07:11 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: C: Everyone's favourite programming language isn't a programming language LXer Syndicated Linux News 0 03-23-2022 08:16 PM
natural-language programming language joeching Linux - Software 21 05-06-2019 04:26 AM
programming a linux shell using the c programming language alexantosh Linux - Newbie 11 10-30-2012 10:25 AM
linux programming employable programming language emekadavid Linux - General 8 09-21-2012 03:19 PM
Can someone explain to me this code from K&R's 'The C Programming Language' book? frankie_DJ Programming 10 11-25-2006 01:35 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:12 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration