get-time-often.c 12.3 KB
Newer Older
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
/*
 * Copyright (C) 2003  Lysator Academic Computer Association.
 *
 * This file is part of the LysKOM server.
 * 
 * LysKOM is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by 
 * the Free Software Foundation; either version 1, or (at your option) 
 * any later version.
 * 
 * LysKOM is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with LysKOM; see the file COPYING.  If not, write to
 * Lysator, c/o ISY, Linkoping University, S-581 83 Linkoping, SWEDEN,
 * or the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, 
 * MA 02139, USA.
 *
 * Please mail bug reports to bug-lyskom@lysator.liu.se. 
 */

/*
 * Issue as many get-time requests to the specified server as it can
 * accept.
 */

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include <ctype.h>
#include <errno.h>
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#ifdef TIME_WITH_SYS_TIME
#  include <sys/time.h>
#  include <time.h>
#else
#  ifdef HAVE_SYS_TIME_H
#    include <sys/time.h>
#  else
#    include <time.h>
#  endif
#endif
#include <unistd.h>
#include <sys/socket.h>
#include <fcntl.h>

#include "oop.h"

#include "s-string.h"
#include "isc.h"

60
#include "timeval-util.h"
61
#include "getopt.h"
62
63
64
65
66
67
68
69
70
71
72
73
74
#include "linkansi.h"
#include "unused.h"

#define BLOCKSIZE (65536)

static long sent_writes = 0;
static long sent_requests = 0;
static long sent_bytes = 0;
static long rcvd_reads = 0;
static long rcvd_requests = 0;
static long rcvd_bytes = 0;

static int server;
75

76
77
static int writing_to_server;

78
static long last_progress_status = 0;
79
80
81
static int abort_pending = 0;

static int do_write_only = 0;
82

83
84
85
86
static oop_call_fd read_stdin;
static oop_call_fd read_server;
static oop_call_fd write_server;
static oop_call_time report_stats;
87
static oop_call_time report_progress;
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
static oop_call_time end_it;

static void
fail(const char *reason)
{
    fprintf(stderr, "get-time-often: ");
    perror(reason);
    exit(1);
}


static int
tcp_connect(const char *host,
	    const char *port)
{
    int sock;
    union isc_address *ia;

    if ((ia = isc_mktcpaddress(host, port)) == NULL)
	fail("isc_mktcpaddress");

    if ((sock = socket(isc_addressfamily(ia), SOCK_STREAM, 0)) < 0)
	fail("socket");

    if (connect(sock, isc_addresspointer(ia), isc_addresssize(ia)) < 0)
	fail("connect");

    return sock;
}

static void *
read_stdin(oop_source *UNUSED(source),
120
	   int fd,
121
122
123
	   oop_event UNUSED(event),
	   void *UNUSED(user))
{
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
    static char readbuf[128];
    ssize_t rv;

    if ((rv = read(fd, readbuf, sizeof(readbuf) - 1)) < 0)
    {
	if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR)
	    fail("read(stdin)");
	else
	    rv = 0;
    }
    else if (rv == 0)
    {
	fprintf(stderr, "eof on stdin");
	exit(1);
    }

    readbuf[rv] = '\0';
    while (rv > 1
	   && (readbuf[rv-1] == '\r'
	       || readbuf[rv-1] == '\n'))
    {
	rv--;
	readbuf[rv] = '\0';
    }

    if (!strcmp(readbuf, "shutdown"))
	return OOP_HALT;

    if (!strcmp(readbuf, "ping"))
    {
	puts("pong");
	fflush(stdout);
	return OOP_CONTINUE;
    }

    fprintf(stderr, "unknown command on stdin: %s.\n", readbuf);
    return OOP_HALT;
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
}

static const char *
parse_ok(const char *beg,
	 const char *end)
{
    const char *hunt;

    for (hunt = beg; hunt < end; hunt++)
	if (*hunt == '\n')
	    return hunt + 1;

    return NULL;
}

