]> rtime.felk.cvut.cz Git - orte.git/blob - orte/manager/ortemanager.c
Better error handling in ORTEDomainCreate()
[orte.git] / orte / manager / ortemanager.c
1 /*
2  *  $Id: ortemanager.c,v 0.0.0.1        2003/10/07
3  *
4  *  DEBUG:  section                     Manager
5  *
6  *  -------------------------------------------------------------------  
7  *                                ORTE                                 
8  *                      Open Real-Time Ethernet                       
9  *                                                                    
10  *                      Copyright (C) 2001-2006                       
11  *  Department of Control Engineering FEE CTU Prague, Czech Republic  
12  *                      http://dce.felk.cvut.cz                       
13  *                      http://www.ocera.org                          
14  *                                                                    
15  *  Author:              Petr Smolik    petr.smolik@wo.cz             
16  *  Advisor:             Pavel Pisa                                   
17  *  Project Responsible: Zdenek Hanzalek                              
18  *  --------------------------------------------------------------------
19  *
20  *  This program is free software; you can redistribute it and/or modify
21  *  it under the terms of the GNU General Public License as published by
22  *  the Free Software Foundation; either version 2 of the License, or
23  *  (at your option) any later version.
24  *
25  *  This program is distributed in the hope that it will be useful,
26  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
27  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28  *  GNU General Public License for more details.
29  *
30  */
31  
32 #include "orte_all.h"
33
34 #ifndef CONFIG_ORTE_RT
35 //global variables
36 ORTEDomain          *d;
37 ORTEDomainProp      dp;
38 int32_t             opt,domain=ORTE_DEFAULT_DOMAIN;
39 Boolean             orteDaemon=ORTE_FALSE;
40 Boolean             orteWinService=ORTE_FALSE;
41 ORTEDomainAppEvents *events=NULL;
42
43 //event system
44 Boolean
45 onMgrAppRemoteNew(const struct ORTEAppInfo *appInfo, void *param) {
46   printf("%s 0x%x-0x%x was accepted\n",
47          (appInfo->appId & 0x3)==MANAGER ? "manager" : "application",
48          appInfo->hostId,appInfo->appId);
49   return ORTE_TRUE;
50 }
51
52 void
53 onMgrAppDelete(const struct ORTEAppInfo *appInfo, void *param) {
54   printf("%s 0x%x-0x%x was deleted\n",
55          (appInfo->appId & 0x3)==MANAGER ? "manager" : "application",
56          appInfo->hostId,appInfo->appId);
57 }
58
59 #ifdef _WIN32
60 //Windows service support
61 void serviceDispatchTable(void);  //forward declaration
62 void removeService(void);         //forward declaration
63 void installService(void);        //forward declaration
64 int managerInit(void) {
65   d=ORTEDomainMgrCreate(domain,&dp,events,ORTE_TRUE);
66   if (d==NULL) return -1;
67   return 0;
68 }
69 int managerStart(void) {
70   ORTEDomainStart(d,ORTE_TRUE,ORTE_FALSE,ORTE_FALSE,ORTE_FALSE,ORTE_TRUE);
71   return 0;
72 }
73 int managerStop(void) {
74   ORTEDomainMgrDestroy(d);
75   return 0;
76 }
77 #endif
78
79 #ifdef CONFIG_ORTE_UNIX
80 //Unix daemon support
81 pthread_mutex_t     mutex; //for wake up
82 pthread_cond_t      cond; //for wake up
83 int                 cvalue;
84 void sig_usr(int signo) {
85   if ((signo==SIGTERM) || (signo==SIGINT)) {
86     pthread_mutex_lock(&mutex);
87     cvalue=1;
88     pthread_cond_signal(&cond);
89     pthread_mutex_unlock(&mutex);
90   }
91 }
92 void waitForEndingCommand(void) {
93   pthread_mutex_init(&mutex, NULL);
94   pthread_cond_init(&cond, NULL);
95   cvalue=0;
96   signal(SIGTERM,sig_usr);
97   signal(SIGINT,sig_usr);
98   pthread_mutex_lock(&mutex);
99   while(cvalue==0)
100     pthread_cond_wait(&cond,&mutex);
101   pthread_mutex_unlock(&mutex);
102   pthread_mutex_destroy(&mutex);
103   pthread_cond_destroy(&cond);
104 }
105 static int daemonInit(void) {
106   pid_t pid;
107
108   if ((pid = fork()) < 0) {
109     return -1;
110   } else
111     if (pid != 0) {
112       exit(0);  /* parent vanishes */
113     }
114   /* child process */
115   setsid();
116   umask(0);
117   close(0);
118   close(1);
119   close(2);
120   return 0;
121 }
122 #endif
123
124 static void usage(void) {
125   printf("usage: ortemanager <parameters> \n");
126   printf("  -p, --peer <IPAdd:IPAdd:...>  possible locations of fellow managers\n");
127   printf("  -k, --key  <IPAdd:IPAdd:...>  manualy assigned manager's keys\n");
128   printf("  -d, --domain <domain>         working manager domain\n");
129   printf("  -v, --verbosity <level>       set verbosity level SECTION, up to LEVEL:...\n");
130   printf("      examples: ORTEManager -v 51.7:32.5 sections 51 and 32\n");
131   printf("                ORTEManager -v ALL.7     all sections up to level 7\n");
132   printf("  -R, --refresh <s>             refresh period in second(s)\n");
133   printf("  -P, --purge <s>               purge time in second(s)\n");
134   printf("  -I, --metaMulticast <IPAdd>   use multicast IPAddr for metatraffic comm.\n");
135   printf("  -t, --timetolive <number>     time-to-live for multicast packets\n");
136   printf("  -L, --listen <IPAdd>          IP address to listen on\n");
137 #ifdef CONFIG_ORTE_UNIX
138   printf("  -D, --daemon                  start program like daemon\n");
139 #endif
140   printf("  -E, --expiration <s>          expiration time of manager in second(s)\n");
141   printf("  -e, --events                  register event system\n");
142   printf("  -l, --logfile <filename>      set log file name\n");
143   printf("  -V, --version                 show version\n");
144 #ifdef _WIN32
145   printf("  -i, --install_service         install service into service manager on Windows\n");
146   printf("  -r, --remove_service          remove service from service manager on Windows\n");
147 #endif
148   printf("  -h, --help                    this usage screen\n");
149 }
150
151 int main(int argc,char *argv[]) {
152 #if defined HAVE_GETOPT_LONG || defined HAVE_GETOPT_LONG_ORTE
153   static struct option long_opts[] = {
154     { "peer",1,0, 'p' },
155     { "key",1,0, 'k' },
156     { "domain",1,0, 'd' },
157     { "verbosity",1,0, 'v' },
158     { "refresh",1,0, 'R' },
159     { "purge",1,0, 'P' },
160     { "metaMulticast",1,0, 'I' },
161     { "timetolive",1,0, 't' },
162     { "listen",1,0, 'L' },
163 #ifdef CONFIG_ORTE_UNIX
164     { "daemon",1,0, 'D' },
165 #endif
166     { "expiration",1,0, 'E' },
167     { "events",0,0, 'e' },
168     { "logfile",1,0, 'l' },
169     { "version",0,0, 'V' },
170     { "install_service",0,0, 'i' },
171     { "remove_service",0,0, 'r' },
172     { "help",  0, 0, 'h' },
173     { 0, 0, 0, 0}
174   };
175 #endif
176
177   ORTEInit();
178   ORTEDomainPropDefaultGet(&dp);
179
180 #if defined HAVE_GETOPT_LONG || defined HAVE_GETOPT_LONG_ORTE
181   while ((opt = getopt_long(argc, argv, "k:p:d:v:R:E:P:I:t:L:l:VhDesir",&long_opts[0], NULL)) != EOF) {
182 #else
183   while ((opt = getopt(argc, argv, "k:p:d:v:R:E:P:I:t:L:l:VhDesir")) != EOF) {
184 #endif
185     switch (opt) {
186       case 'p':
187         dp.mgrs=optarg;
188         break;
189       case 'k':
190         dp.keys=optarg;
191         break;
192       case 'd':
193         domain=strtol(optarg,NULL,0);
194         break;
195       case 'v':
196         ORTEVerbositySetOptions(optarg);
197         break;
198       case 'R':
199         NtpTimeAssembFromMs(dp.baseProp.refreshPeriod,strtol(optarg,NULL,0),0);
200         break;
201       case 'P':
202         NtpTimeAssembFromMs(dp.baseProp.purgeTime,strtol(optarg,NULL,0),0);
203         break;
204       case 'I':
205         dp.multicast.enabled=ORTE_TRUE;
206         dp.multicast.ipAddress=StringToIPAddress(optarg);
207         break;
208       case 'L':
209         dp.listen=StringToIPAddress(optarg);
210         break;
211       case 't':
212         dp.multicast.ttl=strtol(optarg,NULL,0);
213         break;
214       case 'E':
215         NtpTimeAssembFromMs(dp.baseProp.expirationTime,strtol(optarg,NULL,0),0);
216         break;
217       case 'e':
218         events=(ORTEDomainAppEvents*)malloc(sizeof(ORTEDomainAppEvents));
219         ORTEDomainInitEvents(events);
220         events->onMgrNew=onMgrAppRemoteNew;
221         events->onAppRemoteNew=onMgrAppRemoteNew;
222         events->onMgrDelete=onMgrAppDelete;
223         events->onAppDelete=onMgrAppDelete;
224         break;
225       case 'l':
226         ORTEVerbositySetLogFile(optarg);
227         break;
228       case 'V':
229         printf("Open Real-Time Ethernet (%s).\n",dp.version);
230         exit(0);
231         break;
232       case 'D':
233         orteDaemon=ORTE_TRUE;
234         break;
235       #ifdef _WIN32
236       case 's':
237         serviceDispatchTable();
238         exit(0);
239         break;
240       case 'i':
241         installService();
242         orteWinService=ORTE_TRUE;
243         break;
244       case 'r':
245         removeService();
246         exit(0);
247         break;
248       #endif
249       case 'h':
250       default:
251         usage();
252         exit(opt == 'h' ? 0 : 1);
253     }
254   }
255   
256   if (orteWinService) { 
257     exit(0);
258   }
259   
260   d=ORTEDomainMgrCreate(domain,&dp,events,ORTE_TRUE);
261   if (!d) {
262     perror("ORTEDomainMgrCreate");
263     exit(1);
264   }
265
266   #ifdef CONFIG_ORTE_UNIX
267   if (orteDaemon)
268     daemonInit();
269   #endif
270
271   ORTEDomainStart(d,ORTE_TRUE,ORTE_FALSE,ORTE_FALSE,ORTE_FALSE,ORTE_TRUE);
272   #ifndef CONFIG_ORTE_UNIX
273      while(1) ORTESleepMs(1000);
274   #endif
275
276   #ifdef CONFIG_ORTE_UNIX
277   waitForEndingCommand();
278   ORTEDomainMgrDestroy(d);
279   if (events) 
280     free(events);
281   #endif
282
283   exit(0);
284 }
285 #else
286 char *verbosity="";
287 MODULE_PARM(verbosity,"1s");
288 MODULE_PARM_DESC(verbosity,"set verbosity level SECTION, up to LEVEL:...");
289 char *peer="";
290 MODULE_PARM(peer,"1s");
291 MODULE_PARM_DESC(peer,"possible locations of fellow managers");
292 MODULE_LICENSE("GPL");
293 ORTEDomain *d=NULL;
294 pthread_t  thread;
295
296 void *
297 domainInit(void *arg) {
298   ORTEDomainProp dp;
299
300   ORTEDomainPropDefaultGet(&dp);
301   ORTEVerbositySetOptions(verbosity);
302   dp.mgrs=peer;
303   d=ORTEDomainMgrCreate(ORTE_DEFAULT_DOMAIN,&dp,NULL,ORTE_TRUE);
304   return arg;
305 }
306
307 void *
308 domainDestroy(void *arg) {
309   if (!d) return NULL;
310   ORTEDomainMgrDestroy(d);
311   return arg;
312 }
313
314 int
315 init_module(void) {
316   ORTEInit();
317   pthread_create(&thread,NULL,&domainInit,NULL);  //allocate resources in RT 
318   pthread_join(thread,NULL);
319   if (d)
320     ORTEDomainStart(d,ORTE_TRUE,ORTE_FALSE,ORTE_FALSE,ORTE_FALSE,ORTE_TRUE); //manager start
321   return 0;
322 }
323 void
324 cleanup_module(void) {
325   if (!d) return;
326   pthread_create(&thread,NULL,&domainDestroy,NULL);
327   pthread_join(thread,NULL);
328 }
329 #endif