-
Notifications
You must be signed in to change notification settings - Fork 10
/
communications.c
1298 lines (1070 loc) · 33 KB
/
communications.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
//
// Copyright (C) 2001,2002,2003,2004,2005,2006,2007 Jorge Daza Garcia-Blanes
//
// This file is part of DrQueue
//
// This program 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 2 of the License, or
// (at your option) any later version.
//
// This program 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 this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
// USA
//
#include <stdio.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifndef _WIN32
#include <netdb.h>
#else
#include <winsock2.h>
#define socklen_t int
#define ssize_t int
#endif
#include "pointer.h"
#include "communications.h"
#include "database.h"
#include "semaphores.h"
#include "logger.h"
#include "job.h"
#include "drerrno.h"
#include "task.h"
#include "computer_pool.h"
#include "computer.h"
#include "common.h"
#ifdef LIBWRAP
#include <tcpd.h>
#include <syslog.h>
int allow_severity = LOG_INFO;
int deny_severity = LOG_WARNING;
#endif /* LIBWRAP */
#ifdef COMM_REPORT
long int bsent; /* Bytes sent */
long int brecv; /* Bytes received */
#endif
/* Forward Declares */
static ssize_t dr_socket_write (int fd, char *buf, uint32_t len);
static ssize_t dr_socket_read (int fd, char *buf, uint32_t len);
// ONGOING:
// * data size transmission
// * log messages moved to auto
// * perror messagges moved to log_auto
// * anydata_init_received
// * anydata_init_sending
int
get_socket (uint16_t port) {
int sfd;
struct sockaddr_in addr;
int opt = 1;
sfd = socket (PF_INET,SOCK_STREAM,0);
if (sfd == -1) {
drerrno_system = errno;
log_auto (L_ERROR,"get_socket(): call to socket() could not create stream socket. Msg: %s",strerror(drerrno_system));
return sfd;
//kill (0,SIGINT);
} else {
#ifdef _WIN32
if (setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,(char *)&opt,sizeof(opt)) == -1) {
#else
if (setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,(void *)&opt,sizeof(opt)) == -1) {
#endif
drerrno_system = errno;
log_auto (L_ERROR,"get_socket(): call to setsockopt() failed. Msg: %s",strerror(drerrno_system));
}
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind (sfd,(struct sockaddr *)&addr,sizeof (addr)) == -1) {
drerrno_system = errno;
log_auto (L_ERROR,"get_socket(): call to bind() failed. Msg: %s",strerror(drerrno_system));
sfd = -1;
} else {
listen (sfd,MAXLISTEN);
}
}
return sfd;
}
// FIXME: should be named accept_socket_master
int
accept_socket (int sfd, struct database *wdb, struct sockaddr_in *addr) {
int fd;
socklen_t len;
// fix compiler warning
(void)wdb;
// FIXME: use wdb variable
len = sizeof (struct sockaddr_in);
if ((fd = accept (sfd,(struct sockaddr *)addr,&len)) == -1) {
drerrno_system = errno;
log_auto (L_ERROR,"accept_socket(): error accepting connection. Msg: %s",strerror(drerrno_system));
return fd;
}
#ifdef LIBWRAP
/* Check whether logins are denied from this host. */
{
struct request_info req;
request_init(&req, RQ_DAEMON, "drqueue-master", RQ_FILE, fd, 0);
fromhost(&req);
if (!hosts_access(&req)) {
close(fd);
log_master (L_WARNING,"Connection refused by tcp wrapper.");
return (-1);
}
}
#endif /* LIBWRAP */
return fd;
}
int
accept_socket_slave (int sfd) {
int fd;
struct sockaddr_in addr;
socklen_t len = sizeof (struct sockaddr_in);
if ((fd = accept (sfd,(struct sockaddr *)&addr,&len)) == -1) {
drerrno_system = errno;
log_auto (L_ERROR,"accept_socket_slave(): error accepting connection. (%s)",strerror(drerrno_system));
drerrno = DRE_COMMPROBLEM;
return fd;
}
#ifdef LIBWRAP
/* Check whether logins are denied from this host. */
{
struct request_info req;
request_init(&req, RQ_DAEMON, "drqueue-slave", RQ_FILE, fd, 0);
fromhost(&req);
if (!hosts_access(&req)) {
close(fd);
log_slave_computer (L_WARNING,"Connection refused by tcp wrapper.");
return (-1);
}
}
#endif /* LIBWRAP */
return fd;
}
int
connect_to_master (void) {
/* To be used by a slave ! */
/* Connects to the master and returns the socket fd */
/* or -1 in case of error */
int sfd;
char *master;
struct sockaddr_in addr;
struct hostent *hostinfo;
drerrno = DRE_NOERROR;
if ((master = getenv ("DRQUEUE_MASTER")) == NULL) {
drerrno_system = errno;
drerrno = DRE_NOENVMASTER;
return -1;
}
addr.sin_family = AF_INET;
addr.sin_port = htons(MASTERPORT); /* Whatever */
hostinfo = gethostbyname (master);
/* check if ip address of host could be resolved */
if ( (hostinfo == NULL) || (hostinfo->h_addr == NULL) ) {
drerrno_system = errno;
drerrno = DRE_NOTRESOLVE;
return -1;
}
addr.sin_addr = *(struct in_addr *) hostinfo->h_addr;
sfd = socket (PF_INET,SOCK_STREAM,0);
if (sfd == -1) {
drerrno_system = errno;
drerrno = DRE_NOSOCKET;
return -1;
}
if (connect (sfd,(struct sockaddr *)&addr,sizeof (addr)) == -1) {
drerrno_system = errno;
drerrno = DRE_NOCONNECT;
return -1;
}
return sfd;
}
int
connect_to_slave (char *slave) {
/* Connects to the slave and returns the socket fd */
int sfd;
struct sockaddr_in addr;
struct hostent *hostinfo;
addr.sin_family = AF_INET;
addr.sin_port = htons(SLAVEPORT); /* Whatever */
hostinfo = gethostbyname (slave);
if (hostinfo == NULL) {
drerrno_system = errno;
drerrno = DRE_NOTRESOLVE;
return -1;
}
addr.sin_addr = *(struct in_addr *) hostinfo->h_addr;
sfd = socket (PF_INET,SOCK_STREAM,0);
if (sfd == -1) {
drerrno_system = errno;
drerrno = DRE_NOSOCKET;
return -1;
}
if (connect (sfd,(struct sockaddr *)&addr,sizeof (addr)) == -1) {
drerrno_system = errno;
drerrno = DRE_NOCONNECT;
return -1;
}
return sfd;
}
int
check_recv_datasize (int sfd, uint32_t datasize) {
uint32_t remotesize = 0;
uint32_t localsize;
if (!dr_socket_read(sfd,(char*)&remotesize,sizeof(uint32_t))) {
log_auto (L_ERROR,"check_recv_datasize(): communications problem. Could not read from socket or file. (%s)",strerror(errno));
return 0;
}
remotesize = ntohl(remotesize);
localsize = htonl(datasize);
if (!dr_socket_write(sfd,(char*)&localsize,sizeof(uint32_t))) {
log_auto (L_ERROR,"check_recv_datasize(): communications problem. Could not write to socket or file. (%s)",strerror(errno));
return 0;
}
if (datasize != remotesize) {
log_auto (L_ERROR,"check_recv_datasize(): remote and local sizes for data are different.");
log_auto (L_ERROR,"check_recv_datasize(): if you are using different versions of the software please, update older ones.");
log_auto (L_ERROR,"check_recv_datasize(): if not this is probably a BUG. Check if it's has been reported.");
return 0;
}
return 1;
}
int
check_send_datasize (int sfd, uint32_t datasize) {
uint32_t remotesize = 0;
uint32_t localsize;
drerrno = DRE_NOERROR;
log_auto(L_DEBUG3,"check_send_datasize():> Entering...");
localsize = htonl(datasize);
if (!dr_socket_write(sfd,(char*)&localsize,sizeof(uint32_t))) {
log_auto (L_ERROR,"check_send_datasize(): communications problem. Could not write to socket or file. (%s)",strerror(errno));
return 0;
}
if (!dr_socket_read(sfd,(char*)&remotesize,sizeof(uint32_t))) {
log_auto (L_ERROR,"check_send_datasize(): communications problem. Could not read from socket or file. (%s)",strerror(errno));
return 0;
}
remotesize = ntohl(remotesize);
if (datasize != remotesize) {
log_auto (L_ERROR,"check_send_datasize(): remote and local sizes for data are different.");
log_auto (L_ERROR,"check_send_datasize(): if you are using different versions of the software please, update older ones.");
log_auto (L_ERROR,"check_send_datasize(): if not this is probably a BUG. Check if it's has been reported.");
return 0;
}
log_auto(L_DEBUG3,"check_send_datasize():< Returning...");
return 1;
}
int
recv_computer_hwinfo (int sfd, struct computer_hwinfo *hwinfo) {
void *buf;
uint32_t datasize;
datasize = sizeof(struct computer_hwinfo);
if (!check_recv_datasize(sfd, datasize)) {
log_auto (L_ERROR,"recv_computer_hwinfo(): different data sizes for 'struct computer_hwinfo'.");
return 0;
}
buf = hwinfo;
if (!dr_socket_read (sfd,(char*)buf,datasize)) {
log_auto (L_ERROR,"recv_computer_hwinfo(): could not read computer hardware information. (%s)",strerror(errno));
return 0;
}
/* Now we should have the computer hardware info with the values in */
/* network byte order, so we put them in host byte order */
hwinfo->id = ntohl (hwinfo->id);
hwinfo->procspeed = ntohl (hwinfo->procspeed);
hwinfo->ncpus = ntohs (hwinfo->ncpus);
hwinfo->speedindex = ntohl (hwinfo->speedindex);
hwinfo->memory = ntohl (hwinfo->memory);
drerrno = DRE_NOERROR;
return 1;
}
int
send_computer_hwinfo (int sfd, struct computer_hwinfo *hwinfo) {
struct computer_hwinfo bswapped;
void *buf = &bswapped;
uint32_t datasize;
datasize = sizeof(bswapped);
if (!check_send_datasize(sfd,datasize)) {
log_auto (L_ERROR,"send_computer_hwinfo(): different data sizes for struct computer_hwinfo.");
return 0;
}
/* We make a copy coz we need to modify the values */
memcpy (buf,hwinfo,sizeof(bswapped));
/* Prepare for sending */
bswapped.id = htonl (bswapped.id);
bswapped.procspeed = htonl (bswapped.procspeed);
bswapped.ncpus = htons (bswapped.ncpus);
bswapped.speedindex = htonl (bswapped.speedindex);
bswapped.memory = htonl (bswapped.memory);
if (!dr_socket_write (sfd, (char *) buf, datasize)) {
return 0;
}
drerrno = DRE_NOERROR;
return 1;
}
int
recv_request (int sfd, struct request *request) {
/* Returns 0 on failure */
void *buf = request;
if (!dr_socket_read(sfd,(char *)buf,sizeof(struct request))) {
return 0;
}
/* Byte order ! */
request->data = ntohl (request->data);
return 1;
}
int send_request (int sfd, struct request *request, uint8_t who) {
void *buf = request;
request->data = htonl (request->data);
request->who = who;
if (!dr_socket_write (sfd,(char*)buf,sizeof(struct request))) {
return 0;
}
drerrno = DRE_NOERROR;
return 1;
}
int
send_computer_status (int sfd, struct computer_status *status) {
uint16_t i;
uint16_t ntasks;
uint16_t nrunning;
/* Count the tasks. (Shouldn't be necessary) */
// FIXME: Remove this ?
ntasks = 0;
nrunning = 0;
for (i=0;i<MAXTASKS;i++) {
if (status->task[i].used) {
ntasks++;
if (status->task[i].status != TASKSTATUS_FINISHED) {
nrunning++;
}
}
}
status->ntasks = ntasks;
status->nrunning = nrunning;
if (!send_computer_loadavg(sfd,status)) {
return 0;
}
if (!send_computer_ntasks(sfd,status)) {
return 0;
}
/* We just send the used tasks */
for (i=0;i<MAXTASKS;i++) {
if (status->task[i].used) {
if (!send_task(sfd,&status->task[i]))
return 0;
}
}
return 1;
}
int
send_computer_loadavg (int sfd, struct computer_status *status) {
uint16_t nbo_loadavg[3]; // network byte ordered
nbo_loadavg[0] = htons(status->loadavg[0]);
nbo_loadavg[1] = htons(status->loadavg[1]);
nbo_loadavg[2] = htons(status->loadavg[2]);
if (!dr_socket_write(sfd,(char*)nbo_loadavg,sizeof(uint16_t)*3)) {
return 0;
}
return 1;
}
int recv_computer_loadavg (int sfd, struct computer_status *status) {
uint16_t nbo_loadavg[3];
if (!dr_socket_read(sfd,(char*)nbo_loadavg,sizeof(uint16_t)*3)) {
return 0;
}
status->loadavg[0] = ntohs(nbo_loadavg[0]);
status->loadavg[1] = ntohs(nbo_loadavg[1]);
status->loadavg[2] = ntohs(nbo_loadavg[2]);
return 1;
}
int
recv_computer_ntasks (int sfd, struct computer_status *status) {
uint16_t ntasks;
uint16_t nrunning;
if (!dr_socket_read(sfd,(char*)&ntasks,sizeof(uint16_t))) {
return 0;
}
if (!dr_socket_read(sfd,(char*)&nrunning,sizeof(uint16_t))) {
return 0;
}
status->ntasks = ntohs(ntasks);
status->nrunning = ntohs(nrunning);
return 1;
}
int
send_computer_ntasks (int sfd, struct computer_status *status) {
uint16_t nbo_ntasks; // network byte ordered
uint16_t nbo_nrunning; // network byte ordered
nbo_ntasks = htons(status->ntasks);
nbo_nrunning = htons(status->nrunning);
if (!dr_socket_write(sfd,(char*)&nbo_ntasks,sizeof(uint16_t))) {
return 0;
}
if (!dr_socket_write(sfd,(char*)&nbo_nrunning,sizeof(uint16_t))) {
return 0;
}
return 1;
}
int
recv_computer_status (int sfd, struct computer_status *status) {
void *buf;
int i;
struct task task;
computer_status_init (status);
buf = status;
if (!recv_computer_loadavg (sfd,status)) {
drerrno = DRE_ERRORREADING;
fprintf (stderr,"ERROR: recv_computer_loadavg: %s\n",drerrno_str());
}
if (!recv_computer_ntasks (sfd,status)) {
drerrno = DRE_ERRORREADING;
fprintf (stderr,"ERROR: recv_computer_ntasks: %s\n",drerrno_str());
}
if (status->ntasks > MAXTASKS) {
fprintf (stderr,"WARNING: ntasks > MAXTASKS (%i)\n",status->ntasks);
status->ntasks = 0;
}
for (i=0;i<status->ntasks;i++) {
if (!recv_task(sfd,&task)) {
fprintf (stderr,"ERROR: receiving task\n");
return 0;
}
memcpy(&status->task[task.itask],&task,sizeof(task));
}
return 1;
}
int
recv_envvar (int sfd, struct envvar *var, int do_checksize) {
// receives a single environment variable structure
uint32_t datasize;
datasize = sizeof (struct envvar);
if (do_checksize && !check_recv_datasize(sfd, datasize)) {
// FIXME: log it
return 0;
}
if (!dr_socket_read (sfd,(char *)var, datasize)) {
drerrno = DRE_ERRORREADING;
return 0;
}
drerrno = DRE_NOERROR;
return 1;
}
int
send_envvar (int sfd, struct envvar *var, int do_checksize) {
// sends a single environment variable structure
uint32_t datasize;
datasize = sizeof (struct envvar);
if (do_checksize && !check_send_datasize(sfd,datasize)) {
// FIXME: log it
return 0;
}
if (!dr_socket_write (sfd,(char *)var, datasize)) {
// FIXME: log it
return 0;
}
drerrno = DRE_NOERROR;
return 1;
}
int
send_envvars (int sfd, struct envvars *envvars, int do_checksize) {
//fprintf (stderr,"DEBUG: send_envvars() we have %i environment variables available for the request\n",envvars->nvariables);
uint16_t nvariables;
nvariables = htons (envvars->nvariables);
if (!dr_socket_write (sfd,(char *)&nvariables,sizeof(nvariables))) {
return 0;
}
//fprintf (stderr,"DEBUG: send_envvars() and just informed the client about that\n");
if (envvars->nvariables) {
int i;
envvars_attach (envvars);
for (i = 0; i < envvars->nvariables; i++) {
if (!send_envvar (sfd,&(envvars->variables.ptr[i]),do_checksize)) {
return 0;
}
//fprintf (stderr,"DEBUG: send_envvars() just sent (%s,%s)\n",envvars->variables[i].name,envvars->variables[i].value);
}
envvars_detach (envvars);
}
drerrno = DRE_NOERROR;
return 1;
}
int
recv_envvars (int sfd, struct envvars *envvars, int do_checksize) {
// This function leaves envvars DETACHED
uint16_t nvariables;
int i;
struct envvar var;
if (!envvars) {
return 0;
}
if (!envvars_free (envvars)) {
fprintf (stderr,"ERROR: recv_envvars() error calling envvars_free(). (%s)\n",drerrno_str());
return 0;
}
if (!dr_socket_read(sfd,(char*)&nvariables,sizeof(nvariables))) {
fprintf (stderr,"ERROR: recv_envvars() while receiving nvariables. (%s)\n",drerrno_str());
return 0;
}
nvariables = ntohs (nvariables);
if (nvariables) {
//fprintf (stderr,"DEBUG: recv_envvars() we'll receive %i variables\n",nvariables);
for (i = 0; i < nvariables; i++) {
//fprintf (stderr,"DEBUG: recv_envvars() receive variable number %i\n",i);
if (!recv_envvar (sfd, &var, do_checksize)) {
fprintf (stderr,"ERROR: recv_envvars() while receiving a single variable. (%s)\n",drerrno_str());
return 0;
}
envvars_variable_add (envvars,var.name,var.value);
memset (&var,0,sizeof(struct envvar));
}
}
drerrno = DRE_NOERROR;
return 1;
}
int
recv_job (int sfd, struct job *job) {
uint32_t datasize;
datasize = sizeof (struct job);
if (!check_recv_datasize(sfd,datasize)) {
log_auto(L_ERROR,"recv_job(): error on datasize check. Local datasize: %u. (%s)",datasize,strerror(drerrno_system));
return 0;
}
// We should empty all locally reserved structures
job_delete (job);
if (!dr_socket_read(sfd,(char*)job,datasize)) {
return 0;
}
job_fix_received_invalid (job);
// Environment variables
if (!recv_envvars (sfd,&job->envvars,1)) {
fprintf (stderr,"ERROR: recv_job() there was an error while receiving envvars (%s)\n",drerrno_str());
return 0;
}
job_bswap_from_network (job,job);
drerrno = DRE_NOERROR;
return 1;
}
int
send_job (int sfd, struct job *job) {
/* This function _sets_ frame_info = NULL before sending */
struct job bswapped;
void *buf = &bswapped;
uint32_t datasize;
datasize = sizeof(struct job);
if (!check_send_datasize(sfd,datasize)) {
// FIXME: log it
fprintf (stderr,"datasize: %i",datasize);
return 0;
}
/* Prepare for sending */
job_bswap_to_network (job,&bswapped);
if (!dr_socket_write (sfd,(char*)buf,datasize)) {
fprintf (stderr,"ERROR: Failed to write job to sfd\n");
return 0;
}
// Environment variables
if (!send_envvars (sfd,&job->envvars,1)) {
fprintf (stderr,"ERROR: Failed to send Environment\n");
return 0;
}
return 1;
}
int
recv_task (int sfd, struct task *task) {
void *buf;
uint32_t datasize;
buf = task; /* So when copying to buf we're really copying into
job */
datasize = sizeof (*task);
if (!check_recv_datasize(sfd,datasize)) {
// FIXME
return 0;
}
task_init (task);
if (!dr_socket_read(sfd,(char*)buf,datasize)) {
return 0;
}
/* Now we should have the task info with the values in */
/* network byte order, so we put them in host byte order */
task->ijob = ntohl (task->ijob);
task->icomp = ntohl (task->icomp);
task->itask = ntohs (task->itask);
task->frame = ntohl (task->frame);
task->frame_start = ntohl (task->frame_start);
task->frame_end = ntohl (task->frame_end);
task->frame_step = ntohl (task->frame_step);
task->block_size = ntohl (task->block_size);
task->pid = ntohl (task->pid);
task->exitstatus = ntohl (task->exitstatus);
task->start_loading_time = dr_ntoh64 (task->start_loading_time);
return 1;
}
int
send_task (int sfd, struct task *task) {
struct task bswapped;
void *buf = &bswapped;
uint32_t datasize = sizeof (*task);
if (!check_send_datasize(sfd,datasize)) {
// FIXME
return 0;
}
/* We make a copy coz we need to modify the values */
memcpy (buf,task,sizeof(bswapped));
/* Prepare for sending */
bswapped.ijob = htonl (bswapped.ijob);
bswapped.icomp = htonl (bswapped.icomp);
bswapped.itask = htons (bswapped.itask);
bswapped.frame = htonl (bswapped.frame);
bswapped.frame_start = htonl (bswapped.frame_start);
bswapped.frame_end = htonl (bswapped.frame_end);
bswapped.frame_step = htonl (bswapped.frame_step);
bswapped.block_size = htonl (bswapped.block_size);
bswapped.pid = htonl (bswapped.pid);
bswapped.exitstatus = htonl (bswapped.exitstatus);
bswapped.start_loading_time = dr_hton64 (bswapped.start_loading_time);
if (!dr_socket_write (sfd,(char*)buf,datasize)) {
return 0;
}
return 1;
}
int
send_computer (int sfd, struct computer *computer, uint8_t use_local_pools) {
if (!send_computer_status (sfd,&computer->status)) {
printf ("error send_computer_status\n");
return 0;
}
if (!send_computer_hwinfo (sfd,&computer->hwinfo)) {
printf ("error send_computer_hwinfo\n");
return 0;
}
if (!send_computer_limits (sfd,&computer->limits,use_local_pools)) {
printf ("error send_computer_limits\n");
return 0;
}
return 1;
}
int
recv_computer (int sfd, struct computer *computer) {
if (!recv_computer_status (sfd,&computer->status)) {
printf ("error recv_computer_status\n");
return 0;
}
if (!recv_computer_hwinfo (sfd,&computer->hwinfo)) {
printf ("error recv_computer_hwinfo\n");
return 0;
}
if (!recv_computer_limits (sfd,&computer->limits)) {
printf ("error recv_computer_limits\n");
return 0;
}
return 1;
}
int
recv_frame_info (int sfd, struct frame_info *fi) {
void *buf;
buf = fi;
if (!dr_socket_read (sfd,(char*)buf,sizeof (struct frame_info))) {
return 0;
}
fi->start_time = ntohl (fi->start_time);
fi->end_time = ntohl (fi->end_time);
fi->icomp = ntohl (fi->icomp);
fi->itask = ntohs (fi->itask);
fi->requeued = ntohs (fi->requeued);
fi->flags = ntohs (fi->flags);
return 1;
}
int send_frame_info (int sfd, struct frame_info *fi) {
struct frame_info bswapped;
void *buf = &bswapped;
/* We make a copy coz we need to modify the values */
memcpy (buf,fi,sizeof(bswapped));
/* Prepare for sending */
bswapped.start_time = htonl (bswapped.start_time);
bswapped.end_time = htonl (bswapped.end_time);
bswapped.icomp = htonl (bswapped.icomp);
bswapped.itask = htons (bswapped.itask);
bswapped.requeued = htons (bswapped.requeued);
bswapped.flags = htons (bswapped.flags);
if (!dr_socket_write (sfd,(char*)buf,sizeof (struct frame_info))) {
return 0;
}
return 1;
}
int
send_string (int sfd, char *str) {
uint16_t len,lensw;
uint32_t datasize;
len = strlen (str)+1;
lensw = htons (len);
datasize = sizeof (lensw);
if (!check_send_datasize(sfd,datasize)) {
return 0;
}
if (!dr_socket_write (sfd,(char*)&lensw,datasize))
return 0;
datasize = (uint32_t)len;
if (!check_send_datasize(sfd,datasize)) {
return 0;
}
if (!dr_socket_write (sfd,(char*)str,len)) {
return 0;
}
return 1;
}
int
recv_string (int sfd, char **str) {
uint16_t len;
uint32_t datasize;
datasize = sizeof(len);
if (!check_recv_datasize(sfd,datasize)) {
return 0;
}
if (!dr_socket_read (sfd,(char*)&len,datasize)) {
return 0;
}
len = ntohs (len);
datasize = (uint32_t)len;
if (!check_recv_datasize(sfd,datasize)) {
return 0;
}
*str = (char *) malloc (len);
if (!dr_socket_read (sfd,(char*)*str,len)) {
return 0;
}
return 1;
}
int
send_computer_pools (int sfd, struct computer_limits *cl, uint8_t use_local_pools) {
uint16_t i;
uint16_t npools;
struct pool *pool;
uint32_t datasize;
log_auto (L_DEBUG3,"send_computer_pools() : Entering...");
computer_pool_lock (cl);
datasize = sizeof (npools);
if (!check_send_datasize(sfd,datasize)) {
log_auto (L_ERROR,"send_computer_pools(): different data size for npools (uint16_t). Local: %u",datasize);
return 0;
}
log_auto (L_DEBUG3,"send_computer_pools() : going to send '%u' pools",cl->npools);
npools = htons (cl->npools);
if (!dr_socket_write (sfd,(char*)&npools,datasize)) {
log_auto (L_ERROR,"send_computer_pools(): could not send npools. (%s)",strerror(drerrno_system));
return 0;
}
datasize = sizeof(struct pool);
if (cl->npools) {
if (!use_local_pools) {
log_auto (L_DEBUG3,"send_computer_pools(): NOT USING local pools");
if ((pool = (struct pool *)computer_pool_attach_shared_memory(cl)) == (struct pool*)-1) {
log_auto (L_ERROR,"send_computer_pools() : attaching pool shared memory '%ji' shmid. (%s)",cl->poolshmid,strerror(errno));
return 0;
}
} else {
log_auto (L_DEBUG3,"send_computer_pools(): USING local pools");
pool = cl->local_pool.ptr;
}
log_auto (L_DEBUG2,"send_computer_pools(): npools = %u",cl->npools);
for (i=0;i<cl->npools;i++) {
log_auto (L_DEBUG3,"send_computer_pools(): previous to check_send_datasize(struct pool).");
if (!check_send_datasize(sfd,datasize)) {
log_auto (L_ERROR,"send_computer_pools() : different data sizes for struct pool.");
return 0;
}
log_auto (L_DEBUG3,"send_computer_pools(): success. post check_send_datasize(struct pool).");
log_auto (L_DEBUG3,"send_computer_pools(): previous send pool '%i' with name '%s'",i,pool[i].name);
if (!dr_socket_write(sfd,(char*)&pool[i],datasize)) { // write "sizeof(struct pool)" bytes
log_auto (L_ERROR,"send_computer_pools() : error while sending pool number %i. (%s)",i,strerror(drerrno_system));
if (!use_local_pools) {
computer_pool_detach_shared_memory (cl);
}
return 0;
}
log_auto (L_DEBUG3,"send_computer_pools(): success. post send pool '%i' with name '%s'",i,pool[i].name);
}
if (!use_local_pools) {
computer_pool_detach_shared_memory (cl);
}
}
log_auto (L_DEBUG2,"send_computer_pools(): following pool list has been sent:");
log_auto (L_DEBUG3,"send_computer_pools() : Returning...");
return 1;
}
int
recv_computer_pools (int sfd, struct computer_limits *cl) {
uint16_t i;
uint16_t npools;
struct pool pool;
uint32_t datasize;
struct computer_limits limits,old_cl;
log_auto (L_DEBUG3,"recv_computer_pools(): >Entering...");
datasize = sizeof(npools);
if (!check_recv_datasize(sfd,datasize)) {
log_auto (L_ERROR,"recv_computer_pools(): different npools (uint16_t) size.");
return 0;
}
log_auto (L_DEBUG3,"recv_computer_pools() : _before_ receiving the number of pools");
if (!dr_socket_read (sfd,(char*)&npools,datasize)) {
log_auto (L_ERROR,"recv_computer_pools(): error reading from file or socket. (%s)",strerror(drerrno_system));
return 0;
}
npools = ntohs (npools);
log_auto (L_DEBUG2,"recv_computer_pools(): received number of pools, that will be = %u",npools);
memset (&limits,0,sizeof(struct computer_limits));
computer_pool_init (&limits);
datasize = sizeof(struct pool);
for (i=0;i<npools;i++) {
if (!check_recv_datasize(sfd,datasize)) {
log_auto (L_ERROR,"recv_computer_pools(): different pools (struct pool) size. Local size: %u",datasize);
return 0;
}