X-Git-Url: https://uglyman.kremlin.cc/gitweb/gitweb.cgi?p=systembsd.git;a=blobdiff_plain;f=src%2Finterfaces%2Fhostnamed%2Fhostnamed.c;h=7779870b5a46c27070477c1dd819e782ea6c0655;hp=6d83852788ab13c6faf08b88f5eaeffafdcce55a;hb=a1bcc33cfc213eb21997f169ef433f186f483da0;hpb=90f544079ab170636e65ed7815317559f2a7924b diff --git a/src/interfaces/hostnamed/hostnamed.c b/src/interfaces/hostnamed/hostnamed.c index 6d83852..7779870 100644 --- a/src/interfaces/hostnamed/hostnamed.c +++ b/src/interfaces/hostnamed/hostnamed.c @@ -19,14 +19,54 @@ #include #include +#include #include #include #include +/* #include */ #include "hostnamed-gen.h" #include "hostnamed.h" +/* add any sysctl strings that suggest virtualization here */ +/* format: { + * (1) string to be matched against runtime machine's sysctl output. + * can be either the exact string or a substring contained + * within sysctl strings. no "guesses" here, a match should + * reliably indicate the chassis/icon. + * + * (2) string describing chassis type divulged by (1). + * must be one of "desktop", "laptop", "server", + * "tablet", "handset", "vm", "container" or NULL + * if only icon string can be ascertained. "vm" refers + * to operating systems running on baremetal hypervisors + * (hardware virtualization, like XEN) while "container" + * refers to OSs running on shared hypervisors like + * virtualbox or VMware. consider the distinction carefully + * as common virtualization software like KVM may share + * characteristics of both "vm" and "container" types. + * + * (3) string specifying icon to use. follows XDG icon spec. + * see http://standards.freedesktop.org/icon-naming-spec/icon-naming-spec-latest.html + * for allowed strings. + * + * (4) chassis precedence bit. TRUE if (2) is defined and + * we're certain it is the proper string. FALSE in the + * circumstance (2) may be the correct string, unless + * a match with this bit set to TRUE overrides it. + * if (2) is NULL, this bit is inconsequential. + * + * (5) icon precedence bit. see previous definition. + * } */ +struct SYSCTL_LOOKUP_TABLE { + gchar *match_string; + gchar *chassis; + gchar *icon; + gboolean chassis_precedence; + gboolean icon_precedence; +}; + GPtrArray *hostnamed_freeable; Hostname1 *hostnamed_interf; @@ -35,6 +75,29 @@ GMainLoop *hostnamed_loop; guint bus_descriptor; gboolean dbus_interface_exported; /* reliable because of gdbus operational guarantees */ +gchar *CHASSIS, *ICON; + +/* TODO no specific vm or laptop icon in gnome + * NOTE paravirtualization on xen is only available for linuxes right now + * dmesg on linux systems reveals xen and virtualization method (HVM or PVM) + * but we will worry about those later */ +const struct SYSCTL_LOOKUP_TABLE chassis_indicator_table[] = +{ + { "QEMU Virtual CPU", "container", "drive-optical", FALSE, FALSE }, /* could be QEMU running in userspace or as part of KVM */ + { "SmartDC HVM", "vm", "drive-multidisk", TRUE, TRUE }, /* oracle solaris kvm */ + { "VirtualBox", "container", "drive-optical", TRUE, TRUE }, + { "VMware, Inc.", "container", "drive-optical", TRUE, TRUE }, + { "VMware Virtual Platform", "container", "drive-optical", TRUE, TRUE }, + { "Parallels", "container", "drive-optical", TRUE, TRUE } /* need verification */ +}; + +/*const gchar* vmstring_list[] = { + "QEMU Virtual CPU", + "SmartDC HVM", + "KVM", + "VirtualBox" +};*/ + /* --- begin method/property/dbus signal code --- */ static gboolean @@ -89,13 +152,14 @@ our_get_hostname() { gchar *hostname_buf, *ret; size_t hostname_divider; - hostname_buf = (gchar*) g_malloc0(MAXHOSTNAMELEN); /* todo check & free */ + hostname_buf = (gchar*) g_malloc0(MAXHOSTNAMELEN); ret = (gchar*) g_malloc0(MAXHOSTNAMELEN); + g_ptr_array_add(hostnamed_freeable, hostname_buf); g_ptr_array_add(hostnamed_freeable, ret); - if(gethostname(hostname_buf, MAXHOSTNAMELEN)) - return ""; + if(gethostname(hostname_buf, MAXHOSTNAMELEN) || g_strcmp0(hostname_buf, "") == 0) + return "localhost"; hostname_divider = strcspn(hostname_buf, "."); @@ -105,19 +169,49 @@ our_get_hostname() { const gchar * our_get_static_hostname() { - return "TODO"; + const gchar *pretty_hostname; + const gchar *ret; + + pretty_hostname = our_get_pretty_hostname(); + + if(g_strcmp0(pretty_hostname, "") == 0) + ret = our_get_hostname(); + + else if((ret = g_hostname_to_ascii(pretty_hostname))) { + + g_ptr_array_add(hostnamed_freeable, (gpointer)ret); + return ret; + } + + return ret; } const gchar * our_get_pretty_hostname() { - return "TODO"; + GKeyFile *config; + gchar *ret; + + config = g_key_file_new(); + + if(g_key_file_load_from_file(config, "/etc/systemd_compat.conf", G_KEY_FILE_NONE, NULL) + && (ret = g_key_file_get_value(config, "hostnamed", "PrettyHostname", NULL))) { /* ret might need to be freed, docs dont specify but i am suspicious */ + + g_key_file_unref(config); + return ret; + } + + if(config) + g_free(config); + + return ""; } const gchar * our_get_chassis() { - return "TODO"; + return "asdf"; + } const gchar * @@ -263,7 +357,10 @@ int main() { set_signal_handlers(); - hostnamed_loop = g_main_loop_new(NULL, TRUE); + if(!build_chassis_table() || !determine_chassis_and_icon()) + return 1; + + hostnamed_loop = g_main_loop_new(NULL, TRUE); hostnamed_freeable = g_ptr_array_new(); bus_descriptor = g_bus_own_name(G_BUS_TYPE_SYSTEM, @@ -288,6 +385,49 @@ int main() { return 0; } +static gboolean build_chassis_table() { + return TRUE; +} + +gboolean determine_chassis_and_icon() { + + char *hwproduct, *hwmodel, *hwvendor; + size_t hwproduct_size, hwmodel_size, hwvendor_size; + int hwproduct_name[2], hwmodel_name[2], hwvendor_name[2]; + unsigned int i; + + hwproduct_name[0] = CTL_HW; + hwproduct_name[1] = HW_PRODUCT; + + hwmodel_name[0] = CTL_HW; + hwmodel_name[1] = HW_MODEL; + + hwvendor_name[0] = CTL_HW; + hwvendor_name[1] = HW_VENDOR; + + /* pass NULL buffer to check size first, then pass hw to be filled according to freshly-set hw_size */ + if(-1 == sysctl(hwproduct_name, 2, NULL, &hwproduct_size, NULL, 0) || -1 == sysctl(hwproduct_name, 2, hwproduct, &hwproduct_size, NULL, 0)) + return FALSE; + + if(-1 == sysctl(hwmodel_name, 2, NULL, &hwmodel_size, NULL, 0) || -1 == sysctl(hwmodel_name, 2, hwmodel, &hwmodel_size, NULL, 0)) + return FALSE; + + if(-1 == sysctl(hwvendor_name, 2, NULL, &hwvendor_size, NULL, 0) || -1 == sysctl(hwvendor_name, 2, hwvendor, &hwvendor_size, NULL, 0)) + return FALSE; + + /* TODO: test for laptop, if not, dmidecode for desktop vs. server + * probably move this code to vm test func and set a global after running it early, once */ + + for(; i < G_N_ELEMENTS(chassis_indicator_table); i++) + + /* if(strcasestr(sysctl_string, vmstring_list[i])) + return (CHASSIS = ) ? TRUE : FALSE; */ + + return FALSE; + + return TRUE; /* temp */ +} + /* TODO figure out DMI variables on obsd */ /*static gchar *guess_icon_name() {