Skip to content

Comments

improve and clarify exec functionality#4

Open
spotrh wants to merge 1 commit intoToddAtkins:masterfrom
spotrh:execfixup
Open

improve and clarify exec functionality#4
spotrh wants to merge 1 commit intoToddAtkins:masterfrom
spotrh:execfixup

Conversation

@spotrh
Copy link

@spotrh spotrh commented Nov 9, 2018

This was reported against swatch here: https://bugzilla.redhat.com/show_bug.cgi?id=1622187

  1. The man page (perlpod) 'exec' section is misleading, and slightly incorrect.
  2. The man page 'exec' section says that '$N' variables will be substituted. In fact only the first occurrence is substituted, and if other characters are present (e.g. 'abc$2') then it is not substituted at all.
  3. Using the 'quit' action takes too long. In tests it took around 3 minutes for swatch to actually quit.

Steps to Reproduce:

  1. Install swatchdog and enter 'cd /tmp'.
  2. Create the 'swatchdog.conf' file and enter
    watchfor /something(\d*)/
    exec echo $1$1
    quit
  3. Run 'swatchdog --config-file=/tmp/swatchdog.conf --script-dir=. --awk-field-syntax'
  4. From another terminal, run 'logger something32'
  5. Stop swatchdog (or wait for it to timeout)
  6. Modify the config file to use 'exec echo :$1 $1:'
  7. Run swatchdog again
  8. From another terminal, run 'logger something64'
  9. Stop swatchdog
  10. Modify the config file to use 'exec echo :$0 $*:'
  11. Run swatchdog again
  12. From another terminal, run 'logger something82'

Actual results:

(From step 4) output shown is 'Aug' (correct since the first part of the log file line begins with the date/time, but wrong in that the '$1' should appear twice. Adding '--dump-script' to the command-line shows that only one '$1' is substituted. Without the awk-field-syntax option, the correct output is shown.

(From step 5) The 'quit' should execute immediately. It does not because it is actually waiting on the 'tail' command pipe used for input. The wait does eventually time out, not sure what causes that though (tail, perl, exit?)

(From step 8) output shown is 'something64 Aug'. Because of the first colon (:), the $1 is not substituted correctly - it ends up displaying the last token of the log line ('something64'). The second $1 is again substituted correctly, but where have the colons gone? Without the awk-field-syntax, the correct output is shown.

(From step 12) The output is correct. However, if the '--awk-field-option' is removed, an error is shown indicating that '$* is no longer supported'. The output also shows ':./.swatchdog_script.30491 :'. The '$0' is being interpreted as the actual swatchdog script being run.

Expected results:

(From step 4) output should be 'AugAug' when awk-field-option used, and '3232' when it isn't.

(From step 5) The 'quit' should cause swatchdog to immediately exit cleanly.

(From step 8) output should be ':Aug Aug:' when awk field option used, and ':64 64:' when it isn't used.

(From step 12) With the awk fields option, the output should show the whole log line twice, separated by a space and beginning and ending with a colon. Without the awk fields option the output should be exactly the same.

Additional info:

The man page implies that with 'exec' $N variables can be used, but does not specify that their meaning is different depending on whether the '--awk-fields-syntax' option is used or not.

It also implies that '$0' and '$' will be substituted in all cases. This only happens when the awk fields option is used. However, since without the option these variables end up being interpreted by the shell, we will get the $0 interpreted as the swatchdog script name and $ as nothing (since no parameters are passed to the script). It is probably better, more user friendly, if $0 and $* are substituted in all cases (regardless of the awk fields option).

There is no accounting taken for the fact that a variable may be escaped. Entering '$0' for example, should not be interpreted (regardless of any options).

Swatchdog currently calls 'exit' when the 'quit' action is seen. But this waits on the tail command to finish. It is better to call the 'goodbye' function already written into the script, and used to handle signals. This will then kill the tail pid, close any pipes and then exit. It happens immediately.

This PR resolves all the above problems, and rewrites most of the 'exec' part of the man page. In addition, all the variables are substituted - provided they are not escaped. Currently, as shown in test step 4 above, only the first variable is substituted. Any others are ignored (so never shown in the output).

It also fixes one typo in the man page.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant