summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexander Larsson <alexl@redhat.com>2016-02-16 09:55:01 +0100
committerAlexander Larsson <alexl@redhat.com>2016-02-16 09:55:01 +0100
commitdafe63b41ef68f4763a39799442dd8fc35c37e07 (patch)
treeae97907617fdc0c7336b77253643c7055764e878
parent5556d20fd5be5c4fdfd9da77b0c44b3ac31eef13 (diff)
downloadbubblewrap-dafe63b41ef68f4763a39799442dd8fc35c37e07.tar.gz
Move more helper functions to utils.c
-rw-r--r--build-root.c163
-rw-r--r--utils.c150
-rw-r--r--utils.h15
3 files changed, 170 insertions, 158 deletions
diff --git a/build-root.c b/build-root.c
index ef3cdda..6772343 100644
--- a/build-root.c
+++ b/build-root.c
@@ -55,48 +55,6 @@ typedef enum {
BIND_RECURSIVE = (1<<3),
} bind_option_t;
-
-static char *
-load_file_at (int dirfd, const char *path)
-{
- cleanup_fd int fd = -1;
- cleanup_free char *data = NULL;
- ssize_t data_read;
- ssize_t data_len;
- ssize_t res;
-
- fd = openat (dirfd, path, O_CLOEXEC | O_RDONLY);
- if (fd == -1)
- return NULL;
-
- data_read = 0;
- data_len = 4080;
- data = xmalloc (data_len);
-
- do
- {
- if (data_len >= data_read + 1)
- {
- data_len *= 2;
- data = xrealloc (data, data_len);
- }
-
- do
- res = read (fd, data + data_read, data_len - data_read - 1);
- while (res < 0 && errno == EINTR);
-
- if (res < 0)
- return NULL;
-
- data_read += res;
- }
- while (res > 0);
-
- data[data_read] = 0;
-
- return steal_pointer (&data);
-}
-
static char *
skip_line (char *line)
{
@@ -121,13 +79,6 @@ skip_token (char *line, bool eat_whitespace)
return line;
}
-static bool
-str_has_prefix (const char *str,
- const char *prefix)
-{
- return strncmp (str, prefix, strlen (prefix)) == 0;
-}
-
static char *
unescape_mountpoint (const char *escaped, ssize_t len)
{
@@ -291,7 +242,7 @@ get_submounts (const char *parent_mount)
unescaped = unescape_mountpoint (mountpoint, -1);
if (*unescaped == '/' &&
- str_has_prefix (unescaped + 1, parent_mount) &&
+ has_prefix (unescaped + 1, parent_mount) &&
*(unescaped + 1 + strlen (parent_mount)) == '/')
{
if (n_submounts + 1 >= submounts_size)
@@ -367,84 +318,6 @@ stat_is_dir (const char *pathname)
return S_ISDIR (buf.st_mode);
}
-static int
-mkdir_with_parents (const char *pathname,
- int mode,
- bool create_last)
-{
- cleanup_free char *fn = NULL;
- char *p;
- struct stat buf;
-
- if (pathname == NULL || *pathname == '\0')
- {
- errno = EINVAL;
- return 1;
- }
-
- fn = xstrdup (pathname);
-
- p = fn;
- while (*p == '/')
- p++;
-
- do
- {
- while (*p && *p != '/')
- p++;
-
- if (!*p)
- p = NULL;
- else
- *p = '\0';
-
- if (!create_last && p == NULL)
- break;
-
- if (stat (fn, &buf) != 0)
- {
- if (mkdir (fn, mode) == -1 && errno != EEXIST)
- return -1;
- }
- else if (!S_ISDIR (buf.st_mode))
- {
- errno = ENOTDIR;
- return -1;
- }
-
- if (p)
- {
- *p++ = '/';
- while (*p && *p == '/')
- p++;
- }
- }
- while (p);
-
- return 0;
-}
-
-static bool
-write_to_file (int fd,
- const char *content,
- ssize_t len)
-{
- ssize_t res;
-
- while (len > 0)
- {
- res = write (fd, content, len);
- if (res < 0 && errno == EINTR)
- continue;
- if (res <= 0)
- return FALSE;
- len -= res;
- content += res;
- }
-
- return TRUE;
-}
-
#define BUFSIZE 8192
static bool
copy_file_data (int sfd,
@@ -467,7 +340,7 @@ copy_file_data (int sfd,
if (bytes_read == 0)
break;
- if (!write_to_file (dfd, buffer, bytes_read))
+ if (write_to_fd (dfd, buffer, bytes_read) != 0)
return FALSE;
}
@@ -501,29 +374,6 @@ copy_file (const char *src_path,
}
static bool
-write_file_at (int dirfd,
- const char *path,
- const char *content)
-{
- cleanup_fd int fd = -1;
- bool res;
- int errsv;
-
- fd = openat (dirfd, path, O_RDWR | O_CLOEXEC, 0);
- if (fd == -1)
- return FALSE;
-
- res = TRUE;
- if (content)
- res = write_to_file (fd, content, strlen (content));
-
- errsv = errno;
- errno = errsv;
-
- return res;
-}
-
-static bool
create_file (const char *path,
mode_t mode,
const char *content)
@@ -538,7 +388,7 @@ create_file (const char *path,
res = TRUE;
if (content)
- res = write_to_file (fd, content, strlen (content));
+ res = write_to_fd (fd, content, strlen (content));
errsv = errno;
errno = errsv;
@@ -546,7 +396,6 @@ create_file (const char *path,
return res;
}
-
static void
block_sigchild (void)
{
@@ -824,15 +673,15 @@ write_uid_gid_map (uid_t sandbox_uid,
cleanup_free char *gid_map = NULL;
uid_map = strdup_printf ("%d %d 1\n", sandbox_uid, parent_uid);
- if (!write_file_at (proc_fd, "self/uid_map", uid_map))
+ if (write_file_at (proc_fd, "self/uid_map", uid_map) != 0)
die_with_error ("setting up uid map");
if (deny_groups &&
- !write_file_at (proc_fd, "self/setgroups", "deny\n"))
+ write_file_at (proc_fd, "self/setgroups", "deny\n") != 0)
die_with_error ("error writing to setgroups");
gid_map = strdup_printf ("%d %d 1\n", sandbox_gid, parent_gid);
- if (!write_file_at (proc_fd, "self/gid_map", gid_map))
+ if (write_file_at (proc_fd, "self/gid_map", gid_map) != 0)
die_with_error ("setting up gid map");
}
diff --git a/utils.c b/utils.c
index d5cfbdf..1716c9c 100644
--- a/utils.c
+++ b/utils.c
@@ -112,6 +112,13 @@ strfreev (char **str_array)
}
}
+bool
+has_prefix (const char *str,
+ const char *prefix)
+{
+ return strncmp (str, prefix, strlen (prefix)) == 0;
+}
+
void
xsetenv (const char *name, const char *value, int overwrite)
{
@@ -248,6 +255,149 @@ fdwalk (int proc_fd, int (*cb)(void *data, int fd), void *data)
}
int
+write_to_fd (int fd,
+ const char *content,
+ ssize_t len)
+{
+ ssize_t res;
+
+ while (len > 0)
+ {
+ res = write (fd, content, len);
+ if (res < 0 && errno == EINTR)
+ continue;
+ if (res <= 0)
+ return -1;
+ len -= res;
+ content += res;
+ }
+
+ return 0;
+}
+
+int
+write_file_at (int dirfd,
+ const char *path,
+ const char *content)
+{
+ cleanup_fd int fd = -1;
+ bool res;
+ int errsv;
+
+ fd = openat (dirfd, path, O_RDWR | O_CLOEXEC, 0);
+ if (fd == -1)
+ return -1;
+
+ res = 0;
+ if (content)
+ res = write_to_fd (fd, content, strlen (content));
+
+ errsv = errno;
+ errno = errsv;
+
+ return res;
+}
+
+char *
+load_file_at (int dirfd,
+ const char *path)
+{
+ cleanup_fd int fd = -1;
+ cleanup_free char *data = NULL;
+ ssize_t data_read;
+ ssize_t data_len;
+ ssize_t res;
+
+ fd = openat (dirfd, path, O_CLOEXEC | O_RDONLY);
+ if (fd == -1)
+ return NULL;
+
+ data_read = 0;
+ data_len = 4080;
+ data = xmalloc (data_len);
+
+ do
+ {
+ if (data_len >= data_read + 1)
+ {
+ data_len *= 2;
+ data = xrealloc (data, data_len);
+ }
+
+ do
+ res = read (fd, data + data_read, data_len - data_read - 1);
+ while (res < 0 && errno == EINTR);
+
+ if (res < 0)
+ return NULL;
+
+ data_read += res;
+ }
+ while (res > 0);
+
+ data[data_read] = 0;
+
+ return steal_pointer (&data);
+}
+
+int
+mkdir_with_parents (const char *pathname,
+ int mode,
+ bool create_last)
+{
+ cleanup_free char *fn = NULL;
+ char *p;
+ struct stat buf;
+
+ if (pathname == NULL || *pathname == '\0')
+ {
+ errno = EINVAL;
+ return -1;
+ }
+
+ fn = xstrdup (pathname);
+
+ p = fn;
+ while (*p == '/')
+ p++;
+
+ do
+ {
+ while (*p && *p != '/')
+ p++;
+
+ if (!*p)
+ p = NULL;
+ else
+ *p = '\0';
+
+ if (!create_last && p == NULL)
+ break;
+
+ if (stat (fn, &buf) != 0)
+ {
+ if (mkdir (fn, mode) == -1 && errno != EEXIST)
+ return -1;
+ }
+ else if (!S_ISDIR (buf.st_mode))
+ {
+ errno = ENOTDIR;
+ return -1;
+ }
+
+ if (p)
+ {
+ *p++ = '/';
+ while (*p && *p == '/')
+ p++;
+ }
+ }
+ while (p);
+
+ return 0;
+}
+
+int
raw_clone (unsigned long flags,
void *child_stack)
{
diff --git a/utils.h b/utils.h
index 97f9ee2..dc3238a 100644
--- a/utils.h
+++ b/utils.h
@@ -46,7 +46,6 @@ typedef int bool;
#define PIPE_READ_END 0
#define PIPE_WRITE_END 1
-void strfreev (char **str_array);
void die_with_error (const char *format,
...);
void die (const char *format,
@@ -57,6 +56,7 @@ void *xcalloc (size_t size);
void *xrealloc (void *ptr,
size_t size);
char *xstrdup (const char *str);
+void strfreev (char **str_array);
void xsetenv (const char *name,
const char *value,
int overwrite);
@@ -68,9 +68,22 @@ char *strconcat3 (const char *s1,
const char *s3);
char* strdup_printf (const char *format,
...);
+bool has_prefix (const char *str,
+ const char *prefix);
int fdwalk (int proc_fd,
int (*cb)(void *data, int fd),
void *data);
+char *load_file_at (int dirfd,
+ const char *path);
+int write_file_at (int dirfd,
+ const char *path,
+ const char *content);
+int write_to_fd (int fd,
+ const char *content,
+ ssize_t len);
+int mkdir_with_parents (const char *pathname,
+ int mode,
+ bool create_last);
/* syscall wrappers */
int raw_clone (unsigned long flags,