| 1 | #include <unistd.h> |
| 2 | #include <limits.h> |
| 3 | #include <gio/gio.h> |
| 4 | |
| 5 | #include "src/interfaces/hostnamed/hostnamed-gen.c" |
| 6 | |
| 7 | GDBusNodeInfo *spect_data; |
| 8 | |
| 9 | /* handled by codegen |
| 10 | static gchar *hostname; |
| 11 | static gchar *pretty_hostname; |
| 12 | static gchar *static_hostname; |
| 13 | static gchar *icon_name; |
| 14 | static gchar *chassis; |
| 15 | static gchar *kernel_name; |
| 16 | static gchar *kernel_release; |
| 17 | static gchar *kernel_version; |
| 18 | static gchar *os_prettyname; |
| 19 | static gchar *os_cpe; //common platform enumeration string |
| 20 | */ |
| 21 | |
| 22 | /* handled by codegen |
| 23 | static void handle_method_call(GDBusConnection *conn, |
| 24 | const gchar *sender, |
| 25 | const gchar *obj_path, |
| 26 | const gchar *interf_name, |
| 27 | const gchar *method_name, |
| 28 | GVariant *params, |
| 29 | GDBusMethodInvocation *invc, |
| 30 | gpointer usrdat) { |
| 31 | |
| 32 | if(g_strcmp0(interf_name, "org.freedesktop.DBus.Introspectable") == 0 |
| 33 | && g_strcmp0(method_name, "Introspect") == 0) { |
| 34 | |
| 35 | GVariant *xml_ret_gvar; |
| 36 | GString *xml_ret; |
| 37 | |
| 38 | g_dbus_interface_info_generate_xml(spect_data->interfaces[0], (guint)0, xml_ret); |
| 39 | xml_ret_gvar = g_variant_new_string(xml_ret->str); |
| 40 | g_dbus_method_invocation_return_value(invc, xml_ret_gvar); |
| 41 | } |
| 42 | |
| 43 | } |
| 44 | */ |
| 45 | |
| 46 | /* handled by codegen |
| 47 | static GVariant * handle_get_property(GDBusConnection *conn, |
| 48 | const gchar *sender, |
| 49 | const gchar *obj_path, |
| 50 | const gchar *interf_name, |
| 51 | const gchar *prop_name, |
| 52 | GError **err, |
| 53 | gpointer usr_data) { |
| 54 | |
| 55 | const gchar *our_interf_name = "org.freedesktop.hostname1"; |
| 56 | const gchar *our_obj_path = "/org/freedesktop/hostname1"; |
| 57 | |
| 58 | if(g_strcmp0(interf_name, our_interf_name) != 0 |
| 59 | || g_strcmp0(obj_path, our_obj_path) != 0) { |
| 60 | |
| 61 | return NULL; //TODO error |
| 62 | } |
| 63 | |
| 64 | if(g_strcmp0(prop_name, "Hostname") == 0) |
| 65 | return g_variant_new_string(hostname); |
| 66 | |
| 67 | else if(g_strcmp0(prop_name, "StaticHostname") == 0) |
| 68 | return g_variant_new_string(static_hostname); |
| 69 | |
| 70 | else if(g_strcmp0(prop_name, "PrettyHostname") == 0) |
| 71 | return g_variant_new_string(pretty_hostname); |
| 72 | |
| 73 | else if(g_strcmp0(prop_name, "IconName") == 0) |
| 74 | return g_variant_new_string(icon_name); |
| 75 | |
| 76 | else |
| 77 | return NULL; //TODO error |
| 78 | |
| 79 | } |
| 80 | */ |
| 81 | |
| 82 | /* handled by codegen |
| 83 | static gboolean handle_set_property(GDBusConnection *conn, |
| 84 | const gchar *sender, |
| 85 | const gchar *obj_path, |
| 86 | const gchar *interf_name, |
| 87 | const gchar *prop_name, |
| 88 | GVariant *val, |
| 89 | GError **err, |
| 90 | gpointer usr_data) { |
| 91 | |
| 92 | g_dbus_connection_emit_signal(conn, |
| 93 | NULL, |
| 94 | obj_path, |
| 95 | "org.freedesktop.DBus.Properties", |
| 96 | "PropertiesChanged", |
| 97 | NULL, //incorrect |
| 98 | NULL); |
| 99 | |
| 100 | return TRUE; |
| 101 | } |
| 102 | */ |
| 103 | |
| 104 | /* handled by codegen |
| 105 | static const GDBusInterfaceVTable interface_vtable = |
| 106 | { |
| 107 | handle_method_call, |
| 108 | handle_get_property, |
| 109 | handle_set_property |
| 110 | }; |
| 111 | |
| 112 | */ |
| 113 | |
| 114 | static void on_bus_acquired(GDBusConnection *conn, |
| 115 | const gchar *name, |
| 116 | gpointer user_data) { |
| 117 | |
| 118 | GError *err; |
| 119 | |
| 120 | g_print("got bus, name: %s\n", name); |
| 121 | |
| 122 | //GDBusObjectSkeleton *hostnamed_dbobj = g_dbus_object_skeleton_new("/org/freedesktop/hostname1"); |
| 123 | |
| 124 | g_dbus_connection_register_object(conn, |
| 125 | "/org/freedesktop/hostname1", |
| 126 | spect_data->interfaces[0], |
| 127 | &interface_vtable, |
| 128 | NULL, NULL, NULL); |
| 129 | } |
| 130 | |
| 131 | static void on_name_acquired(GDBusConnection *conn, |
| 132 | const gchar *name, |
| 133 | gpointer user_data) { |
| 134 | |
| 135 | g_print("got name %s\n", name); |
| 136 | } |
| 137 | |
| 138 | static void on_name_lost(GDBusConnection *conn, |
| 139 | const gchar *name, |
| 140 | gpointer user_data) { |
| 141 | |
| 142 | g_print("lost name %s, exiting...", name); |
| 143 | //TODO exit through g_main_loop properly... |
| 144 | exit(0); |
| 145 | } |
| 146 | |
| 147 | /* safe call to try and start hostnamed */ |
| 148 | GError * hostnamed_init() { |
| 149 | |
| 150 | guint bus_descriptor; |
| 151 | GError *err = NULL; |
| 152 | gchar **hostnamed_ispect_xml = g_malloc(3000); |
| 153 | gchar *hostnamed_joined_xml = g_malloc(3000); |
| 154 | |
| 155 | g_file_get_contents("conf/hostnamed-ispect.xml", hostnamed_ispect_xml, NULL, err); |
| 156 | hostnamed_joined_xml = g_strjoinv("\n", hostnamed_ispect_xml); |
| 157 | spect_data = g_dbus_node_info_new_for_xml(hostnamed_joined_xml, NULL); |
| 158 | |
| 159 | if(!init_props()) |
| 160 | return err; //TODO error |
| 161 | |
| 162 | bus_descriptor = g_bus_own_name(G_BUS_TYPE_SYSTEM, |
| 163 | "org.freedesktop.hostname1", |
| 164 | G_BUS_NAME_OWNER_FLAGS_NONE, |
| 165 | on_bus_acquired, |
| 166 | on_name_acquired, |
| 167 | on_name_lost, |
| 168 | NULL, |
| 169 | NULL); |
| 170 | |
| 171 | //TODO: malloc and return reference as if a main() closed |
| 172 | return err; |
| 173 | } |
| 174 | /* handled by codegen |
| 175 | gboolean init_props() { |
| 176 | |
| 177 | if(init_hostname() |
| 178 | && init_static_hostname() |
| 179 | && init_pretty_hostname() |
| 180 | && init_icon_name() |
| 181 | && init_chassis() |
| 182 | && init_kernel_name() |
| 183 | && init_kernel_version() |
| 184 | && init_os_name() |
| 185 | && init_os_cpe() ) |
| 186 | return TRUE; |
| 187 | |
| 188 | return FALSE; |
| 189 | } |
| 190 | |
| 191 | //POSIX, for future ports try_hostname should be checked for null-termination |
| 192 | gboolean init_hostname() { |
| 193 | |
| 194 | gchar try_hostname[HOST_NAME_MAX]; |
| 195 | |
| 196 | if(!gethostname(try_hostname, HOST_NAME_MAX)) { |
| 197 | hostname = try_hostname; |
| 198 | return TRUE; |
| 199 | } |
| 200 | |
| 201 | return FALSE; |
| 202 | } |
| 203 | */ |
| 204 | |
| 205 | /* handled by codegen |
| 206 | gboolean init_static_hostname() { |
| 207 | //TODO |
| 208 | return TRUE; |
| 209 | } |
| 210 | |
| 211 | gboolean init_pretty_hostname() { |
| 212 | //TODO |
| 213 | return TRUE; |
| 214 | } |
| 215 | |
| 216 | gboolean init_icon_name() { |
| 217 | //TODO |
| 218 | return TRUE; |
| 219 | } |
| 220 | |
| 221 | gboolean init_chassis() { |
| 222 | //TODO |
| 223 | return TRUE; |
| 224 | } |
| 225 | |
| 226 | gboolean init_kernel_name() { |
| 227 | //TODO |
| 228 | return TRUE; |
| 229 | } |
| 230 | |
| 231 | gboolean init_kernel_version() { |
| 232 | //TODO |
| 233 | return TRUE; |
| 234 | } |
| 235 | |
| 236 | gboolean init_os_name() { |
| 237 | //TODO |
| 238 | return TRUE; |
| 239 | } |
| 240 | |
| 241 | gboolean init_os_cpe() { |
| 242 | //TODO |
| 243 | return TRUE; |
| 244 | } |
| 245 | |
| 246 | //TODO figure out DMI variables on obsd |
| 247 | /*static gchar *guess_icon_name() { |
| 248 | |
| 249 | gchar *filebuf = NULL; |
| 250 | gchar *ret = NULL; |
| 251 | |
| 252 | //TODO vm check |
| 253 | |
| 254 | #if defined(__i386__) || defined(__x86_64__) |
| 255 | /* |
| 256 | Taken with a few minor changes from systemd's hostnamed.c, |
| 257 | copyright 2011 Lennart Poettering. |
| 258 | |
| 259 | See the SMBIOS Specification 2.7.1 section 7.4.1 for |
| 260 | details about the values listed here: |
| 261 | |
| 262 | http://www.dmtf.org/sites/default/files/standards/documents/DSP0134_2.7.1.pdf |
| 263 | */ /* |
| 264 | |
| 265 | if (g_file_get_contents ("/sys/class/dmi/id/chassis_type", &filebuf, NULL, NULL)) { |
| 266 | switch (g_ascii_strtoull (filebuf, NULL, 10)) { |
| 267 | case 0x3: |
| 268 | case 0x4: |
| 269 | case 0x5: |
| 270 | case 0x6: |
| 271 | case 0x7: |
| 272 | ret = g_strdup ("computer-desktop"); |
| 273 | goto out; |
| 274 | case 0x9: |
| 275 | case 0xA: |
| 276 | case 0xE: |
| 277 | ret = g_strdup ("computer-laptop"); |
| 278 | goto out; |
| 279 | case 0x11: |
| 280 | case 0x17: |
| 281 | case 0x1C: |
| 282 | case 0x1D: |
| 283 | ret = g_strdup ("computer-server"); |
| 284 | goto out; |
| 285 | } |
| 286 | } |
| 287 | #endif |
| 288 | ret = g_strdup ("computer"); |
| 289 | out: |
| 290 | g_free (filebuf); |
| 291 | return ret; |
| 292 | }*/ |