| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
#include "asterisk.h" |
| 34 |
|
| 35 |
#include <sys/types.h> |
| 36 |
#include <dirent.h> |
| 37 |
#ifndef __USE_GNU |
| 38 |
#define __USE_GNU 1 |
| 39 |
#endif |
| 40 |
#include <locale.h> |
| 41 |
|
| 42 |
#include "asterisk/cli.h" |
| 43 |
#include "asterisk/linkedlists.h" |
| 44 |
#include "asterisk/localtime.h" |
| 45 |
#include "asterisk/utils.h" |
| 46 |
#include "asterisk/module.h" |
| 47 |
|
| 48 |
|
| 49 |
static char *handle_cli_test_locales(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 50 |
{ |
| 51 |
DIR *localedir; |
| 52 |
struct dirent *dent; |
| 53 |
struct ast_tm atm; |
| 54 |
struct timeval tv; |
| 55 |
const char *orig_locale; |
| 56 |
char origlocalformat[200] = "", localformat[200] = ""; |
| 57 |
struct test_locales { |
| 58 |
AST_LIST_ENTRY(test_locales) list; |
| 59 |
char *localformat; |
| 60 |
char name[0]; |
| 61 |
} *tl = NULL; |
| 62 |
AST_LIST_HEAD_NOLOCK(locales, test_locales) locales; |
| 63 |
int varies = 0, all_successful = 1, count = 0, count_fail = 0; |
| 64 |
|
| 65 |
switch (cmd) { |
| 66 |
case CLI_INIT: |
| 67 |
e->command = "test locale"; |
| 68 |
e->usage = "" |
| 69 |
"Usage: test locale\n" |
| 70 |
" Test thread safety of locale functions.\n"; |
| 71 |
return NULL; |
| 72 |
case CLI_GENERATE: |
| 73 |
return NULL; |
| 74 |
} |
| 75 |
|
| 76 |
if (a->argc != e->args) { |
| 77 |
return CLI_SHOWUSAGE; |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
if (!(localedir = opendir( |
| 82 |
#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined( __NetBSD__ ) || defined(__APPLE__) |
| 83 |
"/usr/share/locale" |
| 84 |
#else |
| 85 |
"/usr/lib/locale" |
| 86 |
#endif |
| 87 |
))) { |
| 88 |
ast_cli(a->fd, "No locales seem to exist on this platform.\n"); |
| 89 |
return CLI_SUCCESS; |
| 90 |
} |
| 91 |
|
| 92 |
tv = ast_tvnow(); |
| 93 |
ast_localtime(&tv, &atm, NULL); |
| 94 |
orig_locale = setlocale(LC_ALL, NULL); |
| 95 |
AST_LIST_HEAD_SET_NOLOCK(&locales, NULL); |
| 96 |
|
| 97 |
|
| 98 |
ast_strftime(origlocalformat, sizeof(origlocalformat), "%c", &atm); |
| 99 |
|
| 100 |
while ((dent = readdir(localedir))) { |
| 101 |
size_t locallen; |
| 102 |
size_t namelen; |
| 103 |
|
| 104 |
if (dent->d_name[0] == '.') { |
| 105 |
continue; |
| 106 |
} |
| 107 |
|
| 108 |
setlocale(LC_ALL, dent->d_name); |
| 109 |
ast_strftime(localformat, sizeof(localformat), "%c", &atm); |
| 110 |
|
| 111 |
locallen = strlen(localformat) + 1; |
| 112 |
namelen = strlen(dent->d_name) + 1; |
| 113 |
|
| 114 |
|
| 115 |
if (!(tl = ast_calloc(1, sizeof(*tl) + locallen + namelen))) { |
| 116 |
continue; |
| 117 |
} |
| 118 |
|
| 119 |
ast_copy_string(tl->name, dent->d_name, namelen); |
| 120 |
tl->localformat = tl->name + namelen; |
| 121 |
ast_copy_string(tl->localformat, localformat, locallen); |
| 122 |
|
| 123 |
AST_LIST_INSERT_TAIL(&locales, tl, list); |
| 124 |
|
| 125 |
|
| 126 |
if (!varies && strcmp(AST_LIST_FIRST(&locales)->localformat, localformat)) { |
| 127 |
varies = 1; |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
setlocale(LC_ALL, orig_locale); |
| 132 |
|
| 133 |
closedir(localedir); |
| 134 |
|
| 135 |
if (!varies) { |
| 136 |
if (!strcmp(origlocalformat, localformat)) { |
| 137 |
ast_cli(a->fd, "WARNING: the locales on your system don't differ. Install more locales if you want this test to mean something.\n"); |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
orig_locale = ast_setlocale(AST_LIST_FIRST(&locales)->name); |
| 142 |
|
| 143 |
while ((tl = AST_LIST_REMOVE_HEAD(&locales, list))) { |
| 144 |
ast_setlocale(tl->name); |
| 145 |
ast_strftime(localformat, sizeof(localformat), "%c", &atm); |
| 146 |
if (strcmp(localformat, tl->localformat)) { |
| 147 |
ast_cli(a->fd, "WARNING: locale test fails for locale %s\n", tl->name); |
| 148 |
all_successful = 0; |
| 149 |
count_fail++; |
| 150 |
} |
| 151 |
ast_free(tl); |
| 152 |
count++; |
| 153 |
} |
| 154 |
|
| 155 |
ast_setlocale(orig_locale); |
| 156 |
|
| 157 |
if (all_successful) { |
| 158 |
ast_cli(a->fd, "All %d locale tests successful\n", count); |
| 159 |
} else if (count_fail == count && count > 0) { |
| 160 |
ast_cli(a->fd, "No locale tests successful out of %d tries\n", count); |
| 161 |
} else if (count > 0) { |
| 162 |
ast_cli(a->fd, "Partial failure (%d/%d) for a %.0f%% failure rate\n", count_fail, count, count_fail * 100.0 / count); |
| 163 |
} else { |
| 164 |
ast_cli(a->fd, "No locales tested. Install more locales.\n"); |
| 165 |
} |
| 166 |
|
| 167 |
return CLI_SUCCESS; |
| 168 |
} |
| 169 |
|
| 170 |
static struct ast_cli_entry cli_locales[] = { |
| 171 |
AST_CLI_DEFINE(handle_cli_test_locales, "Test locales for thread-safety"), |
| 172 |
}; |
| 173 |
|
| 174 |
static int unload_module(void) |
| 175 |
{ |
| 176 |
ast_cli_unregister_multiple(cli_locales, ARRAY_LEN(cli_locales)); |
| 177 |
return 0; |
| 178 |
} |
| 179 |
|
| 180 |
static int load_module(void) |
| 181 |
{ |
| 182 |
ast_cli_register_multiple(cli_locales, ARRAY_LEN(cli_locales)); |
| 183 |
return AST_MODULE_LOAD_SUCCESS; |
| 184 |
} |
| 185 |
|
| 186 |
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Locale tests"); |