Skip to contentSkip to conversation

storage: Add support for STATE_DIRECTORY environment variable

PATCH Superseded Luiz Augusto von Dentz 2 participants 4 messages Apr 15, 2022 – Apr 16, 2022 Bluetooth drivers

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
author maintainer
#1 lore raw

PATCH 2/3 main: Add support for CONFIGURATION_DIRECTORY environment variable

In reply to Luiz Augusto von Dentz

From: Luiz Augusto von Dentz <[email protected]>

When running as a systemd service the CONFIGURATION_DIRECTORY environment variable maybe set:

https://www.freedesktop.org/software/systemd/man/systemd.exec.html

---
 src/main.c | 40 ++++++++++++++++++++++++++++++----------
 1 file changed, 30 insertions(+), 10 deletions(-)
src/main.c +30 -10
index a448320c1..890d159d7 100644
@@ -64,7 +64,7 @@
 
 struct btd_opts btd_opts;
 static GKeyFile *main_conf;
-static char *main_conf_file_path;
+static char main_conf_file_path[PATH_MAX];
 
 static const char *supported_options[] = {
 	"Name",
@@ -175,18 +175,43 @@ GKeyFile *btd_get_main_conf(void)
 	return main_conf;
 }
 
-static GKeyFile *load_config(const char *file)
+static GKeyFile *load_config(const char *name)
 {
 	GError *err = NULL;
 	GKeyFile *keyfile;
+	int len;
+
+	if (!name)
+		return NULL;
+
+	if (name[0] == '/')
+		snprintf(main_conf_file_path, PATH_MAX, "%s", name);
+	else {
+		const char *configdir = getenv("CONFIGURATION_DIRECTORY");
+
+		/* Check if running as service */
+		if (configdir) {
+			/* Check if there multiple paths given */
+			len = strstr(configdir, ":") - configdir;
+			if (len < 0)
+				len = strlen(configdir);
+		} else {
+			configdir = CONFIGDIR;
+			len = strlen(configdir);
+		}
+
+		snprintf(main_conf_file_path, PATH_MAX, "%*s/%s", len,
+						 configdir, name);
+	}
 
 	keyfile = g_key_file_new();
 
 	g_key_file_set_list_separator(keyfile, ',');
 
-	if (!g_key_file_load_from_file(keyfile, file, 0, &err)) {
+	if (!g_key_file_load_from_file(keyfile, main_conf_file_path, 0, &err)) {
 		if (!g_error_matches(err, G_FILE_ERROR, G_FILE_ERROR_NOENT))
-			error("Parsing %s failed: %s", file, err->message);
+			error("Parsing %s failed: %s", main_conf_file_path,
+				err->message);
 		g_error_free(err);
 		g_key_file_free(keyfile);
 		return NULL;
@@ -1194,12 +1219,7 @@ int main(int argc, char *argv[])
 
 	mainloop_sd_notify("STATUS=Starting up");
 
-	if (option_configfile)
-		main_conf_file_path = option_configfile;
-	else
-		main_conf_file_path = CONFIGDIR "/main.conf";
-
-	main_conf = load_config(main_conf_file_path);
+	main_conf = load_config(option_configfile ? : "main.conf");
 
 	parse_config(main_conf);
 
Signature · 1 line
2.35.1
author maintainer
#2 lore raw

PATCH 3/3 build: Make use of StateDirectory and ConfigurationDirectory

In reply to Luiz Augusto von Dentz

From: Luiz Augusto von Dentz <[email protected]>

This makes use of StateDirectory[1] and ConfigurationDirectory[1] to inform systemd what those paths are used for instead of using ReadWritePaths and ReadOnlyPaths which can lead to issues.

[1] https://www.freedesktop.org/software/systemd/man/systemd.exec.html

---
 src/bluetooth.service.in | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
src/bluetooth.service.in +3 -3
index f18801866..4d39ad49d 100644
@@ -15,12 +15,12 @@ LimitNPROC=1
 
 # Filesystem lockdown
 ProtectHome=true
-ProtectSystem=full
+ProtectSystem=strict
 PrivateTmp=true
 ProtectKernelTunables=true
 ProtectControlGroups=true
-ReadWritePaths=@statedir@
-ReadOnlyPaths=@confdir@
+StateDirectory=bluetooth
+ConfigurationDirectory=bluetooth
 
 # Execute Mappings
 MemoryDenyWriteExecute=true
Signature · 1 line
2.35.1

Luiz Augusto von Dentz

author maintainer
#0 lore raw

PATCH 1/3 storage: Add support for STATE_DIRECTORY environment variable

From: Luiz Augusto von Dentz <[email protected]>

When running as a systemd service the STATE_DIRECTORY environment variable maybe set:

https://www.freedesktop.org/software/systemd/man/systemd.exec.html

---
 profiles/audio/a2dp.c |  10 ++--
 src/adapter.c         | 123 +++++++++++++++++++++---------------------
 src/device.c          |  57 ++++++++++----------
 src/storage.c         |  21 +++-----
 src/textfile.c        |  40 +++++++++++++-
 src/textfile.h        |   5 +-
 6 files changed, 147 insertions(+), 109 deletions(-)
profiles/audio/a2dp.c +7 -3
index 21b3faa47..6f5b13711 100644
@@ -40,6 +40,7 @@
 #include "src/service.h"
 #include "src/log.h"
 #include "src/sdpd.h"
+#include "src/textfile.h"
 #include "src/shared/queue.h"
 #include "src/shared/timeout.h"
 #include "src/shared/util.h"
@@ -831,9 +832,10 @@ static void store_remote_seps(struct a2dp_channel *chan)
 
 	ba2str(device_get_address(device), dst_addr);
 
-	snprintf(filename, PATH_MAX, STORAGEDIR "/%s/cache/%s",
+	create_filename(filename, PATH_MAX, "/%s/cache/%s",
 			btd_adapter_get_storage_dir(device_get_adapter(device)),
 			dst_addr);
+
 	key_file = g_key_file_new();
 	if (!g_key_file_load_from_file(key_file, filename, 0, &gerr)) {
 		error("Unable to load key file from %s: (%s)", filename,
@@ -1003,9 +1005,10 @@ static void store_last_used(struct a2dp_channel *chan, uint8_t lseid,
 
 	ba2str(device_get_address(chan->device), dst_addr);
 
-	snprintf(filename, PATH_MAX, STORAGEDIR "/%s/cache/%s",
+	create_filename(filename, PATH_MAX, "/%s/cache/%s",
 		btd_adapter_get_storage_dir(device_get_adapter(chan->device)),
 		dst_addr);
+
 	key_file = g_key_file_new();
 	if (!g_key_file_load_from_file(key_file, filename, 0, &gerr)) {
 		error("Unable to load key file from %s: (%s)", filename,
@@ -2262,9 +2265,10 @@ static void load_remote_seps(struct a2dp_channel *chan)
 
 	ba2str(device_get_address(device), dst_addr);
 
-	snprintf(filename, PATH_MAX, STORAGEDIR "/%s/cache/%s",
+	create_filename(filename, PATH_MAX, "/%s/cache/%s",
 			btd_adapter_get_storage_dir(device_get_adapter(device)),
 			dst_addr);
+
 	key_file = g_key_file_new();
 	if (!g_key_file_load_from_file(key_file, filename, 0, &gerr)) {
 		error("Unable to load key file from %s: (%s)", filename,
src/adapter.c +63 -60

375 lines not on this page · raw message

src/device.c +29 -28

179 lines not on this page · raw message

src/storage.c +7 -14
index d819c2c1e..6e69be978 100644
@@ -43,21 +43,11 @@ struct match {
 	char *pattern;
 };
 
-static inline int create_filename(char *buf, size_t size,
-				const bdaddr_t *bdaddr, const char *name)
-{
-	char addr[18];
-
-	ba2str(bdaddr, addr);
-
-	return create_name(buf, size, STORAGEDIR, addr, name);
-}
-
 int read_discoverable_timeout(const char *src, int *timeout)
 {
 	char filename[PATH_MAX], *str;
 
-	create_name(filename, PATH_MAX, STORAGEDIR, src, "config");
+	create_name(filename, PATH_MAX, src, "config");
 
 	str = textfile_get(filename, "discovto");
 	if (!str)
@@ -77,7 +67,7 @@ int read_pairable_timeout(const char *src, int *timeout)
 {
 	char filename[PATH_MAX], *str;
 
-	create_name(filename, PATH_MAX, STORAGEDIR, src, "config");
+	create_name(filename, PATH_MAX, src, "config");
 
 	str = textfile_get(filename, "pairto");
 	if (!str)
@@ -97,7 +87,7 @@ int read_on_mode(const char *src, char *mode, int length)
 {
 	char filename[PATH_MAX], *str;
 
-	create_name(filename, PATH_MAX, STORAGEDIR, src, "config");
+	create_name(filename, PATH_MAX, src, "config");
 
 	str = textfile_get(filename, "onmode");
 	if (!str)
@@ -115,8 +105,11 @@ int read_local_name(const bdaddr_t *bdaddr, char *name)
 {
 	char filename[PATH_MAX], *str;
 	int len;
+	char addr[18];
 
-	create_filename(filename, PATH_MAX, bdaddr, "config");
+	ba2str(bdaddr, addr);
+
+	create_filename(filename, PATH_MAX, "/%s/config", addr);
 
 	str = textfile_get(filename, "name");
 	if (!str)
src/textfile.c +38 -2
index 8c8d509b4..dcc00c7b1 100644
@@ -19,6 +19,7 @@
 #include <fcntl.h>
 #include <unistd.h>
 #include <stdlib.h>
+#include <stdarg.h>
 #include <string.h>
 #include <sys/file.h>
 #include <sys/stat.h>
@@ -27,6 +28,41 @@
 
 #include "textfile.h"
 
+int create_filename(char *str, size_t size, const char *fmt, ...)
+{
+	static const char *prefix;
+	static int prefix_len;
+	char suffix[PATH_MAX];
+	va_list ap;
+	int err;
+
+	if (!prefix) {
+		const char *statedir = getenv("STATE_DIRECTORY");
+
+		/* Check if running as service */
+		if (statedir) {
+			prefix = statedir;
+
+			/* Check if there multiple paths given */
+			prefix_len = strstr(prefix, ":") - prefix;
+			if (prefix_len < 0)
+				prefix_len = strlen(prefix);
+		} else {
+			prefix = STORAGEDIR;
+			prefix_len = strlen(prefix);
+		}
+	}
+
+	va_start(ap, fmt);
+	err = vsnprintf(suffix, sizeof(suffix), fmt, ap);
+	va_end(ap);
+
+	if (err < 0)
+		return err;
+
+	return snprintf(str, size, "%*s%s", prefix_len, prefix, suffix);
+}
+
 static int create_dirs(const char *filename, const mode_t mode)
 {
 	struct stat st;
@@ -76,9 +112,9 @@ int create_file(const char *filename, const mode_t mode)
 	return 0;
 }
 
-int create_name(char *buf, size_t size, const char *path, const char *address, const char *name)
+int create_name(char *buf, size_t size, const char *address, const char *name)
 {
-	return snprintf(buf, size, "%s/%s/%s", path, address, name);
+	return create_filename(buf, size, "/%s/%s", address, name);
 }
 
 static inline char *find_key(char *map, size_t size, const char *key, size_t len, int icase)
src/textfile.h +3 -2
index b4e2e0248..4ecd5ed98 100644
@@ -8,9 +8,10 @@
  *
  */
 
+int create_filename(char *str, size_t size, const char *fmt, ...)
+					__attribute__((format(printf, 3, 4)));
 int create_file(const char *filename, const mode_t mode);
-int create_name(char *buf, size_t size, const char *path,
-				const char *address, const char *name);
+int create_name(char *buf, size_t size, const char *address, const char *name);
 
 int textfile_put(const char *pathname, const char *key, const char *value);
 int textfile_del(const char *pathname, const char *key);
Signature · 1 line
2.35.1
#3 lore raw

This is automated email and please do not reply to this email!

Dear submitter,

Thank you for submitting the patches to the linux bluetooth mailing list. This is a CI test results with your patch series:
PW Link:https://patchwork.kernel.org/project/bluetooth/list/?series=632674

---Test result---

Test Summary:

CheckPatch                    FAIL      4.99 seconds
GitLint                       PASS      3.13 seconds
Prep - Setup ELL              PASS      50.90 seconds
Build - Prep                  PASS      0.76 seconds
Build - Configure             PASS      10.20 seconds
Build - Make                  PASS      1488.98 seconds
Make Check                    PASS      12.67 seconds
Make Check w/Valgrind         PASS      526.21 seconds
Make Distcheck                PASS      271.84 seconds
Build w/ext ELL - Configure   PASS      10.50 seconds
Build w/ext ELL - Make        PASS      1451.75 seconds
Incremental Build with patchesPASS      4519.14 seconds

Details
##############################
Test: CheckPatch - FAIL
Desc: Run checkpatch.pl script with rule in .checkpatch.conf
Output:
[BlueZ,1/3] storage: Add support for STATE_DIRECTORY environment variable WARNING:PREFER_DEFINED_ATTRIBUTE_MACRO: Prefer __printf(3, 4) over __attribute__((format(printf, 3, 4))) #888: FILE: src/textfile.h:12:
+ __attribute__((format(printf, 3, 4)));

/github/workspace/src/12815401.patch total: 0 errors, 1 warnings, 715 lines checked

NOTE: For some of the reported defects, checkpatch may be able to
mechanically convert to the typical style using --fix or --fix-inplace.

/github/workspace/src/12815401.patch has style problems, please review.

NOTE: Ignored message types: COMMIT_MESSAGE COMPLEX_MACRO CONST_STRUCT FILE_PATH_CHANGES MISSING_SIGN_OFF PREFER_PACKED SPDX_LICENSE_TAG SPLIT_STRING SSCANF_TO_KSTRTO

NOTE: If any of the errors are false positives, please report
them to the maintainer, see CHECKPATCH in MAINTAINERS.

[BlueZ,2/3] main: Add support for CONFIGURATION_DIRECTORY environment variable WARNING:EMBEDDED_FUNCTION_NAME: Prefer using '"%s...", __func__' to using 'main', this function's name, in a string #161: FILE: src/main.c:1222:
+ main_conf = load_config(option_configfile ? : "main.conf");

/github/workspace/src/12815399.patch total: 0 errors, 1 warnings, 67 lines checked

NOTE: For some of the reported defects, checkpatch may be able to
mechanically convert to the typical style using --fix or --fix-inplace.

/github/workspace/src/12815399.patch has style problems, please review.

NOTE: Ignored message types: COMMIT_MESSAGE COMPLEX_MACRO CONST_STRUCT FILE_PATH_CHANGES MISSING_SIGN_OFF PREFER_PACKED SPDX_LICENSE_TAG SPLIT_STRING SSCANF_TO_KSTRTO

NOTE: If any of the errors are false positives, please report
them to the maintainer, see CHECKPATCH in MAINTAINERS.

---
Regards,
Linux Bluetooth