]>
Commit | Line | Data |
---|---|---|
1b78aef5 | 1 | // Copyright (C) Andrew Tridgell 2002 (original file) |
ef36f781 | 2 | // Copyright (C) 2006-2014 Red Hat Inc. (systemtap changes) |
1b78aef5 DS |
3 | // |
4 | // This program is free software; you can redistribute it and/or | |
5 | // modify it under the terms of the GNU General Public License as | |
6 | // published by the Free Software Foundation; either version 2 of the | |
7 | // License, or (at your option) any later version. | |
8 | // | |
9 | // This program is distributed in the hope that it will be useful, but | |
10 | // WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the GNU | |
12 | // General Public License for more details. | |
13 | // | |
14 | // You should have received a copy of the GNU General Public License | |
e8daaf60 | 15 | // along with this program. If not, see <http://www.gnu.org/licenses/>. |
1b78aef5 DS |
16 | |
17 | #include "util.h" | |
0f5d597d | 18 | #include "stap-probe.h" |
1b78aef5 DS |
19 | #include <stdexcept> |
20 | #include <cerrno> | |
c0d1b5a0 | 21 | #include <map> |
01cc94dc | 22 | #include <set> |
c0d1b5a0 | 23 | #include <string> |
85007c04 | 24 | #include <fstream> |
3b6f3bbb | 25 | #include <cassert> |
77455f97 | 26 | #include <ext/stdio_filebuf.h> |
1b78aef5 DS |
27 | |
28 | extern "C" { | |
081b45d1 | 29 | #include <elf.h> |
4cc40e82 | 30 | #include <fcntl.h> |
baba4e15 | 31 | #include <grp.h> |
1b78aef5 | 32 | #include <pwd.h> |
4cc40e82 | 33 | #include <spawn.h> |
8bf4a144 | 34 | #include <stdio.h> |
1b78aef5 | 35 | #include <stdlib.h> |
4cc40e82 JS |
36 | #include <sys/stat.h> |
37 | #include <sys/types.h> | |
38 | #include <sys/wait.h> | |
39 | #include <unistd.h> | |
c0d1b5a0 | 40 | #include <regex.h> |
7a1513b1 | 41 | #include <stdarg.h> |
83ae46dd | 42 | #ifndef SINGLE_THREADED |
26a39006 | 43 | #include <pthread.h> |
83ae46dd | 44 | #endif |
1b78aef5 DS |
45 | } |
46 | ||
47 | using namespace std; | |
77455f97 | 48 | using namespace __gnu_cxx; |
1b78aef5 DS |
49 | |
50 | ||
51 | // Return current users home directory or die. | |
52 | const char * | |
53 | get_home_directory(void) | |
54 | { | |
55 | const char *p = getenv("HOME"); | |
56 | if (p) | |
57 | return p; | |
58 | ||
59 | struct passwd *pwd = getpwuid(getuid()); | |
60 | if (pwd) | |
61 | return pwd->pw_dir; | |
62 | ||
35f29567 CM |
63 | cerr << _("Unable to determine home directory") << endl; |
64 | return "/"; | |
1b78aef5 DS |
65 | } |
66 | ||
67 | ||
b12c8986 DB |
68 | // Get the size of a file in bytes |
69 | size_t | |
70 | get_file_size(const string &path) | |
71 | { | |
72 | struct stat file_info; | |
73 | ||
74 | if (stat(path.c_str(), &file_info) == 0) | |
75 | return file_info.st_size; | |
76 | else | |
77 | return 0; | |
78 | } | |
79 | ||
80 | // Get the size of a file in bytes | |
a5751672 JS |
81 | size_t |
82 | get_file_size(int fd) | |
83 | { | |
84 | struct stat file_info; | |
85 | ||
86 | if (fstat(fd, &file_info) == 0) | |
87 | return file_info.st_size; | |
88 | else | |
89 | return 0; | |
90 | } | |
91 | ||
92 | // Check that a file is present | |
b12c8986 DB |
93 | bool |
94 | file_exists (const string &path) | |
95 | { | |
96 | struct stat file_info; | |
97 | ||
98 | if (stat(path.c_str(), &file_info) == 0) | |
99 | return true; | |
100 | ||
101 | return false; | |
102 | } | |
103 | ||
1b78aef5 DS |
104 | // Copy a file. The copy is done via a temporary file and atomic |
105 | // rename. | |
e16dc041 JS |
106 | bool |
107 | copy_file(const string& src, const string& dest, bool verbose) | |
1b78aef5 DS |
108 | { |
109 | int fd1, fd2; | |
110 | char buf[10240]; | |
111 | int n; | |
112 | string tmp; | |
113 | char *tmp_name; | |
114 | mode_t mask; | |
115 | ||
e16dc041 | 116 | if (verbose) |
b530b5b3 | 117 | clog << _F("Copying %s to %s", src.c_str(), dest.c_str()) << endl; |
e16dc041 | 118 | |
1b78aef5 | 119 | // Open the src file. |
e16dc041 | 120 | fd1 = open(src.c_str(), O_RDONLY); |
1b78aef5 | 121 | if (fd1 == -1) |
e16dc041 | 122 | goto error; |
1b78aef5 DS |
123 | |
124 | // Open the temporary output file. | |
125 | tmp = dest + string(".XXXXXX"); | |
126 | tmp_name = (char *)tmp.c_str(); | |
127 | fd2 = mkstemp(tmp_name); | |
128 | if (fd2 == -1) | |
129 | { | |
130 | close(fd1); | |
e16dc041 | 131 | goto error; |
1b78aef5 DS |
132 | } |
133 | ||
134 | // Copy the src file to the temporary output file. | |
135 | while ((n = read(fd1, buf, sizeof(buf))) > 0) | |
136 | { | |
137 | if (write(fd2, buf, n) != n) | |
138 | { | |
139 | close(fd2); | |
140 | close(fd1); | |
141 | unlink(tmp_name); | |
e16dc041 | 142 | goto error; |
1b78aef5 DS |
143 | } |
144 | } | |
145 | close(fd1); | |
146 | ||
147 | // Set the permissions on the temporary output file. | |
148 | mask = umask(0); | |
149 | fchmod(fd2, 0666 & ~mask); | |
150 | umask(mask); | |
151 | ||
152 | // Close the temporary output file. The close can fail on NFS if | |
153 | // out of space. | |
154 | if (close(fd2) == -1) | |
155 | { | |
156 | unlink(tmp_name); | |
e16dc041 | 157 | goto error; |
1b78aef5 DS |
158 | } |
159 | ||
160 | // Rename the temporary output file to the destination file. | |
e16dc041 JS |
161 | unlink(dest.c_str()); |
162 | if (rename(tmp_name, dest.c_str()) == -1) | |
1b78aef5 DS |
163 | { |
164 | unlink(tmp_name); | |
e16dc041 | 165 | goto error; |
1b78aef5 DS |
166 | } |
167 | ||
e16dc041 JS |
168 | return true; |
169 | ||
170 | error: | |
b530b5b3 LB |
171 | cerr << _F("Copy failed (\"%s\" to \"%s\"): %s", src.c_str(), |
172 | dest.c_str(), strerror(errno)) << endl; | |
e16dc041 | 173 | return false; |
1b78aef5 DS |
174 | } |
175 | ||
176 | ||
177 | // Make sure a directory exists. | |
178 | int | |
3b6f3bbb | 179 | create_dir(const char *dir, int mode) |
1b78aef5 DS |
180 | { |
181 | struct stat st; | |
182 | if (stat(dir, &st) == 0) | |
183 | { | |
184 | if (S_ISDIR(st.st_mode)) | |
185 | return 0; | |
186 | errno = ENOTDIR; | |
187 | return 1; | |
188 | } | |
189 | ||
3b6f3bbb DB |
190 | // Create the directory. We must create each component |
191 | // of the path ourselves. | |
192 | vector<string> components; | |
193 | tokenize (dir, components, "/"); | |
194 | string path; | |
195 | if (*dir == '/') | |
196 | { | |
197 | // Absolute path | |
198 | path = "/"; | |
199 | } | |
200 | unsigned limit = components.size (); | |
201 | assert (limit != 0); | |
202 | for (unsigned ix = 0; ix < limit; ++ix) | |
203 | { | |
204 | path += components[ix] + '/'; | |
205 | if (mkdir(path.c_str (), mode) != 0 && errno != EEXIST) | |
206 | return 1; | |
207 | } | |
1b78aef5 DS |
208 | |
209 | return 0; | |
210 | } | |
211 | ||
98f552c2 DB |
212 | // Remove a file or directory |
213 | int | |
214 | remove_file_or_dir (const char *name) | |
215 | { | |
216 | int rc; | |
217 | struct stat st; | |
218 | ||
219 | if ((rc = stat(name, &st)) != 0) | |
220 | { | |
221 | if (errno == ENOENT) | |
222 | return 0; | |
223 | return 1; | |
224 | } | |
225 | ||
226 | if (remove (name) != 0) | |
227 | return 1; | |
aeb9cc10 | 228 | |
98f552c2 DB |
229 | return 0; |
230 | } | |
1b78aef5 | 231 | |
3892d516 DB |
232 | /* Obtain the gid of the given group. */ |
233 | gid_t get_gid (const char *group_name) | |
234 | { | |
235 | struct group *stgr; | |
236 | /* If we couldn't find the group, return an invalid number. */ | |
237 | stgr = getgrnam(group_name); | |
238 | if (stgr == NULL) | |
239 | return (gid_t)-1; | |
240 | return stgr->gr_gid; | |
241 | } | |
242 | ||
0da3e7a0 DB |
243 | // Determine whether the current user is in the given group |
244 | // by gid. | |
baba4e15 | 245 | bool |
0da3e7a0 | 246 | in_group_id (gid_t target_gid) |
baba4e15 | 247 | { |
baba4e15 DB |
248 | // According to the getgroups() man page, getgroups() may not |
249 | // return the effective gid, so try to match it first. */ | |
db0a43c3 | 250 | if (target_gid == getegid()) |
baba4e15 DB |
251 | return true; |
252 | ||
253 | // Get the list of the user's groups. | |
f429df66 | 254 | int ngids = getgroups(0, 0); // Returns the number to allocate. |
db0a43c3 RM |
255 | if (ngids > 0) { |
256 | gid_t gidlist[ngids]; | |
257 | ngids = getgroups(ngids, gidlist); | |
258 | for (int i = 0; i < ngids; i++) { | |
259 | // If the user is a member of the target group, then we're done. | |
260 | if (gidlist[i] == target_gid) | |
261 | return true; | |
262 | } | |
263 | } | |
baba4e15 | 264 | if (ngids < 0) { |
b530b5b3 | 265 | cerr << _("Unable to retrieve group list") << endl; |
baba4e15 DB |
266 | return false; |
267 | } | |
268 | ||
baba4e15 DB |
269 | // The user is not a member of the target group |
270 | return false; | |
271 | } | |
272 | ||
85007c04 DB |
273 | /* |
274 | * Returns a string describing memory resource usage. | |
275 | * Since it seems getrusage() doesn't maintain the mem related fields, | |
276 | * this routine parses /proc/self/statm to get the statistics. | |
277 | */ | |
278 | string | |
279 | getmemusage () | |
280 | { | |
281 | static long sz = sysconf(_SC_PAGESIZE); | |
282 | ||
d54a65f6 | 283 | long pages; |
85007c04 DB |
284 | ostringstream oss; |
285 | ifstream statm("/proc/self/statm"); | |
286 | statm >> pages; | |
c338e2c5 | 287 | long kb1 = pages * sz / 1024; // total program size; vmsize |
85007c04 | 288 | statm >> pages; |
c338e2c5 | 289 | long kb2 = pages * sz / 1024; // resident set size; vmrss |
85007c04 | 290 | statm >> pages; |
c338e2c5 FCE |
291 | long kb3 = pages * sz / 1024; // shared pages |
292 | statm >> pages; | |
293 | long kb4 = pages * sz / 1024; // text | |
294 | statm >> pages; | |
295 | (void) kb4; | |
296 | long kb5 = pages * sz / 1024; // library | |
297 | statm >> pages; | |
298 | (void) kb5; | |
299 | long kb6 = pages * sz / 1024; // data+stack | |
300 | statm >> pages; | |
301 | long kb7 = pages * sz / 1024; // dirty | |
302 | (void) kb7; | |
303 | ||
304 | oss << _F("using %ldvirt/%ldres/%ldshr/%lddata kb, ", kb1, kb2, kb3, kb6); | |
85007c04 DB |
305 | return oss.str(); |
306 | } | |
307 | ||
1b78aef5 DS |
308 | void |
309 | tokenize(const string& str, vector<string>& tokens, | |
9c269440 | 310 | const string& delimiters) |
1b78aef5 DS |
311 | { |
312 | // Skip delimiters at beginning. | |
313 | string::size_type lastPos = str.find_first_not_of(delimiters, 0); | |
314 | // Find first "non-delimiter". | |
315 | string::size_type pos = str.find_first_of(delimiters, lastPos); | |
316 | ||
317 | while (pos != string::npos || lastPos != string::npos) | |
318 | { | |
319 | // Found a token, add it to the vector. | |
320 | tokens.push_back(str.substr(lastPos, pos - lastPos)); | |
321 | // Skip delimiters. Note the "not_of" | |
322 | lastPos = str.find_first_not_of(delimiters, pos); | |
323 | // Find next "non-delimiter" | |
324 | pos = str.find_first_of(delimiters, lastPos); | |
325 | } | |
326 | } | |
327 | ||
49dbe419 DB |
328 | // Akin to tokenize(...,...), but allow tokens before the first delimeter, after the |
329 | // last delimiter and allow internal empty tokens | |
330 | void | |
331 | tokenize_full(const string& str, vector<string>& tokens, | |
9c269440 | 332 | const string& delimiters) |
49dbe419 DB |
333 | { |
334 | // Check for an empty string or a string of length 1. Neither can have the requested | |
335 | // components. | |
336 | if (str.size() <= 1) | |
337 | return; | |
338 | ||
339 | // Find the first delimeter. | |
340 | string::size_type lastPos = 0; | |
341 | string::size_type pos = str.find_first_of(delimiters, lastPos); | |
342 | if (pos == string::npos) | |
343 | return; // no delimeters | |
344 | ||
345 | /* No leading empty component allowed. */ | |
346 | if (pos == lastPos) | |
347 | ++lastPos; | |
348 | ||
349 | assert (lastPos < str.size()); | |
350 | do | |
351 | { | |
352 | pos = str.find_first_of(delimiters, lastPos); | |
353 | if (pos == string::npos) | |
354 | break; // Final trailing component | |
355 | // Found a token, add it to the vector. | |
356 | tokens.push_back(str.substr (lastPos, pos - lastPos)); | |
357 | // Skip the delimiter. | |
358 | lastPos = pos + 1; | |
359 | } | |
360 | while (lastPos < str.size()); | |
361 | ||
362 | // A final non-delimited token, if it is not empty. | |
363 | if (lastPos < str.size()) | |
364 | { | |
365 | assert (pos == string::npos); | |
366 | tokens.push_back(str.substr (lastPos)); | |
367 | } | |
368 | } | |
1b78aef5 | 369 | |
91699a70 JS |
370 | // Akin to tokenize(...,"::"), but it also has to deal with C++ template |
371 | // madness. We do this naively by balancing '<' and '>' characters. This | |
372 | // doesn't eliminate blanks either, so a leading ::scope still works. | |
373 | void | |
374 | tokenize_cxx(const string& str, vector<string>& tokens) | |
375 | { | |
376 | int angle_count = 0; | |
377 | string::size_type pos = 0; | |
378 | string::size_type colon_pos = str.find("::"); | |
379 | string::size_type angle_pos = str.find_first_of("<>"); | |
380 | while (colon_pos != string::npos && | |
381 | (angle_count == 0 || angle_pos != string::npos)) | |
382 | { | |
383 | if (angle_count > 0 || angle_pos < colon_pos) | |
384 | { | |
385 | angle_count += str.at(angle_pos) == '<' ? 1 : -1; | |
386 | colon_pos = str.find("::", angle_pos + 1); | |
387 | angle_pos = str.find_first_of("<>", angle_pos + 1); | |
388 | } | |
389 | else | |
390 | { | |
391 | tokens.push_back(str.substr(pos, colon_pos - pos)); | |
392 | pos = colon_pos + 2; | |
393 | colon_pos = str.find("::", pos); | |
394 | angle_pos = str.find_first_of("<>", pos); | |
395 | } | |
396 | } | |
397 | tokens.push_back(str.substr(pos)); | |
398 | } | |
399 | ||
004f2b9c JL |
400 | // Searches for lines in buf delimited by either \n, \0. Returns a vector |
401 | // containing tuples of the type (start of line, length of line). If data | |
402 | // remains before the end of the buffer, a last line is added. All delimiters | |
403 | // are kept. | |
404 | vector<pair<const char*,int> > | |
405 | split_lines(const char *buf, size_t n) | |
406 | { | |
407 | vector<pair<const char*,int> > lines; | |
408 | const char *eol, *line; | |
409 | line = eol = buf; | |
410 | while ((size_t)(eol-buf) < n) | |
411 | { | |
412 | if (*eol == '\n' || *eol == '\0') | |
413 | { | |
414 | lines.push_back(make_pair(line, eol-line+1)); | |
415 | line = ++eol; | |
416 | } | |
417 | else | |
418 | eol++; | |
419 | } | |
420 | ||
421 | // add any last line | |
422 | if (eol > line) | |
423 | lines.push_back(make_pair(line, eol-line)); | |
424 | ||
425 | return lines; | |
426 | } | |
91699a70 | 427 | |
d0a7f5a9 FCE |
428 | // Resolve an executable name to a canonical full path name, with the |
429 | // same policy as execvp(). A program name not containing a slash | |
430 | // will be searched along the $PATH. | |
431 | ||
0a567f6d JS |
432 | string find_executable(const string& name) |
433 | { | |
434 | const map<string, string> sysenv; | |
435 | return find_executable(name, "", sysenv); | |
436 | } | |
437 | ||
05fb3e0c | 438 | string find_executable(const string& name, const string& sysroot, |
0a567f6d | 439 | const map<string, string>& sysenv, |
05fb3e0c | 440 | const string& env_path) |
1b78aef5 | 441 | { |
d9736de1 | 442 | string retpath; |
1b78aef5 | 443 | |
d9736de1 FCE |
444 | if (name.size() == 0) |
445 | return name; | |
1b78aef5 | 446 | |
d0a7f5a9 FCE |
447 | struct stat st; |
448 | ||
449 | if (name.find('/') != string::npos) // slash in the path already? | |
450 | { | |
05fb3e0c | 451 | retpath = sysroot + name; |
d0a7f5a9 FCE |
452 | } |
453 | else // Nope, search $PATH. | |
1b78aef5 | 454 | { |
0a567f6d | 455 | const char *path; |
05fb3e0c | 456 | if (sysenv.count(env_path) != 0) |
0a567f6d | 457 | path = sysenv.find(env_path)->second.c_str(); |
05fb3e0c WF |
458 | else |
459 | path = getenv(env_path.c_str()); | |
d9736de1 | 460 | if (path) |
1b78aef5 | 461 | { |
d9736de1 FCE |
462 | // Split PATH up. |
463 | vector<string> dirs; | |
464 | tokenize(string(path), dirs, string(":")); | |
30f926f0 | 465 | |
d9736de1 FCE |
466 | // Search the path looking for the first executable of the right name. |
467 | for (vector<string>::iterator i = dirs.begin(); i != dirs.end(); i++) | |
468 | { | |
05fb3e0c | 469 | string fname = sysroot + *i + "/" + name; |
d9736de1 | 470 | const char *f = fname.c_str(); |
30f926f0 | 471 | |
d9736de1 FCE |
472 | // Look for a normal executable file. |
473 | if (access(f, X_OK) == 0 | |
d0a7f5a9 FCE |
474 | && stat(f, &st) == 0 |
475 | && S_ISREG(st.st_mode)) | |
d9736de1 FCE |
476 | { |
477 | retpath = fname; | |
478 | break; | |
479 | } | |
480 | } | |
481 | } | |
1b78aef5 DS |
482 | } |
483 | ||
dff50e09 | 484 | |
d0a7f5a9 FCE |
485 | // Could not find the program on the $PATH. We'll just fall back to |
486 | // the unqualified name, which our caller will probably fail with. | |
487 | if (retpath == "") | |
05fb3e0c | 488 | retpath = sysroot + name; |
d0a7f5a9 | 489 | |
d9736de1 | 490 | // Canonicalize the path name. |
5bca76a8 JS |
491 | string scf = resolve_path(retpath); |
492 | if (!startswith(scf, sysroot)) | |
493 | throw runtime_error(_F("find_executable(): file %s not in sysroot %s", | |
494 | scf.c_str(), sysroot.c_str())); | |
495 | return scf; | |
1b78aef5 | 496 | } |
8c711d30 | 497 | |
d9736de1 | 498 | |
dbe9d133 JL |
499 | bool is_fully_resolved(const string& path, const string& sysroot, |
500 | const map<string, string>& sysenv, | |
501 | const string& env_path) | |
502 | { | |
503 | return !path.empty() | |
504 | && !contains_glob_chars(path) | |
505 | && path.find('/') != string::npos | |
506 | && path == find_executable(path, sysroot, sysenv, env_path); | |
507 | } | |
d9736de1 | 508 | |
8c711d30 MH |
509 | const string cmdstr_quoted(const string& cmd) |
510 | { | |
511 | // original cmd : substr1 | |
512 | // or : substr1'substr2 | |
513 | // or : substr1'substr2'substr3...... | |
514 | // after quoted : | |
515 | // every substr(even it's empty) is quoted by '' | |
516 | // every single-quote(') is quoted by "" | |
517 | // examples: substr1 --> 'substr1' | |
518 | // substr1'substr2 --> 'substr1'"'"'substr2' | |
519 | ||
520 | string quoted_cmd; | |
521 | string quote("'"); | |
522 | string replace("'\"'\"'"); | |
523 | string::size_type pos = 0; | |
524 | ||
525 | quoted_cmd += quote; | |
dff50e09 FCE |
526 | for (string::size_type quote_pos = cmd.find(quote, pos); |
527 | quote_pos != string::npos; | |
8c711d30 MH |
528 | quote_pos = cmd.find(quote, pos)) { |
529 | quoted_cmd += cmd.substr(pos, quote_pos - pos); | |
530 | quoted_cmd += replace; | |
531 | pos = quote_pos + 1; | |
532 | } | |
533 | quoted_cmd += cmd.substr(pos, cmd.length() - pos); | |
534 | quoted_cmd += quote; | |
535 | ||
536 | return quoted_cmd; | |
537 | } | |
538 | ||
a5e8d632 | 539 | |
5eea6ed1 JS |
540 | const string |
541 | cmdstr_join(const vector<string>& cmds) | |
542 | { | |
543 | if (cmds.empty()) | |
b530b5b3 | 544 | throw runtime_error(_("cmdstr_join called with an empty command!")); |
5eea6ed1 JS |
545 | |
546 | stringstream cmd; | |
547 | cmd << cmdstr_quoted(cmds[0]); | |
548 | for (size_t i = 1; i < cmds.size(); ++i) | |
549 | cmd << " " << cmdstr_quoted(cmds[i]); | |
550 | ||
551 | return cmd.str(); | |
552 | } | |
553 | ||
554 | ||
b30895fa JS |
555 | // signal-safe set of pids |
556 | class spawned_pids_t { | |
557 | private: | |
558 | set<pid_t> pids; | |
83ae46dd | 559 | #ifndef SINGLE_THREADED |
26a39006 | 560 | pthread_mutex_t mux_pids; |
83ae46dd | 561 | #endif |
4cc40e82 | 562 | |
b30895fa JS |
563 | public: |
564 | bool contains (pid_t p) | |
565 | { | |
566 | stap_sigmasker masked; | |
26a39006 | 567 | |
83ae46dd | 568 | #ifndef SINGLE_THREADED |
26a39006 | 569 | pthread_mutex_lock(&mux_pids); |
83ae46dd | 570 | #endif |
26a39006 | 571 | bool ret = (pids.count(p)==0) ? true : false; |
83ae46dd | 572 | #ifndef SINGLE_THREADED |
26a39006 | 573 | pthread_mutex_unlock(&mux_pids); |
83ae46dd | 574 | #endif |
26a39006 CM |
575 | |
576 | return ret; | |
b30895fa JS |
577 | } |
578 | bool insert (pid_t p) | |
579 | { | |
580 | stap_sigmasker masked; | |
26a39006 | 581 | |
83ae46dd | 582 | #ifndef SINGLE_THREADED |
26a39006 | 583 | pthread_mutex_lock(&mux_pids); |
83ae46dd | 584 | #endif |
26a39006 | 585 | bool ret = (p > 0) ? pids.insert(p).second : false; |
83ae46dd | 586 | #ifndef SINGLE_THREADED |
26a39006 | 587 | pthread_mutex_unlock(&mux_pids); |
83ae46dd | 588 | #endif |
26a39006 CM |
589 | |
590 | return ret; | |
b30895fa JS |
591 | } |
592 | void erase (pid_t p) | |
593 | { | |
594 | stap_sigmasker masked; | |
26a39006 | 595 | |
83ae46dd | 596 | #ifndef SINGLE_THREADED |
26a39006 | 597 | pthread_mutex_lock(&mux_pids); |
83ae46dd | 598 | #endif |
b30895fa | 599 | pids.erase(p); |
83ae46dd | 600 | #ifndef SINGLE_THREADED |
26a39006 | 601 | pthread_mutex_unlock(&mux_pids); |
83ae46dd | 602 | #endif |
b30895fa JS |
603 | } |
604 | int killall (int sig) | |
605 | { | |
606 | int ret = 0; | |
607 | stap_sigmasker masked; | |
26a39006 | 608 | |
83ae46dd | 609 | #ifndef SINGLE_THREADED |
26a39006 | 610 | pthread_mutex_lock(&mux_pids); |
83ae46dd | 611 | #endif |
a5fa9c25 JS |
612 | for (set<pid_t>::const_iterator it = pids.begin(); |
613 | it != pids.end(); ++it) | |
b30895fa | 614 | ret = kill(*it, sig) ?: ret; |
83ae46dd | 615 | #ifndef SINGLE_THREADED |
26a39006 | 616 | pthread_mutex_unlock(&mux_pids); |
83ae46dd | 617 | #endif |
b30895fa JS |
618 | return ret; |
619 | } | |
26a39006 CM |
620 | spawned_pids_t() |
621 | { | |
83ae46dd | 622 | #ifndef SINGLE_THREADED |
26a39006 | 623 | pthread_mutex_init(&mux_pids, NULL); |
83ae46dd | 624 | #endif |
26a39006 CM |
625 | } |
626 | ~spawned_pids_t() | |
627 | { | |
83ae46dd | 628 | #ifndef SINGLE_THREADED |
26a39006 | 629 | pthread_mutex_destroy (&mux_pids); |
83ae46dd | 630 | #endif |
26a39006 CM |
631 | } |
632 | ||
b30895fa JS |
633 | }; |
634 | static spawned_pids_t spawned_pids; | |
4cc40e82 | 635 | |
13efed2a JL |
636 | /* Returns exit code of pid if terminated nicely, or 128+signal if terminated |
637 | * not nicely, or -1 if waitpid() failed. So if ret >= 0, it's the rc/signal */ | |
01cc94dc JS |
638 | int |
639 | stap_waitpid(int verbose, pid_t pid) | |
daa75206 JS |
640 | { |
641 | int ret, status; | |
b30895fa JS |
642 | if (verbose > 1 && spawned_pids.contains(pid)) |
643 | clog << _F("Spawn waitpid call on unmanaged pid %d", pid) << endl; | |
01cc94dc JS |
644 | ret = waitpid(pid, &status, 0); |
645 | if (ret == pid) | |
daa75206 | 646 | { |
01cc94dc | 647 | spawned_pids.erase(pid); |
daa75206 | 648 | ret = WIFEXITED(status) ? WEXITSTATUS(status) : 128 + WTERMSIG(status); |
483cf56e | 649 | if (verbose > 1) |
b530b5b3 | 650 | clog << _F("Spawn waitpid result (0x%x): %d", status, ret) << endl; |
daa75206 JS |
651 | } |
652 | else | |
653 | { | |
654 | if (verbose > 1) | |
b530b5b3 | 655 | clog << _F("Spawn waitpid error (%d): %s", ret, strerror(errno)) << endl; |
daa75206 JS |
656 | ret = -1; |
657 | } | |
20f90026 | 658 | PROBE2(stap, stap_system__complete, ret, pid); |
daa75206 JS |
659 | return ret; |
660 | } | |
661 | ||
b63fab87 JS |
662 | static int |
663 | pipe_child_fd(posix_spawn_file_actions_t* fa, int pipefd[2], int childfd) | |
664 | { | |
665 | if (pipe(pipefd)) | |
666 | return -1; | |
667 | ||
668 | int dir = childfd ? 1 : 0; | |
669 | if (!fcntl(pipefd[0], F_SETFD, FD_CLOEXEC) && | |
670 | !fcntl(pipefd[1], F_SETFD, FD_CLOEXEC) && | |
671 | !posix_spawn_file_actions_adddup2(fa, pipefd[dir], childfd)) | |
672 | return 0; | |
673 | ||
674 | close(pipefd[0]); | |
675 | close(pipefd[1]); | |
676 | return -1; | |
677 | } | |
678 | ||
ff520ff4 JS |
679 | static int |
680 | null_child_fd(posix_spawn_file_actions_t* fa, int childfd) | |
681 | { | |
682 | int flags = childfd ? O_WRONLY : O_RDONLY; | |
683 | return posix_spawn_file_actions_addopen(fa, childfd, "/dev/null", flags, 0); | |
684 | } | |
685 | ||
4cc40e82 | 686 | // Runs a command with a saved PID, so we can kill it from the signal handler |
aeb9cc10 | 687 | pid_t |
20f90026 | 688 | stap_spawn(int verbose, const vector<string>& args, |
e4e3d6b7 | 689 | posix_spawn_file_actions_t* fa, const vector<string>& envVec) |
4cc40e82 | 690 | { |
5def5d2a LB |
691 | string::const_iterator it; |
692 | it = args[0].begin(); | |
20f90026 | 693 | string command; |
5def5d2a | 694 | if(*it == '/' && (access(args[0].c_str(), X_OK)==-1)) //checking to see if staprun is executable |
2713ea24 CM |
695 | // XXX PR13274 needs-session to use print_warning() |
696 | clog << _F("WARNING: %s is not executable (%s)", args[0].c_str(), strerror(errno)) << endl; | |
20f90026 JS |
697 | for (size_t i = 0; i < args.size(); ++i) |
698 | command += " " + args[i]; | |
1acfc030 | 699 | PROBE1(stap, stap_system__start, command.c_str()); |
db0a43c3 | 700 | if (verbose > 1) |
b530b5b3 | 701 | clog << _("Running") << command << endl; |
36ef6d6a | 702 | |
20f90026 JS |
703 | char const * argv[args.size() + 1]; |
704 | for (size_t i = 0; i < args.size(); ++i) | |
705 | argv[i] = args[i].c_str(); | |
706 | argv[args.size()] = NULL; | |
707 | ||
e4e3d6b7 CM |
708 | char** env; |
709 | bool allocated; | |
8a398d03 DB |
710 | // environ can be NULL. This has been observed when running under gdb. |
711 | if(envVec.empty() && environ != NULL) | |
e4e3d6b7 CM |
712 | { |
713 | env = environ; | |
714 | allocated = false; | |
715 | } | |
716 | else | |
717 | { | |
718 | allocated = true; | |
719 | env = new char*[envVec.size() + 1]; | |
720 | ||
721 | for (size_t i = 0; i < envVec.size(); ++i) | |
722 | env[i] = (char*)envVec[i].c_str(); | |
723 | env[envVec.size()] = NULL; | |
724 | } | |
725 | ||
20f90026 JS |
726 | pid_t pid = 0; |
727 | int ret = posix_spawnp(&pid, argv[0], fa, NULL, | |
e4e3d6b7 CM |
728 | const_cast<char * const *>(argv), env); |
729 | if (allocated) | |
730 | delete[] env; | |
731 | ||
01cc94dc JS |
732 | PROBE2(stap, stap_system__spawn, ret, pid); |
733 | if (ret != 0) | |
36ef6d6a | 734 | { |
db0a43c3 | 735 | if (verbose > 1) |
b530b5b3 | 736 | clog << _F("Spawn error (%d): %s", ret, strerror(ret)) << endl; |
01cc94dc | 737 | pid = -1; |
4cc40e82 | 738 | } |
01cc94dc JS |
739 | else |
740 | spawned_pids.insert(pid); | |
741 | return pid; | |
742 | } | |
743 | ||
b63fab87 JS |
744 | // The API version of stap_spawn doesn't expose file_actions, for now. |
745 | pid_t | |
20f90026 | 746 | stap_spawn(int verbose, const vector<string>& args) |
b63fab87 | 747 | { |
20f90026 | 748 | return stap_spawn(verbose, args, NULL); |
b63fab87 JS |
749 | } |
750 | ||
645383d5 JS |
751 | pid_t |
752 | stap_spawn_piped(int verbose, const vector<string>& args, | |
e96f2257 | 753 | int *child_in, int *child_out, int* child_err) |
645383d5 JS |
754 | { |
755 | pid_t pid = -1; | |
e96f2257 | 756 | int infd[2], outfd[2], errfd[2]; |
645383d5 JS |
757 | posix_spawn_file_actions_t fa; |
758 | if (posix_spawn_file_actions_init(&fa) != 0) | |
759 | return -1; | |
760 | ||
e96f2257 | 761 | if (child_in && pipe_child_fd(&fa, infd, 0) != 0) |
645383d5 | 762 | goto cleanup_fa; |
e96f2257 JS |
763 | if (child_out && pipe_child_fd(&fa, outfd, 1) != 0) |
764 | goto cleanup_in; | |
645383d5 JS |
765 | if (child_err && pipe_child_fd(&fa, errfd, 2) != 0) |
766 | goto cleanup_out; | |
767 | ||
768 | pid = stap_spawn(verbose, args, &fa); | |
769 | ||
770 | if (child_err) | |
771 | { | |
772 | if (pid > 0) | |
773 | *child_err = errfd[0]; | |
774 | else | |
775 | close(errfd[0]); | |
776 | close(errfd[1]); | |
777 | } | |
778 | ||
779 | cleanup_out: | |
780 | if (child_out) | |
781 | { | |
782 | if (pid > 0) | |
783 | *child_out = outfd[0]; | |
784 | else | |
785 | close(outfd[0]); | |
786 | close(outfd[1]); | |
787 | } | |
788 | ||
e96f2257 JS |
789 | cleanup_in: |
790 | if (child_in) | |
791 | { | |
792 | if (pid > 0) | |
793 | *child_in = infd[1]; | |
794 | else | |
795 | close(infd[1]); | |
796 | close(infd[0]); | |
797 | } | |
798 | ||
645383d5 JS |
799 | cleanup_fa: |
800 | posix_spawn_file_actions_destroy(&fa); | |
801 | ||
802 | return pid; | |
803 | } | |
804 | ||
e4e3d6b7 CM |
805 | // Global set of supported localization variables. Make changes here to |
806 | // add or remove variables. List of variables from: | |
807 | // http://publib.boulder.ibm.com/infocenter/tivihelp/v8r1/index.jsp?topic=/ | |
808 | // com.ibm.netcool_OMNIbus.doc_7.3.0/omnibus/wip/install/concept/omn_con_settingyourlocale.html | |
809 | const set<string>& | |
810 | localization_variables() | |
811 | { | |
812 | static set<string> localeVars; | |
813 | if (localeVars.empty()) | |
814 | { | |
815 | localeVars.insert("LANG"); | |
816 | localeVars.insert("LC_ALL"); | |
817 | localeVars.insert("LC_CTYPE"); | |
818 | localeVars.insert("LC_COLLATE"); | |
819 | localeVars.insert("LC_MESSAGES"); | |
820 | localeVars.insert("LC_TIME"); | |
821 | localeVars.insert("LC_MONETARY"); | |
822 | localeVars.insert("LC_NUMERIC"); | |
823 | } | |
824 | return localeVars; | |
825 | } | |
826 | ||
01cc94dc JS |
827 | // Runs a command with a saved PID, so we can kill it from the signal handler, |
828 | // and wait for it to finish. | |
829 | int | |
b13c6a37 JS |
830 | stap_system(int verbose, const string& description, |
831 | const vector<string>& args, | |
ff520ff4 | 832 | bool null_out, bool null_err) |
01cc94dc | 833 | { |
ff520ff4 JS |
834 | int ret = 0; |
835 | posix_spawn_file_actions_t fa; | |
836 | if (posix_spawn_file_actions_init(&fa) != 0) | |
837 | return -1; | |
4cc40e82 | 838 | |
ff520ff4 JS |
839 | if ((null_out && null_child_fd(&fa, 1) != 0) || |
840 | (null_err && null_child_fd(&fa, 2) != 0)) | |
841 | ret = -1; | |
842 | else | |
843 | { | |
844 | pid_t pid = stap_spawn(verbose, args, &fa); | |
845 | ret = pid; | |
5def5d2a | 846 | if (pid > 0){ |
ff520ff4 | 847 | ret = stap_waitpid(verbose, pid); |
df324c9f JS |
848 | |
849 | // XXX PR13274 needs-session to use print_warning() | |
850 | if (ret > 128) | |
851 | clog << _F("WARNING: %s exited with signal: %d (%s)", | |
852 | description.c_str(), ret - 128, strsignal(ret - 128)) << endl; | |
853 | else if (ret > 0) | |
854 | clog << _F("WARNING: %s exited with status: %d", | |
855 | description.c_str(), ret) << endl; | |
5def5d2a | 856 | } |
ff520ff4 JS |
857 | } |
858 | ||
859 | posix_spawn_file_actions_destroy(&fa); | |
860 | return ret; | |
20f90026 JS |
861 | } |
862 | ||
daa75206 JS |
863 | // Like stap_system, but capture stdout |
864 | int | |
20f90026 | 865 | stap_system_read(int verbose, const vector<string>& args, ostream& out) |
daa75206 | 866 | { |
645383d5 | 867 | int child_fd = -1; |
e96f2257 | 868 | pid_t child = stap_spawn_piped(verbose, args, NULL, &child_fd); |
645383d5 | 869 | if (child > 0) |
daa75206 | 870 | { |
645383d5 JS |
871 | // read everything from the child |
872 | stdio_filebuf<char> in(child_fd, ios_base::in); | |
873 | out << ∈ | |
874 | return stap_waitpid(verbose, child); | |
daa75206 | 875 | } |
645383d5 | 876 | return -1; |
daa75206 JS |
877 | } |
878 | ||
879 | ||
01cc94dc | 880 | // Send a signal to our spawned commands |
4cc40e82 JS |
881 | int |
882 | kill_stap_spawn(int sig) | |
883 | { | |
b30895fa | 884 | return spawned_pids.killall(sig); |
4cc40e82 JS |
885 | } |
886 | ||
c0d1b5a0 | 887 | |
8aabf152 | 888 | |
c0d1b5a0 FCE |
889 | void assert_regexp_match (const string& name, const string& value, const string& re) |
890 | { | |
891 | typedef map<string,regex_t*> cache; | |
892 | static cache compiled; | |
893 | cache::iterator it = compiled.find (re); | |
894 | regex_t* r = 0; | |
895 | if (it == compiled.end()) | |
896 | { | |
897 | r = new regex_t; | |
898 | int rc = regcomp (r, re.c_str(), REG_ICASE|REG_NOSUB|REG_EXTENDED); | |
1d7ae21b | 899 | assert (rc == 0); |
c0d1b5a0 FCE |
900 | compiled[re] = r; |
901 | } | |
902 | else | |
903 | r = it->second; | |
904 | ||
905 | // run regexec | |
906 | int rc = regexec (r, value.c_str(), 0, 0, 0); | |
907 | if (rc) | |
e2d0f787 JS |
908 | throw runtime_error |
909 | (_F("ERROR: Safety pattern mismatch for %s ('%s' vs. '%s') rc=%d", | |
910 | name.c_str(), value.c_str(), re.c_str(), rc)); | |
c0d1b5a0 FCE |
911 | } |
912 | ||
913 | ||
8aabf152 FCE |
914 | int regexp_match (const string& value, const string& re, vector<string>& matches) |
915 | { | |
916 | typedef map<string,regex_t*> cache; // separate cache because we use different regcomp options | |
917 | static cache compiled; | |
918 | cache::iterator it = compiled.find (re); | |
919 | regex_t* r = 0; | |
920 | if (it == compiled.end()) | |
921 | { | |
922 | r = new regex_t; | |
923 | int rc = regcomp (r, re.c_str(), REG_EXTENDED); /* REG_ICASE? */ | |
1d7ae21b | 924 | assert (rc == 0); |
8aabf152 FCE |
925 | compiled[re] = r; |
926 | } | |
927 | else | |
928 | r = it->second; | |
929 | ||
930 | ||
931 | // run regexec | |
932 | #define maxmatches 10 | |
933 | regmatch_t rm[maxmatches]; | |
934 | ||
935 | int rc = regexec (r, value.c_str(), maxmatches, rm, 0); | |
936 | if (rc) return rc; | |
937 | ||
938 | matches.erase(matches.begin(), matches.end()); | |
939 | for (unsigned i=0; i<maxmatches; i++) // XXX: ideally, the number of actual subexpressions in re | |
940 | { | |
941 | if (rm[i].rm_so >= 0) | |
942 | matches.push_back(value.substr (rm[i].rm_so, rm[i].rm_eo-rm[i].rm_so)); | |
943 | else | |
944 | matches.push_back(""); | |
945 | } | |
946 | ||
947 | return 0; | |
948 | } | |
949 | ||
950 | ||
37001baa FCE |
951 | bool contains_glob_chars (const string& str) |
952 | { | |
5750ecc6 FCE |
953 | for (unsigned i=0; i<str.size(); i++) |
954 | { | |
955 | char this_char = str[i]; | |
956 | if (this_char == '\\' && (str.size() > i+1)) | |
957 | { | |
958 | // PR13338: skip the escape backslash and the escaped character | |
959 | i++; | |
960 | continue; | |
961 | } | |
962 | if (this_char == '*' || this_char == '?' || this_char == '[') | |
963 | return true; | |
964 | } | |
965 | ||
966 | return false; | |
967 | } | |
968 | ||
969 | ||
970 | // PR13338: we need these functions to be able to pass through glob metacharacters | |
971 | // through the recursive process("...*...") expansion process. | |
972 | string escape_glob_chars (const string& str) | |
973 | { | |
974 | string op; | |
975 | for (unsigned i=0; i<str.size(); i++) | |
976 | { | |
977 | char this_char = str[i]; | |
978 | if (this_char == '*' || this_char == '?' || this_char == '[') | |
979 | op += '\\'; | |
980 | op += this_char; | |
981 | } | |
982 | return op; | |
37001baa FCE |
983 | } |
984 | ||
5750ecc6 FCE |
985 | string unescape_glob_chars (const string& str) |
986 | { | |
987 | string op; | |
988 | for (unsigned i=0; i<str.size(); i++) | |
989 | { | |
990 | char this_char = str[i]; | |
991 | if (this_char == '\\' && (str.size() > i+1) ) | |
992 | { | |
993 | op += str[i+1]; | |
994 | i++; | |
995 | continue; | |
996 | } | |
997 | op += this_char; | |
998 | } | |
999 | ||
1000 | return op; | |
1001 | } | |
1002 | ||
1003 | ||
1004 | ||
daa75206 JS |
1005 | string |
1006 | normalize_machine(const string& machine) | |
1007 | { | |
1008 | // PR4186: Copy logic from coreutils uname (uname -i) to squash | |
1009 | // i?86->i386. Actually, copy logic from linux top-level Makefile | |
1010 | // to squash uname -m -> $(SUBARCH). | |
1011 | // | |
1012 | // This logic needs to match the logic in the stap_get_arch shell | |
1013 | // function in stap-env. | |
4c25c410 FCE |
1014 | // |
1015 | // But: RHBZ669082 reminds us that this renaming post-dates some | |
1016 | // of the kernel versions we know and love. So in buildrun.cxx | |
1017 | // we undo this renaming for ancient powerpc. | |
daa75206 JS |
1018 | |
1019 | if (machine == "i486") return "i386"; | |
1020 | else if (machine == "i586") return "i386"; | |
1021 | else if (machine == "i686") return "i386"; | |
1022 | else if (machine == "sun4u") return "sparc64"; | |
1023 | else if (machine.substr(0,3) == "arm") return "arm"; | |
1024 | else if (machine == "sa110") return "arm"; | |
1025 | else if (machine == "s390x") return "s390"; | |
7ddd76f3 | 1026 | else if (machine == "aarch64") return "arm64"; |
daa75206 JS |
1027 | else if (machine.substr(0,3) == "ppc") return "powerpc"; |
1028 | else if (machine.substr(0,4) == "mips") return "mips"; | |
1029 | else if (machine.substr(0,3) == "sh2") return "sh"; | |
1030 | else if (machine.substr(0,3) == "sh3") return "sh"; | |
1031 | else if (machine.substr(0,3) == "sh4") return "sh"; | |
1032 | return machine; | |
1033 | } | |
1034 | ||
081b45d1 MW |
1035 | int |
1036 | elf_class_from_normalized_machine (const string &machine) | |
1037 | { | |
1038 | // Must match kernel machine architectures as used un tapset directory. | |
1039 | // And must match normalization done in normalize_machine (). | |
1040 | if (machine == "i386" | |
1041 | || machine == "arm") // arm assumes 32-bit | |
1042 | return ELFCLASS32; | |
1043 | else if (machine == "s390" // powerpc and s390 always assume 64-bit, | |
1044 | || machine == "powerpc" // see normalize_machine (). | |
1045 | || machine == "x86_64" | |
7ddd76f3 WC |
1046 | || machine == "ia64" |
1047 | || machine == "arm64") | |
081b45d1 MW |
1048 | return ELFCLASS64; |
1049 | ||
1050 | cerr << _F("Unknown kernel machine architecture '%s', don't know elf class", | |
1051 | machine.c_str()) << endl; | |
1052 | return -1; | |
1053 | } | |
1054 | ||
aeb9cc10 DB |
1055 | string |
1056 | kernel_release_from_build_tree (const string &kernel_build_tree, int verbose) | |
1057 | { | |
1058 | string version_file_name = kernel_build_tree + "/include/config/kernel.release"; | |
1059 | // The file include/config/kernel.release within the | |
1060 | // build tree is used to pull out the version information | |
1061 | ifstream version_file (version_file_name.c_str()); | |
1062 | if (version_file.fail ()) | |
1063 | { | |
1064 | if (verbose > 1) | |
1065 | //TRANSLATORS: Missing a file | |
1066 | cerr << _F("Missing %s", version_file_name.c_str()) << endl; | |
1067 | return ""; | |
1068 | } | |
1069 | ||
1070 | string kernel_release; | |
1071 | char c; | |
1072 | while (version_file.get(c) && c != '\n') | |
1073 | kernel_release.push_back(c); | |
1074 | ||
1075 | return kernel_release; | |
1076 | } | |
8aabf152 | 1077 | |
cde0f3ce | 1078 | string autosprintf(const char* format, ...) |
7a1513b1 FCE |
1079 | { |
1080 | va_list args; | |
1081 | char *str; | |
1082 | va_start (args, format); | |
1083 | int rc = vasprintf (&str, format, args); | |
1084 | if (rc < 0) | |
33631d57 CM |
1085 | { |
1086 | va_end(args); | |
1087 | return _F("autosprintf/vasprintf error %d", rc); | |
1088 | } | |
7a1513b1 FCE |
1089 | string s = str; |
1090 | va_end (args); | |
1091 | free (str); | |
1092 | return s; /* by copy */ | |
1093 | } | |
1094 | ||
cde0f3ce | 1095 | string |
28946fe7 MW |
1096 | get_self_path() |
1097 | { | |
1098 | char buf[1024]; // This really should be enough for anybody... | |
1099 | const char *file = "/proc/self/exe"; | |
1100 | ssize_t len = readlink(file, buf, sizeof(buf) - 1); | |
1101 | if (len > 0) | |
1102 | { | |
1103 | buf[len] = '\0'; | |
1104 | file = buf; | |
1105 | } | |
1106 | // otherwise the path is ridiculously large, fall back to /proc/self/exe. | |
1107 | // | |
cde0f3ce | 1108 | return string(file); |
28946fe7 MW |
1109 | } |
1110 | ||
30b865ce | 1111 | bool |
a03a3744 | 1112 | is_valid_pid (pid_t pid, string& err_msg) |
30b865ce AJ |
1113 | { |
1114 | err_msg = ""; | |
b6eb07fd | 1115 | if (pid <= 0) |
30b865ce | 1116 | { |
b6eb07fd AJ |
1117 | err_msg = _F("cannot probe pid %d: Invalid pid", pid); |
1118 | return false; | |
1119 | } | |
1120 | else if (kill(pid, 0) == -1) | |
1121 | { | |
1122 | err_msg = _F("cannot probe pid %d: %s", pid, strerror(errno)); | |
30b865ce AJ |
1123 | return false; |
1124 | } | |
1125 | return true; | |
1126 | } | |
1127 | ||
cde0f3ce JL |
1128 | // String sorter using the Levenshtein algorithm |
1129 | // TODO: Performance may be improved by adding a maximum distance | |
1130 | // parameter which would abort the operation if we know the final | |
1131 | // distance will be larger than the maximum. This may entail maintaining | |
1132 | // another data structure, and thus the cost might outweigh the benefit | |
593f09eb JL |
1133 | unsigned |
1134 | levenshtein(const string& a, const string& b) | |
cde0f3ce JL |
1135 | { |
1136 | Array2D<unsigned> d(a.size()+1, b.size()+1); | |
1137 | ||
1138 | // border values | |
1139 | for (unsigned i = 0; i < d.width; i++) | |
1140 | d(i, 0) = i; | |
1141 | for (unsigned j = 0; j < d.height; j++) | |
1142 | d(0, j) = j; | |
1143 | ||
1144 | // the meat | |
1145 | for (unsigned i = 1; i < d.width; i++) { | |
1146 | for (unsigned j = 1; j < d.height; j++) { | |
a88f4aec | 1147 | if (a[i-1] == b[j-1]) // match |
cde0f3ce JL |
1148 | d(i,j) = d(i-1, j-1); |
1149 | else // penalties open for adjustments | |
75d96dc7 JL |
1150 | { |
1151 | unsigned subpen = 2; // substitution penalty | |
1152 | // check if they are upper/lowercase related | |
1153 | if (tolower(a[i-1]) == tolower(b[j-1])) | |
1154 | subpen = 1; // half penalty | |
1155 | d(i,j) = min(min( | |
1156 | d(i-1,j-1) + subpen, // substitution | |
1157 | d(i-1,j) + 2), // deletion | |
1158 | d(i,j-1) + 2); // insertion | |
1159 | } | |
cde0f3ce JL |
1160 | } |
1161 | } | |
1162 | ||
1163 | return d(d.width-1, d.height-1); | |
1164 | } | |
3f95ed01 | 1165 | |
593f09eb JL |
1166 | // Returns comma-separated list of set elements closest to the target string. |
1167 | // Print a maximum amount of 'max' elements, with a maximum levenshtein score | |
1168 | // of 'threshold'. | |
1169 | string | |
1170 | levenshtein_suggest(const string& target, // string to match against | |
1171 | const set<string>& elems, // elements to suggest from | |
1172 | unsigned max, // max elements to print | |
1173 | unsigned threshold) // max leven score to print | |
1174 | { | |
1175 | // calculate leven score for each elem and put in map | |
1176 | multimap<unsigned, string> scores; | |
1177 | for (set<string>::const_iterator it = elems.begin(); | |
1178 | it != elems.end(); ++it) | |
1179 | { | |
1062ce54 JL |
1180 | if (it->empty()) // skip empty strings |
1181 | continue; | |
1182 | ||
59b11ead JL |
1183 | // Approximate levenshtein by size-difference only; real score |
1184 | // is at least this high | |
1185 | unsigned min_score = labs(target.size() - it->size()); | |
1186 | ||
1187 | if (min_score > threshold) // min-score too high for threshold | |
1188 | continue; | |
1189 | ||
1190 | /* Check if we can skip calculating the score for this element. This works | |
1191 | * on knowing two facts: | |
1192 | * (1) We will only print the 'max' number of the top elements. The | |
1193 | * current top 'max' candidates reside in the scores map already. | |
1194 | * (2) The score will be AT LEAST the difference between the lengths of | |
1195 | * the two strings. | |
1196 | * So what we do is retrieve the WORST score of the current best | |
1197 | * candidates by iterating through the map (which is ordered) and | |
1198 | * retrieving the 'max-th' item and check if that's still better than the | |
1199 | * BEST score we could possibly get from the two strings (by comparing | |
1200 | * their lengths). If the 'max-th' item is indeed better, then we know | |
1201 | * this element will NEVER make it to the terminal. So we just skip it and | |
1202 | * move on. Quite tragic if you ask me... | |
1203 | */ | |
1204 | unsigned maxth_score = std::numeric_limits<unsigned>::max(); | |
1205 | if (scores.size() >= max) // do we have at least 'max' items? | |
1206 | { | |
1207 | // retrieve 'max-th' item | |
1208 | multimap<unsigned, string>::iterator itt = scores.begin(); | |
1209 | for (unsigned i = 0; i < max-1; i++) itt++; // will not go to .end() | |
1210 | maxth_score = itt->first; | |
1211 | } | |
1212 | ||
1213 | if (min_score > maxth_score) // min-score too high for known candidates | |
1214 | continue; | |
1215 | ||
593f09eb | 1216 | unsigned score = levenshtein(target, *it); |
59b11ead JL |
1217 | |
1218 | if (score > maxth_score) // actual score too high for known candidates | |
1219 | continue; | |
1220 | ||
1221 | if (score > threshold) // actual score too high for threshold | |
1222 | continue; | |
1223 | ||
1224 | // a candidate! | |
1225 | scores.insert(make_pair(score, *it)); | |
593f09eb JL |
1226 | } |
1227 | ||
1228 | string suggestions; | |
1229 | ||
1230 | // Print out the top 'max' elements | |
1231 | multimap<unsigned, string>::iterator it = scores.begin(); | |
1232 | for (unsigned i = 0; it != scores.end() && i < max; ++it, i++) | |
1233 | suggestions += it->second + ", "; | |
1234 | if (!suggestions.empty()) | |
1235 | suggestions.erase(suggestions.size()-2); | |
1236 | ||
1237 | return suggestions; | |
1238 | } | |
1239 | ||
3f95ed01 JS |
1240 | #ifndef HAVE_PPOLL |
1241 | // This is a poor-man's ppoll, only used carefully by readers that need to be | |
1242 | // interruptible, like remote::run and mutator::run. It does not provide the | |
1243 | // same guarantee of atomicity as on systems with a true ppoll. | |
1244 | // | |
1245 | // In our use, this would cause trouble if a signal came in any time from the | |
1246 | // moment we mask signals to prepare pollfds, to the moment we call poll in | |
1247 | // emulation here. If there's no data on any of the pollfds, we will be stuck | |
1248 | // waiting indefinitely. | |
1249 | // | |
1250 | // Since this is mainly about responsiveness of CTRL-C cleanup, we'll just | |
1251 | // throw in a one-second forced timeout to ensure we have a chance to notice | |
1252 | // there was an interrupt without too much delay. | |
1253 | int | |
1254 | ppoll(struct pollfd *fds, nfds_t nfds, | |
1255 | const struct timespec *timeout_ts, | |
1256 | const sigset_t *sigmask) | |
1257 | { | |
1258 | sigset_t origmask; | |
1259 | int timeout = (timeout_ts == NULL) ? 1000 // don't block forever... | |
1260 | : (timeout_ts->tv_sec * 1000 + timeout_ts->tv_nsec / 1000000); | |
1261 | sigprocmask(SIG_SETMASK, sigmask, &origmask); | |
1262 | int rc = poll(fds, nfds, timeout); | |
1263 | sigprocmask(SIG_SETMASK, &origmask, NULL); | |
1264 | return rc; | |
1265 | } | |
1266 | #endif | |
1267 | ||
1268 | ||
73267b89 | 1269 | /* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */ |