Add mountinfo module #123

Merged
merged 9 commits into from Sep 5, 2016

Conversation

Projects
None yet
3 participants
Collaborator

zyga commented Sep 2, 2016

This patch adds module capable of parsing /proc/self/mountinfo.

Snap-confine needs to parse this file to understand what is currently mounted in a way
that makes bind mounts visible (classic /proc/mounts is insufficient for this purpose).
For extra details about this topic see: https://lwn.net/Articles/265920/

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

zyga added some commits Sep 2, 2016

Add mountinfo module
This patch adds module capable of parsing /proc/self/mountinfo.

Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
Add a few tests for parse_mountinfo_entry
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
Add more tests for mountinfo
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
Add a few more mountinfo tests
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
+ return NULL;
+}
+
+void cleanup_mountinfo(struct mountinfo **ptr)
@tyhicks

tyhicks Sep 2, 2016

Collaborator

You didn't use this function.

@zyga

zyga Sep 2, 2016

Collaborator

This is a library function (for users of mount info), note that free_mountinfo is static/private.

+ return info;
+}
+
+static struct mountinfo_entry *parse_mountinfo_entry(const char *line)
@tyhicks

tyhicks Sep 2, 2016

Collaborator

As I mentioned in IRC, this function is much more complicated than I'd like it to be. However, it looks to be correct and I don't have a better suggestion at this time.

+ int total_used = 0;
+ char *parse_next_string_field() {
+ char *field = &entry->line_buf[0] + total_used;
+ nscanned = sscanf(line + total_offset, "%s %n", field, &offset);
@tyhicks

tyhicks Sep 2, 2016

Collaborator

Could you add a comment about what's going on here with field? It takes a while to realize that the parsed string is being stored in the array at the end of mountinfo_entry, which is being allocated to the size of the passed in line. It is also worth mentioning that the strings stored in that array are delimited with NUL characters from the calloc(). This is quite confusing and is going to take anyone reading the code quite a bit a of time to digest without a comment.

@zyga

zyga Sep 2, 2016

Collaborator

Ack, I'll document this explicitly.

@zyga

zyga Sep 2, 2016

Collaborator

Done

src/mountinfo.c
+ char *super_opts;
+
+ struct mountinfo_entry *next;
+ char line_buf[0]; // Buffer holding all of the text data above;
@tyhicks

tyhicks Sep 2, 2016

Collaborator

How about a comment saying not to add any new fields after .line_buf? Someone not well versed in C might not understand.

@zyga

zyga Sep 2, 2016

Collaborator

Ack

@zyga

zyga Sep 2, 2016

Collaborator

Done

Collaborator

tyhicks commented Sep 2, 2016

Ack after adding the requested comments. Thanks!

zyga added some commits Sep 2, 2016

Comment on mountinfo.line_buf
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
Describe mountinfo entry parser memory use
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
Collaborator

tyhicks commented Sep 2, 2016

Thanks for the added comments. I'm happy with this PR.

Fix a few typos
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
src/mountinfo.c
- // result is similar to using strtok except that the source and destination
- // buffers are separate.
+ // The parsing code below, specifically parse_next_string_field(), uses
+ // this extra memory to hold data parsed from the original line. In the end
@Conan-Kudo

Conan-Kudo Sep 3, 2016

comma missing after "In the end".

src/mountinfo.c
- // all of the text fields.
+ // NOTE: the mountinfo structure is allocated along with enough extra
+ // storage to hold the whole line we are parsing. This is used as backing
+ // store for all of the text fields.
@Conan-Kudo

Conan-Kudo Sep 3, 2016

"all of the text fields" can be simplified to "all text fields".

Conan-Kudo commented Sep 3, 2016

Aside from the couple of comments I made about your typo fixes, it looks good to me. 👍

zyga added some commits Sep 3, 2016

Add comma
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
Simplify one sentence
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>

👍

@zyga zyga merged commit e4e727b into master Sep 5, 2016

1 check passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details

@zyga zyga deleted the mountinfo branch Sep 5, 2016

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