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

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

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