aboutsummaryrefslogtreecommitdiff
path: root/jabberd/main.c
diff options
context:
space:
mode:
authorSergey Poznyakoff <gray@gnu.org.ua>2007-08-19 13:30:35 +0000
committerSergey Poznyakoff <gray@gnu.org.ua>2007-08-19 13:30:35 +0000
commit84a1fcae3b0801a350e77038cfe01c8d0af95ff2 (patch)
tree753a258b08f715ec569ad92954b77135c757e629 /jabberd/main.c
parente147e576cbd4bf38fc838597b6297d9a2168191c (diff)
downloadgsc-84a1fcae3b0801a350e77038cfe01c8d0af95ff2.tar.gz
gsc-84a1fcae3b0801a350e77038cfe01c8d0af95ff2.tar.bz2
New module: wydawca
git-svn-id: file:///svnroot/gsc/trunk@276 d2de0444-eb31-0410-8365-af798a554d48
Diffstat (limited to 'jabberd/main.c')
-rw-r--r--jabberd/main.c322
1 files changed, 74 insertions, 248 deletions
diff --git a/jabberd/main.c b/jabberd/main.c
index 5a5b03b..6a98467 100644
--- a/jabberd/main.c
+++ b/jabberd/main.c
@@ -35,9 +35,7 @@ mode_t jabberd_umask = 037;
void
syslog_printer (int prio, const char *fmt, va_list ap)
{
-#ifdef USE_SYSLOG_ASYNC
- vsyslog_async (prio, fmt, ap);
-#elif HAVE_VSYSLOG
+#if HAVE_VSYSLOG
vsyslog (prio, fmt, ap);
#else
char buf[128];
@@ -49,43 +47,9 @@ syslog_printer (int prio, const char *fmt, va_list ap)
void
stderr_printer (int prio, const char *fmt, va_list ap)
{
- char *p = NULL;
+ char *p = gsc_syslog_priority_to_str (prio);
fprintf (stderr, "%s: ", progname);
- switch (prio) {
- case LOG_EMERG:
- p = "EMERG";
- break;
-
- case LOG_ALERT:
- p = "ALERT";
- break;
-
- case LOG_CRIT:
- p = "CRIT";
- break;
-
- case LOG_ERR:
- p = "ERR";
- break;
-
- case LOG_WARNING:
- p = "WARN";
- break;
-
- case LOG_NOTICE:
- p = "NOTICE";
- break;
-
- case LOG_INFO:
- p = "INFO";
- break;
-
- case LOG_DEBUG:
- p = "DEBUG";
- break;
- }
-
if (p)
fprintf (stderr, "[%s] ", p);
@@ -98,7 +62,7 @@ static void (*log_printer) (int prio, const char *fmt, va_list ap) =
stderr_printer;
void
-logmsg(int prio, char *fmt, ...)
+logmsg (int prio, char *fmt, ...)
{
va_list ap;
va_start (ap, fmt);
@@ -157,180 +121,32 @@ usage ()
}
-struct kw_int
-{
- char *name;
- int tok;
-};
-
-int
-syslog_to_n (struct kw_int *kw, char *str, int *pint)
-{
- int i;
-
- if (strncasecmp (str, "LOG_", 4) == 0)
- str += 4;
-
- for (; kw->name; kw++)
- if (strcasecmp (kw->name, str) == 0)
- {
- *pint = kw->tok;
- return 0;
- }
- return 1;
-}
-
-int
-str_to_facility (char *str, int *pfacility)
-{
- static struct kw_int kw_facility[] = {
- { "USER", LOG_USER },
- { "DAEMON", LOG_DAEMON },
- { "AUTH", LOG_AUTH },
- { "AUTHPRIV",LOG_AUTHPRIV },
- { "LOCAL0", LOG_LOCAL0 },
- { "LOCAL1", LOG_LOCAL1 },
- { "LOCAL2", LOG_LOCAL2 },
- { "LOCAL3", LOG_LOCAL3 },
- { "LOCAL4", LOG_LOCAL4 },
- { "LOCAL5", LOG_LOCAL5 },
- { "LOCAL6", LOG_LOCAL6 },
- { "LOCAL7", LOG_LOCAL7 },
- { "MAIL", LOG_MAIL },
- { NULL }
- };
- return syslog_to_n (kw_facility, str, pfacility);
-}
-
-int
-str_to_priority (char *str, int *pprio)
-{
- static struct kw_int kw_prio[] = {
- { "EMERG", LOG_EMERG },
- { "ALERT", LOG_ALERT },
- { "CRIT", LOG_CRIT },
- { "ERR", LOG_ERR },
- { "WARNING", LOG_WARNING },
- { "NOTICE", LOG_NOTICE },
- { "INFO", LOG_INFO },
- { "DEBUG", LOG_DEBUG },
- { NULL }
- };
- return syslog_to_n (kw_prio, str, pprio);
-}
-
-
-
/* Configuration file handling */
-struct cfg_file
-{
- FILE *fp;
- unsigned line;
- char *buf;
- size_t size;
-};
-
-typedef void (*cfg_fun) (struct cfg_file *file, char *kw, char *val, void *data);
-
-struct kw_handler
-{
- char *kw;
- cfg_fun handler;
-};
-
-static cfg_fun
-find_cfg_fun (struct kw_handler *kwtab, char *kw)
-{
- for (; kwtab->kw; kwtab++)
- if (strcmp (kwtab->kw, kw) == 0)
- return kwtab->handler;
- return NULL;
-}
-
-void
-parse_block (struct cfg_file *file, void *data,
- struct kw_handler *kwtab, const char *end)
-{
- while (getline (&file->buf, &file->size, file->fp) > 0)
- {
- size_t len;
- char *p, *kw, *val = NULL;
- cfg_fun fun;
-
- file->line++;
- for (p = file->buf; *p && isspace (*p); p++)
- ;
- if (*p == '#' || *p == 0)
- continue;
-
- len = strlen (p);
- if (p[len-1] == '\n')
- p[--len] = 0;
-
- for (;len > 0 && isspace (p[len-1]); len--)
- ;
-
- if (len > 0)
- p[len] = 0;
- else
- continue;
-
- kw = p;
- for (; *p && !isspace (*p); p++)
- ;
-
- if (*p)
- {
- *p++ = 0;
- for (; *p && isspace (*p); p++)
- ;
-
- val = p;
- }
-
- if (end && val == NULL && strcmp (kw, end) == 0)
- break;
-
- fun = find_cfg_fun (kwtab, kw);
- if (fun)
- fun (file, kw, val, data);
- else
- {
-#ifdef ALLOW_LEGACY_CONFIG
- logmsg (LOG_WARNING, "%s:%u: legacy line", config_file, file->line);
- register_jabber_process (kw, val);
-#else
- logmsg (LOG_ERR, "%s:%u: unrecognized line", config_file, file->line);
-#endif
- }
- }
-}
-
void
-cfg_syslog_tag (struct cfg_file *file, char *kw, char *val, void *unused)
+cfg_syslog_tag (gsc_config_file_t *file, char *kw, char *val, void *unused)
{
syslog_tag = strdup (val);
}
void
-cfg_syslog_facility (struct cfg_file *file, char *kw, char *val, void *unused)
+cfg_syslog_facility (gsc_config_file_t *file, char *kw, char *val, void *unused)
{
- if (str_to_facility (val, &log_facility))
- logmsg (LOG_ERR, "%s:%u: Unknown facility `%s'",
- config_file, file->line, val);
+ if (gsc_str_to_syslog_facility (val, &log_facility))
+ file->error_msg (file->file_name, file->line, "Unknown facility `%s'",
+ val);
}
void
-cfg_user (struct cfg_file *file, char *kw, char *val, void *unused)
+cfg_user (gsc_config_file_t *file, char *kw, char *val, void *unused)
{
struct passwd *pwd = getpwnam (val);
if (!pwd)
- logmsg (LOG_ERR, "%s:%u: no such user `%s'",
- config_file, file->line, val);
+ file->error_msg (file->file_name, file->line, "no such user `%s'",
+ val);
else if (pwd->pw_uid == 0)
- logmsg (LOG_ERR, "%s:%u: user `%s' has zero UID", config_file,
- file->line, val);
+ file->error_msg (file->file_name, file->line, "user `%s' has zero UID",
+ val);
else
user = strdup (val);
}
@@ -344,7 +160,7 @@ struct group_list
static struct group_list *group_list;
void
-cfg_group (struct cfg_file *file, char *kw, char *val, void *unused)
+cfg_group (gsc_config_file_t *file, char *kw, char *val, void *unused)
{
struct group *group = getgrnam (val);
if (group)
@@ -355,42 +171,42 @@ cfg_group (struct cfg_file *file, char *kw, char *val, void *unused)
group_list = p;
}
else
- logmsg(LOG_ERR, "%s:%u: unknown group `%s'", config_file,
- file->line, val);
+ file->error_msg (file->file_name, file->line, "unknown group `%s'",
+ val);
}
void
-cfg_pidfile (struct cfg_file *file, char *kw, char *val, void *unused)
+cfg_pidfile (gsc_config_file_t *file, char *kw, char *val, void *unused)
{
pidfile = strdup (val);
}
void
-cfg_ctlfile (struct cfg_file *file, char *kw, char *val, void *unused)
+cfg_ctlfile (gsc_config_file_t *file, char *kw, char *val, void *unused)
{
ctlfile = strdup (val);
}
void
-cfg_statfile (struct cfg_file *file, char *kw, char *val, void *unused)
+cfg_statfile (gsc_config_file_t *file, char *kw, char *val, void *unused)
{
ctlfile = strdup (val);
}
void
-cfg_umask (struct cfg_file *file, char *kw, char *val, void *unused)
+cfg_umask (gsc_config_file_t *file, char *kw, char *val, void *unused)
{
char *p;
unsigned long n = strtoul (val, &p, 8);
if (*p)
- logmsg(LOG_ERR, "%s:%u: invalid umask; stopped near `%s'", config_file,
- file->line, p);
+ file->error_msg (file->file_name, file->line,
+ "invalid umask; stopped near `%s'", p);
else
jabberd_umask = (mode_t) n;
}
void
-cfg_prog (struct cfg_file *file, char *kw, char *val, void *unused)
+cfg_prog (gsc_config_file_t *file, char *kw, char *val, void *unused)
{
char *prog = val;
char *p = val;
@@ -424,53 +240,53 @@ struct transport_rec
};
static void
-cfg_transport_command (struct cfg_file *file, char *kw, char *val, void *data)
+cfg_transport_command (gsc_config_file_t *file, char *kw, char *val, void *data)
{
struct transport_rec *prec = data;
prec->command = strdup (val);
}
void
-cfg_transport_facility (struct cfg_file *file, char *kw, char *val, void *data)
+cfg_transport_facility (gsc_config_file_t *file, char *kw, char *val, void *data)
{
struct transport_rec *prec = data;
- if (str_to_facility (val, &prec->facility))
- logmsg (LOG_ERR, "%s:%u: Unknown facility `%s'",
- config_file, file->line, val);
+ if (gsc_str_to_syslog_facility (val, &prec->facility))
+ file->error_msg (file->file_name, file->line,
+ "Unknown facility `%s'",
+ val);
}
void
-cfg_transport_stdout (struct cfg_file *file, char *kw, char *val, void *data)
+cfg_transport_stdout (gsc_config_file_t *file, char *kw, char *val, void *data)
{
struct transport_rec *prec = data;
- if (str_to_priority (val, &prec->retr[RETR_OUT]))
- logmsg (LOG_ERR, "%s:%u: Unknown priority `%s'",
- config_file, file->line, val);
+ if (gsc_str_to_syslog_priority (val, &prec->retr[RETR_OUT]))
+ file->error_msg (file->file_name, file->line, "Unknown priority `%s'",
+ val);
}
void
-cfg_transport_stderr (struct cfg_file *file, char *kw, char *val, void *data)
+cfg_transport_stderr (gsc_config_file_t *file, char *kw, char *val, void *data)
{
struct transport_rec *prec = data;
- if (str_to_priority (val, &prec->retr[RETR_ERR]))
- logmsg (LOG_ERR, "%s:%u: Unknown priority `%s'",
- config_file, file->line, val);
+ if (gsc_str_to_syslog_priority (val, &prec->retr[RETR_ERR]))
+ file->error_msg (file->file_name, file->line, "Unknown priority `%s'",
+ val);
}
void
-cfg_transport_depend (struct cfg_file *file, char *kw, char *val, void *data)
+cfg_transport_depend (gsc_config_file_t *file, char *kw, char *val, void *data)
{
struct transport_rec *prec = data;
int rc;
if (rc = argcv_get (val, NULL, NULL, &prec->depc, &prec->depv))
- {
- logmsg (LOG_ERR, "%s:%u: cannot split dependency line: %s",
- config_file, file->line, strerror (rc));
- }
+ file->error_msg (file->file_name, file->line,
+ "cannot split dependency line: %s",
+ strerror (rc));
}
void
-cfg_transport_pidfile (struct cfg_file *file, char *kw, char *val,
+cfg_transport_pidfile (gsc_config_file_t *file, char *kw, char *val,
void *data)
{
struct transport_rec *prec = data;
@@ -478,14 +294,14 @@ cfg_transport_pidfile (struct cfg_file *file, char *kw, char *val,
}
void
-cfg_transport (struct cfg_file *file, char *kw, char *val, void *unused)
+cfg_transport (gsc_config_file_t *file, char *kw, char *val, void *unused)
{
int rc;
int argc;
char **argv;
struct transport_rec rec;
- static struct kw_handler kwtab[] = {
+ static struct gsc_config_keyword kwtab[] = {
{ "command", cfg_transport_command },
{ "stdout", cfg_transport_stdout },
{ "stderr", cfg_transport_stderr },
@@ -499,12 +315,13 @@ cfg_transport (struct cfg_file *file, char *kw, char *val, void *unused)
if (val)
rec.tag = strdup (val);
- parse_block (file, &rec, kwtab, "end");
+ gsc_config_parse_block (file, &rec, kwtab, "end");
if (rc = argcv_get (rec.command, NULL, NULL, &argc, &argv))
{
- logmsg (LOG_ERR, "%s:%u: cannot split command line: %s",
- config_file, file->line, strerror (rc));
+ file->error_msg (file->file_name, file->line,
+ "cannot split command line: %s",
+ strerror (rc));
return;
}
register_transport (rec.tag, argv, rec.retr, rec.depv, rec.pidfile);
@@ -516,17 +333,17 @@ cfg_transport (struct cfg_file *file, char *kw, char *val, void *unused)
}
void
-cfg_shutdown_timeout (struct cfg_file *file, char *kw, char *val, void *unused)
+cfg_shutdown_timeout (gsc_config_file_t *file, char *kw, char *val, void *unused)
{
char *p;
shutdown_timeout = strtoul (val, &p, 10);
if (*p)
- logmsg (LOG_ERR, "%s:%u: unrecognized timeout value, stopped near %s",
- config_file, file->line, p);
+ file->error_msg (file->file_name, file->line,
+ "unrecognized timeout value, stopped near %s", p);
}
-struct kw_handler kw_handler[] = {
+struct gsc_config_keyword kw_handler[] = {
{ "syslog-tag", cfg_syslog_tag },
{ "syslog-facility", cfg_syslog_facility },
{ "user", cfg_user },
@@ -543,26 +360,35 @@ struct kw_handler kw_handler[] = {
};
void
+jabber_cfg_error (const char *name, unsigned line, const char *fmt, ...)
+{
+ va_list ap;
+ size_t n;
+ char buffer[128];
+
+ n = snprintf (buffer, sizeof buffer, "%s:%u: ", name, line);
+ va_start (ap, fmt);
+ vsnprintf (buffer + n, sizeof buffer - n, fmt, ap);
+ va_end (ap);
+ logmsg (LOG_ERR, "%s", buffer);
+}
+
+void
parse_config ()
{
- struct cfg_file file;
-
- file.fp = fopen (config_file, "r");
- file.buf = NULL;
- file.size = 0;
- file.line = 0;
-
- if (!file.fp)
+ switch (gsc_config_parse (config_file, jabber_cfg_error, kw_handler))
{
+ case gsc_config_success:
+ break;
+
+ case gsc_config_open:
logmsg (LOG_EMERG, "cannot open config file `%s': %s",
config_file, strerror (errno));
exit (1);
- }
-
- parse_block (&file, NULL, kw_handler, NULL);
- free (file.buf);
- fclose (file.fp);
+ case gsc_config_error:
+ exit (1);
+ }
}

Return to:

Send suggestions and report system problems to the System administrator.