+ *exposed = new_exposed;
+ return 0;
+}
+
+static int on_address_change(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
+ union in_addr_union *exposed = userdata;
+
+ assert(rtnl);
+ assert(m);
+ assert(exposed);
+
+ expose_ports(rtnl, exposed);
+ return 0;
+}
+
+static int watch_rtnl(sd_event *event, int recv_fd, union in_addr_union *exposed, sd_rtnl **ret) {
+ union {
+ struct cmsghdr cmsghdr;
+ uint8_t buf[CMSG_SPACE(sizeof(int))];
+ } control = {};
+ struct msghdr mh = {
+ .msg_control = &control,
+ .msg_controllen = sizeof(control),
+ };
+ struct cmsghdr *cmsg;
+ _cleanup_rtnl_unref_ sd_rtnl *rtnl = NULL;
+ int fd, r;
+ ssize_t k;
+
+ assert(event);
+ assert(recv_fd >= 0);
+ assert(ret);
+
+ if (!arg_expose_ports)
+ return 0;
+
+ k = recvmsg(recv_fd, &mh, MSG_NOSIGNAL);
+ if (k < 0)
+ return log_error_errno(errno, "Failed to recv netlink fd: %m");
+
+ cmsg = CMSG_FIRSTHDR(&mh);
+ assert(cmsg->cmsg_level == SOL_SOCKET);
+ assert(cmsg->cmsg_type == SCM_RIGHTS);
+ assert(cmsg->cmsg_len == CMSG_LEN(sizeof(int)));
+ memcpy(&fd, CMSG_DATA(cmsg), sizeof(int));
+
+ r = sd_rtnl_open_fd(&rtnl, fd, 1, RTNLGRP_IPV4_IFADDR);
+ if (r < 0) {
+ safe_close(fd);
+ return log_error_errno(r, "Failed to create rtnl object: %m");
+ }
+
+ r = sd_rtnl_add_match(rtnl, RTM_NEWADDR, on_address_change, exposed);
+ if (r < 0)
+ return log_error_errno(r, "Failed to subscribe to RTM_NEWADDR messages: %m");
+
+ r = sd_rtnl_add_match(rtnl, RTM_DELADDR, on_address_change, exposed);
+ if (r < 0)
+ return log_error_errno(r, "Failed to subscribe to RTM_DELADDR messages: %m");
+
+ r = sd_rtnl_attach_event(rtnl, event, 0);
+ if (r < 0)
+ return log_error_errno(r, "Failed to add to even loop: %m");
+
+ *ret = rtnl;
+ rtnl = NULL;
+
+ return 0;
+}
+
+static int setup_hostname(void) {
+
+ if (arg_share_system)
+ return 0;
+
+ if (sethostname_idempotent(arg_machine) < 0)
+ return -errno;
+
+ return 0;
+}
+
+static int setup_journal(const char *directory) {
+ sd_id128_t machine_id, this_id;
+ _cleanup_free_ char *p = NULL, *b = NULL, *q = NULL, *d = NULL;
+ char *id;
+ int r;
+
+ /* Don't link journals in ephemeral mode */
+ if (arg_ephemeral)
+ return 0;
+
+ p = strappend(directory, "/etc/machine-id");
+ if (!p)
+ return log_oom();
+
+ r = read_one_line_file(p, &b);
+ if (r == -ENOENT && arg_link_journal == LINK_AUTO)
+ return 0;
+ else if (r < 0)
+ return log_error_errno(r, "Failed to read machine ID from %s: %m", p);
+
+ id = strstrip(b);
+ if (isempty(id) && arg_link_journal == LINK_AUTO)
+ return 0;
+
+ /* Verify validity */
+ r = sd_id128_from_string(id, &machine_id);
+ if (r < 0)
+ return log_error_errno(r, "Failed to parse machine ID from %s: %m", p);
+
+ r = sd_id128_get_machine(&this_id);
+ if (r < 0)
+ return log_error_errno(r, "Failed to retrieve machine ID: %m");
+
+ if (sd_id128_equal(machine_id, this_id)) {
+ log_full(arg_link_journal == LINK_AUTO ? LOG_WARNING : LOG_ERR,
+ "Host and machine ids are equal (%s): refusing to link journals", id);
+ if (arg_link_journal == LINK_AUTO)
+ return 0;
+ return -EEXIST;
+ }
+
+ if (arg_link_journal == LINK_NO)
+ return 0;
+
+ free(p);
+ p = strappend("/var/log/journal/", id);
+ q = strjoin(directory, "/var/log/journal/", id, NULL);
+ if (!p || !q)
+ return log_oom();
+
+ if (path_is_mount_point(p, false) > 0) {
+ if (arg_link_journal != LINK_AUTO) {
+ log_error("%s: already a mount point, refusing to use for journal", p);
+ return -EEXIST;
+ }
+
+ return 0;
+ }
+
+ if (path_is_mount_point(q, false) > 0) {
+ if (arg_link_journal != LINK_AUTO) {
+ log_error("%s: already a mount point, refusing to use for journal", q);
+ return -EEXIST;
+ }
+
+ return 0;
+ }
+
+ r = readlink_and_make_absolute(p, &d);