71e3eef1 |
1 | #include <unistd.h> |
2 | #include <limits.h> |
ea207ed3 |
3 | #include <gio/gio.h> |
4 | |
ea87dc10 |
5 | #include "src/interfaces/hostnamed/hostnamed-gen.c" |
71e3eef1 |
6 | |
ff1030d4 |
7 | GDBusNodeInfo *spect_data; |
0df0018d |
8 | |
ea87dc10 |
9 | /* handled by codegen |
e2581ee4 |
10 | static gchar *hostname; |
11 | static gchar *pretty_hostname; |
12 | static gchar *static_hostname; |
13 | static gchar *icon_name; |
71e3eef1 |
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 |
ea87dc10 |
20 | */ |
e2581ee4 |
21 | |
ea87dc10 |
22 | /* handled by codegen |
780e1f19 |
23 | static void handle_method_call(GDBusConnection *conn, |
80043b36 |
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) { |
780e1f19 |
31 | |
71e3eef1 |
32 | if(g_strcmp0(interf_name, "org.freedesktop.DBus.Introspectable") == 0 |
33 | && g_strcmp0(method_name, "Introspect") == 0) { |
e2581ee4 |
34 | |
35 | GVariant *xml_ret_gvar; |
36 | GString *xml_ret; |
ff1030d4 |
37 | |
e2581ee4 |
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 | } |
780e1f19 |
42 | |
a622ef09 |
43 | } |
ea87dc10 |
44 | */ |
780e1f19 |
45 | |
ea87dc10 |
46 | /* handled by codegen |
780e1f19 |
47 | static GVariant * handle_get_property(GDBusConnection *conn, |
80043b36 |
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) { |
71e3eef1 |
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); |
780e1f19 |
69 | |
71e3eef1 |
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 | |
780e1f19 |
79 | } |
ea87dc10 |
80 | */ |
780e1f19 |
81 | |
ea87dc10 |
82 | /* handled by codegen |
780e1f19 |
83 | static gboolean handle_set_property(GDBusConnection *conn, |
80043b36 |
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", |
ea87dc10 |
97 | NULL, //incorrect |
80043b36 |
98 | NULL); |
99 | |
100 | return TRUE; |
780e1f19 |
101 | } |
ea87dc10 |
102 | */ |
387173cb |
103 | |
ea87dc10 |
104 | /* handled by codegen |
a622ef09 |
105 | static const GDBusInterfaceVTable interface_vtable = |
106 | { |
80043b36 |
107 | handle_method_call, |
108 | handle_get_property, |
109 | handle_set_property |
a622ef09 |
110 | }; |
111 | |
ea87dc10 |
112 | */ |
113 | |
80043b36 |
114 | static void on_bus_acquired(GDBusConnection *conn, |
115 | const gchar *name, |
116 | gpointer user_data) { |
11475670 |
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); |
ea207ed3 |
129 | } |
130 | |
80043b36 |
131 | static void on_name_acquired(GDBusConnection *conn, |
132 | const gchar *name, |
133 | gpointer user_data) { |
134 | |
ea207ed3 |
135 | g_print("got name %s\n", name); |
ea207ed3 |
136 | } |
137 | |
80043b36 |
138 | static void on_name_lost(GDBusConnection *conn, |
139 | const gchar *name, |
140 | gpointer user_data) { |
141 | |
f4d34761 |
142 | g_print("lost name %s, exiting...", name); |
e8c57a5d |
143 | //TODO exit through g_main_loop properly... |
144 | exit(0); |
ea207ed3 |
145 | } |
146 | |
0df0018d |
147 | /* safe call to try and start hostnamed */ |
6de394d4 |
148 | GError * hostnamed_init() { |
387173cb |
149 | |
ea207ed3 |
150 | guint bus_descriptor; |
11475670 |
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); |
71e3eef1 |
158 | |
159 | if(!init_props()) |
160 | return err; //TODO error |
e8c57a5d |
161 | |
6de394d4 |
162 | bus_descriptor = g_bus_own_name(G_BUS_TYPE_SYSTEM, |
11475670 |
163 | "org.freedesktop.hostname1", |
ea207ed3 |
164 | G_BUS_NAME_OWNER_FLAGS_NONE, |
165 | on_bus_acquired, |
166 | on_name_acquired, |
167 | on_name_lost, |
168 | NULL, |
169 | NULL); |
496f5d66 |
170 | |
6de394d4 |
171 | //TODO: malloc and return reference as if a main() closed |
172 | return err; |
780e1f19 |
173 | } |
ea87dc10 |
174 | /* handled by codegen |
1c38000a |
175 | gboolean init_props() { |
71e3eef1 |
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 |
1c38000a |
192 | gboolean init_hostname() { |
71e3eef1 |
193 | |
1c38000a |
194 | gchar try_hostname[HOST_NAME_MAX]; |
71e3eef1 |
195 | |
1c38000a |
196 | if(!gethostname(try_hostname, HOST_NAME_MAX)) { |
71e3eef1 |
197 | hostname = try_hostname; |
198 | return TRUE; |
199 | } |
200 | |
201 | return FALSE; |
202 | } |
ea87dc10 |
203 | */ |
71e3eef1 |
204 | |
ea87dc10 |
205 | /* handled by codegen |
1c38000a |
206 | gboolean init_static_hostname() { |
207 | //TODO |
208 | return TRUE; |
209 | } |
71e3eef1 |
210 | |
1c38000a |
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 | } |
71e3eef1 |
225 | |
1c38000a |
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; |
71e3eef1 |
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 | }*/ |