shebang /bin/sh instead of bash for gdbus interface generator script
[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,
30 const gchar *name,
31 gpointer user_data) {
d1e1db9e 32
33 GError *err;
34
35 g_print("got bus, name: %s\n", name);
36
ea207ed3 37}
38
80043b36 39static 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 46static void on_name_lost(GDBusConnection *conn,
47 const gchar *name,
48 gpointer user_data) {
49
faab2eee 50 g_print("lost name %s, exiting...", name);
fd8852d9 51
52 hostnamed_mem_clean();
53
1cd91c9c 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
ea207ed3 60 guint bus_descriptor;
d1e1db9e 61 GError *err = NULL;
fd8852d9 62 gchar **hostnamed_ispect_xml;
63 gchar *hostnamed_joined_xml;
d1e1db9e 64
fd8852d9 65 hostnamed_freeable = g_ptr_array_new();
39df6847 66 hostnamed_ispect_xml = g_malloc(3000);
fd8852d9 67
39df6847 68 g_file_get_contents("conf/hostnamed-ispect.xml", hostnamed_ispect_xml, NULL, NULL);
d1e1db9e 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
fd8852d9 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,
d1e1db9e 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
39df6847 84 //TODO: malloc and return reference as if a main() closed
6de394d4 85 return err;
780e1f19 86}
a35a69c5 87
fd8852d9 88/* free()'s */
89void hostnamed_mem_clean() {
36575bff 90
fd8852d9 91 g_ptr_array_foreach(hostnamed_freeable, (GFunc) g_free, NULL);
a35a69c5 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__)
39df6847 103
a35a69c5 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
39df6847 111
a35a69c5 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}*/