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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
|
/*
* isnsd - the iSNS Daemon
*
* Copyright (C) 2007 Olaf Kirch <olaf.kirch@oracle.com>
*/
#include <getopt.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>
#include <time.h>
#ifdef MTRACE
# include <mcheck.h>
#endif
#include <libisns/isns.h>
#include "security.h"
#include <libisns/util.h>
#include <libisns/paths.h>
#include "internal.h"
enum {
MODE_NORMAL,
MODE_DUMP_DB,
MODE_INIT,
};
static const char * opt_configfile = ISNS_DEFAULT_ISNSD_CONFIG;
static int opt_af = AF_UNSPEC;
static int opt_mode = MODE_NORMAL;
static int opt_foreground = 0;
static char * slp_url;
static int init_server(void);
static void run_server(isns_server_t *, isns_db_t *);
static void usage_and_exit(int, const char *);
static void cleanup(int);
static struct option options[] = {
{ "config", required_argument, NULL, 'c' },
{ "debug", required_argument, NULL, 'd' },
{ "foreground", no_argument, NULL, 'f' },
{ "init", no_argument, NULL, MODE_INIT },
{ "dump-db", no_argument, NULL, MODE_DUMP_DB },
{ "help", no_argument, NULL, 'h' },
{ "version", no_argument, NULL, 'V' },
{ NULL }
};
int
main(int argc, char **argv)
{
isns_server_t *server;
isns_source_t *source;
isns_db_t *db;
int c;
#ifdef MTRACE
mtrace();
#endif
while ((c = getopt_long(argc, argv, "46c:d:fh", options, NULL)) != -1) {
switch (c) {
case '4':
opt_af = AF_INET;
break;
case '6':
opt_af = AF_INET6;
break;
case 'c':
opt_configfile = optarg;
break;
case 'd':
isns_enable_debugging(optarg);
break;
case 'f':
opt_foreground = 1;
break;
case MODE_DUMP_DB:
case MODE_INIT:
opt_mode = c;
break;
case 'h':
usage_and_exit(0, NULL);
__builtin_unreachable();
case 'V':
printf("Open-iSNS version %s\n"
"Copyright (C) 2007, Olaf Kirch <olaf.kirch@oracle.com>\n",
OPENISNS_VERSION_STRING);
return 0;
default:
usage_and_exit(1, "Unknown option");
}
}
if (optind != argc)
usage_and_exit(1, NULL);
isns_read_config(opt_configfile);
if (!isns_config.ic_source_name) {
/*
* Try to read the source name from open-iscsi configuration,
* using the default iniatiator name
*/
isns_read_initiatorname(ISCSI_DEFAULT_INITIATORNAME);
}
isns_init_names();
if (!isns_config.ic_source_name)
usage_and_exit(1, "Please specify an iSNS source name");
source = isns_source_create_iscsi(isns_config.ic_source_name);
if (opt_mode == MODE_INIT)
return !init_server();
if (opt_mode == MODE_NORMAL)
isns_write_pidfile(isns_config.ic_pidfile);
db = isns_db_open(isns_config.ic_database);
if (db == NULL)
isns_fatal("Unable to open database\n");
if (opt_mode == MODE_DUMP_DB) {
isns_db_print(db, isns_print_stdout);
exit(0);
}
if (!opt_foreground) {
if (daemon(0, 0) < 0)
isns_fatal("Unable to background server process\n");
isns_log_background();
isns_update_pidfile(isns_config.ic_pidfile);
}
signal(SIGTERM, cleanup);
signal(SIGINT, cleanup);
server = isns_create_server(source, db, &isns_default_service_ops);
run_server(server, db);
return 0;
}
void
usage_and_exit(int exval, const char *msg)
{
if (msg)
fprintf(stderr, "Error: %s\n", msg);
fprintf(stderr,
"Usage: isnsd [options]\n\n"
" --config Specify alternative config fille\n"
" --foreground Do not put daemon in the background\n"
" --debug Enable debugging (list of debug flags)\n"
" --init Initialize the server (key generation etc)\n"
" --dump-db Display the database contents and exit\n"
" --help Print this message\n"
);
exit(exval);
}
void
cleanup(int sig)
{
isns_remove_pidfile(isns_config.ic_pidfile);
exit(sig == SIGTERM ? 0 : 1);
}
static void
slp_cleanup(void)
{
char *url = slp_url;
slp_url = NULL;
if (url) {
isns_slp_unregister(url);
isns_free(url);
}
}
/*
* Initialize server
*/
int
init_server(void)
{
if (!isns_security_init())
return 0;
/* Anything else? */
return 1;
}
/*
* Server main loop
*/
void
run_server(isns_server_t *server, isns_db_t *db)
{
isns_socket_t *sock;
isns_security_t *ctx = NULL;
isns_message_t *msg, *resp;
int status;
if (isns_config.ic_security) {
const char *ksname;
isns_keystore_t *ks;
ctx = isns_default_security_context(1);
if (!(ksname = isns_config.ic_client_keystore))
isns_fatal("config problem: no key store specified\n");
if (!strcasecmp(ksname, "db:"))
ks = isns_create_db_keystore(db);
else
ks = isns_create_keystore(ksname);
if (ks == NULL)
isns_fatal("Unable to create keystore %s\n", ksname);
isns_security_set_keystore(ctx, ks);
}
status = isns_dd_load_all(db);
if (status != ISNS_SUCCESS)
isns_fatal("Problem loading Discovery Domains from database\n");
/* First socket is the control socket */
sock = isns_create_systemd_socket(0);
if (sock) {
/* Second socket is the iSNS port */
sock = isns_create_systemd_socket(1);
isns_debug_socket("Using systemd fds\n");
goto set_security;
}
if (isns_config.ic_control_socket) {
sock = isns_create_server_socket(isns_config.ic_control_socket,
NULL, AF_UNSPEC, SOCK_STREAM);
if (sock == NULL)
isns_fatal("Unable to create control socket\n");
/*
* isns_socket_set_security_ctx(sock, ctx);
*/
}
sock = isns_create_server_socket(isns_config.ic_bind_address,
"isns", opt_af, SOCK_STREAM);
if (sock == NULL)
isns_fatal("Unable to create server socket\n");
set_security:
isns_socket_set_security_ctx(sock, ctx);
if (isns_config.ic_slp_register) {
slp_url = isns_slp_build_url(0);
isns_slp_register(slp_url);
atexit(slp_cleanup);
}
isns_esi_init(server);
isns_scn_init(server);
while (1) {
struct timeval timeout = { 0, 0 };
time_t now, then, next_timeout = time(NULL) + 3600;
/* Expire entities that haven't seen any activity
* for a while. */
if (isns_config.ic_registration_period) {
then = isns_db_expire(db);
if (then && then < next_timeout)
next_timeout = then;
}
/* Run any timers (eg for ESI) */
then = isns_run_timers();
if (then && then < next_timeout)
next_timeout = then;
/* There may be pending SCNs, push them out now */
then = isns_scn_transmit_all();
if (then && then < next_timeout)
next_timeout = then;
/* Purge any objects that have been marked for removal
* from the DB (deleting them, or moving them to limbo
* state). */
isns_db_purge(db);
/* Determine how long we can sleep before working
* the ESI queues and DB expiry again. */
now = time(NULL);
if (next_timeout <= now)
continue;
timeout.tv_sec = next_timeout - now;
if ((msg = isns_recv_message(&timeout)) == NULL)
continue;
if ((resp = isns_process_message(server, msg)) != NULL) {
isns_socket_t *sock = isns_message_socket(msg);
isns_socket_send(sock, resp);
isns_message_release(resp);
}
isns_message_release(msg);
}
}
|