Wed Aug 15 12:48:20 2012 UTC ()
Don't rely on stat(1) with format "%SHr" to print the correct names.
That uses devname(3) internally, which doesn't work at all in a cross
build environment, and doesn't do what I thought even in a native
environment.

Instead, parse the device major numbers for the pty master and slave
devices from the output of "MAKEDEV -s pty0" and check those against the
actual device node that we are thinking of removing.


(apb)
diff -r1.144 -r1.145 src/usr.sbin/postinstall/postinstall

cvs diff -r1.144 -r1.145 src/usr.sbin/postinstall/Attic/postinstall (expand / switch to unified diff)

--- src/usr.sbin/postinstall/Attic/postinstall 2012/08/14 13:11:24 1.144
+++ src/usr.sbin/postinstall/Attic/postinstall 2012/08/15 12:48:19 1.145
@@ -1,16 +1,16 @@ @@ -1,16 +1,16 @@
1#!/bin/sh 1#!/bin/sh
2# 2#
3# $NetBSD: postinstall,v 1.144 2012/08/14 13:11:24 apb Exp $ 3# $NetBSD: postinstall,v 1.145 2012/08/15 12:48:19 apb Exp $
4# 4#
5# Copyright (c) 2002-2008 The NetBSD Foundation, Inc. 5# Copyright (c) 2002-2008 The NetBSD Foundation, Inc.
6# All rights reserved. 6# All rights reserved.
7# 7#
8# This code is derived from software contributed to The NetBSD Foundation 8# This code is derived from software contributed to The NetBSD Foundation
9# by Luke Mewburn. 9# by Luke Mewburn.
10# 10#
11# Redistribution and use in source and binary forms, with or without 11# Redistribution and use in source and binary forms, with or without
12# modification, are permitted provided that the following conditions 12# modification, are permitted provided that the following conditions
13# are met: 13# are met:
14# 1. Redistributions of source code must retain the above copyright 14# 1. Redistributions of source code must retain the above copyright
15# notice, this list of conditions and the following disclaimer. 15# notice, this list of conditions and the following disclaimer.
16# 2. Redistributions in binary form must reproduce the above copyright 16# 2. Redistributions in binary form must reproduce the above copyright
@@ -45,26 +45,27 @@ @@ -45,26 +45,27 @@
45# dhclient.conf, ...) ? 45# dhclient.conf, ...) ?
46# - support quiet/verbose mode ? 46# - support quiet/verbose mode ?
47# - differentiate between failures caused by missing source 47# - differentiate between failures caused by missing source
48# and real failures 48# and real failures
49# - install moduli into usr/share/examples/ssh and use from there? 49# - install moduli into usr/share/examples/ssh and use from there?
50# - differentiate between "needs fix" versus "can't fix" issues 50# - differentiate between "needs fix" versus "can't fix" issues
51# 51#
52 52
53# This script is executed as part of a cross build. Allow the build 53# This script is executed as part of a cross build. Allow the build
54# environment to override the locations of some tools. 54# environment to override the locations of some tools.
55: ${AWK:=awk} 55: ${AWK:=awk}
56: ${DB:=db} 56: ${DB:=db}
57: ${GREP:=grep} 57: ${GREP:=grep}
 58: ${HOST_SH:=sh}
