diff options
author | Sergey Poznyakoff <gray@gnu.org.ua> | 2009-04-21 16:04:01 +0300 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org.ua> | 2009-04-21 16:09:41 +0300 |
commit | eb0b1eef83ca77da145264c81631299ffc4cc574 (patch) | |
tree | 60d62365877e47a6d80e15ef4b42430171b34e62 /src/main.c | |
parent | dbc2333c048514d0bbdade8a680c3933c7dc79a4 (diff) | |
download | tagr-eb0b1eef83ca77da145264c81631299ffc4cc574.tar.gz tagr-eb0b1eef83ca77da145264c81631299ffc4cc574.tar.bz2 |
Use "grecs" submodule for configuration parsing.
* Makefile.am (ACLOCAL_AMFLAGS): Add -I grecs/am.
(SUBDIRS): grecs
(ChangeLog): New rule.
* ChangeLog: Rename to ChangeLog.svn
* doc: Rename to etc.
* etc/upgrade.awk: New file.
* bootstrap.conf: Initialize "grecs".
* configure.ac: Call GRECS_SETUP.
(AC_CONFIG_FILES): Add grecs/Makefile and grecs/src/Makefile.
* gnulib.modules: Add gitlog-to-changelog and sysexits.
* src/Makefile.am (LDADD, INCLUDES): Add grecs.
(AM_CPPFLAGS): Define additional variables.
* src/log.c (use_stderr): Remove. Use log_to_stderr instead.
(grecs_print_diag): New function.
* src/main.c: Redo command-line support.
(listen_sockaddr): New global.
* src/readconfig.c: Rewrite.
* src/tagr.h: Include sysexits.h and grecs.h
(listen_sockaddr, log_to_stderr, pidfile): New externs.
* src/main.c, src/stat.c, src/report.c: Use sysexits codes.
Diffstat (limited to 'src/main.c')
-rw-r--r-- | src/main.c | 239 |
1 files changed, 173 insertions, 66 deletions
@@ -1,5 +1,5 @@ /* This file is part of tagr. - Copyright (C) 2000, 2005, 2006, Max Bouglacoff, Sergey Poznyakoff + Copyright (C) 2000, 2005, 2006, 2009 Max Bouglacoff, Sergey Poznyakoff This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -49,7 +49,13 @@ static RETSIGTYPE sig_fatal (int); static RETSIGTYPE sig_hup (int); static RETSIGTYPE sig_child (int); -char pidfile[] = TAGR_PIDFILE; +struct grecs_sockaddr listen_sockaddr; +int preprocess_only = 0; +int log_to_stderr = -1; +int test_template_option; +int lint_option; +char *pidfile = TAGR_PIDFILE; + char i_recv_buffer[reply_size (MAXADDR)]; char *recv_buffer = i_recv_buffer; char *configfile = TAGR_CONFIGFILE; @@ -77,31 +83,99 @@ static char args_doc[] = ""; enum { OPT_IMPORT = 256, - OPT_TEST_CONFIG, OPT_TEST_TEMPLATE, OPT_SHOW_DEFAULTS, + OPT_SYSLOG, + OPT_STDERR, + OPT_PREPROCESSOR, + OPT_NO_PREPROCESSOR, + OPT_DUMP_GRAMMAR_TRACE, + OPT_DUMP_LEX_TRACE, + OPT_CONFIG_HELP }; static struct argp_option options[] = { - {"foreground", 'f', NULL, 0, "Run in foreground mode", 0}, - {"config-file", 'c', "FILE", 0, "Read specified configuration FILE", 0}, - {"single-process", 's', NULL, 0, "Run in single-process mode", 0}, - {"html-template", 't', "FILE", 0, "Use given HTML template file", 0}, - {"test", 'T', "DEBUG-FLAGS", 0, - "Run in test mode. DEBUG-FLAGS are: 'c' to test configuration file syntax," - " 't' to test page template.", 0}, - {"test-config", OPT_TEST_CONFIG, NULL, 0, - "Test configuration file syntax (same as -Tc)", 0}, +#define GRID 10 + {NULL, 0, NULL, 0, + N_("Main operation mode"), GRID }, + {"lint", 't', NULL, 0, + N_("parse configuration file and exit"), GRID+1}, + {NULL, 'E', NULL, 0, + N_("preprocess configuration file and exit"), + GRID+1}, {"test-template", OPT_TEST_TEMPLATE, NULL, 0, - "Test page template file syntax (same as -Tt)", 0}, - {"user", 'u', "USER-NAME", 0, "Run with this user privileges", 0}, + N_("test page template file syntax"), GRID+1}, {"import", OPT_IMPORT, NULL, 0, - "Import old (mrtg-style) log files from DIR. If DIR is not given, use basedir value from the configuration file", 0 }, - {"rebuild", 'b', NULL, 0, "Rebuild graphs using existing statistics", 0}, - {"verbose", 'v', NULL, 0, "Increase verbosity level", 0}, - {"list", 'l', NULL, 0, "List contents of the rate database", 0}, + N_("import old (mrtg-style) log files from DIR (or the basedir, if not given)"), + GRID+1 }, + {"rebuild", 'b', NULL, 0, + N_("rebuild graphs using existing statistics"), GRID+1}, + {"list", 'l', NULL, 0, N_("list contents of the rate database"), GRID+1}, {"show-defaults", OPT_SHOW_DEFAULTS, NULL, 0, - "Show configuration default values", 0}, + N_("Show configuration default values"), GRID+1}, +#undef GRID + +#define GRID 20 + {NULL, 0, NULL, 0, + N_("Operation mode modifiers"), GRID }, + + {"foreground", 'f', NULL, 0, N_("run in foreground mode"), GRID+1}, + {"config-file", 'c', "FILE", 0, N_("read specified configuration FILE"), GRID+1}, + {"single-process", 's', NULL, 0, N_("run in single-process mode"), GRID+1}, +#undef GRID + +#define GRID 30 + {NULL, 0, NULL, 0, + N_("Logging"), GRID }, + + {"syslog", OPT_SYSLOG, NULL, 0, N_("log to syslog"), GRID+1}, + {"stderr", OPT_STDERR, NULL, 0, N_("log to stderr"), GRID+1}, +#undef GRID + +#define GRID 40 + {NULL, 0, NULL, 0, + N_("Preprocessor control"), GRID }, + + {"include-directory", 'I', N_("DIR"), 0, N_("add include directory"), + GRID+1}, + {"define", 'D', N_("SYMBOL[=VALUE]"), 0, N_("define a preprocessor symbol"), + GRID+1}, + {"preprocessor", OPT_PREPROCESSOR, N_("COMMAND"), 0, + N_("use COMMAND instead of the default preprocessor"), + GRID+1}, + {"no-preprocessor", OPT_NO_PREPROCESSOR, NULL, 0, + N_("disable preprocessing"), + GRID+1}, +#undef GRID + +#define GRID 50 + {NULL, 0, NULL, 0, + N_("Debugging"), GRID }, + + {"verbose", 'v', NULL, 0, N_("increase verbosity level"), GRID+1}, + {"dump-grammar-trace", OPT_DUMP_GRAMMAR_TRACE, NULL, 0, + N_("dump configuration grammar traces"), GRID+1 }, + {"dump-lex-trace", OPT_DUMP_LEX_TRACE, NULL, 0, + N_("dump lexical analyzer traces"), GRID+1 }, +#undef GRID + +#define GRID 60 + {NULL, 0, NULL, 0, + N_("Other settings"), GRID }, + + {"html-template", 'T', N_("FILE"), 0, + N_("use given HTML template file"), GRID+1}, + {"user", 'u', N_("USER-NAME"), 0, + N_("run with this user privileges"), GRID+1}, +#undef GRID + +#define GRID 70 + {NULL, 0, NULL, 0, + N_("Additional help"), GRID }, + {"config-help", OPT_CONFIG_HELP, NULL, 0, + N_("show configuration file summary"), GRID+1}, +#undef GRID + {NULL} }; @@ -145,22 +219,34 @@ parse_opt (int key, char *arg, struct argp_state *state) configfile = arg; break; + case 'E': + preprocess_only = 1; + break; + case 'l': list_option = 1; break; - + + case 'I': + grecs_preproc_add_include_dir (arg); + break; + + case 'D': + define_symbol (arg); + break; + case 's': single_process_option++; break; - + case 't': - html_template_option = arg; + lint_option = 1; break; - + case 'T': - check_mode = strdup (arg); + html_template_option = arg; break; - + case 'u': user_option = arg; break; @@ -168,17 +254,41 @@ parse_opt (int key, char *arg, struct argp_state *state) case 'v': verbose_level++; break; + + case OPT_CONFIG_HELP: + config_help (); + exit (0); + + case OPT_DUMP_GRAMMAR_TRACE: + grecs_gram_trace (1); + break; + + case OPT_DUMP_LEX_TRACE: + grecs_lex_trace (1); + break; case OPT_IMPORT: import_option++; break; - case OPT_TEST_CONFIG: - add_check_mode ('c'); + case OPT_PREPROCESSOR: + grecs_preprocessor = arg; + break; + + case OPT_NO_PREPROCESSOR: + grecs_preprocessor = NULL; break; + case OPT_SYSLOG: + log_to_stderr = 0; + break; + + case OPT_STDERR: + log_to_stderr = 1; + break; + case OPT_TEST_TEMPLATE: - add_check_mode ('t'); + test_template_option = 1; break; case OPT_SHOW_DEFAULTS: @@ -317,7 +427,7 @@ change_user () if (pwd) { if (change_privs (pwd->pw_uid, pwd->pw_gid)) - exit (1); + exit (EX_NOUSER); chdir (pwd->pw_dir); } @@ -326,21 +436,19 @@ change_user () #define CHECK_USAGE(cond, opt, mode_opt) \ if (cond) \ - die (1, "%s is meaningless with %s", opt, mode_opt); + die (EX_USAGE, "%s is meaningless with %s", opt, mode_opt); int main (int argc, char **argv) { int index, rc; - struct sockaddr salocal; - struct sockaddr_in *sin; fd_set read_fds; argp_program_bug_address = "<" PACKAGE_BUGREPORT ">"; init_syslog (argv[0]); if (argp_parse (&argp, argc, argv, 0, &index, NULL)) - exit (1); + exit (EX_USAGE); if (html_template_option) html_template = html_template_option; @@ -349,31 +457,31 @@ main (int argc, char **argv) argv += index; if (argc != 0 && !(list_option || import_option)) - die (1, "Too many arguments"); + die (EX_USAGE, "Too many arguments"); - if (check_mode) + if (readconfig ()) + exit (EX_CONFIG); + + if (lint_option) { - CHECK_USAGE (import_option, "--import", "--test"); - CHECK_USAGE (rebuild_option, "--rebuild", "--test"); - CHECK_USAGE (list_option, "--list", "--test"); - - for (; *check_mode; check_mode++) - switch (*check_mode) - { - case 'c': - exit (readconfig ()); - - case 't': - exit (check_template ()); - - default: - die (1, "Unknown check letter: %c", *check_mode); - } + CHECK_USAGE (import_option, "--import", "--lint"); + CHECK_USAGE (rebuild_option, "--rebuild", "--lint"); + CHECK_USAGE (list_option, "--list", "--lint"); + exit (0); } - - if (readconfig ()) - exit (2); + if (test_template_option) + { + CHECK_USAGE (import_option, "--import", "--test-template"); + CHECK_USAGE (rebuild_option, "--rebuild", "--test-template"); + CHECK_USAGE (list_option, "--list", "--test-template"); + exit (check_template ()); + } + + if (log_to_stderr == -1) + log_to_stderr = foreground && isatty (0); + grecs_log_to_stderr = log_to_stderr; + if (import_option) { if (argc) @@ -391,22 +499,22 @@ main (int argc, char **argv) if (rebuild_option || import_option || list_option) exit (0); + + if (listen_sockaddr.sa == NULL) + { + logmsg (L_CRIT, _("listener address is not configured")); + exit (EX_CONFIG); + } if (user_option) user = user_option; sockfd = socket (AF_INET, SOCK_DGRAM, 0); if (sockfd < 0) - die (1, "socket: %s", strerror (errno)); - - sin = (struct sockaddr_in *) &salocal; - memset (sin, 0, sizeof (salocal)); - sin->sin_family = AF_INET; - sin->sin_addr.s_addr = INADDR_ANY; - sin->sin_port = htons (port); + die (EX_OSERR, "socket: %s", strerror (errno)); - if (bind (sockfd, &salocal, sizeof (*sin)) < 0) - die (1, "bind: %s", strerror (errno)); + if (bind (sockfd, listen_sockaddr.sa, listen_sockaddr.len) < 0) + die (EX_OSERR, "bind: %s", strerror (errno)); signal (SIGHUP, sig_hup); signal (SIGUSR1, SIG_IGN); @@ -425,7 +533,7 @@ main (int argc, char **argv) pid = fork (); if (pid < 0) - die (1, "cannot fork: %s", strerror (errno)); + die (EX_OSERR, "cannot fork: %s", strerror (errno)); if (pid > 0) { /* Parent branch */ @@ -448,7 +556,6 @@ main (int argc, char **argv) for (i = 0; i < 3; i++) close (i); setsid (); - use_stderr = 0; } change_user (); @@ -562,7 +669,7 @@ sig_fatal (int sig) { logmsg (L_ERR, "FATAL: exiting on signal %d", sig); unlink (pidfile); - exit (1); + exit (EX_UNAVAILABLE); } RETSIGTYPE |