]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/hostname/hostnamed.c
Merge pull request #24628 from medhefgo/boot-sections
[thirdparty/systemd.git] / src / hostname / hostnamed.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <errno.h>
4 #include <sys/utsname.h>
5 #include <sys/stat.h>
6 #include <sys/types.h>
7 #include <unistd.h>
8
9 #include "alloc-util.h"
10 #include "bus-common-errors.h"
11 #include "bus-get-properties.h"
12 #include "bus-log-control-api.h"
13 #include "bus-polkit.h"
14 #include "def.h"
15 #include "env-file-label.h"
16 #include "env-file.h"
17 #include "env-util.h"
18 #include "fileio-label.h"
19 #include "fileio.h"
20 #include "hostname-setup.h"
21 #include "hostname-util.h"
22 #include "id128-util.h"
23 #include "json.h"
24 #include "main-func.h"
25 #include "missing_capability.h"
26 #include "nscd-flush.h"
27 #include "nulstr-util.h"
28 #include "os-util.h"
29 #include "parse-util.h"
30 #include "path-util.h"
31 #include "sd-device.h"
32 #include "selinux-util.h"
33 #include "service-util.h"
34 #include "signal-util.h"
35 #include "stat-util.h"
36 #include "string-table.h"
37 #include "strv.h"
38 #include "user-util.h"
39 #include "util.h"
40 #include "virt.h"
41
42 #define VALID_DEPLOYMENT_CHARS (DIGITS LETTERS "-.:")
43
44 /* Properties we cache are indexed by an enum, to make invalidation easy and systematic (as we can iterate
45 * through them all, and they are uniformly strings). */
46 typedef enum {
47 /* Read from /etc/hostname */
48 PROP_STATIC_HOSTNAME,
49
50 /* Read from /etc/machine-info */
51 PROP_PRETTY_HOSTNAME,
52 PROP_ICON_NAME,
53 PROP_CHASSIS,
54 PROP_DEPLOYMENT,
55 PROP_LOCATION,
56 PROP_HARDWARE_VENDOR,
57 PROP_HARDWARE_MODEL,
58
59 /* Read from /etc/os-release (or /usr/lib/os-release) */
60 PROP_OS_PRETTY_NAME,
61 PROP_OS_CPE_NAME,
62 PROP_OS_HOME_URL,
63 _PROP_MAX,
64 _PROP_INVALID = -EINVAL,
65 } HostProperty;
66
67 typedef struct Context {
68 char *data[_PROP_MAX];
69
70 HostnameSource hostname_source;
71
72 struct stat etc_hostname_stat;
73 struct stat etc_os_release_stat;
74 struct stat etc_machine_info_stat;
75
76 Hashmap *polkit_registry;
77 } Context;
78
79 static void context_reset(Context *c, uint64_t mask) {
80 assert(c);
81
82 for (int p = 0; p < _PROP_MAX; p++) {
83 if (!FLAGS_SET(mask, UINT64_C(1) << p))
84 continue;
85
86 c->data[p] = mfree(c->data[p]);
87 }
88 }
89
90 static void context_destroy(Context *c) {
91 assert(c);
92
93 context_reset(c, UINT64_MAX);
94 bus_verify_polkit_async_registry_free(c->polkit_registry);
95 }
96
97 static void context_read_etc_hostname(Context *c) {
98 struct stat current_stat = {};
99 int r;
100
101 assert(c);
102
103 if (stat("/etc/hostname", &current_stat) >= 0 &&
104 stat_inode_unmodified(&c->etc_hostname_stat, &current_stat))
105 return;
106
107 context_reset(c, UINT64_C(1) << PROP_STATIC_HOSTNAME);
108
109 r = read_etc_hostname(NULL, &c->data[PROP_STATIC_HOSTNAME]);
110 if (r < 0 && r != -ENOENT)
111 log_warning_errno(r, "Failed to read /etc/hostname, ignoring: %m");
112
113 c->etc_hostname_stat = current_stat;
114 }
115
116 static void context_read_machine_info(Context *c) {
117 struct stat current_stat = {};
118 int r;
119
120 assert(c);
121
122 if (stat("/etc/machine-info", &current_stat) >= 0 &&
123 stat_inode_unmodified(&c->etc_machine_info_stat, &current_stat))
124 return;
125
126 context_reset(c,
127 (UINT64_C(1) << PROP_PRETTY_HOSTNAME) |
128 (UINT64_C(1) << PROP_ICON_NAME) |
129 (UINT64_C(1) << PROP_CHASSIS) |
130 (UINT64_C(1) << PROP_DEPLOYMENT) |
131 (UINT64_C(1) << PROP_LOCATION) |
132 (UINT64_C(1) << PROP_HARDWARE_VENDOR) |
133 (UINT64_C(1) << PROP_HARDWARE_MODEL));
134
135 r = parse_env_file(NULL, "/etc/machine-info",
136 "PRETTY_HOSTNAME", &c->data[PROP_PRETTY_HOSTNAME],
137 "ICON_NAME", &c->data[PROP_ICON_NAME],
138 "CHASSIS", &c->data[PROP_CHASSIS],
139 "DEPLOYMENT", &c->data[PROP_DEPLOYMENT],
140 "LOCATION", &c->data[PROP_LOCATION],
141 "HARDWARE_VENDOR", &c->data[PROP_HARDWARE_VENDOR],
142 "HARDWARE_MODEL", &c->data[PROP_HARDWARE_MODEL]);
143 if (r < 0 && r != -ENOENT)
144 log_warning_errno(r, "Failed to read /etc/machine-info, ignoring: %m");
145
146 c->etc_machine_info_stat = current_stat;
147 }
148
149 static void context_read_os_release(Context *c) {
150 struct stat current_stat = {};
151 int r;
152
153 assert(c);
154
155 if ((stat("/etc/os-release", &current_stat) >= 0 ||
156 stat("/usr/lib/os-release", &current_stat) >= 0) &&
157 stat_inode_unmodified(&c->etc_os_release_stat, &current_stat))
158 return;
159
160 context_reset(c,
161 (UINT64_C(1) << PROP_OS_PRETTY_NAME) |
162 (UINT64_C(1) << PROP_OS_CPE_NAME) |
163 (UINT64_C(1) << PROP_OS_HOME_URL));
164
165 r = parse_os_release(NULL,
166 "PRETTY_NAME", &c->data[PROP_OS_PRETTY_NAME],
167 "CPE_NAME", &c->data[PROP_OS_CPE_NAME],
168 "HOME_URL", &c->data[PROP_OS_HOME_URL]);
169 if (r < 0 && r != -ENOENT)
170 log_warning_errno(r, "Failed to read os-release file, ignoring: %m");
171
172 c->etc_os_release_stat = current_stat;
173 }
174
175 static int get_dmi_data(const char *database_key, const char *regular_key, char **ret) {
176 _cleanup_(sd_device_unrefp) sd_device *device = NULL;
177 _cleanup_free_ char *b = NULL;
178 const char *s = NULL;
179 int r;
180
181 r = sd_device_new_from_syspath(&device, "/sys/class/dmi/id");
182 if (r < 0)
183 return log_debug_errno(r, "Failed to open /sys/class/dmi/id device, ignoring: %m");
184
185 if (database_key)
186 (void) sd_device_get_property_value(device, database_key, &s);
187 if (!s && regular_key)
188 (void) sd_device_get_property_value(device, regular_key, &s);
189
190 if (!ret)
191 return !!s;
192
193 if (s) {
194 b = strdup(s);
195 if (!b)
196 return -ENOMEM;
197 }
198
199 *ret = TAKE_PTR(b);
200 return !!s;
201 }
202
203 static int get_hardware_vendor(char **ret) {
204 return get_dmi_data("ID_VENDOR_FROM_DATABASE", "ID_VENDOR", ret);
205 }
206
207 static int get_hardware_model(char **ret) {
208 return get_dmi_data("ID_MODEL_FROM_DATABASE", "ID_MODEL", ret);
209 }
210
211 static int get_hardware_firmware_data(const char *sysattr, char **ret) {
212 _cleanup_(sd_device_unrefp) sd_device *device = NULL;
213 _cleanup_free_ char *b = NULL;
214 const char *s = NULL;
215 int r;
216
217 assert(sysattr);
218
219 r = sd_device_new_from_syspath(&device, "/sys/class/dmi/id");
220 if (r < 0)
221 return log_debug_errno(r, "Failed to open /sys/class/dmi/id device, ignoring: %m");
222
223 (void) sd_device_get_sysattr_value(device, sysattr, &s);
224 if (!isempty(s)) {
225 b = strdup(s);
226 if (!b)
227 return -ENOMEM;
228 }
229
230 if (ret)
231 *ret = TAKE_PTR(b);
232
233 return !isempty(s);
234 }
235
236 static int get_hardware_serial(char **ret) {
237 int r;
238
239 r = get_hardware_firmware_data("product_serial", ret);
240 if (r <= 0)
241 return get_hardware_firmware_data("board_serial", ret);
242
243 return r;
244 }
245
246 static int get_firmware_version(char **ret) {
247 return get_hardware_firmware_data("bios_version", ret);
248 }
249
250 static const char* valid_chassis(const char *chassis) {
251 assert(chassis);
252
253 return nulstr_get(
254 "vm\0"
255 "container\0"
256 "desktop\0"
257 "laptop\0"
258 "convertible\0"
259 "server\0"
260 "tablet\0"
261 "handset\0"
262 "watch\0"
263 "embedded\0",
264 chassis);
265 }
266
267 static bool valid_deployment(const char *deployment) {
268 assert(deployment);
269
270 return in_charset(deployment, VALID_DEPLOYMENT_CHARS);
271 }
272
273 static const char* fallback_chassis(void) {
274 const char *chassis;
275 _cleanup_free_ char *type = NULL;
276 Virtualization v;
277 unsigned t;
278 int r;
279
280 v = detect_virtualization();
281 if (v < 0)
282 log_debug_errno(v, "Failed to detect virtualization, ignoring: %m");
283 else if (VIRTUALIZATION_IS_VM(v))
284 return "vm";
285 else if (VIRTUALIZATION_IS_CONTAINER(v))
286 return "container";
287
288 r = read_one_line_file("/sys/class/dmi/id/chassis_type", &type);
289 if (r < 0) {
290 log_debug_errno(r, "Failed to read DMI chassis type, ignoring: %m");
291 goto try_acpi;
292 }
293
294 r = safe_atou(type, &t);
295 if (r < 0) {
296 log_debug_errno(r, "Failed to parse DMI chassis type \"%s\", ignoring: %m", type);
297 goto try_acpi;
298 }
299
300 /* We only list the really obvious cases here. The DMI data is unreliable enough, so let's not do any
301 * additional guesswork on top of that.
302 *
303 * See the SMBIOS Specification 3.5.0 section 7.4.1 for details about the values listed here:
304 *
305 * https://www.dmtf.org/sites/default/files/standards/documents/DSP0134_3.5.0.pdf
306 */
307
308 switch (t) {
309
310 case 0x03: /* Desktop */
311 case 0x04: /* Low Profile Desktop */
312 case 0x06: /* Mini Tower */
313 case 0x07: /* Tower */
314 case 0x0D: /* All in one (i.e. PC built into monitor) */
315 case 0x23: /* Mini PC */
316 case 0x24: /* Stick PC */
317 return "desktop";
318
319 case 0x8: /* Portable */
320 case 0x9: /* Laptop */
321 case 0xA: /* Notebook */
322 case 0xE: /* Sub Notebook */
323 return "laptop";
324
325 case 0xB: /* Hand Held */
326 return "handset";
327
328 case 0x11: /* Main Server Chassis */
329 case 0x1C: /* Blade */
330 case 0x1D: /* Blade Enclosure */
331 return "server";
332
333 case 0x1E: /* Tablet */
334 return "tablet";
335
336 case 0x1F: /* Convertible */
337 case 0x20: /* Detachable */
338 return "convertible";
339
340 case 0x21: /* IoT Gateway */
341 case 0x22: /* Embedded PC */
342 return "embedded";
343
344 default:
345 log_debug("Unhandled DMI chassis type 0x%02x, ignoring.", t);
346 }
347
348 try_acpi:
349 type = mfree(type);
350 r = read_one_line_file("/sys/firmware/acpi/pm_profile", &type);
351 if (r < 0) {
352 log_debug_errno(r, "Failed read ACPI PM profile, ignoring: %m");
353 goto try_devicetree;
354 }
355
356 r = safe_atou(type, &t);
357 if (r < 0) {
358 log_debug_errno(r, "Failed parse ACPI PM profile \"%s\", ignoring: %m", type);
359 goto try_devicetree;
360 }
361
362 /* We only list the really obvious cases here as the ACPI data is not really super reliable.
363 *
364 * See the ACPI 5.0 Spec Section 5.2.9.1 for details:
365 *
366 * http://www.acpi.info/DOWNLOADS/ACPIspec50.pdf
367 */
368
369 switch (t) {
370
371 case 1: /* Desktop */
372 case 3: /* Workstation */
373 case 6: /* Appliance PC */
374 return "desktop";
375
376 case 2: /* Mobile */
377 return "laptop";
378
379 case 4: /* Enterprise Server */
380 case 5: /* SOHO Server */
381 case 7: /* Performance Server */
382 return "server";
383
384 case 8: /* Tablet */
385 return "tablet";
386
387 default:
388 log_debug("Unhandled ACPI PM profile 0x%02x, ignoring.", t);
389 }
390
391 try_devicetree:
392 type = mfree(type);
393 r = read_one_line_file("/proc/device-tree/chassis-type", &type);
394 if (r < 0) {
395 log_debug_errno(r, "Failed to read device-tree chassis type, ignoring: %m");
396 return NULL;
397 }
398
399 /* Note that the Devicetree specification uses the very same vocabulary
400 * of chassis types as we do, hence we do not need to translate these types:
401 *
402 * https://github.com/devicetree-org/devicetree-specification/blob/master/source/chapter3-devicenodes.rst */
403 chassis = valid_chassis(type);
404 if (!chassis)
405 log_debug("Invalid device-tree chassis type \"%s\", ignoring.", type);
406 return chassis;
407 }
408
409 static char* context_get_chassis(Context *c) {
410 const char *fallback;
411 char *dmi;
412
413 assert(c);
414
415 if (!isempty(c->data[PROP_CHASSIS]))
416 return strdup(c->data[PROP_CHASSIS]);
417
418 if (get_dmi_data("ID_CHASSIS", NULL, &dmi) > 0)
419 return dmi;
420
421 fallback = fallback_chassis();
422 if (fallback)
423 return strdup(fallback);
424
425 return NULL;
426 }
427
428 static char* context_fallback_icon_name(Context *c) {
429 _cleanup_free_ char *chassis = NULL;
430
431 assert(c);
432
433 chassis = context_get_chassis(c);
434 if (chassis)
435 return strjoin("computer-", chassis);
436
437 return strdup("computer");
438 }
439
440 static int context_update_kernel_hostname(
441 Context *c,
442 const char *transient_hn) {
443
444 _cleanup_free_ char *_hn_free = NULL;
445 const char *hn;
446 HostnameSource hns;
447 int r;
448
449 assert(c);
450
451 /* /etc/hostname has the highest preference ... */
452 if (c->data[PROP_STATIC_HOSTNAME]) {
453 hn = c->data[PROP_STATIC_HOSTNAME];
454 hns = HOSTNAME_STATIC;
455
456 /* ... the transient hostname, (ie: DHCP) comes next ... */
457 } else if (transient_hn) {
458 hn = transient_hn;
459 hns = HOSTNAME_TRANSIENT;
460
461 /* ... and the ultimate fallback */
462 } else {
463 hn = _hn_free = get_default_hostname();
464 if (!hn)
465 return log_oom();
466
467 hns = HOSTNAME_DEFAULT;
468 }
469
470 r = sethostname_idempotent(hn);
471 if (r < 0)
472 return log_error_errno(r, "Failed to set hostname: %m");
473
474 if (c->hostname_source != hns) {
475 c->hostname_source = hns;
476 r = 1;
477 }
478
479 (void) nscd_flush_cache(STRV_MAKE("hosts"));
480
481 if (r == 0)
482 log_debug("Hostname was already set to <%s>.", hn);
483 else {
484 log_info("Hostname set to <%s> (%s)", hn, hostname_source_to_string(hns));
485
486 hostname_update_source_hint(hn, hns);
487 }
488
489 return r; /* 0 if no change, 1 if something was done */
490 }
491
492 static void unset_statp(struct stat **p) {
493 if (!*p)
494 return;
495
496 **p = (struct stat) {};
497 }
498
499 static int context_write_data_static_hostname(Context *c) {
500 _cleanup_(unset_statp) struct stat *s = NULL;
501 int r;
502
503 assert(c);
504
505 /* Make sure that if we fail here, we invalidate the cached information, since it was updated
506 * already, even if we can't make it hit the disk. */
507 s = &c->etc_hostname_stat;
508
509 if (isempty(c->data[PROP_STATIC_HOSTNAME])) {
510 if (unlink("/etc/hostname") < 0 && errno != ENOENT)
511 return -errno;
512
513 TAKE_PTR(s);
514 return 0;
515 }
516
517 r = write_string_file_atomic_label("/etc/hostname", c->data[PROP_STATIC_HOSTNAME]);
518 if (r < 0)
519 return r;
520
521 TAKE_PTR(s);
522 return 0;
523 }
524
525 static int context_write_data_machine_info(Context *c) {
526 _cleanup_(unset_statp) struct stat *s = NULL;
527 static const char * const name[_PROP_MAX] = {
528 [PROP_PRETTY_HOSTNAME] = "PRETTY_HOSTNAME",
529 [PROP_ICON_NAME] = "ICON_NAME",
530 [PROP_CHASSIS] = "CHASSIS",
531 [PROP_DEPLOYMENT] = "DEPLOYMENT",
532 [PROP_LOCATION] = "LOCATION",
533 };
534 _cleanup_strv_free_ char **l = NULL;
535 int r;
536
537 assert(c);
538
539 /* Make sure that if we fail here, we invalidate the cached information, since it was updated
540 * already, even if we can't make it hit the disk. */
541 s = &c->etc_machine_info_stat;
542
543 r = load_env_file(NULL, "/etc/machine-info", &l);
544 if (r < 0 && r != -ENOENT)
545 return r;
546
547 for (int p = PROP_PRETTY_HOSTNAME; p <= PROP_LOCATION; p++) {
548 assert(name[p]);
549
550 r = strv_env_assign(&l, name[p], empty_to_null(c->data[p]));
551 if (r < 0)
552 return r;
553 }
554
555 if (strv_isempty(l)) {
556 if (unlink("/etc/machine-info") < 0 && errno != ENOENT)
557 return -errno;
558
559 TAKE_PTR(s);
560 return 0;
561 }
562
563 r = write_env_file_label("/etc/machine-info", l);
564 if (r < 0)
565 return r;
566
567 TAKE_PTR(s);
568 return 0;
569 }
570
571 static int property_get_hardware_property(
572 sd_bus_message *reply,
573 Context *c,
574 HostProperty prop,
575 int (*getter)(char **)) {
576
577 _cleanup_free_ char *from_dmi = NULL;
578
579 assert(reply);
580 assert(c);
581 assert(IN_SET(prop, PROP_HARDWARE_VENDOR, PROP_HARDWARE_MODEL));
582 assert(getter);
583
584 context_read_machine_info(c);
585
586 if (isempty(c->data[prop]))
587 (void) getter(&from_dmi);
588
589 return sd_bus_message_append(reply, "s", from_dmi ?: c->data[prop]);
590 }
591
592 static int property_get_hardware_vendor(
593 sd_bus *bus,
594 const char *path,
595 const char *interface,
596 const char *property,
597 sd_bus_message *reply,
598 void *userdata,
599 sd_bus_error *error) {
600
601 return property_get_hardware_property(reply, userdata, PROP_HARDWARE_VENDOR, get_hardware_vendor);
602 }
603
604 static int property_get_hardware_model(
605 sd_bus *bus,
606 const char *path,
607 const char *interface,
608 const char *property,
609 sd_bus_message *reply,
610 void *userdata,
611 sd_bus_error *error) {
612
613 return property_get_hardware_property(reply, userdata, PROP_HARDWARE_MODEL, get_hardware_model);
614 }
615
616 static int property_get_firmware_version(
617 sd_bus *bus,
618 const char *path,
619 const char *interface,
620 const char *property,
621 sd_bus_message *reply,
622 void *userdata,
623 sd_bus_error *error) {
624
625 _cleanup_free_ char *firmware_version = NULL;
626
627 (void) get_firmware_version(&firmware_version);
628
629 return sd_bus_message_append(reply, "s", firmware_version);
630 }
631
632 static int property_get_hostname(
633 sd_bus *bus,
634 const char *path,
635 const char *interface,
636 const char *property,
637 sd_bus_message *reply,
638 void *userdata,
639 sd_bus_error *error) {
640
641 _cleanup_free_ char *hn = NULL;
642 int r;
643
644 r = gethostname_strict(&hn);
645 if (r < 0) {
646 if (r != -ENXIO)
647 return r;
648
649 hn = get_default_hostname();
650 if (!hn)
651 return -ENOMEM;
652 }
653
654 return sd_bus_message_append(reply, "s", hn);
655 }
656
657 static int property_get_static_hostname(
658 sd_bus *bus,
659 const char *path,
660 const char *interface,
661 const char *property,
662 sd_bus_message *reply,
663 void *userdata,
664 sd_bus_error *error) {
665
666 Context *c = ASSERT_PTR(userdata);
667
668 context_read_etc_hostname(c);
669
670 return sd_bus_message_append(reply, "s", c->data[PROP_STATIC_HOSTNAME]);
671 }
672
673 static int property_get_default_hostname(
674 sd_bus *bus,
675 const char *path,
676 const char *interface,
677 const char *property,
678 sd_bus_message *reply,
679 void *userdata,
680 sd_bus_error *error) {
681
682 _cleanup_free_ char *hn = NULL;
683
684 hn = get_default_hostname();
685 if (!hn)
686 return log_oom();
687
688 return sd_bus_message_append(reply, "s", hn);
689 }
690
691 static void context_determine_hostname_source(Context *c) {
692 _cleanup_free_ char *hostname = NULL;
693 int r;
694
695 assert(c);
696
697 if (c->hostname_source >= 0)
698 return;
699
700 (void) gethostname_full(GET_HOSTNAME_ALLOW_LOCALHOST, &hostname);
701
702 if (streq_ptr(hostname, c->data[PROP_STATIC_HOSTNAME]))
703 c->hostname_source = HOSTNAME_STATIC;
704 else {
705 _cleanup_free_ char *fallback = NULL;
706
707 /* If the hostname was not set by us, try to figure out where it came from. If we set it to
708 * the default hostname, the file will tell us. We compare the string because it is possible
709 * that the hostname was set by an older version that had a different fallback, in the
710 * initramfs or before we reexecuted. */
711
712 r = read_one_line_file("/run/systemd/default-hostname", &fallback);
713 if (r < 0 && r != -ENOENT)
714 log_warning_errno(r, "Failed to read /run/systemd/default-hostname, ignoring: %m");
715
716 if (streq_ptr(fallback, hostname))
717 c->hostname_source = HOSTNAME_DEFAULT;
718 else
719 c->hostname_source = HOSTNAME_TRANSIENT;
720 }
721 }
722
723 static int property_get_hostname_source(
724 sd_bus *bus,
725 const char *path,
726 const char *interface,
727 const char *property,
728 sd_bus_message *reply,
729 void *userdata,
730 sd_bus_error *error) {
731
732 Context *c = ASSERT_PTR(userdata);
733
734 context_read_etc_hostname(c);
735 context_determine_hostname_source(c);
736
737 return sd_bus_message_append(reply, "s", hostname_source_to_string(c->hostname_source));
738 }
739
740 static int property_get_machine_info_field(
741 sd_bus *bus,
742 const char *path,
743 const char *interface,
744 const char *property,
745 sd_bus_message *reply,
746 void *userdata,
747 sd_bus_error *error) {
748
749 sd_bus_slot *slot;
750 Context *c;
751
752 /* Acquire the context object without this property's userdata offset added. Explanation: we want
753 * access to two pointers here: a) the main context object we cache all properties in, and b) the
754 * pointer to the property field inside the context object that we are supposed to update and
755 * use. The latter (b) we get in the 'userdata' function parameter, and sd-bus calculates that for us
756 * from the 'userdata' pointer we supplied when the vtable was registered, with the offset we
757 * specified in the vtable added on top. To get the former (a) we need the 'userdata' pointer from
758 * the vtable registration directly, without the offset added. Hence we ask sd-bus what the slot
759 * object is (which encapsulates the vtable registration), and then query the 'userdata' field
760 * directly off it. */
761 assert_se(slot = sd_bus_get_current_slot(bus));
762 assert_se(c = sd_bus_slot_get_userdata(slot));
763
764 context_read_machine_info(c);
765
766 return sd_bus_message_append(reply, "s", *(char**) userdata);
767 }
768
769 static int property_get_os_release_field(
770 sd_bus *bus,
771 const char *path,
772 const char *interface,
773 const char *property,
774 sd_bus_message *reply,
775 void *userdata,
776 sd_bus_error *error) {
777
778 sd_bus_slot *slot;
779 Context *c;
780
781 /* As above, acquire the current context without this property's userdata offset added. */
782 assert_se(slot = sd_bus_get_current_slot(bus));
783 assert_se(c = sd_bus_slot_get_userdata(slot));
784
785 context_read_os_release(c);
786
787 return sd_bus_message_append(reply, "s", *(char**) userdata);
788 }
789
790 static int property_get_icon_name(
791 sd_bus *bus,
792 const char *path,
793 const char *interface,
794 const char *property,
795 sd_bus_message *reply,
796 void *userdata,
797 sd_bus_error *error) {
798
799 _cleanup_free_ char *n = NULL;
800 Context *c = userdata;
801 const char *name;
802
803 context_read_machine_info(c);
804
805 if (isempty(c->data[PROP_ICON_NAME]))
806 name = n = context_fallback_icon_name(c);
807 else
808 name = c->data[PROP_ICON_NAME];
809
810 if (!name)
811 return -ENOMEM;
812
813 return sd_bus_message_append(reply, "s", name);
814 }
815
816 static int property_get_chassis(
817 sd_bus *bus,
818 const char *path,
819 const char *interface,
820 const char *property,
821 sd_bus_message *reply,
822 void *userdata,
823 sd_bus_error *error) {
824
825 _cleanup_free_ char *chassis = NULL;
826 Context *c = userdata;
827
828 context_read_machine_info(c);
829
830 chassis = context_get_chassis(c);
831
832 return sd_bus_message_append(reply, "s", chassis);
833 }
834
835 static int property_get_uname_field(
836 sd_bus *bus,
837 const char *path,
838 const char *interface,
839 const char *property,
840 sd_bus_message *reply,
841 void *userdata,
842 sd_bus_error *error) {
843
844 struct utsname u;
845
846 assert_se(uname(&u) >= 0);
847
848 return sd_bus_message_append(reply, "s", (char*) &u + PTR_TO_SIZE(userdata));
849 }
850
851 static int method_set_hostname(sd_bus_message *m, void *userdata, sd_bus_error *error) {
852 Context *c = ASSERT_PTR(userdata);
853 const char *name;
854 int interactive, r;
855
856 assert(m);
857
858 r = sd_bus_message_read(m, "sb", &name, &interactive);
859 if (r < 0)
860 return r;
861
862 name = empty_to_null(name);
863
864 /* We always go through with the procedure below without comparing to the current hostname, because
865 * we might want to adjust hostname source information even if the actual hostname is unchanged. */
866
867 if (name && !hostname_is_valid(name, 0))
868 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid hostname '%s'", name);
869
870 context_read_etc_hostname(c);
871
872 r = bus_verify_polkit_async(
873 m,
874 CAP_SYS_ADMIN,
875 "org.freedesktop.hostname1.set-hostname",
876 NULL,
877 interactive,
878 UID_INVALID,
879 &c->polkit_registry,
880 error);
881 if (r < 0)
882 return r;
883 if (r == 0)
884 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
885
886 r = context_update_kernel_hostname(c, name);
887 if (r < 0)
888 return sd_bus_error_set_errnof(error, r, "Failed to set hostname: %m");
889 else if (r > 0)
890 (void) sd_bus_emit_properties_changed(sd_bus_message_get_bus(m),
891 "/org/freedesktop/hostname1", "org.freedesktop.hostname1",
892 "Hostname", "HostnameSource", NULL);
893
894 return sd_bus_reply_method_return(m, NULL);
895 }
896
897 static int method_set_static_hostname(sd_bus_message *m, void *userdata, sd_bus_error *error) {
898 Context *c = ASSERT_PTR(userdata);
899 const char *name;
900 int interactive;
901 int r;
902
903 assert(m);
904
905 r = sd_bus_message_read(m, "sb", &name, &interactive);
906 if (r < 0)
907 return r;
908
909 name = empty_to_null(name);
910
911 context_read_etc_hostname(c);
912
913 if (streq_ptr(name, c->data[PROP_STATIC_HOSTNAME]))
914 return sd_bus_reply_method_return(m, NULL);
915
916 if (name && !hostname_is_valid(name, 0))
917 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid static hostname '%s'", name);
918
919 r = bus_verify_polkit_async(
920 m,
921 CAP_SYS_ADMIN,
922 "org.freedesktop.hostname1.set-static-hostname",
923 NULL,
924 interactive,
925 UID_INVALID,
926 &c->polkit_registry,
927 error);
928 if (r < 0)
929 return r;
930 if (r == 0)
931 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
932
933 r = free_and_strdup_warn(&c->data[PROP_STATIC_HOSTNAME], name);
934 if (r < 0)
935 return r;
936
937 r = context_write_data_static_hostname(c);
938 if (r < 0) {
939 log_error_errno(r, "Failed to write static hostname: %m");
940 if (ERRNO_IS_PRIVILEGE(r))
941 return sd_bus_error_set(error, BUS_ERROR_FILE_IS_PROTECTED, "Not allowed to update /etc/hostname.");
942 if (r == -EROFS)
943 return sd_bus_error_set(error, BUS_ERROR_READ_ONLY_FILESYSTEM, "/etc/hostname is in a read-only filesystem.");
944 return sd_bus_error_set_errnof(error, r, "Failed to set static hostname: %m");
945 }
946
947 r = context_update_kernel_hostname(c, NULL);
948 if (r < 0) {
949 log_error_errno(r, "Failed to set hostname: %m");
950 return sd_bus_error_set_errnof(error, r, "Failed to set hostname: %m");
951 }
952
953 (void) sd_bus_emit_properties_changed(sd_bus_message_get_bus(m),
954 "/org/freedesktop/hostname1", "org.freedesktop.hostname1",
955 "StaticHostname", "Hostname", "HostnameSource", NULL);
956
957 return sd_bus_reply_method_return(m, NULL);
958 }
959
960 static int set_machine_info(Context *c, sd_bus_message *m, int prop, sd_bus_message_handler_t cb, sd_bus_error *error) {
961 int interactive;
962 const char *name;
963 int r;
964
965 assert(c);
966 assert(m);
967
968 r = sd_bus_message_read(m, "sb", &name, &interactive);
969 if (r < 0)
970 return r;
971
972 name = empty_to_null(name);
973
974 context_read_machine_info(c);
975
976 if (streq_ptr(name, c->data[prop]))
977 return sd_bus_reply_method_return(m, NULL);
978
979 if (!isempty(name)) {
980 /* The icon name might ultimately be used as file
981 * name, so better be safe than sorry */
982
983 if (prop == PROP_ICON_NAME && !filename_is_valid(name))
984 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid icon name '%s'", name);
985 if (prop == PROP_PRETTY_HOSTNAME && string_has_cc(name, NULL))
986 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid pretty hostname '%s'", name);
987 if (prop == PROP_CHASSIS && !valid_chassis(name))
988 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid chassis '%s'", name);
989 if (prop == PROP_DEPLOYMENT && !valid_deployment(name))
990 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid deployment '%s'", name);
991 if (prop == PROP_LOCATION && string_has_cc(name, NULL))
992 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid location '%s'", name);
993 }
994
995 /* Since the pretty hostname should always be changed at the
996 * same time as the static one, use the same policy action for
997 * both... */
998
999 r = bus_verify_polkit_async(
1000 m,
1001 CAP_SYS_ADMIN,
1002 prop == PROP_PRETTY_HOSTNAME ? "org.freedesktop.hostname1.set-static-hostname" : "org.freedesktop.hostname1.set-machine-info",
1003 NULL,
1004 interactive,
1005 UID_INVALID,
1006 &c->polkit_registry,
1007 error);
1008 if (r < 0)
1009 return r;
1010 if (r == 0)
1011 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1012
1013 r = free_and_strdup_warn(&c->data[prop], name);
1014 if (r < 0)
1015 return r;
1016
1017 r = context_write_data_machine_info(c);
1018 if (r < 0) {
1019 log_error_errno(r, "Failed to write machine info: %m");
1020 if (ERRNO_IS_PRIVILEGE(r))
1021 return sd_bus_error_set(error, BUS_ERROR_FILE_IS_PROTECTED, "Not allowed to update /etc/machine-info.");
1022 if (r == -EROFS)
1023 return sd_bus_error_set(error, BUS_ERROR_READ_ONLY_FILESYSTEM, "/etc/machine-info is in a read-only filesystem.");
1024 return sd_bus_error_set_errnof(error, r, "Failed to write machine info: %m");
1025 }
1026
1027 log_info("Changed %s to '%s'",
1028 prop == PROP_PRETTY_HOSTNAME ? "pretty hostname" :
1029 prop == PROP_DEPLOYMENT ? "deployment" :
1030 prop == PROP_LOCATION ? "location" :
1031 prop == PROP_CHASSIS ? "chassis" : "icon name", strna(c->data[prop]));
1032
1033 (void) sd_bus_emit_properties_changed(
1034 sd_bus_message_get_bus(m),
1035 "/org/freedesktop/hostname1",
1036 "org.freedesktop.hostname1",
1037 prop == PROP_PRETTY_HOSTNAME ? "PrettyHostname" :
1038 prop == PROP_DEPLOYMENT ? "Deployment" :
1039 prop == PROP_LOCATION ? "Location" :
1040 prop == PROP_CHASSIS ? "Chassis" : "IconName" , NULL);
1041
1042 return sd_bus_reply_method_return(m, NULL);
1043 }
1044
1045 static int method_set_pretty_hostname(sd_bus_message *m, void *userdata, sd_bus_error *error) {
1046 return set_machine_info(userdata, m, PROP_PRETTY_HOSTNAME, method_set_pretty_hostname, error);
1047 }
1048
1049 static int method_set_icon_name(sd_bus_message *m, void *userdata, sd_bus_error *error) {
1050 return set_machine_info(userdata, m, PROP_ICON_NAME, method_set_icon_name, error);
1051 }
1052
1053 static int method_set_chassis(sd_bus_message *m, void *userdata, sd_bus_error *error) {
1054 return set_machine_info(userdata, m, PROP_CHASSIS, method_set_chassis, error);
1055 }
1056
1057 static int method_set_deployment(sd_bus_message *m, void *userdata, sd_bus_error *error) {
1058 return set_machine_info(userdata, m, PROP_DEPLOYMENT, method_set_deployment, error);
1059 }
1060
1061 static int method_set_location(sd_bus_message *m, void *userdata, sd_bus_error *error) {
1062 return set_machine_info(userdata, m, PROP_LOCATION, method_set_location, error);
1063 }
1064
1065 static int method_get_product_uuid(sd_bus_message *m, void *userdata, sd_bus_error *error) {
1066 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
1067 Context *c = ASSERT_PTR(userdata);
1068 int interactive, r;
1069 sd_id128_t uuid;
1070
1071 assert(m);
1072
1073 r = sd_bus_message_read(m, "b", &interactive);
1074 if (r < 0)
1075 return r;
1076
1077 r = bus_verify_polkit_async(
1078 m,
1079 CAP_SYS_ADMIN,
1080 "org.freedesktop.hostname1.get-product-uuid",
1081 NULL,
1082 interactive,
1083 UID_INVALID,
1084 &c->polkit_registry,
1085 error);
1086 if (r < 0)
1087 return r;
1088 if (r == 0)
1089 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1090
1091 r = id128_get_product(&uuid);
1092 if (r < 0) {
1093 if (r == -EADDRNOTAVAIL)
1094 log_debug_errno(r, "DMI product UUID is all 0x00 or all 0xFF, ignoring.");
1095 else
1096 log_full_errno(r == -ENOENT ? LOG_DEBUG : LOG_WARNING, r,
1097 "Failed to read product UUID, ignoring: %m");
1098
1099 return sd_bus_error_set(error, BUS_ERROR_NO_PRODUCT_UUID,
1100 "Failed to read product UUID from firmware.");
1101 }
1102
1103 r = sd_bus_message_new_method_return(m, &reply);
1104 if (r < 0)
1105 return r;
1106
1107 r = sd_bus_message_append_array(reply, 'y', uuid.bytes, sizeof(uuid.bytes));
1108 if (r < 0)
1109 return r;
1110
1111 return sd_bus_send(NULL, reply, NULL);
1112 }
1113
1114 static int method_get_hardware_serial(sd_bus_message *m, void *userdata, sd_bus_error *error) {
1115 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
1116 _cleanup_free_ char *serial = NULL;
1117 Context *c = ASSERT_PTR(userdata);
1118 int r;
1119
1120 assert(m);
1121
1122 r = bus_verify_polkit_async(
1123 m,
1124 CAP_SYS_ADMIN,
1125 "org.freedesktop.hostname1.get-hardware-serial",
1126 NULL,
1127 false,
1128 UID_INVALID,
1129 &c->polkit_registry,
1130 error);
1131 if (r < 0)
1132 return r;
1133 if (r == 0)
1134 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1135
1136 r = get_hardware_serial(&serial);
1137 if (r < 0)
1138 return r;
1139
1140 r = sd_bus_message_new_method_return(m, &reply);
1141 if (r < 0)
1142 return r;
1143
1144 r = sd_bus_message_append(reply, "s", serial);
1145 if (r < 0)
1146 return r;
1147
1148 return sd_bus_send(NULL, reply, NULL);
1149 }
1150
1151 static int method_describe(sd_bus_message *m, void *userdata, sd_bus_error *error) {
1152 _cleanup_free_ char *hn = NULL, *dhn = NULL, *in = NULL, *text = NULL,
1153 *chassis = NULL, *vendor = NULL, *model = NULL, *serial = NULL, *firmware_version = NULL;
1154 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
1155 _cleanup_(json_variant_unrefp) JsonVariant *v = NULL;
1156 sd_id128_t product_uuid = SD_ID128_NULL;
1157 Context *c = ASSERT_PTR(userdata);
1158 bool privileged;
1159 struct utsname u;
1160 int r;
1161
1162 assert(m);
1163
1164 r = bus_verify_polkit_async(
1165 m,
1166 CAP_SYS_ADMIN,
1167 "org.freedesktop.hostname1.get-description",
1168 NULL,
1169 false,
1170 UID_INVALID,
1171 &c->polkit_registry,
1172 NULL);
1173 if (r == 0)
1174 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1175
1176 /* We ignore all authentication errors here, since most data is unprivileged, the one exception being
1177 * the product ID which we'll check explicitly. */
1178 privileged = r > 0;
1179
1180 context_read_etc_hostname(c);
1181 context_read_machine_info(c);
1182 context_read_os_release(c);
1183 context_determine_hostname_source(c);
1184
1185 r = gethostname_strict(&hn);
1186 if (r < 0) {
1187 if (r != -ENXIO)
1188 return log_error_errno(r, "Failed to read local host name: %m");
1189
1190 hn = get_default_hostname();
1191 if (!hn)
1192 return log_oom();
1193 }
1194
1195 dhn = get_default_hostname();
1196 if (!dhn)
1197 return log_oom();
1198
1199 if (isempty(c->data[PROP_ICON_NAME]))
1200 in = context_fallback_icon_name(c);
1201
1202 chassis = context_get_chassis(c);
1203
1204 assert_se(uname(&u) >= 0);
1205
1206 if (isempty(c->data[PROP_HARDWARE_VENDOR]))
1207 (void) get_hardware_vendor(&vendor);
1208 if (isempty(c->data[PROP_HARDWARE_MODEL]))
1209 (void) get_hardware_model(&model);
1210
1211 if (privileged) {
1212 /* The product UUID and hardware serial is only available to privileged clients */
1213 (void) id128_get_product(&product_uuid);
1214 (void) get_hardware_serial(&serial);
1215 }
1216 (void) get_firmware_version(&firmware_version);
1217
1218 r = json_build(&v, JSON_BUILD_OBJECT(
1219 JSON_BUILD_PAIR("Hostname", JSON_BUILD_STRING(hn)),
1220 JSON_BUILD_PAIR("StaticHostname", JSON_BUILD_STRING(c->data[PROP_STATIC_HOSTNAME])),
1221 JSON_BUILD_PAIR("PrettyHostname", JSON_BUILD_STRING(c->data[PROP_PRETTY_HOSTNAME])),
1222 JSON_BUILD_PAIR("DefaultHostname", JSON_BUILD_STRING(dhn)),
1223 JSON_BUILD_PAIR("HostnameSource", JSON_BUILD_STRING(hostname_source_to_string(c->hostname_source))),
1224 JSON_BUILD_PAIR("IconName", JSON_BUILD_STRING(in ?: c->data[PROP_ICON_NAME])),
1225 JSON_BUILD_PAIR("Chassis", JSON_BUILD_STRING(chassis)),
1226 JSON_BUILD_PAIR("Deployment", JSON_BUILD_STRING(c->data[PROP_DEPLOYMENT])),
1227 JSON_BUILD_PAIR("Location", JSON_BUILD_STRING(c->data[PROP_LOCATION])),
1228 JSON_BUILD_PAIR("KernelName", JSON_BUILD_STRING(u.sysname)),
1229 JSON_BUILD_PAIR("KernelRelease", JSON_BUILD_STRING(u.release)),
1230 JSON_BUILD_PAIR("KernelVersion", JSON_BUILD_STRING(u.version)),
1231 JSON_BUILD_PAIR("OperatingSystemPrettyName", JSON_BUILD_STRING(c->data[PROP_OS_PRETTY_NAME])),
1232 JSON_BUILD_PAIR("OperatingSystemCPEName", JSON_BUILD_STRING(c->data[PROP_OS_CPE_NAME])),
1233 JSON_BUILD_PAIR("OperatingSystemHomeURL", JSON_BUILD_STRING(c->data[PROP_OS_HOME_URL])),
1234 JSON_BUILD_PAIR("HardwareVendor", JSON_BUILD_STRING(vendor ?: c->data[PROP_HARDWARE_VENDOR])),
1235 JSON_BUILD_PAIR("HardwareModel", JSON_BUILD_STRING(model ?: c->data[PROP_HARDWARE_MODEL])),
1236 JSON_BUILD_PAIR("HardwareSerial", JSON_BUILD_STRING(serial)),
1237 JSON_BUILD_PAIR("FirmwareVersion", JSON_BUILD_STRING(firmware_version)),
1238 JSON_BUILD_PAIR_CONDITION(!sd_id128_is_null(product_uuid), "ProductUUID", JSON_BUILD_ID128(product_uuid)),
1239 JSON_BUILD_PAIR_CONDITION(sd_id128_is_null(product_uuid), "ProductUUID", JSON_BUILD_NULL)));
1240
1241 if (r < 0)
1242 return log_error_errno(r, "Failed to build JSON data: %m");
1243
1244 r = json_variant_format(v, 0, &text);
1245 if (r < 0)
1246 return log_error_errno(r, "Failed to format JSON data: %m");
1247
1248 r = sd_bus_message_new_method_return(m, &reply);
1249 if (r < 0)
1250 return r;
1251
1252 r = sd_bus_message_append(reply, "s", text);
1253 if (r < 0)
1254 return r;
1255
1256 return sd_bus_send(NULL, reply, NULL);
1257 }
1258
1259 static const sd_bus_vtable hostname_vtable[] = {
1260 SD_BUS_VTABLE_START(0),
1261 SD_BUS_PROPERTY("Hostname", "s", property_get_hostname, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
1262 SD_BUS_PROPERTY("StaticHostname", "s", property_get_static_hostname, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
1263 SD_BUS_PROPERTY("PrettyHostname", "s", property_get_machine_info_field, offsetof(Context, data) + sizeof(char*) * PROP_PRETTY_HOSTNAME, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
1264 SD_BUS_PROPERTY("DefaultHostname", "s", property_get_default_hostname, 0, SD_BUS_VTABLE_PROPERTY_CONST),
1265 SD_BUS_PROPERTY("HostnameSource", "s", property_get_hostname_source, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
1266 SD_BUS_PROPERTY("IconName", "s", property_get_icon_name, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
1267 SD_BUS_PROPERTY("Chassis", "s", property_get_chassis, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
1268 SD_BUS_PROPERTY("Deployment", "s", property_get_machine_info_field, offsetof(Context, data) + sizeof(char*) * PROP_DEPLOYMENT, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
1269 SD_BUS_PROPERTY("Location", "s", property_get_machine_info_field, offsetof(Context, data) + sizeof(char*) * PROP_LOCATION, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
1270 SD_BUS_PROPERTY("KernelName", "s", property_get_uname_field, offsetof(struct utsname, sysname), SD_BUS_VTABLE_ABSOLUTE_OFFSET|SD_BUS_VTABLE_PROPERTY_CONST),
1271 SD_BUS_PROPERTY("KernelRelease", "s", property_get_uname_field, offsetof(struct utsname, release), SD_BUS_VTABLE_ABSOLUTE_OFFSET|SD_BUS_VTABLE_PROPERTY_CONST),
1272 SD_BUS_PROPERTY("KernelVersion", "s", property_get_uname_field, offsetof(struct utsname, version), SD_BUS_VTABLE_ABSOLUTE_OFFSET|SD_BUS_VTABLE_PROPERTY_CONST),
1273 SD_BUS_PROPERTY("OperatingSystemPrettyName", "s", property_get_os_release_field, offsetof(Context, data) + sizeof(char*) * PROP_OS_PRETTY_NAME, SD_BUS_VTABLE_PROPERTY_CONST),
1274 SD_BUS_PROPERTY("OperatingSystemCPEName", "s", property_get_os_release_field, offsetof(Context, data) + sizeof(char*) * PROP_OS_CPE_NAME, SD_BUS_VTABLE_PROPERTY_CONST),
1275 SD_BUS_PROPERTY("HomeURL", "s", property_get_os_release_field, offsetof(Context, data) + sizeof(char*) * PROP_OS_HOME_URL, SD_BUS_VTABLE_PROPERTY_CONST),
1276 SD_BUS_PROPERTY("HardwareVendor", "s", property_get_hardware_vendor, 0, SD_BUS_VTABLE_PROPERTY_CONST),
1277 SD_BUS_PROPERTY("HardwareModel", "s", property_get_hardware_model, 0, SD_BUS_VTABLE_PROPERTY_CONST),
1278 SD_BUS_PROPERTY("FirmwareVersion", "s", property_get_firmware_version, 0, SD_BUS_VTABLE_PROPERTY_CONST),
1279
1280 SD_BUS_METHOD_WITH_ARGS("SetHostname",
1281 SD_BUS_ARGS("s", hostname, "b", interactive),
1282 SD_BUS_NO_RESULT,
1283 method_set_hostname,
1284 SD_BUS_VTABLE_UNPRIVILEGED),
1285 SD_BUS_METHOD_WITH_ARGS("SetStaticHostname",
1286 SD_BUS_ARGS("s", hostname, "b", interactive),
1287 SD_BUS_NO_RESULT,
1288 method_set_static_hostname,
1289 SD_BUS_VTABLE_UNPRIVILEGED),
1290 SD_BUS_METHOD_WITH_ARGS("SetPrettyHostname",
1291 SD_BUS_ARGS("s", hostname, "b", interactive),
1292 SD_BUS_NO_RESULT,
1293 method_set_pretty_hostname,
1294 SD_BUS_VTABLE_UNPRIVILEGED),
1295 SD_BUS_METHOD_WITH_ARGS("SetIconName",
1296 SD_BUS_ARGS("s", icon, "b", interactive),
1297 SD_BUS_NO_RESULT,
1298 method_set_icon_name,
1299 SD_BUS_VTABLE_UNPRIVILEGED),
1300 SD_BUS_METHOD_WITH_ARGS("SetChassis",
1301 SD_BUS_ARGS("s", chassis, "b", interactive),
1302 SD_BUS_NO_RESULT,
1303 method_set_chassis,
1304 SD_BUS_VTABLE_UNPRIVILEGED),
1305 SD_BUS_METHOD_WITH_ARGS("SetDeployment",
1306 SD_BUS_ARGS("s", deployment, "b", interactive),
1307 SD_BUS_NO_RESULT,
1308 method_set_deployment,
1309 SD_BUS_VTABLE_UNPRIVILEGED),
1310 SD_BUS_METHOD_WITH_ARGS("SetLocation",
1311 SD_BUS_ARGS("s", location, "b", interactive),
1312 SD_BUS_NO_RESULT,
1313 method_set_location,
1314 SD_BUS_VTABLE_UNPRIVILEGED),
1315 SD_BUS_METHOD_WITH_ARGS("GetProductUUID",
1316 SD_BUS_ARGS("b", interactive),
1317 SD_BUS_RESULT("ay", uuid),
1318 method_get_product_uuid,
1319 SD_BUS_VTABLE_UNPRIVILEGED),
1320 SD_BUS_METHOD_WITH_ARGS("GetHardwareSerial",
1321 SD_BUS_NO_ARGS,
1322 SD_BUS_RESULT("s", serial),
1323 method_get_hardware_serial,
1324 SD_BUS_VTABLE_UNPRIVILEGED),
1325 SD_BUS_METHOD_WITH_ARGS("Describe",
1326 SD_BUS_NO_ARGS,
1327 SD_BUS_RESULT("s", json),
1328 method_describe,
1329 SD_BUS_VTABLE_UNPRIVILEGED),
1330
1331 SD_BUS_VTABLE_END,
1332 };
1333
1334 static const BusObjectImplementation manager_object = {
1335 "/org/freedesktop/hostname1",
1336 "org.freedesktop.hostname1",
1337 .vtables = BUS_VTABLES(hostname_vtable),
1338 };
1339
1340 static int connect_bus(Context *c, sd_event *event, sd_bus **ret) {
1341 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
1342 int r;
1343
1344 assert(c);
1345 assert(event);
1346 assert(ret);
1347
1348 r = sd_bus_default_system(&bus);
1349 if (r < 0)
1350 return log_error_errno(r, "Failed to get system bus connection: %m");
1351
1352 r = bus_add_implementation(bus, &manager_object, c);
1353 if (r < 0)
1354 return r;
1355
1356 r = bus_log_control_api_register(bus);
1357 if (r < 0)
1358 return r;
1359
1360 r = sd_bus_request_name_async(bus, NULL, "org.freedesktop.hostname1", 0, NULL, NULL);
1361 if (r < 0)
1362 return log_error_errno(r, "Failed to request name: %m");
1363
1364 r = sd_bus_attach_event(bus, event, 0);
1365 if (r < 0)
1366 return log_error_errno(r, "Failed to attach bus to event loop: %m");
1367
1368 *ret = TAKE_PTR(bus);
1369 return 0;
1370 }
1371
1372 static int run(int argc, char *argv[]) {
1373 _cleanup_(context_destroy) Context context = {
1374 .hostname_source = _HOSTNAME_INVALID, /* appropriate value will be set later */
1375 };
1376 _cleanup_(sd_event_unrefp) sd_event *event = NULL;
1377 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
1378 int r;
1379
1380 log_setup();
1381
1382 r = service_parse_argv("systemd-hostnamed.service",
1383 "Manage the system hostname and related metadata.",
1384 BUS_IMPLEMENTATIONS(&manager_object,
1385 &log_control_object),
1386 argc, argv);
1387 if (r <= 0)
1388 return r;
1389
1390 umask(0022);
1391
1392 r = mac_selinux_init();
1393 if (r < 0)
1394 return r;
1395
1396 assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGTERM, SIGINT, -1) >= 0);
1397
1398 r = sd_event_default(&event);
1399 if (r < 0)
1400 return log_error_errno(r, "Failed to allocate event loop: %m");
1401
1402 (void) sd_event_set_watchdog(event, true);
1403
1404 r = sd_event_add_signal(event, NULL, SIGINT, NULL, NULL);
1405 if (r < 0)
1406 return log_error_errno(r, "Failed to install SIGINT handler: %m");
1407
1408 r = sd_event_add_signal(event, NULL, SIGTERM, NULL, NULL);
1409 if (r < 0)
1410 return log_error_errno(r, "Failed to install SIGTERM handler: %m");
1411
1412 r = connect_bus(&context, event, &bus);
1413 if (r < 0)
1414 return r;
1415
1416 r = bus_event_loop_with_idle(event, bus, "org.freedesktop.hostname1", DEFAULT_EXIT_USEC, NULL, NULL);
1417 if (r < 0)
1418 return log_error_errno(r, "Failed to run event loop: %m");
1419
1420 return 0;
1421 }
1422
1423 DEFINE_MAIN_FUNCTION(run);