|
|
28bab8 |
From f9ff58e8cde1d3b6f5eb5307e2019d54c5e28487 Mon Sep 17 00:00:00 2001
|
|
|
28bab8 |
From: Matej Habrnal <mhabrnal@redhat.com>
|
|
|
28bab8 |
Date: Thu, 11 Feb 2016 17:29:50 +0100
|
|
|
28bab8 |
Subject: [PATCH] dd: add a function for compressing dumpdirs
|
|
|
28bab8 |
|
|
|
28bab8 |
Introduce dd_create_archive() function.
|
|
|
28bab8 |
|
|
|
28bab8 |
I added the argument 'flags' in order to avoid the need to introduce
|
|
|
28bab8 |
dd_create_archive_ext() function in the future. I am sure will use this
|
|
|
28bab8 |
flag in the future (e.g. Encrypted).
|
|
|
28bab8 |
|
|
|
28bab8 |
Signed-off-by: Jakub Filak <jfilak@redhat.com>
|
|
|
28bab8 |
Signed-off-by: Matej Habrnal <mhabrnal@redhat.com>
|
|
|
28bab8 |
---
|
|
|
28bab8 |
po/POTFILES.in | 1 +
|
|
|
28bab8 |
src/include/dump_dir.h | 26 +++++
|
|
|
28bab8 |
src/lib/Makefile.am | 1 +
|
|
|
28bab8 |
src/lib/dump_dir.c | 117 ++++++++++++++++++++
|
|
|
28bab8 |
src/plugins/Makefile.am | 2 -
|
|
|
28bab8 |
tests/dump_dir.at | 275 ++++++++++++++++++++++++++++++++++++++++++++++++
|
|
|
28bab8 |
6 files changed, 420 insertions(+), 2 deletions(-)
|
|
|
28bab8 |
|
|
|
28bab8 |
diff --git a/po/POTFILES.in b/po/POTFILES.in
|
|
|
28bab8 |
index 00046e2..30c9cb5 100644
|
|
|
28bab8 |
--- a/po/POTFILES.in
|
|
|
28bab8 |
+++ b/po/POTFILES.in
|
|
|
28bab8 |
@@ -18,6 +18,7 @@ src/lib/abrt_sock.c
|
|
|
28bab8 |
src/lib/client.c
|
|
|
28bab8 |
src/lib/create_dump_dir.c
|
|
|
28bab8 |
src/lib/curl.c
|
|
|
28bab8 |
+src/lib/dump_dir.c
|
|
|
28bab8 |
src/lib/event_config.c
|
|
|
28bab8 |
src/lib/ureport.c
|
|
|
28bab8 |
src/lib/make_descr.c
|
|
|
28bab8 |
diff --git a/src/include/dump_dir.h b/src/include/dump_dir.h
|
|
|
28bab8 |
index 07b119a..092ddeb 100644
|
|
|
28bab8 |
--- a/src/include/dump_dir.h
|
|
|
28bab8 |
+++ b/src/include/dump_dir.h
|
|
|
28bab8 |
@@ -21,6 +21,9 @@
|
|
|
28bab8 |
#ifndef LIBREPORT_DUMP_DIR_H_
|
|
|
28bab8 |
#define LIBREPORT_DUMP_DIR_H_
|
|
|
28bab8 |
|
|
|
28bab8 |
+/* For const_string_vector_const_ptr_t */
|
|
|
28bab8 |
+#include "libreport_types.h"
|
|
|
28bab8 |
+
|
|
|
28bab8 |
/* For DIR */
|
|
|
28bab8 |
#include <sys/types.h>
|
|
|
28bab8 |
#include <dirent.h>
|
|
|
28bab8 |
@@ -178,6 +181,29 @@ int fdump_dir_stat_for_uid(int dir_fd, uid_t uid);
|
|
|
28bab8 |
*/
|
|
|
28bab8 |
int dd_mark_as_notreportable(struct dump_dir *dd, const char *reason);
|
|
|
28bab8 |
|
|
|
28bab8 |
+/* Creates a new archive from the dump directory contents
|
|
|
28bab8 |
+ *
|
|
|
28bab8 |
+ * The dd argument must be opened for reading.
|
|
|
28bab8 |
+ *
|
|
|
28bab8 |
+ * The archive_name must not exist. The file will be created with 0600 mode.
|
|
|
28bab8 |
+ *
|
|
|
28bab8 |
+ * The archive type is deduced from archive_name suffix. The supported archive
|
|
|
28bab8 |
+ * suffixes are the following:
|
|
|
28bab8 |
+ * - '.tag.gz' (note: the implementation uses child gzip process)
|
|
|
28bab8 |
+ *
|
|
|
28bab8 |
+ * The archive will include only the files that are not in the exclude_elements
|
|
|
28bab8 |
+ * list. See get_global_always_excluded_elements().
|
|
|
28bab8 |
+ *
|
|
|
28bab8 |
+ * The argument "flags" is currently unused.
|
|
|
28bab8 |
+ *
|
|
|
28bab8 |
+ * @return 0 on success; otherwise non-0 value. -ENOSYS if archive type is not
|
|
|
28bab8 |
+ * supported. -EEXIST if the archive file already exists. -ECHILD if child
|
|
|
28bab8 |
+ * process fails. Other negative values can be converted to errno values by
|
|
|
28bab8 |
+ * turning them positive.
|
|
|
28bab8 |
+ */
|
|
|
28bab8 |
+int dd_create_archive(struct dump_dir *dd, const char *archive_name,
|
|
|
28bab8 |
+ const_string_vector_const_ptr_t exclude_elements, int flags);
|
|
|
28bab8 |
+
|
|
|
28bab8 |
#ifdef __cplusplus
|
|
|
28bab8 |
}
|
|
|
28bab8 |
#endif
|
|
|
28bab8 |
diff --git a/src/lib/Makefile.am b/src/lib/Makefile.am
|
|
|
28bab8 |
index f9ea602..50142f7 100644
|
|
|
28bab8 |
--- a/src/lib/Makefile.am
|
|
|
28bab8 |
+++ b/src/lib/Makefile.am
|
|
|
28bab8 |
@@ -79,6 +79,7 @@ libreport_la_CPPFLAGS = \
|
|
|
28bab8 |
$(AUGEAS_CFLAGS) \
|
|
|
28bab8 |
-D_GNU_SOURCE
|
|
|
28bab8 |
libreport_la_LDFLAGS = \
|
|
|
28bab8 |
+ -ltar \
|
|
|
28bab8 |
-version-info 0:1:0
|
|
|
28bab8 |
libreport_la_LIBADD = \
|
|
|
28bab8 |
$(JSON_C_LIBS) \
|
|
|
28bab8 |
diff --git a/src/lib/dump_dir.c b/src/lib/dump_dir.c
|
|
|
28bab8 |
index 9096853..a5cd93e 100644
|
|
|
28bab8 |
--- a/src/lib/dump_dir.c
|
|
|
28bab8 |
+++ b/src/lib/dump_dir.c
|
|
|
28bab8 |
@@ -17,6 +17,7 @@
|
|
|
28bab8 |
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
28bab8 |
*/
|
|
|
28bab8 |
#include <sys/utsname.h>
|
|
|
28bab8 |
+#include <libtar.h>
|
|
|
28bab8 |
#include "internal_libreport.h"
|
|
|
28bab8 |
|
|
|
28bab8 |
// Locking logic:
|
|
|
28bab8 |
@@ -1475,3 +1476,119 @@ int dd_mark_as_notreportable(struct dump_dir *dd, const char *reason)
|
|
|
28bab8 |
dd_save_text(dd, FILENAME_NOT_REPORTABLE, reason);
|
|
|
28bab8 |
return 0;
|
|
|
28bab8 |
}
|
|
|
28bab8 |
+
|
|
|
28bab8 |
+/* flags - for future needs */
|
|
|
28bab8 |
+int dd_create_archive(struct dump_dir *dd, const char *archive_name,
|
|
|
28bab8 |
+ const_string_vector_const_ptr_t exclude_elements, int flags)
|
|
|
28bab8 |
+{
|
|
|
28bab8 |
+ if (suffixcmp(archive_name, ".tar.gz") != 0)
|
|
|
28bab8 |
+ return -ENOSYS;
|
|
|
28bab8 |
+
|
|
|
28bab8 |
+ int result = 0;
|
|
|
28bab8 |
+ pid_t child;
|
|
|
28bab8 |
+ TAR* tar = NULL;
|
|
|
28bab8 |
+ int pipe_from_parent_to_child[2];
|
|
|
28bab8 |
+ xpipe(pipe_from_parent_to_child);
|
|
|
28bab8 |
+ child = fork();
|
|
|
28bab8 |
+ if (child < 0)
|
|
|
28bab8 |
+ {
|
|
|
28bab8 |
+ result = -errno;
|
|
|
28bab8 |
+ /* Don't die, let the caller to execute his clean-up code. */
|
|
|
28bab8 |
+ perror_msg("vfork");
|
|
|
28bab8 |
+ return result;
|
|
|
28bab8 |
+ }
|
|
|
28bab8 |
+ if (child == 0)
|
|
|
28bab8 |
+ {
|
|
|
28bab8 |
+ /* child */
|
|
|
28bab8 |
+ close(pipe_from_parent_to_child[1]);
|
|
|
28bab8 |
+ xmove_fd(pipe_from_parent_to_child[0], 0);
|
|
|
28bab8 |
+
|
|
|
28bab8 |
+ int fd = open(archive_name, O_WRONLY | O_CREAT | O_EXCL, 0600);
|
|
|
28bab8 |
+ if (fd < 0)
|
|
|
28bab8 |
+ {
|
|
|
28bab8 |
+ /* This r might interfer with exit status of gzip, but it is
|
|
|
28bab8 |
+ * very unlikely (man 1 gzip):
|
|
|
28bab8 |
+ * Exit status is normally 0; if an error occurs, exit status is
|
|
|
28bab8 |
+ * 1. If a warning occurs, exit status is 2.
|
|
|
28bab8 |
+ */
|
|
|
28bab8 |
+ result = errno == EEXIST ? 100 : 3;
|
|
|
28bab8 |
+ perror_msg("Can't open '%s'", archive_name);
|
|
|
28bab8 |
+ exit(result);
|
|
|
28bab8 |
+ }
|
|
|
28bab8 |
+
|
|
|
28bab8 |
+ xmove_fd(fd, 1);
|
|
|
28bab8 |
+ execlp("gzip", "gzip", NULL);
|
|
|
28bab8 |
+ perror_msg_and_die("Can't execute '%s'", "gzip");
|
|
|
28bab8 |
+ }
|
|
|
28bab8 |
+ close(pipe_from_parent_to_child[0]);
|
|
|
28bab8 |
+
|
|
|
28bab8 |
+ /* If child died (say, in xopen), then parent might get SIGPIPE.
|
|
|
28bab8 |
+ * We want to properly unlock dd, therefore we must not die on SIGPIPE:
|
|
|
28bab8 |
+ */
|
|
|
28bab8 |
+ sighandler_t old_handler = signal(SIGPIPE, SIG_IGN);
|
|
|
28bab8 |
+
|
|
|
28bab8 |
+ /* Create tar writer object */
|
|
|
28bab8 |
+ if (tar_fdopen(&tar, pipe_from_parent_to_child[1], (char *)archive_name,
|
|
|
28bab8 |
+ /*fileops:(standard)*/ NULL, O_WRONLY | O_CREAT, 0644, TAR_GNU) != 0)
|
|
|
28bab8 |
+ {
|
|
|
28bab8 |
+ result = -errno;
|
|
|
28bab8 |
+ log_warning(_("Failed to open TAR writer"));
|
|
|
28bab8 |
+ goto finito;
|
|
|
28bab8 |
+ }
|
|
|
28bab8 |
+
|
|
|
28bab8 |
+ /* Write data to the tarball */
|
|
|
28bab8 |
+ dd_init_next_file(dd);
|
|
|
28bab8 |
+ char *short_name, *full_name;
|
|
|
28bab8 |
+ while (dd_get_next_file(dd, &short_name, &full_name))
|
|
|
28bab8 |
+ {
|
|
|
28bab8 |
+ if (!(exclude_elements && is_in_string_list(short_name, exclude_elements)))
|
|
|
28bab8 |
+ {
|
|
|
28bab8 |
+ if (tar_append_file(tar, full_name, short_name))
|
|
|
28bab8 |
+ result = -errno;
|
|
|
28bab8 |
+ }
|
|
|
28bab8 |
+
|
|
|
28bab8 |
+ free(short_name);
|
|
|
28bab8 |
+ free(full_name);
|
|
|
28bab8 |
+
|
|
|
28bab8 |
+ if (result != 0)
|
|
|
28bab8 |
+ goto finito;
|
|
|
28bab8 |
+ }
|
|
|
28bab8 |
+
|
|
|
28bab8 |
+ /* Close tar writer... */
|
|
|
28bab8 |
+ if (tar_append_eof(tar) != 0)
|
|
|
28bab8 |
+ {
|
|
|
28bab8 |
+ result = -errno;
|
|
|
28bab8 |
+ log_warning(_("Failed to finalize TAR archive"));
|
|
|
28bab8 |
+ goto finito;
|
|
|
28bab8 |
+ }
|
|
|
28bab8 |
+
|
|
|
28bab8 |
+finito:
|
|
|
28bab8 |
+ signal(SIGPIPE, old_handler);
|
|
|
28bab8 |
+
|
|
|
28bab8 |
+ if (tar != NULL && tar_close(tar) != 0)
|
|
|
28bab8 |
+ {
|
|
|
28bab8 |
+ result = -errno;
|
|
|
28bab8 |
+ log_warning(_("Failed to close TAR writer"));
|
|
|
28bab8 |
+ }
|
|
|
28bab8 |
+
|
|
|
28bab8 |
+ /* ...and check that gzip child finished successfully */
|
|
|
28bab8 |
+ int status;
|
|
|
28bab8 |
+ safe_waitpid(child, &status, 0);
|
|
|
28bab8 |
+ if (status != 0)
|
|
|
28bab8 |
+ {
|
|
|
28bab8 |
+ result = -ECHILD;
|
|
|
28bab8 |
+ if (WIFSIGNALED(status))
|
|
|
28bab8 |
+ log_warning(_("gzip killed with signal %d"), WTERMSIG(status));
|
|
|
28bab8 |
+ else if (WIFEXITED(status))
|
|
|
28bab8 |
+ {
|
|
|
28bab8 |
+ if (WEXITSTATUS(status) == 100)
|
|
|
28bab8 |
+ result = -EEXIST;
|
|
|
28bab8 |
+ else
|
|
|
28bab8 |
+ log_warning(_("gzip exited with %d"), WEXITSTATUS(status));
|
|
|
28bab8 |
+ }
|
|
|
28bab8 |
+ else
|
|
|
28bab8 |
+ log_warning(_("gzip process failed"));
|
|
|
28bab8 |
+ }
|
|
|
28bab8 |
+
|
|
|
28bab8 |
+ return result;
|
|
|
28bab8 |
+}
|
|
|
28bab8 |
diff --git a/src/plugins/Makefile.am b/src/plugins/Makefile.am
|
|
|
28bab8 |
index 7ec08d7..d5d75b6 100644
|
|
|
28bab8 |
--- a/src/plugins/Makefile.am
|
|
|
28bab8 |
+++ b/src/plugins/Makefile.am
|
|
|
28bab8 |
@@ -145,7 +145,6 @@ reporter_rhtsupport_CPPFLAGS = \
|
|
|
28bab8 |
$(LIBREPORT_CFLAGS) \
|
|
|
28bab8 |
$(LIBXML_CFLAGS) \
|
|
|
28bab8 |
-D_GNU_SOURCE
|
|
|
28bab8 |
-reporter_rhtsupport_LDFLAGS = -ltar
|
|
|
28bab8 |
reporter_rhtsupport_LDADD = \
|
|
|
28bab8 |
$(GLIB_LIBS) \
|
|
|
28bab8 |
$(LIBXML_LIBS) \
|
|
|
28bab8 |
@@ -168,7 +167,6 @@ reporter_upload_CPPFLAGS = \
|
|
|
28bab8 |
$(CURL_CFLAGS) \
|
|
|
28bab8 |
$(LIBREPORT_CFLAGS) \
|
|
|
28bab8 |
-D_GNU_SOURCE
|
|
|
28bab8 |
-reporter_upload_LDFLAGS = -ltar
|
|
|
28bab8 |
reporter_upload_LDADD = \
|
|
|
28bab8 |
$(GLIB_LIBS) \
|
|
|
28bab8 |
../lib/libreport-web.la \
|
|
|
28bab8 |
diff --git a/tests/dump_dir.at b/tests/dump_dir.at
|
|
|
28bab8 |
index a579243..fb8c7ce 100644
|
|
|
28bab8 |
--- a/tests/dump_dir.at
|
|
|
28bab8 |
+++ b/tests/dump_dir.at
|
|
|
28bab8 |
@@ -47,3 +47,278 @@ int main(void)
|
|
|
28bab8 |
return 0;
|
|
|
28bab8 |
}
|
|
|
28bab8 |
]])
|
|
|
28bab8 |
+
|
|
|
28bab8 |
+## ----------------- ##
|
|
|
28bab8 |
+## dd_create_archive ##
|
|
|
28bab8 |
+## ----------------- ##
|
|
|
28bab8 |
+
|
|
|
28bab8 |
+AT_TESTFUN([dd_create_archive],
|
|
|
28bab8 |
+[[
|
|
|
28bab8 |
+#include "internal_libreport.h"
|
|
|
28bab8 |
+#include <libtar.h>
|
|
|
28bab8 |
+#include <assert.h>
|
|
|
28bab8 |
+
|
|
|
28bab8 |
+void verify_archive(struct dump_dir *dd, const char *file_name,
|
|
|
28bab8 |
+ const_string_vector_const_ptr_t included_files,
|
|
|
28bab8 |
+ const_string_vector_const_ptr_t excluded_files)
|
|
|
28bab8 |
+{
|
|
|
28bab8 |
+ unsigned c = 0;
|
|
|
28bab8 |
+ for (const_string_vector_const_ptr_t i = included_files; i && *i; ++i)
|
|
|
28bab8 |
+ ++c;
|
|
|
28bab8 |
+ int *check_array = xzalloc(c * sizeof(int));
|
|
|
28bab8 |
+
|
|
|
28bab8 |
+ int pipe_from_parent_to_child[2];
|
|
|
28bab8 |
+ xpipe(pipe_from_parent_to_child);
|
|
|
28bab8 |
+ pid_t child = fork();
|
|
|
28bab8 |
+ if (child < 0)
|
|
|
28bab8 |
+ perror_msg_and_die("vfork");
|
|
|
28bab8 |
+
|
|
|
28bab8 |
+ if (child == 0)
|
|
|
28bab8 |
+ {
|
|
|
28bab8 |
+ /* child */
|
|
|
28bab8 |
+ close(pipe_from_parent_to_child[0]);
|
|
|
28bab8 |
+ xmove_fd(xopen(file_name, O_RDONLY), 0);
|
|
|
28bab8 |
+ xmove_fd(pipe_from_parent_to_child[1], 1);
|
|
|
28bab8 |
+ execlp("gzip", "gzip", "-d", NULL);
|
|
|
28bab8 |
+ perror_msg_and_die("Can't execute '%s'", "gzip");
|
|
|
28bab8 |
+ }
|
|
|
28bab8 |
+ close(pipe_from_parent_to_child[1]);
|
|
|
28bab8 |
+
|
|
|
28bab8 |
+ /* If child died (say, in xopen), then parent might get SIGPIPE.
|
|
|
28bab8 |
+ * We want to properly unlock dd, therefore we must not die on SIGPIPE:
|
|
|
28bab8 |
+ */
|
|
|
28bab8 |
+ signal(SIGPIPE, SIG_IGN);
|
|
|
28bab8 |
+
|
|
|
28bab8 |
+ TAR* tar = NULL;
|
|
|
28bab8 |
+ /* Create tar writer object */
|
|
|
28bab8 |
+ if (tar_fdopen(&tar, pipe_from_parent_to_child[0], (char *)file_name,
|
|
|
28bab8 |
+ /*fileops:(standard)*/ NULL, O_RDONLY, 0644, TAR_GNU) != 0)
|
|
|
28bab8 |
+ {
|
|
|
28bab8 |
+ fprintf(stderr, "Failed to open the pipe to gzip for archive: '%s'\n", file_name);
|
|
|
28bab8 |
+ abort();
|
|
|
28bab8 |
+ }
|
|
|
28bab8 |
+
|
|
|
28bab8 |
+ int r = 0;
|
|
|
28bab8 |
+ const char *real_file = "/tmp/libreport-attest-extracted";
|
|
|
28bab8 |
+ while ((r = th_read(tar)) == 0)
|
|
|
28bab8 |
+ {
|
|
|
28bab8 |
+ char *path = th_get_pathname(tar);
|
|
|
28bab8 |
+
|
|
|
28bab8 |
+ if (!TH_ISREG(tar))
|
|
|
28bab8 |
+ {
|
|
|
28bab8 |
+ fprintf(stderr, "Not regular file: '%s', found in archive: '%s'\n", path, file_name);
|
|
|
28bab8 |
+ continue;
|
|
|
28bab8 |
+ }
|
|
|
28bab8 |
+
|
|
|
28bab8 |
+ const_string_vector_const_ptr_t i = included_files;
|
|
|
28bab8 |
+ for (c = 0; i && *i; ++i, ++c)
|
|
|
28bab8 |
+ {
|
|
|
28bab8 |
+ if (strcmp(*i, path) == 0)
|
|
|
28bab8 |
+ break;
|
|
|
28bab8 |
+ }
|
|
|
28bab8 |
+
|
|
|
28bab8 |
+ if (i && *i != NULL)
|
|
|
28bab8 |
+ {
|
|
|
28bab8 |
+ printf("Included file: '%s', found in archive '%s'\n", path, file_name);
|
|
|
28bab8 |
+ check_array[c] += 1;
|
|
|
28bab8 |
+
|
|
|
28bab8 |
+ unlink(real_file);
|
|
|
28bab8 |
+ if(tar_extract_regfile(tar, xstrdup(real_file)) != 0)
|
|
|
28bab8 |
+ {
|
|
|
28bab8 |
+ fprintf(stderr, "TAR failed to extract '%s' to '%s': %s\n", path, real_file, strerror(errno));
|
|
|
28bab8 |
+ abort();
|
|
|
28bab8 |
+ }
|
|
|
28bab8 |
+
|
|
|
28bab8 |
+ char *original = dd_load_text(dd, path);
|
|
|
28bab8 |
+ assert(original != NULL);
|
|
|
28bab8 |
+ assert(original[0] != '\0');
|
|
|
28bab8 |
+
|
|
|
28bab8 |
+ char *extracted = xmalloc_xopen_read_close("/tmp/libreport-attest-extracted", NULL);
|
|
|
28bab8 |
+ assert(extracted != NULL);
|
|
|
28bab8 |
+
|
|
|
28bab8 |
+ if (strcmp(extracted, original) != 0)
|
|
|
28bab8 |
+ {
|
|
|
28bab8 |
+ fprintf(stderr, "Invalid file contents: '%s'\nExp: '%s'\nGot: '%s'\n", path, original, extracted);
|
|
|
28bab8 |
+ abort();
|
|
|
28bab8 |
+ }
|
|
|
28bab8 |
+
|
|
|
28bab8 |
+ free(original);
|
|
|
28bab8 |
+ free(extracted);
|
|
|
28bab8 |
+
|
|
|
28bab8 |
+ continue;
|
|
|
28bab8 |
+ }
|
|
|
28bab8 |
+
|
|
|
28bab8 |
+ i = excluded_files;
|
|
|
28bab8 |
+ for (; i && *i; ++i)
|
|
|
28bab8 |
+ {
|
|
|
28bab8 |
+ if (strcmp(*i, path) == 0)
|
|
|
28bab8 |
+ break;
|
|
|
28bab8 |
+ }
|
|
|
28bab8 |
+
|
|
|
28bab8 |
+ if (i && *i != NULL)
|
|
|
28bab8 |
+ {
|
|
|
28bab8 |
+ fprintf(stderr, "Excluded file: '%s', found in archive '%s'\n", path, file_name);
|
|
|
28bab8 |
+ abort();
|
|
|
28bab8 |
+ }
|
|
|
28bab8 |
+
|
|
|
28bab8 |
+ fprintf(stderr, "Uncategorized file: '%s', found in archive '%s'\n", path, file_name);
|
|
|
28bab8 |
+ }
|
|
|
28bab8 |
+
|
|
|
28bab8 |
+ if (r != 1)
|
|
|
28bab8 |
+ {
|
|
|
28bab8 |
+ fprintf(stderr, "th_read() failed: %s\n", strerror(errno));
|
|
|
28bab8 |
+ abort();
|
|
|
28bab8 |
+ }
|
|
|
28bab8 |
+
|
|
|
28bab8 |
+ tar_close(tar);
|
|
|
28bab8 |
+
|
|
|
28bab8 |
+ int status;
|
|
|
28bab8 |
+ safe_waitpid(child, &status, 0);
|
|
|
28bab8 |
+ if (status != 0)
|
|
|
28bab8 |
+ {
|
|
|
28bab8 |
+ fprintf(stderr, "gzip status code '%d'\n", status);
|
|
|
28bab8 |
+ abort();
|
|
|
28bab8 |
+ }
|
|
|
28bab8 |
+
|
|
|
28bab8 |
+ int err = 0;
|
|
|
28bab8 |
+ const_string_vector_const_ptr_t i = included_files;
|
|
|
28bab8 |
+ for (c = 0; i && *i; ++i, ++c)
|
|
|
28bab8 |
+ {
|
|
|
28bab8 |
+ switch (check_array[c])
|
|
|
28bab8 |
+ {
|
|
|
28bab8 |
+ case 0:
|
|
|
28bab8 |
+ fprintf(stderr, "Not found included file: '%s', in archive: %s\n", *i, file_name);
|
|
|
28bab8 |
+ ++err;
|
|
|
28bab8 |
+ break;
|
|
|
28bab8 |
+ case 1:
|
|
|
28bab8 |
+ fprintf(stdout, "Found included file: '%s', in archive: %s\n", *i, file_name);
|
|
|
28bab8 |
+ break;
|
|
|
28bab8 |
+ default:
|
|
|
28bab8 |
+ fprintf(stderr, "%d occurrences of included file: '%s', in archive: %s\n", check_array[c], *i, file_name);
|
|
|
28bab8 |
+ ++err;
|
|
|
28bab8 |
+ break;
|
|
|
28bab8 |
+ }
|
|
|
28bab8 |
+ }
|
|
|
28bab8 |
+
|
|
|
28bab8 |
+ if (err)
|
|
|
28bab8 |
+ abort();
|
|
|
28bab8 |
+
|
|
|
28bab8 |
+ return;
|
|
|
28bab8 |
+}
|
|
|
28bab8 |
+
|
|
|
28bab8 |
+int main(void)
|
|
|
28bab8 |
+{
|
|
|
28bab8 |
+ g_verbose = 3;
|
|
|
28bab8 |
+
|
|
|
28bab8 |
+ char template[] = "/tmp/XXXXXX";
|
|
|
28bab8 |
+
|
|
|
28bab8 |
+ if (mkdtemp(template) == NULL) {
|
|
|
28bab8 |
+ perror("mkdtemp()");
|
|
|
28bab8 |
+ return EXIT_FAILURE;
|
|
|
28bab8 |
+ }
|
|
|
28bab8 |
+
|
|
|
28bab8 |
+ printf("Dump dir path: %s\n", template);
|
|
|
28bab8 |
+
|
|
|
28bab8 |
+ struct dump_dir *dd = dd_create(template, (uid_t)-1, 0640);
|
|
|
28bab8 |
+ assert(dd != NULL || !"Cannot create new dump directory");
|
|
|
28bab8 |
+
|
|
|
28bab8 |
+
|
|
|
28bab8 |
+#define COMMON_FILES "time", "last_occurrence", "uid", "kernel", \
|
|
|
28bab8 |
+ "architecture", "hostname", "os_info", "os_release", \
|
|
|
28bab8 |
+ "type", "count", "component", "program_log"
|
|
|
28bab8 |
+#define SENSITIVE_FILES "environ", "backtrace", "secret_file", "private_file", \
|
|
|
28bab8 |
+ "useless_file"
|
|
|
28bab8 |
+
|
|
|
28bab8 |
+ dd_create_basic_files(dd, geteuid(), NULL);
|
|
|
28bab8 |
+ dd_save_text(dd, FILENAME_TYPE, "attest");
|
|
|
28bab8 |
+ dd_save_text(dd, FILENAME_COUNT, "1");
|
|
|
28bab8 |
+ dd_save_text(dd, FILENAME_COMPONENT, "libreport-attest");
|
|
|
28bab8 |
+ dd_save_text(dd, "program_log", "Something very important!");
|
|
|
28bab8 |
+
|
|
|
28bab8 |
+ const gchar *excluded_files[] = {
|
|
|
28bab8 |
+ SENSITIVE_FILES,
|
|
|
28bab8 |
+ NULL,
|
|
|
28bab8 |
+ };
|
|
|
28bab8 |
+
|
|
|
28bab8 |
+ for (const gchar **iter = excluded_files; *iter; ++iter)
|
|
|
28bab8 |
+ dd_save_text(dd, *iter, *iter);
|
|
|
28bab8 |
+
|
|
|
28bab8 |
+ /* Un-supported archive type */
|
|
|
28bab8 |
+ {
|
|
|
28bab8 |
+ fprintf(stderr, "TEST-CASE: Un-supported type\n");
|
|
|
28bab8 |
+ fprintf(stdout, "TEST-CASE: Un-supported type\n");
|
|
|
28bab8 |
+ const int r = dd_create_archive(dd, "/tmp/libreport-attest.omg", NULL, 0);
|
|
|
28bab8 |
+ printf("dd_create_archive() == %d\n", r);
|
|
|
28bab8 |
+ assert(r == -ENOSYS || !"Not supported");
|
|
|
28bab8 |
+ }
|
|
|
28bab8 |
+
|
|
|
28bab8 |
+ /* File already exists. */
|
|
|
28bab8 |
+ dd_close(dd);
|
|
|
28bab8 |
+ dd = dd_opendir(template, DD_OPEN_READONLY);
|
|
|
28bab8 |
+ {
|
|
|
28bab8 |
+ fprintf(stderr, "TEST-CASE: File exists\n");
|
|
|
28bab8 |
+ fprintf(stdout, "TEST-CASE: File exists\n");
|
|
|
28bab8 |
+ char file_contents[] = "Non emtpy file";
|
|
|
28bab8 |
+ const char *file_name = "/tmp/libreport-attest.tar.gz";
|
|
|
28bab8 |
+ FILE *test_file = fopen(file_name, "w");
|
|
|
28bab8 |
+ assert(test_file != NULL);
|
|
|
28bab8 |
+ assert(fprintf(test_file, "%s", file_contents) == strlen(file_contents));
|
|
|
28bab8 |
+ fclose(test_file);
|
|
|
28bab8 |
+
|
|
|
28bab8 |
+ assert(dd_create_archive(dd, file_name, NULL, 0) == -EEXIST || !"Exists");
|
|
|
28bab8 |
+
|
|
|
28bab8 |
+ char *canary = xmalloc_xopen_read_close(file_name, NULL);
|
|
|
28bab8 |
+ assert(canary != NULL);
|
|
|
28bab8 |
+ assert(strcmp(canary, file_contents) == 0);
|
|
|
28bab8 |
+ }
|
|
|
28bab8 |
+
|
|
|
28bab8 |
+ dd_close(dd);
|
|
|
28bab8 |
+ dd = dd_opendir(template, DD_OPEN_READONLY);
|
|
|
28bab8 |
+ /* All elements */
|
|
|
28bab8 |
+ {
|
|
|
28bab8 |
+ fprintf(stderr, "TEST-CASE: Compress all elements\n");
|
|
|
28bab8 |
+ fprintf(stdout, "TEST-CASE: Compress all elements\n");
|
|
|
28bab8 |
+
|
|
|
28bab8 |
+ const gchar *included_files[] = {
|
|
|
28bab8 |
+ COMMON_FILES,
|
|
|
28bab8 |
+ SENSITIVE_FILES,
|
|
|
28bab8 |
+ NULL,
|
|
|
28bab8 |
+ };
|
|
|
28bab8 |
+
|
|
|
28bab8 |
+ const char *file_name = "/tmp/libreport-attest-all.tar.gz";
|
|
|
28bab8 |
+ unlink(file_name);
|
|
|
28bab8 |
+ assert(dd_create_archive(dd, file_name, NULL, 0) == 0 || !"All elements");
|
|
|
28bab8 |
+
|
|
|
28bab8 |
+ verify_archive(dd, file_name, included_files, NULL);
|
|
|
28bab8 |
+
|
|
|
28bab8 |
+ unlink(file_name);
|
|
|
28bab8 |
+ }
|
|
|
28bab8 |
+
|
|
|
28bab8 |
+ dd_close(dd);
|
|
|
28bab8 |
+ dd = dd_opendir(template, DD_OPEN_READONLY);
|
|
|
28bab8 |
+ /* Excluded elements */
|
|
|
28bab8 |
+ {
|
|
|
28bab8 |
+ fprintf(stderr, "TEST-CASE: Exclude elements\n");
|
|
|
28bab8 |
+ fprintf(stdout, "TEST-CASE: Exclude elements\n");
|
|
|
28bab8 |
+
|
|
|
28bab8 |
+ const char *included_files[] = {
|
|
|
28bab8 |
+ COMMON_FILES,
|
|
|
28bab8 |
+ NULL,
|
|
|
28bab8 |
+ };
|
|
|
28bab8 |
+
|
|
|
28bab8 |
+ const char *file_name = "/tmp/libreport-attest-excluded.tar.gz";
|
|
|
28bab8 |
+ unlink(file_name);
|
|
|
28bab8 |
+ assert(dd_create_archive(dd, file_name, excluded_files, 0) == 0 || !"Excluded elements");
|
|
|
28bab8 |
+
|
|
|
28bab8 |
+ verify_archive(dd, file_name, included_files, excluded_files);
|
|
|
28bab8 |
+
|
|
|
28bab8 |
+ unlink(file_name);
|
|
|
28bab8 |
+ }
|
|
|
28bab8 |
+
|
|
|
28bab8 |
+ dd_close(dd);
|
|
|
28bab8 |
+ dd = dd_opendir(template, DD_OPEN_READONLY);
|
|
|
28bab8 |
+ assert(dd_delete(dd) == 0);
|
|
|
28bab8 |
+
|
|
|
28bab8 |
+ return 0;
|
|
|
28bab8 |
+}
|
|
|
28bab8 |
+]])
|
|
|
28bab8 |
--
|
|
|
28bab8 |
1.8.3.1
|
|
|
28bab8 |
|