Ejemplo n.º 1
0
static void
usage(const char *progname)
{
    returnerr("Usage: %s [-d]  [-w webserver]  [-p port_num]\n",
  progname);
    returnerr("\t[-c masterhost:port]  [-t run_time | -l loops]\n");
    returnerr("\t[-n numclients]  [-R]\n");
    returnerr("\t[-f config_file]  [-u uilfile | url ...]\n");
    errexit("\n");
} /* END usage() */
Ejemplo n.º 2
0
int resolve_addrs(char *host, char *protocol, struct hostent *host_phe, struct protoent *proto_ppe, unsigned long *addr,
  short *type) {
struct hostent *phe;
struct protoent *ppe;

    /* if IP address given, convert to internal form */
    if (host[0] >= '0' && host[0] <= '9') {
  *addr = inet_addr(host);
  if (*addr == INADDR_NONE)
      return(returnerr("Invalid IP address %s\n", host));

    } else {
  /* look up by name */
  phe = gethostbyname(host);
  if (phe == 0)
  {
      D_PRINTF( "Gethostbyname failed: %s", neterrstr() );
      return(returnerr("Can't get %s host entry\n", host));
  }
  memcpy(host_phe, phe, sizeof(struct hostent));
  memcpy((char *)addr, phe->h_addr, sizeof(*addr));
    }

    /* Map protocol name to protocol number */
    ppe = getprotobyname(protocol);

    if (ppe == 0)
    {
  D_PRINTF( "protobyname returned %d\n",  ppe );
  return(returnerr("Can't get %s protocol entry\n",protocol));
    }
    memcpy(proto_ppe, ppe, sizeof(struct protoent));

    D_PRINTF( "Protocol number %d\n", ppe->p_proto );

    /* Use protocol to choose a socket type */
    if (strcmp(protocol,"udp") == 0)
    {
  *type = SOCK_DGRAM;
    }
    else
    {
  *type = SOCK_STREAM;
  D_PRINTF( "Choosing SOCK_STREAM %d type %d %s\n",
      SOCK_STREAM, *type, neterrstr() );
    }

    return 0;
}
Ejemplo n.º 3
0
/*
 * count_file_list()
 * given a filename, return a guess at the number of pages
 */
int
count_file_list(const char *url_list_file)
{
  FILE *fp;
  long int num_of_pages;
  char a_line[BUFSIZ];
  char *textvalue;
  int returnval;

  fp = fopen(url_list_file, "r");

  if (fp == 0)
    {
      D_PRINTF("Error %d opening filelist %s: %s\n",
               errno, url_list_file, strerror(errno) );;
      return(returnerr("Error %d opening filelist %s: %s\n",
             errno, url_list_file, strerror(errno)));
    }
  else
    {
      num_of_pages = 0;
      D_PRINTF( "Opened file, about to count\n" );
      /*
       * parse through the file line-by-line
       * strip out comments, but don't check for URL consistency
       */
      while (fgets(a_line, BUFSIZ, fp) != 0)
        {
          textvalue = a_line;
          /* check for comments */
          if (strchr(textvalue, '#') != 0)
            {
              /* throw out anything after any '#' */
              D_PRINTF( "Stripping comment from line: %s\n", textvalue );
              returnval = strcspn(textvalue, "#");
              D_PRINTF( "Found first # at %d\n", returnval );
              if (returnval == 0)
                {
                  textvalue = 0;
                }
            }
          /* is there more? */
          if (textvalue != 0)
            {
              num_of_pages++;
              D_PRINTF( "Found %ld pages\n", num_of_pages );
            }
        }
      return(num_of_pages);
    }
  fclose(fp);
} /* end count_file_list() */
Ejemplo n.º 4
0
/* Copy FROM file to new (must not exist) TO file.
   Returns -1 on error, or bytes copied.
*/
int
copy_file(const char *from_path, const char *to_path)
{
    int from_file, to_file;
    char buff[32*1024];
    int cnt, ret, total=0;

    from_file = open(from_path, O_RDONLY, 0);
    if (from_file < 0) {
        return returnerr(stderr, "Error opening '%s' for reading: %s\n",
                         from_path, strerror(errno));
    }

    to_file = open(to_path, O_WRONLY | O_CREAT | O_EXCL, 0600);
    if (to_file < 0) {
        close(from_file);
        return returnerr(stderr, "Error opening '%s' for writing: %s\n",
                         to_path, strerror(errno));
    }

    while ((cnt = read(from_file, buff, sizeof(buff))) > 0) {
        ret = write(to_file, buff, cnt);
        total += ret;
        if (ret != cnt) {
            returnerr(stderr, "Error in copy of '%s' to '%s'\n",
                      from_path, to_path, strerror(errno));
            total = -1;
            break;
        }
    }

    close(from_file);
    close(to_file);
    //TODO: copy permissions
    return total;
}
Ejemplo n.º 5
0
Archivo: ssl.c Proyecto: thomoco/mstone
/* Do any extra initialization related to ssl
 */
