Asterisk - The Open Source Telephony Project GIT-master-a358458
Macros | Functions | Variables
pbx_hangup_handler.c File Reference

PBX Hangup Handler management routines. More...

#include "asterisk.h"
#include "asterisk/_private.h"
#include "asterisk/app.h"
#include "asterisk/cli.h"
#include "asterisk/linkedlists.h"
#include "asterisk/pbx.h"
#include "asterisk/stasis_channels.h"
#include "asterisk/utils.h"
Include dependency graph for pbx_hangup_handler.c:

Go to the source code of this file.

Macros

#define HANDLER_FORMAT   "%-30s %s\n"
 

Functions

void ast_pbx_hangup_handler_destroy (struct ast_channel *chan)
 Destroy the hangup handler container on a channel. More...
 
static void ast_pbx_hangup_handler_headers (int fd)
 
void ast_pbx_hangup_handler_init (struct ast_channel *chan)
 Init the hangup handler container on a channel. More...
 
int ast_pbx_hangup_handler_pop (struct ast_channel *chan)
 Pop the top of the channel hangup handler stack. More...
 
void ast_pbx_hangup_handler_push (struct ast_channel *chan, const char *handler)
 Push the given hangup handler onto the channel hangup handler stack. More...
 
int ast_pbx_hangup_handler_run (struct ast_channel *chan)
 Run all hangup handlers on the channel. More...
 
static void ast_pbx_hangup_handler_show (int fd, struct ast_channel *chan)
 
