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 | |
20 | #include <glib.h> |
ea207ed3 |
21 | #include <gio/gio.h> |
22 | |
ed99526e |
23 | #include "hostnamed.h" |
24 | #include "hostnamed-gen.c" |
71e3eef1 |
25 | |
ed99526e |
26 | GPtrArray *hostnamed_freeable; |
ff1030d4 |
27 | GDBusNodeInfo *spect_data; |
0df0018d |
28 | |
80043b36 |
29 | static void on_bus_acquired(GDBusConnection *conn, |
30 | const gchar *name, |
31 | gpointer user_data) { |
11475670 |
32 | |
33 | GError *err; |
34 | |
35 | g_print("got bus, name: %s\n", name); |
36 | |
ea207ed3 |
37 | } |
38 | |
80043b36 |
39 | static void on_name_acquired(GDBusConnection *conn, |
40 | const gchar *name, |
41 | gpointer user_data) { |
42 | |
ea207ed3 |
43 | g_print("got name %s\n", name); |
ea207ed3 |
44 | } |
45 | |
80043b36 |
46 | static void on_name_lost(GDBusConnection *conn, |
47 | const gchar *name, |
48 | gpointer user_data) { |
49 | |
f4d34761 |
50 | g_print("lost name %s, exiting...", name); |
ed99526e |
51 | |
52 | hostnamed_mem_clean(); |
53 | |
e8c57a5d |
54 | //TODO exit through g_main_loop properly... |
ea207ed3 |
55 | } |
56 | |
0df0018d |
57 | /* safe call to try and start hostnamed */ |
c3b84b0a |
58 | GError *hostnamed_init() { |
387173cb |
59 | |
ea207ed3 |
60 | guint bus_descriptor; |
11475670 |
61 | GError *err = NULL; |
ed99526e |
62 | gchar **hostnamed_ispect_xml; |
63 | gchar *hostnamed_joined_xml; |
11475670 |
64 | |
ed99526e |
65 | hostnamed_freeable = g_ptr_array_new(); |
c3b84b0a |
66 | hostnamed_ispect_xml = g_malloc(3000); |
ed99526e |
67 | |
c3b84b0a |
68 | g_file_get_contents("conf/hostnamed-ispect.xml", hostnamed_ispect_xml, NULL, NULL); |
11475670 |
69 | hostnamed_joined_xml = g_strjoinv("\n", hostnamed_ispect_xml); |
70 | spect_data = g_dbus_node_info_new_for_xml(hostnamed_joined_xml, NULL); |
71e3eef1 |
71 | |
ed99526e |
72 | g_free(hostnamed_ispect_xml); |
73 | g_ptr_array_add(hostnamed_freeable, hostnamed_joined_xml); |
74 | |
6de394d4 |
75 | bus_descriptor = g_bus_own_name(G_BUS_TYPE_SYSTEM, |
11475670 |
76 | "org.freedesktop.hostname1", |
ea207ed3 |
77 | G_BUS_NAME_OWNER_FLAGS_NONE, |
78 | on_bus_acquired, |
79 | on_name_acquired, |
80 | on_name_lost, |
81 | NULL, |
82 | NULL); |
496f5d66 |
83 | |
c3b84b0a |
84 | //TODO: malloc and return reference as if a main() closed |
6de394d4 |
85 | return err; |
780e1f19 |
86 | } |
71e3eef1 |
87 | |
ed99526e |
88 | /* free()'s */ |
89 | void hostnamed_mem_clean() { |
1c38000a |
90 | |
ed99526e |
91 | g_ptr_array_foreach(hostnamed_freeable, (GFunc) g_free, NULL); |
71e3eef1 |
92 | } |
93 | |
94 | //TODO figure out DMI variables on obsd |
95 | /*static gchar *guess_icon_name() { |
96 | |
97 | gchar *filebuf = NULL; |
98 | gchar *ret = NULL; |
99 | |
100 | //TODO vm check |
101 | |
102 | #if defined(__i386__) || defined(__x86_64__) |
c3b84b0a |
103 | |
71e3eef1 |
104 | Taken with a few minor changes from systemd's hostnamed.c, |
105 | copyright 2011 Lennart Poettering. |
106 | |
107 | See the SMBIOS Specification 2.7.1 section 7.4.1 for |
108 | details about the values listed here: |
109 | |
110 | http://www.dmtf.org/sites/default/files/standards/documents/DSP0134_2.7.1.pdf |
c3b84b0a |
111 | |
71e3eef1 |
112 | |
113 | if (g_file_get_contents ("/sys/class/dmi/id/chassis_type", &filebuf, NULL, NULL)) { |
114 | switch (g_ascii_strtoull (filebuf, NULL, 10)) { |
115 | case 0x3: |
116 | case 0x4: |
117 | case 0x5: |
118 | case 0x6: |
119 | case 0x7: |
120 | ret = g_strdup ("computer-desktop"); |
121 | goto out; |
122 | case 0x9: |
123 | case 0xA: |
124 | case 0xE: |
125 | ret = g_strdup ("computer-laptop"); |
126 | goto out; |
127 | case 0x11: |
128 | case 0x17: |
129 | case 0x1C: |
130 | case 0x1D: |
131 | ret = g_strdup ("computer-server"); |
132 | goto out; |
133 | } |
134 | } |
135 | #endif |
136 | ret = g_strdup ("computer"); |
137 | out: |
138 | g_free (filebuf); |
139 | return ret; |
140 | }*/ |