From f791fe4eb686c14b66985b7cc3aa351c2cc90752 Mon Sep 17 00:00:00 2001 From: Johan Hedberg Date: Thu, 4 Jun 2009 16:32:46 +0300 Subject: [PATCH] obexd: Add support for OBEX root folder setup script This patch adds support for running an arbitrary script if the specified root folder doesn't exist. This can be useful if some extra initialization is desired for the root folder. --- obexd/src/main.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/obexd/src/main.c b/obexd/src/main.c index 84f318e7e..f16be8be3 100644 --- a/obexd/src/main.c +++ b/obexd/src/main.c @@ -33,6 +33,7 @@ #include #include #include +#include #include #include #include @@ -158,6 +159,7 @@ static gboolean option_detach = TRUE; static gboolean option_debug = FALSE; static gchar *option_root = NULL; +static gchar *option_root_setup = NULL; static gchar *option_capability = NULL; static gchar *option_devnode = NULL; @@ -176,6 +178,8 @@ static GOptionEntry options[] = { "Enable debug information output" }, { "root", 'r', 0, G_OPTION_ARG_STRING, &option_root, "Specify root folder location", "PATH" }, + { "root-setup", 'S', 0, G_OPTION_ARG_STRING, &option_root_setup, + "Root folder setup script", "SCRIPT" }, { "symlinks", 'l', 0, G_OPTION_ARG_NONE, &option_symlinks, "Enable symlinks on root folder" }, { "capability", 'c', 0, G_OPTION_ARG_STRING, &option_capability, @@ -256,6 +260,45 @@ static int devnode_setup(void) option_devnode); } +static gboolean is_dir(const char *dir) { + struct stat st; + + if (stat(dir, &st) < 0) { + error("stat(%s): %s (%d)", dir, strerror(errno), errno); + return FALSE; + } + + return S_ISDIR(st.st_mode); +} + +static gboolean root_folder_setup(char *root, char *root_setup) +{ + gint status; + char *argv[3] = { root_setup, root, NULL }; + + if (is_dir(root)) + return TRUE; + + if (root_setup == NULL) + return FALSE; + + debug("Setting up %s using %s", root, root_setup); + + if (!g_spawn_sync(NULL, argv, NULL, 0, NULL, NULL, NULL, NULL, + &status, NULL)) { + error("Unable to execute %s", root_setup); + return FALSE; + } + + if (WEXITSTATUS(status) != EXIT_SUCCESS) { + error("%s exited with status %d", root_setup, + WEXITSTATUS(status)); + return FALSE; + } + + return is_dir(root); +} + int main(int argc, char *argv[]) { GOptionContext *context; @@ -357,6 +400,11 @@ int main(int argc, char *argv[]) if (option_devnode) devnode_setup(); + if (!root_folder_setup(option_root, option_root_setup)) { + error("Unable to setup root folder %s", option_root); + exit(EXIT_FAILURE); + } + memset(&sa, 0, sizeof(sa)); sa.sa_handler = sig_term; sigaction(SIGINT, &sa, NULL); -- 2.47.3