All the mail mirrored from lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2 1/3] android: Add native support for Android logger
@ 2014-12-15 14:07 Szymon Janc
  2014-12-15 14:07 ` [PATCH v2 2/3] android/snoop: Use common logging API Szymon Janc
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Szymon Janc @ 2014-12-15 14:07 UTC (permalink / raw
  To: linux-bluetooth; +Cc: Szymon Janc

Lollipop enabled SELinux in enforcing mode but doesn't provide
proper policy for logwrapper. All AOSP native services were converted
to not use logwrapper at all so we should follow the same.

On Android Lollipop we handle logging by sending data to Android
logd over socket. On Android KitKat logs are written directly to
/dev/log/system.

Nice addition over logwrapper is that now we have proper tags for
messages levels like debug, info, error etc.
---
V2: - don't depend on glib on Android
    - use structure for logd header (make code easier to read)
    - reduce getpid calls

 android/Android.mk |   2 +-
 android/log.c      | 213 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 214 insertions(+), 1 deletion(-)
 create mode 100644 android/log.c

diff --git a/android/Android.mk b/android/Android.mk
index c1505c8..17349bf 100644
--- a/android/Android.mk
+++ b/android/Android.mk
@@ -58,7 +58,7 @@ LOCAL_SRC_FILES := \
 	bluez/android/sco.c \
 	bluez/profiles/health/mcap.c \
 	bluez/android/map-client.c \
-	bluez/src/log.c \
+	bluez/android/log.c \
 	bluez/src/shared/mgmt.c \
 	bluez/src/shared/util.c \
 	bluez/src/shared/queue.c \
diff --git a/android/log.c b/android/log.c
new file mode 100644
index 0000000..09d226a
--- /dev/null
+++ b/android/log.c
@@ -0,0 +1,213 @@
+/*
+ *
+ *  BlueZ - Bluetooth protocol stack for Linux
+ *
+ *  Copyright (C) 2014  Intel Corporation. All rights reserved.
+ *
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ */
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <fcntl.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <stdbool.h>
+#include <time.h>
+#include <sys/uio.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+
+#include "src/log.h"
+
+#define LOG_TAG "bluetoothd"
+
+#define LOG_DEBUG 3
+#define LOG_INFO 4
+#define LOG_WARN 5
+#define LOG_ERR 6
+
+#define LOG_ID_SYSTEM 3
+
+struct logd_header {
+	uint8_t id;
+	uint16_t pid; /* Android logd expects only 2 bytes for PID */
+	uint32_t sec;
+	uint32_t nsec;
+} __attribute__ ((packed));
+
+static int log_fd = -1;
+static bool legacy_log = false;
+
+static void android_log(unsigned char level, const char *fmt, va_list ap)
+{
+	struct logd_header header;
+	struct iovec vec[4];
+	int cnt = 0;
+	char *msg;
+	static pid_t pid = 0;
+
+	if (log_fd < 0)
+		return;
+
+	/* no need to call getpid all the time since we don't fork */
+	if (!pid)
+		pid = getpid();
+
+	if (vasprintf(&msg, fmt, ap) < 0)
+		return;
+
+	if (!legacy_log) {
+		struct timespec ts;
+
+		clock_gettime(CLOCK_REALTIME, &ts);
+
+		header.id = LOG_ID_SYSTEM;
+		header.pid = pid;
+		header.sec = ts.tv_sec;
+		header.nsec = ts.tv_nsec;
+
+		vec[0].iov_base = &header;
+		vec[0].iov_len = sizeof(header);
+
+		cnt += 1;
+	}
+
+	vec[cnt + 0].iov_base = &level;
+	vec[cnt + 0].iov_len = sizeof(level);
+	vec[cnt + 1].iov_base = LOG_TAG;
+	vec[cnt + 1].iov_len = sizeof(LOG_TAG);
+	vec[cnt + 2].iov_base  = msg;
+	vec[cnt + 2].iov_len  = strlen(msg) + 1;
+
+	cnt += 3;
+
+	writev(log_fd, vec, cnt);
+
+	free(msg);
+}
+
+void info(const char *format, ...)
+{
+	va_list ap;
+
+	va_start(ap, format);
+
+	android_log(LOG_INFO, format, ap);
+
+	va_end(ap);
+}
+
+void warn(const char *format, ...)
+{
+	va_list ap;
+
+	va_start(ap, format);
+
+	android_log(LOG_WARN, format, ap);
+
+	va_end(ap);
+}
+
+void error(const char *format, ...)
+{
+	va_list ap;
+
+	va_start(ap, format);
+
+	android_log(LOG_ERR, format, ap);
+
+	va_end(ap);
+}
+
+void btd_debug(const char *format, ...)
+{
+	va_list ap;
+
+	va_start(ap, format);
+
+	android_log(LOG_DEBUG, format, ap);
+
+	va_end(ap);
+}
+
+static bool init_legacy_log(void)
+{
+	log_fd = open("/dev/log/system", O_WRONLY);
+	if (log_fd < 0)
+		return false;
+
+	legacy_log = true;
+
+	return true;
+}
+
+static bool init_logd(void)
+{
+	struct sockaddr_un addr;
+
+	log_fd = socket(PF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, 0);
+	if (log_fd < 0)
+		return false;
+
+	if (fcntl(log_fd, F_SETFL, O_NONBLOCK) < 0)
+		goto failed;
+
+	memset(&addr, 0, sizeof(addr));
+	addr.sun_family = AF_UNIX;
+	strcpy(addr.sun_path, "/dev/socket/logdw");
+
+	if (connect(log_fd, (struct sockaddr *)&addr, sizeof(addr)) < 0)
+		goto failed;
+
+	return true;
+
+failed:
+	close(log_fd);
+	log_fd = -1;
+
+	return false;
+}
+
+extern struct btd_debug_desc __start___debug[];
+extern struct btd_debug_desc __stop___debug[];
+
+void __btd_log_init(const char *debug, int detach)
+{
+	if (!init_logd() && !init_legacy_log())
+		return;
+
+	if (debug) {
+		struct btd_debug_desc *desc;
+
+		for (desc = __start___debug; desc < __stop___debug; desc++)
+			desc->flags |= BTD_DEBUG_FLAG_PRINT;
+	}
+
+	info("Bluetooth daemon %s", VERSION);
+}
+
+void __btd_log_cleanup(void)
+{
+	if (log_fd < 0)
+		return;
+
+	close(log_fd);
+	log_fd = -1;
+}
-- 
1.9.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH v2 2/3] android/snoop: Use common logging API
  2014-12-15 14:07 [PATCH v2 1/3] android: Add native support for Android logger Szymon Janc
@ 2014-12-15 14:07 ` Szymon Janc
  2014-12-15 14:07 ` [PATCH v2 3/3] android: Run services without logwrapper Szymon Janc
  2014-12-15 16:05 ` [PATCH v2 1/3] android: Add native support for Android logger Szymon Janc
  2 siblings, 0 replies; 4+ messages in thread
