2006-10-17 [colin] 2.5.5cvs11
[claws.git] / src / plugins / pgpcore / sgpgme.c
index 40e2969a55e677f8d254ce2c85ea6ece89159ddf..a4c744475fd820277e8a643ee95005e59a1ee7d1 100644 (file)
@@ -30,6 +30,8 @@
 #include <glib/gi18n.h>
 #include <stdio.h>
 #include <errno.h>
+#include <sys/types.h>
+#include <sys/wait.h>
 #ifndef G_OS_WIN32
 #include <sys/mman.h>
 #endif
@@ -708,14 +710,43 @@ again:
                g_free(buf);
                if (val == G_ALERTALTERNATE) {
 #ifndef G_OS_WIN32
-                       gchar *cmd = g_strdup_printf("gpg --send-keys %s", key->fpr);
+                       gchar *cmd = g_strdup_printf("gpg --no-tty --send-keys %s", key->fpr);
                        int res = 0;
-                       GTK_EVENTS_FLUSH();
-                       res = system(cmd);
+                       pid_t pid = 0;
+                       pid = fork();
+                       if (pid == -1) {
+                               res = -1;
+                       } else if (pid == 0) {
+                               /* son */
+                               res = system(cmd);
+                               res = WEXITSTATUS(res);
+                               _exit(res);
+                       } else {
+                               int status = 0;
+                               time_t start_wait = time(NULL);
+                               res = -1;
+                               do {
+                                       if (waitpid(pid, &status, WNOHANG) == 0 || !WIFEXITED(status)) {
+                                               usleep(200000);
+                                       } else {
+                                               res = WEXITSTATUS(status);
+                                               break;
+                                       }
+                                       if (time(NULL) - start_wait > 5) {
+                                               debug_print("SIGTERM'ing gpg\n");
+                                               kill(pid, SIGTERM);
+                                       }
+                                       if (time(NULL) - start_wait > 6) {
+                                               debug_print("SIGKILL'ing gpg\n");
+                                               kill(pid, SIGKILL);
+                                               break;
+                                       }
+                               } while(1);
+                       }
                        if (res == 0) {
                                alertpanel_notice(_("Key exported."));
                        } else {
-                               alertpanel_notice(_("Couldn't export key."));
+                               alertpanel_error(_("Couldn't export key."));
                        }
                        g_free(cmd);
 #else