summaryrefslogtreecommitdiff
path: root/src/core_dump_handler/dlt_cdh_context.c
blob: f7b8a82c6f1768482b129e7d4e108b94dce37bf8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
/*
 * @licence app begin@
 * SPDX license identifier: MPL-2.0
 *
 * Copyright (C) 2011-2015, BMW AG
 *
 * This file is part of GENIVI Project DLT - Diagnostic Log and Trace.
 *
 * This Source Code Form is subject to the terms of the
 * Mozilla Public License (MPL), v. 2.0.
 * If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * For further information see http://www.genivi.org/.
 * @licence end@
 */

/*!
 * \author Magneti Marelli http://www.magnetimarelli.com
 * \author Lutz Helwing <lutz_helwing@mentor.com>
 *
 * \copyright Copyright © 2011-2015 BMW AG. \n
 * License MPL-2.0: Mozilla Public License version 2.0 http://mozilla.org/MPL/2.0/.
 *
 * \file dlt_cdh_context.c
 */

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <dirent.h>
#include <syslog.h>

#include "dlt_cdh.h"

/* Global buffer for file reading */
char g_buffer[4096];

/* ===================================================================
** Method      : get_exec_name(...)
**
** Description : read executable filename
**
** Parameters  : INPUT p_pid_str            pid of the process
**               OUTPUT p_exec_name         executable name
**               INPUT p_exec_name_maxsize  size of p_exec_name buffer
**
** Returns     : 0 if success, else -1
** ===================================================================*/
cdh_status_t get_exec_name(unsigned int p_pid, char *p_exec_name, int p_exec_name_maxsize)
{
    char l_exe_link[CORE_MAX_FILENAME_LENGTH] = { 0 };
    char *l_name_ptr = NULL;

    memset(l_exe_link, 0, sizeof(l_exe_link));
    snprintf(l_exe_link, sizeof(l_exe_link) - 1, "/proc/%d/exe", p_pid);

    if (readlink(l_exe_link, g_buffer, p_exec_name_maxsize) < 0)
        return CDH_NOK;

    if ((l_name_ptr = strrchr(g_buffer, '/')) == NULL)
        return CDH_NOK;

    memset(p_exec_name, 0, p_exec_name_maxsize);
    strncpy(p_exec_name, l_name_ptr + 1, p_exec_name_maxsize - 1);

    return CDH_OK;
}

/* ===================================================================
** Method      : dump_file_to(...)
**
** Description : dump the content of file p_src_filename to the file descriptor p_fout
**
** Parameters  : INPUT p_src_filename
**               INPUT p_fout
**
** Returns     : 0 if success, else -1
** ===================================================================*/
cdh_status_t dump_file_to(const char *p_src_filename, FILE *p_fout)
{
    FILE *l_fin = NULL;
    int bytes_read = 0;

    if (p_fout == NULL)
        return CDH_NOK;

    fprintf(p_fout, "\n==== Dumping file <%s> ====\n", p_src_filename);

    if ((l_fin = fopen(p_src_filename, "rt")) == NULL) {
        syslog(LOG_ERR, "ERR opening info file '%s' for dumping [%s]",
               p_src_filename,
               strerror(errno));

        fprintf(p_fout, "**error**\n");

        return CDH_NOK;
    }

    while ((bytes_read = fread(g_buffer, 1, sizeof(g_buffer), l_fin)) != 0) {
        int i = 0;

        /* changes all "\0" in the file to a "\n" */
        /* (needed for example for /proc/<pid>/cmdline, to keep all arguments) */
        for (i = 0; i < bytes_read; i++)
            if (g_buffer[i] == '\000')
                g_buffer[i] = '\n';

        fwrite(g_buffer, 1, bytes_read, p_fout);

        if (ferror(p_fout)) {
            syslog(LOG_ERR, "Writing in context file failed [%s]", strerror(errno));
            fclose(p_fout);
            fclose(l_fin);

            return CDH_NOK;

        }
    }

    if (ferror(l_fin)) {
        syslog(LOG_ERR, "reading '%s' failed [%s]", p_src_filename, strerror(errno));
        fclose(l_fin);

        return CDH_NOK;
    }

    fclose(l_fin);

    fprintf(p_fout, "\n");
    return CDH_OK;
}

