From da513afc7db4f3b7a1ab88e775f102fdd6d3513c Mon Sep 17 00:00:00 2001 From: Philippe Troin Date: Sun, 22 Mar 2020 13:14:34 -0700 Subject: [PATCH 1/4] Log to a per-user directory. Logging to /tmp with fixed names is a security risk. Log to /run/user/1000/pulse directory if XDG_RUNTIME_DIR is available, otherwise log to a per-user directory in /tmp. --- pajackconnect | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/pajackconnect b/pajackconnect index 50ab119..db180ee 100755 --- a/pajackconnect +++ b/pajackconnect @@ -46,6 +46,13 @@ # variable to fetch pasuspender status p=`ps ax | grep pasuspender` +if [ "${XDG_RUNTIME_DIR-}" != "" ] +then + pajack_dir="$XDG_RUNTIME_DIR/pulse" +else + pajack_dir="/tmp/pajackconnect-${LOGNAME-${USER-}}" +fi +mkdir -m 700 -p "$pajack_dir" function restart() { # reload pulse jack module after resume from suspend. @@ -66,16 +73,16 @@ function restart() { # check if pulseaudio is running pa=`pidof pulseaudio | wc -w` # write results to log file - `date +%H:%M:%S >>/tmp/pajack.log` - `echo 'jack-sink =' $jc >>/tmp/pajack.log` - `echo 'jack =' $ja >>/tmp/pajack.log` - `echo 'pulse =' $pa >>/tmp/pajack.log` + `date +%H:%M:%S >>"$pajack_dir/pajack.log"` + `echo 'jack-sink =' $jc >>"$pajack_dir/pajack.log"` + `echo 'jack =' $ja >>"$pajack_dir/pajack.log"` + `echo 'pulse =' $pa >>"$pajack_dir/pajack.log"` if [[ ! "$jc" =~ "jack" ]] && [[ "$ja" == "1" ]]; then # if pulseaudio didn't run, let's try start it if [[ ! "$pa" == "1" ]]; then sleep 5 `pulseaudio -D` - `echo reload pulseaudio >>/tmp/pajack.log` + `echo reload pulseaudio >>"$pajack_dir/pajack.log"` sleep 10 fi # if jack is running and pulse jack modules aren't loaded, @@ -87,13 +94,13 @@ function restart() { # move around source port, seems to make pulseaudio work with jack # otherwise it seems that pulseaudio is connected, but it didn't work. pacmd set-default-source jack_in && sleep 1 - source=`cat /tmp/pasourcej` + source=`cat "$pajack_dir"/pasourcej` pacmd set-default-source $source && sleep 1 pacmd set-default-source jack_in # echo "pa source switched" - `echo reloaded jack modules >>/tmp/pajack.log` + `echo reloaded jack modules >>"$pajack_dir/pajack.log"` else - `echo already run >>/tmp/pajack.log` + `echo already run >>"$pajack_dir/pajack.log"` fi fi } @@ -107,12 +114,12 @@ function start() { exit 0 else # if pasuspender isn't waiting for jack, let's fetch the current - # ports, pulseaudio use, save them to /tmp, for later use. + # ports, pulseaudio use, save them to "$pajack_dir", for later use. #echo "pasuspender is NOT active" sink=`pacmd list short sinks | sed -n "/Default sink name/p"| sed "s/Default sink name://"` - `echo $sink >/tmp/pasinkj` + `echo $sink >"$pajack_dir"/pasinkj` source=`pacmd list short sinks | sed -n "/Default source name/p"| sed "s/Default source name://"` - `echo $source >/tmp/pasourcej` + `echo $source >"$pajack_dir"/pasourcej` # load the pulseaudio jack module and set them as default ports pacmd load-module module-jack-sink channels=2 pacmd load-module module-jack-source channels=2 @@ -153,8 +160,8 @@ function reset() { else # fetch the previous saved pulse audio ports and set them back # as default ports for pulseaudio. Done. - sink=`cat /tmp/pasinkj` - source=`cat /tmp/pasourcej` + sink=`cat "$pajack_dir"/pasinkj` + source=`cat "$pajack_dir"/pasourcej` pacmd set-default-sink $sink pacmd set-default-source $source #echo "pasuspender is NOT active" From 114151407af474bc7fd4a2278ce666535e531728 Mon Sep 17 00:00:00 2001 From: Philippe Troin Date: Sun, 22 Mar 2020 13:15:04 -0700 Subject: [PATCH 2/4] Avoid grepping grep itself. --- pajackconnect | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pajackconnect b/pajackconnect index db180ee..03b9615 100755 --- a/pajackconnect +++ b/pajackconnect @@ -45,7 +45,7 @@ # variable to fetch pasuspender status -p=`ps ax | grep pasuspender` +p=`ps ax | grep '[p]asuspender'` if [ "${XDG_RUNTIME_DIR-}" != "" ] then pajack_dir="$XDG_RUNTIME_DIR/pulse" From fb9260e3ed76b6ba118883166224a8be603efc6e Mon Sep 17 00:00:00 2001 From: Philippe Troin Date: Sun, 22 Mar 2020 13:16:23 -0700 Subject: [PATCH 3/4] Remove unnecessary back-ticks (command expansions). --- pajackconnect | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/pajackconnect b/pajackconnect index 03b9615..b172683 100755 --- a/pajackconnect +++ b/pajackconnect @@ -73,16 +73,16 @@ function restart() { # check if pulseaudio is running pa=`pidof pulseaudio | wc -w` # write results to log file - `date +%H:%M:%S >>"$pajack_dir/pajack.log"` - `echo 'jack-sink =' $jc >>"$pajack_dir/pajack.log"` - `echo 'jack =' $ja >>"$pajack_dir/pajack.log"` - `echo 'pulse =' $pa >>"$pajack_dir/pajack.log"` + date +%H:%M:%S >>"$pajack_dir/pajack.log" + echo 'jack-sink =' $jc >>"$pajack_dir/pajack.log" + echo 'jack =' $ja >>"$pajack_dir/pajack.log" + echo 'pulse =' $pa >>"$pajack_dir/pajack.log" if [[ ! "$jc" =~ "jack" ]] && [[ "$ja" == "1" ]]; then # if pulseaudio didn't run, let's try start it if [[ ! "$pa" == "1" ]]; then sleep 5 - `pulseaudio -D` - `echo reload pulseaudio >>"$pajack_dir/pajack.log"` + pulseaudio -D + echo reload pulseaudio >>"$pajack_dir/pajack.log" sleep 10 fi # if jack is running and pulse jack modules aren't loaded, @@ -98,9 +98,9 @@ function restart() { pacmd set-default-source $source && sleep 1 pacmd set-default-source jack_in # echo "pa source switched" - `echo reloaded jack modules >>"$pajack_dir/pajack.log"` + echo reloaded jack modules >>"$pajack_dir/pajack.log" else - `echo already run >>"$pajack_dir/pajack.log"` + echo already run >>"$pajack_dir/pajack.log" fi fi } @@ -117,9 +117,9 @@ function start() { # ports, pulseaudio use, save them to "$pajack_dir", for later use. #echo "pasuspender is NOT active" sink=`pacmd list short sinks | sed -n "/Default sink name/p"| sed "s/Default sink name://"` - `echo $sink >"$pajack_dir"/pasinkj` + echo $sink >"$pajack_dir"/pasinkj source=`pacmd list short sinks | sed -n "/Default source name/p"| sed "s/Default source name://"` - `echo $source >"$pajack_dir"/pasourcej` + echo $source >"$pajack_dir"/pasourcej # load the pulseaudio jack module and set them as default ports pacmd load-module module-jack-sink channels=2 pacmd load-module module-jack-source channels=2 From 1ab2f91d3fc975e34cde400e709e68f90817633b Mon Sep 17 00:00:00 2001 From: Philippe Troin Date: Sun, 22 Mar 2020 13:18:15 -0700 Subject: [PATCH 4/4] Fix indentation and white-space. --- pajackconnect | 46 +++++++++++++++++++++++----------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/pajackconnect b/pajackconnect index b172683..7fb062a 100755 --- a/pajackconnect +++ b/pajackconnect @@ -21,10 +21,10 @@ # # in the field labelled “Execute script after Shutdown”. # -# for use jack without pulseaudio, add in Qjackctl setting window -# in the serverpath field 'pasuspender -- ' before 'jackd', save settings -# as "No Pulse" for example. Remove 'pasuspender -- ', -# and save settings as 'Pulse'. Now you can select from the +# for use jack without pulseaudio, add in Qjackctl setting window +# in the serverpath field 'pasuspender -- ' before 'jackd', save settings +# as "No Pulse" for example. Remove 'pasuspender -- ', +# and save settings as 'Pulse'. Now you can select from the # Qjackctl setting window, if you would start jack with or without pulse. # # References: @@ -39,12 +39,12 @@ # overall script formating by Hermann Meyer # added possibility to run jack with suspend pulse by Hermann Meyer # added save and restore previous used pulseaudio ports by Hermann Meyer -# added reload pulse jack modules after resume from suspend +# added reload pulse jack modules after resume from suspend # (needs to be called by a systemd.service file) by Hermann Meyer #- -# variable to fetch pasuspender status +# variable to fetch pasuspender status p=`ps ax | grep '[p]asuspender'` if [ "${XDG_RUNTIME_DIR-}" != "" ] then @@ -66,17 +66,17 @@ function restart() { #echo "pasuspender is active" exit 0 else - # check if pulse jack modules are loaded - jc=`pacmd list | grep jack-sink` - # check if jack is running - ja=`pidof jackd | wc -w` - # check if pulseaudio is running - pa=`pidof pulseaudio | wc -w` - # write results to log file - date +%H:%M:%S >>"$pajack_dir/pajack.log" - echo 'jack-sink =' $jc >>"$pajack_dir/pajack.log" - echo 'jack =' $ja >>"$pajack_dir/pajack.log" - echo 'pulse =' $pa >>"$pajack_dir/pajack.log" + # check if pulse jack modules are loaded + jc=`pacmd list | grep jack-sink` + # check if jack is running + ja=`pidof jackd | wc -w` + # check if pulseaudio is running + pa=`pidof pulseaudio | wc -w` + # write results to log file + date +%H:%M:%S >>"$pajack_dir/pajack.log" + echo 'jack-sink =' $jc >>"$pajack_dir/pajack.log" + echo 'jack =' $ja >>"$pajack_dir/pajack.log" + echo 'pulse =' $pa >>"$pajack_dir/pajack.log" if [[ ! "$jc" =~ "jack" ]] && [[ "$ja" == "1" ]]; then # if pulseaudio didn't run, let's try start it if [[ ! "$pa" == "1" ]]; then @@ -103,7 +103,7 @@ function restart() { echo already run >>"$pajack_dir/pajack.log" fi fi -} + } function start() { # if command-line option start is given @@ -137,7 +137,7 @@ function start() { function stop() { # if command-line option stop is given # again, check if pasuspender waiting for jack, if not, unload the pulse - # jack module and reload the module-suspend-on-idle. + # jack module and reload the module-suspend-on-idle. if [[ ! "$p" =~ "jackd" ]]; then #echo "pasuspender is NOT active" pacmd unload-module module-jack-sink @@ -145,7 +145,7 @@ function stop() { pacmd load-module module-suspend-on-idle else # if pasuspender waiting for jack, add a timeout to pasuspender - # to allow a smooth reset action afterward. + # to allow a smooth reset action afterward. #echo "pasuspender is active" pasuspender -- sleep 5 & fi @@ -158,12 +158,12 @@ function reset() { #echo "pasuspender is active" exit 0 else - # fetch the previous saved pulse audio ports and set them back + # fetch the previous saved pulse audio ports and set them back # as default ports for pulseaudio. Done. sink=`cat "$pajack_dir"/pasinkj` source=`cat "$pajack_dir"/pasourcej` - pacmd set-default-sink $sink - pacmd set-default-source $source + pacmd set-default-sink $sink + pacmd set-default-source $source #echo "pasuspender is NOT active" fi }