Initial Import
[profile/ivi/clutter-toys.git] / attic / woohaa / totem-resources.c
1 /* 
2  * Copyright (C) 2007 Bastien Nocera <hadess@hadess.net>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  *
18  * The Totem project hereby grant permission for non-gpl compatible GStreamer
19  * plugins to be used and distributed together with GStreamer and Totem. This
20  * permission are above and beyond the permissions granted by the GPL license
21  * Totem is covered by.
22  *
23  * Monday 7th February 2005: Christian Schaller: Add exception clause.
24  * See license_change file for details.
25  *
26  */
27
28 #include "config.h"
29
30 #include <glib.h>
31 #include <glib/gstdio.h>
32 #include <glib/gthread.h>
33
34 #include <unistd.h>
35 #include <stdlib.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <sys/resource.h>
39
40 #include "totem-resources.h"
41
42 #define MAX_HELPER_MEMORY (256 * 1024 * 1024)   /* 256 MB */
43 #define MAX_HELPER_SECONDS (15)                 /* 15 seconds */
44 #define DEFAULT_SLEEP_TIME (30 * G_USEC_PER_SEC) /* 30 seconds */
45
46 static guint sleep_time = DEFAULT_SLEEP_TIME;
47 static gboolean finished = TRUE;
48
49 static void
50 set_resource_limits (const char *input)
51 {
52         struct rlimit limit;
53         struct stat buf;
54         rlim_t max;
55
56         g_return_if_fail (input != NULL);
57
58         max = MAX_HELPER_MEMORY;
59
60         /* Set the maximum virtual size depending on the size
61          * of the file to process, as we wouldn't be able to
62          * mmap it otherwise */
63         if (g_stat (input, &buf) == 0) {
64                 max = MAX_HELPER_MEMORY + buf.st_size;
65         } else if (g_str_has_prefix (input, "file://") != FALSE) {
66                 char *file;
67                 file = g_filename_from_uri (input, NULL, NULL);
68                 if (file != NULL && g_stat (file, &buf) == 0)
69                         max = MAX_HELPER_MEMORY + buf.st_size;
70                 g_free (file);
71         }
72
73         limit.rlim_cur = max;
74         limit.rlim_max = max;
75
76         setrlimit (RLIMIT_DATA, &limit);
77
78         limit.rlim_cur = MAX_HELPER_SECONDS;
79         limit.rlim_max = MAX_HELPER_SECONDS;
80         setrlimit (RLIMIT_CPU, &limit);
81 }
82
83 static gpointer
84 time_monitor (gpointer data)
85 {
86         const char *app_name;
87
88         g_usleep (sleep_time);
89
90         if (finished != FALSE)
91                 g_thread_exit (NULL);
92
93         app_name = g_get_application_name ();
94         if (app_name == NULL)
95                 app_name = g_get_prgname ();
96         g_print ("%s couln't process file: '%s'\n"
97                  "Reason: Took too much time to process.\n",
98                  app_name,
99                  (const char *) data);
100
101         exit (0);
102 }
103
104 void
105 totem_resources_monitor_start (const char *input, guint wall_clock_time)
106 {
107         set_resource_limits (input);
108
109         if (wall_clock_time != 0)
110                 sleep_time = wall_clock_time;
111
112         finished = FALSE;
113         g_thread_create (time_monitor, (gpointer) input, FALSE, NULL);
114 }
115
116 void
117 totem_resources_monitor_stop (void)
118 {
119         finished = TRUE;
120 }
121