Annotation of freem/src/fmadm.c, revision 1.35

1.1       snw         1: /*
1.35    ! snw         2:  *   $Id: fmadm.c,v 1.34 2025/04/10 15:31:25 snw Exp $
1.1       snw         3:  *    FreeM Administration Tool
                      4:  *
                      5:  *  
1.6       snw         6:  *   Author: Serena Willis <snw@coherent-logic.com>
1.1       snw         7:  *    Copyright (C) 1998 MUG Deutschland
1.7       snw         8:  *    Copyright (C) 2020, 2023, 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.8       snw        26:  *   $Log: fmadm.c,v $
1.35    ! snw        27:  *   Revision 1.34  2025/04/10 15:31:25  snw
        !            28:  *   Attempt to fix DosCopy calls for OS/2
        !            29:  *
1.34      snw        30:  *   Revision 1.33  2025/04/10 01:24:38  snw
                     31:  *   Remove C++ style comments
                     32:  *
1.33      snw        33:  *   Revision 1.32  2025/04/09 19:52:02  snw
                     34:  *   Eliminate as many warnings as possible while building with -Wall
                     35:  *
1.32      snw        36:  *   Revision 1.31  2025/04/04 19:43:18  snw
                     37:  *   Switch to using environment catalog to determine user and group for environment, and remove -u and -g flags from freem
                     38:  *
1.31      snw        39:  *   Revision 1.30  2025/04/03 20:48:14  snw
                     40:  *   Improve daemon error diagnostics and bump to 0.63.0-rc3
                     41:  *
1.30      snw        42:  *   Revision 1.29  2025/04/03 01:41:02  snw
                     43:  *   New features frozen; prepare 0.63.0-rc1
                     44:  *
1.29      snw        45:  *   Revision 1.28  2025/04/02 19:59:38  snw
                     46:  *   Automatically modify env.conf from fmadm reconfigure
                     47:  *
1.28      snw        48:  *   Revision 1.27  2025/04/02 15:36:25  snw
                     49:  *   Do extensive result checking for environment stop/start/restart in fmadm
                     50:  *
1.27      snw        51:  *   Revision 1.26  2025/04/02 14:37:57  snw
                     52:  *   Improve environment control parts of fmadm
                     53:  *
1.26      snw        54:  *   Revision 1.25  2025/04/02 04:50:49  snw
                     55:  *   Allow vendor routines to be upgraded
                     56:  *
1.25      snw        57:  *   Revision 1.24  2025/04/02 03:02:42  snw
                     58:  *   Stop requiring users to pass -e to fmadm when -u or -g are passed
                     59:  *
1.24      snw        60:  *   Revision 1.23  2025/04/02 02:16:27  snw
                     61:  *   Add fmadm status environment command and move journals to a better location
                     62:  *
1.23      snw        63:  *   Revision 1.22  2025/04/01 23:21:45  snw
                     64:  *   fmadm commands for stopping, starting, and restarting environments now functional
                     65:  *
1.22      snw        66:  *   Revision 1.21  2025/04/01 20:11:46  snw
                     67:  *   Further work on fmadm
                     68:  *
1.21      snw        69:  *   Revision 1.20  2025/04/01 16:37:12  snw
                     70:  *   Configure DEFAULT environment the same as others, and set permissions/ownership directly in fmadm configure. Add env.conf file as a centralized configuration listing all environments.
                     71:  *
1.20      snw        72:  *   Revision 1.19  2025/04/01 14:32:11  snw
                     73:  *   Begin work on environment and namespace reorg
                     74:  *
1.19      snw        75:  *   Revision 1.18  2025/03/31 16:33:56  snw
                     76:  *   Work on fmadm edit global
                     77:  *
1.18      snw        78:  *   Revision 1.17  2025/03/30 01:36:58  snw
                     79:  *   Make it easier to bring back fma_gedit, fix double-free in global handler, limit $CHAR to 7-bit ASCII
                     80:  *
1.17      snw        81:  *   Revision 1.16  2025/03/24 20:59:58  snw
                     82:  *   Try using DosCopy API instead of built-in cp function on OS/2
                     83:  *
1.16      snw        84:  *   Revision 1.15  2025/03/24 20:58:05  snw
                     85:  *   Try using DosCopy API instead of built-in cp function on OS/2
                     86:  *
1.15      snw        87:  *   Revision 1.14  2025/03/24 20:57:06  snw
                     88:  *   Try using DosCopy API instead of built-in cp function on OS/2
                     89:  *
1.14      snw        90:  *   Revision 1.13  2025/03/24 20:15:09  snw
                     91:  *   Set file permissions on freemd.exe on OS/2 in fmadm configure
                     92:  *
1.13      snw        93:  *   Revision 1.12  2025/03/24 20:13:34  snw
                     94:  *   Set file permissions on freemd.exe on OS/2 in fmadm configure
                     95:  *
1.12      snw        96:  *   Revision 1.11  2025/03/24 19:25:48  snw
                     97:  *   Make fmadm configure copy freem.exe to freemd.exe for daemon operation on OS/2 systems
                     98:  *
1.11      snw        99:  *   Revision 1.10  2025/03/24 19:22:16  snw
                    100:  *   Make fmadm configure copy freem.exe to freemd.exe for daemon operation on OS/2 systems
                    101:  *
1.10      snw       102:  *   Revision 1.9  2025/03/24 19:19:42  snw
                    103:  *   Make fmadm configure copy freem.exe to freemd.exe for daemon operation on OS/2 systems
                    104:  *
1.9       snw       105:  *   Revision 1.8  2025/03/22 18:43:54  snw
                    106:  *   Make STRLEN 255 chars and add BIGSTR macro for larger buffers
                    107:  *
1.8       snw       108:  *   Revision 1.7  2025/03/09 19:14:25  snw
                    109:  *   First phase of REUSE compliance and header reformat
                    110:  *
1.7       snw       111:  *
                    112:  * SPDX-FileCopyrightText:  (C) 2025 Coherent Logic Development LLC
                    113:  * SPDX-License-Identifier: AGPL-3.0-or-later 
1.1       snw       114:  **/
                    115: 
                    116: #include <sys/types.h>
                    117: #include <sys/stat.h>
1.19      snw       118: #include <pwd.h>
                    119: #include <grp.h>
1.1       snw       120: #include <stddef.h>
                    121: #include <stdio.h>
                    122: #include <string.h>
                    123: #include <dirent.h>
                    124: #include <stdlib.h>
                    125: #include <unistd.h>
                    126: #include <errno.h>
1.4       snw       127: #include <ctype.h>
1.1       snw       128: #include "config.h"
                    129: #include "transact.h"
                    130: #include "namespace.h"
                    131: #include "fs.h"
                    132: 
1.14      snw       133: #if defined(__OS2__)
                    134: # include <os2.h>
                    135: #endif
                    136: 
1.1       snw       137: #ifdef HAVE_LIBREADLINE
                    138: #  if defined(HAVE_READLINE_READLINE_H)
                    139: #    include <readline/readline.h>
                    140: #  elif defined(HAVE_READLINE_H)
                    141: #    include <readline.h>
                    142: #  else /* !defined(HAVE_READLINE_H) */
                    143: extern char *readline ();
                    144: #  endif /* !defined(HAVE_READLINE_H) */
                    145: /*char *cmdline = NULL;*/
                    146: #else /* !defined(HAVE_READLINE_READLINE_H) */
                    147:   /* no readline */
                    148: #endif /* HAVE_LIBREADLINE */
                    149: 
                    150: #ifdef HAVE_READLINE_HISTORY
                    151: #  if defined(HAVE_READLINE_HISTORY_H)
                    152: #    include <readline/history.h>
                    153: #  elif defined(HAVE_HISTORY_H)
                    154: #    include <history.h>
                    155: #  else /* !defined(HAVE_HISTORY_H) */
                    156: extern void add_history ();
                    157: extern int write_history ();
                    158: extern int read_history ();
                    159: #  endif /* defined(HAVE_READLINE_HISTORY_H) */
                    160:   /* no history */
                    161: #endif /* HAVE_READLINE_HISTORY */
                    162: 
                    163: 
                    164: #include "fmadm.h"
                    165: #include "errmsg.h"
                    166: #include "iniconf.h"
                    167: #include "init.h"
                    168: #include "version.h"
                    169: #include "shmmgr.h"
                    170: #include "jobtab.h"
                    171: #include "locktab.h"
                    172: 
                    173: /* namespace configuration */
                    174: char fma_environment[STRLEN];
                    175: char fma_namespace[STRLEN];
1.8       snw       176: char fma_routine_path[PATHLEN];
                    177: char fma_global_path[PATHLEN];
                    178: char fma_journal_path[PATHLEN];
                    179: char fma_pct_global_path[PATHLEN];
                    180: char fma_pct_routine_path[PATHLEN];
1.1       snw       181: char fma_journal_cut_threshold[STRLEN];
1.8       snw       182: char fma_locktab[PATHLEN];
1.1       snw       183: short fma_base_opt = 1;
                    184: short fma_min_args = 2;
                    185: short fma_explicit_namespace = FALSE;
                    186: short fma_explicit_environment = FALSE;
                    187: 
                    188: /* miscellaneous global state */
                    189: char obj_str[STRLEN];
                    190: 
                    191: extern char config_file[4096];
1.20      snw       192: extern char env_config_file[4096];
                    193: extern char env_user[255];
                    194: extern char env_group[255];
1.29      snw       195: extern char env_enabled[10];
1.1       snw       196: 
                    197: int fm_shell(void);
                    198: void fm_checkperms(void);
                    199: void fm_reconfigure(void);
                    200: void fm_configure(void);
1.21      snw       201: int fm_daemonctl (short action, short object, int optc, char **options);
1.1       snw       202: void fm_write (FILE *file, char *buf);
                    203: int fma_jobs_remove (int optc, char **opts);
