summaryrefslogtreecommitdiff
path: root/services/scheduler.cpp
blob: f4bf792ad48a244a967ad75248d9db9b06334f41 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
/*  -*- mode: C++; c-file-style: "gnu"; fill-column: 78 -*- */
/* vim:cinoptions={.5s,g0,p5,t0,(0,^-0.5s,n-0.5s:tw=78:cindent:sw=4: */
/*
    This file is part of Icecream.

    Copyright (c) 2004 Michael Matz <matz@suse.de>
                  2004 Stephan Kulow <coolo@suse.de>

    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.
*/

#ifndef _GNU_SOURCE
// getopt_long
#define _GNU_SOURCE 1
#endif

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/select.h>
#include <sys/signal.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <time.h>
#include <getopt.h>
#include <string>
#include <list>
#include <map>
#include <queue>
#include <algorithm>
#include <cassert>
#include <fstream>
#include <string>
#include <stdio.h>
#include "comm.h"
#include "logging.h"
#include "job.h"
#include "config.h"
#include "bench.h"

#define DEBUG_SCHEDULER 0

/* TODO:
   * leak check
   * are all filedescs closed when done?
   * simplify livetime of the various structures (Jobs/Channels/CSs know
     of each other and sometimes take over ownership)
 */

/* TODO:
  - iron out differences in code size between architectures
   + ia64/i686: 1.63
   + x86_64/i686: 1.48
   + ppc/i686: 1.22
   + ppc64/i686: 1.59
  (missing data for others atm)
*/

/* The typical flow of messages for a remote job should be like this:
     prereq: daemon is connected to scheduler
     * client does GET_CS
     * request gets queued
     * request gets handled
     * scheduler sends USE_CS
     * client asks remote daemon
     * daemon sends JOB_BEGIN
     * client sends END + closes connection
     * daemon sends JOB_DONE (this can be swapped with the above one)
   This means, that iff the client somehow closes the connection we can and
   must remove all traces of jobs resulting from that client in all lists.
 */

using namespace std;

static string pidFilePath;

struct JobStat {
  unsigned long osize;  // output size (uncompressed)
  unsigned long compile_time_real;  // in milliseconds
  unsigned long compile_time_user;
  unsigned long compile_time_sys;
  unsigned int job_id;
  JobStat() : osize(0), compile_time_real(0), compile_time_user(0),
	      compile_time_sys(0), job_id(0) {}
  JobStat& operator +=(const JobStat &st)
  {
    osize += st.osize;
    compile_time_real += st.compile_time_real;
    compile_time_user += st.compile_time_user;
    compile_time_sys += st.compile_time_sys;
    job_id = 0;
    return *this;
  }
  JobStat& operator -=(const JobStat &st)
  {
    osize -= st.osize;
    compile_time_real -= st.compile_time_real;
    compile_time_user -= st.compile_time_user;
    compile_time_sys -= st.compile_time_sys;
    job_id = 0;
    return *this;
  }
private:
  JobStat& operator /=(int d)
  {
    osize /= d;
    compile_time_real /= d;
    compile_time_user /= d;
    compile_time_sys /= d;
    job_id = 0;
    return *this;
  }
public:
  JobStat operator /(int d) const
  {
    JobStat r = *this;
    r /= d;
    return r;
  }

};

class Job;

/* One compile server (receiver, compile daemon)  */
class CS : public MsgChannel
{
public:
  /* The listener port, on which it takes compile requests.  */
  unsigned int remote_port;
  unsigned int hostid;
  string nodename;
  time_t busy_installing;
  string host_platform;

  // unsigned int jobs_done;
  //  unsigned long long rcvd_kb, sent_kb;
  // unsigned int ms_per_job;
  // unsigned int bytes_per_ms;
  // LOAD is load * 1000
  unsigned int load;
  int max_jobs;
  bool noremote;
  list<Job*> joblist;
  Environments compiler_versions;  // Available compilers
  CS (int fd, struct sockaddr *_addr, socklen_t _len, bool text_based)
    : MsgChannel(fd, _addr, _len, text_based),
      load(1000), max_jobs(0), noremote(false),
      state(CONNECTED), type(UNKNOWN), chroot_possible(false)
  {
    hostid = 0;
    busy_installing = 0;
  }
  void pick_new_id()
  {
    assert( !hostid );
    hostid = ++hostid_counter;
  }
  list<JobStat> last_compiled_jobs;
  list<JobStat> last_requested_jobs;
  JobStat cum_compiled;  // cumulated
  JobStat cum_requested;
  enum {CONNECTED, LOGGEDIN} state;
  enum {UNKNOWN, DAEMON, MONITOR, LINE} type;
  bool chroot_possible;
  static unsigned int hostid_counter;
  map<int, int> client_map; // map client ID for daemon to our IDs
  bool is_eligible( const Job *job );
  bool check_remote( const Job *job ) const;
};

unsigned int CS::hostid_counter = 0;

static map<int, CS *> fd2cs;
static bool exit_main_loop = false;

time_t starttime;

class Job
{
public:
  unsigned int id;
  unsigned int local_client_id;
  enum {PENDING, WAITINGFORCS, COMPILING, WAITINGFORDONE} state;
  CS *server;  // on which server we build
  CS *submitter;  // who submitted us
  Environments environments;
  time_t starttime;  // _local_ to the compiler server
  time_t start_on_scheduler;  // starttime local to scheduler
  /**
   * the end signal from client and daemon is a bit of a race and
   * in 99.9% of all cases it's catched correctly. But for the remaining
   * 0.1% we need a solution too - otherwise these jobs are eating up slots.
   * So the solution is to track done jobs (client exited, daemon didn't signal)
   * and after 10s no signal, kill the daemon (and let it rehup) **/
  time_t done_time;

  string target_platform;
  string filename;
  list<Job*> master_job_for;
  unsigned int arg_flags;
  string language; // for debugging
  string preferred_host; // for debugging daemons
  Job (unsigned int _id, CS *subm)
    : id(_id), local_client_id( 0 ), state(PENDING), server(0),
      submitter(subm),
      starttime(0), start_on_scheduler(0), done_time( 0 ), arg_flags( 0 ) {}
  ~Job()
  {
   // XXX is this really deleted on all other paths?
/*    fd2chan.erase (channel->fd);
    delete channel;*/
  }
};

// A subset of connected_hosts representing the compiler servers
static list<CS*> css, monitors, controls;
static list<string> block_css;
static unsigned int new_job_id;
static map<unsigned int, Job*> jobs;

/* XXX Uah.  Don't use a queue for the job requests.  It's a hell
   to delete anything out of them (for clean up).  */
struct UnansweredList
{
  list<Job*> l;
  CS *server;
  bool remove_job (Job *);
};
static list<UnansweredList*> toanswer;

static list<JobStat> all_job_stats;
static JobStat cum_job_stats;

static float server_speed (CS *cs, Job *job = 0);

/* Searches the queue for JOB and removes it.
   Returns true if something was deleted.  */
bool
UnansweredList::remove_job (Job *job)
{
  list<Job*>::iterator it;
  for (it = l.begin(); it != l.end(); ++it)
    if (*it == job)
      {
        l.erase (it);
	return true;
      }
  return false;
}

