Skip to content

Commit 92566eb

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 eb773fa commit 92566eb

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
@@ -22,17 +22,7 @@
2222
#include "translate.h"
2323
#include "zstd-decompress.h"
2424
#include "logging.h"
25-
#define READONLY_PATH "/readonly/firmware/image/"
26-
#define READWRITE_PATH "/readwrite/"
27-
28-
#ifndef ANDROID
29-
#define FIRMWARE_BASE "/lib/firmware/"
30-
#define TQFTPSERV_TMP "/tmp/tqftpserv"
31-
#define UPDATES_DIR "updates/"
32-
#else
33-
#define FIRMWARE_BASE "/vendor/firmware/"
34-
#define TQFTPSERV_TMP "/data/vendor/tmp/tqftpserv"
35-
#endif
25+
#include "config.h"
3626

3727
static int open_maybe_compressed(const char *path);
3828

@@ -134,19 +124,19 @@ static int translate_readonly(const char *file)
134124
}
135125

136126
/* now try with base path */
137-
if (strlen(FIRMWARE_BASE) + strlen(UPDATES_DIR) + strlen(firmware_value) + 1 +
127+
if (strlen(tqftp_config.firmware_base) + strlen(tqftp_config.updates_dir) + strlen(firmware_value) + 1 +
138128
strlen(file) + 1 > sizeof(path))
139129
continue;
140130

141-
strcpy(path, FIRMWARE_BASE);
142-
strcat(path, UPDATES_DIR);
131+
strcpy(path, tqftp_config.firmware_base);
132+
strcat(path, tqftp_config.updates_dir);
143133
strcat(path, firmware_path);
144134
strcat(path, "/");
145135
strcat(path, file);
146136

147137
fd = open_maybe_compressed(path);
148138
if (fd < 0) {
149-
strcpy(path, FIRMWARE_BASE);
139+
strcpy(path, tqftp_config.firmware_base);
150140
strcat(path, firmware_path);
151141
strcat(path, "/");
152142
strcat(path, file);
@@ -179,17 +169,17 @@ static int translate_readwrite(const char *file, int flags)
179169
int ret;
180170
int fd;
181171

182-
ret = mkdir(TQFTPSERV_TMP, 0700);
172+
ret = mkdir(tqftp_config.temp_dir, 0700);
183173
if (ret < 0 && errno != EEXIST) {
184174
TQFTP_LOG_WARN("failed to create temporary tqftpserv directory %s: %s",
185-
TQFTPSERV_TMP, strerror(errno));
175+
tqftp_config.temp_dir, strerror(errno));
186176
return -1;
187177
}
188178

189-
base = open(TQFTPSERV_TMP, O_RDONLY | O_DIRECTORY);
179+
base = open(tqftp_config.temp_dir, O_RDONLY | O_DIRECTORY);
190180
if (base < 0) {
191181
TQFTP_LOG_WARN("failed to open temporary tqftpserv directory %s: %s",
192-
TQFTPSERV_TMP, strerror(errno));
182+
tqftp_config.temp_dir, strerror(errno));
193183
return -1;
194184
}
195185

@@ -211,10 +201,10 @@ static int translate_readwrite(const char *file, int flags)
211201
*/
212202
int translate_open(const char *path, int flags)
213203
{
214-
if (!strncmp(path, READONLY_PATH, strlen(READONLY_PATH)))
215-
return translate_readonly(path + strlen(READONLY_PATH));
216-
else if (!strncmp(path, READWRITE_PATH, strlen(READWRITE_PATH)))
217-
return translate_readwrite(path + strlen(READWRITE_PATH), flags);
204+
if (!strncmp(path, tqftp_config.readonly_path, strlen(tqftp_config.readonly_path)))
205+
return translate_readonly(path + strlen(tqftp_config.readonly_path));
206+
else if (!strncmp(path, tqftp_config.readwrite_path, strlen(tqftp_config.readwrite_path)))
207+
return translate_readwrite(path + strlen(tqftp_config.readwrite_path), flags);
218208

219209
TQFTP_LOG_ERR("invalid path %s, rejecting", path);
220210
errno = ENOENT;

0 commit comments

Comments
 (0)