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

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

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