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