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