Skip to content

Commit

Permalink
Merge pull request #171 from throwaway96/elevateService-method-20240303
Browse files Browse the repository at this point in the history
Add /elevateService method
  • Loading branch information
throwaway96 authored Mar 5, 2024
2 parents 582df90 + 7d58dca commit 6298cc3
Showing 1 changed file with 46 additions and 12 deletions.
58 changes: 46 additions & 12 deletions services/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,12 @@ const availableFlags = {
blockUpdates: 'webosbrew_block_updates',
} as Record<string, FlagName>;

function runningAsRoot(): boolean {
const runningAsRoot: boolean = (() => {
if (typeof process.getuid === 'undefined') {
throw new Error('process.getuid() is missing');
}
return process.getuid() === 0;
}
})();

function asyncCall<T extends Record<string, any>>(srv: Service, uri: string, args: Record<string, any>): Promise<T> {
return new Promise((resolve, reject) => {
Expand Down Expand Up @@ -119,13 +122,15 @@ function hashString(data: string, algorithm: string): string {
/**
* Elevates a package by name.
*/
async function elevateService(pkg: string): Promise<void> {
if (runningAsRoot()) {
console.info('Elevating service...');
await asyncExecFile(path.join(__dirname, 'elevate-service'), [pkg]);
} else {
async function elevateService(pkg: string): Promise<boolean> {
if (!runningAsRoot) {
console.error('Trying to elevate service without running as root. Skipping.');
return false;
}

console.info('Elevating service...');
await asyncExecFile(path.join(__dirname, 'elevate-service'), [pkg]);
return true;
}

/**
Expand Down Expand Up @@ -325,7 +330,7 @@ function runService() {
service.activityManager.idleTimeout = 30;

function getInstallerService(): Service {
if (runningAsRoot()) {
if (runningAsRoot) {
return service;
}
return serviceRemote as Service;
Expand Down Expand Up @@ -396,7 +401,7 @@ function runService() {
// If reelevation fails for some reason the service should still be
// reelevated on reboot on devices with persistent autostart hooks (since
// we launch elevate-service in startup.sh script)
if (runningAsRoot() && pkginfo && pkginfo.Package === kHomebrewChannelPackageId) {
if (runningAsRoot && pkginfo && pkginfo.Package === kHomebrewChannelPackageId) {
message.respond({ statusText: 'Self-update…' });
await createToast('Performing self-update...', service);

Expand Down Expand Up @@ -484,7 +489,7 @@ function runService() {
*/
service.register(
'checkRoot',
tryRespond(async () => ({ returnValue: runningAsRoot() })),
tryRespond(async () => ({ returnValue: runningAsRoot })),
);

/**
Expand All @@ -493,7 +498,7 @@ function runService() {
service.register(
'updateStartupScript',
tryRespond(async () => {
if (!runningAsRoot()) {
if (!runningAsRoot) {
return { returnValue: true, statusText: 'Not running as root.' };
}

Expand Down Expand Up @@ -666,7 +671,7 @@ function runService() {
service.register(
'autostart',
tryRespond(async (message) => {
if (!runningAsRoot()) {
if (!runningAsRoot) {
return { message: 'Not running as root.', returnValue: true };
}
if (await asyncExists('/tmp/webosbrew_startup')) {
Expand Down Expand Up @@ -703,6 +708,35 @@ function runService() {
return { returnValue: true };
}),
);

/**
* Elevates the service specified by "id".
*/
type ElevateServicePayload = { id: string };
service.register(
'elevateService',
tryRespond(async (message: Message) => {
if (!('id' in message.payload)) {
throw new Error('missing "id"');
} else if (typeof message.payload['id'] !== 'string') {

Check failure on line 721 in services/service.ts

View workflow job for this annotation

GitHub Actions / build / build

["id"] is better written in dot notation
throw new Error('"id" is not a string');
} else if (message.payload['id'] === '') {

Check failure on line 723 in services/service.ts

View workflow job for this annotation

GitHub Actions / build / build

["id"] is better written in dot notation
throw new Error('"id" is empty');
}

if (!runningAsRoot) {
throw new Error('not running as root');
}

const payload = message.payload as ElevateServicePayload;

const status = await elevateService(payload.id);

if (!status) {
throw new Error('elevateService() failed');
}
}),
);
}

if (process.argv[2] === 'self-update') {
Expand Down

0 comments on commit 6298cc3

Please sign in to comment.