Protection against SIGPIPE. Block it.
authorviric@llimona
Thu, 05 Apr 2007 23:55:45 +0200
changeset 95 d31aaee661d1
parent 94 aefdd4d9cbab
child 96 d1bb8bfdebc3
Protection against SIGPIPE. Block it.
Makefile
client_run.c
execute.c
mail.c
main.c
main.h
signals.c
--- a/Makefile	Thu Apr 05 22:20:38 2007 +0200
+++ b/Makefile	Thu Apr 05 23:55:45 2007 +0200
@@ -11,7 +11,8 @@
 	msg.o \
 	client_run.o \
 	mail.o \
-	error.o
+	error.o \
+	signals.o
 INSTALL=/usr/bin/install -c
 
 all: ts
@@ -31,6 +32,7 @@
 client_run.o: client_run.c main.h
 mail.o: mail.c main.h
 error.o: error.c main.h msg.h
+signals.o: signals.c main.h
 
 clean:
 	rm -f *.o ts
--- a/client_run.c	Thu Apr 05 22:20:38 2007 +0200
+++ b/client_run.c	Thu Apr 05 23:55:45 2007 +0200
@@ -13,10 +13,12 @@
 
 void c_run_tail(const char *filename)
 {
+    restore_sigmask();
     execlp("tail", "tail", "-f", filename, NULL);
 }
 
 void c_run_cat(const char *filename)
 {
+    restore_sigmask();
     execlp("cat", "cat", filename, NULL);
 }
--- a/execute.c	Thu Apr 05 22:20:38 2007 +0200
+++ b/execute.c	Thu Apr 05 23:55:45 2007 +0200
@@ -11,6 +11,7 @@
 #include <sys/types.h>
 #include <sys/wait.h>
 #include <stdlib.h>
+#include <signal.h>
 
 #include "msg.h"
 #include "main.h"
@@ -91,6 +92,7 @@
     switch(pid)
     {
         case 0: /* child */
+            restore_sigmask();
             dup2(fd_in,0); /* stdout */
             dup2(fd_out,1); /* stdout */
             close(fd_in);
@@ -180,6 +182,7 @@
     switch(pid)
     {
         case 0:
+            restore_sigmask();
             close(server_socket);
             close(p[0]);
             run_child(p[1]);
@@ -199,6 +202,8 @@
     return errorlevel;
 }
 
+#if 0
+Not needed
 static void sigchld_handler(int val)
 {
 }
@@ -214,3 +219,4 @@
 
   sigaction(SIGCHLD, &act, NULL);
 }
+#endif
--- a/mail.c	Thu Apr 05 22:20:38 2007 +0200
+++ b/mail.c	Thu Apr 05 23:55:45 2007 +0200
@@ -29,6 +29,7 @@
     switch(pid)
     {
         case 0: /* Child */
+            restore_sigmask();
             close(0);
             close(1);
             close(2);
@@ -104,6 +105,7 @@
     switch(pid)
     {
         case 0: /* Child */
+            restore_sigmask();
             sprintf(sjobid, "%i", jobid);
             sprintf(serrorlevel, "%i", errorlevel);
             execlp(onfinish, onfinish, sjobid, serrorlevel, ofname, command,
--- a/main.c	Thu Apr 05 22:20:38 2007 +0200
+++ b/main.c	Thu Apr 05 23:55:45 2007 +0200
@@ -7,6 +7,7 @@
 #include <unistd.h>
 #include <stdlib.h>
 #include <string.h>
+#include <signal.h>
 
 #include <stdio.h>
 
@@ -26,6 +27,7 @@
 static char version[] = "Task Spooler v0.3.1 - a task queue system for the unix user.\n"
 "Copyright (C) 2007  Lluis Batlle i Rossell";
 
+
 static void default_command_line()
 {
     command_line.request = c_LIST;
@@ -83,6 +85,7 @@
         {
             case 'K':
                 command_line.request = c_KILL_SERVER;
+                command_line.should_go_background = 0;
                 break;
             case 'l':
                 command_line.request = c_LIST;
@@ -332,6 +335,9 @@
     parse_opts(argc, argv);
     unset_getopt_env();
 
+    /* This will be inherited by the server, if it's run */
+    ignore_sigpipe();
+
     if (command_line.need_server)
         ensure_server_up();
 
--- a/main.h	Thu Apr 05 22:20:38 2007 +0200
+++ b/main.h	Thu Apr 05 23:55:45 2007 +0200
@@ -115,3 +115,7 @@
 };
 void error(const char *str, ...);
 void warning(const char *str, ...);
+
+/* signals.c */
+void ignore_sigpipe();
+void restore_sigmask();
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/signals.c	Thu Apr 05 23:55:45 2007 +0200
@@ -0,0 +1,20 @@
+#include <signal.h>
+#include <stdlib.h> /* for NULL */
+
+/* Some externs refer to this variable */
+static sigset_t normal_sigmask;
+
+void ignore_sigpipe()
+{
+    sigset_t set;
+
+    sigemptyset(&set);
+    sigaddset(&set, SIGPIPE);
+    sigprocmask(SIG_BLOCK, &set, &normal_sigmask);
+}
+
+void restore_sigmask()
+{
+    sigprocmask(SIG_SETMASK, &normal_sigmask, NULL);
+}
+