PengZheng commented on code in PR #476: URL: https://github.com/apache/celix/pull/476#discussion_r1133532110
########## libs/framework/src/celix_launcher.c: ########## @@ -34,135 +34,130 @@ #include <curl/curl.h> #include <signal.h> +#include "framework.h" #include "celix_framework_factory.h" #include "celix_constants.h" #include "celix_framework_utils.h" -static void show_usage(char* prog_name); -static void show_properties(celix_properties_t *embeddedProps, const char *configFile); -static void printEmbeddedBundles(); -static void shutdown_framework(int signal); -static void ignore(int signal); - +static void celixLauncher_shutdownFramework(int signal); +static void celixLauncher_ignore(int signal); static int celixLauncher_launchWithConfigAndProps(const char *configFile, celix_framework_t* *framework, celix_properties_t* packedConfig); +static void celixLauncher_combineProperties(celix_properties_t *original, const celix_properties_t *append); +static celix_properties_t* celixLauncher_createConfig(const char* configFile, celix_properties_t* embeddedProperties); -static void combine_properties(celix_properties_t *original, const celix_properties_t *append); +static void celixLauncher_printUsage(char* progName); +static void celixLauncher_printProperties(celix_properties_t *embeddedProps, const char* configFile); +static void celixLauncher_printEmbeddedBundles(); +static int celixLauncher_createBundleCache(celix_properties_t* embeddedProperties, const char* configFile); #define DEFAULT_CONFIG_FILE "config.properties" static framework_t *g_fw = NULL; + + int celixLauncher_launchAndWaitForShutdown(int argc, char *argv[], celix_properties_t* packedConfig) { - celix_framework_t* framework = NULL; + celix_framework_t* framework = NULL; + int rc = celixLauncher_launchWithArgv(argc, argv, packedConfig, &framework); + if (rc == 0 && framework != NULL) { + celixLauncher_waitForShutdown(framework); + celixLauncher_destroy(framework); + } + return rc; +} +int celixLauncher_launchWithArgv(int argc, char *argv[], celix_properties_t* embeddedConfig, celix_framework_t** frameworkOut) { + celix_framework_t* framework = NULL; // Perform some minimal command-line option parsing... char *opt = NULL; - if (argc > 1) { - opt = argv[1]; - } - - char *config_file = NULL; + char* configFile = NULL; bool showProps = false; bool showEmbeddedBundles = false; + bool createCache = false; for (int i = 1; i < argc; ++i) { opt = argv[i]; // Check whether the user wants some help... - if (strncmp("-?", opt, strlen("-?")) == 0 || strncmp("-h", opt, strlen("-h")) == 0 || strncmp("--help", opt, strlen("--help")) == 0) { - show_usage(argv[0]); - celix_properties_destroy(packedConfig); - return 0; - } else if (strncmp("-p", opt, strlen("-p")) == 0 || strncmp("--props", opt, strlen("--props")) == 0) { + if (strncmp("-?", opt, sizeof("-?")) == 0 || strncmp("-h", opt, sizeof("-h")) == 0 || strncmp("--help", opt, sizeof("--help")) == 0) { + celixLauncher_printUsage(argv[0]); + celix_properties_destroy(embeddedConfig); + return 0; + } else if (strncmp("-p", opt, sizeof("-p")) == 0 || strncmp("--props", opt, sizeof("--props")) == 0) { showProps = true; - } else if (strncmp("--embedded_bundles", opt, strlen("--embedded_bundles")) == 0) { + } else if (strncmp("-c", opt, sizeof("-c")) == 0 || strncmp("--create-bundle-cache", opt, sizeof("--create-bundle-cache")) == 0) { + createCache = true; + } else if (strncmp("--embedded_bundles", opt, sizeof("--embedded_bundles")) == 0) { showEmbeddedBundles = true; } else { - config_file = opt; + configFile = opt; } } - if (config_file == NULL) { - config_file = DEFAULT_CONFIG_FILE; + if (configFile == NULL) { + configFile = DEFAULT_CONFIG_FILE; } - if (packedConfig == NULL) { - packedConfig = celix_properties_create(); + if (embeddedConfig == NULL) { + embeddedConfig = celix_properties_create(); } if (showProps) { - show_properties(packedConfig, config_file); - celix_properties_destroy(packedConfig); + celixLauncher_printProperties(embeddedConfig, configFile); + celix_properties_destroy(embeddedConfig); return 0; } + if (createCache) { + return celixLauncher_createBundleCache(embeddedConfig, configFile); + } + if (showEmbeddedBundles) { - printEmbeddedBundles(); - celix_properties_destroy(packedConfig); + celixLauncher_printEmbeddedBundles(); + celix_properties_destroy(embeddedConfig); return 0; } struct sigaction sigact; memset(&sigact, 0, sizeof(sigact)); - sigact.sa_handler = shutdown_framework; + sigact.sa_handler = celixLauncher_shutdownFramework; sigaction(SIGINT, &sigact, NULL); sigaction(SIGTERM, &sigact, NULL); memset(&sigact, 0, sizeof(sigact)); - sigact.sa_handler = ignore; + sigact.sa_handler = celixLauncher_ignore; sigaction(SIGUSR1, &sigact, NULL); sigaction(SIGUSR2, &sigact, NULL); - int rc = celixLauncher_launchWithConfigAndProps(config_file, &framework, packedConfig); + int rc = celixLauncher_launchWithConfigAndProps(configFile, &framework, embeddedConfig); if (rc == 0) { g_fw = framework; - celixLauncher_waitForShutdown(framework); - celixLauncher_destroy(framework); + *frameworkOut = framework; } return rc; } -static void show_usage(char* prog_name) { - printf("Usage:\n %s [-h|-p] [path/to/runtime/config.properties]\n", basename(prog_name)); - printf("Options:\n"); - printf("\t-h | --help: Show this message\n"); - printf("\t-p | --props: Show the embedded and runtime properties for this Celix container\n"); - printf("\t--embedded_bundles: Show the embedded bundles for this Celix container\n"); - printf("\n"); -} - -static void shutdown_framework(int signal) { +static void celixLauncher_shutdownFramework(int signal) { if (g_fw != NULL) { celixLauncher_stop(g_fw); //NOTE main thread will destroy } } -static void ignore(int signal) { +static void celixLauncher_ignore(int signal) { //ignoring for signal SIGUSR1, SIGUSR2. Can be used on threads } int celixLauncher_launch(const char *configFile, celix_framework_t* *framework) { return celixLauncher_launchWithConfigAndProps(configFile, framework, NULL); } -static int celixLauncher_launchWithConfigAndProps(const char *configFile, celix_framework_t* *framework, celix_properties_t* packedConfig) { - if (packedConfig == NULL) { - packedConfig = celix_properties_create(); - } - - FILE *config = fopen(configFile, "r"); - if (config != NULL) { - celix_properties_t *configProps = celix_properties_loadWithStream(config); - fclose(config); - combine_properties(packedConfig, configProps); - celix_properties_destroy(configProps); - } - - return celixLauncher_launchWithProperties(packedConfig, framework); +static int celixLauncher_launchWithConfigAndProps(const char *configFile, celix_framework_t** framework, celix_properties_t* packedConfig) { + celix_properties_t* config = celixLauncher_createConfig(configFile, packedConfig); + return celixLauncher_launchWithProperties(config, framework); Review Comment: `CELIX_ILLEGAL_STATE` is bad for exit status: > Although any value in the range 0 to 255 can be passed to the parent via the status argument to _exit(), specifying values greater than 128 can cause confu- sion in shell scripts. The reason is that, when a command is terminated by a signal, the shell indicates this fact by setting the value of the variable $? to 128 plus the signal number, and this value is indistinguishable from that yielded when a process calls _exit() with the same status value. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: dev-unsubscr...@celix.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org