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

Diff of /lbbs/src/file_loader.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

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


Legend:
Removed lines/characters  
Changed lines/characters
  Added lines/characters

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