/[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.9 by sysadm, Tue May 20 12:29:26 2025 UTC Revision 1.12 by sysadm, Thu May 29 09:44:01 2025 UTC
# Line 29  Line 29 
29  #include <sys/shm.h>  #include <sys/shm.h>
30  #include <sys/ipc.h>  #include <sys/ipc.h>
31    
 #define MAX_SPLIT_FILE_LINES 65536  
   
32  struct shm_header_t  struct shm_header_t
33  {  {
34          int shmid;          int shmid;
# Line 60  int file_loader_init() Line 58  int file_loader_init()
58    
59  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)
60  {  {
61          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);  
         }  
62    
63          if (shmctl(shmid, IPC_RMID, NULL) == -1)          if (shmctl(shmid, IPC_RMID, NULL) == -1)
64          {          {
# Line 82  void file_loader_cleanup(void) Line 74  void file_loader_cleanup(void)
74          }          }
75    
76          trie_dict_traverse(p_trie_file_dict, trie_file_dict_cleanup_cb);          trie_dict_traverse(p_trie_file_dict, trie_file_dict_cleanup_cb);
   
77          trie_dict_destroy(p_trie_file_dict);          trie_dict_destroy(p_trie_file_dict);
78    
79          p_trie_file_dict = NULL;          p_trie_file_dict = NULL;
80  }  }
81    
82  int load_file_shm(const char *filename)  int load_file(const char *filename)
83  {  {
84          int fd;          int fd;
85          struct stat sb;          struct stat sb;
# Line 102  int load_file_shm(const char *filename) Line 94  int load_file_shm(const char *filename)
94          long line_offsets[MAX_SPLIT_FILE_LINES];          long line_offsets[MAX_SPLIT_FILE_LINES];
95          long *p_line_offsets;          long *p_line_offsets;
96          int64_t shmid_old;          int64_t shmid_old;
         void *p_shm_old;  
97    
98          if ((fd = open(filename, O_RDONLY)) < 0)          if ((fd = open(filename, O_RDONLY)) < 0)
99          {          {
# Line 174  int load_file_shm(const char *filename) Line 165  int load_file_shm(const char *filename)
165          p_line_offsets = p_data + data_len + 1;          p_line_offsets = p_data + data_len + 1;
166          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));
167    
         // Re-mount shm in read-only mode  
168          if (shmdt(p_shm) == -1)          if (shmdt(p_shm) == -1)
169          {          {
170                  log_error("shmdt() error (%d)\n", errno);                  log_error("shmdt(shmid=%d) error (%d)\n", shmid, errno);
                 return -3;  
         }  
   
         p_shm = shmat(shmid, NULL, SHM_RDONLY);  
         if (p_shm == (void *)-1)  
         {  
                 log_error("shmat(shmid=%d) error (%d)\n", shmid, errno);  
171                  return -3;                  return -3;
172          }          }
173    
174          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)
175          {          {
                 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;  
                 }  
   
176                  if (shmctl((int)shmid_old, IPC_RMID, NULL) == -1)                  if (shmctl((int)shmid_old, IPC_RMID, NULL) == -1)
177                  {                  {
178                          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 205  int load_file_shm(const char *filename) Line 180  int load_file_shm(const char *filename)
180                  }                  }
181          }          }
182    
183          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)
184          {          {
185                  log_error("trie_dict_set(%s) error\n", filename);                  log_error("trie_dict_set(%s) error\n", filename);
186    
# Line 220  int load_file_shm(const char *filename) Line 195  int load_file_shm(const char *filename)
195          return 0;          return 0;
196  }  }
197    
198  int unload_file_shm(const char *filename)  int unload_file(const char *filename)
199  {  {
200          const void *p_shm;          int64_t shmid;
         int shmid;  
201    
202          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)
203          {          {
204                  log_error("trie_dict_get(%s) not found\n", filename);                  log_error("trie_dict_get(%s) not found\n", filename);
205                  return -1;                  return -1;
206          }          }
207    
         shmid = *((int *)p_shm);  
   
         if (shmdt(p_shm) == -1)  
         {  
                 log_error("shmdt(shmid=%d) error (%d)\n", shmid, errno);  
         }  
   
208          if (shmctl((int)shmid, IPC_RMID, NULL) == -1)          if (shmctl((int)shmid, IPC_RMID, NULL) == -1)
209          {          {
210                  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 252  int unload_file_shm(const char *filename Line 219  int unload_file_shm(const char *filename
219          return 0;          return 0;
220  }  }
221    
222  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)
223  {  {
224            int64_t shmid;
225          const void *p_shm;          const void *p_shm;
226    
227          if (p_trie_file_dict == NULL)          if (p_trie_file_dict == NULL)
# Line 262  const void *get_file_shm(const char *fil Line 230  const void *get_file_shm(const char *fil
230                  return NULL;                  return NULL;
231          }          }
232    
233          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
234          {          {
235                  log_error("trie_dict_get(%s) not found\n", filename);                  log_error("trie_dict_get(%s) not found\n", filename);
236                  return NULL;                  return NULL;
237          }          }
238    
239            p_shm = shmat((int)shmid, NULL, SHM_RDONLY);
240            if (p_shm == (void *)-1)
241            {
242                    log_error("shmat(shmid=%d) error (%d)\n", (int)shmid, errno);
243                    return NULL;
244            }
245    
246          *p_data_len = ((struct shm_header_t *)p_shm)->data_len;          *p_data_len = ((struct shm_header_t *)p_shm)->data_len;
247          *p_line_total = ((struct shm_header_t *)p_shm)->line_total;          *p_line_total = ((struct shm_header_t *)p_shm)->line_total;
248          *pp_data = p_shm + sizeof(struct shm_header_t);          *pp_data = p_shm + sizeof(struct shm_header_t);
# Line 275  const void *get_file_shm(const char *fil Line 250  const void *get_file_shm(const char *fil
250    
251          return p_shm;          return p_shm;
252  }  }
253    
254    int detach_file_shm(const void *p_shm)
255    {
256            if (p_shm == NULL)
257            {
258                    return -2;
259            }
260    
261            if (shmdt(p_shm) == -1)
262            {
263                    log_error("shmdt() error (%d)\n", errno);
264                    return -1;
265            }
266    
267            return 0;
268    }


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

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