|
root / base / usr / src / cmd / sysdef
sysdef Plain Text 1104 lines 26.0 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
#
# 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 2009 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

PROG= 		sysdef

include ../Makefile.cmd

SUBDIRS += $(MACH64)

all	:	TARGET = all
install	:	TARGET = install
clean	:	TARGET = clean
clobber	:	TARGET = clobber
lint	:	TARGET = lint

RELUSRSBIN=     ../usr/sbin
ROOTSYMLINK=    $(ROOTETC)/$(PROG)

FILEMODE= 0555

.KEEP_STATE:

all:	$(SUBDIRS)

$(ROOTSYMLINK):
	-$(RM) $@; $(SYMLINK) $(RELUSRSBIN)/$(PROG) $@

clean clobber lint:	$(SUBDIRS)

install:	$(SUBDIRS) $(ROOTSYMLINK)

$(SUBDIRS):	FRC
	@cd $@; pwd; $(MAKE) $(TARGET)

FRC:

include ../Makefile.targ
#
# 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 2009 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
# Copyright (c) 2018, Joyent, Inc.

PROG=	sysdef
OBJS=	$(PROG).o sdevinfo.o
SRCS=	$(OBJS:%.o=../%.c)

include ../../Makefile.cmd

LDLIBS	+= -ldevinfo -lelf
CERRWARN += -Wno-parentheses

# not linted
SMATCH=off

FILEMODE= 02555

CLEANFILES += $(OBJS)

.KEEP_STATE:

all: $(PROG) 

$(PROG): $(OBJS)
	$(LINK.c) $(OBJS) -o $@ $(LDLIBS)
	$(POST_PROCESS)

lint:	lint_SRCS

%.o:	../%.c
	$(COMPILE.c) $<

clean:
	$(RM) $(CLEANFILES)

include ../../Makefile.targ

#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License, Version 1.0 only
# (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 2004 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

include ../Makefile.com
include ../../Makefile.cmd.64

CFLAGS64 += -D_ELF64

install: all $(ROOTUSRSBINPROG)
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (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) 1990 - 1997, Sun Microsystems, Inc.
 */

/*
 * For machines that support the openprom, fetch and print the list
 * of devices that the kernel has fetched from the prom or conjured up.
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <stdarg.h>
#include <libdevinfo.h>

static char *progname = "sysdef";
extern int devflag;		/* SunOS4.x devinfo compatible output */
extern int drvname_flag;	/* print out the driver name too */

static int _error(char *opt_noperror, ...);
static int dump_node(di_node_t node, void *arg);

void
sysdef_devinfo(void)
{
	di_node_t root_node;

	/* take a snapshot of kernel devinfo tree */
	if ((root_node = di_init("/", DINFOSUBTREE)) == DI_NODE_NIL) {
		exit(_error("di_init() failed."));
	}

	/*
	 * ...and call di_walk_node to report it out...
	 */
	di_walk_node(root_node, DI_WALK_CLDFIRST, NULL, dump_node);

	di_fini(root_node);
}

/*
 * print out information about this node
 */
static int
dump_node(di_node_t node, void *arg)
{
	int i;
	char *driver_name;
	di_node_t tmp;
	int indent_level = 0;

	/* find indent level */
	tmp = node;
	while ((tmp = di_parent_node(tmp)) != DI_NODE_NIL)
		indent_level++;

	/* we would start at 0, except that we skip the root node */
	if (!devflag)
		indent_level--;

	for (i = 0; i < indent_level; i++)
		(void) putchar('\t');

	if (indent_level >= 0) {
		if (devflag) {
			/*
			 * 4.x devinfo(8) compatible..
			 */
			(void) printf("Node '%s', unit #%d",
				di_node_name(node),
				di_instance(node));
			if (drvname_flag) {
				if (driver_name = di_driver_name(node)) {
					(void) printf(" (driver name: %s)",
					    driver_name);
				}
			} else if (di_state(node) & DI_DRIVER_DETACHED) {
				(void) printf(" (no driver)");
			}
		} else {
			/*
			 * prtconf(8) compatible..
			 */
			(void) printf("%s", di_node_name(node));
			if (di_instance(node) >= 0)
				(void) printf(", instance #%d",
				    di_instance(node));
			if (drvname_flag) {
				if (driver_name = di_driver_name(node)) {
					(void) printf(" (driver name: %s)",
					    driver_name);
				}
			} else if (di_state(node) & DI_DRIVER_DETACHED) {
				(void) printf(" (driver not attached)");
			}
		}
		(void) printf("\n");
	}
	return (DI_WALK_CONTINUE);
}

