From 23353fb77a9e836b37a2923937ec81b6babb5124 Mon Sep 17 00:00:00 2001 From: Dan Winship Date: Fri, 17 Dec 2010 15:30:12 -0500 Subject: [PATCH] Util.killall: add utility for killing unwanted processes Since we have to use pkill, kludgily, for the right combination of portability and featurefulness, put the code in one place rather than duplicating it everywhere. https://bugzilla.gnome.org/show_bug.cgi?id=635089 --- js/misc/util.js | 25 +++++++++++++++++++++++++ js/ui/notificationDaemon.js | 14 +++----------- js/ui/status/power.js | 3 +-- js/ui/statusIconDispatcher.js | 4 ++-- 4 files changed, 31 insertions(+), 15 deletions(-) diff --git a/js/misc/util.js b/js/misc/util.js index e61f0dd94..db5dc01c6 100644 --- a/js/misc/util.js +++ b/js/misc/util.js @@ -148,3 +148,28 @@ function _handleSpawnError(command, err) { notification.setTransient(true); source.notify(notification); } + +// killall: +// @processName: a process name +// +// Kills @processName. If no process with the given name is found, +// this will fail silently. +function killall(processName) { + try { + // pkill is more portable than killall, but on Linux at least + // it won't match if you pass more than 15 characters of the + // process name... However, if you use the '-f' flag to match + // the entire command line, it will work, but we have to be + // careful in that case that we can match + // '/usr/bin/processName' but not 'gedit processName.c' or + // whatever... + + let argv = ['pkill', '-f', '^([^ ]*/)?' + processName + '($| )']; + GLib.spawn_sync(null, argv, null, GLib.SpawnFlags.SEARCH_PATH, null, null); + // It might be useful to return success/failure, but we'd need + // a wrapper around WIFEXITED and WEXITSTATUS. Since none of + // the current callers care, we don't bother. + } catch (e) { + logError(e, 'Failed to kill ' + processName); + } +} diff --git a/js/ui/notificationDaemon.js b/js/ui/notificationDaemon.js index b87b3a41d..0b377a834 100644 --- a/js/ui/notificationDaemon.js +++ b/js/ui/notificationDaemon.js @@ -13,6 +13,7 @@ const Config = imports.misc.config; const Main = imports.ui.main; const MessageTray = imports.ui.messageTray; const Params = imports.misc.params; +const Util = imports.misc.util; let nextNotificationId = 1; @@ -127,17 +128,8 @@ NotificationDaemon.prototype = { log('Failed to acquire org.freedesktop.Notifications'); else { log('Failed to acquire org.freedesktop.Notifications; trying again'); - - // kill the notification-daemon. pkill is more portable - // than killall, but on Linux at least it won't match if - // you pass more than 15 characters of the process name... - // However, if you use the '-f' flag to match the entire - // command line, it will work, but we have to be careful - // in that case that we don't match 'gedit - // notification-daemon.c' or whatever... - let p = new Shell.Process({ args: ['pkill', '-f', - '^([^ ]*/)?(notification-daemon|notify-osd)$']}); - p.run(); + Util.killall('notification-daemon'); + Util.killall('notify-osd'); } }, diff --git a/js/ui/status/power.js b/js/ui/status/power.js index fe787f790..263d48e3d 100644 --- a/js/ui/status/power.js +++ b/js/ui/status/power.js @@ -1,7 +1,6 @@ /* -*- mode: js2; js2-basic-offset: 4; indent-tabs-mode: nil -*- */ const Gio = imports.gi.Gio; -const GLib = imports.gi.GLib; const DBus = imports.dbus; const Lang = imports.lang; const Mainloop = imports.mainloop; @@ -196,7 +195,7 @@ Indicator.prototype = { _checkError: function(error) { if (!this._restarted && error && error.message.match(/org\.freedesktop\.DBus\.Error\.(UnknownMethod|InvalidArgs)/)) { - GLib.spawn_command_line_sync('pkill -f "^gnome-power-manager$"'); + Util.killall('gnome-power-manager'); Util.spawn(['gnome-power-manager']); this._restarted = true; } diff --git a/js/ui/statusIconDispatcher.js b/js/ui/statusIconDispatcher.js index 474cf54d3..874ffb40d 100644 --- a/js/ui/statusIconDispatcher.js +++ b/js/ui/statusIconDispatcher.js @@ -6,6 +6,7 @@ const Signals = imports.signals; const MessageTray = imports.ui.messageTray; const NotificationDaemon = imports.ui.notificationDaemon; +const Util = imports.misc.util; const STANDARD_TRAY_ICON_IMPLEMENTATIONS = { 'bluetooth-applet': 'bluetooth', @@ -33,8 +34,7 @@ StatusIconDispatcher.prototype = { // app-indicators, so that applications fall back to normal // status icons // http://bugzilla.gnome.org/show_bug.cgi=id=621382 - let p = new Shell.Process({ args: ['pkill', '-f', '^([^ ]*/)?indicator-application-service$']}); - p.run(); + Util.killall('indicator-application-service'); }, _onTrayIconAdded: function(o, icon) {