summaryrefslogtreecommitdiff
path: root/src/main.c
blob: c6e20ef083d1fd2cb2aff8755b461bfa911d8e13 (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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * Gypsy
 *
 * A simple to use and understand GPSD replacement
 * that uses D-Bus, GLib and memory allocations
 *
 * Author: Iain Holmes <iain@gnome.org>
 * Copyright (C) 2007
 */

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>

#include <sys/types.h>
#include <sys/stat.h>

#include <glib.h>

#include <dbus/dbus-protocol.h>
#include <dbus/dbus-glib.h>
#include <dbus/dbus-glib-bindings.h>

#include "gypsy-server.h"

#define GYPSY_NAME "org.freedesktop.Gypsy"
#define DEFAULT_PID_FILE LOCALSTATEDIR"/run/Gypsy.pid"

static GMainLoop *mainloop;
/* This is a bit ugly, but it works */
char* nmea_log = NULL;

static void
gypsy_terminate (GObject *object,
		 gpointer userdata)
{
	g_main_loop_quit (mainloop);
}

static void
write_pidfile (const char *pidfile)
{
	FILE *f;

	if ((f = fopen (pidfile, "w")) == NULL) {
		g_printerr ("Opening %s failed: %s\n", pidfile, strerror (errno));
		return;
	}
	if (fprintf (f, "%d", getpid ()) < 0) {
		g_printerr ("Writing to %s failed: %s\n", pidfile, strerror (errno));
	}

	if (fclose (f)) {
		g_printerr ("Closing %s failed: %s\n", pidfile, strerror (errno));
	}
}

static void
name_owner_changed (DBusGProxy  *proxy,
		    const char  *name,
		    const char  *prev_owner,
		    const char  *new_owner,
		    GypsyServer *server)
{
	if (strcmp (new_owner, "") == 0 && strcmp (name, prev_owner) == 0) {
		gypsy_server_remove_clients (server, prev_owner);
	}
}

int
main (int    argc,
      char **argv)
{
	GOptionContext *context;
	DBusGConnection *conn;
	DBusGProxy *proxy;
	GError *error = NULL;
	guint32 request_name_ret;
	GypsyServer *gypsy;
	gboolean become_daemon = FALSE;
	char *pidfile = NULL;
	char *user_pidfile = NULL;

	GOptionEntry entries[] = {
		{ "nmea-log", 0, 0, G_OPTION_ARG_FILENAME, &nmea_log, "Log NMEA data to FILE.[device]", "FILE" },
		{ "no-daemon", 0, 0, G_OPTION_ARG_NONE, &become_daemon, "Don't become a daemon", NULL },
		{ "pid-file", 0, 0, G_OPTION_ARG_FILENAME, &user_pidfile, "Specify the location of a PID file", "FILE" },
		{ NULL }
	};

	if (getuid () != 0) {
		g_printerr ("You must be root to run %s\n", argv[0]);
		return 1;
	}

	context = g_option_context_new ("- GPS daemon");
	g_option_context_add_main_entries (context, entries, NULL);
	
	if (!g_option_context_parse (context, &argc, &argv, &error)) {
#if GLIB_CHECK_VERSION(2, 14, 0)
		char *help;
		help = g_option_context_get_help (context, TRUE, NULL);
		g_print (help);
		g_free (help);
#else
		g_printerr ("Cannot parse arguments: %s\n", error->message);
#endif
		g_error_free (error);
		return 1;
	}
	
	pidfile = g_strdup (user_pidfile ? user_pidfile : DEFAULT_PID_FILE);

	/* Tricky: become_daemon is FALSE by default, so unless it's TRUE
	   because of a CLI option, it'll become TRUE after this
	*/
	become_daemon = !become_daemon;
	if (become_daemon) {
		if (daemon (0, 0) < 0) {
			int saved_errno;

			saved_errno = errno;
			g_printerr ("Could not daemonize: %s [error %u]\n",
				    g_strerror (saved_errno), saved_errno);
			return 1;
		}
		write_pidfile (pidfile);
	}
	g_free (pidfile);

	g_option_context_free (context);

	umask (022);

	g_type_init ();

	mainloop = g_main_loop_new (NULL, FALSE);
	
	conn = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error);
	if (!conn) {
		g_error ("Error getting bus: %s", error->message);
		return 1;
	}
	
	proxy = dbus_g_proxy_new_for_name (conn, 
					   DBUS_SERVICE_DBUS,
					   DBUS_PATH_DBUS,
					   DBUS_INTERFACE_DBUS);
	if (!org_freedesktop_DBus_request_name (proxy, GYPSY_NAME,
						0,  &request_name_ret,
						&error)) {
		g_error ("Error registering D-Bus service %s: %s", 
			 GYPSY_NAME, error->message);
		return 1;
	}

	/* Just quit if GPS is already running */
	if (request_name_ret != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
		return 1;
	}

	gypsy = g_object_new (GYPSY_TYPE_SERVER, NULL);
	g_signal_connect (G_OBJECT (gypsy), "terminate",
			  G_CALLBACK (gypsy_terminate), NULL);

	dbus_g_proxy_add_signal (proxy, "NameOwnerChanged",
				 G_TYPE_STRING, G_TYPE_STRING, 
				 G_TYPE_STRING, G_TYPE_INVALID);
	dbus_g_proxy_connect_signal (proxy, "NameOwnerChanged",
				     G_CALLBACK (name_owner_changed),
				     gypsy, NULL);

	dbus_g_connection_register_g_object (conn, "/org/freedesktop/Gypsy", G_OBJECT (gypsy));

	g_main_loop_run (mainloop);

	return 0;
}