static void
add_job_stats (Job *job, JobDoneMsg *msg)
{
  JobStat st;
  /* We don't want to base our timings on failed or too small jobs.  */
  if (msg->out_uncompressed < 4096
      || msg->exitcode != 0)
    return;

  st.osize = msg->out_uncompressed;
  st.compile_time_real = msg->real_msec;
  st.compile_time_user = msg->user_msec;
  st.compile_time_sys = msg->sys_msec;
  st.job_id = job->id;

  if ( job->arg_flags & CompileJob::Flag_g )
    st.osize = st.osize * 10 / 36; // average over 1900 jobs: faktor 3.6 in osize
  else if ( job->arg_flags & CompileJob::Flag_g3 )
    st.osize = st.osize * 10 / 45; // average over way less jobs: factor 1.25 over -g

  // the difference between the -O flags isn't as big as the one between -O0 and -O>=1
  // the numbers are actually for gcc 3.3 - but they are _very_ rough heurstics anyway)
  if ( job->arg_flags & CompileJob::Flag_O ||
       job->arg_flags & CompileJob::Flag_O2 ||
       job->arg_flags & CompileJob::Flag_Ol2)
    st.osize = st.osize * 58 / 35;

  if ( job->server->last_compiled_jobs.size() >= 7)
    {
      /* Smooth out spikes by not allowing one job to add more than
         20% of the current speed.  */
      float this_speed = (float) st.osize / (float) st.compile_time_user;
      /* The current speed of the server, but without adjusting to the current
         job, hence no second argument.  */
      float cur_speed = server_speed (job->server);

      if ((this_speed / 1.2) > cur_speed)
        st.osize = (long unsigned) (cur_speed * 1.2 * st.compile_time_user);
      else if ((this_speed * 1.2) < cur_speed)
        st.osize = (long unsigned) (cur_speed / 1.2 * st.compile_time_user);
    }

  job->server->last_compiled_jobs.push_back (st);
  job->server->cum_compiled += st;
  if (job->server->last_compiled_jobs.size() > 200)
    {
      job->server->cum_compiled -= *job->server->last_compiled_jobs.begin ();
      job->server->last_compiled_jobs.pop_front ();
    }
  job->submitter->last_requested_jobs.push_back (st);
  job->submitter->cum_requested += st;
  if (job->submitter->last_requested_jobs.size() > 200)
    {
      job->submitter->cum_requested
        -= *job->submitter->last_requested_jobs.begin ();
      job->submitter->last_requested_jobs.pop_front ();
    }
  all_job_stats.push_back (st);
  cum_job_stats += st;
  if (all_job_stats.size () > 2000)
    {
      cum_job_stats -= *all_job_stats.begin ();
      all_job_stats.pop_front ();
    }

#if DEBUG_SCHEDULER > 1
  if ( job->arg_flags < 7000 )
    trace() << "add_job_stats " << job->language << " "
            << ( time( 0 ) - starttime ) << " "
            << st.compile_time_user << " "
            << ( job->arg_flags & CompileJob::Flag_g ? '1' : '0')
            << ( job->arg_flags & CompileJob::Flag_g3 ? '1' : '0')
            << ( job->arg_flags & CompileJob::Flag_O ? '1' : '0')
            << ( job->arg_flags & CompileJob::Flag_O2 ? '1' : '0')
            << ( job->arg_flags & CompileJob::Flag_Ol2 ? '1' : '0')
            << " " << st.osize << " " << msg->out_uncompressed << " "
            << job->server->nodename << " "
            << float(msg->out_uncompressed) / st.compile_time_user << " "
            << server_speed( job->server ) << endl ;
#endif

}

static bool handle_end (CS *c, Msg *);

static void
notify_monitors (Msg* m)
{
  list<CS*>::iterator it, it_old;
  for (it = monitors.begin(); it != monitors.end();)
    {
      it_old = it++;
      /* If we can't send it, don't be clever, simply close this monitor.  */
      if (!(*it_old)->send_msg (*m, MsgChannel::SendNonBlocking /*| MsgChannel::SendBulkOnly*/)) {
        trace() << "monitor is blocking... removing" << endl;
        handle_end (*it_old, 0);
      }
    }
  delete m;
}

static float
server_speed (CS *cs, Job *job)
{
  if (cs->last_compiled_jobs.size() == 0
      || cs->cum_compiled.compile_time_user == 0)
    return 0;
  else
    {
      float f = (float)cs->cum_compiled.osize
	       / (float) cs->cum_compiled.compile_time_user;

      // we only care for the load if we're about to add a job to it
      if (job) {
        /* The submitter of a job gets more speed.  So if he is equally
           fast to the rest of the farm it will be prefered to chose him
           to compile the job.  Then this can be done locally without
           needing the preprocessor.  */
        if (job->submitter == cs)
          f *= 1.1;
        else // ignoring load for submitter - assuming the load is our own
          f *= float(1000 - cs->load) / 1000;
      }

      // below we add a pessimism factor - assuming the first job a computer got is not representative
      if ( cs->last_compiled_jobs.size() < 7 )
          f *= ( -0.5 * cs->last_compiled_jobs.size() + 4.5 );

      return f;
    }
}

static void
handle_monitor_stats( CS *cs, StatsMsg *m = 0)
{
  if ( monitors.empty() )
    return;

  string msg;
  char buffer[1000];
  sprintf( buffer, "Name:%s\n", cs->nodename.c_str() );
  msg += buffer;
  sprintf( buffer, "IP:%s\n", cs->name.c_str() );
  msg += buffer;
  sprintf( buffer, "MaxJobs:%d\n", cs->max_jobs );
  msg += buffer;
  sprintf( buffer, "NoRemote:%s\n", cs->noremote ? "true" : "false"  );
  msg += buffer;
  sprintf( buffer, "Platform:%s\n", cs->host_platform.c_str() );
  msg += buffer;
  sprintf( buffer, "Speed:%f\n", server_speed( cs ) );
  msg += buffer;
  if ( m )
    {
      sprintf( buffer, "Load:%d\n", m->load );
      msg += buffer;
      sprintf( buffer, "LoadAvg1:%d\n", m->loadAvg1 );
      msg += buffer;
      sprintf( buffer, "LoadAvg5:%d\n", m->loadAvg5 );
      msg += buffer;
      sprintf( buffer, "LoadAvg10:%d\n", m->loadAvg10 );
      msg += buffer;
      sprintf( buffer, "FreeMem:%d\n", m->freeMem );
      msg += buffer;
    }
  else
    {
      sprintf( buffer, "Load:%d\n", cs->load );
      msg += buffer;
    }
  notify_monitors( new MonStatsMsg( cs->hostid, msg ) );
}

static Job *
create_new_job (CS *submitter)
{
  ++new_job_id;
  assert (jobs.find(new_job_id) == jobs.end());

  Job *job = new Job (new_job_id, submitter);
  jobs[new_job_id] = job;
  return job;
}

static void
enqueue_job_request (Job *job)
{
  if (!toanswer.empty() && toanswer.back()->server == job->submitter)
    {
      toanswer.back()->l.push_back (job);
    }
  else
    {
      UnansweredList *newone = new UnansweredList();
      newone->server = job->submitter;
      newone->l.push_back (job);
      toanswer.push_back (newone);
    }
}

static Job *
get_job_request (void)
{
  if (toanswer.empty())
    return 0;

  UnansweredList *first = toanswer.front();
  assert (!first->l.empty());
  return first->l.front();
}

/* Removes the first job request (the one returned by get_job_request()) */
static void
remove_job_request (void)
{
  if (toanswer.empty())
    return;
  UnansweredList *first = toanswer.front();
  toanswer.pop_front();
  first->l.pop_front();
  if (first->l.empty())
    {
      delete first;
    }
  else
    {
      toanswer.push_back( first );
    }
}

static string dump_job (Job *job);

static bool
handle_cs_request (MsgChannel *c, Msg *_m)
{
  GetCSMsg *m = dynamic_cast<GetCSMsg *>(_m);
  if (!m)
    return false;

  CS *submitter = static_cast<CS*>( c );

  Job *master_job = 0;

  for ( unsigned int i = 0; i < m->count; ++i )
    {
      Job *job = create_new_job (submitter);
      job->environments = m->versions;
      job->target_platform = m->target;
      job->arg_flags = m->arg_flags;
      job->language = ( m->lang == CompileJob::Lang_C ? "C" : "C++" );
      job->filename = m->filename;
      job->local_client_id = m->client_id;
      job->preferred_host = m->preferred_host;
      enqueue_job_request (job);
      std::ostream& dbg = log_info();
      dbg << "NEW " << job->id << " client="
                    << submitter->nodename << " versions=[";
      for ( Environments::const_iterator it = job->environments.begin();
            it != job->environments.end();)
        {
          dbg << it->second << "(" << it->first << ")";
          if (++it != job->environments.end())
            dbg << ", ";
        }
      dbg << "] " << m->filename << " " << job->language << endl;
      notify_monitors (new MonGetCSMsg (job->id, submitter->hostid, m));
      if ( !master_job )
        {
          master_job = job;
        }
      else
        {
          master_job->master_job_for.push_back( job );
        }
    }
  return true;

}

