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