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

Diff of /lbbs/src/menu.c

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

Revision 1.88 by sysadm, Tue Nov 18 15:15:18 2025 UTC Revision 1.89 by sysadm, Wed Nov 19 14:47:08 2025 UTC
# Line 21  Line 21 
21  #include "user_priv.h"  #include "user_priv.h"
22  #include <ctype.h>  #include <ctype.h>
23  #include <errno.h>  #include <errno.h>
24    #include <fcntl.h>
25  #include <stdio.h>  #include <stdio.h>
26  #include <stdlib.h>  #include <stdlib.h>
27  #include <string.h>  #include <string.h>
28  #include <unistd.h>  #include <unistd.h>
29  #include <sys/ipc.h>  #include <sys/mman.h>
30  #include <sys/shm.h>  #include <sys/stat.h>
31    
32  enum _menu_constant_t  enum _menu_constant_t
33  {  {
34          MENU_SET_RESERVED_LENGTH = sizeof(int16_t) * 4,          MENU_SET_RESERVED_LENGTH = sizeof(int16_t) * 4,
         MENU_SHMGET_RETRY_LIMIT = 10,  
35  };  };
36    
37  static const char MENU_CONF_DELIM_WITH_SPACE[] = " ,\t\r\n";  static const char MENU_CONF_DELIM_WITH_SPACE[] = " ,\t\r\n";
# Line 42  MENU_SET top10_menu; Line 42  MENU_SET top10_menu;
42    
43  int load_menu(MENU_SET *p_menu_set, const char *conf_file)  int load_menu(MENU_SET *p_menu_set, const char *conf_file)
44  {  {
45            char filepath[FILE_PATH_LEN];
46            int fd;
47            void *p_shm = NULL;
48          FILE *fin;          FILE *fin;
49          int fin_line = 0;          int fin_line = 0;
50          char buffer[LINE_BUFFER_LEN];          char buffer[LINE_BUFFER_LEN];
# Line 56  int load_menu(MENU_SET *p_menu_set, cons Line 59  int load_menu(MENU_SET *p_menu_set, cons
59          MENU_ID menu_id;          MENU_ID menu_id;
60          MENU_ITEM_ID menu_item_id;          MENU_ITEM_ID menu_item_id;
61          MENU_SCREEN_ID screen_id;          MENU_SCREEN_ID screen_id;
         int proj_id;  
         key_t key;  
62          size_t size;          size_t size;
63          int retry_cnt;  
64            if (p_menu_set == NULL || conf_file == NULL)
65            {
66                    log_error("NULL pointer error\n");
67                    return -1;
68            }
69    
70          // Initialize the data structure          // Initialize the data structure
71          memset(p_menu_set, 0, sizeof(*p_menu_set));          memset(p_menu_set, 0, sizeof(*p_menu_set));
# Line 69  int load_menu(MENU_SET *p_menu_set, cons Line 75  int load_menu(MENU_SET *p_menu_set, cons
75          if (p_menu_set->p_menu_name_dict == NULL)          if (p_menu_set->p_menu_name_dict == NULL)
76          {          {
77                  log_error("trie_dict_create() error\n");                  log_error("trie_dict_create() error\n");
78                  return -3;                  return -1;
79          }          }
80    
81          // Use trie_dict to search screen_id by menu screen name          // Use trie_dict to search screen_id by menu screen name
# Line 77  int load_menu(MENU_SET *p_menu_set, cons Line 83  int load_menu(MENU_SET *p_menu_set, cons
83          if (p_menu_set->p_menu_screen_dict == NULL)          if (p_menu_set->p_menu_screen_dict == NULL)
84          {          {
85                  log_error("trie_dict_create() error\n");                  log_error("trie_dict_create() error\n");
86                  return -3;                  return -1;
87          }          }
88    
89          if ((fin = fopen(conf_file, "r")) == NULL)          if ((fin = fopen(conf_file, "r")) == NULL)
# Line 93  int load_menu(MENU_SET *p_menu_set, cons Line 99  int load_menu(MENU_SET *p_menu_set, cons
99                     sizeof(MENU_SCREEN) * MAX_MENUS +                     sizeof(MENU_SCREEN) * MAX_MENUS +
100                     MAX_MENU_SCR_BUF_LENGTH * MAX_MENUS;                     MAX_MENU_SCR_BUF_LENGTH * MAX_MENUS;
101    
102          proj_id = (int)(time(NULL) % getpid());          strncpy(filepath, conf_file, sizeof(filepath) - 1);
103          retry_cnt = 0;          filepath[sizeof(filepath) - 1] = '\0';
104            snprintf(p_menu_set->shm_name, sizeof(p_menu_set->shm_name), "/MENU_SHM_%s", basename(filepath));
105    
106          do          if (shm_unlink(p_menu_set->shm_name) == -1 && errno != ENOENT)
107          {          {
108                  key = ftok(conf_file, proj_id + retry_cnt);                  log_error("shm_unlink(%s) error (%d)\n", p_menu_set->shm_name, errno);
109                  if (key == -1)                  return -2;
110                  {          }
                         log_error("ftok(%s %d) error (%d)\n", conf_file, proj_id + retry_cnt, errno);  
                         return -3;  
                 }  
111    
112                  p_menu_set->shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);          if ((fd = shm_open(p_menu_set->shm_name, O_CREAT | O_EXCL | O_RDWR, 0600)) == -1)
113                  if (p_menu_set->shmid == -1)          {
114                  {                  log_error("shm_open(%s) error (%d)\n", p_menu_set->shm_name, errno);
115                          if (errno != EEXIST || retry_cnt + 1 >= MENU_SHMGET_RETRY_LIMIT)                  return -2;
116                          {          }
117                                  log_error("shmget(conf_file=%s, size=%d) error (%d) %d times\n",          if (ftruncate(fd, (off_t)size) == -1)
118                                                    conf_file, size, errno, retry_cnt + 1);          {
119                                  return -3;                  log_error("ftruncate(size=%d) error (%d)\n", size, errno);
120                          }                  close(fd);
121                          log_error("shmget(conf_file=%s, proj_id=%d, key=0x%x, size=%d) error (%d), retry ...\n",                  return -2;
122                                            conf_file, proj_id + retry_cnt, key, size, errno);          }
                         retry_cnt++;  
                 }  
         } while (p_menu_set->shmid == -1);  
123    
124          p_menu_set->p_reserved = shmat(p_menu_set->shmid, NULL, 0);          p_shm = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0L);
125          if (p_menu_set->p_reserved == (void *)-1)          if (p_shm == MAP_FAILED)
126          {          {
127                  log_error("shmat() error (%d)\n", errno);                  log_error("mmap() error (%d)\n", errno);
128                  return -3;                  close(fd);
129                    return -2;
130            }
131    
132            if (close(fd) < 0)
133            {
134                    log_error("close(fd) error (%d)\n", errno);
135                    return -1;
136          }          }
137    
138            p_menu_set->shm_size = size;
139            p_menu_set->p_reserved = p_shm;
140    
141          p_menu_set->p_menu_pool = (char *)(p_menu_set->p_reserved) + MENU_SET_RESERVED_LENGTH;          p_menu_set->p_menu_pool = (char *)(p_menu_set->p_reserved) + MENU_SET_RESERVED_LENGTH;
142          p_menu_set->p_menu_item_pool = (char *)(p_menu_set->p_menu_pool) + sizeof(MENU) * MAX_MENUS;          p_menu_set->p_menu_item_pool = (char *)(p_menu_set->p_menu_pool) + sizeof(MENU) * MAX_MENUS;
143          p_menu_set->p_menu_screen_pool = (char *)(p_menu_set->p_menu_item_pool) + sizeof(MENU_ITEM) * MAX_MENUITEMS;          p_menu_set->p_menu_screen_pool = (char *)(p_menu_set->p_menu_item_pool) + sizeof(MENU_ITEM) * MAX_MENUITEMS;
# Line 1366  int menu_control(MENU_SET *p_menu_set, i Line 1377  int menu_control(MENU_SET *p_menu_set, i
1377    
1378  int unload_menu(MENU_SET *p_menu_set)  int unload_menu(MENU_SET *p_menu_set)
1379  {  {
         int shmid;  
   
1380          if (p_menu_set == NULL)          if (p_menu_set == NULL)
1381          {          {
1382                    log_error("NULL pointer error\n");
1383                  return -1;                  return -1;
1384          }          }
1385    
# Line 1385  int unload_menu(MENU_SET *p_menu_set) Line 1395  int unload_menu(MENU_SET *p_menu_set)
1395                  p_menu_set->p_menu_screen_dict = NULL;                  p_menu_set->p_menu_screen_dict = NULL;
1396          }          }
1397    
         shmid = p_menu_set->shmid;  
   
