forked from rolffokkens/bdsync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bdsync.c
2657 lines (2150 loc) · 70.6 KB
/
bdsync.c
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
354
355
356
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
387
388
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// bdsync
//
// This software is released under GPL2. More information can be found in the accompanying
// README and the COPYING file
// (c) Rolf Fokkens <[email protected]>
//
// Revision History:
// * 0.1 Jun 24 2012 Rolf Fokkens <[email protected]>
// - initial package
// * 0.2 Jun 25 2012 Rolf Fokkens <[email protected]>
// - added a man page
// * 0.3 Jun 26 2012 Rolf Fokkens <[email protected]>
// - fixed endianness
// - fixed implicit 64 bit dependencies, can be compiled 32 bit as well
// * 0.6 Jul 14 2013 Rolf Fokkens <[email protected]>
// - asynchronous implementation to handle delay
// * 0.7 Jul 31 2013 Rolf Fokkens <[email protected]>
// - option to generate a checksum for the source data
// - options to choose digests
//
// Brief protocol notes:
//
// Client: msg_hello "CLIENT" PROTOVER
// Server: msg_hello "SERVER" PROTOVER
//
// Client: msg_devfile DEVSIZE DEVFILE
// Server: msg_size DEVSIZE
//
// Client: msg_digests SALTSIZE SALT DIGEST [CHECKSUM]
//
// Client: msg_gethashes
// Server: msg_hashes
//
// Client: msg_getblock
// Server: msg_block
//
// Client: msg_gethashes
// Server: msg_hashes
//
// Client: msg_getchecksum
// Server: msg_checksum
//
// Client: msg_done
// Server: msg_done
//
#define _LARGEFILE64_SOURCE
#define _FILE_OFFSET_BITS 64
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <stdint.h>
#include <ctype.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>
#include <string.h>
#include "bdsync-hash.h"
#include <netdb.h>
#include <sys/socket.h>
#include <stdarg.h>
#include <syslog.h>
#include <errno.h>
#include <poll.h>
#include <sys/wait.h>
#ifdef DBG_MTRACE
# include <mcheck.h>
#endif
#include <malloc.h>
#define RDAHEAD (1024*1024)
/* max total queuing write data */
#define MAXWRQUEUE 131072
/* msg size is stored in an int */
#define MSGMAX 131072
#define ARGMAX 256
#define SALTSIZE 32
#define ARCHVER "BDSYNC 0.3"
#define PROTOVER "0.5"
enum exitcode {
exitcode_success = 0
, exitcode_invalid_params = 1
, exitcode_invalid_patch_format = 2
, exitcode_diffsize_mismatch = 3
, exitcode_protocol_error = 4
, exitcode_checksum_error = 5
, exitcode_read_error = 6
, exitcode_source_randomness_error = 7
, exitcode_process_error = 8
, exitcode_write_error = 9
, exitcode_digest_error = 10
, exitcode_transmission_error = 11
, exitcode_io_error = 12
, exitcode_connection_error = 13
};
enum diffsize {
ds_none = 0
, ds_strict = 1
, ds_resize
, ds_minsize
, ds_mask = 0x7f
, ds_warn = 0x80
};
struct context {
int blockreqs;
off_t stat_size;
int stat_pct;
off_t stat_diffttl;
struct timeval start_tv;
struct timeval progr_tv;
};
struct timeval wall_time;
void update_time ()
{
gettimeofday (&wall_time, NULL);
}
typedef int (*async_handler)(struct context *, unsigned char, char *, size_t);
extern int checkzero (void *p, int len);
void show_usage (FILE *fp)
{
static char *usage =
# include "bdsync.txt.2"
;
fprintf (fp, "%s", usage);
}
void set_blocking(int fd)
{
int val;
if ((val = fcntl(fd, F_GETFL)) == -1)
return;
if (val & O_NONBLOCK) {
val &= ~O_NONBLOCK;
fcntl(fd, F_SETFL, val);
}
};
void set_nonblocking(int fd)
{
int val;
if ((val = fcntl(fd, F_GETFL)) == -1)
return;
if (!(val & O_NONBLOCK)) {
val |= O_NONBLOCK;
fcntl(fd, F_SETFL, val);
}
};
struct msg {
size_t len;
struct msg *pnxt;
char data[];
};
enum qstate {
qhdr = 1
, qdata
, qeof
};
struct wr_queue {
size_t len, maxlen;
struct msg *phd, *ptl;
size_t pos; /* refers to write pos in head msg */
int wr_fd;
char tlen [sizeof (u_int32_t)];
int state;
};
struct rd_queue {
size_t len, maxlen;
struct msg *phd, *ptl, *ptmp;
size_t pos; /* refers to read pos in head msg */
int rd_fd;
char tlen [sizeof (u_int32_t)];
int state;
};
void init_wr_queue (struct wr_queue *pqueue, int wr_fd)
{
pqueue->len = 0;
pqueue->maxlen = 0;
pqueue->phd = NULL;
pqueue->ptl = NULL;
pqueue->pos = 0;
pqueue->wr_fd = wr_fd;
pqueue->state = qhdr;
set_nonblocking (wr_fd);
}
void init_rd_queue (struct rd_queue *pqueue, int rd_fd)
{
pqueue->len = 0;
pqueue->maxlen = 0;
pqueue->phd = NULL;
pqueue->ptl = NULL;
pqueue->ptmp = NULL;
pqueue->pos = 0;
pqueue->rd_fd = rd_fd;
pqueue->state = qhdr;
set_nonblocking (rd_fd);
}
int isverbose = 0;
void (*vhandler) (char *, va_list);
void verbose_syslog (char *format, va_list ap)
{
vsyslog (LOG_INFO, format, ap);
};
void verbose_printf (char *format, va_list ap)
{
vfprintf (stderr, format, ap);
};
void verbose (int level, char * format, ...)
{
va_list args;
if (level > isverbose) return;
va_start (args, format);
vhandler (format, args);
va_end (args);
};
void exitmsg (enum exitcode code, char * format, ...)
{
va_list args;
va_start (args, format);
vhandler (format, args);
va_end (args);
exit (code);
};
void dump_mallinfo (void)
{
struct mallinfo mi;
mi = mallinfo ();
verbose (3, "dump_mallinfo: arena=%d, ordblks=%d, smblks=%d, hblks=%d, hblkhd=%d, usmblks=%d, fsmblks=%d, uordblks=%d, fordblks=%d, keepcost=%d\n"
, mi.arena, mi.ordblks, mi.smblks, mi.hblks, mi.hblkhd, mi.usmblks, mi.fsmblks, mi.uordblks, mi.fordblks, mi.keepcost);
};
void cleanup_wr_queue (struct wr_queue *pqueue)
{
verbose (3, "cleanup_wr_queue: len=%ld, maxlen=%ld\n", (long)(pqueue->len), (long)(pqueue->maxlen));
};
void cleanup_rd_queue (struct rd_queue *pqueue)
{
verbose (3, "cleanup_rd_queue: len=%ld, maxlen=%ld\n", (long)(pqueue->len), (long)(pqueue->maxlen));
};
struct zero_hash {
char *name;
off_t blocksize;
int hashsize;
unsigned char *hash;
struct zero_hash *pnxt;
};
struct cs_state {
char *name;
off_t nxtpos;
hash_ctx ctx;
int hashsize;
};
hash_ctx _init_cs (const char *checksum, int *hs)
{
hash_alg md;
hash_ctx ctx;
md = hash_getbyname (checksum);
if (!md) {
verbose (0, "Bad checksum %s\n", checksum);
exit (exitcode_checksum_error);
}
*hs = hash_getsize (md);
hash_init (ctx, md);
return ctx;
}
static struct zero_hash *pzeroes = NULL;
static off_t zeroblocksize = 0;
static unsigned char *zeroblock = NULL;
static struct zero_hash *find_zero_hash (const char *name, off_t blocksize, int saltsize, unsigned char *salt)
{
struct zero_hash *pzh = pzeroes;
hash_ctx ctx;
int hs;
verbose (3, "find_zero_hash: name=%s: blocksize=%lld, salt=%p\n", name, blocksize, salt);
while (pzh) {
if (!strcmp (pzh->name, name) && pzh->blocksize == blocksize) return pzh;
pzh = pzh->pnxt;
}
if (blocksize > zeroblocksize) {
if (zeroblock) free (zeroblock);
zeroblock = calloc (1, blocksize);
zeroblocksize = blocksize;
}
ctx = _init_cs (name, &hs);
pzh = pzeroes;
pzeroes = malloc (sizeof (struct zero_hash));
pzeroes->pnxt = pzh;
pzeroes->name = strdup (name);
pzeroes->blocksize = blocksize;
pzeroes->hashsize = hs;
pzeroes->hash = malloc (hs);
hash_update (ctx, salt, saltsize);
hash_update (ctx, zeroblock, blocksize);
hash_finish (ctx, pzeroes->hash);
return pzeroes;
}
struct cs_state *init_checksum (const char *checksum)
{
hash_ctx ctx;
int hs;
struct cs_state *state;
verbose (2, "init_checksum: checksum: %s\n", checksum);
ctx = _init_cs (checksum, &hs);
state = malloc (sizeof (*state));
state->nxtpos = 0;
state->ctx = ctx;
state->hashsize = hs;
state->name = strdup (checksum);
return state;
}
struct dev {
int fd;
off_t size;
off_t relpos; /* data released from cache until pos */
int flush; /* flush each block from the buffer cache */
};
int vpread (struct dev *devp, void *buf, off_t len, off_t pos)
{
off_t rlen = len;
int ret = 0;
char *cbuf = (char *)buf;
if (pos + rlen > devp->size) {
rlen = devp->size - pos;
if (rlen < 0) rlen = 0;
}
if (rlen) {
if (devp->relpos > pos) {
verbose (0, "vpread: pos < relpos: relpos=%lld, pos=%lld\n", (long long)devp->relpos, (long long) pos);
}
ret = pread (devp->fd, cbuf, rlen, pos);
if (ret < 0) exitmsg (exitcode_read_error, "vpread: %s\n", strerror (errno));
if (devp->flush) {
// Use fadvise to release buffer/cache
posix_fadvise (devp->fd, pos, rlen, POSIX_FADV_DONTNEED);
verbose (3, "posix_fadvise: start=%lld end=%lld\n", (long long)pos, (long long)pos+rlen);
}
}
if (rlen < len) memset (cbuf + rlen, 0, len - rlen);
return ret;
}
int handle_err (int fd)
{
# define EBUFSIZ 1024
static char rbuf[EBUFSIZ], ebuf[EBUFSIZ];
static char *ep = ebuf;
ssize_t sz;
while ((sz = read (fd, rbuf, EBUFSIZ)) > 0) {
char *rp = rbuf;
while (sz) {
if (*rp == '\n' || ep == ebuf + EBUFSIZ) {
fprintf (stderr, "RMTERR: ");
fwrite (ebuf, 1, ep - ebuf, stderr);
fprintf (stderr, "\n");
/* if (ep == ebuf + EBUFSIZ) ep = ebuf; */
ep = ebuf;
}
if (!isprint(*rp)) {
rp++;
sz--;
continue;
}
*ep++ = *rp++;
sz--;
}
}
return 0;
}
int update_checksum (struct cs_state *state, off_t pos, struct dev *devp, off_t len, unsigned char *buf)
{
if (!state) return 0;
verbose (3, "update_checksum: checksum: pos=%lld, len=%d\n"
, (long long) state->nxtpos, len);
if (pos > state->nxtpos) {
size_t nrd ;
off_t len = pos - state->nxtpos;
size_t blen = (len > 32768 ? 32768 : len);
char *fbuf = malloc (blen);
while (len) {
verbose (3, "update_checksum: checksum: pos=%lld, len=%d\n"
, (long long) state->nxtpos, blen);
nrd = vpread (devp, fbuf, blen, state->nxtpos);
if (nrd != blen) {
exitmsg (exitcode_checksum_error
, "update_checksum: nrd (%lld) != blen (%d)\n"
, (long long)nrd, (int)blen);
}
hash_update (state->ctx, fbuf, blen);
state->nxtpos += blen;
len -= blen;
}
free (fbuf);
}
if (pos < state->nxtpos) {
len -= (state->nxtpos - pos);
pos = state->nxtpos;
}
if (len <= 0) return 0;
hash_update (state->ctx, buf, len);
state->nxtpos += len;
return 0;
};
int fd_pair(int fd[2])
{
int ret;
ret = socketpair(AF_UNIX, SOCK_STREAM, 0, fd);
// ret = pipe(fd);
return ret;
};
int init_salt (int saltsize, unsigned char *salt, int fixedsalt)
{
if (fixedsalt) {
memset (salt, 0, saltsize);
} else {
int fd = open("/dev/urandom", O_RDONLY);
if (read (fd, salt, saltsize) != saltsize) {
exitmsg (exitcode_source_randomness_error, "piped_child: %s\n", strerror (errno));
}
close(fd);
}
return 0;
};
pid_t piped_child(char **command, int *f_in, int *f_out, int *f_err)
{
pid_t pid;
int child_stdin[2];
int child_stdout[2];
int child_stderr[2];
verbose (2, "opening connection using: %s\n", command[0]);
if (fd_pair(child_stdin) < 0 || fd_pair(child_stdout) < 0 || fd_pair(child_stderr) < 0) {
exitmsg (exitcode_process_error, "piped_child: %s\n", strerror (errno));
}
pid = fork();
if (pid == -1) {
exitmsg (exitcode_process_error, "piped_child: fork: %s\n", strerror (errno));
}
if (pid == 0) {
if (dup2(child_stdin[0], STDIN_FILENO) < 0 ||
dup2(child_stdout[1], STDOUT_FILENO) < 0 ||
dup2(child_stderr[1], STDERR_FILENO) < 0 ||
close(child_stdin[1]) < 0 ||
close(child_stdout[0]) < 0 ||
close(child_stderr[0]) < 0) {
exitmsg (exitcode_process_error, "piped_child: dup2: %s\n", strerror (errno));
}
if (child_stdin[0] != STDIN_FILENO)
close(child_stdin[0]);
if (child_stdout[1] != STDOUT_FILENO)
close(child_stdout[1]);
if (child_stderr[1] != STDERR_FILENO)
close(child_stderr[1]);
// umask(orig_umask);
set_blocking(STDIN_FILENO);
set_blocking(STDOUT_FILENO);
set_blocking(STDERR_FILENO);
execvp(command[0], command);
exitmsg (exitcode_process_error, "piped_child: execvp: %s\n", strerror (errno));
}
if (close(child_stdout[1]) < 0 || close(child_stderr[1]) < 0 || close(child_stdin[0]) < 0) {
exitmsg (exitcode_process_error, "piped_child: close: %s\n", strerror (errno));
}
set_nonblocking (child_stderr[0]);
*f_in = child_stdout[0];
*f_out = child_stdin[1];
*f_err = child_stderr[0];
return pid;
};
pid_t do_command (char *command, struct rd_queue *prd_queue, struct wr_queue *pwr_queue, int *fd_err)
{
int argc = 0;
char *t, *f, *args[ARGMAX];
pid_t pid;
int in_quote = 0;
int f_in, f_out;
command = strdup (command);
for (t = f = command; *f; f++) {
if (*f == ' ') continue;
/* Comparison leaves rooms for server_options(). */
if (argc >= ARGMAX - 1) {
exitmsg (exitcode_invalid_params, "internal: args[] overflowed in do_command()\n");
}
args[argc++] = t;
while (*f != ' ' || in_quote) {
if (!*f) {
if (in_quote) {
exitmsg (exitcode_invalid_params
, "Missing trailing-%c in remote-shell command.\n"
, in_quote);
}
f--;
break;
}
if (*f == '\'' || *f == '"') {
if (!in_quote) {
in_quote = *f++;
continue;
}
if (*f == in_quote && *++f != in_quote) {
in_quote = '\0';
continue;
}
}
*t++ = *f++;
}
*t++ = '\0';
}
args[argc] = NULL;
pid = piped_child (args, &f_in, &f_out, fd_err);
free (command);
init_rd_queue (prd_queue, f_in);
init_wr_queue (pwr_queue, f_out);
return pid;
};
char *int2char (char *buf, off_t val, int bytes)
{
char *p;
for (p = buf; bytes; bytes--) {
*p++ = val & 0xff;
val >>= 8;
}
return buf;
}
off_t char2int (char *buf, int bytes)
{
off_t ret = 0;
unsigned char *p = (unsigned char *)buf + bytes;
for (; bytes; bytes--) {
ret <<= 8;
ret |= *--p;
}
return ret;
}
enum messages {
msg_none = 0
, msg_hello
, msg_devfile
, msg_size
, msg_digests
, msg_gethashes
, msg_hashes
, msg_done
, msg_getblock
, msg_block
, msg_getchecksum
, msg_checksum
, msg_max
};
enum hint {
hint_flushcache = 0
, hint_relpos
, hint_max
};
static char *msgstring[] = {
""
, "hello"
, "devfile"
, "size"
, "digest"
, "gethashes"
, "hashes"
, "done"
, "getblock"
, "block"
, "getchecksum"
, "checksum"
};
static char *hintstring[] = {
"flushcache"
, "relpos"
};
int msg_write (int fd, unsigned char token, char *buf, size_t len)
{
u_int32_t tmp = len + 1;
char tbuf[sizeof (tmp)];
verbose (2, "msg_write: msg = %s, len = %d\n", msgstring[token], len);
if (write (fd, int2char (tbuf, tmp, sizeof (tmp)), sizeof (tmp)) != sizeof (tmp)) {
exit (exitcode_write_error);
}
if (write (fd, &token, sizeof (token)) != sizeof (token)) {
exit (exitcode_write_error);
}
if (write (fd, buf, len) != len) {
exit (exitcode_write_error);
}
return 0;
}
int add_wr_queue (struct wr_queue *pqueue, unsigned char token, char *buf, size_t len)
{
struct msg *pmsg;
verbose (2, "add_wr_queue: msg = %s, len = %d\n", msgstring[token], len);
if (pqueue->state == qeof) {
verbose (0, "add_wr_queue: EOF\n");
exit (exitcode_write_error);
}
pmsg = (struct msg *) malloc (sizeof (struct msg) + len + 1);
pmsg->len = len + 1;
pmsg->data[0] = token;
if (len) memcpy (pmsg->data + 1, buf, len);
pmsg->pnxt = NULL;
if (pqueue->ptl) {
pqueue->ptl->pnxt = pmsg;
} else {
pqueue->phd = pmsg;
}
pqueue->ptl = pmsg;
pqueue->len += (sizeof (pqueue->tlen) + len + 1);
if (pqueue->len > pqueue->maxlen) pqueue->maxlen = pqueue->len;
return 0;
}
int flush_wr_queue (struct wr_queue *pqueue, int wait)
{
size_t retval = 0, len, tmp;
struct msg *phd;
char *pwr;
verbose (3, "flush_wr_queue: wait = %d, len = %lld\n", wait, (long long)pqueue->len);
while ((phd = pqueue->phd) != NULL) {
if (pqueue->state == qhdr) {
len = sizeof (pqueue->tlen) - pqueue->pos;
if (len == 0) {
pqueue->state = qdata;
pqueue->pos = 0;
continue;
}
if (pqueue->pos == 0) {
int2char (pqueue->tlen, phd->len, sizeof (pqueue->tlen));
}
pwr = pqueue->tlen + pqueue->pos;
} else {
len = phd->len - pqueue->pos;
if (len == 0) {
verbose (3, "flush_wr_queue: msg = %s, len = %d\n", msgstring[(unsigned char)(phd->data[0])], phd->len - 1);
pqueue->state = qhdr;
pqueue->pos = 0;
pqueue->phd = phd->pnxt;
if (phd->pnxt == NULL) pqueue->ptl = NULL;
free (phd);
continue;
}
pwr = phd->data + pqueue->pos;
}
if (wait) {
struct pollfd pfd;
pfd.fd = pqueue->wr_fd;
pfd.events = POLLOUT;
tmp = poll (&pfd, 1, -1);
if (pfd.revents) {
if (!(pfd.revents & POLLOUT)) {
verbose (0, "flush_wr_queue: poll error on fd %d\n", pfd.fd);
exit (exitcode_write_error);
}
}
}
tmp = write (pqueue->wr_fd, pwr, len);
if (tmp == 0) break;
if (tmp == -1) {
if (errno == EAGAIN || errno == EWOULDBLOCK) break;
verbose (0, "flush_wr_queue: %s\n", strerror (errno));
exit (exitcode_write_error);
}
retval += tmp;
pqueue->pos += tmp;
verbose (3, "flush_wr_queue: len = %lld\n", (long long int)(pqueue->len - retval));
}
pqueue->len -= retval;
return retval;
}
int fill_rd_queue (struct context *ctx, struct rd_queue *pqueue, async_handler handler)
{
char *prd;
size_t retval = 0, addlen = 0, len, tmp;
verbose (3, "fill_rd_queue: len = %lld\n", (long long)pqueue->len);
while (pqueue->state != qeof) {
if (pqueue->state == qhdr) {
len = sizeof (pqueue->tlen) - pqueue->pos;
if (len == 0) {
struct msg *pmsg;
len = char2int ((char *)pqueue->tlen, sizeof (pqueue->tlen));
if (len > MSGMAX) {
verbose (0, "fill_rd_queue: bad msg size %d\n", (int)len);
exit (exitcode_read_error);
}
pmsg = malloc (sizeof (struct msg) + len + 1);
pmsg->pnxt = NULL;
pmsg->len = len;
pqueue->ptmp = pmsg;
pqueue->state = qdata;
pqueue->pos = 0;
continue;
}
prd = (char *)(pqueue->tlen) + pqueue->pos;
} else {
struct msg *pmsg = pqueue->ptmp;
len = pmsg->len - pqueue->pos;
if (len == 0) {
/* Full message present */
verbose (3, "fill_rd_queue: msg = %s, len = %d\n", msgstring[(unsigned char)(pmsg->data[0])], (int)pmsg->len - 1);
pqueue->state = qhdr;
pqueue->pos = 0;
pqueue->ptmp = NULL;
if (handler) {
unsigned char token = pmsg->data[0];
char *msg = pmsg->data + 1;
size_t msglen = pmsg->len - 1;
if (handler (ctx, token, msg, msglen)) {
/* Handled, no need to queue it */
addlen -= (sizeof (pqueue->tlen) + pmsg->len);
retval ++;
free (pmsg);
continue;
}
}
if (pqueue->ptl) {
pqueue->ptl->pnxt = pmsg;
} else {
pqueue->phd = pmsg;
}
pqueue->ptl = pmsg;
continue;
}
prd = pmsg->data + pqueue->pos;
}
tmp = read (pqueue->rd_fd, prd, len);
if (tmp == 0) {
/* When reading the header at pos 0 it's OK */
if (pqueue->state == qhdr && pqueue->pos == 0) {
pqueue->state = qeof;
verbose (3, "fill_rd_queue: eof\n");
continue;
}
verbose (0, "fill_rd_queue: EOF\n");
exit (exitcode_read_error);
}
if (tmp == -1) {
if (errno == EAGAIN || errno == EWOULDBLOCK) break;
verbose (0, "fill_rd_queue: %s\n", strerror (errno));
exit (exitcode_read_error);
}
pqueue->pos += tmp;
addlen += tmp;
verbose (3, "fill_rd_queue: len = %lld\n", (long long)(pqueue->len + addlen));
}
pqueue->len += addlen;
if (pqueue->len > pqueue->maxlen) pqueue->maxlen = pqueue->len;
return retval;
}
struct timeval get_rd_wait = {0, 0};
int get_rd_queue (struct context *ctx, struct wr_queue *pwr_queue, struct rd_queue *prd_queue, int fd_err, unsigned char *token, char **msg, size_t *msglen, async_handler handler)
{
struct pollfd pfd[3];
struct msg *phd;
int tmp, nfd;
struct timeval tv1, tv2;
int async_cnt = 0;
verbose (3, "get_rd_queue: handler = %d\n", (handler != NULL));
update_time ();
tv1 = wall_time;
while ( prd_queue->state != qeof
&& !async_cnt
&& (prd_queue->state != qhdr || prd_queue->phd == NULL)) {
pfd[0].fd = prd_queue->rd_fd;
pfd[0].events = POLLIN;
pfd[1].fd = pwr_queue->wr_fd;
pfd[1].events = (pwr_queue->phd ? POLLOUT: 0);
pfd[2].fd = fd_err;
pfd[2].events = POLLIN;
nfd = (pwr_queue->state == qeof ? 1 : 2);
tmp = poll (pfd, nfd, -1);
verbose (3, "get_rd_queue: poll %d\n", tmp);
if (pfd[0].revents & POLLIN) async_cnt += fill_rd_queue (ctx, prd_queue, handler);
if (pfd[1].revents) {
if (pfd[1].revents & POLLOUT) {
flush_wr_queue (pwr_queue, 0);
} else {
if (pfd[1].revents & POLLHUP) {
if (pwr_queue->phd == NULL) {
pwr_queue->state = qeof;
verbose (3, "get_rd_queue: poll pollhup\n");
} else {
verbose (0, "get_rd_queue: poll POLLHUP\n");
exit (exitcode_read_error);
}
} else {
verbose (0, "get_rd_queue: poll error on fd %d\n", pfd[1].fd);
exit (exitcode_read_error);
}
}
}
if (pfd[2].revents & POLLIN) handle_err (fd_err);
}
update_time ();
tv2 = wall_time;
tv2.tv_sec -= tv1.tv_sec;
tv2.tv_usec -= tv1.tv_usec;
if (tv2.tv_usec < 0) {
tv2.tv_usec += 1000000;
tv2.tv_sec -= 1;
}
verbose (2, "get_rd_queue: wait = %d.%06d\n", tv2.tv_sec, tv2.tv_usec);
get_rd_wait.tv_sec += tv2.tv_sec;
get_rd_wait.tv_usec += tv2.tv_usec;
if (get_rd_wait.tv_usec >= 1000000) {
get_rd_wait.tv_usec -= 1000000;
get_rd_wait.tv_sec += 1;
}
phd = prd_queue->phd;
if (phd == NULL) {
if (async_cnt) {
*token = msg_none;
*msg = NULL;
verbose (2, "get_rd_queue: msg = -, len = 0\n");
return 0;
}
verbose (0, "get_rd_queue: EOF %d\n", pfd[1].fd);
handle_err (fd_err);
exit (exitcode_read_error);
}
*token = phd->data[0];
*msg = (char *)malloc (phd->len);
*msglen = phd->len - 1;
memcpy (*msg, phd->data + 1, phd->len - 1);
prd_queue->len -= (sizeof (prd_queue->tlen) + phd->len);
prd_queue->phd = phd->pnxt;
if (phd->pnxt == NULL) prd_queue->ptl = NULL;
free (phd);
verbose (2, "get_rd_queue: msg = %s, len = %d\n", msgstring[*token], *msglen);
return 1;
}
int send_msgstring (struct wr_queue *pqueue, int msg, char *str)
{