1.20      snw       204: void set_permissions(char *path, char *user, char *grp, int mode);
1.27      snw       205: int fm_environment_running (char *env);
1.21      snw       206: extern int read_profile_string(char *file, char *section, char *key, char *value);
1.1       snw       207: 
                    208: int main (int argc, char **argv)
                    209: {
                    210:     char action[STRLEN];    
                    211: 
                    212:     short act = -1;
                    213:     short obj = -1;
                    214: 
                    215:     char **opts;
                    216:     int optc = argc - 3;
                    217: 
                    218:     int i = 0;
                    219:     int j = 1;
                    220:     int base_arg = 4;
                    221:     int k = 0;
                    222: 
                    223:     short got_action = FALSE;
                    224:     short got_object = FALSE;
                    225: 
1.29      snw       226:     strcpy (env_enabled, "true");
1.1       snw       227: 
                    228:     /* snprintf (config_file, 4096, "%s/freem.conf", SYSCONFDIR); */
                    229: 
                    230:     base_arg = 1;
                    231: 
                    232:     /* enforce action in argv[1] */
                    233:     if (argc > 1) {
                    234:         if (argv[1][0] == '-') {
                    235:             fprintf (stderr, "fmadm:  first argument, if given, must be an action, not a flag\n");
                    236:             fmadm_usage ();
                    237:             exit (1);
                    238:         }
                    239:     }
                    240:     
                    241:     for (i = base_arg; i < argc; i++) {
                    242:         if (i == 1 && isalpha (argv[i][0])) {
                    243:             got_action = TRUE;
                    244:             strncpy (action, argv[i], STRLEN - 1);
                    245:             base_arg++;
                    246:         }
                    247:         if (i == 2 && isalpha (argv[i][0])) {
                    248:             got_object = TRUE;
                    249:             strncpy (obj_str, argv[i], STRLEN - 1);
                    250:             base_arg++;
                    251:         }
                    252:         if (argv[i][0] == '-') {
                    253:                         
                    254:             switch (argv[i][1]) {
1.20      snw       255: 
                    256:                 case 'u':
                    257:                     if (argv[i][2] != '=') {
                    258:                         fprintf (stderr, "fmadm:  missing equals sign in flag -%c\n", argv[i][1]);
                    259:                         fmadm_usage ();
                    260:                         exit (1);
                    261:                     }
                    262: 
                    263:                     k = 0;
                    264:                     
                    265:                     for (j = 3; j < strlen (argv[i]); j++) {
                    266:                         env_user[k++] = argv[i][j];
                    267:                     }
                    268:                     
                    269:                     base_arg++;
                    270:                     
                    271:                     break;
                    272: 
                    273:                 case 'g':
                    274:                     if (argv[i][2] != '=') {
                    275:                         fprintf (stderr, "fmadm:  missing equals sign in flag -%c\n", argv[i][1]);
                    276:                         fmadm_usage ();
                    277:                         exit (1);
                    278:                     }
                    279: 
                    280:                     k = 0;
                    281:                     
                    282:                     for (j = 3; j < strlen (argv[i]); j++) {
                    283:                         env_group[k++] = argv[i][j];
                    284:                     }
                    285:                     
                    286:                     base_arg++;
                    287:                     
                    288:                     break;
                    289:                     
1.1       snw       290:                 
                    291:                 case 'e':
                    292:                     if (argv[i][2] != '=') {
                    293:                         fprintf (stderr, "fmadm:  missing equals sign in flag -%c\n", argv[i][1]);
                    294:                         fmadm_usage ();
                    295:                         exit (1);
                    296:                     }
                    297: 
                    298:                     k = 0;
                    299:                     
                    300:                     for (j = 3; j < strlen (argv[i]); j++) {
                    301:                         fma_environment[k++] = argv[i][j];
                    302:                     }
1.21      snw       303: 
                    304:                     if (strcmp (fma_environment, "all") == 0 ) {
                    305:                         fprintf (stderr, "fmadm:  'all' is an invalid environment name\n");
                    306:                         exit (1);
                    307:                     }
1.1       snw       308:                     
                    309:                     fma_explicit_environment = TRUE;
                    310:                     base_arg++;
                    311:                     
                    312:                     break;
1.29      snw       313: 
                    314:                 case 'E':
                    315:                     if (argv[i][2] != '=') {
                    316:                         fprintf (stderr, "fmadm:  missing equals sign in flag -%c\n", argv[i][1]);
                    317:                         fmadm_usage ();
                    318:                         exit (1);
                    319:                     }
                    320: 
                    321:                     k = 0;
                    322:                     
                    323:                     for (j = 3; j < strlen (argv[i]); j++) {
                    324:                         env_enabled[k++] = argv[i][j];
                    325:                     }
                    326:                     env_enabled[k] = '\0';
                    327: 
                    328:                     if ((strcmp (env_enabled, "true") != 0) && (strcmp (env_enabled, "false") != 0)) {
                    329:                         fprintf (stderr, "fmadm:  -E (environment enabled) option must be either 'true' or 'false'\n");
                    330:                         fmadm_usage ();
                    331:                         exit (1);
                    332:                     }
                    333:                     
                    334:                     base_arg++;
                    335:                     
                    336:                     break;
                    337:                     
1.1       snw       338:                     
                    339:                 case 'n':
                    340:                     if (argv[i][2] != '=') {
                    341:                         fprintf (stderr, "fmadm:  missing equals sign in flag -%c\n", argv[i][1]);
                    342:                         fmadm_usage ();
                    343:                         exit (1);
                    344:                     }
                    345: 
                    346:                     k = 0;
                    347:                     
                    348:                     for (j = 3; j < strlen (argv[i]); j++) {
                    349:                         fma_namespace[k++] = argv[i][j];
                    350:                     }
                    351:                     
                    352:                     fma_explicit_namespace = TRUE;
                    353:                     base_arg++;
                    354:                     
                    355:                     break;                                                
                    356: 
                    357:             }
                    358:         }
                    359:     }
1.24      snw       360:     
1.21      snw       361:     if (obj != OBJ_DAEMON) {
                    362:         if (strlen (env_user) == 0) {
1.35    ! snw       363:             snprintf (env_user, sizeof (env_user) - 1, "freem");
1.21      snw       364:         }
                    365:         
                    366:         if (strlen (env_group) == 0) {
1.35    ! snw       367:             snprintf (env_group, sizeof (env_group) - 1, "freem");
1.21      snw       368:         }
1.20      snw       369: 
1.1       snw       370: 
1.35    ! snw       371:         if (!fma_explicit_environment) snprintf (fma_environment, sizeof (fma_environment) - 1, "DEFAULT");
        !           372:         if (!fma_explicit_namespace) snprintf (fma_namespace, sizeof (fma_namespace) - 1, "SYSTEM");
1.21      snw       373:     
1.35    ! snw       374:         snprintf (config_file, sizeof (config_file) - 1, "%s/freem/%s/freem.conf", SYSCONFDIR, fma_environment);
1.21      snw       375:     }
1.1       snw       376:     
1.35    ! snw       377:     snprintf (env_config_file, sizeof (env_config_file) - 1, "%s/freem/env.conf", SYSCONFDIR);
1.20      snw       378:     
1.26      snw       379: /*
1.1       snw       380:     printf ("action = '%s' object = '%s' environment = '%s' namespace = '%s' config_file = '%s' base_arg = '%d' next argument = '%s'\n", action, obj_str, fma_environment, fma_namespace, config_file, base_arg, argv[base_arg]);
                    381:     exit(1);
1.26      snw       382: */
1.1       snw       383:     
1.21      snw       384:     /* override for fmadm configure and daemon stuff */
1.1       snw       385:     if (got_action) {
                    386:         if (strcmp (argv[1], "configure") == 0) {
                    387:             fm_configure ();
                    388:             exit (0);
                    389:         }
                    390:         else if (strcmp (argv[1], "reconfigure") == 0) {
                    391:             fm_reconfigure ();
                    392:             exit (0);
                    393:         }
1.21      snw       394:         else if (strcmp (argv[1], "start") == 0 && strcmp (argv[2], "environment") == 0) {
                    395:             act = ACT_START;
                    396:             obj = OBJ_DAEMON;
1.26      snw       397:             goto process_args;
1.21      snw       398:         }
                    399:         else if (strcmp (argv[1], "stop") == 0 && strcmp (argv[2], "environment") == 0) {
                    400:             act = ACT_STOP;
                    401:             obj = OBJ_DAEMON;
1.26      snw       402:             goto process_args;
1.21      snw       403:         }
                    404:         else if (strcmp (argv[1], "restart") == 0  && strcmp (argv[2], "environment") == 0) {
                    405:             act = ACT_RESTART;
                    406:             obj = OBJ_DAEMON;
1.26      snw       407:             goto process_args;
1.23      snw       408:         }
                    409:         else if (strcmp (argv[1], "status") == 0  && strcmp (argv[2], "environment") == 0) {
                    410:             act = ACT_STATUS;
                    411:             obj = OBJ_DAEMON;
1.26      snw       412:             goto process_args;
1.21      snw       413:         }        
1.23      snw       414: 
1.1       snw       415:     }
                    416: 
                    417:     pid = getpid ();
                    418:     
                    419:     shm_init (16777216);
                    420:     tp_init ();
                    421:     jobtab_init ();
                    422:     job_init (TRUE);
                    423: 
                    424:     fm_sig_init ();
1.21      snw       425: 
1.1       snw       426:     /* go to fmadm shell if no arguments passed */
                    427:     if (!got_action && !got_object) return fm_shell ();    
                    428:     
                    429:     if (argc > 1 && strcmp (argv[1], "checkperms") == 0) {
                    430:        fm_checkperms ();
                    431:        exit (0);
                    432:     }
                    433:     
                    434:     set_namespace (fma_namespace, FALSE);
1.26      snw       435: 
                    436: process_args:    
1.1       snw       437:     /* allocate opts array */
                    438:     
                    439:     /* first dimension */
                    440:     if ((opts = (char **) malloc (FMA_MAXARGS * sizeof (char *))) == NULL) {   
                    441:         fprintf (stderr, "fmadm [FATAL]:  could not acquire memory\n");
                    442:         return 1;
                    443:     } 
                    444: 
                    445:     /* second dimension */
                    446:     for (i = 0; i < FMA_MAXARGS; i++) {
                    447:         if ((opts[i] = (char *) malloc (STRLEN * sizeof (char *))) == NULL) {
                    448:             fprintf (stderr, "fmadm [FATAL]:  could not acquire memory\n");
                    449:             return 1;
                    450:         } 
                    451:     }
                    452: 
                    453:     /* copy argv[base_arg] through argv[argc - 1] to opts[1] through opts[argc - 3] */
                    454:     
                    455:     strncpy (opts[0], argv[0], STRLEN - 1); /* preserve argv[0] */
                    456: 
                    457:     j = 1;
                    458:     for (i = base_arg; i < argc; i++) {
1.26      snw       459:         if (i > FMA_MAXARGS) return fmadm_usage();        
                    460:         /* bail if we're going to overrun the array */
1.1       snw       461:         strncpy (opts[j++], argv[i], STRLEN - 1);
                    462:     }
                    463:     
                    464:     if (strncmp (action, "list", STRLEN - 1) == 0) act = ACT_LIST;
                    465:     else if (strncmp (action, "examine", STRLEN - 1) == 0) act = ACT_EXAMINE;
                    466:     else if (strncmp (action, "verify", STRLEN - 1) == 0) act = ACT_VERIFY;
                    467:     else if (strncmp (action, "compact", STRLEN - 1) == 0) act = ACT_COMPACT;
                    468:     else if (strncmp (action, "repair", STRLEN - 1) == 0) act = ACT_REPAIR;
                    469:     else if (strncmp (action, "create", STRLEN - 1) == 0) act = ACT_CREATE;
                    470:     else if (strncmp (action, "remove", STRLEN - 1) == 0) act = ACT_REMOVE;
                    471:     else if (strncmp (action, "import", STRLEN - 1) == 0) act = ACT_IMPORT;
                    472:     else if (strncmp (action, "export", STRLEN - 1) == 0) act = ACT_EXPORT;
                    473:     else if (strncmp (action, "backup", STRLEN - 1) == 0) act = ACT_BACKUP;
                    474:     else if (strncmp (action, "restore", STRLEN - 1) == 0) act = ACT_RESTORE;
                    475:     else if (strncmp (action, "migrate", STRLEN - 1) == 0) act = ACT_MIGRATE;
1.21      snw       476:     else if (strncmp (action, "edit", STRLEN - 1) == 0) act = ACT_EDIT;
                    477:     else if (strncmp (action, "start", STRLEN - 1) == 0) act = ACT_START;
                    478:     else if (strncmp (action, "stop", STRLEN - 1) == 0) act = ACT_STOP;
                    479:     else if (strncmp (action, "restart", STRLEN - 1) == 0) act = ACT_RESTART;
1.26      snw       480:     else if (strncmp (action, "status", STRLEN - 1) == 0) act = ACT_STATUS;
1.1       snw       481:     else return fmadm_usage();
                    482: 
                    483:     if (strncmp (obj_str, "lock", STRLEN - 1) == 0) obj = OBJ_LOCK;
                    484:     else if (strncmp (obj_str, "journal", STRLEN - 1) == 0) obj = OBJ_JOURNAL;
                    485:     else if (strncmp (obj_str, "namespace", STRLEN - 1) == 0) obj = OBJ_NAMESPACE;
                    486:     else if (strncmp (obj_str, "global", STRLEN - 1) == 0) obj = OBJ_GLOBAL;
                    487:     else if (strncmp (obj_str, "routine", STRLEN - 1) == 0) obj = OBJ_ROUTINE;
                    488:     else if (strncmp (obj_str, "job", STRLEN - 1) == 0) obj = OBJ_JOB;
1.21      snw       489:     else if (strncmp (obj_str, "environment", STRLEN - 1) == 0) obj = OBJ_DAEMON;
1.1       snw       490:     else return fmadm_usage();
                    491: 
1.21      snw       492:     if (act > ACT_EDIT) goto act_switch;    
                    493:     
1.1       snw       494:     if (get_conf (fma_namespace, "routines_path", fma_routine_path) == FALSE) {
                    495:         fprintf (stderr, "fmadm:  cannot determine routine path for namespace %s\n", fma_namespace);
                    496:         return 1;
                    497:     }   
                    498: 
                    499:     if (get_conf (fma_namespace, "globals_path", fma_global_path) == FALSE) {
                    500:         fprintf (stderr, "fmadm:  cannot determine global path for namespace %s\n", fma_namespace);
                    501:         return 1;
                    502:     }   
                    503: 
                    504:     if (get_conf ("SYSTEM", "globals_path", fma_pct_global_path) == FALSE) {
                    505:         fprintf (stderr, "fmadm:  cannot determine %% global path for namespace %s\n", "SYSTEM");
                    506:         return 1;
                    507:     }
                    508: 
                    509:     if (get_conf ("SYSTEM", "routines_path", fma_pct_routine_path) == FALSE) {
                    510:         fprintf (stderr, "fmadm:  cannot determine %% routine path for namespace %s\n", "SYSTEM");
                    511:         return 1;
                    512:     }
                    513: 
                    514:     if (get_conf ("SYSTEM", "journal_file", fma_journal_path) == FALSE) {
                    515:         strcpy (fma_journal_path, "");
                    516:     }
                    517: 
                    518:     if (get_conf ("SYSTEM", "journal_cut_threshold", fma_journal_cut_threshold) == FALSE) {
                    519:         strcpy (fma_journal_cut_threshold, "1073741824");
                    520:     }
                    521:     
                    522:     strcpy (gloplib, fma_pct_global_path);
                    523:     stcnv_c2m (gloplib);
                    524: 
                    525:     strcpy (glopath, fma_global_path);
                    526:     stcnv_c2m (glopath);
                    527: 
                    528: 
1.21      snw       529: act_switch:
1.1       snw       530:     switch (act) {
                    531: 
                    532:         
                    533:         case ACT_LIST:
                    534:             fmadm_exit (fm_list (obj, optc, opts));
                    535: 
                    536: 
                    537:         case ACT_EXAMINE:
                    538:             fmadm_exit (fm_examine (obj, optc, opts));
                    539: 
                    540: 
                    541:         case ACT_VERIFY:
                    542:             fmadm_exit (fm_verify (obj, optc, opts));
                    543: 
                    544: 
                    545:         case ACT_COMPACT:
                    546:             fmadm_exit (fm_compact (obj, optc, opts));
                    547: 
                    548: 
                    549:         case ACT_REPAIR:
                    550:             fmadm_exit (fm_repair (obj, optc, opts));
                    551: 
                    552: 
                    553:         case ACT_CREATE:
                    554:             fmadm_exit (fm_create (obj, optc, opts));
                    555: 
                    556: 
                    557:         case ACT_REMOVE:
                    558:             fmadm_exit (fm_remove (obj, optc, opts));
                    559: 
                    560: 
                    561:         case ACT_IMPORT:
                    562:             fmadm_exit (fm_import (obj, optc, opts));
                    563: 
                    564: 
                    565:         case ACT_EXPORT:
                    566:             fmadm_exit (fm_export (obj, optc, opts));
                    567: 
                    568: 
                    569:         case ACT_BACKUP:
                    570:             fmadm_exit (fm_backup (obj, optc, opts));
                    571: 
                    572: 
                    573:         case ACT_RESTORE:
                    574:             fmadm_exit (fm_restore (obj, optc, opts));
                    575: 
                    576: 
                    577:         case ACT_MIGRATE:
                    578:             fmadm_exit (fm_migrate (obj, optc, opts));
                    579: 
                    580: 
                    581:         case ACT_EDIT:
                    582:             fmadm_exit (fm_edit (obj, optc, opts));
                    583: 
1.21      snw       584:         case ACT_START:
                    585:         case ACT_STOP:
                    586:         case ACT_RESTART:
1.23      snw       587:         case ACT_STATUS:
1.21      snw       588:             fmadm_exit (fm_daemonctl (act, obj, optc, opts));
1.1       snw       589: 
                    590:         default:
                    591:             return fmadm_usage();
                    592:     }
                    593: 
                    594:     return 0;   /* should never be reached */
                    595: 
                    596: } /* main() */
                    597: 
                    598: int fm_shell (void)
                    599: {
                    600:     
                    601: #if defined(HAVE_LIBREADLINE) && !defined(_AIX)
                    602:     int cmd;
                    603:     int i;
                    604:     int j;
                    605:     int obj;
                    606:     int optc;
                    607:     int argc;
                    608:     char **args;
                    609:     char **opts;    
                    610:     char *fmarl_buf;
                    611:     char *fma_prompt = (char *) malloc (STRLEN * sizeof (char));
                    612:     char *cmdt = (char *) malloc (65535 * sizeof (char));
                    613:     char *result = (char *) malloc (65535 * sizeof (char));
                    614: 
1.33      snw       615:     obj = 0;
1.1       snw       616:     
                    617:     snprintf (fma_prompt, STRLEN - 1, "fmadm [%s]> ", fma_namespace);
                    618:     
                    619:     if (get_conf (fma_namespace, "routines_path", fma_routine_path) == FALSE) {
                    620:         fprintf (stderr, "fmadm:  cannot determine routine path for namespace %s\n", fma_namespace);
                    621:         return 1;
                    622:     }   
                    623: 
                    624:     if (get_conf (fma_namespace, "globals_path", fma_global_path) == FALSE) {
                    625:         fprintf (stderr, "fmadm:  cannot determine global path for namespace %s\n", fma_namespace);
                    626:         return 1;
                    627:     }   
                    628: 
                    629:     if (get_conf ("SYSTEM", "globals_path", fma_pct_global_path) == FALSE) {
                    630:         fprintf (stderr, "fmadm:  cannot determine %% global path for namespace %s\n", "SYSTEM");
                    631:         return 1;
                    632:     }
                    633: 
                    634:     if (get_conf ("SYSTEM", "routines_path", fma_pct_routine_path) == FALSE) {
                    635:         fprintf (stderr, "fmadm:  cannot determine %% routine path for namespace %s\n", "SYSTEM");
                    636:         return 1;
                    637:     }
                    638: 
                    639:     if (get_conf ("SYSTEM", "journal_file", fma_journal_path) == FALSE) {
                    640:         strcpy (fma_journal_path, "");
                    641:     }
                    642: 
                    643:     if (get_conf ("SYSTEM", "journal_cut_threshold", fma_journal_cut_threshold) == FALSE) {
                    644:         strcpy (fma_journal_cut_threshold, "1073741824");
                    645:     }
                    646:     
                    647:     strcpy (gloplib, fma_pct_global_path);
                    648:     stcnv_c2m (gloplib);
                    649: 
                    650:     strcpy (glopath, fma_global_path);
                    651:     stcnv_c2m (glopath);
                    652:     
                    653:     /* allocate args array */
                    654:     
                    655:     /* first dimension */
                    656:     if ((args = (char **) malloc (FMA_MAXARGS * sizeof (char *))) == NULL) {   
                    657:         fprintf (stderr, "fmadm [FATAL]:  could not acquire memory\n");
                    658:         return 1;
                    659:     } 
                    660: 
                    661:     /* second dimension */
                    662:     for (i = 0; i < FMA_MAXARGS; i++) {
                    663:         if ((args[i] = (char *) malloc (STRLEN * sizeof (char *))) == NULL) {
                    664:             fprintf (stderr, "fmadm [FATAL]:  could not acquire memory\n");
                    665:             return 1;
                    666:         } 
                    667:     }
                    668: 
                    669:     /* allocate opts array */
                    670:     
                    671:     /* first dimension */
                    672:     if ((opts = (char **) malloc (FMA_MAXARGS * sizeof (char *))) == NULL) {   
                    673:         fprintf (stderr, "fmadm [FATAL]:  could not acquire memory\n");
                    674:         return 1;
                    675:     } 
                    676: 
                    677:     /* second dimension */
                    678:     for (i = 0; i < FMA_MAXARGS; i++) {
                    679:         if ((opts[i] = (char *) malloc (STRLEN * sizeof (char *))) == NULL) {
                    680:             fprintf (stderr, "fmadm [FATAL]:  could not acquire memory\n");
                    681:             return 1;
                    682:         } 
                    683:     }
                    684: 
                    685:     
                    686:     for (;;) {
                    687: 
                    688:         fmarl_buf = readline (fma_prompt);
                    689: 
                    690:         if (fmarl_buf == (char *) NULL) continue;
                    691:         
                    692:         cmdt = strtok (fmarl_buf, " ");
                    693: 
1.2       snw       694:         if (cmdt == (char *) NULL) continue;
                    695:         
1.1       snw       696:         for (i = 0; i < strlen (cmdt); i++) cmdt[i] = cmdt[i] | 0140;        
                    697:         
                    698:         if (strcmp (cmdt, "exit") == 0) cmd = FMAC_EXIT;
                    699:         else if (strcmp (cmdt, "quit") == 0) cmd = FMAC_EXIT;
                    700:         else if (strcmp (cmdt, "select") == 0) cmd = FMAC_SELECT;
                    701:         else if (strcmp (cmdt, "list") == 0) cmd = FMAC_LIST;
                    702:         else if (strcmp (cmdt, "examine") == 0) cmd = FMAC_EXAMINE;
                    703:         else if (strcmp (cmdt, "verify") == 0) cmd = FMAC_VERIFY;
                    704:         else if (strcmp (cmdt, "compact") == 0) cmd = FMAC_COMPACT;
                    705:         else if (strcmp (cmdt, "repair") == 0) cmd = FMAC_REPAIR;
                    706:         else if (strcmp (cmdt, "create") == 0) cmd = FMAC_CREATE;
                    707:         else if (strcmp (cmdt, "import") == 0) cmd = FMAC_IMPORT;
                    708:         else if (strcmp (cmdt, "export") == 0) cmd = FMAC_EXPORT;
                    709:         else if (strcmp (cmdt, "backup") == 0) cmd = FMAC_BACKUP;
                    710:         else if (strcmp (cmdt, "restore") == 0) cmd = FMAC_RESTORE;
                    711:         else if (strcmp (cmdt, "migrate") == 0) cmd = FMAC_MIGRATE;
                    712:         else if (strcmp (cmdt, "edit") == 0) cmd = FMAC_EDIT;
                    713:         else if (strcmp (cmdt, "set") == 0) cmd = FMAC_SET;
                    714:         else if (strcmp (cmdt, "show") == 0) cmd = FMAC_SHOW;
                    715:         else if (strcmp (cmdt, "remove") == 0) cmd = FMAC_REMOVE;
                    716:         else cmd = FMAC_INVALID;            
                    717: 
                    718:         i = 0;
                    719:         while ((result = strtok (NULL, " ")) != NULL) {
                    720:             strcpy (args[i++], result);
                    721:         }
                    722: 
                    723:         argc = i;
                    724:         j = 0;
                    725:         
                    726:         for (i = 1; i < argc; i++) {
                    727:             strncpy (opts[j++], args[i], STRLEN - 1);
                    728:         }
                    729: 
                    730:         optc = argc - 1;
                    731:         
                    732:         if (i > 0) {
                    733:             
                    734:             strcpy (obj_str, args[0]);
                    735:         
                    736:             if (strncmp (obj_str, "lock", STRLEN - 1) == 0) obj = OBJ_LOCK;
                    737:             else if (strncmp (obj_str, "journal", STRLEN - 1) == 0) obj = OBJ_JOURNAL;
                    738:             else if (strncmp (obj_str, "namespace", STRLEN - 1) == 0) obj = OBJ_NAMESPACE;
                    739:             else if (strncmp (obj_str, "global", STRLEN - 1) == 0) obj = OBJ_GLOBAL;
                    740:             else if (strncmp (obj_str, "routine", STRLEN - 1) == 0) obj = OBJ_ROUTINE;
                    741:             else if (strncmp (obj_str, "job", STRLEN - 1) == 0) obj = OBJ_JOB;
                    742: 
                    743:         }
                    744:         
                    745:         switch (cmd) {
                    746: 
                    747:             
                    748:             case FMAC_SELECT:
                    749:                 
                    750:                 
                    751:                 break;
                    752: 
                    753:                 
                    754:             case FMAC_LIST:
                    755:                 fm_list (obj, optc, opts);
                    756:                 break;
                    757: 
                    758:                 
                    759:             case FMAC_EXAMINE:
                    760:                 fm_examine (obj, optc, opts);
                    761:                 break;
                    762: 
                    763: 
                    764:             case FMAC_VERIFY:
                    765:                 fm_verify (obj, optc, opts);
                    766:                 break;
                    767: 
                    768: 
                    769:             case FMAC_COMPACT:
                    770:                 fm_compact (obj, optc, opts);
                    771:                 break;
                    772: 
                    773: 
                    774:             case FMAC_REPAIR:
                    775:                 fm_repair (obj, optc, opts);
                    776:                 break;
                    777: 
                    778: 
                    779:             case FMAC_CREATE:
                    780:                 fm_create (obj, optc, opts);
                    781:                 break;
                    782: 
                    783: 
                    784:             case FMAC_REMOVE:
                    785:                 fm_remove (obj, optc, opts);
                    786:                 break;
                    787: 
                    788: 
                    789:             case FMAC_IMPORT:
                    790:                 fm_import (obj, optc, opts);
                    791:                 break;
                    792: 
                    793: 
                    794:             case FMAC_EXPORT:
                    795:                 fm_export (obj, optc, opts);
                    796:                 break;
                    797: 
                    798: 
                    799:             case FMAC_BACKUP:
                    800:                 fm_backup (obj, optc, opts);
                    801:                 break;
                    802: 
                    803: 
                    804:             case FMAC_RESTORE:
                    805:                 fm_restore (obj, optc, opts);
                    806:                 break;
                    807: 
                    808: 
                    809:             case FMAC_MIGRATE:
                    810:                 fm_migrate (obj, optc, opts);
                    811:                 break;
                    812: 
                    813: 
                    814:             case FMAC_EDIT:
                    815:                 fm_edit (obj, optc, opts);
                    816:                 break;
                    817: 
                    818: 
                    819:             case FMAC_SET:
                    820: 
                    821:                 if (i < 2) {
                    822:                     printf ("fmadm:  syntax error\n");
                    823:                     break;
                    824:                 }
                    825:                 
                    826:                 if (strcmp (args[0], "namespace") == 0) {
                    827:                     strcpy (fma_namespace, args[1]);
                    828: 
                    829:                     if (get_conf (fma_namespace, "routines_path", fma_routine_path) == FALSE) {
                    830:                         fprintf (stderr, "fmadm:  cannot determine routine path for namespace %s\n", fma_namespace);
                    831:                         return 1;
                    832:                     }   
                    833:                     
                    834:                     if (get_conf (fma_namespace, "globals_path", fma_global_path) == FALSE) {
                    835:                         fprintf (stderr, "fmadm:  cannot determine global path for namespace %s\n", fma_namespace);
                    836:                         return 1;
                    837:                     }   
                    838:                     
                    839:                     if (get_conf ("SYSTEM", "globals_path", fma_pct_global_path) == FALSE) {
                    840:                         fprintf (stderr, "fmadm:  cannot determine %% global path for namespace %s\n", "SYSTEM");
                    841:                         return 1;
                    842:                     }
                    843:                     
                    844:                     if (get_conf ("SYSTEM", "routines_path", fma_pct_routine_path) == FALSE) {
                    845:                         fprintf (stderr, "fmadm:  cannot determine %% routine path for namespace %s\n", "SYSTEM");
                    846:                         return 1;
                    847:                     }
                    848:                     
                    849:                     if (get_conf ("SYSTEM", "journal_file", fma_journal_path) == FALSE) {
                    850:                         strcpy (fma_journal_path, "");
                    851:                     }
                    852:                     
                    853:                     if (get_conf ("SYSTEM", "journal_cut_threshold", fma_journal_cut_threshold) == FALSE) {
                    854:                         strcpy (fma_journal_cut_threshold, "1073741824");
                    855:                     }
                    856:                     
                    857:                     strcpy (gloplib, fma_pct_global_path);
                    858:                     stcnv_c2m (gloplib);
                    859:                     
                    860:                     strcpy (glopath, fma_global_path);
                    861:                     stcnv_c2m (glopath);
                    862: 
                    863:                     snprintf (fma_prompt, STRLEN - 1, "fmadm [%s]> ", fma_namespace);
                    864:                     
                    865:                 }
                    866:                 else if (strcmp (args[0], "maintenance") == 0) {
                    867:                     if (strcmp (args[1], "on") == 0) {
                    868:                         shm_config->hdr->maintenance_mode = 1;
                    869:                         break;
                    870:                     }
                    871:                     else if (strcmp (args[1], "off") == 0) {
                    872:                         shm_config->hdr->maintenance_mode = 0;
                    873:                         break;
                    874:                     }
                    875:                     else {
                    876:                         printf ("fmadm:  syntax error\n");
                    877:                     }
                    878: 
                    879:                     printf ("fmadm:  syntax error\n");
                    880:                         
                    881:                 }
                    882:                 else {
                    883:                     printf ("fmadm:  syntax error\n");
                    884:                     break;
                    885:                 }
                    886:                 
                    887:                 break;
                    888: 
                    889:                 
                    890:             case FMAC_SHOW:
                    891:                 printf ("Namespace:                  %s\n", fma_namespace);
                    892:                 printf ("Routine Path:               %s\n", fma_routine_path);
                    893:                 printf ("%%-Routine Path:             %s\n", fma_pct_routine_path);
                    894:                 printf ("Global Path:                %s\n", fma_global_path);
                    895:                 printf ("%%-Global Path:              %s\n", fma_pct_global_path);
                    896:                 printf ("Journal File:               %s\n", fma_journal_path);
1.5       snw       897:                 printf ("Journal Cut Threshold:      %s bytes\n", fma_journal_cut_threshold);
1.1       snw       898:                 break;
                    899: 
                    900:             case FMAC_EXIT:
                    901:                 fmadm_exit (0);
                    902:                 break;
                    903: 
                    904: 
                    905:             default:
                    906:                 printf ("fmadm:  '%s' is not a valid fmadm command\n", cmdt);
                    907:                 break;
                    908: 
                    909:         }
                    910:     }
                    911: 
                    912: #endif
                    913:     
                    914: }
                    915: 
                    916: void fmadm_exit (int retval)
                    917: {
                    918:     locktab_unlock_all ();
                    919:     job_remove (pid);
                    920:     
                    921:     shm_exit ();
                    922: 
                    923:     exit (retval);
                    924: }
                    925: 
                    926: int fmadm_usage (void)
                    927: {
                    928: 
1.29      snw       929:     fprintf (stdout, "\nusage:  fmadm <action> <object> [-e=<environment] [-n=<namespace>] [-u=<user>] [-g=<group>] [-E=true|false] [OPTIONS]\n");
1.1       snw       930:     fprintf (stdout, "        fmadm configure\n");
                    931:     fprintf (stdout, "        fmadm reconfigure\n");
                    932:     
                    933:     fprintf (stdout, "        <action> can be one of:\n");
                    934:     fprintf (stdout, "            list, examine, verify, compact, repair, create, remove,\n");
1.21      snw       935:     fprintf (stdout, "            import, export, backup, restore, migrate, edit, start,\n");
1.29      snw       936:     fprintf (stdout, "            stop, restart, status\n\n");
1.1       snw       937: 
                    938:     fprintf (stdout, "        <object> can be one of:\n");
1.24      snw       939:     fprintf (stdout, "            lock, journal, namespace, global, routine, job,\n");
1.21      snw       940:     fprintf (stdout, "            environment\n\n");
1.1       snw       941: 
                    942:     fprintf (stdout, "    Not all actions are valid for all objects. Please see the FreeM manual\n");
                    943:     fprintf (stdout, "    for details on fmadm usage and options.\n\n");
                    944:     
                    945:     return 1;
                    946: 
                    947: } /* fmadm_usage() */
                    948: 
                    949: int fm_list (short object, int optc, char **options)
                    950: {
                    951: 
                    952:     switch (object) {
                    953: 
                    954:         case OBJ_LOCK:
                    955:             return fma_locks_list (optc, options);
                    956: 
                    957:         case OBJ_ROUTINE:
                    958:             return fma_routines_list (optc, options);
                    959: 
                    960:         case OBJ_GLOBAL:
                    961:             return fma_globals_list (optc, options);
                    962: 
                    963:         case OBJ_JOB:
                    964:             return fma_jobs_list (optc, options);
                    965: 
                    966:         default:
                    967:             fprintf (stderr, "fmadm:  'list' is an invalid action for '%s'\n", obj_str);
                    968:             return 1;
                    969: 
                    970:     }
                    971: 
                    972: 
                    973: } /* fm_list() */
                    974: 
                    975: int fm_examine (short object, int optc, char **options)
                    976: {
                    977: 
                    978:     switch (object) {
                    979: 
                    980:         case OBJ_ROUTINE:
                    981:             return fma_routines_examine (optc, options);
                    982: 
                    983:         case OBJ_GLOBAL:
                    984:             return fma_globals_examine (optc, options);
                    985: 
                    986:         case OBJ_JOB:
                    987:             return fma_jobs_examine (optc, options);
                    988: 
                    989:         case OBJ_JOURNAL:
                    990:             return fma_journals_examine (optc, options);
                    991:             
                    992:         default:
                    993:             fprintf (stderr, "fmadm:  'examine' is an invalid action for '%s'\n", obj_str);
                    994:             return 1;
                    995: 
                    996:     }
                    997: 
                    998: } /* fm_examine() */
                    999: 
                   1000: int fm_verify (short object, int optc, char **options)
                   1001: {
                   1002: 
                   1003:     switch (object) {
                   1004: 
                   1005:         case OBJ_GLOBAL:
                   1006:             return fma_globals_verify (optc, options);
                   1007:         
                   1008:         default:
                   1009:             fprintf (stderr, "fmadm:  'examine' is an invalid action for '%s'\n", obj_str);
                   1010:             return 1;
                   1011: 
                   1012:     }
                   1013: 
                   1014: } /* fm_verify() */ 
                   1015: 
                   1016: int fm_compact (short object, int optc, char **options)
                   1017: {
                   1018: 
                   1019:     switch (object) {
                   1020: 
                   1021:         default:
                   1022:             fprintf (stderr, "fmadm:  'compact' is an invalid action for '%s'\n", obj_str);
                   1023:             return 1;
                   1024: 
                   1025:     }
                   1026: 
                   1027: } /* fm_compact() */
                   1028: 
                   1029: int fm_repair (short object, int optc, char **options)
                   1030: {
                   1031: 
                   1032:     switch (object) {
                   1033: 
                   1034:         default:
                   1035:             fprintf (stderr, "fmadm:  'repair' is an invalid action for '%s'\n", obj_str);
                   1036:             return 1;
                   1037: 
                   1038:     }
                   1039: 
                   1040: } /* fm_repair() */
                   1041: 
                   1042: int fm_create (short object, int optc, char **options)
                   1043: {
                   1044: 
                   1045:     switch (object) {
                   1046: 
                   1047:         default:
                   1048:             fprintf (stderr, "fmadm:  'create' is an invalid action for '%s'\n", obj_str);
                   1049:             return 1;
                   1050: 
                   1051:     }
                   1052: } /* fm_create() */
                   1053: 
                   1054: int fm_remove (short object, int optc, char **options)
                   1055: {
                   1056: 
                   1057:     switch (object) {
                   1058: 
                   1059:         case OBJ_JOB:
                   1060:             return fma_jobs_remove (optc, options);
                   1061:         
                   1062:         case OBJ_LOCK:
                   1063:             return fma_locks_remove (optc, options);
                   1064: 
                   1065:         case OBJ_ROUTINE:
                   1066:             return fma_routines_remove (optc, options);
                   1067: 
                   1068:         case OBJ_GLOBAL:
                   1069:             return fma_globals_remove (optc, options);
                   1070: 
                   1071:         default:
                   1072:             fprintf (stderr, "fmadm:  'remove' is an invalid action for '%s'\n", obj_str);
                   1073:             return 1;
                   1074: 
                   1075:     }
                   1076: 
                   1077: } /* fm_remove() */
                   1078: 
                   1079: int fm_import (short object, int optc, char **options)
                   1080: {
                   1081: 
                   1082:     switch (object) {
                   1083: 
                   1084:         case OBJ_ROUTINE: 
                   1085:             return fma_routines_import (optc, options);
                   1086: 
                   1087:         default:
                   1088:             fprintf (stderr, "fmadm:  'import' is an invalid action for '%s'\n", obj_str);
                   1089:             return 1;
                   1090: 
                   1091:     }
                   1092: 
                   1093: } /* fm_import() */
                   1094: 
                   1095: int fm_export (short object, int optc, char **options)
                   1096: {
                   1097: 
                   1098:     switch (object) {
                   1099: 
                   1100:         case OBJ_ROUTINE: 
                   1101:             return fma_routines_export (optc, options);
                   1102: 
                   1103:         default:
                   1104:             fprintf (stderr, "fmadm:  'export' is an invalid action for '%s'\n", obj_str);
                   1105:             return 1;
                   1106: 
                   1107:     }
                   1108: 
                   1109: } /* fm_export() */
                   1110: 
                   1111: int fm_backup (short object, int optc, char **options)
                   1112: {
                   1113: 
                   1114:     switch (object) {
                   1115: 
                   1116:         case OBJ_ROUTINE: 
                   1117:             return fma_routines_backup (optc, options);
                   1118: 
                   1119:         default:
                   1120:             fprintf (stderr, "fmadm:  'backup' is an invalid action for '%s'\n", obj_str);
                   1121:             return 1;
                   1122: 
                   1123:     }
                   1124: 
                   1125: } /* fm_backup() */
                   1126: 
                   1127: int fm_restore (short object, int optc, char **options)
                   1128: {
                   1129: 
                   1130:     switch (object) {
                   1131: 
                   1132:         case OBJ_JOURNAL:
                   1133:             return fma_journals_restore (optc, options);
                   1134:         
                   1135:         default:
                   1136:             fprintf (stderr, "fmadm:  'restore' is an invalid action for '%s'\n", obj_str);
                   1137:             return 1;
                   1138: 
                   1139:     }
                   1140: 
                   1141: } /* fm_restore() */
                   1142: 
                   1143: int fm_migrate (short object, int optc, char **options)
                   1144: {
                   1145: 
                   1146:     switch (object) {
                   1147: 
                   1148:         default:
                   1149:             fprintf (stderr, "fmadm:  'migrate' is an invalid action for '%s'\n", obj_str);
                   1150:             return 1;
                   1151: 
                   1152:     }
                   1153: 
                   1154: } /* fm_migrate() */
                   1155: 
                   1156: int fm_edit (short object, int optc, char **options)
                   1157: {
                   1158: 
                   1159:     switch (object) {
                   1160: 
                   1161:         case OBJ_ROUTINE:
                   1162:             return fma_routines_edit (optc, options);
                   1163: 
1.18      snw      1164:         case OBJ_GLOBAL:
1.1       snw      1165:             return fma_globals_edit (optc, options);
1.18      snw      1166:                         
1.1       snw      1167:         default:
                   1168:             fprintf (stderr, "fmadm:  'edit' is an invalid action for '%s'\n", obj_str);
                   1169:             return 1;
                   1170: 
                   1171:     }
                   1172: 
                   1173: } /* fm_edit() */
                   1174: 
1.22      snw      1175: long fm_get_pid (char *env)
                   1176: {
                   1177:     char pid_file[4096];
                   1178:     char tmp_pid[255];
                   1179:     FILE *fp;
                   1180: 
1.35    ! snw      1181:     snprintf (pid_file, sizeof (pid_file) - 1, "%s/freem/run/%s.pid", LOCALSTATEDIR, env);
1.22      snw      1182: 
                   1183:     if ((fp = fopen (pid_file, "r")) != NULL) {
                   1184:         if (fgets (tmp_pid, 255, fp)) {
                   1185:             fclose (fp);
                   1186:             return atol (tmp_pid);            
                   1187:         }
                   1188:         else {
                   1189:             fclose (fp);
                   1190:             return -1;
                   1191:         }        
                   1192:     }
                   1193:     else {
                   1194:         return -1;
                   1195:     }            
                   1196: }
                   1197: 
1.26      snw      1198: int fm_validate_environment (char *env)
                   1199: {
                   1200:     FILE *fp;
                   1201:     char line[255];
                   1202:     char chkline[255];
                   1203: 
1.35    ! snw      1204:     snprintf (chkline, sizeof (chkline) - 1, "[%s]\n", env);
1.26      snw      1205:     
                   1206:     if ((fp = fopen (env_config_file, "r")) == NULL) {
                   1207:         fprintf (stderr, "fmadm:  could not open %s [%s]\n", env_config_file, strerror (errno));
                   1208:         return FALSE;
                   1209:     }
                   1210: 
                   1211:     while (fgets (line, 254, fp)) {
                   1212:         if (strncmp (line, chkline, 254) == 0) {
                   1213:             fclose (fp);
                   1214:             return TRUE;
                   1215:         }
                   1216:     }
                   1217: 
                   1218:     fclose (fp);
                   1219:     return FALSE;
                   1220: }
                   1221: 
1.31      snw      1222: int fm_start_environment (char *env)
1.26      snw      1223: {
                   1224:     char basecmd[255];
                   1225:     char cmd[4096];    
1.27      snw      1226: 
                   1227:     if (fm_environment_running (env) == TRUE) {
                   1228:         return TRUE;
                   1229:     }
1.26      snw      1230:     
                   1231: #if !defined(__OS2__)
1.35    ! snw      1232:     snprintf (basecmd, sizeof (basecmd) - 1, "%s/bin/freem", PREFIX);
1.26      snw      1233: #else
1.35    ! snw      1234:     snprintf (basecmd, sizeof (basecmd) - 1, "%s/bin/freemd.exe", PREFIX);
1.26      snw      1235: #endif
                   1236: 
                   1237: #if !defined(__OS2__)                    
1.35    ! snw      1238:     snprintf (cmd, sizeof (basecmd) - 1, "%s -d -e %s", basecmd, env);
1.26      snw      1239: #else
1.31      snw      1240:     sprintf (cmd, 4095, "%s -d -k -e %s", basecmd, env);
1.26      snw      1241: #endif
1.27      snw      1242: 
                   1243:     system (cmd);
                   1244: 
                   1245:     sleep (1);
1.26      snw      1246:     
1.27      snw      1247:     return (fm_environment_running (env));
1.26      snw      1248: }
                   1249: 
                   1250: int fm_stop_environment (char *env)
                   1251: {
                   1252:     long epid;
                   1253: 
1.27      snw      1254:     epid = fm_get_pid (env);
1.26      snw      1255:     if (epid > -1) {
                   1256:         kill (epid, SIGINT);
1.27      snw      1257:         sleep (5);
                   1258: 
                   1259:         if (fm_environment_running (env) == FALSE) {
                   1260:             return TRUE;
                   1261:         }
                   1262:         else {
                   1263:             kill (epid, SIGTERM);
                   1264:             sleep (5);
                   1265:             if (fm_environment_running (env) == FALSE) {
                   1266:                 return TRUE;
                   1267:             }
                   1268:             else {
                   1269:                 kill (epid, SIGKILL);
                   1270:                 sleep (5);
                   1271:                 if (fm_environment_running (env) == FALSE) {
                   1272:                     return TRUE;
                   1273:                 }
                   1274:                 else {
                   1275:                     return FALSE;
                   1276:                 }
                   1277:             }
                   1278:         }
1.26      snw      1279:     }
                   1280:     else {
1.27      snw      1281:         return FALSE;
1.26      snw      1282:     }   
                   1283: }
                   1284: 
