| 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 | |
| 17 | #include <unistd.h> |
| 18 | #include <limits.h> |
| 19 | #include <signal.h> |
| 20 | #include <string.h> |
| 21 | |
| 22 | #include <sys/param.h> |
| 23 | #include <sys/sysctl.h> |
| 24 | #include <sys/sensors.h> |
| 25 | #include <sys/ioctl.h> |
| 26 | #include <sys/utsname.h> |
| 27 | |
| 28 | #include <machine/apmvar.h> |
| 29 | |
| 30 | #include <glib/gprintf.h> |
| 31 | #include <glib-unix.h> |
| 32 | /* #include <gtk/gtk.h> */ |
| 33 | |
| 34 | #include "hostnamed-gen.h" |
| 35 | #include "hostnamed.h" |
| 36 | |
| 37 | /* add any sysctl strings that suggest virtualization here */ |
| 38 | /* format: { |
| 39 | * (1) string to be matched against runtime machine's sysctl output. |
| 40 | * can be either the exact string or a substring contained |
| 41 | * within sysctl strings. no "guesses" here, a match should |
| 42 | * reliably indicate the chassis/icon. |
| 43 | * |
| 44 | * (2) string describing chassis type divulged by (1). |
| 45 | * must be one of "desktop", "laptop", "server", |
| 46 | * "tablet", "handset", "vm", "container" or NULL |
| 47 | * if only icon string can be ascertained. "vm" refers |
| 48 | * to operating systems running on baremetal hypervisors |
| 49 | * (hardware virtualization, like XEN) while "container" |
| 50 | * refers to OSs running on shared hypervisors like |
| 51 | * virtualbox or VMware. consider the distinction carefully |
| 52 | * as common virtualization software like KVM may share |
| 53 | * characteristics of both "vm" and "container" types. |
| 54 | * |
| 55 | * (3) string specifying icon to use. follows XDG icon spec. |
| 56 | * see http://standards.freedesktop.org/icon-naming-spec/icon-naming-spec-latest.html |
| 57 | * for allowed strings. |
| 58 | * |
| 59 | * (4) chassis precedence bit. TRUE if (2) is defined and |
| 60 | * we're certain it is the proper string. FALSE in the |
| 61 | * circumstance (2) may be the correct string, unless |
| 62 | * a match with this bit set to TRUE overrides it. |
| 63 | * if (2) is NULL, this bit is inconsequential. |
| 64 | * |
| 65 | * (5) icon precedence bit. see previous definition. |
| 66 | * } */ |
| 67 | struct SYSCTL_LOOKUP_TABLE { |
| 68 | gchar *match_string; |
| 69 | gchar *chassis; |
| 70 | gchar *icon; |
| 71 | gboolean chassis_precedence; |
| 72 | gboolean icon_precedence; |
| 73 | }; |
| 74 | |
| 75 | GPtrArray *hostnamed_freeable; |
| 76 | Hostname1 *hostnamed_interf; |
| 77 | |
| 78 | GMainLoop *hostnamed_loop; |
| 79 | |
| 80 | guint bus_descriptor; |
| 81 | gboolean dbus_interface_exported; /* reliable because of gdbus operational guarantees */ |
| 82 | |
| 83 | gchar *CHASSIS, *ICON; |
| 84 | gchar *OS_CPENAME; |
| 85 | gchar *KERN_NAME, *KERN_RELEASE, *KERN_VERS; |
| 86 | |
| 87 | /* TODO no specific vm or laptop icon in gnome |
| 88 | * NOTE paravirtualization on xen is only available for linuxes right now |
| 89 | * dmesg on linux systems reveals xen and virtualization method (HVM or PVM) |
| 90 | * but we will worry about those later */ |
| 91 | const struct SYSCTL_LOOKUP_TABLE chassis_indicator_table[] = |
| 92 | { |
| 93 | { "QEMU Virtual CPU", "vm", NULL, FALSE, FALSE }, /* could be QEMU running in userspace or as part of KVM */ |
| 94 | { "KVM", "vm", "drive-multidisk", FALSE, FALSE }, |
| 95 | { "SmartDC HVM", "vm", "drive-multidisk", TRUE, TRUE }, /* illumos-joyent kvm */ |
| 96 | { "VirtualBox", "vm", "drive-multidisk", TRUE, TRUE }, |
| 97 | { "VMware, Inc.", "vm", "drive-multidisk", TRUE, TRUE }, |
| 98 | { "VMware Virtual Platform", "vm", "drive-multidisk", TRUE, TRUE }, |
| 99 | { "Parallels", "vm", "drive-multidisk", TRUE, TRUE }, /* need verification */ |
| 100 | { "Xen", "vm", "drive-multidisk", TRUE, TRUE } |
| 101 | }; /* TODO: chroots, etc. are the actual "containers", add them */ |
| 102 | |
| 103 | /* archs to check against when determining if machine is server */ |
| 104 | const gchar *server_archs[] = { |
| 105 | "hppa", |
| 106 | "sparc", |
| 107 | "sparc64" |
| 108 | }; |
| 109 | |
| 110 | /* --- begin method/property/dbus signal code --- */ |
| 111 | |
| 112 | static gboolean |
| 113 | on_handle_set_hostname(Hostname1 *hn1_passed_interf, |
| 114 | GDBusMethodInvocation *invoc, |
| 115 | const gchar *greet, |
| 116 | gpointer data) { |
| 117 | return FALSE; |
| 118 | } |
| 119 | |
| 120 | static gboolean |
| 121 | on_handle_set_static_hostname(Hostname1 *hn1_passed_interf, |
| 122 | GDBusMethodInvocation *invoc, |
| 123 | const gchar *greet, |
| 124 | gpointer data) { |
| 125 | return FALSE; |
| 126 | } |
| 127 | |
| 128 | static gboolean |
| 129 | on_handle_set_pretty_hostname(Hostname1 *hn1_passed_interf, |
| 130 | GDBusMethodInvocation *invoc, |
| 131 | const gchar *greet, |
| 132 | gpointer data) { |
| 133 | return FALSE; |
| 134 | } |
| 135 | |
| 136 | static gboolean |
| 137 | on_handle_set_chassis(Hostname1 *hn1_passed_interf, |
| 138 | GDBusMethodInvocation *invoc, |
| 139 | const gchar *greet, |
| 140 | gpointer data) { |
| 141 | return FALSE; |
| 142 | } |
| 143 | |
| 144 | static gboolean |
| 145 | on_handle_set_icon_name(Hostname1 *hn1_passed_interf, |
| 146 | GDBusMethodInvocation *invoc, |
| 147 | const gchar *greet, |
| 148 | gpointer data) { |
| 149 | return FALSE; |
| 150 | } |
| 151 | |
| 152 | /* note: all hostnamed/hostname1's properties are read-only, |
| 153 | * and do not need set_ functions, gdbus-codegen realized |
| 154 | * this from the XML and handled the to-be error of trying |
| 155 | * to set a read-only property's value |
| 156 | */ |
| 157 | |
| 158 | const gchar * |
| 159 | our_get_hostname() { |
| 160 | |
| 161 | gchar *hostname_buf, *ret; |
| 162 | size_t hostname_divider; |
| 163 | |
| 164 | hostname_buf = (gchar*) g_malloc0(MAXHOSTNAMELEN); |
| 165 | ret = (gchar*) g_malloc0(MAXHOSTNAMELEN); |
| 166 | |
| 167 | g_ptr_array_add(hostnamed_freeable, hostname_buf); |
| 168 | g_ptr_array_add(hostnamed_freeable, ret); |
| 169 | |
| 170 | if(gethostname(hostname_buf, MAXHOSTNAMELEN) || g_strcmp0(hostname_buf, "") == 0) |
| 171 | return "localhost"; |
| 172 | |
| 173 | hostname_divider = strcspn(hostname_buf, "."); |
| 174 | |
| 175 | return strncpy(ret, hostname_buf, hostname_divider); |
| 176 | } |
| 177 | |
| 178 | const gchar * |
| 179 | our_get_static_hostname() { |
| 180 | |
| 181 | const gchar *pretty_hostname; |
| 182 | const gchar *ret; |
| 183 | |
| 184 | pretty_hostname = our_get_pretty_hostname(); |
| 185 | |
| 186 | if(g_strcmp0(pretty_hostname, "") == 0) |
| 187 | ret = our_get_hostname(); |
| 188 | |
| 189 | else if((ret = g_hostname_to_ascii(pretty_hostname))) { |
| 190 | |
| 191 | g_ptr_array_add(hostnamed_freeable, (gpointer)ret); |
| 192 | return ret; |
| 193 | } |
| 194 | |
| 195 | return ret; |
| 196 | } |
| 197 | |
| 198 | const gchar * |
| 199 | our_get_pretty_hostname() { |
| 200 | |
| 201 | GKeyFile *config; |
| 202 | gchar *ret; |
| 203 | |
| 204 | config = g_key_file_new(); |
| 205 | |
| 206 | if(g_key_file_load_from_file(config, "/etc/systemd_compat.conf", G_KEY_FILE_NONE, NULL) |
| 207 | && (ret = g_key_file_get_value(config, "hostnamed", "PrettyHostname", NULL))) { /* ret might need to be freed, docs dont specify but i am suspicious */ |
| 208 | |
| 209 | g_key_file_unref(config); |
| 210 | return ret; |
| 211 | } |
| 212 | |
| 213 | if(config) |
| 214 | g_free(config); |
| 215 | |
| 216 | return ""; |
| 217 | } |
| 218 | |
| 219 | const gchar * |
| 220 | our_get_chassis() { |
| 221 | |
| 222 | if(CHASSIS) |
| 223 | return CHASSIS; |
| 224 | |
| 225 | return "desktop"; |
| 226 | } |
| 227 | |
| 228 | const gchar * |
| 229 | our_get_icon_name() { |
| 230 | |
| 231 | if(ICON) |
| 232 | return ICON; |
| 233 | |
| 234 | return ""; |
| 235 | } |
| 236 | |
| 237 | const gchar * |
| 238 | our_get_kernel_name() { |
| 239 | |
| 240 | if(KERN_NAME) |
| 241 | return KERN_NAME; |
| 242 | |
| 243 | return ""; |
| 244 | } |
| 245 | |
| 246 | const gchar * |
| 247 | our_get_kernel_version() { |
| 248 | |
| 249 | if(KERN_VERS) |
| 250 | return KERN_VERS; |
| 251 | |
| 252 | return ""; |
| 253 | } |
| 254 | |
| 255 | const gchar * |
| 256 | our_get_kernel_release() { |
| 257 | |
| 258 | if(KERN_RELEASE) |
| 259 | return KERN_RELEASE; |
| 260 | |
| 261 | return ""; |
| 262 | } |
| 263 | |
| 264 | const gchar * |
| 265 | our_get_os_cpename() { |
| 266 | |
| 267 | return "ONEDAY"; |
| 268 | } |
| 269 | |
| 270 | const gchar * |
| 271 | our_get_os_pretty_name() { |
| 272 | |
| 273 | return "OpenBSD"; |
| 274 | } |
| 275 | |
| 276 | /* --- end method/property/dbus signal code, begin bus/name handlers --- */ |
| 277 | |
| 278 | static void hostnamed_on_bus_acquired(GDBusConnection *conn, |
| 279 | const gchar *name, |
| 280 | gpointer user_data) { |
| 281 | |
| 282 | g_printf("got bus/name, exporting %s's interface...\n", name); |
| 283 | |
| 284 | hostnamed_interf = hostname1_skeleton_new(); |
| 285 | |
| 286 | /* attach function pointers to generated struct's method handlers */ |
| 287 | g_signal_connect(hostnamed_interf, "handle-set-hostname", G_CALLBACK(on_handle_set_hostname), NULL); |
| 288 | g_signal_connect(hostnamed_interf, "handle-set-static-hostname", G_CALLBACK(on_handle_set_static_hostname), NULL); |
| 289 | g_signal_connect(hostnamed_interf, "handle-set-pretty-hostname", G_CALLBACK(on_handle_set_pretty_hostname), NULL); |
| 290 | g_signal_connect(hostnamed_interf, "handle-set-chassis", G_CALLBACK(on_handle_set_chassis), NULL); |
| 291 | g_signal_connect(hostnamed_interf, "handle-set-icon-name", G_CALLBACK(on_handle_set_icon_name), NULL); |
| 292 | |
| 293 | /* set our properties before export */ |
| 294 | hostname1_set_hostname(hostnamed_interf, our_get_hostname()); |
| 295 | hostname1_set_static_hostname(hostnamed_interf, our_get_static_hostname()); |
| 296 | hostname1_set_pretty_hostname(hostnamed_interf, our_get_pretty_hostname()); |
| 297 | hostname1_set_chassis(hostnamed_interf, our_get_chassis()); |
| 298 | hostname1_set_icon_name(hostnamed_interf, our_get_icon_name()); |
| 299 | hostname1_set_kernel_name(hostnamed_interf, our_get_kernel_name()); |
| 300 | hostname1_set_kernel_version(hostnamed_interf, our_get_kernel_version()); |
| 301 | hostname1_set_kernel_release(hostnamed_interf, our_get_kernel_release()); |
| 302 | hostname1_set_operating_system_cpename(hostnamed_interf, our_get_os_cpename()); |
| 303 | hostname1_set_operating_system_pretty_name(hostnamed_interf, our_get_os_pretty_name()); |
| 304 | |
| 305 | if(!g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(hostnamed_interf), |
| 306 | conn, |
| 307 | "/org/freedesktop/hostname1", |
| 308 | NULL)) { |
| 309 | |
| 310 | g_printf("failed to export %s's interface!\n", name); /* unusual edge case, TODO check errno */ |
| 311 | hostnamed_mem_clean(); |
| 312 | |
| 313 | } else { |
| 314 | |
| 315 | dbus_interface_exported = TRUE; |
| 316 | g_printf("exported %s's interface on the system bus...\n", name); |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | static void hostnamed_on_name_acquired(GDBusConnection *conn, |
| 321 | const gchar *name, |
| 322 | gpointer user_data) { |
| 323 | |
| 324 | g_printf("success!\n"); |
| 325 | } |
| 326 | |
| 327 | static void hostnamed_on_name_lost(GDBusConnection *conn, |
| 328 | const gchar *name, |
| 329 | gpointer user_data) { |
| 330 | |
| 331 | if(!conn) { |
| 332 | |
| 333 | 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); |
| 334 | hostnamed_mem_clean(); |
| 335 | } |
| 336 | |
| 337 | g_printf("lost name %s, exiting...\n", name); |
| 338 | |
| 339 | hostnamed_mem_clean(); |
| 340 | } |
| 341 | |
| 342 | /* --- end bus/name handlers, begin misc unix functions --- */ |
| 343 | |
| 344 | /* safe call to clean and then exit |
| 345 | * this stops our GMainLoop safely before letting main() return */ |
| 346 | void hostnamed_mem_clean() { |
| 347 | |
| 348 | g_printf("exiting...\n"); |
| 349 | |
| 350 | if(dbus_interface_exported) |
| 351 | g_dbus_interface_skeleton_unexport(G_DBUS_INTERFACE_SKELETON(hostnamed_interf)); |
| 352 | |
| 353 | if(g_main_loop_is_running(hostnamed_loop)) |
| 354 | g_main_loop_quit(hostnamed_loop); |
| 355 | |
| 356 | } |
| 357 | |
| 358 | /* wrapper for glib's unix signal handling; called only once if terminatating signal is raised against us */ |
| 359 | gboolean unix_sig_terminate_handler(gpointer data) { |
| 360 | |
| 361 | g_printf("caught SIGINT/HUP/TERM, exiting\n"); |
| 362 | |
| 363 | hostnamed_mem_clean(); |
| 364 | return G_SOURCE_REMOVE; |
| 365 | } |
| 366 | |
| 367 | void set_signal_handlers() { |
| 368 | |
| 369 | /* we don't care about its descriptor, we never need to unregister these */ |
| 370 | g_unix_signal_add(SIGINT, unix_sig_terminate_handler, NULL); |
| 371 | g_unix_signal_add(SIGHUP, unix_sig_terminate_handler, NULL); |
| 372 | g_unix_signal_add(SIGTERM, unix_sig_terminate_handler, NULL); |
| 373 | |
| 374 | /* TODO: the "only once" guarantee only counts towards specific signals. |
| 375 | * make sure calling a SIGINT and SIGHUP doesn't cause term_handler() |
| 376 | * to be called twice */ |
| 377 | } |
| 378 | |
| 379 | int main() { |
| 380 | |
| 381 | hostnamed_freeable = g_ptr_array_new(); |
| 382 | |
| 383 | /* TODO: check for valid, writable config at init. if no, complain to `make install` */ |
| 384 | |
| 385 | CHASSIS = ICON = OS_CPENAME = 0; |
| 386 | KERN_NAME = KERN_RELEASE = KERN_VERS = 0; |
| 387 | |
| 388 | set_signal_handlers(); |
| 389 | |
| 390 | if(!determine_chassis_and_icon() || !set_uname_properties()) |
| 391 | return 1; |
| 392 | |
| 393 | hostnamed_loop = g_main_loop_new(NULL, TRUE); |
| 394 | |
| 395 | bus_descriptor = g_bus_own_name(G_BUS_TYPE_SYSTEM, |
| 396 | "org.freedesktop.hostname1", |
| 397 | G_BUS_NAME_OWNER_FLAGS_NONE, |
| 398 | hostnamed_on_bus_acquired, |
| 399 | hostnamed_on_name_acquired, |
| 400 | hostnamed_on_name_lost, |
| 401 | NULL, |
| 402 | NULL); |
| 403 | |
| 404 | g_main_loop_run(hostnamed_loop); |
| 405 | /* runs until single g_main_loop_quit() call is raised inside <interface>_mem_clean() */ |
| 406 | g_main_loop_unref(hostnamed_loop); |
| 407 | |
| 408 | /* guaranteed unownable */ |
| 409 | g_bus_unown_name(bus_descriptor); |
| 410 | |
| 411 | /* at this point no operations can occur with our data, it is safe to free it + its container */ |
| 412 | g_ptr_array_free(hostnamed_freeable, TRUE); |
| 413 | |
| 414 | return 0; |
| 415 | } |
| 416 | |
| 417 | gboolean set_uname_properties() { |
| 418 | |
| 419 | struct utsname un; |
| 420 | |
| 421 | if(-1 == uname(&un)) |
| 422 | return FALSE; |
| 423 | |
| 424 | KERN_NAME = (gchar*)g_malloc0(sizeof(un.sysname)); |
| 425 | g_ptr_array_add(hostnamed_freeable, KERN_NAME); |
| 426 | g_strlcpy(KERN_NAME, un.sysname, sizeof(un.sysname)); |
| 427 | |
| 428 | KERN_RELEASE = (gchar*)g_malloc0(sizeof(un.release)); |
| 429 | g_ptr_array_add(hostnamed_freeable, KERN_RELEASE); |
| 430 | g_strlcpy(KERN_RELEASE, un.release, sizeof(un.release)); |
| 431 | |
| 432 | KERN_VERS = (gchar*)g_malloc0(sizeof(un.version)); |
| 433 | g_ptr_array_add(hostnamed_freeable, KERN_VERS); |
| 434 | g_strlcpy(KERN_VERS, un.version, sizeof(un.version)); |
| 435 | |
| 436 | return TRUE; |
| 437 | } |
| 438 | |
| 439 | gboolean determine_chassis_and_icon() { |
| 440 | |
| 441 | const size_t bufsize = 4096; |
| 442 | |
| 443 | char *hwproduct, *hwmodel, *hwvendor, *hwmachine; |
| 444 | size_t hwproduct_size, hwmodel_size, hwvendor_size, hwmachine_size; |
| 445 | int hwproduct_name[2], hwmodel_name[2], hwvendor_name[2], hwmachine_name[2]; |
| 446 | unsigned int i; |
| 447 | gboolean UNSURE_CHASSIS_FLAG, UNSURE_ICON_FLAG; |
| 448 | |
| 449 | hwproduct_size = hwmodel_size = hwvendor_size = hwmachine_size = bufsize; |
| 450 | UNSURE_CHASSIS_FLAG = UNSURE_ICON_FLAG = FALSE; |
| 451 | i = 0; |
| 452 | |
| 453 | hwproduct = (char*)g_malloc0(4096); |
| 454 | hwmodel = (char*)g_malloc0(4096); |
| 455 | hwvendor = (char*)g_malloc0(4096); |
| 456 | hwmachine = (char*)g_malloc0(4096); |
| 457 | |
| 458 | g_ptr_array_add(hostnamed_freeable, hwproduct); |
| 459 | g_ptr_array_add(hostnamed_freeable, hwmodel); |
| 460 | g_ptr_array_add(hostnamed_freeable, hwvendor); |
| 461 | g_ptr_array_add(hostnamed_freeable, hwmachine); |
| 462 | |
| 463 | hwproduct_name[0] = CTL_HW; |
| 464 | hwproduct_name[1] = HW_PRODUCT; |
| 465 | |
| 466 | hwmodel_name[0] = CTL_HW; |
| 467 | hwmodel_name[1] = HW_MODEL; |
| 468 | |
| 469 | hwvendor_name[0] = CTL_HW; |
| 470 | hwvendor_name[1] = HW_VENDOR; |
| 471 | |
| 472 | hwmachine_name[0] = CTL_HW; |
| 473 | hwmachine_name[1] = HW_MACHINE; |
| 474 | |
| 475 | /* pass NULL buffer to check size first, then pass hw to be filled according to freshly-set hw_size */ |
| 476 | if(-1 == sysctl(hwproduct_name, 2, NULL, &hwproduct_size, NULL, 0) || -1 == sysctl(hwproduct_name, 2, hwproduct, &hwproduct_size, NULL, 0)) |
| 477 | return FALSE; |
| 478 | |
| 479 | if(-1 == sysctl(hwmodel_name, 2, NULL, &hwmodel_size, NULL, 0) || -1 == sysctl(hwmodel_name, 2, hwmodel, &hwmodel_size, NULL, 0)) |
| 480 | return FALSE; |
| 481 | |
| 482 | if(-1 == sysctl(hwvendor_name, 2, NULL, &hwvendor_size, NULL, 0) || -1 == sysctl(hwvendor_name, 2, hwvendor, &hwvendor_size, NULL, 0)) |
| 483 | return FALSE; |
| 484 | |
| 485 | if(-1 == sysctl(hwmachine_name, 2, NULL, &hwmachine_size, NULL, 0) || -1 == sysctl(hwmachine_name, 2, hwmachine, &hwmachine_size, NULL, 0)) |
| 486 | return FALSE; |
| 487 | |
| 488 | /* TODO: test for laptop, if not, dmidecode for desktop vs. server |
| 489 | * probably move this code to vm test func and set a global after running it early, once */ |
| 490 | |
| 491 | for(; i < G_N_ELEMENTS(chassis_indicator_table); i++) { |
| 492 | if(strcasestr(hwproduct, chassis_indicator_table[i].match_string) |
| 493 | || strcasestr(hwmodel, chassis_indicator_table[i].match_string) |
| 494 | || strcasestr(hwvendor, chassis_indicator_table[i].match_string)) { |
| 495 | |
| 496 | if(!UNSURE_CHASSIS_FLAG && chassis_indicator_table[i].chassis) { |
| 497 | |
| 498 | UNSURE_CHASSIS_FLAG = chassis_indicator_table[i].chassis_precedence; |
| 499 | CHASSIS = chassis_indicator_table[i].chassis; |
| 500 | } |
| 501 | |
| 502 | if(!UNSURE_ICON_FLAG && chassis_indicator_table[i].icon) { |
| 503 | |
| 504 | UNSURE_ICON_FLAG = chassis_indicator_table[i].icon_precedence; |
| 505 | ICON = chassis_indicator_table[i].icon; |
| 506 | } |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | if(up_native_is_laptop()) { |
| 511 | |
| 512 | if(!CHASSIS) |
| 513 | CHASSIS = "laptop"; |
| 514 | if(!ICON) |
| 515 | ICON = "input-touchpad"; /* TODO pull an icon package that actually has the icons we're looking for */ |
| 516 | |
| 517 | } else if(is_server(hwmachine)) { |
| 518 | |
| 519 | if(!CHASSIS) |
| 520 | CHASSIS = "server"; |
| 521 | if(!ICON) |
| 522 | ICON = "uninterruptible-power-supply"; |
| 523 | |
| 524 | } else if(!CHASSIS || !ICON) { |
| 525 | |
| 526 | if(!CHASSIS) |
| 527 | CHASSIS = "desktop"; |
| 528 | if(!ICON) |
| 529 | ICON = "computer"; |
| 530 | } |
| 531 | |
| 532 | return (CHASSIS && ICON); |
| 533 | } |
| 534 | |
| 535 | gboolean is_server(gchar *arch) { |
| 536 | |
| 537 | unsigned int i; |
| 538 | |
| 539 | for(; i < G_N_ELEMENTS(server_archs); i++) |
| 540 | if(strcasestr(arch, server_archs[i])) |
| 541 | return TRUE; |
| 542 | |
| 543 | return FALSE; |
| 544 | } |
| 545 | |
| 546 | gboolean up_native_is_laptop() { |
| 547 | |
| 548 | struct apm_power_info bstate; |
| 549 | struct sensordev acpiac; |
| 550 | |
| 551 | if (up_native_get_sensordev("acpiac0", &acpiac)) |
| 552 | return TRUE; |
| 553 | |
| 554 | if (-1 == ioctl(up_apm_get_fd(), APM_IOC_GETPOWER, &bstate)) |
| 555 | g_error("ioctl on apm fd failed : %s", g_strerror(errno)); |
| 556 | |
| 557 | return bstate.ac_state != APM_AC_UNKNOWN; |
| 558 | } |
| 559 | |
| 560 | int up_apm_get_fd() { |
| 561 | |
| 562 | static int apm_fd = 0; |
| 563 | |
| 564 | if(apm_fd == 0) { |
| 565 | |
| 566 | g_debug("apm_fd is not initialized yet, opening"); |
| 567 | |
| 568 | /* open /dev/apm */ |
| 569 | if((apm_fd = open("/dev/apm", O_RDONLY)) == -1) { |
| 570 | if(errno != ENXIO && errno != ENOENT) |
| 571 | g_error("cannot open device file"); |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | return apm_fd; |
| 576 | } |
| 577 | |
| 578 | gboolean up_native_get_sensordev(const char * id, struct sensordev * snsrdev) { |
| 579 | |
| 580 | int devn; |
| 581 | size_t sdlen = sizeof(struct sensordev); |
| 582 | int mib[] = {CTL_HW, HW_SENSORS, 0, 0 ,0}; |
| 583 | |
| 584 | for (devn = 0 ; ; devn++) { |
| 585 | mib[2] = devn; |
| 586 | if(sysctl(mib, 3, snsrdev, &sdlen, NULL, 0) == -1) { |
| 587 | if(errno == ENXIO) |
| 588 | continue; |
| 589 | if(errno == ENOENT) |
| 590 | break; |
| 591 | } |
| 592 | |
| 593 | if (!strcmp(snsrdev->xname, id)) |
| 594 | return TRUE; |
| 595 | } |
| 596 | |
| 597 | return FALSE; |
| 598 | } |