rough framework
[lemu] / main.c
1 /*
2 * toy server
3 *
4 */
5
6 #include <stdlib.h>
7 #include <stdio.h>
8 #include <string.h>
9 #include <errno.h>
10 #include <pthread.h>
11 #include <arpa/inet.h>
12
13 #include <event2/event.h>
14 #include <event2/thread.h>
15
16 #include <openssl/ssl.h>
17 #include <openssl/crypto.h>
18 #include <openssl/err.h>
19
20 #include "version.h"
21 #include "notify.h"
22 #include "server.h"
23
24 #define DEFAULT_CONFIG_FILE "conf/lemu.conf"
25
26 #define USAGE_FLAGS_LONG (1<<0)
27 static void
28 usage_(char *prog, unsigned int usage_flags)
29 {
30 char *x;
31
32 x = strrchr(prog, '/');
33 if (x && *(x + 1)) {
34 prog = x + 1;
35 }
36
37 printf("Usage: %s [-c config_file]\n", prog);
38 if (! (usage_flags & USAGE_FLAGS_LONG)) {
39 return;
40 }
41 printf(
42 "\t-h \t : this help\n"
43 "\t-c config_file\t : loads the specified config file [default: '" DEFAULT_CONFIG_FILE "']\n"
44 );
45 }
46
47 int
48 main(int argc, char **argv)
49 {
50 struct server *server;
51 char *conf_file = DEFAULT_CONFIG_FILE;
52 int opt;
53 int r;
54
55 /* enable threading in libevent before anything tries to use it */
56 if ( (r = evthread_use_pthreads()) ) {
57 NOTIFY_ERROR("%s:%d", "evthread_use_pthreads", r);
58 }
59
60 SSL_load_error_strings();
61 SSL_library_init();
62 OpenSSL_add_all_algorithms();
63
64 while ( (opt = getopt(argc, argv, "c:h")) != -1 ) {
65 switch (opt) {
66 case 'c':
67 conf_file = optarg;
68 break;
69 case 'h':
70 usage_(argv[0], USAGE_FLAGS_LONG);
71 exit(EXIT_SUCCESS);
72 break;
73 default:
74 usage_(argv[0], 0);
75 exit(EXIT_FAILURE);
76 }
77 }
78
79 server = server_new();
80 if (!server) {
81 NOTIFY_FATAL("server_new");
82 exit(EXIT_FAILURE);
83 }
84
85 if (server_init(server, conf_file)) {
86 NOTIFY_FATAL("error parsing config file '%s'", conf_file);
87 exit(EXIT_FAILURE);
88 }
89
90 server_free(server);
91
92 exit(EXIT_SUCCESS);
93 }