static bool
handle_local_job (CS *c, Msg *_m)
{
  JobLocalBeginMsg *m = dynamic_cast<JobLocalBeginMsg *>(_m);
  if (!m)
    return false;

  ++new_job_id;
  trace() << "handle_local_job " << m->outfile << " " << m->id << endl;
  c->client_map[m->id] = new_job_id;
  notify_monitors (new MonLocalJobBeginMsg( new_job_id, m->outfile, m->stime, c->hostid ) );
  return true;
}

static bool
handle_local_job_done (CS *c, Msg *_m)
{
  JobLocalDoneMsg *m = dynamic_cast<JobLocalDoneMsg *>(_m);
  if (!m)
    return false;

  trace() << "handle_local_job_done " << m->job_id << endl;
  notify_monitors (new JobLocalDoneMsg( c->client_map[m->job_id] ) );
  c->client_map.erase( m->job_id );
  return true;
}

static bool
platforms_compatible( const string &target, const string &platform )
{
  if ( target == platform )
    return true;
  // the below doesn't work as the unmapped platform is transfered back to the
  // client and that asks the daemon for a platform he can't install (see TODO)

  static multimap<string, string> platform_map;

  if (platform_map.empty())
    {
      platform_map.insert( make_pair( string( "i386" ), string( "i486" ) ) );
      platform_map.insert( make_pair( string( "i386" ), string( "i586" ) ) );
      platform_map.insert( make_pair( string( "i386" ), string( "i686" ) ) );
      platform_map.insert( make_pair( string( "i386" ), string( "x86_64" ) ) );

      platform_map.insert( make_pair( string( "i486" ), string( "i586" ) ) );
      platform_map.insert( make_pair( string( "i486" ), string( "i686" ) ) );
      platform_map.insert( make_pair( string( "i486" ), string( "x86_64" ) ) );

      platform_map.insert( make_pair( string( "i586" ), string( "i686" ) ) );
      platform_map.insert( make_pair( string( "i586" ), string( "x86_64" ) ) );

      platform_map.insert( make_pair( string( "i686" ), string( "x86_64" ) ) );

      platform_map.insert( make_pair( string( "ppc" ), string( "ppc64" ) ) );
      platform_map.insert( make_pair( string( "s390" ), string( "s390x" ) ) );
    }

  multimap<string, string>::const_iterator end = platform_map.upper_bound( target );
  for ( multimap<string, string>::const_iterator it = platform_map.lower_bound( target );
        it != end;
        ++it )
    {
      if ( it->second == platform )
        return true;
    }

  return false;
}

/* Given a candidate CS and a JOB, check all installed environments
   on the CS for a match.  Return an empty string if none of the required
   environments for this job is installed.  Otherwise return the
   host platform of the first found installed environment which is among
   the requested.  That can be send to the client, which then completely
   specifies which environment to use (name, host platform and target
   platform).  */
static string
envs_match( CS* cs, const Job *job )
{
  if ( job->submitter == cs)
    return cs->host_platform; // it will compile itself

  /* Check all installed envs on the candidate CS ...  */
  for ( Environments::const_iterator it = cs->compiler_versions.begin(); it != cs->compiler_versions.end(); ++it )
    {
      if ( it->first == job->target_platform )
        {
	  /* ... IT now is an installed environment which produces code for
	     the requested target platform.  Now look at each env which
	     could be installed from the client (i.e. those coming with the
	     job) if it matches in name and additionally could be run
	     by the candidate CS.  */
          for ( Environments::const_iterator it2 = job->environments.begin(); it2 != job->environments.end(); ++it2 )
            {
              if ( it->second == it2->second && platforms_compatible( it2->first, cs->host_platform ) )
                return it2->first;
            }
        }
    }
  return string();
}

/* Given a candidate CS and a JOB, check if any of the requested
   environments could be installed on the CS.  This is the case if that
   env can be run there, i.e. if the host platforms of the CS and of the
   environment are compatible.  Return an empty string if none can be
   installed, otherwise return the platform of the first found
   environments which can be installed.  */
static string
can_install( CS* cs, const Job *job )
{
  // trace() << "can_install host: '" << cs->host_platform << "' target: '" << job->target_platform << "'" << endl;
  if ( cs->busy_installing )
    {
#if DEBUG_SCHEDULER > 0
      trace() << cs->nodename << " is busy installing since " << time(0) - cs->busy_installing << " seconds." << endl;
#endif
      return string();
    }

  for ( Environments::const_iterator it = job->environments.begin(); it != job->environments.end(); ++it )
    {
      if ( platforms_compatible( it->first, cs->host_platform ) )
        return it->first;
    }
  return string();
}

bool CS::check_remote( const Job *job ) const
{
    bool local = (job->submitter == this);
    return local || !noremote;
}

bool CS::is_eligible( const Job *job )
{
  bool jobs_okay = int( joblist.size() ) < max_jobs;
  bool load_okay = load < 1000;
  return jobs_okay
    && chroot_possible
    && load_okay
    && can_install( this, job ).size()
    && this->check_remote( job );
}

