summaryrefslogtreecommitdiff
path: root/src/system/dlt-system-shell.c
blob: c7ebe2c203bd2a183d612e61d9fbf6f3e1ceb6db (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
/*
 * 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/.
 */

/*!
 * \author Lassi Marttala <lassi.lm.marttala@partner.bmw.de>
 *
 * \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-system-shell.c
 */

/*******************************************************************************
**                                                                            **
**  SRC-MODULE: dlt-system-shell.c                                                  **
**                                                                            **
**  TARGET    : linux                                                         **
**                                                                            **
**  PROJECT   : DLT                                                           **
**                                                                            **
**  AUTHOR    : Lassi Marttala <lassi.lm.marttala@partner.bmw.de>             **
**              Alexander Wenzel Alexander.AW.Wenzel@bmw.de                   **
**                                                                            **
**  PURPOSE   :                                                               **
**                                                                            **
**  REMARKS   :                                                               **
**                                                                            **
**  PLATFORM DEPENDANT [yes/no]: yes                                          **
**                                                                            **
**  TO BE CHANGED BY USER [yes/no]: no                                        **
**                                                                            **
*******************************************************************************/

/*******************************************************************************
**                      Author Identity                                       **
********************************************************************************
**                                                                            **
** Initials     Name                       Company                            **
** --------     -------------------------  ---------------------------------- **
**  lm          Lassi Marttala             BMW                                **
*******************************************************************************/
#include "dlt.h"
#include "dlt-system.h"

#include <string.h>
#include <stdlib.h>

#define DLT_SHELL_COMMAND_MAX_LENGTH 1024

DLT_IMPORT_CONTEXT(dltsystem)
DLT_DECLARE_CONTEXT(shellContext)

int dlt_shell_injection_callback(uint32_t service_id, void *data, uint32_t length)
{
    (void)length;

    DLT_LOG(shellContext, DLT_LOG_DEBUG,
            DLT_STRING("dlt-system-shell, injection callback"));
    char text[DLT_SHELL_COMMAND_MAX_LENGTH];
    int syserr = 0;

    if (length <= DLT_SHELL_COMMAND_MAX_LENGTH - 1) {
        strncpy(text, data, length);
        text[length] = 0;
    }
    else {
        strncpy(text, data, DLT_SHELL_COMMAND_MAX_LENGTH - 1);
        text[DLT_SHELL_COMMAND_MAX_LENGTH - 1] = 0;
    }

    DLT_LOG(shellContext, DLT_LOG_DEBUG,
            DLT_STRING("dlt-system-shell, injection injection id:"),
            DLT_UINT32(service_id));
    DLT_LOG(shellContext, DLT_LOG_DEBUG,
            DLT_STRING("dlt-system-shell, injection data:"),
            DLT_STRING(text));

    switch (service_id) {
    case 0x1001:

        if ((syserr = system(text)) != 0)
            DLT_LOG(shellContext, DLT_LOG_ERROR,
                    DLT_STRING("dlt-system-shell, abnormal exit status."),
                    DLT_STRING(text),
                    DLT_INT(syserr));
        else
            DLT_LOG(shellContext, DLT_LOG_INFO,
                    DLT_STRING("Shell command executed:"),
                    DLT_STRING(text));

        break;
    default:
        DLT_LOG(shellContext, DLT_LOG_ERROR,
                DLT_STRING("dlt-system-shell, unknown command received."),
                DLT_UINT32(service_id),
                DLT_STRING(text));
        break;
    }

    return 0;
}

void init_shell()
{
    DLT_LOG(dltsystem, DLT_LOG_DEBUG,
            DLT_STRING("dlt-system-shell, register callback"));
    DLT_REGISTER_CONTEXT(shellContext, "CMD", "Execute Shell commands");
    DLT_REGISTER_INJECTION_CALLBACK(shellContext, 0x1001, dlt_shell_injection_callback);
}