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