static const char *
parse_async(const char *beg,
	    const char *end)
{
    const char *hunt;
    const char *number;
    long len;
    
    for (hunt = beg; hunt < end; hunt++)
    {
	if (isdigit((unsigned char)*hunt))
	{
	    number = hunt;
	    for (hunt++; hunt < end; hunt++)
		if (!isdigit((unsigned char)*hunt))
		    break;
	    if (hunt == end)
		return NULL;
	    if (*hunt == 'H')
	    {
		hunt++;
		len = strtol(number, NULL, 10);
		if (len > end - hunt)
		    return NULL;
		hunt += len;
	    }
202
203
	    else
		hunt--;
204
205
206
207
208
209
210
211
212
213
214
215
216
	}
	else if (*hunt == '\n')
	    return hunt + 1;
	else
	{
	    switch (*hunt)
	    {
	    case ' ':
	    case '{':
	    case '}':
	    case '*':
		break;
	    default:
217
218
219
220
221
		fprintf(stderr, "unexpected char from server in async: %c\n",
			*hunt);
		fprintf(stderr, "Current block looks like this (%d chars):\n",
			(int)(end - beg));
		fprintf(stderr, "%.*s\n", (int)(end - beg), beg);
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
		exit(1);
	    }
	}
    }

    return NULL;
}

static const char *
parse_result(const char *beg,
	     const char *end)
{
    const char *next;
    const char *good_seqno = NULL;

    while (beg < end)
    {
	if (*beg == '=')
	{
	    next = parse_ok(beg + 1, end);
	    if (next != NULL)
		good_seqno = beg + 1;
	}
	else if (*beg == ':')
	    next = parse_async(beg+1, end);
	else
	{
	    fprintf(stderr, "unexpected server output: %c\n", *beg);
	    exit(1);
	}
	if (next == NULL)
	    break;
	beg = next;
    }

    if (good_seqno != NULL)
	rcvd_requests = strtol(good_seqno, NULL, 10);

    return beg;
}

static void *
read_server(oop_source *UNUSED(source),
	    int fd,
	    oop_event UNUSED(event),
	    void *UNUSED(user))
{
    static char readbuf[8 * BLOCKSIZE];
    static char *pos = readbuf;
    char *end;
    const char *src;
    ssize_t rv;

    if ((rv = read(fd, pos, sizeof(readbuf) - (pos - readbuf))) < 0)
    {
	if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR)
	    fail("read(server)");
	else
	    rv = 0;
    }
    else if (rv == 0)
    {
	fprintf(stderr, "eof on server");
	exit(1);
    }
    else
    {
	rcvd_bytes += rv;
	rcvd_reads++;
    }

    end = pos + rv;
    src = parse_result(readbuf, end);

    /* Move any trailing incomplete result to the head of the buffe.  */
    for (pos = readbuf; src < end; )
	*pos++ = *src++;

    if (writing_to_server)
	return OOP_CONTINUE;
    if (rcvd_requests < sent_requests)
	return OOP_CONTINUE;
    return OOP_HALT;
}

307
308
309
310
311
312
313
314
315
316
static void
stop_writing(oop_source *oop)
{
    if (writing_to_server)
    {
	oop->cancel_fd(oop, server, OOP_WRITE);
	writing_to_server = 0;
    }
}

317
static void *
318
write_server(oop_source *source,
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
	    int fd,
	    oop_event UNUSED(event),
	    void *UNUSED(user))
{
    static char writebuf[BLOCKSIZE + 100];
    static char *pos = NULL;
    static char *end = NULL;
    ssize_t rv;
    char *hunt;
    long seq = sent_requests;

    /* Fill the output buffer. */
    if (pos == end)
    {
	pos = writebuf;
	while (pos < writebuf + BLOCKSIZE)
	    pos += sprintf(pos, "%ld 35\n", seq++);
	end = pos;
	pos = writebuf;
    }

    rv = write(fd, pos, end - pos);
    if (rv < 0)
    {
343
	if ((errno == ECONNRESET || errno == EPIPE) && do_write_only)
344
345
346
347
	{
	    stop_writing(source);
	    return OOP_CONTINUE;
	}
348
349
350
351
352
353
354
	if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR)
	    fail("write(server)");
	else
	    rv = 0;
    }
    else if (rv == 0)
    {
355
356
	if (do_write_only)
	    return OOP_HALT;
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
	fprintf(stderr, "eof on server");
	exit(1);
    }
    else
    {
	sent_bytes += rv;
	sent_writes++;
    }
    
    pos += rv;
    for (hunt = pos; hunt > writebuf + 1 && hunt[-1] != '\n'; hunt--);
    if (hunt > writebuf + 1)
    {
	for (hunt--; hunt > writebuf + 1 && hunt[-1] != '\n'; hunt--);
	sent_requests = strtol(hunt, NULL, 10);
    }
    
    return OOP_CONTINUE;
}

static void
arm_timer(oop_source *source,
	  time_t sec,
	  long usec,
	  oop_call_time *call,
	  struct timeval *tv)
{
    struct timeval ival;
    ival.tv_sec = sec;
    ival.tv_usec = usec;
387
388
    if (setup_timer(tv, ival) < 0)
	fprintf(stderr, "gettimeofday failed: %s\n", strerror(errno));
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
    source->on_time(source, *tv, call, tv);
}


static void
do_report_stats(void)
{
    printf("%ld reqs, %ld bytes, %ld reads\n",
	   rcvd_requests, rcvd_bytes, rcvd_reads);

    printf("%ld reqs, %ld bytes, %ld writes\n",
	   sent_requests, sent_bytes, sent_writes);

    printf("%ld pending reqs\n\n",
	   sent_requests - rcvd_requests);
}


static void *
report_stats(oop_source *source,
	     struct timeval UNUSED(tv),
	     void *user)
{
    do_report_stats();
    arm_timer(source, 0, 100000, report_stats, user);
    return OOP_CONTINUE;
}

417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
static void *
report_progress(oop_source *source,
		struct timeval UNUSED(tv),
		void *user)
{
    if (rcvd_requests > last_progress_status)
    {
	last_progress_status = rcvd_requests;
	printf("Progress: %ld pending reqs\n", sent_requests - rcvd_requests);
	fflush(stdout);
    }
    arm_timer(source, 2, 0, report_progress, user);
    return OOP_CONTINUE;
}

432
433
434
435
436
static void *
end_it(oop_source *UNUSED(source),
       struct timeval UNUSED(tv),
       void *UNUSED(user))
{
437
    abort_pending = 0;
438
439
440
441
442
443
444
445
446
447
448
449
    return OOP_HALT;
}

static void *
limit_it(oop_source *oop,
	 struct timeval UNUSED(tv),
	 void *UNUSED(user))
{
    stop_writing(oop);
    return OOP_CONTINUE;
}

450
451
452
453
454
static struct option longopts[] = {
    {"verbose", 0, 0, 'v'},
    {"silent", 0, 0, 's'},
    {"time-abort", 0, 0, 'a'},
    {"time-limit", 0, 0, 'l'},
455
    {"write-only", 0, 0, 'w'},
456
457
458
459

    { 0, 0, 0, 0 }
};