static CS *
pick_server(Job *job)
{
  list<CS*>::iterator it;

#if DEBUG_SCHEDULER > 1
  trace() << "pick_server " << job->id << " " << job->target_platform << endl;
#endif

#if DEBUG_SCHEDULER > 0
  /* consistency checking for now */
  for (list<CS*>::iterator it = css.begin(); it != css.end(); ++it)
    {
      CS* cs= *it;
      for ( list<Job*>::const_iterator it2 = cs->joblist.begin(); it2 != cs->joblist.end(); ++it2 )
        {
          Job *job = *it2;
          assert( jobs.find( job->id ) != jobs.end() );
        }
    }
  for (map<unsigned int, Job*>::const_iterator it = jobs.begin();
       it != jobs.end(); ++it)
    {
      Job *j = it->second;

      CS *cs = j->server;
      assert( j->state != j->COMPILING ||
              find( cs->joblist.begin(),
                    cs->joblist.end(), j ) != cs->joblist.end() );
    }
#endif

  /* if the user wants to test/prefer one specific daemon, we look for that one first */
  if (!job->preferred_host.empty())
    {
	for (it = css.begin(); it != css.end(); ++it)
          {
	     if ((*it)->nodename == job->preferred_host
                 && (*it)->is_eligible( job ))
	       return *it;
	  }
        return 0;
    }

  /* If we have no statistics simply use the first server which is usable.  */
  if (!all_job_stats.size ())
    {
      for (it = css.begin(); it != css.end(); ++it)
        {
          trace() << "no job stats - looking at " << ( *it )->nodename << " load: " << (*it )->load << " can install: " << can_install( *it, job ) << endl;
          if ((*it)->is_eligible( job ))
            {
              trace() << "returning first " << ( *it )->nodename << endl;
              return *it;
            }
        }
      return 0;
    }

  /* Now guess about the job.  First see, if this submitter already
     had other jobs.  Use them as base.  */
  JobStat guess;
  if (job->submitter->last_requested_jobs.size() > 0)
    {
      guess = job->submitter->cum_requested
	        / job->submitter->last_requested_jobs.size();
    }
  else
    {
      /* Otherwise simply average over all jobs.  */
      guess = cum_job_stats / all_job_stats.size();
    }
  CS *best = 0;
  // best uninstalled
  CS *bestui = 0;
  // best preloadable host
  CS *bestpre = 0;

  uint matches = 0;

  for (it = css.begin(); it != css.end(); ++it)
    {
      CS *cs = *it;
      /* For now ignore overloaded servers.  */
      /* Pre-loadable (cs->joblist.size()) == (cs->max_jobs) is checked later.  */
      if (int( cs->joblist.size() ) > cs->max_jobs || cs->load >= 1000)
        {
#if DEBUG_SCHEDULER > 1
          trace() << "overloaded " << cs->nodename << " " << cs->joblist.size() << "/" <<  cs->max_jobs << " jobs, load:" << cs->load << endl;
#endif
          continue;
      }

      // incompatible architecture or busy installing
      if ( !can_install( cs, job ).size() )
        {
#if DEBUG_SCHEDULER > 2
          trace() << cs->nodename << " can't install " << job->id << endl;
#endif
          continue;
        }

      /* Don't use non-chroot-able daemons for remote jobs.  XXX */
      if (!cs->chroot_possible)
        {
	  trace() << cs->nodename << " can't use chroot\n";
	  continue;
	}

      // Check if remote & if remote allowed
      if (!cs->check_remote( job ))
        {
	  trace() << cs->nodename << " fails remote job check\n";
	  continue;
	}


#if DEBUG_SCHEDULER > 1
      trace() << cs->nodename << " compiled " << cs->last_compiled_jobs.size() << " got now: " <<
        cs->joblist.size() << " speed: " << server_speed (cs) << " compile time " <<
        cs->cum_compiled.compile_time_user << " produced code " << cs->cum_compiled.osize << endl;
#endif

      if ( cs->last_compiled_jobs.size() == 0 && cs->joblist.size() == 0 && cs->max_jobs)
	{
	  /* Make all servers compile a job at least once, so we'll get an
	     idea about their speed.  */
	  if (!envs_match (cs, job).empty())
            {
              best = cs;
              matches++;
            }
	  else
            {
              // if there is one server that already got the environment and one that
              // hasn't compiled at all, pick the one with environment first
              bestui = cs;
            }
	  break;
	}

      if (!envs_match (cs, job).empty())
        {
          if ( !best )
            best = cs;
          /* Search the server with the earliest projected time to compile
             the job.  (XXX currently this is equivalent to the fastest one)  */
          else
            if (best->last_compiled_jobs.size() != 0
                && server_speed (best, job) < server_speed (cs, job))
              {
                if (int( cs->joblist.size() ) < cs->max_jobs)
                  best = cs;
                else
                  bestpre = cs;
              }
          matches++;
        }
      else
        {
          if ( !bestui )
            bestui = cs;
          /* Search the server with the earliest projected time to compile
             the job.  (XXX currently this is equivalent to the fastest one)  */
          else
            if (bestui->last_compiled_jobs.size() != 0
                && server_speed (bestui, job) < server_speed (cs, job))
              {
                if (int( cs->joblist.size() ) < cs->max_jobs)
                  bestui = cs;
                else
                  bestpre = cs;
              }
        }
    }

  // to make sure we find the fast computers at least after some time, we overwrite
  // the install rule for every 19th job - if the farm is only filled a bit
  if ( bestui && ( matches < 11 && matches < css.size() / 3 ) && job->id % 19 != 0 )
	best = 0;

  if ( best )
    {
#if DEBUG_SCHEDULER > 1
      trace() << "taking best installed " << best->nodename << " " <<  server_speed (best) << endl;
#endif
      return best;
    }

  if ( bestui )
    {
#if DEBUG_SCHEDULER > 1
      trace() << "taking best uninstalled " << bestui->nodename << " " <<  server_speed (bestui) << endl;
#endif
      return bestui;
    }

  if ( bestpre )
    {
#if DEBUG_SCHEDULER > 1
      trace() << "taking best preload " << bestui->nodename << " " <<  server_speed (bestui) << endl;
#endif
    }

  return bestpre;
}

/* Prunes the list of connected servers by those which haven't
   answered for a long time. Return the number of seconds when
   we have to cleanup next time. */
static time_t
prune_servers ()
{
  list<CS*>::iterator it;

  time_t now = time( 0 );
  time_t min_time = MAX_SCHEDULER_PING;

  for (it = controls.begin(); it != controls.end();)
    {
      if (now - ( *it )->last_talk >= MAX_SCHEDULER_PING) 
        {
	  CS *old = *it;
          ++it;
	  handle_end (old, 0);
	  continue;
        }
      min_time = min (min_time, MAX_SCHEDULER_PING - now + ( *it )->last_talk);
      ++it;
    }

  for (it = css.begin(); it != css.end(); )
    {
      if ((*it)->busy_installing && (now - (*it)->busy_installing >=
                                     MAX_BUSY_INSTALLING))
        {
	  trace() << "busy installing for a long time - removing " << ( *it )->nodename << endl;
	  CS *old = *it;
	  ++it;
	  handle_end (old, 0);
	  continue;
        }

      /* protocol version 27 and newer use TCP keepalive */
      if (IS_PROTOCOL_27(*it)) {
        ++it;
        continue;
      }

      if (now - ( *it )->last_talk >= MAX_SCHEDULER_PING)
        {
          if (( *it )->max_jobs >= 0)
            {
              trace() << "send ping " << ( *it )->nodename << endl;
              ( *it )->max_jobs *= -1; // better not give it away
              if(( *it )->send_msg( PingMsg() ))
		{
                  // give it MAX_SCHEDULER_PONG to answer a ping
                  ( *it )->last_talk = time( 0 ) - MAX_SCHEDULER_PING
		                       + 2 * MAX_SCHEDULER_PONG;
                  min_time = min (min_time, (time_t) 2 * MAX_SCHEDULER_PONG);
		  ++it;
		  continue;
		}
	    }
	  // R.I.P.
	  trace() << "removing " << ( *it )->nodename << endl;
	  CS *old = *it;
	  ++it;
	  handle_end (old, 0);
	  continue;
        }
      else
        min_time = min (min_time, MAX_SCHEDULER_PING - now + ( *it )->last_talk);
#if DEBUG_SCHEDULER > 1
      if ((random() % 400) < 0)
        { // R.I.P.
          trace() << "FORCED removing " << ( *it )->nodename << endl;
          CS *old = *it;
          ++it;
          handle_end (old, 0);
          continue;
        }
#endif

      ++it;
    }

    return min_time;
}

static Job*
delay_current_job()
{
  assert (!toanswer.empty());
  if ( toanswer.size() == 1 )
    return 0;

  UnansweredList *first = toanswer.front();
  toanswer.pop_front();
  toanswer.push_back( first );
  return get_job_request();
}

static bool
empty_queue()
{
  Job *job = get_job_request ();
  if (!job)
    return false;

  assert(!css.empty());

  Job *first_job = job;
  CS *cs = 0;

  while ( true )
    {
      cs = pick_server (job);

      if (cs)
        break;

      /* Ignore the load on the submitter itself if no other host could
         be found.  We only obey to its max job number.  */
      cs = job->submitter;
      if (! (int( cs->joblist.size() ) < cs->max_jobs
             && job->preferred_host.empty()
             /* This should be trivially true.  */
             && can_install (cs, job).size()))
        {
          job = delay_current_job();
          if ( job == first_job || !job ) // no job found in the whole toanswer list
            return false;
        }
      else
        {
          break;
        }
    }

  remove_job_request ();

  job->state = Job::WAITINGFORCS;
  job->server = cs;

  string host_platform = envs_match (cs, job);
  bool gotit = true;
  if (host_platform.empty ())
    {
      gotit = false;
      host_platform = can_install (cs, job);
    }

  // mix and match between job ids
  unsigned matched_job_id = 0;
  unsigned count = 0;
  for (list<JobStat>::const_iterator l = job->submitter->last_requested_jobs.begin();
       l != job->submitter->last_requested_jobs.end(); ++l)
    {
      unsigned rcount = 0;
      for (list<JobStat>::const_iterator r = cs->last_compiled_jobs.begin();
           r != cs->last_compiled_jobs.end(); ++r)
        {
          if (l->job_id == r->job_id)
            matched_job_id = l->job_id;
          if (++rcount > 16)
            break;
        }

       if (matched_job_id || ++count > 16)
         break;
    }

  UseCSMsg m2(host_platform, cs->name, cs->remote_port, job->id,
	      gotit, job->local_client_id, matched_job_id );

  if (!job->submitter->send_msg (m2))
    {
      trace() << "failed to deliver job " << job->id << endl;
      handle_end( job->submitter, 0 ); // will care for the rest
      return true;
    }
  else
    {
#if DEBUG_SCHEDULER >= 0
      if (!gotit)
	trace() << "put " << job->id << " in joblist of " << cs->nodename << " (will install now)" << endl;
      else
        trace() << "put " << job->id << " in joblist of " << cs->nodename << endl;
#endif
      cs->joblist.push_back( job );
      /* if it doesn't have the environment, it will get it. */
      if ( !gotit )
        cs->busy_installing = time(0);

      string env;
      if ( !job->master_job_for.empty() )
        {
          for ( Environments::const_iterator it = job->environments.begin(); it != job->environments.end(); ++it )
            {
              if ( it->first == cs->host_platform )
                {
                  env = it->second;
                  break;
                }
            }
        }
      if ( !env.empty() )
        {
          for ( list<Job*>::iterator it = job->master_job_for.begin(); it != job->master_job_for.end(); ++it )
            { // remove all other environments
              ( *it )->environments.clear();
              ( *it )->environments.push_back( make_pair( cs->host_platform, env ) );
            }
        }
    }
  return true;
}

