Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/{snap-confine,libsnap-confine-private,snap-shutdown}: cleanup low-level C bits #4153

Merged
merged 14 commits into from
Nov 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 13 additions & 4 deletions cmd/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ dist_man_MANS =
noinst_PROGRAMS =
noinst_LIBRARIES =

CHECK_CFLAGS = -Wall -Wextra -Wmissing-prototypes -Wstrict-prototypes \
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gentoo has a policy where those are not added automatically as they tend to break good builds due to new compiler releases. I was wondering if those flags should not be in the unit tests, and instead we could resort to perhaps, just, -Wall

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting, do you know if they push -Werror by default? Just having the warnings enabled should not cause build failures.

-Wno-missing-field-initializers -Wno-unused-parameter

# Make all warnings errors when building for unit tests
if WITH_UNIT_TESTS
CHECK_CFLAGS += -Werror
endif

subdirs = snap-confine snap-discard-ns system-shutdown libsnap-confine-private

# Run check-syntax when checking
Expand Down Expand Up @@ -90,6 +98,7 @@ libsnap_confine_private_a_SOURCES = \
libsnap-confine-private/string-utils.h \
libsnap-confine-private/utils.c \
libsnap-confine-private/utils.h
libsnap_confine_private_a_CFLAGS = $(CHECK_CFLAGS)

if WITH_UNIT_TESTS
noinst_PROGRAMS += libsnap-confine-private/unit-tests
Expand All @@ -111,7 +120,7 @@ libsnap_confine_private_unit_tests_SOURCES = \
libsnap-confine-private/unit-tests.c \
libsnap-confine-private/unit-tests.h \
libsnap-confine-private/utils-test.c
libsnap_confine_private_unit_tests_CFLAGS = $(GLIB_CFLAGS)
libsnap_confine_private_unit_tests_CFLAGS = $(CHECK_CFLAGS) $(GLIB_CFLAGS)
libsnap_confine_private_unit_tests_LDADD = $(GLIB_LIBS)
libsnap_confine_private_unit_tests_CFLAGS += -D_ENABLE_FAULT_INJECTION
libsnap_confine_private_unit_tests_STATIC =
Expand Down Expand Up @@ -190,7 +199,7 @@ snap_confine_snap_confine_SOURCES = \
snap-confine/user-support.c \
snap-confine/user-support.h

snap_confine_snap_confine_CFLAGS = -Wall -Werror $(AM_CFLAGS) -DLIBEXECDIR=\"$(libexecdir)\"
snap_confine_snap_confine_CFLAGS = $(CHECK_CFLAGS) $(AM_CFLAGS) -DLIBEXECDIR=\"$(libexecdir)\"
snap_confine_snap_confine_LDFLAGS = $(AM_LDFLAGS)
snap_confine_snap_confine_LDADD = libsnap-confine-private.a
snap_confine_snap_confine_CFLAGS += $(LIBUDEV_CFLAGS)
Expand Down Expand Up @@ -362,7 +371,7 @@ snap_discard_ns_snap_discard_ns_SOURCES = \
snap-confine/apparmor-support.c \
snap-confine/apparmor-support.h \
snap-discard-ns/snap-discard-ns.c
snap_discard_ns_snap_discard_ns_CFLAGS = -Wall -Werror $(AM_CFLAGS)
snap_discard_ns_snap_discard_ns_CFLAGS = $(CHECK_CFLAGS) $(AM_CFLAGS)
snap_discard_ns_snap_discard_ns_LDFLAGS = $(AM_LDFLAGS)
snap_discard_ns_snap_discard_ns_LDADD = libsnap-confine-private.a
snap_discard_ns_snap_discard_ns_STATIC =
Expand Down Expand Up @@ -406,7 +415,7 @@ system_shutdown_system_shutdown_SOURCES = \
system-shutdown/system-shutdown-utils.h \
system-shutdown/system-shutdown.c
system_shutdown_system_shutdown_LDADD = libsnap-confine-private.a
system_shutdown_system_shutdown_CFLAGS = $(filter-out -fPIE -pie,$(CFLAGS)) -static
system_shutdown_system_shutdown_CFLAGS = $(CHECK_CFLAGS) $(filter-out -fPIE -pie,$(CFLAGS)) -static
system_shutdown_system_shutdown_LDFLAGS = $(filter-out -fPIE -pie,$(LDFLAGS)) -static

