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 /elevateService method #171

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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 @@
/**
* 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 @@
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 @@
// 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 @@
*/
service.register(
'checkRoot',
tryRespond(async () => ({ returnValue: runningAsRoot() })),
tryRespond(async () => ({ returnValue: runningAsRoot })),
);

/**
Expand All @@ -493,7 +498,7 @@
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 @@
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 @@
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
Loading