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



commit 9fdb3099553acead2a25f774033d604ec4dd9f0d
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 |   22 +++++++++++++++++++++-
 1 files changed, 21 insertions(+), 1 deletions(-)
---
diff --git a/libgnome-desktop/gnome-desktop-thumbnail.c b/libgnome-desktop/gnome-desktop-thumbnail.c
index 34dcfd7..8fcfce1 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 {
@@ -1139,6 +1141,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)
 {
@@ -1164,7 +1183,8 @@ 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, &exit_status, NULL))
+                     set_resource_limits, NULL, NULL, NULL,
+                     &exit_status, NULL))
     goto out;
 
   if (exit_status != 0)



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