~panda-roux/MoonGem

ref: 89adac77208ddda2530fe98172b7b64209f74775 MoonGem/src/main.c -rw-r--r-- 1.4 KiB
89adac77 — panda-roux fixing build errors caused by missing headers when DISABLE_LOGGING is defined 7 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <errno.h>
#include <event2/event.h>
#include <stdlib.h>
#include <unistd.h>

#include "gemini.h"
#include "log.h"
#include "options.h"
#include "signals.h"

int main(int argc, const char** argv) {
  cli_options_t* options = parse_options(argc, argv);
  if (options == NULL) {
    return EXIT_FAILURE;
  }

#ifndef MOONGEM_ALLOW_ROOT
  if (getuid() == 0) {
    LOG_ERROR("MoonGem should not be run as root!  Terminating...");
    return EXIT_FAILURE;
  }
#endif

  // move the working directory to the user-defined root path if provided
  if (options->root != NULL) {
    chdir(options->root);
  }

  struct event_base* evtbase = event_base_new();
  if (evtbase == NULL) {
    LOG_ERROR("Failed to initialize event handler state");
    return EXIT_FAILURE;
  }

  struct event** evts = init_signal_handlers(evtbase);
  if (evts == NULL) {
    LOG_ERROR("Failed to initialize signal handlers");
    event_base_free(evtbase);
    return EXIT_FAILURE;
  }

  gemini_listener_t* gemini = init_gemini_listener(options, evtbase);
  if (gemini == NULL) {
    LOG_ERROR("Failed to initialize gemini listener");
    cleanup_signal_handlers(evts);
    event_base_free(evtbase);
  }

  // block while requests are handled
  event_base_dispatch(evtbase);

  cleanup_gemini_listener(gemini);
  cleanup_signal_handlers(evts);
  event_base_free(evtbase);

  destroy_options(options);

  return EXIT_SUCCESS;
}