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