Annotation of freem/src/mumps.c, revision 1.25
1.1 snw 1: /*
1.25 ! snw 2: * $Id: mumps.c,v 1.24 2025/04/16 05:37:06 snw Exp $
1.1 snw 3: * main module of freem
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: mumps.c,v $
1.25 ! snw 27: * Revision 1.24 2025/04/16 05:37:06 snw
! 28: * Refine FreeBSD ASLR fix
! 29: *
1.24 snw 30: * Revision 1.23 2025/04/16 05:33:15 snw
31: * Try to use procctl function to fix ASLR problem on FreeBSD
32: *
1.23 snw 33: * Revision 1.22 2025/04/15 16:49:36 snw
34: * Make use of logprintf throughout codebase
35: *
1.22 snw 36: * Revision 1.21 2025/04/15 02:24:43 snw
37: * Improve FreeM logging capabilities
38: *
1.21 snw 39: * Revision 1.20 2025/04/13 04:22:43 snw
40: * Fix snprintf calls
41: *
1.20 snw 42: * Revision 1.19 2025/04/10 01:24:38 snw
43: * Remove C++ style comments
44: *
1.19 snw 45: * Revision 1.18 2025/04/09 19:52:02 snw
46: * Eliminate as many warnings as possible while building with -Wall
47: *
1.18 snw 48: * Revision 1.17 2025/04/04 21:28:16 snw
49: * Remove custom_user and custom_group vars from freem and shed privileges per environment catalog settings
50: *
1.17 snw 51: * Revision 1.16 2025/04/04 19:43:18 snw
52: * Switch to using environment catalog to determine user and group for environment, and remove -u and -g flags from freem
53: *
1.16 snw 54: * Revision 1.15 2025/04/03 20:48:14 snw
55: * Improve daemon error diagnostics and bump to 0.63.0-rc3
56: *
1.15 snw 57: * Revision 1.14 2025/04/02 04:50:49 snw
58: * Allow vendor routines to be upgraded
59: *
1.14 snw 60: * Revision 1.13 2025/04/02 03:26:22 snw
61: * Don't corrupt the terminal if FreeM runs before fmadm configure has been run
62: *
1.13 snw 63: * Revision 1.12 2025/04/02 02:16:27 snw
64: * Add fmadm status environment command and move journals to a better location
65: *
1.12 snw 66: * Revision 1.11 2025/04/01 23:21:45 snw
67: * fmadm commands for stopping, starting, and restarting environments now functional
68: *
1.11 snw 69: * Revision 1.10 2025/04/01 20:11:46 snw
70: * Further work on fmadm
71: *
1.10 snw 72: * Revision 1.9 2025/03/31 20:01:13 snw
73: * Set d_uid in daemon
74: *
1.9 snw 75: * Revision 1.8 2025/03/24 16:10:48 snw
76: * Print error message and exit on OS/2 if daemon is run without --nofork
77: *
1.8 snw 78: * Revision 1.7 2025/03/24 16:07:55 snw
79: * Force daemon into foreground on OS/2
80: *
1.7 snw 81: * Revision 1.6 2025/03/24 16:04:49 snw
82: * Force daemon into foreground on OS/2
83: *
1.6 snw 84: * Revision 1.5 2025/03/22 21:44:32 snw
85: * Make the startup messages fewer and add environment name to direct-mode prompt
86: *
1.5 snw 87: * Revision 1.4 2025/03/09 19:50:47 snw
88: * Second phase of REUSE compliance and header reformat
89: *
1.4 snw 90: *
91: * SPDX-FileCopyrightText: (C) 2025 Coherent Logic Development LLC
92: * SPDX-License-Identifier: AGPL-3.0-or-later
1.1 snw 93: **/
94:
1.16 snw 95: #define _GNU_SOURCE
1.1 snw 96: #include <stdlib.h>
97: #include <stddef.h>
98: #include "mpsdef.h"
99: #include "errmsg.h"
100: #include "iniconf.h"
101: #include "namespace.h"
102: #include "transact.h"
103: #include "init.h"
104: #include "consttbl.h"
105: #include <setjmp.h>
106: #include <stdio.h>
107: #include <signal.h>
108: #include <time.h>
109: #include <unistd.h>
110: #include <sys/types.h>
111: #include <sys/wait.h>
112: #include <sys/stat.h>
113: #include <limits.h>
114: #include <sys/types.h>
115: #include <pwd.h>
116: #include <string.h>
117: #include <fcntl.h>
118: #include "version.h"
119: #include "shmmgr.h"
120: #include "jobtab.h"
121: #include <errno.h>
122: #include <pwd.h>
123: #include <grp.h>
1.21 snw 124: #include "log.h"
1.1 snw 125: #if defined(HAVE_GETOPT_H)
126: # include <getopt.h>
127: #endif
128:
129: #if !defined(PATH_MAX) && defined(_SCO_DS)
130: # define PATH_MAX 1024
131: #endif
132:
133: extern int xecline(int typ);
134: extern char *getenv(const char *name);
135: void freem_usage(void);
136: void freem_print_version(void);
137: void init_ztrap(void);
138:
139: int main (int argc, char **argv, char **envp)
140: {
141: pid_t fork_pid = 0;
142: short dx_mode = 0;
143:
144: int c;
145: int import_env = FALSE;
146: short skip_init = 0;
147:
148: int option_index = 0;
149:
1.13 snw 150: char fm_initialized = FALSE;
151:
1.1 snw 152: char dx_mcode[512];
153: char startup_routine[256];
154: short routine_mode;
155: char m_dialect[50];
156:
157: char nsnbuf[256];
158:
159: char d_username[40];
160: char d_groupname[40];
161: struct group *d_grp;
162: struct passwd *d_user;
163: gid_t d_gid;
164: uid_t d_uid;
165:
166: #if defined(HAVE_GETOPT_LONG)
167: struct option long_options[] = {
168: {"help", no_argument, 0, 'h'},
169: {"filter", no_argument, &frm_filter, TRUE},
170: {"standard", required_argument, 0, 's'},
171: {"import", no_argument, &import_env, TRUE},
172: {"quiet", no_argument, &quiet_mode, TRUE},
173: {"restricted", no_argument, &restricted_mode, TRUE},
174: {"routine", required_argument, 0, 'r'},
175: {"namespace", required_argument, 0, 'n'},
176: {"environment", required_argument, 0, 'e'},
177: {"version", no_argument, 0, 'v'},
178: {"execute", required_argument, 0, 'x'},
179: {"daemon", no_argument, 0, 'd'},
180: {"nofork", no_argument, 0, 'k'},
181: {"shmsize", required_argument, 0, 'S'},
182: {"user", required_argument, 0, 'u'},
183: {"group", required_argument, 0, 'g'},
184: {0, 0, 0, 0}
185: };
186: #endif
187:
188: char **env;
189: char *varname = (char *) calloc(STRLEN, sizeof(char));
190: char *varval = (char *) calloc(STRLEN, sizeof(char));
191:
192: char *symname = (char *) calloc(STRLEN, sizeof(char));
193: char *symval = (char *) calloc(STRLEN, sizeof(char));
1.18 snw 194:
1.1 snw 195: char cli_rtn_path[PATH_MAX] = {0};
196: char *cli_rtn_file;
197: char cli_rtn_name[256];
198:
1.16 snw 199: char env_ena[25];
200:
1.1 snw 201: routine_mode = FALSE;
202: strcpy (m_dialect, "FREEM");
203:
204: m_argc = argc; /* save arguments count */
205: m_argv = argv; /* save arguments string */
206: m_envp = envp; /* save environment pointer */
207:
208: strncpy (shm_env, "DEFAULT", 8); /* establish default environment name */
209:
210: strncpy (d_username, "freem", 40);
211: strncpy (d_groupname, "freem", 40);
212:
213: strcpy (zb, argv[0]); /* name with which mumps has been called */
214: stcnv_c2m (zb);
215: strcpy (stack0, argv[0]);
216: stcnv_c2m (stack0);
217:
218: /* strncpy (config_file, SYSCONFDIR"/freem.conf", 100); */
219:
220:
221:
222: #if defined(HAVE_GETOPT_LONG)
223:
224: extern char *optarg;
225: extern int optind, optopt;
226:
227: while (1) {
228:
1.16 snw 229: c = getopt_long (argc, argv, "hsfiqRr:n:e:vx:dkpS", long_options, &option_index);
1.1 snw 230:
231: if (c == -1) break;
232: if (c == '?') freem_usage ();
233:
234: switch (c) {
235: case 'h':
236: freem_usage ();
237: break;
238:
239: case 'f':
240: frm_filter = TRUE;
241: break;
242:
243: case 'i':
244: import_env = TRUE;
245: break;
246:
247: case 'q':
248: quiet_mode = TRUE;
249: break;
250:
251: case 'e': /* specify FreeM environment */
1.19 snw 252: strncpy (shm_env, optarg, sizeof (shm_env) - 1);
1.1 snw 253: break;
254:
255: case 'R':
256: restricted_mode = TRUE;
257: break;
258:
259: case 'r': /* startup routine */
260: direct_mode = FALSE;
261: usermode = 0;
262:
263: strcpy (startup_routine, optarg);
264: startup_routine[strlen (startup_routine)] = '\201';
265:
266: routine_mode = TRUE;
267: break;
268:
269: case 'n': /* namespace */
270: {
271: if (validate_namespace (optarg) == TRUE) {
272: strcpy (nsname, optarg);
273: }
274: else {
275: fprintf (stderr, "freem: invalid namespace '%s'\n", optarg);
276: exit (1);
277: }
278:
279: break;
280: }
281: case 'v': /* version */
282: freem_print_version ();
283: break;
284:
285: case 'x': /* execute */
286: direct_mode = FALSE;
287: usermode = 0;
288: dx_mode = 1;
289:
290: strncpy (dx_mcode, optarg, 512 - 1);
291:
292: stcnv_c2m (dx_mcode);
293:
294: break;
295:
296: case 's': /* --standard */
297:
298: if (strcmp (optarg, "M77") == 0) {
299: standard = D_M77;
300: strcpy (m_dialect, "M 1977");
301: }
302: else if (strcmp (optarg, "M84") == 0) {
303: standard = D_M84;
304: strcpy (m_dialect, "M 1984");
305: }
306: else if (strcmp (optarg, "M90") == 0) {
307: standard = D_M90;
308: strcpy (m_dialect, "M 1990");
309: }
310: else if (strcmp (optarg, "M95") == 0) {
311: standard = D_M95;
312: strcpy (m_dialect, "M 1995");
313: }
314: else if (strcmp (optarg, "MDS") == 0) {
315: standard = D_MDS;
316: strcpy (m_dialect, "Millennium Draft Standard");
317: }
318: else if (strcmp (optarg, "M5") == 0) {
319: standard = D_M5;
320: strcpy (m_dialect, "M5");
321: }
322: else if (strcmp (optarg, "FREEM") == 0) {
323: standard = D_FREEM;
324: strcpy (m_dialect, "FREEM");
325: }
326: else {
327: freem_usage ();
328: }
329:
330: break;
331:
332: case 'd': /* --daemon */
333: run_daemon = TRUE;
334: break;
335:
336: case 'k': /* --nofork */
337: nofork = TRUE;
338: break;
339:
340: case 'S': /* --shmsize */
341: shm_init_size = atol (optarg);
1.16 snw 342: break;
1.1 snw 343:
344: }
345:
346: }
347:
348: #else
349:
350: {
351: extern char *optarg;
352: extern int optind, optopt;
353:
1.16 snw 354: while ((c = getopt (argc, argv, "hsfiqRr:n:e:vx:dkS:")) != -1) {
1.1 snw 355:
356: if (c == '?') freem_usage ();
357:
358: switch (c) {
359:
360: case 'h':
361: freem_usage ();
362: break;
363:
364: case 'f':
365: frm_filter = TRUE;
366: break;
367:
368: case 'i':
369: import_env = TRUE;
370: break;
371:
372: case 'q':
373: quiet_mode = TRUE;
374: break;
375:
376: case 'e': /* specify FreeM environment */
1.19 snw 377: strncpy (shm_env, optarg, sizeof (shm_env) - 1);
1.1 snw 378: break;
379:
380: case 'R':
381: restricted_mode = TRUE;
382: break;
383:
384: case 'r': /* startup routine */
385: direct_mode = FALSE;
386: usermode = 0;
387:
388: strcpy (startup_routine, optarg);
389: startup_routine[strlen (startup_routine)] = '\201';
390:
391: routine_mode = TRUE;
392: break;
393:
394: case 'n': /* namespace */
395: strcpy (nsname, optarg);
396: break;
397:
398: case 'v':
399: freem_print_version ();
400: break;
401:
402: case 'x': /* execute */
403: direct_mode = FALSE;
404: usermode = 0;
405: dx_mode = 1;
406:
407: strncpy (dx_mcode, optarg, 512 - 1);
408:
409: stcnv_c2m (dx_mcode);
410:
411: break;
412:
413: case 'd': /* --daemon */
414: run_daemon = TRUE;
415: break;
416:
417: case 'k': /* --nofork */
418: nofork = TRUE;
419: break;
420:
421: case 's': /* --standard */
422:
423: if (strcmp (optarg, "M77") == 0) {
424: standard = D_M77;
425: strcpy (m_dialect, "M 1977");
426: }
427: else if (strcmp (optarg, "M84") == 0) {
428: standard = D_M84;
429: strcpy (m_dialect, "M 1984");
430: }
431: else if (strcmp (optarg, "M90") == 0) {
432: standard = D_M90;
433: strcpy (m_dialect, "M 1990");
434: }
435: else if (strcmp (optarg, "M95") == 0) {
436: standard = D_M95;
437: strcpy (m_dialect, "M 1995");
438: }
439: else if (strcmp (optarg, "MDS") == 0) {
440: standard = D_MDS;
441: strcpy (m_dialect, "Millennium Draft Standard");
442: }
443: else if (strcmp (optarg, "M5") == 0) {
444: standard = D_M5;
445: strcpy (m_dialect, "M5");
446: }
447: else if (strcmp (optarg, "FREEM") == 0) {
448: standard = D_FREEM;
449: strcpy (m_dialect, "FREEM");
450: }
451: else {
452: freem_usage ();
453: }
454:
455: break;
456:
457:
458: case 'S': /* --shmsize */
459: shm_init_size = atol (optarg);
460: break;
461:
462: }
463: }
464: }
465: #endif
1.6 snw 466:
467: #if defined(__OS2__)
468: if (run_daemon == TRUE && nofork == FALSE) {
1.8 snw 469: printf ("freem: running on OS/2; daemon must be run with --nofork or -k\r\n");
470: exit (1);
1.6 snw 471: }
472: #endif
473:
1.20 snw 474: snprintf (config_file, sizeof (config_file) - 1, "%s/freem/%s/freem.conf", SYSCONFDIR, shm_env);
475: snprintf (env_config_file, sizeof (config_file) - 1, "%s/freem/env.conf", SYSCONFDIR);
1.1 snw 476:
1.16 snw 477: if (!file_exists (env_config_file)) {
1.22 snw 478: logprintf (FM_LOG_FATAL, "freem: environment catalog does not exist; may need to run fmadm configure");
1.16 snw 479: }
480:
481: if (!file_exists (config_file)) {
1.22 snw 482: logprintf (FM_LOG_FATAL, "freem: configuration file for %s does not exist; may need to run fmadm configure", shm_env);
1.16 snw 483: }
484:
485: if (read_profile_string (env_config_file, shm_env, "user", d_username) == FALSE) {
1.22 snw 486: logprintf (FM_LOG_FATAL, "freem: could not determine owning user for environment %s", shm_env);
1.16 snw 487: }
488:
489: if (read_profile_string (env_config_file, shm_env, "group", d_groupname) == FALSE) {
1.22 snw 490: logprintf (FM_LOG_FATAL, "freem: could not determine owning group for environment %s", shm_env);
1.16 snw 491: }
1.1 snw 492:
1.16 snw 493: if (read_profile_string (env_config_file, shm_env, "enabled", env_ena) == FALSE) {
1.22 snw 494: logprintf (FM_LOG_FATAL, "freem: could not discover enabled state for environment %s", shm_env);
1.16 snw 495: }
1.1 snw 496:
1.16 snw 497: if (strcmp (env_ena, "true") != 0) {
1.22 snw 498: logprintf (FM_LOG_FATAL, "freem: environment %s is administratively disabled", shm_env);
1.16 snw 499: }
1.1 snw 500:
1.16 snw 501: d_grp = getgrnam (d_groupname);
502: if (d_grp == NULL) {
1.22 snw 503: logprintf (FM_LOG_FATAL, "freem: invalid group '%s'", d_groupname);
1.16 snw 504: }
505: d_gid = d_grp->gr_gid;
506:
507: d_user = getpwnam (d_username);
508: if (d_user == NULL) {
1.22 snw 509: logprintf (FM_LOG_FATAL, "freem: invalid user '%s'", d_username);
1.16 snw 510: }
511: d_uid = d_user->pw_uid;
1.1 snw 512:
1.16 snw 513:
514: #if defined(__linux__)
515: if (run_daemon == FALSE && group_member (d_gid) == 0) {
1.22 snw 516: logprintf (FM_LOG_FATAL, "freem: you must be a member of the %s group to use environment %s", d_groupname, shm_env);
1.17 snw 517: }
1.16 snw 518: #endif
519:
520: if (run_daemon == TRUE) {
521: if (geteuid () != 0 && nofork == FALSE) {
1.22 snw 522: logprintf (FM_LOG_FATAL, "freem: forking daemon must be run as root");
1.16 snw 523: }
1.1 snw 524: }
525:
526: if ((nofork == TRUE) && (run_daemon == FALSE)) {
527: freem_usage ();
528: exit (1);
529: }
530:
531: if ((run_daemon == TRUE) && (nofork == FALSE)) {
532:
533: int fork_fd;
1.22 snw 534:
535: init_log ();
1.1 snw 536:
537: /* daemonize */
538:
539: fork_pid = fork ();
540:
541: if (fork_pid < 0) {
1.22 snw 542: logprintf (FM_LOG_FATAL, "environment error: failure in initial fork()");
1.1 snw 543: }
544:
545: if (fork_pid > 0) {
546: exit (0);
547: }
548:
549: if (setsid () < 0) {
1.22 snw 550: logprintf (FM_LOG_FATAL, "environment error: failure in setsid()");
1.1 snw 551: }
552:
553: signal (SIGCHLD, SIG_IGN);
554:
555: fork_pid = fork ();
556:
557: if (fork_pid < 0) {
1.22 snw 558: logprintf (FM_LOG_FATAL, "environment error: failure in second fork()");
1.1 snw 559: }
560:
561: if (fork_pid > 0) {
1.22 snw 562: logprintf (FM_LOG_DEBUG, "environment: exiting from second fork()");
1.1 snw 563: exit (0);
564: }
565:
566: umask (0);
567:
568: chdir ("/");
569:
570: for (fork_fd = sysconf (_SC_OPEN_MAX); fork_fd > 0; fork_fd--) {
571: close (fork_fd);
572: }
573:
574: if (geteuid () == 0) {
1.22 snw 575:
1.1 snw 576: /* shed privileges */
1.22 snw 577: logprintf (FM_LOG_INFO, "environment: switching to group %s", d_groupname);
1.17 snw 578:
579: if (setgid (d_gid) == -1) {
1.22 snw 580: logprintf (FM_LOG_FATAL, "environment error: failure switching GID");
1.1 snw 581: }
1.17 snw 582:
1.1 snw 583:
584:
1.17 snw 585: if (d_uid != geteuid ()) {
1.22 snw 586:
587: logprintf (FM_LOG_INFO, "environment: switching to username %s", d_username);
1.1 snw 588:
589: if (setuid (d_uid) == -1) {
1.22 snw 590: logprintf (FM_LOG_FATAL, "environment error: failure switching UID");
1.1 snw 591: }
592: }
593:
594: }
595: else {
1.22 snw 596: logprintf (FM_LOG_INFO, "environment: not running as superuser; not shedding privileges");
1.1 snw 597: }
598:
599: freopen ("/dev/null", "r", stdin);
600: freopen ("/dev/null", "w+", stdout);
601: freopen ("/dev/null", "w+", stderr);
602:
603: run_daemon = TRUE;
604: nofork = FALSE;
605:
606: {
607: char pidfile_buf[256];
608: int errsav;
609:
1.11 snw 610: pid_file_path = (char *) malloc (PATH_MAX * sizeof (char));
611: NULLPTRCHK(pid_file_path,"main");
612:
1.10 snw 613: snprintf (pid_file_path, PATH_MAX - 1, "%s/freem/run/%s.pid", LOCALSTATEDIR, shm_env);
614:
1.1 snw 615: pid_fd = open (pid_file_path, O_RDWR | O_CREAT, 0640);
616: errsav = errno;
617:
618: if (pid_fd < 0) {
1.22 snw 619: logprintf (FM_LOG_FATAL, "environment error: could not open PID file %s [%s]", pid_file_path, strerror (errsav));
1.1 snw 620: }
621:
622: if (lockf (pid_fd, F_TLOCK, 0) < 0) {
623: errsav = errno;
1.22 snw 624: logprintf (FM_LOG_FATAL, "environment error: could not lock PID file [%s]", strerror (errsav));
1.1 snw 625: }
626:
1.10 snw 627: sprintf (pidfile_buf, "%ld\n", (long) getpid ());
1.1 snw 628: write (pid_fd, pidfile_buf, strlen (pidfile_buf));
1.10 snw 629:
1.1 snw 630: }
631:
632:
633: } /* END of daemonization */
634:
635: /* handle passing of an arbitrary .m file on the command line */
636: /* this is most often used for shebang-line scripts. */
637: if (optind < argc) {
638:
639: /* not valid for daemon mode */
640: if (run_daemon == TRUE) {
641: fprintf (stderr, "freem: cannot pass --daemon flag in shebang line\r\n");
642: exit (1);
643: }
644:
645: /* bail if file does not exist */
646: if (access (argv[optind], F_OK) == -1) {
647:
648: set_io (UNIX);
649:
650: fprintf (stderr, "Routine %s does not exist.\n", argv[optind]);
651:
652: exit (1);
653:
654: }
655:
656: skip_init = 1;
657:
658: /* initialize FreeM environment */
1.19 snw 659: strncpy (nsnbuf, nsname, sizeof (nsnbuf));
1.1 snw 660: if (init (nsnbuf) == FALSE) {
661:
1.14 snw 662: if (fm_initialized) set_io (UNIX);
1.1 snw 663: fprintf (stderr, "\nError initializing FreeM.\n");
664:
665: exit (1);
666:
1.13 snw 667: }
668: else {
669: fm_initialized = TRUE;
670: }
1.1 snw 671:
672:
673: direct_mode = FALSE;
674: usermode = 0;
675:
676: /* was a path specified at all? */
677: if (strchr (argv[optind], '/') == NULL) {
678:
679: /* the entirety of argv[optind] is the filename */
680: cli_rtn_file = argv[optind];
681:
682: /* use the current directory */
683: sprintf (cli_rtn_path, ".");
684:
685: }
686: else {
687:
688: /* isolate the filename from the path */
689: cli_rtn_file = strrchr (argv[optind], '/') + 1;
690:
691: /* isolate the routine name from the filename */
692: strncpy (cli_rtn_name, cli_rtn_file, strchr (cli_rtn_file, '.') - cli_rtn_file);
693:
694: /* isolate the path from the routine file */
695: strncpy (cli_rtn_path, argv[optind], strrchr (argv[optind], '/') - argv[optind]);
696:
697: }
698:
699: /* do we have a file extension? */
700: if (strchr (cli_rtn_file, '.') != NULL) {
701:
702: /* if so, just remove it */
703: strncpy (cli_rtn_name, cli_rtn_file, strchr (cli_rtn_file, '.') - cli_rtn_file);
704:
705: }
706: else {
707:
708: /* otherwise, just take a direct copy */
709: strcpy (cli_rtn_name, cli_rtn_file);
710:
711: }
712:
713: /* make this the startup routine */
1.20 snw 714: snprintf (startuprou, sizeof (startuprou) - 1, "^%s\201", cli_rtn_name);
1.1 snw 715:
716: /* re-work the namespace config to search for the
717: routine in the discovered path */
718: if (cli_rtn_name[0] == '%') {
719:
1.20 snw 720: snprintf (rou0plib, sizeof (rou0plib) - 1, "%s\201", cli_rtn_path);
721: snprintf (rou1plib, sizeof (rou1plib) - 1, "%s\201", cli_rtn_path);
1.1 snw 722:
723: }
724: else {
725:
1.20 snw 726: snprintf (rou0path, sizeof (rou0path) - 1, "%s\201", cli_rtn_path);
727: snprintf (rou1path, sizeof (rou1path) - 1, "%s\201", cli_rtn_path);
1.1 snw 728:
729: }
730:
731: }
732:
1.13 snw 733:
1.1 snw 734: if (!file_exists (config_file)) {
735:
1.13 snw 736: if (fm_initialized == TRUE) set_io (UNIX);
737:
1.1 snw 738: fprintf (stderr, "\nFreeM has not been configured. Please run 'fmadm configure'.\n\n\n\n");
739:
740: exit (2);
741:
742: }
743:
744: if (!skip_init) {
745: /* initialize FreeM environment */
1.19 snw 746: strncpy (nsnbuf, nsname, sizeof (nsnbuf));
1.13 snw 747: if (init (nsnbuf) == FALSE) {
1.1 snw 748: set_io (UNIX);
749: fprintf (stderr, "\nError initializing FreeM.\n");
750:
751: exit (1);
752: }
1.13 snw 753: else {
754: fm_initialized = TRUE;
755: }
1.24 snw 756: }
757:
1.1 snw 758: if (first_process == TRUE) {
759:
760: char verstr[500];
761: pid_t stop_requester;
762:
763: if (run_daemon == FALSE) {
1.22 snw 764: logprintf (FM_LOG_FATAL, "freem: re-run with --daemon or -d command-line flags");
1.1 snw 765: }
766:
767: stcpy (verstr, FREEM_VERSION_STR);
768: stcnv_m2c (verstr);
769:
770: fprintf (stderr, "Coherent Logic Development FreeM version %s\r\n", verstr);
1.22 snw 771: logprintf (FM_LOG_INFO, "freem: shared memory for environment %s initialized (%ld bytes of shared memory @ '%p')", shm_env, (long) shm_init_size, shm_config->dta);
1.1 snw 772:
773: for (;;) {
774:
775: job_set_status (pid, JSTAT_HOUSEKEEPING);
776:
777:
778: if (shm_config->hdr->maintenance_mode == 1) {
779:
780: job_slot_t *slot;
781:
1.22 snw 782: logprintf (FM_LOG_WARNING, "environment: entering maintenance mode");
1.1 snw 783:
784: for (slot = shm_config->hdr->jobtab_head; slot != NULL; slot = slot->next) {
785:
786: if ((slot->pid != pid) && ((slot->flags & JFLG_FMADM) != JFLG_FMADM)) {
787: kill (slot->pid, SIGINT);
788: }
789:
790: }
791:
792: }
793:
794: if ((stop_requester = job_stop_requested (pid)) != 0) {
795: int connected_jobs;
796:
797: job_set_status (pid, JSTAT_SHUTDOWN);
798:
799: connected_jobs = job_count ();
800:
1.22 snw 801: logprintf (FM_LOG_INFO, "environment: STOP requested by pid %ld", stop_requester);
802: logprintf (FM_LOG_INFO, "environment: there are %d job(s) connected to this environment", connected_jobs);
1.1 snw 803:
804: if (connected_jobs > 1) {
805:
1.22 snw 806: logprintf (FM_LOG_INFO, "environment: asking non-daemon job(s) to disconnect and halt...");
1.1 snw 807: job_request_all_stop ();
808:
1.22 snw 809: logprintf (FM_LOG_INFO, "environment: waiting 5 seconds for job(s) to disconnect...");
1.1 snw 810: sleep (5);
811:
812: connected_jobs = job_count ();
1.22 snw 813: if (connected_jobs > 1) {
814: logprintf (FM_LOG_INFO, "environment: sending SIGTERM to %d job(s)...", connected_jobs);
1.1 snw 815: job_signal_all (SIGTERM);
1.22 snw 816: logprintf (FM_LOG_INFO, "environment: waiting 5 seconds for job(s) to disconnect...");
1.1 snw 817:
818: sleep (5);
819: }
820:
821: connected_jobs = job_count ();
822: if (connected_jobs > 1) {
1.22 snw 823: logprintf (FM_LOG_INFO, "environment: sending SIGKILL to %d job(s)...", connected_jobs);
1.1 snw 824: job_signal_all (SIGKILL);
825: }
826:
827: job_gc_mark ();
828: job_gc_sweep ();
829:
830: }
831:
1.22 snw 832: logprintf (FM_LOG_INFO, "freem: terminating");
1.1 snw 833: cleanup ();
1.11 snw 834:
1.1 snw 835: exit (0);
836:
837: }
838:
839: job_gc_mark ();
840: job_set_status (pid, JSTAT_IDLE);
841: sleep (1);
842:
843: job_set_status (pid, JSTAT_HOUSEKEEPING);
844: job_gc_sweep ();
845: sleep (1);
846: }
847:
848: }
849:
850:
851: #if !defined(_AIX)
1.18 snw 852: if (import_env == TRUE) {
1.1 snw 853: int i_maxlen = 255;
854:
1.18 snw 855: for (env = envp; *env != 0; env++) {
856: varname = strtok (*env, "=");
857: varval = strtok (NULL, "=");
1.1 snw 858:
1.18 snw 859: if (varval != NULL) {
1.1 snw 860: snprintf (symname, i_maxlen, "ENV.%s\201\201", varname);
1.19 snw 861: strncpy (symval, varval, i_maxlen - 1);
1.1 snw 862:
863: stcnv_c2m (symval);
864:
865: symtab (set_sym, symname, symval);
866: }
867: }
868: }
869: #endif
870:
871:
872: if (direct_mode == TRUE && quiet_mode == FALSE) {
873:
874: char verstr[500];
875: char version[256];
876:
877: stcpy (verstr, FREEM_VERSION_STR);
878: stcnv_m2c (verstr);
879:
1.20 snw 880: snprintf (version, sizeof (version) - 1, "\r\nCoherent Logic Development FreeM version %s [DIALECT: %s%s]\r\n\201", verstr, m_dialect, (restricted_mode == TRUE ? "/RESTRICTED" : ""));
1.1 snw 881: write_m (version);
882:
1.20 snw 883: snprintf (version, sizeof (version) - 1, "Copyright (C) 2014, 2020, 2021, 2023, 2025 Coherent Logic Development LLC\r\n\r\n\201");
1.1 snw 884: write_m (version);
885:
1.5 snw 886: /*
1.1 snw 887: printf ("Environment: \t%s\r\n", shm_env);
888: printf ("Environment Daemon:\tPID %d\r\n", shm_config->hdr->first_process);
889: printf ("Interpreter Process:\tPID %d\r\n", pid);
1.5 snw 890: */
1.1 snw 891:
892: }
893: else {
894: write_m ("\r\n\r\n\201");
895: }
896:
897: if (dx_mode) {
898: char k_buf[512];
899:
1.20 snw 900: snprintf (k_buf, sizeof (k_buf) - 1, "%%TMPINITMCODE\201\201");
1.1 snw 901: symtab (set_sym, k_buf, dx_mcode);
902: const_define (k_buf, dx_mcode);
903: }
904:
905: if (routine_mode) {
906: char k_buf[512];
907:
1.20 snw 908: snprintf (k_buf, sizeof (k_buf) - 1, "%%TMPINITROUTINE\201\201");
1.1 snw 909: symtab (set_sym, k_buf, startup_routine);
910: const_define (k_buf, startup_routine);
911: }
912:
913: /* run mumps */
914: xecline (1);
915:
916: exit (0); /* we should never reach that statement */
917:
918: } /* end of main() */
919:
920: void freem_usage(void)
921: {
922: fprintf (stdout, "\nusage: freem [OPTION...]\n\n");
923:
924: fprintf (stdout, "OPTIONS:\n\n");
925:
926: #if defined(HAVE_GETOPT_LONG)
927: fprintf (stdout, "\t-h, --help\n\t\tdisplays this help message\n\n");
928: fprintf (stdout, "\t-i, --import\n\t\timports UNIX environment variables as M locals\n\n");
929: fprintf (stdout, "\t-e <environment-name>, --environment=<environment-name>\n\t\tsets active environment to <environment-name> (DEFAULT if unspecified)\n\n");
930: fprintf (stdout, "\t-f, --filter\n\t\tallows M code to be used as a filter\n\n");
931: fprintf (stdout, "\t-n <NAMESPACE>, --namespace=<NAMESPACE>\n\t\tselects <NAMESPACE> as the startup namespace instead of USER\n\n");
932: fprintf (stdout, "\t-q, --quiet\n\t\tdisables startup messages and prompt string\n\n");
933: fprintf (stdout, "\t-r <LABEL^ROUTINE>, --routine=<LABEL^ROUTINE>\n\t\texecute <LABEL^ROUTINE> on startup instead of entering direct mode\n\n");
934: fprintf (stdout, "\t-s, --standard\n\t\trestrict access to FreeM vendor extensions not present in relevant standards*\n\n");
935: fprintf (stdout, "\t-v, --version\n\t\tdisplay FreeM version information\n\n");
936: fprintf (stdout, "\t-x <MCODE>, --execute=<MCODE>\n\t\texecute M code <MCODE> on startup\n\n");
937: fprintf (stdout, "\t-d, --daemon\n\t\trun the FreeM daemon (one and only one FreeM daemon must always be running)\n\n");
938: fprintf (stdout, "\t-k, --nofork\n\t\trun the FreeM daemon in foreground (requires --daemon)\n\n");
939: fprintf (stdout, "\t-S <BYTES>, --shmsize=<BYTES>\n\t\tsets the size of the shared memory segment where FreeM stores the job table, lock table, and IPC table.\n");
940: #else
941: fprintf (stdout, "\t-h\n\t\tdisplays this help message\n\n");
942: fprintf (stdout, "\t-i\n\t\timports UNIX environment variables as M locals\n\n");
943: fprintf (stdout, "\t-e <environment-name>\n\t\tsets active environment to <environment-name> (DEFAULT if unspecified)\n\n");
944: fprintf (stdout, "\t-f\n\t\tallows M code to be used as a filter\n\n");
945: fprintf (stdout, "\t-n <NAMESPACE>\n\t\tselects <NAMESPACE> as the startup namespace instead of USER\n\n");
946: fprintf (stdout, "\t-q\n\t\tdisables startup messages and prompt string\n\n");
947: fprintf (stdout, "\t-r <LABEL^ROUTINE>\n\t\texecute <LABEL^ROUTINE> on startup instead of entering direct mode\n\n");
948: fprintf (stdout, "\t-s\n\t\trestrict access to FreeM vendor extensions not present in relevant standards*\n\n");
949: fprintf (stdout, "\t-v\n\t\tdisplay FreeM version information\n\n");
950: fprintf (stdout, "\t-x <MCODE>\n\t\texecute M code <MCODE> on startup\n\n");
951: fprintf (stdout, "\t-d\n\t\trun the FreeM daemon (one and only one FreeM daemon must always be running)\n\n");
952: fprintf (stdout, "\t-k\n\t\trun the FreeM daemon in foreground (requires --daemon)\n\n");
953: fprintf (stdout, "\t-S <BYTES>\n\t\tsets the size of the shared memory segment where FreeM stores the job table, lock table, and IPC table.\n");
954: #endif
955: fprintf (stdout, "\t\t - Each concurrent job takes %d bytes (1 page) of shared memory\n", PG_SIZE);
956: fprintf (stdout, "\t\t - Each LOCK takes %d bytes (2 pages) of shared memory\n", PG_SIZE * 2);
957: fprintf (stdout, "\t\t - Each IPC takes %d bytes (1 page) of shared memory\n\n", PG_SIZE);
958: fprintf (stdout, "\t* FreeM attempts to conform (at least loosely) to the Millennium Draft Standard when this mode is selected.\n\n\n");
959: fprintf (stdout, "Report bugs to: freem-bugs@coherent-logic.com\n");
960: fprintf (stdout, "FreeM home page: <https://freem.coherent-logic.com>\n\n");
961:
962: exit (1);
963: }
964:
965: void freem_print_version(void)
966: {
967: char verstr[500];
968: stcpy (verstr, FREEM_VERSION_STR);
969: stcnv_m2c (verstr);
970:
971: fprintf (stdout, "Coherent Logic Development FreeM %s\n", verstr);
972: fprintf (stdout, "Copyright (C) 2014, 2020, 2021, 2023 Coherent Logic Development LLC\n\n");
973: fprintf (stdout, "License AGPLv3+: GNU AGPL version 3 or later <https://gnu.org/license/agpl-3.0.html>\n");
974: fprintf (stdout, "This is free software: you are free to change and redistribute it.\n");
975: fprintf (stdout, "There is NO WARRANTY, to the extent permitted by law.\n");
976:
977: exit (0);
978: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>