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