seahorse-daemon.c 6.86 KB
Newer Older
1 2 3
/*
 * Seahorse
 *
4
 * Copyright (C) 2005 Stefan Walter
5 6 7 8 9 10 11 12 13 14 15
 *
 * 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
Daniel Mustieles's avatar
Daniel Mustieles committed
16
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
17 18
 */

19 20 21 22 23
#include "config.h"

#include "seahorse-context.h"
#include "seahorse-daemon.h"
#include "seahorse-gtkstock.h"
24
#include "seahorse-pgp-module.h"
25 26 27
#include "seahorse-secure-memory.h"
#include "seahorse-unix-signal.h"

28 29 30
#include "libegg/eggsmclient.h"
#include "libegg/eggdesktopfile.h"

31
#include <glib/gi18n.h>
32

33 34 35 36 37 38 39 40 41 42 43
#include <sys/types.h>
#include <sys/signal.h>

#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <err.h>
#include <unistd.h>
#include <syslog.h>
#include <fcntl.h>

44
static gboolean daemon_no_daemonize = FALSE;
45 46
static gboolean daemon_running = FALSE;
static gboolean daemon_quit = FALSE;
47

48 49
static const GOptionEntry options[] = {
    { "no-daemonize", 'd', 0, G_OPTION_ARG_NONE, &daemon_no_daemonize, 
50
        N_("Do not run seahorse-daemon as a daemon"), NULL },
51
    { NULL }
52 53
};

54 55 56 57 58 59 60 61 62 63 64 65
/**
 * SECTION:seahorse-daemon
 * @short_description: The seahorse-daemon. Offering services like DBus, HKP and
 * Avahi
 *
 **/

/**
*
* Daemonizing and some special handling for GPGME
*
**/
66
static void
67
daemonize ()
68 69 70 71 72 73 74 75 76
{
    /* 
     * We can't use the normal daemon call, because we have
     * special things to do in the parent after forking 
     */

    pid_t pid;
    int i;

77
    if (!daemon_no_daemonize) {
78 79 80 81 82
        switch ((pid = fork ())) {
        case -1:
            err (1, _("couldn't fork process"));
            break;

83
        /* The child */
84 85 86 87 88 89 90 91 92 93 94 95 96
        case 0:
            if (setsid () == -1)
                err (1, _("couldn't create new process group"));

            /* Close std descriptors */
            for (i = 0; i <= 2; i++)
                close (i);
            
            /* Open stdin, stdout and stderr. GPGME doesn't work without this */
            open ("/dev/null", O_RDONLY, 0666);
            open ("/dev/null", O_WRONLY, 0666);
            open ("/dev/null", O_WRONLY, 0666);

97 98 99
            if (chdir ("/tmp") < 0)
                warn ("couldn't change to /tmp directory");
            
100
            return; /* Child process returns */
101 102 103 104 105 106 107 108
        };
    }

    /* Not daemonizing */
    else {
        pid = getpid ();
    }

109
    /* The parent process or not daemonizing ... */
110 111
    if (!daemon_no_daemonize)
        exit (0);
112 113
}

114 115 116 117 118
/**
*
* The signal handler for termination
*
**/
119
static void
120
unix_signal (int signal)
121
{
122 123
    daemon_quit = TRUE;
    if (daemon_running)
124 125 126
        gtk_main_quit ();
}

127 128 129 130 131 132 133
/**
* smclient: ignored
* data: ignored
*
* The handler for the "quit" signal
*
**/
134 135 136 137 138 139 140 141
static void
smclient_quit (EggSMClient *smclient, gpointer data)
{
    daemon_quit = TRUE;
    if (daemon_running)
        gtk_main_quit ();    
}

