| 1 |
/*************************************************************************** |
/* SPDX-License-Identifier: GPL-3.0-or-later */ |
| 2 |
file_loader.c - description |
/* |
| 3 |
------------------- |
* file_loader |
| 4 |
Copyright : (C) 2004-2025 by Leaflet |
* - shared memory based file loader |
| 5 |
Email : leaflet@leafok.com |
* |
| 6 |
***************************************************************************/ |
* Copyright (C) 2004-2025 Leaflet <leaflet@leafok.com> |
| 7 |
|
*/ |
|
/*************************************************************************** |
|
|
* * |
|
|
* 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 3 of the License, or * |
|
|
* (at your option) any later version. * |
|
|
* * |
|
|
***************************************************************************/ |
|
| 8 |
|
|
| 9 |
#include "file_loader.h" |
#include "file_loader.h" |
|
#include "trie_dict.h" |
|
|
#include "str_process.h" |
|
| 10 |
#include "log.h" |
#include "log.h" |
| 11 |
#include <fcntl.h> |
#include "str_process.h" |
| 12 |
|
#include "trie_dict.h" |
| 13 |
#include <errno.h> |
#include <errno.h> |
| 14 |
#include <unistd.h> |
#include <fcntl.h> |
| 15 |
#include <stdlib.h> |
#include <stdlib.h> |
| 16 |
|
#include <string.h> |
| 17 |
|
#include <time.h> |
| 18 |
|
#include <unistd.h> |
| 19 |
|
#include <sys/ipc.h> |
| 20 |
#include <sys/mman.h> |
#include <sys/mman.h> |
| 21 |
|
#include <sys/shm.h> |
| 22 |
#include <sys/stat.h> |
#include <sys/stat.h> |
| 23 |
|
|
| 24 |
#define FILE_MMAP_COUNT_LIMIT 256 |
struct shm_header_t |
| 25 |
|
{ |
| 26 |
|
int shmid; |
| 27 |
|
size_t data_len; |
| 28 |
|
long line_total; |
| 29 |
|
}; |
| 30 |
|
|
|
static FILE_MMAP *p_file_mmap_pool = NULL; |
|
|
static int file_mmap_count = 0; |
|
|
static int file_mmap_free_index = -1; |
|
| 31 |
static TRIE_NODE *p_trie_file_dict = NULL; |
static TRIE_NODE *p_trie_file_dict = NULL; |
| 32 |
|
|
| 33 |
int file_loader_init(int max_file_mmap_count) |
int file_loader_init() |
| 34 |
{ |
{ |
| 35 |
if (max_file_mmap_count > FILE_MMAP_COUNT_LIMIT) |
if (p_trie_file_dict != NULL) |
|
{ |
|
|
log_error("file_loader_init(%d) argument error\n", max_file_mmap_count); |
|
|
return -1; |
|
|
} |
|
|
|
|
|
if (p_file_mmap_pool != NULL || p_trie_file_dict != NULL) |
|
| 36 |
{ |
{ |
| 37 |
log_error("File loader already initialized\n"); |
log_error("File loader already initialized\n"); |
| 38 |
return -1; |
return -1; |
| 39 |
} |
} |
| 40 |
|
|
|
p_file_mmap_pool = (FILE_MMAP *)calloc((size_t)max_file_mmap_count, sizeof(FILE_MMAP)); |
|
|
if (p_file_mmap_pool == NULL) |
|
|
{ |
|
|
log_error("calloc(%d p_file_mmap_pool) error\n", max_file_mmap_count); |
|
|
return -2; |
|
|
} |
|
|
|
|
|
file_mmap_count = max_file_mmap_count; |
|
|
file_mmap_free_index = 0; |
|
|
|
|
| 41 |
p_trie_file_dict = trie_dict_create(); |
p_trie_file_dict = trie_dict_create(); |
| 42 |
if (p_trie_file_dict == NULL) |
if (p_trie_file_dict == NULL) |
| 43 |
{ |
{ |
| 48 |
return 0; |
return 0; |
| 49 |
} |
} |
| 50 |
|
|
| 51 |
void file_loader_cleanup(void) |
static void trie_file_dict_cleanup_cb(const char *filename, int64_t value) |
| 52 |
{ |
{ |
| 53 |
if (p_trie_file_dict != NULL) |
int shmid = (int)value; |
| 54 |
|
|
| 55 |
|
if (shmid != 0 && shmctl(shmid, IPC_RMID, NULL) == -1) |
| 56 |
{ |
{ |
| 57 |
trie_dict_destroy(p_trie_file_dict); |
log_error("shmctl(shmid=%d, IPC_RMID) error (%d)\n", shmid, errno); |
|
p_trie_file_dict = NULL; |
|
| 58 |
} |
} |
| 59 |
|
} |
| 60 |
|
|
| 61 |
if (p_file_mmap_pool != NULL) |
void file_loader_cleanup(void) |
| 62 |
|
{ |
| 63 |
|
if (p_trie_file_dict == NULL) |
| 64 |
{ |
{ |
| 65 |
for (int i = 0; i < file_mmap_count; i++) |
return; |
|
{ |
|
|
if (p_file_mmap_pool[i].p_data == NULL) |
|
|
{ |
|
|
continue; |
|
|
} |
|
|
|
|
|
if (munmap(p_file_mmap_pool[i].p_data, p_file_mmap_pool[i].size) < 0) |
|
|
{ |
|
|
log_error("munmap() error (%d)\n", errno); |
|
|
} |
|
|
} |
|
|
free(p_file_mmap_pool); |
|
|
p_file_mmap_pool = NULL; |
|
| 66 |
} |
} |
| 67 |
|
|
| 68 |
file_mmap_count = 0; |
trie_dict_traverse(p_trie_file_dict, trie_file_dict_cleanup_cb); |
| 69 |
file_mmap_free_index = -1; |
trie_dict_destroy(p_trie_file_dict); |
| 70 |
|
|
| 71 |
|
p_trie_file_dict = NULL; |
| 72 |
} |
} |
| 73 |
|
|
| 74 |
int load_file_mmap(const char *filename) |
int load_file(const char *filename) |
| 75 |
{ |
{ |
| 76 |
int fd; |
int fd; |
| 77 |
struct stat sb; |
struct stat sb; |
| 78 |
void *p_data; |
void *p_data; |
| 79 |
|
size_t data_len; |
| 80 |
|
int proj_id; |
| 81 |
|
key_t key; |
| 82 |
size_t size; |
size_t size; |
| 83 |
int file_mmap_index = 0; |
int shmid; |
| 84 |
|
void *p_shm; |
| 85 |
|
long line_total; |
| 86 |
|
long line_offsets[MAX_SPLIT_FILE_LINES]; |
| 87 |
|
long *p_line_offsets; |
| 88 |
|
int64_t shmid_old; |
| 89 |
|
|
| 90 |
if ((fd = open(filename, O_RDONLY)) < 0) |
if ((fd = open(filename, O_RDONLY)) < 0) |
| 91 |
{ |
{ |
| 99 |
return -1; |
return -1; |
| 100 |
} |
} |
| 101 |
|
|
| 102 |
size = (size_t)sb.st_size; |
data_len = (size_t)sb.st_size; |
| 103 |
|
p_data = mmap(NULL, data_len, PROT_READ, MAP_SHARED, fd, 0L); |
|
p_data = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0L); |
|
| 104 |
if (p_data == MAP_FAILED) |
if (p_data == MAP_FAILED) |
| 105 |
{ |
{ |
| 106 |
log_error("mmap() error (%d)\n", errno); |
log_error("mmap() error (%d)\n", errno); |
| 113 |
return -1; |
return -1; |
| 114 |
} |
} |
| 115 |
|
|
| 116 |
if (trie_dict_get(p_trie_file_dict, filename, (int64_t *)&file_mmap_index) == 0) // Not exist |
line_total = split_data_lines(p_data, SCREEN_COLS, line_offsets, MAX_SPLIT_FILE_LINES, 1, NULL); |
| 117 |
|
|
| 118 |
|
// Allocate shared memory |
| 119 |
|
proj_id = (int)(time(NULL) % getpid()); |
| 120 |
|
key = ftok(filename, proj_id); |
| 121 |
|
if (key == -1) |
| 122 |
{ |
{ |
| 123 |
if (file_mmap_free_index == -1) |
log_error("ftok(%s %d) error (%d)\n", filename, proj_id, errno); |
| 124 |
{ |
return -2; |
| 125 |
log_error("file_mmap_pool is depleted\n"); |
} |
|
return -3; |
|
|
} |
|
| 126 |
|
|
| 127 |
file_mmap_index = file_mmap_free_index; |
size = sizeof(struct shm_header_t) + data_len + 1 + sizeof(long) * (size_t)(line_total + 1); |
| 128 |
|
shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600); |
| 129 |
|
if (shmid == -1) |
| 130 |
|
{ |
| 131 |
|
log_error("shmget(size = %d) error (%d)\n", size, errno); |
| 132 |
|
return -3; |
| 133 |
|
} |
| 134 |
|
p_shm = shmat(shmid, NULL, 0); |
| 135 |
|
if (p_shm == (void *)-1) |
| 136 |
|
{ |
| 137 |
|
log_error("shmat(shmid=%d) error (%d)\n", shmid, errno); |
| 138 |
|
return -3; |
| 139 |
|
} |
| 140 |
|
|
| 141 |
if (trie_dict_set(p_trie_file_dict, filename, (int64_t)file_mmap_index) != 1) |
((struct shm_header_t *)p_shm)->shmid = shmid; |
| 142 |
{ |
((struct shm_header_t *)p_shm)->data_len = data_len; |
| 143 |
log_error("trie_dict_set(%s) error\n", filename); |
((struct shm_header_t *)p_shm)->line_total = line_total; |
| 144 |
|
memcpy((char *)p_shm + sizeof(struct shm_header_t), p_data, data_len); |
| 145 |
|
|
| 146 |
if (munmap(p_data, size) < 0) |
if (munmap(p_data, data_len) < 0) |
| 147 |
{ |
{ |
| 148 |
log_error("munmap() error (%d)\n", errno); |
log_error("munmap() error (%d)\n", errno); |
| 149 |
} |
return -2; |
| 150 |
|
} |
| 151 |
|
|
| 152 |
return -2; |
p_data = (char *)p_shm + sizeof(struct shm_header_t); |
| 153 |
} |
p_line_offsets = (long *)((char *)p_data + data_len + 1); |
| 154 |
|
memcpy(p_line_offsets, line_offsets, sizeof(long) * (size_t)(line_total + 1)); |
| 155 |
|
|
| 156 |
do |
if (shmdt(p_shm) == -1) |
| 157 |
{ |
{ |
| 158 |
file_mmap_free_index++; |
log_error("shmdt(shmid=%d) error (%d)\n", shmid, errno); |
| 159 |
if (file_mmap_free_index >= file_mmap_count) |
return -3; |
|
{ |
|
|
file_mmap_free_index = 0; |
|
|
} |
|
|
if (file_mmap_free_index == file_mmap_index) // loop |
|
|
{ |
|
|
file_mmap_free_index = -1; |
|
|
break; |
|
|
} |
|
|
} while (p_file_mmap_pool[file_mmap_free_index].p_data != NULL); |
|
| 160 |
} |
} |
| 161 |
else |
|
| 162 |
|
if (trie_dict_get(p_trie_file_dict, filename, &shmid_old) == 1) |
| 163 |
{ |
{ |
| 164 |
// Unload existing data |
if (shmctl((int)shmid_old, IPC_RMID, NULL) == -1) |
|
if (munmap(p_file_mmap_pool[file_mmap_index].p_data, p_file_mmap_pool[file_mmap_index].size) < 0) |
|
| 165 |
{ |
{ |
| 166 |
log_error("munmap() error (%d)\n", errno); |
log_error("shmctl(shmid=%d, IPC_RMID) error (%d)\n", (int)shmid_old, errno); |
| 167 |
|
return -2; |
| 168 |
} |
} |
| 169 |
} |
} |
| 170 |
|
|
| 171 |
p_file_mmap_pool[file_mmap_index].p_data = p_data; |
if (trie_dict_set(p_trie_file_dict, filename, (int64_t)shmid) != 1) |
| 172 |
p_file_mmap_pool[file_mmap_index].size = size; |
{ |
| 173 |
|
log_error("trie_dict_set(%s) error\n", filename); |
| 174 |
|
|
| 175 |
|
if (shmid != 0 && shmctl(shmid, IPC_RMID, NULL) == -1) |
| 176 |
|
{ |
| 177 |
|
log_error("shmctl(shmid=%d, IPC_RMID) error (%d)\n", shmid, errno); |
| 178 |
|
} |
| 179 |
|
|
| 180 |
p_file_mmap_pool[file_mmap_index].line_total = |
return -4; |
| 181 |
split_data_lines(p_data, SCREEN_COLS, p_file_mmap_pool[file_mmap_index].line_offsets, MAX_FILE_LINES); |
} |
| 182 |
|
|
| 183 |
return 0; |
return 0; |
| 184 |
} |
} |
| 185 |
|
|
| 186 |
int unload_file_mmap(const char *filename) |
int unload_file(const char *filename) |
| 187 |
{ |
{ |
| 188 |
int file_mmap_index = 0; |
int64_t shmid; |
| 189 |
|
|
| 190 |
if (trie_dict_get(p_trie_file_dict, filename, (int64_t *)&file_mmap_index) != 1) |
if (trie_dict_get(p_trie_file_dict, filename, &shmid) != 1) |
| 191 |
{ |
{ |
| 192 |
log_error("trie_dict_get(%s) not found\n", filename); |
log_error("trie_dict_get(%s) not found\n", filename); |
| 193 |
return -1; |
return -1; |
| 194 |
} |
} |
| 195 |
|
|
| 196 |
if (munmap(p_file_mmap_pool[file_mmap_index].p_data, p_file_mmap_pool[file_mmap_index].size) < 0) |
if (shmctl((int)shmid, IPC_RMID, NULL) == -1) |
| 197 |
{ |
{ |
| 198 |
log_error("munmap() error (%d)\n", errno); |
log_error("shmctl(shmid=%d, IPC_RMID) error (%d)\n", (int)shmid, errno); |
|
} |
|
|
|
|
|
p_file_mmap_pool[file_mmap_index].p_data = NULL; |
|
|
p_file_mmap_pool[file_mmap_index].size = 0L; |
|
|
|
|
|
if (file_mmap_free_index == -1) |
|
|
{ |
|
|
file_mmap_free_index = file_mmap_index; |
|
| 199 |
} |
} |
| 200 |
|
|
| 201 |
if (trie_dict_del(p_trie_file_dict, filename) != 1) |
if (trie_dict_del(p_trie_file_dict, filename) != 1) |
| 207 |
return 0; |
return 0; |
| 208 |
} |
} |
| 209 |
|
|
| 210 |
const FILE_MMAP *get_file_mmap(const char *filename) |
const void *get_file_shm_readonly(const char *filename, size_t *p_data_len, long *p_line_total, const void **pp_data, const long **pp_line_offsets) |
| 211 |
{ |
{ |
| 212 |
int file_mmap_index = file_mmap_free_index; |
int64_t shmid; |
| 213 |
|
const void *p_shm; |
| 214 |
|
|
| 215 |
if (p_file_mmap_pool == NULL || p_trie_file_dict == NULL) |
if (p_trie_file_dict == NULL) |
| 216 |
{ |
{ |
| 217 |
log_error("File loader not initialized\n"); |
log_error("File loader not initialized\n"); |
| 218 |
return NULL; |
return NULL; |
| 219 |
} |
} |
| 220 |
|
|
| 221 |
if (trie_dict_get(p_trie_file_dict, filename, (int64_t *)&file_mmap_index) != 1) // Not exist |
if (trie_dict_get(p_trie_file_dict, filename, &shmid) != 1) // Not exist |
| 222 |
{ |
{ |
| 223 |
log_error("trie_dict_get(%s) not found\n", filename); |
log_error("trie_dict_get(%s) not found\n", filename); |
| 224 |
return NULL; |
return NULL; |
| 225 |
} |
} |
| 226 |
|
|
| 227 |
return (&(p_file_mmap_pool[file_mmap_index])); |
p_shm = shmat((int)shmid, NULL, SHM_RDONLY); |
| 228 |
|
if (p_shm == (void *)-1) |
| 229 |
|
{ |
| 230 |
|
log_error("shmat(shmid=%d) error (%d)\n", (int)shmid, errno); |
| 231 |
|
return NULL; |
| 232 |
|
} |
| 233 |
|
|
| 234 |
|
*p_data_len = ((struct shm_header_t *)p_shm)->data_len; |
| 235 |
|
*p_line_total = ((struct shm_header_t *)p_shm)->line_total; |
| 236 |
|
*pp_data = (char *)p_shm + sizeof(struct shm_header_t); |
| 237 |
|
*pp_line_offsets = (const long *)((const char *)(*pp_data) + *p_data_len + 1); |
| 238 |
|
|
| 239 |
|
return p_shm; |
| 240 |
|
} |
| 241 |
|
|
| 242 |
|
int detach_file_shm(const void *p_shm) |
| 243 |
|
{ |
| 244 |
|
if (p_shm == NULL) |
| 245 |
|
{ |
| 246 |
|
return -2; |
| 247 |
|
} |
| 248 |
|
|
| 249 |
|
if (shmdt(p_shm) == -1) |
| 250 |
|
{ |
| 251 |
|
log_error("shmdt() error (%d)\n", errno); |
| 252 |
|
return -1; |
| 253 |
|
} |
| 254 |
|
|
| 255 |
|
return 0; |
| 256 |
} |
} |