~ubuntu-branches/ubuntu/trusty/powertop-1.13/trusty

« back to all changes in this revision

Viewing changes to config.c

  • Committer: Package Import Robot
  • Author(s): Jamie Strandboge
  • Date: 2011-11-11 13:36:57 UTC
  • Revision ID: package-import@ubuntu.com-20111111133657-dumpedggvnmcjb2e
Tags: upstream-1.13
ImportĀ upstreamĀ versionĀ 1.13

Show diffs side-by-side

added added

removed removed

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