separate-extcmd.diff
changeset 62 f5249c9544e5
parent 55 8f5cf5969e25
child 63 d268aa028975
--- a/separate-extcmd.diff	Sun Jan 13 06:42:35 2013 +0200
+++ b/separate-extcmd.diff	Fri Jan 18 11:28:35 2013 +0200
@@ -1,8 +1,8 @@
 Move extcmd code from hooks
 
-diff -r c6f9fa178a53 mcabber/mcabber/Makefile.am
---- a/mcabber/mcabber/Makefile.am	Thu Nov 22 00:52:37 2012 +0200
-+++ b/mcabber/mcabber/Makefile.am	Thu Nov 22 00:53:15 2012 +0200
+diff -r 33b2d85d27dd mcabber/mcabber/Makefile.am
+--- a/mcabber/mcabber/Makefile.am	Sun Jan 13 06:17:12 2013 +0200
++++ b/mcabber/mcabber/Makefile.am	Fri Jan 18 00:10:18 2013 +0200
 @@ -7,7 +7,7 @@
  		  xmpp.c xmpp.h xmpp_helper.c xmpp_helper.h xmpp_defines.h \
  		  xmpp_iq.c xmpp_iq.h xmpp_iqrequest.c xmpp_iqrequest.h \
@@ -20,10 +20,10 @@
  			 $(top_builddir)/include/config.h
  
  if OTR
-diff -r c6f9fa178a53 mcabber/mcabber/extcmd.c
+diff -r 33b2d85d27dd mcabber/mcabber/extcmd.c
 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
-+++ b/mcabber/mcabber/extcmd.c	Thu Nov 22 00:53:15 2012 +0200
-@@ -0,0 +1,121 @@
++++ b/mcabber/mcabber/extcmd.c	Fri Jan 18 00:10:18 2013 +0200
+@@ -0,0 +1,152 @@
 +/*
 + * extcmd.c      -- External event handler command
 + *
@@ -45,10 +45,12 @@
 + * USA
 + */
 +
-+#include <stdlib.h>
-+#include <string.h>
-+#include <sys/types.h>
-+#include <unistd.h>
++#include <stdlib.h>    // mkstemp
++#include <string.h>    // strlen
++#include <signal.h>    // signal
++#include <sys/types.h> // waitpid
++#include <sys/wait.h>  // waitpid
++#include <unistd.h>    // fork, close, write, execl
 +#include <glib.h>
 +
 +#include "screen.h"
@@ -57,19 +59,48 @@
 +#include "utils.h"
 +#include "utf8.h"
 +
++#ifndef WAIT_ANY
++# define WAIT_ANY -1
++#endif
++
 +static char *extcmd = NULL;
 +
++//  ext_sig_handler()
++// SIGCHLD handler.
++// wait()s for any terminated childs (to avoid hanging zombies)
++// and beeps, if child terminates with exit code 2.
++static void ext_sig_handler(int signum)
++{
++  int status;
++  pid_t pid;
++  do {
++    pid = waitpid (WAIT_ANY, &status, WNOHANG);
++    // Check the exit status value if 'eventcmd_checkstatus' is set
++    if (settings_opt_get_int("eventcmd_checkstatus")) {
++      if (pid > 0) {
++        // exit status 2 -> beep
++        if (WIFEXITED(status) && WEXITSTATUS(status) == 2) {
++          scr_beep();
++        }
++      }
++    }
++  } while (pid > 0);
++}
++
 +//  hk_ext_cmd_init()
 +// Initialize external command variable.
 +// Can be called with parameter NULL to reset and free memory
 +void hk_ext_cmd_init(const char *command)
 +{
 +  if (extcmd) {
++    signal(SIGCHLD, SIG_DFL);
 +    g_free(extcmd);
 +    extcmd = NULL;
 +  }
-+  if (command)
++  if (command) {
++    signal(SIGCHLD, sig_handler);
 +    extcmd = expand_filename(command);
++  }
 +}
 +
 +//  hk_ext_cmd()
@@ -138,16 +169,16 @@
 +    if (execl(extcmd, extcmd, arg_type, arg_info, name, arg_data,
 +              (char *)NULL) == -1) {
 +      // scr_LogPrint(LPRINT_LOGNORM, "Cannot execute external command.");
-+      exit(1);
++      exit(EXIT_FAILURE);
 +    }
 +  }
 +  g_free(datafname);
 +}
 +
-+/* vim: set expandtab cindent cinoptions=>2\:2(0:  For Vim users... */
-diff -r c6f9fa178a53 mcabber/mcabber/extcmd.h
++/* vim: set expandtab cindent cinoptions=>2\:2(0 ts=2 sw=2:  For Vim users... */
+diff -r 33b2d85d27dd mcabber/mcabber/extcmd.h
 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
-+++ b/mcabber/mcabber/extcmd.h	Thu Nov 22 00:53:15 2012 +0200
++++ b/mcabber/mcabber/extcmd.h	Fri Jan 18 00:10:18 2013 +0200
 @@ -0,0 +1,15 @@
 +#ifndef __MCABBER_EXTCMD_H__
 +#define __MCABBER_EXTCMD_H__ 1
