You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficserver.apache.org by mt...@apache.org on 2010/05/18 14:48:20 UTC

svn commit: r945629 - in /trafficserver/traffic/trunk/proxy/mgmt2/tools: NetConfig.cc SysAPI.cc TimeConfig.cc

Author: mturk
Date: Tue May 18 12:48:19 2010
New Revision: 945629

URL: http://svn.apache.org/viewvc?rev=945629&view=rev
Log:
Remove hard coded paths from mgmt2 tools

Modified:
    trafficserver/traffic/trunk/proxy/mgmt2/tools/NetConfig.cc
    trafficserver/traffic/trunk/proxy/mgmt2/tools/SysAPI.cc
    trafficserver/traffic/trunk/proxy/mgmt2/tools/TimeConfig.cc

Modified: trafficserver/traffic/trunk/proxy/mgmt2/tools/NetConfig.cc
URL: http://svn.apache.org/viewvc/trafficserver/traffic/trunk/proxy/mgmt2/tools/NetConfig.cc?rev=945629&r1=945628&r2=945629&view=diff
==============================================================================
--- trafficserver/traffic/trunk/proxy/mgmt2/tools/NetConfig.cc (original)
+++ trafficserver/traffic/trunk/proxy/mgmt2/tools/NetConfig.cc Tue May 18 12:48:19 2010
@@ -76,13 +76,6 @@ struct ifafilt;
 #include "I_Layout.h"
 #include "I_Version.h"
 
-// TODO: consolidate location of these defaults
-#define DEFAULT_ROOT_DIRECTORY            PREFIX
-#define DEFAULT_LOCAL_STATE_DIRECTORY     "var/trafficserver"
-#define DEFAULT_SYSTEM_CONFIG_DIRECTORY   "etc/trafficserver"
-#define DEFAULT_LOG_DIRECTORY             "var/log/trafficserver"
-#define DEFAULT_TS_DIRECTORY_FILE         PREFIX "/etc/traffic_server"
-
 #define UP_INTERFACE     0
 #define DOWN_INTERFACE   1
 #define HOSTNAME         2
