summaryrefslogtreecommitdiff
blob: 0a10bc9aa4265a5c6a16166dce7f784a83f4f944 (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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/* Copyright 1999-2010 Gentoo Foundation
 * Distributed under the terms of the GNU General Public License v2
 */
#include <dirent.h>
#include <limits.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>

#include "config.h"

#define ENVD_CONFIG "/etc/env.d/python/config"

/* 127 is the standard return code for "command not found" */
#define EXIT_ERROR 127

char* dir_cat(const char* dir, const char* file)
{
	size_t dir_len = strlen(dir);
	char* abs = malloc(dir_len + strlen(file) + 2);
	strcpy(abs, dir);
	abs[dir_len] = '/';
	abs[dir_len + 1] = 0;
	return strcat(abs, file);
}

const char* find_path(const char* exe)
{
	const char* last_slash = strrchr(exe, '/');
	if (last_slash)
	{
#ifdef HAVE_STRNDUP
		return strndup(exe, last_slash - exe);
#else
		size_t len = last_slash - exe;
		char* ret = malloc(sizeof(char) * (len + 1));
		memcpy(ret, exe, len);
		ret[len] = '\0';
		return(ret);
#endif
	}
	const char* PATH = getenv("PATH");
	if (! PATH)
	{
		/* If PATH is unset, then it defaults to ":/bin:/usr/bin", per
		 * execvp(3).
		 */
		PATH = ":/bin:/usr/bin";
	}
	char* path = strdup(PATH);
#ifdef HAVE_STRTOK_R
	char* state = NULL;
	const char* token = strtok_r(path, ":", &state);
#else
	const char* token = strtok(path, ":");
#endif
	while (token)
	{
		/* If an element of PATH is empty ("::"), then it is "." */
		if (! *token)
		{
			token = ".";
		}
		struct stat sbuf;
		char* str = dir_cat(token, exe);
		if (stat(str, &sbuf) == 0 && (S_ISREG(sbuf.st_mode) || S_ISLNK(sbuf.st_mode)))
		{
			return token;
		}
#ifdef HAVE_STRTOK_R
		token = strtok_r(NULL, ":", &state);
#else
		token = strtok(NULL, ":");
#endif
	}
	return NULL;
}

/* True if a valid file name, and not "python" */
int valid_interpreter(const char* name)
{
	if (! name || ! *name || (strcmp(name, "python") == 0))
	{
		return 0;
	}
	return 1;
}

int get_version(const char* name)
{
	/* Only find files beginning with "python" - this is a fallback,
	 * so we only want CPython
	 */
	if (! valid_interpreter(name) || strncmp(name, "python", 6) != 0)
		return -1;
	int pos = 6;
	int major = 0;
	int minor = 0;
	if (name[pos] < '0' || name[pos] > '9')
		return -1;
	do
	{
		major = major * 10 + name[pos] - '0';
		if (! name[++pos])
			return -1;
	}
	while (name[pos] >= '0' && name[pos] <= '9');
	if (name[pos++] != '.')
		return -1;
	if (name[pos] < '0' || name[pos] > '9')
		return -1;
	do
	{
		minor = minor * 10 + name[pos] - '0';
		if (! name[++pos])
			return (major << 8) | minor;
	}
	while (name[pos] >= '0' && name[pos] <= '9');
	return -1;
}

int filter_python(const struct dirent* file)
{
	return get_version(file->d_name) != -1;
}

/* This implements a version sort, such that the following order applies:
 *    <invalid file names> (should never be seen)
 *    python2.6
 *    python2.9
 *    python2.10
 *    python3.0
 *    python3.1
 *    python3.2
 *    python9.1
 *    python9.9
 *    python9.10
 *    python10.1
 *    python10.9
 *    python10.10
 */
int sort_python(const struct dirent**f1, const struct dirent** f2)
{
	int ver1 = get_version((*f1)->d_name);
	int ver2 = get_version((*f2)->d_name);
	return ver1 - ver2;
}

const char* find_latest(const char* exe)
{
	const char* path = find_path(exe);
	if (! path || ! *path)
	{
		path = "/usr/bin";
	}
	struct dirent** namelist;
	int n = scandir(path, &namelist, filter_python, sort_python);
	const char* ret = NULL;
	if (n < 0)
	{
		return NULL;
	}
	/* Walk backwards through the list. */
	while (n--)
	{
		if (! ret)
			ret = strdup(namelist[n]->d_name);
		free(namelist[n]);
	}
	free(namelist);
	return ret;
}

int main(int argc, char** argv)
{
	const char* EPYTHON = getenv("EPYTHON");
	int script_name_index = -1;
	if (! valid_interpreter(EPYTHON))
	{
		FILE* f = fopen(ENVD_CONFIG, "r");
		if (f)
		{
			struct stat st;
			fstat(fileno(f), &st);
			size_t size = st.st_size;
			char* cont = malloc(size + 1);
			cont = fgets(cont, size + 1, f);
			fclose(f);
			size_t len = strlen(cont);
			if (len && cont[len - 1] == '\n')
				cont[len - 1] = 0;
			EPYTHON = cont;
		}
	}

	if (! valid_interpreter(EPYTHON))
		EPYTHON = find_latest(argv[0]);

	if (! EPYTHON)
		return EXIT_ERROR;

	if (strchr(EPYTHON, '/'))
	{
		fprintf(stderr, "Invalid value of EPYTHON variable or invalid configuration of Python wrapper\n");
		return EXIT_ERROR;
	}

	/* Set GENTOO_PYTHON_PROCESS_NAME environmental variable, if a script with a Python shebang is probably being executed.
	 * argv[0] can be "python", when "#!/usr/bin/env python" shebang is used. */
	if (argc >= 2 && (argv[0][0] == '/' || strcmp(argv[0], "python") == 0) && (argv[1][0] == '/' || strncmp(argv[1], "./", 2) == 0))
		script_name_index = 1;
	else if (argc >= 3 && argv[0][0] == '/' && argv[1][0] == '-' && (argv[2][0] == '/' || strncmp(argv[2], "./", 2) == 0))
		script_name_index = 2;
	if (script_name_index > 0)
	{
		char* script_name = strrchr(argv[script_name_index], '/') + 1;
#ifdef HAVE_SETENV
		setenv("GENTOO_PYTHON_PROCESS_NAME", script_name, 1);
#else
		char* script_name_variable = malloc(sizeof(char) * (strlen("GENTOO_PYTHON_PROCESS_NAME=") + strlen(script_name)));
		sprintf(script_name_variable, "GENTOO_PYTHON_PROCESS_NAME=%s", script_name);
		putenv(script_name_variable);
#endif
	}

	const char* path = find_path(argv[0]);
	if (path)
	{
		argv[0] = dir_cat(path, EPYTHON);
		execv(argv[0], argv);
		/* If this failed, then just search the PATH. */
	}

	argv[0] = (char*) EPYTHON;
	execvp(EPYTHON, argv);
	return EXIT_ERROR;
}