@@ -164,9 +195,9 @@
 +void hk_ext_cmd(const char *bjid, const char *type, const char *info, const char *data);
 +
 +#endif
-diff -r c6f9fa178a53 mcabber/mcabber/hooks.c
---- a/mcabber/mcabber/hooks.c	Thu Nov 22 00:52:37 2012 +0200
-+++ b/mcabber/mcabber/hooks.c	Thu Nov 22 00:53:15 2012 +0200
+diff -r 33b2d85d27dd mcabber/mcabber/hooks.c
+--- a/mcabber/mcabber/hooks.c	Sun Jan 13 06:17:12 2013 +0200
++++ b/mcabber/mcabber/hooks.c	Fri Jan 18 00:10:18 2013 +0200
 @@ -24,6 +24,7 @@
  #include <string.h>
  #include <sys/types.h>
@@ -433,9 +464,9 @@
 -}
 -
  /* vim: set expandtab cindent cinoptions=>2\:2(0 sw=2 ts=2:  For Vim users... */
-diff -r c6f9fa178a53 mcabber/mcabber/hooks.h
---- a/mcabber/mcabber/hooks.h	Thu Nov 22 00:52:37 2012 +0200
-+++ b/mcabber/mcabber/hooks.h	Thu Nov 22 00:53:15 2012 +0200
+diff -r 33b2d85d27dd mcabber/mcabber/hooks.h
+--- a/mcabber/mcabber/hooks.h	Sun Jan 13 06:17:12 2013 +0200
++++ b/mcabber/mcabber/hooks.h	Fri Jan 18 00:10:18 2013 +0200
 @@ -66,9 +66,6 @@
  guint hk_subscription(LmMessageSubType mstype, const gchar *bjid,
                        const gchar *msg);
@@ -446,10 +477,35 @@
  #endif /* __MCABBER_HOOKS_H__ */
  
  /* vim: set expandtab cindent cinoptions=>2\:2(0 sw=2 ts=2:  For Vim users... */
-diff -r c6f9fa178a53 mcabber/mcabber/main.c
---- a/mcabber/mcabber/main.c	Thu Nov 22 00:52:37 2012 +0200
-+++ b/mcabber/mcabber/main.c	Thu Nov 22 00:53:15 2012 +0200
-@@ -44,6 +44,7 @@
+diff -r 33b2d85d27dd mcabber/mcabber/main.c
+--- a/mcabber/mcabber/main.c	Sun Jan 13 06:17:12 2013 +0200
++++ b/mcabber/mcabber/main.c	Fri Jan 18 00:10:18 2013 +0200
+@@ -19,17 +19,15 @@
+  * USA
+  */
+ 
+-#include <stdio.h>
+-#include <stdlib.h>
+-#include <unistd.h>
+-#include <string.h>
+-#include <signal.h>
+-#include <termios.h>
+-#include <sys/types.h>
+-#include <sys/wait.h>
++#include <stdio.h>   // fprintf, fileno, fgets, puts
++#include <stdlib.h>  // exit, getenv
++#include <unistd.h>  // tcsetattr, tcgetattr, getopt
++#include <string.h>  // strchr, strlen, memset
++#include <signal.h>  // signal
++#include <termios.h> // tcsetattr, tcgetattr
+ #include <glib.h>
+ #include <config.h>
+-#include <poll.h>
++#include <poll.h>    // POLLIN, POLLERR, POLLPRI
+ 
+ #include "caps.h"
+ #include "screen.h"
+@@ -44,6 +42,7 @@
  #include "xmpp.h"
  #include "help.h"
  #include "events.h"
@@ -457,3 +513,47 @@
  
  #ifndef MODULES_ENABLE
  # include "fifo.h"
+@@ -58,10 +57,6 @@
+ # include "hgcset.h"
+ #endif
+ 
+-#ifndef WAIT_ANY
+-# define WAIT_ANY -1
+-#endif
+-
+ static unsigned int terminate_ui;
+ GMainContext *main_context;
+ 
+@@ -98,23 +93,7 @@
+ 
+ void sig_handler(int signum)
+ {
+-  if (signum == SIGCHLD) {
+-    int status;
+-    pid_t pid;
+-    do {
+-      pid = waitpid (WAIT_ANY, &status, WNOHANG);
+-      // Check the exit status value if 'eventcmd_checkstatus' is set
+-      if (settings_opt_get_int("eventcmd_checkstatus")) {
+-        if (pid > 0) {
+-          // exit status 2 -> beep
+-          if (WIFEXITED(status) && WEXITSTATUS(status) == 2) {
+-            scr_beep();
+-          }
+-        }
+-      }
+-    } while (pid > 0);
+-    signal(SIGCHLD, sig_handler);
+-  } else if (signum == SIGTERM) {
++  if (signum == SIGTERM) {
+     mcabber_terminate("Killed by SIGTERM");
+   } else if (signum == SIGINT) {
+     mcabber_terminate("Killed by SIGINT");
+@@ -332,7 +311,6 @@
+ 
+   signal(SIGTERM, sig_handler);
+   signal(SIGINT,  sig_handler);
+-  signal(SIGCHLD, sig_handler);
+ #ifdef USE_SIGWINCH
+   signal(SIGWINCH, sig_handler);
+ #endif