/*
 * utility routines
 */

/* _error([no_perror, ] fmt [, arg ...]) */
static int
_error(char *opt_noperror, ...)
{
	int saved_errno;
	va_list ap;
	int no_perror = 0;
	char *fmt;
	extern int errno, _doprnt();

	saved_errno = errno;

	if (progname)
		(void) fprintf(stderr, "%s: ", progname);

	va_start(ap, opt_noperror);
	if (opt_noperror == NULL) {
		no_perror = 1;
		fmt = va_arg(ap, char *);
	} else
		fmt = opt_noperror;
	(void) _doprnt(fmt, ap, stderr);
	va_end(ap);

	if (no_perror)
		(void) fprintf(stderr, "\n");
	else {
		(void) fprintf(stderr, ": ");
		errno = saved_errno;
		perror("");
	}

	return (1);
}
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (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 2004 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
/*	  All Rights Reserved  	*/

	/*
	 * This command can now print the value of data items
	 * from [1] /dev/kmem is the default, and [2] a named
	 * file passed with the -n argument.  If the read is from
	 * /dev/kmem, we also print the value of BSS symbols.
	 * The logic to support this is: if read is from file,
	 * [1] find the section number of .bss, [2] look through
	 * nlist for symbols that are in .bss section and zero
	 * the n_value field.  At print time, if the n_value field
	 * is non-zero, print the info.
	 *
	 * This protects us from trying to read a bss symbol from
	 * the file and, possibly, dropping core.
	 *
	 * When reading from /dev/kmem, the n_value field is the
	 * seek address, and the contents are read from that address.
	 *
	 * NOTE: when reading from /dev/kmem, the actual, incore
	 * values will be printed, for example: the current nodename
	 * will be printed, etc.
	 *
	 * the cmn line usage is: sysdef -i -n namelist -h -d -D
	 * (-i for incore, though this is now the default, the option
	 * is left in place for SVID compatibility)
	 */
#include	<stdio.h>
#include	<nlist.h>
#include	<string.h>
#include	<sys/types.h>
#include	<sys/sysmacros.h>
#include	<sys/var.h>
#include	<sys/tuneable.h>
#include	<sys/modctl.h>
#include	<sys/fcntl.h>
#include	<sys/utsname.h>
#include	<sys/resource.h>
#include	<sys/conf.h>
#include	<sys/stat.h>
#include	<sys/signal.h>
#include	<sys/priocntl.h>
#include	<sys/procset.h>
#include	<sys/systeminfo.h>
#include	<sys/machelf.h>
#include	<dirent.h>
#include	<ctype.h>
#include	<stdlib.h>
#include	<time.h>
#include	<unistd.h>
#include	<fcntl.h>

#include	<libelf.h>

extern void sysdef_devinfo(void);

static gid_t egid;

#define	SYM_VALUE(sym)	(nl[(sym)].n_value)
#define	MEMSEEK(sym)	memseek(sym)
#define	MEMREAD(var)	fread((char *)&var, sizeof (var), 1, \
				(incore ? memfile : sysfile))

struct	var	v;
struct  tune	tune;

int incore = 1;		/* The default is "incore" */
int bss;		/* if read from file, don't read bss symbols */
int hostidf = 0;	/* 0 == print hostid with other info, */
			/* 1 == print just the hostid */
int devflag = 0;	/* SunOS4.x devinfo compatible output */
int drvname_flag = 0;	/* print the driver name as well as the node */
int nflag = 0;
char	*os = "/dev/ksyms";	/* Wont always have a /kernel/unix */
				/* This wont fully replace it funtionally */
				/* but is a reasonable default/placeholder */

char	*mem = "/dev/kmem";

int	nstrpush;
ssize_t	strmsgsz, strctlsz;
short	ts_maxupri;
char 	sys_name[10];
int	nlsize, lnsize;
FILE	*sysfile, *memfile;

