/[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.10 by sysadm, Fri May 23 14:11:38 2025 UTC Revision 1.18 by sysadm, Tue Nov 4 13:49:51 2025 UTC
# Line 1  Line 1 
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 by 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>  #include <string.h>
17  #include <time.h>  #include <time.h>
18    #include <unistd.h>
19    #include <sys/ipc.h>
20  #include <sys/mman.h>  #include <sys/mman.h>
 #include <sys/stat.h>  
21  #include <sys/shm.h>  #include <sys/shm.h>
22  #include <sys/ipc.h>  #include <sys/stat.h>
   
 #define MAX_SPLIT_FILE_LINES 65536  
23    
24  struct shm_header_t  struct shm_header_t
25  {  {
# Line 60  int file_loader_init() Line 50  int file_loader_init()
50    
51  static void trie_file_dict_cleanup_cb(const char *filename, int64_t value)  static void trie_file_dict_cleanup_cb(const char *filename, int64_t value)
52  {  {
53          const void *p_shm = (const void *)value;          int shmid = (int)value;
         int shmid = *((int *)p_shm);  
   
         if (shmdt(p_shm) == -1)  
         {  
                 log_error("shmdt(shmid=%d) error (%d)\n", shmid, errno);  
         }  
54    
55          if (shmctl(shmid, IPC_RMID, NULL) == -1)          if (shmctl(shmid, IPC_RMID, NULL) == -1)
56          {          {
# Line 82  void file_loader_cleanup(void) Line 66  void file_loader_cleanup(void)
66          }          }
67    
68          trie_dict_traverse(p_trie_file_dict, trie_file_dict_cleanup_cb);          trie_dict_traverse(p_trie_file_dict, trie_file_dict_cleanup_cb);
   
69          trie_dict_destroy(p_trie_file_dict);          trie_dict_destroy(p_trie_file_dict);
70    
71          p_trie_file_dict = NULL;          p_trie_file_dict = NULL;
72  }  }
73    
74  int load_file_shm(const char *filename)  int load_file(const char *filename)
75  {  {
76          int fd;          int fd;
77          struct stat sb;          struct stat sb;
# Line 102  int load_file_shm(const char *filename) Line 86  int load_file_shm(const char *filename)
86          long line_offsets[MAX_SPLIT_FILE_LINES];          long line_offsets[MAX_SPLIT_FILE_LINES];
87          long *p_line_offsets;          long *p_line_offsets;
88          int64_t shmid_old;          int64_t shmid_old;
         void *p_shm_old;  
89    
90          if ((fd = open(filename, O_RDONLY)) < 0)          if ((fd = open(filename, O_RDONLY)) < 0)
91          {          {
# Line 130  int load_file_shm(const char *filename) Line 113  int load_file_shm(const char *filename)
113                  return -1;                  return -1;
114          }          }
115    
116          line_total = split_data_lines(p_data, SCREEN_COLS, line_offsets, MAX_SPLIT_FILE_LINES);          line_total = split_data_lines(p_data, SCREEN_COLS, line_offsets, MAX_SPLIT_FILE_LINES, 1, NULL);
         if (line_total >= MAX_SPLIT_FILE_LINES)  
         {  
                 log_error("split_data_lines() truncated over limit lines\n");  
         }  
117    
118          // Allocate shared memory          // Allocate shared memory
119          proj_id = (int)(time(NULL) % getpid());          proj_id = (int)(time(NULL) % getpid());
# Line 162  int load_file_shm(const char *filename) Line 141  int load_file_shm(const char *filename)
141          ((struct shm_header_t *)p_shm)->shmid = shmid;          ((struct shm_header_t *)p_shm)->shmid = shmid;
142          ((struct shm_header_t *)p_shm)->data_len = data_len;          ((struct shm_header_t *)p_shm)->data_len = data_len;
143          ((struct shm_header_t *)p_shm)->line_total = line_total;          ((struct shm_header_t *)p_shm)->line_total = line_total;
144          memcpy(p_shm + sizeof(struct shm_header_t), p_data, data_len);          memcpy((char *)p_shm + sizeof(struct shm_header_t), p_data, data_len);
145    
146          if (munmap(p_data, data_len) < 0)          if (munmap(p_data, data_len) < 0)
147          {          {
# Line 170  int load_file_shm(const char *filename) Line 149  int load_file_shm(const char *filename)
149                  return -2;                  return -2;
150          }          }
151    
152          p_data = p_shm + sizeof(struct shm_header_t);          p_data = (char *)p_shm + sizeof(struct shm_header_t);
153          p_line_offsets = p_data + data_len + 1;          p_line_offsets = (long *)((char *)p_data + data_len + 1);
154          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));
155    
156          // Remap shared memory in read-only mode          if (shmdt(p_shm) == -1)
         p_shm = shmat(shmid, p_shm, SHM_RDONLY | SHM_REMAP);  
         if (p_shm == (void *)-1)  
157          {          {
158                  log_error("shmat(shmid=%d) error (%d)\n", shmid, errno);                  log_error("shmdt(shmid=%d) error (%d)\n", shmid, errno);
159                  return -3;                  return -3;
160          }          }
161    
162          if (trie_dict_get(p_trie_file_dict, filename, (int64_t *)&p_shm_old) == 1)          if (trie_dict_get(p_trie_file_dict, filename, &shmid_old) == 1)
163          {          {
                 shmid_old = *((int *)p_shm_old);  
   
                 if (shmdt(p_shm_old) == -1)  
                 {  
                         log_error("shmdt(shmid=%d) error (%d)\n", shmid_old, errno);  
                         return -3;  
                 }  
   
164                  if (shmctl((int)shmid_old, IPC_RMID, NULL) == -1)                  if (shmctl((int)shmid_old, IPC_RMID, NULL) == -1)
165                  {                  {
166                          log_error("shmctl(shmid=%d, IPC_RMID) error (%d)\n", (int)shmid_old, errno);                          log_error("shmctl(shmid=%d, IPC_RMID) error (%d)\n", (int)shmid_old, errno);
# Line 199  int load_file_shm(const char *filename) Line 168  int load_file_shm(const char *filename)
168                  }                  }
169          }          }
170    
171          if (trie_dict_set(p_trie_file_dict, filename, (int64_t)p_shm) != 1)          if (trie_dict_set(p_trie_file_dict, filename, (int64_t)shmid) != 1)
172          {          {
173                  log_error("trie_dict_set(%s) error\n", filename);                  log_error("trie_dict_set(%s) error\n", filename);
174    
# Line 214  int load_file_shm(const char *filename) Line 183  int load_file_shm(const char *filename)
183          return 0;          return 0;
184  }  }
185    
186  int unload_file_shm(const char *filename)  int unload_file(const char *filename)
187  {  {
188          const void *p_shm;          int64_t shmid;
         int shmid;  
189    
190          if (trie_dict_get(p_trie_file_dict, filename, (int64_t *)&p_shm) != 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    
         shmid = *((int *)p_shm);  
   
         if (shmdt(p_shm) == -1)  
         {  
                 log_error("shmdt(shmid=%d) error (%d)\n", shmid, errno);  
         }  
   
196          if (shmctl((int)shmid, IPC_RMID, NULL) == -1)          if (shmctl((int)shmid, IPC_RMID, NULL) == -1)
197          {          {
198                  log_error("shmctl(shmid=%d, IPC_RMID) error (%d)\n", (int)shmid, errno);                  log_error("shmctl(shmid=%d, IPC_RMID) error (%d)\n", (int)shmid, errno);
# Line 246  int unload_file_shm(const char *filename Line 207  int unload_file_shm(const char *filename
207          return 0;          return 0;
208  }  }
209    
210  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)  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            int64_t shmid;
213          const void *p_shm;          const void *p_shm;
214    
215          if (p_trie_file_dict == NULL)          if (p_trie_file_dict == NULL)
# Line 256  const void *get_file_shm(const char *fil Line 218  const void *get_file_shm(const char *fil
218                  return NULL;                  return NULL;
219          }          }
220    
221          if (trie_dict_get(p_trie_file_dict, filename, (int64_t *)&p_shm) != 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            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;          *p_data_len = ((struct shm_header_t *)p_shm)->data_len;
235          *p_line_total = ((struct shm_header_t *)p_shm)->line_total;          *p_line_total = ((struct shm_header_t *)p_shm)->line_total;
236          *pp_data = p_shm + sizeof(struct shm_header_t);          *pp_data = (char *)p_shm + sizeof(struct shm_header_t);
237          *pp_line_offsets = *pp_data + *p_data_len + 1;          *pp_line_offsets = (const long *)((const char *)(*pp_data) + *p_data_len + 1);
238    
239          return p_shm;          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    }


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

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