Skip to contentSkip to conversation

sensord: Use sigaction() for signal

PATCH In review Andre Prendel 3 participants 4 messages Apr 6, 2009 – May 11, 2009

lore
Share & tools
lore
#0 lore raw

PATCH 7/8 sensord: Use sigaction() for signal

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(-)
prog/sensord/sensord.c +25 -7
@@ -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;
+	sigemptyset(&new.sa_mask);
+	new.sa_flags = SA_RESTART;
+
+	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()");

_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors

#1 lore raw

Hi Andre,

Quoted 11 lines · Andre Prendel
Quoted 4 lines · Andre Prendel

Replace the deprecated signal() function by sigaction().

The signal() function has some disadvantages. This patch replaces signal() by sigaction() to install the signal handlers.

Hi Jean,

there are two patches left for reviewing (7/8, 8/8).

Maybe you have forgotten about that. So here is the reminder ;)

Not forgotten, just too busy. I hope to have some time to review them second half of this week.

Signature · 6 lines
Jean Delvare

_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors
#2 lore raw
In reply to Andre Prendel

Hi Andre,

Sorry for the late review.

Quoted 20 lines · Andre Prendel

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(-)
prog/sensord/sensord.c +0 -1
@@ -65,7 +65,6 @@
 
 static void signalHandler(int sig)
 {
-	signal(sig, signalHandler);
 	switch (sig) {
 	case SIGTERM:
 		done = 1;

prog/sensord/sensord.c line 154

Quoted 9 lines
prog/sensord/sensord.c +6 -0
@@ -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.

prog/sensord/sensord.c line 156

Quoted 2 lines
prog/sensord/sensord.c +2 -0
+	sigemptyset(&new.sa_mask);
+	new.sa_flags = SA_RESTART;

Why do we need this?

Quoted 23 lines
prog/sensord/sensord.c +16 -0
+
+	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);
 	}
 

prog/sensord/sensord.c line 201

Quoted 10 lines
prog/sensord/sensord.c +1 -6
-	/* 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.

Signature · 6 lines
Jean Delvare

_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors
#3 lore raw
In reply to Andre Prendel
Quoted 1 line · Jean Delvare

Hi Andre,

Hello Jean,

Quoted 27 lines

Sorry for the late review.

Quoted 23 lines · Andre Prendel

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(-)
prog/sensord/sensord.c +0 -1
@@ -65,7 +65,6 @@
 
 static void signalHandler(int sig)
 {
-	signal(sig, signalHandler);
 	switch (sig) {
 	case SIGTERM:
 		done = 1;
@@ -147,6 +146,30 @@
 	logOpened = 1;
 }
 
Quoted 10 lines

prog/sensord/sensord.c line 154

Quoted 6 lines
prog/sensord/sensord.c +6 -0
+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.

Quoted 4 lines

prog/sensord/sensord.c line 156

Quoted 2 lines
prog/sensord/sensord.c +2 -0
+	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.

Quoted 32 lines
Quoted 32 lines
prog/sensord/sensord.c +17 -6
+
+	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();
 
Quoted 7 lines

prog/sensord/sensord.c line 201

Quoted 2 lines
prog/sensord/sensord.c +0 -0
 	if ((pid = fork()) = -1) {
 		perror("fork()");

Looks otherwise good.

Signature · 1 line
Jean Delvare

_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors