summaryrefslogtreecommitdiff
path: root/src/miavd.cc
blob: 1658648aff246eea38961b335af38fe63dd9dddc (plain)
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/***************************************************************************
 *            miavd.cc
 *
 *  Sat Aug 21 17:32:24 2004
 *  Copyright  2004  deva
 *  deva@aasimon.org
 ****************************************************************************/

/*
 *    This file is part of MIaV.
 *
 *    MIaV is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 2 of the License, or
 *    (at your option) any later version.
 *
 *    MIaV is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with MIaV; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
 */
#include <stdio.h>
#include <string.h>
#include <getopt.h>

#include <hugin.hpp>

#include <config.h>

#include "miav_daemon.h"
#include "miav_config.h"

static const char version_str[] =
"Pentominos Dataserver - Miav v" VERSION "\n"
;

static const char copyright_str[] =
"Copyright (C) 2006-2007 Bent Bisballe Nyeng - Aasimon.org.\n"
"This is free software.  You may redistribute copies of it under the terms of\n"
"the GNU General Public License <http://www.gnu.org/licenses/gpl.html>.\n"
"There is NO WARRANTY, to the extent permitted by law.\n"
"\n"
"Written by Bent Bisballe Nyeng (deva@aasimon.org)\n"
;

static const char usage_str[] =
"Usage: %s [options]\n"
"Options:\n"
"  -c, --config file   Read configfile from 'file' (default "ETC"/miav.conf)\n"
"  -f, --foreground    Run in foreground mode (non-daemon mode)\n"
"  -u, --user user     Run as 'user' (overrides the configfile)\n"
"  -g, --group group   Run as 'group' (overrides the configfile)\n"
"  -v, --version       Print version information and exit.\n"
"  -h, --help          Print this message and exit.\n"
"  -D, --debug ddd     Enable debug messages on 'ddd'; see documentation for details\n"
"  -L, --logfile file  Write output to file, instead of stderr.\n"
  /*
"  -P, --pidfile file  Write pid of the running daemon to file.\n"
  */
;

int main(int argc, char *argv[])
{
  const char *hugin_filter = "+all";
  const char *logfile = NULL;
  int c;
  std::string configfile = ETC"/miav.conf";
  char *user = NULL;
  char *group = NULL;
  bool foreground = false;
  //  std::string pidfile;

  unsigned int hugin_flags = 0;

  int option_index = 0;
  while(1) {
    //    int this_option_optind = optind ? optind : 1;
    static struct option long_options[] = {
      {"foreground", no_argument, 0, 'f'},
      {"config", required_argument, 0, 'c'},
      {"user", required_argument, 0, 'u'},
      {"group", required_argument, 0, 'g'},
      {"help", no_argument, 0, 'h'},
      {"version", no_argument, 0, 'v'},
      {"debug", required_argument, 0, 'D'},
      //{"pidfile", required_argument, 0, 'P'},
      {"logfile", required_argument, 0, 'L'},
      {0, 0, 0, 0}
    };
    
    c = getopt_long (argc, argv, "hvfc:u:g:D:P:L:",
                     long_options, &option_index);
    
    if (c == -1)
      break;

    switch(c) {
    case 'L':
      hugin_flags |= HUG_FLAG_OUTPUT_TO_FILE;
      logfile = strdup(optarg);
      break;

    case 'c':
      configfile = optarg;
      break;

    case 'f':
      foreground = true;
      break;

    case 'u':
      user = strdup(optarg);
      break;

    case 'g':
      group = strdup(optarg);
      break;

    case 'D':
      hugin_flags |= HUG_FLAG_USE_FILTER;
      hugin_filter = optarg;
      break;
      /*
    case 'P':
      pidfile = optarg;
      break;
      */
    case '?':
    case 'h':
      printf("%s", version_str);
      printf(usage_str, argv[0]);
      return 0;

    case 'v':
      printf("%s", version_str);
      printf("%s", copyright_str);
      return 0;

    default:
      break;
    }
  }

  if(logfile == NULL) hugin_flags |= HUG_FLAG_OUTPUT_TO_STDOUT;

  hug_status_t status = hug_init(hugin_flags,
                                 HUG_OPTION_FILTER, hugin_filter,
                                 HUG_OPTION_FILENAME, logfile,
                                 HUG_OPTION_END);
  if(status != HUG_STATUS_OK) {
    printf("Error: %d\n", status);
    return 1;
  }

  MiavConfig cfg(configfile.c_str());
  config = &cfg; // Global config object

  int port = cfg.readInt("server_port");

  MiavDaemon daemon(port);

  const char *cuser = cfg.readString("server_user")->c_str();
  const char *cgroup = cfg.readString("server_group")->c_str();

  if(user) cuser = user;
  if(group) cgroup = group;

  return daemon.run(cuser, cgroup, foreground);
}