|
|
bf3226 |
commit 342dfbeb8275f5ea6ed52dd3f30126614ec1d037
|
|
|
bf3226 |
Author: Ingo Franzki <ifranzki@linux.ibm.com>
|
|
|
bf3226 |
Date: Mon Feb 15 14:33:07 2021 +0100
|
|
|
bf3226 |
|
|
|
bf3226 |
Event support: pkcsslotd changes
|
|
|
bf3226 |
|
|
|
bf3226 |
Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
|
|
|
bf3226 |
|
|
|
bf3226 |
diff --git a/configure.ac b/configure.ac
|
|
|
bf3226 |
index e0ae4a82..a0b098e1 100644
|
|
|
bf3226 |
--- a/configure.ac
|
|
|
bf3226 |
+++ b/configure.ac
|
|
|
bf3226 |
@@ -234,6 +234,12 @@ AC_ARG_WITH([systemd],
|
|
|
bf3226 |
AS_HELP_STRING([--with-systemd@<:@=DIR@:>@],[systemd system unit files location]),
|
|
|
bf3226 |
[],
|
|
|
bf3226 |
[with_systemd=no])
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+dnl --- libudev development files
|
|
|
bf3226 |
+AC_ARG_WITH([libudev],
|
|
|
bf3226 |
+ AS_HELP_STRING([--with-libudev@<:@=DIR@:>@],[libudev development files location]),
|
|
|
bf3226 |
+ [],
|
|
|
bf3226 |
+ [with_libudev=check])
|
|
|
bf3226 |
|
|
|
bf3226 |
dnl ---
|
|
|
bf3226 |
dnl ---
|
|
|
bf3226 |
@@ -438,6 +444,46 @@ fi
|
|
|
bf3226 |
AC_SUBST([XCRYPTOLINZ_CFLAGS])
|
|
|
bf3226 |
AC_SUBST([XCRYPTOLINZ_LIBS])
|
|
|
bf3226 |
|
|
|
bf3226 |
+dnl --- with_libudev
|
|
|
bf3226 |
+LIBUDEV_CFLAGS=
|
|
|
bf3226 |
+LIBUDEV_LIBS=
|
|
|
bf3226 |
+if test "x$with_libudev" != "xno"; then
|
|
|
bf3226 |
+ if test "x$with_libudev" != "xyes" -a "x$with_libudev" != "xcheck"; then
|
|
|
bf3226 |
+ LIBUDEV_CFLAGS="-I$with_libudev"
|
|
|
bf3226 |
+ LIBUDEV_LIBS="-L$with_libudev"
|
|
|
bf3226 |
+ fi
|
|
|
bf3226 |
+ old_cflags="$CFLAGS"
|
|
|
bf3226 |
+ old_libs="$LIBS"
|
|
|
bf3226 |
+ CFLAGS="$CFLAGS $LIBUDEV_CFLAGS"
|
|
|
bf3226 |
+ LIBS="$LIBS $LIBUDEV_LIBS"
|
|
|
bf3226 |
+ # Use libudev only on s390 platforms, only s390 emits AP related uevents
|
|
|
bf3226 |
+ case $target in
|
|
|
bf3226 |
+ *s390x* | *s390*)
|
|
|
bf3226 |
+ CFLAGS="$CFLAGS -DWITH_LIBUDEV"
|
|
|
bf3226 |
+ ;;
|
|
|
bf3226 |
+ *)
|
|
|
bf3226 |
+ if test "x$with_libudev" != "xyes"; then
|
|
|
bf3226 |
+ with_libudev=no
|
|
|
bf3226 |
+ echo "Default to 'with_libudev=no' on non-s390 platforms"
|
|
|
bf3226 |
+ fi
|
|
|
bf3226 |
+ ;;
|
|
|
bf3226 |
+ esac
|
|
|
bf3226 |
+ if test "x$with_libudev" != "xno"; then
|
|
|
bf3226 |
+ AC_CHECK_HEADER([libudev.h], [with_libudev=yes], [
|
|
|
bf3226 |
+ AC_MSG_ERROR([Build with libudev requested but libudev headers couldn't be found])
|
|
|
bf3226 |
+ ])
|
|
|
bf3226 |
+ AC_CHECK_LIB([udev], [udev_monitor_new_from_netlink], [with_libudev=yes], [
|
|
|
bf3226 |
+ AC_MSG_ERROR([Build with libudev requested but libudev libraries couldn't be found])
|
|
|
bf3226 |
+ ])
|
|
|
bf3226 |
+ fi
|
|
|
bf3226 |
+ if test "x$with_libudev" = "xno"; then
|
|
|
bf3226 |
+ CFLAGS="$old_cflags"
|
|
|
bf3226 |
+ LIBS="$old_libs"
|
|
|
bf3226 |
+ fi
|
|
|
bf3226 |
+fi
|
|
|
bf3226 |
+AC_SUBST([LIBUDEV_CFLAGS])
|
|
|
bf3226 |
+AC_SUBST([LIBUDEV_LIBS])
|
|
|
bf3226 |
+AM_CONDITIONAL([HAVE_LIBUDEV], [test "x$with_libudev" = "xyes"])
|
|
|
bf3226 |
|
|
|
bf3226 |
dnl ---
|
|
|
bf3226 |
dnl --- Now check enabled features, while making sure every required
|
|
|
bf3226 |
@@ -649,6 +695,7 @@ echo " Daemon build: $enable_daemon"
|
|
|
bf3226 |
echo " Library build: $enable_library"
|
|
|
bf3226 |
echo " Systemd service: $enable_systemd"
|
|
|
bf3226 |
echo " Build with locks: $enable_locks"
|
|
|
bf3226 |
+echo " Build with libudev: $with_libudev"
|
|
|
bf3226 |
echo " Build p11sak tool: $enable_p11sak"
|
|
|
bf3226 |
echo " token migrate tool: $enable_pkcstok_migrate"
|
|
|
bf3226 |
echo
|
|
|
bf3226 |
diff --git a/usr/include/slotmgr.h b/usr/include/slotmgr.h
|
|
|
bf3226 |
index 4d038435..e37368a5 100644
|
|
|
bf3226 |
--- a/usr/include/slotmgr.h
|
|
|
bf3226 |
+++ b/usr/include/slotmgr.h
|
|
|
bf3226 |
@@ -31,6 +31,7 @@
|
|
|
bf3226 |
#define OCK_API_LOCK_FILE LOCKDIR_PATH "/LCK..APIlock"
|
|
|
bf3226 |
|
|
|
bf3226 |
#define PROC_SOCKET_FILE_PATH "/var/run/pkcsslotd.socket"
|
|
|
bf3226 |
+#define ADMIN_SOCKET_FILE_PATH "/var/run/pkcsslotd.admin.socket"
|
|
|
bf3226 |
|
|
|
bf3226 |
#define PID_FILE_PATH "/var/run/pkcsslotd.pid"
|
|
|
bf3226 |
#define OCK_CONFIG OCK_CONFDIR "/opencryptoki.conf"
|
|
|
bf3226 |
@@ -45,6 +46,7 @@
|
|
|
bf3226 |
|
|
|
bf3226 |
#define NUMBER_SLOTS_MANAGED 1024
|
|
|
bf3226 |
#define NUMBER_PROCESSES_ALLOWED 1000
|
|
|
bf3226 |
+#define NUMBER_ADMINS_ALLOWED 1000
|
|
|
bf3226 |
|
|
|
bf3226 |
//
|
|
|
bf3226 |
// Per Process Data structure
|
|
|
bf3226 |
diff --git a/usr/sbin/pkcsslotd/pkcsslotd.h b/usr/sbin/pkcsslotd/pkcsslotd.h
|
|
|
bf3226 |
index 69eb59f3..d7edcb3c 100644
|
|
|
bf3226 |
--- a/usr/sbin/pkcsslotd/pkcsslotd.h
|
|
|
bf3226 |
+++ b/usr/sbin/pkcsslotd/pkcsslotd.h
|
|
|
bf3226 |
@@ -92,5 +92,8 @@ int init_socket_server();
|
|
|
bf3226 |
int term_socket_server();
|
|
|
bf3226 |
int init_socket_data(Slot_Mgr_Socket_t *sp);
|
|
|
bf3226 |
int socket_connection_handler(int timeout_secs);
|
|
|
bf3226 |
+#ifdef DEV
|
|
|
bf3226 |
+void dump_socket_handler();
|
|
|
bf3226 |
+#endif
|
|
|
bf3226 |
|
|
|
bf3226 |
#endif /* _SLOTMGR_H */
|
|
|
bf3226 |
diff --git a/usr/sbin/pkcsslotd/pkcsslotd.mk b/usr/sbin/pkcsslotd/pkcsslotd.mk
|
|
|
bf3226 |
index 2d36b4a9..c574edf8 100644
|
|
|
bf3226 |
--- a/usr/sbin/pkcsslotd/pkcsslotd.mk
|
|
|
bf3226 |
+++ b/usr/sbin/pkcsslotd/pkcsslotd.mk
|
|
|
bf3226 |
@@ -8,6 +8,9 @@ CLEANFILES += usr/lib/common/parser.c usr/lib/common/parser.h \
|
|
|
bf3226 |
usr/lib/common/parser.output usr/lib/common/lexer.c
|
|
|
bf3226 |
|
|
|
bf3226 |
usr_sbin_pkcsslotd_pkcsslotd_LDFLAGS = -lpthread -lcrypto
|
|
|
bf3226 |
+if HAVE_LIBUDEV
|
|
|
bf3226 |
+usr_sbin_pkcsslotd_pkcsslotd_LDFLAGS += -ludev
|
|
|
bf3226 |
+endif
|
|
|
bf3226 |
|
|
|
bf3226 |
usr_sbin_pkcsslotd_pkcsslotd_CFLAGS = -DPROGRAM_NAME=\"$(@)\" \
|
|
|
bf3226 |
-I${srcdir}/usr/include -I${srcdir}/usr/lib/common \
|
|
|
bf3226 |
diff --git a/usr/sbin/pkcsslotd/signal.c b/usr/sbin/pkcsslotd/signal.c
|
|
|
bf3226 |
index 49482a2f..17167632 100644
|
|
|
bf3226 |
--- a/usr/sbin/pkcsslotd/signal.c
|
|
|
bf3226 |
+++ b/usr/sbin/pkcsslotd/signal.c
|
|
|
bf3226 |
@@ -21,7 +21,7 @@
|
|
|
bf3226 |
extern BOOL IsValidProcessEntry(pid_t_64 pid, time_t_64 RegTime);
|
|
|
bf3226 |
|
|
|
bf3226 |
static int SigsToIntercept[] = {
|
|
|
bf3226 |
- SIGHUP, SIGINT, SIGQUIT, SIGPIPE, SIGALRM,
|
|
|
bf3226 |
+ SIGHUP, SIGINT, SIGQUIT, SIGALRM,
|
|
|
bf3226 |
SIGTERM, SIGTSTP, SIGTTIN,
|
|
|
bf3226 |
SIGTTOU, SIGUSR1, SIGUSR2, SIGPROF
|
|
|
bf3226 |
};
|
|
|
bf3226 |
@@ -32,8 +32,11 @@ static int SigsToIntercept[] = {
|
|
|
bf3226 |
/* SIGCHLD - Don't want to exit. Should never receive, but we do, apparently
|
|
|
bf3226 |
* when something tries to cancel the GC Thread */
|
|
|
bf3226 |
|
|
|
bf3226 |
+/* SIGPIPE - Don't want to exit. May happen when a connection to an admin
|
|
|
bf3226 |
+ * event sender or a process is closed before all events are delivered. */
|
|
|
bf3226 |
+
|
|
|
bf3226 |
static int SigsToIgnore[] = {
|
|
|
bf3226 |
- SIGCHLD,
|
|
|
bf3226 |
+ SIGCHLD, SIGPIPE,
|
|
|
bf3226 |
};
|
|
|
bf3226 |
|
|
|
bf3226 |
|
|
|
bf3226 |
@@ -71,6 +74,10 @@ void slotdGenericSignalHandler(int Signal)
|
|
|
bf3226 |
CheckForGarbage(shmp);
|
|
|
bf3226 |
#endif
|
|
|
bf3226 |
|
|
|
bf3226 |
+#ifdef DEV
|
|
|
bf3226 |
+ dump_socket_handler();
|
|
|
bf3226 |
+#endif
|
|
|
bf3226 |
+
|
|
|
bf3226 |
for (procindex = 0; (procindex < NUMBER_PROCESSES_ALLOWED); procindex++) {
|
|
|
bf3226 |
|
|
|
bf3226 |
Slot_Mgr_Proc_t_64 *pProc = &(shmp->proc_table[procindex]);
|
|
|
bf3226 |
diff --git a/usr/sbin/pkcsslotd/socket_server.c b/usr/sbin/pkcsslotd/socket_server.c
|
|
|
bf3226 |
index 1fae0b95..41408670 100644
|
|
|
bf3226 |
--- a/usr/sbin/pkcsslotd/socket_server.c
|
|
|
bf3226 |
+++ b/usr/sbin/pkcsslotd/socket_server.c
|
|
|
bf3226 |
@@ -1,4 +1,6 @@
|
|
|
bf3226 |
/*
|
|
|
bf3226 |
+ * COPYRIGHT (c) International Business Machines Corp. 2013, 2021
|
|
|
bf3226 |
+ *
|
|
|
bf3226 |
* This program is provided under the terms of the Common Public License,
|
|
|
bf3226 |
* version 1.0 (CPL-1.0). Any use, reproduction or distribution for this
|
|
|
bf3226 |
* software constitutes recipient's acceptance of CPL-1.0 terms which can be
|
|
|
bf3226 |
@@ -12,6 +14,8 @@
|
|
|
bf3226 |
#include <stdio.h>
|
|
|
bf3226 |
#include <unistd.h>
|
|
|
bf3226 |
#include <string.h>
|
|
|
bf3226 |
+#include <stdlib.h>
|
|
|
bf3226 |
+#include <fcntl.h>
|
|
|
bf3226 |
|
|
|
bf3226 |
#include <sys/time.h>
|
|
|
bf3226 |
#include <sys/socket.h>
|
|
|
bf3226 |
@@ -19,32 +23,1225 @@
|
|
|
bf3226 |
#include <sys/select.h>
|
|
|
bf3226 |
#include <sys/stat.h>
|
|
|
bf3226 |
#include <grp.h>
|
|
|
bf3226 |
+#include <sys/epoll.h>
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+#if defined(__GNUC__) && __GNUC__ >= 7 || defined(__clang__) && __clang_major__ >= 12
|
|
|
bf3226 |
+ #define FALL_THROUGH __attribute__ ((fallthrough))
|
|
|
bf3226 |
+#else
|
|
|
bf3226 |
+ #define FALL_THROUGH ((void)0)
|
|
|
bf3226 |
+#endif
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+#ifdef WITH_LIBUDEV
|
|
|
bf3226 |
+#include <libudev.h>
|
|
|
bf3226 |
+#endif
|
|
|
bf3226 |
|
|
|
bf3226 |
#include "log.h"
|
|
|
bf3226 |
#include "slotmgr.h"
|
|
|
bf3226 |
#include "pkcsslotd.h"
|
|
|
bf3226 |
#include "apictl.h"
|
|
|
bf3226 |
+#include "dlist.h"
|
|
|
bf3226 |
+#include "events.h"
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+#define MAX_EPOLL_EVENTS 128
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+#ifdef WITH_LIBUDEV
|
|
|
bf3226 |
+#define UDEV_RECV_BUFFFER_SIZE 512 * 1024
|
|
|
bf3226 |
+#define UDEV_SUBSYSTEM_AP "ap"
|
|
|
bf3226 |
+#define UDEV_ACTION_BIND "bind"
|
|
|
bf3226 |
+#define UDEV_ACTION_UNBIND "unbind"
|
|
|
bf3226 |
+#define UDEV_ACTION_DEVTYPE_APQN "ap_queue"
|
|
|
bf3226 |
+#define UDEV_PROERTY_DEVTYPE "DEV_TYPE"
|
|
|
bf3226 |
+#endif
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+struct epoll_info {
|
|
|
bf3226 |
+ int (* notify)(int events, void *private);
|
|
|
bf3226 |
+ void (* free)(void *private);
|
|
|
bf3226 |
+ void *private;
|
|
|
bf3226 |
+ unsigned long ref_count;
|
|
|
bf3226 |
+};
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+struct listener_info {
|
|
|
bf3226 |
+ int socket;
|
|
|
bf3226 |
+ const char *file_path;
|
|
|
bf3226 |
+ int (* new_conn)(int socket, struct listener_info *listener);
|
|
|
bf3226 |
+ struct epoll_info ep_info;
|
|
|
bf3226 |
+ unsigned long num_clients;
|
|
|
bf3226 |
+ unsigned long max_num_clients;
|
|
|
bf3226 |
+};
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+enum xfer_state {
|
|
|
bf3226 |
+ XFER_IDLE = 0,
|
|
|
bf3226 |
+ XFER_RECEIVE = 1,
|
|
|
bf3226 |
+ XFER_SEND = 2,
|
|
|
bf3226 |
+};
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+struct client_info {
|
|
|
bf3226 |
+ int socket;
|
|
|
bf3226 |
+ int (* xfer_complete)(void *client);
|
|
|
bf3226 |
+ void (* hangup)(void *client);
|
|
|
bf3226 |
+ void (* free)(void *client);
|
|
|
bf3226 |
+ void *client;
|
|
|
bf3226 |
+ struct epoll_info ep_info;
|
|
|
bf3226 |
+ enum xfer_state xfer_state;
|
|
|
bf3226 |
+ char *xfer_buffer;
|
|
|
bf3226 |
+ size_t xfer_size;
|
|
|
bf3226 |
+ size_t xfer_offset;
|
|
|
bf3226 |
+};
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+enum proc_state {
|
|
|
bf3226 |
+ PROC_INITIAL_SEND = 0,
|
|
|
bf3226 |
+ PROC_WAIT_FOR_EVENT = 1,
|
|
|
bf3226 |
+ PROC_SEND_EVENT = 2,
|
|
|
bf3226 |
+ PROC_SEND_PAYLOAD = 3,
|
|
|
bf3226 |
+ PROC_RECEIVE_REPLY = 4,
|
|
|
bf3226 |
+ PROC_HANGUP = 5,
|
|
|
bf3226 |
+};
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+struct proc_conn_info {
|
|
|
bf3226 |
+ struct client_info client_info;
|
|
|
bf3226 |
+ enum proc_state state;
|
|
|
bf3226 |
+ DL_NODE *events;
|
|
|
bf3226 |
+ struct event_info *event;
|
|
|
bf3226 |
+ event_reply_t reply;
|
|
|
bf3226 |
+};
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+enum admin_state {
|
|
|
bf3226 |
+ ADMIN_RECEIVE_EVENT = 0,
|
|
|
bf3226 |
+ ADMIN_RECEIVE_PAYLOAD = 1,
|
|
|
bf3226 |
+ ADMIN_EVENT_DELIVERED = 2,
|
|
|
bf3226 |
+ ADMIN_SEND_REPLY = 3,
|
|
|
bf3226 |
+ ADMIN_WAIT_FOR_EVENT_LIMIT = 4,
|
|
|
bf3226 |
+ ADMIN_HANGUP = 5,
|
|
|
bf3226 |
+};
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+struct admin_conn_info {
|
|
|
bf3226 |
+ struct client_info client_info;
|
|
|
bf3226 |
+ enum admin_state state;
|
|
|
bf3226 |
+ struct event_info *event;
|
|
|
bf3226 |
+};
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+#ifdef WITH_LIBUDEV
|
|
|
bf3226 |
+struct udev_mon {
|
|
|
bf3226 |
+ struct udev *udev;
|
|
|
bf3226 |
+ struct udev_monitor *mon;
|
|
|
bf3226 |
+ int socket;
|
|
|
bf3226 |
+ struct epoll_info ep_info;
|
|
|
bf3226 |
+ struct event_info *delayed_event;
|
|
|
bf3226 |
+};
|
|
|
bf3226 |
+#endif
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+struct event_info {
|
|
|
bf3226 |
+ event_msg_t event;
|
|
|
bf3226 |
+ char *payload;
|
|
|
bf3226 |
+ event_reply_t reply;
|
|
|
bf3226 |
+ unsigned long proc_ref_count; /* # of processes using this event */
|
|
|
bf3226 |
+ struct admin_conn_info *admin_ref; /* Admin connection to send reply back */
|
|
|
bf3226 |
+};
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+static int epoll_fd = -1;
|
|
|
bf3226 |
+static struct listener_info proc_listener;
|
|
|
bf3226 |
+static DL_NODE *proc_connections = NULL;
|
|
|
bf3226 |
+static struct listener_info admin_listener;
|
|
|
bf3226 |
+static DL_NODE *admin_connections = NULL;
|
|
|
bf3226 |
+#ifdef WITH_LIBUDEV
|
|
|
bf3226 |
+static struct udev_mon udev_mon;
|
|
|
bf3226 |
+#endif
|
|
|
bf3226 |
+static DL_NODE *pending_events = NULL;
|
|
|
bf3226 |
+static unsigned long pending_events_count = 0;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+#define MAX_PENDING_EVENTS 1024
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+/*
|
|
|
bf3226 |
+ * Iterate over all connections in a safe way. Before actually iterating,
|
|
|
bf3226 |
+ * increment the ref count of ALL connections, because any processing may
|
|
|
bf3226 |
+ * cause any of the connections to be hang-up, and thus freed and removed
|
|
|
bf3226 |
+ * from the list. We need to make sure that while we are iterating over the
|
|
|
bf3226 |
+ * connections, none of them gets removed from the list.
|
|
|
bf3226 |
+ */
|
|
|
bf3226 |
+#define FOR_EACH_CONN_SAFE_BEGIN(list, conn) { \
|
|
|
bf3226 |
+ DL_NODE *_node, *_next; \
|
|
|
bf3226 |
+ _node = dlist_get_first(list); \
|
|
|
bf3226 |
+ while (_node != NULL) { \
|
|
|
bf3226 |
+ conn = _node->data; \
|
|
|
bf3226 |
+ _next = dlist_next(_node); \
|
|
|
bf3226 |
+ client_socket_get(&(conn)->client_info); \
|
|
|
bf3226 |
+ _node = _next; \
|
|
|
bf3226 |
+ } \
|
|
|
bf3226 |
+ _node = dlist_get_first(list); \
|
|
|
bf3226 |
+ while (_node != NULL) { \
|
|
|
bf3226 |
+ conn = _node->data; \
|
|
|
bf3226 |
+ _next = dlist_next(_node);
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+#define FOR_EACH_CONN_SAFE_END(list, conn) \
|
|
|
bf3226 |
+ _node = _next; \
|
|
|
bf3226 |
+ } \
|
|
|
bf3226 |
+ _node = dlist_get_first(list); \
|
|
|
bf3226 |
+ while (_node != NULL) { \
|
|
|
bf3226 |
+ conn = _node->data; \
|
|
|
bf3226 |
+ _next = dlist_next(_node); \
|
|
|
bf3226 |
+ client_socket_put(&(conn)->client_info); \
|
|
|
bf3226 |
+ _node = _next; \
|
|
|
bf3226 |
+ } \
|
|
|
bf3226 |
+ }
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+static void listener_socket_close(int socketfd, const char *file_path);
|
|
|
bf3226 |
+static int listener_client_hangup(struct listener_info *listener);
|
|
|
bf3226 |
+static void event_delivered(struct event_info *event);
|
|
|
bf3226 |
+static int client_socket_notify(int events, void *private);
|
|
|
bf3226 |
+static void client_socket_free(void *private);
|
|
|
bf3226 |
+static int proc_xfer_complete(void *client);
|
|
|
bf3226 |
+static int proc_start_deliver_event(struct proc_conn_info *conn);
|
|
|
bf3226 |
+static int proc_deliver_event(struct proc_conn_info *conn,
|
|
|
bf3226 |
+ struct event_info *event);
|
|
|
bf3226 |
+static int proc_event_delivered(struct proc_conn_info *conn,
|
|
|
bf3226 |
+ struct event_info *event);
|
|
|
bf3226 |
+static inline void proc_get(struct proc_conn_info *conn);
|
|
|
bf3226 |
+static inline void proc_put(struct proc_conn_info *conn);
|
|
|
bf3226 |
+static void proc_hangup(void *client);
|
|
|
bf3226 |
+static void proc_free(void *client);
|
|
|
bf3226 |
+static int admin_xfer_complete(void *client);
|
|
|
bf3226 |
+static void admin_event_limit_underrun(struct admin_conn_info *conn);
|
|
|
bf3226 |
+static int admin_event_delivered(struct admin_conn_info *conn,
|
|
|
bf3226 |
+ struct event_info *event);
|
|
|
bf3226 |
+static inline void admin_get(struct admin_conn_info *conn);
|
|
|
bf3226 |
+static inline void admin_put(struct admin_conn_info *conn);
|
|
|
bf3226 |
+static void admin_hangup(void *client);
|
|
|
bf3226 |
+static void admin_free(void *client);
|
|
|
bf3226 |
+#ifdef WITH_LIBUDEV
|
|
|
bf3226 |
+static void udev_mon_term(struct udev_mon *udev_mon);
|
|
|
bf3226 |
+static int udev_mon_notify(int events, void *private);
|
|
|
bf3226 |
+#endif
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+static void epoll_info_init(struct epoll_info *epoll_info,
|
|
|
bf3226 |
+ int (* notify)(int events, void *private),
|
|
|
bf3226 |
+ void (* free_cb)(void *private),
|
|
|
bf3226 |
+ void *private)
|
|
|
bf3226 |
+{
|
|
|
bf3226 |
+ epoll_info->ref_count = 1;
|
|
|
bf3226 |
+ epoll_info->notify = notify;
|
|
|
bf3226 |
+ epoll_info->free = free_cb;
|
|
|
bf3226 |
+ epoll_info->private = private;
|
|
|
bf3226 |
+}
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+static void epoll_info_get(struct epoll_info *epoll_info)
|
|
|
bf3226 |
+{
|
|
|
bf3226 |
+ epoll_info->ref_count++;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ DbgLog(DL3, "%s: private: %p, ref_count: %lu", __func__,
|
|
|
bf3226 |
+ epoll_info->private, epoll_info->ref_count);
|
|
|
bf3226 |
+}
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+static void epoll_info_put(struct epoll_info *epoll_info)
|
|
|
bf3226 |
+{
|
|
|
bf3226 |
+ if (epoll_info->ref_count > 0)
|
|
|
bf3226 |
+ epoll_info->ref_count--;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ DbgLog(DL3, "%s: private: %p, ref_count: %lu", __func__,
|
|
|
bf3226 |
+ epoll_info->private, epoll_info->ref_count);
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ if (epoll_info->ref_count == 0 && epoll_info->free != NULL)
|
|
|
bf3226 |
+ epoll_info->free(epoll_info->private);
|
|
|
bf3226 |
+}
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+static int client_socket_init(int socket, int (* xfer_complete)(void *client),
|
|
|
bf3226 |
+ void (* hangup)(void *client),
|
|
|
bf3226 |
+ void (* free_cb)(void *client), void *client,
|
|
|
bf3226 |
+ struct client_info *client_info)
|
|
|
bf3226 |
+{
|
|
|
bf3226 |
+ struct epoll_event evt;
|
|
|
bf3226 |
+ int rc, err;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ if (xfer_complete == NULL || hangup == NULL)
|
|
|
bf3226 |
+ return -EINVAL;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ epoll_info_init(&client_info->ep_info, client_socket_notify,
|
|
|
bf3226 |
+ client_socket_free, client_info);
|
|
|
bf3226 |
+ client_info->socket = socket;
|
|
|
bf3226 |
+ client_info->xfer_complete = xfer_complete;
|
|
|
bf3226 |
+ client_info->hangup = hangup;
|
|
|
bf3226 |
+ client_info->free = free_cb;
|
|
|
bf3226 |
+ client_info->client = client;
|
|
|
bf3226 |
+ client_info->xfer_state = XFER_IDLE;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ rc = fcntl(socket, F_SETFL, O_NONBLOCK);
|
|
|
bf3226 |
+ if (rc < 0) {
|
|
|
bf3226 |
+ err = errno;
|
|
|
bf3226 |
+ InfoLog("%s: Failed to set client socket %d to non-blocking, errno "
|
|
|
bf3226 |
+ "%d (%s).", __func__, socket, err, strerror(err));
|
|
|
bf3226 |
+ return -err;
|
|
|
bf3226 |
+ }
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ evt.events = EPOLLIN | EPOLLOUT | EPOLLRDHUP | EPOLLERR | EPOLLET;
|
|
|
bf3226 |
+ evt.data.ptr = &client_info->ep_info;
|
|
|
bf3226 |
+ rc = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, socket, &evt);
|
|
|
bf3226 |
+ if (rc != 0) {
|
|
|
bf3226 |
+ err = errno;
|
|
|
bf3226 |
+ InfoLog("%s: Failed to add client socket %d to epoll, errno %d (%s).",
|
|
|
bf3226 |
+ __func__, socket, err, strerror(err));
|
|
|
bf3226 |
+ close(socket);
|
|
|
bf3226 |
+ return -err;
|
|
|
bf3226 |
+ }
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ return 0;
|
|
|
bf3226 |
+}
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+static inline void client_socket_get(struct client_info *client_info)
|
|
|
bf3226 |
+{
|
|
|
bf3226 |
+ epoll_info_get(&client_info->ep_info);
|
|
|
bf3226 |
+}
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+static inline void client_socket_put(struct client_info *client_info)
|
|
|
bf3226 |
+{
|
|
|
bf3226 |
+ epoll_info_put(&client_info->ep_info);
|
|
|
bf3226 |
+}
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+static void client_socket_term(struct client_info *client_info)
|
|
|
bf3226 |
+{
|
|
|
bf3226 |
+ epoll_ctl(epoll_fd, EPOLL_CTL_DEL, client_info->socket, NULL);
|
|
|
bf3226 |
+ close(client_info->socket);
|
|
|
bf3226 |
+ client_info->socket = -1;
|
|
|
bf3226 |
+}
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+static int client_socket_notify(int events, void *private)
|
|
|
bf3226 |
+{
|
|
|
bf3226 |
+ struct client_info *client_info = private;
|
|
|
bf3226 |
+ ssize_t num;
|
|
|
bf3226 |
+ int rc, err, socket = client_info->socket;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ DbgLog(DL3, "%s: Epoll event on client %p socket %d: events: 0x%x xfer: %d",
|
|
|
bf3226 |
+ __func__, client_info, socket, events, client_info->xfer_state);
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ if (socket < 0)
|
|
|
bf3226 |
+ return -ENOTCONN;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ if (events & (EPOLLHUP | EPOLLERR)) {
|
|
|
bf3226 |
+ DbgLog(DL3, "EPOLLHUP | EPOLLERR");
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ client_info->hangup(client_info->client);
|
|
|
bf3226 |
+ client_info = NULL; /* client_info may have been freed by now */
|
|
|
bf3226 |
+ return 0;
|
|
|
bf3226 |
+ }
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ if (client_info->xfer_state == XFER_RECEIVE && (events & EPOLLIN)) {
|
|
|
bf3226 |
+ DbgLog(DL3, "%s: EPOLLIN: buffer: %p size: %lu ofs: %lu", __func__,
|
|
|
bf3226 |
+ client_info->xfer_buffer, client_info->xfer_size,
|
|
|
bf3226 |
+ client_info->xfer_offset);
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ num = read(client_info->socket,
|
|
|
bf3226 |
+ client_info->xfer_buffer + client_info->xfer_offset,
|
|
|
bf3226 |
+ client_info->xfer_size - client_info->xfer_offset);
|
|
|
bf3226 |
+ if (num <= 0) {
|
|
|
bf3226 |
+ err = errno;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ DbgLog(DL3, "%s: read failed with: num: %d errno: %d (%s)",
|
|
|
bf3226 |
+ __func__, num, num < 0 ? err : 0,
|
|
|
bf3226 |
+ num < 0 ? strerror(err) : "none");
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ if (num < 0 && err == EWOULDBLOCK)
|
|
|
bf3226 |
+ return 0; /* Will be continued when socket becomes readable */
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ /* assume connection closed by peer */
|
|
|
bf3226 |
+ client_info->hangup(client_info->client);
|
|
|
bf3226 |
+ client_info = NULL; /* client_info may have been freed by now */
|
|
|
bf3226 |
+ return 0;
|
|
|
bf3226 |
+ } else {
|
|
|
bf3226 |
+ DbgLog(DL3, "%s: %lu bytes received", __func__, num);
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ client_info->xfer_offset += num;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ DbgLog(DL3, "%s: %lu bytes left", __func__,
|
|
|
bf3226 |
+ client_info->xfer_size - client_info->xfer_offset);
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ if (client_info->xfer_offset >= client_info->xfer_size) {
|
|
|
bf3226 |
+ client_info->xfer_state = XFER_IDLE;
|
|
|
bf3226 |
+ client_info->xfer_buffer = NULL;
|
|
|
bf3226 |
+ client_info->xfer_size = 0;
|
|
|
bf3226 |
+ client_info->xfer_offset = 0;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ client_socket_get(client_info);
|
|
|
bf3226 |
+ rc = client_info->xfer_complete(client_info->client);
|
|
|
bf3226 |
+ if (rc != 0) {
|
|
|
bf3226 |
+ InfoLog("%s: xfer_complete callback failed for client "
|
|
|
bf3226 |
+ "socket %d, rc: %d", __func__, socket,
|
|
|
bf3226 |
+ rc);
|
|
|
bf3226 |
+ client_info->hangup(client_info->client);
|
|
|
bf3226 |
+ }
|
|
|
bf3226 |
+ client_socket_put(client_info);
|
|
|
bf3226 |
+ client_info = NULL; /* client_info may have been freed by now */
|
|
|
bf3226 |
+ return rc;
|
|
|
bf3226 |
+ }
|
|
|
bf3226 |
+ return 0;
|
|
|
bf3226 |
+ }
|
|
|
bf3226 |
+ }
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ if (client_info->xfer_state == XFER_SEND && (events & EPOLLOUT)) {
|
|
|
bf3226 |
+ DbgLog(DL3, "%s: EPOLLOUT: buffer: %p size: %lu ofs: %lu", __func__,
|
|
|
bf3226 |
+ client_info->xfer_buffer, client_info->xfer_size,
|
|
|
bf3226 |
+ client_info->xfer_offset);
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ num = write(client_info->socket,
|
|
|
bf3226 |
+ client_info->xfer_buffer + client_info->xfer_offset,
|
|
|
bf3226 |
+ client_info->xfer_size - client_info->xfer_offset);
|
|
|
bf3226 |
+ if (num < 0) {
|
|
|
bf3226 |
+ err = errno;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ DbgLog(DL3, "%s: write failed with: errno: %d (%s)", __func__, err,
|
|
|
bf3226 |
+ strerror(err));
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ if (err == EWOULDBLOCK)
|
|
|
bf3226 |
+ return 0; /* Will be continued when socket becomes writable */
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ /* assume connection closed by peer */
|
|
|
bf3226 |
+ client_info->hangup(client_info->client);
|
|
|
bf3226 |
+ client_info = NULL; /* client_info may have been freed by now */
|
|
|
bf3226 |
+ return 0;
|
|
|
bf3226 |
+ } else {
|
|
|
bf3226 |
+ DbgLog(DL3, "%s: %lu bytes sent", __func__, num);
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ client_info->xfer_offset += num;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ DbgLog(DL3, "%s: %lu bytes left", __func__,
|
|
|
bf3226 |
+ client_info->xfer_size - client_info->xfer_offset);
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ if (client_info->xfer_offset >= client_info->xfer_size) {
|
|
|
bf3226 |
+ client_info->xfer_state = XFER_IDLE;
|
|
|
bf3226 |
+ client_info->xfer_buffer = NULL;
|
|
|
bf3226 |
+ client_info->xfer_size = 0;
|
|
|
bf3226 |
+ client_info->xfer_offset = 0;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ client_socket_get(client_info);
|
|
|
bf3226 |
+ rc = client_info->xfer_complete(client_info->client);
|
|
|
bf3226 |
+ if (rc != 0) {
|
|
|
bf3226 |
+ InfoLog("%s: xfer_complete callback failed for client "
|
|
|
bf3226 |
+ "socket %d, rc: %d", __func__, socket,
|
|
|
bf3226 |
+ rc);
|
|
|
bf3226 |
+ client_info->hangup(client_info->client);
|
|
|
bf3226 |
+ }
|
|
|
bf3226 |
+ client_socket_put(client_info);
|
|
|
bf3226 |
+ client_info = NULL; /* client_info may have been freed by now */
|
|
|
bf3226 |
+ return rc;
|
|
|
bf3226 |
+ }
|
|
|
bf3226 |
+ return 0;
|
|
|
bf3226 |
+ }
|
|
|
bf3226 |
+ }
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ return 0;
|
|
|
bf3226 |
+}
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+static void client_socket_free(void *private)
|
|
|
bf3226 |
+{
|
|
|
bf3226 |
+ struct client_info *client_info = private;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ DbgLog(DL3, "%s: %p", __func__, client_info);
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ if (client_info->free != NULL)
|
|
|
bf3226 |
+ client_info->free(client_info->client);
|
|
|
bf3226 |
+}
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+static int client_socket_receive(struct client_info *client_info,
|
|
|
bf3226 |
+ void *buffer, size_t size)
|
|
|
bf3226 |
+{
|
|
|
bf3226 |
+ if (client_info->socket < 0)
|
|
|
bf3226 |
+ return -ENOTCONN;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ client_info->xfer_state = XFER_RECEIVE;
|
|
|
bf3226 |
+ client_info->xfer_buffer = (char *)buffer;
|
|
|
bf3226 |
+ client_info->xfer_size = size;
|
|
|
bf3226 |
+ client_info->xfer_offset = 0;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ DbgLog(DL3, "%s: Start receive on client socket %d: buffer: %p size: %lu",
|
|
|
bf3226 |
+ __func__, client_info->socket, buffer, size);
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ return client_socket_notify(EPOLLIN, client_info);
|
|
|
bf3226 |
+}
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+static int client_socket_send(struct client_info *client_info,
|
|
|
bf3226 |
+ void *buffer, size_t size)
|
|
|
bf3226 |
+{
|
|
|
bf3226 |
+ if (client_info->socket < 0)
|
|
|
bf3226 |
+ return -ENOTCONN;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ client_info->xfer_state = XFER_SEND;
|
|
|
bf3226 |
+ client_info->xfer_buffer = (char *)buffer;
|
|
|
bf3226 |
+ client_info->xfer_size = size;
|
|
|
bf3226 |
+ client_info->xfer_offset = 0;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ DbgLog(DL3, "%s: Start send on client socket %d: buffer: %p size: %lu",
|
|
|
bf3226 |
+ __func__, client_info->socket, buffer, size);
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ return client_socket_notify(EPOLLOUT, client_info);
|
|
|
bf3226 |
+}
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+static struct event_info *event_new(unsigned int payload_len,
|
|
|
bf3226 |
+ struct admin_conn_info *admin_conn)
|
|
|
bf3226 |
+{
|
|
|
bf3226 |
+ struct event_info *event;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ event = calloc(1, sizeof(struct event_info));
|
|
|
bf3226 |
+ if (event == NULL) {
|
|
|
bf3226 |
+ ErrLog("%s: Failed to allocate the event", __func__);
|
|
|
bf3226 |
+ return NULL;
|
|
|
bf3226 |
+ }
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ event->event.version = EVENT_VERSION_1;
|
|
|
bf3226 |
+ event->event.payload_len = payload_len;
|
|
|
bf3226 |
+ if (payload_len > 0) {
|
|
|
bf3226 |
+ event->payload = malloc(payload_len);
|
|
|
bf3226 |
+ if (event->payload == NULL) {
|
|
|
bf3226 |
+ ErrLog("%s: Failed to allocate the event payload", __func__);
|
|
|
bf3226 |
+ free(event);
|
|
|
bf3226 |
+ return NULL;
|
|
|
bf3226 |
+ }
|
|
|
bf3226 |
+ }
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ event->reply.version = EVENT_VERSION_1;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ if (admin_conn != NULL)
|
|
|
bf3226 |
+ admin_get(admin_conn);
|
|
|
bf3226 |
+ event->admin_ref = admin_conn;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ DbgLog(DL3, "%s: allocated event: %p", __func__, event);
|
|
|
bf3226 |
+ return event;
|
|
|
bf3226 |
+}
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+static void event_limit_underrun()
|
|
|
bf3226 |
+{
|
|
|
bf3226 |
+ struct admin_conn_info *conn;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ DbgLog(DL3, "%s: pending_events_count: %lu", __func__, pending_events_count);
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+#ifdef WITH_LIBUDEV
|
|
|
bf3226 |
+ /* Notify the udev monitor */
|
|
|
bf3226 |
+ udev_mon_notify(EPOLLIN, &udev_mon);
|
|
|
bf3226 |
+#endif
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ /* Notify all admin connections */
|
|
|
bf3226 |
+ FOR_EACH_CONN_SAFE_BEGIN(admin_connections, conn) {
|
|
|
bf3226 |
+ admin_event_limit_underrun(conn);
|
|
|
bf3226 |
+ }
|
|
|
bf3226 |
+ FOR_EACH_CONN_SAFE_END(admin_connections, conn)
|
|
|
bf3226 |
+}
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+static void event_free(struct event_info *event)
|
|
|
bf3226 |
+{
|
|
|
bf3226 |
+ DbgLog(DL3, "%s: free event: %p", __func__, event);
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ if (event->payload != NULL)
|
|
|
bf3226 |
+ free(event->payload);
|
|
|
bf3226 |
+ free(event);
|
|
|
bf3226 |
+}
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+static int event_add_to_pending_list(struct event_info *event)
|
|
|
bf3226 |
+{
|
|
|
bf3226 |
+ DL_NODE *list;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ list = dlist_add_as_last(pending_events, event);
|
|
|
bf3226 |
+ if (list == NULL) {
|
|
|
bf3226 |
+ ErrLog("%s: failed add event to list of pending events", __func__);
|
|
|
bf3226 |
+ return -ENOMEM;
|
|
|
bf3226 |
+ }
|
|
|
bf3226 |
+ pending_events = list;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ pending_events_count++;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ return 0;
|
|
|
bf3226 |
+}
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+static void event_remove_from_pending_list(struct event_info *event)
|
|
|
bf3226 |
+{
|
|
|
bf3226 |
+ DL_NODE *node;
|
|
|
bf3226 |
+ int trigger = 0;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ node = dlist_find(pending_events, event);
|
|
|
bf3226 |
+ if (node != NULL) {
|
|
|
bf3226 |
+ pending_events = dlist_remove_node(pending_events, node);
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ if (pending_events_count >= MAX_PENDING_EVENTS)
|
|
|
bf3226 |
+ trigger = 1;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ if (pending_events_count > 0)
|
|
|
bf3226 |
+ pending_events_count--;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ if (trigger)
|
|
|
bf3226 |
+ event_limit_underrun();
|
|
|
bf3226 |
+ }
|
|
|
bf3226 |
+}
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+static int event_start_deliver(struct event_info *event)
|
|
|
bf3226 |
+{
|
|
|
bf3226 |
+ struct proc_conn_info *conn;
|
|
|
bf3226 |
+ int rc;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ DbgLog(DL3, "%s: event: %p", __func__, event);
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ if (pending_events_count >= MAX_PENDING_EVENTS) {
|
|
|
bf3226 |
+ InfoLog("%s: Max pending events reached", __func__);
|
|
|
bf3226 |
+ return -ENOSPC;
|
|
|
bf3226 |
+ }
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ /* Add event of the list of pending events */
|
|
|
bf3226 |
+ rc = event_add_to_pending_list(event);
|
|
|
bf3226 |
+ if (rc != 0)
|
|
|
bf3226 |
+ return rc;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ /*
|
|
|
bf3226 |
+ * Need to increment the event's ref count here, proc_deliver_event() may
|
|
|
bf3226 |
+ * already complete the event delivery for one process, which then would
|
|
|
bf3226 |
+ * free the event but it needs to be passed to other processes here, too.
|
|
|
bf3226 |
+ */
|
|
|
bf3226 |
+ event->proc_ref_count++;
|
|
|
bf3226 |
+ FOR_EACH_CONN_SAFE_BEGIN(proc_connections, conn) {
|
|
|
bf3226 |
+ rc = proc_deliver_event(conn, event);
|
|
|
bf3226 |
+ if (rc != 0)
|
|
|
bf3226 |
+ proc_hangup(conn);
|
|
|
bf3226 |
+ }
|
|
|
bf3226 |
+ FOR_EACH_CONN_SAFE_END(proc_connections, conn)
|
|
|
bf3226 |
+ event->proc_ref_count--;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ DbgLog(DL3, "%s: proc_ref_count: %u", __func__, event->proc_ref_count);
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ if (event->proc_ref_count == 0)
|
|
|
bf3226 |
+ event_delivered(event);
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ return 0;
|
|
|
bf3226 |
+}
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+static void event_delivered(struct event_info *event)
|
|
|
bf3226 |
+{
|
|
|
bf3226 |
+ struct admin_conn_info *conn;
|
|
|
bf3226 |
+ int rc;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ DbgLog(DL3, "%s: event: %p", __func__, event);
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ event_remove_from_pending_list(event);
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ /* Notify owning admin connection (if available), free otherwise */
|
|
|
bf3226 |
+ if (event->admin_ref != NULL) {
|
|
|
bf3226 |
+ conn = event->admin_ref;
|
|
|
bf3226 |
+ admin_get(conn);
|
|
|
bf3226 |
+ rc = admin_event_delivered(conn, event);
|
|
|
bf3226 |
+ if (rc != 0) {
|
|
|
bf3226 |
+ admin_hangup(conn);
|
|
|
bf3226 |
+ event_free(event);
|
|
|
bf3226 |
+ }
|
|
|
bf3226 |
+ admin_put(conn);
|
|
|
bf3226 |
+ } else {
|
|
|
bf3226 |
+ event_free(event);
|
|
|
bf3226 |
+ }
|
|
|
bf3226 |
+}
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+static int proc_new_conn(int socket, struct listener_info *listener)
|
|
|
bf3226 |
+{
|
|
|
bf3226 |
+ struct proc_conn_info *conn;
|
|
|
bf3226 |
+ struct event_info *event;
|
|
|
bf3226 |
+ DL_NODE *list, *node;
|
|
|
bf3226 |
+ int rc = 0;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ UNUSED(listener);
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ DbgLog(DL0, "%s: Accepted connection from process: socket: %d", __func__,
|
|
|
bf3226 |
+ socket);
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ conn = calloc(1, sizeof(struct proc_conn_info));
|
|
|
bf3226 |
+ if (conn == NULL) {
|
|
|
bf3226 |
+ ErrLog("%s: Failed to to allocate memory for the process connection",
|
|
|
bf3226 |
+ __func__);
|
|
|
bf3226 |
+ return -ENOMEM;
|
|
|
bf3226 |
+ /* Caller will close socket */
|
|
|
bf3226 |
+ }
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ DbgLog(DL3, "%s: process conn: %p", __func__, conn);
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ /* Add currently pending events to this connection */
|
|
|
bf3226 |
+ node = dlist_get_first(pending_events);
|
|
|
bf3226 |
+ while (node != NULL) {
|
|
|
bf3226 |
+ event = (struct event_info *)node->data;
|
|
|
bf3226 |
+ DbgLog(DL3, "%s: event: %p", __func__, event);
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ list = dlist_add_as_last(conn->events, event);
|
|
|
bf3226 |
+ if (list == NULL) {
|
|
|
bf3226 |
+ ErrLog("%s: failed add event to list of process's pending events",
|
|
|
bf3226 |
+ __func__);
|
|
|
bf3226 |
+ rc = -ENOMEM;
|
|
|
bf3226 |
+ goto out;
|
|
|
bf3226 |
+ }
|
|
|
bf3226 |
+ conn->events = list;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ event->proc_ref_count++;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ node = dlist_next(node);
|
|
|
bf3226 |
+ }
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ conn->state = PROC_INITIAL_SEND;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ rc = client_socket_init(socket, proc_xfer_complete, proc_hangup, proc_free,
|
|
|
bf3226 |
+ conn, &conn->client_info);
|
|
|
bf3226 |
+ if (rc != 0)
|
|
|
bf3226 |
+ goto out;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ /* Add it to the process connections list */
|
|
|
bf3226 |
+ list = dlist_add_as_first(proc_connections, conn);
|
|
|
bf3226 |
+ if (list == NULL) {
|
|
|
bf3226 |
+ rc = -ENOMEM;
|
|
|
bf3226 |
+ goto out;
|
|
|
bf3226 |
+ }
|
|
|
bf3226 |
+ proc_connections = list;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ proc_get(conn);
|
|
|
bf3226 |
+ rc = client_socket_send(&conn->client_info, &socketData,
|
|
|
bf3226 |
+ sizeof(socketData));
|
|
|
bf3226 |
+ proc_put(conn);
|
|
|
bf3226 |
+ conn = NULL; /* conn may have been freed by now */
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+out:
|
|
|
bf3226 |
+ if (rc != 0 && conn != NULL) {
|
|
|
bf3226 |
+ proc_hangup(conn);
|
|
|
bf3226 |
+ rc = 0; /* Don't return an error, we have already handled it */
|
|
|
bf3226 |
+ }
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ return rc;
|
|
|
bf3226 |
+}
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+static int proc_xfer_complete(void *client)
|
|
|
bf3226 |
+{
|
|
|
bf3226 |
+ struct proc_conn_info *conn = client;
|
|
|
bf3226 |
+ int rc;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ DbgLog(DL0, "%s: Xfer completed: process: %p socket: %d state: %d",
|
|
|
bf3226 |
+ __func__, conn, conn->client_info.socket, conn->state);
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ /*
|
|
|
bf3226 |
+ * A non-zero return code returned by this function causes the caller to
|
|
|
bf3226 |
+ * call proc_hangup(). Thus, no need to call proc_hangup() ourselves.
|
|
|
bf3226 |
+ */
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ switch (conn->state) {
|
|
|
bf3226 |
+ case PROC_INITIAL_SEND:
|
|
|
bf3226 |
+ conn->state = PROC_WAIT_FOR_EVENT;
|
|
|
bf3226 |
+ rc = proc_start_deliver_event(conn);
|
|
|
bf3226 |
+ conn = NULL; /* conn may have been freed by now */
|
|
|
bf3226 |
+ return rc;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ case PROC_WAIT_FOR_EVENT:
|
|
|
bf3226 |
+ /* handled in proc_start_deliver_event */
|
|
|
bf3226 |
+ break;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ case PROC_SEND_EVENT:
|
|
|
bf3226 |
+ if (conn->event == NULL) {
|
|
|
bf3226 |
+ TraceLog("%s: No current event to handle", __func__);
|
|
|
bf3226 |
+ return -EINVAL;
|
|
|
bf3226 |
+ }
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ if (conn->event->event.payload_len > 0) {
|
|
|
bf3226 |
+ conn->state = PROC_SEND_PAYLOAD;
|
|
|
bf3226 |
+ rc = client_socket_send(&conn->client_info, conn->event->payload,
|
|
|
bf3226 |
+ conn->event->event.payload_len);
|
|
|
bf3226 |
+ conn = NULL; /* conn may have been freed by now */
|
|
|
bf3226 |
+ return rc;
|
|
|
bf3226 |
+ }
|
|
|
bf3226 |
+ FALL_THROUGH;
|
|
|
bf3226 |
+ /* fall through */
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ case PROC_SEND_PAYLOAD:
|
|
|
bf3226 |
+ if (conn->event == NULL) {
|
|
|
bf3226 |
+ TraceLog("%s: No current event to handle", __func__);
|
|
|
bf3226 |
+ return -EINVAL;
|
|
|
bf3226 |
+ }
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ if (conn->event->event.flags & EVENT_FLAGS_REPLY_REQ) {
|
|
|
bf3226 |
+ conn->state = PROC_RECEIVE_REPLY;
|
|
|
bf3226 |
+ rc = client_socket_receive(&conn->client_info, &conn->reply,
|
|
|
bf3226 |
+ sizeof(conn->reply));
|
|
|
bf3226 |
+ conn = NULL; /* conn may have been freed by now */
|
|
|
bf3226 |
+ return rc;
|
|
|
bf3226 |
+ }
|
|
|
bf3226 |
+ FALL_THROUGH;
|
|
|
bf3226 |
+ /* fall through */
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ case PROC_RECEIVE_REPLY:
|
|
|
bf3226 |
+ if (conn->event == NULL) {
|
|
|
bf3226 |
+ TraceLog("%s: No current event to handle", __func__);
|
|
|
bf3226 |
+ return -EINVAL;
|
|
|
bf3226 |
+ }
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ if (conn->event->event.flags & EVENT_FLAGS_REPLY_REQ) {
|
|
|
bf3226 |
+ if (conn->reply.version != EVENT_VERSION_1) {
|
|
|
bf3226 |
+ InfoLog("%s: Reply has a wrong version: %u", __func__,
|
|
|
bf3226 |
+ conn->reply.version);
|
|
|
bf3226 |
+ return -EINVAL;
|
|
|
bf3226 |
+ }
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ /* Update reply counters in event */
|
|
|
bf3226 |
+ conn->event->reply.positive_replies += conn->reply.positive_replies;
|
|
|
bf3226 |
+ conn->event->reply.negative_replies += conn->reply.negative_replies;
|
|
|
bf3226 |
+ conn->event->reply.nothandled_replies +=
|
|
|
bf3226 |
+ conn->reply.nothandled_replies;
|
|
|
bf3226 |
+ }
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ conn->state = PROC_WAIT_FOR_EVENT;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ rc = proc_event_delivered(conn, conn->event);
|
|
|
bf3226 |
+ conn = NULL; /* conn may have been freed by now */
|
|
|
bf3226 |
+ return rc;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ case PROC_HANGUP:
|
|
|
bf3226 |
+ break;
|
|
|
bf3226 |
+ }
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ return 0;
|
|
|
bf3226 |
+}
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+static int proc_start_deliver_event(struct proc_conn_info *conn)
|
|
|
bf3226 |
+{
|
|
|
bf3226 |
+ DL_NODE *node;
|
|
|
bf3226 |
+ int rc;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ if (conn->state != PROC_WAIT_FOR_EVENT)
|
|
|
bf3226 |
+ return 0;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ node = dlist_get_first(conn->events);
|
|
|
bf3226 |
+ if (node == NULL)
|
|
|
bf3226 |
+ return 0;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ conn->event = node->data;
|
|
|
bf3226 |
+ memset(&conn->reply, 0, sizeof(conn->reply));
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ DbgLog(DL3, "%s: process: %p event: %p", __func__, conn, conn->event);
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ conn->state = PROC_SEND_EVENT;
|
|
|
bf3226 |
+ rc = client_socket_send(&conn->client_info, &conn->event->event,
|
|
|
bf3226 |
+ sizeof(conn->event->event));
|
|
|
bf3226 |
+ conn = NULL; /* conn may have been freed by now */
|
|
|
bf3226 |
+ return rc;
|
|
|
bf3226 |
+}
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+static int proc_deliver_event(struct proc_conn_info *conn,
|
|
|
bf3226 |
+ struct event_info *event)
|
|
|
bf3226 |
+{
|
|
|
bf3226 |
+ DL_NODE *list;
|
|
|
bf3226 |
+ int rc;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ DbgLog(DL3, "%s: process: %p event: %p", __func__, conn, event);
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ if (conn->state == PROC_HANGUP)
|
|
|
bf3226 |
+ return 0;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ /* Add to process's event list and incr. reference count */
|
|
|
bf3226 |
+ list = dlist_add_as_last(conn->events, event);
|
|
|
bf3226 |
+ if (list == NULL) {
|
|
|
bf3226 |
+ ErrLog("%s: failed add event to list of process's pending events",
|
|
|
bf3226 |
+ __func__);
|
|
|
bf3226 |
+ return -ENOMEM;
|
|
|
bf3226 |
+ }
|
|
|
bf3226 |
+ conn->events = list;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ event->proc_ref_count++;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ rc = proc_start_deliver_event(conn);
|
|
|
bf3226 |
+ return rc;
|
|
|
bf3226 |
+}
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+static int proc_event_delivered(struct proc_conn_info *conn,
|
|
|
bf3226 |
+ struct event_info *event)
|
|
|
bf3226 |
+{
|
|
|
bf3226 |
+ DL_NODE *node;
|
|
|
bf3226 |
+ int rc;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ DbgLog(DL3, "%s: process: %p event: %p", __func__, conn, event);
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ conn->event = NULL;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ /* Remove from process's event list and decr. reference count */
|
|
|
bf3226 |
+ node = dlist_find(conn->events, event);
|
|
|
bf3226 |
+ if (node != NULL) {
|
|
|
bf3226 |
+ conn->events = dlist_remove_node(conn->events, node);
|
|
|
bf3226 |
+ event->proc_ref_count--;
|
|
|
bf3226 |
+ }
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ DbgLog(DL3, "%s: proc_ref_count: %u", __func__, event->proc_ref_count);
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ if (event->proc_ref_count == 0)
|
|
|
bf3226 |
+ event_delivered(event);
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ /* Deliver further pending events, if any */
|
|
|
bf3226 |
+ rc = proc_start_deliver_event(conn);
|
|
|
bf3226 |
+ conn = NULL; /* conn may have been freed by now */
|
|
|
bf3226 |
+ return rc;
|
|
|
bf3226 |
+}
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+static inline void proc_get(struct proc_conn_info *conn)
|
|
|
bf3226 |
+{
|
|
|
bf3226 |
+ client_socket_get(&conn->client_info);
|
|
|
bf3226 |
+}
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+static inline void proc_put(struct proc_conn_info *conn)
|
|
|
bf3226 |
+{
|
|
|
bf3226 |
+ client_socket_put(&conn->client_info);
|
|
|
bf3226 |
+}
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+static void proc_hangup(void *client)
|
|
|
bf3226 |
+{
|
|
|
bf3226 |
+ struct proc_conn_info *conn = client;
|
|
|
bf3226 |
+ struct event_info *event;
|
|
|
bf3226 |
+ DL_NODE *node;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ DbgLog(DL0, "%s: process: %p socket: %d state: %d", __func__, conn,
|
|
|
bf3226 |
+ conn->client_info.socket, conn->state);
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ if (conn->state == PROC_HANGUP)
|
|
|
bf3226 |
+ return;
|
|
|
bf3226 |
+ conn->state = PROC_HANGUP;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ /* Unlink all pending events */
|
|
|
bf3226 |
+ while ((node = dlist_get_first(conn->events)) != NULL) {
|
|
|
bf3226 |
+ event = node->data;
|
|
|
bf3226 |
+ /* We did not handle this event */
|
|
|
bf3226 |
+ event->reply.nothandled_replies++;
|
|
|
bf3226 |
+ proc_event_delivered(conn, event);
|
|
|
bf3226 |
+ }
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ client_socket_term(&conn->client_info);
|
|
|
bf3226 |
+ proc_put(conn);
|
|
|
bf3226 |
+}
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+static void proc_free(void *client)
|
|
|
bf3226 |
+{
|
|
|
bf3226 |
+ struct proc_conn_info *conn = client;
|
|
|
bf3226 |
+ DL_NODE *node;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ /* Remove it from the process connections list */
|
|
|
bf3226 |
+ node = dlist_find(proc_connections, conn);
|
|
|
bf3226 |
+ if (node != NULL) {
|
|
|
bf3226 |
+ proc_connections = dlist_remove_node(proc_connections, node);
|
|
|
bf3226 |
+ listener_client_hangup(&proc_listener);
|
|
|
bf3226 |
+ }
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ DbgLog(DL0, "%s: process: %p", __func__, conn);
|
|
|
bf3226 |
+ free(conn);
|
|
|
bf3226 |
+}
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+static int admin_new_conn(int socket, struct listener_info *listener)
|
|
|
bf3226 |
+{
|
|
|
bf3226 |
+ struct admin_conn_info *conn;
|
|
|
bf3226 |
+ DL_NODE *list;
|
|
|
bf3226 |
+ int rc = 0;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ UNUSED(listener);
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ DbgLog(DL0, "%s: Accepted connection from admin: socket: %d", __func__,
|
|
|
bf3226 |
+ socket);
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ conn = calloc(1, sizeof(struct admin_conn_info));
|
|
|
bf3226 |
+ if (conn == NULL) {
|
|
|
bf3226 |
+ ErrLog("%s: Failed to to allocate memory for the admin connection",
|
|
|
bf3226 |
+ __func__);
|
|
|
bf3226 |
+ return -ENOMEM;
|
|
|
bf3226 |
+ /* Caller will close socket */
|
|
|
bf3226 |
+ }
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ DbgLog(DL3, "%s: admin conn: %p", __func__, conn);
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ conn->state = ADMIN_RECEIVE_EVENT;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ rc = client_socket_init(socket, admin_xfer_complete, admin_hangup,
|
|
|
bf3226 |
+ admin_free, conn, &conn->client_info);
|
|
|
bf3226 |
+ if (rc != 0)
|
|
|
bf3226 |
+ goto out;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ conn->event = event_new(0, conn);
|
|
|
bf3226 |
+ if (conn->event == NULL) {
|
|
|
bf3226 |
+ ErrLog("%s: Failed to allocate a new event", __func__);
|
|
|
bf3226 |
+ rc = -ENOMEM;
|
|
|
bf3226 |
+ goto out;
|
|
|
bf3226 |
+ }
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ /* Add it to the admin connections list */
|
|
|
bf3226 |
+ list = dlist_add_as_first(admin_connections, conn);
|
|
|
bf3226 |
+ if (list == NULL) {
|
|
|
bf3226 |
+ ErrLog("%s: Failed to add connection to list of admin connections",
|
|
|
bf3226 |
+ __func__);
|
|
|
bf3226 |
+ rc = -ENOMEM;
|
|
|
bf3226 |
+ goto out;
|
|
|
bf3226 |
+ }
|
|
|
bf3226 |
+ admin_connections = list;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ admin_get(conn);
|
|
|
bf3226 |
+ rc = client_socket_receive(&conn->client_info, &conn->event->event,
|
|
|
bf3226 |
+ sizeof(conn->event->event));
|
|
|
bf3226 |
+ admin_put(conn);
|
|
|
bf3226 |
+ conn = NULL; /* conn may have been freed by now */
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+out:
|
|
|
bf3226 |
+ if (rc != 0 && conn != NULL) {
|
|
|
bf3226 |
+ admin_hangup(conn);
|
|
|
bf3226 |
+ rc = 0; /* Don't return an error, we have already handled it */
|
|
|
bf3226 |
+ }
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ return rc;
|
|
|
bf3226 |
+}
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+static int admin_xfer_complete(void *client)
|
|
|
bf3226 |
+{
|
|
|
bf3226 |
+ struct admin_conn_info *conn = client;
|
|
|
bf3226 |
+ int rc;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ DbgLog(DL0, "%s: Xfer completed: admin: %p socket: %d state: %d",
|
|
|
bf3226 |
+ __func__, conn, conn->client_info.socket, conn->state);
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ /*
|
|
|
bf3226 |
+ * A non-zero return code returned by this function causes the caller to
|
|
|
bf3226 |
+ * call admin_hangup(). Thus, no need to call admin_hangup() ourselves.
|
|
|
bf3226 |
+ */
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ if (conn->event == NULL) {
|
|
|
bf3226 |
+ TraceLog("%s: No current event", __func__);
|
|
|
bf3226 |
+ return -EINVAL;
|
|
|
bf3226 |
+ }
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ switch (conn->state) {
|
|
|
bf3226 |
+ case ADMIN_RECEIVE_EVENT:
|
|
|
bf3226 |
+ /* We have received the event from the admin */
|
|
|
bf3226 |
+ DbgLog(DL3, "%s: Event version: %u", __func__,
|
|
|
bf3226 |
+ conn->event->event.version);
|
|
|
bf3226 |
+ DbgLog(DL3, "%s: Event type: 0x%08x", __func__,
|
|
|
bf3226 |
+ conn->event->event.type);
|
|
|
bf3226 |
+ DbgLog(DL3, "%s: Event flags: 0x%08x", __func__,
|
|
|
bf3226 |
+ conn->event->event.flags);
|
|
|
bf3226 |
+ DbgLog(DL3, "%s: Event token_type: 0x%08x", __func__,
|
|
|
bf3226 |
+ conn->event->event.token_type);
|
|
|
bf3226 |
+ DbgLog(DL3, "%s: Event token_name: '%.32s'", __func__,
|
|
|
bf3226 |
+ conn->event->event.token_label);
|
|
|
bf3226 |
+ DbgLog(DL3, "%s: Event process_id: %u", __func__,
|
|
|
bf3226 |
+ conn->event->event.process_id);
|
|
|
bf3226 |
+ DbgLog(DL3, "%s: Event payload_len: %u", __func__,
|
|
|
bf3226 |
+ conn->event->event.payload_len);
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ if (conn->event->event.version != EVENT_VERSION_1) {
|
|
|
bf3226 |
+ InfoLog("%s: Admin event has invalid version: %d", __func__,
|
|
|
bf3226 |
+ conn->event->event.version);
|
|
|
bf3226 |
+ return -EINVAL;
|
|
|
bf3226 |
+ }
|
|
|
bf3226 |
+ if (conn->event->event.payload_len > EVENT_MAX_PAYLOAD_LENGTH) {
|
|
|
bf3226 |
+ InfoLog("%s: Admin event payload is too large: %u", __func__,
|
|
|
bf3226 |
+ conn->event->event.payload_len);
|
|
|
bf3226 |
+ return -EMSGSIZE;
|
|
|
bf3226 |
+ }
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ if (conn->event->event.payload_len > 0) {
|
|
|
bf3226 |
+ conn->event->payload = malloc(conn->event->event.payload_len);
|
|
|
bf3226 |
+ if (conn->event->payload == NULL) {
|
|
|
bf3226 |
+ ErrLog("%s: Failed to allocate the payload buffer", __func__);
|
|
|
bf3226 |
+ return -ENOMEM;
|
|
|
bf3226 |
+ }
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ conn->state = ADMIN_RECEIVE_PAYLOAD;
|
|
|
bf3226 |
+ rc = client_socket_receive(&conn->client_info, conn->event->payload,
|
|
|
bf3226 |
+ conn->event->event.payload_len);
|
|
|
bf3226 |
+ conn = NULL; /* conn may have been freed by now */
|
|
|
bf3226 |
+ return rc;
|
|
|
bf3226 |
+ }
|
|
|
bf3226 |
+ FALL_THROUGH;
|
|
|
bf3226 |
+ /* fall through */
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ case ADMIN_RECEIVE_PAYLOAD:
|
|
|
bf3226 |
+ /* We have received the payload (if any) from the admin */
|
|
|
bf3226 |
+ conn->state = ADMIN_EVENT_DELIVERED;
|
|
|
bf3226 |
+ rc = event_start_deliver(conn->event);
|
|
|
bf3226 |
+ if (rc != 0) {
|
|
|
bf3226 |
+ if (rc == -ENOSPC) {
|
|
|
bf3226 |
+ /* Event limit reached, delay */
|
|
|
bf3226 |
+ conn->state = ADMIN_WAIT_FOR_EVENT_LIMIT;
|
|
|
bf3226 |
+ return 0;
|
|
|
bf3226 |
+ }
|
|
|
bf3226 |
+ return rc;
|
|
|
bf3226 |
+ }
|
|
|
bf3226 |
+ break;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ case ADMIN_WAIT_FOR_EVENT_LIMIT:
|
|
|
bf3226 |
+ /* This state is handled in admin_event_limit_underrun() */
|
|
|
bf3226 |
+ break;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ case ADMIN_EVENT_DELIVERED:
|
|
|
bf3226 |
+ /* This state is handled in admin_event_delivered() */
|
|
|
bf3226 |
+ break;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ case ADMIN_SEND_REPLY:
|
|
|
bf3226 |
+ /* The reply has been sent to the admin */
|
|
|
bf3226 |
+ if (conn->event->admin_ref != NULL)
|
|
|
bf3226 |
+ admin_put(conn->event->admin_ref);
|
|
|
bf3226 |
+ conn->event->admin_ref = NULL;
|
|
|
bf3226 |
+ event_free(conn->event);
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ conn->event = event_new(0, conn);
|
|
|
bf3226 |
+ if (conn->event == NULL) {
|
|
|
bf3226 |
+ ErrLog("%s: Failed to allocate a new event", __func__);
|
|
|
bf3226 |
+ return -ENOMEM;
|
|
|
bf3226 |
+ }
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ conn->state = ADMIN_RECEIVE_EVENT;
|
|
|
bf3226 |
+ rc = client_socket_receive(&conn->client_info, &conn->event->event,
|
|
|
bf3226 |
+ sizeof(conn->event->event));
|
|
|
bf3226 |
+ conn = NULL; /* conn may have been freed by now */
|
|
|
bf3226 |
+ return rc;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ case ADMIN_HANGUP:
|
|
|
bf3226 |
+ break;
|
|
|
bf3226 |
+ }
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ return 0;
|
|
|
bf3226 |
+}
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+static void admin_event_limit_underrun(struct admin_conn_info *conn)
|
|
|
bf3226 |
+{
|
|
|
bf3226 |
+ int rc;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ DbgLog(DL3, "%s: admin: %p state: %d", __func__, conn, conn->state);
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ if (conn->state != ADMIN_WAIT_FOR_EVENT_LIMIT)
|
|
|
bf3226 |
+ return;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ conn->state = ADMIN_EVENT_DELIVERED;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ rc = event_start_deliver(conn->event);
|
|
|
bf3226 |
+ if (rc != 0) {
|
|
|
bf3226 |
+ if (rc == -ENOSPC) {
|
|
|
bf3226 |
+ /* Event limit reached, delay */
|
|
|
bf3226 |
+ conn->state = ADMIN_WAIT_FOR_EVENT_LIMIT;
|
|
|
bf3226 |
+ return;
|
|
|
bf3226 |
+ }
|
|
|
bf3226 |
+ admin_hangup(conn);
|
|
|
bf3226 |
+ }
|
|
|
bf3226 |
+}
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+static int admin_event_delivered(struct admin_conn_info *conn,
|
|
|
bf3226 |
+ struct event_info *event)
|
|
|
bf3226 |
+{
|
|
|
bf3226 |
+ int rc;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ DbgLog(DL3, "%s: admin: %p event: %p", __func__, conn, event);
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ /*
|
|
|
bf3226 |
+ * A non-zero return code returned by this function causes the caller to
|
|
|
bf3226 |
+ * call admin_hangup(). Thus, no need to call admin_hangup() ourselves.
|
|
|
bf3226 |
+ */
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ if (conn->state != ADMIN_EVENT_DELIVERED) {
|
|
|
bf3226 |
+ TraceLog("%s: wrong state: %d", __func__, conn->state);
|
|
|
bf3226 |
+ return -EINVAL;
|
|
|
bf3226 |
+ }
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ if (event->event.flags & EVENT_FLAGS_REPLY_REQ) {
|
|
|
bf3226 |
+ if (conn->event != event) {
|
|
|
bf3226 |
+ TraceLog("%s: event not the current event", __func__);
|
|
|
bf3226 |
+ return -EINVAL;
|
|
|
bf3226 |
+ }
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ DbgLog(DL3, "%s: Reply version: %u", __func__,
|
|
|
bf3226 |
+ event->reply.version);
|
|
|
bf3226 |
+ DbgLog(DL3, "%s: Reply positive: %lu", __func__,
|
|
|
bf3226 |
+ event->reply.positive_replies);
|
|
|
bf3226 |
+ DbgLog(DL3, "%s: Reply negative: %lu", __func__,
|
|
|
bf3226 |
+ event->reply.negative_replies);
|
|
|
bf3226 |
+ DbgLog(DL3, "%s: Reply not-handled: %lu", __func__,
|
|
|
bf3226 |
+ event->reply.nothandled_replies);
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ conn->state = ADMIN_SEND_REPLY;
|
|
|
bf3226 |
+ rc = client_socket_send(&conn->client_info, &event->reply,
|
|
|
bf3226 |
+ sizeof(event->reply));
|
|
|
bf3226 |
+ return rc;
|
|
|
bf3226 |
+ }
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ /* No reply required, free the event, and receive the next one */
|
|
|
bf3226 |
+ if (event->admin_ref != NULL)
|
|
|
bf3226 |
+ admin_put(event->admin_ref);
|
|
|
bf3226 |
+ event->admin_ref = NULL;
|
|
|
bf3226 |
+ event_free(event);
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ conn->event = event_new(0, conn);
|
|
|
bf3226 |
+ if (conn->event == NULL) {
|
|
|
bf3226 |
+ ErrLog("%s: Failed to allocate a new event", __func__);
|
|
|
bf3226 |
+ return -ENOMEM;
|
|
|
bf3226 |
+ }
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ conn->state = ADMIN_RECEIVE_EVENT;
|
|
|
bf3226 |
+ rc = client_socket_receive(&conn->client_info, &conn->event->event,
|
|
|
bf3226 |
+ sizeof(conn->event->event));
|
|
|
bf3226 |
+ return rc;
|
|
|
bf3226 |
+}
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+static inline void admin_get(struct admin_conn_info *conn)
|
|
|
bf3226 |
+{
|
|
|
bf3226 |
+ client_socket_get(&conn->client_info);
|
|
|
bf3226 |
+}
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+static inline void admin_put(struct admin_conn_info *conn)
|
|
|
bf3226 |
+{
|
|
|
bf3226 |
+ client_socket_put(&conn->client_info);
|
|
|
bf3226 |
+}
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+static void admin_hangup(void *client)
|
|
|
bf3226 |
+{
|
|
|
bf3226 |
+ struct admin_conn_info *conn = client;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ DbgLog(DL0, "%s: admin: %p socket: %d state: %d", __func__, conn,
|
|
|
bf3226 |
+ conn->client_info.socket, conn->state);
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ if (conn->state == ADMIN_HANGUP)
|
|
|
bf3226 |
+ return;
|
|
|
bf3226 |
+ conn->state = ADMIN_HANGUP;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ /* Unlink pending event (if any) */
|
|
|
bf3226 |
+ if (conn->event != NULL) {
|
|
|
bf3226 |
+ if (conn->event->admin_ref != NULL)
|
|
|
bf3226 |
+ admin_put(conn->event->admin_ref);
|
|
|
bf3226 |
+ conn->event->admin_ref = NULL;
|
|
|
bf3226 |
+ if (conn->event->proc_ref_count == 0) {
|
|
|
bf3226 |
+ event_remove_from_pending_list(conn->event);
|
|
|
bf3226 |
+ event_free(conn->event);
|
|
|
bf3226 |
+ }
|
|
|
bf3226 |
+ conn->event = NULL;
|
|
|
bf3226 |
+ }
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ client_socket_term(&conn->client_info);
|
|
|
bf3226 |
+ admin_put(conn);
|
|
|
bf3226 |
+}
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+static void admin_free(void *client)
|
|
|
bf3226 |
+{
|
|
|
bf3226 |
+ struct admin_conn_info *conn = client;
|
|
|
bf3226 |
+ DL_NODE *node;
|
|
|
bf3226 |
|
|
|
bf3226 |
-int proc_listener_socket = -1;
|
|
|
bf3226 |
+ /* Remove it from the admin connections list */
|
|
|
bf3226 |
+ node = dlist_find(admin_connections, conn);
|
|
|
bf3226 |
+ if (node != NULL) {
|
|
|
bf3226 |
+ admin_connections = dlist_remove_node(admin_connections, node);
|
|
|
bf3226 |
+ listener_client_hangup(&admin_listener);
|
|
|
bf3226 |
+ }
|
|
|
bf3226 |
|
|
|
bf3226 |
-static void close_listener_socket(int socketfd, const char *file_path);
|
|
|
bf3226 |
+ DbgLog(DL0, "%s: admin: %p", __func__, conn);
|
|
|
bf3226 |
+ free(conn);
|
|
|
bf3226 |
+}
|
|
|
bf3226 |
|
|
|
bf3226 |
-// Creates the daemon's listener socket, to which clients will connect and
|
|
|
bf3226 |
-// retrieve slot information through. Returns the file descriptor of the
|
|
|
bf3226 |
-// created socket.
|
|
|
bf3226 |
-static int create_listener_socket(const char *file_path)
|
|
|
bf3226 |
+static int listener_socket_create(const char *file_path)
|
|
|
bf3226 |
{
|
|
|
bf3226 |
struct sockaddr_un address;
|
|
|
bf3226 |
struct group *grp;
|
|
|
bf3226 |
- int socketfd;
|
|
|
bf3226 |
+ int listener_socket, err;
|
|
|
bf3226 |
|
|
|
bf3226 |
- socketfd = socket(PF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0);
|
|
|
bf3226 |
- if (socketfd < 0) {
|
|
|
bf3226 |
- ErrLog("Failed to create listener socket, errno 0x%X.", errno);
|
|
|
bf3226 |
+ listener_socket = socket(PF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0);
|
|
|
bf3226 |
+ if (listener_socket < 0) {
|
|
|
bf3226 |
+ err = errno;
|
|
|
bf3226 |
+ ErrLog("%s: Failed to create listener socket, errno %d (%s).",
|
|
|
bf3226 |
+ __func__, err, strerror(err));
|
|
|
bf3226 |
return -1;
|
|
|
bf3226 |
}
|
|
|
bf3226 |
if (unlink(file_path) && errno != ENOENT) {
|
|
|
bf3226 |
- ErrLog("Failed to unlink socket file, errno 0x%X.", errno);
|
|
|
bf3226 |
+ err = errno;
|
|
|
bf3226 |
+ ErrLog("%s: Failed to unlink socket file, errno %d (%s).", __func__,
|
|
|
bf3226 |
+ err, strerror(err));
|
|
|
bf3226 |
goto error;
|
|
|
bf3226 |
}
|
|
|
bf3226 |
|
|
|
bf3226 |
@@ -52,50 +1249,389 @@ static int create_listener_socket(const char *file_path)
|
|
|
bf3226 |
address.sun_family = AF_UNIX;
|
|
|
bf3226 |
strcpy(address.sun_path, file_path);
|
|
|
bf3226 |
|
|
|
bf3226 |
- if (bind(socketfd,
|
|
|
bf3226 |
+ if (bind(listener_socket,
|
|
|
bf3226 |
(struct sockaddr *) &address, sizeof(struct sockaddr_un)) != 0) {
|
|
|
bf3226 |
- ErrLog("Failed to bind to socket, errno 0x%X.", errno);
|
|
|
bf3226 |
+ err = errno;
|
|
|
bf3226 |
+ ErrLog("%s: Failed to bind to socket, errno %d (%s).", __func__, err,
|
|
|
bf3226 |
+ strerror(err));
|
|
|
bf3226 |
goto error;
|
|
|
bf3226 |
}
|
|
|
bf3226 |
// make socket file part of the pkcs11 group, and write accessable
|
|
|
bf3226 |
// for that group
|
|
|
bf3226 |
grp = getgrnam("pkcs11");
|
|
|
bf3226 |
if (!grp) {
|
|
|
bf3226 |
- ErrLog("Group PKCS#11 does not exist");
|
|
|
bf3226 |
+ ErrLog("%s: Group PKCS#11 does not exist", __func__);
|
|
|
bf3226 |
goto error;
|
|
|
bf3226 |
}
|
|
|
bf3226 |
if (chown(file_path, 0, grp->gr_gid)) {
|
|
|
bf3226 |
- ErrLog("Could not change file group on socket, errno 0x%X.", errno);
|
|
|
bf3226 |
+ err = errno;
|
|
|
bf3226 |
+ ErrLog("%s: Could not change file group on socket, errno %d (%s).",
|
|
|
bf3226 |
+ __func__, err, strerror(err));
|
|
|
bf3226 |
goto error;
|
|
|
bf3226 |
}
|
|
|
bf3226 |
if (chmod(file_path,
|
|
|
bf3226 |
- S_IRUSR | S_IRGRP | S_IWUSR | S_IWGRP | S_IXUSR | S_IXGRP)) {
|
|
|
bf3226 |
- ErrLog("Could not change file permissions on socket, errno 0x%X.",
|
|
|
bf3226 |
- errno);
|
|
|
bf3226 |
+ S_IRUSR | S_IRGRP | S_IWUSR | S_IWGRP)) {
|
|
|
bf3226 |
+ err = errno;
|
|
|
bf3226 |
+ ErrLog("%s: Could not change file permissions on socket, errno %d (%s).",
|
|
|
bf3226 |
+ __func__, err, strerror(err));
|
|
|
bf3226 |
goto error;
|
|
|
bf3226 |
}
|
|
|
bf3226 |
|
|
|
bf3226 |
- if (listen(socketfd, 20) != 0) {
|
|
|
bf3226 |
- ErrLog("Failed to listen to socket, errno 0x%X.", errno);
|
|
|
bf3226 |
+ if (listen(listener_socket, 20) != 0) {
|
|
|
bf3226 |
+ err = errno;
|
|
|
bf3226 |
+ ErrLog("%s: Failed to listen to socket, errno %d (%s).", __func__, err,
|
|
|
bf3226 |
+ strerror(err));
|
|
|
bf3226 |
goto error;
|
|
|
bf3226 |
}
|
|
|
bf3226 |
|
|
|
bf3226 |
- return socketfd;
|
|
|
bf3226 |
+ return listener_socket;
|
|
|
bf3226 |
|
|
|
bf3226 |
error:
|
|
|
bf3226 |
- if (socketfd >= 0)
|
|
|
bf3226 |
- close_listener_socket(socketfd, file_path);
|
|
|
bf3226 |
+ if (listener_socket >= 0)
|
|
|
bf3226 |
+ listener_socket_close(listener_socket, file_path);
|
|
|
bf3226 |
|
|
|
bf3226 |
return -1;
|
|
|
bf3226 |
}
|
|
|
bf3226 |
|
|
|
bf3226 |
|
|
|
bf3226 |
-static void close_listener_socket(int socketfd, const char *file_path)
|
|
|
bf3226 |
+static void listener_socket_close(int listener_socket, const char *file_path)
|
|
|
bf3226 |
{
|
|
|
bf3226 |
- close(socketfd);
|
|
|
bf3226 |
+ close(listener_socket);
|
|
|
bf3226 |
unlink(file_path);
|
|
|
bf3226 |
}
|
|
|
bf3226 |
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+static int listener_notify(int events, void *private)
|
|
|
bf3226 |
+{
|
|
|
bf3226 |
+ struct listener_info *listener = private;
|
|
|
bf3226 |
+ struct sockaddr_un address;
|
|
|
bf3226 |
+ socklen_t address_length = sizeof(address);
|
|
|
bf3226 |
+ int client_socket, rc, err;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ if ((events & EPOLLIN) == 0)
|
|
|
bf3226 |
+ return 0;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ /* epoll is edge triggered. We must call accept until we get EWOULDBLOCK */
|
|
|
bf3226 |
+ while (listener->num_clients < listener->max_num_clients) {
|
|
|
bf3226 |
+ client_socket = accept(listener->socket, (struct sockaddr *) &address,
|
|
|
bf3226 |
+ &address_length);
|
|
|
bf3226 |
+ if (client_socket < 0) {
|
|
|
bf3226 |
+ err = errno;
|
|
|
bf3226 |
+ if (err == EWOULDBLOCK)
|
|
|
bf3226 |
+ break;
|
|
|
bf3226 |
+ InfoLog("%s: Failed to accept connection on socket %d, errno %d (%s).",
|
|
|
bf3226 |
+ __func__, listener->socket, err, strerror(err));
|
|
|
bf3226 |
+ return -err;
|
|
|
bf3226 |
+ }
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ rc = listener->new_conn(client_socket, listener);
|
|
|
bf3226 |
+ if (rc != 0) {
|
|
|
bf3226 |
+ TraceLog("%s: new_conn callback failed for client socket %d, rc: %d",
|
|
|
bf3226 |
+ __func__, client_socket, rc);
|
|
|
bf3226 |
+ close(client_socket);
|
|
|
bf3226 |
+ continue;
|
|
|
bf3226 |
+ }
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ listener->num_clients++;
|
|
|
bf3226 |
+ }
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ return 0;
|
|
|
bf3226 |
+}
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+static int listener_client_hangup(struct listener_info *listener)
|
|
|
bf3226 |
+{
|
|
|
bf3226 |
+ int rc, trigger = 0;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ if (listener->num_clients >= listener->max_num_clients)
|
|
|
bf3226 |
+ trigger = 1; /* We were at max clients, trigger accept now */
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ if (listener->num_clients > 0)
|
|
|
bf3226 |
+ listener->num_clients--;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ if (trigger && listener->num_clients < listener->max_num_clients) {
|
|
|
bf3226 |
+ rc = listener_notify(EPOLLIN, listener);
|
|
|
bf3226 |
+ if (rc != 0)
|
|
|
bf3226 |
+ return rc;
|
|
|
bf3226 |
+ }
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ return 0;
|
|
|
bf3226 |
+}
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+static int listener_create(const char *file_path,
|
|
|
bf3226 |
+ struct listener_info *listener,
|
|
|
bf3226 |
+ int (* new_conn)(int socket,
|
|
|
bf3226 |
+ struct listener_info *listener),
|
|
|
bf3226 |
+ unsigned long max_num_clients)
|
|
|
bf3226 |
+{
|
|
|
bf3226 |
+ struct epoll_event evt;
|
|
|
bf3226 |
+ int rc, err;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ if (listener == NULL || new_conn == NULL)
|
|
|
bf3226 |
+ return FALSE;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ memset(listener, 0, sizeof(*listener));
|
|
|
bf3226 |
+ epoll_info_init(&listener->ep_info, listener_notify, NULL, listener);
|
|
|
bf3226 |
+ listener->file_path = file_path;
|
|
|
bf3226 |
+ listener->new_conn = new_conn;
|
|
|
bf3226 |
+ listener->max_num_clients = max_num_clients;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ listener->socket = listener_socket_create(file_path);
|
|
|
bf3226 |
+ if (listener->socket < 0)
|
|
|
bf3226 |
+ return FALSE;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ evt.events = EPOLLIN | EPOLLET;
|
|
|
bf3226 |
+ evt.data.ptr = &listener->ep_info;
|
|
|
bf3226 |
+ rc = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, listener->socket, &evt);
|
|
|
bf3226 |
+ if (rc != 0) {
|
|
|
bf3226 |
+ err = errno;
|
|
|
bf3226 |
+ TraceLog("%s: Failed add listener socket %d to epoll, errno %d (%s).",
|
|
|
bf3226 |
+ __func__, listener->socket, err, strerror(err));
|
|
|
bf3226 |
+ return FALSE;
|
|
|
bf3226 |
+ }
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ return TRUE;
|
|
|
bf3226 |
+}
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+static void listener_term(struct listener_info *listener)
|
|
|
bf3226 |
+{
|
|
|
bf3226 |
+ if (listener == NULL || listener->socket < 0)
|
|
|
bf3226 |
+ return;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ epoll_ctl(epoll_fd, EPOLL_CTL_DEL, listener->socket, NULL);
|
|
|
bf3226 |
+ listener_socket_close(listener->socket, listener->file_path);
|
|
|
bf3226 |
+}
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+#ifdef WITH_LIBUDEV
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+static int udev_mon_init(const char *subsystem, struct udev_mon *udev_mon)
|
|
|
bf3226 |
+{
|
|
|
bf3226 |
+ struct epoll_event evt;
|
|
|
bf3226 |
+ int rc, err;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ if (subsystem == NULL || udev_mon == NULL)
|
|
|
bf3226 |
+ return FALSE;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ udev_mon->delayed_event = 0;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ udev_mon->udev = udev_new();
|
|
|
bf3226 |
+ if (udev_mon->udev == NULL) {
|
|
|
bf3226 |
+ ErrLog("%s: udev_new failed", __func__);
|
|
|
bf3226 |
+ goto error;
|
|
|
bf3226 |
+ }
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ udev_mon->mon = udev_monitor_new_from_netlink(udev_mon->udev, "udev");
|
|
|
bf3226 |
+ if (udev_mon->mon == NULL) {
|
|
|
bf3226 |
+ ErrLog("%s: udev_monitor_new_from_netlink failed", __func__);
|
|
|
bf3226 |
+ goto error;
|
|
|
bf3226 |
+ }
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ /*
|
|
|
bf3226 |
+ * Try to increase the receive buffer size. This may fail if the required
|
|
|
bf3226 |
+ * privileges are not given. Ignore if it fails.
|
|
|
bf3226 |
+ */
|
|
|
bf3226 |
+ udev_monitor_set_receive_buffer_size(udev_mon->mon, UDEV_RECV_BUFFFER_SIZE);
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ rc = udev_monitor_filter_add_match_subsystem_devtype(udev_mon->mon,
|
|
|
bf3226 |
+ subsystem, NULL);
|
|
|
bf3226 |
+ if (rc != 0) {
|
|
|
bf3226 |
+ ErrLog("%s: udev_monitor_filter_add_match_subsystem_devtype failed: "
|
|
|
bf3226 |
+ "rc=%d", __func__, rc);
|
|
|
bf3226 |
+ goto error;
|
|
|
bf3226 |
+ }
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ rc = udev_monitor_enable_receiving(udev_mon->mon);
|
|
|
bf3226 |
+ if (rc != 0) {
|
|
|
bf3226 |
+ ErrLog("%s: udev_monitor_enable_receiving failed: rc=%d", __func__, rc);
|
|
|
bf3226 |
+ goto error;
|
|
|
bf3226 |
+ }
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ udev_mon->socket = udev_monitor_get_fd(udev_mon->mon);
|
|
|
bf3226 |
+ if (udev_mon->socket < 0) {
|
|
|
bf3226 |
+ ErrLog("%s: udev_monitor_get_fd failed", __func__);
|
|
|
bf3226 |
+ goto error;
|
|
|
bf3226 |
+ }
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ epoll_info_init(&udev_mon->ep_info, udev_mon_notify, NULL, udev_mon);
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ evt.events = EPOLLIN | EPOLLET;
|
|
|
bf3226 |
+ evt.data.ptr = &udev_mon->ep_info;
|
|
|
bf3226 |
+ rc = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, udev_mon->socket, &evt);
|
|
|
bf3226 |
+ if (rc != 0) {
|
|
|
bf3226 |
+ err = errno;
|
|
|
bf3226 |
+ ErrLog("%s: Failed add udev_mon socket %d to epoll, errno %d (%s).",
|
|
|
bf3226 |
+ __func__, udev_mon->socket, err, strerror(err));
|
|
|
bf3226 |
+ goto error;
|
|
|
bf3226 |
+ }
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ /* Epoll is edge triggered, thus try to receive once */
|
|
|
bf3226 |
+ rc = udev_mon_notify(EPOLLIN, udev_mon);
|
|
|
bf3226 |
+ if (rc != 0)
|
|
|
bf3226 |
+ goto error;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ return TRUE;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+error:
|
|
|
bf3226 |
+ udev_mon_term(udev_mon);
|
|
|
bf3226 |
+ return FALSE;
|
|
|
bf3226 |
+}
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+static int udev_mon_handle_device(struct udev_mon *udev_mon,
|
|
|
bf3226 |
+ struct udev_device *dev)
|
|
|
bf3226 |
+{
|
|
|
bf3226 |
+ const char *action, *devname, *devpath, *devtype, *dev_type_prop;
|
|
|
bf3226 |
+ unsigned int card, domain, dev_type;
|
|
|
bf3226 |
+ struct event_info *event;
|
|
|
bf3226 |
+ event_udev_apqn_data_t *apqn_data;
|
|
|
bf3226 |
+ int rc;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ UNUSED(udev_mon);
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ action = udev_device_get_action(dev);
|
|
|
bf3226 |
+ devname = udev_device_get_sysname(dev);
|
|
|
bf3226 |
+ devpath = udev_device_get_devpath(dev);
|
|
|
bf3226 |
+ devtype = udev_device_get_devtype(dev);
|
|
|
bf3226 |
+ dev_type_prop = udev_device_get_property_value(dev, UDEV_PROERTY_DEVTYPE);
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ if (action == NULL || devname == NULL || devpath == NULL ||
|
|
|
bf3226 |
+ devtype == NULL || dev_type_prop == NULL)
|
|
|
bf3226 |
+ return 0;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ DbgLog(DL3, "%s: Uevent: ACTION=%s DEVNAME=%s DEVPATH=%s DEVTYPE=%s "
|
|
|
bf3226 |
+ "DEV_TYPE=%s", __func__, action, devname, devpath, devtype,
|
|
|
bf3226 |
+ dev_type_prop);
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ /* We are only interested in bind and unbind events ... */
|
|
|
bf3226 |
+ if (strcmp(action, UDEV_ACTION_BIND) != 0 &&
|
|
|
bf3226 |
+ strcmp(action, UDEV_ACTION_UNBIND) != 0)
|
|
|
bf3226 |
+ return 0;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ /* ... for an APQN device */
|
|
|
bf3226 |
+ if (strcmp(devtype, UDEV_ACTION_DEVTYPE_APQN) != 0)
|
|
|
bf3226 |
+ return 0;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ if (sscanf(devname, "%x.%x", &card, &domain) != 2) {
|
|
|
bf3226 |
+ TraceLog("%s: failed to parse APQN from DEVNAME: %s", __func__, devname);
|
|
|
bf3226 |
+ return -EIO;
|
|
|
bf3226 |
+ }
|
|
|
bf3226 |
+ if (sscanf(dev_type_prop, "%x", &dev_type) != 1) {
|
|
|
bf3226 |
+ TraceLog("%s: failed to parse DEV_TYPE: %s", __func__, dev_type_prop);
|
|
|
bf3226 |
+ return -EIO;
|
|
|
bf3226 |
+ }
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ event = event_new(sizeof(event_udev_apqn_data_t), NULL);
|
|
|
bf3226 |
+ if (event == NULL) {
|
|
|
bf3226 |
+ ErrLog("%s: failed to allocate an event", __func__);
|
|
|
bf3226 |
+ return -ENOMEM;
|
|
|
bf3226 |
+ }
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ if (strcmp(udev_device_get_action(dev), UDEV_ACTION_BIND) == 0)
|
|
|
bf3226 |
+ event->event.type = EVENT_TYPE_APQN_ADD;
|
|
|
bf3226 |
+ else
|
|
|
bf3226 |
+ event->event.type = EVENT_TYPE_APQN_REMOVE;
|
|
|
bf3226 |
+ event->event.flags = EVENT_FLAGS_NONE;
|
|
|
bf3226 |
+ event->event.token_type = EVENT_TOK_TYPE_ALL;
|
|
|
bf3226 |
+ memset(event->event.token_label, ' ',
|
|
|
bf3226 |
+ sizeof(event->event.token_label));
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ apqn_data = (event_udev_apqn_data_t *)event->payload;
|
|
|
bf3226 |
+ apqn_data->card = card;
|
|
|
bf3226 |
+ apqn_data->domain = domain;
|
|
|
bf3226 |
+ apqn_data->device_type = dev_type;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ DbgLog(DL3, "%s: Event version: %u", __func__, event->event.version);
|
|
|
bf3226 |
+ DbgLog(DL3, "%s: Event type: 0x%08x", __func__, event->event.type);
|
|
|
bf3226 |
+ DbgLog(DL3, "%s: Event flags: 0x%08x", __func__, event->event.flags);
|
|
|
bf3226 |
+ DbgLog(DL3, "%s: Event token_type: 0x%08x", __func__,
|
|
|
bf3226 |
+ event->event.token_type);
|
|
|
bf3226 |
+ DbgLog(DL3, "%s: Event token_name: '%.32s'", __func__,
|
|
|
bf3226 |
+ event->event.token_label);
|
|
|
bf3226 |
+ DbgLog(DL3, "%s: Event process_id: %u", __func__, event->event.process_id);
|
|
|
bf3226 |
+ DbgLog(DL3, "%s: Event payload_len: %u", __func__,
|
|
|
bf3226 |
+ event->event.payload_len);
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ DbgLog(DL3, "%s: Payload: card: %u", __func__, apqn_data->card);
|
|
|
bf3226 |
+ DbgLog(DL3, "%s: Payload: domain: %u", __func__, apqn_data->domain);
|
|
|
bf3226 |
+ DbgLog(DL3, "%s: Payload: dev.type: %u", __func__, apqn_data->device_type);
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ rc = event_start_deliver(event);
|
|
|
bf3226 |
+ if (rc != 0) {
|
|
|
bf3226 |
+ if (rc == -ENOSPC) {
|
|
|
bf3226 |
+ /* Event limit reached, delay event delivery */
|
|
|
bf3226 |
+ udev_mon->delayed_event = event;
|
|
|
bf3226 |
+ return -ENOSPC;
|
|
|
bf3226 |
+ }
|
|
|
bf3226 |
+ event_free(event);
|
|
|
bf3226 |
+ return rc;
|
|
|
bf3226 |
+ }
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ return 0;
|
|
|
bf3226 |
+}
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+static int udev_mon_notify(int events, void *private)
|
|
|
bf3226 |
+{
|
|
|
bf3226 |
+ struct udev_mon *udev_mon = private;
|
|
|
bf3226 |
+ struct udev_device *dev;
|
|
|
bf3226 |
+ struct event_info *event;
|
|
|
bf3226 |
+ int rc;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ DbgLog(DL3, "%s: Epoll event on udev_mon socket %d: events: 0x%x",
|
|
|
bf3226 |
+ __func__, udev_mon->socket, events);
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ if (udev_mon->delayed_event != NULL) {
|
|
|
bf3226 |
+ /* Deliver delayed event first */
|
|
|
bf3226 |
+ event = udev_mon->delayed_event;
|
|
|
bf3226 |
+ udev_mon->delayed_event = NULL;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ rc = event_start_deliver(event);
|
|
|
bf3226 |
+ if (rc != 0) {
|
|
|
bf3226 |
+ if (rc == -ENOSPC) {
|
|
|
bf3226 |
+ /* Event limit reached, delay event delivery */
|
|
|
bf3226 |
+ udev_mon->delayed_event = event;
|
|
|
bf3226 |
+ return 0;
|
|
|
bf3226 |
+ }
|
|
|
bf3226 |
+ event_free(event);
|
|
|
bf3226 |
+ return rc;
|
|
|
bf3226 |
+ }
|
|
|
bf3226 |
+ }
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ while (1) {
|
|
|
bf3226 |
+ dev = udev_monitor_receive_device(udev_mon->mon);
|
|
|
bf3226 |
+ if (dev == NULL)
|
|
|
bf3226 |
+ break; /* this is just like EWOULDBLOCK */
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ rc = udev_mon_handle_device(udev_mon, dev);
|
|
|
bf3226 |
+ if (rc != 0)
|
|
|
bf3226 |
+ TraceLog("%s: udev_mon_handle_device failed, rc: %d", __func__, rc);
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ udev_device_unref(dev);
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ /* If event limit reached, stop receiving more events */
|
|
|
bf3226 |
+ if (rc == -ENOSPC)
|
|
|
bf3226 |
+ break;
|
|
|
bf3226 |
+ };
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ return 0;
|
|
|
bf3226 |
+}
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+static void udev_mon_term(struct udev_mon *udev_mon)
|
|
|
bf3226 |
+{
|
|
|
bf3226 |
+ if (udev_mon == NULL)
|
|
|
bf3226 |
+ return;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ epoll_ctl(epoll_fd, EPOLL_CTL_DEL, udev_mon->socket, NULL);
|
|
|
bf3226 |
+ if (udev_mon->udev != NULL)
|
|
|
bf3226 |
+ udev_unref(udev_mon->udev);
|
|
|
bf3226 |
+ if (udev_mon->mon != NULL)
|
|
|
bf3226 |
+ udev_monitor_unref(udev_mon->mon);
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ if (udev_mon->delayed_event != NULL)
|
|
|
bf3226 |
+ event_free(udev_mon->delayed_event);
|
|
|
bf3226 |
+}
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+#endif
|
|
|
bf3226 |
+
|
|
|
bf3226 |
int init_socket_data(Slot_Mgr_Socket_t *socketData)
|
|
|
bf3226 |
{
|
|
|
bf3226 |
unsigned int processed = 0;
|
|
|
bf3226 |
@@ -106,7 +1642,7 @@ int init_socket_data(Slot_Mgr_Socket_t *socketData)
|
|
|
bf3226 |
|
|
|
bf3226 |
/* check that we read in correct amount of slots */
|
|
|
bf3226 |
if (processed != NumberSlotsInDB) {
|
|
|
bf3226 |
- ErrLog("Failed to populate slot info.\n");
|
|
|
bf3226 |
+ ErrLog("%s: Failed to populate slot info.", __func__);
|
|
|
bf3226 |
return FALSE;
|
|
|
bf3226 |
}
|
|
|
bf3226 |
|
|
|
bf3226 |
@@ -115,72 +1651,277 @@ int init_socket_data(Slot_Mgr_Socket_t *socketData)
|
|
|
bf3226 |
|
|
|
bf3226 |
int socket_connection_handler(int timeout_secs)
|
|
|
bf3226 |
{
|
|
|
bf3226 |
- int returnVal;
|
|
|
bf3226 |
- fd_set set;
|
|
|
bf3226 |
- struct timeval timeout;
|
|
|
bf3226 |
-
|
|
|
bf3226 |
- FD_ZERO(&set);
|
|
|
bf3226 |
- FD_SET(proc_listener_socket, &set);
|
|
|
bf3226 |
+ struct epoll_event events[MAX_EPOLL_EVENTS];
|
|
|
bf3226 |
+ int num_events, i, rc, err;
|
|
|
bf3226 |
+ struct epoll_info *info;
|
|
|
bf3226 |
|
|
|
bf3226 |
- timeout.tv_sec = timeout_secs;
|
|
|
bf3226 |
- timeout.tv_usec = 0;
|
|
|
bf3226 |
-
|
|
|
bf3226 |
- returnVal = select(proc_listener_socket + 1, &set, NULL, NULL, &timeout);
|
|
|
bf3226 |
- if (returnVal == -1) {
|
|
|
bf3226 |
- ErrLog("select failed on socket connection, errno 0x%X.", errno);
|
|
|
bf3226 |
- return FALSE;
|
|
|
bf3226 |
- } else if (returnVal == 0) {
|
|
|
bf3226 |
- // select call timed out, return
|
|
|
bf3226 |
- return FALSE;
|
|
|
bf3226 |
- } else {
|
|
|
bf3226 |
- struct sockaddr_un address;
|
|
|
bf3226 |
- socklen_t address_length = sizeof(address);
|
|
|
bf3226 |
-
|
|
|
bf3226 |
- int connectionfd = accept(proc_listener_socket,
|
|
|
bf3226 |
- (struct sockaddr *) &address,
|
|
|
bf3226 |
- &address_length);
|
|
|
bf3226 |
- if (connectionfd < 0) {
|
|
|
bf3226 |
- if (errno != EAGAIN && errno != EWOULDBLOCK) {
|
|
|
bf3226 |
- /* These errors are allowed since
|
|
|
bf3226 |
- * socket is non-blocking
|
|
|
bf3226 |
- */
|
|
|
bf3226 |
- ErrLog("Failed to accept socket connection, errno 0x%X.",
|
|
|
bf3226 |
- errno);
|
|
|
bf3226 |
- }
|
|
|
bf3226 |
+ do {
|
|
|
bf3226 |
+ num_events = epoll_wait(epoll_fd, events, MAX_EPOLL_EVENTS,
|
|
|
bf3226 |
+ timeout_secs * 1000);
|
|
|
bf3226 |
+ if (num_events < 0) {
|
|
|
bf3226 |
+ err = errno;
|
|
|
bf3226 |
+ if (err == EINTR)
|
|
|
bf3226 |
+ continue;
|
|
|
bf3226 |
+ ErrLog("%s: epoll_wait failed, errno %d (%s).", __func__, err,
|
|
|
bf3226 |
+ strerror(err));
|
|
|
bf3226 |
return FALSE;
|
|
|
bf3226 |
}
|
|
|
bf3226 |
|
|
|
bf3226 |
- DbgLog(DL0, "Accepted connection from process: socket: %d",
|
|
|
bf3226 |
- connectionfd);
|
|
|
bf3226 |
+ /*
|
|
|
bf3226 |
+ * Inc ref count of all epoll_infos returned by epoll before handling
|
|
|
bf3226 |
+ * any of them via notify. The notify callback may hangup any of
|
|
|
bf3226 |
+ * the connections associated with the returned epoll_infos, and we
|
|
|
bf3226 |
+ * need to avoid them getting freed before we all handled them.
|
|
|
bf3226 |
+ */
|
|
|
bf3226 |
+ for (i = 0; i < num_events; i++)
|
|
|
bf3226 |
+ epoll_info_get(events[i].data.ptr);
|
|
|
bf3226 |
|
|
|
bf3226 |
- if (write(connectionfd, &socketData, sizeof(socketData)) !=
|
|
|
bf3226 |
- sizeof(socketData)) {
|
|
|
bf3226 |
- ErrLog("Failed to write socket data, errno 0x%X.", errno);
|
|
|
bf3226 |
- close(connectionfd);
|
|
|
bf3226 |
- return FALSE;
|
|
|
bf3226 |
+ for (i = 0; i < num_events; i++) {
|
|
|
bf3226 |
+ info = events[i].data.ptr;
|
|
|
bf3226 |
+ if (info == NULL || info->notify == NULL)
|
|
|
bf3226 |
+ continue;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ rc = info->notify(events[i].events, info->private);
|
|
|
bf3226 |
+ if (rc != 0)
|
|
|
bf3226 |
+ TraceLog("%s: notify callback failed, rc: %d", __func__, rc);
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ epoll_info_put(info);
|
|
|
bf3226 |
}
|
|
|
bf3226 |
- close(connectionfd);
|
|
|
bf3226 |
- return TRUE;
|
|
|
bf3226 |
- }
|
|
|
bf3226 |
+ } while (num_events > 0 && rc == 0); /* num_events = 0: timeout */
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ return TRUE;
|
|
|
bf3226 |
}
|
|
|
bf3226 |
|
|
|
bf3226 |
int init_socket_server()
|
|
|
bf3226 |
{
|
|
|
bf3226 |
- proc_listener_socket = create_listener_socket(PROC_SOCKET_FILE_PATH);
|
|
|
bf3226 |
- if (proc_listener_socket < 0)
|
|
|
bf3226 |
+ int err;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ epoll_fd = epoll_create1(0);
|
|
|
bf3226 |
+ if (epoll_fd < 0) {
|
|
|
bf3226 |
+ err = errno;
|
|
|
bf3226 |
+ ErrLog("%s: Failed to open epoll socket, errno %d (%s).", __func__, err,
|
|
|
bf3226 |
+ strerror(err));
|
|
|
bf3226 |
+ return FALSE;
|
|
|
bf3226 |
+ }
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ if (!listener_create(PROC_SOCKET_FILE_PATH, &proc_listener,
|
|
|
bf3226 |
+ proc_new_conn, NUMBER_PROCESSES_ALLOWED)) {
|
|
|
bf3226 |
+ term_socket_server();
|
|
|
bf3226 |
+ return FALSE;
|
|
|
bf3226 |
+ }
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ if (!listener_create(ADMIN_SOCKET_FILE_PATH, &admin_listener,
|
|
|
bf3226 |
+ admin_new_conn, NUMBER_ADMINS_ALLOWED)) {
|
|
|
bf3226 |
+ term_socket_server();
|
|
|
bf3226 |
return FALSE;
|
|
|
bf3226 |
+ }
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+#ifdef WITH_LIBUDEV
|
|
|
bf3226 |
+ if (!udev_mon_init(UDEV_SUBSYSTEM_AP, &udev_mon)) {
|
|
|
bf3226 |
+ term_socket_server();
|
|
|
bf3226 |
+ return FALSE;
|
|
|
bf3226 |
+ }
|
|
|
bf3226 |
+#endif
|
|
|
bf3226 |
|
|
|
bf3226 |
- DbgLog(DL0, "Socket server started");
|
|
|
bf3226 |
+ DbgLog(DL0, "%s: Socket server started", __func__);
|
|
|
bf3226 |
|
|
|
bf3226 |
return TRUE;
|
|
|
bf3226 |
}
|
|
|
bf3226 |
|
|
|
bf3226 |
int term_socket_server()
|
|
|
bf3226 |
{
|
|
|
bf3226 |
- if (proc_listener_socket >= 0)
|
|
|
bf3226 |
- close_listener_socket(proc_listener_socket, PROC_SOCKET_FILE_PATH);
|
|
|
bf3226 |
+ DL_NODE *node, *next;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+#ifdef WITH_LIBUDEV
|
|
|
bf3226 |
+ udev_mon_term(&udev_mon);
|
|
|
bf3226 |
+#endif
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ listener_term(&proc_listener);
|
|
|
bf3226 |
+ listener_term(&admin_listener);
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ node = dlist_get_first(proc_connections);
|
|
|
bf3226 |
+ while (node != NULL) {
|
|
|
bf3226 |
+ next = dlist_next(node);
|
|
|
bf3226 |
+ proc_hangup(node->data);
|
|
|
bf3226 |
+ node = next;
|
|
|
bf3226 |
+ }
|
|
|
bf3226 |
+ dlist_purge(proc_connections);
|
|
|
bf3226 |
|
|
|
bf3226 |
- DbgLog(DL0, "Socket server stopped");
|
|
|
bf3226 |
+ node = dlist_get_first(admin_connections);
|
|
|
bf3226 |
+ while (node != NULL) {
|
|
|
bf3226 |
+ next = dlist_next(node);
|
|
|
bf3226 |
+ admin_hangup(node->data);
|
|
|
bf3226 |
+ node = next;
|
|
|
bf3226 |
+ }
|
|
|
bf3226 |
+ dlist_purge(admin_connections);
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ node = dlist_get_first(pending_events);
|
|
|
bf3226 |
+ while (node != NULL) {
|
|
|
bf3226 |
+ next = dlist_next(node);
|
|
|
bf3226 |
+ event_free((struct event_info *)node->data);
|
|
|
bf3226 |
+ node = next;
|
|
|
bf3226 |
+ }
|
|
|
bf3226 |
+ dlist_purge(pending_events);
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ if (epoll_fd >= 0)
|
|
|
bf3226 |
+ close(epoll_fd);
|
|
|
bf3226 |
+ epoll_fd = -1;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ DbgLog(DL0, "%s: Socket server stopped", __func__);
|
|
|
bf3226 |
|
|
|
bf3226 |
return TRUE;
|
|
|
bf3226 |
}
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+#ifdef DEV
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+static void dump_listener(struct listener_info *listener)
|
|
|
bf3226 |
+{
|
|
|
bf3226 |
+ DbgLog(DL0, " socket: %d", listener->socket);
|
|
|
bf3226 |
+ DbgLog(DL0, " file_path: %s", listener->file_path);
|
|
|
bf3226 |
+ DbgLog(DL0, " ep_info.ref_count: %lu", listener->ep_info.ref_count);
|
|
|
bf3226 |
+ DbgLog(DL0, " num_clients: %lu", listener->num_clients);
|
|
|
bf3226 |
+ DbgLog(DL0, " max_num_clients: %lu", listener->max_num_clients);
|
|
|
bf3226 |
+}
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+static void dump_event_msg(event_msg_t *event, int indent)
|
|
|
bf3226 |
+{
|
|
|
bf3226 |
+ DbgLog(DL0, "%*sevent version: %u", indent, "", event->version);
|
|
|
bf3226 |
+ DbgLog(DL0, "%*sevent type: %08x", indent, "", event->type);
|
|
|
bf3226 |
+ DbgLog(DL0, "%*sevent flags: %08x", indent, "", event->flags);
|
|
|
bf3226 |
+ DbgLog(DL0, "%*sevent token_type: %08x", indent, "", event->token_type);
|
|
|
bf3226 |
+ DbgLog(DL0, "%*sevent token_label: '%.32s'", indent, "", event->token_label);
|
|
|
bf3226 |
+ DbgLog(DL0, "%*sevent process_id: %lu", indent, "", event->process_id);
|
|
|
bf3226 |
+ DbgLog(DL0, "%*sevent payload_len: %u", indent, "", event->payload_len);
|
|
|
bf3226 |
+}
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+static void dump_event_reply(event_reply_t *reply, int indent)
|
|
|
bf3226 |
+{
|
|
|
bf3226 |
+ DbgLog(DL0, "%*sreply version: %u", indent, "", reply->version);
|
|
|
bf3226 |
+ DbgLog(DL0, "%*sreply positive_replies: %u", indent, "", reply->positive_replies);
|
|
|
bf3226 |
+ DbgLog(DL0, "%*sreply negative_replies: %u", indent, "", reply->negative_replies);
|
|
|
bf3226 |
+ DbgLog(DL0, "%*sreply nothandled_replies: %u", indent, "", reply->nothandled_replies);
|
|
|
bf3226 |
+}
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+static void dump_event_info(struct event_info *event, int indent)
|
|
|
bf3226 |
+{
|
|
|
bf3226 |
+ dump_event_msg(&event->event, indent);
|
|
|
bf3226 |
+ dump_event_reply(&event->reply, indent);
|
|
|
bf3226 |
+ DbgLog(DL0, "%*sproc_ref_count: %lu", indent, "", event->proc_ref_count);
|
|
|
bf3226 |
+ if (event->admin_ref != NULL)
|
|
|
bf3226 |
+ DbgLog(DL0, "%*sadmin_ref: %p", indent, "", event->admin_ref);
|
|
|
bf3226 |
+ else
|
|
|
bf3226 |
+ DbgLog(DL0, "%*sadmin_ref: None", indent, "");
|
|
|
bf3226 |
+}
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+static void dump_proc_conn(struct proc_conn_info *proc_conn)
|
|
|
bf3226 |
+{
|
|
|
bf3226 |
+ DL_NODE *node;
|
|
|
bf3226 |
+ unsigned long i;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ DbgLog(DL0, " socket: %d", proc_conn->client_info.socket);
|
|
|
bf3226 |
+ DbgLog(DL0, " state: %d", proc_conn->state);
|
|
|
bf3226 |
+ DbgLog(DL0, " ref-count: %lu", proc_conn->client_info.ep_info.ref_count);
|
|
|
bf3226 |
+ DbgLog(DL0, " xfer state: %d", proc_conn->client_info.xfer_state);
|
|
|
bf3226 |
+ DbgLog(DL0, " xfer size: %d", proc_conn->client_info.xfer_size);
|
|
|
bf3226 |
+ DbgLog(DL0, " xfer offset: %d", proc_conn->client_info.xfer_offset);
|
|
|
bf3226 |
+ DbgLog(DL0, " pending events:");
|
|
|
bf3226 |
+ node = dlist_get_first(proc_conn->events);
|
|
|
bf3226 |
+ i = 1;
|
|
|
bf3226 |
+ while (node != NULL) {
|
|
|
bf3226 |
+ DbgLog(DL0, " event %lu (%p):", i, node->data);
|
|
|
bf3226 |
+ dump_event_info(node->data, 10);
|
|
|
bf3226 |
+ node = dlist_next(node);
|
|
|
bf3226 |
+ i++;
|
|
|
bf3226 |
+ }
|
|
|
bf3226 |
+ if (proc_conn->event != NULL) {
|
|
|
bf3226 |
+ DbgLog(DL0, " current event:");
|
|
|
bf3226 |
+ dump_event_info(proc_conn->event, 8);
|
|
|
bf3226 |
+ DbgLog(DL0, " current reply:");
|
|
|
bf3226 |
+ dump_event_reply(&proc_conn->reply, 8);
|
|
|
bf3226 |
+ } else {
|
|
|
bf3226 |
+ DbgLog(DL0, " current event: none");
|
|
|
bf3226 |
+ }
|
|
|
bf3226 |
+}
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+static void dump_admin_conn(struct admin_conn_info *admin_conn)
|
|
|
bf3226 |
+{
|
|
|
bf3226 |
+ DbgLog(DL0, " socket: %d", admin_conn->client_info.socket);
|
|
|
bf3226 |
+ DbgLog(DL0, " state: %d", admin_conn->state);
|
|
|
bf3226 |
+ DbgLog(DL0, " ref-count: %lu", admin_conn->client_info.ep_info.ref_count);
|
|
|
bf3226 |
+ DbgLog(DL0, " xfer state: %d", admin_conn->client_info.xfer_state);
|
|
|
bf3226 |
+ DbgLog(DL0, " xfer size: %d", admin_conn->client_info.xfer_size);
|
|
|
bf3226 |
+ DbgLog(DL0, " xfer offset: %d", admin_conn->client_info.xfer_offset);
|
|
|
bf3226 |
+ if (admin_conn->event != NULL) {
|
|
|
bf3226 |
+ DbgLog(DL0, " current event (%p):", admin_conn->event);
|
|
|
bf3226 |
+ dump_event_info(admin_conn->event, 8);
|
|
|
bf3226 |
+ } else {
|
|
|
bf3226 |
+ DbgLog(DL0, " current event: none");
|
|
|
bf3226 |
+ }
|
|
|
bf3226 |
+}
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+#ifdef WITH_LIBUDEV
|
|
|
bf3226 |
+void dump_udev_mon(struct udev_mon *udev_mon)
|
|
|
bf3226 |
+{
|
|
|
bf3226 |
+ DbgLog(DL0, " socket: %d", udev_mon->socket);
|
|
|
bf3226 |
+ DbgLog(DL0, " udev: %p", udev_mon->udev);
|
|
|
bf3226 |
+ DbgLog(DL0, " mon: %p", udev_mon->mon);
|
|
|
bf3226 |
+ DbgLog(DL0, " ep_info.ref_count: %lu", udev_mon->ep_info.ref_count);
|
|
|
bf3226 |
+ if (udev_mon->delayed_event != NULL) {
|
|
|
bf3226 |
+ DbgLog(DL0, " delayed_event (%p):", udev_mon->delayed_event);
|
|
|
bf3226 |
+ dump_event_info(udev_mon->delayed_event, 6);
|
|
|
bf3226 |
+ } else {
|
|
|
bf3226 |
+ DbgLog(DL0, " delayed_event: node");
|
|
|
bf3226 |
+ }
|
|
|
bf3226 |
+}
|
|
|
bf3226 |
+#endif
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+void dump_socket_handler()
|
|
|
bf3226 |
+{
|
|
|
bf3226 |
+ DL_NODE *node;
|
|
|
bf3226 |
+ unsigned long i;
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ DbgLog(DL0, "%s: Dump of socket handler data:", __func__);
|
|
|
bf3226 |
+ DbgLog(DL0, " epoll_fd: %d", epoll_fd);
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ DbgLog(DL0, " proc_listener (%p): ", &proc_listener);
|
|
|
bf3226 |
+ dump_listener(&proc_listener);
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ DbgLog(DL0, " proc_connections: ");
|
|
|
bf3226 |
+ node = dlist_get_first(proc_connections);
|
|
|
bf3226 |
+ i = 1;
|
|
|
bf3226 |
+ while (node != NULL) {
|
|
|
bf3226 |
+ DbgLog(DL0, " proc_connection %lu (%p): ", i, node->data);
|
|
|
bf3226 |
+ dump_proc_conn(node->data);
|
|
|
bf3226 |
+ i++;
|
|
|
bf3226 |
+ node = dlist_next(node);
|
|
|
bf3226 |
+ }
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ DbgLog(DL0, " admin_listener (%p): ", &admin_listener);
|
|
|
bf3226 |
+ dump_listener(&admin_listener);
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ DbgLog(DL0, " admin_connections: ");
|
|
|
bf3226 |
+ node = dlist_get_first(admin_connections);
|
|
|
bf3226 |
+ i = 1;
|
|
|
bf3226 |
+ while (node != NULL) {
|
|
|
bf3226 |
+ DbgLog(DL0, " admin_connection %lu (%p): ", i, node->data);
|
|
|
bf3226 |
+ dump_admin_conn(node->data);
|
|
|
bf3226 |
+ i++;
|
|
|
bf3226 |
+ node = dlist_next(node);
|
|
|
bf3226 |
+ }
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+#ifdef WITH_LIBUDEV
|
|
|
bf3226 |
+ DbgLog(DL0, " udev_mon (%p): ", &udev_mon);
|
|
|
bf3226 |
+ dump_udev_mon(&udev_mon);
|
|
|
bf3226 |
+#endif
|
|
|
bf3226 |
+
|
|
|
bf3226 |
+ DbgLog(DL0, " pending events (%lu): ", pending_events_count);
|
|
|
bf3226 |
+ node = dlist_get_first(pending_events);
|
|
|
bf3226 |
+ i = 1;
|
|
|
bf3226 |
+ while (node != NULL) {
|
|
|
bf3226 |
+ DbgLog(DL0, " event %lu (%p): ", i, node->data);
|
|
|
bf3226 |
+ dump_event_info(node->data, 6);
|
|
|
bf3226 |
+ i++;
|
|
|
bf3226 |
+ node = dlist_next(node);
|
|
|
bf3226 |
+ }
|
|
|
bf3226 |
+}
|
|
|
bf3226 |
+#endif
|