@@ -1378,37 +1371,22 @@ rm_stop_proxy()
 {
 
 
-  FILE *ts_file, *rec_file, *pid_file;
+  FILE *rec_file, *pid_file;
   int i = 0, found_pid_path = 0;
   pid_t pid, old_pid;
   char buffer[1024];
   char proxy_pid_path[1024];
-  char ts_base_dir[1024];
-  char rec_config[1024];
+  char *rec_config;
   char *tmp;
 
-  if ((tmp = getenv("TS_ROOT"))) {
-    ink_strncpy(ts_base_dir, tmp, sizeof(ts_base_dir));
-  } else {
-    if ((ts_file = fopen(DEFAULT_TS_DIRECTORY_FILE, "r")) == NULL) {
-      ink_strncpy(ts_base_dir, PREFIX, sizeof(ts_base_dir));
-    } else {
-      NOWARN_UNUSED_RETURN(fgets(buffer, sizeof(buffer), ts_file));
-      fclose(ts_file);
-      while (!isspace(buffer[i])) {
-        ts_base_dir[i] = buffer[i];
-        i++;
-      }
-      ts_base_dir[i] = '\0';
-    }
-  }
-
-  snprintf(rec_config, sizeof(rec_config), "%s/etc/trafficserver/records.config", ts_base_dir);
-
+  rec_config = Layout::relative_to(Layout::get()->sysconfdir, "records.config");
+  printf("rec_config %s\n", rec_config);
   if ((rec_file = fopen(rec_config, "r")) == NULL) {
     //fprintf(stderr, "Error: unable to open %s.\n", rec_config);
+    xfree(rec_config);
     return -1;
   }
+  xfree(rec_config);
 
   while (fgets(buffer, sizeof(buffer), rec_file) != NULL) {
     if (strstr(buffer, "proxy.config.rni.proxy_pid_path") != NULL) {
@@ -1492,12 +1470,13 @@ int
 main(int argc, char **argv)
 {
 
-  int fun_no;
+  int fun_no = 0;
+
   if (argv) {
     fun_no = atoi(argv[1]);
   }
-
-  fun_no = atoi(argv[1]);
+  // Before accessing file system initialize Layout engine
+  Layout::create();
 
   switch (fun_no) {
   case 0:
@@ -2972,12 +2951,12 @@ int
 main(int argc, char **argv)
 {
 
-  int fun_no;
+  int fun_no = 0;
+
   if (argv) {
     fun_no = atoi(argv[1]);
   }
 
-  fun_no = atoi(argv[1]);
   // Before accessing file system initialize Layout engine
   Layout::create();
 

Modified: trafficserver/traffic/trunk/proxy/mgmt2/tools/SysAPI.cc
URL: http://svn.apache.org/viewvc/trafficserver/traffic/trunk/proxy/mgmt2/tools/SysAPI.cc?rev=945629&r1=945628&r2=945629&view=diff
==============================================================================
--- trafficserver/traffic/trunk/proxy/mgmt2/tools/SysAPI.cc (original)
+++ trafficserver/traffic/trunk/proxy/mgmt2/tools/SysAPI.cc Tue May 18 12:48:19 2010
@@ -23,6 +23,7 @@
 
 
 #include "inktomi++.h"
+#include "I_Layout.h"
 
 #ifdef HAVE_PCRE_PCRE_H
 #include <pcre/pcre.h>
@@ -39,20 +40,13 @@
 #include <grp.h>
 
 #include <ctype.h>
-#include "../api2/include/INKMgmtAPI.h"
+#include "INKMgmtAPI.h"
 
 #include <arpa/inet.h>
 #include <netinet/in.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 
-// TODO: consolidate location of these defaults
-#define DEFAULT_ROOT_DIRECTORY            PREFIX
-#define DEFAULT_LOCAL_STATE_DIRECTORY     "var/trafficserver"
-#define DEFAULT_SYSTEM_CONFIG_DIRECTORY   "etc/trafficserver"
-#define DEFAULT_LOG_DIRECTORY             "var/log/trafficserver"
-#define DEFAULT_TS_DIRECTORY_FILE         PREFIX "/etc/traffic_server"
-
 #define NETCONFIG_HOSTNAME  0
 #define NETCONFIG_GATEWAY   1
 #define NETCONFIG_DOMAIN    2
@@ -80,7 +74,6 @@ int Net_GetNIC_Values(char *interface, c
                       char *gateway);
 int find_value(const char *pathname, const char *key, char *value, size_t value_len, const char *delim, int no);
 static bool recordRegexCheck(const char *pattern, const char *value);
-static int getTSdirectory(char *ts_path, size_t ts_path_len);
 
 int
 Net_GetHostname(char *hostname, size_t hostname_len)
@@ -1007,18 +1000,12 @@ NetConfig_Action(int index, ...)
     close(1);                   // close STDOUT
     close(2);                   // close STDERR
 
-    char ts_path[256];
-    char command_path[512];
-
-    if (getTSdirectory(ts_path, sizeof(ts_path))) {
-      DPRINTF(("[SysAPI] unable to determine install directory\n"));
-      _exit(-1);
-    }
-
-    snprintf(command_path, sizeof(command_path), "%s/bin/net_config", ts_path);
+    char *command_path;
 
+    command_path = Layout::relative_to(Layout::get()->bindir, "net_config");
     res = execv(command_path, (char* const*)argv);
 
+    xfree(command_path);
     if (res != 0) {
       DPRINTF(("[SysAPI] fail to call net_config\n"));
     }
@@ -1124,41 +1111,6 @@ Net_GetEncryptedRootPassword(char **pass
 }
 
 int
-getTSdirectory(char *ts_path, size_t ts_path_len)
-{
-  FILE *fp;
-  char *env_path;
-
-  if ((env_path = getenv("TS_ROOT"))) {
-    ink_strncpy(ts_path, env_path, ts_path_len);
-    return 0;
-  }
-
-  if ((fp = fopen(DEFAULT_TS_DIRECTORY_FILE, "r")) == NULL) {
-    ink_strncpy(ts_path, PREFIX, ts_path_len);
-    return 0;
-  }
-
-  if (fgets(ts_path, ts_path_len, fp) == NULL) {
-    fclose(fp);
-    return -1;
-  }
-  // strip newline if it exists
-  int len = strlen(ts_path);
-  if (ts_path[len - 1] == '\n') {
-    ts_path[len - 1] = '\0';
-  }
-  // strip trailing "/" if it exists
-  len = strlen(ts_path);
-  if (ts_path[len - 1] == '/') {
-    ts_path[len - 1] = '\0';
-  }
-
-  fclose(fp);
-  return 0;
-}
-
-int
 Time_GetTime(char *hour, const size_t hourSize, char *minute, const size_t minuteSize, char *second,
              const size_t secondSize)
 {
@@ -1332,17 +1284,12 @@ TimeConfig_Action(int index, bool restar
     //close(1);  // close STDOUT
     //close(2);  // close STDERR
 
-    char ts_path[256];
-    char command_path[512];
-
-    if (getTSdirectory(ts_path, sizeof(ts_path))) {
-      DPRINTF(("[SysAPI] unable to determine install directory\n"));
-      _exit(-1);
-    }
-    snprintf(command_path, sizeof(command_path), "%s/bin/time_config", ts_path);
+    char *command_path;
 
+    command_path = Layout::relative_to(Layout::get()->bindir, "time_config");
     res = execv(command_path, (char* const*)argv);
 
+    xfree(command_path);
     if (res != 0) {
       DPRINTF(("[SysAPI] fail to call time_config\n"));
     }
@@ -1417,12 +1364,11 @@ int
 setSNMP(char *sys_location, char *sys_contact, char *sys_name, char *authtrapenable, char *trap_community,
         char *trap_host)
 {
-  char snmp_path[1024], ts_snmp_path[1024], snmp_path_new[1024], ts_snmp_path_new[1024], buffer[1024],
-    ts_base_dir[1024], buf[1024];
+  char snmp_path[1024], ts_snmp_path[1024], snmp_path_new[1024], ts_snmp_path_new[1024], buf[1024];
   char *tmp, tmp1[1024];
-  FILE *fp, *ts_file, *fp1, *fp_ts, *fp1_ts, *snmppass_fp, *snmppass_fp1;
+  FILE *fp, *fp1, *fp_ts, *fp1_ts, *snmppass_fp, *snmppass_fp1;
   pid_t pid;
-  int i, status;
+  int status;
   const char *mv_binary = MV_BINARY;
 
   bool sys_location_flag = false;
@@ -1431,7 +1377,6 @@ setSNMP(char *sys_location, char *sys_co
   bool authtrapenable_flag = false;
   bool trap_community_flag = false;
   bool trap_host_flag = false;
-  char *env_path;
 
   DPRINTF(("setSNMP(): sys_location: %s, sys_contact: %s, sys_name: %s, authtrapenable: %s, trap_community: %s, trap_host: %s\n", sys_location, sys_contact, sys_name, authtrapenable, trap_community, trap_host));
 
@@ -1442,22 +1387,8 @@ setSNMP(char *sys_location, char *sys_co
     return 1;
   }
 
-  if ((env_path = getenv("TS_ROOT"))) {
-    ink_strncpy(ts_base_dir, env_path, sizeof(ts_base_dir));
-  } else {
-    if ((ts_file = fopen(DEFAULT_TS_DIRECTORY_FILE, "r")) == NULL) {
-      ink_strncpy(ts_base_dir, "/usr/local", sizeof(ts_base_dir));
-    } else {
-      NOWARN_UNUSED_RETURN(fgets(buffer, sizeof(buf), ts_file));
-      fclose(ts_file);
-
-      for (i = 0; !isspace(buffer[i]); i++)
-        ts_base_dir[i] = buffer[i];
-      ts_base_dir[i] = '\0';
-    }
-  }
-
-  snprintf(ts_snmp_path, sizeof(ts_snmp_path), "%s/etc/trafficserver/%s", ts_base_dir, TS_SNMP_PATH);
+  Layout::relative_to(ts_snmp_path, sizeof(ts_snmp_path),
+                      Layout::get()->sysconfdir, TS_SNMP_PATH);
   if ((fp_ts = fopen(ts_snmp_path, "r")) == NULL && (fp_ts = fopen(ts_snmp_path, "a+")) == NULL) {
     DPRINTF(("[SysAPI] failed to open ts snmp configuration file\n"));
     fclose(fp);
@@ -1468,7 +1399,7 @@ setSNMP(char *sys_location, char *sys_co
     DPRINTF(("[SysAPI] failed to open new snmp configuration file\n"));
     return 1;
   }
-  snprintf(ts_snmp_path_new, sizeof(ts_snmp_path_new), "%s/%s.new", ts_base_dir, TS_SNMP_PATH);
+  snprintf(ts_snmp_path_new, sizeof(ts_snmp_path_new), "%s.new", ts_snmp_path);
   if ((fp1_ts = fopen(ts_snmp_path_new, "w")) == NULL) {
     DPRINTF(("[SysAPI] failed to open new ts snmp configuration file\n"));
     return 1;
@@ -1637,8 +1568,8 @@ setSNMP(char *sys_location, char *sys_co
     snprintf(community_string, sizeof(community_string), "%s %s %s\n", community1, trap_community, community2);
 
     //now open the file for modification.
-
-    snprintf(snmp_pass, sizeof(snmp_pass), "%s/bin/snmppass.sh", ts_base_dir);
+    Layout::relative_to(snmp_pass, sizeof(snmp_pass),
+                        Layout::get()->bindir, "snmppass.sh");
     if ((snmppass_fp = fopen(snmp_pass, "r")) == NULL) {
       if ((fp = fopen(snmp_pass, "a+")) == NULL) {
         DPRINTF(("[SysAPI] failed to open ts snmp script file\n"));
@@ -1646,7 +1577,7 @@ setSNMP(char *sys_location, char *sys_co
       }
       fclose(fp);
     }
-    snprintf(snmp_pass_new, sizeof(snmp_pass_new), "%s/bin/snmppass.sh.new", ts_base_dir);
+    snprintf(snmp_pass_new, sizeof(snmp_pass_new), "%s.new", snmp_pass);
     if ((snmppass_fp1 = fopen(snmp_pass_new, "w")) == NULL) {
       DPRINTF(("[SysAPI] failed to open new snmp script file\n"));
       return 1;
@@ -1714,14 +1645,11 @@ Net_SNMPSetUp(char *sys_location, char *
   } else {
     int res;
 
-    char ts_path[256];
     char command_path[512];
 
-    if (getTSdirectory(ts_path, sizeof(ts_path))) {
-      DPRINTF(("[SysAPI] unable to determine install directory\n"));
-      _exit(-1);
-    }
-    snprintf(command_path, sizeof(command_path), "%s/bin/stop_snmp && %s/bin/start_snmp", ts_path, ts_path);
+    snprintf(command_path, sizeof(command_path),
+             "%s/stop_snmp && %s/start_snmp",
+             Layout::get()->bindir, Layout::get()->bindir);
 
     sleep(30);
     char *argv[] = { NULL };
@@ -1786,20 +1714,17 @@ Net_SNMPGetInfo(char *sys_location, size
                 size_t sys_name_len, char *authtrapenable, size_t authtrapenable_len, char *trap_community,
                 size_t trap_community_len, char *trap_host, size_t trap_host_len)
 {
-  char ts_path[256], snmp_config[256], buf[1024];
+  char *snmp_config, buf[1024];
   FILE *fp;
   int status;
 
-  if (getTSdirectory(ts_path, sizeof(ts_path))) {
-    DPRINTF(("[SysAPI]:SNMPGetInfo: unable to determine TS install directory\n"));
-    return -1;
-  }
-
-  snprintf(snmp_config, sizeof(snmp_config), "%s/etc/trafficserver/snmpd.cnf", ts_path);
+  snmp_config = Layout::relative_to(Layout::get()->sysconfdir, "snmpd.conf");
   if ((fp = fopen(snmp_config, "r")) == NULL && (fp = fopen(snmp_config, "a+")) == NULL) {
-    DPRINTF(("[SysAPI] failed to open ts snmp script file\n"));
+    DPRINTF(("[SysAPI] failed to open ts snmp script file %s\n", snmp_config));
+    xfree(snmp_config);
     return 1;
   }
+  xfree(snmp_config);
 
   NOWARN_UNUSED_RETURN(fgets(buf, 1024, fp));
   while (!feof(fp)) {
@@ -1925,7 +1850,6 @@ int Net_GetNIC_Values(char *interface, c
                       char *gateway);
 int find_value(const char *pathname, const char *key, char *value, const char *delim, int no);
 static bool recordRegexCheck(const char *pattern, const char *value);
-static int getTSdirectory(char *ts_path, size_t ts_path_len);
 
 int
 Net_GetHostname(char *hostname, size_t hostname_len)
@@ -2872,16 +2796,10 @@ NetConfig_Action(int index, ...)
   va_list ap;
   va_start(ap, index);
 
-  char ts_path[256];
-  char command_path[512];
-
+  char *command_path;
 
-  if (getTSdirectory(ts_path, sizeof(ts_path))) {
-    DPRINTF(("[SysAPI] unable to determine install directory\n"));
-    _exit(-1);
-  }
+  command_path = Layout::relative_to(Layout::get()->bindir, "net_config");
 
-  snprintf(command_path, sizeof(command_path), "%s/bin/net_config", ts_path);
 
   argv[0] = command_path;
 
@@ -2951,7 +2869,7 @@ NetConfig_Action(int index, ...)
     }
     _exit(res);
   }
-
+  xfree(command_path);
   return 0;
 }
 
@@ -3052,40 +2970,6 @@ Net_GetEncryptedRootPassword(char **pass
 }
 
 int
-getTSdirectory(char *ts_path, size_t ts_path_len)
-{
-  FILE *fp;
-  char *env_path;
-
-  if ((env_path = getenv("TS_ROOT"))) {
-    ink_strncpy(ts_path, env_path, ts_path_len);
-    return 0;
-  }
-  if ((fp = fopen(DEFAULT_TS_DIRECTORY_FILE, "r")) == NULL) {
-    ink_strncpy(ts_path, "/usr/local", ts_path_len);
-    return 0;
-  }
-
-  if (fgets(ts_path, ts_path_len, fp) == NULL) {
-    fclose(fp);
-    return -1;
-  }
-  // strip newline if it exists
-  int len = strlen(ts_path);
-  if (ts_path[len - 1] == '\n') {
-    ts_path[len - 1] = '\0';
-  }
-  // strip trailing "/" if it exists
-  len = strlen(ts_path);
-  if (ts_path[len - 1] == '/') {
-    ts_path[len - 1] = '\0';
-  }
-
-  fclose(fp);
-  return 0;
-}
-
-int
 Time_GetTime(char *hour, char *minute, char *second)
 {
   int status;
@@ -3249,17 +3133,11 @@ TimeConfig_Action(int index, bool restar
     //close(1);  // close STDOUT
     //close(2);  // close STDERR
 
-    char ts_path[256];
-    char command_path[512];
-
-    if (getTSdirectory(ts_path, sizeof(ts_path))) {
-      DPRINTF(("[SysAPI] unable to determine install directory\n"));
-      _exit(-1);
-    }
-    snprintf(command_path, sizeof(command_path), "%s/bin/time_config", ts_path);
+    char *command_path;
 
+    command_path = Layout::relative_to(Layout::get()->bindir, "time_config");
     res = execv(command_path, (char* const*) argv);
-
+    xfree(command_path);
     if (res != 0) {
       DPRINTF(("[SysAPI] fail to call time_config"));
     }

Modified: trafficserver/traffic/trunk/proxy/mgmt2/tools/TimeConfig.cc
URL: http://svn.apache.org/viewvc/trafficserver/traffic/trunk/proxy/mgmt2/tools/TimeConfig.cc?rev=945629&r1=945628&r2=945629&view=diff
==============================================================================
--- trafficserver/traffic/trunk/proxy/mgmt2/tools/TimeConfig.cc (original)
+++ trafficserver/traffic/trunk/proxy/mgmt2/tools/TimeConfig.cc Tue May 18 12:48:19 2010
@@ -38,13 +38,6 @@
 #include <signal.h>
 #include <time.h>
 
-// TODO: consolidate location of these defaults
-#define DEFAULT_ROOT_DIRECTORY            PREFIX
-#define DEFAULT_LOCAL_STATE_DIRECTORY     "var/trafficserver"
-#define DEFAULT_SYSTEM_CONFIG_DIRECTORY   "etc/trafficserver"
-#define DEFAULT_LOG_DIRECTORY             "var/log/trafficserver"
-#define DEFAULT_TS_DIRECTORY_FILE         PREFIX "/etc/traffic_server"
-
 #define CHANGE_ALL 	0
 #define CHANGE_TIME 	1
 #define CHANGE_DATE 	2
@@ -59,8 +52,10 @@ main(int argc, char *argv[])
   struct tm *mPtr;
   struct timeval v;
   FILE *fp, *tmp;
+  // TODO: Use defines instead hard coding 1024
+  //
   char zonepath[1024], no_cop_path[1024], buffer[1024], command[1024];
-  char stop_traffic_server[1024], start_traffic_server[1024];
+  char stop_traffic_server[PATH_MAX_LEN + 1], start_traffic_server[1024];
   char *hour = 0, *minute = 0, *second = 0, *month = 0, *day = 0, *year = 0, *timezone = 0, *ntpservers = 0;
   int reset_all = 0, reset_time = 0, reset_date = 0, reset_timezone = 0, reset_ntp = 0;
   int restart;
@@ -81,28 +76,13 @@ main(int argc, char *argv[])
   // Before accessing file system initialize Layout engine
   Layout::create();
 
-  if ((env_path = getenv("TS_ROOT"))) {
-    strncpy(buffer, env_path, sizeof(buffer) - 1);
-  } else {
-    if ((fp = fopen(DEFAULT_TS_DIRECTORY_FILE, "r")) != NULL) {
-      NOWARN_UNUSED_RETURN(fgets(buffer, 1024, fp));
-      if (buffer[strlen(buffer) - 1] == '\n') {
-        buffer[strlen(buffer) - 1] = '\0';
-      }
-      fclose(fp);
-    } else {
-      strncpy(buffer, PREFIX, sizeof(buffer) - 1);
-    }
-  }
-
-  strncpy(stop_traffic_server, buffer, sizeof(stop_traffic_server) - 1);
-  strncat(stop_traffic_server, "/bin/stop_traffic_server",
-          sizeof(stop_traffic_server) - 1 - strlen(stop_traffic_server));
-  strncpy(start_traffic_server, buffer, sizeof(start_traffic_server) - 1);
-  strncat(start_traffic_server, "/bin/start_traffic_server",
-          sizeof(start_traffic_server) - 1 - strlen(start_traffic_server));
-  strncpy(no_cop_path, buffer, sizeof(no_cop_path) - 1);
-  strncat(no_cop_path, "/etc/trafficserver/internal/no_cop", sizeof(no_cop_path) - 1 - strlen(no_cop_path));
+  Layout::relative_to(stop_traffic_server, sizeof(stop_traffic_server) - 1,
+                      Layout::get()->bindir, "stop_traffic_server");
+  Layout::relative_to(start_traffic_server, sizeof(start_traffic_server) - 1,
+                      Layout::get()->bindir, "start_traffic_server");
+  Layout::relative_to(no_cop_path, sizeof(no_cop_path) - 1,
+                      Layout::get()->bindir, "internal/no_cop");
+  // XXX: Why strncpy here?
   strncpy(zonepath, "/usr/share/zoneinfo/", sizeof(zonepath) - 1);
 
 //  while(access(no_cop_path, F_OK) == -1);