File: dload.c

package info (click to toggle)
evms 2.5.2-1.sarge2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 14,248 kB
  • ctags: 15,488
  • sloc: ansic: 201,340; perl: 12,421; sh: 4,262; makefile: 1,516; yacc: 316; sed: 16
file content (1467 lines) | stat: -rw-r--r-- 45,908 bytes parent folder | download
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
/*
 *
 *   (C) Copyright IBM Corp. 2001, 2003
 *
 *   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
 *
 * Module: dload.c
 *
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <dirent.h>
#include <dlfcn.h>
#include <unistd.h>
#include <errno.h>

#include "fullengine.h"
#include "dload.h"
#include "lists.h"
#include "engine.h"
#include "memman.h"
#include "message.h"
#include "internalAPI.h"
#include "commit.h"
#include "cluster.h"
#include "remote.h"
#include <ldm_funcs.h>
#include <csm_funcs.h>


static void unload_module(so_record_t * so_record) {

	LOG_PROC_ENTRY();

	LOG_DEBUG("Unload module %s.\n", so_record->name);
	engine_free(so_record->name);
	LOG_DEBUG("Issuing dlclose() on handle %p.\n", so_record->handle);
	dlclose(so_record->handle);
	if (!list_empty(so_record->plugin_list)) {
		LOG_WARNING("Warning: Unloading module %s while plug-ins are still loaded from it.\n", so_record->name);
	}
	destroy_list(so_record->plugin_list);
	engine_free(so_record);

	LOG_PROC_EXIT_VOID();
}


static int release_plugin(plugin_record_t * pPlugRec) {

	LOG_PROC_ENTRY();

	if (pPlugRec != NULL) {

		remove_thing(&plugins_list, pPlugRec);

		remove_thing(pPlugRec->so_record->plugin_list, pPlugRec);

		/*
		 * Unload the module if we have no other plug-ins loaded from
		 * the module.
		 */
		if (list_empty(pPlugRec->so_record->plugin_list)) {
			unload_module(pPlugRec->so_record);
		}
	}

	LOG_PROC_EXIT_INT(0);
	return 0;
}


static int unload_plugin(plugin_record_t * pPlugRec) {

	LOG_PROC_ENTRY();

	if (pPlugRec != NULL) {

		if(pPlugRec == cluster_manager) {
			/*
			 * Daemons and workers do not open the Engine on other
			 * nodes in the cluster.
			 */
			if (!(engine_mode & (ENGINE_DAEMON | ENGINE_WORKER))) {
				remote_close_engine();
			}
			disconnect_from_ece();
			local_focus = TRUE;
		}

		if (pPlugRec->functions.plugin->cleanup_evms_plugin != NULL) {
			pPlugRec->functions.plugin->cleanup_evms_plugin();
		}

		release_plugin(pPlugRec);
	}

	LOG_PROC_EXIT_INT(0);
	return 0;
}


/*
 * Unload the plug-ins in an orderly fashion.  Specifically,
 * disk/segment/region/EVMS region managers may have open file descriptors
 * for objects.  They can close the descriptors on cleanup.
 * The cluster manager plug-in should be left around to handle the
 * closes if they are on a remote node.
 */
int unload_plugins(void) {

	int                rc;
	int                i;
	plugin_record_t  * pPlugRec;
	list_anchor_t            plugin_set;
	evms_plugin_code_t unload_order[] = {EVMS_FILESYSTEM_INTERFACE_MODULE,
		                             EVMS_ASSOCIATIVE_FEATURE,
		                             EVMS_FEATURE,
		                             EVMS_REGION_MANAGER,	
		                             EVMS_SEGMENT_MANAGER,	
		                             EVMS_DEVICE_MANAGER,	
		                             EVMS_CLUSTER_MANAGER_INTERFACE_MODULE,	
		                             EVMS_NO_PLUGIN
	                                    };

	LOG_PROC_ENTRY();

	for (i = 0; unload_order[i] != EVMS_NO_PLUGIN; i++) {
		rc = engine_get_plugin_list(unload_order[i], 0, &plugin_set);

		if (rc == 0) {
			list_element_t iter1;
			list_element_t iter2;

			LIST_FOR_EACH_SAFE(plugin_set, iter1, iter2, pPlugRec) {
				delete_element(iter1);
				unload_plugin(pPlugRec);
			}
		}

		destroy_list(plugin_set);
	}

	LOG_PROC_EXIT_INT(0);
	return 0;
}


/*
 *  Called with a plug-in record to see if it is a duplicate of an existing
 *  plug-in record in the list_anchor_t. Matching them based on the plug-in record ID
 *  field allows for a quick comparison of: OEM id, EVMS plug-in Type and
 *  EVMS plug-in ID.  Plug-ins are duplicates if they have matching ID fields!
 *
 *  Returns: TRUE if a duplicate is found in the list
 *           FALSE if we don't find a duplicate plug-in in the list
 */
static boolean isa_duplicate_plugin( plugin_record_t * pPlugRec, list_anchor_t plugin_list ) {

	boolean           result = FALSE;
	list_element_t    iter1;
	list_element_t    iter2;
	plugin_record_t * pr;

	LOG_PROC_ENTRY();

	LIST_FOR_EACH_SAFE(plugin_list, iter1, iter2, pr) {
		
		if (pr->id == pPlugRec->id) {

			engine_user_message(NULL, NULL,
					    _("Duplicate Plug-ins detected\n"
					      "Already have loaded:\n"
					      "  long name: %-40s\n"
					      "  oem name: %-40s\n"
					      "  ID: %#08X\n"
					      "  version: %d.%d.%d\n"
					      "Currently loading:\n"
					      "  long name: %-40s\n"
					      "  oem name: %-40s\n"
					      "  ID: %#08X\n"
					      "  version: %d.%d.%d\n"),
					    pr->long_name,
					    pr->oem_name,
					    pr->id,
					    pr->version.major, pr->version.minor, pr->version.patchlevel,
					    pPlugRec->long_name,
					    pPlugRec->oem_name,
					    pPlugRec->id,
					    pPlugRec->version.major, pPlugRec->version.minor, pPlugRec->version.patchlevel);

			/*
			 * Check to see if the plug-in we are about to load is
			 * older than the one that is loaded.  If so,  return
			 * TRUE that this is a duplicate plug-in.
			 */
			if (pr->version.major > pPlugRec->version.major) {
				result = TRUE;
			} else {
				if ((pr->version.major == pPlugRec->version.major) &&
				    (pr->version.minor > pPlugRec->version.minor)) {
					result = TRUE;
				} else {
					if ((pr->version.minor == pPlugRec->version.minor) &&
					    (pr->version.patchlevel >= pPlugRec->version.patchlevel)) {
						result = TRUE;
					}
				}
			}

			if (result == FALSE) {
				/*
				 * The plug-in we are about to load is newer than
				 * the one that is currently loaded.  Unload the
				 * current plug-in and allow the new one to be
				 * loaded.
				 */

				engine_user_message(NULL, NULL,
						    _("Unloading version %d.%d.%d of the plug-in.\n"),
						    pr->version.major, pr->version.minor, pr->version.patchlevel);
				delete_element(iter1);
				release_plugin(pr);

			} else {
				engine_user_message(NULL, NULL,
						    _("Keeping version %d.%d.%d of the plug-in.\n"),
						    pr->version.major, pr->version.minor, pr->version.patchlevel);
			}

			break;
		}
	}
	
	LOG_PROC_EXIT_BOOLEAN(result);
	return result;
}


