1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
|
/*
* apt-spy (c) Steven Holmes, 2003.
* This software is licensed as detailed in the LICENSE file.
*/
void usage(void);
void version(void);
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <limits.h>
#include "include/update.h"
#include "include/file.h"
#include "include/parse.h"
#include "include/benchmark.h"
/* Our version number. */
const char apt_spy_v[] = "v3.1";
/* Defaults */
/* The default area */
char d_area[] = "All";
/* Default file locations */
char d_out[] = "/etc/apt/sources.list";
char d_config[] = "/etc/apt-spy.conf";
char d_mirror[] = "/var/lib/apt-spy/mirrors.txt";
/* Default file to grab when benchmarking */
char d_file[] = "ls-lR";
/* Default update URL */
char d_update_url[] = "http://http.us.debian.org/debian/README.mirrors.txt";
int BESTNUMBER = 5;
int main(int argc, char *argv[])
{
int c;
char *cur_entry; /* Entry we are benchmarking */
char *distrib = NULL; /* distrubtion to use. */
char *mirror_list = NULL; /* mirror list file */
char *config_file = NULL; /* configuraion file */
char *proxy = NULL; /* Proxy server to use */
char *infile = NULL; /* optional infile */
char *outfile = NULL; /* outfile name */
char *topfile = NULL;
char *area = NULL; /* Area to test */
char *grab_file = NULL; /* File to grab */
char *update_url = NULL; /* URL to use for updating */
char *country_list = NULL; /* List of countries to b/m */
FILE *infile_p, *outfile_p; /* input/output file pointers */
FILE *config_p; /* config file pointer */
FILE *mirror_p; /* mirror list pointer */
FILE *topfile_p;
int timeout = 15; /* time to benchmark each server */
int toplist = 0; /* Whether to write toplist. */
/* Number of servers to test. If negative, test them all. */
int test_number = -1;
/* Server information structures. */
server_t current, *best;
/* Parse options... */
while((c = getopt(argc, argv, "a:c:d:e:f:i:m:o:p:s:t:u:w:n:vh")) != -1)
switch(c) {
/* Area to benchmark */
case 'a':
area = optarg;
break;
/* Distribution we'll write into apt-sources. */
case 'd':
distrib = optarg;
break;
/* Configuration file to use */
case 'c':
config_file = optarg;
break;
/* Number of servers to benchmark */
case 'e':
test_number = atoi(optarg);
break;
/* File, relative to Debian base, to grab from server. */
case 'f':
grab_file = optarg;
break;
/* User-specified list of servers to benchmark. */
case 'i':
infile = optarg;
break;
/* The list of mirrors */
case 'm':
mirror_list = optarg;
break;
/* The output file we use */
case 'o':
outfile = optarg;
break;
/* Proxy server we should use */
case 'p':
proxy = optarg;
break;
/* List of countries to benchmark */
case 's':
country_list = optarg;
break;
/* Time to bencmark each server for. */
case 't':
timeout = atoi(optarg);
break;
/* The URL we should update ourselves from */
case 'u':
update_url = optarg;
break;
/* Should we write a list of the "top" servers? */
case 'w':
toplist = 1;
topfile = optarg;
break;
/* Number of servers to write in "top" server list */
case 'n':
BESTNUMBER = atoi(optarg);
break;
case 'v':
version();
break;
/* Help!! */
case 'h':
default:
usage(); /* display help */
}
argc -= optind;
argv += optind;
/* Simple check for stupidity */
if ((test_number >= 0) && (BESTNUMBER > test_number))
BESTNUMBER = test_number;
best = malloc(sizeof(server_t) * (BESTNUMBER + 1));
if (best == NULL) {
perror("malloc");
exit(1);
}
/* We require an area and distribution argument if we are not updating */
if ((argc == 0) && (distrib == NULL))
usage();
/* Check for silly combination of country and area arguments */
if ((area != NULL) && (country_list != NULL))
usage();
/* Setup default area argument */
if ((area == NULL) && (country_list == NULL))
area = d_area;
/* Setup default file argument if none given */
if (grab_file == NULL)
grab_file = d_file;
/* Open mirror file. We pass argc so it can tell if we're updating
or not */
mirror_p = select_mirror(mirror_list, argc);
if (mirror_p == NULL) {
perror("fopen");
fprintf(stderr, "Error opening mirror file. Exiting.\n");
exit(1);
}
/* the only possible argument now is "update", for updating the
mirrors list */
if (argc == 1) {
if (strcmp(argv[0], "update") != 0)
usage();
/* If necessary, set update_url to default */
if (update_url == NULL)
update_url = d_update_url;
if (update(mirror_p, update_url, proxy) != 0) {
fprintf(stderr, "Update failed. Exiting.\n");
exit(1);
}
printf("Update complete. Exiting.\n");
exit(0);
}
/* argc should be 0. If not, there's something wrong. */
if (argc != 0)
usage();
/* We open the infile. Either a temporary file, or a user-specified
one. */
infile_p = select_infile(infile);
if (infile_p == NULL) {
perror("tmpfile");
fprintf(stderr, "Failed to open infile. Exiting.\n");
exit(1);
}
/* Set up default if user hasn't specified an outfile */
if (outfile == NULL)
outfile = d_out;
/* Check output file for accessibility */
if (check_write_access(outfile) == 1) {
fprintf(stderr, "Could not open outfile. Exiting.\n");
exit(1);
}
/* If topfile specified, check for accessibility */
if (topfile) {
if (check_write_access(topfile) == 1) {
fprintf(stderr, "Could not open topfile. Exiting.\n");
exit(1);
}
}
/* Open config file */
config_p = select_config(config_file);
if (config_p == NULL) {
perror("fopen");
fprintf(stderr, "Error opening config file. Exiting.\n");
exit(1);
}
/* Fill temporary file with useful stuff if it's not user-specified. */
if (infile == NULL) {
if (area != NULL) {
if (build_area_file(config_p, infile_p, mirror_p, area) != 0) {
fprintf(stderr,
"Error building area file. Exiting.\n");
exit(1);
}
} else {
if (build_country_file(config_p, infile_p, mirror_p, country_list) != 0) {
fprintf(stderr,
"Error building country file. Exiting.\n");
exit(1);
}
}
}
/* Make sure we're at the beginning... */
rewind(infile_p);
/* Zero the "best" structure */
for (c = 0; c < BESTNUMBER; c++)
memset(&best[c], 0, sizeof(server_t));
/* This is the main loop. It'll exit when we've exhausted the URL
list or test_number is 0 */
while (((cur_entry = next_entry(infile_p)) != NULL) && test_number != 0) {
if (ferror(infile_p) != 0) {
fprintf(stderr, "Error while reading input file\n");
exit(1);
}
/* Turn entry into a pretty structure */
tokenise(¤t, cur_entry);
/* Do the benchmark */
if (benchmark(¤t, proxy, timeout, grab_file) != 0) {
fprintf(stderr,
"Error while performing benchmark. Exiting.\n");
exit(1);
}
decide_best(¤t, best);
free(cur_entry);
if (test_number > 0)
--test_number;
}
/* Open the output file... */
outfile_p = select_outfile(outfile);
if (outfile_p == NULL) {
perror("fopen");
fprintf(stderr, "Error opening output file. Exiting.\n");
exit(1);
}
/* write the results */
printf("Writing new sources.list file: %s \n", outfile);
if (write_list(outfile_p, best, distrib) != 0) {
fprintf(stderr, "Error writing output file. Exiting.");
exit(1);
}
/* We write out the top servers to a file if asked. Note there's no
point in freeing the "best" structures. */
if (topfile) {
/* Open the topfile */
topfile_p = select_outfile(topfile);
printf("writing topfile: %s\n", topfile);
if (topfile_p == NULL) {
perror("fopen");
fprintf(stderr, "Error opening topfile. Exiting.\n");
exit(1);
}
if (write_top(infile_p, topfile_p, best) != 0) {
fprintf(stderr,
"Error writing top servers list. Exiting.");
exit(1);
}
}
/* We're all done */
exit(0);
}
void usage()
{
printf("Usage: apt-spy [options]\n"
"options:\n"
"-d distribution\tDebian distribution (ie, stable). Required unless updating.\n"
"-a area\t\tArea to benchmark. (eg, Europe).\n"
"-c config\tConfiguration file to use.\n"
"-e number\tNumber of servers to benchmark before exiting.\n");
printf("-f file\t\tFile to grab when benchmarking. (relative to Debian base).\n"
"-i file\t\tSpecify input file. For use with the -w option.\n"
"-m mirror-list\tMirror list to use, or mirror-list to update when updating.\n"
"-o output-file\tWhere to put output.\n"
"-p proxy\tProxy server to use. In format <server>:[port]\n"
"-s country_list\tList of countries to benchmark. Cannot be used with -a.\n"
"-t time\t\tTime to benchmark each server for. An approximate value only.\n"
"-u update-URL\tURL to grab mirror-list from when updating.\n");
printf("-w file\t\tOutput top servers (5 by default) to file for use with -i.\n"
"-n number\tSpecifies number of top servers to output with -w.\n"
"-v\t\tOutput a version number.\n"
"-h\t\tDisplay this message.\n"
"update\t\tUpdate the mirror list.\n");
exit(0);
}
void version()
{
printf("apt-spy %s\n", apt_spy_v);
exit(0);
}
|