--- freem/src/fmadm.c	2025/04/02 03:02:42	1.24
+++ freem/src/fmadm.c	2025/04/02 15:36:25	1.27
@@ -1,5 +1,5 @@
 /*
- *   $Id: fmadm.c,v 1.24 2025/04/02 03:02:42 snw Exp $
+ *   $Id: fmadm.c,v 1.27 2025/04/02 15:36:25 snw Exp $
  *    FreeM Administration Tool
  *
  *  
@@ -24,6 +24,15 @@
  *   along with FreeM.  If not, see <https://www.gnu.org/licenses/>.
  *
  *   $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
+ *
+ *   Revision 1.25  2025/04/02 04:50:49  snw
+ *   Allow vendor routines to be upgraded
+ *
  *   Revision 1.24  2025/04/02 03:02:42  snw
  *   Stop requiring users to pass -e to fmadm when -u or -g are passed
  *
@@ -171,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)
@@ -318,10 +328,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) {
@@ -336,22 +346,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;
         }        
 
     }
@@ -374,7 +384,8 @@ int main (int argc, char **argv)
     }
     
     set_namespace (fma_namespace, FALSE);
-    
+
+process_args:    
     /* allocate opts array */
     
     /* first dimension */
@@ -397,7 +408,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);
     }
     
@@ -417,6 +429,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;
@@ -1141,6 +1154,110 @@ 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 (fm_environment_running (env) == TRUE) {
+        return TRUE;
+    }
+    
+#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, env, e_user, e_grp);
+#else
+    sprintf (cmd, 4095, "%s -d -k -e %s -u %s -g %s", basecmd, env, e_user, e_grp);
+#endif
+
+    system (cmd);
+
+    sleep (1);
+    
+    return (fm_environment_running (env));
+}
+
+int fm_stop_environment (char *env)
+{
+    long epid;
+
+    epid = fm_get_pid (env);
+    if (epid > -1) {
+        kill (epid, SIGINT);
+        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 {
+        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;
@@ -1149,21 +1266,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:
@@ -1185,7 +1295,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 */
@@ -1210,6 +1320,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) {
 
@@ -1222,51 +1338,55 @@ 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:
-#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);
+                    if (result == TRUE) {
+                        fprintf (stderr, "[OK]\n");
+                    }
+                    else {
+                        fprintf (stderr, "[FAIL]\n");
+                    }
                     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);
+                    result = fm_stop_environment (cur_env);
+                    if (result == TRUE) {
+                        fprintf (stderr, "[OK]\n");
                     }
                     else {
-                        fprintf (stderr, "fmadm:  could not obtain environment daemon pid\n");
-                    }
-                    
+                        fprintf (stderr, "[FAIL]\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);
+                    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, "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);
-                    
+                        fprintf (stderr, "[FAIL]\n");
+                    }                       
 
                     break;
+                    
                 case ACT_STATUS:
                     epid = fm_get_pid (cur_env);
                     if (epid > -1) {
@@ -1520,7 +1640,7 @@ void fm_configure (void)
     mkdir (buf, 0775);
     set_permissions (buf, username, groupname, 0775);
     
-    fprintf (stderr, "fmadm:  copying routines from '%s' to '%s'...\n", src_dir, dest_dir);
+    fprintf (stderr, "fmadm:  copying vendor routines from '%s' to '%s'...\n", src_dir, dest_dir);
 
     if ((dir = opendir (src_dir)) == NULL) {
         fprintf (stderr, "\nfmadm:  could not open source directory %s\n", src_dir);
@@ -1531,13 +1651,19 @@ void fm_configure (void)
         char infile[4096];
         char outfile[4096];
         
-        if ((strcmp (ent->d_name, ".") != 0) && (strcmp (ent->d_name, "..") != 0)) {
-            
-            fprintf (stderr, "\t%s\n", ent->d_name);
+        if ((strcmp (ent->d_name, ".") != 0) && (strcmp (ent->d_name, "..") != 0)) {           
             
             snprintf (infile, 4095, "%s/%s", src_dir, ent->d_name);
             snprintf (outfile, 4095, "%s/%s", dest_dir, ent->d_name);
 
+            if (stat (outfile, &etcstat) == 0) {
+                unlink (outfile);
+                fprintf (stderr, "\tupgrade -> %s\n", ent->d_name);
+            }
+            else {
+                fprintf (stderr, "\tnew     -> %s\n", ent->d_name);
+            }
+            
 #if !defined(__OS2__)            
             if (cp (outfile, infile) != 0) {
                 fprintf (stderr, "fmadm:  failure copying %s to %s\n", infile, outfile);