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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  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 
33 #include <unistd.h>
34 #include <stdlib.h>
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <sys/resource.h>
38 
39 #include "totem-resources.h"
40 
41 #define MAX_HELPER_MEMORY (256 * 1024 * 1024)	/* 256 MB */
42 #define MAX_HELPER_SECONDS (15)			/* 15 seconds */
43 #define DEFAULT_SLEEP_TIME (30 * G_USEC_PER_SEC) /* 30 seconds */
44 
45 static guint sleep_time = DEFAULT_SLEEP_TIME;
46 static gboolean finished = TRUE;
47 
48 static void
set_resource_limits(const char * input)49 set_resource_limits (const char *input)
50 {
51 	struct rlimit limit;
52 	struct stat buf;
53 	rlim_t max;
54 
55 	max = MAX_HELPER_MEMORY;
56 
57 	/* Set the maximum virtual size depending on the size
58 	 * of the file to process, as we wouldn't be able to
59 	 * mmap it otherwise */
60 	if (input == NULL) {
61 		max = MAX_HELPER_MEMORY;
62 	} else if (g_stat (input, &buf) == 0) {
63 		max = MAX_HELPER_MEMORY + buf.st_size;
64 	} else if (g_str_has_prefix (input, "file://") != FALSE) {
65 		char *file;
66 		file = g_filename_from_uri (input, NULL, NULL);
67 		if (file != NULL && g_stat (file, &buf) == 0)
68 			max = MAX_HELPER_MEMORY + buf.st_size;
69 		g_free (file);
70 	}
71 
72 	limit.rlim_cur = max;
73 	limit.rlim_max = max;
74 
75 	setrlimit (RLIMIT_DATA, &limit);
76 
77 	limit.rlim_cur = MAX_HELPER_SECONDS;
78 	limit.rlim_max = MAX_HELPER_SECONDS;
79 	setrlimit (RLIMIT_CPU, &limit);
80 }
81 
82 G_GNUC_NORETURN static gpointer
time_monitor(gpointer data)83 time_monitor (gpointer data)
84 {
85 	const char *app_name;
86 
87 	g_usleep (sleep_time);
88 
89 	if (finished != FALSE)
90 		g_thread_exit (NULL);
91 
92 	app_name = g_get_application_name ();
93 	if (app_name == NULL)
94 		app_name = g_get_prgname ();
95 	g_print ("%s couldn't process file: '%s'\n"
96 		 "Reason: Took too much time to process.\n",
97 		 app_name,
98 		 (const char *) data);
99 
100 	exit (0);
101 }
102 
103 void
totem_resources_monitor_start(const char * input,gint wall_clock_time)104 totem_resources_monitor_start (const char *input, gint wall_clock_time)
105 {
106 	set_resource_limits (input);
107 
108 	if (wall_clock_time < 0)
109 		return;
110 
111 	if (wall_clock_time > 0)
112 		sleep_time = wall_clock_time;
113 
114 	finished = FALSE;
115 	g_thread_new ("time-monitor", time_monitor, (gpointer) input);
116 }
117 
118 void
totem_resources_monitor_stop(void)119 totem_resources_monitor_stop (void)
120 {
121 	finished = TRUE;
122 }
123 
124