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