Annotation of freem/src/shmmgr.c, revision 1.17

1.1       snw         1: /*
1.17    ! snw         2:  *   $Id: shmmgr.c,v 1.16 2025/05/12 14:26:15 snw Exp $
1.1       snw         3:  *    shared memory manager
                      4:  *
                      5:  *  
1.3       snw         6:  *   Author: Serena Willis <snw@coherent-logic.com>
1.1       snw         7:  *    Copyright (C) 1998 MUG Deutschland
1.4       snw         8:  *    Copyright (C) 2020, 2025 Coherent Logic Development LLC
1.1       snw         9:  *
                     10:  *
                     11:  *   This file is part of FreeM.
                     12:  *
                     13:  *   FreeM is free software: you can redistribute it and/or modify
                     14:  *   it under the terms of the GNU Affero Public License as published by
                     15:  *   the Free Software Foundation, either version 3 of the License, or
                     16:  *   (at your option) any later version.
                     17:  *
                     18:  *   FreeM is distributed in the hope that it will be useful,
                     19:  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
                     20:  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     21:  *   GNU Affero Public License for more details.
                     22:  *
                     23:  *   You should have received a copy of the GNU Affero Public License
                     24:  *   along with FreeM.  If not, see <https://www.gnu.org/licenses/>.
                     25:  *
1.5       snw        26:  *   $Log: shmmgr.c,v $
1.17    ! snw        27:  *   Revision 1.16  2025/05/12 14:26:15  snw
        !            28:  *   Revert bad change in shared memory manager
        !            29:  *
1.16      snw        30:  *   Revision 1.15  2025/05/09 19:44:50  snw
                     31:  *   Begin shm rework
                     32:  *
1.15      snw        33:  *   Revision 1.14  2025/05/08 14:47:26  snw
                     34:  *   Break everything to begin shared memory rewrite
                     35:  *
1.14      snw        36:  *   Revision 1.13  2025/04/16 17:36:12  snw
                     37:  *   Add FreeBSD shm cleanup script
                     38:  *
1.13      snw        39:  *   Revision 1.12  2025/04/15 21:57:10  snw
                     40:  *   Fix SysV IPC bugs on FreeBSD
                     41:  *
1.12      snw        42:  *   Revision 1.11  2025/04/15 21:08:51  snw
                     43:  *   Add some useful debug output
                     44:  *
1.11      snw        45:  *   Revision 1.10  2025/04/15 19:26:13  snw
                     46:  *   Remove extra whitespace
                     47:  *
1.10      snw        48:  *   Revision 1.9  2025/04/15 16:49:36  snw
                     49:  *   Make use of logprintf throughout codebase
                     50:  *
1.9       snw        51:  *   Revision 1.8  2025/04/14 19:46:18  snw
                     52:  *   Add SHM_REMAP flag to shmat on FreeBSD
                     53:  *
1.8       snw        54:  *   Revision 1.7  2025/04/09 19:52:02  snw
                     55:  *   Eliminate as many warnings as possible while building with -Wall
                     56:  *
1.7       snw        57:  *   Revision 1.6  2025/04/04 19:43:18  snw
                     58:  *   Switch to using environment catalog to determine user and group for environment, and remove -u and -g flags from freem
                     59:  *
1.6       snw        60:  *   Revision 1.5  2025/03/24 02:56:50  snw
                     61:  *   Shared memory compatibility fixes for OS/2
                     62:  *
1.5       snw        63:  *   Revision 1.4  2025/03/09 19:50:47  snw
                     64:  *   Second phase of REUSE compliance and header reformat
                     65:  *
1.4       snw        66:  *
                     67:  * SPDX-FileCopyrightText:  (C) 2025 Coherent Logic Development LLC
                     68:  * SPDX-License-Identifier: AGPL-3.0-or-later
1.1       snw        69:  **/
                     70: 
                     71: #include <stdlib.h>
                     72: #include <stdio.h>
                     73: #include <assert.h>
                     74: #include <string.h>
                     75: #include <unistd.h>
                     76: #include <errno.h>
                     77: #include <signal.h>
                     78: 
                     79: #include "shmmgr.h"
                     80: #include "mpsdef.h"
                     81: #include "locktab.h"
1.9       snw        82: #include "log.h"
1.1       snw        83: 
                     84: #include <sys/types.h>
                     85: #include <sys/ipc.h>
                     86: #include <sys/sem.h>
                     87: 