/*
 *  Validates that a plug-in record is useable.  Does simple checking to
 *  see if we could access the plug-in Ok from the EVMS engine.
 */
static boolean isa_valid_plugin_record(plugin_record_t * pPlugRec) {

	boolean result = TRUE;
	int rc;

	LOG_PROC_ENTRY();

	rc = check_version(pPlugRec->required_engine_api_version, engine_services_api_version);
	if (rc != 0) {

		if (rc < 0) {
			engine_user_message(NULL, NULL,
					    _("The plug-in %s in module %s requires Engine services API version (%d.%d.%d) which is greater than this Engine's services API version (%d.%d.%d).\n"),
					    pPlugRec->short_name, pPlugRec->so_record->name,
					    pPlugRec->required_engine_api_version.major, pPlugRec->required_engine_api_version.minor, pPlugRec->required_engine_api_version.patchlevel,
					    engine_services_api_version.major, engine_services_api_version.minor, engine_services_api_version.patchlevel);

		} else {
			engine_user_message(NULL, NULL,
					    _("The plug-in %s in module %s requires Engine services API version (%d.%d.%d) which is less than this Engine's services API version (%d.%d.%d).\n"),
					    pPlugRec->short_name, pPlugRec->so_record->name,
					    pPlugRec->required_engine_api_version.major, pPlugRec->required_engine_api_version.minor, pPlugRec->required_engine_api_version.patchlevel,
					    engine_services_api_version.major, engine_services_api_version.minor, engine_services_api_version.patchlevel);

		}
		/*
		 * The plug-in requires a different version of the Engine.  We can't
		 * trust the rest of the plug-in record to be in the correct format, so
		 * don't do any further checks.
		 */
		result = FALSE;

	} else {
		switch (GetPluginType(pPlugRec->id)) {
		case EVMS_FILESYSTEM_INTERFACE_MODULE:
			rc = check_version(pPlugRec->required_plugin_api_version.fsim, engine_fsim_api_version);
			if (rc != 0) {
				if (rc < 0) {
					engine_user_message(NULL, NULL,
							    _("The plug-in %s in module %s requires Engine FSIM API version (%d.%d.%d) which is greater than this Engine's FSIM API version (%d.%d.%d).\n"),
							    pPlugRec->short_name, pPlugRec->so_record->name,
							    pPlugRec->required_plugin_api_version.fsim.major, pPlugRec->required_plugin_api_version.fsim.minor, pPlugRec->required_plugin_api_version.fsim.patchlevel,
							    engine_fsim_api_version.major, engine_fsim_api_version.minor, engine_fsim_api_version.patchlevel);

				} else {
					engine_user_message(NULL, NULL,
							    _("The plug-in %s in module %s requires Engine FSIM API version (%d.%d.%d) which is less than this Engine's FSIM API version (%d.%d.%d).\n"),
							    pPlugRec->short_name, pPlugRec->so_record->name,
							    pPlugRec->required_plugin_api_version.fsim.major, pPlugRec->required_plugin_api_version.fsim.minor, pPlugRec->required_plugin_api_version.fsim.patchlevel,
							    engine_fsim_api_version.major, engine_fsim_api_version.minor, engine_fsim_api_version.patchlevel);
				}

				result = FALSE;
			}
			break;

		case EVMS_CLUSTER_MANAGER_INTERFACE_MODULE:
			rc = check_version(pPlugRec->required_plugin_api_version.cluster, engine_cluster_api_version);
			if (rc != 0) {
				if (rc < 0) {
					engine_user_message(NULL, NULL,
							    _("The plug-in %s in module %s requires Engine cluster manager API version (%d.%d.%d) which is greater than this Engine's cluster manager API version (%d.%d.%d).\n"),
							    pPlugRec->short_name, pPlugRec->so_record->name,
							    pPlugRec->required_plugin_api_version.cluster.major, pPlugRec->required_plugin_api_version.cluster.minor, pPlugRec->required_plugin_api_version.cluster.patchlevel,
							    engine_cluster_api_version.major, engine_cluster_api_version.minor, engine_cluster_api_version.patchlevel);

				} else {
					engine_user_message(NULL, NULL,
							    _("The plug-in %s in module %s requires Engine cluster manager API version (%d.%d.%d) which is less than this Engine's cluster manager API version (%d.%d.%d).\n"),
							    pPlugRec->short_name, pPlugRec->so_record->name,
							    pPlugRec->required_plugin_api_version.cluster.major, pPlugRec->required_plugin_api_version.cluster.minor, pPlugRec->required_plugin_api_version.cluster.patchlevel,
							    engine_cluster_api_version.major, engine_cluster_api_version.minor, engine_cluster_api_version.patchlevel);
				}

				result = FALSE;
			}
			break;

		default:
			rc = check_version(pPlugRec->required_plugin_api_version.plugin, engine_plugin_api_version);
			if (rc != 0) {
				if (rc < 0) {
					engine_user_message(NULL, NULL,
							    _("The plug-in %s in module %s requires Engine plug-in API version (%d.%d.%d) which is greater than this Engine's plug-in API version (%d.%d.%d).\n"),
							    pPlugRec->short_name, pPlugRec->so_record->name,
							    pPlugRec->required_plugin_api_version.plugin.major, pPlugRec->required_plugin_api_version.plugin.minor, pPlugRec->required_plugin_api_version.plugin.patchlevel,
							    engine_plugin_api_version.major, engine_plugin_api_version.minor, engine_plugin_api_version.patchlevel);

				} else {
					engine_user_message(NULL, NULL,
							    _("The plug-in %s in module %s requires Engine plug-in API version (%d.%d.%d) which is less than this Engine's plug-in API version (%d.%d.%d).\n"),
							    pPlugRec->short_name, pPlugRec->so_record->name,
							    pPlugRec->required_plugin_api_version.plugin.major, pPlugRec->required_plugin_api_version.plugin.minor, pPlugRec->required_plugin_api_version.plugin.patchlevel,
							    engine_plugin_api_version.major, engine_plugin_api_version.minor, engine_plugin_api_version.patchlevel);

				}

				result = FALSE;
			}
			break;
		}

		if (pPlugRec->container_functions != NULL) {
			rc = check_version(pPlugRec->required_container_api_version, engine_container_api_version);
			if (rc != 0) {
				if (rc < 0) {
					engine_user_message(NULL, NULL,
							    _("The plug-in %s in module %s requires Engine container API version (%d.%d.%d) which is greater than this Engine's container API version (%d.%d.%d).\n"),
							    pPlugRec->short_name, pPlugRec->so_record->name,
							    pPlugRec->required_container_api_version.major, pPlugRec->required_container_api_version.minor, pPlugRec->required_container_api_version.patchlevel,
							    engine_container_api_version.major, engine_container_api_version.minor, engine_container_api_version.patchlevel);

				} else {
					engine_user_message(NULL, NULL,
							    _("The plug-in %s in module %s requires Engine container API version (%d.%d.%d) which is less than this Engine's container API version (%d.%d.%d).\n"),
							    pPlugRec->short_name, pPlugRec->so_record->name,
							    pPlugRec->required_container_api_version.major, pPlugRec->required_container_api_version.minor, pPlugRec->required_container_api_version.patchlevel,
							    engine_container_api_version.major, engine_container_api_version.minor, engine_container_api_version.patchlevel);
				}

				result = FALSE;
			}
		}

		if (pPlugRec->short_name == NULL) {
			if (pPlugRec->long_name == NULL) {
				if (pPlugRec->oem_name == NULL) {
					engine_user_message(NULL, NULL,
							    _("A plug-in in module %s did not provide a short name, long name, nor an oem name.\n"),
							    pPlugRec->so_record->name);
				} else {
					engine_user_message(NULL, NULL,
							    _("A plug-in in module %s did not provide a short name nor a long name.  "
							      "The oem name is \"%s\".\n"),
							    pPlugRec->so_record->name, pPlugRec->oem_name);
				}

			} else {
				if (pPlugRec->oem_name == NULL) {
					engine_user_message(NULL, NULL,
							    _("A plug-in in module %s did not provide a short name nor an oem name.  The long name is \"%s\".\n"),
							    pPlugRec->so_record->name, pPlugRec->long_name);
				} else {
					engine_user_message(NULL, NULL,
							    _("A plug-in in module %s did not provide a short name.  The long name is \"%s\".  "
							      "The oem name is \"%s\".\n"),
							    pPlugRec->so_record->name, pPlugRec->long_name, pPlugRec->oem_name);
				}
			}

			result = FALSE;

		} else {
			if (pPlugRec->long_name == NULL) {
				if (pPlugRec->oem_name == NULL) {
					engine_user_message(NULL, NULL,
							    _("A plug-in in module %s did not provide a long name, nor an oem name.  The short name is \"%s\".\n"),
							    pPlugRec->so_record->name, pPlugRec->short_name);
				} else {
					engine_user_message(NULL, NULL,
							    _("A plug-in in module %s did not provide a long name.  The short name is \"%s\".  The oem name is \"%s\".\n"),
							    pPlugRec->so_record->name, pPlugRec->short_name, pPlugRec->oem_name);
				}

				result = FALSE;

			} else {
				if (pPlugRec->oem_name == NULL) {
					engine_user_message(NULL, NULL,
							    _("A plug-in in module %s did not provide an oem name.  The short name is \"%s\".  The long name is \"%s\".\n"),
							    pPlugRec->so_record->name, pPlugRec->short_name, pPlugRec->long_name);
					result = FALSE;

				} else {
					/* All required names are provided. */
				}
			}
		}

		if (pPlugRec->functions.plugin == NULL) {
			engine_user_message(NULL, NULL,
					    _("The plug-in %s in module %s did not provide a function table.\n"),
					    pPlugRec->short_name, pPlugRec->so_record->name);
			result = FALSE;
		}

	}

	if (!result) {
		engine_user_message(NULL, NULL, _("The plug-in failed to load.\n"));
	}

	LOG_PROC_EXIT_BOOLEAN(result);
	return result;
}


