[353bda]: / gui / oprof_start_util.cpp  Maximize  Restore  History

Download this file

332 lines (277 with data), 7.0 kB

  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
/**
* @file oprof_start_util.cpp
* Miscellaneous helpers for the GUI start
*
* @remark Copyright 2002 OProfile authors
* @remark Read the file COPYING
*
* @author Philippe Elie
* @author John Levon
*/
#include <dirent.h>
#include <unistd.h>
#include <glob.h>
#include <cerrno>
#include <vector>
#include <cmath>
#include <sstream>
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <qfiledialog.h>
#include <qmessagebox.h>
#include "op_file.h"
#include "file_manip.h"
#include "child_reader.h"
#include "op_libiberty.h"
#include "oprof_start.h"
#include "oprof_start_util.h"
using namespace std;
namespace {
// return the ~ expansion suffixed with a '/'
string const get_config_dir()
{
return "/root";
}
string daemon_pid;
} // namespace anon
daemon_status::daemon_status()
: running(false),
nr_interrupts(0)
{
int HZ;
if (!daemon_pid.empty()) {
string proc_filename = string("/proc/") + daemon_pid + "/exe";
string const exec = op_realpath(proc_filename);
if (exec == proc_filename)
daemon_pid.erase();
else
running = true;
}
if (daemon_pid.empty()) {
DIR * dir;
struct dirent * dirent;
if (!(dir = opendir("/proc"))) {
perror("oprofiled: /proc directory could not be opened. ");
exit(EXIT_FAILURE);
}
while ((dirent = readdir(dir))) {
string const exec =
op_realpath(string("/proc/")
+ dirent->d_name + "/exe");
string const name = op_basename(exec);
if (name != "oprofiled")
continue;
daemon_pid = dirent->d_name;
running = true;
}
closedir(dir);
}
HZ = sysconf(_SC_CLK_TCK);
if (HZ == -1) {
perror("oprofiled: Unable to determine clock ticks per second. ");
exit(EXIT_FAILURE);
}
if (daemon_pid.empty())
return;
nr_interrupts = 0;
switch (op_get_interface()) {
case OP_INTERFACE_24:
{
ifstream ifs3("/proc/sys/dev/oprofile/nr_interrupts");
if (ifs3)
ifs3 >> nr_interrupts;
}
break;
case OP_INTERFACE_26:
{
static unsigned int old_sum_interrupts;
unsigned int sum_interrupts = 0;
glob_t file_names;
file_names.gl_offs = 0;
glob("/dev/oprofile/stats/cpu*/sample_received",
GLOB_DOOFFS, NULL, &file_names);
for (size_t i = 0; i < file_names.gl_pathc; ++i) {
ifstream ifs3(file_names.gl_pathv[i]);
if (ifs3) {
unsigned int file_interrupts;
ifs3 >> file_interrupts;
sum_interrupts += file_interrupts;
}
}
if (old_sum_interrupts > sum_interrupts)
// occur if we stop/restart daemon.
old_sum_interrupts = 0;
nr_interrupts = sum_interrupts - old_sum_interrupts;
old_sum_interrupts = sum_interrupts;
globfree(&file_names);
}
break;
default:
break;
}
}
/**
* get_config_filename - get absolute filename of file in user $HOME
* @param filename the relative filename
*
* Get the absolute path of a file in a user's home directory.
*/
string const get_config_filename(string const & filename)
{
return get_config_dir() + "/" + filename;
}
/**
* check_and_create_config_dir - make sure config dir is accessible
*
* Returns %true if the dir is accessible.
*/
bool check_and_create_config_dir()
{
string dir = get_config_filename(".oprofile");
char * name = xstrdup(dir.c_str());
if (create_dir(name)) {
ostringstream out;
out << "unable to create " << dir << " directory ";
out << "cause: " << strerror(errno);
QMessageBox::warning(0, 0, out.str().c_str());
free(name);
return false;
}
free(name);
return true;
}
/**
* format - re-format a string
* @param orig string to format
* @param maxlen width of line
*
* Re-formats a string to fit into a certain width,
* breaking lines at spaces between words.
*
* Returns the formatted string
*/
string const format(string const & orig, uint const maxlen)
{
string text(orig);
istringstream ss(text);
vector<string> lines;
string oline;
string line;
while (getline(ss, oline)) {
if (line.size() + oline.size() < maxlen) {
lines.push_back(line + oline);
line.erase();
} else {
lines.push_back(line);
line.erase();
string s;
string word;
istringstream oss(oline);
while (oss >> word) {
if (line.size() + word.size() > maxlen) {
lines.push_back(line);
line.erase();
}
line += word + " ";
}
}
}
if (line.size())
lines.push_back(line);
string ret;
for(vector<string>::const_iterator it = lines.begin(); it != lines.end(); ++it)
ret += *it + "\n";
return ret;
}
/**
* do_exec_command - execute a command
* @param cmd command name
* @param args arguments to command
*
* Execute a command synchronously. An error message is shown
* if the command returns a non-zero status, which is also returned.
*
* The arguments are verified and will refuse to execute if they contain
* shell metacharacters.
*/
int do_exec_command(string const & cmd, vector<string> const & args)
{
ostringstream err;
bool ok = true;
// verify arguments
for (vector<string>::const_iterator cit = args.begin();
cit != args.end(); ++cit) {
if (verify_argument(*cit))
continue;
QMessageBox::warning(0, 0,
string(
"Could not execute: Argument \"" + *cit +
"\" contains shell metacharacters.\n").c_str());
return EINVAL;
}
child_reader reader(cmd, args);
if (reader.error())
ok = false;
if (ok)
reader.get_data(cout, err);
int ret = reader.terminate_process();
if (ret) {
string error = reader.error_str() + "\n";
error += "Failed: \n" + err.str() + "\n";
string cmdline = cmd;
for (vector<string>::const_iterator cit = args.begin();
cit != args.end(); ++cit) {
cmdline += " " + *cit + " ";
}
error += "\n\nCommand was :\n\n" + cmdline + "\n";
QMessageBox::warning(0, 0, format(error, 50).c_str());
}
return ret;
}
/**
* do_open_file_or_dir - open file/directory
* @param base_dir directory to start at
* @param dir_only directory or filename to select
*
* Select a file or directory. The selection is returned;
* an empty string if the selection was cancelled.
*/
string const do_open_file_or_dir(string const & base_dir, bool dir_only)
{
QString result;
if (dir_only) {
result = QFileDialog::getExistingDirectory(base_dir.c_str(), 0,
"open_file_or_dir", "Get directory name", true);
} else {
result = QFileDialog::getOpenFileName(base_dir.c_str(), 0, 0,
"open_file_or_dir", "Get filename");
}
if (result.isNull())
return string();
else
return result.latin1();
}
/**
* verify_argument - check string for potentially dangerous characters
*
* This function returns false if the string contains dangerous shell
* metacharacters.
*
* WWW Security FAQ dangerous chars:
*
* & ; ` ' \ " | * ? ~ < > ^ ( ) [ ] { } $ \n \r
*
* David Wheeler: ! #
*
* We allow '-' because we disallow whitespace. We allow ':' and '='
*/
bool verify_argument(string const & str)
{
if (str.find_first_not_of(
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz0123456789_:=-+%,./")
!= string::npos)
return false;
return true;
}