Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Exec] Properly split command & pipe support #6819

Merged
merged 8 commits into from
Jan 25, 2020
Merged

[Exec] Properly split command & pipe support #6819

merged 8 commits into from
Jan 25, 2020

Conversation

cpiber
Copy link

@cpiber cpiber commented Jan 12, 2020

Signed-off-by: Constantin Piber cp.piber@gmail.com

- Implemented @@ manual split (@see https://www.openhab.org/docs/configuration/actions.html#exec-actions)
- Pass to shell if not manually split (detect shell via https://stackoverflow.com/a/31547504/7508309, permission by author)
Fixes #6729
Added benefit: Pass to shell allows for pipes

Signed-off-by: Constantin Piber <cp.piber@gmail.com>
@cpiber cpiber requested a review from kgoderis as a code owner January 12, 2020 11:16
@TravisBuddy
Copy link

Travis tests were successful

Hey @cpiber,
we found no major flaws with your code. Still you might want to look at this logfile, as we usually suggest some optional improvements.

@cpiber
Copy link
Author

cpiber commented Jan 12, 2020

Similar to #6815, but allows for pipes and doesn't change output buffer functionality

Copy link
Contributor

@9037568 9037568 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One major concern with this is it will break anyone who is already passing bash or cmd as the command to be executed. Might still be worth doing, but we may need additional input (@J-N-K or @kaikreuzer ?).

Some fixes will be needed, in any event.

Changes as suggested by @9037568

Signed-off-by: Constantin Piber <cp.piber@gmail.com>
Copy link
Author

@cpiber cpiber left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@9037568 See comments and new commit

@TravisBuddy
Copy link

Travis tests were successful

Hey @cpiber,
we found no major flaws with your code. Still you might want to look at this logfile, as we usually suggest some optional improvements.

Signed-off-by: Constantin Piber <cp.piber@gmail.com>
@TravisBuddy
Copy link

Travis tests were successful

Hey @cpiber,
we found no major flaws with your code. Still you might want to look at this logfile, as we usually suggest some optional improvements.

@Hilbrand Hilbrand added the enhancement An enhancement or new feature for an existing add-on label Jan 13, 2020
@Rossko57
Copy link
Contributor

Comment only; please don't forget us Windows users. (I have no idea if any this has an impact there.)

@cpiber
Copy link
Author

cpiber commented Jan 13, 2020

Comment only; please don't forget us Windows users. (I have no idea if any this has an impact there.)

Sorry, new to this.

Tested on Linux with: echo "test space" | grep " ", output: test space (correct)
Tested on Windows: echo "1" | echo "2", output: 2 (correct, need a command to test spaces as well, should work though)
So, on all OSes, the only change should be that quotes are now respected (and pipes enabled), so no breaking changes.

Copy link
Contributor

@9037568 9037568 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed latest changes with commentary.

@9037568 9037568 added the awaiting feedback Awaiting feedback from the pull request author label Jan 16, 2020
Signed-off-by: Constantin Piber <cp.piber@gmail.com>
Signed-off-by: Constantin Piber <cp.piber@gmail.com>
@TravisBuddy
Copy link

Travis tests were successful

Hey @cpiber,
we found no major flaws with your code. Still you might want to look at this logfile, as we usually suggest some optional improvements.

Copy link
Contributor

@9037568 9037568 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since I haven't seen feedback from TPTB, I guess I'll have to ask you to fix the breakage issue, too.
In order to not break existing setups, each of the conditions starting at line 198 needs to check if the received command already starts with one of the shell commands, and if so, not prepend the shell command to the received command.

Please also eliminate the remaining occurrences of commandLine.toString().

Signed-off-by: Constantin Piber <cp.piber@gmail.com>
@TravisBuddy
Copy link

Travis tests were successful

Hey @cpiber,
we found no major flaws with your code. Still you might want to look at this logfile, as we usually suggest some optional improvements.

@9037568 9037568 removed the awaiting feedback Awaiting feedback from the pull request author label Jan 21, 2020
Copy link
Contributor

@9037568 9037568 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be the last set of changes needed...

@9037568 9037568 added the awaiting feedback Awaiting feedback from the pull request author label Jan 22, 2020
only invoke shell if the first WORD is a shell-program, not just the
beginning of the string (ex. shout shouldn't match)

Signed-off-by: Constantin Piber <cp.piber@gmail.com>
@TravisBuddy
Copy link

Travis tests were successful

Hey @cpiber,
we found no major flaws with your code. Still you might want to look at this logfile, as we usually suggest some optional improvements.

Copy link
Contributor

@9037568 9037568 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks for this work, @cpiber !

@9037568 9037568 removed the awaiting feedback Awaiting feedback from the pull request author label Jan 22, 2020
} else {
// Invoke shell with 'c' option and pass string
logger.debug("Passing to shell for parsing command.");
if (getOperatingSystemType() == OS.WINDOWS) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a question here: Why don't you get the operating system type once and then use the local variable in a switch-statement. Are your sure that everything other than OS.WINDOWS and OS.UNKNOWN is a supported *nix? And please add the unsupported OS-name to the warn-message logger.warn("OS '{}' not supported, please split commands manually!", getOperatingSystemName());).

Edit: I found it below. But the if-else is not very intuitive here. Please use a switch-statement.


Process proc = null;
try {
proc = rt.exec(commandLine.toString());
proc = rt.exec(cmdArray);
} catch (Exception e) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it necessary to catch Exception here? According to the docs there is a limited number of exceptions, I think we can omit the NPE if we make sure that none of the commands is null (which should be done anyway).

@@ -193,7 +245,7 @@ public void run() {
isr.close();
} catch (IOException e) {
logger.error("An exception occurred while reading the stdout when executing '{}' : '{}'",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And this should be warn, also below. error is reserved for the framework of occasions where the stability of the system may be affected.

return System.getProperty("os.name");
}

}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please run mvn spotless:apply after the code issues are done.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What exactly does that do?

Copy link
Author

@cpiber cpiber Jan 23, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay so it runs some more tests, I get
[ERROR] Failed to execute goal com.diffplug.spotless:spotless-maven-plugin:1.24.3:apply (default-cli) on project org.openhab.binding.feed.tests: Execution default-cli of goal com.diffplug.spotless:spotless-maven-plugin:1.24.3:apply failed: Overlapping text edits -> [Help 1]
What do I do? Can't find any info on that.
Also doesn't seem to by because of my changes (I didn't touch feed binding, at least I don't think)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reset the changes in other bindings and use mvn spotless:apply -pl bundles\org.openhab.binding.exec to only re-format the exec binding.

Signed-off-by: Constantin Piber <cp.piber@gmail.com>
@TravisBuddy
Copy link

Travis tests were successful

Hey @cpiber,
we found no major flaws with your code. Still you might want to look at this logfile, as we usually suggest some optional improvements.

@J-N-K J-N-K merged commit fecfacb into openhab:2.5.x Jan 25, 2020
andibraeu pushed a commit to andibraeu/openhab-addons that referenced this pull request Feb 17, 2020
* [Exec] Properly split command

Signed-off-by: Constantin Piber <cp.piber@gmail.com>
@kaikreuzer kaikreuzer added this to the 2.5.2 milestone Feb 19, 2020
leluna pushed a commit to leluna/openhab2-addons that referenced this pull request Mar 21, 2020
* [Exec] Properly split command

Signed-off-by: Constantin Piber <cp.piber@gmail.com>
Signed-off-by: leluna <hengrui.jiang@googlemail.com>
Hans-Reiner pushed a commit to Hans-Reiner/openhab2-addons that referenced this pull request Apr 11, 2020
* [Exec] Properly split command

Signed-off-by: Constantin Piber <cp.piber@gmail.com>
Signed-off-by: Hans-Reiner Hoffmann <hans-reiner.hoffmann@gmx.de>
markus7017 pushed a commit to markus7017/openhab-addons that referenced this pull request May 29, 2020
* [Exec] Properly split command

Signed-off-by: Constantin Piber <cp.piber@gmail.com>
@elordude
Copy link

I used to have a exec thing to track CPU/MOBO temperatures, it worked fine for more than a year until the recent updates.

I have tired multiple combinations for the command (below) and none of the work, i have updated the misc/exec.whitelist every time, the error I get is shown below.
/bin/cat /sys/class/hwmon/hwmon2/temp3_input
cat /sys/class/hwmon/hwmon2/temp3_input
sudo /bin/cat /sys/class/hwmon/hwmon2/temp3_input

Thing exec:command:core1temp "System" @ "Exec" [ command="/bin/cat /sys/class/hwmon/hwmon2/temp3_input", transform="JS(milli.js)", interval=10, autorun=true ]

2020-06-23 11:55:47.099 [WARN ] [mmon.WrappedScheduledExecutorService] - Scheduled runnable ended with an exception:
java.lang.NullPointerException: null
at org.openhab.transform.javascript.internal.JavaScriptEngineManager.getScript(JavaScriptEngineManager.java:68) ~[?:?]
at org.openhab.transform.javascript.internal.JavaScriptTransformationService.transform(JavaScriptTransformationService.java:74) ~[?:?]
at org.openhab.binding.exec.internal.handler.ExecHandler.transformResponse(ExecHandler.java:314) ~[?:?]
at org.openhab.binding.exec.internal.handler.ExecHandler.execute(ExecHandler.java:293) ~[?:?]
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) ~[?:1.8.0_252]
at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308) ~[?:1.8.0_252]
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180) ~[?:1.8.0_252]
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294) ~[?:1.8.0_252]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [?:1.8.0_252]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [?:1.8.0_252]
at java.lang.Thread.run(Thread.java:748) [?:1.8.0_252]

