71e3eef1 |
1 | #include <unistd.h> |
2 | #include <limits.h> |
ed99526e |
3 | |
4 | #include <glib.h> |
ea207ed3 |
5 | #include <gio/gio.h> |
6 | |
ed99526e |
7 | #include "hostnamed.h" |
8 | #include "hostnamed-gen.c" |
71e3eef1 |
9 | |
ed99526e |
10 | GPtrArray *hostnamed_freeable; |
ff1030d4 |
11 | GDBusNodeInfo *spect_data; |
0df0018d |
12 | |
80043b36 |
13 | static void on_bus_acquired(GDBusConnection *conn, |
14 | const gchar *name, |
15 | gpointer user_data) { |
11475670 |
16 | |
17 | GError *err; |
18 | |
19 | g_print("got bus, name: %s\n", name); |
20 | |
ed99526e |
21 | /*g_dbus_connection_register_object(conn, |
11475670 |
22 | "/org/freedesktop/hostname1", |
23 | spect_data->interfaces[0], |
24 | &interface_vtable, |
ed99526e |
25 | NULL, NULL, NULL);*/ |
ea207ed3 |
26 | } |
27 | |
80043b36 |
28 | static void on_name_acquired(GDBusConnection *conn, |
29 | const gchar *name, |
30 | gpointer user_data) { |
31 | |
ea207ed3 |
32 | g_print("got name %s\n", name); |
ea207ed3 |
33 | } |
34 | |
80043b36 |
35 | static void on_name_lost(GDBusConnection *conn, |
36 | const gchar *name, |
37 | gpointer user_data) { |
38 | |
f4d34761 |
39 | g_print("lost name %s, exiting...", name); |
ed99526e |
40 | |
41 | hostnamed_mem_clean(); |
42 | |
e8c57a5d |
43 | //TODO exit through g_main_loop properly... |
ea207ed3 |
44 | } |
45 | |
0df0018d |
46 | /* safe call to try and start hostnamed */ |
6de394d4 |
47 | GError * hostnamed_init() { |
387173cb |
48 | |
ea207ed3 |
49 | guint bus_descriptor; |
11475670 |
50 | GError *err = NULL; |
ed99526e |
51 | gchar **hostnamed_ispect_xml; |
52 | gchar *hostnamed_joined_xml; |
11475670 |
53 | |
ed99526e |
54 | hostnamed_freeable = g_ptr_array_new(); |
55 | |
56 | g_file_get_contents("conf/hostnamed-ispect.xml", hostnamed_ispect_xml, GUINT_TO_POINTER(3000), &err); |
11475670 |
57 | hostnamed_joined_xml = g_strjoinv("\n", hostnamed_ispect_xml); |
58 | spect_data = g_dbus_node_info_new_for_xml(hostnamed_joined_xml, NULL); |
71e3eef1 |
59 | |
ed99526e |
60 | g_free(hostnamed_ispect_xml); |
61 | g_ptr_array_add(hostnamed_freeable, hostnamed_joined_xml); |
62 | |
6de394d4 |
63 | bus_descriptor = g_bus_own_name(G_BUS_TYPE_SYSTEM, |
11475670 |
64 | "org.freedesktop.hostname1", |
ea207ed3 |
65 | G_BUS_NAME_OWNER_FLAGS_NONE, |
66 | on_bus_acquired, |
67 | on_name_acquired, |
68 | on_name_lost, |
69 | NULL, |
70 | NULL); |
496f5d66 |
71 | |
6de394d4 |
72 | //TODO: malloc and return reference as if a main() closed |
73 | return err; |
780e1f19 |
74 | } |
71e3eef1 |
75 | |
76 | //POSIX, for future ports try_hostname should be checked for null-termination |
ed99526e |
77 | /* |
1c38000a |
78 | gboolean init_hostname() { |
71e3eef1 |
79 | |
1c38000a |
80 | gchar try_hostname[HOST_NAME_MAX]; |
71e3eef1 |
81 | |
1c38000a |
82 | if(!gethostname(try_hostname, HOST_NAME_MAX)) { |
71e3eef1 |
83 | hostname = try_hostname; |
84 | return TRUE; |
85 | } |
86 | |
87 | return FALSE; |
ed99526e |
88 | }*/ |
1c38000a |
89 | |
ed99526e |
90 | /* free()'s */ |
91 | void hostnamed_mem_clean() { |
1c38000a |
92 | |
ed99526e |
93 | g_ptr_array_foreach(hostnamed_freeable, (GFunc) g_free, NULL); |
71e3eef1 |
94 | } |
95 | |
96 | //TODO figure out DMI variables on obsd |
97 | /*static gchar *guess_icon_name() { |
98 | |
99 | gchar *filebuf = NULL; |
100 | gchar *ret = NULL; |
101 | |
102 | //TODO vm check |
103 | |
104 | #if defined(__i386__) || defined(__x86_64__) |
105 | /* |
106 | Taken with a few minor changes from systemd's hostnamed.c, |
107 | copyright 2011 Lennart Poettering. |
108 | |
109 | See the SMBIOS Specification 2.7.1 section 7.4.1 for |
110 | details about the values listed here: |
111 | |
112 | http://www.dmtf.org/sites/default/files/standards/documents/DSP0134_2.7.1.pdf |
113 | */ /* |
114 | |
115 | if (g_file_get_contents ("/sys/class/dmi/id/chassis_type", &filebuf, NULL, NULL)) { |
116 | switch (g_ascii_strtoull (filebuf, NULL, 10)) { |
117 | case 0x3: |
118 | case 0x4: |
119 | case 0x5: |
120 | case 0x6: |
121 | case 0x7: |
122 | ret = g_strdup ("computer-desktop"); |
123 | goto out; |
124 | case 0x9: |
125 | case 0xA: |
126 | case 0xE: |
127 | ret = g_strdup ("computer-laptop"); |
128 | goto out; |
129 | case 0x11: |
130 | case 0x17: |
131 | case 0x1C: |
132 | case 0x1D: |
133 | ret = g_strdup ("computer-server"); |
134 | goto out; |
135 | } |
136 | } |
137 | #endif |
138 | ret = g_strdup ("computer"); |
139 | out: |
140 | g_free (filebuf); |
141 | return ret; |
142 | }*/ |