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
= "-";
75 settings
.pidfile
= NULL
;
76 settings
.backend
= DEFAULT_BE
;
78 settings
.dbname
= malloc(strlen(DEFDBNAME
) + 1);
79 strcpy(settings
.dbname
, DEFDBNAME
);
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
= malloc(strlen(optarg
) + 1);
90 strcpy(settings
.dbname
, optarg
);
94 settings
.tipc_lower
= atoi(optarg
);
97 settings
.tipc_upper
= atoi(optarg
);
101 settings
.tcp_port
= atoi(optarg
);
104 settings
.tcp_addr
= optarg
;
108 settings
.udp_port
= atoi(optarg
);
111 settings
.udp_addr
= optarg
;
115 settings
.sctp_port
= atoi(optarg
);
118 settings
.sctp_addr
= optarg
;
122 settings
.numobjs
= atoi(optarg
) * 1024;
126 settings
.logfname
= malloc(strlen(optarg
) + 1);
127 strcpy(settings
.logfname
, optarg
);
131 settings
.pidfile
= malloc(strlen(optarg
) + 1);
132 strcpy(settings
.pidfile
, optarg
);
136 settings
.foreground
= 1;
139 settings
.passive
= 1;
142 settings
.read_only
= 1;
150 printf("Unknown parameter '%c'\n", c
);
155 if (settings
.tipc_lower
== -1)
156 settings
.tipc_lower
= TIPC_SERVER_INST
;
157 if (settings
.tipc_upper
== -1)
158 settings
.tipc_upper
= settings
.tipc_lower
;
159 if (settings
.tcp_addr
== NULL
)
160 settings
.tcp_addr
= TCP_SERVER_ADDR
;
161 if (settings
.tcp_port
== -1)
162 settings
.tcp_port
= TCP_SERVER_PORT
;
163 if (settings
.udp_addr
== NULL
)
164 settings
.udp_addr
= UDP_SERVER_ADDR
;
165 if (settings
.udp_port
== -1)
166 settings
.udp_port
= UDP_SERVER_PORT
;
167 if (settings
.sctp_addr
== NULL
)
168 settings
.sctp_addr
= SCTP_SERVER_ADDR
;
169 if (settings
.sctp_port
== -1)
170 settings
.sctp_port
= SCTP_SERVER_PORT
;
171 if (settings
.numobjs
== -1)
172 settings
.numobjs
= 128 * 1024;
174 if (settings
.backend
== BE_UNKNOWN
) {
175 printf("Error: unknown backend\n");
177 } else if (settings
.backend
== BE_UNSUPPORTED
) {
178 printf("Error: unsupported backend\n");
186 int main(int argc
, char **argv
)
194 if (!load_settings(argc
, argv
))
198 perror("Error opening log file");
204 cd
= cache_create(settings
.numobjs
, 0);
206 errlog("Error creating cache");
213 errlog("Error creating queue");
218 db
= db_open(settings
.backend
, settings
.dbname
, 0);
220 errlog("Error opening DB");
223 wlog("Opened database \"%s\" with %s backend\n", settings
.dbname
,
224 be_str_from_type(settings
.backend
));
226 if (!settings
.foreground
) {
231 } else if (pid
< 0) {
232 errlog("Error in fork()");
240 wlog("Starting nmdb\n");
244 dbthread
= db_loop_start(db
);
248 db_loop_stop(dbthread
);
256 unlink(settings
.pidfile
);