Join GitHub today
GitHub is home to over 20 million developers working together to host and review code, manage projects, and build software together.
cmd: add functions to load/save fstab-like files #2775
Conversation
zyga
added some commits
Feb 3, 2017
| + if (result->mnt_type == NULL) { | ||
| + die("cannot copy string"); | ||
| + } | ||
| + result->mnt_opts = strdup(entry->mnt_opts); |
zyga
Feb 13, 2017
Contributor
There's no hard specification on that in the manual page but glibc ensures that we get static "" instead of NULL in each of those field, in each case.
niemeyer
Feb 13, 2017
Contributor
As discussed, might be worth testing for NULL and strdup("") explicitly if we happen to find it.
| +{ | ||
| + FILE *f __attribute__ ((cleanup(sc_cleanup_endmntent))) = NULL; | ||
| + | ||
| + f = setmntent(pathname, "rt"); |
tyhicks
suggested changes
Feb 13, 2017
There's one NULL check that needs to be added and the mentioned test would be a nice thing to have. The other comments are more for discussion and are not blockers.
| + free(entry->mnt_dir); | ||
| + free(entry->mnt_type); | ||
| + free(entry->mnt_opts); | ||
| + memset(entry, 0, sizeof *entry); |
tyhicks
Feb 13, 2017
Contributor
What's the intent of this memset? I initially assumed that it was to scrub memory but that wouldn't make sense because it is only scrubbing the pointer values (not the strings) and the ints in the sc_mount_entry struct.
zyga
Feb 14, 2017
Contributor
It is indeed to scrub the pointers. This is just a habit, so that double-free (if ever is made to happen) becomes harmless.
| + | ||
| +void sc_cleanup_mount_entry_list(struct sc_mount_entry **entryp) | ||
| +{ | ||
| + sc_free_mount_entry_list(*entryp); |
tyhicks
Feb 13, 2017
Contributor
We need to ensure that entryp is not NULL before dereferencing it. That's essential for cleanup functions that are automatically called via the cleanup attribute.
zyga
Feb 13, 2017
Contributor
Note that there are two pointers here. Gcc ensures that this is never called with a NULL pointer. What may happen is that we call sc_free_mount_entry_list(NULL) which should be correct (and if it is not, should be made to be correct).
tyhicks
Feb 13, 2017
Contributor
In my experience with GCC's cleanup attribute, I have not noticed that GCC ensures that a cleanup function is only called when the pointer is non-NULL. It doesn't say anything in the docs about that:
I also noticed that checking for a NULL pointer is something that we do in all of the functions in cmd/libsnap-confine-private/cleanup-funcs.c
zyga
Feb 13, 2017
Contributor
If a variable of any type has the cleanup attribute a function is called with a pointer to that variable. That is by definition not NULL. I've added the non-NULL checks there because someone may just use those functions directly. I guess that's a reason enough to add the check here but I wanted to make sure we agree on how the mechanism works.
zyga
Feb 14, 2017
Contributor
I've added a sanity check for the NULL just in case this is used manually.
| + die("cannot open mount profile %s for reading", | ||
| + pathname); | ||
| + } | ||
| + return NULL; |
tyhicks
Feb 13, 2017
Contributor
As mentioned above, we'd see a segfault here due to sc_cleanup_mount_entry_list() not handling a NULL pointer.
tyhicks
Feb 13, 2017
Contributor
How about adding a test for sc_load_mount_profile() where you pass it a path to a file that does not exist in order to test this case and keep it from regressing.
| + char *mnt_type; /* mount type (see mntent.h) */ | ||
| + char *mnt_opts; /* mount options (see mntent.h) */ | ||
| + int mnt_freq; /* dump frequency in days */ | ||
| + int mnt_passno; /* pass number on parallel fsck */ |
tyhicks
Feb 13, 2017
Contributor
I feel like it is a mistake to replicate the mntent structure and think the sc_mount_entry struct should look like this:
struct sc_mount_entry {
struct mntent entry;
struct sc_mount_entry *next;
};My reasoning is that you won't need to convert back-and-forth between sc_mount_entry and mntent structs. You'll still need to duplicate the strings in sc_clone_mount_entry_from_mntent() but sc_save_mount_profile() would get smaller because you have a mntent ready to be passed to addmntent().
There may come a time that you want to make use of hasmntopt() and that'll be simple because you have a mntent ready to pass to it.
zyga commentedFeb 3, 2017
This patch adds a mount entry structure that mirrors what is in
struct mntent. It is based on the standard library functions for
parsing and saving fstab files correctly.
The main motivation is that the extra linkage between entries and
(upcoming) utiltiy function make this format preferred for processing.
Signed-off-by: Zygmunt Krynicki zygmunt.krynicki@canonical.com