static bool
handle_login (CS *cs, Msg *_m)
{
  LoginMsg *m = dynamic_cast<LoginMsg *>(_m);
  if (!m)
    return false;

  std::ostream& dbg = trace();

  cs->remote_port = m->port;
  cs->compiler_versions = m->envs;
  cs->max_jobs = m->chroot_possible ? m->max_kids : 0;
  cs->noremote = m->noremote;
  if ( m->nodename.length() )
    cs->nodename = m->nodename;
  else
    cs->nodename = cs->name;
  cs->host_platform = m->host_platform;
  cs->chroot_possible = m->chroot_possible;
  cs->pick_new_id();

  for (list<string>::const_iterator it = block_css.begin(); it != block_css.end(); ++it)
      if (cs->name == *it)
          return false;

  dbg << "login " << m->nodename << " protocol version: " << cs->protocol;
  handle_monitor_stats( cs );

  /* remove any other clients with the same IP, they must be stale */
  for (list<CS*>::iterator it = css.begin(); it != css.end(); )
    {
      if (cs->eq_ip(*(*it)))
      {
        CS* old = *it;
        ++it;
        handle_end(old, 0);
        continue;
      }
      ++it;
    }

  css.push_back (cs);

#if 1
  dbg << " [";
  for (Environments::const_iterator it = m->envs.begin();
       it != m->envs.end(); ++it)
    dbg << it->second << "(" << it->first << "), ";
  dbg << "]" << endl;
#endif

  /* Configure the daemon */
  if (IS_PROTOCOL_24( cs ))
    cs->send_msg (ConfCSMsg(icecream_bench_code));

  return true;
}

static bool
handle_relogin (MsgChannel *c, Msg *_m)
{
  LoginMsg *m = dynamic_cast<LoginMsg *>(_m);
  if (!m)
    return false;

  CS *cs = static_cast<CS *>(c);
  cs->compiler_versions = m->envs;
  cs->busy_installing = 0;

  std::ostream &dbg = trace();
  dbg << "RELOGIN " << cs->nodename << "(" << cs->host_platform << "): [";
  for (Environments::const_iterator it = m->envs.begin();
       it != m->envs.end(); ++it)
    dbg << it->second << "(" << it->first << "), ";
  dbg << "]" << endl;

  /* Configure the daemon */
  if (IS_PROTOCOL_24( c ))
    c->send_msg (ConfCSMsg(icecream_bench_code));

  return false;
}

static bool
handle_mon_login (CS *c, Msg *_m)
{
  MonLoginMsg *m = dynamic_cast<MonLoginMsg *>(_m);
  if (!m)
    return false;
  monitors.push_back (c);
  // monitors really want to be fed lazily
  c->setBulkTransfer();

  for (list<CS*>::const_iterator it = css.begin(); it != css.end(); ++it)
    handle_monitor_stats( *it );

  fd2cs.erase( c->fd ); // no expected data from them
  return true;
}

static bool
handle_job_begin (CS *c, Msg *_m)
{
  JobBeginMsg *m = dynamic_cast<JobBeginMsg *>(_m);
  if ( !m )
    return false;

  if (jobs.find(m->job_id) == jobs.end()) {
    trace() << "handle_job_begin: no valid job id " << m->job_id << endl;
    return false;
  }
  Job *job = jobs[m->job_id];
  if (job->server != c)
    {
      trace() << "that job isn't handled by " << c->name << endl;
      return false;
    }
  job->state = Job::COMPILING;
  job->starttime = m->stime;
  job->start_on_scheduler = time(0);
  notify_monitors (new MonJobBeginMsg (m->job_id, m->stime, c->hostid));
#if DEBUG_SCHEDULER >= 0
  trace() << "BEGIN: " << m->job_id << " client=" << job->submitter->nodename
          << "(" << job->target_platform << ")" << " server="
          << job->server->nodename << "(" << job->server->host_platform
          << ")" << endl;
#endif

  return true;
}


static bool
handle_job_done (CS *c, Msg *_m)
{
  JobDoneMsg *m = dynamic_cast<JobDoneMsg *>(_m);
  if ( !m )
    return false;

  Job *j = 0;

  if (m->exitcode == CLIENT_WAS_WAITING_FOR_CS)
    {
      // the daemon saw a cancel of what he believes is waiting in the scheduler
      map<unsigned int, Job*>::iterator mit;
      for (mit = jobs.begin(); mit != jobs.end(); ++mit)
        {
          Job *job = mit->second;
          trace() << "looking for waitcs " << job->server << " " << job->submitter  << " " << c << " "
		  << job->state << " " << job->local_client_id << " " << m->job_id << endl;
          if (job->server == 0 && job->submitter == c && job->state == Job::PENDING
              && job->local_client_id == m->job_id )
            {
              trace() << "STOP (WAITFORCS) FOR " << mit->first << endl;
              j = job;
              m->job_id = j->id; // that's faked

	      /* Unfortunately the toanswer queues are also tagged based on the daemon,
		 so we need to clean them up also.  */
	      list<UnansweredList*>::iterator it;
	      for (it = toanswer.begin(); it != toanswer.end(); ++it)
		if ((*it)->server == c)
		  {
		    UnansweredList *l = *it;
		    list<Job*>::iterator jit;
		    for (jit = l->l.begin(); jit != l->l.end(); ++jit)
		      {
			if (*jit == j)
			  {
			    l->l.erase(jit);
			    break;
			  }
		      }
		    if (l->l.empty())
		      {
			it = toanswer.erase (it);
			break;
		      }
		  }
	    }
	}
    }
  else
    if (jobs.find(m->job_id) != jobs.end())
      j = jobs[m->job_id];

  if (!j)
    {
      trace() << "job ID not present " << m->job_id << endl;
      return false;
    }

  if (j->state == Job::PENDING)
    {
      trace() << "job ID still pending ?! scheduler recently restarted? " << m->job_id << endl;
      return false;
    }

  if (m->is_from_server() && j->server != c)
    {
      log_info() << "the server isn't the same for job " << m->job_id << endl;
      log_info() << "server: " << j->server->nodename << endl;
      log_info() << "msg came from: " << c->nodename << endl;
      // the daemon is not following matz's rules: kick him
      handle_end(c, 0);
      return false;
    }
  if (!m->is_from_server() && j->submitter != c)
    {
      log_info() << "the submitter isn't the same for job " << m->job_id << endl;
      log_info() << "submitter: " << j->submitter->nodename << endl;
      log_info() << "msg came from: " << c->nodename << endl;
      // the daemon is not following matz's rules: kick him
      handle_end(c, 0);
      return false;
    }



  if ( m->exitcode == 0 )
    {
      std::ostream &dbg = trace();
      dbg << "END " << m->job_id
	  << " status=" << m->exitcode;

      if ( m->in_uncompressed )
	dbg << " in=" << m->in_uncompressed
	    << "(" << int( m->in_compressed * 100 / m->in_uncompressed ) << "%)";
      else
	dbg << " in=0(0%)";

      if ( m->out_uncompressed )
	dbg << " out=" << m->out_uncompressed
	    << "(" << int( m->out_compressed * 100 / m->out_uncompressed ) << "%)";
      else
	dbg << " out=0(0%)";

      dbg << " real=" << m->real_msec
	  << " user=" << m->user_msec
	  << " sys=" << m->sys_msec
	  << " pfaults=" << m->pfaults
	  << " server=" << j->server->nodename
	  << endl;
    }
  else
    trace() << "END " << m->job_id
	    << " status=" << m->exitcode << endl;

  if (j->server)
      j->server->joblist.remove (j);

  add_job_stats (j, m);
  notify_monitors (new MonJobDoneMsg (*m));
  jobs.erase (m->job_id);
  delete j;

  return true;
}