@Rossko57
Copy link
Contributor

Not much point opening a months old pull request, just because it mentions exec.

You appear to have run into the javascript issue described here -
#7880

andrewfg pushed a commit to andrewfg/openhab-addons that referenced this pull request Aug 31, 2020
* [Exec] Properly split command

Signed-off-by: Constantin Piber <cp.piber@gmail.com>
andrewfg pushed a commit to andrewfg/openhab-addons that referenced this pull request Aug 31, 2020
* [Exec] Properly split command

Signed-off-by: Constantin Piber <cp.piber@gmail.com>
andrewfg pushed a commit to andrewfg/openhab-addons that referenced this pull request Aug 31, 2020
* [Exec] Properly split command

Signed-off-by: Constantin Piber <cp.piber@gmail.com>
andrewfg pushed a commit to andrewfg/openhab-addons that referenced this pull request Aug 31, 2020
* [Exec] Properly split command

Signed-off-by: Constantin Piber <cp.piber@gmail.com>
DaanMeijer pushed a commit to DaanMeijer/openhab-addons that referenced this pull request Sep 1, 2020
* [Exec] Properly split command

Signed-off-by: Constantin Piber <cp.piber@gmail.com>
Signed-off-by: Daan Meijer <daan@studioseptember.nl>
markus7017 pushed a commit to markus7017/openhab-addons that referenced this pull request Sep 19, 2020
* [Exec] Properly split command

Signed-off-by: Constantin Piber <cp.piber@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement An enhancement or new feature for an existing add-on
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[exec] Cannot use quoted spaces in commands
8 participants