void	setln(char *, int, int, int);
void	getnlist(void);
void	memseek(int);
void	devices(void);
void	sysdev(void);
int	setup(char *);
void	modules(void);

struct nlist	*nl, *nlptr;
int vs, tu, utsnm, bdev, pnstrpush,
    pstrmsgsz, pstrctlsz, endnm,
    pts_maxupri, psys_name, fd_cur, fd_max;

#define	MAXI	300
#define	MAXL	MAXI/11+10
#define	EXPAND	99

struct	link {
	char	*l_cfnm;	/* config name from master table */
	int l_funcidx;		/* index into name list structure */
	unsigned int l_soft :1;	/* software driver flag from master table */
	unsigned int l_dtype:1;	/* set if block device */
	unsigned int l_used :1;	/* set when device entry is printed */
} *ln, *lnptr, *majsrch();

	/* ELF Items */
Elf *elfd = NULL;
Ehdr *ehdr = NULL;

#ifdef _ELF64
#define	elf_getehdr elf64_getehdr
#define	elf_getshdr elf64_getshdr
#else
#define	elf_getehdr elf32_getehdr
#define	elf_getshdr elf32_getshdr
#endif

/* This procedure checks if module "name" is currently loaded */

int
loaded_mod(const char *name)
{
	struct modinfo modinfo;

	/* mi_nextid of -1 means we're getting info on all modules */
	modinfo.mi_id = modinfo.mi_nextid = -1;
	modinfo.mi_info = MI_INFO_ALL;

	while (modctl(MODINFO, modinfo.mi_id, &modinfo) >= 0)
		if (strcmp(modinfo.mi_name, name) == 0)
			return (1);

	return (0);
}

const char *sysv_transition =
	"*\n* IPC %s\n*\n"
	"* The IPC %s module no longer has system-wide limits.\n"
	"* Please see the \"Solaris Tunable Parameters Reference Manual\" for\n"
	"* information on how the old limits map to resource controls and\n"
	"* the prctl(1) and getrctl(2) manual pages for information on\n"
	"* observing the new limits.\n*\n";

const char *sysv_notloaded =
	"*\n* IPC %s module is not loaded\n*\n";

/*
 * Emit a message pointing script writers to the new source for
 * System V IPC information.
 */
void
sysvipc(const char *module, const char *name)
{
	if (loaded_mod(module))
		(void) printf(sysv_transition, name, name);
	else
		(void) printf(sysv_notloaded, name);
}

