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 | |
a35a69c5 |
17 | #include <unistd.h> |
18 | #include <limits.h> |
0f339959 |
19 | #include <signal.h> |
fd8852d9 |
20 | |
d15318db |
21 | #include <sys/param.h> |
28cac2f0 |
22 | #include <string.h> |
d15318db |
23 | |
4c04b514 |
24 | #include <glib/gprintf.h> |
8caf1f61 |
25 | #include <glib-unix.h> |
4c04b514 |
26 | |
1e8c7c88 |
27 | #include "hostnamed-gen.h" |
0f339959 |
28 | #include "hostnamed.h" |
a35a69c5 |
29 | |
fd8852d9 |
30 | GPtrArray *hostnamed_freeable; |
1be94ede |
31 | Hostname1 *hostnamed_interf; |
3d53b501 |
32 | |
0f339959 |
33 | GMainLoop *hostnamed_loop; |
34 | |
35 | guint bus_descriptor; |
36 | gboolean dbus_interface_exported; /* reliable because of gdbus operational guarantees */ |
37 | |
1ce41045 |
38 | /* --- begin method/property/dbus signal code --- */ |
76b67a18 |
39 | |
3d53b501 |
40 | static gboolean |
1be94ede |
41 | on_handle_set_hostname(Hostname1 *hn1_passed_interf, |
3d53b501 |
42 | GDBusMethodInvocation *invoc, |
43 | const gchar *greet, |
44 | gpointer data) { |
45 | return FALSE; |
46 | } |
47 | |
48 | static gboolean |
1be94ede |
49 | on_handle_set_static_hostname(Hostname1 *hn1_passed_interf, |
3d53b501 |
50 | GDBusMethodInvocation *invoc, |
51 | const gchar *greet, |
52 | gpointer data) { |
53 | return FALSE; |
54 | } |
55 | |
56 | static gboolean |
1be94ede |
57 | on_handle_set_pretty_hostname(Hostname1 *hn1_passed_interf, |
3d53b501 |
58 | GDBusMethodInvocation *invoc, |
59 | const gchar *greet, |
60 | gpointer data) { |
61 | return FALSE; |
62 | } |
63 | |
64 | static gboolean |
1be94ede |
65 | on_handle_set_chassis(Hostname1 *hn1_passed_interf, |
3d53b501 |
66 | GDBusMethodInvocation *invoc, |
67 | const gchar *greet, |
68 | gpointer data) { |
69 | return FALSE; |
70 | } |
71 | |
72 | static gboolean |
1be94ede |
73 | on_handle_set_icon_name(Hostname1 *hn1_passed_interf, |
3d53b501 |
74 | GDBusMethodInvocation *invoc, |
75 | const gchar *greet, |
76 | gpointer data) { |
77 | return FALSE; |
78 | } |
79 | |
76b67a18 |
80 | /* note: all hostnamed/hostname1's properties are read-only, |
81 | * and do not need set_ functions, gdbus-codegen realized |
82 | * this from the XML and handled the to-be error of trying |
83 | * to set a read-only property's value |
84 | */ |
85 | |
86 | const gchar * |
87 | our_get_hostname() { |
88 | |
90f54407 |
89 | gchar *hostname_buf, *ret; |
90 | size_t hostname_divider; |
d15318db |
91 | |
a2fffc07 |
92 | hostname_buf = (gchar*) g_malloc0(MAXHOSTNAMELEN); |
90f54407 |
93 | ret = (gchar*) g_malloc0(MAXHOSTNAMELEN); |
a2fffc07 |
94 | |
90f54407 |
95 | g_ptr_array_add(hostnamed_freeable, hostname_buf); |
96 | g_ptr_array_add(hostnamed_freeable, ret); |
7323a4e4 |
97 | |
a2fffc07 |
98 | if(gethostname(hostname_buf, MAXHOSTNAMELEN) || g_strcmp0(hostname_buf, "") == 0) |
99 | return "localhost"; |
d15318db |
100 | |
90f54407 |
101 | hostname_divider = strcspn(hostname_buf, "."); |
28cac2f0 |
102 | |
90f54407 |
103 | return strncpy(ret, hostname_buf, hostname_divider); |
76b67a18 |
104 | } |
105 | |
106 | const gchar * |
107 | our_get_static_hostname() { |
108 | |
af14252f |
109 | const gchar *pretty_hostname; |
110 | const gchar *ret; |
a2fffc07 |
111 | |
112 | pretty_hostname = our_get_pretty_hostname(); |
113 | |
114 | if(g_strcmp0(pretty_hostname, "") == 0) |
115 | ret = our_get_hostname(); |
116 | |
af14252f |
117 | else if((ret = g_hostname_to_ascii(pretty_hostname))) { |
a2fffc07 |
118 | |
af14252f |
119 | g_ptr_array_add(hostnamed_freeable, (gpointer)ret); |
a2fffc07 |
120 | return ret; |
121 | } |
122 | |
123 | return ret; |
76b67a18 |
124 | } |
125 | |
126 | const gchar * |
127 | our_get_pretty_hostname() { |
128 | |
a2fffc07 |
129 | GKeyFile *config; |
130 | gchar *ret; |
131 | |
132 | if(g_key_file_load_from_file(config, "/etc/systemd_compat.conf", G_KEY_FILE_NONE, NULL) |
af14252f |
133 | && (ret = g_key_file_get_value(config, "hostnamed", "PrettyHostname", NULL))) { /* ret might need to be freed, docs dont specify but i am suspicious */ |
a2fffc07 |
134 | |
135 | g_free(config); |
136 | return ret; |
137 | } |
138 | |
139 | if(config) |
140 | g_free(config); |
141 | |
142 | return ""; |
76b67a18 |
143 | } |
144 | |
145 | const gchar * |
146 | our_get_chassis() { |
147 | |
148 | return "TODO"; |
149 | } |
150 | |
151 | const gchar * |
152 | our_get_icon_name() { |
153 | |
154 | return "TODO"; |
155 | } |
156 | |
157 | const gchar * |
158 | our_get_kernel_name() { |
159 | |
160 | return "TODO"; |
161 | } |
162 | |
163 | const gchar * |
164 | our_get_kernel_version() { |
165 | |
166 | return "TODO"; |
167 | } |
168 | |
169 | const gchar * |
170 | our_get_kernel_release() { |
171 | |
172 | return "TODO"; |
173 | } |
174 | |
175 | const gchar * |
176 | our_get_os_cpename() { |
177 | |
178 | return "TODO"; |
179 | } |
180 | |
181 | const gchar * |
182 | our_get_os_pretty_name() { |
183 | |
184 | return "TODO"; |
185 | } |
186 | |
1ce41045 |
187 | /* --- end method/property/dbus signal code, begin bus/name handlers --- */ |
0df0018d |
188 | |
5b005882 |
189 | static void hostnamed_on_bus_acquired(GDBusConnection *conn, |
1cd5e6fe |
190 | const gchar *name, |
191 | gpointer user_data) { |
d1e1db9e |
192 | |
0f339959 |
193 | g_printf("got bus/name, exporting %s's interface...\n", name); |
d1e1db9e |
194 | |
90f54407 |
195 | hostnamed_interf = hostname1_skeleton_new(); |
3d53b501 |
196 | |
76b67a18 |
197 | /* attach function pointers to generated struct's method handlers */ |
3d53b501 |
198 | g_signal_connect(hostnamed_interf, "handle-set-hostname", G_CALLBACK(on_handle_set_hostname), NULL); |
199 | g_signal_connect(hostnamed_interf, "handle-set-static-hostname", G_CALLBACK(on_handle_set_static_hostname), NULL); |
200 | g_signal_connect(hostnamed_interf, "handle-set-pretty-hostname", G_CALLBACK(on_handle_set_pretty_hostname), NULL); |
201 | g_signal_connect(hostnamed_interf, "handle-set-chassis", G_CALLBACK(on_handle_set_chassis), NULL); |
202 | g_signal_connect(hostnamed_interf, "handle-set-icon-name", G_CALLBACK(on_handle_set_icon_name), NULL); |
203 | |
76b67a18 |
204 | /* set our properties before export */ |
1be94ede |
205 | hostname1_set_hostname(hostnamed_interf, our_get_hostname()); |
206 | hostname1_set_static_hostname(hostnamed_interf, our_get_static_hostname()); |
207 | hostname1_set_pretty_hostname(hostnamed_interf, our_get_pretty_hostname()); |
208 | hostname1_set_chassis(hostnamed_interf, our_get_chassis()); |
209 | hostname1_set_icon_name(hostnamed_interf, our_get_icon_name()); |
210 | hostname1_set_kernel_name(hostnamed_interf, our_get_kernel_name()); |
211 | hostname1_set_kernel_version(hostnamed_interf, our_get_kernel_version()); |
212 | hostname1_set_kernel_release(hostnamed_interf, our_get_kernel_release()); |
213 | hostname1_set_operating_system_cpename(hostnamed_interf, our_get_os_cpename()); |
214 | hostname1_set_operating_system_pretty_name(hostnamed_interf, our_get_os_pretty_name()); |
76b67a18 |
215 | |
3d53b501 |
216 | if(!g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(hostnamed_interf), |
1be94ede |
217 | conn, |
218 | "/org/freedesktop/hostname1", |
219 | NULL)) { |
3d53b501 |
220 | |
0f339959 |
221 | g_printf("failed to export %s's interface!\n", name); /* unusual edge case, TODO check errno */ |
90f54407 |
222 | hostnamed_mem_clean(); |
3d53b501 |
223 | |
0f339959 |
224 | } else { |
ea207ed3 |
225 | |
90f54407 |
226 | dbus_interface_exported = TRUE; |
227 | g_printf("exported %s's interface on the system bus...\n", name); |
228 | } |
0f339959 |
229 | } |
1e8c7c88 |
230 | |
0f339959 |
231 | static void hostnamed_on_name_acquired(GDBusConnection *conn, |
90f54407 |
232 | const gchar *name, |
0f339959 |
233 | gpointer user_data) { |
234 | |
235 | g_printf("success!\n"); |
1e8c7c88 |
236 | } |
237 | |
5b005882 |
238 | static void hostnamed_on_name_lost(GDBusConnection *conn, |
1be94ede |
239 | const gchar *name, |
240 | gpointer user_data) { |
80043b36 |
241 | |
90f54407 |
242 | if(!conn) { |
0f339959 |
243 | |
90f54407 |
244 | 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); |
245 | hostnamed_mem_clean(); |
246 | } |
0f339959 |
247 | |
509599f0 |
248 | g_printf("lost name %s, exiting...\n", name); |
fd8852d9 |
249 | |
1cd5e6fe |
250 | hostnamed_mem_clean(); |
0f339959 |
251 | } |
252 | |
253 | /* --- end bus/name handlers, begin misc unix functions --- */ |
254 | |
255 | /* safe call to clean and then exit |
509599f0 |
256 | * this stops our GMainLoop safely before letting main() return */ |
0f339959 |
257 | void hostnamed_mem_clean() { |
258 | |
90f54407 |
259 | g_printf("exiting...\n"); |
0f339959 |
260 | |
90f54407 |
261 | if(dbus_interface_exported) |
262 | g_dbus_interface_skeleton_unexport(G_DBUS_INTERFACE_SKELETON(hostnamed_interf)); |
0f339959 |
263 | |
90f54407 |
264 | if(g_main_loop_is_running(hostnamed_loop)) |
265 | g_main_loop_quit(hostnamed_loop); |
fd8852d9 |
266 | |
ea207ed3 |
267 | } |
268 | |
0f339959 |
269 | /* wrapper for glib's unix signal handling; called only once if terminatating signal is raised against us */ |
270 | gboolean unix_sig_terminate_handler(gpointer data) { |
c62bceb7 |
271 | |
90f54407 |
272 | g_printf("caught SIGINT/HUP/TERM, exiting\n"); |
0f339959 |
273 | |
90f54407 |
274 | hostnamed_mem_clean(); |
275 | return G_SOURCE_REMOVE; |
0f339959 |
276 | } |
277 | |
278 | void set_signal_handlers() { |
279 | |
90f54407 |
280 | /* we don't care about its descriptor, we never need to unregister these */ |
281 | g_unix_signal_add(SIGINT, unix_sig_terminate_handler, NULL); |
282 | g_unix_signal_add(SIGHUP, unix_sig_terminate_handler, NULL); |
283 | g_unix_signal_add(SIGTERM, unix_sig_terminate_handler, NULL); |
04cc16f2 |
284 | |
90f54407 |
285 | /* TODO: the "only once" guarantee only counts towards specific signals. |
286 | * make sure calling a SIGINT and SIGHUP doesn't cause term_handler() |
287 | * to be called twice */ |
0f339959 |
288 | } |
289 | |
290 | int main() { |
90f54407 |
291 | |
292 | set_signal_handlers(); |
387173cb |
293 | |
90f54407 |
294 | hostnamed_loop = g_main_loop_new(NULL, TRUE); |
295 | hostnamed_freeable = g_ptr_array_new(); |
fd8852d9 |
296 | |
90f54407 |
297 | bus_descriptor = g_bus_own_name(G_BUS_TYPE_SYSTEM, |
0f339959 |
298 | "org.freedesktop.hostname1", |
299 | G_BUS_NAME_OWNER_FLAGS_NONE, |
300 | hostnamed_on_bus_acquired, |
301 | hostnamed_on_name_acquired, |
302 | hostnamed_on_name_lost, |
303 | NULL, |
304 | NULL); |
496f5d66 |
305 | |
90f54407 |
306 | g_main_loop_run(hostnamed_loop); |
307 | /* runs until single g_main_loop_quit() call is raised inside <interface>_mem_clean() */ |
308 | g_main_loop_unref(hostnamed_loop); |
1e8c7c88 |
309 | |
90f54407 |
310 | /* guaranteed unownable */ |
311 | g_bus_unown_name(bus_descriptor); |
c62bceb7 |
312 | |
90f54407 |
313 | /* at this point no operations can occur with our data, it is safe to free it + its container */ |
314 | g_ptr_array_free(hostnamed_freeable, TRUE); |
7323a4e4 |
315 | |
90f54407 |
316 | return 0; |
a35a69c5 |
317 | } |
318 | |
a6f11205 |
319 | /* TODO figure out DMI variables on obsd */ |
a35a69c5 |
320 | /*static gchar *guess_icon_name() { |
321 | |
322 | gchar *filebuf = NULL; |
323 | gchar *ret = NULL; |
324 | |
1cd5e6fe |
325 | #if defined(__i386__) || defined(__x86_64__) |
39df6847 |
326 | |
a35a69c5 |
327 | Taken with a few minor changes from systemd's hostnamed.c, |
328 | copyright 2011 Lennart Poettering. |
329 | |
330 | See the SMBIOS Specification 2.7.1 section 7.4.1 for |
331 | details about the values listed here: |
332 | |
333 | http://www.dmtf.org/sites/default/files/standards/documents/DSP0134_2.7.1.pdf |
39df6847 |
334 | |
a35a69c5 |
335 | |
336 | if (g_file_get_contents ("/sys/class/dmi/id/chassis_type", &filebuf, NULL, NULL)) { |
337 | switch (g_ascii_strtoull (filebuf, NULL, 10)) { |
338 | case 0x3: |
339 | case 0x4: |
340 | case 0x5: |
341 | case 0x6: |
342 | case 0x7: |
343 | ret = g_strdup ("computer-desktop"); |
344 | goto out; |
345 | case 0x9: |
346 | case 0xA: |
347 | case 0xE: |
348 | ret = g_strdup ("computer-laptop"); |
349 | goto out; |
350 | case 0x11: |
351 | case 0x17: |
352 | case 0x1C: |
353 | case 0x1D: |
354 | ret = g_strdup ("computer-server"); |
355 | goto out; |
356 | } |
357 | } |
1cd5e6fe |
358 | #endif |
a35a69c5 |
359 | ret = g_strdup ("computer"); |
360 | out: |
361 | g_free (filebuf); |
362 | return ret; |
363 | }*/ |
1e8c7c88 |
364 | |