/************************************************************************************************** / */
/* "ls -l" implementation for /proc/<pid>/fd (at least) */
/* Taken from coreutils sources, lib/filemode.c */
/* */
/* Return a character indicating the type of file described by
 * file mode BITS:
 * '-' regular file
 * 'b' block special file
 * 'c' character special file
 * 'C' high performance ("contiguous data") file
 * 'd' directory
 * 'D' door
 * 'l' symbolic link
 * 'm' multiplexed file (7th edition Unix; obsolete)
 * 'n' network special file (HP-UX)
 * 'p' fifo (named pipe)
 * 'P' port
 * 's' socket
 * 'w' whiteout (4.4BSD)
 * '?' some other file type  */

static char ftypelet(mode_t bits)
{
    /* These are the most common, so test for them first.  */
    if (S_ISREG(bits))
        return '-';

    if (S_ISDIR(bits))
        return 'd';

    /* Other letters standardized by POSIX 1003.1-2004.  */
    if (S_ISBLK(bits))
        return 'b';

    if (S_ISCHR(bits))
        return 'c';

    if (S_ISLNK(bits))
        return 'l';

    if (S_ISFIFO(bits))
        return 'p';

    /* Other file types (though not letters) standardized by POSIX.  */
    if (S_ISSOCK(bits))
        return 's';

    /* Nonstandard file types.
     * if (S_ISCTG (bits))
     * return 'C';
     * if (S_ISDOOR (bits))
     * return 'D';
     * if (S_ISMPB (bits) || S_ISMPC (bits))
     * return 'm';
     * if (S_ISNWK (bits))
     * return 'n';
     * if (S_ISPORT (bits))
     * return 'P';
     * if (S_ISWHT (bits))
     * return 'w';
     */

    return '?';
}

void strmode(mode_t mode, char *str)
{
    if (str == NULL)
        return;

    str[0] = ftypelet(mode);
    str[1] = mode & S_IRUSR ? 'r' : '-';
    str[2] = mode & S_IWUSR ? 'w' : '-';
    str[3] = (mode & S_ISUID
              ? (mode & S_IXUSR ? 's' : 'S')
              :
              (mode & S_IXUSR ? 'x' : '-'));
    str[4] = mode & S_IRGRP ? 'r' : '-';
    str[5] = mode & S_IWGRP ? 'w' : '-';
    str[6] = (mode & S_ISGID
              ? (mode & S_IXGRP ? 's' : 'S')
              :
              (mode & S_IXGRP ? 'x' : '-'));
    str[7] = mode & S_IROTH ? 'r' : '-';
    str[8] = mode & S_IWOTH ? 'w' : '-';
    str[9] = (mode & S_ISVTX
              ? (mode & S_IXOTH ? 't' : 'T')
              :
              (mode & S_IXOTH ? 'x' : '-'));
    str[10] = ' ';
    str[11] = '\0';
}

