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"
17 #define DEFDBNAME "database"
20 /* Define the common structures that are used throughout the whole server. */
21 struct settings settings
;
23 struct cache
*cache_table
;
24 struct queue
*op_queue
;
27 static void help(void) {
30 " -d dbpath database path ('database', must be created with dpmgr)\n"
31 " -l lower TIPC lower port number (10)\n"
32 " -L upper TIPC upper port number (= lower)\n"
33 " -t port TCP listening port (26010)\n"
34 " -T addr TCP listening address (all local addresses)\n"
35 " -u port UDP listening port (26010)\n"
36 " -U addr UDP listening address (all local addresses)\n"
37 " -s port SCTP listening port (26010)\n"
38 " -S addr SCTP listening address (all local addresses)\n"
39 " -c nobj max. number of objects to be cached, in thousands (128)\n"
40 " -o fname log to the given file (defaults to stdout).\n"
41 " -f don't fork and stay in the foreground\n"
42 " -p enable passive mode, for redundancy purposes (read docs.)\n"
43 " -h show this help\n"
45 "Please report bugs to Alberto Bertogli (albertito@gmail.com)\n"
51 static int load_settings(int argc
, char **argv
)
55 settings
.tipc_lower
= -1;
56 settings
.tipc_upper
= -1;
57 settings
.tcp_addr
= NULL
;
58 settings
.tcp_port
= -1;
59 settings
.udp_addr
= NULL
;
60 settings
.udp_port
= -1;
61 settings
.sctp_addr
= NULL
;
62 settings
.sctp_port
= -1;
63 settings
.numobjs
= -1;
64 settings
.foreground
= 0;
66 settings
.logfname
= "-";
68 settings
.dbname
= malloc(strlen(DEFDBNAME
) + 1);
69 strcpy(settings
.dbname
, DEFDBNAME
);
71 while ((c
= getopt(argc
, argv
, "d:l:L:t:T:u:U:c:o:fph?")) != -1) {
74 free(settings
.dbname
);
75 settings
.dbname
= malloc(strlen(optarg
) + 1);
76 strcpy(settings
.dbname
, optarg
);
80 settings
.tipc_lower
= atoi(optarg
);
83 settings
.tipc_upper
= atoi(optarg
);
87 settings
.tcp_port
= atoi(optarg
);
90 settings
.tcp_addr
= optarg
;
94 settings
.udp_port
= atoi(optarg
);
97 settings
.udp_addr
= optarg
;
101 settings
.sctp_port
= atoi(optarg
);
104 settings
.sctp_addr
= optarg
;
108 settings
.numobjs
= atoi(optarg
) * 1024;
112 settings
.logfname
= malloc(strlen(optarg
) + 1);
113 strcpy(settings
.logfname
, optarg
);
117 settings
.foreground
= 1;
120 settings
.passive
= 1;
128 printf("Unknown parameter '%c'\n", c
);
133 if (settings
.tipc_lower
== -1)
134 settings
.tipc_lower
= TIPC_SERVER_INST
;
135 if (settings
.tipc_upper
== -1)
136 settings
.tipc_upper
= settings
.tipc_lower
;
137 if (settings
.tcp_addr
== NULL
)
138 settings
.tcp_addr
= TCP_SERVER_ADDR
;
139 if (settings
.tcp_port
== -1)
140 settings
.tcp_port
= TCP_SERVER_PORT
;
141 if (settings
.udp_addr
== NULL
)
142 settings
.udp_addr
= UDP_SERVER_ADDR
;
143 if (settings
.udp_port
== -1)
144 settings
.udp_port
= UDP_SERVER_PORT
;
145 if (settings
.sctp_addr
== NULL
)
146 settings
.sctp_addr
= SCTP_SERVER_ADDR
;
147 if (settings
.sctp_port
== -1)
148 settings
.sctp_port
= SCTP_SERVER_PORT
;
149 if (settings
.numobjs
== -1)
150 settings
.numobjs
= 128 * 1024;
156 int main(int argc
, char **argv
)
164 if (!load_settings(argc
, argv
))
168 perror("Error opening log file");
174 cd
= cache_create(settings
.numobjs
, 0);
176 errlog("Error creating cache");
183 errlog("Error creating queue");
188 db
= db_open(settings
.dbname
, 0);
190 errlog("Error opening DB");
194 if (!settings
.foreground
) {
199 } else if (pid
< 0) {
200 errlog("Error in fork()");
208 wlog("Starting nmdb\n");
210 dbthread
= db_loop_start(db
);
214 db_loop_stop(dbthread
);