summaryrefslogtreecommitdiff
path: root/src/caja-main.c
blob: 109142461f7957aa8d5dd01be2c30ba44ecc9803 (plain)
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*
 * Caja
 *
 * Copyright (C) 1999, 2000 Red Hat, Inc.
 * Copyright (C) 1999, 2000 Eazel, Inc.
 *
 * Caja 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.
 *
 * Caja 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 St, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * Authors: Elliot Lee <sopwith@redhat.com>,
 *          Darin Adler <darin@bentspoon.com>,
 *          John Sullivan <sullivan@eazel.com>,
 *          Cosimo Cecchi <cosimoc@gnome.org>
 *
 */

/* caja-main.c: Implementation of the routines that drive program lifecycle and main window creation/destruction. */

#include <config.h>
#include <dlfcn.h>
#include <signal.h>
#ifdef HAVE_LOCALE_H
#include <locale.h>
#endif
#ifdef HAVE_MALLOC_H
#include <malloc.h>
#endif
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <gdk/gdkx.h>
#include <gtk/gtk.h>
#include <glib/gi18n.h>
#include <gio/gdesktopappinfo.h>
#include <libxml/parser.h>
#ifdef HAVE_EXEMPI
#include <exempi/xmp.h>
#endif

#include "../eel/eel-debug.h"
#include "../eel/eel-glib-extensions.h"
#include "../eel/eel-self-checks.h"

#include "../libcaja-private/caja-debug-log.h"
#include "../libcaja-private/caja-global-preferences.h"
#include "../libcaja-private/caja-icon-names.h"

#include "../libegg/eggdesktopfile.h"

#include "caja-window.h"

static void dump_debug_log (void)
{
    char *filename;

    filename = g_build_filename (g_get_home_dir (), "caja-debug-log.txt", NULL);
    caja_debug_log_dump (filename, NULL); /* NULL GError */
    g_free (filename);
}

static int debug_log_pipes[2];

static gboolean debug_log_io_cb (GIOChannel *io, GIOCondition condition, gpointer data)
{
    char a;

    while (read (debug_log_pipes[0], &a, 1) != 1)
        ;

    caja_debug_log (TRUE, CAJA_DEBUG_LOG_DOMAIN_USER,
                    "user requested dump of debug log");

    dump_debug_log ();
    return FALSE;
}

static void sigusr1_handler (int sig)
{
    while (write (debug_log_pipes[1], "a", 1) != 1)
        ;
}

/* This is totally broken as we're using non-signal safe
 * calls in sigfatal_handler. Disable by default. */
#ifdef USE_SEGV_HANDLER

/* sigaction structures for the old handlers of these signals */
static struct sigaction old_segv_sa;
static struct sigaction old_abrt_sa;
static struct sigaction old_trap_sa;
static struct sigaction old_fpe_sa;
static struct sigaction old_bus_sa;

static void
sigfatal_handler (int sig)
{
    void (* func) (int);

    /* FIXME: is this totally busted?  We do malloc() inside these functions,
     * and yet we are inside a signal handler...
     */
    caja_debug_log (TRUE, CAJA_DEBUG_LOG_DOMAIN_USER,
                    "debug log dumped due to signal %d", sig);
    dump_debug_log ();

    switch (sig)
    {
    case SIGSEGV:
        func = old_segv_sa.sa_handler;
        break;

    case SIGABRT:
        func = old_abrt_sa.sa_handler;
        break;

    case SIGTRAP:
        func = old_trap_sa.sa_handler;
        break;

    case SIGFPE:
        func = old_fpe_sa.sa_handler;
        break;

    case SIGBUS:
        func = old_bus_sa.sa_handler;
        break;

    default:
        func = NULL;
        break;
    }

    /* this scares me */
    if (func != NULL && func != SIG_IGN && func != SIG_DFL)
        (* func) (sig);
}
#endif

