|
root / base / usr / src / cmd / datadm / datadm.c
datadm.c C 1782 lines 39.5 KB
   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
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
 */

#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/sockio.h>
#include <sys/stat.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <ctype.h>
#include <errno.h>
#include <libintl.h>
#include <locale.h>
#include <fcntl.h>
#include <libdlpi.h>
#include <libdladm.h>
#include <libdlib.h>
#include <libdllink.h>
#include <sys/ib/ibnex/ibnex_devctl.h>

#define	DATADM_OP_VIEW		0x0000
#define	DATADM_OP_UPDATE	0x0001
#define	DATADM_OP_ADD		0x0002
#define	DATADM_OP_REMOVE	0x0003
#define	DATADM_NUM_OPS		0x0004
#define	DATADM_DAT_CONF		"/etc/dat/dat.conf"
#define	DATADM_LINESZ		1024
#define	DATADM_NUM_SP_TOKENS	7
#define	DATADM_NUM_DAT_TOKENS	8
#define	DATADM_DRV_NAME		"driver_name"
#define	DATADM_MAX_TOKENS	16

/*
 * generic entry
 * placed at the top of all entry types
 */
typedef struct datadm_entry {
	struct datadm_entry	*de_next;
} datadm_entry_t;

/*
 * list structure
 * can be manipulated using datadm_walk_list or
 * datadm_enqueue_entry
 */
typedef struct datadm_list {
	datadm_entry_t		*dl_head;
	datadm_entry_t		*dl_tail;
	uint_t			dl_count;
} datadm_list_t;

/*
 * internal representation of the version string in
 * dat.conf or service_provider.conf. the format is
 * <dv_name><dv_major>.<dv_minor>
 */
typedef struct datadm_version {
	char	*dv_name;
	uint_t	dv_major;
	uint_t	dv_minor;
} datadm_version_t;

/*
 * each sp_entry corresponds to an entry in dat.conf or
 * service_provider.conf. an sp_entry is processed by the
 * function datadm_process_sp_entry.
 */
typedef struct datadm_sp_entry {
	datadm_entry_t		spe_header;
	char			*spe_devname;
	datadm_version_t	spe_api_version;
	int			spe_threadsafe;
	int			spe_default;
	char			*spe_libpath;
	datadm_version_t	spe_sp_version;
	char			*spe_sp_data;
	int			spe_invalid;
} datadm_sp_entry_t;

/*
 * an hca_entry is created whenever a new hca device is
 * encountered during sp_entry processing. this structure
 * contains two lists. the sp_list holds sp entries that
 * are added when sp entry processing occurs. duplicate
 * sp entries are not added to this list. the ia_list may
 * be built statically using the information in dat.conf or
 * dynamically. similar to the sp_list,
 * the ia_list contains only unique entries.
 */
typedef struct datadm_hca_entry {
	datadm_entry_t		he_header;
	char			*he_name;
	datadm_list_t		he_sp_list;
	datadm_list_t		he_ia_list;
} datadm_hca_entry_t;

/*
 * an ia_entry is created when a new ia name is encountered
 * during sp_entry processing or when a new ia name is
 * discovered by datadm_build_ia_lists. ia_entry holds the ia
 * device's instance number.
 */
typedef struct datadm_ia_entry {
	datadm_entry_t		iae_header;
	char			iae_name[MAXLINKNAMELEN];
} datadm_ia_entry_t;

/*
 * a comment entry represents one of the comment lines at the
 * top of dat.conf. a list of these lines are saved during the
 * parsing of dat.conf. these lines are written back to dat.conf
 * when dat.conf gets regenerated.
 */
typedef struct datadm_cmnt_entry {
	datadm_entry_t		cmnt_header;
	char			*cmnt_line;
} datadm_cmnt_entry_t;

typedef struct datadm_hca_find_by_name {
	char			*hf_name;
	datadm_hca_entry_t	*hf_hca_entry;
} datadm_hca_find_by_name_t;

/*
 * 2nd argument to datadm_hca_entry_find.
 * hf_hca_entry is filled in if an hca_entry with
 * a matching he_name is found.
 */
typedef struct datadm_hca_find {
	datadm_sp_entry_t	*hf_sp_entry;
	datadm_hca_entry_t	*hf_hca_entry;
} datadm_hca_find_t;

/*
 * 2nd argument to datadm_ia_entry_find.
 * if_ia_entry is filled in if an ia_entry with
 * a matching ia_name is found.
 */
typedef struct datadm_ia_find {
	char			*if_ia_name;
	datadm_ia_entry_t	*if_ia_entry;
} datadm_ia_find_t;

/*
 * this gets passed to datadm_add_plink.
 */
typedef struct datadm_fill_ia_list {
	datadm_list_t		*ia_hca_list;
	dladm_handle_t		ia_dlh;
	int			ia_ibnex_fd;
	int			ia_sock_fd_v4;
	int			ia_sock_fd_v6;
} datadm_fill_ia_list_t;

/*
 * this defines the commandline parameters specified
 * by the user.
 */
typedef struct datadm_args {
	char			*da_sp_conf;
	char			*da_dat_conf;
	int			da_op_type;
} datadm_args_t;

static datadm_args_t		datadm_args;
static datadm_list_t		datadm_conf_header;
static char			*datadm_conf_header_default =
	"#\n"
	"# Copyright (c) 2003, 2010, Oracle and/or its affiliates. "
	"All rights reserved.\n"
	"#\n"
	"# DAT configuration file.\n"
	"#\n"
	"# This file is updated using the datadm(8) command.\n"
	"# Do not hand edit this file.\n"
	"# See datadm(8) man page for more details.\n"
	"#\n"
	"# The fields in this file are -\n"
	"#\n"
	"# IAname version threadsafe default library-path provider-version \\\n"
	"# instance-data platform-information\n"
	"#\n";

/*
 * common parsing functions.
 */
typedef int (*datadm_parse_func_t)(char *, void *);
static int datadm_parse_line(char *, char *[], int *);
static int datadm_parse_generic_str(char *, char **);
static int datadm_parse_nonnull_str(char *, char **);
static int datadm_parse_version(char *, datadm_version_t *);
static int datadm_parse_devname(char *, datadm_sp_entry_t *);
static int datadm_parse_api_version(char *, datadm_sp_entry_t *);
static int datadm_parse_threadsafe(char *, datadm_sp_entry_t *);
static int datadm_parse_default(char *, datadm_sp_entry_t *);
static int datadm_parse_libpath(char *, datadm_sp_entry_t *);
static int datadm_parse_sp_version(char *, datadm_sp_entry_t *);
static int datadm_parse_sp_data(char *, datadm_sp_entry_t *);
static int datadm_parse_ia_name(char *, char *);

/*
 * utility functions
 */
static void datadm_enqueue_entry(datadm_list_t *, datadm_entry_t *);
static int datadm_walk_list(datadm_list_t *,
    int (*)(datadm_entry_t *, void *), void *);
static int datadm_str_match(char *, char *);
static int datadm_version_match(datadm_version_t *, datadm_version_t *);
static int datadm_sp_entry_match(datadm_sp_entry_t *, datadm_sp_entry_t *);

/*
 * entry allocation/deallocation
 */
static datadm_sp_entry_t *datadm_alloc_sp_entry(void);
static datadm_ia_entry_t *datadm_alloc_ia_entry(void);
static datadm_hca_entry_t *datadm_alloc_hca_entry(void);
static datadm_cmnt_entry_t *datadm_alloc_cmnt_entry(void);
static void datadm_free_sp_entry(datadm_sp_entry_t *);
static void datadm_free_ia_entry(datadm_ia_entry_t *);
static void datadm_free_hca_entry(datadm_hca_entry_t *);
static void datadm_free_cmnt_entry(datadm_cmnt_entry_t *);


/*
 * high level parsing functions
 */
static int datadm_parse_sp_conf(datadm_list_t *);
static int datadm_parse_dat_conf(datadm_list_t *);
static int datadm_process_sp_entry(datadm_list_t *, datadm_sp_entry_t *,
    char *);

/*
 * ia devices discovery
 */
static int datadm_build_ia_lists(datadm_list_t *);

/*
 * helper function for OP_REMOVE
 */
static void datadm_invalidate_common_sp_entries(datadm_list_t *,
    datadm_list_t *);

/*
 * output generation
 */
static int datadm_generate_dat_conf(datadm_list_t *);
static int datadm_generate_conf_header(FILE *);
static int datadm_generate_conf_entry(FILE *, datadm_ia_entry_t *,
    datadm_sp_entry_t *);

/*
 * datadm operations
 */
static int datadm_view(void);
static int datadm_update(void);
static int datadm_add(void);
static int datadm_remove(void);

/*
 * usage
 */
static void datadm_usage(void);


/*
 * parse function tables
 */
static datadm_parse_func_t datadm_sp_parse_funcs[DATADM_NUM_SP_TOKENS] = {
	(datadm_parse_func_t)datadm_parse_devname,
	(datadm_parse_func_t)datadm_parse_api_version,
	(datadm_parse_func_t)datadm_parse_threadsafe,
	(datadm_parse_func_t)datadm_parse_default,
	(datadm_parse_func_t)datadm_parse_libpath,
	(datadm_parse_func_t)datadm_parse_sp_version,
	(datadm_parse_func_t)datadm_parse_sp_data
};

static datadm_parse_func_t datadm_dat_parse_funcs[DATADM_NUM_DAT_TOKENS] = {
	(datadm_parse_func_t)datadm_parse_ia_name,
	(datadm_parse_func_t)datadm_parse_api_version,
	(datadm_parse_func_t)datadm_parse_threadsafe,
	(datadm_parse_func_t)datadm_parse_default,
	(datadm_parse_func_t)datadm_parse_libpath,
	(datadm_parse_func_t)datadm_parse_sp_version,
	(datadm_parse_func_t)datadm_parse_sp_data,
	(datadm_parse_func_t)datadm_parse_devname
};

/*
 * operation table
 */
static int (*datadm_ops[DATADM_NUM_OPS])(void) = {
	datadm_view,
	datadm_update,
	datadm_add,
	datadm_remove
};

static void
datadm_usage(void)
{
	(void) fprintf(stderr, gettext(
	    "usage: datadm -v\n"
	    "              -u\n"
	    "              -a <service_provider.conf>\n"
	    "              -r <service_provider.conf>\n"));
}

static int
datadm_parse_generic_str(char *str, char **strptr)
{
	int	len;

	len = strlen(str);
	*strptr = (char *)malloc(len + 1);
	if (*strptr == NULL) {
		return (-1);
	}
	(void) strcpy(*strptr, str);
	return (0);
}

/*
 * this function strips off leading and trailing
 * whitespaces and returns an error for null or
 * empty strings.
 */
static int
datadm_parse_nonnull_str(char *str, char **strptr)
{
	int	len, i;
	char	*start;

	if (str[0] == '\0') {
		return (-1);
	}
	start = str;
	for (i = 0; str[i] != '\0'; i++) {
		if (!isspace(str[i])) {
			start = &str[i];
			break;
		}
	}
	for (; str[i] != '\0'; i++) {
		if (isspace(str[i])) {
			str[i] = '\0';
		}
	}
	len = strlen(start);
	*strptr = (char *)malloc(len + 1);
	if (*strptr == NULL) {
		return (-1);
	}
	(void) strcpy(*strptr, start);
	return (0);
}

/*
 * parses the api_version and sp_version fields in
 * dat.conf and service_provider.conf
 */
static int
datadm_parse_version(char *str, datadm_version_t *version)
{
	int	i = 0, len;
	int	major_idx, minor_idx;

	len = strlen(str);

	for (i = 0; i < len; i++) {
		if (isdigit(str[i])) break;
	}
	if (i == len) {
		return (-1);
	}
	if (i > 0) {
		version->dv_name = (char *)malloc(i + 1);
		bcopy(str, version->dv_name, i);
		version->dv_name[i] = '\0';
	} else {
		version->dv_name = NULL;
	}
	major_idx = i;

	for (; i < len; i++) {
		if (!isdigit(str[i])) break;
	}
	if (i == len) {
		return (-1);
	}
	if (str[i] != '.') {
		return (-1);
	}
	minor_idx = ++i;
	if (i == len) {
		return (-1);
	}
	for (; i < len; i++) {
		if (!isdigit(str[i])) break;
	}
	if (i != len) {
		return (-1);
	}
	version->dv_major = atoi(str + major_idx);
	version->dv_minor = atoi(str + minor_idx);
	return (0);
}

/*
 * parses the ia_name field in dat.conf
 */
static int
datadm_parse_ia_name(char *str, char *ia_name)
{
	if (strlen(str) >= MAXLINKNAMELEN)
		return (-1);
	(void) strlcpy(ia_name, str, MAXLINKNAMELEN);
	return (0);
}

/*
 * parses the device name, strips leading and trailing spaces.
 * the format should be "driver_name=<dev_name>"
 */
static int
datadm_parse_devname(char *str, datadm_sp_entry_t *sp_entry)
{
	int	len, dlen, i, j = 0;
	char	*drv_name = DATADM_DRV_NAME;

	len = strlen(str);
	dlen = strlen(drv_name);

	/*
	 * strip out leading spaces and try to match
	 * the expected string
	 */
	for (i = 0; i < len; i++) {
		if (isspace(str[i]) && j == 0) {
			continue;
		} else {
			if (str[i] == drv_name[j]) {
				j++;
				if (j == dlen) {
					break;
				} else {
					continue;
				}
			} else {
				break;
			}
		}
	}

	/*
	 * j must be dlen if the matching string is found
	 */
	if (j != dlen) {
		return (-1);
	}

	/*
	 * skip past the last char of drv_name
	 */
	i++;

	/*
	 * strip the spaces before the '='
	 */
	for (; i < len; i++) {
		if (!isspace(str[i])) {
			break;
		}
	}

	/*
	 * return if the string is too long or if
	 * the '=' isn't found
	 */
	if (i >= len || str[i] != '=') {
		return (-1);
	}
	i++;
	if (i >= len) {
		/*
		 * no string after the equal
		 */
		return (-1);
	}
	return (datadm_parse_nonnull_str(str + i, &sp_entry->spe_devname));
}

static int
datadm_parse_api_version(char *str, datadm_sp_entry_t *sp_entry)
{
	return (datadm_parse_version(str, &sp_entry->spe_api_version));
}

static int
datadm_parse_threadsafe(char *str, datadm_sp_entry_t *sp_entry)
{
	int retval = 0;

	if (strcmp(str, "threadsafe") == 0) {
		sp_entry->spe_threadsafe = 1;
	} else if (strcmp(str, "nonthreadsafe") == 0) {
		sp_entry->spe_threadsafe = 0;
	} else {
		retval = -1;
	}
	return (retval);
}

static int
datadm_parse_default(char *str, datadm_sp_entry_t *sp_entry)
{
	int retval = 0;

	if (strcmp(str, "default") == 0) {
		sp_entry->spe_default = 1;
	} else if (strcmp(str, "nondefault") == 0) {
		sp_entry->spe_default = 0;
	} else {
		retval = -1;
	}
	return (retval);
}

static int
datadm_parse_libpath(char *str, datadm_sp_entry_t *sp_entry)
{
	return (datadm_parse_nonnull_str(str, &sp_entry->spe_libpath));
}

static int
datadm_parse_sp_version(char *str, datadm_sp_entry_t *sp_entry)
{
	return (datadm_parse_version(str, &sp_entry->spe_sp_version));
}

static int
datadm_parse_sp_data(char *str, datadm_sp_entry_t *sp_entry)
{
	return (datadm_parse_generic_str(str, &sp_entry->spe_sp_data));
}

static void
datadm_enqueue_entry(datadm_list_t *list, datadm_entry_t *entry)
{
	if (list->dl_head == NULL) {
		list->dl_head = entry;
		list->dl_tail = entry;
		list->dl_count = 1;
	} else {
		list->dl_tail->de_next = entry;
		list->dl_tail = entry;
		list->dl_count++;
	}
}

/*
 * iterates through the list applying func on each element.
 * break and return if func returns non-zero.
 */
static int
datadm_walk_list(datadm_list_t *list, int (*func)(datadm_entry_t *, void *),
    void *arg)
{
	datadm_entry_t	*entry;
	int		retval = 0;

	entry = list->dl_head;
	while (entry != NULL) {
		retval = (*func)(entry, arg);
		if (retval != 0) break;
		entry = entry->de_next;
	}
	return (retval);
}

/*
 * iterates through the list applying free_func to each element.
 * list becomes empty when the function returns.
 */
static void
datadm_free_list(datadm_list_t *list, void (*free_func)(datadm_entry_t *))
{
	while (list->dl_head != NULL) {
		datadm_entry_t	*entry;

		entry = list->dl_head;
		list->dl_head = entry->de_next;
		(*free_func)(entry);
	}
	list->dl_count = 0;
	list->dl_tail = NULL;
}

static datadm_sp_entry_t *
datadm_alloc_sp_entry(void)
{
	datadm_sp_entry_t	*sp_entry;

	sp_entry = (datadm_sp_entry_t *)malloc(sizeof (*sp_entry));
	if (sp_entry == NULL) {
		return (NULL);
	}
	bzero(sp_entry, sizeof (*sp_entry));
	return (sp_entry);
}

static void
datadm_free_sp_entry(datadm_sp_entry_t *sp_entry)
{
	if (sp_entry->spe_devname != NULL) {
		free(sp_entry->spe_devname);
		sp_entry->spe_devname = NULL;
	}
	if (sp_entry->spe_api_version.dv_name != NULL) {
		free(sp_entry->spe_api_version.dv_name);
		sp_entry->spe_api_version.dv_name = NULL;
	}
	sp_entry->spe_api_version.dv_major = 0;
	sp_entry->spe_api_version.dv_minor = 0;
	sp_entry->spe_threadsafe = 0;
	sp_entry->spe_default = 0;
	if (sp_entry->spe_libpath != NULL) {
		free(sp_entry->spe_libpath);
		sp_entry->spe_libpath = NULL;
	}
	if (sp_entry->spe_sp_version.dv_name != NULL) {
		free(sp_entry->spe_sp_version.dv_name);
		sp_entry->spe_sp_version.dv_name = NULL;
	}
	sp_entry->spe_sp_version.dv_major = 0;
	sp_entry->spe_sp_version.dv_minor = 0;
	if (sp_entry->spe_sp_data != NULL) {
		free(sp_entry->spe_sp_data);
		sp_entry->spe_sp_data = NULL;
	}
	free(sp_entry);
}

static int
datadm_str_match(char *s1, char *s2)
{
	if (s1 == NULL || s2 == NULL) {
		if (s1 != s2) {
			return (0);
		}
	} else {
		if (strcmp(s1, s2) != 0) {
			return (0);
		}
	}
	return (1);
}

static int
datadm_version_match(datadm_version_t *v1, datadm_version_t *v2)
{
	if (!datadm_str_match(v1->dv_name, v2->dv_name)) {
		return (0);
	}
	if (v1->dv_major != v2->dv_major) {
		return (0);
	}
	if (v1->dv_minor != v2->dv_minor) {
		return (0);
	}
	return (1);
}

