Skip to content

Commit

Permalink
Disconnect system bus on graceful shutdown and test completion
Browse files Browse the repository at this point in the history
  • Loading branch information
benfrancis committed Nov 7, 2024
1 parent 2c5068b commit 9e7f01c
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 26 deletions.
3 changes: 3 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,9 @@ function startGateway(): void {
}

function gracefulExit(): void {
if (Platform.implemented('stop')) {
Platform.stop();
}
AddonManager.unloadAddons();
TunnelService.stop();
}
Expand Down
1 change: 1 addition & 0 deletions src/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ export const getNtpStatus = (): boolean => {

return wrapPlatform<boolean>(platform, 'getNtpStatus')();
};
export const stop = wrapPlatform<string>(platform, 'stop');

export const implemented = (fn: string): boolean => {
if (platform === null) {
Expand Down
7 changes: 7 additions & 0 deletions src/platforms/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,4 +292,11 @@ export default class BasePlatform {
restartNtpSync(): boolean {
throw new NotImplementedError('restartNtpSync');
}

/**
* Gracefully stop any platform services on shutdown.
*/
stop(): void {
throw new NotImplementedError('stop');
}
}
7 changes: 7 additions & 0 deletions src/platforms/linux-ubuntu-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ import NetworkManager, { ConnectionSettings } from './utilities/network-manager'
import { LanMode, NetworkAddresses, WirelessNetwork } from './types';

export class LinuxUbuntuCorePlatform extends BasePlatform {
/**
* Disconnect NetworkManager.
*/
stop(): void {
NetworkManager.stop();
}

/**
* Get the current addresses for Wi-Fi and LAN.
*
Expand Down
51 changes: 25 additions & 26 deletions src/platforms/utilities/network-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ class NetworkManager {
// Note there can only be one system bus instance open at a time.
private systemBus = DBus.getBus('system');

/**
* Disconnect the system bus.
*/
stop(): void {
this.systemBus.disconnect();
}

/**
* Get a list of network adapters from the system network manager.
*
Expand Down Expand Up @@ -147,28 +154,27 @@ class NetworkManager {
*/
getDeviceConnection(path: string): Promise<string> {
return new Promise((resolve, reject) => {
const systemBus = this.systemBus;
systemBus.getInterface(
this.systemBus.getInterface(
'org.freedesktop.NetworkManager',
path,
'org.freedesktop.NetworkManager.Device',
function (error, iface) {
(error, iface) => {
if (error) {
console.error(error);
reject();
return;
}
iface.getProperty('ActiveConnection', function (error, activeConnectionPath) {
iface.getProperty('ActiveConnection', (error, activeConnectionPath) => {
if (error) {
console.error(error);
reject();
return;
}
systemBus.getInterface(
this.systemBus.getInterface(
'org.freedesktop.NetworkManager',
activeConnectionPath,
'org.freedesktop.NetworkManager.Connection.Active',
function (error, iface) {
(error, iface) => {
if (error) {
console.error(error);
reject();
Expand Down Expand Up @@ -297,29 +303,28 @@ class NetworkManager {
* @returns {Promise<Array<AddressData>>} Promise resolves with IP4Config object.
*/
getDeviceIp4Config(path: string): Promise<Array<AddressData>> {
const systemBus = this.systemBus;
return new Promise((resolve, reject) => {
systemBus.getInterface(
this.systemBus.getInterface(
'org.freedesktop.NetworkManager',
path,
'org.freedesktop.NetworkManager.Device',
function (error, iface) {
(error, iface) => {
if (error) {
console.error(error);
reject();
return;
}
iface.getProperty('Ip4Config', function (error, ip4ConfigPath) {
iface.getProperty('Ip4Config', (error, ip4ConfigPath) => {
if (error) {
console.error(error);
reject();
return;
}
systemBus.getInterface(
this.systemBus.getInterface(
'org.freedesktop.NetworkManager',
ip4ConfigPath,
'org.freedesktop.NetworkManager.IP4Config',
function (error, iface) {
(error, iface) => {
if (error) {
console.error(error);
reject();
Expand Down Expand Up @@ -350,9 +355,8 @@ class NetworkManager {
* @returns {Promise<string>} The SSID of the access point.
*/
getAccessPointSsid(path: string): Promise<string> {
const systemBus = this.systemBus;
return new Promise((resolve, reject) => {
systemBus.getInterface(
this.systemBus.getInterface(
'org.freedesktop.NetworkManager',
path,
'org.freedesktop.NetworkManager.AccessPoint',
Expand Down Expand Up @@ -386,9 +390,8 @@ class NetworkManager {
* @returns {Promise<number>} The strength of the signal as a percentage.
*/
getAccessPointStrength(path: string): Promise<number> {
const systemBus = this.systemBus;
return new Promise((resolve, reject) => {
systemBus.getInterface(
this.systemBus.getInterface(
'org.freedesktop.NetworkManager',
path,
'org.freedesktop.NetworkManager.AccessPoint',
Expand Down Expand Up @@ -420,13 +423,12 @@ class NetworkManager {
* @returns {Promise<boolean>} true if encrypted, false if not.
*/
async getAccessPointSecurity(path: string): Promise<boolean> {
const systemBus = this.systemBus;
const wpaFlagRequest = new Promise((resolve, reject) => {
systemBus.getInterface(
this.systemBus.getInterface(
'org.freedesktop.NetworkManager',
path,
'org.freedesktop.NetworkManager.AccessPoint',
function (error, iface) {
(error, iface) => {
if (error) {
console.error(error);
reject();
Expand All @@ -446,7 +448,7 @@ class NetworkManager {
);
});
const wpa2FlagRequest = new Promise((resolve, reject) => {
systemBus.getInterface(
this.systemBus.getInterface(
'org.freedesktop.NetworkManager',
path,
'org.freedesktop.NetworkManager.AccessPoint',
Expand Down Expand Up @@ -536,9 +538,8 @@ class NetworkManager {
* @returns {Promise<string>} Promise resolves with the DBUS object path of an access point.
*/
getActiveAccessPoint(path: string): Promise<string> {
const systemBus = this.systemBus;
return new Promise((resolve, reject) => {
systemBus.getInterface(
this.systemBus.getInterface(
'org.freedesktop.NetworkManager',
path,
'org.freedesktop.NetworkManager.Device.Wireless',
Expand Down Expand Up @@ -632,9 +633,8 @@ class NetworkManager {
secure: boolean,
password: string
): Promise<void> {
const systemBus = this.systemBus;
return new Promise((resolve, reject) => {
systemBus.getInterface(
this.systemBus.getInterface(
'org.freedesktop.NetworkManager',
'/org/freedesktop/NetworkManager',
'org.freedesktop.NetworkManager',
Expand Down Expand Up @@ -695,9 +695,8 @@ class NetworkManager {
* deactivation or rejects with an Error on failure.
*/
disconnectNetworkDevice(path: string): Promise<void> {
const systemBus = this.systemBus;
return new Promise((resolve, reject) => {
systemBus.getInterface(
this.systemBus.getInterface(
'org.freedesktop.NetworkManager',
path,
'org.freedesktop.NetworkManager.Device',
Expand Down
4 changes: 4 additions & 0 deletions src/test/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import Events from '../models/events';
import Logs from '../models/logs';
import Things from '../models/things';
import UserProfile from '../user-profile';
import * as Platform from '../platform';
import e2p from 'event-to-promise';
import fs from 'fs';
import path from 'path';
Expand Down Expand Up @@ -91,6 +92,9 @@ afterEach(async () => {
});

afterAll(async () => {
if (Platform.implemented('stop')) {
Platform.stop();
}
Logs.close();
await addonManager.unloadAddons();

Expand Down

0 comments on commit 9e7f01c

Please sign in to comment.