/*
 *  Two routines to load and unload Linux shared objects.
 */

static int load_module(char * soname, so_record_t * * pso_record) {

	int rc = 0;
	char * error = NULL;
	so_record_t * so_record = engine_alloc(sizeof(so_record_t));

	LOG_PROC_ENTRY();

	*pso_record = NULL;

	if (so_record != NULL) {

		status_message(_("Loading module: %s\n"), soname);
		dlerror();
		so_record->handle = dlopen(soname, RTLD_NOW);

		error = dlerror();
		if (error == NULL) {
			so_record->name = engine_strdup(soname);
			so_record->plugin_list = allocate_list();

			if (so_record->plugin_list != NULL) {
				*pso_record = so_record;

			} else {
				LOG_CRITICAL("Error allocating memory for a plug-in list in a .so record.\n");
				engine_free(so_record->name);
				engine_free(so_record);
				rc = ENOMEM;
			}

		} else {
			engine_user_message(NULL, NULL, _("Error loading %s: %s\n"), soname, error);
			engine_free(so_record);
			rc = ELIBBAD;
		}

	} else {
		LOG_CRITICAL("Error allocating memory for a .so record.\n");
		rc = ENOMEM;
	}

	LOG_PROC_EXIT_INT(rc);
	return rc;
}


static void * get_sym_addr(module_handle_t handle, char * symname) {

	void       * symaddr;
	const char * error;

	LOG_PROC_ENTRY();

	LOG_DEBUG("Get address for symbol %s from module handle %p.\n", symname, handle);

	dlerror();
	symaddr = dlsym(handle, symname);
	if ((error = dlerror()) != NULL) {
		LOG_DEBUG("Error getting address for %s from module %p: %s\n", symname, handle, error);
		symaddr  = NULL;
	}

	LOG_PROC_EXIT_PTR(symaddr);
	return symaddr;
}


