Re: [systemd-devel] sd-bus vs glib object path node hierarchy

2015-06-16 Thread Krzesimir Nowak
16 cze 2015 6:09 PM "Lennart Poettering" 
napisał(a):
>
> On Tue, 16.06.15 15:51, Umut Tezduyar Lindskog (u...@tezduyar.com) wrote:
>
> > Hi,
> >
> > I have noticed that glib vs sd-bus have different hierarchy in terms
> > of how objects are stacked. I don't have any argument why one or the
> > other one would be better but I was wondering what the reason for this
> > difference.
> >
> > "/com/a/b" registered with sd_bus_add_object_vtable
> > Introspection:
> > └─/com/a/b
> >
> > "/com/a/b" registered with glib
> > Introspection:
> > └─/com
> >   └─/com/a
> > └─/com/a/b
>
> Yeah, the spec says nothing about this. It's not clear whether
> "middle" nodes should be synthesized or not for cases like this. I
> decided to keep things minimal for sd-bus, and I think we should stay
> with that unless this turns out to be a real problem for
> something. Note though that the nodes in between actually are
> accessible if you use their path explicitly, they just aren't
> announced in the introspection, that's all.
>
> But maybe Simon has an opinion on this? Simon?

That reminds me of a bug (or not-a-bug) in go-dbus [1] - it also generates
flat hierarchy. This is a problem for tools like d-feet - they fail to
introspect services with flat hierarchy. Of course, it might be a bug in
d-feet itself.

[1] https://github.com/godbus/dbus/issues/14

>
> Lennart
>
> --
> Lennart Poettering, Red Hat
> ___
> systemd-devel mailing list
> systemd-devel@lists.freedesktop.org
> http://lists.freedesktop.org/mailman/listinfo/systemd-devel
___
systemd-devel mailing list
systemd-devel@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/systemd-devel


Re: [systemd-devel] [ANNOUNCE] Git development moved to github

2015-06-03 Thread Krzesimir Nowak
On Mon, Jun 1, 2015 at 8:12 PM, David Herrmann 
wrote:

> Hi
>
> As of today we've disabled git-push to fd.o. The official development
> git repository is now at github [1]. The old repository will still be
> back-synced, but we had to disable push-access to avoid getting
> out-of-sync with github.
>
> In recent months, keeping up with the mailing-list has become more and
> more cumbersome, with many of us missing mails or unable to keep up
> with the traffic. To make sure all community requests and patches will
> get handled in time, we're now trying out the github infrastructure.
> We encourage everyone in the development community to switch over now,
> even though the old fd.o infrastructure will still be maintained.
> Distributions are free to wait until the next release announcement
> before updating anything.
>
> If github does not work out, we will see what else we can try out. But
> lets give it at least a try.
>
> Thanks
> David
>
>
Hi,

I see that some patches from mailing list were imported as issues to
github.com (like this one - https://github.com/systemd/systemd/pull/16).
There's a problem with that - I can't update the PR anymore with followup
fixes and whatnot. What's the workflow in this case? File a new PR and ask
nicely for old one to be deleted?

Thanks,
Krzesimir


> [1] https://github.com/systemd-devs/systemd
> ___
> systemd-devel mailing list
> systemd-devel@lists.freedesktop.org
> http://lists.freedesktop.org/mailman/listinfo/systemd-devel
>
___
systemd-devel mailing list
systemd-devel@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/systemd-devel


[systemd-devel] [PATCH 2/7] nspawn: Close unneeded sockets in outer child

2015-06-01 Thread Krzesimir Nowak
---
 src/nspawn/nspawn.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/src/nspawn/nspawn.c b/src/nspawn/nspawn.c
index bd7532c..28b79c4 100644
--- a/src/nspawn/nspawn.c
+++ b/src/nspawn/nspawn.c
@@ -4348,6 +4348,8 @@ static int outer_child(
 }
 
 pid_socket = safe_close(pid_socket);
+kmsg_socket = safe_close(kmsg_socket);
+rtnl_socket = safe_close(rtnl_socket);
 
 return 0;
 }
-- 
2.1.0

___
systemd-devel mailing list
systemd-devel@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/systemd-devel


[systemd-devel] [PATCH 4/7] machined: Add getter for machine's journal directory descriptor

2015-06-01 Thread Krzesimir Nowak
Sometimes machine's journal is not accessible by path, so we can ask
machined to provide a descriptor to it.
---
 src/machine/machined-dbus.c| 112 +
 src/machine/org.freedesktop.machine1.policy.in |  10 +++
 2 files changed, 122 insertions(+)

