summaryrefslogtreecommitdiffstats
path: root/usr.sbin
diff options
context:
space:
mode:
authorobrien <obrien@FreeBSD.org>2013-02-08 16:10:16 +0000
committerobrien <obrien@FreeBSD.org>2013-02-08 16:10:16 +0000
commit3028e3f8aba938dfd0bf9fda987b8a72140b8027 (patch)
treeb2f038222ff8a70f687652441df00d2b564c8abe /usr.sbin
parent952a6d5a7cd3d3f9007acfa06805262fc04a105f (diff)
parent1d08d5f677c1dfa810e381073590adbae19cc69f (diff)
downloadFreeBSD-src-3028e3f8aba938dfd0bf9fda987b8a72140b8027.zip
FreeBSD-src-3028e3f8aba938dfd0bf9fda987b8a72140b8027.tar.gz
Sync with HEAD.
Diffstat (limited to 'usr.sbin')
-rw-r--r--usr.sbin/Makefile11
-rw-r--r--usr.sbin/Makefile.amd646
-rw-r--r--usr.sbin/Makefile.i3863
-rw-r--r--usr.sbin/Makefile.sparc643
-rw-r--r--usr.sbin/acpi/acpidb/Makefile32
-rw-r--r--usr.sbin/acpi/acpidump/acpi.c275
-rw-r--r--usr.sbin/acpi/acpidump/acpidump.81
-rw-r--r--usr.sbin/acpi/iasl/Makefile36
-rw-r--r--usr.sbin/arp/arp.818
-rw-r--r--usr.sbin/arp/arp.c75
-rw-r--r--usr.sbin/auditdistd/Makefile32
-rw-r--r--usr.sbin/bhyve/Makefile25
-rw-r--r--usr.sbin/bhyve/acpi.c844
-rw-r--r--usr.sbin/bhyve/acpi.h34
-rw-r--r--usr.sbin/bhyve/atpic.c68
-rw-r--r--usr.sbin/bhyve/bhyverun.c789
-rw-r--r--usr.sbin/bhyve/bhyverun.h53
-rw-r--r--usr.sbin/bhyve/consport.c140
-rw-r--r--usr.sbin/bhyve/dbgport.c138
-rw-r--r--usr.sbin/bhyve/dbgport.h36
-rw-r--r--usr.sbin/bhyve/elcr.c65
-rw-r--r--usr.sbin/bhyve/inout.c151
-rw-r--r--usr.sbin/bhyve/inout.h67
-rw-r--r--usr.sbin/bhyve/ioapic.c324
-rw-r--r--usr.sbin/bhyve/ioapic.h38
-rw-r--r--usr.sbin/bhyve/mem.c218
-rw-r--r--usr.sbin/bhyve/mem.h57
-rw-r--r--usr.sbin/bhyve/mevent.c432
-rw-r--r--usr.sbin/bhyve/mevent.h49
-rw-r--r--usr.sbin/bhyve/mevent_test.c180
-rw-r--r--usr.sbin/bhyve/mptbl.c398
-rw-r--r--usr.sbin/bhyve/mptbl.h35
-rw-r--r--usr.sbin/bhyve/pci_emul.c1339
-rw-r--r--usr.sbin/bhyve/pci_emul.h224
-rw-r--r--usr.sbin/bhyve/pci_hostbridge.c52
-rw-r--r--usr.sbin/bhyve/pci_passthru.c759
-rw-r--r--usr.sbin/bhyve/pci_uart.c626
-rw-r--r--usr.sbin/bhyve/pci_virtio_block.c624
-rw-r--r--usr.sbin/bhyve/pci_virtio_net.c886
-rw-r--r--usr.sbin/bhyve/pit_8254.c198
-rw-r--r--usr.sbin/bhyve/pit_8254.h (renamed from usr.sbin/sade/list.h)50
-rw-r--r--usr.sbin/bhyve/pmtmr.c105
-rw-r--r--usr.sbin/bhyve/post.c51
-rw-r--r--usr.sbin/bhyve/rtc.c284
-rw-r--r--usr.sbin/bhyve/spinup_ap.c119
-rw-r--r--usr.sbin/bhyve/spinup_ap.h34
-rw-r--r--usr.sbin/bhyve/uart.c60
-rw-r--r--usr.sbin/bhyve/virtio.h88
-rw-r--r--usr.sbin/bhyve/xmsr.c48
-rw-r--r--usr.sbin/bhyve/xmsr.h34
-rw-r--r--usr.sbin/bhyvectl/Makefile17
-rw-r--r--usr.sbin/bhyvectl/bhyvectl.c1524
-rw-r--r--usr.sbin/bhyveload/Makefile14
-rw-r--r--usr.sbin/bhyveload/bhyveload.8127
-rw-r--r--usr.sbin/bhyveload/bhyveload.c652
-rw-r--r--usr.sbin/bluetooth/hcseriald/hcseriald.82
-rw-r--r--usr.sbin/bluetooth/hcseriald/hcseriald.c2
-rw-r--r--usr.sbin/bsdconfig/Makefile2
-rw-r--r--usr.sbin/bsdconfig/USAGE5
-rwxr-xr-xusr.sbin/bsdconfig/bsdconfig103
-rw-r--r--usr.sbin/bsdconfig/bsdconfig.815
-rwxr-xr-xusr.sbin/bsdconfig/console/console4
-rwxr-xr-xusr.sbin/bsdconfig/console/font4
-rwxr-xr-xusr.sbin/bsdconfig/console/keymap4
-rwxr-xr-xusr.sbin/bsdconfig/console/repeat4
-rwxr-xr-xusr.sbin/bsdconfig/console/saver33
-rwxr-xr-xusr.sbin/bsdconfig/console/screenmap4
-rwxr-xr-xusr.sbin/bsdconfig/console/ttys6
-rwxr-xr-xusr.sbin/bsdconfig/diskmgmt/diskmgmt4
-rwxr-xr-xusr.sbin/bsdconfig/docsinstall/docsinstall4
-rwxr-xr-xusr.sbin/bsdconfig/dot/dot3
-rw-r--r--usr.sbin/bsdconfig/examples/bsdconfigrc3
-rw-r--r--usr.sbin/bsdconfig/include/messages.subr2
-rwxr-xr-xusr.sbin/bsdconfig/mouse/disable4
-rwxr-xr-xusr.sbin/bsdconfig/mouse/enable4
-rwxr-xr-xusr.sbin/bsdconfig/mouse/flags32
-rwxr-xr-xusr.sbin/bsdconfig/mouse/mouse4
-rwxr-xr-xusr.sbin/bsdconfig/mouse/port4
-rwxr-xr-xusr.sbin/bsdconfig/mouse/type4
-rwxr-xr-xusr.sbin/bsdconfig/networking/defaultrouter4
-rwxr-xr-xusr.sbin/bsdconfig/networking/devices4
-rwxr-xr-xusr.sbin/bsdconfig/networking/hostname4
-rw-r--r--usr.sbin/bsdconfig/networking/include/messages.subr12
-rwxr-xr-xusr.sbin/bsdconfig/networking/nameservers4
-rwxr-xr-xusr.sbin/bsdconfig/networking/networking4
-rw-r--r--usr.sbin/bsdconfig/networking/share/common.subr4
-rw-r--r--usr.sbin/bsdconfig/networking/share/device.subr23
-rw-r--r--usr.sbin/bsdconfig/networking/share/hostname.subr115
-rw-r--r--usr.sbin/bsdconfig/networking/share/ipaddr.subr272
-rw-r--r--usr.sbin/bsdconfig/networking/share/media.subr13
-rw-r--r--usr.sbin/bsdconfig/networking/share/netmask.subr99
-rw-r--r--usr.sbin/bsdconfig/networking/share/resolv.subr35
-rw-r--r--usr.sbin/bsdconfig/networking/share/routing.subr45
-rwxr-xr-xusr.sbin/bsdconfig/password/password6
-rw-r--r--usr.sbin/bsdconfig/password/share/password.subr9
-rwxr-xr-xusr.sbin/bsdconfig/security/kern_securelevel5
-rwxr-xr-xusr.sbin/bsdconfig/security/security4
-rw-r--r--usr.sbin/bsdconfig/share/Makefile3
-rw-r--r--usr.sbin/bsdconfig/share/common.subr353
-rw-r--r--usr.sbin/bsdconfig/share/dialog.subr151
-rw-r--r--usr.sbin/bsdconfig/share/mustberoot.subr15
-rw-r--r--usr.sbin/bsdconfig/share/script.subr154
-rw-r--r--usr.sbin/bsdconfig/share/strings.subr2
-rw-r--r--usr.sbin/bsdconfig/share/sysrc.subr9
-rw-r--r--usr.sbin/bsdconfig/share/variable.subr185
-rwxr-xr-xusr.sbin/bsdconfig/startup/misc55
-rwxr-xr-xusr.sbin/bsdconfig/startup/rcadd4
-rwxr-xr-xusr.sbin/bsdconfig/startup/rcconf4
-rwxr-xr-xusr.sbin/bsdconfig/startup/rcdelete10
-rwxr-xr-xusr.sbin/bsdconfig/startup/rcedit4
-rwxr-xr-xusr.sbin/bsdconfig/startup/rcvar6
-rw-r--r--usr.sbin/bsdconfig/startup/share/rcconf.subr42
-rw-r--r--usr.sbin/bsdconfig/startup/share/rcedit.subr45
-rw-r--r--usr.sbin/bsdconfig/startup/share/rcvar.subr26
-rwxr-xr-xusr.sbin/bsdconfig/startup/startup4
-rw-r--r--usr.sbin/bsdconfig/timezone/share/continents.subr5
-rw-r--r--usr.sbin/bsdconfig/timezone/share/countries.subr2
-rw-r--r--usr.sbin/bsdconfig/timezone/share/iso3166.subr5
-rw-r--r--usr.sbin/bsdconfig/timezone/share/menus.subr5
-rw-r--r--usr.sbin/bsdconfig/timezone/share/zones.subr7
-rwxr-xr-xusr.sbin/bsdconfig/timezone/timezone4
-rwxr-xr-xusr.sbin/bsdconfig/ttys/ttys4
-rwxr-xr-xusr.sbin/bsdconfig/usermgmt/groupadd4
-rwxr-xr-xusr.sbin/bsdconfig/usermgmt/groupdel6
-rwxr-xr-xusr.sbin/bsdconfig/usermgmt/groupedit6
-rwxr-xr-xusr.sbin/bsdconfig/usermgmt/groupinput28
-rw-r--r--usr.sbin/bsdconfig/usermgmt/share/group_input.subr116
-rw-r--r--usr.sbin/bsdconfig/usermgmt/share/user_input.subr380
-rwxr-xr-xusr.sbin/bsdconfig/usermgmt/useradd4
-rwxr-xr-xusr.sbin/bsdconfig/usermgmt/userdel6
-rwxr-xr-xusr.sbin/bsdconfig/usermgmt/useredit6
-rwxr-xr-xusr.sbin/bsdconfig/usermgmt/userinput49
-rwxr-xr-xusr.sbin/bsdconfig/usermgmt/usermgmt7
-rw-r--r--usr.sbin/bsdinstall/bsdinstall.8149
-rw-r--r--usr.sbin/bsdinstall/partedit/Makefile8
-rw-r--r--usr.sbin/bsdinstall/partedit/diskeditor.c2
-rw-r--r--usr.sbin/bsdinstall/partedit/part_wizard.c15
-rw-r--r--usr.sbin/bsdinstall/partedit/partedit.c26
-rw-r--r--usr.sbin/bsdinstall/partedit/partedit.h2
-rw-r--r--usr.sbin/bsdinstall/partedit/sade.8 (renamed from usr.sbin/sade/sade.8)30
-rw-r--r--usr.sbin/bsdinstall/partedit/scripted.c213
-rw-r--r--usr.sbin/bsdinstall/scripts/Makefile2
-rwxr-xr-xusr.sbin/bsdinstall/scripts/mirrorselect16
-rwxr-xr-xusr.sbin/bsdinstall/scripts/script90
-rwxr-xr-xusr.sbin/bsdinstall/scripts/services2
-rwxr-xr-xusr.sbin/bsdinstall/scripts/wlanconfig2
-rw-r--r--[-rwxr-xr-x]usr.sbin/bsnmpd/modules/snmp_target/Makefile0
-rw-r--r--[-rwxr-xr-x]usr.sbin/bsnmpd/modules/snmp_usm/Makefile0
-rw-r--r--[-rwxr-xr-x]usr.sbin/bsnmpd/modules/snmp_vacm/Makefile0
-rw-r--r--[-rwxr-xr-x]usr.sbin/bsnmpd/tools/libbsnmptools/bsnmptools.c0
-rw-r--r--usr.sbin/chkgrp/chkgrp.87
-rw-r--r--usr.sbin/chkgrp/chkgrp.c66
-rw-r--r--usr.sbin/cpucontrol/intel.c3
-rw-r--r--usr.sbin/cpucontrol/via.c3
-rw-r--r--usr.sbin/crunch/crunchgen/crunchgen.c36
-rw-r--r--usr.sbin/crunch/crunchide/exec_elf32.c230
-rw-r--r--usr.sbin/crunch/examples/really-big.conf2
-rw-r--r--usr.sbin/daemon/daemon.c2
-rw-r--r--usr.sbin/edquota/edquota.c2
-rw-r--r--usr.sbin/gssd/Makefile8
-rw-r--r--usr.sbin/gssd/gssd.825
-rw-r--r--usr.sbin/gssd/gssd.c340
-rw-r--r--usr.sbin/ifmcstat/ifmcstat.c47
-rw-r--r--usr.sbin/inetd/inetd.c2
-rw-r--r--usr.sbin/iostat/iostat.828
-rw-r--r--usr.sbin/iostat/iostat.c27
-rwxr-xr-xusr.sbin/mergemaster/mergemaster.sh19
-rw-r--r--usr.sbin/mountd/exports.57
-rw-r--r--usr.sbin/mountd/mountd.c2
-rw-r--r--usr.sbin/moused/moused.810
-rw-r--r--usr.sbin/mptable/mptable.c16
-rw-r--r--usr.sbin/mtest/mtest.c4
-rw-r--r--usr.sbin/mtree/Makefile16
-rw-r--r--usr.sbin/ndiscvt/inf-parse.y1
-rw-r--r--usr.sbin/ndiscvt/ndiscvt.c2
-rw-r--r--usr.sbin/ndp/ndp.815
-rw-r--r--usr.sbin/ndp/ndp.c67
-rw-r--r--usr.sbin/newsyslog/newsyslog.84
-rw-r--r--usr.sbin/newsyslog/newsyslog.c40
-rw-r--r--usr.sbin/nfscbd/nfscbd.86
-rw-r--r--usr.sbin/nfsd/nfsd.815
-rw-r--r--usr.sbin/nfsd/nfsd.c123
-rw-r--r--usr.sbin/nfsd/nfsv4.44
-rw-r--r--usr.sbin/nmtree/Makefile33
-rw-r--r--usr.sbin/pciconf/Makefile2
-rw-r--r--usr.sbin/pciconf/cap.c26
-rw-r--r--usr.sbin/pkg/Makefile2
-rw-r--r--usr.sbin/pkg/dns_utils.c135
-rw-r--r--usr.sbin/pkg/dns_utils.h45
-rw-r--r--usr.sbin/pkg/pkg.c56
-rw-r--r--usr.sbin/pkg_install/add/main.c2
-rw-r--r--usr.sbin/pkg_install/add/pkg_add.16
-rw-r--r--usr.sbin/pkg_install/create/pkg_create.16
-rw-r--r--usr.sbin/pkg_install/delete/pkg_delete.16
-rw-r--r--usr.sbin/pkg_install/info/pkg_info.110
-rw-r--r--usr.sbin/pkg_install/lib/lib.h2
-rw-r--r--usr.sbin/pkg_install/lib/pkgng.c47
-rw-r--r--usr.sbin/pkg_install/updating/pkg_updating.19
-rw-r--r--usr.sbin/pkg_install/version/pkg_version.19
-rw-r--r--usr.sbin/portsnap/portsnap/portsnap.sh28
-rw-r--r--usr.sbin/ppp/README.changes2
-rw-r--r--usr.sbin/ppp/arp.c5
-rw-r--r--usr.sbin/ppp/defs.h2
-rw-r--r--usr.sbin/ppp/ppp.8.m420
-rw-r--r--usr.sbin/pw/bitmap.c3
-rw-r--r--usr.sbin/pw/grupd.c11
-rw-r--r--usr.sbin/pw/pw_group.c5
-rw-r--r--usr.sbin/pw/pw_log.c2
-rw-r--r--usr.sbin/pw/pw_nis.c2
-rw-r--r--usr.sbin/pw/pw_user.c29
-rw-r--r--usr.sbin/pw/pw_vpw.c236
-rw-r--r--usr.sbin/pw/pwupd.c16
-rw-r--r--usr.sbin/pw/rm_r.c2
-rw-r--r--usr.sbin/rarpd/rarpd.c9
-rw-r--r--usr.sbin/route6d/route6d.846
-rw-r--r--usr.sbin/route6d/route6d.c967
-rw-r--r--usr.sbin/rpcbind/rpcbind.c4
-rw-r--r--usr.sbin/rpcbind/util.c24
-rw-r--r--usr.sbin/sade/Makefile27
-rw-r--r--usr.sbin/sade/Makefile.depend24
-rw-r--r--usr.sbin/sade/command.c179
-rw-r--r--usr.sbin/sade/devices.c344
-rw-r--r--usr.sbin/sade/disks.c972
-rw-r--r--usr.sbin/sade/dmenu.c93
-rw-r--r--usr.sbin/sade/globals.c84
-rw-r--r--usr.sbin/sade/help/partition.hlp169
-rw-r--r--usr.sbin/sade/help/slice.hlp57
-rw-r--r--usr.sbin/sade/install.c249
-rw-r--r--usr.sbin/sade/label.c1646
-rw-r--r--usr.sbin/sade/main.c123
-rw-r--r--usr.sbin/sade/menus.c109
-rw-r--r--usr.sbin/sade/misc.c426
-rw-r--r--usr.sbin/sade/msg.c357
-rw-r--r--usr.sbin/sade/sade.h465
-rw-r--r--usr.sbin/sade/system.c304
-rw-r--r--usr.sbin/sade/termcap.c104
-rw-r--r--usr.sbin/sade/variable.c324
-rw-r--r--usr.sbin/sade/wizard.c200
-rw-r--r--usr.sbin/service/service.86
-rwxr-xr-xusr.sbin/service/service.sh31
-rw-r--r--usr.sbin/syslogd/syslogd.c6
-rw-r--r--usr.sbin/sysrc/Makefile2
-rw-r--r--usr.sbin/tcpdrop/tcpdrop.85
-rw-r--r--usr.sbin/tcpdrop/tcpdrop.c38
-rw-r--r--usr.sbin/timed/timed/acksend.c19
-rw-r--r--usr.sbin/timed/timed/byteorder.c6
-rw-r--r--usr.sbin/timed/timed/candidate.c3
-rw-r--r--usr.sbin/timed/timed/cksum.c4
-rw-r--r--usr.sbin/timed/timed/correct.c10
-rw-r--r--usr.sbin/timed/timed/master.c41
-rw-r--r--usr.sbin/timed/timed/measure.c20
-rw-r--r--usr.sbin/timed/timed/networkdelta.c2
-rw-r--r--usr.sbin/timed/timed/readmsg.c16
-rw-r--r--usr.sbin/timed/timed/slave.c11
-rw-r--r--usr.sbin/timed/timed/timed.c46
-rw-r--r--usr.sbin/timed/timedc/cmds.c24
-rw-r--r--usr.sbin/timed/timedc/timedc.c16
-rw-r--r--usr.sbin/watchdogd/watchdogd.c10
-rw-r--r--usr.sbin/wpa/wpa_supplicant/wpa_supplicant.871
-rw-r--r--usr.sbin/ypserv/yp_main.c2
260 files changed, 16637 insertions, 8735 deletions
diff --git a/usr.sbin/Makefile b/usr.sbin/Makefile
index 296856b..532595b 100644
--- a/usr.sbin/Makefile
+++ b/usr.sbin/Makefile
@@ -39,7 +39,6 @@ SUBDIR= adduser \
kldxref \
mailwrapper \
makefs \
- manctl \
memcontrol \
mergemaster \
mfiutil \
@@ -55,8 +54,9 @@ SUBDIR= adduser \
nfsdumpstate \
nfsrevoke \
nfsuserd \
+ nmtree \
nologin \
- pc-sysinstall \
+ ${_pc_sysinstall} \
pciconf \
periodic \
powerd \
@@ -110,6 +110,9 @@ SUBDIR+= amd
.if ${MK_AUDIT} != "no"
SUBDIR+= audit
SUBDIR+= auditd
+.if ${MK_OPENSSL} != "no"
+SUBDIR+= auditdistd
+.endif
SUBDIR+= auditreduce
SUBDIR+= praudit
.endif
@@ -261,6 +264,10 @@ SUBDIR+= ntp
SUBDIR+= keyserv
.endif
+.if ${MK_PC_SYSINSTALL} != "no"
+_pc_sysinstall= pc-sysinstall
+.endif
+
.if ${MK_PF} != "no"
SUBDIR+= ftp-proxy
.endif
diff --git a/usr.sbin/Makefile.amd64 b/usr.sbin/Makefile.amd64
index 26eb2b0..5ee2165 100644
--- a/usr.sbin/Makefile.amd64
+++ b/usr.sbin/Makefile.amd64
@@ -10,6 +10,9 @@ SUBDIR+= acpi
SUBDIR+= apm
.endif
SUBDIR+= asf
+SUBDIR+= bhyve
+SUBDIR+= bhyvectl
+SUBDIR+= bhyveload
SUBDIR+= boot0cfg
.if ${MK_TOOLCHAIN} != "no"
SUBDIR+= btxld
@@ -23,7 +26,4 @@ SUBDIR+= ndiscvt
.endif
SUBDIR+= sicontrol
SUBDIR+= spkrtest
-.if ${MK_SYSINSTALL} != "no"
-SUBDIR+= sade
-.endif
SUBDIR+= zzz
diff --git a/usr.sbin/Makefile.i386 b/usr.sbin/Makefile.i386
index 9588eb8..901c2d9 100644
--- a/usr.sbin/Makefile.i386
+++ b/usr.sbin/Makefile.i386
@@ -17,9 +17,6 @@ SUBDIR+= mptable
SUBDIR+= ndiscvt
.endif
SUBDIR+= pnpinfo
-.if ${MK_SYSINSTALL} != "no"
-SUBDIR+= sade
-.endif
SUBDIR+= sicontrol
SUBDIR+= spkrtest
SUBDIR+= zzz
diff --git a/usr.sbin/Makefile.sparc64 b/usr.sbin/Makefile.sparc64
index 479dafc..81f7a9b 100644
--- a/usr.sbin/Makefile.sparc64
+++ b/usr.sbin/Makefile.sparc64
@@ -2,6 +2,3 @@
SUBDIR+= eeprom
SUBDIR+= ofwdump
-.if ${MK_SYSINSTALL} != "no"
-SUBDIR+= sade
-.endif
diff --git a/usr.sbin/acpi/acpidb/Makefile b/usr.sbin/acpi/acpidb/Makefile
index eab9e38..9eccb4d 100644
--- a/usr.sbin/acpi/acpidb/Makefile
+++ b/usr.sbin/acpi/acpidb/Makefile
@@ -4,13 +4,14 @@ PROG= acpidb
SRCS= acpidb.c
# components/debugger
-SRCS+= dbcmds.c dbdisply.c dbexec.c dbfileio.c dbhistry.c \
- dbinput.c dbmethod.c dbnames.c dbstats.c dbutils.c \
- dbxface.c
+SRCS+= dbcmds.c dbconvert.c dbdisply.c dbexec.c dbfileio.c \
+ dbhistry.c dbinput.c dbmethod.c dbnames.c dbstats.c \
+ dbutils.c dbxface.c
# components/disassembler
-SRCS+= dmbuffer.c dmnames.c dmobject.c dmopcode.c dmresrc.c \
- dmresrcl.c dmresrcl2.c dmresrcs.c dmutils.c dmwalk.c
+SRCS+= dmbuffer.c dmdeferred.c dmnames.c dmobject.c dmopcode.c \
+ dmresrc.c dmresrcl.c dmresrcl2.c dmresrcs.c dmutils.c \
+ dmwalk.c
# components/dispatcher
SRCS+= dsargs.c dscontrol.c dsfield.c dsinit.c dsmethod.c \
@@ -19,8 +20,8 @@ SRCS+= dsargs.c dscontrol.c dsfield.c dsinit.c dsmethod.c \
# components/events
SRCS+= evevent.c evglock.c evgpe.c evgpeblk.c evgpeinit.c \
- evgpeutil.c evmisc.c evregion.c evrgnini.c evsci.c \
- evxface.c evxfevnt.c evxfregn.c
+ evgpeutil.c evhandler.c evmisc.c evregion.c evrgnini.c \
+ evsci.c evxface.c evxfevnt.c evxfregn.c
# components/executer
SRCS+= exconfig.c exconvrt.c excreate.c exdebug.c exdump.c \
@@ -36,19 +37,20 @@ SRCS+= hwacpi.c hwesleep.c hwgpe.c hwpci.c hwregs.c hwsleep.c \
# components/namespace
SRCS+= nsaccess.c nsalloc.c nsdump.c nseval.c nsinit.c \
nsload.c nsnames.c nsobject.c nsparse.c nspredef.c \
- nsrepair.c nsrepair2.c nssearch.c nsutils.c nswalk.c \
- nsxfeval.c nsxfname.c nsxfobj.c
+ nsprepkg.c nsrepair.c nsrepair2.c nssearch.c nsutils.c \
+ nswalk.c nsxfeval.c nsxfname.c nsxfobj.c
# components/parser
-SRCS+= psargs.c psloop.c psopcode.c psparse.c psscope.c \
- pstree.c psutils.c pswalk.c psxface.c
+SRCS+= psargs.c psloop.c psobject.c psopcode.c psopinfo.c \
+ psparse.c psscope.c pstree.c psutils.c pswalk.c \
+ psxface.c
# components/os_specific/service_layers
SRCS+= osunixxf.c
# components/resources
-SRCS+= rsaddr.c rscalc.c rscreate.c rsdump.c rsinfo.c \
- rsio.c rsirq.c rslist.c rsmemory.c rsmisc.c \
+SRCS+= rsaddr.c rscalc.c rscreate.c rsdump.c rsdumpinfo.c \
+ rsinfo.c rsio.c rsirq.c rslist.c rsmemory.c rsmisc.c \
rsserial.c rsutils.c rsxface.c
# components/tables
@@ -59,8 +61,8 @@ SRCS+= tbfadt.c tbfind.c tbinstal.c tbutils.c tbxface.c \
SRCS+= utaddress.c utalloc.c utcache.c utcopy.c utdebug.c \
utdecode.c utdelete.c uteval.c utexcep.c utglobal.c \
utids.c utinit.c utlock.c utmath.c utmisc.c utmutex.c \
- utobject.c utosi.c utresrc.c utstate.c uttrack.c \
- utxface.c utxferror.c utxfinit.c
+ utobject.c utosi.c utownerid.c utresrc.c utstate.c \
+ utstring.c uttrack.c utxface.c utxferror.c utxfinit.c
MAN= acpidb.8
WARNS?= 2
diff --git a/usr.sbin/acpi/acpidump/acpi.c b/usr.sbin/acpi/acpidump/acpi.c
index b0a751c..c7f5bd8 100644
--- a/usr.sbin/acpi/acpidump/acpi.c
+++ b/usr.sbin/acpi/acpidump/acpi.c
@@ -123,6 +123,31 @@ static const char *TCPA_pcclient_strings[] = {
"Table of Devices",
};
+#define PRINTFLAG_END() printflag_end()
+
+static char pf_sep = '{';
+
+static void
+printflag_end(void)
+{
+
+ if (pf_sep != '{') {
+ printf("}");
+ pf_sep = '{';
+ }
+ printf("\n");
+}
+
+static void
+printflag(uint64_t var, uint64_t mask, const char *name)
+{
+
+ if (var & mask) {
+ printf("%c%s", pf_sep, name);
+ pf_sep = ',';
+ }
+}
+
static void
acpi_print_string(char *s, size_t length)
{
@@ -729,6 +754,238 @@ acpi_handle_tcpa(ACPI_TABLE_HEADER *sdp)
printf(END_COMMENT);
}
+static const char *
+devscope_type2str(int type)
+{
+ static char typebuf[16];
+
+ switch (type) {
+ case 1:
+ return ("PCI Endpoint Device");
+ case 2:
+ return ("PCI Sub-Hierarchy");
+ case 3:
+ return ("IOAPIC");
+ case 4:
+ return ("HPET");
+ default:
+ snprintf(typebuf, sizeof(typebuf), "%d", type);
+ return (typebuf);
+ }
+}
+
+static int
+acpi_handle_dmar_devscope(void *addr, int remaining)
+{
+ char sep;
+ int pathlen;
+ ACPI_DMAR_PCI_PATH *path, *pathend;
+ ACPI_DMAR_DEVICE_SCOPE *devscope = addr;
+
+ if (remaining < (int)sizeof(ACPI_DMAR_DEVICE_SCOPE))
+ return (-1);
+
+ if (remaining < devscope->Length)
+ return (-1);
+
+ printf("\n");
+ printf("\t\tType=%s\n", devscope_type2str(devscope->EntryType));
+ printf("\t\tLength=%d\n", devscope->Length);
+ printf("\t\tEnumerationId=%d\n", devscope->EnumerationId);
+ printf("\t\tStartBusNumber=%d\n", devscope->Bus);
+
+ path = (ACPI_DMAR_PCI_PATH *)(devscope + 1);
+ pathlen = devscope->Length - sizeof(ACPI_DMAR_DEVICE_SCOPE);
+ pathend = path + pathlen / sizeof(ACPI_DMAR_PCI_PATH);
+ if (path < pathend) {
+ sep = '{';
+ printf("\t\tPath=");
+ do {
+ printf("%c%d:%d", sep, path->Device, path->Function);
+ sep=',';
+ path++;
+ } while (path < pathend);
+ printf("}\n");
+ }
+
+ return (devscope->Length);
+}
+
+static void
+acpi_handle_dmar_drhd(ACPI_DMAR_HARDWARE_UNIT *drhd)
+{
+ char *cp;
+ int remaining, consumed;
+
+ printf("\n");
+ printf("\tType=DRHD\n");
+ printf("\tLength=%d\n", drhd->Header.Length);
+
+#define PRINTFLAG(var, flag) printflag((var), ACPI_DMAR_## flag, #flag)
+
+ printf("\tFlags=");
+ PRINTFLAG(drhd->Flags, INCLUDE_ALL);
+ PRINTFLAG_END();
+
+#undef PRINTFLAG
+
+ printf("\tSegment=%d\n", drhd->Segment);
+ printf("\tAddress=0x%0jx\n", drhd->Address);
+
+ remaining = drhd->Header.Length - sizeof(ACPI_DMAR_HARDWARE_UNIT);
+ if (remaining > 0)
+ printf("\tDevice Scope:");
+ while (remaining > 0) {
+ cp = (char *)drhd + drhd->Header.Length - remaining;
+ consumed = acpi_handle_dmar_devscope(cp, remaining);
+ if (consumed <= 0)
+ break;
+ else
+ remaining -= consumed;
+ }
+}
+
+static void
+acpi_handle_dmar_rmrr(ACPI_DMAR_RESERVED_MEMORY *rmrr)
+{
+ char *cp;
+ int remaining, consumed;
+
+ printf("\n");
+ printf("\tType=RMRR\n");
+ printf("\tLength=%d\n", rmrr->Header.Length);
+ printf("\tSegment=%d\n", rmrr->Segment);
+ printf("\tBaseAddress=0x%0jx\n", rmrr->BaseAddress);
+ printf("\tLimitAddress=0x%0jx\n", rmrr->EndAddress);
+
+ remaining = rmrr->Header.Length - sizeof(ACPI_DMAR_RESERVED_MEMORY);
+ if (remaining > 0)
+ printf("\tDevice Scope:");
+ while (remaining > 0) {
+ cp = (char *)rmrr + rmrr->Header.Length - remaining;
+ consumed = acpi_handle_dmar_devscope(cp, remaining);
+ if (consumed <= 0)
+ break;
+ else
+ remaining -= consumed;
+ }
+}
+
+static void
+acpi_handle_dmar_atsr(ACPI_DMAR_ATSR *atsr)
+{
+ char *cp;
+ int remaining, consumed;
+
+ printf("\n");
+ printf("\tType=ATSR\n");
+ printf("\tLength=%d\n", atsr->Header.Length);
+
+#define PRINTFLAG(var, flag) printflag((var), ACPI_DMAR_## flag, #flag)
+
+ printf("\tFlags=");
+ PRINTFLAG(atsr->Flags, ALL_PORTS);
+ PRINTFLAG_END();
+
+#undef PRINTFLAG
+
+ printf("\tSegment=%d\n", atsr->Segment);
+
+ remaining = atsr->Header.Length - sizeof(ACPI_DMAR_ATSR);
+ if (remaining > 0)
+ printf("\tDevice Scope:");
+ while (remaining > 0) {
+ cp = (char *)atsr + atsr->Header.Length - remaining;
+ consumed = acpi_handle_dmar_devscope(cp, remaining);
+ if (consumed <= 0)
+ break;
+ else
+ remaining -= consumed;
+ }
+}
+
+static void
+acpi_handle_dmar_rhsa(ACPI_DMAR_RHSA *rhsa)
+{
+
+ printf("\n");
+ printf("\tType=RHSA\n");
+ printf("\tLength=%d\n", rhsa->Header.Length);
+ printf("\tBaseAddress=0x%0jx\n", rhsa->BaseAddress);
+ printf("\tProximityDomain=0x%08x\n", rhsa->ProximityDomain);
+}
+
+static int
+acpi_handle_dmar_remapping_structure(void *addr, int remaining)
+{
+ ACPI_DMAR_HEADER *hdr = addr;
+
+ if (remaining < (int)sizeof(ACPI_DMAR_HEADER))
+ return (-1);
+
+ if (remaining < hdr->Length)
+ return (-1);
+
+ switch (hdr->Type) {
+ case ACPI_DMAR_TYPE_HARDWARE_UNIT:
+ acpi_handle_dmar_drhd(addr);
+ break;
+ case ACPI_DMAR_TYPE_RESERVED_MEMORY:
+ acpi_handle_dmar_rmrr(addr);
+ break;
+ case ACPI_DMAR_TYPE_ATSR:
+ acpi_handle_dmar_atsr(addr);
+ break;
+ case ACPI_DMAR_HARDWARE_AFFINITY:
+ acpi_handle_dmar_rhsa(addr);
+ break;
+ default:
+ printf("\n");
+ printf("\tType=%d\n", hdr->Type);
+ printf("\tLength=%d\n", hdr->Length);
+ break;
+ }
+ return (hdr->Length);
+}
+
+#ifndef ACPI_DMAR_X2APIC_OPT_OUT
+#define ACPI_DMAR_X2APIC_OPT_OUT (0x2)
+#endif
+
+static void
+acpi_handle_dmar(ACPI_TABLE_HEADER *sdp)
+{
+ char *cp;
+ int remaining, consumed;
+ ACPI_TABLE_DMAR *dmar;
+
+ printf(BEGIN_COMMENT);
+ acpi_print_sdt(sdp);
+ dmar = (ACPI_TABLE_DMAR *)sdp;
+ printf("\tHost Address Width=%d\n", dmar->Width + 1);
+
+#define PRINTFLAG(var, flag) printflag((var), ACPI_DMAR_## flag, #flag)
+
+ printf("\tFlags=");
+ PRINTFLAG(dmar->Flags, INTR_REMAP);
+ PRINTFLAG(dmar->Flags, X2APIC_OPT_OUT);
+ PRINTFLAG_END();
+
+#undef PRINTFLAG
+
+ remaining = sdp->Length - sizeof(ACPI_TABLE_DMAR);
+ while (remaining > 0) {
+ cp = (char *)sdp + sdp->Length - remaining;
+ consumed = acpi_handle_dmar_remapping_structure(cp, remaining);
+ if (consumed <= 0)
+ break;
+ else
+ remaining -= consumed;
+ }
+
+ printf(END_COMMENT);
+}
+
static void
acpi_print_srat_memory(ACPI_SRAT_MEM_AFFINITY *mp)
{
@@ -854,7 +1111,6 @@ acpi_print_fadt(ACPI_TABLE_HEADER *sdp)
{
ACPI_TABLE_FADT *fadt;
const char *pm;
- char sep;
fadt = (ACPI_TABLE_FADT *)sdp;
printf(BEGIN_COMMENT);
@@ -914,25 +1170,17 @@ acpi_print_fadt(ACPI_TABLE_HEADER *sdp)
printf("\tDAY_ALRM=%d, MON_ALRM=%d, CENTURY=%d\n",
fadt->DayAlarm, fadt->MonthAlarm, fadt->Century);
-#define PRINTFLAG(var, flag) do { \
- if ((var) & ACPI_FADT_## flag) { \
- printf("%c%s", sep, #flag); sep = ','; \
- } \
-} while (0)
+#define PRINTFLAG(var, flag) printflag((var), ACPI_FADT_## flag, #flag)
printf("\tIAPC_BOOT_ARCH=");
- sep = '{';
PRINTFLAG(fadt->BootFlags, LEGACY_DEVICES);
PRINTFLAG(fadt->BootFlags, 8042);
PRINTFLAG(fadt->BootFlags, NO_VGA);
PRINTFLAG(fadt->BootFlags, NO_MSI);
PRINTFLAG(fadt->BootFlags, NO_ASPM);
- if (fadt->BootFlags != 0)
- printf("}");
- printf("\n");
+ PRINTFLAG_END();
printf("\tFlags=");
- sep = '{';
PRINTFLAG(fadt->Flags, WBINVD);
PRINTFLAG(fadt->Flags, WBINVD_FLUSH);
PRINTFLAG(fadt->Flags, C1_SUPPORTED);
@@ -953,8 +1201,7 @@ acpi_print_fadt(ACPI_TABLE_HEADER *sdp)
PRINTFLAG(fadt->Flags, REMOTE_POWER_ON);
PRINTFLAG(fadt->Flags, APIC_CLUSTER);
PRINTFLAG(fadt->Flags, APIC_PHYSICAL);
- if (fadt->Flags != 0)
- printf("}\n");
+ PRINTFLAG_END();
#undef PRINTFLAG
@@ -1127,6 +1374,8 @@ acpi_handle_rsdt(ACPI_TABLE_HEADER *rsdp)
acpi_handle_srat(sdp);
else if (!memcmp(sdp->Signature, ACPI_SIG_TCPA, 4))
acpi_handle_tcpa(sdp);
+ else if (!memcmp(sdp->Signature, ACPI_SIG_DMAR, 4))
+ acpi_handle_dmar(sdp);
else {
printf(BEGIN_COMMENT);
acpi_print_sdt(sdp);
diff --git a/usr.sbin/acpi/acpidump/acpidump.8 b/usr.sbin/acpi/acpidump/acpidump.8
index 1401e38..f1c7a18 100644
--- a/usr.sbin/acpi/acpidump/acpidump.8
+++ b/usr.sbin/acpi/acpidump/acpidump.8
@@ -97,6 +97,7 @@ flag, the
utility dumps contents of the following tables:
.Pp
.Bl -tag -offset indent -width 12345 -compact
+.It DMAR
.It DSDT
.It ECDT
.It FACS
diff --git a/usr.sbin/acpi/iasl/Makefile b/usr.sbin/acpi/iasl/Makefile
index 6c263ce..b348141 100644
--- a/usr.sbin/acpi/iasl/Makefile
+++ b/usr.sbin/acpi/iasl/Makefile
@@ -10,25 +10,26 @@ SRCS+= ahpredef.c dmextern.c dmrestag.c dmtable.c dmtbdump.c \
# compiler
SRCS+= aslanalyze.c aslbtypes.c aslcodegen.c aslcompile.c \
aslcompiler.y.h aslcompilerlex.c aslcompilerparse.c \
- aslerror.c aslfiles.c aslfold.c asllength.c \
- asllisting.c aslload.c asllookup.c aslmain.c aslmap.c \
- aslopcodes.c asloperands.c aslopt.c aslpredef.c \
- aslresource.c aslrestype1.c aslrestype1i.c \
- aslrestype2.c aslrestype2d.c aslrestype2e.c \
- aslrestype2q.c aslrestype2s.c aslrestype2w.c \
- aslstartup.c aslstubs.c asltransform.c asltree.c \
- aslutils.c asluuid.c aslwalks.c dtcompile.c dtexpress.c \
- dtfield.c dtio.c dtparser.y.h dtparserlex.c \
- dtparserparse.c dtsubtable.c dttable.c dttemplate.c \
- dtutils.c prexpress.c prmacros.c prparser.y.h \
- prparserlex.c prparserparse.c prscan.c prutils.c
+ aslerror.c aslfileio.c aslfiles.c aslfold.c aslhex.c \
+ asllength.c asllisting.c aslload.c asllookup.c \
+ aslmain.c aslmap.c aslmethod.c aslnamesp.c aslopcodes.c \
+ asloperands.c aslopt.c aslpredef.c aslresource.c \
+ aslrestype1.c aslrestype1i.c aslrestype2.c \
+ aslrestype2d.c aslrestype2e.c aslrestype2q.c \
+ aslrestype2s.c aslrestype2w.c aslstartup.c aslstubs.c \
+ asltransform.c asltree.c aslutils.c asluuid.c \
+ aslwalks.c aslxref.c dtcompile.c dtexpress.c dtfield.c \
+ dtio.c dtparser.y.h dtparserlex.c dtparserparse.c \
+ dtsubtable.c dttable.c dttemplate.c dtutils.c \
+ prexpress.c prmacros.c prparser.y.h prparserlex.c \
+ prparserparse.c prscan.c prutils.c
# components/debugger
SRCS+= dbfileio.c
# components/disassembler
-SRCS+= dmbuffer.c dmnames.c dmopcode.c dmresrc.c dmresrcl.c \
- dmresrcl2.c dmresrcs.c dmutils.c dmwalk.c
+SRCS+= dmbuffer.c dmdeferred.c dmnames.c dmopcode.c dmresrc.c \
+ dmresrcl.c dmresrcl2.c dmresrcs.c dmutils.c dmwalk.c
# components/dispatcher
SRCS+= dsargs.c dscontrol.c dsfield.c dsobject.c dsopcode.c \
@@ -42,8 +43,8 @@ SRCS+= exconvrt.c excreate.c exdump.c exmisc.c exmutex.c \
exstoren.c exstorob.c exsystem.c exutils.c
# components/parser
-SRCS+= psargs.c psloop.c psopcode.c psparse.c psscope.c \
- pstree.c psutils.c pswalk.c
+SRCS+= psargs.c psloop.c psobject.c psopcode.c psopinfo.c \
+ psparse.c psscope.c pstree.c psutils.c pswalk.c
# components/namespace
SRCS+= nsaccess.c nsalloc.c nsdump.c nsnames.c nsobject.c \
@@ -56,7 +57,8 @@ SRCS+= tbfadt.c tbinstal.c tbutils.c tbxface.c
SRCS+= utaddress.c utalloc.c utcache.c utcopy.c utdebug.c \
utdecode.c utdelete.c utexcep.c utglobal.c utinit.c \
utlock.c utmath.c utmisc.c utmutex.c utobject.c \
- utresrc.c utstate.c utxface.c utxferror.c
+ utownerid.c utresrc.c utstate.c utstring.c utxface.c \
+ utxferror.c
# os_specific/service_layers
SRCS+= osunixxf.c
diff --git a/usr.sbin/arp/arp.8 b/usr.sbin/arp/arp.8
index d7306d3..87c5c5f 100644
--- a/usr.sbin/arp/arp.8
+++ b/usr.sbin/arp/arp.8
@@ -28,7 +28,7 @@
.\" @(#)arp.8 8.1 (Berkeley) 6/6/93
.\" $FreeBSD$
.\"
-.Dd December 25, 2008
+.Dd January 31, 2013
.Dt ARP 8
.Os
.Sh NAME
@@ -54,12 +54,12 @@
.Fl s Ar hostname ether_addr
.Op Cm temp
.Op Cm blackhole No \&| Cm reject
-.Op Cm pub Op Cm only
+.Op Cm pub
.Nm
.Fl S Ar hostname ether_addr
.Op Cm temp
.Op Cm blackhole No \&| Cm reject
-.Op Cm pub Op Cm only
+.Op Cm pub
.Nm
.Fl f Ar filename
.Sh DESCRIPTION
@@ -140,16 +140,6 @@ can be given as
in which case the interfaces on this host will be examined,
and if one of them is found to occupy the same subnet, its
Ethernet address will be used.
-If the
-.Cm only
-keyword is also specified, this will create a
-.Dq "published (proxy only)"
-entry.
-This type of entry is created automatically if
-.Nm
-detects that a routing table entry for
-.Ar hostname
-already exists.
.Pp
If the
.Cm reject
@@ -181,7 +171,7 @@ in the file should be of the form
.Ar hostname ether_addr
.Op Cm temp
.Op Cm blackhole No \&| Cm reject
-.Op Cm pub Op Cm only
+.Op Cm pub
.Ed
.Pp
with argument meanings as given above.
diff --git a/usr.sbin/arp/arp.c b/usr.sbin/arp/arp.c
index 9a7f91f..8bdd555 100644
--- a/usr.sbin/arp/arp.c
+++ b/usr.sbin/arp/arp.c
@@ -81,28 +81,28 @@ __FBSDID("$FreeBSD$");
#include <unistd.h>
typedef void (action_fn)(struct sockaddr_dl *sdl,
- struct sockaddr_inarp *s_in, struct rt_msghdr *rtm);
+ struct sockaddr_in *s_in, struct rt_msghdr *rtm);
static int search(u_long addr, action_fn *action);
static action_fn print_entry;
static action_fn nuke_entry;
-static int delete(char *host, int do_proxy);
+static int delete(char *host);
static void usage(void);
static int set(int argc, char **argv);
static int get(char *host);
static int file(char *name);
static struct rt_msghdr *rtmsg(int cmd,
- struct sockaddr_inarp *dst, struct sockaddr_dl *sdl);
+ struct sockaddr_in *dst, struct sockaddr_dl *sdl);
static int get_ether_addr(in_addr_t ipaddr, struct ether_addr *hwaddr);
-static struct sockaddr_inarp *getaddr(char *host);
+static struct sockaddr_in *getaddr(char *host);
static int valid_type(int type);
static int nflag; /* no reverse dns lookups */
static char *rifname;
static time_t expire_time;
-static int flags, doing_proxy, proxy_only;
+static int flags, doing_proxy;
/* which function we're supposed to do */
#define F_GET 1
@@ -179,7 +179,7 @@ main(int argc, char *argv[])
if (argc < 2 || argc > 6)
usage();
if (func == F_REPLACE)
- (void)delete(argv[0], 0);
+ (void)delete(argv[0]);
rtn = set(argc, argv) ? 1 : 0;
break;
case F_DELETE:
@@ -187,15 +187,8 @@ main(int argc, char *argv[])
if (argc != 0)
usage();
search(0, nuke_entry);
- } else {
- if (argc == 2 && strncmp(argv[1], "pub", 3) == 0)
- ch = SIN_PROXY;
- else if (argc == 1)
- ch = 0;
- else
- usage();
- rtn = delete(argv[0], ch);
- }
+ } else
+ rtn = delete(argv[0]);
break;
case F_FILESET:
if (argc != 1)
@@ -246,15 +239,15 @@ file(char *name)
}
/*
- * Given a hostname, fills up a (static) struct sockaddr_inarp with
+ * Given a hostname, fills up a (static) struct sockaddr_in with
* the address of the host and returns a pointer to the
* structure.
*/
-static struct sockaddr_inarp *
+static struct sockaddr_in *
getaddr(char *host)
{
struct hostent *hp;
- static struct sockaddr_inarp reply;
+ static struct sockaddr_in reply;
bzero(&reply, sizeof(reply));
reply.sin_len = sizeof(reply);
@@ -298,8 +291,8 @@ valid_type(int type)
static int
set(int argc, char **argv)
{
- struct sockaddr_inarp *addr;
- struct sockaddr_inarp *dst; /* what are we looking for */
+ struct sockaddr_in *addr;
+ struct sockaddr_in *dst; /* what are we looking for */
struct sockaddr_dl *sdl;
struct rt_msghdr *rtm;
struct ether_addr *ea;
@@ -316,7 +309,7 @@ set(int argc, char **argv)
dst = getaddr(host);
if (dst == NULL)
return (1);
- doing_proxy = flags = proxy_only = expire_time = 0;
+ doing_proxy = flags = expire_time = 0;
while (argc-- > 0) {
if (strncmp(argv[0], "temp", 4) == 0) {
struct timespec tp;
@@ -332,7 +325,12 @@ set(int argc, char **argv)
flags |= RTF_ANNOUNCE;
doing_proxy = 1;
if (argc && strncmp(argv[1], "only", 3) == 0) {
- proxy_only = 1;
+ /*
+ * Compatibility: in pre FreeBSD 8 times
+ * the "only" keyword used to mean that
+ * an ARP entry should be announced, but
+ * not installed into routing table.
+ */
argc--; argv++;
}
} else if (strncmp(argv[0], "blackhole", 9) == 0) {
@@ -385,7 +383,7 @@ set(int argc, char **argv)
warn("%s", host);
return (1);
}
- addr = (struct sockaddr_inarp *)(rtm + 1);
+ addr = (struct sockaddr_in *)(rtm + 1);
sdl = (struct sockaddr_dl *)(SA_SIZE(addr) + (char *)addr);
if ((sdl->sdl_family != AF_LINK) ||
@@ -405,7 +403,7 @@ set(int argc, char **argv)
static int
get(char *host)
{
- struct sockaddr_inarp *addr;
+ struct sockaddr_in *addr;
addr = getaddr(host);
if (addr == NULL)
@@ -425,9 +423,9 @@ get(char *host)
* Delete an arp entry
*/
static int
-delete(char *host, int do_proxy)
+delete(char *host)
{
- struct sockaddr_inarp *addr, *dst;
+ struct sockaddr_in *addr, *dst;
struct rt_msghdr *rtm;
struct sockaddr_dl *sdl;
struct sockaddr_dl sdl_m;
@@ -456,7 +454,7 @@ delete(char *host, int do_proxy)
warn("%s", host);
return (1);
}
- addr = (struct sockaddr_inarp *)(rtm + 1);
+ addr = (struct sockaddr_in *)(rtm + 1);
sdl = (struct sockaddr_dl *)(SA_SIZE(addr) + (char *)addr);
/*
@@ -504,7 +502,7 @@ search(u_long addr, action_fn *action)
size_t needed;
char *lim, *buf, *next;
struct rt_msghdr *rtm;
- struct sockaddr_inarp *sin2;
+ struct sockaddr_in *sin2;
struct sockaddr_dl *sdl;
char ifname[IF_NAMESIZE];
int st, found_entry = 0;
@@ -538,7 +536,7 @@ search(u_long addr, action_fn *action)
lim = buf + needed;
for (next = buf; next < lim; next += rtm->rtm_msglen) {
rtm = (struct rt_msghdr *)next;
- sin2 = (struct sockaddr_inarp *)(rtm + 1);
+ sin2 = (struct sockaddr_in *)(rtm + 1);
sdl = (struct sockaddr_dl *)((char *)sin2 + SA_SIZE(sin2));
if (rifname && if_indextoname(sdl->sdl_index, ifname) &&
strcmp(ifname, rifname))
@@ -562,7 +560,7 @@ static int64_t lifindex = -1;
static void
print_entry(struct sockaddr_dl *sdl,
- struct sockaddr_inarp *addr, struct rt_msghdr *rtm)
+ struct sockaddr_in *addr, struct rt_msghdr *rtm)
{
const char *host;
struct hostent *hp;
@@ -612,8 +610,6 @@ print_entry(struct sockaddr_dl *sdl,
else
printf(" expired");
}
- if (addr->sin_other & SIN_PROXY)
- printf(" published (proxy only)");
if (rtm->rtm_flags & RTF_ANNOUNCE)
printf(" published");
switch(sdl->sdl_type) {
@@ -659,12 +655,12 @@ print_entry(struct sockaddr_dl *sdl,
*/
static void
nuke_entry(struct sockaddr_dl *sdl __unused,
- struct sockaddr_inarp *addr, struct rt_msghdr *rtm __unused)
+ struct sockaddr_in *addr, struct rt_msghdr *rtm __unused)
{
char ip[20];
snprintf(ip, sizeof(ip), "%s", inet_ntoa(addr->sin_addr));
- (void)delete(ip, 0);
+ delete(ip);
}
static void
@@ -682,7 +678,7 @@ usage(void)
}
static struct rt_msghdr *
-rtmsg(int cmd, struct sockaddr_inarp *dst, struct sockaddr_dl *sdl)
+rtmsg(int cmd, struct sockaddr_in *dst, struct sockaddr_dl *sdl)
{
static int seq;
int rlen;
@@ -728,14 +724,9 @@ rtmsg(int cmd, struct sockaddr_inarp *dst, struct sockaddr_dl *sdl)
rtm->rtm_rmx.rmx_expire = expire_time;
rtm->rtm_inits = RTV_EXPIRE;
rtm->rtm_flags |= (RTF_HOST | RTF_STATIC | RTF_LLDATA);
- dst->sin_other = 0;
if (doing_proxy) {
- if (proxy_only)
- dst->sin_other = SIN_PROXY;
- else {
- rtm->rtm_addrs |= RTA_NETMASK;
- rtm->rtm_flags &= ~RTF_HOST;
- }
+ rtm->rtm_addrs |= RTA_NETMASK;
+ rtm->rtm_flags &= ~RTF_HOST;
}
/* FALLTHROUGH */
case RTM_GET:
diff --git a/usr.sbin/auditdistd/Makefile b/usr.sbin/auditdistd/Makefile
new file mode 100644
index 0000000..468290b
--- /dev/null
+++ b/usr.sbin/auditdistd/Makefile
@@ -0,0 +1,32 @@
+#
+# $FreeBSD$
+#
+
+OPENBSMDIR=${.CURDIR}/../../contrib/openbsm
+.PATH: ${OPENBSMDIR}/bin/auditdistd
+
+# Addition of auditdistd because otherwise generated parse.c can't find
+# auditdistd.h. This seems like a makefile non-feature.
+CFLAGS+=-I${OPENBSMDIR} -I${OPENBSMDIR}/bin/auditdistd
+
+NO_WFORMAT=
+
+PROG= auditdistd
+SRCS= auditdistd.c
+SRCS+= parse.y pjdlog.c
+SRCS+= proto.c proto_common.c proto_socketpair.c proto_tcp.c proto_tls.c
+SRCS+= receiver.c
+SRCS+= sandbox.c sender.c subr.c
+SRCS+= token.l trail.c
+MAN= auditdistd.8 auditdistd.conf.5
+
+DPADD= ${LIBL} ${LIBPTHREAD} ${LIBUTIL}
+LDADD= -ll -lpthread -lutil
+DPADD+= ${LIBCRYPTO} ${LIBSSL}
+LDADD+= -lcrypto -lssl
+
+YFLAGS+=-v
+
+CLEANFILES=parse.c parse.h parse.output
+
+.include <bsd.prog.mk>
diff --git a/usr.sbin/bhyve/Makefile b/usr.sbin/bhyve/Makefile
new file mode 100644
index 0000000..ef39079
--- /dev/null
+++ b/usr.sbin/bhyve/Makefile
@@ -0,0 +1,25 @@
+#
+# $FreeBSD$
+#
+
+PROG= bhyve
+
+DEBUG_FLAGS= -g -O0
+
+SRCS= acpi.c atpic.c bhyverun.c consport.c dbgport.c elcr.c inout.c
+SRCS+= ioapic.c mem.c mevent.c mptbl.c
+SRCS+= pci_emul.c pci_hostbridge.c pci_passthru.c pci_virtio_block.c
+SRCS+= pci_virtio_net.c pci_uart.c pit_8254.c pmtmr.c post.c rtc.c uart.c
+SRCS+= xmsr.c spinup_ap.c
+
+.PATH: ${.CURDIR}/../../sys/amd64/vmm
+SRCS+= vmm_instruction_emul.c
+
+NO_MAN=
+
+DPADD= ${LIBVMMAPI} ${LIBMD} ${LIBPTHREAD}
+LDADD= -lvmmapi -lmd -lpthread
+
+WARNS?= 2
+
+.include <bsd.prog.mk>
diff --git a/usr.sbin/bhyve/acpi.c b/usr.sbin/bhyve/acpi.c
new file mode 100644
index 0000000..32effdc
--- /dev/null
+++ b/usr.sbin/bhyve/acpi.c
@@ -0,0 +1,844 @@
+/*-
+ * Copyright (c) 2012 NetApp, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ */
+
+/*
+ * bhyve ACPI table generator.
+ *
+ * Create the minimal set of ACPI tables required to boot FreeBSD (and
+ * hopefully other o/s's) by writing out ASL template files for each of
+ * the tables and the compiling them to AML with the Intel iasl compiler.
+ * The AML files are then read into guest memory.
+ *
+ * The tables are placed in the guest's ROM area just below 1MB physical,
+ * above the MPTable.
+ *
+ * Layout
+ * ------
+ * RSDP -> 0xf0400 (36 bytes fixed)
+ * RSDT -> 0xf0440 (36 bytes + 4*N table addrs, 2 used)
+ * XSDT -> 0xf0480 (36 bytes + 8*N table addrs, 2 used)
+ * MADT -> 0xf0500 (depends on #CPUs)
+ * FADT -> 0xf0600 (268 bytes)
+ * FACS -> 0xf0780 (64 bytes)
+ * DSDT -> 0xf0800 (variable - can go up to 0x100000)
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <sys/param.h>
+#include <sys/errno.h>
+#include <sys/stat.h>
+
+#include <paths.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "bhyverun.h"
+#include "acpi.h"
+
+/*
+ * Define the base address of the ACPI tables, and the offsets to
+ * the individual tables
+ */
+#define BHYVE_ACPI_BASE 0xf0400
+#define RSDT_OFFSET 0x040
+#define XSDT_OFFSET 0x080
+#define MADT_OFFSET 0x100
+#define FADT_OFFSET 0x200
+#define FACS_OFFSET 0x380
+#define DSDT_OFFSET 0x400
+
+#define BHYVE_ASL_TEMPLATE "bhyve.XXXXXXX"
+#define BHYVE_ASL_SUFFIX ".aml"
+#define BHYVE_ASL_COMPILER "/usr/sbin/iasl"
+
+#define BHYVE_PM_TIMER_ADDR 0x408
+
+static int basl_keep_temps;
+static int basl_verbose_iasl;
+static int basl_ncpu;
+static uint32_t basl_acpi_base = BHYVE_ACPI_BASE;
+
+/*
+ * Contains the full pathname of the template to be passed
+ * to mkstemp/mktemps(3)
+ */
+static char basl_template[MAXPATHLEN];
+static char basl_stemplate[MAXPATHLEN];
+
+struct basl_fio {
+ int fd;
+ FILE *fp;
+ char f_name[MAXPATHLEN];
+};
+
+#define EFPRINTF(...) \
+ err = fprintf(__VA_ARGS__); if (err < 0) goto err_exit;
+
+#define EFFLUSH(x) \
+ err = fflush(x); if (err != 0) goto err_exit;
+
+static int
+basl_fwrite_rsdp(FILE *fp)
+{
+ int err;
+
+ err = 0;
+
+ EFPRINTF(fp, "/*\n");
+ EFPRINTF(fp, " * bhyve RSDP template\n");
+ EFPRINTF(fp, " */\n");
+ EFPRINTF(fp, "[0008]\t\tSignature : \"RSD PTR \"\n");
+ EFPRINTF(fp, "[0001]\t\tChecksum : 43\n");
+ EFPRINTF(fp, "[0006]\t\tOem ID : \"BHYVE \"\n");
+ EFPRINTF(fp, "[0001]\t\tRevision : 02\n");
+ EFPRINTF(fp, "[0004]\t\tRSDT Address : %08X\n",
+ basl_acpi_base + RSDT_OFFSET);
+ EFPRINTF(fp, "[0004]\t\tLength : 00000024\n");
+ EFPRINTF(fp, "[0008]\t\tXSDT Address : 00000000%08X\n",
+ basl_acpi_base + XSDT_OFFSET);
+ EFPRINTF(fp, "[0001]\t\tExtended Checksum : 00\n");
+ EFPRINTF(fp, "[0003]\t\tReserved : 000000\n");
+
+ EFFLUSH(fp);
+
+ return (0);
+
+err_exit:
+ return (errno);
+}
+
+static int
+basl_fwrite_rsdt(FILE *fp)
+{
+ int err;
+
+ err = 0;
+
+ EFPRINTF(fp, "/*\n");
+ EFPRINTF(fp, " * bhyve RSDT template\n");
+ EFPRINTF(fp, " */\n");
+ EFPRINTF(fp, "[0004]\t\tSignature : \"RSDT\"\n");
+ EFPRINTF(fp, "[0004]\t\tTable Length : 00000000\n");
+ EFPRINTF(fp, "[0001]\t\tRevision : 01\n");
+ EFPRINTF(fp, "[0001]\t\tChecksum : 00\n");
+ EFPRINTF(fp, "[0006]\t\tOem ID : \"BHYVE \"\n");
+ EFPRINTF(fp, "[0008]\t\tOem Table ID : \"BVRSDT \"\n");
+ EFPRINTF(fp, "[0004]\t\tOem Revision : 00000001\n");
+ /* iasl will fill in the compiler ID/revision fields */
+ EFPRINTF(fp, "[0004]\t\tAsl Compiler ID : \"xxxx\"\n");
+ EFPRINTF(fp, "[0004]\t\tAsl Compiler Revision : 00000000\n");
+ EFPRINTF(fp, "\n");
+
+ /* Add in pointers to the MADT and FADT */
+ EFPRINTF(fp, "[0004]\t\tACPI Table Address 0 : %08X\n",
+ basl_acpi_base + MADT_OFFSET);
+ EFPRINTF(fp, "[0004]\t\tACPI Table Address 1 : %08X\n",
+ basl_acpi_base + FADT_OFFSET);
+
+ EFFLUSH(fp);
+
+ return (0);
+
+err_exit:
+ return (errno);
+}
+
+static int
+basl_fwrite_xsdt(FILE *fp)
+{
+ int err;
+
+ err = 0;
+
+ EFPRINTF(fp, "/*\n");
+ EFPRINTF(fp, " * bhyve XSDT template\n");
+ EFPRINTF(fp, " */\n");
+ EFPRINTF(fp, "[0004]\t\tSignature : \"XSDT\"\n");
+ EFPRINTF(fp, "[0004]\t\tTable Length : 00000000\n");
+ EFPRINTF(fp, "[0001]\t\tRevision : 01\n");
+ EFPRINTF(fp, "[0001]\t\tChecksum : 00\n");
+ EFPRINTF(fp, "[0006]\t\tOem ID : \"BHYVE \"\n");
+ EFPRINTF(fp, "[0008]\t\tOem Table ID : \"BVXSDT \"\n");
+ EFPRINTF(fp, "[0004]\t\tOem Revision : 00000001\n");
+ /* iasl will fill in the compiler ID/revision fields */
+ EFPRINTF(fp, "[0004]\t\tAsl Compiler ID : \"xxxx\"\n");
+ EFPRINTF(fp, "[0004]\t\tAsl Compiler Revision : 00000000\n");
+ EFPRINTF(fp, "\n");
+
+ /* Add in pointers to the MADT and FADT */
+ EFPRINTF(fp, "[0004]\t\tACPI Table Address 0 : 00000000%08X\n",
+ basl_acpi_base + MADT_OFFSET);
+ EFPRINTF(fp, "[0004]\t\tACPI Table Address 1 : 00000000%08X\n",
+ basl_acpi_base + FADT_OFFSET);
+
+ EFFLUSH(fp);
+
+ return (0);
+
+err_exit:
+ return (errno);
+}
+
+static int
+basl_fwrite_madt(FILE *fp)
+{
+ int err;
+ int i;
+
+ err = 0;
+
+ EFPRINTF(fp, "/*\n");
+ EFPRINTF(fp, " * bhyve MADT template\n");
+ EFPRINTF(fp, " */\n");
+ EFPRINTF(fp, "[0004]\t\tSignature : \"APIC\"\n");
+ EFPRINTF(fp, "[0004]\t\tTable Length : 00000000\n");
+ EFPRINTF(fp, "[0001]\t\tRevision : 01\n");
+ EFPRINTF(fp, "[0001]\t\tChecksum : 00\n");
+ EFPRINTF(fp, "[0006]\t\tOem ID : \"BHYVE \"\n");
+ EFPRINTF(fp, "[0008]\t\tOem Table ID : \"BVMADT \"\n");
+ EFPRINTF(fp, "[0004]\t\tOem Revision : 00000001\n");
+
+ /* iasl will fill in the compiler ID/revision fields */
+ EFPRINTF(fp, "[0004]\t\tAsl Compiler ID : \"xxxx\"\n");
+ EFPRINTF(fp, "[0004]\t\tAsl Compiler Revision : 00000000\n");
+ EFPRINTF(fp, "\n");
+
+ EFPRINTF(fp, "[0004]\t\tLocal Apic Address : FEE00000\n");
+ EFPRINTF(fp, "[0004]\t\tFlags (decoded below) : 00000001\n");
+ EFPRINTF(fp, "\t\t\tPC-AT Compatibility : 1\n");
+ EFPRINTF(fp, "\n");
+
+ /* Add a Processor Local APIC entry for each CPU */
+ for (i = 0; i < basl_ncpu; i++) {
+ EFPRINTF(fp, "[0001]\t\tSubtable Type : 00\n");
+ EFPRINTF(fp, "[0001]\t\tLength : 08\n");
+ EFPRINTF(fp, "[0001]\t\tProcessor ID : %02d\n", i);
+ EFPRINTF(fp, "[0001]\t\tLocal Apic ID : %02d\n", i);
+ EFPRINTF(fp, "[0004]\t\tFlags (decoded below) : 00000001\n");
+ EFPRINTF(fp, "\t\t\tProcessor Enabled : 1\n");
+ EFPRINTF(fp, "\n");
+ }
+
+ /* Always a single IOAPIC entry, with ID ncpu+1 */
+ EFPRINTF(fp, "[0001]\t\tSubtable Type : 01\n");
+ EFPRINTF(fp, "[0001]\t\tLength : 0C\n");
+ EFPRINTF(fp, "[0001]\t\tI/O Apic ID : %02d\n", basl_ncpu);
+ EFPRINTF(fp, "[0001]\t\tReserved : 00\n");
+ EFPRINTF(fp, "[0004]\t\tAddress : fec00000\n");
+ EFPRINTF(fp, "[0004]\t\tInterrupt : 00000000\n");
+ EFPRINTF(fp, "\n");
+
+ /* Override the 8259 chained vector. XXX maybe not needed */
+ EFPRINTF(fp, "[0001]\t\tSubtable Type : 02\n");
+ EFPRINTF(fp, "[0001]\t\tLength : 0A\n");
+ EFPRINTF(fp, "[0001]\t\tBus : 00\n");
+ EFPRINTF(fp, "[0001]\t\tSource : 09\n");
+ EFPRINTF(fp, "[0004]\t\tInterrupt : 00000009\n");
+ EFPRINTF(fp, "[0002]\t\tFlags (decoded below) : 0000\n");
+ EFPRINTF(fp, "\t\t\tPolarity : 0\n");
+ EFPRINTF(fp, "\t\t\tTrigger Mode : 0\n");
+ EFPRINTF(fp, "\n");
+
+ EFFLUSH(fp);
+
+ return (0);
+
+err_exit:
+ return (errno);
+}
+
+static int
+basl_fwrite_fadt(FILE *fp)
+{
+ int err;
+
+ err = 0;
+
+ EFPRINTF(fp, "/*\n");
+ EFPRINTF(fp, " * bhyve FADT template\n");
+ EFPRINTF(fp, " */\n");
+ EFPRINTF(fp, "[0004]\t\tSignature : \"FACP\"\n");
+ EFPRINTF(fp, "[0004]\t\tTable Length : 0000010C\n");
+ EFPRINTF(fp, "[0001]\t\tRevision : 05\n");
+ EFPRINTF(fp, "[0001]\t\tChecksum : 00\n");
+ EFPRINTF(fp, "[0006]\t\tOem ID : \"BHYVE \"\n");
+ EFPRINTF(fp, "[0008]\t\tOem Table ID : \"BVFACP \"\n");
+ EFPRINTF(fp, "[0004]\t\tOem Revision : 00000001\n");
+ /* iasl will fill in the compiler ID/revision fields */
+ EFPRINTF(fp, "[0004]\t\tAsl Compiler ID : \"xxxx\"\n");
+ EFPRINTF(fp, "[0004]\t\tAsl Compiler Revision : 00000000\n");
+ EFPRINTF(fp, "\n");
+
+ EFPRINTF(fp, "[0004]\t\tFACS Address : %08X\n",
+ basl_acpi_base + FACS_OFFSET);
+ EFPRINTF(fp, "[0004]\t\tDSDT Address : %08X\n",
+ basl_acpi_base + DSDT_OFFSET);
+ EFPRINTF(fp, "[0001]\t\tModel : 00\n");
+ EFPRINTF(fp, "[0001]\t\tPM Profile : 00 [Unspecified]\n");
+ EFPRINTF(fp, "[0002]\t\tSCI Interrupt : 0009\n");
+ EFPRINTF(fp, "[0004]\t\tSMI Command Port : 00000000\n");
+ EFPRINTF(fp, "[0001]\t\tACPI Enable Value : 00\n");
+ EFPRINTF(fp, "[0001]\t\tACPI Disable Value : 00\n");
+ EFPRINTF(fp, "[0001]\t\tS4BIOS Command : 00\n");
+ EFPRINTF(fp, "[0001]\t\tP-State Control : 00\n");
+ EFPRINTF(fp, "[0004]\t\tPM1A Event Block Address : 00000000\n");
+ EFPRINTF(fp, "[0004]\t\tPM1B Event Block Address : 00000000\n");
+ EFPRINTF(fp, "[0004]\t\tPM1A Control Block Address : 00000000\n");
+ EFPRINTF(fp, "[0004]\t\tPM1B Control Block Address : 00000000\n");
+ EFPRINTF(fp, "[0004]\t\tPM2 Control Block Address : 00000000\n");
+ EFPRINTF(fp, "[0004]\t\tPM Timer Block Address : %08X\n",
+ BHYVE_PM_TIMER_ADDR);
+ EFPRINTF(fp, "[0004]\t\tGPE0 Block Address : 00000000\n");
+ EFPRINTF(fp, "[0004]\t\tGPE1 Block Address : 00000000\n");
+ EFPRINTF(fp, "[0001]\t\tPM1 Event Block Length : 04\n");
+ EFPRINTF(fp, "[0001]\t\tPM1 Control Block Length : 02\n");
+ EFPRINTF(fp, "[0001]\t\tPM2 Control Block Length : 00\n");
+ EFPRINTF(fp, "[0001]\t\tPM Timer Block Length : 04\n");
+ EFPRINTF(fp, "[0001]\t\tGPE0 Block Length : 00\n");
+ EFPRINTF(fp, "[0001]\t\tGPE1 Block Length : 00\n");
+ EFPRINTF(fp, "[0001]\t\tGPE1 Base Offset : 00\n");
+ EFPRINTF(fp, "[0001]\t\t_CST Support : 00\n");
+ EFPRINTF(fp, "[0002]\t\tC2 Latency : 0000\n");
+ EFPRINTF(fp, "[0002]\t\tC3 Latency : 0000\n");
+ EFPRINTF(fp, "[0002]\t\tCPU Cache Size : 0000\n");
+ EFPRINTF(fp, "[0002]\t\tCache Flush Stride : 0000\n");
+ EFPRINTF(fp, "[0001]\t\tDuty Cycle Offset : 00\n");
+ EFPRINTF(fp, "[0001]\t\tDuty Cycle Width : 00\n");
+ EFPRINTF(fp, "[0001]\t\tRTC Day Alarm Index : 00\n");
+ EFPRINTF(fp, "[0001]\t\tRTC Month Alarm Index : 00\n");
+ EFPRINTF(fp, "[0001]\t\tRTC Century Index : 00\n");
+ EFPRINTF(fp, "[0002]\t\tBoot Flags (decoded below) : 0000\n");
+ EFPRINTF(fp, "\t\t\tLegacy Devices Supported (V2) : 0\n");
+ EFPRINTF(fp, "\t\t\t8042 Present on ports 60/64 (V2) : 0\n");
+ EFPRINTF(fp, "\t\t\tVGA Not Present (V4) : 1\n");
+ EFPRINTF(fp, "\t\t\tMSI Not Supported (V4) : 0\n");
+ EFPRINTF(fp, "\t\t\tPCIe ASPM Not Supported (V4) : 1\n");
+ EFPRINTF(fp, "\t\t\tCMOS RTC Not Present (V5) : 0\n");
+ EFPRINTF(fp, "[0001]\t\tReserved : 00\n");
+ EFPRINTF(fp, "[0004]\t\tFlags (decoded below) : 00000000\n");
+ EFPRINTF(fp, "\t\t\tWBINVD instruction is operational (V1) : 1\n");
+ EFPRINTF(fp, "\t\t\tWBINVD flushes all caches (V1) : 0\n");
+ EFPRINTF(fp, "\t\t\tAll CPUs support C1 (V1) : 0\n");
+ EFPRINTF(fp, "\t\t\tC2 works on MP system (V1) : 0\n");
+ EFPRINTF(fp, "\t\t\tControl Method Power Button (V1) : 1\n");
+ EFPRINTF(fp, "\t\t\tControl Method Sleep Button (V1) : 1\n");
+ EFPRINTF(fp, "\t\t\tRTC wake not in fixed reg space (V1) : 0\n");
+ EFPRINTF(fp, "\t\t\tRTC can wake system from S4 (V1) : 0\n");
+ EFPRINTF(fp, "\t\t\t32-bit PM Timer (V1) : 1\n");
+ EFPRINTF(fp, "\t\t\tDocking Supported (V1) : 0\n");
+ EFPRINTF(fp, "\t\t\tReset Register Supported (V2) : 0\n");
+ EFPRINTF(fp, "\t\t\tSealed Case (V3) : 0\n");
+ EFPRINTF(fp, "\t\t\tHeadless - No Video (V3) : 1\n");
+ EFPRINTF(fp, "\t\t\tUse native instr after SLP_TYPx (V3) : 0\n");
+ EFPRINTF(fp, "\t\t\tPCIEXP_WAK Bits Supported (V4) : 0\n");
+ EFPRINTF(fp, "\t\t\tUse Platform Timer (V4) : 0\n");
+ EFPRINTF(fp, "\t\t\tRTC_STS valid on S4 wake (V4) : 0\n");
+ EFPRINTF(fp, "\t\t\tRemote Power-on capable (V4) : 0\n");
+ EFPRINTF(fp, "\t\t\tUse APIC Cluster Model (V4) : 0\n");
+ EFPRINTF(fp, "\t\t\tUse APIC Physical Destination Mode (V4) : 1\n");
+ EFPRINTF(fp, "\t\t\tHardware Reduced (V5) : 0\n");
+ EFPRINTF(fp, "\t\t\tLow Power S0 Idle (V5) : 0\n");
+ EFPRINTF(fp, "\n");
+
+ EFPRINTF(fp,
+ "[0012]\t\tReset Register : [Generic Address Structure]\n");
+ EFPRINTF(fp, "[0001]\t\tSpace ID : 01 [SystemIO]\n");
+ EFPRINTF(fp, "[0001]\t\tBit Width : 08\n");
+ EFPRINTF(fp, "[0001]\t\tBit Offset : 00\n");
+ EFPRINTF(fp, "[0001]\t\tEncoded Access Width : 01 [Byte Access:8]\n");
+ EFPRINTF(fp, "[0008]\t\tAddress : 0000000000000001\n");
+ EFPRINTF(fp, "\n");
+
+ EFPRINTF(fp, "[0001]\t\tValue to cause reset : 00\n");
+ EFPRINTF(fp, "[0003]\t\tReserved : 000000\n");
+ EFPRINTF(fp, "[0008]\t\tFACS Address : 00000000%08X\n",
+ basl_acpi_base + FACS_OFFSET);
+ EFPRINTF(fp, "[0008]\t\tDSDT Address : 00000000%08X\n",
+ basl_acpi_base + DSDT_OFFSET);
+ EFPRINTF(fp,
+ "[0012]\t\tPM1A Event Block : [Generic Address Structure]\n");
+ EFPRINTF(fp, "[0001]\t\tSpace ID : 01 [SystemIO]\n");
+ EFPRINTF(fp, "[0001]\t\tBit Width : 20\n");
+ EFPRINTF(fp, "[0001]\t\tBit Offset : 00\n");
+ EFPRINTF(fp, "[0001]\t\tEncoded Access Width : 02 [Word Access:16]\n");
+ EFPRINTF(fp, "[0008]\t\tAddress : 0000000000000001\n");
+ EFPRINTF(fp, "\n");
+
+ EFPRINTF(fp,
+ "[0012]\t\tPM1B Event Block : [Generic Address Structure]\n");
+ EFPRINTF(fp, "[0001]\t\tSpace ID : 01 [SystemIO]\n");
+ EFPRINTF(fp, "[0001]\t\tBit Width : 00\n");
+ EFPRINTF(fp, "[0001]\t\tBit Offset : 00\n");
+ EFPRINTF(fp,
+ "[0001]\t\tEncoded Access Width : 00 [Undefined/Legacy]\n");
+ EFPRINTF(fp, "[0008]\t\tAddress : 0000000000000000\n");
+ EFPRINTF(fp, "\n");
+
+ EFPRINTF(fp,
+ "[0012]\t\tPM1A Control Block : [Generic Address Structure]\n");
+ EFPRINTF(fp, "[0001]\t\tSpace ID : 01 [SystemIO]\n");
+ EFPRINTF(fp, "[0001]\t\tBit Width : 10\n");
+ EFPRINTF(fp, "[0001]\t\tBit Offset : 00\n");
+ EFPRINTF(fp, "[0001]\t\tEncoded Access Width : 02 [Word Access:16]\n");
+ EFPRINTF(fp, "[0008]\t\tAddress : 0000000000000001\n");
+ EFPRINTF(fp, "\n");
+
+ EFPRINTF(fp,
+ "[0012]\t\tPM1B Control Block : [Generic Address Structure]\n");
+ EFPRINTF(fp, "[0001]\t\tSpace ID : 01 [SystemIO]\n");
+ EFPRINTF(fp, "[0001]\t\tBit Width : 00\n");
+ EFPRINTF(fp, "[0001]\t\tBit Offset : 00\n");
+ EFPRINTF(fp,
+ "[0001]\t\tEncoded Access Width : 00 [Undefined/Legacy]\n");
+ EFPRINTF(fp, "[0008]\t\tAddress : 0000000000000000\n");
+ EFPRINTF(fp, "\n");
+
+ EFPRINTF(fp,
+ "[0012]\t\tPM2 Control Block : [Generic Address Structure]\n");
+ EFPRINTF(fp, "[0001]\t\tSpace ID : 01 [SystemIO]\n");
+ EFPRINTF(fp, "[0001]\t\tBit Width : 08\n");
+ EFPRINTF(fp, "[0001]\t\tBit Offset : 00\n");
+ EFPRINTF(fp,
+ "[0001]\t\tEncoded Access Width : 00 [Undefined/Legacy]\n");
+ EFPRINTF(fp, "[0008]\t\tAddress : 0000000000000000\n");
+ EFPRINTF(fp, "\n");
+
+ /* Valid for bhyve */
+ EFPRINTF(fp,
+ "[0012]\t\tPM Timer Block : [Generic Address Structure]\n");
+ EFPRINTF(fp, "[0001]\t\tSpace ID : 01 [SystemIO]\n");
+ EFPRINTF(fp, "[0001]\t\tBit Width : 32\n");
+ EFPRINTF(fp, "[0001]\t\tBit Offset : 00\n");
+ EFPRINTF(fp,
+ "[0001]\t\tEncoded Access Width : 03 [DWord Access:32]\n");
+ EFPRINTF(fp, "[0008]\t\tAddress : 00000000%08X\n",
+ BHYVE_PM_TIMER_ADDR);
+ EFPRINTF(fp, "\n");
+
+ EFPRINTF(fp, "[0012]\t\tGPE0 Block : [Generic Address Structure]\n");
+ EFPRINTF(fp, "[0001]\t\tSpace ID : 01 [SystemIO]\n");
+ EFPRINTF(fp, "[0001]\t\tBit Width : 80\n");
+ EFPRINTF(fp, "[0001]\t\tBit Offset : 00\n");
+ EFPRINTF(fp, "[0001]\t\tEncoded Access Width : 01 [Byte Access:8]\n");
+ EFPRINTF(fp, "[0008]\t\tAddress : 0000000000000000\n");
+ EFPRINTF(fp, "\n");
+
+ EFPRINTF(fp, "[0012]\t\tGPE1 Block : [Generic Address Structure]\n");
+ EFPRINTF(fp, "[0001]\t\tSpace ID : 01 [SystemIO]\n");
+ EFPRINTF(fp, "[0001]\t\tBit Width : 00\n");
+ EFPRINTF(fp, "[0001]\t\tBit Offset : 00\n");
+ EFPRINTF(fp,
+ "[0001]\t\tEncoded Access Width : 00 [Undefined/Legacy]\n");
+ EFPRINTF(fp, "[0008]\t\tAddress : 0000000000000000\n");
+ EFPRINTF(fp, "\n");
+
+ EFPRINTF(fp,
+ "[0012]\t\tSleep Control Register : [Generic Address Structure]\n");
+ EFPRINTF(fp, "[0001]\t\tSpace ID : 01 [SystemIO]\n");
+ EFPRINTF(fp, "[0001]\t\tBit Width : 08\n");
+ EFPRINTF(fp, "[0001]\t\tBit Offset : 00\n");
+ EFPRINTF(fp, "[0001]\t\tEncoded Access Width : 01 [Byte Access:8]\n");
+ EFPRINTF(fp, "[0008]\t\tAddress : 0000000000000000\n");
+ EFPRINTF(fp, "\n");
+
+ EFPRINTF(fp,
+ "[0012]\t\tSleep Status Register : [Generic Address Structure]\n");
+ EFPRINTF(fp, "[0001]\t\tSpace ID : 01 [SystemIO]\n");
+ EFPRINTF(fp, "[0001]\t\tBit Width : 08\n");
+ EFPRINTF(fp, "[0001]\t\tBit Offset : 00\n");
+ EFPRINTF(fp, "[0001]\t\tEncoded Access Width : 01 [Byte Access:8]\n");
+ EFPRINTF(fp, "[0008]\t\tAddress : 0000000000000000\n");
+
+ EFFLUSH(fp);
+
+ return (0);
+
+err_exit:
+ return (errno);
+}
+
+static int
+basl_fwrite_facs(FILE *fp)
+{
+ int err;
+
+ err = 0;
+
+ EFPRINTF(fp, "/*\n");
+ EFPRINTF(fp, " * bhyve FACS template\n");
+ EFPRINTF(fp, " */\n");
+ EFPRINTF(fp, "[0004]\t\tSignature : \"FACS\"\n");
+ EFPRINTF(fp, "[0004]\t\tLength : 00000040\n");
+ EFPRINTF(fp, "[0004]\t\tHardware Signature : 00000000\n");
+ EFPRINTF(fp, "[0004]\t\t32 Firmware Waking Vector : 00000000\n");
+ EFPRINTF(fp, "[0004]\t\tGlobal Lock : 00000000\n");
+ EFPRINTF(fp, "[0004]\t\tFlags (decoded below) : 00000000\n");
+ EFPRINTF(fp, "\t\t\tS4BIOS Support Present : 0\n");
+ EFPRINTF(fp, "\t\t\t64-bit Wake Supported (V2) : 0\n");
+ EFPRINTF(fp,
+ "[0008]\t\t64 Firmware Waking Vector : 0000000000000000\n");
+ EFPRINTF(fp, "[0001]\t\tVersion : 02\n");
+ EFPRINTF(fp, "[0003]\t\tReserved : 000000\n");
+ EFPRINTF(fp, "[0004]\t\tOspmFlags (decoded below) : 00000000\n");
+ EFPRINTF(fp, "\t\t\t64-bit Wake Env Required (V2) : 0\n");
+
+ EFFLUSH(fp);
+
+ return (0);
+
+err_exit:
+ return (errno);
+}
+
+static int
+basl_fwrite_dsdt(FILE *fp)
+{
+ int err;
+
+ err = 0;
+
+ EFPRINTF(fp, "/*\n");
+ EFPRINTF(fp, " * bhyve DSDT template\n");
+ EFPRINTF(fp, " */\n");
+ EFPRINTF(fp, "DefinitionBlock (\"bhyve_dsdt.aml\", \"DSDT\", 2,"
+ "\"BHYVE \", \"BVDSDT \", 0x00000001)\n");
+ EFPRINTF(fp, "{\n");
+ EFPRINTF(fp, " Scope (_SB)\n");
+ EFPRINTF(fp, " {\n");
+ EFPRINTF(fp, " Device (PCI0)\n");
+ EFPRINTF(fp, " {\n");
+ EFPRINTF(fp, " Name (_HID, EisaId (\"PNP0A03\"))\n");
+ EFPRINTF(fp, " Name (_ADR, Zero)\n");
+ EFPRINTF(fp, " Name (_UID, One)\n");
+ EFPRINTF(fp, " Name (_CRS, ResourceTemplate ()\n");
+ EFPRINTF(fp, " {\n");
+ EFPRINTF(fp, " WordBusNumber (ResourceProducer, MinFixed,"
+ "MaxFixed, PosDecode,\n");
+ EFPRINTF(fp, " 0x0000, // Granularity\n");
+ EFPRINTF(fp, " 0x0000, // Range Minimum\n");
+ EFPRINTF(fp, " 0x00FF, // Range Maximum\n");
+ EFPRINTF(fp, " 0x0000, // Transl Offset\n");
+ EFPRINTF(fp, " 0x0100, // Length\n");
+ EFPRINTF(fp, " ,, )\n");
+ EFPRINTF(fp, " IO (Decode16,\n");
+ EFPRINTF(fp, " 0x0CF8, // Range Minimum\n");
+ EFPRINTF(fp, " 0x0CF8, // Range Maximum\n");
+ EFPRINTF(fp, " 0x01, // Alignment\n");
+ EFPRINTF(fp, " 0x08, // Length\n");
+ EFPRINTF(fp, " )\n");
+ EFPRINTF(fp, " WordIO (ResourceProducer, MinFixed, MaxFixed,"
+ "PosDecode, EntireRange,\n");
+ EFPRINTF(fp, " 0x0000, // Granularity\n");
+ EFPRINTF(fp, " 0x0000, // Range Minimum\n");
+ EFPRINTF(fp, " 0x0CF7, // Range Maximum\n");
+ EFPRINTF(fp, " 0x0000, // Transl Offset\n");
+ EFPRINTF(fp, " 0x0CF8, // Length\n");
+ EFPRINTF(fp, " ,, , TypeStatic)\n");
+ EFPRINTF(fp, " WordIO (ResourceProducer, MinFixed, MaxFixed,"
+ "PosDecode, EntireRange,\n");
+ EFPRINTF(fp, " 0x0000, // Granularity\n");
+ EFPRINTF(fp, " 0x0D00, // Range Minimum\n");
+ EFPRINTF(fp, " 0xFFFF, // Range Maximum\n");
+ EFPRINTF(fp, " 0x0000, // Transl Offset\n");
+ EFPRINTF(fp, " 0xF300, // Length\n");
+ EFPRINTF(fp, " ,, , TypeStatic)\n");
+ EFPRINTF(fp, " })\n");
+ EFPRINTF(fp, " }\n");
+ EFPRINTF(fp, " }\n");
+ EFPRINTF(fp, "\n");
+ EFPRINTF(fp, " Scope (_SB.PCI0)\n");
+ EFPRINTF(fp, " {\n");
+ EFPRINTF(fp, " Device (ISA)\n");
+ EFPRINTF(fp, " {\n");
+ EFPRINTF(fp, " Name (_ADR, 0x00010000)\n");
+ EFPRINTF(fp, " OperationRegion (P40C, PCI_Config, 0x60, 0x04)\n");
+ EFPRINTF(fp, " }\n");
+ EFPRINTF(fp, " }\n");
+ EFPRINTF(fp, "\n");
+ EFPRINTF(fp, " Scope (_SB.PCI0.ISA)\n");
+ EFPRINTF(fp, " {\n");
+ EFPRINTF(fp, " Device (RTC)\n");
+ EFPRINTF(fp, " {\n");
+ EFPRINTF(fp, " Name (_HID, EisaId (\"PNP0B00\"))\n");
+ EFPRINTF(fp, " Name (_CRS, ResourceTemplate ()\n");
+ EFPRINTF(fp, " {\n");
+ EFPRINTF(fp, " IO (Decode16,\n");
+ EFPRINTF(fp, " 0x0070, // Range Minimum\n");
+ EFPRINTF(fp, " 0x0070, // Range Maximum\n");
+ EFPRINTF(fp, " 0x10, // Alignment\n");
+ EFPRINTF(fp, " 0x02, // Length\n");
+ EFPRINTF(fp, " )\n");
+ EFPRINTF(fp, " IRQNoFlags ()\n");
+ EFPRINTF(fp, " {8}\n");
+ EFPRINTF(fp, " IO (Decode16,\n");
+ EFPRINTF(fp, " 0x0072, // Range Minimum\n");
+ EFPRINTF(fp, " 0x0072, // Range Maximum\n");
+ EFPRINTF(fp, " 0x02, // Alignment\n");
+ EFPRINTF(fp, " 0x06, // Length\n");
+ EFPRINTF(fp, " )\n");
+ EFPRINTF(fp, " })\n");
+ EFPRINTF(fp, " }\n");
+ EFPRINTF(fp, " }\n");
+ EFPRINTF(fp, "}\n");
+
+ EFFLUSH(fp);
+
+ return (0);
+
+err_exit:
+ return (errno);
+}
+
+static int
+basl_open(struct basl_fio *bf, int suffix)
+{
+ int err;
+
+ err = 0;
+
+ if (suffix) {
+ strncpy(bf->f_name, basl_stemplate, MAXPATHLEN);
+ bf->fd = mkstemps(bf->f_name, strlen(BHYVE_ASL_SUFFIX));
+ } else {
+ strncpy(bf->f_name, basl_template, MAXPATHLEN);
+ bf->fd = mkstemp(bf->f_name);
+ }
+
+ if (bf->fd > 0) {
+ bf->fp = fdopen(bf->fd, "w+");
+ if (bf->fp == NULL) {
+ unlink(bf->f_name);
+ close(bf->fd);
+ }
+ } else {
+ err = 1;
+ }
+
+ return (err);
+}
+
+static void
+basl_close(struct basl_fio *bf)
+{
+
+ if (!basl_keep_temps)
+ unlink(bf->f_name);
+ fclose(bf->fp);
+}
+
+static int
+basl_start(struct basl_fio *in, struct basl_fio *out)
+{
+ int err;
+
+ err = basl_open(in, 0);
+ if (!err) {
+ err = basl_open(out, 1);
+ if (err) {
+ basl_close(in);
+ }
+ }
+
+ return (err);
+}
+
+static void
+basl_end(struct basl_fio *in, struct basl_fio *out)
+{
+
+ basl_close(in);
+ basl_close(out);
+}
+
+static int
+basl_load(int fd, uint64_t off)
+{
+ struct stat sb;
+ int err;
+
+ err = 0;
+
+ if (fstat(fd, &sb) < 0 ||
+ read(fd, paddr_guest2host(basl_acpi_base + off), sb.st_size) < 0)
+ err = errno;
+
+ return (err);
+}
+
+static int
+basl_compile(int (*fwrite_section)(FILE *fp), uint64_t offset)
+{
+ struct basl_fio io[2];
+ static char iaslbuf[3*MAXPATHLEN + 10];
+ char *fmt;
+ int err;
+
+ err = basl_start(&io[0], &io[1]);
+ if (!err) {
+ err = (*fwrite_section)(io[0].fp);
+
+ if (!err) {
+ /*
+ * iasl sends the results of the compilation to
+ * stdout. Shut this down by using the shell to
+ * redirect stdout to /dev/null, unless the user
+ * has requested verbose output for debugging
+ * purposes
+ */
+ fmt = basl_verbose_iasl ?
+ "%s -p %s %s" :
+ "/bin/sh -c \"%s -p %s %s\" 1> /dev/null";
+
+ snprintf(iaslbuf, sizeof(iaslbuf),
+ fmt,
+ BHYVE_ASL_COMPILER,
+ io[1].f_name, io[0].f_name);
+ err = system(iaslbuf);
+
+ if (!err) {
+ /*
+ * Copy the aml output file into guest
+ * memory at the specified location
+ */
+ err = basl_load(io[1].fd, offset);
+ }
+ }
+ basl_end(&io[0], &io[1]);
+ }
+
+ return (err);
+}
+
+static int
+basl_make_templates(void)
+{
+ const char *tmpdir;
+ int err;
+ int len;
+
+ err = 0;
+
+ /*
+ *
+ */
+ if ((tmpdir = getenv("BHYVE_TMPDIR")) == NULL || *tmpdir == '\0' ||
+ (tmpdir = getenv("TMPDIR")) == NULL || *tmpdir == '\0') {
+ tmpdir = _PATH_TMP;
+ }
+
+ len = strlen(tmpdir);
+
+ if ((len + sizeof(BHYVE_ASL_TEMPLATE) + 1) < MAXPATHLEN) {
+ strcpy(basl_template, tmpdir);
+ while (len > 0 && basl_template[len - 1] == '/')
+ len--;
+ basl_template[len] = '/';
+ strcpy(&basl_template[len + 1], BHYVE_ASL_TEMPLATE);
+ } else
+ err = E2BIG;
+
+ if (!err) {
+ /*
+ * len has been intialized (and maybe adjusted) above
+ */
+ if ((len + sizeof(BHYVE_ASL_TEMPLATE) + 1 +
+ sizeof(BHYVE_ASL_SUFFIX)) < MAXPATHLEN) {
+ strcpy(basl_stemplate, tmpdir);
+ basl_stemplate[len] = '/';
+ strcpy(&basl_stemplate[len + 1], BHYVE_ASL_TEMPLATE);
+ len = strlen(basl_stemplate);
+ strcpy(&basl_stemplate[len], BHYVE_ASL_SUFFIX);
+ } else
+ err = E2BIG;
+ }
+
+ return (err);
+}
+
+static struct {
+ int (*wsect)(FILE *fp);
+ uint64_t offset;
+} basl_ftables[] =
+{
+ { basl_fwrite_rsdp, 0},
+ { basl_fwrite_rsdt, RSDT_OFFSET },
+ { basl_fwrite_xsdt, XSDT_OFFSET },
+ { basl_fwrite_madt, MADT_OFFSET },
+ { basl_fwrite_fadt, FADT_OFFSET },
+ { basl_fwrite_facs, FACS_OFFSET },
+ { basl_fwrite_dsdt, DSDT_OFFSET },
+ { NULL }
+};
+
+int
+acpi_build(struct vmctx *ctx, int ncpu, int ioapic)
+{
+ int err;
+ int i;
+
+ err = 0;
+ basl_ncpu = ncpu;
+
+ if (!ioapic) {
+ fprintf(stderr, "ACPI tables require an ioapic\n");
+ return (EINVAL);
+ }
+
+ /*
+ * For debug, allow the user to have iasl compiler output sent
+ * to stdout rather than /dev/null
+ */
+ if (getenv("BHYVE_ACPI_VERBOSE_IASL"))
+ basl_verbose_iasl = 1;
+
+ /*
+ * Allow the user to keep the generated ASL files for debugging
+ * instead of deleting them following use
+ */
+ if (getenv("BHYVE_ACPI_KEEPTMPS"))
+ basl_keep_temps = 1;
+
+ i = 0;
+ err = basl_make_templates();
+
+ /*
+ * Run through all the ASL files, compiling them and
+ * copying them into guest memory
+ */
+ while (!err && basl_ftables[i].wsect != NULL) {
+ err = basl_compile(basl_ftables[i].wsect,
+ basl_ftables[i].offset);
+ i++;
+ }
+
+ return (err);
+}
diff --git a/usr.sbin/bhyve/acpi.h b/usr.sbin/bhyve/acpi.h
new file mode 100644
index 0000000..fec6c9d
--- /dev/null
+++ b/usr.sbin/bhyve/acpi.h
@@ -0,0 +1,34 @@
+/*-
+ * Copyright (c) 2012 NetApp, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ */
+
+#ifndef _ACPI_H_
+#define _ACPI_H_
+
+int acpi_build(struct vmctx *ctx, int ncpu, int ioapic);
+
+#endif /* _ACPI_H_ */
diff --git a/usr.sbin/bhyve/atpic.c b/usr.sbin/bhyve/atpic.c
new file mode 100644
index 0000000..a9fb084
--- /dev/null
+++ b/usr.sbin/bhyve/atpic.c
@@ -0,0 +1,68 @@
+/*-
+ * Copyright (c) 2011 NetApp, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <sys/param.h>
+
+#include <stdio.h>
+#include <string.h>
+#include <assert.h>
+
+#include "inout.h"
+
+/*
+ * FreeBSD only writes to the 8259 interrupt controllers to put them in a
+ * shutdown state.
+ *
+ * So, we just ignore the writes.
+ */
+
+#define IO_ICU1 0x20
+#define IO_ICU2 0xA0
+#define ICU_IMR_OFFSET 1
+
+static int
+atpic_handler(struct vmctx *ctx, int vcpu, int in, int port, int bytes,
+ uint32_t *eax, void *arg)
+{
+ if (bytes != 1)
+ return (-1);
+
+ if (in)
+ return (-1);
+
+ /* Pretend all writes to the 8259 are alright */
+ return (0);
+}
+
+INOUT_PORT(atpic, IO_ICU1, IOPORT_F_INOUT, atpic_handler);
+INOUT_PORT(atpic, IO_ICU1 + ICU_IMR_OFFSET, IOPORT_F_INOUT, atpic_handler);
+INOUT_PORT(atpic, IO_ICU2, IOPORT_F_INOUT, atpic_handler);
+INOUT_PORT(atpic, IO_ICU2 + ICU_IMR_OFFSET, IOPORT_F_INOUT, atpic_handler);
diff --git a/usr.sbin/bhyve/bhyverun.c b/usr.sbin/bhyve/bhyverun.c
new file mode 100644
index 0000000..8d03649
--- /dev/null
+++ b/usr.sbin/bhyve/bhyverun.c
@@ -0,0 +1,789 @@
+/*-
+ * Copyright (c) 2011 NetApp, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <sys/types.h>
+#include <sys/mman.h>
+#include <sys/time.h>
+
+#include <machine/segments.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <libgen.h>
+#include <unistd.h>
+#include <assert.h>
+#include <errno.h>
+#include <signal.h>
+#include <pthread.h>
+#include <pthread_np.h>
+
+#include <machine/vmm.h>
+#include <vmmapi.h>
+
+#include "bhyverun.h"
+#include "acpi.h"
+#include "inout.h"
+#include "dbgport.h"
+#include "mem.h"
+#include "mevent.h"
+#include "mptbl.h"
+#include "pci_emul.h"
+#include "xmsr.h"
+#include "ioapic.h"
+#include "spinup_ap.h"
+
+#define DEFAULT_GUEST_HZ 100
+#define DEFAULT_GUEST_TSLICE 200
+
+#define GUEST_NIO_PORT 0x488 /* guest upcalls via i/o port */
+
+#define VMEXIT_SWITCH 0 /* force vcpu switch in mux mode */
+#define VMEXIT_CONTINUE 1 /* continue from next instruction */
+#define VMEXIT_RESTART 2 /* restart current instruction */
+#define VMEXIT_ABORT 3 /* abort the vm run loop */
+#define VMEXIT_RESET 4 /* guest machine has reset */
+
+#define MB (1024UL * 1024)
+#define GB (1024UL * MB)
+
+typedef int (*vmexit_handler_t)(struct vmctx *, struct vm_exit *, int *vcpu);
+
+int guest_tslice = DEFAULT_GUEST_TSLICE;
+int guest_hz = DEFAULT_GUEST_HZ;
+char *vmname;
+
+u_long lomem_sz;
+u_long himem_sz;
+
+int guest_ncpus;
+
+static int pincpu = -1;
+static int guest_vcpu_mux;
+static int guest_vmexit_on_hlt, guest_vmexit_on_pause, disable_x2apic;
+
+static int foundcpus;
+
+static int strictio;
+
+static int acpi;
+
+static char *lomem_addr;
+static char *himem_addr;
+
+static char *progname;
+static const int BSP = 0;
+
+static int cpumask;
+
+static void vm_loop(struct vmctx *ctx, int vcpu, uint64_t rip);
+
+struct vm_exit vmexit[VM_MAXCPU];
+
+struct fbsdstats {
+ uint64_t vmexit_bogus;
+ uint64_t vmexit_bogus_switch;
+ uint64_t vmexit_hlt;
+ uint64_t vmexit_pause;
+ uint64_t vmexit_mtrap;
+ uint64_t vmexit_paging;
+ uint64_t cpu_switch_rotate;
+ uint64_t cpu_switch_direct;
+ int io_reset;
+} stats;
+
+struct mt_vmm_info {
+ pthread_t mt_thr;
+ struct vmctx *mt_ctx;
+ int mt_vcpu;
+} mt_vmm_info[VM_MAXCPU];
+
+static void
+usage(int code)
+{
+
+ fprintf(stderr,
+ "Usage: %s [-aehABHIP][-g <gdb port>][-z <hz>][-s <pci>]"
+ "[-S <pci>][-p pincpu][-n <pci>][-m lowmem][-M highmem]"
+ " <vmname>\n"
+ " -a: local apic is in XAPIC mode (default is X2APIC)\n"
+ " -A: create an ACPI table\n"
+ " -g: gdb port (default is %d and 0 means don't open)\n"
+ " -c: # cpus (default 1)\n"
+ " -p: pin vcpu 'n' to host cpu 'pincpu + n'\n"
+ " -B: inject breakpoint exception on vm entry\n"
+ " -H: vmexit from the guest on hlt\n"
+ " -I: present an ioapic to the guest\n"
+ " -P: vmexit from the guest on pause\n"
+ " -e: exit on unhandled i/o access\n"
+ " -h: help\n"
+ " -z: guest hz (default is %d)\n"
+ " -s: <slot,driver,configinfo> PCI slot config\n"
+ " -S: <slot,driver,configinfo> legacy PCI slot config\n"
+ " -m: lowmem in MB\n"
+ " -M: highmem in MB\n"
+ " -x: mux vcpus to 1 hcpu\n"
+ " -t: mux vcpu timeslice hz (default %d)\n",
+ progname, DEFAULT_GDB_PORT, DEFAULT_GUEST_HZ,
+ DEFAULT_GUEST_TSLICE);
+ exit(code);
+}
+
+void *
+paddr_guest2host(uintptr_t gaddr)
+{
+ if (lomem_sz == 0)
+ return (NULL);
+
+ if (gaddr < lomem_sz) {
+ return ((void *)(lomem_addr + gaddr));
+ } else if (gaddr >= 4*GB && gaddr < (4*GB + himem_sz)) {
+ return ((void *)(himem_addr + gaddr - 4*GB));
+ } else
+ return (NULL);
+}
+
+int
+fbsdrun_disable_x2apic(void)
+{
+
+ return (disable_x2apic);
+}
+
+int
+fbsdrun_vmexit_on_pause(void)
+{
+
+ return (guest_vmexit_on_pause);
+}
+
+int
+fbsdrun_vmexit_on_hlt(void)
+{
+
+ return (guest_vmexit_on_hlt);
+}
+
+int
+fbsdrun_muxed(void)
+{
+
+ return (guest_vcpu_mux);
+}
+
+static void *
+fbsdrun_start_thread(void *param)
+{
+ char tname[MAXCOMLEN + 1];
+ struct mt_vmm_info *mtp;
+ int vcpu;
+
+ mtp = param;
+ vcpu = mtp->mt_vcpu;
+
+ snprintf(tname, sizeof(tname), "%s vcpu %d", vmname, vcpu);
+ pthread_set_name_np(mtp->mt_thr, tname);
+
+ vm_loop(mtp->mt_ctx, vcpu, vmexit[vcpu].rip);
+
+ /* not reached */
+ exit(1);
+ return (NULL);
+}
+
+void
+fbsdrun_addcpu(struct vmctx *ctx, int vcpu, uint64_t rip)
+{
+ int error;
+
+ if (cpumask & (1 << vcpu)) {
+ fprintf(stderr, "addcpu: attempting to add existing cpu %d\n",
+ vcpu);
+ exit(1);
+ }
+
+ cpumask |= 1 << vcpu;
+ foundcpus++;
+
+ /*
+ * Set up the vmexit struct to allow execution to start
+ * at the given RIP
+ */
+ vmexit[vcpu].rip = rip;
+ vmexit[vcpu].inst_length = 0;
+
+ if (vcpu == BSP || !guest_vcpu_mux){
+ mt_vmm_info[vcpu].mt_ctx = ctx;
+ mt_vmm_info[vcpu].mt_vcpu = vcpu;
+
+ error = pthread_create(&mt_vmm_info[vcpu].mt_thr, NULL,
+ fbsdrun_start_thread, &mt_vmm_info[vcpu]);
+ assert(error == 0);
+ }
+}
+
+static int
+fbsdrun_get_next_cpu(int curcpu)
+{
+
+ /*
+ * Get the next available CPU. Assumes they arrive
+ * in ascending order with no gaps.
+ */
+ return ((curcpu + 1) % foundcpus);
+}
+
+static int
+vmexit_catch_reset(void)
+{
+ stats.io_reset++;
+ return (VMEXIT_RESET);
+}
+
+static int
+vmexit_catch_inout(void)
+{
+ return (VMEXIT_ABORT);
+}
+
+static int
+vmexit_handle_notify(struct vmctx *ctx, struct vm_exit *vme, int *pvcpu,
+ uint32_t eax)
+{
+#if PG_DEBUG /* put all types of debug here */
+ if (eax == 0) {
+ pause_noswitch = 1;
+ } else if (eax == 1) {
+ pause_noswitch = 0;
+ } else {
+ pause_noswitch = 0;
+ if (eax == 5) {
+ vm_set_capability(ctx, *pvcpu, VM_CAP_MTRAP_EXIT, 1);
+ }
+ }
+#endif
+ return (VMEXIT_CONTINUE);
+}
+
+static int
+vmexit_inout(struct vmctx *ctx, struct vm_exit *vme, int *pvcpu)
+{
+ int error;
+ int bytes, port, in, out;
+ uint32_t eax;
+ int vcpu;
+
+ vcpu = *pvcpu;
+
+ port = vme->u.inout.port;
+ bytes = vme->u.inout.bytes;
+ eax = vme->u.inout.eax;
+ in = vme->u.inout.in;
+ out = !in;
+
+ /* We don't deal with these */
+ if (vme->u.inout.string || vme->u.inout.rep)
+ return (VMEXIT_ABORT);
+
+ /* Special case of guest reset */
+ if (out && port == 0x64 && (uint8_t)eax == 0xFE)
+ return (vmexit_catch_reset());
+
+ /* Extra-special case of host notifications */
+ if (out && port == GUEST_NIO_PORT)
+ return (vmexit_handle_notify(ctx, vme, pvcpu, eax));
+
+ error = emulate_inout(ctx, vcpu, in, port, bytes, &eax, strictio);
+ if (error == 0 && in)
+ error = vm_set_register(ctx, vcpu, VM_REG_GUEST_RAX, eax);
+
+ if (error == 0)
+ return (VMEXIT_CONTINUE);
+ else {
+ fprintf(stderr, "Unhandled %s%c 0x%04x\n",
+ in ? "in" : "out",
+ bytes == 1 ? 'b' : (bytes == 2 ? 'w' : 'l'), port);
+ return (vmexit_catch_inout());
+ }
+}
+
+static int
+vmexit_rdmsr(struct vmctx *ctx, struct vm_exit *vme, int *pvcpu)
+{
+ fprintf(stderr, "vm exit rdmsr 0x%x, cpu %d\n", vme->u.msr.code,
+ *pvcpu);
+ return (VMEXIT_ABORT);
+}
+
+static int
+vmexit_wrmsr(struct vmctx *ctx, struct vm_exit *vme, int *pvcpu)
+{
+ int newcpu;
+ int retval = VMEXIT_CONTINUE;
+
+ newcpu = emulate_wrmsr(ctx, *pvcpu, vme->u.msr.code,vme->u.msr.wval);
+
+ if (guest_vcpu_mux && *pvcpu != newcpu) {
+ retval = VMEXIT_SWITCH;
+ *pvcpu = newcpu;
+ }
+
+ return (retval);
+}
+
+static int
+vmexit_spinup_ap(struct vmctx *ctx, struct vm_exit *vme, int *pvcpu)
+{
+ int newcpu;
+ int retval = VMEXIT_CONTINUE;
+
+ newcpu = spinup_ap(ctx, *pvcpu,
+ vme->u.spinup_ap.vcpu, vme->u.spinup_ap.rip);
+
+ if (guest_vcpu_mux && *pvcpu != newcpu) {
+ retval = VMEXIT_SWITCH;
+ *pvcpu = newcpu;
+ }
+
+ return (retval);
+}
+
+static int
+vmexit_vmx(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
+{
+
+ fprintf(stderr, "vm exit[%d]\n", *pvcpu);
+ fprintf(stderr, "\treason\t\tVMX\n");
+ fprintf(stderr, "\trip\t\t0x%016lx\n", vmexit->rip);
+ fprintf(stderr, "\tinst_length\t%d\n", vmexit->inst_length);
+ fprintf(stderr, "\terror\t\t%d\n", vmexit->u.vmx.error);
+ fprintf(stderr, "\texit_reason\t%u\n", vmexit->u.vmx.exit_reason);
+ fprintf(stderr, "\tqualification\t0x%016lx\n",
+ vmexit->u.vmx.exit_qualification);
+
+ return (VMEXIT_ABORT);
+}
+
+static int bogus_noswitch = 1;
+
+static int
+vmexit_bogus(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
+{
+ stats.vmexit_bogus++;
+
+ if (!guest_vcpu_mux || guest_ncpus == 1 || bogus_noswitch) {
+ return (VMEXIT_RESTART);
+ } else {
+ stats.vmexit_bogus_switch++;
+ vmexit->inst_length = 0;
+ *pvcpu = -1;
+ return (VMEXIT_SWITCH);
+ }
+}
+
+static int
+vmexit_hlt(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
+{
+ stats.vmexit_hlt++;
+ if (fbsdrun_muxed()) {
+ *pvcpu = -1;
+ return (VMEXIT_SWITCH);
+ } else {
+ /*
+ * Just continue execution with the next instruction. We use
+ * the HLT VM exit as a way to be friendly with the host
+ * scheduler.
+ */
+ return (VMEXIT_CONTINUE);
+ }
+}
+
+static int pause_noswitch;
+
+static int
+vmexit_pause(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
+{
+ stats.vmexit_pause++;
+
+ if (fbsdrun_muxed() && !pause_noswitch) {
+ *pvcpu = -1;
+ return (VMEXIT_SWITCH);
+ } else {
+ return (VMEXIT_CONTINUE);
+ }
+}
+
+static int
+vmexit_mtrap(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
+{
+ stats.vmexit_mtrap++;
+
+ return (VMEXIT_RESTART);
+}
+
+static int
+vmexit_paging(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
+{
+ int err;
+ stats.vmexit_paging++;
+
+ err = emulate_mem(ctx, *pvcpu, vmexit->u.paging.gpa,
+ &vmexit->u.paging.vie);
+
+ if (err) {
+ if (err == EINVAL) {
+ fprintf(stderr,
+ "Failed to emulate instruction at 0x%lx\n",
+ vmexit->rip);
+ } else if (err == ESRCH) {
+ fprintf(stderr, "Unhandled memory access to 0x%lx\n",
+ vmexit->u.paging.gpa);
+ }
+
+ return (VMEXIT_ABORT);
+ }
+
+ return (VMEXIT_CONTINUE);
+}
+
+static void
+sigalrm(int sig)
+{
+ return;
+}
+
+static void
+setup_timeslice(void)
+{
+ struct sigaction sa;
+ struct itimerval itv;
+ int error;
+
+ /*
+ * Setup a realtime timer to generate a SIGALRM at a
+ * frequency of 'guest_tslice' ticks per second.
+ */
+ sigemptyset(&sa.sa_mask);
+ sa.sa_flags = 0;
+ sa.sa_handler = sigalrm;
+
+ error = sigaction(SIGALRM, &sa, NULL);
+ assert(error == 0);
+
+ itv.it_interval.tv_sec = 0;
+ itv.it_interval.tv_usec = 1000000 / guest_tslice;
+ itv.it_value.tv_sec = 0;
+ itv.it_value.tv_usec = 1000000 / guest_tslice;
+
+ error = setitimer(ITIMER_REAL, &itv, NULL);
+ assert(error == 0);
+}
+
+static vmexit_handler_t handler[VM_EXITCODE_MAX] = {
+ [VM_EXITCODE_INOUT] = vmexit_inout,
+ [VM_EXITCODE_VMX] = vmexit_vmx,
+ [VM_EXITCODE_BOGUS] = vmexit_bogus,
+ [VM_EXITCODE_RDMSR] = vmexit_rdmsr,
+ [VM_EXITCODE_WRMSR] = vmexit_wrmsr,
+ [VM_EXITCODE_MTRAP] = vmexit_mtrap,
+ [VM_EXITCODE_PAGING] = vmexit_paging,
+ [VM_EXITCODE_SPINUP_AP] = vmexit_spinup_ap,
+};
+
+static void
+vm_loop(struct vmctx *ctx, int vcpu, uint64_t rip)
+{
+ int error, rc, prevcpu;
+
+ if (guest_vcpu_mux)
+ setup_timeslice();
+
+ if (pincpu >= 0) {
+ error = vm_set_pinning(ctx, vcpu, pincpu + vcpu);
+ assert(error == 0);
+ }
+
+ while (1) {
+ error = vm_run(ctx, vcpu, rip, &vmexit[vcpu]);
+ if (error != 0) {
+ /*
+ * It is possible that 'vmmctl' or some other process
+ * has transitioned the vcpu to CANNOT_RUN state right
+ * before we tried to transition it to RUNNING.
+ *
+ * This is expected to be temporary so just retry.
+ */
+ if (errno == EBUSY)
+ continue;
+ else
+ break;
+ }
+
+ prevcpu = vcpu;
+ rc = (*handler[vmexit[vcpu].exitcode])(ctx, &vmexit[vcpu],
+ &vcpu);
+ switch (rc) {
+ case VMEXIT_SWITCH:
+ assert(guest_vcpu_mux);
+ if (vcpu == -1) {
+ stats.cpu_switch_rotate++;
+ vcpu = fbsdrun_get_next_cpu(prevcpu);
+ } else {
+ stats.cpu_switch_direct++;
+ }
+ /* fall through */
+ case VMEXIT_CONTINUE:
+ rip = vmexit[vcpu].rip + vmexit[vcpu].inst_length;
+ break;
+ case VMEXIT_RESTART:
+ rip = vmexit[vcpu].rip;
+ break;
+ case VMEXIT_RESET:
+ exit(0);
+ default:
+ exit(1);
+ }
+ }
+ fprintf(stderr, "vm_run error %d, errno %d\n", error, errno);
+}
+
+static int
+num_vcpus_allowed(struct vmctx *ctx)
+{
+ int tmp, error;
+
+ error = vm_get_capability(ctx, BSP, VM_CAP_UNRESTRICTED_GUEST, &tmp);
+
+ /*
+ * The guest is allowed to spinup more than one processor only if the
+ * UNRESTRICTED_GUEST capability is available.
+ */
+ if (error == 0)
+ return (VM_MAXCPU);
+ else
+ return (1);
+}
+
+int
+main(int argc, char *argv[])
+{
+ int c, error, gdb_port, inject_bkpt, tmp, err, ioapic, bvmcons;
+ int max_vcpus;
+ struct vmctx *ctx;
+ uint64_t rip;
+
+ bvmcons = 0;
+ inject_bkpt = 0;
+ progname = basename(argv[0]);
+ gdb_port = DEFAULT_GDB_PORT;
+ guest_ncpus = 1;
+ ioapic = 0;
+
+ while ((c = getopt(argc, argv, "abehABHIPxp:g:c:z:s:S:n:m:M:")) != -1) {
+ switch (c) {
+ case 'a':
+ disable_x2apic = 1;
+ break;
+ case 'A':
+ acpi = 1;
+ break;
+ case 'b':
+ bvmcons = 1;
+ break;
+ case 'B':
+ inject_bkpt = 1;
+ break;
+ case 'x':
+ guest_vcpu_mux = 1;
+ break;
+ case 'p':
+ pincpu = atoi(optarg);
+ break;
+ case 'c':
+ guest_ncpus = atoi(optarg);
+ break;
+ case 'g':
+ gdb_port = atoi(optarg);
+ break;
+ case 'z':
+ guest_hz = atoi(optarg);
+ break;
+ case 't':
+ guest_tslice = atoi(optarg);
+ break;
+ case 's':
+ pci_parse_slot(optarg, 0);
+ break;
+ case 'S':
+ pci_parse_slot(optarg, 1);
+ break;
+ case 'm':
+ lomem_sz = strtoul(optarg, NULL, 0) * MB;
+ break;
+ case 'M':
+ himem_sz = strtoul(optarg, NULL, 0) * MB;
+ break;
+ case 'H':
+ guest_vmexit_on_hlt = 1;
+ break;
+ case 'I':
+ ioapic = 1;
+ break;
+ case 'P':
+ guest_vmexit_on_pause = 1;
+ break;
+ case 'e':
+ strictio = 1;
+ break;
+ case 'h':
+ usage(0);
+ default:
+ usage(1);
+ }
+ }
+ argc -= optind;
+ argv += optind;
+
+ if (argc != 1)
+ usage(1);
+
+ /* No need to mux if guest is uni-processor */
+ if (guest_ncpus <= 1)
+ guest_vcpu_mux = 0;
+
+ /* vmexit on hlt if guest is muxed */
+ if (guest_vcpu_mux) {
+ guest_vmexit_on_hlt = 1;
+ guest_vmexit_on_pause = 1;
+ }
+
+ vmname = argv[0];
+
+ ctx = vm_open(vmname);
+ if (ctx == NULL) {
+ perror("vm_open");
+ exit(1);
+ }
+
+ max_vcpus = num_vcpus_allowed(ctx);
+ if (guest_ncpus > max_vcpus) {
+ fprintf(stderr, "%d vCPUs requested but only %d available\n",
+ guest_ncpus, max_vcpus);
+ exit(1);
+ }
+
+ if (fbsdrun_vmexit_on_hlt()) {
+ err = vm_get_capability(ctx, BSP, VM_CAP_HALT_EXIT, &tmp);
+ if (err < 0) {
+ fprintf(stderr, "VM exit on HLT not supported\n");
+ exit(1);
+ }
+ vm_set_capability(ctx, BSP, VM_CAP_HALT_EXIT, 1);
+ handler[VM_EXITCODE_HLT] = vmexit_hlt;
+ }
+
+ if (fbsdrun_vmexit_on_pause()) {
+ /*
+ * pause exit support required for this mode
+ */
+ err = vm_get_capability(ctx, BSP, VM_CAP_PAUSE_EXIT, &tmp);
+ if (err < 0) {
+ fprintf(stderr,
+ "SMP mux requested, no pause support\n");
+ exit(1);
+ }
+ vm_set_capability(ctx, BSP, VM_CAP_PAUSE_EXIT, 1);
+ handler[VM_EXITCODE_PAUSE] = vmexit_pause;
+ }
+
+ if (fbsdrun_disable_x2apic())
+ err = vm_set_x2apic_state(ctx, BSP, X2APIC_DISABLED);
+ else
+ err = vm_set_x2apic_state(ctx, BSP, X2APIC_ENABLED);
+
+ if (err) {
+ fprintf(stderr, "Unable to set x2apic state (%d)\n", err);
+ exit(1);
+ }
+
+ if (lomem_sz != 0) {
+ lomem_addr = vm_map_memory(ctx, 0, lomem_sz);
+ if (lomem_addr == (char *) MAP_FAILED) {
+ lomem_sz = 0;
+ } else if (himem_sz != 0) {
+ himem_addr = vm_map_memory(ctx, 4*GB, himem_sz);
+ if (himem_addr == (char *) MAP_FAILED) {
+ lomem_sz = 0;
+ himem_sz = 0;
+ }
+ }
+ }
+
+ init_inout();
+ init_pci(ctx);
+ if (ioapic)
+ ioapic_init(0);
+
+ if (gdb_port != 0)
+ init_dbgport(gdb_port);
+
+ if (bvmcons)
+ init_bvmcons();
+
+ error = vm_get_register(ctx, BSP, VM_REG_GUEST_RIP, &rip);
+ assert(error == 0);
+
+ if (inject_bkpt) {
+ error = vm_inject_event(ctx, BSP, VM_HW_EXCEPTION, IDT_BP);
+ assert(error == 0);
+ }
+
+ /*
+ * build the guest tables, MP etc.
+ */
+ mptable_build(ctx, guest_ncpus, ioapic);
+
+ if (acpi) {
+ error = acpi_build(ctx, guest_ncpus, ioapic);
+ assert(error == 0);
+ }
+
+ /*
+ * Add CPU 0
+ */
+ fbsdrun_addcpu(ctx, BSP, rip);
+
+ /*
+ * Head off to the main event dispatch loop
+ */
+ mevent_dispatch();
+
+ exit(1);
+}
diff --git a/usr.sbin/bhyve/bhyverun.h b/usr.sbin/bhyve/bhyverun.h
new file mode 100644
index 0000000..45033b8
--- /dev/null
+++ b/usr.sbin/bhyve/bhyverun.h
@@ -0,0 +1,53 @@
+/*-
+ * Copyright (c) 2011 NetApp, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ */
+
+#ifndef _FBSDRUN_H_
+#define _FBSDRUN_H_
+
+#ifndef CTASSERT /* Allow lint to override */
+#define CTASSERT(x) _CTASSERT(x, __LINE__)
+#define _CTASSERT(x, y) __CTASSERT(x, y)
+#define __CTASSERT(x, y) typedef char __assert ## y[(x) ? 1 : -1]
+#endif
+
+struct vmctx;
+extern int guest_hz;
+extern int guest_tslice;
+extern int guest_ncpus;
+extern char *vmname;
+
+extern u_long lomem_sz, himem_sz;
+
+void *paddr_guest2host(uintptr_t);
+
+void fbsdrun_addcpu(struct vmctx *ctx, int cpu, uint64_t rip);
+int fbsdrun_muxed(void);
+int fbsdrun_vmexit_on_hlt(void);
+int fbsdrun_vmexit_on_pause(void);
+int fbsdrun_disable_x2apic(void);
+#endif
diff --git a/usr.sbin/bhyve/consport.c b/usr.sbin/bhyve/consport.c
new file mode 100644
index 0000000..3915b6d
--- /dev/null
+++ b/usr.sbin/bhyve/consport.c
@@ -0,0 +1,140 @@
+/*-
+ * Copyright (c) 2011 NetApp, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <sys/types.h>
+#include <sys/select.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <termios.h>
+#include <unistd.h>
+#include <stdbool.h>
+
+#include "inout.h"
+
+#define BVM_CONSOLE_PORT 0x220
+#define BVM_CONS_SIG ('b' << 8 | 'v')
+
+static struct termios tio_orig, tio_new;
+
+static void
+ttyclose(void)
+{
+ tcsetattr(STDIN_FILENO, TCSANOW, &tio_orig);
+}
+
+static void
+ttyopen(void)
+{
+ tcgetattr(STDIN_FILENO, &tio_orig);
+
+ cfmakeraw(&tio_new);
+ tcsetattr(STDIN_FILENO, TCSANOW, &tio_new);
+
+ atexit(ttyclose);
+}
+
+static bool
+tty_char_available(void)
+{
+ fd_set rfds;
+ struct timeval tv;
+
+ FD_ZERO(&rfds);
+ FD_SET(STDIN_FILENO, &rfds);
+ tv.tv_sec = 0;
+ tv.tv_usec = 0;
+ if (select(STDIN_FILENO + 1, &rfds, NULL, NULL, &tv) > 0) {
+ return (true);
+ } else {
+ return (false);
+ }
+}
+
+static int
+ttyread(void)
+{
+ char rb;
+
+ if (tty_char_available()) {
+ read(STDIN_FILENO, &rb, 1);
+ return (rb & 0xff);
+ } else {
+ return (-1);
+ }
+}
+
+static void
+ttywrite(unsigned char wb)
+{
+ (void) write(STDOUT_FILENO, &wb, 1);
+}
+
+static int
+console_handler(struct vmctx *ctx, int vcpu, int in, int port, int bytes,
+ uint32_t *eax, void *arg)
+{
+ static int opened;
+
+ if (bytes == 2 && in) {
+ *eax = BVM_CONS_SIG;
+ return (0);
+ }
+
+ if (bytes != 4)
+ return (-1);
+
+ if (!opened) {
+ ttyopen();
+ opened = 1;
+ }
+
+ if (in)
+ *eax = ttyread();
+ else
+ ttywrite(*eax);
+
+ return (0);
+}
+
+static struct inout_port consport = {
+ "bvmcons",
+ BVM_CONSOLE_PORT,
+ IOPORT_F_INOUT,
+ console_handler
+};
+
+void
+init_bvmcons(void)
+{
+
+ register_inout(&consport);
+}
diff --git a/usr.sbin/bhyve/dbgport.c b/usr.sbin/bhyve/dbgport.c
new file mode 100644
index 0000000..034531c
--- /dev/null
+++ b/usr.sbin/bhyve/dbgport.c
@@ -0,0 +1,138 @@
+/*-
+ * Copyright (c) 2011 NetApp, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <sys/uio.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <errno.h>
+
+#include "inout.h"
+#include "dbgport.h"
+
+#define BVM_DBG_PORT 0x224
+#define BVM_DBG_SIG ('B' << 8 | 'V')
+
+static int listen_fd, conn_fd;
+
+static struct sockaddr_in sin;
+
+static int
+dbg_handler(struct vmctx *ctx, int vcpu, int in, int port, int bytes,
+ uint32_t *eax, void *arg)
+{
+ char ch;
+ int nwritten, nread, printonce;
+
+ if (bytes == 2 && in) {
+ *eax = BVM_DBG_SIG;
+ return (0);
+ }
+
+ if (bytes != 4)
+ return (-1);
+
+again:
+ printonce = 0;
+ while (conn_fd < 0) {
+ if (!printonce) {
+ printf("Waiting for connection from gdb\r\n");
+ printonce = 1;
+ }
+ conn_fd = accept(listen_fd, NULL, NULL);
+ if (conn_fd >= 0)
+ fcntl(conn_fd, F_SETFL, O_NONBLOCK);
+ else if (errno != EINTR)
+ perror("accept");
+ }
+
+ if (in) {
+ nread = read(conn_fd, &ch, 1);
+ if (nread == -1 && errno == EAGAIN)
+ *eax = -1;
+ else if (nread == 1)
+ *eax = ch;
+ else {
+ close(conn_fd);
+ conn_fd = -1;
+ goto again;
+ }
+ } else {
+ ch = *eax;
+ nwritten = write(conn_fd, &ch, 1);
+ if (nwritten != 1) {
+ close(conn_fd);
+ conn_fd = -1;
+ goto again;
+ }
+ }
+ return (0);
+}
+
+static struct inout_port dbgport = {
+ "bvmdbg",
+ BVM_DBG_PORT,
+ IOPORT_F_INOUT,
+ dbg_handler
+};
+
+void
+init_dbgport(int sport)
+{
+ conn_fd = -1;
+
+ if ((listen_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
+ perror("socket");
+ exit(1);
+ }
+
+ sin.sin_len = sizeof(sin);
+ sin.sin_family = AF_INET;
+ sin.sin_addr.s_addr = htonl(INADDR_ANY);
+ sin.sin_port = htons(sport);
+
+ if (bind(listen_fd, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
+ perror("bind");
+ exit(1);
+ }
+
+ if (listen(listen_fd, 1) < 0) {
+ perror("listen");
+ exit(1);
+ }
+
+ register_inout(&dbgport);
+}
diff --git a/usr.sbin/bhyve/dbgport.h b/usr.sbin/bhyve/dbgport.h
new file mode 100644
index 0000000..8c7dab7
--- /dev/null
+++ b/usr.sbin/bhyve/dbgport.h
@@ -0,0 +1,36 @@
+/*-
+ * Copyright (c) 2011 NetApp, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ */
+
+#ifndef _DBGPORT_H_
+#define _DBGPORT_H_
+
+#define DEFAULT_GDB_PORT 6466
+
+void init_dbgport(int port);
+
+#endif
diff --git a/usr.sbin/bhyve/elcr.c b/usr.sbin/bhyve/elcr.c
new file mode 100644
index 0000000..2417ae1
--- /dev/null
+++ b/usr.sbin/bhyve/elcr.c
@@ -0,0 +1,65 @@
+/*-
+ * Copyright (c) 2011 NetApp, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <sys/types.h>
+
+#include "inout.h"
+
+/*
+ * EISA interrupt Level Control Register.
+ *
+ * This is a 16-bit register with one bit for each of the IRQ0 through IRQ15.
+ * A level triggered irq is indicated by setting the corresponding bit to '1'.
+ */
+#define ELCR_PORT 0x4d0
+
+static uint8_t elcr[2] = { 0x00, 0x00 };
+
+static int
+elcr_handler(struct vmctx *ctx, int vcpu, int in, int port, int bytes,
+ uint32_t *eax, void *arg)
+{
+ int idx;
+
+ if (bytes != 1)
+ return (-1);
+
+ idx = port - ELCR_PORT;
+
+ if (in)
+ *eax = elcr[idx];
+ else
+ elcr[idx] = *eax;
+
+ return (0);
+}
+INOUT_PORT(elcr, ELCR_PORT + 0, IOPORT_F_INOUT, elcr_handler);
+INOUT_PORT(elcr, ELCR_PORT + 1, IOPORT_F_INOUT, elcr_handler);
diff --git a/usr.sbin/bhyve/inout.c b/usr.sbin/bhyve/inout.c
new file mode 100644
index 0000000..5f47a89f
--- /dev/null
+++ b/usr.sbin/bhyve/inout.c
@@ -0,0 +1,151 @@
+/*-
+ * Copyright (c) 2011 NetApp, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <sys/param.h>
+#include <sys/linker_set.h>
+
+#include <stdio.h>
+#include <assert.h>
+
+#include "inout.h"
+
+SET_DECLARE(inout_port_set, struct inout_port);
+
+#define MAX_IOPORTS (1 << 16)
+
+static struct {
+ const char *name;
+ int flags;
+ inout_func_t handler;
+ void *arg;
+} inout_handlers[MAX_IOPORTS];
+
+static int
+default_inout(struct vmctx *ctx, int vcpu, int in, int port, int bytes,
+ uint32_t *eax, void *arg)
+{
+ if (in) {
+ switch (bytes) {
+ case 4:
+ *eax = 0xffffffff;
+ break;
+ case 2:
+ *eax = 0xffff;
+ break;
+ case 1:
+ *eax = 0xff;
+ break;
+ }
+ }
+
+ return (0);
+}
+
+int
+emulate_inout(struct vmctx *ctx, int vcpu, int in, int port, int bytes,
+ uint32_t *eax, int strict)
+{
+ int flags;
+ uint32_t mask;
+ inout_func_t handler;
+ void *arg;
+
+ assert(port < MAX_IOPORTS);
+
+ handler = inout_handlers[port].handler;
+
+ if (strict && handler == default_inout)
+ return (-1);
+
+ if (!in) {
+ switch (bytes) {
+ case 1:
+ mask = 0xff;
+ break;
+ case 2:
+ mask = 0xffff;
+ break;
+ default:
+ mask = 0xffffffff;
+ break;
+ }
+ *eax = *eax & mask;
+ }
+
+ flags = inout_handlers[port].flags;
+ arg = inout_handlers[port].arg;
+
+ if ((in && (flags & IOPORT_F_IN)) || (!in && (flags & IOPORT_F_OUT)))
+ return ((*handler)(ctx, vcpu, in, port, bytes, eax, arg));
+ else
+ return (-1);
+}
+
+void
+init_inout(void)
+{
+ struct inout_port **iopp, *iop;
+ int i;
+
+ /*
+ * Set up the default handler for all ports
+ */
+ for (i = 0; i < MAX_IOPORTS; i++) {
+ inout_handlers[i].name = "default";
+ inout_handlers[i].flags = IOPORT_F_IN | IOPORT_F_OUT;
+ inout_handlers[i].handler = default_inout;
+ inout_handlers[i].arg = NULL;
+ }
+
+ /*
+ * Overwrite with specified handlers
+ */
+ SET_FOREACH(iopp, inout_port_set) {
+ iop = *iopp;
+ assert(iop->port < MAX_IOPORTS);
+ inout_handlers[iop->port].name = iop->name;
+ inout_handlers[iop->port].flags = iop->flags;
+ inout_handlers[iop->port].handler = iop->handler;
+ inout_handlers[iop->port].arg = NULL;
+ }
+}
+
+int
+register_inout(struct inout_port *iop)
+{
+ assert(iop->port < MAX_IOPORTS);
+ inout_handlers[iop->port].name = iop->name;
+ inout_handlers[iop->port].flags = iop->flags;
+ inout_handlers[iop->port].handler = iop->handler;
+ inout_handlers[iop->port].arg = iop->arg;
+
+ return (0);
+}
diff --git a/usr.sbin/bhyve/inout.h b/usr.sbin/bhyve/inout.h
new file mode 100644
index 0000000..a73b78d
--- /dev/null
+++ b/usr.sbin/bhyve/inout.h
@@ -0,0 +1,67 @@
+/*-
+ * Copyright (c) 2011 NetApp, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ */
+
+#ifndef _INOUT_H_
+#define _INOUT_H_
+
+#include <sys/linker_set.h>
+
+struct vmctx;
+
+typedef int (*inout_func_t)(struct vmctx *ctx, int vcpu, int in, int port,
+ int bytes, uint32_t *eax, void *arg);
+
+struct inout_port {
+ const char *name;
+ int port;
+ int flags;
+ inout_func_t handler;
+ void *arg;
+};
+#define IOPORT_F_IN 0x1
+#define IOPORT_F_OUT 0x2
+#define IOPORT_F_INOUT 0x3
+
+#define INOUT_PORT(name, port, flags, handler) \
+ static struct inout_port __CONCAT(__inout_port, __LINE__) = { \
+ #name, \
+ (port), \
+ (flags), \
+ (handler), \
+ 0 \
+ }; \
+ DATA_SET(inout_port_set, __CONCAT(__inout_port, __LINE__))
+
+void init_inout(void);
+int emulate_inout(struct vmctx *, int vcpu, int in, int port, int bytes,
+ uint32_t *eax, int strict);
+int register_inout(struct inout_port *iop);
+
+void init_bvmcons(void);
+
+#endif /* _INOUT_H_ */
diff --git a/usr.sbin/bhyve/ioapic.c b/usr.sbin/bhyve/ioapic.c
new file mode 100644
index 0000000..c712692
--- /dev/null
+++ b/usr.sbin/bhyve/ioapic.c
@@ -0,0 +1,324 @@
+/*-
+ * Copyright (c) 2012 NetApp, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <sys/types.h>
+
+#include <x86/apicreg.h>
+#include <machine/vmm.h>
+
+#include <string.h>
+#include <assert.h>
+#include <stdbool.h>
+
+#include <vmmapi.h>
+
+#include "inout.h"
+#include "mem.h"
+#include "bhyverun.h"
+
+#include <stdio.h>
+
+#define IOAPIC_PADDR 0xFEC00000
+
+#define IOREGSEL 0x00
+#define IOWIN 0x10
+
+#define REDIR_ENTRIES 16
+#define INTR_ASSERTED(ioapic, pin) ((ioapic)->pinstate[(pin)] == true)
+
+struct ioapic {
+ int inited;
+ uint32_t id;
+ uint64_t redtbl[REDIR_ENTRIES];
+ bool pinstate[REDIR_ENTRIES];
+
+ uintptr_t paddr; /* gpa where the ioapic is mapped */
+ uint32_t ioregsel;
+ struct memory_region *region;
+};
+
+static struct ioapic ioapics[1]; /* only a single ioapic for now */
+
+static int ioapic_region_read(struct ioapic *ioapic, uintptr_t paddr,
+ int size, uint64_t *data);
+static int ioapic_region_write(struct ioapic *ioapic, uintptr_t paddr,
+ int size, uint64_t data);
+static int ioapic_region_handler(struct vmctx *vm, int vcpu, int dir,
+ uintptr_t paddr, int size, uint64_t *val,
+ void *arg1, long arg2);
+
+static void
+ioapic_set_pinstate(struct vmctx *ctx, int pin, bool newstate)
+{
+ int vector, apicid, vcpu;
+ uint32_t low, high;
+ struct ioapic *ioapic;
+
+ ioapic = &ioapics[0]; /* assume a single ioapic */
+
+ if (pin < 0 || pin >= REDIR_ENTRIES)
+ return;
+
+ /* Nothing to do if interrupt pin has not changed state */
+ if (ioapic->pinstate[pin] == newstate)
+ return;
+
+ ioapic->pinstate[pin] = newstate; /* record it */
+
+ /* Nothing to do if interrupt pin is deasserted */
+ if (!INTR_ASSERTED(ioapic, pin))
+ return;
+
+ /*
+ * XXX
+ * We only deal with:
+ * - edge triggered interrupts
+ * - physical destination mode
+ * - fixed delivery mode
+ */
+ low = ioapic->redtbl[pin];
+ high = ioapic->redtbl[pin] >> 32;
+ if ((low & IOART_INTMASK) == IOART_INTMCLR &&
+ (low & IOART_TRGRMOD) == IOART_TRGREDG &&
+ (low & IOART_DESTMOD) == IOART_DESTPHY &&
+ (low & IOART_DELMOD) == IOART_DELFIXED) {
+ vector = low & IOART_INTVEC;
+ apicid = high >> APIC_ID_SHIFT;
+ if (apicid != 0xff) {
+ /* unicast */
+ vcpu = vm_apicid2vcpu(ctx, apicid);
+ vm_lapic_irq(ctx, vcpu, vector);
+ } else {
+ /* broadcast */
+ vcpu = 0;
+ while (vcpu < guest_ncpus) {
+ vm_lapic_irq(ctx, vcpu, vector);
+ vcpu++;
+ }
+ }
+ }
+}
+
+void
+ioapic_deassert_pin(struct vmctx *ctx, int pin)
+{
+ ioapic_set_pinstate(ctx, pin, false);
+}
+
+void
+ioapic_assert_pin(struct vmctx *ctx, int pin)
+{
+ ioapic_set_pinstate(ctx, pin, true);
+}
+
+void
+ioapic_init(int which)
+{
+ struct mem_range memp;
+ struct ioapic *ioapic;
+ int error;
+ int i;
+
+ assert(which == 0);
+
+ ioapic = &ioapics[which];
+ assert(ioapic->inited == 0);
+
+ bzero(ioapic, sizeof(struct ioapic));
+
+ /* Initialize all redirection entries to mask all interrupts */
+ for (i = 0; i < REDIR_ENTRIES; i++)
+ ioapic->redtbl[i] = 0x0001000000010000UL;
+
+ ioapic->paddr = IOAPIC_PADDR;
+
+ /* Register emulated memory region */
+ memp.name = "ioapic";
+ memp.flags = MEM_F_RW;
+ memp.handler = ioapic_region_handler;
+ memp.arg1 = ioapic;
+ memp.arg2 = which;
+ memp.base = ioapic->paddr;
+ memp.size = sizeof(struct IOAPIC);
+ error = register_mem(&memp);
+
+ assert (error == 0);
+
+ ioapic->inited = 1;
+}
+
+static uint32_t
+ioapic_read(struct ioapic *ioapic, uint32_t addr)
+{
+ int regnum, pin, rshift;
+
+ assert(ioapic->inited);
+
+ regnum = addr & 0xff;
+ switch (regnum) {
+ case IOAPIC_ID:
+ return (ioapic->id);
+ break;
+ case IOAPIC_VER:
+ return ((REDIR_ENTRIES << MAXREDIRSHIFT) | 0x11);
+ break;
+ case IOAPIC_ARB:
+ return (ioapic->id);
+ break;
+ default:
+ break;
+ }
+
+ /* redirection table entries */
+ if (regnum >= IOAPIC_REDTBL &&
+ regnum < IOAPIC_REDTBL + REDIR_ENTRIES * 2) {
+ pin = (regnum - IOAPIC_REDTBL) / 2;
+ if ((regnum - IOAPIC_REDTBL) % 2)
+ rshift = 32;
+ else
+ rshift = 0;
+
+ return (ioapic->redtbl[pin] >> rshift);
+ }
+
+ return (0);
+}
+
+static void
+ioapic_write(struct ioapic *ioapic, uint32_t addr, uint32_t data)
+{
+ int regnum, pin, lshift;
+
+ assert(ioapic->inited);
+
+ regnum = addr & 0xff;
+ switch (regnum) {
+ case IOAPIC_ID:
+ ioapic->id = data & APIC_ID_MASK;
+ break;
+ case IOAPIC_VER:
+ case IOAPIC_ARB:
+ /* readonly */
+ break;
+ default:
+ break;
+ }
+
+ /* redirection table entries */
+ if (regnum >= IOAPIC_REDTBL &&
+ regnum < IOAPIC_REDTBL + REDIR_ENTRIES * 2) {
+ pin = (regnum - IOAPIC_REDTBL) / 2;
+ if ((regnum - IOAPIC_REDTBL) % 2)
+ lshift = 32;
+ else
+ lshift = 0;
+
+ ioapic->redtbl[pin] &= ~((uint64_t)0xffffffff << lshift);
+ ioapic->redtbl[pin] |= ((uint64_t)data << lshift);
+ }
+}
+
+static int
+ioapic_region_read(struct ioapic *ioapic, uintptr_t paddr, int size,
+ uint64_t *data)
+{
+ int offset;
+
+ offset = paddr - ioapic->paddr;
+
+ /*
+ * The IOAPIC specification allows 32-bit wide accesses to the
+ * IOREGSEL (offset 0) and IOWIN (offset 16) registers.
+ */
+ if (size != 4 || (offset != IOREGSEL && offset != IOWIN)) {
+#if 1
+ printf("invalid access to ioapic%d: size %d, offset %d\n",
+ (int)(ioapic - ioapics), size, offset);
+#endif
+ *data = 0;
+ return (0);
+ }
+
+ if (offset == IOREGSEL)
+ *data = ioapic->ioregsel;
+ else
+ *data = ioapic_read(ioapic, ioapic->ioregsel);
+
+ return (0);
+}
+
+static int
+ioapic_region_write(struct ioapic *ioapic, uintptr_t paddr, int size,
+ uint64_t data)
+{
+ int offset;
+
+ offset = paddr - ioapic->paddr;
+
+ /*
+ * The ioapic specification allows 32-bit wide accesses to the
+ * IOREGSEL (offset 0) and IOWIN (offset 16) registers.
+ */
+ if (size != 4 || (offset != IOREGSEL && offset != IOWIN)) {
+#if 1
+ printf("invalid access to ioapic%d: size %d, offset %d\n",
+ (int)(ioapic - ioapics), size, offset);
+#endif
+ return (0);
+ }
+
+ if (offset == IOREGSEL)
+ ioapic->ioregsel = data;
+ else
+ ioapic_write(ioapic, ioapic->ioregsel, data);
+
+ return (0);
+}
+
+static int
+ioapic_region_handler(struct vmctx *vm, int vcpu, int dir, uintptr_t paddr,
+ int size, uint64_t *val, void *arg1, long arg2)
+{
+ struct ioapic *ioapic;
+ int which;
+
+ ioapic = arg1;
+ which = arg2;
+
+ assert(ioapic == &ioapics[which]);
+
+ if (dir == MEM_F_READ)
+ ioapic_region_read(ioapic, paddr, size, val);
+ else
+ ioapic_region_write(ioapic, paddr, size, *val);
+
+ return (0);
+}
diff --git a/usr.sbin/bhyve/ioapic.h b/usr.sbin/bhyve/ioapic.h
new file mode 100644
index 0000000..4696f9a
--- /dev/null
+++ b/usr.sbin/bhyve/ioapic.h
@@ -0,0 +1,38 @@
+/*-
+ * Copyright (c) 2012 NetApp, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ */
+
+#ifndef _IOAPIC_H_
+#define _IOAPIC_H_
+
+struct vmctx;
+
+void ioapic_init(int num);
+void ioapic_deassert_pin(struct vmctx *ctx, int pin);
+void ioapic_assert_pin(struct vmctx *ctx, int pin);
+
+#endif
diff --git a/usr.sbin/bhyve/mem.c b/usr.sbin/bhyve/mem.c
new file mode 100644
index 0000000..27f4782
--- /dev/null
+++ b/usr.sbin/bhyve/mem.c
@@ -0,0 +1,218 @@
+/*-
+ * Copyright (c) 2012 NetApp, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ */
+
+/*
+ * Memory ranges are represented with an RB tree. On insertion, the range
+ * is checked for overlaps. On lookup, the key has the same base and limit
+ * so it can be searched within the range.
+ *
+ * It is assumed that all setup of ranges takes place in single-threaded
+ * mode before vCPUs have been started. As such, no locks are used on the
+ * RB tree. If this is no longer the case, then a r/w lock could be used,
+ * with readers on the lookup and a writer if the tree needs to be changed
+ * (and per vCPU caches flushed)
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <sys/types.h>
+#include <sys/tree.h>
+#include <sys/errno.h>
+#include <machine/vmm.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <assert.h>
+
+#include "mem.h"
+
+struct mmio_rb_range {
+ RB_ENTRY(mmio_rb_range) mr_link; /* RB tree links */
+ struct mem_range mr_param;
+ uint64_t mr_base;
+ uint64_t mr_end;
+};
+
+struct mmio_rb_tree;
+RB_PROTOTYPE(mmio_rb_tree, mmio_rb_range, mr_link, mmio_rb_range_compare);
+
+RB_HEAD(mmio_rb_tree, mmio_rb_range) mmio_rbroot;
+
+/*
+ * Per-vCPU cache. Since most accesses from a vCPU will be to
+ * consecutive addresses in a range, it makes sense to cache the
+ * result of a lookup.
+ */
+static struct mmio_rb_range *mmio_hint[VM_MAXCPU];
+
+static int
+mmio_rb_range_compare(struct mmio_rb_range *a, struct mmio_rb_range *b)
+{
+ if (a->mr_end < b->mr_base)
+ return (-1);
+ else if (a->mr_base > b->mr_end)
+ return (1);
+ return (0);
+}
+
+static int
+mmio_rb_lookup(uint64_t addr, struct mmio_rb_range **entry)
+{
+ struct mmio_rb_range find, *res;
+
+ find.mr_base = find.mr_end = addr;
+
+ res = RB_FIND(mmio_rb_tree, &mmio_rbroot, &find);
+
+ if (res != NULL) {
+ *entry = res;
+ return (0);
+ }
+
+ return (ENOENT);
+}
+
+static int
+mmio_rb_add(struct mmio_rb_range *new)
+{
+ struct mmio_rb_range *overlap;
+
+ overlap = RB_INSERT(mmio_rb_tree, &mmio_rbroot, new);
+
+ if (overlap != NULL) {
+#ifdef RB_DEBUG
+ printf("overlap detected: new %lx:%lx, tree %lx:%lx\n",
+ new->mr_base, new->mr_end,
+ overlap->mr_base, overlap->mr_end);
+#endif
+
+ return (EEXIST);
+ }
+
+ return (0);
+}
+
+#if 0
+static void
+mmio_rb_dump(void)
+{
+ struct mmio_rb_range *np;
+
+ RB_FOREACH(np, mmio_rb_tree, &mmio_rbroot) {
+ printf(" %lx:%lx, %s\n", np->mr_base, np->mr_end,
+ np->mr_param.name);
+ }
+}
+#endif
+
+RB_GENERATE(mmio_rb_tree, mmio_rb_range, mr_link, mmio_rb_range_compare);
+
+static int
+mem_read(void *ctx, int vcpu, uint64_t gpa, uint64_t *rval, int size, void *arg)
+{
+ int error;
+ struct mem_range *mr = arg;
+
+ error = (*mr->handler)(ctx, vcpu, MEM_F_READ, gpa, size,
+ rval, mr->arg1, mr->arg2);
+ return (error);
+}
+
+static int
+mem_write(void *ctx, int vcpu, uint64_t gpa, uint64_t wval, int size, void *arg)
+{
+ int error;
+ struct mem_range *mr = arg;
+
+ error = (*mr->handler)(ctx, vcpu, MEM_F_WRITE, gpa, size,
+ &wval, mr->arg1, mr->arg2);
+ return (error);
+}
+
+int
+emulate_mem(struct vmctx *ctx, int vcpu, uint64_t paddr, struct vie *vie)
+{
+ struct mmio_rb_range *entry;
+ int err;
+
+ /*
+ * First check the per-vCPU cache
+ */
+ if (mmio_hint[vcpu] &&
+ paddr >= mmio_hint[vcpu]->mr_base &&
+ paddr <= mmio_hint[vcpu]->mr_end) {
+ entry = mmio_hint[vcpu];
+ } else
+ entry = NULL;
+
+ if (entry == NULL) {
+ if (mmio_rb_lookup(paddr, &entry))
+ return (ESRCH);
+
+ /* Update the per-vCPU cache */
+ mmio_hint[vcpu] = entry;
+ }
+
+ assert(entry != NULL && entry == mmio_hint[vcpu]);
+
+ err = vmm_emulate_instruction(ctx, vcpu, paddr, vie,
+ mem_read, mem_write, &entry->mr_param);
+ return (err);
+}
+
+int
+register_mem(struct mem_range *memp)
+{
+ struct mmio_rb_range *mrp;
+ int err;
+
+ err = 0;
+
+ mrp = malloc(sizeof(struct mmio_rb_range));
+
+ if (mrp != NULL) {
+ mrp->mr_param = *memp;
+ mrp->mr_base = memp->base;
+ mrp->mr_end = memp->base + memp->size - 1;
+
+ err = mmio_rb_add(mrp);
+ if (err)
+ free(mrp);
+ } else
+ err = ENOMEM;
+
+ return (err);
+}
+
+void
+init_mem(void)
+{
+
+ RB_INIT(&mmio_rbroot);
+}
diff --git a/usr.sbin/bhyve/mem.h b/usr.sbin/bhyve/mem.h
new file mode 100644
index 0000000..88fafe1
--- /dev/null
+++ b/usr.sbin/bhyve/mem.h
@@ -0,0 +1,57 @@
+/*-
+ * Copyright (c) 2012 NetApp, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ */
+
+#ifndef _MEM_H_
+#define _MEM_H_
+
+#include <sys/linker_set.h>
+
+struct vmctx;
+
+typedef int (*mem_func_t)(struct vmctx *ctx, int vcpu, int dir, uint64_t addr,
+ int size, uint64_t *val, void *arg1, long arg2);
+
+struct mem_range {
+ const char *name;
+ int flags;
+ mem_func_t handler;
+ void *arg1;
+ long arg2;
+ uint64_t base;
+ uint64_t size;
+};
+#define MEM_F_READ 0x1
+#define MEM_F_WRITE 0x2
+#define MEM_F_RW 0x3
+
+void init_mem(void);
+int emulate_mem(struct vmctx *, int vcpu, uint64_t paddr, struct vie *vie);
+
+int register_mem(struct mem_range *memp);
+
+#endif /* _MEM_H_ */
diff --git a/usr.sbin/bhyve/mevent.c b/usr.sbin/bhyve/mevent.c
new file mode 100644
index 0000000..a6109db
--- /dev/null
+++ b/usr.sbin/bhyve/mevent.c
@@ -0,0 +1,432 @@
+/*-
+ * Copyright (c) 2011 NetApp, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ */
+
+/*
+ * Micro event library for FreeBSD, designed for a single i/o thread
+ * using kqueue, and having events be persistent by default.
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <assert.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <sys/types.h>
+#include <sys/event.h>
+#include <sys/time.h>
+
+#include <pthread.h>
+#include <pthread_np.h>
+
+#include "mevent.h"
+
+#define MEVENT_MAX 64
+
+#define MEV_ENABLE 1
+#define MEV_DISABLE 2
+#define MEV_DEL_PENDING 3
+
+extern char *vmname;
+
+static pthread_t mevent_tid;
+static int mevent_pipefd[2];
+static pthread_mutex_t mevent_lmutex = PTHREAD_MUTEX_INITIALIZER;
+
+struct mevent {
+ void (*me_func)(int, enum ev_type, void *);
+ int me_fd;
+ enum ev_type me_type;
+ void *me_param;
+ int me_cq;
+ int me_state;
+ int me_closefd;
+ LIST_ENTRY(mevent) me_list;
+};
+
+static LIST_HEAD(listhead, mevent) global_head, change_head;
+
+static void
+mevent_qlock(void)
+{
+ pthread_mutex_lock(&mevent_lmutex);
+}
+
+static void
+mevent_qunlock(void)
+{
+ pthread_mutex_unlock(&mevent_lmutex);
+}
+
+static void
+mevent_pipe_read(int fd, enum ev_type type, void *param)
+{
+ char buf[MEVENT_MAX];
+ int status;
+
+ /*
+ * Drain the pipe read side. The fd is non-blocking so this is
+ * safe to do.
+ */
+ do {
+ status = read(fd, buf, sizeof(buf));
+ } while (status == MEVENT_MAX);
+}
+
+static void
+mevent_notify(void)
+{
+ char c;
+
+ /*
+ * If calling from outside the i/o thread, write a byte on the
+ * pipe to force the i/o thread to exit the blocking kevent call.
+ */
+ if (mevent_pipefd[1] != 0 && pthread_self() != mevent_tid) {
+ write(mevent_pipefd[1], &c, 1);
+ }
+}
+
+static int
+mevent_kq_filter(struct mevent *mevp)
+{
+ int retval;
+
+ retval = 0;
+
+ if (mevp->me_type == EVF_READ)
+ retval = EVFILT_READ;
+
+ if (mevp->me_type == EVF_WRITE)
+ retval = EVFILT_WRITE;
+
+ return (retval);
+}
+
+static int
+mevent_kq_flags(struct mevent *mevp)
+{
+ int ret;
+
+ switch (mevp->me_state) {
+ case MEV_ENABLE:
+ ret = EV_ADD;
+ break;
+ case MEV_DISABLE:
+ ret = EV_DISABLE;
+ break;
+ case MEV_DEL_PENDING:
+ ret = EV_DELETE;
+ break;
+ }
+
+ return (ret);
+}
+
+static int
+mevent_kq_fflags(struct mevent *mevp)
+{
+ /* XXX nothing yet, perhaps EV_EOF for reads ? */
+ return (0);
+}
+
+static int
+mevent_build(int mfd, struct kevent *kev)
+{
+ struct mevent *mevp, *tmpp;
+ int i;
+
+ i = 0;
+
+ mevent_qlock();
+
+ LIST_FOREACH_SAFE(mevp, &change_head, me_list, tmpp) {
+ if (mevp->me_closefd) {
+ /*
+ * A close of the file descriptor will remove the
+ * event
+ */
+ close(mevp->me_fd);
+ } else {
+ kev[i].ident = mevp->me_fd;
+ kev[i].filter = mevent_kq_filter(mevp);
+ kev[i].flags = mevent_kq_flags(mevp);
+ kev[i].fflags = mevent_kq_fflags(mevp);
+ kev[i].data = 0;
+ kev[i].udata = mevp;
+ i++;
+ }
+
+ mevp->me_cq = 0;
+ LIST_REMOVE(mevp, me_list);
+
+ if (mevp->me_state == MEV_DEL_PENDING) {
+ free(mevp);
+ } else {
+ LIST_INSERT_HEAD(&global_head, mevp, me_list);
+ }
+
+ assert(i < MEVENT_MAX);
+ }
+
+ mevent_qunlock();
+
+ return (i);
+}
+
+static void
+mevent_handle(struct kevent *kev, int numev)
+{
+ struct mevent *mevp;
+ int i;
+
+ for (i = 0; i < numev; i++) {
+ mevp = kev[i].udata;
+
+ /* XXX check for EV_ERROR ? */
+
+ (*mevp->me_func)(mevp->me_fd, mevp->me_type, mevp->me_param);
+ }
+}
+
+struct mevent *
+mevent_add(int fd, enum ev_type type,
+ void (*func)(int, enum ev_type, void *), void *param)
+{
+ struct mevent *lp, *mevp;
+
+ if (fd < 0 || func == NULL) {
+ return (NULL);
+ }
+
+ mevp = NULL;
+
+ mevent_qlock();
+
+ /*
+ * Verify that the fd/type tuple is not present in any list
+ */
+ LIST_FOREACH(lp, &global_head, me_list) {
+ if (lp->me_fd == fd && lp->me_type == type) {
+ goto exit;
+ }
+ }
+
+ LIST_FOREACH(lp, &change_head, me_list) {
+ if (lp->me_fd == fd && lp->me_type == type) {
+ goto exit;
+ }
+ }
+
+ /*
+ * Allocate an entry, populate it, and add it to the change list.
+ */
+ mevp = malloc(sizeof(struct mevent));
+ if (mevp == NULL) {
+ goto exit;
+ }
+
+ memset(mevp, 0, sizeof(struct mevent));
+ mevp->me_fd = fd;
+ mevp->me_type = type;
+ mevp->me_func = func;
+ mevp->me_param = param;
+
+ LIST_INSERT_HEAD(&change_head, mevp, me_list);
+ mevp->me_cq = 1;
+ mevp->me_state = MEV_ENABLE;
+ mevent_notify();
+
+exit:
+ mevent_qunlock();
+
+ return (mevp);
+}
+
+static int
+mevent_update(struct mevent *evp, int newstate)
+{
+ /*
+ * It's not possible to enable/disable a deleted event
+ */
+ if (evp->me_state == MEV_DEL_PENDING)
+ return (EINVAL);
+
+ /*
+ * No update needed if state isn't changing
+ */
+ if (evp->me_state == newstate)
+ return (0);
+
+ mevent_qlock();
+
+ evp->me_state = newstate;
+
+ /*
+ * Place the entry onto the changed list if not already there.
+ */
+ if (evp->me_cq == 0) {
+ evp->me_cq = 1;
+ LIST_REMOVE(evp, me_list);
+ LIST_INSERT_HEAD(&change_head, evp, me_list);
+ mevent_notify();
+ }
+
+ mevent_qunlock();
+
+ return (0);
+}
+
+int
+mevent_enable(struct mevent *evp)
+{
+
+ return (mevent_update(evp, MEV_ENABLE));
+}
+
+int
+mevent_disable(struct mevent *evp)
+{
+
+ return (mevent_update(evp, MEV_DISABLE));
+}
+
+static int
+mevent_delete_event(struct mevent *evp, int closefd)
+{
+ mevent_qlock();
+
+ /*
+ * Place the entry onto the changed list if not already there, and
+ * mark as to be deleted.
+ */
+ if (evp->me_cq == 0) {
+ evp->me_cq = 1;
+ LIST_REMOVE(evp, me_list);
+ LIST_INSERT_HEAD(&change_head, evp, me_list);
+ mevent_notify();
+ }
+ evp->me_state = MEV_DEL_PENDING;
+
+ if (closefd)
+ evp->me_closefd = 1;
+
+ mevent_qunlock();
+
+ return (0);
+}
+
+int
+mevent_delete(struct mevent *evp)
+{
+
+ return (mevent_delete_event(evp, 0));
+}
+
+int
+mevent_delete_close(struct mevent *evp)
+{
+
+ return (mevent_delete_event(evp, 1));
+}
+
+static void
+mevent_set_name(void)
+{
+ char tname[MAXCOMLEN + 1];
+
+ snprintf(tname, sizeof(tname), "%s mevent", vmname);
+ pthread_set_name_np(mevent_tid, tname);
+}
+
+void
+mevent_dispatch(void)
+{
+ struct kevent changelist[MEVENT_MAX];
+ struct kevent eventlist[MEVENT_MAX];
+ struct mevent *pipev;
+ int mfd;
+ int numev;
+ int ret;
+
+ mevent_tid = pthread_self();
+ mevent_set_name();
+
+ mfd = kqueue();
+ assert(mfd > 0);
+
+ /*
+ * Open the pipe that will be used for other threads to force
+ * the blocking kqueue call to exit by writing to it. Set the
+ * descriptor to non-blocking.
+ */
+ ret = pipe(mevent_pipefd);
+ if (ret < 0) {
+ perror("pipe");
+ exit(0);
+ }
+
+ /*
+ * Add internal event handler for the pipe write fd
+ */
+ pipev = mevent_add(mevent_pipefd[0], EVF_READ, mevent_pipe_read, NULL);
+ assert(pipev != NULL);
+
+ for (;;) {
+ /*
+ * Build changelist if required.
+ * XXX the changelist can be put into the blocking call
+ * to eliminate the extra syscall. Currently better for
+ * debug.
+ */
+ numev = mevent_build(mfd, changelist);
+ if (numev) {
+ ret = kevent(mfd, changelist, numev, NULL, 0, NULL);
+ if (ret == -1) {
+ perror("Error return from kevent change");
+ }
+ }
+
+ /*
+ * Block awaiting events
+ */
+ ret = kevent(mfd, NULL, 0, eventlist, MEVENT_MAX, NULL);
+ if (ret == -1) {
+ perror("Error return from kevent monitor");
+ }
+
+ /*
+ * Handle reported events
+ */
+ mevent_handle(eventlist, ret);
+ }
+}
diff --git a/usr.sbin/bhyve/mevent.h b/usr.sbin/bhyve/mevent.h
new file mode 100644
index 0000000..32a9d74
--- /dev/null
+++ b/usr.sbin/bhyve/mevent.h
@@ -0,0 +1,49 @@
+/*-
+ * Copyright (c) 2011 NetApp, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ */
+
+#ifndef _MEVENT_H_
+#define _MEVENT_H_
+
+enum ev_type {
+ EVF_READ,
+ EVF_WRITE
+};
+
+struct mevent;
+
+struct mevent *mevent_add(int fd, enum ev_type type,
+ void (*func)(int, enum ev_type, void *),
+ void *param);
+int mevent_enable(struct mevent *evp);
+int mevent_disable(struct mevent *evp);
+int mevent_delete(struct mevent *evp);
+int mevent_delete_close(struct mevent *evp);
+
+void mevent_dispatch(void);
+
+#endif /* _MEVENT_H_ */
diff --git a/usr.sbin/bhyve/mevent_test.c b/usr.sbin/bhyve/mevent_test.c
new file mode 100644
index 0000000..c72a497
--- /dev/null
+++ b/usr.sbin/bhyve/mevent_test.c
@@ -0,0 +1,180 @@
+/*-
+ * Copyright (c) 2011 NetApp, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ */
+
+/*
+ * Test program for the micro event library. Set up a simple TCP echo
+ * service.
+ *
+ * cc mevent_test.c mevent.c -lpthread
+ */
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <pthread.h>
+
+#include "mevent.h"
+
+#define TEST_PORT 4321
+
+static pthread_mutex_t accept_mutex = PTHREAD_MUTEX_INITIALIZER;
+static pthread_cond_t accept_condvar = PTHREAD_COND_INITIALIZER;
+
+#define MEVENT_ECHO
+
+#ifdef MEVENT_ECHO
+struct esync {
+ pthread_mutex_t e_mt;
+ pthread_cond_t e_cond;
+};
+
+static void
+echoer_callback(int fd, enum ev_type type, void *param)
+{
+ struct esync *sync = param;
+
+ pthread_mutex_lock(&sync->e_mt);
+ pthread_cond_signal(&sync->e_cond);
+ pthread_mutex_unlock(&sync->e_mt);
+}
+
+static void *
+echoer(void *param)
+{
+ struct esync sync;
+ struct mevent *mev;
+ char buf[128];
+ int fd = (int)(uintptr_t) param;
+ int len;
+
+ pthread_mutex_init(&sync.e_mt, NULL);
+ pthread_cond_init(&sync.e_cond, NULL);
+
+ pthread_mutex_lock(&sync.e_mt);
+
+ mev = mevent_add(fd, EVF_READ, echoer_callback, &sync);
+ if (mev == NULL) {
+ printf("Could not allocate echoer event\n");
+ exit(1);
+ }
+
+ while (!pthread_cond_wait(&sync.e_cond, &sync.e_mt)) {
+ len = read(fd, buf, sizeof(buf));
+ if (len > 0) {
+ write(fd, buf, len);
+ write(0, buf, len);
+ } else {
+ break;
+ }
+ }
+
+ mevent_delete_close(mev);
+
+ pthread_mutex_unlock(&sync.e_mt);
+ pthread_mutex_destroy(&sync.e_mt);
+ pthread_cond_destroy(&sync.e_cond);
+}
+
+#else
+
+static void *
+echoer(void *param)
+{
+ char buf[128];
+ int fd = (int)(uintptr_t) param;
+ int len;
+
+ while ((len = read(fd, buf, sizeof(buf))) > 0) {
+ write(1, buf, len);
+ }
+}
+#endif /* MEVENT_ECHO */
+
+static void
+acceptor_callback(int fd, enum ev_type type, void *param)
+{
+ pthread_mutex_lock(&accept_mutex);
+ pthread_cond_signal(&accept_condvar);
+ pthread_mutex_unlock(&accept_mutex);
+}
+
+static void *
+acceptor(void *param)
+{
+ struct sockaddr_in sin;
+ pthread_t tid;
+ int news;
+ int s;
+
+ if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
+ perror("socket");
+ exit(1);
+ }
+
+ sin.sin_len = sizeof(sin);
+ sin.sin_family = AF_INET;
+ sin.sin_addr.s_addr = htonl(INADDR_ANY);
+ sin.sin_port = htons(TEST_PORT);
+
+ if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
+ perror("bind");
+ exit(1);
+ }
+
+ if (listen(s, 1) < 0) {
+ perror("listen");
+ exit(1);
+ }
+
+ (void) mevent_add(s, EVF_READ, acceptor_callback, NULL);
+
+ pthread_mutex_lock(&accept_mutex);
+
+ while (!pthread_cond_wait(&accept_condvar, &accept_mutex)) {
+ news = accept(s, NULL, NULL);
+ if (news < 0) {
+ perror("accept error");
+ } else {
+ printf("incoming connection, spawning thread\n");
+ pthread_create(&tid, NULL, echoer,
+ (void *)(uintptr_t)news);
+ }
+ }
+}
+
+main()
+{
+ pthread_t tid;
+
+ pthread_create(&tid, NULL, acceptor, NULL);
+
+ mevent_dispatch();
+}
diff --git a/usr.sbin/bhyve/mptbl.c b/usr.sbin/bhyve/mptbl.c
new file mode 100644
index 0000000..52790f3
--- /dev/null
+++ b/usr.sbin/bhyve/mptbl.c
@@ -0,0 +1,398 @@
+/*-
+ * Copyright (c) 2012 NetApp, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <sys/types.h>
+#include <sys/errno.h>
+#include <x86/mptable.h>
+
+#include <stdio.h>
+#include <string.h>
+
+#include "bhyverun.h"
+#include "mptbl.h"
+
+#define MPTABLE_BASE 0xF0000
+
+#define LAPIC_PADDR 0xFEE00000
+#define LAPIC_VERSION 16
+
+#define IOAPIC_PADDR 0xFEC00000
+#define IOAPIC_VERSION 0x11
+
+#define MP_SPECREV 4
+#define MPFP_SIG "_MP_"
+
+/* Configuration header defines */
+#define MPCH_SIG "PCMP"
+#define MPCH_OEMID "BHyVe "
+#define MPCH_OEMID_LEN 8
+#define MPCH_PRODID "Hypervisor "
+#define MPCH_PRODID_LEN 12
+
+/* Processor entry defines */
+#define MPEP_SIG_FAMILY 6 /* XXX bhyve should supply this */
+#define MPEP_SIG_MODEL 26
+#define MPEP_SIG_STEPPING 5
+#define MPEP_SIG \
+ ((MPEP_SIG_FAMILY << 8) | \
+ (MPEP_SIG_MODEL << 4) | \
+ (MPEP_SIG_STEPPING))
+
+#define MPEP_FEATURES (0xBFEBFBFF) /* XXX Intel i7 */
+
+/* Define processor entry struct since <x86/mptable.h> gets it wrong */
+typedef struct BPROCENTRY {
+ u_char type;
+ u_char apic_id;
+ u_char apic_version;
+ u_char cpu_flags;
+ uint32_t cpu_signature;
+ uint32_t feature_flags;
+ uint32_t reserved1;
+ uint32_t reserved2;
+} *bproc_entry_ptr;
+CTASSERT(sizeof(struct BPROCENTRY) == 20);
+
+/* Bus entry defines */
+#define MPE_NUM_BUSES 2
+#define MPE_BUSNAME_LEN 6
+#define MPE_BUSNAME_ISA "ISA "
+#define MPE_BUSNAME_PCI "PCI "
+
+static void *oem_tbl_start;
+static int oem_tbl_size;
+
+static uint8_t
+mpt_compute_checksum(void *base, size_t len)
+{
+ uint8_t *bytes;
+ uint8_t sum;
+
+ for(bytes = base, sum = 0; len > 0; len--) {
+ sum += *bytes++;
+ }
+
+ return (256 - sum);
+}
+
+static void
+mpt_build_mpfp(mpfps_t mpfp, vm_paddr_t gpa)
+{
+
+ memset(mpfp, 0, sizeof(*mpfp));
+ memcpy(mpfp->signature, MPFP_SIG, 4);
+ mpfp->pap = gpa + sizeof(*mpfp);
+ mpfp->length = 1;
+ mpfp->spec_rev = MP_SPECREV;
+ mpfp->checksum = mpt_compute_checksum(mpfp, sizeof(*mpfp));
+}
+
+static void
+mpt_build_mpch(mpcth_t mpch)
+{
+
+ memset(mpch, 0, sizeof(*mpch));
+ memcpy(mpch->signature, MPCH_SIG, 4);
+ mpch->spec_rev = MP_SPECREV;
+ memcpy(mpch->oem_id, MPCH_OEMID, MPCH_OEMID_LEN);
+ memcpy(mpch->product_id, MPCH_PRODID, MPCH_PRODID_LEN);
+ mpch->apic_address = LAPIC_PADDR;
+}
+
+static void
+mpt_build_proc_entries(bproc_entry_ptr mpep, int ncpu)
+{
+ int i;
+
+ for (i = 0; i < ncpu; i++) {
+ memset(mpep, 0, sizeof(*mpep));
+ mpep->type = MPCT_ENTRY_PROCESSOR;
+ mpep->apic_id = i; // XXX
+ mpep->apic_version = LAPIC_VERSION;
+ mpep->cpu_flags = PROCENTRY_FLAG_EN;
+ if (i == 0)
+ mpep->cpu_flags |= PROCENTRY_FLAG_BP;
+ mpep->cpu_signature = MPEP_SIG;
+ mpep->feature_flags = MPEP_FEATURES;
+ mpep++;
+ }
+}
+
+static void
+mpt_build_bus_entries(bus_entry_ptr mpeb)
+{
+
+ memset(mpeb, 0, sizeof(*mpeb));
+ mpeb->type = MPCT_ENTRY_BUS;
+ mpeb->bus_id = ISA;
+ memcpy(mpeb->bus_type, MPE_BUSNAME_ISA, MPE_BUSNAME_LEN);
+ mpeb++;
+
+ memset(mpeb, 0, sizeof(*mpeb));
+ mpeb->type = MPCT_ENTRY_BUS;
+ mpeb->bus_id = PCI;
+ memcpy(mpeb->bus_type, MPE_BUSNAME_PCI, MPE_BUSNAME_LEN);
+}
+
+static void
+mpt_build_ioapic_entries(io_apic_entry_ptr mpei, int id)
+{
+
+ memset(mpei, 0, sizeof(*mpei));
+ mpei->type = MPCT_ENTRY_IOAPIC;
+ mpei->apic_id = id;
+ mpei->apic_version = IOAPIC_VERSION;
+ mpei->apic_flags = IOAPICENTRY_FLAG_EN;
+ mpei->apic_address = IOAPIC_PADDR;
+}
+
+#ifdef notyet
+static void
+mpt_build_ioint_entries(struct mpe_ioint *mpeii, int num_pins, int id)
+{
+ int pin;
+
+ /*
+ * The following config is taken from kernel mptable.c
+ * mptable_parse_default_config_ints(...), for now
+ * just use the default config, tweek later if needed.
+ */
+
+
+ /* Run through all 16 pins. */
+ for (pin = 0; pin < num_pins; pin++) {
+ memset(mpeii, 0, sizeof(*mpeii));
+ mpeii->entry_type = MP_ENTRY_IOINT;
+ mpeii->src_bus_id = MPE_BUSID_ISA;
+ mpeii->dst_apic_id = id;
+
+ /*
+ * All default configs route IRQs from bus 0 to the first 16
+ * pins of the first I/O APIC with an APIC ID of 2.
+ */
+ mpeii->dst_apic_intin = pin;
+ switch (pin) {
+ case 0:
+ /* Pin 0 is an ExtINT pin. */
+ mpeii->intr_type = MPEII_INTR_EXTINT;
+ break;
+ case 2:
+ /* IRQ 0 is routed to pin 2. */
+ mpeii->intr_type = MPEII_INTR_INT;
+ mpeii->src_bus_irq = 0;
+ break;
+ case 5:
+ case 10:
+ case 11:
+ /*
+ * PCI Irqs set to level triggered.
+ */
+ mpeii->intr_flags = MPEII_FLAGS_TRIGMODE_LEVEL;
+ mpeii->src_bus_id = MPE_BUSID_PCI;
+ default:
+ /* All other pins are identity mapped. */
+ mpeii->intr_type = MPEII_INTR_INT;
+ mpeii->src_bus_irq = pin;
+ break;
+ }
+ mpeii++;
+ }
+
+}
+
+#define COPYSTR(dest, src, bytes) \
+ memcpy(dest, src, bytes); \
+ str[bytes] = 0;
+
+static void
+mptable_dump(struct mp_floating_pointer *mpfp, struct mp_config_hdr *mpch)
+{
+ static char str[16];
+ int i;
+ char *cur;
+
+ union mpe {
+ struct mpe_proc *proc;
+ struct mpe_bus *bus;
+ struct mpe_ioapic *ioapic;
+ struct mpe_ioint *ioint;
+ struct mpe_lint *lnit;
+ char *p;
+ };
+
+ union mpe mpe;
+
+ printf(" MP Floating Pointer :\n");
+ COPYSTR(str, mpfp->signature, 4);
+ printf("\tsignature:\t%s\n", str);
+ printf("\tmpch paddr:\t%x\n", mpfp->mptable_paddr);
+ printf("\tlength:\t%x\n", mpfp->length);
+ printf("\tspecrec:\t%x\n", mpfp->specrev);
+ printf("\tchecksum:\t%x\n", mpfp->checksum);
+ printf("\tfeature1:\t%x\n", mpfp->feature1);
+ printf("\tfeature2:\t%x\n", mpfp->feature2);
+ printf("\tfeature3:\t%x\n", mpfp->feature3);
+ printf("\tfeature4:\t%x\n", mpfp->feature4);
+
+ printf(" MP Configuration Header :\n");
+ COPYSTR(str, mpch->signature, 4);
+ printf(" signature: %s\n", str);
+ printf(" length: %x\n", mpch->length);
+ printf(" specrec: %x\n", mpch->specrev);
+ printf(" checksum: %x\n", mpch->checksum);
+ COPYSTR(str, mpch->oemid, MPCH_OEMID_LEN);
+ printf(" oemid: %s\n", str);
+ COPYSTR(str, mpch->prodid, MPCH_PRODID_LEN);
+ printf(" prodid: %s\n", str);
+ printf(" oem_ptr: %x\n", mpch->oem_ptr);
+ printf(" oem_sz: %x\n", mpch->oem_sz);
+ printf(" nr_entries: %x\n", mpch->nr_entries);
+ printf(" apic paddr: %x\n", mpch->lapic_paddr);
+ printf(" ext_length: %x\n", mpch->ext_length);
+ printf(" ext_checksum: %x\n", mpch->ext_checksum);
+
+ cur = (char *)mpch + sizeof(*mpch);
+ for (i = 0; i < mpch->nr_entries; i++) {
+ mpe.p = cur;
+ switch(*mpe.p) {
+ case MP_ENTRY_PROC:
+ printf(" MP Processor Entry :\n");
+ printf(" lapic_id: %x\n", mpe.proc->lapic_id);
+ printf(" lapic_version: %x\n", mpe.proc->lapic_version);
+ printf(" proc_flags: %x\n", mpe.proc->proc_flags);
+ printf(" proc_signature: %x\n", mpe.proc->proc_signature);
+ printf(" feature_flags: %x\n", mpe.proc->feature_flags);
+ cur += sizeof(struct mpe_proc);
+ break;
+ case MP_ENTRY_BUS:
+ printf(" MP Bus Entry :\n");
+ printf(" busid: %x\n", mpe.bus->busid);
+ COPYSTR(str, mpe.bus->busname, MPE_BUSNAME_LEN);
+ printf(" busname: %s\n", str);
+ cur += sizeof(struct mpe_bus);
+ break;
+ case MP_ENTRY_IOAPIC:
+ printf(" MP IOAPIC Entry :\n");
+ printf(" ioapi_id: %x\n", mpe.ioapic->ioapic_id);
+ printf(" ioapi_version: %x\n", mpe.ioapic->ioapic_version);
+ printf(" ioapi_flags: %x\n", mpe.ioapic->ioapic_flags);
+ printf(" ioapi_paddr: %x\n", mpe.ioapic->ioapic_paddr);
+ cur += sizeof(struct mpe_ioapic);
+ break;
+ case MP_ENTRY_IOINT:
+ printf(" MP IO Interrupt Entry :\n");
+ printf(" intr_type: %x\n", mpe.ioint->intr_type);
+ printf(" intr_flags: %x\n", mpe.ioint->intr_flags);
+ printf(" src_bus_id: %x\n", mpe.ioint->src_bus_id);
+ printf(" src_bus_irq: %x\n", mpe.ioint->src_bus_irq);
+ printf(" dst_apic_id: %x\n", mpe.ioint->dst_apic_id);
+ printf(" dst_apic_intin: %x\n", mpe.ioint->dst_apic_intin);
+ cur += sizeof(struct mpe_ioint);
+ break;
+ case MP_ENTRY_LINT:
+ printf(" MP Local Interrupt Entry :\n");
+ cur += sizeof(struct mpe_lint);
+ break;
+ }
+
+ }
+}
+#endif
+
+void
+mptable_add_oemtbl(void *tbl, int tblsz)
+{
+
+ oem_tbl_start = tbl;
+ oem_tbl_size = tblsz;
+}
+
+int
+mptable_build(struct vmctx *ctx, int ncpu, int ioapic)
+{
+ mpcth_t mpch;
+ bus_entry_ptr mpeb;
+ io_apic_entry_ptr mpei;
+ bproc_entry_ptr mpep;
+ mpfps_t mpfp;
+ char *curraddr;
+ char *startaddr;
+
+ if (paddr_guest2host(0) == NULL) {
+ printf("mptable requires mapped mem\n");
+ return (ENOMEM);
+ }
+
+ startaddr = curraddr = paddr_guest2host(MPTABLE_BASE);
+
+ mpfp = (mpfps_t)curraddr;
+ mpt_build_mpfp(mpfp, MPTABLE_BASE);
+ curraddr += sizeof(*mpfp);
+
+ mpch = (mpcth_t)curraddr;
+ mpt_build_mpch(mpch);
+ curraddr += sizeof(*mpch);
+
+ mpep = (bproc_entry_ptr)curraddr;
+ mpt_build_proc_entries(mpep, ncpu);
+ curraddr += sizeof(*mpep) * ncpu;
+ mpch->entry_count += ncpu;
+
+ mpeb = (bus_entry_ptr) curraddr;
+ mpt_build_bus_entries(mpeb);
+ curraddr += sizeof(*mpeb) * MPE_NUM_BUSES;
+ mpch->entry_count += MPE_NUM_BUSES;
+
+ if (ioapic) {
+ mpei = (io_apic_entry_ptr)curraddr;
+ mpt_build_ioapic_entries(mpei, ncpu + 1);
+ curraddr += sizeof(*mpei);
+ mpch->entry_count++;
+ }
+
+#ifdef notyet
+ mpt_build_ioint_entries((struct mpe_ioint*)curraddr, MPEII_MAX_IRQ,
+ ncpu + 1);
+ curraddr += sizeof(struct mpe_ioint) * MPEII_MAX_IRQ;
+ mpch->entry_count += MPEII_MAX_IRQ;
+#endif
+
+ if (oem_tbl_start) {
+ mpch->oem_table_pointer = curraddr - startaddr + MPTABLE_BASE;
+ mpch->oem_table_size = oem_tbl_size;
+ memcpy(curraddr, oem_tbl_start, oem_tbl_size);
+ }
+
+ mpch->base_table_length = curraddr - (char *)mpch;
+ mpch->checksum = mpt_compute_checksum(mpch, sizeof(*mpch));
+
+ return (0);
+}
diff --git a/usr.sbin/bhyve/mptbl.h b/usr.sbin/bhyve/mptbl.h
new file mode 100644
index 0000000..3c4c527
--- /dev/null
+++ b/usr.sbin/bhyve/mptbl.h
@@ -0,0 +1,35 @@
+/*-
+ * Copyright (c) 2012 NetApp, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ */
+
+#ifndef _MPTBL_H_
+#define _MPTBL_H_
+
+int mptable_build(struct vmctx *ctx, int ncpu, int ioapic);
+void mptable_add_oemtbl(void *tbl, int tblsz);
+
+#endif /* _MPTBL_H_ */
diff --git a/usr.sbin/bhyve/pci_emul.c b/usr.sbin/bhyve/pci_emul.c
new file mode 100644
index 0000000..32a3deb
--- /dev/null
+++ b/usr.sbin/bhyve/pci_emul.c
@@ -0,0 +1,1339 @@
+/*-
+ * Copyright (c) 2011 NetApp, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <sys/param.h>
+#include <sys/linker_set.h>
+
+#include <ctype.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <strings.h>
+#include <assert.h>
+
+#include <machine/vmm.h>
+#include <vmmapi.h>
+
+#include "bhyverun.h"
+#include "inout.h"
+#include "mem.h"
+#include "mptbl.h"
+#include "pci_emul.h"
+#include "ioapic.h"
+
+#define CONF1_ADDR_PORT 0x0cf8
+#define CONF1_DATA_PORT 0x0cfc
+
+#define CFGWRITE(pi,off,val,b) \
+do { \
+ if ((b) == 1) { \
+ pci_set_cfgdata8((pi),(off),(val)); \
+ } else if ((b) == 2) { \
+ pci_set_cfgdata16((pi),(off),(val)); \
+ } else { \
+ pci_set_cfgdata32((pi),(off),(val)); \
+ } \
+} while (0)
+
+#define MAXSLOTS (PCI_SLOTMAX + 1)
+#define MAXFUNCS (PCI_FUNCMAX + 1)
+
+static struct slotinfo {
+ char *si_name;
+ char *si_param;
+ struct pci_devinst *si_devi;
+ int si_legacy;
+} pci_slotinfo[MAXSLOTS][MAXFUNCS];
+
+/*
+ * Used to keep track of legacy interrupt owners/requestors
+ */
+#define NLIRQ 16
+
+static struct lirqinfo {
+ int li_generic;
+ int li_acount;
+ struct pci_devinst *li_owner; /* XXX should be a list */
+} lirq[NLIRQ];
+
+SET_DECLARE(pci_devemu_set, struct pci_devemu);
+
+static uint64_t pci_emul_iobase;
+static uint64_t pci_emul_membase32;
+static uint64_t pci_emul_membase64;
+
+#define PCI_EMUL_IOBASE 0x2000
+#define PCI_EMUL_IOLIMIT 0x10000
+
+#define PCI_EMUL_MEMBASE32 (lomem_sz)
+#define PCI_EMUL_MEMLIMIT32 0xE0000000 /* 3.5GB */
+
+#define PCI_EMUL_MEMBASE64 0xD000000000UL
+#define PCI_EMUL_MEMLIMIT64 0xFD00000000UL
+
+static int pci_emul_devices;
+
+/*
+ * I/O access
+ */
+
+/*
+ * Slot options are in the form:
+ *
+ * <slot>[:<func>],<emul>[,<config>]
+ *
+ * slot is 0..31
+ * func is 0..7
+ * emul is a string describing the type of PCI device e.g. virtio-net
+ * config is an optional string, depending on the device, that can be
+ * used for configuration.
+ * Examples are:
+ * 1,virtio-net,tap0
+ * 3:0,dummy
+ */
+static void
+pci_parse_slot_usage(char *aopt)
+{
+ printf("Invalid PCI slot info field \"%s\"\n", aopt);
+ free(aopt);
+}
+
+void
+pci_parse_slot(char *opt, int legacy)
+{
+ char *slot, *func, *emul, *config;
+ char *str, *cpy;
+ int snum, fnum;
+
+ str = cpy = strdup(opt);
+
+ config = NULL;
+
+ if (strchr(str, ':') != NULL) {
+ slot = strsep(&str, ":");
+ func = strsep(&str, ",");
+ } else {
+ slot = strsep(&str, ",");
+ func = NULL;
+ }
+
+ emul = strsep(&str, ",");
+ if (str != NULL) {
+ config = strsep(&str, ",");
+ }
+
+ if (emul == NULL) {
+ pci_parse_slot_usage(cpy);
+ return;
+ }
+
+ snum = atoi(slot);
+ fnum = func ? atoi(func) : 0;
+ if (snum < 0 || snum >= MAXSLOTS || fnum < 0 || fnum >= MAXFUNCS) {
+ pci_parse_slot_usage(cpy);
+ } else {
+ pci_slotinfo[snum][fnum].si_name = emul;
+ pci_slotinfo[snum][fnum].si_param = config;
+ pci_slotinfo[snum][fnum].si_legacy = legacy;
+ }
+}
+
+static int
+pci_valid_pba_offset(struct pci_devinst *pi, uint64_t offset)
+{
+
+ if (offset < pi->pi_msix.pba_offset)
+ return (0);
+
+ if (offset >= pi->pi_msix.pba_offset + pi->pi_msix.pba_size) {
+ return (0);
+ }
+
+ return (1);
+}
+
+int
+pci_emul_msix_twrite(struct pci_devinst *pi, uint64_t offset, int size,
+ uint64_t value)
+{
+ int msix_entry_offset;
+ int tab_index;
+ char *dest;
+
+ /* support only 4 or 8 byte writes */
+ if (size != 4 && size != 8)
+ return (-1);
+
+ /*
+ * Return if table index is beyond what device supports
+ */
+ tab_index = offset / MSIX_TABLE_ENTRY_SIZE;
+ if (tab_index >= pi->pi_msix.table_count)
+ return (-1);
+
+ msix_entry_offset = offset % MSIX_TABLE_ENTRY_SIZE;
+
+ /* support only aligned writes */
+ if ((msix_entry_offset % size) != 0)
+ return (-1);
+
+ dest = (char *)(pi->pi_msix.table + tab_index);
+ dest += msix_entry_offset;
+
+ if (size == 4)
+ *((uint32_t *)dest) = value;
+ else
+ *((uint64_t *)dest) = value;
+
+ return (0);
+}
+
+uint64_t
+pci_emul_msix_tread(struct pci_devinst *pi, uint64_t offset, int size)
+{
+ char *dest;
+ int msix_entry_offset;
+ int tab_index;
+ uint64_t retval = ~0;
+
+ /* support only 4 or 8 byte reads */
+ if (size != 4 && size != 8)
+ return (retval);
+
+ msix_entry_offset = offset % MSIX_TABLE_ENTRY_SIZE;
+
+ /* support only aligned reads */
+ if ((msix_entry_offset % size) != 0) {
+ return (retval);
+ }
+
+ tab_index = offset / MSIX_TABLE_ENTRY_SIZE;
+
+ if (tab_index < pi->pi_msix.table_count) {
+ /* valid MSI-X Table access */
+ dest = (char *)(pi->pi_msix.table + tab_index);
+ dest += msix_entry_offset;
+
+ if (size == 4)
+ retval = *((uint32_t *)dest);
+ else
+ retval = *((uint64_t *)dest);
+ } else if (pci_valid_pba_offset(pi, offset)) {
+ /* return 0 for PBA access */
+ retval = 0;
+ }
+
+ return (retval);
+}
+
+int
+pci_msix_table_bar(struct pci_devinst *pi)
+{
+
+ if (pi->pi_msix.table != NULL)
+ return (pi->pi_msix.table_bar);
+ else
+ return (-1);
+}
+
+int
+pci_msix_pba_bar(struct pci_devinst *pi)
+{
+
+ if (pi->pi_msix.table != NULL)
+ return (pi->pi_msix.pba_bar);
+ else
+ return (-1);
+}
+
+static int
+pci_emul_io_handler(struct vmctx *ctx, int vcpu, int in, int port, int bytes,
+ uint32_t *eax, void *arg)
+{
+ struct pci_devinst *pdi = arg;
+ struct pci_devemu *pe = pdi->pi_d;
+ uint64_t offset;
+ int i;
+
+ for (i = 0; i <= PCI_BARMAX; i++) {
+ if (pdi->pi_bar[i].type == PCIBAR_IO &&
+ port >= pdi->pi_bar[i].addr &&
+ port + bytes <= pdi->pi_bar[i].addr + pdi->pi_bar[i].size) {
+ offset = port - pdi->pi_bar[i].addr;
+ if (in)
+ *eax = (*pe->pe_barread)(ctx, vcpu, pdi, i,
+ offset, bytes);
+ else
+ (*pe->pe_barwrite)(ctx, vcpu, pdi, i, offset,
+ bytes, *eax);
+ return (0);
+ }
+ }
+ return (-1);
+}
+
+static int
+pci_emul_mem_handler(struct vmctx *ctx, int vcpu, int dir, uint64_t addr,
+ int size, uint64_t *val, void *arg1, long arg2)
+{
+ struct pci_devinst *pdi = arg1;
+ struct pci_devemu *pe = pdi->pi_d;
+ uint64_t offset;
+ int bidx = (int) arg2;
+
+ assert(bidx <= PCI_BARMAX);
+ assert(pdi->pi_bar[bidx].type == PCIBAR_MEM32 ||
+ pdi->pi_bar[bidx].type == PCIBAR_MEM64);
+ assert(addr >= pdi->pi_bar[bidx].addr &&
+ addr + size <= pdi->pi_bar[bidx].addr + pdi->pi_bar[bidx].size);
+
+ offset = addr - pdi->pi_bar[bidx].addr;
+
+ if (dir == MEM_F_WRITE)
+ (*pe->pe_barwrite)(ctx, vcpu, pdi, bidx, offset, size, *val);
+ else
+ *val = (*pe->pe_barread)(ctx, vcpu, pdi, bidx, offset, size);
+
+ return (0);
+}
+
+
+static int
+pci_emul_alloc_resource(uint64_t *baseptr, uint64_t limit, uint64_t size,
+ uint64_t *addr)
+{
+ uint64_t base;
+
+ assert((size & (size - 1)) == 0); /* must be a power of 2 */
+
+ base = roundup2(*baseptr, size);
+
+ if (base + size <= limit) {
+ *addr = base;
+ *baseptr = base + size;
+ return (0);
+ } else
+ return (-1);
+}
+
+int
+pci_emul_alloc_bar(struct pci_devinst *pdi, int idx, enum pcibar_type type,
+ uint64_t size)
+{
+
+ return (pci_emul_alloc_pbar(pdi, idx, 0, type, size));
+}
+
+int
+pci_emul_alloc_pbar(struct pci_devinst *pdi, int idx, uint64_t hostbase,
+ enum pcibar_type type, uint64_t size)
+{
+ int i, error;
+ uint64_t *baseptr, limit, addr, mask, lobits, bar;
+ struct inout_port iop;
+ struct mem_range memp;
+
+ assert(idx >= 0 && idx <= PCI_BARMAX);
+
+ if ((size & (size - 1)) != 0)
+ size = 1UL << flsl(size); /* round up to a power of 2 */
+
+ switch (type) {
+ case PCIBAR_NONE:
+ baseptr = NULL;
+ addr = mask = lobits = 0;
+ break;
+ case PCIBAR_IO:
+ if (hostbase &&
+ pci_slotinfo[pdi->pi_slot][pdi->pi_func].si_legacy) {
+ assert(hostbase < PCI_EMUL_IOBASE);
+ baseptr = &hostbase;
+ } else {
+ baseptr = &pci_emul_iobase;
+ }
+ limit = PCI_EMUL_IOLIMIT;
+ mask = PCIM_BAR_IO_BASE;
+ lobits = PCIM_BAR_IO_SPACE;
+ break;
+ case PCIBAR_MEM64:
+ /*
+ * XXX
+ * Some drivers do not work well if the 64-bit BAR is allocated
+ * above 4GB. Allow for this by allocating small requests under
+ * 4GB unless then allocation size is larger than some arbitrary
+ * number (32MB currently).
+ */
+ if (size > 32 * 1024 * 1024) {
+ /*
+ * XXX special case for device requiring peer-peer DMA
+ */
+ if (size == 0x100000000UL)
+ baseptr = &hostbase;
+ else
+ baseptr = &pci_emul_membase64;
+ limit = PCI_EMUL_MEMLIMIT64;
+ mask = PCIM_BAR_MEM_BASE;
+ lobits = PCIM_BAR_MEM_SPACE | PCIM_BAR_MEM_64 |
+ PCIM_BAR_MEM_PREFETCH;
+ break;
+ } else {
+ baseptr = &pci_emul_membase32;
+ limit = PCI_EMUL_MEMLIMIT32;
+ mask = PCIM_BAR_MEM_BASE;
+ lobits = PCIM_BAR_MEM_SPACE | PCIM_BAR_MEM_64;
+ }
+ break;
+ case PCIBAR_MEM32:
+ baseptr = &pci_emul_membase32;
+ limit = PCI_EMUL_MEMLIMIT32;
+ mask = PCIM_BAR_MEM_BASE;
+ lobits = PCIM_BAR_MEM_SPACE | PCIM_BAR_MEM_32;
+ break;
+ default:
+ printf("pci_emul_alloc_base: invalid bar type %d\n", type);
+ assert(0);
+ }
+
+ if (baseptr != NULL) {
+ error = pci_emul_alloc_resource(baseptr, limit, size, &addr);
+ if (error != 0)
+ return (error);
+ }
+
+ pdi->pi_bar[idx].type = type;
+ pdi->pi_bar[idx].addr = addr;
+ pdi->pi_bar[idx].size = size;
+
+ /* Initialize the BAR register in config space */
+ bar = (addr & mask) | lobits;
+ pci_set_cfgdata32(pdi, PCIR_BAR(idx), bar);
+
+ if (type == PCIBAR_MEM64) {
+ assert(idx + 1 <= PCI_BARMAX);
+ pdi->pi_bar[idx + 1].type = PCIBAR_MEMHI64;
+ pci_set_cfgdata32(pdi, PCIR_BAR(idx + 1), bar >> 32);
+ }
+
+ /* add a handler to intercept accesses to the I/O bar */
+ if (type == PCIBAR_IO) {
+ iop.name = pdi->pi_name;
+ iop.flags = IOPORT_F_INOUT;
+ iop.handler = pci_emul_io_handler;
+ iop.arg = pdi;
+
+ for (i = 0; i < size; i++) {
+ iop.port = addr + i;
+ register_inout(&iop);
+ }
+ } else if (type == PCIBAR_MEM32 || type == PCIBAR_MEM64) {
+ /* add memory bar intercept handler */
+ memp.name = pdi->pi_name;
+ memp.flags = MEM_F_RW;
+ memp.base = addr;
+ memp.size = size;
+ memp.handler = pci_emul_mem_handler;
+ memp.arg1 = pdi;
+ memp.arg2 = idx;
+
+ error = register_mem(&memp);
+ assert(error == 0);
+ }
+
+ return (0);
+}
+
+#define CAP_START_OFFSET 0x40
+static int
+pci_emul_add_capability(struct pci_devinst *pi, u_char *capdata, int caplen)
+{
+ int i, capoff, capid, reallen;
+ uint16_t sts;
+
+ static u_char endofcap[4] = {
+ PCIY_RESERVED, 0, 0, 0
+ };
+
+ assert(caplen > 0 && capdata[0] != PCIY_RESERVED);
+
+ reallen = roundup2(caplen, 4); /* dword aligned */
+
+ sts = pci_get_cfgdata16(pi, PCIR_STATUS);
+ if ((sts & PCIM_STATUS_CAPPRESENT) == 0) {
+ capoff = CAP_START_OFFSET;
+ pci_set_cfgdata8(pi, PCIR_CAP_PTR, capoff);
+ pci_set_cfgdata16(pi, PCIR_STATUS, sts|PCIM_STATUS_CAPPRESENT);
+ } else {
+ capoff = pci_get_cfgdata8(pi, PCIR_CAP_PTR);
+ while (1) {
+ assert((capoff & 0x3) == 0);
+ capid = pci_get_cfgdata8(pi, capoff);
+ if (capid == PCIY_RESERVED)
+ break;
+ capoff = pci_get_cfgdata8(pi, capoff + 1);
+ }
+ }
+
+ /* Check if we have enough space */
+ if (capoff + reallen + sizeof(endofcap) > PCI_REGMAX + 1)
+ return (-1);
+
+ /* Copy the capability */
+ for (i = 0; i < caplen; i++)
+ pci_set_cfgdata8(pi, capoff + i, capdata[i]);
+
+ /* Set the next capability pointer */
+ pci_set_cfgdata8(pi, capoff + 1, capoff + reallen);
+
+ /* Copy of the reserved capability which serves as the end marker */
+ for (i = 0; i < sizeof(endofcap); i++)
+ pci_set_cfgdata8(pi, capoff + reallen + i, endofcap[i]);
+
+ return (0);
+}
+
+static struct pci_devemu *
+pci_emul_finddev(char *name)
+{
+ struct pci_devemu **pdpp, *pdp;
+
+ SET_FOREACH(pdpp, pci_devemu_set) {
+ pdp = *pdpp;
+ if (!strcmp(pdp->pe_emu, name)) {
+ return (pdp);
+ }
+ }
+
+ return (NULL);
+}
+
+static void
+pci_emul_init(struct vmctx *ctx, struct pci_devemu *pde, int slot, int func,
+ char *params)
+{
+ struct pci_devinst *pdi;
+ pdi = malloc(sizeof(struct pci_devinst));
+ bzero(pdi, sizeof(*pdi));
+
+ pdi->pi_vmctx = ctx;
+ pdi->pi_bus = 0;
+ pdi->pi_slot = slot;
+ pdi->pi_func = func;
+ pdi->pi_d = pde;
+ snprintf(pdi->pi_name, PI_NAMESZ, "%s-pci-%d", pde->pe_emu, slot);
+
+ /* Disable legacy interrupts */
+ pci_set_cfgdata8(pdi, PCIR_INTLINE, 255);
+ pci_set_cfgdata8(pdi, PCIR_INTPIN, 0);
+
+ pci_set_cfgdata8(pdi, PCIR_COMMAND,
+ PCIM_CMD_PORTEN | PCIM_CMD_MEMEN | PCIM_CMD_BUSMASTEREN);
+
+ if ((*pde->pe_init)(ctx, pdi, params) != 0) {
+ free(pdi);
+ } else {
+ pci_emul_devices++;
+ pci_slotinfo[slot][func].si_devi = pdi;
+ }
+}
+
+void
+pci_populate_msicap(struct msicap *msicap, int msgnum, int nextptr)
+{
+ int mmc;
+
+ CTASSERT(sizeof(struct msicap) == 14);
+
+ /* Number of msi messages must be a power of 2 between 1 and 32 */
+ assert((msgnum & (msgnum - 1)) == 0 && msgnum >= 1 && msgnum <= 32);
+ mmc = ffs(msgnum) - 1;
+
+ bzero(msicap, sizeof(struct msicap));
+ msicap->capid = PCIY_MSI;
+ msicap->nextptr = nextptr;
+ msicap->msgctrl = PCIM_MSICTRL_64BIT | (mmc << 1);
+}
+
+int
+pci_emul_add_msicap(struct pci_devinst *pi, int msgnum)
+{
+ struct msicap msicap;
+
+ pci_populate_msicap(&msicap, msgnum, 0);
+
+ return (pci_emul_add_capability(pi, (u_char *)&msicap, sizeof(msicap)));
+}
+
+static void
+pci_populate_msixcap(struct msixcap *msixcap, int msgnum, int barnum,
+ uint32_t msix_tab_size, int nextptr)
+{
+ CTASSERT(sizeof(struct msixcap) == 12);
+
+ assert(msix_tab_size % 4096 == 0);
+
+ bzero(msixcap, sizeof(struct msixcap));
+ msixcap->capid = PCIY_MSIX;
+ msixcap->nextptr = nextptr;
+
+ /*
+ * Message Control Register, all fields set to
+ * zero except for the Table Size.
+ * Note: Table size N is encoded as N-1
+ */
+ msixcap->msgctrl = msgnum - 1;
+
+ /*
+ * MSI-X BAR setup:
+ * - MSI-X table start at offset 0
+ * - PBA table starts at a 4K aligned offset after the MSI-X table
+ */
+ msixcap->table_info = barnum & PCIM_MSIX_BIR_MASK;
+ msixcap->pba_info = msix_tab_size | (barnum & PCIM_MSIX_BIR_MASK);
+}
+
+static void
+pci_msix_table_init(struct pci_devinst *pi, int table_entries)
+{
+ int i, table_size;
+
+ assert(table_entries > 0);
+ assert(table_entries <= MAX_MSIX_TABLE_ENTRIES);
+
+ table_size = table_entries * MSIX_TABLE_ENTRY_SIZE;
+ pi->pi_msix.table = malloc(table_size);
+ bzero(pi->pi_msix.table, table_size);
+
+ /* set mask bit of vector control register */
+ for (i = 0; i < table_entries; i++)
+ pi->pi_msix.table[i].vector_control |= PCIM_MSIX_VCTRL_MASK;
+}
+
+int
+pci_emul_add_msixcap(struct pci_devinst *pi, int msgnum, int barnum)
+{
+ uint16_t pba_index;
+ uint32_t tab_size;
+ struct msixcap msixcap;
+
+ assert(msgnum >= 1 && msgnum <= MAX_MSIX_TABLE_ENTRIES);
+ assert(barnum >= 0 && barnum <= PCIR_MAX_BAR_0);
+
+ tab_size = msgnum * MSIX_TABLE_ENTRY_SIZE;
+
+ /* Align table size to nearest 4K */
+ tab_size = roundup2(tab_size, 4096);
+
+ pi->pi_msix.table_bar = barnum;
+ pi->pi_msix.pba_bar = barnum;
+ pi->pi_msix.table_offset = 0;
+ pi->pi_msix.table_count = msgnum;
+ pi->pi_msix.pba_offset = tab_size;
+
+ /* calculate the MMIO size required for MSI-X PBA */
+ pba_index = (msgnum - 1) / (PBA_TABLE_ENTRY_SIZE * 8);
+ pi->pi_msix.pba_size = (pba_index + 1) * PBA_TABLE_ENTRY_SIZE;
+
+ pci_msix_table_init(pi, msgnum);
+
+ pci_populate_msixcap(&msixcap, msgnum, barnum, tab_size, 0);
+
+ /* allocate memory for MSI-X Table and PBA */
+ pci_emul_alloc_bar(pi, barnum, PCIBAR_MEM32,
+ tab_size + pi->pi_msix.pba_size);
+
+ return (pci_emul_add_capability(pi, (u_char *)&msixcap,
+ sizeof(msixcap)));
+}
+
+void
+msixcap_cfgwrite(struct pci_devinst *pi, int capoff, int offset,
+ int bytes, uint32_t val)
+{
+ uint16_t msgctrl, rwmask;
+ int off, table_bar;
+
+ off = offset - capoff;
+ table_bar = pi->pi_msix.table_bar;
+ /* Message Control Register */
+ if (off == 2 && bytes == 2) {
+ rwmask = PCIM_MSIXCTRL_MSIX_ENABLE | PCIM_MSIXCTRL_FUNCTION_MASK;
+ msgctrl = pci_get_cfgdata16(pi, offset);
+ msgctrl &= ~rwmask;
+ msgctrl |= val & rwmask;
+ val = msgctrl;
+
+ pi->pi_msix.enabled = val & PCIM_MSIXCTRL_MSIX_ENABLE;
+ pi->pi_msix.function_mask = val & PCIM_MSIXCTRL_FUNCTION_MASK;
+ }
+
+ CFGWRITE(pi, offset, val, bytes);
+}
+
+void
+msicap_cfgwrite(struct pci_devinst *pi, int capoff, int offset,
+ int bytes, uint32_t val)
+{
+ uint16_t msgctrl, rwmask, msgdata, mme;
+ uint32_t addrlo;
+
+ /*
+ * If guest is writing to the message control register make sure
+ * we do not overwrite read-only fields.
+ */
+ if ((offset - capoff) == 2 && bytes == 2) {
+ rwmask = PCIM_MSICTRL_MME_MASK | PCIM_MSICTRL_MSI_ENABLE;
+ msgctrl = pci_get_cfgdata16(pi, offset);
+ msgctrl &= ~rwmask;
+ msgctrl |= val & rwmask;
+ val = msgctrl;
+
+ addrlo = pci_get_cfgdata32(pi, capoff + 4);
+ if (msgctrl & PCIM_MSICTRL_64BIT)
+ msgdata = pci_get_cfgdata16(pi, capoff + 12);
+ else
+ msgdata = pci_get_cfgdata16(pi, capoff + 8);
+
+ /*
+ * XXX check delivery mode, destination mode etc
+ */
+ mme = msgctrl & PCIM_MSICTRL_MME_MASK;
+ pi->pi_msi.enabled = msgctrl & PCIM_MSICTRL_MSI_ENABLE ? 1 : 0;
+ if (pi->pi_msi.enabled) {
+ pi->pi_msi.cpu = (addrlo >> 12) & 0xff;
+ pi->pi_msi.vector = msgdata & 0xff;
+ pi->pi_msi.msgnum = 1 << (mme >> 4);
+ } else {
+ pi->pi_msi.cpu = 0;
+ pi->pi_msi.vector = 0;
+ pi->pi_msi.msgnum = 0;
+ }
+ }
+
+ CFGWRITE(pi, offset, val, bytes);
+}
+
+/*
+ * This function assumes that 'coff' is in the capabilities region of the
+ * config space.
+ */
+static void
+pci_emul_capwrite(struct pci_devinst *pi, int offset, int bytes, uint32_t val)
+{
+ int capid;
+ uint8_t capoff, nextoff;
+
+ /* Do not allow un-aligned writes */
+ if ((offset & (bytes - 1)) != 0)
+ return;
+
+ /* Find the capability that we want to update */
+ capoff = CAP_START_OFFSET;
+ while (1) {
+ capid = pci_get_cfgdata8(pi, capoff);
+ if (capid == PCIY_RESERVED)
+ break;
+
+ nextoff = pci_get_cfgdata8(pi, capoff + 1);
+ if (offset >= capoff && offset < nextoff)
+ break;
+
+ capoff = nextoff;
+ }
+ assert(offset >= capoff);
+
+ /*
+ * Capability ID and Next Capability Pointer are readonly
+ */
+ if (offset == capoff || offset == capoff + 1)
+ return;
+
+ switch (capid) {
+ case PCIY_MSI:
+ msicap_cfgwrite(pi, capoff, offset, bytes, val);
+ break;
+ case PCIY_MSIX:
+ msixcap_cfgwrite(pi, capoff, offset, bytes, val);
+ break;
+ default:
+ break;
+ }
+}
+
+static int
+pci_emul_iscap(struct pci_devinst *pi, int offset)
+{
+ int found;
+ uint16_t sts;
+ uint8_t capid, lastoff;
+
+ found = 0;
+ sts = pci_get_cfgdata16(pi, PCIR_STATUS);
+ if ((sts & PCIM_STATUS_CAPPRESENT) != 0) {
+ lastoff = pci_get_cfgdata8(pi, PCIR_CAP_PTR);
+ while (1) {
+ assert((lastoff & 0x3) == 0);
+ capid = pci_get_cfgdata8(pi, lastoff);
+ if (capid == PCIY_RESERVED)
+ break;
+ lastoff = pci_get_cfgdata8(pi, lastoff + 1);
+ }
+ if (offset >= CAP_START_OFFSET && offset <= lastoff)
+ found = 1;
+ }
+ return (found);
+}
+
+void
+init_pci(struct vmctx *ctx)
+{
+ struct pci_devemu *pde;
+ struct slotinfo *si;
+ int slot, func;
+
+ pci_emul_iobase = PCI_EMUL_IOBASE;
+ pci_emul_membase32 = PCI_EMUL_MEMBASE32;
+ pci_emul_membase64 = PCI_EMUL_MEMBASE64;
+
+ for (slot = 0; slot < MAXSLOTS; slot++) {
+ for (func = 0; func < MAXFUNCS; func++) {
+ si = &pci_slotinfo[slot][func];
+ if (si->si_name != NULL) {
+ pde = pci_emul_finddev(si->si_name);
+ if (pde != NULL) {
+ pci_emul_init(ctx, pde, slot, func,
+ si->si_param);
+ }
+ }
+ }
+ }
+
+ /*
+ * Allow ISA IRQs 5,10,11,12, and 15 to be available for
+ * generic use
+ */
+ lirq[5].li_generic = 1;
+ lirq[10].li_generic = 1;
+ lirq[11].li_generic = 1;
+ lirq[12].li_generic = 1;
+ lirq[15].li_generic = 1;
+}
+
+int
+pci_msi_enabled(struct pci_devinst *pi)
+{
+ return (pi->pi_msi.enabled);
+}
+
+int
+pci_msi_msgnum(struct pci_devinst *pi)
+{
+ if (pi->pi_msi.enabled)
+ return (pi->pi_msi.msgnum);
+ else
+ return (0);
+}
+
+int
+pci_msix_enabled(struct pci_devinst *pi)
+{
+
+ return (pi->pi_msix.enabled && !pi->pi_msi.enabled);
+}
+
+void
+pci_generate_msix(struct pci_devinst *pi, int index)
+{
+ struct msix_table_entry *mte;
+
+ if (!pci_msix_enabled(pi))
+ return;
+
+ if (pi->pi_msix.function_mask)
+ return;
+
+ if (index >= pi->pi_msix.table_count)
+ return;
+
+ mte = &pi->pi_msix.table[index];
+ if ((mte->vector_control & PCIM_MSIX_VCTRL_MASK) == 0) {
+ /* XXX Set PBA bit if interrupt is disabled */
+ vm_lapic_irq(pi->pi_vmctx,
+ (mte->addr >> 12) & 0xff, mte->msg_data & 0xff);
+ }
+}
+
+void
+pci_generate_msi(struct pci_devinst *pi, int msg)
+{
+
+ if (pci_msi_enabled(pi) && msg < pci_msi_msgnum(pi)) {
+ vm_lapic_irq(pi->pi_vmctx,
+ pi->pi_msi.cpu,
+ pi->pi_msi.vector + msg);
+ }
+}
+
+int
+pci_is_legacy(struct pci_devinst *pi)
+{
+
+ return (pci_slotinfo[pi->pi_slot][pi->pi_func].si_legacy);
+}
+
+static int
+pci_lintr_alloc(struct pci_devinst *pi, int vec)
+{
+ int i;
+
+ assert(vec < NLIRQ);
+
+ if (vec == -1) {
+ for (i = 0; i < NLIRQ; i++) {
+ if (lirq[i].li_generic &&
+ lirq[i].li_owner == NULL) {
+ vec = i;
+ break;
+ }
+ }
+ } else {
+ if (lirq[vec].li_owner != NULL) {
+ vec = -1;
+ }
+ }
+ assert(vec != -1);
+
+ lirq[vec].li_owner = pi;
+ pi->pi_lintr_pin = vec;
+
+ return (vec);
+}
+
+int
+pci_lintr_request(struct pci_devinst *pi, int vec)
+{
+
+ vec = pci_lintr_alloc(pi, vec);
+ pci_set_cfgdata8(pi, PCIR_INTLINE, vec);
+ pci_set_cfgdata8(pi, PCIR_INTPIN, 1);
+ return (0);
+}
+
+void
+pci_lintr_assert(struct pci_devinst *pi)
+{
+
+ assert(pi->pi_lintr_pin);
+ ioapic_assert_pin(pi->pi_vmctx, pi->pi_lintr_pin);
+}
+
+void
+pci_lintr_deassert(struct pci_devinst *pi)
+{
+
+ assert(pi->pi_lintr_pin);
+ ioapic_deassert_pin(pi->pi_vmctx, pi->pi_lintr_pin);
+}
+
+/*
+ * Return 1 if the emulated device in 'slot' is a multi-function device.
+ * Return 0 otherwise.
+ */
+static int
+pci_emul_is_mfdev(int slot)
+{
+ int f, numfuncs;
+
+ numfuncs = 0;
+ for (f = 0; f < MAXFUNCS; f++) {
+ if (pci_slotinfo[slot][f].si_devi != NULL) {
+ numfuncs++;
+ }
+ }
+ return (numfuncs > 1);
+}
+
+/*
+ * Ensure that the PCIM_MFDEV bit is properly set (or unset) depending on
+ * whether or not is a multi-function being emulated in the pci 'slot'.
+ */
+static void
+pci_emul_hdrtype_fixup(int slot, int off, int bytes, uint32_t *rv)
+{
+ int mfdev;
+
+ if (off <= PCIR_HDRTYPE && off + bytes > PCIR_HDRTYPE) {
+ mfdev = pci_emul_is_mfdev(slot);
+ switch (bytes) {
+ case 1:
+ case 2:
+ *rv &= ~PCIM_MFDEV;
+ if (mfdev) {
+ *rv |= PCIM_MFDEV;
+ }
+ break;
+ case 4:
+ *rv &= ~(PCIM_MFDEV << 16);
+ if (mfdev) {
+ *rv |= (PCIM_MFDEV << 16);
+ }
+ break;
+ }
+ }
+}
+
+static int cfgbus, cfgslot, cfgfunc, cfgoff;
+
+static int
+pci_emul_cfgaddr(struct vmctx *ctx, int vcpu, int in, int port, int bytes,
+ uint32_t *eax, void *arg)
+{
+ uint32_t x;
+
+ assert(!in);
+
+ if (bytes != 4)
+ return (-1);
+
+ x = *eax;
+ cfgoff = x & PCI_REGMAX;
+ cfgfunc = (x >> 8) & PCI_FUNCMAX;
+ cfgslot = (x >> 11) & PCI_SLOTMAX;
+ cfgbus = (x >> 16) & PCI_BUSMAX;
+
+ return (0);
+}
+INOUT_PORT(pci_cfgaddr, CONF1_ADDR_PORT, IOPORT_F_OUT, pci_emul_cfgaddr);
+
+static int
+pci_emul_cfgdata(struct vmctx *ctx, int vcpu, int in, int port, int bytes,
+ uint32_t *eax, void *arg)
+{
+ struct pci_devinst *pi;
+ struct pci_devemu *pe;
+ int coff, idx, needcfg;
+ uint64_t mask, bar;
+
+ assert(bytes == 1 || bytes == 2 || bytes == 4);
+
+ if (cfgbus == 0)
+ pi = pci_slotinfo[cfgslot][cfgfunc].si_devi;
+ else
+ pi = NULL;
+
+ coff = cfgoff + (port - CONF1_DATA_PORT);
+
+#if 0
+ printf("pcicfg-%s from 0x%0x of %d bytes (%d/%d/%d)\n\r",
+ in ? "read" : "write", coff, bytes, cfgbus, cfgslot, cfgfunc);
+#endif
+
+ /*
+ * Just return if there is no device at this cfgslot:cfgfunc or
+ * if the guest is doing an un-aligned access
+ */
+ if (pi == NULL || (coff & (bytes - 1)) != 0) {
+ if (in)
+ *eax = 0xffffffff;
+ return (0);
+ }
+
+ pe = pi->pi_d;
+
+ /*
+ * Config read
+ */
+ if (in) {
+ /* Let the device emulation override the default handler */
+ if (pe->pe_cfgread != NULL) {
+ needcfg = pe->pe_cfgread(ctx, vcpu, pi,
+ coff, bytes, eax);
+ } else {
+ needcfg = 1;
+ }
+
+ if (needcfg) {
+ if (bytes == 1)
+ *eax = pci_get_cfgdata8(pi, coff);
+ else if (bytes == 2)
+ *eax = pci_get_cfgdata16(pi, coff);
+ else
+ *eax = pci_get_cfgdata32(pi, coff);
+ }
+
+ pci_emul_hdrtype_fixup(cfgslot, coff, bytes, eax);
+ } else {
+ /* Let the device emulation override the default handler */
+ if (pe->pe_cfgwrite != NULL &&
+ (*pe->pe_cfgwrite)(ctx, vcpu, pi, coff, bytes, *eax) == 0)
+ return (0);
+
+ /*
+ * Special handling for write to BAR registers
+ */
+ if (coff >= PCIR_BAR(0) && coff < PCIR_BAR(PCI_BARMAX + 1)) {
+ /*
+ * Ignore writes to BAR registers that are not
+ * 4-byte aligned.
+ */
+ if (bytes != 4 || (coff & 0x3) != 0)
+ return (0);
+ idx = (coff - PCIR_BAR(0)) / 4;
+ switch (pi->pi_bar[idx].type) {
+ case PCIBAR_NONE:
+ bar = 0;
+ break;
+ case PCIBAR_IO:
+ mask = ~(pi->pi_bar[idx].size - 1);
+ mask &= PCIM_BAR_IO_BASE;
+ bar = (*eax & mask) | PCIM_BAR_IO_SPACE;
+ break;
+ case PCIBAR_MEM32:
+ mask = ~(pi->pi_bar[idx].size - 1);
+ mask &= PCIM_BAR_MEM_BASE;
+ bar = *eax & mask;
+ bar |= PCIM_BAR_MEM_SPACE | PCIM_BAR_MEM_32;
+ break;
+ case PCIBAR_MEM64:
+ mask = ~(pi->pi_bar[idx].size - 1);
+ mask &= PCIM_BAR_MEM_BASE;
+ bar = *eax & mask;
+ bar |= PCIM_BAR_MEM_SPACE | PCIM_BAR_MEM_64 |
+ PCIM_BAR_MEM_PREFETCH;
+ break;
+ case PCIBAR_MEMHI64:
+ mask = ~(pi->pi_bar[idx - 1].size - 1);
+ mask &= PCIM_BAR_MEM_BASE;
+ bar = ((uint64_t)*eax << 32) & mask;
+ bar = bar >> 32;
+ break;
+ default:
+ assert(0);
+ }
+ pci_set_cfgdata32(pi, coff, bar);
+
+ } else if (pci_emul_iscap(pi, coff)) {
+ pci_emul_capwrite(pi, coff, bytes, *eax);
+ } else {
+ CFGWRITE(pi, coff, *eax, bytes);
+ }
+ }
+
+ return (0);
+}
+
+INOUT_PORT(pci_cfgdata, CONF1_DATA_PORT+0, IOPORT_F_INOUT, pci_emul_cfgdata);
+INOUT_PORT(pci_cfgdata, CONF1_DATA_PORT+1, IOPORT_F_INOUT, pci_emul_cfgdata);
+INOUT_PORT(pci_cfgdata, CONF1_DATA_PORT+2, IOPORT_F_INOUT, pci_emul_cfgdata);
+INOUT_PORT(pci_cfgdata, CONF1_DATA_PORT+3, IOPORT_F_INOUT, pci_emul_cfgdata);
+
+/*
+ * I/O ports to configure PCI IRQ routing. We ignore all writes to it.
+ */
+static int
+pci_irq_port_handler(struct vmctx *ctx, int vcpu, int in, int port, int bytes,
+ uint32_t *eax, void *arg)
+{
+ assert(in == 0);
+ return (0);
+}
+INOUT_PORT(pci_irq, 0xC00, IOPORT_F_OUT, pci_irq_port_handler);
+INOUT_PORT(pci_irq, 0xC01, IOPORT_F_OUT, pci_irq_port_handler);
+
+#define PCI_EMUL_TEST
+#ifdef PCI_EMUL_TEST
+/*
+ * Define a dummy test device
+ */
+#define DIOSZ 20
+#define DMEMSZ 4096
+struct pci_emul_dsoftc {
+ uint8_t ioregs[DIOSZ];
+ uint8_t memregs[DMEMSZ];
+};
+
+#define PCI_EMUL_MSI_MSGS 4
+#define PCI_EMUL_MSIX_MSGS 16
+
+static int
+pci_emul_dinit(struct vmctx *ctx, struct pci_devinst *pi, char *opts)
+{
+ int error;
+ struct pci_emul_dsoftc *sc;
+
+ sc = malloc(sizeof(struct pci_emul_dsoftc));
+ memset(sc, 0, sizeof(struct pci_emul_dsoftc));
+
+ pi->pi_arg = sc;
+
+ pci_set_cfgdata16(pi, PCIR_DEVICE, 0x0001);
+ pci_set_cfgdata16(pi, PCIR_VENDOR, 0x10DD);
+ pci_set_cfgdata8(pi, PCIR_CLASS, 0x02);
+
+ error = pci_emul_add_msicap(pi, PCI_EMUL_MSI_MSGS);
+ assert(error == 0);
+
+ error = pci_emul_alloc_bar(pi, 0, PCIBAR_IO, DIOSZ);
+ assert(error == 0);
+
+ error = pci_emul_alloc_bar(pi, 1, PCIBAR_MEM32, DMEMSZ);
+ assert(error == 0);
+
+ return (0);
+}
+
+static void
+pci_emul_diow(struct vmctx *ctx, int vcpu, struct pci_devinst *pi, int baridx,
+ uint64_t offset, int size, uint64_t value)
+{
+ int i;
+ struct pci_emul_dsoftc *sc = pi->pi_arg;
+
+ if (baridx == 0) {
+ if (offset + size > DIOSZ) {
+ printf("diow: iow too large, offset %ld size %d\n",
+ offset, size);
+ return;
+ }
+
+ if (size == 1) {
+ sc->ioregs[offset] = value & 0xff;
+ } else if (size == 2) {
+ *(uint16_t *)&sc->ioregs[offset] = value & 0xffff;
+ } else if (size == 4) {
+ *(uint32_t *)&sc->ioregs[offset] = value;
+ } else {
+ printf("diow: iow unknown size %d\n", size);
+ }
+
+ /*
+ * Special magic value to generate an interrupt
+ */
+ if (offset == 4 && size == 4 && pci_msi_enabled(pi))
+ pci_generate_msi(pi, value % pci_msi_msgnum(pi));
+
+ if (value == 0xabcdef) {
+ for (i = 0; i < pci_msi_msgnum(pi); i++)
+ pci_generate_msi(pi, i);
+ }
+ }
+
+ if (baridx == 1) {
+ if (offset + size > DMEMSZ) {
+ printf("diow: memw too large, offset %ld size %d\n",
+ offset, size);
+ return;
+ }
+
+ if (size == 1) {
+ sc->memregs[offset] = value;
+ } else if (size == 2) {
+ *(uint16_t *)&sc->memregs[offset] = value;
+ } else if (size == 4) {
+ *(uint32_t *)&sc->memregs[offset] = value;
+ } else if (size == 8) {
+ *(uint64_t *)&sc->memregs[offset] = value;
+ } else {
+ printf("diow: memw unknown size %d\n", size);
+ }
+
+ /*
+ * magic interrupt ??
+ */
+ }
+
+ if (baridx > 1) {
+ printf("diow: unknown bar idx %d\n", baridx);
+ }
+}
+
+static uint64_t
+pci_emul_dior(struct vmctx *ctx, int vcpu, struct pci_devinst *pi, int baridx,
+ uint64_t offset, int size)
+{
+ struct pci_emul_dsoftc *sc = pi->pi_arg;
+ uint32_t value;
+
+ if (baridx == 0) {
+ if (offset + size > DIOSZ) {
+ printf("dior: ior too large, offset %ld size %d\n",
+ offset, size);
+ return (0);
+ }
+
+ if (size == 1) {
+ value = sc->ioregs[offset];
+ } else if (size == 2) {
+ value = *(uint16_t *) &sc->ioregs[offset];
+ } else if (size == 4) {
+ value = *(uint32_t *) &sc->ioregs[offset];
+ } else {
+ printf("dior: ior unknown size %d\n", size);
+ }
+ }
+
+ if (baridx == 1) {
+ if (offset + size > DMEMSZ) {
+ printf("dior: memr too large, offset %ld size %d\n",
+ offset, size);
+ return (0);
+ }
+
+ if (size == 1) {
+ value = sc->memregs[offset];
+ } else if (size == 2) {
+ value = *(uint16_t *) &sc->memregs[offset];
+ } else if (size == 4) {
+ value = *(uint32_t *) &sc->memregs[offset];
+ } else if (size == 8) {
+ value = *(uint64_t *) &sc->memregs[offset];
+ } else {
+ printf("dior: ior unknown size %d\n", size);
+ }
+ }
+
+
+ if (baridx > 1) {
+ printf("dior: unknown bar idx %d\n", baridx);
+ return (0);
+ }
+
+ return (value);
+}
+
+struct pci_devemu pci_dummy = {
+ .pe_emu = "dummy",
+ .pe_init = pci_emul_dinit,
+ .pe_barwrite = pci_emul_diow,
+ .pe_barread = pci_emul_dior
+};
+PCI_EMUL_SET(pci_dummy);
+
+#endif /* PCI_EMUL_TEST */
diff --git a/usr.sbin/bhyve/pci_emul.h b/usr.sbin/bhyve/pci_emul.h
new file mode 100644
index 0000000..8c6260c
--- /dev/null
+++ b/usr.sbin/bhyve/pci_emul.h
@@ -0,0 +1,224 @@
+/*-
+ * Copyright (c) 2011 NetApp, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ */
+
+#ifndef _PCI_EMUL_H_
+#define _PCI_EMUL_H_
+
+#include <sys/types.h>
+#include <sys/queue.h>
+#include <sys/kernel.h>
+
+#include <dev/pci/pcireg.h>
+
+#include <assert.h>
+
+#define PCI_BARMAX PCIR_MAX_BAR_0 /* BAR registers in a Type 0 header */
+#define PCIY_RESERVED 0x00
+
+struct vmctx;
+struct pci_devinst;
+struct memory_region;
+
+struct pci_devemu {
+ char *pe_emu; /* Name of device emulation */
+
+ /* instance creation */
+ int (*pe_init)(struct vmctx *, struct pci_devinst *,
+ char *opts);
+
+ /* config space read/write callbacks */
+ int (*pe_cfgwrite)(struct vmctx *ctx, int vcpu,
+ struct pci_devinst *pi, int offset,
+ int bytes, uint32_t val);
+ int (*pe_cfgread)(struct vmctx *ctx, int vcpu,
+ struct pci_devinst *pi, int offset,
+ int bytes, uint32_t *retval);
+
+ /* BAR read/write callbacks */
+ void (*pe_barwrite)(struct vmctx *ctx, int vcpu,
+ struct pci_devinst *pi, int baridx,
+ uint64_t offset, int size, uint64_t value);
+ uint64_t (*pe_barread)(struct vmctx *ctx, int vcpu,
+ struct pci_devinst *pi, int baridx,
+ uint64_t offset, int size);
+};
+#define PCI_EMUL_SET(x) DATA_SET(pci_devemu_set, x);
+
+enum pcibar_type {
+ PCIBAR_NONE,
+ PCIBAR_IO,
+ PCIBAR_MEM32,
+ PCIBAR_MEM64,
+ PCIBAR_MEMHI64
+};
+
+struct pcibar {
+ enum pcibar_type type; /* io or memory */
+ uint64_t size;
+ uint64_t addr;
+};
+
+#define PI_NAMESZ 40
+
+struct msix_table_entry {
+ uint64_t addr;
+ uint32_t msg_data;
+ uint32_t vector_control;
+} __packed;
+
+/*
+ * In case the structure is modified to hold extra information, use a define
+ * for the size that should be emulated.
+ */
+#define MSIX_TABLE_ENTRY_SIZE 16
+#define MAX_MSIX_TABLE_ENTRIES 2048
+#define PBA_TABLE_ENTRY_SIZE 8
+
+struct pci_devinst {
+ struct pci_devemu *pi_d;
+ struct vmctx *pi_vmctx;
+ uint8_t pi_bus, pi_slot, pi_func;
+ uint8_t pi_lintr_pin;
+ char pi_name[PI_NAMESZ];
+ uint16_t pi_iobase;
+ int pi_bar_getsize;
+
+ struct {
+ int enabled;
+ int cpu;
+ int vector;
+ int msgnum;
+ } pi_msi;
+
+ struct {
+ int enabled;
+ int table_bar;
+ int pba_bar;
+ size_t table_offset;
+ int table_count;
+ size_t pba_offset;
+ size_t pba_size;
+ int function_mask;
+ struct msix_table_entry *table; /* allocated at runtime */
+ } pi_msix;
+
+ void *pi_arg; /* devemu-private data */
+
+ u_char pi_cfgdata[PCI_REGMAX + 1];
+ struct pcibar pi_bar[PCI_BARMAX + 1];
+};
+
+struct msicap {
+ uint8_t capid;
+ uint8_t nextptr;
+ uint16_t msgctrl;
+ uint32_t addrlo;
+ uint32_t addrhi;
+ uint16_t msgdata;
+} __packed;
+
+struct msixcap {
+ uint8_t capid;
+ uint8_t nextptr;
+ uint16_t msgctrl;
+ uint32_t table_info; /* bar index and offset within it */
+ uint32_t pba_info; /* bar index and offset within it */
+} __packed;
+
+void init_pci(struct vmctx *ctx);
+void msicap_cfgwrite(struct pci_devinst *pi, int capoff, int offset,
+ int bytes, uint32_t val);
+void msixcap_cfgwrite(struct pci_devinst *pi, int capoff, int offset,
+ int bytes, uint32_t val);
+void pci_callback(void);
+int pci_emul_alloc_bar(struct pci_devinst *pdi, int idx,
+ enum pcibar_type type, uint64_t size);
+int pci_emul_alloc_pbar(struct pci_devinst *pdi, int idx,
+ uint64_t hostbase, enum pcibar_type type, uint64_t size);
+int pci_emul_add_msicap(struct pci_devinst *pi, int msgnum);
+int pci_is_legacy(struct pci_devinst *pi);
+void pci_generate_msi(struct pci_devinst *pi, int msgnum);
+void pci_generate_msix(struct pci_devinst *pi, int msgnum);
+void pci_lintr_assert(struct pci_devinst *pi);
+void pci_lintr_deassert(struct pci_devinst *pi);
+int pci_lintr_request(struct pci_devinst *pi, int ivec);
+int pci_msi_enabled(struct pci_devinst *pi);
+int pci_msix_enabled(struct pci_devinst *pi);
+int pci_msix_table_bar(struct pci_devinst *pi);
+int pci_msix_pba_bar(struct pci_devinst *pi);
+int pci_msi_msgnum(struct pci_devinst *pi);
+void pci_parse_slot(char *opt, int legacy);
+void pci_populate_msicap(struct msicap *cap, int msgs, int nextptr);
+int pci_emul_add_msixcap(struct pci_devinst *pi, int msgnum, int barnum);
+int pci_emul_msix_twrite(struct pci_devinst *pi, uint64_t offset, int size,
+ uint64_t value);
+uint64_t pci_emul_msix_tread(struct pci_devinst *pi, uint64_t offset, int size);
+
+static __inline void
+pci_set_cfgdata8(struct pci_devinst *pi, int offset, uint8_t val)
+{
+ assert(offset <= PCI_REGMAX);
+ *(uint8_t *)(pi->pi_cfgdata + offset) = val;
+}
+
+static __inline void
+pci_set_cfgdata16(struct pci_devinst *pi, int offset, uint16_t val)
+{
+ assert(offset <= (PCI_REGMAX - 1) && (offset & 1) == 0);
+ *(uint16_t *)(pi->pi_cfgdata + offset) = val;
+}
+
+static __inline void
+pci_set_cfgdata32(struct pci_devinst *pi, int offset, uint32_t val)
+{
+ assert(offset <= (PCI_REGMAX - 3) && (offset & 3) == 0);
+ *(uint32_t *)(pi->pi_cfgdata + offset) = val;
+}
+
+static __inline uint8_t
+pci_get_cfgdata8(struct pci_devinst *pi, int offset)
+{
+ assert(offset <= PCI_REGMAX);
+ return (*(uint8_t *)(pi->pi_cfgdata + offset));
+}
+
+static __inline uint16_t
+pci_get_cfgdata16(struct pci_devinst *pi, int offset)
+{
+ assert(offset <= (PCI_REGMAX - 1) && (offset & 1) == 0);
+ return (*(uint16_t *)(pi->pi_cfgdata + offset));
+}
+
+static __inline uint32_t
+pci_get_cfgdata32(struct pci_devinst *pi, int offset)
+{
+ assert(offset <= (PCI_REGMAX - 3) && (offset & 3) == 0);
+ return (*(uint32_t *)(pi->pi_cfgdata + offset));
+}
+
+#endif /* _PCI_EMUL_H_ */
diff --git a/usr.sbin/bhyve/pci_hostbridge.c b/usr.sbin/bhyve/pci_hostbridge.c
new file mode 100644
index 0000000..c77762d
--- /dev/null
+++ b/usr.sbin/bhyve/pci_hostbridge.c
@@ -0,0 +1,52 @@
+/*-
+ * Copyright (c) 2011 NetApp, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include "pci_emul.h"
+
+static int
+pci_hostbridge_init(struct vmctx *ctx, struct pci_devinst *pi, char *opts)
+{
+
+ /* config space */
+ pci_set_cfgdata16(pi, PCIR_VENDOR, 0x1275); /* NetApp */
+ pci_set_cfgdata16(pi, PCIR_DEVICE, 0x1275); /* NetApp */
+ pci_set_cfgdata8(pi, PCIR_HDRTYPE, PCIM_HDRTYPE_BRIDGE);
+ pci_set_cfgdata8(pi, PCIR_CLASS, PCIC_BRIDGE);
+ pci_set_cfgdata8(pi, PCIR_SUBCLASS, PCIS_BRIDGE_HOST);
+
+ return (0);
+}
+
+struct pci_devemu pci_de_hostbridge = {
+ .pe_emu = "hostbridge",
+ .pe_init = pci_hostbridge_init,
+};
+PCI_EMUL_SET(pci_de_hostbridge);
diff --git a/usr.sbin/bhyve/pci_passthru.c b/usr.sbin/bhyve/pci_passthru.c
new file mode 100644
index 0000000..08a9a58
--- /dev/null
+++ b/usr.sbin/bhyve/pci_passthru.c
@@ -0,0 +1,759 @@
+/*-
+ * Copyright (c) 2011 NetApp, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <sys/param.h>
+#include <sys/types.h>
+#include <sys/pciio.h>
+#include <sys/ioctl.h>
+
+#include <dev/io/iodev.h>
+#include <dev/pci/pcireg.h>
+
+#include <machine/iodev.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+#include <machine/vmm.h>
+#include <vmmapi.h>
+#include "pci_emul.h"
+#include "mem.h"
+
+#ifndef _PATH_DEVPCI
+#define _PATH_DEVPCI "/dev/pci"
+#endif
+
+#ifndef _PATH_DEVIO
+#define _PATH_DEVIO "/dev/io"
+#endif
+
+#define LEGACY_SUPPORT 1
+
+#define MSIX_TABLE_COUNT(ctrl) (((ctrl) & PCIM_MSIXCTRL_TABLE_SIZE) + 1)
+#define MSIX_CAPLEN 12
+
+static int pcifd = -1;
+static int iofd = -1;
+
+struct passthru_softc {
+ struct pci_devinst *psc_pi;
+ struct pcibar psc_bar[PCI_BARMAX + 1];
+ struct {
+ int capoff;
+ int msgctrl;
+ int emulated;
+ } psc_msi;
+ struct {
+ int capoff;
+ } psc_msix;
+ struct pcisel psc_sel;
+};
+
+static int
+msi_caplen(int msgctrl)
+{
+ int len;
+
+ len = 10; /* minimum length of msi capability */
+
+ if (msgctrl & PCIM_MSICTRL_64BIT)
+ len += 4;
+
+#if 0
+ /*
+ * Ignore the 'mask' and 'pending' bits in the MSI capability.
+ * We'll let the guest manipulate them directly.
+ */
+ if (msgctrl & PCIM_MSICTRL_VECTOR)
+ len += 10;
+#endif
+
+ return (len);
+}
+
+static uint32_t
+read_config(const struct pcisel *sel, long reg, int width)
+{
+ struct pci_io pi;
+
+ bzero(&pi, sizeof(pi));
+ pi.pi_sel = *sel;
+ pi.pi_reg = reg;
+ pi.pi_width = width;
+
+ if (ioctl(pcifd, PCIOCREAD, &pi) < 0)
+ return (0); /* XXX */
+ else
+ return (pi.pi_data);
+}
+
+static void
+write_config(const struct pcisel *sel, long reg, int width, uint32_t data)
+{
+ struct pci_io pi;
+
+ bzero(&pi, sizeof(pi));
+ pi.pi_sel = *sel;
+ pi.pi_reg = reg;
+ pi.pi_width = width;
+ pi.pi_data = data;
+
+ (void)ioctl(pcifd, PCIOCWRITE, &pi); /* XXX */
+}
+
+#ifdef LEGACY_SUPPORT
+static int
+passthru_add_msicap(struct pci_devinst *pi, int msgnum, int nextptr)
+{
+ int capoff, i;
+ struct msicap msicap;
+ u_char *capdata;
+
+ pci_populate_msicap(&msicap, msgnum, nextptr);
+
+ /*
+ * XXX
+ * Copy the msi capability structure in the last 16 bytes of the
+ * config space. This is wrong because it could shadow something
+ * useful to the device.
+ */
+ capoff = 256 - roundup(sizeof(msicap), 4);
+ capdata = (u_char *)&msicap;
+ for (i = 0; i < sizeof(msicap); i++)
+ pci_set_cfgdata8(pi, capoff + i, capdata[i]);
+
+ return (capoff);
+}
+#endif /* LEGACY_SUPPORT */
+
+static int
+cfginitmsi(struct passthru_softc *sc)
+{
+ int i, ptr, capptr, cap, sts, caplen, table_size;
+ uint32_t u32;
+ struct pcisel sel;
+ struct pci_devinst *pi;
+ struct msixcap msixcap;
+ uint32_t *msixcap_ptr;
+
+ pi = sc->psc_pi;
+ sel = sc->psc_sel;
+
+ /*
+ * Parse the capabilities and cache the location of the MSI
+ * and MSI-X capabilities.
+ */
+ sts = read_config(&sel, PCIR_STATUS, 2);
+ if (sts & PCIM_STATUS_CAPPRESENT) {
+ ptr = read_config(&sel, PCIR_CAP_PTR, 1);
+ while (ptr != 0 && ptr != 0xff) {
+ cap = read_config(&sel, ptr + PCICAP_ID, 1);
+ if (cap == PCIY_MSI) {
+ /*
+ * Copy the MSI capability into the config
+ * space of the emulated pci device
+ */
+ sc->psc_msi.capoff = ptr;
+ sc->psc_msi.msgctrl = read_config(&sel,
+ ptr + 2, 2);
+ sc->psc_msi.emulated = 0;
+ caplen = msi_caplen(sc->psc_msi.msgctrl);
+ capptr = ptr;
+ while (caplen > 0) {
+ u32 = read_config(&sel, capptr, 4);
+ pci_set_cfgdata32(pi, capptr, u32);
+ caplen -= 4;
+ capptr += 4;
+ }
+ } else if (cap == PCIY_MSIX) {
+ /*
+ * Copy the MSI-X capability
+ */
+ sc->psc_msix.capoff = ptr;
+ caplen = 12;
+ msixcap_ptr = (uint32_t*) &msixcap;
+ capptr = ptr;
+ while (caplen > 0) {
+ u32 = read_config(&sel, capptr, 4);
+ *msixcap_ptr = u32;
+ pci_set_cfgdata32(pi, capptr, u32);
+ caplen -= 4;
+ capptr += 4;
+ msixcap_ptr++;
+ }
+ }
+ ptr = read_config(&sel, ptr + PCICAP_NEXTPTR, 1);
+ }
+ }
+
+ if (sc->psc_msix.capoff != 0) {
+ pi->pi_msix.pba_bar =
+ msixcap.pba_info & PCIM_MSIX_BIR_MASK;
+ pi->pi_msix.pba_offset =
+ msixcap.pba_info & ~PCIM_MSIX_BIR_MASK;
+ pi->pi_msix.table_bar =
+ msixcap.table_info & PCIM_MSIX_BIR_MASK;
+ pi->pi_msix.table_offset =
+ msixcap.table_info & ~PCIM_MSIX_BIR_MASK;
+ pi->pi_msix.table_count = MSIX_TABLE_COUNT(msixcap.msgctrl);
+
+ /* Allocate the emulated MSI-X table array */
+ table_size = pi->pi_msix.table_count * MSIX_TABLE_ENTRY_SIZE;
+ pi->pi_msix.table = malloc(table_size);
+ bzero(pi->pi_msix.table, table_size);
+
+ /* Mask all table entries */
+ for (i = 0; i < pi->pi_msix.table_count; i++) {
+ pi->pi_msix.table[i].vector_control |=
+ PCIM_MSIX_VCTRL_MASK;
+ }
+ }
+
+#ifdef LEGACY_SUPPORT
+ /*
+ * If the passthrough device does not support MSI then craft a
+ * MSI capability for it. We link the new MSI capability at the
+ * head of the list of capabilities.
+ */
+ if ((sts & PCIM_STATUS_CAPPRESENT) != 0 && sc->psc_msi.capoff == 0) {
+ int origptr, msiptr;
+ origptr = read_config(&sel, PCIR_CAP_PTR, 1);
+ msiptr = passthru_add_msicap(pi, 1, origptr);
+ sc->psc_msi.capoff = msiptr;
+ sc->psc_msi.msgctrl = pci_get_cfgdata16(pi, msiptr + 2);
+ sc->psc_msi.emulated = 1;
+ pci_set_cfgdata8(pi, PCIR_CAP_PTR, msiptr);
+ }
+#endif
+
+ /* Make sure one of the capabilities is present */
+ if (sc->psc_msi.capoff == 0 && sc->psc_msix.capoff == 0)
+ return (-1);
+ else
+ return (0);
+}
+
+static uint64_t
+msix_table_read(struct passthru_softc *sc, uint64_t offset, int size)
+{
+ struct pci_devinst *pi;
+ struct msix_table_entry *entry;
+ uint8_t *src8;
+ uint16_t *src16;
+ uint32_t *src32;
+ uint64_t *src64;
+ uint64_t data;
+ size_t entry_offset;
+ int index;
+
+ pi = sc->psc_pi;
+
+ index = offset / MSIX_TABLE_ENTRY_SIZE;
+ if (index >= pi->pi_msix.table_count)
+ return (-1);
+
+ entry = &pi->pi_msix.table[index];
+ entry_offset = offset % MSIX_TABLE_ENTRY_SIZE;
+
+ switch(size) {
+ case 1:
+ src8 = (uint8_t *)((void *)entry + entry_offset);
+ data = *src8;
+ break;
+ case 2:
+ src16 = (uint16_t *)((void *)entry + entry_offset);
+ data = *src16;
+ break;
+ case 4:
+ src32 = (uint32_t *)((void *)entry + entry_offset);
+ data = *src32;
+ break;
+ case 8:
+ src64 = (uint64_t *)((void *)entry + entry_offset);
+ data = *src64;
+ break;
+ default:
+ return (-1);
+ }
+
+ return (data);
+}
+
+static void
+msix_table_write(struct vmctx *ctx, int vcpu, struct passthru_softc *sc,
+ uint64_t offset, int size, uint64_t data)
+{
+ struct pci_devinst *pi;
+ struct msix_table_entry *entry;
+ uint32_t *dest;
+ size_t entry_offset;
+ uint32_t vector_control;
+ int error, index;
+
+ pi = sc->psc_pi;
+ index = offset / MSIX_TABLE_ENTRY_SIZE;
+ if (index >= pi->pi_msix.table_count)
+ return;
+
+ entry = &pi->pi_msix.table[index];
+ entry_offset = offset % MSIX_TABLE_ENTRY_SIZE;
+
+ /* Only 4 byte naturally-aligned writes are supported */
+ assert(size == 4);
+ assert(entry_offset % 4 == 0);
+
+ vector_control = entry->vector_control;
+ dest = (uint32_t *)((void *)entry + entry_offset);
+ *dest = data;
+ /* If MSI-X hasn't been enabled, do nothing */
+ if (pi->pi_msix.enabled) {
+ /* If the entry is masked, don't set it up */
+ if ((entry->vector_control & PCIM_MSIX_VCTRL_MASK) == 0 ||
+ (vector_control & PCIM_MSIX_VCTRL_MASK) == 0) {
+ error = vm_setup_msix(ctx, vcpu, sc->psc_sel.pc_bus,
+ sc->psc_sel.pc_dev,
+ sc->psc_sel.pc_func,
+ index, entry->msg_data,
+ entry->vector_control,
+ entry->addr);
+ }
+ }
+}
+
+static int
+init_msix_table(struct vmctx *ctx, struct passthru_softc *sc, uint64_t base)
+{
+ int b, s, f;
+ int error, idx;
+ size_t len, remaining, table_size;
+ vm_paddr_t start;
+ struct pci_devinst *pi = sc->psc_pi;
+
+ assert(pci_msix_table_bar(pi) >= 0 && pci_msix_pba_bar(pi) >= 0);
+
+ b = sc->psc_sel.pc_bus;
+ s = sc->psc_sel.pc_dev;
+ f = sc->psc_sel.pc_func;
+
+ /*
+ * If the MSI-X table BAR maps memory intended for
+ * other uses, it is at least assured that the table
+ * either resides in its own page within the region,
+ * or it resides in a page shared with only the PBA.
+ */
+ if (pi->pi_msix.pba_bar == pi->pi_msix.table_bar &&
+ ((pi->pi_msix.pba_offset - pi->pi_msix.table_offset) < 4096)) {
+ /* Need to also emulate the PBA, not supported yet */
+ printf("Unsupported MSI-X configuration: %d/%d/%d\n", b, s, f);
+ return (-1);
+ }
+
+ /* Compute the MSI-X table size */
+ table_size = pi->pi_msix.table_count * MSIX_TABLE_ENTRY_SIZE;
+ table_size = roundup2(table_size, 4096);
+
+ idx = pi->pi_msix.table_bar;
+ start = pi->pi_bar[idx].addr;
+ remaining = pi->pi_bar[idx].size;
+
+ /* Map everything before the MSI-X table */
+ if (pi->pi_msix.table_offset > 0) {
+ len = pi->pi_msix.table_offset;
+ error = vm_map_pptdev_mmio(ctx, b, s, f, start, len, base);
+ if (error)
+ return (error);
+
+ base += len;
+ start += len;
+ remaining -= len;
+ }
+
+ /* Skip the MSI-X table */
+ base += table_size;
+ start += table_size;
+ remaining -= table_size;
+
+ /* Map everything beyond the end of the MSI-X table */
+ if (remaining > 0) {
+ len = remaining;
+ error = vm_map_pptdev_mmio(ctx, b, s, f, start, len, base);
+ if (error)
+ return (error);
+ }
+
+ return (0);
+}
+
+static int
+cfginitbar(struct vmctx *ctx, struct passthru_softc *sc)
+{
+ int i, error;
+ struct pci_devinst *pi;
+ struct pci_bar_io bar;
+ enum pcibar_type bartype;
+ uint64_t base;
+
+ pi = sc->psc_pi;
+
+ /*
+ * Initialize BAR registers
+ */
+ for (i = 0; i <= PCI_BARMAX; i++) {
+ bzero(&bar, sizeof(bar));
+ bar.pbi_sel = sc->psc_sel;
+ bar.pbi_reg = PCIR_BAR(i);
+
+ if (ioctl(pcifd, PCIOCGETBAR, &bar) < 0)
+ continue;
+
+ if (PCI_BAR_IO(bar.pbi_base)) {
+ bartype = PCIBAR_IO;
+ base = bar.pbi_base & PCIM_BAR_IO_BASE;
+ } else {
+ switch (bar.pbi_base & PCIM_BAR_MEM_TYPE) {
+ case PCIM_BAR_MEM_64:
+ bartype = PCIBAR_MEM64;
+ break;
+ default:
+ bartype = PCIBAR_MEM32;
+ break;
+ }
+ base = bar.pbi_base & PCIM_BAR_MEM_BASE;
+ }
+
+ /* Cache information about the "real" BAR */
+ sc->psc_bar[i].type = bartype;
+ sc->psc_bar[i].size = bar.pbi_length;
+ sc->psc_bar[i].addr = base;
+
+ /* Allocate the BAR in the guest I/O or MMIO space */
+ error = pci_emul_alloc_pbar(pi, i, base, bartype,
+ bar.pbi_length);
+ if (error)
+ return (-1);
+
+ /* The MSI-X table needs special handling */
+ if (i == pci_msix_table_bar(pi)) {
+ error = init_msix_table(ctx, sc, base);
+ if (error)
+ return (-1);
+ } else if (bartype != PCIBAR_IO) {
+ /* Map the physical MMIO space in the guest MMIO space */
+ error = vm_map_pptdev_mmio(ctx, sc->psc_sel.pc_bus,
+ sc->psc_sel.pc_dev, sc->psc_sel.pc_func,
+ pi->pi_bar[i].addr, pi->pi_bar[i].size, base);
+ if (error)
+ return (-1);
+ }
+
+ /*
+ * 64-bit BAR takes up two slots so skip the next one.
+ */
+ if (bartype == PCIBAR_MEM64) {
+ i++;
+ assert(i <= PCI_BARMAX);
+ sc->psc_bar[i].type = PCIBAR_MEMHI64;
+ }
+ }
+ return (0);
+}
+
+static int
+cfginit(struct vmctx *ctx, struct pci_devinst *pi, int bus, int slot, int func)
+{
+ int error;
+ struct passthru_softc *sc;
+
+ error = 1;
+ sc = pi->pi_arg;
+
+ bzero(&sc->psc_sel, sizeof(struct pcisel));
+ sc->psc_sel.pc_bus = bus;
+ sc->psc_sel.pc_dev = slot;
+ sc->psc_sel.pc_func = func;
+
+ if (cfginitmsi(sc) != 0)
+ goto done;
+
+ if (cfginitbar(ctx, sc) != 0)
+ goto done;
+
+ error = 0; /* success */
+done:
+ return (error);
+}
+
+static int
+passthru_init(struct vmctx *ctx, struct pci_devinst *pi, char *opts)
+{
+ int bus, slot, func, error;
+ struct passthru_softc *sc;
+
+ sc = NULL;
+ error = 1;
+
+ if (pcifd < 0) {
+ pcifd = open(_PATH_DEVPCI, O_RDWR, 0);
+ if (pcifd < 0)
+ goto done;
+ }
+
+ if (iofd < 0) {
+ iofd = open(_PATH_DEVIO, O_RDWR, 0);
+ if (iofd < 0)
+ goto done;
+ }
+
+ if (opts == NULL ||
+ sscanf(opts, "%d/%d/%d", &bus, &slot, &func) != 3)
+ goto done;
+
+ if (vm_assign_pptdev(ctx, bus, slot, func) != 0)
+ goto done;
+
+ sc = malloc(sizeof(struct passthru_softc));
+ memset(sc, 0, sizeof(struct passthru_softc));
+
+ pi->pi_arg = sc;
+ sc->psc_pi = pi;
+
+ /* initialize config space */
+ if ((error = cfginit(ctx, pi, bus, slot, func)) != 0)
+ goto done;
+
+ error = 0; /* success */
+done:
+ if (error) {
+ free(sc);
+ vm_unassign_pptdev(ctx, bus, slot, func);
+ }
+ return (error);
+}
+
+static int
+bar_access(int coff)
+{
+ if (coff >= PCIR_BAR(0) && coff < PCIR_BAR(PCI_BARMAX + 1))
+ return (1);
+ else
+ return (0);
+}
+
+static int
+msicap_access(struct passthru_softc *sc, int coff)
+{
+ int caplen;
+
+ if (sc->psc_msi.capoff == 0)
+ return (0);
+
+ caplen = msi_caplen(sc->psc_msi.msgctrl);
+
+ if (coff >= sc->psc_msi.capoff && coff < sc->psc_msi.capoff + caplen)
+ return (1);
+ else
+ return (0);
+}
+
+static int
+msixcap_access(struct passthru_softc *sc, int coff)
+{
+ if (sc->psc_msix.capoff == 0)
+ return (0);
+
+ return (coff >= sc->psc_msix.capoff &&
+ coff < sc->psc_msix.capoff + MSIX_CAPLEN);
+}
+
+static int
+passthru_cfgread(struct vmctx *ctx, int vcpu, struct pci_devinst *pi,
+ int coff, int bytes, uint32_t *rv)
+{
+ struct passthru_softc *sc;
+
+ sc = pi->pi_arg;
+
+ /*
+ * PCI BARs and MSI capability is emulated.
+ */
+ if (bar_access(coff) || msicap_access(sc, coff))
+ return (-1);
+
+#ifdef LEGACY_SUPPORT
+ /*
+ * Emulate PCIR_CAP_PTR if this device does not support MSI capability
+ * natively.
+ */
+ if (sc->psc_msi.emulated) {
+ if (coff >= PCIR_CAP_PTR && coff < PCIR_CAP_PTR + 4)
+ return (-1);
+ }
+#endif
+
+ /* Everything else just read from the device's config space */
+ *rv = read_config(&sc->psc_sel, coff, bytes);
+
+ return (0);
+}
+
+static int
+passthru_cfgwrite(struct vmctx *ctx, int vcpu, struct pci_devinst *pi,
+ int coff, int bytes, uint32_t val)
+{
+ int error, msix_table_entries, i;
+ struct passthru_softc *sc;
+
+ sc = pi->pi_arg;
+
+ /*
+ * PCI BARs are emulated
+ */
+ if (bar_access(coff))
+ return (-1);
+
+ /*
+ * MSI capability is emulated
+ */
+ if (msicap_access(sc, coff)) {
+ msicap_cfgwrite(pi, sc->psc_msi.capoff, coff, bytes, val);
+
+ error = vm_setup_msi(ctx, vcpu, sc->psc_sel.pc_bus,
+ sc->psc_sel.pc_dev, sc->psc_sel.pc_func, pi->pi_msi.cpu,
+ pi->pi_msi.vector, pi->pi_msi.msgnum);
+ if (error != 0) {
+ printf("vm_setup_msi returned error %d\r\n", errno);
+ exit(1);
+ }
+ return (0);
+ }
+
+ if (msixcap_access(sc, coff)) {
+ msixcap_cfgwrite(pi, sc->psc_msix.capoff, coff, bytes, val);
+ if (pi->pi_msix.enabled) {
+ msix_table_entries = pi->pi_msix.table_count;
+ for (i = 0; i < msix_table_entries; i++) {
+ error = vm_setup_msix(ctx, vcpu, sc->psc_sel.pc_bus,
+ sc->psc_sel.pc_dev,
+ sc->psc_sel.pc_func, i,
+ pi->pi_msix.table[i].msg_data,
+ pi->pi_msix.table[i].vector_control,
+ pi->pi_msix.table[i].addr);
+
+ if (error) {
+ printf("vm_setup_msix returned error %d\r\n", errno);
+ exit(1);
+ }
+ }
+ }
+ return (0);
+ }
+
+#ifdef LEGACY_SUPPORT
+ /*
+ * If this device does not support MSI natively then we cannot let
+ * the guest disable legacy interrupts from the device. It is the
+ * legacy interrupt that is triggering the virtual MSI to the guest.
+ */
+ if (sc->psc_msi.emulated && pci_msi_enabled(pi)) {
+ if (coff == PCIR_COMMAND && bytes == 2)
+ val &= ~PCIM_CMD_INTxDIS;
+ }
+#endif
+
+ write_config(&sc->psc_sel, coff, bytes, val);
+
+ return (0);
+}
+
+static void
+passthru_write(struct vmctx *ctx, int vcpu, struct pci_devinst *pi, int baridx,
+ uint64_t offset, int size, uint64_t value)
+{
+ struct passthru_softc *sc;
+ struct iodev_pio_req pio;
+
+ sc = pi->pi_arg;
+
+ if (baridx == pci_msix_table_bar(pi)) {
+ msix_table_write(ctx, vcpu, sc, offset, size, value);
+ } else {
+ assert(pi->pi_bar[baridx].type == PCIBAR_IO);
+ bzero(&pio, sizeof(struct iodev_pio_req));
+ pio.access = IODEV_PIO_WRITE;
+ pio.port = sc->psc_bar[baridx].addr + offset;
+ pio.width = size;
+ pio.val = value;
+
+ (void)ioctl(iofd, IODEV_PIO, &pio);
+ }
+}
+
+static uint64_t
+passthru_read(struct vmctx *ctx, int vcpu, struct pci_devinst *pi, int baridx,
+ uint64_t offset, int size)
+{
+ struct passthru_softc *sc;
+ struct iodev_pio_req pio;
+ uint64_t val;
+
+ sc = pi->pi_arg;
+
+ if (baridx == pci_msix_table_bar(pi)) {
+ val = msix_table_read(sc, offset, size);
+ } else {
+ assert(pi->pi_bar[baridx].type == PCIBAR_IO);
+ bzero(&pio, sizeof(struct iodev_pio_req));
+ pio.access = IODEV_PIO_READ;
+ pio.port = sc->psc_bar[baridx].addr + offset;
+ pio.width = size;
+ pio.val = 0;
+
+ (void)ioctl(iofd, IODEV_PIO, &pio);
+
+ val = pio.val;
+ }
+
+ return (val);
+}
+
+struct pci_devemu passthru = {
+ .pe_emu = "passthru",
+ .pe_init = passthru_init,
+ .pe_cfgwrite = passthru_cfgwrite,
+ .pe_cfgread = passthru_cfgread,
+ .pe_barwrite = passthru_write,
+ .pe_barread = passthru_read,
+};
+PCI_EMUL_SET(passthru);
diff --git a/usr.sbin/bhyve/pci_uart.c b/usr.sbin/bhyve/pci_uart.c
new file mode 100644
index 0000000..dd30551
--- /dev/null
+++ b/usr.sbin/bhyve/pci_uart.c
@@ -0,0 +1,626 @@
+/*-
+ * Copyright (c) 2012 NetApp, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <sys/types.h>
+#include <sys/select.h>
+#include <dev/ic/ns16550.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <assert.h>
+#include <termios.h>
+#include <unistd.h>
+#include <stdbool.h>
+#include <string.h>
+#include <pthread.h>
+
+#include "bhyverun.h"
+#include "pci_emul.h"
+#include "mevent.h"
+
+#define COM1_BASE 0x3F8
+#define COM1_IRQ 4
+#define COM2_BASE 0x2F8
+#define COM2_IRQ 3
+
+#define DEFAULT_RCLK 1843200
+#define DEFAULT_BAUD 9600
+
+#define FCR_RX_MASK 0xC0
+
+#define MCR_OUT1 0x04
+#define MCR_OUT2 0x08
+
+#define MSR_DELTA_MASK 0x0f
+
+#ifndef REG_SCR
+#define REG_SCR com_scr
+#endif
+
+#define FIFOSZ 16
+
+/*
+ * Pick a PCI vid/did of a chip with a single uart at
+ * BAR0, that most versions of FreeBSD can understand:
+ * Siig CyberSerial 1-port.
+ */
+#define COM_VENDOR 0x131f
+#define COM_DEV 0x2000
+
+static int pci_uart_stdio; /* stdio in use for i/o */
+
+static int pci_uart_nldevs; /* number of legacy devices - 2 max */
+
+static struct {
+ uint64_t baddr;
+ int vector;
+} pci_uart_lres[] = {
+ { COM1_BASE, COM1_IRQ},
+ { COM2_BASE, COM2_IRQ},
+ { 0, 0 }
+};
+
+struct fifo {
+ uint8_t buf[FIFOSZ];
+ int rindex; /* index to read from */
+ int windex; /* index to write to */
+ int num; /* number of characters in the fifo */
+ int size; /* size of the fifo */
+};
+
+struct pci_uart_softc {
+ struct pci_devinst *pi;
+ pthread_mutex_t mtx; /* protects all softc elements */
+ uint8_t data; /* Data register (R/W) */
+ uint8_t ier; /* Interrupt enable register (R/W) */
+ uint8_t lcr; /* Line control register (R/W) */
+ uint8_t mcr; /* Modem control register (R/W) */
+ uint8_t lsr; /* Line status register (R/W) */
+ uint8_t msr; /* Modem status register (R/W) */
+ uint8_t fcr; /* FIFO control register (W) */
+ uint8_t scr; /* Scratch register (R/W) */
+
+ uint8_t dll; /* Baudrate divisor latch LSB */
+ uint8_t dlh; /* Baudrate divisor latch MSB */
+
+ struct fifo rxfifo;
+
+ int opened;
+ int stdio;
+ bool thre_int_pending; /* THRE interrupt pending */
+};
+
+static void pci_uart_drain(int fd, enum ev_type ev, void *arg);
+
+static struct termios tio_orig, tio_new; /* I/O Terminals */
+
+static void
+ttyclose(void)
+{
+ tcsetattr(STDIN_FILENO, TCSANOW, &tio_orig);
+}
+
+static void
+ttyopen(void)
+{
+ tcgetattr(STDIN_FILENO, &tio_orig);
+
+ cfmakeraw(&tio_new);
+ tcsetattr(STDIN_FILENO, TCSANOW, &tio_new);
+
+ atexit(ttyclose);
+}
+
+static bool
+tty_char_available(void)
+{
+ fd_set rfds;
+ struct timeval tv;
+
+ FD_ZERO(&rfds);
+ FD_SET(STDIN_FILENO, &rfds);
+ tv.tv_sec = 0;
+ tv.tv_usec = 0;
+ if (select(STDIN_FILENO + 1, &rfds, NULL, NULL, &tv) > 0 ) {
+ return (true);
+ } else {
+ return (false);
+ }
+}
+
+static int
+ttyread(void)
+{
+ char rb;
+
+ if (tty_char_available()) {
+ read(STDIN_FILENO, &rb, 1);
+ return (rb & 0xff);
+ } else {
+ return (-1);
+ }
+}
+
+static void
+ttywrite(unsigned char wb)
+{
+ (void) write(STDIN_FILENO, &wb, 1);
+}
+
+static void
+fifo_reset(struct fifo *fifo, int size)
+{
+ bzero(fifo, sizeof(struct fifo));
+ fifo->size = size;
+}
+
+static int
+fifo_putchar(struct fifo *fifo, uint8_t ch)
+{
+
+ if (fifo->num < fifo->size) {
+ fifo->buf[fifo->windex] = ch;
+ fifo->windex = (fifo->windex + 1) % fifo->size;
+ fifo->num++;
+ return (0);
+ } else
+ return (-1);
+}
+
+static int
+fifo_getchar(struct fifo *fifo)
+{
+ int c;
+
+ if (fifo->num > 0) {
+ c = fifo->buf[fifo->rindex];
+ fifo->rindex = (fifo->rindex + 1) % fifo->size;
+ fifo->num--;
+ return (c);
+ } else
+ return (-1);
+}
+
+static int
+fifo_numchars(struct fifo *fifo)
+{
+
+ return (fifo->num);
+}
+
+static int
+fifo_available(struct fifo *fifo)
+{
+
+ return (fifo->num < fifo->size);
+}
+
+static void
+pci_uart_opentty(struct pci_uart_softc *sc)
+{
+ struct mevent *mev;
+
+ assert(sc->opened == 0);
+ assert(sc->stdio);
+
+ ttyopen();
+ mev = mevent_add(STDIN_FILENO, EVF_READ, pci_uart_drain, sc);
+ assert(mev);
+}
+
+static void
+pci_uart_legacy_res(uint64_t *bar, int *ivec)
+{
+ if (pci_uart_lres[pci_uart_nldevs].baddr != 0) {
+ *bar = pci_uart_lres[pci_uart_nldevs].baddr;
+ *ivec = pci_uart_lres[pci_uart_nldevs].vector;
+ pci_uart_nldevs++;
+ } else {
+ /* TODO: print warning ? */
+ *bar = 0;
+ *ivec= -1;
+ }
+}
+
+/*
+ * The IIR returns a prioritized interrupt reason:
+ * - receive data available
+ * - transmit holding register empty
+ * - modem status change
+ *
+ * Return an interrupt reason if one is available.
+ */
+static int
+pci_uart_intr_reason(struct pci_uart_softc *sc)
+{
+
+ if ((sc->lsr & LSR_OE) != 0 && (sc->ier & IER_ERLS) != 0)
+ return (IIR_RLS);
+ else if (fifo_numchars(&sc->rxfifo) > 0 && (sc->ier & IER_ERXRDY) != 0)
+ return (IIR_RXTOUT);
+ else if (sc->thre_int_pending && (sc->ier & IER_ETXRDY) != 0)
+ return (IIR_TXRDY);
+ else if ((sc->msr & MSR_DELTA_MASK) != 0 && (sc->ier & IER_EMSC) != 0)
+ return (IIR_MLSC);
+ else
+ return (IIR_NOPEND);
+}
+
+static void
+pci_uart_reset(struct pci_uart_softc *sc)
+{
+ uint16_t divisor;
+
+ divisor = DEFAULT_RCLK / DEFAULT_BAUD / 16;
+ sc->dll = divisor;
+ sc->dlh = divisor >> 16;
+
+ fifo_reset(&sc->rxfifo, 1); /* no fifo until enabled by software */
+}
+
+/*
+ * Toggle the COM port's intr pin depending on whether or not we have an
+ * interrupt condition to report to the processor.
+ */
+static void
+pci_uart_toggle_intr(struct pci_uart_softc *sc)
+{
+ uint8_t intr_reason;
+
+ intr_reason = pci_uart_intr_reason(sc);
+
+ if (intr_reason == IIR_NOPEND)
+ pci_lintr_deassert(sc->pi);
+ else
+ pci_lintr_assert(sc->pi);
+}
+
+static void
+pci_uart_drain(int fd, enum ev_type ev, void *arg)
+{
+ struct pci_uart_softc *sc;
+ int ch;
+
+ sc = arg;
+
+ assert(fd == STDIN_FILENO);
+ assert(ev == EVF_READ);
+
+ /*
+ * This routine is called in the context of the mevent thread
+ * to take out the softc lock to protect against concurrent
+ * access from a vCPU i/o exit
+ */
+ pthread_mutex_lock(&sc->mtx);
+
+ if ((sc->mcr & MCR_LOOPBACK) != 0) {
+ (void) ttyread();
+ } else {
+ while (fifo_available(&sc->rxfifo) &&
+ ((ch = ttyread()) != -1)) {
+ fifo_putchar(&sc->rxfifo, ch);
+ }
+ pci_uart_toggle_intr(sc);
+ }
+
+ pthread_mutex_unlock(&sc->mtx);
+}
+
+static void
+pci_uart_write(struct vmctx *ctx, int vcpu, struct pci_devinst *pi,
+ int baridx, uint64_t offset, int size, uint64_t value)
+{
+ struct pci_uart_softc *sc;
+ int fifosz;
+ uint8_t msr;
+
+ sc = pi->pi_arg;
+
+ assert(baridx == 0);
+ assert(size == 1);
+
+ /* Open terminal */
+ if (!sc->opened && sc->stdio) {
+ pci_uart_opentty(sc);
+ sc->opened = 1;
+ }
+
+ pthread_mutex_lock(&sc->mtx);
+
+ /*
+ * Take care of the special case DLAB accesses first
+ */
+ if ((sc->lcr & LCR_DLAB) != 0) {
+ if (offset == REG_DLL) {
+ sc->dll = value;
+ goto done;
+ }
+
+ if (offset == REG_DLH) {
+ sc->dlh = value;
+ goto done;
+ }
+ }
+
+ switch (offset) {
+ case REG_DATA:
+ if (sc->mcr & MCR_LOOPBACK) {
+ if (fifo_putchar(&sc->rxfifo, value) != 0)
+ sc->lsr |= LSR_OE;
+ } else if (sc->stdio) {
+ ttywrite(value);
+ } /* else drop on floor */
+ sc->thre_int_pending = true;
+ break;
+ case REG_IER:
+ /*
+ * Apply mask so that bits 4-7 are 0
+ * Also enables bits 0-3 only if they're 1
+ */
+ sc->ier = value & 0x0F;
+ break;
+ case REG_FCR:
+ /*
+ * When moving from FIFO and 16450 mode and vice versa,
+ * the FIFO contents are reset.
+ */
+ if ((sc->fcr & FCR_ENABLE) ^ (value & FCR_ENABLE)) {
+ fifosz = (value & FCR_ENABLE) ? FIFOSZ : 1;
+ fifo_reset(&sc->rxfifo, fifosz);
+ }
+
+ /*
+ * The FCR_ENABLE bit must be '1' for the programming
+ * of other FCR bits to be effective.
+ */
+ if ((value & FCR_ENABLE) == 0) {
+ sc->fcr = 0;
+ } else {
+ if ((value & FCR_RCV_RST) != 0)
+ fifo_reset(&sc->rxfifo, FIFOSZ);
+
+ sc->fcr = value &
+ (FCR_ENABLE | FCR_DMA | FCR_RX_MASK);
+ }
+ break;
+ case REG_LCR:
+ sc->lcr = value;
+ break;
+ case REG_MCR:
+ /* Apply mask so that bits 5-7 are 0 */
+ sc->mcr = value & 0x1F;
+
+ msr = 0;
+ if (sc->mcr & MCR_LOOPBACK) {
+ /*
+ * In the loopback mode certain bits from the
+ * MCR are reflected back into MSR
+ */
+ if (sc->mcr & MCR_RTS)
+ msr |= MSR_CTS;
+ if (sc->mcr & MCR_DTR)
+ msr |= MSR_DSR;
+ if (sc->mcr & MCR_OUT1)
+ msr |= MSR_RI;
+ if (sc->mcr & MCR_OUT2)
+ msr |= MSR_DCD;
+ }
+
+ /*
+ * Detect if there has been any change between the
+ * previous and the new value of MSR. If there is
+ * then assert the appropriate MSR delta bit.
+ */
+ if ((msr & MSR_CTS) ^ (sc->msr & MSR_CTS))
+ sc->msr |= MSR_DCTS;
+ if ((msr & MSR_DSR) ^ (sc->msr & MSR_DSR))
+ sc->msr |= MSR_DDSR;
+ if ((msr & MSR_DCD) ^ (sc->msr & MSR_DCD))
+ sc->msr |= MSR_DDCD;
+ if ((sc->msr & MSR_RI) != 0 && (msr & MSR_RI) == 0)
+ sc->msr |= MSR_TERI;
+
+ /*
+ * Update the value of MSR while retaining the delta
+ * bits.
+ */
+ sc->msr &= MSR_DELTA_MASK;
+ sc->msr |= msr;
+ break;
+ case REG_LSR:
+ /*
+ * Line status register is not meant to be written to
+ * during normal operation.
+ */
+ break;
+ case REG_MSR:
+ /*
+ * As far as I can tell MSR is a read-only register.
+ */
+ break;
+ case REG_SCR:
+ sc->scr = value;
+ break;
+ default:
+ break;
+ }
+
+done:
+ pci_uart_toggle_intr(sc);
+ pthread_mutex_unlock(&sc->mtx);
+}
+
+uint64_t
+pci_uart_read(struct vmctx *ctx, int vcpu, struct pci_devinst *pi,
+ int baridx, uint64_t offset, int size)
+{
+ struct pci_uart_softc *sc;
+ uint8_t iir, intr_reason;
+ uint64_t reg;
+
+ sc = pi->pi_arg;
+
+ assert(baridx == 0);
+ assert(size == 1);
+
+ /* Open terminal */
+ if (!sc->opened && sc->stdio) {
+ pci_uart_opentty(sc);
+ sc->opened = 1;
+ }
+
+ pthread_mutex_lock(&sc->mtx);
+
+ /*
+ * Take care of the special case DLAB accesses first
+ */
+ if ((sc->lcr & LCR_DLAB) != 0) {
+ if (offset == REG_DLL) {
+ reg = sc->dll;
+ goto done;
+ }
+
+ if (offset == REG_DLH) {
+ reg = sc->dlh;
+ goto done;
+ }
+ }
+
+ switch (offset) {
+ case REG_DATA:
+ reg = fifo_getchar(&sc->rxfifo);
+ break;
+ case REG_IER:
+ reg = sc->ier;
+ break;
+ case REG_IIR:
+ iir = (sc->fcr & FCR_ENABLE) ? IIR_FIFO_MASK : 0;
+
+ intr_reason = pci_uart_intr_reason(sc);
+
+ /*
+ * Deal with side effects of reading the IIR register
+ */
+ if (intr_reason == IIR_TXRDY)
+ sc->thre_int_pending = false;
+
+ iir |= intr_reason;
+
+ reg = iir;
+ break;
+ case REG_LCR:
+ reg = sc->lcr;
+ break;
+ case REG_MCR:
+ reg = sc->mcr;
+ break;
+ case REG_LSR:
+ /* Transmitter is always ready for more data */
+ sc->lsr |= LSR_TEMT | LSR_THRE;
+
+ /* Check for new receive data */
+ if (fifo_numchars(&sc->rxfifo) > 0)
+ sc->lsr |= LSR_RXRDY;
+ else
+ sc->lsr &= ~LSR_RXRDY;
+
+ reg = sc->lsr;
+
+ /* The LSR_OE bit is cleared on LSR read */
+ sc->lsr &= ~LSR_OE;
+ break;
+ case REG_MSR:
+ /*
+ * MSR delta bits are cleared on read
+ */
+ reg = sc->msr;
+ sc->msr &= ~MSR_DELTA_MASK;
+ break;
+ case REG_SCR:
+ reg = sc->scr;
+ break;
+ default:
+ reg = 0xFF;
+ break;
+ }
+
+done:
+ pci_uart_toggle_intr(sc);
+ pthread_mutex_unlock(&sc->mtx);
+
+ return (reg);
+}
+
+static int
+pci_uart_init(struct vmctx *ctx, struct pci_devinst *pi, char *opts)
+{
+ struct pci_uart_softc *sc;
+ uint64_t bar;
+ int ivec;
+
+ sc = malloc(sizeof(struct pci_uart_softc));
+ memset(sc, 0, sizeof(struct pci_uart_softc));
+
+ pi->pi_arg = sc;
+ sc->pi = pi;
+
+ pthread_mutex_init(&sc->mtx, NULL);
+
+ /* initialize config space */
+ pci_set_cfgdata16(pi, PCIR_DEVICE, COM_DEV);
+ pci_set_cfgdata16(pi, PCIR_VENDOR, COM_VENDOR);
+ pci_set_cfgdata8(pi, PCIR_CLASS, PCIC_SIMPLECOMM);
+ if (pci_is_legacy(pi)) {
+ pci_uart_legacy_res(&bar, &ivec);
+ pci_emul_alloc_pbar(pi, 0, bar, PCIBAR_IO, 8);
+ } else {
+ ivec = -1;
+ pci_emul_alloc_bar(pi, 0, PCIBAR_IO, 8);
+ }
+ pci_lintr_request(pi, ivec);
+
+ if (opts != NULL && !strcmp("stdio", opts) && !pci_uart_stdio) {
+ pci_uart_stdio = 1;
+ sc->stdio = 1;
+ }
+
+ pci_uart_reset(sc);
+
+ return (0);
+}
+
+struct pci_devemu pci_de_com = {
+ .pe_emu = "uart",
+ .pe_init = pci_uart_init,
+ .pe_barwrite = pci_uart_write,
+ .pe_barread = pci_uart_read
+};
+PCI_EMUL_SET(pci_de_com);
diff --git a/usr.sbin/bhyve/pci_virtio_block.c b/usr.sbin/bhyve/pci_virtio_block.c
new file mode 100644
index 0000000..c8fc914
--- /dev/null
+++ b/usr.sbin/bhyve/pci_virtio_block.c
@@ -0,0 +1,624 @@
+/*-
+ * Copyright (c) 2011 NetApp, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <sys/param.h>
+#include <sys/linker_set.h>
+#include <sys/stat.h>
+#include <sys/uio.h>
+#include <sys/ioctl.h>
+#include <sys/disk.h>
+
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <string.h>
+#include <strings.h>
+#include <unistd.h>
+#include <assert.h>
+#include <pthread.h>
+
+#include "bhyverun.h"
+#include "pci_emul.h"
+#include "virtio.h"
+
+#define VTBLK_RINGSZ 64
+
+#define VTBLK_CFGSZ 28
+
+#define VTBLK_R_CFG VTCFG_R_CFG1
+#define VTBLK_R_CFG_END VTBLK_R_CFG + VTBLK_CFGSZ -1
+#define VTBLK_R_MAX VTBLK_R_CFG_END
+
+#define VTBLK_REGSZ VTBLK_R_MAX+1
+
+#define VTBLK_MAXSEGS 32
+
+#define VTBLK_S_OK 0
+#define VTBLK_S_IOERR 1
+
+/*
+ * Host capabilities
+ */
+#define VTBLK_S_HOSTCAPS \
+ ( 0x00000004 | /* host maximum request segments */ \
+ 0x10000000 ) /* supports indirect descriptors */
+
+static int use_msix = 1;
+
+struct vring_hqueue {
+ /* Internal state */
+ uint16_t hq_size;
+ uint16_t hq_cur_aidx; /* trails behind 'avail_idx' */
+
+ /* Host-context pointers to the queue */
+ struct virtio_desc *hq_dtable;
+ uint16_t *hq_avail_flags;
+ uint16_t *hq_avail_idx; /* monotonically increasing */
+ uint16_t *hq_avail_ring;
+
+ uint16_t *hq_used_flags;
+ uint16_t *hq_used_idx; /* monotonically increasing */
+ struct virtio_used *hq_used_ring;
+};
+
+/*
+ * Config space
+ */
+struct vtblk_config {
+ uint64_t vbc_capacity;
+ uint32_t vbc_size_max;
+ uint32_t vbc_seg_max;
+ uint16_t vbc_geom_c;
+ uint8_t vbc_geom_h;
+ uint8_t vbc_geom_s;
+ uint32_t vbc_blk_size;
+ uint32_t vbc_sectors_max;
+} __packed;
+CTASSERT(sizeof(struct vtblk_config) == VTBLK_CFGSZ);
+
+/*
+ * Fixed-size block header
+ */
+struct virtio_blk_hdr {
+#define VBH_OP_READ 0
+#define VBH_OP_WRITE 1
+ uint32_t vbh_type;
+ uint32_t vbh_ioprio;
+ uint64_t vbh_sector;
+} __packed;
+
+/*
+ * Debug printf
+ */
+static int pci_vtblk_debug;
+#define DPRINTF(params) if (pci_vtblk_debug) printf params
+#define WPRINTF(params) printf params
+
+/*
+ * Per-device softc
+ */
+struct pci_vtblk_softc {
+ struct pci_devinst *vbsc_pi;
+ int vbsc_fd;
+ int vbsc_status;
+ int vbsc_isr;
+ int vbsc_lastq;
+ uint32_t vbsc_features;
+ uint64_t vbsc_pfn;
+ struct vring_hqueue vbsc_q;
+ struct vtblk_config vbsc_cfg;
+ uint16_t msix_table_idx_req;
+ uint16_t msix_table_idx_cfg;
+};
+
+/*
+ * Return the size of IO BAR that maps virtio header and device specific
+ * region. The size would vary depending on whether MSI-X is enabled or
+ * not
+ */
+static uint64_t
+pci_vtblk_iosize(struct pci_devinst *pi)
+{
+
+ if (pci_msix_enabled(pi))
+ return (VTBLK_REGSZ);
+ else
+ return (VTBLK_REGSZ - (VTCFG_R_CFG1 - VTCFG_R_MSIX));
+}
+
+/*
+ * Return the number of available descriptors in the vring taking care
+ * of the 16-bit index wraparound.
+ */
+static int
+hq_num_avail(struct vring_hqueue *hq)
+{
+ int ndesc;
+
+ if (*hq->hq_avail_idx >= hq->hq_cur_aidx)
+ ndesc = *hq->hq_avail_idx - hq->hq_cur_aidx;
+ else
+ ndesc = UINT16_MAX - hq->hq_cur_aidx + *hq->hq_avail_idx + 1;
+
+ assert(ndesc >= 0 && ndesc <= hq->hq_size);
+
+ return (ndesc);
+}
+
+static void
+pci_vtblk_update_status(struct pci_vtblk_softc *sc, uint32_t value)
+{
+ if (value == 0) {
+ DPRINTF(("vtblk: device reset requested !\n"));
+ }
+
+ sc->vbsc_status = value;
+}
+
+static void
+pci_vtblk_proc(struct pci_vtblk_softc *sc, struct vring_hqueue *hq)
+{
+ struct iovec iov[VTBLK_MAXSEGS];
+ struct virtio_blk_hdr *vbh;
+ struct virtio_desc *vd, *vid;
+ struct virtio_used *vu;
+ uint8_t *status;
+ int i;
+ int err;
+ int iolen;
+ int nsegs;
+ int uidx, aidx, didx;
+ int writeop;
+ off_t offset;
+
+ uidx = *hq->hq_used_idx;
+ aidx = hq->hq_cur_aidx;
+ didx = hq->hq_avail_ring[aidx % hq->hq_size];
+ assert(didx >= 0 && didx < hq->hq_size);
+
+ vd = &hq->hq_dtable[didx];
+
+ /*
+ * Verify that the descriptor is indirect, and obtain
+ * the pointer to the indirect descriptor.
+ * There has to be space for at least 3 descriptors
+ * in the indirect descriptor array: the block header,
+ * 1 or more data descriptors, and a status byte.
+ */
+ assert(vd->vd_flags & VRING_DESC_F_INDIRECT);
+
+ nsegs = vd->vd_len / sizeof(struct virtio_desc);
+ assert(nsegs >= 3);
+ assert(nsegs < VTBLK_MAXSEGS + 2);
+
+ vid = paddr_guest2host(vd->vd_addr);
+ assert((vid->vd_flags & VRING_DESC_F_INDIRECT) == 0);
+
+ /*
+ * The first descriptor will be the read-only fixed header
+ */
+ vbh = paddr_guest2host(vid[0].vd_addr);
+ assert(vid[0].vd_len == sizeof(struct virtio_blk_hdr));
+ assert(vid[0].vd_flags & VRING_DESC_F_NEXT);
+ assert((vid[0].vd_flags & VRING_DESC_F_WRITE) == 0);
+
+ writeop = (vbh->vbh_type == VBH_OP_WRITE);
+
+ offset = vbh->vbh_sector * DEV_BSIZE;
+
+ /*
+ * Build up the iovec based on the guest's data descriptors
+ */
+ for (i = 1, iolen = 0; i < nsegs - 1; i++) {
+ iov[i-1].iov_base = paddr_guest2host(vid[i].vd_addr);
+ iov[i-1].iov_len = vid[i].vd_len;
+ iolen += vid[i].vd_len;
+
+ assert(vid[i].vd_flags & VRING_DESC_F_NEXT);
+ assert((vid[i].vd_flags & VRING_DESC_F_INDIRECT) == 0);
+
+ /*
+ * - write op implies read-only descriptor,
+ * - read op implies write-only descriptor,
+ * therefore test the inverse of the descriptor bit
+ * to the op.
+ */
+ assert(((vid[i].vd_flags & VRING_DESC_F_WRITE) == 0) ==
+ writeop);
+ }
+
+ /* Lastly, get the address of the status byte */
+ status = paddr_guest2host(vid[nsegs - 1].vd_addr);
+ assert(vid[nsegs - 1].vd_len == 1);
+ assert((vid[nsegs - 1].vd_flags & VRING_DESC_F_NEXT) == 0);
+ assert(vid[nsegs - 1].vd_flags & VRING_DESC_F_WRITE);
+
+ DPRINTF(("virtio-block: %s op, %d bytes, %d segs, offset %ld\n\r",
+ writeop ? "write" : "read", iolen, nsegs - 2, offset));
+
+ if (writeop){
+ err = pwritev(sc->vbsc_fd, iov, nsegs - 2, offset);
+ } else {
+ err = preadv(sc->vbsc_fd, iov, nsegs - 2, offset);
+ }
+
+ *status = err < 0 ? VTBLK_S_IOERR : VTBLK_S_OK;
+
+ /*
+ * Return the single indirect descriptor back to the host
+ */
+ vu = &hq->hq_used_ring[uidx % hq->hq_size];
+ vu->vu_idx = didx;
+ vu->vu_tlen = 1;
+ hq->hq_cur_aidx++;
+ *hq->hq_used_idx += 1;
+}
+
+static void
+pci_vtblk_qnotify(struct pci_vtblk_softc *sc)
+{
+ struct vring_hqueue *hq = &sc->vbsc_q;
+ int i;
+ int ndescs;
+
+ /*
+ * Calculate number of ring entries to process
+ */
+ ndescs = hq_num_avail(hq);
+
+ if (ndescs == 0)
+ return;
+
+ /*
+ * Run through all the entries, placing them into iovecs and
+ * sending when an end-of-packet is found
+ */
+ for (i = 0; i < ndescs; i++)
+ pci_vtblk_proc(sc, hq);
+
+ /*
+ * Generate an interrupt if able
+ */
+ if ((*hq->hq_avail_flags & VRING_AVAIL_F_NO_INTERRUPT) == 0) {
+ if (use_msix) {
+ pci_generate_msix(sc->vbsc_pi, sc->msix_table_idx_req);
+ } else if (sc->vbsc_isr == 0) {
+ sc->vbsc_isr = 1;
+ pci_generate_msi(sc->vbsc_pi, 0);
+ }
+ }
+
+}
+
+static void
+pci_vtblk_ring_init(struct pci_vtblk_softc *sc, uint64_t pfn)
+{
+ struct vring_hqueue *hq;
+
+ sc->vbsc_pfn = pfn << VRING_PFN;
+
+ /*
+ * Set up host pointers to the various parts of the
+ * queue
+ */
+ hq = &sc->vbsc_q;
+ hq->hq_size = VTBLK_RINGSZ;
+
+ hq->hq_dtable = paddr_guest2host(pfn << VRING_PFN);
+ hq->hq_avail_flags = (uint16_t *)(hq->hq_dtable + hq->hq_size);
+ hq->hq_avail_idx = hq->hq_avail_flags + 1;
+ hq->hq_avail_ring = hq->hq_avail_flags + 2;
+ hq->hq_used_flags = (uint16_t *)roundup2((uintptr_t)hq->hq_avail_ring,
+ VRING_ALIGN);
+ hq->hq_used_idx = hq->hq_used_flags + 1;
+ hq->hq_used_ring = (struct virtio_used *)(hq->hq_used_flags + 2);
+
+ /*
+ * Initialize queue indexes
+ */
+ hq->hq_cur_aidx = 0;
+}
+
+static int
+pci_vtblk_init(struct vmctx *ctx, struct pci_devinst *pi, char *opts)
+{
+ struct stat sbuf;
+ struct pci_vtblk_softc *sc;
+ off_t size;
+ int fd;
+ int sectsz;
+ const char *env_msi;
+
+ if (opts == NULL) {
+ printf("virtio-block: backing device required\n");
+ return (1);
+ }
+
+ /*
+ * Access to guest memory is required. Fail if
+ * memory not mapped
+ */
+ if (paddr_guest2host(0) == NULL)
+ return (1);
+
+ /*
+ * The supplied backing file has to exist
+ */
+ fd = open(opts, O_RDWR);
+ if (fd < 0) {
+ perror("Could not open backing file");
+ return (1);
+ }
+
+ if (fstat(fd, &sbuf) < 0) {
+ perror("Could not stat backing file");
+ close(fd);
+ return (1);
+ }
+
+ /*
+ * Deal with raw devices
+ */
+ size = sbuf.st_size;
+ sectsz = DEV_BSIZE;
+ if (S_ISCHR(sbuf.st_mode)) {
+ if (ioctl(fd, DIOCGMEDIASIZE, &size) < 0 ||
+ ioctl(fd, DIOCGSECTORSIZE, &sectsz)) {
+ perror("Could not fetch dev blk/sector size");
+ close(fd);
+ return (1);
+ }
+ assert(size != 0);
+ assert(sectsz != 0);
+ }
+
+ sc = malloc(sizeof(struct pci_vtblk_softc));
+ memset(sc, 0, sizeof(struct pci_vtblk_softc));
+
+ pi->pi_arg = sc;
+ sc->vbsc_pi = pi;
+ sc->vbsc_fd = fd;
+
+ /* setup virtio block config space */
+ sc->vbsc_cfg.vbc_capacity = size / sectsz;
+ sc->vbsc_cfg.vbc_seg_max = VTBLK_MAXSEGS;
+ sc->vbsc_cfg.vbc_blk_size = sectsz;
+ sc->vbsc_cfg.vbc_size_max = 0; /* not negotiated */
+ sc->vbsc_cfg.vbc_geom_c = 0; /* no geometry */
+ sc->vbsc_cfg.vbc_geom_h = 0;
+ sc->vbsc_cfg.vbc_geom_s = 0;
+ sc->vbsc_cfg.vbc_sectors_max = 0;
+
+ /* initialize config space */
+ pci_set_cfgdata16(pi, PCIR_DEVICE, VIRTIO_DEV_BLOCK);
+ pci_set_cfgdata16(pi, PCIR_VENDOR, VIRTIO_VENDOR);
+ pci_set_cfgdata8(pi, PCIR_CLASS, PCIC_STORAGE);
+ pci_set_cfgdata16(pi, PCIR_SUBDEV_0, VIRTIO_TYPE_BLOCK);
+
+ if ((env_msi = getenv("BHYVE_USE_MSI"))) {
+ if (strcasecmp(env_msi, "yes") == 0)
+ use_msix = 0;
+ }
+
+ if (use_msix) {
+ /* MSI-X Support */
+ sc->msix_table_idx_req = VIRTIO_MSI_NO_VECTOR;
+ sc->msix_table_idx_cfg = VIRTIO_MSI_NO_VECTOR;
+
+ if (pci_emul_add_msixcap(pi, 2, 1))
+ return (1);
+ } else {
+ /* MSI Support */
+ pci_emul_add_msicap(pi, 1);
+ }
+
+ pci_emul_alloc_bar(pi, 0, PCIBAR_IO, VTBLK_REGSZ);
+
+ return (0);
+}
+
+static uint64_t
+vtblk_adjust_offset(struct pci_devinst *pi, uint64_t offset)
+{
+ /*
+ * Device specific offsets used by guest would change
+ * based on whether MSI-X capability is enabled or not
+ */
+ if (!pci_msix_enabled(pi)) {
+ if (offset >= VTCFG_R_MSIX)
+ return (offset + (VTCFG_R_CFG1 - VTCFG_R_MSIX));
+ }
+
+ return (offset);
+}
+
+static void
+pci_vtblk_write(struct vmctx *ctx, int vcpu, struct pci_devinst *pi,
+ int baridx, uint64_t offset, int size, uint64_t value)
+{
+ struct pci_vtblk_softc *sc = pi->pi_arg;
+
+ if (use_msix) {
+ if (baridx == pci_msix_table_bar(pi) ||
+ baridx == pci_msix_pba_bar(pi)) {
+ pci_emul_msix_twrite(pi, offset, size, value);
+ return;
+ }
+ }
+
+ assert(baridx == 0);
+
+ if (offset + size > pci_vtblk_iosize(pi)) {
+ DPRINTF(("vtblk_write: 2big, offset %ld size %d\n",
+ offset, size));
+ return;
+ }
+
+ offset = vtblk_adjust_offset(pi, offset);
+
+ switch (offset) {
+ case VTCFG_R_GUESTCAP:
+ assert(size == 4);
+ sc->vbsc_features = value & VTBLK_S_HOSTCAPS;
+ break;
+ case VTCFG_R_PFN:
+ assert(size == 4);
+ pci_vtblk_ring_init(sc, value);
+ break;
+ case VTCFG_R_QSEL:
+ assert(size == 2);
+ sc->vbsc_lastq = value;
+ break;
+ case VTCFG_R_QNOTIFY:
+ assert(size == 2);
+ assert(value == 0);
+ pci_vtblk_qnotify(sc);
+ break;
+ case VTCFG_R_STATUS:
+ assert(size == 1);
+ pci_vtblk_update_status(sc, value);
+ break;
+ case VTCFG_R_CFGVEC:
+ assert(size == 2);
+ sc->msix_table_idx_cfg = value;
+ break;
+ case VTCFG_R_QVEC:
+ assert(size == 2);
+ sc->msix_table_idx_req = value;
+ break;
+ case VTCFG_R_HOSTCAP:
+ case VTCFG_R_QNUM:
+ case VTCFG_R_ISR:
+ case VTBLK_R_CFG ... VTBLK_R_CFG_END:
+ DPRINTF(("vtblk: write to readonly reg %ld\n\r", offset));
+ break;
+ default:
+ DPRINTF(("vtblk: unknown i/o write offset %ld\n\r", offset));
+ value = 0;
+ break;
+ }
+}
+
+uint64_t
+pci_vtblk_read(struct vmctx *ctx, int vcpu, struct pci_devinst *pi,
+ int baridx, uint64_t offset, int size)
+{
+ struct pci_vtblk_softc *sc = pi->pi_arg;
+ void *ptr;
+ uint32_t value;
+
+ if (use_msix) {
+ if (baridx == pci_msix_table_bar(pi) ||
+ baridx == pci_msix_pba_bar(pi)) {
+ return (pci_emul_msix_tread(pi, offset, size));
+ }
+ }
+
+ assert(baridx == 0);
+
+ if (offset + size > pci_vtblk_iosize(pi)) {
+ DPRINTF(("vtblk_read: 2big, offset %ld size %d\n",
+ offset, size));
+ return (0);
+ }
+
+ offset = vtblk_adjust_offset(pi, offset);
+
+ switch (offset) {
+ case VTCFG_R_HOSTCAP:
+ assert(size == 4);
+ value = VTBLK_S_HOSTCAPS;
+ break;
+ case VTCFG_R_GUESTCAP:
+ assert(size == 4);
+ value = sc->vbsc_features; /* XXX never read ? */
+ break;
+ case VTCFG_R_PFN:
+ assert(size == 4);
+ value = sc->vbsc_pfn >> VRING_PFN;
+ break;
+ case VTCFG_R_QNUM:
+ value = (sc->vbsc_lastq == 0) ? VTBLK_RINGSZ: 0;
+ break;
+ case VTCFG_R_QSEL:
+ assert(size == 2);
+ value = sc->vbsc_lastq; /* XXX never read ? */
+ break;
+ case VTCFG_R_QNOTIFY:
+ assert(size == 2);
+ value = 0; /* XXX never read ? */
+ break;
+ case VTCFG_R_STATUS:
+ assert(size == 1);
+ value = sc->vbsc_status;
+ break;
+ case VTCFG_R_ISR:
+ assert(size == 1);
+ value = sc->vbsc_isr;
+ sc->vbsc_isr = 0; /* a read clears this flag */
+ break;
+ case VTCFG_R_CFGVEC:
+ assert(size == 2);
+ value = sc->msix_table_idx_cfg;
+ break;
+ case VTCFG_R_QVEC:
+ assert(size == 2);
+ value = sc->msix_table_idx_req;
+ break;
+ case VTBLK_R_CFG ... VTBLK_R_CFG_END:
+ assert(size + offset <= (VTBLK_R_CFG_END + 1));
+ ptr = (uint8_t *)&sc->vbsc_cfg + offset - VTBLK_R_CFG;
+ if (size == 1) {
+ value = *(uint8_t *) ptr;
+ } else if (size == 2) {
+ value = *(uint16_t *) ptr;
+ } else {
+ value = *(uint32_t *) ptr;
+ }
+ break;
+ default:
+ DPRINTF(("vtblk: unknown i/o read offset %ld\n\r", offset));
+ value = 0;
+ break;
+ }
+
+ return (value);
+}
+
+struct pci_devemu pci_de_vblk = {
+ .pe_emu = "virtio-blk",
+ .pe_init = pci_vtblk_init,
+ .pe_barwrite = pci_vtblk_write,
+ .pe_barread = pci_vtblk_read
+};
+PCI_EMUL_SET(pci_de_vblk);
diff --git a/usr.sbin/bhyve/pci_virtio_net.c b/usr.sbin/bhyve/pci_virtio_net.c
new file mode 100644
index 0000000..327ebf7
--- /dev/null
+++ b/usr.sbin/bhyve/pci_virtio_net.c
@@ -0,0 +1,886 @@
+/*-
+ * Copyright (c) 2011 NetApp, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <sys/param.h>
+#include <sys/linker_set.h>
+#include <sys/select.h>
+#include <sys/uio.h>
+#include <sys/ioctl.h>
+
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <string.h>
+#include <strings.h>
+#include <unistd.h>
+#include <assert.h>
+#include <md5.h>
+#include <pthread.h>
+
+#include "bhyverun.h"
+#include "pci_emul.h"
+#include "mevent.h"
+#include "virtio.h"
+
+#define VTNET_RINGSZ 256
+
+#define VTNET_MAXSEGS 32
+
+/*
+ * PCI config-space register offsets
+ */
+#define VTNET_R_CFG0 24
+#define VTNET_R_CFG1 25
+#define VTNET_R_CFG2 26
+#define VTNET_R_CFG3 27
+#define VTNET_R_CFG4 28
+#define VTNET_R_CFG5 29
+#define VTNET_R_CFG6 30
+#define VTNET_R_CFG7 31
+#define VTNET_R_MAX 31
+
+#define VTNET_REGSZ VTNET_R_MAX+1
+
+/*
+ * Host capabilities
+ */
+#define VTNET_S_HOSTCAPS \
+ ( 0x00000020 | /* host supplies MAC */ \
+ 0x00008000 | /* host can merge Rx buffers */ \
+ 0x00010000 ) /* config status available */
+
+/*
+ * Queue definitions.
+ */
+#define VTNET_RXQ 0
+#define VTNET_TXQ 1
+#define VTNET_CTLQ 2
+
+#define VTNET_MAXQ 3
+
+static int use_msix = 1;
+
+struct vring_hqueue {
+ /* Internal state */
+ uint16_t hq_size;
+ uint16_t hq_cur_aidx; /* trails behind 'avail_idx' */
+
+ /* Host-context pointers to the queue */
+ struct virtio_desc *hq_dtable;
+ uint16_t *hq_avail_flags;
+ uint16_t *hq_avail_idx; /* monotonically increasing */
+ uint16_t *hq_avail_ring;
+
+ uint16_t *hq_used_flags;
+ uint16_t *hq_used_idx; /* monotonically increasing */
+ struct virtio_used *hq_used_ring;
+};
+
+/*
+ * Fixed network header size
+ */
+struct virtio_net_rxhdr {
+ uint8_t vrh_flags;
+ uint8_t vrh_gso_type;
+ uint16_t vrh_hdr_len;
+ uint16_t vrh_gso_size;
+ uint16_t vrh_csum_start;
+ uint16_t vrh_csum_offset;
+ uint16_t vrh_bufs;
+} __packed;
+
+/*
+ * Debug printf
+ */
+static int pci_vtnet_debug;
+#define DPRINTF(params) if (pci_vtnet_debug) printf params
+#define WPRINTF(params) printf params
+
+/*
+ * Per-device softc
+ */
+struct pci_vtnet_softc {
+ struct pci_devinst *vsc_pi;
+ pthread_mutex_t vsc_mtx;
+ struct mevent *vsc_mevp;
+
+ int vsc_curq;
+ int vsc_status;
+ int vsc_isr;
+ int vsc_tapfd;
+ int vsc_rx_ready;
+ int vsc_rxpend;
+
+ uint32_t vsc_features;
+ uint8_t vsc_macaddr[6];
+
+ uint64_t vsc_pfn[VTNET_MAXQ];
+ struct vring_hqueue vsc_hq[VTNET_MAXQ];
+ uint16_t vsc_msix_table_idx[VTNET_MAXQ];
+};
+
+/*
+ * Return the size of IO BAR that maps virtio header and device specific
+ * region. The size would vary depending on whether MSI-X is enabled or
+ * not.
+ */
+static uint64_t
+pci_vtnet_iosize(struct pci_devinst *pi)
+{
+ if (pci_msix_enabled(pi))
+ return (VTNET_REGSZ);
+ else
+ return (VTNET_REGSZ - (VTCFG_R_CFG1 - VTCFG_R_MSIX));
+}
+
+/*
+ * Return the number of available descriptors in the vring taking care
+ * of the 16-bit index wraparound.
+ */
+static int
+hq_num_avail(struct vring_hqueue *hq)
+{
+ int ndesc;
+
+ if (*hq->hq_avail_idx >= hq->hq_cur_aidx)
+ ndesc = *hq->hq_avail_idx - hq->hq_cur_aidx;
+ else
+ ndesc = UINT16_MAX - hq->hq_cur_aidx + *hq->hq_avail_idx + 1;
+
+ assert(ndesc >= 0 && ndesc <= hq->hq_size);
+
+ return (ndesc);
+}
+
+static uint16_t
+pci_vtnet_qsize(int qnum)
+{
+ /* XXX no ctl queue currently */
+ if (qnum == VTNET_CTLQ) {
+ return (0);
+ }
+
+ /* XXX fixed currently. Maybe different for tx/rx/ctl */
+ return (VTNET_RINGSZ);
+}
+
+static void
+pci_vtnet_ring_reset(struct pci_vtnet_softc *sc, int ring)
+{
+ struct vring_hqueue *hq;
+
+ assert(ring < VTNET_MAXQ);
+
+ hq = &sc->vsc_hq[ring];
+
+ /*
+ * Reset all soft state
+ */
+ hq->hq_cur_aidx = 0;
+}
+
+static void
+pci_vtnet_update_status(struct pci_vtnet_softc *sc, uint32_t value)
+{
+
+ if (value == 0) {
+ DPRINTF(("vtnet: device reset requested !\n"));
+ pci_vtnet_ring_reset(sc, VTNET_RXQ);
+ pci_vtnet_ring_reset(sc, VTNET_TXQ);
+ sc->vsc_rx_ready = 0;
+ }
+
+ sc->vsc_status = value;
+}
+
+/*
+ * Called to send a buffer chain out to the tap device
+ */
+static void
+pci_vtnet_tap_tx(struct pci_vtnet_softc *sc, struct iovec *iov, int iovcnt,
+ int len)
+{
+ char pad[60];
+
+ if (sc->vsc_tapfd == -1)
+ return;
+
+ /*
+ * If the length is < 60, pad out to that and add the
+ * extra zero'd segment to the iov. It is guaranteed that
+ * there is always an extra iov available by the caller.
+ */
+ if (len < 60) {
+ memset(pad, 0, 60 - len);
+ iov[iovcnt].iov_base = pad;
+ iov[iovcnt].iov_len = 60 - len;
+ iovcnt++;
+ }
+ (void) writev(sc->vsc_tapfd, iov, iovcnt);
+}
+
+/*
+ * Called when there is read activity on the tap file descriptor.
+ * Each buffer posted by the guest is assumed to be able to contain
+ * an entire ethernet frame + rx header.
+ * MP note: the dummybuf is only used for discarding frames, so there
+ * is no need for it to be per-vtnet or locked.
+ */
+static uint8_t dummybuf[2048];
+
+static void
+pci_vtnet_tap_rx(struct pci_vtnet_softc *sc)
+{
+ struct virtio_desc *vd;
+ struct virtio_used *vu;
+ struct vring_hqueue *hq;
+ struct virtio_net_rxhdr *vrx;
+ uint8_t *buf;
+ int i;
+ int len;
+ int ndescs;
+ int didx, uidx, aidx; /* descriptor, avail and used index */
+
+ /*
+ * Should never be called without a valid tap fd
+ */
+ assert(sc->vsc_tapfd != -1);
+
+ /*
+ * But, will be called when the rx ring hasn't yet
+ * been set up.
+ */
+ if (sc->vsc_rx_ready == 0) {
+ /*
+ * Drop the packet and try later.
+ */
+ (void) read(sc->vsc_tapfd, dummybuf, sizeof(dummybuf));
+ return;
+ }
+
+ /*
+ * Calculate the number of available rx buffers
+ */
+ hq = &sc->vsc_hq[VTNET_RXQ];
+
+ ndescs = hq_num_avail(hq);
+
+ if (ndescs == 0) {
+ /*
+ * Need to wait for host notification to read
+ */
+ if (sc->vsc_rxpend == 0) {
+ WPRINTF(("vtnet: no rx descriptors !\n"));
+ sc->vsc_rxpend = 1;
+ }
+
+ /*
+ * Drop the packet and try later
+ */
+ (void) read(sc->vsc_tapfd, dummybuf, sizeof(dummybuf));
+ return;
+ }
+
+ aidx = hq->hq_cur_aidx;
+ uidx = *hq->hq_used_idx;
+ for (i = 0; i < ndescs; i++) {
+ /*
+ * 'aidx' indexes into the an array of descriptor indexes
+ */
+ didx = hq->hq_avail_ring[aidx % hq->hq_size];
+ assert(didx >= 0 && didx < hq->hq_size);
+
+ vd = &hq->hq_dtable[didx];
+
+ /*
+ * Get a pointer to the rx header, and use the
+ * data immediately following it for the packet buffer.
+ */
+ vrx = (struct virtio_net_rxhdr *)paddr_guest2host(vd->vd_addr);
+ buf = (uint8_t *)(vrx + 1);
+
+ len = read(sc->vsc_tapfd, buf,
+ vd->vd_len - sizeof(struct virtio_net_rxhdr));
+
+ if (len < 0 && errno == EWOULDBLOCK) {
+ break;
+ }
+
+ /*
+ * The only valid field in the rx packet header is the
+ * number of buffers, which is always 1 without TSO
+ * support.
+ */
+ memset(vrx, 0, sizeof(struct virtio_net_rxhdr));
+ vrx->vrh_bufs = 1;
+
+ /*
+ * Write this descriptor into the used ring
+ */
+ vu = &hq->hq_used_ring[uidx % hq->hq_size];
+ vu->vu_idx = didx;
+ vu->vu_tlen = len + sizeof(struct virtio_net_rxhdr);
+ uidx++;
+ aidx++;
+ }
+
+ /*
+ * Update the used pointer, and signal an interrupt if allowed
+ */
+ *hq->hq_used_idx = uidx;
+ hq->hq_cur_aidx = aidx;
+
+ if ((*hq->hq_avail_flags & VRING_AVAIL_F_NO_INTERRUPT) == 0) {
+ if (use_msix) {
+ pci_generate_msix(sc->vsc_pi,
+ sc->vsc_msix_table_idx[VTNET_RXQ]);
+ } else {
+ sc->vsc_isr |= 1;
+ pci_generate_msi(sc->vsc_pi, 0);
+ }
+ }
+}
+
+static void
+pci_vtnet_tap_callback(int fd, enum ev_type type, void *param)
+{
+ struct pci_vtnet_softc *sc = param;
+
+ pthread_mutex_lock(&sc->vsc_mtx);
+ pci_vtnet_tap_rx(sc);
+ pthread_mutex_unlock(&sc->vsc_mtx);
+
+}
+
+static void
+pci_vtnet_ping_rxq(struct pci_vtnet_softc *sc)
+{
+ /*
+ * A qnotify means that the rx process can now begin
+ */
+ if (sc->vsc_rx_ready == 0) {
+ sc->vsc_rx_ready = 1;
+ }
+
+ /*
+ * If the rx queue was empty, attempt to receive a
+ * packet that was previously blocked due to no rx bufs
+ * available
+ */
+ if (sc->vsc_rxpend) {
+ WPRINTF(("vtnet: rx resumed\n\r"));
+ sc->vsc_rxpend = 0;
+ pci_vtnet_tap_rx(sc);
+ }
+}
+
+static void
+pci_vtnet_proctx(struct pci_vtnet_softc *sc, struct vring_hqueue *hq)
+{
+ struct iovec iov[VTNET_MAXSEGS + 1];
+ struct virtio_desc *vd;
+ struct virtio_used *vu;
+ int i;
+ int plen;
+ int tlen;
+ int uidx, aidx, didx;
+
+ uidx = *hq->hq_used_idx;
+ aidx = hq->hq_cur_aidx;
+ didx = hq->hq_avail_ring[aidx % hq->hq_size];
+ assert(didx >= 0 && didx < hq->hq_size);
+
+ vd = &hq->hq_dtable[didx];
+
+ /*
+ * Run through the chain of descriptors, ignoring the
+ * first header descriptor. However, include the header
+ * length in the total length that will be put into the
+ * used queue.
+ */
+ tlen = vd->vd_len;
+ vd = &hq->hq_dtable[vd->vd_next];
+
+ for (i = 0, plen = 0;
+ i < VTNET_MAXSEGS;
+ i++, vd = &hq->hq_dtable[vd->vd_next]) {
+ iov[i].iov_base = paddr_guest2host(vd->vd_addr);
+ iov[i].iov_len = vd->vd_len;
+ plen += vd->vd_len;
+ tlen += vd->vd_len;
+
+ if ((vd->vd_flags & VRING_DESC_F_NEXT) == 0)
+ break;
+ }
+ assert(i < VTNET_MAXSEGS);
+
+ DPRINTF(("virtio: packet send, %d bytes, %d segs\n\r", plen, i + 1));
+ pci_vtnet_tap_tx(sc, iov, i + 1, plen);
+
+ /*
+ * Return this chain back to the host
+ */
+ vu = &hq->hq_used_ring[uidx % hq->hq_size];
+ vu->vu_idx = didx;
+ vu->vu_tlen = tlen;
+ hq->hq_cur_aidx = aidx + 1;
+ *hq->hq_used_idx = uidx + 1;
+
+ /*
+ * Generate an interrupt if able
+ */
+ if ((*hq->hq_avail_flags & VRING_AVAIL_F_NO_INTERRUPT) == 0) {
+ if (use_msix) {
+ pci_generate_msix(sc->vsc_pi,
+ sc->vsc_msix_table_idx[VTNET_TXQ]);
+ } else {
+ sc->vsc_isr |= 1;
+ pci_generate_msi(sc->vsc_pi, 0);
+ }
+ }
+}
+
+static void
+pci_vtnet_ping_txq(struct pci_vtnet_softc *sc)
+{
+ struct vring_hqueue *hq = &sc->vsc_hq[VTNET_TXQ];
+ int i;
+ int ndescs;
+
+ /*
+ * Calculate number of ring entries to process
+ */
+ ndescs = hq_num_avail(hq);
+
+ if (ndescs == 0)
+ return;
+
+ /*
+ * Run through all the entries, placing them into iovecs and
+ * sending when an end-of-packet is found
+ */
+ for (i = 0; i < ndescs; i++)
+ pci_vtnet_proctx(sc, hq);
+}
+
+static void
+pci_vtnet_ping_ctlq(struct pci_vtnet_softc *sc)
+{
+
+ DPRINTF(("vtnet: control qnotify!\n\r"));
+}
+
+static void
+pci_vtnet_ring_init(struct pci_vtnet_softc *sc, uint64_t pfn)
+{
+ struct vring_hqueue *hq;
+ int qnum = sc->vsc_curq;
+
+ assert(qnum < VTNET_MAXQ);
+
+ sc->vsc_pfn[qnum] = pfn << VRING_PFN;
+
+ /*
+ * Set up host pointers to the various parts of the
+ * queue
+ */
+ hq = &sc->vsc_hq[qnum];
+ hq->hq_size = pci_vtnet_qsize(qnum);
+
+ hq->hq_dtable = paddr_guest2host(pfn << VRING_PFN);
+ hq->hq_avail_flags = (uint16_t *)(hq->hq_dtable + hq->hq_size);
+ hq->hq_avail_idx = hq->hq_avail_flags + 1;
+ hq->hq_avail_ring = hq->hq_avail_flags + 2;
+ hq->hq_used_flags = (uint16_t *)roundup2((uintptr_t)hq->hq_avail_ring,
+ VRING_ALIGN);
+ hq->hq_used_idx = hq->hq_used_flags + 1;
+ hq->hq_used_ring = (struct virtio_used *)(hq->hq_used_flags + 2);
+
+ /*
+ * Initialize queue indexes
+ */
+ hq->hq_cur_aidx = 0;
+}
+
+static int
+pci_vtnet_init(struct vmctx *ctx, struct pci_devinst *pi, char *opts)
+{
+ MD5_CTX mdctx;
+ unsigned char digest[16];
+ char nstr[80];
+ struct pci_vtnet_softc *sc;
+ const char *env_msi;
+
+ /*
+ * Access to guest memory is required. Fail if
+ * memory not mapped
+ */
+ if (paddr_guest2host(0) == NULL)
+ return (1);
+
+ sc = malloc(sizeof(struct pci_vtnet_softc));
+ memset(sc, 0, sizeof(struct pci_vtnet_softc));
+
+ pi->pi_arg = sc;
+ sc->vsc_pi = pi;
+
+ pthread_mutex_init(&sc->vsc_mtx, NULL);
+
+ /*
+ * Use MSI if set by user
+ */
+ if ((env_msi = getenv("BHYVE_USE_MSI")) != NULL) {
+ if (strcasecmp(env_msi, "yes") == 0)
+ use_msix = 0;
+ }
+
+ /*
+ * Attempt to open the tap device
+ */
+ sc->vsc_tapfd = -1;
+ if (opts != NULL) {
+ char tbuf[80];
+
+ strcpy(tbuf, "/dev/");
+ strlcat(tbuf, opts, sizeof(tbuf));
+
+ sc->vsc_tapfd = open(tbuf, O_RDWR);
+ if (sc->vsc_tapfd == -1) {
+ WPRINTF(("open of tap device %s failed\n", tbuf));
+ } else {
+ /*
+ * Set non-blocking and register for read
+ * notifications with the event loop
+ */
+ int opt = 1;
+ if (ioctl(sc->vsc_tapfd, FIONBIO, &opt) < 0) {
+ WPRINTF(("tap device O_NONBLOCK failed\n"));
+ close(sc->vsc_tapfd);
+ sc->vsc_tapfd = -1;
+ }
+
+ sc->vsc_mevp = mevent_add(sc->vsc_tapfd,
+ EVF_READ,
+ pci_vtnet_tap_callback,
+ sc);
+ if (sc->vsc_mevp == NULL) {
+ WPRINTF(("Could not register event\n"));
+ close(sc->vsc_tapfd);
+ sc->vsc_tapfd = -1;
+ }
+ }
+ }
+
+ /*
+ * The MAC address is the standard NetApp OUI of 00-a0-98,
+ * followed by an MD5 of the vm name. The slot/func number is
+ * prepended to this for slots other than 1:0, so that
+ * a bootloader can netboot from the equivalent of slot 1.
+ */
+ if (pi->pi_slot == 1 && pi->pi_func == 0) {
+ strncpy(nstr, vmname, sizeof(nstr));
+ } else {
+ snprintf(nstr, sizeof(nstr), "%d-%d-%s", pi->pi_slot,
+ pi->pi_func, vmname);
+ }
+
+ MD5Init(&mdctx);
+ MD5Update(&mdctx, nstr, strlen(nstr));
+ MD5Final(digest, &mdctx);
+
+ sc->vsc_macaddr[0] = 0x00;
+ sc->vsc_macaddr[1] = 0xa0;
+ sc->vsc_macaddr[2] = 0x98;
+ sc->vsc_macaddr[3] = digest[0];
+ sc->vsc_macaddr[4] = digest[1];
+ sc->vsc_macaddr[5] = digest[2];
+
+ /* initialize config space */
+ pci_set_cfgdata16(pi, PCIR_DEVICE, VIRTIO_DEV_NET);
+ pci_set_cfgdata16(pi, PCIR_VENDOR, VIRTIO_VENDOR);
+ pci_set_cfgdata8(pi, PCIR_CLASS, PCIC_NETWORK);
+ pci_set_cfgdata16(pi, PCIR_SUBDEV_0, VIRTIO_TYPE_NET);
+
+ if (use_msix) {
+ /* MSI-X support */
+ int i;
+
+ for (i = 0; i < VTNET_MAXQ; i++)
+ sc->vsc_msix_table_idx[i] = VIRTIO_MSI_NO_VECTOR;
+
+ /*
+ * BAR 1 used to map MSI-X table and PBA
+ */
+ if (pci_emul_add_msixcap(pi, VTNET_MAXQ, 1))
+ return (1);
+ } else {
+ /* MSI support */
+ pci_emul_add_msicap(pi, 1);
+ }
+
+ pci_emul_alloc_bar(pi, 0, PCIBAR_IO, VTNET_REGSZ);
+
+ return (0);
+}
+
+/*
+ * Function pointer array to handle queue notifications
+ */
+static void (*pci_vtnet_qnotify[VTNET_MAXQ])(struct pci_vtnet_softc *) = {
+ pci_vtnet_ping_rxq,
+ pci_vtnet_ping_txq,
+ pci_vtnet_ping_ctlq
+};
+
+static uint64_t
+vtnet_adjust_offset(struct pci_devinst *pi, uint64_t offset)
+{
+ /*
+ * Device specific offsets used by guest would change based on
+ * whether MSI-X capability is enabled or not
+ */
+ if (!pci_msix_enabled(pi)) {
+ if (offset >= VTCFG_R_MSIX)
+ return (offset + (VTCFG_R_CFG1 - VTCFG_R_MSIX));
+ }
+
+ return (offset);
+}
+
+static void
+pci_vtnet_write(struct vmctx *ctx, int vcpu, struct pci_devinst *pi,
+ int baridx, uint64_t offset, int size, uint64_t value)
+{
+ struct pci_vtnet_softc *sc = pi->pi_arg;
+ void *ptr;
+
+ if (use_msix) {
+ if (baridx == pci_msix_table_bar(pi) ||
+ baridx == pci_msix_pba_bar(pi)) {
+ pci_emul_msix_twrite(pi, offset, size, value);
+ return;
+ }
+ }
+
+ assert(baridx == 0);
+
+ if (offset + size > pci_vtnet_iosize(pi)) {
+ DPRINTF(("vtnet_write: 2big, offset %ld size %d\n",
+ offset, size));
+ return;
+ }
+
+ pthread_mutex_lock(&sc->vsc_mtx);
+
+ offset = vtnet_adjust_offset(pi, offset);
+
+ switch (offset) {
+ case VTCFG_R_GUESTCAP:
+ assert(size == 4);
+ sc->vsc_features = value & VTNET_S_HOSTCAPS;
+ break;
+ case VTCFG_R_PFN:
+ assert(size == 4);
+ pci_vtnet_ring_init(sc, value);
+ break;
+ case VTCFG_R_QSEL:
+ assert(size == 2);
+ assert(value < VTNET_MAXQ);
+ sc->vsc_curq = value;
+ break;
+ case VTCFG_R_QNOTIFY:
+ assert(size == 2);
+ assert(value < VTNET_MAXQ);
+ (*pci_vtnet_qnotify[value])(sc);
+ break;
+ case VTCFG_R_STATUS:
+ assert(size == 1);
+ pci_vtnet_update_status(sc, value);
+ break;
+ case VTCFG_R_CFGVEC:
+ assert(size == 2);
+ sc->vsc_msix_table_idx[VTNET_CTLQ] = value;
+ break;
+ case VTCFG_R_QVEC:
+ assert(size == 2);
+ assert(sc->vsc_curq != VTNET_CTLQ);
+ sc->vsc_msix_table_idx[sc->vsc_curq] = value;
+ break;
+ case VTNET_R_CFG0:
+ case VTNET_R_CFG1:
+ case VTNET_R_CFG2:
+ case VTNET_R_CFG3:
+ case VTNET_R_CFG4:
+ case VTNET_R_CFG5:
+ assert((size + offset) <= (VTNET_R_CFG5 + 1));
+ ptr = &sc->vsc_macaddr[offset - VTNET_R_CFG0];
+ /*
+ * The driver is allowed to change the MAC address
+ */
+ sc->vsc_macaddr[offset - VTNET_R_CFG0] = value;
+ if (size == 1) {
+ *(uint8_t *) ptr = value;
+ } else if (size == 2) {
+ *(uint16_t *) ptr = value;
+ } else {
+ *(uint32_t *) ptr = value;
+ }
+ break;
+ case VTCFG_R_HOSTCAP:
+ case VTCFG_R_QNUM:
+ case VTCFG_R_ISR:
+ case VTNET_R_CFG6:
+ case VTNET_R_CFG7:
+ DPRINTF(("vtnet: write to readonly reg %ld\n\r", offset));
+ break;
+ default:
+ DPRINTF(("vtnet: unknown i/o write offset %ld\n\r", offset));
+ value = 0;
+ break;
+ }
+
+ pthread_mutex_unlock(&sc->vsc_mtx);
+}
+
+uint64_t
+pci_vtnet_read(struct vmctx *ctx, int vcpu, struct pci_devinst *pi,
+ int baridx, uint64_t offset, int size)
+{
+ struct pci_vtnet_softc *sc = pi->pi_arg;
+ void *ptr;
+ uint64_t value;
+
+ if (use_msix) {
+ if (baridx == pci_msix_table_bar(pi) ||
+ baridx == pci_msix_pba_bar(pi)) {
+ return (pci_emul_msix_tread(pi, offset, size));
+ }
+ }
+
+ assert(baridx == 0);
+
+ if (offset + size > pci_vtnet_iosize(pi)) {
+ DPRINTF(("vtnet_read: 2big, offset %ld size %d\n",
+ offset, size));
+ return (0);
+ }
+
+ pthread_mutex_lock(&sc->vsc_mtx);
+
+ offset = vtnet_adjust_offset(pi, offset);
+
+ switch (offset) {
+ case VTCFG_R_HOSTCAP:
+ assert(size == 4);
+ value = VTNET_S_HOSTCAPS;
+ break;
+ case VTCFG_R_GUESTCAP:
+ assert(size == 4);
+ value = sc->vsc_features; /* XXX never read ? */
+ break;
+ case VTCFG_R_PFN:
+ assert(size == 4);
+ value = sc->vsc_pfn[sc->vsc_curq] >> VRING_PFN;
+ break;
+ case VTCFG_R_QNUM:
+ assert(size == 2);
+ value = pci_vtnet_qsize(sc->vsc_curq);
+ break;
+ case VTCFG_R_QSEL:
+ assert(size == 2);
+ value = sc->vsc_curq; /* XXX never read ? */
+ break;
+ case VTCFG_R_QNOTIFY:
+ assert(size == 2);
+ value = sc->vsc_curq; /* XXX never read ? */
+ break;
+ case VTCFG_R_STATUS:
+ assert(size == 1);
+ value = sc->vsc_status;
+ break;
+ case VTCFG_R_ISR:
+ assert(size == 1);
+ value = sc->vsc_isr;
+ sc->vsc_isr = 0; /* a read clears this flag */
+ break;
+ case VTCFG_R_CFGVEC:
+ assert(size == 2);
+ value = sc->vsc_msix_table_idx[VTNET_CTLQ];
+ break;
+ case VTCFG_R_QVEC:
+ assert(size == 2);
+ assert(sc->vsc_curq != VTNET_CTLQ);
+ value = sc->vsc_msix_table_idx[sc->vsc_curq];
+ break;
+ case VTNET_R_CFG0:
+ case VTNET_R_CFG1:
+ case VTNET_R_CFG2:
+ case VTNET_R_CFG3:
+ case VTNET_R_CFG4:
+ case VTNET_R_CFG5:
+ assert((size + offset) <= (VTNET_R_CFG5 + 1));
+ ptr = &sc->vsc_macaddr[offset - VTNET_R_CFG0];
+ if (size == 1) {
+ value = *(uint8_t *) ptr;
+ } else if (size == 2) {
+ value = *(uint16_t *) ptr;
+ } else {
+ value = *(uint32_t *) ptr;
+ }
+ break;
+ case VTNET_R_CFG6:
+ assert(size != 4);
+ value = 0x01; /* XXX link always up */
+ break;
+ case VTNET_R_CFG7:
+ assert(size == 1);
+ value = 0; /* XXX link status in LSB */
+ break;
+ default:
+ DPRINTF(("vtnet: unknown i/o read offset %ld\n\r", offset));
+ value = 0;
+ break;
+ }
+
+ pthread_mutex_unlock(&sc->vsc_mtx);
+
+ return (value);
+}
+
+struct pci_devemu pci_de_vnet = {
+ .pe_emu = "virtio-net",
+ .pe_init = pci_vtnet_init,
+ .pe_barwrite = pci_vtnet_write,
+ .pe_barread = pci_vtnet_read
+};
+PCI_EMUL_SET(pci_de_vnet);
diff --git a/usr.sbin/bhyve/pit_8254.c b/usr.sbin/bhyve/pit_8254.c
new file mode 100644
index 0000000..c96596a
--- /dev/null
+++ b/usr.sbin/bhyve/pit_8254.c
@@ -0,0 +1,198 @@
+/*-
+ * Copyright (c) 2011 NetApp, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <sys/time.h>
+
+#include <machine/clock.h>
+
+#include <stdio.h>
+#include <assert.h>
+
+#include "bhyverun.h"
+#include "inout.h"
+#include "pit_8254.h"
+
+#define TIMER_SEL_MASK 0xc0
+#define TIMER_RW_MASK 0x30
+#define TIMER_MODE_MASK 0x0f
+#define TIMER_SEL_READBACK 0xc0
+
+#define TIMER_DIV(freq, hz) (((freq) + (hz) / 2) / (hz))
+
+#define PIT_8254_FREQ 1193182
+static const int nsecs_per_tick = 1000000000 / PIT_8254_FREQ;
+
+struct counter {
+ struct timeval tv; /* uptime when counter was loaded */
+ uint16_t initial; /* initial counter value */
+ uint8_t cr[2];
+ uint8_t ol[2];
+ int crbyte;
+ int olbyte;
+};
+
+static void
+timevalfix(struct timeval *t1)
+{
+
+ if (t1->tv_usec < 0) {
+ t1->tv_sec--;
+ t1->tv_usec += 1000000;
+ }
+ if (t1->tv_usec >= 1000000) {
+ t1->tv_sec++;
+ t1->tv_usec -= 1000000;
+ }
+}
+
+static void
+timevalsub(struct timeval *t1, const struct timeval *t2)
+{
+
+ t1->tv_sec -= t2->tv_sec;
+ t1->tv_usec -= t2->tv_usec;
+ timevalfix(t1);
+}
+
+static void
+latch(struct counter *c)
+{
+ struct timeval tv2;
+ uint16_t lval;
+ uint64_t delta_nsecs, delta_ticks;
+
+ /* cannot latch a new value until the old one has been consumed */
+ if (c->olbyte != 0)
+ return;
+
+ if (c->initial == 0 || c->initial == 1) {
+ /*
+ * XXX the program that runs the VM can be stopped and
+ * restarted at any time. This means that state that was
+ * created by the guest is destroyed between invocations
+ * of the program.
+ *
+ * If the counter's initial value is not programmed we
+ * assume a value that would be set to generate 'guest_hz'
+ * interrupts per second.
+ */
+ c->initial = TIMER_DIV(PIT_8254_FREQ, guest_hz);
+ gettimeofday(&c->tv, NULL);
+ }
+
+ (void)gettimeofday(&tv2, NULL);
+ timevalsub(&tv2, &c->tv);
+ delta_nsecs = tv2.tv_sec * 1000000000 + tv2.tv_usec * 1000;
+ delta_ticks = delta_nsecs / nsecs_per_tick;
+
+ lval = c->initial - delta_ticks % c->initial;
+ c->olbyte = 2;
+ c->ol[1] = lval; /* LSB */
+ c->ol[0] = lval >> 8; /* MSB */
+}
+
+static int
+pit_8254_handler(struct vmctx *ctx, int vcpu, int in, int port, int bytes,
+ uint32_t *eax, void *arg)
+{
+ int sel, rw, mode;
+ uint8_t val;
+ struct counter *c;
+
+ static struct counter counter[3];
+
+ if (bytes != 1)
+ return (-1);
+
+ val = *eax;
+
+ if (port == TIMER_MODE) {
+ assert(in == 0);
+ sel = val & TIMER_SEL_MASK;
+ rw = val & TIMER_RW_MASK;
+ mode = val & TIMER_MODE_MASK;
+
+ if (sel == TIMER_SEL_READBACK)
+ return (-1);
+ if (rw != TIMER_LATCH && rw != TIMER_16BIT)
+ return (-1);
+
+ if (rw != TIMER_LATCH) {
+ /*
+ * Counter mode is not affected when issuing a
+ * latch command.
+ */
+ if (mode != TIMER_RATEGEN && mode != TIMER_SQWAVE)
+ return (-1);
+ }
+
+ c = &counter[sel >> 6];
+ if (rw == TIMER_LATCH)
+ latch(c);
+ else
+ c->olbyte = 0; /* reset latch after reprogramming */
+
+ return (0);
+ }
+
+ /* counter ports */
+ assert(port >= TIMER_CNTR0 && port <= TIMER_CNTR2);
+ c = &counter[port - TIMER_CNTR0];
+
+ if (in) {
+ /*
+ * XXX
+ * The spec says that once the output latch is completely
+ * read it should revert to "following" the counter. We don't
+ * do this because it is hard and any reasonable OS should
+ * always latch the counter before trying to read it.
+ */
+ if (c->olbyte == 0)
+ c->olbyte = 2;
+ *eax = c->ol[--c->olbyte];
+ } else {
+ c->cr[c->crbyte++] = *eax;
+ if (c->crbyte == 2) {
+ c->crbyte = 0;
+ c->initial = c->cr[0] | (uint16_t)c->cr[1] << 8;
+ if (c->initial == 0)
+ c->initial = 0xffff;
+ gettimeofday(&c->tv, NULL);
+ }
+ }
+
+ return (0);
+}
+
+INOUT_PORT(8254, TIMER_MODE, IOPORT_F_OUT, pit_8254_handler);
+INOUT_PORT(8254, TIMER_CNTR0, IOPORT_F_INOUT, pit_8254_handler);
+INOUT_PORT(8254, TIMER_CNTR1, IOPORT_F_INOUT, pit_8254_handler);
+INOUT_PORT(8254, TIMER_CNTR2, IOPORT_F_INOUT, pit_8254_handler);
diff --git a/usr.sbin/sade/list.h b/usr.sbin/bhyve/pit_8254.h
index 8c5cf2a..61bd15d 100644
--- a/usr.sbin/sade/list.h
+++ b/usr.sbin/bhyve/pit_8254.h
@@ -1,55 +1,45 @@
-/*
- * $FreeBSD$
- *
- * Copyright (c) 1997 FreeBSD, Inc.
+/*-
+ * Copyright (c) 2011 NetApp, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer,
- * verbatim and that no modifications are made prior to this
- * point in the file.
+ * notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
- * THIS SOFTWARE IS PROVIDED BY PAUL TRAINA ``AS IS'' AND
+ * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL PAUL TRAINA OR HIS KILLER RATS BE LIABLE
+ * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, LIFE OR PROFITS; OR BUSINESS INTERRUPTION)
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
+ * $FreeBSD$
*/
-/* The structure */
-typedef struct _qelement {
- struct _qelement *q_forw;
- struct _qelement *q_back;
-} qelement;
+#ifndef _PIT_8254_H_
+#define _PIT_8254_H_
-#define INITQUE(Xhead) { \
- (Xhead).q_forw = &(Xhead); \
- (Xhead).q_back = &(Xhead); \
-}
+/*
+ * Borrowed from amd64/include/timerreg.h because in that file it is
+ * conditionally compiled for #ifdef _KERNEL only.
+ */
-#define EMPTYQUE(Xhead) \
- ((Xhead).q_forw == &(Xhead))
+#include <dev/ic/i8253reg.h>
-#define INSQUEUE(elem, pred) { \
- register qelement *Xe = (qelement *) (elem); \
- register qelement *Xp = (qelement *) (pred); \
- Xp->q_forw = (Xe->q_forw = (Xe->q_back = Xp)->q_forw)->q_back = Xe; \
-}
+#define IO_TIMER1 0x40 /* 8253 Timer #1 */
+#define TIMER_CNTR0 (IO_TIMER1 + TIMER_REG_CNTR0)
+#define TIMER_CNTR1 (IO_TIMER1 + TIMER_REG_CNTR1)
+#define TIMER_CNTR2 (IO_TIMER1 + TIMER_REG_CNTR2)
+#define TIMER_MODE (IO_TIMER1 + TIMER_REG_MODE)
-#define REMQUE(elem) { \
- register qelement *Xe = (qelement *) (elem); \
- (Xe->q_back->q_forw = Xe->q_forw)->q_back = Xe->q_back; \
-}
+#endif /* _PIT_8254_H_ */
diff --git a/usr.sbin/bhyve/pmtmr.c b/usr.sbin/bhyve/pmtmr.c
new file mode 100644
index 0000000..b85b0a7
--- /dev/null
+++ b/usr.sbin/bhyve/pmtmr.c
@@ -0,0 +1,105 @@
+/*-
+ * Copyright (c) 2012 NetApp, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <sys/types.h>
+#include <sys/sysctl.h>
+#include <sys/time.h>
+#include <machine/cpufunc.h>
+
+#include <stdio.h>
+#include <time.h>
+#include <assert.h>
+#include <pthread.h>
+
+#include "inout.h"
+
+/*
+ * The ACPI Power Management timer is a free-running 24- or 32-bit
+ * timer with a frequency of 3.579545MHz
+ *
+ * This implementation will be 32-bits
+ */
+
+#define IO_PMTMR 0x408 /* 4-byte i/o port for the timer */
+
+#define PMTMR_FREQ 3579545 /* 3.579545MHz */
+
+static pthread_mutex_t pmtmr_mtx;
+static uint64_t pmtmr_tscf;
+static uint64_t pmtmr_old;
+static uint64_t pmtmr_tsc_old;
+
+static uint32_t
+pmtmr_val(void)
+{
+ uint64_t pmtmr_tsc_new;
+ uint64_t pmtmr_new;
+ static int inited = 0;
+
+ if (!inited) {
+ size_t len;
+
+ inited = 1;
+ pthread_mutex_init(&pmtmr_mtx, NULL);
+ len = sizeof(pmtmr_tscf);
+ sysctlbyname("machdep.tsc_freq", &pmtmr_tscf, &len,
+ NULL, 0);
+ pmtmr_tsc_old = rdtsc();
+ pmtmr_old = pmtmr_tsc_old / pmtmr_tscf * PMTMR_FREQ;
+ }
+
+ pthread_mutex_lock(&pmtmr_mtx);
+ pmtmr_tsc_new = rdtsc();
+ pmtmr_new = (pmtmr_tsc_new - pmtmr_tsc_old) * PMTMR_FREQ / pmtmr_tscf +
+ pmtmr_old;
+ pmtmr_old = pmtmr_new;
+ pmtmr_tsc_old = pmtmr_tsc_new;
+ pthread_mutex_unlock(&pmtmr_mtx);
+
+ return (pmtmr_new);
+}
+
+static int
+pmtmr_handler(struct vmctx *ctx, int vcpu, int in, int port, int bytes,
+ uint32_t *eax, void *arg)
+{
+ assert(in == 1);
+
+ if (bytes != 4)
+ return (-1);
+
+ *eax = pmtmr_val();
+
+ return (0);
+}
+
+INOUT_PORT(pmtmr, IO_PMTMR, IOPORT_F_IN, pmtmr_handler);
+
diff --git a/usr.sbin/bhyve/post.c b/usr.sbin/bhyve/post.c
new file mode 100644
index 0000000..092a551
--- /dev/null
+++ b/usr.sbin/bhyve/post.c
@@ -0,0 +1,51 @@
+/*-
+ * Copyright (c) 2011 NetApp, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <sys/types.h>
+
+#include <assert.h>
+
+#include "inout.h"
+
+static int
+post_data_handler(struct vmctx *ctx, int vcpu, int in, int port, int bytes,
+ uint32_t *eax, void *arg)
+{
+ assert(in == 1);
+
+ if (bytes != 1)
+ return (-1);
+
+ *eax = 0xff; /* return some garbage */
+ return (0);
+}
+
+INOUT_PORT(post, 0x84, IOPORT_F_IN, post_data_handler);
diff --git a/usr.sbin/bhyve/rtc.c b/usr.sbin/bhyve/rtc.c
new file mode 100644
index 0000000..df21ac8
--- /dev/null
+++ b/usr.sbin/bhyve/rtc.c
@@ -0,0 +1,284 @@
+/*-
+ * Copyright (c) 2011 NetApp, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <sys/types.h>
+#include <sys/time.h>
+
+#include <stdio.h>
+#include <time.h>
+#include <assert.h>
+
+#include "inout.h"
+
+#define IO_RTC 0x70
+
+#define RTC_SEC 0x00 /* seconds */
+#define RTC_MIN 0x02
+#define RTC_HRS 0x04
+#define RTC_WDAY 0x06
+#define RTC_DAY 0x07
+#define RTC_MONTH 0x08
+#define RTC_YEAR 0x09
+#define RTC_CENTURY 0x32 /* current century */
+
+#define RTC_STATUSA 0xA
+#define RTCSA_TUP 0x80 /* time update, don't look now */
+
+#define RTC_STATUSB 0xB
+#define RTCSB_DST 0x01
+#define RTCSB_24HR 0x02
+#define RTCSB_BIN 0x04 /* 0 = BCD, 1 = Binary */
+#define RTCSB_PINTR 0x40 /* 1 = enable periodic clock interrupt */
+#define RTCSB_HALT 0x80 /* stop clock updates */
+
+#define RTC_INTR 0x0c /* status register C (R) interrupt source */
+
+#define RTC_STATUSD 0x0d /* status register D (R) Lost Power */
+#define RTCSD_PWR 0x80 /* clock power OK */
+
+#define RTC_DIAG 0x0e
+
+#define RTC_RSTCODE 0x0f
+
+#define RTC_EQUIPMENT 0x14
+
+static int addr;
+
+/* XXX initialize these to default values as they would be from BIOS */
+static uint8_t status_a, status_b, rstcode;
+
+static u_char const bin2bcd_data[] = {
+ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
+ 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19,
+ 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29,
+ 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
+ 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
+ 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
+ 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
+ 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
+ 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
+ 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99
+};
+#define bin2bcd(bin) (bin2bcd_data[bin])
+
+#define rtcout(val) ((status_b & RTCSB_BIN) ? (val) : bin2bcd((val)))
+
+static void
+timevalfix(struct timeval *t1)
+{
+
+ if (t1->tv_usec < 0) {
+ t1->tv_sec--;
+ t1->tv_usec += 1000000;
+ }
+ if (t1->tv_usec >= 1000000) {
+ t1->tv_sec++;
+ t1->tv_usec -= 1000000;
+ }
+}
+
+static void
+timevalsub(struct timeval *t1, const struct timeval *t2)
+{
+
+ t1->tv_sec -= t2->tv_sec;
+ t1->tv_usec -= t2->tv_usec;
+ timevalfix(t1);
+}
+
+static int
+rtc_addr_handler(struct vmctx *ctx, int vcpu, int in, int port, int bytes,
+ uint32_t *eax, void *arg)
+{
+ if (bytes != 1)
+ return (-1);
+
+ if (in) {
+ /* straight read of this register will return 0xFF */
+ *eax = 0xff;
+ return (0);
+ }
+
+ switch (*eax & 0x7f) {
+ case RTC_SEC:
+ case RTC_MIN:
+ case RTC_HRS:
+ case RTC_WDAY:
+ case RTC_DAY:
+ case RTC_MONTH:
+ case RTC_YEAR:
+ case RTC_CENTURY:
+ case RTC_STATUSA:
+ case RTC_STATUSB:
+ case RTC_INTR:
+ case RTC_STATUSD:
+ case RTC_DIAG:
+ case RTC_RSTCODE:
+ case RTC_EQUIPMENT:
+ break;
+ default:
+ return (-1);
+ }
+
+ addr = *eax & 0x7f;
+ return (0);
+}
+
+static int
+rtc_data_handler(struct vmctx *ctx, int vcpu, int in, int port, int bytes,
+ uint32_t *eax, void *arg)
+{
+ int hour;
+ time_t t;
+ struct timeval cur, delta;
+
+ static struct timeval last;
+ static struct tm tm;
+
+ if (bytes != 1)
+ return (-1);
+
+ gettimeofday(&cur, NULL);
+
+ /*
+ * Increment the cached time only once per second so we can guarantee
+ * that the guest has at least one second to read the hour:min:sec
+ * separately and still get a coherent view of the time.
+ */
+ delta = cur;
+ timevalsub(&delta, &last);
+ if (delta.tv_sec >= 1 && (status_b & RTCSB_HALT) == 0) {
+ t = cur.tv_sec;
+ localtime_r(&t, &tm);
+ last = cur;
+ }
+
+ if (in) {
+ switch (addr) {
+ case RTC_SEC:
+ *eax = rtcout(tm.tm_sec);
+ return (0);
+ case RTC_MIN:
+ *eax = rtcout(tm.tm_min);
+ return (0);
+ case RTC_HRS:
+ if (status_b & RTCSB_24HR)
+ hour = tm.tm_hour;
+ else
+ hour = (tm.tm_hour % 12) + 1;
+
+ *eax = rtcout(hour);
+
+ /*
+ * If we are representing time in the 12-hour format
+ * then set the MSB to indicate PM.
+ */
+ if ((status_b & RTCSB_24HR) == 0 && tm.tm_hour >= 12)
+ *eax |= 0x80;
+
+ return (0);
+ case RTC_WDAY:
+ *eax = rtcout(tm.tm_wday + 1);
+ return (0);
+ case RTC_DAY:
+ *eax = rtcout(tm.tm_mday);
+ return (0);
+ case RTC_MONTH:
+ *eax = rtcout(tm.tm_mon + 1);
+ return (0);
+ case RTC_YEAR:
+ *eax = rtcout(tm.tm_year % 100);
+ return (0);
+ case RTC_CENTURY:
+ *eax = rtcout(tm.tm_year / 100);
+ break;
+ case RTC_STATUSA:
+ *eax = status_a;
+ return (0);
+ case RTC_STATUSB:
+ *eax = status_b;
+ return (0);
+ case RTC_INTR:
+ *eax = 0;
+ return (0);
+ case RTC_STATUSD:
+ *eax = RTCSD_PWR;
+ return (0);
+ case RTC_DIAG:
+ *eax = 0;
+ return (0);
+ case RTC_RSTCODE:
+ *eax = rstcode;
+ return (0);
+ case RTC_EQUIPMENT:
+ *eax = 0;
+ return (0);
+ default:
+ return (-1);
+ }
+ }
+
+ switch (addr) {
+ case RTC_STATUSA:
+ status_a = *eax & ~RTCSA_TUP;
+ break;
+ case RTC_STATUSB:
+ /* XXX not implemented yet XXX */
+ if (*eax & RTCSB_PINTR)
+ return (-1);
+ status_b = *eax;
+ break;
+ case RTC_STATUSD:
+ /* ignore write */
+ break;
+ case RTC_RSTCODE:
+ rstcode = *eax;
+ break;
+ case RTC_SEC:
+ case RTC_MIN:
+ case RTC_HRS:
+ case RTC_WDAY:
+ case RTC_DAY:
+ case RTC_MONTH:
+ case RTC_YEAR:
+ case RTC_CENTURY:
+ /*
+ * Ignore writes to the time of day registers
+ */
+ break;
+ default:
+ return (-1);
+ }
+ return (0);
+}
+
+INOUT_PORT(rtc, IO_RTC, IOPORT_F_INOUT, rtc_addr_handler);
+INOUT_PORT(rtc, IO_RTC + 1, IOPORT_F_INOUT, rtc_data_handler);
diff --git a/usr.sbin/bhyve/spinup_ap.c b/usr.sbin/bhyve/spinup_ap.c
new file mode 100644
index 0000000..2632aed
--- /dev/null
+++ b/usr.sbin/bhyve/spinup_ap.c
@@ -0,0 +1,119 @@
+/*-
+ * Copyright (c) 2012 NetApp, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <sys/param.h>
+#include <sys/types.h>
+
+#include <machine/vmm.h>
+#include <vmmapi.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <assert.h>
+
+#include "bhyverun.h"
+#include "spinup_ap.h"
+
+static void
+spinup_ap_realmode(struct vmctx *ctx, int newcpu, uint64_t *rip)
+{
+ int vector, error;
+ uint16_t cs;
+ uint64_t desc_base;
+ uint32_t desc_limit, desc_access;
+
+ vector = *rip >> PAGE_SHIFT;
+ *rip = 0;
+
+ /*
+ * Update the %cs and %rip of the guest so that it starts
+ * executing real mode code at at 'vector << 12'.
+ */
+ error = vm_set_register(ctx, newcpu, VM_REG_GUEST_RIP, *rip);
+ assert(error == 0);
+
+ error = vm_get_desc(ctx, newcpu, VM_REG_GUEST_CS, &desc_base,
+ &desc_limit, &desc_access);
+ assert(error == 0);
+
+ desc_base = vector << PAGE_SHIFT;
+ error = vm_set_desc(ctx, newcpu, VM_REG_GUEST_CS,
+ desc_base, desc_limit, desc_access);
+ assert(error == 0);
+
+ cs = (vector << PAGE_SHIFT) >> 4;
+ error = vm_set_register(ctx, newcpu, VM_REG_GUEST_CS, cs);
+ assert(error == 0);
+}
+
+int
+spinup_ap(struct vmctx *ctx, int vcpu, int newcpu, uint64_t rip)
+{
+ int error;
+
+ assert(newcpu != 0);
+ assert(newcpu < guest_ncpus);
+
+ error = vcpu_reset(ctx, newcpu);
+ assert(error == 0);
+
+ /* Set up capabilities */
+ if (fbsdrun_vmexit_on_hlt()) {
+ error = vm_set_capability(ctx, newcpu, VM_CAP_HALT_EXIT, 1);
+ assert(error == 0);
+ }
+
+ if (fbsdrun_vmexit_on_pause()) {
+ error = vm_set_capability(ctx, newcpu, VM_CAP_PAUSE_EXIT, 1);
+ assert(error == 0);
+ }
+
+ if (fbsdrun_disable_x2apic())
+ error = vm_set_x2apic_state(ctx, newcpu, X2APIC_DISABLED);
+ else
+ error = vm_set_x2apic_state(ctx, newcpu, X2APIC_ENABLED);
+ assert(error == 0);
+
+ /*
+ * Enable the 'unrestricted guest' mode for 'newcpu'.
+ *
+ * Set up the processor state in power-on 16-bit mode, with the CS:IP
+ * init'd to the specified low-mem 4K page.
+ */
+ error = vm_set_capability(ctx, newcpu, VM_CAP_UNRESTRICTED_GUEST, 1);
+ assert(error == 0);
+
+ spinup_ap_realmode(ctx, newcpu, &rip);
+
+ fbsdrun_addcpu(ctx, newcpu, rip);
+
+ return (newcpu);
+}
diff --git a/usr.sbin/bhyve/spinup_ap.h b/usr.sbin/bhyve/spinup_ap.h
new file mode 100644
index 0000000..2749ee9
--- /dev/null
+++ b/usr.sbin/bhyve/spinup_ap.h
@@ -0,0 +1,34 @@
+/*-
+ * Copyright (c) 2012 NetApp, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ */
+
+#ifndef _SPINUP_AP_H_
+#define _SPINUP_AP_H_
+
+int spinup_ap(struct vmctx *ctx, int vcpu, int newcpu, uint64_t rip);
+
+#endif
diff --git a/usr.sbin/bhyve/uart.c b/usr.sbin/bhyve/uart.c
new file mode 100644
index 0000000..640f3bf
--- /dev/null
+++ b/usr.sbin/bhyve/uart.c
@@ -0,0 +1,60 @@
+/*-
+ * Copyright (c) 2011 NetApp, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <sys/types.h>
+#include <assert.h>
+
+#include "inout.h"
+
+#define COM1 0x3F8
+#define COM2 0x2F8
+
+#define REG_IIR 2
+
+static int
+com_handler(struct vmctx *ctx, int vcpu, int in, int port, int bytes,
+ uint32_t *eax, void *arg)
+{
+ assert(in);
+
+ if (bytes != 1)
+ return (-1);
+
+ /*
+ * COM port is not implemented so we return 0xFF for all registers
+ */
+ *eax = 0xFF;
+
+ return (0);
+}
+
+INOUT_PORT(uart, COM1 + REG_IIR, IOPORT_F_IN, com_handler);
+INOUT_PORT(uart, COM2 + REG_IIR, IOPORT_F_IN, com_handler);
diff --git a/usr.sbin/bhyve/virtio.h b/usr.sbin/bhyve/virtio.h
new file mode 100644
index 0000000..04ef586
--- /dev/null
+++ b/usr.sbin/bhyve/virtio.h
@@ -0,0 +1,88 @@
+/*-
+ * Copyright (c) 2011 NetApp, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ */
+
+#ifndef _VIRTIO_H_
+#define _VIRTIO_H_
+
+#define VRING_ALIGN 4096
+
+#define VRING_DESC_F_NEXT (1 << 0)
+#define VRING_DESC_F_WRITE (1 << 1)
+#define VRING_DESC_F_INDIRECT (1 << 2)
+
+#define VRING_AVAIL_F_NO_INTERRUPT 1
+#define VIRTIO_MSI_NO_VECTOR 0xFFFF
+
+struct virtio_desc {
+ uint64_t vd_addr;
+ uint32_t vd_len;
+ uint16_t vd_flags;
+ uint16_t vd_next;
+} __packed;
+
+struct virtio_used {
+ uint32_t vu_idx;
+ uint32_t vu_tlen;
+} __packed;
+
+/*
+ * PFN register shift amount
+ */
+#define VRING_PFN 12
+
+/*
+ * Virtio device types
+ */
+#define VIRTIO_TYPE_NET 1
+#define VIRTIO_TYPE_BLOCK 2
+
+/*
+ * PCI vendor/device IDs
+ */
+#define VIRTIO_VENDOR 0x1AF4
+#define VIRTIO_DEV_NET 0x1000
+#define VIRTIO_DEV_BLOCK 0x1001
+
+/*
+ * PCI config space constants
+ */
+#define VTCFG_R_HOSTCAP 0
+#define VTCFG_R_GUESTCAP 4
+#define VTCFG_R_PFN 8
+#define VTCFG_R_QNUM 12
+#define VTCFG_R_QSEL 14
+#define VTCFG_R_QNOTIFY 16
+#define VTCFG_R_STATUS 18
+#define VTCFG_R_ISR 19
+#define VTCFG_R_CFGVEC 20
+#define VTCFG_R_QVEC 22
+#define VTCFG_R_CFG0 20 /* No MSI-X */
+#define VTCFG_R_CFG1 24 /* With MSI-X */
+#define VTCFG_R_MSIX 20
+
+#endif /* _VIRTIO_H_ */
diff --git a/usr.sbin/bhyve/xmsr.c b/usr.sbin/bhyve/xmsr.c
new file mode 100644
index 0000000..9c05f02
--- /dev/null
+++ b/usr.sbin/bhyve/xmsr.c
@@ -0,0 +1,48 @@
+/*-
+ * Copyright (c) 2011 NetApp, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <sys/types.h>
+
+#include <machine/vmm.h>
+#include <vmmapi.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "xmsr.h"
+
+int
+emulate_wrmsr(struct vmctx *ctx, int vcpu, uint32_t code, uint64_t val)
+{
+
+ printf("Unknown WRMSR code %x, val %lx, cpu %d\n", code, val, vcpu);
+ exit(1);
+}
diff --git a/usr.sbin/bhyve/xmsr.h b/usr.sbin/bhyve/xmsr.h
new file mode 100644
index 0000000..8cebcea
--- /dev/null
+++ b/usr.sbin/bhyve/xmsr.h
@@ -0,0 +1,34 @@
+/*-
+ * Copyright (c) 2011 NetApp, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ */
+
+#ifndef _XMSR_H_
+#define _XMSR_H_
+
+int emulate_wrmsr(struct vmctx *ctx, int vcpu, uint32_t code, uint64_t val);
+
+#endif
diff --git a/usr.sbin/bhyvectl/Makefile b/usr.sbin/bhyvectl/Makefile
new file mode 100644
index 0000000..9fde12c
--- /dev/null
+++ b/usr.sbin/bhyvectl/Makefile
@@ -0,0 +1,17 @@
+#
+# $FreeBSD$
+#
+
+PROG= bhyvectl
+SRCS= bhyvectl.c
+
+NO_MAN=
+
+DPADD= ${LIBVMMAPI}
+LDADD= -lvmmapi
+
+WARNS?= 3
+
+CFLAGS+= -I${.CURDIR}/../../sys/amd64/vmm
+
+.include <bsd.prog.mk>
diff --git a/usr.sbin/bhyvectl/bhyvectl.c b/usr.sbin/bhyvectl/bhyvectl.c
new file mode 100644
index 0000000..35e1388
--- /dev/null
+++ b/usr.sbin/bhyvectl/bhyvectl.c
@@ -0,0 +1,1524 @@
+/*-
+ * Copyright (c) 2011 NetApp, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <sys/param.h>
+#include <sys/types.h>
+#include <sys/sysctl.h>
+#include <sys/errno.h>
+#include <sys/mman.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <libgen.h>
+#include <libutil.h>
+#include <fcntl.h>
+#include <string.h>
+#include <getopt.h>
+#include <assert.h>
+
+#include <machine/vmm.h>
+#include <vmmapi.h>
+
+#include "intel/vmcs.h"
+
+#define MB (1UL << 20)
+#define GB (1UL << 30)
+
+#define REQ_ARG required_argument
+#define NO_ARG no_argument
+#define OPT_ARG optional_argument
+
+static const char *progname;
+
+static void
+usage(void)
+{
+
+ (void)fprintf(stderr,
+ "Usage: %s --vm=<vmname>\n"
+ " [--cpu=<vcpu_number>]\n"
+ " [--create]\n"
+ " [--destroy]\n"
+ " [--get-all]\n"
+ " [--get-stats]\n"
+ " [--set-desc-ds]\n"
+ " [--get-desc-ds]\n"
+ " [--set-desc-es]\n"
+ " [--get-desc-es]\n"
+ " [--set-desc-gs]\n"
+ " [--get-desc-gs]\n"
+ " [--set-desc-fs]\n"
+ " [--get-desc-fs]\n"
+ " [--set-desc-cs]\n"
+ " [--get-desc-cs]\n"
+ " [--set-desc-ss]\n"
+ " [--get-desc-ss]\n"
+ " [--set-desc-tr]\n"
+ " [--get-desc-tr]\n"
+ " [--set-desc-ldtr]\n"
+ " [--get-desc-ldtr]\n"
+ " [--set-desc-gdtr]\n"
+ " [--get-desc-gdtr]\n"
+ " [--set-desc-idtr]\n"
+ " [--get-desc-idtr]\n"
+ " [--run]\n"
+ " [--capname=<capname>]\n"
+ " [--getcap]\n"
+ " [--setcap=<0|1>]\n"
+ " [--desc-base=<BASE>]\n"
+ " [--desc-limit=<LIMIT>]\n"
+ " [--desc-access=<ACCESS>]\n"
+ " [--set-cr0=<CR0>]\n"
+ " [--get-cr0]\n"
+ " [--set-cr3=<CR3>]\n"
+ " [--get-cr3]\n"
+ " [--set-cr4=<CR4>]\n"
+ " [--get-cr4]\n"
+ " [--set-dr7=<DR7>]\n"
+ " [--get-dr7]\n"
+ " [--set-rsp=<RSP>]\n"
+ " [--get-rsp]\n"
+ " [--set-rip=<RIP>]\n"
+ " [--get-rip]\n"
+ " [--get-rax]\n"
+ " [--set-rax=<RAX>]\n"
+ " [--get-rbx]\n"
+ " [--get-rcx]\n"
+ " [--get-rdx]\n"
+ " [--get-rsi]\n"
+ " [--get-rdi]\n"
+ " [--get-rbp]\n"
+ " [--get-r8]\n"
+ " [--get-r9]\n"
+ " [--get-r10]\n"
+ " [--get-r11]\n"
+ " [--get-r12]\n"
+ " [--get-r13]\n"
+ " [--get-r14]\n"
+ " [--get-r15]\n"
+ " [--set-rflags=<RFLAGS>]\n"
+ " [--get-rflags]\n"
+ " [--set-cs]\n"
+ " [--get-cs]\n"
+ " [--set-ds]\n"
+ " [--get-ds]\n"
+ " [--set-es]\n"
+ " [--get-es]\n"
+ " [--set-fs]\n"
+ " [--get-fs]\n"
+ " [--set-gs]\n"
+ " [--get-gs]\n"
+ " [--set-ss]\n"
+ " [--get-ss]\n"
+ " [--get-tr]\n"
+ " [--get-ldtr]\n"
+ " [--get-vmcs-pinbased-ctls]\n"
+ " [--get-vmcs-procbased-ctls]\n"
+ " [--get-vmcs-procbased-ctls2]\n"
+ " [--get-vmcs-entry-interruption-info]\n"
+ " [--set-vmcs-entry-interruption-info=<info>]\n"
+ " [--get-vmcs-eptp]\n"
+ " [--get-vmcs-guest-physical-address\n"
+ " [--get-vmcs-guest-linear-address\n"
+ " [--set-vmcs-exception-bitmap]\n"
+ " [--get-vmcs-exception-bitmap]\n"
+ " [--get-vmcs-io-bitmap-address]\n"
+ " [--get-vmcs-tsc-offset]\n"
+ " [--get-vmcs-guest-pat]\n"
+ " [--get-vmcs-host-pat]\n"
+ " [--get-vmcs-host-cr0]\n"
+ " [--get-vmcs-host-cr3]\n"
+ " [--get-vmcs-host-cr4]\n"
+ " [--get-vmcs-host-rip]\n"
+ " [--get-vmcs-host-rsp]\n"
+ " [--get-vmcs-cr0-mask]\n"
+ " [--get-vmcs-cr0-shadow]\n"
+ " [--get-vmcs-cr4-mask]\n"
+ " [--get-vmcs-cr4-shadow]\n"
+ " [--get-vmcs-cr3-targets]\n"
+ " [--get-vmcs-apic-access-address]\n"
+ " [--get-vmcs-virtual-apic-address]\n"
+ " [--get-vmcs-tpr-threshold]\n"
+ " [--get-vmcs-msr-bitmap]\n"
+ " [--get-vmcs-msr-bitmap-address]\n"
+ " [--get-vmcs-vpid]\n"
+ " [--get-vmcs-ple-gap]\n"
+ " [--get-vmcs-ple-window]\n"
+ " [--get-vmcs-instruction-error]\n"
+ " [--get-vmcs-exit-ctls]\n"
+ " [--get-vmcs-entry-ctls]\n"
+ " [--get-vmcs-guest-sysenter]\n"
+ " [--get-vmcs-link]\n"
+ " [--get-vmcs-exit-reason]\n"
+ " [--get-vmcs-exit-qualification]\n"
+ " [--get-vmcs-exit-interruption-info]\n"
+ " [--get-vmcs-exit-interruption-error]\n"
+ " [--get-vmcs-interruptibility]\n"
+ " [--set-pinning=<host_cpuid>]\n"
+ " [--get-pinning]\n"
+ " [--set-x2apic-state=<state>]\n"
+ " [--get-x2apic-state]\n"
+ " [--set-lowmem=<memory below 4GB in units of MB>]\n"
+ " [--get-lowmem]\n"
+ " [--set-highmem=<memory above 4GB in units of MB>]\n"
+ " [--get-highmem]\n",
+ progname);
+ exit(1);
+}
+
+static int get_stats, getcap, setcap, capval;
+static const char *capname;
+static int create, destroy, get_lowmem, get_highmem;
+static uint64_t lowmem, highmem;
+static int set_cr0, get_cr0, set_cr3, get_cr3, set_cr4, get_cr4;
+static int set_efer, get_efer;
+static int set_dr7, get_dr7;
+static int set_rsp, get_rsp, set_rip, get_rip, set_rflags, get_rflags;
+static int set_rax, get_rax;
+static int get_rbx, get_rcx, get_rdx, get_rsi, get_rdi, get_rbp;
+static int get_r8, get_r9, get_r10, get_r11, get_r12, get_r13, get_r14, get_r15;
+static int set_desc_ds, get_desc_ds;
+static int set_desc_es, get_desc_es;
+static int set_desc_fs, get_desc_fs;
+static int set_desc_gs, get_desc_gs;
+static int set_desc_cs, get_desc_cs;
+static int set_desc_ss, get_desc_ss;
+static int set_desc_gdtr, get_desc_gdtr;
+static int set_desc_idtr, get_desc_idtr;
+static int set_desc_tr, get_desc_tr;
+static int set_desc_ldtr, get_desc_ldtr;
+static int set_cs, set_ds, set_es, set_fs, set_gs, set_ss, set_tr, set_ldtr;
+static int get_cs, get_ds, get_es, get_fs, get_gs, get_ss, get_tr, get_ldtr;
+static int set_pinning, get_pinning, pincpu;
+static int set_x2apic_state, get_x2apic_state;
+enum x2apic_state x2apic_state;
+static int run;
+
+/*
+ * VMCS-specific fields
+ */
+static int get_pinbased_ctls, get_procbased_ctls, get_procbased_ctls2;
+static int get_eptp, get_io_bitmap, get_tsc_offset;
+static int get_vmcs_entry_interruption_info, set_vmcs_entry_interruption_info;
+static int get_vmcs_interruptibility;
+uint32_t vmcs_entry_interruption_info;
+static int get_vmcs_gpa, get_vmcs_gla;
+static int get_exception_bitmap, set_exception_bitmap, exception_bitmap;
+static int get_cr0_mask, get_cr0_shadow;
+static int get_cr4_mask, get_cr4_shadow;
+static int get_cr3_targets;
+static int get_apic_access_addr, get_virtual_apic_addr, get_tpr_threshold;
+static int get_msr_bitmap, get_msr_bitmap_address;
+static int get_vpid, get_ple_gap, get_ple_window;
+static int get_inst_err, get_exit_ctls, get_entry_ctls;
+static int get_host_cr0, get_host_cr3, get_host_cr4;
+static int get_host_rip, get_host_rsp;
+static int get_guest_pat, get_host_pat;
+static int get_guest_sysenter, get_vmcs_link;
+static int get_vmcs_exit_reason, get_vmcs_exit_qualification;
+static int get_vmcs_exit_interruption_info, get_vmcs_exit_interruption_error;
+
+static uint64_t desc_base;
+static uint32_t desc_limit, desc_access;
+
+static int get_all;
+
+static void
+dump_vm_run_exitcode(struct vm_exit *vmexit, int vcpu)
+{
+ printf("vm exit[%d]\n", vcpu);
+ printf("\trip\t\t0x%016lx\n", vmexit->rip);
+ printf("\tinst_length\t%d\n", vmexit->inst_length);
+ switch (vmexit->exitcode) {
+ case VM_EXITCODE_INOUT:
+ printf("\treason\t\tINOUT\n");
+ printf("\tdirection\t%s\n", vmexit->u.inout.in ? "IN" : "OUT");
+ printf("\tbytes\t\t%d\n", vmexit->u.inout.bytes);
+ printf("\tflags\t\t%s%s\n",
+ vmexit->u.inout.string ? "STRING " : "",
+ vmexit->u.inout.rep ? "REP " : "");
+ printf("\tport\t\t0x%04x\n", vmexit->u.inout.port);
+ printf("\teax\t\t0x%08x\n", vmexit->u.inout.eax);
+ break;
+ case VM_EXITCODE_VMX:
+ printf("\treason\t\tVMX\n");
+ printf("\terror\t\t%d\n", vmexit->u.vmx.error);
+ printf("\texit_reason\t0x%08x (%u)\n",
+ vmexit->u.vmx.exit_reason, vmexit->u.vmx.exit_reason);
+ printf("\tqualification\t0x%016lx\n",
+ vmexit->u.vmx.exit_qualification);
+ break;
+ default:
+ printf("*** unknown vm run exitcode %d\n", vmexit->exitcode);
+ break;
+ }
+}
+
+static int
+dump_vmcs_msr_bitmap(int vcpu, u_long addr)
+{
+ int error, fd, byte, bit, readable, writeable;
+ u_int msr;
+ const char *bitmap;
+
+ error = -1;
+ bitmap = MAP_FAILED;
+
+ fd = open("/dev/mem", O_RDONLY, 0);
+ if (fd < 0)
+ goto done;
+
+ bitmap = mmap(NULL, PAGE_SIZE, PROT_READ, 0, fd, addr);
+ if (bitmap == MAP_FAILED)
+ goto done;
+
+ for (msr = 0; msr < 0x2000; msr++) {
+ byte = msr / 8;
+ bit = msr & 0x7;
+
+ /* Look at MSRs in the range 0x00000000 to 0x00001FFF */
+ readable = (bitmap[byte] & (1 << bit)) ? 0 : 1;
+ writeable = (bitmap[2048 + byte] & (1 << bit)) ? 0 : 1;
+ if (readable || writeable) {
+ printf("msr 0x%08x[%d]\t\t%c%c\n", msr, vcpu,
+ readable ? 'R' : '-',
+ writeable ? 'W' : '-');
+ }
+
+ /* Look at MSRs in the range 0xC0000000 to 0xC0001FFF */
+ byte += 1024;
+ readable = (bitmap[byte] & (1 << bit)) ? 0 : 1;
+ writeable = (bitmap[2048 + byte] & (1 << bit)) ? 0 : 1;
+ if (readable || writeable) {
+ printf("msr 0x%08x[%d]\t\t%c%c\n",
+ 0xc0000000 + msr, vcpu,
+ readable ? 'R' : '-',
+ writeable ? 'W' : '-');
+ }
+ }
+
+ error = 0;
+done:
+ if (bitmap != MAP_FAILED)
+ munmap((void *)bitmap, PAGE_SIZE);
+ if (fd >= 0)
+ close(fd);
+ return (error);
+}
+
+static int
+vm_get_vmcs_field(struct vmctx *ctx, int vcpu, int field, uint64_t *ret_val)
+{
+
+ return (vm_get_register(ctx, vcpu, VMCS_IDENT(field), ret_val));
+}
+
+static int
+vm_set_vmcs_field(struct vmctx *ctx, int vcpu, int field, uint64_t val)
+{
+
+ return (vm_set_register(ctx, vcpu, VMCS_IDENT(field), val));
+}
+
+enum {
+ VMNAME = 1000, /* avoid collision with return values from getopt */
+ VCPU,
+ SET_LOWMEM,
+ SET_HIGHMEM,
+ SET_EFER,
+ SET_CR0,
+ SET_CR3,
+ SET_CR4,
+ SET_DR7,
+ SET_RSP,
+ SET_RIP,
+ SET_RAX,
+ SET_RFLAGS,
+ DESC_BASE,
+ DESC_LIMIT,
+ DESC_ACCESS,
+ SET_CS,
+ SET_DS,
+ SET_ES,
+ SET_FS,
+ SET_GS,
+ SET_SS,
+ SET_TR,
+ SET_LDTR,
+ SET_PINNING,
+ SET_X2APIC_STATE,
+ SET_VMCS_EXCEPTION_BITMAP,
+ SET_VMCS_ENTRY_INTERRUPTION_INFO,
+ SET_CAP,
+ CAPNAME,
+};
+
+int
+main(int argc, char *argv[])
+{
+ char *vmname;
+ int error, ch, vcpu;
+ vm_paddr_t gpa;
+ size_t len;
+ struct vm_exit vmexit;
+ uint64_t ctl, eptp, bm, addr, u64;
+ struct vmctx *ctx;
+
+ uint64_t cr0, cr3, cr4, dr7, rsp, rip, rflags, efer, pat;
+ uint64_t rax, rbx, rcx, rdx, rsi, rdi, rbp;
+ uint64_t r8, r9, r10, r11, r12, r13, r14, r15;
+ uint64_t cs, ds, es, fs, gs, ss, tr, ldtr;
+
+ struct option opts[] = {
+ { "vm", REQ_ARG, 0, VMNAME },
+ { "cpu", REQ_ARG, 0, VCPU },
+ { "set-lowmem", REQ_ARG, 0, SET_LOWMEM },
+ { "set-highmem",REQ_ARG, 0, SET_HIGHMEM },
+ { "set-efer", REQ_ARG, 0, SET_EFER },
+ { "set-cr0", REQ_ARG, 0, SET_CR0 },
+ { "set-cr3", REQ_ARG, 0, SET_CR3 },
+ { "set-cr4", REQ_ARG, 0, SET_CR4 },
+ { "set-dr7", REQ_ARG, 0, SET_DR7 },
+ { "set-rsp", REQ_ARG, 0, SET_RSP },
+ { "set-rip", REQ_ARG, 0, SET_RIP },
+ { "set-rax", REQ_ARG, 0, SET_RAX },
+ { "set-rflags", REQ_ARG, 0, SET_RFLAGS },
+ { "desc-base", REQ_ARG, 0, DESC_BASE },
+ { "desc-limit", REQ_ARG, 0, DESC_LIMIT },
+ { "desc-access",REQ_ARG, 0, DESC_ACCESS },
+ { "set-cs", REQ_ARG, 0, SET_CS },
+ { "set-ds", REQ_ARG, 0, SET_DS },
+ { "set-es", REQ_ARG, 0, SET_ES },
+ { "set-fs", REQ_ARG, 0, SET_FS },
+ { "set-gs", REQ_ARG, 0, SET_GS },
+ { "set-ss", REQ_ARG, 0, SET_SS },
+ { "set-tr", REQ_ARG, 0, SET_TR },
+ { "set-ldtr", REQ_ARG, 0, SET_LDTR },
+ { "set-pinning",REQ_ARG, 0, SET_PINNING },
+ { "set-x2apic-state",REQ_ARG, 0, SET_X2APIC_STATE },
+ { "set-vmcs-exception-bitmap",
+ REQ_ARG, 0, SET_VMCS_EXCEPTION_BITMAP },
+ { "set-vmcs-entry-interruption-info",
+ REQ_ARG, 0, SET_VMCS_ENTRY_INTERRUPTION_INFO },
+ { "capname", REQ_ARG, 0, CAPNAME },
+ { "setcap", REQ_ARG, 0, SET_CAP },
+ { "getcap", NO_ARG, &getcap, 1 },
+ { "get-stats", NO_ARG, &get_stats, 1 },
+ { "get-desc-ds",NO_ARG, &get_desc_ds, 1 },
+ { "set-desc-ds",NO_ARG, &set_desc_ds, 1 },
+ { "get-desc-es",NO_ARG, &get_desc_es, 1 },
+ { "set-desc-es",NO_ARG, &set_desc_es, 1 },
+ { "get-desc-ss",NO_ARG, &get_desc_ss, 1 },
+ { "set-desc-ss",NO_ARG, &set_desc_ss, 1 },
+ { "get-desc-cs",NO_ARG, &get_desc_cs, 1 },
+ { "set-desc-cs",NO_ARG, &set_desc_cs, 1 },
+ { "get-desc-fs",NO_ARG, &get_desc_fs, 1 },
+ { "set-desc-fs",NO_ARG, &set_desc_fs, 1 },
+ { "get-desc-gs",NO_ARG, &get_desc_gs, 1 },
+ { "set-desc-gs",NO_ARG, &set_desc_gs, 1 },
+ { "get-desc-tr",NO_ARG, &get_desc_tr, 1 },
+ { "set-desc-tr",NO_ARG, &set_desc_tr, 1 },
+ { "set-desc-ldtr", NO_ARG, &set_desc_ldtr, 1 },
+ { "get-desc-ldtr", NO_ARG, &get_desc_ldtr, 1 },
+ { "set-desc-gdtr", NO_ARG, &set_desc_gdtr, 1 },
+ { "get-desc-gdtr", NO_ARG, &get_desc_gdtr, 1 },
+ { "set-desc-idtr", NO_ARG, &set_desc_idtr, 1 },
+ { "get-desc-idtr", NO_ARG, &get_desc_idtr, 1 },
+ { "get-lowmem", NO_ARG, &get_lowmem, 1 },
+ { "get-highmem",NO_ARG, &get_highmem, 1 },
+ { "get-efer", NO_ARG, &get_efer, 1 },
+ { "get-cr0", NO_ARG, &get_cr0, 1 },
+ { "get-cr3", NO_ARG, &get_cr3, 1 },
+ { "get-cr4", NO_ARG, &get_cr4, 1 },
+ { "get-dr7", NO_ARG, &get_dr7, 1 },
+ { "get-rsp", NO_ARG, &get_rsp, 1 },
+ { "get-rip", NO_ARG, &get_rip, 1 },
+ { "get-rax", NO_ARG, &get_rax, 1 },
+ { "get-rbx", NO_ARG, &get_rbx, 1 },
+ { "get-rcx", NO_ARG, &get_rcx, 1 },
+ { "get-rdx", NO_ARG, &get_rdx, 1 },
+ { "get-rsi", NO_ARG, &get_rsi, 1 },
+ { "get-rdi", NO_ARG, &get_rdi, 1 },
+ { "get-rbp", NO_ARG, &get_rbp, 1 },
+ { "get-r8", NO_ARG, &get_r8, 1 },
+ { "get-r9", NO_ARG, &get_r9, 1 },
+ { "get-r10", NO_ARG, &get_r10, 1 },
+ { "get-r11", NO_ARG, &get_r11, 1 },
+ { "get-r12", NO_ARG, &get_r12, 1 },
+ { "get-r13", NO_ARG, &get_r13, 1 },
+ { "get-r14", NO_ARG, &get_r14, 1 },
+ { "get-r15", NO_ARG, &get_r15, 1 },
+ { "get-rflags", NO_ARG, &get_rflags, 1 },
+ { "get-cs", NO_ARG, &get_cs, 1 },
+ { "get-ds", NO_ARG, &get_ds, 1 },
+ { "get-es", NO_ARG, &get_es, 1 },
+ { "get-fs", NO_ARG, &get_fs, 1 },
+ { "get-gs", NO_ARG, &get_gs, 1 },
+ { "get-ss", NO_ARG, &get_ss, 1 },
+ { "get-tr", NO_ARG, &get_tr, 1 },
+ { "get-ldtr", NO_ARG, &get_ldtr, 1 },
+ { "get-vmcs-pinbased-ctls",
+ NO_ARG, &get_pinbased_ctls, 1 },
+ { "get-vmcs-procbased-ctls",
+ NO_ARG, &get_procbased_ctls, 1 },
+ { "get-vmcs-procbased-ctls2",
+ NO_ARG, &get_procbased_ctls2, 1 },
+ { "get-vmcs-guest-linear-address",
+ NO_ARG, &get_vmcs_gla, 1 },
+ { "get-vmcs-guest-physical-address",
+ NO_ARG, &get_vmcs_gpa, 1 },
+ { "get-vmcs-entry-interruption-info",
+ NO_ARG, &get_vmcs_entry_interruption_info, 1},
+ { "get-vmcs-eptp", NO_ARG, &get_eptp, 1 },
+ { "get-vmcs-exception-bitmap",
+ NO_ARG, &get_exception_bitmap, 1 },
+ { "get-vmcs-io-bitmap-address",
+ NO_ARG, &get_io_bitmap, 1 },
+ { "get-vmcs-tsc-offset", NO_ARG,&get_tsc_offset, 1 },
+ { "get-vmcs-cr0-mask", NO_ARG, &get_cr0_mask, 1 },
+ { "get-vmcs-cr0-shadow", NO_ARG,&get_cr0_shadow, 1 },
+ { "get-vmcs-cr4-mask", NO_ARG, &get_cr4_mask, 1 },
+ { "get-vmcs-cr4-shadow", NO_ARG,&get_cr4_shadow, 1 },
+ { "get-vmcs-cr3-targets", NO_ARG, &get_cr3_targets, 1},
+ { "get-vmcs-apic-access-address",
+ NO_ARG, &get_apic_access_addr, 1},
+ { "get-vmcs-virtual-apic-address",
+ NO_ARG, &get_virtual_apic_addr, 1},
+ { "get-vmcs-tpr-threshold",
+ NO_ARG, &get_tpr_threshold, 1 },
+ { "get-vmcs-msr-bitmap",
+ NO_ARG, &get_msr_bitmap, 1 },
+ { "get-vmcs-msr-bitmap-address",
+ NO_ARG, &get_msr_bitmap_address, 1 },
+ { "get-vmcs-vpid", NO_ARG, &get_vpid, 1 },
+ { "get-vmcs-ple-gap", NO_ARG, &get_ple_gap, 1 },
+ { "get-vmcs-ple-window", NO_ARG,&get_ple_window,1 },
+ { "get-vmcs-instruction-error",
+ NO_ARG, &get_inst_err, 1 },
+ { "get-vmcs-exit-ctls", NO_ARG, &get_exit_ctls, 1 },
+ { "get-vmcs-entry-ctls",
+ NO_ARG, &get_entry_ctls, 1 },
+ { "get-vmcs-guest-pat", NO_ARG, &get_guest_pat, 1 },
+ { "get-vmcs-host-pat", NO_ARG, &get_host_pat, 1 },
+ { "get-vmcs-host-cr0",
+ NO_ARG, &get_host_cr0, 1 },
+ { "get-vmcs-host-cr3",
+ NO_ARG, &get_host_cr3, 1 },
+ { "get-vmcs-host-cr4",
+ NO_ARG, &get_host_cr4, 1 },
+ { "get-vmcs-host-rip",
+ NO_ARG, &get_host_rip, 1 },
+ { "get-vmcs-host-rsp",
+ NO_ARG, &get_host_rsp, 1 },
+ { "get-vmcs-guest-sysenter",
+ NO_ARG, &get_guest_sysenter, 1 },
+ { "get-vmcs-link", NO_ARG, &get_vmcs_link, 1 },
+ { "get-vmcs-exit-reason",
+ NO_ARG, &get_vmcs_exit_reason, 1 },
+ { "get-vmcs-exit-qualification",
+ NO_ARG, &get_vmcs_exit_qualification, 1 },
+ { "get-vmcs-exit-interruption-info",
+ NO_ARG, &get_vmcs_exit_interruption_info, 1},
+ { "get-vmcs-exit-interruption-error",
+ NO_ARG, &get_vmcs_exit_interruption_error, 1},
+ { "get-vmcs-interruptibility",
+ NO_ARG, &get_vmcs_interruptibility, 1 },
+ { "get-pinning",NO_ARG, &get_pinning, 1 },
+ { "get-x2apic-state",NO_ARG, &get_x2apic_state, 1 },
+ { "get-all", NO_ARG, &get_all, 1 },
+ { "run", NO_ARG, &run, 1 },
+ { "create", NO_ARG, &create, 1 },
+ { "destroy", NO_ARG, &destroy, 1 },
+ { NULL, 0, NULL, 0 }
+ };
+
+ vcpu = 0;
+ progname = basename(argv[0]);
+
+ while ((ch = getopt_long(argc, argv, "", opts, NULL)) != -1) {
+ switch (ch) {
+ case 0:
+ break;
+ case VMNAME:
+ vmname = optarg;
+ break;
+ case VCPU:
+ vcpu = atoi(optarg);
+ break;
+ case SET_LOWMEM:
+ lowmem = atoi(optarg) * MB;
+ lowmem = roundup(lowmem, 2 * MB);
+ break;
+ case SET_HIGHMEM:
+ highmem = atoi(optarg) * MB;
+ highmem = roundup(highmem, 2 * MB);
+ break;
+ case SET_EFER:
+ efer = strtoul(optarg, NULL, 0);
+ set_efer = 1;
+ break;
+ case SET_CR0:
+ cr0 = strtoul(optarg, NULL, 0);
+ set_cr0 = 1;
+ break;
+ case SET_CR3:
+ cr3 = strtoul(optarg, NULL, 0);
+ set_cr3 = 1;
+ break;
+ case SET_CR4:
+ cr4 = strtoul(optarg, NULL, 0);
+ set_cr4 = 1;
+ break;
+ case SET_DR7:
+ dr7 = strtoul(optarg, NULL, 0);
+ set_dr7 = 1;
+ break;
+ case SET_RSP:
+ rsp = strtoul(optarg, NULL, 0);
+ set_rsp = 1;
+ break;
+ case SET_RIP:
+ rip = strtoul(optarg, NULL, 0);
+ set_rip = 1;
+ break;
+ case SET_RAX:
+ rax = strtoul(optarg, NULL, 0);
+ set_rax = 1;
+ break;
+ case SET_RFLAGS:
+ rflags = strtoul(optarg, NULL, 0);
+ set_rflags = 1;
+ break;
+ case DESC_BASE:
+ desc_base = strtoul(optarg, NULL, 0);
+ break;
+ case DESC_LIMIT:
+ desc_limit = strtoul(optarg, NULL, 0);
+ break;
+ case DESC_ACCESS:
+ desc_access = strtoul(optarg, NULL, 0);
+ break;
+ case SET_CS:
+ cs = strtoul(optarg, NULL, 0);
+ set_cs = 1;
+ break;
+ case SET_DS:
+ ds = strtoul(optarg, NULL, 0);
+ set_ds = 1;
+ break;
+ case SET_ES:
+ es = strtoul(optarg, NULL, 0);
+ set_es = 1;
+ break;
+ case SET_FS:
+ fs = strtoul(optarg, NULL, 0);
+ set_fs = 1;
+ break;
+ case SET_GS:
+ gs = strtoul(optarg, NULL, 0);
+ set_gs = 1;
+ break;
+ case SET_SS:
+ ss = strtoul(optarg, NULL, 0);
+ set_ss = 1;
+ break;
+ case SET_TR:
+ tr = strtoul(optarg, NULL, 0);
+ set_tr = 1;
+ break;
+ case SET_LDTR:
+ ldtr = strtoul(optarg, NULL, 0);
+ set_ldtr = 1;
+ break;
+ case SET_PINNING:
+ pincpu = strtol(optarg, NULL, 0);
+ set_pinning = 1;
+ break;
+ case SET_X2APIC_STATE:
+ x2apic_state = strtol(optarg, NULL, 0);
+ set_x2apic_state = 1;
+ break;
+ case SET_VMCS_EXCEPTION_BITMAP:
+ exception_bitmap = strtoul(optarg, NULL, 0);
+ set_exception_bitmap = 1;
+ break;
+ case SET_VMCS_ENTRY_INTERRUPTION_INFO:
+ vmcs_entry_interruption_info = strtoul(optarg, NULL, 0);
+ set_vmcs_entry_interruption_info = 1;
+ break;
+ case SET_CAP:
+ capval = strtoul(optarg, NULL, 0);
+ setcap = 1;
+ break;
+ case CAPNAME:
+ capname = optarg;
+ break;
+ default:
+ usage();
+ }
+ }
+ argc -= optind;
+ argv += optind;
+
+ if (vmname == NULL)
+ usage();
+
+ error = 0;
+
+ if (!error && create)
+ error = vm_create(vmname);
+
+ if (!error) {
+ ctx = vm_open(vmname);
+ if (ctx == NULL)
+ error = -1;
+ }
+
+ if (!error && lowmem)
+ error = vm_setup_memory(ctx, 0, lowmem, NULL);
+
+ if (!error && highmem)
+ error = vm_setup_memory(ctx, 4 * GB, highmem, NULL);
+
+ if (!error && set_efer)
+ error = vm_set_register(ctx, vcpu, VM_REG_GUEST_EFER, efer);
+
+ if (!error && set_cr0)
+ error = vm_set_register(ctx, vcpu, VM_REG_GUEST_CR0, cr0);
+
+ if (!error && set_cr3)
+ error = vm_set_register(ctx, vcpu, VM_REG_GUEST_CR3, cr3);
+
+ if (!error && set_cr4)
+ error = vm_set_register(ctx, vcpu, VM_REG_GUEST_CR4, cr4);
+
+ if (!error && set_dr7)
+ error = vm_set_register(ctx, vcpu, VM_REG_GUEST_DR7, dr7);
+
+ if (!error && set_rsp)
+ error = vm_set_register(ctx, vcpu, VM_REG_GUEST_RSP, rsp);
+
+ if (!error && set_rip)
+ error = vm_set_register(ctx, vcpu, VM_REG_GUEST_RIP, rip);
+
+ if (!error && set_rax)
+ error = vm_set_register(ctx, vcpu, VM_REG_GUEST_RAX, rax);
+
+ if (!error && set_rflags) {
+ error = vm_set_register(ctx, vcpu, VM_REG_GUEST_RFLAGS,
+ rflags);
+ }
+
+ if (!error && set_desc_ds) {
+ error = vm_set_desc(ctx, vcpu, VM_REG_GUEST_DS,
+ desc_base, desc_limit, desc_access);
+ }
+
+ if (!error && set_desc_es) {
+ error = vm_set_desc(ctx, vcpu, VM_REG_GUEST_ES,
+ desc_base, desc_limit, desc_access);
+ }
+
+ if (!error && set_desc_ss) {
+ error = vm_set_desc(ctx, vcpu, VM_REG_GUEST_SS,
+ desc_base, desc_limit, desc_access);
+ }
+
+ if (!error && set_desc_cs) {
+ error = vm_set_desc(ctx, vcpu, VM_REG_GUEST_CS,
+ desc_base, desc_limit, desc_access);
+ }
+
+ if (!error && set_desc_fs) {
+ error = vm_set_desc(ctx, vcpu, VM_REG_GUEST_FS,
+ desc_base, desc_limit, desc_access);
+ }
+
+ if (!error && set_desc_gs) {
+ error = vm_set_desc(ctx, vcpu, VM_REG_GUEST_GS,
+ desc_base, desc_limit, desc_access);
+ }
+
+ if (!error && set_desc_tr) {
+ error = vm_set_desc(ctx, vcpu, VM_REG_GUEST_TR,
+ desc_base, desc_limit, desc_access);
+ }
+
+ if (!error && set_desc_ldtr) {
+ error = vm_set_desc(ctx, vcpu, VM_REG_GUEST_LDTR,
+ desc_base, desc_limit, desc_access);
+ }
+
+ if (!error && set_desc_gdtr) {
+ error = vm_set_desc(ctx, vcpu, VM_REG_GUEST_GDTR,
+ desc_base, desc_limit, 0);
+ }
+
+ if (!error && set_desc_idtr) {
+ error = vm_set_desc(ctx, vcpu, VM_REG_GUEST_IDTR,
+ desc_base, desc_limit, 0);
+ }
+
+ if (!error && set_cs)
+ error = vm_set_register(ctx, vcpu, VM_REG_GUEST_CS, cs);
+
+ if (!error && set_ds)
+ error = vm_set_register(ctx, vcpu, VM_REG_GUEST_DS, ds);
+
+ if (!error && set_es)
+ error = vm_set_register(ctx, vcpu, VM_REG_GUEST_ES, es);
+
+ if (!error && set_fs)
+ error = vm_set_register(ctx, vcpu, VM_REG_GUEST_FS, fs);
+
+ if (!error && set_gs)
+ error = vm_set_register(ctx, vcpu, VM_REG_GUEST_GS, gs);
+
+ if (!error && set_ss)
+ error = vm_set_register(ctx, vcpu, VM_REG_GUEST_SS, ss);
+
+ if (!error && set_tr)
+ error = vm_set_register(ctx, vcpu, VM_REG_GUEST_TR, tr);
+
+ if (!error && set_ldtr)
+ error = vm_set_register(ctx, vcpu, VM_REG_GUEST_LDTR, ldtr);
+
+ if (!error && set_pinning)
+ error = vm_set_pinning(ctx, vcpu, pincpu);
+
+ if (!error && set_x2apic_state)
+ error = vm_set_x2apic_state(ctx, vcpu, x2apic_state);
+
+ if (!error && set_exception_bitmap) {
+ error = vm_set_vmcs_field(ctx, vcpu, VMCS_EXCEPTION_BITMAP,
+ exception_bitmap);
+ }
+
+ if (!error && set_vmcs_entry_interruption_info) {
+ error = vm_set_vmcs_field(ctx, vcpu, VMCS_ENTRY_INTR_INFO,
+ vmcs_entry_interruption_info);
+ }
+
+ if (!error && (get_lowmem || get_all)) {
+ gpa = 0;
+ error = vm_get_memory_seg(ctx, gpa, &len);
+ if (error == 0)
+ printf("lowmem\t\t0x%016lx/%ld\n", gpa, len);
+ }
+
+ if (!error && (get_highmem || get_all)) {
+ gpa = 4 * GB;
+ error = vm_get_memory_seg(ctx, gpa, &len);
+ if (error == 0)
+ printf("highmem\t\t0x%016lx/%ld\n", gpa, len);
+ }
+
+ if (!error && (get_efer || get_all)) {
+ error = vm_get_register(ctx, vcpu, VM_REG_GUEST_EFER, &efer);
+ if (error == 0)
+ printf("efer[%d]\t\t0x%016lx\n", vcpu, efer);
+ }
+
+ if (!error && (get_cr0 || get_all)) {
+ error = vm_get_register(ctx, vcpu, VM_REG_GUEST_CR0, &cr0);
+ if (error == 0)
+ printf("cr0[%d]\t\t0x%016lx\n", vcpu, cr0);
+ }
+
+ if (!error && (get_cr3 || get_all)) {
+ error = vm_get_register(ctx, vcpu, VM_REG_GUEST_CR3, &cr3);
+ if (error == 0)
+ printf("cr3[%d]\t\t0x%016lx\n", vcpu, cr3);
+ }
+
+ if (!error && (get_cr4 || get_all)) {
+ error = vm_get_register(ctx, vcpu, VM_REG_GUEST_CR4, &cr4);
+ if (error == 0)
+ printf("cr4[%d]\t\t0x%016lx\n", vcpu, cr4);
+ }
+
+ if (!error && (get_dr7 || get_all)) {
+ error = vm_get_register(ctx, vcpu, VM_REG_GUEST_DR7, &dr7);
+ if (error == 0)
+ printf("dr7[%d]\t\t0x%016lx\n", vcpu, dr7);
+ }
+
+ if (!error && (get_rsp || get_all)) {
+ error = vm_get_register(ctx, vcpu, VM_REG_GUEST_RSP, &rsp);
+ if (error == 0)
+ printf("rsp[%d]\t\t0x%016lx\n", vcpu, rsp);
+ }
+
+ if (!error && (get_rip || get_all)) {
+ error = vm_get_register(ctx, vcpu, VM_REG_GUEST_RIP, &rip);
+ if (error == 0)
+ printf("rip[%d]\t\t0x%016lx\n", vcpu, rip);
+ }
+
+ if (!error && (get_rax || get_all)) {
+ error = vm_get_register(ctx, vcpu, VM_REG_GUEST_RAX, &rax);
+ if (error == 0)
+ printf("rax[%d]\t\t0x%016lx\n", vcpu, rax);
+ }
+
+ if (!error && (get_rbx || get_all)) {
+ error = vm_get_register(ctx, vcpu, VM_REG_GUEST_RBX, &rbx);
+ if (error == 0)
+ printf("rbx[%d]\t\t0x%016lx\n", vcpu, rbx);
+ }
+
+ if (!error && (get_rcx || get_all)) {
+ error = vm_get_register(ctx, vcpu, VM_REG_GUEST_RCX, &rcx);
+ if (error == 0)
+ printf("rcx[%d]\t\t0x%016lx\n", vcpu, rcx);
+ }
+
+ if (!error && (get_rdx || get_all)) {
+ error = vm_get_register(ctx, vcpu, VM_REG_GUEST_RDX, &rdx);
+ if (error == 0)
+ printf("rdx[%d]\t\t0x%016lx\n", vcpu, rdx);
+ }
+
+ if (!error && (get_rsi || get_all)) {
+ error = vm_get_register(ctx, vcpu, VM_REG_GUEST_RSI, &rsi);
+ if (error == 0)
+ printf("rsi[%d]\t\t0x%016lx\n", vcpu, rsi);
+ }
+
+ if (!error && (get_rdi || get_all)) {
+ error = vm_get_register(ctx, vcpu, VM_REG_GUEST_RDI, &rdi);
+ if (error == 0)
+ printf("rdi[%d]\t\t0x%016lx\n", vcpu, rdi);
+ }
+
+ if (!error && (get_rbp || get_all)) {
+ error = vm_get_register(ctx, vcpu, VM_REG_GUEST_RBP, &rbp);
+ if (error == 0)
+ printf("rbp[%d]\t\t0x%016lx\n", vcpu, rbp);
+ }
+
+ if (!error && (get_r8 || get_all)) {
+ error = vm_get_register(ctx, vcpu, VM_REG_GUEST_R8, &r8);
+ if (error == 0)
+ printf("r8[%d]\t\t0x%016lx\n", vcpu, r8);
+ }
+
+ if (!error && (get_r9 || get_all)) {
+ error = vm_get_register(ctx, vcpu, VM_REG_GUEST_R9, &r9);
+ if (error == 0)
+ printf("r9[%d]\t\t0x%016lx\n", vcpu, r9);
+ }
+
+ if (!error && (get_r10 || get_all)) {
+ error = vm_get_register(ctx, vcpu, VM_REG_GUEST_R10, &r10);
+ if (error == 0)
+ printf("r10[%d]\t\t0x%016lx\n", vcpu, r10);
+ }
+
+ if (!error && (get_r11 || get_all)) {
+ error = vm_get_register(ctx, vcpu, VM_REG_GUEST_R11, &r11);
+ if (error == 0)
+ printf("r11[%d]\t\t0x%016lx\n", vcpu, r11);
+ }
+
+ if (!error && (get_r12 || get_all)) {
+ error = vm_get_register(ctx, vcpu, VM_REG_GUEST_R12, &r12);
+ if (error == 0)
+ printf("r12[%d]\t\t0x%016lx\n", vcpu, r12);
+ }
+
+ if (!error && (get_r13 || get_all)) {
+ error = vm_get_register(ctx, vcpu, VM_REG_GUEST_R13, &r13);
+ if (error == 0)
+ printf("r13[%d]\t\t0x%016lx\n", vcpu, r13);
+ }
+
+ if (!error && (get_r14 || get_all)) {
+ error = vm_get_register(ctx, vcpu, VM_REG_GUEST_R14, &r14);
+ if (error == 0)
+ printf("r14[%d]\t\t0x%016lx\n", vcpu, r14);
+ }
+
+ if (!error && (get_r15 || get_all)) {
+ error = vm_get_register(ctx, vcpu, VM_REG_GUEST_R15, &r15);
+ if (error == 0)
+ printf("r15[%d]\t\t0x%016lx\n", vcpu, r15);
+ }
+
+ if (!error && (get_rflags || get_all)) {
+ error = vm_get_register(ctx, vcpu, VM_REG_GUEST_RFLAGS,
+ &rflags);
+ if (error == 0)
+ printf("rflags[%d]\t0x%016lx\n", vcpu, rflags);
+ }
+
+ if (!error && (get_stats || get_all)) {
+ int i, num_stats;
+ uint64_t *stats;
+ struct timeval tv;
+ const char *desc;
+
+ stats = vm_get_stats(ctx, vcpu, &tv, &num_stats);
+ if (stats != NULL) {
+ printf("vcpu%d\n", vcpu);
+ for (i = 0; i < num_stats; i++) {
+ desc = vm_get_stat_desc(ctx, i);
+ printf("%-32s\t%ld\n", desc, stats[i]);
+ }
+ }
+ }
+
+ if (!error && (get_desc_ds || get_all)) {
+ error = vm_get_desc(ctx, vcpu, VM_REG_GUEST_DS,
+ &desc_base, &desc_limit, &desc_access);
+ if (error == 0) {
+ printf("ds desc[%d]\t0x%016lx/0x%08x/0x%08x\n",
+ vcpu, desc_base, desc_limit, desc_access);
+ }
+ }
+
+ if (!error && (get_desc_es || get_all)) {
+ error = vm_get_desc(ctx, vcpu, VM_REG_GUEST_ES,
+ &desc_base, &desc_limit, &desc_access);
+ if (error == 0) {
+ printf("es desc[%d]\t0x%016lx/0x%08x/0x%08x\n",
+ vcpu, desc_base, desc_limit, desc_access);
+ }
+ }
+
+ if (!error && (get_desc_fs || get_all)) {
+ error = vm_get_desc(ctx, vcpu, VM_REG_GUEST_FS,
+ &desc_base, &desc_limit, &desc_access);
+ if (error == 0) {
+ printf("fs desc[%d]\t0x%016lx/0x%08x/0x%08x\n",
+ vcpu, desc_base, desc_limit, desc_access);
+ }
+ }
+
+ if (!error && (get_desc_gs || get_all)) {
+ error = vm_get_desc(ctx, vcpu, VM_REG_GUEST_GS,
+ &desc_base, &desc_limit, &desc_access);
+ if (error == 0) {
+ printf("gs desc[%d]\t0x%016lx/0x%08x/0x%08x\n",
+ vcpu, desc_base, desc_limit, desc_access);
+ }
+ }
+
+ if (!error && (get_desc_ss || get_all)) {
+ error = vm_get_desc(ctx, vcpu, VM_REG_GUEST_SS,
+ &desc_base, &desc_limit, &desc_access);
+ if (error == 0) {
+ printf("ss desc[%d]\t0x%016lx/0x%08x/0x%08x\n",
+ vcpu, desc_base, desc_limit, desc_access);
+ }
+ }
+
+ if (!error && (get_desc_cs || get_all)) {
+ error = vm_get_desc(ctx, vcpu, VM_REG_GUEST_CS,
+ &desc_base, &desc_limit, &desc_access);
+ if (error == 0) {
+ printf("cs desc[%d]\t0x%016lx/0x%08x/0x%08x\n",
+ vcpu, desc_base, desc_limit, desc_access);
+ }
+ }
+
+ if (!error && (get_desc_tr || get_all)) {
+ error = vm_get_desc(ctx, vcpu, VM_REG_GUEST_TR,
+ &desc_base, &desc_limit, &desc_access);
+ if (error == 0) {
+ printf("tr desc[%d]\t0x%016lx/0x%08x/0x%08x\n",
+ vcpu, desc_base, desc_limit, desc_access);
+ }
+ }
+
+ if (!error && (get_desc_ldtr || get_all)) {
+ error = vm_get_desc(ctx, vcpu, VM_REG_GUEST_LDTR,
+ &desc_base, &desc_limit, &desc_access);
+ if (error == 0) {
+ printf("ldtr desc[%d]\t0x%016lx/0x%08x/0x%08x\n",
+ vcpu, desc_base, desc_limit, desc_access);
+ }
+ }
+
+ if (!error && (get_desc_gdtr || get_all)) {
+ error = vm_get_desc(ctx, vcpu, VM_REG_GUEST_GDTR,
+ &desc_base, &desc_limit, &desc_access);
+ if (error == 0) {
+ printf("gdtr[%d]\t\t0x%016lx/0x%08x\n",
+ vcpu, desc_base, desc_limit);
+ }
+ }
+
+ if (!error && (get_desc_idtr || get_all)) {
+ error = vm_get_desc(ctx, vcpu, VM_REG_GUEST_IDTR,
+ &desc_base, &desc_limit, &desc_access);
+ if (error == 0) {
+ printf("idtr[%d]\t\t0x%016lx/0x%08x\n",
+ vcpu, desc_base, desc_limit);
+ }
+ }
+
+ if (!error && (get_cs || get_all)) {
+ error = vm_get_register(ctx, vcpu, VM_REG_GUEST_CS, &cs);
+ if (error == 0)
+ printf("cs[%d]\t\t0x%04lx\n", vcpu, cs);
+ }
+
+ if (!error && (get_ds || get_all)) {
+ error = vm_get_register(ctx, vcpu, VM_REG_GUEST_DS, &ds);
+ if (error == 0)
+ printf("ds[%d]\t\t0x%04lx\n", vcpu, ds);
+ }
+
+ if (!error && (get_es || get_all)) {
+ error = vm_get_register(ctx, vcpu, VM_REG_GUEST_ES, &es);
+ if (error == 0)
+ printf("es[%d]\t\t0x%04lx\n", vcpu, es);
+ }
+
+ if (!error && (get_fs || get_all)) {
+ error = vm_get_register(ctx, vcpu, VM_REG_GUEST_FS, &fs);
+ if (error == 0)
+ printf("fs[%d]\t\t0x%04lx\n", vcpu, fs);
+ }
+
+ if (!error && (get_gs || get_all)) {
+ error = vm_get_register(ctx, vcpu, VM_REG_GUEST_GS, &gs);
+ if (error == 0)
+ printf("gs[%d]\t\t0x%04lx\n", vcpu, gs);
+ }
+
+ if (!error && (get_ss || get_all)) {
+ error = vm_get_register(ctx, vcpu, VM_REG_GUEST_SS, &ss);
+ if (error == 0)
+ printf("ss[%d]\t\t0x%04lx\n", vcpu, ss);
+ }
+
+ if (!error && (get_tr || get_all)) {
+ error = vm_get_register(ctx, vcpu, VM_REG_GUEST_TR, &tr);
+ if (error == 0)
+ printf("tr[%d]\t\t0x%04lx\n", vcpu, tr);
+ }
+
+ if (!error && (get_ldtr || get_all)) {
+ error = vm_get_register(ctx, vcpu, VM_REG_GUEST_LDTR, &ldtr);
+ if (error == 0)
+ printf("ldtr[%d]\t\t0x%04lx\n", vcpu, ldtr);
+ }
+
+ if (!error && (get_pinning || get_all)) {
+ error = vm_get_pinning(ctx, vcpu, &pincpu);
+ if (error == 0) {
+ if (pincpu < 0)
+ printf("pincpu[%d]\tunpinned\n", vcpu);
+ else
+ printf("pincpu[%d]\t%d\n", vcpu, pincpu);
+ }
+ }
+
+ if (!error && (get_x2apic_state || get_all)) {
+ error = vm_get_x2apic_state(ctx, vcpu, &x2apic_state);
+ if (error == 0)
+ printf("x2apic_state[%d]\t%d\n", vcpu, x2apic_state);
+ }
+
+ if (!error && (get_pinbased_ctls || get_all)) {
+ error = vm_get_vmcs_field(ctx, vcpu, VMCS_PIN_BASED_CTLS, &ctl);
+ if (error == 0)
+ printf("pinbased_ctls[%d]\t0x%08lx\n", vcpu, ctl);
+ }
+
+ if (!error && (get_procbased_ctls || get_all)) {
+ error = vm_get_vmcs_field(ctx, vcpu,
+ VMCS_PRI_PROC_BASED_CTLS, &ctl);
+ if (error == 0)
+ printf("procbased_ctls[%d]\t0x%08lx\n", vcpu, ctl);
+ }
+
+ if (!error && (get_procbased_ctls2 || get_all)) {
+ error = vm_get_vmcs_field(ctx, vcpu,
+ VMCS_SEC_PROC_BASED_CTLS, &ctl);
+ if (error == 0)
+ printf("procbased_ctls2[%d]\t0x%08lx\n", vcpu, ctl);
+ }
+
+ if (!error && (get_vmcs_gla || get_all)) {
+ error = vm_get_vmcs_field(ctx, vcpu,
+ VMCS_GUEST_LINEAR_ADDRESS, &u64);
+ if (error == 0)
+ printf("gla[%d]\t\t0x%016lx\n", vcpu, u64);
+ }
+
+ if (!error && (get_vmcs_gpa || get_all)) {
+ error = vm_get_vmcs_field(ctx, vcpu,
+ VMCS_GUEST_PHYSICAL_ADDRESS, &u64);
+ if (error == 0)
+ printf("gpa[%d]\t\t0x%016lx\n", vcpu, u64);
+ }
+
+ if (!error && (get_vmcs_entry_interruption_info || get_all)) {
+ error = vm_get_vmcs_field(ctx, vcpu, VMCS_ENTRY_INTR_INFO,&u64);
+ if (error == 0) {
+ printf("entry_interruption_info[%d]\t0x%08lx\n",
+ vcpu, u64);
+ }
+ }
+
+ if (!error && (get_eptp || get_all)) {
+ error = vm_get_vmcs_field(ctx, vcpu, VMCS_EPTP, &eptp);
+ if (error == 0)
+ printf("eptp[%d]\t\t0x%016lx\n", vcpu, eptp);
+ }
+
+ if (!error && (get_exception_bitmap || get_all)) {
+ error = vm_get_vmcs_field(ctx, vcpu, VMCS_EXCEPTION_BITMAP,
+ &bm);
+ if (error == 0)
+ printf("exception_bitmap[%d]\t0x%08lx\n", vcpu, bm);
+ }
+
+ if (!error && (get_io_bitmap || get_all)) {
+ error = vm_get_vmcs_field(ctx, vcpu, VMCS_IO_BITMAP_A, &bm);
+ if (error == 0)
+ printf("io_bitmap_a[%d]\t0x%08lx\n", vcpu, bm);
+ error = vm_get_vmcs_field(ctx, vcpu, VMCS_IO_BITMAP_B, &bm);
+ if (error == 0)
+ printf("io_bitmap_b[%d]\t0x%08lx\n", vcpu, bm);
+ }
+
+ if (!error && (get_tsc_offset || get_all)) {
+ uint64_t tscoff;
+ error = vm_get_vmcs_field(ctx, vcpu, VMCS_TSC_OFFSET, &tscoff);
+ if (error == 0)
+ printf("tsc_offset[%d]\t0x%016lx\n", vcpu, tscoff);
+ }
+
+ if (!error && (get_cr0_mask || get_all)) {
+ uint64_t cr0mask;
+ error = vm_get_vmcs_field(ctx, vcpu, VMCS_CR0_MASK, &cr0mask);
+ if (error == 0)
+ printf("cr0_mask[%d]\t\t0x%016lx\n", vcpu, cr0mask);
+ }
+
+ if (!error && (get_cr0_shadow || get_all)) {
+ uint64_t cr0shadow;
+ error = vm_get_vmcs_field(ctx, vcpu, VMCS_CR0_SHADOW,
+ &cr0shadow);
+ if (error == 0)
+ printf("cr0_shadow[%d]\t\t0x%016lx\n", vcpu, cr0shadow);
+ }
+
+ if (!error && (get_cr4_mask || get_all)) {
+ uint64_t cr4mask;
+ error = vm_get_vmcs_field(ctx, vcpu, VMCS_CR4_MASK, &cr4mask);
+ if (error == 0)
+ printf("cr4_mask[%d]\t\t0x%016lx\n", vcpu, cr4mask);
+ }
+
+ if (!error && (get_cr4_shadow || get_all)) {
+ uint64_t cr4shadow;
+ error = vm_get_vmcs_field(ctx, vcpu, VMCS_CR4_SHADOW,
+ &cr4shadow);
+ if (error == 0)
+ printf("cr4_shadow[%d]\t\t0x%016lx\n", vcpu, cr4shadow);
+ }
+
+ if (!error && (get_cr3_targets || get_all)) {
+ uint64_t target_count, target_addr;
+ error = vm_get_vmcs_field(ctx, vcpu, VMCS_CR3_TARGET_COUNT,
+ &target_count);
+ if (error == 0) {
+ printf("cr3_target_count[%d]\t0x%08lx\n",
+ vcpu, target_count);
+ }
+
+ error = vm_get_vmcs_field(ctx, vcpu, VMCS_CR3_TARGET0,
+ &target_addr);
+ if (error == 0) {
+ printf("cr3_target0[%d]\t\t0x%016lx\n",
+ vcpu, target_addr);
+ }
+
+ error = vm_get_vmcs_field(ctx, vcpu, VMCS_CR3_TARGET1,
+ &target_addr);
+ if (error == 0) {
+ printf("cr3_target1[%d]\t\t0x%016lx\n",
+ vcpu, target_addr);
+ }
+
+ error = vm_get_vmcs_field(ctx, vcpu, VMCS_CR3_TARGET2,
+ &target_addr);
+ if (error == 0) {
+ printf("cr3_target2[%d]\t\t0x%016lx\n",
+ vcpu, target_addr);
+ }
+
+ error = vm_get_vmcs_field(ctx, vcpu, VMCS_CR3_TARGET3,
+ &target_addr);
+ if (error == 0) {
+ printf("cr3_target3[%d]\t\t0x%016lx\n",
+ vcpu, target_addr);
+ }
+ }
+
+ if (!error && (get_apic_access_addr || get_all)) {
+ error = vm_get_vmcs_field(ctx, vcpu, VMCS_APIC_ACCESS, &addr);
+ if (error == 0)
+ printf("apic_access_addr[%d]\t0x%016lx\n", vcpu, addr);
+ }
+
+ if (!error && (get_virtual_apic_addr || get_all)) {
+ error = vm_get_vmcs_field(ctx, vcpu, VMCS_VIRTUAL_APIC, &addr);
+ if (error == 0)
+ printf("virtual_apic_addr[%d]\t0x%016lx\n", vcpu, addr);
+ }
+
+ if (!error && (get_tpr_threshold || get_all)) {
+ uint64_t threshold;
+ error = vm_get_vmcs_field(ctx, vcpu, VMCS_TPR_THRESHOLD,
+ &threshold);
+ if (error == 0)
+ printf("tpr_threshold[%d]\t0x%08lx\n", vcpu, threshold);
+ }
+
+ if (!error && (get_msr_bitmap_address || get_all)) {
+ error = vm_get_vmcs_field(ctx, vcpu, VMCS_MSR_BITMAP, &addr);
+ if (error == 0)
+ printf("msr_bitmap[%d]\t\t0x%016lx\n", vcpu, addr);
+ }
+
+ if (!error && (get_msr_bitmap || get_all)) {
+ error = vm_get_vmcs_field(ctx, vcpu, VMCS_MSR_BITMAP, &addr);
+ if (error == 0)
+ error = dump_vmcs_msr_bitmap(vcpu, addr);
+ }
+
+ if (!error && (get_vpid || get_all)) {
+ uint64_t vpid;
+ error = vm_get_vmcs_field(ctx, vcpu, VMCS_VPID, &vpid);
+ if (error == 0)
+ printf("vpid[%d]\t\t0x%04lx\n", vcpu, vpid);
+ }
+
+ if (!error && (get_ple_window || get_all)) {
+ uint64_t window;
+ error = vm_get_vmcs_field(ctx, vcpu, VMCS_PLE_WINDOW, &window);
+ if (error == 0)
+ printf("ple_window[%d]\t\t0x%08lx\n", vcpu, window);
+ }
+
+ if (!error && (get_ple_gap || get_all)) {
+ uint64_t gap;
+ error = vm_get_vmcs_field(ctx, vcpu, VMCS_PLE_GAP, &gap);
+ if (error == 0)
+ printf("ple_gap[%d]\t\t0x%08lx\n", vcpu, gap);
+ }
+
+ if (!error && (get_inst_err || get_all)) {
+ uint64_t insterr;
+ error = vm_get_vmcs_field(ctx, vcpu, VMCS_INSTRUCTION_ERROR,
+ &insterr);
+ if (error == 0) {
+ printf("instruction_error[%d]\t0x%08lx\n",
+ vcpu, insterr);
+ }
+ }
+
+ if (!error && (get_exit_ctls || get_all)) {
+ error = vm_get_vmcs_field(ctx, vcpu, VMCS_EXIT_CTLS, &ctl);
+ if (error == 0)
+ printf("exit_ctls[%d]\t\t0x%08lx\n", vcpu, ctl);
+ }
+
+ if (!error && (get_entry_ctls || get_all)) {
+ error = vm_get_vmcs_field(ctx, vcpu, VMCS_ENTRY_CTLS, &ctl);
+ if (error == 0)
+ printf("entry_ctls[%d]\t\t0x%08lx\n", vcpu, ctl);
+ }
+
+ if (!error && (get_host_pat || get_all)) {
+ error = vm_get_vmcs_field(ctx, vcpu, VMCS_HOST_IA32_PAT, &pat);
+ if (error == 0)
+ printf("host_pat[%d]\t\t0x%016lx\n", vcpu, pat);
+ }
+
+ if (!error && (get_guest_pat || get_all)) {
+ error = vm_get_vmcs_field(ctx, vcpu, VMCS_GUEST_IA32_PAT, &pat);
+ if (error == 0)
+ printf("guest_pat[%d]\t\t0x%016lx\n", vcpu, pat);
+ }
+
+ if (!error && (get_host_cr0 || get_all)) {
+ error = vm_get_vmcs_field(ctx, vcpu, VMCS_HOST_CR0, &cr0);
+ if (error == 0)
+ printf("host_cr0[%d]\t\t0x%016lx\n", vcpu, cr0);
+ }
+
+ if (!error && (get_host_cr3 || get_all)) {
+ error = vm_get_vmcs_field(ctx, vcpu, VMCS_HOST_CR3, &cr3);
+ if (error == 0)
+ printf("host_cr3[%d]\t\t0x%016lx\n", vcpu, cr3);
+ }
+
+ if (!error && (get_host_cr4 || get_all)) {
+ error = vm_get_vmcs_field(ctx, vcpu, VMCS_HOST_CR4, &cr4);
+ if (error == 0)
+ printf("host_cr4[%d]\t\t0x%016lx\n", vcpu, cr4);
+ }
+
+ if (!error && (get_host_rip || get_all)) {
+ error = vm_get_vmcs_field(ctx, vcpu, VMCS_HOST_RIP, &rip);
+ if (error == 0)
+ printf("host_rip[%d]\t\t0x%016lx\n", vcpu, rip);
+ }
+
+ if (!error && (get_host_rsp || get_all)) {
+ error = vm_get_vmcs_field(ctx, vcpu, VMCS_HOST_RSP, &rsp);
+ if (error == 0)
+ printf("host_rsp[%d]\t\t0x%016lx\n", vcpu, rsp);
+ }
+
+ if (!error && (get_guest_sysenter || get_all)) {
+ error = vm_get_vmcs_field(ctx, vcpu,
+ VMCS_GUEST_IA32_SYSENTER_CS, &cs);
+ if (error == 0)
+ printf("guest_sysenter_cs[%d]\t0x%08lx\n", vcpu, cs);
+
+ error = vm_get_vmcs_field(ctx, vcpu,
+ VMCS_GUEST_IA32_SYSENTER_ESP, &rsp);
+ if (error == 0)
+ printf("guest_sysenter_sp[%d]\t0x%016lx\n", vcpu, rsp);
+ error = vm_get_vmcs_field(ctx, vcpu,
+ VMCS_GUEST_IA32_SYSENTER_EIP, &rip);
+ if (error == 0)
+ printf("guest_sysenter_ip[%d]\t0x%016lx\n", vcpu, rip);
+ }
+
+ if (!error && (get_vmcs_link || get_all)) {
+ error = vm_get_vmcs_field(ctx, vcpu, VMCS_LINK_POINTER, &addr);
+ if (error == 0)
+ printf("vmcs_pointer[%d]\t0x%016lx\n", vcpu, addr);
+ }
+
+ if (!error && (get_vmcs_exit_reason || get_all)) {
+ error = vm_get_vmcs_field(ctx, vcpu, VMCS_EXIT_REASON, &u64);
+ if (error == 0)
+ printf("vmcs_exit_reason[%d]\t0x%016lx\n", vcpu, u64);
+ }
+
+ if (!error && (get_vmcs_exit_qualification || get_all)) {
+ error = vm_get_vmcs_field(ctx, vcpu, VMCS_EXIT_QUALIFICATION,
+ &u64);
+ if (error == 0)
+ printf("vmcs_exit_qualification[%d]\t0x%016lx\n",
+ vcpu, u64);
+ }
+
+ if (!error && (get_vmcs_exit_interruption_info || get_all)) {
+ error = vm_get_vmcs_field(ctx, vcpu,
+ VMCS_EXIT_INTERRUPTION_INFO, &u64);
+ if (error == 0) {
+ printf("vmcs_exit_interruption_info[%d]\t0x%08lx\n",
+ vcpu, u64);
+ }
+ }
+
+ if (!error && (get_vmcs_exit_interruption_error || get_all)) {
+ error = vm_get_vmcs_field(ctx, vcpu,
+ VMCS_EXIT_INTERRUPTION_ERROR, &u64);
+ if (error == 0) {
+ printf("vmcs_exit_interruption_error[%d]\t0x%08lx\n",
+ vcpu, u64);
+ }
+ }
+
+ if (!error && (get_vmcs_interruptibility || get_all)) {
+ error = vm_get_vmcs_field(ctx, vcpu,
+ VMCS_GUEST_INTERRUPTIBILITY, &u64);
+ if (error == 0) {
+ printf("vmcs_guest_interruptibility[%d]\t0x%08lx\n",
+ vcpu, u64);
+ }
+ }
+
+ if (!error && setcap) {
+ int captype;
+ captype = vm_capability_name2type(capname);
+ error = vm_set_capability(ctx, vcpu, captype, capval);
+ if (error != 0 && errno == ENOENT)
+ printf("Capability \"%s\" is not available\n", capname);
+ }
+
+ if (!error && (getcap || get_all)) {
+ int captype, val, getcaptype;
+
+ if (getcap && capname)
+ getcaptype = vm_capability_name2type(capname);
+ else
+ getcaptype = -1;
+
+ for (captype = 0; captype < VM_CAP_MAX; captype++) {
+ if (getcaptype >= 0 && captype != getcaptype)
+ continue;
+ error = vm_get_capability(ctx, vcpu, captype, &val);
+ if (error == 0) {
+ printf("Capability \"%s\" is %s on vcpu %d\n",
+ vm_capability_type2name(captype),
+ val ? "set" : "not set", vcpu);
+ } else if (errno == ENOENT) {
+ printf("Capability \"%s\" is not available\n",
+ vm_capability_type2name(captype));
+ } else {
+ break;
+ }
+ }
+ }
+
+ if (!error && run) {
+ error = vm_get_register(ctx, vcpu, VM_REG_GUEST_RIP, &rip);
+ assert(error == 0);
+
+ error = vm_run(ctx, vcpu, rip, &vmexit);
+ if (error == 0)
+ dump_vm_run_exitcode(&vmexit, vcpu);
+ else
+ printf("vm_run error %d\n", error);
+ }
+
+ if (error)
+ printf("errno = %d\n", errno);
+
+ if (!error && destroy)
+ vm_destroy(ctx);
+
+ exit(error);
+}
diff --git a/usr.sbin/bhyveload/Makefile b/usr.sbin/bhyveload/Makefile
new file mode 100644
index 0000000..7b00818
--- /dev/null
+++ b/usr.sbin/bhyveload/Makefile
@@ -0,0 +1,14 @@
+# $FreeBSD$
+
+PROG= bhyveload
+SRCS= bhyveload.c
+MAN= bhyveload.8
+
+DPADD+= ${LIBVMMAPI}
+LDADD+= -lvmmapi
+
+WARNS?= 3
+
+CFLAGS+=-I${.CURDIR}/../../sys/boot/userboot
+
+.include <bsd.prog.mk>
diff --git a/usr.sbin/bhyveload/bhyveload.8 b/usr.sbin/bhyveload/bhyveload.8
new file mode 100644
index 0000000..4a05116
--- /dev/null
+++ b/usr.sbin/bhyveload/bhyveload.8
@@ -0,0 +1,127 @@
+.\"
+.\" Copyright (c) 2012 NetApp Inc
+.\" All rights reserved.
+.\"
+.\" Redistribution and use in source and binary forms, with or without
+.\" modification, are permitted provided that the following conditions
+.\" are met:
+.\" 1. Redistributions of source code must retain the above copyright
+.\" notice, this list of conditions and the following disclaimer.
+.\" 2. Redistributions in binary form must reproduce the above copyright
+.\" notice, this list of conditions and the following disclaimer in the
+.\" documentation and/or other materials provided with the distribution.
+.\"
+.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+.\" SUCH DAMAGE.
+.\"
+.\" $FreeBSD$
+.\"
+.Dd January 7, 2012
+.Dt BHYVELOAD 8
+.Os
+.Sh NAME
+.Nm bhyveload
+.Nd load a
+.Fx
+guest inside a bhyve virtual machine
+.Sh SYNOPSIS
+.Nm
+.Op Fl m Ar lowmem
+.Op Fl M Ar highmem
+.Op Fl d Ar disk-path
+.Op Fl h Ar host-path
+.Ar vmname
+.Sh DESCRIPTION
+.Nm
+is used to load a
+.Fx
+guest inside a
+.Xr bhyve 4
+virtual machine.
+.Pp
+.Nm
+is based on
+.Xr loader 8
+and will present an interface identical to the
+.Fx
+loader on the user's terminal.
+.Pp
+The virtual machine is identified as
+.Ar vmname
+and will be created if it does not already exist.
+.Sh OPTIONS
+The following options are available:
+.Bl -tag -width indent
+.It Fl m Ar lowmem
+.Ar lowmem
+is the amount of memory allocated below 4GB in the guest's physical address
+space.
+.Pp
+The default value of
+.Ar lowmem
+is 256MB.
+.It Fl M Ar highmem
+.Ar highmem
+is the amount of memory allocated above 4GB in the guest's physical address
+space.
+.Pp
+The default value of
+.Ar highmem
+is 0MB.
+.It Fl d Ar disk-path
+The
+.Ar disk-path
+is the pathname of the guest's boot disk image.
+.It Fl h Ar host-path
+The
+.Ar host-path
+is the directory at the top of the guest's boot filesystem.
+.El
+.Sh EXAMPLES
+To create a virtual machine named
+.Ar freebsd-vm
+that boots off the ISO image
+.Pa /freebsd/release.iso
+and has 1GB memory allocated to it:
+.Pp
+.Dl "bhyveload -m 256 -M 768 -d /freebsd/release.iso freebsd-vm"
+.Pp
+In the example above the 1GB allocation is split in two segments:
+.Pp
+.Bl -dash -compact
+.It
+256MB below the 4GB boundary (0MB - 256MB)
+.It
+768MB above the 4GB boundary (4096MB - 4864MB)
+.El
+.Sh SEE ALSO
+.Xr bhyve 4 ,
+.Xr bhyve 8 ,
+.Xr loader 8 ,
+.Xr vmm 4
+.Sh HISTORY
+.Nm
+first appeared in
+.Fx 10.0 ,
+and was developed at NetApp Inc.
+.Sh AUTHORS
+.Nm
+was developed by
+.An -nosplit
+.An "Neel Natu" Aq neel@FreeBSD.org
+at NetApp Inc with a lot of help from
+.An Doug Rabson Aq dfr@FreeBSD.org
+.Sh BUGS
+.Nm
+can only load
+.Fx
+as a guest.
diff --git a/usr.sbin/bhyveload/bhyveload.c b/usr.sbin/bhyveload/bhyveload.c
new file mode 100644
index 0000000..ef12d9f
--- /dev/null
+++ b/usr.sbin/bhyveload/bhyveload.c
@@ -0,0 +1,652 @@
+/*-
+ * Copyright (c) 2011 NetApp, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ */
+
+/*-
+ * Copyright (c) 2011 Google, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <sys/ioctl.h>
+#include <sys/stat.h>
+#include <sys/disk.h>
+
+#include <machine/specialreg.h>
+#include <machine/vmm.h>
+
+#include <dirent.h>
+#include <dlfcn.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <getopt.h>
+#include <limits.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <termios.h>
+#include <unistd.h>
+
+#include <vmmapi.h>
+
+#include "userboot.h"
+
+#define MB (1024 * 1024UL)
+#define GB (1024 * 1024 * 1024UL)
+#define BSP 0
+
+static char *host_base = "/";
+static struct termios term, oldterm;
+static int disk_fd = -1;
+
+static char *vmname, *progname, *membase;
+static uint64_t lowmem, highmem;
+static struct vmctx *ctx;
+
+static uint64_t gdtbase, cr3, rsp;
+
+static void cb_exit(void *arg, int v);
+
+/*
+ * Console i/o callbacks
+ */
+
+static void
+cb_putc(void *arg, int ch)
+{
+ char c = ch;
+
+ write(1, &c, 1);
+}
+
+static int
+cb_getc(void *arg)
+{
+ char c;
+
+ if (read(0, &c, 1) == 1)
+ return (c);
+ return (-1);
+}
+
+static int
+cb_poll(void *arg)
+{
+ int n;
+
+ if (ioctl(0, FIONREAD, &n) >= 0)
+ return (n > 0);
+ return (0);
+}
+
+/*
+ * Host filesystem i/o callbacks
+ */
+
+struct cb_file {
+ int cf_isdir;
+ size_t cf_size;
+ struct stat cf_stat;
+ union {
+ int fd;
+ DIR *dir;
+ } cf_u;
+};
+
+static int
+cb_open(void *arg, const char *filename, void **hp)
+{
+ struct stat st;
+ struct cb_file *cf;
+ char path[PATH_MAX];
+
+ if (!host_base)
+ return (ENOENT);
+
+ strlcpy(path, host_base, PATH_MAX);
+ if (path[strlen(path) - 1] == '/')
+ path[strlen(path) - 1] = 0;
+ strlcat(path, filename, PATH_MAX);
+ cf = malloc(sizeof(struct cb_file));
+ if (stat(path, &cf->cf_stat) < 0) {
+ free(cf);
+ return (errno);
+ }
+
+ cf->cf_size = st.st_size;
+ if (S_ISDIR(cf->cf_stat.st_mode)) {
+ cf->cf_isdir = 1;
+ cf->cf_u.dir = opendir(path);
+ if (!cf->cf_u.dir)
+ goto out;
+ *hp = cf;
+ return (0);
+ }
+ if (S_ISREG(cf->cf_stat.st_mode)) {
+ cf->cf_isdir = 0;
+ cf->cf_u.fd = open(path, O_RDONLY);
+ if (cf->cf_u.fd < 0)
+ goto out;
+ *hp = cf;
+ return (0);
+ }
+
+out:
+ free(cf);
+ return (EINVAL);
+}
+
+static int
+cb_close(void *arg, void *h)
+{
+ struct cb_file *cf = h;
+
+ if (cf->cf_isdir)
+ closedir(cf->cf_u.dir);
+ else
+ close(cf->cf_u.fd);
+ free(cf);
+
+ return (0);
+}
+
+static int
+cb_isdir(void *arg, void *h)
+{
+ struct cb_file *cf = h;
+
+ return (cf->cf_isdir);
+}
+
+static int
+cb_read(void *arg, void *h, void *buf, size_t size, size_t *resid)
+{
+ struct cb_file *cf = h;
+ ssize_t sz;
+
+ if (cf->cf_isdir)
+ return (EINVAL);
+ sz = read(cf->cf_u.fd, buf, size);
+ if (sz < 0)
+ return (EINVAL);
+ *resid = size - sz;
+ return (0);
+}
+
+static int
+cb_readdir(void *arg, void *h, uint32_t *fileno_return, uint8_t *type_return,
+ size_t *namelen_return, char *name)
+{
+ struct cb_file *cf = h;
+ struct dirent *dp;
+
+ if (!cf->cf_isdir)
+ return (EINVAL);
+
+ dp = readdir(cf->cf_u.dir);
+ if (!dp)
+ return (ENOENT);
+
+ /*
+ * Note: d_namlen is in the range 0..255 and therefore less
+ * than PATH_MAX so we don't need to test before copying.
+ */
+ *fileno_return = dp->d_fileno;
+ *type_return = dp->d_type;
+ *namelen_return = dp->d_namlen;
+ memcpy(name, dp->d_name, dp->d_namlen);
+ name[dp->d_namlen] = 0;
+
+ return (0);
+}
+
+static int
+cb_seek(void *arg, void *h, uint64_t offset, int whence)
+{
+ struct cb_file *cf = h;
+
+ if (cf->cf_isdir)
+ return (EINVAL);
+ if (lseek(cf->cf_u.fd, offset, whence) < 0)
+ return (errno);
+ return (0);
+}
+
+static int
+cb_stat(void *arg, void *h, int *mode, int *uid, int *gid, uint64_t *size)
+{
+ struct cb_file *cf = h;
+
+ *mode = cf->cf_stat.st_mode;
+ *uid = cf->cf_stat.st_uid;
+ *gid = cf->cf_stat.st_gid;
+ *size = cf->cf_stat.st_size;
+ return (0);
+}
+
+/*
+ * Disk image i/o callbacks
+ */
+
+static int
+cb_diskread(void *arg, int unit, uint64_t from, void *to, size_t size,
+ size_t *resid)
+{
+ ssize_t n;
+
+ if (unit != 0 || disk_fd == -1)
+ return (EIO);
+ n = pread(disk_fd, to, size, from);
+ if (n < 0)
+ return (errno);
+ *resid = size - n;
+ return (0);
+}
+
+static int
+cb_diskioctl(void *arg, int unit, u_long cmd, void *data)
+{
+ struct stat sb;
+
+ if (unit != 0 || disk_fd == -1)
+ return (EBADF);
+
+ switch (cmd) {
+ case DIOCGSECTORSIZE:
+ *(u_int *)data = 512;
+ break;
+ case DIOCGMEDIASIZE:
+ if (fstat(disk_fd, &sb) == 0)
+ *(off_t *)data = sb.st_size;
+ else
+ return (ENOTTY);
+ break;
+ default:
+ return (ENOTTY);
+ }
+
+ return (0);
+}
+
+/*
+ * Guest virtual machine i/o callbacks
+ */
+static int
+cb_copyin(void *arg, const void *from, uint64_t to, size_t size)
+{
+
+ to &= 0x7fffffff;
+ if (to > lowmem)
+ return (EFAULT);
+ if (to + size > lowmem)
+ size = lowmem - to;
+
+ memcpy(&membase[to], from, size);
+
+ return (0);
+}
+
+static int
+cb_copyout(void *arg, uint64_t from, void *to, size_t size)
+{
+
+ from &= 0x7fffffff;
+ if (from > lowmem)
+ return (EFAULT);
+ if (from + size > lowmem)
+ size = lowmem - from;
+
+ memcpy(to, &membase[from], size);
+
+ return (0);
+}
+
+static void
+cb_setreg(void *arg, int r, uint64_t v)
+{
+ int error;
+ enum vm_reg_name vmreg;
+
+ vmreg = VM_REG_LAST;
+
+ switch (r) {
+ case 4:
+ vmreg = VM_REG_GUEST_RSP;
+ rsp = v;
+ break;
+ default:
+ break;
+ }
+
+ if (vmreg == VM_REG_LAST) {
+ printf("test_setreg(%d): not implemented\n", r);
+ cb_exit(NULL, USERBOOT_EXIT_QUIT);
+ }
+
+ error = vm_set_register(ctx, BSP, vmreg, v);
+ if (error) {
+ perror("vm_set_register");
+ cb_exit(NULL, USERBOOT_EXIT_QUIT);
+ }
+}
+
+static void
+cb_setmsr(void *arg, int r, uint64_t v)
+{
+ int error;
+ enum vm_reg_name vmreg;
+
+ vmreg = VM_REG_LAST;
+
+ switch (r) {
+ case MSR_EFER:
+ vmreg = VM_REG_GUEST_EFER;
+ break;
+ default:
+ break;
+ }
+
+ if (vmreg == VM_REG_LAST) {
+ printf("test_setmsr(%d): not implemented\n", r);
+ cb_exit(NULL, USERBOOT_EXIT_QUIT);
+ }
+
+ error = vm_set_register(ctx, BSP, vmreg, v);
+ if (error) {
+ perror("vm_set_msr");
+ cb_exit(NULL, USERBOOT_EXIT_QUIT);
+ }
+}
+
+static void
+cb_setcr(void *arg, int r, uint64_t v)
+{
+ int error;
+ enum vm_reg_name vmreg;
+
+ vmreg = VM_REG_LAST;
+
+ switch (r) {
+ case 0:
+ vmreg = VM_REG_GUEST_CR0;
+ break;
+ case 3:
+ vmreg = VM_REG_GUEST_CR3;
+ cr3 = v;
+ break;
+ case 4:
+ vmreg = VM_REG_GUEST_CR4;
+ break;
+ default:
+ break;
+ }
+
+ if (vmreg == VM_REG_LAST) {
+ printf("test_setcr(%d): not implemented\n", r);
+ cb_exit(NULL, USERBOOT_EXIT_QUIT);
+ }
+
+ error = vm_set_register(ctx, BSP, vmreg, v);
+ if (error) {
+ perror("vm_set_cr");
+ cb_exit(NULL, USERBOOT_EXIT_QUIT);
+ }
+}
+
+static void
+cb_setgdt(void *arg, uint64_t base, size_t size)
+{
+ int error;
+
+ error = vm_set_desc(ctx, BSP, VM_REG_GUEST_GDTR, base, size - 1, 0);
+ if (error != 0) {
+ perror("vm_set_desc(gdt)");
+ cb_exit(NULL, USERBOOT_EXIT_QUIT);
+ }
+
+ gdtbase = base;
+}
+
+static void
+cb_exec(void *arg, uint64_t rip)
+{
+ int error;
+
+ error = vm_setup_freebsd_registers(ctx, BSP, rip, cr3, gdtbase, rsp);
+ if (error) {
+ perror("vm_setup_freebsd_registers");
+ cb_exit(NULL, USERBOOT_EXIT_QUIT);
+ }
+
+ cb_exit(NULL, 0);
+}
+
+/*
+ * Misc
+ */
+
+static void
+cb_delay(void *arg, int usec)
+{
+
+ usleep(usec);
+}
+
+static void
+cb_exit(void *arg, int v)
+{
+
+ tcsetattr(0, TCSAFLUSH, &oldterm);
+ exit(v);
+}
+
+static void
+cb_getmem(void *arg, uint64_t *ret_lowmem, uint64_t *ret_highmem)
+{
+
+ *ret_lowmem = lowmem;
+ *ret_highmem = highmem;
+}
+
+static const char *
+cb_getenv(void *arg, int num)
+{
+ int max;
+
+ static const char * var[] = {
+ "smbios.bios.vendor=BHYVE",
+ "boot_serial=1",
+ NULL
+ };
+
+ max = sizeof(var) / sizeof(var[0]);
+
+ if (num < max)
+ return (var[num]);
+ else
+ return (NULL);
+}
+
+static struct loader_callbacks cb = {
+ .getc = cb_getc,
+ .putc = cb_putc,
+ .poll = cb_poll,
+
+ .open = cb_open,
+ .close = cb_close,
+ .isdir = cb_isdir,
+ .read = cb_read,
+ .readdir = cb_readdir,
+ .seek = cb_seek,
+ .stat = cb_stat,
+
+ .diskread = cb_diskread,
+ .diskioctl = cb_diskioctl,
+
+ .copyin = cb_copyin,
+ .copyout = cb_copyout,
+ .setreg = cb_setreg,
+ .setmsr = cb_setmsr,
+ .setcr = cb_setcr,
+ .setgdt = cb_setgdt,
+ .exec = cb_exec,
+
+ .delay = cb_delay,
+ .exit = cb_exit,
+ .getmem = cb_getmem,
+
+ .getenv = cb_getenv,
+};
+
+static void
+usage(void)
+{
+
+ printf("usage: %s [-d <disk image path>] [-h <host filesystem path>] "
+ "[-m <lowmem>][-M <highmem>] "
+ "<vmname>\n", progname);
+ exit(1);
+}
+
+int
+main(int argc, char** argv)
+{
+ void *h;
+ void (*func)(struct loader_callbacks *, void *, int, int);
+ int opt, error;
+ char *disk_image;
+
+ progname = argv[0];
+
+ lowmem = 128 * MB;
+ highmem = 0;
+ disk_image = NULL;
+
+ while ((opt = getopt(argc, argv, "d:h:m:M:")) != -1) {
+ switch (opt) {
+ case 'd':
+ disk_image = optarg;
+ break;
+
+ case 'h':
+ host_base = optarg;
+ break;
+
+ case 'm':
+ lowmem = strtoul(optarg, NULL, 0) * MB;
+ break;
+
+ case 'M':
+ highmem = strtoul(optarg, NULL, 0) * MB;
+ break;
+
+ case '?':
+ usage();
+ }
+ }
+
+ argc -= optind;
+ argv += optind;
+
+ if (argc != 1)
+ usage();
+
+ vmname = argv[0];
+
+ error = vm_create(vmname);
+ if (error != 0 && errno != EEXIST) {
+ perror("vm_create");
+ exit(1);
+
+ }
+
+ ctx = vm_open(vmname);
+ if (ctx == NULL) {
+ perror("vm_open");
+ exit(1);
+ }
+
+ error = vm_setup_memory(ctx, 0, lowmem, &membase);
+ if (error) {
+ perror("vm_setup_memory(lowmem)");
+ exit(1);
+ }
+
+ if (highmem != 0) {
+ error = vm_setup_memory(ctx, 4 * GB, highmem, NULL);
+ if (error) {
+ perror("vm_setup_memory(highmem)");
+ exit(1);
+ }
+ }
+
+ tcgetattr(0, &term);
+ oldterm = term;
+ term.c_lflag &= ~(ICANON|ECHO);
+ term.c_iflag &= ~ICRNL;
+ tcsetattr(0, TCSAFLUSH, &term);
+ h = dlopen("/boot/userboot.so", RTLD_LOCAL);
+ if (!h) {
+ printf("%s\n", dlerror());
+ return (1);
+ }
+ func = dlsym(h, "loader_main");
+ if (!func) {
+ printf("%s\n", dlerror());
+ return (1);
+ }
+
+ if (disk_image) {
+ disk_fd = open(disk_image, O_RDONLY);
+ }
+ func(&cb, NULL, USERBOOT_VERSION_3, disk_fd >= 0);
+}
diff --git a/usr.sbin/bluetooth/hcseriald/hcseriald.8 b/usr.sbin/bluetooth/hcseriald/hcseriald.8
index 111b28c..8d2dd47 100644
--- a/usr.sbin/bluetooth/hcseriald/hcseriald.8
+++ b/usr.sbin/bluetooth/hcseriald/hcseriald.8
@@ -54,7 +54,7 @@ Do not disassociate from the controlling terminal, i.e., run in foreground.
.It Fl f Ar device
Callout device name.
Example:
-.Fl f Pa /dev/cuad0 .
+.Fl f Pa /dev/cuau0 .
.It Fl h
Display usage message and exit.
.It Fl n Ar node_name
diff --git a/usr.sbin/bluetooth/hcseriald/hcseriald.c b/usr.sbin/bluetooth/hcseriald/hcseriald.c
index b811c1d..440b0d4 100644
--- a/usr.sbin/bluetooth/hcseriald/hcseriald.c
+++ b/usr.sbin/bluetooth/hcseriald/hcseriald.c
@@ -257,7 +257,7 @@ usage(void)
{
fprintf(stderr, "Usage: %s -f device -n node_name [-s speed -d -h]\n" \
"Where:\n" \
- "\t-f device tty device name, ex. /dev/cuad1\n" \
+ "\t-f device tty device name, ex. /dev/cuau1\n" \
"\t-n node_name set Netgraph node name to node_name\n" \
"\t-s speed set tty speed, ex. 115200\n" \
"\t-d run in foreground\n" \
diff --git a/usr.sbin/bsdconfig/Makefile b/usr.sbin/bsdconfig/Makefile
index 7576194..4faad0b 100644
--- a/usr.sbin/bsdconfig/Makefile
+++ b/usr.sbin/bsdconfig/Makefile
@@ -1,7 +1,5 @@
# $FreeBSD$
-NO_OBJ=
-
SUBDIR= console \
diskmgmt \
docsinstall \
diff --git a/usr.sbin/bsdconfig/USAGE b/usr.sbin/bsdconfig/USAGE
index 86355b0..eac7dbe 100644
--- a/usr.sbin/bsdconfig/USAGE
+++ b/usr.sbin/bsdconfig/USAGE
@@ -1,5 +1,5 @@
# Copyright (c) 2012 Ron McDowell
-# Copyright (c) 2012 Devin Teske
+# Copyright (c) 2012-2013 Devin Teske
# All Rights Reserved.
#
# Redistribution and use in source and binary forms, with or without
@@ -31,6 +31,9 @@ Usage:
@PROGRAM_NAME@ [OPTIONS] [command [OPTIONS]]
OPTIONS:
+ -f file Load file as script and then exit. If multiple occurrences,
+ program will only exit after last occurrence. If file is a
+ single dash (`-'), @PROGRAM_NAME@ reads from standard input.
-h Print usage statement and exit.
-S Secure X11 mode (implies `-X'). As root, always prompt-for
and validate sudo(8) username/password before starting.
diff --git a/usr.sbin/bsdconfig/bsdconfig b/usr.sbin/bsdconfig/bsdconfig
index f80e5a9..07b73fe 100755
--- a/usr.sbin/bsdconfig/bsdconfig
+++ b/usr.sbin/bsdconfig/bsdconfig
@@ -1,7 +1,7 @@
#!/bin/sh
#-
# Copyright (c) 2012 Ron McDowell
-# Copyright (c) 2012 Devin Teske
+# Copyright (c) 2012-2013 Devin Teske
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
@@ -31,6 +31,7 @@
BSDCFG_SHARE="/usr/share/bsdconfig"
. $BSDCFG_SHARE/common.subr || exit 1
+f_dprintf "%s: loading includes..." "$0"
f_include $BSDCFG_SHARE/dialog.subr
f_include $BSDCFG_SHARE/mustberoot.subr
f_include $BSDCFG_SHARE/strings.subr
@@ -70,7 +71,7 @@ usage()
#
local longest_cmd
longest_cmd=$( echo "$cmd_list" | f_longest_line_length )
- f_dprintf "longest_cmd=[$longest_cmd]"
+ f_dprintf "longest_cmd=[%s]" "$longest_cmd"
#
# Determine the maximum width of terminal/console
@@ -79,7 +80,7 @@ usage()
max_size=$( stty size 2> /dev/null )
: ${max_size:="24 80"}
max_width="${max_size#*[$IFS]}"
- f_dprintf "max_width=[$max_width]"
+ f_dprintf "max_width=[%s]" "$max_width"
#
# Using the longest command-length as the width of a single column,
@@ -92,7 +93,7 @@ usage()
ncols=$(( $ncols + 1 ))
x=$(( $x + 3 + $longest_cmd ))
done
- f_dprintf "ncols=[$ncols] x=[$x]"
+ f_dprintf "ncols=[%u] x=[%u]" $ncols $x
#
# Re-format the command-list into multiple columns
@@ -117,7 +118,8 @@ usage()
{
n++
cur_col = (( n - 1 ) % ncols ) + 1
- printf "f_dprintf \"row_item[%u]=[%s]\"\n", cur_col, $0
+ printf "f_dprintf \"row_item[%u]=[%%s]\" \"%s\"\n",
+ cur_col, $0
row_item[cur_col] = $0
if ( cur_col == ncols ) print_row()
}
@@ -190,6 +192,7 @@ dialog_menu_main()
--ok-label \"\$msg_ok\" \
--cancel-label \"\$msg_exit_bsdconfig\" \
--help-button \
+ --help-label \"\$msg_help\" \
${USE_XDIALOG:+--help \"\"} \
--menu \"\$prompt\" $size $menu_list \
2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
@@ -202,19 +205,48 @@ dialog_menu_main()
############################################################ MAIN
#
+# If $0 is not "bsdconfig", interpret it either as a keyword to a menuitem or
+# as a valid resword (see script.subr for additional details about reswords).
+#
+if [ "$pgm" != "bsdconfig" ]; then
+ if indexfile=$( f_index_file "$pgm" ) &&
+ cmd=$( f_index_menusel_command "$indexfile" "$pgm" )
+ then
+ f_dprintf "pgm=[%s] indexfile=[%s] cmd=[%s]" \
+ "$pgm" "$indexfile" "$cmd"
+ exec "$cmd" "$@" || exit 1
+ else
+ f_include $BSDCFG_SHARE/script.subr
+ for resword in $RESWORDS; do
+ [ "$pgm" = "$resword" ] || continue
+ # Found a match
+ f_dprintf "pgm=[%s] A valid resWord!" "$pgm"
+ f_dispatch $resword
+ exit $?
+ done
+ fi
+fi
+
+#
# Process command-line arguments
#
-while getopts hSX flag; do
+scripts_loaded=0
+while getopts f:hSX flag; do
case "$flag" in
+ f) [ $scripts_loaded -eq 0 ] && f_include $BSDCFG_SHARE/script.subr
+ f_script_load "$OPTARG"
+ scripts_loaded=$(( $scripts_loaded + 1 ));;
h|\?) usage;;
esac
done
shift $(( $OPTIND -1 ))
+# If we've loaded any scripts, do not continue any further
+[ $scripts_loaded -gt 0 ] && exit
+
#
# Initialize
#
-f_dialog_init
f_dialog_title "$msg_main_menu"
[ "$SECURE" ] && f_mustberoot_init
@@ -222,14 +254,6 @@ f_dialog_title "$msg_main_menu"
# Incorporate rc-file if it exists
[ -f "$HOME/.bsdconfigrc" ] && f_include "$HOME/.bsdconfigrc"
-#
-# Log our operating environment for debugging purposes
-#
-export UNAME_S="$(uname -s)" # Operating System (i.e. FreeBSD)
-export UNAME_P="$(uname -p)" # Processor Architecture (i.e. i386)
-export UNAME_R="$(uname -r)" # Release Level (i.e. X.Y-RELEASE)
-f_dprintf "UNAME_S=[$UNAME_S] UNAME_P=[$UNAME_P] UNAME_R=[$UNAME_R]"
-
cd $BSDCFG_LIBE || f_die 1 "$msg_directory_not_found" "$BSDCFG_LIBE"
#
@@ -244,51 +268,18 @@ if [ "$1" ]; then
esac
#
- # Find the INDEX (possibly i18n) claiming this keyword
- #
- lang="${LANG:-$LC_ALL}"
- if [ "$lang" ]; then
- sel=$( grep "^menu_selection=\"$1|" */INDEX.$lang \
- 2> /dev/null | tail -1 )
-
- # Fall-back to non-i18n sources if nothing was found
- [ "$sel" ] ||
- sel=$( grep "^menu_selection=\"$1|" */INDEX | tail -1 )
- else
- sel=$( grep "^menu_selection=\"$1|" */INDEX | tail -1 )
- fi
-
- #
- # If no matches, display usage (which shows valid keywords)
+ # Find the INDEX (possibly i18n) claiming this keyword and get the
+ # command to execute from the menu_selection line.
#
- if [ ! "$sel" ]; then
+ if ! { indexfile=$( f_index_file "$1" ) &&
+ cmd=$( f_index_menusel_command "$indexfile" "$1" )
+ }; then
+ # no matches, display usage (which shows valid keywords)
f_err "%s: %s: $msg_not_found\n" "$pgm" "$1"
usage
fi
- #
- # The command to execute is after the pipe-character (|) in the
- # menu_selection property of the INDEX file for the menuitem.
- #
- cmd="${sel#*|}"
- cmd="${cmd%\"}"
- if [ ! "$cmd" ]; then
- echo "$pgm: $1: $msg_not_found"
- usage
- fi
shift
-
- #
- # If the command pathname is not fully qualified fix-up/force to be
- # relative to the menuitem directory.
- #
- case "$cmd" in
- /*) : already fully qualified ;;
- *)
- dir="${sel%%/*}"
- cmd="$dir/$cmd"
- esac
-
exec $cmd ${USE_XDIALOG:+-X} "$@" || exit 1
fi
@@ -299,7 +290,7 @@ while :; do
dialog_menu_main
retval=$?
mtag=$( f_dialog_menutag )
- f_dprintf "retval=$retval mtag=[$mtag]"
+ f_dprintf "retval=%u mtag=[%s]" $retval "$mtag"
if [ $retval -eq 2 ]; then
# The Help button was pressed
@@ -321,7 +312,7 @@ while :; do
*) # Dynamically loaded menuitem
cmd=$( eval echo \"\$menu_program$mtag\" )
- f_dprintf "cmd=[$cmd]"
+ f_dprintf "cmd=[%s]" "$cmd"
$cmd ${USE_XDIALOG:+-X}
;;
diff --git a/usr.sbin/bsdconfig/bsdconfig.8 b/usr.sbin/bsdconfig/bsdconfig.8
index 5bb3e73..1c1150b 100644
--- a/usr.sbin/bsdconfig/bsdconfig.8
+++ b/usr.sbin/bsdconfig/bsdconfig.8
@@ -1,5 +1,5 @@
.\" Copyright (c) 2012 Ron McDowell
-.\" Copyright (c) 2012 Devin Teske
+.\" Copyright (c) 2012-2013 Devin Teske
.\" All rights reserved.
.\"
.\" Redistribution and use in source and binary forms, with or without
@@ -59,7 +59,7 @@
.\"
.\" $FreeBSD$
.\"
-.Dd Mar 20, 2012
+.Dd Jan 5, 2013
.Dt BSDCONFIG 8
.Os
.Sh NAME
@@ -85,6 +85,17 @@ a master menu listing the available commands.
.Pp
The following options are available:
.Bl -tag -width indent+
+.It Fl f Ar file
+Load
+.Ar file
+as script and then exit.
+If multiple occurrences, program will only exit after last occurrence.
+If
+.Ar file
+is a single dash
+.Pq Sq Fl ,
+.Nm
+reads from standard input.
.It Fl h
Print usage statement and exit.
.It Fl S
diff --git a/usr.sbin/bsdconfig/console/console b/usr.sbin/bsdconfig/console/console
index 73d9f5b..5f3b7af 100755
--- a/usr.sbin/bsdconfig/console/console
+++ b/usr.sbin/bsdconfig/console/console
@@ -30,13 +30,14 @@
BSDCFG_SHARE="/usr/share/bsdconfig"
. $BSDCFG_SHARE/common.subr || exit 1
+f_dprintf "%s: loading includes..." "$0"
f_include $BSDCFG_SHARE/dialog.subr
f_include $BSDCFG_SHARE/mustberoot.subr
BSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="080.console"
f_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
-ipgm=$( f_index_menu_selection $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
+ipgm=$( f_index_menusel_keyword $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
[ $? -eq $SUCCESS -a "$ipgm" ] && pgm="$ipgm"
############################################################ FUNCTIONS
@@ -102,7 +103,6 @@ shift $(( $OPTIND - 1 ))
#
# Initialize
#
-f_dialog_init
f_dialog_title "$msg_system_console_configuration"
f_dialog_backtitle "${ipgm:+bsdconfig }$pgm"
f_mustberoot_init
diff --git a/usr.sbin/bsdconfig/console/font b/usr.sbin/bsdconfig/console/font
index 2fe4611..022952f 100755
--- a/usr.sbin/bsdconfig/console/font
+++ b/usr.sbin/bsdconfig/console/font
@@ -30,6 +30,7 @@
BSDCFG_SHARE="/usr/share/bsdconfig"
. $BSDCFG_SHARE/common.subr || exit 1
+f_dprintf "%s: loading includes..." "$0"
f_include $BSDCFG_SHARE/dialog.subr
f_include $BSDCFG_SHARE/mustberoot.subr
f_include $BSDCFG_SHARE/sysrc.subr
@@ -37,7 +38,7 @@ f_include $BSDCFG_SHARE/sysrc.subr
BSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="080.console"
f_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
-ipgm=$( f_index_menu_selection $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
+ipgm=$( f_index_menusel_keyword $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
[ $? -eq $SUCCESS -a "$ipgm" ] && pgm="$ipgm"
############################################################ FUNCTIONS
@@ -110,7 +111,6 @@ shift $(( $OPTIND - 1 ))
#
# Initialize
#
-f_dialog_init
f_dialog_title "$msg_system_console_font"
f_dialog_backtitle "${ipgm:+bsdconfig }$pgm"
f_mustberoot_init
diff --git a/usr.sbin/bsdconfig/console/keymap b/usr.sbin/bsdconfig/console/keymap
index ea1de2d..5518140 100755
--- a/usr.sbin/bsdconfig/console/keymap
+++ b/usr.sbin/bsdconfig/console/keymap
@@ -30,6 +30,7 @@
BSDCFG_SHARE="/usr/share/bsdconfig"
. $BSDCFG_SHARE/common.subr || exit 1
+f_dprintf "%s: loading includes..." "$0"
f_include $BSDCFG_SHARE/dialog.subr
f_include $BSDCFG_SHARE/mustberoot.subr
f_include $BSDCFG_SHARE/sysrc.subr
@@ -37,7 +38,7 @@ f_include $BSDCFG_SHARE/sysrc.subr
BSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="080.console"
f_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
-ipgm=$( f_index_menu_selection $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
+ipgm=$( f_index_menusel_keyword $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
[ $? -eq $SUCCESS -a "$ipgm" ] && pgm="$ipgm"
############################################################ CONFIGURATION
@@ -150,7 +151,6 @@ shift $(( $OPTIND - 1 ))
#
# Initialize
#
-f_dialog_init
f_dialog_title "$msg_system_console_keymap"
f_dialog_backtitle "${ipgm:+bsdconfig }$pgm"
f_mustberoot_init
diff --git a/usr.sbin/bsdconfig/console/repeat b/usr.sbin/bsdconfig/console/repeat
index 76d60d1..93357e8 100755
--- a/usr.sbin/bsdconfig/console/repeat
+++ b/usr.sbin/bsdconfig/console/repeat
@@ -30,6 +30,7 @@
BSDCFG_SHARE="/usr/share/bsdconfig"
. $BSDCFG_SHARE/common.subr || exit 1
+f_dprintf "%s: loading includes..." "$0"
f_include $BSDCFG_SHARE/dialog.subr
f_include $BSDCFG_SHARE/mustberoot.subr
f_include $BSDCFG_SHARE/sysrc.subr
@@ -37,7 +38,7 @@ f_include $BSDCFG_SHARE/sysrc.subr
BSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="080.console"
f_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
-ipgm=$( f_index_menu_selection $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
+ipgm=$( f_index_menusel_keyword $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
[ $? -eq $SUCCESS -a "$ipgm" ] && pgm="$ipgm"
############################################################ FUNCTIONS
@@ -100,7 +101,6 @@ shift $(( $OPTIND - 1 ))
#
# Initialize
#
-f_dialog_init
f_dialog_title "$msg_system_console_keyboard_repeat_rate"
f_dialog_backtitle "${ipgm:+bsdconfig }$pgm"
f_mustberoot_init
diff --git a/usr.sbin/bsdconfig/console/saver b/usr.sbin/bsdconfig/console/saver
index 0ec332c..4f6729b 100755
--- a/usr.sbin/bsdconfig/console/saver
+++ b/usr.sbin/bsdconfig/console/saver
@@ -30,6 +30,7 @@
BSDCFG_SHARE="/usr/share/bsdconfig"
. $BSDCFG_SHARE/common.subr || exit 1
+f_dprintf "%s: loading includes..." "$0"
f_include $BSDCFG_SHARE/dialog.subr
f_include $BSDCFG_SHARE/mustberoot.subr
f_include $BSDCFG_SHARE/sysrc.subr
@@ -37,7 +38,7 @@ f_include $BSDCFG_SHARE/sysrc.subr
BSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="080.console"
f_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
-ipgm=$( f_index_menu_selection $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
+ipgm=$( f_index_menusel_keyword $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
[ $? -eq $SUCCESS -a "$ipgm" ] && pgm="$ipgm"
############################################################ FUNCTIONS
@@ -110,7 +111,6 @@ shift $(( $OPTIND - 1 ))
#
# Initialize
#
-f_dialog_init
f_dialog_title "$msg_system_console_screen_saver"
f_dialog_backtitle "${ipgm:+bsdconfig }$pgm"
f_mustberoot_init
@@ -167,33 +167,10 @@ while :; do
break ;;
"$msg_timeout") # Set the screen saver timeout interval
f_dialog_title "$msg_value_required"
- title="$DIALOG_TITLE"
- btitle="$DIALOG_BACKTITLE"
+ blanktime=$( f_dialog_input "$msg_enter_timeout_period" \
+ "$( f_sysrc_get blanktime )"
+ ) && f_sysrc_set blanktime "$blanktime"
f_dialog_title_restore
- prompt="$msg_enter_timeout_period"
- blanktime=$( f_sysrc_get blanktime )
- hline=""
- size=$( f_dialog_inputbox_size \
- "$title" \
- "$btitle" \
- "$prompt" \
- "$blanktime" \
- "$hline" )
- dialog_inputbox=$( $DIALOG \
- --title "$title" \
- --backtitle "$btitle" \
- --hline "$hline" \
- --ok-label "$msg_ok" \
- --cancel-label "$msg_cancel" \
- --inputbox "$prompt" $size \
- "$blanktime" \
- 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
- )
- retval=$?
- setvar DIALOG_INPUTBOX_$$ "$dialog_inputbox"
- blanktime=$( f_dialog_inputstr )
- [ $retval -eq $SUCCESS ] &&
- f_sysrc_set blanktime "$blanktime"
;;
esac
done
diff --git a/usr.sbin/bsdconfig/console/screenmap b/usr.sbin/bsdconfig/console/screenmap
index bdabe6a..5c5e29e 100755
--- a/usr.sbin/bsdconfig/console/screenmap
+++ b/usr.sbin/bsdconfig/console/screenmap
@@ -30,6 +30,7 @@
BSDCFG_SHARE="/usr/share/bsdconfig"
. $BSDCFG_SHARE/common.subr || exit 1
+f_dprintf "%s: loading includes..." "$0"
f_include $BSDCFG_SHARE/dialog.subr
f_include $BSDCFG_SHARE/mustberoot.subr
f_include $BSDCFG_SHARE/sysrc.subr
@@ -37,7 +38,7 @@ f_include $BSDCFG_SHARE/sysrc.subr
BSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="080.console"
f_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
-ipgm=$( f_index_menu_selection $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
+ipgm=$( f_index_menusel_keyword $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
[ $? -eq $SUCCESS -a "$ipgm" ] && pgm="$ipgm"
############################################################ FUNCTIONS
@@ -102,7 +103,6 @@ shift $(( $OPTIND - 1 ))
#
# Initialize
#
-f_dialog_init
f_dialog_title "$msg_system_console_screenmap"
f_dialog_backtitle "${ipgm:+bsdconfig }$pgm"
f_mustberoot_init
diff --git a/usr.sbin/bsdconfig/console/ttys b/usr.sbin/bsdconfig/console/ttys
index fcf45ef..3f9945e 100755
--- a/usr.sbin/bsdconfig/console/ttys
+++ b/usr.sbin/bsdconfig/console/ttys
@@ -30,6 +30,7 @@
BSDCFG_SHARE="/usr/share/bsdconfig"
. $BSDCFG_SHARE/common.subr || exit 1
+f_dprintf "%s: loading includes..." "$0"
f_include $BSDCFG_SHARE/dialog.subr
f_include $BSDCFG_SHARE/mustberoot.subr
f_include $BSDCFG_SHARE/sysrc.subr
@@ -37,7 +38,7 @@ f_include $BSDCFG_SHARE/sysrc.subr
BSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="080.console"
f_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
-ipgm=$( f_index_menu_selection $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
+ipgm=$( f_index_menusel_keyword $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
[ $? -eq $SUCCESS -a "$ipgm" ] && pgm="$ipgm"
############################################################ CONFIGURATION
@@ -172,7 +173,6 @@ shift $(( $OPTIND - 1 ))
#
# Initialize
#
-f_dialog_init
f_dialog_title "$msg_system_console_terminal_type"
f_dialog_backtitle "${ipgm:+bsdconfig }$pgm"
f_mustberoot_init
@@ -192,7 +192,7 @@ while :; do
err=$( ttys_set_type "$consterm" 2>&1 )
[ "$err" ] || break
- f_show_msg "%s" "$err"
+ f_dialog_msgbox "$err"
done
exit $SUCCESS
diff --git a/usr.sbin/bsdconfig/diskmgmt/diskmgmt b/usr.sbin/bsdconfig/diskmgmt/diskmgmt
index aab901f..27852fb 100755
--- a/usr.sbin/bsdconfig/diskmgmt/diskmgmt
+++ b/usr.sbin/bsdconfig/diskmgmt/diskmgmt
@@ -30,13 +30,14 @@
BSDCFG_SHARE="/usr/share/bsdconfig"
. $BSDCFG_SHARE/common.subr || exit 1
+f_dprintf "%s: loading includes..." "$0"
f_include $BSDCFG_SHARE/dialog.subr
f_include $BSDCFG_SHARE/mustberoot.subr
BSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="050.diskmgmt"
f_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
-ipgm=$( f_index_menu_selection $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
+ipgm=$( f_index_menusel_keyword $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
[ $? -eq $SUCCESS -a "$ipgm" ] && pgm="$ipgm"
############################################################ CONFIGURATION
@@ -65,7 +66,6 @@ shift $(( $OPTIND - 1 ))
#
# Initialize
#
-f_dialog_init
f_mustberoot_init
#
diff --git a/usr.sbin/bsdconfig/docsinstall/docsinstall b/usr.sbin/bsdconfig/docsinstall/docsinstall
index d6fe2d2..52e0479 100755
--- a/usr.sbin/bsdconfig/docsinstall/docsinstall
+++ b/usr.sbin/bsdconfig/docsinstall/docsinstall
@@ -30,13 +30,14 @@
BSDCFG_SHARE="/usr/share/bsdconfig"
. $BSDCFG_SHARE/common.subr || exit 1
+f_dprintf "%s: loading includes..." "$0"
f_include $BSDCFG_SHARE/dialog.subr
f_include $BSDCFG_SHARE/mustberoot.subr
BSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="020.docsinstall"
f_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
-ipgm=$( f_index_menu_selection $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
+ipgm=$( f_index_menusel_keyword $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
[ $? -eq $SUCCESS -a "$ipgm" ] && pgm="$ipgm"
############################################################ CONFIGURATION
@@ -65,7 +66,6 @@ shift $(( $OPTIND - 1 ))
#
# Initialize
#
-f_dialog_init
f_mustberoot_init
#
diff --git a/usr.sbin/bsdconfig/dot/dot b/usr.sbin/bsdconfig/dot/dot
index 1a90324..2cc659c 100755
--- a/usr.sbin/bsdconfig/dot/dot
+++ b/usr.sbin/bsdconfig/dot/dot
@@ -30,12 +30,13 @@
BSDCFG_SHARE="/usr/share/bsdconfig"
. $BSDCFG_SHARE/common.subr || exit 1
+f_dprintf "%s: loading includes..." "$0"
BSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="dot"
f_include_lang $BSDCFG_LIBE/include/messages.subr
f_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
-ipgm=$( f_index_menu_selection $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
+ipgm=$( f_index_menusel_keyword $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
[ $? -eq $SUCCESS -a "$ipgm" ] && pgm="$ipgm"
############################################################ CONFIGURATION
diff --git a/usr.sbin/bsdconfig/examples/bsdconfigrc b/usr.sbin/bsdconfig/examples/bsdconfigrc
index a863a94..21d4264 100644
--- a/usr.sbin/bsdconfig/examples/bsdconfigrc
+++ b/usr.sbin/bsdconfig/examples/bsdconfigrc
@@ -31,5 +31,6 @@
# debugging aid for development
# f_dprintf() {
-# echo "$(date):$pgm: $1" >> $HOME/out
+# local format="$1"; shift
+# printf "$(date):$pgm:$format\n" "$@" >> $HOME/out
# }
diff --git a/usr.sbin/bsdconfig/include/messages.subr b/usr.sbin/bsdconfig/include/messages.subr
index ec988ad..dc7c246 100644
--- a/usr.sbin/bsdconfig/include/messages.subr
+++ b/usr.sbin/bsdconfig/include/messages.subr
@@ -34,6 +34,7 @@ msg_becoming_root_via_sudo="Becoming root via sudo(8)..."
msg_cancel="Cancel"
msg_cancel_exit="Cancel/Exit"
msg_cannot_create_permission_denied="%s: cannot create %s: Permission denied"
+msg_command_failed_rest_of_script_aborted="Command \`%s' failed - rest of script aborted."
msg_created_path="Created %s"
msg_directory_not_found="%s: Directory not found."
msg_exit="Exit"
@@ -56,6 +57,7 @@ msg_secure_mode_requires_x11="Secure-mode requires X11 (use \`-X')!"
msg_secure_mode_requires_root="Secure-mode requires root-access!"
msg_sorry_try_again="Sorry, try again."
msg_try_sudo_only_this_once="Try sudo(8) only this once"
+msg_unable_to_open="Unable to open %s"
msg_unknown_user="Unknown user: %s"
msg_usage="Usage"
msg_user_disallowed="User disallowed: %s"
diff --git a/usr.sbin/bsdconfig/mouse/disable b/usr.sbin/bsdconfig/mouse/disable
index 099cf31..0f41d73 100755
--- a/usr.sbin/bsdconfig/mouse/disable
+++ b/usr.sbin/bsdconfig/mouse/disable
@@ -30,6 +30,7 @@
BSDCFG_SHARE="/usr/share/bsdconfig"
. $BSDCFG_SHARE/common.subr || exit 1
+f_dprintf "%s: loading includes..." "$0"
f_include $BSDCFG_SHARE/dialog.subr
f_include $BSDCFG_SHARE/mustberoot.subr
f_include $BSDCFG_SHARE/sysrc.subr
@@ -37,7 +38,7 @@ f_include $BSDCFG_SHARE/sysrc.subr
BSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="110.mouse"
f_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
-ipgm=$( f_index_menu_selection $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
+ipgm=$( f_index_menusel_keyword $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
[ $? -eq $SUCCESS -a "$ipgm" ] && pgm="$ipgm"
############################################################ CONFIGURATION
@@ -65,7 +66,6 @@ shift $(( $OPTIND - 1 ))
#
# Initialize
#
-f_dialog_init
f_dialog_title "$msg_mouse_disable"
f_dialog_backtitle "${ipgm:+bsdconfig }$pgm"
f_mustberoot_init
diff --git a/usr.sbin/bsdconfig/mouse/enable b/usr.sbin/bsdconfig/mouse/enable
index e1cf796..f401461 100755
--- a/usr.sbin/bsdconfig/mouse/enable
+++ b/usr.sbin/bsdconfig/mouse/enable
@@ -30,6 +30,7 @@
BSDCFG_SHARE="/usr/share/bsdconfig"
. $BSDCFG_SHARE/common.subr || exit 1
+f_dprintf "%s: loading includes..." "$0"
f_include $BSDCFG_SHARE/dialog.subr
f_include $BSDCFG_SHARE/mustberoot.subr
f_include $BSDCFG_SHARE/sysrc.subr
@@ -37,7 +38,7 @@ f_include $BSDCFG_SHARE/sysrc.subr
BSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="110.mouse"
f_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
-ipgm=$( f_index_menu_selection $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
+ipgm=$( f_index_menusel_keyword $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
[ $? -eq $SUCCESS -a "$ipgm" ] && pgm="$ipgm"
############################################################ CONFIGURATION
@@ -65,7 +66,6 @@ shift $(( $OPTIND - 1 ))
#
# Initialize
#
-f_dialog_init
f_dialog_title "$msg_mouse_enable"
f_dialog_backtitle "${ipgm:+bsdconfig }$pgm"
f_mustberoot_init
diff --git a/usr.sbin/bsdconfig/mouse/flags b/usr.sbin/bsdconfig/mouse/flags
index c989048..06218ac 100755
--- a/usr.sbin/bsdconfig/mouse/flags
+++ b/usr.sbin/bsdconfig/mouse/flags
@@ -30,6 +30,7 @@
BSDCFG_SHARE="/usr/share/bsdconfig"
. $BSDCFG_SHARE/common.subr || exit 1
+f_dprintf "%s: loading includes..." "$0"
f_include $BSDCFG_SHARE/dialog.subr
f_include $BSDCFG_SHARE/mustberoot.subr
f_include $BSDCFG_SHARE/sysrc.subr
@@ -37,7 +38,7 @@ f_include $BSDCFG_SHARE/sysrc.subr
BSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="110.mouse"
f_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
-ipgm=$( f_index_menu_selection $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
+ipgm=$( f_index_menusel_keyword $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
[ $? -eq $SUCCESS -a "$ipgm" ] && pgm="$ipgm"
############################################################ CONFIGURATION
@@ -65,7 +66,6 @@ shift $(( $OPTIND - 1 ))
#
# Initialize
#
-f_dialog_init
f_dialog_title "$msg_mouse_flags"
f_dialog_backtitle "${ipgm:+bsdconfig }$pgm"
f_mustberoot_init
@@ -79,32 +79,10 @@ flags=$( f_sysrc_get moused_flags )
# Prompt the user with the current value
#
f_dialog_title "$msg_value_required"
-title="$DIALOG_TITLE"
-btitle="$DIALOG_BACKTITLE"
+flags=$( f_dialog_input "$msg_please_specify_the_mouse_daemon_flags" \
+ "$flags"
+ ) || f_die
f_dialog_title_restore
-hline=
-prompt="$msg_please_specify_the_mouse_daemon_flags"
-size=$( f_dialog_inputbox_size \
- "$title" \
- "$btitle" \
- "$prompt" \
- "$flags" \
- "$hline" )
-dialog_inputbox=$( eval $DIALOG \
- --title \"\$title\" \
- --backtitle \"\$btitle\" \
- --hline \"\$hline\" \
- --ok-label \"\$msg_ok\" \
- --cancel-label \"\$msg_cancel\" \
- --inputbox \"\$prompt\" $size \
- \"\$flags\" \
- 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
-)
-retval=$?
-setvar MENU_INPUTBOX_$$ "$dialog_inputbox"
-flags=$( f_dialog_inputstr )
-
-[ $retval -eq $SUCCESS ] || f_die
#
# Save the new value
diff --git a/usr.sbin/bsdconfig/mouse/mouse b/usr.sbin/bsdconfig/mouse/mouse
index 911ea95..95d7677 100755
--- a/usr.sbin/bsdconfig/mouse/mouse
+++ b/usr.sbin/bsdconfig/mouse/mouse
@@ -30,13 +30,14 @@
BSDCFG_SHARE="/usr/share/bsdconfig"
. $BSDCFG_SHARE/common.subr || exit 1
+f_dprintf "%s: loading includes..." "$0"
f_include $BSDCFG_SHARE/dialog.subr
f_include $BSDCFG_SHARE/mustberoot.subr
BSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="110.mouse"
f_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
-ipgm=$( f_index_menu_selection $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
+ipgm=$( f_index_menusel_keyword $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
[ $? -eq $SUCCESS -a "$ipgm" ] && pgm="$ipgm"
############################################################ FUNCTIONS
@@ -101,7 +102,6 @@ shift $(( $OPTIND - 1 ))
#
# Initialize
#
-f_dialog_init
f_dialog_title "$msg_please_configure_your_mouse"
f_dialog_backtitle "${ipgm:+bsdconfig }$pgm"
f_mustberoot_init
diff --git a/usr.sbin/bsdconfig/mouse/port b/usr.sbin/bsdconfig/mouse/port
index 4ab54a7..87d4a0b 100755
--- a/usr.sbin/bsdconfig/mouse/port
+++ b/usr.sbin/bsdconfig/mouse/port
@@ -30,6 +30,7 @@
BSDCFG_SHARE="/usr/share/bsdconfig"
. $BSDCFG_SHARE/common.subr || exit 1
+f_dprintf "%s: loading includes..." "$0"
f_include $BSDCFG_SHARE/dialog.subr
f_include $BSDCFG_SHARE/mustberoot.subr
f_include $BSDCFG_SHARE/sysrc.subr
@@ -37,7 +38,7 @@ f_include $BSDCFG_SHARE/sysrc.subr
BSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="110.mouse"
f_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
-ipgm=$( f_index_menu_selection $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
+ipgm=$( f_index_menusel_keyword $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
[ $? -eq $SUCCESS -a "$ipgm" ] && pgm="$ipgm"
############################################################ FUNCTIONS
@@ -102,7 +103,6 @@ shift $(( $OPTIND - 1 ))
#
# Initialize
#
-f_dialog_init
f_dialog_title "$msg_select_your_mouse_port_from_the_following_menu"
f_dialog_backtitle "${ipgm:+bsdconfig }$pgm"
f_mustberoot_init
diff --git a/usr.sbin/bsdconfig/mouse/type b/usr.sbin/bsdconfig/mouse/type
index f1e88fb..ee9c4ed 100755
--- a/usr.sbin/bsdconfig/mouse/type
+++ b/usr.sbin/bsdconfig/mouse/type
@@ -30,6 +30,7 @@
BSDCFG_SHARE="/usr/share/bsdconfig"
. $BSDCFG_SHARE/common.subr || exit 1
+f_dprintf "%s: loading includes..." "$0"
f_include $BSDCFG_SHARE/dialog.subr
f_include $BSDCFG_SHARE/mustberoot.subr
f_include $BSDCFG_SHARE/sysrc.subr
@@ -37,7 +38,7 @@ f_include $BSDCFG_SHARE/sysrc.subr
BSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="110.mouse"
f_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
-ipgm=$( f_index_menu_selection $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
+ipgm=$( f_index_menusel_keyword $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
[ $? -eq $SUCCESS -a "$ipgm" ] && pgm="$ipgm"
############################################################ FUNCTIONS
@@ -106,7 +107,6 @@ shift $(( $OPTIND - 1 ))
#
# Initialize
#
-f_dialog_init
f_dialog_title "$msg_select_a_protocol_type_for_your_mouse"
f_dialog_backtitle "${ipgm:+bsdconfig }$pgm"
f_mustberoot_init
diff --git a/usr.sbin/bsdconfig/networking/defaultrouter b/usr.sbin/bsdconfig/networking/defaultrouter
index c2375b3..44e522e 100755
--- a/usr.sbin/bsdconfig/networking/defaultrouter
+++ b/usr.sbin/bsdconfig/networking/defaultrouter
@@ -30,6 +30,7 @@
BSDCFG_SHARE="/usr/share/bsdconfig"
. $BSDCFG_SHARE/common.subr || exit 1
+f_dprintf "%s: loading includes..." "$0"
f_include $BSDCFG_SHARE/dialog.subr
f_include $BSDCFG_SHARE/mustberoot.subr
f_include $BSDCFG_SHARE/networking/routing.subr
@@ -37,7 +38,7 @@ f_include $BSDCFG_SHARE/networking/routing.subr
BSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="120.networking"
f_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
-ipgm=$( f_index_menu_selection $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
+ipgm=$( f_index_menusel_keyword $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
[ $? -eq $SUCCESS -a "$ipgm" ] && pgm="$ipgm"
############################################################ MAIN
@@ -58,7 +59,6 @@ shift $(( $OPTIND - 1 ))
#
# Initialize
#
-f_dialog_init
f_dialog_title "$msg_default_router"
f_dialog_backtitle "${ipgm:+bsdconfig }$pgm"
f_mustberoot_init
diff --git a/usr.sbin/bsdconfig/networking/devices b/usr.sbin/bsdconfig/networking/devices
index c7c550f..443294f 100755
--- a/usr.sbin/bsdconfig/networking/devices
+++ b/usr.sbin/bsdconfig/networking/devices
@@ -30,6 +30,7 @@
BSDCFG_SHARE="/usr/share/bsdconfig"
. $BSDCFG_SHARE/common.subr || exit 1
+f_dprintf "%s: loading includes..." "$0"
f_include $BSDCFG_SHARE/dialog.subr
f_include $BSDCFG_SHARE/mustberoot.subr
f_include $BSDCFG_SHARE/sysrc.subr
@@ -41,7 +42,7 @@ f_include $BSDCFG_SHARE/networking/netmask.subr
BSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="120.networking"
f_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
-ipgm=$( f_index_menu_selection $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
+ipgm=$( f_index_menusel_keyword $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
[ $? -eq $SUCCESS -a "$ipgm" ] && pgm="$ipgm"
############################################################ MAIN
@@ -62,7 +63,6 @@ shift $(( $OPTIND - 1 ))
#
# Initialize
#
-f_dialog_init
f_dialog_title "$msg_networking_devices"
f_dialog_backtitle "${ipgm:+bsdconfig }$pgm"
f_mustberoot_init
diff --git a/usr.sbin/bsdconfig/networking/hostname b/usr.sbin/bsdconfig/networking/hostname
index 32679b4..231320e 100755
--- a/usr.sbin/bsdconfig/networking/hostname
+++ b/usr.sbin/bsdconfig/networking/hostname
@@ -30,6 +30,7 @@
BSDCFG_SHARE="/usr/share/bsdconfig"
. $BSDCFG_SHARE/common.subr || exit 1
+f_dprintf "%s: loading includes..." "$0"
f_include $BSDCFG_SHARE/dialog.subr
f_include $BSDCFG_SHARE/mustberoot.subr
f_include $BSDCFG_SHARE/networking/hostname.subr
@@ -37,7 +38,7 @@ f_include $BSDCFG_SHARE/networking/hostname.subr
BSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="120.networking"
f_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
-ipgm=$( f_index_menu_selection $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
+ipgm=$( f_index_menusel_keyword $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
[ $? -eq $SUCCESS -a "$ipgm" ] && pgm="$ipgm"
############################################################ MAIN
@@ -58,7 +59,6 @@ shift $(( $OPTIND - 1 ))
#
# Initialize
#
-f_dialog_init
f_dialog_title "$msg_hostname_domain"
f_dialog_backtitle "${ipgm:+bsdconfig }$pgm"
f_mustberoot_init
diff --git a/usr.sbin/bsdconfig/networking/include/messages.subr b/usr.sbin/bsdconfig/networking/include/messages.subr
index dac8e20..3a3d1bc 100644
--- a/usr.sbin/bsdconfig/networking/include/messages.subr
+++ b/usr.sbin/bsdconfig/networking/include/messages.subr
@@ -55,10 +55,14 @@ msg_hostname_label_is_null="ERROR! One or more individual labels within the host
msg_hostname_label_starts_or_ends_with_hyphen="ERROR! One or more individual labels within the hostname\n(separated by dots) starts or ends with a hyphen (hyphens\nare allowed, but a label cannot begin or end with a hyphen).\n\nInvalid Hostname: %s"
msg_internal_error_nsindex_value="FATAL! dialog_input_nameserver_edit_awk: variable\nnsindex must be a whole positive integer greater-\nthan or equal-to zero.\n\nInvalid nsindex: %s"
msg_ipaddr4="ipaddr"
-msg_ipv4_addr_octet_contains_invalid_chars="ERROR! One or more individual octets within the IP address\n(separated by dots) contains one or more invalid characters.\nOctets must contain only the characters 0-9.\n\nInvalid IP Address: %s"
-msg_ipv4_addr_octet_exceeds_max_value="ERROR! One or more individual octets within the IP address\n(separated by dots) exceeds the maximum of 255.\n\nInvalid IP Address: %s"
-msg_ipv4_addr_octet_is_null="ERROR! One or more individual octets within the IP address\n(separated by dots) are null and/or missing.\n\nInvalid IP Address: %s"
-msg_ipv4_addr_octet_missing_or_extra="ERROR! The IP address entered has either too few or too many\noctets.\n\nInvalid IP Address: %s"
+msg_ipv4_addr_octet_contains_invalid_chars="ERROR! One or more individual octets within the IPv4 address\n(separated by dots) contains one or more invalid characters.\nOctets must contain only the characters 0-9.\n\nInvalid IP Address: %s"
+msg_ipv4_addr_octet_exceeds_max_value="ERROR! One or more individual octets within the IPv4 address\n(separated by dots) exceeds the maximum of 255.\n\nInvalid IP Address: %s"
+msg_ipv4_addr_octet_is_null="ERROR! One or more individual octets within the IPv4 address\n(separated by dots) are null and/or missing.\n\nInvalid IP Address: %s"
+msg_ipv4_addr_octet_missing_or_extra="ERROR! The IPv4 address entered has either too few (less than\nfour) or too many (more than four) octets, separated by dots.\n\nInvalid IP Address: %s"
+msg_ipv6_addr_segment_contains_invalid_chars="ERROR! One or more individual segments within the IP address\n(separated by colons) contains one or more invalid characters.\nSegments must contain only combinations of the characters 0-9,\nA-F, or a-f.\n\nInvalid IPv6 Address: %s"
+msg_ipv6_addr_segment_contains_too_many_chars="ERROR! One or more individual segments within the IP address\n(separated by colons) exceeds the length of 4 hex-digits.\n\nInvalid IPv6 Address: %s"
+msg_ipv6_addr_too_few_or_extra_segments="ERROR! The IP address entered has either too few (less than 3), too\nmany (more than 8), or not enough segments, separated by colons.\n\nInvalid IPv6 Address: %s"
+msg_ipv6_addr_too_many_null_segments="ERROR! Too many/incorrect null segments. A single null\nsegment is allowed within the IP address (separated by\ncolons) but not allowed at the beginning or end (unless\na double-null segment; i.e., \"::*\" or \"*::\").\n\nInvalid IPv6 Address: %s"
msg_ipv4_mask_field_contains_invalid_chars="ERROR! One or more individual fields within the subnet mask\n(separated by dots) contains one or more invalid characters.\n\nInvalid Subnet Mask: %s"
msg_ipv4_mask_field_exceeds_max_value="ERROR! One or more individual fields within the subnet mask\n(separated by dots) exceeds the maximum of 255.\n\nInvalid Subnet Mask: %s"
msg_ipv4_mask_field_invalid_value="ERROR! One or more individual fields within the subnet mask\n(separated by dots) contains one or more invalid integers.\nFields must be one of 0/128/192/224/240/248/252/254/255.\n\nInvalid Subnet Mask: %s"
diff --git a/usr.sbin/bsdconfig/networking/nameservers b/usr.sbin/bsdconfig/networking/nameservers
index 13cdf51..9ebebc8 100755
--- a/usr.sbin/bsdconfig/networking/nameservers
+++ b/usr.sbin/bsdconfig/networking/nameservers
@@ -30,6 +30,7 @@
BSDCFG_SHARE="/usr/share/bsdconfig"
. $BSDCFG_SHARE/common.subr || exit 1
+f_dprintf "%s: loading includes..." "$0"
f_include $BSDCFG_SHARE/dialog.subr
f_include $BSDCFG_SHARE/mustberoot.subr
f_include $BSDCFG_SHARE/networking/resolv.subr
@@ -37,7 +38,7 @@ f_include $BSDCFG_SHARE/networking/resolv.subr
BSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="120.networking"
f_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
-ipgm=$( f_index_menu_selection $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
+ipgm=$( f_index_menusel_keyword $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
[ $? -eq $SUCCESS -a "$ipgm" ] && pgm="$ipgm"
############################################################ MAIN
@@ -58,7 +59,6 @@ shift $(( $OPTIND - 1 ))
#
# Initialize
#
-f_dialog_init
f_dialog_title "$msg_dns_nameservers"
f_dialog_backtitle "${ipgm:+bsdconfig }$pgm"
f_mustberoot_init
diff --git a/usr.sbin/bsdconfig/networking/networking b/usr.sbin/bsdconfig/networking/networking
index fe3ce1b..8acfbde 100755
--- a/usr.sbin/bsdconfig/networking/networking
+++ b/usr.sbin/bsdconfig/networking/networking
@@ -30,13 +30,14 @@
BSDCFG_SHARE="/usr/share/bsdconfig"
. $BSDCFG_SHARE/common.subr || exit 1
+f_dprintf "%s: loading includes..." "$0"
f_include $BSDCFG_SHARE/dialog.subr
f_include $BSDCFG_SHARE/mustberoot.subr
BSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="120.networking"
f_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
-ipgm=$( f_index_menu_selection $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
+ipgm=$( f_index_menusel_keyword $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
[ $? -eq $SUCCESS -a "$ipgm" ] && pgm="$ipgm"
############################################################ FUNCTIONS
@@ -98,7 +99,6 @@ shift $(( $OPTIND - 1 ))
#
# Initialize
#
-f_dialog_init
f_dialog_title "$msg_network_management"
f_dialog_backtitle "${ipgm:+bsdconfig }$pgm"
f_mustberoot_init
diff --git a/usr.sbin/bsdconfig/networking/share/common.subr b/usr.sbin/bsdconfig/networking/share/common.subr
index e8970d0..e501a9c 100644
--- a/usr.sbin/bsdconfig/networking/share/common.subr
+++ b/usr.sbin/bsdconfig/networking/share/common.subr
@@ -62,4 +62,8 @@ f_nfs_mounted()
[ "$( df -t nfs )" ]
}
+############################################################ MAIN
+
+f_dprintf "%s: Successfully loaded." networking/common.subr
+
fi # ! $_NETWORKING_COMMON_SUBR
diff --git a/usr.sbin/bsdconfig/networking/share/device.subr b/usr.sbin/bsdconfig/networking/share/device.subr
index 4f870af..23ac83b 100644
--- a/usr.sbin/bsdconfig/networking/share/device.subr
+++ b/usr.sbin/bsdconfig/networking/share/device.subr
@@ -30,6 +30,7 @@ if [ ! "$_NETWORKING_DEVICE_SUBR" ]; then _NETWORKING_DEVICE_SUBR=1
BSDCFG_SHARE="/usr/share/bsdconfig"
. $BSDCFG_SHARE/common.subr || exit 1
+f_dprintf "%s: loading includes..." networking/device.subr
f_include $BSDCFG_SHARE/dialog.subr
f_include $BSDCFG_SHARE/sysrc.subr
f_include $BSDCFG_SHARE/networking/common.subr
@@ -342,6 +343,7 @@ f_dialog_menu_netdev_edit()
--ok-label \"\$msg_ok\" \
--cancel-label \"\$msg_cancel\" \
--help-button \
+ --help-label \"\$msg_help\" \
${USE_XDIALOG:+--help \"\"} \
--menu \"\$prompt\" $size \
$menu_list \
@@ -378,11 +380,7 @@ f_dialog_menu_netdev_edit()
if f_nfs_mounted && ! f_jailed; then
local setting="$( printf "$msg_current_dhcp_status" \
"$interface" "$dhcp_status" )"
- local message="$(
- printf "$msg_nfs_mounts_may_cause_hang" \
- "$setting"
- )"
- f_dialog_msgbox "$message"
+ f_show_msg "$msg_nfs_mounts_may_cause_hang" "$setting"
continue
fi
@@ -440,8 +438,7 @@ f_dialog_menu_netdev_edit()
"$options" != "$options_orig" -o \
"$dhcp" != "$dhcp_orig" ]
then
- f_dialog_info "$( printf "$msg_saving_network_interface" \
- "$interface" )"
+ f_show_info "$msg_saving_network_interface" "$interface"
local value=
if [ "$dhcp" ]; then
@@ -459,11 +456,9 @@ f_dialog_menu_netdev_edit()
# Re/Apply the settings if desired
#
if [ ! "$dhcp" ]; then
- f_dialog_yesno "Would you like to bring the $interface" \
- "interface up right now?"
- if [ $? -eq $SUCCESS ]; then
- f_dialog_info "$( printf "$msg_bring_interface_up" \
- "$interface" )"
+ if f_yesno "$msg_bring_interface_up" "$interface"
+ then
+ f_show_info "$msg_bring_interface_up" "$interface"
local dr="$( f_sysrc_get defaultrouter )" err
if [ "$dr" = "NO" -o ! "$dr" ]; then
@@ -501,4 +496,8 @@ f_dialog_menu_netdev_edit()
return $SUCCESS
}
+############################################################ MAIN
+
+f_dprintf "%s: Successfully loaded." networking/device.subr
+
fi # ! $_NETWORKING_DEVICE_SUBR
diff --git a/usr.sbin/bsdconfig/networking/share/hostname.subr b/usr.sbin/bsdconfig/networking/share/hostname.subr
index fed8679..b276059 100644
--- a/usr.sbin/bsdconfig/networking/share/hostname.subr
+++ b/usr.sbin/bsdconfig/networking/share/hostname.subr
@@ -30,6 +30,7 @@ if [ ! "$_NETWORKING_HOSTNAME_SUBR" ]; then _NETWORKING_HOSTNAME_SUBR=1
BSDCFG_SHARE="/usr/share/bsdconfig"
. $BSDCFG_SHARE/common.subr || exit 1
+f_dprintf "%s: loading includes..." networking/hostname.subr
f_include $BSDCFG_SHARE/sysrc.subr
f_include $BSDCFG_SHARE/dialog.subr
f_include $BSDCFG_SHARE/networking/common.subr
@@ -40,7 +41,7 @@ f_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
############################################################ FUNCTIONS
-# f_dialog_validate_hostname $hostname
+# f_validate_hostname $hostname
#
# Returns zero if the given argument (a fully-qualified hostname) is compliant
# with standards set-forth in RFC's 952 and 1123 of the Network Working Group:
@@ -65,9 +66,9 @@ f_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
# 3 One or more individual labels within the hostname are null.
#
# If the hostname is determined to be invalid, the appropriate error will be
-# displayed using the f_dialog_msgbox function.
+# displayed using the f_show_msg function.
#
-f_dialog_validate_hostname()
+f_validate_hostname()
{
local fqhn="$1"
@@ -95,23 +96,51 @@ f_dialog_validate_hostname()
done
)
+}
- #
- # Produce an appropriate error message if necessary.
- #
- local retval=$?
- case $retval in
- 1) f_dialog_msgbox "$( printf \
- "$msg_hostname_label_contains_invalid_chars" "$fqhn" )";;
- 2) f_dialog_msgbox "$( printf \
- "$msg_hostname_label_starts_or_ends_with_hyphen" "$fqhn" )";;
- 3) f_dialog_msgbox "$( printf \
- "$msg_hostname_label_is_null" "$fqhn" )";;
- 63) f_dialog_msgbox "$( printf \
- "$msg_hostname_label_exceeds_max_length" "$fqhn" )";;
- 255) f_dialog_msgbox "$( printf \
- "$msg_hostname_exceeds_max_length" "$fqhn" )";;
+# f_dialog_hnerror $error $hostname
+#
+# Display a msgbox with the appropriate error message for an error returned by
+# the f_validate_hostname function.
+#
+f_dialog_hnerror()
+{
+ local error="$1" fqhn="$2"
+
+ [ ${error:-0} -ne 0 ] || return $SUCCESS
+
+ case "$error" in
+ 1) f_show_msg "$msg_hostname_label_contains_invalid_chars" "$fqhn";;
+ 2) f_show_msg "$msg_hostname_label_starts_or_ends_with_hyphen" "$fqhn";;
+ 3) f_show_msg "$msg_hostname_label_is_null" "$fqhn";;
+ 63) f_show_msg "$msg_hostname_label_exceeds_max_length" "$fqhn";;
+ 255) f_show_msg "$msg_hostname_exceeds_max_length" "$fqhn";;
esac
+}
+
+# f_dialog_validate_hostname $hostname
+#
+# Returns zero if the given argument (a fully-qualified hostname) is compliant
+# with standards set-forth in RFC's 952 and 1123 of the Network Working Group:
+#
+# RFC 952 - DoD Internet host table specification
+# http://tools.ietf.org/html/rfc952
+#
+# RFC 1123 - Requirements for Internet Hosts - Application and Support
+# http://tools.ietf.org/html/rfc1123
+#
+# If the hostname is determined to be invalid, the appropriate error will be
+# displayed using the f_dialog_hnerror function above.
+#
+f_dialog_validate_hostname()
+{
+ local fqhn="$1"
+
+ f_validate_hostname "$fqhn"
+ local retval=$?
+
+ # Produce an appropriate error message if necessary.
+ [ $retval -eq $SUCCESS ] || f_dialog_hnerror $retval "$fqhn"
return $retval
}
@@ -132,41 +161,15 @@ f_dialog_input_hostname()
msg="$msg_please_enter_fqhn"
fi
- local hline="$hline_alnum_punc_tab_enter"
-
#
# Loop until the user provides taint-free input.
#
- local size height width
while :; do
-
- size=$( f_dialog_inputbox_size \
- "$DIALOG_TITLE" \
- "$DIALOG_BACKTITLE" \
- "$msg" \
- "$hostname" \
- "$hline" )
- local dialog_inputbox
- dialog_inputbox=$( eval $DIALOG \
- --title \"\$DIALOG_TITLE\" \
- --backtitle \"\$DIALOG_BACKTITLE\" \
- --hline \"\$hline\" \
- --ok-label \"\$msg_ok\" \
- --cancel-label \"\$msg_cancel\" \
- --inputbox \"\$msg\" $size \
- \"\$hostname\" \
- 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
- )
-
- local retval=$?
- setvar DIALOG_INPUTBOX_$$ "$dialog_inputbox"
- hostname=$( f_dialog_inputstr )
-
- [ $retval -eq $SUCCESS ] || return $retval
-
+ hostname=$( f_dialog_input "$msg" "$hostname" \
+ "$hline_alnum_punc_tab_enter"
+ ) || return
# Taint-check the user's input
f_dialog_validate_hostname "$hostname" && break
-
done
#
@@ -187,7 +190,7 @@ f_dialog_input_hostname()
# the stored configuration (in rc.conf(5)).
#
if [ "$( hostname )" != "$( f_sysrc_get hostname )" ]; then
- [ ! "$USE_XDIALOG" ] && dialog_clear
+ [ ! "$USE_XDIALOG" ] && f_dialog_clear
#
# If connected via ssh(1) and performing X11-Forwarding, don't
@@ -195,15 +198,11 @@ f_dialog_input_hostname()
# "X11 connection rejected because of wrong authentication."
#
if [ "$USE_XDIALOG" -a "$SSH_CONNECTION" ]; then
- f_dialog_msgbox "$(
- printf "$msg_activate_hostname_x11warning" \
- "$( hostname )" "$hostname"
- )"
+ f_show_msg "$msg_activate_hostname_x11warning" \
+ "$( hostname )" "$hostname"
else
- f_dialog_yesno "$(
- printf "$msg_activate_hostname" \
- "$( hostname )" "$hostname" \
- )" \
+ f_yesno "$msg_activate_hostname" \
+ "$( hostname )" "$hostname" \
&& hostname "$hostname"
fi
fi
@@ -211,4 +210,8 @@ f_dialog_input_hostname()
return $SUCCESS
}
+############################################################ MAIN
+
+f_dprintf "%s: Successfully loaded." networking/hostname.subr
+
fi # ! $_NETWORKING_HOSTNAME_SUBR
diff --git a/usr.sbin/bsdconfig/networking/share/ipaddr.subr b/usr.sbin/bsdconfig/networking/share/ipaddr.subr
index 7e858dc..d5398e5 100644
--- a/usr.sbin/bsdconfig/networking/share/ipaddr.subr
+++ b/usr.sbin/bsdconfig/networking/share/ipaddr.subr
@@ -30,7 +30,7 @@ if [ ! "$_NETWORKING_IPADDR_SUBR" ]; then _NETWORKING_IPADDR_SUBR=1
BSDCFG_SHARE="/usr/share/bsdconfig"
. $BSDCFG_SHARE/common.subr || exit 1
-f_include $BSDCFG_SHARE/sysrc.subr
+f_dprintf "%s: loading includes..." networking/ipaddr.subr
f_include $BSDCFG_SHARE/dialog.subr
f_include $BSDCFG_SHARE/strings.subr
f_include $BSDCFG_SHARE/networking/common.subr
@@ -60,7 +60,7 @@ f_ifconfig_inet()
'
}
-# f_dialog_validate_ipaddr $ipaddr
+# f_validate_ipaddr $ipaddr
#
# Returns zero if the given argument (an IP address) is of the proper format.
#
@@ -73,10 +73,7 @@ f_ifconfig_inet()
# maximum of 255 (or 2^8, being an octet comprised of 8 bits).
# 4 The IP address has either too few or too many octets.
#
-# If the IP address is determined to be invalid, the appropriate error will be
-# displayed using the above dialog_msgbox function.
-#
-f_dialog_validate_ipaddr()
+f_validate_ipaddr()
{
local ip="$1"
@@ -106,21 +103,222 @@ f_dialog_validate_ipaddr()
[ $noctets -eq 4 ] || exit 4
)
+}
- #
- # Produce an appropriate error message if necessary.
- #
+# f_dialog_iperror $error $ipaddr
+#
+# Display a msgbox with the appropriate error message for an error returned by
+# the f_validate_ipaddr function above.
+#
+f_dialog_iperror()
+{
+ local error="$1" ip="$2"
+
+ [ ${error:-0} -ne 0 ] || return $SUCCESS
+
+ case "$error" in
+ 1) f_show_msg "$msg_ipv4_addr_octet_contains_invalid_chars" "$ip";;
+ 2) f_show_msg "$msg_ipv4_addr_octet_is_null" "$ip";;
+ 3) f_show_msg "$msg_ipv4_addr_octet_exceeds_max_value" "$ip";;
+ 4) f_show_msg "$msg_ipv4_addr_octet_missing_or_extra" "$ip";;
+ esac
+}
+
+# f_dialog_validate_ipaddr $ipaddr
+#
+# Returns zero if the given argument (an IP address) is of the proper format.
+#
+# If the IP address is determined to be invalid, the appropriate error will be
+# displayed using the f_dialog_iperror function above.
+#
+f_dialog_validate_ipaddr()
+{
+ local ip="$1"
+
+ f_validate_ipaddr "$ip"
local retval=$?
- case $retval in
- 1) f_dialog_msgbox "$( printf \
- "$msg_ipv4_addr_octet_contains_invalid_chars" "$ip" )";;
- 2) f_dialog_msgbox "$( printf \
- "$msg_ipv4_addr_octet_is_null" "$ip" )";;
- 3) f_dialog_msgbox "$( printf \
- "$msg_ipv4_addr_octet_exceeds_max_value" "$ip" )";;
- 4) f_dialog_msgbox "$( printf \
- "$msg_ipv4_addr_octet_missing_or_extra" "$ip" )";;
+
+ # Produce an appropriate error message if necessary.
+ [ $retval -eq $SUCCESS ] || f_dialog_iperror $retval "$ip"
+
+ return $retval
+}
+
+# f_validate_ipaddr6 $ipv6_addr
+#
+# Returns zero if the given argument (an IPv6 address) is of the proper format.
+#
+# The return status for invalid IP address is one of:
+# 1 One or more individual segments within the IP address
+# (separated by colons) contains one or more invalid characters.
+# Segments must contain only combinations of the characters 0-9,
+# A-F, or a-f.
+# 2 Too many/incorrect null segments. A single null segment is
+# allowed within the IP address (separated by colons) but not
+# allowed at the beginning or end (unless a double-null segment;
+# i.e., "::*" or "*::").
+# 3 One or more individual segments within the IP address
+# (separated by colons) exceeds the length of 4 hex-digits.
+# 4 The IP address entered has either too few (less than 3), too
+# many (more than 8), or not enough segments, separated by
+# colons.
+# 5* The IPv4 address at the end of the IPv6 address is invalid.
+# * When there is an error with the dotted-quad IPv4 address at the
+# end of the IPv6 address, the return value of 5 is OR'd with a
+# bit-shifted (<< 4) return of f_validate_ipaddr.
+#
+f_validate_ipaddr6()
+{
+ local ip="$1"
+
+ ( # Operate within a sub-shell to protect the parent environment
+
+ IFS=":" # Split on `colon'
+ set -- $ip:
+
+ # Return error if too many or too few segments
+ # Using 9 as max in case of leading or trailing null spanner
+ [ $# -gt 9 -o $# -lt 3 ] && exit 4
+
+ h="[0-9A-Fa-f]"
+ nulls=0
+ nsegments=$#
+ contains_ipv4_segment=
+
+ while [ $# -gt 0 ]; do
+
+ segment="${1%:}"
+ shift
+
+ #
+ # Return error if this segment makes one null too-many.
+ # A single null segment is allowed anywhere in the
+ # middle as well as double null segments are allowed at
+ # the beginning or end (but not both).
+ #
+ if [ ! "$segment" ]; then
+ nulls=$(( $nulls + 1 ))
+ if [ $nulls -eq 3 ]; then
+ # Only valid syntax for 3 nulls is `::'
+ [ "$ip" = "::" ] || exit 2
+ elif [ $nulls -eq 2 ]; then
+ # Only valid if begins/ends with `::'
+ case "$ip" in
+ ::*|*::) : fall thru ;;
+ *) exit 2
+ esac
+ fi
+ continue
+ fi
+
+ #
+ # Return error if not a valid hexadecimal short
+ #
+ case "$segment" in
+ $h|$h$h|$h$h$h|$h$h$h$h)
+ : valid segment of 1-4 hexadecimal digits
+ ;;
+ *[!0-9A-Fa-f]*)
+ # Segment contains at least one invalid char
+
+ # Return error immediately if not last segment
+ [ $# -eq 0 ] || exit 1
+
+ # Otherwise, check for legacy IPv4 notation
+ case "$segment" in
+ *[!0-9.]*)
+ # Segment contains at least one invalid
+ # character even for an IPv4 address
+ exit 1
+ esac
+
+ # Return error if not enough segments
+ if [ $nulls -eq 0 ]; then
+ [ $nsegments -eq 7 ] || exit 4
+ fi
+
+ contains_ipv4_segment=1
+
+ # Validate the IPv4 address
+ f_validate_ipaddr "$segment" ||
+ exit $(( 5 | $? << 4 ))
+ ;;
+ *)
+ # Segment characters are all valid but too many
+ exit 3
+ esac
+
+ done
+
+ if [ $nulls -eq 1 ]; then
+ # Single null segment cannot be at beginning/end
+ case "$ip" in
+ :*|*:) exit 2
+ esac
+ fi
+
+ #
+ # A legacy IPv4 address can span the last two 16-bit segments,
+ # reducing the amount of maximum allowable segments by-one.
+ #
+ maxsegments=8
+ if [ "$contains_ipv4_segment" ]; then
+ maxsegments=7
+ fi
+
+ case $nulls in
+ # Return error if missing segments with no null spanner
+ 0) [ $nsegments -eq $maxsegments ] || exit 4 ;;
+ # Return error if null spanner with too many segments
+ 1) [ $nsegments -le $maxsegments ] || exit 4 ;;
+ # Return error if leading/trailing `::' with too many segments
+ 2) [ $nsegments -le $(( $maxsegments + 1 )) ] || exit 4 ;;
+ esac
+
+ exit $SUCCESS
+ )
+}
+
+# f_dialog_ip6error $error $ipv6_addr
+#
+# Display a msgbox with the appropriate error message for an error returned by
+# the f_validate_ipaddr6 function above.
+#
+f_dialog_ip6error()
+{
+ local error="$1" ip="$2"
+
+ [ ${error:-0} -ne 0 ] || return $SUCCESS
+
+ case "$error" in
+ 1) f_show_msg "$msg_ipv6_addr_segment_contains_invalid_chars" "$ip";;
+ 2) f_show_msg "$msg_ipv6_addr_too_many_null_segments" "$ip";;
+ 3) f_show_msg "$msg_ipv6_addr_segment_contains_too_many_chars" "$ip";;
+ 4) f_show_msg "$msg_ipv6_addr_too_few_or_extra_segments" "$ip";;
+ *)
+ if [ $(( $error & 0xF )) -eq 5 ]; then
+ # IPv4 at the end of IPv6 address is invalid
+ f_dialog_iperror $(( $error >> 4 )) "$ip"
+ fi
esac
+}
+
+# f_dialog_validate_ipaddr6 $ipv6_addr
+#
+# Returns zero if the given argument (an IPv6 address) is of the proper format.
+#
+# If the IP address is determined to be invalid, the appropriate error will be
+# displayed using the f_dialog_ip6error function above.
+#
+f_dialog_validate_ipaddr6()
+{
+ local ip="$1"
+
+ f_validate_ipaddr6 "$ip"
+ local retval=$?
+
+ # Produce an appropriate error message if necessary.
+ [ $retval -eq $SUCCESS ] || f_dialog_ip6error $retval "$ip"
return $retval
}
@@ -148,48 +346,26 @@ f_dialog_input_ipaddr()
if f_nfs_mounted && ! f_jailed; then
local setting="$( printf "$msg_current_ipaddr" \
"$interface" "$_ipaddr" )"
- local message="$( printf "$msg_nfs_mounts_may_cause_hang" \
- "$setting" )"
- f_dialog_msgbox "$message"
+ f_show_msg "$msg_nfs_mounts_may_cause_hang" "$setting"
return $FAILURE
fi
local msg="$( printf "$msg_please_enter_new_ip_addr" "$interface" )"
- local hline="$hline_num_punc_tab_enter"
- local size="$( f_dialog_inputbox_size \
- "$DIALOG_TITLE" \
- "$DIALOG_BACKTITLE" \
- "$msg" \
- "$_ipaddr" \
- "$hline" )"
#
# Loop until the user provides taint-free input.
#
+ local retval
while :; do
- local dialog_inputbox
- dialog_inputbox=$( eval $DIALOG \
- --title \"\$DIALOG_TITLE\" \
- --backtitle \"\$DIALOG_BACKTITLE\" \
- --hline \"\$hline\" \
- --ok-label \"\$msg_ok\" \
- --cancel-label \"\$msg_cancel\" \
- --inputbox \"\$msg\" $size \
- \"\$_ipaddr\" \
- 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
- )
-
- local retval=$?
- setvar DIALOG_INPUTBOX_$$ "$dialog_inputbox"
- _input=$( f_dialog_inputstr )
-
#
# Return error status if:
- # - User has not made any changes to the given value
# - User has either pressed ESC or chosen Cancel/No
+ # - User has not made any changes to the given value
#
+ _input=$( f_dialog_input "$msg" "$_ipaddr" \
+ "$hline_num_punc_tab_enter"
+ ) || return
[ "$_ipaddr" = "$_input" ] && return $FAILURE
- [ $retval -eq $SUCCESS ] || return $retval
# Return success if NULL value was entered
[ "$_input" ] || return $SUCCESS
@@ -236,4 +412,8 @@ f_dialog_input_ipaddr()
return $SUCCESS
}
+############################################################ MAIN
+
+f_dprintf "%s: Successfully loaded." networking/ipaddr.subr
+
fi # ! $_NETWORKING_IPADDR_SUBR
diff --git a/usr.sbin/bsdconfig/networking/share/media.subr b/usr.sbin/bsdconfig/networking/share/media.subr
index e7da98a..eed8364a 100644
--- a/usr.sbin/bsdconfig/networking/share/media.subr
+++ b/usr.sbin/bsdconfig/networking/share/media.subr
@@ -30,6 +30,7 @@ if [ ! "$_NETWORKING_MEDIA_SUBR" ]; then _NETWORKING_MEDIA_SUBR=1
BSDCFG_SHARE="/usr/share/bsdconfig"
. $BSDCFG_SHARE/common.subr || exit 1
+f_dprintf "%s: loading includes..." networking/media.subr
f_include $BSDCFG_SHARE/dialog.subr
f_include $BSDCFG_SHARE/strings.subr
f_include $BSDCFG_SHARE/sysrc.subr
@@ -114,9 +115,7 @@ f_dialog_input_options()
if f_nfs_mounted && ! f_jailed; then
local setting="$( printf "$msg_current_options" \
"$interface" "$options" )"
- local message="$( printf "$msg_nfs_mounts_may_cause_hang" \
- "$setting" )"
- f_dialog_msgbox "$message"
+ f_show_msg "$msg_nfs_mounts_may_cause_hang" "$setting"
return $FAILURE
fi
@@ -165,9 +164,7 @@ f_dialog_menu_media_options()
if f_nfs_mounted && ! f_jailed; then
local setting="$( printf "$msg_current_options" \
"$interface" "$_options" )"
- local message="$( printf "$msg_nfs_mounts_may_cause_hang" \
- "$setting" )"
- f_dialog_msgbox "$message"
+ f_show_msg "$msg_nfs_mounts_may_cause_hang" "$setting"
return $FAILURE
fi
@@ -238,4 +235,8 @@ f_dialog_menu_media_options()
return $retval
}
+############################################################ MAIN
+
+f_dprintf "%s: Successfully loaded." networking/media.subr
+
fi # ! $_NETWORKING_MEDIA_SUBR
diff --git a/usr.sbin/bsdconfig/networking/share/netmask.subr b/usr.sbin/bsdconfig/networking/share/netmask.subr
index 2f2b378..3a8665d 100644
--- a/usr.sbin/bsdconfig/networking/share/netmask.subr
+++ b/usr.sbin/bsdconfig/networking/share/netmask.subr
@@ -30,6 +30,7 @@ if [ ! "$_NETWORKING_NETMASK_SUBR" ]; then _NETWORKING_NETMASK_SUBR=1
BSDCFG_SHARE="/usr/share/bsdconfig"
. $BSDCFG_SHARE/common.subr || exit 1
+f_dprintf "%s: loading includes..." networking/netmask.subr
f_include $BSDCFG_SHARE/dialog.subr
f_include $BSDCFG_SHARE/strings.subr
f_include $BSDCFG_SHARE/networking/common.subr
@@ -69,7 +70,7 @@ f_ifconfig_netmask()
echo $netmask
}
-# f_dialog_validate_netmask $netmask
+# f_validate_netmask $netmask
#
# Returns zero if the given argument (a subnet mask) is of the proper format.
#
@@ -85,10 +86,7 @@ f_ifconfig_netmask()
# invalid integer (only 0,128,192,224,240,248,252,254,255 are
# valid integers).
#
-# If the subnet mask is determined to be invalid, the appropriate error will be
-# displayed using the f_dialog_msgbox function.
-#
-f_dialog_validate_netmask()
+f_validate_netmask()
{
local mask="$1"
@@ -121,23 +119,44 @@ f_dialog_validate_netmask()
[ $nfields -eq 4 ] || exit 4
)
+}
- #
- # Produce an appropriate error message if necessary.
- #
- local retval=$?
- case $retval in
- 1) f_dialog_msgbox "$( printf \
- "$msg_ipv4_mask_field_contains_invalid_chars" "$mask" )";;
- 2) f_dialog_msgbox "$( printf \
- "$msg_ipv4_mask_field_is_null" "$mask" )";;
- 3) f_dialog_msgbox "$( printf \
- "$msg_ipv4_mask_field_exceeds_max_value" "$mask" )";;
- 4) f_dialog_msgbox "$( printf \
- "$msg_ipv4_mask_field_missing_or_extra" "$mask" )";;
- 5) f_dialog_msgbox "$( printf \
- "$msg_ipv4_mask_field_invalid_value" "$mask" )";;
+# f_dialog_maskerror $error $netmask
+#
+# Display a msgbox with the appropriate error message for an error returned by
+# the f_validate_netmask function.
+#
+f_dialog_maskerror()
+{
+ local error="$1" netmask="$2"
+
+ [ ${error:-0} -ne 0 ] || return $SUCCESS
+
+ case "$error" in
+ 1) f_show_msg "$msg_ipv4_mask_field_contains_invalid_chars" "$mask";;
+ 2) f_show_msg "$msg_ipv4_mask_field_is_null" "$mask";;
+ 3) f_show_msg "$msg_ipv4_mask_field_exceeds_max_value" "$mask";;
+ 4) f_show_msg "$msg_ipv4_mask_field_missing_or_extra" "$mask";;
+ 5) f_show_msg "$msg_ipv4_mask_field_invalid_value" "$mask";;
esac
+}
+
+# f_dialog_validate_netmask $netmask
+#
+# Returns zero if the given argument (a subnet mask) is of the proper format.
+#
+# If the subnet mask is determined to be invalid, the appropriate error will be
+# displayed using the f_dialog_maskerror function above.
+#
+f_dialog_validate_netmask()
+{
+ local netmask="$1"
+
+ f_validate_netmask "$netmask"
+ local retval=$?
+
+ # Produce an appropriate error message if necessary.
+ [ $retval -eq $SUCCESS ] || f_dialog_maskerror $retval "$netmask"
return $retval
}
@@ -158,48 +177,24 @@ f_dialog_input_netmask()
if f_nfs_mounted && ! f_jailed; then
local setting="$( printf "$msg_current_subnet" \
"$interface" "$_netmask" )"
- local message="$( printf "$msg_nfs_mounts_may_cause_hang" \
- "$setting" )"
- f_dialog_msgbox "$message"
+ f_show_msg "$msg_nfs_mounts_may_cause_hang" "$setting"
return $FAILURE
fi
- local msg="$( printf "$msg_please_enter_subnet_mask" "$interface" )"
- local hline="$hline_num_punc_tab_enter"
- local size="$( f_dialog_inputbox_size \
- "$DIALOG_TITLE" \
- "$DIALOG_BACKTITLE" \
- "$msg" \
- "$_netmask" \
- "$hline" )"
-
#
# Loop until the user provides taint-free input.
#
+ local msg="$( printf "$msg_please_enter_subnet_mask" "$interface" )"
while :; do
- local dialog_inputbox
- dialog_inputbox=$( eval $DIALOG \
- --title \"\$DIALOG_TITLE\" \
- --backtitle \"\$DIALOG_BACKTITLE\" \
- --hline \"\$hline\" \
- --ok-label \"\$msg_ok\" \
- --cancel-label \"\$msg_cancel\" \
- --inputbox \"\$msg\" $size \
- \"\$_netmask\" \
- 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
- )
-
- local retval=$?
- setvar DIALOG_INPUTBOX_$$ "$dialog_inputbox"
- _input=$( f_dialog_inputstr )
-
#
# Return error status if:
- # - User has not made any changes to the given value
# - User has either pressed ESC or chosen Cancel/No
+ # - User has not made any changes to the given value
#
+ _input=$( f_dialog_input "$msg" "$_netmask" \
+ "$hline_num_punc_tab_enter"
+ ) || return
[ "$_netmask" = "$_input" ] && return $FAILURE
- [ $retval -eq $SUCCESS ] || return $retval
# Return success if NULL value was entered
[ "$_input" ] || return $SUCCESS
@@ -215,4 +210,8 @@ f_dialog_input_netmask()
netmask="$_netmask"
}
+############################################################ MAIN
+
+f_dprintf "%s: Successfully loaded." networking/netmask.subr
+
fi # ! $_NETWORKING_NETMASK_SUBR
diff --git a/usr.sbin/bsdconfig/networking/share/resolv.subr b/usr.sbin/bsdconfig/networking/share/resolv.subr
index 154748b..7d9af3b 100644
--- a/usr.sbin/bsdconfig/networking/share/resolv.subr
+++ b/usr.sbin/bsdconfig/networking/share/resolv.subr
@@ -30,6 +30,7 @@ if [ ! "$_NETWORKING_RESOLV_SUBR" ]; then _NETWORKING_RESOLV_SUBR=1
BSDCFG_SHARE="/usr/share/bsdconfig"
. $BSDCFG_SHARE/common.subr || exit 1
+f_dprintf "%s: loading includes..." networking/resolv.subr
f_include $BSDCFG_SHARE/dialog.subr
f_include $BSDCFG_SHARE/strings.subr
f_include $BSDCFG_SHARE/networking/common.subr
@@ -329,36 +330,13 @@ f_dialog_input_nameserver()
msg="$msg_please_enter_nameserver"
fi
- local hline="$hline_num_punc_tab_enter"
- local size="$( f_dialog_inputbox_size \
- "$DIALOG_TITLE" \
- "$DIALOG_BACKTITLE" \
- "$msg" \
- "$ns" \
- "$hline" )"
-
#
# Loop until the user provides taint-free input.
#
while :; do
-
- local dialog_inputbox
- dialog_inputbox=$( eval $DIALOG \
- --title \"\$DIALOG_TITLE\" \
- --backtitle \"\$DIALOG_BACKTITLE\" \
- --hline \"\$hline\" \
- --ok-label \"\$msg_ok\" \
- --cancel-label \"\$msg_cancel\" \
- --inputbox \"\$msg\" $size \
- \"\$ns\" \
- 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
- )
-
- local retval=$?
- setvar DIALOG_INPUTBOX_$$ "$dialog_inputbox"
- new_ns=$( f_dialog_inputstr )
-
- [ $retval -eq $SUCCESS ] || return $retval
+ new_ns=$( f_dialog_input "$msg" "$ns" \
+ "$hline_num_punc_tab_enter"
+ ) || return
# Take only the first "word" of the user's input
new_ns="${new_ns%%[$IFS]*}"
@@ -369,7 +347,6 @@ f_dialog_input_nameserver()
# Update prompt to allow user to re-edit previous entry
ns="$new_ns"
-
done
#
@@ -512,4 +489,8 @@ f_dialog_menu_nameservers()
done
}
+############################################################ MAIN
+
+f_dprintf "%s: Successfully loaded." networking/resolv.subr
+
fi # ! $_NETWORKING_RESOLV_SUBR
diff --git a/usr.sbin/bsdconfig/networking/share/routing.subr b/usr.sbin/bsdconfig/networking/share/routing.subr
index b8ccdcd..4207ab2 100644
--- a/usr.sbin/bsdconfig/networking/share/routing.subr
+++ b/usr.sbin/bsdconfig/networking/share/routing.subr
@@ -30,6 +30,7 @@ if [ ! "$_NETWORKING_ROUTING_SUBR" ]; then _NETWORKING_ROUTING_SUBR=1
BSDCFG_SHARE="/usr/share/bsdconfig"
. $BSDCFG_SHARE/common.subr || exit 1
+f_dprintf "%s: loading includes..." networking/routing.subr
f_include $BSDCFG_SHARE/sysrc.subr
f_include $BSDCFG_SHARE/dialog.subr
f_include $BSDCFG_SHARE/networking/common.subr
@@ -91,41 +92,20 @@ f_dialog_input_defaultrouter()
if f_nfs_mounted && ! f_jailed; then
local setting="$( printf "$msg_current_default_router" \
"$defaultrouter" )"
- local message="$( printf "$msg_nfs_mounts_may_cause_hang" \
- "$setting" )"
- f_dialog_msgbox "$message"
+ f_show_msg "$msg_nfs_mounts_may_cause_hang" "$setting"
return $FAILURE
fi
- local msg="$msg_please_enter_default_router"
- local hline="$hline_num_punc_tab_enter"
- local size="$( f_dialog_inputbox_size \
- "$DIALOG_TITLE" \
- "$DIALOG_BACKTITLE" \
- "$msg" \
- "$defaultrouter" \
- "$hline" )"
-
#
# Loop until the user provides taint-free input.
#
+ local retval
while :; do
- local dialog_inputbox
- dialog_inputbox=$( eval $DIALOG \
- --title \"\$DIALOG_TITLE\" \
- --backtitle \"\$DIALOG_BACKTITLE\" \
- --hline \"\$hline\" \
- --ok-label \"\$msg_ok\" \
- --cancel-label \"\$msg_cancel\" \
- --inputbox \"\$msg\" $size \
- \"\$defaultrouter\" \
- 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
+ defaultrouter=$( f_dialog_input \
+ "$msg_please_enter_default_router" \
+ "$defaultrouter" "$hline_num_punc_tab_enter"
)
-
- local retval=$?
- setvar DIALOG_INPUTBOX_$$ "$dialog_inputbox"
- defaultrouter=$( f_dialog_inputstr )
-
+ retval=$?
[ "$defaultrouter" ] || return $SUCCESS
[ $retval -eq $SUCCESS ] || return $retval
@@ -149,11 +129,8 @@ f_dialog_input_defaultrouter()
#
if [ "$( f_route_get_default )" != "$defaultrouter" ]; then
f_dialog_clear
- f_dialog_yesno "$(
- printf "$msg_activate_default_router" \
- "$( f_route_get_default )" "$defaultrouter"
- )"
-
+ f_yesno "$msg_activate_default_router" \
+ "$( f_route_get_default )" "$defaultrouter"
if [ $? -eq $SUCCESS ]; then
local err
@@ -168,4 +145,8 @@ f_dialog_input_defaultrouter()
fi
}
+############################################################ MAIN
+
+f_dprintf "%s: Successfully loaded." networking/routing.subr
+
fi # ! $_NETWORKING_ROUTING_SUBR
diff --git a/usr.sbin/bsdconfig/password/password b/usr.sbin/bsdconfig/password/password
index 7a439e6..50d963d 100755
--- a/usr.sbin/bsdconfig/password/password
+++ b/usr.sbin/bsdconfig/password/password
@@ -30,13 +30,14 @@
BSDCFG_SHARE="/usr/share/bsdconfig"
. $BSDCFG_SHARE/common.subr || exit 1
+f_dprintf "%s: loading includes..." "$0"
f_include $BSDCFG_SHARE/dialog.subr
f_include $BSDCFG_SHARE/mustberoot.subr
f_include $BSDCFG_SHARE/password/password.subr
BSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="040.password"
-ipgm=$( f_index_menu_selection $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
+ipgm=$( f_index_menusel_keyword $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
[ $? -eq $SUCCESS -a "$ipgm" ] && pgm="$ipgm"
############################################################ CONFIGURATION
@@ -64,7 +65,6 @@ shift $(( $OPTIND - 1 ))
#
# Initialize
#
-f_dialog_init
f_dialog_title "$msg_root_password"
f_dialog_backtitle "${ipgm:+bsdconfig }$pgm"
f_mustberoot_init
@@ -72,7 +72,7 @@ f_mustberoot_init
if f_dialog_input_password; then
err=$( echo "$pw_password" | pw usermod $USER_ROOT -h 0 2>&1 ) ||
f_die $? "%s" "$err"
- f_show_msg "$msg_password_changed"
+ f_dialog_msgbox "$msg_password_changed"
fi
return $SUCCESS
diff --git a/usr.sbin/bsdconfig/password/share/password.subr b/usr.sbin/bsdconfig/password/share/password.subr
index a0d1c50..f4ebaab9 100644
--- a/usr.sbin/bsdconfig/password/share/password.subr
+++ b/usr.sbin/bsdconfig/password/share/password.subr
@@ -30,6 +30,7 @@ if [ ! "$_PASSWORD_PASSWORD_SUBR" ]; then _PASSWORD_PASSWORD_SUBR=1
BSDCFG_SHARE="/usr/share/bsdconfig"
. $BSDCFG_SHARE/common.subr || exit 1
+f_dprintf "%s: loading includes..." password/password.subr
f_include $BSDCFG_SHARE/dialog.subr
BSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="040.password"
@@ -107,13 +108,13 @@ f_dialog_input_password()
# Check for NULL entry
if ! [ "$_password1" -o "$_password2" ]; then
- f_show_msg "$msg_password_is_empty"
+ f_dialog_msgbox "$msg_password_is_empty"
continue
fi
# Check for password mismatch
if [ "$_password1" != "$_password2" ]; then
- f_show_msg "$msg_passwords_do_not_match"
+ f_dialog_msgbox "$msg_passwords_do_not_match"
continue
fi
@@ -124,4 +125,8 @@ f_dialog_input_password()
return $SUCCESS
}
+############################################################ MAIN
+
+f_dprintf "%s: Successfully loaded." password/password.subr
+
fi # ! $_PASSWORD_PASSWORD_SUBR
diff --git a/usr.sbin/bsdconfig/security/kern_securelevel b/usr.sbin/bsdconfig/security/kern_securelevel
index 1bb89d8..42594c6 100755
--- a/usr.sbin/bsdconfig/security/kern_securelevel
+++ b/usr.sbin/bsdconfig/security/kern_securelevel
@@ -30,6 +30,7 @@
BSDCFG_SHARE="/usr/share/bsdconfig"
. $BSDCFG_SHARE/common.subr || exit 1
+f_dprintf "%s: loading includes..." "$0"
f_include $BSDCFG_SHARE/dialog.subr
f_include $BSDCFG_SHARE/mustberoot.subr
f_include $BSDCFG_SHARE/sysrc.subr
@@ -39,7 +40,7 @@ f_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
SECURELEVEL_HELPFILE=$BSDCFG_LIBE/$APP_DIR/include/securelevel.hlp
-ipgm=$( f_index_menu_selection $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
+ipgm=$( f_index_menusel_keyword $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
[ $? -eq $SUCCESS -a "$ipgm" ] && pgm="$ipgm"
############################################################ FUNCTIONS
@@ -77,6 +78,7 @@ dialog_menu_main()
--ok-label \"\$msg_ok\" \
--cancel-label \"\$msg_cancel\" \
--help-button \
+ --help-label \"\$msg_help\" \
${USE_XDIALOG:+--help \"\"} \
--menu \"\$prompt\" $size \
$menu_list \
@@ -105,7 +107,6 @@ shift $(( $OPTIND - 1 ))
#
# Initialize
#
-f_dialog_init
f_dialog_title "$msg_securelevels_menu_title"
f_dialog_backtitle "${ipgm:+bsdconfig }$pgm"
f_mustberoot_init
diff --git a/usr.sbin/bsdconfig/security/security b/usr.sbin/bsdconfig/security/security
index 3dd71c5..f503e51 100755
--- a/usr.sbin/bsdconfig/security/security
+++ b/usr.sbin/bsdconfig/security/security
@@ -30,6 +30,7 @@
BSDCFG_SHARE="/usr/share/bsdconfig"
. $BSDCFG_SHARE/common.subr || exit 1
+f_dprintf "%s: loading includes..." "$0"
f_include $BSDCFG_SHARE/dialog.subr
f_include $BSDCFG_SHARE/mustberoot.subr
f_include $BSDCFG_SHARE/sysrc.subr
@@ -37,7 +38,7 @@ f_include $BSDCFG_SHARE/sysrc.subr
BSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="130.security"
f_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
-ipgm=$( f_index_menu_selection $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
+ipgm=$( f_index_menusel_keyword $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
[ $? -eq $SUCCESS -a "$ipgm" ] && pgm="$ipgm"
############################################################ FUNCTIONS
@@ -120,7 +121,6 @@ shift $(( $OPTIND - 1 ))
#
# Initialize
#
-f_dialog_init
f_dialog_title "$msg_system_security_options_menu"
f_dialog_backtitle "${ipgm:+bsdconfig }$pgm"
f_mustberoot_init
diff --git a/usr.sbin/bsdconfig/share/Makefile b/usr.sbin/bsdconfig/share/Makefile
index 7f299ec..cd56dfc 100644
--- a/usr.sbin/bsdconfig/share/Makefile
+++ b/usr.sbin/bsdconfig/share/Makefile
@@ -3,7 +3,8 @@
NO_OBJ=
FILESDIR= ${SHAREDIR}/bsdconfig
-FILES= common.subr dialog.subr mustberoot.subr strings.subr sysrc.subr
+FILES= common.subr dialog.subr mustberoot.subr script.subr \
+ strings.subr sysrc.subr variable.subr
beforeinstall:
mkdir -p ${DESTDIR}${FILESDIR}
diff --git a/usr.sbin/bsdconfig/share/common.subr b/usr.sbin/bsdconfig/share/common.subr
index 46fdc6f..c66a54f 100644
--- a/usr.sbin/bsdconfig/share/common.subr
+++ b/usr.sbin/bsdconfig/share/common.subr
@@ -27,6 +27,15 @@ if [ ! "$_COMMON_SUBR" ]; then _COMMON_SUBR=1
#
# $FreeBSD$
#
+############################################################ CONFIGURATION
+
+#
+# Default file descriptors to link to stdout/stderr for passthru allowing
+# redirection within a sub-shell to bypass directly to the terminal.
+#
+: ${TERMINAL_STDOUT_PASSTHRU:=3}}
+: ${TERMINAL_STDERR_PASSTHRU:=4}}
+
############################################################ GLOBALS
#
@@ -46,15 +55,35 @@ ARGV="$@"
SUCCESS=0
FAILURE=1
+#
+# Operating environment details
+#
+export UNAME_S="$(uname -s)" # Operating System (i.e. FreeBSD)
+export UNAME_P="$(uname -p)" # Processor Architecture (i.e. i386)
+export UNAME_R="$(uname -r)" # Release Level (i.e. X.Y-RELEASE)
+
############################################################ FUNCTIONS
+# f_dprintf $fmt [ $opts ... ]
+#
+# Sensible debug function. Override in ~/.bsdconfigrc if desired.
+# See /usr/share/examples/bsdconfig/bsdconfigrc for example.
#
-# This is an empty function by default, to use it, copy
-# /usr/share/examples/bsdconfig/bsdconfigrc to $HOME/.bsdconfigrc
+# If $debug is set and non-NULL, prints DEBUG info using printf(1) syntax:
+# + To $debugFile, if set and non-NULL
+# + To standard output if $debugFile is either NULL or unset
+# + To both if $debugFile begins with a single plus-sign (`+')
#
f_dprintf()
{
- : this page intentionally left blank
+ [ "$debug" ] || return $SUCCESS
+ local fmt="$1"; shift
+ case "$debugFile" in ""|+*)
+ printf "DEBUG: $fmt${fmt:+\n}" "$@" >&${TERMINAL_STDOUT_PASSTHRU:-1}
+ esac
+ [ "${debugFile#+}" ] &&
+ printf "DEBUG: $fmt${fmt:+\n}" "$@" >> "${debugFile#+}"
+ return $SUCCESS
}
# f_err $fmt [ $opts ... ]
@@ -63,7 +92,7 @@ f_dprintf()
#
f_err()
{
- printf "$@" >&2
+ printf "$@" >&${TERMINAL_STDERR_PASSTHRU:-2}
}
# f_quietly $command [ $arguments ... ]
@@ -85,6 +114,30 @@ f_have()
f_quietly type "$@"
}
+# f_getvar $var_to_get [$var_to_set]
+#
+# Utility function designed to go along with the already-builtin setvar.
+# Allows clean variable name indirection without forking or sub-shells.
+#
+# Returns error status if the requested variable ($var_to_get) is not set.
+#
+# If $var_to_set is missing or NULL, the value of $var_to_get is printed to
+# standard output for capturing in a sub-shell (which is less-recommended
+# because of performance degredation; for example, when called in a loop).
+#
+f_getvar()
+{
+ local var_to_get="$1" var_to_set="$2"
+ [ "$var_to_set" ] || local value
+ eval ${var_to_set:-value}=\"\${$var_to_get}\"
+ eval [ \"\${$var_to_get+set}\" ]
+ local retval=$?
+ eval f_dprintf '"f_getvar: var=[%s] value=[%s] r=%u"' \
+ \"\$var_to_get\" \"\$${var_to_set:-value}\" \$retval
+ [ "$var_to_set" ] || { [ "$value" ] && echo "$value"; }
+ return $retval
+}
+
# f_die [ $status [ $fmt [ $opts ... ]]]
#
# Abruptly terminate due to an error optionally displaying a message in a
@@ -119,6 +172,27 @@ f_interrupt()
f_die
}
+# f_show_info $fmt [ $opts ... ]
+#
+# Display a message in a dialog infobox using printf(1) syntax.
+#
+f_show_info()
+{
+ local msg
+ msg=$( printf "$@" )
+
+ #
+ # Use f_dialog_infobox from dialog.subr if possible, otherwise fall
+ # back to dialog(1) (without options, making it obvious when using
+ # un-aided system dialog).
+ #
+ if f_have f_dialog_info; then
+ f_dialog_info "$msg"
+ else
+ dialog --infobox "$msg" 0 0
+ fi
+}
+
# f_show_msg $fmt [ $opts ... ]
#
# Display a message in a dialog box using printf(1) syntax.
@@ -140,6 +214,50 @@ f_show_msg()
fi
}
+
+# f_yesno $fmt [ $opts ... ]
+#
+# Display a message in a dialog yes/no box using printf(1) syntax.
+#
+f_yesno()
+{
+ local msg
+ msg=$( printf "$@" )
+
+ #
+ # Use f_dialog_yesno from dialog.subr if possible, otherwise fall
+ # back to dialog(1) (without options, making it obvious when using
+ # un-aided system dialog).
+ #
+ if f_have f_dialog_yesno; then
+ f_dialog_yesno "$msg"
+ else
+ dialog --yesno "$msg" 0 0
+ fi
+}
+
+# f_noyes $fmt [ $opts ... ]
+#
+# Display a message in a dialog yes/no box using printf(1) syntax.
+# NOTE: THis is just like the f_yesno function except "No" is default.
+#
+f_noyes()
+{
+ local msg
+ msg=$( printf "$@" )
+
+ #
+ # Use f_dialog_noyes from dialog.subr if possible, otherwise fall
+ # back to dialog(1) (without options, making it obvious when using
+ # un-aided system dialog).
+ #
+ if f_have f_dialog_noyes; then
+ f_dialog_noyes "$msg"
+ else
+ dialog --defaultno --yesno "$msg" 0 0
+ fi
+}
+
# f_show_help $file
#
# Display a language help-file. Automatically takes $LANG and $LC_ALL into
@@ -181,6 +299,7 @@ f_show_help()
f_include()
{
local file="$1"
+ f_dprintf "f_include: file=[%s]" "$file"
. "$file" || exit $?
}
@@ -202,7 +321,7 @@ f_include_lang()
local file="$1"
local lang="${LANG:-$LC_ALL}"
- f_dprintf "lang=[$lang]"
+ f_dprintf "f_include_lang: file=[%s] lang=[%s]" "$file" "$lang"
if [ -f "$file.$lang" ]; then
. "$file.$lang" || exit $?
else
@@ -247,7 +366,7 @@ f_usage()
local file="$1"
local lang="${LANG:-$LC_ALL}"
- f_dprintf "lang=[$lang]"
+ f_dprintf "f_usage: file=[%s] lang=[%s]" "$file" "$lang"
shift 1 # file
@@ -271,50 +390,188 @@ f_usage()
exit $FAILURE
}
-# f_index_menu_selection $file $pgm
+# f_index_file $keyword
+#
+# Process all INDEX files known to bsdconfig and return the path to first file
+# containing a menu_selection line with a keyword portion matching $keyword.
+#
+# If $LANG or $LC_ALL (in order of preference, respectively) is set,
+# "INDEX.encoding" files will be searched first.
+#
+# If no file is found, error status is returned along with the NULL string.
+#
+# This function is a two-parter. Below is the awk(1) portion of the function,
+# afterward is the sh(1) function which utilizes the below awk script.
+#
+f_index_file_awk='
+# Variables that should be defined on the invocation line:
+# -v keyword="keyword"
+BEGIN { found = 0 }
+( $0 ~ "^menu_selection=\"" keyword "\\|" ) {
+ print FILENAME
+ found++
+ exit
+}
+END { exit ! found }
+'
+f_index_file()
+{
+ local keyword="$1"
+ local lang="${LANG:-$LC_ALL}"
+
+ f_dprintf "f_index_file: keyword=[%s] lang=[%s]" "$keyword" "$lang"
+
+ if [ "$lang" ]; then
+ awk -v keyword="$keyword" "$f_index_file_awk" \
+ $BSDCFG_LIBE${BSDCFG_LIBE:+/}*/INDEX.$lang &&
+ return
+ # No match, fall-thru to non-i18n sources
+ fi
+ awk -v keyword="$keyword" "$f_index_file_awk" \
+ $BSDCFG_LIBE${BSDCFG_LIBE:+/}*/INDEX
+}
+
+# f_index_menusel_keyword $indexfile $pgm
+#
+# Process $indexfile and return only the keyword portion of the menu_selection
+# line with a command portion matching $pgm.
#
-# Process $file looking for $menu_selection values that correspond to $pgm.
# This function is for internationalization (i18n) mapping of the on-disk
# scriptname ($pgm) into the localized language (given language-specific
-# $file). If $LANG or $LC_ALL (in orderder of preference, respectively) is set,
-# ".encoding" will automatically be appended as a suffix to the provided $file
-# pathname.
+# $indexfile). If $LANG or $LC_ALL (in orderder of preference, respectively) is
+# set, ".encoding" will automatically be appended as a suffix to the provided
+# $indexfile pathname.
#
-# If, within $file, multiple $menu_selection values map to $pgm, only the first
-# one will be returned. If no mapping can be made, the NULL string is returned.
+# If, within $indexfile, multiple $menu_selection values map to $pgm, only the
+# first one will be returned. If no mapping can be made, the NULL string is
+# returned.
#
-# If $file does not exist, error status is returned along with the NULL string.
+# If $indexfile does not exist, error status is returned with NULL.
#
# This function is a two-parter. Below is the awk(1) portion of the function,
# afterward is the sh(1) function which utilizes the below awk script.
#
-f_index_menusel_awk='
+f_index_menusel_keyword_awk='
# Variables that should be defined on the invocation line:
# -v pgm="program_name"
#
-( $0 ~ "^menu_selection=.*\\|" pgm "\"" ) {
- sub(/\|.*/, "")
- sub(/^menu_selection="/, "")
- print
- exit
+BEGIN {
+ prefix = "menu_selection=\""
+ plen = length(prefix)
+ found = 0
+}
+{
+ if (!match($0, "^" prefix ".*\\|.*\"")) next
+
+ keyword = command = substr($0, plen + 1, RLENGTH - plen - 1)
+ sub(/^.*\|/, "", command)
+ sub(/\|.*$/, "", keyword)
+
+ if ( command == pgm )
+ {
+ print keyword
+ found++
+ exit
+ }
}
+END { exit ! found }
'
-f_index_menu_selection()
+f_index_menusel_keyword()
{
- local file="$1" pgm="$2"
+ local indexfile="$1" pgm="$2"
local lang="${LANG:-$LC_ALL}"
- f_dprintf "lang=[$lang]"
+ f_dprintf "f_index_menusel_keyword: index=[%s] pgm=[%s] lang=[%s]" \
+ "$indexfile" "$pgm" "$lang"
+
+ if [ -f "$indexfile.$lang" ]; then
+ awk -v pgm="$pgm" \
+ "$f_index_menusel_keyword_awk" \
+ "$indexfile.$lang"
+ elif [ -f "$indexfile" ]; then
+ awk -v pgm="$pgm" \
+ "$f_index_menusel_keyword_awk" \
+ "$indexfile"
+ fi
+}
- if [ -f "$file.$lang" ]; then
- awk -v pgm="$pgm" "$f_index_menusel_awk" "$file.$lang" ||
- exit $FAILURE
- elif [ -f "$file" ]; then
- awk -v pgm="$pgm" "$f_index_menusel_awk" "$file" ||
- exit $FAILURE
+# f_index_menusel_command $indexfile $keyword
+#
+# Process $indexfile and return only the command portion of the menu_selection
+# line with a keyword portion matching $keyword.
+#
+# This function is for mapping [possibly international] keywords into the
+# command to be executed. If $LANG or $LC_ALL (order of preference) is set,
+# ".encoding" will automatically be appended as a suffix to the provided
+# $indexfile pathname.
+#
+# If, within $indexfile, multiple $menu_selection values map to $keyword, only
+# the first one will be returned. If no mapping can be made, the NULL string is
+# returned.
+#
+# If $indexfile doesn't exist, error status is returned with NULL.
+#
+# This function is a two-parter. Below is the awk(1) portion of the function,
+# afterward is the sh(1) function which utilizes the below awk script.
+#
+f_index_menusel_command_awk='
+# Variables that should be defined on the invocation line:
+# -v key="keyword"
+#
+BEGIN {
+ prefix = "menu_selection=\""
+ plen = length(prefix)
+ found = 0
+}
+{
+ if (!match($0, "^" prefix ".*\\|.*\"")) next
+
+ keyword = command = substr($0, plen + 1, RLENGTH - plen - 1)
+ sub(/^.*\|/, "", command)
+ sub(/\|.*$/, "", keyword)
+
+ if ( keyword == key )
+ {
+ print command
+ found++
+ exit
+ }
+}
+END { exit ! found }
+'
+f_index_menusel_command()
+{
+ local indexfile="$1" keyword="$2" command
+ local lang="${LANG:-$LC_ALL}"
+
+ f_dprintf "f_index_menusel_command: index=[%s] key=[%s] lang=[%s]" \
+ "$indexfile" "$keyword" "$lang"
+
+ if [ -f "$indexfile.$lang" ]; then
+ command=$( awk -v key="$keyword" \
+ "$f_index_menusel_command_awk" \
+ "$indexfile.$lang" ) || return $FAILURE
+ elif [ -f "$indexfile" ]; then
+ command=$( awk -v key="$keyword" \
+ "$f_index_menusel_command_awk" \
+ "$indexfile" ) || return $FAILURE
else
return $FAILURE
fi
+
+ #
+ # If the command pathname is not fully qualified fix-up/force to be
+ # relative to the $indexfile directory.
+ #
+ case "$command" in
+ /*) : already fully qualified ;;
+ *)
+ local indexdir="${indexfile%/*}"
+ [ "$indexdir" != "$indexfile" ] || indexdir="."
+ command="$indexdir/$command"
+ esac
+
+ echo "$command"
}
############################################################ MAIN
@@ -327,4 +584,42 @@ trap 'f_die' SIGTERM SIGPIPE SIGXCPU SIGXFSZ \
SIGFPE SIGTRAP SIGABRT SIGSEGV
trap '' SIGALRM SIGPROF SIGUSR1 SIGUSR2 SIGHUP SIGVTALRM
+#
+# Clone terminal stdout/stderr so we can redirect to it from within sub-shells
+#
+eval exec $TERMINAL_STDOUT_PASSTHRU\>\&1
+eval exec $TERMINAL_STDERR_PASSTHRU\>\&2
+
+#
+# Make debugging persistant if set
+#
+[ "$debug" ] && export debug
+
+#
+# Truncate the debug file upon initialization (now). Note that we will trim a
+# leading plus (`+') from the value of debugFile to support persistant meaning
+# that f_dprintf() should print both to standard output and $debugFile (minus
+# the leading plus, of course).
+#
+_debug_file="${debugFile#+}"
+if [ "$_debug_file" ]; then
+ if ( umask 022 && :> "$_debug_file" ); then
+ f_dprintf "Successfully initialized debugFile \`%s'" \
+ "$_debug_file"
+ else
+ unset debugFile
+ f_dprintf "Unable to initialize debugFile \`%s'" \
+ "$_debug_file"
+ fi
+fi
+unset _debug_file
+
+#
+# Log our operating environment for debugging purposes
+#
+f_dprintf "UNAME_S=[%s] UNAME_P=[%s] UNAME_R=[%s]" \
+ "$UNAME_S" "$UNAME_P" "$UNAME_R"
+
+f_dprintf "%s: Successfully loaded." common.subr
+
fi # ! $_COMMON_SUBR
diff --git a/usr.sbin/bsdconfig/share/dialog.subr b/usr.sbin/bsdconfig/share/dialog.subr
index 353a8da..78286267 100644
--- a/usr.sbin/bsdconfig/share/dialog.subr
+++ b/usr.sbin/bsdconfig/share/dialog.subr
@@ -1,6 +1,6 @@
if [ ! "$_DIALOG_SUBR" ]; then _DIALOG_SUBR=1
#
-# Copyright (c) 2006-2012 Devin Teske
+# Copyright (c) 2006-2013 Devin Teske
# All Rights Reserved.
#
# Redistribution and use in source and binary forms, with or without
@@ -30,7 +30,9 @@ if [ ! "$_DIALOG_SUBR" ]; then _DIALOG_SUBR=1
BSDCFG_SHARE="/usr/share/bsdconfig"
. $BSDCFG_SHARE/common.subr || exit 1
+f_dprintf "%s: loading includes..." dialog.subr
f_include $BSDCFG_SHARE/strings.subr
+f_include $BSDCFG_SHARE/variable.subr
BSDCFG_LIBE="/usr/libexec/bsdconfig"
f_include_lang $BSDCFG_LIBE/include/messages.subr
@@ -42,7 +44,7 @@ f_include_lang $BSDCFG_LIBE/include/messages.subr
# execution of dialog from within a sub-shell (so-long as its standard output
# is explicitly redirected to this file descriptor).
#
-: ${DIALOG_TERMINAL_PASSTHRU_FD:=3}
+: ${DIALOG_TERMINAL_PASSTHRU_FD:=${TERMINAL_STDOUT_PASSTHRU:-3}}
############################################################ GLOBALS
@@ -72,11 +74,15 @@ unset XDIALOG_FORCE_AUTOSIZE
unset XDIALOG_INFOBOX_TIMEOUT
#
-# Default behavior is to call f_dialog_init() automatically if not already
-# called manually by the time the first f_dialog_*() function is used.
+# Default behavior is to call f_dialog_init() automatically when loaded.
#
: ${DIALOG_SELF_INITIALIZE=1}
+#
+# Default terminal size (used if/when running without a controlling terminal)
+#
+: ${DEFAULT_TERMINAL_SIZE:=24 80}
+
############################################################ GENERIC FUNCTIONS
# f_dialog_title [$new_title]
@@ -93,7 +99,7 @@ f_dialog_title()
{
local new_title="$1"
- if [ "$new_title" ]; then
+ if [ "${1+set}" ]; then
if [ "$USE_XDIALOG" ]; then
_DIALOG_BACKTITLE="$DIALOG_BACKTITLE"
DIALOG_BACKTITLE="$new_title"
@@ -134,7 +140,7 @@ f_dialog_backtitle()
{
local new_backtitle="$1"
- if [ "$new_backtitle" ]; then
+ if [ "${1+set}" ]; then
if [ "$USE_XDIALOG" ]; then
_DIALOG_TITLE="$DIALOG_TITLE"
DIALOG_TITLE="$new_backtitle"
@@ -185,8 +191,6 @@ f_dialog_backtitle_restore()
#
f_dialog_infobox_size()
{
- [ "$DIALOG_SELF_INITIALIZE" ] && f_dialog_init
-
local title="$1" btitle="$2" prompt="$3" hline="$4" n=0
local min_width max_size
@@ -195,7 +199,8 @@ f_dialog_infobox_size()
max_size="$XDIALOG_MAXSIZE" # see CONFIGURATION
else
min_width=24
- max_size=$( stty size ) # usually "24 80"
+ max_size=$( stty size 2> /dev/null ) # usually "24 80"
+ : ${max_size:=$DEFAULT_TERMINAL_SIZE}
fi
local max_height="${max_size%%[$IFS]*}"
@@ -283,7 +288,7 @@ f_dialog_infobox_size()
n=$( echo "$btitle" | f_number_of_lines )
height=$(( $height + $n + 2 ))
fi
-
+
# Make sure height is less than maximum screen size
[ $height -le $max_height ] || height=$max_height
@@ -308,8 +313,6 @@ f_dialog_infobox_size()
#
f_dialog_buttonbox_size()
{
- [ "$DIALOG_SELF_INITIALIZE" ] && f_dialog_init
-
local title="$1" btitle="$2" prompt="$3" hline="$4"
local size="$( f_dialog_infobox_size \
"$title" "$btitle" "$prompt" "$hline" )"
@@ -329,7 +332,8 @@ f_dialog_buttonbox_size()
if [ "$USE_XDIALOG" ]; then
max_size="$XDIALOG_MAXSIZE" # see CONFIGURATION
else
- max_size=$( stty size ) # usually "24 80"
+ max_size=$( stty size 2> /dev/null ) # usually "24 80"
+ : ${max_size:=$DEFAULT_TERMINAL_SIZE}
fi
local max_height="${max_size%%[$IFS]*}"
[ $height -le $max_height ] || height=$max_height
@@ -355,8 +359,6 @@ f_dialog_buttonbox_size()
#
f_dialog_inputbox_size()
{
- [ "$DIALOG_SELF_INITIALIZE" ] && f_dialog_init
-
local title="$1" btitle="$2" prompt="$3" init="$4" hline="$5" n
local size="$( f_dialog_buttonbox_size \
"$title" "$btitle" "$prompt" "$hline" )"
@@ -369,7 +371,8 @@ f_dialog_inputbox_size()
max_size="$XDIALOG_MAXSIZE" # see CONFIGURATION
else
min_width=24
- max_size=$( stty size ) # usually "24 80"
+ max_size=$( stty size 2> /dev/null ) # usually "24 80"
+ : ${max_size:=$DEFAULT_TERMINAL_SIZE}
fi
local max_height="${max_size%%[$IFS]*}"
local max_width="${max_size##*[$IFS]}"
@@ -422,8 +425,6 @@ f_dialog_inputbox_size()
#
f_xdialog_2inputsbox_size()
{
- [ "$DIALOG_SELF_INITIALIZE" ] && f_dialog_init
-
local title="$1" btitle="$2" prompt="$3"
local label1="$4" init1="$5" label2="$6" init2="$7" n
local size="$( f_dialog_inputbox_size \
@@ -480,6 +481,7 @@ f_xdialog_2inputsbox_size()
# Add height for a second inputbox
height=$(( $height + 2 ))
+ [ $height -le $max_height ] || height=$max_height
#
# Bump width for second initial text (if not already at maximum width).
@@ -519,8 +521,6 @@ f_xdialog_2inputsbox_size()
#
f_dialog_menu_size()
{
- [ "$DIALOG_SELF_INITIALIZE" ] && f_dialog_init
-
local title="$1" btitle="$2" prompt="$3" hline="$4" n=0
local min_width min_rows max_size
@@ -531,7 +531,8 @@ f_dialog_menu_size()
else
min_width=24
min_rows=0
- max_size=$( stty size ) # usually "24 80"
+ max_size=$( stty size 2> /dev/null ) # usually "24 80"
+ : ${max_size:=$DEFAULT_TERMINAL_SIZE}
fi
local max_width="${max_size##*[$IFS]}"
@@ -603,8 +604,6 @@ f_dialog_menu_size()
#
f_dialog_menu_with_help_size()
{
- [ "$DIALOG_SELF_INITIALIZE" ] && f_dialog_init
-
local title="$1" btitle="$2" prompt="$3" hline="$4" n=0
local min_width min_rows max_size
@@ -615,7 +614,8 @@ f_dialog_menu_with_help_size()
else
min_width=24
min_rows=0
- max_size=$( stty size ) # usually "24 80"
+ max_size=$( stty size 2> /dev/null ) # usually "24 80"
+ : ${max_size:=$DEFAULT_TERMINAL_SIZE}
fi
local max_width="${max_size##*[$IFS]}"
@@ -704,8 +704,6 @@ f_dialog_menu_with_help_size()
#
f_dialog_radiolist_size()
{
- [ "$DIALOG_SELF_INITIALIZE" ] && f_dialog_init
-
local title="$1" btitle="$2" prompt="$3" hline="$4" n=0
local min_width min_rows max_size
@@ -716,7 +714,8 @@ f_dialog_radiolist_size()
else
min_width=24
min_rows=0
- max_size=$( stty size ) # usually "24 80"
+ max_size=$( stty size 2> /dev/null ) # usually "24 80"
+ : ${max_size:=$DEFAULT_TERMINAL_SIZE}
fi
local max_width="${max_size##*[$IFS]}"
@@ -788,8 +787,6 @@ f_dialog_radiolist_size()
#
f_dialog_calendar_size()
{
- [ "$DIALOG_SELF_INITIALIZE" ] && f_dialog_init
-
local title="$1" btitle="$2" prompt="$3" hline="$4" n
local size="$( f_dialog_infobox_size \
"$title" "$btitle" "$prompt" "$hline" )"
@@ -804,7 +801,8 @@ f_dialog_calendar_size()
else
min_height=0
min_width=40
- max_size=$( stty size ) # usually "24 80"
+ max_size=$( stty size 2> /dev/null ) # usually "24 80"
+ : ${max_size:=$DEFAULT_TERMINAL_SIZE}
fi
local max_height="${max_size%%[$IFS]*}"
local max_width="${max_size##*[$IFS]}"
@@ -869,8 +867,6 @@ f_dialog_calendar_size()
#
f_dialog_timebox_size()
{
- [ "$DIALOG_SELF_INITIALIZE" ] && f_dialog_init
-
local title="$1" btitle="$2" prompt="$3" hline="$4" n
local size="$( f_dialog_infobox_size \
"$title" "$btitle" "$prompt" "$hline" )"
@@ -884,7 +880,8 @@ f_dialog_timebox_size()
else
min_height=0
min_width=20
- max_size=$( stty size ) # usually "24 80"
+ max_size=$( stty size 2> /dev/null ) # usually "24 80"
+ : ${max_size:=$DEFAULT_TERMINAL_SIZE}
fi
local max_height="${max_size%%[$IFS]*}"
local max_width="${max_size##*[$IFS]}"
@@ -934,8 +931,6 @@ f_dialog_timebox_size()
#
f_dialog_clear()
{
- [ "$DIALOG_SELF_INITIALIZE" ] && f_dialog_init
-
$DIALOG --clear
}
@@ -944,12 +939,10 @@ f_dialog_clear()
# f_dialog_info $info_text ...
#
# Throw up a dialog(1) infobox. The infobox remains until another dialog is
-# displayed or `dialog --clear' (or dialog_clear) is called.
+# displayed or `dialog --clear' (or f_dialog_clear) is called.
#
f_dialog_info()
{
- [ "$DIALOG_SELF_INITIALIZE" ] && f_dialog_init
-
local info_text="$*"
local size="$( f_dialog_infobox_size \
"$DIALOG_TITLE" \
@@ -972,8 +965,6 @@ f_dialog_info()
#
f_xdialog_info()
{
- [ "$DIALOG_SELF_INITIALIZE" ] && f_dialog_init
-
local info_text="$*"
local size="$( f_dialog_infobox_size \
"$DIALOG_TITLE" \
@@ -1000,8 +991,6 @@ f_xdialog_info()
#
f_dialog_msgbox()
{
- [ "$DIALOG_SELF_INITIALIZE" ] && f_dialog_init
-
local msg_text="$*"
local size="$( f_dialog_buttonbox_size \
"$DIALOG_TITLE" \
@@ -1028,8 +1017,6 @@ f_dialog_msgbox()
#
f_dialog_textbox()
{
- [ "$DIALOG_SELF_INITIALIZE" ] && f_dialog_init
-
local file="$1"
local contents retval size
@@ -1070,10 +1057,11 @@ f_dialog_textbox()
#
f_dialog_yesno()
{
- [ "$DIALOG_SELF_INITIALIZE" ] && f_dialog_init
-
local msg_text="$*"
local hline="$hline_arrows_tab_enter"
+
+ f_interactive || return 0 # If non-interactive, return YES all the time
+
local size="$( f_dialog_buttonbox_size \
"$DIALOG_TITLE" \
"$DIALOG_BACKTITLE" \
@@ -1112,10 +1100,11 @@ f_dialog_yesno()
#
f_dialog_noyes()
{
- [ "$DIALOG_SELF_INITIALIZE" ] && f_dialog_init
-
local msg_text="$*"
local hline="$hline_arrows_tab_enter"
+
+ f_interactive || return 1 # If non-interactive, return NO all the time
+
local size="$( f_dialog_buttonbox_size \
"$DIALOG_TITLE" \
"$DIALOG_BACKTITLE" \
@@ -1148,7 +1137,7 @@ f_dialog_noyes()
# f_dialog_inputstr
#
# Obtain the inputstr entered by the user from the most recently displayed
-# dialog(1) inputbox and clean up any temporary files.
+# dialog(1) inputbox and clean up any temporary files/variables.
#
f_dialog_inputstr()
{
@@ -1171,12 +1160,58 @@ f_dialog_inputstr()
return $SUCCESS
}
+# f_dialog_input $prompt [$init [$hline]]
+#
+# Prompt the user with a dialog(1) inputbox to enter some value. The inputbox
+# remains until the the user presses ENTER or ESC, or otherwise ends the
+# editing session, by selecting `Cancel' for example.
+#
+# If the user presses ENTER, the exit status is zero (success), otherwise if
+# the user presses ESC the exit status is 255, or if the user chose Cancel, the
+# exit status is instead 1.
+#
+# NOTE: The hline should correspond to the type of data you want from the user.
+# NOTE: Should not be used to edit multiline values.
+#
+f_dialog_input()
+{
+ local prompt="$1" init="$2" hline="$3"
+ local size="$( f_dialog_inputbox_size \
+ "$DIALOG_TITLE" \
+ "$DIALOG_BACKTITLE" \
+ "$prompt" \
+ "$init" \
+ "$hline" )"
+
+ local opterm="--"
+ [ "$USE_XDIALOG" ] && opterm=
+
+ local dialog_input
+ dialog_input=$(
+ eval $DIALOG \
+ --title \"\$DIALOG_TITLE\" \
+ --backtitle \"\$DIALOG_BACKTITLE\" \
+ --hline \"\$hline\" \
+ --ok-label \"\$msg_ok\" \
+ --cancel-label \"\$msg_cancel\" \
+ --inputbox \"\$prompt\" $size \
+ $opterm \"\$init\" \
+ 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
+ )
+ local retval=$?
+
+ setvar DIALOG_INPUTBOX_$$ "$dialog_input"
+ f_dialog_inputstr
+
+ return $retval
+}
+
############################################################ MENU FUNCTIONS
# f_dialog_menutag
#
# Obtain the menutag chosen by the user from the most recently displayed
-# dialog(1) menu and clean up any temporary files.
+# dialog(1) menu and clean up any temporary files/variables.
#
f_dialog_menutag()
{
@@ -1460,6 +1495,22 @@ f_dialog_init()
DIALOG_BACKTITLE="$_DIALOG_TITLE"
unset _DIALOG_TITLE
fi
+
+ f_dprintf "f_dialog_init: dialog(1) API initialized."
}
+############################################################ MAIN
+
+#
+# Self-initialize unless requested otherwise
+#
+f_dprintf "%s: DIALOG_SELF_INITIALIZE=[%s]" \
+ dialog.subr "$DIALOG_SELF_INITIALIZE"
+case "$DIALOG_SELF_INITIALIZE" in
+""|0|[Nn][Oo]|[Oo][Ff][Ff]|[Ff][Aa][Ll][Ss][Ee]) : do nothing ;;
+*) f_dialog_init
+esac
+
+f_dprintf "%s: Successfully loaded." dialog.subr
+
fi # ! $_DIALOG_SUBR
diff --git a/usr.sbin/bsdconfig/share/mustberoot.subr b/usr.sbin/bsdconfig/share/mustberoot.subr
index fd37c64..2ead592 100644
--- a/usr.sbin/bsdconfig/share/mustberoot.subr
+++ b/usr.sbin/bsdconfig/share/mustberoot.subr
@@ -30,6 +30,7 @@ if [ ! "$_MUSTBEROOT_SUBR" ]; then _MUSTBEROOT_SUBR=1
BSDCFG_SHARE="/usr/share/bsdconfig"
. $BSDCFG_SHARE/common.subr || exit 1
+f_dprintf "%s: loading includes..." mustberoot.subr
f_include $BSDCFG_SHARE/dialog.subr
BSDCFG_LIBE="/usr/libexec/bsdconfig"
@@ -127,7 +128,7 @@ f_become_root_via_sudo()
1) # Always try sudo(8) when run as $user
local err
if ! err=$( touch "$checkpath" 2>&1 ); then
- f_show_msg "%s" "$err"
+ f_dialog_msgbox "$err"
else
f_show_msg "$msg_created_path" "$checkpath"
fi
@@ -277,7 +278,7 @@ f_authenticate_some_user()
#
# Secure-mode has been requested.
#
-
+
[ "$USE_XDIALOG" ] || f_die 1 "$msg_secure_mode_requires_x11"
[ "$(id -u)" = "0" ] || f_die 1 "$msg_secure_mode_requires_root"
@@ -338,16 +339,14 @@ f_authenticate_some_user()
case "$user" in
root|toor)
nfailures=$(( $nfailures + 1 ))
- f_dialog_msgbox "$( printf \
- "$msg_user_disallowed" "$user" )"
+ f_show_msg "$msg_user_disallowed" "$user"
continue
esac
fi
if ! f_quietly id "$user"; then
nfailures=$(( $nfailures + 1 ))
if [ "$SECURE_DIVULGE_UNKNOWN_USER" ]; then
- f_dialog_msgbox "$( printf \
- "$msg_unknown_user" "$user" )"
+ f_show_msg "$msg_unknown_user" "$user"
elif [ $nfailures -lt $PASSWD_TRIES ]; then
f_dialog_info "$msg_sorry_try_again"
sleep 1
@@ -421,4 +420,8 @@ f_mustberoot_init()
fi
}
+############################################################ MAIN
+
+f_dprintf "%s: Successfully loaded." mustberoot.subr
+
fi # ! $_MUSTBEROOT_SUBR
diff --git a/usr.sbin/bsdconfig/share/script.subr b/usr.sbin/bsdconfig/share/script.subr
new file mode 100644
index 0000000..84d1cab
--- /dev/null
+++ b/usr.sbin/bsdconfig/share/script.subr
@@ -0,0 +1,154 @@
+if [ ! "$_SCRIPT_SUBR" ]; then _SCRIPT_SUBR=1
+#
+# Copyright (c) 2012 Devin Teske
+# All Rights Reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+# 1. Redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer.
+# 2. Redistributions in binary form must reproduce the above copyright
+# notice, this list of conditions and the following disclaimer in the
+# documentation and/or other materials provided with the distribution.
+#
+# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, THE
+# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+# DAMAGES (INLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+# SUCH DAMAGE.
+#
+# $FreeBSD$
+#
+############################################################ INCLUDES
+
+BSDCFG_SHARE="/usr/share/bsdconfig"
+. $BSDCFG_SHARE/common.subr || exit 1
+f_dprintf "%s: loading includes..." script.subr
+f_include $BSDCFG_SHARE/variable.subr
+
+############################################################ GLOBALS
+
+RESWORDS=
+
+############################################################ FUNCTIONS
+
+# f_resword_new $resword $function
+#
+# Create a new `reserved' word for scripting purposes. Reswords call pre-
+# defined functions but differ from those functions in the following ways:
+#
+# + Reswords do not take arguments but instead get all their data from
+# the environment variable namespace.
+# + Unless noError is set (must be non-NULL), if calling the resword
+# results in failure, the application will terminate prematurely.
+# + noError is unset after each/every resword is called.
+#
+# Reswords should not be used in bsdconfig itself (hence the name `reserved
+# word') but instead only in scripts loaded through f_script_load()).
+#
+f_resword_new()
+{
+ local resword="$1" func="$2"
+ [ "$resword" ] || return $FAILURE
+ f_dprintf "script.subr: New resWord %s -> %s" "$resword" "$func"
+ eval $resword\(\){ f_dispatch $func $resword\; }
+ RESWORDS="$RESWORDS${RESWORDS:+ }$resword"
+}
+
+# f_dispatch $func [$resword]
+#
+# Wrapper function used by `reserved words' (reswords) to call other functions.
+# If $noError is set and non-NULL, a failure result from $func is ignored,
+# otherwise the application is prematurely terminated using f_die().
+#
+# NOTE: $noError is unset after every call.
+#
+f_dispatch()
+{
+ local func="$1" resword="${2:-$1}"
+ f_dprintf "f_dispatch: calling resword \`%s'" "$resword"
+ eval $func
+ local retval=$? _ignore_this_error
+ f_getvar $VAR_NO_ERROR _ignore_this_error
+ [ $retval -eq $SUCCESS ] ||
+ [ "$_ignore_this_error" ] || f_die $retval \
+ "$msg_command_failed_rest_of_script_aborted" "$resword"
+ unset $VAR_NO_ERROR
+}
+
+# f_script_load [$file]
+#
+# Load a script (usually filled with reswords). If $file is missing or NULL,
+# use one of the following instead (in order):
+#
+# $configFile
+# install.cfg
+# /stand/install.fg
+# /tmp/install.cfg
+#
+# Unknown/unregistered reswords will generate sh(1) syntax errors but not cause
+# premature termination.
+#
+# Returns success if a script was loaded and itself returned success.
+#
+f_script_load()
+{
+ local script="$1" config_file retval=$SUCCESS
+
+ f_dprintf "f_script_load: script=[%s]" "$script"
+ if [ ! "$script" ]; then
+ f_getvar $VAR_CONFIG_FILE config_file
+ for script in \
+ $config_file \
+ install.cfg \
+ /stand/install.cfg \
+ /tmp/install.cfg \
+ ; do
+ [ -e "$script" ] && break
+ done
+ fi
+
+ local old_interactive=
+ f_getvar $VAR_NONINTERACTIVE old_interactive # save a copy
+
+ # Hint to others that we're running from a script, should they care
+ setvar $VAR_NONINTERACTIVE yes
+
+ if [ "$script" = "-" ]; then
+ f_dprintf "f_script_load: Loading script from stdin"
+ eval "$( cat )"
+ retval=$?
+ else
+ f_dprintf "f_script_load: Loading script \`%s'" "$script"
+ if [ ! -e "$script" ]; then
+ f_show_msg "$msg_unable_to_open" "$script"
+ return $FAILURE
+ fi
+ . "$script"
+ retval=$?
+ fi
+
+ [ "$old_interactive" ] &&
+ setvar $VAR_NONINTERACTIVE "$old_interactive"
+
+ return $retval
+}
+
+############################################################ MAIN
+
+#
+# Reserved words meant for scripting
+#
+f_resword_new dumpVariables f_dump_variables
+f_resword_new loadConfig f_script_load
+
+f_dprintf "%s: Successfully loaded." script.subr
+
+fi # ! $_SCRIPT_SUBR
diff --git a/usr.sbin/bsdconfig/share/strings.subr b/usr.sbin/bsdconfig/share/strings.subr
index d11a9a2..6fe0f32 100644
--- a/usr.sbin/bsdconfig/share/strings.subr
+++ b/usr.sbin/bsdconfig/share/strings.subr
@@ -101,4 +101,6 @@ f_isinteger()
( : $((0/$arg)) ) > /dev/null 2>&1
}
+f_dprintf "%s: Successfully loaded." strings.subr
+
fi # ! $_STRINGS_SUBR
diff --git a/usr.sbin/bsdconfig/share/sysrc.subr b/usr.sbin/bsdconfig/share/sysrc.subr
index 1ee70e6..36a550e 100644
--- a/usr.sbin/bsdconfig/share/sysrc.subr
+++ b/usr.sbin/bsdconfig/share/sysrc.subr
@@ -32,7 +32,10 @@ BSDCFG_SHARE="/usr/share/bsdconfig"
[ "$_COMMON_SUBR" ] || . $BSDCFG_SHARE/common.subr || exit 1
BSDCFG_LIBE="/usr/libexec/bsdconfig"
-[ ! "$_SYSRC_JAILED" ] && f_include_lang $BSDCFG_LIBE/include/messages.subr
+if [ ! "$_SYSRC_JAILED" ]; then
+ f_dprintf "%s: loading includes..." sysrc.subr
+ f_include_lang $BSDCFG_LIBE/include/messages.subr
+fi
############################################################ CONFIGURATION
@@ -623,4 +626,8 @@ f_sysrc_delete()
done
}
+############################################################ MAIN
+
+f_dprintf "%s: Successfully loaded." sysrc.subr
+
fi # ! $_SYSRC_SUBR
diff --git a/usr.sbin/bsdconfig/share/variable.subr b/usr.sbin/bsdconfig/share/variable.subr
new file mode 100644
index 0000000..235f6dc
--- /dev/null
+++ b/usr.sbin/bsdconfig/share/variable.subr
@@ -0,0 +1,185 @@
+if [ ! "$_VARIABLE_SUBR" ]; then _VARIABLE_SUBR=1
+#
+# Copyright (c) 2012 Devin Teske
+# All Rights Reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+# 1. Redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer.
+# 2. Redistributions in binary form must reproduce the above copyright
+# notice, this list of conditions and the following disclaimer in the
+# documentation and/or other materials provided with the distribution.
+#
+# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, THE
+# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+# DAMAGES (INLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+# SUCH DAMAGE.
+#
+# $FreeBSD$
+#
+############################################################ INCLUDES
+
+BSDCFG_SHARE="/usr/share/bsdconfig"
+. $BSDCFG_SHARE/common.subr || exit 1
+f_dprintf "%s: loading includes..." variable.subr
+f_include $BSDCFG_SHARE/dialog.subr
+
+############################################################ GLOBALS
+
+VARIABLES=
+
+#
+# Default behavior is to call f_variable_set_defaults() when loaded.
+#
+: ${VARIABLE_SELF_INITIALIZE=1}
+
+#
+# File to write when f_dump_variables() is called.
+#
+: ${VARIABLE_DUMPFILE:=/etc/bsdconfig.vars}
+
+############################################################ FUNCTIONS
+
+# f_variable_new $handle $variable
+#
+# Register a new variable named $variable with the given reference-handle
+# $handle. The environment variable $handle is set to $variable allowing you to
+# use the f_getvar() function (from common.subr) with $handle to get the value
+# of environment variable $variable. For example:
+#
+# f_variable_new VAR_ABC abc
+#
+# allows the later indirection:
+#
+# f_getvar $VAR_ABC
+#
+# to return the value of environment variable `abc'. Variables registered in
+# this manner are recorded in the $VARIABLES environment variable for later
+# allowing dynamic enumeration of so-called `registered/advertised' variables.
+#
+f_variable_new()
+{
+ local handle="$1" variable="$2"
+ [ "$handle" ] || return $FAILURE
+ f_dprintf "variable.subr: New variable %s -> %s" "$handle" "$variable"
+ setvar $handle $variable
+ VARIABLES="$VARIABLES${VARIABLES:+ }$handle"
+}
+
+# f_variable_get_value $var [ $fmt [ $opts ... ] ]
+#
+# Unless nonInteractive is set, prompt the user with a given value (pre-filled
+# with the value of $var) and give them the chance to change the value.
+#
+# Unlike f_getvar() (from common.subr) which can return a variable to the
+# caller on standard output, this function has no [meaningful] output.
+#
+# Returns success unless $var is either NULL or missing.
+#
+f_variable_get_value()
+{
+ local var="$1" cp
+
+ [ "$var" ] || return $FAILURE
+
+ if ! { f_getvar $var cp && ! f_interactive; }; then
+ shift 1 # var
+ cp=$( f_dialog_input "$( printf "$@" )" "$cp" ) &&
+ setvar $var "$cp"
+ fi
+
+ return $SUCCESS
+}
+
+# f_variable_set_defaults
+#
+# Installs sensible defaults for registered/advertised variables.
+#
+f_variable_set_defaults()
+{
+ #
+ # Initialize various user-edittable values to their defaults
+ #
+ setvar $VAR_RELNAME "$UNAME_R"
+
+ f_dprintf "f_variable_set_defaults: Defaults initialized."
+}
+
+# f_dump_variables
+#
+# Dump a list of registered/advertised variables and their respective values to
+# $VARIABLE_DUMPFILE. Returns success unless the file couldn't be written. If
+# an error occurs, it is displayed using f_dialog_msgbox() (from dialog.subr).
+#
+f_dump_variables()
+{
+ local err sanitize_awk="{ gsub(/'/, \"'\\\\''\"); print }"
+ if ! err=$(
+ ( for handle in $VARIABLES; do
+ f_getvar $handle var || continue
+ f_getvar $var value || continue
+ value=$( echo "$value" | awk "$sanitize_awk" )
+ printf "%s='%s'\n" "$var" "$value"
+ done > "$VARIABLE_DUMPFILE" ) 2>&1
+ ); then
+ f_dialog_msgbox "$err"
+ return $FAILURE
+ fi
+}
+
+# f_debugging
+#
+# Are we in debug mode? Returns success if extra DEBUG information has been
+# requested (by setting $debug to non-NULL), otherwise false.
+#
+f_debugging()
+{
+ local value
+ f_getvar $VAR_DEBUG value && [ "$value" ]
+}
+
+# f_interactive()
+#
+# Are we running interactively? Return error if $nonInteractive is set and non-
+# NULL, otherwise return success.
+#
+f_interactive()
+{
+ local value
+ ! f_getvar $VAR_NONINTERACTIVE value || [ ! "$value" ]
+}
+
+############################################################ MAIN
+
+#
+# Variables that can be tweaked from config files
+#
+f_variable_new VAR_CONFIG_FILE configFile
+f_variable_new VAR_DEBUG debug
+f_variable_new VAR_DEBUG_FILE debugFile
+f_variable_new VAR_NO_ERROR noError
+f_variable_new VAR_NONINTERACTIVE nonInteractive
+f_variable_new VAR_RELNAME releaseName
+
+#
+# Self-initialize unless requested otherwise
+#
+f_dprintf "%s: VARIABLE_SELF_INITIALIZE=[%s]" \
+ variable.subr "$VARIABLE_SELF_INITIALIZE"
+case "$VARIABLE_SELF_INITIALIZE" in
+""|0|[Nn][Oo]|[Oo][Ff][Ff]|[Ff][Aa][Ll][Ss][Ee]) : do nothing ;;
+*) f_variable_set_defaults
+esac
+
+f_dprintf "%s: Successfully loaded." variable.subr
+
+fi # ! $_VARIABLE_SUBR
diff --git a/usr.sbin/bsdconfig/startup/misc b/usr.sbin/bsdconfig/startup/misc
index 3879c57..37bb80f 100755
--- a/usr.sbin/bsdconfig/startup/misc
+++ b/usr.sbin/bsdconfig/startup/misc
@@ -30,6 +30,7 @@
BSDCFG_SHARE="/usr/share/bsdconfig"
. $BSDCFG_SHARE/common.subr || exit 1
+f_dprintf "%s: loading includes..." "$0"
f_include $BSDCFG_SHARE/dialog.subr
f_include $BSDCFG_SHARE/mustberoot.subr
f_include $BSDCFG_SHARE/sysrc.subr
@@ -37,7 +38,7 @@ f_include $BSDCFG_SHARE/sysrc.subr
BSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="140.startup"
f_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
-ipgm=$( f_index_menu_selection $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
+ipgm=$( f_index_menusel_keyword $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
[ $? -eq $SUCCESS -a "$ipgm" ] && pgm="$ipgm"
############################################################ GLOBALS
@@ -253,34 +254,11 @@ dialog_menu_main()
#
dialog_input_value()
{
- local hline="$hline_alnum_tab_enter"
- local size prompt="$1" _input="$2"
+ local prompt="$1" _input="$2"
f_dialog_title "$msg_value_required"
-
- size=$( f_dialog_inputbox_size \
- "$DIALOG_TITLE" \
- "$DIALOG_BACKTITLE" \
- "$prompt" \
- "$_input" \
- "$hline" )
-
- local dialog_inputbox
- dialog_inputbox=$( eval $DIALOG \
- --title \"\$DIALOG_TITLE\" \
- --backtitle \"\$DIALOG_BACKTITLE\" \
- --hline \"\$hline\" \
- --ok-label \"\$msg_ok\" \
- --cancel-label \"\$msg_cancel\" \
- --inputbox \"\$prompt\" $size \
- \"\$_input\" \
- 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
- )
-
- retval=$?
- setvar DIALOG_INPUTBOX_$$ "$dialog_inputbox"
- _input=$( f_dialog_inputstr )
-
+ _input=$( f_dialog_input "$prompt" "$_input" "$hline_alnum_tab_enter" )
+ local retval=$?
f_dialog_title_restore
# Return if user has either pressed ESC or chosen Cancel/No
@@ -308,7 +286,6 @@ shift $(( $OPTIND - 1 ))
#
# Initialize
#
-f_dialog_init
f_dialog_title "$msg_miscellaneous_startup_services"
f_dialog_backtitle "${ipgm:+bsdconfig }$pgm"
f_mustberoot_init
@@ -330,14 +307,14 @@ while :; do
?" [X] "*) err=$( f_sysrc_set apm_enable NO 2>&1 ) ;;
?" [ ] "*) err=$( f_sysrc_set apm_enable YES 2>&1 ) ;;
esac
- [ $? -eq $SUCCESS ] || f_show_msg "%s\n" "$err" ;;
+ [ $? -eq $SUCCESS ] || f_dialog_msgbox "$err\n" ;;
?" ["?"] $msg_startup_dirs")
dialog_input_value \
"$msg_startup_dirs_desc" \
"$( f_sysrc_get local_startup )"
if [ $? -eq $SUCCESS ]; then
err=$( f_sysrc_set local_startup "$value" ) ||
- f_show_msg "%s\n" "$err"
+ f_dialog_msgbox "$err\n"
fi
;;
?" ["?"] $msg_named")
@@ -345,14 +322,14 @@ while :; do
?" [X] "*) err=$( f_sysrc_set named_enable NO 2>&1 ) ;;
?" [ ] "*) err=$( f_sysrc_set named_enable YES 2>&1 ) ;;
esac
- [ $? -eq $SUCCESS ] || f_show_msg "%s\n" "$err" ;;
+ [ $? -eq $SUCCESS ] || f_dialog_msgbox "$err\n" ;;
?" ["?"] $msg_named_flags")
dialog_input_value \
"$msg_named_flags_desc" \
"$( f_sysrc_get named_flags )"
if [ $? -eq $SUCCESS ]; then
err=$( f_sysrc_set named_flags "$value" ) ||
- f_show_msg "%s\n" "$err"
+ f_dialog_msgbox "$err\n"
fi
;;
?" ["?"] $msg_nis_client")
@@ -362,14 +339,14 @@ while :; do
err=$( f_sysrc_set nis_client_enable YES 2>&1 ) \
&& err=$( f_sysrc_set rpcbind_enable YES 2>&1 ) ;;
esac
- [ $? -eq $SUCCESS ] || f_show_msg "%s\n" "$err" ;;
+ [ $? -eq $SUCCESS ] || f_dialog_msgbox "$err\n" ;;
?" ["?"] $msg_nis_domainname")
dialog_input_value \
"$msg_nis_domainname_desc" \
"$( f_sysrc_get nisdomainname )"
if [ $? -eq $SUCCESS ]; then
err=$( f_sysrc_set nisdomainname "$value" ) ||
- f_show_msg "%s\n" "$err"
+ f_dialog_msgbox "$err\n"
fi
;;
?" ["?"] $msg_nis_server")
@@ -379,31 +356,31 @@ while :; do
err=$( f_sysrc_set nis_server_enable YES 2>&1 ) \
&& err=$( f_sysrc_set rpcbind_enable YES 2>&1 ) ;;
esac
- [ $? -eq $SUCCESS ] || f_show_msg "%s\n" "$err" ;;
+ [ $? -eq $SUCCESS ] || f_dialog_msgbox "$err\n" ;;
?" ["?"] $msg_accounting")
case "$mtag" in
?" [X] "*) err=$( f_sysrc_set accounting_enable NO 2>&1 ) ;;
?" [ ] "*) err=$( f_sysrc_set accounting_enable YES 2>&1 ) ;;
esac
- [ $? -eq $SUCCESS ] || f_show_msg "%s\n" "$err" ;;
+ [ $? -eq $SUCCESS ] || f_dialog_msgbox "$err\n" ;;
?" ["?"] $msg_lpd")
case "$mtag" in
?" [X] "*) err=$( f_sysrc_set lpd_enable NO 2>&1 ) ;;
?" [ ] "*) err=$( f_sysrc_set lpd_enable YES 2>&1 ) ;;
esac
- [ $? -eq $SUCCESS ] || f_show_msg "%s\n" "$err" ;;
+ [ $? -eq $SUCCESS ] || f_dialog_msgbox "$err\n" ;;
?" ["?"] $msg_sco")
case "$mtag" in
?" [X] "*) err=$( f_sysrc_set ibcs2_enable NO 2>&1 ) ;;
?" [ ] "*) err=$( f_sysrc_set ibcs2_enable YES 2>&1 ) ;;
esac
- [ $? -eq $SUCCESS ] || f_show_msg "%s\n" "$err" ;;
+ [ $? -eq $SUCCESS ] || f_dialog_msgbox "$err\n" ;;
?" ["?"] $msg_svr4")
case "$mtag" in
?" [X] "*) err=$( f_sysrc_set svr4_enable NO 2>&1 ) ;;
?" [ ] "*) err=$( f_sysrc_set svr4_enable YES 2>&1 ) ;;
esac
- [ $? -eq $SUCCESS ] || f_show_msg "%s\n" "$err" ;;
+ [ $? -eq $SUCCESS ] || f_dialog_msgbox "$err\n" ;;
esac
done
diff --git a/usr.sbin/bsdconfig/startup/rcadd b/usr.sbin/bsdconfig/startup/rcadd
index 8f28840..3bea670 100755
--- a/usr.sbin/bsdconfig/startup/rcadd
+++ b/usr.sbin/bsdconfig/startup/rcadd
@@ -30,6 +30,7 @@
BSDCFG_SHARE="/usr/share/bsdconfig"
. $BSDCFG_SHARE/common.subr || exit 1
+f_dprintf "%s: loading includes..." "$0"
f_include $BSDCFG_SHARE/dialog.subr
f_include $BSDCFG_SHARE/mustberoot.subr
f_include $BSDCFG_SHARE/startup/rcconf.subr
@@ -37,7 +38,7 @@ f_include $BSDCFG_SHARE/startup/rcconf.subr
BSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="140.startup"
f_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
-ipgm=$( f_index_menu_selection $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
+ipgm=$( f_index_menusel_keyword $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
[ $? -eq $SUCCESS -a "$ipgm" ] && pgm="$ipgm"
############################################################ GLOBALS
@@ -106,7 +107,6 @@ shift $(( $OPTIND - 1 ))
#
# Initialize
#
-f_dialog_init
f_dialog_title "$msg_add_startup_directive"
f_dialog_backtitle "${ipgm:+bsdconfig }$pgm"
f_mustberoot_init
diff --git a/usr.sbin/bsdconfig/startup/rcconf b/usr.sbin/bsdconfig/startup/rcconf
index 4fb5859..d09874b 100755
--- a/usr.sbin/bsdconfig/startup/rcconf
+++ b/usr.sbin/bsdconfig/startup/rcconf
@@ -30,6 +30,7 @@
BSDCFG_SHARE="/usr/share/bsdconfig"
. $BSDCFG_SHARE/common.subr || exit 1
+f_dprintf "%s: loading includes..." "$0"
f_include $BSDCFG_SHARE/dialog.subr
f_include $BSDCFG_SHARE/mustberoot.subr
f_include $BSDCFG_SHARE/sysrc.subr
@@ -38,7 +39,7 @@ f_include $BSDCFG_SHARE/startup/rcconf.subr
BSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="140.startup"
f_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
-ipgm=$( f_index_menu_selection $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
+ipgm=$( f_index_menusel_keyword $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
[ $? -eq $SUCCESS -a "$ipgm" ] && pgm="$ipgm"
############################################################ GLOBALS
@@ -205,7 +206,6 @@ shift $(( $OPTIND - 1 ))
#
# Initialize
#
-f_dialog_init
f_dialog_title "$msg_view_edit_startup_configuration"
f_dialog_backtitle "${ipgm:+bsdconfig }$pgm"
f_mustberoot_init
diff --git a/usr.sbin/bsdconfig/startup/rcdelete b/usr.sbin/bsdconfig/startup/rcdelete
index b431d88..578b13e 100755
--- a/usr.sbin/bsdconfig/startup/rcdelete
+++ b/usr.sbin/bsdconfig/startup/rcdelete
@@ -30,6 +30,7 @@
BSDCFG_SHARE="/usr/share/bsdconfig"
. $BSDCFG_SHARE/common.subr || exit 1
+f_dprintf "%s: loading includes..." "$0"
f_include $BSDCFG_SHARE/dialog.subr
f_include $BSDCFG_SHARE/mustberoot.subr
f_include $BSDCFG_SHARE/sysrc.subr
@@ -38,7 +39,7 @@ f_include $BSDCFG_SHARE/startup/rcconf.subr
BSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="140.startup"
f_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
-ipgm=$( f_index_menu_selection $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
+ipgm=$( f_index_menusel_keyword $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
[ $? -eq $SUCCESS -a "$ipgm" ] && pgm="$ipgm"
############################################################ GLOBALS
@@ -209,9 +210,7 @@ dialog_menu_delete()
[ $# -ge 1 ] || return $FAILURE
if [ $# -eq 1 ]; then
- msg=$( printf "$msg_are_you_sure_you_want_to_delete" \
- "$delete_vars" )
- f_dialog_noyes "$msg"
+ f_noyes "$msg_are_you_sure_you_want_to_delete" "$delete_vars"
return $?
fi
@@ -280,7 +279,6 @@ shift $(( $OPTIND - 1 ))
#
# Initialize
#
-f_dialog_init
f_dialog_title "$msg_delete_startup_directives"
f_dialog_backtitle "${ipgm:+bsdconfig }$pgm"
f_mustberoot_init
@@ -320,7 +318,7 @@ while :; do
for var in $delete_vars; do
err=$( f_sysrc_delete $var 2>&1 )
if [ $? -ne $SUCCESS ]; then
- f_show_msg "%s\n" "$err"
+ f_dialog_msgbox "$err\n"
break
fi
done
diff --git a/usr.sbin/bsdconfig/startup/rcedit b/usr.sbin/bsdconfig/startup/rcedit
index c060fcb..0114368 100755
--- a/usr.sbin/bsdconfig/startup/rcedit
+++ b/usr.sbin/bsdconfig/startup/rcedit
@@ -30,6 +30,7 @@
BSDCFG_SHARE="/usr/share/bsdconfig"
. $BSDCFG_SHARE/common.subr || exit 1
+f_dprintf "%s: loading includes..." "$0"
f_include $BSDCFG_SHARE/dialog.subr
f_include $BSDCFG_SHARE/mustberoot.subr
f_include $BSDCFG_SHARE/startup/rcedit.subr
@@ -37,7 +38,7 @@ f_include $BSDCFG_SHARE/startup/rcedit.subr
BSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="140.startup"
f_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
-ipgm=$( f_index_menu_selection $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
+ipgm=$( f_index_menusel_keyword $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
[ $? -eq $SUCCESS -a "$ipgm" ] && pgm="$ipgm"
############################################################ MAIN
@@ -58,7 +59,6 @@ shift $(( $OPTIND - 1 ))
#
# Initialize
#
-f_dialog_init
f_dialog_backtitle "${ipgm:+bsdconfig }$pgm"
f_mustberoot_init
diff --git a/usr.sbin/bsdconfig/startup/rcvar b/usr.sbin/bsdconfig/startup/rcvar
index b7a5229..d7ced9e 100755
--- a/usr.sbin/bsdconfig/startup/rcvar
+++ b/usr.sbin/bsdconfig/startup/rcvar
@@ -30,6 +30,7 @@
BSDCFG_SHARE="/usr/share/bsdconfig"
. $BSDCFG_SHARE/common.subr || exit 1
+f_dprintf "%s: loading includes..." "$0"
f_include $BSDCFG_SHARE/dialog.subr
f_include $BSDCFG_SHARE/mustberoot.subr
f_include $BSDCFG_SHARE/sysrc.subr
@@ -38,7 +39,7 @@ f_include $BSDCFG_SHARE/startup/rcvar.subr
BSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="140.startup"
f_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
-ipgm=$( f_index_menu_selection $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
+ipgm=$( f_index_menusel_keyword $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
[ $? -eq $SUCCESS -a "$ipgm" ] && pgm="$ipgm"
############################################################ GLOBALS
@@ -167,7 +168,6 @@ shift $(( $OPTIND - 1 ))
#
# Initialize
#
-f_dialog_init
f_dialog_title "$msg_toggle_startup_services"
f_dialog_backtitle "${ipgm:+bsdconfig }$pgm"
f_mustberoot_init
@@ -194,7 +194,7 @@ while :; do
esac
err=$( f_sysrc_set "$rcvar" "$value" 2>&1 ) ||
- f_show_msg "$err"
+ f_dialog_msgbox "$err"
esac
done
diff --git a/usr.sbin/bsdconfig/startup/share/rcconf.subr b/usr.sbin/bsdconfig/startup/share/rcconf.subr
index 16cb290..12eafd9 100644
--- a/usr.sbin/bsdconfig/startup/share/rcconf.subr
+++ b/usr.sbin/bsdconfig/startup/share/rcconf.subr
@@ -30,6 +30,7 @@ if [ ! "$_STARTUP_RCCONF_SUBR" ]; then _STARTUP_RCCONF_SUBR=1
BSDCFG_SHARE="/usr/share/bsdconfig"
. $BSDCFG_SHARE/common.subr || exit 1
+f_dprintf "%s: loading includes..." startup/rcconf.subr
f_include $BSDCFG_SHARE/sysrc.subr
BSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="140.startup"
@@ -418,50 +419,27 @@ f_dialog_input_rclist()
#
f_dialog_input_rcvar()
{
- local msg="$msg_please_enter_rcvar_name"
- local hline="$hline_alnum_tab_enter"
-
#
# Loop until the user provides taint-free/valid input
#
- local size _input="$1"
+ local _input="$1"
while :; do
- size=$( f_dialog_inputbox_size \
- "$DIALOG_TITLE" \
- "$DIALOG_BACKTITLE" \
- "$msg" \
- "$_input" \
- "$hline" )
-
- local dialog_inputbox
- dialog_inputbox=$( eval $DIALOG \
- --title \"\$DIALOG_TITLE\" \
- --backtitle \"\$DIALOG_BACKTITLE\" \
- --hline \"\$hline\" \
- --ok-label \"\$msg_ok\" \
- --cancel-label \"\$msg_cancel\" \
- --inputbox \"\$msg\" $size \
- \"\$_input\" \
- 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
- )
-
- retval=$?
- setvar DIALOG_INPUTBOX_$$ "$dialog_inputbox"
- _input=$( f_dialog_inputstr )
# Return if user either pressed ESC or chosen Cancel/No
- [ $retval -eq $SUCCESS ] || return $retval
+ _input=$( f_dialog_input "$msg_please_enter_rcvar_name" \
+ "$_input" "$hline_alnum_tab_enter"
+ ) || return
# Check for invalid entry (1of2)
if ! echo "$_input" | grep -q "^[[:alpha:]_]"; then
- f_show_msg "$msg_rcvar_must_start_with"
+ f_dialog_msgbox "$msg_rcvar_must_start_with"
continue
fi
# Check for invalid entry (2of2)
if ! echo "$_input" | grep -q "^[[:alpha:]_][[:alnum:]_]*$"
then
- f_show_msg "$msg_rcvar_contains_invalid_chars"
+ f_dialog_msgbox "$msg_rcvar_contains_invalid_chars"
continue
fi
@@ -469,9 +447,13 @@ f_dialog_input_rcvar()
break
done
- f_dprintf "f_dialog_input_rcvar: rcvar->[$rcvar]"
+ f_dprintf "f_dialog_input_rcvar: rcvar->[%s]" "$rcvar"
return $SUCCESS
}
+############################################################ MAIN
+
+f_dprintf "%s: Successfully loaded." startup/rcconf.subr
+
fi # ! $_STARTUP_RCCONF_SUBR
diff --git a/usr.sbin/bsdconfig/startup/share/rcedit.subr b/usr.sbin/bsdconfig/startup/share/rcedit.subr
index 909fef2..cb4a411 100644
--- a/usr.sbin/bsdconfig/startup/share/rcedit.subr
+++ b/usr.sbin/bsdconfig/startup/share/rcedit.subr
@@ -30,6 +30,7 @@ if [ ! "$_STARTUP_RCEDIT_SUBR" ]; then _STARTUP_RCEDIT_SUBR=1
BSDCFG_SHARE="/usr/share/bsdconfig"
. $BSDCFG_SHARE/common.subr || exit 1
+f_dprintf "%s: loading includes..." startup/rcedit.subr
f_include $BSDCFG_SHARE/dialog.subr
f_include $BSDCFG_SHARE/sysrc.subr
@@ -53,8 +54,7 @@ f_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
#
f_dialog_rcedit()
{
- local msg size retval var="$1" _input
- local hline="$hline_alnum_punc_tab_enter"
+ local msg var="$1" _input
msg=$( printf "$msg_please_enter_a_new_value" \
"$var" "$( f_sysrc_get_default "$var" )" )
@@ -67,43 +67,22 @@ f_dialog_rcedit()
_input=$( f_sysrc_get "$var" )
fi
- size=$( f_dialog_inputbox_size \
- "$DIALOG_TITLE" \
- "$DIALOG_BACKTITLE" \
- "$msg" \
- "$_input" \
- "$hline" )
-
- local opterm="--"
- [ "$USE_XDIALOG" ] && opterm=
-
- local dialog_inputbox
- dialog_inputbox=$( eval $DIALOG \
- --title \"\$DIALOG_TITLE\" \
- --backtitle \"\$DIALOG_BACKTITLE\" \
- --hline \"\$hline\" \
- --ok-label \"\$msg_ok\" \
- --cancel-label \"\$msg_cancel\" \
- --inputbox \"\$msg\" $size \
- $opterm \"\$_input\" \
- 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
- )
-
- retval=$?
- setvar DIALOG_INPUTBOX_$$ "$dialog_inputbox"
- _input=$( f_dialog_inputstr )
-
# Return if user has either pressed ESC or chosen Cancel/No
- [ $retval -eq $SUCCESS ] || return $retval
+ _input=$( f_dialog_input "$msg" "$_input" \
+ "$hline_alnum_punc_tab_enter"
+ ) || return
# Return if the value has not changed from current
local cur_val="$( f_sysrc_get "$var" )"
[ "$_input" = "$cur_val" ] && return $SUCCESS
-
- f_dprintf "$var: [$cur_val]->[$_input]"
- err=$( f_sysrc_set "$var" "$_input" 2>&1 ) ||
- f_show_msg "$err"
+ f_dprintf "%s: [%s]->[%s]" "$var" "$cur_val" "$_input"
+
+ err=$( f_sysrc_set "$var" "$_input" 2>&1 ) || f_dialog_msgbox "$err"
}
+############################################################ MAIN
+
+f_dprintf "%s: Successfully loaded." startup/rcedit.subr
+
fi # ! $_STARTUP_RCEDIT_SUBR
diff --git a/usr.sbin/bsdconfig/startup/share/rcvar.subr b/usr.sbin/bsdconfig/startup/share/rcvar.subr
index 3cd60e8..702c5e9 100644
--- a/usr.sbin/bsdconfig/startup/share/rcvar.subr
+++ b/usr.sbin/bsdconfig/startup/share/rcvar.subr
@@ -30,6 +30,7 @@ if [ ! "$_STARTUP_RCVAR_SUBR" ]; then _STARTUP_RCVAR_SUBR=1
BSDCFG_SHARE="/usr/share/bsdconfig"
. $BSDCFG_SHARE/common.subr || exit 1
+f_dprintf "%s: loading includes..." startup/rcvar.subr
f_include $BSDCFG_SHARE/sysrc.subr
############################################################ CONFIGURATION
@@ -106,11 +107,11 @@ f_startup_rcvar_map()
)"
#
- # Calculate a digest given the checksums of all dependencies (scripts and
- # the defaults file). This digest will be used to determine if an on-disk
- # global persistant cache file (containg this digest on the first line)
- # is valid and can be used to quickly populate the cache value for
- # immediate return.
+ # Calculate a digest given the checksums of all dependencies (scripts
+ # and the defaults file). This digest will be used to determine if an
+ # on-disk global persistant cache file (containg this digest on the
+ # first line) is valid and can be used to quickly populate the cache
+ # value for immediate return.
#
local rc_script_list_digest
rc_script_list_digest=$( cd "$ETC_RC_D" &&
@@ -130,11 +131,12 @@ f_startup_rcvar_map()
read digest rest_ignored
#
- # If the stored digest matches the calculated-one
- # populate the in-memory cache from the on-disk
- # cache and provide success exit status.
+ # If the stored digest matches the calculated-
+ # one populate the in-memory cache from the on-
+ # disk cache and provide success exit status.
#
- if [ "$digest" = "$rc_script_list_digest" ]; then
+ if [ "$digest" = "$rc_script_list_digest" ]
+ then
cat
exit $SUCCESS
else
@@ -150,7 +152,7 @@ f_startup_rcvar_map()
echo "$STARTUP_RCVAR_MAP"
return $SUCCESS
fi
- # Otherwise, fall-through to create in-memory cache from scratch
+ # Otherwise, fall-thru to create in-memory cache from scratch
fi
#
@@ -203,4 +205,8 @@ f_startup_rcvar_map()
mv "$tmpfile" "$STARTUP_RCVAR_MAP_CACHEFILE"
}
+############################################################ MAIN
+
+f_dprintf "%s: Successfully loaded." startup/rcvar.subr
+
fi # ! $_STARTUP_RCVAR_SUBR
diff --git a/usr.sbin/bsdconfig/startup/startup b/usr.sbin/bsdconfig/startup/startup
index c481761..c9229b7 100755
--- a/usr.sbin/bsdconfig/startup/startup
+++ b/usr.sbin/bsdconfig/startup/startup
@@ -30,13 +30,14 @@
BSDCFG_SHARE="/usr/share/bsdconfig"
. $BSDCFG_SHARE/common.subr || exit 1
+f_dprintf "%s: loading includes..." "$0"
f_include $BSDCFG_SHARE/dialog.subr
f_include $BSDCFG_SHARE/mustberoot.subr
BSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="140.startup"
f_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
-ipgm=$( f_index_menu_selection $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
+ipgm=$( f_index_menusel_keyword $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
[ $? -eq $SUCCESS -a "$ipgm" ] && pgm="$ipgm"
############################################################ FUNCTIONS
@@ -99,7 +100,6 @@ shift $(( $OPTIND - 1 ))
#
# Initialize
#
-f_dialog_init
f_dialog_title "$msg_startup"
f_dialog_backtitle "${ipgm:+bsdconfig }$pgm"
f_mustberoot_init
diff --git a/usr.sbin/bsdconfig/timezone/share/continents.subr b/usr.sbin/bsdconfig/timezone/share/continents.subr
index 8f3e516..9a66ec7 100644
--- a/usr.sbin/bsdconfig/timezone/share/continents.subr
+++ b/usr.sbin/bsdconfig/timezone/share/continents.subr
@@ -30,6 +30,7 @@ if [ ! "$_TIMEZONE_CONTINENTS_SUBR" ]; then _TIMEZONE_CONTINENTS_SUBR=1
BSDCFG_SHARE="/usr/share/bsdconfig"
. $BSDCFG_SHARE/common.subr || exit 1
+f_dprintf "%s: loading includes..." timezone/continents.subr
BSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="090.timezone"
f_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
@@ -135,4 +136,8 @@ f_OCEANP()
esac
}
+############################################################ MAIN
+
+f_dprintf "%s: Successfully loaded." timezone/continents.subr
+
fi # ! $_TIMEZONE_CONTINENTS_SUBR
diff --git a/usr.sbin/bsdconfig/timezone/share/countries.subr b/usr.sbin/bsdconfig/timezone/share/countries.subr
index 9c6a164..6a39dc7 100644
--- a/usr.sbin/bsdconfig/timezone/share/countries.subr
+++ b/usr.sbin/bsdconfig/timezone/share/countries.subr
@@ -75,4 +75,6 @@ f_sort_countries()
export COUNTRIES
}
+f_dprintf "%s: Successfully loaded." timezone/countries.subr
+
fi # ! $_TIMEZONE_COUNTRIES_SUBR
diff --git a/usr.sbin/bsdconfig/timezone/share/iso3166.subr b/usr.sbin/bsdconfig/timezone/share/iso3166.subr
index 72e934a..9af6393 100644
--- a/usr.sbin/bsdconfig/timezone/share/iso3166.subr
+++ b/usr.sbin/bsdconfig/timezone/share/iso3166.subr
@@ -30,6 +30,7 @@ if [ ! "$_TIMEZONE_ISO3166_SUBR" ]; then _TIMEZONE_ISO3166_SUBR=1
BSDCFG_SHARE="/usr/share/bsdconfig"
. $BSDCFG_SHARE/common.subr || exit 1
+f_dprintf "%s: loading includes..." timezone/iso3166.subr
BSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="090.timezone"
f_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
@@ -194,4 +195,8 @@ f_read_iso3166_table()
"$_PATH_ISO3166" )
}
+############################################################ MAIN
+
+f_dprintf "%s: Successfully loaded." timezone/iso3166.subr
+
fi # ! $_TIMEZONE_ISO3166_SUBR
diff --git a/usr.sbin/bsdconfig/timezone/share/menus.subr b/usr.sbin/bsdconfig/timezone/share/menus.subr
index 5ce50f8..687e102 100644
--- a/usr.sbin/bsdconfig/timezone/share/menus.subr
+++ b/usr.sbin/bsdconfig/timezone/share/menus.subr
@@ -30,6 +30,7 @@ if [ ! "$_TIMEZONE_MENUS_SUBR" ]; then _TIMEZONE_MENUS_SUBR=1
BSDCFG_SHARE="/usr/share/bsdconfig"
. $BSDCFG_SHARE/common.subr || exit 1
+f_dprintf "%s: loading includes..." timezone/menus.subr
f_include $BSDCFG_SHARE/dialog.subr
############################################################ GLOBALS
@@ -217,4 +218,8 @@ f_make_menus()
eval $( :| awk "$f_make_menus_awk" )
}
+############################################################ MAIN
+
+f_dprintf "%s: Successfully loaded." timezone/menus.subr
+
fi # ! $_TIMEZONE_MENUS_SUBR
diff --git a/usr.sbin/bsdconfig/timezone/share/zones.subr b/usr.sbin/bsdconfig/timezone/share/zones.subr
index 81008a9..3ba1d7a 100644
--- a/usr.sbin/bsdconfig/timezone/share/zones.subr
+++ b/usr.sbin/bsdconfig/timezone/share/zones.subr
@@ -30,6 +30,7 @@ if [ ! "$_TIMEZONE_ZONES_SUBR" ]; then _TIMEZONE_ZONES_SUBR=1
BSDCFG_SHARE="/usr/share/bsdconfig"
. $BSDCFG_SHARE/common.subr || exit 1
+f_dprintf "%s: loading includes..." timezone/zones.subr
f_include $BSDCFG_SHARE/dialog.subr
f_include $BSDCFG_SHARE/timezone/continents.subr
@@ -619,7 +620,7 @@ f_install_zoneinfo()
return $rv
}
-
+
# f_confirm_zone $filename
#
# Prompt the user to confirm the new timezone data. The first (and only)
@@ -667,4 +668,8 @@ f_set_zone_utc()
f_install_zoneinfo_file ""
}
+############################################################ MAIN
+
+f_dprintf "%s: Successfully loaded." timezone/zones.subr
+
fi # ! $_TIMEZONE_ZONES_SUBR
diff --git a/usr.sbin/bsdconfig/timezone/timezone b/usr.sbin/bsdconfig/timezone/timezone
index 02626a6..5649b60 100755
--- a/usr.sbin/bsdconfig/timezone/timezone
+++ b/usr.sbin/bsdconfig/timezone/timezone
@@ -30,6 +30,7 @@
BSDCFG_SHARE="/usr/share/bsdconfig"
. $BSDCFG_SHARE/common.subr || exit 1
+f_dprintf "%s: loading includes..." "$0"
f_include $BSDCFG_SHARE/dialog.subr
f_include $BSDCFG_SHARE/mustberoot.subr
f_include $BSDCFG_SHARE/strings.subr
@@ -42,7 +43,7 @@ f_include $BSDCFG_SHARE/timezone/zones.subr
BSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="090.timezone"
f_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
-ipgm=$( f_index_menu_selection $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
+ipgm=$( f_index_menusel_keyword $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
[ $? -eq $SUCCESS -a "$ipgm" ] && pgm="$ipgm"
############################################################ CONFIGURATION
@@ -131,7 +132,6 @@ shift $(( $OPTIND - 1 ))
#
# Initialize
#
-f_dialog_init
f_dialog_title "$msg_time_zone"
f_dialog_backtitle "${ipgm:+bsdconfig }$pgm"
f_mustberoot_init
diff --git a/usr.sbin/bsdconfig/ttys/ttys b/usr.sbin/bsdconfig/ttys/ttys
index 4e67e2c..db7cb20 100755
--- a/usr.sbin/bsdconfig/ttys/ttys
+++ b/usr.sbin/bsdconfig/ttys/ttys
@@ -30,13 +30,14 @@
BSDCFG_SHARE="/usr/share/bsdconfig"
. $BSDCFG_SHARE/common.subr || exit 1
+f_dprintf "%s: loading includes..." "$0"
f_include $BSDCFG_SHARE/dialog.subr
f_include $BSDCFG_SHARE/mustberoot.subr
BSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="150.ttys"
f_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
-ipgm=$( f_index_menu_selection $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
+ipgm=$( f_index_menusel_keyword $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
[ $? -eq $SUCCESS -a "$ipgm" ] && pgm="$ipgm"
############################################################ CONFIGURATION
@@ -76,7 +77,6 @@ shift $(( $OPTIND - 1 ))
#
# Initialize
#
-f_dialog_init
f_dialog_title "$msg_configure_ttys"
f_dialog_backtitle "${ipgm:+bsdconfig }$pgm"
f_mustberoot_init
diff --git a/usr.sbin/bsdconfig/usermgmt/groupadd b/usr.sbin/bsdconfig/usermgmt/groupadd
index 7326ab3..34962a9 100755
--- a/usr.sbin/bsdconfig/usermgmt/groupadd
+++ b/usr.sbin/bsdconfig/usermgmt/groupadd
@@ -31,11 +31,12 @@
BSDCFG_SHARE="/usr/share/bsdconfig"
. $BSDCFG_SHARE/common.subr || exit 1
+f_dprintf "%s: loading includes..." "$0"
f_include $BSDCFG_SHARE/dialog.subr
BSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="070.usermgmt"
-ipgm=$( f_index_menu_selection $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
+ipgm=$( f_index_menusel_keyword $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
[ $? -eq $SUCCESS -a "$ipgm" ] && pgm="$ipgm"
############################################################ MAIN
@@ -56,7 +57,6 @@ shift $(( $OPTIND - 1 ))
#
# Chain-load to groupinput to centralize code and minimize duplication
#
-f_dialog_init
$BSDCFG_LIBE/$APP_DIR/groupinput ${USE_XDIALOG:+-X} mode="Add"
################################################################################
diff --git a/usr.sbin/bsdconfig/usermgmt/groupdel b/usr.sbin/bsdconfig/usermgmt/groupdel
index cbbe554..b51f002 100755
--- a/usr.sbin/bsdconfig/usermgmt/groupdel
+++ b/usr.sbin/bsdconfig/usermgmt/groupdel
@@ -31,6 +31,7 @@
BSDCFG_SHARE="/usr/share/bsdconfig"
. $BSDCFG_SHARE/common.subr || exit 1
+f_dprintf "%s: loading includes..." "$0"
f_include $BSDCFG_SHARE/dialog.subr
f_include $BSDCFG_SHARE/mustberoot.subr
f_include $BSDCFG_SHARE/usermgmt/group_input.subr
@@ -38,7 +39,7 @@ f_include $BSDCFG_SHARE/usermgmt/group_input.subr
BSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="070.usermgmt"
f_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
-ipgm=$( f_index_menu_selection $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
+ipgm=$( f_index_menusel_keyword $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
[ $? -eq $SUCCESS -a "$ipgm" ] && pgm="$ipgm"
############################################################ MAIN
@@ -59,7 +60,6 @@ shift $(( $OPTIND - 1 ))
#
# Initialize
#
-f_dialog_init
f_dialog_title "$msg_delete $msg_group"
f_dialog_backtitle "${ipgm:+bsdconfig }$pgm"
f_mustberoot_init
@@ -71,7 +71,7 @@ while :; do
f_dialog_menu_group_list
retval=$?
mtag=$( f_dialog_menutag )
- f_dprintf "retval=$retval mtag=[$mtag]"
+ f_dprintf "retval=%u mtag=[%s]" $retval "$mtag"
[ $retval -eq 0 ] || f_die
diff --git a/usr.sbin/bsdconfig/usermgmt/groupedit b/usr.sbin/bsdconfig/usermgmt/groupedit
index 6de6fa8..c192c0f 100755
--- a/usr.sbin/bsdconfig/usermgmt/groupedit
+++ b/usr.sbin/bsdconfig/usermgmt/groupedit
@@ -31,6 +31,7 @@
BSDCFG_SHARE="/usr/share/bsdconfig"
. $BSDCFG_SHARE/common.subr || exit 1
+f_dprintf "%s: loading includes..." "$0"
f_include $BSDCFG_SHARE/dialog.subr
f_include $BSDCFG_SHARE/mustberoot.subr
f_include $BSDCFG_SHARE/usermgmt/group_input.subr
@@ -38,7 +39,7 @@ f_include $BSDCFG_SHARE/usermgmt/group_input.subr
BSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="070.usermgmt"
f_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
-ipgm=$( f_index_menu_selection $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
+ipgm=$( f_index_menusel_keyword $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
[ $? -eq $SUCCESS -a "$ipgm" ] && pgm="$ipgm"
############################################################ MAIN
@@ -59,7 +60,6 @@ shift $(( $OPTIND - 1 ))
#
# Initialize
#
-f_dialog_init
f_dialog_title "$msg_edit_view $msg_group"
f_dialog_backtitle "${ipgm:+bsdconfig }$pgm"
f_mustberoot_init
@@ -71,7 +71,7 @@ while :; do
f_dialog_menu_group_list
retval=$?
mtag=$( f_dialog_menutag )
- f_dprintf "retval=$retval mtag=[$mtag]"
+ f_dprintf "retval=%s mtag=[%s]" $retval "$mtag"
[ $retval -eq 0 ] || f_die
diff --git a/usr.sbin/bsdconfig/usermgmt/groupinput b/usr.sbin/bsdconfig/usermgmt/groupinput
index 2991d33..c459224 100755
--- a/usr.sbin/bsdconfig/usermgmt/groupinput
+++ b/usr.sbin/bsdconfig/usermgmt/groupinput
@@ -31,6 +31,7 @@
BSDCFG_SHARE="/usr/share/bsdconfig"
. $BSDCFG_SHARE/common.subr || exit 1
+f_dprintf "%s: loading includes..." "$0"
f_include $BSDCFG_SHARE/dialog.subr
f_include $BSDCFG_SHARE/mustberoot.subr
f_include $BSDCFG_SHARE/usermgmt/group_input.subr
@@ -38,7 +39,7 @@ f_include $BSDCFG_SHARE/usermgmt/group_input.subr
BSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="070.usermgmt"
f_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
-ipgm=$( f_index_menu_selection $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
+ipgm=$( f_index_menusel_keyword $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
[ $? -eq $SUCCESS -a "$ipgm" ] && pgm="$ipgm"
############################################################ CONFIGURATION
@@ -62,10 +63,10 @@ save_changes()
err=$( pw groupdel "$group_name" 2>&1 )
retval=$?
if [ $retval -ne $SUCCESS ]; then
- f_show_msg "%s %s\n" "$msg_error" "$err"
+ f_dialog_msgbox "$msg_error $err\n"
return $retval
fi
- f_show_msg "$msg_group_deleted"
+ f_dialog_msgbox "$msg_group_deleted"
;;
Add)
local cmd="pw groupadd -n '$group_name'"
@@ -77,14 +78,14 @@ save_changes()
elif [ "$group_password" ]; then
cmd="echo \"\$group_password\" | $cmd -h 0"
fi
- f_dprintf "cmd=$cmd"
+ f_dprintf "cmd=%s" "$cmd"
err=$( eval $cmd 2>&1 )
retval=$?
if [ $retval -ne $SUCCESS ]; then
- f_show_msg "%s %s\n" "$msg_error" "$err"
+ f_dialog_msgbox "$msg_error $err\n"
return $retval
fi
- f_show_msg "$msg_group_added"
+ f_dialog_msgbox "$msg_group_added"
;;
Edit/View)
local cmd="pw groupmod -n '$group_name'"
@@ -96,14 +97,14 @@ save_changes()
elif [ "$group_password" ]; then
cmd="echo \"\$group_password\" | $cmd -h 0"
fi
- f_dprintf "cmd=$cmd"
+ f_dprintf "cmd=%s" "$cmd"
err=$( eval $cmd 2>&1 )
retval=$?
if [ $retval -ne $SUCCESS ]; then
- f_show_msg "%s %s\n" "$msg_error" "$err"
+ f_dialog_msgbox "$msg_error $err\n"
return $retval
fi
- f_show_msg "$msg_group_updated"
+ f_dialog_msgbox "$msg_group_updated"
;;
esac
@@ -136,19 +137,18 @@ dialog_title_update()
while [ $# -gt 0 ]; do
key="${1%%=*}"
value="${1#*=}"
- f_dprintf "key=[$key] value=[$value]"
+ f_dprintf "key=[%s] value=[%s]" "$key" "$value"
case "$key" in
mode) mode="$value";;
group) group="$value";;
esac
shift
done
-f_dprintf "mode=[$mode] group=[$group]"
+f_dprintf "mode=[%s] group=[%s]" "$mode" "$group"
#
# Initialize
#
-f_dialog_init
dialog_title_update "$mode"
f_dialog_backtitle "${ipgm:+bsdconfig }$pgm"
f_mustberoot_init
@@ -242,7 +242,7 @@ while :; do
retval=$?
setvar DIALOG_MENU_$$ "$dialog_menu"
mtag=$( f_dialog_menutag )
- f_dprintf "retval=$retval mtag=[$mtag]"
+ f_dprintf "retval=%u mtag=[%s]" $retval "$mtag"
# Exit if user has either pressed ESC or chosen Cancel/No
[ $retval -eq $SUCCESS ] || f_die
@@ -261,7 +261,7 @@ while :; do
f_dialog_menu_group_list
retval=$?
mtag=$( f_dialog_menutag )
- f_dprintf "retval=$retval mtag=[$mtag]"
+ f_dprintf "retval=%u mtag=[%s]" $retval "$mtag"
# Loop if user has either pressed ESC or chosen Cancel/No
[ $retval -eq $SUCCESS ] || continue
diff --git a/usr.sbin/bsdconfig/usermgmt/share/group_input.subr b/usr.sbin/bsdconfig/usermgmt/share/group_input.subr
index e3d59d0..278e0a4 100644
--- a/usr.sbin/bsdconfig/usermgmt/share/group_input.subr
+++ b/usr.sbin/bsdconfig/usermgmt/share/group_input.subr
@@ -31,6 +31,7 @@ if [ ! "$_USERMGMT_GROUP_INPUT_SUBR" ]; then _USERMGMT_GROUP_INPUT_SUBR=1
BSDCFG_SHARE="/usr/share/bsdconfig"
. $BSDCFG_SHARE/common.subr || exit 1
+f_dprintf "%s: loading includes..." usermgmt/group_input.subr
f_include $BSDCFG_SHARE/dialog.subr
f_include $BSDCFG_SHARE/strings.subr
@@ -111,39 +112,16 @@ f_dialog_menu_group_list()
#
f_dialog_input_group_name()
{
- local msg="$( printf "$msg_group" )"
- local hline="$hline_alnum_tab_enter"
-
#
# Loop until the user provides taint-free/valid input
#
- local size retval _name="$1" _input="$1"
+ local _name="$1" _input="$1"
while :; do
- size=$( f_dialog_inputbox_size \
- "$DIALOG_TITLE" \
- "$DIALOG_BACKTITLE" \
- "$msg" \
- "$_input" \
- "$hline" )
-
- local dialog_inputbox
- dialog_inputbox=$( eval $DIALOG \
- --title \"\$DIALOG_TITLE\" \
- --backtitle \"\$DIALOG_BACKTITLE\" \
- --hline \"\$hline\" \
- --ok-label \"\$msg_ok\" \
- --cancel-label \"\$msg_cancel\" \
- --inputbox \"\$msg\" $size \
- \"\$_input\" \
- 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
- )
-
- retval=$?
- setvar DIALOG_INPUTBOX_$$ "$dialog_inputbox"
- _input=$( f_dialog_inputstr )
# Return if user has either pressed ESC or chosen Cancel/No
- [ $retval -eq $SUCCESS ] || return $retval
+ _input=$( f_dialog_input "$msg_group" "$_input" \
+ "$hline_alnum_tab_enter"
+ ) || return
# Check for no-change
[ "$_input" = "$_name" ] && return $SUCCESS
@@ -156,13 +134,13 @@ f_dialog_input_group_name()
# Check for NULL entry
if [ ! "$_input" ]; then
- f_show_msg "$msg_group_is_empty"
+ f_dialog_msgbox "$msg_group_is_empty"
continue
fi
# Check for invalid entry
if ! echo "$_input" | grep -q "^[[:alpha:]]"; then
- f_show_msg "$msg_group_must_start_with_letter"
+ f_dialog_msgbox "$msg_group_must_start_with_letter"
continue
fi
@@ -177,7 +155,7 @@ f_dialog_input_group_name()
done
save_flag=1
- f_dprintf "group_name: [$cur_group_name]->[$group_name]"
+ f_dprintf "group_name: [%s]->[%s]" "$cur_group_name" "$group_name"
return $SUCCESS
}
@@ -251,7 +229,7 @@ f_dialog_input_group_password()
# Check for password mismatch
if [ "$_password1" != "$_password2" ]; then
- f_show_msg "$msg_group_passwords_do_not_match"
+ f_dialog_msgbox "$msg_group_passwords_do_not_match"
continue
fi
@@ -270,7 +248,8 @@ f_dialog_input_group_password()
done
save_flag=1
- f_dprintf "group_password: [$cur_group_password]->[$group_password]"
+ f_dprintf "group_password: [%s]->[%s]" \
+ "$cur_group_password" "$group_password"
return $SUCCESS
}
@@ -283,40 +262,17 @@ f_dialog_input_group_password()
#
f_dialog_input_group_gid()
{
- local msg size retval _input="$1"
- local hline="$hline_num_tab_enter"
-
- msg=$( printf "$msg_group_id_leave_empty_for_default" )
- size=$( f_dialog_inputbox_size \
- "$DIALOG_TITLE" \
- "$DIALOG_BACKTITLE" \
- "$msg" \
- "$_input" \
- "$hline" )
-
- local dialog_inputbox
- dialog_inputbox=$( eval $DIALOG \
- --title \"\$DIALOG_TITLE\" \
- --backtitle \"\$DIALOG_BACKTITLE\" \
- --hline \"\$hline\" \
- --ok-label \"\$msg_ok\" \
- --cancel-label \"\$msg_cancel\" \
- --inputbox \"\$msg\" $size \
- \"\$_input\" \
- 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
- )
-
- retval=$?
- setvar DIALOG_INPUTBOX_$$ "$dialog_inputbox"
- _input=$( f_dialog_inputstr )
+ local _input="$1"
# Return if user has either pressed ESC or chosen Cancel/No
- [ $retval -eq $SUCCESS ] || return $retval
+ _input=$( f_dialog_input "$msg_group_id_leave_empty_for_default" \
+ "$_input" "$hline_num_tab_enter"
+ ) || return
group_gid="$_input"
save_flag=1
- f_dprintf "group_gid: [$cur_group_gid]->[$group_gid]"
+ f_dprintf "group_gid: [%s]->[%s]" "$cur_group_gid" "$group_gid"
return $SUCCESS
}
@@ -365,7 +321,7 @@ f_dialog_input_group_members()
retval=$?
setvar DIALOG_MENU_$$ "$dialog_menu"
menu_choice=$( f_dialog_menutag )
- f_dprintf "retval=$retval menu_choice=[$menu_choice]"
+ f_dprintf "retval=%u menu_choice=[%s]" $retval "$menu_choice"
# Return if user has either pressed ESC or chosen Cancel/No
[ $retval -eq $SUCCESS ] || return $retval
@@ -416,36 +372,13 @@ f_dialog_input_group_members()
_input="$_group_members"
;;
2) # Enter Group Members manually
- hline="$hline_num_tab_enter"
- msg=$(
- printf "$msg_group_members ($msg_separated_by_commas)"
- )
- size=$( f_dialog_inputbox_size \
- "$DIALOG_TITLE" \
- "$DIALOG_BACKTITLE" \
- "$msg" \
- "$_input" \
- "$hline" )
-
- local dialog_inputbox
- dialog_inputbox=$( eval $DIALOG \
- --title \"\$DIALOG_TITLE\" \
- --backtitle \"\$DIALOG_BACKTITLE\" \
- --hline \"\$hline\" \
- --ok-label \"\$msg_ok\" \
- --cancel-label \"\$msg_cancel\" \
- --inputbox \"\$msg\" $size \
- \"\$_input\" \
- 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
- )
-
- retval=$?
- setvar DIALOG_INPUTBOX_$$ "$dialog_inputbox"
- _group_members=$( f_dialog_inputstr )
+ msg="$msg_group_members ($msg_separated_by_commas)"
# Return to previous menu if user has either
# pressed ESC or chosen Cancel/No
- [ $retval -eq $SUCCESS ] || continue
+ _group_members=$( f_dialog_input "$msg" "$_input" \
+ "$hline_num_tab_enter"
+ ) || continue
_input="$_group_members"
;;
@@ -454,9 +387,14 @@ f_dialog_input_group_members()
group_members="$_input"
save_flag=1
- f_dprintf "group_members: [$cur_group_members]->[$group_members]"
+ f_dprintf "group_members: [%s]->[%s]" \
+ "$cur_group_members" "$group_members"
return $SUCCESS
}
+############################################################ MAIN
+
+f_dprintf "%s: Successfully loaded." usermgmt/group_input.subr
+
fi # ! $_USERMGMT_GROUP_INPUT_SUBR
diff --git a/usr.sbin/bsdconfig/usermgmt/share/user_input.subr b/usr.sbin/bsdconfig/usermgmt/share/user_input.subr
index 8b356b6..e0281ed 100644
--- a/usr.sbin/bsdconfig/usermgmt/share/user_input.subr
+++ b/usr.sbin/bsdconfig/usermgmt/share/user_input.subr
@@ -31,6 +31,7 @@ if [ ! "$_USERMGMT_USER_INPUT_SUBR" ]; then _USERMGMT_USER_INPUT_SUBR=1
BSDCFG_SHARE="/usr/share/bsdconfig"
. $BSDCFG_SHARE/common.subr || exit 1
+f_dprintf "%s: loading includes..." usermgmt/user_input.subr
f_include $BSDCFG_SHARE/dialog.subr
f_include $BSDCFG_SHARE/strings.subr
@@ -198,8 +199,8 @@ f_dialog_input_member_groups()
done
save_flag=1
- local debug="pw_member_groups:"
- f_dprintf "$debug [$cur_pw_member_groups]->[$pw_member_groups]"
+ f_dprintf "pw_member_groups: [%s]->[%s]" \
+ "$cur_pw_member_groups" "$pw_member_groups"
return $SUCCESS
}
@@ -216,39 +217,16 @@ f_dialog_input_member_groups()
#
f_dialog_input_name()
{
- local msg="$( printf "$msg_login" )"
- local hline="$hline_alnum_tab_enter"
-
#
# Loop until the user provides taint-free/valid input
#
- local size retval _name="$1" _input="$1"
+ local _name="$1" _input="$1"
while :; do
- size=$( f_dialog_inputbox_size \
- "$DIALOG_TITLE" \
- "$DIALOG_BACKTITLE" \
- "$msg" \
- "$_input" \
- "$hline" )
-
- local dialog_inputbox
- dialog_inputbox=$( eval $DIALOG \
- --title \"\$DIALOG_TITLE\" \
- --backtitle \"\$DIALOG_BACKTITLE\" \
- --hline \"\$hline\" \
- --ok-label \"\$msg_ok\" \
- --cancel-label \"\$msg_cancel\" \
- --inputbox \"\$msg\" $size \
- \"\$_input\" \
- 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
- )
-
- retval=$?
- setvar DIALOG_INPUTBOX_$$ "$dialog_inputbox"
- _input=$( f_dialog_inputstr )
# Return if user has either pressed ESC or chosen Cancel/No
- [ $retval -eq $SUCCESS ] || return $retval
+ _input=$( f_dialog_input "$msg_login" "$_input" \
+ "$hline_alnum_tab_enter"
+ ) || return
# Check for no-change
[ "$_input" = "$_name" ] && return $SUCCESS
@@ -261,13 +239,13 @@ f_dialog_input_name()
# Check for NULL entry
if [ ! "$_input" ]; then
- f_show_msg "$msg_login_is_empty"
+ f_dialog_msgbox "$msg_login_is_empty"
continue
fi
# Check for invalid entry
if ! echo "$_input" | grep -q "^[[:alpha:]]"; then
- f_show_msg "$msg_login_must_start_with_letter"
+ f_dialog_msgbox "$msg_login_must_start_with_letter"
continue
fi
@@ -282,7 +260,7 @@ f_dialog_input_name()
done
save_flag=1
- f_dprintf "pw_name: [$cur_pw_name]->[$pw_name]"
+ f_dprintf "pw_name: [%s]->[%s]" "$cur_pw_name" "$pw_name"
return $SUCCESS
}
@@ -356,7 +334,7 @@ f_dialog_input_password()
# Check for password mismatch
if [ "$_password1" != "$_password2" ]; then
- f_show_msg "$msg_passwords_do_not_match"
+ f_dialog_msgbox "$msg_passwords_do_not_match"
continue
fi
@@ -375,7 +353,7 @@ f_dialog_input_password()
done
save_flag=1
- f_dprintf "pw_password: [$cur_pw_password]->[$pw_password]"
+ f_dprintf "pw_password: [%s]->[%s]" "$cur_pw_password" "$pw_password"
return $SUCCESS
}
@@ -389,40 +367,17 @@ f_dialog_input_password()
#
f_dialog_input_gecos()
{
- local msg size retval _input="$1"
- local hline="$hline_alnum_punc_tab_enter"
-
- msg=$( printf "$msg_full_name" )
- size=$( f_dialog_inputbox_size \
- "$DIALOG_TITLE" \
- "$DIALOG_BACKTITLE" \
- "$msg" \
- "$_input" \
- "$hline" )
-
- local dialog_inputbox
- dialog_inputbox=$( eval $DIALOG \
- --title \"\$DIALOG_TITLE\" \
- --backtitle \"\$DIALOG_BACKTITLE\" \
- --hline \"\$hline\" \
- --ok-label \"\$msg_ok\" \
- --cancel-label \"\$msg_cancel\" \
- --inputbox \"\$msg\" $size \
- \"\$_input\" \
- 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
- )
-
- retval=$?
- setvar DIALOG_INPUTBOX_$$ "$dialog_inputbox"
- _input=$( f_dialog_inputstr )
+ local _input="$1"
# Return if user has either pressed ESC or chosen Cancel/No
- [ $retval -eq $SUCCESS ] || return $retval
+ _input=$( f_dialog_input "$msg_full_name" "$_input" \
+ "$hline_alnum_punc_tab_enter"
+ ) || return
pw_gecos="$_input"
save_flag=1
- f_dprintf "pw_gecos: [$cur_pw_gecos]->[$pw_gecos]"
+ f_dprintf "pw_gecos: [%s]->[%s]" "$cur_pw_gecos" "$pw_gecos"
return $SUCCESS
}
@@ -435,40 +390,17 @@ f_dialog_input_gecos()
#
f_dialog_input_uid()
{
- local msg size retval _input="$1"
- local hline="$hline_num_tab_enter"
-
- msg=$( printf "$msg_user_id_leave_empty_for_default" )
- size=$( f_dialog_inputbox_size \
- "$DIALOG_TITLE" \
- "$DIALOG_BACKTITLE" \
- "$msg" \
- "$_input" \
- "$hline" )
-
- local dialog_inputbox
- dialog_inputbox=$( eval $DIALOG \
- --title \"\$DIALOG_TITLE\" \
- --backtitle \"\$DIALOG_BACKTITLE\" \
- --hline \"\$hline\" \
- --ok-label \"\$msg_ok\" \
- --cancel-label \"\$msg_cancel\" \
- --inputbox \"\$msg\" $size \
- \"\$_input\" \
- 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
- )
-
- retval=$?
- setvar DIALOG_INPUTBOX_$$ "$dialog_inputbox"
- _input=$( f_dialog_inputstr )
+ local _input="$1"
# Return if user has either pressed ESC or chosen Cancel/No
- [ $retval -eq $SUCCESS ] || return $retval
+ _input=$( f_dialog_input "$msg_user_id_leave_empty_for_default" \
+ "$_input" "$hline_num_tab_enter"
+ ) || return
pw_uid="$_input"
save_flag=1
- f_dprintf "pw_uid: [$cur_pw_uid]->[$pw_uid]"
+ f_dprintf "pw_uid: [%s]->[%s]" "$cur_pw_uid" "$pw_uid"
return $SUCCESS
}
@@ -481,40 +413,17 @@ f_dialog_input_uid()
#
f_dialog_input_gid()
{
- local msg size retval _input="$1"
- local hline="$hline_num_tab_enter"
-
- msg=$( printf "$msg_group_id_leave_empty_for_default" )
- size=$( f_dialog_inputbox_size \
- "$DIALOG_TITLE" \
- "$DIALOG_BACKTITLE" \
- "$msg" \
- "$_input" \
- "$hline" )
-
- local dialog_inputbox
- dialog_inputbox=$( eval $DIALOG \
- --title \"\$DIALOG_TITLE\" \
- --backtitle \"\$DIALOG_BACKTITLE\" \
- --hline \"\$hline\" \
- --ok-label \"\$msg_ok\" \
- --cancel-label \"\$msg_cancel\" \
- --inputbox \"\$msg\" $size \
- \"\$_input\" \
- 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
- )
-
- retval=$?
- setvar DIALOG_INPUTBOX_$$ "$dialog_inputbox"
- _input=$( f_dialog_inputstr )
+ local _input="$1"
# Return if user has either pressed ESC or chosen Cancel/No
- [ $retval -eq $SUCCESS ] || return $retval
+ _input=$( f_dialog_input "$msg_group_id_leave_empty_for_default" \
+ "$_input" "$hline_num_tab_enter"
+ ) || return
pw_gid="$_input"
save_flag=1
- f_dprintf "pw_gid: [$cur_pw_gid]->[$pw_gid]"
+ f_dprintf "pw_gid: [%s]->[%s]" "$cur_pw_gid" "$pw_gid"
return $SUCCESS
}
@@ -527,40 +436,17 @@ f_dialog_input_gid()
#
f_dialog_input_class()
{
- local msg size retval _input="$1"
- local hline="$hline_alnum_tab_enter"
-
- msg=$( printf "$msg_login_class" )
- size=$( f_dialog_inputbox_size \
- "$DIALOG_TITLE" \
- "$DIALOG_BACKTITLE" \
- "$msg" \
- "$_input" \
- "$hline" )
-
- local dialog_inputbox
- dialog_inputbox=$( eval $DIALOG \
- --title \"\$DIALOG_TITLE\" \
- --backtitle \"\$DIALOG_BACKTITLE\" \
- --hline \"\$hline\" \
- --ok-label \"\$msg_ok\" \
- --cancel-label \"\$msg_cancel\" \
- --inputbox \"\$msg\" $size \
- \"\$_input\" \
- 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
- )
-
- retval=$?
- setvar DIALOG_INPUTBOX_$$ "$dialog_inputbox"
- _input=$( f_dialog_inputstr )
+ local _input="$1"
# Return if user has either pressed ESC or chosen Cancel/No
- [ $retval -eq $SUCCESS ] || return $retval
+ _input=$( f_dialog_input "$msg_login_class" "$_input" \
+ "$hline_alnum_tab_enter"
+ ) || return
pw_class="$_input"
save_flag=1
- f_dprintf "pw_class: [$cur_pw_class]->[$pw_class]"
+ f_dprintf "pw_class: [%s]->[%s]" "$cur_pw_class" "$pw_class"
return $SUCCESS
}
@@ -611,7 +497,7 @@ f_dialog_input_change()
retval=$?
setvar DIALOG_MENU_$$ "$dialog_menu"
date_type=$( f_dialog_menutag )
- f_dprintf "retval=$retval date_type=[$date_type]"
+ f_dprintf "retval=%u date_type=[%s]" $retval "$date_type"
# Return if user has either pressed ESC or chosen Cancel/No
[ $retval -eq $SUCCESS ] || return $retval
@@ -647,7 +533,7 @@ f_dialog_input_change()
retval=$?
setvar DIALOG_INPUTBOX_$$ "$dialog_inputbox"
ret_date=$( f_dialog_inputstr )
- f_dprintf "retval=$retval ret_date=[$ret_date]"
+ f_dprintf "retval=%u ret_date=[%s]" $retval "$ret_date"
# Return to menu if either ESC or Cancel/No
[ $retval -eq $SUCCESS ] || continue
@@ -674,7 +560,7 @@ f_dialog_input_change()
retval=$?
setvar DIALOG_INPUTBOX_$$ "$dialog_inputbox"
ret_time=$( f_dialog_inputstr )
- f_dprintf "retval=$retval ret_time=[$ret_time]"
+ f_dprintf "retval=%u ret_time=[%s]" $retval "$ret_time"
# Return to menu if either ESC or Cancel/No
[ $retval -eq $SUCCESS ] || continue
@@ -683,7 +569,7 @@ f_dialog_input_change()
-j -f "%d/%m/%Y %T" \
-- "$ret_date $ret_time" \
+%s 2> /dev/null )
- f_dprintf "_input=[$_input]"
+ f_dprintf "_input=[%s]" "$_input"
break ;;
3) # Enter number of days into the future
@@ -695,45 +581,25 @@ f_dialog_input_change()
f_isinteger "$ret_days" &&
ret_days=$(( $ret_days + 1 ))
- msg="$msg_password_expires_in_how_many_days"
- size=$( f_dialog_inputbox_size \
- "$DIALOG_TITLE" \
- "$DIALOG_BACKTITLE" \
- "$msg" \
- "$ret_days" \
- "$hline" )
-
- local dialog_inputbox
- dialog_inputbox=$( eval $DIALOG \
- --title \"\$DIALOG_TITLE\" \
- --backtitle \"\$DIALOG_BACKTITLE\" \
- --hline \"\$hline\" \
- --ok-label \"\$msg_ok\" \
- --cancel-label \"\$msg_cancel\" \
- --inputbox \"\$msg\" $size \
- \"\$ret_days\" \
- 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
- )
- retval=$?
- setvar DIALOG_INPUTBOX_$$ "$dialog_inputbox"
- ret_days=$( f_dialog_inputstr )
-
# Return to menu if either ESC or Cancel/No
- [ $retval -eq $SUCCESS ] || continue
+ ret_days=$( f_dialog_input \
+ "$msg_password_expires_in_how_many_days" \
+ "$ret_days" "$hline"
+ ) || continue
# Taint-check the user's input
if ! f_isinteger "$ret_days"; then
- f_show_msg "$msg_invalid_number_of_days"
+ f_dialog_msgbox "$msg_invalid_number_of_days"
continue
fi
- f_dprintf "ret_days=[$ret_days]"
+ f_dprintf "ret_days=[%s]" "$ret_days"
case "$ret_days" in
[-+]*) _input=$( date -v${ret_days}d +%s );;
0) _input=$( date +%s );;
*) _input=$( date -v+${ret_days}d +%s );;
esac
- f_dprintf "_input=[$_input]"
+ f_dprintf "_input=[%s]" "$_input"
break ;;
4) # Enter value manually
@@ -741,40 +607,21 @@ f_dialog_input_change()
msg=$( printf "$msg_number_of_seconds_since_epoch" \
"$( date -r 1 "+%c %Z" )" )
- size=$( f_dialog_inputbox_size \
- "$DIALOG_TITLE" \
- "$DIALOG_BACKTITLE" \
- "$msg" \
- "$_input" \
- "$hline" )
- local dialog_inputbox
- dialog_inputbox=$( eval $DIALOG \
- --title \"\$DIALOG_TITLE\" \
- --backtitle \"\$DIALOG_BACKTITLE\" \
- --hline \"\$hline\" \
- --ok-label \"\$msg_ok\" \
- --cancel-label \"\$msg_cancel\" \
- --inputbox \"\$msg\" $size \
- \"\$_input\" \
- 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
- )
-
- retval=$?
- setvar DIALOG_INPUTBOX_$$ "$dialog_inputbox"
- ret_secs=$( f_dialog_inputstr )
# Return to menu if either ESC or Cancel/No
- [ $retval -eq $SUCCESS ] || continue
+ ret_secs=$( f_dialog_input \
+ "$msg" "$_input" "$hline" ) || continue
_input="$ret_secs"
# Taint-check the user's input
if ! f_isinteger "${_input:-0}"; then
- f_show_msg "$msg_invalid_number_of_seconds"
+ f_dialog_msgbox \
+ "$msg_invalid_number_of_seconds"
continue
fi
- f_dprintf "_input=[$_input]"
+ f_dprintf "_input=[%s]" "$_input"
break ;;
esac
@@ -784,8 +631,8 @@ f_dialog_input_change()
pw_password_expire="$_input"
save_flag=1
- local debug=pw_password_expire
- f_dprintf "$debug: [$cur_pw_password_expire]->[$pw_password_expire]"
+ f_dprintf "pw_password_expire: [%s]->[%s]" \
+ "$cur_pw_password_expire" "$pw_password_expire"
return $SUCCESS
}
@@ -836,7 +683,7 @@ f_dialog_input_expire()
retval=$?
setvar DIALOG_MENU_$$ "$dialog_menu"
date_type=$( f_dialog_menutag )
- f_dprintf "retval=$retval date_type=[$date_type]"
+ f_dprintf "retval=%u date_type=[%s]" $retval "$date_type"
# Return if user has either pressed ESC or chosen Cancel/No
[ $retval -eq $SUCCESS ] || return $retval
@@ -872,7 +719,7 @@ f_dialog_input_expire()
retval=$?
setvar DIALOG_INPUTBOX_$$ "$dialog_inputbox"
ret_date=$( f_dialog_inputstr )
- f_dprintf "retval=$retval ret_date=[$ret_date]"
+ f_dprintf "retval=%u ret_date=[%s]" $retval "$ret_date"
# Return to menu if either ESC or Cancel/No
[ $retval -eq $SUCCESS ] || continue
@@ -898,7 +745,7 @@ f_dialog_input_expire()
retval=$?
setvar DIALOG_INPUTBOX_$$ "$dialog_inputbox"
ret_time=$( f_dialog_inputstr )
- f_dprintf "retval=$retval ret_time=[$ret_time]"
+ f_dprintf "retval=%u ret_time=[%s]" $retval "$ret_time"
# Return to menu if either ESC or Cancel/No
[ $retval -eq $SUCCESS ] || continue
@@ -907,7 +754,7 @@ f_dialog_input_expire()
-j -f "%d/%m/%Y %T" \
-- "$ret_date $ret_time" \
+%s 2> /dev/null )
- f_dprintf "_input=[$_input]"
+ f_dprintf "_input=[%s]" "$_input"
break ;;
3) # Enter number of days into the future
@@ -919,45 +766,25 @@ f_dialog_input_expire()
f_isinteger "$ret_days" &&
ret_days=$(( $ret_days + 1 ))
- msg="$msg_account_expires_in_how_many_days"
- size=$( f_dialog_inputbox_size \
- "$DIALOG_TITLE" \
- "$DIALOG_BACKTITLE" \
- "$msg" \
- "$ret_days" \
- "$hline" )
-
- local dialog_inputbox
- dialog_inputbox=$( eval $DIALOG \
- --title \"\$DIALOG_TITLE\" \
- --backtitle \"\$DIALOG_BACKTITLE\" \
- --hline \"\$hline\" \
- --ok-label \"\$msg_ok\" \
- --cancel-label \"\$msg_cancel\" \
- --inputbox \"\$msg\" $size \
- \"\$ret_days\" \
- 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
- )
- retval=$?
- setvar DIALOG_INPUTBOX_$$ "$dialog_inputbox"
- ret_days=$( f_dialog_inputstr )
-
# Return to menu if either ESC or Cancel/No
- [ $retval -eq $SUCCESS ] || continue
+ ret_days=$( f_dialog_input \
+ "$msg_account_expires_in_how_many_days" \
+ "$ret_days" "$hline"
+ ) || continue
# Taint-check the user's input
if ! f_isinteger "$ret_days"; then
- f_show_msg "$msg_invalid_number_of_days"
+ f_dialog_msgbox "$msg_invalid_number_of_days"
continue
fi
- f_dprintf "ret_days=[$ret_days]"
+ f_dprintf "ret_days=[%s]" "$ret_days"
case "$ret_days" in
[-+]*) _input=$( date -v${ret_days}d +%s );;
0) _input=$( date +%s );;
*) _input=$( date -v+${ret_days}d +%s );;
esac
- f_dprintf "_input=[$_input]"
+ f_dprintf "_input=[%s]" "$_input"
break ;;
4) # Enter value manually
@@ -965,40 +792,21 @@ f_dialog_input_expire()
msg=$( printf "$msg_number_of_seconds_since_epoch" \
"$( date -r 1 "+%c %Z" )" )
- size=$( f_dialog_inputbox_size \
- "$DIALOG_TITLE" \
- "$DIALOG_BACKTITLE" \
- "$msg" \
- "$_input" \
- "$hline" )
- local dialog_inputbox
- dialog_inputbox=$( eval $DIALOG \
- --title \"\$DIALOG_TITLE\" \
- --backtitle \"\$DIALOG_BACKTITLE\" \
- --hline \"\$hline\" \
- --ok-label \"\$msg_ok\" \
- --cancel-label \"\$msg_cancel\" \
- --inputbox \"\$msg\" $size \
- \"\$_input\" \
- 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
- )
-
- retval=$?
- setvar DIALOG_INPUTBOX_$$ "$dialog_inputbox"
- ret_secs=$( f_dialog_inputstr )
# Return to menu if either ESC or Cancel/No
- [ $retval -eq $SUCCESS ] || continue
+ ret_secs=$( f_dialog_input \
+ "$msg" "$_input" "$hline" ) || continue
_input="$ret_secs"
# Taint-check the user's input
if ! f_isinteger "${_input:-0}"; then
- f_show_msg "$msg_invalid_number_of_seconds"
+ f_dialog_msgbox \
+ "$msg_invalid_number_of_seconds"
continue
fi
- f_dprintf "_input=[$_input]"
+ f_dprintf "_input=[%s]" "$_input"
break ;;
esac
@@ -1008,8 +816,8 @@ f_dialog_input_expire()
pw_account_expire="$_input"
save_flag=1
- local debug=pw_account_expire
- f_dprintf "$debug: [$cur_pw_account_expire]->[$pw_account_expire]"
+ f_dprintf "pw_account_expire: [%s]->[%s]" \
+ "$cur_pw_account_expire" "$pw_account_expire"
return $SUCCESS
}
@@ -1022,40 +830,17 @@ f_dialog_input_expire()
#
f_dialog_input_home_dir()
{
- local msg size retval _input="$1"
- local hline="$hline_alnum_punc_tab_enter"
-
- msg=$( printf "$msg_home_directory" )
- size=$( f_dialog_inputbox_size \
- "$DIALOG_TITLE" \
- "$DIALOG_BACKTITLE" \
- "$msg" \
- "$_input" \
- "$hline" )
-
- local dialog_inputbox
- dialog_inputbox=$( eval $DIALOG \
- --title \"\$DIALOG_TITLE\" \
- --backtitle \"\$DIALOG_BACKTITLE\" \
- --hline \"\$hline\" \
- --ok-label \"\$msg_ok\" \
- --cancel-label \"\$msg_cancel\" \
- --inputbox \"\$msg\" $size \
- \"\$_input\" \
- 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
- )
-
- retval=$?
- setvar DIALOG_INPUTBOX_$$ "$dialog_inputbox"
- _input=$( f_dialog_inputstr )
+ local _input="$1"
# Return if user has either pressed ESC or chosen Cancel/No
- [ $retval -eq $SUCCESS ] || return $retval
+ _input=$( f_dialog_input "$msg_home_directory" "$_input" \
+ "$hline_alnum_punc_tab_enter"
+ ) || return
pw_home_dir="$_input"
save_flag=1
- f_dprintf "pw_home_dir: [$cur_pw_home_dir]->[$pw_home_dir]"
+ f_dprintf "pw_home_dir: [%s]->[%s]" "$cur_pw_home_dir" "$pw_home_dir"
return $SUCCESS
}
@@ -1081,7 +866,8 @@ f_dialog_input_home_create()
fi
save_flag=1
- f_dprintf "pw_home_create: [$cur_pw_home_create]->[$pw_home_create]"
+ f_dprintf "pw_home_create: [%s]->[%s]" \
+ "$cur_pw_home_create" "$pw_home_create"
[ $retval -ne 255 ] # return failure if user pressed ESC
}
@@ -1123,7 +909,8 @@ f_dialog_input_group_delete()
fi
save_flag=1
- f_dprintf "pw_group_delete: [$cur_pw_group_delete]->[$pw_group_delete]"
+ f_dprintf "pw_group_delete: [%s]->[%s]" \
+ "$cur_pw_group_delete" "$pw_group_delete"
[ $retval -ne 255 ] # return failure if user pressed ESC
}
@@ -1149,7 +936,8 @@ f_dialog_input_home_delete()
fi
save_flag=1
- f_dprintf "pw_home_delete: [$cur_pw_home_delete]->[$pw_home_delete]"
+ f_dprintf "pw_home_delete: [%s]->[%s]" \
+ "$cur_pw_home_delete" "$pw_home_delete"
[ $retval -ne 255 ] # return failure if user pressed ESC
}
@@ -1176,8 +964,8 @@ f_dialog_input_dotfiles_create()
fi
save_flag=1
- local debug="pw_dotfiles_create:"
- f_dprintf "$debug: [$cur_pw_dotfiles_create]->[$pw_dotfiles_create]"
+ f_dprintf "pw_dotfiles_create: [%s]->[%s]" \
+ "$cur_pw_dotfiles_create" "$pw_dotfiles_create"
[ $retval -ne 255 ] # return failure if user pressed ESC
}
@@ -1235,9 +1023,13 @@ f_dialog_input_shell()
pw_shell="$_input"
save_flag=1
- f_dprintf "pw_shell: [$cur_pw_shell]->[$pw_shell]"
+ f_dprintf "pw_shell: [%s]->[%s]" "$cur_pw_shell" "$pw_shell"
return $SUCCESS
}
+############################################################ MAIN
+
+f_dprintf "%s: Successfully loaded." usermgmt/user_input.subr
+
fi # ! $_USERMGMT_USER_INPUT_SUBR
diff --git a/usr.sbin/bsdconfig/usermgmt/useradd b/usr.sbin/bsdconfig/usermgmt/useradd
index 4821ccd..8a69729 100755
--- a/usr.sbin/bsdconfig/usermgmt/useradd
+++ b/usr.sbin/bsdconfig/usermgmt/useradd
@@ -31,11 +31,12 @@
BSDCFG_SHARE="/usr/share/bsdconfig"
. $BSDCFG_SHARE/common.subr || exit 1
+f_dprintf "%s: loading includes..." "$0"
f_include $BSDCFG_SHARE/dialog.subr
BSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="070.usermgmt"
-ipgm=$( f_index_menu_selection $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
+ipgm=$( f_index_menusel_keyword $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
[ $? -eq $SUCCESS -a "$ipgm" ] && pgm="$ipgm"
############################################################ MAIN
@@ -56,7 +57,6 @@ shift $(( $OPTIND - 1 ))
#
# Chain-load to userinput to centralize code and minimize duplication
#
-f_dialog_init
$BSDCFG_LIBE/$APP_DIR/userinput ${USE_XDIALOG:+-X} mode="Add"
################################################################################
diff --git a/usr.sbin/bsdconfig/usermgmt/userdel b/usr.sbin/bsdconfig/usermgmt/userdel
index c530682..4a7e2be 100755
--- a/usr.sbin/bsdconfig/usermgmt/userdel
+++ b/usr.sbin/bsdconfig/usermgmt/userdel
@@ -31,6 +31,7 @@
BSDCFG_SHARE="/usr/share/bsdconfig"
. $BSDCFG_SHARE/common.subr || exit 1
+f_dprintf "%s: loading includes..." "$0"
f_include $BSDCFG_SHARE/dialog.subr
f_include $BSDCFG_SHARE/mustberoot.subr
f_include $BSDCFG_SHARE/usermgmt/user_input.subr
@@ -38,7 +39,7 @@ f_include $BSDCFG_SHARE/usermgmt/user_input.subr
BSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="070.usermgmt"
f_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
-ipgm=$( f_index_menu_selection $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
+ipgm=$( f_index_menusel_keyword $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
[ $? -eq $SUCCESS -a "$ipgm" ] && pgm="$ipgm"
############################################################ MAIN
@@ -59,7 +60,6 @@ shift $(( $OPTIND - 1 ))
#
# Initialize
#
-f_dialog_init
f_dialog_title "$msg_delete $msg_login"
f_dialog_backtitle "${ipgm:+bsdconfig }$pgm"
f_mustberoot_init
@@ -71,7 +71,7 @@ while :; do
f_dialog_menu_user_list
retval=$?
mtag=$( f_dialog_menutag )
- f_dprintf "retval=$retval mtag=[$mtag]"
+ f_dprintf "retval=%u mtag=[%s]" $retval "$mtag"
[ $retval -eq 0 ] || f_die
diff --git a/usr.sbin/bsdconfig/usermgmt/useredit b/usr.sbin/bsdconfig/usermgmt/useredit
index a38afa2..67aba07 100755
--- a/usr.sbin/bsdconfig/usermgmt/useredit
+++ b/usr.sbin/bsdconfig/usermgmt/useredit
@@ -31,6 +31,7 @@
BSDCFG_SHARE="/usr/share/bsdconfig"
. $BSDCFG_SHARE/common.subr || exit 1
+f_dprintf "%s: loading includes..." "$0"
f_include $BSDCFG_SHARE/dialog.subr
f_include $BSDCFG_SHARE/mustberoot.subr
f_include $BSDCFG_SHARE/usermgmt/user_input.subr
@@ -38,7 +39,7 @@ f_include $BSDCFG_SHARE/usermgmt/user_input.subr
BSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="070.usermgmt"
f_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
-ipgm=$( f_index_menu_selection $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
+ipgm=$( f_index_menusel_keyword $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
[ $? -eq $SUCCESS -a "$ipgm" ] && pgm="$ipgm"
############################################################ MAIN
@@ -59,7 +60,6 @@ shift $(( $OPTIND - 1 ))
#
# Initialize
#
-f_dialog_init
f_dialog_title "$msg_edit_view $msg_login"
f_dialog_backtitle "${ipgm:+bsdconfig }$pgm"
f_mustberoot_init
@@ -71,7 +71,7 @@ while :; do
f_dialog_menu_user_list
retval=$?
mtag=$( f_dialog_menutag )
- f_dprintf "retval=$retval mtag=[$mtag]"
+ f_dprintf "retval=%u mtag=[%s]" $retval "$mtag"
[ $retval -eq 0 ] || f_die
diff --git a/usr.sbin/bsdconfig/usermgmt/userinput b/usr.sbin/bsdconfig/usermgmt/userinput
index aca8aed..ad62a09 100755
--- a/usr.sbin/bsdconfig/usermgmt/userinput
+++ b/usr.sbin/bsdconfig/usermgmt/userinput
@@ -31,6 +31,7 @@
BSDCFG_SHARE="/usr/share/bsdconfig"
. $BSDCFG_SHARE/common.subr || exit 1
+f_dprintf "%s: loading includes..." "$0"
f_include $BSDCFG_SHARE/dialog.subr
f_include $BSDCFG_SHARE/mustberoot.subr
f_include $BSDCFG_SHARE/usermgmt/user_input.subr
@@ -38,7 +39,7 @@ f_include $BSDCFG_SHARE/usermgmt/user_input.subr
BSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="070.usermgmt"
f_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
-ipgm=$( f_index_menu_selection $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
+ipgm=$( f_index_menusel_keyword $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
[ $? -eq $SUCCESS -a "$ipgm" ] && pgm="$ipgm"
############################################################ CONFIGURATION
@@ -80,22 +81,22 @@ save_changes()
err=$( pw userdel -u "$pw_uid" 2>&1 )
retval=$?
if [ $retval -ne $SUCCESS ]; then
- f_show_msg "%s %s\n" "$msg_error" "$err"
+ f_dialog_msgbox "$msg_error $err\n"
return $retval
fi
- f_show_msg "$msg_login_deleted"
+ f_dialog_msgbox "$msg_login_deleted"
if [ "$pw_group_delete" = "$msg_yes" ] &&
f_quietly pw groupshow -g "$pw_gid"
then
err=$( pw groupdel -g "$pw_gid" 2>&1 ) ||
- f_show_msg "%s %s\n" "$msg_warning" "$err"
+ f_dialog_msgbox "$msg_warning $err\n"
fi
if [ "$pw_home_delete" = "$msg_yes" ]; then
f_dialog_info "$msg_deleting_home_directory"
err=$( rm -Rf "$pw_home_dir" 2>&1 ) ||
- f_show_msg "%s %s\n" "$msg_warning" "$err"
+ f_dialog_msgbox "$msg_warning $err\n"
fi
;;
Add)
@@ -114,30 +115,30 @@ save_changes()
elif [ "$pw_password" ]; then
cmd="echo \"\$pw_password\" | $cmd -h 0"
fi
- f_dprintf "cmd=$cmd"
+ f_dprintf "cmd=%s" "$cmd"
err=$( eval $cmd 2>&1 )
retval=$?
if [ $retval -ne $SUCCESS ]; then
- f_show_msg "%s %s\n" "$msg_error" "$err"
+ f_dialog_msgbox "$msg_error $err\n"
return $retval
fi
- f_show_msg "$msg_login_added"
+ f_dialog_msgbox "$msg_login_added"
if [ "$pw_home_create" = "$msg_yes" ]; then
err=$( mkdir -p "$pw_home_dir" 2>&1 )
if [ $? -ne $SUCCESS ]; then
- f_show_msg "%s %s\n" "$msg_warning" "$err"
+ f_dialog_msgbox "$msg_warning $err\n"
elif [ -e "$pw_home_dir" ]; then
err=$( chown -R "$pw_uid:$pw_gid" \
"$pw_home_dir" 2>&1 )
- [ $? -eq $SUCCESS ] || f_show_msg \
- "%s %s\n" "$msg_warning" "$err"
+ [ $? -eq $SUCCESS ] ||
+ f_dialog_msgbox "$msg_warning $err\n"
fi
fi
if [ "$pw_dotfiles_create" = "$msg_yes" ]; then
err=$( copy_dotfiles 2>&1 ) ||
- f_show_msg "%s %s\n" "$msg_warning" "$err"
+ f_dialog_msgbox "$msg_warning $err\n"
fi
user="$pw_name"
@@ -160,30 +161,30 @@ save_changes()
elif [ "$pw_password" ]; then
cmd="echo \"\$pw_password\" | $cmd -h 0"
fi
- f_dprintf "cmd=$cmd"
+ f_dprintf "cmd=%s" "$cmd"
err=$( eval $cmd 2>&1 )
retval=$?
if [ $retval -ne $SUCCESS ]; then
- f_show_msg "%s %s\n" "$msg_error" "$err"
+ f_dialog_msgbox "$msg_error $err\n"
return $retval
fi
- f_show_msg "$msg_login_updated"
+ f_dialog_msgbox "$msg_login_updated"
if [ "$pw_home_create" = "$msg_yes" ]; then
err=$( mkdir -p "$pw_home_dir" )
if [ $? -ne $SUCCESS ]; then
- f_show_msg "%s %s\n" "$msg_warning" "$err"
+ f_dialog_msgbox "$msg_warning $err\n"
elif [ -e "$pw_home_dir" ]; then
err=$( chown -R "$pw_uid:$pw_gid" \
"$pw_home_dir" 2>&1 )
- [ $? -eq $SUCCESS ] || f_show_msg \
- "%s %s\n" "$msg_warning" "$err"
+ [ $? -eq $SUCCESS ] ||
+ f_dialog_msgbox "$msg_warning $err\n"
fi
fi
if [ "$pw_dotfiles_create" = "$msg_yes" ]; then
err=$( copy_dotfiles 2>&1 ) ||
- f_show_msg "%s %s\n" "$msg_warning" "$err"
+ f_dialog_msgbox "$msg_warning $err\n"
fi
;;
esac
@@ -217,19 +218,18 @@ dialog_title_update()
while [ $# -gt 0 ]; do
key="${1%%=*}"
value="${1#*=}"
- f_dprintf "key=[$key] value=[$value]"
+ f_dprintf "key=[%s] value=[%s]" "$key" "$value"
case "$key" in
mode) mode="$value";;
user) user="$value";;
esac
shift
done
-f_dprintf "mode=[$mode] user=[$user]"
+f_dprintf "mode=[%s] user=[%s]" "$mode" "$user"
#
# Initialize
#
-f_dialog_init
dialog_title_update "$mode"
f_dialog_backtitle "${ipgm:+bsdconfig }$pgm"
f_mustberoot_init
@@ -391,7 +391,6 @@ while :; do
\"\$hline\" \
$menu_items )
- local dialog_menu
dialog_menu=$( eval $DIALOG \
--title \"\$DIALOG_TITLE\" \
--backtitle \"\$DIALOG_BACKTITLE\" \
@@ -406,7 +405,7 @@ while :; do
retval=$?
setvar DIALOG_MENU_$$ "$dialog_menu"
mtag=$( f_dialog_menutag )
- f_dprintf "retval=$retval mtag=[$mtag]"
+ f_dprintf "retval=%u mtag=[%s]" $retval "$mtag"
# Exit if user has either pressed ESC or chosen Cancel/No
[ $retval -eq $SUCCESS ] || f_die
@@ -425,7 +424,7 @@ while :; do
f_dialog_menu_user_list
retval=$?
mtag=$( f_dialog_menutag )
- f_dprintf "retval=$retval mtag=[$mtag]"
+ f_dprintf "retval=%u mtag=[%s]" $retval "$mtag"
# Loop if user has either pressed ESC or chosen Cancel/No
[ $retval -eq $SUCCESS ] || continue
diff --git a/usr.sbin/bsdconfig/usermgmt/usermgmt b/usr.sbin/bsdconfig/usermgmt/usermgmt
index a2e4b40..5376bde 100755
--- a/usr.sbin/bsdconfig/usermgmt/usermgmt
+++ b/usr.sbin/bsdconfig/usermgmt/usermgmt
@@ -31,6 +31,7 @@
BSDCFG_SHARE="/usr/share/bsdconfig"
. $BSDCFG_SHARE/common.subr || exit 1
+f_dprintf "%s: loading includes..." "$0"
f_include $BSDCFG_SHARE/dialog.subr
f_include $BSDCFG_SHARE/mustberoot.subr
@@ -39,7 +40,7 @@ f_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
USERMGMT_HELPFILE=$BSDCFG_LIBE/$APP_DIR/include/usermgmt.hlp
-ipgm=$( f_index_menu_selection $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
+ipgm=$( f_index_menusel_keyword $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
[ $? -eq $SUCCESS -a "$ipgm" ] && pgm="$ipgm"
############################################################ FUNCTIONS
@@ -87,6 +88,7 @@ dialog_menu_main()
--ok-label \"\$msg_ok\" \
--cancel-label \"\$msg_cancel\" \
--help-button \
+ --help-label \"\$msg_help\" \
${USE_XDIALOG:+--help \"\"} \
--menu \"\" $size $menu_list \
2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
@@ -114,7 +116,6 @@ shift $(( $OPTIND - 1 ))
#
# Initialize
#
-f_dialog_init
f_dialog_title "$msg_login_management"
f_dialog_backtitle "${ipgm:+bsdconfig }$pgm"
f_mustberoot_init
@@ -126,7 +127,7 @@ while :; do
dialog_menu_main
retval=$?
mtag=$( f_dialog_menutag )
- f_dprintf "retval=$retval mtag=[$mtag]"
+ f_dprintf "retval=%u mtag=[%s]" $retval "$mtag"
if [ $retval -eq 2 ]; then
# The Help button was pressed
diff --git a/usr.sbin/bsdinstall/bsdinstall.8 b/usr.sbin/bsdinstall/bsdinstall.8
index 97033c9..095062f 100644
--- a/usr.sbin/bsdinstall/bsdinstall.8
+++ b/usr.sbin/bsdinstall/bsdinstall.8
@@ -1,5 +1,5 @@
.\"-
-.\" Copyright (c) 2011 Nathan Whitehorn <nwhitehorn@FreeBSD.org>
+.\" Copyright (c) 2011-2013 Nathan Whitehorn <nwhitehorn@FreeBSD.org>
.\" All rights reserved.
.\"
.\" Redistribution and use in source and binary forms, with or without
@@ -25,7 +25,7 @@
.\"
.\" $FreeBSD$
.\"
-.Dd June 11, 2011
+.Dd January 21, 2013
.Dt BSDINSTALL 8
.Os
.Sh NAME
@@ -52,9 +52,10 @@ can be invoked separately by an installation script.
.Sh TARGETS
Most of the following targets are only useful for scripting the installer.
For interactive use, most users will be interested only in the
-.Cm auto
+.Cm auto ,
+.Cm jail ,
and
-.Cm jail
+.Cm script
targets.
.Bl -tag -width ".Cm jail Ar destination"
.It Cm auto
@@ -68,6 +69,12 @@ Behavior is generally similar to
.Cm auto ,
except that disk partitioning and network setup are skipped and a kernel is
not installed into the new system.
+.It Cm script Ar script
+Runs the installation script at
+.Pa script .
+See
+.Sx SCRIPTING
+for more information on this target.
.It Cm keymap
If the current controlling TTY is a
.Xr syscons 4
@@ -104,11 +111,64 @@ partition schemes. Partitions disks, runs
.Xr newfs 8 ,
and writes the new system's
.Pa fstab .
+.It Cm scriptedpart Ar parameters
+Sets up disks like
+.Cm autopart
+and
+.Cm partedit ,
+but non-interactively according to the disk setup specified in
+.Ar parameters .
+Each disk setup is specified by a three-part argument:
+.Pp
+.Ar disk
+.Op Ar scheme
+.Op Ar {partitions}
+.Pp
+Multiple disk setups are separated by semicolons. The
+.Ar disk
+argument specifies the disk on which to operate (which will be erased),
+while the
+.Ar scheme
+argument specifies the
+.Xr gpart 8
+partition scheme to apply to the disk. If
+.Ar scheme
+is unspecified,
+.Cm scriptedpart
+will apply the default bootable scheme on your platform.
+The
+.Ar partitions
+argument is also optional and specifies how to partition
+.Ar disk .
+It consists of a comma-separated list of partitions to create enclosed in
+curly braces. Each partition declaration takes the form
+.Pp
+.Ar size
+.Ar type
+.Op Ar mount point
+.Pp
+.Ar size
+specifies the partition size to create in bytes (K, M, and G suffixes
+can be appended to specify kilobytes, megabytes, and gigabytes respectively),
+while the
+.Em auto
+keyword causes the partition to take all the remaining space on the disk. The
+.Ar type
+option chooses the
+.Xr gpart 8
+filesystem type (e.g. freebsd-ufs or freebsd-swap).
+The optional
+.Ar mount point
+argument sets where the created partition is to be mounted in the installed
+system. As an example, a typical invocation looks like:
+.Pp
+bsdinstall scriptedpart ada0 { 20G freebsd-ufs /, 4G freebsd-swap, 20G freebsd-ufs /var, auto freebsd-ufs /usr }
.It Cm mount
Mounts the file systems previously configured by
-.Cm autopart
+.Cm autopart ,
+.Cm partedit ,
or
-.Cm partedit
+.Cm scriptedpart
under
.Ev BSDINSTALL_CHROOT .
.It Cm distfetch
@@ -153,13 +213,27 @@ process. Many are used internally during installation and have reasonable
default values for most installation scenarios. Others are set by various
interactive user prompts, and can be usefully overridden when making scripted
or customized installers.
-.Bl -tag -width ".Ev BSDINSTALL_DISTDIR"
+.Bl -tag -width ".Ev BSDINSTALL_DISTSITE"
.It Ev DISTRIBUTIONS
The set of distributions to install (e.g. "base kernel ports"). Default: none
.It Ev BSDINSTALL_DISTDIR
The directory in which the distribution files can be found (or to which they
should be downloaded). Default:
.Pa /usr/freebsd-dist
+.It Ev BSDINSTALL_DISTSITE
+URL from which the distribution files should be downloaded if they are not
+already present in the directory defined by
+.Ev BSDINSTALL_DISTDIR .
+This should be a full path to the files, including architecture and release
+names. Most targets (e.g.
+.Cm auto
+and
+.Cm jail )
+that prompt for a
+.Fx
+mirror will skip that step if this variable is already defined in the
+environment. Example:
+.Pa ftp://ftp.freebsd.org/pub/FreeBSD/releases/powerpc/powerpc64/9.1-RELEASE
.It Ev BSDINSTALL_CHROOT
The directory into which the distribution files should be unpacked and the
directory at which the root file system of the new system should be mounted.
@@ -177,6 +251,67 @@ target is executed. If this directory does not already exist, it will be
created. Default:
.Pa /tmp/bsdinstall_etc
.El
+.Sh SCRIPTING
+.Nm
+scripts consist of two parts: a
+.Em preamble
+and a
+.Em setup script .
+The preamble sets up the options for the installation (how to partition the
+disk[s], which distributions to install, etc.) and the optional second part is
+a shell script run under
+.Xr chroot 8
+in the newly installed system before
+.Nm
+exits. The two parts are separated by the usual script header (#!), which
+also sets the interpreter for the setup script.
+.Pp
+A typical bsdinstall script looks like this:
+.Bd -literal -offset indent
+PARTITIONS=ada0
+DISTRIBUTIONS="kernel.txz base.txz"
+
+#!/bin/sh
+echo "ifconfig_em0=DHCP" >> /etc/rc.conf
+echo "sshd_enable=YES" >> /etc/rc.conf
+pkg install puppet
+.Ed
+.Pp
+On
+.Fx
+release media, such a script placed at
+.Pa /etc/installerconfig
+will be run at boot time and the system will be rebooted automatically after
+the installation has completed. This can be used for unattended network
+installation of new systems; see
+.Xr diskless 8
+for details.
+.Ss PREAMBLE
+The preamble consists of installer settings. These control global installation
+parameters (see
+.Sx ENVIRONMENT VARIABLES )
+as well as disk partitioning. The preamble is interpreted as a
+.Xr sh 1
+script run at the very beginning of the install. If more complicated behavior
+than setting these variables is desired, arbitrary commands can be run here
+to extend the installer. In addition to the variables in
+.Sx ENVIRONMENT VARIABLES ,
+in particular
+.Ev DISTRIBUTIONS ,
+the preamble can contain a variable
+.Ev PARTITIONS
+which is passed to the
+.Cm scriptedpart
+target to control disk setup.
+.Ss SETUP SCRIPT
+Following the preamble is an optional shell script, beginning with a #!
+declaration. This script will be run at the end of the installation process
+inside a
+.Xr chroot 8
+environment in the newly installed system and can be used to set up
+configuration files, install packages, etc. Note that newly configured
+system services (e.g. networking) have not been started in the installed
+system at this time and only installation host services are available.
.Sh HISTORY
This version of
.Nm
diff --git a/usr.sbin/bsdinstall/partedit/Makefile b/usr.sbin/bsdinstall/partedit/Makefile
index 9618b09..b7f5097 100644
--- a/usr.sbin/bsdinstall/partedit/Makefile
+++ b/usr.sbin/bsdinstall/partedit/Makefile
@@ -2,7 +2,9 @@
BINDIR= /usr/libexec/bsdinstall
PROG= partedit
-LINKS= ${BINDIR}/partedit ${BINDIR}/autopart
+LINKS= ${BINDIR}/partedit ${BINDIR}/autopart \
+ ${BINDIR}/partedit ${BINDIR}/scriptedpart
+SYMLINKS= ${BINDIR}/partedit /usr/sbin/sade
LDADD= -lgeom -lncursesw -lutil -ldialog -lm
PARTEDIT_ARCH= ${MACHINE}
@@ -14,9 +16,9 @@ PARTEDIT_ARCH= generic
.endif
SRCS= diskeditor.c partedit.c gpart_ops.c partedit_${PARTEDIT_ARCH}.c \
- part_wizard.c
+ part_wizard.c scripted.c
WARNS?= 3
-NO_MAN= true
+MAN= sade.8
.include <bsd.prog.mk>
diff --git a/usr.sbin/bsdinstall/partedit/diskeditor.c b/usr.sbin/bsdinstall/partedit/diskeditor.c
index 7933caa..9149c2b 100644
--- a/usr.sbin/bsdinstall/partedit/diskeditor.c
+++ b/usr.sbin/bsdinstall/partedit/diskeditor.c
@@ -77,7 +77,7 @@ diskeditor_show(const char *title, const char *cprompt,
int partlist_height, partlist_width;
int cur_scroll = 0;
int key, fkey;
- int cur_button = 0, cur_part = 0;
+ int cur_button = 5, cur_part = 0;
int result = DLG_EXIT_UNKNOWN;
static DLG_KEYS_BINDING binding[] = {
diff --git a/usr.sbin/bsdinstall/partedit/part_wizard.c b/usr.sbin/bsdinstall/partedit/part_wizard.c
index e840ac9..a304fb8 100644
--- a/usr.sbin/bsdinstall/partedit/part_wizard.c
+++ b/usr.sbin/bsdinstall/partedit/part_wizard.c
@@ -42,7 +42,6 @@
static char *boot_disk(struct gmesh *mesh);
static char *wizard_partition(struct gmesh *mesh, const char *disk);
-static int wizard_makeparts(struct gmesh *mesh, const char *disk);
int
part_wizard(void) {
@@ -71,7 +70,7 @@ startwizard:
dlg_put_backtitle();
error = geom_gettree(&mesh);
- error = wizard_makeparts(&mesh, schemeroot);
+ error = wizard_makeparts(&mesh, schemeroot, 1);
if (error)
goto startwizard;
free(schemeroot);
@@ -168,12 +167,6 @@ provider_for_name(struct gmesh *mesh, const char *name)
struct ggeom *gp;
LIST_FOREACH(classp, &mesh->lg_class, lg_class) {
- if (strcmp(classp->lg_name, "DISK") != 0 &&
- strcmp(classp->lg_name, "PART") != 0 &&
- strcmp(classp->lg_name, "RAID") != 0 &&
- strcmp(classp->lg_name, "MD") != 0)
- continue;
-
LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
if (LIST_EMPTY(&gp->lg_provider))
continue;
@@ -288,8 +281,8 @@ query:
return (retval);
}
-static int
-wizard_makeparts(struct gmesh *mesh, const char *disk)
+int
+wizard_makeparts(struct gmesh *mesh, const char *disk, int interactive)
{
struct gmesh submesh;
struct gclass *classp;
@@ -310,7 +303,7 @@ wizard_makeparts(struct gmesh *mesh, const char *disk)
pp = provider_for_name(mesh, disk);
available = gpart_max_free(gp, NULL)*pp->lg_sectorsize;
- if (available < MIN_FREE_SPACE) {
+ if (interactive && available < MIN_FREE_SPACE) {
char availablestr[10], neededstr[10], message[512];
humanize_number(availablestr, 7, available, "B", HN_AUTOSCALE,
HN_DECIMAL);
diff --git a/usr.sbin/bsdinstall/partedit/partedit.c b/usr.sbin/bsdinstall/partedit/partedit.c
index 2436f08..eff87fe 100644
--- a/usr.sbin/bsdinstall/partedit/partedit.c
+++ b/usr.sbin/bsdinstall/partedit/partedit.c
@@ -41,6 +41,7 @@
#include "partedit.h"
struct pmetadata_head part_metadata;
+static int sade_mode = 0;
static int apply_changes(struct gmesh *mesh);
static struct partedit_item *read_geom_mesh(struct gmesh *mesh, int *nitems);
@@ -75,12 +76,15 @@ main(int argc, const char **argv)
int i, op, nitems, nscroll;
int error;
+ if (strcmp(basename(argv[0]), "sade") == 0)
+ sade_mode = 1;
+
TAILQ_INIT(&part_metadata);
init_fstab_metadata();
init_dialog(stdin, stdout);
- if (strcmp(basename(argv[0]), "sade") != 0)
+ if (!sade_mode)
dialog_vars.backtitle = __DECONST(char *, "FreeBSD Installer");
dialog_vars.item_help = TRUE;
nscroll = i = 0;
@@ -92,13 +96,20 @@ main(int argc, const char **argv)
prompt = "Please review the disk setup. When complete, press "
"the Finish button.";
part_wizard();
+ } else if (strcmp(basename(argv[0]), "scriptedpart") == 0) {
+ error = scripted_editor(argc, argv);
+ prompt = NULL;
+ if (error != 0) {
+ end_dialog();
+ return (error);
+ }
} else {
prompt = "Create partitions for FreeBSD. No changes will be "
"made until you select Finish.";
}
/* Show the part editor either immediately, or to confirm wizard */
- while (1) {
+ while (prompt != NULL) {
dlg_clear();
dlg_put_backtitle();
@@ -185,6 +196,15 @@ main(int argc, const char **argv)
free(items);
}
+ if (prompt == NULL) {
+ error = geom_gettree(&mesh);
+ if (validate_setup()) {
+ error = apply_changes(&mesh);
+ } else {
+ gpart_revert_all(&mesh);
+ error = -1;
+ }
+ }
geom_deletetree(&mesh);
free(items);
@@ -261,7 +281,7 @@ validate_setup(void)
* Check for root partitions that we aren't formatting, which is
* usually a mistake
*/
- if (root->newfs == NULL) {
+ if (root->newfs == NULL && !sade_mode) {
dialog_vars.defaultno = TRUE;
cancel = dialog_yesno("Warning", "The chosen root partition "
"has a preexisting filesystem. If it contains an existing "
diff --git a/usr.sbin/bsdinstall/partedit/partedit.h b/usr.sbin/bsdinstall/partedit/partedit.h
index bffb076..121e3a2 100644
--- a/usr.sbin/bsdinstall/partedit/partedit.h
+++ b/usr.sbin/bsdinstall/partedit/partedit.h
@@ -55,6 +55,8 @@ struct partition_metadata *get_part_metadata(const char *name, int create);
void delete_part_metadata(const char *name);
int part_wizard(void);
+int scripted_editor(int argc, const char **argv);
+int wizard_makeparts(struct gmesh *mesh, const char *disk, int interactive);
/* gpart operations */
void gpart_delete(struct gprovider *pp);
diff --git a/usr.sbin/sade/sade.8 b/usr.sbin/bsdinstall/partedit/sade.8
index 8acda90..441ddc0 100644
--- a/usr.sbin/sade/sade.8
+++ b/usr.sbin/bsdinstall/partedit/sade.8
@@ -24,7 +24,7 @@
.\"
.\" $FreeBSD$
.\"
-.Dd August 8, 2006
+.Dd December 30, 2012
.Dt SADE 8
.Os
.Sh NAME
@@ -46,28 +46,30 @@ The
.Nm
utility aims to provide a handy tool for disk management
tasks on an already installed system.
-The goal is to save
-some of the useful functionality of the old
-.Xr sysinstall 8
-which
-will be removed from the system in favor of the new installer.
+The goal is to provide the same text interface for disk management in
+.Xr bsdinstall 8
+in the post-installation environment.
.Sh SEE ALSO
-.Xr sysinstall 8
+.Xr bsdinstall 8 ,
+.Xr gpart 8
.Sh HISTORY
-This version of
+A program called
.Nm
first appeared in
-.Fx 6.3 .
-The code is extracted from the
+.Fx 6.3
+as a utility encapsulating features from the
.Xr sysinstall 8
-utility.
+installer. It was replaced in
+.Fx 10.0
+with the equivalent part of
+.Xr bsdinstall 8 .
.Sh AUTHORS
-.An Jordan K. Hubbard Aq jkh@FreeBSD.org
+.An Nathan Whitehorn Aq nwhitehorn@FreeBSD.org
.Sh BUGS
The utility misses a lot of nice features, such as tools for
manipulating
.Xr gmirror 8
or
-.Xr gvinum 8
-stuff.
+.Xr zfs 8
+.
These will be added later.
diff --git a/usr.sbin/bsdinstall/partedit/scripted.c b/usr.sbin/bsdinstall/partedit/scripted.c
new file mode 100644
index 0000000..4ac3482
--- /dev/null
+++ b/usr.sbin/bsdinstall/partedit/scripted.c
@@ -0,0 +1,213 @@
+/*-
+ * Copyright (c) 2013 Nathan Whitehorn
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ */
+
+#include <sys/param.h>
+#include <errno.h>
+#include <libutil.h>
+#include <inttypes.h>
+
+#include <libgeom.h>
+#include <dialog.h>
+#include <dlg_keys.h>
+
+#include "partedit.h"
+
+static struct gprovider *
+provider_for_name(struct gmesh *mesh, const char *name)
+{
+ struct gclass *classp;
+ struct gprovider *pp = NULL;
+ struct ggeom *gp;
+
+ LIST_FOREACH(classp, &mesh->lg_class, lg_class) {
+ LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
+ if (LIST_EMPTY(&gp->lg_provider))
+ continue;
+
+ LIST_FOREACH(pp, &gp->lg_provider, lg_provider)
+ if (strcmp(pp->lg_name, name) == 0)
+ break;
+
+ if (pp != NULL) break;
+ }
+
+ if (pp != NULL) break;
+ }
+
+ return (pp);
+}
+
+static int
+part_config(char *disk, const char *scheme, char *config)
+{
+ char *partition, *ap, *size = NULL, *type = NULL, *mount = NULL;
+ struct gclass *classp;
+ struct gmesh mesh;
+ struct ggeom *gpart = NULL;
+ int error;
+
+ if (scheme == NULL)
+ scheme = default_scheme();
+
+ error = geom_gettree(&mesh);
+ if (provider_for_name(&mesh, disk) == NULL) {
+ fprintf(stderr, "GEOM provider %s not found\n", disk);
+ geom_deletetree(&mesh);
+ return (-1);
+ }
+
+ /* Remove any existing partitioning and create new scheme */
+ LIST_FOREACH(classp, &mesh.lg_class, lg_class)
+ if (strcmp(classp->lg_name, "PART") == 0)
+ break;
+ if (classp != NULL) {
+ LIST_FOREACH(gpart, &classp->lg_geom, lg_geom)
+ if (strcmp(gpart->lg_name, disk) == 0)
+ break;
+ }
+ if (gpart != NULL)
+ gpart_destroy(gpart);
+ gpart_partition(disk, scheme);
+
+ if (strcmp(scheme, "PC98") == 0 || strcmp(scheme, "MBR") == 0) {
+ struct gmesh submesh;
+ geom_gettree(&submesh);
+ gpart_create(provider_for_name(&submesh, disk),
+ "freebsd", NULL, NULL, &disk, 0);
+ geom_deletetree(&submesh);
+ } else {
+ disk= strdup(disk);
+ }
+
+ geom_deletetree(&mesh);
+ error = geom_gettree(&mesh);
+
+ /* Create partitions */
+ if (config == NULL) {
+ wizard_makeparts(&mesh, disk, 0);
+ goto finished;
+ }
+
+ while ((partition = strsep(&config, ",")) != NULL) {
+ while ((ap = strsep(&partition, " \t\n")) != NULL) {
+ if (*ap == '\0')
+ continue;
+ if (size == NULL)
+ size = ap;
+ else if (type == NULL)
+ type = ap;
+ else if (mount == NULL)
+ mount = ap;
+ }
+ if (size == NULL)
+ continue;
+ if (strcmp(size, "auto") == 0)
+ size = NULL;
+ gpart_create(provider_for_name(&mesh, disk), type, size, mount,
+ NULL, 0);
+ geom_deletetree(&mesh);
+ error = geom_gettree(&mesh);
+ size = type = mount = NULL;
+ }
+
+finished:
+ geom_deletetree(&mesh);
+ free(disk);
+
+ return (0);
+}
+
+static
+int parse_disk_config(char *input)
+{
+ char *ap;
+ char *disk = NULL, *scheme = NULL, *partconfig = NULL;
+
+ while (input != NULL && *input != 0) {
+ if (isspace(*input)) {
+ input++;
+ continue;
+ }
+
+ switch(*input) {
+ case '{':
+ input++;
+ partconfig = strchr(input, '}');
+ if (partconfig == NULL) {
+ fprintf(stderr, "Malformed partition setup "
+ "string: %s\n", input);
+ return (1);
+ }
+ *partconfig = '\0';
+ ap = partconfig+1;
+ partconfig = input;
+ input = ap;
+ break;
+ default:
+ if (disk == NULL)
+ disk = strsep(&input, " \t\n");
+ else if (scheme == NULL)
+ scheme = strsep(&input, " \t\n");
+ else {
+ fprintf(stderr, "Unknown directive: %s\n",
+ strsep(&input, " \t\n"));
+ return (1);
+ }
+ }
+ } while (input != NULL && *input != 0);
+
+ if (disk != NULL)
+ return (part_config(disk, scheme, partconfig));
+
+ return (0);
+}
+
+int
+scripted_editor(int argc, const char **argv)
+{
+ char *token;
+ int i, error = 0, len = 0;
+
+ for (i = 1; i < argc; i++)
+ len += strlen(argv[i]) + 1;
+ char inputbuf[len], *input = inputbuf;
+ strcpy(input, argv[1]);
+ for (i = 2; i < argc; i++) {
+ strcat(input, " ");
+ strcat(input, argv[i]);
+ }
+
+ while ((token = strsep(&input, ";")) != NULL) {
+ error = parse_disk_config(token);
+ if (error != 0)
+ return (error);
+ }
+
+ return (0);
+}
+
diff --git a/usr.sbin/bsdinstall/scripts/Makefile b/usr.sbin/bsdinstall/scripts/Makefile
index 5e450b7..bcfafc0 100644
--- a/usr.sbin/bsdinstall/scripts/Makefile
+++ b/usr.sbin/bsdinstall/scripts/Makefile
@@ -2,7 +2,7 @@
SCRIPTS= auto adduser checksum config docsinstall hostname jail keymap \
mirrorselect mount netconfig netconfig_ipv4 netconfig_ipv6 rootpass \
- services time umount wlanconfig
+ script services time umount wlanconfig
BINDIR= /usr/libexec/bsdinstall
NO_MAN= true
diff --git a/usr.sbin/bsdinstall/scripts/mirrorselect b/usr.sbin/bsdinstall/scripts/mirrorselect
index 7b9ad65..30df830 100755
--- a/usr.sbin/bsdinstall/scripts/mirrorselect
+++ b/usr.sbin/bsdinstall/scripts/mirrorselect
@@ -39,8 +39,6 @@ MIRROR=`dialog --backtitle "FreeBSD Installer" \
--menu "Please select the site closest to you or \"other\" if you'd like to specify a different choice. Also note that not every site listed here carries more than the base distribution kits. Only Primary sites are guaranteed to carry the full range of possible distributions. Select a site that's close!" \
0 0 0 \
ftp://ftp.freebsd.org "Main Site"\
- ftp://snapshots.jp.freebsd.org "Snapshots Server Japan"\
- ftp://snapshots.se.freebsd.org "Snapshots Server Sweden"\
ftp://ftp.freebsd.org "IPv6 Main Site"\
ftp://ftp3.ie.freebsd.org "IPv6 Ireland"\
ftp://ftp.il.freebsd.org "IPv6 Israel"\
@@ -151,6 +149,7 @@ MIRROR=`dialog --backtitle "FreeBSD Installer" \
ftp://ftp3.se.freebsd.org "Sweden #3"\
ftp://ftp4.se.freebsd.org "Sweden #4"\
ftp://ftp5.se.freebsd.org "Sweden #5"\
+ ftp://ftp6.se.freebsd.org "Sweden #6"\
ftp://ftp.ch.freebsd.org "Switzerland"\
ftp://ftp2.ch.freebsd.org "Switzerland #2"\
ftp://ftp.tw.freebsd.org "Taiwan"\
@@ -192,7 +191,18 @@ MIRROR=`dialog --backtitle "FreeBSD Installer" \
MIRROR_BUTTON=$?
exec 3>&-
-BSDINSTALL_DISTSITE="$MIRROR/pub/FreeBSD/releases/`uname -m`/`uname -p`/`uname -r`"
+_UNAME_R=`uname -r`
+
+case ${_UNAME_R} in
+ *-CURRENT|*-STABLE|*-BETA*|*-PRERELEASE|*-RC*)
+ RELDIR="snapshots"
+ ;;
+ *)
+ RELDIR="releases"
+ ;;
+esac
+
+BSDINSTALL_DISTSITE="$MIRROR/pub/FreeBSD/${RELDIR}/`uname -m`/`uname -p`/${_UNAME_R}"
case $MIRROR_BUTTON in
$DIALOG_CANCEL)
diff --git a/usr.sbin/bsdinstall/scripts/script b/usr.sbin/bsdinstall/scripts/script
new file mode 100755
index 0000000..58ae6e9
--- /dev/null
+++ b/usr.sbin/bsdinstall/scripts/script
@@ -0,0 +1,90 @@
+#!/bin/sh
+#-
+# Copyright (c) 2013 Nathan Whitehorn
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+# 1. Redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer.
+# 2. Redistributions in binary form must reproduce the above copyright
+# notice, this list of conditions and the following disclaimer in the
+# documentation and/or other materials provided with the distribution.
+#
+# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+# SUCH DAMAGE.
+#
+# $FreeBSD$
+
+# VARIABLES:
+# PARTITIONS
+# DISTRIBUTIONS
+# BSDINSTALL_DISTDIR
+
+error() {
+ test -f $PATH_FSTAB && bsdinstall umount
+ echo "Installation Error!"
+ cat $BSDINSTALL_LOG
+ echo "Installation Error!"
+ exit 1
+}
+
+set -e
+trap error EXIT
+
+SCRIPT="$1"
+shift
+
+echo "Begun Installation at $(date)" > $BSDINSTALL_LOG
+rm -rf $BSDINSTALL_TMPETC
+mkdir $BSDINSTALL_TMPETC
+
+split -a 2 -p '^#!.*' "$SCRIPT" /tmp/bsdinstall-installscript-
+
+. /tmp/bsdinstall-installscript-aa
+: ${DISTRIBUTIONS="kernel.txz base.txz"}; export DISTRIBUTIONS
+export BSDINSTALL_DISTDIR
+
+# Make partitions
+rm -f $PATH_FSTAB
+touch $PATH_FSTAB
+bsdinstall scriptedpart "$PARTITIONS"
+bsdinstall mount
+
+# Unpack distributions
+bsdinstall checksum
+bsdinstall distextract
+
+# Finalize install
+bsdinstall config
+
+# Make sure networking is functional, if we can arrange that
+if [ ! -f $BSDINSTALL_CHROOT/etc/resolv.conf -a -f /etc/resolv.conf ]; then
+ cp /etc/resolv.conf $BSDINSTALL_CHROOT/etc/resolv.conf
+fi
+
+# Run post-install script
+if [ -f /tmp/bsdinstall-installscript-ab ]; then
+ cp /tmp/bsdinstall-installscript-ab $BSDINSTALL_CHROOT/tmp/installscript
+ chmod a+x $BSDINSTALL_CHROOT/tmp/installscript
+ mount -t devfs devfs "$BSDINSTALL_CHROOT/dev"
+ chroot $BSDINSTALL_CHROOT /tmp/installscript $@ 2>&1
+ umount "$BSDINSTALL_CHROOT/dev"
+ rm $BSDINSTALL_CHROOT/tmp/installscript
+fi
+
+bsdinstall umount
+
+echo "Installation Completed at $(date)" >> $BSDINSTALL_LOG
+
+trap true EXIT
diff --git a/usr.sbin/bsdinstall/scripts/services b/usr.sbin/bsdinstall/scripts/services
index 52aa57e..a4cfd46 100755
--- a/usr.sbin/bsdinstall/scripts/services
+++ b/usr.sbin/bsdinstall/scripts/services
@@ -45,7 +45,7 @@ DAEMONS=$(dialog --backtitle "FreeBSD Installer" \
sshd "Secure shell daemon" ${sshd_enable:-off} \
moused "PS/2 mouse pointer on console" ${moused_enable:-off} \
ntpd "Synchronize system and network time" ${ntpd_enable:-off} \
- powerd "Adjust CPU frequency dynamically" ${powerd_enable:-off} \
+ powerd "Adjust CPU frequency dynamically if supported" ${powerd_enable:-off} \
2>&1 1>&3)
exec 3>&-
diff --git a/usr.sbin/bsdinstall/scripts/wlanconfig b/usr.sbin/bsdinstall/scripts/wlanconfig
index 6894279..0a3599c 100755
--- a/usr.sbin/bsdinstall/scripts/wlanconfig
+++ b/usr.sbin/bsdinstall/scripts/wlanconfig
@@ -60,7 +60,7 @@ NETWORKS=`echo "$SCAN_RESULTS" | awk -F '\t' \
'/..:..:..:..:..:../ {if (length($5) > 0) printf("\"%s\"\t%s\n", $5, $4);}' |
sort | uniq`
-if [ -z $NETWORKS ]; then
+if [ -z "$NETWORKS" ]; then
dialog --backtitle "FreeBSD Installer" --title "Error" \
--yesno "No wireless networks were found. Rescan?" 0 0 && \
exec $0 $@
diff --git a/usr.sbin/bsnmpd/modules/snmp_target/Makefile b/usr.sbin/bsnmpd/modules/snmp_target/Makefile
index 10141e5..10141e5 100755..100644
--- a/usr.sbin/bsnmpd/modules/snmp_target/Makefile
+++ b/usr.sbin/bsnmpd/modules/snmp_target/Makefile
diff --git a/usr.sbin/bsnmpd/modules/snmp_usm/Makefile b/usr.sbin/bsnmpd/modules/snmp_usm/Makefile
index 4ae818a..4ae818a 100755..100644
--- a/usr.sbin/bsnmpd/modules/snmp_usm/Makefile
+++ b/usr.sbin/bsnmpd/modules/snmp_usm/Makefile
diff --git a/usr.sbin/bsnmpd/modules/snmp_vacm/Makefile b/usr.sbin/bsnmpd/modules/snmp_vacm/Makefile
index 1be8d62..1be8d62 100755..100644
--- a/usr.sbin/bsnmpd/modules/snmp_vacm/Makefile
+++ b/usr.sbin/bsnmpd/modules/snmp_vacm/Makefile
diff --git a/usr.sbin/bsnmpd/tools/libbsnmptools/bsnmptools.c b/usr.sbin/bsnmpd/tools/libbsnmptools/bsnmptools.c
index 52aa1a9..52aa1a9 100755..100644
--- a/usr.sbin/bsnmpd/tools/libbsnmptools/bsnmptools.c
+++ b/usr.sbin/bsnmpd/tools/libbsnmptools/bsnmptools.c
diff --git a/usr.sbin/chkgrp/chkgrp.8 b/usr.sbin/chkgrp/chkgrp.8
index abdaecc..6a57356 100644
--- a/usr.sbin/chkgrp/chkgrp.8
+++ b/usr.sbin/chkgrp/chkgrp.8
@@ -34,6 +34,7 @@
.Nd check the syntax of the group file
.Sh SYNOPSIS
.Nm
+.Op Fl q
.Op Ar groupfile
.Sh DESCRIPTION
The
@@ -47,6 +48,12 @@ contains whitespace, and that the third field (the group ID) is
numeric.
It will also check for invalid characters in the group names
and group members.
+The following options are available:
+.Bl -tag -width indent
+.It Fl q
+This option disables printing of text when the group format
+is correct.
+.El
.Sh FILES
.Bl -tag -width /etc/group -compact
.It Pa /etc/group
diff --git a/usr.sbin/chkgrp/chkgrp.c b/usr.sbin/chkgrp/chkgrp.c
index ac40ed5..c9515b5 100644
--- a/usr.sbin/chkgrp/chkgrp.c
+++ b/usr.sbin/chkgrp/chkgrp.c
@@ -30,15 +30,19 @@
__FBSDID("$FreeBSD$");
#include <err.h>
+#include <errno.h>
#include <ctype.h>
+#include <limits.h>
+#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <unistd.h>
#include <sysexits.h>
static char empty[] = { 0 };
-static void
+static void __dead2
usage(void)
{
fprintf(stderr, "usage: chkgrp [groupfile]\n");
@@ -50,27 +54,35 @@ main(int argc, char *argv[])
{
unsigned int i;
size_t len;
+ int quiet;
+ int ch;
int n = 0, k, e = 0;
char *line, *f[4], *p;
const char *cp, *gfn;
FILE *gf;
- /* check arguments */
- switch (argc) {
- case 1:
- gfn = "/etc/group";
- break;
- case 2:
- gfn = argv[1];
- break;
- default:
- gfn = NULL; /* silence compiler */
- usage();
+ quiet = 0;
+ while ((ch = getopt(argc, argv, "q")) != -1) {
+ switch (ch) {
+ case 'q':
+ quiet = 1;
+ break;
+ case '?':
+ default:
+ usage();
+ }
}
+ if (optind == argc)
+ gfn = "/etc/group";
+ else if (optind == argc - 1)
+ gfn = argv[optind];
+ else
+ usage();
+
/* open group file */
if ((gf = fopen(gfn, "r")) == NULL)
- err(EX_IOERR, "%s", gfn); /* XXX - is IO_ERR the correct exit code? */
+ err(EX_NOINPUT, "%s", gfn);
/* check line by line */
while (++n) {
@@ -78,7 +90,7 @@ main(int argc, char *argv[])
break;
if (len > 0 && line[len - 1] != '\n') {
warnx("%s: line %d: no newline character", gfn, n);
- e++;
+ e = 1;
}
while (len && isspace(line[len-1]))
len--;
@@ -111,14 +123,14 @@ main(int argc, char *argv[])
warnx("%s: line %d: missing field(s)", gfn, n);
for ( ; k < 4; k++)
f[k] = empty;
- e++;
+ e = 1;
}
for (cp = f[0] ; *cp ; cp++) {
if (!isalnum(*cp) && *cp != '.' && *cp != '_' && *cp != '-' &&
(cp > f[0] || *cp != '+')) {
warnx("%s: line %d: '%c' invalid character", gfn, n, *cp);
- e++;
+ e = 1;
}
}
@@ -126,14 +138,14 @@ main(int argc, char *argv[])
if (!isalnum(*cp) && *cp != '.' && *cp != '_' && *cp != '-' &&
*cp != ',') {
warnx("%s: line %d: '%c' invalid character", gfn, n, *cp);
- e++;
+ e = 1;
}
}
/* check if fourth field ended with a colon */
if (i < len) {
warnx("%s: line %d: too many fields", gfn, n);
- e++;
+ e = 1;
}
/* check that none of the fields contain whitespace */
@@ -141,14 +153,26 @@ main(int argc, char *argv[])
if (strcspn(f[k], " \t") != strlen(f[k])) {
warnx("%s: line %d: field %d contains whitespace",
gfn, n, k+1);
- e++;
+ e = 1;
}
}
/* check that the GID is numeric */
if (strspn(f[2], "0123456789") != strlen(f[2])) {
warnx("%s: line %d: GID is not numeric", gfn, n);
- e++;
+ e = 1;
+ }
+
+ /* check the range of the group id */
+ errno = 0;
+ unsigned long groupid = strtoul(f[2], NULL, 10);
+ if (errno != 0) {
+ warnx("%s: line %d: strtoul failed", gfn, n);
+ }
+ else if (groupid > GID_MAX) {
+ warnx("%s: line %d: group id is too large (> %ju)",
+ gfn, n, (uintmax_t)GID_MAX);
+ e = 1;
}
#if 0
@@ -163,7 +187,7 @@ main(int argc, char *argv[])
/* done */
fclose(gf);
- if (e == 0)
+ if (e == 0 && quiet == 0)
printf("%s is fine\n", gfn);
exit(e ? EX_DATAERR : EX_OK);
}
diff --git a/usr.sbin/cpucontrol/intel.c b/usr.sbin/cpucontrol/intel.c
index 15ac939..96ab704 100644
--- a/usr.sbin/cpucontrol/intel.c
+++ b/usr.sbin/cpucontrol/intel.c
@@ -33,6 +33,7 @@ __FBSDID("$FreeBSD$");
#include <unistd.h>
#include <fcntl.h>
#include <err.h>
+#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
@@ -266,7 +267,9 @@ matched:
args.size = data_size;
error = ioctl(devfd, CPUCTL_UPDATE, &args);
if (error < 0) {
+ error = errno;
fprintf(stderr, "failed.\n");
+ errno = error;
WARN(0, "ioctl()");
goto fail;
}
diff --git a/usr.sbin/cpucontrol/via.c b/usr.sbin/cpucontrol/via.c
index 71ae406..d17e31f 100644
--- a/usr.sbin/cpucontrol/via.c
+++ b/usr.sbin/cpucontrol/via.c
@@ -33,6 +33,7 @@ __FBSDID("$FreeBSD$");
#include <unistd.h>
#include <fcntl.h>
#include <err.h>
+#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
@@ -203,7 +204,9 @@ via_update(const char *dev, const char *path)
args.size = data_size;
error = ioctl(devfd, CPUCTL_UPDATE, &args);
if (error < 0) {
+ error = errno;
fprintf(stderr, "failed.\n");
+ errno = error;
WARN(0, "ioctl()");
goto fail;
}
diff --git a/usr.sbin/crunch/crunchgen/crunchgen.c b/usr.sbin/crunch/crunchgen/crunchgen.c
index 48d6f33..e25c1ac 100644
--- a/usr.sbin/crunch/crunchgen/crunchgen.c
+++ b/usr.sbin/crunch/crunchgen/crunchgen.c
@@ -105,11 +105,11 @@ int list_mode;
/* general library routines */
-void status(char *str);
+void status(const char *str);
void out_of_memory(void);
void add_string(strlst_t **listp, char *str);
-int is_dir(char *pathname);
-int is_nonempty_file(char *pathname);
+int is_dir(const char *pathname);
+int is_nonempty_file(const char *pathname);
int subtract_strlst(strlst_t **lista, strlst_t **listb);
int in_list(strlst_t **listp, char *str);
@@ -119,6 +119,8 @@ void usage(void);
void parse_conf_file(void);
void gen_outputs(void);
+extern char *crunched_skel[];
+
int
main(int argc, char **argv)
@@ -245,7 +247,7 @@ usage(void)
/* helper routines for parse_conf_file */
void parse_one_file(char *filename);
-void parse_line(char *line, int *fc, char **fv, int nf);
+void parse_line(char *pline, int *fc, char **fv, int nf);
void add_srcdirs(int argc, char **argv);
void add_progs(int argc, char **argv);
void add_link(int argc, char **argv);
@@ -340,15 +342,15 @@ parse_one_file(char *filename)
void
-parse_line(char *line, int *fc, char **fv, int nf)
+parse_line(char *pline, int *fc, char **fv, int nf)
{
char *p;
- p = line;
+ p = pline;
*fc = 0;
while (1) {
- while (isspace(*p))
+ while (isspace((unsigned char)*p))
p++;
if (*p == '\0' || *p == '#')
@@ -357,7 +359,7 @@ parse_line(char *line, int *fc, char **fv, int nf)
if (*fc < nf)
fv[(*fc)++] = p;
- while (*p && !isspace(*p) && *p != '#')
+ while (*p && !isspace((unsigned char)*p) && *p != '#')
p++;
if (*p == '\0' || *p == '#')
@@ -767,17 +769,17 @@ fillin_program_objs(prog_t *p, char *path)
}
cp = line + 6;
- while (isspace(*cp))
+ while (isspace((unsigned char)*cp))
cp++;
while(*cp) {
obj = cp;
- while (*cp && !isspace(*cp))
+ while (*cp && !isspace((unsigned char)*cp))
cp++;
if (*cp)
*cp++ = '\0';
add_string(&p->objs, obj);
- while (isspace(*cp))
+ while (isspace((unsigned char)*cp))
cp++;
}
}
@@ -887,7 +889,6 @@ gen_output_makefile(void)
void
gen_output_cfile(void)
{
- extern char *crunched_skel[];
char **cp;
FILE *outcf;
prog_t *p;
@@ -945,7 +946,7 @@ char *genident(char *str)
for (d = s = n; *s != '\0'; s++) {
if (*s == '-')
*d++ = '_';
- else if (*s == '_' || isalnum(*s))
+ else if (*s == '_' || isalnum((unsigned char)*s))
*d++ = *s;
}
*d = '\0';
@@ -978,6 +979,7 @@ top_makefile_rules(FILE *outmk)
{
prog_t *p;
+ fprintf(outmk, "LD?= ld\n");
if ( subtract_strlst(&libs, &libs_so) )
fprintf(outmk, "# NOTE: Some LIBS declarations below overridden by LIBS_SO\n");
@@ -1107,7 +1109,7 @@ prog_makefile_rules(FILE *outmk, prog_t *p)
fprintf(outmk, " $(%s_LIBS)", p->ident);
fprintf(outmk, "\n");
- fprintf(outmk, "\tld -dc -r -o %s.lo %s_stub.o $(%s_OBJPATHS)",
+ fprintf(outmk, "\t$(LD) -dc -r -o %s.lo %s_stub.o $(%s_OBJPATHS)",
p->name, p->name, p->ident);
if (p->libs)
fprintf(outmk, " $(%s_LIBS)", p->ident);
@@ -1135,7 +1137,7 @@ output_strlst(FILE *outf, strlst_t *lst)
*/
void
-status(char *str)
+status(const char *str)
{
static int lastlen = 0;
int len, spaces;
@@ -1211,7 +1213,7 @@ in_list(strlst_t **listp, char *str)
}
int
-is_dir(char *pathname)
+is_dir(const char *pathname)
{
struct stat buf;
@@ -1222,7 +1224,7 @@ is_dir(char *pathname)
}
int
-is_nonempty_file(char *pathname)
+is_nonempty_file(const char *pathname)
{
struct stat buf;
diff --git a/usr.sbin/crunch/crunchide/exec_elf32.c b/usr.sbin/crunch/crunchide/exec_elf32.c
index d01fe7e..752007f 100644
--- a/usr.sbin/crunch/crunchide/exec_elf32.c
+++ b/usr.sbin/crunch/crunchide/exec_elf32.c
@@ -31,7 +31,7 @@
#include <sys/cdefs.h>
#ifndef lint
#if 0
-__RCSID("$NetBSD: exec_elf32.c,v 1.4 1997/08/12 06:07:24 mikel Exp $");
+__RCSID("$NetBSD: exec_elf32.c,v 1.6 1999/09/20 04:12:16 christos Exp $");
#endif
#endif
__FBSDID("$FreeBSD$");
@@ -45,6 +45,7 @@ __FBSDID("$FreeBSD$");
#include <sys/stat.h>
#include <errno.h>
+#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -82,11 +83,9 @@ __FBSDID("$FreeBSD$");
#define xe32toh(x) ((data == ELFDATA2MSB) ? be32toh(x) : le32toh(x))
#define htoxe32(x) ((data == ELFDATA2MSB) ? htobe32(x) : htole32(x))
-struct listelem {
- struct listelem *next;
- void *mem;
- off_t file;
- size_t size;
+struct shlayout {
+ Elf_Shdr *shdr;
+ void *bufp;
};
static ssize_t
@@ -98,7 +97,7 @@ xreadatoff(int fd, void *buf, off_t off, size_t size, const char *fn)
perror(fn);
return -1;
}
- if ((rv = read(fd, buf, size)) != size) {
+ if ((size_t)(rv = read(fd, buf, size)) != size) {
fprintf(stderr, "%s: read error: %s\n", fn,
rv == -1 ? strerror(errno) : "short read");
return -1;
@@ -115,7 +114,7 @@ xwriteatoff(int fd, void *buf, off_t off, size_t size, const char *fn)
perror(fn);
return -1;
}
- if ((rv = write(fd, buf, size)) != size) {
+ if ((size_t)(rv = write(fd, buf, size)) != size) {
fprintf(stderr, "%s: write error: %s\n", fn,
rv == -1 ? strerror(errno) : "short write");
return -1;
@@ -162,7 +161,7 @@ ELFNAMEEND(check)(int fd, const char *fn)
*/
if (fstat(fd, &sb) == -1)
return 0;
- if (sb.st_size < sizeof eh)
+ if (sb.st_size < (off_t)(sizeof eh))
return 0;
if (read(fd, &eh, sizeof eh) != sizeof eh)
return 0;
@@ -235,87 +234,154 @@ int
ELFNAMEEND(hide)(int fd, const char *fn)
{
Elf_Ehdr ehdr;
- Elf_Shdr *shdrp = NULL, *symtabshdr, *strtabshdr;
+ struct shlayout *layoutp = NULL;
+ Elf_Shdr *shdrp = NULL, *symtabshdr, *strtabshdr, *shstrtabshdr;
+ Elf_Shdr shdrshdr;
Elf_Sym *symtabp = NULL;
- char *strtabp = NULL;
- Elf_Size nsyms, ewi;
+ char *shstrtabp = NULL, *strtabp = NULL;
+ Elf_Size nsyms, ewi;
+ Elf_Off off;
ssize_t shdrsize;
- int rv, i, weird;
- size_t nstrtab_size, nstrtab_nextoff, fn_size;
+ int rv, i, weird, l, m, r, strtabidx;
+ size_t nstrtab_size, nstrtab_nextoff, fn_size, size;
char *nstrtabp = NULL;
unsigned char data;
- Elf_Off maxoff, stroff;
const char *weirdreason = NULL;
+ void *buf;
+ Elf_Half shnum;
rv = 0;
if (xreadatoff(fd, &ehdr, 0, sizeof ehdr, fn) != sizeof ehdr)
goto bad;
data = ehdr.e_ident[EI_DATA];
+ shnum = xe16toh(ehdr.e_shnum);
- shdrsize = xe16toh(ehdr.e_shnum) * xe16toh(ehdr.e_shentsize);
+ shdrsize = shnum * xe16toh(ehdr.e_shentsize);
if ((shdrp = xmalloc(shdrsize, fn, "section header table")) == NULL)
goto bad;
if (xreadatoff(fd, shdrp, xewtoh(ehdr.e_shoff), shdrsize, fn) !=
shdrsize)
goto bad;
- symtabshdr = strtabshdr = NULL;
+ symtabshdr = strtabshdr = shstrtabshdr = NULL;
weird = 0;
- maxoff = stroff = 0;
- for (i = 0; i < xe16toh(ehdr.e_shnum); i++) {
- if (xewtoh(shdrp[i].sh_offset) > maxoff)
- maxoff = xewtoh(shdrp[i].sh_offset);
+ for (i = 0; i < shnum; i++) {
switch (xe32toh(shdrp[i].sh_type)) {
case SHT_SYMTAB:
- if (symtabshdr != NULL)
+ if (symtabshdr != NULL) {
weird = 1;
+ weirdreason = "multiple symbol tables";
+ }
symtabshdr = &shdrp[i];
strtabshdr = &shdrp[xe32toh(shdrp[i].sh_link)];
-
- /* Check whether the string table is the last section */
- stroff = xewtoh(shdrp[xe32toh(shdrp[i].sh_link)].sh_offset);
- if (!weird && xe32toh(shdrp[i].sh_link) != (xe16toh(ehdr.e_shnum) - 1)) {
- weird = 1;
- weirdreason = "string table not last section";
- }
+ break;
+ case SHT_STRTAB:
+ if (i == xe16toh(ehdr.e_shstrndx))
+ shstrtabshdr = &shdrp[i];
break;
}
}
- if (! weirdreason)
- weirdreason = "unsupported";
if (symtabshdr == NULL)
goto out;
- if (strtabshdr == NULL)
+ if (strtabshdr == NULL) {
weird = 1;
- if (!weird && stroff != maxoff) {
+ weirdreason = "string table does not exist";
+ }
+ if (shstrtabshdr == NULL) {
weird = 1;
- weirdreason = "string table section not last in file";
- }
+ weirdreason = "section header string table does not exist";
+ }
+ if (weirdreason == NULL)
+ weirdreason = "unsupported";
if (weird) {
fprintf(stderr, "%s: weird executable (%s)\n", fn, weirdreason);
goto bad;
}
/*
+ * sort section layout table by offset
+ */
+ layoutp = xmalloc((shnum + 1) * sizeof(struct shlayout),
+ fn, "layout table");
+ if (layoutp == NULL)
+ goto bad;
+
+ /* add a pseudo entry to represent the section header table */
+ shdrshdr.sh_offset = ehdr.e_shoff;
+ shdrshdr.sh_size = htoxew(shdrsize);
+ shdrshdr.sh_addralign = htoxew(ELFSIZE / 8);
+ layoutp[shnum].shdr = &shdrshdr;
+
+ /* insert and sort normal section headers */
+ for (i = shnum; i-- != 0;) {
+ l = i + 1;
+ r = shnum;
+ while (l <= r) {
+ m = ( l + r) / 2;
+ if (xewtoh(shdrp[i].sh_offset) >
+ xewtoh(layoutp[m].shdr->sh_offset))
+ l = m + 1;
+ else
+ r = m - 1;
+ }
+
+ if (r != i) {
+ memmove(&layoutp[i], &layoutp[i + 1],
+ sizeof(struct shlayout) * (r - i));
+ }
+
+ layoutp[r].shdr = &shdrp[i];
+ layoutp[r].bufp = NULL;
+ }
+ ++shnum;
+
+ /*
* load up everything we need
*/
- /* symbol table */
- if ((symtabp = xmalloc(xewtoh(symtabshdr->sh_size), fn, "symbol table"))
- == NULL)
+ /* load section string table for debug use */
+ if ((shstrtabp = xmalloc(xewtoh(shstrtabshdr->sh_size), fn,
+ "section string table")) == NULL)
goto bad;
- if (xreadatoff(fd, symtabp, xewtoh(symtabshdr->sh_offset),
- xewtoh(symtabshdr->sh_size), fn) != xewtoh(symtabshdr->sh_size))
+ if ((size_t)xreadatoff(fd, shstrtabp, xewtoh(shstrtabshdr->sh_offset),
+ xewtoh(shstrtabshdr->sh_size), fn) != xewtoh(shstrtabshdr->sh_size))
goto bad;
- /* string table */
- if ((strtabp = xmalloc(xewtoh(strtabshdr->sh_size), fn, "string table"))
- == NULL)
- goto bad;
- if (xreadatoff(fd, strtabp, xewtoh(strtabshdr->sh_offset),
- xewtoh(strtabshdr->sh_size), fn) != xewtoh(strtabshdr->sh_size))
- goto bad;
+ /* we need symtab, strtab, and everything behind strtab */
+ strtabidx = INT_MAX;
+ for (i = 0; i < shnum; i++) {
+ if (layoutp[i].shdr == &shdrshdr) {
+ /* not load section header again */
+ layoutp[i].bufp = shdrp;
+ continue;
+ }
+ if (layoutp[i].shdr == shstrtabshdr) {
+ /* not load section string table again */
+ layoutp[i].bufp = shstrtabp;
+ continue;
+ }
+
+ if (layoutp[i].shdr == strtabshdr)
+ strtabidx = i;
+ if (layoutp[i].shdr == symtabshdr || i >= strtabidx) {
+ off = xewtoh(layoutp[i].shdr->sh_offset);
+ size = xewtoh(layoutp[i].shdr->sh_size);
+ layoutp[i].bufp = xmalloc(size, fn,
+ shstrtabp + xewtoh(layoutp[i].shdr->sh_name));
+ if (layoutp[i].bufp == NULL)
+ goto bad;
+ if ((size_t)xreadatoff(fd, layoutp[i].bufp, off, size, fn) !=
+ size)
+ goto bad;
+
+ /* set symbol table and string table */
+ if (layoutp[i].shdr == symtabshdr)
+ symtabp = layoutp[i].bufp;
+ else if (layoutp[i].shdr == strtabshdr)
+ strtabp = layoutp[i].bufp;
+ }
+ }
nstrtab_size = 256;
nstrtabp = xmalloc(nstrtab_size, fn, "new string table");
@@ -365,26 +431,62 @@ ELFNAMEEND(hide)(int fd, const char *fn)
strtabshdr->sh_size = htoxew(nstrtab_nextoff);
/*
- * write new tables to the file
+ * update section header table in ascending order of offset
*/
- if (xwriteatoff(fd, shdrp, xewtoh(ehdr.e_shoff), shdrsize, fn) !=
- shdrsize)
- goto bad;
- if (xwriteatoff(fd, symtabp, xewtoh(symtabshdr->sh_offset),
- xewtoh(symtabshdr->sh_size), fn) != xewtoh(symtabshdr->sh_size))
- goto bad;
- /* write new symbol table strings */
- if ((size_t)xwriteatoff(fd, nstrtabp, xewtoh(strtabshdr->sh_offset),
- xewtoh(strtabshdr->sh_size), fn) != xewtoh(strtabshdr->sh_size))
- goto bad;
+ for (i = strtabidx + 1; i < shnum; i++) {
+ Elf_Off off, align;
+ off = xewtoh(layoutp[i - 1].shdr->sh_offset) +
+ xewtoh(layoutp[i - 1].shdr->sh_size);
+ align = xewtoh(layoutp[i].shdr->sh_addralign);
+ off = (off + (align - 1)) & ~(align - 1);
+ layoutp[i].shdr->sh_offset = htoxew(off);
+ }
+
+ /*
+ * write data to the file in descending order of offset
+ */
+ for (i = shnum; i-- != 0;) {
+ if (layoutp[i].shdr == strtabshdr) {
+ /* new string table */
+ buf = nstrtabp;
+ } else
+ buf = layoutp[i].bufp;
+
+ if (layoutp[i].shdr == &shdrshdr ||
+ layoutp[i].shdr == symtabshdr || i >= strtabidx) {
+ if (buf == NULL)
+ goto bad;
+
+ /*
+ * update the offset of section header table in elf
+ * header if needed.
+ */
+ if (layoutp[i].shdr == &shdrshdr &&
+ ehdr.e_shoff != shdrshdr.sh_offset) {
+ ehdr.e_shoff = shdrshdr.sh_offset;
+ off = (ELFSIZE == 32) ? 32 : 44;
+ size = sizeof(Elf_Off);
+ if ((size_t)xwriteatoff(fd, &ehdr.e_shoff, off, size,
+ fn) != size)
+ goto bad;
+ }
+
+ off = xewtoh(layoutp[i].shdr->sh_offset);
+ size = xewtoh(layoutp[i].shdr->sh_size);
+ if ((size_t)xwriteatoff(fd, buf, off, size, fn) != size)
+ goto bad;
+ }
+ }
out:
- if (shdrp != NULL)
- free(shdrp);
- if (symtabp != NULL)
- free(symtabp);
- if (strtabp != NULL)
- free(strtabp);
+ if (layoutp != NULL) {
+ for (i = 0; i < shnum; i++) {
+ if (layoutp[i].bufp != NULL)
+ free(layoutp[i].bufp);
+ }
+ free(layoutp);
+ }
+ free(nstrtabp);
return (rv);
bad:
diff --git a/usr.sbin/crunch/examples/really-big.conf b/usr.sbin/crunch/examples/really-big.conf
index fbd7f03..922078a 100644
--- a/usr.sbin/crunch/examples/really-big.conf
+++ b/usr.sbin/crunch/examples/really-big.conf
@@ -72,7 +72,7 @@ progs dev_mkdb diskpart edquota flcopy gettable grfinfo hilinfo htable inetd
progs iostat iteconfig kvm_mkdb mtree named portmap pppd
progs pstat pwd_mkdb quot quotaon rarpd rbootd repquota rmt rpc.bootparamd
progs rwhod sa spray sysctl syslogd tcpdump
-progs traceroute trpt trsp update vipw vnconfig ypbind yppoll ypset
+progs traceroute trpt update vipw vnconfig ypbind yppoll ypset
special amd srcdir /usr/src/usr.sbin/amd/amd
special amd objs vers.amd.o afs_ops.o am_ops.o clock.o util.o xutil.o efs_ops.o mapc.o info_file.o info_hes.o info_ndbm.o info_passwd.o info_nis.o info_union.o map.o srvr_afs.o srvr_nfs.o mntfs.o misc_rpc.o mount_fs.o mtab.o mtab_bsd.o nfs_ops.o nfs_prot_svc.o nfs_start.o nfs_subr.o opts.o pfs_ops.o rpc_fwd.o sched.o sfs_ops.o amq_svc.o amq_subr.o umount_fs.o host_ops.o nfsx_ops.o ufs_ops.o ifs_ops.o amd.o get_args.o restart.o wire.o
diff --git a/usr.sbin/daemon/daemon.c b/usr.sbin/daemon/daemon.c
index 7c8e9fe..b012c88 100644
--- a/usr.sbin/daemon/daemon.c
+++ b/usr.sbin/daemon/daemon.c
@@ -62,7 +62,7 @@ main(int argc, char *argv[])
nochdir = noclose = 1;
restart = 0;
pidfile = user = NULL;
- while ((ch = getopt(argc, argv, "-cfp:ru:")) != -1) {
+ while ((ch = getopt(argc, argv, "cfp:ru:")) != -1) {
switch (ch) {
case 'c':
nochdir = 0;
diff --git a/usr.sbin/edquota/edquota.c b/usr.sbin/edquota/edquota.c
index 930a460..a92a3c8 100644
--- a/usr.sbin/edquota/edquota.c
+++ b/usr.sbin/edquota/edquota.c
@@ -49,8 +49,6 @@ __FBSDID("$FreeBSD$");
* Disk quota editor.
*/
-#include <sys/param.h>
-#include <sys/stat.h>
#include <sys/file.h>
#include <sys/mount.h>
#include <sys/wait.h>
diff --git a/usr.sbin/gssd/Makefile b/usr.sbin/gssd/Makefile
index 3fab810..3663bf9 100644
--- a/usr.sbin/gssd/Makefile
+++ b/usr.sbin/gssd/Makefile
@@ -1,5 +1,7 @@
# $FreeBSD$
+.include <bsd.own.mk>
+
PROG= gssd
MAN= gssd.8
SRCS= gssd.c gssd.h gssd_svc.c gssd_xdr.c gssd_prot.c
@@ -9,6 +11,12 @@ WARNS?= 1
DPADD= ${LIBGSSAPI}
LDADD= -lgssapi
+.if ${MK_KERBEROS_SUPPORT} != "no"
+DPADD+= ${LIBKRB5} ${LIBHX509} ${LIBASN1} ${LIBROKEN} ${LIBCOM_ERR} ${LIBCRYPT} ${LIBCRYPTO}
+LDADD+= -lkrb5 -lhx509 -lasn1 -lroken -lcom_err -lcrypt -lcrypto
+.else
+CFLAGS+= -DWITHOUT_KERBEROS
+.endif
CLEANFILES= gssd_svc.c gssd.h
diff --git a/usr.sbin/gssd/gssd.8 b/usr.sbin/gssd/gssd.8
index 273a9ff..73eb6e3 100644
--- a/usr.sbin/gssd/gssd.8
+++ b/usr.sbin/gssd/gssd.8
@@ -25,7 +25,7 @@
.\"
.\" $FreeBSD$
.\"
-.Dd November 5, 2008
+.Dd December 30, 2012
.Dt GSSD 8
.Os
.Sh NAME
@@ -34,6 +34,9 @@
.Sh SYNOPSIS
.Nm
.Op Fl d
+.Op Fl s Ar dir-list
+.Op Fl c Ar file-substring
+.Op Fl r Ar preferred-realm
.Sh DESCRIPTION
The
.Nm
@@ -46,6 +49,26 @@ Run in debug mode.
In this mode,
.Nm
will not fork when it starts.
+.It Fl s Ar dir-list
+Look for an appropriate credential cache file in this list of directories.
+The list should be full pathnames from root, separated by ':' characters.
+Usually this list will simply be "/tmp".
+Without this option,
+.Nm
+assumes that the credential cache file is called /tmp/krb5cc_<uid>,
+where <uid> is the effective uid for the RPC caller.
+.It Fl c Ar file-substring
+Set a file-substring for the credential cache file names.
+Only files with this substring embedded in their names will be
+selected as candidates when
+.Fl s
+has been specified.
+If not specified, it defaults to "krb5cc_".
+.It Fl r Ar preferred-realm
+Use Kerberos credentials for this realm when searching for
+credentials in directories specified with
+.Fl s .
+If not specified, the default Kerberos realm will be used.
.El
.Sh FILES
.Bl -tag -width ".Pa /etc/krb5.keytab" -compact
diff --git a/usr.sbin/gssd/gssd.c b/usr.sbin/gssd/gssd.c
index ba2805b..9777943 100644
--- a/usr.sbin/gssd/gssd.c
+++ b/usr.sbin/gssd/gssd.c
@@ -33,8 +33,13 @@ __FBSDID("$FreeBSD$");
#include <sys/linker.h>
#include <sys/module.h>
#include <sys/queue.h>
+#include <sys/syslog.h>
#include <ctype.h>
+#include <dirent.h>
#include <err.h>
+#ifndef WITHOUT_KERBEROS
+#include <krb5.h>
+#endif
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
@@ -63,8 +68,12 @@ int gss_resource_count;
uint32_t gss_next_id;
uint32_t gss_start_time;
int debug_level;
+static char ccfile_dirlist[PATH_MAX + 1], ccfile_substring[NAME_MAX + 1];
+static char pref_realm[1024];
static void gssd_load_mech(void);
+static int find_ccache_file(const char *, uid_t, char *);
+static int is_a_valid_tgt_cache(const char *, uid_t, int *, time_t *);
extern void gssd_1(struct svc_req *rqstp, SVCXPRT *transp);
extern int gssd_syscall(char *path);
@@ -81,14 +90,50 @@ main(int argc, char **argv)
int fd, oldmask, ch, debug;
SVCXPRT *xprt;
+ /*
+ * Initialize the credential cache file name substring and the
+ * search directory list.
+ */
+ strlcpy(ccfile_substring, "krb5cc_", sizeof(ccfile_substring));
+ ccfile_dirlist[0] = '\0';
+ pref_realm[0] = '\0';
debug = 0;
- while ((ch = getopt(argc, argv, "d")) != -1) {
+ while ((ch = getopt(argc, argv, "ds:c:r:")) != -1) {
switch (ch) {
case 'd':
debug_level++;
break;
+ case 's':
+#ifndef WITHOUT_KERBEROS
+ /*
+ * Set the directory search list. This enables use of
+ * find_ccache_file() to search the directories for a
+ * suitable credentials cache file.
+ */
+ strlcpy(ccfile_dirlist, optarg, sizeof(ccfile_dirlist));
+#else
+ errx(1, "This option not available when built"
+ " without MK_KERBEROS\n");
+#endif
+ break;
+ case 'c':
+ /*
+ * Specify a non-default credential cache file
+ * substring.
+ */
+ strlcpy(ccfile_substring, optarg,
+ sizeof(ccfile_substring));
+ break;
+ case 'r':
+ /*
+ * Set the preferred realm for the credential cache tgt.
+ */
+ strlcpy(pref_realm, optarg, sizeof(pref_realm));
+ break;
default:
- fprintf(stderr, "usage: %s [-d]\n", argv[0]);
+ fprintf(stderr,
+ "usage: %s [-d] [-s dir-list] [-c file-substring]"
+ " [-r preferred-realm]\n", argv[0]);
exit(1);
break;
}
@@ -106,21 +151,43 @@ main(int argc, char **argv)
sun.sun_len = SUN_LEN(&sun);
fd = socket(AF_LOCAL, SOCK_STREAM, 0);
if (!fd) {
+ if (debug_level == 0) {
+ syslog(LOG_ERR, "Can't create local gssd socket");
+ exit(1);
+ }
err(1, "Can't create local gssd socket");
}
oldmask = umask(S_IXUSR|S_IRWXG|S_IRWXO);
if (bind(fd, (struct sockaddr *) &sun, sun.sun_len) < 0) {
+ if (debug_level == 0) {
+ syslog(LOG_ERR, "Can't bind local gssd socket");
+ exit(1);
+ }
err(1, "Can't bind local gssd socket");
}
umask(oldmask);
if (listen(fd, SOMAXCONN) < 0) {
+ if (debug_level == 0) {
+ syslog(LOG_ERR, "Can't listen on local gssd socket");
+ exit(1);
+ }
err(1, "Can't listen on local gssd socket");
}
xprt = svc_vc_create(fd, RPC_MAXDATASIZE, RPC_MAXDATASIZE);
if (!xprt) {
+ if (debug_level == 0) {
+ syslog(LOG_ERR,
+ "Can't create transport for local gssd socket");
+ exit(1);
+ }
err(1, "Can't create transport for local gssd socket");
}
if (!svc_reg(xprt, GSSD, GSSDVERS, gssd_1, NULL)) {
+ if (debug_level == 0) {
+ syslog(LOG_ERR,
+ "Can't register service for local gssd socket");
+ exit(1);
+ }
err(1, "Can't register service for local gssd socket");
}
@@ -244,13 +311,52 @@ gssd_init_sec_context_1_svc(init_sec_context_args *argp, init_sec_context_res *r
gss_cred_id_t cred = GSS_C_NO_CREDENTIAL;
gss_ctx_id_t ctx = GSS_C_NO_CONTEXT;
gss_name_t name = GSS_C_NO_NAME;
- char ccname[strlen("FILE:/tmp/krb5cc_") + 6 + 1];
+ char ccname[PATH_MAX + 5 + 1], *cp, *cp2;
+ int gotone;
- snprintf(ccname, sizeof(ccname), "FILE:/tmp/krb5cc_%d",
- (int) argp->uid);
+ memset(result, 0, sizeof(*result));
+ if (ccfile_dirlist[0] != '\0' && argp->cred == 0) {
+ /*
+ * For the "-s" case and no credentials provided as an
+ * argument, search the directory list for an appropriate
+ * credential cache file. If the search fails, return failure.
+ */
+ gotone = 0;
+ cp = ccfile_dirlist;
+ do {
+ cp2 = strchr(cp, ':');
+ if (cp2 != NULL)
+ *cp2 = '\0';
+ gotone = find_ccache_file(cp, argp->uid, ccname);
+ if (gotone != 0)
+ break;
+ if (cp2 != NULL)
+ *cp2++ = ':';
+ cp = cp2;
+ } while (cp != NULL && *cp != '\0');
+ if (gotone == 0) {
+ result->major_status = GSS_S_CREDENTIALS_EXPIRED;
+ return (TRUE);
+ }
+ } else {
+ /*
+ * If there wasn't a "-s" option or the credentials have
+ * been provided as an argument, do it the old way.
+ * When credentials are provided, the uid should be root.
+ */
+ if (argp->cred != 0 && argp->uid != 0) {
+ if (debug_level == 0)
+ syslog(LOG_ERR, "gss_init_sec_context:"
+ " cred for non-root");
+ else
+ fprintf(stderr, "gss_init_sec_context:"
+ " cred for non-root\n");
+ }
+ snprintf(ccname, sizeof(ccname), "FILE:/tmp/krb5cc_%d",
+ (int) argp->uid);
+ }
setenv("KRB5CCNAME", ccname, TRUE);
- memset(result, 0, sizeof(*result));
if (argp->cred) {
cred = gssd_find_resource(argp->cred);
if (!cred) {
@@ -273,7 +379,6 @@ gssd_init_sec_context_1_svc(init_sec_context_args *argp, init_sec_context_res *r
}
}
- memset(result, 0, sizeof(*result));
result->major_status = gss_init_sec_context(&result->minor_status,
cred, &ctx, name, argp->mech_type,
argp->req_flags, argp->time_req, argp->input_chan_bindings,
@@ -493,13 +598,53 @@ gssd_acquire_cred_1_svc(acquire_cred_args *argp, acquire_cred_res *result, struc
{
gss_name_t desired_name = GSS_C_NO_NAME;
gss_cred_id_t cred;
- char ccname[strlen("FILE:/tmp/krb5cc_") + 6 + 1];
+ char ccname[PATH_MAX + 5 + 1], *cp, *cp2;
+ int gotone;
- snprintf(ccname, sizeof(ccname), "FILE:/tmp/krb5cc_%d",
- (int) argp->uid);
+ memset(result, 0, sizeof(*result));
+ if (ccfile_dirlist[0] != '\0' && argp->desired_name == 0) {
+ /*
+ * For the "-s" case and no name provided as an
+ * argument, search the directory list for an appropriate
+ * credential cache file. If the search fails, return failure.
+ */
+ gotone = 0;
+ cp = ccfile_dirlist;
+ do {
+ cp2 = strchr(cp, ':');
+ if (cp2 != NULL)
+ *cp2 = '\0';
+ gotone = find_ccache_file(cp, argp->uid, ccname);
+ if (gotone != 0)
+ break;
+ if (cp2 != NULL)
+ *cp2++ = ':';
+ cp = cp2;
+ } while (cp != NULL && *cp != '\0');
+ if (gotone == 0) {
+ result->major_status = GSS_S_CREDENTIALS_EXPIRED;
+ return (TRUE);
+ }
+ } else {
+ /*
+ * If there wasn't a "-s" option or the name has
+ * been provided as an argument, do it the old way.
+ * When a name is provided, it will normally exist in the
+ * default keytab file and the uid will be root.
+ */
+ if (argp->desired_name != 0 && argp->uid != 0) {
+ if (debug_level == 0)
+ syslog(LOG_ERR, "gss_acquire_cred:"
+ " principal_name for non-root");
+ else
+ fprintf(stderr, "gss_acquire_cred:"
+ " principal_name for non-root\n");
+ }
+ snprintf(ccname, sizeof(ccname), "FILE:/tmp/krb5cc_%d",
+ (int) argp->uid);
+ }
setenv("KRB5CCNAME", ccname, TRUE);
- memset(result, 0, sizeof(*result));
if (argp->desired_name) {
desired_name = gssd_find_resource(argp->desired_name);
if (!desired_name) {
@@ -608,3 +753,176 @@ gssd_1_freeresult(SVCXPRT *transp, xdrproc_t xdr_result, caddr_t result)
return (TRUE);
}
+
+/*
+ * Search a directory for the most likely candidate to be used as the
+ * credential cache for a uid. If successful, return 1 and fill the
+ * file's path id into "rpath". Otherwise, return 0.
+ */
+static int
+find_ccache_file(const char *dirpath, uid_t uid, char *rpath)
+{
+ DIR *dirp;
+ struct dirent *dp;
+ struct stat sb;
+ time_t exptime, oexptime;
+ int gotone, len, rating, orating;
+ char namepath[PATH_MAX + 5 + 1];
+ char retpath[PATH_MAX + 5 + 1];
+
+ dirp = opendir(dirpath);
+ if (dirp == NULL)
+ return (0);
+ gotone = 0;
+ orating = 0;
+ oexptime = 0;
+ while ((dp = readdir(dirp)) != NULL) {
+ len = snprintf(namepath, sizeof(namepath), "%s/%s", dirpath,
+ dp->d_name);
+ if (len < sizeof(namepath) &&
+ strstr(dp->d_name, ccfile_substring) != NULL &&
+ lstat(namepath, &sb) >= 0 &&
+ sb.st_uid == uid &&
+ S_ISREG(sb.st_mode)) {
+ len = snprintf(namepath, sizeof(namepath), "FILE:%s/%s",
+ dirpath, dp->d_name);
+ if (len < sizeof(namepath) &&
+ is_a_valid_tgt_cache(namepath, uid, &rating,
+ &exptime) != 0) {
+ if (gotone == 0 || rating > orating ||
+ (rating == orating && exptime > oexptime)) {
+ orating = rating;
+ oexptime = exptime;
+ strcpy(retpath, namepath);
+ gotone = 1;
+ }
+ }
+ }
+ }
+ closedir(dirp);
+ if (gotone != 0) {
+ strcpy(rpath, retpath);
+ return (1);
+ }
+ return (0);
+}
+
+/*
+ * Try to determine if the file is a valid tgt cache file.
+ * Check that the file has a valid tgt for a principal.
+ * If it does, return 1, otherwise return 0.
+ * It also returns a "rating" and the expiry time for the TGT, when found.
+ * This "rating" is higher based on heuristics that make it more
+ * likely to be the correct credential cache file to use. It can
+ * be used by the caller, along with expiry time, to select from
+ * multiple credential cache files.
+ */
+static int
+is_a_valid_tgt_cache(const char *filepath, uid_t uid, int *retrating,
+ time_t *retexptime)
+{
+#ifndef WITHOUT_KERBEROS
+ krb5_context context;
+ krb5_principal princ;
+ krb5_ccache ccache;
+ krb5_error_code retval;
+ krb5_cc_cursor curse;
+ krb5_creds krbcred;
+ int gotone, orating, rating, ret;
+ struct passwd *pw;
+ char *cp, *cp2, *pname;
+ time_t exptime;
+
+ /* Find a likely name for the uid principal. */
+ pw = getpwuid(uid);
+
+ /*
+ * Do a bunch of krb5 library stuff to try and determine if
+ * this file is a credentials cache with an appropriate TGT
+ * in it.
+ */
+ retval = krb5_init_context(&context);
+ if (retval != 0)
+ return (0);
+ retval = krb5_cc_resolve(context, filepath, &ccache);
+ if (retval != 0) {
+ krb5_free_context(context);
+ return (0);
+ }
+ ret = 0;
+ orating = 0;
+ exptime = 0;
+ retval = krb5_cc_start_seq_get(context, ccache, &curse);
+ if (retval == 0) {
+ while ((retval = krb5_cc_next_cred(context, ccache, &curse,
+ &krbcred)) == 0) {
+ gotone = 0;
+ rating = 0;
+ retval = krb5_unparse_name(context, krbcred.server,
+ &pname);
+ if (retval == 0) {
+ cp = strchr(pname, '/');
+ if (cp != NULL) {
+ *cp++ = '\0';
+ if (strcmp(pname, "krbtgt") == 0 &&
+ krbcred.times.endtime > time(NULL)
+ ) {
+ gotone = 1;
+ /*
+ * Test to see if this is a
+ * tgt for cross-realm auth.
+ * Rate it higher, if it is not.
+ */
+ cp2 = strchr(cp, '@');
+ if (cp2 != NULL) {
+ *cp2++ = '\0';
+ if (strcmp(cp, cp2) ==
+ 0)
+ rating++;
+ }
+ }
+ }
+ free(pname);
+ }
+ if (gotone != 0) {
+ retval = krb5_unparse_name(context,
+ krbcred.client, &pname);
+ if (retval == 0) {
+ cp = strchr(pname, '@');
+ if (cp != NULL) {
+ *cp++ = '\0';
+ if (pw != NULL && strcmp(pname,
+ pw->pw_name) == 0)
+ rating++;
+ if (strchr(pname, '/') == NULL)
+ rating++;
+ if (pref_realm[0] != '\0' &&
+ strcmp(cp, pref_realm) == 0)
+ rating++;
+ }
+ }
+ free(pname);
+ if (rating > orating) {
+ orating = rating;
+ exptime = krbcred.times.endtime;
+ } else if (rating == orating &&
+ krbcred.times.endtime > exptime)
+ exptime = krbcred.times.endtime;
+ ret = 1;
+ }
+ krb5_free_cred_contents(context, &krbcred);
+ }
+ krb5_cc_end_seq_get(context, ccache, &curse);
+ }
+ krb5_cc_close(context, ccache);
+ krb5_free_context(context);
+ if (ret != 0) {
+ *retrating = orating;
+ *retexptime = exptime;
+ }
+ return (ret);
+#else /* WITHOUT_KERBEROS */
+ return (0);
+#endif /* !WITHOUT_KERBEROS */
+}
+
diff --git a/usr.sbin/ifmcstat/ifmcstat.c b/usr.sbin/ifmcstat/ifmcstat.c
index 838c83d..599e850 100644
--- a/usr.sbin/ifmcstat/ifmcstat.c
+++ b/usr.sbin/ifmcstat/ifmcstat.c
@@ -167,7 +167,7 @@ static const char * inm_mode(u_int mode);
#endif
#ifdef INET6
static void in6_ifinfo(struct mld_ifinfo *);
-static const char * inet6_n2a(struct in6_addr *);
+static const char * inet6_n2a(struct in6_addr *, uint32_t);
#endif
int main(int, char **);
@@ -296,7 +296,8 @@ in_ifinfo(struct igmp_ifinfo *igi)
printf("igmpv?(%d)", igi->igi_version);
break;
}
- printb(" flags", igi->igi_flags, "\020\1SILENT\2LOOPBACK");
+ if (igi->igi_flags)
+ printb(" flags", igi->igi_flags, "\020\1SILENT\2LOOPBACK");
if (igi->igi_version == IGMP_VERSION_3) {
printf(" rv %u qi %u qri %u uri %u",
igi->igi_rv, igi->igi_qi, igi->igi_qri, igi->igi_uri);
@@ -459,7 +460,8 @@ if6_addrlist(struct ifaddr *ifap)
if (sa.sa_family != PF_INET6)
goto nextifap;
KREAD(ifap, &if6a, struct in6_ifaddr);
- printf("\tinet6 %s\n", inet6_n2a(&if6a.ia_addr.sin6_addr));
+ printf("\tinet6 %s\n", inet6_n2a(&if6a.ia_addr.sin6_addr,
+ if6a.ia_addr.sin6_scope_id));
/*
* Print per-link MLD information, if available.
*/
@@ -514,7 +516,7 @@ in6_multientry(struct in6_multi *mc)
struct in6_multi multi;
KREAD(mc, &multi, struct in6_multi);
- printf("\t\tgroup %s", inet6_n2a(&multi.in6m_addr));
+ printf("\t\tgroup %s", inet6_n2a(&multi.in6m_addr, 0));
printf(" refcnt %u\n", multi.in6m_refcount);
return (multi.in6m_entry.le_next);
@@ -751,7 +753,8 @@ in6_ifinfo(struct mld_ifinfo *mli)
printf("mldv?(%d)", mli->mli_version);
break;
}
- printb(" flags", mli->mli_flags, "\020\1SILENT");
+ if (mli->mli_flags)
+ printb(" flags", mli->mli_flags, "\020\1SILENT\2USEALLOW");
if (mli->mli_version == MLD_VERSION_2) {
printf(" rv %u qi %u qri %u uri %u",
mli->mli_rv, mli->mli_qi, mli->mli_qri, mli->mli_uri);
@@ -764,26 +767,17 @@ in6_ifinfo(struct mld_ifinfo *mli)
}
static const char *
-inet6_n2a(struct in6_addr *p)
+inet6_n2a(struct in6_addr *p, uint32_t scope_id)
{
static char buf[NI_MAXHOST];
struct sockaddr_in6 sin6;
- u_int32_t scopeid;
const int niflags = NI_NUMERICHOST;
memset(&sin6, 0, sizeof(sin6));
sin6.sin6_family = AF_INET6;
sin6.sin6_len = sizeof(struct sockaddr_in6);
sin6.sin6_addr = *p;
- if (IN6_IS_ADDR_LINKLOCAL(p) || IN6_IS_ADDR_MC_LINKLOCAL(p) ||
- IN6_IS_ADDR_MC_NODELOCAL(p)) {
- scopeid = ntohs(*(u_int16_t *)&sin6.sin6_addr.s6_addr[2]);
- if (scopeid) {
- sin6.sin6_scope_id = scopeid;
- sin6.sin6_addr.s6_addr[2] = 0;
- sin6.sin6_addr.s6_addr[3] = 0;
- }
- }
+ sin6.sin6_scope_id = scope_id;
if (getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len,
buf, sizeof(buf), NULL, 0, niflags) == 0) {
return (buf);
@@ -1115,7 +1109,8 @@ ifmcstat_getifmaddrs(void)
#ifdef INET6
{
const char *p =
- inet6_n2a(&pifasa->sin6.sin6_addr);
+ inet6_n2a(&pifasa->sin6.sin6_addr,
+ pifasa->sin6.sin6_scope_id);
strlcpy(addrbuf, p, sizeof(addrbuf));
break;
}
@@ -1136,7 +1131,14 @@ ifmcstat_getifmaddrs(void)
break;
}
- fprintf(stdout, "\t%s %s\n", pafname, addrbuf);
+ fprintf(stdout, "\t%s %s", pafname, addrbuf);
+#ifdef INET6
+ if (pifasa->sa.sa_family == AF_INET6 &&
+ pifasa->sin6.sin6_scope_id)
+ fprintf(stdout, " scopeid 0x%x",
+ pifasa->sin6.sin6_scope_id);
+#endif
+ fprintf(stdout, "\n");
#ifdef INET
/*
* Print per-link IGMP information, if available.
@@ -1196,7 +1198,8 @@ next_ifnet:
/* Print this group address. */
#ifdef INET6
if (pgsa->sa.sa_family == AF_INET6) {
- const char *p = inet6_n2a(&pgsa->sin6.sin6_addr);
+ const char *p = inet6_n2a(&pgsa->sin6.sin6_addr,
+ pgsa->sin6.sin6_scope_id);
strlcpy(addrbuf, p, sizeof(addrbuf));
} else
#endif
@@ -1208,6 +1211,12 @@ next_ifnet:
}
fprintf(stdout, "\t\tgroup %s", addrbuf);
+#ifdef INET6
+ if (pgsa->sa.sa_family == AF_INET6 &&
+ pgsa->sin6.sin6_scope_id)
+ fprintf(stdout, " scopeid 0x%x",
+ pgsa->sin6.sin6_scope_id);
+#endif
#ifdef INET
if (pgsa->sa.sa_family == AF_INET) {
inm_print_sources_sysctl(thisifindex,
diff --git a/usr.sbin/inetd/inetd.c b/usr.sbin/inetd/inetd.c
index 6b28764..eebcfea 100644
--- a/usr.sbin/inetd/inetd.c
+++ b/usr.sbin/inetd/inetd.c
@@ -1387,7 +1387,7 @@ setsockopt(fd, SOL_SOCKET, opt, (char *)&on, sizeof (on))
}
}
if (sep->se_socktype == SOCK_STREAM)
- listen(sep->se_fd, 64);
+ listen(sep->se_fd, -1);
enable(sep);
if (debug) {
warnx("registered %s on %d",
diff --git a/usr.sbin/iostat/iostat.8 b/usr.sbin/iostat/iostat.8
index d8957ef..1d2d517 100644
--- a/usr.sbin/iostat/iostat.8
+++ b/usr.sbin/iostat/iostat.8
@@ -56,7 +56,7 @@
.\"
.\" @(#)iostat.8 8.1 (Berkeley) 6/6/93
.\"
-.Dd November 24, 2010
+.Dd December 15, 2012
.Dt IOSTAT 8
.Os
.Sh NAME
@@ -366,6 +366,32 @@ average duration of transactions, in milliseconds
% of time the device had one or more outstanding transactions
.El
.Pp
+The extended
+.Nm
+device display, with the
+.Fl x
+and
+.Fl I
+flags specified, shows the following statistics:
+.Pp
+.Bl -tag -width indent -compact
+.It r/i
+read operations per time period
+.It w/i
+write operations per time period
+.It kr/i
+kilobytes read per time period
+.It kw/i
+kilobytes write per time period
+.It qlen
+transactions queue length
+.It tsvc_t/i
+total duration of transactions per time period, in seconds
+.It sb/i
+total time the device had one or more outstanding transactions per
+time period, in seconds
+.El
+.Pp
The old-style
.Nm
display (using
diff --git a/usr.sbin/iostat/iostat.c b/usr.sbin/iostat/iostat.c
index 1c32f94..92b98e4 100644
--- a/usr.sbin/iostat/iostat.c
+++ b/usr.sbin/iostat/iostat.c
@@ -731,10 +731,10 @@ devstats(int perf_select, long double etime, int havelast)
u_int64_t total_bytes, total_transfers, total_blocks;
u_int64_t total_bytes_read, total_transfers_read;
u_int64_t total_bytes_write, total_transfers_write;
- long double busy_pct;
+ long double busy_pct, busy_time;
u_int64_t queue_len;
long double total_mb;
- long double blocks_per_second, ms_per_transaction;
+ long double blocks_per_second, ms_per_transaction, total_duration;
int firstline = 1;
char *devname;
@@ -745,14 +745,13 @@ devstats(int perf_select, long double etime, int havelast)
if (Cflag > 0)
printf(" cpu ");
printf("\n");
- if (Iflag == 0)
- printf(
- "device r/s w/s kr/s kw/s qlen svc_t %%b "
- );
- else
- printf(
- "device r/i w/i kr/i kw/i qlen svc_t %%b "
- );
+ if (Iflag == 0) {
+ printf("device r/s w/s kr/s kw/s qlen "
+ "svc_t %%b ");
+ } else {
+ printf("device r/i w/i kr/i"
+ " kw/i qlen tsvc_t/i sb/i ");
+ }
if (Tflag > 0)
printf("tin tout ");
if (Cflag > 0)
@@ -789,6 +788,8 @@ devstats(int perf_select, long double etime, int havelast)
DSM_MS_PER_TRANSACTION, &ms_per_transaction,
DSM_BUSY_PCT, &busy_pct,
DSM_QUEUE_LENGTH, &queue_len,
+ DSM_TOTAL_DURATION, &total_duration,
+ DSM_TOTAL_BUSY_TIME, &busy_time,
DSM_NONE) != 0)
errx(1, "%s", devstat_errbuf);
@@ -827,7 +828,9 @@ devstats(int perf_select, long double etime, int havelast)
queue_len,
ms_per_transaction, busy_pct);
else
- printf("%-8.8s %5.1Lf %5.1Lf %7.1Lf %7.1Lf %4" PRIu64 " %5.1Lf %3.0Lf ",
+ printf("%-8.8s %11.1Lf %11.1Lf "
+ "%12.1Lf %12.1Lf %4" PRIu64
+ " %10.1Lf %9.1Lf ",
devname,
(long double)total_transfers_read,
(long double)total_transfers_write,
@@ -836,7 +839,7 @@ devstats(int perf_select, long double etime, int havelast)
(long double)
total_bytes_write / 1024,
queue_len,
- ms_per_transaction, busy_pct);
+ total_duration, busy_time);
if (firstline) {
/*
* If this is the first device
diff --git a/usr.sbin/mergemaster/mergemaster.sh b/usr.sbin/mergemaster/mergemaster.sh
index 1723fee..48b58f3 100755
--- a/usr.sbin/mergemaster/mergemaster.sh
+++ b/usr.sbin/mergemaster/mergemaster.sh
@@ -421,11 +421,11 @@ check_pager () {
echo " I cannot execute it. So, what would you like to do?"
echo ''
echo " Use 'e' to exit mergemaster and fix your PAGER variable"
- if [ -x /usr/bin/less -o -x /usr/local/bin/less ]; then
echo " Use 'l' to set PAGER to 'less' for this run"
- fi
echo " Use 'm' to use plain old 'more' as your PAGER for this run"
echo ''
+ echo " or you may type an absolute path to PAGER for this run"
+ echo ''
echo " Default is to use plain old 'more' "
echo ''
echo -n "What should I do? [Use 'more'] "
@@ -436,21 +436,14 @@ check_pager () {
exit 0
;;
[lL])
- if [ -x /usr/bin/less ]; then
- PAGER=/usr/bin/less
- elif [ -x /usr/local/bin/less ]; then
- PAGER=/usr/local/bin/less
- else
- echo ''
- echo " *** Fatal Error:"
- echo " You asked to use 'less' as your pager, but I can't"
- echo " find it in /usr/bin or /usr/local/bin"
- exit 1
- fi
+ PAGER=less
;;
[mM]|'')
PAGER=more
;;
+ /*)
+ PAGER="$FIXPAGER"
+ ;;
*)
echo ''
echo "invalid choice: ${FIXPAGER}"
diff --git a/usr.sbin/mountd/exports.5 b/usr.sbin/mountd/exports.5
index 75a5121..1293524 100644
--- a/usr.sbin/mountd/exports.5
+++ b/usr.sbin/mountd/exports.5
@@ -28,7 +28,7 @@
.\" @(#)exports.5 8.3 (Berkeley) 3/29/95
.\" $FreeBSD$
.\"
-.Dd July 12, 2011
+.Dd December 23, 2012
.Dt EXPORTS 5
.Os
.Sh NAME
@@ -311,12 +311,15 @@ For the third form which specifies the NFSv4 tree root, the directory path
specifies the location within the server's file system tree which is the
root of the NFSv4 tree.
All entries of this form must specify the same directory path.
-This location can be any directory and does not
+For file systems other than ZFS,
+this location can be any directory and does not
need to be within an exported file system. If it is not in an exported
file system, a very limited set of operations are permitted, so that an
NFSv4 client can traverse the tree to an exported file system.
Although parts of the NFSv4 tree can be non-exported, the entire NFSv4 tree
must consist of local file systems capable of being exported via NFS.
+All ZFS file systems in the subtree below the NFSv4 tree root must be
+exported.
NFSv4 does not use the mount protocol and does permit clients to cross server
mount point boundaries, although not all clients are capable of crossing the
mount points.
diff --git a/usr.sbin/mountd/mountd.c b/usr.sbin/mountd/mountd.c
index 543fc14..bc290d6 100644
--- a/usr.sbin/mountd/mountd.c
+++ b/usr.sbin/mountd/mountd.c
@@ -649,7 +649,7 @@ create_service(struct netconfig *nconf)
*/
if ((fd = __rpc_nconf2fd(nconf)) < 0) {
int non_fatal = 0;
- if (errno == EPROTONOSUPPORT &&
+ if (errno == EAFNOSUPPORT &&
nconf->nc_semantics != NC_TPI_CLTS)
non_fatal = 1;
diff --git a/usr.sbin/moused/moused.8 b/usr.sbin/moused/moused.8
index 30b2200..5e4dd43 100644
--- a/usr.sbin/moused/moused.8
+++ b/usr.sbin/moused/moused.8
@@ -296,7 +296,7 @@ information are:
.Bl -tag -compact -width modelxxx
.It Ar port
Port (device file) name, i.e.\&
-.Pa /dev/cuad0 ,
+.Pa /dev/cuau0 ,
.Pa /dev/mse0
and
.Pa /dev/psm0 .
@@ -564,7 +564,7 @@ There may be more than one serial port to which the serial
mouse can be attached.
Many people often assign the first, built-in
serial port
-.Pa /dev/cuad0
+.Pa /dev/cuau0
to the mouse.
You can attach multiple USB mice to your system or to your USB hub.
They are accessible as
@@ -731,16 +731,16 @@ utility
UNIX-domain stream socket for X10 MouseRemote events
.El
.Sh EXAMPLES
-.Dl "moused -p /dev/cuad0 -i type"
+.Dl "moused -p /dev/cuau0 -i type"
.Pp
Let the
.Nm
utility determine the protocol type of the mouse at the serial port
-.Pa /dev/cuad0 .
+.Pa /dev/cuau0 .
If successful, the command will print the type, otherwise it will say
.Dq Li unknown .
.Bd -literal -offset indent
-moused -p /dev/cuad0
+moused -p /dev/cuau0
vidcontrol -m on
.Ed
.Pp
diff --git a/usr.sbin/mptable/mptable.c b/usr.sbin/mptable/mptable.c
index 3c38ccf..01fa897 100644
--- a/usr.sbin/mptable/mptable.c
+++ b/usr.sbin/mptable/mptable.c
@@ -42,7 +42,7 @@ static const char rcsid[] =
#define EXTENDED_PROCESSING_READY
#define OEM_PROCESSING_READY_NOT
-#include <sys/types.h>
+#include <sys/param.h>
#include <err.h>
#include <fcntl.h>
#include <paths.h>
@@ -270,8 +270,8 @@ static void pnstr( char* s, int c );
/* global data */
static int pfd; /* physical /dev/mem fd */
-static int busses[16];
-static int apics[16];
+static int busses[256];
+static int apics[256];
static int ncpu;
static int nbus;
@@ -710,10 +710,12 @@ MPConfigTableHeader( u_int32_t pap )
printf( "MP Config Base Table Entries:\n\n" );
- /* initialze tables */
- for ( x = 0; x < 16; ++x ) {
- busses[ x ] = apics[ x ] = 0xff;
- }
+ /* initialize tables */
+ for (x = 0; x < (int)nitems(busses); x++)
+ busses[x] = 0xff;
+
+ for (x = 0; x < (int)nitems(apics); x++)
+ apics[x] = 0xff;
ncpu = 0;
nbus = 0;
diff --git a/usr.sbin/mtest/mtest.c b/usr.sbin/mtest/mtest.c
index 60f7e09..a28fab7 100644
--- a/usr.sbin/mtest/mtest.c
+++ b/usr.sbin/mtest/mtest.c
@@ -204,12 +204,12 @@ main(int argc, char **argv)
s6 = -1;
#ifdef INET
s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
- if (s == -1 && errno != EPROTONOSUPPORT)
+ if (s == -1 && errno != EAFNOSUPPORT)
err(1, "can't open IPv4 socket");
#endif
#ifdef INET6
s6 = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
- if (s6 == -1 && errno != EPROTONOSUPPORT)
+ if (s6 == -1 && errno != EAFNOSUPPORT)
err(1, "can't open IPv6 socket");
#endif
if (s == -1 && s6 == -1)
diff --git a/usr.sbin/mtree/Makefile b/usr.sbin/mtree/Makefile
index cbc4fd9..c378455 100644
--- a/usr.sbin/mtree/Makefile
+++ b/usr.sbin/mtree/Makefile
@@ -1,10 +1,12 @@
# From: @(#)Makefile 8.1 (Berkeley) 6/6/93
# $FreeBSD$
+.include <bsd.own.mk>
+
.PATH: ${.CURDIR}/../../usr.bin/cksum
-PROG= mtree
-MAN= mtree.8 mtree.5
+PROG= fmtree
+MAN= fmtree.8 mtree.5
SRCS= compare.c crc.c create.c excludes.c misc.c mtree.c spec.c verify.c
SRCS+= specspec.c
@@ -12,4 +14,14 @@ CFLAGS+= -DMD5 -DSHA1 -DRMD160 -DSHA256
DPADD= ${LIBMD}
LDADD= -lmd
+.if ${MK_NMTREE} == "no"
+LINKS= ${BINDIR}/fmtree ${BINDIR}/mtree
+MLINKS= fmtree.8 mtree.8
+.endif
+
+CLEANFILES+= fmtree.8
+
+fmtree.8: mtree.8
+ cp ${.ALLSRC} ${.TARGET}
+
.include <bsd.prog.mk>
diff --git a/usr.sbin/ndiscvt/inf-parse.y b/usr.sbin/ndiscvt/inf-parse.y
index 8a84956..2a8876d 100644
--- a/usr.sbin/ndiscvt/inf-parse.y
+++ b/usr.sbin/ndiscvt/inf-parse.y
@@ -40,7 +40,6 @@ __FBSDID("$FreeBSD$");
#include "inf.h"
-extern int yyparse (void);
extern int yylex (void);
extern void yyerror(const char *);
%}
diff --git a/usr.sbin/ndiscvt/ndiscvt.c b/usr.sbin/ndiscvt/ndiscvt.c
index 3775a33..7636c4c 100644
--- a/usr.sbin/ndiscvt/ndiscvt.c
+++ b/usr.sbin/ndiscvt/ndiscvt.c
@@ -325,6 +325,8 @@ main(int argc, char *argv[])
rewind (fp);
img = calloc(fsize, 1);
n = fread (img, fsize, 1, fp);
+ if (n == 0)
+ err(1, "reading .SYS file '%s' failed", sysfile);
fclose(fp);
diff --git a/usr.sbin/ndp/ndp.8 b/usr.sbin/ndp/ndp.8
index 1fb63f81..223cfba 100644
--- a/usr.sbin/ndp/ndp.8
+++ b/usr.sbin/ndp/ndp.8
@@ -29,7 +29,7 @@
.\"
.\" $FreeBSD$
.\"
-.Dd July 9, 2012
+.Dd Jan 10, 2013
.Dt NDP 8
.Os
.\"
@@ -192,6 +192,15 @@ on
This flag is set by
.Va net.inet6.ip6.auto_linklocal
sysctl variable.
+.It Ic no_prefer_iface
+The address on the outgoing interface is preferred by source addess
+selection rule.
+If this flag is set, stop treating the address on the
+.Ar interface
+as special even when the
+.Ar interface
+is outgoing interface.
+The default value of this flag is off.
.It Ic disabled
Disable IPv6 operation on the interface.
When disabled, the interface discards any IPv6 packets
@@ -231,8 +240,8 @@ responding to requests for
.Ar hostname
even though the host address is not its own.
.It Fl t
-Print timestamp on each entry,
-making it possible to merge output with
+Print timestamp for each entry,
+to make it possible to merge the output with
.Xr tcpdump 1 .
Most useful when used with
.Fl A .
diff --git a/usr.sbin/ndp/ndp.c b/usr.sbin/ndp/ndp.c
index be21d0f..0449c12 100644
--- a/usr.sbin/ndp/ndp.c
+++ b/usr.sbin/ndp/ndp.c
@@ -404,12 +404,8 @@ set(argc, argv)
return 1;
}
sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
-#ifdef __KAME__
- if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)) {
- *(u_int16_t *)&sin->sin6_addr.s6_addr[2] =
- htons(((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id);
- }
-#endif
+ sin->sin6_scope_id =
+ ((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id;
ea = (u_char *)LLADDR(&sdl_m);
if (ndp_ether_aton(eaddr, ea) == 0)
sdl_m.sdl_alen = 6;
@@ -440,9 +436,6 @@ set(argc, argv)
goto overwrite;
}
}
- /*
- * IPv4 arp command retries with sin_other = SIN_PROXY here.
- */
fprintf(stderr, "set: cannot configure a new entry\n");
return 1;
}
@@ -478,12 +471,6 @@ get(host)
return;
}
sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
-#ifdef __KAME__
- if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)) {
- *(u_int16_t *)&sin->sin6_addr.s6_addr[2] =
- htons(((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id);
- }
-#endif
dump(&sin->sin6_addr, 0);
if (found_entry == 0) {
getnameinfo((struct sockaddr *)sin, sin->sin6_len, host_buf,
@@ -520,12 +507,8 @@ delete(host)
return 1;
}
sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
-#ifdef __KAME__
- if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)) {
- *(u_int16_t *)&sin->sin6_addr.s6_addr[2] =
- htons(((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id);
- }
-#endif
+ sin->sin6_scope_id =
+ ((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id;
if (rtmsg(RTM_GET) < 0) {
errx(1, "RTM_GET(%s) failed", host);
/* NOTREACHED */
@@ -537,9 +520,6 @@ delete(host)
!(rtm->rtm_flags & RTF_GATEWAY)) {
goto delete;
}
- /*
- * IPv4 arp command retries with sin_other = SIN_PROXY here.
- */
fprintf(stderr, "delete: cannot delete non-NDP entry\n");
return 1;
}
@@ -556,16 +536,8 @@ delete:
NEXTADDR(RTA_DST, sin_m);
rtm->rtm_flags |= RTF_LLDATA;
if (rtmsg(RTM_DELETE) == 0) {
- struct sockaddr_in6 s6 = *sin; /* XXX: for safety */
-
-#ifdef __KAME__
- if (IN6_IS_ADDR_LINKLOCAL(&s6.sin6_addr)) {
- s6.sin6_scope_id = ntohs(*(u_int16_t *)&s6.sin6_addr.s6_addr[2]);
- *(u_int16_t *)&s6.sin6_addr.s6_addr[2] = 0;
- }
-#endif
- getnameinfo((struct sockaddr *)&s6,
- s6.sin6_len, host_buf,
+ getnameinfo((struct sockaddr *)sin,
+ sin->sin6_len, host_buf,
sizeof(host_buf), NULL, 0,
(nflag ? NI_NUMERICHOST : 0));
printf("%s (%s) deleted\n", host, host_buf);
@@ -666,10 +638,6 @@ again:;
/* XXX: should scope id be filled in the kernel? */
if (sin->sin6_scope_id == 0)
sin->sin6_scope_id = sdl->sdl_index;
-#ifdef __KAME__
- /* KAME specific hack; removed the embedded id */
- *(u_int16_t *)&sin->sin6_addr.s6_addr[2] = 0;
-#endif
}
getnameinfo((struct sockaddr *)sin, sin->sin6_len, host_buf,
sizeof(host_buf), NULL, 0, (nflag ? NI_NUMERICHOST : 0));
@@ -1008,6 +976,9 @@ ifinfo(ifname, argc, argv)
#ifdef ND6_IFF_AUTO_LINKLOCAL
SETFLAG("auto_linklocal", ND6_IFF_AUTO_LINKLOCAL);
#endif
+#ifdef ND6_IFF_NO_PREFER_IFACE
+ SETFLAG("no_prefer_iface", ND6_IFF_NO_PREFER_IFACE);
+#endif
SETVALUE("basereachable", ND.basereachable);
SETVALUE("retrans", ND.retrans);
SETVALUE("curhlim", ND.chlim);
@@ -1081,6 +1052,10 @@ ifinfo(ifname, argc, argv)
if ((ND.flags & ND6_IFF_AUTO_LINKLOCAL))
printf("auto_linklocal ");
#endif
+#ifdef ND6_IFF_NO_PREFER_IFACE
+ if ((ND.flags & ND6_IFF_NO_PREFER_IFACE))
+ printf("no_prefer_iface ");
+#endif
}
putc('\n', stdout);
#undef ND
@@ -1331,22 +1306,6 @@ plist()
p6.sin6_len = sizeof(p6);
p6.sin6_addr = PR.prefix;
#endif
-
- /*
- * copy link index to sin6_scope_id field.
- * XXX: KAME specific.
- */
- if (IN6_IS_ADDR_LINKLOCAL(&p6.sin6_addr)) {
- u_int16_t linkid;
-
- memcpy(&linkid, &p6.sin6_addr.s6_addr[2],
- sizeof(linkid));
- linkid = ntohs(linkid);
- p6.sin6_scope_id = linkid;
- p6.sin6_addr.s6_addr[2] = 0;
- p6.sin6_addr.s6_addr[3] = 0;
- }
-
niflags = NI_NUMERICHOST;
if (getnameinfo((struct sockaddr *)&p6,
sizeof(p6), namebuf, sizeof(namebuf),
diff --git a/usr.sbin/newsyslog/newsyslog.8 b/usr.sbin/newsyslog/newsyslog.8
index 82fe5b7..87f60ac 100644
--- a/usr.sbin/newsyslog/newsyslog.8
+++ b/usr.sbin/newsyslog/newsyslog.8
@@ -125,7 +125,9 @@ reasons for either trimming that log or skipping it.
Cause
.Nm
not to trim the logs, but to print out what it would do if this option
-were not specified.
+were not specified. This option implies the
+.Fl r
+option.
.It Fl r
Remove the restriction that
.Nm
diff --git a/usr.sbin/newsyslog/newsyslog.c b/usr.sbin/newsyslog/newsyslog.c
index 875f911..d0ef3d3 100644
--- a/usr.sbin/newsyslog/newsyslog.c
+++ b/usr.sbin/newsyslog/newsyslog.c
@@ -179,7 +179,7 @@ struct sigwork_entry {
int sw_pidok; /* true if pid value is valid */
pid_t sw_pid; /* the process id from the PID file */
const char *sw_pidtype; /* "daemon" or "process group" */
- int run_cmd; /* run command or send PID to signal */
+ int sw_runcmd; /* run command or send PID to signal */
char sw_fname[1]; /* file the PID was read from or shell cmd */
};
@@ -644,7 +644,7 @@ parse_args(int argc, char **argv)
break;
case 'n':
noaction++;
- break;
+ /* FALLTHROUGH */
case 'r':
needroot = 0;
break;
@@ -1582,7 +1582,7 @@ delete_oldest_timelog(const struct conf_entry *ent, const char *archive_dir)
oldlogs[i].fname);
else if (unlinkat(dir_fd, oldlogs[i].fname, 0) != 0) {
snprintf(errbuf, sizeof(errbuf),
- "Could not delet old logfile '%s'",
+ "Could not delete old logfile '%s'",
oldlogs[i].fname);
perror(errbuf);
}
@@ -1814,12 +1814,21 @@ do_rotate(const struct conf_entry *ent)
printf("\tcp %s %s\n", ent->log, file1);
else
printf("\tln %s %s\n", ent->log, file1);
+ printf("\ttouch %s\t\t"
+ "# Update mtime for 'when'-interval processing\n",
+ file1);
} else {
if (!(flags & CE_BINARY)) {
/* Report the trimming to the old log */
log_trim(ent->log, ent);
}
savelog(ent->log, file1);
+ /*
+ * Interval-based rotations are done using the mtime of
+ * the most recently archived log, so make sure it gets
+ * updated during a rotation.
+ */
+ utimes(file1, NULL);
}
change_attrs(file1, ent);
}
@@ -1857,7 +1866,7 @@ do_sigwork(struct sigwork_entry *swork)
int kres, secs;
char *tmp;
- if (!(swork->sw_pidok) || swork->sw_pid == 0)
+ if (swork->sw_runcmd == 0 && (!(swork->sw_pidok) || swork->sw_pid == 0))
return; /* no work to do... */
/*
@@ -1891,14 +1900,19 @@ do_sigwork(struct sigwork_entry *swork)
}
if (noaction) {
- printf("\tkill -%d %d \t\t# %s\n", swork->sw_signum,
- (int)swork->sw_pid, swork->sw_fname);
- if (secs > 0)
- printf("\tsleep %d\n", secs);
+ if (swork->sw_runcmd)
+ printf("\tsh -c '%s %d'\n", swork->sw_fname,
+ swork->sw_signum);
+ else {
+ printf("\tkill -%d %d \t\t# %s\n", swork->sw_signum,
+ (int)swork->sw_pid, swork->sw_fname);
+ if (secs > 0)
+ printf("\tsleep %d\n", secs);
+ }
return;
}
- if (swork->run_cmd) {
+ if (swork->sw_runcmd) {
asprintf(&tmp, "%s %d", swork->sw_fname, swork->sw_signum);
if (tmp == NULL) {
warn("can't allocate memory to run %s",
@@ -1974,7 +1988,7 @@ do_zipwork(struct zipwork_entry *zwork)
else
pgm_name++;
- if (zwork->zw_swork != NULL && zwork->zw_swork->run_cmd == 0 &&
+ if (zwork->zw_swork != NULL && zwork->zw_swork->sw_runcmd == 0 &&
zwork->zw_swork->sw_pidok <= 0) {
warnx(
"log %s not compressed because daemon(s) not notified",
@@ -2066,10 +2080,12 @@ save_sigwork(const struct conf_entry *ent)
tmpsiz = sizeof(struct sigwork_entry) + strlen(ent->pid_cmd_file) + 1;
stmp = malloc(tmpsiz);
- stmp->run_cmd = 0;
+ stmp->sw_runcmd = 0;
/* If this is a command to run we just set the flag and run command */
if (ent->flags & CE_PID2CMD) {
- stmp->run_cmd = 1;
+ stmp->sw_pid = -1;
+ stmp->sw_pidok = 0;
+ stmp->sw_runcmd = 1;
} else {
set_swpid(stmp, ent);
}
diff --git a/usr.sbin/nfscbd/nfscbd.8 b/usr.sbin/nfscbd/nfscbd.8
index e8efd55..958e534 100644
--- a/usr.sbin/nfscbd/nfscbd.8
+++ b/usr.sbin/nfscbd/nfscbd.8
@@ -77,10 +77,8 @@ has been specified.
For more information on what callbacks and Open Delegations do, see
.%T "Network File System (NFS) Version 4 Protocol" ,
RFC3530.
-.Pp
-The
-.Nm
-utility exits 0 on success or >0 if an error occurred.
+.Sh EXIT STATUS
+.Ex -std
.Sh SEE ALSO
.Xr nfsv4 4 ,
.Xr mount_nfs 8
diff --git a/usr.sbin/nfsd/nfsd.8 b/usr.sbin/nfsd/nfsd.8
index ea99640..19e5644 100644
--- a/usr.sbin/nfsd/nfsd.8
+++ b/usr.sbin/nfsd/nfsd.8
@@ -41,6 +41,8 @@ server
.Op Fl arduteo
.Op Fl n Ar num_servers
.Op Fl h Ar bindip
+.Op Fl maxthreads Ar max_threads
+.Op Fl minthreads Ar min_threads
.Sh DESCRIPTION
The
.Nm
@@ -74,8 +76,17 @@ Unregister the
service with
.Xr rpcbind 8
without creating any servers.
-.It Fl n
-Specifies how many servers to create.
+.It Fl n Ar threads
+Specifies how many servers to create. This option is equivalent to specifying
+.Fl maxthreads
+and
+.Fl minthreads
+with their respective arguments to
+.Ar threads .
+.It Fl maxthreads Ar threads
+Specifies the maximum servers that will be kept around to service requests.
+.It Fl minthreads Ar threads
+Specifies the minimum servers that will be kept around to service requests.
.It Fl h Ar bindip
Specifies which IP address or hostname to bind to on the local host.
This option is recommended when a host has multiple interfaces.
diff --git a/usr.sbin/nfsd/nfsd.c b/usr.sbin/nfsd/nfsd.c
index c269e47..0e39138 100644
--- a/usr.sbin/nfsd/nfsd.c
+++ b/usr.sbin/nfsd/nfsd.c
@@ -53,6 +53,7 @@ static const char rcsid[] =
#include <sys/module.h>
#include <sys/types.h>
#include <sys/stat.h>
+#include <sys/sysctl.h>
#include <sys/ucred.h>
#include <rpc/rpc.h>
@@ -71,14 +72,11 @@ static const char rcsid[] =
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
+#include <sysexits.h>
+
+#include <getopt.h>
-/* Global defs */
-#ifdef DEBUG
-#define syslog(e, s...) fprintf(stderr,s)
-static int debug = 1;
-#else
static int debug = 0;
-#endif
#define NFSD_STABLERESTART "/var/db/nfs-stablerestart"
#define NFSD_STABLEBACKUP "/var/db/nfs-stablerestart.bak"
@@ -86,11 +84,26 @@ static int debug = 0;
#define DEFNFSDCNT 4
static pid_t children[MAXNFSDCNT]; /* PIDs of children */
static int nfsdcnt; /* number of children */
+static int nfsdcnt_set;
+static int minthreads;
+static int maxthreads;
static int new_syscall;
static int run_v4server = 1; /* Force running of nfsv4 server */
static int nfssvc_nfsd; /* Set to correct NFSSVC_xxx flag */
static int stablefd = -1; /* Fd for the stable restart file */
static int backupfd; /* Fd for the backup stable restart file */
+static const char *getopt_shortopts;
+static const char *getopt_usage;
+
+static int minthreads_set;
+static int maxthreads_set;
+
+static struct option longopts[] = {
+ { "debug", no_argument, &debug, 1 },
+ { "minthreads", required_argument, &minthreads_set, 1 },
+ { "maxthreads", required_argument, &maxthreads_set, 1 },
+ { NULL, 0, NULL, 0}
+};
void cleanup(int);
void child_cleanup(int);
@@ -145,26 +158,28 @@ main(int argc, char **argv)
int udpflag, ecode, error, s, srvcnt;
int bindhostc, bindanyflag, rpcbreg, rpcbregcnt;
int nfssvc_addsock;
+ int longindex = 0;
+ const char *lopt;
char **bindhost = NULL;
pid_t pid;
nfsdcnt = DEFNFSDCNT;
unregister = reregister = tcpflag = maxsock = 0;
bindanyflag = udpflag = connect_type_cnt = bindhostc = 0;
-#define GETOPT "ah:n:rdtueo"
-#define USAGE "[-ardtueo] [-n num_servers] [-h bindip]"
- while ((ch = getopt(argc, argv, GETOPT)) != -1)
+ getopt_shortopts = "ah:n:rdtueo";
+ getopt_usage =
+ "usage:\n"
+ " nfsd [-ardtueo] [-h bindip]\n"
+ " [-n numservers] [--minthreads #] [--maxthreads #]\n";
+ while ((ch = getopt_long(argc, argv, getopt_shortopts, longopts,
+ &longindex)) != -1)
switch (ch) {
case 'a':
bindanyflag = 1;
break;
case 'n':
+ nfsdcnt_set = 1;
nfsdcnt = atoi(optarg);
- if (nfsdcnt < 1 || nfsdcnt > MAXNFSDCNT) {
- warnx("nfsd count %d; reset to %d", nfsdcnt,
- DEFNFSDCNT);
- nfsdcnt = DEFNFSDCNT;
- }
break;
case 'h':
bindhostc++;
@@ -193,6 +208,14 @@ main(int argc, char **argv)
case 'o':
run_v4server = 0;
break;
+ case 0:
+ lopt = longopts[longindex].name;
+ if (!strcmp(lopt, "minthreads")) {
+ minthreads = atoi(optarg);
+ } else if (!strcmp(lopt, "maxthreads")) {
+ maxthreads = atoi(optarg);
+ }
+ break;
default:
case '?':
usage();
@@ -201,6 +224,10 @@ main(int argc, char **argv)
udpflag = 1;
argv += optind;
argc -= optind;
+ if (minthreads_set && maxthreads_set && minthreads > maxthreads)
+ errx(EX_USAGE,
+ "error: minthreads(%d) can't be greater than "
+ "maxthreads(%d)", minthreads, maxthreads);
/*
* XXX
@@ -209,6 +236,7 @@ main(int argc, char **argv)
if (argc > 1)
usage();
if (argc == 1) {
+ nfsdcnt_set = 1;
nfsdcnt = atoi(argv[0]);
if (nfsdcnt < 1 || nfsdcnt > MAXNFSDCNT) {
warnx("nfsd count %d; reset to %d", nfsdcnt,
@@ -236,7 +264,7 @@ main(int argc, char **argv)
ip6flag = 1;
s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
if (s == -1) {
- if (errno != EPROTONOSUPPORT)
+ if (errno != EPROTONOSUPPORT && errno != EAFNOSUPPORT)
err(1, "socket");
ip6flag = 0;
} else if (getnetconfigent("udp6") == NULL ||
@@ -356,7 +384,7 @@ main(int argc, char **argv)
(void)signal(SIGCHLD, reapchild);
(void)signal(SIGUSR2, backup_stable);
- openlog("nfsd", LOG_PID, LOG_DAEMON);
+ openlog("nfsd", LOG_PID | (debug ? LOG_PERROR : 0), LOG_DAEMON);
/*
* For V4, we open the stablerestart file and call nfssvc()
@@ -374,13 +402,13 @@ main(int argc, char **argv)
if (run_v4server > 0) {
open_stable(&stablefd, &backupfd);
if (stablefd < 0) {
- syslog(LOG_ERR, "Can't open %s\n", NFSD_STABLERESTART);
+ syslog(LOG_ERR, "Can't open %s: %m\n", NFSD_STABLERESTART);
exit(1);
}
/* This system call will fail for old kernels, but that's ok. */
nfssvc(NFSSVC_BACKUPSTABLE, NULL);
if (nfssvc(NFSSVC_STABLERESTART, (caddr_t)&stablefd) < 0) {
- syslog(LOG_ERR, "Can't read stable storage file\n");
+ syslog(LOG_ERR, "Can't read stable storage file: %m\n");
exit(1);
}
nfssvc_addsock = NFSSVC_NFSDADDSOCK;
@@ -401,6 +429,16 @@ main(int argc, char **argv)
}
if (!new_syscall) {
+ if (nfsdcnt < 1) {
+ warnx("nfsd count too low %d; reset to %d", nfsdcnt,
+ DEFNFSDCNT);
+ nfsdcnt = DEFNFSDCNT;
+ }
+ if (nfsdcnt > MAXNFSDCNT) {
+ warnx("nfsd count too high %d; reset to %d", nfsdcnt,
+ DEFNFSDCNT);
+ nfsdcnt = MAXNFSDCNT;
+ }
/* If we use UDP only, we start the last server below. */
srvcnt = tcpflag ? nfsdcnt : nfsdcnt - 1;
for (i = 0; i < srvcnt; i++) {
@@ -855,7 +893,7 @@ setbindhost(struct addrinfo **ai, const char *bindhost, struct addrinfo hints)
void
usage(void)
{
- (void)fprintf(stderr, "usage: nfsd %s\n", USAGE);
+ (void)fprintf(stderr, "%s", getopt_usage);
exit(1);
}
@@ -923,6 +961,29 @@ nfsd_exit(int status)
exit(status);
}
+static int
+get_tuned_nfsdcount(void)
+{
+ int ncpu, error, tuned_nfsdcnt;
+ size_t ncpu_size;
+
+ ncpu_size = sizeof(ncpu);
+ error = sysctlbyname("hw.ncpu", &ncpu, &ncpu_size, NULL, 0);
+ if (error) {
+ warnx("sysctlbyname(hw.ncpu) failed defaulting to %d nfs servers",
+ DEFNFSDCNT);
+ tuned_nfsdcnt = DEFNFSDCNT;
+ } else {
+ tuned_nfsdcnt = ncpu * 8;
+ }
+ if (!new_syscall && tuned_nfsdcnt > MAXNFSDCNT) {
+ warnx("nfsd count %d; truncated to %d", tuned_nfsdcnt,
+ MAXNFSDCNT);
+ tuned_nfsdcnt = MAXNFSDCNT;
+ }
+ return tuned_nfsdcnt;
+}
+
void
start_server(int master)
{
@@ -952,8 +1013,28 @@ start_server(int master)
}
}
nfsdargs.principal = principal;
- nfsdargs.minthreads = nfsdcnt;
- nfsdargs.maxthreads = nfsdcnt;
+
+ if (minthreads_set) {
+ nfsdargs.minthreads = minthreads;
+ if (!maxthreads_set)
+ nfsdargs.maxthreads = minthreads;
+ }
+ if (maxthreads_set) {
+ nfsdargs.maxthreads = maxthreads;
+ if (!minthreads_set)
+ nfsdargs.minthreads = maxthreads;
+ }
+ if (nfsdcnt_set) {
+ nfsdargs.minthreads = nfsdcnt;
+ nfsdargs.maxthreads = nfsdcnt;
+ }
+ if (!minthreads_set && !maxthreads_set && !nfsdcnt_set) {
+ int tuned_nfsdcnt;
+
+ tuned_nfsdcnt = get_tuned_nfsdcount();
+ nfsdargs.minthreads = tuned_nfsdcnt;
+ nfsdargs.maxthreads = tuned_nfsdcnt;
+ }
error = nfssvc(nfssvc_nfsd, &nfsdargs);
if (error < 0 && errno == EAUTH) {
/*
diff --git a/usr.sbin/nfsd/nfsv4.4 b/usr.sbin/nfsd/nfsv4.4
index d2c19ed..679bb69 100644
--- a/usr.sbin/nfsd/nfsv4.4
+++ b/usr.sbin/nfsd/nfsv4.4
@@ -24,7 +24,7 @@
.\"
.\" $FreeBSD$
.\"
-.Dd May 15, 2011
+.Dd December 23, 2012
.Dt NFSV4 4
.Os
.Sh NAME
@@ -98,6 +98,8 @@ allows a limited subset of operations to be performed on non-exported subtrees
of the local file system, so that traversal of the tree to the exported
subtrees is possible.
As such, the ``<rootdir>'' can be in a non-exported file system.
+The exception is ZFS, which checks exports and, as such, all ZFS file systems
+below the ``<rootdir>'' must be exported.
However,
the entire tree that is rooted at that point must be in local file systems
that are of types that can be NFS exported.
diff --git a/usr.sbin/nmtree/Makefile b/usr.sbin/nmtree/Makefile
new file mode 100644
index 0000000..1b8cc01
--- /dev/null
+++ b/usr.sbin/nmtree/Makefile
@@ -0,0 +1,33 @@
+# $FreeBSD$
+
+.include <bsd.own.mk>
+
+.PATH: ${.CURDIR}/../../contrib/mtree
+
+PROG= nmtree
+MAN= nmtree.8
+SRCS= compare.c crc.c create.c excludes.c getid.c misc.c mtree.c \
+ spec.c specspec.c verify.c
+LDADD+= -lmd -lutil
+
+CFLAGS+= -I${.CURDIR}/../../contrib/mknod
+.PATH: ${.CURDIR}/../../contrib/mknod
+SRCS+= pack_dev.c
+
+CFLAGS+= -I${.CURDIR}/../../lib/libnetbsd
+LIBNETBSDDIR= ${.OBJDIR}/../../lib/libnetbsd
+LIBNETBSD= ${LIBNETBSDDIR}/libnetbsd.a
+DPADD+= ${LIBNETBSD}
+LDADD+= ${LIBNETBSD}
+
+.if ${MK_NMTREE} != "no"
+LINKS= ${BINDIR}/nmtree ${BINDIR}/mtree
+MLINKS= nmtree.8 mtree.8
+.endif
+
+CLEANFILES+= nmtree.8
+
+nmtree.8: mtree.8
+ cp ${.ALLSRC} ${.TARGET}
+
+.include <bsd.prog.mk>
diff --git a/usr.sbin/pciconf/Makefile b/usr.sbin/pciconf/Makefile
index 32e9848..a839733 100644
--- a/usr.sbin/pciconf/Makefile
+++ b/usr.sbin/pciconf/Makefile
@@ -5,8 +5,6 @@ PROG= pciconf
SRCS= pciconf.c cap.c err.c
MAN= pciconf.8
-CFLAGS+= -I${.CURDIR}/../../sys
-
WARNS?= 3
.include <bsd.prog.mk>
diff --git a/usr.sbin/pciconf/cap.c b/usr.sbin/pciconf/cap.c
index ce184dd..1c2ab4a 100644
--- a/usr.sbin/pciconf/cap.c
+++ b/usr.sbin/pciconf/cap.c
@@ -449,24 +449,28 @@ cap_express(int fd, struct pci_conf *p, uint8_t ptr)
static void
cap_msix(int fd, struct pci_conf *p, uint8_t ptr)
{
- uint32_t val;
+ uint32_t pba_offset, table_offset, val;
+ int msgnum, pba_bar, table_bar;
uint16_t ctrl;
- int msgnum, table_bar, pba_bar;
ctrl = read_config(fd, &p->pc_sel, ptr + PCIR_MSIX_CTRL, 2);
msgnum = (ctrl & PCIM_MSIXCTRL_TABLE_SIZE) + 1;
+
val = read_config(fd, &p->pc_sel, ptr + PCIR_MSIX_TABLE, 4);
table_bar = PCIR_BAR(val & PCIM_MSIX_BIR_MASK);
+ table_offset = val & ~PCIM_MSIX_BIR_MASK;
+
val = read_config(fd, &p->pc_sel, ptr + PCIR_MSIX_PBA, 4);
- pba_bar = PCIR_BAR(val & PCIM_MSIX_BIR_MASK);
- printf("MSI-X supports %d message%s ", msgnum,
- (msgnum == 1) ? "" : "s");
- if (table_bar == pba_bar)
- printf("in map 0x%x", table_bar);
- else
- printf("in maps 0x%x and 0x%x", table_bar, pba_bar);
- if (ctrl & PCIM_MSIXCTRL_MSIX_ENABLE)
- printf(" enabled");
+ pba_bar = PCIR_BAR(val & PCIM_MSIX_BIR_MASK);
+ pba_offset = val & ~PCIM_MSIX_BIR_MASK;
+
+ printf("MSI-X supports %d message%s%s\n", msgnum,
+ (msgnum == 1) ? "" : "s",
+ (ctrl & PCIM_MSIXCTRL_MSIX_ENABLE) ? ", enabled" : "");
+
+ printf(" ");
+ printf("Table in map 0x%x[0x%x], PBA in map 0x%x[0x%x]",
+ table_bar, table_offset, pba_bar, pba_offset);
}
static void
diff --git a/usr.sbin/pkg/Makefile b/usr.sbin/pkg/Makefile
index 360e301..c2ca0a3 100644
--- a/usr.sbin/pkg/Makefile
+++ b/usr.sbin/pkg/Makefile
@@ -1,7 +1,7 @@
# $FreeBSD$
PROG= pkg
-SRC= pkg.c
+SRCS= pkg.c dns_utils.c
NO_MAN= yes
DPADD= ${LIBARCHIVE} ${LIBELF} ${LIBFETCH}
diff --git a/usr.sbin/pkg/dns_utils.c b/usr.sbin/pkg/dns_utils.c
new file mode 100644
index 0000000..239be90
--- /dev/null
+++ b/usr.sbin/pkg/dns_utils.c
@@ -0,0 +1,135 @@
+/*-
+ * Copyright (c) 2012 Baptiste Daroussin <bapt@FreeBSD.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer,
+ * without modification, immediately at the beginning of the file.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <stdlib.h>
+#include <string.h>
+#include <netinet/in.h>
+#include <resolv.h>
+
+#include "dns_utils.h"
+
+typedef union {
+ HEADER hdr;
+ unsigned char buf[1024];
+} dns_query;
+
+struct dns_srvinfo *
+dns_getsrvinfo(const char *zone)
+{
+ struct dns_srvinfo **res, *first;
+ unsigned char *end, *p;
+ char host[MAXHOSTNAMELEN];
+ dns_query q;
+ int len, qdcount, ancount, n, i;
+ unsigned int type, class, ttl, priority, weight, port;
+
+ if ((len = res_query(zone, C_IN, T_SRV, q.buf, sizeof(q.buf))) == -1 ||
+ len < (int)sizeof(HEADER))
+ return (NULL);
+
+ qdcount = ntohs(q.hdr.qdcount);
+ ancount = ntohs(q.hdr.ancount);
+
+ end = q.buf + len;
+ p = q.buf + sizeof(HEADER);
+
+ while(qdcount > 0 && p < end) {
+ qdcount--;
+ if((len = dn_expand(q.buf, end, p, host, MAXHOSTNAMELEN)) < 0)
+ return (NULL);
+ p += len + NS_QFIXEDSZ;
+ }
+
+ res = calloc(ancount, sizeof(struct dns_srvinfo));
+ if (res == NULL)
+ return (NULL);
+
+ n = 0;
+ while (ancount > 0 && p < end) {
+ ancount--;
+ len = dn_expand(q.buf, end, p, host, MAXHOSTNAMELEN);
+ if (len < 0) {
+ for (i = 0; i < n; i++)
+ free(res[i]);
+ free(res);
+ return NULL;
+ }
+
+ p += len;
+
+ NS_GET16(type, p);
+ NS_GET16(class, p);
+ NS_GET32(ttl, p);
+ NS_GET16(len, p);
+
+ if (type != T_SRV) {
+ p += len;
+ continue;
+ }
+
+ NS_GET16(priority, p);
+ NS_GET16(weight, p);
+ NS_GET16(port, p);
+
+ len = dn_expand(q.buf, end, p, host, MAXHOSTNAMELEN);
+ if (len < 0) {
+ for (i = 0; i < n; i++)
+ free(res[i]);
+ free(res);
+ return (NULL);
+ }
+
+ res[n] = malloc(sizeof(struct dns_srvinfo));
+ if (res[n] == NULL) {
+ for (i = 0; i < n; i++)
+ free(res[i]);
+ free(res);
+ return (NULL);
+ }
+ res[n]->type = type;
+ res[n]->class = class;
+ res[n]->ttl = ttl;
+ res[n]->priority = priority;
+ res[n]->weight = weight;
+ res[n]->port = port;
+ res[n]->next = NULL;
+ strlcpy(res[n]->host, host, MAXHOSTNAMELEN);
+
+ p += len;
+ n++;
+ }
+
+ for (i = 0; i < n - 1; i++)
+ res[i]->next = res[i + 1];
+
+ first = res[0];
+ free(res);
+
+ return (first);
+}
diff --git a/usr.sbin/pkg/dns_utils.h b/usr.sbin/pkg/dns_utils.h
new file mode 100644
index 0000000..0f3367b
--- /dev/null
+++ b/usr.sbin/pkg/dns_utils.h
@@ -0,0 +1,45 @@
+/*-
+ * Copyright (c) 2012 Baptiste Daroussin <bapt@FreeBSD.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ */
+
+#ifndef DNS_UTILS_H
+#define DNS_UTILS_H
+struct dns_srvinfo {
+ unsigned int type;
+ unsigned int class;
+ unsigned int ttl;
+ unsigned int priority;
+ unsigned int weight;
+ unsigned int port;
+ char host[MAXHOSTNAMELEN];
+ struct dns_srvinfo *next;
+};
+
+struct dns_srvinfo *
+ dns_getsrvinfo(const char *zone);
+
+#endif
diff --git a/usr.sbin/pkg/pkg.c b/usr.sbin/pkg/pkg.c
index 8dc7bcf..1b3146b 100644
--- a/usr.sbin/pkg/pkg.c
+++ b/usr.sbin/pkg/pkg.c
@@ -48,9 +48,10 @@ __FBSDID("$FreeBSD$");
#include <unistd.h>
#include "elf_tables.h"
+#include "dns_utils.h"
#define _LOCALBASE "/usr/local"
-#define _PKGS_URL "http://pkgbeta.FreeBSD.org"
+#define _PKGS_URL "http://pkg.FreeBSD.org"
static const char *
elf_corres_to_string(struct _elf_corres *m, int e)
@@ -281,16 +282,20 @@ install_pkg_static(char *path, char *pkgpath)
static int
bootstrap_pkg(void)
{
+ struct url *u;
FILE *remote;
FILE *config;
char *site;
+ struct dns_srvinfo *mirrors, *current;
+ /* To store _https._tcp. + hostname + \0 */
+ char zone[MAXHOSTNAMELEN + 13];
char url[MAXPATHLEN];
char conf[MAXPATHLEN];
char abi[BUFSIZ];
char tmppkg[MAXPATHLEN];
char buf[10240];
char pkgstatic[MAXPATHLEN];
- int fd, retry, ret;
+ int fd, retry, ret, max_retry;
struct url_stat st;
off_t done, r;
time_t now;
@@ -298,9 +303,11 @@ bootstrap_pkg(void)
done = 0;
last = 0;
+ max_retry = 3;
ret = -1;
remote = NULL;
config = NULL;
+ current = mirrors = NULL;
printf("Bootstrapping pkg please wait\n");
@@ -324,12 +331,37 @@ bootstrap_pkg(void)
return (-1);
}
- retry = 3;
- do {
- remote = fetchXGetURL(url, &st, "");
- if (remote == NULL)
- sleep(1);
- } while (remote == NULL && retry-- > 0);
+ retry = max_retry;
+
+ u = fetchParseURL(url);
+ while (remote == NULL) {
+ if (retry == max_retry) {
+ if (strcmp(u->scheme, "file") != 0) {
+ snprintf(zone, sizeof(zone),
+ "_%s._tcp.%s", u->scheme, u->host);
+ printf("%s\n", zone);
+ mirrors = dns_getsrvinfo(zone);
+ current = mirrors;
+ }
+ }
+
+ if (mirrors != NULL)
+ strlcpy(u->host, current->host, sizeof(u->host));
+
+ remote = fetchXGet(u, &st, "");
+ if (remote == NULL) {
+ --retry;
+ if (retry <= 0)
+ goto fetchfail;
+ if (mirrors == NULL) {
+ sleep(1);
+ } else {
+ current = current->next;
+ if (current == NULL)
+ current = mirrors;
+ }
+ }
+ }
if (remote == NULL)
goto fetchfail;
@@ -420,6 +452,14 @@ main(__unused int argc, char *argv[])
getenv("LOCALBASE") ? getenv("LOCALBASE") : _LOCALBASE);
if (access(pkgpath, X_OK) == -1) {
+ /*
+ * To allow 'pkg -N' to be used as a reliable test for whether
+ * a system is configured to use pkg, don't bootstrap pkg
+ * when that argument is given as argv[1].
+ */
+ if (argv[1] != NULL && strcmp(argv[1], "-N") == 0)
+ errx(EXIT_FAILURE, "pkg is not installed");
+
/*
* Do not ask for confirmation if either of stdin or stdout is
* not tty. Check the environment to see if user has answer
diff --git a/usr.sbin/pkg_install/add/main.c b/usr.sbin/pkg_install/add/main.c
index 563e04b..c91c8b6 100644
--- a/usr.sbin/pkg_install/add/main.c
+++ b/usr.sbin/pkg_install/add/main.c
@@ -136,7 +136,6 @@ main(int argc, char **argv)
static char temppackageroot[MAXPATHLEN];
static char pkgaddpath[MAXPATHLEN];
- warnpkgng();
if (*argv[0] != '/' && strchr(argv[0], '/') != NULL)
PkgAddCmd = realpath(argv[0], pkgaddpath);
else
@@ -295,6 +294,7 @@ main(int argc, char **argv)
if (chroot("."))
errx(1, "chroot to %s failed", Chroot);
}
+ warnpkgng();
/* Make sure the sub-execs we invoke get found */
setenv("PATH",
"/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin",
diff --git a/usr.sbin/pkg_install/add/pkg_add.1 b/usr.sbin/pkg_install/add/pkg_add.1
index d146441..923fe45 100644
--- a/usr.sbin/pkg_install/add/pkg_add.1
+++ b/usr.sbin/pkg_install/add/pkg_add.1
@@ -15,7 +15,7 @@
.\"
.\" $FreeBSD$
.\"
-.Dd January 4, 2009
+.Dd November 9, 2012
.Dt PKG_ADD 1
.Os
.Sh NAME
@@ -578,6 +578,10 @@ and
are not suitable for creating the staging area.
.It Pa /var/db/pkg
Default location of the installed package database.
+.It Ev PKG_OLD_NOWARN
+If set
+.Nm
+will not warn about its use in the presence of pkgng databases.
.El
.Sh SEE ALSO
.Xr pkg_create 1 ,
diff --git a/usr.sbin/pkg_install/create/pkg_create.1 b/usr.sbin/pkg_install/create/pkg_create.1
index fa0365e..18780a0 100644
--- a/usr.sbin/pkg_install/create/pkg_create.1
+++ b/usr.sbin/pkg_install/create/pkg_create.1
@@ -23,7 +23,7 @@
.\" [jkh] Took John's changes back and made some additional extensions for
.\" better integration with FreeBSD's new ports collection.
.\"
-.Dd Oct 12, 2010
+.Dd November 9, 2012
.Dt PKG_CREATE 1
.Os
.Sh NAME
@@ -635,6 +635,10 @@ does not exist.
The last choice if
.Pa /tmp
is unsuitable.
+.It Ev PKG_OLD_NOWARN
+If set
+.Nm
+will not warn about its use in the presence of pkgng databases.
.El
.Sh SEE ALSO
.Xr pkg_add 1 ,
diff --git a/usr.sbin/pkg_install/delete/pkg_delete.1 b/usr.sbin/pkg_install/delete/pkg_delete.1
index f10651c..a07b1b6 100644
--- a/usr.sbin/pkg_install/delete/pkg_delete.1
+++ b/usr.sbin/pkg_install/delete/pkg_delete.1
@@ -17,7 +17,7 @@
.\" @(#)pkg_delete.1
.\" $FreeBSD$
.\"
-.Dd May 30, 2008
+.Dd November 9, 2012
.Dt PKG_DELETE 1
.Os
.Sh NAME
@@ -276,6 +276,10 @@ specifies an alternative location for the installed package database.
.Bl -tag -width /var/db/pkg -compact
.It Pa /var/db/pkg
Default location of the installed package database.
+.It Ev PKG_OLD_NOWARN
+If set
+.Nm
+will not warn about its use in the presence of pkgng databases.
.El
.Sh SEE ALSO
.Xr pkg_add 1 ,
diff --git a/usr.sbin/pkg_install/info/pkg_info.1 b/usr.sbin/pkg_install/info/pkg_info.1
index 8b032c1..c29687d 100644
--- a/usr.sbin/pkg_install/info/pkg_info.1
+++ b/usr.sbin/pkg_install/info/pkg_info.1
@@ -17,7 +17,7 @@
.\" @(#)pkg_info.1
.\" $FreeBSD$
.\"
-.Dd May 30, 2008
+.Dd November 9, 2012
.Dt PKG_INFO 1
.Os
.Sh NAME
@@ -258,6 +258,10 @@ Specifies an alternative package location, if a given package cannot be
found.
.It Ev PKGDIR
Specifies an alternative location to save downloaded packages to.
+.It Ev PKG_OLD_NOWARN
+If set
+.Nm
+will not warn about its use in the presence of pkgng databases.
.El
.Sh FILES
.Bl -tag -width ".Pa /var/db/pkg" -compact
@@ -277,6 +281,10 @@ The last choice if
is unsuitable.
.It Pa /var/db/pkg
Default location of the installed package database.
+.It Ev PKG_OLD_NOWARN
+If set
+.Nm
+will not warn about its use in the presence of pkgng databases.
.El
.Sh SEE ALSO
.Xr pkg_add 1 ,
diff --git a/usr.sbin/pkg_install/lib/lib.h b/usr.sbin/pkg_install/lib/lib.h
index 88fcf34..77b2c71 100644
--- a/usr.sbin/pkg_install/lib/lib.h
+++ b/usr.sbin/pkg_install/lib/lib.h
@@ -99,7 +99,7 @@
* Version of the package tools - increase whenever you make a change
* in the code that is not cosmetic only.
*/
-#define PKG_INSTALL_VERSION 20120918
+#define PKG_INSTALL_VERSION 20130122
#define PKG_WRAPCONF_FNAME "/var/db/pkg_install.conf"
#define main(argc, argv) real_main(argc, argv)
diff --git a/usr.sbin/pkg_install/lib/pkgng.c b/usr.sbin/pkg_install/lib/pkgng.c
index 76c8ad8..3aba383 100644
--- a/usr.sbin/pkg_install/lib/pkgng.c
+++ b/usr.sbin/pkg_install/lib/pkgng.c
@@ -1,6 +1,6 @@
-/*
- * FreeBSD install - a package for the installation and maintenance
- * of non-core utilities.
+/*-
+ * Copyright (c) 2012 Eitan Adler
+ * All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -11,28 +11,51 @@
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
- * Eitan Adler
- *
- * detect pkgng's existence and warn
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
*
+ * $FreeBSD$
*/
#include <sys/cdefs.h>
-__FBSDID("$FreeBSD$");
#include "lib.h"
#include <err.h>
-void warnpkgng(void) {
- char pkgngpath[MAXPATHLEN];
+static const char message[] = "You appear to be using the newer pkg(1) tool on \
+this system for package management, rather than the legacy package \
+management tools (pkg_*). The legacy tools should no longer be used on \
+this system.";
+
+void warnpkgng(void)
+{
+ char pkgngpath[MAXPATHLEN + 1];
char *pkgngdir;
+ char *dontwarn;
+ int rc;
+ dontwarn = getenv("PKG_OLD_NOWARN");
+ if (dontwarn != NULL)
+ return;
pkgngdir = getenv("PKG_DBDIR");
if (pkgngdir == NULL)
pkgngdir = "/var/db/pkg";
- strcpy(pkgngpath, pkgngdir);
- strcat(pkgngpath, "/local.sqlite");
+
+ rc = snprintf(pkgngpath, sizeof(pkgngpath), "%s/local.sqlite", pkgngdir);
+ if ((size_t)rc >= sizeof(pkgngpath)) {
+ warnx("path too long: %s/local.sqlite", pkgngdir);
+ return;
+ }
if (access(pkgngpath, F_OK) == 0)
- warnx("Don't use the pkg_ tools if you are using pkgng");
+ warnx(message);
}
diff --git a/usr.sbin/pkg_install/updating/pkg_updating.1 b/usr.sbin/pkg_install/updating/pkg_updating.1
index 9d12724..3fe16fd 100644
--- a/usr.sbin/pkg_install/updating/pkg_updating.1
+++ b/usr.sbin/pkg_install/updating/pkg_updating.1
@@ -10,7 +10,7 @@
.\"
.\" $FreeBSD$
.\"
-.Dd May 30, 2008
+.Dd November 9, 2012
.Dt PKG_UPDATING 1
.Os
.Sh NAME
@@ -77,6 +77,13 @@ installed ports:
Fetch UPDATING file from ftp mirror and show all entries of all
installed ports:
.Dl % pkg_updating -f ftp://ftp.freebsd.org/pub/FreeBSD/ports/packages/UPDATING
+.Sh ENVRIOMENT
+.Bl -tag -width indent
+.It Ev PKG_OLD_NOWARN
+If set
+.Nm
+will not warn about its use in the presence of pkgng databases.
+.El
.Sh SEE ALSO
.Xr pkg_add 1 ,
.Xr pkg_create 1 ,
diff --git a/usr.sbin/pkg_install/version/pkg_version.1 b/usr.sbin/pkg_install/version/pkg_version.1
index db0d7af..26caf8c 100644
--- a/usr.sbin/pkg_install/version/pkg_version.1
+++ b/usr.sbin/pkg_install/version/pkg_version.1
@@ -25,7 +25,7 @@
.\"
.\" $FreeBSD$
.\"
-.Dd May 30, 2008
+.Dd November 9, 2012
.Dt PKG_VERSION 1
.Os
.Sh NAME
@@ -233,6 +233,13 @@ The following command compares two package version strings:
The
.Fl c
option has been deprecated and is no longer supported.
+.Sh ENVRIOMENT
+.Bl -tag -width indent
+.It Ev PKG_OLD_NOWARN
+If set
+.Nm
+will not warn about its use in the presence of pkgng databases.
+.El
.Sh SEE ALSO
.Xr fetch 1 ,
.Xr pkg_add 1 ,
diff --git a/usr.sbin/portsnap/portsnap/portsnap.sh b/usr.sbin/portsnap/portsnap/portsnap.sh
index ed9c28d..f9f4716 100644
--- a/usr.sbin/portsnap/portsnap/portsnap.sh
+++ b/usr.sbin/portsnap/portsnap/portsnap.sh
@@ -612,6 +612,28 @@ fetch_progress() {
echo -n " "
}
+pct_fmt()
+{
+ printf " \r"
+ printf "($1/$2) %02.2f%% " `echo "scale=4;$LNC / $TOTAL * 100"|bc`
+}
+
+fetch_progress_percent() {
+ TOTAL=$1
+ LNC=0
+ pct_fmt $LNC $TOTAL
+ while read x; do
+ LNC=$(($LNC + 1))
+ if [ $(($LNC % 100)) = 0 ]; then
+ pct_fmt $LNC $TOTAL
+ elif [ $(($LNC % 10)) = 0 ]; then
+ echo -n .
+ fi
+ done
+ pct_fmt $LNC $TOTAL
+ echo " done. "
+}
+
# Sanity-check an index file
fetch_index_sanity() {
if grep -qvE "^[-_+./@0-9A-Za-z]+\|[0-9a-f]{64}$" INDEX.new ||
@@ -781,11 +803,13 @@ fetch_update() {
echo " done." 1>${QUIETREDIR}
# Attempt to fetch ports patches
- echo -n "Fetching `wc -l < patchlist | tr -d ' '` "
+ patchcnt=`wc -l < patchlist | tr -d ' '`
+ echo -n "Fetching $patchcnt "
echo ${NDEBUG} "patches.${DDSTATS}"
+ echo " "
tr '|' '-' < patchlist | lam -s "bp/" - |
xargs ${XARGST} ${PHTTPGET} ${SERVERNAME} \
- 2>${STATSREDIR} | fetch_progress
+ 2>${STATSREDIR} | fetch_progress_percent $patchcnt
echo "done."
# Attempt to apply ports patches
diff --git a/usr.sbin/ppp/README.changes b/usr.sbin/ppp/README.changes
index bf626e3..4ed3da6 100644
--- a/usr.sbin/ppp/README.changes
+++ b/usr.sbin/ppp/README.changes
@@ -53,7 +53,7 @@ o Errors in `add' and `delete' are logged as warnings rather than being
written to the TCP/IP log.
o Any number of diagnostic prompts are allowed, and they are allowed in
interactive mode.
-o The default `device' is cuad1, then cuad0
+o The default `device' is cuau1, then cuau0
o A password of "*" in ppp.secret causes a passwd database lookup in
pap mode.
o The value of the CONNECT environment variable is logged in the
diff --git a/usr.sbin/ppp/arp.c b/usr.sbin/ppp/arp.c
index e67f89b..b38b97a 100644
--- a/usr.sbin/ppp/arp.c
+++ b/usr.sbin/ppp/arp.c
@@ -91,7 +91,7 @@
*/
static struct {
struct rt_msghdr hdr;
- struct sockaddr_inarp dst;
+ struct sockaddr_in dst;
struct sockaddr_dl hwa;
char extra[128];
} arpmsg;
@@ -124,10 +124,9 @@ arp_ProxySub(struct bundle *bundle, struct in_addr addr, int add)
arpmsg.hdr.rtm_seq = ++bundle->routing_seq;
arpmsg.hdr.rtm_addrs = RTA_DST | RTA_GATEWAY;
arpmsg.hdr.rtm_inits = RTV_EXPIRE;
- arpmsg.dst.sin_len = sizeof(struct sockaddr_inarp);
+ arpmsg.dst.sin_len = sizeof(struct sockaddr_in);
arpmsg.dst.sin_family = AF_INET;
arpmsg.dst.sin_addr.s_addr = addr.s_addr;
- arpmsg.dst.sin_other = SIN_PROXY;
arpmsg.hdr.rtm_msglen = (char *) &arpmsg.hwa - (char *) &arpmsg
+ arpmsg.hwa.sdl_len;
diff --git a/usr.sbin/ppp/defs.h b/usr.sbin/ppp/defs.h
index f56ee32..d7cc799 100644
--- a/usr.sbin/ppp/defs.h
+++ b/usr.sbin/ppp/defs.h
@@ -30,7 +30,7 @@
/* Check the following definitions for your machine environment */
#ifdef __FreeBSD__
-# define MODEM_LIST "/dev/cuad1\0/dev/cuad0" /* name of tty device */
+# define MODEM_LIST "/dev/cuau1\0/dev/cuau0" /* name of tty device */
#else
# ifdef __OpenBSD__
# define MODEM_LIST "/dev/cua01\0/dev/cua00" /* name of tty device */
diff --git a/usr.sbin/ppp/ppp.8.m4 b/usr.sbin/ppp/ppp.8.m4
index 7d3f6d2..91b9c51 100644
--- a/usr.sbin/ppp/ppp.8.m4
+++ b/usr.sbin/ppp/ppp.8.m4
@@ -551,7 +551,7 @@ and have not authenticated yourself using the correct password.
.Pp
You can start by specifying the device name and speed:
.Bd -literal -offset indent
-ppp ON awfulhak> set device /dev/cuad0
+ppp ON awfulhak> set device /dev/cuau0
ppp ON awfulhak> set speed 38400
.Ed
.Pp
@@ -610,7 +610,7 @@ Name: deflink
Phone Number: N/A
Defaults:
- Device List: /dev/cuad0
+ Device List: /dev/cuau0
Characteristics: 38400bps, cs8, even parity, CTS/RTS on
Connect time: 0 secs
@@ -1216,10 +1216,10 @@ It is usually also necessary to train your modem to the same DTR speed
as the getty:
.Bd -literal -offset indent
# ppp
-ppp ON awfulhak> set device /dev/cuad1
+ppp ON awfulhak> set device /dev/cuau1
ppp ON awfulhak> set speed 38400
ppp ON awfulhak> term
-deflink: Entering terminal mode on /dev/cuad1
+deflink: Entering terminal mode on /dev/cuau1
Type `~?' for help
at
OK
@@ -2164,7 +2164,7 @@ and
.Dq set speed
to specify your serial line and speed, for example:
.Bd -literal -offset indent
-set device /dev/cuad0
+set device /dev/cuau0
set speed 115200
.Ed
.Pp
@@ -2529,7 +2529,7 @@ Armed with this information, the following configuration might be used:
mp:
set timeout 0
set log phase chat
- set device /dev/cuad0 /dev/cuad1 /dev/cuad2
+ set device /dev/cuau0 /dev/cuau1 /dev/cuau2
set phone "123456789"
set dial "ABORT BUSY ABORT NO\\sCARRIER TIMEOUT 5 \\"\\" ATZ \e
OK-AT-OK \\\\dATDT\\\\T TIMEOUT 45 CONNECT"
@@ -2563,9 +2563,9 @@ line above, and specifying the following after the
.Dq clone
command:
.Bd -literal -offset indent
- link 1 set device /dev/cuad0
- link 2 set device /dev/cuad1
- link 3 set device /dev/cuad2
+ link 1 set device /dev/cuau0
+ link 2 set device /dev/cuau1
+ link 3 set device /dev/cuau2
.Ed
.Pp
Use the
@@ -4073,7 +4073,7 @@ The default link name is
.Sq deflink .
Renaming it to
.Sq modem ,
-.Sq cuad0
+.Sq cuau0
or
.Sq USR
may make the log file more readable.
diff --git a/usr.sbin/pw/bitmap.c b/usr.sbin/pw/bitmap.c
index bcfea7e..8e96bff 100644
--- a/usr.sbin/pw/bitmap.c
+++ b/usr.sbin/pw/bitmap.c
@@ -50,8 +50,7 @@ bm_alloc(int size)
void
bm_dealloc(struct bitmap * bm)
{
- if (bm->map)
- free(bm->map);
+ free(bm->map);
}
static void
diff --git a/usr.sbin/pw/grupd.c b/usr.sbin/pw/grupd.c
index e9f6b5e..3f78e95 100644
--- a/usr.sbin/pw/grupd.c
+++ b/usr.sbin/pw/grupd.c
@@ -50,12 +50,11 @@ setgrdir(const char * dir)
{
if (dir == NULL)
return -1;
- else {
- char * d = malloc(strlen(dir)+1);
- if (d == NULL)
- return -1;
- grpath = strcpy(d, dir);
- }
+ else
+ grpath = strdup(dir);
+ if (grpath == NULL)
+ return -1;
+
return 0;
}
diff --git a/usr.sbin/pw/pw_group.c b/usr.sbin/pw/pw_group.c
index f4f2116..3259412 100644
--- a/usr.sbin/pw/pw_group.c
+++ b/usr.sbin/pw/pw_group.c
@@ -274,8 +274,7 @@ pw_group(struct userconf * cnf, int mode, struct cargs * args)
pw_log(cnf, mode, W_GROUP, "%s(%ld)", grp->gr_name, (long) grp->gr_gid);
- if (members)
- free(members);
+ free(members);
return EXIT_SUCCESS;
}
@@ -408,7 +407,7 @@ print_group(struct group * grp, int pretty)
char *buf = NULL;
buf = gr_make(grp);
- fputs(buf, stdout);
+ printf("%s\n", buf);
free(buf);
} else {
int i;
diff --git a/usr.sbin/pw/pw_log.c b/usr.sbin/pw/pw_log.c
index f16274f..b774423 100644
--- a/usr.sbin/pw/pw_log.c
+++ b/usr.sbin/pw/pw_log.c
@@ -47,7 +47,6 @@ pw_log(struct userconf * cnf, int mode, int which, char const * fmt,...)
}
if (logfile != NULL) {
va_list argp;
- int l;
time_t now = time(NULL);
struct tm *t = localtime(&now);
char nfmt[256];
@@ -57,7 +56,6 @@ pw_log(struct userconf * cnf, int mode, int which, char const * fmt,...)
name = "unknown";
/* ISO 8601 International Standard Date format */
strftime(nfmt, sizeof nfmt, "%Y-%m-%d %T ", t);
- l = strlen(nfmt);
sprintf(nfmt + strlen(nfmt), "[%s:%s%s] %s\n", name, Which[which], Modes[mode], fmt);
va_start(argp, fmt);
vfprintf(logfile, nfmt, argp);
diff --git a/usr.sbin/pw/pw_nis.c b/usr.sbin/pw/pw_nis.c
index af5901a..918fc30 100644
--- a/usr.sbin/pw/pw_nis.c
+++ b/usr.sbin/pw/pw_nis.c
@@ -66,6 +66,8 @@ pw_nisupdate(const char * path, struct passwd * pwd, char const * user)
pw_fini();
err(1, "pw_copy()");
}
+ if (chmod(pw_tempname(), 0644) == -1)
+ err(1, "chmod()");
if (rename(pw_tempname(), path) == -1)
err(1, "rename()");
diff --git a/usr.sbin/pw/pw_user.c b/usr.sbin/pw/pw_user.c
index abf1c35..5f4d7a9 100644
--- a/usr.sbin/pw/pw_user.c
+++ b/usr.sbin/pw/pw_user.c
@@ -394,7 +394,7 @@ pw_user(struct userconf * cnf, int mode, struct cargs * args)
/*
* Remove crontabs
*/
- sprintf(file, "/var/cron/tabs/%s", pwd->pw_name);
+ snprintf(file, sizeof(file), "/var/cron/tabs/%s", pwd->pw_name);
if (access(file, F_OK) == 0) {
sprintf(file, "crontab -u %s -r", pwd->pw_name);
system(file);
@@ -425,7 +425,7 @@ pw_user(struct userconf * cnf, int mode, struct cargs * args)
}
grp = GETGRNAM(a_name->val);
- if (*grp->gr_mem == NULL)
+ if (grp != NULL && *grp->gr_mem == NULL)
delgrent(GETGRNAM(a_name->val));
SETGRENT();
while ((grp = GETGRENT()) != NULL) {
@@ -745,25 +745,20 @@ pw_user(struct userconf * cnf, int mode, struct cargs * args)
*/
if (mode == M_ADD || getarg(args, 'G') != NULL) {
- int i, j;
+ int i;
for (i = 0; cnf->groups[i] != NULL; i++) {
grp = GETGRNAM(cnf->groups[i]);
- for (j = 0; grp->gr_mem[j] != NULL; j++) {
- if (!strcmp(grp->gr_mem[j], pwd->pw_name))
- break;
- }
- if (grp->gr_mem[j] != NULL) /* user already member of group */
+ grp = gr_add(grp, pwd->pw_name);
+ /*
+ * grp can only be NULL in 2 cases:
+ * - the new member is already a member
+ * - a problem with memory occurs
+ * in both cases we want to skip now.
+ */
+ if (grp == NULL)
continue;
-
- if (j == 0)
- grp->gr_mem = NULL;
-
- grp->gr_mem = reallocf(grp->gr_mem, sizeof(*grp->gr_mem) *
- (j + 2));
-
- grp->gr_mem[j] = pwd->pw_name;
- grp->gr_mem[j+1] = NULL;
chggrent(cnf->groups[i], grp);
+ free(grp);
}
}
diff --git a/usr.sbin/pw/pw_vpw.c b/usr.sbin/pw/pw_vpw.c
index 674b64f..99663be 100644
--- a/usr.sbin/pw/pw_vpw.c
+++ b/usr.sbin/pw/pw_vpw.c
@@ -30,6 +30,10 @@ static const char rcsid[] =
"$FreeBSD$";
#endif /* not lint */
+#include <pwd.h>
+#include <grp.h>
+#include <libutil.h>
+#define _WITH_GETLINE
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
@@ -55,101 +59,44 @@ vsetpwent(void)
}
static struct passwd *
-vnextpwent(char const * nam, uid_t uid, int doclose)
+vnextpwent(char const *nam, uid_t uid, int doclose)
{
- struct passwd * pw = NULL;
- static char pwtmp[1024];
-
- strlcpy(pwtmp, getpwpath(_MASTERPASSWD), sizeof(pwtmp));
-
- if (pwd_fp != NULL || (pwd_fp = fopen(pwtmp, "r")) != NULL) {
- int done = 0;
-
- static struct passwd pwd;
-
- while (!done && fgets(pwtmp, sizeof pwtmp, pwd_fp) != NULL)
- {
- int i, quickout = 0;
- char * q;
- char * p = strchr(pwtmp, '\n');
-
- if (p == NULL) {
- while (fgets(pwtmp, sizeof pwtmp, pwd_fp) != NULL && strchr(pwtmp, '\n')==NULL)
- ; /* Skip long lines */
- continue;
- }
-
- /* skip comments & empty lines */
- if (*pwtmp =='\n' || *pwtmp == '#')
+ struct passwd *pw;
+ char *line;
+ size_t linecap;
+ ssize_t linelen;
+
+ pw = NULL;
+ line = NULL;
+ linecap = 0;
+ linelen = 0;
+
+ if (pwd_fp != NULL || (pwd_fp = fopen(getpwpath(_MASTERPASSWD), "r")) != NULL) {
+ while ((linelen = getline(&line, &linecap, pwd_fp)) > 0) {
+ /* Skip comments and empty lines */
+ if (*line == '\n' || *line == '#')
continue;
-
- i = 0;
- q = p = pwtmp;
- bzero(&pwd, sizeof pwd);
- while (!quickout && (p = strsep(&q, ":\n")) != NULL) {
- switch (i++)
- {
- case 0: /* username */
- pwd.pw_name = p;
- if (nam) {
- if (strcmp(nam, p) == 0)
- done = 1;
- else
- quickout = 1;
- }
- break;
- case 1: /* password */
- pwd.pw_passwd = p;
- break;
- case 2: /* uid */
- pwd.pw_uid = atoi(p);
- if (uid != (uid_t)-1) {
- if (uid == pwd.pw_uid)
- done = 1;
- else
- quickout = 1;
- }
- break;
- case 3: /* gid */
- pwd.pw_gid = atoi(p);
- break;
- case 4: /* class */
- if (nam == NULL && uid == (uid_t)-1)
- done = 1;
- pwd.pw_class = p;
- break;
- case 5: /* change */
- pwd.pw_change = (time_t)atol(p);
- break;
- case 6: /* expire */
- pwd.pw_expire = (time_t)atol(p);
- break;
- case 7: /* gecos */
- pwd.pw_gecos = p;
- break;
- case 8: /* directory */
- pwd.pw_dir = p;
- break;
- case 9: /* shell */
- pwd.pw_shell = p;
- break;
- }
- }
- }
+ /* trim latest \n */
+ if (line[linelen - 1 ] == '\n')
+ line[linelen - 1] = '\0';
+ pw = pw_scan(line, PWSCAN_MASTER);
+ if (uid != (uid_t)-1) {
+ if (uid == pw->pw_uid)
+ break;
+ } else if (nam != NULL) {
+ if (strcmp(nam, pw->pw_name) == 0)
+ break;
+ } else
+ break;
+ free(pw);
+ pw = NULL;
+ }
if (doclose)
vendpwent();
- if (done && pwd.pw_name) {
- pw = &pwd;
+ }
+ free(line);
- #define CKNULL(s) s = s ? s : ""
- CKNULL(pwd.pw_passwd);
- CKNULL(pwd.pw_class);
- CKNULL(pwd.pw_gecos);
- CKNULL(pwd.pw_dir);
- CKNULL(pwd.pw_shell);
- }
- }
- return pw;
+ return (pw);
}
struct passwd *
@@ -192,93 +139,44 @@ vsetgrent(void)
}
static struct group *
-vnextgrent(char const * nam, gid_t gid, int doclose)
+vnextgrent(char const *nam, gid_t gid, int doclose)
{
- struct group * gr = NULL;
-
- static char * grtmp = NULL;
- static int grlen = 0;
- static char ** mems = NULL;
- static int memlen = 0;
-
- extendline(&grtmp, &grlen, MAXPATHLEN);
- strlcpy(grtmp, getgrpath(_GROUP), MAXPATHLEN);
-
- if (grp_fp != NULL || (grp_fp = fopen(grtmp, "r")) != NULL) {
- int done = 0;
-
- static struct group grp;
-
- while (!done && fgets(grtmp, grlen, grp_fp) != NULL)
- {
- int i, quickout = 0;
- int mno = 0;
- char * q, * p;
- const char * sep = ":\n";
-
- if ((p = strchr(grtmp, '\n')) == NULL) {
- int l;
- extendline(&grtmp, &grlen, grlen + PWBUFSZ);
- l = strlen(grtmp);
- if (fgets(grtmp + l, grlen - l, grp_fp) == NULL)
- break; /* No newline terminator on last line */
- }
+ struct group *gr;
+ char *line;
+ size_t linecap;
+ ssize_t linelen;
+
+ gr = NULL;
+ line = NULL;
+ linecap = 0;
+ linelen = 0;
+
+ if (grp_fp != NULL || (grp_fp = fopen(getgrpath(_GROUP), "r")) != NULL) {
+ while ((linelen = getline(&line, &linecap, grp_fp)) > 0) {
/* Skip comments and empty lines */
- if (*grtmp == '\n' || *grtmp == '#')
+ if (*line == '\n' || *line == '#')
continue;
- i = 0;
- q = p = grtmp;
- bzero(&grp, sizeof grp);
- extendarray(&mems, &memlen, 200);
- while (!quickout && (p = strsep(&q, sep)) != NULL) {
- switch (i++)
- {
- case 0: /* groupname */
- grp.gr_name = p;
- if (nam) {
- if (strcmp(nam, p) == 0)
- done = 1;
- else
- quickout = 1;
- }
- break;
- case 1: /* password */
- grp.gr_passwd = p;
+ /* trim latest \n */
+ if (line[linelen - 1 ] == '\n')
+ line[linelen - 1] = '\0';
+ gr = gr_scan(line);
+ if (gid != (gid_t)-1) {
+ if (gid == gr->gr_gid)
break;
- case 2: /* gid */
- grp.gr_gid = atoi(p);
- if (gid != (gid_t)-1) {
- if (gid == (gid_t)grp.gr_gid)
- done = 1;
- else
- quickout = 1;
- } else if (nam == NULL)
- done = 1;
+ } else if (nam != NULL) {
+ if (strcmp(nam, gr->gr_name) == 0)
break;
- case 3:
- q = p;
- sep = ",\n";
- break;
- default:
- if (*p) {
- extendarray(&mems, &memlen, mno + 2);
- mems[mno++] = p;
- }
- break;
- }
- }
- grp.gr_mem = mems;
- mems[mno] = NULL;
- }
+ } else
+ break;
+ free(gr);
+ gr = NULL;
+ }
if (doclose)
vendgrent();
- if (done && grp.gr_name) {
- gr = &grp;
-
- CKNULL(grp.gr_passwd);
- }
}
- return gr;
+ free(line);
+
+ return (gr);
}
struct group *
diff --git a/usr.sbin/pw/pwupd.c b/usr.sbin/pw/pwupd.c
index 4ab0f01..22662db 100644
--- a/usr.sbin/pw/pwupd.c
+++ b/usr.sbin/pw/pwupd.c
@@ -56,12 +56,10 @@ setpwdir(const char * dir)
{
if (dir == NULL)
return -1;
- else {
- char * d = malloc(strlen(dir)+1);
- if (d == NULL)
- return -1;
- pwpath = strcpy(d, dir);
- }
+ else
+ pwpath = strdup(dir);
+ if (pwpath == NULL)
+ return -1;
return 0;
}
@@ -148,7 +146,11 @@ pw_update(struct passwd * pwd, char const * user)
pw_fini();
err(1, "pw_copy()");
}
- if (pw_mkdb(user) == -1) {
+ /*
+ * in case of deletion of a user, the whole database
+ * needs to be regenerated
+ */
+ if (pw_mkdb(pw != NULL ? user : NULL) == -1) {
pw_fini();
err(1, "pw_mkdb()");
}
diff --git a/usr.sbin/pw/rm_r.c b/usr.sbin/pw/rm_r.c
index 4ad590b..797ca9d 100644
--- a/usr.sbin/pw/rm_r.c
+++ b/usr.sbin/pw/rm_r.c
@@ -52,7 +52,7 @@ rm_r(char const * dir, uid_t uid)
while ((e = readdir(d)) != NULL) {
if (strcmp(e->d_name, ".") != 0 && strcmp(e->d_name, "..") != 0) {
- sprintf(file, "%s/%s", dir, e->d_name);
+ snprintf(file, sizeof(file), "%s/%s", dir, e->d_name);
if (lstat(file, &st) == 0) { /* Need symlinks, not
* linked file */
if (S_ISDIR(st.st_mode)) /* Directory - recurse */
diff --git a/usr.sbin/rarpd/rarpd.c b/usr.sbin/rarpd/rarpd.c
index f10a7da..5386185 100644
--- a/usr.sbin/rarpd/rarpd.c
+++ b/usr.sbin/rarpd/rarpd.c
@@ -692,11 +692,10 @@ rarp_process(struct if_info *ii, u_char *pkt, u_int len)
* host (i.e. the guy running rarpd), won't try to ARP for the hardware
* address of the guy being booted (he cannot answer the ARP).
*/
-struct sockaddr_inarp sin_inarp = {
- sizeof(struct sockaddr_inarp), AF_INET, 0,
+struct sockaddr_in sin_inarp = {
+ sizeof(struct sockaddr_in), AF_INET, 0,
{0},
{0},
- 0, 0
};
struct sockaddr_dl sin_dl = {
sizeof(struct sockaddr_dl), AF_LINK, 0, IFT_ETHER, 0, 6,
@@ -712,7 +711,7 @@ update_arptab(u_char *ep, in_addr_t ipaddr)
{
struct timespec tp;
int cc;
- struct sockaddr_inarp *ar, *ar2;
+ struct sockaddr_in *ar, *ar2;
struct sockaddr_dl *ll, *ll2;
struct rt_msghdr *rt;
int xtype, xindex;
@@ -740,7 +739,7 @@ update_arptab(u_char *ep, in_addr_t ipaddr)
rt->rtm_addrs = RTA_DST;
rt->rtm_type = RTM_GET;
rt->rtm_seq = ++seq;
- ar2 = (struct sockaddr_inarp *)rtmsg.rtspace;
+ ar2 = (struct sockaddr_in *)rtmsg.rtspace;
bcopy(ar, ar2, sizeof(*ar));
rt->rtm_msglen = sizeof(*rt) + sizeof(*ar);
errno = 0;
diff --git a/usr.sbin/route6d/route6d.8 b/usr.sbin/route6d/route6d.8
index 121f5f2..2dfd91c 100644
--- a/usr.sbin/route6d/route6d.8
+++ b/usr.sbin/route6d/route6d.8
@@ -16,7 +16,7 @@
.\"
.\" $FreeBSD$
.\"
-.Dd January 31, 1997
+.Dd November 18, 2012
.Dt ROUTE6D 8
.Os
.Sh NAME
@@ -41,6 +41,15 @@
.Op Fl O Ar prefix/preflen,if1[,if2...\&]
.Ek
.Bk -words
+.Op Fl P Ar number
+.Ek
+.Bk -words
+.Op Fl p Ar pidfile
+.Ek
+.Bk -words
+.Op Fl Q Ar number
+.Ek
+.Bk -words
.Op Fl T Ar if1[,if2...\&]
.Ek
.Bk -words
@@ -80,6 +89,12 @@ and advertises the aggregated route
.Ar prefix/preflen ,
to the interfaces specified in the comma-separated interface list,
.Ar if1[,if2...] .
+The characters
+.Qq Li * ,
+.Qq Li \&? ,
+and
+.Qq Li \&[
+in the interface list will be interpreted as shell-style pattern.
The
.Nm
utility creates a static route to
@@ -143,7 +158,7 @@ option.
For example, with
.Do
.Fl L
-.Li 3ffe::/16,if1
+.Li 2001:db8::/16,if1
.Fl L
.Li ::/0,if1
.Dc
@@ -164,6 +179,33 @@ With this option
.Nm
will only advertise routes that matches
.Ar prefix/preflen .
+.It Fl P Ar number
+Specifies routes to be ignored in calculation of expiration timer.
+The
+.Ar number
+must be
+.Li 1 ,
+.Li 2 ,
+or
+.Li 3
+and it means route flags of
+.Li RTF_PROTO1 ,
+.Li RTF_PROTO2 ,
+or
+.Li RTF_PROTO3 .
+When
+.Li 1
+is specified, routes with
+.Li RTF_PROTO1
+will never expire.
+.It Fl p Ar pidfile
+Specifies an alternative file in which to store the process ID.
+The default is
+.Pa /var/run/route6d.pid .
+.It Fl Q Ar number
+Specifies flag which will be used for routes added by RIP protocol.
+The default is
+.Li 2 Pq Li RTF_PROTO2 .
.\"
.It Fl q
Makes
diff --git a/usr.sbin/route6d/route6d.c b/usr.sbin/route6d/route6d.c
index 8723ebe..421ac1a 100644
--- a/usr.sbin/route6d/route6d.c
+++ b/usr.sbin/route6d/route6d.c
@@ -31,13 +31,14 @@
*/
#ifndef lint
-static char _rcsid[] = "$KAME: route6d.c,v 1.104 2003/10/31 00:30:20 itojun Exp $";
+static const char _rcsid[] = "$KAME: route6d.c,v 1.104 2003/10/31 00:30:20 itojun Exp $";
#endif
#include <stdio.h>
#include <time.h>
#include <unistd.h>
+#include <fnmatch.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
@@ -78,6 +79,7 @@ static char _rcsid[] = "$KAME: route6d.c,v 1.104 2003/10/31 00:30:20 itojun Exp
#include "route6d.h"
#define MAXFILTER 40
+#define RT_DUMP_MAXRETRY 15
#ifdef DEBUG
#define INIT_INTERVAL6 6
@@ -90,21 +92,10 @@ static char _rcsid[] = "$KAME: route6d.c,v 1.104 2003/10/31 00:30:20 itojun Exp
((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
#define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
-/*
- * Following two macros are highly depending on KAME Release
- */
-#define IN6_LINKLOCAL_IFINDEX(addr) \
- ((addr).s6_addr[2] << 8 | (addr).s6_addr[3])
-
-#define SET_IN6_LINKLOCAL_IFINDEX(addr, index) \
- do { \
- (addr).s6_addr[2] = ((index) >> 8) & 0xff; \
- (addr).s6_addr[3] = (index) & 0xff; \
- } while (0)
-
-struct ifc { /* Configuration of an interface */
- char *ifc_name; /* if name */
- struct ifc *ifc_next;
+struct ifc { /* Configuration of an interface */
+ TAILQ_ENTRY(ifc) ifc_next;
+
+ char ifc_name[IFNAMSIZ]; /* if name */
int ifc_index; /* if index */
int ifc_mtu; /* if mtu */
int ifc_metric; /* if metric */
@@ -112,30 +103,32 @@ struct ifc { /* Configuration of an interface */
short ifc_cflags; /* IFC_XXX */
struct in6_addr ifc_mylladdr; /* my link-local address */
struct sockaddr_in6 ifc_ripsin; /* rip multicast address */
- struct iff *ifc_filter; /* filter structure */
- struct ifac *ifc_addr; /* list of AF_INET6 addresses */
+ TAILQ_HEAD(, ifac) ifc_ifac_head; /* list of AF_INET6 addrs */
+ TAILQ_HEAD(, iff) ifc_iff_head; /* list of filters */
int ifc_joined; /* joined to ff02::9 */
};
+TAILQ_HEAD(, ifc) ifc_head = TAILQ_HEAD_INITIALIZER(ifc_head);
-struct ifac { /* Adddress associated to an interface */
- struct ifc *ifa_conf; /* back pointer */
- struct ifac *ifa_next;
- struct in6_addr ifa_addr; /* address */
- struct in6_addr ifa_raddr; /* remote address, valid in p2p */
- int ifa_plen; /* prefix length */
+struct ifac { /* Adddress associated to an interface */
+ TAILQ_ENTRY(ifac) ifac_next;
+
+ struct ifc *ifac_ifc; /* back pointer */
+ struct in6_addr ifac_addr; /* address */
+ struct in6_addr ifac_raddr; /* remote address, valid in p2p */
+ int ifac_scope_id; /* scope id */
+ int ifac_plen; /* prefix length */
};
-struct iff {
+struct iff { /* Filters for an interface */
+ TAILQ_ENTRY(iff) iff_next;
+
int iff_type;
struct in6_addr iff_addr;
int iff_plen;
- struct iff *iff_next;
};
-struct ifc *ifc;
-int nifc; /* number of valid ifc's */
struct ifc **index2ifc;
-int nindex2ifc;
+unsigned int nindex2ifc;
struct ifc *loopifcp = NULL; /* pointing to loopback */
#ifdef HAVE_POLL_H
struct pollfd set[2];
@@ -162,8 +155,9 @@ struct rip6 *ripbuf; /* packet buffer for sending */
* suppressing the specifics covered by the aggregate.
*/
-struct riprt {
- struct riprt *rrt_next; /* next destination */
+struct riprt {
+ TAILQ_ENTRY(riprt) rrt_next; /* next destination */
+
struct riprt *rrt_same; /* same destination - future use */
struct netinfo6 rrt_info; /* network info */
struct in6_addr rrt_gw; /* gateway */
@@ -172,8 +166,7 @@ struct riprt {
time_t rrt_t; /* when the route validated */
int rrt_index; /* ifindex from which this route got */
};
-
-struct riprt *riprt = 0;
+TAILQ_HEAD(, riprt) riprt_head = TAILQ_HEAD_INITIALIZER(riprt_head);
int dflag = 0; /* debug flag */
int qflag = 0; /* quiet flag */
@@ -181,6 +174,8 @@ int nflag = 0; /* don't update kernel routing table */
int aflag = 0; /* age out even the statically defined routes */
int hflag = 0; /* don't split horizon */
int lflag = 0; /* exchange site local routes */
+int Pflag = 0; /* don't age out routes with RTF_PROTO[123] */
+int Qflag = RTF_PROTO2; /* set RTF_PROTO[123] flag to routes by RIPng */
int sflag = 0; /* announce static routes w/ split horizon */
int Sflag = 0; /* announce static routes to every interface */
unsigned long routetag = 0; /* route tag attached on originating case */
@@ -222,7 +217,7 @@ int out_filter(struct riprt *, struct ifc *);
void init(void);
void sockopt(struct ifc *);
void ifconfig(void);
-void ifconfig1(const char *, const struct sockaddr *, struct ifc *, int);
+int ifconfig1(const char *, const struct sockaddr *, struct ifc *, int);
void rtrecv(void);
int rt_del(const struct sockaddr_in6 *, const struct sockaddr_in6 *,
const struct sockaddr_in6 *);
@@ -240,12 +235,13 @@ void applyplen(struct in6_addr *, int);
void ifrtdump(int);
void ifdump(int);
void ifdump0(FILE *, const struct ifc *);
+void ifremove(int);
void rtdump(int);
void rt_entry(struct rt_msghdr *, int);
void rtdexit(void);
void riprequest(struct ifc *, struct netinfo6 *, int,
struct sockaddr_in6 *);
-void ripflush(struct ifc *, struct sockaddr_in6 *);
+void ripflush(struct ifc *, struct sockaddr_in6 *, int, struct netinfo6 *np);
void sendrequest(struct ifc *);
int sin6mask2len(const struct sockaddr_in6 *);
int mask2len(const struct in6_addr *, int);
@@ -260,7 +256,7 @@ char *hms(void);
const char *inet6_n2p(const struct in6_addr *);
struct ifac *ifa_match(const struct ifc *, const struct in6_addr *, int);
struct in6_addr *plen2mask(int);
-struct riprt *rtsearch(struct netinfo6 *, struct riprt **);
+struct riprt *rtsearch(struct netinfo6 *);
int ripinterval(int);
time_t ripsuptrig(void);
void fatal(const char *, ...)
@@ -276,16 +272,23 @@ void setindex2ifc(int, struct ifc *);
#define MALLOC(type) ((type *)malloc(sizeof(type)))
+#define IFIL_TYPE_ANY 0x0
+#define IFIL_TYPE_A 'A'
+#define IFIL_TYPE_N 'N'
+#define IFIL_TYPE_T 'T'
+#define IFIL_TYPE_O 'O'
+#define IFIL_TYPE_L 'L'
+
int
-main(argc, argv)
- int argc;
- char **argv;
+main(int argc, char *argv[])
{
int ch;
int error = 0;
+ unsigned long proto;
struct ifc *ifcp;
sigset_t mask, omask;
- FILE *pidfile;
+ const char *pidfile = ROUTE6D_PID;
+ FILE *pidfh;
char *progname;
char *ep;
@@ -296,7 +299,7 @@ main(argc, argv)
progname = *argv;
pid = getpid();
- while ((ch = getopt(argc, argv, "A:N:O:R:T:L:t:adDhlnqsS")) != -1) {
+ while ((ch = getopt(argc, argv, "A:N:O:R:T:L:t:adDhlnp:P:Q:qsS")) != -1) {
switch (ch) {
case 'A':
case 'N':
@@ -318,6 +321,41 @@ main(argc, argv)
/*NOTREACHED*/
}
break;
+ case 'p':
+ pidfile = optarg;
+ break;
+ case 'P':
+ ep = NULL;
+ proto = strtoul(optarg, &ep, 0);
+ if (!ep || *ep != '\0' || 3 < proto) {
+ fatal("invalid P flag");
+ /*NOTREACHED*/
+ }
+ if (proto == 0)
+ Pflag = 0;
+ if (proto == 1)
+ Pflag |= RTF_PROTO1;
+ if (proto == 2)
+ Pflag |= RTF_PROTO2;
+ if (proto == 3)
+ Pflag |= RTF_PROTO3;
+ break;
+ case 'Q':
+ ep = NULL;
+ proto = strtoul(optarg, &ep, 0);
+ if (!ep || *ep != '\0' || 3 < proto) {
+ fatal("invalid Q flag");
+ /*NOTREACHED*/
+ }
+ if (proto == 0)
+ Qflag = 0;
+ if (proto == 1)
+ Qflag |= RTF_PROTO1;
+ if (proto == 2)
+ Qflag |= RTF_PROTO2;
+ if (proto == 3)
+ Qflag |= RTF_PROTO3;
+ break;
case 'R':
if ((rtlog = fopen(optarg, "w")) == NULL) {
fatal("Can not write to routelog");
@@ -372,11 +410,10 @@ main(argc, argv)
init();
ifconfig();
- for (ifcp = ifc; ifcp; ifcp = ifcp->ifc_next) {
+ TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
if (ifcp->ifc_index < 0) {
- fprintf(stderr,
-"No ifindex found at %s (no link-local address?)\n",
- ifcp->ifc_name);
+ fprintf(stderr, "No ifindex found at %s "
+ "(no link-local address?)\n", ifcp->ifc_name);
error++;
}
}
@@ -386,20 +423,19 @@ main(argc, argv)
fatal("No loopback found");
/*NOTREACHED*/
}
- for (ifcp = ifc; ifcp; ifcp = ifcp->ifc_next)
+ TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
ifrt(ifcp, 0);
+ }
filterconfig();
krtread(0);
if (dflag)
ifrtdump(0);
-#if 1
pid = getpid();
- if ((pidfile = fopen(ROUTE6D_PID, "w")) != NULL) {
- fprintf(pidfile, "%d\n", pid);
- fclose(pidfile);
+ if ((pidfh = fopen(pidfile, "w")) != NULL) {
+ fprintf(pidfh, "%d\n", pid);
+ fclose(pidfh);
}
-#endif
if ((ripbuf = (struct rip6 *)malloc(RIP6_MAXMTU)) == NULL) {
fatal("malloc");
@@ -427,8 +463,8 @@ main(argc, argv)
*/
alarm(ripinterval(INIT_INTERVAL6));
- for (ifcp = ifc; ifcp; ifcp = ifcp->ifc_next) {
- if (iff_find(ifcp, 'N'))
+ TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
+ if (iff_find(ifcp, IFIL_TYPE_N) != NULL)
continue;
if (ifcp->ifc_index > 0 && (ifcp->ifc_flags & IFF_UP))
sendrequest(ifcp);
@@ -495,8 +531,7 @@ main(argc, argv)
}
void
-sighandler(signo)
- int signo;
+sighandler(int signo)
{
switch (signo) {
@@ -520,12 +555,12 @@ sighandler(signo)
*/
/* ARGSUSED */
void
-rtdexit()
+rtdexit(void)
{
struct riprt *rrt;
alarm(0);
- for (rrt = riprt; rrt; rrt = rrt->rrt_next) {
+ TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
if (rrt->rrt_rflags & RRTF_AGGREGATE) {
delroute(&rrt->rrt_info, &rrt->rrt_gw);
}
@@ -547,39 +582,27 @@ rtdexit()
*/
/* ARGSUSED */
void
-ripalarm()
+ripalarm(void)
{
struct ifc *ifcp;
- struct riprt *rrt, *rrt_prev, *rrt_next;
+ struct riprt *rrt, *rrt_tmp;
time_t t_lifetime, t_holddown;
/* age the RIP routes */
- rrt_prev = 0;
t_lifetime = time(NULL) - RIP_LIFETIME;
t_holddown = t_lifetime - RIP_HOLDDOWN;
- for (rrt = riprt; rrt; rrt = rrt_next) {
- rrt_next = rrt->rrt_next;
-
- if (rrt->rrt_t == 0) {
- rrt_prev = rrt;
+ TAILQ_FOREACH_SAFE(rrt, &riprt_head, rrt_next, rrt_tmp) {
+ if (rrt->rrt_t == 0)
continue;
- }
- if (rrt->rrt_t < t_holddown) {
- if (rrt_prev) {
- rrt_prev->rrt_next = rrt->rrt_next;
- } else {
- riprt = rrt->rrt_next;
- }
+ else if (rrt->rrt_t < t_holddown) {
+ TAILQ_REMOVE(&riprt_head, rrt, rrt_next);
delroute(&rrt->rrt_info, &rrt->rrt_gw);
free(rrt);
- continue;
- }
- if (rrt->rrt_t < t_lifetime)
+ } else if (rrt->rrt_t < t_lifetime)
rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6;
- rrt_prev = rrt;
}
/* Supply updates */
- for (ifcp = ifc; ifcp; ifcp = ifcp->ifc_next) {
+ TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
if (ifcp->ifc_index > 0 && (ifcp->ifc_flags & IFF_UP))
ripsend(ifcp, &ifcp->ifc_ripsin, 0);
}
@@ -587,15 +610,14 @@ ripalarm()
}
void
-init()
+init(void)
{
int error;
const int int0 = 0, int1 = 1, int255 = 255;
struct addrinfo hints, *res;
char port[NI_MAXSERV];
- ifc = (struct ifc *)NULL;
- nifc = 0;
+ TAILQ_INIT(&ifc_head);
nindex2ifc = 0; /*initial guess*/
index2ifc = NULL;
snprintf(port, sizeof(port), "%u", RIP6_PORT);
@@ -735,13 +757,8 @@ init()
/*
* ripflush flushes the rip datagram stored in the rip buffer
*/
-static int nrt;
-static struct netinfo6 *np;
-
void
-ripflush(ifcp, sin6)
- struct ifc *ifcp;
- struct sockaddr_in6 *sin6;
+ripflush(struct ifc *ifcp, struct sockaddr_in6 *sin6, int nrt, struct netinfo6 *np)
{
int i;
int error;
@@ -783,21 +800,19 @@ ripflush(ifcp, sin6)
ifcp->ifc_name, inet6_n2p(&ifcp->ifc_ripsin.sin6_addr));
ifcp->ifc_flags &= ~IFF_UP; /* As if down for AF_INET6 */
}
- nrt = 0; np = ripbuf->rip6_nets;
}
/*
* Generate RIP6_RESPONSE packets and send them.
*/
void
-ripsend(ifcp, sin6, flag)
- struct ifc *ifcp;
- struct sockaddr_in6 *sin6;
- int flag;
+ripsend(struct ifc *ifcp, struct sockaddr_in6 *sin6, int flag)
{
struct riprt *rrt;
struct in6_addr *nh; /* next hop */
+ struct netinfo6 *np;
int maxrte;
+ int nrt;
if (qflag)
return;
@@ -811,20 +826,24 @@ ripsend(ifcp, sin6, flag)
sizeof(struct udphdr) -
sizeof(struct rip6) + sizeof(struct netinfo6)) /
sizeof(struct netinfo6);
- nrt = 0; np = ripbuf->rip6_nets; nh = NULL;
- for (rrt = riprt; rrt; rrt = rrt->rrt_next) {
+ nh = NULL;
+ nrt = 0;
+ np = ripbuf->rip6_nets;
+ TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
if (rrt->rrt_rflags & RRTF_NOADVERTISE)
continue;
/* Put the route to the buffer */
*np = rrt->rrt_info;
np++; nrt++;
if (nrt == maxrte) {
- ripflush(NULL, sin6);
+ ripflush(NULL, sin6, nrt, np);
nh = NULL;
+ nrt = 0;
+ np = ripbuf->rip6_nets;
}
}
if (nrt) /* Send last packet */
- ripflush(NULL, sin6);
+ ripflush(NULL, sin6, nrt, np);
return;
}
@@ -833,11 +852,11 @@ ripsend(ifcp, sin6, flag)
return;
/* -N: no use */
- if (iff_find(ifcp, 'N') != NULL)
+ if (iff_find(ifcp, IFIL_TYPE_N) != NULL)
return;
/* -T: generate default route only */
- if (iff_find(ifcp, 'T') != NULL) {
+ if (iff_find(ifcp, IFIL_TYPE_T) != NULL) {
struct netinfo6 rrt_info;
memset(&rrt_info, 0, sizeof(struct netinfo6));
rrt_info.rip6_dest = in6addr_any;
@@ -848,7 +867,7 @@ ripsend(ifcp, sin6, flag)
np = ripbuf->rip6_nets;
*np = rrt_info;
nrt = 1;
- ripflush(ifcp, sin6);
+ ripflush(ifcp, sin6, nrt, np);
return;
}
@@ -858,7 +877,7 @@ ripsend(ifcp, sin6, flag)
sizeof(struct netinfo6);
nrt = 0; np = ripbuf->rip6_nets; nh = NULL;
- for (rrt = riprt; rrt; rrt = rrt->rrt_next) {
+ TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
if (rrt->rrt_rflags & RRTF_NOADVERTISE)
continue;
@@ -880,11 +899,14 @@ ripsend(ifcp, sin6, flag)
!IN6_IS_ADDR_UNSPECIFIED(&rrt->rrt_gw) &&
(rrt->rrt_rflags & RRTF_NH_NOT_LLADDR) == 0) {
if (nh == NULL || !IN6_ARE_ADDR_EQUAL(nh, &rrt->rrt_gw)) {
- if (nrt == maxrte - 2)
- ripflush(ifcp, sin6);
+ if (nrt == maxrte - 2) {
+ ripflush(ifcp, sin6, nrt, np);
+ nh = NULL;
+ nrt = 0;
+ np = ripbuf->rip6_nets;
+ }
+
np->rip6_dest = rrt->rrt_gw;
- if (IN6_IS_ADDR_LINKLOCAL(&np->rip6_dest))
- SET_IN6_LINKLOCAL_IFINDEX(np->rip6_dest, 0);
np->rip6_plen = 0;
np->rip6_tag = 0;
np->rip6_metric = NEXTHOP_METRIC;
@@ -895,8 +917,12 @@ ripsend(ifcp, sin6, flag)
!IN6_ARE_ADDR_EQUAL(nh, &rrt->rrt_gw) ||
rrt->rrt_rflags & RRTF_NH_NOT_LLADDR)) {
/* Reset nexthop */
- if (nrt == maxrte - 2)
- ripflush(ifcp, sin6);
+ if (nrt == maxrte - 2) {
+ ripflush(ifcp, sin6, nrt, np);
+ nh = NULL;
+ nrt = 0;
+ np = ripbuf->rip6_nets;
+ }
memset(np, 0, sizeof(struct netinfo6));
np->rip6_metric = NEXTHOP_METRIC;
nh = NULL;
@@ -907,21 +933,21 @@ ripsend(ifcp, sin6, flag)
*np = rrt->rrt_info;
np++; nrt++;
if (nrt == maxrte) {
- ripflush(ifcp, sin6);
+ ripflush(ifcp, sin6, nrt, np);
nh = NULL;
+ nrt = 0;
+ np = ripbuf->rip6_nets;
}
}
if (nrt) /* Send last packet */
- ripflush(ifcp, sin6);
+ ripflush(ifcp, sin6, nrt, np);
}
/*
* outbound filter logic, per-route/interface.
*/
int
-out_filter(rrt, ifcp)
- struct riprt *rrt;
- struct ifc *ifcp;
+out_filter(struct riprt *rrt, struct ifc *ifcp)
{
struct iff *iffp;
struct in6_addr ia;
@@ -931,7 +957,7 @@ out_filter(rrt, ifcp)
* -A: filter out less specific routes, if we have aggregated
* route configured.
*/
- for (iffp = ifcp->ifc_filter; iffp; iffp = iffp->iff_next) {
+ TAILQ_FOREACH(iffp, &ifcp->ifc_iff_head, iff_next) {
if (iffp->iff_type != 'A')
continue;
if (rrt->rrt_info.rip6_plen <= iffp->iff_plen)
@@ -948,7 +974,7 @@ out_filter(rrt, ifcp)
*/
if ((rrt->rrt_rflags & RRTF_AGGREGATE) != 0) {
ok = 0;
- for (iffp = ifcp->ifc_filter; iffp; iffp = iffp->iff_next) {
+ TAILQ_FOREACH(iffp, &ifcp->ifc_iff_head, iff_next) {
if (iffp->iff_type != 'A')
continue;
if (rrt->rrt_info.rip6_plen == iffp->iff_plen &&
@@ -965,9 +991,9 @@ out_filter(rrt, ifcp)
/*
* -O: advertise only if prefix matches the configured prefix.
*/
- if (iff_find(ifcp, 'O')) {
+ if (iff_find(ifcp, IFIL_TYPE_O) != NULL) {
ok = 0;
- for (iffp = ifcp->ifc_filter; iffp; iffp = iffp->iff_next) {
+ TAILQ_FOREACH(iffp, &ifcp->ifc_iff_head, iff_next) {
if (iffp->iff_type != 'O')
continue;
if (rrt->rrt_info.rip6_plen < iffp->iff_plen)
@@ -992,9 +1018,7 @@ out_filter(rrt, ifcp)
* It checks options specified in the arguments and the split horizon rule.
*/
int
-tobeadv(rrt, ifcp)
- struct riprt *rrt;
- struct ifc *ifcp;
+tobeadv(struct riprt *rrt, struct ifc *ifcp)
{
/* Special care for static routes */
@@ -1019,9 +1043,7 @@ tobeadv(rrt, ifcp)
* Send a rip packet actually.
*/
int
-sendpacket(sin6, len)
- struct sockaddr_in6 *sin6;
- int len;
+sendpacket(struct sockaddr_in6 *sin6, int len)
{
struct msghdr m;
struct cmsghdr *cm;
@@ -1036,12 +1058,9 @@ sendpacket(sin6, len)
sin6 = &sincopy;
if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) ||
- IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
- /* XXX: do not mix the interface index and link index */
- idx = IN6_LINKLOCAL_IFINDEX(sin6->sin6_addr);
- SET_IN6_LINKLOCAL_IFINDEX(sin6->sin6_addr, 0);
- sin6->sin6_scope_id = idx;
- } else
+ IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
+ idx = sin6->sin6_scope_id;
+ else
idx = 0;
m.msg_name = (caddr_t)sin6;
@@ -1080,7 +1099,7 @@ sendpacket(sin6, len)
* table if necessary.
*/
void
-riprecv()
+riprecv(void)
{
struct ifc *ifcp, *ic;
struct sockaddr_in6 fsock;
@@ -1144,10 +1163,8 @@ riprecv()
break;
}
}
- if (idx && IN6_IS_ADDR_LINKLOCAL(&fsock.sin6_addr))
- SET_IN6_LINKLOCAL_IFINDEX(fsock.sin6_addr, idx);
- if (len < sizeof(struct rip6)) {
+ if ((size_t)len < sizeof(struct rip6)) {
trace(1, "Packet too short\n");
return;
}
@@ -1221,7 +1238,7 @@ riprecv()
return;
}
- idx = IN6_LINKLOCAL_IFINDEX(fsock.sin6_addr);
+ idx = fsock.sin6_scope_id;
ifcp = (idx < nindex2ifc) ? index2ifc[idx] : NULL;
if (!ifcp) {
trace(1, "Packets to unknown interface index %d\n", idx);
@@ -1235,7 +1252,7 @@ riprecv()
}
/* -N: no use */
- if (iff_find(ifcp, 'N') != NULL)
+ if (iff_find(ifcp, IFIL_TYPE_N) != NULL)
return;
tracet(1, "Recv(%s): from %s.%d info(%zd)\n",
@@ -1248,7 +1265,6 @@ riprecv()
/* modify neighbor address */
if (IN6_IS_ADDR_LINKLOCAL(&np->rip6_dest)) {
nh = np->rip6_dest;
- SET_IN6_LINKLOCAL_IFINDEX(nh, idx);
trace(1, "\tNexthop: %s\n", inet6_n2p(&nh));
} else if (IN6_IS_ADDR_UNSPECIFIED(&np->rip6_dest)) {
nh = fsock.sin6_addr;
@@ -1300,23 +1316,23 @@ riprecv()
/*
* -L: listen only if the prefix matches the configuration
*/
- ok = 1; /* if there's no L filter, it is ok */
- for (iffp = ifcp->ifc_filter; iffp; iffp = iffp->iff_next) {
- if (iffp->iff_type != 'L')
- continue;
- ok = 0;
- if (np->rip6_plen < iffp->iff_plen)
- continue;
- /* special rule: ::/0 means default, not "in /0" */
- if (iffp->iff_plen == 0 && np->rip6_plen > 0)
- continue;
- ia = np->rip6_dest;
- applyplen(&ia, iffp->iff_plen);
- if (IN6_ARE_ADDR_EQUAL(&ia, &iffp->iff_addr)) {
- ok = 1;
- break;
- }
- }
+ ok = 1; /* if there's no L filter, it is ok */
+ TAILQ_FOREACH(iffp, &ifcp->ifc_iff_head, iff_next) {
+ if (iffp->iff_type != IFIL_TYPE_L)
+ continue;
+ ok = 0;
+ if (np->rip6_plen < iffp->iff_plen)
+ continue;
+ /* special rule: ::/0 means default, not "in /0" */
+ if (iffp->iff_plen == 0 && np->rip6_plen > 0)
+ continue;
+ ia = np->rip6_dest;
+ applyplen(&ia, iffp->iff_plen);
+ if (IN6_ARE_ADDR_EQUAL(&ia, &iffp->iff_addr)) {
+ ok = 1;
+ break;
+ }
+ }
if (!ok) {
trace(2, " (filtered)\n");
continue;
@@ -1329,7 +1345,7 @@ riprecv()
np->rip6_metric = HOPCNT_INFINITY6;
applyplen(&np->rip6_dest, np->rip6_plen);
- if ((rrt = rtsearch(np, NULL)) != NULL) {
+ if ((rrt = rtsearch(np)) != NULL) {
if (rrt->rrt_t == 0)
continue; /* Intf route has priority */
nq = &rrt->rrt_info;
@@ -1398,20 +1414,20 @@ riprecv()
if (nq->rip6_plen == sizeof(struct in6_addr) * 8)
rrt->rrt_flags |= RTF_HOST;
- /* Put the route to the list */
- rrt->rrt_next = riprt;
- riprt = rrt;
/* Update routing table */
addroute(rrt, &nh, ifcp);
rrt->rrt_rflags |= RRTF_CHANGED;
need_trigger = 1;
rrt->rrt_t = t;
+
+ /* Put the route to the list */
+ TAILQ_INSERT_HEAD(&riprt_head, rrt, rrt_next);
}
}
/* XXX need to care the interval between triggered updates */
if (need_trigger) {
if (nextalarm > time(NULL) + RIP_TRIG_INT6_MAX) {
- for (ic = ifc; ic; ic = ic->ifc_next) {
+ TAILQ_FOREACH(ic, &ifc_head, ifc_next) {
if (ifcp->ifc_index == ic->ifc_index)
continue;
if (ic->ifc_flags & IFF_UP)
@@ -1420,8 +1436,9 @@ riprecv()
}
}
/* Reset the flag */
- for (rrt = riprt; rrt; rrt = rrt->rrt_next)
+ TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
rrt->rrt_rflags &= ~RRTF_CHANGED;
+ }
}
}
@@ -1429,8 +1446,7 @@ riprecv()
* Send all routes request packet to the specified interface.
*/
void
-sendrequest(ifcp)
- struct ifc *ifcp;
+sendrequest(struct ifc *ifcp)
{
struct netinfo6 *np;
int error;
@@ -1458,11 +1474,10 @@ sendrequest(ifcp)
* Process a RIP6_REQUEST packet.
*/
void
-riprequest(ifcp, np, nn, sin6)
- struct ifc *ifcp;
- struct netinfo6 *np;
- int nn;
- struct sockaddr_in6 *sin6;
+riprequest(struct ifc *ifcp,
+ struct netinfo6 *np,
+ int nn,
+ struct sockaddr_in6 *sin6)
{
int i;
struct riprt *rrt;
@@ -1472,7 +1487,7 @@ riprequest(ifcp, np, nn, sin6)
/* Specific response, don't split-horizon */
trace(1, "\tRIP Request\n");
for (i = 0; i < nn; i++, np++) {
- rrt = rtsearch(np, NULL);
+ rrt = rtsearch(np);
if (rrt)
np->rip6_metric = rrt->rrt_info.rip6_metric;
else
@@ -1490,7 +1505,7 @@ riprequest(ifcp, np, nn, sin6)
* Get information of each interface.
*/
void
-ifconfig()
+ifconfig(void)
{
struct ifaddrs *ifap, *ifa;
struct ifc *ifcp;
@@ -1521,14 +1536,14 @@ ifconfig()
/*NOTREACHED*/
}
memset(ifcp, 0, sizeof(*ifcp));
+
ifcp->ifc_index = -1;
- ifcp->ifc_next = ifc;
- ifc = ifcp;
- nifc++;
- ifcp->ifc_name = allocopy(ifa->ifa_name);
- ifcp->ifc_addr = 0;
- ifcp->ifc_filter = 0;
+ strlcpy(ifcp->ifc_name, ifa->ifa_name,
+ sizeof(ifcp->ifc_name));
+ TAILQ_INIT(&ifcp->ifc_ifac_head);
+ TAILQ_INIT(&ifcp->ifc_iff_head);
ifcp->ifc_flags = ifa->ifa_flags;
+ TAILQ_INSERT_HEAD(&ifc_head, ifcp, ifc_next);
trace(1, "newif %s <%s>\n", ifcp->ifc_name,
ifflags(ifcp->ifc_flags));
if (!strcmp(ifcp->ifc_name, LOOPBACK_IF))
@@ -1543,7 +1558,10 @@ ifconfig()
}
ifcp->ifc_flags = ifa->ifa_flags;
}
- ifconfig1(ifa->ifa_name, ifa->ifa_addr, ifcp, s);
+ if (ifconfig1(ifa->ifa_name, ifa->ifa_addr, ifcp, s) < 0) {
+ /* maybe temporary failure */
+ continue;
+ }
if ((ifcp->ifc_flags & (IFF_LOOPBACK | IFF_UP)) == IFF_UP
&& 0 < ifcp->ifc_index && !ifcp->ifc_joined) {
mreq.ipv6mr_multiaddr = ifcp->ifc_ripsin.sin6_addr;
@@ -1561,68 +1579,67 @@ ifconfig()
freeifaddrs(ifap);
}
-void
-ifconfig1(name, sa, ifcp, s)
- const char *name;
- const struct sockaddr *sa;
- struct ifc *ifcp;
- int s;
+int
+ifconfig1(const char *name,
+ const struct sockaddr *sa,
+ struct ifc *ifcp,
+ int s)
{
struct in6_ifreq ifr;
const struct sockaddr_in6 *sin6;
- struct ifac *ifa;
+ struct ifac *ifac;
int plen;
char buf[BUFSIZ];
sin6 = (const struct sockaddr_in6 *)sa;
if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr) && !lflag)
- return;
+ return (-1);
ifr.ifr_addr = *sin6;
strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
if (ioctl(s, SIOCGIFNETMASK_IN6, (char *)&ifr) < 0) {
- fatal("ioctl: SIOCGIFNETMASK_IN6");
- /*NOTREACHED*/
+ syslog(LOG_INFO, "ioctl: SIOCGIFNETMASK_IN6");
+ return (-1);
}
plen = sin6mask2len(&ifr.ifr_addr);
- if ((ifa = ifa_match(ifcp, &sin6->sin6_addr, plen)) != NULL) {
+ if ((ifac = ifa_match(ifcp, &sin6->sin6_addr, plen)) != NULL) {
/* same interface found */
/* need check if something changed */
/* XXX not yet implemented */
- return;
+ return (-1);
}
/*
* New address is found
*/
- if ((ifa = MALLOC(struct ifac)) == NULL) {
+ if ((ifac = MALLOC(struct ifac)) == NULL) {
fatal("malloc: struct ifac");
/*NOTREACHED*/
}
- memset(ifa, 0, sizeof(*ifa));
- ifa->ifa_conf = ifcp;
- ifa->ifa_next = ifcp->ifc_addr;
- ifcp->ifc_addr = ifa;
- ifa->ifa_addr = sin6->sin6_addr;
- ifa->ifa_plen = plen;
+ memset(ifac, 0, sizeof(*ifac));
+
+ ifac->ifac_ifc = ifcp;
+ ifac->ifac_addr = sin6->sin6_addr;
+ ifac->ifac_plen = plen;
+ ifac->ifac_scope_id = sin6->sin6_scope_id;
if (ifcp->ifc_flags & IFF_POINTOPOINT) {
ifr.ifr_addr = *sin6;
if (ioctl(s, SIOCGIFDSTADDR_IN6, (char *)&ifr) < 0) {
fatal("ioctl: SIOCGIFDSTADDR_IN6");
/*NOTREACHED*/
}
- ifa->ifa_raddr = ifr.ifr_dstaddr.sin6_addr;
- inet_ntop(AF_INET6, (void *)&ifa->ifa_raddr, buf, sizeof(buf));
+ ifac->ifac_raddr = ifr.ifr_dstaddr.sin6_addr;
+ inet_ntop(AF_INET6, (void *)&ifac->ifac_raddr, buf,
+ sizeof(buf));
trace(1, "found address %s/%d -- %s\n",
- inet6_n2p(&ifa->ifa_addr), ifa->ifa_plen, buf);
+ inet6_n2p(&ifac->ifac_addr), ifac->ifac_plen, buf);
} else {
trace(1, "found address %s/%d\n",
- inet6_n2p(&ifa->ifa_addr), ifa->ifa_plen);
+ inet6_n2p(&ifac->ifac_addr), ifac->ifac_plen);
}
- if (ifcp->ifc_index < 0 && IN6_IS_ADDR_LINKLOCAL(&ifa->ifa_addr)) {
- ifcp->ifc_mylladdr = ifa->ifa_addr;
- ifcp->ifc_index = IN6_LINKLOCAL_IFINDEX(ifa->ifa_addr);
+ if (ifcp->ifc_index < 0 && IN6_IS_ADDR_LINKLOCAL(&ifac->ifac_addr)) {
+ ifcp->ifc_mylladdr = ifac->ifac_addr;
+ ifcp->ifc_index = ifac->ifac_scope_id;
memcpy(&ifcp->ifc_ripsin, &ripsin, ripsin.ss_len);
- SET_IN6_LINKLOCAL_IFINDEX(ifcp->ifc_ripsin.sin6_addr,
- ifcp->ifc_index);
+ ifcp->ifc_ripsin.sin6_scope_id = ifcp->ifc_index;
setindex2ifc(ifcp->ifc_index, ifcp);
ifcp->ifc_mtu = getifmtu(ifcp->ifc_index);
if (ifcp->ifc_mtu > RIP6_MAXMTU)
@@ -1636,6 +1653,34 @@ ifconfig1(name, sa, ifcp, s)
ifcp->ifc_index, ifcp->ifc_mtu, ifcp->ifc_metric);
} else
ifcp->ifc_cflags |= IFC_CHANGED;
+
+ TAILQ_INSERT_HEAD(&ifcp->ifc_ifac_head, ifac, ifac_next);
+
+ return 0;
+}
+
+void
+ifremove(int ifindex)
+{
+ struct ifc *ifcp;
+ struct riprt *rrt;
+
+ TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
+ if (ifcp->ifc_index == ifindex)
+ break;
+ }
+ if (ifcp == NULL)
+ return;
+
+ tracet(1, "ifremove: %s is departed.\n", ifcp->ifc_name);
+ TAILQ_REMOVE(&ifc_head, ifcp, ifc_next);
+
+ TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
+ if (rrt->rrt_index == ifcp->ifc_index &&
+ rrt->rrt_rflags & RRTF_AGGREGATE)
+ delroute(&rrt->rrt_info, &rrt->rrt_gw);
+ }
+ free(ifcp);
}
/*
@@ -1643,30 +1688,35 @@ ifconfig1(name, sa, ifcp, s)
* Update interface information as necesssary.
*/
void
-rtrecv()
+rtrecv(void)
{
char buf[BUFSIZ];
- char *p, *q;
+ char *p, *q = NULL;
struct rt_msghdr *rtm;
struct ifa_msghdr *ifam;
struct if_msghdr *ifm;
+ struct if_announcemsghdr *ifan;
int len;
struct ifc *ifcp, *ic;
int iface = 0, rtable = 0;
struct sockaddr_in6 *rta[RTAX_MAX];
struct sockaddr_in6 mask;
- int i, addrs;
+ int i, addrs = 0;
struct riprt *rrt;
if ((len = read(rtsock, buf, sizeof(buf))) < 0) {
perror("read from rtsock");
exit(1);
}
+ if (len == 0)
+ return;
+#if 0
if (len < sizeof(*rtm)) {
trace(1, "short read from rtsock: %d (should be > %lu)\n",
len, (u_long)sizeof(*rtm));
return;
}
+#endif
if (dflag >= 2) {
fprintf(stderr, "rtmsg:\n");
for (i = 0; i < len; i++) {
@@ -1677,6 +1727,9 @@ rtrecv()
}
for (p = buf; p - buf < len; p += ((struct rt_msghdr *)p)->rtm_msglen) {
+ if (((struct rt_msghdr *)p)->rtm_version != RTM_VERSION)
+ continue;
+
/* safety against bogus message */
if (((struct rt_msghdr *)p)->rtm_msglen <= 0) {
trace(1, "bogus rtmsg: length=%d\n",
@@ -1698,6 +1751,18 @@ rtrecv()
addrs = ifm->ifm_addrs;
q = (char *)(ifm + 1);
break;
+ case RTM_IFANNOUNCE:
+ ifan = (struct if_announcemsghdr *)p;
+ switch (ifan->ifan_what) {
+ case IFAN_ARRIVAL:
+ iface++;
+ break;
+ case IFAN_DEPARTURE:
+ ifremove(ifan->ifan_index);
+ iface++;
+ break;
+ }
+ break;
default:
rtm = (struct rt_msghdr *)p;
addrs = rtm->rtm_addrs;
@@ -1847,10 +1912,10 @@ rtrecv()
if (iface) {
trace(1, "rtsock: reconfigure interfaces, refresh interface routes\n");
ifconfig();
- for (ifcp = ifc; ifcp; ifcp = ifcp->ifc_next)
+ TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
if (ifcp->ifc_cflags & IFC_CHANGED) {
if (ifrt(ifcp, 1)) {
- for (ic = ifc; ic; ic = ic->ifc_next) {
+ TAILQ_FOREACH(ic, &ifc_head, ifc_next) {
if (ifcp->ifc_index == ic->ifc_index)
continue;
if (ic->ifc_flags & IFF_UP)
@@ -1858,11 +1923,13 @@ rtrecv()
RRTF_CHANGED);
}
/* Reset the flag */
- for (rrt = riprt; rrt; rrt = rrt->rrt_next)
+ TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
rrt->rrt_rflags &= ~RRTF_CHANGED;
+ }
}
ifcp->ifc_cflags &= ~IFC_CHANGED;
}
+ }
}
if (rtable) {
trace(1, "rtsock: read routing table again\n");
@@ -1874,10 +1941,9 @@ rtrecv()
* remove specified route from the internal routing table.
*/
int
-rt_del(sdst, sgw, smask)
- const struct sockaddr_in6 *sdst;
- const struct sockaddr_in6 *sgw;
- const struct sockaddr_in6 *smask;
+rt_del(const struct sockaddr_in6 *sdst,
+ const struct sockaddr_in6 *sgw,
+ const struct sockaddr_in6 *smask)
{
const struct in6_addr *dst = NULL;
const struct in6_addr *gw = NULL;
@@ -1913,7 +1979,7 @@ rt_del(sdst, sgw, smask)
trace(1, "\t%s is an interface route, guessing prefixlen\n",
inet6_n2p(dst));
longest = NULL;
- for (rrt = riprt; rrt; rrt = rrt->rrt_next) {
+ TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
if (IN6_ARE_ADDR_EQUAL(&rrt->rrt_info.rip6_dest,
&sdst->sin6_addr)
&& IN6_IS_ADDR_LOOPBACK(&rrt->rrt_gw)) {
@@ -1946,7 +2012,7 @@ rt_del(sdst, sgw, smask)
applyplen(&ni6.rip6_dest, ni6.rip6_plen); /*to be sure*/
trace(1, "\tfind route %s/%d\n", inet6_n2p(&ni6.rip6_dest),
ni6.rip6_plen);
- if (!rrt && (rrt = rtsearch(&ni6, NULL)) == NULL) {
+ if (!rrt && (rrt = rtsearch(&ni6)) == NULL) {
trace(1, "\tno route found\n");
return -1;
}
@@ -1973,14 +2039,13 @@ rt_del(sdst, sgw, smask)
* remove specified address from internal interface/routing table.
*/
int
-rt_deladdr(ifcp, sifa, smask)
- struct ifc *ifcp;
- const struct sockaddr_in6 *sifa;
- const struct sockaddr_in6 *smask;
+rt_deladdr(struct ifc *ifcp,
+ const struct sockaddr_in6 *sifa,
+ const struct sockaddr_in6 *smask)
{
const struct in6_addr *addr = NULL;
int prefix;
- struct ifac *ifa = NULL;
+ struct ifac *ifac = NULL;
struct netinfo6 ni6;
struct riprt *rrt = NULL;
time_t t_lifetime;
@@ -1995,41 +2060,28 @@ rt_deladdr(ifcp, sifa, smask)
trace(1, "\tdeleting %s/%d from %s\n",
inet6_n2p(addr), prefix, ifcp->ifc_name);
- ifa = ifa_match(ifcp, addr, prefix);
- if (!ifa) {
+ ifac = ifa_match(ifcp, addr, prefix);
+ if (!ifac) {
trace(1, "\tno matching ifa found for %s/%d on %s\n",
inet6_n2p(addr), prefix, ifcp->ifc_name);
return -1;
}
- if (ifa->ifa_conf != ifcp) {
+ if (ifac->ifac_ifc != ifcp) {
trace(1, "\taddress table corrupt: back pointer does not match "
"(%s != %s)\n",
- ifcp->ifc_name, ifa->ifa_conf->ifc_name);
+ ifcp->ifc_name, ifac->ifac_ifc->ifc_name);
return -1;
}
- /* remove ifa from interface */
- if (ifcp->ifc_addr == ifa)
- ifcp->ifc_addr = ifa->ifa_next;
- else {
- struct ifac *p;
- for (p = ifcp->ifc_addr; p; p = p->ifa_next) {
- if (p->ifa_next == ifa) {
- p->ifa_next = ifa->ifa_next;
- break;
- }
- }
- }
- ifa->ifa_next = NULL;
- ifa->ifa_conf = NULL;
+ TAILQ_REMOVE(&ifcp->ifc_ifac_head, ifac, ifac_next);
t_lifetime = time(NULL) - RIP_LIFETIME;
/* age route for interface address */
memset(&ni6, 0, sizeof(ni6));
- ni6.rip6_dest = ifa->ifa_addr;
- ni6.rip6_plen = ifa->ifa_plen;
+ ni6.rip6_dest = ifac->ifac_addr;
+ ni6.rip6_plen = ifac->ifac_plen;
applyplen(&ni6.rip6_dest, ni6.rip6_plen);
trace(1, "\tfind interface route %s/%d on %d\n",
inet6_n2p(&ni6.rip6_dest), ni6.rip6_plen, ifcp->ifc_index);
- if ((rrt = rtsearch(&ni6, NULL)) != NULL) {
+ if ((rrt = rtsearch(&ni6)) != NULL) {
struct in6_addr none;
memset(&none, 0, sizeof(none));
if (rrt->rrt_index == ifcp->ifc_index &&
@@ -2052,15 +2104,16 @@ rt_deladdr(ifcp, sifa, smask)
/* age route for p2p destination */
if (ifcp->ifc_flags & IFF_POINTOPOINT) {
memset(&ni6, 0, sizeof(ni6));
- ni6.rip6_dest = ifa->ifa_raddr;
+ ni6.rip6_dest = ifac->ifac_raddr;
ni6.rip6_plen = 128;
applyplen(&ni6.rip6_dest, ni6.rip6_plen); /*to be sure*/
trace(1, "\tfind p2p route %s/%d on %d\n",
inet6_n2p(&ni6.rip6_dest), ni6.rip6_plen,
ifcp->ifc_index);
- if ((rrt = rtsearch(&ni6, NULL)) != NULL) {
+ if ((rrt = rtsearch(&ni6)) != NULL) {
if (rrt->rrt_index == ifcp->ifc_index &&
- IN6_ARE_ADDR_EQUAL(&rrt->rrt_gw, &ifa->ifa_addr)) {
+ IN6_ARE_ADDR_EQUAL(&rrt->rrt_gw,
+ &ifac->ifac_addr)) {
trace(1, "\troute found, age it\n");
if (rrt->rrt_t == 0 || rrt->rrt_t > t_lifetime) {
rrt->rrt_t = t_lifetime;
@@ -2077,7 +2130,9 @@ rt_deladdr(ifcp, sifa, smask)
} else
trace(1, "\tno p2p route found\n");
}
- return updated ? 0 : -1;
+ free(ifac);
+
+ return ((updated) ? 0 : -1);
}
/*
@@ -2085,12 +2140,10 @@ rt_deladdr(ifcp, sifa, smask)
* list.
*/
int
-ifrt(ifcp, again)
- struct ifc *ifcp;
- int again;
+ifrt(struct ifc *ifcp, int again)
{
- struct ifac *ifa;
- struct riprt *rrt = NULL, *search_rrt, *prev_rrt, *loop_rrt;
+ struct ifac *ifac;
+ struct riprt *rrt = NULL, *search_rrt, *loop_rrt;
struct netinfo6 *np;
time_t t_lifetime;
int need_trigger = 0;
@@ -2105,23 +2158,23 @@ ifrt(ifcp, again)
return 0;
}
- for (ifa = ifcp->ifc_addr; ifa; ifa = ifa->ifa_next) {
- if (IN6_IS_ADDR_LINKLOCAL(&ifa->ifa_addr)) {
+ TAILQ_FOREACH(ifac, &ifcp->ifc_ifac_head, ifac_next) {
+ if (IN6_IS_ADDR_LINKLOCAL(&ifac->ifac_addr)) {
#if 0
trace(1, "route: %s on %s: "
"skip linklocal interface address\n",
- inet6_n2p(&ifa->ifa_addr), ifcp->ifc_name);
+ inet6_n2p(&ifac->ifac_addr), ifcp->ifc_name);
#endif
continue;
}
- if (IN6_IS_ADDR_UNSPECIFIED(&ifa->ifa_addr)) {
+ if (IN6_IS_ADDR_UNSPECIFIED(&ifac->ifac_addr)) {
#if 0
trace(1, "route: %s: skip unspec interface address\n",
ifcp->ifc_name);
#endif
continue;
}
- if (IN6_IS_ADDR_LOOPBACK(&ifa->ifa_addr)) {
+ if (IN6_IS_ADDR_LOOPBACK(&ifac->ifac_addr)) {
#if 0
trace(1, "route: %s: skip loopback address\n",
ifcp->ifc_name);
@@ -2135,17 +2188,17 @@ ifrt(ifcp, again)
rrt->rrt_same = NULL;
rrt->rrt_index = ifcp->ifc_index;
rrt->rrt_t = 0; /* don't age */
- rrt->rrt_info.rip6_dest = ifa->ifa_addr;
+ rrt->rrt_info.rip6_dest = ifac->ifac_addr;
rrt->rrt_info.rip6_tag = htons(routetag & 0xffff);
rrt->rrt_info.rip6_metric = 1 + ifcp->ifc_metric;
- rrt->rrt_info.rip6_plen = ifa->ifa_plen;
+ rrt->rrt_info.rip6_plen = ifac->ifac_plen;
rrt->rrt_flags = RTF_HOST;
rrt->rrt_rflags |= RRTF_CHANGED;
- applyplen(&rrt->rrt_info.rip6_dest, ifa->ifa_plen);
+ applyplen(&rrt->rrt_info.rip6_dest, ifac->ifac_plen);
memset(&rrt->rrt_gw, 0, sizeof(struct in6_addr));
- rrt->rrt_gw = ifa->ifa_addr;
+ rrt->rrt_gw = ifac->ifac_addr;
np = &rrt->rrt_info;
- search_rrt = rtsearch(np, &prev_rrt);
+ search_rrt = rtsearch(np);
if (search_rrt != NULL) {
if (search_rrt->rrt_info.rip6_metric <=
rrt->rrt_info.rip6_metric) {
@@ -2159,25 +2212,21 @@ ifrt(ifcp, again)
goto next;
}
- if (prev_rrt)
- prev_rrt->rrt_next = rrt->rrt_next;
- else
- riprt = rrt->rrt_next;
+ TAILQ_REMOVE(&riprt_head, rrt, rrt_next);
delroute(&rrt->rrt_info, &rrt->rrt_gw);
}
/* Attach the route to the list */
trace(1, "route: %s/%d: register route (%s)\n",
inet6_n2p(&np->rip6_dest), np->rip6_plen,
ifcp->ifc_name);
- rrt->rrt_next = riprt;
- riprt = rrt;
+ TAILQ_INSERT_HEAD(&riprt_head, rrt, rrt_next);
addroute(rrt, &rrt->rrt_gw, ifcp);
rrt = NULL;
sendrequest(ifcp);
ripsend(ifcp, &ifcp->ifc_ripsin, 0);
need_trigger = 1;
} else {
- for (loop_rrt = riprt; loop_rrt; loop_rrt = loop_rrt->rrt_next) {
+ TAILQ_FOREACH(loop_rrt, &riprt_head, rrt_next) {
if (loop_rrt->rrt_index == ifcp->ifc_index) {
t_lifetime = time(NULL) - RIP_LIFETIME;
if (loop_rrt->rrt_t == 0 || loop_rrt->rrt_t > t_lifetime) {
@@ -2202,12 +2251,10 @@ ifrt(ifcp, again)
* since BSD kernels do not look at prefix length on p2p interfaces.
*/
void
-ifrt_p2p(ifcp, again)
- struct ifc *ifcp;
- int again;
+ifrt_p2p(struct ifc *ifcp, int again)
{
- struct ifac *ifa;
- struct riprt *rrt, *orrt, *prevrrt;
+ struct ifac *ifac;
+ struct riprt *rrt, *orrt;
struct netinfo6 *np;
struct in6_addr addr, dest;
int advert, ignore, i;
@@ -2219,11 +2266,11 @@ ifrt_p2p(ifcp, again)
const char *category = "";
const char *noadv;
- for (ifa = ifcp->ifc_addr; ifa; ifa = ifa->ifa_next) {
- addr = ifa->ifa_addr;
- dest = ifa->ifa_raddr;
- applyplen(&addr, ifa->ifa_plen);
- applyplen(&dest, ifa->ifa_plen);
+ TAILQ_FOREACH(ifac, &ifcp->ifc_ifac_head, ifac_next) {
+ addr = ifac->ifac_addr;
+ dest = ifac->ifac_raddr;
+ applyplen(&addr, ifac->ifac_plen);
+ applyplen(&dest, ifac->ifac_plen);
advert = ignore = 0;
switch (behavior) {
case CISCO:
@@ -2268,7 +2315,7 @@ ifrt_p2p(ifcp, again)
* A/64 -> A/64
* A B/128 -> A/128 and B/128
*/
- if (!IN6_IS_ADDR_UNSPECIFIED(&ifa->ifa_raddr)) {
+ if (!IN6_IS_ADDR_UNSPECIFIED(&ifac->ifac_raddr)) {
if (IN6_ARE_ADDR_EQUAL(&addr, &dest))
advert |= P2PADVERT_NETWORK;
else {
@@ -2294,22 +2341,22 @@ ifrt_p2p(ifcp, again)
rrt->rrt_t = 0; /* don't age */
switch (i) {
case P2PADVERT_NETWORK:
- rrt->rrt_info.rip6_dest = ifa->ifa_addr;
- rrt->rrt_info.rip6_plen = ifa->ifa_plen;
+ rrt->rrt_info.rip6_dest = ifac->ifac_addr;
+ rrt->rrt_info.rip6_plen = ifac->ifac_plen;
applyplen(&rrt->rrt_info.rip6_dest,
- ifa->ifa_plen);
+ ifac->ifac_plen);
category = "network";
break;
case P2PADVERT_ADDR:
- rrt->rrt_info.rip6_dest = ifa->ifa_addr;
+ rrt->rrt_info.rip6_dest = ifac->ifac_addr;
rrt->rrt_info.rip6_plen = 128;
rrt->rrt_gw = in6addr_loopback;
category = "addr";
break;
case P2PADVERT_DEST:
- rrt->rrt_info.rip6_dest = ifa->ifa_raddr;
+ rrt->rrt_info.rip6_dest = ifac->ifac_raddr;
rrt->rrt_info.rip6_plen = 128;
- rrt->rrt_gw = ifa->ifa_addr;
+ rrt->rrt_gw = ifac->ifac_addr;
category = "dest";
break;
}
@@ -2330,23 +2377,19 @@ ifrt_p2p(ifcp, again)
rrt->rrt_info.rip6_tag = htons(routetag & 0xffff);
rrt->rrt_info.rip6_metric = 1 + ifcp->ifc_metric;
np = &rrt->rrt_info;
- orrt = rtsearch(np, &prevrrt);
+ orrt = rtsearch(np);
if (!orrt) {
/* Attach the route to the list */
trace(1, "route: %s/%d: register route "
"(%s on %s%s)\n",
inet6_n2p(&np->rip6_dest), np->rip6_plen,
category, ifcp->ifc_name, noadv);
- rrt->rrt_next = riprt;
- riprt = rrt;
+ TAILQ_INSERT_HEAD(&riprt_head, rrt, rrt_next);
} else if (rrt->rrt_index != orrt->rrt_index ||
rrt->rrt_info.rip6_metric != orrt->rrt_info.rip6_metric) {
- /* swap route */
- rrt->rrt_next = orrt->rrt_next;
- if (prevrrt)
- prevrrt->rrt_next = rrt;
- else
- riprt = rrt;
+ /* replace route */
+ TAILQ_INSERT_BEFORE(orrt, rrt, rrt_next);
+ TAILQ_REMOVE(&riprt_head, orrt, rrt_next);
free(orrt);
trace(1, "route: %s/%d: update (%s on %s%s)\n",
@@ -2372,8 +2415,7 @@ ifrt_p2p(ifcp, again)
}
int
-getifmtu(ifindex)
- int ifindex;
+getifmtu(int ifindex)
{
int mib[6];
char *buf;
@@ -2410,8 +2452,7 @@ getifmtu(ifindex)
}
const char *
-rttypes(rtm)
- struct rt_msghdr *rtm;
+rttypes(struct rt_msghdr *rtm)
{
#define RTTYPE(s, f) \
do { \
@@ -2454,8 +2495,7 @@ do { \
}
const char *
-rtflags(rtm)
- struct rt_msghdr *rtm;
+rtflags(struct rt_msghdr *rtm)
{
static char buf[BUFSIZ];
@@ -2524,8 +2564,7 @@ do { \
}
const char *
-ifflags(flags)
- int flags;
+ifflags(int flags)
{
static char buf[BUFSIZ];
@@ -2564,8 +2603,7 @@ do { \
}
void
-krtread(again)
- int again;
+krtread(int again)
{
int mib[6];
size_t msize;
@@ -2583,6 +2621,8 @@ krtread(again)
mib[4] = NET_RT_DUMP; /* Dump the kernel routing table */
mib[5] = 0; /* No flags */
do {
+ if (retry)
+ sleep(1);
retry++;
errmsg = NULL;
if (buf)
@@ -2599,7 +2639,7 @@ krtread(again)
errmsg = "sysctl NET_RT_DUMP";
continue;
}
- } while (retry < 5 && errmsg != NULL);
+ } while (retry < RT_DUMP_MAXRETRY && errmsg != NULL);
if (errmsg) {
fatal("%s (with %d retries, msize=%lu)", errmsg, retry,
(u_long)msize);
@@ -2616,16 +2656,14 @@ krtread(again)
}
void
-rt_entry(rtm, again)
- struct rt_msghdr *rtm;
- int again;
+rt_entry(struct rt_msghdr *rtm, int again)
{
struct sockaddr_in6 *sin6_dst, *sin6_gw, *sin6_mask;
struct sockaddr_in6 *sin6_genmask, *sin6_ifp;
char *rtmp, *ifname = NULL;
struct riprt *rrt, *orrt;
struct netinfo6 *np;
- int s;
+ int ifindex;
sin6_dst = sin6_gw = sin6_mask = sin6_genmask = sin6_ifp = 0;
if ((rtm->rtm_flags & RTF_UP) == 0 || rtm->rtm_flags &
@@ -2641,6 +2679,9 @@ rt_entry(rtm, again)
if (rtm->rtm_flags & RTF_CLONED)
return;
#endif
+ /* XXX: Ignore connected routes. */
+ if (!(rtm->rtm_flags & (RTF_GATEWAY|RTF_HOST|RTF_STATIC)))
+ return;
/*
* do not look at dynamic routes.
* netbsd/openbsd cloned routes have UGHD.
@@ -2690,6 +2731,8 @@ rt_entry(rtm, again)
rrt->rrt_t = time(NULL);
if (aflag == 0 && (rtm->rtm_flags & RTF_STATIC))
rrt->rrt_t = 0; /* Don't age static routes */
+ if (rtm->rtm_flags & Pflag)
+ rrt->rrt_t = 0; /* Don't age PROTO[123] routes */
if ((rtm->rtm_flags & (RTF_HOST|RTF_GATEWAY)) == RTF_HOST)
rrt->rrt_t = 0; /* Don't age non-gateway host routes */
np->rip6_tag = 0;
@@ -2707,7 +2750,7 @@ rt_entry(rtm, again)
else
np->rip6_plen = 0;
- orrt = rtsearch(np, NULL);
+ orrt = rtsearch(np);
if (orrt && orrt->rrt_info.rip6_metric != HOPCNT_INFINITY6) {
/* Already found */
if (!again) {
@@ -2735,16 +2778,16 @@ rt_entry(rtm, again)
trace(1, " gw %s", inet6_n2p(&rrt->rrt_gw));
/* Interface */
- s = rtm->rtm_index;
- if (s < nindex2ifc && index2ifc[s])
- ifname = index2ifc[s]->ifc_name;
+ ifindex = rtm->rtm_index;
+ if ((unsigned int)ifindex < nindex2ifc && index2ifc[ifindex])
+ ifname = index2ifc[ifindex]->ifc_name;
else {
trace(1, " not configured\n");
free(rrt);
return;
}
- trace(1, " if %s sock %d", ifname, s);
- rrt->rrt_index = s;
+ trace(1, " if %s sock %d", ifname, ifindex);
+ rrt->rrt_index = ifindex;
trace(1, "\n");
@@ -2762,23 +2805,21 @@ rt_entry(rtm, again)
/* Put it to the route list */
if (orrt && orrt->rrt_info.rip6_metric == HOPCNT_INFINITY6) {
/* replace route list */
- rrt->rrt_next = orrt->rrt_next;
- *orrt = *rrt;
+ TAILQ_INSERT_BEFORE(orrt, rrt, rrt_next);
+ TAILQ_REMOVE(&riprt_head, orrt, rrt_next);
+
trace(1, "route: %s/%d flags %s: replace new route\n",
inet6_n2p(&np->rip6_dest), np->rip6_plen,
rtflags(rtm));
- free(rrt);
- } else {
- rrt->rrt_next = riprt;
- riprt = rrt;
- }
+ free(orrt);
+ } else
+ TAILQ_INSERT_HEAD(&riprt_head, rrt, rrt_next);
}
int
-addroute(rrt, gw, ifcp)
- struct riprt *rrt;
- const struct in6_addr *gw;
- struct ifc *ifcp;
+addroute(struct riprt *rrt,
+ const struct in6_addr *gw,
+ struct ifc *ifcp)
{
struct netinfo6 *np;
u_char buf[BUFSIZ], buf1[BUFSIZ], buf2[BUFSIZ];
@@ -2806,6 +2847,7 @@ addroute(rrt, gw, ifcp)
rtm->rtm_seq = ++seq;
rtm->rtm_pid = pid;
rtm->rtm_flags = rrt->rrt_flags;
+ rtm->rtm_flags |= Qflag;
rtm->rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK;
rtm->rtm_rmx.rmx_hopcount = np->rip6_metric - 1;
rtm->rtm_inits = RTV_HOPCOUNT;
@@ -2848,9 +2890,7 @@ addroute(rrt, gw, ifcp)
}
int
-delroute(np, gw)
- struct netinfo6 *np;
- struct in6_addr *gw;
+delroute(struct netinfo6 *np, struct in6_addr *gw)
{
u_char buf[BUFSIZ], buf2[BUFSIZ];
struct rt_msghdr *rtm;
@@ -2873,6 +2913,7 @@ delroute(np, gw)
rtm->rtm_seq = ++seq;
rtm->rtm_pid = pid;
rtm->rtm_flags = RTF_UP | RTF_GATEWAY;
+ rtm->rtm_flags |= Qflag;
if (np->rip6_plen == sizeof(struct in6_addr) * 8)
rtm->rtm_flags |= RTF_HOST;
rtm->rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK;
@@ -2915,9 +2956,7 @@ delroute(np, gw)
}
struct in6_addr *
-getroute(np, gw)
- struct netinfo6 *np;
- struct in6_addr *gw;
+getroute(struct netinfo6 *np, struct in6_addr *gw)
{
u_char buf[BUFSIZ];
int myseq;
@@ -2964,8 +3003,7 @@ getroute(np, gw)
}
const char *
-inet6_n2p(p)
- const struct in6_addr *p;
+inet6_n2p(const struct in6_addr *p)
{
static char buf[BUFSIZ];
@@ -2973,8 +3011,7 @@ inet6_n2p(p)
}
void
-ifrtdump(sig)
- int sig;
+ifrtdump(int sig)
{
ifdump(sig);
@@ -2982,12 +3019,11 @@ ifrtdump(sig)
}
void
-ifdump(sig)
- int sig;
+ifdump(int sig)
{
struct ifc *ifcp;
FILE *dump;
- int i;
+ int nifc = 0;
if (sig == 0)
dump = stderr;
@@ -2996,21 +3032,25 @@ ifdump(sig)
dump = stderr;
fprintf(dump, "%s: Interface Table Dump\n", hms());
+ TAILQ_FOREACH(ifcp, &ifc_head, ifc_next)
+ nifc++;
fprintf(dump, " Number of interfaces: %d\n", nifc);
- for (i = 0; i < 2; i++) {
- fprintf(dump, " %sadvertising interfaces:\n", i ? "non-" : "");
- for (ifcp = ifc; ifcp; ifcp = ifcp->ifc_next) {
- if (i == 0) {
- if ((ifcp->ifc_flags & IFF_UP) == 0)
- continue;
- if (iff_find(ifcp, 'N') != NULL)
- continue;
- } else {
- if (ifcp->ifc_flags & IFF_UP)
- continue;
- }
- ifdump0(dump, ifcp);
- }
+
+ fprintf(dump, " advertising interfaces:\n");
+ TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
+ if ((ifcp->ifc_flags & IFF_UP) == 0)
+ continue;
+ if (iff_find(ifcp, IFIL_TYPE_N) != NULL)
+ continue;
+ ifdump0(dump, ifcp);
+ }
+ fprintf(dump, "\n");
+ fprintf(dump, " non-advertising interfaces:\n");
+ TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
+ if ((ifcp->ifc_flags & IFF_UP) &&
+ (iff_find(ifcp, IFIL_TYPE_N) == NULL))
+ continue;
+ ifdump0(dump, ifcp);
}
fprintf(dump, "\n");
if (dump != stderr)
@@ -3018,11 +3058,9 @@ ifdump(sig)
}
void
-ifdump0(dump, ifcp)
- FILE *dump;
- const struct ifc *ifcp;
+ifdump0(FILE *dump, const struct ifc *ifcp)
{
- struct ifac *ifa;
+ struct ifac *ifac;
struct iff *iffp;
char buf[BUFSIZ];
const char *ft;
@@ -3032,53 +3070,51 @@ ifdump0(dump, ifcp)
ifcp->ifc_name, ifcp->ifc_index, ifflags(ifcp->ifc_flags),
inet6_n2p(&ifcp->ifc_mylladdr),
ifcp->ifc_mtu, ifcp->ifc_metric);
- for (ifa = ifcp->ifc_addr; ifa; ifa = ifa->ifa_next) {
+ TAILQ_FOREACH(ifac, &ifcp->ifc_ifac_head, ifac_next) {
if (ifcp->ifc_flags & IFF_POINTOPOINT) {
- inet_ntop(AF_INET6, (void *)&ifa->ifa_raddr,
+ inet_ntop(AF_INET6, (void *)&ifac->ifac_raddr,
buf, sizeof(buf));
fprintf(dump, "\t%s/%d -- %s\n",
- inet6_n2p(&ifa->ifa_addr),
- ifa->ifa_plen, buf);
+ inet6_n2p(&ifac->ifac_addr),
+ ifac->ifac_plen, buf);
} else {
fprintf(dump, "\t%s/%d\n",
- inet6_n2p(&ifa->ifa_addr),
- ifa->ifa_plen);
- }
- }
- if (ifcp->ifc_filter) {
- fprintf(dump, "\tFilter:");
- for (iffp = ifcp->ifc_filter; iffp; iffp = iffp->iff_next) {
- addr = 0;
- switch (iffp->iff_type) {
- case 'A':
- ft = "Aggregate"; addr++; break;
- case 'N':
- ft = "No-use"; break;
- case 'O':
- ft = "Advertise-only"; addr++; break;
- case 'T':
- ft = "Default-only"; break;
- case 'L':
- ft = "Listen-only"; addr++; break;
- default:
- snprintf(buf, sizeof(buf), "Unknown-%c", iffp->iff_type);
- ft = buf;
- addr++;
- break;
- }
- fprintf(dump, " %s", ft);
- if (addr) {
- fprintf(dump, "(%s/%d)", inet6_n2p(&iffp->iff_addr),
- iffp->iff_plen);
- }
+ inet6_n2p(&ifac->ifac_addr),
+ ifac->ifac_plen);
+ }
+ }
+
+ fprintf(dump, "\tFilter:\n");
+ TAILQ_FOREACH(iffp, &ifcp->ifc_iff_head, iff_next) {
+ addr = 0;
+ switch (iffp->iff_type) {
+ case IFIL_TYPE_A:
+ ft = "Aggregate"; addr++; break;
+ case IFIL_TYPE_N:
+ ft = "No-use"; break;
+ case IFIL_TYPE_O:
+ ft = "Advertise-only"; addr++; break;
+ case IFIL_TYPE_T:
+ ft = "Default-only"; break;
+ case IFIL_TYPE_L:
+ ft = "Listen-only"; addr++; break;
+ default:
+ snprintf(buf, sizeof(buf), "Unknown-%c", iffp->iff_type);
+ ft = buf;
+ addr++;
+ break;
}
+ fprintf(dump, "\t\t%s", ft);
+ if (addr)
+ fprintf(dump, "(%s/%d)", inet6_n2p(&iffp->iff_addr),
+ iffp->iff_plen);
fprintf(dump, "\n");
}
+ fprintf(dump, "\n");
}
void
-rtdump(sig)
- int sig;
+rtdump(int sig)
{
struct riprt *rrt;
char buf[BUFSIZ];
@@ -3093,7 +3129,7 @@ rtdump(sig)
t = time(NULL);
fprintf(dump, "\n%s: Routing Table Dump\n", hms());
- for (rrt = riprt; rrt; rrt = rrt->rrt_next) {
+ TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
if (rrt->rrt_t == 0)
age = 0;
else
@@ -3127,11 +3163,11 @@ rtdump(sig)
* -O 5f09:c400::/32,ef0,ef1 (only when match)
*/
void
-filterconfig()
+filterconfig(void)
{
int i;
char *p, *ap, *iflp, *ifname, *ep;
- struct iff ftmp, *iff_obj;
+ struct iff iff, *iffp;
struct ifc *ifcp;
struct riprt *rrt;
#if 0
@@ -3142,7 +3178,8 @@ filterconfig()
for (i = 0; i < nfilter; i++) {
ap = filter[i];
iflp = NULL;
- ifcp = NULL;
+ iffp = &iff;
+ memset(iffp, 0, sizeof(*iffp));
if (filtertype[i] == 'N' || filtertype[i] == 'T') {
iflp = ap;
goto ifonly;
@@ -3156,22 +3193,21 @@ filterconfig()
/*NOTREACHED*/
}
*p++ = '\0';
- if (inet_pton(AF_INET6, ap, &ftmp.iff_addr) != 1) {
+ if (inet_pton(AF_INET6, ap, &iffp->iff_addr) != 1) {
fatal("invalid prefix specified for '%s'", ap);
/*NOTREACHED*/
}
errno = 0;
ep = NULL;
plen = strtoul(p, &ep, 10);
- if (errno || !*p || *ep || plen > sizeof(ftmp.iff_addr) * 8) {
+ if (errno || !*p || *ep || plen > sizeof(iffp->iff_addr) * 8) {
fatal("invalid prefix length specified for '%s'", ap);
/*NOTREACHED*/
}
- ftmp.iff_plen = plen;
- ftmp.iff_next = NULL;
- applyplen(&ftmp.iff_addr, ftmp.iff_plen);
+ iffp->iff_plen = plen;
+ applyplen(&iffp->iff_addr, iffp->iff_plen);
ifonly:
- ftmp.iff_type = filtertype[i];
+ iffp->iff_type = filtertype[i];
if (iflp == NULL || *iflp == '\0') {
fatal("no interface specified for '%s'", ap);
/*NOTREACHED*/
@@ -3181,27 +3217,28 @@ ifonly:
ifname = iflp;
if ((iflp = strchr(iflp, ',')) != NULL)
*iflp++ = '\0';
- ifcp = ifc_find(ifname);
- if (ifcp == NULL) {
- fatal("no interface %s exists", ifname);
- /*NOTREACHED*/
- }
- iff_obj = (struct iff *)malloc(sizeof(struct iff));
- if (iff_obj == NULL) {
- fatal("malloc of iff_obj");
- /*NOTREACHED*/
+
+ TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
+ if (fnmatch(ifname, ifcp->ifc_name, 0) != 0)
+ continue;
+
+ iffp = malloc(sizeof(*iffp));
+ if (iffp == NULL) {
+ fatal("malloc of iff");
+ /*NOTREACHED*/
+ }
+ memcpy(iffp, &iff, sizeof(*iffp));
+#if 0
+ syslog(LOG_INFO, "Add filter: type %d, ifname %s.", iffp->iff_type, ifname);
+#endif
+ TAILQ_INSERT_HEAD(&ifcp->ifc_iff_head, iffp, iff_next);
}
- memcpy((void *)iff_obj, (void *)&ftmp,
- sizeof(struct iff));
- /* link it to the interface filter */
- iff_obj->iff_next = ifcp->ifc_filter;
- ifcp->ifc_filter = iff_obj;
}
/*
* -A: aggregate configuration.
*/
- if (filtertype[i] != 'A')
+ if (filtertype[i] != IFIL_TYPE_A)
continue;
/* put the aggregate to the kernel routing table */
rrt = (struct riprt *)malloc(sizeof(struct riprt));
@@ -3210,8 +3247,8 @@ ifonly:
/*NOTREACHED*/
}
memset(rrt, 0, sizeof(struct riprt));
- rrt->rrt_info.rip6_dest = ftmp.iff_addr;
- rrt->rrt_info.rip6_plen = ftmp.iff_plen;
+ rrt->rrt_info.rip6_dest = iff.iff_addr;
+ rrt->rrt_info.rip6_plen = iff.iff_plen;
rrt->rrt_info.rip6_metric = 1;
rrt->rrt_info.rip6_tag = htons(routetag & 0xffff);
rrt->rrt_gw = in6addr_loopback;
@@ -3239,11 +3276,10 @@ ifonly:
}
#endif
/* Put the route to the list */
- rrt->rrt_next = riprt;
- riprt = rrt;
+ TAILQ_INSERT_HEAD(&riprt_head, rrt, rrt_next);
trace(1, "Aggregate: %s/%d for %s\n",
- inet6_n2p(&ftmp.iff_addr), ftmp.iff_plen,
- ifcp->ifc_name);
+ inet6_n2p(&iff.iff_addr), iff.iff_plen,
+ loopifcp->ifc_name);
/* Add this route to the kernel */
if (nflag) /* do not modify kernel routing table */
continue;
@@ -3258,19 +3294,19 @@ ifonly:
* with the address and prefix length specified in the arguments.
*/
struct ifac *
-ifa_match(ifcp, ia, plen)
- const struct ifc *ifcp;
- const struct in6_addr *ia;
- int plen;
+ifa_match(const struct ifc *ifcp,
+ const struct in6_addr *ia,
+ int plen)
{
- struct ifac *ifa;
+ struct ifac *ifac;
- for (ifa = ifcp->ifc_addr; ifa; ifa = ifa->ifa_next) {
- if (IN6_ARE_ADDR_EQUAL(&ifa->ifa_addr, ia) &&
- ifa->ifa_plen == plen)
+ TAILQ_FOREACH(ifac, &ifcp->ifc_ifac_head, ifac_next) {
+ if (IN6_ARE_ADDR_EQUAL(&ifac->ifac_addr, ia) &&
+ ifac->ifac_plen == plen)
break;
}
- return ifa;
+
+ return (ifac);
}
/*
@@ -3279,30 +3315,22 @@ ifa_match(ifcp, ia, plen)
* Note: This is not a rtalloc(). Therefore exact match is necessary.
*/
struct riprt *
-rtsearch(np, prev_rrt)
- struct netinfo6 *np;
- struct riprt **prev_rrt;
+rtsearch(struct netinfo6 *np)
{
struct riprt *rrt;
- if (prev_rrt)
- *prev_rrt = NULL;
- for (rrt = riprt; rrt; rrt = rrt->rrt_next) {
+ TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
if (rrt->rrt_info.rip6_plen == np->rip6_plen &&
IN6_ARE_ADDR_EQUAL(&rrt->rrt_info.rip6_dest,
&np->rip6_dest))
- return rrt;
- if (prev_rrt)
- *prev_rrt = rrt;
+ break;
}
- if (prev_rrt)
- *prev_rrt = NULL;
- return 0;
+
+ return (rrt);
}
int
-sin6mask2len(sin6)
- const struct sockaddr_in6 *sin6;
+sin6mask2len(const struct sockaddr_in6 *sin6)
{
return mask2len(&sin6->sin6_addr,
@@ -3310,9 +3338,7 @@ sin6mask2len(sin6)
}
int
-mask2len(addr, lenlim)
- const struct in6_addr *addr;
- int lenlim;
+mask2len(const struct in6_addr *addr, int lenlim)
{
int i = 0, j;
const u_char *p = (const u_char *)addr;
@@ -3339,8 +3365,7 @@ mask2len(addr, lenlim)
}
void
-applymask(addr, mask)
- struct in6_addr *addr, *mask;
+applymask(struct in6_addr *addr, struct in6_addr *mask)
{
int i;
u_long *p, *q;
@@ -3355,9 +3380,7 @@ static const u_char plent[8] = {
};
void
-applyplen(ia, plen)
- struct in6_addr *ia;
- int plen;
+applyplen(struct in6_addr *ia, int plen)
{
u_char *p;
int i;
@@ -3377,8 +3400,7 @@ static const int pl2m[9] = {
};
struct in6_addr *
-plen2mask(n)
- int n;
+plen2mask(int n)
{
static struct in6_addr ia;
u_char *p;
@@ -3398,8 +3420,7 @@ plen2mask(n)
}
char *
-allocopy(p)
- char *p;
+allocopy(char *p)
{
int len = strlen(p) + 1;
char *q = (char *)malloc(len);
@@ -3414,7 +3435,7 @@ allocopy(p)
}
char *
-hms()
+hms(void)
{
static char buf[BUFSIZ];
time_t t;
@@ -3433,8 +3454,7 @@ hms()
#define RIPRANDDEV 1.0 /* 30 +- 15, max - min = 30 */
int
-ripinterval(timer)
- int timer;
+ripinterval(int timer)
{
double r = rand();
@@ -3444,7 +3464,7 @@ ripinterval(timer)
}
time_t
-ripsuptrig()
+ripsuptrig(void)
{
time_t t;
@@ -3554,7 +3574,7 @@ trace(level, fmt, va_alist)
}
unsigned int
-if_maxindex()
+if_maxindex(void)
{
struct if_nameindex *p, *p0;
unsigned int max = 0;
@@ -3569,36 +3589,33 @@ if_maxindex()
}
struct ifc *
-ifc_find(name)
- char *name;
+ifc_find(char *name)
{
struct ifc *ifcp;
- for (ifcp = ifc; ifcp; ifcp = ifcp->ifc_next) {
+ TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
if (strcmp(name, ifcp->ifc_name) == 0)
- return ifcp;
+ break;
}
- return (struct ifc *)NULL;
+ return (ifcp);
}
struct iff *
-iff_find(ifcp, type)
- struct ifc *ifcp;
- int type;
+iff_find(struct ifc *ifcp, int type)
{
struct iff *iffp;
- for (iffp = ifcp->ifc_filter; iffp; iffp = iffp->iff_next) {
- if (iffp->iff_type == type)
- return iffp;
+ TAILQ_FOREACH(iffp, &ifcp->ifc_iff_head, iff_next) {
+ if (type == IFIL_TYPE_ANY ||
+ type == iffp->iff_type)
+ break;
}
- return NULL;
+
+ return (iffp);
}
void
-setindex2ifc(idx, ifcp)
- int idx;
- struct ifc *ifcp;
+setindex2ifc(int idx, struct ifc *ifcp)
{
int n, nsize;
struct ifc **p;
diff --git a/usr.sbin/rpcbind/rpcbind.c b/usr.sbin/rpcbind/rpcbind.c
index 3d11af1..fb6c99d 100644
--- a/usr.sbin/rpcbind/rpcbind.c
+++ b/usr.sbin/rpcbind/rpcbind.c
@@ -289,7 +289,7 @@ init_transport(struct netconfig *nconf)
*/
if ((fd = __rpc_nconf2fd(nconf)) < 0) {
int non_fatal = 0;
- if (errno == EPROTONOSUPPORT)
+ if (errno == EAFNOSUPPORT)
non_fatal = 1;
syslog(non_fatal?LOG_DEBUG:LOG_ERR, "cannot create socket for %s",
nconf->nc_netid);
@@ -352,7 +352,7 @@ init_transport(struct netconfig *nconf)
*/
if ((fd = __rpc_nconf2fd(nconf)) < 0) {
int non_fatal = 0;
- if (errno == EPROTONOSUPPORT &&
+ if (errno == EAFNOSUPPORT &&
nconf->nc_semantics != NC_TPI_CLTS)
non_fatal = 1;
syslog(non_fatal ? LOG_DEBUG : LOG_ERR,
diff --git a/usr.sbin/rpcbind/util.c b/usr.sbin/rpcbind/util.c
index 16e6f70..8ddb13e 100644
--- a/usr.sbin/rpcbind/util.c
+++ b/usr.sbin/rpcbind/util.c
@@ -57,9 +57,6 @@ static struct sockaddr_in6 *local_in6;
#endif
static int bitmaskcmp(void *, void *, void *, int);
-#ifdef INET6
-static void in6_fillscopeid(struct sockaddr_in6 *);
-#endif
/*
* For all bits set in "mask", compare the corresponding bits in
@@ -79,26 +76,6 @@ bitmaskcmp(void *dst, void *src, void *mask, int bytelen)
}
/*
- * Similar to code in ifconfig.c. Fill in the scope ID for link-local
- * addresses returned by getifaddrs().
- */
-#ifdef INET6
-static void
-in6_fillscopeid(struct sockaddr_in6 *sin6)
-{
- u_int16_t ifindex;
-
- if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
- ifindex = ntohs(*(u_int16_t *)&sin6->sin6_addr.s6_addr[2]);
- if (sin6->sin6_scope_id == 0 && ifindex != 0) {
- sin6->sin6_scope_id = ifindex;
- *(u_int16_t *)&sin6->sin6_addr.s6_addr[2] = 0;
- }
- }
-}
-#endif
-
-/*
* Find a server address that can be used by `caller' to contact
* the local service specified by `serv_uaddr'. If `clnt_uaddr' is
* non-NULL, it is used instead of `caller' as a hint suggesting
@@ -202,7 +179,6 @@ addrmerge(struct netbuf *caller, char *serv_uaddr, char *clnt_uaddr,
* a link-local address then use the scope id to see
* which one.
*/
- in6_fillscopeid(SA2SIN6(ifsa));
if (IN6_IS_ADDR_LINKLOCAL(&SA2SIN6ADDR(ifsa)) &&
IN6_IS_ADDR_LINKLOCAL(&SA2SIN6ADDR(caller_sa)) &&
IN6_IS_ADDR_LINKLOCAL(&SA2SIN6ADDR(hint_sa))) {
diff --git a/usr.sbin/sade/Makefile b/usr.sbin/sade/Makefile
deleted file mode 100644
index 1a7c8b2..0000000
--- a/usr.sbin/sade/Makefile
+++ /dev/null
@@ -1,27 +0,0 @@
-# $FreeBSD$
-
-.if ${MACHINE_CPUARCH} != "ia64"
-_wizard= wizard.c
-.endif
-
-PROG= sade
-MAN= sade.8
-SRCS= command.c devices.c \
- disks.c dmenu.c \
- globals.c install.c \
- label.c main.c menus.c misc.c \
- msg.c system.c termcap.c \
- variable.c ${_wizard}
-WARNS?= 3
-
-# command.c
-
-.if ${MACHINE} == "pc98"
-CFLAGS+= -DPC98
-.endif
-CFLAGS+= -I${.CURDIR}/../../contrib/dialog -I.
-
-DPADD= ${LIBDIALOG} ${LIBNCURSESW} ${LIBM} ${LIBUTIL} ${LIBDISK}
-LDADD= -ldialog -lncursesw -lm -lutil -ldisk
-
-.include <bsd.prog.mk>
diff --git a/usr.sbin/sade/Makefile.depend b/usr.sbin/sade/Makefile.depend
deleted file mode 100644
index 9f05925..0000000
--- a/usr.sbin/sade/Makefile.depend
+++ /dev/null
@@ -1,24 +0,0 @@
-# Autogenerated - do NOT edit!
-
-DEP_RELDIR := ${_PARSEDIR:S,${SRCTOP}/,,}
-
-DEP_MACHINE := ${.PARSEFILE:E}
-
-DIRDEPS = \
- gnu/lib/libdialog \
- gnu/lib/libgcc \
- include \
- include/xlocale \
- lib/${CSU_DIR} \
- lib/libc \
- lib/libdisk \
- lib/libutil \
- lib/msun \
- lib/ncurses/ncursesw \
-
-
-.include <dirdeps.mk>
-
-.if ${DEP_RELDIR} == ${_DEP_RELDIR}
-# local dependencies - needed for -jN in clean tree
-.endif
diff --git a/usr.sbin/sade/command.c b/usr.sbin/sade/command.c
deleted file mode 100644
index b3968df..0000000
--- a/usr.sbin/sade/command.c
+++ /dev/null
@@ -1,179 +0,0 @@
-/*
- * $FreeBSD$
- *
- * Copyright (c) 1995
- * Jordan Hubbard. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer,
- * verbatim and that no modifications are made prior to this
- * point in the file.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY JORDAN HUBBARD ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL JORDAN HUBBARD OR HIS PETS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, LIFE OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- *
- */
-
-#include "sade.h"
-
-#define MAX_NUM_COMMANDS 10
-
-typedef struct {
- char key[FILENAME_MAX];
- struct {
- enum { CMD_SHELL, CMD_FUNCTION } type;
- void *ptr, *data;
- } cmds[MAX_NUM_COMMANDS];
- int ncmds;
-} Command;
-
-#define MAX_CMDS 200
-static Command *commandStack[MAX_CMDS];
-int numCommands;
-
-/* Nuke the command stack */
-void
-command_clear(void)
-{
- int i, j;
-
- for (i = 0; i < numCommands; i++)
- for (j = 0; j < commandStack[i]->ncmds; j++)
- if (commandStack[i]->cmds[j].type == CMD_SHELL)
- free(commandStack[i]->cmds[j].ptr);
- free(commandStack[i]);
- numCommands = 0;
-}
-
-static void
-addit(char *key, int type, void *cmd, void *data)
-{
- int i;
-
- /* First, look for the key already present and add a command to it if found */
- for (i = 0; i < numCommands; i++) {
- if (!strcmp(commandStack[i]->key, key)) {
- if (commandStack[i]->ncmds == MAX_NUM_COMMANDS)
- msgFatal("More than %d commands stacked up behind %s??", MAX_NUM_COMMANDS, key);
- commandStack[i]->cmds[commandStack[i]->ncmds].type = type;
- commandStack[i]->cmds[commandStack[i]->ncmds].ptr = cmd;
- commandStack[i]->cmds[commandStack[i]->ncmds].data = data;
- ++(commandStack[i]->ncmds);
- return;
- }
- }
- if (numCommands == MAX_CMDS)
- msgFatal("More than %d commands accumulated??", MAX_CMDS);
-
- /* If we fell to here, it's a new key */
- commandStack[numCommands] = safe_malloc(sizeof(Command));
- strcpy(commandStack[numCommands]->key, key);
- commandStack[numCommands]->ncmds = 1;
- commandStack[numCommands]->cmds[0].type = type;
- commandStack[numCommands]->cmds[0].ptr = cmd;
- commandStack[numCommands]->cmds[0].data = data;
- ++numCommands;
-}
-
-/* Add a shell command under a given key */
-void
-command_shell_add(char *key, const char *fmt, ...)
-{
- va_list args;
- char *cmd;
-
- cmd = (char *)safe_malloc(256);
- va_start(args, fmt);
- vsnprintf(cmd, 256, fmt, args);
- va_end(args);
-
- addit(key, CMD_SHELL, cmd, NULL);
-}
-
-/* Add a shell command under a given key */
-void
-command_func_add(char *key, commandFunc func, void *data)
-{
- addit(key, CMD_FUNCTION, func, data);
-}
-
-static int
-sort_compare(Command *p1, Command *p2)
-{
- if (!p1 && !p2)
- return 0;
- else if (!p1 && p2) /* NULL has a "greater" value for commands */
- return 1;
- else if (p1 && !p2)
- return -1;
- else
- return strcmp(p1->key, p2->key);
-}
-
-void
-command_sort(void)
-{
- int i, j;
-
- commandStack[numCommands] = NULL;
- /* Just do a crude bubble sort since the list is small */
- for (i = 0; i < numCommands; i++) {
- for (j = 0; j < numCommands; j++) {
- if (sort_compare(commandStack[j], commandStack[j + 1]) > 0) {
- Command *tmp = commandStack[j];
-
- commandStack[j] = commandStack[j + 1];
- commandStack[j + 1] = tmp;
- }
- }
- }
-}
-
-/* Run all accumulated commands in sorted order */
-void
-command_execute(void)
-{
- int i, j, ret;
- commandFunc func;
-
- for (i = 0; i < numCommands; i++) {
- for (j = 0; j < commandStack[i]->ncmds; j++) {
- /* If it's a shell command, run system on it */
- if (commandStack[i]->cmds[j].type == CMD_SHELL) {
- msgNotify("Doing %s", (char *)commandStack[i]->cmds[j].ptr);
- ret = vsystem("%s", (char *)commandStack[i]->cmds[j].ptr);
- if (isDebug())
- msgDebug("Command `%s' returns status %d\n",
- (char *)commandStack[i]->cmds[j].ptr, ret);
- }
- else {
- /* It's a function pointer - call it with the key and
- the data */
- func = (commandFunc)commandStack[i]->cmds[j].ptr;
- if (isDebug())
- msgDebug("%p: Execute(%s, %s)\n",
- func, commandStack[i]->key,
- (char *)commandStack[i]->cmds[j].data);
- ret = (*func)(commandStack[i]->key, commandStack[i]->cmds[j].data);
- if (isDebug())
- msgDebug("Function @ %p returns status %d\n",
- commandStack[i]->cmds[j].ptr, ret);
- }
- }
- }
-}
diff --git a/usr.sbin/sade/devices.c b/usr.sbin/sade/devices.c
deleted file mode 100644
index 672ee20..0000000
--- a/usr.sbin/sade/devices.c
+++ /dev/null
@@ -1,344 +0,0 @@
-/*
- * $FreeBSD$
- *
- * Copyright (c) 1995
- * Jordan Hubbard. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer,
- * verbatim and that no modifications are made prior to this
- * point in the file.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY JORDAN HUBBARD ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL JORDAN HUBBARD OR HIS PETS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, LIFE OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- *
- */
-
-#include <sys/fcntl.h>
-#include <sys/param.h>
-#include <sys/socket.h>
-#include <sys/ioctl.h>
-#include <sys/errno.h>
-#include <sys/time.h>
-#include <sys/stat.h>
-#include <ctype.h>
-#include <libdisk.h>
-
-#include "sade.h"
-
-/* how much to bias minor number for a given /dev/<ct#><un#>s<s#> slice */
-#define SLICE_DELTA (0x10000)
-
-static Device *Devices[DEV_MAX];
-static int numDevs;
-
-#define DEVICE_ENTRY(type, name, descr, max) { type, name, descr, max }
-
-#define DISK(name, descr, max) \
- DEVICE_ENTRY(DEVICE_TYPE_DISK, name, descr, max)
-
-static struct _devname {
- DeviceType type;
- char *name;
- char *description;
- int max;
-} device_names[] = {
- DISK("da%d", "SCSI disk device", 16),
- DISK("ad%d", "ATA/IDE disk device", 16),
- DISK("ar%d", "ATA/IDE RAID device", 16),
- DISK("afd%d", "ATAPI/IDE floppy device", 4),
- DISK("mlxd%d", "Mylex RAID disk", 4),
- DISK("amrd%d", "AMI MegaRAID drive", 4),
- DISK("idad%d", "Compaq RAID array", 4),
- DISK("twed%d", "3ware ATA RAID array", 4),
- DISK("aacd%d", "Adaptec FSA RAID array", 4),
- DISK("ipsd%d", "IBM ServeRAID RAID array", 4),
- DISK("mfid%d", "LSI MegaRAID SAS array", 4),
- { 0, NULL, NULL, 0 },
-};
-
-Device *
-new_device(char *name)
-{
- Device *dev;
-
- dev = safe_malloc(sizeof(Device));
- bzero(dev, sizeof(Device));
- if (name)
- SAFE_STRCPY(dev->name, name);
- return dev;
-}
-
-/* Stubs for unimplemented strategy routines */
-Boolean
-dummyInit(Device *dev)
-{
- return TRUE;
-}
-
-FILE *
-dummyGet(Device *dev, char *dist, Boolean probe)
-{
- return NULL;
-}
-
-void
-dummyShutdown(Device *dev)
-{
- return;
-}
-
-static int
-deviceTry(struct _devname dev, char *try, int i)
-{
- int fd;
- char unit[80];
-
- snprintf(unit, sizeof unit, dev.name, i);
- snprintf(try, FILENAME_MAX, "/dev/%s", unit);
- if (isDebug())
- msgDebug("deviceTry: attempting to open %s\n", try);
- fd = open(try, O_RDONLY);
- if (fd >= 0) {
- if (isDebug())
- msgDebug("deviceTry: open of %s succeeded.\n", try);
- }
- return fd;
-}
-
-/* Register a new device in the devices array */
-Device *
-deviceRegister(char *name, char *desc, char *devname, DeviceType type,
- Boolean (*init)(Device *), FILE * (*get)(Device *, char *, Boolean),
- void (*shutdown)(Device *), void *private)
-{
- Device *newdev = NULL;
-
- if (numDevs == DEV_MAX)
- msgFatal("Too many devices found!");
- else {
- newdev = new_device(name);
- newdev->description = desc;
- newdev->devname = devname;
- newdev->type = type;
- newdev->init = init ? init : dummyInit;
- newdev->get = get ? get : dummyGet;
- newdev->shutdown = shutdown ? shutdown : dummyShutdown;
- newdev->private = private;
- Devices[numDevs] = newdev;
- Devices[++numDevs] = NULL;
- }
- return newdev;
-}
-
-/* Reset the registered device chain */
-void
-deviceReset(void)
-{
- int i;
-
- for (i = 0; i < numDevs; i++) {
- DEVICE_SHUTDOWN(Devices[i]);
-
- /* XXX this potentially leaks Devices[i]->private if it's being
- * used to point to something dynamic, but you're not supposed
- * to call this routine at such times that some open instance
- * has its private ptr pointing somewhere anyway. XXX
- */
- free(Devices[i]);
- }
- Devices[numDevs = 0] = NULL;
-}
-
-/* Get all device information for devices we have attached */
-void
-deviceGetAll(void)
-{
- int i, j;
- char **names;
-
- msgNotify("Probing devices, please wait (this can take a while)...");
-
- /* Next, try to find all the types of devices one might need
- * during the second stage of the installation.
- */
- for (i = 0; device_names[i].name; i++) {
- for (j = 0; j < device_names[i].max; j++) {
- char try[FILENAME_MAX];
-
- switch(device_names[i].type) {
- case DEVICE_TYPE_DISK:
- deviceTry(device_names[i], try, j);
- break;
-
- default:
- break;
- }
- }
- }
-
- /* Finally, go get the disks and look for DOS partitions to register */
- if ((names = Disk_Names()) != NULL) {
- int i;
-
- for (i = 0; names[i]; i++) {
- Disk *d;
-
- /* Ignore memory disks */
- if (!strncmp(names[i], "md", 2))
- continue;
-
- /*
- * XXX
- * Due to unknown reasons, Disk_Names() returns SCSI CDROM as a
- * valid disk. This is main reason why sysinstall presents SCSI
- * CDROM to available disks in Fdisk/Label menu. In addition,
- * adding a blank SCSI CDROM to the menu generates floating point
- * exception in sparc64. Disk_Names() just extracts sysctl
- * "kern.disks". Why GEOM treats SCSI CDROM as a disk is beyond
- * me and that should be investigated.
- * For temporary workaround, ignore SCSI CDROM device.
- */
- if (!strncmp(names[i], "cd", 2))
- continue;
-
- d = Open_Disk(names[i]);
- if (!d) {
- msgDebug("Unable to open disk %s\n", names[i]);
- continue;
- }
-
- deviceRegister(names[i], names[i], d->name, DEVICE_TYPE_DISK,
- dummyInit, dummyGet, dummyShutdown, d);
- if (isDebug())
- msgDebug("Found a disk device named %s\n", names[i]);
-
-#if 0
- /* Look for existing DOS partitions to register as "DOS media devices" */
- for (c1 = d->chunks->part; c1; c1 = c1->next) {
- if (c1->type == fat || c1->type == efi || c1->type == extended) {
- Device *dev;
- char devname[80];
-
- /* Got one! */
- snprintf(devname, sizeof devname, "/dev/%s", c1->name);
- dev = deviceRegister(c1->name, c1->name, strdup(devname), DEVICE_TYPE_DOS,
- mediaInitDOS, mediaGetDOS, mediaShutdownDOS, NULL);
- dev->private = c1;
- if (isDebug())
- msgDebug("Found a DOS partition %s on drive %s\n", c1->name, d->name);
- }
- }
-#endif
- }
- free(names);
- }
- dlg_clear();
-}
-
-/* Rescan all devices, after closing previous set - convenience function */
-void
-deviceRescan(void)
-{
- deviceReset();
- deviceGetAll();
-}
-
-/*
- * Find all devices that match the criteria, allowing "wildcarding" as well
- * by allowing NULL or ANY values to match all. The array returned is static
- * and may be used until the next invocation of deviceFind().
- */
-Device **
-deviceFind(char *name, DeviceType class)
-{
- static Device *found[DEV_MAX];
- int i, j;
-
- j = 0;
- for (i = 0; i < numDevs; i++) {
- if ((!name || !strcmp(Devices[i]->name, name))
- && (class == DEVICE_TYPE_ANY || class == Devices[i]->type))
- found[j++] = Devices[i];
- }
- found[j] = NULL;
- return j ? found : NULL;
-}
-
-Device **
-deviceFindDescr(char *name, char *desc, DeviceType class)
-{
- static Device *found[DEV_MAX];
- int i, j;
-
- j = 0;
- for (i = 0; i < numDevs; i++) {
- if ((!name || !strcmp(Devices[i]->name, name)) &&
- (!desc || !strcmp(Devices[i]->description, desc)) &&
- (class == DEVICE_TYPE_ANY || class == Devices[i]->type))
- found[j++] = Devices[i];
- }
- found[j] = NULL;
- return j ? found : NULL;
-}
-
-int
-deviceCount(Device **devs)
-{
- int i;
-
- if (!devs)
- return 0;
- for (i = 0; devs[i]; i++);
- return i;
-}
-
-/*
- * Create a menu listing all the devices of a certain type in the system.
- * The passed-in menu is expected to be a "prototype" from which the new
- * menu is cloned.
- */
-DMenu *
-deviceCreateMenu(DMenu *menu, DeviceType type, int (*hook)(dialogMenuItem *d))
-{
- Device **devs;
- int numdevs;
- DMenu *tmp = NULL;
- int i, j;
-
- devs = deviceFind(NULL, type);
- numdevs = deviceCount(devs);
- if (!numdevs)
- return NULL;
- tmp = (DMenu *)safe_malloc(sizeof(DMenu) + (sizeof(dialogMenuItem) * (numdevs + 1)));
- bcopy(menu, tmp, sizeof(DMenu));
- for (i = 0; devs[i]; i++) {
- tmp->items[i].prompt = devs[i]->name;
- for (j = 0; j < numDevs; j++) {
- if (devs[i] == Devices[j]) {
- tmp->items[i].title = Devices[j]->description;
- break;
- }
- }
- if (j == numDevs)
- tmp->items[i].title = "<unknown device type>";
- tmp->items[i].fire = hook;
- }
- tmp->items[i].title = NULL;
- return tmp;
-}
diff --git a/usr.sbin/sade/disks.c b/usr.sbin/sade/disks.c
deleted file mode 100644
index 513643b..0000000
--- a/usr.sbin/sade/disks.c
+++ /dev/null
@@ -1,972 +0,0 @@
-/*
- * $FreeBSD$
- *
- * Copyright (c) 1995
- * Jordan Hubbard. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer,
- * verbatim and that no modifications are made prior to this
- * point in the file.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY JORDAN HUBBARD ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL JORDAN HUBBARD OR HIS PETS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, LIFE OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- *
- */
-
-#include "sade.h"
-#include <ctype.h>
-#include <fcntl.h>
-#include <inttypes.h>
-#include <libdisk.h>
-#include <sys/stat.h>
-#include <sys/disklabel.h>
-
-#ifdef WITH_SLICES
-enum size_units_t { UNIT_BLOCKS, UNIT_KILO, UNIT_MEG, UNIT_GIG, UNIT_SIZE };
-
-#ifdef PC98
-#define SUBTYPE_FREEBSD 50324
-#define SUBTYPE_FAT 37218
-#else
-#define SUBTYPE_FREEBSD 165
-#define SUBTYPE_FAT 6
-#endif
-#define SUBTYPE_EFI 239
-
-#ifdef PC98
-#define OTHER_SLICE_VALUES \
- "Other popular values are 37218 for a\n" \
- "DOS FAT partition.\n\n"
-#else
-#define OTHER_SLICE_VALUES \
- "Other popular values are 6 for a\n" \
- "DOS FAT partition, 131 for a Linux ext2fs partition, or\n" \
- "130 for a Linux swap partition.\n\n"
-#endif
-#define NON_FREEBSD_NOTE \
- "Note: If you choose a non-FreeBSD partition type, it will not\n" \
- "be formatted or otherwise prepared, it will simply reserve space\n" \
- "for you to use another tool, such as DOS format, to later format\n" \
- "and actually use the partition."
-
-/* Where we start displaying chunk information on the screen */
-#define CHUNK_START_ROW 5
-
-/* Where we keep track of MBR chunks */
-#define CHUNK_INFO_ENTRIES 16
-static struct chunk *chunk_info[CHUNK_INFO_ENTRIES];
-static int current_chunk;
-
-static void diskPartitionNonInteractive(Device *dev);
-#if !defined(__ia64__)
-static u_char * bootalloc(char *name, size_t *size);
-#endif
-
-static void
-record_chunks(Disk *d)
-{
- struct chunk *c1 = NULL;
- int i = 0;
- daddr_t last_free = 0;
-
- if (!d->chunks)
- msgFatal("No chunk list found for %s!", d->name);
-
- for (c1 = d->chunks->part; c1; c1 = c1->next) {
- if (c1->type == unused && c1->size > last_free) {
- last_free = c1->size;
- current_chunk = i;
- }
- chunk_info[i++] = c1;
- }
- chunk_info[i] = NULL;
- if (current_chunk >= i)
- current_chunk = i - 1;
-}
-
-static daddr_t Total;
-
-static void
-check_geometry(Disk *d)
-{
- int sg;
-
-#ifdef PC98
- if (d->bios_cyl >= 65536 || d->bios_hd > 256 || d->bios_sect >= 256)
-#else
- if (d->bios_cyl > 65536 || d->bios_hd > 256 || d->bios_sect >= 64)
-#endif
- {
- dlg_clear();
- sg = msgYesNo("WARNING: It is safe to use a geometry of %lu/%lu/%lu for %s on\n"
- "computers with modern BIOS versions. If this disk is to be used\n"
- "on an old machine it is recommended that it does not have more\n"
- "than 65535 cylinders, more than 255 heads, or more than\n"
-#ifdef PC98
- "255"
-#else
- "63"
-#endif
- " sectors per track.\n"
- "\n"
- "Would you like to keep using the current geometry?\n",
- d->bios_cyl, d->bios_hd, d->bios_sect, d->name);
- if (sg == 1) {
- Sanitize_Bios_Geom(d);
- msgConfirm("A geometry of %lu/%lu/%lu was calculated for %s.\n"
- "\n"
- "If you are not sure about this, please consult the Hardware Guide\n"
- "in the Documentation submenu or use the (G)eometry command to\n"
- "change it. Remember: you need to enter whatever your BIOS thinks\n"
- "the geometry is! For IDE, it's what you were told in the BIOS\n"
- "setup. For SCSI, it's the translation mode your controller is\n"
- "using. Do NOT use a ``physical geometry''.\n",
- d->bios_cyl, d->bios_hd, d->bios_sect, d->name);
- }
- }
-}
-
-static void
-print_chunks(Disk *d, int u)
-{
- int row;
- int i;
- daddr_t sz;
- char *szstr;
-
- szstr = (u == UNIT_GIG ? "GB" : (u == UNIT_MEG ? "MB" :
- (u == UNIT_KILO ? "KB" : "ST")));
-
- Total = 0;
- for (i = 0; chunk_info[i]; i++)
- Total += chunk_info[i]->size;
- attrset(A_NORMAL);
- mvaddstr(0, 0, "Disk name:\t");
- clrtobot();
- attrset(A_REVERSE); addstr(d->name); attrset(A_NORMAL);
- attrset(A_REVERSE); mvaddstr(0, 55, "FDISK Partition Editor"); attrset(A_NORMAL);
- mvprintw(1, 0,
- "DISK Geometry:\t%lu cyls/%lu heads/%lu sectors = %jd sectors (%jdMB)",
- d->bios_cyl, d->bios_hd, d->bios_sect,
- (intmax_t)d->bios_cyl * d->bios_hd * d->bios_sect,
- (intmax_t)d->bios_cyl * d->bios_hd * d->bios_sect / (1024/512) / 1024);
- mvprintw(3, 0, "%6s %10s(%s) %10s %8s %6s %10s %8s %8s",
- "Offset", "Size", szstr, "End", "Name", "PType", "Desc",
- "Subtype", "Flags");
- for (i = 0, row = CHUNK_START_ROW; chunk_info[i]; i++, row++) {
- switch(u) {
- default: /* fall thru */
- case UNIT_BLOCKS:
- sz = chunk_info[i]->size;
- break;
- case UNIT_KILO:
- sz = chunk_info[i]->size / (1024/512);
- break;
- case UNIT_MEG:
- sz = chunk_info[i]->size / (1024/512) / 1024;
- break;
- case UNIT_GIG:
- sz = chunk_info[i]->size / (1024/512) / 1024 / 1024;
- break;
- }
- if (i == current_chunk)
- attrset(ATTR_SELECTED);
- mvprintw(row, 0, "%10jd %10jd %10jd %8s %6d %10s %8d\t%-6s",
- (intmax_t)chunk_info[i]->offset, (intmax_t)sz,
- (intmax_t)chunk_info[i]->end, chunk_info[i]->name,
- chunk_info[i]->type,
- slice_type_name(chunk_info[i]->type, chunk_info[i]->subtype),
- chunk_info[i]->subtype, ShowChunkFlags(chunk_info[i]));
- if (i == current_chunk)
- attrset(A_NORMAL);
- }
-}
-
-static void
-print_command_summary(void)
-{
- mvprintw(14, 0, "The following commands are supported (in upper or lower case):");
- mvprintw(16, 0, "A = Use Entire Disk G = set Drive Geometry C = Create Slice");
- mvprintw(17, 0, "D = Delete Slice Z = Toggle Size Units S = Set Bootable | = Expert m.");
- mvprintw(18, 0, "T = Change Type U = Undo All Changes W = Write Changes Q = Finish");
- mvprintw(21, 0, "Use F1 or ? to get more help, arrow keys to select.");
- move(0, 0);
-}
-
-#ifdef PC98
-static void
-getBootMgr(char *dname, u_char **bootipl, size_t *bootipl_size,
- u_char **bootmenu, size_t *bootmenu_size)
-{
- static u_char *boot0;
- static size_t boot0_size;
- static u_char *boot05;
- static size_t boot05_size;
-
- char str[80];
- char *cp;
- int i = 0;
-
- dlg_clr_result();
- cp = variable_get(VAR_BOOTMGR);
- if (!cp) {
- /* Figure out what kind of IPL the user wants */
- sprintf(str, "Install Boot Manager for drive %s?", dname);
- MenuIPLType.title = str;
- i = dmenuOpen(&MenuIPLType);
- } else {
- if (!strncmp(cp, "boot", 4))
- dlg_add_result(MenuIPLType.items[0].prompt);
- else
- dlg_add_result(MenuIPLType.items[1].prompt);
- }
- if (cp || i) {
- if (!strcmp(dialog_vars.input_result, MenuIPLType.items[0].prompt)) {
- if (!boot0) boot0 = bootalloc("boot0", &boot0_size);
- *bootipl = boot0;
- *bootipl_size = boot0_size;
- if (!boot05) boot05 = bootalloc("boot0.5", &boot05_size);
- *bootmenu = boot05;
- *bootmenu_size = boot05_size;
- return;
- }
- }
- *bootipl = NULL;
- *bootipl_size = 0;
- *bootmenu = NULL;
- *bootmenu_size = 0;
-}
-#else
-static void
-getBootMgr(char *dname, u_char **bootCode, size_t *bootCodeSize)
-{
-#if defined(__i386__) || defined(__amd64__) /* only meaningful on x86 */
- static u_char *mbr, *boot0;
- static size_t mbr_size, boot0_size;
- char str[80];
- char *cp;
- int i = 0;
-
- dlg_clr_result();
- cp = variable_get(VAR_BOOTMGR);
- if (!cp) {
- /* Figure out what kind of MBR the user wants */
- sprintf(str, "Install Boot Manager for drive %s?", dname);
- MenuMBRType.title = str;
- i = dmenuOpen(&MenuMBRType);
- }
- else {
- if (!strcmp(cp, "standard"))
- dlg_add_result(MenuMBRType.items[0].prompt);
- if (!strncmp(cp, "boot", 4))
- dlg_add_result(MenuMBRType.items[1].prompt);
- else
- dlg_add_result(MenuMBRType.items[2].prompt);
- }
- if (cp || i) {
- if (!strcmp(dialog_vars.input_result, MenuMBRType.items[0].prompt)) {
- if (!mbr) mbr = bootalloc("mbr", &mbr_size);
- *bootCode = mbr;
- *bootCodeSize = mbr_size;
- return;
- } else if (!strcmp(dialog_vars.input_result, MenuMBRType.items[1].prompt)) {
- if (!boot0) boot0 = bootalloc("boot0", &boot0_size);
- *bootCode = boot0;
- *bootCodeSize = boot0_size;
- return;
- }
- }
-#endif
- *bootCode = NULL;
- *bootCodeSize = 0;
-}
-#endif
-#endif /* WITH_SLICES */
-
-#ifdef WITH_SLICES
-void
-diskPartition(Device *dev)
-{
- char *cp, *p;
- int rv, key = 0;
- int i;
- Boolean chunking;
- char *msg = NULL;
-#ifdef PC98
- u_char *bootipl;
- size_t bootipl_size;
- u_char *bootmenu;
- size_t bootmenu_size;
-#else
- u_char *mbrContents;
- size_t mbrSize;
-#endif
- WINDOW *w = savescr();
- Disk *d = (Disk *)dev->private;
- int size_unit;
-
- size_unit = UNIT_BLOCKS;
- chunking = TRUE;
- keypad(stdscr, TRUE);
-
- /* Flush both the dialog and curses library views of the screen
- since we don't always know who called us */
- dlg_clear(), clear();
- current_chunk = 0;
-
- /* Set up the chunk array */
- record_chunks(d);
-
- /* Give the user a chance to sanitize the disk geometry, if necessary */
- check_geometry(d);
-
- while (chunking) {
- char *val, geometry[80];
-
- /* Now print our overall state */
- if (d)
- print_chunks(d, size_unit);
- print_command_summary();
- if (msg) {
- attrset(title_attr); mvprintw(23, 0, msg); attrset(A_NORMAL);
- beep();
- msg = NULL;
- }
- else {
- move(23, 0);
- clrtoeol();
- }
-
- /* Get command character */
- key = getch();
- switch (toupper(key)) {
- case '\014': /* ^L (redraw) */
- clear();
- msg = NULL;
- break;
-
- case '\020': /* ^P */
- case KEY_UP:
- case '-':
- if (current_chunk != 0)
- --current_chunk;
- break;
-
- case '\016': /* ^N */
- case KEY_DOWN:
- case '+':
- case '\r':
- case '\n':
- if (chunk_info[current_chunk + 1])
- ++current_chunk;
- break;
-
- case KEY_HOME:
- current_chunk = 0;
- break;
-
- case KEY_END:
- while (chunk_info[current_chunk + 1])
- ++current_chunk;
- break;
-
- case KEY_F(1):
- case '?':
- systemDisplayHelp("slice");
- clear();
- break;
-
- case 'A':
- case 'F': /* Undocumented magic Dangerously Dedicated mode */
-#if !defined(__i386__) && !defined(__amd64__)
- rv = 1;
-#else /* The rest is only relevant on x86 */
- cp = variable_get(VAR_DEDICATE_DISK);
- if (cp && !strcasecmp(cp, "always"))
- rv = 1;
- else if (toupper(key) == 'A')
- rv = 0;
- else {
- rv = msgYesNo("Do you want to do this with a true partition entry\n"
- "so as to remain cooperative with any future possible\n"
- "operating systems on the drive(s)?\n"
- "(See also the section about ``dangerously dedicated''\n"
- "disks in the FreeBSD FAQ.)");
- if (rv == -1)
- rv = 0;
- }
-#endif
- All_FreeBSD(d, rv);
- variable_set2(DISK_PARTITIONED, "yes", 0);
- record_chunks(d);
- clear();
- break;
-
- case 'C':
- if (chunk_info[current_chunk]->type != unused)
- msg = "Slice in use, delete it first or move to an unused one.";
- else {
- char *val, tmp[20], name[16], *cp;
- daddr_t size;
- int subtype;
- chunk_e partitiontype;
-#ifdef PC98
- snprintf(name, sizeof (name), "%s", "FreeBSD");
- val = msgGetInput(name,
- "Please specify the name for new FreeBSD slice.");
- if (val)
- strncpy(name, val, sizeof (name));
-#else
- name[0] = '\0';
-#endif
- snprintf(tmp, 20, "%jd", (intmax_t)chunk_info[current_chunk]->size);
- val = msgGetInput(tmp, "Please specify the size for new FreeBSD slice in blocks\n"
- "or append a trailing `M' for megabytes (e.g. 20M).");
- if (val && (size = strtoimax(val, &cp, 0)) > 0) {
- if (*cp && toupper(*cp) == 'M')
- size *= ONE_MEG;
- else if (*cp && toupper(*cp) == 'G')
- size *= ONE_GIG;
- sprintf(tmp, "%d", SUBTYPE_FREEBSD);
- val = msgGetInput(tmp, "Enter type of partition to create:\n\n"
- "Pressing Enter will choose the default, a native FreeBSD\n"
- "slice (type %u). "
- OTHER_SLICE_VALUES
- NON_FREEBSD_NOTE, SUBTYPE_FREEBSD);
- if (val && (subtype = strtol(val, NULL, 0)) > 0) {
- if (subtype == SUBTYPE_FREEBSD)
- partitiontype = freebsd;
- else if (subtype == SUBTYPE_FAT)
- partitiontype = fat;
- else if (subtype == SUBTYPE_EFI)
- partitiontype = efi;
- else
-#ifdef PC98
- partitiontype = pc98;
-#else
- partitiontype = mbr;
-#endif
- Create_Chunk(d, chunk_info[current_chunk]->offset, size, partitiontype, subtype,
- (chunk_info[current_chunk]->flags & CHUNK_ALIGN), name);
- variable_set2(DISK_PARTITIONED, "yes", 0);
- record_chunks(d);
- }
- }
- clear();
- }
- break;
-
- case KEY_DC:
- case 'D':
- if (chunk_info[current_chunk]->type == unused)
- msg = "Slice is already unused!";
- else {
- Delete_Chunk(d, chunk_info[current_chunk]);
- variable_set2(DISK_PARTITIONED, "yes", 0);
- record_chunks(d);
- }
- break;
-
- case 'T':
- if (chunk_info[current_chunk]->type == unused)
- msg = "Slice is currently unused (use create instead)";
- else {
- char *val, tmp[20];
- int subtype;
- chunk_e partitiontype;
-
- sprintf(tmp, "%d", chunk_info[current_chunk]->subtype);
- val = msgGetInput(tmp, "New partition type:\n\n"
- "Pressing Enter will use the current type. To choose a native\n"
- "FreeBSD slice enter %u. "
- OTHER_SLICE_VALUES
- NON_FREEBSD_NOTE, SUBTYPE_FREEBSD);
- if (val && (subtype = strtol(val, NULL, 0)) > 0) {
- if (subtype == SUBTYPE_FREEBSD)
- partitiontype = freebsd;
- else if (subtype == SUBTYPE_FAT)
- partitiontype = fat;
- else if (subtype == SUBTYPE_EFI)
- partitiontype = efi;
- else
-#ifdef PC98
- partitiontype = pc98;
-#else
- partitiontype = mbr;
-#endif
- chunk_info[current_chunk]->type = partitiontype;
- chunk_info[current_chunk]->subtype = subtype;
- }
- }
- break;
-
- case 'G':
- snprintf(geometry, 80, "%lu/%lu/%lu", d->bios_cyl, d->bios_hd, d->bios_sect);
- val = msgGetInput(geometry, "Please specify the new geometry in cyl/hd/sect format.\n"
- "Don't forget to use the two slash (/) separator characters!\n"
- "It's not possible to parse the field without them.");
- if (val) {
- long nc, nh, ns;
- nc = strtol(val, &val, 0);
- nh = strtol(val + 1, &val, 0);
- ns = strtol(val + 1, 0, 0);
- Set_Bios_Geom(d, nc, nh, ns);
- }
- clear();
- break;
-
- case 'S':
- /* Clear active states so we won't have two */
- for (i = 0; (chunk_info[i] != NULL) && (i < CHUNK_INFO_ENTRIES); i++)
- chunk_info[i]->flags &= !CHUNK_ACTIVE;
-
- /* Set Bootable */
- chunk_info[current_chunk]->flags |= CHUNK_ACTIVE;
- break;
-
- case 'U':
- if (!variable_cmp(DISK_LABELLED, "written")) {
- msgConfirm("You've already written this information out - you\n"
- "can't undo it.");
- }
- else if (!msgNoYes("Are you SURE you want to Undo everything?")) {
- char cp[BUFSIZ];
-
- sstrncpy(cp, d->name, sizeof cp);
- Free_Disk(dev->private);
- d = Open_Disk(cp);
- if (!d)
- msgConfirm("Can't reopen disk %s! Internal state is probably corrupted", cp);
- dev->private = d;
- variable_unset(DISK_PARTITIONED);
- variable_unset(DISK_LABELLED);
- if (d)
- record_chunks(d);
- }
- clear();
- break;
-
- case 'W':
- if (!msgNoYes("WARNING: You are about to modify an EXISTING installation.\n"
- "You should simply type Q when you are finished\n"
- "here and write to the disk from the label editor.\n\n"
- "Are you absolutely sure you want to continue?")) {
- variable_set2(DISK_PARTITIONED, "yes", 0);
-
-#ifdef PC98
- /*
- * Don't trash the IPL if the first (and therefore only) chunk
- * is marked for a truly dedicated disk (i.e., the disklabel
- * starts at sector 0), even in cases where the user has
- * requested a FreeBSD Boot Manager -- both would be fatal in
- * this case.
- */
- /*
- * Don't offer to update the IPL on this disk if the first
- * "real" chunk looks like a FreeBSD "all disk" partition,
- * or the disk is entirely FreeBSD.
- */
- if ((d->chunks->part->type != freebsd) ||
- (d->chunks->part->offset > 1))
- getBootMgr(d->name, &bootipl, &bootipl_size,
- &bootmenu, &bootmenu_size);
- else {
- bootipl = NULL;
- bootipl_size = 0;
- bootmenu = NULL;
- bootmenu_size = 0;
- }
- Set_Boot_Mgr(d, bootipl, bootipl_size, bootmenu, bootmenu_size);
-#else
- /*
- * Don't trash the MBR if the first (and therefore only) chunk
- * is marked for a truly dedicated disk (i.e., the disklabel
- * starts at sector 0), even in cases where the user has
- * requested booteasy or a "standard" MBR -- both would be
- * fatal in this case.
- */
- /*
- * Don't offer to update the MBR on this disk if the first
- * "real" chunk looks like a FreeBSD "all disk" partition,
- * or the disk is entirely FreeBSD.
- */
- if ((d->chunks->part->type != freebsd) ||
- (d->chunks->part->offset > 1))
- getBootMgr(d->name, &mbrContents, &mbrSize);
- else {
- mbrContents = NULL;
- mbrSize = 0;
- }
- Set_Boot_Mgr(d, mbrContents, mbrSize);
-#endif
-
- if (DITEM_STATUS(diskPartitionWrite(dev)) != DITEM_SUCCESS)
- msgConfirm("Disk partition write returned an error status!");
- else
- msgConfirm("Wrote FDISK partition information out successfully.");
- }
- clear();
- break;
-
- case '|':
- if (!msgNoYes("Are you SURE you want to go into Wizard mode?\n"
- "No seat belts whatsoever are provided!")) {
- clear();
- refresh();
- slice_wizard(d);
- variable_set2(DISK_PARTITIONED, "yes", 0);
- record_chunks(d);
- }
- else
- msg = "Wise choice!";
- clear();
- break;
-
- case '\033': /* ESC */
- case 'Q':
- chunking = FALSE;
-#ifdef PC98
- /*
- * Don't trash the IPL if the first (and therefore only) chunk
- * is marked for a truly dedicated disk (i.e., the disklabel
- * starts at sector 0), even in cases where the user has requested
- * a FreeBSD Boot Manager -- both would be fatal in this case.
- */
- /*
- * Don't offer to update the IPL on this disk if the first "real"
- * chunk looks like a FreeBSD "all disk" partition, or the disk is
- * entirely FreeBSD.
- */
- if ((d->chunks->part->type != freebsd) ||
- (d->chunks->part->offset > 1)) {
- if (variable_cmp(DISK_PARTITIONED, "written")) {
- getBootMgr(d->name, &bootipl, &bootipl_size,
- &bootmenu, &bootmenu_size);
- if (bootipl != NULL && bootmenu != NULL)
- Set_Boot_Mgr(d, bootipl, bootipl_size,
- bootmenu, bootmenu_size);
- }
- }
-#else
- /*
- * Don't trash the MBR if the first (and therefore only) chunk
- * is marked for a truly dedicated disk (i.e., the disklabel
- * starts at sector 0), even in cases where the user has requested
- * booteasy or a "standard" MBR -- both would be fatal in this case.
- */
- /*
- * Don't offer to update the MBR on this disk if the first "real"
- * chunk looks like a FreeBSD "all disk" partition, or the disk is
- * entirely FreeBSD.
- */
- if ((d->chunks->part->type != freebsd) ||
- (d->chunks->part->offset > 1)) {
- if (variable_cmp(DISK_PARTITIONED, "written")) {
- getBootMgr(d->name, &mbrContents, &mbrSize);
- if (mbrContents != NULL)
- Set_Boot_Mgr(d, mbrContents, mbrSize);
- }
- }
-#endif
- break;
-
- case 'Z':
- size_unit = (size_unit + 1) % UNIT_SIZE;
- break;
-
- default:
- beep();
- msg = "Type F1 or ? for help";
- break;
- }
- }
- p = CheckRules(d);
- if (p) {
- char buf[FILENAME_MAX];
- DIALOG_VARS save_vars;
-
- dlg_save_vars(&save_vars);
- dialog_vars.help_line = "Press F1 to read more about disk slices.";
- dialog_vars.help_file = systemHelpFile("partition", buf);
- if (!variable_get(VAR_NO_WARN))
- xdialog_msgbox("Disk slicing warning:", p, -1, -1, 1);
- free(p);
- dlg_restore_vars(&save_vars);
- }
- restorescr(w);
-}
-#endif /* WITH_SLICES */
-
-#if !defined(__ia64__)
-static u_char *
-bootalloc(char *name, size_t *size)
-{
- char buf[FILENAME_MAX];
- struct stat sb;
-
- snprintf(buf, sizeof buf, "/boot/%s", name);
- if (stat(buf, &sb) != -1) {
- int fd;
-
- fd = open(buf, O_RDONLY);
- if (fd != -1) {
- u_char *cp;
-
- cp = malloc(sb.st_size);
- if (read(fd, cp, sb.st_size) != sb.st_size) {
- free(cp);
- close(fd);
- msgDebug("bootalloc: couldn't read %ld bytes from %s\n", (long)sb.st_size, buf);
- return NULL;
- }
- close(fd);
- if (size != NULL)
- *size = sb.st_size;
- return cp;
- }
- msgDebug("bootalloc: couldn't open %s\n", buf);
- }
- else
- msgDebug("bootalloc: can't stat %s\n", buf);
- return NULL;
-}
-#endif /* !__ia64__ */
-
-#ifdef WITH_SLICES
-static int
-partitionHook(dialogMenuItem *selected)
-{
- Device **devs = NULL;
-
- devs = deviceFind(selected->prompt, DEVICE_TYPE_DISK);
- if (!devs) {
- msgConfirm("Unable to find disk %s!", selected->prompt);
- return DITEM_FAILURE;
- }
- diskPartition(devs[0]);
- return DITEM_SUCCESS;
-}
-
-int
-diskPartitionEditor(dialogMenuItem *self)
-{
- DMenu *menu;
- Device **devs;
-
- devs = deviceFind(variable_get(VAR_DISK), DEVICE_TYPE_DISK);
- if (devs == NULL) {
- msgConfirm("No disks found! Please verify that your disk controller is being\n"
- "properly probed at boot time. See the Hardware Guide on the\n"
- "Documentation menu for clues on diagnosing this type of problem.");
- return DITEM_FAILURE;
- }
- else {
- /* No disks are selected, fall-back case now */
- int cnt = deviceCount(devs);
-
- if (cnt == 1) {
- if (variable_get(VAR_NONINTERACTIVE) &&
- !variable_get(VAR_DISKINTERACTIVE))
- diskPartitionNonInteractive(devs[0]);
- else
- diskPartition(devs[0]);
- return DITEM_SUCCESS;
- }
- else {
- int result;
-
- menu = deviceCreateMenu(&MenuDiskDevices, DEVICE_TYPE_DISK, partitionHook);
- if (!menu) {
- msgConfirm("No devices suitable for installation found!\n\n"
- "Please verify that your disk controller (and attached drives)\n"
- "were detected properly. This can be done by pressing the\n"
- "[Scroll Lock] key and using the Arrow keys to move back to\n"
- "the boot messages. Press [Scroll Lock] again to return.");
- return DITEM_FAILURE;
- }
-
- result = dmenuOpen(menu) ? DITEM_SUCCESS : DITEM_FAILURE;
- free(menu);
- return result;
- }
- }
- return DITEM_SUCCESS;
-}
-#endif /* WITH_SLICES */
-
-int
-diskPartitionWrite(Device *dev)
-{
- Disk *d = (Disk *)dev->private;
-#if !defined(__ia64__)
- static u_char *boot1;
-#endif
-#if defined(__i386__) || defined(__amd64__)
- static u_char *boot2;
-#endif
-
- if (!variable_cmp(DISK_PARTITIONED, "written"))
- return DITEM_SUCCESS;
-
-#if defined(__i386__) || defined(__amd64__)
- if (!boot1) boot1 = bootalloc("boot1", NULL);
- if (!boot2) boot2 = bootalloc("boot2", NULL);
- Set_Boot_Blocks(d, boot1, boot2);
-#elif !defined(__ia64__)
- if (!boot1) boot1 = bootalloc("boot1", NULL);
- Set_Boot_Blocks(d, boot1, NULL);
-#endif
-
- msgNotify("Writing partition information to drive %s", d->name);
- if (!Fake && Write_Disk(d)) {
- msgConfirm("ERROR: Unable to write data to disk %s!", d->name);
- return DITEM_FAILURE;
- }
-
- /* Now it's not "yes", but "written" */
- variable_set2(DISK_PARTITIONED, "written", 0);
- return DITEM_SUCCESS | DITEM_RESTORE;
-}
-
-#ifdef WITH_SLICES
-/* Partition a disk based wholly on which variables are set */
-static void
-diskPartitionNonInteractive(Device *dev)
-{
- char *cp;
- int i, all_disk = 0;
- daddr_t sz;
-#ifdef PC98
- u_char *bootipl;
- size_t bootipl_size;
- u_char *bootmenu;
- size_t bootmenu_size;
-#else
- u_char *mbrContents;
- size_t mbrSize;
-#endif
- Disk *d = (Disk *)dev->private;
-
- record_chunks(d);
- cp = variable_get(VAR_GEOMETRY);
- if (cp) {
- if (!strcasecmp(cp, "sane")) {
-#ifdef PC98
- if (d->bios_cyl >= 65536 || d->bios_hd > 256 || d->bios_sect >= 256)
-#else
- if (d->bios_cyl > 65536 || d->bios_hd > 256 || d->bios_sect >= 64)
-#endif
- {
- msgDebug("Warning: A geometry of %lu/%lu/%lu for %s is incorrect.\n",
- d->bios_cyl, d->bios_hd, d->bios_sect, d->name);
- Sanitize_Bios_Geom(d);
- msgDebug("Sanitized geometry for %s is %lu/%lu/%lu.\n",
- d->name, d->bios_cyl, d->bios_hd, d->bios_sect);
- }
- } else {
- msgDebug("Setting geometry from script to: %s\n", cp);
- d->bios_cyl = strtol(cp, &cp, 0);
- d->bios_hd = strtol(cp + 1, &cp, 0);
- d->bios_sect = strtol(cp + 1, 0, 0);
- }
- }
-
- cp = variable_get(VAR_PARTITION);
- if (cp) {
- if (!strcmp(cp, "free")) {
- /* Do free disk space case */
- for (i = 0; chunk_info[i]; i++) {
- /* If a chunk is at least 10MB in size, use it. */
- if (chunk_info[i]->type == unused && chunk_info[i]->size > (10 * ONE_MEG)) {
- Create_Chunk(d, chunk_info[i]->offset, chunk_info[i]->size,
- freebsd, 3,
- (chunk_info[i]->flags & CHUNK_ALIGN),
- "FreeBSD");
- variable_set2(DISK_PARTITIONED, "yes", 0);
- break;
- }
- }
- if (!chunk_info[i]) {
- msgConfirm("Unable to find any free space on this disk!");
- return;
- }
- }
- else if (!strcmp(cp, "all")) {
- /* Do all disk space case */
- msgDebug("Warning: Devoting all of disk %s to FreeBSD.\n", d->name);
-
- All_FreeBSD(d, FALSE);
- }
- else if (!strcmp(cp, "exclusive")) {
- /* Do really-all-the-disk-space case */
- msgDebug("Warning: Devoting all of disk %s to FreeBSD.\n", d->name);
-
- All_FreeBSD(d, all_disk = TRUE);
- }
- else if ((sz = strtoimax(cp, &cp, 0))) {
- /* Look for sz bytes free */
- if (*cp && toupper(*cp) == 'M')
- sz *= ONE_MEG;
- else if (*cp && toupper(*cp) == 'G')
- sz *= ONE_GIG;
- for (i = 0; chunk_info[i]; i++) {
- /* If a chunk is at least sz MB, use it. */
- if (chunk_info[i]->type == unused && chunk_info[i]->size >= sz) {
- Create_Chunk(d, chunk_info[i]->offset, sz, freebsd, 3,
- (chunk_info[i]->flags & CHUNK_ALIGN),
- "FreeBSD");
- variable_set2(DISK_PARTITIONED, "yes", 0);
- break;
- }
- }
- if (!chunk_info[i]) {
- msgConfirm("Unable to find %jd free blocks on this disk!",
- (intmax_t)sz);
- return;
- }
- }
- else if (!strcmp(cp, "existing")) {
- /* Do existing FreeBSD case */
- for (i = 0; chunk_info[i]; i++) {
- if (chunk_info[i]->type == freebsd)
- break;
- }
- if (!chunk_info[i]) {
- msgConfirm("Unable to find any existing FreeBSD partitions on this disk!");
- return;
- }
- }
- else {
- msgConfirm("`%s' is an invalid value for %s - is config file valid?", cp, VAR_PARTITION);
- return;
- }
- if (!all_disk) {
-#ifdef PC98
- getBootMgr(d->name, &bootipl, &bootipl_size,
- &bootmenu, &bootmenu_size);
- Set_Boot_Mgr(d, bootipl, bootipl_size, bootmenu, bootmenu_size);
-#else
- getBootMgr(d->name, &mbrContents, &mbrSize);
- Set_Boot_Mgr(d, mbrContents, mbrSize);
-#endif
- }
- variable_set2(DISK_PARTITIONED, "yes", 0);
- }
-}
-#endif /* WITH_SLICES */
diff --git a/usr.sbin/sade/dmenu.c b/usr.sbin/sade/dmenu.c
deleted file mode 100644
index 186c1e9..0000000
--- a/usr.sbin/sade/dmenu.c
+++ /dev/null
@@ -1,93 +0,0 @@
-/*
- * $FreeBSD$
- *
- * Copyright (c) 1995
- * Jordan Hubbard. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer,
- * verbatim and that no modifications are made prior to this
- * point in the file.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY JORDAN HUBBARD ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL JORDAN HUBBARD OR HIS PETS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, LIFE OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- *
- */
-
-#include "sade.h"
-#include <errno.h>
-
-#define MAX_MENU 15
-
-static int
-menu_height(DMenu *menu, int n)
-{
- int max;
- char *t;
-
- max = MAX_MENU;
- if (StatusLine > 24)
- max += StatusLine - 24;
- for (t = menu->prompt; *t; t++) {
- if (*t == '\n')
- --max;
- }
- return n > max ? max : n;
-}
-
-/* Traverse over an internal menu */
-Boolean
-dmenuOpen(DMenu *menu)
-{
- int n, rval = 0;
-
- /* Count up all the items */
- for (n = 0; menu->items[n].title; n++)
- ;
-
- while (1) {
- char buf[FILENAME_MAX];
- DIALOG_VARS save_vars;
- WINDOW *w = savescr();
-
- /* Any helpful hints, put 'em up! */
- dlg_save_vars(&save_vars);
- dialog_vars.help_line = menu->helpline;
- dialog_vars.help_file = systemHelpFile(menu->helpfile, buf);
- dlg_clear();
- /* Pop up that dialog! */
- if (menu->type & DMENU_NORMAL_TYPE) {
- rval = xdialog_menu(menu->title, menu->prompt,
- -1, -1, menu_height(menu, n), n, menu->items);
- } else if (menu->type & DMENU_RADIO_TYPE) {
- rval = xdialog_radiolist(menu->title, menu->prompt,
- -1, -1, menu_height(menu, n), n, menu->items);
- } else {
- msgFatal("Menu: `%s' is of an unknown type\n", menu->title);
- }
- dlg_restore_vars(&save_vars);
- if (rval) {
- restorescr(w);
- return FALSE;
- } else if (menu->type & DMENU_SELECTION_RETURNS) {
- restorescr(w);
- return TRUE;
- } else
- delwin(w);
- }
-}
diff --git a/usr.sbin/sade/globals.c b/usr.sbin/sade/globals.c
deleted file mode 100644
index 0b08da3..0000000
--- a/usr.sbin/sade/globals.c
+++ /dev/null
@@ -1,84 +0,0 @@
-/*
- * $FreeBSD$
- *
- * Copyright (c) 1995
- * Jordan Hubbard. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer,
- * verbatim and that no modifications are made prior to this
- * point in the file.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY JORDAN HUBBARD ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL JORDAN HUBBARD OR HIS PETS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, LIFE OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- *
- */
-
-#include "sade.h"
-
-/*
- * Various global variables and an initialization hook to set them to
- * whatever values we feel are appropriate.
- */
-
-int DebugFD; /* Where diagnostic output goes */
-Boolean Fake; /* Only pretend to be useful */
-Boolean DialogActive; /* Is libdialog initialized? */
-Boolean ColorDisplay; /* Are we on a color display? */
-Boolean OnVTY; /* Are we on a VTY? */
-Boolean Restarting; /* Are we restarting sysinstall? */
-Variable *VarHead; /* The head of the variable chain */
-int BootMgr; /* Which boot manager we're using */
-int StatusLine; /* Where to stick our status messages */
-jmp_buf BailOut; /* Beam me up, scotty! The natives are pissed! */
-
-Chunk *HomeChunk;
-Chunk *RootChunk;
-Chunk *SwapChunk;
-Chunk *TmpChunk;
-Chunk *UsrChunk;
-Chunk *VarChunk;
-#ifdef __ia64__
-Chunk *EfiChunk;
-#endif
-
-/*
- * Yes, I know some of these are already automatically initialized as
- * globals. I simply find it clearer to set everything explicitly.
- */
-void
-globalsInit(void)
-{
- DebugFD = -1;
- ColorDisplay = FALSE;
- Fake = FALSE;
- Restarting = FALSE;
- OnVTY = FALSE;
- DialogActive = FALSE;
- VarHead = NULL;
-
- HomeChunk = NULL;
- RootChunk = NULL;
- SwapChunk = NULL;
- TmpChunk = NULL;
- UsrChunk = NULL;
- VarChunk = NULL;
-#ifdef __ia64__
- EfiChunk = NULL;
-#endif
-}
diff --git a/usr.sbin/sade/help/partition.hlp b/usr.sbin/sade/help/partition.hlp
deleted file mode 100644
index 3d13b34..0000000
--- a/usr.sbin/sade/help/partition.hlp
+++ /dev/null
@@ -1,169 +0,0 @@
-This is the FreeBSD DiskLabel Editor.
-
-NOTE: If you're entering this editor from the update procedure then
-you probably shouldn't (C)reate anything at all but rather use only
-the (M)ount command to check and mount existing partitions for
-upgrading.
-
-If you would like the label editor to do most of the following for
-you, simply type `A' for automatic partitioning of the disk.
-
-If you wish to create partitions manually you may do so by moving the
-highlighted selection bar with the arrow keys over the FreeBSD
-partition(s) displayed at the top of the screen. Typing (C)reate
-while a partition with available free space is selected will allow you
-to create a BSD partition inside of it using some or all of its
-available space.
-
-Typing (M)ount over an existing partition entry (displayed in the
-middle of the screen) will allow you to set a mount point for it
-without initializing it. If you want it initialized, use the (T)oggle
-command to flip the Newfs flag. When Newfs is set to "Y", the
-filesystem in question will be ERASED and rebuilt from scratch!
-
-
-You should use this editor to create at least the following
-filesystems:
-
- Name Purpose Min Size? Optional?
- ---- ------- --------- ---------
- / Root filesystem 118MB No
- swap Swap space 2 * MEM No
- /usr System & user files 128MB or more Yes
-
-Note: If you do not create a /usr filesystem then your / filesystem
-will need to be bigger - at least 240MB. This is not recommended as
-any media errors that may occur during disk I/O to user files will
-corrupt the filesystem containing vital system files as well. It is
-for this reason that / is generally kept on its own filesystem, where
-it should be considered essentially "read only" in your administration
-of it.
-
-Swap space is a little tricker, and the rule of "2 * MEM" is simply a
-best-guess approximation and not necessarily accurate for your
-intended usage of the system. If you intend to use the system heavily
-in a server or multi-user application, you may be well advised to
-increase this size. You may also create swap space on multiple drives
-for a larger "total" swap and this is, in fact, recommended if you
-have multiple, fast drives for which such load-balancing can only help
-overall I/O performance.
-
-The /usr filesystem should be sized according to what kind of
-distributions you're trying to load and how many packages you intend
-to install in locations like /usr/local. You can also make /usr/local
-a separate filesystem if you don't want to risk filling up your /usr
-by mistake.
-
-Another useful filesystem to create is /var, which contains mail, news
-printer spool files and other temporary items. It is a popular
-candidate for a separate partition and should be sized according to
-your estimates of the amount of mail, news or spooled print jobs that
-may be stored there.
-
-WARNING: If you do not create a separate filesystem for /var, space
-for such files will be allocated out of the root (/) filesystem
-instead. You may therefore wish to make the / partition bigger if you
-expect a lot of mail or news and do not want to make /var its own
-partition.
-
-If you're new to this installation, you might also want to read the
-following explanation of how FreeBSD's new "slice" paradigm for
-looking at disk storage works:
-
-
-In FreeBSD's new system, a device name can be broken up into up to 3
-parts. Take a typical name like ``/dev/da0s1a'':
-
- The first three characters represent the drive name. If we had
- a system with two SCSI drives on it then we'd see /dev/da0 and
- /dev/da1 as the device entries representing the entire drives.
-
- Next you have the "slice" (or "FDISK Partition") number,
- as seen in the Partition Editor. Assuming that our da0
- contained two slices, a FreeBSD slice and a DOS slice, that
- would give us /dev/da0s1 and /dev/da0s2 as device entries pointing
- to the entire slices.
-
- Next, if a slice is a FreeBSD slice, you can have a number of
- (confusingly named) "partitions" inside of it.
-
- These partitions are where various filesystems or swap areas live,
- and using our hypothetical two-SCSI-disk machine again, we might
- have something like the following layout on da0:
-
- Name Mountpoint
- ---- ----------
- da0s1a /
- da0s1b <swap space>
- da0s1e /usr
-
-Once you understand all this, then the purpose of the label editor
-becomes fairly clear: You're carving up the FreeBSD slices displayed
-at the top of the screen into smaller pieces, which are displayed in
-the middle of the screen, and then assigning FreeBSD file system names
-(mount points) to them.
-
-You can also use the label editor to mount existing partitions/slices
-into your filesystem hierarchy, as is frequently done for DOS FAT
-slices. For FreeBSD partitions, you can also toggle the "newfs" state
-so that the partitions are either (re)created from scratch or simply
-checked and mounted (the contents are preserved).
-
-If you set (S)oftUpdates on a filesystem, it will cause the
-"Soft Updates" policy to be in effect for it. This basically causes
-both metadata and data blocks to be written asynchronously to disk,
-but with extra state information which causes the metadata and any
-related data blocks to be committed in a single transaction. This
-results in async metadata update speeds (which are considerably
-faster than the default sync) without the potential for data loss
-which could occur if you simply mounted the filesystem with purely
-"async" update policy and then had a power failure. If you wish
-to later turn the softupdates policy back off, use the command
-"tunefs -n disable devicename". NOTE: It is probably not wise
-to use this on your root filesystem unless you have a large
-(e.g. non-standard size) root. The reason is that smaller filesystems
-with significant activity can temporarily overflow if the soft updates
-policy results in free'd blocks not being "garbage collected" as fast
-as they're being requested.
-
-The UNIX File System (UFS) on FreeBSD supports two different on-disk
-layouts: UFS1 and UFS2. UFS1 was the default file system in use
-through FreeBSD 5.0-RELEASE; as of FreeBSD 5.1-RELEASE, the default
-is now UFS2, with the exception of the PC98 platform. UFS2 provides
-sparse inode allocation (faster fsck), 64-bit storage pointers (larger
-maximum size), and native extended attributes (required for ACLs, MAC,
-and other advanced security and file system services). The selection
-of UFS1 or UFS2 must be made when the file system is created--later
-conversion is not currently possible. UFS2 is the recommended file
-system, but if disks are to be used on older FreeBSD systems, UFS1
-improves portability. When dual-booting between FreeBSD 4.x or
-earlier and FreeBSD 5.x, UFS1 file systems will be accessible from
-both. To toggle a file system to UFS1, press '1'. To restore it to
-UFS2, press '2'.
-
-WARNING: FreeBSD on i386 is currently unable to boot from root file
-systems larger than 1.5TB.
-
-To add additional flags to the newfs command line for UFS file
-systems, press 'N'. These options will be specified before the
-device argument of the command line, but after any other options
-placed there by sysinstall, such as the UFS version and soft
-updates flag; as such, arguments provided may override existing
-settings. To completely replace the newfs command used by
-sysinstall, press 'Z' to convert a partition to a Custom
-partition type. Sysinstall will prompt you with the newfs
-command line that it would have used based on existing settings
-prior to the change, but allow you to modify any aspect of the
-command line. Once a partition has been converted to a custom
-partition in the label editor, you will need to restart the
-labeling process or delete and recreate the partition to restore
-it to a non-custom state. Custom partitions are represented by
-the letters "CST" instead of "UFS" or "FAT.
-
-When you're done, type `Q' to exit.
-
-No actual changes will be made to the disk until you (C)ommit from the
-Install menu or (W)rite directly from this one. You're working with
-what is essentially a copy of the disk label(s), both here and in the
-FDISK Partition Editor, and the actual on-disk labels won't be
-affected by any changes you make until you explicitly say so.
diff --git a/usr.sbin/sade/help/slice.hlp b/usr.sbin/sade/help/slice.hlp
deleted file mode 100644
index e9f3abb..0000000
--- a/usr.sbin/sade/help/slice.hlp
+++ /dev/null
@@ -1,57 +0,0 @@
-This is the Main Slice (``FDISK'' or PC-style Partition) Editor.
-
-Possible commands are printed at the bottom and the Master Boot Record
-contents are shown at the top. You can move up and down with the
-arrow keys and (C)reate a new slice whenever the highlighted
-selection bar is over a slice whose type is marked as "unused."
-
-You are expected to leave this screen with at least one slice
-marked "FreeBSD." Note that unlike Linux, you don't need to create
-multiple FreeBSD FDISK partition entries for different things like
-swap, file systems, etc. The usual convention is to create ONE
-FreeBSD slice (FDISK partition) per drive and then subsection this slice
-into swap and file systems with the Label editor.
-
-No actual changes will be made to the disk until you (C)ommit from the
-Install menu or use the (W)rite option here! You're working with what
-is essentially a copy of the disk label(s), both here and in the Label
-Editor.
-
-If you want to use the entire disk for FreeBSD, type `A'. Slices will
-be aligned to fictitious cylinder boundaries and space will be reserved
-in front of the FreeBSD slice for a [future] possible boot manager.
-
-For the truly dedicated disk case, type `F'. You'll be asked whether or
-not you wish to keep the disk (potentially) compatible with other
-operating systems, i.e. the information in the FDISK table should be
-kept valid. A truly dedicated disk can be achieved by selecting `No'.
-In that case, all BIOS geometry considerations will no longer be in
-effect and you can safely ignore any ``The detected geometry is
-invalid'' warning messages you may later see. It is also not necessary
-in this case to set a slice bootable or install an MBR boot manager as
-both things are then irrelevant. The FreeBSD slice will start at
-absolute sector 0 of the disk (so that FreeBSD's disk label is identical
-to the Master Boot Record) and extend to the very last sector of the
-disk medium. Needless to say, such a disk cannot have any sort of a
-boot manager, `disk manager', or anything else that has to interact with
-the BIOS. This option is therefore only considered safe for SCSI disks
-and most IDE disks and is primarily intended for people who are going to
-set up a dedicated FreeBSD server or workstation, not a typical `home PC'.
-
-If you select the default of `Yes' at the compatibility, slices will be
-aligned to fictitious cylinder boundaries and space will be reserved
-in front of the FreeBSD slice for a [future] possible boot manager.
-This is pretty much equivalent to having chosen `A' originally.
-
-The flags field has the following legend:
-
- '=' -- This slice is properly aligned.
- 'A' -- This slice is marked active.
- 'R' -- This slice contains the root (/) filesystem
-
-If no slice is marked Active, you will need to either install
-a Boot Manager (the option for which will be presented later in the
-installation) or set one Active before leaving this screen.
-
-To leave the slice editor, type `Q'.
-
diff --git a/usr.sbin/sade/install.c b/usr.sbin/sade/install.c
deleted file mode 100644
index e3364fe..0000000
--- a/usr.sbin/sade/install.c
+++ /dev/null
@@ -1,249 +0,0 @@
-/*
- * $FreeBSD$
- *
- * Copyright (c) 1995
- * Jordan Hubbard. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer,
- * verbatim and that no modifications are made prior to this
- * point in the file.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY JORDAN HUBBARD ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL JORDAN HUBBARD OR HIS PETS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, LIFE OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- *
- */
-
-#include <ctype.h>
-#include <sys/consio.h>
-#include <sys/disklabel.h>
-#include <sys/errno.h>
-#include <sys/ioctl.h>
-#include <sys/fcntl.h>
-#include <sys/wait.h>
-#include <sys/uio.h>
-#include <sys/param.h>
-#define MSDOSFS
-#include <sys/mount.h>
-#include <ufs/ufs/ufsmount.h>
-#include <fs/msdosfs/msdosfsmount.h>
-#undef MSDOSFS
-#include <sys/stat.h>
-#include <sys/sysctl.h>
-#include <libdisk.h>
-#include <limits.h>
-#include <unistd.h>
-#include <termios.h>
-
-#include "sade.h"
-
-#define TERMCAP_FILE "/usr/share/misc/termcap"
-
-Boolean
-checkLabels(Boolean whinge)
-{
- Boolean status;
-
- /* Don't allow whinging if noWarn is set */
- if (variable_get(VAR_NO_WARN))
- whinge = FALSE;
-
- status = TRUE;
- HomeChunk = RootChunk = SwapChunk = NULL;
- TmpChunk = UsrChunk = VarChunk = NULL;
-#ifdef __ia64__
- EfiChunk = NULL;
-#endif
-
- /* We don't need to worry about root/usr/swap if we're already multiuser */
- return status;
-}
-
-#define QUEUE_YES 1
-#define QUEUE_NO 0
-static int
-performNewfs(PartInfo *pi, char *dname, int queue)
-{
- char buffer[LINE_MAX];
-
- if (pi->do_newfs) {
- switch(pi->newfs_type) {
- case NEWFS_UFS:
- snprintf(buffer, LINE_MAX, "%s %s %s %s %s",
- NEWFS_UFS_CMD,
- pi->newfs_data.newfs_ufs.softupdates ? "-U" : "",
- pi->newfs_data.newfs_ufs.ufs1 ? "-O1" : "-O2",
- pi->newfs_data.newfs_ufs.user_options,
- dname);
- break;
-
- case NEWFS_MSDOS:
- snprintf(buffer, LINE_MAX, "%s %s", NEWFS_MSDOS_CMD,
- dname);
- break;
-
- case NEWFS_CUSTOM:
- snprintf(buffer, LINE_MAX, "%s %s",
- pi->newfs_data.newfs_custom.command, dname);
- break;
- }
-
- if (queue == QUEUE_YES) {
- command_shell_add(pi->mountpoint, "%s", buffer);
- return (0);
- } else
- return (vsystem("%s", buffer));
- }
- return (0);
-}
-
-/* Go newfs and/or mount all the filesystems we've been asked to */
-int
-installFilesystems(Device *dev)
-{
- Disk *disk = (Disk *)dev->private;
- Chunk *c1, *c2;
- PartInfo *root;
- char dname[80];
- Boolean upgrade = FALSE;
-
- /* If we've already done this, bail out */
- if (!variable_cmp(DISK_LABELLED, "written"))
- return DITEM_SUCCESS;
-
- upgrade = !variable_cmp(SYSTEM_STATE, "upgrade");
- if (!checkLabels(TRUE))
- return DITEM_FAILURE;
-
- root = (RootChunk != NULL) ? (PartInfo *)RootChunk->private_data : NULL;
-
- command_clear();
-
- /* Now buzz through the rest of the partitions and mount them too */
- if (!disk->chunks) {
- msgConfirm("No chunk list found for %s!", disk->name);
- return DITEM_FAILURE | DITEM_RESTORE;
- }
- for (c1 = disk->chunks->part; c1; c1 = c1->next) {
-#ifdef __ia64__
- if (c1->type == part) {
- c2 = c1;
- {
-#elif defined(__powerpc__)
- if (c1->type == apple) {
- for (c2 = c1->part; c2; c2 = c2->next) {
-#else
- if (c1->type == freebsd) {
- for (c2 = c1->part; c2; c2 = c2->next) {
-#endif
- if (c2->type == part && c2->subtype != FS_SWAP && c2->private_data) {
- PartInfo *tmp = (PartInfo *)c2->private_data;
-
- /* Already did root */
- if (c2 == RootChunk)
- continue;
-
- sprintf(dname, "/dev/%s", c2->name);
-
- if (tmp->do_newfs && (!upgrade ||
- !msgNoYes("You are upgrading - are you SURE you"
- " want to newfs /dev/%s?", c2->name)))
- performNewfs(tmp, dname, QUEUE_YES);
- else
- command_shell_add(tmp->mountpoint,
- "fsck_ffs -y /dev/%s", c2->name);
- command_func_add(tmp->mountpoint, Mount, c2->name);
- }
- else if (c2->type == part && c2->subtype == FS_SWAP) {
- char fname[80];
- int i;
-
- if (c2 == SwapChunk)
- continue;
- sprintf(fname, "/dev/%s", c2->name);
- i = (Fake || swapon(fname));
- if (!i) {
- dlg_clear();
- msgNotify("Added %s as an additional swap device", fname);
- }
- else {
- msgConfirm("Unable to add %s as a swap device: %s", fname, strerror(errno));
- }
- }
- }
- }
- else if (c1->type == fat && c1->private_data &&
- (root->do_newfs || upgrade)) {
- char name[FILENAME_MAX];
-
- sprintf(name, "/%s", ((PartInfo *)c1->private_data)->mountpoint);
- Mkdir(name);
- }
-#if defined(__ia64__)
- else if (c1->type == efi && c1->private_data) {
- PartInfo *pi = (PartInfo *)c1->private_data;
-
- sprintf(dname, "/dev/%s", c1->name);
-
- if (pi->do_newfs && (!upgrade ||
- !msgNoYes("You are upgrading - are you SURE you want to "
- "newfs /dev/%s?", c1->name)))
- performNewfs(pi, dname, QUEUE_YES);
- }
-#endif
- }
-
- command_sort();
- command_execute();
- dlg_clear();
- return DITEM_SUCCESS | DITEM_RESTORE;
-}
-
-static char *
-getRelname(void)
-{
- static char buf[64];
- size_t sz = (sizeof buf) - 1;
-
- if (sysctlbyname("kern.osrelease", buf, &sz, NULL, 0) != -1) {
- buf[sz] = '\0';
- return buf;
- }
- else
- return "<unknown>";
-}
-
-/* Initialize various user-settable values to their defaults */
-int
-installVarDefaults(dialogMenuItem *self)
-{
-
- /* Set default startup options */
- variable_set2(VAR_RELNAME, getRelname(), 0);
- variable_set2(SYSTEM_STATE, "update", 0);
- variable_set2(VAR_NEWFS_ARGS, "-b 16384 -f 2048", 0);
- variable_set2(VAR_CONSTERM, "NO", 0);
- return DITEM_SUCCESS;
-}
-
-/* Load the environment up from various system configuration files */
-void
-installEnvironment(void)
-{
-}
-
diff --git a/usr.sbin/sade/label.c b/usr.sbin/sade/label.c
deleted file mode 100644
index d6b03b5..0000000
--- a/usr.sbin/sade/label.c
+++ /dev/null
@@ -1,1646 +0,0 @@
-/*
- * $FreeBSD$
- *
- * Copyright (c) 1995
- * Jordan Hubbard. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer,
- * verbatim and that no modifications are made prior to this
- * point in the file.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY JORDAN HUBBARD ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL JORDAN HUBBARD OR HIS PETS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, LIFE OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- *
- */
-
-#include <sys/types.h>
-#include <ctype.h>
-#include <inttypes.h>
-#include <libdisk.h>
-#include <sys/disklabel.h>
-#include <sys/param.h>
-#include <sys/sysctl.h>
-
-#include "sade.h"
-
-#define AUTO_HOME 0 /* do not create /home automatically */
-
-/*
- * Everything to do with editing the contents of disk labels.
- */
-
-/* A nice message we use a lot in the disklabel editor */
-#define MSG_NOT_APPLICABLE "That option is not applicable here"
-
-/* Where to start printing the freebsd slices */
-#define CHUNK_SLICE_START_ROW 2
-#define CHUNK_PART_START_ROW 11
-
-/* The smallest filesystem we're willing to create */
-#define FS_MIN_SIZE ONE_MEG
-
-/*
- * Minimum partition sizes
- */
-#if defined(__alpha__) || defined(__ia64__) || defined(__sparc64__) || defined(__amd64__)
-#define ROOT_MIN_SIZE 128
-#else
-#define ROOT_MIN_SIZE 118
-#endif
-#define SWAP_MIN_SIZE 32
-#define USR_MIN_SIZE 128
-#define VAR_MIN_SIZE 20
-#define TMP_MIN_SIZE 20
-#define HOME_MIN_SIZE 20
-
-/*
- * Swap size limit for auto-partitioning (4G).
- */
-#define SWAP_AUTO_LIMIT_SIZE 4096
-
-/*
- * Default partition sizes. If we do not have sufficient disk space
- * for this configuration we scale things relative to the NOM vs DEFAULT
- * sizes. If the disk is larger then /home will get any remaining space.
- */
-#define ROOT_DEFAULT_SIZE 512
-#define USR_DEFAULT_SIZE 8192
-#define VAR_DEFAULT_SIZE 1024
-#define TMP_DEFAULT_SIZE 512
-#define HOME_DEFAULT_SIZE USR_DEFAULT_SIZE
-
-/*
- * Nominal partition sizes. These are used to scale the default sizes down
- * when we have insufficient disk space. If this isn't sufficient we scale
- * down using the MIN sizes instead.
- */
-#define ROOT_NOMINAL_SIZE 256
-#define USR_NOMINAL_SIZE 1536
-#define VAR_NOMINAL_SIZE 128
-#define TMP_NOMINAL_SIZE 128
-#define HOME_NOMINAL_SIZE USR_NOMINAL_SIZE
-
-/* The bottom-most row we're allowed to scribble on */
-#define CHUNK_ROW_MAX 16
-
-
-/* All the chunks currently displayed on the screen */
-static struct {
- struct chunk *c;
- PartType type;
-} label_chunk_info[MAX_CHUNKS + 1];
-static int here;
-
-/*** with this value we try to track the most recently added label ***/
-static int label_focus = 0, pslice_focus = 0;
-
-static int diskLabel(Device *dev);
-static int diskLabelNonInteractive(Device *dev);
-static char *try_auto_label(Device *dev, int perc, int *req);
-
-static int
-labelHook(dialogMenuItem *selected)
-{
- Device **devs = NULL;
-
- devs = deviceFind(selected->prompt, DEVICE_TYPE_DISK);
- if (!devs) {
- msgConfirm("Unable to find disk %s!", selected->prompt);
- return DITEM_FAILURE;
- }
- diskLabel(devs[0]);
- return DITEM_SUCCESS;
-}
-
-int
-diskLabelEditor(dialogMenuItem *self)
-{
- DMenu *menu;
- Device **devs;
- int result;
-
- devs = deviceFind(variable_get(VAR_DISK), DEVICE_TYPE_DISK);
- if (devs == NULL) {
- msgConfirm("No disks found! Please verify that your disk controller is being\n"
- "properly probed at boot time. See the Hardware Guide on the\n"
- "Documentation menu for clues on diagnosing this type of problem.");
- return DITEM_FAILURE;
- }
- else {
- /* No disks are selected, fall-back case now */
- int cnt = deviceCount(devs);
- if (cnt == 1) {
- if (variable_get(VAR_NONINTERACTIVE) &&
- !variable_get(VAR_DISKINTERACTIVE))
- result = diskLabelNonInteractive(devs[0]);
- else
- result = diskLabel(devs[0]);
- }
- else {
- menu = deviceCreateMenu(&MenuDiskDevices, DEVICE_TYPE_DISK, labelHook);
- if (!menu) {
- msgConfirm("No devices suitable for installation found!\n\n"
- "Please verify that your disk controller (and attached drives)\n"
- "were detected properly. This can be done by pressing the\n"
- "[Scroll Lock] key and using the Arrow keys to move back to\n"
- "the boot messages. Press [Scroll Lock] again to return.");
- result = DITEM_FAILURE;
- }
- else {
- result = dmenuOpen(menu) ? DITEM_SUCCESS : DITEM_FAILURE;
- free(menu);
- }
- }
- }
- if (DITEM_STATUS(result) != DITEM_FAILURE) {
- if (variable_cmp(DISK_LABELLED, "written"))
- variable_set2(DISK_LABELLED, "yes", 0);
- }
- return result;
-}
-
-int
-diskLabelCommit(Device *dev)
-{
- char *cp;
- int i;
-
- /* Already done? */
- if ((cp = variable_get(DISK_LABELLED)) && strcmp(cp, "yes"))
- i = DITEM_SUCCESS;
- else if (!cp) {
- msgConfirm("You must assign disk labels before this option can be used.");
- i = DITEM_FAILURE;
- }
- /* The routine will guard against redundant writes, just as this one does */
- else if (DITEM_STATUS(diskPartitionWrite(dev)) != DITEM_SUCCESS)
- i = DITEM_FAILURE;
- else if (DITEM_STATUS(installFilesystems(dev)) != DITEM_SUCCESS)
- i = DITEM_FAILURE;
- else {
- msgInfo("All filesystem information written successfully.");
- variable_set2(DISK_LABELLED, "written", 0);
- i = DITEM_SUCCESS;
- }
- return i;
-}
-
-/* See if we're already using a desired partition name */
-static Boolean
-check_conflict(char *name)
-{
- int i;
-
- for (i = 0; label_chunk_info[i].c; i++)
- if ((label_chunk_info[i].type == PART_FILESYSTEM || label_chunk_info[i].type == PART_FAT
- || label_chunk_info[i].type == PART_EFI) && label_chunk_info[i].c->private_data
- && !strcmp(((PartInfo *)label_chunk_info[i].c->private_data)->mountpoint, name))
- return TRUE;
- return FALSE;
-}
-
-/* How much space is in this FreeBSD slice? */
-static daddr_t
-space_free(struct chunk *c)
-{
- struct chunk *c1;
- daddr_t sz = c->size;
-
- for (c1 = c->part; c1; c1 = c1->next) {
- if (c1->type != unused)
- sz -= c1->size;
- }
- if (sz < 0)
- msgFatal("Partitions are larger than actual chunk??");
- return sz;
-}
-
-/* Snapshot the current situation into the displayed chunks structure */
-static void
-record_label_chunks(Device *dev)
-{
- int j, p;
- struct chunk *c1, *c2;
- Disk *d = (Disk *)dev->private;
-
- j = p = 0;
- /* First buzz through and pick up the FreeBSD slices */
- if (!d->chunks)
- msgFatal("No chunk list found for %s!", d->name);
-
-#ifdef __ia64__
- label_chunk_info[j].type = PART_SLICE;
- label_chunk_info[j].c = d->chunks;
- j++;
-#endif
-
- /* Put the slice entries first */
- for (c1 = d->chunks->part; c1; c1 = c1->next) {
- if (c1->type == freebsd) {
- label_chunk_info[j].type = PART_SLICE;
- label_chunk_info[j].c = c1;
- ++j;
- }
-#ifdef __powerpc__
- if (c1->type == apple) {
- label_chunk_info[j].type = PART_SLICE;
- label_chunk_info[j].c = c1;
- ++j;
- }
-#endif
- }
-
- /* Then buzz through and pick up the partitions */
- for (c1 = d->chunks->part; c1; c1 = c1->next) {
- if (c1->type == freebsd) {
- for (c2 = c1->part; c2; c2 = c2->next) {
- if (c2->type == part) {
- if (c2->subtype == FS_SWAP)
- label_chunk_info[j].type = PART_SWAP;
- else
- label_chunk_info[j].type = PART_FILESYSTEM;
- label_chunk_info[j].c = c2;
- ++j;
- }
- }
- }
- else if (c1->type == fat) {
- label_chunk_info[j].type = PART_FAT;
- label_chunk_info[j].c = c1;
- ++j;
- }
-#ifdef __ia64__
- else if (c1->type == efi) {
- label_chunk_info[j].type = PART_EFI;
- label_chunk_info[j].c = c1;
- ++j;
- }
- else if (c1->type == part) {
- if (c1->subtype == FS_SWAP)
- label_chunk_info[j].type = PART_SWAP;
- else
- label_chunk_info[j].type = PART_FILESYSTEM;
- label_chunk_info[j].c = c1;
- ++j;
- }
-#endif
-#ifdef __powerpc__
- else if (c1->type == apple) {
- for (c2 = c1->part; c2; c2 = c2->next) {
- if (c2->type == part) {
- if (c2->subtype == FS_SWAP)
- label_chunk_info[j].type = PART_SWAP;
- else
- label_chunk_info[j].type = PART_FILESYSTEM;
- label_chunk_info[j].c = c2;
- ++j;
- }
- }
- }
-#endif
- }
- label_chunk_info[j].c = NULL;
- if (here >= j) {
- here = j ? j - 1 : 0;
- }
-}
-
-/* A new partition entry */
-static PartInfo *
-new_part(PartType type, char *mpoint, Boolean newfs)
-{
- PartInfo *pi;
-
- if (!mpoint)
- mpoint = (type == PART_EFI) ? "/efi" : "/change_me";
-
- pi = (PartInfo *)safe_malloc(sizeof(PartInfo));
- sstrncpy(pi->mountpoint, mpoint, FILENAME_MAX);
-
- pi->do_newfs = newfs;
-
- if (type == PART_EFI) {
- pi->newfs_type = NEWFS_MSDOS;
- } else {
- pi->newfs_type = NEWFS_UFS;
- strcpy(pi->newfs_data.newfs_ufs.user_options, "");
- pi->newfs_data.newfs_ufs.acls = FALSE;
- pi->newfs_data.newfs_ufs.multilabel = FALSE;
- pi->newfs_data.newfs_ufs.softupdates = strcmp(mpoint, "/");
-#ifdef PC98
- pi->newfs_data.newfs_ufs.ufs1 = TRUE;
-#else
- pi->newfs_data.newfs_ufs.ufs1 = FALSE;
-#endif
- }
-
- return pi;
-}
-
-/* Get the mountpoint for a partition and save it away */
-static PartInfo *
-get_mountpoint(PartType type, struct chunk *old)
-{
- char *val;
- PartInfo *tmp;
- Boolean newfs;
-
- if (old && old->private_data)
- tmp = old->private_data;
- else
- tmp = NULL;
- val = (tmp != NULL) ? tmp->mountpoint : (type == PART_EFI) ? "/efi" : NULL;
- val = msgGetInput(val, "Please specify a mount point for the partition");
- if (!val || !*val) {
- if (!old)
- return NULL;
- else {
- free(old->private_data);
- old->private_data = NULL;
- }
- return NULL;
- }
-
- /* Is it just the same value? */
- if (tmp && !strcmp(tmp->mountpoint, val))
- return NULL;
-
- /* Did we use it already? */
- if (check_conflict(val)) {
- msgConfirm("You already have a mount point for %s assigned!", val);
- return NULL;
- }
-
- /* Is it bogus? */
- if (*val != '/') {
- msgConfirm("Mount point must start with a / character");
- return NULL;
- }
-
- /* Is it going to be mounted on root? */
- if (!strcmp(val, "/")) {
- if (old)
- old->flags |= CHUNK_IS_ROOT;
- }
- else if (old)
- old->flags &= ~CHUNK_IS_ROOT;
-
- newfs = TRUE;
- if (tmp) {
- newfs = tmp->do_newfs;
- safe_free(tmp);
- }
- val = string_skipwhite(string_prune(val));
- tmp = new_part(type, val, newfs);
- if (old) {
- old->private_data = tmp;
- old->private_free = safe_free;
- }
- return tmp;
-}
-
-/* Get the type of the new partiton */
-static PartType
-get_partition_type(void)
-{
- int i;
- static char *fs_types[] = {
-#ifdef __ia64__
- "EFI", "An EFI system partition",
-#endif
- "FS", "A file system",
- "Swap", "A swap partition.",
- };
- WINDOW *w = savescr();
-
- dlg_clr_result();
- i = dialog_menu("Please choose a partition type",
- "If you want to use this partition for swap space, select Swap.\n"
- "If you want to put a filesystem on it, choose FS.",
- -1, -1,
-#ifdef __ia64__
- 3, 3,
-#else
- 2, 2,
-#endif
- fs_types);
- restorescr(w);
- if (!i) {
-#ifdef __ia64__
- if (!strcmp(dialog_vars.input_result, "EFI"))
- return PART_EFI;
-#endif
- if (!strcmp(dialog_vars.input_result, "FS"))
- return PART_FILESYSTEM;
- else if (!strcmp(dialog_vars.input_result, "Swap"))
- return PART_SWAP;
- }
- return PART_NONE;
-}
-
-/* If the user wants a special newfs command for this, set it */
-static void
-getNewfsCmd(PartInfo *p)
-{
- char buffer[NEWFS_CMD_ARGS_MAX];
- char *val;
-
- switch (p->newfs_type) {
- case NEWFS_UFS:
- snprintf(buffer, NEWFS_CMD_ARGS_MAX, "%s %s %s %s",
- NEWFS_UFS_CMD, p->newfs_data.newfs_ufs.softupdates ? "-U" : "",
- p->newfs_data.newfs_ufs.ufs1 ? "-O1" : "-O2",
- p->newfs_data.newfs_ufs.user_options);
- break;
- case NEWFS_MSDOS:
- snprintf(buffer, NEWFS_CMD_ARGS_MAX, "%s", NEWFS_MSDOS_CMD);
- break;
- case NEWFS_CUSTOM:
- strcpy(buffer, p->newfs_data.newfs_custom.command);
- break;
- }
-
- val = msgGetInput(buffer,
- "Please enter the newfs command and options you'd like to use in\n"
- "creating this file system.");
- if (val != NULL) {
- p->newfs_type = NEWFS_CUSTOM;
- strlcpy(p->newfs_data.newfs_custom.command, val, NEWFS_CMD_ARGS_MAX);
- }
-}
-
-static void
-getNewfsOptionalArguments(PartInfo *p)
-{
- char buffer[NEWFS_CMD_ARGS_MAX];
- char *val;
-
- /* Must be UFS, per argument checking in I/O routines. */
-
- strlcpy(buffer, p->newfs_data.newfs_ufs.user_options,
- NEWFS_CMD_ARGS_MAX);
- val = msgGetInput(buffer,
- "Please enter any additional UFS newfs options you'd like to\n"
- "use in creating this file system.");
- if (val != NULL)
- strlcpy(p->newfs_data.newfs_ufs.user_options, val,
- NEWFS_CMD_ARGS_MAX);
-}
-
-#define MAX_MOUNT_NAME 9
-
-#define PART_PART_COL 0
-#define PART_MOUNT_COL 10
-#define PART_SIZE_COL (PART_MOUNT_COL + MAX_MOUNT_NAME + 3)
-#define PART_NEWFS_COL (PART_SIZE_COL + 8)
-#define PART_OFF 38
-
-#define TOTAL_AVAIL_LINES (10)
-#define PSLICE_SHOWABLE (4)
-
-
-/* stick this all up on the screen */
-static void
-print_label_chunks(void)
-{
- int i, j, srow, prow, pcol;
- daddr_t sz;
- char clrmsg[80];
- int ChunkPartStartRow;
- WINDOW *ChunkWin;
-
- /*********************************************************/
- /*** These values are for controlling screen resources ***/
- /*** Each label line holds up to 2 labels, so beware! ***/
- /*** strategy will be to try to always make sure the ***/
- /*** highlighted label is in the active display area. ***/
- /*********************************************************/
- int pslice_max, label_max;
- int pslice_count, label_count, label_focus_found, pslice_focus_found;
-
- attrset(A_REVERSE);
- mvaddstr(0, 25, "FreeBSD Disklabel Editor");
- attrset(A_NORMAL);
-
- /*** Count the number of partition slices ***/
- pslice_count = 0;
- for (i = 0; label_chunk_info[i].c ; i++) {
- if (label_chunk_info[i].type == PART_SLICE)
- ++pslice_count;
- }
- pslice_max = pslice_count;
-
- /*** 4 line max for partition slices ***/
- if (pslice_max > PSLICE_SHOWABLE) {
- pslice_max = PSLICE_SHOWABLE;
- }
- ChunkPartStartRow = CHUNK_SLICE_START_ROW + 3 + pslice_max;
-
- /*** View partition slices modulo pslice_max ***/
- label_max = TOTAL_AVAIL_LINES - pslice_max;
-
- for (i = 0; i < 2; i++) {
- mvaddstr(ChunkPartStartRow - 2, PART_PART_COL + (i * PART_OFF), "Part");
- mvaddstr(ChunkPartStartRow - 1, PART_PART_COL + (i * PART_OFF), "----");
-
- mvaddstr(ChunkPartStartRow - 2, PART_MOUNT_COL + (i * PART_OFF), "Mount");
- mvaddstr(ChunkPartStartRow - 1, PART_MOUNT_COL + (i * PART_OFF), "-----");
-
- mvaddstr(ChunkPartStartRow - 2, PART_SIZE_COL + (i * PART_OFF) + 3, "Size");
- mvaddstr(ChunkPartStartRow - 1, PART_SIZE_COL + (i * PART_OFF) + 3, "----");
-
- mvaddstr(ChunkPartStartRow - 2, PART_NEWFS_COL + (i * PART_OFF), "Newfs");
- mvaddstr(ChunkPartStartRow - 1, PART_NEWFS_COL + (i * PART_OFF), "-----");
- }
- srow = CHUNK_SLICE_START_ROW;
- prow = 0;
- pcol = 0;
-
- /*** these variables indicate that the focused item is shown currently ***/
- label_focus_found = 0;
- pslice_focus_found = 0;
-
- label_count = 0;
- pslice_count = 0;
- mvprintw(CHUNK_SLICE_START_ROW - 1, 0, " ");
- mvprintw(CHUNK_SLICE_START_ROW + pslice_max, 0, " ");
-
- ChunkWin = newwin(CHUNK_ROW_MAX - ChunkPartStartRow, 76, ChunkPartStartRow, 0);
-
- wclear(ChunkWin);
- /*** wrefresh(ChunkWin); ***/
-
- for (i = 0; label_chunk_info[i].c; i++) {
- /* Is it a slice entry displayed at the top? */
- if (label_chunk_info[i].type == PART_SLICE) {
- /*** This causes the new pslice to replace the previous display ***/
- /*** focus must remain on the most recently active pslice ***/
- if (pslice_count == pslice_max) {
- if (pslice_focus_found) {
- /*** This is where we can mark the more following ***/
- attrset(A_BOLD);
- mvprintw(CHUNK_SLICE_START_ROW + pslice_max, 0, "***MORE***");
- attrset(A_NORMAL);
- continue;
- }
- else {
- /*** this is where we set the more previous ***/
- attrset(A_BOLD);
- mvprintw(CHUNK_SLICE_START_ROW - 1, 0, "***MORE***");
- attrset(A_NORMAL);
- pslice_count = 0;
- srow = CHUNK_SLICE_START_ROW;
- }
- }
-
- sz = space_free(label_chunk_info[i].c);
- if (i == here)
- attrset(ATTR_SELECTED);
- if (i == pslice_focus)
- pslice_focus_found = -1;
-
- if (label_chunk_info[i].c->type == whole) {
- if (sz >= 100 * ONE_GIG)
- mvprintw(srow++, 0,
- "Disk: %s\t\tFree: %jd blocks (%jdGB)",
- label_chunk_info[i].c->disk->name, (intmax_t)sz,
- (intmax_t)(sz / ONE_GIG));
- else
- mvprintw(srow++, 0,
- "Disk: %s\t\tFree: %jd blocks (%jdMB)",
- label_chunk_info[i].c->disk->name, (intmax_t)sz,
- (intmax_t)(sz / ONE_MEG));
- } else {
- if (sz >= 100 * ONE_GIG)
- mvprintw(srow++, 0,
- "Disk: %s\tPartition name: %s\tFree: %jd blocks (%jdGB)",
- label_chunk_info[i].c->disk->name,
- label_chunk_info[i].c->name,
- (intmax_t)sz, (intmax_t)(sz / ONE_GIG));
- else
- mvprintw(srow++, 0,
- "Disk: %s\tPartition name: %s\tFree: %jd blocks (%jdMB)",
- label_chunk_info[i].c->disk->name,
- label_chunk_info[i].c->name,
- (intmax_t)sz, (intmax_t)(sz / ONE_MEG));
- }
- attrset(A_NORMAL);
- clrtoeol();
- move(0, 0);
- /*** refresh(); ***/
- ++pslice_count;
- }
- /* Otherwise it's a DOS, swap or filesystem entry in the Chunk window */
- else {
- char onestr[PART_OFF], num[10], *mountpoint, newfs[12];
-
- /*
- * We copy this into a blank-padded string so that it looks like
- * a solid bar in reverse-video
- */
- memset(onestr, ' ', PART_OFF - 1);
- onestr[PART_OFF - 1] = '\0';
-
- /*** Track how many labels have been displayed ***/
- if (label_count == ((label_max - 1 ) * 2)) {
- if (label_focus_found) {
- continue;
- }
- else {
- label_count = 0;
- prow = 0;
- pcol = 0;
- }
- }
-
- /* Go for two columns if we've written one full columns worth */
- /*** if (prow == (CHUNK_ROW_MAX - ChunkPartStartRow)) ***/
- if (label_count == label_max - 1) {
- pcol = PART_OFF;
- prow = 0;
- }
- memcpy(onestr + PART_PART_COL, label_chunk_info[i].c->name, strlen(label_chunk_info[i].c->name));
- /* If it's a filesystem, display the mountpoint */
- if (label_chunk_info[i].c->private_data && (label_chunk_info[i].type == PART_FILESYSTEM
- || label_chunk_info[i].type == PART_FAT || label_chunk_info[i].type == PART_EFI))
- mountpoint = ((PartInfo *)label_chunk_info[i].c->private_data)->mountpoint;
- else if (label_chunk_info[i].type == PART_SWAP)
- mountpoint = "swap";
- else
- mountpoint = "<none>";
-
- /* Now display the newfs field */
- if (label_chunk_info[i].type == PART_FAT)
- strcpy(newfs, "DOS");
-#if defined(__ia64__)
- else if (label_chunk_info[i].type == PART_EFI) {
- strcpy(newfs, "EFI");
- if (label_chunk_info[i].c->private_data) {
- strcat(newfs, " ");
- PartInfo *pi = (PartInfo *)label_chunk_info[i].c->private_data;
- strcat(newfs, pi->do_newfs ? " Y" : " N");
- }
- }
-#endif
- else if (label_chunk_info[i].c->private_data && label_chunk_info[i].type == PART_FILESYSTEM) {
- PartInfo *pi = (PartInfo *)label_chunk_info[i].c->private_data;
-
- switch (pi->newfs_type) {
- case NEWFS_UFS:
- strcpy(newfs, NEWFS_UFS_STRING);
- if (pi->newfs_data.newfs_ufs.ufs1)
- strcat(newfs, "1");
- else
- strcat(newfs, "2");
- if (pi->newfs_data.newfs_ufs.softupdates)
- strcat(newfs, "+S");
- else
- strcat(newfs, " ");
-
- break;
- case NEWFS_MSDOS:
- strcpy(newfs, "FAT");
- break;
- case NEWFS_CUSTOM:
- strcpy(newfs, "CUST");
- break;
- }
- strcat(newfs, pi->do_newfs ? " Y" : " N ");
- }
- else if (label_chunk_info[i].type == PART_SWAP)
- strcpy(newfs, "SWAP");
- else
- strcpy(newfs, "*");
- for (j = 0; j < MAX_MOUNT_NAME && mountpoint[j]; j++)
- onestr[PART_MOUNT_COL + j] = mountpoint[j];
- if (label_chunk_info[i].c->size == 0)
- snprintf(num, 10, "%5dMB", 0);
- else if (label_chunk_info[i].c->size < (100 * ONE_GIG))
- snprintf(num, 10, "%5jdMB",
- (intmax_t)label_chunk_info[i].c->size / ONE_MEG);
- else
- snprintf(num, 10, "%5jdGB",
- (intmax_t)label_chunk_info[i].c->size / ONE_GIG);
- memcpy(onestr + PART_SIZE_COL, num, strlen(num));
- memcpy(onestr + PART_NEWFS_COL, newfs, strlen(newfs));
- onestr[PART_NEWFS_COL + strlen(newfs)] = '\0';
- if (i == label_focus) {
- label_focus_found = -1;
- wattrset(ChunkWin, A_BOLD);
- }
- if (i == here)
- wattrset(ChunkWin, ATTR_SELECTED);
-
- /*** lazy man's way of expensively padding this string ***/
- while (strlen(onestr) < 37)
- strcat(onestr, " ");
-
- mvwaddstr(ChunkWin, prow, pcol, onestr);
- wattrset(ChunkWin, A_NORMAL);
- move(0, 0);
- ++prow;
- ++label_count;
- }
- }
-
- /*** this will erase all the extra stuff ***/
- memset(clrmsg, ' ', 37);
- clrmsg[37] = '\0';
-
- while (pslice_count < pslice_max) {
- mvprintw(srow++, 0, clrmsg);
- clrtoeol();
- ++pslice_count;
- }
- while (label_count < (2 * (label_max - 1))) {
- mvwaddstr(ChunkWin, prow++, pcol, clrmsg);
- ++label_count;
- if (prow == (label_max - 1)) {
- prow = 0;
- pcol = PART_OFF;
- }
- }
- refresh();
- wrefresh(ChunkWin);
-}
-
-static void
-print_command_summary(void)
-{
- mvprintw(17, 0, "The following commands are valid here (upper or lower case):");
- mvprintw(18, 0, "C = Create D = Delete M = Mount pt.");
- mvprintw(18, 56, "W = Write");
- mvprintw(19, 0, "N = Newfs Opts Q = Finish S = Toggle SoftUpdates Z = Custom Newfs");
- mvprintw(20, 0, "T = Toggle Newfs U = Undo A = Auto Defaults R = Delete+Merge");
- mvprintw(22, 0, "Use F1 or ? to get more help, arrow keys to select.");
- move(0, 0);
-}
-
-static void
-clear_wins(void)
-{
-
- clear();
- print_label_chunks();
-}
-
-static int
-diskLabel(Device *dev)
-{
- daddr_t sz;
- int key = 0;
- Boolean labeling;
- char *msg = NULL;
- PartInfo *p, *oldp;
- PartType type;
- WINDOW *w = savescr();
-
- label_focus = 0;
- pslice_focus = 0;
- here = 0;
-
- labeling = TRUE;
- keypad(stdscr, TRUE);
- record_label_chunks(dev);
-
- clear();
- while (labeling) {
- char *cp;
- int rflags = DELCHUNK_NORMAL;
-
- print_label_chunks();
- print_command_summary();
- if (msg) {
- attrset(title_attr); mvprintw(23, 0, msg); attrset(A_NORMAL);
- clrtoeol();
- beep();
- msg = NULL;
- }
- else {
- move(23, 0);
- clrtoeol();
- }
-
- refresh();
- key = getch();
- switch (toupper(key)) {
- static char _msg[40];
-
- case '\014': /* ^L */
- clear_wins();
- break;
-
- case '\020': /* ^P */
- case KEY_UP:
- case '-':
- if (here != 0)
- --here;
- else
- while (label_chunk_info[here + 1].c)
- ++here;
- break;
-
- case '\016': /* ^N */
- case KEY_DOWN:
- case '+':
- case '\r':
- case '\n':
- if (label_chunk_info[here + 1].c)
- ++here;
- else
- here = 0;
- break;
-
- case KEY_HOME:
- here = 0;
- break;
-
- case KEY_END:
- while (label_chunk_info[here + 1].c)
- ++here;
- break;
-
- case KEY_F(1):
- case '?':
- systemDisplayHelp("partition");
- clear_wins();
- break;
-
- case '1':
- if (label_chunk_info[here].type == PART_FILESYSTEM) {
- PartInfo *pi =
- ((PartInfo *)label_chunk_info[here].c->private_data);
-
- if ((pi != NULL) &&
- (pi->newfs_type == NEWFS_UFS)) {
- pi->newfs_data.newfs_ufs.ufs1 = true;
- } else
- msg = MSG_NOT_APPLICABLE;
- } else
- msg = MSG_NOT_APPLICABLE;
- break;
- break;
-
- case '2':
- if (label_chunk_info[here].type == PART_FILESYSTEM) {
- PartInfo *pi =
- ((PartInfo *)label_chunk_info[here].c->private_data);
-
- if ((pi != NULL) &&
- (pi->newfs_type == NEWFS_UFS)) {
- pi->newfs_data.newfs_ufs.ufs1 = false;
- } else
- msg = MSG_NOT_APPLICABLE;
- } else
- msg = MSG_NOT_APPLICABLE;
- break;
- break;
-
- case 'A':
- if (label_chunk_info[here].type != PART_SLICE) {
- msg = "You can only do this in a disk slice (at top of screen)";
- break;
- }
- /*
- * Generate standard partitions automatically. If we do not
- * have sufficient space we attempt to scale-down the size
- * of the partitions within certain bounds.
- */
- {
- int perc;
- int req = 0;
-
- for (perc = 100; perc > 0; perc -= 5) {
- req = 0; /* reset for each loop */
- if ((msg = try_auto_label(dev, perc, &req)) == NULL)
- break;
- }
- if (msg) {
- if (req) {
- msgConfirm("%s", msg);
- clear_wins();
- msg = NULL;
- }
- }
- }
- break;
-
- case 'C':
- if (label_chunk_info[here].type != PART_SLICE) {
- msg = "You can only do this in a master partition (see top of screen)";
- break;
- }
- sz = space_free(label_chunk_info[here].c);
- if (sz <= FS_MIN_SIZE) {
- msg = "Not enough space to create an additional FreeBSD partition";
- break;
- }
- else {
- char *val;
- daddr_t size;
- struct chunk *tmp;
- char osize[80];
- u_long flags = 0;
-
-#ifdef __powerpc__
- /* Always use the maximum size for apple partitions */
- if (label_chunk_info[here].c->type == apple)
- size = sz;
- else {
-#endif
- sprintf(osize, "%jd", (intmax_t)sz);
- val = msgGetInput(osize,
- "Please specify the partition size in blocks or append a trailing G for\n"
-#ifdef __ia64__
- "gigabytes, M for megabytes.\n"
-#else
- "gigabytes, M for megabytes, or C for cylinders.\n"
-#endif
- "%jd blocks (%jdMB) are free.",
- (intmax_t)sz, (intmax_t)sz / ONE_MEG);
- if (!val || (size = strtoimax(val, &cp, 0)) <= 0) {
- clear_wins();
- break;
- }
-
- if (*cp) {
- if (toupper(*cp) == 'M')
- size *= ONE_MEG;
- else if (toupper(*cp) == 'G')
- size *= ONE_GIG;
-#ifndef __ia64__
- else if (toupper(*cp) == 'C')
- size *= (label_chunk_info[here].c->disk->bios_hd * label_chunk_info[here].c->disk->bios_sect);
-#endif
- }
- if (size <= FS_MIN_SIZE) {
- msgConfirm("The minimum filesystem size is %dMB", FS_MIN_SIZE / ONE_MEG);
- clear_wins();
- break;
- }
-#ifdef __powerpc__
- }
-#endif
- type = get_partition_type();
- if (type == PART_NONE) {
- clear_wins();
- beep();
- break;
- }
-
- if (type == PART_FILESYSTEM || type == PART_EFI) {
- if ((p = get_mountpoint(type, NULL)) == NULL) {
- clear_wins();
- beep();
- break;
- }
- else if (!strcmp(p->mountpoint, "/")) {
- if (type != PART_FILESYSTEM) {
- clear_wins();
- beep();
- break;
- }
- else
- flags |= CHUNK_IS_ROOT;
- }
- else
- flags &= ~CHUNK_IS_ROOT;
- }
- else
- p = NULL;
-
- if ((flags & CHUNK_IS_ROOT) && (size < (ROOT_MIN_SIZE * ONE_MEG))) {
- msgConfirm("Warning: This is smaller than the recommended size for a\n"
- "root partition. For a variety of reasons, root\n"
- "partitions should usually be at least %dMB in size", ROOT_MIN_SIZE);
- }
- tmp = Create_Chunk_DWIM(label_chunk_info[here].c->disk,
- label_chunk_info[here].c, size,
-#ifdef __ia64__
- (type == PART_EFI) ? efi : part,
- (type == PART_EFI) ? 0 : (type == PART_SWAP) ? FS_SWAP : FS_BSDFFS,
-#else
- part, (type == PART_SWAP) ? FS_SWAP : FS_BSDFFS,
-#endif
- flags);
- if (!tmp) {
- msgConfirm("Unable to create the partition. Too big?");
- clear_wins();
- break;
- }
-
-#ifdef __alpha__
- /*
- * SRM requires that the root partition is at the
- * beginning of the disk and cannot boot otherwise.
- * Warn Alpha users if they are about to shoot themselves in
- * the foot in this way.
- *
- * Since partitions may not start precisely at offset 0 we
- * check for a "close to 0" instead. :-(
- */
- if ((flags & CHUNK_IS_ROOT) && (tmp->offset > 1024)) {
- msgConfirm("Your root partition `a' does not seem to be the first\n"
- "partition. The Alpha's firmware can only boot from the\n"
- "first partition. So it is unlikely that your current\n"
- "disk layout will be bootable boot after installation.\n"
- "\n"
- "Please allocate the root partition before allocating\n"
- "any others.\n");
- }
-#endif /* alpha */
-
- tmp->private_data = p;
- tmp->private_free = safe_free;
- if (variable_cmp(DISK_LABELLED, "written"))
- variable_set2(DISK_LABELLED, "yes", 0);
- record_label_chunks(dev);
- clear_wins();
- /* This is where we assign focus to new label so it shows. */
- {
- int i;
- label_focus = -1;
- for (i = 0; label_chunk_info[i].c; ++i) {
- if (label_chunk_info[i].c == tmp) {
- label_focus = i;
- break;
- }
- }
- if (label_focus == -1)
- label_focus = i - 1;
- }
- }
- break;
-
- case KEY_DC:
- case 'R': /* recover space (delete w/ recover) */
- /*
- * Delete the partition w/ space recovery.
- */
- rflags = DELCHUNK_RECOVER;
- /* fall through */
- case 'D': /* delete */
- if (label_chunk_info[here].type == PART_SLICE) {
- msg = MSG_NOT_APPLICABLE;
- break;
- }
- else if (label_chunk_info[here].type == PART_FAT) {
- msg = "Use the Disk Partition Editor to delete DOS partitions";
- break;
- }
- Delete_Chunk2(label_chunk_info[here].c->disk, label_chunk_info[here].c, rflags);
- if (variable_cmp(DISK_LABELLED, "written"))
- variable_set2(DISK_LABELLED, "yes", 0);
- record_label_chunks(dev);
- break;
-
- case 'M': /* mount */
- switch(label_chunk_info[here].type) {
- case PART_SLICE:
- msg = MSG_NOT_APPLICABLE;
- break;
-
- case PART_SWAP:
- msg = "You don't need to specify a mountpoint for a swap partition.";
- break;
-
- case PART_FAT:
- case PART_EFI:
- case PART_FILESYSTEM:
- oldp = label_chunk_info[here].c->private_data;
- p = get_mountpoint(label_chunk_info[here].type, label_chunk_info[here].c);
- if (p) {
- if (!oldp)
- p->do_newfs = FALSE;
- if ((label_chunk_info[here].type == PART_FAT ||
- label_chunk_info[here].type == PART_EFI) &&
- (!strcmp(p->mountpoint, "/") ||
- !strcmp(p->mountpoint, "/usr") ||
- !strcmp(p->mountpoint, "/var"))) {
- msgConfirm("%s is an invalid mount point for a DOS partition!", p->mountpoint);
- strcpy(p->mountpoint, "/bogus");
- }
- }
- if (variable_cmp(DISK_LABELLED, "written"))
- variable_set2(DISK_LABELLED, "yes", 0);
- record_label_chunks(dev);
- clear_wins();
- break;
-
- default:
- msgFatal("Bogus partition under cursor???");
- break;
- }
- break;
-
- case 'N': /* Set newfs options */
- if (label_chunk_info[here].c->private_data &&
- ((PartInfo *)label_chunk_info[here].c->private_data)->do_newfs)
- getNewfsOptionalArguments(
- label_chunk_info[here].c->private_data);
- else
- msg = MSG_NOT_APPLICABLE;
- clear_wins();
- break;
-
- case 'S': /* Toggle soft updates flag */
- if (label_chunk_info[here].type == PART_FILESYSTEM) {
- PartInfo *pi = ((PartInfo *)label_chunk_info[here].c->private_data);
- if (pi != NULL &&
- pi->newfs_type == NEWFS_UFS)
- pi->newfs_data.newfs_ufs.softupdates =
- !pi->newfs_data.newfs_ufs.softupdates;
- else
- msg = MSG_NOT_APPLICABLE;
- }
- else
- msg = MSG_NOT_APPLICABLE;
- break;
-
- case 'T': /* Toggle newfs state */
- if ((label_chunk_info[here].type == PART_FILESYSTEM ||
- label_chunk_info[here].type == PART_EFI) &&
- (label_chunk_info[here].c->private_data)) {
- PartInfo *pi = ((PartInfo *)label_chunk_info[here].c->private_data);
- if (!pi->do_newfs)
- label_chunk_info[here].c->flags |= CHUNK_NEWFS;
- else
- label_chunk_info[here].c->flags &= ~CHUNK_NEWFS;
-
- label_chunk_info[here].c->private_data =
- new_part(label_chunk_info[here].type, pi ? pi->mountpoint : NULL, pi ? !pi->do_newfs
- : TRUE);
- if (pi != NULL &&
- pi->newfs_type == NEWFS_UFS) {
- PartInfo *pi_new = label_chunk_info[here].c->private_data;
-
- pi_new->newfs_data.newfs_ufs = pi->newfs_data.newfs_ufs;
- }
- safe_free(pi);
- label_chunk_info[here].c->private_free = safe_free;
- if (variable_cmp(DISK_LABELLED, "written"))
- variable_set2(DISK_LABELLED, "yes", 0);
- }
- else
- msg = MSG_NOT_APPLICABLE;
- break;
-
- case 'U':
- clear();
- if (!variable_cmp(DISK_LABELLED, "written")) {
- msgConfirm("You've already written out your changes -\n"
- "it's too late to undo!");
- }
- else if (!msgNoYes("Are you SURE you want to Undo everything?")) {
- Disk *d;
-
- variable_unset(DISK_PARTITIONED);
- variable_unset(DISK_LABELLED);
- if ((d = Open_Disk(dev->name)) != NULL) {
- Free_Disk(dev->private);
- dev->private = d;
-#ifdef WITH_SLICES
- diskPartition(dev);
-#endif
- }
- record_label_chunks(dev);
- }
- clear_wins();
- break;
-
- case 'W':
- if (!variable_cmp(DISK_LABELLED, "written")) {
- msgConfirm("You've already written out your changes - if you\n"
- "wish to overwrite them, you'll have to restart\n"
- "%s first.", ProgName);
- }
- else if (!msgNoYes("WARNING: You are about to modify an EXISTING\n"
- "installation.\n\n"
- "Are you absolutely sure you want to continue?")) {
- variable_set2(DISK_LABELLED, "yes", 0);
- diskLabelCommit(dev);
- }
- clear_wins();
- break;
-
- case 'Z': /* Set newfs command line */
- if (label_chunk_info[here].c->private_data &&
- ((PartInfo *)label_chunk_info[here].c->private_data)->do_newfs)
- getNewfsCmd(label_chunk_info[here].c->private_data);
- else
- msg = MSG_NOT_APPLICABLE;
- clear_wins();
- break;
-
-#ifndef __ia64__
- case '|':
- if (!msgNoYes("Are you sure you want to go into Wizard mode?\n\n"
- "This is an entirely undocumented feature which you are not\n"
- "expected to understand!")) {
- dlg_clear();
- end_dialog();
- DialogActive = FALSE;
- if (dev->private) {
- slice_wizard(((Disk *)dev->private));
- }
- if (variable_cmp(DISK_LABELLED, "written"))
- variable_set2(DISK_LABELLED, "yes", 0);
- DialogActive = TRUE;
- record_label_chunks(dev);
- clear_wins();
- }
- else
- msg = "A most prudent choice!";
- break;
-#endif
-
- case '\033': /* ESC */
- case 'Q':
- labeling = FALSE;
- break;
-
- default:
- beep();
- sprintf(_msg, "Invalid key %d - Type F1 or ? for help", key);
- msg = _msg;
- break;
- }
- if (label_chunk_info[here].type == PART_SLICE)
- pslice_focus = here;
- else
- label_focus = here;
- }
- restorescr(w);
- return DITEM_SUCCESS;
-}
-
-static __inline daddr_t
-requested_part_size(char *varName, daddr_t nom, int def, int perc)
-{
- char *cp;
- daddr_t sz;
-
- if ((cp = variable_get(varName)) != NULL)
- sz = strtoimax(cp, NULL, 0);
- else
- sz = nom + (def - nom) * perc / 100;
- return(sz * ONE_MEG);
-}
-
-/*
- * Attempt to auto-label the disk. 'perc' (0-100) scales
- * the size of the various partitions within appropriate
- * bounds (NOMINAL through DEFAULT sizes). The procedure
- * succeeds of NULL is returned. A non-null return message
- * is either a failure-status message (*req == 0), or
- * a confirmation requestor (*req == 1). *req is 0 on
- * entry to this call.
- *
- * As a special exception to the usual sizing rules, /var is given
- * additional space equal to the amount of physical memory present
- * if perc == 100 in order to ensure that users with large hard drives
- * will have enough space to store a crashdump in /var/crash.
- *
- * We autolabel the following partitions: /, swap, /var, /tmp, /usr,
- * and /home. /home receives any extra left over disk space.
- */
-static char *
-try_auto_label(Device *dev, int perc, int *req)
-{
- daddr_t sz;
- Chunk *AutoHome, *AutoRoot, *AutoSwap;
- Chunk *AutoTmp, *AutoUsr, *AutoVar;
-#ifdef __ia64__
- Chunk *AutoEfi;
-#endif
- int mib[2];
- unsigned long physmem;
- size_t size;
- char *msg = NULL;
-
- sz = space_free(label_chunk_info[here].c);
- if (sz <= FS_MIN_SIZE)
- return("Not enough free space to create a new partition in the slice");
-
- (void)checkLabels(FALSE);
- AutoHome = AutoRoot = AutoSwap = NULL;
- AutoTmp = AutoUsr = AutoVar = NULL;
-
-#ifdef __ia64__
- AutoEfi = NULL;
- if (EfiChunk == NULL) {
- sz = 100 * ONE_MEG;
- AutoEfi = Create_Chunk_DWIM(label_chunk_info[here].c->disk,
- label_chunk_info[here].c, sz, efi, 0, 0);
- if (AutoEfi == NULL) {
- *req = 1;
- msg = "Unable to create the EFI system partition. Too big?";
- goto done;
- }
- AutoEfi->private_data = new_part(PART_EFI, "/efi", TRUE);
- AutoEfi->private_free = safe_free;
- AutoEfi->flags |= CHUNK_NEWFS;
- record_label_chunks(dev);
- }
-#endif
-
- if (RootChunk == NULL) {
- sz = requested_part_size(VAR_ROOT_SIZE, ROOT_NOMINAL_SIZE, ROOT_DEFAULT_SIZE, perc);
-
- AutoRoot = Create_Chunk_DWIM(label_chunk_info[here].c->disk,
- label_chunk_info[here].c, sz, part,
- FS_BSDFFS, CHUNK_IS_ROOT | CHUNK_AUTO_SIZE);
- if (!AutoRoot) {
- *req = 1;
- msg = "Unable to create the root partition. Too big?";
- goto done;
- }
- AutoRoot->private_data = new_part(PART_FILESYSTEM, "/", TRUE);
- AutoRoot->private_free = safe_free;
- AutoRoot->flags |= CHUNK_NEWFS;
- record_label_chunks(dev);
- }
- if (SwapChunk == NULL) {
- sz = requested_part_size(VAR_SWAP_SIZE, 0, 0, perc);
- if (sz == 0) {
- daddr_t nom;
- daddr_t def;
-
- mib[0] = CTL_HW;
- mib[1] = HW_PHYSMEM;
- size = sizeof physmem;
- sysctl(mib, 2, &physmem, &size, (void *)0, (size_t)0);
- def = 2 * (int)(physmem / 512);
- if (def < SWAP_MIN_SIZE * ONE_MEG)
- def = SWAP_MIN_SIZE * ONE_MEG;
- if (def > SWAP_AUTO_LIMIT_SIZE * ONE_MEG)
- def = SWAP_AUTO_LIMIT_SIZE * ONE_MEG;
- nom = (int)(physmem / 512) / 8;
- sz = nom + (def - nom) * perc / 100;
- }
- AutoSwap = Create_Chunk_DWIM(label_chunk_info[here].c->disk,
- label_chunk_info[here].c, sz, part,
- FS_SWAP, CHUNK_AUTO_SIZE);
- if (!AutoSwap) {
- *req = 1;
- msg = "Unable to create the swap partition. Too big?";
- goto done;
- }
- AutoSwap->private_data = 0;
- AutoSwap->private_free = safe_free;
- record_label_chunks(dev);
- }
- if (VarChunk == NULL) {
- /* Work out how much extra space we want for a crash dump */
- unsigned long crashdumpsz;
-
- mib[0] = CTL_HW;
- mib[1] = HW_PHYSMEM;
- size = sizeof(physmem);
- sysctl(mib, 2, &physmem, &size, (void *)0, (size_t)0);
-
- if (perc == 100)
- crashdumpsz = physmem / 1048576;
- else
- crashdumpsz = 0;
-
- sz = requested_part_size(VAR_VAR_SIZE, VAR_NOMINAL_SIZE, \
- VAR_DEFAULT_SIZE + crashdumpsz, perc);
-
- AutoVar = Create_Chunk_DWIM(label_chunk_info[here].c->disk,
- label_chunk_info[here].c, sz, part,
- FS_BSDFFS, CHUNK_AUTO_SIZE);
- if (!AutoVar) {
- *req = 1;
- msg = "Not enough free space for /var - you will need to\n"
- "partition your disk manually with a custom install!";
- goto done;
- }
- AutoVar->private_data = new_part(PART_FILESYSTEM, "/var", TRUE);
- AutoVar->private_free = safe_free;
- AutoVar->flags |= CHUNK_NEWFS;
- record_label_chunks(dev);
- }
- if (TmpChunk == NULL && !variable_get(VAR_NO_TMP)) {
- sz = requested_part_size(VAR_TMP_SIZE, TMP_NOMINAL_SIZE, TMP_DEFAULT_SIZE, perc);
-
- AutoTmp = Create_Chunk_DWIM(label_chunk_info[here].c->disk,
- label_chunk_info[here].c, sz, part,
- FS_BSDFFS, CHUNK_AUTO_SIZE);
- if (!AutoTmp) {
- *req = 1;
- msg = "Not enough free space for /tmp - you will need to\n"
- "partition your disk manually with a custom install!";
- goto done;
- }
- AutoTmp->private_data = new_part(PART_FILESYSTEM, "/tmp", TRUE);
- AutoTmp->private_free = safe_free;
- AutoTmp->flags |= CHUNK_NEWFS;
- record_label_chunks(dev);
- }
- if (UsrChunk == NULL && !variable_get(VAR_NO_USR)) {
- sz = requested_part_size(VAR_USR_SIZE, USR_NOMINAL_SIZE, USR_DEFAULT_SIZE, perc);
-#if AUTO_HOME == 0
- if (sz < space_free(label_chunk_info[here].c))
- sz = space_free(label_chunk_info[here].c);
-#endif
- if (sz) {
- if (sz < (USR_MIN_SIZE * ONE_MEG)) {
- *req = 1;
- msg = "Not enough free space for /usr - you will need to\n"
- "partition your disk manually with a custom install!";
- }
-
- AutoUsr = Create_Chunk_DWIM(label_chunk_info[here].c->disk,
- label_chunk_info[here].c, sz, part,
- FS_BSDFFS, CHUNK_AUTO_SIZE);
- if (!AutoUsr) {
- msg = "Unable to create the /usr partition. Not enough space?\n"
- "You will need to partition your disk manually with a custom install!";
- goto done;
- }
- AutoUsr->private_data = new_part(PART_FILESYSTEM, "/usr", TRUE);
- AutoUsr->private_free = safe_free;
- AutoUsr->flags |= CHUNK_NEWFS;
- record_label_chunks(dev);
- }
- }
-#if AUTO_HOME == 1
- if (HomeChunk == NULL && !variable_get(VAR_NO_HOME)) {
- sz = requested_part_size(VAR_HOME_SIZE, HOME_NOMINAL_SIZE, HOME_DEFAULT_SIZE, perc);
- if (sz < space_free(label_chunk_info[here].c))
- sz = space_free(label_chunk_info[here].c);
- if (sz) {
- if (sz < (HOME_MIN_SIZE * ONE_MEG)) {
- *req = 1;
- msg = "Not enough free space for /home - you will need to\n"
- "partition your disk manually with a custom install!";
- goto done;
- }
-
- AutoHome = Create_Chunk_DWIM(label_chunk_info[here].c->disk,
- label_chunk_info[here].c, sz, part,
- FS_BSDFFS, CHUNK_AUTO_SIZE);
- if (!AutoHome) {
- msg = "Unable to create the /home partition. Not enough space?\n"
- "You will need to partition your disk manually with a custom install!";
- goto done;
- }
- AutoHome->private_data = new_part(PART_FILESYSTEM, "/home", TRUE);
- AutoHome->private_free = safe_free;
- AutoHome->flags |= CHUNK_NEWFS;
- record_label_chunks(dev);
- }
- }
-#endif
-
- /* At this point, we're reasonably "labelled" */
- if (variable_cmp(DISK_LABELLED, "written"))
- variable_set2(DISK_LABELLED, "yes", 0);
-
-done:
- if (msg) {
- if (AutoRoot != NULL)
- Delete_Chunk(AutoRoot->disk, AutoRoot);
- if (AutoSwap != NULL)
- Delete_Chunk(AutoSwap->disk, AutoSwap);
- if (AutoVar != NULL)
- Delete_Chunk(AutoVar->disk, AutoVar);
- if (AutoTmp != NULL)
- Delete_Chunk(AutoTmp->disk, AutoTmp);
- if (AutoUsr != NULL)
- Delete_Chunk(AutoUsr->disk, AutoUsr);
- if (AutoHome != NULL)
- Delete_Chunk(AutoHome->disk, AutoHome);
- record_label_chunks(dev);
- }
- return(msg);
-}
-
-static int
-diskLabelNonInteractive(Device *dev)
-{
- char *cp;
- PartType type;
- PartInfo *p;
- u_long flags;
- int i, status;
- Device **devs;
- Disk *d;
-
- status = DITEM_SUCCESS;
- cp = variable_get(VAR_DISK);
- if (!cp) {
- msgConfirm("diskLabel: No disk selected - can't label automatically.");
- return DITEM_FAILURE;
- }
- devs = deviceFind(cp, DEVICE_TYPE_DISK);
- if (!devs) {
- msgConfirm("diskLabel: No disk device %s found!", cp);
- return DITEM_FAILURE;
- }
- if (dev)
- d = dev->private;
- else
- d = devs[0]->private;
- record_label_chunks(dev);
- for (i = 0; label_chunk_info[i].c; i++) {
- Chunk *c1 = label_chunk_info[i].c;
-
- if (label_chunk_info[i].type == PART_SLICE) {
- char name[512];
- char typ[10], mpoint[50];
- int entries;
-
- for (entries = 1;; entries++) {
- intmax_t sz;
- int soft = 0;
- snprintf(name, sizeof name, "%s-%d", c1->name, entries);
- if ((cp = variable_get(name)) == NULL)
- break;
- if (sscanf(cp, "%s %jd %s %d", typ, &sz, mpoint, &soft) < 3) {
- msgConfirm("For slice entry %s, got an invalid detail entry of: %s", c1->name, cp);
- status = DITEM_FAILURE;
- break;
- } else {
- Chunk *tmp;
-
- flags = 0;
- if (!strcmp(typ, "swap")) {
- type = PART_SWAP;
- strcpy(mpoint, "SWAP");
- } else {
- type = PART_FILESYSTEM;
- if (!strcmp(mpoint, "/"))
- flags |= CHUNK_IS_ROOT;
- }
- if (!sz)
- sz = space_free(c1);
- if (sz > space_free(c1)) {
- msgConfirm("Not enough free space to create partition: %s", mpoint);
- status = DITEM_FAILURE;
- break;
- }
- if (!(tmp = Create_Chunk_DWIM(d, c1, sz, part,
- (type == PART_SWAP) ? FS_SWAP : FS_BSDFFS, flags))) {
- msgConfirm("Unable to create from partition spec: %s. Too big?", cp);
- status = DITEM_FAILURE;
- break;
- } else {
- PartInfo *pi;
- pi = tmp->private_data = new_part(PART_FILESYSTEM, mpoint, TRUE);
- tmp->private_free = safe_free;
- pi->newfs_data.newfs_ufs.softupdates = soft;
- }
- }
- }
- } else {
- /* Must be something we can set a mountpoint for */
- cp = variable_get(c1->name);
- if (cp) {
- char mpoint[50], do_newfs[8];
- Boolean newfs = FALSE;
-
- do_newfs[0] = '\0';
- if (sscanf(cp, "%s %s", mpoint, do_newfs) != 2) {
- msgConfirm("For slice entry %s, got an invalid detail entry of: %s", c1->name, cp);
- status = DITEM_FAILURE;
- break;
- }
- newfs = toupper(do_newfs[0]) == 'Y' ? TRUE : FALSE;
- if (c1->private_data) {
- p = c1->private_data;
- p->do_newfs = newfs;
- strcpy(p->mountpoint, mpoint);
- }
- else {
- c1->private_data = new_part(PART_FILESYSTEM, mpoint, newfs);
- c1->private_free = safe_free;
- }
- if (!strcmp(mpoint, "/"))
- c1->flags |= CHUNK_IS_ROOT;
- else
- c1->flags &= ~CHUNK_IS_ROOT;
- }
- }
- }
- if (status == DITEM_SUCCESS)
- variable_set2(DISK_LABELLED, "yes", 0);
- return status;
-}
diff --git a/usr.sbin/sade/main.c b/usr.sbin/sade/main.c
deleted file mode 100644
index cb493f3..0000000
--- a/usr.sbin/sade/main.c
+++ /dev/null
@@ -1,123 +0,0 @@
-/*
- * $FreeBSD$
- *
- * Copyright (c) 1995
- * Jordan Hubbard. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer,
- * verbatim and that no modifications are made prior to this
- * point in the file.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY JORDAN HUBBARD ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL JORDAN HUBBARD OR HIS PETS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, LIFE OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- *
- */
-
-#include "sade.h"
-#include <sys/signal.h>
-#include <sys/fcntl.h>
-
-const char *StartName; /* Initial contents of argv[0] */
-const char *ProgName = "sade";
-
-int
-main(int argc, char **argv)
-{
- int status;
-
- /* Record name to be able to restart */
- StartName = argv[0];
-
- signal(SIGPIPE, SIG_IGN);
-
- /* We don't work too well when running as non-root anymore */
- if (geteuid() != 0) {
- fprintf(stderr, "Error: This utility should only be run as root.\n");
- return 1;
- }
-
-#ifdef PC98
- {
- /* XXX */
- char *p = getenv("TERM");
- if (p && strcmp(p, "cons25") == 0)
- setenv("TERM", "cons25w", 1);
- }
-#endif
-
- /* Set up whatever things need setting up */
- systemInitialize(argc, argv);
-
- /* Set default flag and variable values */
- installVarDefaults(NULL);
-
- if (argc > 1 && !strcmp(argv[1], "-fake")) {
- variable_set2(VAR_DEBUG, "YES", 0);
- Fake = TRUE;
- msgConfirm("I'll be just faking it from here on out, OK?");
- }
- if (argc > 1 && !strcmp(argv[1], "-restart"))
- Restarting = TRUE;
-
- /* Try to preserve our scroll-back buffer */
- if (OnVTY) {
- int i;
-
- for (i = 0; i < 25; i++)
- putchar('\n');
- }
- /* Move stderr aside */
- if (DebugFD)
- dup2(DebugFD, 2);
-
- /* Initialize driver modules, if we haven't already done so (ie,
- the user hit Ctrl-C -> Restart. */
- if (!pvariable_get("modulesInitialize")) {
- pvariable_set("modulesInitialize=1");
- }
-
- /* Probe for all relevant devices on the system */
- deviceGetAll();
-
- /* First, see if we have any arguments to process (and argv[0] counts if it's not "sysinstall") */
-
- status = setjmp(BailOut);
- if (status) {
- msgConfirm("A signal %d was caught - I'm saving what I can and shutting\n"
- "down. If you can reproduce the problem, please turn Debug on\n"
- "in the Options menu for the extra information it provides\n"
- "in debugging problems like this.", status);
- ;
- }
-
- /* Begin user dialog at outer menu */
- dlg_clear();
- while (1) {
- dmenuOpen(&MenuMain);
- if (getpid() != 1
- || !msgNoYes("Are you sure you wish to exit?")
- )
- break;
- }
-
- /* Shut down curses */
- endwin();
-
- return 0;
-}
diff --git a/usr.sbin/sade/menus.c b/usr.sbin/sade/menus.c
deleted file mode 100644
index 886f80c..0000000
--- a/usr.sbin/sade/menus.c
+++ /dev/null
@@ -1,109 +0,0 @@
-/*
- * Copyright (c) 1995
- * Jordan Hubbard. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer,
- * verbatim and that no modifications are made prior to this
- * point in the file.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY JORDAN HUBBARD ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL JORDAN HUBBARD OR HIS PETS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, LIFE OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- *
- */
-
-#ifndef lint
-static const char rcsid[] =
- "$FreeBSD$";
-#endif
-
-#include "sade.h"
-
-/* All the system menus go here.
- *
- * Hardcoded things like version number strings will disappear from
- * these menus just as soon as I add the code for doing inline variable
- * expansion.
- */
-
-DMenu MenuDiskDevices = {
- DMENU_NORMAL_TYPE | DMENU_SELECTION_RETURNS,
- "Select Drive(s)",
- "Please select the drive on which you wish to perform this\n"
- "operation. If you are attempting to install a boot partition\n"
- "on a drive other than the first one or have multiple operating\n"
- "systems on your machine, you will have the option to install a boot\n"
- "manager later. To select a drive, use the arrow keys to move to it\n"
- "and press [SPACE] or [ENTER].\n\n"
- "Use [TAB] to get to the buttons and leave this menu.",
- "Press F1 for important information regarding disk geometry!",
- "drives",
- { { NULL, NULL, NULL } },
-};
-
-DMenu MenuMain = {
- DMENU_NORMAL_TYPE,
- "Disklabel and partitioning utility",
- "This is a utility for partitioning and/or labelling your disks.",
- "DISKUTIL",
- "main",
- {
-#ifdef WITH_SLICES
- { "1 Partition", "Managing disk partitions", diskPartitionEditor },
-#endif
- { "2 Label", "Label allocated disk partitions", diskLabelEditor },
- { NULL, NULL, NULL }
- },
-};
-
-#if defined(__i386__) || defined(__amd64__)
-#ifdef PC98
-/* IPL type menu */
-DMenu MenuIPLType = {
- DMENU_RADIO_TYPE | DMENU_SELECTION_RETURNS,
- "overwrite me", /* will be disk specific label */
- "If you want a FreeBSD Boot Manager, select \"BootMgr\". If you would\n"
- "prefer your Boot Manager to remain untouched then select \"None\".\n\n",
- "Press F1 to read about drive setup",
- "drives",
- { { "BootMgr", "Install the FreeBSD Boot Manager", NULL },
- { "None", "Leave the IPL untouched", NULL },
- { NULL, NULL, NULL } },
-};
-#else
-/* MBR type menu */
-DMenu MenuMBRType = {
- DMENU_RADIO_TYPE | DMENU_SELECTION_RETURNS,
- "overwrite me", /* will be disk specific label */
- "FreeBSD comes with a boot manager that allows you to easily\n"
- "select between FreeBSD and any other operating systems on your machine\n"
- "at boot time. If you have more than one drive and want to boot\n"
- "from the second one, the boot manager will also make it possible\n"
- "to do so (limitations in the PC BIOS usually prevent this otherwise).\n"
- "If you have other operating systems installed and would like a choice when\n"
- "booting, choose \"BootMgr\". If you would prefer to keep your existing\n"
- "boot manager, select \"None\".\n",
- "",
- "drives",
- { { "Standard", "Install a standard MBR (non-interactive boot manager)", NULL },
- { "BootMgr", "Install the FreeBSD boot manager", NULL },
- { "None", "Do not install a boot manager", NULL },
- { NULL, NULL, NULL } }
-};
-#endif /* PC98 */
-#endif /* __i386__ */
diff --git a/usr.sbin/sade/misc.c b/usr.sbin/sade/misc.c
deleted file mode 100644
index 3279e63..0000000
--- a/usr.sbin/sade/misc.c
+++ /dev/null
@@ -1,426 +0,0 @@
-/*
- * Miscellaneous support routines..
- *
- * $FreeBSD$
- *
- * Copyright (c) 1995
- * Jordan Hubbard. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer,
- * verbatim and that no modifications are made prior to this
- * point in the file.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY JORDAN HUBBARD ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL JORDAN HUBBARD OR HIS PETS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, LIFE OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- *
- */
-
-#include <ctype.h>
-#include <unistd.h>
-#include <sys/stat.h>
-#include <sys/errno.h>
-#include <sys/file.h>
-#include <sys/types.h>
-#include <dirent.h>
-#include <sys/wait.h>
-#include <sys/param.h>
-#include <sys/mount.h>
-#include <ufs/ufs/ufsmount.h>
-#include <sys/reboot.h>
-#include <sys/disklabel.h>
-#include <fs/msdosfs/msdosfsmount.h>
-
-#include "sade.h"
-
-/* Quick check to see if a file is readable */
-Boolean
-file_readable(char *fname)
-{
- if (!access(fname, F_OK))
- return TRUE;
- return FALSE;
-}
-
-/* sane strncpy() function */
-char *
-sstrncpy(char *dst, const char *src, int size)
-{
- dst[size] = '\0';
- return strncpy(dst, src, size);
-}
-
-/* Clip the whitespace off the end of a string */
-char *
-string_prune(char *str)
-{
- int len = str ? strlen(str) : 0;
-
- while (len && isspace(str[len - 1]))
- str[--len] = '\0';
- return str;
-}
-
-/* run the whitespace off the front of a string */
-char *
-string_skipwhite(char *str)
-{
- while (*str && isspace(*str))
- ++str;
- return str;
-}
-
-Boolean
-directory_exists(const char *dirname)
-{
- DIR *tptr;
-
- if (!dirname)
- return FALSE;
- if (!strlen(dirname))
- return FALSE;
-
- tptr = opendir(dirname);
- if (!tptr)
- return (FALSE);
-
- closedir(tptr);
- return (TRUE);
-}
-
-/* A free guaranteed to take NULL ptrs */
-void
-safe_free(void *ptr)
-{
- if (ptr)
- free(ptr);
-}
-
-/* A malloc that checks errors */
-void *
-safe_malloc(size_t size)
-{
- void *ptr;
-
- if (size <= 0)
- msgFatal("Invalid malloc size of %ld!", (long)size);
- ptr = malloc(size);
- if (!ptr)
- msgFatal("Out of memory!");
- bzero(ptr, size);
- return ptr;
-}
-
-int
-Mkdir(char *ipath)
-{
- struct stat sb;
- int final;
- char *p, *path;
-
- if (file_readable(ipath) || Fake)
- return DITEM_SUCCESS;
-
- path = strcpy(alloca(strlen(ipath) + 1), ipath);
- if (isDebug())
- msgDebug("mkdir(%s)\n", path);
- p = path;
- if (p[0] == '/') /* Skip leading '/'. */
- ++p;
- for (final = FALSE; !final; ++p) {
- if (p[0] == '\0' || (p[0] == '/' && p[1] == '\0'))
- final = TRUE;
- else if (p[0] != '/')
- continue;
- *p = '\0';
- if (stat(path, &sb)) {
- if (errno != ENOENT) {
- msgConfirm("Couldn't stat directory %s: %s", path, strerror(errno));
- return DITEM_FAILURE;
- }
- if (isDebug())
- msgDebug("mkdir(%s..)\n", path);
- if (mkdir(path, S_IRWXU | S_IRWXG | S_IRWXO) < 0) {
- msgConfirm("Couldn't create directory %s: %s", path,strerror(errno));
- return DITEM_FAILURE;
- }
- }
- *p = '/';
- }
- return DITEM_SUCCESS;
-}
-
-int
-Mount(char *mountp, void *dev)
-{
- struct ufs_args ufsargs;
- char device[80];
- char mountpoint[FILENAME_MAX];
-
- if (Fake)
- return DITEM_SUCCESS;
-
- if (*((char *)dev) != '/') {
- sprintf(device, "/dev/%s", (char *)dev);
- sprintf(mountpoint, "%s", mountp);
- }
- else {
- strcpy(device, dev);
- strcpy(mountpoint, mountp);
- }
- memset(&ufsargs,0,sizeof ufsargs);
-
- if (Mkdir(mountpoint)) {
- msgConfirm("Unable to make directory mountpoint for %s!", mountpoint);
- return DITEM_FAILURE;
- }
- if (isDebug())
- msgDebug("mount %s %s\n", device, mountpoint);
-
- ufsargs.fspec = device;
- if (mount("ufs", mountpoint, 0,
- (caddr_t)&ufsargs) == -1) {
- msgConfirm("Error mounting %s on %s : %s", device, mountpoint, strerror(errno));
- return DITEM_FAILURE;
- }
- return DITEM_SUCCESS;
-}
-
-WINDOW *
-savescr(void)
-{
- WINDOW *w;
-
- w = dupwin(newscr);
- return w;
-}
-
-void
-restorescr(WINDOW *w)
-{
- touchwin(w);
- wrefresh(w);
- delwin(w);
-}
-
-static int
-xdialog_count_rows(const char *p)
-{
- int rows = 0;
-
- while ((p = strchr(p, '\n')) != NULL) {
- p++;
- if (*p == '\0')
- break;
- rows++;
- }
-
- return rows ? rows : 1;
-}
-
-static int
-xdialog_count_columns(const char *p)
-{
- int len;
- int max_len = 0;
- const char *q;
-
- for (; (q = strchr(p, '\n')) != NULL; p = q + 1) {
- len = q - p;
- max_len = MAX(max_len, len);
- }
-
- len = strlen(p);
- max_len = MAX(max_len, len);
- return max_len;
-}
-
-int
-xdialog_menu(const char *title, const char *cprompt, int height, int width,
- int menu_height, int item_no, dialogMenuItem *ditems)
-{
- int i, result, choice = 0;
- DIALOG_LISTITEM *listitems;
- DIALOG_VARS save_vars;
-
- dlg_save_vars(&save_vars);
-
- /* initialize list items */
- listitems = dlg_calloc(DIALOG_LISTITEM, item_no + 1);
- assert_ptr(listitems, "xdialog_menu");
- for (i = 0; i < item_no; i++) {
- listitems[i].name = ditems[i].prompt;
- listitems[i].text = ditems[i].title;
- }
-
- /* calculate height */
- if (height < 0)
- height = xdialog_count_rows(cprompt) + menu_height + 4 + 2;
- if (height > LINES)
- height = LINES;
-
- /* calculate width */
- if (width < 0) {
- int tag_x = 0;
-
- for (i = 0; i < item_no; i++) {
- int j, l;
-
- l = strlen(listitems[i].name);
- for (j = 0; j < item_no; j++) {
- int k = strlen(listitems[j].text);
- tag_x = MAX(tag_x, l + k + 2);
- }
- }
- width = MAX(xdialog_count_columns(cprompt), title != NULL ? xdialog_count_columns(title) : 0);
- width = MAX(width, tag_x + 4) + 4;
- }
- width = MAX(width, 24);
- if (width > COLS)
- width = COLS;
-
- /* show menu */
- dialog_vars.default_item = listitems[choice].name;
- result = dlg_menu(title, cprompt, height, width,
- menu_height, item_no, listitems, &choice, NULL);
- switch (result) {
- case DLG_EXIT_ESC:
- result = -1;
- break;
- case DLG_EXIT_OK:
- if (ditems[choice].fire != NULL) {
- int status;
- WINDOW *save;
-
- save = savescr();
- status = ditems[choice].fire(ditems + choice);
- restorescr(save);
- }
- result = 0;
- break;
- case DLG_EXIT_CANCEL:
- default:
- result = 1;
- break;
- }
-
- free(listitems);
- dlg_restore_vars(&save_vars);
- return result;
-}
-
-int
-xdialog_radiolist(const char *title, const char *cprompt, int height, int width,
- int menu_height, int item_no, dialogMenuItem *ditems)
-{
- int i, result, choice = 0;
- DIALOG_LISTITEM *listitems;
- DIALOG_VARS save_vars;
-
- dlg_save_vars(&save_vars);
-
- /* initialize list items */
- listitems = dlg_calloc(DIALOG_LISTITEM, item_no + 1);
- assert_ptr(listitems, "xdialog_menu");
- for (i = 0; i < item_no; i++) {
- listitems[i].name = ditems[i].prompt;
- listitems[i].text = ditems[i].title;
- listitems[i].state = i == choice;
- }
-
- /* calculate height */
- if (height < 0)
- height = xdialog_count_rows(cprompt) + menu_height + 4 + 2;
- if (height > LINES)
- height = LINES;
-
- /* calculate width */
- if (width < 0) {
- int check_x = 0;
-
- for (i = 0; i < item_no; i++) {
- int j, l;
-
- l = strlen(listitems[i].name);
- for (j = 0; j < item_no; j++) {
- int k = strlen(listitems[j].text);
- check_x = MAX(check_x, l + k + 6);
- }
- }
- width = MAX(xdialog_count_columns(cprompt), title != NULL ? xdialog_count_columns(title) : 0);
- width = MAX(width, check_x + 4) + 4;
- }
- width = MAX(width, 24);
- if (width > COLS)
- width = COLS;
-
- /* show menu */
- dialog_vars.default_item = listitems[choice].name;
- result = dlg_checklist(title, cprompt, height, width,
- menu_height, item_no, listitems, NULL, FLAG_RADIO, &choice);
- switch (result) {
- case DLG_EXIT_ESC:
- result = -1;
- break;
- case DLG_EXIT_OK:
- if (ditems[choice].fire != NULL) {
- int status;
- WINDOW *save;
-
- save = savescr();
- status = ditems[choice].fire(ditems + choice);
- restorescr(save);
- }
- result = 0;
- break;
- case DLG_EXIT_CANCEL:
- default:
- result = 1;
- break;
- }
-
- /* save result */
- if (result == 0)
- dlg_add_result(listitems[choice].name);
- free(listitems);
- dlg_restore_vars(&save_vars);
- return result;
-}
-
-int
-xdialog_msgbox(const char *title, const char *cprompt,
- int height, int width, int pauseopt)
-{
- /* calculate height */
- if (height < 0)
- height = 2 + xdialog_count_rows(cprompt) + 2 + !!pauseopt;
- if (height > LINES)
- height = LINES;
-
- /* calculate width */
- if (width < 0) {
- width = title != NULL ? xdialog_count_columns(title) : 0;
- width = MAX(width, xdialog_count_columns(cprompt)) + 4;
- }
- if (pauseopt)
- width = MAX(width, 10);
- if (width > COLS)
- width = COLS;
-
- return dialog_msgbox(title, cprompt, height, width, pauseopt);
-}
diff --git a/usr.sbin/sade/msg.c b/usr.sbin/sade/msg.c
deleted file mode 100644
index fb3368e..0000000
--- a/usr.sbin/sade/msg.c
+++ /dev/null
@@ -1,357 +0,0 @@
-/*
- * $FreeBSD$
- *
- * Copyright (c) 1995
- * Jordan Hubbard. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer,
- * verbatim and that no modifications are made prior to this
- * point in the file.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY JORDAN HUBBARD ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL JORDAN HUBBARD OR HIS PETS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, LIFE OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- *
- */
-
-#include "sade.h"
-#include <stdarg.h>
-#include <sys/ioctl.h>
-#include <sys/consio.h>
-
-Boolean
-isDebug(void)
-{
- char *cp;
-
- return (cp = variable_get(VAR_DEBUG)) && strcmp(cp, "no");
-}
-
-/* Whack up an informational message on the status line, in stand-out */
-void
-msgYap(const char *fmt, ...)
-{
- va_list args;
- char *errstr;
- int attrs;
-
- errstr = (char *)alloca(FILENAME_MAX);
- va_start(args, fmt);
- vsnprintf(errstr, FILENAME_MAX, fmt, args);
- va_end(args);
- attrs = getattrs(stdscr);
- attrset(A_REVERSE);
- mvaddstr(StatusLine, 0, errstr);
- attrset(attrs);
- refresh();
-}
-
-/* Whack up an informational message on the status line */
-void
-msgInfo(const char *fmt, ...)
-{
- va_list args;
- char *errstr;
- int i, attrs;
- char line[81];
-
- attrs = getattrs(stdscr);
- /* NULL is a special convention meaning "erase the old stuff" */
- if (!fmt) {
- move(StatusLine, 0);
- clrtoeol();
- return;
- }
- errstr = (char *)alloca(FILENAME_MAX);
- va_start(args, fmt);
- vsnprintf(errstr, FILENAME_MAX, fmt, args);
- va_end(args);
- memset(line, ' ', 80);
- for (i = 0; i < 80; i++) {
- if (errstr[i])
- line[i] = errstr[i];
- else
- break;
- }
- line[80] = '\0';
- attrset(ATTR_TITLE);
- mvaddstr(StatusLine, 0, line);
- attrset(attrs);
- move(StatusLine, 79);
- refresh();
-}
-
-/* Whack up a warning on the status line */
-void
-msgWarn(const char *fmt, ...)
-{
- va_list args;
- char *errstr;
- int attrs;
-
- errstr = (char *)alloca(FILENAME_MAX);
- strcpy(errstr, "Warning: ");
- va_start(args, fmt);
- vsnprintf((char *)(errstr + strlen(errstr)), FILENAME_MAX, fmt, args);
- va_end(args);
- attrs = getattrs(stdscr);
- beep();
- attrset(ATTR_TITLE);
- mvaddstr(StatusLine, 0, errstr);
- attrset(attrs);
- refresh();
- if (OnVTY && isDebug())
- msgDebug("Warning message `%s'\n", errstr);
-}
-
-/* Whack up an error on the status line */
-void
-msgError(const char *fmt, ...)
-{
- va_list args;
- char *errstr;
- int attrs;
-
- errstr = (char *)alloca(FILENAME_MAX);
- strcpy(errstr, "Error: ");
- va_start(args, fmt);
- vsnprintf((char *)(errstr + strlen(errstr)), FILENAME_MAX, fmt, args);
- va_end(args);
- beep();
- attrs = getattrs(stdscr);
- attrset(ATTR_TITLE);
- mvaddstr(StatusLine, 0, errstr);
- attrset(attrs);
- refresh();
- if (OnVTY && isDebug())
- msgDebug("Error message `%s'\n", errstr);
-}
-
-/* Whack up a fatal error on the status line */
-void
-msgFatal(const char *fmt, ...)
-{
- va_list args;
- char *errstr;
- int attrs;
-
- errstr = (char *)alloca(FILENAME_MAX);
- strcpy(errstr, "Fatal Error: ");
- va_start(args, fmt);
- vsnprintf((char *)(errstr + strlen(errstr)), FILENAME_MAX, fmt, args);
- va_end(args);
- beep();
- attrs = getattrs(stdscr);
- attrset(ATTR_TITLE);
- mvaddstr(StatusLine, 0, errstr);
- addstr(" - ");
- addstr("PRESS ANY KEY TO ");
- if (getpid() == 1)
- addstr("REBOOT");
- else
- addstr("QUIT");
- attrset(attrs);
- refresh();
- if (OnVTY)
- msgDebug("Fatal error `%s'!\n", errstr);
- getch();
-}
-
-/* Put up a message in a popup confirmation box */
-void
-msgConfirm(const char *fmt, ...)
-{
- va_list args;
- char *errstr;
- WINDOW *w = savescr();
-
- errstr = (char *)alloca(FILENAME_MAX);
- va_start(args, fmt);
- vsnprintf(errstr, FILENAME_MAX, fmt, args);
- va_end(args);
- dialog_vars.help_line = NULL;
- dialog_vars.help_file = NULL;
- if (OnVTY) {
- ioctl(0, VT_ACTIVATE, 1);
- msgInfo(NULL);
- }
- dialog_vars.help_line = "Press Enter or Space";
- xdialog_msgbox("Message", errstr, -1, -1, 1);
- dialog_vars.help_line = NULL;
-
- restorescr(w);
-}
-
-/* Put up a message in a popup information box */
-void
-msgNotify(const char *fmt, ...)
-{
- va_list args;
- char *errstr;
-
- errstr = (char *)alloca(FILENAME_MAX);
- va_start(args, fmt);
- vsnprintf(errstr, FILENAME_MAX, fmt, args);
- va_end(args);
- dialog_vars.help_line = NULL;
- dialog_vars.help_file = NULL;
- if (isDebug())
- msgDebug("Notify: %s\n", errstr);
- xdialog_msgbox(NULL, errstr, -1, -1, 0);
-}
-
-/* Put up a message in a popup yes/no box and return 0 for YES, 1 for NO */
-int
-msgYesNo(const char *fmt, ...)
-{
- va_list args;
- char *errstr;
- int ret;
- WINDOW *w = savescr();
-
- errstr = (char *)alloca(FILENAME_MAX);
- va_start(args, fmt);
- vsnprintf(errstr, FILENAME_MAX, fmt, args);
- va_end(args);
- dialog_vars.help_line = NULL;
- dialog_vars.help_file = NULL;
- if (OnVTY) {
- ioctl(0, VT_ACTIVATE, 1); /* Switch back */
- msgInfo(NULL);
- }
- if (variable_get(VAR_NONINTERACTIVE))
- return 0; /* If non-interactive, return YES all the time */
- ret = dialog_yesno("User Confirmation Requested", errstr, -1, -1);
- restorescr(w);
- return ret;
-}
-
-/* Put up a message in a popup no/yes box and return 0 for YES, 1 for NO */
-int
-msgNoYes(const char *fmt, ...)
-{
- va_list args;
- char *errstr;
- int ret;
- WINDOW *w = savescr();
- DIALOG_VARS save_vars;
-
- errstr = (char *)alloca(FILENAME_MAX);
- va_start(args, fmt);
- vsnprintf(errstr, FILENAME_MAX, fmt, args);
- va_end(args);
- dialog_vars.help_line = NULL;
- dialog_vars.help_file = NULL;
- if (OnVTY) {
- ioctl(0, VT_ACTIVATE, 1); /* Switch back */
- msgInfo(NULL);
- }
- if (variable_get(VAR_NONINTERACTIVE))
- return 1; /* If non-interactive, return NO all the time */
- dlg_save_vars(&save_vars);
- dialog_vars.defaultno = TRUE;
- ret = dialog_yesno("User Confirmation Requested", errstr, -1, -1);
- dlg_restore_vars(&save_vars);
- restorescr(w);
- return ret;
-}
-
-/* Put up a message in an input box and return the value */
-char *
-msgGetInput(char *buf, const char *fmt, ...)
-{
- va_list args;
- char *errstr;
- static char input_buffer[256];
- int rval;
- WINDOW *w = savescr();
-
- errstr = (char *)alloca(FILENAME_MAX);
- va_start(args, fmt);
- vsnprintf(errstr, FILENAME_MAX, fmt, args);
- va_end(args);
- dialog_vars.help_line = NULL;
- dialog_vars.help_file = NULL;
- if (buf)
- SAFE_STRCPY(input_buffer, buf);
- else
- input_buffer[0] = '\0';
- if (OnVTY) {
- ioctl(0, VT_ACTIVATE, 1); /* Switch back */
- msgInfo(NULL);
- }
- rval = dialog_inputbox("Value Required", errstr, -1, -1, input_buffer, 0);
- restorescr(w);
- if (!rval)
- return dialog_vars.input_result;
- else
- return NULL;
-}
-
-/* Write something to the debugging port */
-void
-msgDebug(const char *fmt, ...)
-{
- va_list args;
- char *dbg;
-
- if (DebugFD == -1)
- return;
- dbg = (char *)alloca(FILENAME_MAX);
- strcpy(dbg, "DEBUG: ");
- va_start(args, fmt);
- vsnprintf((char *)(dbg + strlen(dbg)), FILENAME_MAX, fmt, args);
- va_end(args);
- write(DebugFD, dbg, strlen(dbg));
-}
-
-/* Tell the user there's some output to go look at */
-void
-msgWeHaveOutput(const char *fmt, ...)
-{
- va_list args;
- char *errstr;
- WINDOW *w = savescr();
-
- errstr = (char *)alloca(FILENAME_MAX);
- va_start(args, fmt);
- vsnprintf(errstr, FILENAME_MAX, fmt, args);
- va_end(args);
- dialog_vars.help_line = NULL;
- dialog_vars.help_file = NULL;
- msgDebug("Notify: %s\n", errstr);
- dlg_clear();
- sleep(2);
- xdialog_msgbox(NULL, errstr, -1, -1, 0);
- restorescr(w);
-}
-
-/* Simple versions of msgConfirm() and msgNotify() for calling from scripts */
-int
-msgSimpleConfirm(const char *str)
-{
- msgConfirm("%s", str);
- return DITEM_SUCCESS;
-}
-
-int
-msgSimpleNotify(const char *str)
-{
- msgNotify("%s", str);
- return DITEM_SUCCESS;
-}
diff --git a/usr.sbin/sade/sade.h b/usr.sbin/sade/sade.h
deleted file mode 100644
index 3e163ad..0000000
--- a/usr.sbin/sade/sade.h
+++ /dev/null
@@ -1,465 +0,0 @@
-/*
- * Copyright (c) 1995
- * Jordan Hubbard. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer,
- * verbatim and that no modifications are made prior to this
- * point in the file.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY JORDAN HUBBARD ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL JORDAN HUBBARD OR HIS PETS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, LIFE OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- *
- * $FreeBSD$
- */
-
-#ifndef _SADE_H_INCLUDE
-#define _SADE_H_INCLUDE
-
-#include <sys/types.h>
-#include <sys/wait.h>
-#include <errno.h>
-#include <setjmp.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-#include <dialog.h>
-
-/*** Defines ***/
-
-#if defined(__i386__) || defined(__amd64__)
-#define WITH_SYSCONS
-#define WITH_MICE
-#endif
-
-#if defined(__i386__) || defined(__amd64__)
-#define WITH_SLICES
-#endif
-
-#if defined(__i386__)
-#define WITH_LINUX
-#endif
-
-/* device limits */
-#define DEV_NAME_MAX 128 /* The maximum length of a device name */
-#define DEV_MAX 100 /* The maximum number of devices we'll deal with */
-#define IO_ERROR -2 /* Status code for I/O error rather than normal EOF */
-
-/*
- * I make some pretty gross assumptions about having a max of 50 chunks
- * total - 8 slices and 42 partitions. I can't easily display many more
- * than that on the screen at once!
- *
- * For 2.1 I'll revisit this and try to make it more dynamic, but since
- * this will catch 99.99% of all possible cases, I'm not too worried.
- */
-#define MAX_CHUNKS 40
-
-/* Internal environment variable names */
-#define DISK_PARTITIONED "_diskPartitioned"
-#define DISK_LABELLED "_diskLabelled"
-#define DISK_SELECTED "_diskSelected"
-#define SYSTEM_STATE "_systemState"
-#define RUNNING_ON_ROOT "_runningOnRoot"
-
-/* Ones that can be tweaked from config files */
-#define VAR_BLANKTIME "blanktime"
-#define VAR_BOOTMGR "bootManager"
-#define VAR_DEBUG "debug"
-#define VAR_DISK "disk"
-#define VAR_DISKINTERACTIVE "diskInteractive"
-#define VAR_DEDICATE_DISK "dedicateDisk"
-#define VAR_COMMAND "command"
-#define VAR_CONFIG_FILE "configFile"
-#define VAR_GEOMETRY "geometry"
-#define VAR_INSTALL_CFG "installConfig"
-#define VAR_INSTALL_ROOT "installRoot"
-#define VAR_LABEL "label"
-#define VAR_LABEL_COUNT "labelCount"
-#define VAR_NEWFS_ARGS "newfsArgs"
-#define VAR_NO_CONFIRM "noConfirm"
-#define VAR_NO_ERROR "noError"
-#define VAR_NO_WARN "noWarn"
-#define VAR_NO_USR "noUsr"
-#define VAR_NO_TMP "noTmp"
-#define VAR_NO_HOME "noHome"
-#define VAR_NONINTERACTIVE "nonInteractive"
-#define VAR_PARTITION "partition"
-#define VAR_RELNAME "releaseName"
-#define VAR_ROOT_SIZE "rootSize"
-#define VAR_SWAP_SIZE "swapSize"
-#define VAR_TAPE_BLOCKSIZE "tapeBlocksize"
-#define VAR_UFS_PATH "ufs"
-#define VAR_USR_SIZE "usrSize"
-#define VAR_VAR_SIZE "varSize"
-#define VAR_TMP_SIZE "tmpSize"
-#define VAR_TERM "TERM"
-#define VAR_CONSTERM "_consterm"
-
-#define DEFAULT_TAPE_BLOCKSIZE "20"
-
-/* One MB worth of blocks */
-#define ONE_MEG 2048
-#define ONE_GIG (ONE_MEG * 1024)
-
-/* Which selection attributes to use */
-#define ATTR_SELECTED (ColorDisplay ? item_selected_attr : item_attr)
-#define ATTR_TITLE button_active_attr
-
-/* Handy strncpy() macro */
-#define SAFE_STRCPY(to, from) sstrncpy((to), (from), sizeof (to) - 1)
-
-/*** Types ***/
-typedef int Boolean;
-typedef struct disk Disk;
-typedef struct chunk Chunk;
-
-/* special return codes for `fire' actions */
-#define DITEM_STATUS(flag) ((flag) & 0x0000FFFF)
-#define DITEM_SUCCESS 0
-#define DITEM_FAILURE 1
-
-/* flags - returned in upper 16 bits of return status */
-#define DITEM_LEAVE_MENU (1 << 16)
-#define DITEM_RESTORE (1 << 19)
-
-/* for use in describing more exotic behaviors */
-typedef struct _dmenu_item {
- char *prompt;
- char *title;
- int (*fire)(struct _dmenu_item *self);
-} dialogMenuItem;
-
-/* Bitfields for menu options */
-#define DMENU_NORMAL_TYPE 0x1 /* Normal dialog menu */
-#define DMENU_RADIO_TYPE 0x2 /* Radio dialog menu */
-#define DMENU_SELECTION_RETURNS 0x8 /* Immediate return on item selection */
-
-typedef struct _dmenu {
- int type; /* What sort of menu we are */
- char *title; /* Our title */
- char *prompt; /* Our prompt */
- char *helpline; /* Line of help at bottom */
- char *helpfile; /* Help file for "F1" */
- dialogMenuItem items[]; /* Array of menu items */
-} DMenu;
-
-/* An rc.conf variable */
-typedef struct _variable {
- struct _variable *next;
- char *name;
- char *value;
- int dirty;
-} Variable;
-
-#define NO_ECHO_OBJ(type) ((type) | (DITEM_NO_ECHO << 16))
-#define TYPE_OF_OBJ(type) ((type) & 0xff)
-#define ATTR_OF_OBJ(type) ((type) >> 16)
-
-/* A screen layout structure */
-typedef struct _layout {
- int y; /* x & Y co-ordinates */
- int x;
- int len; /* The size of the dialog on the screen */
- int maxlen; /* How much the user can type in ... */
- char *prompt; /* The string for the prompt */
- char *help; /* The display for the help line */
- void *var; /* The var to set when this changes */
- int type; /* The type of the dialog to create */
- void *obj; /* The obj pointer returned by libdialog */
-} Layout;
-
-typedef enum {
- DEVICE_TYPE_NONE,
- DEVICE_TYPE_DISK,
- DEVICE_TYPE_DOS,
- DEVICE_TYPE_UFS,
- DEVICE_TYPE_ANY,
-} DeviceType;
-
-/* A "device" from sade's point of view */
-typedef struct _device {
- char name[DEV_NAME_MAX];
- char *description;
- char *devname;
- DeviceType type;
- Boolean (*init)(struct _device *dev);
- FILE * (*get)(struct _device *dev, char *file, Boolean probe);
- void (*shutdown)(struct _device *dev);
- void *private;
- unsigned int flags;
- unsigned int volume;
-} Device;
-
-/* Some internal representations of partitions */
-typedef enum {
- PART_NONE,
- PART_SLICE,
- PART_SWAP,
- PART_FILESYSTEM,
- PART_FAT,
- PART_EFI
-} PartType;
-
-#define NEWFS_UFS_CMD "newfs"
-#define NEWFS_MSDOS_CMD "newfs_msdos"
-
-enum newfs_type { NEWFS_UFS, NEWFS_MSDOS, NEWFS_CUSTOM };
-#define NEWFS_UFS_STRING "UFS"
-#define NEWFS_MSDOS_STRING "FAT"
-#define NEWFS_CUSTOM_STRING "CST"
-
-/* The longest set of custom command line arguments we'll pass. */
-#define NEWFS_CMD_ARGS_MAX 256
-
-typedef struct _part_info {
- char mountpoint[FILENAME_MAX];
-
- /* Is invocation of newfs desired? */
- Boolean do_newfs;
-
- enum newfs_type newfs_type;
- union {
- struct {
- char user_options[NEWFS_CMD_ARGS_MAX];
- Boolean acls; /* unused */
- Boolean multilabel; /* unused */
- Boolean softupdates;
- Boolean ufs1;
- } newfs_ufs;
- struct {
- /* unused */
- } newfs_msdos;
- struct {
- char command[NEWFS_CMD_ARGS_MAX];
- } newfs_custom;
- } newfs_data;
-} PartInfo;
-
-/* An option */
-typedef struct _opt {
- char *name;
- char *desc;
- enum { OPT_IS_STRING, OPT_IS_INT, OPT_IS_FUNC, OPT_IS_VAR } type;
- void *data;
- void *aux;
- char *(*check)(void);
-} Option;
-
-typedef int (*commandFunc)(char *key, void *data);
-
-#define EXTRAS_FIELD_LEN 128
-
-/*** Externs ***/
-extern jmp_buf BailOut; /* Used to get the heck out */
-extern int DebugFD; /* Where diagnostic output goes */
-extern Boolean Fake; /* Don't actually modify anything - testing */
-extern Boolean Restarting; /* Are we restarting sysinstall? */
-extern Boolean SystemWasInstalled; /* Did we install it? */
-extern Boolean RunningAsInit; /* Are we running stand-alone? */
-extern Boolean DialogActive; /* Is the dialog() stuff up? */
-extern Boolean ColorDisplay; /* Are we on a color display? */
-extern Boolean OnVTY; /* On a syscons VTY? */
-extern Variable *VarHead; /* The head of the variable chain */
-extern int BootMgr; /* Which boot manager to use */
-extern int StatusLine; /* Where to print our status messages */
-#if defined(__i386__) || defined(__amd64__)
-#ifdef PC98
-extern DMenu MenuIPLType; /* Type of IPL to write on the disk */
-#else
-extern DMenu MenuMBRType; /* Type of MBR to write on the disk */
-#endif
-#endif
-extern DMenu MenuMain; /* New main menu */
-extern DMenu MenuDiskDevices; /* Disk type devices */
-extern const char * StartName; /* Which name we were started as */
-extern const char * ProgName; /* Program's proper name */
-
-/* Important chunks. */
-extern Chunk *HomeChunk;
-extern Chunk *RootChunk;
-extern Chunk *SwapChunk;
-extern Chunk *TmpChunk;
-extern Chunk *UsrChunk;
-extern Chunk *VarChunk;
-#ifdef __ia64__
-extern Chunk *EfiChunk;
-#endif
-
-/* Stuff from libdialog which isn't properly declared outside */
-extern void display_helpfile(void);
-extern void display_helpline(WINDOW *w, int y, int width);
-
-/*** Prototypes ***/
-
-/* command.c */
-extern void command_clear(void);
-extern void command_sort(void);
-extern void command_execute(void);
-extern void command_shell_add(char *key, const char *fmt, ...) __printflike(2, 3);
-extern void command_func_add(char *key, commandFunc func, void *data);
-
-/* devices.c */
-extern DMenu *deviceCreateMenu(DMenu *menu, DeviceType type, int (*hook)(dialogMenuItem *d));
-extern void deviceGetAll(void);
-extern void deviceReset(void);
-extern void deviceRescan(void);
-extern Device **deviceFind(char *name, DeviceType type);
-extern Device **deviceFindDescr(char *name, char *desc, DeviceType class);
-extern int deviceCount(Device **devs);
-extern Device *new_device(char *name);
-extern Device *deviceRegister(char *name, char *desc, char *devicename, DeviceType type,
- Boolean (*init)(Device *mediadev),
- FILE * (*get)(Device *dev, char *file, Boolean probe),
- void (*shutDown)(Device *mediadev),
- void *private);
-extern Boolean dummyInit(Device *dev);
-extern FILE *dummyGet(Device *dev, char *dist, Boolean probe);
-extern void dummyShutdown(Device *dev);
-
-/* disks.c */
-#ifdef WITH_SLICES
-extern void diskPartition(Device *dev);
-extern int diskPartitionEditor(dialogMenuItem *self);
-#endif
-extern int diskPartitionWrite(Device *dev);
-
-/* dispatch.c */
-extern int dispatchCommand(char *command);
-extern int dispatch_load_floppy(dialogMenuItem *self);
-extern int dispatch_load_file_int(int);
-extern int dispatch_load_file(dialogMenuItem *self);
-
-/* dmenu.c */
-extern int dmenuSetValue(dialogMenuItem *tmp);
-extern Boolean dmenuOpen(DMenu *menu);
-extern int dmenuRadioCheck(dialogMenuItem *item);
-
-/* dos.c */
-extern Boolean mediaCloseDOS(Device *dev, FILE *fp);
-extern Boolean mediaInitDOS(Device *dev);
-extern FILE *mediaGetDOS(Device *dev, char *file, Boolean probe);
-extern void mediaShutdownDOS(Device *dev);
-
-/* globals.c */
-extern void globalsInit(void);
-
-/* install.c */
-extern Boolean checkLabels(Boolean whinge);
-extern int installCommit(dialogMenuItem *self);
-extern int installCustomCommit(dialogMenuItem *self);
-extern int installFilesystems(Device *dev);
-extern int installVarDefaults(dialogMenuItem *self);
-extern void installEnvironment(void);
-extern Boolean copySelf(void);
-
-/* kget.c */
-extern int kget(char *out);
-
-/* label.c */
-extern int diskLabelEditor(dialogMenuItem *self);
-extern int diskLabelCommit(Device *dev);
-
-/* misc.c */
-extern Boolean file_readable(char *fname);
-extern Boolean directory_exists(const char *dirname);
-extern char *string_prune(char *str);
-extern char *string_skipwhite(char *str);
-extern void safe_free(void *ptr);
-extern void *safe_malloc(size_t size);
-extern int Mkdir(char *);
-extern int Mount(char *, void *data);
-
-extern WINDOW *savescr(void);
-extern void restorescr(WINDOW *w);
-extern char *sstrncpy(char *dst, const char *src, int size);
-
-extern int xdialog_menu(const char *title, const char *cprompt,
- int height, int width, int menu_height,
- int item_no, dialogMenuItem *ditems);
-extern int xdialog_radiolist(const char *title, const char *cprompt,
- int height, int width, int menu_height,
- int item_no, dialogMenuItem *ditems);
-extern int xdialog_msgbox(const char *title, const char *cprompt,
- int height, int width, int pauseopt);
-
-/* msg.c */
-extern Boolean isDebug(void);
-extern void msgInfo(const char *fmt, ...) __printf0like(1, 2);
-extern void msgYap(const char *fmt, ...) __printflike(1, 2);
-extern void msgWarn(const char *fmt, ...) __printflike(1, 2);
-extern void msgDebug(const char *fmt, ...) __printflike(1, 2);
-extern void msgError(const char *fmt, ...) __printflike(1, 2);
-extern void msgFatal(const char *fmt, ...) __printflike(1, 2);
-extern void msgConfirm(const char *fmt, ...) __printflike(1, 2);
-extern void msgNotify(const char *fmt, ...) __printflike(1, 2);
-extern void msgWeHaveOutput(const char *fmt, ...) __printflike(1, 2);
-extern int msgYesNo(const char *fmt, ...) __printflike(1, 2);
-extern int msgNoYes(const char *fmt, ...) __printflike(1, 2);
-extern char *msgGetInput(char *buf, const char *fmt, ...) __printflike(2, 3);
-extern int msgSimpleConfirm(const char *);
-extern int msgSimpleNotify(const char *);
-
-/* pccard.c */
-extern void pccardInitialize(void);
-
-/* system.c */
-extern void systemInitialize(int argc, char **argv);
-extern void systemShutdown(int status);
-extern int execExecute(char *cmd, char *name);
-extern int systemExecute(char *cmd);
-extern void systemSuspendDialog(void);
-extern void systemResumeDialog(void);
-extern int systemDisplayHelp(char *file);
-extern char *systemHelpFile(char *file, char *buf);
-extern void systemChangeFont(const u_char font[]);
-extern void systemChangeLang(char *lang);
-extern void systemChangeTerminal(char *color, const u_char c_termcap[], char *mono, const u_char m_termcap[]);
-extern void systemChangeScreenmap(const u_char newmap[]);
-extern int vsystem(const char *fmt, ...) __printflike(1, 2);
-
-/* termcap.c */
-extern int set_termcap(void);
-
-/* variable.c */
-extern void variable_set(char *var, int dirty);
-extern void variable_set2(char *name, char *value, int dirty);
-extern char *variable_get(char *var);
-extern int variable_cmp(char *var, char *value);
-extern void variable_unset(char *var);
-extern char *variable_get_value(char *var, char *prompt, int dirty);
-extern int variable_check(char *data);
-extern int variable_check2(char *data);
-extern int dump_variables(dialogMenuItem *self);
-extern void free_variables(void);
-extern void pvariable_set(char *var);
-extern char *pvariable_get(char *var);
-
-/* wizard.c */
-extern void slice_wizard(Disk *d);
-
-/*
- * Macros. Please find a better place for us!
- */
-#define DEVICE_INIT(d) ((d) != NULL ? (d)->init((d)) : (Boolean)0)
-#define DEVICE_GET(d, b, f) ((d) != NULL ? (d)->get((d), (b), (f)) : NULL)
-#define DEVICE_SHUTDOWN(d) ((d) != NULL ? (d)->shutdown((d)) : (void)0)
-
-#endif
-/* _SYSINSTALL_H_INCLUDE */
diff --git a/usr.sbin/sade/system.c b/usr.sbin/sade/system.c
deleted file mode 100644
index 1e0e52f..0000000
--- a/usr.sbin/sade/system.c
+++ /dev/null
@@ -1,304 +0,0 @@
-/*
- * $FreeBSD$
- *
- * Jordan Hubbard
- *
- * My contributions are in the public domain.
- *
- * Parts of this file are also blatently stolen from Poul-Henning Kamp's
- * previous version of sysinstall, and as such fall under his "BEERWARE license"
- * so buy him a beer if you like it! Buy him a beer for me, too!
- * Heck, get him completely drunk and send me pictures! :-)
- */
-
-#include <signal.h>
-#include <termios.h>
-#include <sys/param.h>
-#include <sys/reboot.h>
-#include <sys/consio.h>
-#include <sys/fcntl.h>
-#include <sys/ioctl.h>
-#include <sys/mount.h>
-#include <sys/stat.h>
-#include <sys/sysctl.h>
-#include <ufs/ufs/ufsmount.h>
-
-#include "sade.h"
-
-/* Where we stick our temporary expanded doc file */
-#define DOC_TMP_DIR "/tmp/.doc"
-#define DOC_TMP_FILE "/tmp/.doc/doc.tmp"
-
-/*
- * Handle interrupt signals - this probably won't work in all cases
- * due to our having bogotified the internal state of dialog or curses,
- * but we'll give it a try.
- */
-static int
-intr_continue(dialogMenuItem *self)
-{
- return DITEM_LEAVE_MENU;
-}
-
-static int
-intr_restart(dialogMenuItem *self)
-{
- int ret, fd, fdmax;
-
- free_variables();
- fdmax = getdtablesize();
- for (fd = 3; fd < fdmax; fd++)
- close(fd);
- ret = execl(StartName, StartName, "-restart", (char *)NULL);
- msgDebug("execl failed (%s)\n", strerror(errno));
- /* NOTREACHED */
- return -1;
-}
-
-static dialogMenuItem intrmenu[] = {
- { "Restart", "Restart the program", intr_restart },
- { "Continue", "Continue without restarting", intr_continue },
-};
-
-
-static void
-handle_intr(int sig)
-{
- WINDOW *save = savescr();
-
- dialog_vars.help_line = NULL;
- dialog_vars.help_file = NULL;
- if (OnVTY) {
- ioctl(0, VT_ACTIVATE, 1); /* Switch back */
- msgInfo(NULL);
- }
- (void)xdialog_menu("Installation interrupt",
- "Do you want to abort the installation?",
- -1, -1, 2, 2, intrmenu);
- restorescr(save);
-}
-
-/* Expand a file into a convenient location, nuking it each time */
-static char *
-expand(char *fname)
-{
- char *gunzip = "/usr/bin/gunzip";
-
- if (!directory_exists(DOC_TMP_DIR)) {
- Mkdir(DOC_TMP_DIR);
- if (chown(DOC_TMP_DIR, 0, 0) < 0)
- return NULL;
- if (chmod(DOC_TMP_DIR, S_IRWXU) < 0)
- return NULL;
- }
- else
- unlink(DOC_TMP_FILE);
- if (!file_readable(fname) || vsystem("%s < %s > %s", gunzip, fname, DOC_TMP_FILE))
- return NULL;
- return DOC_TMP_FILE;
-}
-
-/* Initialize system defaults */
-void
-systemInitialize(int argc, char **argv)
-{
- size_t i;
- int boothowto;
- sigset_t signalset;
-
- signal(SIGINT, SIG_IGN);
- globalsInit();
-
- i = sizeof(boothowto);
- if (!sysctlbyname("debug.boothowto", &boothowto, &i, NULL, 0) &&
- (i == sizeof(boothowto)) && (boothowto & RB_VERBOSE))
- variable_set2(VAR_DEBUG, "YES", 0);
-
- if (set_termcap() == -1) {
- printf("Can't find terminal entry\n");
- exit(-1);
- }
-
- /* XXX - libdialog has particularly bad return value checking */
- init_dialog(stdin, stdout);
-
- /* If we haven't crashed I guess dialog is running ! */
- DialogActive = TRUE;
-
- /* Make sure HOME is set for those utilities that need it */
- signal(SIGINT, handle_intr);
- /*
- * Make sure we can be interrupted even if we were re-executed
- * from an interrupt.
- */
- sigemptyset(&signalset);
- sigaddset(&signalset, SIGINT);
- sigprocmask(SIG_UNBLOCK, &signalset, NULL);
-
- (void)vsystem("rm -rf %s", DOC_TMP_DIR);
-}
-
-/* Run some general command */
-int
-systemExecute(char *command)
-{
- int status;
- struct termios foo;
- WINDOW *w = savescr();
-
- dlg_clear();
- end_dialog();
- DialogActive = FALSE;
- if (tcgetattr(0, &foo) != -1) {
- foo.c_cc[VERASE] = '\010';
- tcsetattr(0, TCSANOW, &foo);
- }
- if (!Fake)
- status = system(command);
- else {
- status = 0;
- msgDebug("systemExecute: Faked execution of `%s'\n", command);
- }
- DialogActive = TRUE;
- restorescr(w);
- return status;
-}
-
-/* suspend/resume libdialog/curses screen */
-static WINDOW *oldW;
-
-void
-systemSuspendDialog(void)
-{
-
- oldW = savescr();
- dlg_clear();
- end_dialog();
- DialogActive = FALSE;
-}
-
-void
-systemResumeDialog(void)
-{
-
- DialogActive = TRUE;
- restorescr(oldW);
-}
-
-/* Display a help file in a filebox */
-int
-systemDisplayHelp(char *file)
-{
- char *fname = NULL;
- char buf[FILENAME_MAX];
- int ret = 0;
- WINDOW *w = savescr();
-
- fname = systemHelpFile(file, buf);
- if (!fname) {
- snprintf(buf, FILENAME_MAX, "The %s file is not provided on this particular floppy image.", file);
- dialog_vars.help_line = NULL;
- dialog_vars.help_file = NULL;
- xdialog_msgbox("Sorry!", buf, -1, -1, 1);
- ret = 1;
- }
- else {
- dialog_vars.help_line = NULL;
- dialog_vars.help_file = NULL;
- dialog_textbox(file, fname, LINES, COLS);
- }
- restorescr(w);
- return ret;
-}
-
-char *
-systemHelpFile(char *file, char *buf)
-{
- if (!file)
- return NULL;
- if (file[0] == '/')
- return file;
- snprintf(buf, FILENAME_MAX, "/stand/help/%s.hlp.gz", file);
- if (file_readable(buf))
- return expand(buf);
- snprintf(buf, FILENAME_MAX, "/stand/help/%s.hlp", file);
- if (file_readable(buf))
- return expand(buf);
- snprintf(buf, FILENAME_MAX, "/stand/help/%s.TXT.gz", file);
- if (file_readable(buf))
- return expand(buf);
- snprintf(buf, FILENAME_MAX, "/stand/help/%s.TXT", file);
- if (file_readable(buf))
- return expand(buf);
- snprintf(buf, FILENAME_MAX, "/usr/src/usr.sbin/%s/help/%s.hlp", ProgName,
- file);
- if (file_readable(buf))
- return buf;
- snprintf(buf, FILENAME_MAX, "/usr/src/usr.sbin/%s/help/%s.TXT", ProgName,
- file);
- if (file_readable(buf))
- return buf;
- return NULL;
-}
-
-int
-vsystem(const char *fmt, ...)
-{
- va_list args;
- int pstat;
- pid_t pid;
- int omask;
- sig_t intsave, quitsave;
- char *cmd;
- int i;
- struct stat sb;
-
- cmd = (char *)alloca(FILENAME_MAX);
- cmd[0] = '\0';
- va_start(args, fmt);
- vsnprintf(cmd, FILENAME_MAX, fmt, args);
- va_end(args);
-
- omask = sigblock(sigmask(SIGCHLD));
- if (Fake) {
- msgDebug("vsystem: Faked execution of `%s'\n", cmd);
- return 0;
- }
- if (isDebug())
- msgDebug("Executing command `%s'\n", cmd);
- pid = fork();
- if (pid == -1) {
- (void)sigsetmask(omask);
- i = 127;
- }
- else if (!pid) { /* Junior */
- (void)sigsetmask(omask);
- if (DebugFD != -1) {
- dup2(DebugFD, 0);
- dup2(DebugFD, 1);
- dup2(DebugFD, 2);
- }
- else {
- close(1); open("/dev/null", O_WRONLY);
- dup2(1, 2);
- }
- if (stat("/stand/sh", &sb) == 0)
- execl("/stand/sh", "/stand/sh", "-c", cmd, (char *)NULL);
- else
- execl("/bin/sh", "/bin/sh", "-c", cmd, (char *)NULL);
- exit(1);
- }
- else {
- intsave = signal(SIGINT, SIG_IGN);
- quitsave = signal(SIGQUIT, SIG_IGN);
- pid = waitpid(pid, &pstat, 0);
- (void)sigsetmask(omask);
- (void)signal(SIGINT, intsave);
- (void)signal(SIGQUIT, quitsave);
- i = (pid == -1) ? -1 : WEXITSTATUS(pstat);
- if (isDebug())
- msgDebug("Command `%s' returns status of %d\n", cmd, i);
- }
- return i;
-}
-
diff --git a/usr.sbin/sade/termcap.c b/usr.sbin/sade/termcap.c
deleted file mode 100644
index 1c2f39b..0000000
--- a/usr.sbin/sade/termcap.c
+++ /dev/null
@@ -1,104 +0,0 @@
-/*
- * Copyright (c) 1994, Paul Richards.
- *
- * All rights reserved.
- *
- * This software may be used, modified, copied, distributed, and sold, in both
- * source and binary form provided that the above copyright and these terms
- * are retained, verbatim, as the first lines of this file. Under no
- * circumstances is the author responsible for the proper functioning of this
- * software, nor does the author assume any responsibility for damages
- * incurred with its use.
- *
- * $FreeBSD$
- */
-
-#include "sade.h"
-#include <stdarg.h>
-#include <fcntl.h>
-#include <sys/errno.h>
-#include <sys/ioctl.h>
-#include <sys/consio.h>
-
-#define VTY_STATUS_LINE 24
-#define TTY_STATUS_LINE 23
-
-static void
-prompt_term(char **termp)
-{
- char str[80];
-
- printf("\nPlease set your TERM variable before running this program.\n");
- printf("Defaulting to an ANSI compatible terminal - please press RETURN\n");
- fgets(str, sizeof(str), stdin); /* Just to make it interactive */
- *termp = (char *)"ansi";
-}
-
-int
-set_termcap(void)
-{
- char *term;
- int stat;
- struct winsize ws;
-
- term = getenv("TERM");
- stat = ioctl(STDERR_FILENO, GIO_COLOR, &ColorDisplay);
-
- if (isDebug())
- DebugFD = open("sade.debug", O_WRONLY|O_CREAT|O_TRUNC, 0644);
- else
- DebugFD = -1;
- if (DebugFD < 0)
- DebugFD = open("/dev/null", O_RDWR, 0);
-
- if (!OnVTY || (stat < 0)) {
- if (!term) {
- char *term;
-
- prompt_term(&term);
- if (setenv("TERM", term, 1) < 0)
- return -1;
- }
- if (DebugFD < 0)
- DebugFD = open("/dev/null", O_RDWR, 0);
- }
- else {
- int i, on;
-
- if (getpid() == 1) {
- DebugFD = open("/dev/ttyv1", O_WRONLY);
- if (DebugFD != -1) {
- on = 1;
- i = ioctl(DebugFD, TIOCCONS, (char *)&on);
- msgDebug("ioctl(%d, TIOCCONS, NULL) = %d (%s)\n",
- DebugFD, i, !i ? "success" : strerror(errno));
- }
- }
-
-#ifdef PC98
- if (!term) {
- if (setenv("TERM", "cons25w", 1) < 0)
- return -1;
- }
-#else
- if (ColorDisplay) {
- if (!term) {
- if (setenv("TERM", "xterm", 1) < 0)
- return -1;
- }
- }
- else {
- if (!term) {
- if (setenv("TERM", "vt100", 1) < 0)
- return -1;
- }
- }
-#endif
- }
- if (ioctl(0, TIOCGWINSZ, &ws) == -1) {
- msgDebug("Unable to get terminal size - errno %d\n", errno);
- ws.ws_row = 0;
- }
- StatusLine = ws.ws_row ? ws.ws_row - 1: (OnVTY ? VTY_STATUS_LINE : TTY_STATUS_LINE);
- return 0;
-}
diff --git a/usr.sbin/sade/variable.c b/usr.sbin/sade/variable.c
deleted file mode 100644
index ae3a6e0..0000000
--- a/usr.sbin/sade/variable.c
+++ /dev/null
@@ -1,324 +0,0 @@
-/*
- * $FreeBSD$
- *
- * Copyright (c) 1995
- * Jordan Hubbard. All rights reserved.
- * Copyright (c) 2001
- * Murray Stokely. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer,
- * verbatim and that no modifications are made prior to this
- * point in the file.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY JORDAN HUBBARD ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL JORDAN HUBBARD OR HIS PETS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, LIFE OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- *
- */
-
-#include "sade.h"
-
-/* Routines for dealing with variable lists */
-
-static void
-make_variable(char *var, char *value, int dirty)
-{
- Variable *vp;
-
- /* Trim leading and trailing whitespace */
- var = string_skipwhite(string_prune(var));
-
- if (!var || !*var)
- return;
-
-
- /* Now search to see if it's already in the list */
- for (vp = VarHead; vp; vp = vp->next) {
- if (!strcmp(vp->name, var)) {
- if (vp->dirty && !dirty)
- return;
- setenv(var, value, 1);
- free(vp->value);
- vp->value = strdup(value);
- if (dirty != -1)
- vp->dirty = dirty;
- return;
- }
- }
-
- setenv(var, value, 1);
- /* No? Create a new one */
- vp = (Variable *)safe_malloc(sizeof(Variable));
- vp->name = strdup(var);
- vp->value = strdup(value);
- if (dirty == -1)
- dirty = 0;
- vp->dirty = dirty;
- vp->next = VarHead;
- VarHead = vp;
-}
-
-void
-variable_set(char *var, int dirty)
-{
- char tmp[1024], *cp;
-
- if (!var)
- msgFatal("NULL variable name & value passed.");
- else if (!*var)
- msgDebug("Warning: Zero length name & value passed to variable_set()\n");
- SAFE_STRCPY(tmp, var);
- if ((cp = strchr(tmp, '=')) == NULL)
- msgFatal("Invalid variable format: %s", var);
- *(cp++) = '\0';
- make_variable(tmp, string_skipwhite(cp), dirty);
-}
-
-void
-variable_set2(char *var, char *value, int dirty)
-{
- if (!var || !value)
- msgFatal("Null name or value passed to set_variable2(%s) = %s!",
- var ? var : "", value ? value : "");
- else if (!*var || !*value)
- msgDebug("Warning: Zero length name or value passed to variable_set2(%s) = %s\n",
- var, value);
- make_variable(var, value, dirty);
-}
-
-char *
-variable_get(char *var)
-{
- return getenv(var);
-}
-
-int
-variable_cmp(char *var, char *value)
-{
- char *val;
-
- if ((val = variable_get(var)))
- return strcmp(val, value);
- return -1;
-}
-
-void
-variable_unset(char *var)
-{
- Variable *vp;
- char name[512], *cp;
-
- if ((cp = strchr(var, '=')) != NULL)
- sstrncpy(name, var, cp - var);
- else
- SAFE_STRCPY(name, var);
- unsetenv(name);
- /* Now search to see if it's in our list, if we have one.. */
- if (!VarHead)
- return;
- else if (!VarHead->next && !strcmp(VarHead->name, name)) {
- safe_free(VarHead->name);
- safe_free(VarHead->value);
- free(VarHead);
- VarHead = NULL;
- }
- else {
- for (vp = VarHead; vp; vp = vp->next) {
- if (!strcmp(vp->name, name)) {
- Variable *save = vp->next;
-
- safe_free(vp->name);
- safe_free(vp->value);
- *vp = *save;
- safe_free(save);
- break;
- }
- }
- }
-}
-
-/* Prompt user for the name of a variable */
-char *
-variable_get_value(char *var, char *prompt, int dirty)
-{
- char *cp;
-
- cp = variable_get(var);
- if (cp && variable_get(VAR_NONINTERACTIVE))
- return cp;
- else if ((cp = msgGetInput(cp, "%s", prompt)) != NULL)
- variable_set2(var, cp, dirty);
- else
- cp = NULL;
- return cp;
-}
-
-/* Check if value passed in data (in the form "variable=value") is
- * valid, and it's status compared to the value of variable stored in
- * env
- *
- * Possible return values :
- * -3: Invalid line, the data string is NOT set as an env variable
- * -2: Invalid line, the data string is set as an env variable
- * -1: Invalid line
- * 0: Valid line, is NOT equal to env version
- * 1: Valid line, is equal to env version
- * 2: Valid line, value empty - e.g. foo=""
- * 3: Valid line, does not exist in env
-*/
-int
-variable_check2(char *data)
-{
- char *cp, *cp2, *cp3, tmp[256];
-
- if (data == NULL)
- return -1;
- SAFE_STRCPY(tmp, data);
- if ((cp = strchr(tmp, '=')) != NULL) {
- *(cp++) = '\0';
- if (*cp == '"') { /* smash quotes if present */
- ++cp;
- if ((cp3 = strchr(cp, '"')) != NULL)
- *cp3 = '\0';
- }
- else if ((cp3 = strchr(cp, ',')) != NULL)
- *cp3 = '\0';
- cp2 = variable_get(tmp);
- if (cp2 != NULL) {
- if (*cp == '\0')
- return 2;
- else
- return strcmp(cp, cp2) == 0 ? 1 : 0;
- }
- else
- return 3;
- }
- else
- return variable_get(tmp) != NULL ? -2 : -3;
-}
-
-/* Check if the value passed in data (in the form "variable=value") is
- equal to the value of variable stored in env */
-int
-variable_check(char *data)
-{
- int ret;
- ret = variable_check2(data);
-
- switch(ret) {
- case -2:
- case 1:
- case 2:
- return TRUE;
- /* NOT REACHED */
- default:
- return FALSE;
- }
-}
-
-int
-dump_variables(dialogMenuItem *unused)
-{
- FILE *fp;
- Variable *vp;
-
- if (isDebug())
- msgDebug("Writing %s variables to file..\n", ProgName);
-
- fp = fopen("/etc/sade.vars", "w");
- if (!fp) {
- msgConfirm("Unable to write to /etc/%s.vars: %s",
- ProgName, strerror(errno));
- return DITEM_FAILURE;
- }
-
- for (vp = VarHead; vp; vp = vp->next)
- fprintf(fp, "%s=\"%s\" (%d)\n", vp->name, vp->value, vp->dirty);
-
- fclose(fp);
-
- return DITEM_SUCCESS;
-}
-
-/* Free all of the variables, useful to really start over as when the
- user selects "restart" from the interrupt menu. */
-void
-free_variables(void)
-{
- Variable *vp;
-
- /* Free the variables from our list, if we have one.. */
- if (!VarHead)
- return;
- else if (!VarHead->next) {
- unsetenv(VarHead->name);
- safe_free(VarHead->name);
- safe_free(VarHead->value);
- free(VarHead);
- VarHead = NULL;
- }
- else {
- for (vp = VarHead; vp; ) {
- Variable *save = vp;
- unsetenv(vp->name);
- safe_free(vp->name);
- safe_free(vp->value);
- vp = vp->next;
- safe_free(save);
- }
- VarHead = NULL;
- }
-}
-
-/*
- * Persistent variables. The variables modified by these functions
- * are not cleared between invocations of sysinstall. This is useful
- * to allow the user to completely restart sysinstall, without having
- * it load all of the modules again from the installation media which
- * are still in memory.
- */
-
-void
-pvariable_set(char *var)
-{
- char *p;
- char tmp[1024];
-
- if (!var)
- msgFatal("NULL variable name & value passed.");
- else if (!*var)
- msgDebug("Warning: Zero length name & value passed to variable_set()\n");
- /* Add a trivial namespace to whatever name the caller chooses. */
- SAFE_STRCPY(tmp, "SYSINSTALL_PVAR");
- if (strchr(var, '=') == NULL)
- msgFatal("Invalid variable format: %s", var);
- strlcat(tmp, var, 1024);
- p = strchr(tmp, '=');
- *p = '\0';
- setenv(tmp, p + 1, 1);
-}
-
-char *
-pvariable_get(char *var)
-{
- char tmp[1024];
-
- SAFE_STRCPY(tmp, "SYSINSTALL_PVAR");
- strlcat(tmp, var, 1024);
- return getenv(tmp);
-}
diff --git a/usr.sbin/sade/wizard.c b/usr.sbin/sade/wizard.c
deleted file mode 100644
index 5edd872..0000000
--- a/usr.sbin/sade/wizard.c
+++ /dev/null
@@ -1,200 +0,0 @@
-/*
- * ----------------------------------------------------------------------------
- * "THE BEER-WARE LICENSE" (Revision 42):
- * <phk@FreeBSD.org> wrote this file. As long as you retain this notice you
- * can do whatever you want with this stuff. If we meet some day, and you think
- * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
- * ----------------------------------------------------------------------------
- *
- * $FreeBSD$
- */
-
-#include "sade.h"
-#include <fcntl.h>
-#include <err.h>
-#include <libdisk.h>
-
-static int
-scan_block(int fd, daddr_t block)
-{
- u_char foo[512];
-
- if (-1 == lseek(fd,block * 512,SEEK_SET))
- err(1,"lseek");
- if (512 != read(fd,foo, 512))
- return 1;
- return 0;
-}
-
-static void
-Scan_Disk(Disk *d)
-{
- char device[64];
- u_long l;
- int i,j,fd;
-
- strcpy(device,"/dev/");
- strcat(device,d->name);
-
- fd = open(device,O_RDWR);
- if (fd < 0) {
- msgWarn("open(%s) failed", device);
- return;
- }
- for(i=-1,l=0;;l++) {
- j = scan_block(fd,l);
- if (j != i) {
- if (i == -1) {
- printf("%c: %lu.",j ? 'B' : 'G', l);
- fflush(stdout);
- } else if (i == 0) {
- printf(".%lu\nB: %lu.",l-1,l);
- fflush(stdout);
- } else {
- printf(".%lu\nG: %lu.",l-1,l);
- fflush(stdout);
- }
- i = j;
- }
- }
- close(fd);
-}
-
-void
-slice_wizard(Disk *d)
-{
- Disk *db;
- char myprompt[BUFSIZ];
- char input[BUFSIZ];
- char *p,*q=0;
- char **cp,*cmds[200];
- int ncmd,i;
-
- systemSuspendDialog();
- sprintf(myprompt,"%s> ", d->name);
- while(1) {
- printf("--==##==--\n");
- Debug_Disk(d);
- p = CheckRules(d);
- if (p) {
- printf("%s",p);
- free(p);
- }
- printf("%s", myprompt);
- fflush(stdout);
- q = p = fgets(input,sizeof(input),stdin);
- if(!p)
- break;
- for(cp = cmds; (*cp = strsep(&p, " \t\n")) != NULL;)
- if (**cp != '\0')
- cp++;
- ncmd = cp - cmds;
- if(!ncmd)
- continue;
- if (!strcasecmp(*cmds,"quit")) { break; }
- if (!strcasecmp(*cmds,"exit")) { break; }
- if (!strcasecmp(*cmds,"q")) { break; }
- if (!strcasecmp(*cmds,"x")) { break; }
- if (!strcasecmp(*cmds,"delete") && ncmd == 2) {
- printf("delete = %d\n",
- Delete_Chunk(d,
- (struct chunk *)strtol(cmds[1],0,0)));
- continue;
- }
- if (!strcasecmp(*cmds,"allfreebsd")) {
- All_FreeBSD(d, 0);
- continue;
- }
- if (!strcasecmp(*cmds,"dedicate")) {
- All_FreeBSD(d, 1);
- continue;
- }
- if (!strcasecmp(*cmds,"bios") && ncmd == 4) {
- Set_Bios_Geom(d,
- strtol(cmds[1],0,0),
- strtol(cmds[2],0,0),
- strtol(cmds[3],0,0));
- continue;
- }
- if (!strcasecmp(*cmds,"list")) {
- cp = Disk_Names();
- printf("Disks:");
- for(i=0;cp[i];i++) {
- printf(" %s",cp[i]);
- free(cp[i]);
- }
- free(cp);
- continue;
- }
-#ifdef PC98
- if (!strcasecmp(*cmds,"create") && ncmd == 7) {
- printf("Create=%d\n",
- Create_Chunk(d,
- strtol(cmds[1],0,0),
- strtol(cmds[2],0,0),
- strtol(cmds[3],0,0),
- strtol(cmds[4],0,0),
- strtol(cmds[5],0,0),
- cmds[6]));
- continue;
- }
-#else
- if (!strcasecmp(*cmds,"create") && ncmd == 6) {
- printf("Create=%d\n",
- Create_Chunk(d,
- strtol(cmds[1],0,0),
- strtol(cmds[2],0,0),
- strtol(cmds[3],0,0),
- strtol(cmds[4],0,0),
- strtol(cmds[5],0,0), ""));
- continue;
- }
-#endif
- if (!strcasecmp(*cmds,"read")) {
- db = d;
- if (ncmd > 1)
- d = Open_Disk(cmds[1]);
- else
- d = Open_Disk(d->name);
- if (d)
- Free_Disk(db);
- else
- d = db;
- continue;
- }
- if (!strcasecmp(*cmds,"scan")) {
- Scan_Disk(d);
- continue;
- }
- if (!strcasecmp(*cmds,"write")) {
- printf("Write=%d\n",
- Fake ? 0 : Write_Disk(d));
- q = strdup(d->name);
- Free_Disk(d);
- d = Open_Disk(q);
- continue;
- }
- if (strcasecmp(*cmds,"help"))
- printf("\007ERROR\n");
- printf("CMDS:\n");
- printf("allfreebsd\t\t");
- printf("dedicate\t\t");
- printf("bios cyl hd sect\n");
- printf("collapse [pointer]\t\t");
-#ifdef PC98
- printf("create offset size enum subtype flags name\n");
-#else
- printf("create offset size enum subtype flags\n");
-#endif
- printf("subtype(part): swap=1, ffs=7\t\t");
- printf("delete pointer\n");
- printf("list\t\t");
- printf("quit\n");
- printf("read [disk]\t\t");
- printf("scan\n");
- printf("write\t\t");
- printf("\n");
-
- }
- systemResumeDialog();
-}
diff --git a/usr.sbin/service/service.8 b/usr.sbin/service/service.8
index 10f2e8f..0c2b2d3 100644
--- a/usr.sbin/service/service.8
+++ b/usr.sbin/service/service.8
@@ -24,7 +24,7 @@
.\"
.\" $FreeBSD$
.\"
-.Dd December 26, 2009
+.Dd December 11, 2012
.Dt SERVICE 8
.Os
.Sh NAME
@@ -34,6 +34,8 @@
.Nm
.Fl e
.Nm
+.Fl R
+.Nm
.Op Fl v
.Fl l | r
.Nm
@@ -62,6 +64,8 @@ then that list of scripts is checked for an
.Qq rcvar
assignment.
If present the script is checked to see if it is enabled.
+.It Fl R
+Restart all enabled local services.
.It Fl l
List all files in
.Pa /etc/rc.d
diff --git a/usr.sbin/service/service.sh b/usr.sbin/service/service.sh
index 1794315..cf181fa 100755
--- a/usr.sbin/service/service.sh
+++ b/usr.sbin/service/service.sh
@@ -33,29 +33,58 @@ usage () {
echo ''
echo 'Usage:'
echo "${0##*/} -e"
+ echo "${0##*/} -R"
echo "${0##*/} [-v] -l | -r"
echo "${0##*/} [-v] <rc.d script> start|stop|etc."
echo "${0##*/} -h"
echo ''
echo '-e Show services that are enabled'
+ echo "-R Stop and start enabled $local_startup services"
echo "-l List all scripts in /etc/rc.d and $local_startup"
echo '-r Show the results of boot time rcorder'
echo '-v Verbose'
echo ''
}
-while getopts 'ehlrv' COMMAND_LINE_ARGUMENT ; do
+while getopts 'ehlrRv' COMMAND_LINE_ARGUMENT ; do
case "${COMMAND_LINE_ARGUMENT}" in
e) ENABLED=eopt ;;
h) usage ; exit 0 ;;
l) LIST=lopt ;;
r) RCORDER=ropt ;;
+ R) RESTART=Ropt ;;
v) VERBOSE=vopt ;;
*) usage ; exit 1 ;;
esac
done
shift $(( $OPTIND - 1 ))
+if [ -n "$RESTART" ]; then
+ skip="-s nostart"
+ if [ `/sbin/sysctl -n security.jail.jailed` -eq 1 ]; then
+ skip="$skip -s nojail"
+ fi
+ [ -n "$local_startup" ] && find_local_scripts_new
+ files=`rcorder ${skip} ${local_rc} 2>/dev/null`
+
+ for file in `reverse_list ${files}`; do
+ if grep -q ^rcvar $file; then
+ eval `grep ^name= $file`
+ eval `grep ^rcvar $file`
+ checkyesno $rcvar 2>/dev/null && run_rc_script ${file} stop
+ fi
+ done
+ for file in $files; do
+ if grep -q ^rcvar $file; then
+ eval `grep ^name= $file`
+ eval `grep ^rcvar $file`
+ checkyesno $rcvar 2>/dev/null && run_rc_script ${file} start
+ fi
+ done
+
+ exit 0
+fi
+
if [ -n "$ENABLED" -o -n "$RCORDER" ]; then
# Copied from /etc/rc
skip="-s nostart"
diff --git a/usr.sbin/syslogd/syslogd.c b/usr.sbin/syslogd/syslogd.c
index fff7e4c..bb56b6b 100644
--- a/usr.sbin/syslogd/syslogd.c
+++ b/usr.sbin/syslogd/syslogd.c
@@ -1873,6 +1873,7 @@ cfline(const char *line, struct filed *f, const char *prog, const char *host)
pri = decode(buf, prioritynames);
if (pri < 0) {
+ errno = 0;
(void)snprintf(ebuf, sizeof ebuf,
"unknown priority name \"%s\"", buf);
logerror(ebuf);
@@ -1901,6 +1902,7 @@ cfline(const char *line, struct filed *f, const char *prog, const char *host)
} else {
i = decode(buf, facilitynames);
if (i < 0) {
+ errno = 0;
(void)snprintf(ebuf, sizeof ebuf,
"unknown facility name \"%s\"",
buf);
@@ -2687,6 +2689,7 @@ socksetup(int af, char *bindhostname)
logerror("socket");
continue;
}
+#ifdef INET6
if (r->ai_family == AF_INET6) {
if (setsockopt(*s, IPPROTO_IPV6, IPV6_V6ONLY,
(char *)&on, sizeof (on)) < 0) {
@@ -2695,6 +2698,7 @@ socksetup(int af, char *bindhostname)
continue;
}
}
+#endif
if (setsockopt(*s, SOL_SOCKET, SO_REUSEADDR,
(char *)&on, sizeof (on)) < 0) {
logerror("setsockopt");
@@ -2711,8 +2715,8 @@ socksetup(int af, char *bindhostname)
*/
if (!NoBind) {
if (bind(*s, r->ai_addr, r->ai_addrlen) < 0) {
- close(*s);
logerror("bind");
+ close(*s);
continue;
}
diff --git a/usr.sbin/sysrc/Makefile b/usr.sbin/sysrc/Makefile
index 38c52ee..ca3dc56 100644
--- a/usr.sbin/sysrc/Makefile
+++ b/usr.sbin/sysrc/Makefile
@@ -1,7 +1,5 @@
# $FreeBSD$
-NO_OBJ=
-
SCRIPTS= sysrc
MAN= sysrc.8
diff --git a/usr.sbin/tcpdrop/tcpdrop.8 b/usr.sbin/tcpdrop/tcpdrop.8
index 93bfe8e..9649dec 100644
--- a/usr.sbin/tcpdrop/tcpdrop.8
+++ b/usr.sbin/tcpdrop/tcpdrop.8
@@ -17,7 +17,7 @@
.\"
.\" $FreeBSD$
.\"
-.Dd March 24, 2009
+.Dd January 30, 2013
.Dt TCPDROP 8
.Os
.Sh NAME
@@ -62,6 +62,9 @@ will be dropped.
.Pp
Addresses and ports may be specified by name or numeric value.
Both IPv4 and IPv6 address formats are supported.
+.Pp
+The addresses and ports may be separated by periods or colons
+instead of spaces.
.Sh EXIT STATUS
.Ex -std
.Sh EXAMPLES
diff --git a/usr.sbin/tcpdrop/tcpdrop.c b/usr.sbin/tcpdrop/tcpdrop.c
index dce6c6d..ef3f6bd 100644
--- a/usr.sbin/tcpdrop/tcpdrop.c
+++ b/usr.sbin/tcpdrop/tcpdrop.c
@@ -50,6 +50,7 @@ struct host_service {
static bool tcpdrop_list_commands = false;
+static char *findport(const char *);
static struct xinpgen *getxpcblist(const char *);
static void sockinfo(const struct sockaddr *, struct host_service *);
static bool tcpdrop(const struct sockaddr *, const struct sockaddr *);
@@ -65,6 +66,7 @@ static void usage(void);
int
main(int argc, char *argv[])
{
+ char *lport, *fport;
bool dropall;
int ch;
@@ -93,15 +95,43 @@ main(int argc, char *argv[])
exit(0);
}
- if (argc != 4 || tcpdrop_list_commands)
+ if ((argc != 2 && argc != 4) || tcpdrop_list_commands)
usage();
- if (!tcpdropbyname(argv[0], argv[1], argv[2], argv[3]))
+ if (argc == 2) {
+ lport = findport(argv[0]);
+ fport = findport(argv[1]);
+ if (lport == NULL || lport[1] == '\0' || fport == NULL ||
+ fport[1] == '\0')
+ usage();
+ *lport++ = '\0';
+ *fport++ = '\0';
+ if (!tcpdropbyname(argv[0], lport, argv[1], fport))
+ exit(1);
+ } else if (!tcpdropbyname(argv[0], argv[1], argv[2], argv[3]))
exit(1);
exit(0);
}
+static char *
+findport(const char *arg)
+{
+ char *dot, *colon;
+
+ /* A strrspn() or strrpbrk() would be nice. */
+ dot = strrchr(arg, '.');
+ colon = strrchr(arg, ':');
+ if (dot == NULL)
+ return (colon);
+ if (colon == NULL)
+ return (dot);
+ if (dot < colon)
+ return (colon);
+ else
+ return (dot);
+}
+
static struct xinpgen *
getxpcblist(const char *name)
{
@@ -237,7 +267,7 @@ tcpdropbyname(const char *lhost, const char *lport, const char *fhost,
error = getaddrinfo(fhost, fport, &hints, &foreign);
if (error != 0) {
freeaddrinfo(local); /* XXX gratuitous */
- errx(1, "getaddrinfo: %s port %s: %s", lhost, lport,
+ errx(1, "getaddrinfo: %s port %s: %s", fhost, fport,
gai_strerror(error));
}
@@ -318,6 +348,8 @@ usage(void)
{
fprintf(stderr,
"usage: tcpdrop local-address local-port foreign-address foreign-port\n"
+" tcpdrop local-address:local-port foreign-address:foreign-port\n"
+" tcpdrop local-address.local-port foreign-address.foreign-port\n"
" tcpdrop [-l] -a\n");
exit(1);
}
diff --git a/usr.sbin/timed/timed/acksend.c b/usr.sbin/timed/timed/acksend.c
index fde1404..8492f9e 100644
--- a/usr.sbin/timed/timed/acksend.c
+++ b/usr.sbin/timed/timed/acksend.c
@@ -42,10 +42,7 @@ struct tsp *answer;
extern u_short sequence;
void
-xmit(type, seq, addr)
- int type;
- u_int seq;
- struct sockaddr_in *addr;
+xmit(int type, u_int seq, struct sockaddr_in *addr)
{
static struct tsp msg;
@@ -68,15 +65,15 @@ xmit(type, seq, addr)
*
* Because this function calls readmsg(), none of its args may be in
* a message provided by readmsg().
+ * message this message
+ * addr to here
+ * ack look for this ack
+ * net receive from this network
+ * bad 1=losing patience
*/
struct tsp *
-acksend(message, addr, name, ack, net, bad)
- struct tsp *message; /* this message */
- struct sockaddr_in *addr; /* to here */
- char *name;
- int ack; /* look for this ack */
- struct netinfo *net; /* receive from this network */
- int bad; /* 1=losing patience */
+acksend(struct tsp *message, struct sockaddr_in *addr, char *name,
+ int ack, struct netinfo *net, int bad)
{
struct timeval twait;
int count;
diff --git a/usr.sbin/timed/timed/byteorder.c b/usr.sbin/timed/timed/byteorder.c
index a9fd151..abacbd6 100644
--- a/usr.sbin/timed/timed/byteorder.c
+++ b/usr.sbin/timed/timed/byteorder.c
@@ -42,8 +42,7 @@ static const char rcsid[] =
* messages. Protocol is defined in /usr/include/protocols/timed.h
*/
void
-bytenetorder(ptr)
- struct tsp *ptr;
+bytenetorder(struct tsp *ptr)
{
ptr->tsp_seq = htons((u_short)ptr->tsp_seq);
switch (ptr->tsp_type) {
@@ -62,8 +61,7 @@ bytenetorder(ptr)
}
void
-bytehostorder(ptr)
- struct tsp *ptr;
+bytehostorder(struct tsp *ptr)
{
ptr->tsp_seq = ntohs((u_short)ptr->tsp_seq);
switch (ptr->tsp_type) {
diff --git a/usr.sbin/timed/timed/candidate.c b/usr.sbin/timed/timed/candidate.c
index 7eae18e..c92ae70 100644
--- a/usr.sbin/timed/timed/candidate.c
+++ b/usr.sbin/timed/timed/candidate.c
@@ -44,8 +44,7 @@ static const char rcsid[] =
* candidate sends an election request, the candidature is withdrawn.
*/
int
-election(net)
- struct netinfo *net;
+election(struct netinfo *net)
{
struct tsp *resp, msg;
struct timeval then, wait;
diff --git a/usr.sbin/timed/timed/cksum.c b/usr.sbin/timed/timed/cksum.c
index 243ea38..7fa0a5e 100644
--- a/usr.sbin/timed/timed/cksum.c
+++ b/usr.sbin/timed/timed/cksum.c
@@ -49,9 +49,7 @@ static const char rcsid[] =
* worry about carries except at the end.
*/
int
-in_cksum(addr, len)
- u_short *addr;
- int len;
+in_cksum(u_short *addr, int len)
{
register int nleft = len;
register u_short *w = addr;
diff --git a/usr.sbin/timed/timed/correct.c b/usr.sbin/timed/timed/correct.c
index 0eab083..8a5b464 100644
--- a/usr.sbin/timed/timed/correct.c
+++ b/usr.sbin/timed/timed/correct.c
@@ -47,8 +47,7 @@ static void adjclock(struct timeval *);
* own
*/
void
-correct(avdelta)
- long avdelta;
+correct(long avdelta)
{
struct hosttbl *htp;
int corr;
@@ -112,8 +111,7 @@ correct(avdelta)
static void
-adjclock(corr)
- struct timeval *corr;
+adjclock(struct timeval *corr)
{
static int passes = 0;
static int smoother = 0;
@@ -176,9 +174,7 @@ adjclock(corr)
* spent in the queue
*/
void
-adj_msg_time(msg, now)
- struct tsp *msg;
- struct timeval *now;
+adj_msg_time(struct tsp *msg, struct timeval *now)
{
msg->tsp_time.tv_sec += (now->tv_sec - from_when.tv_sec);
msg->tsp_time.tv_usec += (now->tv_usec - from_when.tv_usec);
diff --git a/usr.sbin/timed/timed/master.c b/usr.sbin/timed/timed/master.c
index b64259b..9a130bf 100644
--- a/usr.sbin/timed/timed/master.c
+++ b/usr.sbin/timed/timed/master.c
@@ -63,7 +63,7 @@ static void mchgdate(struct tsp *);
* takes the appropriate action.
*/
int
-master()
+master(void)
{
struct hosttbl *htp;
long pollingtime;
@@ -339,8 +339,7 @@ loop:
* change the system date on the master
*/
static void
-mchgdate(msg)
- struct tsp *msg;
+mchgdate(struct tsp *msg)
{
char tname[MAXHOSTNAMELEN];
char olddate[32];
@@ -386,8 +385,7 @@ mchgdate(msg)
* synchronize all of the slaves
*/
void
-synch(mydelta)
- long mydelta;
+synch(long mydelta)
{
struct hosttbl *htp;
int measure_status;
@@ -460,7 +458,7 @@ synch(mydelta)
* has received the command to set the network time
*/
void
-spreadtime()
+spreadtime(void)
{
struct hosttbl *htp;
struct tsp to;
@@ -499,8 +497,7 @@ spreadtime()
}
void
-prthp(delta)
- clock_t delta;
+prthp(clock_t delta)
{
static time_t next_time;
time_t this_time;
@@ -538,8 +535,7 @@ static struct hosttbl *lasthfree = &hosttbl[0];
struct hosttbl * /* answer or 0 */
-findhost(name)
- char *name;
+findhost(char *name)
{
int i, j;
struct hosttbl *htp;
@@ -565,10 +561,7 @@ findhost(name)
* add a host to the list of controlled machines if not already there
*/
struct hosttbl *
-addmach(name, addr, ntp)
- char *name;
- struct sockaddr_in *addr;
- struct netinfo *ntp;
+addmach(char *name, struct sockaddr_in *addr, struct netinfo *ntp)
{
struct hosttbl *ret, *p, *b, *f;
@@ -654,8 +647,7 @@ addmach(name, addr, ntp)
* remove the machine with the given index in the host table.
*/
struct hosttbl *
-remmach(htp)
- struct hosttbl *htp;
+remmach(struct hosttbl *htp)
{
struct hosttbl *lprv, *hnxt, *f, *b;
@@ -706,8 +698,7 @@ remmach(htp)
* given network.
*/
void
-rmnetmachs(ntp)
- struct netinfo *ntp;
+rmnetmachs(struct netinfo *ntp)
{
struct hosttbl *htp;
@@ -722,8 +713,7 @@ rmnetmachs(ntp)
}
void
-masterup(net)
- struct netinfo *net;
+masterup(struct netinfo *net)
{
xmit(TSP_MASTERUP, 0, &net->dest_addr);
@@ -736,8 +726,7 @@ masterup(net)
}
void
-newslave(msg)
- struct tsp *msg;
+newslave(struct tsp *msg)
{
struct hosttbl *htp;
struct tsp *answer, to;
@@ -782,8 +771,7 @@ newslave(msg)
* react to a TSP_QUIT:
*/
void
-doquit(msg)
- struct tsp *msg;
+doquit(struct tsp *msg)
{
if (fromnet->status == MASTER) {
if (!good_host_name(msg->tsp_name)) {
@@ -814,7 +802,7 @@ doquit(msg)
}
void
-traceon()
+traceon(void)
{
if (!fd) {
fd = fopen(_PATH_TIMEDLOG, "w");
@@ -832,8 +820,7 @@ traceon()
void
-traceoff(msg)
- char *msg;
+traceoff(char *msg)
{
get_goodgroup(1);
setstatus();
diff --git a/usr.sbin/timed/timed/measure.c b/usr.sbin/timed/timed/measure.c
index 81ab7c4..8fe43b5 100644
--- a/usr.sbin/timed/timed/measure.c
+++ b/usr.sbin/timed/timed/measure.c
@@ -56,14 +56,12 @@ static n_short seqno = 0;
/*
* Measures the differences between machines' clocks using
* ICMP timestamp messages.
+ * maxmsec wait this many msec at most
+ * wmsec msec to wait for an answer
+ * print print complaints on stderr
*/
int /* status val defined in globals.h */
-measure(maxmsec, wmsec, hname, addr, print)
- u_long maxmsec; /* wait this many msec at most */
- u_long wmsec; /* msec to wait for an answer */
- char *hname;
- struct sockaddr_in *addr;
- int print; /* print complaints on stderr */
+measure(u_long maxmsec, u_long wmsec, char *hname, struct sockaddr_in *addr, int print)
{
int length;
int measure_status;
@@ -292,9 +290,7 @@ quit:
* round a number of milliseconds into a struct timeval
*/
void
-mstotvround(res, x)
- struct timeval *res;
- long x;
+mstotvround(struct timeval *res, long x)
{
if (x < 0)
x = -((-x + 3)/5);
@@ -310,8 +306,7 @@ mstotvround(res, x)
}
void
-timevaladd(tv1, tv2)
- struct timeval *tv1, *tv2;
+timevaladd(struct timeval *tv1, struct timeval *tv2)
{
tv1->tv_sec += tv2->tv_sec;
tv1->tv_usec += tv2->tv_usec;
@@ -326,8 +321,7 @@ timevaladd(tv1, tv2)
}
void
-timevalsub(res, tv1, tv2)
- struct timeval *res, *tv1, *tv2;
+timevalsub(struct timeval *res, struct timeval *tv1, struct timeval *tv2)
{
res->tv_sec = tv1->tv_sec - tv2->tv_sec;
res->tv_usec = tv1->tv_usec - tv2->tv_usec;
diff --git a/usr.sbin/timed/timed/networkdelta.c b/usr.sbin/timed/timed/networkdelta.c
index e9a437a..bf2a353 100644
--- a/usr.sbin/timed/timed/networkdelta.c
+++ b/usr.sbin/timed/timed/networkdelta.c
@@ -62,7 +62,7 @@ static long median(float, float *, long *, long *, unsigned int);
* bad values.
*/
long
-networkdelta()
+networkdelta(void)
{
struct hosttbl *htp;
long med;
diff --git a/usr.sbin/timed/timed/readmsg.c b/usr.sbin/timed/timed/readmsg.c
index aca2dbe..8168612 100644
--- a/usr.sbin/timed/timed/readmsg.c
+++ b/usr.sbin/timed/timed/readmsg.c
@@ -69,11 +69,7 @@ struct timeval from_when;
*/
struct tsp *
-readmsg(type, machfrom, intvl, netfrom)
- int type;
- char *machfrom;
- struct timeval *intvl;
- struct netinfo *netfrom;
+readmsg(int type, char *machfrom, struct timeval *intvl, struct netinfo *netfrom)
{
int length;
fd_set ready;
@@ -342,7 +338,7 @@ again:
* only the type ACK is to be sent by a slave
*/
void
-slaveack()
+slaveack(void)
{
switch(msgin.tsp_type) {
@@ -374,7 +370,7 @@ slaveack()
* These packets should be acknowledged.
*/
void
-ignoreack()
+ignoreack(void)
{
switch(msgin.tsp_type) {
@@ -402,7 +398,7 @@ ignoreack()
* to the messages received by a master
*/
void
-masterack()
+masterack(void)
{
struct tsp resp;
@@ -445,9 +441,7 @@ masterack()
* Print a TSP message
*/
void
-print(msg, addr)
- struct tsp *msg;
- struct sockaddr_in *addr;
+print(struct tsp *msg, struct sockaddr_in *addr)
{
char tm[26];
time_t tsp_time_sec;
diff --git a/usr.sbin/timed/timed/slave.c b/usr.sbin/timed/timed/slave.c
index 26e34f7..15c0c43 100644
--- a/usr.sbin/timed/timed/slave.c
+++ b/usr.sbin/timed/timed/slave.c
@@ -55,7 +55,7 @@ static void setmaster(struct tsp *);
static void answerdelay(void);
int
-slave()
+slave(void)
{
int tries;
long electiontime, refusetime, looktime, looptime, adjtime;
@@ -610,8 +610,7 @@ loop:
* tell the world who our master is
*/
static void
-setmaster(msg)
- struct tsp *msg;
+setmaster(struct tsp *msg)
{
if (slavenet
&& (slavenet != old_slavenet
@@ -640,9 +639,7 @@ setmaster(msg)
* handle date change request on a slave
*/
static void
-schgdate(msg, newdate)
- struct tsp *msg;
- char *newdate;
+schgdate(struct tsp *msg, char *newdate)
{
struct tsp to;
u_short seq;
@@ -680,7 +677,7 @@ schgdate(msg, newdate)
* contention and likely collisions.
*/
static void
-answerdelay()
+answerdelay(void)
{
struct timeval timeout;
diff --git a/usr.sbin/timed/timed/timed.c b/usr.sbin/timed/timed/timed.c
index 4b0030c..e178912 100644
--- a/usr.sbin/timed/timed/timed.c
+++ b/usr.sbin/timed/timed/timed.c
@@ -110,9 +110,7 @@ static void usage(void);
* overhauled at Silicon Graphics
*/
int
-main(argc, argv)
- int argc;
- char *argv[];
+main(int argc, char *argv[])
{
int on;
int ret;
@@ -434,7 +432,7 @@ main(argc, argv)
}
static void
-usage()
+usage(void)
{
#ifdef HAVENIS
fprintf(stderr,
@@ -450,10 +448,7 @@ usage()
* suppress an upstart, untrustworthy, self-appointed master
*/
void
-suppress(addr, name,net)
- struct sockaddr_in *addr;
- char *name;
- struct netinfo *net;
+suppress(struct sockaddr_in *addr, char *name, struct netinfo *net)
{
struct sockaddr_in tgt;
char tname[MAXHOSTNAMELEN];
@@ -478,8 +473,7 @@ suppress(addr, name,net)
}
void
-lookformaster(ntp)
- struct netinfo *ntp;
+lookformaster(struct netinfo *ntp)
{
struct tsp resp, conflict, *answer;
struct timeval ntime;
@@ -578,7 +572,7 @@ lookformaster(ntp)
* networks;
*/
void
-setstatus()
+setstatus(void)
{
struct netinfo *ntp;
@@ -631,8 +625,7 @@ setstatus()
}
void
-makeslave(net)
- struct netinfo *net;
+makeslave(struct netinfo *net)
{
register struct netinfo *ntp;
@@ -647,7 +640,7 @@ makeslave(net)
* Try to become master over ignored nets..
*/
static void
-checkignorednets()
+checkignorednets(void)
{
register struct netinfo *ntp;
@@ -669,8 +662,7 @@ checkignorednets()
* Take a hint about for a good network.
*/
static void
-pickslavenet(ntp)
- struct netinfo *ntp;
+pickslavenet(struct netinfo *ntp)
{
if (slavenet != 0 && slavenet->status == SLAVE) {
makeslave(slavenet); /* prune extras */
@@ -690,8 +682,7 @@ pickslavenet(ntp)
* returns a random number in the range [inf, sup]
*/
long
-casual(inf, sup)
- long inf, sup;
+casual(long inf, long sup)
{
double value;
@@ -700,7 +691,7 @@ casual(inf, sup)
}
char *
-date()
+date(void)
{
time_t tv_sec;
@@ -709,8 +700,7 @@ date()
}
void
-addnetname(name)
- char *name;
+addnetname(char *name)
{
register struct nets **netlist = &nets;
@@ -723,11 +713,11 @@ addnetname(name)
(*netlist)->name = name;
}
-/* note a host as trustworthy */
+/* note a host as trustworthy
+ * perm 1=not part of the netgroup
+ */
static void
-add_good_host(name, perm)
- char *name;
- int perm; /* 1=not part of the netgroup */
+add_good_host(char *name, int perm)
{
register struct goodhost *ghp;
register struct hostent *hentp;
@@ -753,8 +743,7 @@ add_good_host(name, perm)
/* update our image of the net-group of trustworthy hosts
*/
void
-get_goodgroup(force)
- int force;
+get_goodgroup(int force)
{
# define NG_DELAY (30*60*CLK_TCK) /* 30 minutes */
static unsigned long last_update = -NG_DELAY;
@@ -822,8 +811,7 @@ get_goodgroup(force)
/* see if a machine is trustworthy
*/
int /* 1=trust hp to change our date */
-good_host_name(name)
- char *name;
+good_host_name(char *name)
{
register struct goodhost *ghp = goodhosts;
register char c;
diff --git a/usr.sbin/timed/timedc/cmds.c b/usr.sbin/timed/timedc/cmds.c
index 84740f6..e39bf03 100644
--- a/usr.sbin/timed/timedc/cmds.c
+++ b/usr.sbin/timed/timedc/cmds.c
@@ -32,7 +32,6 @@
static char sccsid[] = "@(#)cmds.c 8.1 (Berkeley) 6/6/93";
#endif /* not lint */
#endif
-
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
@@ -78,8 +77,7 @@ void bytehostorder(struct tsp *);
/* compute the difference between our date and another machine
*/
static int /* difference in days from our time */
-daydiff(hostname)
- char *hostname;
+daydiff(char *hostname)
{
int i;
int trials;
@@ -164,9 +162,7 @@ daydiff(hostname)
* measurement.
*/
void
-clockdiff(argc, argv)
- int argc;
- char *argv[];
+clockdiff(int argc, char *argv[])
{
int measure_status;
extern int measure(u_long, u_long, char *, struct sockaddr_in*, int);
@@ -263,9 +259,7 @@ clockdiff(argc, argv)
* finds location of master timedaemon
*/
void
-msite(argc, argv)
- int argc;
- char *argv[];
+msite(int argc, char *argv[])
{
ssize_t cc;
fd_set ready;
@@ -360,7 +354,7 @@ msite(argc, argv)
* quits timedc
*/
void
-quit()
+quit(void)
{
exit(0);
}
@@ -372,9 +366,7 @@ quit()
* reliability of communication channel.
*/
void
-testing(argc, argv)
- int argc;
- char *argv[];
+testing(int argc, char *argv[])
{
struct servent *srvp;
struct sockaddr_in sin;
@@ -422,9 +414,7 @@ testing(argc, argv)
* Enables or disables tracing on local timedaemon
*/
void
-tracing(argc, argv)
- int argc;
- char *argv[];
+tracing(int argc, char *argv[])
{
int onflag;
int length;
@@ -513,7 +503,7 @@ tracing(argc, argv)
}
int
-priv_resources()
+priv_resources(void)
{
int port;
struct sockaddr_in sin;
diff --git a/usr.sbin/timed/timedc/timedc.c b/usr.sbin/timed/timedc/timedc.c
index 7047f57..f81da89 100644
--- a/usr.sbin/timed/timedc/timedc.c
+++ b/usr.sbin/timed/timedc/timedc.c
@@ -62,9 +62,7 @@ jmp_buf toplevel;
static struct cmd *getcmd(char *);
int
-main(argc, argv)
- int argc;
- char *argv[];
+main(int argc, char *argv[])
{
register struct cmd *c;
@@ -131,8 +129,7 @@ main(argc, argv)
}
void
-intr(signo)
- int signo;
+intr(int signo __unused)
{
if (!fromatty)
exit(0);
@@ -141,8 +138,7 @@ intr(signo)
static struct cmd *
-getcmd(name)
- char *name;
+getcmd(char *name)
{
register char *p, *q;
register struct cmd *c, *found;
@@ -175,7 +171,7 @@ getcmd(name)
* Slice a string up into argc/argv.
*/
void
-makeargv()
+makeargv(void)
{
register char *cp;
register char **argp = margv;
@@ -203,9 +199,7 @@ makeargv()
* Help command.
*/
void
-help(argc, argv)
- int argc;
- char *argv[];
+help(int argc, char *argv[])
{
register struct cmd *c;
diff --git a/usr.sbin/watchdogd/watchdogd.c b/usr.sbin/watchdogd/watchdogd.c
index 8194f96..50bdd91 100644
--- a/usr.sbin/watchdogd/watchdogd.c
+++ b/usr.sbin/watchdogd/watchdogd.c
@@ -71,6 +71,14 @@ static int nap = 1;
static char *test_cmd = NULL;
/*
+ * Ask malloc() to map minimum-sized chunks of virtual address space at a time,
+ * so that mlockall() won't needlessly wire megabytes of unused memory into the
+ * process. This must be done using the malloc_conf string so that it gets set
+ * up before the first allocation, which happens before entry to main().
+ */
+const char * malloc_conf = "lg_chunk:0";
+
+/*
* Periodically pat the watchdog, preventing it from firing.
*/
int
@@ -188,7 +196,7 @@ watchdog_loop(void)
if (watchdog_onoff(0) == 0) {
end_program = 2;
} else {
- warnx("Could not stop the watchdog, not exitting");
+ warnx("Could not stop the watchdog, not exiting");
end_program = 0;
}
}
diff --git a/usr.sbin/wpa/wpa_supplicant/wpa_supplicant.8 b/usr.sbin/wpa/wpa_supplicant/wpa_supplicant.8
index 4858410..466759d 100644
--- a/usr.sbin/wpa/wpa_supplicant/wpa_supplicant.8
+++ b/usr.sbin/wpa/wpa_supplicant/wpa_supplicant.8
@@ -24,7 +24,7 @@
.\"
.\" $FreeBSD$
.\"
-.Dd March 24, 2008
+.Dd November 7, 2012
.Dt WPA_SUPPLICANT 8
.Os
.Sh NAME
@@ -32,9 +32,26 @@
.Nd "WPA/802.11i Supplicant for wireless network devices"
.Sh SYNOPSIS
.Nm
-.Op Fl BdehLqsvw
+.Op Fl BdhKLqstuvW
+.Op Fl b Ar br_ifname
+.Fl c Ar config-file
+.Op Fl C Ar ctrl
+.Op Fl D Ar driver
+.Op Fl f Ar debug file
+.Op Fl g Ar global ctrl
+.Fl i Ar ifname
+.Op Fl o Ar override driver
+.Op Fl O Ar override ctrl
+.Op Fl P Ar pid file
+.Oo Fl N
.Fl i Ar ifname
.Fl c Ar config-file
+.Op Fl C Ar ctrl
+.Op Fl D driver
+.Op Fl p Ar driver_param
+.Op Fl b Ar br_ifname
+.No ...
+.Oc
.Sh DESCRIPTION
The
.Nm
@@ -87,15 +104,36 @@ utility, using
.Sh OPTIONS
The following options are available:
.Bl -tag -width indent
+.It Fl b
+Optional bridge interface name.
+.It Fl B
+Detach from the controlling terminal and run as a daemon process
+in the background.
.It Fl d
Enable debugging messages.
If this option is supplied twice, more verbose messages are displayed.
-.It Fl e
-Use an external IEEE 802.1X Supplicant program and disable the
-internal Supplicant.
-This option is not normally used.
+.It Fl D
+Driver name (can be multiple drivers: nl80211,wext).
+.It Fl f
+Log output to debug file instead of stdout.
+.It Fl g
+Global ctrl_interface.
.It Fl h
Show help text.
+.It Fl K
+Include key information in debugging output.
+.It Fl L
+Display the license for this program on the terminal and exit.
+.It Fl N
+Start describing a new interface.
+.It Fl o
+Overrides driver parameter for new interfaces.
+.It Fl O
+Override ctrl_interface parameter for new interfaces.
+.It Fl p
+Specify driver parameters.
+.It Fl P
+File in which to save the process PID.
.It Fl q
Decrease debugging verbosity (i.e., counteract the use of the
.Fl d
@@ -104,23 +142,14 @@ flag).
Send log messages through
.Xr syslog 3
instead of to the terminal.
+.It Fl t
+Include timestamp in debug messages.
+.It Fl u
+Enable DBus control interface.
.It Fl v
Display version information on the terminal and exit.
-.It Fl w
-If the specified interface is not present, wait for it to be
-added; e.g.\& a cardbus device to be inserted.
-This option is not normally used; instead,
-.Xr devd 8
-should be configured to launch
-.Nm
-when a device is created.
-.It Fl B
-Detach from the controlling terminal and run as a daemon process
-in the background.
-.It Fl K
-Include key information in debugging output.
-.It Fl L
-Display the license for this program on the terminal and exit.
+.It Fl W
+Wait for a control interface monitor before starting.
.El
.Sh SEE ALSO
.Xr an 4 ,
diff --git a/usr.sbin/ypserv/yp_main.c b/usr.sbin/ypserv/yp_main.c
index 69dae29..60bd6a1 100644
--- a/usr.sbin/ypserv/yp_main.c
+++ b/usr.sbin/ypserv/yp_main.c
@@ -292,7 +292,7 @@ create_service(const int sock, const struct netconfig *nconf,
s = __rpc_nconf2fd(nconf);
if (s < 0) {
- if (errno == EPROTONOSUPPORT)
+ if (errno == EAFNOSUPPORT)
_msgout("unsupported"
" transport: %s",
nconf->nc_netid);
OpenPOWER on IntegriCloud