Question

An embedded Linux system that continuously writes files (~1/s) to a FAT32 partition occasionally corrupts the partition when interrupted. The process can be interrupted 2 ways: a power cycle, or using musb_hdrc/gadget to copy files to and from the host via USB. The musb_hdrc/gadget is used to switch access to the partition from internal embedded control to USB for transferring files to and from an external host. The solution may be to do a killall process-name but will this safely kill the Linux process? IOW: will the process complete any file IO already started or will there still be corrupted partitions? According to the killall man page If no signal name is specified SIGTERM is sent. Does something need to be added to the code to make sure the kill is clean and/or is there a special killall signal name to do a clean kill?

Was it helpful?

Solution 2

Well, you can modify your application to catch any signal except SIGKILL.
Shutdown operation or cleanup can be performed in a signal handler
Here is a simple example of setting up a handler to delete temporary files when certain fatal signals happen:

#include <signal.h>

 void
 termination_handler (int signum)
 {
   printf ("Caught signal %d\n", sig);
   switch(signum)
   {
       case SIGINT:
       printf ("CTRL C not allowed\n");
       break;

       case SIGTERM:
       struct temp_file *p;
       for (p = temp_file_list; p; p = p->next)
           unlink (p->name);
       exit(1);
       break;

       default:
   ...
   }
 }

 int
 main (void)
 {
   ...
   if (signal (SIGINT, termination_handler) == SIG_IGN)
     signal (SIGINT, SIG_IGN);
   if (signal (SIGHUP, termination_handler) == SIG_IGN)
     signal (SIGHUP, SIG_IGN);
   if (signal (SIGTERM, termination_handler) == SIG_IGN)
     signal (SIGTERM, SIG_IGN);
   ...
 }

Code after signal handler will not execute in case of SIGTERM.

OTHER TIPS

If the code is well written, the process should catch the SIGTERM and cleanly stop what it is doing and cleanly close every open resource.

If you can't modify the code, and you already observed that a SIGTERM signal brutally stops the process (just like a SIGKILL), i guess there's nothing you can do to stop it nicely.

Modulo bugs, there is no partition (and filesystem) corruption possible as a consequence of killing a process that does a regular file I/O (i.e. does not work with partition as a device, manipulating raw blocks).

If the process doesn't care to handle or ignore signals, SIGTERM will kill it in just the same merciless manner as SIGKILL would. For processes that actually try to exit gracefully on signals, SIGTERM means would you please shut down, while SIGKILL just kills (it's impossible to catch it).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top