Skip to content

Commit 173177e

Browse files
committed
tqftpserv: Enable config file support in tqftpserv
Integrate configuration management via config file and command-line arguments to customize server behavior. This allows runtime configuration of key filesystem paths including readonly_path, readwrite_path, firmware_base, updates_dir, and temp_dir. It also enables dynamic adjustment of the default log level, supporting flexible deployment across platforms like Linux and Android without requiring code changes. Signed-off-by: Bhaskar Valaboju <[email protected]>
1 parent 46d75e0 commit 173177e

File tree

2 files changed

+36
-24
lines changed

2 files changed

+36
-24
lines changed

tqftpserv.c

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "list.h"
1717
#include "translate.h"
1818
#include "logging.h"
19+
#include "config.h"
1920

2021
#define MAX(x, y) ((x) > (y) ? (x) : (y))
2122

@@ -603,7 +604,7 @@ static void client_close_and_free(struct tftp_client *client)
603604
free(client);
604605
}
605606

606-
int main()
607+
int main(int argc, char **argv)
607608
{
608609
struct tftp_client *client;
609610
struct tftp_client *next;
@@ -618,6 +619,27 @@ int main()
618619
int ret;
619620
int fd;
620621

622+
/* Initialize configuration with defaults */
623+
tqftp_config_init_defaults(&tqftp_config);
624+
625+
/* Parse command line arguments */
626+
ret = tqftp_config_parse_args(argc, argv, &tqftp_config);
627+
if (ret == 1) {
628+
/* Help was shown */
629+
return 0;
630+
} else if (ret < 0) {
631+
/* Error in arguments */
632+
return 1;
633+
}
634+
635+
/* Initialize logging */
636+
tqftp_log_init_with_config(&tqftp_config.log_config);
637+
638+
TQFTP_LOG_INFO("TQFTP server starting");
639+
TQFTP_LOG_DEBUG("Configuration: readonly_path=%s, readwrite_path=%s, firmware_base=%s, temp_dir=%s",
640+
tqftp_config.readonly_path, tqftp_config.readwrite_path,
641+
tqftp_config.firmware_base, tqftp_config.temp_dir);
642+
621643
fd = qrtr_open(0);
622644
if (fd < 0) {
623645
TQFTP_LOG_ERR("failed to open qrtr socket");

translate.c

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,7 @@
1818
#include "translate.h"
1919
#include "zstd-decompress.h"
2020
#include "logging.h"
21-
#define READONLY_PATH "/readonly/firmware/image/"
22-
#define READWRITE_PATH "/readwrite/"
23-
24-
#ifndef ANDROID
25-
#define FIRMWARE_BASE "/lib/firmware/"
26-
#define TQFTPSERV_TMP "/tmp/tqftpserv"
27-
#define UPDATES_DIR "updates/"
28-
#else
29-
#define FIRMWARE_BASE "/vendor/firmware/"
30-
#define TQFTPSERV_TMP "/data/vendor/tmp/tqftpserv"
31-
#endif
21+
#include "config.h"
3222

3323
static int open_maybe_compressed(const char *path);
3424

@@ -130,19 +120,19 @@ static int translate_readonly(const char *file)
130120
}
131121

132122
/* now try with base path */
133-
if (strlen(FIRMWARE_BASE) + strlen(UPDATES_DIR) + strlen(firmware_value) + 1 +
123+
if (strlen(tqftp_config.firmware_base) + strlen(tqftp_config.updates_dir) + strlen(firmware_value) + 1 +
134124
strlen(file) + 1 > sizeof(path))
135125
continue;
136126

137-
strcpy(path, FIRMWARE_BASE);
138-
strcat(path, UPDATES_DIR);
127+
strcpy(path, tqftp_config.firmware_base);
128+
strcat(path, tqftp_config.updates_dir);
139129
strcat(path, firmware_path);
140130
strcat(path, "/");
141131
strcat(path, file);
142132

143133
fd = open_maybe_compressed(path);
144134
if (fd < 0) {
145-
strcpy(path, FIRMWARE_BASE);
135+
strcpy(path, tqftp_config.firmware_base);
146136
strcat(path, firmware_path);
147137
strcat(path, "/");
148138
strcat(path, file);
@@ -175,17 +165,17 @@ static int translate_readwrite(const char *file, int flags)
175165
int ret;
176166
int fd;
177167

178-
ret = mkdir(TQFTPSERV_TMP, 0700);
168+
ret = mkdir(tqftp_config.temp_dir, 0700);
179169
if (ret < 0 && errno != EEXIST) {
180170
TQFTP_LOG_WARN("failed to create temporary tqftpserv directory %s: %s",
181-
TQFTPSERV_TMP, strerror(errno));
171+
tqftp_config.temp_dir, strerror(errno));
182172
return -1;
183173
}
184174

185-
base = open(TQFTPSERV_TMP, O_RDONLY | O_DIRECTORY);
175+
base = open(tqftp_config.temp_dir, O_RDONLY | O_DIRECTORY);
186176
if (base < 0) {
187177
TQFTP_LOG_WARN("failed to open temporary tqftpserv directory %s: %s",
188-
TQFTPSERV_TMP, strerror(errno));
178+
tqftp_config.temp_dir, strerror(errno));
189179
return -1;
190180
}
191181

@@ -207,10 +197,10 @@ static int translate_readwrite(const char *file, int flags)
207197
*/
208198
int translate_open(const char *path, int flags)
209199
{
210-
if (!strncmp(path, READONLY_PATH, strlen(READONLY_PATH)))
211-
return translate_readonly(path + strlen(READONLY_PATH));
212-
else if (!strncmp(path, READWRITE_PATH, strlen(READWRITE_PATH)))
213-
return translate_readwrite(path + strlen(READWRITE_PATH), flags);
200+
if (!strncmp(path, tqftp_config.readonly_path, strlen(tqftp_config.readonly_path)))
201+
return translate_readonly(path + strlen(tqftp_config.readonly_path));
202+
else if (!strncmp(path, tqftp_config.readwrite_path, strlen(tqftp_config.readwrite_path)))
203+
return translate_readwrite(path + strlen(tqftp_config.readwrite_path), flags);
214204

215205
TQFTP_LOG_ERR("invalid path %s, rejecting", path);
216206
errno = ENOENT;

0 commit comments

Comments
 (0)