Blame src/server.c

Packit Service 3bdf47
/*
Packit Service 3bdf47
 * Copyright © 2001, 2002 Havoc Pennington
Packit Service 3bdf47
 * Copyright © 2002 Red Hat, Inc.
Packit Service 3bdf47
 * Copyright © 2002 Sun Microsystems
Packit Service 3bdf47
 * Copyright © 2003 Mariano Suarez-Alvarez
Packit Service 3bdf47
 * Copyright © 2008, 2010, 2011 Christian Persch
Packit Service 3bdf47
 *
Packit Service 3bdf47
 * This program is free software: you can redistribute it and/or modify
Packit Service 3bdf47
 * it under the terms of the GNU General Public License as published by
Packit Service 3bdf47
 * the Free Software Foundation, either version 3 of the License, or
Packit Service 3bdf47
 * (at your option) any later version.
Packit Service 3bdf47
 *
Packit Service 3bdf47
 * This program is distributed in the hope that it will be useful,
Packit Service 3bdf47
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 3bdf47
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit Service 3bdf47
 * GNU General Public License for more details.
Packit Service 3bdf47
 *
Packit Service 3bdf47
 * You should have received a copy of the GNU General Public License
Packit Service 3bdf47
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
Packit Service 3bdf47
 */
Packit Service 3bdf47
Packit Service 3bdf47
#include <config.h>
Packit Service 3bdf47
Packit Service 3bdf47
#include <errno.h>
Packit Service 3bdf47
#include <locale.h>
Packit Service 3bdf47
#include <pthread.h>
Packit Service 3bdf47
#include <stdlib.h>
Packit Service 3bdf47
#include <time.h>
Packit Service 3bdf47
#include <unistd.h>
Packit Service 3bdf47
#include <sys/resource.h>
Packit Service 3bdf47
#include <sys/types.h>
Packit Service 3bdf47
Packit Service 3bdf47
#include <glib.h>
Packit Service 3bdf47
#include <glib/gi18n.h>
Packit Service 3bdf47
#include <glib/gstdio.h>
Packit Service 3bdf47
#include <gio/gio.h>
Packit Service 3bdf47
Packit Service 3bdf47
#include "terminal-app.h"
Packit Service 3bdf47
#include "terminal-debug.h"
Packit Service 3bdf47
#include "terminal-gdbus.h"
Packit Service 3bdf47
#include "terminal-i18n.h"
Packit Service 3bdf47
#include "terminal-defines.h"
Packit Service 3bdf47
#include "terminal-libgsystem.h"
Packit Service 3bdf47
Packit Service 3bdf47
static char *app_id = NULL;
Packit Service 3bdf47
Packit Service 3bdf47
#define INACTIVITY_TIMEOUT (100 /* ms */)
Packit Service 3bdf47
Packit Service 3bdf47
static gboolean
Packit Service 3bdf47
option_app_id_cb (const gchar *option_name,
Packit Service 3bdf47
                    const gchar *value,
Packit Service 3bdf47
                    gpointer     data,
Packit Service 3bdf47
                    GError     **error)
Packit Service 3bdf47
{
Packit Service 3bdf47
  if (!g_application_id_is_valid (value) ||
Packit Service 3bdf47
      !g_dbus_is_name (value)) {
Packit Service 3bdf47
    g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
Packit Service 3bdf47
                 "\"%s\" is not a valid application ID", value);
Packit Service 3bdf47
    return FALSE;
Packit Service 3bdf47
  }
Packit Service 3bdf47
Packit Service 3bdf47
  g_free (app_id);
Packit Service 3bdf47
  app_id = g_strdup (value);
Packit Service 3bdf47
Packit Service 3bdf47
  return TRUE;
Packit Service 3bdf47
}
Packit Service 3bdf47
Packit Service 3bdf47
static const GOptionEntry options[] = {
Packit Service 3bdf47
  { "app-id", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_CALLBACK, option_app_id_cb, "Application ID", "ID" },
Packit Service 3bdf47
  { NULL }
Packit Service 3bdf47
};
Packit Service 3bdf47
Packit Service 3bdf47
/* We use up to 8 FDs per terminal, so let's bump the limit way up.
Packit Service 3bdf47
 * However we need to restore the original limit for the child processes.
Packit Service 3bdf47
 */
Packit Service 3bdf47
Packit Service 3bdf47
static struct rlimit sv_rlimit_nofile;
Packit Service 3bdf47
Packit Service 3bdf47
static void
Packit Service 3bdf47
atfork_child_restore_rlimit_nofile (void)
Packit Service 3bdf47
{
Packit Service 3bdf47
  if (setrlimit (RLIMIT_NOFILE, &sv_rlimit_nofile) < 0)
Packit Service 3bdf47
    _exit (127);
Packit Service 3bdf47
}
Packit Service 3bdf47
Packit Service 3bdf47
static gboolean
Packit Service 3bdf47
increase_rlimit_nofile (void)
Packit Service 3bdf47
{
Packit Service 3bdf47
  struct rlimit l;
Packit Service 3bdf47
Packit Service 3bdf47
  if (getrlimit (RLIMIT_NOFILE, &sv_rlimit_nofile) < 0)
Packit Service 3bdf47
    return FALSE;
Packit Service 3bdf47
Packit Service 3bdf47
  if (pthread_atfork (NULL, NULL, atfork_child_restore_rlimit_nofile) != 0)
Packit Service 3bdf47
    return FALSE;
Packit Service 3bdf47
Packit Service 3bdf47
  l.rlim_cur = l.rlim_max = sv_rlimit_nofile.rlim_max;
Packit Service 3bdf47
  if (setrlimit (RLIMIT_NOFILE, &l) < 0)
Packit Service 3bdf47
    return FALSE;
Packit Service 3bdf47
Packit Service 3bdf47
  return TRUE;
Packit Service 3bdf47
}
Packit Service 3bdf47
Packit Service 3bdf47
static int
Packit Service 3bdf47
init_server (int argc,
Packit Service 3bdf47
             char *argv[],
Packit Service 3bdf47
             GApplication **application)
