--- freem/src/fmadm.c 2025/04/02 04:50:49 1.25 +++ freem/src/fmadm.c 2025/04/02 14:37:57 1.26 @@ -1,5 +1,5 @@ /* - * $Id: fmadm.c,v 1.25 2025/04/02 04:50:49 snw Exp $ + * $Id: fmadm.c,v 1.26 2025/04/02 14:37:57 snw Exp $ * FreeM Administration Tool * * @@ -24,6 +24,9 @@ * along with FreeM. If not, see . * * $Log: fmadm.c,v $ + * Revision 1.26 2025/04/02 14:37:57 snw + * Improve environment control parts of fmadm + * * Revision 1.25 2025/04/02 04:50:49 snw * Allow vendor routines to be upgraded * @@ -321,10 +324,10 @@ int main (int argc, char **argv) snprintf (env_config_file, 4096, "%s/freem/env.conf", SYSCONFDIR); - /* +/* 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]); exit(1); - */ +*/ /* override for fmadm configure and daemon stuff */ if (got_action) { @@ -339,22 +342,22 @@ int main (int argc, char **argv) else if (strcmp (argv[1], "start") == 0 && strcmp (argv[2], "environment") == 0) { act = ACT_START; obj = OBJ_DAEMON; - goto act_switch; + goto process_args; } else if (strcmp (argv[1], "stop") == 0 && strcmp (argv[2], "environment") == 0) { act = ACT_STOP; obj = OBJ_DAEMON; - goto act_switch; + goto process_args; } else if (strcmp (argv[1], "restart") == 0 && strcmp (argv[2], "environment") == 0) { act = ACT_RESTART; obj = OBJ_DAEMON; - goto act_switch; + goto process_args; } else if (strcmp (argv[1], "status") == 0 && strcmp (argv[2], "environment") == 0) { act = ACT_STATUS; obj = OBJ_DAEMON; - goto act_switch; + goto process_args; } } @@ -377,7 +380,8 @@ int main (int argc, char **argv) } set_namespace (fma_namespace, FALSE); - + +process_args: /* allocate opts array */ /* first dimension */ @@ -400,7 +404,8 @@ int main (int argc, char **argv) j = 1; for (i = base_arg; i < argc; i++) { - if (i > FMA_MAXARGS) return fmadm_usage(); /* bail if we're going to overrun the array */ + if (i > FMA_MAXARGS) return fmadm_usage(); + /* bail if we're going to overrun the array */ strncpy (opts[j++], argv[i], STRLEN - 1); } @@ -420,6 +425,7 @@ int main (int argc, char **argv) else if (strncmp (action, "start", STRLEN - 1) == 0) act = ACT_START; else if (strncmp (action, "stop", STRLEN - 1) == 0) act = ACT_STOP; else if (strncmp (action, "restart", STRLEN - 1) == 0) act = ACT_RESTART; + else if (strncmp (action, "status", STRLEN - 1) == 0) act = ACT_STATUS; else return fmadm_usage(); if (strncmp (obj_str, "lock", STRLEN - 1) == 0) obj = OBJ_LOCK; @@ -1144,6 +1150,65 @@ long fm_get_pid (char *env) } } +int fm_validate_environment (char *env) +{ + FILE *fp; + char line[255]; + char chkline[255]; + + snprintf (chkline, 254, "[%s]\n", env); + + if ((fp = fopen (env_config_file, "r")) == NULL) { + fprintf (stderr, "fmadm: could not open %s [%s]\n", env_config_file, strerror (errno)); + return FALSE; + } + + while (fgets (line, 254, fp)) { + if (strncmp (line, chkline, 254) == 0) { + fclose (fp); + return TRUE; + } + } + + fclose (fp); + return FALSE; +} + +int fm_start_environment (char *env, char *e_user, char *e_grp) +{ + char basecmd[255]; + char cmd[4096]; + +#if !defined(__OS2__) + snprintf (basecmd, 254, "%s/bin/freem", PREFIX); +#else + snprintf (basecmd, 254, "%s/bin/freemd.exe", PREFIX); +#endif + +#if !defined(__OS2__) + snprintf (cmd, 4095, "%s -d -e %s -u %s -g %s", basecmd, cur_env, e_user, e_grp); +#else + sprintf (cmd, 4095, "%s -d -k -e %s -u %s -g %s", basecmd, cur_env, e_user, e_grp); +#endif + + return (system (cmd)); +} + +int fm_stop_environment (char *env) +{ + long epid; + + epid = fm_get_pid (cur_env); + if (epid > -1) { + fprintf (stderr, "fmadm: stopping environment daemon pid %d\n", epid); + kill (epid, SIGINT); + kill (epid, SIGTERM); + } + else { + fprintf (stderr, "fmadm: could not obtain environment daemon pid\n"); + } +} + int fm_daemonctl (short action, short object, int optc, char **options) { FILE *ef; @@ -1152,21 +1217,14 @@ int fm_daemonctl (short action, short ob char line[255]; char tmps[255]; char *cur_env; - char cmd[4096]; char verb[40]; char e_user[255]; char e_grp[255]; char e_ena[10]; - char basecmd[255]; char *savptr; int result; long epid; - -#if !defined(__OS2__) - snprintf (basecmd, 254, "%s/bin/freem", PREFIX); -#else - snprintf (basecmd, 254, "%s/bin/freemd.exe", PREFIX); -#endif + switch (action) { case ACT_START: @@ -1188,7 +1246,7 @@ int fm_daemonctl (short action, short ob envlist = (char *) malloc (sizeof (char) * BIGSTR); NULLPTRCHK(envlist,"fm_daemonctl"); - strcpy (envlist, options[0]); + strcpy (envlist, options[1]); } else { /* no environment specified; do 'action' for all environments */ @@ -1213,6 +1271,12 @@ int fm_daemonctl (short action, short ob savptr = envlist; cur_env = strtok_r (envlist, ",", &savptr); do { + + if (fm_validate_environment (cur_env) == FALSE) { + fprintf (stderr, "fmadm: %s is not a valid environment\n", cur_env); + continue; + } + result = read_profile_string (env_config_file, cur_env, "enabled", e_ena); if (result == FALSE || strcmp (e_ena, "true") == 0) { @@ -1228,48 +1292,25 @@ int fm_daemonctl (short action, short ob printf ("fmadm: %s environment %s\n", verb, cur_env); switch (action) { + case ACT_START: -#if !defined(__OS2__) - snprintf (cmd, 4095, "%s -d -e %s -u %s -g %s", basecmd, cur_env, e_user, e_grp); -#else - sprintf (cmd, 4095, "%s -d -k -e %s -u %s -g %s", basecmd, cur_env, e_user, e_grp); -#endif - system (cmd); + result = fm_start_environment (cur_env, e_user, e_grp); break; + case ACT_STOP: - epid = fm_get_pid (cur_env); - if (epid > -1) { - fprintf (stderr, "fmadm: stopping environment daemon pid %d\n", epid); - kill (epid, SIGINT); - kill (epid, SIGTERM); - } - else { - fprintf (stderr, "fmadm: could not obtain environment daemon pid\n"); - } - + result = fm_stop_environment (cur_env); break; + case ACT_RESTART: - epid = fm_get_pid (cur_env); - if (epid > -1) { - fprintf (stderr, "fmadm: stopping environment daemon pid %d\n", epid); - kill (epid, SIGINT); - kill (epid, SIGTERM); - } - else { - fprintf (stderr, "fmadm: could not obtain environment daemon pid\n"); - } + result = fm_stop_environment (cur_env); + fprintf (stderr, "fmadm: waiting 2 seconds\n"); sleep (2); fprintf (stderr, "fmadm: starting environment %s\n", cur_env); -#if !defined(__OS2__) - snprintf (cmd, 4095, "%s -d -e %s -u %s -g %s", basecmd, cur_env, e_user, e_grp); -#else - sprintf (cmd, 4095, "%s -d -k -e %s -u %s -g %s", basecmd, cur_env, e_user, e_grp); -#endif - system (cmd); - + result = fm_start_environment (cur_env, e_user, e_grp); break; + case ACT_STATUS: epid = fm_get_pid (cur_env); if (epid > -1) {