int
main(int argc, char *argv[])
{
	struct	utsname utsname;
	Elf_Scn *scn;
	Shdr *shdr;
	char *name;
	int ndx;
	int i;
	char hostid[256], *end;
	unsigned long hostval;
	uint_t	rlim_fd_cur, rlim_fd_max;

	egid = getegid();
	setegid(getgid());

	while ((i = getopt(argc, argv, "dihDn:?")) != EOF) {
		switch (i) {
		case 'D':
			drvname_flag++;
			break;
		case 'd':
			devflag++;
			break;
		case 'h':
			hostidf++;
			break;
		case 'i':
			incore++;	/* In case "-i and -n" passed */
			break;		/* Not logical, but not disallowed */
		case 'n':
			nflag = 1;
			incore--;	/* Not incore, use specified file */
			os = optarg;
			break;
		default:
			fprintf(stderr,
				"usage: %s [-D -d -i -h -n namelist]\n",
					argv[0]);
			return (1);
		}
	}

	/*
	 * Prints hostid of machine.
	 */
	if (sysinfo(SI_HW_SERIAL, hostid, sizeof (hostid)) == -1) {
		fprintf(stderr, "hostid: sysinfo failed\n");
		return (1);
	}
	hostval = strtoul(hostid, &end, 10);
	if (hostval == 0 && end == hostid) {
		fprintf(stderr, "hostid: hostid string returned by "
		    "sysinfo not numeric: \"%s\"\n", hostid);
		return (1);
	}
	if (!devflag)
		fprintf(stdout, "*\n* Hostid\n*\n  %8.8x\n", hostval);

	if (hostidf)
		return (0);

	if (((sysfile = fopen(os, "r")) == NULL) && nflag) {
		fprintf(stderr, "cannot open %s\n", os);
		return (1);
	}

	if (sysfile) {
		if (incore) {
			int memfd;

			setegid(egid);
			if ((memfile = fopen(mem, "r")) == NULL) {
				fprintf(stderr, "cannot open %s\n", mem);
				return (1);
			}
			setegid(getgid());

			memfd = fileno(memfile);
			fcntl(memfd, F_SETFD,
			    fcntl(memfd, F_GETFD, 0) | FD_CLOEXEC);
		}

		/*
		 *	Use libelf to read both COFF and ELF namelists
		 */

		if ((elf_version(EV_CURRENT)) == EV_NONE) {
			fprintf(stderr, "ELF Access Library out of date\n");
			return (1);
		}

		if ((elfd = elf_begin(fileno(sysfile), ELF_C_READ,
		    NULL)) == NULL) {
			fprintf(stderr, "Unable to elf begin %s (%s)\n",
				os, elf_errmsg(-1));
			return (1);
		}

		if ((ehdr = elf_getehdr(elfd)) == NULL) {
			fprintf(stderr, "%s: Can't read Exec header (%s)\n",
				os, elf_errmsg(-1));
			return (1);
		}

		if ((((elf_kind(elfd)) != ELF_K_ELF) &&
		    ((elf_kind(elfd)) != ELF_K_COFF)) ||
		    (ehdr->e_type != ET_EXEC)) {
			fprintf(stderr, "%s: invalid file\n", os);
			elf_end(elfd);
			return (1);
		}

		/*
		 *	If this is a file read, look for .bss section
		 */

		if (!incore) {
			ndx = 1;
			scn = NULL;
			while ((scn = elf_nextscn(elfd, scn)) != NULL) {
				if ((shdr = elf_getshdr(scn)) == NULL) {
					fprintf(stderr,
					    "%s: Error reading Shdr (%s)\n",
					    os, elf_errmsg(-1));
					return (1);
				}
				name = elf_strptr(elfd, ehdr->e_shstrndx,
				    (size_t)shdr->sh_name);
				if ((name) && ((strcmp(name, ".bss")) == 0)) {
					bss = ndx;
				}
				ndx++;
			}
		} /* (!incore) */
	}

	uname(&utsname);
	if (!devflag)
		printf("*\n* %s Configuration\n*\n", utsname.machine);

	if (sysfile) {
		nlsize = MAXI;
		lnsize = MAXL;
		nl = (struct nlist *)calloc(nlsize, sizeof (struct nlist));
		ln = (struct link *)calloc(lnsize, sizeof (struct link));
		nlptr = nl;
		lnptr = ln;

		bdev = setup("bdevsw");
		setup("");

		getnlist();

		if (!devflag)
			printf("*\n* Devices\n*\n");
		devices();
		if (devflag)
			return (0);

		printf("*\n* Loadable Objects\n");

		modules();
	}

	printf("*\n* System Configuration\n*\n");

	sysdev();

	if (sysfile) {
		/* easy stuff */
		printf("*\n* Tunable Parameters\n*\n");
		nlptr = nl;
		vs = setup("v");
		tu = setup("tune");
		utsnm = setup("utsname");
		pnstrpush = setup("nstrpush");
		pstrmsgsz = setup("strmsgsz");
		pstrctlsz = setup("strctlsz");
		pts_maxupri = setup("ts_maxupri");
		psys_name = setup("sys_name");
		fd_cur = setup("rlim_fd_cur");
		fd_max = setup("rlim_fd_max");

		/*
		 * This assignment to endnm must follow all calls to setup().
		 */
		endnm = setup("");

		getnlist();

		for (nlptr = &nl[vs]; nlptr != &nl[endnm]; nlptr++) {
			if (nlptr->n_value == 0 &&
			    (incore || nlptr->n_scnum != bss)) {
				fprintf(stderr, "namelist error on <%s>\n",
				    nlptr->n_name);
				/* return (1); */
			}
		}
		if (SYM_VALUE(vs)) {
			MEMSEEK(vs);
			MEMREAD(v);
		}
		printf("%8d	maximum memory allowed in buffer cache "
		    "(bufhwm)\n", v.v_bufhwm * 1024);
		printf("%8d	maximum number of processes (v.v_proc)\n",
		    v.v_proc);
		printf("%8d	maximum global priority in sys class "
		    "(MAXCLSYSPRI)\n", v.v_maxsyspri);
		printf("%8d	maximum processes per user id (v.v_maxup)\n",
		    v.v_maxup);
		printf("%8d	auto update time limit in seconds (NAUTOUP)\n",
		    v.v_autoup);
		if (SYM_VALUE(tu)) {
			MEMSEEK(tu);
			MEMREAD(tune);
		}
		printf("%8d	page stealing low water mark (GPGSLO)\n",
		    tune.t_gpgslo);
		printf("%8d	fsflush run rate (FSFLUSHR)\n",
		    tune.t_fsflushr);
		printf("%8d	minimum resident memory for avoiding "
		    "deadlock (MINARMEM)\n", tune.t_minarmem);
		printf("%8d	minimum swapable memory for avoiding deadlock "
		    "(MINASMEM)\n", tune.t_minasmem);
	}

	printf("*\n* Utsname Tunables\n*\n");
	if (sysfile && SYM_VALUE(utsnm)) {
		MEMSEEK(utsnm);
		MEMREAD(utsname);
	}
	printf("%8s  release (REL)\n", utsname.release);
	printf("%8s  node name (NODE)\n", utsname.nodename);
	printf("%8s  system name (SYS)\n", utsname.sysname);
	printf("%8s  version (VER)\n", utsname.version);

	if (sysfile) {
		printf("*\n* Process Resource Limit Tunables "
		    "(Current:Maximum)\n*\n");
		if (SYM_VALUE(fd_cur)) {
			MEMSEEK(fd_cur);
			MEMREAD(rlim_fd_cur);
		}
		if (SYM_VALUE(fd_max)) {
			MEMSEEK(fd_max);
			MEMREAD(rlim_fd_max);
		}

		printf("0x%16.16x:", rlim_fd_cur);
		printf("0x%16.16x", rlim_fd_max);
		printf("\tfile descriptors\n");

		printf("*\n* Streams Tunables\n*\n");
		if (SYM_VALUE(pnstrpush)) {
			MEMSEEK(pnstrpush);	MEMREAD(nstrpush);
			printf("%6d	maximum number of pushes allowed "
			    "(NSTRPUSH)\n", nstrpush);
		}
		if (SYM_VALUE(pstrmsgsz)) {
			MEMSEEK(pstrmsgsz);	MEMREAD(strmsgsz);
			printf("%6ld	maximum stream message size "
			    "(STRMSGSZ)\n", strmsgsz);
		}
		if (SYM_VALUE(pstrctlsz)) {
			MEMSEEK(pstrctlsz);	MEMREAD(strctlsz);
			printf("%6ld	max size of ctl part of message "
			    "(STRCTLSZ)\n", strctlsz);
		}
	}

	sysvipc("msgsys", "Messages");
	sysvipc("semsys", "Semaphores");
	sysvipc("shmsys", "Shared Memory");

	if (sysfile) {
		if (SYM_VALUE(pts_maxupri)) {
			printf("*\n* Time Sharing Scheduler Tunables\n*\n");
			MEMSEEK(pts_maxupri);	MEMREAD(ts_maxupri);
			printf("%d	maximum time sharing user "
			    "priority (TSMAXUPRI)\n", ts_maxupri);
		}

		if (SYM_VALUE(psys_name)) {
			MEMSEEK(psys_name);	MEMREAD(sys_name);
			printf("%s	system class name (SYS_NAME)\n",
			    sys_name);
		}

		if (elfd)
			elf_end(elfd);
	}
	return (0);
}