static int setup_clustering() {

	int rc = 0;
	int i;

	LOG_PROC_ENTRY();

	rc = connect_to_ece();

	if (rc == 0) {

		if (current_node_name != NULL) {
			if (!(engine_mode & ENGINE_DAEMON)) {
				for (i = 0; i < num_config_nodes; i++) {
					if (strcmp(current_node_name, config_node_names->node_info[i].node_name) == 0) {
						current_nodeid = &config_nodes[i];
						break;
					}
				}

				if (current_nodeid == &no_nodeid) {
					engine_user_message(NULL, NULL,
							    _("%s is not the name of a node in this cluster. "
							      "The Engine cannot be opened on the node named %s.\n"),
							    current_node_name, current_node_name);
					rc = ENOENT;
				}

				if (rc == 0) {
					if (current_nodeid != my_nodeid) {
						local_focus = FALSE;
					}
				}

			} else {
				engine_user_message(NULL, NULL,
						    _("The node_name parameter is not valid when starting the daemon.\n"));
				rc = EINVAL;
			}

		} else {
			current_nodeid = my_nodeid;
		}

		/*
		 * Neither daemons nor workers verify versions with other
		 * daemons and open the Engine on other nodes in the cluster.
		 */
		if ((rc == 0) && (!(engine_mode & (ENGINE_DAEMON | ENGINE_WORKER)))) {
			/*
			 * Make sure the EVMS daemons in the cluster are
			 * at the correct protocol version.
			 */
			status_message(_("Verifying communication protocol versions...\n"));

			rc = remote_verify_version();
			if (rc != 0) {
				engine_user_message(NULL, NULL,
						    _("There was an error when validating the version of the daemon protocol on the other nodes in the cluster.  "
						      "The Engine will run with clustering support disabled.  "
						      "EVMS will only manage local devices on this system.\n"));
				disconnect_from_ece();
				local_focus = TRUE;
				rc = 0;
			}
		}

		if (rc != 0) {
			disconnect_from_ece();
			local_focus = TRUE;
		}

	} else {
		if (current_node_name != NULL) {
			engine_user_message(NULL, NULL,
					    _("There was an error when connecting to %s.  "
					      "The error code was %d: %s  "
					      "This machine is not running in a clustered environment.\n"),
					    cluster_manager->short_name, rc, evms_strerror(rc));

		} else {
			engine_user_message(NULL, NULL,
					    _("There was an error when connecting to %s.  "
					      "The error code was %d: %s  "
					      "EVMS will only manage local devices on this system.\n"),
					    cluster_manager->short_name, rc, evms_strerror(rc));
			rc = 0;
		}
	}

	LOG_PROC_EXIT_INT(rc);
	return rc;
}


/*
 * Dummy functions for filling in function table entries that a plug-in
 * does not provide.
 */
static int return_ENOSYS() {
	return ENOSYS;
}


static int return_0() {
	return 0;
}

static void return_void() {
	return;
}


static int validate_plugin_functions(plugin_record_t * pPlugRec) {

	int rc = 0;

	LOG_PROC_ENTRY();

	/* A plug-in must support the following functions. */

	if ((pPlugRec->functions.plugin->setup_evms_plugin        == NULL) ||
	    (pPlugRec->functions.plugin->discover                 == NULL) ||
	    (pPlugRec->functions.plugin->discard                  == NULL) ||
	    (pPlugRec->functions.plugin->add_sectors_to_kill_list == NULL) ||
	    (pPlugRec->functions.plugin->commit_changes           == NULL) ||
	    (pPlugRec->functions.plugin->read                     == NULL) ||
	    (pPlugRec->functions.plugin->write                    == NULL)) {
		rc = ENOSYS;
	}

	/*
	 * The remaining functions are not absolutely necessary for a plug-in to
	 * function.  If the plug-in doesn't specify them, we supply a default.
	 */

	if (pPlugRec->functions.plugin->can_add_feature == NULL) {
		pPlugRec->functions.plugin->can_add_feature = (void *) return_ENOSYS;
	}
	if (pPlugRec->functions.plugin->can_unassign == NULL) {
		pPlugRec->functions.plugin->can_unassign = (void *) return_ENOSYS;
	}
	if (pPlugRec->functions.plugin->can_delete == NULL) {
		pPlugRec->functions.plugin->can_delete = (void *) return_ENOSYS;
	}
	if (pPlugRec->functions.plugin->can_expand == NULL) {
		pPlugRec->functions.plugin->can_expand = (void *) return_0;
	}
	if (pPlugRec->functions.plugin->can_expand_by == NULL) {
		pPlugRec->functions.plugin->can_expand_by = (void *) return_ENOSYS;
	}
	if (pPlugRec->functions.plugin->can_shrink == NULL) {
		pPlugRec->functions.plugin->can_shrink = (void *) return_0;
	}
	if (pPlugRec->functions.plugin->can_shrink_by == NULL) {
		pPlugRec->functions.plugin->can_shrink_by = (void *) return_ENOSYS;
	}
	if (pPlugRec->functions.plugin->can_replace_child == NULL) {
		pPlugRec->functions.plugin->can_replace_child = (void *) return_ENOSYS;
	}
	if (pPlugRec->functions.plugin->can_set_volume == NULL) {
		pPlugRec->functions.plugin->can_set_volume = (void *) return_0;
	}
	if (pPlugRec->functions.plugin->set_volume == NULL) {
		pPlugRec->functions.plugin->set_volume = (void *) return_void;
	}
	if (pPlugRec->functions.plugin->create == NULL) {
		pPlugRec->functions.plugin->create = (void *) return_ENOSYS;
	}
	if (pPlugRec->functions.plugin->assign == NULL) {
		pPlugRec->functions.plugin->assign = (void *) return_ENOSYS;
	}
	if (pPlugRec->functions.plugin->delete == NULL) {
		pPlugRec->functions.plugin->delete = (void *) return_ENOSYS;
	}
	if (pPlugRec->functions.plugin->unassign == NULL) {
		pPlugRec->functions.plugin->unassign = (void *) return_ENOSYS;
	}
	if (pPlugRec->functions.plugin->expand == NULL) {
		pPlugRec->functions.plugin->expand = (void *) return_ENOSYS;
	}
	if (pPlugRec->functions.plugin->shrink == NULL) {
		pPlugRec->functions.plugin->shrink = (void *) return_ENOSYS;
	}
	if (pPlugRec->functions.plugin->replace_child == NULL) {
		pPlugRec->functions.plugin->replace_child = (void *) return_ENOSYS;
	}
	if (pPlugRec->functions.plugin->can_activate == NULL) {
		pPlugRec->functions.plugin->can_activate = (void *) return_ENOSYS;
	}
	if (pPlugRec->functions.plugin->activate == NULL) {
		pPlugRec->functions.plugin->activate = (void *) return_ENOSYS;
	}
	if (pPlugRec->functions.plugin->can_deactivate == NULL) {
		pPlugRec->functions.plugin->can_deactivate = (void *) return_ENOSYS;
	}
	if (pPlugRec->functions.plugin->deactivate == NULL) {
		pPlugRec->functions.plugin->deactivate = (void *) return_ENOSYS;
	}
	if (pPlugRec->functions.plugin->get_option_count == NULL) {
		pPlugRec->functions.plugin->get_option_count = (void *) return_0;
	}
	if (pPlugRec->functions.plugin->init_task == NULL) {
		pPlugRec->functions.plugin->init_task = (void *) return_ENOSYS;
	}
	if (pPlugRec->functions.plugin->set_option == NULL) {
		pPlugRec->functions.plugin->set_option = (void *) return_ENOSYS;
	}
	if (pPlugRec->functions.plugin->set_objects == NULL) {
		pPlugRec->functions.plugin->set_objects = (void *) return_ENOSYS;
	}
	if (pPlugRec->functions.plugin->get_info == NULL) {
		pPlugRec->functions.plugin->get_info = (void *) return_ENOSYS;
	}
	if (pPlugRec->functions.plugin->set_info == NULL) {
		pPlugRec->functions.plugin->set_info = (void *) return_ENOSYS;
	}
	if (pPlugRec->functions.plugin->get_plugin_info == NULL) {
		pPlugRec->functions.plugin->get_plugin_info = (void *) return_ENOSYS;
	}
	if (pPlugRec->functions.plugin->get_plugin_functions == NULL) {
		pPlugRec->functions.plugin->get_plugin_functions = (void *) return_ENOSYS;
	}
	if (pPlugRec->functions.plugin->plugin_function == NULL) {
		pPlugRec->functions.plugin->plugin_function = (void *) return_ENOSYS;
	}
	if (pPlugRec->functions.plugin->backup_metadata == NULL) {
		pPlugRec->functions.plugin->backup_metadata = (void *) return_ENOSYS;
	}

	LOG_PROC_EXIT_INT(rc);
	return rc;
}


