LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 01-18-2008, 02:54 AM   #1
bluehive
LQ Newbie
 
Registered: Jan 2008
Posts: 5

Rep: Reputation: 0
Exclamation How can a process detect the status of a signal


Hi all,
Is it possible for a process to know what signals it handle and the handler for each signal? I looked at signal API but couldn't find it helpful.

If I issue a command like this:
$ nohup ./my_exe&
- This process has a handler setup for SIGHUP.

But if it is invoked with 'nohup' like here, the process should _ignore_ its own handler and subsequently the signal. The reason is 'nohup' sets the handler for signal SIGHUP as 'SIG_IGN'.

And issuing '$kill -1 pid' should just be ignored!

So is it possible for a process to know the handler for a signal(like if its SIG_IGN or SIG_DFL). This way it can decide whether to catch it or not.

This program is in C.

Thanks,
Vishal
 
Old 01-18-2008, 04:42 AM   #2
ashok449
Member
 
Registered: Sep 2007
Location: noida
Distribution: suse
Posts: 63

Rep: Reputation: 16
Try this
Code:
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
void ouch_signal(int sig)
{
printf("OUCH! - I got signal %d\n", sig);
(void) signal(SIGHUP, SIG_IGN);
}
int main()
{
(void) signal(SIGHUP, ouch_signal);
while(1) {
printf("Hello World!\n");
sleep(1);
}
}
run the program and
#kill -HUP <pid>


Keep coding
-- Ashok Athukuri

Last edited by ashok449; 01-18-2008 at 05:11 AM. Reason: l'll try and let u know
 
Old 01-18-2008, 03:41 PM   #3
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 69
Quote:
Originally Posted by bluehive View Post
Hi all,
Is it possible for a process to know what signals it handle and the handler for each signal? I looked at signal API but couldn't find it helpful.
It is possible for a process to examine the signal handler or action associated with a specific signal. This is done with sigaction().
Quote:
Originally Posted by bluehive View Post
If I issue a command like this:
$ nohup ./my_exe&
- This process has a handler setup for SIGHUP.

But if it is invoked with 'nohup' like here, the process should _ignore_ its own handler and subsequently the signal.
This is wrong. I donít think you understand nohup. When a process is started normally, all signal actions are set to SIG_DFL except those which were set to SIG_IGN in the parent process. So nohup will explicitly set the signal action for SIGHUP to SIG_IGN prior to the exec. This doesnít in any way restrict your new process from overriding the signal action. In fact, if your program already changes the signal action, it should continue to do so (the only difference is that instead of changing it from SIG_DFL to a function address, it will change it from SIG_IGN to a function address).

Consider this demonstrative C code:
Code:
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>

int main()
{
	struct sigaction oldsa;
	if(sigaction(SIGHUP, NULL, &oldsa) == -1) { /*mark*/
		perror("Getting signal action");
		exit(EXIT_FAILURE);
	}

	printf("This program was %scalled with nohup.\n",
	       (oldsa.sa_handler == SIG_IGN ? "" : "not "));

	exit(EXIT_SUCCESS);
}
As it is, this function will currently figure out whether or not the program was invoked with nohup. If you wanted to, you could at the same time change the signal handler to your liking by changing the second argument of sigaction() in the line marked in the comment. You could also change the signal action to a custom signal handler anywhere subsequent the marked line (e.g., with signal()) if you wanted.
 
Old 01-18-2008, 10:50 PM   #4
ashok449
Member
 
Registered: Sep 2007
Location: noida
Distribution: suse
Posts: 63

Rep: Reputation: 16
Yeah, thats true if you wanna check the current status of the signal handler and then looking for action then do like

Code:
struct sigaction oldsa;
	if(sigaction(SIGHUP, NULL, &oldsa) == -1) { /*mark*/
		perror("Getting signal action");
		exit(EXIT_FAILURE);
	}
if (oldsa.sa_handler == SIG_DFL)    
   oldsa.sa_handler = SIG_IGN;   
      
   if (sigaction(SIGHUP, &oldsa, NULL) == -1)
      perror("Failed to ignore SIGHUP");

I guess that leaves you both chances to do that ...



-- Ashok Athukuri
 
Old 01-22-2008, 04:55 AM   #5
bluehive
LQ Newbie
 
Registered: Jan 2008
Posts: 5

Original Poster
Rep: Reputation: 0
Hi "osor" and Ashok,
Thanks so much for the help and sharing the more about signals. I mistakenly skipped sigaction()'s functionality to gather info of "previous" handler. Thanks!
I could solve the issue but still nohup leaves some doubts to me. How does nohup changes signal handler for other process? As nohup should be forking the application, and changing handler for its child(But how because as far as I know signal handler info is kept in uarea? And it should be accessible to kernel or to the invoked process only)!

Thanks in advance,
Vishal
 
Old 01-22-2008, 12:23 PM   #6
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 69
Quote:
Originally Posted by bluehive View Post
How does nohup changes signal handler for other process?
There are two different types of nohup one thatís builtin to some shells (which uses fork), and one thatís a stand-alone executable. Hereís how the standalone one works (I leave out any code relating to file descriptor juggling which is unrelated to your question):
Code:
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>

int main(int argc, char *argv[])
{
	signal(SIGHUP, SIG_IGN);
	execvp(argv[1], argv+1);
	
	exit(EXIT_FAILURE);
}
Hereís what happens when you run something like:
Code:
nohup somecommand its arguments
  1. The shell forks off a process, and a new process with its own PID is created in the kernelís process table.
  2. The image of the new process is replaced by executing nohup with the specific parameters.
  3. Now, in the new process image, the code tells the kernel that the process will ignore SIGHUPs.
  4. The process image is replaced by the file which was the first argument of the nohup command (ďsomecommandĒ) and the subsequent arguments are all the arguments after and including the first argument (ďsomecommand its argumentsĒ).
Each time the process image was replaced, a bunch of things happen (e.g., file descriptors open in the calling process image shall remain open in the new process image, except for those whose close-on- exec flag FD_CLOEXEC is set), and you can see what these all are by reading the standard. One of these things is the way in which signal handling is inherited:
Quote:
Originally Posted by exec manpage
Signals set to the default action (SIG_DFL) in the calling process image shall be set to the default action in the new process image. Except for SIGCHLD, signals set to be ignored (SIG_IGN) by the calling process image shall be set to be ignored by the new process image. Signals set to be caught by the calling process image shall be set to the default action in the new process image (see <signal.h>). If the SIGCHLD signal is set to be ignored by the calling process image, it is unspecified whether the SIGCHLD signal is set to be ignored or to the default action in the new process image. After a successful call to any of the exec functions, alternate signal stacks are not preserved and the SA_ONSTACK flag shall be cleared for all signals.
 
  


Reply


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
how a father process know which child process send the signal SIGCHLD icoming Programming 10 07-20-2010 07:26 AM
Problem w/ Cyrus Status=Bounced Signal 11 davidswanson1 Linux - Server 0 12-21-2006 06:24 PM
how to auto detect the status of sendmail!!!!!!!!!! dhanju Linux - Networking 5 10-22-2005 06:35 AM
how to detect that I'm handling signal igor.kopriva Programming 2 10-01-2004 05:46 AM
Sending a signal to a process (Help me) rajesh_b Programming 2 09-17-2004 01:30 AM


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

Main Menu
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