File: totem-resources.c

package info (click to toggle)
mate-control-center 1.20.4-2~bpo9%2B1
  • links: PTS, VCS
  • area: main
  • in suites: stretch-backports
  • size: 23,420 kB
  • sloc: ansic: 33,247; xml: 1,411; makefile: 833; sh: 23
file content (123 lines) | stat: -rw-r--r-- 3,213 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/* 
 * Copyright (C) 2007 Bastien Nocera <hadess@hadess.net>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA.
 *
 * The Totem project hereby grant permission for non-gpl compatible GStreamer
 * plugins to be used and distributed together with GStreamer and Totem. This
 * permission are above and beyond the permissions granted by the GPL license
 * Totem is covered by.
 *
 * Monday 7th February 2005: Christian Schaller: Add exception clause.
 * See license_change file for details.
 *
 */

#include <config.h>

#include <glib.h>
#include <glib/gstdio.h>

#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/resource.h>

#include "totem-resources.h"

#define MAX_HELPER_MEMORY (256 * 1024 * 1024)	/* 256 MB */
#define MAX_HELPER_SECONDS (15)			/* 15 seconds */
#define DEFAULT_SLEEP_TIME (30 * G_USEC_PER_SEC) /* 30 seconds */

static guint sleep_time = DEFAULT_SLEEP_TIME;
static gboolean finished = TRUE;

static void
set_resource_limits (const char *input)
{
	struct rlimit limit;
	struct stat buf;
	rlim_t max;

	max = MAX_HELPER_MEMORY;

	/* Set the maximum virtual size depending on the size
	 * of the file to process, as we wouldn't be able to
	 * mmap it otherwise */
	if (input == NULL) {
		max = MAX_HELPER_MEMORY;
	} else if (g_stat (input, &buf) == 0) {
		max = MAX_HELPER_MEMORY + buf.st_size;
	} else if (g_str_has_prefix (input, "file://") != FALSE) {
		char *file;
		file = g_filename_from_uri (input, NULL, NULL);
		if (file != NULL && g_stat (file, &buf) == 0)
			max = MAX_HELPER_MEMORY + buf.st_size;
		g_free (file);
	}

	limit.rlim_cur = max;
	limit.rlim_max = max;

	setrlimit (RLIMIT_DATA, &limit);

	limit.rlim_cur = MAX_HELPER_SECONDS;
	limit.rlim_max = MAX_HELPER_SECONDS;
	setrlimit (RLIMIT_CPU, &limit);
}

G_GNUC_NORETURN static gpointer
time_monitor (gpointer data)
{
	const char *app_name;

	g_usleep (sleep_time);

	if (finished != FALSE)
		g_thread_exit (NULL);

	app_name = g_get_application_name ();
	if (app_name == NULL)
		app_name = g_get_prgname ();
	g_print ("%s couldn't process file: '%s'\n"
		 "Reason: Took too much time to process.\n",
		 app_name,
		 (const char *) data);

	exit (0);
}

void
totem_resources_monitor_start (const char *input, gint wall_clock_time)
{
	set_resource_limits (input);

	if (wall_clock_time < 0)
		return;

	if (wall_clock_time > 0)
		sleep_time = wall_clock_time;

	finished = FALSE;
	g_thread_new ("time-monitor", time_monitor, (gpointer) input);
}

void
totem_resources_monitor_stop (void)
{
	finished = TRUE;
}