Line |
Branch |
Exec |
Source |
1 |
|
|
/* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
2 |
|
|
/* cc-hostname.c |
3 |
|
|
* |
4 |
|
|
* Copyright 2023 Red Hat Inc |
5 |
|
|
* |
6 |
|
|
* This program is free software: you can redistribute it and/or modify |
7 |
|
|
* it under the terms of the GNU General Public License as published by |
8 |
|
|
* the Free Software Foundation, either version 3 of the License, or |
9 |
|
|
* (at your option) any later version. |
10 |
|
|
* |
11 |
|
|
* This program is distributed in the hope that it will be useful, |
12 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
|
|
* GNU General Public License for more details. |
15 |
|
|
* |
16 |
|
|
* You should have received a copy of the GNU General Public License |
17 |
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
18 |
|
|
* |
19 |
|
|
* Author(s): |
20 |
|
|
* Felipe Borges <felipeborges@gnome.org> |
21 |
|
|
* |
22 |
|
|
* SPDX-License-Identifier: GPL-3.0-or-later |
23 |
|
|
*/ |
24 |
|
|
|
25 |
|
|
#undef G_LOG_DOMAIN |
26 |
|
|
#define G_LOG_DOMAIN "cc-hostname" |
27 |
|
|
|
28 |
|
|
#ifdef HAVE_CONFIG_H |
29 |
|
|
# include <config.h> |
30 |
|
|
#endif |
31 |
|
|
|
32 |
|
|
#include "cc-hostname.h" |
33 |
|
|
#include "hostname-helper.h" |
34 |
|
|
#include "shell/cc-object-storage.h" |
35 |
|
|
|
36 |
|
|
#define HOSTNAME_BUS_NAME "org.freedesktop.hostname1" |
37 |
|
|
#define HOSTNAME_OBJECT_PATH "/org/freedesktop/hostname1" |
38 |
|
|
|
39 |
|
|
struct _CcHostname |
40 |
|
|
{ |
41 |
|
|
GObject parent_instance; |
42 |
|
|
|
43 |
|
|
GDBusProxy *proxy; |
44 |
|
|
}; |
45 |
|
|
|
46 |
|
✗ |
G_DEFINE_TYPE (CcHostname, cc_hostname, G_TYPE_OBJECT) |
47 |
|
|
|
48 |
|
|
static void |
49 |
|
✗ |
cc_hostname_dispose (GObject *object) |
50 |
|
|
{ |
51 |
|
✗ |
CcHostname *self = CC_HOSTNAME (object); |
52 |
|
|
|
53 |
|
✗ |
g_clear_object (&self->proxy); |
54 |
|
|
|
55 |
|
✗ |
G_OBJECT_CLASS (cc_hostname_parent_class)->dispose (object); |
56 |
|
✗ |
} |
57 |
|
|
|
58 |
|
|
static void |
59 |
|
✗ |
cc_hostname_constructed (GObject *object) |
60 |
|
|
{ |
61 |
|
✗ |
CcHostname *self = CC_HOSTNAME (object); |
62 |
|
✗ |
g_autoptr(GError) error = NULL; |
63 |
|
|
|
64 |
|
✗ |
self->proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM, |
65 |
|
|
G_DBUS_PROXY_FLAGS_NONE, |
66 |
|
|
NULL, |
67 |
|
|
HOSTNAME_BUS_NAME, |
68 |
|
|
HOSTNAME_OBJECT_PATH, |
69 |
|
|
HOSTNAME_BUS_NAME, |
70 |
|
|
NULL, |
71 |
|
|
&error); |
72 |
|
✗ |
if (self->proxy == NULL) { |
73 |
|
✗ |
g_critical ("Couldn't connect to hostnamed: %s", error->message); |
74 |
|
|
|
75 |
|
✗ |
return; |
76 |
|
|
} |
77 |
|
|
} |
78 |
|
|
|
79 |
|
|
static void |
80 |
|
✗ |
cc_hostname_init (CcHostname *self) |
81 |
|
|
{ |
82 |
|
✗ |
} |
83 |
|
|
|
84 |
|
|
static void |
85 |
|
✗ |
cc_hostname_class_init (CcHostnameClass *klass) |
86 |
|
|
{ |
87 |
|
✗ |
GObjectClass *object_class = G_OBJECT_CLASS (klass); |
88 |
|
|
|
89 |
|
✗ |
object_class->constructed = cc_hostname_constructed; |
90 |
|
✗ |
object_class->dispose = cc_hostname_dispose; |
91 |
|
✗ |
} |
92 |
|
|
|
93 |
|
|
CcHostname * |
94 |
|
✗ |
cc_hostname_get_default (void) |
95 |
|
|
{ |
96 |
|
✗ |
g_autoptr(CcHostname) self = NULL; |
97 |
|
|
|
98 |
|
✗ |
if (cc_object_storage_has_object (CC_OBJECT_HOSTNAME)) { |
99 |
|
✗ |
self = cc_object_storage_get_object (CC_OBJECT_HOSTNAME); |
100 |
|
|
} else { |
101 |
|
✗ |
self = g_object_new (CC_TYPE_HOSTNAME, NULL); |
102 |
|
✗ |
cc_object_storage_add_object (CC_OBJECT_HOSTNAME, self); |
103 |
|
|
} |
104 |
|
|
|
105 |
|
✗ |
return self; |
106 |
|
|
} |
107 |
|
|
|
108 |
|
|
gchar * |
109 |
|
✗ |
cc_hostname_get_property (CcHostname *self, |
110 |
|
|
const gchar *property) |
111 |
|
|
{ |
112 |
|
✗ |
g_autoptr(GVariant) variant = NULL; |
113 |
|
✗ |
g_autoptr(GVariant) inner = NULL; |
114 |
|
✗ |
g_autoptr(GError) error = NULL; |
115 |
|
|
|
116 |
|
✗ |
g_return_val_if_fail (CC_IS_HOSTNAME (self), NULL); |
117 |
|
✗ |
g_return_val_if_fail (property != NULL, NULL); |
118 |
|
|
|
119 |
|
✗ |
if (!self->proxy) |
120 |
|
✗ |
return g_strdup (""); |
121 |
|
|
|
122 |
|
✗ |
variant = g_dbus_proxy_get_cached_property (self->proxy, property); |
123 |
|
✗ |
if (variant) |
124 |
|
✗ |
return g_variant_dup_string (variant, NULL); |
125 |
|
|
|
126 |
|
✗ |
variant = g_dbus_proxy_call_sync (self->proxy, |
127 |
|
|
"org.freedesktop.DBus.Properties.Get", |
128 |
|
|
g_variant_new ("(ss)", HOSTNAME_BUS_NAME, property), |
129 |
|
|
G_DBUS_CALL_FLAGS_NONE, |
130 |
|
|
-1, |
131 |
|
|
NULL, |
132 |
|
|
&error); |
133 |
|
✗ |
if (variant == NULL) { |
134 |
|
✗ |
g_warning ("Failed to get property '%s': %s", property, error->message); |
135 |
|
|
|
136 |
|
✗ |
return NULL; |
137 |
|
|
} |
138 |
|
|
|
139 |
|
✗ |
g_variant_get (variant, "(v)", &inner); |
140 |
|
✗ |
return g_variant_dup_string (inner, NULL); |
141 |
|
|
} |
142 |
|
|
|
143 |
|
|
gchar * |
144 |
|
✗ |
cc_hostname_get_display_hostname (CcHostname *self) |
145 |
|
|
{ |
146 |
|
✗ |
g_autofree gchar *str = NULL; |
147 |
|
|
|
148 |
|
✗ |
g_return_val_if_fail (CC_IS_HOSTNAME (self), NULL); |
149 |
|
|
|
150 |
|
✗ |
str = cc_hostname_get_property (self, "PrettyHostname"); |
151 |
|
|
/* Empty strings means that we need to fallback */ |
152 |
|
✗ |
if (str != NULL && *str == '\0') |
153 |
|
✗ |
return cc_hostname_get_property (self, "Hostname"); |
154 |
|
|
|
155 |
|
✗ |
return g_steal_pointer (&str); |
156 |
|
|
} |
157 |
|
|
|
158 |
|
|
void |
159 |
|
✗ |
cc_hostname_set_hostname (CcHostname *self, |
160 |
|
|
const gchar *hostname) |
161 |
|
|
{ |
162 |
|
✗ |
g_autofree gchar *static_hostname = NULL; |
163 |
|
✗ |
g_autoptr(GVariant) pretty_result = NULL; |
164 |
|
✗ |
g_autoptr(GVariant) static_result = NULL; |
165 |
|
✗ |
g_autoptr(GError) pretty_error = NULL; |
166 |
|
✗ |
g_autoptr(GError) static_error = NULL; |
167 |
|
|
|
168 |
|
✗ |
g_return_if_fail (CC_IS_HOSTNAME (self)); |
169 |
|
✗ |
g_return_if_fail (hostname != NULL); |
170 |
|
|
|
171 |
|
✗ |
g_debug ("Setting PrettyHostname to '%s'", hostname); |
172 |
|
✗ |
pretty_result = g_dbus_proxy_call_sync (self->proxy, |
173 |
|
|
"SetPrettyHostname", |
174 |
|
|
g_variant_new ("(sb)", hostname, FALSE), |
175 |
|
|
G_DBUS_CALL_FLAGS_NONE, |
176 |
|
|
-1, NULL, &pretty_error); |
177 |
|
✗ |
if (pretty_result == NULL) |
178 |
|
✗ |
g_warning ("Could not set PrettyHostname: %s", pretty_error->message); |
179 |
|
|
|
180 |
|
|
/* Set the static hostname */ |
181 |
|
✗ |
static_hostname = pretty_hostname_to_static (hostname, FALSE); |
182 |
|
✗ |
g_assert (hostname); |
183 |
|
|
|
184 |
|
✗ |
g_debug ("Setting StaticHostname to '%s'", static_hostname); |
185 |
|
✗ |
static_result = g_dbus_proxy_call_sync (self->proxy, |
186 |
|
|
"SetStaticHostname", |
187 |
|
|
g_variant_new ("(sb)", static_hostname, FALSE), |
188 |
|
|
G_DBUS_CALL_FLAGS_NONE, |
189 |
|
|
-1, NULL, &static_error); |
190 |
|
✗ |
if (static_result == NULL) |
191 |
|
✗ |
g_warning ("Could not set StaticHostname: %s", static_error->message); |
192 |
|
|
} |
193 |
|
|
|
194 |
|
|
gchar * |
195 |
|
✗ |
cc_hostname_get_chassis_type (CcHostname *self) |
196 |
|
|
{ |
197 |
|
✗ |
g_return_val_if_fail (CC_IS_HOSTNAME (self), NULL); |
198 |
|
|
|
199 |
|
✗ |
return cc_hostname_get_property (self, "Chassis"); |
200 |
|
|
} |
201 |
|
|
|
202 |
|
|
gboolean |
203 |
|
✗ |
cc_hostname_is_vm_chassis (CcHostname *self) |
204 |
|
|
{ |
205 |
|
✗ |
g_autofree gchar *chassis_type = NULL; |
206 |
|
|
|
207 |
|
✗ |
g_return_val_if_fail (CC_IS_HOSTNAME (self), FALSE); |
208 |
|
|
|
209 |
|
✗ |
chassis_type = cc_hostname_get_chassis_type (self); |
210 |
|
✗ |
return g_strcmp0 (chassis_type, "vm") == 0; |
211 |
|
|
} |
212 |
|
|
|