460
461
462
463
int
main(int argc,
     char **argv)
{
464
    int optc;
465
466
467
468
    int oldflags;
    ssize_t rv;
    oop_source_sys *sys;
    oop_source *oop;
469
    struct timeval progress_timer;
470
471
472
    struct timeval report_timer;
    struct timeval end_timer;
    char buf[8192];
473
474
    int retry;
    int again;
475
476
477
478
479
480

    /* What should we do? */
    int do_limit_time = 0;
    int do_abort_time = 0;
    int verbose = 0;

481
482
483
484
    if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
	fail("signal");

    while ((optc = getopt_long(argc, argv, "wvalp",
485
			       longopts, (int *) 0)) != EOF)
486
    {
487
488
489
490
491
	switch (optc)
	{
	case 'v':
	    verbose = 1;
	    break;
492

493
494
495
496
497
498
499
500
501
502
503
504
	case 's':
	    verbose = 0;
	    break;

	case 'a':
	    do_abort_time = 1;
	    break;

	case 'l':
	    do_limit_time = 1;
	    break;

505
506
507
508
	case 'w':
	    do_write_only = 1;
	    break;

509
510
511
	case '?':
	    fail("usage");
	}
512
    }
513
514
515

    if (optind + 2 != argc)
	fail("usage");
516
    
517
    server = tcp_connect(argv[optind], argv[optind + 1]);
518
519
520
521
522
523
524
525
526
527
528
529
    puts("connected");
    if (write(server, "A3Hfoo\n", 7) != 7)
	fail("write");
    if ((rv = read(server, buf, sizeof(buf))) < 0)
	fail("read");
    if (rv == 0)
    {
	fprintf(stderr, "unexpected EOF from server");
	exit(1);
    }
    if (rv != 7 || strncmp(buf, "LysKOM\n", 7) != 0)
    {
530
	fprintf(stderr, "Got bad handshake ``%.*s'' from the server\n",
531
		(int)rv, buf);
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
	exit(1);
    }

    puts("handshake OK");

    if ((sys = oop_sys_new()) == NULL)
	fail("oop_sys_new");

    /* Make server connection nonblocking. */
    if ((oldflags = fcntl(server, F_GETFL, 0)) < 0)
	fail("fcntl(F_GETFL)");
    if (fcntl(server, F_SETFL, oldflags|O_NONBLOCK) < 0)
	fail("fcntl(F_SETFL)");

    oop = oop_sys_source(sys);

    oop->on_fd(oop, 0, OOP_READ, read_stdin, NULL);
549
550
    if (!do_write_only)
	oop->on_fd(oop, server, OOP_READ, read_server, NULL);
551
552
553
554
555
556
    oop->on_fd(oop, server, OOP_WRITE, write_server, NULL);
    writing_to_server = 1;

    if (verbose)
	arm_timer(oop, 0, 0, report_stats, &report_timer);

557
558
    arm_timer(oop, 0, 500000, report_progress, &progress_timer);

559
560
561
    if (do_limit_time)
	arm_timer(oop, 5, 0, limit_it, &end_timer);
    else if (do_abort_time)
562
563
    {
	abort_pending = 1;
564
	arm_timer(oop, do_write_only ? 15 : 5, 0, end_it, &end_timer);
565
    }
566
567
568

    oop_sys_run(sys);

569
570
    if (do_write_only)
    {
571
572
	again = 1;
	for (retry = 3; again && retry > 0; retry--)
573
	{
574
	    again = 0;
575
576
577
	    memset(buf, '\n', sizeof(buf));
	    rv = write(server, buf, sizeof(buf));
	    if (rv == -1)
578
579
580
581
582
583
584
585
	    {
		if (retry > 1 && (errno == EAGAIN || errno == EWOULDBLOCK
				  || errno == EINTR))
		{
		    again = 1;
		    sleep(1);
		    break;
		}
586

587
588
589
		/* We expect EPIPE or ECONNRESET. */
		else if (errno != EPIPE && errno != ECONNRESET)
		    fprintf(stderr, "final write: %s\n", strerror(errno));
590
591
	    }
	    if (rv == 0)
592
		fprintf(stderr, "final write returned 0\n");
593

594
595
	    if (rv > 0)
		fprintf(stderr, "ERROR: final write succeeded: %d\n", (int)rv);
596
597
598
	}
    }

599
600
601
    if (verbose)
	oop->cancel_time(oop, report_timer, report_stats, &report_timer);

602
603
604
    if (abort_pending)
	oop->cancel_time(oop, end_timer, end_it, &end_timer);

605
606
607
    printf("End of progress reports\n");
    oop->cancel_time(oop, progress_timer, report_progress, &progress_timer);

608
609
610
    do_report_stats();

    oop->cancel_fd(oop, 0, OOP_READ);
611
612
    if (!do_write_only)
	oop->cancel_fd(oop, server, OOP_READ);
613
614
615
616
617
    stop_writing(oop);
    oop_sys_delete(sys);

    return 0;
}