static bool
handle_ping (CS* c, Msg * /*_m*/)
{
  c->last_talk = time( 0 );
  if ( c->max_jobs < 0 )
    c->max_jobs *= -1;
  return true;
}

static bool
handle_stats (CS * c, Msg * _m)
{
  StatsMsg *m = dynamic_cast<StatsMsg *>(_m);
  if (!m)
    return false;

  /* Before protocol 25, ping and stat handling was
     clutched together.  */
  if (!IS_PROTOCOL_25(c))
    {
      c->last_talk = time( 0 );
      if ( c && c->max_jobs < 0 )
        c->max_jobs *= -1;
    }

  for (list<CS*>::iterator it = css.begin(); it != css.end(); ++it)
    if ( *it == c )
      {
        ( *it )->load = m->load;
        handle_monitor_stats( *it, m );
        return true;
      }

  return false;
}

static string
dump_job (Job *job)
{
  char buffer[1000];
  string line;
  snprintf (buffer, sizeof (buffer), "%d %s sub:%s on:%s ",
	   job->id,
	   job->state == Job::PENDING ? "PEND"
	     : job->state == Job::WAITINGFORCS ? "WAIT"
	     : job->state == Job::COMPILING ? "COMP"
             : job->state == Job::WAITINGFORDONE ? "DONE"
	     : "Huh?",
	   job->submitter ? job->submitter->nodename.c_str() : "<>",
	   job->server ? job->server->nodename.c_str() : "<unknown>");
  buffer[sizeof (buffer) - 1] = 0;
  line = buffer;
  line = line + job->filename;
  return line;
}

/* Splits the string S between characters in SET and add them to list L.  */
static void
split_string (const string &s, const char *set, list<string> &l)
{
  string::size_type end = 0;
  while (end != string::npos)
    {
      string::size_type start = s.find_first_not_of (set, end);
      if (start == string::npos)
        break;
      end = s.find_first_of (set, start);
      /* Do we really need to check end here or is the subtraction
         defined on every platform correctly (with GCC it's ensured,
	 that (npos - start) is the rest of the string).  */
      if (end != string::npos)
        l.push_back (s.substr (start, end - start));
      else
        l.push_back (s.substr (start));
    }
}

static bool
handle_control_login(CS* c)
{
    c->type = CS::LINE;
    c->last_talk = time (0);
    c->setBulkTransfer();
    c->state = CS::LOGGEDIN;
    assert(find(controls.begin(), controls.end(), c) == controls.end());
    controls.push_back(c);

    std::ostringstream o;
    o << "200-ICECC " VERSION ": "
      << time(0) - starttime << "s uptime, "
      << css.size() << " hosts, "
      << jobs.size() << " jobs in queue "
      << "(" << new_job_id << " total)." << endl;
    o << "200 Use 'help' for help and 'quit' to quit." << endl;
    return c->send_msg(TextMsg(o.str()));
}

static bool
handle_line (CS *c, Msg *_m)
{
  TextMsg *m = dynamic_cast<TextMsg *>(_m);
  if (!m)
    return false;

  char buffer[1000];
  string line;
  list<string> l;
  split_string (m->text, " \t\n", l);
  string cmd;

  c->last_talk = time(0);

  if (l.empty())
    cmd = "";
  else
    {
      cmd = l.front();
      l.pop_front();
      transform(cmd.begin(), cmd.end(), cmd.begin(), ::tolower);
    }
  if (cmd == "listcs")
    {
      for (list<CS*>::iterator it = css.begin(); it != css.end(); ++it)
	{
	  CS* cs= *it;
	  sprintf (buffer, " (%s:%d) ", cs->name.c_str(), cs->remote_port);
	  line = " " + cs->nodename + buffer;
	  line += "[" + cs->host_platform + "] speed=";
	  sprintf (buffer, "%.2f jobs=%d/%d load=%d", server_speed (cs),
	  	   (int)cs->joblist.size(), cs->max_jobs, cs->load);
	  line += buffer;
          if (cs->busy_installing)
            {
              sprintf( buffer, " busy installing since %ld s",  time(0) - cs->busy_installing );
              line += buffer;
            }
	  if(!c->send_msg (TextMsg (line)))
            return false;
          for ( list<Job*>::const_iterator it2 = cs->joblist.begin(); it2 != cs->joblist.end(); ++it2 )
            if(!c->send_msg (TextMsg ("   " + dump_job (*it2) ) ))
              return false;
	}
    }
  else if (cmd == "listblocks")
    {
      for (list<string>::const_iterator it = block_css.begin(); it != block_css.end(); ++it)
        if(!c->send_msg (TextMsg ("   " + (*it) ) ))
          return false;
    }
  else if (cmd == "listjobs")
    {
      for (map<unsigned int, Job*>::const_iterator it = jobs.begin();
	   it != jobs.end(); ++it)
	if(!c->send_msg( TextMsg( " " + dump_job (it->second) ) ))
          return false;
    }
  else if (cmd == "quit" || cmd == "exit" )
    {
      handle_end(c, 0);
      return false;
    }
  else if (cmd == "removecs" || cmd == "blockcs")
    {
      if (l.empty()) {
        if(!c->send_msg (TextMsg (string ("401 Sure. But which hosts?"))))
          return false;
      }
      else
        for (list<string>::const_iterator si = l.begin(); si != l.end(); ++si)
	  for (list<CS*>::iterator it = css.begin(); it != css.end(); ++it)
	    if ((*it)->nodename == *si || (*it)->name == *si)
	      {
                if (cmd == "blockcs")
                    block_css.push_back((*it)->name);
                if (c->send_msg (TextMsg (string ("removing host ") + *si)))
                    handle_end ( *it, 0);
		break;
	      }
    }
  else if (cmd == "crashme")
    {
      *(int *)0 = 42;  // ;-)
    }
  else if (cmd == "internals" )
    {
      for (list<CS*>::iterator it = css.begin(); it != css.end(); ++it)
        {
          Msg *msg = NULL;

	  if (!l.empty())
	    {
	      list<string>::const_iterator si;
	      for (si = l.begin(); si != l.end(); ++si) {
	        if ((*it)->nodename == *si || (*it)->name == *si)
		  break;
              }
	      if(si == l.end())
		continue;
	    }

          if(( *it )->send_msg( GetInternalStatus() ))
              msg = ( *it )->get_msg();
          if ( msg && msg->type == M_STATUS_TEXT ) {
            if (!c->send_msg( TextMsg( dynamic_cast<StatusTextMsg*>( msg )->text ) ))
              return false;
          }
          else {
            if (!c->send_msg( TextMsg( ( *it )->nodename + " not reporting\n" ) ))
              return false;
          }
	  delete msg;
        }
    }
  else if (cmd == "help")
    {
      if (!c->send_msg (TextMsg (
        "listcs\nlistblocks\nlistjobs\nremovecs\nblockcs\ninternals\nhelp\nquit")))
        return false;
    }
  else
    {
      string txt = "Invalid command '";
      txt += m->text;
      txt += "'";
      if(!c->send_msg (TextMsg (txt)))
        return false;
    }
  return c->send_msg (TextMsg (string ("200 done")));
}

// return false if some error occured, leaves C open.  */
static bool
try_login (CS *c, Msg *m)
{
  bool ret = true;
  switch (m->type)
    {
    case M_LOGIN:
      c->type = CS::DAEMON;
      ret = handle_login (c, m);
      break;
    case M_MON_LOGIN:
      c->type = CS::MONITOR;
      ret = handle_mon_login (c, m);
      break;
    default:
      log_info() << "Invalid first message " << (char)m->type << endl;
      ret = false;
      break;
    }
  if (ret)
    c->state = CS::LOGGEDIN;
  else
    handle_end (c, m);

  delete m;
  return ret;
}

static bool
handle_end (CS *toremove, Msg *m)
{
#if DEBUG_SCHEDULER > 1
  trace() << "Handle_end " << toremove << " " << m << endl;
#else
  ( void )m;
#endif

  switch (toremove->type) {
  case CS::MONITOR:
    {
      assert (find (monitors.begin(), monitors.end(), toremove) != monitors.end());
      monitors.remove (toremove);
#if DEBUG_SCHEDULER > 1
      trace() << "handle_end(moni) " << monitors.size() << endl;
#endif
    }
    break;
  case CS::DAEMON:
    {
      log_info() << "remove daemon " << toremove->nodename << endl;

      notify_monitors(new  MonStatsMsg( toremove->hostid, "State:Offline\n" ) );

      /* A daemon disconnected.  We must remove it from the css list,
         and we have to delete all jobs scheduled on that daemon.
	 There might be still clients connected running on the machine on which
	 the daemon died.  We expect that the daemon dying makes the client
	 disconnect soon too.  */
      css.remove (toremove);

      /* Unfortunately the toanswer queues are also tagged based on the daemon,
         so we need to clean them up also.  */
      list<UnansweredList*>::iterator it;
      for (it = toanswer.begin(); it != toanswer.end();)
	if ((*it)->server == toremove)
	  {
	    UnansweredList *l = *it;
	    list<Job*>::iterator jit;
	    for (jit = l->l.begin(); jit != l->l.end(); ++jit)
	      {
		trace() << "STOP (DAEMON) FOR " << (*jit)->id << endl;
                notify_monitors (new MonJobDoneMsg ( JobDoneMsg (( *jit )->id,  255 )));
		if ((*jit)->server)
		  (*jit)->server->busy_installing = 0;
		jobs.erase( (*jit)->id );
		delete (*jit);
	      }
	    delete l;
	    it = toanswer.erase (it);
	  }
	else
	  ++it;

      map<unsigned int, Job*>::iterator mit;
      for (mit = jobs.begin(); mit != jobs.end(); )
        {
          Job *job = mit->second;
          if (job->server == toremove || job->submitter == toremove)
            {
              trace() << "STOP (DAEMON2) FOR " << mit->first << endl;
              notify_monitors (new MonJobDoneMsg ( JobDoneMsg( job->id,  255 )));
	      /* If this job is removed because the submitter is removed
		 also remove the job from the servers joblist.  */
	      if (job->server && job->server != toremove)
		job->server->joblist.remove (job);
	      if (job->server)
	        job->server->busy_installing = 0;
              jobs.erase( mit++ );
              delete job;
            }
          else
            {
              ++mit;
            }
        }
    }
    break;
  case CS::LINE:
    {
      if (!toremove->send_msg (TextMsg ("200 Good Bye!"))) {
      }
      controls.remove (toremove);
    }
    break;
  default:
    {
      trace() << "remote end had UNKNOWN type?" << endl;
    }
  }

  fd2cs.erase (toremove->fd);
  delete toremove;
  return true;
}

/* Returns TRUE if C was not closed.  */
static bool
handle_activity (CS *c)
{
  Msg *m;
  bool ret = true;
  m = c->get_msg (0);
  if (!m)
    {
      handle_end (c, m);
      return false;
    }
  /* First we need to login.  */
  if (c->state == CS::CONNECTED)
    return try_login (c, m);

  switch (m->type)
    {
    case M_JOB_BEGIN: ret = handle_job_begin (c, m); break;
    case M_JOB_DONE: ret = handle_job_done (c, m); break;
    case M_PING: ret = handle_ping (c, m); break;
    case M_STATS: ret = handle_stats (c, m); break;
    case M_END: handle_end (c, m); ret = false; break;
    case M_JOB_LOCAL_BEGIN: ret = handle_local_job (c, m); break;
    case M_JOB_LOCAL_DONE: ret = handle_local_job_done( c, m ); break;
    case M_LOGIN: ret = handle_relogin (c, m); break;
    case M_TEXT: ret = handle_line (c, m); break;
    case M_GET_CS: ret = handle_cs_request (c, m); break;
    default:
      log_info() << "Invalid message type arrived " << ( char )m->type << endl;
      handle_end (c, m);
      ret = false;
      break;
    }
  delete m;
  return ret;
}

static int
open_broad_listener ()
{
  int listen_fd;
  struct sockaddr_in myaddr;
  if ((listen_fd = socket (PF_INET, SOCK_DGRAM, 0)) < 0)
    {
      log_perror ("socket()");
      return -1;
    }
  int optval = 1;
  if (setsockopt (listen_fd, SOL_SOCKET, SO_BROADCAST, &optval, sizeof(optval)) < 0)
    {
      log_perror ("setsockopt()");
      return -1;
    }
  myaddr.sin_family = AF_INET;
  myaddr.sin_port = htons (8765);
  myaddr.sin_addr.s_addr = INADDR_ANY;
  if (bind (listen_fd, (struct sockaddr *) &myaddr, sizeof (myaddr)) < 0)
    {
      log_perror ("bind()");
      return -1;
    }
  return listen_fd;
}

static int
open_tcp_listener (short port)
{
  int fd;
  struct sockaddr_in myaddr;
  if ((fd = socket (PF_INET, SOCK_STREAM, 0)) < 0)
    {
      log_perror ("socket()");
      return -1;
    }
  int optval = 1;
  if (setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)) < 0)
    {
      log_perror ("setsockopt()");
      return -1;
    }
  /* Although we select() on fd we need O_NONBLOCK, due to
     possible network errors making accept() block although select() said
     there was some activity.  */
  if (fcntl (fd, F_SETFL, O_NONBLOCK) < 0)
    {
      log_perror ("fcntl()");
      return -1;
    }
  myaddr.sin_family = AF_INET;
  myaddr.sin_port = htons (port);
  myaddr.sin_addr.s_addr = INADDR_ANY;
  if (bind (fd, (struct sockaddr *) &myaddr, sizeof (myaddr)) < 0)
    {
      log_perror ("bind()");
      return -1;
    }
  if (listen (fd, 10) < 0)
    {
      log_perror ("listen()");
      return -1;
    }
  return fd;
}

static void
usage(const char* reason = 0)
{
  if (reason)
     cerr << reason << endl;

  cerr << "ICECREAM scheduler " VERSION "\n";
  cerr << "usage: scheduler [options] \n"
       << "Options:\n"
       << "  -n, --netname <name>\n"
       << "  -p, --port <port>\n"
       << "  -h, --help\n"
       << "  -l, --log-file <file>\n"
       << "  -d, --daemonize\n"
       << "  -v[v[v]]]\n"
       << endl;

  exit(1);
}

static void
trigger_exit( int signum )
{
  if (!exit_main_loop)
    exit_main_loop = true;
  else
    {
      // hmm, we got killed already. try better
      cerr << "forced exit." << endl;
      _exit(1);
    }
  // make BSD happy
  signal(signum, trigger_exit);
}

