d8c292ef01f2b991ebb1f9b6045dc947c99e60e1
[openafs.git] / src / packaging / Debian / patches / find-core-files
1 --- openafs.orig/src/bozo/bnode.c
2 +++ openafs/src/bozo/bnode.c
3 @@ -28,6 +28,7 @@
4  #include <time.h>
5  #endif
6  #include <sys/stat.h>
7 +#include <dirent.h>
8  
9  #ifdef HAVE_STRING_H
10  #include <string.h>
11 @@ -105,13 +106,45 @@
12      char tbuffer[256];
13      struct stat tstat;
14      register afs_int32 code;
15 +    char *corefile = NULL;
16  #ifdef BOZO_SAVE_CORES
17      struct timeval Start;
18      struct tm *TimeFields;
19      char FileName[256];
20  #endif
21  
22 +    /* Linux always appends the PID to core dumps from threaded processes, so
23 +     * we have to scan the directory to find core files under another name. */
24      code = stat(AFSDIR_SERVER_CORELOG_FILEPATH, &tstat);
25 +    if (code) {
26 +        DIR *logdir;
27 +        struct dirent *file;
28 +        char *p;
29 +        size_t length;
30 +        unsigned long pid;
31 +
32 +        logdir = opendir(AFSDIR_LOGS_DIR);
33 +        if (logdir == NULL)
34 +            return;
35 +        while ((file = readdir(logdir)) != NULL) {
36 +            if (strncmp(file->d_name, "core.", 5) != 0)
37 +                continue;
38 +            pid = atol(file->d_name + 5);
39 +            if (pid == aproc->pid) {
40 +                length = strlen(AFSDIR_LOGS_DIR) + strlen(file->d_name) + 2;
41 +                corefile = malloc(length);
42 +                if (corefile == NULL) {
43 +                    closedir(logdir);
44 +                    return;
45 +                }
46 +                snprintf(corefile, length, "%s/%s", AFSDIR_LOGS_DIR,
47 +                         file->d_name);
48 +                code = 0;
49 +                break;
50 +            }
51 +        }
52 +        closedir(logdir);
53 +    }
54      if (code)
55         return;
56  
57 @@ -124,7 +157,12 @@
58             TimeFields->tm_hour, TimeFields->tm_min, TimeFields->tm_sec);
59      strcpy(tbuffer, FileName);
60  #endif
61 -    code = renamefile(AFSDIR_SERVER_CORELOG_FILEPATH, tbuffer);
62 +    if (corefile == NULL)
63 +        code = renamefile(AFSDIR_SERVER_CORELOG_FILEPATH, tbuffer);
64 +    else {
65 +        code = renamefile(corefile, tbuffer);
66 +        free(corefile);
67 +    }
68  }
69  
70  int