views:

518

answers:

12

I'd like to emulate violent system shutdown, i.e. to get as close as possible to power outage on an application level. We are talking about c/c++ application on Linux. I need the application to terminate itself.

Currently i see several options:

  1. call exit()
  2. call _exit()
  3. call abort()
  4. do division by zero or dereference NULL.
  5. other options?

What is the best choice?

Partly duplicate of this question

+7  A: 

Why not do a halt? Or call panic?

Charlie Martin
Please be more specific, what code do i need from inside the application.
Drakosha
system("/sbin/halt"); Call void panic(char*); http://lxr.linux.no/linux+v2.6.29/kernel/panic.c#L56
Charlie Martin
Should i call 'halt -fn'? Can i call 'panic' as regular application, i.e. not kernel module? What header is it in?
Drakosha
I dn't have the Linux man page handy -- -f is "force"? Anyway, yeah, -n would most closely simulate a real powerfail shutdown.
Charlie Martin
+11  A: 
kill -9

It kills a process and does not allow any signal handlers to run.

sharth
i updated the question a little, please take a look.
Drakosha
+1 as this is probably as close as you can get without trying mess with allocated memory inside the application
Wayne
To really simulate a power failure, you're going to need to do better than kill -9since that kills the application process dead dead dead, but won't prevent the operating system from flushing dirty write buffers to disk. In a real power outage, the application might have completed some writes that the operating system is still waiting to write to disk. Power goes out, and the pending writes vanish.
Dave W. Smith
+19  A: 

IMHO the closest to come to a power outrage is to run the application in a VM and to power of the VM without shutting down. In all other cases where the OS is still running when the application terminates the OS will do some cleanup that would not occur in a real power outage.

lothar
Pretty good idea, it basically resembles the one in the first comment, but without risking data loss on the actual main machine.
none
As long as the VM app doesn't try to save the state. I think VMWare does that.
ChristianLinnell
Not if you do kill -9 to the VM, the OS will kill the VM immediatelly with no questions
Miquel
+7  A: 

Try

raise(SIGKILL)

in the process, or from the command line:

kill -9 pid

where pid is the PID of your process (these two methods are equivalent and should not perform any cleanup)

immibis
Or: kill(getpid(), SIGKILL); Using 'raise()' was a C standards committee invention because the C standard does not recognize process IDs as a valid concept.
Jonathan Leffler
+5  A: 

At the application level, the most violent you can get is _exit(). Division by zero, segfaults, etc are all signals, which can be trapped - if untrapped, they're basically the same as _exit(), but may leave a coredump depending on the signal.

If you truly want a hard shutdown, the best bet is to cut power in the most violent way possible. Invoking /sbin/poweroff -fn is about as close as you can get, although it may do some cleanup at the hardware level on its way out.

If you really want to stress things, though, your best bet is to really, truly cut the power - install some sort of software controlled relay on the power cord, and have the software cut that. The uncontrolled loss of power will turn up all sorts of weird stuff. For example, data on disk can be corrupted due to RAM losing power before the DMA controller or hard disk. This is not something you can test by anything other than actually cutting power, in your production hardware configuration, over multiple trials.

bdonlan
+4  A: 

You're unclear as to what your requirements are. If you're doing tests of how you will recover from a power failure, you need to actually cause a power failure. Even doing things like a kernel panic will allow write buffers on hard disks to flush, since they are independent of the CPU.

A remote power strip might be a solution if you really need to test the complete failure case.

Don Neufeld
+1  A: 

I've had regression tests that we used to perform where we flicked the power switch to OFF. While doing disk IO.

Failure to recover later was, well: a failure.

You can buy reliability like that: generally you'll need an "end user certificate".

You can get there in software by talking (dirty) to your UPS. APC UPSes will definitely do power off under software control!

Who says systems can't power cycle themselves ?

Tim Williscroft
+1  A: 

You could try using a virtual machine. Freeze it, screw it hard, and see what happens.

Otherwise kill -9 would be the best solution.

AndreasT
+1  A: 

If you need the application to terminate itself, the following seems appropriate:

kill(getpid(), SIGKILL); // same as kill -9

If that's not violent enough (and it may not be), then I like the idea of terminating a VM inside which your application is running. You should be able to rig up something where the application can send a command to the host machine (via ssh or something) to terminate its own VM.

Greg Hewgill
A: 

Infinite recursion, should run out of stack space (if not, the OOM killer will finish the job):

void a() { a(); }

Fork bomb (if the app doesn't have any fork limits then the OOM killer should kill the app at some point):

  while(1)
    fork();

Run out of memory:

  while(1)
    malloc(1);
A: 

Within a single running process kill(getpid(), SIGKILL) is the most extreme, as no cleanup is possible.

Otherwise, try a VM, or put a test machine on a power strip and turn the power off, if you are doing automated testing.

A: 

As pointed out try to consume as much resources as possible until the kernel kills you.

while(1) malloc(1); fork();

Another way is trying to write to a read only page, just keep writing memory until you get a bus error.

If you can get to the kernel, a great way to kill it is simply writing over a data structure the kernel uses, bonus points if you find a page as only readable and marked as writable and then overwrite it. BTW most linux kernels allow writing to the syscall_table or interrupt table, if you write there your system will crash for sure.

daniel