]> rtime.felk.cvut.cz Git - can-usb1.git/blob - ulan/host/apps/ul_asd/ul_asd.c
Initializing repo
[can-usb1.git] / ulan / host / apps / ul_asd / ul_asd.c
1 /*******************************************************************
2   uLan Communication - addresss server 
3
4   ul_asd.c      - 
5
6   (C) Copyright 1996-2004 by Pavel Pisa - project originator
7         http://cmp.felk.cvut.cz/~pisa
8   (C) Copyright 1996-2004 PiKRON Ltd.
9         http://www.pikron.com
10   (C) Copyright 2002-2004 Petr Smolik
11   
12
13   The uLan driver project can be used and distributed 
14   in compliance with any of next licenses
15    - GPL - GNU Public License
16      See file COPYING for details.
17    - LGPL - Lesser GNU Public License
18    - MPL - Mozilla Public License
19    - and other licenses added by project originator
20
21   Code can be modified and re-distributed under any combination
22   of the above listed licenses. If contributor does not agree with
23   some of the licenses, he/she can delete appropriate line.
24   WARNING: if you delete all lines, you are not allowed to
25   distribute code or sources in any form.
26  *******************************************************************/
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <getopt.h>
32 #include <unistd.h>
33 #include <sys/types.h>
34 #include <ul_netbase.h>
35 #include <orbit/orbit.h>
36 #include "ul_asd.h"
37 #include <signal.h>
38
39 #define GSA_OFFSET(_type,_member) \
40                 ((int)&(((_type*)0)->_member))
41
42 int daemon_flg=0;
43 int short_flg   = 0;
44 int log_all_flg = 0;
45 int terminate_flg = 0;
46
47 ul_asd_context_t ul_cont[1];
48
49 void sig_usr(int signo) {
50   if ((signo==SIGTERM) || (signo==SIGINT)) {
51     terminate_flg=1;
52   }
53 }
54
55 static int daemon_init(void) {
56 #ifndef _WIN32
57   pid_t pid;
58
59   if ((pid = fork()) < 0) {
60     return -1;
61   } else
62     if (pid != 0) {
63       exit(0);  /* parent vanishes */
64     }
65   /* child process */
66   setsid();
67   umask(0);
68   close(0);
69   close(1);
70   close(2);
71 #endif
72   return 0;
73 }
74
75
76 static void
77 usage()
78 {
79   printf("usage: ul_asd <parameters>\n");
80   printf("  -d, --uldev  <name>    name of uLan device [%s]\n", ul_cont->ul_dev_name);
81   printf("  -n, --ns  <name>       addr of name server\n");
82   printf("        [%s]\n", ul_cont->orbit_ns_addr);
83   printf("  -i, --ip  <name>       IP address for communication with client(s)\n");
84   printf("        [%s]\n", ul_cont->orbit_orb_iiop_ipname);
85   printf("  -c, --conf  <name>     name of configuration file [%s]\n", ul_cont->conf_filename);
86 #ifndef _WIN32
87   printf("  -D, --daemon           starting like deamon\n");
88 #endif  /* _WIN32 */
89   printf("  -V, --version          show version\n");
90   printf("  -h, --help             this usage screen\n");
91 }
92
93 static void
94 main_options()
95 {
96   static struct option long_opts[] = {
97     { "uldev", 1, 0, 'd' },
98     { "conf", 1, 0, 'c' },
99     { "ns", 1, 0, 'n' },
100     { "ip", 1, 0, 'i' },
101 #ifndef _WIN32
102     { "daemon",  0, 0, 'D' },
103 #endif /* _WIN32 */
104     { "version",0,0, 'V' },
105     { "help",  0, 0, 'h' },
106     { 0, 0, 0, 0}
107   };
108   int  opt;
109
110  #ifndef HAS_GETOPT_LONG
111     while ((opt = getopt(ul_cont->argc, ul_cont->argv, "d:c:n:i:DVh")) != EOF)
112  #else
113     while ((opt = getopt_long(ul_cont->argc, ul_cont->argv, "d:c:n:i:DVh",
114                             &long_opts[0], NULL)) != EOF)
115  #endif
116     switch (opt) {
117     case 'd':
118       ul_cont->ul_dev_name=optarg;
119       break;
120     case 'c':
121       strcpy(ul_cont->conf_filename,optarg);
122       break;
123     case 'n':
124       ul_cont->orbit_ns_addr=optarg;
125       break;
126     case 'i':
127       ul_cont->orbit_orb_iiop_ipname=optarg;
128       break;
129     #ifndef _WIN32
130     case 'D':
131       daemon_flg=1;
132       break;  
133     #endif  /* _WIN32 */
134     case 'V':
135       printf("uLan address server - version 0.1\n");
136       exit(0);
137     case 'h':
138     default:
139       usage();
140       exit(opt == 'h' ? 0 : 1);
141   }
142 }
143
144 int main(int argc, char *argv[])
145 {
146   GError  *err    = NULL;
147   char basename[128];
148   char *filename="ul_asd.xml";
149
150   /**********************************************************/
151   /* Initizalization */
152   ul_cont->argc = argc;
153   ul_cont->argv = argv;
154   ul_cont->ul_dev_name=UL_DEV_NAME;
155   ul_cont->orbit_ns_addr=ORBIT_NS_ADDR_DEFAULT;
156   ul_cont->orbit_orb_iiop_ipname=ORBIT_ORBIIOPIPName_DEFAULT;
157
158 #ifdef _WIN32
159   GetWindowsDirectory(basename,sizeof(basename));
160 #else
161   strcpy(basename,"/etc/ulan/");
162 #endif
163   sprintf(ul_cont->conf_filename,"%s%s",basename,filename);
164
165   main_options();
166
167   //daemonize
168   if (daemon_flg) 
169     daemon_init();
170
171   if (ul_asd_server_init(ul_cont)<0) 
172     exit(1);
173
174   if (ul_asd_net_init(ul_cont)<0) 
175     exit(1);
176  
177 //  g_thread_init (NULL);
178   ul_cont->mutex = g_mutex_new (); 
179
180   ul_asd_conf_load(NULL,ul_cont); 
181
182   /**********************************************************/
183   /* Starting */
184   ul_cont->ul_server_thread = g_thread_create ((GThreadFunc) ul_asd_server_run, 
185                              ul_cont, 
186                              TRUE,   //joinable 
187                              &err);
188   ul_cont->ul_net_thread =   g_thread_create ((GThreadFunc) ul_asd_net_run, 
189                              ul_cont, 
190                              TRUE,   //joinable 
191                              &err);
192
193   /**********************************************************/
194   /* init signal handling */
195   signal(SIGINT,  sig_usr);
196   signal(SIGHUP,  sig_usr);
197
198   while(!terminate_flg) {
199     usleep(100*1000);
200   }
201   
202   //stop & cleanup server thread
203   ul_asd_server_stop(ul_cont);
204   g_thread_join (ul_cont->ul_server_thread);
205   ul_asd_server_cleanup(ul_cont);
206   
207   //stop & cleanup net thread
208   ul_asd_net_stop(ul_cont);
209   g_thread_join (ul_cont->ul_net_thread);
210   ul_asd_net_cleanup(ul_cont);
211
212   g_mutex_free (ul_cont->mutex);
213
214   return 0;
215 }