static int
datadm_sp_entry_match(datadm_sp_entry_t *sp1, datadm_sp_entry_t *sp2)
{
	if (!datadm_str_match(sp1->spe_devname, sp2->spe_devname)) {
		return (0);
	}
	if (!datadm_version_match(&sp1->spe_api_version,
	    &sp2->spe_api_version)) {
		return (0);
	}
	if (sp1->spe_threadsafe != sp2->spe_threadsafe) {
		return (0);
	}
	if (sp1->spe_default != sp2->spe_default) {
		return (0);
	}
	if (!datadm_str_match(sp1->spe_libpath, sp2->spe_libpath)) {
		return (0);
	}
	if (!datadm_version_match(&sp1->spe_sp_version,
	    &sp2->spe_sp_version)) {
		return (0);
	}
	if (!datadm_str_match(sp1->spe_sp_data, sp2->spe_sp_data)) {
		return (0);
	}
	return (1);
}

static datadm_ia_entry_t *
datadm_alloc_ia_entry(void)
{
	datadm_ia_entry_t	*ia_entry;

	ia_entry = (datadm_ia_entry_t *)malloc(sizeof (*ia_entry));
	if (ia_entry == NULL) {
		return (NULL);
	}
	bzero(ia_entry, sizeof (*ia_entry));
	return (ia_entry);
}

static void
datadm_free_ia_entry(datadm_ia_entry_t *ia_entry)
{
	free(ia_entry);
}

static datadm_hca_entry_t *
datadm_alloc_hca_entry(void)
{
	datadm_hca_entry_t	*hca_entry;

	hca_entry = (datadm_hca_entry_t *)malloc(sizeof (*hca_entry));
	if (hca_entry == NULL) {
		return (NULL);
	}
	bzero(hca_entry, sizeof (*hca_entry));
	return (hca_entry);
}

static void
datadm_free_hca_entry(datadm_hca_entry_t *hca_entry)
{
	if (hca_entry->he_name != NULL) {
		free(hca_entry->he_name);
		hca_entry->he_name = NULL;
	}
	datadm_free_list(&hca_entry->he_sp_list,
	    (void (*)(datadm_entry_t *))datadm_free_sp_entry);
	datadm_free_list(&hca_entry->he_ia_list,
	    (void (*)(datadm_entry_t *))datadm_free_ia_entry);
	free(hca_entry);
}

static int
datadm_hca_entry_match(datadm_hca_entry_t *h1, datadm_hca_entry_t *h2)
{
	if (!datadm_str_match(h1->he_name, h2->he_name)) {
		return (0);
	}
	return (1);
}

static int
datadm_hca_entry_find(datadm_hca_entry_t *h1, datadm_hca_find_t *hf)
{
	if (datadm_str_match(h1->he_name, hf->hf_sp_entry->spe_devname)) {
		hf->hf_hca_entry = h1;
		return (1);
	}
	return (0);
}

static int
datadm_ia_entry_find(datadm_ia_entry_t *i1, datadm_ia_find_t *iaf)
{
	if (strcmp(i1->iae_name, iaf->if_ia_name) == 0) {
		iaf->if_ia_entry = i1;
		return (1);
	}
	return (0);
}

static datadm_cmnt_entry_t *
datadm_alloc_cmnt_entry(void)
{
	datadm_cmnt_entry_t	*cmnt_entry;

	cmnt_entry = (datadm_cmnt_entry_t *)malloc(sizeof (*cmnt_entry));
	if (cmnt_entry == NULL) {
		return (NULL);
	}
	bzero(cmnt_entry, sizeof (*cmnt_entry));
	return (cmnt_entry);
}

static void
datadm_free_cmnt_entry(datadm_cmnt_entry_t *cmnt_entry)
{
	if (cmnt_entry->cmnt_line != NULL) {
		free(cmnt_entry->cmnt_line);
		cmnt_entry->cmnt_line = NULL;
	}
	free(cmnt_entry);
}

/*
 * tokenizes a line and strips off the quotes from quoted strings
 */
static int
datadm_parse_line(char *line_buf, char *tokens[], int *token_count)
{
	int			len, i;
	int			count = 0;
	char			*start = NULL;

	/* the line must not be longer than DATADM_LINESZ */
	len = strlen(line_buf);
	if (line_buf[len - 1] != '\n') {
		return (-1);
	}
	/* discard blank lines and comments */
	if (len == 1) {
		*token_count = 0;
		return (0);
	}
	if (len >= 2 && line_buf[0] == '#') {
		*token_count = 0;
		return (0);
	}
	/* removes the new line */
	line_buf[len - 1] = '\0';
	len--;

	for (i = 0; i < len; i++) {
		if (start != NULL) {
			/*
			 * start points to the start of
			 * a new token. if start is '"',
			 * we should expect a quoted
			 * string.
			 */
			if (*start == '\"') {
				/*
				 * keep scanning until we
				 * hit the end quote.
				 */
				if (line_buf[i] != '\"') {
					continue;
				}
				/*
				 * skip past the start quote
				 */
				start++;
			} else {
				/*
				 * our token is not a quoted
				 * string. our token ends only
				 * when we hit a whitespace.
				 */
				if (!isspace(line_buf[i])) {
					continue;
				}
			}
			/*
			 * nullify the end quote (if any)
			 * and update the tokens array.
			 */
			line_buf[i] = '\0';
			tokens[count] = start;
			start = NULL;
			count++;
		} else {
			/*
			 * skip whitespaces
			 */
			if (isspace(line_buf[i])) {
				continue;
			} else {
				start = &line_buf[i];
			}
		}
		if (count == DATADM_MAX_TOKENS) {
			start = NULL;
			break;
		}
	}
	if (start != NULL) {
		tokens[count] = start;
		start = NULL;
		count++;
	}
	*token_count = count;
	return (0);
}

/*
 * attempts to save sp_entry into hca_list.
 * becomes no-op if sp entry already exists.
 * new hca entries and ia entries are created as needed.
 */
static int
datadm_process_sp_entry(datadm_list_t *hca_list, datadm_sp_entry_t *sp_entry,
    char *ia_name)
{
	datadm_hca_find_t	hca_find;
	datadm_ia_find_t	ia_find;
	datadm_hca_entry_t	*hca_entry;

	hca_find.hf_sp_entry = sp_entry;
	hca_find.hf_hca_entry = NULL;
	(void) datadm_walk_list(hca_list, (int (*)(datadm_entry_t *, void *))
	    datadm_hca_entry_find, (void *)&hca_find);

	if (hca_find.hf_hca_entry == NULL) {
		int	dlen;

		/*
		 * hca_entry not found, need to create
		 * and insert one.
		 */
		hca_entry = datadm_alloc_hca_entry();
		if (hca_entry == NULL) {
			return (-1);
		}
		dlen = strlen(sp_entry->spe_devname);
		hca_entry->he_name = (char *)malloc(dlen + 1);
		if (hca_entry->he_name == NULL) {
			datadm_free_hca_entry(hca_entry);
			return (-1);
		}
		(void) strcpy(hca_entry->he_name, sp_entry->spe_devname);
		datadm_enqueue_entry(hca_list, (datadm_entry_t *)hca_entry);
	} else {
		hca_entry = hca_find.hf_hca_entry;
	}
	if (ia_name == NULL) {
		goto put_sp_entry;
	}
	ia_find.if_ia_name = ia_name;
	ia_find.if_ia_entry = NULL;
	(void) datadm_walk_list(&hca_entry->he_ia_list,
	    (int (*)(datadm_entry_t *, void *))datadm_ia_entry_find, &ia_find);

	if (ia_find.if_ia_entry == NULL) {
		datadm_ia_entry_t	*ia_entry;

		/*
		 * ia_entry not found, need to create
		 * and insert one.
		 */
		ia_entry = datadm_alloc_ia_entry();
		if (ia_entry == NULL) {
			return (-1);
		}
		(void) strlcpy(ia_entry->iae_name, ia_name, MAXLINKNAMELEN);
		datadm_enqueue_entry(&hca_entry->he_ia_list,
		    (datadm_entry_t *)ia_entry);
	}

put_sp_entry:;

	if (datadm_walk_list(&hca_entry->he_sp_list,
	    (int (*)(datadm_entry_t *, void *))datadm_sp_entry_match,
	    (void *)sp_entry)) {
		return (1);
	} else {
		/*
		 * only insert sp_entry if it is not found.
		 */
		datadm_enqueue_entry(&hca_entry->he_sp_list,
		    (datadm_entry_t *)sp_entry);
	}
	return (0);
}

/*
 * parses service_provider.conf
 */
static int
datadm_parse_sp_conf(datadm_list_t *hca_list)
{
	datadm_sp_entry_t	*sp_entry;
	FILE			*sp_file;
	char			*sp_conf = datadm_args.da_sp_conf;
	char			*tokens[DATADM_MAX_TOKENS];
	char			line_buf[DATADM_LINESZ];
	int			retval = 0;
	int			token_count = 0;
	int			line_count = 0;

	sp_file = fopen(sp_conf, "r");
	if (sp_file == NULL) {
		(void) fprintf(stderr,
		    gettext("datadm: cannot open %s\n"), sp_conf);
		return (-1);
	}

	for (;;) {
		bzero(line_buf, DATADM_LINESZ);
		if (fgets(line_buf, DATADM_LINESZ, sp_file) == NULL) {
			break;
		}
		token_count = 0;
		line_count++;
		retval = datadm_parse_line(line_buf, tokens, &token_count);
		if (retval != 0) {
			(void) fprintf(stderr, gettext(
			    "datadm: %s: line %d exceeded max length %d\n"),
			    sp_conf, line_count, DATADM_LINESZ);
			break;
		}
		if (token_count == 0) continue;
		if (token_count == DATADM_NUM_SP_TOKENS) {
			int i = 0;

			sp_entry = datadm_alloc_sp_entry();
			if (sp_entry == NULL) {
				retval = -1;
				break;
			}

			/*
			 * sp_entry gets filled incrementally by
			 * each parsing function
			 */
			for (i = 0; i < DATADM_NUM_SP_TOKENS &&
			    retval == 0; i++) {
				retval = (*datadm_sp_parse_funcs[i])
				    (tokens[i], (void *)sp_entry);
			}
			if (retval != 0) {
				(void) fprintf(stderr, gettext(
				    "datadm: parse error: %s, "
				    "line %d, token: %s\n"),
				    sp_conf, line_count, tokens[i - 1]);
				datadm_free_sp_entry(sp_entry);
				sp_entry = NULL;
				break;
			}

			retval = datadm_process_sp_entry(hca_list,
			    sp_entry, NULL);
			if (retval != 0) {
				datadm_free_sp_entry(sp_entry);
				if (retval == 1) {
					retval = 0;
				} else {
					break;
				}
			}
		} else {
			(void) fprintf(stderr, gettext(
			    "datadm: parse error: %s, line %d, "
			    "# of tokens: %d, expected %d\n"), sp_conf,
			    line_count, token_count, DATADM_NUM_SP_TOKENS);
			retval = -1;
			break;
		}
	}
	if (retval != 0) {
		datadm_free_list(hca_list,
		    (void (*)(datadm_entry_t *))datadm_free_hca_entry);
	}
	(void) fclose(sp_file);
	return (retval);
}

/*
 * parses dat.conf
 */