From: Szymon Janc @ 2014-12-15 14:07 UTC (permalink / raw
  To: linux-bluetooth; +Cc: Szymon Janc

This allows to log without logwrapper.
---
 android/Android.mk         |  1 +
 android/Makefile.am        |  4 ++--
 android/bluetoothd-snoop.c | 14 ++++++++++++--
 3 files changed, 15 insertions(+), 4 deletions(-)

diff --git a/android/Android.mk b/android/Android.mk
index 17349bf..c1a9eff 100644
--- a/android/Android.mk
+++ b/android/Android.mk
@@ -447,6 +447,7 @@ LOCAL_SRC_FILES := \
 	bluez/android/bluetoothd-snoop.c \
 	bluez/monitor/mainloop.c \
 	bluez/src/shared/btsnoop.c \
+	bluez/android/log.c \
 
 LOCAL_C_INCLUDES := \
 	$(LOCAL_PATH)/bluez \
diff --git a/android/Makefile.am b/android/Makefile.am
index 6d74c05..ac9c360 100644
--- a/android/Makefile.am
+++ b/android/Makefile.am
@@ -11,8 +11,8 @@ android_system_emulator_LDADD = src/libshared-mainloop.la
 
 noinst_PROGRAMS += android/bluetoothd-snoop
 
-android_bluetoothd_snoop_SOURCES = android/bluetoothd-snoop.c
-android_bluetoothd_snoop_LDADD = src/libshared-mainloop.la
+android_bluetoothd_snoop_SOURCES = android/bluetoothd-snoop.c src/log.c
+android_bluetoothd_snoop_LDADD = src/libshared-mainloop.la @GLIB_LIBS@
 
 noinst_PROGRAMS += android/bluetoothd
 
diff --git a/android/bluetoothd-snoop.c b/android/bluetoothd-snoop.c
index dc34869..776dca7 100644
--- a/android/bluetoothd-snoop.c
+++ b/android/bluetoothd-snoop.c
@@ -25,7 +25,6 @@
 #include <config.h>
 #endif
 
-#include <stdio.h>
 #include <ctype.h>
 #include <stdlib.h>
 #include <unistd.h>
@@ -39,6 +38,7 @@
 
 #include "monitor/mainloop.h"
 #include "src/shared/btsnoop.h"
+#include "src/log.h"
 
 #define DEFAULT_SNOOP_FILE "/sdcard/btsnoop_hci.log"
 
@@ -218,6 +218,10 @@ int main(int argc, char *argv[])
 	const char *path;
 	sigset_t mask;
 
+	__btd_log_init(NULL, 0);
+
+	DBG("");
+
 	set_capabilities();
 
 	if (argc > 1)
@@ -237,13 +241,19 @@ int main(int argc, char *argv[])
 		rename(DEFAULT_SNOOP_FILE, DEFAULT_SNOOP_FILE ".old");
 
 	if (open_monitor(path) < 0) {
-		printf("Failed to start bluetoothd_snoop\n");
+		error("bluetoothd_snoop: start failed");
 		return EXIT_FAILURE;
 	}
 
+	info("bluetoothd_snoop: started");
+
 	mainloop_run();
 
 	close_monitor();
 
+	info("bluetoothd_snoop: stopped");
+
+	__btd_log_cleanup();
+
 	return EXIT_SUCCESS;
 }
-- 
1.9.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH v2 3/3] android: Run services without logwrapper
  2014-12-15 14:07 [PATCH v2 1/3] android: Add native support for Android logger Szymon Janc
  2014-12-15 14:07 ` [PATCH v2 2/3] android/snoop: Use common logging API Szymon Janc
@ 2014-12-15 14:07 ` Szymon Janc
  2014-12-15 16:05 ` [PATCH v2 1/3] android: Add native support for Android logger Szymon Janc
  2 siblings, 0 replies; 4+ messages in thread
From: Szymon Janc @ 2014-12-15 14:07 UTC (permalink / raw
  To: linux-bluetooth; +Cc: Szymon Janc

Those services now log directly to Android logger so no need for
logwrapper which was causing issues with SELinux.
---
 android/init.bluetooth.rc | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/android/init.bluetooth.rc b/android/init.bluetooth.rc
index af62121..2d43f73 100644
--- a/android/init.bluetooth.rc
+++ b/android/init.bluetooth.rc
@@ -21,7 +21,7 @@ on property:bluetooth.stop=snoop
     setprop bluetooth.stop none
     stop bluetoothd-snoop
 
-service bluetoothd /system/bin/logwrapper /system/bin/bluetoothd
+service bluetoothd /system/bin/bluetoothd
     class main
     # init does not yet support setting capabilities so run as root,
     # bluetoothd drop uid to bluetooth with the right linux capabilities
@@ -29,7 +29,7 @@ service bluetoothd /system/bin/logwrapper /system/bin/bluetoothd
     disabled
     oneshot
 
-service bluetoothd-snoop /system/bin/logwrapper /system/bin/bluetoothd-snoop
+service bluetoothd-snoop /system/bin/bluetoothd-snoop
     class main
     # init does not yet support setting capabilities so run as root,
     # bluetoothd-snoop drops unneeded linux capabilities
-- 
1.9.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH v2 1/3] android: Add native support for Android logger
  2014-12-15 14:07 [PATCH v2 1/3] android: Add native support for Android logger Szymon Janc
  2014-12-15 14:07 ` [PATCH v2 2/3] android/snoop: Use common logging API Szymon Janc
  2014-12-15 14:07 ` [PATCH v2 3/3] android: Run services without logwrapper Szymon Janc
@ 2014-12-15 16:05 ` Szymon Janc
  2 siblings, 0 replies; 4+ messages in thread
