--- freem/src/fmadm.c 2025/04/02 14:37:57 1.26 +++ freem/src/fmadm.c 2025/04/02 15:36:25 1.27 @@ -1,5 +1,5 @@ /* - * $Id: fmadm.c,v 1.26 2025/04/02 14:37:57 snw Exp $ + * $Id: fmadm.c,v 1.27 2025/04/02 15:36:25 snw Exp $ * FreeM Administration Tool * * @@ -24,6 +24,9 @@ * along with FreeM. If not, see . * * $Log: fmadm.c,v $ + * Revision 1.27 2025/04/02 15:36:25 snw + * Do extensive result checking for environment stop/start/restart in fmadm + * * Revision 1.26 2025/04/02 14:37:57 snw * Improve environment control parts of fmadm * @@ -177,6 +180,7 @@ int fm_daemonctl (short action, short ob void fm_write (FILE *file, char *buf); int fma_jobs_remove (int optc, char **opts); void set_permissions(char *path, char *user, char *grp, int mode); +int fm_environment_running (char *env); extern int read_profile_string(char *file, char *section, char *key, char *value); int main (int argc, char **argv) @@ -1178,6 +1182,10 @@ int fm_start_environment (char *env, cha { char basecmd[255]; char cmd[4096]; + + if (fm_environment_running (env) == TRUE) { + return TRUE; + } #if !defined(__OS2__) snprintf (basecmd, 254, "%s/bin/freem", PREFIX); @@ -1186,29 +1194,70 @@ int fm_start_environment (char *env, cha #endif #if !defined(__OS2__) - snprintf (cmd, 4095, "%s -d -e %s -u %s -g %s", basecmd, cur_env, e_user, e_grp); + snprintf (cmd, 4095, "%s -d -e %s -u %s -g %s", basecmd, 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); + sprintf (cmd, 4095, "%s -d -k -e %s -u %s -g %s", basecmd, env, e_user, e_grp); #endif + + system (cmd); + + sleep (1); - return (system (cmd)); + return (fm_environment_running (env)); } int fm_stop_environment (char *env) { long epid; - epid = fm_get_pid (cur_env); + epid = fm_get_pid (env); if (epid > -1) { - fprintf (stderr, "fmadm: stopping environment daemon pid %d\n", epid); kill (epid, SIGINT); - kill (epid, SIGTERM); + sleep (5); + + if (fm_environment_running (env) == FALSE) { + return TRUE; + } + else { + kill (epid, SIGTERM); + sleep (5); + if (fm_environment_running (env) == FALSE) { + return TRUE; + } + else { + kill (epid, SIGKILL); + sleep (5); + if (fm_environment_running (env) == FALSE) { + return TRUE; + } + else { + return FALSE; + } + } + } } else { - fprintf (stderr, "fmadm: could not obtain environment daemon pid\n"); + return FALSE; } } +int fm_environment_running (char *env) +{ + long epid; + int result; + + epid = fm_get_pid (env); + + if (epid == -1) { + return FALSE; + } + else { + result = kill (epid, 0); + + return ((result == 0) ? TRUE : FALSE); + } +} + int fm_daemonctl (short action, short object, int optc, char **options) { FILE *ef; @@ -1289,26 +1338,53 @@ int fm_daemonctl (short action, short ob strcpy (e_grp, "freem"); } - printf ("fmadm: %s environment %s\n", verb, cur_env); + switch (action) { + case ACT_START: + case ACT_STOP: + case ACT_RESTART: + fprintf (stderr, "fmadm: %s environment %s... ", verb, cur_env); + break; + case ACT_STATUS: + fprintf (stderr, "fmadm: %s environment %s\n", verb, cur_env); + break; + } switch (action) { case ACT_START: result = fm_start_environment (cur_env, e_user, e_grp); + if (result == TRUE) { + fprintf (stderr, "[OK]\n"); + } + else { + fprintf (stderr, "[FAIL]\n"); + } break; case ACT_STOP: result = fm_stop_environment (cur_env); + if (result == TRUE) { + fprintf (stderr, "[OK]\n"); + } + else { + fprintf (stderr, "[FAIL]\n"); + } break; case ACT_RESTART: - 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 (fm_stop_environment (cur_env) == TRUE) { + result = fm_start_environment (cur_env, e_user, e_grp); + if (result == TRUE) { + fprintf (stderr, "[OK]\n"); + } + else { + fprintf (stderr, "[FAIL]\n"); + } + } + else { + fprintf (stderr, "[FAIL]\n"); + } - result = fm_start_environment (cur_env, e_user, e_grp); break; case ACT_STATUS: