You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
2.2 KiB

/**
* CheckCommand module
*
* @author Deminder <tremminder@gmail.com>
* @copyright 2021
* @license GNU General Public License v3.0
*/
/* exported doCheck, maybeCancel, isChecking */
const ExtensionUtils = imports.misc.extensionUtils;
const Me = ExtensionUtils.getCurrentExtension();
const { RootMode, Convenience } = Me.imports.lib;
const Gio = imports.gi.Gio;
const logDebug = Convenience.logDebug;
let checkCancel;
/**
* Wait for checkCmd to execute successfully.
*
* @param {string} checkCmd check command
* @param {(line: string) => void} onLog
* @param {async () => void} redoRootProtection
*/
function doCheck(checkCmd, onLog, redoRootProtection) {
if (checkCancel !== undefined) {
return Promise.reject(
new Error(
'Confirmation canceled: attempted to start a second check command!'
)
);
}
checkCancel = new Gio.Cancellable();
const checkWatchCancel = new Gio.Cancellable();
return Promise.all([
_doCheck(checkCmd, checkWatchCancel, onLog),
continueRootProtectionDuringCheck(checkWatchCancel, redoRootProtection),
]);
}
/**
*
* @param checkCmd
* @param checkWatchCancel
* @param onLog
*/
async function _doCheck(checkCmd, checkWatchCancel, onLog) {
try {
await RootMode.execCheck(checkCmd, checkCancel, true, onLog);
logDebug(`Check command "${checkCmd}" confirmed shutdown.`);
} finally {
checkCancel = undefined;
checkWatchCancel.cancel();
}
}
/**
*
* @param cancellable
* @param redoRootProtection
*/
async function continueRootProtectionDuringCheck(
cancellable,
redoRootProtection
) {
try {
await RootMode.execCheck(['sleep', '30'], cancellable, false);
} catch (err) {
logDebug('RootProtection during check: Canceled');
}
if (checkCancel === undefined) {
logDebug('RootProtection during check: Done');
} else {
await redoRootProtection();
logDebug('RootProtection during check: Continue');
await continueRootProtectionDuringCheck(cancellable, redoRootProtection);
}
}
/**
*
*/
function isChecking() {
return checkCancel !== undefined && !checkCancel.is_cancelled();
}
/**
*
*/
function maybeCancel() {
const doCancel = isChecking();
if (doCancel) {
checkCancel.cancel();
}
return doCancel;
}