--- lbbs/src/menu.c 2025/10/13 05:30:33 1.72 +++ lbbs/src/menu.c 2025/11/04 14:30:44 1.82 @@ -1,18 +1,10 @@ -/*************************************************************************** - menu.c - description - ------------------- - Copyright : (C) 2004-2025 by Leaflet - Email : leaflet@leafok.com - ***************************************************************************/ - -/*************************************************************************** - * * - * This program is free software; you can redistribute it and/or modify * - * it under the terms of the GNU General Public License as published by * - * the Free Software Foundation; either version 3 of the License, or * - * (at your option) any later version. * - * * - ***************************************************************************/ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +/* + * menu + * - configurable user interactive menu feature + * + * Copyright (C) 2004-2025 by Leaflet + */ #include "bbs.h" #include "bbs_cmd.h" @@ -38,7 +30,10 @@ #define MENU_SET_RESERVED_LENGTH (sizeof(int16_t) * 4) +#define MENU_SHMGET_RETRY_LIMIT 10 + MENU_SET bbs_menu; +MENU_SET top10_menu; int load_menu(MENU_SET *p_menu_set, const char *conf_file) { @@ -59,6 +54,7 @@ int load_menu(MENU_SET *p_menu_set, cons int proj_id; key_t key; size_t size; + int retry_cnt; // Initialize the data structure memset(p_menu_set, 0, sizeof(*p_menu_set)); @@ -86,35 +82,50 @@ int load_menu(MENU_SET *p_menu_set, cons } // 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; - } - size = MENU_SET_RESERVED_LENGTH + sizeof(MENU) * MAX_MENUS + sizeof(MENU_ITEM) * MAX_MENUITEMS + sizeof(MENU_SCREEN) * MAX_MENUS + MAX_MENU_SCR_BUF_LENGTH * MAX_MENUS; - p_menu_set->shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600); - if (p_menu_set->shmid == -1) + + proj_id = (int)(time(NULL) % getpid()); + retry_cnt = 0; + + do { - log_error("shmget(size = %d) error (%d)\n", size, errno); - return -3; - } + key = ftok(conf_file, proj_id + retry_cnt); + if (key == -1) + { + log_error("ftok(%s %d) error (%d)\n", conf_file, proj_id + retry_cnt, errno); + return -3; + } + + p_menu_set->shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600); + if (p_menu_set->shmid == -1) + { + if (errno != EEXIST || retry_cnt + 1 >= MENU_SHMGET_RETRY_LIMIT) + { + log_error("shmget(conf_file=%s, size=%d) error (%d) %d times\n", + conf_file, size, errno, retry_cnt + 1); + return -3; + } + log_error("shmget(conf_file=%s, proj_id=%d, key=0x%x, size=%d) error (%d), retry ...\n", + conf_file, proj_id + retry_cnt, key, size, errno); + retry_cnt++; + } + } while (p_menu_set->shmid == -1); + p_menu_set->p_reserved = shmat(p_menu_set->shmid, NULL, 0); if (p_menu_set->p_reserved == (void *)-1) { log_error("shmat() error (%d)\n", errno); return -3; } - p_menu_set->p_menu_pool = p_menu_set->p_reserved + MENU_SET_RESERVED_LENGTH; - p_menu_set->p_menu_item_pool = p_menu_set->p_menu_pool + sizeof(MENU) * MAX_MENUS; - p_menu_set->p_menu_screen_pool = p_menu_set->p_menu_item_pool + sizeof(MENU_ITEM) * MAX_MENUITEMS; - p_menu_set->p_menu_screen_buf = p_menu_set->p_menu_screen_pool + sizeof(MENU_SCREEN) * MAX_MENUS; + + p_menu_set->p_menu_pool = (char *)(p_menu_set->p_reserved) + MENU_SET_RESERVED_LENGTH; + p_menu_set->p_menu_item_pool = (char *)(p_menu_set->p_menu_pool) + sizeof(MENU) * MAX_MENUS; + 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_buf = (char *)(p_menu_set->p_menu_screen_pool) + sizeof(MENU_SCREEN) * MAX_MENUS; p_menu_set->p_menu_screen_buf_free = p_menu_set->p_menu_screen_buf; p_menu_set->menu_count = 0; @@ -988,7 +999,7 @@ int display_menu(MENU_SET *p_menu_set) return EXITMENU; } - if(p_menu->item_count <= 0) // empty menu + if (p_menu->item_count <= 0) // empty menu { moveto(p_menu->screen_row, p_menu->screen_col); clrtoeol(); @@ -1140,7 +1151,6 @@ int menu_control(MENU_SET *p_menu_set, i switch (key) { case CR: - igetch_reset(); case KEY_RIGHT: if (p_menu_item->submenu) { @@ -1374,7 +1384,7 @@ int unload_menu(MENU_SET *p_menu_set) detach_menu_shm(p_menu_set); - if (shmctl(shmid, IPC_RMID, NULL) == -1) + if (shmid != 0 && shmctl(shmid, IPC_RMID, NULL) == -1) { log_error("shmctl(shmid=%d, IPC_RMID) error (%d)\n", shmid, errno); return -1; @@ -1395,10 +1405,10 @@ int get_menu_shm_readonly(MENU_SET *p_me } p_menu_set->p_reserved = p_shm; - p_menu_set->p_menu_pool = p_menu_set->p_reserved + MENU_SET_RESERVED_LENGTH; - p_menu_set->p_menu_item_pool = p_menu_set->p_menu_pool + sizeof(MENU) * MAX_MENUS; - p_menu_set->p_menu_screen_pool = p_menu_set->p_menu_item_pool + sizeof(MENU_ITEM) * MAX_MENUITEMS; - p_menu_set->p_menu_screen_buf = p_menu_set->p_menu_screen_pool + sizeof(MENU_SCREEN) * MAX_MENUS; + p_menu_set->p_menu_pool = (char *)(p_menu_set->p_reserved) + MENU_SET_RESERVED_LENGTH; + p_menu_set->p_menu_item_pool = (char *)(p_menu_set->p_menu_pool) + sizeof(MENU) * MAX_MENUS; + 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_buf = (char *)(p_menu_set->p_menu_screen_pool) + sizeof(MENU_SCREEN) * MAX_MENUS; p_menu_set->p_menu_screen_buf_free = p_menu_set->p_menu_screen_buf; p_menu_set->choose_step = 0;