00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 #include "asterisk.h"
00041
00042 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 328209 $");
00043
00044 #include <stdlib.h>
00045 #include <stdio.h>
00046 #include <string.h>
00047 #include <unistd.h>
00048 #include <errno.h>
00049 #include <signal.h>
00050 #include <pthread.h>
00051
00052 #include "ais/ais.h"
00053
00054 #include "asterisk/module.h"
00055 #include "asterisk/options.h"
00056 #include "asterisk/logger.h"
00057 #include "asterisk/channel.h"
00058 #include "asterisk/utils.h"
00059 #include "asterisk/cli.h"
00060
00061 static struct {
00062 pthread_t id;
00063 unsigned int stop:1;
00064 } dispatch_thread = {
00065 .id = AST_PTHREADT_NULL,
00066 };
00067
00068 SaVersionT ais_version = { 'B', 1, 1 };
00069
00070 static const struct ais_error {
00071 SaAisErrorT error;
00072 const char *desc;
00073 } ais_errors[] = {
00074 { SA_AIS_OK, "OK" },
00075 { SA_AIS_ERR_LIBRARY, "Library Error" },
00076 { SA_AIS_ERR_VERSION, "Version Not Compatible" },
00077 { SA_AIS_ERR_INIT, "Callback Not Registered" },
00078 { SA_AIS_ERR_TIMEOUT, "Timeout" },
00079 { SA_AIS_ERR_TRY_AGAIN , "Try Again" },
00080 { SA_AIS_ERR_INVALID_PARAM, "Invalid Parameter" },
00081 { SA_AIS_ERR_NO_MEMORY, "No Memory" },
00082 { SA_AIS_ERR_BAD_HANDLE, "Invalid Handle" },
00083 { SA_AIS_ERR_BUSY, "Resource Already In Use" },
00084 { SA_AIS_ERR_ACCESS, "Access Denied" },
00085 { SA_AIS_ERR_NOT_EXIST, "Does Not Exist" },
00086 { SA_AIS_ERR_NAME_TOO_LONG, "Name Too Long" },
00087 { SA_AIS_ERR_EXIST, "Already Exists" },
00088 { SA_AIS_ERR_NO_SPACE, "Buffer Too Small" },
00089 { SA_AIS_ERR_INTERRUPT, "Request Interrupted" },
00090 { SA_AIS_ERR_NAME_NOT_FOUND, "Name Not Found" },
00091 { SA_AIS_ERR_NO_RESOURCES, "Not Enough Resources" },
00092 { SA_AIS_ERR_NOT_SUPPORTED, "Requested Function Not Supported" },
00093 { SA_AIS_ERR_BAD_OPERATION, "Operation Not Allowed" },
00094 { SA_AIS_ERR_FAILED_OPERATION, "Operation Failed" },
00095 { SA_AIS_ERR_MESSAGE_ERROR, "Communication Error" },
00096 { SA_AIS_ERR_QUEUE_FULL, "Destination Queue Full" },
00097 { SA_AIS_ERR_QUEUE_NOT_AVAILABLE, "Destination Queue Not Available" },
00098 { SA_AIS_ERR_BAD_FLAGS, "Invalid Flags" },
00099 { SA_AIS_ERR_TOO_BIG, "Value Too Large" },
00100 { SA_AIS_ERR_NO_SECTIONS, "No More Sections to Initialize" },
00101 };
00102
00103 const char *ais_err2str(SaAisErrorT error)
00104 {
00105 int x;
00106
00107 for (x = 0; x < ARRAY_LEN(ais_errors); x++) {
00108 if (ais_errors[x].error == error)
00109 return ais_errors[x].desc;
00110 }
00111
00112 return "Unknown";
00113 }
00114
00115 static void *dispatch_thread_handler(void *data)
00116 {
00117 SaSelectionObjectT clm_fd, evt_fd;
00118 int res;
00119 struct pollfd pfd[2] = { { .events = POLLIN, }, { .events = POLLIN, } };
00120 SaAisErrorT ais_res;
00121
00122 ais_res = saClmSelectionObjectGet(clm_handle, &clm_fd);
00123 if (ais_res != SA_AIS_OK) {
00124 ast_log(LOG_ERROR, "Failed to retrieve select fd for CLM service. "
00125 "This module will not operate.\n");
00126 return NULL;
00127 }
00128
00129 ais_res = saEvtSelectionObjectGet(evt_handle, &evt_fd);
00130 if (ais_res != SA_AIS_OK) {
00131 ast_log(LOG_ERROR, "Failed to retrieve select fd for EVT service. "
00132 "This module will not operate.\n");
00133 return NULL;
00134 }
00135
00136 pfd[0].fd = clm_fd;
00137 pfd[1].fd = evt_fd;
00138
00139 while (!dispatch_thread.stop) {
00140 pfd[0].revents = 0;
00141 pfd[1].revents = 0;
00142
00143 res = ast_poll(pfd, 2, -1);
00144 if (res == -1 && errno != EINTR && errno != EAGAIN) {
00145 ast_log(LOG_ERROR, "Select error (%s) dispatch thread going away now, "
00146 "and the module will no longer operate.\n", strerror(errno));
00147 break;
00148 }
00149
00150 if (pfd[0].revents & POLLIN) {
00151 saClmDispatch(clm_handle, SA_DISPATCH_ALL);
00152 }
00153 if (pfd[1].revents & POLLIN) {
00154 saEvtDispatch(evt_handle, SA_DISPATCH_ALL);
00155 }
00156 }
00157
00158 return NULL;
00159 }
00160
00161 static int load_module(void)
00162 {
00163 if (ast_ais_clm_load_module())
00164 goto return_error;
00165
00166 if (ast_ais_evt_load_module())
00167 goto evt_failed;
00168
00169 if (ast_pthread_create_background(&dispatch_thread.id, NULL,
00170 dispatch_thread_handler, NULL)) {
00171 ast_log(LOG_ERROR, "Error starting AIS dispatch thread.\n");
00172 goto dispatch_failed;
00173 }
00174
00175 return AST_MODULE_LOAD_SUCCESS;
00176
00177 dispatch_failed:
00178 ast_ais_evt_unload_module();
00179 evt_failed:
00180 ast_ais_clm_unload_module();
00181 return_error:
00182 return AST_MODULE_LOAD_DECLINE;
00183 }
00184
00185 static int unload_module(void)
00186 {
00187 ast_ais_clm_unload_module();
00188 ast_ais_evt_unload_module();
00189
00190 if (dispatch_thread.id != AST_PTHREADT_NULL) {
00191 dispatch_thread.stop = 1;
00192 pthread_kill(dispatch_thread.id, SIGURG);
00193 pthread_join(dispatch_thread.id, NULL);
00194 }
00195
00196 return 0;
00197 }
00198
00199 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "SAForum AIS");