Skip to content

Commit

Permalink
Replace notification logic with systembus-notify
Browse files Browse the repository at this point in the history
Gets rid of sudo hacks and system() calls.

#183
  • Loading branch information
rfjakob committed Apr 4, 2020
1 parent 4b6ad53 commit 17b3d5e
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 21 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,10 @@ accept

Changelog
---------
* vNEXT
* Replace notification logic with [systembus-notify](https://github.com/rfjakob/systembus-notify)
([#183](https://github.com/rfjakob/earlyoom/issues/183))

* v1.5, 2020-03-22
* `-p`: set oom_score_adj to `-100` instead of `-1000`
([#170](https://github.com/rfjakob/earlyoom/issues/170))
Expand Down
42 changes: 27 additions & 15 deletions kill.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,25 @@ static int isnumeric(char* str)
}
}

static void maybe_notify(char* notif_command, char* notif_args)
static void notify(const char* summary, const char* body)
{
if (!notif_command)
int pid = fork();
if(pid > 0) {
// parent
return;

char notif[PATH_MAX + 2000];
snprintf(notif, sizeof(notif), "%s %s", notif_command, notif_args);
if (system(notif) != 0)
warn("system('%s') failed: %s\n", notif, strerror(errno));
}
char summary2[1024] = {0};
snprintf(summary2, sizeof(summary2), "string:%s", summary);
char body2[1024] = "string:";
if(body != NULL) {
snprintf(body2, sizeof(body2), "string:%s", body);
}
// Complete command line looks like this:
// dbus-send --system / net.nuetzlich.SystemNotifications.Notify 'string:summary text' 'string:and body text'
execl("/usr/bin/dbus-send", "dbus-send", "--system", "/", "net.nuetzlich.SystemNotifications.Notify",
summary2, body2, NULL);
warn("notify: exec failed: %s\n", strerror(errno));
exit(1);
}

/*
Expand Down Expand Up @@ -234,8 +244,9 @@ void kill_largest_process(const poll_loop_args_t *args, int sig)

if (victim.pid <= 0) {
warn("Could not find a process to kill. Sleeping 1 second.\n");
maybe_notify(args->notif_command,
"-i dialog-error 'earlyoom' 'Error: Could not find a process to kill. Sleeping 1 second.'");
if(args->notify) {
notify("earlyoom", "Error: Could not find a process to kill. Sleeping 1 second.");
}
sleep(1);
return;
}
Expand Down Expand Up @@ -267,11 +278,11 @@ void kill_largest_process(const poll_loop_args_t *args, int sig)
// that there is enough memory to spawn the notification helper.
if (sig != 0) {
char notif_args[PATH_MAX + 1000];
// maybe_notify() calls system(). We must sanitize the strings we pass.
sanitize(victim.name);
snprintf(notif_args, sizeof(notif_args),
"-i dialog-warning 'earlyoom' 'Low memory! Killing process %d %s'", victim.pid, victim.name);
maybe_notify(args->notif_command, notif_args);
"Low memory! Killing process %d %s", victim.pid, victim.name);
if(args->notify) {
notify("earlyoom", notif_args);
}
}

if (sig == 0) {
Expand All @@ -280,8 +291,9 @@ void kill_largest_process(const poll_loop_args_t *args, int sig)

if (res != 0) {
warn("kill failed: %s\n", strerror(saved_errno));
maybe_notify(args->notif_command,
"-i dialog-error 'earlyoom' 'Error: Failed to kill process'");
if(args->notify) {
notify("earlyoom", "Error: Failed to kill process");
}
// Killing the process may have failed because we are not running as root.
// In that case, trying again in 100ms will just yield the same error.
// Throttle ourselves to not spam the log.
Expand Down
4 changes: 2 additions & 2 deletions kill.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ typedef struct {
double swap_kill_percent;
/* ignore /proc/PID/oom_score_adj? */
bool ignore_oom_score_adj;
/* notifcation command to launch when killing something. NULL = no-op. */
char* notif_command;
/* send d-bus notifications? */
bool notify;
/* prefer/avoid killing these processes. NULL = no-op. */
regex_t* prefer_regex;
regex_t* avoid_regex;
Expand Down
8 changes: 4 additions & 4 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,12 @@ int main(int argc, char* argv[])
fprintf(stderr, "Ignoring positive oom_score_adj values (-i)\n");
break;
case 'n':
args.notif_command = "notify-send";
fprintf(stderr, "Notifying using '%s'\n", args.notif_command);
args.notify = true;
fprintf(stderr, "Notifying through D-Bus\n");
break;
case 'N':
args.notif_command = optarg;
fprintf(stderr, "Notifying using '%s'\n", args.notif_command);
args.notify = true;
fprintf(stderr, "Notifying through D-Bus, argument '%s' ignored for compatability\n", optarg);
break;
case 'd':
enable_debug = 1;
Expand Down

0 comments on commit 17b3d5e

Please sign in to comment.