if WITH_UNIT_TESTS
Expand Down
12 changes: 10 additions & 2 deletions cmd/autogen.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
#!/bin/sh
# Welcome to the Happy Maintainer's Utility Script
#
# Set BUILD_DIR to the directory where the build will happen, otherwise $PWD
# will be used
set -eux

BUILD_DIR=${BUILD_DIR:-.}
selfdir=$(dirname "$0")
SRC_DIR=$(readlink -f "$selfdir")

# We need the VERSION file to configure
if [ ! -e VERSION ]; then
( cd .. && ./mkversion.sh )
Expand Down Expand Up @@ -46,6 +53,7 @@ case "$ID" in
;;
esac

echo "Configuring with: $extra_opts"
echo "Configuring in build directory $BUILD_DIR with: $extra_opts"
mkdir -p "$BUILD_DIR" && cd "$BUILD_DIR"
# shellcheck disable=SC2086
./configure --enable-maintainer-mode --prefix=/usr $extra_opts
"${SRC_DIR}/configure" --enable-maintainer-mode --prefix=/usr $extra_opts
8 changes: 4 additions & 4 deletions cmd/libsnap-confine-private/classic-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const char *os_release_classic = ""
"NAME=\"Ubuntu\"\n"
"VERSION=\"17.04 (Zesty Zapus)\"\n" "ID=ubuntu\n" "ID_LIKE=debian\n";