1.27      snw      1285: int fm_environment_running (char *env)
                   1286: {       
                   1287:     long epid;
                   1288:     int result;
                   1289:     
                   1290:     epid = fm_get_pid (env);
                   1291: 
                   1292:     if (epid == -1) {
                   1293:         return FALSE;
                   1294:     }
                   1295:     else {
                   1296:         result = kill (epid, 0);
                   1297: 
                   1298:         return ((result == 0) ? TRUE : FALSE);
                   1299:     }
                   1300: }
                   1301: 
1.21      snw      1302: int fm_daemonctl (short action, short object, int optc, char **options)
                   1303: {
                   1304:     FILE *ef;
                   1305:     char *envlist;
                   1306:     char env[255];
                   1307:     char line[255];
                   1308:     char tmps[255];
                   1309:     char *cur_env;
1.23      snw      1310:     char verb[40];
1.21      snw      1311:     char e_user[255];
                   1312:     char e_grp[255];
                   1313:     char e_ena[10];
                   1314:     char *savptr;
                   1315:     int result;
1.22      snw      1316:     long epid;
1.30      snw      1317:     int retval;
1.26      snw      1318:    
1.30      snw      1319:     retval = 0;
1.21      snw      1320:     
                   1321:     switch (action) {
                   1322:         case ACT_START:
                   1323:             sprintf (verb, "starting");
                   1324:             break;
                   1325:         case ACT_STOP:
                   1326:             sprintf (verb, "stopping");
                   1327:             break;
                   1328:         case ACT_RESTART:
                   1329:             sprintf (verb, "restarting");
                   1330:             break;
1.23      snw      1331:         case ACT_STATUS:
                   1332:             sprintf (verb, "checking status of");
                   1333:             break;
1.21      snw      1334:     }
                   1335:     
                   1336:     if (optc) {
                   1337:         /* environment list specified as command-line argument */
1.23      snw      1338:         envlist = (char *) malloc (sizeof (char) * BIGSTR);
                   1339:         NULLPTRCHK(envlist,"fm_daemonctl");
                   1340: 
1.26      snw      1341:         strcpy (envlist, options[1]);
1.21      snw      1342:     }
                   1343:     else {
                   1344:         /* no environment specified; do 'action' for all environments */
                   1345:         envlist = (char *) malloc (sizeof (char) * BIGSTR);
                   1346:         NULLPTRCHK(envlist,"fm_daemonctl");
                   1347: 
                   1348:         ef = fopen (env_config_file, "r");
                   1349: 
                   1350:         while (fgets (line, 254, ef)) {
                   1351:             if (line[0] == '[') {
                   1352:                 strncpy (env, &(line[1]), 255);
                   1353:                 env[strlen (env) - 2] = '\0';
1.35    ! snw      1354:                 snprintf (tmps, sizeof (tmps) - 1, "%s,", env);
1.21      snw      1355:                 strncat (envlist, tmps, BIGSTR - 1);
                   1356:             }
                   1357:         }
                   1358:         envlist[strlen (envlist) - 1] = '\0';
                   1359: 
                   1360:         fclose (ef);        
                   1361:     }
                   1362: 
                   1363:     savptr = envlist;
                   1364:     cur_env = strtok_r (envlist, ",", &savptr);
                   1365:     do {
1.26      snw      1366:         
                   1367:         if (fm_validate_environment (cur_env) == FALSE) {
                   1368:             fprintf (stderr, "fmadm:  %s is not a valid environment\n", cur_env);
                   1369:             continue;
                   1370:         }
                   1371: 
1.21      snw      1372:         result = read_profile_string (env_config_file, cur_env, "enabled", e_ena);        
                   1373:         if (result == FALSE || strcmp (e_ena, "true") == 0) {
                   1374: 
                   1375:             result = read_profile_string (env_config_file, cur_env, "user", e_user);
                   1376:             if (result == FALSE) {
                   1377:                 strcpy (e_user, "freem");
                   1378:             }
                   1379:             result = read_profile_string (env_config_file, cur_env, "group", e_grp);
                   1380:             if (result == FALSE) {
                   1381:                 strcpy (e_grp, "freem");
                   1382:             }
1.22      snw      1383: 
1.27      snw      1384:             switch (action) {
                   1385:                 case ACT_START:
                   1386:                 case ACT_STOP:
                   1387:                 case ACT_RESTART:
                   1388:                     fprintf (stderr, "fmadm:  %s environment %s... ", verb, cur_env);
                   1389:                     break;
                   1390:                 case ACT_STATUS:
                   1391:                     fprintf (stderr, "fmadm:  %s environment %s\n", verb, cur_env);
                   1392:                     break;
                   1393:             }
1.21      snw      1394: 
                   1395:             switch (action) {
1.26      snw      1396:                 
1.21      snw      1397:                 case ACT_START:
1.31      snw      1398:                     result = fm_start_environment (cur_env);
1.27      snw      1399:                     if (result == TRUE) {
                   1400:                         fprintf (stderr, "[OK]\n");
                   1401:                     }
                   1402:                     else {
                   1403:                         fprintf (stderr, "[FAIL]\n");
                   1404:                     }
1.21      snw      1405:                     break;
1.26      snw      1406: 
1.21      snw      1407:                 case ACT_STOP:
1.26      snw      1408:                     result = fm_stop_environment (cur_env);
1.27      snw      1409:                     if (result == TRUE) {
                   1410:                         fprintf (stderr, "[OK]\n");
                   1411:                     }
                   1412:                     else {
                   1413:                         fprintf (stderr, "[FAIL]\n");
1.30      snw      1414:                         retval++;
1.27      snw      1415:                     }                    
1.22      snw      1416:                     break;
1.26      snw      1417: 
1.21      snw      1418:                 case ACT_RESTART:
1.27      snw      1419:                     if (fm_stop_environment (cur_env) == TRUE) {
1.31      snw      1420:                         result = fm_start_environment (cur_env);
1.27      snw      1421:                         if (result == TRUE) {
                   1422:                             fprintf (stderr, "[OK]\n");
                   1423:                         }
                   1424:                         else {
                   1425:                             fprintf (stderr, "[FAIL]\n");
1.30      snw      1426:                             retval++;
1.27      snw      1427:                         }                        
                   1428:                     }
                   1429:                     else {
                   1430:                         fprintf (stderr, "[FAIL]\n");
1.30      snw      1431:                         retval++;
1.27      snw      1432:                     }                       
1.22      snw      1433: 
1.21      snw      1434:                     break;
1.26      snw      1435:                     
1.23      snw      1436:                 case ACT_STATUS:
                   1437:                     epid = fm_get_pid (cur_env);
                   1438:                     if (epid > -1) {
1.32      snw      1439:                         fprintf (stderr, " - %s environment daemon running as pid %ld\n", cur_env, epid);
1.23      snw      1440:                     }
                   1441:                     else {
                   1442:                         fprintf (stderr, " - %s environment daemon does not appear to be running\n", cur_env);
                   1443:                     }
1.22      snw      1444:             }            
1.21      snw      1445:         }
                   1446:         else {
                   1447:             printf ("fmadm:  %s environment is disabled; skipping\n", cur_env);
                   1448:         }
                   1449:     } while ((cur_env = strtok_r (NULL, ",", &savptr)) != NULL);
                   1450: 
                   1451:     free (envlist);
1.30      snw      1452:     exit (retval);
1.21      snw      1453:     
                   1454: } /* fm_daemonctl() */
                   1455: 
