Skip to content
20 changes: 20 additions & 0 deletions app/src/main/java/com/termux/api/cron/CronWorker.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
import com.termux.shared.shell.command.ExecutionCommand;
import com.termux.shared.termux.TermuxConstants;

import java.security.SecureRandom;
import java.util.Locale;
import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

Expand All @@ -29,6 +32,7 @@ public class CronWorker extends Worker {
private long maxRuntime;
private boolean continueOnConstraints;
private int gracePeriod;
private String appShellName;

public CronWorker(@NonNull Context appContext, @NonNull WorkerParameters workerParams) {
super(appContext, workerParams);
Expand Down Expand Up @@ -95,6 +99,8 @@ private void handleInputData() {
maxRuntime = inputData.getLong(WORKER_INPUT_MAX_RUNTIME, 3600);
continueOnConstraints = inputData.getBoolean(WORKER_INPUT_CONTINUE, false);
gracePeriod = inputData.getInt(WORKER_INPUT_DELAY, 5000);
appShellName = createAppShellName(jobId, executableUri);
Logger.logDebug(LOG_TAG, getId() + " - " + appShellName);
}

@Override
Expand Down Expand Up @@ -135,6 +141,7 @@ private void sendStartIntent() {
intent.setClassName(TermuxConstants.TERMUX_PACKAGE_NAME, TermuxConstants.TERMUX_APP.TERMUX_SERVICE_NAME);
intent.putExtra(TermuxConstants.TERMUX_APP.TERMUX_SERVICE.EXTRA_RUNNER, executionCommand.runner);
intent.putExtra(TermuxConstants.TERMUX_APP.TERMUX_SERVICE.EXTRA_BACKGROUND, true); // Also pass in case user using termux-app version < 0.119.0
intent.putExtra(TermuxConstants.TERMUX_APP.TERMUX_SERVICE.EXTRA_SHELL_NAME, appShellName);
intent.putExtra(TermuxConstants.TERMUX_APP.TERMUX_SERVICE.EXTRA_PENDING_INTENT, pi);

Context context = getApplicationContext();
Expand All @@ -151,6 +158,7 @@ private void sendKillIntent() {
// needs to be replaced with TermuxConstants.ACTION_SERVICE_STOP
Intent intent = new Intent("com.termux.service_execution_stop", executableUri);
intent.setClassName(TermuxConstants.TERMUX_PACKAGE_NAME, TermuxConstants.TERMUX_APP.TERMUX_SERVICE_NAME);
intent.putExtra(TermuxConstants.TERMUX_APP.TERMUX_SERVICE.EXTRA_SHELL_NAME, appShellName);
// needs to be replaced with TermuxConstants.EXTRA_TERMINATE_GRACE_PERIOD
intent.putExtra("com.termux.execute.stop.delay", gracePeriod);

Expand All @@ -166,4 +174,16 @@ private void sendKillIntent() {
private void scheduleNextExecution() {
CronScheduler.scheduleAlarmForJob(getApplicationContext(), jobId);
}

private static String createAppShellName(int jobId, Uri executableUri) {
char[] allowedCharsArray = ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789").toCharArray();
char[] randomId = new char[6];
Random random = new SecureRandom();
for (int i = 0; i < randomId.length; i++) {
randomId[i] = allowedCharsArray[random.nextInt(allowedCharsArray.length)];
}

return String.format(Locale.getDefault(),
"%s-%d-%s", executableUri.getLastPathSegment(), jobId, String.copyValueOf(randomId));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Use FileUtils.getFileBasename((UriUtils.getUriFilePathWithFragment(executableUri)) possibly with null check, read the java docs for why. However, I have locally added the com.termux.execute.executable extra for the path in TermuxService to get away from this uri mess.

Copy link
Copy Markdown
Author

@lvogt lvogt Feb 9, 2024

Choose a reason for hiding this comment

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

Did you actually encountered paths with # ? I know it's allowed, but it feels very wrong ;)

Anyway I applied your suggestion. Thanks.

Edit: To be honest: I should have written "I just tried it and its allowed (at least on ext4)" instead of "know" :D

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yeah, some user reported it somewhere for termux-open, it got fixed with termux/termux-app@3e518a6

Linux allows all characters/bytes other than null byte, # isn't that unlikely, it's an ascii character after all.

}
}