void
mstoneSslInit(void)
{
#ifdef USE_PTHREADS
  int n;
  num_locks = CRYPTO_num_locks();
  locks = xcalloc(num_locks * sizeof(*locks));
  for (n = 0; n < num_locks; n++) {
    if (pthread_mutex_init(&locks[n], NULL)) {
      returnerr(stderr, "Failed to initialize mutex %d\n", n);
    }
  }
  CRYPTO_set_id_callback(thread_id);
  CRYPTO_set_locking_callback(thread_lock);
  D_PRINTF(stderr, "Initialized SSL for threading\n");

#endif /* USE_PTHREADS */
}
Ejemplo n.º 6
0
void ClientThread(void *dummy)
{

  THREAD FILE *logfile;

  THREAD stats_t  timestat;

  THREAD rqst_timer_t timerarray[MAXNUMOFFILES];
  THREAD SOCKET mastersock = BADSOCKET_VALUE; /* connection to webmaster */


  THREAD page_stats_t *page_stats;    /* actually a dynamic array */

    int   loopcnt = 0;
    int   filecnt;
    int   loop;
    int   ran_number;
    int   page_index;
    int   page_number;
    int   file_count = 0;
    char  file_name[50];
    struct timeval  runningtime;
    time_t    junk;
    int   i;
    int   returnval;

    /*
     * INITIALIZE DATA
     */

    page_stats =
      (page_stats_t *)mymalloc((number_of_pages)*sizeof(page_stats_t));

    for (i=0; i < number_of_pages; i++) {
        page_stats_init(&(page_stats[i]));
    }

    if (debug)
      {
  /*
   *  OPEN A DEBUG FILE
   */
  fflush(stderr);
  sprintf(file_name, "%s.%d", DEBUG_FILE, (int)getpid());
  debugfile = fopen(file_name, "w+");
  if (debugfile == 0)
      errexit("Can't open debug file\n");
  D_PRINTF( "Running in debug mode, %d\n",amclient );
    }

    if (record_all_transactions)
    {
  /*
   *  OPEN A LOG FILE.
   */
  sprintf(file_name, "%s%d", LOG_FILE, (int)getpid());
  returnerr("Log file is %s\n", file_name);
  logfile = fopen(file_name, "w+");
    }

    /* Initialize random number generator */
    junk = getpid ();
    rand_r(&junk);
    D_PRINTF( "Random seed: %d\n", junk );

    for (i=0; i < MAXNUMOFFILES; i++)
    {
  rqtimer_init(&(timerarray[i]));
    }
    stats_init(&timestat);

    D_PRINTF( "Number of files %d\n", numfiles );

    timestat.total_num_of_files = numfiles;

    if (amclient)
    {
        /*
         * WE ARE A CLIENT PROCESS. (i.e. WE ARE NOT RUN BY A USER, BUT BY
         * THE MASTER WWWSTONE PROCESS. WE NEED TO CONNECT TO THE
         * MASTER WHO WILL SYNCHRONIZE ALL THE CLIENTS.
         */
  D_PRINTF( "Trying to connect with %s\n",connectstr );

  mastersock = connecttomaster(connectstr);

  D_PRINTF( "connecttomaster returns %d, %s\n",
          mastersock, neterrstr() );

  if(BADSOCKET(mastersock))
        {
            /*
             * ERROR CONNECTING TO THE MASTER. ABORT.
             */
            errexit("Error connecting to the master: %s\n", neterrstr());
        }
    } /* END IF CLIENT */

#ifdef WIN32
    /* Tell parent we're ready */
    InterlockedIncrement(&CounterSemaphore);

    /* Wait for main() thread to release us */
    WaitForSingleObject(hSemaphore, INFINITE);
    ReleaseSemaphore(hSemaphore, 1, 0);
#endif /* WIN32 */
    if (testtime != 0)
    {
       /*
        * IF RUNNING A TIMED TEST, WE WILL LOOP
  * UNTIL THE ALARM GOES OFF.
        * WE'LL ALSO NEED TO SET THE SIGNAL HANDLER
        */
#ifndef WIN32
      /*signal(SIGALRM, alarmhandler);*/
       /*
        * SEND SIGALRM IN testtime SECONDS
        */
      /*alarm(testtime);*/
#endif /* WIN32 */
    }

    /*
     * AND THEY'RE OFF...
     */

    if (testtime)
  numloops = INFINITY;
    GETTIMEOFDAY(&(timestat.starttime), &(timestat.starttimezone));

    /* LOOP UNTIL WE HIT numloops, OR WE RUN OUT OF TIME */
    for(loopcnt = 0;  (loopcnt < numloops) && !timeexpired;  loopcnt++)
    {
  /*
   * THIS IS WHERE LOAD TESTING IS DONE.
   * GET A RANDOM NUMBER, THEN INDEX INTO THE
   * PAGE, AND THEN REQUEST THAT SET OF FILES.
   */
  if (uil_filelist_f) /* HAVE FILELIST */
  {
      D_PRINTF( "Have filelist\n" );
      /* if (testtime != 0) /* RUNNING IN TIMED MODE */
            if (1)
      {
    D_PRINTF( "Running in timed mode\n" );
    /* random number between 0 and totalweight-1 */
                junk = getpid ();
    ran_number = (rand_r(&junk) % total_weight);
    D_PRINTF( "random %ld\n", ran_number );

    /* loop through pages, find correct one
     * while ran_number is positive, decrement it
     * by the load_num of the current page
     * example: ran_number is 5, pages have weights of 10 and 10
     *          first iteration page_index = 0, ran_number = -5
     *          iteration halted, page_index = 0
     */
    page_index = -1;
    while (ran_number >= 0)
      {
        page_index++;
        D_PRINTF( "Current page index %d: %ld - %d\n",
          page_index, ran_number,
          load_file_list[page_index].load_num
          );
        ran_number -= load_file_list[page_index].load_num;
      }

    if (page_index >= number_of_pages) { page_index--; }

    D_PRINTF( "Final page index %d\n", page_index );
    filecnt = makeload(load_file_list[page_index].num_of_files,
               page_index, timerarray, &timestat, mastersock, page_stats);
                testtime = 1;
      }
      else /* NOT RUNNING IN TIMED MODE */
      {
    for (page_number = 0;
         page_number < number_of_pages;
         page_number++)
    {
        filecnt = makeload(load_file_list[page_number].num_of_files,
      page_number, timerarray, &timestat, mastersock, page_stats);

    } /* END for page_number */
      } /* END if/else TIMED MODE */
  }
  else /* NO FILELIST */
  {
      D_PRINTF( "No filelist\n" );
     /*
      * LOOP THROUGH UNTIL numfiles TIMES OR UNTIL TIMER EXPIRES
      * AND ALARM SETS filecnt TO INFINITY.
      */

      /* does this still work?? */
      /* filecnt = makeload(numfiles, -1, timerarray); */
  } /* END if HAVE FILELIST */

  if (filecnt > 0)
      file_count += filecnt;

    } /* END while loopcnt */

    GETTIMEOFDAY(&(timestat.endtime), &(timestat.endtimezone));
    D_PRINTF( "Test run complete\n" );
    signal(SIGALRM, 0);

    if (testtime == 0)
    {
  numfiles = loopcnt;

  if (uil_filelist_f)
  {
      numfiles = file_count;
  }
    }

    /* This option ( "-R" ) looks broken (e.g. l > 50) -- JEF 2/15/96 */
    if (record_all_transactions)
    {
  /*
   * DUMP THE LOG FILE INFORMATION.
   */
  for (loop=0; loop < (loopcnt * file_count); loop++)
  {
      fprintf(logfile, " entertime \t%d.%d\n"
           " beforeconnect \t%d.%d\n"
           " afterconnect \t%d.%d\n"
           " beforeheader \t%d.%d\n"
           " afterheader \t%d.%d\n"
           " afterbody \t%d.%d\n"
           " exittime \t%d.%d\n"
           " total bytes \t%d\n"
           " body bytes\t%d\n",
    timerarray[loop].entertime.tv_sec,
    timerarray[loop].entertime.tv_usec,
    timerarray[loop].beforeconnect.tv_sec,
    timerarray[loop].beforeconnect.tv_usec,
    timerarray[loop].afterconnect.tv_sec,
    timerarray[loop].afterconnect.tv_usec,
    timerarray[loop].beforeheader.tv_sec,
    timerarray[loop].beforeheader.tv_usec,
    timerarray[loop].afterheader.tv_sec,
    timerarray[loop].afterheader.tv_usec,
    timerarray[loop].afterbody.tv_sec,
    timerarray[loop].afterbody.tv_usec,
    timerarray[loop].exittime.tv_sec,
    timerarray[loop].exittime.tv_usec,
    timerarray[loop].totalbytes,
    timerarray[loop].bodybytes);
  } /* end for loop */
    } /* end if recording all transactions */

    D_PRINTF( "total errors: %d\n",timestat.rs.totalerrs );
    /* gethostname(timestat.hostname,MAXHOSTNAMELEN); */
    /* D_PRINTF( "Test for host: %s\n",timestat.hostname ); */
    D_PRINTF( "Server is: %s running at port number: %d\n",
  webserver,portnum );

    /* sprintf(timestat.hostname,"%s:%d",timestat.hostname,getpid()); */
    if (amclient) /* CLIENT TO A WEBMASTER */
    {
   char *stats_as_text;

        /*
         * SEND THE TIMING DATA TO THE MASTER
         */
  stats_as_text = stats_to_text(&timestat);
  D_PRINTF( "stats_to_text returned %s\n", stats_as_text );

  returnval = senddata(mastersock, stats_as_text,
    SIZEOF_STATSTEXTBASE + number_of_pages*SIZEOF_DOUBLETEXT);
  D_PRINTF( "Wrote time stats to master %d\n", returnval );

  if (returnval < 1)
    {
      D_PRINTF( "Error while writing time stats: %s\n",
        neterrstr() );
      errexit("Error while writing time stats: %s\n",
        neterrstr());
    }

  if (uil_filelist_f)
    /* write pagestats */
    {
      char *page_stats_as_text;
      for (i = 0; i < number_of_pages; i++)
        {
    D_PRINTF( "On page_stats[%d]\n", i );
    page_stats_as_text = page_stats_to_text(&page_stats[i]);
    returnval = strlen(page_stats_as_text);
    D_PRINTF( "page_stats_to_text[%d] returned %d\n",
      i, returnval );
    returnval = senddata(mastersock, page_stats_as_text,
             SIZEOF_PAGESTATSTEXT);
    if (returnval < 1)
      {
        D_PRINTF( "Error while writing page_stats[%d]: %s\n",
          i, neterrstr() );
        errexit("Error while writing page_stats[%d]: %s\n",
          i, neterrstr());
      } /* end if */
    D_PRINTF( "Wrote %d bytes of page_stats[%d] to master\n",
      returnval, i );
        } /* end for */
    } /* end if filelist */

  D_PRINTF( "About to close socket\n" );
        if (NETCLOSE(mastersock))
      D_PRINTF( "Close socket error: %s\n", neterrstr() );
    }
    else /* NOT A CLIENT TO A WEBMASTER */
    {
  if (testtime)
    {
      printf("Test ran for: %d minutes\n",(testtime/60));
  }
  else
  {
      printf("Test ran for: %d iterations.\n",numloops);
  }
  compdifftime(&(timestat.endtime), &(timestat.starttime),
    &(runningtime));
  printf("Total time of test (sec) %d.%d\n", runningtime.tv_sec,
    runningtime.tv_usec);
  printf("Files retrieved per iteration: %d\n",numfiles);  /* 'per iteration' */
  printf("----------------------------------\n");
  printf("Totals:\n\n");
  rqstat_print(&(timestat.rs));

  if (timestat.rs.totalconnects == 0)
      goto end;
  printf("Thruput = %5.2lf Kbytes/sec\n",
         thruputpersec(timestat.rs.totalbytes, &runningtime) / 1000);

  if (uil_filelist_f && numloops && verbose)
  {
      for (loop = 0; loop < number_of_pages; loop++)
      {
    if (timestat.page_numbers[loop] != 0)
    {
        printf ("===============================================================================\n");
        printf ("Page # %d\n\n", loop);
        printf ("Total number of times page was hit %d\n",
          page_stats[loop].totalpages);
        rqstat_print(&(page_stats[loop].rs));
        printf ("Page size %d \n", page_stats[loop].page_size);
        printf ("===============================================================================\n\n");
    } /* END if timestat */
      } /* END for loop */
  } /* END if filelist */
    } /* END if client */

end:
    if(record_all_transactions)
      fclose(logfile);
    if(debug)
    {
  D_PRINTF( "Client exiting.\n" );
  fclose(debugfile);
    }

#ifdef WIN32
    /* tell parent we're done */
    InterlockedIncrement(&CounterSemaphore);
#endif /* WIN32 */

} /* END ClientThread() */
Ejemplo n.º 7
0
void
ACE_TMAIN(int argc, ACE_TCHAR *argv[])
{
    int   file_count=0;
    int   getoptch;
    int   currarg;
    extern char *optarg;
    extern int  optind;
    int   i, j;
    char  *tempch;
    int   err;

#define SLEEP_USEC  100
#ifdef WIN32
    WSADATA   WSAData;
#else

    struct timeval sleeptime;

    /* set the amount of time that we'll pause before sending a "." to the
       webmaster */

    sleeptime.tv_sec = SLEEP_USEC/1000000;
    sleeptime.tv_usec = SLEEP_USEC % 1000000;
#endif /* WIN32 */

    debugfile = stderr;

#ifdef WIN32
    MessageBeep(~0U); /* announce our existence */
    MessageBeep(~0U);
    MessageBeep(~0U);

    err = WSAStartup(MAKEWORD(1,1), &WSAData);
    if (err != 0) {
  errexit("Error in WSAStartup()\n");
    }

    atexit(sock_cleanup);

    SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_HIGHEST);

    /* create semaphore in locked state */
    hSemaphore = CreateSemaphore(0, 0, 1, 0);
    if(hSemaphore == 0)
    {
  errexit("Create semaphore failed: %d", GetLastError());
    }
