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