/[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.84 by sysadm, Wed Nov 5 04:19:21 2025 UTC Revision 1.91 by sysadm, Thu Nov 20 01:02:15 2025 UTC
# Line 6  Line 6 
6   * Copyright (C) 2004-2025  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 "bbs.h"  #include "bbs.h"
14  #include "bbs_cmd.h"  #include "bbs_cmd.h"
15  #include "bbs_cmd.h"  #include "bbs_cmd.h"
# Line 17  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 38  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            size_t size;
48            void *p_shm;
49          FILE *fin;          FILE *fin;
50          int fin_line = 0;          int fin_line = 0;
51          char buffer[LINE_BUFFER_LEN];          char buffer[LINE_BUFFER_LEN];
# Line 52  int load_menu(MENU_SET *p_menu_set, cons Line 60  int load_menu(MENU_SET *p_menu_set, cons
60          MENU_ID menu_id;          MENU_ID menu_id;
61          MENU_ITEM_ID menu_item_id;          MENU_ITEM_ID menu_item_id;
62          MENU_SCREEN_ID screen_id;          MENU_SCREEN_ID screen_id;
63          int proj_id;  
64          key_t key;          if (p_menu_set == NULL || conf_file == NULL)
65          size_t size;          {
66          int retry_cnt;                  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 65  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 73  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 89  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 188  int load_menu(MENU_SET *p_menu_set, cons Line 203  int load_menu(MENU_SET *p_menu_set, cons
203                                          return -1;                                          return -1;
204                                  }                                  }
205                                  p = q;                                  p = q;
206                                  while (isalnum(*q) || *q == '_' || *q == '-')                                  while (isalnum((int)*q) || *q == '_' || *q == '-')
207                                  {                                  {
208                                          q++;                                          q++;
209                                  }                                  }
# Line 271  int load_menu(MENU_SET *p_menu_set, cons Line 286  int load_menu(MENU_SET *p_menu_set, cons
286                                                  else                                                  else
287                                                  {                                                  {
288                                                          q = p;                                                          q = p;
289                                                          while (isalnum(*q) || *q == '_' || *q == '-')                                                          while (isalnum((int)*q) || *q == '_' || *q == '-')
290                                                          {                                                          {
291                                                                  q++;                                                                  q++;
292                                                          }                                                          }
# Line 298  int load_menu(MENU_SET *p_menu_set, cons Line 313  int load_menu(MENU_SET *p_menu_set, cons
313                                                          return -1;                                                          return -1;
314                                                  }                                                  }
315                                                  p = q;                                                  p = q;
316                                                  while (isdigit(*q))                                                  while (isdigit((int)*q))
317                                                  {                                                  {
318                                                          q++;                                                          q++;
319                                                  }                                                  }
# Line 317  int load_menu(MENU_SET *p_menu_set, cons Line 332  int load_menu(MENU_SET *p_menu_set, cons
332                                                          return -1;                                                          return -1;
333                                                  }                                                  }
334                                                  p = q;                                                  p = q;
335                                                  while (isdigit(*q))                                                  while (isdigit((int)*q))
336                                                  {                                                  {
337                                                          q++;                                                          q++;
338                                                  }                                                  }
# Line 336  int load_menu(MENU_SET *p_menu_set, cons Line 351  int load_menu(MENU_SET *p_menu_set, cons
351                                                          return -1;                                                          return -1;
352                                                  }                                                  }
353                                                  p = q;                                                  p = q;
354                                                  while (isdigit(*q))                                                  while (isdigit((int)*q))
355                                                  {                                                  {
356                                                          q++;                                                          q++;
357                                                  }                                                  }
# Line 355  int load_menu(MENU_SET *p_menu_set, cons Line 370  int load_menu(MENU_SET *p_menu_set, cons
370                                                          return -1;                                                          return -1;
371                                                  }                                                  }
372                                                  p = q;                                                  p = q;
373                                                  while (isdigit(*q))                                                  while (isdigit((int)*q))
374                                                  {                                                  {
375                                                          q++;                                                          q++;
376                                                  }                                                  }
# Line 460  int load_menu(MENU_SET *p_menu_set, cons Line 475  int load_menu(MENU_SET *p_menu_set, cons
475                                                          return -1;                                                          return -1;
476                                                  }                                                  }
477                                                  p = q;                                                  p = q;
478                                                  while (isdigit(*q))                                                  while (isdigit((int)*q))
479                                                  {                                                  {
480                                                          q++;                                                          q++;
481                                                  }                                                  }
# Line 479  int load_menu(MENU_SET *p_menu_set, cons Line 494  int load_menu(MENU_SET *p_menu_set, cons
494                                                          return -1;                                                          return -1;
495                                                  }                                                  }
496                                                  p = q;                                                  p = q;
497                                                  while (isdigit(*q))                                                  while (isdigit((int)*q))
498                                                  {                                                  {
499                                                          q++;                                                          q++;
500                                                  }                                                  }
# Line 547  int load_menu(MENU_SET *p_menu_set, cons Line 562  int load_menu(MENU_SET *p_menu_set, cons
562                                                          return -1;                                                          return -1;
563                                                  }                                                  }
564                                                  p = q;                                                  p = q;
565                                                  while (isdigit(*q))                                                  while (isdigit((int)*q))
566                                                  {                                                  {
567                                                          q++;                                                          q++;
568                                                  }                                                  }
# Line 566  int load_menu(MENU_SET *p_menu_set, cons Line 581  int load_menu(MENU_SET *p_menu_set, cons
581                                                          return -1;                                                          return -1;
582                                                  }                                                  }
583                                                  p = q;                                                  p = q;
584                                                  while (isdigit(*q))                                                  while (isdigit((int)*q))
585                                                  {                                                  {
586                                                          q++;                                                          q++;
587                                                  }                                                  }
# Line 585  int load_menu(MENU_SET *p_menu_set, cons Line 600  int load_menu(MENU_SET *p_menu_set, cons
600                                                          return -1;                                                          return -1;
601                                                  }                                                  }
602                                                  p = q;                                                  p = q;
603                                                  while (isalnum(*q) || *q == '_' || *q == '-')                                                  while (isalnum((int)*q) || *q == '_' || *q == '-')
604                                                  {                                                  {
605                                                          q++;                                                          q++;
606                                                  }                                                  }
# Line 615  int load_menu(MENU_SET *p_menu_set, cons Line 630  int load_menu(MENU_SET *p_menu_set, cons
630                                                          return -1;                                                          return -1;
631                                                  }                                                  }
632                                                  p = q;                                                  p = q;
633                                                  while (isdigit(*q))                                                  while (isdigit((int)*q))
634                                                  {                                                  {
635                                                          q++;                                                          q++;
636                                                  }                                                  }
# Line 634  int load_menu(MENU_SET *p_menu_set, cons Line 649  int load_menu(MENU_SET *p_menu_set, cons
649                                                          return -1;                                                          return -1;
650                                                  }                                                  }
651                                                  p = q;                                                  p = q;
652                                                  while (isdigit(*q))                                                  while (isdigit((int)*q))
653                                                  {                                                  {
654                                                          q++;                                                          q++;
655                                                  }                                                  }
# Line 653  int load_menu(MENU_SET *p_menu_set, cons Line 668  int load_menu(MENU_SET *p_menu_set, cons
668                                                          return -1;                                                          return -1;
669                                                  }                                                  }
670                                                  p = q;                                                  p = q;
671                                                  while (isdigit(*q))                                                  while (isdigit((int)*q))
672                                                  {                                                  {
673                                                          q++;                                                          q++;
674                                                  }                                                  }
# Line 699  int load_menu(MENU_SET *p_menu_set, cons Line 714  int load_menu(MENU_SET *p_menu_set, cons
714                                  p_screen = get_menu_screen_by_id(p_menu_set, screen_id);                                  p_screen = get_menu_screen_by_id(p_menu_set, screen_id);
715    
716                                  q = p;                                  q = p;
717                                  while (isalnum(*q) || *q == '_' || *q == '-')                                  while (isalnum((int)*q) || *q == '_' || *q == '-')
718                                  {                                  {
719                                          q++;                                          q++;
720                                  }                                  }
# Line 1362  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 1381  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 1396  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 1421  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          void *p_shm;          if (p_menu_set == NULL)
   
         // 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 && mprotect(p_menu_set->p_reserved, p_menu_set->shm_size, PROT_READ) < 0)
