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

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

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