1398          detach_menu_shm(p_menu_set);          detach_menu_shm(p_menu_set);
1399    
1400          if (shmid != 0 && shmctl(shmid, IPC_RMID, NULL) == -1)          if (shm_unlink(p_menu_set->shm_name) == -1 && errno != ENOENT)
1401          {          {
1402                  log_error("shmctl(shmid=%d, IPC_RMID) error (%d)\n", shmid, errno);                  log_error("shm_unlink(%s) error (%d)\n", p_menu_set->shm_name, errno);
1403                  return -1;                  return -2;
1404          }          }
1405    
1406          return 0;          return 0;
# Line 1400  int unload_menu(MENU_SET *p_menu_set) Line 1408  int unload_menu(MENU_SET *p_menu_set)
1408    
1409  int get_menu_shm_readonly(MENU_SET *p_menu_set)  int get_menu_shm_readonly(MENU_SET *p_menu_set)
1410  {  {
1411            int fd;
1412          void *p_shm;          void *p_shm;
1413            struct stat sb;
1414            size_t size;
1415    
1416          p_shm = shmat(p_menu_set->shmid, NULL, SHM_RDONLY);          if (p_menu_set == NULL)
         if (p_shm == (void *)-1)  
1417          {          {
1418                  log_error("shmat(menu_shm shmid = %d) error (%d)\n", p_menu_set->shmid, errno);                  log_error("NULL pointer error\n");
1419                  return -1;                  return -1;
1420          }          }
1421    
1422            if ((fd = shm_open(p_menu_set->shm_name, O_RDONLY, 0600)) == -1)
1423            {
1424                    log_error("shm_open(%s) error (%d)\n", p_menu_set->shm_name, errno);
1425                    return -2;
1426            }
1427    
1428            if (fstat(fd, &sb) < 0)
1429            {
1430                    log_error("fstat(fd) error (%d)\n", errno);
1431                    close(fd);
1432                    return -2;
1433            }
1434    
1435            size = (size_t)sb.st_size;
1436    
1437            p_shm = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0L);
1438            if (p_shm == MAP_FAILED)
1439            {
1440                    log_error("mmap() error (%d)\n", errno);
1441                    close(fd);
1442                    return -2;
1443            }
1444    
1445            if (close(fd) < 0)
1446            {
1447                    log_error("close(fd) error (%d)\n", errno);
1448                    return -1;
1449            }
1450    
1451            p_menu_set->shm_size = size;
1452          p_menu_set->p_reserved = p_shm;          p_menu_set->p_reserved = p_shm;
1453    
1454          p_menu_set->p_menu_pool = (char *)(p_menu_set->p_reserved) + MENU_SET_RESERVED_LENGTH;          p_menu_set->p_menu_pool = (char *)(p_menu_set->p_reserved) + MENU_SET_RESERVED_LENGTH;
1455          p_menu_set->p_menu_item_pool = (char *)(p_menu_set->p_menu_pool) + sizeof(MENU) * MAX_MENUS;          p_menu_set->p_menu_item_pool = (char *)(p_menu_set->p_menu_pool) + sizeof(MENU) * MAX_MENUS;
1456          p_menu_set->p_menu_screen_pool = (char *)(p_menu_set->p_menu_item_pool) + sizeof(MENU_ITEM) * MAX_MENUITEMS;          p_menu_set->p_menu_screen_pool = (char *)(p_menu_set->p_menu_item_pool) + sizeof(MENU_ITEM) * MAX_MENUITEMS;
# Line 1425  int get_menu_shm_readonly(MENU_SET *p_me Line 1466  int get_menu_shm_readonly(MENU_SET *p_me
1466    
1467  int set_menu_shm_readonly(MENU_SET *p_menu_set)  int set_menu_shm_readonly(MENU_SET *p_menu_set)
1468  {  {
1469  #ifndef __CYGWIN__          if (p_menu_set == NULL)
         void *p_shm;  
   
         // Remap shared memory in read-only mode  
         p_shm = shmat(p_menu_set->shmid, p_menu_set->p_reserved, SHM_RDONLY | SHM_REMAP);  
         if (p_shm == (void *)-1)  
1470          {          {
1471                  log_error("shmat(menu_shm shmid = %d) error (%d)\n", p_menu_set->shmid, errno);                  log_error("NULL pointer error\n");
1472                  return -1;                  return -1;
1473          }          }
1474    
1475          p_menu_set->p_reserved = p_shm;          if (p_menu_set->p_reserved != NULL && munmap(p_menu_set->p_reserved, p_menu_set->shm_size) < 0)
1476  #endif          {
1477                    log_error("munmap() error (%d)\n", errno);
1478                    return -2;
1479            }
1480    
1481            if (get_menu_shm_readonly(p_menu_set) < 0)
1482            {
1483                    log_error("get_menu_shm_readonly() error\n");
1484                    return -3;
1485            }
1486    
1487          return 0;          return 0;
1488  }  }
1489    
1490  int detach_menu_shm(MENU_SET *p_menu_set)  int detach_menu_shm(MENU_SET *p_menu_set)
1491  {  {
1492            if (p_menu_set == NULL)
1493            {
1494                    log_error("NULL pointer error\n");
1495                    return -1;
1496            }
1497    
1498          p_menu_set->menu_count = 0;          p_menu_set->menu_count = 0;
1499          p_menu_set->menu_item_count = 0;          p_menu_set->menu_item_count = 0;
1500          p_menu_set->menu_screen_count = 0;          p_menu_set->menu_screen_count = 0;
# Line 1458  int detach_menu_shm(MENU_SET *p_menu_set Line 1509  int detach_menu_shm(MENU_SET *p_menu_set
1509          p_menu_set->p_menu_name_dict = NULL;          p_menu_set->p_menu_name_dict = NULL;
1510          p_menu_set->p_menu_screen_dict = NULL;          p_menu_set->p_menu_screen_dict = NULL;
1511    
1512          if (p_menu_set->p_reserved != NULL && shmdt(p_menu_set->p_reserved) == -1)          if (p_menu_set->p_reserved != NULL && munmap(p_menu_set->p_reserved, p_menu_set->shm_size) < 0)
1513          {          {
1514                  log_error("shmdt() error (%d)\n", errno);                  log_error("munmap() error (%d)\n", errno);
1515                  return -1;                  return -2;
1516          }          }
1517    
1518          p_menu_set->p_reserved = NULL;          p_menu_set->p_reserved = NULL;


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

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