static int
datadm_parse_dat_conf(datadm_list_t *hca_list)
{
	boolean_t		save_header = B_TRUE;
	datadm_sp_entry_t	*sp_entry;
	FILE			*dat_file;
	char			*dat_conf = datadm_args.da_dat_conf;
	char			*tokens[DATADM_MAX_TOKENS];
	char			line_buf[DATADM_LINESZ];
	int			retval = 0;
	int			token_count = 0;
	int			line_count = 0;

	dat_file = fopen(dat_conf, "r");
	if (dat_file == NULL) {
		/* dat.conf not existing is not an error for OP_ADD */
		if (datadm_args.da_op_type == DATADM_OP_ADD) {
			return (0);
		}
		(void) fprintf(stderr, gettext("datadm: cannot open %s\n"),
		    dat_conf);
		return (-1);
	}

	for (;;) {
		bzero(line_buf, DATADM_LINESZ);
		if (fgets(line_buf, DATADM_LINESZ, dat_file) == NULL) {
			break;
		}
		token_count = 0;
		line_count++;
		retval = datadm_parse_line(line_buf, tokens, &token_count);
		if (retval != 0) {
			(void) fprintf(stderr, gettext(
			    "datadm: %s: line %d exceeded max length %d\n"),
			    dat_conf, line_count, DATADM_LINESZ);
			break;
		}
		if (token_count == 0) {
			datadm_cmnt_entry_t	*cmnt_entry;
			int			cmnt_len;

			/*
			 * comments are saved only if they are
			 * at the top of dat.conf.
			 */
			if (!save_header) continue;
			cmnt_entry = datadm_alloc_cmnt_entry();
			if (cmnt_entry == NULL) {
				perror("datadm: malloc");
				retval = -1;
				break;
			}
			cmnt_len = strlen(line_buf);
			cmnt_entry->cmnt_line = (char *)malloc(cmnt_len + 1);
			if (cmnt_entry->cmnt_line == NULL) {
				perror("datadm: malloc");
				datadm_free_cmnt_entry(cmnt_entry);
				retval = -1;
				break;
			}
			(void) strncpy(cmnt_entry->cmnt_line,
			    line_buf, cmnt_len);
			cmnt_entry->cmnt_line[cmnt_len] = '\0';
			datadm_enqueue_entry(&datadm_conf_header,
			    (datadm_entry_t *)cmnt_entry);
			continue;
		}
		if (token_count == DATADM_NUM_DAT_TOKENS) {
			int i = 0;
			char ia_name[MAXLINKNAMELEN];

			/*
			 * we stop saving comment lines once
			 * we see the first valid line.
			 */
			save_header = B_FALSE;
			sp_entry = datadm_alloc_sp_entry();
			if (sp_entry == NULL) {
				retval = -1;
				break;
			}

			/*
			 * sp_entry gets filled incrementally by
			 * each parsing function
			 */
			for (i = 0; i < DATADM_NUM_DAT_TOKENS &&
			    retval == 0; i++) {
				void	*arg;

				if (i == 0) {
					/*
					 * the first token (ia name)
					 * does not belong to an
					 * sp_entry
					 */
					arg = (void *)ia_name;
				} else {
					arg = (void *)sp_entry;
				}
				retval = (*datadm_dat_parse_funcs[i])
				    (tokens[i], arg);
			}
			if (retval != 0) {
				(void) fprintf(stderr, gettext(
				    "datadm: parse error: %s, "
				    "line %d, token: %s\n"), dat_conf,
				    line_count, tokens[i - 1]);
				datadm_free_sp_entry(sp_entry);
				sp_entry = NULL;
				break;
			}

			/*
			 * we ignore the ibds in dat.conf if we are
			 * doing update
			 */
			if (datadm_args.da_op_type == DATADM_OP_UPDATE) {
				retval = datadm_process_sp_entry(hca_list,
				    sp_entry, NULL);
			} else {
				retval = datadm_process_sp_entry(hca_list,
				    sp_entry, ia_name);
			}
			if (retval != 0) {
				datadm_free_sp_entry(sp_entry);
				if (retval == 1) {
					retval = 0;
				} else {
					break;
				}
			}
		} else {
			(void) fprintf(stderr, gettext(
			    "datadm: parse error: %s, line %d, "
			    "# of tokens: %d, expected %d\n"), dat_conf,
			    line_count, token_count, DATADM_NUM_DAT_TOKENS);
			retval = -1;
			break;
		}
	}
	if (retval != 0) {
		datadm_free_list(&datadm_conf_header,
		    (void (*)(datadm_entry_t *))datadm_free_cmnt_entry);
		datadm_free_list(hca_list,
		    (void (*)(datadm_entry_t *))datadm_free_hca_entry);
	}
	(void) fclose(dat_file);
	return (retval);
}

/*
 * used by OP_REMOVE to invalidate common sp entries between hl1 and hl2.
 * invalid sp entries will be ignored by datadm_generate_dat_conf.
 */
static void
datadm_invalidate_common_sp_entries(datadm_list_t *hl1, datadm_list_t *hl2)
{
	datadm_entry_t	*he1, *he2;

	he1 = hl1->dl_head;
	while (he1 != NULL) {
		he2 = hl2->dl_head;
		while (he2 != NULL) {
			datadm_entry_t	*se1, *se2;

			if (!datadm_hca_entry_match(
			    (datadm_hca_entry_t *)he1,
			    (datadm_hca_entry_t *)he2)) {
				he2 = he2->de_next;
				continue;
			}
			se1 = ((datadm_hca_entry_t *)he1)->he_sp_list.dl_head;
			while (se1 != NULL) {
				se2 = ((datadm_hca_entry_t *)he2)->
				    he_sp_list.dl_head;
				while (se2 != NULL) {
					if (!datadm_sp_entry_match(
					    (datadm_sp_entry_t *)se1,
					    (datadm_sp_entry_t *)se2)) {
						se2 = se2->de_next;
						continue;
					}
					((datadm_sp_entry_t *)se1)->
					    spe_invalid = 1;
					break;
				}
				se1 = se1->de_next;
			}
			break;
		}
		he1 = he1->de_next;
	}
}

static int
datadm_hca_entry_find_by_name(datadm_hca_entry_t *h1,
    datadm_hca_find_by_name_t *hf)
{
	if (datadm_str_match(h1->he_name, hf->hf_name)) {
		hf->hf_hca_entry = h1;
		return (1);
	}
	return (0);
}

datadm_hca_entry_t *
datadm_hca_lookup_by_name(datadm_list_t *hca_list, char *hca_driver_name)
{
	datadm_hca_find_by_name_t	hf;

	hf.hf_name = hca_driver_name;
	hf.hf_hca_entry = NULL;
	(void) datadm_walk_list(hca_list,
	    (int (*)(datadm_entry_t *, void *))datadm_hca_entry_find_by_name,
	    &hf);
	return (hf.hf_hca_entry);
}