/*
 * setup - add an entry to a namelist structure array
 */
int
setup(char *nam)
{
	int idx;

	if (nlptr >= &nl[nlsize]) {
		if ((nl = (struct nlist *)realloc(nl,
		    (nlsize + EXPAND) * sizeof (struct nlist))) == NULL) {
			fprintf(stderr, "Namelist space allocation failed\n");
			exit(1);
		}
		nlptr = &nl[nlsize];
		nlsize += EXPAND;
	}

	nlptr->n_name = malloc(strlen(nam) + 1); /* pointer to next string */
	strcpy(nlptr->n_name, nam);	/* move name into string table */
	nlptr->n_type = 0;
	nlptr->n_value = 0;
	idx = nlptr++ - nl;
	return (idx);
}

/*
 * Handle the configured devices
 */
void
devices(void)
{
	setegid(egid);
	sysdef_devinfo();
	setegid(getgid());
}

char	*LS_MODULES = "/bin/ls -R -p -i -1 ";
char	*MODULES_TMPFILE = "/tmp/sysdef.sort.XXXXXX";

void
modules()
{
	int i;
	int n_dirs = 0;
	ino_t *inodes;
	char *curr, *next;
	char **dirs;
	char *modpath, *ls_cmd;
	char *tmpf;
	int curr_len, modpathlen;
	int ls_cmd_len = strlen(LS_MODULES);
	int sfd;

	if ((modctl(MODGETPATHLEN, NULL, &modpathlen)) != 0) {
		fprintf(stderr, "sysdef: fail to get module path length\n");
		exit(1);
	}
	if ((modpath = malloc(modpathlen + 1)) == NULL) {
		fprintf(stderr, "sysdef: malloc failed\n");
		exit(1);
	}
	if (modctl(MODGETPATH, NULL, modpath) != 0) {
		fprintf(stderr, "sysdef: fail to get module path\n");
		exit(1);
	}

	/*
	 * Figure out number of directory entries in modpath.
	 * Module paths are stored in a space separated string
	 */
	curr = modpath;
	while (curr) {
		n_dirs++;
		curr = strchr(curr + 1, ' ');
	}

	if (((inodes = (ino_t *)malloc(n_dirs * sizeof (ino_t))) == NULL) ||
	    ((dirs = (char **)malloc(n_dirs * sizeof (char *))) == NULL)) {
		fprintf(stderr, "sysdef: malloc failed\n");
		exit(1);
	}

	if ((tmpf = malloc(strlen(MODULES_TMPFILE) + 1)) == NULL) {
		fprintf(stderr, "sysdef: malloc failed\n");
		exit(1);
	}

	curr = modpath;
	for (i = 0; i < n_dirs; i++) {
		int j, len, inode, ino;
		char line[100], path[100], *pathptr = "";
		char srtbuf[100], *sorted_fname;
		FILE *lspipe, *srtpipe, *fp;
		struct stat stat_buf;

		if (next = strchr(curr, ' ')) {
			*next = '\0';
		}

		/*
		 * Make sure the module path is present.
		 */
		if (stat(curr, &stat_buf) == -1) {
			curr = next ? next + 1 : NULL;
			inodes[i] = (ino_t)-1;
			continue;
		}

		/*
		 * On sparcs, /platform/SUNW,... can be symbolic link to
		 * /platform/sun4x. We check the inode number of directory
		 * and skip any duplication.
		 */
		dirs[i] = curr;
		inodes[i] = stat_buf.st_ino;

		for (j = 0; inodes[i] != inodes[j]; j++)
			;
		if (j != i) {
			curr = next ? next + 1 : NULL;
			continue;
		}

		printf("*\n* Loadable Object Path = %s\n*\n", curr);

		curr_len = strlen(curr);
		if ((ls_cmd = malloc(ls_cmd_len + curr_len + 1)) == NULL) {
			fprintf(stderr, "sysdef: malloc failed\n");
			exit(1);
		}

		(void) sprintf(ls_cmd, "%s%s", LS_MODULES, curr);

		/*
		 * List the loadable objects in the directory tree, sorting
		 * them by inode so as to note any hard links.  A temporary
		 * file in /tmp  is used to store output from sort before
		 * listing.
		 */
		if ((lspipe = popen(ls_cmd, "r")) == NULL) {
			fprintf(stderr, "sysdef: cannot open ls pipe\n");
			exit(1);
		}
		free(ls_cmd);

		(void) strcpy(tmpf, MODULES_TMPFILE);
		if ((sorted_fname = mktemp(tmpf)) == NULL ||
		    (strcmp(sorted_fname, "") == 0)) {
			fprintf(stderr,
			    "sysdef: cannot create unique tmp file name\n");
			exit(1);
		}

		if ((sfd = open(sorted_fname, O_RDWR|O_CREAT|O_EXCL,
		    0600)) == -1) {
			fprintf(stderr, "sysdef: cannot open %s\n",
			    sorted_fname);
			exit(1);
		}

		sprintf(srtbuf, "/bin/sort - > %s", sorted_fname);
		if ((srtpipe = popen(srtbuf, "w")) == NULL) {
			fprintf(stderr, "sysdef: cannot open sort pipe\n");
			exit(1);
		}

		while (fgets(line, 99, lspipe) != NULL) {
			char *tmp;
			/*
			 * 'line' has <cr>, skip blank lines & dir entries
			 */
			if (((len = strlen(line)) <= 1) ||
			    (line[len-2] == '/'))
				continue;

			/* remember path of each subdirectory */

			if (line[0] == '/') {
				(void) strcpy(path, &line[curr_len]);
				tmp = strtok(&path[1], ":");
				if ((tmp == NULL) || (tmp[0] == '\n')) {
					continue;
				}
				pathptr = &path[1];
				(void) strcat(pathptr, "/");
				continue;
			} else {
				char *tmp1 = strtok(line, " ");
				tmp = strtok(NULL, "\n");
				/*
				 * eliminate .conf file
				 */
				if (strstr(tmp, ".conf")) {
					continue;
				}
				/*
				 * Printing the (inode, path, module)
				 * ripple.
				 */
				fprintf(srtpipe, "%s %s%s\n",
				    tmp1, pathptr, tmp);
			}
		}
		(void) pclose(lspipe);
		(void) pclose(srtpipe);

		/*
		 * A note on data synchronization. We opened sfd above,
		 * before calling popen, to ensure that the tempfile
		 * was created exclusively to prevent a malicious user
		 * from creating a link in /tmp to make us overwrite
		 * another file. We have never read from sfd, there
		 * can be no stale data cached anywhere.
		 */
		if ((fp = fdopen(sfd, "r")) == NULL) {
			fprintf(stderr, "sysdef: cannot open sorted file: %s",
			    sorted_fname);
			exit(1);
		}
		inode = -1;
		while (fgets(line, 99, fp) != NULL) {

			sscanf(line, "%d %s",  &ino, path);
			if (ino == inode)
				printf("\thard link:  ");
			printf("%s\n", path);
			inode = ino;
		}
		(void) fclose(fp);
		(void) unlink(sorted_fname);
		curr = next ? next + 1 : NULL;
	}
	free(tmpf);
	free(modpath);
}

