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 <stdio.h> 30 #include <stdlib.h> 31 32 #include "../public/nsapi.h" 33 34 #include <ucx/string.h> 35 36 #include "httplistener.h" 37 #include "log.h" 38 #include "configmanager.h" 39 40 static ServerConfiguration *current_config = NULL; 41 static time_t sc_last_modified = 0; 42 43 static UcxMap *config_files; 44 45 static conf_global_vars_s global_vars; 46 47 void init_configuration_manager() { 48 /* init parser */ 49 init_server_config_parser(); 50 51 config_files = ucx_map_new(16); 52 } 53 54 NSAPI_PUBLIC conf_global_vars_s* conf_getglobals() { 55 return &global_vars; 56 } 57 58 void cfgmgr_attach_file(ConfigFile *cf) { 59 ucx_map_sstr_put(config_files, cf->file, cf); 60 } 61 62 ConfigFile* cfgmgr_get_file(sstr_t name) { 63 return ucx_map_sstr_get(config_files, name); 64 } 65 66 int cfgmgr_reload_file(ConfigFile *f, ServerConfiguration *conf, int *reload) { 67 struct stat s; 68 if(stat(f->file.ptr, &s) != 0) { 69 fprintf( 70 stderr, 71 "Error: Cannot get stat of file %s\n", f->file.ptr); 72 perror("cfgmgr_load_config: stat"); 73 return -1; 74 } 75 76 //printf("1 time: %d - %d\n", f->last_modified, s.st_mtim.tv_sec); 77 if(f->last_modified != s.st_mtime) { 78 /* reload the file */ 79 if(f->last_modified != 0) { 80 log_ereport( 81 LOG_INFORM, 82 "reload configuration file: %s", 83 f->file.ptr); 84 } 85 if(f->reload(f, conf)) { 86 return -1; 87 } 88 f->last_modified = s.st_mtime; 89 if(reload) { 90 *reload = 1; 91 } 92 } 93 return 0; 94 } 95 96 int cfgmgr_load_config(ServerConfiguration **set_cfg) { 97 int cfgreload = 0; 98 99 /* check config files */ 100 UcxMapIterator iter = ucx_map_iterator(config_files); 101 ConfigFile *f; 102 UCX_MAP_FOREACH(key, f, iter) { 103 if(cfgmgr_reload_file(f, current_config, &cfgreload) == -1) { 104 return -1; 105 } 106 } 107 108 struct stat s; 109 if(stat("config/server.conf", &s) != 0) { 110 perror("stat(\"config/server.conf\")"); 111 return -1; 112 } 113 114 ServerConfiguration *config; 115 if(cfgreload || !current_config || sc_last_modified != s.st_mtime) { 116 config = load_server_conf( 117 current_config, 118 "config/server.conf"); 119 120 if(config == NULL) { 121 return -1; 122 } 123 124 sc_last_modified = s.st_mtime; 125 } else { 126 log_ereport(LOG_VERBOSE, "no reconfig required"); 127 config = current_config; 128 } 129 130 if(set_cfg) { 131 *set_cfg = config; 132 } 133 ServerConfiguration *old_conf = NULL; 134 if(current_config != config) { 135 old_conf = current_config; 136 } 137 current_config = config; 138 if(old_conf) { 139 cfg_unref(old_conf); 140 } 141 return 0; 142 } 143 144 ServerConfiguration *cfgmgr_get_server_config() { 145 //cfg_ref(current_config); 146 return current_config; 147 } 148 149 150