include polkit.h in all interfaces, edit makefile accordingly
[systembsd.git] / src / interfaces / localed / localed.c
CommitLineData
3b82e3c1 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
5b005882 17#include <unistd.h>
18#include <limits.h>
c12c41f4 19#include <signal.h>
5b005882 20
b6ad18ad 21#include <sys/param.h>
22#include <string.h>
23
4c04b514 24#include <glib/gprintf.h>
8caf1f61 25#include <glib-unix.h>
904d744d 26#include <polkit/polkit.h>
4c04b514 27
1e8c7c88 28#include "localed-gen.h"
a33bcdfa 29#include "localed.h"
5b005882 30
b6ad18ad 31GPtrArray *localed_freeable;
32Locale1 *localed_interf;
33
c12c41f4 34GMainLoop *localed_loop;
35
36guint bus_descriptor;
37gboolean dbus_interface_exported; /* reliable because of gdbus operational guarantees */
38
1ce41045 39/* --- begin method/property/dbus signal code --- */
b6ad18ad 40
41/*static gboolean
42on_handle_set_hostname(Locale1 *hn1_passed_interf,
43 GDBusMethodInvocation *invoc,
44 const gchar *greet,
45 gpointer data) {
46 return FALSE;
47}
48
49const gchar *
50our_get_hostname() {
51
90f54407 52 gchar *hostname_buf, *ret;
53 size_t hostname_divider;
b6ad18ad 54
90f54407 55 hostname_buf = (gchar*) g_malloc0(MAXHOSTNAMELEN);
56 ret = (gchar*) g_malloc0(MAXHOSTNAMELEN);
57 g_ptr_array_add(localed_freeable, hostname_buf);
58 g_ptr_array_add(localed_freeable, ret);
b6ad18ad 59
90f54407 60 if(gethostname(hostname_buf, MAXHOSTNAMELEN))
61 return "";
b6ad18ad 62
90f54407 63 hostname_divider = strcspn(hostname_buf, ".");
b6ad18ad 64
90f54407 65 return strncpy(ret, hostname_buf, hostname_divider);
b6ad18ad 66}*/
67
1ce41045 68/* --- end method/property/dbus signal code, begin bus/name handlers --- */
b6ad18ad 69
70static void localed_on_bus_acquired(GDBusConnection *conn,
71 const gchar *name,
72 gpointer user_data) {
73
254ceec0 74 g_printf("got bus/name, exporting %s's interface...\n", name);
509599f0 75
b6ad18ad 76 localed_interf = locale1_skeleton_new();
77
78 /* attach function pointers to generated struct's method handlers
79 g_signal_connect(localed_interf, "handle-set-hostname", G_CALLBACK(on_handle_set_hostname), NULL); */
80
81 /* set our properties before export
82 locale1_set_hostname(localed_interf, our_get_hostname()); */
83
84 if(!g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(localed_interf),
85 conn,
86 "/org/freedesktop/locale1",
87 NULL)) {
88
509599f0 89 g_printf("failed to export %s's interface!\n", name);
90 localed_mem_clean();
91
92 } else {
93
94 dbus_interface_exported = TRUE;
95 g_printf("exported %s's interface on the system bus...\n", name);
b6ad18ad 96 }
509599f0 97}
b6ad18ad 98
509599f0 99static void localed_on_name_acquired(GDBusConnection *conn,
90f54407 100 const gchar *name,
509599f0 101 gpointer user_data) {
102
90f54407 103 g_printf("success!\n");
b6ad18ad 104}
105
b6ad18ad 106static void localed_on_name_lost(GDBusConnection *conn,
107 const gchar *name,
108 gpointer user_data) {
109
90f54407 110 if(!conn) {
b6ad18ad 111
90f54407 112 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);
113 localed_mem_clean();
114 }
b6ad18ad 115
90f54407 116 g_print("lost name %s, exiting...\n", name);
509599f0 117
118 localed_mem_clean();
b6ad18ad 119}
120
b70beb08 121/* --- end bus/name handlers, begin misc unix functions --- */
122
509599f0 123/* safe call to clean and then exit
124 * this stops our GMainLoop safely before letting main() return */
b70beb08 125void localed_mem_clean() {
126
90f54407 127 g_printf("exiting...\n");
341587db 128
90f54407 129 if(dbus_interface_exported)
130 g_dbus_interface_skeleton_unexport(G_DBUS_INTERFACE_SKELETON(localed_interf));
341587db 131
90f54407 132 if(g_main_loop_is_running(localed_loop))
133 g_main_loop_quit(localed_loop);
341587db 134
b70beb08 135}
136
712eb329 137/* wrapper for glib's unix signal handling; called only once if terminating signal is raised against us */
138gboolean unix_sig_terminate_handler(gpointer data) {
139
90f54407 140 g_printf("caught SIGINT/HUP/TERM, exiting\n");
712eb329 141
90f54407 142 localed_mem_clean();
143 return G_SOURCE_REMOVE;
712eb329 144}
145
2ef2cfe1 146void set_signal_handlers() {
147
148 /* we don't care about its descriptor, we never need to unregister these */
149 g_unix_signal_add(SIGINT, unix_sig_terminate_handler, NULL);
150 g_unix_signal_add(SIGHUP, unix_sig_terminate_handler, NULL);
151 g_unix_signal_add(SIGTERM, unix_sig_terminate_handler, NULL);
152}
153
1e8c7c88 154int main() {
b6ad18ad 155
90f54407 156 set_signal_handlers();
9728ae1f 157
90f54407 158 localed_loop = g_main_loop_new(NULL, TRUE);
159 localed_freeable = g_ptr_array_new();
b6ad18ad 160
90f54407 161 bus_descriptor = g_bus_own_name(G_BUS_TYPE_SYSTEM,
b6ad18ad 162 "org.freedesktop.locale1",
163 G_BUS_NAME_OWNER_FLAGS_NONE,
164 localed_on_bus_acquired,
165 localed_on_name_acquired,
166 localed_on_name_lost,
167 NULL,
168 NULL);
169
90f54407 170 g_main_loop_run(localed_loop);
171 /* runs until single g_main_loop_quit() call is raised inside <interface>_mem_clean() */
172 g_main_loop_unref(localed_loop);
b6ad18ad 173
90f54407 174 /* guaranteed unownable */
175 g_bus_unown_name(bus_descriptor);
b6ad18ad 176
90f54407 177 /* at this point no operations can occur with our data, it is safe to free it + its container */
178 g_ptr_array_free(localed_freeable, TRUE);
b6ad18ad 179
90f54407 180 return 0;
5b005882 181}