Skip to content

Commit

Permalink
Refactor _sys_file helper function
Browse files Browse the repository at this point in the history
Also fixed a leak from missing kfree
  • Loading branch information
JNE committed Oct 10, 2024
1 parent 232d8a5 commit fcfc49b
Showing 1 changed file with 29 additions and 19 deletions.
48 changes: 29 additions & 19 deletions src/sys.c
Original file line number Diff line number Diff line change
Expand Up @@ -1020,36 +1020,46 @@ void fh_remove_hooks(struct ftrace_hook *hooks) {
}
}

static char *_sys_file(char *prefix, char *file, int len) {
if (*file == 0) {
char s[8] = {0};
static char *_sys_file(const char *prefix, char *file, int max) {
int prefix_len, rand_len;
bool rc = false;

if (file && prefix) {

prefix_len = strlen(prefix);
rand_len = max - prefix_len - 1; /** for '\0' */

snprintf(&s[0], 7, "%s", kv_util_random_AZ_string(7));
snprintf(file, len-1, "%s%s", prefix, s);
{
const char *tmp[] = {s,NULL};
fs_add_name_ro(tmp, 0);
if (rand_len > 0) {
char *rand_buf = kv_util_random_AZ_string(rand_len);

if (rand_buf) {
snprintf(file, max, "%s%s", prefix, rand_buf);
kfree(rand_buf);
rc = true;
}
}
prinfo("new %s, filename: '%s'\n", prefix, file);
}

return file;
return rc ? file : NULL;
}

char *sys_ttyfile(void) {
static char file[16] = {0};
if (*file == '\0') {
if (!_sys_file("/var/.", file, 16))
return NULL;
static char file[32] = {0};
if (*file == 0) {
if (_sys_file("/var/.", file, 31)) {
const char *var[] = {file, NULL};
fs_add_name_ro(var,0);
}
}
return file;
}

char *sys_sslfile(void) {
static char file[16] = {0};
if (*file == '\0') {
if (!_sys_file("/tmp/.", file, 16))
return NULL;
static char file[32] = {0};
if (*file == 0) {
if (_sys_file("/tmp/.", file, 31)) {
const char *tmp[] = {file, NULL};
fs_add_name_ro(tmp,0);
}
}
return file;
}
Expand Down

0 comments on commit fcfc49b

Please sign in to comment.