Index: manager.c =================================================================== --- manager.c (revision 809) +++ manager.c (working copy) @@ -87,6 +87,8 @@ static pthread_t t; AST_MUTEX_DEFINE_STATIC(sessionlock); static int block_sockets = 0; +static int num_sessions = 0; +struct eventqent *master_eventq = NULL; static struct permalias { int num; @@ -240,6 +242,22 @@ return RESULT_SUCCESS; } +/*! \brief handle_showmaneventq: CLI command show manager eventq */ +static int handle_showmaneventq(int fd, int argc, char *argv[]) +{ + struct eventqent *s; + ast_mutex_lock(&sessionlock); + s = master_eventq; + while (s) { + ast_cli(fd, "Usecount: %d\n",s->usecount); + ast_cli(fd, "Category: %d\n", s->category); + ast_cli(fd, "Event:\n%s", s->eventdata); + s = s->next; + } + ast_mutex_unlock(&sessionlock); + return RESULT_SUCCESS; +} + static char showmancmd_help[] = "Usage: show manager command \n" " Shows the detailed description for a specific Asterisk manager interface command.\n"; @@ -253,6 +271,11 @@ " Prints a listing of the users that are currently connected to the\n" "Asterisk manager interface.\n"; +static char showmaneventq_help[] = +"Usage: show manager eventq\n" +" Prints a listing of all events pending in the Asterisk manger\n" +"event queue.\n"; + static struct ast_cli_entry show_mancmd_cli = { { "show", "manager", "command", NULL }, handle_showmancmd, "Show a manager interface command", showmancmd_help, complete_show_mancmd }; @@ -265,6 +288,27 @@ { { "show", "manager", "connected", NULL }, handle_showmanconn, "Show connected manager interface users", showmanconn_help }; +static struct ast_cli_entry show_maneventq_cli = + { { "show", "manager", "eventq", NULL }, + handle_showmaneventq, "Show manager interface queued events", showmaneventq_help }; + +static void unuse_eventqent(struct eventqent *e) +{ + /* XXX Need to atomically decrement the users. Change this to atomic_dec + one day when we have such a beast XXX */ + int val; + ast_mutex_lock(&e->lock); + e->usecount--; + val = !e->usecount && e->next; + ast_mutex_unlock(&e->lock); + /* Wake up sleeping beauty */ + if (val) { + /* The most totally excellent global variable name 't' refers + * to accept_thread()'s handle. */ + pthread_kill(t, SIGURG); + } +} + static void free_session(struct mansession *s) { struct eventqent *eqe; @@ -274,7 +318,7 @@ while(s->eventq) { eqe = s->eventq; s->eventq = s->eventq->next; - free(eqe); + unuse_eventqent(eqe); } free(s); } @@ -296,6 +340,7 @@ else sessions = cur->next; free_session(s); + num_sessions--; } else ast_log(LOG_WARNING, "Trying to delete nonexistent session %p?\n", s); ast_mutex_unlock(&sessionlock); @@ -1237,6 +1282,30 @@ return 0; } +static int process_events(struct mansession *s) +{ + struct eventqent *eqe; + int ret = 0; + ast_mutex_lock(&s->__lock); + if (s->fd > -1) { + s->busy--; + if (!s->eventq) + s->eventq = master_eventq; + while(s->eventq->next) { + eqe = s->eventq->next; + if ((s->authenticated && (s->readperm & eqe->category) == eqe->category) && + ((s->send_events & eqe->category) == eqe->category)) { + if (!ret && ast_carefulwrite(s->fd, eqe->eventdata, strlen(eqe->eventdata), s->writetimeout) < 0) + ret = -1; + } + unuse_eventqent(s->eventq); + s->eventq = eqe; + } + } + ast_mutex_unlock(&s->__lock); + return ret; +} + static int process_message(struct mansession *s, struct message *m) { char action[80] = ""; @@ -1244,6 +1313,7 @@ char *id = astman_get_header(m,"ActionID"); char idText[256] = ""; char iabuf[INET_ADDRSTRLEN]; + int ret = 0; ast_copy_string(action, astman_get_header(m, "Action"), sizeof(action)); ast_log( LOG_DEBUG, "Manager received command '%s'\n", action ); @@ -1294,8 +1364,6 @@ } else astman_send_error(s, m, "Authentication Required"); } else { - int ret=0; - struct eventqent *eqe; ast_mutex_lock(&s->__lock); s->busy = 1; ast_mutex_unlock(&s->__lock); @@ -1313,21 +1381,10 @@ } if (!tmp) astman_send_error(s, m, "Invalid/unknown command"); - ast_mutex_lock(&s->__lock); - s->busy = 0; - while(s->eventq) { - if (ast_carefulwrite(s->fd, s->eventq->eventdata, strlen(s->eventq->eventdata), s->writetimeout) < 0) { - ret = -1; - break; - } - eqe = s->eventq; - s->eventq = s->eventq->next; - free(eqe); - } - ast_mutex_unlock(&s->__lock); + } + if (ret) return ret; - } - return 0; + return process_events(s); } static int get_input(struct mansession *s, char *output) @@ -1356,12 +1413,20 @@ fds[0].fd = s->fd; fds[0].events = POLLIN; do { + ast_mutex_lock(&s->__lock); + s->waiting_thread = pthread_self(); + ast_mutex_unlock(&s->__lock); + res = poll(fds, 1, -1); + + ast_mutex_lock(&s->__lock); + s->waiting_thread = AST_PTHREADT_NULL; + ast_mutex_unlock(&s->__lock); if (res < 0) { if (errno == EINTR) { if (s->dead) return -1; - continue; + return 0; } ast_log(LOG_WARNING, "Select returned error: %s\n", strerror(errno)); return -1; @@ -1403,8 +1468,12 @@ memset(&m, 0, sizeof(m)); } else if (m.hdrcount < AST_MAX_MANHEADERS - 1) m.hdrcount++; - } else if (res < 0) + } else if (res < 0) { break; + } else if (s->eventq->next) { + if (process_events(s)) + break; + } } if (s->authenticated) { if (option_verbose > 1) { @@ -1428,16 +1497,36 @@ int as; struct sockaddr_in sin; socklen_t sinlen; + struct eventqent *eqe; struct mansession *s; struct protoent *p; int arg = 1; int flags; pthread_attr_t attr; + struct pollfd pfds[1]; pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); for (;;) { + ast_mutex_lock(&sessionlock); + /* Purge master event queue of old, unused events, but make sure we + always keep at least one in the queue */ + eqe = master_eventq; + while (master_eventq->next && !master_eventq->usecount) { + eqe = master_eventq; + master_eventq = master_eventq->next; + free(eqe); + } + ast_mutex_unlock(&sessionlock); + + pfds[0].fd = asock; + pfds[0].events = POLLIN; + /* Wait for something to happen, but timeout every few seconds so + we can ditch any old manager sessions */ + if (poll(pfds, 1, 5000) < 1) + continue; + sinlen = sizeof(sin); as = accept(asock, (struct sockaddr *)&sin, &sinlen); if (as < 0) { @@ -1468,8 +1557,17 @@ s->fd = as; s->send_events = -1; ast_mutex_lock(&sessionlock); + num_sessions++; s->next = sessions; sessions = s; + /* Find the last place in the master event queue and hook ourselves + in there */ + s->eventq = master_eventq; + while(s->eventq->next) + s->eventq = s->eventq->next; + ast_mutex_lock(&s->eventq->lock); + s->eventq->usecount++; + ast_mutex_unlock(&s->eventq->lock); ast_mutex_unlock(&sessionlock); if (ast_pthread_create(&s->t, &attr, session_do, s)) destroy_session(s); @@ -1478,21 +1576,24 @@ return NULL; } -static int append_event(struct mansession *s, const char *str) +static int append_event(const char *str, int category) { struct eventqent *tmp, *prev=NULL; tmp = malloc(sizeof(struct eventqent) + strlen(str)); if (tmp) { + ast_mutex_init(&tmp->lock); tmp->next = NULL; + tmp->category = category; strcpy(tmp->eventdata, str); - if (s->eventq) { - prev = s->eventq; + if (master_eventq) { + prev = master_eventq; while(prev->next) prev = prev->next; prev->next = tmp; } else { - s->eventq = tmp; + master_eventq = tmp; } + tmp->usecount = num_sessions; return 0; } return -1; @@ -1507,36 +1608,28 @@ char *tmp_next = tmp; size_t tmp_left = sizeof(tmp) - 2; va_list ap; + struct timeval now; + /* Abort if there aren't any manager sessions */ + if (!num_sessions) + return 0; + + ast_build_string(&tmp_next, &tmp_left, "Event: %s\r\nPrivilege: %s\r\n", + event, authority_to_str(category, auth, sizeof(auth))); + va_start(ap, fmt); + ast_build_string_va(&tmp_next, &tmp_left, fmt, ap); + va_end(ap); + *tmp_next++ = '\r'; + *tmp_next++ = '\n'; + *tmp_next = '\0'; + ast_mutex_lock(&sessionlock); + /* Append even to master list and wake up any sleeping sessions */ + append_event(tmp, category); for (s = sessions; s; s = s->next) { - if ((s->readperm & category) != category) - continue; - - if ((s->send_events & category) != category) - continue; - - if (ast_strlen_zero(tmp)) { - ast_build_string(&tmp_next, &tmp_left, "Event: %s\r\nPrivilege: %s\r\n", - event, authority_to_str(category, auth, sizeof(auth))); - va_start(ap, fmt); - ast_build_string_va(&tmp_next, &tmp_left, fmt, ap); - va_end(ap); - *tmp_next++ = '\r'; - *tmp_next++ = '\n'; - *tmp_next = '\0'; - } - ast_mutex_lock(&s->__lock); - if (s->busy) { - append_event(s, tmp); - } else if (!s->dead) { - if (ast_carefulwrite(s->fd, tmp, tmp_next - tmp, s->writetimeout) < 0) { - ast_log(LOG_WARNING, "Disconnecting slow (or gone) manager session!\n"); - s->dead = 1; - pthread_kill(s->t, SIGURG); - } - } + if (s->waiting_thread != AST_PTHREADT_NULL) + pthread_kill(s->waiting_thread, SIGURG); ast_mutex_unlock(&s->__lock); } ast_mutex_unlock(&sessionlock); @@ -1668,8 +1761,11 @@ ast_cli_register(&show_mancmd_cli); ast_cli_register(&show_mancmds_cli); ast_cli_register(&show_manconn_cli); + ast_cli_register(&show_maneventq_cli); ast_extension_state_add(NULL, NULL, manager_state_cb, NULL); registered = 1; + /* Append placeholder event so master_eventq never runs dry */ + append_event("Event: Placeholder\r\n\r\n", 0); } portno = DEFAULT_MANAGER_PORT; displayconnects = 1; @@ -1703,8 +1799,7 @@ if ((val = ast_variable_retrieve(cfg, "general", "displayconnects"))) { displayconnects = ast_true(val);; } - - + ba.sin_family = AF_INET; ba.sin_port = htons(portno); memset(&ba.sin_addr, 0, sizeof(ba.sin_addr)); Index: include/asterisk/manager.h =================================================================== --- include/asterisk/manager.h (revision 809) +++ include/asterisk/manager.h (working copy) @@ -60,6 +60,9 @@ #define AST_MAX_MANHEADER_LEN 256 struct eventqent { + int usecount; + int category; + ast_mutex_t lock; struct eventqent *next; char eventdata[1]; }; @@ -77,6 +80,8 @@ int busy; /*! Whether or not we're "dead" */ int dead; + /*! Whether a manager session has someone waiting on events */ + pthread_t waiting_thread; /*! Logged in username */ char username[80]; /*! Authentication challenge */