add lcov and lcov-clean target
[m6w6/libmemcached] / clients / ms_setting.c
index 51f89714ac7c407f1039698444c6e2bbf3d23e70..d43356c7a802995e53d09ea69d8a8f2ced924f2c 100644 (file)
 #include "config.h"
 
 #include <ctype.h>
+#include <inttypes.h>
+#include <limits.h>
+#include <pwd.h>
+#include <strings.h>
+#include <sys/types.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+
+
 #include "ms_setting.h"
 #include "ms_conn.h"
 
@@ -20,6 +30,8 @@
 #define RAND_CHAR_SIZE             (10 * 1024 * 1024)      /* 10M character table */
 #define RESERVED_RAND_CHAR_SIZE    (2 * 1024 * 1024)       /* reserved 2M to avoid pointer sloping over */
 
+#define DEFAULT_CONFIG_NAME ".memslap.cnf"
+
 #define DEFAULT_THREADS_NUM        1                       /* default start one thread */
 #define DEFAULT_CONNS_NUM          16                      /* default each thread with 16 connections */
 #define DEFAULT_EXE_NUM            0                       /* default execute number is 0 */
@@ -57,6 +69,82 @@ static void ms_print_setting(void);
 static void ms_setting_slapmode_init_pre(void);
 static void ms_setting_slapmode_init_post(void);
 
+#if !defined(HAVE_GETLINE)
+#include <limits.h>
+static ssize_t getline (char **line, size_t *line_size, FILE *fp)
+{
+  char delim= '\n';
+  ssize_t result= 0;
+  size_t cur_len= 0;
+
+  if (line == NULL || line_size == NULL || fp == NULL)
+  {
+    errno = EINVAL;
+    return -1;
+  }
+
+  if (*line == NULL || *line_size == 0)
+  {
+    char *new_line;
+    *line_size = 120;
+    new_line= (char *) realloc (*line, *line_size);
+    if (new_line == NULL)
+    {
+      result= -1;
+      return result;
+    }
+    *line= new_line;
+  }
+
+  for (;;)
+  {
+    int i= getc(fp);
+    if (i == EOF)
+    {
+      result = -1;
+      break;
+    }
+
+    /* Make enough space for len+1 (for final NUL) bytes.  */
+    if (cur_len + 1 >= *line_size)
+    {
+      size_t needed_max=
+        SSIZE_MAX < SIZE_MAX ? (size_t) SSIZE_MAX + 1 : SIZE_MAX;
+      size_t needed= (2 * (*line_size)) + 1;
+      char *new_line;
+
+      if (needed_max < needed)
+        needed= needed_max;
+      if (cur_len + 1 >= needed)
+      {
+        result= -1;
+        errno= EOVERFLOW;
+        return result;
+      }
+
+      new_line= (char *)realloc(*line, needed);
+      if (new_line == NULL)
+      {
+        result= -1;
+        return result;
+      }
+
+      *line= new_line;
+      *line_size= needed;
+    }
+
+    (*line)[cur_len]= (char)i;
+    cur_len++;
+
+    if (i == delim)
+      break;
+  }
+  (*line)[cur_len] = '\0';
+  if (cur_len != 0)
+    return (ssize_t)cur_len;
+  return result;
+}
+#endif
 
 /**
  * parse the server list string, and build the servers
@@ -119,7 +207,7 @@ static void ms_get_serverlist(char *str)
     {
       srvs= (ms_mcd_server_t *)realloc(
         ms_setting.servers,
-        (uint64_t)ms_setting.total_srv_cnt
+        (size_t)ms_setting.total_srv_cnt
         * sizeof(ms_mcd_server_t) * 2);
       if (srvs == NULL)
       {
@@ -248,12 +336,30 @@ static int ms_read_is_data(char *line, ssize_t nread)
  */
 static void ms_no_config_file()
 {
-  FILE *fd= fopen("config", "w+");
+  char userpath[PATH_MAX];
+  struct passwd *usr= NULL;
+  FILE *fd;
+
+  usr= getpwuid(getuid());
+
+  snprintf(userpath, PATH_MAX, "%s/%s", usr->pw_dir, DEFAULT_CONFIG_NAME);
+
+  if (access (userpath, F_OK | R_OK) == 0)
+    goto exit;
+
+  fd= fopen(userpath, "w+");
 
+  if (fd == NULL)
+  {
+    fprintf(stderr, "Could not create default configure file %s\n", userpath);
+    perror(strerror(errno));
+    exit(1);
+  }
   fprintf(fd, "%s", DEFAULT_CONGIF_STR);
   fclose(fd);
 
-  ms_setting.cfg_file= strdup("config");
+exit:
+  ms_setting.cfg_file= strdup(userpath);
 } /* ms_no_config_file */
 
 
@@ -276,13 +382,11 @@ static void ms_parse_cfg_file(char *cfg_file)
   int end_of_file= 0;
   ms_key_distr_t *key_distr= NULL;
   ms_value_distr_t *val_distr= NULL;
-  bool no_cfg= false;
 
   if (cfg_file == NULL)
   {
     ms_no_config_file();
     cfg_file= ms_setting.cfg_file;
-    no_cfg= true;
   }
 
   /*read key value configure file*/
@@ -297,6 +401,7 @@ static void ms_parse_cfg_file(char *cfg_file)
     if ((((nread= getline(&line, &read_len, f)) == 1)
          || ! ms_read_is_data(line, nread)) && (nread != EOF)) /* bypass blank line */
       continue;
+
     if (nread == EOF)
     {
       fprintf(stderr, "Bad configuration file, no configuration find.\n");
@@ -319,7 +424,7 @@ static void ms_parse_cfg_file(char *cfg_file)
 
         if (nread != EOF)
         {
-          if (sscanf(line, "%lu %lu %lf ", &start_len,
+          if (sscanf(line, "%zu %zu %lf ", &start_len,
                      &end_len, &proportion) != 3)
           {
             conf_type= ms_get_conf_type(line);
@@ -334,7 +439,7 @@ static void ms_parse_cfg_file(char *cfg_file)
           {
             key_distr= (ms_key_distr_t *)realloc(
               ms_setting.key_distr,
-              (uint64_t)ms_setting.
+              (size_t)ms_setting.
                  total_key_rng_cnt * sizeof(ms_key_distr_t) * 2);
             if (key_distr == NULL)
             {
@@ -361,7 +466,7 @@ static void ms_parse_cfg_file(char *cfg_file)
 
         if (nread != EOF)
         {
-          if (sscanf(line, "%lu %lu %lf %lu", &start_len, &end_len,
+          if (sscanf(line, "%zu %zu %lf %zu", &start_len, &end_len,
                      &proportion, &frequence) != 3)
           {
             conf_type= ms_get_conf_type(line);
@@ -378,7 +483,7 @@ static void ms_parse_cfg_file(char *cfg_file)
           {
             val_distr= (ms_value_distr_t *)realloc(
               ms_setting.value_distr,
-              (uint64_t)ms_setting.
+              (size_t)ms_setting.
                  total_val_rng_cnt * sizeof(ms_value_distr_t) * 2);
             if (val_distr == NULL)
             {
@@ -449,13 +554,10 @@ static void ms_parse_cfg_file(char *cfg_file)
 
   fclose(f);
 
-  if (no_cfg)
-  {
-    remove(ms_setting.cfg_file);
-  }
-
   if (line != NULL)
+  {
     free(line);
+  }
 } /* ms_parse_cfg_file */
 
 
@@ -778,9 +880,9 @@ static void ms_print_setting()
   }
   else
   {
-    fprintf(stdout, "execute number: %ld\n", ms_setting.exec_num);
+    fprintf(stdout, "execute number: %" PRId64 "\n", ms_setting.exec_num);
   }
-  fprintf(stdout, "windows size: %ldk\n",
+  fprintf(stdout, "windows size: %" PRId64 "k\n",
           (int64_t)(ms_setting.win_size / 1024));
   fprintf(stdout, "set proportion: set_prop=%.2f\n",
           ms_setting.cmd_distr[CMD_SET].cmd_prop);
@@ -830,7 +932,7 @@ void ms_setting_init_pre()
   ms_setting.run_time= DEFAULT_RUN_TIME;
   ms_setting.total_srv_cnt= MCD_SRVS_NUM_INIT;
   ms_setting.servers= (ms_mcd_server_t *)malloc(
-    (uint64_t)ms_setting.total_srv_cnt
+    (size_t)ms_setting.total_srv_cnt
     * sizeof(ms_mcd_server_t));
   if (ms_setting.servers == NULL)
   {
@@ -848,9 +950,9 @@ void ms_setting_init_pre()
 static void ms_setting_slapmode_init_post()
 {
   ms_setting.total_key_rng_cnt= KEY_RANGE_COUNT_INIT;
-  ms_setting.key_distr= (ms_key_distr_t *)malloc(
-    (uint64_t)ms_setting.total_key_rng_cnt
-    * sizeof(ms_key_distr_t));
+  ms_setting.key_distr= 
+    (ms_key_distr_t *)malloc((size_t)ms_setting.total_key_rng_cnt * sizeof(ms_key_distr_t));
+
   if (ms_setting.key_distr == NULL)
   {
     fprintf(stderr, "Can't allocate key distribution structure.\n");
@@ -858,10 +960,10 @@ static void ms_setting_slapmode_init_post()
   }
 
   ms_setting.total_val_rng_cnt= VALUE_RANGE_COUNT_INIT;
-  ms_setting.value_distr= (ms_value_distr_t *)malloc(
-    (uint64_t)ms_setting.total_val_rng_cnt
-    * sizeof(
-      ms_value_distr_t));
+
+  ms_setting.value_distr= 
+    (ms_value_distr_t *)malloc((size_t)ms_setting.total_val_rng_cnt * sizeof( ms_value_distr_t));
+
   if (ms_setting.value_distr == NULL)
   {
     fprintf(stderr, "Can't allocate value distribution structure.\n");