From 8f5e3dc0f3134ea2fdaffd13a16464b1a2f2ecd4 Mon Sep 17 00:00:00 2001 From: Martin Ejdestig Date: Mon, 14 Dec 2020 01:43:52 +0100 Subject: Add support for logging with VSOCK (#255) For more information about VSOCK, see "man vsock" ( https://man7.org/linux/man-pages/man7/vsock.7.html ). Makes it possible for processes in a virtual machine to log directly in DLT running on host without setting up a network connection between guest and host. It is also probably more efficient. Have not done any performance measurements (not main reason for patch), but no forwarding is required as when running DLT in a multi-node setup. When building dlt-daemon for host, WITH_DLT_DAEMON_VSOCK_IPC should be enabled for daemon to listen on incoming VSOCK requests. Local communication method between applications and daemon is still determined with DLT_IPC. When building for guest, WITH_DLT_LIB_VSOCK_IPC should be enabled and DLT_IPC will be ignored, which will make libdlt open a VSOCK socket to the deamon for logging messages. VSOCK can be tested without a virtual machine. Since VMADDR_CID_HOST is used by libdlt when connecting, see vsock man page, clients can be run on host to test VSOCK communication. Some modifications has been done to be able to handle logging through FIFO pipe and socket in the same build of dlt-daemon: - dlt_receiver_init/free_unix_socket() is renamed to dlt_receiver_init/free_global_buffer() and used for FIFO as well. Also fixes memory leak since dlt_receiver_free_unix_socket() was used regardless of whether DLT_USE_UNIX_SOCKET was defined or not. - Pass type to dlt_receiver_init() instead of dlt_receiver_receive(). And remove preprocessor conditionals for handling DLT_CONNECTION_APP_MSG in dlt_daemon_process_user_messages(). Also fixes wrong enum type being passed to dlt_receiver_receive() in dlt_client.c (DltClient::mode was used as a DltReceiverType enum but it is a DltClientMode enum). - Add a flag to DltDaemonApplication to indicate whether file descriptor is "owned" by the DltDaemonApplication or not. When dlt_daemon_application_add() is called due to message received on a socket, fd is passed as an argument (app does not own fd). For FIFO, a per application FIFO is opened (app owns the fd). Also fixes so that user handle is reset for both application and all its contexts when resetting any. Prevents fd from being used by accident after it has been closed. dlt_mkdir_recursive() is moved to src/daemon since it is only used in the daemon. Minimizes use of DLT_USE_UNIX_SOCKET_IPC. Other bugfixes: - Call DLT_SEM_FREE() if setting socket to O_NONBLOCK fails in src/lib/dlt_user.c:dlt_initialize_socket_connection(). - Close socket if dlt_receiver_init() fails in src/lib/dlt_user.c:dlt_initialize_socket_connection(). Signed-off-by: Martin Ejdestig --- include/dlt/dlt_common.h | 24 +++++++++--------------- include/dlt/dlt_types.h | 2 +- include/dlt/dlt_user.h.in | 2 +- 3 files changed, 11 insertions(+), 17 deletions(-) (limited to 'include/dlt') diff --git a/include/dlt/dlt_common.h b/include/dlt/dlt_common.h index 9bbd544..71b9c10 100644 --- a/include/dlt/dlt_common.h +++ b/include/dlt/dlt_common.h @@ -423,7 +423,7 @@ extern const char dltSerialHeader[DLT_ID_SIZE]; */ extern char dltSerialHeaderChar[DLT_ID_SIZE]; -#ifndef DLT_USE_UNIX_SOCKET_IPC +#if defined DLT_DAEMON_USE_FIFO_IPC || defined DLT_LIB_USE_FIFO_IPC /** * The common base-path of the dlt-daemon-fifo and application-generated fifos */ @@ -779,6 +779,7 @@ typedef struct char *buf; /**< pointer to position within receiver buffer */ char *backup_buf; /** pointer to the buffer with partial messages if any **/ int fd; /**< connection handle */ + DltReceiverType type; /**< type of connection handle */ int32_t buffersize; /**< size of receiver buffer */ struct sockaddr_in addr; /**< socket address information */ } DltReceiver; @@ -1157,7 +1158,7 @@ DltReturnValue dlt_file_free(DltFile *file, int verbose); * @param filename the filename */ void dlt_log_set_filename(const char *filename); -#ifndef DLT_USE_UNIX_SOCKET_IPC +#if defined DLT_DAEMON_USE_FIFO_IPC || defined DLT_LIB_USE_FIFO_IPC /** * Set FIFO base direction * @param pipe_dir the pipe direction @@ -1211,10 +1212,11 @@ void dlt_log_free(void); * Initialising a dlt receiver structure * @param receiver pointer to dlt receiver structure * @param _fd handle to file/socket/fifo, fram which the data should be received + * @param type specify whether received data is from socket or file/fifo * @param _buffersize size of data buffer for storing the received data * @return negative value if there was an error */ -DltReturnValue dlt_receiver_init(DltReceiver *receiver, int _fd, int _buffersize); +DltReturnValue dlt_receiver_init(DltReceiver *receiver, int _fd, DltReceiverType type, int _buffersize); /** * De-Initialize a dlt receiver structure * @param receiver pointer to dlt receiver structure @@ -1225,23 +1227,23 @@ DltReturnValue dlt_receiver_free(DltReceiver *receiver); * Initialising a dlt receiver structure * @param receiver pointer to dlt receiver structure * @param fd handle to file/socket/fifo, fram which the data should be received + * @param type specify whether received data is from socket or file/fifo * @param buffer data buffer for storing the received data * @return negative value if there was an error and zero if success */ -DltReturnValue dlt_receiver_init_unix_socket(DltReceiver *receiver, int fd, char **buffer); +DltReturnValue dlt_receiver_init_global_buffer(DltReceiver *receiver, int fd, DltReceiverType type, char **buffer); /** * De-Initialize a dlt receiver structure * @param receiver pointer to dlt receiver structure * @return negative value if there was an error and zero if success */ -DltReturnValue dlt_receiver_free_unix_socket(DltReceiver *receiver); +DltReturnValue dlt_receiver_free_global_buffer(DltReceiver *receiver); /** * Receive data from socket or file/fifo using the dlt receiver structure * @param receiver pointer to dlt receiver structure - * @param from_src specify whether received data is from socket or file/fifo * @return number of received bytes or negative value if there was an error */ -int dlt_receiver_receive(DltReceiver *receiver, DltReceiverType from_src); +int dlt_receiver_receive(DltReceiver *receiver); /** * Remove a specific size of bytes from the received data * @param receiver pointer to dlt receiver structure @@ -1616,14 +1618,6 @@ void dlt_getloginfo_conv_ascii_to_id(char *rp, int *rp_count, char *wp, int len) */ void dlt_hex_ascii_to_binary(const char *ptr, uint8_t *binary, int *size); -# ifndef DLT_USE_UNIX_SOCKET_IPC -/** - * Create the specified path, recursive if necessary - * behaves like calling mkdir -p \ on the console - */ -int dlt_mkdir_recursive(const char *dir); -# endif - # ifdef __cplusplus } # endif diff --git a/include/dlt/dlt_types.h b/include/dlt/dlt_types.h index 0047233..9943e31 100644 --- a/include/dlt/dlt_types.h +++ b/include/dlt/dlt_types.h @@ -176,7 +176,7 @@ typedef enum typedef float float32_t; typedef double float64_t; -#ifdef DLT_USE_UNIX_SOCKET_IPC +#if defined DLT_LIB_USE_UNIX_SOCKET_IPC || defined DLT_LIB_USE_VSOCK_IPC /** * Definition Library connection state */ diff --git a/include/dlt/dlt_user.h.in b/include/dlt/dlt_user.h.in index e4b6569..473575b 100644 --- a/include/dlt/dlt_user.h.in +++ b/include/dlt/dlt_user.h.in @@ -253,7 +253,7 @@ typedef struct int corrupt_message_size; int16_t corrupt_message_size_size; # endif -# ifdef DLT_USE_UNIX_SOCKET_IPC +# if defined DLT_LIB_USE_UNIX_SOCKET_IPC || defined DLT_LIB_USE_VSOCK_IPC DltUserConnectionState connection_state; # endif uint16_t log_buf_len; /**< length of message buffer, by default: DLT_USER_BUF_MAX_SIZE */ -- cgit v1.2.1