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

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

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