1.1       snw      1456: void fm_checkperms(void)
                   1457: {
                   1458: 
                   1459: } /* fm_checkperms() */
                   1460: 
                   1461: 
                   1462: void fm_reconfigure(void)
                   1463: {
                   1464:     char config_backup[4096];
                   1465:     
                   1466:     int retval;   
                   1467:     
                   1468:     if (geteuid () != 0) {
                   1469:         fprintf (stderr, "fmadm:  not superuser\n");
                   1470:         exit (1);
                   1471:     }
                   1472:     
1.35    ! snw      1473:     snprintf (config_backup, sizeof (config_backup) - 1, "%s.orig", config_file);
1.1       snw      1474: 
                   1475:     fprintf (stderr, "fmadm:  reconfiguring FreeM with system defaults for %s...\n", FREEM_VERSION_CSTR);
                   1476:     fprintf (stderr, "fmadm:  backing up %s to %s...\t", config_file, config_backup);    
                   1477: 
1.29      snw      1478: #if !defined(__OS2__)    
                   1479:     retval = cp (config_backup, config_file);
                   1480: #else
1.34      snw      1481:     retval = DosCopy (config_file, config_backup, 1);
1.29      snw      1482: #endif    
1.1       snw      1483: 
                   1484:     if (retval == 0) {
1.18      snw      1485:        fprintf (stderr, "[OK]\n");
1.1       snw      1486:        
                   1487:        fm_configure ();
                   1488: 
                   1489:        fprintf (stderr, "\n\nYou may wish to edit %s if site-specific changes were made to the original FreeM configuration.\n", config_file);
                   1490:        exit (0);
                   1491:     }
                   1492:     else {
                   1493:        fprintf (stderr, "[FAIL (%s)]\n", strerror (errno));
                   1494:        exit (1);
                   1495:     }
                   1496:     
                   1497: } /* fm_reconfigure() */
                   1498: 
1.29      snw      1499: void update_conf (char *file, char *section, char *key, char *new_value)
                   1500: {
                   1501:     char old_value[255];
                   1502:     char tbuf[255];
                   1503: 
1.35    ! snw      1504:     snprintf (tbuf, sizeof (tbuf) - 1, "%s.%s", section, key);
1.29      snw      1505:         
                   1506:     read_profile_string (file, section, key, old_value);
                   1507:     if (strcmp (old_value, new_value) != 0) {
                   1508:         modify_profile_string (file, section, key, new_value);
                   1509:         fprintf (stderr, "\t%-40s%-20s -> %s\n", tbuf, old_value, new_value);
                   1510:     }
                   1511:     else {
                   1512:         fprintf (stderr, "\t%-40s%-20s\n", tbuf, "no change");
                   1513:     }
                   1514: }