Packit Service 3bdf47
{
Packit Service 3bdf47
  if (G_UNLIKELY ((getuid () != geteuid () ||
Packit Service 3bdf47
                  getgid () != getegid ()) &&
Packit Service 3bdf47
                  geteuid () == 0 &&
Packit Service 3bdf47
                  getegid () == 0)) {
Packit Service 3bdf47
    g_printerr ("Wrong euid/egid, exiting.\n");
Packit Service 3bdf47
    return _EXIT_FAILURE_WRONG_ID;
Packit Service 3bdf47
  }
Packit Service 3bdf47
Packit Service 3bdf47
  if (setlocale (LC_ALL, "") == NULL) {
Packit Service 3bdf47
    g_printerr ("Locale not supported.\n");
Packit Service 3bdf47
    return _EXIT_FAILURE_UNSUPPORTED_LOCALE;
Packit Service 3bdf47
  }
Packit Service 3bdf47
Packit Service 3bdf47
  terminal_i18n_init (TRUE);
Packit Service 3bdf47
  const char *charset;
Packit Service 3bdf47
  if (!g_get_charset (&charset)) {
Packit Service 3bdf47
    g_printerr ("Non UTF-8 locale (%s) is not supported!\n", charset);
Packit Service 3bdf47
    return _EXIT_FAILURE_NO_UTF8;
Packit Service 3bdf47
  }
Packit Service 3bdf47
Packit Service 3bdf47
  /* Sanitise environment */
Packit Service 3bdf47
  g_unsetenv ("DBUS_STARTER_BUS_TYPE");
Packit Service 3bdf47
Packit Service 3bdf47
  /* Not interested in silly debug spew polluting the journal, bug #749195 */
Packit Service 3bdf47
  if (g_getenv ("G_ENABLE_DIAGNOSTIC") == NULL)
Packit Service 3bdf47
    g_setenv ("G_ENABLE_DIAGNOSTIC", "0", TRUE);
Packit Service 3bdf47
Packit Service 3bdf47
  _terminal_debug_init ();
Packit Service 3bdf47
Packit Service 3bdf47
  /* Change directory to $HOME so we don't prevent unmounting, e.g. if the
Packit Service 3bdf47
   * factory is started by nautilus-open-terminal. See bug #565328.
Packit Service 3bdf47
   * On failure back to /.
Packit Service 3bdf47
   */
Packit Service 3bdf47
  const char *home_dir = g_get_home_dir ();
Packit Service 3bdf47
  if (home_dir == NULL || chdir (home_dir) < 0)
Packit Service 3bdf47
    (void) chdir ("/");
Packit Service 3bdf47
Packit Service 3bdf47
  g_set_prgname ("gnome-terminal-server");
Packit Service 3bdf47
  g_set_application_name (_("Terminal"));
Packit Service 3bdf47
Packit Service 3bdf47
  GError *error = NULL;
Packit Service 3bdf47
  if (!gtk_init_with_args (&argc, &argv, NULL, options, NULL, &error)) {
Packit Service 3bdf47
    if (error != NULL) {
Packit Service 3bdf47
      g_printerr ("Failed to parse arguments: %s\n", error->message);
Packit Service 3bdf47
      g_error_free (error);
Packit Service 3bdf47
    }
Packit Service 3bdf47
    return _EXIT_FAILURE_GTK_INIT;
Packit Service 3bdf47
  }
Packit Service 3bdf47
Packit Service 3bdf47
  if (!increase_rlimit_nofile ()) {
Packit Service 3bdf47
    g_printerr ("Failed to increase RLIMIT_NOFILE: %m\n");
Packit Service 3bdf47
  }
Packit Service 3bdf47
Packit Service 3bdf47
  /* Now we can create the app */
Packit Service 3bdf47
  GApplication *app = terminal_app_new (app_id);
Packit Service 3bdf47
  g_free (app_id);
Packit Service 3bdf47
  app_id = NULL;
Packit Service 3bdf47
Packit Service 3bdf47
  /* We stay around a bit after the last window closed */
Packit Service 3bdf47
  g_application_set_inactivity_timeout (app, INACTIVITY_TIMEOUT);
Packit Service 3bdf47
Packit Service 3bdf47
  *application = app;
Packit Service 3bdf47
  return 0;
Packit Service 3bdf47
}
Packit Service 3bdf47
Packit Service 3bdf47
int
Packit Service 3bdf47
main (int argc,
Packit Service 3bdf47
      char *argv[])
Packit Service 3bdf47
{
Packit Service 3bdf47
  gs_unref_object GApplication *app = NULL;
Packit Service 3bdf47
  int r = init_server (argc, argv, &app);
Packit Service 3bdf47
  if (r != 0)
Packit Service 3bdf47
    return r;
Packit Service 3bdf47
Packit Service 3bdf47
  return g_application_run (app, 0, NULL);
Packit Service 3bdf47
}