Skip to contentSkip to conversation

bashizm in hotplug scripts (including 20040401)

PATCH Arkadiusz Miskiewicz 3 participants 6 messages Apr 2, 2004 – Apr 5, 2004

lore
Share & tools
lore
#0 lore raw

Hi,

hotplug package is seriously broken and won't work on POSIX/SUSv3 compilant /bin/sh shells.

$ echo $((0xff))
ksh: 0xff: bad number `0xff'

There is a lot of hex to dec conversions using bash only function in hotplug, example:
echo $((0xff))

Under ksh (pdksh)

$ echo $((0xff))
ksh: 0xff: bad number `0xff'

There is more portable version
hex=0xff
echo "$((16#echo "$hex" | sed -e 's#^0x##g'))"

it works well on zsh, pdksh, bash in their enhaced mode but for example on pdksh run in sh mode it overflows - example:
[arekm@mobarm arekm]$ echo $((16#ff))
255
[arekm@mobarm arekm]$ echo $((16#ffffffff))
-1

pdksh run as pdksh works though.

There are two solutions known to me right now:
- first, very easy - #!/bin/bash instead of #!/bin/sh in these scripts
- second working, POSIX/SUSv3 compilant but much slower; use awk for conversions (see attached patch).

Any other known fast and portable hex to dec conversions?

ps. using hex2dec() function regardles of choosen conversion method would be nice - it would allow easy way to change the way of doing conversion + it explicitly says where hex number should occur

Signature · 2 lines
Arkadiusz Miśkiewicz     CS at FoE, Wroclaw University of Technology
arekm.pld-linux.org, 1024/3DB19BBD, JID: arekm.jabber.org, PLD/Linux
etc/hotplug/hotplug.functions +9 -0
diff -urN hotplug-2004_04_01.org/etc/hotplug/hotplug.functions hotplug-2004_04_01/etc/hotplug/hotplug.functions
@@ -190,4 +190,13 @@
     # empty line terminates events
 }
 
+####################################################################
+
+# usage: hex2dec hexnumber
+hex2dec()
+{
+    hex="$1"
+    echo | awk "{ printf(\"%d\n\", \"$hex\"); }"
+}
+
 # vim:syntax=sh
etc/hotplug/ieee1394.agent +8 -6
diff -urN hotplug-2004_04_01.org/etc/hotplug/ieee1394.agent hotplug-2004_04_01/etc/hotplug/ieee1394.agent
@@ -39,9 +39,9 @@
 fi
 
 
-device_vendor_id=$((0x$VENDOR_ID))
-device_specifier_id=$((0x$SPECIFIER_ID))
-device_version=$((0x$VERSION))
+device_vendor_id=$(hex2dec 0x$VENDOR_ID)
+device_specifier_id=$(hex2dec 0x$SPECIFIER_ID)
+device_version=$(hex2dec 0x$VERSION)
 
 MATCH_VENDOR_ID=0x0001
 MATCH_SPECIFIER_ID=0x0004
@@ -63,9 +63,11 @@
 	: check match for $module
 
 	# convert from hex to dec
-	match_flags=$(($match_flags))
-	vendor_id=$(($vendor_id)); model_id=$(($model_id))
-	specifier_id=$(($specifier_id)); version=$(($version))
+	match_flags=$(hex2dec $match_flags)
+	vendor_id=$(hex2dec $vendor_id);
+	model_id=$(hex2dec $model_id)
+	specifier_id=$(hex2dec $specifier_id);
+	version=$(hex2dec $version)
 
 	: vendor_id $vendor_id $device_vendor_id
 	if [ $(($match_flags & $MATCH_VENDOR_ID)) -ne 0 -a $vendor_id -ne $device_vendor_id ]; then
etc/hotplug/input.agent +21 -21

76 lines not on this page · raw message

etc/hotplug/pci.agent +12 -9
diff -urN hotplug-2004_04_01.org/etc/hotplug/pci.agent hotplug-2004_04_01/etc/hotplug/pci.agent
@@ -59,18 +59,18 @@
 
 pci_convert_vars ()
 {
-    pci_class=$((0x$PCI_CLASS))
+    pci_class=$(hex2dec 0x$PCI_CLASS)
 
     set $(echo $PCI_ID | sed -e 's/\([^:]*\):\(.*\)/\1 \2/')
-    pci_id_vendor=$((0x$1))
-    pci_id_device=$((0x$2))
+    pci_id_vendor=$(hex2dec 0x$1)
+    pci_id_device=$(hex2dec 0x$2)
 
     set $(echo $PCI_SUBSYS_ID | sed -e 's/\([^:]*\):\(.*\)/\1 \2/')
-    pci_subid_vendor=$((0x$1))
-    pci_subid_device=$((0x$2))
+    pci_subid_vendor=$(hex2dec 0x$1)
+    pci_subid_device=$(hex2dec 0x$2)
 }
 
-PCI_ANY=$((0xffffffff))
+PCI_ANY=$(hex2dec 0xffffffff)
 
 
 #
@@ -95,9 +95,12 @@
 	esac
 
 	# convert the fields from hex to dec
-	vendor=$(($vendor)); device=$(($device))
-	subvendor=$(($subvendor)); subdevice=$(($subdevice))
-	class=$(($class)); class_mask=$(($class_mask))
+	vendor=$(hex2dec $vendor)
+	device=$(hex2dec $device)
+	subvendor=$(hex2dec $subvendor)
+	subdevice=$(hex2dec $subdevice)
+	class=$(hex2dec $class)
+	class_mask=$(hex2dec $class_mask)
 
 	: checkmatch $module
 
etc/hotplug/pci.rc +1 -0
diff -urN hotplug-2004_04_01.org/etc/hotplug/pci.rc hotplug-2004_04_01/etc/hotplug/pci.rc
@@ -65,6 +65,7 @@
 case "$1" in
   start)
 	pci_boot_events
+	exit $?
         ;;
   stop)
 	# echo $"pci stop -- ignored"
etc/hotplug/usb.agent +31 -31

95 lines not on this page · raw message

etc/hotplug.d/default/default.hotplug +1 -1
diff -urN hotplug-2004_04_01.org/etc/hotplug.d/default/default.hotplug hotplug-2004_04_01/etc/hotplug.d/default/default.hotplug
@@ -32,7 +32,7 @@
 
 # DEBUG=yes export DEBUG
 
-debug_mesg "arguments ($*) env (`env`)"
+[ -n "$DEBUG" ] && debug_mesg "arguments ($*) env (`env`)"
 
 #
 # Only one required argument:  event type type being dispatched.
#1 lore raw
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

"Arkadiusz" = Arkadiusz Miskiewicz <[email protected]> writes:

Arkadiusz> hotplug package is seriously broken and won't work on Arkadiusz> POSIX/SUSv3 compilant /bin/sh shells.

Arkadiusz> $ echo $((0xff))
Arkadiusz> ksh: 0xff: bad number `0xff'

Yuck! In section 2.6.4, the standard says:

Only the decimal-constant, octal-constant, and hexadecimal-constant constants specified in the ISO C standard, Section 6.4.4.1 are required to be recognised as constants.

and this section has:

hexadecimal-constant:
hexadecimal-prefix hexadecimal-digit
hexadecimal-constant hexadecimal-digit

hexadecimal-prefix: one of

    0x 0X

Looks like ksh is broken (and my testing of pdksh 5.2.14 (in Debian unstable) support this).

However...

    Arkadiusz> There is more portable version
    Arkadiusz> hex=0xff
    Arkadiusz> echo "$((16#`echo "$hex" | sed -e 's#^0x##g'`))"

This will work just as well, is as POSIX compliant as the above (see below) and avoids the fork/exec overhead:

  echo $((16#${hex#0x}))

Arkadiusz> it works well on zsh, pdksh, bash in their enhaced mode Arkadiusz> [...]

I've tested the above in pdksh and bash.

    Arkadiusz> [...] but for example on pdksh run in sh mode it
    Arkadiusz> overflows - example:

    Arkadiusz> [arekm@mobarm arekm]$ echo $((16#ff))
    Arkadiusz> 255
    Arkadiusz> [arekm@mobarm arekm]$ echo $((16#ffffffff))
    Arkadiusz> -1

Debian's ash-0.4.25 overflows too.

Note that POSIX says:

Only signed long integer arithmetic is required.

There's another problem. In ash:

  echo $((16#ff))

fails. Neither the POSIX or C standard mentions the "base#num" notation!

Looks like the best way of fixing this is to leave hotplug the way it is (except if a workaround for the overlows is needed) and fix pdksh... :-)

Incidently, last time I looked, hotplug had quite a few uses of things like sed and cut that could be replaced by clever us of the POSIX substring operations:

  ${paramter%word}
  ${paramter%%word}
  ${paramter#word}
  ${paramter##word}

At the time I thought these were bash-specific, so I didn't send a patch. Now that I've read the relevant part of the standard, I'd be happy to create a patch that uses these if it is likely to be applied.

peace & happiness,
martin

-------------------------------------------------------
This SF.Net email is sponsored by: IBM Linux Tutorials
Free Linux tutorial presented by Daniel Robbins, President and CEO of GenToo technologies. Learn everything from fundamentals to system administration.http://ads.osdn.com/?ad_id70&alloc_id638&op=click
_______________________________________________
Linux-hotplug-devel mailing list http://linux-hotplug.sourceforge.net
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel

#2 lore raw
In reply to Arkadiusz Miskiewicz

Dnia Friday 02 of April 2004 03:09, Martin Schwenke napisa³:

Quoted 5 lines

Yuck! In section 2.6.4, the standard says:

Only the decimal-constant, octal-constant, and hexadecimal-constant constants specified in the ISO C standard, Section 6.4.4.1 are required to be recognised as constants.

Missed that :/

Quoted 8 lines
    Arkadiusz> [...] but for example on pdksh run in sh mode it
    Arkadiusz> overflows - example:

    Arkadiusz> [arekm@mobarm arekm]$ echo $((16#ff))
    Arkadiusz> 255
    Arkadiusz> [arekm@mobarm arekm]$ echo $((16#ffffffff))
    Arkadiusz> -1
Quoted 5 lines

Debian's ash-0.4.25 overflows too.

Note that POSIX says:

Only signed long integer arithmetic is required.

So hotplug scripts still need the change because they do arithmetic beyond signed long:
[misiek@arm ~/rpm/SOURCES/hotplug-2004_04_01.org]$ grep -r 0xffffffff . ./etc/hotplug/pci.agent:PCI_ANY=$((0xffffffff))

Quoted 2 lines

Looks like the best way of fixing this is to leave hotplug the way it is (except if a workaround for the overlows is needed)

It's needed. Either create some workaround or put #!/bin/bash there. Any propositions for workaround?

Quoted 2 lines

and fix
pdksh... :-)

http://cvs.pld-linux.org/cgi-bin/cvsweb/SOURCES/pdksh-hex.patch?rev=1.1 from OpenBSD people

Quoted 2 lines

peace & happiness,
martin

Signature · 13 lines
Arkadiusz Mi¶kiewicz     CS at FoE, Wroclaw University of Technology
arekm.pld-linux.org, 1024/3DB19BBD, JID: arekm.jabber.org, PLD/Linux


-------------------------------------------------------
This SF.Net email is sponsored by: IBM Linux Tutorials
Free Linux tutorial presented by Daniel Robbins, President and CEO of
GenToo technologies. Learn everything from fundamentals to system
administration.http://ads.osdn.com/?ad_id70&alloc_id638&opÌk
_______________________________________________
Linux-hotplug-devel mailing list  http://linux-hotplug.sourceforge.net
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel
#3 lore raw
In reply to Arkadiusz Miskiewicz
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

"Arkadiusz" = Arkadiusz Miskiewicz <[email protected]> writes:

Arkadiusz> So hotplug scripts still need the change because they Arkadiusz> do arithmetic beyond signed long:

Arkadiusz> [...]$ grep -r 0xffffffff . Arkadiusz> ./etc/hotplug/pci.agent:PCI_ANY=$((0xffffffff))

The value of PCI_ANY only ever compared against other things - there's no useful arithmetic done. The PCI 2.2 specification only allows Vendor ID and Device ID to be 16 bits (in configuration space at least). If there any reason for using the value 0xffffffff, rather than just 0xffff? Greg?

peace & happiness,
martin

-------------------------------------------------------
This SF.Net email is sponsored by: IBM Linux Tutorials
Free Linux tutorial presented by Daniel Robbins, President and CEO of GenToo technologies. Learn everything from fundamentals to system administration.http://ads.osdn.com/?ad_id70&alloc_id638&op=click
_______________________________________________
Linux-hotplug-devel mailing list http://linux-hotplug.sourceforge.net
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel

#4 lore raw
In reply to Arkadiusz Miskiewicz
Quoted 5 lines · Martin Schwenke
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

"Arkadiusz" = Arkadiusz Miskiewicz <[email protected]> writes:

Arkadiusz> So hotplug scripts still need the change because they Arkadiusz> do arithmetic beyond signed long:

Quoted 8 lines

Arkadiusz> [...]$ grep -r 0xffffffff . Arkadiusz> ./etc/hotplug/pci.agent:PCI_ANY=$((0xffffffff))

The value of PCI_ANY only ever compared against other things - there's no useful arithmetic done. The PCI 2.2 specification only allows Vendor ID and Device ID to be 16 bits (in configuration space at least). If there any reason for using the value 0xffffffff, rather than just 0xffff? Greg?

Look at include/linux/mod_devicetable.h for why that is a 32 bit number (needs to match up with the pci_device_id fields.)

Now why those fields are 32 bits, you will probably have to go dig through the 2.3 kernel archives, I don't remember anymore.

thanks,

greg k-h

-------------------------------------------------------
This SF.Net email is sponsored by: IBM Linux Tutorials
Free Linux tutorial presented by Daniel Robbins, President and CEO of GenToo technologies. Learn everything from fundamentals to system administration.http://ads.osdn.com/?ad_id70&alloc_id638&op=click
_______________________________________________
Linux-hotplug-devel mailing list http://linux-hotplug.sourceforge.net
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel

#5 lore raw
In reply to Arkadiusz Miskiewicz

Dnia Monday 05 of April 2004 08:06, Martin Schwenke napisa³:

Quoted 5 lines

The value of PCI_ANY only ever compared against other things - there's no useful arithmetic done. The PCI 2.2 specification only allows Vendor ID and Device ID to be 16 bits (in configuration space at least). If there any reason for using the value 0xffffffff, rather than just 0xffff? Greg?

If 16 bits is max in reality then it seems that we don't have to do anything because:

PCI_ANY=$((0xffffffff))

would overflow to -1 and

vendor=$(($vendor));
this will also overflow to -1

if [ $vendor -ne $PCI_ANY -a $vendor -ne $pci_id_vendor ]; then and we end up with -1 -ne -1 comparsion.

Anyway comment about this should be added in hotplug scripts.

Quoted 2 lines

peace & happiness,
martin

Signature · 13 lines
Arkadiusz Mi¶kiewicz     CS at FoE, Wroclaw University of Technology
arekm.pld-linux.org, 1024/3DB19BBD, JID: arekm.jabber.org, PLD/Linux


-------------------------------------------------------
This SF.Net email is sponsored by: IBM Linux Tutorials
Free Linux tutorial presented by Daniel Robbins, President and CEO of
GenToo technologies. Learn everything from fundamentals to system
administration.http://ads.osdn.com/?ad_id70&alloc_id638&opÌk
_______________________________________________
Linux-hotplug-devel mailing list  http://linux-hotplug.sourceforge.net
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel