summaryrefslogtreecommitdiff
path: root/nslcd
diff options
context:
space:
mode:
authorArthur de Jong <arthur@arthurdejong.org>2013-02-23 17:01:55 +0100
committerArthur de Jong <arthur@arthurdejong.org>2013-02-23 17:25:50 +0100
commitefca5ca5599bbad982ac4f259047c7f07337951a (patch)
tree3d74fdfcf83418eb3bcfa131f7aec8d4b2031f94 /nslcd
parent22be9b0ba4b8ab3fbcd76bf5fce858779cc7056c (diff)
handle the log configuration option in nslcd
Diffstat (limited to 'nslcd')
-rw-r--r--nslcd/cfg.c52
-rw-r--r--nslcd/log.c46
-rw-r--r--nslcd/log.h3
-rw-r--r--nslcd/nslcd.c16
4 files changed, 104 insertions, 13 deletions
diff --git a/nslcd/cfg.c b/nslcd/cfg.c
index 651fd3f..afb4af7 100644
--- a/nslcd/cfg.c
+++ b/nslcd/cfg.c
@@ -282,6 +282,53 @@ static void handle_gid(const char *filename, int lnr,
exit(EXIT_FAILURE);
}
+static int parse_loglevel(const char *filename, int lnr, const char *value)
+{
+ if (strcasecmp(value, "crit") == 0)
+ return LOG_CRIT;
+ else if ((strcasecmp(value, "error") == 0) || (strcasecmp(value, "err") == 0))
+ return LOG_ERR;
+ else if (strcasecmp(value, "warning")==0)
+ return LOG_WARNING;
+ else if (strcasecmp(value, "notice")==0)
+ return LOG_NOTICE;
+ else if (strcasecmp(value, "info")==0)
+ return LOG_INFO;
+ else if (strcasecmp(value, "debug")==0)
+ return LOG_DEBUG;
+ else
+ {
+ log_log(LOG_ERR, "%s:%d: not a log level '%s'",
+ filename, lnr, value);
+ exit(EXIT_FAILURE);
+ }
+}
+
+static void handle_log(const char *filename, int lnr,
+ const char *keyword, char *line)
+{
+ int level = LOG_INFO;
+ char scheme[64];
+ char loglevel[32];
+ check_argumentcount(filename, lnr, keyword,
+ get_token(&line, scheme, sizeof(scheme)) != NULL);
+ if (get_token(&line, loglevel, sizeof(loglevel)) != NULL)
+ level = parse_loglevel(filename, lnr, loglevel);
+ get_eol(filename, lnr, keyword, &line);
+ if (strcasecmp(scheme, "none") == 0)
+ log_addlogging_none();
+ else if (strcasecmp(scheme, "syslog") == 0)
+ log_addlogging_syslog(level);
+ else if (scheme[0] == '/')
+ log_addlogging_file(level, scheme);
+ else
+ {
+ log_log(LOG_ERR, "%s:%d: %s: invalid argument '%s'",
+ filename, lnr, keyword, scheme);
+ exit(EXIT_FAILURE);
+ }
+}
+
/* add a single URI to the list of URIs in the configuration */
static void add_uri(const char *filename, int lnr,
struct ldap_config *cfg, const char *uri)
@@ -1066,6 +1113,10 @@ static void cfg_read(const char *filename, struct ldap_config *cfg)
{
handle_gid(filename, lnr, keyword, line, &cfg->gid);
}
+ else if (strcasecmp(keyword, "log") == 0)
+ {
+ handle_log(filename, lnr, keyword, line);
+ }
/* general connection options */
else if (strcasecmp(keyword, "uri") == 0)
{
@@ -1418,6 +1469,7 @@ static void cfg_dump(void)
log_log(LOG_DEBUG, "CFG: gid %d", nslcd_cfg->gid);
else
log_log(LOG_DEBUG, "CFG: # gid not set");
+ log_log_config();
for (i = 0; i < (NSS_LDAP_CONFIG_MAX_URIS + 1); i++)
if (nslcd_cfg->uris[i].uri != NULL)
log_log(LOG_DEBUG, "CFG: uri %s", nslcd_cfg->uris[i].uri);
diff --git a/nslcd/log.c b/nslcd/log.c
index bd7bf7d..0676cf6 100644
--- a/nslcd/log.c
+++ b/nslcd/log.c
@@ -42,6 +42,7 @@
/* storage for logging modes */
static struct log_cfg {
int loglevel;
+ const char *scheme;
FILE *fp; /* NULL == syslog */
struct log_cfg *next;
} *loglist = NULL;
@@ -80,7 +81,7 @@ void log_setdefaultloglevel(int loglevel)
}
/* add logging method to configuration list */
-static void addlogging(int loglevel, FILE *fp)
+static void addlogging(int loglevel, const char *scheme, FILE *fp)
{
struct log_cfg *tmp, *lst;
/* create new logstruct */
@@ -90,8 +91,9 @@ static void addlogging(int loglevel, FILE *fp)
log_log(LOG_CRIT, "malloc() returned NULL");
exit(EXIT_FAILURE);
}
- tmp->fp = fp;
tmp->loglevel = loglevel;
+ tmp->scheme = scheme;
+ tmp->fp = fp;
tmp->next = NULL;
/* save the struct in the list */
if (loglist == NULL)
@@ -107,6 +109,12 @@ static void addlogging(int loglevel, FILE *fp)
void log_addlogging_file(int loglevel, const char *filename)
{
FILE *fp;
+ filename = strdup(filename);
+ if (filename == NULL)
+ {
+ log_log(LOG_CRIT, "strdup() returned NULL");
+ exit(EXIT_FAILURE);
+ }
fp = fopen(filename, "a");
if (fp == NULL)
{
@@ -114,21 +122,21 @@ void log_addlogging_file(int loglevel, const char *filename)
filename, strerror(errno));
exit(1);
}
- addlogging(loglevel, fp);
+ addlogging(loglevel, filename, fp);
}
/* configure logging to syslog */
void log_addlogging_syslog(int loglevel)
{
openlog(PACKAGE, LOG_PID, LOG_DAEMON);
- addlogging(loglevel, NULL);
+ addlogging(loglevel, "syslog", NULL);
}
/* configure a null logging mode (no logging) */
void log_addlogging_none()
{
/* this is a hack, but it's so easy */
- addlogging(LOG_EMERG, NULL);
+ addlogging(LOG_EMERG, "none", NULL);
}
/* start the logging with the configured logging methods
@@ -294,3 +302,31 @@ void log_log(int pri, const char *format, ...)
}
}
}
+
+static const char *loglevel2str(int loglevel)
+{
+ switch (loglevel)
+ {
+ case LOG_CRIT: return "crit";
+ case LOG_ERR: return "error";
+ case LOG_WARNING: return "warning";
+ case LOG_NOTICE: return "notice";
+ case LOG_INFO: return "info";
+ case LOG_DEBUG: return "debug";
+ default: return "???";
+ }
+}
+
+/* log the logging configuration on DEBUG loglevel */
+void log_log_config(void)
+{
+ struct log_cfg *lst;
+ for (lst = loglist; lst != NULL; lst = lst->next)
+ {
+ if (lst->loglevel == LOG_EMERG)
+ log_log(LOG_DEBUG, "CFG: log %s", lst->scheme);
+ else
+ log_log(LOG_DEBUG, "CFG: log %s %s", lst->scheme,
+ loglevel2str(lst->loglevel));
+ }
+}
diff --git a/nslcd/log.h b/nslcd/log.h
index 3cf8cb1..277daa7 100644
--- a/nslcd/log.h
+++ b/nslcd/log.h
@@ -59,4 +59,7 @@ void log_setrequest(const char *format, ...)
void log_log(int pri, const char *format, ...)
LIKE_PRINTF(2, 3);
+/* log the logging configuration on DEBUG loglevel */
+void log_log_config(void);
+
#endif /* not NSLCD__LOG_H */
diff --git a/nslcd/nslcd.c b/nslcd/nslcd.c
index 0356f61..4be063a 100644
--- a/nslcd/nslcd.c
+++ b/nslcd/nslcd.c
@@ -681,6 +681,14 @@ int main(int argc, char *argv[])
#ifdef HAVE_PTHREAD_TIMEDJOIN_NP
struct timespec ts;
#endif /* HAVE_PTHREAD_TIMEDJOIN_NP */
+ /* close all file descriptors (except stdin/out/err) */
+ i = sysconf(_SC_OPEN_MAX) - 1;
+ /* if the system does not have OPEN_MAX just close the first 32 and
+ hope we closed enough */
+ if (i < 0)
+ i = 32;
+ for (; i > 3; i--)
+ close(i);
/* parse the command line */
parse_cmdline(argc, argv);
/* clean the environment */
@@ -727,14 +735,6 @@ int main(int argc, char *argv[])
NSLCD_PIDFILE, strerror(errno));
exit(EXIT_FAILURE);
}
- /* close all file descriptors (except stdin/out/err) */
- i = sysconf(_SC_OPEN_MAX) - 1;
- /* if the system does not have OPEN_MAX just close the first 32 and
- hope we closed enough */
- if (i < 0)
- i = 32;
- for (; i > 3; i--)
- close(i);
/* daemonize */
if ((!nslcd_debugging) && (daemon(0, 0) < 0))
{