static int validate_fsim_functions(plugin_record_t * pPlugRec) {

	int rc = 0;

	LOG_PROC_ENTRY();

	/* An FSIM must support the following functions. */

	if ((pPlugRec->functions.fsim->setup_evms_plugin == NULL) ||
	    (pPlugRec->functions.fsim->probe             == NULL) ||
	    (pPlugRec->functions.fsim->discard           == NULL)) {
		rc = ENOSYS;
	}

	/*
	 * The remaining functions are not absolutely necessary for a plug-in to
	 * function.  If the plug-in doesn't specify them, we supply a default.
	 */
	if (pPlugRec->functions.fsim->get_fs_size == NULL) {
		pPlugRec->functions.fsim->get_fs_size = (void *) return_ENOSYS;
	}
	if (pPlugRec->functions.fsim->get_fs_limits == NULL) {
		pPlugRec->functions.fsim->get_fs_limits = (void *) return_ENOSYS;
	}
	if (pPlugRec->functions.fsim->can_mkfs == NULL) {
		pPlugRec->functions.fsim->can_mkfs = (void *) return_ENOSYS;
	}
	if (pPlugRec->functions.fsim->can_unmkfs == NULL) {
		pPlugRec->functions.fsim->can_unmkfs = (void *) return_ENOSYS;
	}
	if (pPlugRec->functions.fsim->can_fsck == NULL) {
		pPlugRec->functions.fsim->can_fsck = (void *) return_ENOSYS;
	}
	if (pPlugRec->functions.fsim->can_expand_by == NULL) {
		pPlugRec->functions.fsim->can_expand_by = (void *) return_ENOSYS;
	}
	if (pPlugRec->functions.fsim->can_shrink_by == NULL) {
		pPlugRec->functions.fsim->can_shrink_by = (void *) return_ENOSYS;
	}
	if (pPlugRec->functions.fsim->mkfs_setup == NULL) {
		pPlugRec->functions.fsim->mkfs_setup = (void *) return_0;
	}
	if (pPlugRec->functions.fsim->mkfs == NULL) {
		pPlugRec->functions.fsim->mkfs = (void *) return_ENOSYS;
	}
	if (pPlugRec->functions.fsim->unmkfs_setup == NULL) {
		pPlugRec->functions.fsim->unmkfs_setup = (void *) return_0;
	}
	if (pPlugRec->functions.fsim->unmkfs == NULL) {
		pPlugRec->functions.fsim->unmkfs = (void *) return_ENOSYS;
	}
	if (pPlugRec->functions.fsim->fsck == NULL) {
		pPlugRec->functions.fsim->fsck = (void *) return_ENOSYS;
	}
	if (pPlugRec->functions.fsim->expand == NULL) {
		pPlugRec->functions.fsim->expand = (void *) return_ENOSYS;
	}
	if (pPlugRec->functions.fsim->shrink == NULL) {
		pPlugRec->functions.fsim->shrink = (void *) return_ENOSYS;
	}
	if (pPlugRec->functions.fsim->commit_changes == NULL) {
		pPlugRec->functions.fsim->commit_changes = (void *) return_0;
	}
	if (pPlugRec->functions.fsim->get_option_count == NULL) {
		pPlugRec->functions.fsim->get_option_count = (void *) return_0;
	}
	if (pPlugRec->functions.fsim->init_task == NULL) {
		pPlugRec->functions.fsim->init_task = (void *) return_ENOSYS;
	}
	if (pPlugRec->functions.fsim->set_option == NULL) {
		pPlugRec->functions.fsim->set_option = (void *) return_ENOSYS;
	}
	if (pPlugRec->functions.fsim->set_volumes == NULL) {
		pPlugRec->functions.fsim->set_volumes = (void *) return_ENOSYS;
	}
	if (pPlugRec->functions.fsim->get_volume_info == NULL) {
		pPlugRec->functions.fsim->get_volume_info = (void *) return_ENOSYS;
	}
	if (pPlugRec->functions.fsim->get_plugin_info == NULL) {
		pPlugRec->functions.fsim->get_plugin_info = (void *) return_ENOSYS;
	}
	if (pPlugRec->functions.fsim->get_plugin_functions == NULL) {
		pPlugRec->functions.fsim->get_plugin_functions = (void *) return_ENOSYS;
	}
	if (pPlugRec->functions.fsim->plugin_function == NULL) {
		pPlugRec->functions.fsim->plugin_function = (void *) return_ENOSYS;
	}

	LOG_PROC_EXIT_INT(rc);
	return rc;
}


