/[LeafOK_CVS]/lbbs/src/file_loader.c
ViewVC logotype

Contents of /lbbs/src/file_loader.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.21 - (show annotations)
Tue Nov 11 00:28:05 2025 UTC (4 months ago) by sysadm
Branch: MAIN
Changes since 1.20: +4 -0 lines
Content type: text/x-csrc
Use config.h

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

webmaster@leafok.com
ViewVC Help
Powered by ViewVC 1.3.0-beta1