#endif /* WIN32 */

    memset(webserver, 0, sizeof(webserver));
    memset(webmaster, 0, sizeof(webmaster));
    memset(proxyserver, 0, sizeof(proxyserver));
    memset(connectstr, 0, sizeof(connectstr));

    /*
     * PARSE THE COMMAND LINE OPTIONS
     */

    while((getoptch = getopt(argc,argv,"P:f:t:l:p:u:R:w:c:n:sdv")) != EOF)
    {
        switch(getoptch)
        {
  case 'c':
            sprintf(connectstr, "%s", optarg);
      amclient = 1;
            printf("%s", OKSTR);     /* sent back to webmaster */
            fflush(stdout);
      break;
        case 'd':
            debug = 0; /* sumedh */
            break;
  case 'f':
      sprintf(configfile, "%s", optarg);
      break;
        case 'l':
            numloops = atoi(optarg);
            break;
        case 'n':
            numclients = atoi(optarg);
            break;
  case 'u':
      sprintf(uil_filelist, "%s", optarg);
      uil_filelist_f = 1;
      break;
  case 'p':
      portnum = atoi(optarg);
      break;
  case 's':
      savefile = 1;
      break;
        case 't':
            testtime = 60 * atoi(optarg);
            break;
  case 'v':
      verbose = 1;
      break;
        case 'w':
            havewebserver = 1;
            sprintf(webserver,"%s",optarg);
            break;
  case 'P':
      haveproxyserver = 1;
      sprintf(proxyserver, "%s", optarg);
      break;
  case 'R':
      record_all_transactions = 1;
      break;
        default:
            usage(argv[0]);
        }
    }

    returnerr("Client begins...\n");
    D_PRINTF( "Running in debug mode\n\n" );

    /* print the command line */
    for (i = 0; i < argc; i++)
  D_PRINTF( "%s ", argv[i] );
    D_PRINTF( "\n\n" );

    if(testtime && numloops)
    {
  /*
         * EITHER numloops OR testtime, BUT NOT BOTH.
         */
        usage(argv[0]);
    }

    if(havewebserver != 1)
    {
#ifdef WIN32
        /*
         * THE SERVER'S NAME MUST BE SPECIFIED
         */
        returnerr("No WWW Server specified\n");
        usage(argv[0]);
#else
  /* IF IT ISN'T, WE ASSUME LOCALHOST */
  sprintf(webserver, "%s", "localhost");
  havewebserver = 1;
#endif /* WIN32 */
    }

    currarg = optind;
    numfiles = 0;
    while(currarg != argc)
    {
       /*
        * GET THE URLS TO RETRIEVE.
        */
       if (numfiles == MAXNUMOFFILES) {
     returnerr("Maximum of %d files on the command line.\n");
     usage(argv[0]);
       }
       sscanf(argv[currarg],"%s",filelist[numfiles]);
       numfiles++;
       currarg++;
    }

    if ((numfiles != 0) && uil_filelist_f)
    {
  returnerr("Both a filelist and UIL specified.\n");
  usage(argv[0]);
    }

    if((numfiles == 0) && !(uil_filelist_f))
    {
        /*
         * AT LEAST ONE FILE MUST BE SPECIFIED
         */
  returnerr("No UIL resources or filelist specified \n");
        usage(argv[0]);
    }

    if((numloops == 0) && (testtime == 0))
    {
        /*
         * NO SPECIFIED NUMBER OF LOOPS, AND NO TEST TIME
         */
       usage(argv[0]);
    }
    if(numclients > MAXPROCSPERNODE || numclients < 1)
    {
  returnerr("Number of Clients must be between 1 and %d\n", MAXPROCSPERNODE);
  exit(1);
    }

    /* allow use of IP address */
    if(amclient) {
  if((tempch = strpbrk(connectstr,":")) == 0)
  {
      /*
       * INCORRECT FORMAT OF ConnectStr. CORRECT FORMAT IS
       * HOSTNAME:PORT OR HOST-IP:PORT
       */
      D_PRINTF( "Incorrect format %s: use hostname:port or ip_addr:port\n",
         connectstr );
      returnerr("Incorrect format %s: use host:port or ip_addr:port\n", connectstr);
      exit(1);
  } else {
      strncpy(webmaster, connectstr, tempch-connectstr);
  }
  if(resolve_addrs(webmaster, "tcp", &webmast_phe, &webmast_ppe, &webmast_addr, &webmast_type))
      exit(1);
    }

    if (haveproxyserver)
    {
  D_PRINTF( "Copying proxy %s to webserver\n", proxyserver );
  strcpy(webserver, proxyserver);
    }

    if (resolve_addrs(webserver, "tcp", &webserv_phe, &webserv_ppe, &webserv_addr, &webserv_type))
  exit(1);

    /*
     * INITIALIZE DATA
     */
    /* allocate space for dynamic arrays */
    load_file_list =
      (page_list_t *)mymalloc((MAXNUMOFPAGES)*sizeof(page_list_t));

    if (uil_filelist_f)
    {
      /* take a guess at the number of URLs in the file */
      D_PRINTF( "About to parse filelist %s\n", uil_filelist );
      number_of_pages = count_file_list(uil_filelist);
      numfiles = 1;

      /* IF WE HAVE A FILELIST, PARSE IT */
      /* allocate memory */
      D_PRINTF( "Allocating page list: %ld by %d\n",
  number_of_pages, numfiles );
      for (i=0; i<number_of_pages; i++)
  {
    for (j=0; j<MAXNUMOFFILES; j++)
      {
        load_file_list[i].servername[j] =
    (char *)mymalloc(URL_SIZE);
        load_file_list[i].filename[j] =
    (char *)mymalloc(URL_SIZE);
      }
  }

      D_PRINTF( "Parsing file list: %s\n", uil_filelist );
      parse_file_list(uil_filelist, load_file_list,
          &number_of_pages, &numfiles);
      /* free memory for pages that won't be used? */
      D_PRINTF( "Actual page list: %ld by %d\n",
  number_of_pages, MAXNUMOFFILES );

      D_PRINTF( "Setting up weighting for %ld pages\n",
  number_of_pages );
      total_weight = load_percent(load_file_list, number_of_pages);
      /*      total_weight = load_percent(load_file_list, number_of_pages, pages); */
    }
    else
    {
  /* no uil file */
  number_of_pages = numfiles;
    }

    if (number_of_pages < 1)
      {
  /* no pages - exit */
  D_PRINTF( "No valid URLs found\n" );
  errexit("No valid URLs found\n");
      }

#ifndef WIN32
    /*
     * IF WE ARE TO FORK ADDITIONAL CLIENTS ON THIS MACHINE,
     * WE MUST DO IT BEFORE WE CONNECT TO THE MASTER.
     *
     * FIRST, SET UP SIGNAL HANDLING
     */
    signal(SIGCHLD, childhandler);
    for(i = 0; i < numclients; i++)
    {
      thr_create (0, 0, ClientThread, 0, THR_BOUND, 0);

      /* switch(fork())
         {
             case 0:
                 numclients = 1;
                 ClientThread(0);
                 exit(0);
                 break;
             case -1:
                 errexit("Error forking child processes\n");
                 exit(1);
             default:
                break;
         } */
      select(0,(fd_set *)0,(fd_set *)0, (fd_set *)0, &sleeptime);
    }

    /*
     * Wait for all children to exit.
     */

    while (thr_join(0, 0, 0) == 0);

    /*     for(;;)
       {
      int pid = wait((int*)0);
  if ((pid == -1) && errno == ECHILD) break;
       } */
#else
    /* start threads on NT */
    for (i = 0; i < numclients; i++)
    {
  if (_beginthread(ClientThread, 0, 0) == -1)
  {
      errexit("_beginthread failed: %d", GetLastError());
  }
    }
