Try to preserve current working directory #48

Merged
merged 6 commits into from Jun 24, 2016
Prev

Switch to get_current_dir_name()

This patch changes getcwd to get_current_dir_name and does a tiny
refactor to move cleanup functions to a dedicated module so that they
can be shared.

Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
  • Loading branch information...
commit 0e690b310d1ce4fb772fab3085015897d62bf7d0 @zyga zyga committed Jun 24, 2016
View
@@ -10,7 +10,9 @@ snap_confine_SOURCES = \
mount-support.c \
mount-support.h \
mount-support-nvidia.c \
- mount-support-nvidia.h
+ mount-support-nvidia.h \
+ cleanup-funcs.c \
+ cleanup-funcs.h
snap_confine_CFLAGS = -Wall -Werror $(AM_CFLAGS)
snap_confine_LDFLAGS = $(AM_LDFLAGS)
snap_confine_LDADD =
View
@@ -0,0 +1,23 @@
+/*
+ * Copyright (C) 2015 Canonical Ltd
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 3 as
+ * published by the Free Software Foundation.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#include <stdlib.h>
+
+void sc_cleanup_string(char **ptr)
+{
+ free(*ptr);
+}
View
@@ -0,0 +1,29 @@
+/*
+ * Copyright (C) 2015 Canonical Ltd
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 3 as
+ * published by the Free Software Foundation.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#ifndef SNAP_CONFINE_CLEANUP_FUNCS_H
+#define SNAP_CONFINE_CLEANUP_FUNCS_H
+
+/**
+ * Free a dynamically allocated string.
+ *
+ * This function is designed to be used with
+ * __attribute__((cleanup(sc_cleanup_string))).
+ **/
+void sc_cleanup_string(char **ptr);
+
+#endif
View
@@ -32,6 +32,7 @@
#include "seccomp-support.h"
#include "udev-support.h"
#endif // ifdef STRICT_CONFINEMENT
+#include "cleanup-funcs.h"
int main(int argc, char **argv)
{
@@ -89,8 +90,10 @@ int main(int argc, char **argv)
// Get the current working directory before we start fiddling with
// mounts and possibly pivot_root. At the end of the whole process, we
// will try to re-locate to the same directory (if possible).
- char vanilla_cwd[512];
- if (getcwd(vanilla_cwd, sizeof vanilla_cwd) == NULL) {
+ char *vanilla_cwd __attribute__ ((cleanup(sc_cleanup_string))) =
+ NULL;
+ vanilla_cwd = get_current_dir_name();
+ if (vanilla_cwd == NULL) {
die("cannot get the current working directory");
}
@jdstrand

jdstrand Jun 24, 2016

Contributor

I suggest using get_current_dir_name() instead of getcwd(), like we do in mount-support.c. This has the advantage of not having to pick an arbitrary size for vanilla_cwd but does mean you need to free() the result.

// do the mounting if run on a non-native snappy system
@@ -78,11 +78,6 @@ static const char *nvidia_globs[] = {
static const size_t nvidia_globs_len =
sizeof nvidia_globs / sizeof *nvidia_globs;
-static void cleanup_string(char **ptr)
-{
- free(*ptr);
-}
-
// Populate libgl_dir with a symlink farm to files matching glob_list.
//
// The symbolic links are made in one of two ways. If the library found is a
@@ -113,7 +108,8 @@ static void sc_populate_libgl_with_hostfs_symlinks(const char *libgl_dir,
char symlink_name[512];
char symlink_target[512];
const char *pathname = glob_res.gl_pathv[i];
- char *pathname_copy __attribute__ ((cleanup(cleanup_string))) =
+ char *pathname_copy
+ __attribute__ ((cleanup(sc_cleanup_string))) =
strdup(pathname);
char *filename = basename(pathname_copy);
struct stat stat_buf;