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

Add CronAPI #657

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
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
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
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
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.

}
}