+static int setup_journal(const char *directory) {
+ sd_id128_t machine_id;
+ char *p = NULL, *b = NULL, *l, *q = NULL, *d = NULL;
+ int r;
+
+ if (arg_link_journal == LINK_NO)
+ return 0;
+
+ p = strappend(directory, "/etc/machine-id");
+ if (!p) {
+ r = log_oom();
+ goto finish;
+ }
+
+ r = read_one_line_file(p, &b);
+ if (r == -ENOENT && arg_link_journal == LINK_AUTO) {
+ r = 0;
+ goto finish;
+ } else if (r < 0) {
+ log_error("Failed to read machine ID: %s", strerror(-r));
+ return r;
+ }
+
+ l = strstrip(b);
+ if (isempty(l) && arg_link_journal == LINK_AUTO) {
+ r = 0;
+ goto finish;
+ }
+
+ /* Verify validaty */
+ r = sd_id128_from_string(l, &machine_id);
+ if (r < 0) {
+ log_error("Failed to parse machine ID: %s", strerror(-r));
+ goto finish;
+ }
+
+ free(p);
+ p = strappend("/var/log/journal/", l);
+ q = strjoin(directory, "/var/log/journal/", l, NULL);
+ if (!p || !q) {
+ r = log_oom();
+ goto finish;
+ }
+
+ if (path_is_mount_point(p, false) > 0 ||
+ path_is_mount_point(q, false) > 0) {
+ if (arg_link_journal != LINK_AUTO) {
+ log_error("Journal already a mount point, refusing.");
+ r = -EEXIST;
+ goto finish;
+ }
+
+ r = 0;
+ goto finish;
+ }
+
+ r = readlink_and_make_absolute(p, &d);
+ if (r >= 0) {
+ if ((arg_link_journal == LINK_GUEST ||
+ arg_link_journal == LINK_AUTO) &&
+ path_equal(d, q)) {
+
+ mkdir_p(q, 0755);
+
+ r = 0;
+ goto finish;
+ }
+
+ if (unlink(p) < 0) {
+ log_error("Failed to remove symlink %s: %m", p);
+ r = -errno;
+ goto finish;
+ }
+ } else if (r == -EINVAL) {