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