Clone of https://github.com/NixOS/nixpkgs.git (to stress-test knotserver)

[WIP] usb_modeswitch

+334 -10
+13
nixos/modules/hardware/usb-wwan.nix
··· 21 21 ###### implementation 22 22 23 23 config = mkIf config.hardware.usbWwan.enable { 24 + # Attaches device specific handlers. 24 25 services.udev.packages = with pkgs; [ usb-modeswitch-data ]; 26 + 27 + # Triggered by udev, usb-modeswitch creates systemd services via a 28 + # template unit in the usb-modeswitch package. 29 + systemd.packages = with pkgs; [ usb-modeswitch ]; 30 + 31 + # The systemd service requires the usb-modeswitch-data. The 32 + # usb-modeswitch package intends to discover this via the 33 + # filesystem at /usr/share/usb_modeswitch, and merge it with user 34 + # configuration in /etc/usb_modeswitch.d. Configuring the correct 35 + # path in the package is difficult, as it would cause a cyclic 36 + # dependency. 37 + environment.etc."usb_modeswitch.d".source = "${pkgs.usb-modeswitch-data}/share/usb_modeswitch"; 25 38 }; 26 39 }
+294
pkgs/development/tools/misc/usb-modeswitch/configurable-usb-modeswitch.patch
··· 1 + diff --git a/Makefile b/Makefile 2 + index 463a11f..f20072c 100644 3 + --- a/Makefile 4 + +++ b/Makefile 5 + @@ -5,11 +5,11 @@ CFLAGS += -Wall 6 + LIBS = `pkg-config --libs --cflags libusb-1.0` 7 + RM = /bin/rm -f 8 + OBJS = usb_modeswitch.c 9 + -PREFIX = $(DESTDIR)/usr 10 + -ETCDIR = $(DESTDIR)/etc 11 + +PREFIX = /usr/local 12 + +ETCDIR = $(PREFIX)/etc 13 + SYSDIR = $(ETCDIR)/systemd/system 14 + UPSDIR = $(ETCDIR)/init 15 + -UDEVDIR = $(DESTDIR)/lib/udev 16 + +UDEVDIR = $(PREFIX)/lib/udev 17 + SBINDIR = $(PREFIX)/sbin 18 + MANDIR = $(PREFIX)/share/man/man1 19 + VPATH = jimtcl 20 + @@ -22,10 +22,17 @@ endif 21 + JIM_CONFIGURE_OPTS = --disable-lineedit \ 22 + --with-out-jim-ext="stdlib posix load signal syslog" --prefix=/usr 23 + 24 + +USE_UPSTART=$(shell if command -v initctl > /dev/null; then echo "true"; fi) 25 + +USE_SYSTEMD=$(shell if command -v systemctl > /dev/null; then echo "true"; fi) 26 + + 27 + .PHONY: clean install install-common uninstall \ 28 + script shared static \ 29 + dispatcher-script dispatcher-shared dispatcher-static \ 30 + - install-script install-shared install-static 31 + + install-script install-shared install-static \ 32 + + install-upstart install-systemd \ 33 + + configure-dispatcher configure-script \ 34 + + configure-upstart configure-systemd \ 35 + + configure 36 + 37 + all: script 38 + 39 + @@ -46,7 +53,25 @@ jim/libjim.a: 40 + cd jim && CFLAGS="$(CFLAGS)" CC="$(CC)" ./configure $(JIM_CONFIGURE_OPTS) 41 + $(MAKE) -C jim lib 42 + 43 + -dispatcher-script: usb_modeswitch.tcl 44 + +configure-dispatcher: 45 + + sed -i \ 46 + + -e 's,^\(set setup(sbindir) \).*$$,\1$(SBINDIR),' \ 47 + + -e 's,^\(set setup(etcdir) \).*$$,\1$(ETCDIR),' \ 48 + + usb_modeswitch.tcl 49 + + 50 + +configure-script: 51 + + sed -i -e 's,^\(SBINDIR=\).*$$,\1$(SBINDIR),' usb_modeswitch.sh 52 + + 53 + +configure-systemd: 54 + + sed -i -e 's,@sbindir@,$(SBINDIR),' usb_modeswitch@.service 55 + + 56 + +configure-upstart: 57 + + sed -i -e 's,@sbindir@,$(SBINDIR),' usb-modeswitch-upstart.conf 58 + + 59 + +configure: configure-dispatcher configure-script \ 60 + + configure-systemd configure-upstart 61 + + 62 + +dispatcher-script: configure-dispatcher usb_modeswitch.tcl 63 + sed 's_!/usr/bin/tclsh_!'"$(TCL)"'_' < usb_modeswitch.tcl > usb_modeswitch_dispatcher 64 + 65 + dispatcher-shared: jim/libjim.so dispatcher.c usb_modeswitch.string 66 + @@ -55,7 +80,7 @@ dispatcher-shared: jim/libjim.so dispatcher.c usb_modeswitch.string 67 + dispatcher-static: jim/libjim.a dispatcher.c usb_modeswitch.string 68 + $(CC) dispatcher.c $(LDFLAGS) jim/libjim.a -Ijim -o usb_modeswitch_dispatcher $(CFLAGS) 69 + 70 + -usb_modeswitch.string: usb_modeswitch.tcl 71 + +usb_modeswitch.string: configure-dispatcher usb_modeswitch.tcl 72 + $(HOST_TCL) make_string.tcl usb_modeswitch.tcl > $@ 73 + 74 + clean: 75 + @@ -76,16 +101,28 @@ ums-clean: 76 + # If the systemd folder is present, install the service for starting the dispatcher 77 + # If not, use the dispatcher directly from the udev rule as in previous versions 78 + 79 + -install-common: $(PROG) usb_modeswitch_dispatcher 80 + - install -D --mode=755 usb_modeswitch $(SBINDIR)/usb_modeswitch 81 + - install -D --mode=755 usb_modeswitch.sh $(UDEVDIR)/usb_modeswitch 82 + - install -D --mode=644 usb_modeswitch.conf $(ETCDIR)/usb_modeswitch.conf 83 + - install -D --mode=644 usb_modeswitch.1 $(MANDIR)/usb_modeswitch.1 84 + - install -D --mode=644 usb_modeswitch_dispatcher.1 $(MANDIR)/usb_modeswitch_dispatcher.1 85 + - install -D --mode=755 usb_modeswitch_dispatcher $(SBINDIR)/usb_modeswitch_dispatcher 86 + +install-common: $(PROG) configure usb_modeswitch_dispatcher 87 + + install -D --mode=755 usb_modeswitch $(DESTDIR)$(SBINDIR)/usb_modeswitch 88 + + install -D --mode=755 usb_modeswitch.sh $(DESTDIR)$(UDEVDIR)/usb_modeswitch 89 + + install -D --mode=644 usb_modeswitch.conf $(DESTDIR)$(ETCDIR)/usb_modeswitch.conf 90 + + install -D --mode=644 usb_modeswitch.1 $(DESTDIR)$(MANDIR)/usb_modeswitch.1 91 + + install -D --mode=644 usb_modeswitch_dispatcher.1 $(DESTDIR)$(MANDIR)/usb_modeswitch_dispatcher.1 92 + + install -D --mode=755 usb_modeswitch_dispatcher $(DESTDIR)$(SBINDIR)/usb_modeswitch_dispatcher 93 + install -d $(DESTDIR)/var/lib/usb_modeswitch 94 + - test -d $(UPSDIR) -a -e /sbin/initctl && install --mode=644 usb-modeswitch-upstart.conf $(UPSDIR) || test 1 95 + - test -d $(SYSDIR) -a \( -e /usr/bin/systemctl -o -e /bin/systemctl \) && install --mode=644 usb_modeswitch@.service $(SYSDIR) || test 1 96 + + 97 + +install-upstart: 98 + + install -D --mode=644 usb-modeswitch-upstart.conf $(DESTDIR)$(UPSDIR)/usb-modeswitch-upstart.conf 99 + + 100 + +install-systemd: 101 + + install -D --mode=644 usb_modeswitch@.service $(DESTDIR)$(SYSDIR)/usb_modeswitch@.service 102 + + 103 + +ifeq ($(USE_UPSTART),true) 104 + +install-common: install-upstart 105 + +endif 106 + + 107 + +ifeq ($(USE_SYSTEMD),true) 108 + +install-common: install-systemd 109 + +endif 110 + 111 + install: install-script 112 + 113 + @@ -96,10 +133,10 @@ install-shared: dispatcher-shared install-common 114 + install-static: dispatcher-static install-common 115 + 116 + uninstall: 117 + - $(RM) $(SBINDIR)/usb_modeswitch 118 + - $(RM) $(SBINDIR)/usb_modeswitch_dispatcher 119 + - $(RM) $(UDEVDIR)/usb_modeswitch 120 + - $(RM) $(ETCDIR)/usb_modeswitch.conf 121 + - $(RM) $(MANDIR)/usb_modeswitch.1 122 + + $(RM) $(DESTDIR)$(SBINDIR)/usb_modeswitch 123 + + $(RM) $(DESTDIR)$(SBINDIR)/usb_modeswitch_dispatcher 124 + + $(RM) $(DESTDIR)$(UDEVDIR)/usb_modeswitch 125 + + $(RM) $(DESTDIR)$(ETCDIR)/usb_modeswitch.conf 126 + + $(RM) $(DESTDIR)$(MANDIR)/usb_modeswitch.1 127 + $(RM) -R $(DESTDIR)/var/lib/usb_modeswitch 128 + - $(RM) $(SYSDIR)/usb_modeswitch@.service 129 + + $(RM) $(DESTDIR)$(SYSDIR)/usb_modeswitch@.service 130 + diff --git a/usb-modeswitch-upstart.conf b/usb-modeswitch-upstart.conf 131 + index 0d82b69..1c177b4 100644 132 + --- a/usb-modeswitch-upstart.conf 133 + +++ b/usb-modeswitch-upstart.conf 134 + @@ -1,5 +1,5 @@ 135 + start on usb-modeswitch-upstart 136 + task 137 + script 138 + - exec /usr/sbin/usb_modeswitch_dispatcher --switch-mode $UMS_PARAM 139 + + exec @sbindir@/usb_modeswitch_dispatcher --switch-mode $UMS_PARAM 140 + end script 141 + diff --git a/usb_modeswitch.sh b/usb_modeswitch.sh 142 + index eb3fa3e..0e93166 100755 143 + --- a/usb_modeswitch.sh 144 + +++ b/usb_modeswitch.sh 145 + @@ -1,5 +1,9 @@ 146 + #!/bin/sh 147 + # part of usb_modeswitch 2.5.2 148 + + 149 + +# Compile time configuration, injected by the Makefile 150 + +SBINDIR=/usr/sbin 151 + + 152 + device_in() 153 + { 154 + if [ ! -e /var/lib/usb_modeswitch/$1 ]; then 155 + @@ -37,7 +41,7 @@ if [ $(expr "$1" : "--.*") ]; then 156 + v_id=$3 157 + fi 158 + fi 159 + -PATH=/sbin:/usr/sbin:$PATH 160 + + 161 + case "$1" in 162 + --driver-bind) 163 + # driver binding code removed 164 + @@ -46,9 +50,7 @@ case "$1" in 165 + --symlink-name) 166 + device_in "link_list" $v_id $p_id 167 + if [ "$?" = "1" ]; then 168 + - if [ -e "/usr/sbin/usb_modeswitch_dispatcher" ]; then 169 + - exec usb_modeswitch_dispatcher $1 $2 2>>/dev/null 170 + - fi 171 + + exec $SBINDIR/usb_modeswitch_dispatcher $1 $2 2>>/dev/null 172 + fi 173 + exit 0 174 + ;; 175 + @@ -61,15 +63,13 @@ if [ "$p2" = "" -a "$p1" != "" ]; then 176 + p2=$p1 177 + fi 178 + 179 + -PATH=/bin:/sbin:/usr/bin:/usr/sbin 180 + -init_path=`readlink -f /sbin/init` 181 + -if [ `basename $init_path` = "systemd" ]; then 182 + +if command -v systemctl > /dev/null; then 183 + systemctl --no-block start usb_modeswitch@$p2.service 184 + -elif [ -e "/etc/init/usb-modeswitch-upstart.conf" ]; then 185 + +elif command -v initctl > /dev/null; then 186 + initctl emit --no-wait usb-modeswitch-upstart UMS_PARAM=$p2 187 + else 188 + # only old distros, new udev will kill all subprocesses 189 + exec 1<&- 2<&- 5<&- 7<&- 190 + - exec usb_modeswitch_dispatcher --switch-mode $p2 & 191 + + exec $SBINDIR/usb_modeswitch_dispatcher --switch-mode $p2 & 192 + fi 193 + exit 0 194 + diff --git a/usb_modeswitch.tcl b/usb_modeswitch.tcl 195 + index d2ee50c..8a48751 100755 196 + --- a/usb_modeswitch.tcl 197 + +++ b/usb_modeswitch.tcl 198 + @@ -12,6 +12,16 @@ 199 + # Part of usb-modeswitch-2.5.2 package 200 + # (C) Josua Dietze 2009-2017 201 + 202 + +# Compile-time configuration, injected by the Makefile. 203 + +set setup(sbindir) /usr/sbin 204 + +set setup(etcdir) /etc 205 + + 206 + +# External dependency default location 207 + +set setup(dbdir) /usr/share/usb_modeswitch 208 + + 209 + +# Derived configuration 210 + +set setup(dbdir_etc) $setup(etcdir)/usb_modeswitch.d 211 + + 212 + set arg0 [lindex $argv 0] 213 + if [regexp {\.tcl$} $arg0] { 214 + if [file exists $arg0] { 215 + @@ -91,10 +101,8 @@ if {![regexp {(.*?):.*$} $arg1 d device]} { 216 + } 217 + set flags(logwrite) 1 218 + 219 + -set setup(dbdir) /usr/share/usb_modeswitch 220 + -set setup(dbdir_etc) /etc/usb_modeswitch.d 221 + if {![file exists $setup(dbdir)] && ![file exists $setup(dbdir_etc)]} { 222 + - Log "\nError: no config database found in /usr/share or /etc. Exit" 223 + + Log "\nError: no config database found in $setup(dbdir) or $setup(dbdir_etc). Exit" 224 + SafeExit 225 + } 226 + 227 + @@ -261,7 +269,7 @@ if {$config(NoMBIMCheck)==0 && $usb(bNumConfigurations) > 1} { 228 + if [CheckMBIM] { 229 + Log " driver for MBIM devices is available" 230 + Log "Find MBIM configuration number ..." 231 + - if [catch {set cfgno [exec /usr/sbin/usb_modeswitch -j -Q $busParam $devParam -v $usb(idVendor) -p $usb(idProduct)]} err] { 232 + + if [catch {set cfgno [exec $setup(sbindir)/usb_modeswitch -j -Q $busParam $devParam -v $usb(idVendor) -p $usb(idProduct)]} err] { 233 + Log "Error when trying to find MBIM configuration, switch to legacy modem mode" 234 + } else { 235 + set cfgno [string trim $cfgno] 236 + @@ -297,7 +305,7 @@ if {$report == ""} { 237 + # Now we are actually switching 238 + if $flags(logging) { 239 + Log "Command line:\nusb_modeswitch -W -D $configParam $busParam $devParam -v $usb(idVendor) -p $usb(idProduct) -f \$flags(config)" 240 + - catch {set report [exec /usr/sbin/usb_modeswitch -W -D $configParam $busParam $devParam -v $usb(idVendor) -p $usb(idProduct) -f "$flags(config)" 2>@1]} report 241 + + catch {set report [exec $setup(sbindir)/usb_modeswitch -W -D $configParam $busParam $devParam -v $usb(idVendor) -p $usb(idProduct) -f "$flags(config)" 2>@1]} report 242 + Log "\nVerbose debug output of usb_modeswitch and libusb follows" 243 + Log "(Note that some USB errors are to be expected in the process)" 244 + Log "--------------------------------" 245 + @@ -305,7 +313,7 @@ if {$report == ""} { 246 + Log "--------------------------------" 247 + Log "(end of usb_modeswitch output)\n" 248 + } else { 249 + - catch {set report [exec /usr/sbin/usb_modeswitch -Q -D $configParam $busParam $devParam -v $usb(idVendor) -p $usb(idProduct) -f "$flags(config)" 2>@1]} report 250 + + catch {set report [exec $setup(sbindir)/usb_modeswitch -Q -D $configParam $busParam $devParam -v $usb(idVendor) -p $usb(idProduct) -f "$flags(config)" 2>@1]} report 251 + } 252 + } 253 + 254 + @@ -498,9 +506,9 @@ return 1 255 + 256 + proc {ParseGlobalConfig} {} { 257 + 258 + -global flags 259 + +global flags setup 260 + set configFile "" 261 + -set places [list /etc/usb_modeswitch.conf /etc/sysconfig/usb_modeswitch /etc/default/usb_modeswitch] 262 + +set places [list $setup(etcdir)/usb_modeswitch.conf $setup(etcdir)/sysconfig/usb_modeswitch $setup(etcdir)/default/usb_modeswitch] 263 + foreach cfg $places { 264 + if [file exists $cfg] { 265 + set configFile $cfg 266 + @@ -897,10 +905,12 @@ proc {SysLog} {msg} { 267 + 268 + global flags 269 + if {![info exists flags(logger)]} { 270 + - set flags(logger) "" 271 + - foreach fn {/bin/logger /usr/bin/logger} { 272 + - if [file exists $fn] { 273 + - set flags(logger) $fn 274 + + set flags(logger) [exec sh -c "command -v logger || true"] 275 + + if {$flags(logger) == ""} { 276 + + foreach fn {/bin/logger /usr/bin/logger} { 277 + + if [file exists $fn] { 278 + + set flags(logger) $fn 279 + + } 280 + } 281 + } 282 + Log "Logger is $flags(logger)" 283 + diff --git a/usb_modeswitch@.service b/usb_modeswitch@.service 284 + index f74a8bf..90cb96a 100644 285 + --- a/usb_modeswitch@.service 286 + +++ b/usb_modeswitch@.service 287 + @@ -3,6 +3,6 @@ Description=USB_ModeSwitch_%i 288 + 289 + [Service] 290 + Type=oneshot 291 + -ExecStart=/usr/sbin/usb_modeswitch_dispatcher --switch-mode %i 292 + +ExecStart=@sbindir@/usb_modeswitch_dispatcher --switch-mode %i 293 + #ExecStart=/bin/echo %i 294 +
+5 -2
pkgs/development/tools/misc/usb-modeswitch/data.nix
··· 9 9 sha256 = "0b1wari3aza6qjggqd0hk2zsh93k1q8scgmwh6f8wr0flpr3whff"; 10 10 }; 11 11 12 - inherit (usb-modeswitch) makeFlags; 12 + makeFlags = [ 13 + "PREFIX=$(out)" 14 + "DESTDIR=$(out)" 15 + ]; 13 16 14 17 prePatch = '' 15 - sed -i 's@usb_modeswitch@${usb-modeswitch}/bin/usb_modeswitch@g' 40-usb_modeswitch.rules 18 + sed -i 's@usb_modeswitch@${usb-modeswitch}/lib/udev/usb_modeswitch@g' 40-usb_modeswitch.rules 16 19 ''; 17 20 18 21 # we add tcl here so we can patch in support for new devices by dropping config into
+22 -8
pkgs/development/tools/misc/usb-modeswitch/default.nix
··· 1 - { stdenv, fetchurl, pkgconfig, libusb1 }: 1 + { stdenv, lib, fetchurl, pkgconfig, makeWrapper 2 + , libusb1, tcl, utillinux, coreutils, bash }: 2 3 3 4 stdenv.mkDerivation rec { 4 5 name = "usb-modeswitch-${version}"; ··· 9 10 sha256 = "19ifi80g9ns5dmspchjvfj4ykxssq9yrci8m227dgb3yr04srzxb"; 10 11 }; 11 12 13 + patches = [ ./configurable-usb-modeswitch.patch ]; 14 + 15 + # Remove attempts to write to /etc and /var/lib. 16 + postPatch = '' 17 + sed -i \ 18 + -e '/^\tinstall .* usb_modeswitch.conf/s,$(ETCDIR),$(out)/etc,' \ 19 + -e '\,^\tinstall -d .*/var/lib/usb_modeswitch,d' \ 20 + Makefile 21 + ''; 22 + 12 23 makeFlags = [ 13 - "DESTDIR=$(out)" 14 24 "PREFIX=$(out)" 25 + "ETCDIR=/etc" 26 + "USE_UPSTART=false" 27 + "USE_SYSTEMD=true" 28 + "SYSDIR=$(out)/lib/systemd/system" 29 + "UDEVDIR=$(out)/lib/udev" 15 30 ]; 16 31 17 - # make clean: we always build from source. It should be necessary on x86_64 only 18 - preConfigure = '' 19 - find -type f | xargs sed 's@/bin/rm@rm@g' -i 20 - make clean 32 + postFixup = '' 33 + wrapProgram $out/bin/usb_modeswitch_dispatcher \ 34 + --set PATH ${lib.makeBinPath [ utillinux coreutils bash ]} 21 35 ''; 22 36 23 - buildInputs = [ libusb1 ]; 24 - nativeBuildInputs = [ pkgconfig ]; 37 + buildInputs = [ libusb1 tcl ]; 38 + nativeBuildInputs = [ pkgconfig makeWrapper ]; 25 39 26 40 meta = with stdenv.lib; { 27 41 description = "A mode switching tool for controlling 'multi-mode' USB devices";