blob: 4ae617b489e0e8c698aadc40530f800d544f12eb [file] [log] [blame]
Arjan van der Vencc857522007-05-13 17:28:15 +00001/*
Arjan van der Venee42d812007-05-18 18:33:19 +00002 * Copyright 2007, Intel Corporation
Arjan van der Vencc857522007-05-13 17:28:15 +00003 *
4 * This file is part of PowerTOP
5 *
6 * This program file is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program in a file named COPYING; if not, write to the
17 * Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301 USA
20 *
21 * Authors:
22 * Arjan van de Ven <arjan@linux.intel.com>
23 */
24
25#include <unistd.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29#include <stdint.h>
30#include <sys/types.h>
31#include <dirent.h>
Arjan van der Ven2484be22007-06-03 21:31:22 +000032#include <sys/types.h>
33#include <signal.h>
Arjan van der Vencc857522007-05-13 17:28:15 +000034
35#include "powertop.h"
36
Arjan van der Vend1d13382007-05-26 21:24:16 +000037char process_to_kill[1024];
38
Arjan van der Ven2484be22007-06-03 21:31:22 +000039static void fancy_kill(void)
40{
41 FILE *file;
42 char line[2048];
43 char *tokill;
44 int pid = 0;
45 tokill = &process_to_kill[1];
46
47 file = popen(" ps -A -o pid,command", "r");
48 if (!file)
49 return;
50 while (!feof(file)) {
51 memset(line, 0, 2048);
52 if (fgets(line, 2047, file)==NULL)
53 break;
54 if (!strstr(line, tokill))
55 continue;
56 pid = strtoul(line, NULL, 10);
57
58 }
59 pclose(file);
60 if (pid<2)
61 return;
62 kill(pid, SIGTERM);
63}
64
Arjan van der Vend1d13382007-05-26 21:24:16 +000065void do_kill(void)
66{
67 char line[2048];
Arjan van der Ven2484be22007-06-03 21:31:22 +000068
69 if (process_to_kill[0] == '-') {
70 fancy_kill();
71 } else {
72 sprintf(line, "killall %s &> /dev/null", process_to_kill);
73 system(line);
74 }
Arjan van der Vend1d13382007-05-26 21:24:16 +000075}
76
Arjan van der Venf9533992007-06-03 19:18:31 +000077void suggest_process_death(char *process_match, char *tokill, struct line *slines, int linecount, double minwakeups, char *comment, int weight)
Arjan van der Vencc857522007-05-13 17:28:15 +000078{
79 int i;
Arjan van der Vend1d13382007-05-26 21:24:16 +000080
Arjan van der Vencc857522007-05-13 17:28:15 +000081 for (i = 0; i < linecount; i++) {
Arjan van der Ven356cd2c2007-12-13 21:52:01 +000082 if (slines[i].string && strstr(slines[i].string, process_match)) {
Arjan van der Vend1d13382007-05-26 21:24:16 +000083 char hotkey_string[300];
Arjan van der Ven9b8e6972007-06-11 19:56:49 +000084 sprintf(hotkey_string, _(" K - kill %s "), tokill);
Arjan van der Vend1d13382007-05-26 21:24:16 +000085 strcpy(process_to_kill, tokill);
Arjan van der Venf9533992007-06-03 19:18:31 +000086 if (minwakeups < slines[i].count)
Arjan van der Vend1d13382007-05-26 21:24:16 +000087 add_suggestion(comment, weight, 'K' , hotkey_string, do_kill);
Arjan van der Vencc857522007-05-13 17:28:15 +000088 break;
89 }
90 }
91 fflush(stdout);
Arjan van der Vencc857522007-05-13 17:28:15 +000092}