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