All of lore.kernel.org
 help / color / mirror / Atom feed
From: Andre Prendel <andre.prendel@gmx.de>
To: lm-sensors@vger.kernel.org
Subject: Re: [lm-sensors] [PATCH 7/8] sensord: Use sigaction() for signal
Date: Mon, 11 May 2009 08:31:24 +0000	[thread overview]
Message-ID: <20090511083124.GA5197@ubuntu> (raw)
In-Reply-To: <20090406075758.GH4734@ubuntu>

On Fri, May 08, 2009 at 03:06:34PM +0200, Jean Delvare wrote:
> Hi Andre,

Hello Jean,

> 
> Sorry for the late review.
> 
> On Mon, 6 Apr 2009 09:57:58 +0200, Andre Prendel wrote:
> > Replace the deprecated signal() function by sigaction().
> > 
> > The signal() function has some disadvantages. This patch replaces
> > signal() by sigaction() to install the signal handlers.
> > ---
> > 
> >  sensord.c |   32 +++++++++++++++++++++++++-------
> >  1 file changed, 25 insertions(+), 7 deletions(-)
> > 
> > --- quilt-sensors.orig/prog/sensord/sensord.c	2009-04-04 18:25:22.000000000 +0200
> > +++ quilt-sensors/prog/sensord/sensord.c	2009-04-05 17:07:19.000000000 +0200
> > @@ -65,7 +65,6 @@
> >  
> >  static void signalHandler(int sig)
> >  {
> > -	signal(sig, signalHandler);
> >  	switch (sig) {
> >  	case SIGTERM:
> >  		done = 1;
> > @@ -147,6 +146,30 @@
> >  	logOpened = 1;
> >  }
> >  
> > +static void install_sighandler(void)
> > +{
> > +	struct sigaction new;
> > +	int ret;
> > +
> > +	new.sa_handler = signalHandler;
> 
> "new" is a structure on the stack, it is uninitialized, you must zero
> it using memset() before using it.
> 

never seen this in any example before (and never did it before), but
you're right.

Manpage says:

        The sigaction structure is defined as something like

           struct sigaction {
               void     (*sa_handler)(int);
               void     (*sa_sigaction)(int, siginfo_t *, void *);
               sigset_t   sa_mask;
               int        sa_flags;
               void     (*sa_restorer)(void);
           };

        On some architectures a union  is  involved:  do  not  assign  to  both
        sa_handler and sa_sigaction.

        The sa_restorer element is obsolete and should not be used.  POSIX does
        not specify a sa_restorer element.

So the first two elements needn't to be a union. If so, sa_action
should be initialized (to NULL). Sa_restorer is deprecated (not
specified in POSIX). sa_mask and sa_flags is already initialized.

> > +	sigemptyset(&new.sa_mask);
> > +	new.sa_flags = SA_RESTART;
> 
> Why do we need this?

System calls will be restarted automatically if they were interrupted
by a signal. That means, the syscall (if interrupted) doesn't return
EINTR. IMO useful feature.

> 
> > +
> > +	ret = sigaction(SIGTERM, &new, NULL);
> > +	if (ret = -1) {
> > +		fprintf(stderr, "Could not set sighandler for SIGTERM: %s\n",
> > +			strerror(errno));
> > +		exit(EXIT_FAILURE);
> > +	}
> > +
> > +	ret = sigaction(SIGHUP, &new, NULL);
> > +	if (ret = -1) {
> > +		fprintf(stderr, "Could not set sighandler for SIGHUP: %s\n",
> > +			strerror(errno));
> > +		exit(EXIT_FAILURE);
> > +	}
> > +}
> > +
> >  static void daemonize(void)
> >  {
> >  	int pid;
> > @@ -172,12 +195,7 @@
> >  		exit(EXIT_FAILURE);
> >  	}
> >  
> > -	/* I should use sigaction but... */
> > -	if (signal(SIGTERM, signalHandler) = SIG_ERR ||
> > -	    signal (SIGHUP, signalHandler) = SIG_ERR) {
> > -		perror("signal");
> > -		exit(EXIT_FAILURE);
> > -	}
> > +	install_sighandler();
> >  
> >  	if ((pid = fork()) = -1) {
> >  		perror("fork()");
> 
> Looks otherwise good.
> 
> -- 
> Jean Delvare

_______________________________________________
lm-sensors mailing list
lm-sensors@lm-sensors.org
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors

      parent reply	other threads:[~2009-05-11  8:31 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-04-06  7:57 [lm-sensors] [PATCH 7/8] sensord: Use sigaction() for signal Andre Prendel
2009-05-04  9:42 ` Jean Delvare
2009-05-08 13:06 ` Jean Delvare
2009-05-11  8:31 ` Andre Prendel [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20090511083124.GA5197@ubuntu \
    --to=andre.prendel@gmx.de \
    --cc=lm-sensors@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.