1.1       snw      1515: 
                   1516: void fm_configure (void)
                   1517: {
1.21      snw      1518:     char rundir[4096];
1.19      snw      1519:     char varbase[4096];
1.1       snw      1520:     char sysrtn[4096];
                   1521:     char sysgbl[4096];
                   1522:     char usrrtn[4096];
                   1523:     char usrgbl[4096];
                   1524: 
1.23      snw      1525:     char jnldir[4096];
1.1       snw      1526:     char jnlfile[4096];
                   1527:     char jnlmode[4];
                   1528:     char jnlhostid[4096];    
                   1529:     char jnlcut[4096];
                   1530:     char hostid[4096];
                   1531: 
                   1532:     char confbase[4096];
                   1533:     char envbase[4096];
                   1534: 
                   1535:     char nsbase[4096];
                   1536:     
                   1537:     char buf[4096];
                   1538:     FILE *fp;
                   1539: 
1.29      snw      1540:     int reconfigure = FALSE;
                   1541:     
1.1       snw      1542:     struct stat etcstat;
1.19      snw      1543: 
                   1544:     DIR *dir;
                   1545:     struct dirent *ent;
                   1546:     char src_dir[4096];
                   1547:     char dest_dir[4096];
                   1548: 
1.20      snw      1549:     char *username = env_user;
                   1550:     char *groupname = env_group;
1.28      snw      1551:     
1.20      snw      1552: #if !defined(__OS2__)
1.32      snw      1553:     struct group *d_grp;
                   1554:     struct passwd *d_user;
                   1555: 
1.24      snw      1556:     if (geteuid () != 0) {
                   1557:         fprintf (stderr, "fmadm:  not superuser\n");
                   1558:         exit (1);
                   1559:     }
                   1560: 
1.20      snw      1561:     if ((d_grp = getgrnam (groupname)) == NULL) {
                   1562:         fprintf (stderr, "fmadm:  '%s' group must exist before configuring\n", groupname);
                   1563:         exit (1);
                   1564:     }
                   1565:     if ((d_user = getpwnam (username)) == NULL) {
                   1566:         fprintf (stderr, "fmadm:  '%s' user must exist before configuring\n", username);
                   1567:         exit (1);
                   1568:     }
                   1569: #endif    
1.23      snw      1570:         
1.35    ! snw      1571:     snprintf (varbase, sizeof (varbase) - 1, "%s/freem", LOCALSTATEDIR);
        !          1572:     snprintf (rundir, sizeof (rundir) - 1, "%s/freem/run", LOCALSTATEDIR);
        !          1573:     snprintf (sysrtn, sizeof (sysrtn) - 1, "%s/freem/%s/SYSTEM/routines", LOCALSTATEDIR, fma_environment);
        !          1574:     snprintf (sysgbl, sizeof (sysgbl) - 1, "%s/freem/%s/SYSTEM/globals", LOCALSTATEDIR, fma_environment);
        !          1575:     snprintf (usrrtn, sizeof (usrrtn) - 1, "%s/freem/%s/USER/routines", LOCALSTATEDIR, fma_environment);
        !          1576:     snprintf (usrgbl, sizeof (usrgbl) - 1, "%s/freem/%s/USER/globals", LOCALSTATEDIR, fma_environment);
        !          1577:     snprintf (jnldir, sizeof (jnldir) - 1, "%s/freem/%s/journals", LOCALSTATEDIR, fma_environment);
        !          1578:     snprintf (jnlfile, sizeof (jnlfile) - 1, "%s/freem_journal_%s.dat", jnldir, fma_environment);
        !          1579:     snprintf (jnlmode, sizeof (jnlmode) - 1, "on");
        !          1580:     snprintf (jnlhostid, sizeof (jnlhostid) - 1, "DEFAULT");
        !          1581:     snprintf (jnlcut, sizeof (jnlcut) - 1, "4294967000");
1.1       snw      1582:     
                   1583:     if (geteuid () != 0) {
                   1584:         fprintf (stderr, "fmadm:  not superuser\n");
                   1585:         exit (1);
                   1586:     }
                   1587:     
                   1588:     if (file_exists (config_file)) {
1.29      snw      1589:         reconfigure = TRUE;
1.1       snw      1590:     }
1.29      snw      1591:     
1.1       snw      1592:     gethostname (hostid, 4095);
                   1593:     uuid_v4 (buf);
                   1594: 
1.35    ! snw      1595:     snprintf (jnlhostid, sizeof (jnlhostid) - 1, "%s:%s", hostid, buf);
        !          1596:     snprintf (confbase, sizeof (confbase) - 1, "%s/freem", SYSCONFDIR);
        !          1597:     snprintf (envbase, sizeof (envbase) - 1, "%s/freem/%s", SYSCONFDIR, fma_environment); 
        !          1598:     snprintf (nsbase, sizeof (nsbase) - 1, "%s/freem/%s", LOCALSTATEDIR, fma_environment);
1.1       snw      1599: 
1.9       snw      1600: #if defined(__OS2__)
                   1601:     {
                   1602:         char srcfile[PATHLEN];
1.11      snw      1603:         char dstfile[PATHLEN];        
                   1604:         
1.35    ! snw      1605:         snprintf (srcfile, PATHLEN - 1, "%s/bin/freem.exe", PREFIX);
        !          1606:         snprintf (dstfile, PATHLEN - 1, "%s/bin/freemd.exe", PREFIX);
1.11      snw      1607: 
                   1608:         unlink (dstfile);
1.9       snw      1609:         
1.10      snw      1610:         fprintf (stderr, "fmadm:  running on OS/2; will copy %s to %s\n", srcfile, dstfile);
1.12      snw      1611:         
1.16      snw      1612:         if (DosCopy (srcfile, dstfile, 1) != 0) {
1.9       snw      1613:             fprintf (stderr, "fmadm:  fatal error copying %s to %s\n", srcfile, dstfile);
                   1614:             exit (1);
                   1615:         }
1.13      snw      1616: 
                   1617:         chmod (dstfile, 0755);
1.9       snw      1618:     }
                   1619: #endif
1.1       snw      1620: 
1.29      snw      1621:     if (reconfigure == FALSE) {
                   1622:         printf ("\nFreeM Initial Environment Configuration\n");
                   1623:         printf ("---------------------------------------\n\n");
                   1624: 
                   1625:         printf ("This utility will create the initial configuration files for ");
                   1626:         printf ("FreeM environment '%s' (owned by %s:%s) in '%s'.\n\n", fma_environment, username, groupname, config_file);    
                   1627:     }
                   1628:     else {
                   1629:         printf ("\nFreeM Environment Upgrade/Reconfiguration\n");
                   1630:         printf ("-----------------------------------------\n\n");
                   1631: 
                   1632:         printf ("This utility will update the configuration files for ");
                   1633:         printf ("FreeM environment '%s' (owned by %s:%s) in '%s'.\n\n", fma_environment, username, groupname, config_file);
                   1634:     }
1.9       snw      1635:     
1.1       snw      1636:     /* check for existence of needed directories */
                   1637:     if (stat (SYSCONFDIR, &etcstat) == -1) {
1.23      snw      1638:        fprintf (stderr, "fmadm:  creating %s [SYSCONFDIR]\n", SYSCONFDIR);
1.19      snw      1639:        mkdir (SYSCONFDIR, 0775);
1.20      snw      1640:         set_permissions (SYSCONFDIR, username, groupname, 0775);
1.1       snw      1641:     }
                   1642: 
                   1643:     if (stat (confbase, &etcstat) == -1) {
1.23      snw      1644:         fprintf (stderr, "fmadm:  creating %s [confbase]\n", confbase);
1.19      snw      1645:         mkdir (confbase, 0775);
1.20      snw      1646:         set_permissions (confbase, username, groupname, 0775);
1.1       snw      1647:     }
                   1648: 
1.19      snw      1649:     if (stat (varbase, &etcstat) == -1) {
1.23      snw      1650:         fprintf (stderr, "fmadm:  creating %s [varbase]\n", varbase);
1.19      snw      1651:         mkdir (varbase, 0775);
1.20      snw      1652:         set_permissions (varbase, username, groupname, 0775);
1.19      snw      1653:     }
1.21      snw      1654: 
1.23      snw      1655:     if (stat (envbase, &etcstat) == -1) {
                   1656:         fprintf (stderr, "fmadm:  creating %s [envbase]\n", envbase);
                   1657:         mkdir (envbase, 0775);
                   1658:         set_permissions (envbase, username, groupname, 0775);
                   1659:     }
                   1660:     
1.21      snw      1661:     if (stat (rundir, &etcstat) == -1) {
1.23      snw      1662:         fprintf (stderr, "fmadm:  creating %s [rundir]\n", rundir);
1.21      snw      1663:         mkdir (rundir, 0777);
                   1664:         chmod (rundir, 0777);
                   1665:     }
                   1666: 
1.1       snw      1667:     if (stat (nsbase, &etcstat) == -1) {
1.23      snw      1668:         fprintf (stderr, "fmadm:  creating %s [nsbase]\n", nsbase);
1.19      snw      1669:         mkdir (nsbase, 0775);
1.20      snw      1670:         set_permissions (nsbase, username, groupname, 0775);
1.1       snw      1671:     }
                   1672: 
1.23      snw      1673:     if (stat (jnldir, &etcstat) == -1) {
                   1674:         fprintf (stderr, "fmadm:  creating %s [jnldir]\n", jnldir);
                   1675:         mkdir (jnldir, 0775);   
                   1676:         set_permissions (jnldir, username, groupname, 0775);
                   1677:     }
                   1678: 
                   1679:     
1.35    ! snw      1680:     snprintf (src_dir, sizeof (src_dir) - 1, "%s/freem/mlib", DATADIR);
        !          1681:     snprintf (dest_dir, sizeof (dest_dir) - 1, "%s/freem/%s/SYSTEM/routines", LOCALSTATEDIR, fma_environment);
1.29      snw      1682: 
                   1683:     if (reconfigure == FALSE) {
                   1684:         fprintf (stderr, "fmadm:  populating new environment '%s'\n", fma_environment);
                   1685:     }
                   1686:     else {
                   1687:         fprintf (stderr, "fmadm:  upgrading environment '%s'\n", fma_environment);
                   1688:     }
1.19      snw      1689:     
1.35    ! snw      1690:     snprintf (buf, sizeof (buf) - 1, "%s/freem/%s/SYSTEM", LOCALSTATEDIR, fma_environment);
1.19      snw      1691:     mkdir (buf, 0775);
1.20      snw      1692:     set_permissions (buf, username, groupname, 0775);
1.19      snw      1693:     
1.35    ! snw      1694:     snprintf (buf, sizeof (buf) - 1, "%s/freem/%s/USER", LOCALSTATEDIR, fma_environment);
1.19      snw      1695:     mkdir (buf, 0775);
1.20      snw      1696:     set_permissions (buf, username, groupname, 0775);
1.19      snw      1697:     
1.35    ! snw      1698:     snprintf (buf, sizeof (buf) - 1, "%s/freem/%s/SYSTEM/routines", LOCALSTATEDIR, fma_environment);
1.19      snw      1699:     mkdir (buf, 0775);
1.20      snw      1700:     set_permissions (buf, username, groupname, 0775);
1.19      snw      1701:     
1.35    ! snw      1702:     snprintf (buf, sizeof (buf) - 1, "%s/freem/%s/USER/globals", LOCALSTATEDIR, fma_environment);
1.19      snw      1703:     mkdir (buf, 0775);
1.20      snw      1704:     set_permissions (buf, username, groupname, 0775);
1.19      snw      1705:     
1.35    ! snw      1706:     snprintf (buf, sizeof (buf) - 1, "%s/freem/%s/SYSTEM/globals", LOCALSTATEDIR, fma_environment);
1.19      snw      1707:     mkdir (buf, 0775);
1.20      snw      1708:     set_permissions (buf, username, groupname, 0775);
1.19      snw      1709:     
1.35    ! snw      1710:     snprintf (buf, sizeof (buf) - 1, "%s/freem/%s/USER/routines", LOCALSTATEDIR, fma_environment);
1.19      snw      1711:     mkdir (buf, 0775);
1.20      snw      1712:     set_permissions (buf, username, groupname, 0775);
1.19      snw      1713:     
1.25      snw      1714:     fprintf (stderr, "fmadm:  copying vendor routines from '%s' to '%s'...\n", src_dir, dest_dir);
1.1       snw      1715: 
1.19      snw      1716:     if ((dir = opendir (src_dir)) == NULL) {
                   1717:         fprintf (stderr, "\nfmadm:  could not open source directory %s\n", src_dir);
                   1718:         exit (1);
                   1719:     }
                   1720:     
                   1721:     while ((ent = readdir (dir)) != NULL) {
                   1722:         char infile[4096];
                   1723:         char outfile[4096];
1.1       snw      1724:         
1.25      snw      1725:         if ((strcmp (ent->d_name, ".") != 0) && (strcmp (ent->d_name, "..") != 0)) {           
1.19      snw      1726:             
1.35    ! snw      1727:             snprintf (infile, sizeof (infile) - 1, "%s/%s", src_dir, ent->d_name);
        !          1728:             snprintf (outfile, sizeof (outfile) - 1, "%s/%s", dest_dir, ent->d_name);
1.1       snw      1729: 
1.25      snw      1730:             if (stat (outfile, &etcstat) == 0) {
                   1731:                 unlink (outfile);
                   1732:                 fprintf (stderr, "\tupgrade -> %s\n", ent->d_name);
                   1733:             }
                   1734:             else {
                   1735:                 fprintf (stderr, "\tnew     -> %s\n", ent->d_name);
                   1736:             }
                   1737:             
1.19      snw      1738: #if !defined(__OS2__)            
                   1739:             if (cp (outfile, infile) != 0) {
                   1740:                 fprintf (stderr, "fmadm:  failure copying %s to %s\n", infile, outfile);
                   1741:             }
1.20      snw      1742:             set_permissions (outfile, username, groupname, 0755);
1.19      snw      1743: #else
                   1744:             if (DosCopy (infile, outfile, 1) != 0) {
                   1745:                 fprintf (stderr, "fmadm:  failure copying %s to %s\n", infile, outfile);
1.1       snw      1746:             }
1.19      snw      1747: #endif
1.1       snw      1748:             
                   1749:         }
                   1750:         
                   1751:     }
1.20      snw      1752: 
1.28      snw      1753:     if (fm_validate_environment (fma_environment) == FALSE) {
                   1754:         fp = fopen (env_config_file, "a+");
                   1755:         
                   1756:         fprintf (stderr, "Creating %s... ", env_config_file);
                   1757:         
1.35    ! snw      1758:         snprintf (buf, sizeof (buf) - 1, "[%s]", fma_environment);
1.28      snw      1759:         fm_write (fp, buf);
                   1760:         
1.35    ! snw      1761:         snprintf (buf, sizeof (buf) - 1, "user=%s", env_user);
1.28      snw      1762:         fm_write (fp, buf);
                   1763:         
1.35    ! snw      1764:         snprintf (buf, sizeof (buf) - 1, "group=%s", env_group);
1.28      snw      1765:         fm_write (fp, buf);
                   1766:         
1.35    ! snw      1767:         snprintf (buf, sizeof (buf) - 1, "enabled=%s", env_enabled);
1.28      snw      1768:         fm_write (fp, buf);
                   1769:         
1.35    ! snw      1770:         snprintf (buf, sizeof (buf) - 1, "env_path=%s/freem/%s", LOCALSTATEDIR, fma_environment);
1.28      snw      1771:         fm_write (fp, buf);
                   1772:         
                   1773:         fclose (fp);
                   1774:         fprintf (stderr, "[OK]\n");
                   1775:     }
                   1776:     else {
1.29      snw      1777:         fprintf (stderr, "\nUpdating %s: \n", env_config_file);
1.20      snw      1778: 
1.29      snw      1779:         update_conf (env_config_file, fma_environment, "user", env_user);
                   1780:         update_conf (env_config_file, fma_environment, "group", env_group);
                   1781:         update_conf (env_config_file, fma_environment, "enabled", env_enabled);
1.28      snw      1782:     }
1.29      snw      1783: 
                   1784:     if (reconfigure == FALSE) {
                   1785:         fp = fopen (config_file, "a+");
                   1786:         
                   1787:         fprintf (stderr, "Creating %s... ", config_file); 
                   1788:         
1.35    ! snw      1789:         snprintf (buf, sizeof (buf) - 1, "[SYSTEM]");
1.29      snw      1790:         fm_write (fp, buf);
                   1791:         
1.35    ! snw      1792:         snprintf (buf, sizeof (buf) - 1, "root=%s/freem/%s/SYSTEM", LOCALSTATEDIR, fma_environment);
1.29      snw      1793:         fm_write (fp, buf);
                   1794:         
1.35    ! snw      1795:         snprintf (buf, sizeof (buf) - 1, "routines_path=%s", sysrtn);
1.29      snw      1796:         fm_write (fp, buf);
                   1797:         
1.35    ! snw      1798:         snprintf (buf, sizeof (buf) - 1, "globals_path=%s", sysgbl);
1.29      snw      1799:         fm_write (fp, buf);
                   1800:         
1.35    ! snw      1801:         snprintf (buf, sizeof (buf) - 1, "journal_file=%s", jnlfile);
1.29      snw      1802:         fm_write (fp, buf);
                   1803:         
1.35    ! snw      1804:         snprintf (buf, sizeof (buf) - 1, "journal_mode=%s", jnlmode);
1.29      snw      1805:         fm_write (fp, buf);
                   1806:         
1.35    ! snw      1807:         snprintf (buf, sizeof (buf) - 1, "journal_host_id=%s", jnlhostid);
1.29      snw      1808:         fm_write (fp, buf);
                   1809:         
1.35    ! snw      1810:         snprintf (buf, sizeof (buf) - 1, "journal_cut_threshold=%s", jnlcut);
1.29      snw      1811:         fm_write (fp, buf);
                   1812:         
1.35    ! snw      1813:         snprintf (buf, sizeof (buf) - 1, "zdate_format=%%x");
1.29      snw      1814:         fm_write (fp, buf);
                   1815:         
1.35    ! snw      1816:         snprintf (buf, sizeof (buf) - 1, "ztime_format=%%X");
1.29      snw      1817:         fm_write (fp, buf);
                   1818:         
1.35    ! snw      1819:         snprintf (buf, sizeof (buf) - 1, "\n[USER]");
1.29      snw      1820:         fm_write (fp, buf);
                   1821:         
1.35    ! snw      1822:         snprintf (buf, sizeof (buf) - 1, "root=%s/freem/%s/USER", LOCALSTATEDIR, fma_environment);
1.29      snw      1823:         fm_write (fp, buf);
                   1824:         
1.35    ! snw      1825:         snprintf (buf, sizeof (buf) - 1, "routines_path=%s", usrrtn);
1.29      snw      1826:         fm_write (fp, buf);
                   1827:         
1.35    ! snw      1828:         snprintf (buf, sizeof (buf) - 1, "globals_path=%s", usrgbl);
1.29      snw      1829:         fm_write (fp, buf);
                   1830:         
                   1831:         fclose (fp);
                   1832:         set_permissions (config_file, username, groupname, 0755);
                   1833:         fprintf (stderr, "[OK]\n");
1.1       snw      1834:     
1.29      snw      1835:         printf ("FreeM initial configuration is complete.\n\n");
                   1836:     }
                   1837:     else {
                   1838:         char tmpsd[255];
                   1839:         read_profile_string (config_file, "SYSTEM", "journal_host_id", tmpsd);
                   1840:         
                   1841:         /* existing configuration */
                   1842:         fprintf (stderr, "\nUpdating environment configuration for %s:\n", fma_environment);
1.1       snw      1843: 
1.35    ! snw      1844:         snprintf (buf, sizeof (buf) - 1, "%s/freem/%s/SYSTEM", LOCALSTATEDIR, fma_environment);
1.29      snw      1845:         update_conf (config_file, "SYSTEM", "root", buf);
                   1846:         update_conf (config_file, "SYSTEM", "routines_path", sysrtn);       
                   1847:         update_conf (config_file, "SYSTEM", "globals_path", sysgbl);
                   1848:         update_conf (config_file, "SYSTEM", "journal_file", jnlfile);        
                   1849:         update_conf (config_file, "SYSTEM", "journal_mode", jnlmode);
                   1850:         update_conf (config_file, "SYSTEM", "journal_host_id", tmpsd);
                   1851:         update_conf (config_file, "SYSTEM", "journal_cut_threshold", jnlcut);
                   1852: 
1.35    ! snw      1853:         snprintf (buf, sizeof (buf) - 1, "%%x");
1.29      snw      1854:         update_conf (config_file, "SYSTEM", "zdate_format", buf);
                   1855:         
1.35    ! snw      1856:         snprintf (buf, sizeof (buf) - 1, "%%X");
1.29      snw      1857:         update_conf (config_file, "SYSTEM", "ztime_format", buf);
                   1858: 
                   1859:         /* USER */        
1.35    ! snw      1860:         snprintf (buf, sizeof (buf) - 1, "%s/freem/%s/USER", LOCALSTATEDIR, fma_environment);
1.29      snw      1861:         update_conf (config_file, "USER", "root", buf);        
                   1862:         update_conf (config_file, "USER", "routines_path", usrrtn);
                   1863:         update_conf (config_file, "USER", "globals_path", usrgbl);
1.1       snw      1864: 
1.29      snw      1865:     }
1.1       snw      1866:     
1.20      snw      1867:         
1.29      snw      1868:     printf ("\n\nUSER globals:                   %s\n", usrgbl);
                   1869:     printf ("USER routines:                  %s\n", usrrtn);
                   1870:     printf ("SYSTEM globals:                 %s\n", sysgbl);
                   1871:     printf ("SYSTEM routines:                %s\n", sysrtn);
                   1872:     printf ("After-image journal:            %s [%s]\n", jnlfile, jnlmode);
                   1873:     printf ("Journal cut threshold:          %s bytes\n", jnlcut);
                   1874:     printf ("Distributed journaling host ID: %s\n", jnlhostid);
1.1       snw      1875: 
1.29      snw      1876:     if (reconfigure == TRUE) {
                   1877:         fprintf (stderr, "\nIf you previously defined environments other than '%s', you should run\n'fmadm reconfigure -e=<environment-name>' on each of them to ensure they have\nthe latest vendor routines and correct, updated settings.\n\n", fma_environment);
                   1878:     }
1.1       snw      1879:     
                   1880: } /* fm_configure */
                   1881: 
