summaryrefslogtreecommitdiff
path: root/tools/mysqlmanager.c
blob: f2f89c6bc07f6715c8fa7fb5f33b7abb850a5cbd (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
/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB

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

/* MySQL server management daemon
 *
 * Written by:
 *   Sasha Pachev <sasha@mysql.com>
 **/

#include <my_global.h>
#include <my_pthread.h>
#include <mysql.h>
#include <mysql_version.h>
#include <mysqld_error.h>
#include <my_sys.h>
#include <my_dir.h>
#include <m_string.h>
#include <m_ctype.h>
#include <hash.h>
#include <getopt.h>
#include <stdarg.h>
#include <sys/stat.h>
#include <errno.h>
#include <violite.h>
#include <md5.h>
#include <signal.h>
#ifdef HAVE_SYS_WAIT_H
#include <sys/wait.h>
#endif

#define MANAGER_VERSION "1.0"
#define MANAGER_GREETING "MySQL Server Management Daemon v. 1.0" 

#define LOG_ERR  1
#define LOG_WARN 2
#define LOG_INFO 3
#define LOG_DEBUG 4

#define CHILD_START 1
#define CHILD_STOP  2

#ifndef MANAGER_PORT
#define MANAGER_PORT  23546
#endif

#ifndef MANAGER_CONNECT_RETRIES
#define MANAGER_CONNECT_RETRIES 5
#endif

#ifndef MANAGER_MAX_CMD_LEN
#define MANAGER_MAX_CMD_LEN 16384
#endif

#ifndef MANAGER_LOG_FILE
#define MANAGER_LOG_FILE "/var/log/mysqlmanager.log"
#endif

#ifndef MANAGER_BACK_LOG
#define MANAGER_BACK_LOG 50
#endif

#ifndef MAX_USER_NAME
#define MAX_USER_NAME 16
#endif

#ifndef MANAGER_PW_FILE
#define MANAGER_PW_FILE "/etc/mysqlmanager.passwd"
#endif

#ifndef MAX_HOST
#define MAX_HOST 128
#endif

#ifndef MAX_LAUNCHER_MSG
#define MAX_LAUNCHER_MSG 256
#endif

/* Variable naming convention - if starts with manager_, either is set
   directly by the user, or used closely in ocnjunction with a variable
   set by the user
*/

#if defined(__i386__) && defined(HAVE_LINUXTHREADS)
#define DO_STACKTRACE 1
#endif

uint manager_port = MANAGER_PORT;
FILE* errfp;
const char* manager_log_file = MANAGER_LOG_FILE;
pthread_mutex_t lock_log,lock_shutdown,lock_exec_hash,lock_launch_thd;
pthread_cond_t cond_launch_thd;
pthread_t loop_th,launch_msg_th;
int manager_sock = -1;
uchar* stack_bottom=0;
struct sockaddr_in manager_addr;
ulong manager_bind_addr = INADDR_ANY;
int manager_back_log = MANAGER_BACK_LOG;
int in_shutdown = 0, shutdown_requested=0;
int manager_connect_retries=MANAGER_CONNECT_RETRIES;
const char* manager_greeting = MANAGER_GREETING;
uint manager_max_cmd_len = MANAGER_MAX_CMD_LEN;
const char* manager_pw_file=MANAGER_PW_FILE;
int one_thread = 0; /* for debugging */

typedef enum {PARAM_STDOUT,PARAM_STDERR} PARAM_TYPE;

/* messages */

#define MAX_CLIENT_MSG_LEN  256
#define NET_BLOCK    2048
#define MD5_LEN      16
#define ESCAPE_CHAR '\\'
#define EOL_CHAR '\n'

/* access flags */

#define PRIV_SHUTDOWN 1

struct manager_thd
{
  Vio* vio;
  char user[MAX_USER_NAME+1];
  int priv_flags;
  char* cmd_buf;
  int fatal,finished;
};

struct manager_user
{
  char user[MAX_USER_NAME+1];
  char md5_pass[MD5_LEN];
  int user_len;
  const char* error;
};

HASH exec_hash,user_hash;
struct manager_exec* cur_launch_exec=0;

static struct manager_thd* manager_thd_new(Vio* vio);

static struct manager_exec* manager_exec_new(char* arg_start,char* arg_end);
static void manager_exec_print(Vio* vio,struct manager_exec* e);
static void manager_thd_free(struct manager_thd* thd);
static void manager_exec_free(void* e);
static void manager_exec_connect(struct manager_exec* e);
static int manager_exec_launch(struct manager_exec* e);
static struct manager_exec* manager_exec_by_pid(pid_t pid);

static struct manager_user* manager_user_new(char* buf);
static void manager_user_free(void* u);

static char* arg_strmov(char* dest, const char* src, int n);
static byte* get_exec_key(const byte* e, uint* len,
			  my_bool __attribute__((unused)) t);
static byte* get_user_key(const byte* u, uint* len,
			  my_bool __attribute__((unused)) t);
static uint tokenize_args(char* arg_start,char** arg_end);
static void init_arg_array(char* arg_str,char** args,uint arg_count);
static int hex_val(char c);
static int open_and_dup(int fd,char* path);
static void update_req_len(struct manager_exec* e);

typedef int (*manager_cmd_handler)(struct manager_thd*,char*,char*);

static void handle_child(int __attribute__((unused)) sig);
static void handle_sigpipe(int __attribute__((unused)) sig);

/* exec() in a threaded application is full of problems
   to solve this, we fork off a launcher at the very start
   and communicate with it through a pipe
*/
static void fork_launcher();
static void run_launcher_loop();
int to_launcher_pipe[2],from_launcher_pipe[2];
pid_t launcher_pid;
int in_segfault=0;

struct manager_cmd
{
  const char* name;
  const char* help;
  manager_cmd_handler handler_func;
  int len;
};

struct manager_exec
{
  char* ident;
  int ident_len;
  const char* error;
  char* bin_path;
  char** args;
  char con_user[16];
  char con_pass[16];
  int con_port;
  pid_t pid;
  int exit_code;
  pthread_mutex_t lock;
  pthread_cond_t cond;
  pthread_t th;
  char con_sock[FN_REFLEN];
  char con_host[MAX_HOST];
  char stderr_path[FN_REFLEN];
  char stdout_path[FN_REFLEN];
  MYSQL mysql;
  char* data_buf;
  int req_len;
  int start_wait_timeout;
  int stderr_path_size,stdout_path_size,data_buf_size;
  int num_args;
};

static int set_exec_param(struct manager_thd* thd, char* args_start,
			  char* args_end, PARAM_TYPE param_type);

#define HANDLE_DECL(com) static int com (struct manager_thd* thd, char* args_start,char* args_end)
#define HANDLE_NOARG_DECL(com) static int com \
  (struct manager_thd* thd, char* __attribute__((unused)) args_start,\
 char* __attribute__((unused)) args_end)


HANDLE_NOARG_DECL(handle_ping);
HANDLE_NOARG_DECL(handle_quit);
HANDLE_NOARG_DECL(handle_help);
HANDLE_NOARG_DECL(handle_shutdown);
HANDLE_DECL(handle_def_exec);
HANDLE_DECL(handle_start_exec);
HANDLE_DECL(handle_stop_exec);
HANDLE_DECL(handle_set_exec_con);
HANDLE_DECL(handle_set_exec_stdout);
HANDLE_DECL(handle_set_exec_stderr);
HANDLE_NOARG_DECL(handle_show_exec);
HANDLE_DECL(handle_query);


struct manager_cmd commands[] =
{
  {"ping", "Check if this server is alive", handle_ping,4},
  {"quit", "Finish session", handle_quit,4},
  {"shutdown", "Shutdown this server", handle_shutdown,8},
  {"def_exec", "Define executable entry", handle_def_exec,8},
  {"start_exec", "Launch process defined by executable entry",
   handle_start_exec,10},
  {"stop_exec", "Stop process defined by executable entry",
   handle_stop_exec,9},
  {"set_exec_con", "Set connection parameters for executable entry",
   handle_set_exec_con,12},
  {"set_exec_stdout", "Set stdout path for executable entry",
   handle_set_exec_stdout,15},
  {"set_exec_stderr", "Set stderr path for executable entry",
   handle_set_exec_stderr,15},
  {"query","Run query against MySQL server",handle_query,5},
  {"show_exec","Show defined executable entries",handle_show_exec,9},
  {"help", "Print this message", handle_help,4},
  {0,0,0,0}
};

struct option long_options[] =
{
  {"debug", optional_argument, 0, '#'},
  {"help",  no_argument, 0, 'h'},
  {"port",  required_argument, 0, 'P'},
  {"log",  required_argument, 0, 'l'},
  {"bind-address", required_argument, 0, 'b'},
  {"tcp-backlog", required_argument, 0, 'B'},
  {"greeting", required_argument, 0, 'g'},
  {"max-command-len",required_argument,0,'m'},
  {"one-thread",no_argument,0,'d'},
  {"connect-retries",required_argument,0,'C'},
  {"password-file",required_argument,0,'p'},
  {"version", no_argument, 0, 'V'},
  {0, 0, 0, 0}
};

static void die(const char* fmt,...);
static void print_time(FILE* fp);
static void clean_up();
static struct manager_cmd* lookup_cmd(char* s,int len);
static void client_msg(Vio* vio,int err_code,const char* fmt,...);
static void client_msg_pre(Vio* vio,int err_code,const char* fmt,...);
static void client_msg_raw(Vio* vio,int err_code,int pre,const char* fmt,
			    va_list args);
static int authenticate(struct manager_thd* thd);
static char* read_line(struct manager_thd* thd); /* returns pointer to end of
						 line
					      */
static pthread_handler_decl(process_connection, arg);
static pthread_handler_decl(process_launcher_messages, arg);
static int exec_line(struct manager_thd* thd,char* buf,char* buf_end);

#ifdef DO_STACKTRACE
void print_stacktrace();
#endif

static void handle_segfault(int sig)
{
  if (in_segfault)
    exit(1);
  in_segfault=1;
  fprintf(errfp,"Got fatal signal %d\n",sig);
#ifdef DO_STACKTRACE
  print_stacktrace();
#endif
  exit(1);
}

static void handle_sigpipe(int __attribute__((unused)) sig)
{
  signal(SIGPIPE,handle_sigpipe);
}

#ifdef DO_STACKTRACE

#define MAX_DEPTH 25
#define SIGRETURN_FRAME_COUNT 1

void print_stacktrace()
{
  uchar** fp;
  int i;
  LINT_INIT(fp);
  fprintf(errfp,"Fatal errror, stacktrace follows:\n");
#ifdef __i386__  
  __asm__ __volatile__("movl %%ebp,%0" :"=r"(fp) :"r"(fp));
#endif
  if (!fp)
  {
    fprintf(errfp,"frame points is NULL, cannot trace stack\n");
    return;
  }
  for(i=0;i<MAX_DEPTH && fp<(uchar**)stack_bottom;i++)
  {
#ifdef __i386__    
    uchar** new_fp = (uchar**)*fp;
    fprintf(errfp, "%p\n", i == SIGRETURN_FRAME_COUNT ?
	    *(fp+17) : *(fp+1));
#endif /* __386__ */
    if (new_fp <= fp )
    {
      fprintf(errfp, "New value of fp=%p failed sanity check,\
 terminating stack trace!\n", new_fp);
      return;
    }
    fp = new_fp;
  }
  fprintf(errfp,"Stack trace successful\n");
  fflush(errfp);
}
#endif

static int exec_line(struct manager_thd* thd,char* buf,char* buf_end)
{
  char* p=buf;
  struct manager_cmd* cmd;
  for (;p<buf_end && !isspace(*p);p++)
    *p=tolower(*p);
  if (!(cmd=lookup_cmd(buf,(int)(p-buf))))
  {
    client_msg(thd->vio,MANAGER_CLIENT_ERR,
	       "Unrecognized command, type help to see list of supported\
 commands");
    return 1;
  }
  for (;p<buf_end && isspace(*p);p++);
  return cmd->handler_func(thd,p,buf_end);
}

static struct manager_cmd* lookup_cmd(char* s,int len)
{
  struct manager_cmd* cmd = commands;
  for (;cmd->name;cmd++)
  {
    if (cmd->len == len && !memcmp(cmd->name,s,len))
      return cmd;
  }
  return 0;
}

HANDLE_NOARG_DECL(handle_ping)
{
  client_msg(thd->vio,MANAGER_OK,"Server management daemon is alive");
  return 0;
}

HANDLE_NOARG_DECL(handle_quit)
{
  client_msg(thd->vio,MANAGER_OK,"Goodbye");
  thd->finished=1;
  return 0;
}

HANDLE_NOARG_DECL(handle_help)
{
  struct manager_cmd* cmd = commands;
  Vio* vio = thd->vio;
  client_msg_pre(vio,MANAGER_INFO,"Available commands:");
  for (;cmd->name;cmd++)
  {
    client_msg_pre(vio,MANAGER_INFO,"%s - %s", cmd->name, cmd->help);
  }
  client_msg_pre(vio,MANAGER_INFO,"End of help");
  return 0;
}

HANDLE_NOARG_DECL(handle_shutdown)
{
  client_msg(thd->vio,MANAGER_OK,"Shutdown started, goodbye");
  thd->finished=1;
  shutdown_requested = 1;
  if (!one_thread)
  {
    kill(launcher_pid,SIGTERM);
    pthread_kill(loop_th,SIGTERM);
  }
  return 0;
}

HANDLE_DECL(handle_set_exec_con)
{
  int num_args;
  const char* error=0;
  struct manager_exec* e;
  char* arg_p;
  if ((num_args=tokenize_args(args_start,&args_end))<2)
  {
    error="Too few arguments";
    goto err;
  }
  arg_p=args_start;
  pthread_mutex_lock(&lock_exec_hash);
  if (!(e=(struct manager_exec*)hash_search(&exec_hash,arg_p,
					    strlen(arg_p))))
  {
    pthread_mutex_unlock(&lock_exec_hash);
    error="Exec definition entry does not exist";
    goto err;
  }
  arg_p+=strlen(arg_p)+1;
  arg_p+=(strnmov(e->con_user,arg_p,sizeof(e->con_user))-e->con_user)+1;
  if (num_args >= 3)
  {
    arg_p+=(strnmov(e->con_host,arg_p,sizeof(e->con_host))-e->con_host)+1;
    if (num_args == 4)
    {
      if (!(e->con_port=atoi(arg_p)))
	strnmov(e->con_sock,arg_p,sizeof(e->con_sock));
      else
	e->con_sock[0]=0;
    }
    else if(num_args > 4)
    {
      pthread_mutex_unlock(&lock_exec_hash);
      error="Too many arguments";
      goto err;
    }
  }
  pthread_mutex_unlock(&lock_exec_hash);
  client_msg(thd->vio,MANAGER_OK,"Entry updated");
  return 0;
err:
  client_msg(thd->vio,MANAGER_CLIENT_ERR,error);
  return 1;
}

HANDLE_DECL(handle_set_exec_stdout)
{
  return set_exec_param(thd,args_start,args_end,PARAM_STDOUT);
}

HANDLE_DECL(handle_set_exec_stderr)
{
  return set_exec_param(thd,args_start,args_end,PARAM_STDERR);
}

static int set_exec_param(struct manager_thd* thd, char* args_start,
			  char* args_end, PARAM_TYPE param_type)
{
  int num_args;
  const char* error=0;
  struct manager_exec* e;
  char* arg_p;
  char* param;
  int param_size;
  
  if ((num_args=tokenize_args(args_start,&args_end))<2)
  {
    error="Too few arguments";
    goto err;
  }
  arg_p=args_start;
  pthread_mutex_lock(&lock_exec_hash);
  if (!(e=(struct manager_exec*)hash_search(&exec_hash,arg_p,
					    strlen(arg_p))))
  {
    pthread_mutex_unlock(&lock_exec_hash);
    error="Exec definition entry does not exist";
    goto err;
  }
  arg_p+=strlen(arg_p)+1;
  param_size=strlen(arg_p)+1;
  switch (param_type)
  {
  case PARAM_STDOUT:
    param=e->stdout_path;
    e->req_len+=(param_size-e->stdout_path_size);
    e->stdout_path_size=param_size;
    break;
  case PARAM_STDERR:
    param=e->stderr_path;
    e->req_len+=(param_size-e->stderr_path_size);
    e->stderr_path_size=param_size;
    break;
  default:
    error="Internal error";
    goto err;
  }
  strnmov(param,arg_p,FN_REFLEN);
  pthread_mutex_unlock(&lock_exec_hash);
  client_msg(thd->vio,MANAGER_OK,"Entry updated");
  return 0;
err:
  client_msg(thd->vio,MANAGER_CLIENT_ERR,error);
  return 1;
}


HANDLE_DECL(handle_start_exec)
{
  int num_args;
  struct manager_exec* e;
  int ident_len;
  const char* error=0;
  struct timespec t;
  if ((num_args=tokenize_args(args_start,&args_end))<1)
  {
    error="Too few arguments";
    goto err;
  }
  ident_len=strlen(args_start);
  pthread_mutex_lock(&lock_exec_hash);
  if (!(e=(struct manager_exec*)hash_search(&exec_hash,args_start,
					    ident_len)))
  {
    pthread_mutex_unlock(&lock_exec_hash);
    error="Exec definition entry does not exist";
    goto err;
  }
  pthread_mutex_unlock(&lock_exec_hash);
  manager_exec_launch(e);
  if ((error=e->error))
    goto err;
  pthread_mutex_lock(&e->lock);
  t.tv_sec=time(0)+(e->start_wait_timeout=atoi(args_start+ident_len+1));
  t.tv_nsec=0;
  if (!e->pid)
    pthread_cond_timedwait(&e->cond,&e->lock,&t);
  if (!e->pid)
  {
    pthread_mutex_unlock(&e->lock);
    error="Process failed to start withing alotted time";
    goto err;
  }
  mysql_close(&e->mysql);
  manager_exec_connect(e);
  error=e->error;
  pthread_mutex_unlock(&e->lock);
  if (error)
    goto err;
  client_msg(thd->vio,MANAGER_OK,"'%s' started",e->ident);
  return 0;
err:
  client_msg(thd->vio,MANAGER_CLIENT_ERR,error);
  return 1;
}

HANDLE_DECL(handle_stop_exec)
{
  int num_args;
  struct timespec abstime;
  struct manager_exec* e;
  int ident_len;
  const char* error=0;
  if ((num_args=tokenize_args(args_start,&args_end))<2)
  {
    error="Too few arguments";
    goto err;
  }
  ident_len=strlen(args_start);
  abstime.tv_sec=time(0)+atoi(args_start+1+ident_len);
  abstime.tv_nsec=0;
  pthread_mutex_lock(&lock_exec_hash);
  if (!(e=(struct manager_exec*)hash_search(&exec_hash,args_start,
					    ident_len)))
  {
    pthread_mutex_unlock(&lock_exec_hash);
    error="Exec definition entry does not exist";
    goto err;
  }
  pthread_mutex_unlock(&lock_exec_hash);
  pthread_mutex_lock(&e->lock);
  e->th=pthread_self();
  if (!e->pid)
  {
    e->th=0;
    pthread_mutex_unlock(&e->lock);
    error="Process not running";
    goto err;
  }
  if (mysql_shutdown(&e->mysql))
  {
    e->th=0;
    pthread_mutex_unlock(&e->lock);
    error="Could not send shutdown command";
    goto err;
  }
  if (e->pid)
    pthread_cond_timedwait(&e->cond,&e->lock,&abstime);
  if (e->pid)
    error="Process failed to terminate within alotted time";
  e->th=0;
  pthread_mutex_unlock(&e->lock);
  if (!error)
  {
    client_msg(thd->vio,MANAGER_OK,"'%s' terminated",e->ident);
    return 0;
  }
err:
  client_msg(thd->vio,MANAGER_CLIENT_ERR,error);
  return 1;
}

HANDLE_DECL(handle_query)
{
  const char* error=0;
  struct manager_exec* e;
  MYSQL_RES* res=0;
  MYSQL_ROW row;
  MYSQL_FIELD* fields;
  int num_fields,i,ident_len;
  char* ident,*query;
  query=ident=args_start;
  while (!isspace(*query))
    query++;
  if (query == ident)
  {
    error="Missing server identifier";
    goto err;
  }
  ident_len=(int)(query-ident);
  while (query<args_end && isspace(*query))
    query++;
  if (query == args_end)
  {
    error="Missing query";
    goto err;
  }
  pthread_mutex_lock(&lock_exec_hash);
  if (!(e=(struct manager_exec*)hash_search(&exec_hash,ident,
					    ident_len)))
  {
    pthread_mutex_unlock(&lock_exec_hash);
    error="Exec definition entry does not exist";
    goto err;
  }
  pthread_mutex_unlock(&lock_exec_hash);
  pthread_mutex_lock(&e->lock);
  if (!e->pid)
  {
    error="Process is not running";
    pthread_mutex_unlock(&e->lock);
    goto err;
  }
  
  if (mysql_query(&e->mysql,query))
  {
    error=mysql_error(&e->mysql);
    pthread_mutex_unlock(&e->lock);
    goto err;
  }
  if ((res=mysql_store_result(&e->mysql)))
  {
    char buf[MAX_CLIENT_MSG_LEN],*p,*buf_end;
    fields=mysql_fetch_fields(res);
    num_fields=mysql_num_fields(res);
    p=buf;
    buf_end=buf+sizeof(buf);
    for (i=0;i<num_fields && p<buf_end-2;i++)
    {
      p=arg_strmov(p,fields[i].name,buf_end-p-2);
      *p++='\t';
    }
    *p=0;
    client_msg_pre(thd->vio,MANAGER_OK,buf);
    
    while ((row=mysql_fetch_row(res)))
    {
      p=buf;
      for (i=0;i<num_fields && p<buf_end-2;i++)
      {
	p=arg_strmov(p,row[i],buf_end-p-2);
	*p++='\t';
      }
      *p=0;
      client_msg_pre(thd->vio,MANAGER_OK,buf);
    }
  }
  pthread_mutex_unlock(&e->lock);
  client_msg(thd->vio,MANAGER_OK,"End");
  return 0;
err:
  client_msg(thd->vio,MANAGER_CLIENT_ERR,error);
  return 1;
}

HANDLE_DECL(handle_def_exec)
{
  struct manager_exec* e=0,*old_e;
  const char* error=0;
  if (!(e=manager_exec_new(args_start,args_end)))
  {
    error="Out of memory";
    goto err;
  }
  if (e->error)
  {
    error=e->error;
    goto err;
  }
  pthread_mutex_lock(&lock_exec_hash);
  if ((old_e=(struct manager_exec*)hash_search(&exec_hash,(byte*)e->ident,
					       e->ident_len)))
  {
    strnmov(e->stdout_path,old_e->stdout_path,sizeof(e->stdout_path));
    strnmov(e->stderr_path,old_e->stderr_path,sizeof(e->stderr_path));
    strnmov(e->con_user,old_e->con_user,sizeof(e->con_user));
    strnmov(e->con_host,old_e->con_host,sizeof(e->con_host));
    strnmov(e->con_sock,old_e->con_sock,sizeof(e->con_sock));
    e->con_port=old_e->con_port;
    update_req_len(e);
    hash_delete(&exec_hash,(byte*)old_e);
  }
  hash_insert(&exec_hash,(byte*)e);
  pthread_mutex_unlock(&lock_exec_hash);
  client_msg(thd->vio,MANAGER_OK,"Exec definition created");
  return 0;
err:
  client_msg(thd->vio,MANAGER_CLIENT_ERR,error);
  if (e)
    manager_exec_free(e);
  return 1;
}

HANDLE_NOARG_DECL(handle_show_exec)
{
  uint i;
  client_msg_pre(thd->vio,MANAGER_INFO,"Exec_def\tPid\tExit_status\tCon_info\
\tStdout\tStderr\tArguments");
  pthread_mutex_lock(&lock_exec_hash);
  for (i=0;i<exec_hash.records;i++)
  {
    struct manager_exec* e=(struct manager_exec*)hash_element(&exec_hash,i);
    manager_exec_print(thd->vio,e);
  }
  pthread_mutex_unlock(&lock_exec_hash);
  client_msg(thd->vio,MANAGER_INFO,"End");
  return 0;
}

static struct manager_exec* manager_exec_by_pid(pid_t pid)
{
  struct manager_exec* e;
  uint i;
  pthread_mutex_lock(&lock_exec_hash);
  for (i=0;i<exec_hash.records;i++)
  {
    e=(struct manager_exec*)hash_element(&exec_hash,i);
    if (e->pid==pid)
    {
      pthread_mutex_unlock(&lock_exec_hash);
      return e;
    }
  }
 pthread_mutex_unlock(&lock_exec_hash);
 return 0;					 
}

static void manager_exec_connect(struct manager_exec* e)
{
  int i;
  int connect_retries;
  
  if (!(connect_retries=e->start_wait_timeout))
    connect_retries=manager_connect_retries;
  
  for (i=0;i<connect_retries;i++)
  {
    if (mysql_real_connect(&e->mysql,e->con_host,e->con_user,e->con_pass,0,
			   e->con_port,e->con_sock,0))
      return;
    sleep(1);
  }
  e->error="Could not connect to MySQL server withing the number of tries";
}

static int manager_exec_launch(struct manager_exec* e)
{
  if (one_thread)
  {
    pid_t tmp_pid;
    switch ((tmp_pid=fork()))
    {
    case -1:
      e->error="Cannot fork";
      return 1;
    case 0:
    {
      int err_code;
      close(manager_sock);
      err_code=execv(e->bin_path,e->args);
      exit(err_code);
    }
    default:
      e->pid=tmp_pid;
      manager_exec_connect(e);
      return 0;
    }
  }
  else
  {
    if (my_write(to_launcher_pipe[1],(byte*)&e->req_len,
		 sizeof(int),MYF(MY_NABP))||
	my_write(to_launcher_pipe[1],(byte*)&e->num_args,
		 sizeof(int),MYF(MY_NABP)) ||
	my_write(to_launcher_pipe[1],e->stdout_path,e->stdout_path_size,
		 MYF(MY_NABP)) ||
	my_write(to_launcher_pipe[1],e->stderr_path,e->stderr_path_size,
		 MYF(MY_NABP)) ||
	my_write(to_launcher_pipe[1],e->data_buf,e->data_buf_size,
		 MYF(MY_NABP)))
    {
      e->error="Failed write request to launcher";
      return 1;
    }
  }
  return 0;
}

static char* arg_strmov(char* dest, const char* src, int n)
{
  char* dest_end = dest+n-1;
  char c;
  for (;dest<dest_end && (c=*src++);)
  {
    if (c=='%')
      *dest++='%';
    *dest++=c;
  }
  return dest;
}

static void manager_exec_print(Vio* vio,struct manager_exec* e)
{
  char buf[MAX_MYSQL_MANAGER_MSG];
  char* p=buf,*buf_end=buf+sizeof(buf)-1;
  char** args=e->args;
  
  p=arg_strmov(p,e->ident,(int)(buf_end-p)-1);
  *p++='\t';
  if (p>buf_end-15)
    goto end;
  p=int10_to_str(e->pid,p,10);
  *p++='\t';
  p=int10_to_str(e->exit_code,p,10);
  *p++='\t';
  
  p=arg_strmov(p,e->con_user,(int)(buf_end-p)-1);
  *p++='@';
  if (p==buf_end)
    goto end;
  p=arg_strmov(p,e->con_host,(int)(buf_end-p)-11);
  *p++=':';
  if (p==buf_end-10)
    goto end;
  if (e->con_sock[0])
  {
    p=arg_strmov(p,e->con_sock,(int)(buf_end-p)-1);
  }
  else
  {
    p=int10_to_str(e->con_port,p,10);
  }
  *p++='\t';
  p=arg_strmov(p,e->stdout_path,(int)(buf_end-p)-1);
  if (p==buf_end-1)
    goto end;
  *p++='\t';
  p=arg_strmov(p,e->stderr_path,(int)(buf_end-p)-1);
  if (p==buf_end-1)
    goto end;
  *p++='\t';
  
  for(;p<buf_end && *args;args++)
  {
    p=arg_strmov(p,*args,(int)(buf_end-p)-1);
    *p++='\t';
  }
end:  
  *p=0;
  client_msg_pre(vio,MANAGER_INFO,buf);
  return;
}

static int authenticate(struct manager_thd* thd)
{
  char* buf_end,*buf,*p,*p_end;
  my_MD5_CTX context;
  uchar digest[MD5_LEN];
  struct manager_user* u;
  char c;
  
  client_msg(thd->vio,MANAGER_INFO, manager_greeting);
  if (!(buf_end=read_line(thd)))
    return -1;
  for (buf=thd->cmd_buf,p=thd->user,p_end=p+MAX_USER_NAME;
       buf<buf_end && (c=*buf) && p<p_end; buf++,p++)
  {
    if (isspace(c))
    {
      *p=0;
      break;
    }
    else
      *p=c;
  }
  if (p==p_end || buf==buf_end)
    return 1;
  if (!(u=(struct manager_user*)hash_search(&user_hash,thd->user,
					    (uint)(p-thd->user))))
    return 1;
  for (;isspace(*buf) && buf<buf_end;buf++) /* empty */;
  
  my_MD5Init(&context);
  my_MD5Update(&context,buf,(uint)(buf_end-buf));
  my_MD5Final(digest,&context);
  if (memcmp(u->md5_pass,digest,MD5_LEN))
    return 1;
  client_msg(thd->vio,MANAGER_OK,"OK");
  return 0;
}

static void print_time(FILE* fp)
{
  struct tm now;
  time_t t;
  time(&t);
  localtime_r(&t,&now);
  fprintf(fp,"[%d-%02d-%02d %02d:%02d:%02d] ", now.tm_year+1900,
	  now.tm_mon+1,now.tm_mday,now.tm_hour,now.tm_min,
	  now.tm_sec);
}

static void die(const char* fmt, ...)
{
  va_list args;
  va_start(args,fmt);
  if (fmt)
  {
    if (errfp==stderr)
      fprintf(errfp, "%s: ", my_progname);
    else
      {
	print_time(errfp);
	fprintf(errfp,"Fatal error: ");
      }
    vfprintf(errfp, fmt, args);
    if (errno)
      fprintf(errfp, " errno=%d", errno);
    fprintf(errfp, "\n");
    fflush(errfp);
  }
  va_end(args);
  clean_up();
  exit(1);
}

void print_msg_type(int msg_type)
{
  const char* msg;
  switch (msg_type)
  {
  case LOG_ERR: msg = "ERROR"; break;
  case LOG_WARN: msg = "WARNING"; break;
  case LOG_INFO: msg = "INFO"; break;
#ifndef DBUG_OFF    
  case LOG_DEBUG: msg = "DEBUG"; break;
#endif    
  default: msg = "UNKNOWN TYPE"; break;
  }
  fprintf(errfp," %s: ", msg); 
}

static void log_msg(const char* fmt, int msg_type, va_list args)
{
  pthread_mutex_lock(&lock_log);
  print_time(errfp);
  print_msg_type(msg_type);
  vfprintf(errfp,fmt,args);
  fputc('\n',errfp);
  fflush(errfp);
  pthread_mutex_unlock(&lock_log);
}

/* No 'inline' here becasue functions with ... can't do that portable */
#define LOG_MSG_FUNC(type,TYPE) static void type  \
 (const char* fmt,...) { \
  va_list args; \
  va_start(args,fmt); \
  log_msg(fmt,TYPE,args);\
 }

LOG_MSG_FUNC(log_err,LOG_ERR)
LOG_MSG_FUNC(log_warn,LOG_WARN)
LOG_MSG_FUNC(log_info,LOG_INFO)

#ifndef DBUG_OFF
LOG_MSG_FUNC(log_debug,LOG_DEBUG)
#else
void log_debug(const char* __attribute__((unused)) fmt,...) {}
#endif

static pthread_handler_decl(process_launcher_messages,
			    __attribute__((unused)) arg)
{
  my_thread_init();
  for (;!in_shutdown;)
  {
    pid_t pid;
    struct manager_exec* e;
    char buf[MAX_LAUNCHER_MSG];
    if (read(from_launcher_pipe[0],buf,MAX_LAUNCHER_MSG)<0)
    {
      log_err("error reading launcher message");
      sleep(1);
      continue;
    }
    switch (buf[0])
    {
    case CHILD_START:
    {
      char* ident=buf+1;
      int ident_len=strlen(ident);
      memcpy(&pid,ident+ident_len+1,sizeof(pid));
      log_debug("process message - ident=%s  ident_len=%d  pid=%d",ident,
		ident_len,pid);
      pthread_mutex_lock(&lock_exec_hash);
      log_debug("hash has %d records",exec_hash.records);
      e=(struct manager_exec*)hash_search(&exec_hash,ident,ident_len);
      if (e)
      {
	pthread_mutex_lock(&e->lock);
	e->pid=pid;
	pthread_cond_broadcast(&e->cond);
	pthread_mutex_unlock(&e->lock);
      }
      pthread_mutex_unlock(&lock_exec_hash);
      log_debug("unlocked mutex");
      break;
    }
    case CHILD_STOP:
      memcpy(&pid,buf+1,sizeof(pid));
      e=manager_exec_by_pid(pid);
      if (e)
      {
	pthread_mutex_lock(&e->lock);
	e->pid=0;
	memcpy(&e->exit_code,buf+1+sizeof(pid),sizeof(int));
	pthread_cond_broadcast(&e->cond);
	pthread_mutex_unlock(&e->lock);
      }
      break;
    default:
      log_err("Got invalid launcher message");
      break;
    }
  }
  return 0;
}

static pthread_handler_decl(process_connection,arg)
{
  struct manager_thd* thd = (struct manager_thd*)arg;
  my_thread_init();
  pthread_detach_this_thread();
  for (;!thd->finished;)
  {
    char* buf_end;
    if ((!(buf_end=read_line(thd)) || exec_line(thd,thd->cmd_buf,buf_end))
	&& thd->fatal)
    {
      log_err("Thread aborted");
      break;
    }
  }
  manager_thd_free(thd);
  pthread_exit(0);
  return 0;					/* Don't get cc warning */
}

static void client_msg_raw(Vio* vio, int err_code, int pre, const char* fmt,
			   va_list args)
{
  char buf[MAX_CLIENT_MSG_LEN],*p,*buf_end;
  p=buf;
  buf_end=buf+sizeof(buf);
  p=int10_to_str(err_code,p,10);
  if (pre)
    *p++='-';
  *p++=' ';
  p+=my_vsnprintf(p,buf_end-p,fmt,args);
  if (p>buf_end-2)
    p=buf_end - 2;
  *p++='\r';
  *p++='\n';
  if (vio_write(vio,buf,(uint)(p-buf))<=0)
    log_err("Failed writing to client: errno=%d",errno);
}

static void client_msg(Vio* vio, int err_code, const char* fmt, ...)
{
  va_list args;
  va_start(args,fmt);
  client_msg_raw(vio,err_code,0,fmt,args);
}

static void client_msg_pre(Vio* vio, int err_code, const char* fmt, ...)
{
  va_list args;
  va_start(args,fmt);
  client_msg_raw(vio,err_code,1,fmt,args);
}

static char* read_line(struct manager_thd* thd)
{
  char* p=thd->cmd_buf;
  char* buf_end = thd->cmd_buf + manager_max_cmd_len;
  int escaped = 0;
  for (;p<buf_end;)
  {
    int len,read_len;
    char *block_end,*p_back;
    read_len = min(NET_BLOCK,(uint)(buf_end-p));
    if ((len=vio_read(thd->vio,p,read_len))<=0)
    {
      log_err("Error reading command from client");
      thd->fatal=1;
      return 0;
    }
    block_end=p+len;
    /* a trick to unescape in place */
    for (p_back=p;p<block_end;p++)
    {
      char c=*p;
      if (c==ESCAPE_CHAR)
      {
	if (!escaped)
	{
	  escaped=1;
	  continue;
	}
	else
	  escaped=0;
      }
      if (c==EOL_CHAR && !escaped)
	break;
      *p_back++=c;
      escaped=0;
    }
    if (p!=block_end)
    {
      *p_back=0;
      return p_back;
    }
  }
  client_msg(thd->vio,MANAGER_CLIENT_ERR,"Command line too long");
  return 0;
}

static void handle_child(int __attribute__((unused)) sig)
{
  pid_t child;
  int child_status;
  
  for(;(child=waitpid(-1,&child_status,WNOHANG))>0;)
  {
    char msg_buf[1+sizeof(int)+sizeof(int)];
    msg_buf[0]=CHILD_STOP;
    memcpy(msg_buf+1,&child,sizeof(int));
    memcpy(msg_buf+1+sizeof(int),&child_status,sizeof(int));
    if (write(from_launcher_pipe[1],msg_buf,sizeof(msg_buf))!=sizeof(msg_buf))
      log_err("launcher: error writing message on child exit"); 
  }
  signal(SIGCHLD,handle_child);
}

struct manager_thd* manager_thd_new(Vio* vio)
{
  struct manager_thd* tmp;
  if (!(tmp=(struct manager_thd*)my_malloc(sizeof(*tmp)+manager_max_cmd_len,
					MYF(0))))
  {
    log_err("Out of memory in manager_thd_new");
    return 0;
  }
  tmp->vio=vio;
  tmp->user[0]=0;
  tmp->priv_flags=0;
  tmp->fatal=tmp->finished=0;
  tmp->cmd_buf=(char*)tmp+sizeof(*tmp);
  return tmp;
}

static void manager_thd_free(struct manager_thd* thd)
{
  if (thd->vio)
    vio_close(thd->vio);
  my_free((byte*)thd->vio,MYF(0));
}

static void clean_up()
{
  pthread_mutex_lock(&lock_shutdown);
  if (in_shutdown)
  {
    pthread_mutex_unlock(&lock_shutdown);
    return;
  }
  in_shutdown = 1;
  pthread_mutex_unlock(&lock_shutdown);
  log_info("Shutdown started");
  if (manager_sock)
    close(manager_sock);
  log_info("Ended");
  if (errfp != stderr)
    fclose(errfp);
  hash_free(&exec_hash);
}

static void print_version(void)
{
  printf("%s  Ver %s Distrib %s, for %s (%s)\n",my_progname,MANAGER_VERSION,
	 MYSQL_SERVER_VERSION,SYSTEM_TYPE,MACHINE_TYPE);
}

static void usage()
{
  print_version();
  printf("MySQL AB, by Sasha\n");
  printf("This software comes with ABSOLUTELY NO WARRANTY\n\n");
  printf("Manages instances of MySQL server.\n\n");
  printf("Usage: %s [OPTIONS]", my_progname);
  printf("\n\
  -?, --help               Display this help and exit.\n");
#ifndef DBUG_OFF
  puts("\
  -#, --debug=[...]        Output debug log. Often this is 'd:t:o,filename`");
#endif
  printf("\
  -P, --port=...           Port number to listen on.\n\
  -l, --log=...            Path to log file.\n\
  -b, --bind-address=...   Address to listen on.\n\
  -B, --tcp-backlog==...   Size of TCP/IP listen queue.\n\
  -g, --greeting=          Set greeting on connect \n\
  -m, --max-command-len    Maximum command length \n\
  -d, --one-thread         Use one thread ( for debugging)  \n\
  -C, --connect-retries    Number of attempts to establish MySQL connection \n\
  -m, --max-command-len    Maximum command length \n\
  -V, --version            Output version information and exit.\n\n");
}

static int parse_args(int argc, char **argv)
{
  int c, option_index = 0;
  while ((c=getopt_long(argc,argv,"P:?#:Vl:b:B:g:m:dC:p:",
			long_options,&option_index)) != EOF)
  {
    switch (c)
      {
      case '#':
	DBUG_PUSH(optarg ? optarg : "d:t:O,/tmp/mysqlmgrd.trace");
	break;
      case 'd':
	one_thread=1;
	break;
      case 'p':
	manager_pw_file=optarg;
	break;
      case 'C':
        manager_connect_retries=atoi(optarg);
	break;
      case 'P':
	manager_port=atoi(optarg);
	break;
      case 'm':
	manager_max_cmd_len=atoi(optarg);
	break;
      case 'g':
	manager_greeting=optarg;
      case 'b':
	manager_bind_addr = inet_addr(optarg);
	break;
      case 'B':
	manager_back_log = atoi(optarg);
	break;
      case 'l':
	manager_log_file=optarg;
	break;
      case 'V':
	print_version();
	exit(0);
      case '?':
	usage();
	exit(0);
      default:
	usage();
	exit(1);
      }
  }
  return 0;
}

static int init_server()
{
  int arg=1;
  log_info("Started");
  if ((manager_sock=socket(PF_INET,SOCK_STREAM,0)) < 0)
    die("Could not create socket");
  bzero((char*)&manager_addr, sizeof(manager_addr));
  manager_addr.sin_family = AF_INET;
  manager_addr.sin_addr.s_addr = manager_bind_addr;
  manager_addr.sin_port = htons(manager_port);
  setsockopt(manager_sock,SOL_SOCKET, SO_REUSEADDR,(char*)&arg,sizeof(arg));
  if (bind(manager_sock,(struct sockaddr*)&manager_addr, sizeof(manager_addr)) < 0)
    die("Could not bind");
  if (listen(manager_sock,manager_back_log) < 0)
    die("Could not listen");

  return 0;
}

static int run_server_loop()
{
  pthread_t th;
  struct manager_thd *thd;
  int client_sock;
  uint len;
  Vio* vio;
  pthread_attr_t thr_attr;
  (void) pthread_attr_init(&thr_attr);
#if !defined(HAVE_DEC_3_2_THREADS)
  pthread_attr_setscope(&thr_attr,PTHREAD_SCOPE_SYSTEM);
  (void) pthread_attr_setdetachstate(&thr_attr,PTHREAD_CREATE_DETACHED);
#endif

  for (;!shutdown_requested;)
  {
    len=sizeof(struct sockaddr_in);
    if ((client_sock=accept(manager_sock,(struct sockaddr*)&manager_addr,
			    &len)) <0)
    {
      if (shutdown_requested)
	break;
      if (errno != EAGAIN)
      {
	log_warn("Error in accept, errno=%d", errno);
	sleep(1); /* avoid tying up CPU if accept is broken */
      }
      continue;
    }
    if (shutdown_requested)
      break;
    if (!(vio=vio_new(client_sock,VIO_TYPE_TCPIP,FALSE)))
    {
      log_err("Could not create I/O object");
      close(client_sock);
      continue;
    }
    if (!(thd=manager_thd_new(vio)))
    {
      log_err("Could not create thread object");
      vio_close(vio);
      continue;
    }
    
    if (authenticate(thd))
    {
      client_msg(vio,MANAGER_ACCESS, "Access denied");
      manager_thd_free(thd);
      continue;
    }
    if (shutdown_requested)
      break;
    if (one_thread)
    {
      process_connection((void*)thd);
      manager_thd_free(thd);
      continue;
    }
    else if (pthread_create(&th,&thr_attr,process_connection,(void*)thd))
    {
      client_msg(vio,MANAGER_INTERNAL_ERR,"Could not create thread, errno=%d",
		 errno);
      manager_thd_free(thd);
      continue;
    }
  }
  (void) pthread_attr_destroy(&thr_attr);
  return 0;
}

static FILE* open_log_stream()
{
  FILE* fp;
  if (!(fp=fopen(manager_log_file,"a")))
    die("Could not open log file '%s'", manager_log_file);
  return fp;
}

static byte* get_user_key(const byte* u, uint* len,
			  my_bool __attribute__((unused)) t)
{
  register const char* key;
  key = ((struct manager_user*)u)->user;
  *len = ((struct manager_user*)u)->user_len;
  return (byte*)key;
}

static byte* get_exec_key(const byte* e, uint* len,
			 my_bool __attribute__((unused)) t)
{
  register const char* key;
  key = ((struct manager_exec*)e)->ident;
  *len = ((struct manager_exec*)e)->ident_len;
  return (byte*)key;
}

static void init_arg_array(char* arg_str,char** args,uint arg_count)
{
  char* p = arg_str;
  for (;arg_count>0;arg_count--)
  {
    *args++=p;
    p += strlen(p)+1;
  }
  *args=0;
}

static uint tokenize_args(char* arg_start,char** arg_end)
{
  char* p, *p_write,*p_end;
  uint arg_count=0;
  int quoted=0,escaped=0,last_space=0;
  p_end=*arg_end;
  p_write=p=arg_start;
  for(;p<p_end;p++)
  {
    char c = *p;
    switch (c)
    {
    case ' ':
    case '\r':
    case '\n':  
      if (!quoted)
      {
	if (!last_space)
	{
	  *p_write++=0;
	  arg_count++;
	  last_space=1;
	}
      }
      else
	*p_write++=c;
      escaped=0;
      break;
    case '"':
      if (!escaped)
	quoted=!quoted;
      else
	*p_write++=c;
      last_space=0;
      escaped=0;
      break;
    case '\\':
      if (!escaped)
	escaped=1;
      else
      {
	*p_write++=c;
	escaped=0;
      }
      last_space=0;
      break;
    default:
      escaped=last_space=0;
      *p_write++=c;
      break;
    }
  }
  if (!last_space && p_write>arg_start)
    arg_count++;
  *p_write=0;
  *arg_end=p_write;
  log_debug("arg_count=%d,arg_start='%s'",arg_count,arg_start);
  return arg_count;
}

static void update_req_len(struct manager_exec* e)
{
  e->req_len=e->data_buf_size+
    (e->stdout_path_size=strlen(e->stdout_path)+1)+
    (e->stderr_path_size=strlen(e->stderr_path)+1);
 }

static struct manager_exec* manager_exec_new(char* arg_start,char* arg_end)
{
  struct manager_exec* tmp;
  char* first_arg;
  uint arg_len,num_args;
  num_args=tokenize_args(arg_start,&arg_end);
  arg_len=(uint)(arg_end-arg_start)+1; /* include \0 terminator*/
  if (!(tmp=(struct manager_exec*)my_malloc(sizeof(*tmp)+arg_len+
					    sizeof(char*)*num_args,
					    MYF(MY_ZEROFILL))))
    return 0;
  if (num_args<2)
  {
    tmp->error="Too few arguments";
    return tmp;
  }
  /* We have to allocate 'args' first as this must be alligned */
  tmp->args=(char**)(tmp +1);
  tmp->data_buf= (char*) (tmp->args + num_args);
  memcpy(tmp->data_buf,arg_start,arg_len);
  tmp->data_buf_size=arg_len;
  tmp->num_args=num_args; 
  tmp->ident=tmp->data_buf;
  tmp->ident_len=strlen(tmp->ident);
  first_arg=tmp->ident+tmp->ident_len+1;
  init_arg_array(first_arg,tmp->args,num_args-1);
  strmov(tmp->con_user,"root");
  tmp->con_port=MYSQL_PORT;
  memcpy(tmp->con_host,"localhost",10);
  tmp->bin_path=tmp->args[0];
  tmp->stdout_path_size=tmp->stderr_path_size=1;
  tmp->req_len=tmp->data_buf_size+2;
  pthread_mutex_init(&tmp->lock,0);
  pthread_cond_init(&tmp->cond,0);
  mysql_init(&tmp->mysql);
  return tmp;
}

static void manager_exec_free(void* e)
{
  mysql_close(&((struct manager_exec*)e)->mysql);
  my_free(e,MYF(0));
}

static int hex_val(char c)
{
  if (isdigit(c))
    return c-'0';
  c=tolower(c);
  return c-'a'+10;
}

static struct manager_user* manager_user_new(char* buf)
{
  struct manager_user* tmp;
  char* p,*user_end,*p_end;
  char c;
  if (!(tmp=(struct manager_user*)my_malloc(sizeof(*tmp),MYF(0))))
    return 0;
  p=tmp->user;
  tmp->error=0;
  user_end=p+MAX_USER_NAME;
  for (;(c=*buf) && p<user_end;buf++)
  {
    if (c == ':')
    {
      *p=0;
      tmp->user_len=p-tmp->user;
      buf++;
      break;
    }
    else
      *p++=c;
  }
  if (!c)
    tmp->error="Missing ':'";
  if (p == user_end)
    tmp->error="Username too long";
  if (tmp->error)
    return tmp;
  if (strlen(buf) < 2*MD5_LEN)
  {
    tmp->error="Invalid MD5 sum, too short";
    return tmp;
  }
  p=tmp->md5_pass;
  p_end=p+MD5_LEN;
  for (; p<p_end;p++,buf+=2)
  {
    *p=hex_val(*buf)*16+hex_val(buf[1]);
  }
  
  return tmp;
}

static void manager_user_free(void* u)
{
  my_free((gptr)u,MYF(0));
}

static void init_user_hash()
{
  FILE* f;
  char buf[80];
  int line_num=1;
  if (hash_init(&user_hash,1024,0,0,get_user_key,manager_user_free,MYF(0)))
    die("Could not initialize user hash");
  if (!(f=fopen(manager_pw_file,"r")))
    die("Could not open password file '%s'", manager_pw_file);
  for (;;line_num++)
  {
    struct manager_user* u;
    if (!fgets(buf,sizeof(buf),f) || feof(f))
      break;
    if (buf[0] == '#')
      continue;
    if (!(u=manager_user_new(buf)))
      die("Out of memory while reading user line");
    if (u->error)
    {
      die("Error on line %d of '%s': %s",line_num,manager_pw_file, u->error);
    }
    else
    {
      hash_insert(&user_hash,(gptr)u);
    }
  }
  fclose(f);
}

static void init_globals()
{
  pthread_attr_t thr_attr;
  if (hash_init(&exec_hash,1024,0,0,get_exec_key,manager_exec_free,MYF(0)))
    die("Exec hash initialization failed");
  if (!one_thread)
  {
    (void) pthread_attr_init(&thr_attr);
#if !defined(HAVE_DEC_3_2_THREADS)
    pthread_attr_setscope(&thr_attr,PTHREAD_SCOPE_SYSTEM);
    (void) pthread_attr_setdetachstate(&thr_attr,PTHREAD_CREATE_DETACHED);
#endif
    fork_launcher();
    if (pthread_create(&launch_msg_th,&thr_attr,process_launcher_messages,0))
      die("Could not start launcher message handler thread");
    /* (void) pthread_attr_destroy(&thr_attr); */
  }
  init_user_hash();
  loop_th=pthread_self();
  signal(SIGPIPE,handle_sigpipe);
}

static int open_and_dup(int fd,char* path)
{
  int old_fd;
  if ((old_fd=my_open(path,O_WRONLY|O_APPEND|O_CREAT,MYF(0)))<0)
  {
    log_err("Could not open '%s' for append, errno=%d",path,errno);
    return 1;
  }
  if (dup2(old_fd,fd)<0)
  {
    log_err("Failed in dup2(), errno=%d",errno);
    return 1;
  }
  my_close(old_fd,MYF(0));
  return 0;
}

static void run_launcher_loop()
{
  for (;;)
  {
    int req_len,ident_len,num_args;
    char* request_buf=0;
    pid_t pid;
    char* exec_path,*ident,*stdout_path,*stderr_path;
    char** args=0;
    
    if (my_read(to_launcher_pipe[0],(byte*)&req_len,
		sizeof(int),MYF(MY_NABP|MY_FULL_IO)) ||
	my_read(to_launcher_pipe[0],(byte*)&num_args,
		sizeof(int),MYF(MY_NABP|MY_FULL_IO)) ||
	!(request_buf=(char*)my_malloc(req_len+sizeof(pid)+2,MYF(0))) ||
	!(args=(char**)my_malloc(num_args*sizeof(char*),MYF(0))) ||
	my_read(to_launcher_pipe[0],request_buf,req_len,
		MYF(MY_NABP|MY_FULL_IO)))
    {
      log_err("launcher: Error reading request");
      my_free((gptr)request_buf,MYF(MY_ALLOW_ZERO_PTR));
      my_free((gptr)args,MYF(MY_ALLOW_ZERO_PTR));
      sleep(1);
      continue;
    }
    stdout_path=request_buf;
    stderr_path=stdout_path+strlen(stdout_path)+1;
    request_buf=stderr_path+strlen(stderr_path); /* black magic */
    ident=request_buf+1;
    ident_len=strlen(ident);
    exec_path=ident+ident_len+1;
    log_debug("num_args=%d,req_len=%d,ident=%s,ident_len=%d,exec_path=%s,\
stdout_path=%s,stderr_path=%s",
	      num_args,
	      req_len,ident,ident_len,exec_path,stdout_path,stderr_path);
    init_arg_array(exec_path,args,num_args-1);    
        
    switch ((pid=fork()))
    {
    case -1:
      log_err("launcher: cannot fork");
      sleep(1);
      break;
    case 0:
      if (open_and_dup(1,stdout_path) || open_and_dup(2,stderr_path))
	exit(1);
      if (execv(exec_path,args))
	log_err("launcher: cannot exec %s",exec_path);
      exit(1);
    default:
      request_buf[0]=CHILD_START;
      memcpy(request_buf+ident_len+2,&pid,sizeof(pid));
      if (write(from_launcher_pipe[1],request_buf,ident_len+2+sizeof(pid))<0)
	log_err("launcher: error sending launch status report");
      break;
    }
    my_free((gptr)(stdout_path),MYF(0));
    my_free((gptr)args,MYF(0));
  }
}

static void fork_launcher()
{
  if (pipe(to_launcher_pipe) || pipe(from_launcher_pipe))
    die("Could not create launcher pipes");
  switch ((launcher_pid=fork()))
  {
  case 0:
    signal(SIGCHLD,handle_child);
    run_launcher_loop();
    exit(0);
  case -1: die("Could not fork the launcher");
  default: return;
  }
}

static int daemonize()
{
  switch (fork())
    {
    case -1:
      die("Cannot fork");
    case 0:
      errfp = open_log_stream();
      init_globals();
      close(0);
      close(1);
      close(2);
      init_server();
      run_server_loop();
      clean_up();
      break;
    default:
      break;
    }
  return 0;
}

int main(int argc, char** argv)
{
  char c;
  stack_bottom= (uchar *) &c;
  MY_INIT(argv[0]);
  errfp = stderr;
  parse_args(argc,argv);
  pthread_mutex_init(&lock_log,0);
  pthread_mutex_init(&lock_shutdown,0);
  pthread_mutex_init(&lock_exec_hash,0);
  pthread_mutex_init(&lock_launch_thd,0);
  pthread_cond_init(&cond_launch_thd,0);
#ifdef DO_STACKTRACE
  signal(SIGSEGV,handle_segfault);
#endif
  if (one_thread)
  {
    init_globals();
    init_server();
    run_server_loop();
    clean_up();
    return 0;
  }
  else
    return daemonize();
}