Skip to content

Commit 468bc50

Browse files
committed
Refactor: libpacemaker: Filter out non-regular profile files
profile_filter() previously did not check whether the file was a regular file, because it didn't have access to the profiling directory path. There is no user data argument. However, we can hack around this with a global variable. This way, we don't end up with a "Timings" section of the output if we're ultimately going to ignore all the files that passed the scandir() filter. glibc doesn't enforce PATH_MAX, so we shouldn't assume a path is less than that. The overhead of crm_strdup_printf() calls is negligible compared to profiling overhead and does not count toward profiling time. https://www.gnu.org/software/libc/manual/html_node/Limits-for-Files.html Signed-off-by: Reid Wahl <[email protected]>
1 parent e49ec15 commit 468bc50

File tree

1 file changed

+30
-17
lines changed

1 file changed

+30
-17
lines changed

lib/pacemaker/pcmk_simulate.c

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
#include "libpacemaker_private.h"
2424

25+
static const char *profiling_dir = NULL;
2526
static pcmk__output_t *out = NULL;
2627
static cib_t *fake_cib = NULL;
2728
static GList *fake_resource_list = NULL;
@@ -322,27 +323,42 @@ write_sim_dotfile(pcmk_scheduler_t *scheduler, const char *dot_file,
322323
*
323324
* \param[in] entry Directory entry
324325
*
325-
* \retval 0 if the filename begins with '.' or does not end in ".xml"
326-
* \retval 1 otherwise
326+
* \retval 1 if the filename ends with ".xml", does not begin with ".", and
327+
* refers to a regular file
328+
* \retval 0 otherwise
327329
*/
328330
static int
329331
profile_filter(const struct dirent *entry)
330332
{
331333
const char *filename = entry->d_name;
334+
char *buf = NULL;
335+
struct stat sb;
336+
int rc = 0;
332337

333338
if (pcmk__str_any_of(filename, ".", "..", NULL)) {
334339
// Skip current (".") and parent ("..") directory links
335-
return 0;
340+
goto done;
336341
}
337342
if (filename[0] == '.') {
338343
crm_trace("Not profiling hidden file '%s'", filename);
339-
return 0;
344+
goto done;
340345
}
341346
if (!pcmk__ends_with_ext(filename, ".xml")) {
342347
crm_trace("Not profiling file '%s' without '.xml' extension", filename);
343-
return 0;
348+
goto done;
344349
}
345-
return 1;
350+
351+
buf = crm_strdup_printf("%s/%s", profiling_dir, filename);
352+
if ((stat(buf, &sb) != 0) || !S_ISREG(sb.st_mode)) {
353+
crm_trace("Not profiling file '%s': not a regular file", filename);
354+
goto done;
355+
}
356+
357+
rc = 1;
358+
359+
done:
360+
free(buf);
361+
return rc;
346362
}
347363

348364
/*!
@@ -431,7 +447,11 @@ pcmk__profile_dir(pcmk__output_t *out, uint32_t flags, const char *dir,
431447
scheduler_flags |= pcmk__sched_show_utilization;
432448
}
433449

450+
// Hack to pass user data to profile_filter
451+
profiling_dir = dir;
434452
num_files = scandir(dir, &namelist, profile_filter, alphasort);
453+
profiling_dir = NULL;
454+
435455
if (num_files < 0) {
436456
rc = errno;
437457
goto done;
@@ -443,18 +463,11 @@ pcmk__profile_dir(pcmk__output_t *out, uint32_t flags, const char *dir,
443463
out->begin_list(out, NULL, NULL, "Timings");
444464

445465
for (int i = 0; i < num_files; i++) {
446-
const char *filename = namelist[i]->d_name;
447-
char buffer[FILENAME_MAX];
448-
struct stat prop;
466+
// glibc doesn't enforce PATH_MAX, so don't limit the buffer size
467+
char *path = crm_strdup_printf("%s/%s", dir, namelist[i]->d_name);
449468

450-
// Check for regular file here because profile_filter() doesn't have dir
451-
snprintf(buffer, sizeof(buffer), "%s/%s", dir, filename);
452-
453-
if ((stat(buffer, &prop) == 0) && S_ISREG(prop.st_mode)) {
454-
profile_file(buffer, repeat, scheduler, scheduler_flags, use_date);
455-
} else {
456-
crm_trace("Not profiling file '%s': not a regular file", filename);
457-
}
469+
profile_file(path, repeat, scheduler, scheduler_flags, use_date);
470+
free(path);
458471
free(namelist[i]);
459472
}
460473
out->end_list(out);

0 commit comments

Comments
 (0)