static int validate_container_functions(plugin_record_t * pPlugRec) {

	int rc = 0;

	LOG_PROC_ENTRY();

	/*
	 * If a plug-in supports containers it must supply the following
	 * functions.
	 */

	if ((pPlugRec->container_functions->can_delete_container     == NULL) ||
	    (pPlugRec->container_functions->create_container         == NULL) ||
	    (pPlugRec->container_functions->discard_container        == NULL) ||
	    (pPlugRec->container_functions->delete_container         == NULL) ||
	    (pPlugRec->container_functions->commit_container_changes == NULL) ||
	    (pPlugRec->container_functions->get_container_info       == NULL)) {

		rc = ENOSYS;
	}

	/*
	 * The remaining functions are not absolutely necessary for a plug-in to
	 * function.  If the plug-in doesn't specify them, we supply a default.
	 */
	if (pPlugRec->container_functions->can_expand_container == NULL) {
		pPlugRec->container_functions->can_expand_container = (void *) return_ENOSYS;
	}
	if (pPlugRec->container_functions->can_shrink_container == NULL) {
		pPlugRec->container_functions->can_shrink_container = (void *) return_ENOSYS;
	}
	if (pPlugRec->container_functions->expand_container == NULL) {
		pPlugRec->container_functions->expand_container = (void *) return_ENOSYS;
	}
	if (pPlugRec->container_functions->shrink_container == NULL) {
		pPlugRec->container_functions->shrink_container = (void *) return_ENOSYS;
	}
	if (pPlugRec->container_functions->get_plugin_functions == NULL) {
		pPlugRec->container_functions->get_plugin_functions = (void *) return_ENOSYS;
	}
	if (pPlugRec->container_functions->plugin_function == NULL) {
		pPlugRec->container_functions->plugin_function = (void *) return_ENOSYS;
	}
	if (pPlugRec->container_functions->backup_container_metadata == NULL) {
		pPlugRec->container_functions->backup_container_metadata = (void *) return_ENOSYS;
	}

	LOG_PROC_EXIT_INT(rc);
	return rc;
}


static int validate_cluster_functions(plugin_record_t * pPlugRec) {

	int rc = 0;

	LOG_PROC_ENTRY();

	/* No cluster functions are optional. */

	if ((pPlugRec->functions.cluster->setup_evms_plugin    == NULL) ||
	    (pPlugRec->functions.cluster->cleanup_evms_plugin  == NULL) ||
	    (pPlugRec->functions.cluster->register_callback    == NULL) ||
	    (pPlugRec->functions.cluster->unregister_callback  == NULL) ||
	    (pPlugRec->functions.cluster->send_msg             == NULL) ||
	    (pPlugRec->functions.cluster->get_my_nodeid        == NULL) ||
	    (pPlugRec->functions.cluster->get_num_config_nodes == NULL) ||
	    (pPlugRec->functions.cluster->get_all_nodes        == NULL) ||
	    (pPlugRec->functions.cluster->get_membership       == NULL)) {

		rc = ENOSYS;
	}

	LOG_PROC_EXIT_INT(rc);
	return rc;
}


static int check_plugin(list_anchor_t plugin_list, plugin_record_t * pPlugRec) {

	int  rc = 0;

	LOG_PROC_ENTRY();

	status_message(_("Validating plug-in: %s\n"),
		       pPlugRec->long_name);

	if (isa_valid_plugin_record( pPlugRec ) == TRUE) {

		/* Don't run with duplicate plug-ins loaded ! */
		if (isa_duplicate_plugin( pPlugRec, plugin_list ) == FALSE) {
			uint plugin_type;

			plugin_type = GetPluginType(pPlugRec->id);

			switch (plugin_type) {
			
			/* Only load Device Managers in daemon mode. */
			case EVMS_ASSOCIATIVE_FEATURE:
			case EVMS_FEATURE:
			case EVMS_REGION_MANAGER:
			case EVMS_SEGMENT_MANAGER:
				if (engine_mode & ENGINE_DAEMON) {
					rc = E_NOLOAD;
					break;
				}

			case EVMS_DEVICE_MANAGER:
				rc = validate_plugin_functions(pPlugRec);
				if (rc == 0) {
					if (pPlugRec->container_functions != NULL) {
						rc = validate_container_functions(pPlugRec);

						if (rc != 0) {
							engine_user_message(NULL, NULL,
									    _("The %s plug-in in module %s failed to load.  It does not have a valid container functions table.\n"),
									    pPlugRec->short_name, pPlugRec->so_record->name);
						}
					}

				} else {
					engine_user_message(NULL, NULL,
							    _("The %s plug-in in module %s failed to load.  It does not have a valid function table.\n"),
							    pPlugRec->short_name, pPlugRec->so_record->name);
				}
				break;

			case EVMS_FILESYSTEM_INTERFACE_MODULE:
				
				/* Don't load FSIMs in daemon mode. */
				if (engine_mode & ENGINE_DAEMON) {
					rc = E_NOLOAD;

				} else {
					rc = validate_fsim_functions(pPlugRec);
					if (rc != 0) {
						engine_user_message(NULL, NULL,
								    _("The %s plug-in in module %s failed to load.  It does not have a valid function table.\n"),
								    pPlugRec->short_name, pPlugRec->so_record->name);
					}

				}
				break;

			case EVMS_CLUSTER_MANAGER_INTERFACE_MODULE:
				/*
				 * Load a cluster manager only if one has not successfully
				 * loaded already.
				 */
				if (cluster_manager == NULL) {
					rc = validate_cluster_functions(pPlugRec);
					if (rc != 0) {
						engine_user_message(NULL, NULL,
								    _("The %s plug-in in module %s failed to load.  It does not have a valid function table.\n"),
								    pPlugRec->short_name, pPlugRec->so_record->name);
					}

				} else {
					LOG_DETAILS("Skip loading cluster manager %s because cluster manager %s is already loaded.\n", pPlugRec->short_name, cluster_manager->short_name);
				}
				break;

			default:
				engine_user_message(NULL, NULL,
						    _("The %s plug-in in module %s failed to load.  It is of type of %#x which is not valid.\n"),
						    pPlugRec->short_name, pPlugRec->so_record->name, plugin_type);
				rc = ENOSYS;
			}

		} else {
			rc = EEXIST;
		}

	} else {
		LOG_ERROR("Plug-in returned an invalid plug-in record.\n");
		rc = ENOSYS;
	}

	LOG_PROC_EXIT_INT(rc);
	return rc;
}


