How to implement SIGKILL and SIGTERM and print a message?


 
Thread Tools Search this Thread
Top Forums Programming How to implement SIGKILL and SIGTERM and print a message?
# 8  
Old 11-10-2007
Quote:
Originally Posted by jim mcnamara
Porter is right about keeping things simple in a signal handler. But there is more - do not call most routines in the standard C library - as Porter points out fflush() is one of them.

If you absolutely must do I/O use write(). The idea is that you call something that cannot be interrupted by another signal - this is called an atomic operation.

write() is "partially" guaranteed to be atomic, but is as close as you'll get. POSIX says write() must guarantee a write the size of PIPE_BUF bytes or smaller to be atomic. There are other I/O system calls that are like this.
Are there any list of function calls that should not be called in a signal handler ?
I have been thinking write () is uninterrupted until it is done - this is something new from the statement that it is ' partially ' atomic.

Is there any way I could test this behavior by some kind of simulation ?

Here is one more point / question ,
with newer signal semantics - using sigaction

kernel guarantees the binary that when executing the handler registered for a possible signal, within that block another signal of the same kind would not be delivered until it returns - in such case why shouldn't a fflush operation be used ?
# 9  
Old 11-10-2007
Quote:
Originally Posted by matrixmadhan
... list of function calls that should not be called in a signal handler ?
Different operating systems have different implementations of functions, so it is better to err on the side of safety. It would be simpler to have a list of functions that can be called.

Quote:
Originally Posted by matrixmadhan
I have been thinking write () is uninterrupted until it is done - this is something new from the statement that it is ' partially ' atomic.
Also depends if you are using non-blocking IO.

Is there any way I could test this behavior by some kind of simulation ?

Here is one more point / question ,
with newer signal semantics - using sigaction

Quote:
Originally Posted by matrixmadhan
kernel guarantees the binary that when executing the handler registered for a possible signal, within that block another signal of the same kind would not be delivered until it returns - in such case why shouldn't a fflush operation be used ?
Good point, except alot of memory management his handled inside libc, not the kernel, so a signal can occur in the middle of, say, malloc or free, while the routine is updating a list of free memory pointers, if you interrupt and call in the middle another memory allocation routine you could upset the apple cart. Basically, those routines not being reentrant.

If you move onto threaded code, you don't use signal handlers at all, you use sigwait() to return you a signal once it occurs.
# 10  
Old 11-11-2007
Thanks for all the help. I have everything working except KIll -9, But I am learning in stages thanks to you all Smilie.


questions:
1. I am still confused about using atexit(). Isn't wastefull to resources to call another function that just exits? All I need to do really is call exit(); I havent implemented logging or anything and all filehandles and kids are closed.

2. I still need to work on the printfs..I just got back from AC at 2am. Banged this out when I got back--haha.


3. "you have done sigfillset but done nothing with the sigset_t, you need to call sigprocmask to block or unblock those signals."

I am still working on this one!!

4. There has to be a way to at least know sigkill is coming so I can print before it happens. I say this because some linux distros show the sigkill or are they faking it with sigterm? I did notice sigterm and sigkill are always together.


Here is what I Have now: Feel free to critique(so I don't learn bad Habits) Smilie.

So, is flush in or out? I am thinking, I dont need it because when I exit, that should clean up right?


Code:
static struct sigaction act; 


sigemptyset (&act.sa_mask);
  act.sa_flags = 0;

  /* Register the handler for SIGINT To Ignore. */
  act.sa_handler = SIG_IGN;
  sigaction (SIGINT, &act, NULL);

  /* Register the handler for SIGHUP To Ignore */
  act.sa_handler = SIG_IGN;
  sigaction (SIGHUP, &act, NULL);


       //calls function catcher.

  /* Register the handler for SIGTERM. */
  act.sa_handler =  catcher;
  sigaction (SIGTERM, &act, 0);


  //SIGKILL CANNOT BE CAUGHT!! KILL -9
  /* Register the handler for SIGKILL. */
  act.sa_handler =  catcher;
  sigaction (SIGKILL, &act, 0);




=======================================================
//gracefully quit on sigterm and sigkill.
void catcher(int sig)
{

  printf ("Caught signal %d\n", sig);
  fflush(stdout);
  switch (sig)

   case SIGTERM:

          printf("exiting sigterm");
          exit(1);
          fflush(stdout);
          break;

        case SIGKILL:

          printf("sigkilla");
          exit(1);
          fflush(stdout);
          break;
  }
}   // end function catcher