static void
setup_debug_log_signals (void)
{
    struct sigaction sa;
    GIOChannel *io;

    if (pipe (debug_log_pipes) == -1)
        g_error ("Could not create pipe() for debug log");

    io = g_io_channel_unix_new (debug_log_pipes[0]);
    g_io_add_watch (io, G_IO_IN, debug_log_io_cb, NULL);

    sa.sa_handler = sigusr1_handler;
    sigemptyset (&sa.sa_mask);
    sa.sa_flags = 0;
    sigaction (SIGUSR1, &sa, NULL);

    /* This is totally broken as we're using non-signal safe
     * calls in sigfatal_handler. Disable by default. */
#ifdef USE_SEGV_HANDLER
    sa.sa_handler = sigfatal_handler;
    sigemptyset (&sa.sa_mask);
    sa.sa_flags = 0;

    sigaction(SIGSEGV, &sa, &old_segv_sa);
    sigaction(SIGABRT, &sa, &old_abrt_sa);
    sigaction(SIGTRAP, &sa, &old_trap_sa);
    sigaction(SIGFPE,  &sa, &old_fpe_sa);
    sigaction(SIGBUS,  &sa, &old_bus_sa);
#endif
}

static GLogFunc default_log_handler;

static void
log_override_cb (const gchar   *log_domain,
                 GLogLevelFlags log_level,
                 const gchar   *message,
                 gpointer       user_data)
{
    gboolean is_debug;
    gboolean is_milestone;

    is_debug = ((log_level & G_LOG_LEVEL_DEBUG) != 0);
    is_milestone = !is_debug;

    caja_debug_log (is_milestone, CAJA_DEBUG_LOG_DOMAIN_GLOG, "%s", message);

    if (!is_debug)
        (* default_log_handler) (log_domain, log_level, message, user_data);
}

static void
setup_debug_log_glog (void)
{
    default_log_handler = g_log_set_default_handler (log_override_cb, NULL);
}

static void
setup_debug_log (void)
{
    char *config_filename;

    config_filename = g_build_filename (g_get_home_dir (), "caja-debug-log.conf", NULL);
    caja_debug_log_load_configuration (config_filename, NULL); /* NULL GError */
    g_free (config_filename);

    setup_debug_log_signals ();
    setup_debug_log_glog ();
}

static gboolean
running_in_mate (void)
{
    return (g_strcmp0 (g_getenv ("XDG_CURRENT_DESKTOP"), "MATE") == 0)
        || (g_strcmp0 (g_getenv ("XDG_SESSION_DESKTOP"), "mate") == 0)
        || (g_strcmp0 (g_getenv ("DESKTOP_SESSION"), "mate") == 0);
}

static gboolean
running_as_root (void)
{
    return geteuid () == 0;
}

int
main (int argc, char *argv[])
{
	gint retval;
    CajaApplication *application;

#if defined (HAVE_MALLOPT) && defined(M_MMAP_THRESHOLD)
	/* Caja uses lots and lots of small and medium size allocations,
	 * and then a few large ones for the desktop background. By default
	 * glibc uses a dynamic treshold for how large allocations should
	 * be mmaped. Unfortunately this triggers quickly for caja when
	 * it does the desktop background allocations, raising the limit
	 * such that a lot of temporary large allocations end up on the
	 * heap and are thus not returned to the OS. To fix this we set
	 * a hardcoded limit. I don't know what a good value is, but 128K
	 * was the old glibc static limit, lets use that.
	 */
	mallopt (M_MMAP_THRESHOLD, 128 *1024);
#endif

	if (g_getenv ("CAJA_DEBUG") != NULL) {
		eel_make_warnings_and_criticals_stop_in_debugger ();
	}
	
	/* Initialize gettext support */
	bindtextdomain (GETTEXT_PACKAGE, MATELOCALEDIR);
	bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
	textdomain (GETTEXT_PACKAGE);

	g_set_prgname ("caja");

	gdk_set_allowed_backends ("x11");

	if (g_file_test (DATADIR "/applications/caja.desktop", G_FILE_TEST_EXISTS)) {
		egg_set_desktop_file (DATADIR "/applications/caja.desktop");
	}
	
#ifdef HAVE_EXEMPI
	xmp_init();
#endif

	setup_debug_log ();

	/* Initialize the services that we use. */
	LIBXML_TEST_VERSION

    /* Run the caja application. */
    application = caja_application_new();

    retval = g_application_run (G_APPLICATION (application),
                                argc, argv);

    g_object_unref (application);

 	eel_debug_shut_down ();

	return retval;
}