#endif /* WIN32 */

#ifdef WIN32
    /* wait for children to get to sync point */
    while (CounterSemaphore < numclients)
        sleep(1);
    CounterSemaphore = 0;

    /* start all children simultaneously */
    ReleaseSemaphore(hSemaphore, 1, 0);

    if (testtime) {
  sleep(testtime);
  alarmhandler(); /* signal end of test to threads */
    }

    /*
     * Wait for all threads to exit.
     */
    while (CounterSemaphore < numclients)
        sleep(1);

    CloseHandle(hSemaphore);
#endif /* WIN32 */

    return;
} /* end main() */
Ejemplo n.º 8
0
/*
 * fetch the set of files that constitute a page
 *
 * maxcount = the number of files in the WWW page
 * pageval = the number of the WWW page (offset in load_file_list[])
 *  (if -1, use page # 0 - does this still work?)
 *
 * returns the number of files retrieved
 */
static int
makeload(int maxcount, int pageval, THREAD rqst_timer_t *timerarray, THREAD stats_t *timestat, THREAD SOCKET mastersock, THREAD page_stats_t *page_stats)
{
    int cnt;
    int returnval;
    page_stats_t page_stats_tmp;
    char server[MAXHOSTNAMELEN];

    NETPORT loc_portnum;

    D_PRINTF( "Starting makeload(maxcount %d, pageval %d)\n",
  maxcount, pageval );

    strcpy( server, webserver); /* Put in default value */

    page_stats_init(&page_stats_tmp);
    D_PRINTF( "Page stats initialized\n" );

    for (cnt = 0; cnt < maxcount; cnt++)
    {
  D_PRINTF( "Loop count %d in makeload()\n", cnt );
  if (pageval == -1)
  {
      pageval = cnt;
  }
  if (timeexpired)
  {
      break;
  }

  /* check for a filename */
  if (strlen(load_file_list[pageval].filename[cnt]) < 1)
  {
      D_PRINTF( "Bad filename at pageval %d, count %d\n",
    pageval, cnt );
      return(returnerr("Bad filename at pageval %d, count %d\n",
    pageval, cnt));
  }

        /*        if (load_file_list[pageval].port_number[cnt] != 0)
        {
            loc_portnum = load_file_list[pageval].port_number[cnt];
        }
        else
        {
            loc_portnum = portnum;
        } */
        loc_portnum = portnum;
        if ((load_file_list[pageval].servername[cnt] != 0)
        &&
      *load_file_list[pageval].servername[cnt])
  {
      D_PRINTF( "Copying URL server %s to server\n",
    load_file_list[pageval].servername[cnt] );
      strcpy(server, load_file_list[pageval].servername[cnt]);
  }

  if (haveproxyserver)
    {
      D_PRINTF( "Copying proxy %s to webserver\n", proxyserver );
      strcpy(server, proxyserver);
    }


  D_PRINTF( "Calling get(%s, %d, %s, &(timearray[%d]))\n",
      server, loc_portnum, load_file_list[pageval].filename[cnt],
      cnt );

  returnval = get(server, loc_portnum,
        load_file_list[pageval].filename[cnt],
        &(timerarray[cnt]));
  if (returnval < 0)
  {
      D_PRINTF( "***GET() RETURNED AN ERROR\n" );
  }

  /*
  * DID GET() RETURN A VALID TIME?
  */
  if ((returnval == 0) && (timerarray[cnt].valid == 2))
  {
      timerarray[cnt].page_number = pageval;

      accumstats(&timerarray[cnt], &page_stats_tmp, timestat);
  }
  else if (!timeexpired) /* INVALID, INCREMENT THE ERROR COUNTER */
  {
      D_PRINTF( "GET error counter incremented\n" );
      timestat->rs.totalerrs++;
  }

  if (amclient) {
      fd_set readfds;
      struct timeval timeout;
      int rv;

      timeout.tv_sec = 0;
      timeout.tv_usec = 0;
      FD_ZERO(&readfds);
      FD_SET(mastersock, &readfds);

      /* if the webmaster has aborted, quit */
      D_PRINTF("Before select() on webmaster socket\n");
      if (rv = select(FD_SETSIZE, &readfds, 0, 0, &timeout))  {
    D_PRINTF("select() returned %d\n", rv);
    D_PRINTF("Client terminating at request of webmaster\n");
    exit(2);
      }
  }

    } /* END for cnt */

    /*
     * DO WE HAVE A VALID RETURN FROM GET()?
     * WHY NOT USE returnval HERE?
     */
    if ((returnval == 0) &&
    (cnt == load_file_list[pageval].num_of_files) &&
    (timerarray[cnt-1].valid == 2))
    {
  rqst_stats_t *ps_rs;
  rqst_stats_t *pst_rs;

  ps_rs = &(page_stats[pageval].rs);
  pst_rs = &(page_stats_tmp.rs);

  rqstat_sum(ps_rs, pst_rs);

  page_stats[pageval].totalpages++;

  if (page_stats[pageval].page_size == 0)
  {
      page_stats[pageval].page_size = (unsigned)
    page_stats_tmp.rs.totalbody;
  }
    }

    D_PRINTF( "\nMakeload output page %d: %d errors, %d pages\n",
      pageval, timestat->rs.totalerrs, page_stats[pageval].totalpages );
    D_PRINTF( "Makeload returning %d\n", cnt );

    return(cnt);

} /* END makeload() */
Ejemplo n.º 9
0
SOCKET
connecttomaster(char *str)
{
    char    *tempch;
    SOCKET  sock;
    char  msg[100];
    char  ConnectStr[100];  /* Fix to handle multiple threads */
    int   tries;

    strcpy(ConnectStr, str);

    /*
     * BREAK UP THE connectstr INTO A HOSTNAME/HOST-IP AND A PORT NUMBER.
     */
    if((tempch = strpbrk(ConnectStr,":")) == 0)
    {
        /*
         * INCORRECT FORMAT OF ConnectStr. CORRECT FORMAT IS
         * HOSTNAME:PORT OR HOST-IP:PORT
         */
        D_PRINTF( "Incorrect format %s: use hostname:port or ip_addr:port\n",
    ConnectStr );
  return(returnerr("Incorrect format %s: use host:port or ip_addr:port\n",
    ConnectStr));
    }

    /*
     * ZERO OUT THE COLON SO WE HAVE TWO STRINGS, THE HOSTNAME AND THE PORT
     */
    *tempch = '\0';
    tempch++;

    /* loop here to connect to webmaster - TCP/IP allows no more than 5
     * connection requests outstanding at once and thus the webmaster may
     * reject a connection if there are a lot of client processes
     */
#define MAXTRIES 30
#define TRYDELAY_SEC  1
    for (tries = 0; tries < MAXTRIES; tries++) {

        sock = connectsock(ConnectStr,(NETPORT)atoi(tempch),"tcp");

  if (!BADSOCKET(sock))
      break;

  sleep(TRYDELAY_SEC);
    }

    if (BADSOCKET(sock))
    {
  /*  ERROR CONNECTING TO MASTER PROCESS */
  return(returnerr("Could not connect to master process\n"));
    }

    /*
     * SIGNAL THE MASTER THAT WE ARE READY TO PROCEED.  WHEN ALL
     * CHILD PROCESSES HAVE CONNECTED AND SENT THIS SIGNAL,
     * THE MASTER WILL ISSUE US A GO SIGNAL.
     */
    if(NETWRITE(sock,READYSTR,READYSTRLEN) != READYSTRLEN)
    {
  return(returnerr("Error sending READY message to master"));
    }

    memset(msg,0,GOSTRLEN+1);
    if(NETREAD(sock,msg,GOSTRLEN) != GOSTRLEN)
    {
  D_PRINTF( "Error receiving GO message from master: %s\n", neterrstr()
    );
  return(returnerr("Error receiving GO message from master\n"));
    }

    if(strncmp(GOSTR,msg,GOSTRLEN))
    {
       /*
        * WE RECEIVED A MESSAGE OTHER THAN GO. PRINT IT OUT AND RETURN ERROR
        */
  return(returnerr("Received non-GO message %s\n",msg));
    }

    return(sock);

} /* END connecttomaster() */
Ejemplo n.º 10
0
/* connect to a socket given the hostname and protocol */
SOCKET
connectsock(char *host, NETPORT portnum, char *protocol)
 {
  struct sockaddr_in sin;   /* an Internet endpoint address */
  SOCKET  s;              /* socket descriptor */
  int     type;           /* socket type */
  short   proto;
  int     returnval;  /* temporary return value */

  D_PRINTF( "Beginning connectsock; host=%s port=%d proto=%s\n", host,
  portnum, protocol );

  sin.sin_family = AF_INET;
  memset((char *)&sin, 0, sizeof(sin));
  D_PRINTF( "Zeroed address structure\n" );

  sin.sin_port = htons(portnum);
  D_PRINTF( "Set port number %d\n", portnum );

  /* get the contact information */
  if (strcmp(host, webserver) == 0) {
      sin.sin_addr.S_ADDR = webserv_addr;
      sin.sin_family = PF_INET;
      proto = webserv_ppe.p_proto;
      type = webserv_type;
  } else if (strcmp(host, webmaster) == 0) {
      sin.sin_addr.S_ADDR = webmast_addr;
      sin.sin_family = PF_INET;
      proto = webmast_ppe.p_proto;
      type = webmast_type;
  } else {
      struct hostent  host_phe;
      struct protoent host_ppe;
      unsigned long host_addr;
      short     host_type;       /* socket type */

      if (resolve_addrs(host, "tcp", &host_phe, &host_ppe, &host_addr, &host_type))
    return returnerr("Can't resolve hostname %s in get()\n", host);
      sin.sin_addr.S_ADDR = host_addr;
      sin.sin_family = PF_INET;
      proto = host_ppe.p_proto;
      type = host_type;
  }

  /* Allocate a socket */
  s = socket(PF_INET, type, proto);
  D_PRINTF( "Socket %d returned %d, %s\n",
  type, s, neterrstr() );

  if (BADSOCKET(s))
  {
    D_PRINTF( "Can't create socket: %s\n",neterrstr() );
    return BADSOCKET_VALUE;
  }

  /* Connect the socket */
  D_PRINTF( "Trying to connect %d with size %d, %s\n",
  s, sizeof(sin), neterrstr() );
  D_PRINTF( "Address is family %d, port %d, addr %s\n",
  sin.sin_family, ntohs(sin.sin_port),
  inet_ntoa(sin.sin_addr) );

  returnval = connect(s, (struct sockaddr *)&sin, sizeof(sin));
  D_PRINTF( "Connect returned %d, %s\n",
  returnval, neterrstr() );
  if (returnval < 0)
    {
      D_PRINTF( "Can't connect: %s\n", neterrstr() );
      NETCLOSE(s);
      return BADSOCKET_VALUE;
    }

  /* all done, returning socket descriptor */
  D_PRINTF( "Returning %d from connectsock call\n", s );
  return(s);

} /* END connectsock() */
Ejemplo n.º 11
0
/*
 * parse_file_list()
 * given a filename, a pointer to a page list, and pointers to integers
 * for the number of pages and the number of files, parse its contents.
 */
