[Ltrace-devel] r46 - in ltrace/trunk: . etc

Ian Wienand ianw-guest at costa.debian.org
Mon Jul 17 21:18:37 UTC 2006


Author: ianw-guest
Date: 2006-07-17 21:18:35 +0000 (Mon, 17 Jul 2006)
New Revision: 46

Modified:
   ltrace/trunk/ChangeLog
   ltrace/trunk/etc/ltrace.conf
   ltrace/trunk/ltrace.1
   ltrace/trunk/ltrace.c
   ltrace/trunk/options.c
   ltrace/trunk/options.h
Log:
add -F option to read in config files
Steve Fink <sphink at gmail.com>


Modified: ltrace/trunk/ChangeLog
===================================================================
--- ltrace/trunk/ChangeLog	2006-06-19 12:20:05 UTC (rev 45)
+++ ltrace/trunk/ChangeLog	2006-07-17 21:18:35 UTC (rev 46)
@@ -1,3 +1,8 @@
+2006-07-16  Steve Fink  <sphink at gmail.com>
+
+	* options.c: implement -F flag for alternate config file(s)
+	* ltrace.c: load SYSCONFDIR/ltrace.conf and ~/.ltrace.conf by default
+
 2006-06-19  Ian Wienand  <ianw at ieee.org>
 
 	* sysdeps/linux-gnu/mksyscallent: update, fix for ia64

Modified: ltrace/trunk/etc/ltrace.conf
===================================================================
--- ltrace/trunk/etc/ltrace.conf	2006-06-19 12:20:05 UTC (rev 45)
+++ ltrace/trunk/etc/ltrace.conf	2006-07-17 21:18:35 UTC (rev 46)
@@ -1,4 +1,9 @@
 ; ltrace.conf
+;
+; ~/.ltrace.conf will also be read, if it exists. The -F option may be
+; used to suppress the automatic inclusion of both this file and
+; ~/.ltrace.conf, and load a different config file or config files
+; instead.
 
 ; Argument types:
 ; +		== May vary (ie, is a returned value) (prefix)

Modified: ltrace/trunk/ltrace.1
===================================================================
--- ltrace/trunk/ltrace.1	2006-06-19 12:20:05 UTC (rev 45)
+++ ltrace/trunk/ltrace.1	2006-07-17 21:18:35 UTC (rev 46)
@@ -62,6 +62,12 @@
 or clone(2) system calls.
 The new process is attached as soon as its pid is known.
 .TP
+.I \-F
+Load an alternate config file. Normally, /etc/ltrace.conf and
+~/.ltrace.conf will be read (the latter only if it exists).
+Use this option to load the given file or files instead of
+those two default files.
+.TP
 .I \-h, \-\-help
 Show a summary of the options to ltrace and exit.
 .TP

Modified: ltrace/trunk/ltrace.c
===================================================================
--- ltrace/trunk/ltrace.c	2006-06-19 12:20:05 UTC (rev 45)
+++ ltrace/trunk/ltrace.c	2006-07-17 21:18:35 UTC (rev 46)
@@ -5,6 +5,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
+#include <pwd.h>
 #include <string.h>
 #include <errno.h>
 #include <sys/param.h>
@@ -18,10 +19,6 @@
 #include "options.h"
 #include "debug.h"
 
-#ifndef SYSCONFDIR
-#define SYSCONFDIR "/etc"
-#endif
-
 char *command = NULL;
 struct process *list_of_processes = NULL;
 