1476            {
1477                    log_error("mprotect() error (%d)\n", errno);
1478                    return -2;
1479            }
1480    
1481          return 0;          return 0;
1482  }  }
1483    
1484  int detach_menu_shm(MENU_SET *p_menu_set)  int detach_menu_shm(MENU_SET *p_menu_set)
1485  {  {
1486            if (p_menu_set == NULL)
1487            {
1488                    log_error("NULL pointer error\n");
1489                    return -1;
1490            }
1491    
1492          p_menu_set->menu_count = 0;          p_menu_set->menu_count = 0;
1493          p_menu_set->menu_item_count = 0;          p_menu_set->menu_item_count = 0;
1494          p_menu_set->menu_screen_count = 0;          p_menu_set->menu_screen_count = 0;
# Line 1452  int detach_menu_shm(MENU_SET *p_menu_set Line 1503  int detach_menu_shm(MENU_SET *p_menu_set
1503          p_menu_set->p_menu_name_dict = NULL;          p_menu_set->p_menu_name_dict = NULL;
1504          p_menu_set->p_menu_screen_dict = NULL;          p_menu_set->p_menu_screen_dict = NULL;
1505    
1506          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)
1507          {          {
1508                  log_error("shmdt() error (%d)\n", errno);                  log_error("munmap() error (%d)\n", errno);
1509                  return -1;                  return -2;
1510          }          }
1511    
1512          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