• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2007, Intel Corporation
3  *
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>
32 
33 #include "powertop.h"
34 
35 /* static arrays are not nice programming.. but they're easy */
36 static char configlines[5000][100];
37 static int configcount;
38 
read_kernel_config(void)39 static void read_kernel_config(void)
40 {
41 	FILE *file;
42 	char version[100], *c;
43 	char filename[100];
44 	if (configcount)
45 		return;
46 	if (access("/proc/config.gz", R_OK) == 0) {
47 		file = popen("zcat /proc/config.gz 2> /dev/null", "r");
48 		while (file && !feof(file)) {
49 			char line[100];
50 			if (fgets(line, 100, file) == NULL)
51 				break;
52 			strcpy(configlines[configcount++], line);
53 		}
54 		pclose(file);
55 		return;
56 	}
57 	file = fopen("/proc/sys/kernel/osrelease", "r");
58 	if (!file)
59 		return;
60 	if (fgets(version, 100, file) == NULL) {
61 		fclose(file);
62 		return;
63 	}
64 	fclose(file);
65 	c = strchr(version, '\n');
66 	if (c)
67 		*c = 0;
68 	sprintf(filename, "/boot/config-%s", version);
69 	file = fopen(filename, "r");
70 	if (!file) {
71 		sprintf(filename, "/lib/modules/%s/build/.config", version);
72 		file = fopen(filename, "r");
73 	}
74 	if (!file)
75 		return;
76 	while (!feof(file)) {
77 		char line[100];
78 		if (fgets(line, 100, file) == NULL)
79 			break;
80 		strcpy(configlines[configcount++], line);
81 	}
82 	fclose(file);
83 }
84 
85 /*
86  * Suggest the user to turn on/off a kernel config option.
87  * "comment" gets displayed if it's not already set to the right value
88  */
suggest_kernel_config(char * string,int onoff,char * comment,int weight)89 void suggest_kernel_config(char *string, int onoff, char *comment, int weight)
90 {
91 	int i;
92 	char searchon[100];
93 	char searchoff[100];
94 	int found = 0;
95 
96 	read_kernel_config();
97 
98 	sprintf(searchon, "%s=", string);
99 	sprintf(searchoff, "# %s is not set", string);
100 
101 	for (i = 0; i < configcount; i++) {
102 		if (onoff && strstr(configlines[i], searchon))
103 			return;
104 		if (onoff==0 && strstr(configlines[i], searchoff))
105 			return;
106 		if (onoff==0 && strstr(configlines[i], searchon))
107 			found = 1;
108 	}
109 	if (onoff || found)
110 		add_suggestion(comment, weight, 0, NULL, NULL);
111 	fflush(stdout);
112 }
113