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> |
cd16a588 |
26 | |
3c3794ac |
27 | #include "localed-gen.h" |
ce19cb8a |
28 | #include "localed.h" |
828caf9a |
29 | |
2eae47af |
30 | GPtrArray *localed_freeable; |
31 | Locale1 *localed_interf; |
32 | |
f50e1f3b |
33 | GMainLoop *localed_loop; |
34 | |
35 | guint bus_descriptor; |
36 | gboolean dbus_interface_exported; /* reliable because of gdbus operational guarantees */ |
37 | |
ff036f75 |
38 | /* --- begin method/property/dbus signal code --- */ |
2eae47af |
39 | |
40 | /*static gboolean |
41 | on_handle_set_hostname(Locale1 *hn1_passed_interf, |
42 | GDBusMethodInvocation *invoc, |
43 | const gchar *greet, |
44 | gpointer data) { |
45 | return FALSE; |
46 | } |
47 | |
48 | const gchar * |
49 | our_get_hostname() { |
50 | |
51 | gchar *hostname_buf, *ret; |
52 | size_t hostname_divider; |
53 | |
54 | hostname_buf = (gchar*) g_malloc0(MAXHOSTNAMELEN); |
55 | ret = (gchar*) g_malloc0(MAXHOSTNAMELEN); |
56 | g_ptr_array_add(localed_freeable, hostname_buf); |
57 | g_ptr_array_add(localed_freeable, ret); |
58 | |
59 | if(gethostname(hostname_buf, MAXHOSTNAMELEN)) |
60 | return ""; |
61 | |
62 | hostname_divider = strcspn(hostname_buf, "."); |
63 | |
64 | return strncpy(ret, hostname_buf, hostname_divider); |
65 | }*/ |
66 | |
ff036f75 |
67 | /* --- end method/property/dbus signal code, begin bus/name handlers --- */ |
2eae47af |
68 | |
69 | static void localed_on_bus_acquired(GDBusConnection *conn, |
70 | const gchar *name, |
71 | gpointer user_data) { |
72 | |
ca5a7f9f |
73 | g_print("got bus/name, exporting %s's interface...\n", name); |
74 | |
2eae47af |
75 | localed_interf = locale1_skeleton_new(); |
76 | |
77 | /* attach function pointers to generated struct's method handlers |
78 | g_signal_connect(localed_interf, "handle-set-hostname", G_CALLBACK(on_handle_set_hostname), NULL); */ |
79 | |
80 | /* set our properties before export |
81 | locale1_set_hostname(localed_interf, our_get_hostname()); */ |
82 | |
83 | if(!g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(localed_interf), |
84 | conn, |
85 | "/org/freedesktop/locale1", |
86 | NULL)) { |
87 | |
ca5a7f9f |
88 | g_printf("failed to export %s's interface!\n", name); |
89 | localed_mem_clean(); |
90 | |
91 | } else { |
92 | |
93 | dbus_interface_exported = TRUE; |
94 | g_printf("exported %s's interface on the system bus...\n", name); |
2eae47af |
95 | } |
ca5a7f9f |
96 | } |
2eae47af |
97 | |
ca5a7f9f |
98 | static void localed_on_name_acquired(GDBusConnection *conn, |
99 | const gchar *name, |
100 | gpointer user_data) { |
101 | |
102 | g_printf("success!\n"); |
2eae47af |
103 | } |
104 | |
2eae47af |
105 | static void localed_on_name_lost(GDBusConnection *conn, |
106 | const gchar *name, |
107 | gpointer user_data) { |
108 | |
ca5a7f9f |
109 | if(!conn) { |
2eae47af |
110 | |
ca5a7f9f |
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 | localed_mem_clean(); |
113 | } |
2eae47af |
114 | |
ca5a7f9f |
115 | g_print("lost name %s, exiting...\n", name); |
116 | |
117 | localed_mem_clean(); |
2eae47af |
118 | } |
119 | |
eb20fe6d |
120 | /* --- end bus/name handlers, begin misc unix functions --- */ |
121 | |
ca5a7f9f |
122 | /* safe call to clean and then exit |
123 | * this stops our GMainLoop safely before letting main() return */ |
eb20fe6d |
124 | void localed_mem_clean() { |
125 | |
5a2ebcdb |
126 | g_printf("exiting...\n"); |
127 | |
128 | if(dbus_interface_exported) |
129 | g_dbus_interface_skeleton_unexport(G_DBUS_INTERFACE_SKELETON(localed_interf)); |
130 | |
131 | if(g_main_loop_is_running(localed_loop)) |
132 | g_main_loop_quit(localed_loop); |
133 | |
eb20fe6d |
134 | } |
135 | |
a6e4b32c |
136 | /* wrapper for glib's unix signal handling; called only once if terminating signal is raised against us */ |
137 | gboolean unix_sig_terminate_handler(gpointer data) { |
138 | |
139 | g_printf("caught SIGINT/HUP/TERM, exiting\n"); |
140 | |
141 | localed_mem_clean(); |
142 | return G_SOURCE_REMOVE; |
143 | } |
144 | |
34d88d96 |
145 | void 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 | |
3c3794ac |
153 | int main() { |
2eae47af |
154 | |
2eae47af |
155 | localed_loop = g_main_loop_new(NULL, TRUE); |
156 | localed_freeable = g_ptr_array_new(); |
157 | |
158 | bus_descriptor = g_bus_own_name(G_BUS_TYPE_SYSTEM, |
159 | "org.freedesktop.locale1", |
160 | G_BUS_NAME_OWNER_FLAGS_NONE, |
161 | localed_on_bus_acquired, |
162 | localed_on_name_acquired, |
163 | localed_on_name_lost, |
164 | NULL, |
165 | NULL); |
166 | |
167 | g_main_loop_run(localed_loop); |
168 | g_main_loop_unref(localed_loop); |
169 | |
170 | g_bus_unown_name(bus_descriptor); |
171 | |
172 | localed_mem_clean(); |
173 | |
3c3794ac |
174 | return 0; |
828caf9a |
175 | } |