Annotation of freem/src/shmmgr.c, revision 1.16
1.1 snw 1: /*
1.16 ! snw 2: * $Id: shmmgr.c,v 1.15 2025/05/09 19:44:50 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.16 ! snw 27: * Revision 1.15 2025/05/09 19:44:50 snw
! 28: * Begin shm rework
! 29: *
1.15 snw 30: * Revision 1.14 2025/05/08 14:47:26 snw
31: * Break everything to begin shared memory rewrite
32: *
1.14 snw 33: * Revision 1.13 2025/04/16 17:36:12 snw
34: * Add FreeBSD shm cleanup script
35: *
1.13 snw 36: * Revision 1.12 2025/04/15 21:57:10 snw
37: * Fix SysV IPC bugs on FreeBSD
38: *
1.12 snw 39: * Revision 1.11 2025/04/15 21:08:51 snw
40: * Add some useful debug output
41: *
1.11 snw 42: * Revision 1.10 2025/04/15 19:26:13 snw
43: * Remove extra whitespace
44: *
1.10 snw 45: * Revision 1.9 2025/04/15 16:49:36 snw
46: * Make use of logprintf throughout codebase
47: *
1.9 snw 48: * Revision 1.8 2025/04/14 19:46:18 snw
49: * Add SHM_REMAP flag to shmat on FreeBSD
50: *
1.8 snw 51: * Revision 1.7 2025/04/09 19:52:02 snw
52: * Eliminate as many warnings as possible while building with -Wall
53: *
1.7 snw 54: * Revision 1.6 2025/04/04 19:43:18 snw
55: * Switch to using environment catalog to determine user and group for environment, and remove -u and -g flags from freem
56: *
1.6 snw 57: * Revision 1.5 2025/03/24 02:56:50 snw
58: * Shared memory compatibility fixes for OS/2
59: *
1.5 snw 60: * Revision 1.4 2025/03/09 19:50:47 snw
61: * Second phase of REUSE compliance and header reformat
62: *
1.4 snw 63: *
64: * SPDX-FileCopyrightText: (C) 2025 Coherent Logic Development LLC
65: * SPDX-License-Identifier: AGPL-3.0-or-later
1.1 snw 66: **/
67:
68: #include <stdlib.h>
69: #include <stdio.h>
70: #include <assert.h>
71: #include <string.h>
72: #include <unistd.h>
73: #include <errno.h>
74: #include <signal.h>
75:
76: #include "shmmgr.h"
77: #include "mpsdef.h"
78: #include "locktab.h"
1.9 snw 79: #include "log.h"
1.1 snw 80:
81: #include <sys/types.h>
82: #include <sys/ipc.h>
83: #include <sys/sem.h>
84:
1.5 snw 85: #if !defined(__OpenBSD__) && !defined(__APPLE__) && !defined(__OS2__)
1.1 snw 86: union semun {
87: int val; /* Value for SETVAL */
88: struct semid_ds *buf; /* Buffer for IPC_STAT, IPC_SET */
89: unsigned short *array; /* Array for GETALL, SETALL */
90: struct seminfo *__buf; /* Buffer for IPC_INFO
91: (Linux-specific) */
92: };
93: #endif
94:
95: int semid_shm;
96: extern int semid_locktab;
97: extern int semid_jobtab;
98: extern int semid_tp;
99: extern int semid_symtab;
100:
101: void shm_daemon_init(void);
102:
103: shm_config_t *shm_config = (shm_config_t *) NULL;
104:
1.12 snw 105: #if defined(__FreeBSD__)
1.13 snw 106: # define FM_SHM_PERMS 0777
1.12 snw 107: #else
108: # define FM_SHM_PERMS 0770
109: #endif
110:
1.1 snw 111: short shm_init(const size_t seg_size)
112: {
113: size_t alloc_map_size;
114: long pg_size;
115: key_t shm_sk;
1.13 snw 116:
117: #if defined(__FreeBSD__)
118: struct shmid_ds ctl;
119: #endif
120:
1.1 snw 121: shm_sk = ftok (config_file, 5);
122: pg_size = sysconf (_SC_PAGESIZE);
123:
124: shm_config = (shm_config_t *) malloc (sizeof (shm_config_t));
125: NULLPTRCHK(shm_config,"shm_init");
126:
127: /* figure out how many pages we can fit in the segment, accounting for header size */
128: shm_config->pgct = (seg_size / pg_size) - sizeof (shm_hdr_t);
129:
130: /* how big will the alloc map be? */
131: alloc_map_size = shm_config->pgct * sizeof (shm_page_t);
132:
133: shm_config->segsiz = seg_size + alloc_map_size + pg_size;
134: shm_config->key = ftok (config_file, 1);
135: shm_config->pgsiz = pg_size;
1.12 snw 136:
137: shm_config->seg_id = shmget (shm_config->key, shm_config->segsiz, FM_SHM_PERMS | IPC_CREAT);
1.1 snw 138: if (shm_config->seg_id == -1) {
139: if (errno == 22) {
1.9 snw 140: logprintf (FM_LOG_ERROR, "shm_init: cannot get shared memory segment of %ld bytes", (unsigned long) shm_config->segsiz);
141: 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 142: }
143: return SHMS_GET_ERR;
144: }
145:
146: #if !defined(__arm__)
147: shm_config->dta = shmat (shm_config->seg_id, NULL, 0);
148: #else
149: shm_config->dta = shmat (shm_config->seg_id, (void *) 0x1000000, 0);
150: #endif
151:
152: if (shm_config->dta == (void *) -1) {
1.13 snw 153: logprintf (FM_LOG_FATAL, "shm_init: shmat() failed (error code %d [%s])", errno, strerror (errno));
1.1 snw 154: }
155: /* view the first sizeof (shm_hdr_t) bytes of the data area as an shm_hdr_t */
156: shm_config->hdr = (shm_hdr_t *) shm_config->dta;
157:
158: if (shm_config->hdr->magic != shm_config->key) {
159:
160: /* the shm segment is brand new */
161: first_process = TRUE;
162:
163: shm_daemon_init ();
164:
165: }
166: else {
167:
168: /* this shared mem segment was initialized before */
169: int daemon_chk;
170:
171: /* check if the daemon recorded in the header is actually running */
172: daemon_chk = kill (shm_config->hdr->first_process, 0);
173:
174: if (daemon_chk == -1 && errno == ESRCH) {
175:
1.9 snw 176: logprintf (FM_LOG_WARNING, "shm_init: recovering from crashed daemon pid %ld", shm_config->hdr->first_process);
1.1 snw 177:
178: first_process = TRUE;
179:
180: shm_daemon_init ();
181:
182: }
183: else {
184:
185:
186: first_process = FALSE;
187:
188: semid_shm = semget (shm_sk, 1, 0);
189: if (semid_shm == -1) {
1.9 snw 190: logprintf (FM_LOG_FATAL, "shm_init: could not attach to shared memory semaphore [%s]", strerror (errno));
1.1 snw 191: }
192:
1.8 snw 193:
1.16 ! snw 194: shm_config->buf = SHMALIGN(shm_config->dta + (sizeof (shm_hdr_t) * shm_config->pgct));
1.1 snw 195:
196:
197: }
198:
199: }
200:
201: locktab_init ();
202:
203: assert(shm_address_to_page_num(shm_page_num_to_address(20)) == 20);
204:
205:
206: return TRUE;
207: }
208:
209: void shm_daemon_init(void)
210: {
211: union semun arg;
212: key_t shm_sk;
213: register int i;
214:
215: shm_sk = ftok (config_file, 5);
216:
217: semid_shm = semget (shm_sk, 1, 0660 | IPC_CREAT);
218: if (semid_shm == -1) {
1.9 snw 219: logprintf (FM_LOG_FATAL, "shm_init: failed to create shared memory semaphore [%s]", strerror (errno));
1.1 snw 220: }
221:
222: arg.val = 1;
223: if (semctl (semid_shm, 0, SETVAL, arg) == -1) {
1.9 snw 224: logprintf (FM_LOG_FATAL, "shm_init: failed to initialize shared memory semaphore [%s]", strerror (errno));
1.1 snw 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));
1.9 snw 243: 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 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) {
1.9 snw 261: logprintf (FM_LOG_ERROR, "shm_exit: failure in shmdt() [%s]", strerror (errno));
1.1 snw 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) {
1.9 snw 270: logprintf (FM_LOG_ERROR, "shm_exit: failure in shmctl() [%s]", strerror (errno));
1.1 snw 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) {
1.9 snw 356: logprintf (FM_LOG_FATAL, "shm_alloc_pages: could not get exclusive access to shared memory");
1.1 snw 357: }
358:
359:
360: for (i = 0; i < shm_config->pgct; i++) {
361:
362: pg = shm_get_alloc_map_entry (i);
363: NULLPTRCHK(pg,"shm_alloc_pages");
364:
365: free_pages_gotten = 0;
366:
367: if (pg->pg_state == PG_FREE) {
368:
369: candidate_page = i;
370:
371: for (j = i; ((j < (i + page_count)) && (j < shm_config->pgct)); j++) {
372: pg = shm_get_alloc_map_entry (j);
373:
374: if (pg->pg_state == PG_FREE) free_pages_gotten++;
375:
376: }
377:
378: if (free_pages_gotten == page_count) {
379:
380: for (j = candidate_page; j < (candidate_page + page_count); j++) {
381: pg = shm_get_alloc_map_entry (j);
382:
383: pg->pg_state = PG_ALLOC;
384: pg->pid = pid;
385:
386: if (j == candidate_page) {
387: pg->is_first = TRUE;
388: }
389:
390: if (j == candidate_page + (page_count - 1)) {
391: pg->is_last = TRUE;
392: }
393:
394: }
395:
396: shm_release_sem ();
397:
398: return (void *) shm_config->buf + (shm_config->pgsiz * candidate_page);
399:
400: }
401:
402: }
403:
404: }
405:
406: shm_release_sem ();
407:
408: return (void *) NULL;
409:
410: }
411:
412:
413: void *shm_alloc(const size_t bytes)
414: {
415: int pages_needed = bytes / shm_config->pgsiz;
416: float extra = bytes % shm_config->pgsiz;
417:
418: if (extra > 0) {
419: pages_needed++;
420: }
421:
422: return shm_alloc_pages (pages_needed);
423: }
424:
425: void shm_free_page(const int page_number)
426: {
427: register int i;
428: shm_page_t *a = shm_get_alloc_map_entry (page_number);
429:
430: if (a->is_first == FALSE) {
1.9 snw 431: logprintf (FM_LOG_ERROR, "shm_free_page: attempt to free page in the middle of allocation chain");
1.1 snw 432: return;
433: }
434:
435: if (a->pg_state == PG_FREE) {
1.9 snw 436: logprintf (FM_LOG_FATAL, "shm_free_page: double free attempted in page %d", page_number);
1.1 snw 437: }
438:
439: if (shm_get_sem () == FALSE) {
1.9 snw 440: logprintf (FM_LOG_FATAL, "shm_free_page: could not get exclusive access to shared memory");
1.1 snw 441: }
442:
443:
444: for (i = page_number; i < shm_config->pgct; i++) {
445:
446: a = shm_get_alloc_map_entry (i);
447:
448: if (a->is_last) {
449: a->is_first = FALSE;
450: a->pg_state = PG_FREE;
451: a->pid = 0;
452: a->is_last = FALSE;
453:
454: shm_release_sem ();
455:
456: return;
457: }
458: else {
459: a->is_first = FALSE;
460: a->pg_state = PG_FREE;
461: a->pid = 0;
462: a->is_last = FALSE;
463: }
464:
465: }
466:
467: shm_release_sem ();
468:
469: }
470:
471: void shm_free(const void *addr)
472: {
473: shm_free_page (shm_address_to_page_num (addr));
474: }
475:
476: void shm_dump(void)
477: {
478:
479: printf ("SHARED MEMORY CONFIGURATION\r\n");
1.2 snw 480: printf (" pgsiz %ld\r\n", (unsigned long) shm_config->pgsiz);
1.1 snw 481: printf (" pgct %d\r\n", shm_config->pgct);
1.7 snw 482: printf (" key %d\r\n", shm_config->key);
1.1 snw 483: printf (" segid %d\r\n", shm_config->seg_id);
1.2 snw 484: printf (" sizeof shm_page_t %ld\r\n", (long) sizeof (shm_page_t));
485: printf (" segsiz %ld\r\n", (long) shm_config->segsiz);
1.1 snw 486: printf (" shm address %p\r\n", shm_config->dta);
1.2 snw 487: printf (" alloc_map size %ld\r\n", (unsigned long) sizeof (shm_page_t) * shm_config->pgct);
1.1 snw 488: printf (" buf address %p\r\n", shm_config->buf);
489: }
490:
491: void shm_dump_pages(void)
492: {
493:
494: register int i;
495: shm_page_t *p;
496:
497: printf ("%-10s%-10s%-10s%-10s%-10s\r\n", "PAGE", "PID", "BMHEAD", "BMTAIL", "STATE");
498:
499: for (i = 0; i < shm_config->pgct; i++) {
500:
501: p = shm_get_alloc_map_entry (i);
502:
503: printf ("%-10d%-10d%-10s%-10s%-10s\r\n",
504: i,
505: p->pid,
506: (p->is_first == TRUE) ? "Y" : "N",
507: (p->is_last == TRUE) ? "Y" : "N",
508: (p->pg_state == PG_FREE) ? "PG_FREE" : "PG_ALLOC");
509:
510: }
511:
512: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>