--- freem/src/fmadm.c 2025/04/01 20:11:46 1.21 +++ freem/src/fmadm.c 2025/04/01 23:21:45 1.22 @@ -1,5 +1,5 @@ /* - * $Id: fmadm.c,v 1.21 2025/04/01 20:11:46 snw Exp $ + * $Id: fmadm.c,v 1.22 2025/04/01 23:21:45 snw Exp $ * FreeM Administration Tool * * @@ -24,6 +24,9 @@ * along with FreeM. If not, see . * * $Log: fmadm.c,v $ + * Revision 1.22 2025/04/01 23:21:45 snw + * fmadm commands for stopping, starting, and restarting environments now functional + * * Revision 1.21 2025/04/01 20:11:46 snw * Further work on fmadm * @@ -1104,6 +1107,31 @@ int fm_edit (short object, int optc, cha } /* fm_edit() */ +long fm_get_pid (char *env) +{ + char pid_file[4096]; + char tmp_pid[255]; + long res; + + FILE *fp; + + snprintf (pid_file, 4095, "%s/freem/run/%s.pid", LOCALSTATEDIR, env); + + if ((fp = fopen (pid_file, "r")) != NULL) { + if (fgets (tmp_pid, 255, fp)) { + fclose (fp); + return atol (tmp_pid); + } + else { + fclose (fp); + return -1; + } + } + else { + return -1; + } +} + int fm_daemonctl (short action, short object, int optc, char **options) { FILE *ef; @@ -1120,6 +1148,7 @@ int fm_daemonctl (short action, short ob char basecmd[255]; char *savptr; int result; + long epid; #if !defined(__OS2__) snprintf (basecmd, 254, "%s/bin/freem", PREFIX); @@ -1177,7 +1206,7 @@ int fm_daemonctl (short action, short ob if (result == FALSE) { strcpy (e_grp, "freem"); } - + printf ("fmadm: %s environment %s\n", verb, cur_env); switch (action) { @@ -1186,15 +1215,44 @@ int fm_daemonctl (short action, short ob 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 +#endif + system (cmd); break; case ACT_STOP: - case ACT_RESTART: - printf ("fmadm: stop and restart not yet implemented\n"); + 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"); + } + 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"); + } + 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); + - system (cmd); + break; + } } else { printf ("fmadm: %s environment is disabled; skipping\n", cur_env);