void
parse_file_list (const char *url_list_file, page_list_t *page_list,
                 long int *num_of_pages, long int *num_of_files)
{
  FILE        *fp;
  int         filenum;
  int         returnval;
  int         loadnum;
  char        a_line[BUFSIZ];
  char        tempbuf[BUFSIZ];
  char        *textvalue;
  int         numfiles = 1, numpages = 0;
  int         maxpages = *num_of_pages;
  page_list_t *pp;

  extern int haveproxyserver;

  fp = fopen(url_list_file, "r");

  if (fp == 0)
    {
      errexit("Error %d opening filelist: %s\n", errno, strerror(errno));
    }
  else
    {
      /*
       * GRAB A LINE. FORMAT IS: URL WEIGHTINGFACTOR
       * EXAMPLE: http://www/file.html 1
       */
      D_PRINTF( "File is open.\n" );
      while(fgets(a_line, BUFSIZ, fp) != 0)
        {
          textvalue = a_line;
          /* check for comments */
          if (strchr(textvalue, '#') != 0)
            {
              /* throw out anything after a '#' */
              D_PRINTF( "Stripping comment from line: %s\n", textvalue );
              returnval = strcspn(textvalue, "#");
              D_PRINTF( "Found first # at %d\n", returnval );
              if (returnval == 0)
                continue;
            }

          if (numpages >= *num_of_pages)
          errexit("Out of space in parse_file_list()\n");

          pp = &page_list[numpages];

          D_PRINTF( "Processing page %ld\n", numpages );
          loadnum = 0;

          if (textvalue != 0)
            { 
              /* is there more? */
              /* check for weighting factor */
              D_PRINTF( "Setting page values from: %s\n", textvalue );
              returnval = sscanf(textvalue, "%s%d", tempbuf, &loadnum);
              D_PRINTF("Scan for weighting returns %d, %d\n",
                       returnval, loadnum);
              if (returnval == EOF || loadnum <= 0)
                {
                  pp->load_num = 1;
                }
              else
                {
                  pp->load_num = loadnum;
                }
              D_PRINTF("Setting load=%d for line: %s\n",
                       pp->load_num, textvalue);

              /* placeholder for grouping multiple files onto one page */
              pp->num_of_files = 1;
              filenum = 0;

              textvalue = tempbuf;
              D_PRINTF( "Line is now: %s\n", textvalue );

              /*
              * if we've got a proxy server, we'll assume that the
              * remaining text is a valid URL, and stuff it into
              * page_list[numpages].filename[filenum]
              * Otherwise, we'll have to parse it out.
              */

              if (haveproxyserver)
                {
                  pp->servername[filenum] = 0;
                  pp->port_number[filenum] = 0;
                  strcpy(pp->filename[filenum], textvalue);
                }
              else /* no proxy server, so we have to parse it out... */
                {
                  /* try http://server(:port)/file */
                  D_PRINTF( "Trying http://server(:port)/filename\n" );
                  returnval = sscanf(textvalue, "http://%[^/]%s",
                                     tempbuf,
                                     a_line);
                  /* check server string for :port */
                  if (returnval != 0 && returnval != EOF)
                    {
                      D_PRINTF( "Setting filename %s\n", a_line );
                      strcpy(pp->filename[filenum], a_line);

                      D_PRINTF( "Checking %s for :portnumber\n", tempbuf );
                      returnval = sscanf(tempbuf, "%[^:]:%d", a_line,
                      &pp->port_number[filenum]);

                      if (returnval < 2)
                        {
                          pp->port_number[filenum] = 80;
                        }
                      if (returnval == EOF)
                        {
                          pp->servername[filenum] = 0;
                        }
                      else
                        {
                          D_PRINTF("Port number %d, setting server %s\n",
                                   pp->port_number[filenum], a_line);

                          strcpy(pp->servername[filenum], a_line);
                        }

                      D_PRINTF("Server %s, port number %d\n",
                               pp->servername[filenum],
                               pp->port_number[filenum]);
                    }
                  else /* no good - try straight filename */
                    {
                      pp->port_number[filenum] = 80;
                      D_PRINTF( "Trying filename, returnval=%d\n", returnval );
                      pp->servername[filenum] = 0;
                      D_PRINTF("Server %s, port number %d\n",
                               pp->servername[filenum], pp->port_number[filenum]);
                      returnval = sscanf(textvalue, "%s", a_line);
                      D_PRINTF( "Scan returned filename %s\n", a_line );

                      strcpy(pp->filename[filenum], a_line);
                    } /* end of parsing */
                } /* end if haveproxyserver */

              D_PRINTF("Done parsing line\n");
              D_PRINTF("Got server %s, port %d, file %s, returnval %d\n",
                       pp->servername[filenum],
                       pp->port_number[filenum],
                       pp->filename[filenum],
                       returnval);
            } /* end if textvalue not NULL */

          numpages++;
        } /* end while not EOF */
      if (numpages < 1)
        {
          returnerr("No files are specified by filelist\n");
        }
    } /* end if file ok */
    fclose(fp);
    D_PRINTF("Returning %ld pages and %ld files\n",
             numpages, numfiles );

    *num_of_pages = numpages;
    *num_of_files = numfiles;
}