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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
|
/**
* @file rpath.c
*/
#include "spm.h"
/**
* Wrapper function to execute `patchelf` with arguments
* @param _filename Path of file to modify
* @param _args Arguments to pass to `patchelf`
* @return success=Process struct, failure=NULL
*/
Process *patchelf(const char *_filename, const char *_args) {
char *filename = strdup(_filename);
char *args = strdup(_args);
Process *proc_info = NULL;
char sh_cmd[PATH_MAX];
sh_cmd[0] = '\0';
strchrdel(args, "&;|");
strchrdel(filename, "&;|");
sprintf(sh_cmd, "patchelf %s %s 2>&1", args, filename);
shell(&proc_info, SHELL_OUTPUT, sh_cmd);
free(filename);
free(args);
return proc_info;
}
/**
* Determine whether a RPATH or RUNPATH is present in file
*
* TODO: Replace with OS-native solution(s)
*
* @param _filename path to executable or library
* @return -1=OS error, 0=has rpath, 1=not found
*/
int has_rpath(const char *_filename) {
int result = 1; // default: not found
char *filename = strdup(_filename);
if (!filename) {
return -1;
}
// sanitize input path
strchrdel(filename, "&;|");
Process *pe = patchelf(filename, "--print-rpath");
strip(pe->output);
if (!isempty(pe->output)) {
result = 0;
}
else {
// something went wrong with patchelf other than
// what we're looking for
result = -1;
}
free(filename);
shell_free(pe);
return result;
}
/**
* Returns a RPATH or RUNPATH if one is defined in `_filename`
*
* TODO: Replace with OS-native solution(s)
*
* @param _filename path to executable or library
* @return RPATH string, NULL=error (caller is responsible for freeing memory)
*/
char *rpath_get(const char *_filename) {
if ((has_rpath(_filename)) != 0) {
return strdup("");
}
char *filename = strdup(_filename);
if (!filename) {
return NULL;
}
char *path = strdup(filename);
if (!path) {
free(filename);
return NULL;
}
char *rpath = NULL;
// sanitize input path
strchrdel(path, "&;|");
Process *pe = patchelf(filename, "--print-rpath");
if (pe->returncode != 0) {
fprintf(stderr, "patchelf error: %s %s\n", path, strip(pe->output));
return NULL;
}
rpath = (char *)calloc(strlen(pe->output) + 1, sizeof(char));
if (!rpath) {
free(filename);
free(path);
shell_free(pe);
return NULL;
}
strncpy(rpath, pe->output, strlen(pe->output));
strip(rpath);
free(filename);
free(path);
shell_free(pe);
return rpath;
}
/**
* Generate a RPATH in the form of:
*
* `$ORIGIN/relative/path/to/lib/from/_filename/path`
*
* @param _filename
* @return
*/
char *rpath_generate(const char *_filename) {
const char *origin = "$ORIGIN/";
char *filename = realpath(_filename, NULL);
if (!filename) {
return NULL;
}
char *nearest_lib = rpath_autodetect(filename);
if (!nearest_lib) {
free(filename);
return NULL;
}
char *result = (char *)calloc(strlen(origin) + strlen(nearest_lib) + 1, sizeof(char));
if (!result) {
free(filename);
free(nearest_lib);
return NULL;
}
sprintf(result, "%s%s", origin, nearest_lib);
free(filename);
free(nearest_lib);
return result;
}
/**
* Set the RPATH of an executable
* @param filename
* @param rpath
* @return
*/
int rpath_set(const char *filename, const char *rpath) {
int returncode = 0;
char *rpath_orig = rpath_get(filename);
if (!rpath_orig) {
return -1;
}
// Are the original and new RPATH identical?
if (strcmp(rpath_orig, rpath) == 0) {
free(rpath_orig);
return 0;
}
char args[PATH_MAX];
memset(args, '\0', PATH_MAX);
sprintf(args, "--set-rpath '%s'", rpath); // note: rpath requires single-quotes
Process *pe = patchelf(filename, args);
if (pe != NULL) {
returncode = pe->returncode;
}
shell_free(pe);
free(rpath_orig);
return returncode;
}
/**
* Automatically detect the nearest lib directory and set the RPATH of an executable
* @param filename
* @param _rpath
* @return
*/
int rpath_autoset(const char *filename) {
int returncode = 0;
char *rpath_new = rpath_generate(filename);
if (!rpath_new) {
return -1;
}
returncode = rpath_set(filename, rpath_new);
free(rpath_new);
return returncode;
}
/**
* Using `filename` as a starting point, step backward through the filesystem looking for a lib directory
* @param filename path to file (or a directory)
* @return success=relative path from `filename` to nearest lib directory, failure=NULL
*/
char *rpath_autodetect(const char *filename) {
int has_real_libdir = 0;
char *rootdir = dirname(filename);
char *start = realpath(rootdir, NULL);
char *cwd = realpath(".", NULL);
char *result = NULL;
// Change directory to the requested root
chdir(start);
char *visit = NULL; // Current directory
char tmp[PATH_MAX]; // Current directory with lib directory appended
char relative[PATH_MAX]; // Generated relative path to lib directory
char sep[2]; // Holds the platform's directory separator
// Initialize character arrays;
tmp[0] = '\0';
relative[0] = '\0';
sprintf(sep, "%c", DIRSEP);
while(1) {
// Where are we in the file system?
if((visit = getcwd(NULL, PATH_MAX)) == NULL) {
exit(errno);
}
// Using the current visit path, check if it contains a lib directory
snprintf(tmp, PATH_MAX, "%s%c%s", visit, DIRSEP, "lib");
if (access(tmp, F_OK) == 0) {
strcat(relative, "lib");
has_real_libdir = 1; // gate for memory allocation below
free(visit);
break;
}
// Reaching the top of the file system indicates our search for a lib directory failed
else if (strcmp(visit, "/") == 0) {
free(visit);
break;
}
// Assemble relative path step for this location
strcat(relative, "..");
strcat(relative, sep);
// Step one directory level back
chdir("..");
free(visit);
}
// If we found a viable lib directory, allocate memory for it
if (has_real_libdir) {
result = (char *)calloc(strlen(relative) + 1, sizeof(char));
if (!result) {
chdir(cwd); // return to calling directory
return NULL;
}
// Copy character array data to the result
strncpy(result, relative, strlen(relative));
}
chdir(cwd); // return to calling directory
free(rootdir);
free(cwd);
free(start);
return result;
}
|