Skip to content

Commit

Permalink
service.ts: add /elevateService method
Browse files Browse the repository at this point in the history
  • Loading branch information
throwaway96 committed Feb 29, 2024
1 parent 9040f59 commit 3da2193
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion services/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,14 @@ function hashString(data: string, algorithm: string): string {
/**
* Elevates a package by name.
*/
async function elevateService(pkg: string): Promise<void> {
async function elevateService(pkg: string): Promise<boolean> {
if (runningAsRoot) {
console.info('Elevating service...');
await asyncExecFile(path.join(__dirname, 'elevate-service'), [pkg]);
return true;
} else {
console.error('Trying to elevate service without running as root. Skipping.');
return false;
}
}

Expand Down Expand Up @@ -745,6 +747,33 @@ 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)) {
message.respond(makeError('missing "id"'));
return;
} else if (typeof message.payload['id'] !== 'string') {
message.respond(makeError('"id" is not a string'));
return;
}

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

const payload = message.payload as ElevateServicePayload;

const status = await elevateService(payload.id);

return { returnValue: status };
}),
);
}

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

0 comments on commit 3da2193

Please sign in to comment.