/[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.3 - (show annotations)
Sat May 17 05:54:42 2025 UTC (10 months ago) by sysadm
Branch: MAIN
Changes since 1.2: +106 -102 lines
Content type: text/x-csrc
Refine

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 static TRIE_NODE *p_trie_file_dict = NULL;
35
36 int file_loader_init()
37 {
38 if (p_trie_file_dict != NULL)
39 {
40 log_error("File loader already initialized\n");
41 return -1;
42 }
43
44 p_trie_file_dict = trie_dict_create();
45 if (p_trie_file_dict == NULL)
46 {
47 log_error("trie_dict_create() error\n");
48 return -2;
49 }
50
51 return 0;
52 }
53
54 static void trie_file_dict_cleanup_cb(const char *filename, int64_t shmid)
55 {
56 log_std("Cleanup: %s %ld\n", filename, shmid);
57 if (shmctl((int)shmid, IPC_RMID, NULL) == -1)
58 {
59 log_error("shmctl(shmid=%d, IPC_RMID) error (%d)\n", (int)shmid, errno);
60 }
61 }
62
63 void file_loader_cleanup(void)
64 {
65 if (p_trie_file_dict == NULL)
66 {
67 return;
68 }
69
70 trie_dict_traverse(p_trie_file_dict, trie_file_dict_cleanup_cb);
71 trie_dict_destroy(p_trie_file_dict);
72 p_trie_file_dict = NULL;
73 }
74
75 int load_file_shm(const char *filename)
76 {
77 int fd;
78 struct stat sb;
79 void *p_data;
80 size_t data_len;
81 int proj_id;
82 key_t key;
83 size_t size;
84 int shmid;
85 int64_t shmid_old;
86 void *p_shm;
87 long line_total;
88 long line_offsets[MAX_SPLIT_FILE_LINES];
89 long *p_line_offsets;
90
91 if ((fd = open(filename, O_RDONLY)) < 0)
92 {
93 log_error("open(%s) error (%d)\n", filename, errno);
94 return -1;
95 }
96
97 if (fstat(fd, &sb) < 0)
98 {
99 log_error("fstat(fd) error (%d)\n", errno);
100 return -1;
101 }
102
103 data_len = (size_t)sb.st_size;
104 p_data = mmap(NULL, data_len, PROT_READ, MAP_SHARED, fd, 0L);
105 if (p_data == MAP_FAILED)
106 {
107 log_error("mmap() error (%d)\n", errno);
108 return -2;
109 }
110
111 if (close(fd) < 0)
112 {
113 log_error("close(fd) error (%d)\n", errno);
114 return -1;
115 }
116
117 line_total = split_data_lines(p_data, SCREEN_COLS, line_offsets, MAX_SPLIT_FILE_LINES);
118 if (line_total >= MAX_SPLIT_FILE_LINES)
119 {
120 log_error("split_data_lines() truncated over limit lines\n");
121 }
122
123 // Allocate shared memory
124 proj_id = (int)(time(NULL) % getpid());
125 key = ftok(filename, proj_id);
126 if (key == -1)
127 {
128 log_error("ftok(%s %d) error (%d)\n", filename, proj_id, errno);
129 return -2;
130 }
131
132 size = sizeof(data_len) + sizeof(line_total) + data_len + 1 + sizeof(long) * (size_t)(line_total + 1);
133 shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);
134 if (shmid == -1)
135 {
136 log_error("shmget(size = %d) error (%d)\n", size, errno);
137 return -3;
138 }
139 p_shm = shmat(shmid, NULL, 0);
140 if (p_shm == (void *)-1)
141 {
142 log_error("shmat() error (%d)\n", errno);
143 return -3;
144 }
145
146 *((size_t *)p_shm) = data_len;
147 *((long *)(p_shm + sizeof(data_len))) = line_total;
148 memcpy(p_shm + sizeof(data_len) + sizeof(line_total), 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 = p_shm + sizeof(data_len) + sizeof(line_total);
157 p_line_offsets = 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() error (%d)\n", 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 (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_shm(const char *filename)
191 {
192 int64_t shmid = 0;
193
194 if (trie_dict_get(p_trie_file_dict, filename, (int64_t *)&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(const char *filename)
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() error (%d)\n", errno);
235 return NULL;
236 }
237
238 return p_shm;
239 }

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