@@ -18,7 +18,12 @@ constexpr auto c_shareDocsDir = "/usr/share/doc";
1818constexpr auto c_shareDocsMount = SHARE_PATH " /doc" ;
1919constexpr auto c_x11RuntimeDir = SHARE_PATH " /.X11-unix" ;
2020constexpr auto c_xdgRuntimeDir = SHARE_PATH " /runtime-dir" ;
21- constexpr auto c_stdErrLogMount = SHARE_PATH " /stderr.log" ;
21+ constexpr auto c_stdErrLogFile = SHARE_PATH " /stderr.log" ;
22+
23+ constexpr auto c_coreDir = SHARE_PATH " /dumps" ;
24+ constexpr auto c_corePatternDefault = " core.%e" ;
25+ constexpr auto c_corePatternFile = " /proc/sys/kernel/core_pattern" ;
26+ constexpr auto c_corePatternEnv = " WSL2_WSLG_CORE_PATTERN" ;
2227
2328constexpr auto c_sharedMemoryMountPoint = " /mnt/shared_memory" ;
2429constexpr auto c_sharedMemoryMountPointEnv = " WSL2_SHARED_MEMORY_MOUNT_POINT" ;
7984
8085 // Open a file for logging errors and set it to stderr for WSLGd as well as any child process.
8186 {
82- wil::unique_fd fd_stdErrLog (open (c_stdErrLogMount , (O_RDWR | O_CREAT), (S_IRUSR | S_IRGRP | S_IROTH)));
83- if (fd_stdErrLog && (fd_stdErrLog .get () != STDERR_FILENO)) {
84- dup2 (fd_stdErrLog .get (), STDERR_FILENO);
87+ wil::unique_fd stdErrLogFd (open (c_stdErrLogFile , (O_RDWR | O_CREAT), (S_IRUSR | S_IRGRP | S_IROTH)));
88+ if (stdErrLogFd && (stdErrLogFd .get () != STDERR_FILENO)) {
89+ dup2 (stdErrLogFd .get (), STDERR_FILENO);
8590 }
8691 }
8792
@@ -186,6 +191,34 @@ try {
186191
187192 SetupOptionalEnv ();
188193
194+ // "ulimits -c unlimited" for core dumps.
195+ struct rlimit limit;
196+ limit.rlim_cur = RLIM_INFINITY;
197+ limit.rlim_max = RLIM_INFINITY;
198+ THROW_LAST_ERROR_IF (setrlimit (RLIMIT_CORE, &limit) < 0 );
199+
200+ // create folder to store core files.
201+ std::filesystem::create_directories (c_coreDir);
202+ THROW_LAST_ERROR_IF (chmod (c_coreDir, 0777 ) < 0 );
203+
204+ // update core_pattern.
205+ {
206+ wil::unique_file corePatternFile (fopen (c_corePatternFile, " w" ));
207+ if (corePatternFile.get ()) {
208+ // combine folder path and core pattern.
209+ std::string corePatternFullPath (c_coreDir);
210+ corePatternFullPath += " /" ;
211+ auto corePattern = getenv (c_corePatternEnv);
212+ if (corePattern) {
213+ corePatternFullPath += corePattern;
214+ } else {
215+ corePatternFullPath += c_corePatternDefault; // set to default core_pattern.
216+ }
217+ // write to core_pattern file.
218+ THROW_LAST_ERROR_IF (fprintf (corePatternFile.get (), " %s" , corePatternFullPath.c_str ()) < 0 );
219+ }
220+ }
221+
189222 // Set shared memory mount point to env when available.
190223 if (!is_shared_memory_mounted ||
191224 (setenv (c_sharedMemoryMountPointEnv, c_sharedMemoryMountPoint, true ) < 0 )) {
0 commit comments