Mercurial > hg > freeDiameter
annotate freeDiameter/main.c @ 9:fc7c18867cf7
New extension mechanism committed
author | Sebastien Decugis <sdecugis@nict.go.jp> |
---|---|
date | Thu, 24 Sep 2009 14:01:48 +0900 |
parents | 3e143f047f78 |
children | c5c99c73c2bf |
rev | line source |
---|---|
0 | 1 /********************************************************************************************************* |
2 * Software License Agreement (BSD License) * | |
3 * Author: Sebastien Decugis <sdecugis@nict.go.jp> * | |
4 * * | |
5 * Copyright (c) 2009, WIDE Project and NICT * | |
6 * All rights reserved. * | |
7 * * | |
8 * Redistribution and use of this software in source and binary forms, with or without modification, are * | |
9 * permitted provided that the following conditions are met: * | |
10 * * | |
11 * * Redistributions of source code must retain the above * | |
12 * copyright notice, this list of conditions and the * | |
13 * following disclaimer. * | |
14 * * | |
15 * * Redistributions in binary form must reproduce the above * | |
16 * copyright notice, this list of conditions and the * | |
17 * following disclaimer in the documentation and/or other * | |
18 * materials provided with the distribution. * | |
19 * * | |
20 * * Neither the name of the WIDE Project or NICT nor the * | |
21 * names of its contributors may be used to endorse or * | |
22 * promote products derived from this software without * | |
23 * specific prior written permission of WIDE Project and * | |
24 * NICT. * | |
25 * * | |
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED * | |
27 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A * | |
28 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR * | |
29 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * | |
30 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * | |
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR * | |
32 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * | |
33 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * | |
34 *********************************************************************************************************/ | |
35 | |
1
bafb831ba688
Fix names to proper case for freeDiameter
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
0
diff
changeset
|
36 #include "fD.h" |
0 | 37 |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
38 #include <signal.h> |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
39 #include <getopt.h> |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
40 |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
41 |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
42 /* Display package version */ |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
43 static void main_version_core(void) |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
44 { |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
45 printf("%s, version %d.%d.%d" |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
46 #ifdef HG_VERSION |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
47 " (r%s" |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
48 # ifdef PACKAGE_HG_REVISION |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
49 "/%s" |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
50 # endif /* PACKAGE_HG_VERSION */ |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
51 ")" |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
52 #endif /* HG_VERSION */ |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
53 "\n", |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
54 FD_PROJECT_NAME, FD_PROJECT_VERSION_MAJOR, FD_PROJECT_VERSION_MINOR, FD_PROJECT_VERSION_REV |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
55 #ifdef HG_VERSION |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
56 , HG_VERSION |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
57 # ifdef PACKAGE_HG_REVISION |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
58 , PACKAGE_HG_REVISION |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
59 # endif /* PACKAGE_HG_VERSION */ |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
60 #endif /* HG_VERSION */ |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
61 ); |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
62 } |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
63 |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
64 /* Display package version and general info */ |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
65 static void main_version(void) |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
66 { |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
67 main_version_core(); |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
68 printf( "%s\n", FD_PROJECT_COPYRIGHT); |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
69 printf( "\nSee " FD_PROJECT_NAME " homepage at http://aaa.koganei.wide.ad.jp/\n" |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
70 " for information, updates and bug reports on this software.\n"); |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
71 } |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
72 |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
73 /* Print command-line options */ |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
74 static void main_help( void ) |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
75 { |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
76 main_version_core(); |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
77 printf( " This daemon is an implementation of the Diameter protocol\n" |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
78 " used for Authentication, Authorization, and Accounting (AAA).\n"); |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
79 printf("\nUsage: " FD_PROJECT_BINARY " [OPTIONS]...\n"); |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
80 printf( " -h, --help Print help and exit\n" |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
81 " -V, --version Print version and exit\n" |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
82 " -c, --config=filename Read configuration from this file instead of the \n" |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
83 " default location (%s).\n", DEFAULT_CONF_FILE); |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
84 printf( "\nDebug:\n" |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
85 " These options are mostly useful for developers\n" |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
86 " -d, --debug Increase verbosity of debug messages\n" |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
87 " -q, --quiet Decrease verbosity then remove debug messages\n"); |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
88 } |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
89 |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
90 /* Parse the command-line */ |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
91 static int main_cmdline(int argc, char *argv[]) |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
92 { |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
93 int c; |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
94 int option_index = 0; |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
95 |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
96 struct option long_options[] = { |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
97 { "help", 0, NULL, 'h' }, |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
98 { "version", 0, NULL, 'V' }, |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
99 { "config", 1, NULL, 'c' }, |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
100 { "debug", 0, NULL, 'd' }, |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
101 { "quiet", 0, NULL, 'q' }, |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
102 { NULL, 0, NULL, 0 } |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
103 }; |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
104 |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
105 TRACE_ENTRY("%d %p", argc, argv); |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
106 |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
107 /* Loop on arguments */ |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
108 while (1) { |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
109 c = getopt_long (argc, argv, "hVc:dq", long_options, &option_index); |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
110 if (c == -1) |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
111 break; /* Exit from the loop. */ |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
112 |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
113 switch (c) { |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
114 case 'h': /* Print help and exit. */ |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
115 main_help(); |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
116 exit(0); |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
117 |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
118 case 'V': /* Print version and exit. */ |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
119 main_version(); |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
120 exit(0); |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
121 |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
122 case 'c': /* Read configuration from this file instead of the default location.. */ |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
123 CHECK_PARAMS( optarg ); |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
124 fd_g_config->conf_file = optarg; |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
125 break; |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
126 |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
127 case 'd': /* Increase verbosity of debug messages. */ |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
128 fd_g_debug_lvl++; |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
129 break; |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
130 |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
131 case 'q': /* Decrease verbosity then remove debug messages. */ |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
132 fd_g_debug_lvl--; |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
133 break; |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
134 |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
135 case '?': /* Invalid option. */ |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
136 /* `getopt_long' already printed an error message. */ |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
137 TRACE_DEBUG(INFO, "getopt_long found an invalid character\n"); |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
138 return EINVAL; |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
139 |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
140 default: /* bug: option not considered. */ |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
141 TRACE_DEBUG(INFO, "A command-line option is missing in parser: %c\n", c); |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
142 ASSERT(0); |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
143 return EINVAL; |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
144 } |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
145 } |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
146 |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
147 return 0; |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
148 |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
149 } |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
150 |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
151 #ifdef HAVE_SIGNALENT_H |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
152 const char *const signalstr[] = { |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
153 # include "signalent.h" |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
154 }; |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
155 const int nsignalstr = sizeof signalstr / sizeof signalstr[0]; |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
156 # define SIGNALSTR(sig) (((sig) < nsignalstr) ? signalstr[(sig)] : "unknown") |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
157 #else /* HAVE_SIGNALENT_H */ |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
158 # define SIGNALSTR(sig) ("") |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
159 #endif /* HAVE_SIGNALENT_H */ |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
160 |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
161 |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
162 /* signal handler */ |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
163 static void * sig_hdl(void * arg) |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
164 { |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
165 sigset_t sig_main; |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
166 int sig = 0; |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
167 |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
168 TRACE_ENTRY(); |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
169 fd_log_threadname("Main signal handler"); |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
170 |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
171 sigemptyset(&sig_main); |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
172 sigaddset(&sig_main, SIGINT); |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
173 sigaddset(&sig_main, SIGTERM); |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
174 |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
175 CHECK_SYS_DO( sigwait(&sig_main, &sig), TRACE_DEBUG(INFO, "Error in sigwait function") ); |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
176 |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
177 TRACE_DEBUG(INFO, "Received signal %s (%d), exiting", SIGNALSTR(sig), sig); |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
178 CHECK_FCT_DO( fd_event_send(fd_g_config->g_fifo_main, FM_TERMINATE, NULL), exit(2) ); |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
179 return NULL; |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
180 } |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
181 |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
182 /* The static configuration structure */ |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
183 static struct fd_config conf; |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
184 struct fd_config * fd_g_config = &conf; |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
185 |
0 | 186 /* Entry point */ |
187 int main(int argc, char * argv[]) | |
188 { | |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
189 int ret; |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
190 pthread_t sig_th; |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
191 sigset_t sig_all; |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
192 |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
193 memset(fd_g_config, 0, sizeof(struct fd_config)); |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
194 sigfillset(&sig_all); |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
195 CHECK_POSIX( pthread_sigmask(SIG_BLOCK, &sig_all, NULL) ); |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
196 |
0 | 197 /* Initialize the library */ |
198 CHECK_FCT( fd_lib_init() ); | |
199 | |
200 /* Name this thread */ | |
201 fd_log_threadname("Main"); | |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
202 |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
203 /* Initialize the config */ |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
204 CHECK_FCT( fd_conf_init() ); |
0 | 205 |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
206 /* Parse the command-line */ |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
207 CHECK_FCT( main_cmdline(argc, argv) ); |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
208 |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
209 /* Allow SIGINT and SIGTERM from this point */ |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
210 CHECK_POSIX( pthread_create(&sig_th, NULL, sig_hdl, NULL) ); |
0 | 211 |
212 /* Add definitions of the base protocol */ | |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
213 CHECK_FCT( fd_dict_base_protocol(fd_g_config->g_dict) ); |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
214 |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
215 /* Initialize other modules */ |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
216 CHECK_FCT( fd_ext_init() ); |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
217 |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
218 /* Parse the configuration file */ |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
219 CHECK_FCT( fd_conf_parse() ); |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
220 |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
221 /* Load the dynamic extensions */ |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
222 CHECK_FCT( fd_ext_load() ); |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
223 |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
224 /* Start the peer state machines */ |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
225 |
0 | 226 |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
227 /* Now, just wait for events */ |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
228 TRACE_DEBUG(INFO, FD_PROJECT_BINARY " daemon initialized."); |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
229 fd_conf_dump(); |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
230 while (1) { |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
231 int code; |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
232 CHECK_FCT_DO( fd_event_get(fd_g_config->g_fifo_main, &code, NULL), break ); |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
233 switch (code) { |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
234 case FM_TERMINATE: |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
235 ret = 0; |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
236 goto end; |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
237 |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
238 default: |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
239 TRACE_DEBUG(INFO, "Unexpected event in the daemon (%d), terminating.\n", code); |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
240 ret = -1; |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
241 goto end; |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
242 } |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
243 } |
0 | 244 |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
245 end: |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
246 TRACE_DEBUG(INFO, FD_PROJECT_BINARY " daemon is stopping..."); |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
247 |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
248 /* cleanups */ |
9
fc7c18867cf7
New extension mechanism committed
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
8
diff
changeset
|
249 CHECK_FCT_DO( fd_ext_fini(), /* continue */ ); |
fc7c18867cf7
New extension mechanism committed
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
8
diff
changeset
|
250 CHECK_FCT_DO( fd_thr_term(&sig_th), /* continue */ ); |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
251 |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
3
diff
changeset
|
252 return ret; |
0 | 253 } |