static boolean_t
datadm_add_plink(char *linkname, datadm_fill_ia_list_t *ia_args)
{
	datalink_class_t	class;
	datalink_id_t		linkid;
	dladm_ib_attr_t		ib_attr;
	ibnex_ctl_query_hca_t	query_hca;
	datadm_hca_entry_t	*hca;
	struct lifreq		req;
	datadm_ia_find_t	ia_find;
	datadm_ia_entry_t	*ia_entry;

	if ((dladm_name2info(ia_args->ia_dlh, linkname, &linkid, NULL, &class,
	    NULL) != DLADM_STATUS_OK) ||
	    (class != DATALINK_CLASS_PART) ||
	    (dladm_part_info(ia_args->ia_dlh, linkid, &ib_attr,
	    DLADM_OPT_ACTIVE) != DLADM_STATUS_OK)) {
		return (B_FALSE);
	}

	(void) strlcpy(req.lifr_name, linkname, sizeof (req.lifr_name));
	/*
	 * we don't really need to know the ip address.
	 * we just want to check if the device is plumbed
	 * or not.
	 */
	if (ioctl(ia_args->ia_sock_fd_v4, SIOCGLIFADDR, (caddr_t)&req) != 0) {
		/*
		 * we try v6 if the v4 address isn't found.
		 */
		if (ioctl(ia_args->ia_sock_fd_v6, SIOCGLIFADDR,
		    (caddr_t)&req) != 0)
			return (B_FALSE);
	}

	bzero(&query_hca, sizeof (query_hca));
	query_hca.hca_guid = ib_attr.dia_hca_guid;
	if (ioctl(ia_args->ia_ibnex_fd, IBNEX_CTL_QUERY_HCA, &query_hca) == -1)
		return (B_FALSE);

	if ((hca = datadm_hca_lookup_by_name(ia_args->ia_hca_list,
	    query_hca.hca_info.hca_driver_name)) == NULL)
		return (B_FALSE);

	ia_find.if_ia_name = linkname;
	ia_find.if_ia_entry = NULL;
	(void) datadm_walk_list(&hca->he_ia_list,
	    (int (*)(datadm_entry_t *, void *))
	    datadm_ia_entry_find, &ia_find);

	if (ia_find.if_ia_entry == NULL) {
		/*
		 * we insert an ia entry only if
		 * it is unique.
		 */
		ia_entry = datadm_alloc_ia_entry();
		if (ia_entry != NULL) {
			(void) strlcpy(ia_entry->iae_name, linkname,
			    MAXLINKNAMELEN);
			datadm_enqueue_entry(&hca->he_ia_list,
			    (datadm_entry_t *)ia_entry);
		}
	}

	return (B_FALSE);
}

/*
 * build ia lists for each hca_list element
 */