From: Szymon Janc @ 2014-12-15 16:05 UTC (permalink / raw
  To: linux-bluetooth

On Monday 15 of December 2014 15:07:53 Szymon Janc wrote:
> Lollipop enabled SELinux in enforcing mode but doesn't provide
> proper policy for logwrapper. All AOSP native services were converted
> to not use logwrapper at all so we should follow the same.
> 
> On Android Lollipop we handle logging by sending data to Android
> logd over socket. On Android KitKat logs are written directly to
> /dev/log/system.
> 
> Nice addition over logwrapper is that now we have proper tags for
> messages levels like debug, info, error etc.
> ---
> V2: - don't depend on glib on Android
>     - use structure for logd header (make code easier to read)
>     - reduce getpid calls
> 
>  android/Android.mk |   2 +-
>  android/log.c      | 213 +++++++++++++++++++++++++++++++++++++++++++++++++++++
>  2 files changed, 214 insertions(+), 1 deletion(-)
>  create mode 100644 android/log.c
> 
> diff --git a/android/Android.mk b/android/Android.mk
> index c1505c8..17349bf 100644
> --- a/android/Android.mk
> +++ b/android/Android.mk
> @@ -58,7 +58,7 @@ LOCAL_SRC_FILES := \
>  	bluez/android/sco.c \
>  	bluez/profiles/health/mcap.c \
>  	bluez/android/map-client.c \
> -	bluez/src/log.c \
> +	bluez/android/log.c \
>  	bluez/src/shared/mgmt.c \
>  	bluez/src/shared/util.c \
>  	bluez/src/shared/queue.c \
> diff --git a/android/log.c b/android/log.c
> new file mode 100644
> index 0000000..09d226a
> --- /dev/null
> +++ b/android/log.c
> @@ -0,0 +1,213 @@
> +/*
> + *
> + *  BlueZ - Bluetooth protocol stack for Linux
> + *
> + *  Copyright (C) 2014  Intel Corporation. All rights reserved.
> + *
> + *
> + *  This program is free software; you can redistribute it and/or modify
> + *  it under the terms of the GNU General Public License as published by
> + *  the Free Software Foundation; either version 2 of the License, or
> + *  (at your option) any later version.
> + *
> + *  This program is distributed in the hope that it will be useful,
> + *  but WITHOUT ANY WARRANTY; without even the implied warranty of
> + *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> + *  GNU General Public License for more details.
> + *
> + *  You should have received a copy of the GNU General Public License
> + *  along with this program; if not, write to the Free Software
> + *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
> + *
> + */
> +#ifdef HAVE_CONFIG_H
> +#include <config.h>
> +#endif
> +
> +#include <fcntl.h>
> +#include <stdio.h>
> +#include <unistd.h>
> +#include <stdbool.h>
> +#include <time.h>
> +#include <sys/uio.h>
> +#include <sys/types.h>
> +#include <sys/socket.h>
> +#include <sys/un.h>
> +
> +#include "src/log.h"
> +
> +#define LOG_TAG "bluetoothd"
> +
> +#define LOG_DEBUG 3
> +#define LOG_INFO 4
> +#define LOG_WARN 5
> +#define LOG_ERR 6
> +
> +#define LOG_ID_SYSTEM 3
> +
> +struct logd_header {
> +	uint8_t id;
> +	uint16_t pid; /* Android logd expects only 2 bytes for PID */
> +	uint32_t sec;
> +	uint32_t nsec;
> +} __attribute__ ((packed));
> +
> +static int log_fd = -1;
> +static bool legacy_log = false;
> +
> +static void android_log(unsigned char level, const char *fmt, va_list ap)
> +{
> +	struct logd_header header;
> +	struct iovec vec[4];
> +	int cnt = 0;
> +	char *msg;
> +	static pid_t pid = 0;
> +
> +	if (log_fd < 0)
> +		return;
> +
> +	/* no need to call getpid all the time since we don't fork */
> +	if (!pid)
> +		pid = getpid();
> +
> +	if (vasprintf(&msg, fmt, ap) < 0)
> +		return;
> +
> +	if (!legacy_log) {
> +		struct timespec ts;
> +
> +		clock_gettime(CLOCK_REALTIME, &ts);
> +
> +		header.id = LOG_ID_SYSTEM;
> +		header.pid = pid;
> +		header.sec = ts.tv_sec;
> +		header.nsec = ts.tv_nsec;
> +
> +		vec[0].iov_base = &header;
> +		vec[0].iov_len = sizeof(header);
> +
> +		cnt += 1;
> +	}
> +
> +	vec[cnt + 0].iov_base = &level;
> +	vec[cnt + 0].iov_len = sizeof(level);
> +	vec[cnt + 1].iov_base = LOG_TAG;
> +	vec[cnt + 1].iov_len = sizeof(LOG_TAG);
> +	vec[cnt + 2].iov_base  = msg;
> +	vec[cnt + 2].iov_len  = strlen(msg) + 1;
> +
> +	cnt += 3;
> +
> +	writev(log_fd, vec, cnt);
> +
> +	free(msg);
> +}
> +
> +void info(const char *format, ...)
> +{
> +	va_list ap;
> +
> +	va_start(ap, format);
> +
> +	android_log(LOG_INFO, format, ap);
> +
> +	va_end(ap);
> +}
> +
> +void warn(const char *format, ...)
> +{
> +	va_list ap;
> +
> +	va_start(ap, format);
> +
> +	android_log(LOG_WARN, format, ap);
> +
> +	va_end(ap);
> +}
> +
> +void error(const char *format, ...)
> +{
> +	va_list ap;
> +
> +	va_start(ap, format);
> +
> +	android_log(LOG_ERR, format, ap);
> +
> +	va_end(ap);
> +}
> +
> +void btd_debug(const char *format, ...)
> +{
> +	va_list ap;
> +
> +	va_start(ap, format);
> +
> +	android_log(LOG_DEBUG, format, ap);
> +
> +	va_end(ap);
> +}
> +
> +static bool init_legacy_log(void)
> +{
> +	log_fd = open("/dev/log/system", O_WRONLY);
> +	if (log_fd < 0)
> +		return false;
> +
> +	legacy_log = true;
> +
> +	return true;
> +}
> +
> +static bool init_logd(void)
> +{
> +	struct sockaddr_un addr;
> +
> +	log_fd = socket(PF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, 0);
> +	if (log_fd < 0)
> +		return false;
> +
> +	if (fcntl(log_fd, F_SETFL, O_NONBLOCK) < 0)
> +		goto failed;
> +
> +	memset(&addr, 0, sizeof(addr));
> +	addr.sun_family = AF_UNIX;
> +	strcpy(addr.sun_path, "/dev/socket/logdw");
> +
> +	if (connect(log_fd, (struct sockaddr *)&addr, sizeof(addr)) < 0)
> +		goto failed;
> +
> +	return true;
> +
> +failed:
> +	close(log_fd);
> +	log_fd = -1;
> +
> +	return false;
> +}
> +
> +extern struct btd_debug_desc __start___debug[];
> +extern struct btd_debug_desc __stop___debug[];
> +
> +void __btd_log_init(const char *debug, int detach)
> +{
> +	if (!init_logd() && !init_legacy_log())
> +		return;
> +
> +	if (debug) {
> +		struct btd_debug_desc *desc;
> +
> +		for (desc = __start___debug; desc < __stop___debug; desc++)
> +			desc->flags |= BTD_DEBUG_FLAG_PRINT;
> +	}
> +
> +	info("Bluetooth daemon %s", VERSION);
> +}
> +
> +void __btd_log_cleanup(void)
> +{
> +	if (log_fd < 0)
> +		return;
> +
> +	close(log_fd);
> +	log_fd = -1;
> +}
> 

Applied.

-- 
Best regards, 
Szymon Janc

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2014-12-15 16:05 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-12-15 14:07 [PATCH v2 1/3] android: Add native support for Android logger Szymon Janc
2014-12-15 14:07 ` [PATCH v2 2/3] android/snoop: Use common logging API Szymon Janc
2014-12-15 14:07 ` [PATCH v2 3/3] android: Run services without logwrapper Szymon Janc
2014-12-15 16:05 ` [PATCH v2 1/3] android: Add native support for Android logger Szymon Janc

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.