void
sysdev(void)
{
	printf("  swap files\n");
	fflush(stdout);
	if (system("/sbin/swap -l") < 0)
		fprintf(stderr, "unknown swap file(s)\n");
}

void
memseek(int sym)
{
	Elf_Scn *scn;
	Shdr *eshdr;
	long eoff;

	if (incore) {
		if ((fseek(memfile, nl[sym].n_value, 0)) != 0) {
			fprintf(stderr, "%s: fseek error (in memseek)\n", mem);
			exit(1);
		}
	} else {
		if ((scn = elf_getscn(elfd, nl[sym].n_scnum)) == NULL) {
			fprintf(stderr, "%s: Error reading Scn %d (%s)\n",
				os, nl[sym].n_scnum, elf_errmsg(-1));
			exit(1);
		}

		if ((eshdr = elf_getshdr(scn)) == NULL) {
			fprintf(stderr, "%s: Error reading Shdr %d (%s)\n",
				os, nl[sym].n_scnum, elf_errmsg(-1));
			exit(1);
		}

		eoff = (long)(nl[sym].n_value - eshdr->sh_addr +
		    eshdr->sh_offset);

		if ((fseek(sysfile, eoff, 0)) != 0) {
			fprintf(stderr, "%s: fseek error (in memseek)\n", os);
			exit(1);
		}
	}
}

/*
 * filter out bss symbols if the reads are from the file
 */
void
getnlist(void)
{
	struct nlist *p;

	nlist(os, nl);

	/*
	 * The nlist is done. If any symbol is a bss
	 * and we are not reading from incore, zero
	 * the n_value field. (Won't be printed if
	 * n_value == 0.)
	 */
	if (!incore) {
		for (p = nl; p->n_name && p->n_name[0]; p++) {
			if (p->n_scnum == bss) {
				p->n_value = 0;
			}
		}
	}
}