/[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.16 by sysadm, Wed Jul 2 03:08:10 2025 UTC Revision 1.22 by sysadm, Wed Nov 19 03:12:58 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  Leaflet <leaflet@leafok.com>
7     */
8  /***************************************************************************  
9   *                                                                         *  #ifdef HAVE_CONFIG_H
10   *   This program is free software; you can redistribute it and/or modify  *  #include "config.h"
11   *   it under the terms of the GNU General Public License as published by  *  #endif
  *   the Free Software Foundation; either version 3 of the License, or     *  
  *   (at your option) any later version.                                   *  
  *                                                                         *  
  ***************************************************************************/  
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          int fd;          int fd;
36          struct stat sb;          struct stat sb;
37          void *p_data;          void *p_data;
38          size_t data_len;          size_t data_len;
         int proj_id;  
         key_t key;  
39          size_t size;          size_t size;
         int shmid;  
40          void *p_shm;          void *p_shm;
41          long line_total;          long line_total;
42          long line_offsets[MAX_SPLIT_FILE_LINES];          long line_offsets[MAX_SPLIT_FILE_LINES];
43          long *p_line_offsets;          long *p_line_offsets;
44          int64_t shmid_old;  
45            if (filename == NULL)
46            {
47                    log_error("NULL pointer error\n");
48                    return -1;
49            }
50    
51          if ((fd = open(filename, O_RDONLY)) < 0)          if ((fd = open(filename, O_RDONLY)) < 0)
52          {          {
# Line 104  int load_file(const char *filename) Line 57  int load_file(const char *filename)
57          if (fstat(fd, &sb) < 0)          if (fstat(fd, &sb) < 0)
58          {          {
59                  log_error("fstat(fd) error (%d)\n", errno);                  log_error("fstat(fd) error (%d)\n", errno);
60                    close(fd);
61                  return -1;                  return -1;
62          }          }
63    
# Line 112  int load_file(const char *filename) Line 66  int load_file(const char *filename)
66          if (p_data == MAP_FAILED)          if (p_data == MAP_FAILED)
67          {          {
68                  log_error("mmap() error (%d)\n", errno);                  log_error("mmap() error (%d)\n", errno);
69                    close(fd);
70                  return -2;                  return -2;
71          }          }
72    
# Line 124  int load_file(const char *filename) Line 79  int load_file(const char *filename)
79          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);
80    
81          // Allocate shared memory          // Allocate shared memory
82          proj_id = (int)(time(NULL) % getpid());          size = sizeof(struct shm_header_t) + data_len + 1 + sizeof(long) * (size_t)(line_total + 1);
83          key = ftok(filename, proj_id);  
84          if (key == -1)          if (unload_file(filename) < 0)
85          {          {
                 log_error("ftok(%s %d) error (%d)\n", filename, proj_id, errno);  
86                  return -2;                  return -2;
87          }          }
88    
89          size = sizeof(struct shm_header_t) + data_len + 1 + sizeof(long) * (size_t)(line_total + 1);          strncpy(filepath, filename, sizeof(filepath) - 1);
90          shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);          filepath[sizeof(filepath) - 1] = '\0';
91          if (shmid == -1)  
92            if ((fd = shm_open(basename(filepath), O_CREAT | O_EXCL | O_RDWR, 0600)) == -1)
93          {          {
94                  log_error("shmget(size = %d) error (%d)\n", size, errno);                  log_error("shm_open(%s) error (%d)\n", basename(filepath), errno);
95                  return -3;                  return -2;
96          }          }
97          p_shm = shmat(shmid, NULL, 0);          if (ftruncate(fd, (off_t)size) == -1)
         if (p_shm == (void *)-1)  
98          {          {
99                  log_error("shmat(shmid=%d) error (%d)\n", shmid, errno);                  log_error("ftruncate(size=%d) error (%d)\n", size, errno);
100                  return -3;                  close(fd);
101                    return -2;
102          }          }
103    
104          ((struct shm_header_t *)p_shm)->shmid = shmid;          p_shm = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0L);
105            if (p_shm == MAP_FAILED)
106            {
107                    log_error("mmap() error (%d)\n", errno);
108                    close(fd);
109                    return -2;
110            }
111    
112            if (close(fd) < 0)
113            {
114                    log_error("close(fd) error (%d)\n", errno);
115                    return -1;
116            }
117    
118            ((struct shm_header_t *)p_shm)->shm_size = size;
119          ((struct shm_header_t *)p_shm)->data_len = data_len;          ((struct shm_header_t *)p_shm)->data_len = data_len;
120          ((struct shm_header_t *)p_shm)->line_total = line_total;          ((struct shm_header_t *)p_shm)->line_total = line_total;
121          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);
122    
123          if (munmap(p_data, data_len) < 0)          if (munmap(p_data, data_len) < 0)
124          {          {
125                  log_error("munmap() error (%d)\n", errno);                  log_error("munmap() error (%d)\n", errno);
126                    munmap(p_shm, size);
127                  return -2;                  return -2;
128          }          }
129    
130          p_data = p_shm + sizeof(struct shm_header_t);          p_data = (char *)p_shm + sizeof(struct shm_header_t);
131          p_line_offsets = p_data + data_len + 1;          p_line_offsets = (long *)((char *)p_data + data_len + 1);
132          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));
133    
134          if (shmdt(p_shm) == -1)          if (munmap(p_shm, size) < 0)
135          {          {
136                  log_error("shmdt(shmid=%d) error (%d)\n", shmid, errno);                  log_error("munmap() error (%d)\n", errno);
137                  return -3;                  return -2;
138          }          }
139    
140          if (trie_dict_get(p_trie_file_dict, filename, &shmid_old) == 1)          return 0;
141          {  }
142                  if (shmctl((int)shmid_old, IPC_RMID, NULL) == -1)  
143                  {  int unload_file(const char *filename)
144                          log_error("shmctl(shmid=%d, IPC_RMID) error (%d)\n", (int)shmid_old, errno);  {
145                          return -2;          char filepath[FILE_PATH_LEN];
                 }  
         }  