static char * handle_show_hangup_all (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 'show hanguphandlers all' CLI command implementation function... More...
 
static char * handle_show_hangup_channel (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 'show hanguphandlers <channel>' CLI command implementation function... More...
 
int load_pbx_hangup_handler (void)
 
static void publish_hangup_handler_message (const char *action, struct ast_channel *chan, const char *handler)
 
static void unload_pbx_hangup_handler (void)
 

Variables

static struct ast_cli_entry cli []
 

Detailed Description

PBX Hangup Handler management routines.

Author
Corey Farrell git@c.nosp@m.fwar.nosp@m.e.com

Definition in file pbx_hangup_handler.c.

Macro Definition Documentation

◆ HANDLER_FORMAT

#define HANDLER_FORMAT   "%-30s %s\n"

Definition at line 168 of file pbx_hangup_handler.c.

Function Documentation

◆ ast_pbx_hangup_handler_destroy()

void ast_pbx_hangup_handler_destroy ( struct ast_channel chan)

Destroy the hangup handler container on a channel.

Since
11.0
Parameters
chanChannel to destroy the hangup handler container on.

Definition at line 103 of file pbx_hangup_handler.c.

104{
106 struct ast_hangup_handler *h_handler;
107
108 ast_channel_lock(chan);
109
110 /* Get rid of each of the hangup handlers on the channel */
112 while ((h_handler = AST_LIST_REMOVE_HEAD(handlers, node))) {
113 ast_free(h_handler);
114 }
115
116 ast_channel_unlock(chan);
117}
#define ast_free(a)
Definition: astmm.h:180
#define ast_channel_lock(chan)
Definition: channel.h:2922
struct ast_hangup_handler_list * ast_channel_hangup_handlers(struct ast_channel *chan)
#define ast_channel_unlock(chan)
Definition: channel.h:2923
#define AST_LIST_REMOVE_HEAD(head, field)
Removes and returns the head entry from a list.
Definition: linkedlists.h:833
Definition: test_heap.c:38
static const struct ast_aeap_message_handler handlers[]
Definition: test_aeap.c:135

References ast_channel_hangup_handlers(), ast_channel_lock, ast_channel_unlock, ast_free, AST_LIST_REMOVE_HEAD, and handlers.

Referenced by ast_channel_destructor(), and ast_dummy_channel_destructor().

◆ ast_pbx_hangup_handler_headers()

static void ast_pbx_hangup_handler_headers ( int  fd)
static

Definition at line 177 of file pbx_hangup_handler.c.

178{
179 ast_cli(fd, HANDLER_FORMAT, "Channel", "Handler");
180}
void ast_cli(int fd, const char *fmt,...)
Definition: clicompat.c:6
#define HANDLER_FORMAT

References ast_cli(), and HANDLER_FORMAT.

Referenced by handle_show_hangup_all(), and handle_show_hangup_channel().

◆ ast_pbx_hangup_handler_init()

void ast_pbx_hangup_handler_init ( struct ast_channel chan)

Init the hangup handler container on a channel.

Since
11.0
Parameters
chanChannel to init the hangup handler container on.

Definition at line 95 of file pbx_hangup_handler.c.

96{
98
101}
#define AST_LIST_HEAD_INIT_NOLOCK(head)
Initializes a list head structure.
Definition: linkedlists.h:681

References ast_channel_hangup_handlers(), AST_LIST_HEAD_INIT_NOLOCK, and handlers.

Referenced by __ast_channel_alloc_ap(), and __ast_dummy_channel_alloc().

◆ ast_pbx_hangup_handler_pop()

int ast_pbx_hangup_handler_pop ( struct ast_channel chan)

Pop the top of the channel hangup handler stack.

Since
11.0
Parameters
chanChannel to push the hangup handler onto.
Return values
TRUEif a handler was popped off of the stack.

Definition at line 119 of file pbx_hangup_handler.c.

120{
122 struct ast_hangup_handler *h_handler;
123
124 ast_channel_lock(chan);
126 h_handler = AST_LIST_REMOVE_HEAD(handlers, node);
127 if (h_handler) {
128 publish_hangup_handler_message("pop", chan, h_handler->args);
129 }
130 ast_channel_unlock(chan);
131 if (h_handler) {
132 ast_free(h_handler);
133 return 1;
134 }
135 return 0;
136}
static void publish_hangup_handler_message(const char *action, struct ast_channel *chan, const char *handler)

References ast_hangup_handler::args, ast_channel_hangup_handlers(), ast_channel_lock, ast_channel_unlock, ast_free, AST_LIST_REMOVE_HEAD, handlers, and publish_hangup_handler_message().

Referenced by func_channel_write_real().

◆ ast_pbx_hangup_handler_push()

void ast_pbx_hangup_handler_push ( struct ast_channel chan,
const char *  handler 
)

Push the given hangup handler onto the channel hangup handler stack.

Since
11.0
Parameters
chanChannel to push the hangup handler onto.
handlerGosub application parameter string.

Definition at line 138 of file pbx_hangup_handler.c.

139{
141 struct ast_hangup_handler *h_handler;
142 const char *expanded_handler;
143
145 return;
146 }
147
148 expanded_handler = ast_app_expand_sub_args(chan, handler);
149 if (!expanded_handler) {
150 return;
151 }
152 h_handler = ast_malloc(sizeof(*h_handler) + 1 + strlen(expanded_handler));
153 if (!h_handler) {
154 ast_free((char *) expanded_handler);
155 return;
156 }
157 strcpy(h_handler->args, expanded_handler);/* Safe */
158 ast_free((char *) expanded_handler);
159
160 ast_channel_lock(chan);
161
164 publish_hangup_handler_message("push", chan, h_handler->args);
165 ast_channel_unlock(chan);
166}
#define ast_malloc(len)
A wrapper for malloc()
Definition: astmm.h:191
const char * ast_app_expand_sub_args(struct ast_channel *chan, const char *args)
Add missing context/exten to subroutine argument string.
Definition: main/app.c:278
#define AST_LIST_INSERT_HEAD(head, elm, field)
Inserts a list entry at the head of a list.
Definition: linkedlists.h:711
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:65
static void handler(const char *name, int response_code, struct ast_variable *get_params, struct ast_variable *path_vars, struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
Definition: test_ari.c:59

References ast_hangup_handler::args, ast_app_expand_sub_args(), ast_channel_hangup_handlers(), ast_channel_lock, ast_channel_unlock, ast_free, AST_LIST_INSERT_HEAD, ast_malloc, ast_strlen_zero(), handler(), handlers, and publish_hangup_handler_message().

Referenced by func_channel_write_real().

◆ ast_pbx_hangup_handler_run()

int ast_pbx_hangup_handler_run ( struct ast_channel chan)

Run all hangup handlers on the channel.

Since
11.0
Parameters
chanChannel to run the hangup handlers on.
Note
Absolutely NO channel locks should be held before calling this function.
Return values
Zeroif no hangup handlers run.
non-zeroif hangup handlers were run.

Definition at line 58 of file pbx_hangup_handler.c.

59{
61 struct ast_hangup_handler *h_handler;
62
63 ast_channel_lock(chan);
67 return 0;
68 }
69
70 /*
71 * Make sure that the channel is marked as hungup since we are
72 * going to run the hangup handlers on it.
73 */
75
76 for (;;) {
79 if (!h_handler) {
80 break;
81 }
82
83 publish_hangup_handler_message("run", chan, h_handler->args);
85
86 ast_app_exec_sub(NULL, chan, h_handler->args, 1);
87 ast_free(h_handler);
88
89 ast_channel_lock(chan);
90 }
92 return 1;
93}
@ AST_SOFTHANGUP_HANGUP_EXEC
Definition: channel.h:1154
int ast_softhangup_nolock(struct ast_channel *chan, int cause)
Softly hangup up a channel (no channel lock)
Definition: channel.c:2458
int ast_app_exec_sub(struct ast_channel *autoservice_chan, struct ast_channel *sub_chan, const char *sub_args, int ignore_hangup)
Run a subroutine on a channel, placing an optional second channel into autoservice.
Definition: main/app.c:297
#define AST_LIST_EMPTY(head)
Checks whether the specified list contains any entries.
Definition: linkedlists.h:450
#define NULL
Definition: resample.c:96

References ast_hangup_handler::args, ast_app_exec_sub(), ast_channel_hangup_handlers(), ast_channel_lock, ast_channel_unlock, ast_free, AST_LIST_EMPTY, AST_LIST_REMOVE_HEAD, AST_SOFTHANGUP_HANGUP_EXEC, ast_softhangup_nolock(), handlers, NULL, and publish_hangup_handler_message().

Referenced by __ast_pbx_run(), and ast_hangup().

◆ ast_pbx_hangup_handler_show()

static void ast_pbx_hangup_handler_show ( int  fd,
struct ast_channel chan 
)
static

Definition at line 190 of file pbx_hangup_handler.c.

191{
193 struct ast_hangup_handler *h_handler;
194 int first = 1;
195
196 ast_channel_lock(chan);
198 AST_LIST_TRAVERSE(handlers, h_handler, node) {
199 ast_cli(fd, HANDLER_FORMAT, first ? ast_channel_name(chan) : "", h_handler->args);
200 first = 0;
201 }
202 ast_channel_unlock(chan);
203}
struct sla_ringing_trunk * first
Definition: app_sla.c:332
const char * ast_channel_name(const struct ast_channel *chan)
#define AST_LIST_TRAVERSE(head, var, field)
Loops over (traverses) the entries in a list.
Definition: linkedlists.h:491

References ast_hangup_handler::args, ast_channel_hangup_handlers(), ast_channel_lock, ast_channel_name(), ast_channel_unlock, ast_cli(), AST_LIST_TRAVERSE, first, HANDLER_FORMAT, and handlers.

Referenced by handle_show_hangup_all(), and handle_show_hangup_channel().

◆ handle_show_hangup_all()

static char * handle_show_hangup_all ( struct ast_cli_entry e,
int  cmd,
struct ast_cli_args a 
)
static

'show hanguphandlers all' CLI command implementation function...

Definition at line 244 of file pbx_hangup_handler.c.

245{
246 struct ast_channel_iterator *iter;
247 struct ast_channel *chan;
248
249 switch (cmd) {
250 case CLI_INIT:
251 e->command = "core show hanguphandlers all";
252 e->usage =
253 "Usage: core show hanguphandlers all\n"
254 " Show hangup handlers for all channels.\n";
255 return NULL;
256 case CLI_GENERATE:
257 return NULL;
258 }
259
260 if (a->argc < 4) {
261 return CLI_SHOWUSAGE;
262 }
263
265 if (!iter) {
266 return CLI_FAILURE;
267 }
268
270 for (; (chan = ast_channel_iterator_next(iter)); ast_channel_unref(chan)) {
272 }
274
275 return CLI_SUCCESS;
276}
struct ast_channel_iterator * ast_channel_iterator_destroy(struct ast_channel_iterator *i)
Destroy a channel iterator.
Definition: channel.c:1360
struct ast_channel * ast_channel_iterator_next(struct ast_channel_iterator *i)
Get the next channel for a channel iterator.
Definition: channel.c:1422
#define ast_channel_unref(c)
Decrease channel reference count.
Definition: channel.h:2958
struct ast_channel_iterator * ast_channel_iterator_all_new(void)
Create a new channel iterator.
Definition: channel.c:1408
#define CLI_SHOWUSAGE
Definition: cli.h:45
#define CLI_SUCCESS
Definition: cli.h:44
@ CLI_INIT
Definition: cli.h:152
@ CLI_GENERATE
Definition: cli.h:153
#define CLI_FAILURE
Definition: cli.h:46
static void ast_pbx_hangup_handler_headers(int fd)
static void ast_pbx_hangup_handler_show(int fd, struct ast_channel *chan)
Main Channel structure associated with a channel.
char * command
Definition: cli.h:186
const char * usage
Definition: cli.h:177
static struct test_val a

References a, ast_channel_iterator_all_new(), ast_channel_iterator_destroy(), ast_channel_iterator_next(), ast_channel_unref, ast_pbx_hangup_handler_headers(), ast_pbx_hangup_handler_show(), CLI_FAILURE, CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, NULL, and ast_cli_entry::usage.

◆ handle_show_hangup_channel()

static char * handle_show_hangup_channel ( struct ast_cli_entry e,
int  cmd,
struct ast_cli_args a 
)
static

'show hanguphandlers <channel>' CLI command implementation function...

Definition at line 208 of file pbx_hangup_handler.c.

209{
210 struct ast_channel *chan;
211
212 switch (cmd) {
213 case CLI_INIT:
214 e->command = "core show hanguphandlers";
215 e->usage =
216 "Usage: core show hanguphandlers <channel>\n"
217 " Show hangup handlers of a specified channel.\n";
218 return NULL;
219 case CLI_GENERATE:
220 return ast_complete_channels(a->line, a->word, a->pos, a->n, e->args);
221 }
222
223 if (a->argc < 4) {
224 return CLI_SHOWUSAGE;
225 }
226
227 chan = ast_channel_get_by_name(a->argv[3]);
228 if (!chan) {
229 ast_cli(a->fd, "Channel does not exist.\n");
230 return CLI_FAILURE;
231 }
232
235
236 ast_channel_unref(chan);
237
238 return CLI_SUCCESS;
239}
struct ast_channel * ast_channel_get_by_name(const char *name)
Find a channel by name.
Definition: channel.c:1454
char * ast_complete_channels(const char *line, const char *word, int pos, int state, int rpos)
Command completion for the list of active channels.
Definition: main/cli.c:1860
int args
This gets set in ast_cli_register()
Definition: cli.h:185

References a, ast_cli_entry::args, ast_channel_get_by_name(), ast_channel_unref, ast_cli(), ast_complete_channels(), ast_pbx_hangup_handler_headers(), ast_pbx_hangup_handler_show(), CLI_FAILURE, CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, NULL, and ast_cli_entry::usage.

◆ load_pbx_hangup_handler()

int load_pbx_hangup_handler ( void  )

Provided by pbx_hangup_handler.c

Definition at line 288 of file pbx_hangup_handler.c.

289{
292
293 return 0;
294}
int ast_register_cleanup(void(*func)(void))
Register a function to be executed before Asterisk gracefully exits.
Definition: clicompat.c:19
#define ast_cli_register_multiple(e, len)
Register multiple commands.
Definition: cli.h:265
static struct ast_cli_entry cli[]
static void unload_pbx_hangup_handler(void)
#define ARRAY_LEN(a)
Definition: utils.h:666

References ARRAY_LEN, ast_cli_register_multiple, ast_register_cleanup(), cli, and unload_pbx_hangup_handler().

Referenced by asterisk_daemon().

◆ publish_hangup_handler_message()

static void publish_hangup_handler_message ( const char *  action,
struct ast_channel chan,
const char *  handler 
)
static

Definition at line 44 of file pbx_hangup_handler.c.

45{
46 RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
47
48 blob = ast_json_pack("{s: s, s: s}",
49 "type", action,
50 "handler", S_OR(handler, ""));
51 if (!blob) {
52 return;
53 }
54
56}
struct stasis_message_type * ast_channel_hangup_handler_type(void)
Message type for hangup handler related actions.
void ast_channel_publish_blob(struct ast_channel *chan, struct stasis_message_type *type, struct ast_json *blob)
Publish a channel blob message.
void ast_json_unref(struct ast_json *value)
Decrease refcount on value. If refcount reaches zero, value is freed.
Definition: json.c:73
struct ast_json * ast_json_pack(char const *format,...)
Helper for creating complex JSON values.
Definition: json.c:612
#define S_OR(a, b)
returns the equivalent of logic or for strings: first one if not empty, otherwise second one.
Definition: strings.h:80
Abstract JSON element (object, array, string, int, ...).
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:941

References ast_channel_hangup_handler_type(), ast_channel_publish_blob(), ast_json_pack(), ast_json_unref(), handler(), NULL, RAII_VAR, and S_OR.

Referenced by ast_pbx_hangup_handler_pop(), ast_pbx_hangup_handler_push(), and ast_pbx_hangup_handler_run().

◆ unload_pbx_hangup_handler()

static void unload_pbx_hangup_handler ( void  )
static

Definition at line 283 of file pbx_hangup_handler.c.

284{
286}
int ast_cli_unregister_multiple(struct ast_cli_entry *e, int len)
Unregister multiple commands.
Definition: clicompat.c:30

References ARRAY_LEN, ast_cli_unregister_multiple(), and cli.

Referenced by load_pbx_hangup_handler().

Variable Documentation

◆ cli

struct ast_cli_entry cli[]
static
Initial value:
= {
{ .handler = handle_show_hangup_all , .summary = "Show hangup handlers of all channels" ,},
{ .handler = handle_show_hangup_channel , .summary = "Show hangup handlers of a specified channel" ,},
}
static char * handle_show_hangup_all(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
'show hanguphandlers all' CLI command implementation function...
static char * handle_show_hangup_channel(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
'show hanguphandlers <channel>' CLI command implementation function...

Definition at line 278 of file pbx_hangup_handler.c.

Referenced by load_pbx_hangup_handler(), and unload_pbx_hangup_handler().