blob: d5818eaf3f4a7240fb4dedc3bbd3e83c7c1c2cc7 [file] [log] [blame]
Arjan van der Ven28615352007-05-11 20:08:05 +00001/*
Arjan van der Venee42d812007-05-18 18:33:19 +00002 * Copyright 2007, Intel Corporation
Arjan van der Ven12e55ca2007-05-12 22:52:54 +00003 *
Arjan van der Ven28615352007-05-11 20:08:05 +00004 * 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.
Arjan van der Ven0eb44da2007-05-13 03:39:32 +00009 *
Arjan van der Ven28615352007-05-11 20:08:05 +000010 * 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.
Arjan van der Ven0eb44da2007-05-13 03:39:32 +000014 *
Arjan van der Ven28615352007-05-11 20:08:05 +000015 * You should have received a copy of the GNU General Public License
Arjan van der Ven0eb44da2007-05-13 03:39:32 +000016 * 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,
Arjan van der Ven28615352007-05-11 20:08:05 +000019 * 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
Arjan van der Vencc857522007-05-13 17:28:15 +000033#include "powertop.h"
34
Arjan van der Ven28615352007-05-11 20:08:05 +000035/* static arrays are not nice programming.. but they're easy */
36static char configlines[5000][100];
37static int configcount;
38
Arjan van der Ven28615352007-05-11 20:08:05 +000039static void read_kernel_config(void)
40{
Arjan van der Ven12e55ca2007-05-12 22:52:54 +000041 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) {
Arjan van der Ven95521af2007-05-27 17:49:39 +000047 file = popen("zcat /proc/config.gz 2> /dev/null", "r");
Arjan van der Ven12e55ca2007-05-12 22:52:54 +000048 while (file && !feof(file)) {
49 char line[100];
Arjan van der Venef08f1c2007-05-16 18:28:57 +000050 if (fgets(line, 100, file) == NULL)
51 break;
Arjan van der Ven12e55ca2007-05-12 22:52:54 +000052 strcpy(configlines[configcount++], line);
53 }
54 pclose(file);
55 return;
56 }
57 file = fopen("/proc/sys/kernel/osrelease", "r");
58 if (!file)
59 return;
Arjan van der Venef08f1c2007-05-16 18:28:57 +000060 if (fgets(version, 100, file) == NULL) {
61 fclose(file);
62 return;
63 }
Arjan van der Ven12e55ca2007-05-12 22:52:54 +000064 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");
Arjan van der Venedad70e2007-05-11 20:36:43 +000070 if (!file) {
71 sprintf(filename, "/lib/modules/%s/build/.config", version);
72 file = fopen(filename, "r");
73 }
Arjan van der Ven12e55ca2007-05-12 22:52:54 +000074 if (!file)
75 return;
76 while (!feof(file)) {
77 char line[100];
Arjan van der Venef08f1c2007-05-16 18:28:57 +000078 if (fgets(line, 100, file) == NULL)
79 break;
Arjan van der Ven12e55ca2007-05-12 22:52:54 +000080 strcpy(configlines[configcount++], line);
81 }
82 fclose(file);
Arjan van der Ven28615352007-05-11 20:08:05 +000083}
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 */
Arjan van der Ven825ec692007-05-24 17:41:06 +000089void suggest_kernel_config(char *string, int onoff, char *comment, int weight)
Arjan van der Ven28615352007-05-11 20:08:05 +000090{
Arjan van der Ven12e55ca2007-05-12 22:52:54 +000091 int i;
Arjan van der Ven5582c6a2007-05-12 22:58:07 +000092 char searchon[100];
93 char searchoff[100];
94 int found = 0;
Arjan van der Ven12e55ca2007-05-12 22:52:54 +000095
Arjan van der Ven12e55ca2007-05-12 22:52:54 +000096 read_kernel_config();
Arjan van der Ven5582c6a2007-05-12 22:58:07 +000097
98 sprintf(searchon, "%s=", string);
99 sprintf(searchoff, "# %s is not set", string);
Arjan van der Ven12e55ca2007-05-12 22:52:54 +0000100
101 for (i = 0; i < configcount; i++) {
Arjan van der Ven5582c6a2007-05-12 22:58:07 +0000102 if (onoff && strstr(configlines[i], searchon))
Arjan van der Ven12e55ca2007-05-12 22:52:54 +0000103 return;
Arjan van der Ven5582c6a2007-05-12 22:58:07 +0000104 if (onoff==0 && strstr(configlines[i], searchoff))
105 return;
106 if (onoff==0 && strstr(configlines[i], searchon))
107 found = 1;
Arjan van der Ven12e55ca2007-05-12 22:52:54 +0000108 }
Arjan van der Ven5582c6a2007-05-12 22:58:07 +0000109 if (onoff || found)
Arjan van der Venb43056b2007-05-24 20:47:42 +0000110 add_suggestion(comment, weight, 0, NULL, NULL);
Arjan van der Ven12e55ca2007-05-12 22:52:54 +0000111 fflush(stdout);
Arjan van der Ven28615352007-05-11 20:08:05 +0000112}