146    
147          if (trie_dict_set(p_trie_file_dict, filename, (int64_t)shmid) != 1)          if (filename == NULL)
148          {          {
149                  log_error("trie_dict_set(%s) error\n", filename);                  log_error("NULL pointer error\n");
150                    return -1;
151            }
152    
153                  if (shmctl(shmid, IPC_RMID, NULL) == -1)          strncpy(filepath, filename, sizeof(filepath) - 1);
154                  {          filepath[sizeof(filepath) - 1] = '\0';
                         log_error("shmctl(shmid=%d, IPC_RMID) error (%d)\n", shmid, errno);  
                 }  
155    
156                  return -4;          if (shm_unlink(basename(filepath)) == -1 && errno != ENOENT)
157            {
158                    log_error("shm_unlink(%s) error (%d)\n", basename(filepath), errno);
159                    return -2;
160          }          }
161    
162          return 0;          return 0;
163  }  }
164    
165  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)
166  {  {
167          int64_t shmid;          char filepath[FILE_PATH_LEN];
168            int fd;
169            void *p_shm = NULL;
170            struct stat sb;
171            size_t size;
172    
173          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)
174          {          {
175                  log_error("trie_dict_get(%s) not found\n", filename);                  log_error("NULL pointer error\n");
176                  return -1;                  return NULL;
177          }          }
178    
179          if (shmctl((int)shmid, IPC_RMID, NULL) == -1)          strncpy(filepath, filename, sizeof(filepath) - 1);
180            filepath[sizeof(filepath) - 1] = '\0';
181    
182            if ((fd = shm_open(basename(filepath), O_RDONLY, 0600)) == -1)
183          {          {
184                  log_error("shmctl(shmid=%d, IPC_RMID) error (%d)\n", (int)shmid, errno);                  log_error("shm_open(%s) error (%d)\n", basename(filepath), errno);
185                    return NULL;
186          }          }
187    
188          if (trie_dict_del(p_trie_file_dict, filename) != 1)          if (fstat(fd, &sb) < 0)
189          {          {
190                  log_error("trie_dict_del(%s) error\n", filename);                  log_error("fstat(fd) error (%d)\n", errno);
191                  return -2;                  close(fd);
192                    return NULL;
193          }          }
194    
195          return 0;          size = (size_t)sb.st_size;
 }  
196    
197  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)          p_shm = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0L);
198  {          if (p_shm == MAP_FAILED)
         int64_t shmid;  
         const void *p_shm;  
   
         if (p_trie_file_dict == NULL)  
199          {          {
200                  log_error("File loader not initialized\n");                  log_error("mmap() error (%d)\n", errno);
201                    close(fd);
202                  return NULL;                  return NULL;
203          }          }
204    
205          if (trie_dict_get(p_trie_file_dict, filename, &shmid) != 1) // Not exist          if (close(fd) < 0)
206          {          {
207                  log_error("trie_dict_get(%s) not found\n", filename);                  log_error("close(fd) error (%d)\n", errno);
208                  return NULL;                  return NULL;
209          }          }
210    
211          p_shm = shmat((int)shmid, NULL, SHM_RDONLY);          if (((struct shm_header_t *)p_shm)->shm_size != size)
         if (p_shm == (void *)-1)  
212          {          {
213                  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);
214                    munmap(p_shm, size);
215                  return NULL;                  return NULL;
216          }          }
217    
218          *p_data_len = ((struct shm_header_t *)p_shm)->data_len;          *p_data_len = ((struct shm_header_t *)p_shm)->data_len;
219          *p_line_total = ((struct shm_header_t *)p_shm)->line_total;          *p_line_total = ((struct shm_header_t *)p_shm)->line_total;
220          *pp_data = p_shm + sizeof(struct shm_header_t);          *pp_data = (char *)p_shm + sizeof(struct shm_header_t);
221          *pp_line_offsets = *pp_data + *p_data_len + 1;          *pp_line_offsets = (const long *)((const char *)(*pp_data) + *p_data_len + 1);
222    
223          return p_shm;          return p_shm;
224  }  }
225    
226  int detach_file_shm(const void *p_shm)  int detach_file_shm(void *p_shm)
227  {  {
228            size_t size;
229    
230          if (p_shm == NULL)          if (p_shm == NULL)
231          {          {
232                  return -2;                  return -1;
233          }          }
234    
235          if (shmdt(p_shm) == -1)          size = ((struct shm_header_t *)p_shm)->shm_size;
236    
237            if (munmap(p_shm, size) < 0)
238          {          {
239                  log_error("shmdt() error (%d)\n", errno);                  log_error("munmap() error (%d)\n", errno);
240                  return -1;                  return -2;
241          }          }
242    
243          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