2 #include <stdio.h> /* printf() */
3 #include <unistd.h> /* malloc(), fork() and getopt() */
4 #include <stdlib.h> /* atoi() */
5 #include <sys/types.h> /* for pid_t */
6 #include <string.h> /* for strcpy() and strlen() */
7 #include <pthread.h> /* for pthread_t */
13 #include "net-const.h"
18 #define DEFDBNAME "database"
21 /* Define the common structures that are used throughout the whole server. */
22 struct settings settings
;
24 struct cache
*cache_table
;
25 struct queue
*op_queue
;
28 static void help(void) {
32 " -b backend backend to use (" DEFAULT_BE_NAME
")\n"
33 " -d dbpath database path ('database' by default)\n"
34 " -l lower TIPC lower port number (10)\n"
35 " -L upper TIPC upper port number (= lower)\n"
36 " -t port TCP listening port (26010)\n"
37 " -T addr TCP listening address (all local addresses)\n"
38 " -u port UDP listening port (26010)\n"
39 " -U addr UDP listening address (all local addresses)\n"
40 " -s port SCTP listening port (26010)\n"
41 " -S addr SCTP listening address (all local addresses)\n"
42 " -c nobj max. number of objects to be cached, in thousands (128)\n"
43 " -o fname log to the given file (stdout).\n"
44 " -i pidfile file to write the PID to (none).\n"
45 " -f don't fork and stay in the foreground\n"
46 " -p enable passive mode, for redundancy purposes (read docs.)\n"
47 " -r read-only mode\n"
48 " -h show this help\n"
50 "Available backends: " SUPPORTED_BE
"\n"
52 "Please report bugs to Alberto Bertogli (albertito@blitiri.com.ar)\n"
58 static int load_settings(int argc
, char **argv
)
62 settings
.tipc_lower
= -1;
63 settings
.tipc_upper
= -1;
64 settings
.tcp_addr
= NULL
;
65 settings
.tcp_port
= -1;
66 settings
.udp_addr
= NULL
;
67 settings
.udp_port
= -1;
68 settings
.sctp_addr
= NULL
;
69 settings
.sctp_port
= -1;
70 settings
.numobjs
= -1;
71 settings
.foreground
= 0;
73 settings
.read_only
= 0;
74 settings
.logfname
= NULL
;
75 settings
.pidfile
= NULL
;
76 settings
.backend
= DEFAULT_BE
;
78 settings
.dbname
= strdup(DEFDBNAME
);
79 settings
.logfname
= strdup("-");
81 while ((c
= getopt(argc
, argv
,
82 "b:d:l:L:t:T:u:U:s:S:c:o:i:fprh?")) != -1) {
85 settings
.backend
= be_type_from_str(optarg
);
88 free(settings
.dbname
);
89 settings
.dbname
= strdup(optarg
);
93 settings
.tipc_lower
= atoi(optarg
);
96 settings
.tipc_upper
= atoi(optarg
);
100 settings
.tcp_port
= atoi(optarg
);
103 settings
.tcp_addr
= optarg
;
107 settings
.udp_port
= atoi(optarg
);
110 settings
.udp_addr
= optarg
;
114 settings
.sctp_port
= atoi(optarg
);
117 settings
.sctp_addr
= optarg
;
121 settings
.numobjs
= atoi(optarg
) * 1024;
125 free(settings
.logfname
);
126 settings
.logfname
= strdup(optarg
);
130 free(settings
.pidfile
);
131 settings
.pidfile
= strdup(optarg
);
135 settings
.foreground
= 1;
138 settings
.passive
= 1;
141 settings
.read_only
= 1;
149 printf("Unknown parameter '%c'\n", c
);
154 if (settings
.tipc_lower
== -1)
155 settings
.tipc_lower
= TIPC_SERVER_INST
;
156 if (settings
.tipc_upper
== -1)
157 settings
.tipc_upper
= settings
.tipc_lower
;
158 if (settings
.tcp_addr
== NULL
)
159 settings
.tcp_addr
= TCP_SERVER_ADDR
;
160 if (settings
.tcp_port
== -1)
161 settings
.tcp_port
= TCP_SERVER_PORT
;
162 if (settings
.udp_addr
== NULL
)
163 settings
.udp_addr
= UDP_SERVER_ADDR
;
164 if (settings
.udp_port
== -1)
165 settings
.udp_port
= UDP_SERVER_PORT
;
166 if (settings
.sctp_addr
== NULL
)
167 settings
.sctp_addr
= SCTP_SERVER_ADDR
;
168 if (settings
.sctp_port
== -1)
169 settings
.sctp_port
= SCTP_SERVER_PORT
;
170 if (settings
.numobjs
== -1)
171 settings
.numobjs
= 128 * 1024;
173 if (settings
.backend
== BE_UNKNOWN
) {
174 printf("Error: unknown backend\n");
176 } else if (settings
.backend
== BE_UNSUPPORTED
) {
177 printf("Error: unsupported backend\n");
185 static void free_settings()
187 free(settings
.dbname
);
188 free(settings
.logfname
);
189 free(settings
.pidfile
);
193 int main(int argc
, char **argv
)
201 if (!load_settings(argc
, argv
))
205 perror("Error opening log file");
211 cd
= cache_create(settings
.numobjs
, 0);
213 errlog("Error creating cache");
220 errlog("Error creating queue");
225 db
= db_open(settings
.backend
, settings
.dbname
, 0);
227 errlog("Error opening DB");
230 wlog("Opened database \"%s\" with %s backend\n", settings
.dbname
,
231 be_str_from_type(settings
.backend
));
233 if (!settings
.foreground
) {
238 } else if (pid
< 0) {
239 errlog("Error in fork()");
247 wlog("Starting nmdb\n");
251 dbthread
= db_loop_start(db
);
255 db_loop_stop(dbthread
);
263 if (settings
.pidfile
)
264 unlink(settings
.pidfile
);