Commit | Line | Data |
---|---|---|
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> |
f8257c5d | 20 | #include <string.h> |
fd8852d9 | 21 | |
d15318db | 22 | #include <sys/param.h> |
f1ad9351 | 23 | #include <sys/sysctl.h> |
f8257c5d | 24 | #include <sys/sensors.h> |
25 | #include <sys/ioctl.h> | |
bd24ff31 | 26 | #include <sys/utsname.h> |
f8257c5d | 27 | |
28 | #include <machine/apmvar.h> | |
d15318db | 29 | |
4c04b514 | 30 | #include <glib/gprintf.h> |
8caf1f61 | 31 | #include <glib-unix.h> |
904d744d | 32 | #include <polkit/polkit.h> |
4c04b514 | 33 | |
1e8c7c88 | 34 | #include "hostnamed-gen.h" |
0f339959 | 35 | #include "hostnamed.h" |
a35a69c5 | 36 | |
483e90b7 | 37 | #include "../../polkit-auth.h" |
38 | ||
a1bcc33c | 39 | /* format: { |
40 | * (1) string to be matched against runtime machine's sysctl output. | |
41 | * can be either the exact string or a substring contained | |
42 | * within sysctl strings. no "guesses" here, a match should | |
43 | * reliably indicate the chassis/icon. | |
44 | * | |
45 | * (2) string describing chassis type divulged by (1). | |
46 | * must be one of "desktop", "laptop", "server", | |
47 | * "tablet", "handset", "vm", "container" or NULL | |
48 | * if only icon string can be ascertained. "vm" refers | |
49 | * to operating systems running on baremetal hypervisors | |
50 | * (hardware virtualization, like XEN) while "container" | |
51 | * refers to OSs running on shared hypervisors like | |
52 | * virtualbox or VMware. consider the distinction carefully | |
53 | * as common virtualization software like KVM may share | |
54 | * characteristics of both "vm" and "container" types. | |
55 | * | |
56 | * (3) string specifying icon to use. follows XDG icon spec. | |
57 | * see http://standards.freedesktop.org/icon-naming-spec/icon-naming-spec-latest.html | |
58 | * for allowed strings. | |
59 | * | |
60 | * (4) chassis precedence bit. TRUE if (2) is defined and | |
61 | * we're certain it is the proper string. FALSE in the | |
62 | * circumstance (2) may be the correct string, unless | |
63 | * a match with this bit set to TRUE overrides it. | |
64 | * if (2) is NULL, this bit is inconsequential. | |
65 | * | |
66 | * (5) icon precedence bit. see previous definition. | |
67 | * } */ | |
68 | struct SYSCTL_LOOKUP_TABLE { | |
69 | gchar *match_string; | |
70 | gchar *chassis; | |
71 | gchar *icon; | |
72 | gboolean chassis_precedence; | |
73 | gboolean icon_precedence; | |
74 | }; | |
75 | ||
fd8852d9 | 76 | GPtrArray *hostnamed_freeable; |
1be94ede | 77 | Hostname1 *hostnamed_interf; |
3d53b501 | 78 | |
0f339959 | 79 | GMainLoop *hostnamed_loop; |
80 | ||
81 | guint bus_descriptor; | |
82 | gboolean dbus_interface_exported; /* reliable because of gdbus operational guarantees */ | |
83 | ||
19c6b83d | 84 | gchar *HOSTNAME, *STATIC_HOSTNAME, *PRETTY_HOSTNAME; |
a1bcc33c | 85 | gchar *CHASSIS, *ICON; |
19c6b83d | 86 | gchar *KERN_NAME, *KERN_RELEASE, *KERN_VERS, *OS_CPENAME; |
a1bcc33c | 87 | |
88 | /* TODO no specific vm or laptop icon in gnome | |
89 | * NOTE paravirtualization on xen is only available for linuxes right now | |
90 | * dmesg on linux systems reveals xen and virtualization method (HVM or PVM) | |
91 | * but we will worry about those later */ | |
c7028b11 | 92 | |
93 | /* add any sysctl strings that suggest virtualization here */ | |
a1bcc33c | 94 | const struct SYSCTL_LOOKUP_TABLE chassis_indicator_table[] = |
95 | { | |
87df323f | 96 | { "QEMU Virtual CPU", "vm", NULL, FALSE, FALSE }, /* could be QEMU running in userspace or as part of KVM */ |
6edc347a | 97 | { "KVM", "vm", "drive-multidisk", FALSE, FALSE }, |
b21074ae | 98 | { "SmartDC HVM", "vm", "drive-multidisk", TRUE, TRUE }, /* illumos-joyent kvm */ |
87df323f | 99 | { "VirtualBox", "vm", "drive-multidisk", TRUE, TRUE }, |
100 | { "VMware, Inc.", "vm", "drive-multidisk", TRUE, TRUE }, | |
101 | { "VMware Virtual Platform", "vm", "drive-multidisk", TRUE, TRUE }, | |
102 | { "Parallels", "vm", "drive-multidisk", TRUE, TRUE }, /* need verification */ | |
c7028b11 | 103 | { "Xen", "vm", "drive-multidisk", FALSE, FALSE } |
87df323f | 104 | }; /* TODO: chroots, etc. are the actual "containers", add them */ |
76b67a18 | 105 | |
f8257c5d | 106 | /* archs to check against when determining if machine is server */ |
107 | const gchar *server_archs[] = { | |
108 | "hppa", | |
109 | "sparc", | |
110 | "sparc64" | |
111 | }; | |
f1ad9351 | 112 | |
a1bcc33c | 113 | /* --- begin method/property/dbus signal code --- */ |
f1ad9351 | 114 | |
2bc9066a | 115 | /* TODO free some strings here */ |
3d53b501 | 116 | static gboolean |
1be94ede | 117 | on_handle_set_hostname(Hostname1 *hn1_passed_interf, |
3d53b501 | 118 | GDBusMethodInvocation *invoc, |
119 | const gchar *greet, | |
120 | gpointer data) { | |
5b70f403 | 121 | GVariant *params; |
122 | gchar *proposed_hostname, *valid_hostname_buf; | |
2bc9066a | 123 | const gchar *bus_name; |
124 | gboolean policykit_auth, ret, try_to_set; | |
3ccecdd6 | 125 | size_t check_length; |
2bc9066a | 126 | check_auth_result is_authed; |
5b70f403 | 127 | |
5b70f403 | 128 | proposed_hostname = NULL; |
2bc9066a | 129 | ret = try_to_set = FALSE; |
5b70f403 | 130 | |
131 | params = g_dbus_method_invocation_get_parameters(invoc); | |
132 | g_variant_get(params, "(sb)", &proposed_hostname, &policykit_auth); | |
2bc9066a | 133 | bus_name = g_dbus_method_invocation_get_sender(invoc); |
5b70f403 | 134 | |
2bc9066a | 135 | /* verify caller has correct permissions via polkit */ |
5fd84921 | 136 | is_authed = polkit_try_auth(bus_name, "org.freedesktop.hostname1.set-hostname", policykit_auth); |
5b70f403 | 137 | |
2bc9066a | 138 | switch(is_authed) { |
139 | ||
140 | case AUTHORIZED_NATIVELY: | |
141 | case AUTHORIZED_BY_PROMPT: | |
142 | try_to_set = TRUE; | |
143 | break; | |
144 | ||
145 | case UNAUTHORIZED_NATIVELY: | |
146 | case UNAUTHORIZED_FAILED_PROMPT: | |
147 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.hostname1.Error.EACCES", "Insufficient permissions to set hostname."); | |
148 | break; | |
149 | ||
150 | case ERROR_BADBUS: | |
151 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.hostname1.Error.EFAULT", "Provided bus name is invalid."); | |
152 | break; | |
153 | ||
154 | case ERROR_BADACTION: | |
155 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.hostname1.Error.EFAULT", "Provided action ID is invalid."); | |
156 | break; | |
157 | ||
158 | case ERROR_GENERIC: | |
159 | default: | |
160 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.hostname1.Error.ECANCELED", "Failed to set hostname for unknown reason."); | |
161 | break; | |
162 | } | |
163 | ||
164 | /* verify passed hostname's validity */ | |
165 | if(try_to_set && proposed_hostname && (valid_hostname_buf = g_hostname_to_ascii(proposed_hostname))) { | |
166 | ||
167 | check_length = strnlen(valid_hostname_buf, MAXHOSTNAMELEN + 1); | |
168 | ||
169 | if(check_length > MAXHOSTNAMELEN) { | |
5b70f403 | 170 | |
3ccecdd6 | 171 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.hostname1.Error.ENAMETOOLONG", "Hostname string exceeded maximum length."); |
2bc9066a | 172 | g_free(valid_hostname_buf); |
173 | ||
174 | } else if(sethostname(proposed_hostname, check_length)) { | |
175 | ||
176 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.hostname1.Error.ECANCELED", "Failed to set hostname for unknown reason."); | |
177 | g_free(valid_hostname_buf); | |
5b70f403 | 178 | |
2bc9066a | 179 | } else { |
5b70f403 | 180 | |
2bc9066a | 181 | HOSTNAME = valid_hostname_buf; |
3ccecdd6 | 182 | hostname1_set_hostname(hn1_passed_interf, HOSTNAME); |
2bc9066a | 183 | g_ptr_array_add(hostnamed_freeable, valid_hostname_buf); |
3ccecdd6 | 184 | ret = TRUE; |
185 | hostname1_complete_set_hostname(hn1_passed_interf, invoc); | |
186 | } | |
187 | } | |
5b70f403 | 188 | |
189 | return ret; | |
3d53b501 | 190 | } |
191 | ||
192 | static gboolean | |
1be94ede | 193 | on_handle_set_static_hostname(Hostname1 *hn1_passed_interf, |
3d53b501 | 194 | GDBusMethodInvocation *invoc, |
195 | const gchar *greet, | |
196 | gpointer data) { | |
933f62bb | 197 | |
198 | GVariant *params; | |
199 | gchar *proposed_static_hostname, *valid_static_hostname_buf; | |
200 | const gchar *bus_name; | |
201 | gboolean policykit_auth, ret, try_to_set; | |
202 | size_t check_length; | |
203 | check_auth_result is_authed; | |
204 | ||
205 | proposed_static_hostname = NULL; | |
206 | ret = try_to_set = FALSE; | |
207 | ||
208 | params = g_dbus_method_invocation_get_parameters(invoc); | |
209 | g_variant_get(params, "(sb)", &proposed_static_hostname, &policykit_auth); | |
210 | bus_name = g_dbus_method_invocation_get_sender(invoc); | |
211 | ||
212 | /* verify caller has correct permissions via polkit */ | |
5fd84921 | 213 | is_authed = polkit_try_auth(bus_name, "org.freedesktop.hostname1.set-static-hostname", policykit_auth); |
933f62bb | 214 | |
215 | switch(is_authed) { | |
216 | ||
217 | case AUTHORIZED_NATIVELY: | |
218 | case AUTHORIZED_BY_PROMPT: | |
219 | try_to_set = TRUE; | |
220 | break; | |
221 | ||
222 | case UNAUTHORIZED_NATIVELY: | |
223 | case UNAUTHORIZED_FAILED_PROMPT: | |
224 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.hostname1.Error.EACCES", "Insufficient permissions to set static hostname."); | |
225 | break; | |
226 | ||
227 | case ERROR_BADBUS: | |
228 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.hostname1.Error.EFAULT", "Provided bus name is invalid."); | |
229 | break; | |
230 | ||
231 | case ERROR_BADACTION: | |
232 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.hostname1.Error.EFAULT", "Provided action ID is invalid."); | |
233 | break; | |
234 | ||
235 | case ERROR_GENERIC: | |
236 | default: | |
237 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.hostname1.Error.ECANCELED", "Failed to set static hostname for unknown reason."); | |
238 | break; | |
239 | } | |
240 | ||
241 | /* verify passed hostname's validity */ | |
242 | if(try_to_set && proposed_static_hostname && (valid_static_hostname_buf = g_hostname_to_ascii(proposed_static_hostname))) { | |
243 | ||
244 | check_length = strnlen(valid_static_hostname_buf, MAXHOSTNAMELEN + 1); | |
245 | ||
246 | if(check_length > MAXHOSTNAMELEN) { | |
247 | ||
248 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.hostname1.Error.ENAMETOOLONG", "Static hostname string exceeded maximum length."); | |
249 | g_free(valid_static_hostname_buf); | |
250 | ||
251 | } else if(!(STATIC_HOSTNAME = valid_static_hostname_buf)) { | |
252 | ||
253 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.hostname1.Error.ECANCELED", "Failed to set static hostname for unknown reason."); | |
254 | g_free(valid_static_hostname_buf); | |
255 | ||
256 | } else { | |
257 | ||
258 | g_strdelimit(STATIC_HOSTNAME, " ", '-'); | |
057ab9c2 | 259 | hostname1_set_static_hostname(hn1_passed_interf, STATIC_HOSTNAME); |
933f62bb | 260 | g_ptr_array_add(hostnamed_freeable, valid_static_hostname_buf); |
057ab9c2 | 261 | ret = (!sethostname(valid_static_hostname_buf, MAXHOSTNAMELEN)) ? TRUE : FALSE; /* TODO set /etc/myname, guarantee domain or substitue .home.network" */ |
933f62bb | 262 | hostname1_complete_set_static_hostname(hn1_passed_interf, invoc); |
263 | } | |
264 | } | |
265 | ||
266 | return ret; | |
3d53b501 | 267 | } |
268 | ||
269 | static gboolean | |
1be94ede | 270 | on_handle_set_pretty_hostname(Hostname1 *hn1_passed_interf, |
3d53b501 | 271 | GDBusMethodInvocation *invoc, |
272 | const gchar *greet, | |
273 | gpointer data) { | |
933f62bb | 274 | |
275 | GVariant *params; | |
276 | gchar *proposed_pretty_hostname, *valid_pretty_hostname_buf, *computed_static_hostname; | |
277 | const gchar *bus_name; | |
278 | gboolean policykit_auth, ret, try_to_set; | |
279 | size_t check_length; | |
280 | check_auth_result is_authed; | |
281 | GKeyFile *config; | |
282 | ||
283 | config = g_key_file_new(); | |
284 | proposed_pretty_hostname = NULL; | |
285 | ret = try_to_set = FALSE; | |
286 | ||
287 | params = g_dbus_method_invocation_get_parameters(invoc); | |
288 | g_variant_get(params, "(sb)", &proposed_pretty_hostname, &policykit_auth); | |
289 | bus_name = g_dbus_method_invocation_get_sender(invoc); | |
290 | ||
291 | /* verify caller has correct permissions via polkit */ | |
5fd84921 | 292 | is_authed = polkit_try_auth(bus_name, "org.freedesktop.hostname1.set-pretty-hostname", policykit_auth); |
933f62bb | 293 | |
294 | switch(is_authed) { | |
295 | ||
296 | case AUTHORIZED_NATIVELY: | |
297 | case AUTHORIZED_BY_PROMPT: | |
298 | try_to_set = TRUE; | |
299 | break; | |
300 | ||
301 | case UNAUTHORIZED_NATIVELY: | |
302 | case UNAUTHORIZED_FAILED_PROMPT: | |
303 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.hostname1.Error.EACCES", "Insufficient permissions to set pretty hostname."); | |
304 | break; | |
305 | ||
306 | case ERROR_BADBUS: | |
307 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.hostname1.Error.EFAULT", "Provided bus name is invalid."); | |
308 | break; | |
309 | ||
310 | case ERROR_BADACTION: | |
311 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.hostname1.Error.EFAULT", "Provided action ID is invalid."); | |
312 | break; | |
313 | ||
314 | case ERROR_GENERIC: | |
315 | default: | |
316 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.hostname1.Error.ECANCELED", "Failed to set pretty hostname for unknown reason."); | |
317 | break; | |
318 | } | |
319 | ||
320 | /* verify passed hostname's validity */ | |
321 | if(try_to_set && proposed_pretty_hostname && (valid_pretty_hostname_buf = g_locale_to_utf8(proposed_pretty_hostname, -1, 0, 0, NULL))) { | |
322 | ||
323 | check_length = strnlen(valid_pretty_hostname_buf, MAXHOSTNAMELEN + 1); | |
324 | ||
325 | if(check_length > MAXHOSTNAMELEN) { | |
326 | ||
327 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.hostname1.Error.ENAMETOOLONG", "Static hostname string exceeded maximum length."); | |
328 | g_free(valid_pretty_hostname_buf); | |
329 | ||
330 | } else if(!(PRETTY_HOSTNAME = valid_pretty_hostname_buf)) { | |
331 | ||
332 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.hostname1.Error.ECANCELED", "Failed to set pretty hostname for unknown reason."); | |
333 | g_free(valid_pretty_hostname_buf); | |
334 | ||
335 | } else { | |
336 | ||
337 | hostname1_set_pretty_hostname(hn1_passed_interf, PRETTY_HOSTNAME); | |
338 | g_ptr_array_add(hostnamed_freeable, valid_pretty_hostname_buf); | |
339 | hostname1_complete_set_pretty_hostname(hn1_passed_interf, invoc); | |
340 | ret = TRUE; | |
341 | ||
3d6bffb8 | 342 | if(g_key_file_load_from_file(config, "/etc/machine-info", G_KEY_FILE_NONE, NULL)) { |
933f62bb | 343 | |
3d6bffb8 | 344 | g_key_file_set_string(config, "hostnamed", "PRETTY_HOSTNAME", valid_pretty_hostname_buf); |
933f62bb | 345 | |
45e09604 | 346 | /* if((computed_static_hostname = g_hostname_to_ascii(PRETTY_HOSTNAME))) { |
933f62bb | 347 | |
348 | g_strdelimit(computed_static_hostname, " ", '-'); | |
349 | hostname1_set_static_hostname(hn1_passed_interf, computed_static_hostname); | |
350 | STATIC_HOSTNAME = computed_static_hostname; | |
351 | g_ptr_array_add(hostnamed_freeable, computed_static_hostname); | |
352 | g_key_file_set_string(config, "hostnamed", "StaticHostname", computed_static_hostname); | |
353 | ||
45e09604 | 354 | } */ |
933f62bb | 355 | } |
933f62bb | 356 | } |
357 | } | |
358 | ||
3d6bffb8 | 359 | g_key_file_save_to_file(config, "/etc/machine-info", NULL); |
933f62bb | 360 | g_key_file_unref(config); |
361 | ||
362 | return ret; | |
3d53b501 | 363 | } |
364 | ||
365 | static gboolean | |
1be94ede | 366 | on_handle_set_chassis(Hostname1 *hn1_passed_interf, |
3d53b501 | 367 | GDBusMethodInvocation *invoc, |
368 | const gchar *greet, | |
369 | gpointer data) { | |
933f62bb | 370 | |
371 | GVariant *params; | |
372 | gchar *proposed_chassis_name, *valid_chassis_name_buf; | |
373 | const gchar *bus_name; | |
374 | gboolean policykit_auth, ret, try_to_set; | |
375 | check_auth_result is_authed; | |
376 | GKeyFile *config; | |
377 | ||
378 | config = g_key_file_new(); | |
379 | proposed_chassis_name = NULL; | |
380 | ret = try_to_set = FALSE; | |
381 | valid_chassis_name_buf = (gchar *)g_malloc0(8192); | |
382 | ||
383 | params = g_dbus_method_invocation_get_parameters(invoc); | |
384 | g_variant_get(params, "(sb)", &proposed_chassis_name, &policykit_auth); | |
385 | bus_name = g_dbus_method_invocation_get_sender(invoc); | |
386 | ||
387 | g_strlcpy(valid_chassis_name_buf, proposed_chassis_name, (gsize)64); | |
388 | ||
389 | /* verify caller has correct permissions via polkit */ | |
5fd84921 | 390 | is_authed = polkit_try_auth(bus_name, "org.freedesktop.hostname1.set-chassis", policykit_auth); |
933f62bb | 391 | |
392 | switch(is_authed) { | |
393 | ||
394 | case AUTHORIZED_NATIVELY: | |
395 | case AUTHORIZED_BY_PROMPT: | |
396 | try_to_set = TRUE; | |
397 | break; | |
398 | ||
399 | case UNAUTHORIZED_NATIVELY: | |
400 | case UNAUTHORIZED_FAILED_PROMPT: | |
401 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.hostname1.Error.EACCES", "Insufficient permissions to set chassis type."); | |
402 | break; | |
403 | ||
404 | case ERROR_BADBUS: | |
405 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.hostname1.Error.EFAULT", "Provided bus name is invalid."); | |
406 | break; | |
407 | ||
408 | case ERROR_BADACTION: | |
409 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.hostname1.Error.EFAULT", "Provided action ID is invalid."); | |
410 | break; | |
411 | ||
412 | case ERROR_GENERIC: | |
413 | default: | |
414 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.hostname1.Error.ECANCELED", "Failed to set chassis type for unknown reason."); | |
415 | break; | |
416 | } | |
417 | ||
418 | /* verify passed chassis type's validity */ | |
419 | if(try_to_set && proposed_chassis_name) { | |
420 | ||
421 | if(!is_valid_chassis_type(proposed_chassis_name)) { | |
422 | ||
423 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.hostname1.Error.ECANCELED", "Chassis type must be 'desktop', 'laptop', 'server', 'tablet', 'handset', 'vm', or 'container'."); | |
424 | g_free(valid_chassis_name_buf); | |
425 | ||
426 | } else if(!(CHASSIS = valid_chassis_name_buf)) { | |
427 | ||
428 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.hostname1.Error.ECANCELED", "Failed to set chassis type for unknown reason."); | |
429 | g_free(valid_chassis_name_buf); | |
430 | ||
431 | } else { | |
432 | ||
433 | hostname1_set_chassis(hn1_passed_interf, CHASSIS); | |
434 | g_ptr_array_add(hostnamed_freeable, valid_chassis_name_buf); | |
435 | hostname1_complete_set_chassis(hn1_passed_interf, invoc); | |
436 | ||
3d6bffb8 | 437 | if(g_key_file_load_from_file(config, "/etc/machine-info", G_KEY_FILE_NONE, NULL)) { |
933f62bb | 438 | |
439 | ret = TRUE; | |
440 | g_key_file_set_string(config, "hostnamed", "ChassisType", valid_chassis_name_buf); | |
441 | ||
442 | } | |
443 | } | |
444 | } | |
445 | ||
3d6bffb8 | 446 | g_key_file_save_to_file(config, "/etc/machine-info", NULL); |
933f62bb | 447 | g_key_file_unref(config); |
448 | ||
449 | return ret; | |
3d53b501 | 450 | } |
451 | ||
452 | static gboolean | |
1be94ede | 453 | on_handle_set_icon_name(Hostname1 *hn1_passed_interf, |
3d53b501 | 454 | GDBusMethodInvocation *invoc, |
455 | const gchar *greet, | |
456 | gpointer data) { | |
933f62bb | 457 | |
458 | GVariant *params; | |
459 | gchar *proposed_icon_name, *valid_icon_name_buf; | |
460 | const gchar *bus_name; | |
461 | gboolean policykit_auth, ret, try_to_set; | |
462 | check_auth_result is_authed; | |
463 | GKeyFile *config; | |
464 | ||
465 | config = g_key_file_new(); | |
466 | proposed_icon_name = NULL; | |
467 | ret = try_to_set = FALSE; | |
468 | ||
469 | params = g_dbus_method_invocation_get_parameters(invoc); | |
470 | g_variant_get(params, "(sb)", &proposed_icon_name, &policykit_auth); | |
471 | bus_name = g_dbus_method_invocation_get_sender(invoc); | |
472 | ||
473 | /* verify caller has correct permissions via polkit */ | |
5fd84921 | 474 | is_authed = polkit_try_auth(bus_name, "org.freedesktop.hostname1.set-icon-name", policykit_auth); |
933f62bb | 475 | |
476 | switch(is_authed) { | |
477 | ||
478 | case AUTHORIZED_NATIVELY: | |
479 | case AUTHORIZED_BY_PROMPT: | |
480 | try_to_set = TRUE; | |
481 | break; | |
482 | ||
483 | case UNAUTHORIZED_NATIVELY: | |
484 | case UNAUTHORIZED_FAILED_PROMPT: | |
485 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.hostname1.Error.EACCES", "Insufficient permissions to set icon name."); | |
486 | break; | |
487 | ||
488 | case ERROR_BADBUS: | |
489 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.hostname1.Error.EFAULT", "Provided bus name is invalid."); | |
490 | break; | |
491 | ||
492 | case ERROR_BADACTION: | |
493 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.hostname1.Error.EFAULT", "Provided action ID is invalid."); | |
494 | break; | |
495 | ||
496 | case ERROR_GENERIC: | |
497 | default: | |
498 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.hostname1.Error.ECANCELED", "Failed to set icon name for unknown reason."); | |
499 | break; | |
500 | } | |
501 | ||
502 | /* verify passed chassis type's validity */ | |
503 | if(try_to_set && proposed_icon_name) { | |
504 | ||
505 | g_strlcpy(valid_icon_name_buf, proposed_icon_name, (gsize)64); | |
506 | ||
507 | if(!(ICON = valid_icon_name_buf)) { | |
508 | ||
509 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.hostname1.Error.ECANCELED", "Failed to set icon name for unknown reason."); | |
510 | g_free(valid_icon_name_buf); | |
511 | ||
512 | } else { | |
513 | ||
514 | hostname1_set_icon_name(hn1_passed_interf, ICON); | |
515 | g_ptr_array_add(hostnamed_freeable, valid_icon_name_buf); | |
516 | hostname1_complete_set_icon_name(hn1_passed_interf, invoc); | |
517 | ||
3d6bffb8 | 518 | if(g_key_file_load_from_file(config, "/etc/machine-info", G_KEY_FILE_NONE, NULL)) { |
933f62bb | 519 | |
520 | ret = TRUE; | |
521 | g_key_file_set_string(config, "hostnamed", "IconName", valid_icon_name_buf); | |
522 | ||
523 | } | |
524 | } | |
525 | } | |
526 | ||
3d6bffb8 | 527 | g_key_file_save_to_file(config, "/etc/machine-info", NULL); |
933f62bb | 528 | g_key_file_unref(config); |
529 | ||
530 | return ret; | |
3d53b501 | 531 | } |
532 | ||
76b67a18 | 533 | /* note: all hostnamed/hostname1's properties are read-only, |
534 | * and do not need set_ functions, gdbus-codegen realized | |
535 | * this from the XML and handled the to-be error of trying | |
536 | * to set a read-only property's value | |
537 | */ | |
538 | ||
539 | const gchar * | |
540 | our_get_hostname() { | |
541 | ||
3ccecdd6 | 542 | gchar *hostname_buf; |
543 | hostname_buf = (gchar *)g_malloc0(MAXHOSTNAMELEN); | |
544 | ||
545 | if(gethostname(hostname_buf, MAXHOSTNAMELEN)) | |
546 | return "localhost.home.network"; /* TODO bomb out here probably */ | |
547 | ||
548 | else if(!g_strcmp0(HOSTNAME, hostname_buf)) { | |
549 | ||
550 | g_free(hostname_buf); | |
19c6b83d | 551 | return HOSTNAME; |
3ccecdd6 | 552 | } |
553 | ||
554 | g_ptr_array_add(hostnamed_freeable, hostname_buf); | |
555 | HOSTNAME = hostname_buf; | |
556 | hostname1_set_hostname(hostnamed_interf, HOSTNAME); | |
19c6b83d | 557 | |
3ccecdd6 | 558 | return HOSTNAME; |
76b67a18 | 559 | } |
560 | ||
561 | const gchar * | |
562 | our_get_static_hostname() { | |
563 | ||
3ccecdd6 | 564 | if(STATIC_HOSTNAME && g_strcmp0(STATIC_HOSTNAME, "")) |
19c6b83d | 565 | return STATIC_HOSTNAME; |
566 | else if(HOSTNAME) | |
567 | return HOSTNAME; | |
568 | ||
3ccecdd6 | 569 | return "localhost.home.network"; |
76b67a18 | 570 | } |
571 | ||
572 | const gchar * | |
573 | our_get_pretty_hostname() { | |
574 | ||
19c6b83d | 575 | if(PRETTY_HOSTNAME) |
576 | return PRETTY_HOSTNAME; | |
577 | ||
a2fffc07 | 578 | return ""; |
76b67a18 | 579 | } |
580 | ||
581 | const gchar * | |
582 | our_get_chassis() { | |
583 | ||
f8257c5d | 584 | if(CHASSIS) |
585 | return CHASSIS; | |
f1ad9351 | 586 | |
19c6b83d | 587 | return "desktop"; /* this leads to the most generic beheivor in the unlikely case its returned */ |
76b67a18 | 588 | } |
589 | ||
590 | const gchar * | |
591 | our_get_icon_name() { | |
592 | ||
f8257c5d | 593 | if(ICON) |
594 | return ICON; | |
595 | ||
596 | return ""; | |
76b67a18 | 597 | } |
598 | ||
599 | const gchar * | |
600 | our_get_kernel_name() { | |
601 | ||
bd24ff31 | 602 | if(KERN_NAME) |
603 | return KERN_NAME; | |
604 | ||
605 | return ""; | |
76b67a18 | 606 | } |
607 | ||
608 | const gchar * | |
609 | our_get_kernel_version() { | |
610 | ||
bd24ff31 | 611 | if(KERN_VERS) |
612 | return KERN_VERS; | |
613 | ||
614 | return ""; | |
76b67a18 | 615 | } |
616 | ||
617 | const gchar * | |
618 | our_get_kernel_release() { | |
619 | ||
bd24ff31 | 620 | if(KERN_RELEASE) |
621 | return KERN_RELEASE; | |
622 | ||
623 | return ""; | |
76b67a18 | 624 | } |
625 | ||
626 | const gchar * | |
627 | our_get_os_cpename() { | |
628 | ||
3808ecc5 AJ |
629 | /* XXX needs to parse /etc/os-release (fallback to /usr/local/lib/os-release) */ |
630 | return ""; | |
76b67a18 | 631 | } |
632 | ||
633 | const gchar * | |
634 | our_get_os_pretty_name() { | |
635 | ||
bd24ff31 | 636 | return "OpenBSD"; |
76b67a18 | 637 | } |
638 | ||
1ce41045 | 639 | /* --- end method/property/dbus signal code, begin bus/name handlers --- */ |
0df0018d | 640 | |
5b005882 | 641 | static void hostnamed_on_bus_acquired(GDBusConnection *conn, |
9cab3afe | 642 | const gchar *name, |
643 | gpointer user_data) { | |
d1e1db9e | 644 | |
0f339959 | 645 | g_printf("got bus/name, exporting %s's interface...\n", name); |
d1e1db9e | 646 | |
90f54407 | 647 | hostnamed_interf = hostname1_skeleton_new(); |
3d53b501 | 648 | |
76b67a18 | 649 | /* attach function pointers to generated struct's method handlers */ |
3d53b501 | 650 | g_signal_connect(hostnamed_interf, "handle-set-hostname", G_CALLBACK(on_handle_set_hostname), NULL); |
651 | g_signal_connect(hostnamed_interf, "handle-set-static-hostname", G_CALLBACK(on_handle_set_static_hostname), NULL); | |
652 | g_signal_connect(hostnamed_interf, "handle-set-pretty-hostname", G_CALLBACK(on_handle_set_pretty_hostname), NULL); | |
653 | g_signal_connect(hostnamed_interf, "handle-set-chassis", G_CALLBACK(on_handle_set_chassis), NULL); | |
654 | g_signal_connect(hostnamed_interf, "handle-set-icon-name", G_CALLBACK(on_handle_set_icon_name), NULL); | |
655 | ||
76b67a18 | 656 | /* set our properties before export */ |
1be94ede | 657 | hostname1_set_hostname(hostnamed_interf, our_get_hostname()); |
658 | hostname1_set_static_hostname(hostnamed_interf, our_get_static_hostname()); | |
659 | hostname1_set_pretty_hostname(hostnamed_interf, our_get_pretty_hostname()); | |
660 | hostname1_set_chassis(hostnamed_interf, our_get_chassis()); | |
661 | hostname1_set_icon_name(hostnamed_interf, our_get_icon_name()); | |
662 | hostname1_set_kernel_name(hostnamed_interf, our_get_kernel_name()); | |
663 | hostname1_set_kernel_version(hostnamed_interf, our_get_kernel_version()); | |
664 | hostname1_set_kernel_release(hostnamed_interf, our_get_kernel_release()); | |
665 | hostname1_set_operating_system_cpename(hostnamed_interf, our_get_os_cpename()); | |
666 | hostname1_set_operating_system_pretty_name(hostnamed_interf, our_get_os_pretty_name()); | |
76b67a18 | 667 | |
3d53b501 | 668 | if(!g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(hostnamed_interf), |
1be94ede | 669 | conn, |
670 | "/org/freedesktop/hostname1", | |
671 | NULL)) { | |
3d53b501 | 672 | |
0f339959 | 673 | g_printf("failed to export %s's interface!\n", name); /* unusual edge case, TODO check errno */ |
90f54407 | 674 | hostnamed_mem_clean(); |
3d53b501 | 675 | |
0f339959 | 676 | } else { |
ea207ed3 | 677 | |
90f54407 | 678 | dbus_interface_exported = TRUE; |
679 | g_printf("exported %s's interface on the system bus...\n", name); | |
680 | } | |
0f339959 | 681 | } |
1e8c7c88 | 682 | |
0f339959 | 683 | static void hostnamed_on_name_acquired(GDBusConnection *conn, |
90f54407 | 684 | const gchar *name, |
0f339959 | 685 | gpointer user_data) { |
686 | ||
687 | g_printf("success!\n"); | |
1e8c7c88 | 688 | } |
689 | ||
5b005882 | 690 | static void hostnamed_on_name_lost(GDBusConnection *conn, |
1be94ede | 691 | const gchar *name, |
692 | gpointer user_data) { | |
80043b36 | 693 | |
90f54407 | 694 | if(!conn) { |
0f339959 | 695 | |
90f54407 | 696 | 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); |
697 | hostnamed_mem_clean(); | |
698 | } | |
0f339959 | 699 | |
509599f0 | 700 | g_printf("lost name %s, exiting...\n", name); |
fd8852d9 | 701 | |
1cd5e6fe | 702 | hostnamed_mem_clean(); |
0f339959 | 703 | } |
704 | ||
705 | /* --- end bus/name handlers, begin misc unix functions --- */ | |
706 | ||
707 | /* safe call to clean and then exit | |
509599f0 | 708 | * this stops our GMainLoop safely before letting main() return */ |
0f339959 | 709 | void hostnamed_mem_clean() { |
710 | ||
90f54407 | 711 | g_printf("exiting...\n"); |
0f339959 | 712 | |
90f54407 | 713 | if(dbus_interface_exported) |
714 | g_dbus_interface_skeleton_unexport(G_DBUS_INTERFACE_SKELETON(hostnamed_interf)); | |
0f339959 | 715 | |
90f54407 | 716 | if(g_main_loop_is_running(hostnamed_loop)) |
717 | g_main_loop_quit(hostnamed_loop); | |
fd8852d9 | 718 | |
ea207ed3 | 719 | } |
720 | ||
0f339959 | 721 | /* wrapper for glib's unix signal handling; called only once if terminatating signal is raised against us */ |
722 | gboolean unix_sig_terminate_handler(gpointer data) { | |
c62bceb7 | 723 | |
90f54407 | 724 | g_printf("caught SIGINT/HUP/TERM, exiting\n"); |
0f339959 | 725 | |
90f54407 | 726 | hostnamed_mem_clean(); |
727 | return G_SOURCE_REMOVE; | |
0f339959 | 728 | } |
729 | ||
730 | void set_signal_handlers() { | |
731 | ||
90f54407 | 732 | /* we don't care about its descriptor, we never need to unregister these */ |
733 | g_unix_signal_add(SIGINT, unix_sig_terminate_handler, NULL); | |
734 | g_unix_signal_add(SIGHUP, unix_sig_terminate_handler, NULL); | |
735 | g_unix_signal_add(SIGTERM, unix_sig_terminate_handler, NULL); | |
04cc16f2 | 736 | |
90f54407 | 737 | /* TODO: the "only once" guarantee only counts towards specific signals. |
738 | * make sure calling a SIGINT and SIGHUP doesn't cause term_handler() | |
739 | * to be called twice */ | |
0f339959 | 740 | } |
741 | ||
742 | int main() { | |
bd24ff31 | 743 | |
744 | hostnamed_freeable = g_ptr_array_new(); | |
745 | ||
a0ebb315 | 746 | /* TODO: check for valid, writable config at init. if no, complain to `make install` */ |
bd24ff31 | 747 | |
748 | CHASSIS = ICON = OS_CPENAME = 0; | |
749 | KERN_NAME = KERN_RELEASE = KERN_VERS = 0; | |
baf05b70 | 750 | HOSTNAME = STATIC_HOSTNAME = PRETTY_HOSTNAME = NULL; |
483e90b7 | 751 | |
90f54407 | 752 | set_signal_handlers(); |
387173cb | 753 | |
46835f3e | 754 | if(!determine_chassis_and_icon() || !set_uname_properties() || !set_names()) |
a1bcc33c | 755 | return 1; |
756 | ||
baf05b70 | 757 | hostnamed_loop = g_main_loop_new(NULL, TRUE); |
fd8852d9 | 758 | |
90f54407 | 759 | bus_descriptor = g_bus_own_name(G_BUS_TYPE_SYSTEM, |
0f339959 | 760 | "org.freedesktop.hostname1", |
761 | G_BUS_NAME_OWNER_FLAGS_NONE, | |
762 | hostnamed_on_bus_acquired, | |
763 | hostnamed_on_name_acquired, | |
764 | hostnamed_on_name_lost, | |
765 | NULL, | |
766 | NULL); | |
496f5d66 | 767 | |
90f54407 | 768 | g_main_loop_run(hostnamed_loop); |
769 | /* runs until single g_main_loop_quit() call is raised inside <interface>_mem_clean() */ | |
770 | g_main_loop_unref(hostnamed_loop); | |
1e8c7c88 | 771 | |
90f54407 | 772 | /* guaranteed unownable */ |
773 | g_bus_unown_name(bus_descriptor); | |
c62bceb7 | 774 | |
90f54407 | 775 | /* at this point no operations can occur with our data, it is safe to free it + its container */ |
776 | g_ptr_array_free(hostnamed_freeable, TRUE); | |
7323a4e4 | 777 | |
90f54407 | 778 | return 0; |
a35a69c5 | 779 | } |
780 | ||
46835f3e | 781 | gboolean set_names() { |
782 | ||
baf05b70 | 783 | /* (1) set up */ |
784 | gchar *hostname_buf, *static_hostname_buf, *pretty_hostname_buf; | |
785 | GKeyFile *config; | |
46835f3e | 786 | size_t hostname_divider; |
787 | ||
baf05b70 | 788 | hostname_buf = (gchar*) g_malloc0(MAXHOSTNAMELEN); |
789 | static_hostname_buf = (gchar*) g_malloc0(4096); | |
790 | pretty_hostname_buf = (gchar*) g_malloc0(4096); | |
46835f3e | 791 | |
baf05b70 | 792 | config = g_key_file_new(); |
46835f3e | 793 | |
baf05b70 | 794 | g_ptr_array_add(hostnamed_freeable, hostname_buf); |
795 | g_ptr_array_add(hostnamed_freeable, static_hostname_buf); | |
796 | g_ptr_array_add(hostnamed_freeable, pretty_hostname_buf); | |
46835f3e | 797 | |
baf05b70 | 798 | /* (2) set HOSTNAME */ |
799 | if(gethostname(hostname_buf, MAXHOSTNAMELEN) || !g_strcmp0(hostname_buf, "")) | |
800 | HOSTNAME = "localhost"; | |
46835f3e | 801 | |
baf05b70 | 802 | HOSTNAME = hostname_buf; |
46835f3e | 803 | |
baf05b70 | 804 | /* this bit gets you the /etc/myname style hostname |
805 | hostname_divider = strcspn(hostname_buf, "."); | |
806 | strncpy(ret, hostname_buf, hostname_divider); */ | |
46835f3e | 807 | |
baf05b70 | 808 | /* (3) set PRETTY_HOSTNAME */ |
3d6bffb8 AJ |
809 | if(g_key_file_load_from_file(config, "/etc/machine-info", G_KEY_FILE_NONE, NULL) |
810 | && (pretty_hostname_buf = g_key_file_get_value(config, "hostnamed", "PRETTY_HOSTNAME", NULL))) | |
baf05b70 | 811 | PRETTY_HOSTNAME = pretty_hostname_buf; |
812 | else | |
813 | PRETTY_HOSTNAME = ""; | |
814 | ||
45e09604 | 815 | |
816 | /* (4) set STATIC_HOSTNAME */ | |
817 | if((static_hostname_buf = g_key_file_get_value(config, "hostnamed", "STATIC_HOSTNAME", NULL))) | |
818 | STATIC_HOSTNAME = static_hostname_buf; | |
46835f3e | 819 | |
45e09604 | 820 | else |
821 | STATIC_HOSTNAME = ""; | |
46835f3e | 822 | |
45e09604 | 823 | if(config) |
824 | g_key_file_unref(config); | |
46835f3e | 825 | |
baf05b70 | 826 | return (HOSTNAME && STATIC_HOSTNAME && PRETTY_HOSTNAME) ? TRUE : FALSE; |
46835f3e | 827 | } |
828 | ||
bd24ff31 | 829 | gboolean set_uname_properties() { |
830 | ||
831 | struct utsname un; | |
832 | ||
833 | if(-1 == uname(&un)) | |
834 | return FALSE; | |
835 | ||
836 | KERN_NAME = (gchar*)g_malloc0(sizeof(un.sysname)); | |
837 | g_ptr_array_add(hostnamed_freeable, KERN_NAME); | |
838 | g_strlcpy(KERN_NAME, un.sysname, sizeof(un.sysname)); | |
839 | ||
840 | KERN_RELEASE = (gchar*)g_malloc0(sizeof(un.release)); | |
841 | g_ptr_array_add(hostnamed_freeable, KERN_RELEASE); | |
842 | g_strlcpy(KERN_RELEASE, un.release, sizeof(un.release)); | |
843 | ||
844 | KERN_VERS = (gchar*)g_malloc0(sizeof(un.version)); | |
845 | g_ptr_array_add(hostnamed_freeable, KERN_VERS); | |
846 | g_strlcpy(KERN_VERS, un.version, sizeof(un.version)); | |
847 | ||
848 | return TRUE; | |
849 | } | |
850 | ||
a1bcc33c | 851 | gboolean determine_chassis_and_icon() { |
852 | ||
b81ab32a | 853 | const size_t bufsize = 4096; |
854 | ||
f8257c5d | 855 | char *hwproduct, *hwmodel, *hwvendor, *hwmachine; |
856 | size_t hwproduct_size, hwmodel_size, hwvendor_size, hwmachine_size; | |
857 | int hwproduct_name[2], hwmodel_name[2], hwvendor_name[2], hwmachine_name[2]; | |
f1ad9351 | 858 | unsigned int i; |
f8257c5d | 859 | gboolean UNSURE_CHASSIS_FLAG, UNSURE_ICON_FLAG; |
860 | ||
b81ab32a | 861 | hwproduct_size = hwmodel_size = hwvendor_size = hwmachine_size = bufsize; |
68e777c6 | 862 | UNSURE_CHASSIS_FLAG = UNSURE_ICON_FLAG = FALSE; |
5ac7f542 | 863 | i = 0; |
f1ad9351 | 864 | |
b81ab32a | 865 | hwproduct = (char*)g_malloc0(4096); |
866 | hwmodel = (char*)g_malloc0(4096); | |
867 | hwvendor = (char*)g_malloc0(4096); | |
868 | hwmachine = (char*)g_malloc0(4096); | |
869 | ||
bd24ff31 | 870 | g_ptr_array_add(hostnamed_freeable, hwproduct); |
871 | g_ptr_array_add(hostnamed_freeable, hwmodel); | |
872 | g_ptr_array_add(hostnamed_freeable, hwvendor); | |
873 | g_ptr_array_add(hostnamed_freeable, hwmachine); | |
874 | ||
a1bcc33c | 875 | hwproduct_name[0] = CTL_HW; |
876 | hwproduct_name[1] = HW_PRODUCT; | |
f1ad9351 | 877 | |
a1bcc33c | 878 | hwmodel_name[0] = CTL_HW; |
879 | hwmodel_name[1] = HW_MODEL; | |
880 | ||
881 | hwvendor_name[0] = CTL_HW; | |
882 | hwvendor_name[1] = HW_VENDOR; | |
883 | ||
f8257c5d | 884 | hwmachine_name[0] = CTL_HW; |
885 | hwmachine_name[1] = HW_MACHINE; | |
886 | ||
a1bcc33c | 887 | /* pass NULL buffer to check size first, then pass hw to be filled according to freshly-set hw_size */ |
888 | if(-1 == sysctl(hwproduct_name, 2, NULL, &hwproduct_size, NULL, 0) || -1 == sysctl(hwproduct_name, 2, hwproduct, &hwproduct_size, NULL, 0)) | |
889 | return FALSE; | |
890 | ||
891 | if(-1 == sysctl(hwmodel_name, 2, NULL, &hwmodel_size, NULL, 0) || -1 == sysctl(hwmodel_name, 2, hwmodel, &hwmodel_size, NULL, 0)) | |
892 | return FALSE; | |
893 | ||
894 | if(-1 == sysctl(hwvendor_name, 2, NULL, &hwvendor_size, NULL, 0) || -1 == sysctl(hwvendor_name, 2, hwvendor, &hwvendor_size, NULL, 0)) | |
895 | return FALSE; | |
896 | ||
f8257c5d | 897 | if(-1 == sysctl(hwmachine_name, 2, NULL, &hwmachine_size, NULL, 0) || -1 == sysctl(hwmachine_name, 2, hwmachine, &hwmachine_size, NULL, 0)) |
898 | return FALSE; | |
899 | ||
a1bcc33c | 900 | /* TODO: test for laptop, if not, dmidecode for desktop vs. server |
901 | * probably move this code to vm test func and set a global after running it early, once */ | |
902 | ||
f8257c5d | 903 | for(; i < G_N_ELEMENTS(chassis_indicator_table); i++) { |
904 | if(strcasestr(hwproduct, chassis_indicator_table[i].match_string) | |
905 | || strcasestr(hwmodel, chassis_indicator_table[i].match_string) | |
906 | || strcasestr(hwvendor, chassis_indicator_table[i].match_string)) { | |
907 | ||
908 | if(!UNSURE_CHASSIS_FLAG && chassis_indicator_table[i].chassis) { | |
909 | ||
910 | UNSURE_CHASSIS_FLAG = chassis_indicator_table[i].chassis_precedence; | |
911 | CHASSIS = chassis_indicator_table[i].chassis; | |
912 | } | |
913 | ||
914 | if(!UNSURE_ICON_FLAG && chassis_indicator_table[i].icon) { | |
915 | ||
916 | UNSURE_ICON_FLAG = chassis_indicator_table[i].icon_precedence; | |
917 | ICON = chassis_indicator_table[i].icon; | |
918 | } | |
919 | } | |
920 | } | |
921 | ||
922 | if(up_native_is_laptop()) { | |
923 | ||
924 | if(!CHASSIS) | |
925 | CHASSIS = "laptop"; | |
926 | if(!ICON) | |
927 | ICON = "input-touchpad"; /* TODO pull an icon package that actually has the icons we're looking for */ | |
928 | ||
929 | } else if(is_server(hwmachine)) { | |
930 | ||
931 | if(!CHASSIS) | |
932 | CHASSIS = "server"; | |
933 | if(!ICON) | |
934 | ICON = "uninterruptible-power-supply"; | |
935 | ||
936 | } else if(!CHASSIS || !ICON) { | |
f1ad9351 | 937 | |
f8257c5d | 938 | if(!CHASSIS) |
939 | CHASSIS = "desktop"; | |
940 | if(!ICON) | |
941 | ICON = "computer"; | |
942 | } | |
943 | ||
944 | return (CHASSIS && ICON); | |
945 | } | |
946 | ||
947 | gboolean is_server(gchar *arch) { | |
948 | ||
949 | unsigned int i; | |
a1bcc33c | 950 | |
f8257c5d | 951 | for(; i < G_N_ELEMENTS(server_archs); i++) |
952 | if(strcasestr(arch, server_archs[i])) | |
953 | return TRUE; | |
954 | ||
955 | return FALSE; | |
956 | } | |
957 | ||
958 | gboolean up_native_is_laptop() { | |
959 | ||
960 | struct apm_power_info bstate; | |
961 | struct sensordev acpiac; | |
962 | ||
963 | if (up_native_get_sensordev("acpiac0", &acpiac)) | |
964 | return TRUE; | |
965 | ||
966 | if (-1 == ioctl(up_apm_get_fd(), APM_IOC_GETPOWER, &bstate)) | |
967 | g_error("ioctl on apm fd failed : %s", g_strerror(errno)); | |
968 | ||
969 | return bstate.ac_state != APM_AC_UNKNOWN; | |
970 | } | |
971 | ||
972 | int up_apm_get_fd() { | |
973 | ||
974 | static int apm_fd = 0; | |
975 | ||
976 | if(apm_fd == 0) { | |
977 | ||
978 | g_debug("apm_fd is not initialized yet, opening"); | |
979 | ||
980 | /* open /dev/apm */ | |
981 | if((apm_fd = open("/dev/apm", O_RDONLY)) == -1) { | |
982 | if(errno != ENXIO && errno != ENOENT) | |
983 | g_error("cannot open device file"); | |
984 | } | |
985 | } | |
986 | ||
987 | return apm_fd; | |
988 | } | |
989 | ||
990 | gboolean up_native_get_sensordev(const char * id, struct sensordev * snsrdev) { | |
991 | ||
992 | int devn; | |
993 | size_t sdlen = sizeof(struct sensordev); | |
994 | int mib[] = {CTL_HW, HW_SENSORS, 0, 0 ,0}; | |
995 | ||
996 | for (devn = 0 ; ; devn++) { | |
997 | mib[2] = devn; | |
998 | if(sysctl(mib, 3, snsrdev, &sdlen, NULL, 0) == -1) { | |
999 | if(errno == ENXIO) | |
1000 | continue; | |
1001 | if(errno == ENOENT) | |
1002 | break; | |
1003 | } | |
1004 | ||
1005 | if (!strcmp(snsrdev->xname, id)) | |
1006 | return TRUE; | |
1007 | } | |
1008 | ||
1009 | return FALSE; | |
f1ad9351 | 1010 | } |
933f62bb | 1011 | |
1012 | static gboolean is_valid_chassis_type(gchar *test) { | |
1013 | ||
1014 | if(!g_strcmp0(test, "desktop") || | |
1015 | !g_strcmp0(test, "laptop") || | |
1016 | !g_strcmp0(test, "server") || | |
1017 | !g_strcmp0(test, "tablet") || | |
1018 | !g_strcmp0(test, "handset") || | |
1019 | !g_strcmp0(test, "vm") || | |
1020 | !g_strcmp0(test, "container") || | |
1021 | !g_strcmp0(test, "")) | |
1022 | return TRUE; | |
1023 | ||
1024 | return FALSE; | |
1025 | } |