[gnome-desktop/wip/thumbnail-resources: 32/32] thumbnail: Set a resource limit for thumbnailing scripts



commit 1f91e62af5b6af027e5e555e490b3ed5a78775a6
Author: Jasper St. Pierre <jstpierre mecheye net>
Date:   Mon Oct 1 18:11:21 2012 -0300

    thumbnail: Set a resource limit for thumbnailing scripts
    
    Thumbnailing scripts may exhaust a lot of memory. We also call them
    synchronously, which is a bit annoying, but hard to change without
    breaking the API. Force them to take up to 256MB and take 2 seconds
    of CPU time at most.
    
    Some code based on thumbnailing code from libgsf. Patch written by
    Jody Goldberg.

 libgnome-desktop/gnome-desktop-thumbnail.c |   21 ++++++++++++++++++++-
 1 files changed, 20 insertions(+), 1 deletions(-)
---
diff --git a/libgnome-desktop/gnome-desktop-thumbnail.c b/libgnome-desktop/gnome-desktop-thumbnail.c
index 89f8636..ece6bb3 100644
--- a/libgnome-desktop/gnome-desktop-thumbnail.c
+++ b/libgnome-desktop/gnome-desktop-thumbnail.c
@@ -45,6 +45,8 @@
 #include <glib/gstdio.h>
 #include <libgsystem.h>
 
+#include <sys/resource.h>
+
 #define SECONDS_BETWEEN_STATS 10
 
 struct _GnomeDesktopThumbnailFactoryPrivate {
@@ -1154,6 +1156,23 @@ expand_thumbnailing_script (const char *script,
   return NULL;
 }
 
+#define MAX_HELPER_MEMORY (256 * 1024 * 1024)	/* 256 MB */
+#define MAX_HELPER_SECONDS (2)			/* 2 seconds */
+
+static void
+set_resource_limits (gpointer user_data)
+{
+  struct rlimit limit;
+
+  limit.rlim_cur = MAX_HELPER_MEMORY;
+  limit.rlim_max = MAX_HELPER_MEMORY;
+  setrlimit (RLIMIT_AS, &limit);
+
+  limit.rlim_cur = MAX_HELPER_SECONDS;
+  limit.rlim_max = MAX_HELPER_SECONDS;
+  setrlimit (RLIMIT_CPU, &limit);
+}
+
 static GdkPixbuf *
 run_script (char *script, const char *uri, int size)
 {
@@ -1179,7 +1198,7 @@ run_script (char *script, const char *uri, int size)
     goto out;
 
   if (!g_spawn_sync (NULL, argv, NULL, G_SPAWN_SEARCH_PATH,
-                     NULL, NULL, NULL, NULL,
+                     set_resource_limits, NULL, NULL, NULL,
                      &exit_status, NULL))
     goto out;
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]