static int
datadm_build_ia_lists(datadm_list_t *hca_list)
{
	dladm_handle_t		dlh;
	datadm_fill_ia_list_t	ia_args;
	int			rv = -1;
	int			fd = -1;
	int			sv4 = -1;
	int			sv6 = -1;

	if (dladm_open(&dlh) != DLADM_STATUS_OK)
		return (-1);

	if ((fd = open(IBNEX_DEVCTL_DEV, O_RDONLY)) < 0)
		goto out;

	if ((sv4 = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
		perror("datadm: socket");
		goto out;
	}

	if ((sv6 = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
		perror("datadm: socket");
		goto out;
	}

	ia_args.ia_hca_list = hca_list;
	ia_args.ia_dlh = dlh;
	ia_args.ia_ibnex_fd = fd;
	ia_args.ia_sock_fd_v4 = sv4;
	ia_args.ia_sock_fd_v6 = sv6;

	dlpi_walk((boolean_t (*) (const char *, void *))datadm_add_plink,
	    &ia_args, 0);
	rv = 0;

out:
	if (sv4 != -1)
		(void) close(sv4);
	if (sv6 != -1)
		(void) close(sv6);
	if (fd != -1)
		(void) close(fd);

	dladm_close(dlh);
	return (rv);
}

static int
datadm_generate_conf_entry(FILE *outfile, datadm_ia_entry_t *ia_entry,
    datadm_sp_entry_t *sp_entry)
{
	int	retval;

	retval = fprintf(outfile,
	    "%s  %s%d.%d  %s  %s  %s  %s%d.%d  \"%s\"  \"%s%s%s\"\n",
	    ia_entry->iae_name,
	    (sp_entry->spe_api_version.dv_name ?
	    sp_entry->spe_api_version.dv_name : ""),
	    sp_entry->spe_api_version.dv_major,
	    sp_entry->spe_api_version.dv_minor,
	    (sp_entry->spe_threadsafe ? "threadsafe" : "nonthreadsafe"),
	    (sp_entry->spe_default ? "default" : "nondefault"),
	    sp_entry->spe_libpath,
	    (sp_entry->spe_sp_version.dv_name ?
	    sp_entry->spe_sp_version.dv_name : ""),
	    sp_entry->spe_sp_version.dv_major,
	    sp_entry->spe_sp_version.dv_minor,
	    sp_entry->spe_sp_data,
	    DATADM_DRV_NAME, "=", sp_entry->spe_devname);

	if (retval < 0) {
		return (-1);
	}
	return (0);
}

/*
 * generate dat.conf header
 */
static int
datadm_generate_conf_header(FILE *outfile)
{
	datadm_entry_t		*cep;
	datadm_cmnt_entry_t	*cmnt;
	int			retval = 0;

	cep = datadm_conf_header.dl_head;
	if (cep == NULL) {
		/*
		 * if dat.conf doesn't have a header, we prepend a
		 * default one.
		 */
		retval = fprintf(outfile, "%s", datadm_conf_header_default);
		goto done;
	}
	while (cep != NULL) {
		cmnt = (datadm_cmnt_entry_t *)cep;
		if (cmnt->cmnt_line != NULL) {
			int		len;

			retval = fprintf(outfile, "%s", cmnt->cmnt_line);
			if (retval < 0) {
				break;
			}

			/*
			 * append a newline if the comment line doesn't
			 * have one.
			 */
			len = strlen(cmnt->cmnt_line);
			if (cmnt->cmnt_line[len - 1] != '\n') {
				retval = fprintf(outfile, "\n");
				if (retval < 0) {
					break;
				}
			}
		}
		cep = cep->de_next;
	}
done:;
	if (retval < 0) {
		return (-1);
	}
	return (0);
}

/*
 * outputs dat.conf to stdout or to basedir/etc/dat/dat.conf
 */
static int
datadm_generate_dat_conf(datadm_list_t *hca_list)
{
	FILE			*outfile = NULL;
	char			*dat_conf = datadm_args.da_dat_conf;
	datadm_entry_t		*hep;
	int			retval = 0;

	if (datadm_args.da_op_type == DATADM_OP_VIEW) {
		outfile = stdout;
	} else {
		outfile = fopen(dat_conf, "w+");
		if (outfile == NULL) {
			(void) fprintf(stderr, gettext(
			    "datadm: cannot open %s: %s\n"),
			    dat_conf, strerror(errno));
			return (-1);
		}
	}
	if (outfile != stdout) {
		/*
		 * do not generate the header if we are
		 * printing to the screen
		 */
		retval = datadm_generate_conf_header(outfile);
		if (retval != 0) {
			goto done;
		}
	}
	hep = hca_list->dl_head;
	while (hep != NULL) {
		datadm_entry_t	*iep;

		iep = ((datadm_hca_entry_t *)hep)->he_ia_list.dl_head;
		while (iep != NULL) {
			datadm_entry_t	*sep;

			sep = ((datadm_hca_entry_t *)hep)->he_sp_list.dl_head;
			while (sep != NULL) {
				if (((datadm_sp_entry_t *)sep)->spe_invalid) {
					sep = sep->de_next;
					continue;
				}
				retval = datadm_generate_conf_entry(outfile,
				    (datadm_ia_entry_t *)iep,
				    (datadm_sp_entry_t *)sep);
				if (retval != 0) {
					goto done;
				}
				sep = sep->de_next;
			}
			iep = iep->de_next;
		}
		hep = hep->de_next;
	}
	retval = fflush(outfile);
done:;
	if (outfile != stdout) {
		(void) fclose(outfile);
	}
	if (retval < 0) {
		perror("datadm: fprintf");
	}
	return (retval);
}

static int
datadm_view(void)
{
	int			retval = 0;
	datadm_list_t		hca_list;

	bzero(&hca_list, sizeof (hca_list));

	retval = datadm_parse_dat_conf(&hca_list);
	if (retval != 0) {
		goto cleanup;
	}
	retval = datadm_generate_dat_conf(&hca_list);
	if (retval != 0) {
		goto cleanup;
	}

cleanup:;
	datadm_free_list(&datadm_conf_header,
	    (void (*)(datadm_entry_t *))datadm_free_cmnt_entry);
	datadm_free_list(&hca_list,
	    (void (*)(datadm_entry_t *))datadm_free_hca_entry);
	return (retval);
}

static int
datadm_update(void)
{
	int			retval = 0;
	datadm_list_t		hca_list;

	bzero(&hca_list, sizeof (hca_list));

	retval = datadm_parse_dat_conf(&hca_list);
	if (retval != 0) {
		goto cleanup;
	}
	retval = datadm_build_ia_lists(&hca_list);
	if (retval != 0) {
		goto cleanup;
	}
	retval = datadm_generate_dat_conf(&hca_list);
	if (retval != 0) {
		goto cleanup;
	}

cleanup:;
	datadm_free_list(&datadm_conf_header,
	    (void (*)(datadm_entry_t *))datadm_free_cmnt_entry);
	datadm_free_list(&hca_list,
	    (void (*)(datadm_entry_t *))datadm_free_hca_entry);
	return (retval);
}

static int
datadm_add(void)
{
	int			retval = 0;
	datadm_list_t		hca_list;

	bzero(&hca_list, sizeof (hca_list));

	retval = datadm_parse_dat_conf(&hca_list);
	if (retval != 0) {
		goto cleanup;
	}
	retval = datadm_parse_sp_conf(&hca_list);
	if (retval != 0) {
		goto cleanup;
	}
	retval = datadm_build_ia_lists(&hca_list);
	if (retval != 0) {
		goto cleanup;
	}
	retval = datadm_generate_dat_conf(&hca_list);
	if (retval != 0) {
		goto cleanup;
	}

cleanup:;
	datadm_free_list(&datadm_conf_header,
	    (void (*)(datadm_entry_t *))datadm_free_cmnt_entry);
	datadm_free_list(&hca_list,
	    (void (*)(datadm_entry_t *))datadm_free_hca_entry);
	return (retval);
}

static int
datadm_remove(void)
{
	int			retval = 0;
	datadm_list_t		hca_list;
	datadm_list_t		hca_list2;

	bzero(&hca_list, sizeof (hca_list));
	bzero(&hca_list2, sizeof (hca_list2));

	retval = datadm_parse_dat_conf(&hca_list);
	if (retval != 0) {
		goto cleanup;
	}
	retval = datadm_parse_sp_conf(&hca_list2);
	if (retval != 0) {
		goto cleanup;
	}
	datadm_invalidate_common_sp_entries(&hca_list, &hca_list2);

	retval = datadm_generate_dat_conf(&hca_list);
	if (retval != 0) {
		goto cleanup;
	}

cleanup:;
	datadm_free_list(&datadm_conf_header,
	    (void (*)(datadm_entry_t *))datadm_free_cmnt_entry);
	datadm_free_list(&hca_list,
	    (void (*)(datadm_entry_t *))datadm_free_hca_entry);
	datadm_free_list(&hca_list2,
	    (void (*)(datadm_entry_t *))datadm_free_hca_entry);
	return (retval);
}

static int
datadm_locate_dat_conf(char *basedir)
{
	char		*dat_conf;

	if (basedir == NULL) {
		datadm_args.da_dat_conf = DATADM_DAT_CONF;
		return (0);
	}
	dat_conf = (char *)malloc(strlen(basedir) +
	    strlen(DATADM_DAT_CONF) + 1);
	if (dat_conf == NULL) {
		return (-1);
	}
	dat_conf[0] = '\0';
	(void) strcat(dat_conf, basedir);
	(void) strcat(dat_conf, DATADM_DAT_CONF);
	datadm_args.da_dat_conf = dat_conf;
	return (0);
}

int
main(int argc, char **argv)
{
	extern char	*optarg;
	extern int	optind;
	char		*basedir = NULL;
	int		c, retval;
	int		op_type = -1, errflg = 0;

	bzero(&datadm_args, sizeof (datadm_args));
	bzero(&datadm_conf_header, sizeof (datadm_conf_header));

	(void) setlocale(LC_ALL, "");
#if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
#define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
#endif
	(void) textdomain(TEXT_DOMAIN);

	while ((c = getopt(argc, argv, "vua:r:b:")) != EOF) {
		switch (c) {
		case 'v':
			if (op_type != -1) errflg = 1;
			op_type = DATADM_OP_VIEW;
			break;
		case 'u':
			if (op_type != -1) errflg = 1;
			op_type = DATADM_OP_UPDATE;
			break;
		case 'a':
			if (op_type != -1) errflg = 1;
			op_type = DATADM_OP_ADD;
			datadm_args.da_sp_conf = optarg;
			break;
		case 'r':
			if (op_type != -1) errflg = 1;
			op_type = DATADM_OP_REMOVE;
			datadm_args.da_sp_conf = optarg;
			break;
		case 'b':
			basedir = optarg;
			break;
		default:
			errflg = 1;
			break;
		}
		if (errflg != 0) {
			break;
		}
	}
	if (errflg != 0 || op_type == -1 || optind < argc) {
		datadm_usage();
		return (1);
	}
	datadm_args.da_op_type = op_type;
	if (datadm_locate_dat_conf(basedir)) {
		return (1);
	}

	retval = (*datadm_ops[op_type])();
	return (retval);
}