3b82e3c1 |
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 | |
a35a69c5 |
17 | #include <unistd.h> |
18 | #include <limits.h> |
fd8852d9 |
19 | |
d15318db |
20 | #include <sys/param.h> |
21 | |
4c04b514 |
22 | #include <glib/gprintf.h> |
23 | |
1e8c7c88 |
24 | #include "hostnamed-gen.h" |
a35a69c5 |
25 | |
fd8852d9 |
26 | GPtrArray *hostnamed_freeable; |
1be94ede |
27 | Hostname1 *hostnamed_interf; |
3d53b501 |
28 | |
7323a4e4 |
29 | /* --- begin method/property/signal code --- */ |
76b67a18 |
30 | |
3d53b501 |
31 | static gboolean |
1be94ede |
32 | on_handle_set_hostname(Hostname1 *hn1_passed_interf, |
3d53b501 |
33 | GDBusMethodInvocation *invoc, |
34 | const gchar *greet, |
35 | gpointer data) { |
36 | return FALSE; |
37 | } |
38 | |
39 | static gboolean |
1be94ede |
40 | on_handle_set_static_hostname(Hostname1 *hn1_passed_interf, |
3d53b501 |
41 | GDBusMethodInvocation *invoc, |
42 | const gchar *greet, |
43 | gpointer data) { |
44 | return FALSE; |
45 | } |
46 | |
47 | static gboolean |
1be94ede |
48 | on_handle_set_pretty_hostname(Hostname1 *hn1_passed_interf, |
3d53b501 |
49 | GDBusMethodInvocation *invoc, |
50 | const gchar *greet, |
51 | gpointer data) { |
52 | return FALSE; |
53 | } |
54 | |
55 | static gboolean |
1be94ede |
56 | on_handle_set_chassis(Hostname1 *hn1_passed_interf, |
3d53b501 |
57 | GDBusMethodInvocation *invoc, |
58 | const gchar *greet, |
59 | gpointer data) { |
60 | return FALSE; |
61 | } |
62 | |
63 | static gboolean |
1be94ede |
64 | on_handle_set_icon_name(Hostname1 *hn1_passed_interf, |
3d53b501 |
65 | GDBusMethodInvocation *invoc, |
66 | const gchar *greet, |
67 | gpointer data) { |
68 | return FALSE; |
69 | } |
70 | |
76b67a18 |
71 | /* note: all hostnamed/hostname1's properties are read-only, |
72 | * and do not need set_ functions, gdbus-codegen realized |
73 | * this from the XML and handled the to-be error of trying |
74 | * to set a read-only property's value |
75 | */ |
76 | |
77 | const gchar * |
78 | our_get_hostname() { |
79 | |
d15318db |
80 | gchar *hostname_buf; |
81 | |
82 | hostname_buf = (gchar*) g_malloc0(MAXHOSTNAMELEN); |
7323a4e4 |
83 | g_ptr_array_add(hostnamed_freeable, hostname_buf); |
84 | |
85 | if(gethostname(hostname_buf, MAXHOSTNAMELEN)) |
86 | return ""; |
d15318db |
87 | |
88 | return hostname_buf; |
76b67a18 |
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 | |
7323a4e4 |
145 | /* --- end method/property/signal code, begin bus/name handlers --- */ |
0df0018d |
146 | |
5b005882 |
147 | static void hostnamed_on_bus_acquired(GDBusConnection *conn, |
1cd5e6fe |
148 | const gchar *name, |
149 | gpointer user_data) { |
d1e1db9e |
150 | |
1cd5e6fe |
151 | g_print("got bus, name: %s\n", name); |
d1e1db9e |
152 | |
ea207ed3 |
153 | } |
154 | |
5b005882 |
155 | static void hostnamed_on_name_acquired(GDBusConnection *conn, |
1be94ede |
156 | const gchar *name, |
157 | gpointer user_data) { |
80043b36 |
158 | |
3d53b501 |
159 | g_print("got '%s' on system bus\n", name); |
160 | |
1be94ede |
161 | hostnamed_interf = hostname1_skeleton_new(); |
3d53b501 |
162 | |
76b67a18 |
163 | /* attach function pointers to generated struct's method handlers */ |
3d53b501 |
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 | |
76b67a18 |
170 | /* set our properties before export */ |
1be94ede |
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()); |
76b67a18 |
181 | |
3d53b501 |
182 | if(!g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(hostnamed_interf), |
1be94ede |
183 | conn, |
184 | "/org/freedesktop/hostname1", |
185 | NULL)) { |
3d53b501 |
186 | |
1be94ede |
187 | g_printf("Failed to export Hostname1's interface!"); |
3d53b501 |
188 | } |
189 | |
ea207ed3 |
190 | } |
191 | |
7323a4e4 |
192 | /* --- end bus/name handlers, begin misc functions --- */ |
193 | |
1e8c7c88 |
194 | /* free()'s */ |
195 | void hostnamed_mem_clean() { |
196 | |
7323a4e4 |
197 | ddg_ptr_array_foreach(hostnamed_freeable, (GFunc) g_free, NULL); |
198 | g_ptr_array_free(hostnamed_freeable); |
1e8c7c88 |
199 | } |
200 | |
5b005882 |
201 | static void hostnamed_on_name_lost(GDBusConnection *conn, |
1be94ede |
202 | const gchar *name, |
203 | gpointer user_data) { |
80043b36 |
204 | |
1cd5e6fe |
205 | g_print("lost name %s, exiting...", name); |
fd8852d9 |
206 | |
1cd5e6fe |
207 | hostnamed_mem_clean(); |
3d53b501 |
208 | g_dbus_interface_skeleton_unexport(G_DBUS_INTERFACE_SKELETON(hostnamed_interf)); |
fd8852d9 |
209 | |
ea207ed3 |
210 | } |
211 | |
0df0018d |
212 | /* safe call to try and start hostnamed */ |
7f0a0212 |
213 | void hostnamed_init() { |
387173cb |
214 | |
1cd5e6fe |
215 | guint bus_descriptor; |
fd8852d9 |
216 | |
1cd5e6fe |
217 | bus_descriptor = g_bus_own_name(G_BUS_TYPE_SYSTEM, |
218 | "org.freedesktop.hostname1", |
219 | G_BUS_NAME_OWNER_FLAGS_NONE, |
5b005882 |
220 | hostnamed_on_bus_acquired, |
221 | hostnamed_on_name_acquired, |
222 | hostnamed_on_name_lost, |
1cd5e6fe |
223 | NULL, |
224 | NULL); |
496f5d66 |
225 | |
780e1f19 |
226 | } |
a35a69c5 |
227 | |
1e8c7c88 |
228 | int main() { |
36575bff |
229 | |
1e8c7c88 |
230 | GMainLoop *hostnamed_loop; |
231 | hostnamed_loop = g_main_loop_new(NULL, TRUE); |
232 | |
233 | /* config stuff here */ |
234 | |
1e8c7c88 |
235 | hostnamed_init(); |
236 | g_main_loop_run(hostnamed_loop); |
237 | g_main_loop_unref(hostnamed_loop); |
238 | |
7323a4e4 |
239 | hostnamed_mem_clean(); |
240 | |
1e8c7c88 |
241 | return 0; |
a35a69c5 |
242 | } |
243 | |
a6f11205 |
244 | /* TODO figure out DMI variables on obsd */ |
a35a69c5 |
245 | /*static gchar *guess_icon_name() { |
246 | |
247 | gchar *filebuf = NULL; |
248 | gchar *ret = NULL; |
249 | |
1cd5e6fe |
250 | #if defined(__i386__) || defined(__x86_64__) |
39df6847 |
251 | |
a35a69c5 |
252 | Taken with a few minor changes from systemd's hostnamed.c, |
253 | copyright 2011 Lennart Poettering. |
254 | |
255 | See the SMBIOS Specification 2.7.1 section 7.4.1 for |
256 | details about the values listed here: |
257 | |
258 | http://www.dmtf.org/sites/default/files/standards/documents/DSP0134_2.7.1.pdf |
39df6847 |
259 | |
a35a69c5 |
260 | |
261 | if (g_file_get_contents ("/sys/class/dmi/id/chassis_type", &filebuf, NULL, NULL)) { |
262 | switch (g_ascii_strtoull (filebuf, NULL, 10)) { |
263 | case 0x3: |
264 | case 0x4: |
265 | case 0x5: |
266 | case 0x6: |
267 | case 0x7: |
268 | ret = g_strdup ("computer-desktop"); |
269 | goto out; |
270 | case 0x9: |
271 | case 0xA: |
272 | case 0xE: |
273 | ret = g_strdup ("computer-laptop"); |
274 | goto out; |
275 | case 0x11: |
276 | case 0x17: |
277 | case 0x1C: |
278 | case 0x1D: |
279 | ret = g_strdup ("computer-server"); |
280 | goto out; |
281 | } |
282 | } |
1cd5e6fe |
283 | #endif |
a35a69c5 |
284 | ret = g_strdup ("computer"); |
285 | out: |
286 | g_free (filebuf); |
287 | return ret; |
288 | }*/ |
1e8c7c88 |
289 | |