summaryrefslogtreecommitdiff
path: root/polkitd/main.c
blob: 49e2810498baa7eaf05a57841eb99891bb2664d8 (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
/***************************************************************************
 * CVSID: $Id$
 *
 * main.c : Main for polkitd
 *
 * Copyright (C) 2006 David Zeuthen, <david@fubar.dk>
 *
 * 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
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 **************************************************************************/

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include <dbus/dbus-glib.h>

#include "polkit-session.h"
#include "polkit-manager.h"

#include "polkit-interface-session-glue.h"
#include "polkit-interface-manager-glue.h"

/** Print out program usage.
 *
 */
static void
usage (int argc, char *argv[])
{
	fprintf (stderr, "\n" "usage : polkitd [--no-daemon] [--verbose]\n");
	fprintf (stderr,
		 "\n"
		 "        -n, --no-daemon      Do not daemonize\n"
		 "        -v, --verbose        Print out debug\n"
		 "        -h, --help           Show this information and exit\n"
		 "        -V, --version        Output version information and exit"
		 "\n"
		 "The PolicyKit daemon maintains a list of privileges and\n"
		 "provides interfaces for changing it.\n"
		 "\n"
		 "For more information visit http://freedesktop.org/Software/hal\n"
		 "\n");
}

static void 
delete_pid (void)
{
	unlink (POLKITD_PID_FILE);
}

int
main (int argc, char *argv[])
{
	DBusGConnection *bus;
	DBusGProxy *bus_proxy;
	GError *error = NULL;
	PolicyKitManager *manager;
	GMainLoop *mainloop;
	guint request_name_result;
	int ret;
	gboolean no_daemon = FALSE;
	gboolean is_verbose = FALSE;
	static const struct option long_options[] = {
		{"help", no_argument, NULL, 'h'},
		{"no-daemon", no_argument, NULL, 'n'},
		{"verbose", no_argument, NULL, 'v'},
		{"version", no_argument, NULL, 'V'},
		{NULL, 0, NULL, 0}
	};


	ret = 1;

	g_type_init ();

	while (TRUE) {
		int c;
		
		c = getopt_long (argc, argv, "nhVv", long_options, NULL);

		if (c == -1)
			break;
		
		switch (c) {
		case 'n':
			no_daemon = TRUE;
			break;

		case 'v':
			is_verbose = TRUE;
			break;

		case 'h':
			usage (argc, argv);
			ret = 0;
			goto out;

		case 'V':
			printf (PACKAGE_NAME " version " PACKAGE_VERSION "\n");
			ret = 0;
			goto out;
			
		default:
			usage (argc, argv);
			goto out;
		}
	}


	if (!no_daemon) {
		int child_pid;
		int dev_null_fd;
		int pf;
		ssize_t written;
		char pid[9];
		

		if (chdir ("/") < 0) {
			g_warning ("Could not chdir to /: %s", strerror (errno));
			goto out;
		}

		child_pid = fork ();
		switch (child_pid) {
		case -1:
			g_warning ("Cannot fork(): %s", strerror (errno));
			goto out;

		case 0:
			/* child */
			dev_null_fd = open ("/dev/null", O_RDWR);
			/* ignore if we can't open /dev/null */
			if (dev_null_fd >= 0) {
				/* attach /dev/null to stdout, stdin, stderr */
				dup2 (dev_null_fd, 0);
				dup2 (dev_null_fd, 1);
				dup2 (dev_null_fd, 2);
				close (dev_null_fd);
			}

			umask (022);
			break;

		default:
			/* parent exits */
			exit (0);
			break;
		}

		/* create session */
		setsid ();

		/* remove old pid file */
		unlink (POLKITD_PID_FILE);

		/* make a new pid file */
		if ((pf = open (POLKITD_PID_FILE, O_WRONLY|O_CREAT|O_TRUNC|O_EXCL, 0644)) > 0) {
			snprintf (pid, sizeof(pid), "%lu\n", (long unsigned) getpid ());
			written = write (pf, pid, strlen(pid));
			close (pf);
			g_atexit (delete_pid);
		}
	} else {
		g_debug (("not becoming a daemon"));
	}

	g_type_init ();

	dbus_g_object_type_install_info (POLKIT_TYPE_MANAGER, &dbus_glib_polkit_manager_object_info);
	dbus_g_object_type_install_info (POLKIT_TYPE_SESSION, &dbus_glib_polkit_session_object_info);
	dbus_g_error_domain_register (POLKIT_MANAGER_ERROR, NULL, POLKIT_MANAGER_TYPE_ERROR);
	dbus_g_error_domain_register (POLKIT_SESSION_ERROR, NULL, POLKIT_SESSION_TYPE_ERROR);

	mainloop = g_main_loop_new (NULL, FALSE);

	bus = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error);
	if (bus == NULL) {
		g_warning ("Couldn't connect to system bus: %s", error->message);
		g_error_free (error);
		goto out;
	}

	bus_proxy = dbus_g_proxy_new_for_name (bus, "org.freedesktop.DBus",
					       "/org/freedesktop/DBus",
					       "org.freedesktop.DBus");
	if (!dbus_g_proxy_call (bus_proxy, "RequestName", &error,
				G_TYPE_STRING, "org.freedesktop.PolicyKit",
				G_TYPE_UINT, 0,
				G_TYPE_INVALID,
				G_TYPE_UINT, &request_name_result,
				G_TYPE_INVALID)) {
		g_warning ("Failed to acquire org.freedesktop.PolicyKit: %s", error->message);
		g_error_free (error);
		goto out;
	}
	


	manager = polkit_manager_new (bus, bus_proxy);

	g_debug ("service running");

	g_main_loop_run (mainloop);

	ret = 0;
out:
	return ret;
}