static int load_module_plugins(so_record_t * so_record, plugin_record_t * * ppPlugRec, list_anchor_t plugin_list) {

	int rc = 0;
	plugin_record_t * pPlugRec = *ppPlugRec;

	LOG_PROC_ENTRY();

	while ((rc == 0) && (pPlugRec != NULL)) {
		pPlugRec->so_record = so_record;

		rc = check_plugin(plugin_list, pPlugRec);

		if (rc == 0) {
			list_element_t el1;

			pPlugRec->type = PLUGIN;

			el1 = insert_thing(plugin_list,
					  pPlugRec,
					  INSERT_AFTER,
					  NULL);

			if (el1 != NULL) {
				list_element_t el2;

				el2 = insert_thing(so_record->plugin_list,
						  pPlugRec,
						  INSERT_AFTER,
						  NULL);

				if (el2 != NULL) {
					LOG_DEFAULT("Loaded from %s.\n", so_record->name);
					LOG_DEFAULT("  short name:  %s\n", pPlugRec->short_name);
					LOG_DEFAULT("  long name:   %s\n", pPlugRec->long_name);
					LOG_DEFAULT("  version:     %d.%d.%d\n", pPlugRec->version.major, pPlugRec->version.minor, pPlugRec->version.patchlevel);

				} else {
					LOG_CRITICAL("Failed to put the plug-in record in .so record.\n");
					delete_element(el1);
				}

			} else {
				LOG_CRITICAL("Failed to put plug-in record in the plugins_list.\n");
			}

		} else {
			/*
			 * Don't let any plug-in load error stop the plug-in
			 * loading process.
			 */
			rc = 0;
		}

		/* Check if the .so has another plug-in record. */
		ppPlugRec++;
		pPlugRec = *ppPlugRec;
	}

	/*
	 * If we didn't load any plug-ins from this module, then unload
	 * the module.
	 */
	if (list_empty(so_record->plugin_list)) {
		unload_module(so_record);
	}

	LOG_PROC_EXIT_INT(rc);
	return rc;
}


/*
 * Call the plug-ins' setup_evms_plugin() in a civilized order.
 */
static int setup_plugins() {

	int  rc = 0;
	list_anchor_t            plugin_set;
	list_element_t     iter;
	plugin_record_t  * plug_rec;

	LOG_PROC_ENTRY();

	rc = engine_get_plugin_list(EVMS_CLUSTER_MANAGER_INTERFACE_MODULE, 0, &plugin_set);

	if (rc != 0) {
		LOG_SERIOUS("Failed to get a list of cluster manager plug-ins.  Error code is %d: %s\n", rc, evms_strerror(rc));
		LOG_PROC_EXIT_INT(rc);
		return rc;
	}

	LIST_FOR_EACH(plugin_set, iter, plug_rec) {
		if (cluster_manager == NULL) {
			status_message(_("Setup plug-in: %s\n"), plug_rec->long_name);
			rc = plug_rec->functions.cluster->setup_evms_plugin(&engine_functions);

			if (rc == 0) {
				/* Save a pointer to the cluster manager plug-in. */
				cluster_manager = plug_rec;

				/* Clustering turns on PIDs in log timestamps. */
				log_pid = TRUE;

				rc = setup_clustering();

				if (rc != 0) {
					destroy_list(plugin_set);
					LOG_PROC_EXIT_INT(rc);
					return rc;
				}

			} else {
				if (rc != E_NOLOAD) {
					engine_user_message(NULL, NULL,
							    _("The plug-in %s in module %s failed to load.  The plug-in's setup_evms_plugin() function failed with error code %d: %s.\n"),
							    plug_rec->short_name, plug_rec->so_record->name, rc, strerror(rc));
				}
			}

		} else {
			LOG_DETAILS("Cluster manger plug-in %s is already loaded.  I'm not loading cluster manager %s.\n",
				    cluster_manager->short_name, plug_rec->short_name);
		}

		if (rc != 0) {
			release_plugin(plug_rec);
		}
	}

	destroy_list(plugin_set);

	rc = engine_get_plugin_list(EVMS_DEVICE_MANAGER, 0, &plugin_set);

	if (rc != 0) {
		LOG_SERIOUS("Failed to get a list of device manager plug-ins.  Error code is %d: %s\n", rc, evms_strerror(rc));
		LOG_PROC_EXIT_INT(rc);
		return rc;
	}

	LIST_FOR_EACH(plugin_set, iter, plug_rec) {
		status_message(_("Setup plug-in: %s\n"), plug_rec->long_name);
		rc = plug_rec->functions.plugin->setup_evms_plugin(&engine_functions);
		if (rc == 0) {
			/* Handle special plug-ins. */
			switch (plug_rec->id) {
				case LDM_PLUGIN_ID_2:
					{
						/* Start up the Local Disk Manager read cache. */
						plug_rec->functions.plugin->plugin_function(NULL, LDM_Start_Caching, NULL, NULL);
					}

					/*
					 * Save the pointer to the Local Disk Manager Plug-in for
					 * future reference.
					 */
					local_disk_manager = plug_rec;
					break;

				case REPLACE_PLUGIN_ID:
					replace_plugin = plug_rec;
					break;

				default:
					break;

			}

		} else {
			if (rc != E_NOLOAD) {
				engine_user_message(NULL, NULL,
						    _("The plug-in %s in module %s failed to load.  The plug-in's setup_evms_plugin() function failed with error code %d: %s.\n"),
						    plug_rec->short_name, plug_rec->so_record->name, rc, strerror(rc));
			}

			release_plugin(plug_rec);
		}
	}

	destroy_list(plugin_set);

	rc = engine_get_plugin_list(EVMS_SEGMENT_MANAGER, 0, &plugin_set);

	if (rc != 0) {
		LOG_SERIOUS("Failed to get a list of segment manager plug-ins.  Error code is %d: %s\n", rc, evms_strerror(rc));
		LOG_PROC_EXIT_INT(rc);
		return rc;
	}

	LIST_FOR_EACH(plugin_set, iter, plug_rec) {
		status_message(_("Setup plug-in: %s\n"), plug_rec->long_name);
		rc = plug_rec->functions.plugin->setup_evms_plugin(&engine_functions);
		if (rc == 0) {
			/* Handle special plug-ins. */
			switch (plug_rec->id) {
				case CSM_PLUGIN_ID:
					cluster_segment_manager = plug_rec;
					break;

				default:
					break;
			}

		} else {
			if (rc != E_NOLOAD) {
				engine_user_message(NULL, NULL,
						    _("The plug-in %s in module %s failed to load.  The plug-in's setup_evms_plugin() function failed with error code %d: %s.\n"),
						    plug_rec->short_name, plug_rec->so_record->name, rc, strerror(rc));
			}

			release_plugin(plug_rec);
		}
	}

	destroy_list(plugin_set);

	rc = engine_get_plugin_list(EVMS_REGION_MANAGER, 0, &plugin_set);

	if (rc != 0) {
		LOG_SERIOUS("Failed to get a list of region manager plug-ins.  Error code is %d: %s\n", rc, evms_strerror(rc));
		LOG_PROC_EXIT_INT(rc);
		return rc;
	}

	LIST_FOR_EACH(plugin_set, iter, plug_rec) {
		status_message(_("Setup plug-in: %s\n"), plug_rec->long_name);
		rc = plug_rec->functions.plugin->setup_evms_plugin(&engine_functions);
		if (rc != 0) {
			if (rc != E_NOLOAD) {
				engine_user_message(NULL, NULL,
						    _("The plug-in %s in module %s failed to load.  The plug-in's setup_evms_plugin() function failed with error code %d: %s.\n"),
						    plug_rec->short_name, plug_rec->so_record->name, rc, strerror(rc));
			}

			release_plugin(plug_rec);
		}
	}

	destroy_list(plugin_set);

	rc = engine_get_plugin_list(EVMS_FEATURE, 0, &plugin_set);

	if (rc != 0) {
		LOG_SERIOUS("Failed to get a list of EVMS featur plug-ins.  Error code is %d: %s\n", rc, evms_strerror(rc));
		LOG_PROC_EXIT_INT(rc);
		return rc;
	}

	LIST_FOR_EACH(plugin_set, iter, plug_rec) {
		status_message(_("Setup plug-in: %s\n"), plug_rec->long_name);
		rc = plug_rec->functions.plugin->setup_evms_plugin(&engine_functions);
		if (rc != 0) {
			if (rc != E_NOLOAD) {
				engine_user_message(NULL, NULL,
						    _("The plug-in %s in module %s failed to load.  The plug-in's setup_evms_plugin() function failed with error code %d: %s.\n"),
						    plug_rec->short_name, plug_rec->so_record->name, rc, strerror(rc));
			}

			release_plugin(plug_rec);
		}
	}

	destroy_list(plugin_set);

	rc = engine_get_plugin_list(EVMS_ASSOCIATIVE_FEATURE, 0, &plugin_set);

	if (rc != 0) {
		LOG_SERIOUS("Failed to get a list of EVMS associative feature plug-ins.  Error code is %d: %s\n", rc, evms_strerror(rc));
		LOG_PROC_EXIT_INT(rc);
		return rc;
	}

	LIST_FOR_EACH(plugin_set, iter, plug_rec) {
		status_message(_("Setup plug-in: %s\n"), plug_rec->long_name);
		rc = plug_rec->functions.plugin->setup_evms_plugin(&engine_functions);
		if (rc != 0) {
			if (rc != E_NOLOAD) {
				engine_user_message(NULL, NULL,
						    _("The plug-in %s in module %s failed to load.  The plug-in's setup_evms_plugin() function failed with error code %d: %s.\n"),
						    plug_rec->short_name, plug_rec->so_record->name, rc, strerror(rc));
			}

			release_plugin(plug_rec);
		}
	}

	destroy_list(plugin_set);

	rc = engine_get_plugin_list(EVMS_FILESYSTEM_INTERFACE_MODULE, 0, &plugin_set);

	if (rc != 0) {
		LOG_SERIOUS("Failed to get a list of file system interface plug-ins.  Error code is %d: %s\n", rc, evms_strerror(rc));
		LOG_PROC_EXIT_INT(rc);
		return rc;
	}

	LIST_FOR_EACH(plugin_set, iter, plug_rec) {
		status_message(_("Setup plug-in: %s\n"), plug_rec->long_name);
		rc = plug_rec->functions.fsim->setup_evms_plugin(&engine_functions);
		if (rc != 0) {
			if (rc != E_NOLOAD) {
				engine_user_message(NULL, NULL,
						    _("The plug-in %s in module %s failed to load.  The plug-in's setup_evms_plugin() function failed with error code %d: %s.\n"),
						    plug_rec->short_name, plug_rec->so_record->name, rc, strerror(rc));
			}

			release_plugin(plug_rec);
		}
	}

	destroy_list(plugin_set);

	LOG_PROC_EXIT_INT(rc);
	return rc;
}


