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> |
21 | |
cd16a588 |
22 | #include <glib/gprintf.h> |
23 | |
3c3794ac |
24 | #include "hostnamed-gen.h" |
71e3eef1 |
25 | |
ed99526e |
26 | GPtrArray *hostnamed_freeable; |
ff1030d4 |
27 | GDBusNodeInfo *spect_data; |
9d2500b0 |
28 | Hostname1 *hostnamed_interf; |
7ce16a35 |
29 | |
355f60ae |
30 | /* begin method/property/signal code */ |
31 | |
32 | /* TODO make sure these guys only work if called by root */ |
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 | |
043bd2ed |
82 | int hostname_try; |
83 | gchar *hostname_buf; |
84 | |
85 | hostname_buf = (gchar*) g_malloc0(MAXHOSTNAMELEN); |
86 | hostname_try = gethostname(hostname_buf, MAXHOSTNAMELEN); |
87 | |
88 | return hostname_buf; |
355f60ae |
89 | } |
90 | |
91 | const gchar * |
92 | our_get_static_hostname() { |
93 | |
94 | return "TODO"; |
95 | } |
96 | |
97 | const gchar * |
98 | our_get_pretty_hostname() { |
99 | |
100 | return "TODO"; |
101 | } |
102 | |
103 | const gchar * |
104 | our_get_chassis() { |
105 | |
106 | return "TODO"; |
107 | } |
108 | |
109 | const gchar * |
110 | our_get_icon_name() { |
111 | |
112 | return "TODO"; |
113 | } |
114 | |
115 | const gchar * |
116 | our_get_kernel_name() { |
117 | |
118 | return "TODO"; |
119 | } |
120 | |
121 | const gchar * |
122 | our_get_kernel_version() { |
123 | |
124 | return "TODO"; |
125 | } |
126 | |
127 | const gchar * |
128 | our_get_kernel_release() { |
129 | |
130 | return "TODO"; |
131 | } |
132 | |
133 | const gchar * |
134 | our_get_os_cpename() { |
135 | |
136 | return "TODO"; |
137 | } |
138 | |
139 | const gchar * |
140 | our_get_os_pretty_name() { |
141 | |
142 | return "TODO"; |
143 | } |
144 | |
7ce16a35 |
145 | /* end method/property/signal code, begin bus/name handlers */ |
0df0018d |
146 | |
828caf9a |
147 | static void hostnamed_on_bus_acquired(GDBusConnection *conn, |
fbc0295f |
148 | const gchar *name, |
149 | gpointer user_data) { |
11475670 |
150 | |
fbc0295f |
151 | g_print("got bus, name: %s\n", name); |
11475670 |
152 | |
ea207ed3 |
153 | } |
154 | |
828caf9a |
155 | static void hostnamed_on_name_acquired(GDBusConnection *conn, |
9d2500b0 |
156 | const gchar *name, |
157 | gpointer user_data) { |
80043b36 |
158 | |
7ce16a35 |
159 | g_print("got '%s' on system bus\n", name); |
160 | |
9d2500b0 |
161 | hostnamed_interf = hostname1_skeleton_new(); |
7ce16a35 |
162 | |
355f60ae |
163 | /* attach function pointers to generated struct's method handlers */ |
7ce16a35 |
164 | g_signal_connect(hostnamed_interf, "handle-set-hostname", G_CALLBACK(on_handle_set_hostname), NULL); |
165 | g_signal_connect(hostnamed_interf, "handle-set-static-hostname", G_CALLBACK(on_handle_set_static_hostname), NULL); |
166 | g_signal_connect(hostnamed_interf, "handle-set-pretty-hostname", G_CALLBACK(on_handle_set_pretty_hostname), NULL); |
167 | g_signal_connect(hostnamed_interf, "handle-set-chassis", G_CALLBACK(on_handle_set_chassis), NULL); |
168 | g_signal_connect(hostnamed_interf, "handle-set-icon-name", G_CALLBACK(on_handle_set_icon_name), NULL); |
169 | |
355f60ae |
170 | /* set our properties before export */ |
9d2500b0 |
171 | hostname1_set_hostname(hostnamed_interf, our_get_hostname()); |
172 | hostname1_set_static_hostname(hostnamed_interf, our_get_static_hostname()); |
173 | hostname1_set_pretty_hostname(hostnamed_interf, our_get_pretty_hostname()); |
174 | hostname1_set_chassis(hostnamed_interf, our_get_chassis()); |
175 | hostname1_set_icon_name(hostnamed_interf, our_get_icon_name()); |
176 | hostname1_set_kernel_name(hostnamed_interf, our_get_kernel_name()); |
177 | hostname1_set_kernel_version(hostnamed_interf, our_get_kernel_version()); |
178 | hostname1_set_kernel_release(hostnamed_interf, our_get_kernel_release()); |
179 | hostname1_set_operating_system_cpename(hostnamed_interf, our_get_os_cpename()); |
180 | hostname1_set_operating_system_pretty_name(hostnamed_interf, our_get_os_pretty_name()); |
355f60ae |
181 | |
7ce16a35 |
182 | if(!g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(hostnamed_interf), |
9d2500b0 |
183 | conn, |
184 | "/org/freedesktop/hostname1", |
185 | NULL)) { |
7ce16a35 |
186 | |
9d2500b0 |
187 | g_printf("Failed to export Hostname1's interface!"); |
7ce16a35 |
188 | } |
189 | |
ea207ed3 |
190 | } |
191 | |
3c3794ac |
192 | /* free()'s */ |
193 | void hostnamed_mem_clean() { |
194 | |
195 | g_ptr_array_foreach(hostnamed_freeable, (GFunc) g_free, NULL); |
196 | } |
197 | |
828caf9a |
198 | static void hostnamed_on_name_lost(GDBusConnection *conn, |
9d2500b0 |
199 | const gchar *name, |
200 | gpointer user_data) { |
80043b36 |
201 | |
fbc0295f |
202 | g_print("lost name %s, exiting...", name); |
ed99526e |
203 | |
fbc0295f |
204 | hostnamed_mem_clean(); |
7ce16a35 |
205 | g_dbus_interface_skeleton_unexport(G_DBUS_INTERFACE_SKELETON(hostnamed_interf)); |
ed99526e |
206 | |
ea207ed3 |
207 | } |
208 | |
0df0018d |
209 | /* safe call to try and start hostnamed */ |
c09a09bf |
210 | void hostnamed_init() { |
387173cb |
211 | |
fbc0295f |
212 | guint bus_descriptor; |
ed99526e |
213 | |
fbc0295f |
214 | bus_descriptor = g_bus_own_name(G_BUS_TYPE_SYSTEM, |
215 | "org.freedesktop.hostname1", |
216 | G_BUS_NAME_OWNER_FLAGS_NONE, |
828caf9a |
217 | hostnamed_on_bus_acquired, |
218 | hostnamed_on_name_acquired, |
219 | hostnamed_on_name_lost, |
fbc0295f |
220 | NULL, |
221 | NULL); |
496f5d66 |
222 | |
780e1f19 |
223 | } |
71e3eef1 |
224 | |
3c3794ac |
225 | int main() { |
1c38000a |
226 | |
3c3794ac |
227 | GMainLoop *hostnamed_loop; |
228 | hostnamed_loop = g_main_loop_new(NULL, TRUE); |
229 | |
230 | /* config stuff here */ |
231 | |
232 | |
233 | hostnamed_init(); |
234 | g_main_loop_run(hostnamed_loop); |
235 | g_main_loop_unref(hostnamed_loop); |
236 | |
237 | return 0; |
71e3eef1 |
238 | } |
239 | |
6981850a |
240 | /* TODO figure out DMI variables on obsd */ |
71e3eef1 |
241 | /*static gchar *guess_icon_name() { |
242 | |
243 | gchar *filebuf = NULL; |
244 | gchar *ret = NULL; |
245 | |
fbc0295f |
246 | #if defined(__i386__) || defined(__x86_64__) |
c3b84b0a |
247 | |
71e3eef1 |
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 |
c3b84b0a |
255 | |
71e3eef1 |
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 | } |
fbc0295f |
279 | #endif |
71e3eef1 |
280 | ret = g_strdup ("computer"); |
281 | out: |
282 | g_free (filebuf); |
283 | return ret; |
284 | }*/ |
3c3794ac |
285 | |