int bb_shutdown(void) { static int called = 0; mutex_lock(status_mutex); if (called) { mutex_unlock(status_mutex); return -1; } debug("bb", 0, "Shutting down " GW_NAME "..."); called = 1; set_shutdown_status(); mutex_unlock(status_mutex); #ifndef NO_SMS debug("bb", 0, "shutting down smsc"); smsc2_shutdown(); #endif #ifndef NO_WAP debug("bb", 0, "shutting down udp"); udp_shutdown(); #endif store_shutdown(); return 0; }
int main(int argc, char **argv) { int cf_index; Cfg *cfg; bb_status = BB_RUNNING; gwlib_init(); start_time = time(NULL); suspended = gwlist_create(); isolated = gwlist_create(); gwlist_add_producer(suspended); gwlist_add_producer(isolated); cf_index = get_and_set_debugs(argc, argv, check_args); if (argv[cf_index] == NULL) cfg_filename = octstr_create("kannel.conf"); else cfg_filename = octstr_create(argv[cf_index]); cfg = cfg_create(cfg_filename); if (cfg_read(cfg) == -1) panic(0, "Couldn't read configuration from `%s'.", octstr_get_cstr(cfg_filename)); dlr_init(cfg); report_versions("bearerbox"); flow_threads = gwlist_create(); if (init_bearerbox(cfg) == NULL) panic(0, "Initialization failed."); info(0, "----------------------------------------"); info(0, GW_NAME " bearerbox II version %s starting", GW_VERSION); gwthread_sleep(5.0); /* give time to threads to register themselves */ if (store_load(dispatch_into_queue) == -1) panic(0, "Cannot start with store-file failing"); info(0, "MAIN: Start-up done, entering mainloop"); if (bb_status == BB_SUSPENDED) { info(0, "Gateway is now SUSPENDED by startup arguments"); } else if (bb_status == BB_ISOLATED) { info(0, "Gateway is now ISOLATED by startup arguments"); gwlist_remove_producer(suspended); } else { smsc2_resume(1); gwlist_remove_producer(suspended); gwlist_remove_producer(isolated); } while (bb_status != BB_SHUTDOWN && bb_status != BB_DEAD && gwlist_producer_count(flow_threads) > 0) { /* debug("bb", 0, "Main Thread: going to sleep."); */ /* * Not infinite sleep here, because we should notice * when all "flow threads" are dead and shutting bearerbox * down. * XXX if all "flow threads" call gwthread_wakeup(MAIN_THREAD_ID), * we can enter infinite sleep then. */ gwthread_sleep(10.0); /* debug("bb", 0, "Main Thread: woken up."); */ if (bb_todo == 0) { continue; } if (bb_todo & BB_LOGREOPEN) { warning(0, "SIGHUP received, catching and re-opening logs"); log_reopen(); alog_reopen(); bb_todo = bb_todo & ~BB_LOGREOPEN; } if (bb_todo & BB_CHECKLEAKS) { warning(0, "SIGQUIT received, reporting memory usage."); gw_check_leaks(); bb_todo = bb_todo & ~BB_CHECKLEAKS; } } if (bb_status == BB_SHUTDOWN || bb_status == BB_DEAD) warning(0, "Killing signal or HTTP admin command received, shutting down..."); /* call shutdown */ bb_shutdown(); /* wait until flow threads exit */ while (gwlist_consume(flow_threads) != NULL) ; info(0, "All flow threads have died, killing core"); bb_status = BB_DEAD; httpadmin_stop(); boxc_cleanup(); smsc2_cleanup(); store_shutdown(); empty_msg_lists(); gwlist_destroy(flow_threads, NULL); gwlist_destroy(suspended, NULL); gwlist_destroy(isolated, NULL); mutex_destroy(status_mutex); alog_close(); /* if we have any */ bb_alog_shutdown(); cfg_destroy(cfg); octstr_destroy(cfg_filename); dlr_shutdown(); /* now really restart */ if (restart) restart_box(argv); gwlib_shutdown(); return 0; }
int main(int argc, char **argv) { char id[UUID_STR_LEN + 1]; int cf_index, ret, type; Octstr *os, *store_type, *store_location, *status; Msg *msg; CfgGroup *grp; conf_file = NULL; gwlib_init(); //This can be overwritten with the -v flag at runtime log_set_output_level(DEFAULT_LOG_LEVEL); cf_index = get_and_set_debugs(argc, argv, check_args); if (argv[cf_index] == NULL) { print_usage(argv[0]); goto error; } if (conf_file == NULL) conf_file = octstr_create("kannel.conf"); cfg = cfg_create(conf_file); if (cfg_read(cfg) == -1) panic(0, "Couldn't read configuration from `%s'.", octstr_get_cstr(conf_file)); info(0, "1"); grp = cfg_get_single_group(cfg, octstr_imm("core")); if (grp == NULL) { printf("FATAL: Could not load Kannel's core group. Exiting.\n"); return 2; } store_location = cfg_get(grp, octstr_imm("store-location")); store_type = cfg_get(grp, octstr_imm("store-type")); store_init(store_type, store_location, -1, msg_pack, msg_unpack_wrapper); switch (command) { case COMMAND_LIST: printf("Listing records %d -> %d\n", list_from+1, list_limit); print_header(); store_load(print_msg); if (counter == 0) { printf("|%60s%14s%60s|\n", "", "Store is Empty", ""); } print_sep(); break; case COMMAND_DELETE: store_load(msg_count); msg = msg_create(ack); msg->ack.nack = ack_failed; msg->ack.time = time(NULL); uuid_parse(octstr_get_cstr(param_1), msg->ack.id); ret = store_save(msg); if (ret == 0) { printf("Deleted message %s\n", octstr_get_cstr(param_1)); counter--; } else { printf("Could not delete message %s\n", octstr_get_cstr(param_1)); } msg_destroy(msg); break; case COMMAND_EXPORT: counter = 0; type = 0; list = gwlist_create(); store_load(msg_push); printf("Exporting %ld messages...\n", gwlist_len(list)); if ((octstr_compare(param_1, octstr_imm("file")) == 0) || (octstr_compare(param_1, octstr_imm("spool")) == 0)) { store_shutdown(); store_init(param_1, param_2, -1, msg_pack, msg_unpack_wrapper); store_load(msg_count); while ((os = gwlist_extract_first(list)) != NULL) { msg = msg_unpack_wrapper(os); if (msg != NULL) { ret = store_save(msg); if (ret == 0) { counter++; } else { printf("Error saving message\n"); } } else { printf("Error extracting message\n"); } msg_destroy(msg); } status = NULL; } else if (octstr_compare(param_1, octstr_imm("text")) == 0) { status = store_status(BBSTATUS_TEXT); } else if (octstr_compare(param_1, octstr_imm("html")) == 0) { status = store_status(BBSTATUS_HTML); } else if (octstr_compare(param_1, octstr_imm("xml")) == 0) { status = store_status(BBSTATUS_XML); } else { status = NULL; } if (status != NULL) { file = fopen(octstr_get_cstr(param_2), "w"); if (file == NULL) { error(errno, "Failed to open '%s' for writing, cannot create output file", octstr_get_cstr(param_2)); return -1; } octstr_print(file, status); fflush(file); if (file != NULL) fclose(file); //printf("%s", octstr_get_cstr(status)); } gwlist_destroy(list, octstr_destroy_item); break; default: break; } octstr_destroy(store_type); octstr_destroy(store_location); cfg_destroy(cfg); store_shutdown(); error: gwlib_shutdown(); return 1; }