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