2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.exec.internal.handler;
15 import static org.openhab.binding.exec.internal.ExecBindingConstants.*;
17 import java.io.BufferedReader;
18 import java.io.IOException;
19 import java.io.InputStreamReader;
20 import java.math.BigDecimal;
21 import java.time.ZonedDateTime;
22 import java.util.Arrays;
23 import java.util.Calendar;
24 import java.util.Date;
25 import java.util.IllegalFormatException;
26 import java.util.concurrent.ScheduledFuture;
27 import java.util.concurrent.TimeUnit;
28 import java.util.regex.Matcher;
29 import java.util.regex.Pattern;
30 import java.util.regex.PatternSyntaxException;
32 import org.apache.commons.lang3.StringUtils;
33 import org.eclipse.jdt.annotation.NonNullByDefault;
34 import org.eclipse.jdt.annotation.Nullable;
35 import org.openhab.binding.exec.internal.ExecWhitelistWatchService;
36 import org.openhab.core.library.types.DateTimeType;
37 import org.openhab.core.library.types.DecimalType;
38 import org.openhab.core.library.types.OnOffType;
39 import org.openhab.core.library.types.StringType;
40 import org.openhab.core.thing.ChannelUID;
41 import org.openhab.core.thing.Thing;
42 import org.openhab.core.thing.ThingStatus;
43 import org.openhab.core.thing.binding.BaseThingHandler;
44 import org.openhab.core.transform.TransformationException;
45 import org.openhab.core.transform.TransformationHelper;
46 import org.openhab.core.transform.TransformationService;
47 import org.openhab.core.types.Command;
48 import org.openhab.core.types.RefreshType;
49 import org.osgi.framework.BundleContext;
50 import org.osgi.framework.FrameworkUtil;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
55 * The {@link ExecHandler} is responsible for handling commands, which are
56 * sent to one of the channels.
58 * @author Karel Goderis - Initial contribution
59 * @author Constantin Piber - Added better argument support (delimiter and pass to shell)
60 * @author Jan N. Klug - Add command whitelist check
63 public class ExecHandler extends BaseThingHandler {
65 * Use this to separate between command and parameter, and also between parameters.
67 public static final String CMD_LINE_DELIMITER = "@@";
72 public static final String[] SHELL_WINDOWS = new String[] { "cmd" };
73 public static final String[] SHELL_NIX = new String[] { "sh", "bash", "zsh", "csh" };
74 private final ExecWhitelistWatchService execWhitelistWatchService;
76 private Logger logger = LoggerFactory.getLogger(ExecHandler.class);
78 private final BundleContext bundleContext;
80 // List of Configurations constants
81 public static final String INTERVAL = "interval";
82 public static final String TIME_OUT = "timeout";
83 public static final String COMMAND = "command";
84 public static final String TRANSFORM = "transform";
85 public static final String AUTORUN = "autorun";
87 // RegEx to extract a parse a function String <code>'(.*?)\((.*)\)'</code>
88 private static final Pattern EXTRACT_FUNCTION_PATTERN = Pattern.compile("(.*?)\\((.*)\\)");
90 private @Nullable ScheduledFuture<?> executionJob;
91 private @Nullable String lastInput;
93 private static Runtime rt = Runtime.getRuntime();
95 public ExecHandler(Thing thing, ExecWhitelistWatchService execWhitelistWatchService) {
97 this.bundleContext = FrameworkUtil.getBundle(ExecHandler.class).getBundleContext();
98 this.execWhitelistWatchService = execWhitelistWatchService;
102 public void handleCommand(ChannelUID channelUID, Command command) {
103 if (command instanceof RefreshType) {
104 // Placeholder for later refinement
106 if (channelUID.getId().equals(RUN)) {
107 if (command instanceof OnOffType) {
108 if (command == OnOffType.ON) {
109 scheduler.schedule(this::execute, 0, TimeUnit.SECONDS);
112 } else if (channelUID.getId().equals(INPUT)) {
113 if (command instanceof StringType) {
114 String previousInput = lastInput;
115 lastInput = command.toString();
116 if (lastInput != null && !lastInput.equals(previousInput)) {
117 if (getConfig().get(AUTORUN) != null && ((Boolean) getConfig().get(AUTORUN))) {
118 logger.trace("Executing command '{}' after a change of the input channel to '{}'",
119 getConfig().get(COMMAND), lastInput);
120 scheduler.schedule(this::execute, 0, TimeUnit.SECONDS);
129 public void initialize() {
130 if (executionJob == null || executionJob.isCancelled()) {
131 if ((getConfig().get(INTERVAL)) != null && ((BigDecimal) getConfig().get(INTERVAL)).intValue() > 0) {
132 int pollingInterval = ((BigDecimal) getConfig().get(INTERVAL)).intValue();
133 executionJob = scheduler.scheduleWithFixedDelay(this::execute, 0, pollingInterval, TimeUnit.SECONDS);
137 updateStatus(ThingStatus.ONLINE);
141 public void dispose() {
142 if (executionJob != null && !executionJob.isCancelled()) {
143 executionJob.cancel(true);
148 public void execute() {
149 String commandLine = (String) getConfig().get(COMMAND);
150 if (!execWhitelistWatchService.isWhitelisted(commandLine)) {
151 logger.warn("Tried to execute '{}', but it is not contained in whitelist.", commandLine);
156 if ((getConfig().get(TIME_OUT)) != null) {
157 timeOut = ((BigDecimal) getConfig().get(TIME_OUT)).intValue() * 1000;
160 if (commandLine != null && !commandLine.isEmpty()) {
161 updateState(RUN, OnOffType.ON);
163 // For some obscure reason, when using Apache Common Exec, or using a straight implementation of
164 // Runtime.Exec(), on Mac OS X (Yosemite and El Capitan), there seems to be a lock race condition
165 // randomly appearing (on UNIXProcess) *when* one tries to gobble up the stdout and sterr output of the
166 // subprocess in separate threads. It seems to be common "wisdom" to do that in separate threads, but
167 // only when keeping everything between .exec() and .waitfor() in the same thread, this lock race
168 // condition seems to go away. This approach of not reading the outputs in separate threads *might* be a
169 // problem for external commands that generate a lot of output, but this will be dependent on the limits
170 // of the underlying operating system.
172 Date date = Calendar.getInstance().getTime();
174 if (lastInput != null) {
175 commandLine = String.format(commandLine, date, lastInput);
177 commandLine = String.format(commandLine, date);
179 } catch (IllegalFormatException e) {
181 "An exception occurred while formatting the command line '{}' with the current time '{}' and input value '{}': {}",
182 commandLine, date, lastInput, e.getMessage());
183 updateState(RUN, OnOffType.OFF);
184 updateState(OUTPUT, new StringType(e.getMessage()));
190 if (commandLine.contains(CMD_LINE_DELIMITER)) {
191 logger.debug("Splitting by '{}'", CMD_LINE_DELIMITER);
193 cmdArray = commandLine.split(CMD_LINE_DELIMITER);
194 } catch (PatternSyntaxException e) {
195 logger.warn("An exception occurred while splitting '{}' : '{}'", commandLine, e.getMessage());
196 updateState(RUN, OnOffType.OFF);
197 updateState(OUTPUT, new StringType(e.getMessage()));
201 // Invoke shell with 'c' option and pass string
202 logger.debug("Passing to shell for parsing command.");
203 switch (getOperatingSystemType()) {
205 shell = SHELL_WINDOWS;
206 logger.debug("OS: WINDOWS ({})", getOperatingSystemName());
207 cmdArray = createCmdArray(shell, "/c", commandLine);
213 // assume sh is present, should all be POSIX-compliant
215 logger.debug("OS: *NIX ({})", getOperatingSystemName());
216 cmdArray = createCmdArray(shell, "-c", commandLine);
219 logger.debug("OS: Unknown ({})", getOperatingSystemName());
220 logger.warn("OS {} not supported, please manually split commands!", getOperatingSystemName());
221 updateState(RUN, OnOffType.OFF);
222 updateState(OUTPUT, new StringType("OS not supported, please manually split commands!"));
227 if (cmdArray.length == 0) {
228 logger.trace("Empty command received, not executing");
232 logger.trace("The command to be executed will be '{}'", Arrays.asList(cmdArray));
236 proc = rt.exec(cmdArray);
237 } catch (Exception e) {
238 logger.warn("An exception occurred while executing '{}' : '{}'", Arrays.asList(cmdArray),
240 updateState(RUN, OnOffType.OFF);
241 updateState(OUTPUT, new StringType(e.getMessage()));
245 StringBuilder outputBuilder = new StringBuilder();
246 StringBuilder errorBuilder = new StringBuilder();
248 try (InputStreamReader isr = new InputStreamReader(proc.getInputStream());
249 BufferedReader br = new BufferedReader(isr)) {
251 while ((line = br.readLine()) != null) {
252 outputBuilder.append(line).append("\n");
253 logger.debug("Exec [{}]: '{}'", "OUTPUT", line);
255 } catch (IOException e) {
256 logger.warn("An exception occurred while reading the stdout when executing '{}' : '{}'", commandLine,
260 try (InputStreamReader isr = new InputStreamReader(proc.getErrorStream());
261 BufferedReader br = new BufferedReader(isr)) {
263 while ((line = br.readLine()) != null) {
264 errorBuilder.append(line).append("\n");
265 logger.debug("Exec [{}]: '{}'", "ERROR", line);
267 } catch (IOException e) {
268 logger.warn("An exception occurred while reading the stderr when executing '{}' : '{}'", commandLine,
272 boolean exitVal = false;
274 exitVal = proc.waitFor(timeOut, TimeUnit.MILLISECONDS);
275 } catch (InterruptedException e) {
276 logger.warn("An exception occurred while waiting for the process ('{}') to finish : '{}'", commandLine,
281 logger.warn("Forcibly termininating the process ('{}') after a timeout of {} ms", commandLine, timeOut);
282 proc.destroyForcibly();
285 updateState(RUN, OnOffType.OFF);
286 updateState(EXIT, new DecimalType(proc.exitValue()));
288 outputBuilder.append(errorBuilder.toString());
290 outputBuilder.append(errorBuilder.toString());
292 String transformedResponse = StringUtils.chomp(outputBuilder.toString());
293 String transformation = (String) getConfig().get(TRANSFORM);
295 if (transformation != null && transformation.length() > 0) {
296 transformedResponse = transformResponse(transformedResponse, transformation);
299 updateState(OUTPUT, new StringType(transformedResponse));
301 DateTimeType stampType = new DateTimeType(ZonedDateTime.now());
302 updateState(LAST_EXECUTION, stampType);
306 protected @Nullable String transformResponse(String response, String transformation) {
307 String transformedResponse;
310 String[] parts = splitTransformationConfig(transformation);
311 String transformationType = parts[0];
312 String transformationFunction = parts[1];
314 TransformationService transformationService = TransformationHelper.getTransformationService(bundleContext,
316 if (transformationService != null) {
317 transformedResponse = transformationService.transform(transformationFunction, response);
319 transformedResponse = response;
320 logger.warn("Couldn't transform response because transformationService of type '{}' is unavailable",
323 } catch (TransformationException te) {
324 logger.warn("An exception occurred while transforming '{}' with '{}' : '{}'", response, transformation,
327 // in case of an error we return the response without any transformation
328 transformedResponse = response;
331 logger.debug("Transformed response is '{}'", transformedResponse);
332 return transformedResponse;
336 * Splits a transformation configuration string into its two parts - the
337 * transformation type and the function/pattern to apply.
339 * @param transformation the string to split
340 * @return a string array with exactly two entries for the type and the function
342 protected String[] splitTransformationConfig(String transformation) {
343 Matcher matcher = EXTRACT_FUNCTION_PATTERN.matcher(transformation);
345 if (!matcher.matches()) {
346 throw new IllegalArgumentException("given transformation function '" + transformation
347 + "' does not follow the expected pattern '<function>(<pattern>)'");
352 String type = matcher.group(1);
353 String pattern = matcher.group(2);
355 return new String[] { type, pattern };
359 * Transforms the command string into an array.
360 * Either invokes the shell and passes using the "c" option
361 * or (if command already starts with one of the shells) splits by space.
363 * @param shell (path), picks to first one to execute the command
364 * @param cOption "c"-option string
365 * @param commandLine to execute
366 * @return command array
368 protected String[] createCmdArray(String[] shell, String cOption, String commandLine) {
369 boolean startsWithShell = false;
370 for (String sh : shell) {
371 if (commandLine.startsWith(sh + " ")) {
372 startsWithShell = true;
377 if (!startsWithShell) {
378 return new String[] { shell[0], cOption, commandLine };
380 logger.debug("Splitting by spaces");
382 return commandLine.split(" ");
383 } catch (PatternSyntaxException e) {
384 logger.warn("An exception occurred while splitting '{}' : '{}'", commandLine, e.getMessage());
385 updateState(RUN, OnOffType.OFF);
386 updateState(OUTPUT, new StringType(e.getMessage()));
387 return new String[] {};
393 * Contains information about which operating system openHAB is running on.
394 * Found on https://stackoverflow.com/a/31547504/7508309, slightly modified
396 * @author Constantin Piber (for Memin) - Initial contribution
408 private static OS os = OS.NOT_SET;
410 public static OS getOperatingSystemType() {
411 if (os == OS.NOT_SET) {
412 String operSys = System.getProperty("os.name").toLowerCase();
413 if (operSys.contains("win")) {
415 } else if (operSys.contains("nix") || operSys.contains("nux") || operSys.contains("aix")) {
417 } else if (operSys.endsWith("bsd")) {
419 } else if (operSys.contains("mac")) {
421 } else if (operSys.contains("sunos")) {
430 public static String getOperatingSystemName() {
431 String osname = System.getProperty("os.name");
432 return osname != null ? osname : "unknown";