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

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

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