llvm.org GIT mirror llvm / 81e35ff
Support: Don't remove special files on signals. - Similar to Path::eraseFromDisk(), we don't want LLVM to remove things like /dev/null, even if it has the permission. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@166105 91177308-0d34-0410-b5e6-96231b3b80d8 Daniel Dunbar 6 years ago
1 changed file(s) with 21 addition(s) and 9 deletion(s). Raw diff Collapse all Expand all
120120 /// NB: This must be an async signal safe function. It cannot allocate or free
121121 /// memory, even in debug builds.
122122 static void RemoveFilesToRemove() {
123 // Note: avoid iterators in case of debug iterators that allocate or release
123 // We avoid iterators in case of debug iterators that allocate or release
124124 // memory.
125125 for (unsigned i = 0, e = FilesToRemove.size(); i != e; ++i) {
126 // Note that we don't want to use any external code here, and we don't care
127 // about errors. We're going to try as hard as we can as often as we need
128 // to to make these files go away. If these aren't files, too bad.
129 //
130 // We do however rely on a std::string implementation for which repeated
131 // calls to 'c_str()' don't allocate memory. We pre-call 'c_str()' on all
132 // of these strings to try to ensure this is safe.
133 unlink(FilesToRemove[i].c_str());
126 // We rely on a std::string implementation for which repeated calls to
127 // 'c_str()' don't allocate memory. We pre-call 'c_str()' on all of these
128 // strings to try to ensure this is safe.
129 const char *path = FilesToRemove[i].c_str();
130
131 // Get the status so we can determine if it's a file or directory. If we
132 // can't stat the file, ignore it.
133 struct stat buf;
134 if (stat(path, &buf) != 0)
135 continue;
136
137 // If this is not a regular file, ignore it. We want to prevent removal of
138 // special files like /dev/null, even if the compiler is being run with the
139 // super-user permissions.
140 if (!S_ISREG(buf.st_mode))
141 continue;
142
143 // Otherwise, remove the file. We ignore any errors here as there is nothing
144 // else we can do.
145 unlink(path);
134146 }
135147 }
136148