add polkit auth object, compile it in makefile, add header to interfaces
[systembsd.git] / src / interfaces / localed / localed.c
CommitLineData
baa40e28 1/*
2 * Copyright (c) 2014 Ian Sutton <ian@kremlin.cc>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
828caf9a 17#include <unistd.h>
18#include <limits.h>
f50e1f3b 19#include <signal.h>
828caf9a 20
2eae47af 21#include <sys/param.h>
22#include <string.h>
23
cd16a588 24#include <glib/gprintf.h>
ba9914b6 25#include <glib-unix.h>
eb78abe9 26#include <polkit/polkit.h>
cd16a588 27
3c3794ac 28#include "localed-gen.h"
ce19cb8a 29#include "localed.h"
828caf9a 30
06701625 31#include "../../polkit-auth.h"
32
2eae47af 33GPtrArray *localed_freeable;
34Locale1 *localed_interf;
35
f50e1f3b 36GMainLoop *localed_loop;
37
38guint bus_descriptor;
39gboolean dbus_interface_exported; /* reliable because of gdbus operational guarantees */
40
ff036f75 41/* --- begin method/property/dbus signal code --- */
2eae47af 42
43/*static gboolean
44on_handle_set_hostname(Locale1 *hn1_passed_interf,
45 GDBusMethodInvocation *invoc,
46 const gchar *greet,
47 gpointer data) {
48 return FALSE;
49}
50
51const gchar *
52our_get_hostname() {
53
99ef55dd 54 gchar *hostname_buf, *ret;
55 size_t hostname_divider;
2eae47af 56
99ef55dd 57 hostname_buf = (gchar*) g_malloc0(MAXHOSTNAMELEN);
58 ret = (gchar*) g_malloc0(MAXHOSTNAMELEN);
59 g_ptr_array_add(localed_freeable, hostname_buf);
60 g_ptr_array_add(localed_freeable, ret);
2eae47af 61
99ef55dd 62 if(gethostname(hostname_buf, MAXHOSTNAMELEN))
63 return "";
2eae47af 64
99ef55dd 65 hostname_divider = strcspn(hostname_buf, ".");
2eae47af 66
99ef55dd 67 return strncpy(ret, hostname_buf, hostname_divider);
2eae47af 68}*/
69
ff036f75 70/* --- end method/property/dbus signal code, begin bus/name handlers --- */
2eae47af 71
72static void localed_on_bus_acquired(GDBusConnection *conn,
73 const gchar *name,
74 gpointer user_data) {
75
a0d8931a 76 g_printf("got bus/name, exporting %s's interface...\n", name);
ca5a7f9f 77
2eae47af 78 localed_interf = locale1_skeleton_new();
79
80 /* attach function pointers to generated struct's method handlers
81 g_signal_connect(localed_interf, "handle-set-hostname", G_CALLBACK(on_handle_set_hostname), NULL); */
82
83 /* set our properties before export
84 locale1_set_hostname(localed_interf, our_get_hostname()); */
85
86 if(!g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(localed_interf),
87 conn,
88 "/org/freedesktop/locale1",
89 NULL)) {
90
ca5a7f9f 91 g_printf("failed to export %s's interface!\n", name);
92 localed_mem_clean();
93
94 } else {
95
96 dbus_interface_exported = TRUE;
97 g_printf("exported %s's interface on the system bus...\n", name);
2eae47af 98 }
ca5a7f9f 99}
2eae47af 100
ca5a7f9f 101static void localed_on_name_acquired(GDBusConnection *conn,
99ef55dd 102 const gchar *name,
ca5a7f9f 103 gpointer user_data) {
104
99ef55dd 105 g_printf("success!\n");
2eae47af 106}
107
2eae47af 108static void localed_on_name_lost(GDBusConnection *conn,
109 const gchar *name,
110 gpointer user_data) {
111
99ef55dd 112 if(!conn) {
2eae47af 113
99ef55dd 114 g_printf("failed to connect to the system bus while trying to acquire name '%s': either dbus-daemon isn't running or we don't have permission to push names and/or their interfaces to it.\n", name);
115 localed_mem_clean();
116 }
2eae47af 117
99ef55dd 118 g_print("lost name %s, exiting...\n", name);
ca5a7f9f 119
120 localed_mem_clean();
2eae47af 121}
122
eb20fe6d 123/* --- end bus/name handlers, begin misc unix functions --- */
124
ca5a7f9f 125/* safe call to clean and then exit
126 * this stops our GMainLoop safely before letting main() return */
eb20fe6d 127void localed_mem_clean() {
128
99ef55dd 129 g_printf("exiting...\n");
5a2ebcdb 130
99ef55dd 131 if(dbus_interface_exported)
132 g_dbus_interface_skeleton_unexport(G_DBUS_INTERFACE_SKELETON(localed_interf));
5a2ebcdb 133
99ef55dd 134 if(g_main_loop_is_running(localed_loop))
135 g_main_loop_quit(localed_loop);
5a2ebcdb 136
eb20fe6d 137}
138
a6e4b32c 139/* wrapper for glib's unix signal handling; called only once if terminating signal is raised against us */
140gboolean unix_sig_terminate_handler(gpointer data) {
141
99ef55dd 142 g_printf("caught SIGINT/HUP/TERM, exiting\n");
a6e4b32c 143
99ef55dd 144 localed_mem_clean();
145 return G_SOURCE_REMOVE;
a6e4b32c 146}
147
34d88d96 148void set_signal_handlers() {
149
150 /* we don't care about its descriptor, we never need to unregister these */
151 g_unix_signal_add(SIGINT, unix_sig_terminate_handler, NULL);
152 g_unix_signal_add(SIGHUP, unix_sig_terminate_handler, NULL);
153 g_unix_signal_add(SIGTERM, unix_sig_terminate_handler, NULL);
154}
155
3c3794ac 156int main() {
2eae47af 157
99ef55dd 158 set_signal_handlers();
7a088364 159
99ef55dd 160 localed_loop = g_main_loop_new(NULL, TRUE);
161 localed_freeable = g_ptr_array_new();
2eae47af 162
99ef55dd 163 bus_descriptor = g_bus_own_name(G_BUS_TYPE_SYSTEM,
2eae47af 164 "org.freedesktop.locale1",
165 G_BUS_NAME_OWNER_FLAGS_NONE,
166 localed_on_bus_acquired,
167 localed_on_name_acquired,
168 localed_on_name_lost,
169 NULL,
170 NULL);
171
99ef55dd 172 g_main_loop_run(localed_loop);
173 /* runs until single g_main_loop_quit() call is raised inside <interface>_mem_clean() */
174 g_main_loop_unref(localed_loop);
2eae47af 175
99ef55dd 176 /* guaranteed unownable */
177 g_bus_unown_name(bus_descriptor);
2eae47af 178
99ef55dd 179 /* at this point no operations can occur with our data, it is safe to free it + its container */
180 g_ptr_array_free(localed_freeable, TRUE);
2eae47af 181
99ef55dd 182 return 0;
828caf9a 183}