UNIXworkcode

1 /* 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3 * 4 * Copyright 2013 Olaf Wintermann. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include <dlfcn.h> 30 #include <ucx/string.h> 31 32 #include "../daemon/func.h" 33 #include "../daemon/log.h" 34 35 #include "init.h" 36 37 int init_test(pblock *pb, Session *sn, Request *rq) { 38 printf("init-test\n"); 39 return REQ_PROCEED; 40 } 41 42 int load_modules(pblock *pb, Session *sn, Request *rq) { 43 char *shlib = pblock_findval("shlib", pb); 44 char *funcs = pblock_findval("funcs", pb); 45 46 if(shlib == NULL || funcs == NULL) { 47 log_ereport(LOG_MISCONFIG, "load-modules: missing parameters"); 48 if(!shlib && funcs) { 49 log_ereport( 50 LOG_MISCONFIG, 51 "load-modules: missing shlib parameter"); 52 } else if(!shlib && !funcs) { 53 log_ereport( 54 LOG_MISCONFIG, 55 "load-modules: missing funcs parameter"); 56 } else { 57 log_ereport( 58 LOG_MISCONFIG, 59 "load-modules: missing shlib and funcs parameters"); 60 } 61 } 62 63 // load lib 64 void *lib = dlopen(shlib, RTLD_GLOBAL | RTLD_NOW); 65 if(lib == NULL) { 66 char *dlerr = dlerror(); 67 fprintf(stderr, "Cannot load library %s %s\n", shlib, dlerr); 68 69 log_ereport(LOG_FAILURE, "Cannot load library %s: %s", shlib, dlerr); 70 71 return REQ_ABORTED; 72 } 73 74 // load function symbols 75 int b = 0; 76 for(int i=0;;i++) { 77 if(funcs[i] == '-') { 78 funcs[i] = '_'; 79 } 80 if(funcs[i] == ',' || funcs[i] == 0) { 81 if(funcs[i] == 0) { 82 b = 1; 83 } 84 85 funcs[i] = 0; 86 87 // we have a function name 88 void *sym = dlsym(lib, funcs); 89 if(sym == NULL) { 90 fprintf(stderr, "Cannot load symbol %s\n", funcs); 91 dlclose(lib); 92 return REQ_ABORTED; 93 } 94 struct FuncStruct fc; 95 ZERO(&fc, sizeof(struct FuncStruct)); 96 fc.func = (FuncPtr)sym; 97 fc.name = sstrdup(sstr(funcs)).ptr; 98 add_function(&fc); 99 100 if(b) { 101 break; 102 } 103 104 funcs += i + 1; 105 i = 0; 106 } 107 } 108 109 // if exists, execute nsapi_module_init 110 void *sym = dlsym(lib, "nsapi_module_init"); 111 if(sym) { 112 /* 113 * We remove shlib and funcs from the pblock. The module init function 114 * gets all remaining parameters. 115 */ 116 pblock_remove("shlib", pb); 117 pblock_remove("funcs", pb); 118 119 FuncPtr init_func = (FuncPtr)sym; 120 int ret = init_func(pb, NULL, NULL); 121 if(ret != REQ_PROCEED && ret != REQ_NOACTION) { 122 log_ereport( 123 LOG_FAILURE, 124 "nsapi_module_init for module %s failed", 125 shlib); 126 return REQ_ABORTED; 127 } 128 } 129 130 return REQ_PROCEED; 131 } 132