Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dlt-daemon: create sockets using "android way" #333

Merged
merged 1 commit into from
Oct 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Android.bp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ cc_defaults {
"-DDLT_DAEMON_USE_UNIX_SOCKET_IPC",
"-DDLT_LIB_USE_UNIX_SOCKET_IPC",
"-DCONFIGURATION_FILES_DIR=\"/vendor/etc\"",
"-DDLT_USER_IPC_PATH=\"/data/local/tmp\"",
"-DDLT_USER_IPC_PATH=\"/dev/socket\"",
] + [
"-Wno-unused-parameter",
"-W",
Expand Down
143 changes: 105 additions & 38 deletions src/daemon/dlt-daemon.c
Original file line number Diff line number Diff line change
Expand Up @@ -1436,12 +1436,111 @@ static int dlt_daemon_init_vsock(DltDaemonLocal *daemon_local)
}
#endif

#ifdef DLT_DAEMON_USE_UNIX_SOCKET_IPC
static DltReturnValue dlt_daemon_init_app_socket(DltDaemonLocal *daemon_local)
{
/* socket access permission set to srw-rw-rw- (666) */
int mask = S_IXUSR | S_IXGRP | S_IXOTH;
DltReturnValue ret = DLT_RETURN_OK;
int fd = -1;

if (daemon_local == NULL) {
dlt_vlog(LOG_ERR, "%s: Invalid function parameters\n", __func__);
return DLT_RETURN_ERROR;
}

#ifdef ANDROID
/* on android if we want to use security contexts on Unix sockets,
* they should be created by init (see dlt-daemon.rc in src/daemon)
* and recovered through the below API */
ret = dlt_daemon_unix_android_get_socket(&fd, daemon_local->flags.appSockPath);
if (ret < DLT_RETURN_OK) {
/* we failed to get app socket created by init.
* To avoid blocking users to launch dlt-daemon only through
* init on android (e.g: by hand for debugging purpose), try to
* create app socket by ourselves */
ret = dlt_daemon_unix_socket_open(&fd,
daemon_local->flags.appSockPath,
SOCK_STREAM,
mask);
}
#else
ret = dlt_daemon_unix_socket_open(&fd,
daemon_local->flags.appSockPath,
SOCK_STREAM,
mask);
#endif
if (ret == DLT_RETURN_OK) {
if (dlt_connection_create(daemon_local,
&daemon_local->pEvent,
fd,
POLLIN,
DLT_CONNECTION_APP_CONNECT)) {
dlt_log(LOG_CRIT, "Could not create connection for app socket.\n");
return DLT_RETURN_ERROR;
}
}
else {
dlt_log(LOG_CRIT, "Could not create and open app socket.\n");
return DLT_RETURN_ERROR;
}

return ret;
}
#endif

static DltReturnValue dlt_daemon_initialize_control_socket(DltDaemonLocal *daemon_local)
{
/* socket access permission set to srw-rw---- (660) */
int mask = S_IXUSR | S_IXGRP | S_IROTH | S_IWOTH | S_IXOTH;
DltReturnValue ret = DLT_RETURN_OK;
int fd = -1;

if (daemon_local == NULL) {
dlt_vlog(LOG_ERR, "%s: Invalid function parameters\n", __func__);
return -1;
}

#ifdef ANDROID
/* on android if we want to use security contexts on Unix sockets,
* they should be created by init (see dlt-daemon.rc in src/daemon)
* and recovered through the below API */
ret = dlt_daemon_unix_android_get_socket(&fd, daemon_local->flags.ctrlSockPath);
if (ret < DLT_RETURN_OK) {
/* we failed to get app socket created by init.
* To avoid blocking users to launch dlt-daemon only through
* init on android (e.g by hand for debugging purpose), try to
* create app socket by ourselves */
ret = dlt_daemon_unix_socket_open(&fd,
daemon_local->flags.ctrlSockPath,
SOCK_STREAM,
mask);
}
#else
ret = dlt_daemon_unix_socket_open(&fd,
daemon_local->flags.ctrlSockPath,
SOCK_STREAM,
mask);
#endif
if (ret == DLT_RETURN_OK) {
if (dlt_connection_create(daemon_local,
&daemon_local->pEvent,
fd,
POLLIN,
DLT_CONNECTION_CONTROL_CONNECT) < DLT_RETURN_OK) {
dlt_log(LOG_ERR, "Could not initialize control socket.\n");
ret = DLT_RETURN_ERROR;
}
}

return ret;
}

