+ if(up_native_is_laptop()) {
+
+ if(!CHASSIS)
+ CHASSIS = "laptop";
+ if(!ICON)
+ ICON = "input-touchpad"; /* TODO pull an icon package that actually has the icons we're looking for */
+
+ } else if(is_server(hwmachine)) {
+
+ if(!CHASSIS)
+ CHASSIS = "server";
+ if(!ICON)
+ ICON = "uninterruptible-power-supply";
+
+ } else if(!CHASSIS || !ICON) {
+
+ if(!CHASSIS)
+ CHASSIS = "desktop";
+ if(!ICON)
+ ICON = "computer";
+ }
+
+ return (CHASSIS && ICON);
+}
+
+gboolean is_server(gchar *arch) {
+
+ unsigned int i;
+
+ for(; i < G_N_ELEMENTS(server_archs); i++)
+ if(strcasestr(arch, server_archs[i]))
+ return TRUE;
+
+ return FALSE;
+}
+
+gboolean up_native_is_laptop() {
+
+ struct apm_power_info bstate;
+ struct sensordev acpiac;
+
+ if (up_native_get_sensordev("acpiac0", &acpiac))
+ return TRUE;
+
+ if (-1 == ioctl(up_apm_get_fd(), APM_IOC_GETPOWER, &bstate))
+ g_error("ioctl on apm fd failed : %s", g_strerror(errno));
+
+ return bstate.ac_state != APM_AC_UNKNOWN;
+}
+
+int up_apm_get_fd() {
+
+ static int apm_fd = 0;
+
+ if(apm_fd == 0) {
+
+ g_debug("apm_fd is not initialized yet, opening");
+
+ /* open /dev/apm */
+ if((apm_fd = open("/dev/apm", O_RDONLY)) == -1) {
+ if(errno != ENXIO && errno != ENOENT)
+ g_error("cannot open device file");
+ }
+ }
+
+ return apm_fd;
+}
+
+gboolean up_native_get_sensordev(const char * id, struct sensordev * snsrdev) {
+
+ int devn;
+ size_t sdlen = sizeof(struct sensordev);
+ int mib[] = {CTL_HW, HW_SENSORS, 0, 0 ,0};
+
+ for (devn = 0 ; ; devn++) {
+ mib[2] = devn;
+ if(sysctl(mib, 3, snsrdev, &sdlen, NULL, 0) == -1) {
+ if(errno == ENXIO)
+ continue;
+ if(errno == ENOENT)
+ break;
+ }
+
+ if (!strcmp(snsrdev->xname, id))
+ return TRUE;
+ }
+
+ return FALSE;
+}
+
+static gboolean is_valid_chassis_type(gchar *test) {
+
+ if(!g_strcmp0(test, "desktop") ||
+ !g_strcmp0(test, "laptop") ||
+ !g_strcmp0(test, "server") ||
+ !g_strcmp0(test, "tablet") ||
+ !g_strcmp0(test, "handset") ||
+ !g_strcmp0(test, "vm") ||
+ !g_strcmp0(test, "container") ||
+ !g_strcmp0(test, ""))
+ return TRUE;
+
+ return FALSE;
+}
+
+/* returns a proper, bsd-style FQDN hostname safe to write to /etc/myname
+ * if proposed_hostname does not contain an appended domain, the one in /etc/myname is substituted.
+ * failing that, DEFAULT_DOMAIN is used. NULL if proposed_hostname is invalid
+ * returns string that should be g_free()'d, or NULL if passed an invalid hostname */
+static gchar *get_bsd_hostname(gchar *proposed_hostname) {
+
+ gchar *bsd_hostname, *ascii_translated_hostname, **myname_contents, *passed_domain, *temp_buf;
+ size_t domain_len, check_len;
+ gboolean read_result;
+
+ g_strdelimit(proposed_hostname, "`~!@#$%^&*()_=+[{]}|:;'\"\\", '-');
+
+ ascii_translated_hostname = g_hostname_to_ascii(proposed_hostname);
+ check_len = strnlen(ascii_translated_hostname, MAXHOSTNAMELEN);
+
+ if(!ascii_translated_hostname || !check_len || check_len > MAXHOSTNAMELEN || !g_strcmp0("", ascii_translated_hostname) || !g_strcmp0(".", ascii_translated_hostname)) {
+
+ bsd_hostname = NULL;
+ passed_domain = NULL;
+ myname_contents = NULL;
+
+ } else if((passed_domain = has_domain(ascii_translated_hostname))) {
+
+ bsd_hostname = (gchar *) g_malloc0(MAXHOSTNAMELEN);
+ g_strlcpy(bsd_hostname, ascii_translated_hostname, MAXHOSTNAMELEN);
+
+ passed_domain = NULL;
+ myname_contents = NULL;
+
+ } else {
+
+ myname_contents = (gchar **) g_malloc0(MAXHOSTNAMELEN * 2);
+ read_result = g_file_get_contents(OS_HOSTNAME_PATH, myname_contents, NULL, NULL);
+
+ if(read_result && (passed_domain = has_domain(myname_contents[0]))) {
+
+ domain_len = strnlen(passed_domain, MAXHOSTNAMELEN);
+
+ if((domain_len + check_len) > MAXHOSTNAMELEN)
+ bsd_hostname = NULL;
+ else
+ bsd_hostname = g_strconcat(ascii_translated_hostname, passed_domain, NULL);
+
+ } else if(myname_contents[0]) {
+
+ g_printf("%s does not contain a proper FQDN! this is a significant error on BSD machines, otherwise OK.\nfalling back to default domain, '%s'\n", OS_HOSTNAME_PATH, DEFAULT_DOMAIN);
+
+ domain_len = strnlen(DEFAULT_DOMAIN, MAXHOSTNAMELEN);
+
+ if((domain_len + check_len) > MAXHOSTNAMELEN)
+ bsd_hostname = NULL;
+ else
+ bsd_hostname = g_strconcat(ascii_translated_hostname, DEFAULT_DOMAIN, NULL);
+
+ } else {
+
+ g_printf("could not read hostname at %s, this is a major error\n", OS_HOSTNAME_PATH);
+ bsd_hostname = NULL;
+ passed_domain = (gchar *) g_malloc0(MAXHOSTNAMELEN);
+ }
+ }
+
+ if(passed_domain)
+ g_free(passed_domain);
+ if(myname_contents)
+ g_free(myname_contents);
+
+ if(bsd_hostname && !strchr(bsd_hostname, '\n')) {
+
+ temp_buf = bsd_hostname;
+ bsd_hostname = g_strconcat(bsd_hostname, "\n", NULL);
+ g_free(temp_buf);
+ }
+
+ return bsd_hostname;
+}
+
+/* returns NULL if no domain, otherwise append-appropriate domain string you must g_free()
+ * i.e. has_domain("foo.bar.com") returns ".bar.com"
+ * only pass g_hostname_to_ascii'd strings */
+static gchar *has_domain(const gchar *test) {
+
+ size_t hostname_len, full_len;
+ gchar *ret;
+
+ hostname_len = strcspn(test, ".");
+ full_len = strnlen(test, MAXHOSTNAMELEN);
+
+ if(full_len == hostname_len)
+ return NULL;
+
+ ret = (gchar *) g_malloc0(MAXHOSTNAMELEN);
+ g_strlcpy(ret, &test[hostname_len], MAXHOSTNAMELEN);
+
+ return ret;
+}