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