2006-10-07 [colin] 2.5.3cvs20
[claws.git] / src / plugins / pgpcore / pgp_viewer.c
index db308803eabf108638f2e38924af5f3a3ff4b241..eb5f1167aaf0c8cb9d29384fa109accabcb742f0 100644 (file)
@@ -1,6 +1,7 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2006 Hiroyuki Yamamoto
+ * Copyright (C) 1999-2006 Hiroyuki Yamamoto & the Sylpheed-Claws team
+ * This file Copyright (C) 2006 Colin Leroy <colin@colino.net>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -24,6 +25,8 @@
 #include <stddef.h>
 #include <glib.h>
 #include <glib/gi18n.h>
+#include <sys/types.h>
+#include <sys/wait.h>
 
 #include "version.h"
 #include "common/sylpheed.h"
@@ -118,8 +121,38 @@ static void pgpview_show_mime_part(TextView *textview, MimeInfo *partinfo)
 #ifndef G_OS_WIN32
                        gchar *cmd = g_strdup_printf("gpg --recv-keys %s", sig->fpr);
                        int res = 0;
+                       pid_t pid = 0;
                        GTK_EVENTS_FLUSH();
-                       res = system(cmd);
+                       
+                       pid = fork();
+                       if (pid == -1) {
+                               res = -1;
+                       } else if (pid == 0) {
+                               /* son */
+                               res = system(cmd);
+                               _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) {
                                TEXTVIEW_INSERT(_("   This key has been imported to your keyring.\n"));
                        } else {