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

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

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