@@ -1025,21 +1025,19 @@ bool internal_sigismember(__sanitizer_sigset_t *set, int signum) {
1025
1025
1026
1026
# if !SANITIZER_NETBSD
1027
1027
// ThreadLister implementation.
1028
- ThreadLister::ThreadLister (pid_t pid) : pid_ (pid), buffer_ (4096 ) {
1029
- char task_directory_path[80 ];
1030
- internal_snprintf (task_directory_path, sizeof (task_directory_path),
1031
- " /proc/%d/task/" , pid);
1032
- descriptor_ = internal_open (task_directory_path, O_RDONLY | O_DIRECTORY);
1033
- if (internal_iserror (descriptor_)) {
1034
- Report (" Can't open /proc/%d/task for reading.\n " , pid);
1035
- }
1028
+ ThreadLister::ThreadLister (pid_t pid) : buffer_ (4096 ) {
1029
+ task_path_.AppendF (" /proc/%d/task" , pid);
1030
+ status_path_.AppendF (" %s/status" , task_path_.data ());
1036
1031
}
1037
1032
1038
1033
ThreadLister::Result ThreadLister::ListThreads (
1039
1034
InternalMmapVector<tid_t > *threads) {
1040
- if (internal_iserror (descriptor_))
1035
+ int descriptor = internal_open (task_path_.data (), O_RDONLY | O_DIRECTORY);
1036
+ if (internal_iserror (descriptor)) {
1037
+ Report (" Can't open %s for reading.\n " , task_path_.data ());
1041
1038
return Error;
1042
- internal_lseek (descriptor_, 0 , SEEK_SET);
1039
+ }
1040
+ auto acts_cleanup = at_scope_exit ([&] { internal_close (descriptor); });
1043
1041
threads->clear ();
1044
1042
1045
1043
Result result = Ok;
@@ -1048,11 +1046,11 @@ ThreadLister::Result ThreadLister::ListThreads(
1048
1046
buffer_.resize (buffer_.capacity ());
1049
1047
CHECK_GE (buffer_.size (), 4096 );
1050
1048
uptr read = internal_getdents (
1051
- descriptor_ , (struct linux_dirent *)buffer_.data (), buffer_.size ());
1049
+ descriptor , (struct linux_dirent *)buffer_.data (), buffer_.size ());
1052
1050
if (!read)
1053
1051
return result;
1054
1052
if (internal_iserror (read)) {
1055
- Report (" Can't read directory entries from /proc/%d/task .\n " , pid_ );
1053
+ Report (" Can't read directory entries from %s .\n " , task_path_. data () );
1056
1054
return Error;
1057
1055
}
1058
1056
@@ -1093,9 +1091,7 @@ ThreadLister::Result ThreadLister::ListThreads(
1093
1091
bool ThreadLister::IsAlive (int tid) {
1094
1092
// /proc/%d/task/%d/status uses same call to detect alive threads as
1095
1093
// proc_task_readdir. See task_state implementation in Linux.
1096
- char path[80 ];
1097
- internal_snprintf (path, sizeof (path), " /proc/%d/task/%d/status" , pid_, tid);
1098
- if (!ReadFileToVector (path, &buffer_) || buffer_.empty ())
1094
+ if (!ReadFileToVector (status_path_.data (), &buffer_) || buffer_.empty ())
1099
1095
return false ;
1100
1096
buffer_.push_back (0 );
1101
1097
static const char kPrefix [] = " \n PPid:" ;
@@ -1106,10 +1102,6 @@ bool ThreadLister::IsAlive(int tid) {
1106
1102
return (int )internal_atoll (field) != 0 ;
1107
1103
}
1108
1104
1109
- ThreadLister::~ThreadLister () {
1110
- if (!internal_iserror (descriptor_))
1111
- internal_close (descriptor_);
1112
- }
1113
1105
# endif
1114
1106
1115
1107
# if SANITIZER_WORDSIZE == 32
0 commit comments