# 11  
Old 11-11-2007
1. "atexit" does not call exit, it installs a callback function that will be called when the process does exit.

2. exit(int) is a function that does not return, there is no point in having code following it, eg fflush().

3.remember, the code in a signal handler:

(a) just be setting variables/flags
(b) using minimal IO, write for example may be used, but nothing that uses the heap
(c) siglongjmp may be used, give that a go.....
# 12  
Old 11-11-2007
thanks for all the help.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to check the string in a file and print an attribute in the message

Hi, I am new to shell scripting and got a task to complete. Task is : we have a log file where in i need to traverse through the whole file to check the string "Person Type missing in message" and after that i need to get EMPLID=xxxxxx from the file and print details in a different file. ... (1 Reply)
Discussion started by: suren424
1 Replies

2. UNIX for Advanced & Expert Users

Print message while using sqlplus

I want to connect to oracle database from solaris... After that i will drop and create a no.of tables.One of the table example is as below. sqlplus -s usrname/password@dbname << SQL >> $logfile 2>&1 echo " dropping the table1" | tee logfile DROP TABLE Table1 echo "creating the table1" |... (1 Reply)
Discussion started by: millan
1 Replies

3. Shell Programming and Scripting

SIGSTOP and SIGKILL

Which is sent to a terminal when it closes? SIGKILL? Reason I ask is I have a script I want to run in the background, but want it to run even if the terminal window is closed. Or, I'd like it to background itself if the terminal is closed but not if its running in an open window. I will learn how... (5 Replies)
Discussion started by: DC Slick
5 Replies

4. Shell Programming and Scripting

How to detect SIGTERM,SIGKILL signal in UNIX

Dear All We have JBOSS server running on Linux we need to track Graceful Shutdown(SIGTERM) and Forceful Shutdown(SIGKILL) timestamp and write it into one file, I am new to UNIX Signal processing if is it possible how to detect it? We generally do $kill PID For Graceful... (5 Replies)
Discussion started by: mnmonu
5 Replies

5. Solaris

SIGQUIT and SIGKILL message

Dear All, I have machine with SunOS 5.10 Generic_138888-01 sun4v sparc SUNW,SPARC-Enterprise-T5120. Yesterday there is something at dmesg command : May 25 18:09:02 cacao_launcher: Timeout occured on heartbeat channel, cleanup engaged May 25 18:09:07 cacao_launcher: watchdog : warning,... (0 Replies)
Discussion started by: mbah_jiman
0 Replies

6. Shell Programming and Scripting

Print a message at specific line on prompt

Hi Friends, I am using HP-UNIX(ksh). I want to print a message at specific line on the prompt screen. For Example: for num in 1 10 3 145 do echo $num // need to print this on the same line for each number sleep 2 done Actual Output: ========== 1 10 3 145 Expected Output:... (5 Replies)
Discussion started by: Niroj
5 Replies

7. Shell Programming and Scripting

Error while trying to print message

Hi all Geting this error while trying to print message as : ./logfunc: print: bad file unit number heres what i m trying to do : log_date="$(date '+%d/%m/%Y %H:%M:%S')" log_type="Message" print "${log_date}: ${log_type}" print -u3 "${log_date}: ${log_type}" this error is due to... (3 Replies)
Discussion started by: Navatha
3 Replies

8. Solaris

To print Coloured Prelogin Message on SOLARIS--9................

Hiiii..... Every one...... I am using /etc/issue file to display Pre-login Message on my system, installed with SOLARIS-9. I am getting this Message in White fonts having Black background ( Colour of the Screen)..... So, is there ... (2 Replies)
Discussion started by: prashantshukla
2 Replies

9. Shell Programming and Scripting

Why SIGKILL will occur?

Hi Gurus, I am executing my Datastage jobs on UNIX operating System. While running the jobs i am getting the following error: main_program: Unexpected termination by Unix signal 9(SIGKILL) Can any one please let me know what are the possible situations where this SIGKILL will arrise? ... (9 Replies)
Discussion started by: choppas
9 Replies

10. Programming

signals - SIGTERM

Hi all, I need some urgent help. we are using Dynix/ptx V4.5 on i386, have several processes and instances are running on the box round the clock.we increased the processes recently. We have coded to handle the signals in our programs. Recently, we noticed most of our processes are... (2 Replies)
Discussion started by: reddyb
2 Replies
Login or Register to Ask a Question