]> rtime.felk.cvut.cz Git - frescor/frsh-forb.git/blob - src/forb/src/forbrun/forbrun.c
forbrun: Exit whenever the first forb_main() returns
[frescor/frsh-forb.git] / src / forb / 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         exit(ret);
51         return NULL;
52 }
53
54 int main(int argc, char *argv[])
55 {
56         forb_orb orb;
57         bool opt_daemon = false;
58         char *opt_pidfile = NULL;
59         int i;
60         forb_init_attr_t attr = {
61                 .orb_id = "org.frescor.fcb",
62                 .peer_discovery_callback = NULL, /* TODO */
63                 .peer_dead_callback = NULL,      /* TODO */
64                 .fixed_tcp_port = 0,
65 #ifdef CONFIG_FORB_PROTO_INET_DEFAULT           
66                 .fixed_server_id = 0, /* TODO */
67                 .redistribute_hellos = false , /* TODO */
68 #endif
69         };
70         int  opt;
71
72         while ((opt = getopt_long(argc, argv, "d::hil:p:", &long_opts[0], NULL)) != EOF) {
73                 switch (opt) {
74 #if 0                   
75                         case 'l':
76                                 if (*optarg == '?') {
77                                         ul_logreg_for_each_domain(print_log_domain, NULL);
78                                         exit(0);
79                                 }
80                                 {
81                                         int ret;
82                                         ret = ul_log_domain_arg2levels(optarg);
83                                         if (ret) 
84                                                 error(1, EINVAL, "Error parsing -l argument at char %d\n", ret);
85                                 }
86                                 break;
87 #endif
88                         case 'd':
89                                 opt_daemon = true;
90                                 opt_pidfile = optarg;
91                                 break;
92                         case 'i':
93                                 attr.orb_id = optarg;
94                                 break;
95                         case 'h':
96                         /*default:*/
97                                 usage();
98                                 exit(opt == 'h' ? 0 : 1);
99                                 break;
100                         case 'p':
101                                 attr.fixed_tcp_port = atol(optarg);
102                                 break;
103                 }
104         }
105
106         if (opt_daemon)
107                 forb_daemon_prepare(opt_pidfile); /* == fork() */
108
109         orb = forb_init(&argc, &argv, &attr);
110         if (!orb) error(1, errno, "FORB initialization failed");
111
112 #if CONFIG_FCB_INET && !CONFIG_FORB_PROTO_INET_DEFAULT
113         ret = register_inet_port(orb);
114         if (ret) error(0, errno, "INET port registration failed");
115 #endif
116
117         for (i = optind; i < argc; i++) {
118                 void *handle;
119                 fosa_thread_id_t tid;
120                 struct forb_main_data data;
121
122                 data.filename = argv[i];
123                 handle = dlopen(data.filename, RTLD_LOCAL|RTLD_LAZY);
124                 if (!handle)
125                         error(1, errno, "dlopen(\"%s\") failed", data.filename);
126                 data.forb_main = dlsym(handle, "forb_main");
127                 if (!data.forb_main)
128                         error(1, errno, "Cannot find forb_main() in %s", data.filename);
129
130                 data.orb = orb;
131                 data.argc = 0;  /* TODO argc and argv after "--" */
132                 data.argv = NULL; /* TODO argc and argv after "--" */
133                 fosa_thread_create(&tid, NULL, forb_main_thread, &data);
134
135                 forb_wait_for_server_ready(orb);
136         }
137
138         if (opt_daemon)
139                 forb_daemon_ready();
140
141         /* Allow other threads to continue execution, but exit
142          * ourselves (without exit()ing the whole process. */
143         pthread_exit(NULL);     
144
145         return 0;
146 }