int
main (int argc, char * argv[])
{
  int listen_fd, remote_fd, broad_fd, text_fd;
  struct sockaddr_in remote_addr;
  unsigned int port = 8765;
  socklen_t remote_len;
  char *netname = (char*)"ICECREAM";
  bool detach = false;
  int debug_level = Error;
  string logfile;

  while ( true ) {
    int option_index = 0;
    static const struct option long_options[] = {
      { "netname", 1, NULL, 'n' },
      { "help", 0, NULL, 'h' },
      { "port", 0, NULL, 'p' },
      { "daemonize", 0, NULL, 'd'},
      { "log-file", 1, NULL, 'l'},
      { 0, 0, 0, 0 }
    };

    const int c = getopt_long( argc, argv, "n:p:hl:vdr", long_options, &option_index );
    if ( c == -1 ) break; // eoo

    switch ( c ) {
    case 0:
      {
        ( void ) long_options[option_index].name;
      }
      break;
    case 'd':
      detach = true;
      break;
    case 'l':
      if ( optarg && *optarg )
        logfile = optarg;
      else
        usage( "Error: -l requires argument" );
      break;
    case 'v':
      if ( debug_level & Warning )
        if ( debug_level & Info ) // for second call
                        debug_level |= Debug;
        else
          debug_level |= Info;
      else
        debug_level |= Warning;
      break;
    case 'n':
      if ( optarg && *optarg )
        netname = optarg;
      else
        usage("Error: -n requires argument");
      break;
    case 'p':
      if ( optarg && *optarg )
        {
          port = 0; port = atoi( optarg );
          if ( 0 == port )
            usage("Error: Invalid port specified");
         }
      else
        usage("Error: -p requires argument");
      break;

    default:
      usage();
    }
  }

  if ( !logfile.size() && detach )
    logfile = "/var/log/icecc_scheduler";

  setup_debug( debug_level, logfile );
  if ( detach )
    daemon( 0, 0 );

  listen_fd = open_tcp_listener (port);
  if (listen_fd < 0)
    return 1;
  text_fd = open_tcp_listener (port + 1);
  if (text_fd < 0)
    return 1;
  broad_fd = open_broad_listener ();
  if (broad_fd < 0)
    return 1;

  if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
    {
      log_warning() << "signal(SIGPIPE, ignore) failed: " << strerror(errno) << endl;
      return 1;
    }

  starttime = time( 0 );

  ofstream pidFile;
  string progName = argv[0];
  progName = progName.substr(progName.rfind('/')+1);
  pidFilePath = string(RUNDIR)+string("/")+progName+string(".pid");
  pidFile.open(pidFilePath.c_str());
  pidFile << getpid() << endl;
  pidFile.close();

  signal(SIGTERM, trigger_exit);
  signal(SIGINT, trigger_exit);
  signal(SIGALRM, trigger_exit);

  time_t next_listen = 0;

  while (!exit_main_loop)
    {
      struct timeval tv;
      tv.tv_usec = 0;
      tv.tv_sec = prune_servers ();

      while (empty_queue())
	continue;

      fd_set read_set;
      int max_fd = 0;
      FD_ZERO (&read_set);
      if (time(0) >= next_listen)
        {
          max_fd = listen_fd;
          FD_SET (listen_fd, &read_set);
          if (text_fd > max_fd)
            max_fd = text_fd;
          FD_SET (text_fd, &read_set);
        }
      if (broad_fd > max_fd)
        max_fd = broad_fd;
      FD_SET (broad_fd, &read_set);
      for (map<int, CS*>::const_iterator it = fd2cs.begin(); it != fd2cs.end();)
        {
          int i = it->first;
          CS *c = it->second;
          bool ok = true;
          ++it;
          /* handle_activity() can delete c and make the iterator
             invalid.  */
          while (ok && c->has_msg ())
            if (!handle_activity (c))
              ok = false;
          if (ok)
            {
              if (i > max_fd)
                max_fd = i;
              FD_SET (i, &read_set);
            }
        }
      max_fd = select (max_fd + 1, &read_set, NULL, NULL, &tv);
      if (max_fd < 0 && errno == EINTR)
        continue;
      if (max_fd < 0)
        {
	  log_perror ("select()");
	  return 1;
	}
      if (FD_ISSET (listen_fd, &read_set))
        {
	  max_fd--;
          bool pending_connections = true;
          while (pending_connections)
            {
              remote_len = sizeof (remote_addr);
              remote_fd = accept (listen_fd,
                                  (struct sockaddr *) &remote_addr,
                                  &remote_len );
              if (remote_fd < 0)
                pending_connections = false;

              if (remote_fd < 0 && errno != EAGAIN && errno != EINTR && errno
                  != EWOULDBLOCK)
                {
                  log_perror ("accept()");
                  /* don't quit because of ECONNABORTED, this can happen during
                   * floods  */
                }
              if (remote_fd >= 0)
                {
                  CS *cs = new CS (remote_fd, (struct sockaddr*) &remote_addr, remote_len, false);
                  trace() << "accepted " << cs->name << endl;
                  cs->last_talk = time( 0 );

                  if ( !cs->protocol ) // protocol mismatch
                    {
                      delete cs;
                      continue;
                    }
                  fd2cs[cs->fd] = cs;
                  while (!cs->read_a_bit () || cs->has_msg ())
                    if(! handle_activity (cs))
                      break;
                }
            }
          next_listen = time(0) + 1;
        }
      if (max_fd && FD_ISSET (text_fd, &read_set))
        {
	  max_fd--;
	  remote_len = sizeof (remote_addr);
          remote_fd = accept (text_fd,
                              (struct sockaddr *) &remote_addr,
                              &remote_len );
	  if (remote_fd < 0 && errno != EAGAIN && errno != EINTR)
	    {
	      log_perror ("accept()");
	      /* Don't quit the scheduler just because a debugger couldn't
	         connect.  */
	    }
	  if (remote_fd >= 0)
	    {
	      CS *cs = new CS (remote_fd, (struct sockaddr*) &remote_addr, remote_len, true);
	      fd2cs[cs->fd] = cs;
              if (!handle_control_login(cs))
                {
                  handle_end(cs, 0);
                  continue;
                }
	      while (!cs->read_a_bit () || cs->has_msg ())
	        if (!handle_activity (cs))
                  break;
	    }
	}
      if (max_fd && FD_ISSET (broad_fd, &read_set))
        {
	  max_fd--;
	  char buf[16];
	  struct sockaddr_in broad_addr;
	  socklen_t broad_len = sizeof (broad_addr);
	  if (recvfrom (broad_fd, buf, 1, 0, (struct sockaddr*) &broad_addr,
			&broad_len) != 1)
	    {
	      int err = errno;
	      log_perror ("recvfrom()");
	      /* Some linux 2.6 kernels can return from select with
	         data available, and then return from read() with EAGAIN
		 even on a blocking socket (breaking POSIX).  Happens
		 when the arriving packet has a wrong checksum.  So
		 we ignore EAGAIN here, but still abort for all other errors. */
	      if (err != EAGAIN)
	        return -1;
	    }
            /* Only answer if daemon would be able to talk to us. */
	  else if (buf[0] >= MIN_PROTOCOL_VERSION)
	    {
	      log_info() << "broadcast from " << inet_ntoa (broad_addr.sin_addr)
                         << ":" << ntohs (broad_addr.sin_port) << "\n";
	      buf[0]++;
	      memset (buf + 1, 0, sizeof (buf) - 1);
	      snprintf (buf + 1, sizeof (buf) - 1, "%s", netname);
	      buf[sizeof (buf) - 1] = 0;
	      if (sendto (broad_fd, buf, sizeof (buf), 0,
	      		  (struct sockaddr*)&broad_addr, broad_len) != sizeof (buf))
		{
		  log_perror ("sendto()");
		}
	    }
	}
      for (map<int, CS*>::const_iterator it = fd2cs.begin();
           max_fd && it != fd2cs.end();)
        {
          int i = it->first;
          CS *c = it->second;
          /* handle_activity can delete the channel from the fd2cs list,
             hence advance the iterator right now, so it doesn't become
             invalid.  */
          ++it;
          if (FD_ISSET (i, &read_set))
            {
              while (!c->read_a_bit () || c->has_msg ())
                if(!handle_activity (c))
                  break;
              max_fd--;
            }
        }
    }
  shutdown (broad_fd, SHUT_RDWR);
  close (broad_fd);
  unlink(pidFilePath.c_str());
  return 0;
}