diff --git a/src/machine/machined-dbus.c b/src/machine/machined-dbus.c
index 0e971a6..ea5f6f7 100644
--- a/src/machine/machined-dbus.c
+++ b/src/machine/machined-dbus.c
@@ -37,6 +37,7 @@
 #include "machined.h"
 #include "machine-dbus.h"
 #include "formats-util.h"
+#include "process-util.h"
 
 static int property_get_pool_path(
 sd_bus *bus,
@@ -840,6 +841,116 @@ static int method_set_image_limit(sd_bus_message 
*message, void *userdata, sd_bu
 return bus_image_method_set_limit(message, i, error);
 }
 
+static int get_journal_fd_child(int socket_fd, int mntns_fd, int root_fd) {
+_cleanup_close_ int fd = -1;
+int r;
+
+r = namespace_enter(-1, mntns_fd, -1, root_fd);
+if (r < 0)
+return r;
+
+fd = open("/var/log/journal", O_RDONLY|O_CLOEXEC|O_DIRECTORY);
+if (fd < 0)
+return -errno;
+
+r = send_fd(socket_fd, fd);
+return r;
+}
+
+static int get_journal_fd_parent(int socket_fd, pid_t child, sd_bus_error 
*error, int* journal_fd) {
+int r;
+siginfo_t si;
+
+r = wait_for_terminate(child, &si);
+if (r < 0)
+return sd_bus_error_set_errnof(error, r, "Failed to wait for 
child: %m");
+if (si.si_code != CLD_EXITED || si.si_status != EXIT_SUCCESS)
+return sd_bus_error_setf(error, SD_BUS_ERROR_FAILED, "Child 
died abnormally.");
+
+r = receive_fd(socket_fd, journal_fd);
+if (r < 0)
+return sd_bus_error_set_errnof(error, r, "Failed to receive 
journal fd: %m");
+
+return 0;
+}
+
+static int get_journal_fd(Machine *machine, sd_bus_error *error, int 
*journal_fd) {
+_cleanup_close_pair_ int pair[2] = { -1, -1 };
+_cleanup_close_ int mntns_fd = -1, root_fd = -1, fd = -1;
+pid_t child;
+int r;
+
+assert(machine);
+assert(error);
+assert(journal_fd);
+
+r = socketpair(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0, pair);
+if (r < 0)
+return sd_bus_error_set_errnof(error, errno, "Failed to create 
pair of sockets: %m");
+
+r = namespace_open(machine->leader, NULL, &mntns_fd, NULL, &root_fd);
+if (r < 0)
+return sd_bus_error_set_errnof(error, r, "Failed to open 
leader's namespace(): %m");
+
+child = fork();
+if (child < 0)
+return sd_bus_error_set_errnof(error, errno, "Failed to 
fork(): %m");
+
+if (child == 0) {
+pair[0] = safe_close(pair[0]);
+r = get_journal_fd_child(pair[1], mntns_fd, root_fd);
+pair[1] = safe_close(pair[1]);
+if (r < 0)
+_exit(EXIT_FAILURE);
+_exit(EXIT_SUCCESS);
+}
+
+pair[1] = safe_close(pair[1]);
+r = get_journal_fd_parent(pair[0], child, error, journal_fd);
+return r;
+}
+
+static int method_get_journal(sd_bus_message *message, void *userdata, 
sd_bus_error *error) {
+Manager *m = userdata;
+const char *name;
+int r;
+Machine *machine;
+_cleanup_close_ int journal_fd = -1;
+
+assert(message);
+assert(m);
+
+r = sd_bus_message_read(message, "s", &name);
+if (r < 0)
+return r;
+
+r = bus_verify_polkit_async(
+message,
+CAP_SYS_ADMIN,
+"org.freedesktop.machine1.get-journal",
+false,
+UID_INVALID,
+&m->polkit_registry,
+error);
+if (r < 0)
+return r;
+if (r == 0)
+return 1; /* Will call us back */
+
+if (!machine_name_is_valid(name))
+return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, 
"Invalid machine name");
+
+machine = hashmap_get(m->machines, name);
+if (!machine)
+return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_MACHINE, "No 
machine '%s' known", name);
+
+r = get_journal_fd(machine, error, &journal_fd);
+if (r < 0)
+return r;
+
+return sd_bus_reply_method_return(message, "h", journal_fd);
+}
+
 const sd_bus_vtable manager_vtable[] = {
 SD_BUS_VTABLE_START(0),
 SD_BUS_PROPERTY("PoolPath", "s", property_get_pool_path, 0, 0),
@@ -869,6 +980,7 @@ const sd_bus_vtable manager_vtable[] = {
 SD_BUS_METHOD("MarkImageReadOnly", "sb", NULL, 
method_mark_image_read_only, SD_BUS_VTABLE_UNPRIVILEGED),
 SD_BUS_METHOD("SetPoolLimit", "t", NULL, method_set_pool_limit, 
SD_BUS_VTABLE

[systemd-devel] [PATCH 6/7] journal: Open JournalFile by dirfd and filename instead of path

2015-06-01 Thread Krzesimir Nowak
That way we can have access to a file that is not accessible by path,
for example to a file in overlayfs in different mount namespace (which
is the case for rkt pods).
---
 src/journal-remote/journal-remote-write.c |  12 +-
 src/journal-remote/journal-remote.c   |  34 ++--
 src/journal/journal-file.c| 108 +-
 src/journal/journal-file.h|  10 +-
 src/journal/journal-internal.h|   3 +-
 src/journal/journal-vacuum.c  |  27 +--
 src/journal/journal-vacuum.h  |   3 +-
 src/journal/journal-verify.c  |   4 +-
 src/journal/journalctl.c  |   8 +-
 src/journal/journald-server.c | 113 +++
 src/journal/sd-journal.c  | 324 --
 src/journal/test-journal-flush.c  |  13 +-
 src/journal/test-journal-interleaving.c   |  55 +++--
 src/journal/test-journal-stream.c |  10 +-
 src/journal/test-journal-verify.c |  13 +-
 src/journal/test-journal.c|  21 +-
 src/shared/util.c |  20 ++
 src/shared/util.h |   1 +
 18 files changed, 515 insertions(+), 264 deletions(-)

diff --git a/src/journal-remote/journal-remote-write.c 
b/src/journal-remote/journal-remote-write.c
index 99820fa..0fdeb7f 100644
--- a/src/journal-remote/journal-remote-write.c
+++ b/src/journal-remote/journal-remote-write.c
@@ -59,7 +59,7 @@ static int do_rotate(JournalFile **f, bool compress, bool 
seal) {
 int r = journal_file_rotate(f, compress, seal);
 if (r < 0) {
 if (*f)
-log_error_errno(r, "Failed to rotate %s: %m", 
(*f)->path);
+log_error_errno(r, "Failed to rotate %s/%s: %m", 
(*f)->directory->path, (*f)->filename);
 else
 log_error_errno(r, "Failed to create rotated journal: 
%m");
 }
@@ -93,7 +93,7 @@ Writer* writer_free(Writer *w) {
 return NULL;
 
 if (w->journal) {
-log_debug("Closing journal file %s.", w->journal->path);
+log_debug("Closing journal file %s/%s.", 
w->journal->directory->path, w->journal->filename);
 journal_file_close(w->journal);
 }
 
@@ -136,8 +136,8 @@ int writer_write(Writer *w,
 assert(iovw->count > 0);
 
 if (journal_file_rotate_suggested(w->journal, 0)) {
-log_info("%s: Journal header limits reached or header 
out-of-date, rotating",
- w->journal->path);
+log_info("%s/%s: Journal header limits reached or header 
out-of-date, rotating",
+ w->journal->directory->path, w->journal->filename);
 r = do_rotate(&w->journal, compress, seal);
 if (r < 0)
 return r;
@@ -151,12 +151,12 @@ int writer_write(Writer *w,
 return 1;
 }
 
-log_debug_errno(r, "%s: Write failed, rotating: %m", w->journal->path);
+log_debug_errno(r, "%s/%s: Write failed, rotating: %m", 
w->journal->directory->path, w->journal->filename);
 r = do_rotate(&w->journal, compress, seal);
 if (r < 0)
 return r;
 else
-log_debug("%s: Successfully rotated journal", 
w->journal->path);
+log_debug("%s/%s: Successfully rotated journal", 
w->journal->directory->path, w->journal->filename);
 
 log_debug("Retrying write.");
 r = journal_file_append_entry(w->journal, ts, iovw->iovec, iovw->count,
diff --git a/src/journal-remote/journal-remote.c 
b/src/journal-remote/journal-remote.c
index 911e2a1..1a4c337 100644
--- a/src/journal-remote/journal-remote.c
+++ b/src/journal-remote/journal-remote.c
@@ -162,14 +162,20 @@ static int spawn_getter(const char *getter, const char 
*url) {
 #define filename_escape(s) xescape((s), "/ ")
 
 static int open_output(Writer *w, const char* host) {
-_cleanup_free_ char *_output = NULL;
-const char *output;
+_cleanup_free_ char *directory = NULL;
+_cleanup_free_ char *filename = NULL;
+JournalDirectory *dir;
 int r;
 
 switch (arg_split_mode) {
-case JOURNAL_WRITE_SPLIT_NONE:
+case JOURNAL_WRITE_SPLIT_NONE: {
+const char *output;
+
 output = arg_output ?: REMOTE_JOURNAL_PATH "/remote.journal";
+directory = dirname_malloc(output);
+filename = basename_malloc(output);
 break;
+}
 
 case JOURNAL_WRITE_SPLIT_HOST: {
 _cleanup_free_ char *name;
@@ -180,13 +186,10 @@ static int open_output(Writer *w, const char* host) {
 if (!name)
 return log_oom();
 
-r = asprintf(&_output, "%s/remote-%s.journal",
- arg_output ?: REMOTE_JOURNAL_PATH,
- 

[systemd-devel] [PATCH 0/7] Make "journalctl -M" work with journals inside overlayfs

2015-06-01 Thread Krzesimir Nowak
Hi,

When having a container that keeps its journal inside overlayfs,
journalctl from host can't access them anymore. The way to fix, as
proposed by Lennart, was basically journalctl to get file descriptor
of journal directory from machined and go from there. This situation
happens inside rkt containers - currently to get logs from them, we
need to link the journal to host and call journalctl with merge flag,
journalctl -m _HOSTNAME=

So, first three commits are some fixes for minor bugs I noticed or
small refactorings I made during this work.

Fourth patch adds a GetJournal() method to machine1.Manager which
takes a machine name and returns a file descriptor. It works by
entering a mount namespace of machine's leader and getting a
descriptor of /var/log/journal.

Fifth and sixth patches convert the JournalFile from being open()ed
with a path to being openat()ed with dirfd and filename. The fifth
patch wraps the directory fd and path into a refcounted struct, so
many JournalFiles actually hold a ref to JournalDirectory instead of
holding a descriptor. The sixth patch changes the JournalFile to use
JournalDirectory and all the other sources that were using
JournalFile. The patch is quite big because of that, sorry about
that. Maybe you will want to minimize the changes (by preserving
JournalFile's path member).

Seventh patch hooks journalctl to machine1.Manager's GetJournal.

Krzesimir Nowak (7):
  nspawn, shared: Factor out sending and receiving fd
  nspawn: Close unneeded sockets in outer child
  nspawn, machined: Fix comments and error messages related to child
failures
  machined: Add getter for machine's journal directory descriptor
  journal: Add JournalDirectory
  journal: Open JournalFile by dirfd and filename instead of path
  journal: Try getting journal fd from machined

 Makefile.am|   4 +-
 src/journal-remote/journal-remote-write.c  |  12 +-
 src/journal-remote/journal-remote.c|  34 ++-
 src/journal/journal-dir.c  | 141 ++
 src/journal/journal-dir.h  |  37 +++
 src/journal/journal-file.c | 108 +++
 src/journal/journal-file.h |  10 +-
 src/journal/journal-internal.h |   3 +-
 src/journal/journal-vacuum.c   |  27 +-
 src/journal/journal-vacuum.h   |   3 +-
 src/journal/journal-verify.c   |   4 +-
 src/journal/journalctl.c   |   8 +-
 src/journal/journald-server.c  | 113 +---
 src/journal/sd-journal.c   | 376 ++---
 src/journal/test-journal-flush.c   |  13 +-
 src/journal/test-journal-interleaving.c|  55 ++--
 src/journal/test-journal-stream.c  |  10 +-
 src/journal/test-journal-verify.c  |  13 +-
 src/journal/test-journal.c |  21 +-
 src/machine/machine-dbus.c |  18 +-
 src/machine/machined-dbus.c| 112 
 src/machine/org.freedesktop.machine1.policy.in |  10 +
 src/nspawn/nspawn.c|  72 +
 src/shared/util.c  |  76 +
 src/shared/util.h  |   4 +
 25 files changed, 949 insertions(+), 335 deletions(-)
 create mode 100644 src/journal/journal-dir.c
 create mode 100644 src/journal/journal-dir.h

-- 
2.1.0

___
systemd-devel mailing list
systemd-devel@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/systemd-devel


[systemd-devel] [PATCH 3/7] nspawn, machined: Fix comments and error messages related to child failures

2015-06-01 Thread Krzesimir Nowak
---
 src/machine/machine-dbus.c | 18 +-
 src/nspawn/nspawn.c|  4 ++--
 2 files changed, 11 insertions(+), 11 deletions(-)

diff --git a/src/machine/machine-dbus.c b/src/machine/machine-dbus.c
index 0892479..12f7961 100644
--- a/src/machine/machine-dbus.c
+++ b/src/machine/machine-dbus.c
@@ -327,9 +327,9 @@ int bus_machine_method_get_addresses(sd_bus_message 
*message, void *userdata, sd
 
 r = wait_for_terminate(child, &si);
 if (r < 0)
-return sd_bus_error_set_errnof(error, r, "Failed to wait for 
client: %m");
+return sd_bus_error_set_errnof(error, r, "Failed to wait for 
child: %m");
 if (si.si_code != CLD_EXITED || si.si_status != EXIT_SUCCESS)
-return sd_bus_error_setf(error, SD_BUS_ERROR_FAILED, "Client 
died abnormally.");
+return sd_bus_error_setf(error, SD_BUS_ERROR_FAILED, "Child 
died abnormally.");
 
 r = sd_bus_message_close_container(reply);
 if (r < 0)
@@ -404,9 +404,9 @@ int bus_machine_method_get_os_release(sd_bus_message 
*message, void *userdata, s
 
 r = wait_for_terminate(child, &si);
 if (r < 0)
-return sd_bus_error_set_errnof(error, r, "Failed to wait for 
client: %m");
+return sd_bus_error_set_errnof(error, r, "Failed to wait for 
child: %m");
 if (si.si_code != CLD_EXITED || si.si_status != EXIT_SUCCESS)
-return sd_bus_error_setf(error, SD_BUS_ERROR_FAILED, "Client 
died abnormally.");
+return sd_bus_error_setf(error, SD_BUS_ERROR_FAILED, "Child 
died abnormally.");
 
 r = sd_bus_message_new_method_return(message, &reply);
 if (r < 0)
@@ -738,11 +738,11 @@ int bus_machine_method_bind_mount(sd_bus_message 
*message, void *userdata, sd_bu
 
 r = wait_for_terminate(child, &si);
 if (r < 0) {
-r = sd_bus_error_set_errnof(error, r, "Failed to wait for 
client: %m");
+r = sd_bus_error_set_errnof(error, r, "Failed to wait for 
child: %m");
 goto finish;
 }
 if (si.si_code != CLD_EXITED) {
-r = sd_bus_error_setf(error, SD_BUS_ERROR_FAILED, "Client died 
abnormally.");
+r = sd_bus_error_setf(error, SD_BUS_ERROR_FAILED, "Child died 
abnormally.");
 goto finish;
 }
 if (si.si_status != EXIT_SUCCESS) {
@@ -750,7 +750,7 @@ int bus_machine_method_bind_mount(sd_bus_message *message, 
void *userdata, sd_bu
 if (read(errno_pipe_fd[0], &r, sizeof(r)) == sizeof(r))
 r = sd_bus_error_set_errnof(error, r, "Failed to 
mount: %m");
 else
-r = sd_bus_error_setf(error, SD_BUS_ERROR_FAILED, 
"Client failed.");
+r = sd_bus_error_setf(error, SD_BUS_ERROR_FAILED, 
"Child failed.");
 goto finish;
 }
 
@@ -786,7 +786,7 @@ static int machine_operation_done(sd_event_source *s, const 
siginfo_t *si, void
 o->pid = 0;
 
 if (si->si_code != CLD_EXITED) {
-r = sd_bus_error_setf(&error, SD_BUS_ERROR_FAILED, "Client 
died abnormally.");
+r = sd_bus_error_setf(&error, SD_BUS_ERROR_FAILED, "Child died 
abnormally.");
 goto fail;
 }
 
@@ -794,7 +794,7 @@ static int machine_operation_done(sd_event_source *s, const 
siginfo_t *si, void
 if (read(o->errno_fd, &r, sizeof(r)) == sizeof(r))
 r = sd_bus_error_set_errnof(&error, r, "%m");
 else
-r = sd_bus_error_setf(&error, SD_BUS_ERROR_FAILED, 
"Client failed.");
+r = sd_bus_error_setf(&error, SD_BUS_ERROR_FAILED, 
"Child failed.");
 
 goto fail;
 }
diff --git a/src/nspawn/nspawn.c b/src/nspawn/nspawn.c
index 28b79c4..2b397b6 100644
--- a/src/nspawn/nspawn.c
+++ b/src/nspawn/nspawn.c
@@ -4806,8 +4806,8 @@ int main(int argc, char *argv[]) {
 }
 
 /* Let the child know that we are ready and wait that the 
child is completely ready now. */
-if (!barrier_place_and_sync(&barrier)) { /* #5 */
-log_error("Client died too early.");
+if (!barrier_place_and_sync(&barrier)) { /* #4 */
+log_error("Child died too early.");
 r = -ESRCH;
 goto finish;
 }
-- 
2.1.0

___
systemd-devel mailing list
systemd-devel@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/systemd-devel


[systemd-devel] [PATCH 5/7] journal: Add JournalDirectory

2015-06-01 Thread Krzesimir Nowak
This ref-counted struct holds a path and a descriptor to a
directory. The descriptor should be used for "real" work (openat,
renameat and others) and the path should be used for
logging/debugging.
---
 Makefile.am   |   2 +
 src/journal/journal-dir.c | 141 ++
 src/journal/journal-dir.h |  37 
 3 files changed, 180 insertions(+)
 create mode 100644 src/journal/journal-dir.c
 create mode 100644 src/journal/journal-dir.h

diff --git a/Makefile.am b/Makefile.am
index 43b819b..32e7ca3 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -4644,6 +4644,8 @@ libsystemd_journal_internal_la_SOURCES = \
src/systemd/_sd-common.h \
src/journal/journal-file.c \
src/journal/journal-file.h \
+   src/journal/journal-dir.c \
+   src/journal/journal-dir.h \
src/journal/journal-vacuum.c \
src/journal/journal-vacuum.h \
src/journal/journal-verify.c \
diff --git a/src/journal/journal-dir.c b/src/journal/journal-dir.c
new file mode 100644
index 000..689e1f0
--- /dev/null
+++ b/src/journal/journal-dir.c
@@ -0,0 +1,141 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+/***
+  This file is part of systemd.
+
+  Copyright 2015 Endocode AG
+
+  systemd is free software; you can redistribute it and/or modify it
+  under the terms of the GNU Lesser General Public License as published by
+  the Free Software Foundation; either version 2.1 of the License, or
+  (at your option) any later version.
+
+  systemd is distributed in the hope that it will be useful, but
+  WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+  Lesser General Public License for more details.
+
+  You should have received a copy of the GNU Lesser General Public License
+  along with systemd; If not, see .
+***/
+
+#include "journal-dir.h"
+#include "macro.h"
+#include "util.h"
+
+static int journal_directory_new_steal(char *path, int fd, JournalDirectory 
**dir) {
+JournalDirectory *d;
+
+assert(path);
+assert(fd >= 0);
+assert(dir);
+
+d = new0(JournalDirectory, 1);
+if (!d)
+return -ENOMEM;
+d->path = path;
+d->fd = fd;
+d->n_ref = 1;
+*dir = d;
+return 0;
+}
+
+int journal_directory_open(const char *path, JournalDirectory **dir)
+{
+_cleanup_free_ char *p = NULL;
+_cleanup_close_ int fd = -1;
+int r;
+
+assert(path);
+assert(dir);
+
+fd = open(path, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW);
+if (fd < 0)
+return -errno;
+
+p = strdup(path);
+if (!p)
+return -ENOMEM;
+
+r = journal_directory_new_steal(p, fd, dir);
+if (r < 0)
+return r;
+p = NULL;
+fd = -1;
+return 0;
+}
+
+int journal_directory_new(const char *path, int fd, JournalDirectory **dir)
+{
+_cleanup_free_ char *p = NULL;
+_cleanup_close_ int dfd = -1;
+int r;
+
+assert(path);
+assert(fd >= 0);
+assert(dir);
+
+dfd = fcntl(fd, F_DUPFD_CLOEXEC, 3);
+if (dfd < 0)
+return -errno;
+
+p = strdup(path);
+if (!p)
+return -ENOMEM;
+
+r = journal_directory_new_steal(p, dfd, dir);
+if (r < 0)
+return r;
+p = NULL;
+dfd = -1;
+return 0;
+}
+
+JournalDirectory *journal_directory_ref(JournalDirectory *dir)
+{
+assert(dir);
+assert(dir->n_ref > 0);
+
+dir->n_ref ++;
+return dir;
+}
+
+JournalDirectory *journal_directory_unref(JournalDirectory *dir)
+{
+if (dir) {
+PROTECT_ERRNO;
+
+assert(dir->n_ref > 0);
+
+dir->n_ref --;
+if (!dir->n_ref) {
+safe_close(dir->fd);
+free(dir->path);
+free(dir);
+}
+}
+
+return NULL;
+}
+
+int journal_directory_opendir(JournalDirectory *dir, DIR **de)
+{
+int fd;
+DIR* d;
+
+assert(dir);
+assert(de);
+
+fd = fcntl(dir->fd, F_DUPFD_CLOEXEC, 3);
+if (fd < 0)
+return -errno;
+
+d = fdopendir(fd);
+if (!d) {
+safe_close(fd);
+return -errno;
+}
+
+*de = d;
+return 0;
+}
diff --git a/src/journal/journal-dir.h b/src/journal/journal-dir.h
new file mode 100644
index 000..65ae229
--- /dev/null
+++ b/src/journal/journal-dir.h
@@ -0,0 +1,37 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+#pragma once
+
+/***
+  This file is part of systemd.
+
+  Copyright 2015 Endocode AG
+
+  systemd is free software; you can redistribute it and/or modify it
+  under the t

[systemd-devel] [PATCH 7/7] journal: Try getting journal fd from machined

2015-06-01 Thread Krzesimir Nowak
---
 Makefile.am  |  2 +-
 src/journal/sd-journal.c | 52 +++-
 2 files changed, 52 insertions(+), 2 deletions(-)

diff --git a/Makefile.am b/Makefile.am
index 32e7ca3..ee9c29b 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -4399,8 +4399,8 @@ systemd_journal_upload_CFLAGS = \
$(LIBCURL_CFLAGS)
 
 systemd_journal_upload_LDADD = \
-   libsystemd-internal.la \
libsystemd-journal-internal.la \
+   libsystemd-internal.la \
libsystemd-shared.la \
$(LIBCURL_LIBS)
 
diff --git a/src/journal/sd-journal.c b/src/journal/sd-journal.c
index 630cc3a..debd45c 100644
--- a/src/journal/sd-journal.c
+++ b/src/journal/sd-journal.c
@@ -28,6 +28,8 @@
 #include 
 #include 
 
+#include "bus-error.h"
+#include "bus-util.h"
 #include "sd-journal.h"
 #include "journal-def.h"
 #include "journal-file.h"
@@ -1701,6 +1703,52 @@ fail:
 return r;
 }
 
+static int try_journal_fd(sd_journal *j, const char *machine) {
+_cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
+_cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
+_cleanup_bus_close_unref_ sd_bus *bus = NULL;
+_cleanup_free_ char *p = NULL;
+int fd;
+int r;
+
+r = sd_bus_default_system(&bus);
+if (r < 0)
+return log_error_errno(r, "Failed to get D-Bus connection: 
%m");
+
+r = sd_bus_call_method(
+bus,
+"org.freedesktop.machine1",
+"/org/freedesktop/machine1",
+"org.freedesktop.machine1.Manager",
+"GetJournal",
+&error,
+&reply,
+"s", machine);
+if (r < 0) {
+log_error("Failed to get journal fd from machined: %s", 
bus_error_message(&error, r));
+return r;
+}
+
+r = sd_bus_message_read(reply, "h", &fd);
+if (r < 0)
+return bus_log_parse_error(r);
+
+if (fd < 0)
+return -ENODATA;
+
+/* TODO: Just some bogus directory name with machine id in it,
+ * so it will look semi-nicely in logs. Is that alright? */
+p = strjoin("machine://", machine, "/journal", NULL);
+if (!p)
+return -ENOMEM;
+
+r = add_root_directory_with_fd(j, p, fd);
+if (r < 0)
+return r;
+
+return 0;
+}
+
 _public_ int sd_journal_open_container(sd_journal **ret, const char *machine, 
int flags) {
 _cleanup_free_ char *root = NULL, *class = NULL;
 sd_journal *j;
@@ -1731,7 +1779,9 @@ _public_ int sd_journal_open_container(sd_journal **ret, 
const char *machine, in
 j->prefix = root;
 root = NULL;
 
-r = add_search_paths(j);
+r = try_journal_fd(j, machine);
+if (r == -ENODATA)
+r = add_search_paths(j);
 if (r < 0)
 goto fail;
 
-- 
2.1.0

___
systemd-devel mailing list
systemd-devel@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/systemd-devel


[systemd-devel] [PATCH 1/7] nspawn, shared: Factor out sending and receiving fd

2015-06-01 Thread Krzesimir Nowak
Right now it can be used to sent rtnl and kmsg descriptors. These
functions will be used later to send journal directory descriptor in
machined.
---
 src/nspawn/nspawn.c | 66 +++--
 src/shared/util.c   | 56 +
 src/shared/util.h   |  3 +++
 3 files changed, 67 insertions(+), 58 deletions(-)

diff --git a/src/nspawn/nspawn.c b/src/nspawn/nspawn.c
index 4211a3d..bd7532c 100644
--- a/src/nspawn/nspawn.c
+++ b/src/nspawn/nspawn.c
@@ -1839,15 +1839,6 @@ static int setup_kmsg(const char *dest, int kmsg_socket) 
{
 const char *from, *to;
 _cleanup_umask_ mode_t u;
 int fd, k;
-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;
 
 assert(kmsg_socket >= 0);
 
@@ -1872,17 +1863,9 @@ static int setup_kmsg(const char *dest, int kmsg_socket) 
{
 if (fd < 0)
 return log_error_errno(errno, "Failed to open fifo: %m");
 
-cmsg = CMSG_FIRSTHDR(&mh);
-cmsg->cmsg_level = SOL_SOCKET;
-cmsg->cmsg_type = SCM_RIGHTS;
-cmsg->cmsg_len = CMSG_LEN(sizeof(int));
-memcpy(CMSG_DATA(cmsg), &fd, sizeof(int));
-
-mh.msg_controllen = cmsg->cmsg_len;
-
 /* Store away the fd in the socket, so that it stays open as
  * long as we run the child */
-k = sendmsg(kmsg_socket, &mh, MSG_NOSIGNAL);
+k = send_fd(kmsg_socket, fd);
 safe_close(fd);
 
 if (k < 0)
@@ -1894,20 +1877,11 @@ static int setup_kmsg(const char *dest, int 
kmsg_socket) {
 return 0;
 }
 
-static int send_rtnl(int send_fd) {
-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;
+static int send_rtnl(int sender_fd) {
 _cleanup_close_ int fd = -1;
-ssize_t k;
+int r;
 
-assert(send_fd >= 0);
+assert(sender_fd >= 0);
 
 if (!arg_expose_ports)
 return 0;
@@ -1916,18 +1890,10 @@ static int send_rtnl(int send_fd) {
 if (fd < 0)
 return log_error_errno(errno, "Failed to allocate container 
netlink: %m");
 
-cmsg = CMSG_FIRSTHDR(&mh);
-cmsg->cmsg_level = SOL_SOCKET;
-cmsg->cmsg_type = SCM_RIGHTS;
-cmsg->cmsg_len = CMSG_LEN(sizeof(int));
-memcpy(CMSG_DATA(cmsg), &fd, sizeof(int));
-
-mh.msg_controllen = cmsg->cmsg_len;
-
 /* Store away the fd in the socket, so that it stays open as
  * long as we run the child */
-k = sendmsg(send_fd, &mh, MSG_NOSIGNAL);
-if (k < 0)
+r = send_fd(sender_fd, fd);
+if (r < 0)
 return log_error_errno(errno, "Failed to send netlink fd: %m");
 
 return 0;
@@ -2032,18 +1998,8 @@ static int on_address_change(sd_rtnl *rtnl, 
sd_rtnl_message *m, void *userdata)
 }
 
 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);
@@ -2052,16 +2008,10 @@ static int watch_rtnl(sd_event *event, int recv_fd, 
union in_addr_union *exposed
 if (!arg_expose_ports)
 return 0;
 
-k = recvmsg(recv_fd, &mh, MSG_NOSIGNAL);
-if (k < 0)
+r = receive_fd(recv_fd, &fd);
+if (r < 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);
diff --git a/src/shared/util.c b/src/shared/util.c
index 8a61079..395af7c 100644
--- a/src/shared/util.c
+++ b/src/shared/util.c
@@ -6046,3 +6046,59 @@ int reset_uid_gid(void) {
 
 return 0;
 }
+
+int send_fd(int sender_fd, int fd) {
+union {
+struct cmsghdr cmsghdr;
+uint8_t buf[CMSG_SPACE(sizeof(int))];
+} control = {};
+s