LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 10-17-2006, 05:07 AM   #1
khtw
LQ Newbie
 
Registered: Oct 2006
Distribution: NEBULA First 2.2 OS
Posts: 12

Rep: Reputation: 0
C Code to reboot LINUX in the linuxrc script


Hi,

I've been googling this quite a bit, and can't find anything that works. OK, first, what I want to do:

A. I want to write a little piece of code that will restart LINUX
B. This has to run in the linuxrc script (which is built into the boot.img part of the booting of LINUX).
C. If it is compiled, all the better. So, C would be nice.

What did not work:

1. Using the "reboot" command or source. Because init thread/app has not started yet, there is not rc to take to run level 6 to shut down!
2. Code that calls interupts, because I can't find a way to call interupts in g++ in LINUX as I did years and years ago in DOS with Borland C (with int(0x90,null,null)

If any-one has an idea, or even a direction I can look into, it would be appr.

Thanks

KHTW
 
Old 10-18-2006, 12:59 AM   #2
IBall
Senior Member
 
Registered: Nov 2003
Location: Perth, Western Australia
Distribution: Ubuntu, Debian, Various using VMWare
Posts: 2,088

Rep: Reputation: 61
Why on earth do you want to restart your computer before it has even started

I would say that this will cause major problems, since your computer will be restarted everytime you boot, and you will never be able to change that, because you will never be able to get into your system.

Unless I have mis-understood you...

--Ian
 
Old 10-19-2006, 02:24 AM   #3
khtw
LQ Newbie
 
Registered: Oct 2006
Distribution: NEBULA First 2.2 OS
Posts: 12

Original Poster
Rep: Reputation: 0
Reason for restart

Hi,

The reason I have for wanting to restart on linuxrc is that I created a very small boot.img along a very small kernel to do a few specific things. It is not needed to load a lot of things, so I insmod what I need, do what I want (some C code), and then would like to restart the PC when it is done. So, this is not on my running OS, but a very specific and applied tool I've created. At the moment I need the user to <CTRL><ALT><DEL>, but would love to have a non-user interaction system.

Thanks for you reply

khtw
 
Old 10-19-2006, 02:46 AM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 12,497

Rep: Reputation: 1077Reputation: 1077Reputation: 1077Reputation: 1077Reputation: 1077Reputation: 1077Reputation: 1077Reputation: 1077
Have a look at kexec.
 
Old 10-19-2006, 05:36 PM   #5
khtw
LQ Newbie
 
Registered: Oct 2006
Distribution: NEBULA First 2.2 OS
Posts: 12

Original Poster
Rep: Reputation: 0
Thanks, it gives me something to investigate more.

Although, on first glance, it does not seem to be able to do what I want it to do. My kernel boots, and there is no telling what the default OS on the HD is (LINUX, BSD, Windows, Solaris). In the LINUX setup, it works great. So that that is 1/4 of my problem solved!

Thanks for you help. Any more ideas will be appreciated.

khtw
 
Old 10-19-2006, 06:51 PM   #6
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: LFS-Version SVN-20091202, Arch 2009.08
Posts: 1,496

Rep: Reputation: 68
If the C program is running as root you could always call shutdown from a system call or mabye even init 0
 
Old 10-20-2006, 01:58 AM   #7
khtw
LQ Newbie
 
Registered: Oct 2006
Distribution: NEBULA First 2.2 OS
Posts: 12

Original Poster
Rep: Reputation: 0
Hi,

init is not loaded, and there is no run levels (yet).

Thanks for the interest.

khtw
 
Old 10-20-2006, 07:04 AM   #8
slantoflight
Member
 
Registered: Aug 2005
Distribution: Smoothwall
Posts: 283
Blog Entries: 3

Rep: Reputation: 35
just insert
shutdown -r -n -t : "now"
at the end of your script.

you have a shutdown binary hopefully. if not, you should definately get one.
 
Old 10-20-2006, 10:16 AM   #9
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: LFS-Version SVN-20091202, Arch 2009.08
Posts: 1,496

Rep: Reputation: 68
If he doesent have init going shutdown isent going to do him any good. All shutdown does is call init 0 with some time variables and a system message.
 
Old 10-20-2006, 10:40 AM   #10
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,014

Rep: Reputation: 115Reputation: 115
If he doesn't have init he doesn't have Linux or any *nix like OS running. What is the exact error message if you try to use reboot?
 
Old 10-21-2006, 02:43 AM   #11
khtw
LQ Newbie
 
Registered: Oct 2006
Distribution: NEBULA First 2.2 OS
Posts: 12

Original Poster
Rep: Reputation: 0
Hay,

OK, quick detail of what is there, and what is not:

isolinux/lilo/grub
this loads
kernel (2.6.16)
and
boot.img
and this runs
linuxrc (default start script contained in boot.img)
which
insmod <lots of stuff, like ext2.ko, etc.>
then it runs
C CODE (my stuff).
NEEDS
RESTART SYSTEM

