[PATCH] add python client ipc bindings
by Angus Salkeld
This is a quick prototype to play with swig, try:
shell1 $ ./examples/ipcserver -u
shell2 $ ./examples/ipcclient.py
Signed-off-by: Angus Salkeld <asalkeld(a)redhat.com>
---
.gitignore | 7 ++---
Makefile.am | 2 +-
bindings/.gitignore | 2 +
bindings/Makefile.am | 32 +++++++++++++++++++++++
bindings/__init__.py | 2 +
bindings/helper.c | 67 +++++++++++++++++++++++++++++++++++++++++++++++++
bindings/helper.h | 26 +++++++++++++++++++
bindings/qbipc.py | 41 ++++++++++++++++++++++++++++++
bindings/qbipcmod.i | 34 +++++++++++++++++++++++++
configure.ac | 8 +++++-
examples/ipcclient.py | 41 ++++++++++++++++++++++++++++++
include/.gitignore | 3 ++
12 files changed, 259 insertions(+), 6 deletions(-)
create mode 100644 bindings/.gitignore
create mode 100644 bindings/Makefile.am
create mode 100644 bindings/__init__.py
create mode 100644 bindings/helper.c
create mode 100644 bindings/helper.h
create mode 100644 bindings/qbipc.py
create mode 100644 bindings/qbipcmod.i
create mode 100755 examples/ipcclient.py
create mode 100644 include/.gitignore
diff --git a/.gitignore b/.gitignore
index 8bed236..547124e 100644
--- a/.gitignore
+++ b/.gitignore
@@ -8,11 +8,9 @@
*.rpm
*.pc
*.log
+*.pyc
Makefile
Makefile.in
-include/config.*
-include/qb/qbconfig.h
-include/stamp-h1
aclocal.m4
autom4te.cache/
config.guess
@@ -23,6 +21,7 @@ configure
depcomp
install-sh
missing
+py-compile
.libs
m4
libtool
@@ -36,4 +35,4 @@ libqb.spec
include/qb/stamp-h2
cov
compat_reports
-abi_dumps
\ No newline at end of file
+abi_dumps
diff --git a/Makefile.am b/Makefile.am
index 3420bea..def7ddd 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -41,7 +41,7 @@ ACLOCAL_AMFLAGS = -I m4
dist_doc_DATA = COPYING INSTALL README.markdown
-SUBDIRS = include lib docs tests examples
+SUBDIRS = include lib docs tests examples bindings
doxygen:
$(MAKE) -C docs doxygen
diff --git a/bindings/.gitignore b/bindings/.gitignore
new file mode 100644
index 0000000..f1ae8b4
--- /dev/null
+++ b/bindings/.gitignore
@@ -0,0 +1,2 @@
+qbipcmod.py
+qbipcmod_wrap.c
diff --git a/bindings/Makefile.am b/bindings/Makefile.am
new file mode 100644
index 0000000..cabc405
--- /dev/null
+++ b/bindings/Makefile.am
@@ -0,0 +1,32 @@
+# Copyright (c) 2012 Red Hat, Inc.
+#
+# Authors: Angus Salkeld <asalkeld(a)redhat.com>
+#
+# This file is part of libqb.
+#
+# libqb is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Lesser General Public License as published by
+# the Free Software Foundation, either version 2.1 of the License, or
+# (at your option) any later version.
+#
+# libqb 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 Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public License
+# along with libqb. If not, see <http://www.gnu.org/licenses/>.
+#
+BUILT_SOURCES = $(srcdir)/qbipcmod_wrap.c
+SWIG_SOURCES = qbipcmod.i
+
+pkgpython_PYTHON = qbipc.py qbipcmod.py __init__.py
+pkgpyexec_LTLIBRARIES = _qbipcmod.la
+_qbipcmod_la_SOURCES = $(BUILT_SOURCES) $(SWIG_SOURCES) helper.c
+_qbipcmod_la_CFLAGS = $(shell $(PKGCONFIG) python --cflags) $(CFLAGS) $(SWIG_PYTHON_CPPFLAGS)
+_qbipcmod_la_LDFLAGS = -avoid-version -module
+_qbipcmod_la_LIBADD = $(top_builddir)/lib/libqb.la
+_qbipcmod_la_DEPENDENCIES = $(top_builddir)/lib/libqb.la
+
+$(srcdir)/%_wrap.c : %.i
+ $(SWIG) -python -nothreads $(SWIG_PYTHON_OPT) -o $@ $<
diff --git a/bindings/__init__.py b/bindings/__init__.py
new file mode 100644
index 0000000..feff2bb
--- /dev/null
+++ b/bindings/__init__.py
@@ -0,0 +1,2 @@
+# This file is required for python packages.
+# It is intentionally empty.
diff --git a/bindings/helper.c b/bindings/helper.c
new file mode 100644
index 0000000..b642407
--- /dev/null
+++ b/bindings/helper.c
@@ -0,0 +1,67 @@
+/*
+ * Copyright (C) 2012 Red Hat, Inc.
+ *
+ * Author: Angus Salkeld <asalkeld(a)redhat.com>
+ *
+ * This file is part of libqb.
+ *
+ * libqb is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, either version 2.1 of the License, or
+ * (at your option) any later version.
+ *
+ * libqb 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 Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with libqb. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "os_base.h"
+
+#include <qb/qbdefs.h>
+#include <qb/qbutil.h>
+#include <qb/qbipcc.h>
+
+struct my_req {
+ struct qb_ipc_request_header hdr;
+ char message[1];
+};
+
+struct my_res {
+ struct qb_ipc_response_header hdr;
+ char message[1];
+};
+
+#define BUF_SIZE 4096
+static char buffer[BUF_SIZE];
+
+char*
+qb_ipcc_send_recv_string(qb_ipcc_connection_t *c, int id, const char *request)
+{
+ struct my_req *req;
+ struct my_res *res = (struct my_res*)buffer;
+ int rc;
+ int s = sizeof(struct qb_ipc_request_header) + (sizeof(char) * (strlen(request) + 1));
+
+ req = malloc(s);
+ req->hdr.id = QB_IPC_MSG_USER_START + id;
+ req->hdr.size = s;
+ strcpy(req->message, request);
+
+ rc = qb_ipcc_send(c, req, s);
+ free(req);
+ if (rc < 0) {
+ perror("qb_ipcc_send");
+ return NULL;
+ }
+ rc = qb_ipcc_recv(c, res, BUF_SIZE - sizeof(struct my_res), -1);
+ if (rc < 0) {
+ perror("qb_ipcc_recv");
+ return NULL;
+ }
+ return res->message;
+}
+
diff --git a/bindings/helper.h b/bindings/helper.h
new file mode 100644
index 0000000..2336277
--- /dev/null
+++ b/bindings/helper.h
@@ -0,0 +1,26 @@
+/*
+ * Copyright (C) 2012 Red Hat, Inc.
+ *
+ * Author: Angus Salkeld <asalkeld(a)redhat.com>
+ *
+ * This file is part of libqb.
+ *
+ * libqb is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, either version 2.1 of the License, or
+ * (at your option) any later version.
+ *
+ * libqb 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 Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with libqb. If not, see <http://www.gnu.org/licenses/>.
+ */
+#ifndef _QB_HELPER_H_
+#define _QB_HELPER_H_
+
+char* qb_ipcc_send_recv_string(qb_ipcc_connection_t *c, int mid, const char *request);
+
+#endif /* _QB_HELPER_H_ */
\ No newline at end of file
diff --git a/bindings/qbipc.py b/bindings/qbipc.py
new file mode 100644
index 0000000..a8f0517
--- /dev/null
+++ b/bindings/qbipc.py
@@ -0,0 +1,41 @@
+# Copyright (C) 2012 Red Hat, Inc.
+#
+# Author: Angus Salkeld <asalkeld(a)redhat.com>
+#
+# 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 the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program 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 this program; if not, see <http://www.gnu.org/licenses/>.
+#
+import sys
+
+sys.path.append('/usr/lib64/python2.7/site-packages/libqb/')
+sys.path.append('/usr/lib/python2.7/site-packages/libqb/')
+
+from libqb import qbipcmod
+
+class qbipcconnection(object):
+
+ def __init__(self, service_name, max_message_size):
+
+ self.c = qbipcmod.qb_ipcc_connect(service_name, max_message_size)
+ if self.c is None:
+ raise Exception('qb_ipcc_connect')
+
+ def send_recv(self, mid, message):
+ return qbipcmod.qb_ipcc_send_recv_string(self.c, mid, message)
+
+ def disconnect(self):
+ if self.c:
+ qbipcmod.qb_ipcc_disconnect(self.c)
+
+
+
diff --git a/bindings/qbipcmod.i b/bindings/qbipcmod.i
new file mode 100644
index 0000000..1e6e532
--- /dev/null
+++ b/bindings/qbipcmod.i
@@ -0,0 +1,34 @@
+/*
+ * Copyright (C) 2012 Red Hat, Inc.
+ *
+ * Author: Angus Salkeld <asalkeld(a)redhat.com>
+ *
+ * This file is part of libqb.
+ *
+ * libqb is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, either version 2.1 of the License, or
+ * (at your option) any later version.
+ *
+ * libqb 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 Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with libqb. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+%module qbipcmod
+%{
+#include <qb/qbipcc.h>
+#include <qb/qbipcs.h>
+#include "helper.h"
+%}
+
+%include "../include/qb/qbipcc.h"
+
+char* qb_ipcc_send_recv_string(qb_ipcc_connection_t *c, int mid, const char *request);
+
+%include "../include/qb/qbipcs.h"
+
diff --git a/configure.ac b/configure.ac
index fd158aa..f635f7d 100644
--- a/configure.ac
+++ b/configure.ac
@@ -21,6 +21,10 @@ AC_CONFIG_MACRO_DIR([m4])
AC_CANONICAL_HOST
AC_PROG_LIBTOOL
AC_LANG([C])
+AC_PROG_CXX
+AM_PATH_PYTHON
+SWIG_ENABLE_CXX
+SWIG_PYTHON
dnl Fix default variables - "prefix" variable if not specified
if test "$prefix" = "NONE"; then
@@ -62,6 +66,7 @@ AM_PROG_CC_C_O
AC_PROG_INSTALL
AC_PROG_LN_S
AC_PROG_MAKE_SET
+AC_PATH_PROGS(SWIG, swig)
AC_CHECK_PROGS([PKGCONFIG], [pkg-config])
AC_CHECK_PROGS([DOXYGEN], [doxygen])
AM_CONDITIONAL(HAVE_DOXYGEN, test -n "${DOXYGEN}")
@@ -510,7 +515,8 @@ AC_CONFIG_FILES([Makefile
examples/Makefile
docs/Makefile
docs/man.dox
- docs/html.dox])
+ docs/html.dox
+ bindings/Makefile])
AC_OUTPUT
diff --git a/examples/ipcclient.py b/examples/ipcclient.py
new file mode 100755
index 0000000..6c5bc32
--- /dev/null
+++ b/examples/ipcclient.py
@@ -0,0 +1,41 @@
+#!/usr/bin/env python
+#
+# Copyright (C) 2012 Red Hat, Inc.
+#
+# Author: Angus Salkeld <asalkeld(a)redhat.com>
+#
+# 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 the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program 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 this program; if not, see <http://www.gnu.org/licenses/>.
+#
+import sys
+
+from libqb import qbipc
+
+if __name__ == '__main__':
+ c = qbipc.qbipcconnection('ipcserver', 512)
+
+ while True:
+ print "SEND (q to quit) : "
+ str_data = raw_input("> ")
+ if str_data is None:
+ continue
+ if 'q' in str_data:
+ break
+
+ msg = c.send_recv(123, str_data)
+
+ if msg:
+ print("Response: %s " % msg)
+
+ c.disconnect()
+
diff --git a/include/.gitignore b/include/.gitignore
new file mode 100644
index 0000000..63f0fc5
--- /dev/null
+++ b/include/.gitignore
@@ -0,0 +1,3 @@
+config.*
+qb/qbconfig.h
+stamp-h1
--
1.7.7.6
12 years, 2 months
[PATCH 1/2] IPC: better handle a disconnect been called from within connection_created()
by Angus Salkeld
Signed-off-by: Angus Salkeld <asalkeld(a)redhat.com>
---
lib/ipc_us.c | 6 +++++-
1 files changed, 5 insertions(+), 1 deletions(-)
diff --git a/lib/ipc_us.c b/lib/ipc_us.c
index 8c989aa..4251f25 100644
--- a/lib/ipc_us.c
+++ b/lib/ipc_us.c
@@ -612,10 +612,14 @@ send_response:
}
if (res == 0) {
+ qb_ipcs_connection_ref(c);
if (s->serv_fns.connection_created) {
s->serv_fns.connection_created(c);
}
- c->state = QB_IPCS_CONNECTION_ESTABLISHED;
+ if (c->state == QB_IPCS_CONNECTION_ACTIVE) {
+ c->state = QB_IPCS_CONNECTION_ESTABLISHED;
+ }
+ qb_ipcs_connection_unref(c);
} else {
if (res == -EACCES) {
qb_util_log(LOG_ERR, "Invalid IPC credentials.");
--
1.7.7.6
12 years, 2 months
[PATCH] PTRIE: refcount the notifier structs
by Angus Salkeld
This fixes: https://github.com/asalkeld/libqb/issues/29
Signed-off-by: Angus Salkeld <asalkeld(a)redhat.com>
---
lib/map_int.h | 1 +
lib/trie.c | 35 +++++++++++++++++++++++++++--------
2 files changed, 28 insertions(+), 8 deletions(-)
diff --git a/lib/map_int.h b/lib/map_int.h
index 3d70b02..d6aa7fd 100644
--- a/lib/map_int.h
+++ b/lib/map_int.h
@@ -68,6 +68,7 @@ struct qb_map_notifier {
qb_map_notify_fn callback;
int32_t events;
void *user_data;
+ int32_t refcount;
};
diff --git a/lib/trie.c b/lib/trie.c
index 6a102c8..7a05c68 100644
--- a/lib/trie.c
+++ b/lib/trie.c
@@ -540,6 +540,22 @@ trie_get(struct qb_map *map, const char *key)
}
static void
+trie_notify_deref(struct qb_map_notifier *f)
+{
+ f->refcount--;
+ if (f->refcount == 0) {
+ qb_list_del(&f->list);
+ free(f);
+ }
+}
+
+static void
+trie_notify_ref(struct qb_map_notifier *f)
+{
+ f->refcount++;
+}
+
+static void
trie_notify(struct trie_node *n,
uint32_t event, const char *key, void *old_value, void *value)
{
@@ -549,8 +565,9 @@ trie_notify(struct trie_node *n,
do {
for (list = c->notifier_head.next;
- list != &c->notifier_head; list = list->next) {
+ list != &c->notifier_head; ) {
tn = qb_list_entry(list, struct qb_map_notifier, list);
+ trie_notify_ref(tn);
if ((tn->events & event) &&
((tn->events & QB_MAP_NOTIFY_RECURSIVE) ||
@@ -564,6 +581,8 @@ trie_notify(struct trie_node *n,
tn->callback(QB_MAP_NOTIFY_FREE, (char *)key,
old_value, value, tn->user_data);
}
+ list = list->next;
+ trie_notify_deref(tn);
}
c = c->parent;
} while (c);
@@ -611,6 +630,7 @@ trie_notify_add(qb_map_t * m, const char *key,
f->events = events;
f->user_data = user_data;
f->callback = fn;
+ f->refcount = 1;
qb_list_init(&f->list);
if (key) {
if (events & QB_MAP_NOTIFY_RECURSIVE) {
@@ -640,7 +660,6 @@ trie_notify_del(qb_map_t * m, const char *key,
struct qb_map_notifier *f;
struct trie_node *n;
struct qb_list_head *list;
- struct qb_list_head *next;
int32_t found = QB_FALSE;
if (key) {
@@ -652,21 +671,21 @@ trie_notify_del(qb_map_t * m, const char *key,
return -ENOENT;
}
for (list = n->notifier_head.next;
- list != &n->notifier_head; list = next) {
+ list != &n->notifier_head; ) {
f = qb_list_entry(list, struct qb_map_notifier, list);
- next = list->next;
+ trie_notify_ref(f);
if (f->events == events && f->callback == fn) {
if (cmp_userdata && (f->user_data == user_data)) {
+ trie_notify_deref(f);
found = QB_TRUE;
- qb_list_del(&f->list);
- free(f);
} else if (!cmp_userdata) {
+ trie_notify_deref(f);
found = QB_TRUE;
- qb_list_del(&f->list);
- free(f);
}
}
+ list = list->next;
+ trie_notify_deref(f);
}
if (found) {
trie_node_release(t, n);
--
1.7.7.5
12 years, 3 months
[PATCH] LOG: make sure qb_log_from_external_source() takes priority into account.
by Angus Salkeld
Fixes: https://github.com/asalkeld/libqb/issues/28
Signed-off-by: Angus Salkeld <asalkeld(a)redhat.com>
---
lib/log_dcs.c | 4 +++-
tests/check_log.c | 20 ++++++++++++++++++++
2 files changed, 23 insertions(+), 1 deletions(-)
diff --git a/lib/log_dcs.c b/lib/log_dcs.c
index 215252e..73ca2d8 100644
--- a/lib/log_dcs.c
+++ b/lib/log_dcs.c
@@ -128,6 +128,7 @@ qb_log_dcs_get(int32_t * newly_created,
assert(rc == 0);
if (csl_head->cs &&
format == csl_head->cs->format &&
+ priority == csl_head->cs->priority &&
strcmp(safe_filename, csl_head->cs->filename) == 0) {
return csl_head->cs;
}
@@ -145,7 +146,8 @@ qb_log_dcs_get(int32_t * newly_created,
for (csl = csl_head; csl; csl = csl->next) {
assert(csl->cs->lineno == lineno);
if (format == csl->cs->format &&
- strcmp(safe_filename, csl->cs->filename) == 0) {
+ priority == csl->cs->priority &&
+ strcmp(safe_filename, csl->cs->filename) == 0) {
cs = csl->cs;
break;
}
diff --git a/tests/check_log.c b/tests/check_log.c
index 751072d..b399c31 100644
--- a/tests/check_log.c
+++ b/tests/check_log.c
@@ -278,6 +278,26 @@ START_TEST(test_log_basic)
ck_assert_int_eq(num_msgs, 1);
ck_assert_str_eq(test_buf, "same filename/lineno");
+ /* check filtering works on same file/lineno but different
+ * log level.
+ */
+ qb_log_filter_ctl(t, QB_LOG_FILTER_CLEAR_ALL,
+ QB_LOG_FILTER_FILE, "*", LOG_DEBUG);
+ qb_log_filter_ctl(t, QB_LOG_FILTER_ADD,
+ QB_LOG_FILTER_FILE, __FILE__, LOG_INFO);
+
+ num_msgs = 0;
+ qb_log_from_external_source(__func__, __FILE__,
+ "same filename/lineno, this level %d",
+ LOG_INFO, 56, 0, LOG_INFO);
+ ck_assert_int_eq(num_msgs, 1);
+ ck_assert_str_eq(test_buf, "same filename/lineno, this level 6");
+
+ num_msgs = 0;
+ qb_log_from_external_source(__func__, __FILE__,
+ "same filename/lineno, this level %d",
+ LOG_DEBUG, 56, 0, LOG_DEBUG);
+ ck_assert_int_eq(num_msgs, 0);
}
END_TEST
--
1.7.7.5
12 years, 3 months
[PATCH] Deprecate qb_util_set_log_function()
by Angus Salkeld
And remove uses of it from the test programs
Signed-off-by: Angus Salkeld <asalkeld(a)redhat.com>
---
include/qb/qbutil.h | 3 +-
tests/bmc.c | 54 +++++++++++++++----------------
tests/bms.c | 86 ++++++++++++++++++++++++--------------------------
tests/check_log.c | 11 +-----
tests/loop.c | 49 +++++++++++++---------------
tests/rbreader.c | 40 ++++++++++++++++-------
tests/rbwriter.c | 25 ++++++++-------
7 files changed, 135 insertions(+), 133 deletions(-)
diff --git a/include/qb/qbutil.h b/include/qb/qbutil.h
index 2b450e9..16e6817 100644
--- a/include/qb/qbutil.h
+++ b/include/qb/qbutil.h
@@ -32,6 +32,7 @@ extern "C" {
#ifndef S_SPLINT_S
#include <unistd.h>
#endif /* S_SPLINT_S */
+#include <qb/qbdefs.h>
/**
* @file qbutil.h
@@ -88,7 +89,7 @@ typedef void (*qb_util_log_fn_t) (const char *file_name,
/**
* Use this function to output libqb internal log message as you wish.
*/
-void qb_util_set_log_function(qb_util_log_fn_t fn);
+void qb_util_set_log_function(qb_util_log_fn_t fn) QB_GNUC_DEPRECATED;
/**
* Add milliseconds onto the timespec.
diff --git a/tests/bmc.c b/tests/bmc.c
index 6a4eeb4..585811c 100644
--- a/tests/bmc.c
+++ b/tests/bmc.c
@@ -21,7 +21,7 @@
#include "os_base.h"
#include <signal.h>
-#include <qb/qbdefs.h>
+#include <qb/qblog.h>
#include <qb/qbutil.h>
#include <qb/qbipcc.h>
@@ -45,8 +45,8 @@ static void bm_finish(const char *operation, int32_t size)
ops_per_sec = ((float)ITERATIONS) / elapsed;
mbs_per_sec = ((((float)ITERATIONS) * size) / elapsed) / (1024.0 * 1024.0);
- printf("write size, %d, OPs/sec, %9.3f, ", size, ops_per_sec);
- printf("MB/sec, %9.3f\n", mbs_per_sec);
+ qb_log(LOG_INFO, "write size, %d, OPs/sec, %9.3f, MB/sec, %9.3f",
+ size, ops_per_sec, mbs_per_sec);
}
struct my_req {
@@ -70,11 +70,11 @@ repeat_send:
if (res == -EAGAIN) {
goto repeat_send;
} else if (res == -EINVAL || res == -EINTR || res == -ENOTCONN) {
- perror("qb_ipcc_send");
+ qb_perror(LOG_ERR, "qb_ipcc_send");
return -1;
} else {
errno = -res;
- perror("qb_ipcc_send");
+ qb_perror(LOG_ERR, "qb_ipcc_send");
goto repeat_send;
}
}
@@ -87,7 +87,7 @@ repeat_send:
return -1;
}
if (res < 0) {
- perror("qb_ipcc_recv");
+ qb_perror(LOG_ERR, "qb_ipcc_recv");
}
assert(res == sizeof(struct qb_ipc_response_header));
assert(res_header.id == 13);
@@ -101,7 +101,7 @@ repeat_send:
return -1;
}
if (res < 0) {
- perror("qb_ipcc_event_recv");
+ qb_perror(LOG_ERR, "qb_ipcc_event_recv");
}
assert(res == sizeof(struct qb_ipc_response_header));
assert(res_header.id == 13);
@@ -114,33 +114,27 @@ struct qb_ipc_request_header *global_zcb_buffer;
static void show_usage(const char *name)
{
- printf("usage: \n");
- printf("%s <options>\n", name);
- printf("\n");
- printf(" options:\n");
- printf("\n");
- printf(" -n non-blocking ipc (default blocking)\n");
- printf(" -e receive events\n");
- printf(" -v verbose\n");
- printf(" -h show this help text\n");
- printf("\n");
+ qb_log(LOG_INFO, "usage: \n");
+ qb_log(LOG_INFO, "%s <options>\n", name);
+ qb_log(LOG_INFO, "\n");
+ qb_log(LOG_INFO, " options:\n");
+ qb_log(LOG_INFO, "\n");
+ qb_log(LOG_INFO, " -n non-blocking ipc (default blocking)\n");
+ qb_log(LOG_INFO, " -e receive events\n");
+ qb_log(LOG_INFO, " -v verbose\n");
+ qb_log(LOG_INFO, " -h show this help text\n");
+ qb_log(LOG_INFO, "\n");
}
static void sigterm_handler(int32_t num)
{
- printf("bmc: %s(%d)\n", __func__, num);
+ qb_log(LOG_INFO, "bmc: %s(%d)\n", __func__, num);
qb_ipcc_disconnect(conn);
exit(0);
}
-static void libqb_log_writer(const char *file_name,
- int32_t file_line,
- int32_t severity, const char *msg)
-{
- printf("libqb: %s:%d [%d] %s\n", file_name, file_line, severity, msg);
-}
-
-int32_t main(int32_t argc, char *argv[])
+int32_t
+main(int32_t argc, char *argv[])
{
const char *options = "nevh";
int32_t opt;
@@ -149,7 +143,11 @@ int32_t main(int32_t argc, char *argv[])
mypid = getpid();
- qb_util_set_log_function(libqb_log_writer);
+ qb_log_init("bmc", LOG_USER, LOG_EMERG);
+ qb_log_ctl(QB_LOG_SYSLOG, QB_LOG_CONF_ENABLED, QB_FALSE);
+ qb_log_filter_ctl(QB_LOG_STDERR, QB_LOG_FILTER_ADD,
+ QB_LOG_FILTER_FILE, "*", LOG_INFO);
+ qb_log_ctl(QB_LOG_STDERR, QB_LOG_CONF_ENABLED, QB_TRUE);
while ((opt = getopt(argc, argv, options)) != -1) {
switch (opt) {
@@ -175,7 +173,7 @@ int32_t main(int32_t argc, char *argv[])
signal(SIGTERM, sigterm_handler);
conn = qb_ipcc_connect("bm1", MAX_MSG_SIZE);
if (conn == NULL) {
- perror("qb_ipcc_connect");
+ qb_perror(LOG_ERR, "qb_ipcc_connect");
exit(1);
}
diff --git a/tests/bms.c b/tests/bms.c
index cade87b..4583dcf 100644
--- a/tests/bms.c
+++ b/tests/bms.c
@@ -22,6 +22,7 @@
#include <signal.h>
#include <qb/qbdefs.h>
+#include <qb/qblog.h>
#include <qb/qbutil.h>
#include <qb/qbloop.h>
#include <qb/qbipcs.h>
@@ -46,12 +47,12 @@ static int32_t s1_connection_accept_fn(qb_ipcs_connection_t *c, uid_t uid, gid_t
#if 0
if (uid == 0 && gid == 0) {
if (verbose) {
- printf("%s:%d %s authenticated connection\n",
+ qb_log(LOG_INFO, "%s:%d %s authenticated connection\n",
__FILE__, __LINE__, __func__);
}
return 1;
}
- printf("%s:%d %s() BAD user!\n", __FILE__, __LINE__, __func__);
+ qb_log(LOG_INFO, "%s:%d %s() BAD user!\n", __FILE__, __LINE__, __func__);
return 0;
#else
return 0;
@@ -64,14 +65,14 @@ static void s1_connection_created_fn(qb_ipcs_connection_t *c)
struct qb_ipcs_stats srv_stats;
qb_ipcs_stats_get(s1, &srv_stats, QB_FALSE);
- printf("\n Connection created\n > active:%d\n > closed:%d\n\n",
+ qb_log(LOG_NOTICE, "Connection created > active:%d > closed:%d",
srv_stats.active_connections,
srv_stats.closed_connections);
}
static void s1_connection_destroyed_fn(qb_ipcs_connection_t *c)
{
- printf("connection about to be freed\n");
+ qb_log(LOG_INFO, "connection about to be freed\n");
}
static int32_t s1_connection_closed_fn(qb_ipcs_connection_t *c)
@@ -83,18 +84,18 @@ static int32_t s1_connection_closed_fn(qb_ipcs_connection_t *c)
qb_ipcs_connection_stats_get(c, &stats, QB_FALSE);
- printf("\n Connection to pid:%d destroyed\n > active:%d\n > closed:%d\n\n",
+ qb_log(LOG_INFO, "Connection to pid:%d destroyed > active:%d > closed:%d",
stats.client_pid,
srv_stats.active_connections,
srv_stats.closed_connections);
- printf(" Requests %"PRIu64"\n", stats.requests);
- printf(" Responses %"PRIu64"\n", stats.responses);
- printf(" Events %"PRIu64"\n", stats.events);
- printf(" Send retries %"PRIu64"\n", stats.send_retries);
- printf(" Recv retries %"PRIu64"\n", stats.recv_retries);
- printf(" FC state %d\n", stats.flow_control_state);
- printf(" FC count %"PRIu64"\n\n", stats.flow_control_count);
+ qb_log(LOG_INFO, " Requests %"PRIu64"\n", stats.requests);
+ qb_log(LOG_INFO, " Responses %"PRIu64"\n", stats.responses);
+ qb_log(LOG_INFO, " Events %"PRIu64"\n", stats.events);
+ qb_log(LOG_INFO, " Send retries %"PRIu64"\n", stats.send_retries);
+ qb_log(LOG_INFO, " Recv retries %"PRIu64"\n", stats.recv_retries);
+ qb_log(LOG_INFO, " FC state %d\n", stats.flow_control_state);
+ qb_log(LOG_INFO, " FC count %"PRIu64"\n\n", stats.flow_control_count);
return 0;
}
@@ -105,11 +106,8 @@ static int32_t s1_msg_process_fn(qb_ipcs_connection_t *c,
struct qb_ipc_response_header response;
ssize_t res;
- if (verbose > 2) {
- printf("%s:%d %s > msg:%d, size:%d\n",
- __FILE__, __LINE__, __func__,
- req_pt->id, req_pt->size);
- }
+ qb_log(LOG_TRACE, "msg:%d, size:%d",
+ req_pt->id, req_pt->size);
response.size = sizeof(struct qb_ipc_response_header);
response.id = 13;
response.error = 0;
@@ -117,49 +115,43 @@ static int32_t s1_msg_process_fn(qb_ipcs_connection_t *c,
res = qb_ipcs_response_send(c, &response,
sizeof(response));
if (res < 0) {
- perror("qb_ipcs_response_send");
+ qb_perror(LOG_ERR, "qb_ipcs_response_send");
}
}
if (events) {
res = qb_ipcs_event_send(c, &response,
sizeof(response));
if (res < 0) {
- perror("qb_ipcs_event_send");
+ qb_perror(LOG_ERR, "qb_ipcs_event_send");
}
}
return 0;
}
-static void ipc_log_fn(const char *file_name,
- int32_t file_line, int32_t severity, const char *msg)
-{
- fprintf(stderr, "%s:%d [%d] %s\n", file_name, file_line, severity, msg);
-}
-
static void sigusr1_handler(int32_t num)
{
- printf("%s(%d)\n", __func__, num);
+ qb_log(LOG_INFO, "%s(%d)\n", __func__, num);
qb_ipcs_destroy(s1);
exit(0);
}
static void show_usage(const char *name)
{
- printf("usage: \n");
- printf("%s <options>\n", name);
- printf("\n");
- printf(" options:\n");
- printf("\n");
- printf(" -n non-blocking ipc (default blocking)\n");
- printf(" -e send events back instead for responses\n");
- printf(" -v verbose\n");
- printf(" -h show this help text\n");
- printf(" -m use shared memory\n");
- printf(" -p use posix message queues\n");
- printf(" -s use sysv message queues\n");
- printf(" -u use unix sockets\n");
- printf(" -g use glib mainloop\n");
- printf("\n");
+ qb_log(LOG_INFO, "usage: \n");
+ qb_log(LOG_INFO, "%s <options>\n", name);
+ qb_log(LOG_INFO, "\n");
+ qb_log(LOG_INFO, " options:\n");
+ qb_log(LOG_INFO, "\n");
+ qb_log(LOG_INFO, " -n non-blocking ipc (default blocking)\n");
+ qb_log(LOG_INFO, " -e send events back instead for responses\n");
+ qb_log(LOG_INFO, " -v verbose\n");
+ qb_log(LOG_INFO, " -h show this help text\n");
+ qb_log(LOG_INFO, " -m use shared memory\n");
+ qb_log(LOG_INFO, " -p use posix message queues\n");
+ qb_log(LOG_INFO, " -s use sysv message queues\n");
+ qb_log(LOG_INFO, " -u use unix sockets\n");
+ qb_log(LOG_INFO, " -g use glib mainloop\n");
+ qb_log(LOG_INFO, "\n");
}
#ifdef HAVE_GLIB
@@ -323,13 +315,17 @@ int32_t main(int32_t argc, char *argv[])
signal(SIGILL, sigusr1_handler);
signal(SIGTERM, sigusr1_handler);
- qb_util_set_log_function(ipc_log_fn);
+ qb_log_init("bms", LOG_USER, LOG_EMERG);
+ qb_log_ctl(QB_LOG_SYSLOG, QB_LOG_CONF_ENABLED, QB_FALSE);
+ qb_log_filter_ctl(QB_LOG_STDERR, QB_LOG_FILTER_ADD,
+ QB_LOG_FILTER_FILE, "*", LOG_INFO + verbose);
+ qb_log_ctl(QB_LOG_STDERR, QB_LOG_CONF_ENABLED, QB_TRUE);
if (!use_glib) {
bms_loop = qb_loop_create();
s1 = qb_ipcs_create("bm1", 0, ipc_type, &sh);
if (s1 == 0) {
- perror("qb_ipcs_create");
+ qb_perror(LOG_ERR, "qb_ipcs_create");
exit(1);
}
qb_ipcs_poll_handlers_set(s1, &ph);
@@ -343,7 +339,7 @@ int32_t main(int32_t argc, char *argv[])
s1 = qb_ipcs_create("bm1", 0, ipc_type, &sh);
if (s1 == 0) {
- perror("qb_ipcs_create");
+ qb_perror(LOG_ERR, "qb_ipcs_create");
exit(1);
}
qb_ipcs_poll_handlers_set(s1, &glib_ph);
@@ -351,7 +347,7 @@ int32_t main(int32_t argc, char *argv[])
g_main_loop_run(glib_loop);
#else
- printf("You don't seem to have glib-devel installed.\n");
+ qb_log(LOG_ERR, "You don't seem to have glib-devel installed.\n");
#endif
}
return EXIT_SUCCESS;
diff --git a/tests/check_log.c b/tests/check_log.c
index 58b2d4c..751072d 100644
--- a/tests/check_log.c
+++ b/tests/check_log.c
@@ -515,21 +515,14 @@ static Suite *log_suite(void)
return s;
}
-static void libqb_log_fn(const char *file_name,
- int32_t file_line, int32_t severity, const char *msg)
-{
- printf("libqb: %s:%d %s\n", file_name, file_line, msg);
-}
-
-int32_t main(void)
+int32_t
+main(void)
{
int32_t number_failed;
Suite *s = log_suite();
SRunner *sr = srunner_create(s);
- qb_util_set_log_function(libqb_log_fn);
-
srunner_run_all(sr, CK_VERBOSE);
number_failed = srunner_ntests_failed(sr);
srunner_free(sr);
diff --git a/tests/loop.c b/tests/loop.c
index 2a203fc..1a6d7dd 100644
--- a/tests/loop.c
+++ b/tests/loop.c
@@ -19,51 +19,50 @@
* along with libqb. If not, see <http://www.gnu.org/licenses/>.
*/
#include "os_base.h"
-#include <signal.h>
-
#include <sys/poll.h>
-#include <qb/qbloop.h>
-#include <qb/qbutil.h>
#include <qb/qbdefs.h>
+#include <qb/qbutil.h>
+#include <qb/qblog.h>
+#include <qb/qbloop.h>
static struct qb_loop *l;
static qb_loop_timer_handle th;
-static void job_3_9(void *data) { printf("%s\n", __func__); }
-static void job_1_2(void *data) { printf("%s\n", __func__); }
-static void job_2_4(void *data) { printf("%s\n", __func__); }
-static void job_3_5(void *data) { printf("%s\n", __func__); }
-static void job_3_6(void *data) { printf("%s\n", __func__); }
-static void job_1_1(void *data) { printf("%s\n", __func__); }
-static void job_3_7(void *data) { printf("%s\n", __func__); }
-static void job_2_3(void *data) { printf("%s\n", __func__); }
-static void job_2_8(void *data) { printf("%s\n", __func__); }
-static void job_1_9(void *data) { printf("%s\n", __func__); }
+static void job_3_9(void *data) { qb_log(LOG_INFO, "%s\n", __func__); }
+static void job_1_2(void *data) { qb_log(LOG_INFO, "%s\n", __func__); }
+static void job_2_4(void *data) { qb_log(LOG_INFO, "%s\n", __func__); }
+static void job_3_5(void *data) { qb_log(LOG_INFO, "%s\n", __func__); }
+static void job_3_6(void *data) { qb_log(LOG_INFO, "%s\n", __func__); }
+static void job_1_1(void *data) { qb_log(LOG_INFO, "%s\n", __func__); }
+static void job_3_7(void *data) { qb_log(LOG_INFO, "%s\n", __func__); }
+static void job_2_3(void *data) { qb_log(LOG_INFO, "%s\n", __func__); }
+static void job_2_8(void *data) { qb_log(LOG_INFO, "%s\n", __func__); }
+static void job_1_9(void *data) { qb_log(LOG_INFO, "%s\n", __func__); }
static void more_important_jobs(void *data)
{
- printf("%s\n", __func__);
+ qb_enter();
qb_loop_job_add(l, QB_LOOP_HIGH, NULL, job_1_2);
qb_loop_job_add(l, QB_LOOP_HIGH, NULL, job_1_9);
}
static int32_t handle_reconf_signal(int32_t sig, void *data)
{
- printf("%s(%d) \n", __func__, sig);
+ qb_log(LOG_INFO, "signal %d", sig);
return 0;
}
static int32_t handle_exit_signal(int32_t sig, void *data)
{
- printf("%s(%d) exiting ... bye\n", __func__, sig);
+ qb_log(LOG_INFO, "exiting (signal %d)... bye", sig);
qb_loop_stop(l);
return -1;
}
static void more_jobs(void *data)
{
- printf("%s\n", __func__);
+ qb_log(LOG_INFO, "%s\n", __func__);
qb_loop_timer_add(l, QB_LOOP_HIGH, 3109*QB_TIME_NS_IN_MSEC, NULL, job_1_1, &th);
qb_loop_job_add(l, QB_LOOP_LOW, NULL, job_3_7);
qb_loop_timer_add(l, QB_LOOP_LOW, 1000*QB_TIME_NS_IN_MSEC, NULL, more_important_jobs, &th);
@@ -82,7 +81,7 @@ static int32_t read_stdin(int32_t fd, int32_t revents, void *data)
char buf[100];
ssize_t len = read(fd, buf, 100);
buf[len-1] = '\0';
- printf("typed > \"%s\"\n", buf);
+ qb_log(LOG_INFO, "typed > \"%s\"\n", buf);
if (strcmp(buf, "more") == 0) {
more_jobs(NULL);
}
@@ -90,17 +89,15 @@ static int32_t read_stdin(int32_t fd, int32_t revents, void *data)
return 0;
}
-static void libqb_log_fn(const char *file_name,
- int32_t file_line, int32_t severity, const char *msg)
-{
- printf("libqb: %s:%d %s\n", file_name, file_line, msg);
-}
-
int main(int argc, char * argv[])
{
qb_loop_signal_handle sh;
- qb_util_set_log_function(libqb_log_fn);
+ qb_log_init("loop", LOG_USER, LOG_EMERG);
+ qb_log_ctl(QB_LOG_SYSLOG, QB_LOG_CONF_ENABLED, QB_FALSE);
+ qb_log_filter_ctl(QB_LOG_STDERR, QB_LOG_FILTER_ADD,
+ QB_LOG_FILTER_FILE, "*", LOG_DEBUG);
+ qb_log_ctl(QB_LOG_STDERR, QB_LOG_CONF_ENABLED, QB_TRUE);
l = qb_loop_create();
diff --git a/tests/rbreader.c b/tests/rbreader.c
index 3725f0f..6b5356b 100644
--- a/tests/rbreader.c
+++ b/tests/rbreader.c
@@ -1,3 +1,21 @@
+/*
+ * Copyright (C) 2012 Red Hat, Inc.
+ *
+ * Author: Angus Salkeld <asalkeld(a)redhat.com>
+ *
+ * libqb is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, either version 2.1 of the License, or
+ * (at your option) any later version.
+ *
+ * libqb 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 Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with libqb. If not, see <http://www.gnu.org/licenses/>.
+ */
#include <stdlib.h>
#include <stdio.h>
@@ -8,6 +26,7 @@
#include <qb/qbdefs.h>
#include <qb/qbrb.h>
#include <qb/qbutil.h>
+#include <qb/qblog.h>
#define BUFFER_CHUNK_SIZE (50*50*10)
static qb_ringbuffer_t *rb = NULL;
@@ -16,17 +35,10 @@ static int keep_reading = QB_TRUE;
static void sigterm_handler(int32_t num)
{
- printf("reader: %s(%d)\n", __func__, num);
+ qb_log(LOG_INFO, "signal %d", num);
keep_reading = QB_FALSE;
}
-static void libqb_log_writer(const char *file_name,
- int32_t file_line,
- int32_t severity, const char *msg)
-{
- printf("libqb:reader: %s:%d %s\n", file_name, file_line, msg);
-}
-
int32_t main(int32_t argc, char *argv[])
{
ssize_t num_read;
@@ -34,20 +46,24 @@ int32_t main(int32_t argc, char *argv[])
signal(SIGINT, sigterm_handler);
- qb_util_set_log_function(libqb_log_writer);
+ qb_log_init("rbreader", LOG_USER, LOG_EMERG);
+ qb_log_ctl(QB_LOG_SYSLOG, QB_LOG_CONF_ENABLED, QB_FALSE);
+ qb_log_filter_ctl(QB_LOG_STDERR, QB_LOG_FILTER_ADD,
+ QB_LOG_FILTER_FILE, "*", LOG_INFO);
+ qb_log_ctl(QB_LOG_STDERR, QB_LOG_CONF_ENABLED, QB_TRUE);
rb = qb_rb_open("tester", BUFFER_CHUNK_SIZE * 3,
QB_RB_FLAG_SHARED_PROCESS | QB_RB_FLAG_CREATE, 0);
if (rb == NULL) {
- printf("reader: failed to create ringbuffer\n");
+ qb_perror(LOG_ERR, "failed to create ringbuffer");
return -1;
}
while (keep_reading) {
num_read = qb_rb_chunk_read(rb, buffer,
BUFFER_CHUNK_SIZE, 5500);
- if (num_read == -1) {
- printf("reader: nothing to read\n");
+ if (num_read < 0) {
+ qb_perror(LOG_ERR, "nothing to read");
}
}
qb_rb_close(rb);
diff --git a/tests/rbwriter.c b/tests/rbwriter.c
index 8985d0d..d105398 100644
--- a/tests/rbwriter.c
+++ b/tests/rbwriter.c
@@ -33,6 +33,7 @@
#include <qb/qbrb.h>
#include <qb/qbdefs.h>
#include <qb/qbutil.h>
+#include <qb/qblog.h>
static qb_ringbuffer_t *rb = NULL;
#define ITERATIONS 100000
@@ -52,18 +53,11 @@ do { \
static void sigterm_handler(int32_t num)
{
- printf("writer: %s(%d)\n", __func__, num);
+ qb_log(LOG_INFO, "writer: %s(%d)\n", __func__, num);
qb_rb_close(rb);
exit(0);
}
-static void libqb_log_writer(const char *file_name,
- int32_t file_line,
- int32_t severity, const char *msg)
-{
- printf("libqb:writer: %s:%d %s\n", file_name, file_line, msg);
-}
-
static void bm_start(void)
{
gettimeofday(&tv1, NULL);
@@ -86,8 +80,8 @@ static void bm_finish(const char *operation, int32_t size)
(((float)tv_elapsed.tv_sec) +
(((float)tv_elapsed.tv_usec) / 1000000.0))) / (1024.0 * 1024.0);
- printf("write size %d OPs/sec %9.3f ", size, ops_per_sec);
- printf("MB/sec %9.3f\n", mbs_per_sec);
+ qb_log(LOG_INFO, "write size %d OPs/sec %9.3f MB/sec %9.3f",
+ size, ops_per_sec, mbs_per_sec);
}
static void bmc_connect(void)
@@ -96,7 +90,7 @@ static void bmc_connect(void)
QB_RB_FLAG_SHARED_PROCESS, 0);
if (rb == NULL) {
- printf("writer: failed to create ringbuffer\n");
+ qb_perror(LOG_ERR, "failed to create ringbuffer");
exit(1);
}
@@ -133,11 +127,12 @@ int32_t main(int32_t argc, char *argv[])
int32_t opt;
int32_t i, j;
int32_t size;
+ int32_t verbose = 0;
while ((opt = getopt(argc, argv, options)) != -1) {
switch (opt) {
case 'v':
- qb_util_set_log_function(libqb_log_writer);
+ verbose++;
break;
case 'h':
default:
@@ -149,6 +144,12 @@ int32_t main(int32_t argc, char *argv[])
signal(SIGINT, sigterm_handler);
+ qb_log_init("rbwriter", LOG_USER, LOG_EMERG);
+ qb_log_ctl(QB_LOG_SYSLOG, QB_LOG_CONF_ENABLED, QB_FALSE);
+ qb_log_filter_ctl(QB_LOG_STDERR, QB_LOG_FILTER_ADD,
+ QB_LOG_FILTER_FILE, "*", LOG_INFO + verbose);
+ qb_log_ctl(QB_LOG_STDERR, QB_LOG_CONF_ENABLED, QB_TRUE);
+
bmc_connect();
for (j = 1; j < 49; j++) {
--
1.7.7.5
12 years, 3 months
[PATCH] LOG: only bump messages that need it (>info)
by Angus Salkeld
else all the messages get their priorities distorted
Signed-off-by: Angus Salkeld <asalkeld(a)redhat.com>
---
lib/log_syslog.c | 8 +++++++-
1 files changed, 7 insertions(+), 1 deletions(-)
diff --git a/lib/log_syslog.c b/lib/log_syslog.c
index 6bb6182..16a3da0 100644
--- a/lib/log_syslog.c
+++ b/lib/log_syslog.c
@@ -31,8 +31,14 @@ _syslog_logger(int32_t target,
{
char output_buffer[QB_LOG_MAX_LEN];
struct qb_log_target *t = qb_log_target_get(target);
- int32_t final_priority = cs->priority + t->priority_bump;
+ int32_t final_priority = cs->priority;
+ if (final_priority > LOG_INFO) {
+ /*
+ * only bump the priority if it is greater than info.
+ */
+ final_priority += t->priority_bump;
+ }
if (final_priority > LOG_DEBUG) {
return;
}
--
1.7.7.5
12 years, 3 months
[PATCH] LOOP: allow a timer to be created without returning the handle
by Angus Salkeld
This is if the user does not plan to delete the timer.
Signed-off-by: Angus Salkeld <asalkeld(a)redhat.com>
---
lib/loop_poll.c | 9 +++------
lib/loop_timerlist.c | 7 +++----
tests/check_loop.c | 2 --
3 files changed, 6 insertions(+), 12 deletions(-)
diff --git a/lib/loop_poll.c b/lib/loop_poll.c
index 2c1bd65..cc1f580 100644
--- a/lib/loop_poll.c
+++ b/lib/loop_poll.c
@@ -733,11 +733,6 @@ qb_loop_timer_add(struct qb_loop *l,
"can't add a timer with either (l == NULL || timer_fn == NULL)");
return -EINVAL;
}
- if (timer_handle_out == NULL) {
- qb_util_log(LOG_ERR,
- "can't add a timer with (timer_handle_out == NULL)");
- return -ENOENT;
- }
fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC | TFD_NONBLOCK);
if (fd == -1) {
res = -errno;
@@ -771,7 +766,9 @@ qb_loop_timer_add(struct qb_loop *l,
pe->type = QB_TIMER;
pe->add_to_jobs = _qb_timer_add_to_jobs_;
- *timer_handle_out = (((uint64_t) (pe->check)) << 32) | pe->install_pos;
+ if (timer_handle_out) {
+ *timer_handle_out = (((uint64_t) (pe->check)) << 32) | pe->install_pos;
+ }
return res;
diff --git a/lib/loop_timerlist.c b/lib/loop_timerlist.c
index afdebf4..7e7cb19 100644
--- a/lib/loop_timerlist.c
+++ b/lib/loop_timerlist.c
@@ -186,9 +186,6 @@ qb_loop_timer_add(struct qb_loop * l,
return -EINVAL;
}
my_src = (struct qb_timer_source *)l->timer_source;
- if (timer_handle_out == 0) {
- return -ENOENT;
- }
i = _get_empty_array_position_(my_src);
assert(qb_array_index(my_src->timers, i, (void **)&t) >= 0);
@@ -208,7 +205,9 @@ qb_loop_timer_add(struct qb_loop * l,
}
}
- *timer_handle_out = (((uint64_t) (t->check)) << 32) | t->install_pos;
+ if (timer_handle_out) {
+ *timer_handle_out = (((uint64_t) (t->check)) << 32) | t->install_pos;
+ }
return timerlist_add_duration(&my_src->timerlist,
make_job_from_tmo, t,
nsec_duration, &t->timerlist_handle);
diff --git a/tests/check_loop.c b/tests/check_loop.c
index 9a19b82..b358efd 100644
--- a/tests/check_loop.c
+++ b/tests/check_loop.c
@@ -193,8 +193,6 @@ START_TEST(test_loop_timer_input)
ck_assert_int_eq(res, -EINVAL);
res = qb_loop_timer_add(l, QB_LOOP_LOW, 5*QB_TIME_NS_IN_MSEC, l, NULL, &test_th);
ck_assert_int_eq(res, -EINVAL);
- res = qb_loop_timer_add(l, QB_LOOP_LOW, 5*QB_TIME_NS_IN_MSEC, l, job_1, NULL);
- ck_assert_int_eq(res, -ENOENT);
qb_loop_destroy(l);
}
END_TEST
--
1.7.7.5
12 years, 3 months
[PATCH] IPC: check for the server liveness before disconnecting.
by Angus Salkeld
if the server is dead then is_connected will cause the resourses
to be properly cleaned up.
Signed-off-by: Angus Salkeld <asalkeld(a)redhat.com>
---
lib/ipcc.c | 15 +++++++++++++++
lib/ipcs.c | 4 ++--
tests/check_ipc.c | 12 ++++++++----
3 files changed, 25 insertions(+), 6 deletions(-)
diff --git a/lib/ipcc.c b/lib/ipcc.c
index 6bcc5d5..43c7d43 100644
--- a/lib/ipcc.c
+++ b/lib/ipcc.c
@@ -306,11 +306,26 @@ qb_ipcc_event_recv(struct qb_ipcc_connection * c, void *msg_pt,
void
qb_ipcc_disconnect(struct qb_ipcc_connection *c)
{
+ struct qb_ipc_one_way *ow = NULL;
+
qb_util_log(LOG_DEBUG, "%s()", __func__);
if (c == NULL) {
return;
}
+
+ if (c->needs_sock_for_poll) {
+ ow = &c->setup;
+ }
+ if (c->type == QB_IPC_SOCKET) {
+ ow = &c->event;
+ }
+ if (ow) {
+ if (qb_ipc_us_recv_ready(ow, 1000) == -ENOTCONN) {
+ c->is_connected = QB_FALSE;
+ }
+ }
+
qb_ipcc_us_sock_close(c->setup.u.us.sock);
if (c->funcs.disconnect) {
c->funcs.disconnect(c);
diff --git a/lib/ipcs.c b/lib/ipcs.c
index 92c24c2..bf1f9f2 100644
--- a/lib/ipcs.c
+++ b/lib/ipcs.c
@@ -467,8 +467,8 @@ qb_ipcs_connection_unref(struct qb_ipcs_connection *c)
return;
}
if (c->refcount < 1) {
- qb_util_log(LOG_ERR, "%s() ref:%d state:%d fd:%d",
- __func__, c->refcount, c->state,
+ qb_util_log(LOG_ERR, "ref:%d state:%d fd:%d",
+ c->refcount, c->state,
c->setup.u.us.sock);
assert(0);
}
diff --git a/tests/check_ipc.c b/tests/check_ipc.c
index 95919b4..225d379 100644
--- a/tests/check_ipc.c
+++ b/tests/check_ipc.c
@@ -340,11 +340,14 @@ test_ipc_exit(void)
/* kill the server */
stop_process(pid);
- /* assertion: we can call send/recv without locking up */
- rc = send_and_check(100, recv_timeout, QB_FALSE);
- ck_assert_int_eq(rc, -ENOTCONN);
+ /*
+ * wait a bit for the server to die.
+ */
+ sleep(1);
- /* this needs to free up the shared mem */
+ /*
+ * this needs to free up the shared mem
+ */
qb_ipcc_disconnect(conn);
}
@@ -565,6 +568,7 @@ test_ipc_server_fail(void)
sleep(1);
/*
* try recv from the exit'ed server
+ * assertion: we can call recv without locking up
*/
res = qb_ipcc_recv(conn, &res_header,
sizeof(struct qb_ipc_response_header), 100);
--
1.7.7.5
12 years, 3 months
[PATCH] add tests for signal handlers
by Angus Salkeld
Signed-off-by: Angus Salkeld <asalkeld(a)redhat.com>
---
lib/loop.c | 2 ++
tests/check_loop.c | 39 +++++++++++++++++++++++++++++++++++++++
2 files changed, 41 insertions(+), 0 deletions(-)
diff --git a/lib/loop.c b/lib/loop.c
index 8cb0ab2..142e7eb 100644
--- a/lib/loop.c
+++ b/lib/loop.c
@@ -121,6 +121,8 @@ qb_loop_run(struct qb_loop *l)
int32_t todo = 0;
int32_t ms_timeout;
+ l->stop_requested = QB_FALSE;
+
do {
if (p_stop == QB_LOOP_LOW) {
p_stop = QB_LOOP_HIGH;
diff --git a/tests/check_loop.c b/tests/check_loop.c
index d1842bc..9a19b82 100644
--- a/tests/check_loop.c
+++ b/tests/check_loop.c
@@ -393,6 +393,41 @@ START_TEST(test_loop_timer_expire_leak)
}
END_TEST
+static int received_signum = 0;
+
+static int32_t
+sig_handler(int32_t rsignal, void *data)
+{
+ qb_loop_t *l = (qb_loop_t *)data;
+ qb_log(LOG_DEBUG, "caught signal %d", rsignal);
+ received_signum = rsignal;
+ qb_loop_stop(l);
+ return -1;
+}
+
+START_TEST(test_loop_sig_handling)
+{
+ qb_loop_signal_handle handle;
+ qb_loop_t *l = qb_loop_create();
+ fail_if(l == NULL);
+
+ qb_loop_signal_add(l, QB_LOOP_HIGH, SIGINT,
+ l, sig_handler, &handle);
+ qb_loop_signal_add(l, QB_LOOP_HIGH, SIGTERM,
+ l, sig_handler, &handle);
+ qb_loop_signal_add(l, QB_LOOP_HIGH, SIGQUIT,
+ l, sig_handler, &handle);
+ kill(getpid(), SIGINT);
+ qb_loop_run(l);
+ ck_assert_int_eq(received_signum, SIGINT);
+ kill(getpid(), SIGQUIT);
+ qb_loop_run(l);
+ ck_assert_int_eq(received_signum, SIGQUIT);
+
+ qb_loop_destroy(l);
+}
+END_TEST
+
static Suite *loop_timer_suite(void)
{
TCase *tc;
@@ -417,6 +452,10 @@ static Suite *loop_timer_suite(void)
tcase_set_timeout(tc, 30);
suite_add_tcase(s, tc);
+ tc = tcase_create("signals");
+ tcase_add_test(tc, test_loop_sig_handling);
+ tcase_set_timeout(tc, 10);
+ suite_add_tcase(s, tc);
return s;
}
--
1.7.7.5
12 years, 3 months