/* ===================================================================
** Method      : list_dircontent_to(...)
**
** Description : list the filenames in p_dirname directory to the file descriptor p_fout
**
** Parameters  : INPUT p_dirname
**               INPUT p_fout
**
** Returns     : 0 if success, else -1
** ===================================================================*/
cdh_status_t list_dircontent_to(const char *p_dirname, FILE *p_fout)
{
    DIR *l_dd = NULL; /* directory descriptor */
    struct dirent *l_entity = NULL;

    if ((l_dd = opendir(p_dirname)) == NULL) {
        syslog(LOG_ERR, "ERR reading info dir '%s' failed [%s]", p_dirname, strerror(errno));
        return CDH_NOK;
    }

    fprintf(p_fout, "==== Listing directory <%s> ====\n", p_dirname);

    while ((l_entity = readdir(l_dd)) != NULL) {
        char l_fullpath[CORE_MAX_FILENAME_LENGTH] = { 0 };
        char l_linkpath[CORE_MAX_FILENAME_LENGTH] = { 0 };
        char l_modebuf[12] = { 0 };

        struct stat l_stat;
        ssize_t l_size = 0;

        if (!strcmp(l_entity->d_name, ".") || !strcmp(l_entity->d_name, ".."))
            continue;

        snprintf(l_fullpath, sizeof(l_fullpath), "%s/%s", p_dirname, l_entity->d_name);

        if (lstat(l_fullpath, &l_stat) < 0) {
            syslog(LOG_ERR, "ERR lstat on '%s' failed. [%s]", l_fullpath, strerror(errno));
            continue;
        }

        strmode(l_stat.st_mode, l_modebuf);

        fprintf(p_fout, "%s  %ld  %d %d %ld %4s",
                l_modebuf,
                l_stat.st_nlink,
                l_stat.st_uid,
                l_stat.st_gid,
                l_stat.st_size,
                l_entity->d_name);

        switch (l_stat.st_mode & S_IFMT) {
        case S_IFBLK:
            fprintf(p_fout, " [block device]\n");
            break;

        case S_IFCHR:
            fprintf(p_fout, " [character device]\n");
            break;

        case S_IFDIR:
            fprintf(p_fout, " [directory]\n");
            break;

        case S_IFIFO:
            fprintf(p_fout, " [FIFO/pipe]\n");
            break;

        case S_IFLNK:
            l_size = readlink(l_fullpath, l_linkpath, sizeof(l_linkpath));
            l_linkpath[l_size] = 0;
            fprintf(p_fout, " -> %s\n", l_linkpath);
            break;

        case S_IFREG:
            fprintf(p_fout, " [regular file]\n");
            break;

        case S_IFSOCK:
            fprintf(p_fout, " [socket]\n");
            break;

        default:
            fprintf(p_fout, " [unknown?]\n");
            break;
        }
    } /* while ( (l_entity = readdir(l_dd)) != NULL ) */

    fprintf(p_fout, "===========================\n");
    closedir(l_dd);

    return CDH_OK;
}

/************************************************************************************************** / */
/* END of "ls -l" implementation for /proc/<pid>/fd (at least) */
/************************************************************************************************** / */

/* ===================================================================
** Method      : write_proc_context(...)
**
** Description : write the context data of the crashed process
**               (context data coming mainly from /proc)
**
** Parameters  : INPUT p_proc   crashed process info
**
** Returns     : 0 if success, else -1
** ===================================================================*/
cdh_status_t write_proc_context(const proc_info_t *p_proc)
{
    FILE *l_fout = NULL;
    char l_procfile[256] = { 0 };
    char l_outfilename[CORE_MAX_FILENAME_LENGTH] = { 0 };

    if (p_proc == NULL)
        return CDH_NOK;

    snprintf(l_outfilename, sizeof(l_outfilename), CONTEXT_FILE_PATTERN,
             CORE_TMP_DIRECTORY,
             p_proc->timestamp,
             p_proc->name,
             p_proc->pid);

    if ((l_fout = fopen(l_outfilename, "w+t")) == NULL) {
        syslog(LOG_ERR, "ERR Cannot open context file '%s' [%s]", l_outfilename, strerror(errno));
        return CDH_NOK;
    }

#define PROC_FILENAME(x) do { \
        snprintf(l_procfile, sizeof(l_procfile), "/proc/%d/"x, \
                 p_proc->pid); \
} while (0)

    fprintf(l_fout, "ProcName:%s\n", p_proc->name);
    fprintf(l_fout, "ThreadName:%s\n", p_proc->threadname);
    fprintf(l_fout, "PID:%d\n", p_proc->pid);
    fprintf(l_fout, "signal:%d\n", p_proc->signal);

    PROC_FILENAME("cmdline");
    dump_file_to(l_procfile, l_fout);

    PROC_FILENAME("cgroup");
    dump_file_to(l_procfile, l_fout);

    PROC_FILENAME("stack");
    dump_file_to(l_procfile, l_fout);

    dump_file_to("/proc/loadavg", l_fout);
    dump_file_to("/etc/sysrel", l_fout);
    dump_file_to("/proc/version", l_fout);

    PROC_FILENAME("environ");
    dump_file_to(l_procfile, l_fout);

    PROC_FILENAME("status");
    dump_file_to(l_procfile, l_fout);

    PROC_FILENAME("sched");
    dump_file_to(l_procfile, l_fout);

    PROC_FILENAME("maps");
    dump_file_to(l_procfile, l_fout);

    PROC_FILENAME("fd");
    list_dircontent_to(l_procfile, l_fout);

#undef PROC_FILENAME

    fflush(l_fout);

    return CDH_OK;
}