Mercurial > hg > freeDiameter
comparison freeDiameter/main.c @ 10:c5c99c73c2bf
Added some extensions and functions in the daemon
author | Sebastien Decugis <sdecugis@nict.go.jp> |
---|---|
date | Fri, 25 Sep 2009 16:12:08 +0900 |
parents | fc7c18867cf7 |
children | 6576ef5e01eb |
comparison
equal
deleted
inserted
replaced
9:fc7c18867cf7 | 10:c5c99c73c2bf |
---|---|
36 #include "fD.h" | 36 #include "fD.h" |
37 | 37 |
38 #include <signal.h> | 38 #include <signal.h> |
39 #include <getopt.h> | 39 #include <getopt.h> |
40 | 40 |
41 /* forward declarations */ | |
42 static void * sig_hdl(void * arg); | |
43 static int main_cmdline(int argc, char *argv[]); | |
44 | |
45 /* The static configuration structure */ | |
46 static struct fd_config conf; | |
47 struct fd_config * fd_g_config = &conf; | |
48 | |
49 /* freeDiameter starting point */ | |
50 int main(int argc, char * argv[]) | |
51 { | |
52 int ret; | |
53 pthread_t sig_th; | |
54 sigset_t sig_all; | |
55 | |
56 memset(fd_g_config, 0, sizeof(struct fd_config)); | |
57 sigfillset(&sig_all); | |
58 CHECK_POSIX( pthread_sigmask(SIG_BLOCK, &sig_all, NULL) ); | |
59 | |
60 /* Initialize the library */ | |
61 CHECK_FCT( fd_lib_init() ); | |
62 | |
63 /* Name this thread */ | |
64 fd_log_threadname("Main"); | |
65 | |
66 /* Initialize the config */ | |
67 CHECK_FCT( fd_conf_init() ); | |
68 | |
69 /* Parse the command-line */ | |
70 CHECK_FCT( main_cmdline(argc, argv) ); | |
71 | |
72 /* Allow SIGINT and SIGTERM from this point */ | |
73 CHECK_POSIX( pthread_create(&sig_th, NULL, sig_hdl, NULL) ); | |
74 | |
75 /* Add definitions of the base protocol */ | |
76 CHECK_FCT( fd_dict_base_protocol(fd_g_config->cnf_dict) ); | |
77 | |
78 /* Initialize other modules */ | |
79 CHECK_FCT( fd_ext_init() ); | |
80 CHECK_FCT( fd_queues_init() ); | |
81 CHECK_FCT( fd_msg_init() ); | |
82 | |
83 /* Parse the configuration file */ | |
84 CHECK_FCT( fd_conf_parse() ); | |
85 | |
86 /* Load the dynamic extensions */ | |
87 CHECK_FCT( fd_ext_load() ); | |
88 | |
89 /* Start the peer state machines */ | |
90 | |
91 | |
92 /* Now, just wait for events */ | |
93 TRACE_DEBUG(INFO, FD_PROJECT_BINARY " daemon initialized."); | |
94 fd_conf_dump(); | |
95 while (1) { | |
96 int code; | |
97 CHECK_FCT_DO( fd_event_get(fd_g_config->cnf_main_ev, &code, NULL), break ); | |
98 switch (code) { | |
99 case FDEV_DUMP_DICT: | |
100 fd_dict_dump(fd_g_config->cnf_dict); | |
101 break; | |
102 | |
103 case FDEV_DUMP_EXT: | |
104 fd_ext_dump(); | |
105 break; | |
106 | |
107 case FDEV_DUMP_QUEUES: | |
108 fd_fifo_dump(0, "Incoming messages", fd_g_incoming, fd_msg_dump_walk); | |
109 fd_fifo_dump(0, "Outgoing messages", fd_g_outgoing, fd_msg_dump_walk); | |
110 fd_fifo_dump(0, "Local messages", fd_g_local, fd_msg_dump_walk); | |
111 break; | |
112 | |
113 case FDEV_DUMP_CONFIG: | |
114 fd_conf_dump(); | |
115 break; | |
116 | |
117 | |
118 case FDEV_TERMINATE: | |
119 ret = 0; | |
120 goto end; | |
121 | |
122 default: | |
123 TRACE_DEBUG(INFO, "Unexpected event in the daemon (%d), ignored.\n", code); | |
124 } | |
125 } | |
126 | |
127 end: | |
128 TRACE_DEBUG(INFO, FD_PROJECT_BINARY " daemon is stopping..."); | |
129 | |
130 /* cleanups */ | |
131 CHECK_FCT_DO( fd_ext_fini(), /* continue */ ); | |
132 CHECK_FCT_DO( fd_thr_term(&sig_th), /* continue */ ); | |
133 | |
134 return ret; | |
135 } | |
41 | 136 |
42 /* Display package version */ | 137 /* Display package version */ |
43 static void main_version_core(void) | 138 static void main_version_core(void) |
44 { | 139 { |
45 printf("%s, version %d.%d.%d" | 140 printf("%s, version %d.%d.%d" |
119 main_version(); | 214 main_version(); |
120 exit(0); | 215 exit(0); |
121 | 216 |
122 case 'c': /* Read configuration from this file instead of the default location.. */ | 217 case 'c': /* Read configuration from this file instead of the default location.. */ |
123 CHECK_PARAMS( optarg ); | 218 CHECK_PARAMS( optarg ); |
124 fd_g_config->conf_file = optarg; | 219 fd_g_config->cnf_file = optarg; |
125 break; | 220 break; |
126 | 221 |
127 case 'd': /* Increase verbosity of debug messages. */ | 222 case 'd': /* Increase verbosity of debug messages. */ |
128 fd_g_debug_lvl++; | 223 fd_g_debug_lvl++; |
129 break; | 224 break; |
156 # define SIGNALSTR(sig) (((sig) < nsignalstr) ? signalstr[(sig)] : "unknown") | 251 # define SIGNALSTR(sig) (((sig) < nsignalstr) ? signalstr[(sig)] : "unknown") |
157 #else /* HAVE_SIGNALENT_H */ | 252 #else /* HAVE_SIGNALENT_H */ |
158 # define SIGNALSTR(sig) ("") | 253 # define SIGNALSTR(sig) ("") |
159 #endif /* HAVE_SIGNALENT_H */ | 254 #endif /* HAVE_SIGNALENT_H */ |
160 | 255 |
161 | |
162 /* signal handler */ | 256 /* signal handler */ |
163 static void * sig_hdl(void * arg) | 257 static void * sig_hdl(void * arg) |
164 { | 258 { |
165 sigset_t sig_main; | 259 sigset_t sig_main; |
166 int sig = 0; | 260 int sig = 0; |
173 sigaddset(&sig_main, SIGTERM); | 267 sigaddset(&sig_main, SIGTERM); |
174 | 268 |
175 CHECK_SYS_DO( sigwait(&sig_main, &sig), TRACE_DEBUG(INFO, "Error in sigwait function") ); | 269 CHECK_SYS_DO( sigwait(&sig_main, &sig), TRACE_DEBUG(INFO, "Error in sigwait function") ); |
176 | 270 |
177 TRACE_DEBUG(INFO, "Received signal %s (%d), exiting", SIGNALSTR(sig), sig); | 271 TRACE_DEBUG(INFO, "Received signal %s (%d), exiting", SIGNALSTR(sig), sig); |
178 CHECK_FCT_DO( fd_event_send(fd_g_config->g_fifo_main, FM_TERMINATE, NULL), exit(2) ); | 272 CHECK_FCT_DO( fd_event_send(fd_g_config->cnf_main_ev, FDEV_TERMINATE, NULL), exit(2) ); |
179 return NULL; | 273 return NULL; |
180 } | 274 } |
181 | 275 |
182 /* The static configuration structure */ | |
183 static struct fd_config conf; | |
184 struct fd_config * fd_g_config = &conf; | |
185 | |
186 /* Entry point */ | |
187 int main(int argc, char * argv[]) | |
188 { | |
189 int ret; | |
190 pthread_t sig_th; | |
191 sigset_t sig_all; | |
192 | |
193 memset(fd_g_config, 0, sizeof(struct fd_config)); | |
194 sigfillset(&sig_all); | |
195 CHECK_POSIX( pthread_sigmask(SIG_BLOCK, &sig_all, NULL) ); | |
196 | |
197 /* Initialize the library */ | |
198 CHECK_FCT( fd_lib_init() ); | |
199 | |
200 /* Name this thread */ | |
201 fd_log_threadname("Main"); | |
202 | |
203 /* Initialize the config */ | |
204 CHECK_FCT( fd_conf_init() ); | |
205 | |
206 /* Parse the command-line */ | |
207 CHECK_FCT( main_cmdline(argc, argv) ); | |
208 | |
209 /* Allow SIGINT and SIGTERM from this point */ | |
210 CHECK_POSIX( pthread_create(&sig_th, NULL, sig_hdl, NULL) ); | |
211 | |
212 /* Add definitions of the base protocol */ | |
213 CHECK_FCT( fd_dict_base_protocol(fd_g_config->g_dict) ); | |
214 | |
215 /* Initialize other modules */ | |
216 CHECK_FCT( fd_ext_init() ); | |
217 | |
218 /* Parse the configuration file */ | |
219 CHECK_FCT( fd_conf_parse() ); | |
220 | |
221 /* Load the dynamic extensions */ | |
222 CHECK_FCT( fd_ext_load() ); | |
223 | |
224 /* Start the peer state machines */ | |
225 | |
226 | |
227 /* Now, just wait for events */ | |
228 TRACE_DEBUG(INFO, FD_PROJECT_BINARY " daemon initialized."); | |
229 fd_conf_dump(); | |
230 while (1) { | |
231 int code; | |
232 CHECK_FCT_DO( fd_event_get(fd_g_config->g_fifo_main, &code, NULL), break ); | |
233 switch (code) { | |
234 case FM_TERMINATE: | |
235 ret = 0; | |
236 goto end; | |
237 | |
238 default: | |
239 TRACE_DEBUG(INFO, "Unexpected event in the daemon (%d), terminating.\n", code); | |
240 ret = -1; | |
241 goto end; | |
242 } | |
243 } | |
244 | |
245 end: | |
246 TRACE_DEBUG(INFO, FD_PROJECT_BINARY " daemon is stopping..."); | |
247 | |
248 /* cleanups */ | |
249 CHECK_FCT_DO( fd_ext_fini(), /* continue */ ); | |
250 CHECK_FCT_DO( fd_thr_term(&sig_th), /* continue */ ); | |
251 | |
252 return ret; | |
253 } |