static void test_is_on_classic()
static void test_is_on_classic(void)
{
g_file_set_contents("os-release.classic", os_release_classic,
strlen(os_release_classic), NULL);
Expand All @@ -36,7 +36,7 @@ static void test_is_on_classic()
const char *os_release_core = ""
"NAME=\"Ubuntu Core\"\n" "VERSION=\"16\"\n" "ID=ubuntu-core\n";

static void test_is_on_core()
static void test_is_on_core(void)
{
g_file_set_contents("os-release.core", os_release_core,
strlen(os_release_core), NULL);
Expand All @@ -52,7 +52,7 @@ const char *os_release_classic_with_long_line = ""
"ID_LIKE=debian\n"
"LONG=line.line.line.line.line.line.line.line.line.line.line.line.line.line.line.line.line.line.line.line.line.line.line.line.line.line.line.line.line.line.line.line.line.line.line.line.line.line.line.line.line.line.line.line.line.line.line.line.line.line.line.line.";

static void test_is_on_classic_with_long_line()
static void test_is_on_classic_with_long_line(void)
{
g_file_set_contents("os-release.classic-with-long-line",
os_release_classic, strlen(os_release_classic),
Expand All @@ -62,7 +62,7 @@ static void test_is_on_classic_with_long_line()
unlink("os-release.classic-with-long-line");
}

static void __attribute__ ((constructor)) init()
static void __attribute__ ((constructor)) init(void)
{
g_test_add_func("/classic/on-classic", test_is_on_classic);
g_test_add_func("/classic/on-classic-with-long-line",
Expand Down
2 changes: 1 addition & 1 deletion cmd/libsnap-confine-private/classic.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@
// Location of the host filesystem directory in the core snap.
#define SC_HOSTFS_DIR "/var/lib/snapd/hostfs"

bool is_running_on_classic_distribution();
bool is_running_on_classic_distribution(void);

#endif
17 changes: 10 additions & 7 deletions cmd/libsnap-confine-private/cleanup-funcs-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,25 @@

#include <glib.h>

static int called = 0;

static void cleanup_fn(int *ptr)
{
called = 1;
}

// Test that cleanup functions are applied as expected
static void test_cleanup_sanity()
static void test_cleanup_sanity(void)
{
int called = 0;
void fn(int *ptr) {
called = 1;
}
{
int test SC_CLEANUP(fn);
int test SC_CLEANUP(cleanup_fn);
test = 0;
test++;
}
g_assert_cmpint(called, ==, 1);
}

static void __attribute__ ((constructor)) init()
static void __attribute__ ((constructor)) init(void)
{
g_test_add_func("/cleanup/sanity", test_cleanup_sanity);
}
30 changes: 15 additions & 15 deletions cmd/libsnap-confine-private/error-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include <errno.h>
#include <glib.h>

static void test_sc_error_init()
static void test_sc_error_init(void)
{
struct sc_error *err;
// Create an error
Expand All @@ -35,7 +35,7 @@ static void test_sc_error_init()
g_assert_cmpstr(sc_error_msg(err), ==, "printer is on fire");
}

static void test_sc_error_init_from_errno()
static void test_sc_error_init_from_errno(void)
{
struct sc_error *err;
// Create an error
Expand All @@ -49,7 +49,7 @@ static void test_sc_error_init_from_errno()
g_assert_cmpstr(sc_error_msg(err), ==, "printer is on fire");
}

static void test_sc_error_cleanup()
static void test_sc_error_cleanup(void)
{
// Check that sc_error_cleanup() is safe to use.

Expand All @@ -64,7 +64,7 @@ static void test_sc_error_cleanup()
g_assert_null(err);
}

static void test_sc_error_domain__NULL()
static void test_sc_error_domain__NULL(void)
{
// Check that sc_error_domain() dies if called with NULL error.
if (g_test_subprocess()) {
Expand All @@ -82,7 +82,7 @@ static void test_sc_error_domain__NULL()
("cannot obtain error domain from NULL error\n");
}

static void test_sc_error_code__NULL()
static void test_sc_error_code__NULL(void)
{
// Check that sc_error_code() dies if called with NULL error.
if (g_test_subprocess()) {
Expand All @@ -99,7 +99,7 @@ static void test_sc_error_code__NULL()
g_test_trap_assert_stderr("cannot obtain error code from NULL error\n");
}

static void test_sc_error_msg__NULL()
static void test_sc_error_msg__NULL(void)
{
// Check that sc_error_msg() dies if called with NULL error.
if (g_test_subprocess()) {
Expand All @@ -117,7 +117,7 @@ static void test_sc_error_msg__NULL()
("cannot obtain error message from NULL error\n");
}

static void test_sc_die_on_error__NULL()
static void test_sc_die_on_error__NULL(void)
{
// Check that sc_die_on_error() does nothing if called with NULL error.
if (g_test_subprocess()) {
Expand All @@ -128,7 +128,7 @@ static void test_sc_die_on_error__NULL()
g_test_trap_assert_passed();
}

static void test_sc_die_on_error__regular()
static void test_sc_die_on_error__regular(void)
{
// Check that sc_die_on_error() dies if called with an error.
if (g_test_subprocess()) {
Expand All @@ -142,7 +142,7 @@ static void test_sc_die_on_error__regular()
g_test_trap_assert_stderr("just testing\n");
}

static void test_sc_die_on_error__errno()
static void test_sc_die_on_error__errno(void)
{
// Check that sc_die_on_error() dies if called with an errno-based error.
if (g_test_subprocess()) {
Expand All @@ -156,7 +156,7 @@ static void test_sc_die_on_error__errno()
g_test_trap_assert_stderr("just testing: No such file or directory\n");
}

static void test_sc_error_forward__nothing()
static void test_sc_error_forward__nothing(void)
{
// Check that forwarding NULL does exactly that.
struct sc_error *recipient = (void *)0xDEADBEEF;
Expand All @@ -165,7 +165,7 @@ static void test_sc_error_forward__nothing()
g_assert_null(recipient);
}

static void test_sc_error_forward__something_somewhere()
static void test_sc_error_forward__something_somewhere(void)
{
// Check that forwarding a real error works OK.
struct sc_error *recipient = NULL;
Expand All @@ -176,7 +176,7 @@ static void test_sc_error_forward__something_somewhere()
g_assert_nonnull(recipient);
}

static void test_sc_error_forward__something_nowhere()
static void test_sc_error_forward__something_nowhere(void)
{
// Check that forwarding a real error nowhere calls die()
if (g_test_subprocess()) {
Expand All @@ -196,7 +196,7 @@ static void test_sc_error_forward__something_nowhere()
g_test_trap_assert_stderr("just testing\n");
}

static void test_sc_error_match__typical()
static void test_sc_error_match__typical(void)
{
// NULL error doesn't match anything.
g_assert_false(sc_error_match(NULL, "domain", 42));
Expand All @@ -210,7 +210,7 @@ static void test_sc_error_match__typical()
g_assert_false(sc_error_match(err, "other-domain", 1));
}

static void test_sc_error_match__NULL_domain()
static void test_sc_error_match__NULL_domain(void)
{
// Using a NULL domain is a fatal bug.
if (g_test_subprocess()) {
Expand All @@ -227,7 +227,7 @@ static void test_sc_error_match__NULL_domain()
g_test_trap_assert_stderr("cannot match error to a NULL domain\n");
}

static void __attribute__ ((constructor)) init()
static void __attribute__ ((constructor)) init(void)
{
g_test_add_func("/error/sc_error_init", test_sc_error_init);
g_test_add_func("/error/sc_error_init_from_errno",
Expand Down
4 changes: 2 additions & 2 deletions cmd/libsnap-confine-private/fault-injection-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ static bool broken_alter_msg(struct sc_fault_state *state, void *ptr)
return true;
}

static void test_fault_injection()
static void test_fault_injection(void)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dislike that this needs to be done :/ That is one of my complains about C (one of many ;) - the fact that K&R C from ~1879 requires us to nowdays write clunky (void) like this. Oh well.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess you meant 1978, although it does feel like 19th century by modern standards :)

{
g_assert_false(sc_faulty("foo", NULL));

Expand All @@ -57,7 +57,7 @@ static void test_fault_injection()
sc_reset_faults();
}

static void __attribute__ ((constructor)) init()
static void __attribute__ ((constructor)) init(void)
{
g_test_add_func("/fault-injection", test_fault_injection);
}
2 changes: 1 addition & 1 deletion cmd/libsnap-confine-private/fault-injection.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ void sc_break(const char *name, sc_fault_fn fn)
sc_faults = fault;
}

void sc_reset_faults()
void sc_reset_faults(void)
{
struct sc_fault *next_fault;
for (struct sc_fault * fault = sc_faults; fault != NULL;
Expand Down
2 changes: 1 addition & 1 deletion cmd/libsnap-confine-private/fault-injection.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ void sc_break(const char *name, sc_fault_fn fn);
/**
* Remove all the injected faults.
**/
void sc_reset_faults();
void sc_reset_faults(void);

#endif // ifndef _ENABLE_FAULT_INJECTION

Expand Down
8 changes: 4 additions & 4 deletions cmd/libsnap-confine-private/locking-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ static void sc_set_lock_dir(const char *dir)
//
// The directory is automatically reset to the real value at the end of the
// test.
static const char *sc_test_use_fake_lock_dir()
static const char *sc_test_use_fake_lock_dir(void)
{
char *lock_dir = NULL;
if (g_test_subprocess()) {
Expand All @@ -60,7 +60,7 @@ static const char *sc_test_use_fake_lock_dir()
}

// Check that locking a namespace actually flock's the mutex with LOCK_EX
static void test_sc_lock_unlock()
static void test_sc_lock_unlock(void)
{
const char *lock_dir = sc_test_use_fake_lock_dir();
int fd = sc_lock("foo");
Expand All @@ -86,7 +86,7 @@ static void test_sc_lock_unlock()
g_assert_cmpint(err, ==, 0);
}

static void test_sc_enable_sanity_timeout()
static void test_sc_enable_sanity_timeout(void)
{
if (g_test_subprocess()) {
sc_enable_sanity_timeout();
Expand All @@ -101,7 +101,7 @@ static void test_sc_enable_sanity_timeout()
g_test_trap_assert_failed();
}

static void __attribute__ ((constructor)) init()
static void __attribute__ ((constructor)) init(void)
{
g_test_add_func("/locking/sc_lock_unlock", test_sc_lock_unlock);
g_test_add_func("/locking/sc_enable_sanity_timeout",
Expand Down
6 changes: 3 additions & 3 deletions cmd/libsnap-confine-private/locking.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ static void sc_SIGALRM_handler(int signum)
sanity_timeout_expired = 1;
}

void sc_enable_sanity_timeout()
void sc_enable_sanity_timeout(void)
{
sanity_timeout_expired = 0;
struct sigaction act = {.sa_handler = sc_SIGALRM_handler };
Expand All @@ -64,7 +64,7 @@ void sc_enable_sanity_timeout()
debug("sanity timeout initialized and set for three seconds");
}

void sc_disable_sanity_timeout()
void sc_disable_sanity_timeout(void)
{
if (sanity_timeout_expired) {
die("sanity timeout expired");
Expand Down Expand Up @@ -134,7 +134,7 @@ void sc_unlock(const char *scope, int lock_fd)
close(lock_fd);
}

int sc_lock_global()
int sc_lock_global(void)
{
return sc_lock(NULL);
}
Expand Down