change buffer size for gethostname() to HOST_NAME_MAX to avoid accidently truncating...
[systembsd.git] / src / interfaces / hostnamed / hostnamed.c
CommitLineData
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 26GPtrArray *hostnamed_freeable;
ff1030d4 27GDBusNodeInfo *spect_data;
0df0018d 28
80043b36 29static void on_bus_acquired(GDBusConnection *conn,
fbc0295f 30 const gchar *name,
31 gpointer user_data) {
11475670 32
fbc0295f 33 GError *err;
11475670 34
fbc0295f 35 g_print("got bus, name: %s\n", name);
11475670 36
ea207ed3 37}
38
80043b36 39static void on_name_acquired(GDBusConnection *conn,
fbc0295f 40 const gchar *name,
41 gpointer user_data) {
80043b36 42
fbc0295f 43 g_print("got name %s\n", name);
ea207ed3 44}
45
80043b36 46static void on_name_lost(GDBusConnection *conn,
fbc0295f 47 const gchar *name,
48 gpointer user_data) {
80043b36 49
fbc0295f 50 g_print("lost name %s, exiting...", name);
ed99526e 51
fbc0295f 52 hostnamed_mem_clean();
ed99526e 53
fbc0295f 54 //TODO exit through g_main_loop properly...
ea207ed3 55}
56
0df0018d 57/* safe call to try and start hostnamed */
c3b84b0a 58GError *hostnamed_init() {
387173cb 59
fbc0295f 60 guint bus_descriptor;
61 GError *err = NULL;
62 gchar **hostnamed_ispect_xml;
63 gchar *hostnamed_joined_xml;
11475670 64
fbc0295f 65 hostnamed_freeable = g_ptr_array_new();
66 hostnamed_ispect_xml = g_malloc(3000);
ed99526e 67
fbc0295f 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);
71e3eef1 71
fbc0295f 72 g_free(hostnamed_ispect_xml);
73 g_ptr_array_add(hostnamed_freeable, hostnamed_joined_xml);
ed99526e 74
fbc0295f 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
fbc0295f 84 //TODO: malloc and return reference as if a main() closed
85 return err;
780e1f19 86}
71e3eef1 87
ed99526e 88/* free()'s */
89void hostnamed_mem_clean() {
1c38000a 90
fbc0295f 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
fbc0295f 100 //TODO vm check
71e3eef1 101
fbc0295f 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 }
fbc0295f 135 #endif
71e3eef1 136 ret = g_strdup ("computer");
137 out:
138 g_free (filebuf);
139 return ret;
140}*/