]> rtime.felk.cvut.cz Git - orte.git/blob - orte/manager/ortemanager.c
Fix warning about implicit declaration of umask()
[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 #ifdef HAVE_SYS_STAT_H
81   #include <sys/stat.h>         /* For umask() */
82 #endif
83 //Unix daemon support
84 void waitForEndingCommand(void) {
85         sigset_t sigset;
86         sigemptyset(&sigset);
87         sigaddset(&sigset, SIGINT);
88         sigaddset(&sigset, SIGTERM);
89         sigwaitinfo(&sigset, NULL);
90 }
91 static int daemonInit(void) {
92   pid_t pid;
93
94   if ((pid = fork()) < 0) {
95     return -1;
96   } else
97     if (pid != 0) {
98       exit(0);  /* parent vanishes */
99     }
100   /* child process */
101   setsid();
102   umask(0);
103   close(0);
104   close(1);
105   close(2);
106   return 0;
107 }
108 #endif
109
110 static void usage(void) {
111   printf("usage: ortemanager <parameters> \n");
112   printf("  -p, --peer <IPAdd:IPAdd:...>  possible locations of fellow managers\n");
113   printf("  -k, --key  <IPAdd:IPAdd:...>  manualy assigned manager's keys\n");
114   printf("  -d, --domain <domain>         working manager domain\n");
115   printf("  -v, --verbosity <level>       set verbosity level SECTION, up to LEVEL:...\n");
116   printf("      examples: ORTEManager -v 51.7:32.5 sections 51 and 32\n");
117   printf("                ORTEManager -v ALL.7     all sections up to level 7\n");
118   printf("  -R, --refresh <s>             refresh period in second(s)\n");
119   printf("  -P, --purge <s>               purge time in second(s)\n");
120   printf("  -I, --metaMulticast <IPAdd>   use multicast IPAddr for metatraffic comm.\n");
121   printf("  -t, --timetolive <number>     time-to-live for multicast packets\n");
122   printf("  -L, --listen <IPAdd>          IP address to listen on\n");
123 #ifdef CONFIG_ORTE_UNIX
124   printf("  -D, --daemon                  start program like daemon\n");
125 #endif
126   printf("  -E, --expiration <s>          expiration time of manager in second(s)\n");
127   printf("  -e, --events                  register event system\n");
128   printf("  -l, --logfile <filename>      set log file name\n");
129   printf("  -V, --version                 show version\n");
130 #ifdef _WIN32
131   printf("  -i, --install_service         install service into service manager on Windows\n");
132   printf("  -r, --remove_service          remove service from service manager on Windows\n");
133 #endif
134   printf("  -h, --help                    this usage screen\n");
135 }
136
137 int main(int argc,char *argv[]) {
138 #if defined HAVE_GETOPT_LONG || defined HAVE_GETOPT_LONG_ORTE
139   static struct option long_opts[] = {
140     { "peer",1,0, 'p' },
141     { "key",1,0, 'k' },
142     { "domain",1,0, 'd' },
143     { "verbosity",1,0, 'v' },
144     { "refresh",1,0, 'R' },
145     { "purge",1,0, 'P' },
146     { "metaMulticast",1,0, 'I' },
147     { "timetolive",1,0, 't' },
148     { "listen",1,0, 'L' },
149 #ifdef CONFIG_ORTE_UNIX
150     { "daemon",1,0, 'D' },
151 #endif
152     { "expiration",1,0, 'E' },
153     { "events",0,0, 'e' },
154     { "logfile",1,0, 'l' },
155     { "version",0,0, 'V' },
156     { "install_service",0,0, 'i' },
157     { "remove_service",0,0, 'r' },
158     { "help",  0, 0, 'h' },
159     { 0, 0, 0, 0}
160   };
161 #endif
162
163   ORTEInit();
164   ORTEDomainPropDefaultGet(&dp);
165
166 #if defined HAVE_GETOPT_LONG || defined HAVE_GETOPT_LONG_ORTE
167   while ((opt = getopt_long(argc, argv, "k:p:d:v:R:E:P:I:t:L:l:VhDesir",&long_opts[0], NULL)) != EOF) {
168 #else
169   while ((opt = getopt(argc, argv, "k:p:d:v:R:E:P:I:t:L:l:VhDesir")) != EOF) {
170 #endif
171     switch (opt) {
172       case 'p':
173         dp.mgrs=optarg;
174         break;
175       case 'k':
176         dp.keys=optarg;
177         break;
178       case 'd':
179         domain=strtol(optarg,NULL,0);
180         break;
181       case 'v':
182         ORTEVerbositySetOptions(optarg);
183         break;
184       case 'R':
185         NtpTimeAssembFromMs(dp.baseProp.refreshPeriod,strtol(optarg,NULL,0),0);
186         break;
187       case 'P':
188         NtpTimeAssembFromMs(dp.baseProp.purgeTime,strtol(optarg,NULL,0),0);
189         break;
190       case 'I':
191         dp.multicast.enabled=ORTE_TRUE;
192         dp.multicast.ipAddress=StringToIPAddress(optarg);
193         break;
194       case 'L':
195         dp.listen=StringToIPAddress(optarg);
196         break;
197       case 't':
198         dp.multicast.ttl=strtol(optarg,NULL,0);
199         break;
200       case 'E':
201         NtpTimeAssembFromMs(dp.baseProp.expirationTime,strtol(optarg,NULL,0),0);
202         break;
203       case 'e':
204         events=(ORTEDomainAppEvents*)malloc(sizeof(ORTEDomainAppEvents));
205         ORTEDomainInitEvents(events);
206         events->onMgrNew=onMgrAppRemoteNew;
207         events->onAppRemoteNew=onMgrAppRemoteNew;
208         events->onMgrDelete=onMgrAppDelete;
209         events->onAppDelete=onMgrAppDelete;
210         break;
211       case 'l':
212         ORTEVerbositySetLogFile(optarg);
213         break;
214       case 'V':
215         printf("Open Real-Time Ethernet (%s).\n",dp.version);
216         exit(0);
217         break;
218       case 'D':
219         orteDaemon=ORTE_TRUE;
220         break;
221       #ifdef _WIN32
222       case 's':
223         serviceDispatchTable();
224         exit(0);
225         break;
226       case 'i':
227         installService();
228         orteWinService=ORTE_TRUE;
229         break;
230       case 'r':
231         removeService();
232         exit(0);
233         break;
234       #endif
235       case 'h':
236       default:
237         usage();
238         exit(opt == 'h' ? 0 : 1);
239     }
240   }
241   
242   if (orteWinService) { 
243     exit(0);
244   }
245   
246   d=ORTEDomainMgrCreate(domain,&dp,events,ORTE_TRUE);
247   if (!d) {
248     perror("ORTEDomainMgrCreate");
249     exit(1);
250   }
251
252   #ifdef CONFIG_ORTE_UNIX
253   if (orteDaemon)
254     daemonInit();
255   #endif
256
257   ORTEDomainStart(d,ORTE_TRUE,ORTE_FALSE,ORTE_FALSE,ORTE_FALSE,ORTE_TRUE);
258   #ifndef CONFIG_ORTE_UNIX
259      while(1) ORTESleepMs(1000);
260   #endif
261
262   #ifdef CONFIG_ORTE_UNIX
263   waitForEndingCommand();
264   ORTEDomainMgrDestroy(d);
265   if (events) 
266     free(events);
267   #endif
268
269   exit(0);
270 }
271 #else
272 char *verbosity="";
273 MODULE_PARM(verbosity,"1s");
274 MODULE_PARM_DESC(verbosity,"set verbosity level SECTION, up to LEVEL:...");
275 char *peer="";
276 MODULE_PARM(peer,"1s");
277 MODULE_PARM_DESC(peer,"possible locations of fellow managers");
278 MODULE_LICENSE("GPL");
279 ORTEDomain *d=NULL;
280 pthread_t  thread;
281
282 void *
283 domainInit(void *arg) {
284   ORTEDomainProp dp;
285
286   ORTEDomainPropDefaultGet(&dp);
287   ORTEVerbositySetOptions(verbosity);
288   dp.mgrs=peer;
289   d=ORTEDomainMgrCreate(ORTE_DEFAULT_DOMAIN,&dp,NULL,ORTE_TRUE);
290   return arg;
291 }
292
293 void *
294 domainDestroy(void *arg) {
295   if (!d) return NULL;
296   ORTEDomainMgrDestroy(d);
297   return arg;
298 }
299
300 int
301 init_module(void) {
302   ORTEInit();
303   pthread_create(&thread,NULL,&domainInit,NULL);  //allocate resources in RT 
304   pthread_join(thread,NULL);
305   if (d)
306     ORTEDomainStart(d,ORTE_TRUE,ORTE_FALSE,ORTE_FALSE,ORTE_FALSE,ORTE_TRUE); //manager start
307   return 0;
308 }
309 void
310 cleanup_module(void) {
311   if (!d) return;
312   pthread_create(&thread,NULL,&domainDestroy,NULL);
313   pthread_join(thread,NULL);
314 }
315 #endif