17a192d395ce5d7e5945afa684146d5b0abc53480mbligh/*
27a192d395ce5d7e5945afa684146d5b0abc53480mbligh * Copyright 2007, Intel Corporation
37a192d395ce5d7e5945afa684146d5b0abc53480mbligh *
47a192d395ce5d7e5945afa684146d5b0abc53480mbligh * This file is part of PowerTOP
57a192d395ce5d7e5945afa684146d5b0abc53480mbligh *
67a192d395ce5d7e5945afa684146d5b0abc53480mbligh * This program file is free software; you can redistribute it and/or modify it
77a192d395ce5d7e5945afa684146d5b0abc53480mbligh * under the terms of the GNU General Public License as published by the
87a192d395ce5d7e5945afa684146d5b0abc53480mbligh * Free Software Foundation; version 2 of the License.
97a192d395ce5d7e5945afa684146d5b0abc53480mbligh *
107a192d395ce5d7e5945afa684146d5b0abc53480mbligh * This program is distributed in the hope that it will be useful, but WITHOUT
117a192d395ce5d7e5945afa684146d5b0abc53480mbligh * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
127a192d395ce5d7e5945afa684146d5b0abc53480mbligh * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
137a192d395ce5d7e5945afa684146d5b0abc53480mbligh * for more details.
147a192d395ce5d7e5945afa684146d5b0abc53480mbligh *
157a192d395ce5d7e5945afa684146d5b0abc53480mbligh * You should have received a copy of the GNU General Public License
167a192d395ce5d7e5945afa684146d5b0abc53480mbligh * along with this program in a file named COPYING; if not, write to the
177a192d395ce5d7e5945afa684146d5b0abc53480mbligh * Free Software Foundation, Inc.,
187a192d395ce5d7e5945afa684146d5b0abc53480mbligh * 51 Franklin Street, Fifth Floor,
197a192d395ce5d7e5945afa684146d5b0abc53480mbligh * Boston, MA 02110-1301 USA
207a192d395ce5d7e5945afa684146d5b0abc53480mbligh *
217a192d395ce5d7e5945afa684146d5b0abc53480mbligh * Authors:
227a192d395ce5d7e5945afa684146d5b0abc53480mbligh * 	Arjan van de Ven <arjan@linux.intel.com>
237a192d395ce5d7e5945afa684146d5b0abc53480mbligh */
247a192d395ce5d7e5945afa684146d5b0abc53480mbligh
257a192d395ce5d7e5945afa684146d5b0abc53480mbligh#include <unistd.h>
267a192d395ce5d7e5945afa684146d5b0abc53480mbligh#include <stdio.h>
277a192d395ce5d7e5945afa684146d5b0abc53480mbligh#include <stdlib.h>
287a192d395ce5d7e5945afa684146d5b0abc53480mbligh#include <string.h>
297a192d395ce5d7e5945afa684146d5b0abc53480mbligh#include <stdint.h>
307a192d395ce5d7e5945afa684146d5b0abc53480mbligh#include <sys/types.h>
317a192d395ce5d7e5945afa684146d5b0abc53480mbligh#include <dirent.h>
327a192d395ce5d7e5945afa684146d5b0abc53480mbligh
337a192d395ce5d7e5945afa684146d5b0abc53480mbligh#include "powertop.h"
347a192d395ce5d7e5945afa684146d5b0abc53480mbligh
357a192d395ce5d7e5945afa684146d5b0abc53480mbligh/* static arrays are not nice programming.. but they're easy */
367a192d395ce5d7e5945afa684146d5b0abc53480mblighstatic char configlines[5000][100];
377a192d395ce5d7e5945afa684146d5b0abc53480mblighstatic int configcount;
387a192d395ce5d7e5945afa684146d5b0abc53480mbligh
397a192d395ce5d7e5945afa684146d5b0abc53480mblighstatic void read_kernel_config(void)
407a192d395ce5d7e5945afa684146d5b0abc53480mbligh{
417a192d395ce5d7e5945afa684146d5b0abc53480mbligh	FILE *file;
427a192d395ce5d7e5945afa684146d5b0abc53480mbligh	char version[100], *c;
437a192d395ce5d7e5945afa684146d5b0abc53480mbligh	char filename[100];
447a192d395ce5d7e5945afa684146d5b0abc53480mbligh	if (configcount)
457a192d395ce5d7e5945afa684146d5b0abc53480mbligh		return;
467a192d395ce5d7e5945afa684146d5b0abc53480mbligh	if (access("/proc/config.gz", R_OK) == 0) {
477a192d395ce5d7e5945afa684146d5b0abc53480mbligh		file = popen("zcat /proc/config.gz 2> /dev/null", "r");
487a192d395ce5d7e5945afa684146d5b0abc53480mbligh		while (file && !feof(file)) {
497a192d395ce5d7e5945afa684146d5b0abc53480mbligh			char line[100];
507a192d395ce5d7e5945afa684146d5b0abc53480mbligh			if (fgets(line, 100, file) == NULL)
517a192d395ce5d7e5945afa684146d5b0abc53480mbligh				break;
527a192d395ce5d7e5945afa684146d5b0abc53480mbligh			strcpy(configlines[configcount++], line);
537a192d395ce5d7e5945afa684146d5b0abc53480mbligh		}
547a192d395ce5d7e5945afa684146d5b0abc53480mbligh		pclose(file);
557a192d395ce5d7e5945afa684146d5b0abc53480mbligh		return;
567a192d395ce5d7e5945afa684146d5b0abc53480mbligh	}
577a192d395ce5d7e5945afa684146d5b0abc53480mbligh	file = fopen("/proc/sys/kernel/osrelease", "r");
587a192d395ce5d7e5945afa684146d5b0abc53480mbligh	if (!file)
597a192d395ce5d7e5945afa684146d5b0abc53480mbligh		return;
607a192d395ce5d7e5945afa684146d5b0abc53480mbligh	if (fgets(version, 100, file) == NULL) {
617a192d395ce5d7e5945afa684146d5b0abc53480mbligh		fclose(file);
627a192d395ce5d7e5945afa684146d5b0abc53480mbligh		return;
637a192d395ce5d7e5945afa684146d5b0abc53480mbligh	}
647a192d395ce5d7e5945afa684146d5b0abc53480mbligh	fclose(file);
657a192d395ce5d7e5945afa684146d5b0abc53480mbligh	c = strchr(version, '\n');
667a192d395ce5d7e5945afa684146d5b0abc53480mbligh	if (c)
677a192d395ce5d7e5945afa684146d5b0abc53480mbligh		*c = 0;
687a192d395ce5d7e5945afa684146d5b0abc53480mbligh	sprintf(filename, "/boot/config-%s", version);
697a192d395ce5d7e5945afa684146d5b0abc53480mbligh	file = fopen(filename, "r");
707a192d395ce5d7e5945afa684146d5b0abc53480mbligh	if (!file) {
717a192d395ce5d7e5945afa684146d5b0abc53480mbligh		sprintf(filename, "/lib/modules/%s/build/.config", version);
727a192d395ce5d7e5945afa684146d5b0abc53480mbligh		file = fopen(filename, "r");
737a192d395ce5d7e5945afa684146d5b0abc53480mbligh	}
747a192d395ce5d7e5945afa684146d5b0abc53480mbligh	if (!file)
757a192d395ce5d7e5945afa684146d5b0abc53480mbligh		return;
767a192d395ce5d7e5945afa684146d5b0abc53480mbligh	while (!feof(file)) {
777a192d395ce5d7e5945afa684146d5b0abc53480mbligh		char line[100];
787a192d395ce5d7e5945afa684146d5b0abc53480mbligh		if (fgets(line, 100, file) == NULL)
797a192d395ce5d7e5945afa684146d5b0abc53480mbligh			break;
807a192d395ce5d7e5945afa684146d5b0abc53480mbligh		strcpy(configlines[configcount++], line);
817a192d395ce5d7e5945afa684146d5b0abc53480mbligh	}
827a192d395ce5d7e5945afa684146d5b0abc53480mbligh	fclose(file);
837a192d395ce5d7e5945afa684146d5b0abc53480mbligh}
847a192d395ce5d7e5945afa684146d5b0abc53480mbligh
857a192d395ce5d7e5945afa684146d5b0abc53480mbligh/*
867a192d395ce5d7e5945afa684146d5b0abc53480mbligh * Suggest the user to turn on/off a kernel config option.
877a192d395ce5d7e5945afa684146d5b0abc53480mbligh * "comment" gets displayed if it's not already set to the right value
887a192d395ce5d7e5945afa684146d5b0abc53480mbligh */
897a192d395ce5d7e5945afa684146d5b0abc53480mblighvoid suggest_kernel_config(char *string, int onoff, char *comment, int weight)
907a192d395ce5d7e5945afa684146d5b0abc53480mbligh{
917a192d395ce5d7e5945afa684146d5b0abc53480mbligh	int i;
927a192d395ce5d7e5945afa684146d5b0abc53480mbligh	char searchon[100];
937a192d395ce5d7e5945afa684146d5b0abc53480mbligh	char searchoff[100];
947a192d395ce5d7e5945afa684146d5b0abc53480mbligh	int found = 0;
957a192d395ce5d7e5945afa684146d5b0abc53480mbligh
967a192d395ce5d7e5945afa684146d5b0abc53480mbligh	read_kernel_config();
977a192d395ce5d7e5945afa684146d5b0abc53480mbligh
987a192d395ce5d7e5945afa684146d5b0abc53480mbligh	sprintf(searchon, "%s=", string);
997a192d395ce5d7e5945afa684146d5b0abc53480mbligh	sprintf(searchoff, "# %s is not set", string);
1007a192d395ce5d7e5945afa684146d5b0abc53480mbligh
1017a192d395ce5d7e5945afa684146d5b0abc53480mbligh	for (i = 0; i < configcount; i++) {
1027a192d395ce5d7e5945afa684146d5b0abc53480mbligh		if (onoff && strstr(configlines[i], searchon))
1037a192d395ce5d7e5945afa684146d5b0abc53480mbligh			return;
1047a192d395ce5d7e5945afa684146d5b0abc53480mbligh		if (onoff==0 && strstr(configlines[i], searchoff))
1057a192d395ce5d7e5945afa684146d5b0abc53480mbligh			return;
1067a192d395ce5d7e5945afa684146d5b0abc53480mbligh		if (onoff==0 && strstr(configlines[i], searchon))
1077a192d395ce5d7e5945afa684146d5b0abc53480mbligh			found = 1;
1087a192d395ce5d7e5945afa684146d5b0abc53480mbligh	}
1097a192d395ce5d7e5945afa684146d5b0abc53480mbligh	if (onoff || found)
1107a192d395ce5d7e5945afa684146d5b0abc53480mbligh		add_suggestion(comment, weight, 0, NULL, NULL);
1117a192d395ce5d7e5945afa684146d5b0abc53480mbligh	fflush(stdout);
1127a192d395ce5d7e5945afa684146d5b0abc53480mbligh}
113