Asterisk - The Open Source Telephony Project
21.4.1
|
Standard Command Line Interface. More...
#include "asterisk.h"
#include "asterisk/_private.h"
#include "asterisk/paths.h"
#include <signal.h>
#include <ctype.h>
#include <regex.h>
#include <pwd.h>
#include <grp.h>
#include "asterisk/cli.h"
#include "asterisk/linkedlists.h"
#include "asterisk/module.h"
#include "asterisk/pbx.h"
#include "asterisk/channel.h"
#include "asterisk/utils.h"
#include "asterisk/app.h"
#include "asterisk/lock.h"
#include "asterisk/threadstorage.h"
#include "asterisk/logger_category.h"
#include "asterisk/translate.h"
#include "asterisk/bridge.h"
#include "asterisk/stasis_channels.h"
#include "asterisk/stasis_bridges.h"
#include "asterisk/vector.h"
#include "asterisk/stream.h"
Go to the source code of this file.
Data Structures | |
struct | channel_set_debug_args |
struct | cli_perm |
List of restrictions per user. More... | |
struct | cli_perm_head |
struct | cli_perms |
List of users and permissions. More... | |
struct | helpers |
struct | module_level |
map a debug or verbose level to a module name More... | |
struct | module_level_list |
struct | usergroup_cli_perm |
list of users to apply restrictions. More... | |
Macros | |
#define | AST_CLI_INITLEN 256 |
Initial buffer size for resulting strings in ast_cli() | |
#define | CONCISE_FORMAT_STRING "%s!%s!%s!%d!%s!%s!%s!%s!%s!%s!%d!%s!%s!%s\n" |
#define | DAY (HOUR*24) |
#define | DEBUG_HANDLER 0 |
#define | FORMAT_STRING "%-64.64s %-32.32s %-7.7s %-30.30s\n" |
#define | FORMAT_STRING "%-25s %-20s %-20s\n" |
#define | FORMAT_STRING2 "%-64.64s %-32.32s %-7.7s %-30.30s\n" |
#define | HOUR (MINUTE*60) |
#define | MINUTE (SECOND*60) |
#define | MODLIST_FORMAT "%-30s %-40.40s %-10d %-11s %13s\n" |
#define | MODLIST_FORMAT2 "%-30s %-40.40s %-10s %-11s %13s\n" |
#define | NEEDCOMMA(x) ((x) ? ", " : "") /* define if we need a comma */ |
#define | SECOND (1) |
#define | TRACE_HANDLER 1 |
#define | VERBOSE_FORMAT_STRING "%-80.80s %-24.24s %-24.24s %4d %-7.7s %-12.12s %-25.25s %-15.15s %8.8s %-11.11s %-11.11s %-20.20s\n" |
#define | VERBOSE_FORMAT_STRING2 "%-80.80s %-24.24s %-24.24s %-4.4s %-7.7s %-12.12s %-25.25s %-15.15s %8.8s %-11.11s %-11.11s %-20.20s\n" |
#define | VERBOSE_HANDLER 2 |
#define | WEEK (DAY*7) |
#define | YEAR (DAY*365) |
Functions | |
static char * | __ast_cli_generator (const char *text, const char *word, int state, int lock) |
int | __ast_cli_register (struct ast_cli_entry *e, struct ast_module *module) |
int | __ast_cli_register_multiple (struct ast_cli_entry *e, int len, struct ast_module *module) |
static void | __init_ast_cli_buf (void) |
static int | allowed_on_shutdown (struct ast_cli_entry *e) |
void | ast_builtins_init (void) |
initialize the _full_cmd string in * each of the builtins. More... | |
void | ast_cli (int fd, const char *fmt,...) |
int | ast_cli_allow_at_shutdown (struct ast_cli_entry *e) |
Allow a CLI command to be executed while Asterisk is shutting down. More... | |
void | ast_cli_channels_init (void) |
int | ast_cli_command_full (int uid, int gid, int fd, const char *s) |
Interprets a command Interpret a command s, sending output to fd if uid:gid has permissions to run this command. uid = CLI_NO_PERMS to avoid checking user permissions gid = CLI_NO_PERMS to avoid checking group permissions. More... | |
int | ast_cli_command_multiple_full (int uid, int gid, int fd, size_t size, const char *s) |
Executes multiple CLI commands Interpret strings separated by NULL and execute each one, sending output to fd if uid has permissions, uid = CLI_NO_PERMS to avoid checking users permissions. gid = CLI_NO_PERMS to avoid checking group permissions. More... | |
char * | ast_cli_complete (const char *word, const char *const choices[], int state) |
int | ast_cli_completion_add (char *value) |
Add a result to a request for completion options. More... | |
char ** | ast_cli_completion_matches (const char *text, const char *word) |
Generates a NULL-terminated array of strings that 1) begin with the string in the second parameter, and 2) are valid in a command after the string in the first parameter. More... | |
struct ast_vector_string * | ast_cli_completion_vector (const char *text, const char *word) |
Generates a vector of strings for CLI completion. More... | |
char * | ast_cli_generator (const char *text, const char *word, int state) |
Readline madness Useful for readline, that's about it. More... | |
int | ast_cli_perms_init (int reload) |
void | ast_cli_print_timestr_fromseconds (int fd, int seconds, const char *prefix) |
Print on cli a duration in seconds in format s year(s), s week(s), s day(s), s hour(s), s second(s) More... | |
int | ast_cli_unregister (struct ast_cli_entry *e) |
Unregisters a command or an array of commands. More... | |
int | ast_cli_unregister_multiple (struct ast_cli_entry *e, int len) |
Unregister multiple commands. More... | |
char * | ast_complete_channels (const char *line, const char *word, int pos, int state, int rpos) |
Command completion for the list of active channels. More... | |
unsigned int | ast_debug_get_by_module (const char *module) |
Get the debug level for a module. More... | |
AST_THREADSTORAGE_RAW (completion_storage) | |
unsigned int | ast_trace_get_by_module (const char *module) |
Get the trace level for a module. More... | |
static int | channel_set_debug (void *obj, void *arg, void *data, int flags) |
static void | cli_channels_shutdown (void) |
static int | cli_completion_vector_add (struct ast_vector_string *vec, char *value) |
static int | cli_has_permissions (int uid, int gid, const char *command) |
static int | cli_is_registered (struct ast_cli_entry *e) |
static struct ast_cli_entry * | cli_next (struct ast_cli_entry *e) |
static void | cli_shutdown (void) |
static char * | complete_number (const char *partial, unsigned int min, unsigned int max, int n) |
static void | destroy_user_perms (void) |
cleanup (free) cli_perms linkedlist. | |
static char * | find_best (const char *argv[]) |
static struct ast_cli_entry * | find_cli (const char *const cmds[], int match_type) |
static struct module_level * | find_module_level (const char *module, struct module_level_list *mll) |
Find the module level setting. More... | |
static char * | group_show_channels (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
static char * | handle_chanlist (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
static char * | handle_cli_check_permissions (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
handles CLI command 'cli check permissions' | |
static char * | handle_cli_malloc_trim (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
static char * | handle_cli_reload_permissions (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
handles CLI command 'cli reload permissions' | |
static char * | handle_cli_show_permissions (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
handles CLI command 'cli show permissions' | |
static char * | handle_cli_wait_fullybooted (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
static char * | handle_commandmatchesarray (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
static char * | handle_core_reload (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
static char * | handle_core_set_debug_channel (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
static char * | handle_debug (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
static char * | handle_debug_category (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
static char * | handle_debug_or_trace (int handler, struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
static char * | handle_help (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
static char * | handle_load (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
static char * | handle_logger_mute (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
static char * | handle_modlist (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
static char * | handle_nodebugchan_deprecated (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
static char * | handle_refresh (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
static char * | handle_reload (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
static char * | handle_showcalls (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
static char * | handle_showchan (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
static char * | handle_showuptime (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
static char * | handle_softhangup (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
static char * | handle_trace (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
static char * | handle_unload (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
static char * | handle_verbose (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
static char * | help1 (int fd, const char *const match[], int locked) |
helper for final part of handle_help if locked = 1, assume the list is already locked | |
static char * | is_prefix (const char *word, const char *token, int pos, int *actual) |
if word is a valid prefix for token, returns the pos-th match as a malloced string, or NULL otherwise. Always tell in *actual how many matches we got. | |
static int | modlist_modentry (const char *module, const char *description, int usecnt, const char *status, const char *like, enum ast_module_support_level support_level) |
static int | more_words (const char *const *dst) |
returns true if there are more words to match | |
static char * | parse_args (const char *s, int *argc, const char *argv[], int max, int *trailingwhitespace) |
static void | print_uptimestr (int fd, struct timeval timeval, const char *prefix, int printsec) |
static void | remove_shutdown_command (struct ast_cli_entry *e) |
static int | set_full_cmd (struct ast_cli_entry *e) |
static void | status_debug_verbose (struct ast_cli_args *a, int handler, int old_val, int cur_val) |
static int | word_match (const char *cmd, const char *cli_word) |
Variables | |
static struct ast_threadstorage | ast_cli_buf = { .once = PTHREAD_ONCE_INIT , .key_init = __init_ast_cli_buf , .custom_init = NULL , } |
static struct ast_cli_entry | cli_channels_cli [] |
static struct ast_cli_entry | cli_cli [] |
static int | cli_default_perm = 1 |
Default permissions value 1=Permit 0=Deny. | |
static struct cli_perms | cli_perms = { .first = NULL, .last = NULL, .lock = { PTHREAD_RWLOCK_INITIALIZER , NULL, {1, 0} } , } |
static const char | cli_rsvd [] = "[]{}|*%" |
static int | climodentryfd = -1 |
static ast_mutex_t | climodentrylock = { PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP , NULL, {1, 0} } |
static struct module_level_list | debug_modules = AST_RWLIST_HEAD_INIT_VALUE |
static struct helpers | helpers = { .first = NULL, .last = NULL, .lock = { PTHREAD_RWLOCK_INITIALIZER , NULL, {1, 0} } , } |
static const char | perms_config [] = "cli_permissions.conf" |
CLI permissions config file. | |
static ast_mutex_t | permsconfiglock = { PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP , NULL, {1, 0} } |
mutex used to prevent a user from running the 'cli reload permissions' command while it is already running. | |
struct { | |
size_t current | |
struct ast_cli_entry ** elems | |
size_t max | |
} | shutdown_commands |
static ast_rwlock_t | shutdown_commands_lock = { PTHREAD_RWLOCK_INITIALIZER , NULL, {1, 0} } |
static struct module_level_list | trace_modules = AST_RWLIST_HEAD_INIT_VALUE |
Standard Command Line Interface.
Definition in file main/cli.c.
void ast_builtins_init | ( | void | ) |
initialize the _full_cmd string in * each of the builtins.
Provided by cli.c
Definition at line 2238 of file main/cli.c.
References ast_cli_register_multiple, ast_register_cleanup(), and AST_VECTOR_INIT.
int ast_cli_allow_at_shutdown | ( | struct ast_cli_entry * | e | ) |
Allow a CLI command to be executed while Asterisk is shutting down.
CLI commands by defeault are disabled when Asterisk is shutting down. This is to ensure the safety of the shutdown since CLI commands may attempt to access resources that have been freed as a result of the shutdown.
If a CLI command should be allowed at shutdown, then the best way to enable this is to call ast_cli_allow_at_shutdown during the CLI_INIT state of the CLI handler.
Definition at line 3061 of file main/cli.c.
References AST_VECTOR_APPEND.
void ast_cli_channels_init | ( | void | ) |
Provided by cli.c
Definition at line 2245 of file main/cli.c.
References ast_cli_register_multiple, and ast_register_cleanup().
int ast_cli_command_full | ( | int | uid, |
int | gid, | ||
int | fd, | ||
const char * | s | ||
) |
Interprets a command Interpret a command s, sending output to fd if uid:gid has permissions to run this command. uid = CLI_NO_PERMS to avoid checking user permissions gid = CLI_NO_PERMS to avoid checking group permissions.
uid | User ID that is trying to run the command. |
gid | Group ID that is trying to run the command. |
fd | pipe |
s | incoming string |
0 | on success |
-1 | on failure |
Definition at line 2974 of file main/cli.c.
References ast_atomic_fetchadd_int(), ast_join, ast_module_running_ref, ast_module_unref, AST_RWLIST_RDLOCK, AST_RWLIST_UNLOCK, ast_shutting_down(), find_best(), ast_cli_entry::inuse, ast_cli_entry::module, S_OR, and ast_cli_entry::usage.
Referenced by ast_cli_command_multiple_full().
int ast_cli_command_multiple_full | ( | int | uid, |
int | gid, | ||
int | fd, | ||
size_t | size, | ||
const char * | s | ||
) |
Executes multiple CLI commands Interpret strings separated by NULL and execute each one, sending output to fd if uid has permissions, uid = CLI_NO_PERMS to avoid checking users permissions. gid = CLI_NO_PERMS to avoid checking group permissions.
uid | User ID that is trying to run the command. |
gid | Group ID that is trying to run the command. |
fd | pipe |
size | is the total size of the string |
s | incoming string |
Definition at line 3039 of file main/cli.c.
References ast_cli_command_full().
char* ast_cli_complete | ( | const char * | word, |
const char *const | choices[], | ||
int | pos | ||
) |
Helper function to generate cli entries from a NULL-terminated array. Returns the n-th matching entry from the array, or NULL if not found. Can be used to implement generate() for static entries as below (in this example we complete the word in position 2):
Definition at line 1846 of file main/cli.c.
References ast_cli_completion_add(), and ast_strdup.
Referenced by handle_orig().
int ast_cli_completion_add | ( | char * | value | ) |
Add a result to a request for completion options.
value | A completion option text. |
0 | Success |
-1 | Failure |
This is an alternative to returning individual values from CLI_GENERATE. Instead of repeatedly being asked for the next match and having to start over, you can call this function repeatedly from your own stateful loop. When all matches have been added you can return NULL from the CLI_GENERATE function.
Definition at line 2761 of file main/cli.c.
References ast_threadstorage_get_ptr().
Referenced by ast_cli_complete(), ast_complete_applications(), ast_complete_channels(), cli_complete_show(), handle_cli_sound_show(), and handle_show_named_acl_cmd().
char** ast_cli_completion_matches | ( | const char * | , |
const char * | |||
) |
Generates a NULL-terminated array of strings that 1) begin with the string in the second parameter, and 2) are valid in a command after the string in the first parameter.
The first entry (offset 0) of the result is the longest common substring in the results, useful to extend the string that has been completed. Subsequent entries are all possible values, followed by a NULL. All strings and the array itself are malloc'ed and must be freed by the caller.
Definition at line 2705 of file main/cli.c.
References ast_cli_completion_vector(), AST_VECTOR_APPEND, AST_VECTOR_CALLBACK_VOID, AST_VECTOR_PTR_FREE, and AST_VECTOR_STEAL_ELEMENTS.
struct ast_vector_string* ast_cli_completion_vector | ( | const char * | text, |
const char * | word | ||
) |
Generates a vector of strings for CLI completion.
text | Complete input being matched. |
word | Current word being matched |
The results contain strings that both: 1) Begin with the string in word. 2) Are valid in a command after the string in text.
The first entry (offset 0) of the result is the longest common substring in the results, useful to extend the string that has been completed. Subsequent entries are all possible values.
Definition at line 2766 of file main/cli.c.
References ast_calloc, ast_cli_generator(), ast_strndup, ast_threadstorage_get_ptr(), ast_threadstorage_set_ptr(), AST_VECTOR_CALLBACK_VOID, AST_VECTOR_GET, AST_VECTOR_INSERT_AT, AST_VECTOR_PTR_FREE, AST_VECTOR_REMOVE, and AST_VECTOR_SIZE.
Referenced by ast_cli_completion_matches().
char* ast_cli_generator | ( | const char * | , |
const char * | , | ||
int | |||
) |
Readline madness Useful for readline, that's about it.
0 | on success |
-1 | on failure |
Only call this function to proxy the CLI generator to another.
Definition at line 2952 of file main/cli.c.
Referenced by ast_cli_completion_vector(), cli_alias_passthrough(), and handle_cli_check_permissions().
int ast_cli_perms_init | ( | int | reload | ) |
Provided by cli.c
Definition at line 2105 of file main/cli.c.
References ast_calloc, ast_category_browse(), ast_config_destroy(), ast_config_load2(), AST_LIST_INSERT_TAIL, AST_LIST_TRAVERSE, AST_RWLIST_UNLOCK, AST_RWLIST_WRLOCK, ast_strdup, cli_default_perm, cli_perm::command, CONFIG_FLAG_FILEUNCHANGED, destroy_user_perms(), usergroup_cli_perm::gid, ast_variable::name, ast_variable::next, cli_perm::permit, usergroup_cli_perm::perms, perms_config, permsconfiglock, usergroup_cli_perm::uid, and ast_variable::value.
Referenced by handle_cli_reload_permissions().
void ast_cli_print_timestr_fromseconds | ( | int | fd, |
int | seconds, | ||
const char * | prefix | ||
) |
Print on cli a duration in seconds in format s year(s), s week(s), s day(s), s hour(s), s second(s)
fd | fd to print by ast_cli |
seconds | The time (in seconds) to print |
prefix | A Prefix string to add before of duration formatted |
Definition at line 3056 of file main/cli.c.
References ast_tv().
Referenced by handle_cdr_pgsql_status(), and realtime_ldap_status().
int ast_cli_unregister | ( | struct ast_cli_entry * | e | ) |
Unregisters a command or an array of commands.
e | which cli entry to unregister Unregister your own command. You must pass a completed ast_cli_entry structure |
Definition at line 2432 of file main/cli.c.
References ast_cli_entry::_full_cmd, AST_RWLIST_UNLOCK, AST_RWLIST_WRLOCK, ast_cli_entry::cmda, ast_cli_entry::command, ast_cli_entry::inuse, and ast_cli_entry::usage.
Referenced by alias_unregister_cb(), ast_cli_unregister_multiple(), unload_module(), and xmldoc_unload_documentation().
int ast_cli_unregister_multiple | ( | struct ast_cli_entry * | e, |
int | len | ||
) |
Unregister multiple commands.
e | pointer to first cli entry to unregister |
len | number of entries to unregister |
Definition at line 2543 of file main/cli.c.
References ast_cli_unregister().
char* ast_complete_channels | ( | const char * | line, |
const char * | word, | ||
int | pos, | ||
int | state, | ||
int | rpos | ||
) |
Command completion for the list of active channels.
This can be called from a CLI command completion function that wants to complete from the list of active channels. 'rpos' is the required position in the command. This function will return NULL immediately if 'rpos' is not the same as the current position, 'pos'.
Definition at line 1865 of file main/cli.c.
References ao2_iterator_destroy(), ao2_iterator_init(), ao2_ref, ast_cli_completion_add(), ast_strdup, ast_channel_snapshot::base, ast_channel_snapshot_base::name, and ast_channel_snapshot::state.
Referenced by handle_cli_agi_add_cmd(), handle_redirect(), handle_show_chanvar(), handle_show_hangup_channel(), and handle_showchan().
unsigned int ast_debug_get_by_module | ( | const char * | module | ) |
Get the debug level for a module.
module | the name of module |
Definition at line 136 of file main/cli.c.
References AST_LIST_TRAVERSE, AST_RWLIST_RDLOCK, and AST_RWLIST_UNLOCK.
|
static |
Find the module level setting.
module | Module name to look for. |
mll | List to search. |
level | struct found on success. |
NULL | not found. |
Definition at line 351 of file main/cli.c.
References AST_LIST_TRAVERSE.
|
static |
< Buffer for CDR variables.
< Accumulation buffer for all output.
Definition at line 1645 of file main/cli.c.
References ast_callid_strnprint(), ast_cdr_serialize_variables(), AST_CHAN_TP_INTERNAL, ast_channel_get_bridge(), ast_channel_get_by_name(), ast_channel_get_stream_topology(), ast_channel_unref, ast_codec_media_type2str(), ast_complete_channels(), ast_format_cap_get_names(), AST_FORMAT_CAP_NAMES_LEN, ast_format_get_name(), AST_LIST_TRAVERSE, ast_state2str(), ast_str_append(), ast_str_buffer(), ast_str_create, ast_str_strlen(), ast_str_thread_get(), ast_stream_get_formats(), ast_stream_get_group(), ast_stream_get_metadata_list(), ast_stream_get_name(), ast_stream_get_state(), ast_stream_get_type(), ast_stream_state2str(), ast_stream_topology_get_count(), ast_stream_topology_get_stream(), ast_translate_path_to_str(), ast_tvnow(), ast_tvzero(), ast_variables_destroy(), ast_bridge::callid, ast_cli_entry::command, ast_variable::name, ast_bridge::name, ast_variable::next, S_COR, S_OR, ast_bridge::uniqueid, ast_cli_entry::usage, and ast_variable::value.
|
static |
initialize the _full_cmd string and related parameters, return 0 on success, -1 on error.
Definition at line 2070 of file main/cli.c.
References ast_cli_entry::_full_cmd, ast_cli_entry::args, ast_join, ast_strdup, ast_cli_entry::cmda, and ast_cli_entry::cmdlen.
|
static |
match a word in the CLI entry. returns -1 on mismatch, 0 on match of an optional word, 1 on match of a full word.
The pattern can be any_word match for equal [foo|bar|baz] optionally, one of these words {foo|bar|baz} exactly, one of these words % any word
Definition at line 2262 of file main/cli.c.
|
static |
Some regexp characters in cli arguments are reserved and used as separators.
Definition at line 2064 of file main/cli.c.
|
static |
lists of module names and their debug/trace levels
Definition at line 107 of file main/cli.c.