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

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

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