Package: longrun
Version: 0.9-17
Severity: wishlist
Tags: patch

*** Please type your report below this line ***
Added support for external configuration file defined through -C.
Currently, the supported options in the configuration file are "cpuid=",
"msr=", and "verbose". The default configuration file is set at
/etc/longrun.conf if it exists in the system.

The new changes are based on suggestions of Joey Hess <[EMAIL PROTECTED]> 
regarding the (mis)use of getline. This new patch now makes use of
fgets() for reading from a stream, and removes the need for glibc.

This allows the defaults for longrun to be defined in the configuration
file instead of in the source code in compile time.

This added feature can easily be extended as necessary in the future.

The default location for the configuration file is '/etc/longrun.conf'.

This is an isolated patch for longrun.c which applies to 0.9-17, which
doesn't change any other files in the debian package. This patch adds the
above functionality without touching other external files.

Should this be forwarded to the upstream author?

-- System Information:
Debian Release: 3.1
Architecture: i386 (i686)
Kernel: Linux 2.6.10-5-386
Locale: LANG=en_PH.UTF-8, LC_CTYPE=en_PH.UTF-8 (charmap=UTF-8)

Versions of packages longrun depends on:
ii  libc6               2.3.2.ds1-20ubuntu14 GNU C Library: Shared
libraries an

-- no debconf information

-- 
Dean Michael C. Berris <[EMAIL PROTECTED]>
GPG Key: 0x08AE6EAC
http://mikhailberis.blogspot.com
Mobile: +63 928 7291459
--- longrun.c	2005-10-08 03:31:43.000000000 +0800
+++ ../../longrun-0.9/longrun.c	2005-10-05 21:21:55.000000000 +0800
@@ -33,7 +33,11 @@
 #include <sys/io.h>
 #include <sys/sysmacros.h>
 #include <locale.h>
+
+#ifndef __USE_UNIX98
 #define __USE_UNIX98	/* for pread/pwrite */
+#endif
+
 #define __USE_FILE_OFFSET64 /* we should use 64 bit offset for pread/pwrite */
 #include <unistd.h>
 
@@ -88,12 +92,19 @@
 
 int opt_verbose = 0;		/* verbosity */
 
+/* Configuration File Support */
+#define MAXLINE 255
+#define CONFIG_FILENAME "/etc/longrun.conf"
+char * conf_cpuid = NULL;
+char * conf_msr = NULL;
+
 void usage(int status) {
 	FILE *stream = status ? stderr : stdout;
 
 	fprintf(stream,
 		_("%s %s (%s)\n"
-		"usage: %s [-c device] [-m device] [-hlpv] [-f flag] [-s low high] [-t tlx]\n"
+		"usage: %s [-C filename] [-c device] [-m device] [-hlpv] [-f flag] [-s low high] [-t tlx]\n"
+		" -C filename	set the config file to read\n"
 		" -c device     set CPUID device\n"
 		" -m device     set MSR device\n"
 		" -h            print this help\n"
@@ -514,6 +525,63 @@
 	printf(_("LongRun flags: %s\n"), (lower & 1) ? _("performance") : _("economy"));
 }
 
+void parse_line(char * line, size_t len) {
+	char * key = NULL;
+	char * value = NULL;
+	if (len && line) {
+		/* ignore lines that start with # */
+		if (line[0] == '#')
+			return;
+		
+		/* special config file option "verbose" */
+		if (!strncmp("verbose", line, 6)) {
+			opt_verbose++;
+			printf(_("Verbose mode set in configuration file.\n"));
+			return;
+		}
+		
+		if ((key = strtok(line, "=")) == NULL) {
+			if (opt_verbose)
+				printf(_("encountered invalid line '%s'\n"), line);
+			
+			return;
+		}
+		
+		value = strtok(NULL, "\n");
+		
+		if (!strncmp("cpuid", key, 5)) {
+			conf_cpuid = malloc(sizeof(char) * (strlen(value) + 1));
+			strcpy(conf_cpuid, value);
+			cpuid_device = conf_cpuid;
+			if (opt_verbose)
+				printf(_("cpuid device set to: %s\n"), conf_cpuid);
+			return;
+		}
+		
+		if (!strncmp("msr", key, 3)) {
+			conf_msr = malloc(sizeof(char) * (strlen(value) + 1));
+			strcpy(conf_msr, value);
+			msr_device = conf_msr;
+			if (opt_verbose)
+				printf(_("msr device set to: %s\n"), conf_msr);
+			return;
+		}
+		
+	}
+}
+
+void conf_cleanup() {
+	if (!conf_cpuid) {
+		free(conf_cpuid);
+		conf_cpuid = NULL;
+	}
+	
+	if (!conf_msr) {
+		free(conf_msr);
+		conf_cpuid = NULL;
+	}
+}
+
 int main(int argc, char *argv[])
 {
 	int low, high;
@@ -524,6 +592,10 @@
 	int opt_print = 0;
 	int opt_set = 0;
 	int opt_atm = 0;
+	/* for configuration file reading */
+	int opt_conf= 0;
+	char * c_filename = NULL;
+	FILE * c_file = NULL;
 
 	if (argc)
 		progname = my_basename(argv[0]);
@@ -538,8 +610,12 @@
 	cpuid_device = CPUID_DEVICE;
 
 	/* command line options */
-	while ((g = getopt(argc, argv, "c:f:m:hlpstv")) != EOF) {
+	while ((g = getopt(argc, argv, "C:c:f:m:hlpstv")) != EOF) {
 		switch (g) {
+		case 'C':
+			c_filename = optarg;
+			opt_conf++;
+			break;
 		case 'c':
 			cpuid_device = optarg;
 			break;
@@ -585,6 +661,26 @@
 		fprintf(stderr, _("%s: must be run as root\n"), progname);
 		exit(1);
 	}
+	
+	/* load the config file settings by default */
+	if (!c_filename)
+		c_filename = CONFIG_FILENAME;
+	
+	if ((c_file = fopen(c_filename, "r")) == NULL) {
+		fprintf(stderr, _("%s: configuration file named '%s' not found.\n"), progname, c_filename);
+		exit(1);
+	} else {
+		char * read = NULL;
+		char * line = (char *) malloc((sizeof(char) * MAXLINE) + 1);
+		
+		while ((read = fgets(line, MAXLINE, c_file)) != NULL) {
+			parse_line(line, strlen(line));
+		}
+		
+		free(line);
+		
+		fclose(c_file);
+	}
 
 	if ((cpuid_fd = open(cpuid_device, O_RDWR)) < 0) {
 		error_warn(_("error opening %s"), cpuid_device);
@@ -606,6 +702,7 @@
 
 	if (opt_list) {
 		list_longrun();
+		conf_cleanup();
 		exit(0);
 	}
 	if (opt_set) {
@@ -623,6 +720,7 @@
 	if (opt_print || opt_verbose) {
 		print_longrun();
 	}
-
+	
+	conf_cleanup();
 	exit(0);
 }

Attachment: signature.asc
Description: This is a digitally signed message part

Reply via email to