From c00f1a39706a208b73f9a4ae46c2db11fac0450c Mon Sep 17 00:00:00 2001 From: Mark Farrugia Date: Wed, 31 Oct 2018 13:39:39 +1100 Subject: Add macro to check if configfs is mounted We cannot configure AVIRT without configfs being mounted on the filesystem Signed-off-by: Mark Farrugia --- src/avirt-config.c | 47 +++++++++++++++++++++++++---------------------- 1 file changed, 25 insertions(+), 22 deletions(-) (limited to 'src') diff --git a/src/avirt-config.c b/src/avirt-config.c index e1b60b9..cbdd857 100644 --- a/src/avirt-config.c +++ b/src/avirt-config.c @@ -44,15 +44,30 @@ fprintf(stderr, "[%s]: AVIRT DEBUG: " fmt "\n", __func__, ##args); #endif -#define WRITE_TO_PATH(path, fmt, args...) \ - do { \ - FILE *fd = fopen(path, "w"); \ - if (!fd) { \ +/** + * Checks whether the configfs filesystem is mounted + */ +#define IS_CONFIGFS_MOUNTED() \ + do { \ + int err; \ + if (!configfs_mounted) { \ + err = mount_configfs(); \ + if (err < 0) return err; \ + } \ + } while (0) + +/** + * Write the given formatted string to a file given by path + */ +#define WRITE_TO_PATH(path, fmt, args...) \ + do { \ + FILE *fd = fopen(path, "w"); \ + if (!fd) { \ AVIRT_ERROR_V("Failed to open file at '%s'", path); \ - return -EPERM; \ - } \ - fprintf(fd, fmt, ##args); \ - fclose(fd); \ + return -EPERM; \ + } \ + fprintf(fd, fmt, ##args); \ + fclose(fd); \ } while (0) static bool configfs_mounted = false; @@ -108,13 +123,7 @@ int snd_avirt_stream_new(const char *name, unsigned int channels, int direction, char path_attr[AVIRT_CONFIGFS_PATH_MAXLEN]; FILE *fd; - // Check whether the configfs filesystem is mounted - if (!configfs_mounted) - { - err = mount_configfs(); - if (err < 0) - return err; - } + IS_CONFIGFS_MOUNTED(); // Check if card is already sealed if (card_sealed) @@ -170,7 +179,6 @@ int snd_avirt_stream_new(const char *name, unsigned int channels, int direction, int snd_avirt_card_seal() { - int err; char path_sealed[AVIRT_CONFIGFS_PATH_MAXLEN]; FILE *fd; @@ -181,12 +189,7 @@ int snd_avirt_card_seal() return -EPERM; } - if (!configfs_mounted) - { - err = mount_configfs(); - if (err < 0) - return err; - } + IS_CONFIGFS_MOUNTED(); strcpy(path_sealed, AVIRT_CONFIGFS_PATH_STREAMS); strcat(path_sealed, "/sealed"); -- cgit 1.2.3-korg