baa40e28 |
1 | /* |
2 | * Copyright (c) 2014 Ian Sutton <ian@kremlin.cc> |
3 | * |
4 | * Permission to use, copy, modify, and distribute this software for any |
5 | * purpose with or without fee is hereby granted, provided that the above |
6 | * copyright notice and this permission notice appear in all copies. |
7 | * |
8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
15 | */ |
16 | |
71e3eef1 |
17 | #include <unistd.h> |
18 | #include <limits.h> |
ed99526e |
19 | |
043bd2ed |
20 | #include <sys/param.h> |
8f3d38b1 |
21 | #include <string.h> |
043bd2ed |
22 | |
cd16a588 |
23 | #include <glib/gprintf.h> |
24 | |
3c3794ac |
25 | #include "hostnamed-gen.h" |
71e3eef1 |
26 | |
ed99526e |
27 | GPtrArray *hostnamed_freeable; |
9d2500b0 |
28 | Hostname1 *hostnamed_interf; |
7ce16a35 |
29 | |
af9f8b50 |
30 | /* --- begin method/property/signal code --- */ |
355f60ae |
31 | |
7ce16a35 |
32 | static gboolean |
9d2500b0 |
33 | on_handle_set_hostname(Hostname1 *hn1_passed_interf, |
7ce16a35 |
34 | GDBusMethodInvocation *invoc, |
35 | const gchar *greet, |
36 | gpointer data) { |
37 | return FALSE; |
38 | } |
39 | |
40 | static gboolean |
9d2500b0 |
41 | on_handle_set_static_hostname(Hostname1 *hn1_passed_interf, |
7ce16a35 |
42 | GDBusMethodInvocation *invoc, |
43 | const gchar *greet, |
44 | gpointer data) { |
45 | return FALSE; |
46 | } |
47 | |
48 | static gboolean |
9d2500b0 |
49 | on_handle_set_pretty_hostname(Hostname1 *hn1_passed_interf, |
7ce16a35 |
50 | GDBusMethodInvocation *invoc, |
51 | const gchar *greet, |
52 | gpointer data) { |
53 | return FALSE; |
54 | } |
55 | |
56 | static gboolean |
9d2500b0 |
57 | on_handle_set_chassis(Hostname1 *hn1_passed_interf, |
7ce16a35 |
58 | GDBusMethodInvocation *invoc, |
59 | const gchar *greet, |
60 | gpointer data) { |
61 | return FALSE; |
62 | } |
63 | |
64 | static gboolean |
9d2500b0 |
65 | on_handle_set_icon_name(Hostname1 *hn1_passed_interf, |
7ce16a35 |
66 | GDBusMethodInvocation *invoc, |
67 | const gchar *greet, |
68 | gpointer data) { |
69 | return FALSE; |
70 | } |
71 | |
355f60ae |
72 | /* note: all hostnamed/hostname1's properties are read-only, |
73 | * and do not need set_ functions, gdbus-codegen realized |
74 | * this from the XML and handled the to-be error of trying |
75 | * to set a read-only property's value |
76 | */ |
77 | |
78 | const gchar * |
79 | our_get_hostname() { |
80 | |
8f3d38b1 |
81 | gchar *hostname_buf, *ret; |
82 | size_t hostname_divider; |
043bd2ed |
83 | |
84 | hostname_buf = (gchar*) g_malloc0(MAXHOSTNAMELEN); |
8f3d38b1 |
85 | ret = (gchar*) g_malloc0(MAXHOSTNAMELEN); |
af9f8b50 |
86 | g_ptr_array_add(hostnamed_freeable, hostname_buf); |
8f3d38b1 |
87 | g_ptr_array_add(hostnamed_freeable, ret); |
af9f8b50 |
88 | |
89 | if(gethostname(hostname_buf, MAXHOSTNAMELEN)) |
90 | return ""; |
043bd2ed |
91 | |
8f3d38b1 |
92 | hostname_divider = strcspn(hostname_buf, "."); |
93 | |
94 | return strncpy(ret, hostname_buf, hostname_divider); |
355f60ae |
95 | } |
96 | |
97 | const gchar * |
98 | our_get_static_hostname() { |
99 | |
100 | return "TODO"; |
101 | } |
102 | |
103 | const gchar * |
104 | our_get_pretty_hostname() { |
105 | |
106 | return "TODO"; |
107 | } |
108 | |
109 | const gchar * |
110 | our_get_chassis() { |
111 | |
112 | return "TODO"; |
113 | } |
114 | |
115 | const gchar * |
116 | our_get_icon_name() { |
117 | |
118 | return "TODO"; |
119 | } |
120 | |
121 | const gchar * |
122 | our_get_kernel_name() { |
123 | |
124 | return "TODO"; |
125 | } |
126 | |
127 | const gchar * |
128 | our_get_kernel_version() { |
129 | |
130 | return "TODO"; |
131 | } |
132 | |
133 | const gchar * |
134 | our_get_kernel_release() { |
135 | |
136 | return "TODO"; |
137 | } |
138 | |
139 | const gchar * |
140 | our_get_os_cpename() { |
141 | |
142 | return "TODO"; |
143 | } |
144 | |
145 | const gchar * |
146 | our_get_os_pretty_name() { |
147 | |
148 | return "TODO"; |
149 | } |
150 | |
af9f8b50 |
151 | /* --- end method/property/signal code, begin bus/name handlers --- */ |
0df0018d |
152 | |
828caf9a |
153 | static void hostnamed_on_bus_acquired(GDBusConnection *conn, |
fbc0295f |
154 | const gchar *name, |
155 | gpointer user_data) { |
11475670 |
156 | |
fbc0295f |
157 | g_print("got bus, name: %s\n", name); |
11475670 |
158 | |
ea207ed3 |
159 | } |
160 | |
828caf9a |
161 | static void hostnamed_on_name_acquired(GDBusConnection *conn, |
9d2500b0 |
162 | const gchar *name, |
163 | gpointer user_data) { |
80043b36 |
164 | |
7ce16a35 |
165 | g_print("got '%s' on system bus\n", name); |
166 | |
9d2500b0 |
167 | hostnamed_interf = hostname1_skeleton_new(); |
7ce16a35 |
168 | |
355f60ae |
169 | /* attach function pointers to generated struct's method handlers */ |
7ce16a35 |
170 | g_signal_connect(hostnamed_interf, "handle-set-hostname", G_CALLBACK(on_handle_set_hostname), NULL); |
171 | g_signal_connect(hostnamed_interf, "handle-set-static-hostname", G_CALLBACK(on_handle_set_static_hostname), NULL); |
172 | g_signal_connect(hostnamed_interf, "handle-set-pretty-hostname", G_CALLBACK(on_handle_set_pretty_hostname), NULL); |
173 | g_signal_connect(hostnamed_interf, "handle-set-chassis", G_CALLBACK(on_handle_set_chassis), NULL); |
174 | g_signal_connect(hostnamed_interf, "handle-set-icon-name", G_CALLBACK(on_handle_set_icon_name), NULL); |
175 | |
355f60ae |
176 | /* set our properties before export */ |
9d2500b0 |
177 | hostname1_set_hostname(hostnamed_interf, our_get_hostname()); |
178 | hostname1_set_static_hostname(hostnamed_interf, our_get_static_hostname()); |
179 | hostname1_set_pretty_hostname(hostnamed_interf, our_get_pretty_hostname()); |
180 | hostname1_set_chassis(hostnamed_interf, our_get_chassis()); |
181 | hostname1_set_icon_name(hostnamed_interf, our_get_icon_name()); |
182 | hostname1_set_kernel_name(hostnamed_interf, our_get_kernel_name()); |
183 | hostname1_set_kernel_version(hostnamed_interf, our_get_kernel_version()); |
184 | hostname1_set_kernel_release(hostnamed_interf, our_get_kernel_release()); |
185 | hostname1_set_operating_system_cpename(hostnamed_interf, our_get_os_cpename()); |
186 | hostname1_set_operating_system_pretty_name(hostnamed_interf, our_get_os_pretty_name()); |
355f60ae |
187 | |
7ce16a35 |
188 | if(!g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(hostnamed_interf), |
9d2500b0 |
189 | conn, |
190 | "/org/freedesktop/hostname1", |
191 | NULL)) { |
7ce16a35 |
192 | |
9d2500b0 |
193 | g_printf("Failed to export Hostname1's interface!"); |
7ce16a35 |
194 | } |
195 | |
ea207ed3 |
196 | } |
197 | |
af9f8b50 |
198 | /* --- end bus/name handlers, begin misc functions --- */ |
199 | |
3c3794ac |
200 | /* free()'s */ |
201 | void hostnamed_mem_clean() { |
202 | |
a3041e78 |
203 | g_ptr_array_foreach(hostnamed_freeable, (GFunc) g_free, NULL); |
204 | g_ptr_array_free(hostnamed_freeable, TRUE); |
3c3794ac |
205 | } |
206 | |
828caf9a |
207 | static void hostnamed_on_name_lost(GDBusConnection *conn, |
9d2500b0 |
208 | const gchar *name, |
209 | gpointer user_data) { |
80043b36 |
210 | |
fbc0295f |
211 | g_print("lost name %s, exiting...", name); |
ed99526e |
212 | |
fbc0295f |
213 | hostnamed_mem_clean(); |
7ce16a35 |
214 | g_dbus_interface_skeleton_unexport(G_DBUS_INTERFACE_SKELETON(hostnamed_interf)); |
ed99526e |
215 | |
ea207ed3 |
216 | } |
217 | |
a3041e78 |
218 | int main() { |
219 | |
220 | guint bus_descriptor; |
221 | GMainLoop *hostnamed_loop; |
387173cb |
222 | |
8f3d38b1 |
223 | hostnamed_loop = g_main_loop_new(NULL, TRUE); |
224 | hostnamed_freeable = g_ptr_array_new(); |
ed99526e |
225 | |
a3041e78 |
226 | bus_descriptor = g_bus_own_name(G_BUS_TYPE_SYSTEM, |
fbc0295f |
227 | "org.freedesktop.hostname1", |
228 | G_BUS_NAME_OWNER_FLAGS_NONE, |
828caf9a |
229 | hostnamed_on_bus_acquired, |
230 | hostnamed_on_name_acquired, |
231 | hostnamed_on_name_lost, |
fbc0295f |
232 | NULL, |
233 | NULL); |
496f5d66 |
234 | |
3c3794ac |
235 | g_main_loop_run(hostnamed_loop); |
236 | g_main_loop_unref(hostnamed_loop); |
237 | |
a3041e78 |
238 | g_bus_unown_name(bus_descriptor); |
239 | |
af9f8b50 |
240 | hostnamed_mem_clean(); |
241 | |
3c3794ac |
242 | return 0; |
71e3eef1 |
243 | } |
244 | |
6981850a |
245 | /* TODO figure out DMI variables on obsd */ |
71e3eef1 |
246 | /*static gchar *guess_icon_name() { |
247 | |
248 | gchar *filebuf = NULL; |
249 | gchar *ret = NULL; |
250 | |
fbc0295f |
251 | #if defined(__i386__) || defined(__x86_64__) |
c3b84b0a |
252 | |
71e3eef1 |
253 | Taken with a few minor changes from systemd's hostnamed.c, |
254 | copyright 2011 Lennart Poettering. |
255 | |
256 | See the SMBIOS Specification 2.7.1 section 7.4.1 for |
257 | details about the values listed here: |
258 | |
259 | http://www.dmtf.org/sites/default/files/standards/documents/DSP0134_2.7.1.pdf |
c3b84b0a |
260 | |
71e3eef1 |
261 | |
262 | if (g_file_get_contents ("/sys/class/dmi/id/chassis_type", &filebuf, NULL, NULL)) { |
263 | switch (g_ascii_strtoull (filebuf, NULL, 10)) { |
264 | case 0x3: |
265 | case 0x4: |
266 | case 0x5: |
267 | case 0x6: |
268 | case 0x7: |
269 | ret = g_strdup ("computer-desktop"); |
270 | goto out; |
271 | case 0x9: |
272 | case 0xA: |
273 | case 0xE: |
274 | ret = g_strdup ("computer-laptop"); |
275 | goto out; |
276 | case 0x11: |
277 | case 0x17: |
278 | case 0x1C: |
279 | case 0x1D: |
280 | ret = g_strdup ("computer-server"); |
281 | goto out; |
282 | } |
283 | } |
fbc0295f |
284 | #endif |
71e3eef1 |
285 | ret = g_strdup ("computer"); |
286 | out: |
287 | g_free (filebuf); |
288 | return ret; |
289 | }*/ |
3c3794ac |
290 | |