summaryrefslogtreecommitdiffstats
path: root/lib/Plugins/SOSreport.cpp
blob: 4938f7d785213ef6bd8dd12ad8924b7cd0f0c77c (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
/*
    SOSreport.cpp

    Copyright (C) 2009  RedHat inc.

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
    */

#include <ext/stdio_filebuf.h>
#include <fstream>
#include <sstream>
#include "abrtlib.h"
#include "abrt_types.h"
#include "ABRTException.h"
#include "SOSreport.h"
#include "DebugDump.h"
#include "ABRTException.h"
#include "CommLayerInner.h"

/* abrtlib candidate */
static void CopyFile(const char *pSourceName, const char *pDestName)
{
    int src = open(pSourceName, O_RDONLY);
    if (src < 0)
    {
        throw CABRTException(EXCEP_PLUGIN, ssprintf("Can't open input sosreport file '%s'", pSourceName));
    }
    int dst = open(pDestName, O_WRONLY | O_TRUNC | O_CREAT, 0666);
    if (dst < 0)
    {
        close(src);
        throw CABRTException(EXCEP_PLUGIN, ssprintf("Can't open output sosreport file '%s'", pDestName));
    }
    copyfd_eof(src, dst);
    close(src);
    close(dst);
}

static void ErrorCheck(int pos)
{
    if (pos < 0)
    {
        throw CABRTException(EXCEP_PLUGIN, "Can't find filename in sosreport output");
    }
}

static std::string ParseFilename(const std::string& pOutput)
{
    /*
    the sosreport's filename is embedded in sosreport's output.
    It appears on the line after the string in 'sosreport_filename_marker',
    it has leading spaces, and a trailing newline.  This function trims
    any leading and trailing whitespace from the filename.
    */
    static const char sosreport_filename_marker[] =
          "Your sosreport has been generated and saved in:";

    int p = pOutput.find(sosreport_filename_marker);
    ErrorCheck(p);

    p += sizeof(sosreport_filename_marker)-1;

    int filename_start = pOutput.find_first_not_of(" \n\t", p);
    ErrorCheck(p);

    int line_end = pOutput.find_first_of('\n',filename_start);
    ErrorCheck(p);

    int filename_end = pOutput.find_last_not_of(" \n\t",line_end);
    ErrorCheck(p);

    return pOutput.substr(filename_start, filename_end - filename_start + 1);
}

/* TODO: do not duplicate: RunApp.cpp has same function too */
static void ParseArgs(const char *psArgs, vector_string_t& pArgs)
{
    unsigned ii;
    bool is_quote = false;
    std::string item;

    for (ii = 0; psArgs[ii]; ii++)
    {
        if (psArgs[ii] == '"')
        {
            is_quote = !is_quote;
        }
        else if (psArgs[ii] == ',' && !is_quote)
        {
            pArgs.push_back(item);
            item.clear();
        }
        else
        {
            item += psArgs[ii];
        }
    }

    if (item.size() != 0)
    {
        pArgs.push_back(item);
    }
}

void CActionSOSreport::Run(const std::string& pActionDir,
                           const std::string& pArgs)
{
    update_client(_("Executing SOSreport plugin..."));

    static const char command_default[] = "sosreport --batch --no-progressbar --only=anaconda --only=bootloader"
                                            " --only=devicemapper --only=filesys --only=hardware --only=kernel"
                                            " --only=libraries --only=memory --only=networking --only=nfsserver"
                                            " --only=pam --only=process --only=rpm -k rpm.rpmva=off --only=ssh"
                                            " --only=startup --only=yum 2>&1";
    static const char command_prefix[] = "sosreport --batch --no-progressbar";
    std::string command;

    vector_string_t args;
    ParseArgs(pArgs.c_str(), args);

    if (args.size() == 0 || args[0] == "")
    {
        command = std::string(command_default);
    }
    else
    {
        command = std::string(command_prefix) + ' ' + args[0] + " 2>&1";
    }

    update_client(_("running sosreport: ") + command);
    FILE *fp = popen(command.c_str(), "r");

    if (fp == NULL)
    {
        throw CABRTException(EXCEP_PLUGIN, std::string("CActionSOSreport::Run(): cannot execute ") + command);
    }

    std::ostringstream output_stream;
    __gnu_cxx::stdio_filebuf<char> command_output_buffer(fp, std::ios_base::in);

    output_stream << command << std::endl;
    output_stream << &command_output_buffer;

    pclose(fp);
    update_client(_("done running sosreport"));

    std::string output = output_stream.str();

    std::string sosreport_filename = ParseFilename(output);
    std::string sosreport_dd_filename = pActionDir + "/sosreport.tar.bz2";

    CDebugDump dd;
    dd.Open(pActionDir);
    //Not useful
    //dd.SaveText("sosreportoutput", output);
    CopyFile(sosreport_filename.c_str(), sosreport_dd_filename.c_str());
}

PLUGIN_INFO(ACTION,
            CActionSOSreport,
            "SOSreport",
            "0.0.2",
            "Run sosreport, save the output in the crash dump",
            "gavin@redhat.com",
            "https://fedorahosted.org/abrt/wiki",
            "");