1.20      snw      1882: void set_permissions(char *path, char *user, char *grp, int mode)
                   1883: {
                   1884:     
                   1885: #if !defined(__OS2__)
                   1886:     struct group *d_grp;
                   1887:     struct passwd *d_user;
                   1888:     gid_t d_gid;
                   1889:     uid_t d_uid;
                   1890: #endif    
                   1891: 
                   1892: 
                   1893: #if !defined(__OS2__)    
                   1894:     if ((d_grp = getgrnam (grp)) == NULL) {
                   1895:         fprintf (stderr, "fmadm:  '%s' group must exist before configuring\n", grp);
                   1896:         exit (1);
                   1897:     }
                   1898:     d_gid = d_grp->gr_gid;
                   1899: 
                   1900:     if ((d_user = getpwnam (user)) == NULL) {
                   1901:         fprintf (stderr, "fmadm:  '%s' user must exist before configuring\n", user);
                   1902:         exit (1);
                   1903:     }
                   1904:     d_uid = d_user->pw_uid;
                   1905: 
                   1906:     if (chown (path, d_uid, d_gid) != 0) {
                   1907:         fprintf (stderr, "fmadm:  error setting ownership on %s\n", path);
                   1908:         exit (1);
                   1909:     }
                   1910: #endif    
                   1911: 
                   1912:     if (chmod (path, mode) != 0) {
                   1913:         fprintf (stderr, "fmadm:  error setting permissions on %s to %d\n", path, mode);
                   1914:         exit (1);
                   1915:     }
                   1916:     
                   1917: }
                   1918: 
1.1       snw      1919: void fm_write (FILE *file, char *buf)
                   1920: {
                   1921:     fprintf (file, "%s\n", buf);
                   1922: }
                   1923: 
                   1924: void fm_sig_attach (int sig, void *handler)
                   1925: {
                   1926:     struct sigaction act;
                   1927: 
                   1928:     act.sa_handler = handler;
                   1929:     sigaction (sig, &act, NULL);
                   1930:     
                   1931: }
                   1932: 
                   1933: void fm_sig_init (void)
                   1934: {
                   1935:     sig_attach (SIGINT, &fm_on_sigint);
                   1936:     sig_attach (SIGTERM, &fm_on_sigterm);
                   1937: }
                   1938: 
                   1939: void fm_on_sigint (void)
                   1940: {
                   1941:     fprintf (stderr, "\nfmadm:  caught SIGINT\n");
                   1942:     fmadm_exit (0);
                   1943: }
                   1944: 
                   1945: void fm_on_sigterm (void)
                   1946: {
                   1947:     fprintf (stderr, "\nfmadm:  caught SIGTERM\n");
                   1948:     fmadm_exit (0);
                   1949: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>