@@ -98,7 +95,6 @@
 int main(int argc, char **argv)
 {
 	struct opt_p_t *opt_p_tmp;
-	char *home;
 
 	atexit(normal_exit);
 	signal(SIGINT, signal_exit);	/* Detach processes when interrupted */
@@ -106,17 +102,24 @@
 
 	guess_cols();
 	argv = process_options(argc, argv);
-	read_config_file(SYSCONFDIR "/ltrace.conf");
-	home = getenv("HOME");
-	if (home) {
+        while (opt_F) {
+	    /* If filename begins with ~, expand it to the user's home */
+	    /* directory. This does not correctly handle ~yoda, but that */
+	    /* isn't as bad as it seems because the shell will normally */
+	    /* be doing the expansion for us; only the hardcoded */
+	    /* ~/.ltrace.conf should ever use this code. */
+	    if (opt_F->filename[0] == '~') {
 		char path[PATH_MAX];
-		if (strlen(home) > PATH_MAX - 15) {
-			fprintf(stderr, "Error: $HOME too long\n");
-			exit(1);
-		}
-		strcpy(path, getenv("HOME"));
-		strcat(path, "/.ltrace.conf");
+		char *home_dir = getpwuid(geteuid())->pw_dir;
+		strncpy(path, home_dir, PATH_MAX - 1);
+		path[PATH_MAX - 1] = '\0';
+		strncat(path, opt_F->filename + 1,
+			PATH_MAX - strlen(path) - 1);
 		read_config_file(path);
+	    } else {
+		read_config_file(opt_F->filename);
+	    }
+	    opt_F = opt_F->next;
 	}
 	if (opt_e) {
 		struct opt_e_t *tmp = opt_e;

Modified: ltrace/trunk/options.c
===================================================================
--- ltrace/trunk/options.c	2006-06-19 12:20:05 UTC (rev 45)
+++ ltrace/trunk/options.c	2006-07-17 21:18:35 UTC (rev 46)
@@ -21,6 +21,13 @@
 #include "options.h"
 #include "defs.h"
 
+#ifndef SYSCONFDIR
+#define SYSCONFDIR "/etc"
+#endif
+
+#define SYSTEM_CONFIG_FILE SYSCONFDIR "/trace.conf"
+#define USER_CONFIG_FILE "~/.ltrace.conf"
+
 #define MAX_LIBRARY		30
 char *library[MAX_LIBRARY];
 int library_num = 0;
@@ -53,6 +60,9 @@
 /* List of global function names given to -x: */
 struct opt_x_t *opt_x = NULL;
 
+/* List of filenames give to option -F: */
+struct opt_F_t *opt_F = NULL;	/* alternate configuration file(s) */
+
 #ifdef PLT_REINITALISATION_BP
 /* Set a break on the routine named here in order to re-initialize breakpoints
    after all the PLTs have been initialzed */
@@ -88,6 +98,12 @@
 		"  -e expr             modify which events to trace.\n"
 		"  -f                  follow forks.\n"
 # if HAVE_GETOPT_LONG
+                "  -F, --config=FILE   load alternate configuration file\n"
+# else
+                "  -F FILE             load alternate configuration file\n"
+# endif
+                "                      (can be repeated).\n"
+# if HAVE_GETOPT_LONG
 		"  -h, --help          display this help and exit.\n"
 # else
 		"  -h                  display this help and exit.\n"
@@ -174,6 +190,7 @@
 		int option_index = 0;
 		static struct option long_options[] = {
 			{"align", 1, 0, 'a'},
+                        {"config", 1, 0, 'F'},
 			{"debug", 0, 0, 'd'},
 # ifdef USE_DEMANGLE
 			{"demangle", 0, 0, 'C'},
@@ -189,14 +206,14 @@
 # ifdef USE_DEMANGLE
 				"C"
 # endif
-				"a:e:l:n:o:p:s:u:x:X:", long_options,
+				"a:e:F:l:n:o:p:s:u:x:X:", long_options,
 				&option_index);
 #else
 		c = getopt(argc, argv, "+cdfhiLrStTV"
 # ifdef USE_DEMANGLE
 			   "C"
 # endif
-			   "a:e:l:n:o:p:s:u:x:X:");
+			   "a:e:F:l:n:o:p:s:u:x:X:");
 #endif
 		if (c == -1) {
 			break;
@@ -252,6 +269,18 @@
 		case 'f':
 			opt_f = 1;
 			break;
+                case 'F':
+                	{
+                          struct opt_F_t *tmp = malloc(sizeof(struct opt_F_t));
+                          if (!tmp) {
+                            perror("ltrace: malloc");
+                            exit(1);
+                          }
+                          tmp->filename = strdup(optarg);
+                          tmp->next = opt_F;
+                          opt_F = tmp;
+                          break;
+                        }
 		case 'h':
 			usage();
 			exit(0);
@@ -371,6 +400,29 @@
 	argv += optind;
 #endif
 
+        if (!opt_F) {
+	    opt_F = malloc(sizeof(struct opt_F_t));
+	    opt_F->next = malloc(sizeof(struct opt_F_t));
+	    opt_F->next->next = NULL;
+	    opt_F->filename = USER_CONFIG_FILE;
+	    opt_F->next->filename = SYSTEM_CONFIG_FILE;
+        }
+	/* Reverse the config file list since it was built by
+	 * prepending, and it would make more sense to process the
+	 * files in the order they were given. Probably it would make
+	 * more sense to keep a tail pointer instead? */
+	{
+	    struct opt_F_t *egg = NULL;
+	    struct opt_F_t *chicken;
+	    while (opt_F) {
+		chicken = opt_F->next;
+		opt_F->next = egg;
+		egg = opt_F;
+		opt_F = chicken;
+	    }
+	    opt_F = egg;
+	}
+
 	if (!opt_p && argc < 1) {
 		fprintf(stderr, "%s: too few arguments\n", progname);
 		usage();

Modified: ltrace/trunk/options.h
===================================================================
--- ltrace/trunk/options.h	2006-06-19 12:20:05 UTC (rev 45)
+++ ltrace/trunk/options.h	2006-07-17 21:18:35 UTC (rev 46)
@@ -31,6 +31,11 @@
 	struct opt_e_t *next;
 };
 
+struct opt_F_t {
+	char *filename;
+	struct opt_F_t *next;
+};
+
 struct opt_x_t {
 	char *name;
 	int found;
@@ -42,6 +47,8 @@
 extern struct opt_e_t *opt_e;	/* list of function names to display */
 extern int opt_e_enable;	/* 0 if '!' is used, 1 otherwise */
 
+extern struct opt_F_t *opt_F;	/* alternate configuration file(s) */
+
 extern struct opt_x_t *opt_x;	/* list of functions to break at */
 
 extern char **process_options(int argc, char **argv);




More information about the Ltrace-devel mailing list