1.5       snw        88: #if !defined(__OpenBSD__) && !defined(__APPLE__) && !defined(__OS2__)
1.1       snw        89: union semun {
                     90:     int              val;    /* Value for SETVAL */
                     91:     struct semid_ds *buf;    /* Buffer for IPC_STAT, IPC_SET */
                     92:     unsigned short  *array;  /* Array for GETALL, SETALL */
                     93:     struct seminfo  *__buf;  /* Buffer for IPC_INFO
                     94:                                 (Linux-specific) */
                     95: };
                     96: #endif
                     97: 
                     98: int semid_shm;
                     99: extern int semid_locktab;
                    100: extern int semid_jobtab;
                    101: extern int semid_tp;
                    102: extern int semid_symtab;
                    103: 
                    104: void shm_daemon_init(void);
                    105: 
                    106: shm_config_t *shm_config = (shm_config_t *) NULL;
                    107: 
1.12      snw       108: #if defined(__FreeBSD__)
1.13      snw       109: # define FM_SHM_PERMS 0777
1.12      snw       110: #else
                    111: # define FM_SHM_PERMS 0770
                    112: #endif
                    113: 
1.1       snw       114: short shm_init(const size_t seg_size)
                    115: {
                    116:     size_t alloc_map_size;
                    117:     long pg_size;
                    118:     key_t shm_sk;
1.13      snw       119:     
                    120: #if defined(__FreeBSD__)
                    121:     struct shmid_ds ctl;
                    122: #endif
                    123:     
1.1       snw       124:     shm_sk = ftok (config_file, 5);    
                    125:     pg_size = sysconf (_SC_PAGESIZE);
                    126:     
                    127:     shm_config = (shm_config_t *) malloc (sizeof (shm_config_t));
                    128:     NULLPTRCHK(shm_config,"shm_init");    
                    129:     
                    130:     /* figure out how many pages we can fit in the segment, accounting for header size */
1.17    ! snw       131:     shm_config->pgct = (seg_size - sizeof (shm_hdr_t)) / pg_size;
1.1       snw       132:     
                    133:     /* how big will the alloc map be? */
                    134:     alloc_map_size = shm_config->pgct * sizeof (shm_page_t);
                    135:     
                    136:     shm_config->segsiz = seg_size + alloc_map_size + pg_size;
                    137:     shm_config->key = ftok (config_file, 1);
                    138:     shm_config->pgsiz = pg_size;
1.12      snw       139:        
                    140:     shm_config->seg_id = shmget (shm_config->key, shm_config->segsiz, FM_SHM_PERMS | IPC_CREAT);
1.1       snw       141:     if (shm_config->seg_id == -1) {
                    142:         if (errno == 22) {
1.9       snw       143:             logprintf (FM_LOG_ERROR, "shm_init:  cannot get shared memory segment of %ld bytes", (unsigned long) shm_config->segsiz);
                    144:             fprintf (stderr, "\r\nYou may need to tune your kernel parameters, or manually set a smaller shared memory segment size in both the FreeM daemon and each interpreter process by using the `-S` command-line flag.\r\n\r\nPlease refer to the FreeM Platform Notes for your operating system for details.\r\n"); 
1.1       snw       145:         }
                    146:         return SHMS_GET_ERR;
                    147:     }
                    148: 
                    149: #if !defined(__arm__)    
                    150:     shm_config->dta = shmat (shm_config->seg_id, NULL, 0);
                    151: #else
                    152:     shm_config->dta = shmat (shm_config->seg_id, (void *) 0x1000000, 0);
                    153: #endif
                    154:     
                    155:     if (shm_config->dta == (void *) -1) {     
1.13      snw       156:         logprintf (FM_LOG_FATAL, "shm_init:  shmat() failed (error code %d [%s])", errno, strerror (errno));
1.1       snw       157:     }
                    158:     /* view the first sizeof (shm_hdr_t) bytes of the data area as an shm_hdr_t */
                    159:     shm_config->hdr = (shm_hdr_t *) shm_config->dta;
                    160:     
                    161:     if (shm_config->hdr->magic != shm_config->key) {
                    162: 
                    163:         /* the shm segment is brand new */
                    164:         first_process = TRUE;
                    165: 
                    166:         shm_daemon_init ();
                    167:         
                    168:     }
                    169:     else {
                    170: 
                    171:         /* this shared mem segment was initialized before */
                    172:         int daemon_chk;
                    173: 
                    174:         /* check if the daemon recorded in the header is actually running */
                    175:         daemon_chk = kill (shm_config->hdr->first_process, 0);
                    176: 
                    177:         if (daemon_chk == -1 && errno == ESRCH) {
1.9       snw       178:             logprintf (FM_LOG_WARNING, "shm_init:  recovering from crashed daemon pid %ld", shm_config->hdr->first_process);
1.1       snw       179:             first_process = TRUE;
                    180:             shm_daemon_init ();
                    181:         }
                    182:         else {
                    183:             first_process = FALSE;
                    184: 
                    185:             semid_shm = semget (shm_sk, 1, 0);
                    186:             if (semid_shm == -1) {
1.9       snw       187:                 logprintf (FM_LOG_FATAL, "shm_init:  could not attach to shared memory semaphore [%s]", strerror (errno));         
1.1       snw       188:             }
                    189:             
1.16      snw       190:             shm_config->buf = SHMALIGN(shm_config->dta + (sizeof (shm_hdr_t) * shm_config->pgct));
1.1       snw       191:         }
                    192: 
                    193:     }
                    194:     
                    195:     locktab_init ();
                    196:     
                    197:     assert(shm_address_to_page_num(shm_page_num_to_address(20)) == 20);
                    198:     
                    199: 
                    200:     return TRUE;
                    201: }
                    202: 
                    203: void shm_daemon_init(void)
                    204: {
                    205:     union semun arg;
                    206:     key_t shm_sk;
                    207:     register int i;
                    208: 
                    209:     shm_sk = ftok (config_file, 5);
                    210:     
                    211:     semid_shm = semget (shm_sk, 1, 0660 | IPC_CREAT);
                    212:     if (semid_shm == -1) {
1.9       snw       213:         logprintf (FM_LOG_FATAL, "shm_init:  failed to create shared memory semaphore [%s]", strerror (errno));
1.1       snw       214:     }
                    215:     
                    216:     arg.val = 1;
                    217:     if (semctl (semid_shm, 0, SETVAL, arg) == -1) {
1.9       snw       218:         logprintf (FM_LOG_FATAL, "shm_init:  failed to initialize shared memory semaphore [%s]", strerror (errno));  
1.1       snw       219:     }
                    220:     
                    221:     /* zero out the segment */
                    222:     memset (shm_config->dta, 0, shm_config->segsiz);
                    223:     
                    224:     /* we are the process that created the segment: initialize everything */
                    225:     shm_config->hdr->magic = shm_config->key;
                    226:     shm_config->hdr->first_process = pid;
                    227:     
                    228:     /* store the address we got into the shm_hdr (borrowed from RSM) */
                    229:     shm_config->hdr->shmad = shm_config->dta;
                    230:     shm_config->hdr->maintenance_mode = 0;
                    231:     
                    232:     /* alloc_map comes after the header */
                    233: /*
                    234:     shm_config->alloc_map = (shm_page_t *) (shm_config->dta + sizeof (shm_hdr_t));
                    235: */    
                    236:     shm_config->buf = SHMALIGN(shm_config->dta + (sizeof (shm_hdr_t) * shm_config->pgct));
1.9       snw       237:     logprintf (FM_LOG_INFO, "shm_daemon_init:  allocator buffer aligned at %p (system page size %ld)", shm_config->buf, sysconf (_SC_PAGESIZE));
1.1       snw       238:     
                    239:     for (i = 0; i < shm_config->pgct; i++) {
                    240:         shm_config->hdr->alloc_map[i].is_first = FALSE;
                    241:         shm_config->hdr->alloc_map[i].is_last = FALSE;
                    242:         shm_config->hdr->alloc_map[i].pg_state = PG_FREE;
                    243:     }
                    244:     
                    245: }
                    246: 
                    247: short shm_exit(void)
                    248: {
                    249:     int res;
                    250:     union semun arg;
                    251: 
                    252:     res = shmdt (shm_config->dta);
                    253: 
                    254:     if (res == -1) {
1.9       snw       255:         logprintf (FM_LOG_ERROR, "shm_exit:  failure in shmdt() [%s]", strerror (errno));
1.1       snw       256:         return FALSE;
                    257:     }
                    258:     
                    259:     if (first_process) {
                    260: 
                    261:         res = shmctl (shm_config->seg_id, IPC_RMID, 0);
                    262: 
                    263:         if (res == -1) {
1.9       snw       264:             logprintf (FM_LOG_ERROR, "shm_exit:  failure in shmctl() [%s]", strerror (errno));
1.1       snw       265:             return FALSE;
                    266:         }
                    267: 
                    268:         semctl (semid_shm, 0, IPC_RMID, arg);
                    269:         semctl (semid_locktab, 0, IPC_RMID, arg);
                    270:         semctl (semid_jobtab, 0, IPC_RMID, arg);
                    271:         semctl (semid_tp, 0, IPC_RMID, arg);
                    272:         semctl (semid_symtab, 0, IPC_RMID, arg);
                    273:         
                    274:     }
                    275: 
                    276:     return TRUE;
                    277:     
                    278: }
                    279: 
                    280: short shm_get_sem(void)
                    281: {
                    282:     
                    283:     int tries;
                    284:     struct sembuf s = {0, -1, 0};
                    285:     
                    286:     for (tries = 0; tries < 3; tries++) {
                    287: 
                    288:         if (semop (semid_shm, &s, 1) != -1) {
                    289:             return TRUE;
                    290:         }
                    291: 
                    292:         sleep (1);
                    293: 
                    294:     }
                    295: 
                    296:     return FALSE;
                    297:     
                    298: }
                    299: 
                    300: short shm_release_sem(void)
                    301: {
                    302:     struct sembuf s = {0, 1, 0};
                    303: 
                    304:     if (semop (semid_shm, &s, 1) != -1) {
                    305:         return TRUE;
                    306:     }
                    307: 
                    308:     return FALSE;
                    309: }
                    310: 
                    311: void shm_set_maintenance_mode (const short maintenance_mode)
                    312: {
                    313:     if (shm_get_sem () == TRUE) {
                    314:         shm_config->hdr->maintenance_mode = maintenance_mode;
                    315: 
                    316:         shm_release_sem ();
                    317:     }
                    318: }
                    319: 
                    320: shm_page_t *shm_get_alloc_map_entry(const int page_number)
                    321: {
                    322:     return &(shm_config->hdr->alloc_map[page_number]);
                    323: }
                    324: 
                    325: void *shm_page_num_to_address(const int page_num)
                    326: {
                    327:     return (void *) shm_config->buf + (shm_config->pgsiz * page_num);
                    328: }
                    329: 
                    330: int shm_address_to_page_num(const void *address)
                    331: {
                    332:     unsigned long val = (unsigned long) address - (unsigned long) shm_config->buf;
                    333:     unsigned long new_val = val / shm_config->pgsiz;
                    334:     
                    335:     return (int) new_val;
                    336: }
                    337: 
                    338: void *shm_alloc_pages(const int page_count)
                    339: {
                    340:     
                    341:     register int i;
                    342:     register int j;
                    343: 
                    344:     int candidate_page = 0;
                    345:     int free_pages_gotten = 0;
                    346:     
                    347:     shm_page_t *pg;
                    348: 
                    349:     if (shm_get_sem () == FALSE) {
1.9       snw       350:         logprintf (FM_LOG_FATAL, "shm_alloc_pages:  could not get exclusive access to shared memory");
1.1       snw       351:     }
                    352: 
                    353:     
                    354:     for (i = 0; i < shm_config->pgct; i++) {
                    355: 
                    356:         pg = shm_get_alloc_map_entry (i);
                    357:         NULLPTRCHK(pg,"shm_alloc_pages");
                    358: 
                    359:         free_pages_gotten = 0;
                    360:         
                    361:         if (pg->pg_state == PG_FREE) {
                    362: 
                    363:             candidate_page = i;
                    364:             
                    365:             for (j = i; ((j < (i + page_count)) && (j < shm_config->pgct)); j++) {
                    366:                 pg = shm_get_alloc_map_entry (j);
                    367: 
                    368:                 if (pg->pg_state == PG_FREE) free_pages_gotten++;
                    369:                 
                    370:             }
                    371: 
                    372:             if (free_pages_gotten == page_count) {
                    373: 
                    374:                 for (j = candidate_page; j < (candidate_page + page_count); j++) {
                    375:                     pg = shm_get_alloc_map_entry (j);
                    376: 
                    377:                     pg->pg_state = PG_ALLOC;
                    378:                     pg->pid = pid;
                    379:                     
                    380:                     if (j == candidate_page) {
                    381:                         pg->is_first = TRUE;
                    382:                     }
                    383: 
                    384:                     if (j == candidate_page + (page_count - 1)) {
                    385:                         pg->is_last = TRUE;
                    386:                     }
                    387:                     
                    388:                 }
                    389: 
                    390:                 shm_release_sem ();
                    391:                 
                    392:                 return (void *) shm_config->buf + (shm_config->pgsiz * candidate_page);
                    393:                 
                    394:             }
                    395:             
                    396:         }
                    397: 
                    398:     }
                    399: 
                    400:     shm_release_sem ();
                    401:     
                    402:     return (void *) NULL;
                    403:     
                    404: }
                    405: 
                    406: 
                    407: void *shm_alloc(const size_t bytes)
                    408: {
                    409:     int pages_needed = bytes / shm_config->pgsiz;
                    410:     float extra = bytes % shm_config->pgsiz;
                    411: 
                    412:     if (extra > 0) {
                    413:         pages_needed++;
                    414:     }
                    415: 
                    416:     return shm_alloc_pages (pages_needed);
                    417: }
                    418: 
                    419: void shm_free_page(const int page_number)
                    420: {
                    421:     register int i;
                    422:     shm_page_t *a = shm_get_alloc_map_entry (page_number);
                    423: 
                    424:     if (a->is_first == FALSE) {
1.9       snw       425:         logprintf (FM_LOG_ERROR, "shm_free_page:  attempt to free page in the middle of allocation chain");
1.1       snw       426:         return;
                    427:     }
                    428: 
                    429:     if (a->pg_state == PG_FREE) {
1.9       snw       430:         logprintf (FM_LOG_FATAL, "shm_free_page:  double free attempted in page %d", page_number);
1.1       snw       431:     }
                    432:     
                    433:     if (shm_get_sem () == FALSE) {
1.9       snw       434:         logprintf (FM_LOG_FATAL, "shm_free_page:  could not get exclusive access to shared memory");
1.1       snw       435:     }
                    436: 
                    437:     
                    438:     for (i = page_number; i < shm_config->pgct; i++) {
                    439: 
                    440:         a = shm_get_alloc_map_entry (i);        
                    441:         
                    442:         if (a->is_last) {
                    443:             a->is_first = FALSE;
                    444:             a->pg_state = PG_FREE;
                    445:             a->pid = 0;
                    446:             a->is_last = FALSE;
                    447: 
                    448:             shm_release_sem ();
                    449:             
                    450:             return;
                    451:         }
                    452:         else {
                    453:             a->is_first = FALSE;
                    454:             a->pg_state = PG_FREE;
                    455:             a->pid = 0;
                    456:             a->is_last = FALSE;
                    457:         }
                    458:         
                    459:     }
                    460: 
                    461:     shm_release_sem ();
                    462:    
                    463: }
                    464: 
                    465: void shm_free(const void *addr)
                    466: {
                    467:     shm_free_page (shm_address_to_page_num (addr));
                    468: }
                    469: 
                    470: void shm_dump(void)
                    471: {
                    472: 
                    473:     printf ("SHARED MEMORY CONFIGURATION\r\n");
1.2       snw       474:     printf ("  pgsiz                   %ld\r\n", (unsigned long) shm_config->pgsiz);
1.1       snw       475:     printf ("  pgct                    %d\r\n", shm_config->pgct);
1.7       snw       476:     printf ("  key                     %d\r\n", shm_config->key);
1.1       snw       477:     printf ("  segid                   %d\r\n", shm_config->seg_id);
1.2       snw       478:     printf ("  sizeof shm_page_t       %ld\r\n", (long) sizeof (shm_page_t));
                    479:     printf ("  segsiz                  %ld\r\n", (long) shm_config->segsiz);
1.1       snw       480:     printf ("  shm address             %p\r\n", shm_config->dta);
1.2       snw       481:     printf ("  alloc_map size          %ld\r\n", (unsigned long) sizeof (shm_page_t) * shm_config->pgct);
1.1       snw       482:     printf ("  buf address             %p\r\n", shm_config->buf);
                    483: }
                    484: 
                    485: void shm_dump_pages(void)
                    486: {
                    487: 
                    488:     register int i;
                    489:     shm_page_t *p;
                    490: 
                    491:     printf ("%-10s%-10s%-10s%-10s%-10s\r\n", "PAGE", "PID", "BMHEAD", "BMTAIL", "STATE");
                    492:     
                    493:     for (i = 0; i < shm_config->pgct; i++) {
                    494: 
                    495:         p = shm_get_alloc_map_entry (i);
                    496: 
                    497:         printf ("%-10d%-10d%-10s%-10s%-10s\r\n",
                    498:                 i,
                    499:                 p->pid,
                    500:                 (p->is_first == TRUE) ? "Y" : "N",
                    501:                 (p->is_last == TRUE) ? "Y" : "N",
                    502:                 (p->pg_state == PG_FREE) ? "PG_FREE" : "PG_ALLOC");
                    503:         
                    504:     }
                    505: 
                    506: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>