]> rtime.felk.cvut.cz Git - frescor/frsh-forb.git/blob - src/forb/src/forbrun/forbrun.c
forbrun: Correctly handle arguments to -i switch
[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 <ul_logreg.h>
9 #include <stdbool.h>
10 #include <dlfcn.h>
11 #include <forb/forb-internal.h>
12 #include <pthread.h>
13
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' },
19     { 0, 0, 0, 0}
20 };
21
22 static void
23 usage(void)
24 {
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");
30 }
31
32 int print_log_domain(ul_log_domain_t *domain, void *context)
33 {
34         printf("%s = %d\n", domain->name, domain->level);
35         return 0;
36 }
37
38 struct forb_main_data {
39         char *filename;
40         forb_orb orb;
41         int (*forb_main)(forb_orb orb, int argc, char *argv[]);
42         int argc;
43         char **argv;
44 };
45
46 void *forb_main_thread(void *arg)
47 {
48         struct forb_main_data *data = arg;
49         int ret;
50         ret = data->forb_main(data->orb, data->argc, data->argv);
51         free(data);
52         exit(ret);
53         return NULL;
54 }
55
56 int main(int argc, char *argv[])
57 {
58         unsigned libs_loaded_cnt = 0;
59         forb_orb orb;
60         bool opt_daemon = false;
61         char *opt_pidfile = NULL;
62         int i, ret;
63         forb_init_attr_t attr = {
64                 .orb_id = "",
65                 .peer_discovery_callback = NULL, /* TODO */
66                 .peer_dead_callback = NULL,      /* TODO */
67                 .fixed_tcp_port = 0,
68 #ifdef CONFIG_FORB_PROTO_INET_DEFAULT           
69                 .fixed_server_id = 0, /* TODO */
70                 .redistribute_hellos = false , /* TODO */
71 #endif
72         };
73         int  opt;
74
75         while ((opt = getopt_long(argc, argv, "d::hi:l:p:", &long_opts[0], NULL)) != EOF) {
76                 switch (opt) {
77                         case 'l':
78                                 if (*optarg == '?') {
79                                         ul_logreg_for_each_domain(print_log_domain, NULL);
80                                         exit(0);
81                                 }
82                                 {
83                                         ret = ul_log_domain_arg2levels(optarg);
84                                         if (ret) 
85                                                 error(1, EINVAL, "Error parsing -l argument at char %d\n", ret);
86                                 }
87                                 break;
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
113 #if CONFIG_FCB_INET && !CONFIG_FORB_PROTO_INET_DEFAULT
114         ret = register_inet_port(orb);
115         if (ret) error(0, errno, "INET port registration failed");
116 #endif
117
118         for (i = optind; i < argc; i++) {
119                 void *handle;
120                 fosa_thread_id_t tid;
121                 struct forb_main_data *data = malloc(sizeof(*data));
122
123                 data->filename = argv[i];
124                 handle = dlopen(data->filename, RTLD_LOCAL|RTLD_LAZY);
125                 if (!handle)
126                         error(1, errno, "dlopen(\"%s\") failed", data->filename);
127                 data->forb_main = dlsym(handle, "forb_main");
128                 if (!data->forb_main)
129                         error(1, errno, "Cannot find forb_main() in %s", data->filename);
130
131                 data->orb = orb;
132                 
133                 // prepare forb_main() parameters
134                 data->argc = 0;
135                 data->argv = &argv[i];
136                 while (i < argc && strcmp(argv[i], "--")) {
137                         i++;
138                         data->argc++;
139                 }
140                 
141                 fosa_thread_create(&tid, NULL, forb_main_thread, data);
142                 libs_loaded_cnt++;
143
144                 ret = forb_wait_for_server_ready(orb);
145                 if (ret)
146                         error(1, errno, "forb_wait_for_server_ready");
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 }