diff --git a/check b/check index 1ade50a..a4e841f 100755 --- a/check +++ b/check @@ -1,334 +1,334 @@ #!/bin/bash export CFLAGS="$CFLAGS" export MAKEFLAGS="$MAKEFLAGS --no-print-directory" help_all() { echo echo "Usage: check " echo echo "Commands:" echo echo " ansi Check using ansi compiler option" echo " sysv Check using sys-v semaphores" echo " nosection Check without gcc __attribute__(section)" echo " noepoll Check using poll (not epoll)" echo " nogettime Check without gettime()" echo " bsd Check with a bsd-like config" echo " mac Check with a mac/darwin-like config" echo " dist do make distcheck" echo " rpm Run rpmlint" echo " mock Test doing a mock build" echo " coverity Run coverity" echo " clang Run clang-analyze" echo " abi Check abi compatibility" echo " api_sanity api sanity test" echo echo " help This help" echo exit 1 } if [ $# -lt 1 ] then help_all fi command=$1 shift args="$@" if [ -n "$(git rev-parse)" ] ; then perror "Must be inside a git repository to work" exit 1 fi up=$(git rev-parse --show-cdup) if [ "x$up" == "x" ] ; then up="." fi cd $up set -e if [ ! -e build-aux/install-sh ] then ./autogen.sh fi if [ ! -e Makefile ] then ./configure --quiet fi check() { options="$1 --enable-debug --enable-slow-tests --enable-fatal-warnings --quiet" echo "./configure $options" echo "ENV CFLAGS=\"$CFLAGS\"" echo "ENV MAKEFLAGS=\"$MAKEFLAGS\"" ( ./configure $options ) make check if [ $? -ne 0 ] then echo "======================" echo failed: $1 echo "======================" if [ -f tests/test-suite.log ] then cat tests/test-suite.log fi exit 1 fi } check_ansi() { echo "checking ansi" echo "===============" check "--enable-ansi" } check_nosection() { echo "checking nosection" echo "=======================" # no __attribute__((section)) check "ac_cv_link_attribute_section=no" } check_sysv() { # use sys-v semaphores echo "checking sysv" echo "=======================" ORIG_CFLAGS=$CFLAGS export CFLAGS="$CFLAGS -DDISABLE_POSIX_THREAD_PROCESS_SHARED" check export CFLAGS=$ORIG_CFLAGS } check_nogettime() { # no clock_gettime echo "checking nogettime" echo "=======================" check "ac_cv_func_clock_gettime=no" } check_noepoll() { # no epoll echo "checking noepoll" echo "=======================" check "ac_cv_func_epoll_create1=no ac_cv_func_epoll_create=no" } check_bsd() { # bsd-like echo "checking bsd" echo "=======================" ORIG_CFLAGS=$CFLAGS check "ac_cv_func_sem_timedwait=no ac_cv_func_clock_gettime=no ac_cv_func_epoll_create1=no ac_cv_func_epoll_create=no" export CFLAGS=$ORIG_CFLAGS } check_mac() { # mac-like echo "checking mac" echo "=======================" ORIG_CFLAGS=$CFLAGS export CFLAGS="$CFLAGS -DDISABLE_POSIX_THREAD_PROCESS_SHARED" check "ac_cv_func_clock_gettime=no ac_cv_func_epoll_create1=no ac_cv_func_epoll_create=no ac_cv_link_attribute_section=no" export CFLAGS=$ORIG_CFLAGS } check_dist() { # normal configure with distcheck echo "checking dist" echo "======================" set +e ./configure --quiet make distcheck set -e } check_rpm() { echo "checking rpm building" echo "======================" set +e make maintainer-clean ./autogen.sh ./configure --quiet make rpm echo sudo rpm -Uvf --force libqb-*.rpm echo rpmlint libqb rpmlint libqb echo rpmlint libqb-debuginfo rpmlint libqb-debuginfo echo rpmlint libqb-devel rpmlint libqb-devel set -e } check_mock() { echo "checking mock building" echo "======================" set +e make maintainer-clean rm -f *.rpm ./autogen.sh ./configure --quiet make srpm mock --no-clean --rebuild *.src.rpm } check_coverity() { echo "checking coverity" echo "======================" make clean cov-build --dir=cov make cov-analyze --dir cov \ --concurrency \ --all \ --aggressiveness-level high \ --security \ --wait-for-license cov-format-errors --dir cov } check_clang() { - if [ ! -f /usr/lib64/clang-analyzer/scan-build/ccc-analyzer ] + if [ ! -f /usr/libexec/clang-analyzer/scan-build/ccc-analyzer ] then echo try installing clang-analyze exit 1 fi echo "checking clang" echo "====================" make clean ./configure \ - CC=/usr/lib64/clang-analyzer/scan-build/ccc-analyzer \ - CXX=/usr/lib64/clang-analyzer/scan-build/c++-analyzer + CC=/usr/libexec/clang-analyzer/scan-build/ccc-analyzer \ + CXX=/usr/libexec/clang-analyzer/scan-build/c++-analyzer make check } check_abi() { ver1=$1 ver2=$2 if [ -z "$ver1" ] ; then echo need two versions. exit 1 fi if [ -z "$ver2" ] ; then echo need two versions. exit 1 fi TMPL=build-aux/abi-check-templ.xml checker=abi-compliance-checker mkdir -p abi_dumps/libqb for v in $ver1 $ver2 do p=$(pwd)_inst_$v sed -e "s|@PREFIX@|$p|" -e "s|@VERSION@|$v|" $TMPL > abi_dumps/libqb/$v.xml done for v in $ver1 $ver2 do p=$(pwd)_inst_$v t=v$v b=api-check-$v echo "== Version $v ==" if [ ! -f abi_dumps/libqb/libqb_$v.abi.tar.gz ] then git checkout -B $b $t ./autogen.sh ./configure make make install DESTDIR=$p $checker -l libqb -dump_abi abi_dumps/libqb/$v.xml fi done $checker -l libqb \ -d1 abi_dumps/libqb/libqb_$ver1.abi.tar.gz \ -d2 abi_dumps/libqb/libqb_$ver2.abi.tar.gz google-chrome compat_reports/libqb/$ver1\_to_$ver2/abi_compat_report.html echo mv compat_reports/libqb/$ver1\_to_$ver2/abi_compat_report.html abi_compat_report_$ver1\_to_$ver2.html echo scp abi_compat_report_$ver1\_to_$ver2.html fedorahosted.org:quarterback git checkout master } check_api_sanity() { make export CFLAGS="-Wall -ggdb2" api-sanity-checker -l libqb -d build-aux/api-auto-test.xml -gen -build -run google-chrome test_results/libqb/master/test_results.html google-chrome test_results/libqb/master/test_results.html } check_all() { check_ansi check_nosection check_sysv check_noepoll check_nogettime check_bsd check_dist check_rpm } case $command in help) help_all $args ;; ansi) check_ansi ;; nosection) check_nosection ;; sysv) check_sysv ;; noepoll) check_noepoll ;; nogettime) check_nogettime ;; bsd) check_bsd ;; mac) check_mac ;; rpm) check_rpm ;; mock) check_mock ;; dist) check_dist ;; coverity) check_coverity ;; clang) check_clang ;; abi) check_abi $args ;; api_sanity) check_api_sanity ;; all) check_all ;; *) help_all ;; esac cd - exit 0 diff --git a/lib/ipcs.c b/lib/ipcs.c index 96e04bc..dd968d6 100644 --- a/lib/ipcs.c +++ b/lib/ipcs.c @@ -1,968 +1,965 @@ /* * Copyright (C) 2010 Red Hat, Inc. * * Author: Angus Salkeld * * 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 . */ #include "os_base.h" #include "util_int.h" #include "ipc_int.h" #include #include #include static void qb_ipcs_flowcontrol_set(struct qb_ipcs_connection *c, int32_t fc_enable); static int32_t new_event_notification(struct qb_ipcs_connection * c); static QB_LIST_DECLARE(qb_ipc_services); qb_ipcs_service_t * qb_ipcs_create(const char *name, int32_t service_id, enum qb_ipc_type type, struct qb_ipcs_service_handlers *handlers) { struct qb_ipcs_service *s; s = calloc(1, sizeof(struct qb_ipcs_service)); if (s == NULL) { return NULL; } if (type == QB_IPC_NATIVE) { #ifdef DISABLE_IPC_SHM s->type = QB_IPC_SOCKET; #else s->type = QB_IPC_SHM; #endif /* DISABLE_IPC_SHM */ } else { s->type = type; } s->pid = getpid(); s->needs_sock_for_poll = QB_FALSE; s->poll_priority = QB_LOOP_MED; /* Initial alloc ref */ qb_ipcs_ref(s); s->service_id = service_id; (void)strlcpy(s->name, name, NAME_MAX); s->serv_fns.connection_accept = handlers->connection_accept; s->serv_fns.connection_created = handlers->connection_created; s->serv_fns.msg_process = handlers->msg_process; s->serv_fns.connection_closed = handlers->connection_closed; s->serv_fns.connection_destroyed = handlers->connection_destroyed; qb_list_init(&s->connections); qb_list_init(&s->list); qb_list_add(&s->list, &qb_ipc_services); return s; } void qb_ipcs_poll_handlers_set(struct qb_ipcs_service *s, struct qb_ipcs_poll_handlers *handlers) { s->poll_fns.job_add = handlers->job_add; s->poll_fns.dispatch_add = handlers->dispatch_add; s->poll_fns.dispatch_mod = handlers->dispatch_mod; s->poll_fns.dispatch_del = handlers->dispatch_del; } void qb_ipcs_service_context_set(qb_ipcs_service_t* s, void *context) { s->context = context; } void * qb_ipcs_service_context_get(qb_ipcs_service_t* s) { return s->context; } int32_t qb_ipcs_run(struct qb_ipcs_service *s) { int32_t res = 0; if (s->poll_fns.dispatch_add == NULL || s->poll_fns.dispatch_mod == NULL || s->poll_fns.dispatch_del == NULL) { res = -EINVAL; goto run_cleanup; } switch (s->type) { case QB_IPC_SOCKET: qb_ipcs_us_init((struct qb_ipcs_service *)s); break; case QB_IPC_SHM: #ifdef DISABLE_IPC_SHM res = -ENOTSUP; #else qb_ipcs_shm_init((struct qb_ipcs_service *)s); #endif /* DISABLE_IPC_SHM */ break; case QB_IPC_POSIX_MQ: case QB_IPC_SYSV_MQ: res = -ENOTSUP; break; default: res = -EINVAL; break; } if (res == 0) { res = qb_ipcs_us_publish(s); if (res < 0) { (void)qb_ipcs_us_withdraw(s); goto run_cleanup; } } run_cleanup: if (res < 0) { /* Failed to run services, removing initial alloc reference. */ qb_ipcs_unref(s); } return res; } static int32_t _modify_dispatch_descriptor_(struct qb_ipcs_connection *c) { qb_ipcs_dispatch_mod_fn disp_mod = c->service->poll_fns.dispatch_mod; if (c->service->type == QB_IPC_SOCKET) { return disp_mod(c->service->poll_priority, c->event.u.us.sock, c->poll_events, c, qb_ipcs_dispatch_connection_request); } else { return disp_mod(c->service->poll_priority, c->setup.u.us.sock, c->poll_events, c, qb_ipcs_dispatch_connection_request); } return -EINVAL; } void qb_ipcs_request_rate_limit(struct qb_ipcs_service *s, enum qb_ipcs_rate_limit rl) { struct qb_ipcs_connection *c; enum qb_loop_priority old_p = s->poll_priority; struct qb_list_head *pos; struct qb_list_head *n; switch (rl) { case QB_IPCS_RATE_FAST: s->poll_priority = QB_LOOP_HIGH; break; case QB_IPCS_RATE_SLOW: case QB_IPCS_RATE_OFF: case QB_IPCS_RATE_OFF_2: s->poll_priority = QB_LOOP_LOW; break; default: case QB_IPCS_RATE_NORMAL: s->poll_priority = QB_LOOP_MED; break; } qb_list_for_each_safe(pos, n, &s->connections) { c = qb_list_entry(pos, struct qb_ipcs_connection, list); qb_ipcs_connection_ref(c); if (rl == QB_IPCS_RATE_OFF) { qb_ipcs_flowcontrol_set(c, 1); } else if (rl == QB_IPCS_RATE_OFF_2) { qb_ipcs_flowcontrol_set(c, 2); } else { qb_ipcs_flowcontrol_set(c, QB_FALSE); } if (old_p != s->poll_priority) { (void)_modify_dispatch_descriptor_(c); } qb_ipcs_connection_unref(c); } } void qb_ipcs_ref(struct qb_ipcs_service *s) { qb_atomic_int_inc(&s->ref_count); } void qb_ipcs_unref(struct qb_ipcs_service *s) { int32_t free_it; assert(s->ref_count > 0); free_it = qb_atomic_int_dec_and_test(&s->ref_count); if (free_it) { qb_util_log(LOG_DEBUG, "%s() - destroying", __func__); free(s); } } void qb_ipcs_destroy(struct qb_ipcs_service *s) { struct qb_ipcs_connection *c = NULL; struct qb_list_head *pos; struct qb_list_head *n; if (s == NULL) { return; } qb_list_for_each_safe(pos, n, &s->connections) { c = qb_list_entry(pos, struct qb_ipcs_connection, list); if (c == NULL) { continue; } qb_ipcs_disconnect(c); } (void)qb_ipcs_us_withdraw(s); /* service destroyed, remove initial alloc ref */ qb_ipcs_unref(s); } /* * connection API */ static struct qb_ipc_one_way * _event_sock_one_way_get(struct qb_ipcs_connection * c) { if (c->service->needs_sock_for_poll) { return &c->setup; } if (c->event.type == QB_IPC_SOCKET) { return &c->event; } return NULL; } static struct qb_ipc_one_way * _response_sock_one_way_get(struct qb_ipcs_connection * c) { if (c->service->needs_sock_for_poll) { return &c->setup; } if (c->response.type == QB_IPC_SOCKET) { return &c->response; } return NULL; } ssize_t qb_ipcs_response_send(struct qb_ipcs_connection *c, const void *data, size_t size) { ssize_t res; if (c == NULL) { return -EINVAL; } qb_ipcs_connection_ref(c); res = c->service->funcs.send(&c->response, data, size); if (res == size) { c->stats.responses++; } else if (res == -EAGAIN || res == -ETIMEDOUT) { struct qb_ipc_one_way *ow = _response_sock_one_way_get(c); if (ow) { ssize_t res2 = qb_ipc_us_ready(ow, &c->setup, 0, POLLOUT); if (res2 < 0) { res = res2; } } c->stats.send_retries++; } qb_ipcs_connection_unref(c); return res; } ssize_t qb_ipcs_response_sendv(struct qb_ipcs_connection * c, const struct iovec * iov, size_t iov_len) { ssize_t res; if (c == NULL) { return -EINVAL; } qb_ipcs_connection_ref(c); res = c->service->funcs.sendv(&c->response, iov, iov_len); if (res > 0) { c->stats.responses++; } else if (res == -EAGAIN || res == -ETIMEDOUT) { struct qb_ipc_one_way *ow = _response_sock_one_way_get(c); if (ow) { ssize_t res2 = qb_ipc_us_ready(ow, &c->setup, 0, POLLOUT); if (res2 < 0) { res = res2; } } c->stats.send_retries++; } qb_ipcs_connection_unref(c); return res; } static int32_t resend_event_notifications(struct qb_ipcs_connection *c) { ssize_t res = 0; if (!c->service->needs_sock_for_poll) { return res; } if (c->outstanding_notifiers > 0) { res = qb_ipc_us_send(&c->setup, c->receive_buf, c->outstanding_notifiers); } if (res > 0) { c->outstanding_notifiers -= res; } assert(c->outstanding_notifiers >= 0); if (c->outstanding_notifiers == 0) { c->poll_events = POLLIN | POLLPRI | POLLNVAL; (void)_modify_dispatch_descriptor_(c); } return res; } static int32_t new_event_notification(struct qb_ipcs_connection * c) { ssize_t res = 0; if (!c->service->needs_sock_for_poll) { return res; } assert(c->outstanding_notifiers >= 0); if (c->outstanding_notifiers > 0) { c->outstanding_notifiers++; res = resend_event_notifications(c); } else { res = qb_ipc_us_send(&c->setup, &c->outstanding_notifiers, 1); if (res == -EAGAIN) { /* * notify the client later, when we can. */ c->outstanding_notifiers++; c->poll_events = POLLOUT | POLLIN | POLLPRI | POLLNVAL; (void)_modify_dispatch_descriptor_(c); } } return res; } ssize_t qb_ipcs_event_send(struct qb_ipcs_connection * c, const void *data, size_t size) { ssize_t res; ssize_t resn; if (c == NULL) { return -EINVAL; } else if (size > c->event.max_msg_size) { return -EMSGSIZE; } qb_ipcs_connection_ref(c); res = c->service->funcs.send(&c->event, data, size); if (res == size) { c->stats.events++; resn = new_event_notification(c); if (resn < 0 && resn != -EAGAIN && resn != -ENOBUFS) { errno = -resn; qb_util_perror(LOG_WARNING, "new_event_notification (%s)", c->description); res = resn; } } else if (res == -EAGAIN || res == -ETIMEDOUT) { struct qb_ipc_one_way *ow = _event_sock_one_way_get(c); if (c->outstanding_notifiers > 0) { resn = resend_event_notifications(c); } if (ow) { resn = qb_ipc_us_ready(ow, &c->setup, 0, POLLOUT); if (resn < 0) { res = resn; } } c->stats.send_retries++; } qb_ipcs_connection_unref(c); return res; } ssize_t qb_ipcs_event_sendv(struct qb_ipcs_connection * c, const struct iovec * iov, size_t iov_len) { ssize_t res; ssize_t resn; if (c == NULL) { return -EINVAL; } qb_ipcs_connection_ref(c); res = c->service->funcs.sendv(&c->event, iov, iov_len); if (res > 0) { c->stats.events++; resn = new_event_notification(c); if (resn < 0 && resn != -EAGAIN) { errno = -resn; qb_util_perror(LOG_WARNING, "new_event_notification (%s)", c->description); res = resn; } } else if (res == -EAGAIN || res == -ETIMEDOUT) { struct qb_ipc_one_way *ow = _event_sock_one_way_get(c); if (c->outstanding_notifiers > 0) { resn = resend_event_notifications(c); } if (ow) { resn = qb_ipc_us_ready(ow, &c->setup, 0, POLLOUT); if (resn < 0) { res = resn; } } c->stats.send_retries++; } qb_ipcs_connection_unref(c); return res; } qb_ipcs_connection_t * qb_ipcs_connection_first_get(struct qb_ipcs_service * s) { struct qb_ipcs_connection *c; if (qb_list_empty(&s->connections)) { return NULL; } c = qb_list_first_entry(&s->connections, struct qb_ipcs_connection, list); qb_ipcs_connection_ref(c); return c; } qb_ipcs_connection_t * qb_ipcs_connection_next_get(struct qb_ipcs_service * s, struct qb_ipcs_connection * current) { struct qb_ipcs_connection *c; if (current == NULL || qb_list_is_last(¤t->list, &s->connections)) { return NULL; } c = qb_list_first_entry(¤t->list, struct qb_ipcs_connection, list); qb_ipcs_connection_ref(c); return c; } int32_t qb_ipcs_service_id_get(struct qb_ipcs_connection * c) { if (c == NULL) { return -EINVAL; } return c->service->service_id; } struct qb_ipcs_connection * qb_ipcs_connection_alloc(struct qb_ipcs_service *s) { struct qb_ipcs_connection *c = calloc(1, sizeof(struct qb_ipcs_connection)); if (c == NULL) { return NULL; } c->pid = 0; c->euid = -1; c->egid = -1; c->receive_buf = NULL; c->context = NULL; c->fc_enabled = QB_FALSE; c->state = QB_IPCS_CONNECTION_INACTIVE; c->poll_events = POLLIN | POLLPRI | POLLNVAL; c->setup.type = s->type; c->request.type = s->type; c->response.type = s->type; c->event.type = s->type; (void)strlcpy(c->description, "not set yet", CONNECTION_DESCRIPTION); /* initial alloc ref */ qb_ipcs_connection_ref(c); /* * The connection makes use of the service object. Give the connection * a reference to the service so we know the service can never be destroyed * until the connection is done with it. */ qb_ipcs_ref(s); c->service = s; qb_list_init(&c->list); return c; } void qb_ipcs_connection_ref(struct qb_ipcs_connection *c) { if (c) { qb_atomic_int_inc(&c->refcount); } } void qb_ipcs_connection_unref(struct qb_ipcs_connection *c) { int32_t free_it; if (c == NULL) { return; } if (c->refcount < 1) { qb_util_log(LOG_ERR, "ref:%d state:%d (%s)", c->refcount, c->state, c->description); assert(0); } free_it = qb_atomic_int_dec_and_test(&c->refcount); if (free_it) { qb_list_del(&c->list); if (c->service->serv_fns.connection_destroyed) { c->service->serv_fns.connection_destroyed(c); } c->service->funcs.disconnect(c); /* Let go of the connection's reference to the service */ qb_ipcs_unref(c->service); free(c->receive_buf); free(c); } } void qb_ipcs_disconnect(struct qb_ipcs_connection *c) { int32_t res = 0; qb_loop_job_dispatch_fn rerun_job; if (c == NULL) { return; } qb_util_log(LOG_DEBUG, "%s(%s) state:%d", __func__, c->description, c->state); if (c->state == QB_IPCS_CONNECTION_ACTIVE) { c->service->funcs.disconnect(c); c->state = QB_IPCS_CONNECTION_INACTIVE; c->service->stats.closed_connections++; /* This removes the initial alloc ref */ qb_ipcs_connection_unref(c); /* return early as it's an incomplete connection. */ return; } if (c->state == QB_IPCS_CONNECTION_ESTABLISHED) { c->service->funcs.disconnect(c); c->state = QB_IPCS_CONNECTION_SHUTTING_DOWN; c->service->stats.active_connections--; c->service->stats.closed_connections++; } if (c->state == QB_IPCS_CONNECTION_SHUTTING_DOWN) { int scheduled_retry = 0; res = 0; if (c->service->serv_fns.connection_closed) { res = c->service->serv_fns.connection_closed(c); } if (res != 0) { /* OK, so they want the connection_closed * function re-run */ rerun_job = (qb_loop_job_dispatch_fn) qb_ipcs_disconnect; res = c->service->poll_fns.job_add(QB_LOOP_LOW, c, rerun_job); if (res == 0) { /* this function is going to be called again. * so hold off on the unref */ scheduled_retry = 1; } } if (scheduled_retry == 0) { /* This removes the initial alloc ref */ qb_ipcs_connection_unref(c); } } } static void qb_ipcs_flowcontrol_set(struct qb_ipcs_connection *c, int32_t fc_enable) { if (c == NULL) { return; } if (c->fc_enabled != fc_enable) { c->service->funcs.fc_set(&c->request, fc_enable); c->fc_enabled = fc_enable; c->stats.flow_control_state = fc_enable; c->stats.flow_control_count++; } } static int32_t _process_request_(struct qb_ipcs_connection *c, int32_t ms_timeout) { int32_t res = 0; ssize_t size; struct qb_ipc_request_header *hdr; - qb_ipcs_connection_ref(c); - if (c->service->funcs.peek && c->service->funcs.reclaim) { size = c->service->funcs.peek(&c->request, (void **)&hdr, ms_timeout); } else { hdr = c->receive_buf; size = c->service->funcs.recv(&c->request, hdr, c->request.max_msg_size, ms_timeout); } if (size < 0) { if (size != -EAGAIN && size != -ETIMEDOUT) { qb_util_perror(LOG_DEBUG, "recv from client connection failed (%s)", c->description); } else { c->stats.recv_retries++; } res = size; goto cleanup; } else if (size == 0 || hdr->id == QB_IPC_MSG_DISCONNECT) { qb_util_log(LOG_DEBUG, "client requesting a disconnect (%s)", c->description); res = -ESHUTDOWN; goto cleanup; } else { c->stats.requests++; res = c->service->serv_fns.msg_process(c, hdr, hdr->size); /* 0 == good, negative == backoff */ if (res < 0) { res = -ENOBUFS; } else { res = size; } } if (c && c->service->funcs.peek && c->service->funcs.reclaim) { c->service->funcs.reclaim(&c->request); } cleanup: - qb_ipcs_connection_unref(c); return res; } #define IPC_REQUEST_TIMEOUT 10 #define MAX_RECV_MSGS 50 static ssize_t _request_q_len_get(struct qb_ipcs_connection *c) { ssize_t q_len; if (c->service->funcs.q_len_get) { q_len = c->service->funcs.q_len_get(&c->request); if (q_len <= 0) { return q_len; } if (c->service->poll_priority == QB_LOOP_MED) { q_len = QB_MIN(q_len, 5); } else if (c->service->poll_priority == QB_LOOP_LOW) { q_len = 1; } else { q_len = QB_MIN(q_len, MAX_RECV_MSGS); } } else { q_len = 1; } return q_len; } int32_t qb_ipcs_dispatch_connection_request(int32_t fd, int32_t revents, void *data) { struct qb_ipcs_connection *c = (struct qb_ipcs_connection *)data; char bytes[MAX_RECV_MSGS]; int32_t res = 0; int32_t res2; int32_t recvd = 0; ssize_t avail; if (revents & POLLNVAL) { qb_util_log(LOG_DEBUG, "NVAL conn (%s)", c->description); res = -EINVAL; goto dispatch_cleanup; } if (revents & POLLHUP) { qb_util_log(LOG_DEBUG, "HUP conn (%s)", c->description); res = -ESHUTDOWN; goto dispatch_cleanup; } if (revents & POLLOUT) { /* try resend events now that fd can write */ res = resend_event_notifications(c); if (res < 0 && res != -EAGAIN) { errno = -res; qb_util_perror(LOG_WARNING, "resend_event_notifications (%s)", c->description); } /* nothing to read */ if ((revents & POLLIN) == 0) { res = 0; goto dispatch_cleanup; } } if (c->fc_enabled) { res = 0; goto dispatch_cleanup; } avail = _request_q_len_get(c); if (c->service->needs_sock_for_poll && avail == 0) { res2 = qb_ipc_us_recv(&c->setup, bytes, 1, 0); if (qb_ipc_us_sock_error_is_disconnected(res2)) { errno = -res2; qb_util_perror(LOG_WARNING, "conn (%s) disconnected", c->description); res = -ESHUTDOWN; goto dispatch_cleanup; } else { qb_util_log(LOG_WARNING, "conn (%s) Nothing in q but got POLLIN on fd:%d (res2:%d)", c->description, fd, res2); res = 0; goto dispatch_cleanup; } } do { res = _process_request_(c, IPC_REQUEST_TIMEOUT); if (res == -ESHUTDOWN) { goto dispatch_cleanup; } if (res > 0 || res == -ENOBUFS || res == -EINVAL) { recvd++; } if (res > 0) { avail--; } } while (avail > 0 && res > 0 && !c->fc_enabled); if (c->service->needs_sock_for_poll && recvd > 0) { res2 = qb_ipc_us_recv(&c->setup, bytes, recvd, -1); if (qb_ipc_us_sock_error_is_disconnected(res2)) { errno = -res2; qb_util_perror(LOG_ERR, "error receiving from setup sock (%s)", c->description); res = -ESHUTDOWN; goto dispatch_cleanup; } } res = QB_MIN(0, res); if (res == -EAGAIN || res == -ETIMEDOUT || res == -ENOBUFS) { res = 0; } if (res != 0) { if (res != -ENOTCONN) { /* * Abnormal state (ENOTCONN is normal shutdown). */ errno = -res; qb_util_perror(LOG_ERR, "request returned error (%s)", c->description); } } dispatch_cleanup: if (res != 0) { qb_ipcs_disconnect(c); } return res; } void qb_ipcs_context_set(struct qb_ipcs_connection *c, void *context) { if (c == NULL) { return; } c->context = context; } void * qb_ipcs_context_get(struct qb_ipcs_connection *c) { if (c == NULL) { return NULL; } return c->context; } void * qb_ipcs_connection_service_context_get(qb_ipcs_connection_t *c) { if (c == NULL || c->service == NULL) { return NULL; } return c->service->context; } int32_t qb_ipcs_connection_stats_get(qb_ipcs_connection_t * c, struct qb_ipcs_connection_stats * stats, int32_t clear_after_read) { if (c == NULL) { return -EINVAL; } memcpy(stats, &c->stats, sizeof(struct qb_ipcs_connection_stats)); if (clear_after_read) { memset(&c->stats, 0, sizeof(struct qb_ipcs_connection_stats_2)); c->stats.client_pid = c->pid; } return 0; } struct qb_ipcs_connection_stats_2* qb_ipcs_connection_stats_get_2(qb_ipcs_connection_t *c, int32_t clear_after_read) { struct qb_ipcs_connection_stats_2 * stats; if (c == NULL) { errno = EINVAL; return NULL; } stats = calloc(1, sizeof(struct qb_ipcs_connection_stats_2)); if (stats == NULL) { return NULL; } memcpy(stats, &c->stats, sizeof(struct qb_ipcs_connection_stats_2)); if (c->service->funcs.q_len_get) { stats->event_q_length = c->service->funcs.q_len_get(&c->event); } else { stats->event_q_length = 0; } if (clear_after_read) { memset(&c->stats, 0, sizeof(struct qb_ipcs_connection_stats_2)); c->stats.client_pid = c->pid; } return stats; } int32_t qb_ipcs_stats_get(struct qb_ipcs_service * s, struct qb_ipcs_stats * stats, int32_t clear_after_read) { if (s == NULL) { return -EINVAL; } memcpy(stats, &s->stats, sizeof(struct qb_ipcs_stats)); if (clear_after_read) { memset(&s->stats, 0, sizeof(struct qb_ipcs_stats)); } return 0; } void qb_ipcs_connection_auth_set(qb_ipcs_connection_t *c, uid_t uid, gid_t gid, mode_t mode) { if (c) { c->auth.uid = uid; c->auth.gid = gid; c->auth.mode = mode; } } int32_t qb_ipcs_connection_get_buffer_size(qb_ipcs_connection_t *c) { if (c == NULL) { return -EINVAL; } /* request, response, and event shoud all have the same * buffer size allocated. It doesn't matter which we return * here. */ return c->response.max_msg_size; } void qb_ipcs_enforce_buffer_size(qb_ipcs_service_t *s, uint32_t buf_size) { if (s == NULL) { return; } s->max_buffer_size = buf_size; } diff --git a/lib/trie.c b/lib/trie.c index 56001a2..a7068f3 100644 --- a/lib/trie.c +++ b/lib/trie.c @@ -1,797 +1,796 @@ /* * Copyright (C) 2011 Red Hat, Inc. * * Author: Angus Salkeld * * 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 . */ #include #include #include #include #include #include "map_int.h" struct trie_iter { struct qb_map_iter i; const char *prefix; struct trie_node *n; struct trie_node *root; }; struct trie_node { uint32_t idx; char *segment; uint32_t num_segments; char *key; void *value; struct trie_node **children; uint32_t num_children; uint32_t refcount; struct trie_node *parent; struct qb_list_head notifier_head; }; struct trie { struct qb_map map; size_t length; uint32_t num_nodes; uint32_t mem_used; struct trie_node *header; }; static void trie_notify(struct trie_node *n, uint32_t event, const char *key, void *old_value, void *value); static struct trie_node *trie_new_node(struct trie *t, struct trie_node *parent); /* * characters are stored in reverse to make accessing the * more common case (non-control chars) more space efficient. */ #define TRIE_CHAR2INDEX(ch) (126 - ch) #define TRIE_INDEX2CHAR(idx) (126 - idx) static int32_t trie_node_alive(struct trie_node *node) { if (node->value == NULL || node->refcount <= 0) { return QB_FALSE; } return QB_TRUE; } static struct trie_node * trie_node_next(struct trie_node *node, struct trie_node *root, int all) { struct trie_node *c = node; struct trie_node *n; struct trie_node *p; int i; keep_going: n = NULL; /* child/outward */ for (i = c->num_children - 1; i >= 0; i--) { if (c->children[i]) { n = c->children[i]; break; } } if (n) { if (all || trie_node_alive(n)) { return n; } else { c = n; goto keep_going; } } /* sibling/parent */ if (c == root) { return NULL; } p = c; do { for (i = p->idx - 1; i >= 0; i--) { if (p->parent->children[i]) { n = p->parent->children[i]; break; } } if (n == NULL) { p = p->parent; } } while (n == NULL && p != root); if (n) { if (all || trie_node_alive(n)) { return n; } if (n == root) { return NULL; } c = n; goto keep_going; } return n; } static struct trie_node * new_child_node(struct trie *t, struct trie_node * parent, char ch) { struct trie_node *new_node; int old_max_idx; int i; int idx = TRIE_CHAR2INDEX(ch); if (idx >= parent->num_children) { old_max_idx = parent->num_children; parent->num_children = QB_MAX(idx + 1, 30); t->mem_used += (sizeof(struct trie_node*) * (parent->num_children - old_max_idx)); parent->children = realloc(parent->children, (parent->num_children * sizeof(struct trie_node*))); if (parent->children == NULL) { return NULL; } for (i = old_max_idx; i < parent->num_children; i++) { parent->children[i] = NULL; } } new_node = trie_new_node(t, parent); if (new_node == NULL) { return NULL; } new_node->idx = idx; parent->children[idx] = new_node; return new_node; } static struct trie_node * trie_node_split(struct trie *t, struct trie_node *cur_node, int seg_cnt) { struct trie_node *split_node; struct trie_node ** children = cur_node->children; uint32_t num_children = cur_node->num_children; int i; int s; cur_node->children = NULL; cur_node->num_children = 0; split_node = new_child_node(t, cur_node, cur_node->segment[seg_cnt]); if (split_node == NULL) { return NULL; } split_node->children = children; split_node->num_children = num_children; for (i = 0; i < split_node->num_children; i++) { if (split_node->children[i]) { split_node->children[i]->parent = split_node; } } split_node->value = cur_node->value; split_node->key = cur_node->key; split_node->refcount = cur_node->refcount; cur_node->value = NULL; cur_node->key = NULL; cur_node->refcount = 0; /* move notifier list to split */ qb_list_replace(&cur_node->notifier_head, &split_node->notifier_head); qb_list_init(&cur_node->notifier_head); if (seg_cnt < cur_node->num_segments) { split_node->num_segments = cur_node->num_segments - seg_cnt - 1; split_node->segment = malloc(split_node->num_segments * sizeof(char)); if (split_node->segment == NULL) { free(split_node); return NULL; } for (i = (seg_cnt + 1); i < cur_node->num_segments; i++) { s = i - seg_cnt - 1; split_node->segment[s] = cur_node->segment[i]; cur_node->segment[i] = '\0'; } cur_node->num_segments = seg_cnt; } return cur_node; } static struct trie_node * trie_insert(struct trie *t, const char *key) { struct trie_node *cur_node = t->header; struct trie_node *new_node; char *cur = (char *)key; int idx = TRIE_CHAR2INDEX(key[0]); int seg_cnt = 0; do { new_node = NULL; if (cur_node->num_segments > 0 && seg_cnt < cur_node->num_segments) { if (cur_node->segment[seg_cnt] == *cur) { /* we found the char in the segment */ seg_cnt++; } else { cur_node = trie_node_split(t, cur_node, seg_cnt); if (cur_node == NULL) { return NULL; } new_node = new_child_node(t, cur_node, *cur); if (new_node == NULL) { return NULL; } } } else if (idx < cur_node->num_children && cur_node->children[idx]) { /* the char can be found on the next node */ new_node = cur_node->children[idx]; } else if (cur_node == t->header) { /* the root node is empty so make it on the next node */ new_node = new_child_node(t, cur_node, *cur); if (new_node == NULL) { return NULL; } } else if (cur_node->value == NULL && qb_list_empty(&cur_node->notifier_head) && cur_node->num_children == 0 && seg_cnt == cur_node->num_segments) { /* we are on a leaf (with no value) so just add it as a segment */ cur_node->segment = realloc(cur_node->segment, cur_node->num_segments + 1); cur_node->segment[cur_node->num_segments] = *cur; t->mem_used += sizeof(char); cur_node->num_segments++; seg_cnt++; } else if (seg_cnt == cur_node->num_segments) { /* on the last segment need to make a new node */ new_node = new_child_node(t, cur_node, *cur); if (new_node == NULL) { return NULL; } } else /* need_to_split */ { cur_node = trie_node_split(t, cur_node, seg_cnt); if (cur_node == NULL) { return NULL; } new_node = new_child_node(t, cur_node, *cur); if (new_node == NULL) { return NULL; } } if (new_node) { seg_cnt = 0; cur_node = new_node; } cur++; idx = TRIE_CHAR2INDEX(*cur); } while (*cur != '\0'); if (cur_node->num_segments > 0 && seg_cnt < cur_node->num_segments) { /* we need to split */ cur_node = trie_node_split(t, cur_node, seg_cnt); if (cur_node == NULL) { return NULL; } new_node = new_child_node(t, cur_node, *cur); if (new_node == NULL) { return NULL; } } return cur_node; } static struct trie_node * trie_lookup(struct trie *t, const char *key, int exact_match) { struct trie_node *cur_node = t->header; char *cur = (char *)key; int idx = TRIE_CHAR2INDEX(key[0]); int seg_cnt = 0; do { if (cur_node->num_segments > 0 && seg_cnt < cur_node->num_segments) { if (cur_node->segment[seg_cnt] == *cur) { /* we found the char in the segment */ seg_cnt++; } else { return NULL; } } else if (idx < cur_node->num_children && cur_node->children[idx]) { /* the char can be found on the next node */ cur_node = cur_node->children[idx]; seg_cnt = 0; } else { return NULL; } cur++; idx = TRIE_CHAR2INDEX(*cur); } while (*cur != '\0'); if (exact_match && cur_node->num_segments > 0 && seg_cnt < cur_node->num_segments) { return NULL; } return cur_node; } static void trie_node_release(struct trie *t, struct trie_node *node) { int i; int empty = QB_FALSE; if (node->key == NULL && node->parent != NULL && qb_list_empty(&node->notifier_head)) { struct trie_node *p = node->parent; if (node->num_children == 0) { empty = QB_TRUE; } else { empty = QB_TRUE; for (i = node->num_children - 1; i >= 0; i--) { if (node->children[i]) { empty = QB_FALSE; break; } } } if (!empty) { return; } /* * unlink the node from the parent */ p->children[node->idx] = NULL; free(node->segment); free(node->children); free(node); t->num_nodes--; t->mem_used -= sizeof(struct trie_node); trie_node_release(t, p); } } static void trie_node_destroy(struct trie *t, struct trie_node *n) { if (n->value == NULL) { return; } trie_notify(n, QB_MAP_NOTIFY_DELETED, n->key, n->value, NULL); n->key = NULL; n->value = NULL; trie_node_release(t, n); } static void trie_print_node(struct trie_node *n, struct trie_node *r, const char *suffix) { int i; if (n->parent) { trie_print_node(n->parent, n, suffix); } if (n->idx == 0) { return; } printf("[%c", TRIE_INDEX2CHAR(n->idx)); for (i = 0; i < n->num_segments; i++) { printf("%c", n->segment[i]); } if (n == r) { printf("] (%d) %s\n", n->refcount, suffix); } else { printf("] "); } } static void trie_node_ref(struct trie *t, struct trie_node *node) { if (t->header == node) { return; } node->refcount++; } static void trie_node_deref(struct trie *t, struct trie_node *node) { if (!trie_node_alive(node)) { return; } node->refcount--; if (node->refcount > 0) { return; } trie_node_destroy(t, node); } static void trie_destroy(struct qb_map *map) { struct trie *t = (struct trie *)map; struct trie_node *cur_node = t->header; struct trie_node *fwd_node; do { fwd_node = trie_node_next(cur_node, t->header, QB_FALSE); trie_node_destroy(t, cur_node); } while ((cur_node = fwd_node)); free(t); } static struct trie_node * trie_new_node(struct trie *t, struct trie_node *parent) { struct trie_node *new_node = calloc(1, sizeof(struct trie_node)); if (new_node == NULL) { return NULL; } new_node->parent = parent; new_node->num_children = 0; new_node->children = NULL; new_node->num_segments = 0; new_node->segment = NULL; t->num_nodes++; t->mem_used += sizeof(struct trie_node); qb_list_init(&new_node->notifier_head); return new_node; } void qb_trie_dump(qb_map_t* m) { struct trie * t = (struct trie*)m; struct trie_node *n; if (t == NULL) { return; } printf("nodes: %d, bytes: %d\n", t->num_nodes, t->mem_used); n = t->header; do { if (n->num_children == 0) { trie_print_node(n, n, " "); } n = trie_node_next(n, t->header, QB_FALSE); } while (n); } static void trie_put(struct qb_map *map, const char *key, const void *value) { struct trie *t = (struct trie *)map; struct trie_node *n = trie_insert(t, key); if (n) { const char *old_value = n->value; const char *old_key = n->key; n->key = (char *)key; n->value = (void *)value; if (old_value == NULL) { trie_node_ref(t, n); t->length++; trie_notify(n, QB_MAP_NOTIFY_INSERTED, n->key, NULL, n->value); } else { trie_notify(n, QB_MAP_NOTIFY_REPLACED, (char *)old_key, (void *)old_value, (void *)value); } } } static int32_t trie_rm(struct qb_map *map, const char *key) { struct trie *t = (struct trie *)map; struct trie_node *n = trie_lookup(t, key, QB_TRUE); if (n) { trie_node_deref(t, n); t->length--; return QB_TRUE; } else { return QB_FALSE; } } static void * trie_get(struct qb_map *map, const char *key) { struct trie *t = (struct trie *)map; struct trie_node *n = trie_lookup(t, key, QB_TRUE); if (n) { return n->value; } return NULL; } 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) { struct trie_node *c = n; struct qb_list_head *list; struct qb_list_head *next; struct qb_map_notifier *tn; do { qb_list_for_each_safe(list, next, &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) || (n == c))) { tn->callback(event, (char *)key, old_value, value, tn->user_data); } if (((event & QB_MAP_NOTIFY_DELETED) || (event & QB_MAP_NOTIFY_REPLACED)) && (tn->events & QB_MAP_NOTIFY_FREE)) { tn->callback(QB_MAP_NOTIFY_FREE, (char *)key, old_value, value, tn->user_data); } trie_notify_deref(tn); } c = c->parent; } while (c); } static int32_t trie_notify_add(qb_map_t * m, const char *key, qb_map_notify_fn fn, int32_t events, void *user_data) { struct trie *t = (struct trie *)m; struct qb_map_notifier *f; struct trie_node *n; struct qb_list_head *list; int add_to_tail = QB_FALSE; if (key) { n = trie_lookup(t, key, QB_TRUE); if (n == NULL) { n = trie_insert(t, key); } } else { n = t->header; } if (n) { qb_list_for_each(list, &n->notifier_head) { f = qb_list_entry(list, struct qb_map_notifier, list); if (events & QB_MAP_NOTIFY_FREE && f->events == events) { /* only one free notifier */ return -EEXIST; } if (f->events == events && f->callback == fn && f->user_data == user_data) { return -EEXIST; } } f = malloc(sizeof(struct qb_map_notifier)); if (f == NULL) { return -errno; } 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) { add_to_tail = QB_TRUE; } } else { if (events & QB_MAP_NOTIFY_FREE) { add_to_tail = QB_TRUE; } } if (add_to_tail) { qb_list_add_tail(&f->list, &n->notifier_head); } else { qb_list_add(&f->list, &n->notifier_head); } return 0; } return -EINVAL; } static int32_t trie_notify_del(qb_map_t * m, const char *key, qb_map_notify_fn fn, int32_t events, int32_t cmp_userdata, void *user_data) { struct trie *t = (struct trie *)m; - 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) { n = trie_lookup(t, key, QB_FALSE); } else { n = t->header; } if (n == NULL) { return -ENOENT; } qb_list_for_each_safe(list, next, &n->notifier_head) { - f = qb_list_entry(list, struct qb_map_notifier, list); - trie_notify_ref(f); + struct qb_map_notifier *f = qb_list_entry(list, struct qb_map_notifier, list); if (f->events == events && f->callback == fn) { if (cmp_userdata && (f->user_data == user_data)) { - trie_notify_deref(f); found = QB_TRUE; } else if (!cmp_userdata) { - trie_notify_deref(f); found = QB_TRUE; } } - trie_notify_deref(f); + + if (found) { + trie_notify_deref(f); + } } if (found) { trie_node_release(t, n); return 0; } else { return -ENOENT; } } static qb_map_iter_t * trie_iter_create(struct qb_map *map, const char *prefix) { struct trie_iter *i = malloc(sizeof(struct trie_iter)); struct trie *t = (struct trie *)map; if (i == NULL) { return NULL; } i->i.m = map; i->prefix = prefix; i->n = t->header; i->root = t->header; return (qb_map_iter_t *) i; } static const char * trie_iter_next(qb_map_iter_t * i, void **value) { struct trie_iter *si = (struct trie_iter *)i; struct trie_node *p = si->n; struct trie *t = (struct trie *)(i->m); if (p == NULL) { return NULL; } if (p->parent == NULL && si->prefix) { si->root = trie_lookup(t, si->prefix, QB_FALSE); if (si->root == NULL) { si->n = NULL; } else if (si->root->value == NULL) { si->n = trie_node_next(si->root, si->root, QB_FALSE); } else { si->n = si->root; } } else { si->n = trie_node_next(p, si->root, QB_FALSE); } if (si->n == NULL) { trie_node_deref(t, p); return NULL; } trie_node_ref(t, si->n); trie_node_deref(t, p); *value = si->n->value; return si->n->key; } static void trie_iter_free(qb_map_iter_t * i) { struct trie_iter *si = (struct trie_iter *)i; struct trie *t = (struct trie *)(i->m); if (si->n != NULL) { /* if free'ing the iterator before getting to the last * node make sure we de-ref the current node. */ trie_node_deref(t, si->n); } free(i); } static size_t trie_count_get(struct qb_map *map) { struct trie *list = (struct trie *)map; return list->length; } qb_map_t * qb_trie_create(void) { struct trie *t = malloc(sizeof(struct trie)); if (t == NULL) { return NULL; } t->map.put = trie_put; t->map.get = trie_get; t->map.rm = trie_rm; t->map.count_get = trie_count_get; t->map.iter_create = trie_iter_create; t->map.iter_next = trie_iter_next; t->map.iter_free = trie_iter_free; t->map.destroy = trie_destroy; t->map.notify_add = trie_notify_add; t->map.notify_del = trie_notify_del; t->length = 0; t->num_nodes = 0; t->mem_used = sizeof(struct trie); t->header = trie_new_node(t, NULL); return (qb_map_t *) t; }