int dlt_daemon_local_connection_init(DltDaemon *daemon,
DltDaemonLocal *daemon_local,
int verbose)
{
int fd = -1;
int mask = 0;

PRINT_FUNCTION_VERBOSE(verbose);

Expand All @@ -1453,25 +1552,9 @@ int dlt_daemon_local_connection_init(DltDaemon *daemon,
DltBindAddress_t *head = daemon_local->flags.ipNodes;

#ifdef DLT_DAEMON_USE_UNIX_SOCKET_IPC
/* create and open socket to receive incoming connections from user application
* socket access permission set to srw-rw-rw- (666) */
mask = S_IXUSR | S_IXGRP | S_IXOTH;

if (dlt_daemon_unix_socket_open(&fd,
daemon_local->flags.appSockPath,
SOCK_STREAM,
mask) == DLT_RETURN_OK) {
if (dlt_connection_create(daemon_local,
&daemon_local->pEvent,
fd,
POLLIN,
DLT_CONNECTION_APP_CONNECT)) {
dlt_log(LOG_CRIT, "Could not initialize app socket.\n");
return DLT_RETURN_ERROR;
}
}
else {
dlt_log(LOG_CRIT, "Could not initialize app socket.\n");
/* create and open socket to receive incoming connections from user application */
if (dlt_daemon_init_app_socket(daemon_local) < DLT_RETURN_OK) {
dlt_log(LOG_ERR, "Unable to initialize app socket.\n");
return DLT_RETURN_ERROR;
}

Expand Down Expand Up @@ -1548,24 +1631,8 @@ int dlt_daemon_local_connection_init(DltDaemon *daemon,
#endif

/* create and open unix socket to receive incoming connections from
* control application
* socket access permission set to srw-rw---- (660) */
mask = S_IXUSR | S_IXGRP | S_IROTH | S_IWOTH | S_IXOTH;

if (dlt_daemon_unix_socket_open(&fd,
daemon_local->flags.ctrlSockPath,
SOCK_STREAM,
mask) == DLT_RETURN_OK) {
if (dlt_connection_create(daemon_local,
&daemon_local->pEvent,
fd,
POLLIN,
DLT_CONNECTION_CONTROL_CONNECT)) {
dlt_log(LOG_ERR, "Could not initialize control socket.\n");
return DLT_RETURN_ERROR;
}
}
else {
* control application */
if (dlt_daemon_initialize_control_socket(daemon_local) < DLT_RETURN_OK) {
dlt_log(LOG_ERR, "Could not initialize control socket.\n");
return DLT_RETURN_ERROR;
}
Expand Down
2 changes: 2 additions & 0 deletions src/daemon/dlt-daemon.rc
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ service dlt-daemon /vendor/bin/dlt-daemon
class late_start
user root
group root
socket dlt stream 666 root root
socket dlt-ctrl.sock stream 660 root root
disabled
44 changes: 43 additions & 1 deletion src/daemon/dlt_daemon_unix_socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,59 @@
#include <stdlib.h>
#include <string.h>
#include <sys/un.h>
#include <sys/socket.h>
#if defined(ANDROID)
# include <cutils/sockets.h> /* for android_get_control_socket() */
# include <libgen.h> /* for basename() */
#else
# include <sys/socket.h> /* for socket(), connect(), (), and recv() */
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <syslog.h>
#include <errno.h>

#include "dlt-daemon.h"
#include "dlt_common.h"
#include "dlt-daemon_cfg.h"
#include "dlt_daemon_socket.h"
#include "dlt_daemon_unix_socket.h"

#ifdef ANDROID
DltReturnValue dlt_daemon_unix_android_get_socket(int *sock, const char *sock_path)
{
DltReturnValue ret = DLT_RETURN_OK;

if ((sock == NULL) || (sock_path == NULL)) {
dlt_log(LOG_ERR, "dlt_daemon_unix_android_get_socket: arguments invalid");
ret = DLT_RETURN_WRONG_PARAMETER;
}
else {
const char* sock_name = basename(sock_path);
if (sock_name == NULL) {
dlt_log(LOG_WARNING,
"dlt_daemon_unix_android_get_socket: can't get socket name from its path");
ret = DLT_RETURN_ERROR;
}
else {
*sock = android_get_control_socket(sock_name);
if (*sock < 0) {
dlt_log(LOG_WARNING,
"dlt_daemon_unix_android_get_socket: can get socket from init");
ret = DLT_RETURN_ERROR;
}
else {
if (listen(*sock, 1) == -1) {
dlt_vlog(LOG_WARNING, "unix socket: listen error: %s", strerror(errno));
ret = DLT_RETURN_ERROR;
}
}
}
}

return ret;
}
#endif

int dlt_daemon_unix_socket_open(int *sock, char *sock_path, int type, int mask)
{
struct sockaddr_un addr;
Expand Down
3 changes: 3 additions & 0 deletions src/daemon/dlt_daemon_unix_socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@
#ifndef DLT_DAEMON_UNIX_SOCKET_H
#define DLT_DAEMON_UNIX_SOCKET_H

#ifdef ANDROID
DltReturnValue dlt_daemon_unix_android_get_socket(int *sock, const char *sock_path);
#endif
int dlt_daemon_unix_socket_open(int *sock, char *socket_path, int type, int mask);
int dlt_daemon_unix_socket_close(int sock);

Expand Down