1 #define WITH_C99 /* For ul_gsa.h iterators */
11 #include <forb/forb-internal.h>
14 static struct option long_opts[] = {
15 { "daemon", optional_argument, NULL, 'd' },
16 { "id", required_argument, NULL, 'i' },
17 { "loglevel", required_argument, NULL, 'l' },
18 { "port", required_argument, NULL, 'p' },
25 printf("usage: forbrun [ options ] -- <forb-server>.so [ options for forb_main() ] [-- ...]\n");
26 printf(" -d, --daemon [pid-file] go to background after initialization of servers\n");
27 printf(" -i, --id <textual orb id>\n");
28 printf(" -l, --loglevel <number>|<domain>=<number>,...\n");
29 printf(" -p, --port <number> listen on a fixed port number\n");
32 int print_log_domain(ul_log_domain_t *domain, void *context)
34 printf("%s = %d\n", domain->name, domain->level);
38 struct forb_main_data {
41 int (*forb_main)(forb_orb orb, int argc, char *argv[]);
46 void *forb_main_thread(void *arg)
48 struct forb_main_data *data = arg;
50 ret = data->forb_main(data->orb, data->argc, data->argv);
56 int main(int argc, char *argv[])
58 unsigned libs_loaded_cnt = 0;
60 bool opt_daemon = false;
61 char *opt_pidfile = NULL;
63 forb_init_attr_t attr = {
65 .peer_discovery_callback = NULL, /* TODO */
66 .peer_dead_callback = NULL, /* TODO */
68 #ifdef CONFIG_FORB_PROTO_INET_DEFAULT
69 .fixed_server_id = 0, /* TODO */
70 .redistribute_hellos = false , /* TODO */
75 while ((opt = getopt_long(argc, argv, "d::hil:p:x", &long_opts[0], NULL)) != EOF) {
79 ul_logreg_for_each_domain(print_log_domain, NULL);
84 ret = ul_log_domain_arg2levels(optarg);
86 error(1, EINVAL, "Error parsing -l argument at char %d\n", ret);
99 exit(opt == 'h' ? 0 : 1);
102 attr.fixed_tcp_port = atol(optarg);
108 forb_daemon_prepare(opt_pidfile); /* == fork() */
110 orb = forb_init(&argc, &argv, &attr);
111 if (!orb) error(1, errno, "FORB initialization failed");
114 #if CONFIG_FCB_INET && !CONFIG_FORB_PROTO_INET_DEFAULT
115 ret = register_inet_port(orb);
116 if (ret) error(0, errno, "INET port registration failed");
119 for (i = optind; i < argc; i++) {
121 fosa_thread_id_t tid;
122 struct forb_main_data *data = malloc(sizeof(*data));
124 data->filename = argv[i];
125 handle = dlopen(data->filename, RTLD_LOCAL|RTLD_LAZY);
127 error(1, errno, "dlopen(\"%s\") failed", data->filename);
128 data->forb_main = dlsym(handle, "forb_main");
129 if (!data->forb_main)
130 error(1, errno, "Cannot find forb_main() in %s", data->filename);
134 // prepare forb_main() parameters
136 data->argv = &argv[i];
137 while (i < argc && strcmp(argv[i], "--")) {
142 fosa_thread_create(&tid, NULL, forb_main_thread, data);
145 forb_wait_for_server_ready(orb);
151 /* Allow other threads to continue execution, but exit
152 * ourselves (without exit()ing the whole process. */
153 if (libs_loaded_cnt > 0)