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