]> rtime.felk.cvut.cz Git - frescor/forb.git/blob - src/forbrun/forbrun.c
74fd4fc131faeab780fdcbc2f015509e43e2dcf3
[frescor/forb.git] / src / forbrun / forbrun.c
1 #define WITH_C99                /* For ul_gsa.h iterators */
2 #include <getopt.h>
3 #include <forb.h>
4 #include <error.h>
5 #include <errno.h>
6 #include <stdio.h>
7 #include <ul_log.h>
8 #include <stdbool.h>
9 #include <dlfcn.h>
10 #include <forb/forb-internal.h>
11 #include <pthread.h>
12
13 static struct option long_opts[] = {
14     { "daemon",   optional_argument, NULL, 'd' },
15     { "id",       required_argument, NULL, 'i' },
16 /*     { "loglevel", required_argument, NULL, 'l' }, */
17     { "port",     required_argument, NULL, 'p' },
18     { 0, 0, 0, 0}
19 };
20
21 static void
22 usage(void)
23 {
24         printf("usage: forbrun [ options ] -- <forb-server>.so [ options for forb_main() ] [-- ...]\n");
25         printf("  -d, --daemon [pid-file]   go to background after initialization of servers\n");
26         printf("  -i, --id <orb id>         \n");
27 /*      printf("  -l, --loglevel <number>|<domain>=<number>,...\n"); */
28         printf("  -p, --port <number>       listen on a fixed port number\n");
29 }
30
31 int print_log_domain(ul_log_domain_t *domain, void *context)
32 {
33         printf("%s = %d\n", domain->name, domain->level);
34         return 0;
35 }
36
37 struct forb_main_data {
38         char *filename;
39         forb_orb orb;
40         int (*forb_main)(forb_orb orb, int argc, char *argv[]);
41         int argc;
42         char **argv;
43 };
44
45 void *forb_main_thread(void *arg)
46 {
47         struct forb_main_data *data = arg;
48         int ret;
49         ret = data->forb_main(data->orb, data->argc, data->argv);
50         free(data);
51         exit(ret);
52         return NULL;
53 }
54
55 int main(int argc, char *argv[])
56 {
57         unsigned libs_loaded_cnt = 0;
58         forb_orb orb;
59         bool opt_daemon = false;
60         char *opt_pidfile = NULL;
61         int i;
62         forb_init_attr_t attr = {
63                 .orb_id = "org.frescor.fcb",
64                 .peer_discovery_callback = NULL, /* TODO */
65                 .peer_dead_callback = NULL,      /* TODO */
66                 .fixed_tcp_port = 0,
67 #ifdef CONFIG_FORB_PROTO_INET_DEFAULT           
68                 .fixed_server_id = 0, /* TODO */
69                 .redistribute_hellos = false , /* TODO */
70 #endif
71         };
72         int  opt;
73
74         while ((opt = getopt_long(argc, argv, "d::hil:p:x", &long_opts[0], NULL)) != EOF) {
75                 switch (opt) {
76 #if 0                   
77                         case 'l':
78                                 if (*optarg == '?') {
79                                         ul_logreg_for_each_domain(print_log_domain, NULL);
80                                         exit(0);
81                                 }
82                                 {
83                                         int ret;
84                                         ret = ul_log_domain_arg2levels(optarg);
85                                         if (ret) 
86                                                 error(1, EINVAL, "Error parsing -l argument at char %d\n", ret);
87                                 }
88                                 break;
89 #endif
90                         case 'd':
91                                 opt_daemon = true;
92                                 opt_pidfile = optarg;
93                                 break;
94                         case 'i':
95                                 attr.orb_id = optarg;
96                                 break;
97                         case 'h':
98                         /*default:*/
99                                 usage();
100                                 exit(opt == 'h' ? 0 : 1);
101                                 break;
102                         case 'p':
103                                 attr.fixed_tcp_port = atol(optarg);
104                                 break;
105                 }
106         }
107
108         if (opt_daemon)
109                 forb_daemon_prepare(opt_pidfile); /* == fork() */
110
111         orb = forb_init(&argc, &argv, &attr);
112         if (!orb) error(1, errno, "FORB initialization failed");
113
114
115 #if CONFIG_FCB_INET && !CONFIG_FORB_PROTO_INET_DEFAULT
116         ret = register_inet_port(orb);
117         if (ret) error(0, errno, "INET port registration failed");
118 #endif
119
120         for (i = optind; i < argc; i++) {
121                 void *handle;
122                 fosa_thread_id_t tid;
123                 struct forb_main_data *data = malloc(sizeof(*data));
124
125                 data->filename = argv[i];
126                 handle = dlopen(data->filename, RTLD_LOCAL|RTLD_LAZY);
127                 if (!handle)
128                         error(1, errno, "dlopen(\"%s\") failed", data->filename);
129                 data->forb_main = dlsym(handle, "forb_main");
130                 if (!data->forb_main)
131                         error(1, errno, "Cannot find forb_main() in %s", data->filename);
132
133                 data->orb = orb;
134                 
135                 // prepare forb_main() parameters
136                 data->argc = 0;
137                 data->argv = &argv[i];
138                 while (i < argc && strcmp(argv[i], "--")) {
139                         i++;
140                         data->argc++;
141                 }
142                 
143                 fosa_thread_create(&tid, NULL, forb_main_thread, data);
144                 libs_loaded_cnt++;
145
146                 forb_wait_for_server_ready(orb);
147         }
148
149         if (opt_daemon)
150                 forb_daemon_ready();
151
152         /* Allow other threads to continue execution, but exit
153          * ourselves (without exit()ing the whole process. */
154         if (libs_loaded_cnt > 0)
155                 pthread_exit(NULL);     
156
157         return 0;
158 }