/[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.9 - (show annotations)
Tue May 20 12:29:26 2025 UTC (9 months, 4 weeks ago) by sysadm
Branch: MAIN
Changes since 1.8: +14 -0 lines
Content type: text/x-csrc
Mount shm of loaded file in read-only mode for child process

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

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