Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/lib/libc/gen A little more restructuring so that we don't ne...
details: https://anonhg.NetBSD.org/src/rev/1a151f497129
branches: trunk
changeset: 350458:1a151f497129
user: christos <christos%NetBSD.org@localhost>
date: Thu Jan 12 01:58:39 2017 +0000
description:
A little more restructuring so that we don't need mutex stuff in syslog_ss.
diffstat:
lib/libc/gen/syslog.c | 69 +++++++++++++++-
lib/libc/gen/syslog_private.h | 7 +-
lib/libc/gen/syslog_ss.c | 18 +++-
lib/libc/gen/xsyslog.c | 187 ++++++++++++++---------------------------
4 files changed, 151 insertions(+), 130 deletions(-)
diffs (truncated from 420 to 300 lines):
diff -r e246412305f1 -r 1a151f497129 lib/libc/gen/syslog.c
--- a/lib/libc/gen/syslog.c Thu Jan 12 01:58:03 2017 +0000
+++ b/lib/libc/gen/syslog.c Thu Jan 12 01:58:39 2017 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: syslog.c,v 1.56 2017/01/12 00:38:01 christos Exp $ */
+/* $NetBSD: syslog.c,v 1.57 2017/01/12 01:58:39 christos Exp $ */
/*
* Copyright (c) 1983, 1988, 1993
@@ -34,7 +34,7 @@
#if 0
static char sccsid[] = "@(#)syslog.c 8.5 (Berkeley) 4/29/95";
#else
-__RCSID("$NetBSD: syslog.c,v 1.56 2017/01/12 00:38:01 christos Exp $");
+__RCSID("$NetBSD: syslog.c,v 1.57 2017/01/12 01:58:39 christos Exp $");
#endif
#endif /* LIBC_SCCS and not lint */
@@ -67,6 +67,15 @@
__weak_alias(vsyslog,_vsyslog)
__weak_alias(syslogp,_syslogp)
__weak_alias(vsyslogp,_vsyslogp)
+__weak_alias(closelog,_closelog)
+__weak_alias(openlog,_openlog)
+__weak_alias(setlogmask,_setlogmask)
+#endif
+
+static struct syslog_data _syslog_data = SYSLOG_DATA_INIT;
+
+#ifdef _REENTRANT
+static mutex_t syslog_mutex = MUTEX_INITIALIZER;
#endif
static size_t
@@ -103,6 +112,24 @@
return (size_t)(p - op);
}
+static int
+lock(const struct syslog_data *data)
+{
+ int rv = data == &_syslog_data;
+ if (rv)
+ mutex_lock(&syslog_mutex);
+ return rv;
+}
+
+static int
+unlock(const struct syslog_data *data)
+{
+ int rv = data == &_syslog_data;
+ if (rv)
+ mutex_unlock(&syslog_mutex);
+ return rv;
+}
+
static struct syslog_fun _syslog_fun = {
timefun,
strerror_r,
@@ -111,8 +138,46 @@
#else
vsnprintf,
#endif
+ lock,
+ unlock,
};
+void
+openlog(const char *ident, int logstat, int logfac)
+{
+ openlog_r(ident, logstat, logfac, &_syslog_data);
+}
+
+void
+closelog(void)
+{
+ closelog_r(&_syslog_data);
+}
+
+/* setlogmask -- set the log mask level */
+int
+setlogmask(int pmask)
+{
+ return setlogmask_r(pmask, &_syslog_data);
+}
+
+void
+openlog_r(const char *ident, int logstat, int logfac, struct syslog_data *data)
+{
+ lock(data);
+ _openlog_unlocked_r(ident, logstat, logfac, data);
+ unlock(data);
+}
+
+void
+closelog_r(struct syslog_data *data)
+{
+ lock(data);
+ _closelog_unlocked_r(data);
+ data->log_tag = NULL;
+ unlock(data);
+}
+
/*
* syslog, vsyslog --
* print message on log file; output is intended for syslogd(8).
diff -r e246412305f1 -r 1a151f497129 lib/libc/gen/syslog_private.h
--- a/lib/libc/gen/syslog_private.h Thu Jan 12 01:58:03 2017 +0000
+++ b/lib/libc/gen/syslog_private.h Thu Jan 12 01:58:39 2017 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: syslog_private.h,v 1.1 2017/01/12 00:38:01 christos Exp $ */
+/* $NetBSD: syslog_private.h,v 1.2 2017/01/12 01:58:39 christos Exp $ */
/*-
* Copyright (c) 2016 The NetBSD Foundation, Inc.
@@ -41,10 +41,13 @@
size_t (*timefun)(char *, size_t);
int (*errfun)(int, char *, size_t);
int __printflike(3, 0) (*prfun)(char *, size_t, const char *, va_list);
+ int (*lock)(const struct syslog_data *);
+ int (*unlock)(const struct syslog_data *);
};
void _vxsyslogp_r(int , struct syslog_fun *, struct syslog_data *,
const char *, const char *, const char *, va_list);
+void _openlog_unlocked_r(const char *, int, int, struct syslog_data *);
+void _closelog_unlocked_r(struct syslog_data *);
-extern struct syslog_data _syslog_data;
extern struct syslog_fun _syslog_ss_fun;
diff -r e246412305f1 -r 1a151f497129 lib/libc/gen/syslog_ss.c
--- a/lib/libc/gen/syslog_ss.c Thu Jan 12 01:58:03 2017 +0000
+++ b/lib/libc/gen/syslog_ss.c Thu Jan 12 01:58:39 2017 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: syslog_ss.c,v 1.1 2017/01/12 00:38:01 christos Exp $ */
+/* $NetBSD: syslog_ss.c,v 1.2 2017/01/12 01:58:39 christos Exp $ */
/*-
* Copyright (c) 2016 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
*/
#include <sys/cdefs.h>
#if defined(LIBC_SCCS) && !defined(lint)
-__RCSID("$NetBSD: syslog_ss.c,v 1.1 2017/01/12 00:38:01 christos Exp $");
+__RCSID("$NetBSD: syslog_ss.c,v 1.2 2017/01/12 01:58:39 christos Exp $");
#endif /* LIBC_SCCS and not lint */
#include "namespace.h"
@@ -54,10 +54,24 @@
#endif
}
+static int
+lock_ss(const struct syslog_data *data __unused)
+{
+ return 0;
+}
+
+static int
+unlock_ss(const struct syslog_data *data __unused)
+{
+ return 0;
+}
+
struct syslog_fun _syslog_ss_fun = {
timefun_ss,
strerror_r_ss,
vsnprintf_ss,
+ lock_ss,
+ unlock_ss,
};
void
diff -r e246412305f1 -r 1a151f497129 lib/libc/gen/xsyslog.c
--- a/lib/libc/gen/xsyslog.c Thu Jan 12 01:58:03 2017 +0000
+++ b/lib/libc/gen/xsyslog.c Thu Jan 12 01:58:39 2017 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: xsyslog.c,v 1.1 2017/01/12 00:38:01 christos Exp $ */
+/* $NetBSD: xsyslog.c,v 1.2 2017/01/12 01:58:39 christos Exp $ */
/*
* Copyright (c) 1983, 1988, 1993
@@ -34,7 +34,7 @@
#if 0
static char sccsid[] = "@(#)syslog.c 8.5 (Berkeley) 4/29/95";
#else
-__RCSID("$NetBSD: xsyslog.c,v 1.1 2017/01/12 00:38:01 christos Exp $");
+__RCSID("$NetBSD: xsyslog.c,v 1.2 2017/01/12 01:58:39 christos Exp $");
#endif
#endif /* LIBC_SCCS and not lint */
@@ -58,40 +58,70 @@
#include "reentrant.h"
#include "extern.h"
-#ifdef __weak_alias
-__weak_alias(closelog,_closelog)
-__weak_alias(openlog,_openlog)
-__weak_alias(setlogmask,_setlogmask)
-#endif
-
-struct syslog_data _syslog_data = SYSLOG_DATA_INIT;
+static void
+disconnectlog_r(struct syslog_data *data)
+{
+ /*
+ * If the user closed the FD and opened another in the same slot,
+ * that's their problem. They should close it before calling on
+ * system services.
+ */
+ if (data->log_file != -1) {
+ (void)close(data->log_file);
+ data->log_file = -1;
+ }
+ data->log_connected = 0; /* retry connect */
+}
-static void openlog_unlocked_r(const char *, int, int,
- struct syslog_data *);
-static void disconnectlog_r(struct syslog_data *);
-static void connectlog_r(struct syslog_data *);
+static void
+connectlog_r(struct syslog_data *data)
+{
+ /* AF_UNIX address of local logger */
+ static const struct sockaddr_un sun = {
+ .sun_family = AF_LOCAL,
+ .sun_len = sizeof(sun),
+ .sun_path = _PATH_LOG,
+ };
-#ifdef _REENTRANT
-static mutex_t syslog_mutex = MUTEX_INITIALIZER;
-#endif
-
-void
-openlog(const char *ident, int logstat, int logfac)
-{
- openlog_r(ident, logstat, logfac, &_syslog_data);
+ if (data->log_file == -1 || fcntl(data->log_file, F_GETFL, 0) == -1) {
+ if ((data->log_file = socket(AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC,
+ 0)) == -1)
+ return;
+ data->log_connected = 0;
+ }
+ if (!data->log_connected) {
+ if (connect(data->log_file,
+ (const struct sockaddr *)(const void *)&sun,
+ (socklen_t)sizeof(sun)) == -1) {
+ (void)close(data->log_file);
+ data->log_file = -1;
+ } else
+ data->log_connected = 1;
+ }
}
void
-closelog(void)
+_openlog_unlocked_r(const char *ident, int logstat, int logfac,
+ struct syslog_data *data)
{
- closelog_r(&_syslog_data);
+ if (ident != NULL)
+ data->log_tag = ident;
+ data->log_stat = logstat;
+ if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0)
+ data->log_fac = logfac;
+
+ if (data->log_stat & LOG_NDELAY) /* open immediately */
+ connectlog_r(data);
+
+ data->log_opened = 1;
}
-/* setlogmask -- set the log mask level */
-int
-setlogmask(int pmask)
+void
+_closelog_unlocked_r(struct syslog_data *data)
{
- return setlogmask_r(pmask, &_syslog_data);
+ (void)close(data->log_file);
+ data->log_file = -1;
+ data->log_connected = 0;
}
static void
@@ -151,8 +181,7 @@
prlen = (*fun->timefun)(p, tbuf_left);
- if (data == &_syslog_data)
- mutex_lock(&syslog_mutex);
+ (*fun->lock)(data);
if (data->log_hostname[0] == '\0' && gethostname(data->log_hostname,
sizeof(data->log_hostname)) == -1) {
@@ -171,8 +200,7 @@
prlen = snprintf_ss(p, tbuf_left, "%s ",
data->log_tag ? data->log_tag : "-");
Home |
Main Index |
Thread Index |
Old Index