cmd: make locking around namespaces explicit #3149

Merged
merged 21 commits into from Apr 24, 2017

Conversation

Projects
None yet
3 participants
Contributor

zyga commented Apr 6, 2017

This branch allows anyone to use new locking primitives (sc_lock/sc_unlock) and
makes the namespace module not do any locking anymore. Locking is now moved
explicitly to happen in snap-confine so that we can do various initialization
under one global lock and then some more initialization under per-snap lock.
This will allow us to move the sharing of /snap to inside snap-confine and have
support for running inside containers and running with deputy systemd.

Signed-off-by: Zygmunt Krynicki zygmunt.krynicki@canonical.com

zyga added some commits Apr 6, 2017

cmd/libsnap: add support for generic locking
This patch adds a helper function for calling something while holding an
exclusive filesystem lock. The lock can be scoped to a specific snap or
global.

Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
cmd/snap-confine: use new locking primitives
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
cmd/snap-confine: don't hold global lock while doing per-snap init
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
cmd/libsnap: add explicit lock/unlock functions
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
cmd/snap-confine: use explicit lock/unlock operations
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
cmd/libsnap: make sanity timeout APIs public and move them to locking
The sanity timeout APIs belong logically with the locking primitives
so move them there. In addition, since they are needed in the namespace
module, make them public.

Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
cmd/libsnap: add tests for the locking module
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
cmd/snap-confine,snap-discard-ns: remove locking from namespace module
The namespace module now relies the caller to use locking primitives
for all the locking operations.

Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
cmd/snap-discard-ns: add explicit locking around namespace operations
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
cmd/libsnap: remove sc_call_while_locked
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
cmd/libsnap: document sanity timeout on sc_lock
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
cmd/libsnap: it is 2017
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
cmd/snap-confine: drop two implemented TODOs
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
cmd/libsnap: remove needless void (thanks mvo)
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
cmd: add and use sc_lock_global()
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>

Looks good, some comments inline.

+ gint exit_status = 0;
+ GError *error = NULL;
+
+ argv = calloc(5, sizeof *argv);
@mvo5

mvo5 Apr 6, 2017

Collaborator

What is the advantage of doing this (and the following 14 lines) over:

const char *argv[] = { "rm", "-rf", "--", dir, NULL };

i.e. http://paste.ubuntu.com/24327160/

@zyga

zyga Apr 6, 2017

Contributor

Arguments need to be writable.

@mvo5

mvo5 Apr 6, 2017

Collaborator

What about: char *argv[] = { "rm", "-rf", "--", (char*)dir, NULL }; then (especially since execv will not modify them, according to http://pubs.opengroup.org/onlinepubs/009604499/functions/exec.html). Anyway, one of the reasons why C code reviews make me grumpy.

@zyga

zyga Apr 7, 2017

Contributor

I mean that individual arguments need to be writable according to the API. That's why all the strdup's are up there.

@mvo5

mvo5 Apr 7, 2017

Collaborator

The opengroup page is an interessting read (the RATIONAL part about argv) to see why argv is char*. i.e. The statement about argv[] and envp[] being constants is included to make explicit to future writers of language bindings that these objects are completely constant. Due to a limitation of the ISO C standard, it is not possible to state that idea in standard C.. But shrug things like this make me do as little as possible with C code.

cmd/libsnap-confine-private/locking.c
+ return lock_fd;
+}
+
+int sc_lock_global()
@mvo5

mvo5 Apr 6, 2017

Collaborator

❤️

@mvo5

mvo5 Apr 6, 2017

Collaborator

I guess this also needs a sc_unlock_global() :)

@zyga

zyga Apr 6, 2017

Contributor

Done :)

cmd/snap-confine/snap-confine.c
sc_initialize_ns_groups();
+ // TODO: implement this.
+ debug("share snap directory here...");
+ sc_unlock(NULL, global_lock_fd);
@mvo5

mvo5 Apr 6, 2017

Collaborator

I would prefer sc_unlock_global() here.

@zyga

zyga Apr 6, 2017

Contributor

Yeah, will do

@zyga

zyga Apr 6, 2017

Contributor

Done

struct sc_ns_group *group =
sc_open_ns_group(snap_name, SC_NS_FAIL_GRACEFULLY);
if (group != NULL) {
- sc_lock_ns_mutex(group);
@mvo5

mvo5 Apr 6, 2017

Collaborator

It seems cmd/snap-confine/ns-support.h still has a reference in the header to this one?

@zyga

zyga Apr 6, 2017

Contributor

Ah, thanks, I'll check if there's anything else stale there.

@zyga

zyga Apr 6, 2017

Contributor

Done

zyga added some commits Apr 6, 2017

cmd/snap-confine: remove traces of now-gone locking in ns-support.h
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
cmd/libsnap: add sc_unlock_global
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
cmd/snap-confine: use sc_unlock_global
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
Contributor

zyga commented Apr 13, 2017

The errors show some issues in the CI setup. We're executing snap-confine under a profile that doesn't have the correct rules but those rules are now present in the tree. Probably the run-from-core logic is faulty somewhere. I'll investigate this now.

zyga added some commits Apr 13, 2017

mvo5 approved these changes Apr 18, 2017

Looks good (but needs a second review). I still dislike https://github.com/snapcore/snapd/pull/3149/files#r110140992 but shrug its just tests.

@zyga zyga merged commit fd71323 into snapcore:master Apr 24, 2017

6 checks passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details
xenial-amd64 autopkgtest finished (success)
Details
xenial-i386 autopkgtest finished (success)
Details
xenial-ppc64el autopkgtest finished (success)
Details
yakkety-amd64 autopkgtest finished (success)
Details
zesty-amd64 autopkgtest finished (success)
Details

@zyga zyga deleted the zyga:locking branch Apr 24, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment