[5336] | 1 | //roarpluginrunner.c: |
---|
| 2 | |
---|
| 3 | /* |
---|
[5381] | 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2011-2012 |
---|
[5336] | 5 | * |
---|
| 6 | * This file is part of roarclients a part of RoarAudio, |
---|
| 7 | * a cross-platform sound system for both, home and professional use. |
---|
| 8 | * See README for details. |
---|
| 9 | * |
---|
| 10 | * This file is free software; you can redistribute it and/or modify |
---|
| 11 | * it under the terms of the GNU General Public License version 3 |
---|
| 12 | * as published by the Free Software Foundation. |
---|
| 13 | * |
---|
| 14 | * RoarAudio is distributed in the hope that it will be useful, |
---|
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 17 | * GNU General Public License for more details. |
---|
| 18 | * |
---|
| 19 | * You should have received a copy of the GNU General Public License |
---|
| 20 | * along with this software; see the file COPYING. If not, write to |
---|
| 21 | * the Free Software Foundation, 51 Franklin Street, Fifth Floor, |
---|
| 22 | * Boston, MA 02110-1301, USA. |
---|
| 23 | * |
---|
| 24 | */ |
---|
| 25 | |
---|
| 26 | int g_verbose = 0; |
---|
| 27 | #define ROAR_DBG_INFOVAR g_verbose |
---|
| 28 | |
---|
| 29 | #include <roaraudio.h> |
---|
| 30 | |
---|
| 31 | enum action { |
---|
| 32 | RUN, |
---|
[5484] | 33 | RUN_AS_APPLICATION, |
---|
[5336] | 34 | EXPLAIN |
---|
| 35 | }; |
---|
| 36 | |
---|
[5712] | 37 | static struct roar_dl_librarypara * g_para = NULL; |
---|
| 38 | static struct roar_scheduler * g_sched = NULL; |
---|
| 39 | static struct roar_scheduler_source g_s_service = {.type = ROAR_SCHEDULER_CPI_SERVICE}; |
---|
[5336] | 40 | |
---|
[5354] | 41 | static void usage (const char * progname) { |
---|
[5336] | 42 | fprintf(stderr, "Usage: %s [OPTIONS]... PLUGIN\n", progname); |
---|
| 43 | |
---|
| 44 | fprintf(stderr, "\nOptions:\n\n"); |
---|
| 45 | |
---|
[5484] | 46 | fprintf(stderr, " -h --help - This help.\n" |
---|
| 47 | " -v --verbose - Be verbose. Can be used multiple times.\n" |
---|
| 48 | " --server SERVER - Set default server to SERVER.\n" |
---|
| 49 | " --run - Run plugin.\n" |
---|
| 50 | " --run-as-application - Same as --run except all tailing arguments are\n" |
---|
| 51 | " passed to the plugin.\n" |
---|
| 52 | " --explain - Explain plugin.\n" |
---|
| 53 | " --appname NAME - Sets the appname.\n" |
---|
| 54 | " --abiversion ABI - Set the ABI version.\n" |
---|
| 55 | " --args ARGS - Set plugin arguments.\n" |
---|
[5353] | 56 | ); |
---|
[5715] | 57 | |
---|
| 58 | fprintf(stderr, "\nCPI Options:\n\n"); |
---|
| 59 | fprintf(stderr, " -t --tcp - Use TCP listen socket\n" |
---|
| 60 | " -u --unix - Use UNIX Domain listen socket (default)\n" |
---|
| 61 | " -n --decnet - use DECnet listen socket\n" |
---|
| 62 | " --port PORT - TCP Port to bind to\n" |
---|
| 63 | " --bind ADDR - Node/Hostname/Path to bind to\n" |
---|
| 64 | " --proto PROTO - Use PROTO as protocol on Socket\n" |
---|
| 65 | " --new-sock - Parameters for new socket follow\n" |
---|
| 66 | " --client-fh FH - Comunicate with a client over this handle\n" |
---|
| 67 | "\n" |
---|
| 68 | ); |
---|
[5336] | 69 | } |
---|
| 70 | |
---|
| 71 | static int do_run(const char * name) { |
---|
[5712] | 72 | struct roar_scheduler_source s_container; |
---|
[5442] | 73 | struct roar_plugincontainer * cont = roar_plugincontainer_new(g_para); |
---|
| 74 | int err; |
---|
[5336] | 75 | |
---|
[5442] | 76 | if ( cont == NULL ) |
---|
[5336] | 77 | return -1; |
---|
| 78 | |
---|
[5712] | 79 | memset(&s_container, 0, sizeof(s_container)); |
---|
| 80 | s_container.type = ROAR_SCHEDULER_PLUGINCONTAINER; |
---|
| 81 | s_container.handle.container = cont; |
---|
| 82 | roar_scheduler_source_add(g_sched, &s_container); |
---|
| 83 | |
---|
[5442] | 84 | if ( roar_plugincontainer_load(cont, name, NULL) == -1 ) { |
---|
| 85 | err = roar_error; |
---|
[5715] | 86 | roar_scheduler_source_del(g_sched, &s_container); |
---|
[5442] | 87 | roar_plugincontainer_unref(cont); |
---|
| 88 | roar_error = err; |
---|
| 89 | return -1; |
---|
| 90 | } |
---|
[5336] | 91 | |
---|
[5442] | 92 | roar_plugincontainer_appsched_trigger(cont, ROAR_DL_APPSCHED_INIT); |
---|
| 93 | |
---|
[5712] | 94 | roar_scheduler_run(g_sched); |
---|
[5336] | 95 | |
---|
[5442] | 96 | roar_plugincontainer_appsched_trigger(cont, ROAR_DL_APPSCHED_UPDATE); |
---|
| 97 | roar_plugincontainer_appsched_trigger(cont, ROAR_DL_APPSCHED_FREE); |
---|
| 98 | |
---|
[5712] | 99 | roar_scheduler_source_del(g_sched, &s_container); |
---|
[5442] | 100 | roar_plugincontainer_unref(cont); |
---|
[5336] | 101 | return 0; |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | static const char * _ptr2str(const void * p) { |
---|
[5517] | 105 | #if defined(ROAR_HAVE_H_DLFCN) && defined(ROAR_HAVE_DLADDR) |
---|
[5353] | 106 | static char buf[80]; |
---|
| 107 | Dl_info info; |
---|
| 108 | #else |
---|
[5336] | 109 | static char buf[24]; |
---|
[5353] | 110 | #endif |
---|
[5336] | 111 | |
---|
| 112 | if ( p == NULL ) |
---|
| 113 | return "<not set>"; |
---|
| 114 | |
---|
[5517] | 115 | #if defined(ROAR_HAVE_H_DLFCN) && defined(ROAR_HAVE_DLADDR) |
---|
[5353] | 116 | if ( dladdr(p, &info) != 0 ) { |
---|
| 117 | if ( p == info.dli_saddr ) { |
---|
| 118 | snprintf(buf, sizeof(buf), "%p <%s in \"%s\">", p, info.dli_sname, info.dli_fname); |
---|
| 119 | return buf; |
---|
| 120 | } |
---|
| 121 | } |
---|
| 122 | #endif |
---|
| 123 | |
---|
[5336] | 124 | snprintf(buf, sizeof(buf), "%p", p); |
---|
| 125 | |
---|
| 126 | return buf; |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | static const char * _ptrrange2str(const void * p, size_t len) { |
---|
[5353] | 130 | static char buf[80]; |
---|
[5336] | 131 | |
---|
| 132 | if ( p == NULL ) |
---|
| 133 | return "<not set>"; |
---|
| 134 | |
---|
| 135 | if ( len == 0 ) |
---|
| 136 | return _ptr2str(p); |
---|
| 137 | |
---|
| 138 | snprintf(buf, sizeof(buf), "%p-%p", p, p + len); |
---|
| 139 | |
---|
| 140 | return buf; |
---|
| 141 | } |
---|
| 142 | |
---|
| 143 | static int do_explain(const char * name) { |
---|
[5427] | 144 | struct roar_dl_lhandle * lhandle = roar_dl_open(name, ROAR_DL_FLAG_LAZY|ROAR_DL_FLAG_PLUGINPATH, 0, g_para); |
---|
[5336] | 145 | struct roar_dl_libraryinst * (*func)(struct roar_dl_librarypara * para); |
---|
| 146 | struct roar_dl_libraryinst * lib; |
---|
[5347] | 147 | int libok = 0, libnameok = 0, libdepok = 0; |
---|
[5336] | 148 | int tmp; |
---|
| 149 | int i; |
---|
[5347] | 150 | size_t iter; |
---|
| 151 | char c; |
---|
[5336] | 152 | |
---|
| 153 | if ( lhandle == NULL ) |
---|
| 154 | return -1; |
---|
| 155 | |
---|
| 156 | func = roar_dl_getsym(lhandle, "_roaraudio_library_init", -1); |
---|
| 157 | if ( func == NULL ) { |
---|
| 158 | fprintf(stderr, "Warning: Not a RA lib: %s\n", name); |
---|
| 159 | roar_dl_unref(lhandle); |
---|
| 160 | return 0; |
---|
| 161 | } |
---|
| 162 | |
---|
| 163 | lib = func(g_para); |
---|
| 164 | if ( lib == NULL ) { |
---|
[5353] | 165 | fprintf(stderr, "Warning: Can not RA init: %s: %s\n", name, roar_error2str(roar_error)); |
---|
[5336] | 166 | roar_dl_unref(lhandle); |
---|
| 167 | return 0; |
---|
| 168 | } |
---|
| 169 | |
---|
| 170 | if ( lib->version == ROAR_DL_LIBINST_VERSION && lib->len == sizeof(*lib) ) { |
---|
| 171 | libok = 1; |
---|
| 172 | } |
---|
| 173 | |
---|
| 174 | printf("lib = %s\n", _ptr2str(lib)); |
---|
[5353] | 175 | if ( g_verbose || !libok ) { |
---|
| 176 | printf("|-> version = %i (%smatch)\n", lib->version, lib->version == ROAR_DL_LIBINST_VERSION ? "" : "no "); |
---|
| 177 | printf("%c-> len = %zu (%smatch)\n", libok ? '|' : '\\', lib->len, lib->len == sizeof(*lib) ? "" : "no "); |
---|
| 178 | } |
---|
[5336] | 179 | |
---|
| 180 | if ( libok ) { |
---|
[5353] | 181 | if ( g_verbose || lib->unload != NULL ) |
---|
| 182 | printf("|-> unload = %s\n", _ptr2str(lib->unload)); |
---|
[5336] | 183 | printf("|-> func = {"); |
---|
[5346] | 184 | i = 0; |
---|
| 185 | while (i < ROAR_DL_FN_MAX) { |
---|
| 186 | for (tmp = 0; i < ROAR_DL_FN_MAX; i++) { |
---|
| 187 | if ( lib->func[i] != NULL ) { |
---|
| 188 | break; |
---|
| 189 | } |
---|
| 190 | tmp++; |
---|
| 191 | } |
---|
| 192 | if (tmp) |
---|
| 193 | printf("%i x <not set>%s", tmp, i < (ROAR_DL_FN_MAX-1) ? ", " : ""); |
---|
| 194 | |
---|
| 195 | if ( i < ROAR_DL_FN_MAX ) { |
---|
| 196 | printf("[%i] = %s%s", i, _ptr2str(lib->func[i]), i < (ROAR_DL_FN_MAX-1) ? ", " : ""); |
---|
| 197 | i++; |
---|
[5336] | 198 | } |
---|
| 199 | } |
---|
[5346] | 200 | |
---|
[5336] | 201 | printf("}\n"); |
---|
| 202 | printf("|-> libname = %s\n", _ptr2str(lib->libname)); |
---|
| 203 | if ( lib->libname != NULL ) { |
---|
| 204 | if ( lib->libname->version == ROAR_DL_LIBNAME_VERSION && lib->libname->len == sizeof(*(lib->libname)) ) { |
---|
| 205 | libnameok = 1; |
---|
| 206 | } |
---|
[5353] | 207 | if ( g_verbose || !libnameok ) { |
---|
| 208 | printf("| |-> version = %i (%smatch)\n", lib->libname->version, |
---|
| 209 | lib->libname->version == ROAR_DL_LIBNAME_VERSION ? "" : "no "); |
---|
| 210 | printf("| %c-> len = %zu (%smatch)\n", libnameok ? '|' : '\\', lib->libname->len, |
---|
| 211 | lib->libname->len == sizeof(*(lib->libname)) ? "" : "no "); |
---|
| 212 | } |
---|
| 213 | |
---|
[5336] | 214 | if ( libnameok ) { |
---|
[5347] | 215 | #define _ps(k,name) \ |
---|
[5336] | 216 | tmp = (lib->libname->name) != NULL; \ |
---|
[5353] | 217 | if ( tmp || g_verbose || (k) == '\\' ) \ |
---|
[5347] | 218 | printf("| %c-> %-15s = %s%s%s\n", (k), #name, tmp ? "\"" : "", \ |
---|
[5336] | 219 | tmp ? (lib->libname->name) : "<not set>", tmp ? "\"" : ""); |
---|
| 220 | |
---|
| 221 | _ps('|', name); |
---|
| 222 | _ps('|', libname); |
---|
| 223 | _ps('|', libversion); |
---|
| 224 | _ps('|', abiversion); |
---|
| 225 | _ps('|', description); |
---|
| 226 | _ps('|', contact); |
---|
| 227 | _ps('|', authors); |
---|
| 228 | _ps('\\', license); |
---|
| 229 | #undef _ps |
---|
| 230 | } |
---|
| 231 | } |
---|
[5353] | 232 | if ( g_verbose || lib->global_data_pointer != NULL ) |
---|
| 233 | printf("|-> global_data_len = %zu\n", lib->global_data_len); |
---|
| 234 | if ( g_verbose || lib->global_data_init != NULL ) |
---|
| 235 | printf("|-> global_data_init = %s\n", _ptrrange2str(lib->global_data_init, lib->global_data_len)); |
---|
| 236 | if ( g_verbose || lib->global_data_pointer != NULL ) |
---|
| 237 | printf("|-> global_data_pointer = %s\n", _ptr2str(lib->global_data_pointer)); |
---|
| 238 | |
---|
[5347] | 239 | if ( lib->libdep != NULL && lib->libdep_len ) { |
---|
| 240 | printf("|-> libdep = %s\n", _ptr2str(lib->libdep)); |
---|
| 241 | for (iter = 0; iter < lib->libdep_len; iter++) { |
---|
| 242 | printf("| %c-> Table entry %zu = %p\n", iter == (lib->libdep_len-1) ? '\\' : '|', iter, &(lib->libdep[iter])); |
---|
| 243 | c = iter == (lib->libdep_len-1) ? ' ' : '|'; |
---|
| 244 | if ( lib->libdep[iter].version == ROAR_DL_LIBDEP_VERSION && |
---|
| 245 | lib->libdep[iter].len == sizeof(struct roar_dl_librarydep) ) { |
---|
| 246 | libdepok = 1; |
---|
| 247 | } else { |
---|
| 248 | libdepok = 0; |
---|
| 249 | } |
---|
[5353] | 250 | if ( g_verbose || !libdepok ) { |
---|
| 251 | printf("| %c |-> version = %i (%smatch)\n", c, lib->libdep[iter].version, |
---|
| 252 | lib->libdep[iter].version == ROAR_DL_LIBDEP_VERSION ? "" : "no "); |
---|
| 253 | printf("| %c %c-> len = %zu (%smatch)\n", c, libdepok ? '|' : '\\', |
---|
| 254 | lib->libdep[iter].len, |
---|
| 255 | lib->libdep[iter].len == sizeof(struct roar_dl_librarydep) ? |
---|
| 256 | "" : "no "); |
---|
| 257 | } |
---|
| 258 | |
---|
[5347] | 259 | if ( libdepok ) { |
---|
| 260 | printf("| %c |-> flags = 0x%.8lX\n", c, (unsigned long int)lib->libdep[iter].flags); |
---|
| 261 | #define _ps(k,name) \ |
---|
| 262 | tmp = (lib->libdep[iter].name) != NULL; \ |
---|
[5353] | 263 | if ( tmp || g_verbose || (k) == '\\' ) \ |
---|
[5347] | 264 | printf("| %c %c-> %-11s = %s%s%s\n", c, (k), #name, tmp ? "\"" : "", \ |
---|
| 265 | tmp ? (lib->libdep[iter].name) : "<not set>", tmp ? "\"" : ""); |
---|
| 266 | _ps('|', name); |
---|
| 267 | _ps('|', libname); |
---|
| 268 | _ps('\\', abiversion); |
---|
| 269 | #undef _ps |
---|
| 270 | } |
---|
| 271 | } |
---|
| 272 | printf("|-> libdep_len = %zu\n", lib->libdep_len); |
---|
| 273 | } else if ( (lib->libdep == NULL && lib->libdep_len) || (lib->libdep != NULL && !lib->libdep_len) ) { |
---|
| 274 | printf("|-> libdep = %s (invalid)\n", _ptr2str(lib->libdep)); |
---|
| 275 | printf("|-> libdep_len = %zu (invalid)\n", lib->libdep_len); |
---|
| 276 | } |
---|
[5353] | 277 | if ( g_verbose || lib->appsched != NULL ) { |
---|
| 278 | printf("|-> appsched = %s\n", _ptr2str(lib->appsched)); |
---|
| 279 | if ( lib->appsched != NULL ) { |
---|
[5355] | 280 | if ( g_verbose || lib->appsched->init != NULL ) |
---|
[5353] | 281 | printf("| |-> init = %s\n", _ptr2str(lib->appsched->init)); |
---|
[5355] | 282 | if ( g_verbose || lib->appsched->free != NULL ) |
---|
[5353] | 283 | printf("| |-> free = %s\n", _ptr2str(lib->appsched->free)); |
---|
[5355] | 284 | if ( g_verbose || lib->appsched->update != NULL ) |
---|
[5353] | 285 | printf("| |-> update = %s\n", _ptr2str(lib->appsched->update)); |
---|
[5355] | 286 | if ( g_verbose || lib->appsched->tick != NULL ) |
---|
[5353] | 287 | printf("| |-> tick = %s\n", _ptr2str(lib->appsched->tick)); |
---|
| 288 | printf("| \\-> wait = %s\n", _ptr2str(lib->appsched->wait)); |
---|
| 289 | } |
---|
[5336] | 290 | } |
---|
[5353] | 291 | #define _ps(k,name) \ |
---|
| 292 | tmp = (lib->name) != NULL; \ |
---|
| 293 | if ( tmp || g_verbose || (k) == '\\' ) \ |
---|
| 294 | printf("%c-> %-19s = %s%s%s\n", (k), #name, tmp ? "\"" : "", \ |
---|
| 295 | tmp ? (lib->name) : "<not set>", tmp ? "\"" : ""); |
---|
| 296 | _ps('|', host_appname); |
---|
| 297 | _ps('\\', host_abiversion); |
---|
| 298 | #undef _ps |
---|
[5336] | 299 | } |
---|
| 300 | |
---|
| 301 | roar_dl_unref(lhandle); |
---|
| 302 | return 0; |
---|
| 303 | } |
---|
| 304 | |
---|
| 305 | static int do_plugin(enum action action, const char * name) { |
---|
| 306 | switch (action) { |
---|
| 307 | case EXPLAIN: |
---|
| 308 | return do_explain(name); |
---|
| 309 | break; |
---|
| 310 | case RUN: |
---|
[5484] | 311 | case RUN_AS_APPLICATION: |
---|
[5336] | 312 | return do_run(name); |
---|
| 313 | break; |
---|
| 314 | default: |
---|
| 315 | roar_err_set(ROAR_ERROR_BADRQC); |
---|
| 316 | return -1; |
---|
| 317 | break; |
---|
| 318 | } |
---|
| 319 | } |
---|
| 320 | |
---|
| 321 | static inline void _clear_para(void) { |
---|
[5341] | 322 | if ( g_para == NULL ) |
---|
| 323 | return; |
---|
| 324 | |
---|
| 325 | roar_dl_para_unref(g_para); |
---|
[5336] | 326 | g_para = NULL; |
---|
| 327 | } |
---|
| 328 | |
---|
[5484] | 329 | static inline int _add_para(struct roar_dl_librarypara * para, const char * pluginargs, size_t argc, char * argv[]) { |
---|
| 330 | struct roar_keyval * kv; |
---|
| 331 | ssize_t argslen, argvlen; |
---|
| 332 | ssize_t arglen; |
---|
| 333 | ssize_t pluginargc; |
---|
| 334 | size_t argv_phys = argc; |
---|
| 335 | size_t i; |
---|
| 336 | int error; |
---|
| 337 | char * sp, * c; |
---|
| 338 | int after_parser_end; |
---|
| 339 | |
---|
| 340 | if ( pluginargs == NULL ) |
---|
| 341 | pluginargs = ""; |
---|
| 342 | |
---|
| 343 | argslen = roar_mm_strlen(pluginargs) + 1 /* tailing '\0' */; |
---|
| 344 | |
---|
| 345 | after_parser_end = 0; |
---|
| 346 | argvlen = 0; |
---|
| 347 | for (i = 0; i < argv_phys; i++) { |
---|
| 348 | arglen = roar_mm_strlen(argv[i]); |
---|
| 349 | argvlen += arglen; |
---|
| 350 | if ( !after_parser_end ) { |
---|
| 351 | if ( !strcmp(argv[i], "--") ) { |
---|
| 352 | after_parser_end = 1; |
---|
| 353 | argc--; |
---|
| 354 | } else if ( arglen > 1 && argv[i][0] == '-' && !(arglen > 2 && argv[i][1] == '-') ) { |
---|
| 355 | argc += arglen - 2; |
---|
| 356 | } |
---|
| 357 | } |
---|
| 358 | } |
---|
| 359 | argvlen += argc; // the '\0's. |
---|
| 360 | |
---|
| 361 | para->args_store = roar_mm_malloc(argslen+argvlen); |
---|
| 362 | if ( para == NULL ) |
---|
| 363 | return -1; |
---|
| 364 | |
---|
| 365 | memcpy(para->args_store, pluginargs, argslen); |
---|
| 366 | |
---|
| 367 | pluginargc = roar_keyval_split(&kv, para->args_store, NULL, NULL, 1); |
---|
| 368 | if ( pluginargc == -1 ) { |
---|
| 369 | error = roar_error; |
---|
| 370 | roar_mm_free(para->args_store); |
---|
| 371 | para->args_store = NULL; |
---|
| 372 | roar_error = error; |
---|
| 373 | return -1; |
---|
| 374 | } |
---|
| 375 | |
---|
| 376 | para->argv = roar_mm_malloc((pluginargc+argc)*sizeof(struct roar_keyval)); |
---|
| 377 | if ( para->argv == NULL ) { |
---|
| 378 | error = roar_error; |
---|
| 379 | roar_mm_free(kv); |
---|
| 380 | roar_mm_free(para->args_store); |
---|
| 381 | para->args_store = NULL; |
---|
| 382 | roar_error = error; |
---|
| 383 | return -1; |
---|
| 384 | } |
---|
| 385 | |
---|
| 386 | para->argc = pluginargc + argc; |
---|
| 387 | |
---|
| 388 | memcpy(para->argv, kv, pluginargc*sizeof(struct roar_keyval)); |
---|
| 389 | roar_mm_free(kv); |
---|
| 390 | |
---|
| 391 | sp = para->args_store + argslen; |
---|
| 392 | kv = para->argv + pluginargc; |
---|
| 393 | |
---|
| 394 | after_parser_end = 0; |
---|
| 395 | for (i = 0; i < argv_phys; i++) { |
---|
| 396 | arglen = roar_mm_strlen(argv[i]) + 1; |
---|
| 397 | if ( after_parser_end || !(arglen > 2 && argv[i][0] == '-' && !(arglen > 3 && argv[i][1] == '-')) ) |
---|
| 398 | memcpy(sp, argv[i], arglen); |
---|
| 399 | |
---|
| 400 | if ( !after_parser_end && !strcmp(argv[i], "--") ) { |
---|
| 401 | after_parser_end = 1; |
---|
| 402 | continue; |
---|
| 403 | } else if ( !after_parser_end && arglen > 3 && sp[0] == '-' && sp[1] == '-' ) { |
---|
| 404 | kv->key = sp + 2; |
---|
| 405 | kv->value = NULL; |
---|
| 406 | for (c = sp + 2; *c; c++) { |
---|
| 407 | if (*c == '=') { |
---|
| 408 | *c = 0; |
---|
| 409 | c++; |
---|
| 410 | kv->value = c; |
---|
| 411 | break; |
---|
| 412 | } |
---|
| 413 | } |
---|
| 414 | } else if ( !after_parser_end && arglen > 2 && argv[i][0] == '-' ) { |
---|
| 415 | for (c = argv[i] + 1; *c; c++) { |
---|
| 416 | sp[0] = *c; |
---|
| 417 | sp[1] = 0; |
---|
| 418 | kv->key = sp; |
---|
| 419 | kv->value = NULL; |
---|
| 420 | sp += 2; |
---|
| 421 | kv++; |
---|
| 422 | } |
---|
| 423 | continue; |
---|
| 424 | } else { |
---|
| 425 | kv->key = NULL; |
---|
| 426 | kv->value = sp; |
---|
| 427 | } |
---|
| 428 | |
---|
| 429 | sp += arglen; |
---|
| 430 | kv++; |
---|
| 431 | } |
---|
| 432 | |
---|
| 433 | return 0; |
---|
| 434 | } |
---|
| 435 | |
---|
[5715] | 436 | static struct roar_scheduler_source * add_cpi_common(int proto) { |
---|
| 437 | struct roar_scheduler_source * source; |
---|
| 438 | int err; |
---|
| 439 | |
---|
| 440 | if ( proto == -1 ) { |
---|
| 441 | roar_err_set(ROAR_ERROR_INVAL); |
---|
| 442 | return NULL; |
---|
| 443 | } |
---|
| 444 | |
---|
| 445 | source = roar_mm_malloc(sizeof(struct roar_scheduler_source)); |
---|
| 446 | if ( source == NULL ) |
---|
| 447 | return NULL; |
---|
| 448 | |
---|
| 449 | memset(source, 0, sizeof(struct roar_scheduler_source)); |
---|
| 450 | |
---|
| 451 | source->vio = roar_mm_malloc(sizeof(struct roar_vio_calls)); |
---|
| 452 | if ( source->vio == NULL ) { |
---|
| 453 | err = roar_error; |
---|
| 454 | roar_mm_free(source); |
---|
| 455 | roar_error = err; |
---|
| 456 | return NULL; |
---|
| 457 | } |
---|
| 458 | |
---|
| 459 | source->flags = ROAR_SCHEDULER_FLAG_FREE; |
---|
| 460 | source->handle.cpi.proto = proto; |
---|
| 461 | |
---|
| 462 | return source; |
---|
| 463 | } |
---|
| 464 | |
---|
| 465 | static int add_cpi_listen(int proto, int type, const char * host, int port) { |
---|
| 466 | struct roar_scheduler_source * source = add_cpi_common(proto); |
---|
| 467 | int err; |
---|
| 468 | |
---|
| 469 | if ( source == NULL ) |
---|
| 470 | return -1; |
---|
| 471 | |
---|
| 472 | if ( roar_vio_open_socket_listen(source->vio, type, host, port) == -1 ) { |
---|
| 473 | err = roar_error; |
---|
| 474 | roar_mm_free(source->vio); |
---|
| 475 | roar_mm_free(source); |
---|
| 476 | roar_error = err; |
---|
| 477 | return -1; |
---|
| 478 | } |
---|
| 479 | |
---|
| 480 | source->type = ROAR_SCHEDULER_CPI_LISTEN; |
---|
| 481 | |
---|
| 482 | source->vio->flags |= ROAR_VIO_FLAGS_FREESELF; |
---|
| 483 | |
---|
| 484 | if ( roar_scheduler_source_add(g_sched, source) == -1 ) { |
---|
| 485 | err = roar_error; |
---|
| 486 | roar_vio_close(source->vio); |
---|
| 487 | roar_mm_free(source); |
---|
| 488 | roar_error = err; |
---|
| 489 | return -1; |
---|
| 490 | } |
---|
| 491 | |
---|
| 492 | return 0; |
---|
| 493 | } |
---|
| 494 | |
---|
| 495 | static int add_cpi_client(int proto, int fh) { |
---|
| 496 | struct roar_scheduler_source * source = add_cpi_common(proto); |
---|
| 497 | struct roar_dl_librarypara * para; |
---|
| 498 | int err; |
---|
| 499 | |
---|
| 500 | if ( source == NULL ) |
---|
| 501 | return -1; |
---|
| 502 | |
---|
| 503 | if ( roar_vio_open_fh(source->vio, fh) == -1 ) { |
---|
| 504 | err = roar_error; |
---|
| 505 | roar_mm_free(source->vio); |
---|
| 506 | roar_mm_free(source); |
---|
| 507 | roar_error = err; |
---|
| 508 | return -1; |
---|
| 509 | } |
---|
| 510 | |
---|
| 511 | source->type = ROAR_SCHEDULER_CPI_CLIENT; |
---|
| 512 | |
---|
| 513 | source->vio->flags |= ROAR_VIO_FLAGS_FREESELF; |
---|
| 514 | |
---|
| 515 | if ( roar_scheduler_source_add(g_sched, source) == -1 ) { |
---|
| 516 | err = roar_error; |
---|
| 517 | roar_vio_close(source->vio); |
---|
| 518 | roar_mm_free(source); |
---|
| 519 | roar_error = err; |
---|
| 520 | return -1; |
---|
| 521 | } |
---|
| 522 | |
---|
| 523 | if ( source->flags & ROAR_SCHEDULER_FLAG_STUB ) { |
---|
| 524 | ROAR_ERR("Can not handle proto STUB clients. Try to use --client-fh as last argument."); |
---|
| 525 | |
---|
| 526 | roar_scheduler_source_del(g_sched, source); |
---|
| 527 | |
---|
| 528 | return -1; |
---|
| 529 | } |
---|
| 530 | |
---|
| 531 | if ( source->handle.cpi.impl->set_proto != NULL ) { |
---|
| 532 | para = roar_dl_getpara(source->lhandle); |
---|
| 533 | |
---|
| 534 | if ( source->lhandle != NULL ) |
---|
| 535 | roar_dl_context_restore(source->lhandle); |
---|
| 536 | source->handle.cpi.impl->set_proto(source->handle.cpi.client, source->vio, &(source->handle.cpi.obuffer), &(source->handle.cpi.userdata), source->handle.cpi.protopara, source->handle.cpi.protoparalen, para); |
---|
| 537 | if ( source->lhandle != NULL ) |
---|
| 538 | roar_dl_context_store(source->lhandle); |
---|
| 539 | |
---|
| 540 | if ( para != NULL ) |
---|
| 541 | roar_dl_para_unref(para); |
---|
| 542 | } |
---|
| 543 | |
---|
| 544 | return 0; |
---|
| 545 | } |
---|
| 546 | |
---|
[5336] | 547 | int main (int argc, char * argv[]) { |
---|
[5353] | 548 | const char * appname = "roarpluginrunner " ROAR_VSTR_ROARAUDIO; |
---|
| 549 | const char * abiversion = "1.0beta0"; |
---|
[5341] | 550 | const char * pluginargs = NULL; |
---|
[5336] | 551 | enum action action = RUN; |
---|
| 552 | int ret = 0; |
---|
| 553 | int i; |
---|
[5534] | 554 | const char * k; |
---|
[5715] | 555 | int cpi_type = ROAR_SOCKET_TYPE_UNKNOWN; |
---|
| 556 | int cpi_proto = -1; |
---|
| 557 | int cpi_port = 0; |
---|
| 558 | int cpi_touched = 0; |
---|
| 559 | const char * cpi_host = NULL; |
---|
[5336] | 560 | |
---|
[5712] | 561 | g_sched = roar_scheduler_new(ROAR_SCHEDULER_FLAG_NONE, ROAR_SCHEDULER_STRATEGY_DEFAULT); |
---|
| 562 | if ( g_sched == NULL ) { |
---|
| 563 | fprintf(stderr, "Error creating scheduler object: %s\n", roar_error2str(roar_error)); |
---|
| 564 | return 1; |
---|
| 565 | } |
---|
| 566 | |
---|
| 567 | roar_scheduler_source_add(g_sched, &g_s_service); |
---|
| 568 | |
---|
[5336] | 569 | for (i = 1; i < argc; i++) { |
---|
| 570 | k = argv[i]; |
---|
| 571 | |
---|
| 572 | if ( !strcmp(k, "-h") || !strcmp(k, "--help") ) { |
---|
| 573 | usage(argv[0]); |
---|
[5712] | 574 | roar_scheduler_unref(g_sched); |
---|
[5336] | 575 | return 0; |
---|
| 576 | } else if ( !strcmp(k, "--run") ) { |
---|
| 577 | action = RUN; |
---|
[5484] | 578 | } else if ( !strcmp(k, "--run-as-application") ) { |
---|
| 579 | action = RUN_AS_APPLICATION; |
---|
[5336] | 580 | } else if ( !strcmp(k, "--explain") ) { |
---|
| 581 | action = EXPLAIN; |
---|
[5355] | 582 | } else if ( !strcmp(k, "-v") || !strcmp(k, "--verbose") ) { |
---|
[5336] | 583 | g_verbose++; |
---|
[5353] | 584 | } else if ( !strcmp(k, "--server") ) { |
---|
| 585 | roar_libroar_set_server(argv[++i]); |
---|
[5715] | 586 | |
---|
| 587 | } else if ( !strcmp(k, "--tcp") || !strcmp(k, "-t") ) { |
---|
| 588 | cpi_type = ROAR_SOCKET_TYPE_TCP; |
---|
| 589 | cpi_touched++; |
---|
| 590 | } else if ( !strcmp(k, "--unix") || !strcmp(k, "-u") ) { |
---|
| 591 | cpi_type = ROAR_SOCKET_TYPE_UNIX; |
---|
| 592 | cpi_touched++; |
---|
| 593 | } else if ( !strcmp(k, "--decnet") || !strcmp(k, "-n") ) { |
---|
| 594 | cpi_type = ROAR_SOCKET_TYPE_DECNET; |
---|
| 595 | cpi_touched++; |
---|
| 596 | } else if ( !strcmp(k, "--port") ) { |
---|
| 597 | cpi_port = atoi(argv[++i]); |
---|
| 598 | cpi_touched++; |
---|
| 599 | } else if ( !strcmp(k, "--bind") ) { |
---|
| 600 | cpi_host = argv[++i]; |
---|
| 601 | cpi_touched++; |
---|
| 602 | } else if ( !strcmp(k, "--proto") ) { |
---|
| 603 | cpi_proto = roar_str2proto(argv[++i]); |
---|
| 604 | if ( cpi_proto == -1 ) { |
---|
| 605 | fprintf(stderr, "Unknown protocol: %s: %s\n", argv[i], roar_error2str(roar_error)); |
---|
| 606 | return 1; |
---|
| 607 | } |
---|
| 608 | } else if ( !strcmp(k, "--new-sock") ) { |
---|
| 609 | if ( cpi_touched && add_cpi_listen(cpi_proto, cpi_type, cpi_host, cpi_port) == -1 ) { |
---|
| 610 | fprintf(stderr, "Can not open socket for CPI: %s\n", roar_error2str(roar_error)); |
---|
| 611 | return 1; |
---|
| 612 | } |
---|
| 613 | cpi_touched = 0; |
---|
| 614 | } else if ( !strcmp(k, "--client-fh") ) { |
---|
| 615 | if ( add_cpi_client(cpi_proto, atoi(argv[++i])) == -1 ) { |
---|
| 616 | fprintf(stderr, "Can not add CPI client: %s\n", roar_error2str(roar_error)); |
---|
| 617 | return 1; |
---|
| 618 | } |
---|
| 619 | |
---|
[5341] | 620 | } else if ( !strcmp(k, "--appname") ) { |
---|
| 621 | appname = argv[++i]; |
---|
| 622 | _clear_para(); |
---|
| 623 | } else if ( !strcmp(k, "--abiversion") ) { |
---|
| 624 | abiversion = argv[++i]; |
---|
| 625 | _clear_para(); |
---|
| 626 | } else if ( !strcmp(k, "--args") ) { |
---|
| 627 | pluginargs = argv[++i]; |
---|
| 628 | _clear_para(); |
---|
[5336] | 629 | } else { |
---|
[5715] | 630 | if ( cpi_touched && add_cpi_listen(cpi_proto, cpi_type, cpi_host, cpi_port) == -1 ) { |
---|
| 631 | fprintf(stderr, "Can not open socket for CPI: %s\n", roar_error2str(roar_error)); |
---|
| 632 | return 1; |
---|
| 633 | } |
---|
| 634 | cpi_touched = 0; |
---|
| 635 | |
---|
[5336] | 636 | if ( g_para == NULL ) |
---|
[5484] | 637 | g_para = roar_dl_para_new(action == RUN_AS_APPLICATION ? NULL : pluginargs, NULL, appname, abiversion); |
---|
| 638 | if ( action == RUN_AS_APPLICATION && _add_para(g_para, pluginargs, argc - i - 1, &(argv[i+1])) == -1 ) { |
---|
| 639 | fprintf(stderr, "Error parsing plugin arguments: %s\n", roar_error2str(roar_error)); |
---|
| 640 | } else { |
---|
| 641 | roar_err_set(ROAR_ERROR_NONE); |
---|
| 642 | if ( do_plugin(action, k) == -1 ) { |
---|
| 643 | fprintf(stderr, "Error loading plugin: %s\n", |
---|
| 644 | roar_error != ROAR_ERROR_NONE ? roar_error2str(roar_error) : roar_dl_errstr(NULL)); |
---|
| 645 | ret = 1; |
---|
| 646 | } |
---|
[5341] | 647 | } |
---|
[5484] | 648 | if ( action == RUN_AS_APPLICATION ) |
---|
| 649 | break; // end looping over arguments |
---|
[5336] | 650 | } |
---|
| 651 | } |
---|
| 652 | |
---|
| 653 | _clear_para(); |
---|
| 654 | |
---|
[5712] | 655 | roar_scheduler_unref(g_sched); |
---|
[5336] | 656 | return ret; |
---|
| 657 | } |
---|
| 658 | |
---|
| 659 | //ll |
---|