142 143 144 145 146 147 148 149 150
/**
* log_domain: will be added to the message. Can be NULL
* log_level: the log level
* message: the log message
* user_data: passed to g_log_default_handler, nothing else
*
* Writes log messages
*
**/
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
static void
log_handler (const gchar *log_domain, GLogLevelFlags log_level, 
             const gchar *message, gpointer user_data)
{
    int level;

    /* Note that crit and err are the other way around in syslog */
        
    switch (G_LOG_LEVEL_MASK & log_level) {
    case G_LOG_LEVEL_ERROR:
        level = LOG_CRIT;
        break;
    case G_LOG_LEVEL_CRITICAL:
        level = LOG_ERR;
        break;
    case G_LOG_LEVEL_WARNING:
        level = LOG_WARNING;
        break;
    case G_LOG_LEVEL_MESSAGE:
        level = LOG_NOTICE;
        break;
    case G_LOG_LEVEL_INFO:
        level = LOG_INFO;
        break;
    case G_LOG_LEVEL_DEBUG:
        level = LOG_DEBUG;
        break;
    default:
        level = LOG_ERR;
        break;
    }
    
    /* Log to syslog first */
    if (log_domain)
        syslog (level, "%s: %s", log_domain, message);
    else
        syslog (level, "%s", message);
 
    /* And then to default handler for aborting and stuff like that */
    g_log_default_handler (log_domain, log_level, message, user_data); 
}

193 194 195 196 197
/**
*
* Prepares the logging, sets the log handler to the function "log_handler"
*
**/
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
static void
prepare_logging ()
{
    GLogLevelFlags flags = G_LOG_FLAG_FATAL | G_LOG_LEVEL_ERROR | 
                G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING | 
                G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO;
                
    openlog ("seahorse-daemon", LOG_PID, LOG_AUTH);
    
    g_log_set_handler (NULL, flags, log_handler, NULL);
    g_log_set_handler ("Glib", flags, log_handler, NULL);
    g_log_set_handler ("Gtk", flags, log_handler, NULL);
    g_log_set_handler ("Gnome", flags, log_handler, NULL);
}

213 214 215 216 217 218 219 220 221 222

/**
 * main:
 * @argc: default arguments
 * @argv: default arguments
 *
 * Parses the options, daemonizes, starts HKP, DBus and Avahi
 *
 * Returns: 0 on success
 */
223 224
int main(int argc, char* argv[])
{
225
    GOptionContext *octx = NULL;
226
    GError *error = NULL;
227

228
    seahorse_secure_memory_init ();
229
    
230 231
    octx = g_option_context_new ("");
    g_option_context_add_main_entries (octx, options, GETTEXT_PACKAGE);
232
    g_option_context_add_group (octx, egg_sm_client_get_option_group ());
233

234 235 236 237 238 239
    if (!gtk_init_with_args (&argc, &argv, _("Encryption Daemon (Seahorse)"), 
                             (GOptionEntry *)options, GETTEXT_PACKAGE, &error)) {
	    g_printerr ("seahorse-daemon: %s\n", error->message);
	    g_error_free (error);
	    exit (1);
    }
240 241
     	 
	g_signal_connect (egg_sm_client_get (), "quit", G_CALLBACK (smclient_quit), NULL);
242

243 244 245
    /*
     * All functions after this point have to print messages
     * nicely and not just called exit()
246
     */
247
    daemonize ();
248 249

    /* Handle some signals */
250 251
    seahorse_unix_signal_register (SIGINT, unix_signal);
    seahorse_unix_signal_register (SIGTERM, unix_signal);
252

253 254 255
    /* We log to the syslog */
    prepare_logging ();

256
    /* Insert Icons into Stock */
Stefan Walter's avatar
Stefan Walter committed
257
    seahorse_gtkstock_init ();
258
   
259 260 261
    /* Make the default SeahorseContext */
    seahorse_context_new (SEAHORSE_CONTEXT_APP | SEAHORSE_CONTEXT_DAEMON);

262
    /* Load the various components */
263
    seahorse_pgp_module_init ();
264

265
    seahorse_context_refresh_auto (NULL);
266

Stefan Walter's avatar
Stefan Walter committed
267
    /* Initialize the various daemon components */
268
    seahorse_dbus_server_init ();
269

270
    /* Sometimes we've already gotten a quit signal */
271 272
    if(!daemon_quit) {
        daemon_running = TRUE;
273 274 275
        gtk_main ();
        g_message ("left gtk_main\n");
    }
276

277
    seahorse_dbus_server_cleanup ();
278

279
    g_option_context_free (octx);
280 281
    seahorse_context_destroy (SCTX_APP ());

282 283
    return 0;
}