So, the LINUX kernel set the environment for to load modules, and startes up linuxrc for me. linuxrc script loads the modules I need to make things accessable like drives that I need. When the logic in my C code are done, I would LOVE the whole bundle to restart itself. At the moment it just prompts the user to please <CTLR><ALT><DEL> to restart his/her machine. And this does do a restart, no errors, not problems.

So, you see there was no need for me to create a RAM disk and load a init and a root OS into just for a simple task.

Areas I've looked at to do the restart script/code:

1. the old DOS interupts. Calling a restart interupt on your PC BIOS will make the PC reboot. I have DOS code (Borland C) for this, but can't get an interupt call on LINUX.

2. Then I thought of maybe forcing a <CTRL><ALT><DEL> key to the system, but still needs interupts.

3. Had a look at the "shutdown" and "reboot" LINUX apps, but they just target init and run levels.

4. Had a look at kexec (as learned on this thread), but I can't 100% say that the OS on the PC is LINUX. It might be BSD, Netware or even Windows.

So I still sit with the question: how do you (with g++/gcc, or any other thing that will run in LINUX, not reliant on init or other threads that are assumed started) under a half started LINUX (thus just kernel) force your PC to just simulate a <CTLR><ALT><DEL> to restart.

Thanks

khtw
 
Old 10-21-2006, 05:33 AM   #12
slantoflight
Member
 
Registered: Aug 2005
Distribution: Smoothwall
Posts: 283
Blog Entries: 3

Rep: Reputation: 35
shutdown -r -n -t : "now" with -n actually doesn't call init. If the man page can be believed. It does restart the computer fairly quickly on my system. And it seems to work even with init completely gone. So i would hazard a guess its standalone.
 
Old 10-21-2006, 11:23 AM   #13
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,014

Rep: Reputation: 115Reputation: 115
I don't think that solution is needed, and at worst a bad idea. I think you have do have init, especially if the three-fingered-salute works. Have you actually checked whether init is running or not? The surest way is to check the cmdline in /proc/1
 
Old 10-24-2006, 06:19 AM   #14
khtw
LQ Newbie
 
Registered: Oct 2006
Distribution: NEBULA First 2.2 OS
Posts: 12

Original Poster
Rep: Reputation: 0
Hay,

shutdown does not work, regardless of parameters. It always interacts with init.

As far as usibility and need for this app goes: YES it works, YES I need it. People told Linux Torvolts not to start LINUX, because there was a MINIX or WINDOWS. So, we all have reasons for wanting to do something different.

So, still, the question is put forward: how do I restart a kernel without init running.

khtw
 
Old 10-24-2006, 10:02 AM   #15
orgcandman
Member
 
Registered: May 2002
Location: dracut MA
Distribution: Ubuntu; PNE-LE; LFS (no book)
Posts: 594

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by tuxdev
If he doesn't have init he doesn't have Linux or any *nix like OS running.
False. By default, linux kernel has an init parameter which is set to /bin/init, or /sbin/init, BUT if you put something like
"init=/bin/bash"
in your bootloader options to pass to the kernel, the system will boot without init. In fact, many boot floppies and embedded linux systems don't even use init, and opt for the linuxrc system or even just shell scripts or a custom boot program.

Quote:
Originally Posted by tuxdev
don't think that solution is needed, and at worst a bad idea. I think you have do have init, especially if the three-fingered-salute works.
Again, wrong here. Linux has an interrupt handler for the 3-finger salute. When that happens, linux kills the "init" program, since every other process is a child of that program. Init's sighand will call the proper shutdown routines. Again, if you do the init= line on boot, you'll see. It's much less graceful when the kernel handler doesn't have init to fall back on (nothing is unmounted, disks aren't synced, etc).

In fact, I think the first few linux systems I was running didn't have init. They just ran a custom program which called bash scripts.

As far as shutting down the system, as I said, if you can kill the root process (PID=1) then you shutdown the system. It's not terribly graceful, you should have synced disks, unmounted all filesystems, cleaned up any programs running, and THEN have pid=1 return 0. That will cause the kernel to reboot the system.
 
  


Reply

Tags
rebooting


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
linuxrc:please help in editing bongski55 Red Hat 3 06-11-2009 06:29 AM
What is the meaning of 'linuxrc'? leonzheng Linux - Newbie 4 11-13-2006 10:04 AM
linuxrc problem? cstovall Linux - Software 0 11-08-2005 08:22 PM
when linuxrc will get crash? alwar Linux - Software 0 01-20-2005 12:56 AM
GENTOO - "init=/linuxrc" (/linuxrc: not found | kernel panic!!) halo14 Linux - Distributions 6 01-14-2005 02:03 PM


All times are GMT -5. The time now is 01:38 AM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration