File: main.c

package info (click to toggle)
system-tools-backends 2.10.2-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,412 kB
  • sloc: perl: 15,454; sh: 10,177; ansic: 824; makefile: 202
file content (124 lines) | stat: -rw-r--r-- 3,139 bytes parent folder | download | duplicates (6)
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
/* -*- Mode: C; c-file-style: "gnu"; tab-width: 8 -*- */
/* Copyright (C) 2006 Carlos Garnacho
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
 *
 * Authors: Carlos Garnacho Parro  <carlosg@gnome.org>
 */

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <stdlib.h>
#include "dispatcher.h"
#include "config.h"

static StbDispatcher *dispatcher = NULL;

static void
daemonize (void)
{
  int dev_null_fd, pidfile_fd;
  gchar *str;

  dev_null_fd = open ("/dev/null", O_RDWR);

  dup2 (dev_null_fd, 0);
  dup2 (dev_null_fd, 1);
  dup2 (dev_null_fd, 2);

  if (fork () != 0)
    exit (0);

  setsid ();

  if ((pidfile_fd = open (LOCALSTATEDIR "/run/system-tools-backends.pid",
                          O_CREAT | O_WRONLY,
                          S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) != -1)
    {
      str = g_strdup_printf ("%d", getpid ());
      write (pidfile_fd, str, strlen (str));
      g_free (str);

      close (pidfile_fd);
    }

  close (dev_null_fd);
}

static void
remove_pidfile (void)
{
  unlink (LOCALSTATEDIR "/run/system-tools-backends.pid");
}

void
signal_received (gint signal)
{
  switch (signal)
    {
    case SIGTERM:
    case SIGABRT:
      g_object_unref (dispatcher);
      remove_pidfile ();
      exit (0);
      break;
    default:
      break;
    }
}

int
main (int argc, char *argv[])
{
  GMainLoop *main_loop;
  gboolean debug = FALSE;
  gboolean daemon = FALSE;
  GOptionContext *context;
  GOptionEntry entries[] =
    {
      { "debug",     'd', 0, G_OPTION_ARG_NONE, &debug,     "Debug mode",     NULL },
      { "daemon",    'D', 0, G_OPTION_ARG_NONE, &daemon, "Daemon mode", NULL },
      /* Option kept for backward compatibility */
      { "no-daemon", 'n', G_OPTION_FLAG_HIDDEN | G_OPTION_FLAG_REVERSE,
                             G_OPTION_ARG_NONE, &daemon, "No daemon mode (default)", NULL },
      { NULL }
    };

  g_type_init ();

  /* parse options */
  context = g_option_context_new (NULL);
  g_option_context_add_main_entries (context, entries, NULL);
  g_option_context_parse (context, &argc, &argv, NULL);
  g_option_context_free (context);

  if (daemon)
    daemonize ();

  signal (SIGTERM, signal_received);
  dispatcher = stb_dispatcher_get ();

  if (G_UNLIKELY (debug))
    stb_dispatcher_set_debug (dispatcher, TRUE);

  main_loop = g_main_loop_new (NULL, FALSE);
  g_main_loop_run (main_loop);

  return 0;
}