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

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

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