58: ${MAKE:=make} 59: ${MAKE:=make}
59: ${PWD_MKDB:=/usr/sbin/pwd_mkdb} 60: ${PWD_MKDB:=/usr/sbin/pwd_mkdb}
60: ${STAT:=stat} 61: ${STAT:=stat}
61 62
62# 63#
63# helper functions 64# helper functions
64# 65#
65 66
66err() 67err()
67{ 68{
68 exitval=$1 69 exitval=$1
69 shift 70 shift
70 echo 1>&2 "${PROGNAME}: $*" 71 echo 1>&2 "${PROGNAME}: $*"
@@ -1729,43 +1730,74 @@ do_obsolete() @@ -1729,43 +1730,74 @@ do_obsolete()
1729additem ptyfsoldnodes "remove legacy device nodes when using ptyfs" 1730additem ptyfsoldnodes "remove legacy device nodes when using ptyfs"
1730do_ptyfsoldnodes() 1731do_ptyfsoldnodes()
1731{ 1732{
1732 [ -n "$1" ] || err 3 "USAGE: do_ptyfsoldnodes fix|check" 1733 [ -n "$1" ] || err 3 "USAGE: do_ptyfsoldnodes fix|check"
1733 _ptyfs_op="$1" 1734 _ptyfs_op="$1"
1734 1735
1735 # Check whether ptyfs is in use 1736 # Check whether ptyfs is in use
1736 failed=0; 1737 failed=0;
1737 if ! ${GREP} -E "^ptyfs" "${DEST_DIR}/etc/fstab" > /dev/null; then 1738 if ! ${GREP} -E "^ptyfs" "${DEST_DIR}/etc/fstab" > /dev/null; then
1738 msg "ptyfs is not in use" 1739 msg "ptyfs is not in use"
1739 return 0 1740 return 0
1740 fi 1741 fi
1741 1742
 1743 # Find the device major numbers for the pty master and slave
 1744 # devices, by parsing the output from "MAKEDEV -s pty0".
 1745 #
 1746 # Output from MAKEDEV looks like this:
 1747 # ./ttyp0 type=char device=netbsd,5,0 mode=666 gid=0 uid=0
 1748 # ./ptyp0 type=char device=netbsd,6,0 mode=666 gid=0 uid=0
 1749 #
 1750 # Output from awk, used in the eval statement, looks like this:
 1751 # maj_ptym=6; maj_ptys=5;
 1752 #
 1753 eval "$(
 1754 ${HOST_SH} "${DEST_DIR}/dev/MAKEDEV" -s pty0 2>/dev/null \
 1755 | ${AWK} '\
 1756 BEGIN { before_re = ".*device=[a-zA-Z]*,"; after_re = ",.*"; }
 1757 /ptyp0/ { maj_ptym = gensub(before_re, "", 1, $0);
 1758 maj_ptym = gensub(after_re, "", 1, maj_ptym); }
 1759 /ttyp0/ { maj_ptys = gensub(before_re, "", 1, $0);
 1760 maj_ptys = gensub(after_re, "", 1, maj_ptys); }
 1761 END { print "maj_ptym=" maj_ptym "; maj_ptys=" maj_ptys ";"; }
 1762 '
 1763 )"
 1764 #msg "Major numbers are maj_ptym=${maj_ptym} maj_ptys=${maj_ptys}"
 1765 if [ -z "$maj_ptym" ] || [ -z "$maj_ptys" ]; then
 1766 msg "Cannot find device major numbers for pty master and slave"
 1767 return 1
 1768 fi
 1769
1742 # look for /dev/[pt]ty[p-zP-T][0-9a-zA-Z], and check that they 1770 # look for /dev/[pt]ty[p-zP-T][0-9a-zA-Z], and check that they
1743 # are tty or pty device nodes. ttyv* is typically not a 1771 # have the expected device major numbers. ttyv* is typically not a
1744 # pty device, but we check it anyway. 1772 # pty device, but we check it anyway.
1745 # 1773 #
1746 # The "for d1" loop is intended to avoid overflowing ARG_MAX; 1774 # The "for d1" loop is intended to avoid overflowing ARG_MAX;
1747 # otherwise we could have used a single glob pattern. 1775 # otherwise we could have used a single glob pattern.
1748 # 1776 #
1749 # If there are no files that match a particular pattern, 1777 # If there are no files that match a particular pattern,
1750 # then stat prints something like: 1778 # then stat prints something like:
1751 # stat: /dev/[pt]tyx?: lstat: No such file or directory 1779 # stat: /dev/[pt]tyx?: lstat: No such file or directory
1752 # and we ignore it. XXX: We also ignore other error messages. 1780 # and we ignore it. XXX: We also ignore other error messages.
1753 # 1781 #
1754 _ptyfs_tmp="$(mktemp /tmp/postinstall.ptyfs.XXXXXXXX)" 1782 _ptyfs_tmp="$(mktemp /tmp/postinstall.ptyfs.XXXXXXXX)"
1755 for d1 in p q r s t u v w x y z P Q R S T; do 1783 for d1 in p q r s t u v w x y z P Q R S T; do
1756 ${STAT} -f "%SHr %N" "${DEST_DIR}/dev/"[pt]ty${d1}? 2>&1 1784 ${STAT} -f "%Hr %N" "${DEST_DIR}/dev/"[pt]ty${d1}? 2>&1
1757 done \ 1785 done \
1758 | ${AWK} '/^[pt]ty/ {print $2}' >"${_ptyfs_tmp}" 1786 | while read -r major node ; do
 1787 case "$major" in
 1788 ${maj_ptym}|${maj_ptys}) echo "$node" ;;
 1789 esac
 1790 done >"${_ptyfs_tmp}"
1759 1791
1760 _desc="legacy device node" 1792 _desc="legacy device node"
1761 while read node; do 1793 while read node; do
1762 if [ "${_ptyfs_op}" = "check" ]; then 1794 if [ "${_ptyfs_op}" = "check" ]; then
1763 msg "Remove ${_desc} ${node}" 1795 msg "Remove ${_desc} ${node}"
1764 failed=1 1796 failed=1
1765 else # "fix" 1797 else # "fix"
1766 if rm "${node}"; then 1798 if rm "${node}"; then
1767 msg "Removed ${_desc} ${node}" 1799 msg "Removed ${_desc} ${node}"
1768 else 1800 else
1769 warn "Failed to remove ${_desc} ${node}" 1801 warn "Failed to remove ${_desc} ${node}"
1770 failed=1 1802 failed=1
1771 fi 1803 fi