#include "asterisk.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <signal.h>
#include <errno.h>
#include <unistd.h>
#include <dirent.h>
#include "asterisk/channel.h"
#include "asterisk/file.h"
#include "asterisk/app.h"
#include "asterisk/dsp.h"
#include "asterisk/logger.h"
#include "asterisk/options.h"
#include "asterisk/astdb.h"
#include "asterisk/cli.h"
#include "asterisk/utils.h"
#include "asterisk/lock.h"
#include "asterisk/manager.h"
#include "db1-ast/include/db.h"
Go to the source code of this file.
Functions | |
int | ast_db_del (const char *family, const char *keys) |
int | ast_db_deltree (const char *family, const char *keytree) |
void | ast_db_freetree (struct ast_db_entry *dbe) |
int | ast_db_get (const char *family, const char *keys, char *value, int valuelen) |
ast_db_entry * | ast_db_gettree (const char *family, const char *keytree) |
int | ast_db_put (const char *family, const char *keys, char *value) |
int | astdb_init (void) |
static int | database_del (int fd, int argc, char *argv[]) |
static int | database_deltree (int fd, int argc, char *argv[]) |
static int | database_get (int fd, int argc, char *argv[]) |
static int | database_put (int fd, int argc, char *argv[]) |
static int | database_show (int fd, int argc, char *argv[]) |
static int | database_showkey (int fd, int argc, char *argv[]) |
static int | dbinit (void) |
static int | keymatch (const char *key, const char *prefix) |
static int | manager_dbget (struct mansession *s, const struct message *m) |
static int | manager_dbput (struct mansession *s, const struct message *m) |
static int | subkeymatch (const char *key, const char *suffix) |
Variables | |
static DB * | astdb |
ast_cli_entry | cli_database [] |
static char | database_del_usage [] |
static char | database_deltree_usage [] |
static char | database_get_usage [] |
static char | database_put_usage [] |
static char | database_show_usage [] |
static char | database_showkey_usage [] |
static ast_mutex_t | dblock = { PTHREAD_MUTEX_INITIALIZER , 1, { NULL }, { 0 }, 0, { NULL }, { 0 }, PTHREAD_MUTEX_INITIALIZER } |
Definition in file db.c.
int ast_db_del | ( | const char * | family, | |
const char * | keys | |||
) |
Definition at line 217 of file db.c.
References ast_log(), ast_mutex_lock, ast_mutex_unlock, dbinit(), dblock, LOG_DEBUG, and option_debug.
Referenced by __expire_registry(), ast_privacy_set(), auth_exec(), cache_lookup_internal(), database_del(), del_exec(), destroy_association(), dump_agents(), dump_queue_members(), function_db_delete(), handle_dbdel(), process_clearcache(), reload_agents(), reload_queue_members(), and update_registry().
00218 { 00219 char fullkey[256]; 00220 DBT key; 00221 int res, fullkeylen; 00222 00223 ast_mutex_lock(&dblock); 00224 if (dbinit()) { 00225 ast_mutex_unlock(&dblock); 00226 return -1; 00227 } 00228 00229 fullkeylen = snprintf(fullkey, sizeof(fullkey), "/%s/%s", family, keys); 00230 memset(&key, 0, sizeof(key)); 00231 key.data = fullkey; 00232 key.size = fullkeylen + 1; 00233 00234 res = astdb->del(astdb, &key, 0); 00235 astdb->sync(astdb, 0); 00236 00237 ast_mutex_unlock(&dblock); 00238 00239 if (res && option_debug) 00240 ast_log(LOG_DEBUG, "Unable to find key '%s' in family '%s'\n", keys, family); 00241 return res; 00242 }
int ast_db_deltree | ( | const char * | family, | |
const char * | keytree | |||
) |
Definition at line 100 of file db.c.
References ast_mutex_lock, ast_mutex_unlock, dbinit(), dblock, keymatch(), keys, and prefix.
Referenced by ast_privacy_reset(), database_deltree(), deltree_exec(), dundi_flush(), handle_dbdeltree(), and iax_provision_reload().
00101 { 00102 char prefix[256]; 00103 DBT key, data; 00104 char *keys; 00105 int res; 00106 int pass; 00107 00108 if (family) { 00109 if (keytree) { 00110 snprintf(prefix, sizeof(prefix), "/%s/%s", family, keytree); 00111 } else { 00112 snprintf(prefix, sizeof(prefix), "/%s", family); 00113 } 00114 } else if (keytree) { 00115 return -1; 00116 } else { 00117 prefix[0] = '\0'; 00118 } 00119 00120 ast_mutex_lock(&dblock); 00121 if (dbinit()) { 00122 ast_mutex_unlock(&dblock); 00123 return -1; 00124 } 00125 00126 memset(&key, 0, sizeof(key)); 00127 memset(&data, 0, sizeof(data)); 00128 pass = 0; 00129 while (!(res = astdb->seq(astdb, &key, &data, pass++ ? R_NEXT : R_FIRST))) { 00130 if (key.size) { 00131 keys = key.data; 00132 keys[key.size - 1] = '\0'; 00133 } else { 00134 keys = "<bad key>"; 00135 } 00136 if (keymatch(keys, prefix)) { 00137 astdb->del(astdb, &key, 0); 00138 } 00139 } 00140 astdb->sync(astdb, 0); 00141 ast_mutex_unlock(&dblock); 00142 return 0; 00143 }
void ast_db_freetree | ( | struct ast_db_entry * | dbe | ) |
Definition at line 461 of file db.c.
References free, last, and ast_db_entry::next.
Referenced by process_clearcache(), reload_agents(), and reload_queue_members().
00462 { 00463 struct ast_db_entry *last; 00464 while (dbe) { 00465 last = dbe; 00466 dbe = dbe->next; 00467 free(last); 00468 } 00469 }
int ast_db_get | ( | const char * | family, | |
const char * | keys, | |||
char * | value, | |||
int | valuelen | |||
) |
Definition at line 172 of file db.c.
References ast_copy_string(), ast_log(), ast_mutex_lock, ast_mutex_unlock, dbinit(), dblock, LOG_DEBUG, LOG_NOTICE, and option_debug.
Referenced by ast_privacy_check(), auth_exec(), blacklist_read(), cache_lookup_internal(), check_access(), create_addr(), database_get(), database_increment(), function_db_delete(), function_db_exists(), function_db_read(), handle_dbget(), iax_provision_version(), load_password(), lookupblacklist_exec(), lookupcidname_exec(), manager_dbget(), populate_addr(), reg_source_db(), reload_agents(), and reload_queue_members().
00173 { 00174 char fullkey[256] = ""; 00175 DBT key, data; 00176 int res, fullkeylen; 00177 00178 ast_mutex_lock(&dblock); 00179 if (dbinit()) { 00180 ast_mutex_unlock(&dblock); 00181 return -1; 00182 } 00183 00184 fullkeylen = snprintf(fullkey, sizeof(fullkey), "/%s/%s", family, keys); 00185 memset(&key, 0, sizeof(key)); 00186 memset(&data, 0, sizeof(data)); 00187 memset(value, 0, valuelen); 00188 key.data = fullkey; 00189 key.size = fullkeylen + 1; 00190 00191 res = astdb->get(astdb, &key, &data, 0); 00192 00193 /* Be sure to NULL terminate our data either way */ 00194 if (res) { 00195 if (option_debug) 00196 ast_log(LOG_DEBUG, "Unable to find key '%s' in family '%s'\n", keys, family); 00197 } else { 00198 #if 0 00199 printf("Got value of size %d\n", data.size); 00200 #endif 00201 if (data.size) { 00202 ((char *)data.data)[data.size - 1] = '\0'; 00203 /* Make sure that we don't write too much to the dst pointer or we don't read too much from the source pointer */ 00204 ast_copy_string(value, data.data, (valuelen > data.size) ? data.size : valuelen); 00205 } else { 00206 ast_log(LOG_NOTICE, "Strange, empty value for /%s/%s\n", family, keys); 00207 } 00208 } 00209 00210 /* Data is not fully isolated for concurrency, so the lock must be extended 00211 * to after the copy to the output buffer. */ 00212 ast_mutex_unlock(&dblock); 00213 00214 return res; 00215 }
struct ast_db_entry* ast_db_gettree | ( | const char * | family, | |
const char * | keytree | |||
) |
Definition at line 399 of file db.c.
References ast_log(), ast_malloc, ast_mutex_lock, ast_mutex_unlock, ast_strlen_zero(), ast_db_entry::data, dbinit(), dblock, ast_db_entry::key, keymatch(), keys, last, LOG_WARNING, sla_ringing_trunk::next, and prefix.
Referenced by process_clearcache(), reload_agents(), and reload_queue_members().
00400 { 00401 char prefix[256]; 00402 DBT key, data; 00403 char *keys, *values; 00404 int values_len; 00405 int res; 00406 int pass; 00407 struct ast_db_entry *last = NULL; 00408 struct ast_db_entry *cur, *ret=NULL; 00409 00410 if (!ast_strlen_zero(family)) { 00411 if (!ast_strlen_zero(keytree)) { 00412 /* Family and key tree */ 00413 snprintf(prefix, sizeof(prefix), "/%s/%s", family, keytree); 00414 } else { 00415 /* Family only */ 00416 snprintf(prefix, sizeof(prefix), "/%s", family); 00417 } 00418 } else { 00419 prefix[0] = '\0'; 00420 } 00421 ast_mutex_lock(&dblock); 00422 if (dbinit()) { 00423 ast_mutex_unlock(&dblock); 00424 ast_log(LOG_WARNING, "Database unavailable\n"); 00425 return NULL; 00426 } 00427 memset(&key, 0, sizeof(key)); 00428 memset(&data, 0, sizeof(data)); 00429 pass = 0; 00430 while (!(res = astdb->seq(astdb, &key, &data, pass++ ? R_NEXT : R_FIRST))) { 00431 if (key.size) { 00432 keys = key.data; 00433 keys[key.size - 1] = '\0'; 00434 } else { 00435 keys = "<bad key>"; 00436 } 00437 if (data.size) { 00438 values = data.data; 00439 values[data.size - 1] = '\0'; 00440 } else { 00441 values = "<bad value>"; 00442 } 00443 values_len = strlen(values) + 1; 00444 if (keymatch(keys, prefix) && (cur = ast_malloc(sizeof(*cur) + strlen(keys) + 1 + values_len))) { 00445 cur->next = NULL; 00446 cur->key = cur->data + values_len; 00447 strcpy(cur->data, values); 00448 strcpy(cur->key, keys); 00449 if (last) { 00450 last->next = cur; 00451 } else { 00452 ret = cur; 00453 } 00454 last = cur; 00455 } 00456 } 00457 ast_mutex_unlock(&dblock); 00458 return ret; 00459 }
int ast_db_put | ( | const char * | family, | |
const char * | keys, | |||
char * | value | |||
) |
Definition at line 145 of file db.c.
References ast_log(), ast_mutex_lock, ast_mutex_unlock, dbinit(), dblock, and LOG_WARNING.
Referenced by ast_privacy_set(), cache_save(), cache_save_hint(), database_increment(), database_put(), dump_agents(), dump_queue_members(), function_db_write(), handle_command_response(), handle_dbput(), iax_provision_build(), manager_dbput(), mgcp_ss(), parse_register_contact(), save_secret(), ss_thread(), and update_registry().
00146 { 00147 char fullkey[256]; 00148 DBT key, data; 00149 int res, fullkeylen; 00150 00151 ast_mutex_lock(&dblock); 00152 if (dbinit()) { 00153 ast_mutex_unlock(&dblock); 00154 return -1; 00155 } 00156 00157 fullkeylen = snprintf(fullkey, sizeof(fullkey), "/%s/%s", family, keys); 00158 memset(&key, 0, sizeof(key)); 00159 memset(&data, 0, sizeof(data)); 00160 key.data = fullkey; 00161 key.size = fullkeylen + 1; 00162 data.data = value; 00163 data.size = strlen(value) + 1; 00164 res = astdb->put(astdb, &key, &data, 0); 00165 astdb->sync(astdb, 0); 00166 ast_mutex_unlock(&dblock); 00167 if (res) 00168 ast_log(LOG_WARNING, "Unable to put value '%s' for key '%s' in family '%s'\n", value, keys, family); 00169 return res; 00170 }
int astdb_init | ( | void | ) |
Provided by db.c
Definition at line 588 of file db.c.
References ast_cli_register_multiple(), ast_manager_register, cli_database, dbinit(), EVENT_FLAG_SYSTEM, manager_dbget(), and manager_dbput().
Referenced by main().
00589 { 00590 dbinit(); 00591 ast_cli_register_multiple(cli_database, sizeof(cli_database) / sizeof(struct ast_cli_entry)); 00592 ast_manager_register("DBGet", EVENT_FLAG_SYSTEM, manager_dbget, "Get DB Entry"); 00593 ast_manager_register("DBPut", EVENT_FLAG_SYSTEM, manager_dbput, "Put DB Entry"); 00594 return 0; 00595 }
static int database_del | ( | int | fd, | |
int | argc, | |||
char * | argv[] | |||
) | [static] |
Definition at line 273 of file db.c.
References ast_cli(), ast_db_del(), RESULT_SHOWUSAGE, and RESULT_SUCCESS.
00274 { 00275 int res; 00276 if (argc != 4) 00277 return RESULT_SHOWUSAGE; 00278 res = ast_db_del(argv[2], argv[3]); 00279 if (res) { 00280 ast_cli(fd, "Database entry does not exist.\n"); 00281 } else { 00282 ast_cli(fd, "Database entry removed.\n"); 00283 } 00284 return RESULT_SUCCESS; 00285 }
static int database_deltree | ( | int | fd, | |
int | argc, | |||
char * | argv[] | |||
) | [static] |
Definition at line 287 of file db.c.
References ast_cli(), ast_db_deltree(), RESULT_SHOWUSAGE, and RESULT_SUCCESS.
00288 { 00289 int res; 00290 if ((argc < 3) || (argc > 4)) 00291 return RESULT_SHOWUSAGE; 00292 if (argc == 4) { 00293 res = ast_db_deltree(argv[2], argv[3]); 00294 } else { 00295 res = ast_db_deltree(argv[2], NULL); 00296 } 00297 if (res) { 00298 ast_cli(fd, "Database entries do not exist.\n"); 00299 } else { 00300 ast_cli(fd, "Database entries removed.\n"); 00301 } 00302 return RESULT_SUCCESS; 00303 }
static int database_get | ( | int | fd, | |
int | argc, | |||
char * | argv[] | |||
) | [static] |
Definition at line 258 of file db.c.
References ast_cli(), ast_db_get(), RESULT_SHOWUSAGE, and RESULT_SUCCESS.
00259 { 00260 int res; 00261 char tmp[256]; 00262 if (argc != 4) 00263 return RESULT_SHOWUSAGE; 00264 res = ast_db_get(argv[2], argv[3], tmp, sizeof(tmp)); 00265 if (res) { 00266 ast_cli(fd, "Database entry not found.\n"); 00267 } else { 00268 ast_cli(fd, "Value: %s\n", tmp); 00269 } 00270 return RESULT_SUCCESS; 00271 }
static int database_put | ( | int | fd, | |
int | argc, | |||
char * | argv[] | |||
) | [static] |
Definition at line 244 of file db.c.
References ast_cli(), ast_db_put(), RESULT_SHOWUSAGE, and RESULT_SUCCESS.
00245 { 00246 int res; 00247 if (argc != 5) 00248 return RESULT_SHOWUSAGE; 00249 res = ast_db_put(argv[2], argv[3], argv[4]); 00250 if (res) { 00251 ast_cli(fd, "Failed to update entry\n"); 00252 } else { 00253 ast_cli(fd, "Updated database successfully\n"); 00254 } 00255 return RESULT_SUCCESS; 00256 }
static int database_show | ( | int | fd, | |
int | argc, | |||
char * | argv[] | |||
) | [static] |
Definition at line 305 of file db.c.
References ast_cli(), ast_mutex_lock, ast_mutex_unlock, dbinit(), dblock, keymatch(), keys, prefix, RESULT_SHOWUSAGE, and RESULT_SUCCESS.
00306 { 00307 char prefix[256]; 00308 DBT key, data; 00309 char *keys, *values; 00310 int res; 00311 int pass; 00312 00313 if (argc == 4) { 00314 /* Family and key tree */ 00315 snprintf(prefix, sizeof(prefix), "/%s/%s", argv[2], argv[3]); 00316 } else if (argc == 3) { 00317 /* Family only */ 00318 snprintf(prefix, sizeof(prefix), "/%s", argv[2]); 00319 } else if (argc == 2) { 00320 /* Neither */ 00321 prefix[0] = '\0'; 00322 } else { 00323 return RESULT_SHOWUSAGE; 00324 } 00325 ast_mutex_lock(&dblock); 00326 if (dbinit()) { 00327 ast_mutex_unlock(&dblock); 00328 ast_cli(fd, "Database unavailable\n"); 00329 return RESULT_SUCCESS; 00330 } 00331 memset(&key, 0, sizeof(key)); 00332 memset(&data, 0, sizeof(data)); 00333 pass = 0; 00334 while (!(res = astdb->seq(astdb, &key, &data, pass++ ? R_NEXT : R_FIRST))) { 00335 if (key.size) { 00336 keys = key.data; 00337 keys[key.size - 1] = '\0'; 00338 } else { 00339 keys = "<bad key>"; 00340 } 00341 if (data.size) { 00342 values = data.data; 00343 values[data.size - 1]='\0'; 00344 } else { 00345 values = "<bad value>"; 00346 } 00347 if (keymatch(keys, prefix)) { 00348 ast_cli(fd, "%-50s: %-25s\n", keys, values); 00349 } 00350 } 00351 ast_mutex_unlock(&dblock); 00352 return RESULT_SUCCESS; 00353 }
static int database_showkey | ( | int | fd, | |
int | argc, | |||
char * | argv[] | |||
) | [static] |
Definition at line 355 of file db.c.
References ast_cli(), ast_mutex_lock, ast_mutex_unlock, dbinit(), dblock, keys, RESULT_SHOWUSAGE, RESULT_SUCCESS, and subkeymatch().
00356 { 00357 char suffix[256]; 00358 DBT key, data; 00359 char *keys, *values; 00360 int res; 00361 int pass; 00362 00363 if (argc == 3) { 00364 /* Key only */ 00365 snprintf(suffix, sizeof(suffix), "/%s", argv[2]); 00366 } else { 00367 return RESULT_SHOWUSAGE; 00368 } 00369 ast_mutex_lock(&dblock); 00370 if (dbinit()) { 00371 ast_mutex_unlock(&dblock); 00372 ast_cli(fd, "Database unavailable\n"); 00373 return RESULT_SUCCESS; 00374 } 00375 memset(&key, 0, sizeof(key)); 00376 memset(&data, 0, sizeof(data)); 00377 pass = 0; 00378 while (!(res = astdb->seq(astdb, &key, &data, pass++ ? R_NEXT : R_FIRST))) { 00379 if (key.size) { 00380 keys = key.data; 00381 keys[key.size - 1] = '\0'; 00382 } else { 00383 keys = "<bad key>"; 00384 } 00385 if (data.size) { 00386 values = data.data; 00387 values[data.size - 1]='\0'; 00388 } else { 00389 values = "<bad value>"; 00390 } 00391 if (subkeymatch(keys, suffix)) { 00392 ast_cli(fd, "%-50s: %-25s\n", keys, values); 00393 } 00394 } 00395 ast_mutex_unlock(&dblock); 00396 return RESULT_SUCCESS; 00397 }
static int dbinit | ( | void | ) | [static] |
Definition at line 63 of file db.c.
References ast_config_AST_DB, ast_log(), errno, and LOG_WARNING.
Referenced by ast_db_del(), ast_db_deltree(), ast_db_get(), ast_db_gettree(), ast_db_put(), astdb_init(), database_show(), and database_showkey().
00064 { 00065 if (!astdb && !(astdb = dbopen((char *)ast_config_AST_DB, O_CREAT | O_RDWR, 0664, DB_BTREE, NULL))) { 00066 ast_log(LOG_WARNING, "Unable to open Asterisk database '%s': %s\n", ast_config_AST_DB, strerror(errno)); 00067 return -1; 00068 } 00069 return 0; 00070 }
static int keymatch | ( | const char * | key, | |
const char * | prefix | |||
) | [inline, static] |
Definition at line 73 of file db.c.
Referenced by ast_db_deltree(), ast_db_gettree(), and database_show().
00074 { 00075 int preflen = strlen(prefix); 00076 if (!preflen) 00077 return 1; 00078 if (!strcasecmp(key, prefix)) 00079 return 1; 00080 if ((strlen(key) > preflen) && !strncasecmp(key, prefix, preflen)) { 00081 if (key[preflen] == '/') 00082 return 1; 00083 } 00084 return 0; 00085 }
static int manager_dbget | ( | struct mansession * | s, | |
const struct message * | m | |||
) | [static] |
Definition at line 551 of file db.c.
References ast_db_get(), ast_strlen_zero(), astman_append(), astman_get_header(), astman_send_ack(), astman_send_error(), and s.
Referenced by astdb_init().
00552 { 00553 const char *id = astman_get_header(m,"ActionID"); 00554 char idText[256] = ""; 00555 const char *family = astman_get_header(m, "Family"); 00556 const char *key = astman_get_header(m, "Key"); 00557 char tmp[256]; 00558 int res; 00559 00560 if (ast_strlen_zero(family)) { 00561 astman_send_error(s, m, "No family specified."); 00562 return 0; 00563 } 00564 if (ast_strlen_zero(key)) { 00565 astman_send_error(s, m, "No key specified."); 00566 return 0; 00567 } 00568 00569 if (!ast_strlen_zero(id)) 00570 snprintf(idText, sizeof(idText) ,"ActionID: %s\r\n", id); 00571 00572 res = ast_db_get(family, key, tmp, sizeof(tmp)); 00573 if (res) { 00574 astman_send_error(s, m, "Database entry not found"); 00575 } else { 00576 astman_send_ack(s, m, "Result will follow"); 00577 astman_append(s, "Event: DBGetResponse\r\n" 00578 "Family: %s\r\n" 00579 "Key: %s\r\n" 00580 "Val: %s\r\n" 00581 "%s" 00582 "\r\n", 00583 family, key, tmp, idText); 00584 } 00585 return 0; 00586 }
static int manager_dbput | ( | struct mansession * | s, | |
const struct message * | m | |||
) | [static] |
Definition at line 526 of file db.c.
References ast_db_put(), ast_strlen_zero(), astman_get_header(), astman_send_ack(), astman_send_error(), s, and S_OR.
Referenced by astdb_init().
00527 { 00528 const char *family = astman_get_header(m, "Family"); 00529 const char *key = astman_get_header(m, "Key"); 00530 const char *val = astman_get_header(m, "Val"); 00531 int res; 00532 00533 if (ast_strlen_zero(family)) { 00534 astman_send_error(s, m, "No family specified"); 00535 return 0; 00536 } 00537 if (ast_strlen_zero(key)) { 00538 astman_send_error(s, m, "No key specified"); 00539 return 0; 00540 } 00541 00542 res = ast_db_put(family, key, (char *) S_OR(val, "")); 00543 if (res) { 00544 astman_send_error(s, m, "Failed to update entry"); 00545 } else { 00546 astman_send_ack(s, m, "Updated database successfully"); 00547 } 00548 return 0; 00549 }
static int subkeymatch | ( | const char * | key, | |
const char * | suffix | |||
) | [inline, static] |
Definition at line 87 of file db.c.
Referenced by database_showkey().
00088 { 00089 int suffixlen = strlen(suffix); 00090 if (suffixlen) { 00091 const char *subkey = key + strlen(key) - suffixlen; 00092 if (subkey < key) 00093 return 0; 00094 if (!strcasecmp(subkey, suffix)) 00095 return 1; 00096 } 00097 return 0; 00098 }
struct ast_cli_entry cli_database[] |
char database_del_usage[] [static] |
char database_deltree_usage[] [static] |
char database_get_usage[] [static] |
char database_put_usage[] [static] |
char database_show_usage[] [static] |
char database_showkey_usage[] [static] |
ast_mutex_t dblock = { PTHREAD_MUTEX_INITIALIZER , 1, { NULL }, { 0 }, 0, { NULL }, { 0 }, PTHREAD_MUTEX_INITIALIZER } [static] |
Definition at line 61 of file db.c.
Referenced by ast_db_del(), ast_db_deltree(), ast_db_get(), ast_db_gettree(), ast_db_put(), database_show(), and database_showkey().