/[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.75 by sysadm, Sat Oct 18 12:06:10 2025 UTC Revision 1.77 by sysadm, Wed Oct 29 05:30:05 2025 UTC
# Line 38  Line 38 
38    
39  #define MENU_SET_RESERVED_LENGTH (sizeof(int16_t) * 4)  #define MENU_SET_RESERVED_LENGTH (sizeof(int16_t) * 4)
40    
41    #define MENU_SHMGET_RETRY_LIMIT 3
42    
43  MENU_SET bbs_menu;  MENU_SET bbs_menu;
44  MENU_SET top10_menu;  MENU_SET top10_menu;
45    
# Line 60  int load_menu(MENU_SET *p_menu_set, cons Line 62  int load_menu(MENU_SET *p_menu_set, cons
62          int proj_id;          int proj_id;
63          key_t key;          key_t key;
64          size_t size;          size_t size;
65            int retry_cnt;
66    
67          // Initialize the data structure          // Initialize the data structure
68          memset(p_menu_set, 0, sizeof(*p_menu_set));          memset(p_menu_set, 0, sizeof(*p_menu_set));
# Line 87  int load_menu(MENU_SET *p_menu_set, cons Line 90  int load_menu(MENU_SET *p_menu_set, cons
90          }          }
91    
92          // Allocate shared memory          // Allocate shared memory
         proj_id = (int)(time(NULL) % getpid());  
         key = ftok(conf_file, proj_id);  
         if (key == -1)  
         {  
                 log_error("ftok(%s %d) error (%d)\n", conf_file, proj_id, errno);  
                 return -2;  
         }  
   
93          size = MENU_SET_RESERVED_LENGTH +          size = MENU_SET_RESERVED_LENGTH +
94                     sizeof(MENU) * MAX_MENUS +                     sizeof(MENU) * MAX_MENUS +
95                     sizeof(MENU_ITEM) * MAX_MENUITEMS +                     sizeof(MENU_ITEM) * MAX_MENUITEMS +
96                     sizeof(MENU_SCREEN) * MAX_MENUS +                     sizeof(MENU_SCREEN) * MAX_MENUS +
97                     MAX_MENU_SCR_BUF_LENGTH * MAX_MENUS;                     MAX_MENU_SCR_BUF_LENGTH * MAX_MENUS;
98          p_menu_set->shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);  
99          if (p_menu_set->shmid == -1)          proj_id = (int)(time(NULL) % getpid());
100            retry_cnt = 0;
101    
102            do
103          {          {
104                  log_error("shmget(conf_file=%s, proj_id=%d, size = %d) error (%d)\n", conf_file, proj_id, size, errno);                  key = ftok(conf_file, proj_id + retry_cnt);
105                  return -3;                  if (key == -1)
106          }                  {
107                            log_error("ftok(%s %d) error (%d)\n", conf_file, proj_id, errno);
108                            return -2;
109                    }
110    
111                    p_menu_set->shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);
112    
113                    if (p_menu_set->shmid == -1)
114                    {
115                            if (errno != EEXIST || retry_cnt + 1 >= MENU_SHMGET_RETRY_LIMIT)
116                            {
117                                    log_error("shmget(conf_file=%s, size=%d) error (%d) %d times\n",
118                                                      conf_file, size, errno, retry_cnt + 1);
119                                    break;
120                            }
121    #ifdef _DEBUG
122                            log_error("shmget(conf_file=%s, proj_id=%d, key=0x%x, size=%d) error (%d), retry ...\n",
123                                              conf_file, proj_id + retry_cnt, key, size, errno);
124    #endif                                    
125                            retry_cnt++;
126                    }
127            } while (p_menu_set->shmid == -1);
128    
129          p_menu_set->p_reserved = shmat(p_menu_set->shmid, NULL, 0);          p_menu_set->p_reserved = shmat(p_menu_set->shmid, NULL, 0);
130          if (p_menu_set->p_reserved == (void *)-1)          if (p_menu_set->p_reserved == (void *)-1)
131          {          {
132                  log_error("shmat() error (%d)\n", errno);                  log_error("shmat() error (%d)\n", errno);
133                  return -3;                  return -3;
134          }          }
135    
136          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;
137          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;
138          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;


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

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