/*
 *   This routine is called to load plug-ins for EVMS. Since we have
 *   no idea which plug-ins are available on the system we load them
 *   in the following manner:
 *
 *     (1) Try and open the "well known" EVMS Plugin directory ... /usr/lib/evms/
 *     (2) For each file found in the plug-in directory we'll call load_plugin()
 *         which can recognize plug-ins and load them.
 *     (3) If load_plugin() returns success we'll add the plug-in to the plugin
 *         list_anchor_t by inserting it as an object.
 *
 *   Finally, If we find at least 1 plug-in we'll return success.
 */

int load_plugins() {
	DIR           * pDir;
	struct dirent * pDirent;
	int             rc = 0;
	uint            plugins_loaded = 0;
	plugin_record_t * * ppPlugRec;
	char            so_name[256];

	LOG_PROC_ENTRY();

	/* Check if the plug-ins have been loaded already. */
	if (list_empty(&plugins_list)) {
		pDir = opendir(PluginDirectory);
		if (pDir != NULL) {

			pDirent = readdir(pDir);
			while ((rc == 0) && (pDirent != NULL)) {

				if ((strcmp(pDirent->d_name,".") != 0) &&
				    (strcmp(pDirent->d_name,"..") != 0)) {
					so_record_t * so_record;

					strcpy(so_name, PluginDirectory);
					strcat(so_name, "/");
					strcat(so_name, pDirent->d_name);

					LOG_DETAILS("Module to load is %s\n", so_name);

					rc = load_module(so_name, &so_record);

					if (rc == 0) {
						if (so_record != NULL) {

							/* Check for an array of plug-in pointers. */
							ppPlugRec = (plugin_record_t * *) get_sym_addr(so_record->handle, "evms_plugin_records");

							if (ppPlugRec != NULL) {

								/* Load the plug-in(s) in the module. */
								rc = load_module_plugins(so_record, ppPlugRec, &plugins_list);

							} else {
								/*
								 * The module doesn't export the published name
								 * for a plug-in record table.  It must not be
								 * an Engine plug-in.  Unload the module.
								 */
								engine_user_message(NULL, NULL,
										    _("Failed to load module %s.  It does not export an \"evms_plugin_records\" variable.\n"),
										    so_record->name);
								unload_module(so_record);
							}

						} else {
							LOG_WARNING("load_module() failed.\n");
						}

					} else {
						/*
						 * Don't let module load failures stop the plug-in
						 * loading process.
						 */
						rc = 0;
					}
				}

				pDirent = readdir(pDir);
			}

			closedir(pDir);

		} else {
			LOG_WARNING("Could not open PluginDirectory %s.\n", PluginDirectory);
			rc = ENOENT;
		}

		/* Find out how many plug-ins got loaded. */
		plugins_loaded = list_count(&plugins_list);

		LOG_DEBUG("Loaded %d plug-in(s).\n", plugins_loaded);
		LOG_DEBUG("Return code is %d.\n", rc);
		if (rc == 0) {
			if (plugins_loaded == 0) {
				rc = ENOENT;
			}
		} else {
			if (plugins_loaded != 0) {
				unload_plugins();
			}
		}

		if (rc == 0) {
			rc = setup_plugins();
		}

	}

	LOG_PROC_EXIT_INT(rc);
	return rc;
}