/* -*- 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 #include #include #include #include #include "miav_daemon.h" #include "miav_config.h" static const char version_str[] = "MIaV server 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 .\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"/miavd.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"/miavd.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, pidfile); }