summaryrefslogtreecommitdiff
blob: fc5af43064b7c6101cfaf3182647ee41b850ff80 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
diff -aur distcc-1.2.1.orig/src/distcc.c distcc-1.2.1/src/distcc.c
--- distcc-1.2.1.orig/src/distcc.c	2003-02-23 14:33:19.000000000 -0800
+++ distcc-1.2.1/src/distcc.c	2003-02-26 16:36:57.000000000 -0800
@@ -481,6 +481,156 @@
     }
 }
 
+static char *dcc_safe_getcwd(void) 
+{
+	char *cwdstr;
+	size_t len = 200;
+
+	do {
+		cwdstr = malloc(len);
+		if (NULL == cwdstr) break;
+
+		if (NULL == getcwd(cwdstr, len)) {
+			free(cwdstr);
+			cwdstr = NULL;
+			if (ERANGE == errno) {
+				len += 200;
+				continue;
+			}
+		}
+	} while(0);
+
+	if (NULL == cwdstr) {
+		rs_log_error("can't get working directory");
+		exit(1);
+	}
+
+	return cwdstr;
+}
+
+/**
+ * Filter PATH past point that wrapper was called
+ */
+static void dcc_wrapper_trunc_path(char *progname, char *path, char *argv[]) 
+{
+    static const char *envpath = "PATH=";
+    const char *p;
+    char *n, *str;
+    char *ppath = NULL;
+    int len, maxlen, plen = 0;
+
+    maxlen = strlen(path) + strlen(envpath) + 1;
+    str = malloc(maxlen);
+    if (NULL == str) exit(1);
+
+    /* if we were called explicitly, find the (partial) path */
+    if (progname != argv[0]) {
+    	/* eliminate this explicit path from PATH */ 
+        char *cwdstr = NULL;
+	size_t cwdlen = 0;
+
+	if ('/' != argv[0][0]) {
+		cwdstr = dcc_safe_getcwd();
+		cwdlen = strlen(cwdstr);
+		/* or we could just error out and exit??? */
+	}
+
+	plen = progname - argv[0] - 1;
+	len = cwdlen + plen;
+        ppath = malloc(len);
+	if (NULL == ppath) exit(1);
+
+	ppath[0] = 0;
+	if (NULL != cwdstr) {
+		strncpy(ppath, cwdstr, len);
+		ppath[cwdlen++] = '/';
+		ppath[cwdlen] = 0;
+		free(cwdstr);
+	}
+	strncat(ppath, argv[0], cwdlen + plen);
+	rs_trace("looking for explicit path: %s\n", ppath);
+    }
+    else
+        ppath = NULL;
+	    
+    for (p = n = path; NULL != n; p = n ? n + 1 : NULL) {
+        n = strchr(p, ':');
+        if (NULL != n)	
+            len = n - p;
+	else
+            len = strlen(p);
+
+        if (NULL != ppath) {
+	    /* check for partial path name match,
+	        pmatch is last portion of path */
+	    rs_trace("checking for ppath in %s\n", p);
+            if (len != plen || 0 != strncmp(p, ppath, len))
+                continue;
+        }
+	else {
+	    strncpy(str, p, len);
+            snprintf(str + len, maxlen - len, "/%s", progname);
+	    rs_trace("checking %s\n", str);
+            if (0 != access(str, X_OK))
+                continue;
+	    rs_trace("found %s\n", str);
+        }
+        /* getting here means either we found the path we ran in, or
+           we found the target program we're trying to run;
+           so PATH now starts at next entry */
+	if (NULL != n)
+	    p = n + 1;
+	else
+	    p = "";
+        break;
+    }
+
+    /* if temp path, p, still contains something, reset PATH;
+       p will be NULL if relative or non-PATH invocation, so
+       the current path is probably free from recursion */
+    if (NULL != p) {
+	strncpy(str, envpath, maxlen);
+	strncat(str, p, maxlen);
+	rs_trace("wrapper is setting %s\n", str);
+	n = strdup(str);
+        if ((NULL == n) || (-1 == putenv(n))) {
+                rs_log_error("putenv PATH failed");
+                exit(1);
+        }
+    }
+    else
+        rs_trace("wrapper not modifying PATH");
+
+    free(str);
+    free(ppath);
+}
+
+static char *dcc_support_wrapper(char *argv[])
+{
+    char *progname, *envstr;
+
+    /* see if program was called with a full or partial path */
+    if ((progname = strrchr(argv[0], '/')) != NULL)
+        progname++;
+    else
+        progname = argv[0];
+
+    if (NULL == (envstr = getenv("DISTCC_HOSTS"))) {
+    	char localonly[] = "DISTCC_HOSTS=localhost";
+    	if (-1 == putenv(localonly)) {
+            rs_log_error("unable to set default DISTCC_HOSTS");
+	    exit(1);
+	}
+    }
+
+    /* right now, we're only playing with path... but
+       wouldn't it be interesting to look at CHOST, too? */
+    if (NULL != (envstr = getenv("PATH")))
+        dcc_wrapper_trunc_path(progname, envstr, argv);
+    
+    return progname;
+}
+
 
 /**
  * distcc client entry point.
@@ -490,6 +640,7 @@
  * Performs basic setup and checks for distcc arguments, and then kicks of
  * dcc_build_somewhere().
  **/
+#include <libgen.h>
 int main(int argc, char **argv)
 {
     int status;
@@ -499,17 +650,25 @@
 
     dcc_set_trace_from_env();
 
-    if (argc <= 1 || !strcmp(argv[1], "--help")) {
-        dcc_show_usage();
-        exit(0);
-    } else if (!strcmp(argv[1], "--version")) {
-        dcc_show_version("distcc");
-        exit(0);
-    }
-
     dcc_recursion_safeguard();
 
-    dcc_find_compiler(argv, &compiler_args);
+    if (NULL != strstr(basename(argv[0]), "distcc")) {
+
+        if (argc <= 1 || !strcmp(argv[1], "--help")) {
+            dcc_show_usage();
+            exit(0);
+        } else if (!strcmp(argv[1], "--version")) {
+            dcc_show_version("distcc");
+            exit(0);
+        }
+
+        dcc_find_compiler(argv, &compiler_args);
+    }
+    else {
+    	char *progname = dcc_support_wrapper(argv);
+        dcc_shallowcopy_argv(argv, &compiler_args, 0);
+        compiler_args[0] = progname;
+    }
 
     dcc_exit(dcc_build_somewhere(compiler_args, &status));
 }