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