summaryrefslogtreecommitdiff
path: root/src/dump_systemstate/dump_systemstate.c
blob: 776ec2b2b0b4245106ca98d00e2445d7e605fe2b (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
/*
 * dump_systemstate
 *
 * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the License);
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * @file    dump_systemstate.c
 * @brief   dump system states.
 */

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <time.h>
#include <limits.h>
#include <getopt.h>
#include <sys/stat.h>
#include <sys/vfs.h>

#include "shared/util.h"
#include "shared/log.h"

#define FILE_PERM (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH)

#define TIMEOUT_DEFAULT 60

static struct dump_item {
	const char *title;
	const char *path;
} dump_item[] = {
	{"==== Binary version "            , "/etc/info.ini"},
	{"==== Tizen version "             , "/etc/tizen-release"},
	{"==== Kernel version "            , "/proc/version"},
	{"==== Boot arguments "            , "/proc/cmdline"},
	{"==== CPU & system architecture " , "/proc/cpuinfo"},
	{"==== System uptime "             , "/proc/uptime"},
	{"==== System statistics "         , "/proc/stat"},
	{"==== System memory usage "       , "/proc/meminfo"},
	{"==== Device major numbers "      , "/proc/devices"},
	{"==== System disk I/O satistics " , "/proc/diskstats"},
};

static void usage()
{
	fprintf(stderr, "usage: dump_systemstate [-k] [-d] [-f file]\n"
			"  -f: write to file (instead of stdout)\n"
			"  -k: dump kernel messages (only root)\n"
			"  -d: dump dlog messages\n"
			"  -j: dump journal log messages\n"
	   );
}

/* get disk used percentage */
static int get_disk_used_percent(const char *path)
{
	struct statfs lstatfs;
	int percent;

	if (!path)
		return -1;

	if (statfs(path, &lstatfs) < 0)
		return -1;
	percent = (((lstatfs.f_blocks - lstatfs.f_bfree) * 1000) / (lstatfs.f_blocks)) + 9;
	percent = percent/10;
	return percent;
}

int main(int argc, char *argv[])
{
	int c, ret, i, is_root, dpercent;
	const char *arg_file = NULL;
	int out_fd = -1;
	bool arg_dlog = false;
	bool arg_dmesg = false;
	bool arg_journal = false;
	char timestr[80];
	time_t cur_time;
	struct tm gm_tm;
	struct tm loc_tm;

	while ((c = getopt(argc, argv, "hf:kdj")) != -1) {
		switch (c) {
		case 'd':
			arg_dlog = true;
			break;
		case 'k':
			arg_dmesg = true;
			break;
		case 'j':
			arg_journal = true;
			break;
		case 'f':
			arg_file = optarg;
			break;
		case '?':
		case 'h':
			printf("\n");
			usage();
			ret = 0;
			goto exit;
		}
	}
	ret = 0;
	cur_time = time(NULL);
	gmtime_r(&cur_time, &gm_tm);
	localtime_r(&cur_time, &loc_tm);
	is_root = !(geteuid());

	/* open output file */
	if (arg_file == NULL) {
		out_fd = STDOUT_FILENO;
	} else {
		out_fd = open(arg_file, O_WRONLY | O_TRUNC | O_CREAT, FILE_PERM);
		if (out_fd < 0) {
			perror("couldn't open output file");
			ret = out_fd;
			goto exit;
		}
	}
	/* print timestamp */
	strftime(timestr, sizeof(timestr),
					"%Y-%m-%d %H:%M:%S%z", &loc_tm);
	fprintf_fd(out_fd, "dump_systemstate: %s\n", timestr);

	for (i = 0; i < ARRAY_SIZE(dump_item); i++) {
		fsync(out_fd);
		fprintf_fd(out_fd, "\n%s(%s)\n",
						dump_item[i].title, dump_item[i].path);
		ret = dump_file_write_fd((char *)dump_item[i].path, out_fd);
		if (ret < 0)
			goto exit_close;
	}
	fprintf_fd(out_fd, "\n");

	fprintf_fd(out_fd, "\n==== System disk space usage (/bin/df -h)\n");
	char *df_args[] = {"/bin/df", "-h", NULL};
	ret = run_command_write_fd_timeout(df_args[0], df_args, NULL, out_fd, NULL, 0, TIMEOUT_DEFAULT);
	if (ret < 0)
		goto exit_close;

	dpercent = get_disk_used_percent("/opt");
	if (90 < dpercent) {
		fprintf_fd(out_fd, "\n==== System disk space usage detail - %d%% (/bin/du -ah /opt)\n", dpercent);
		char *du_args[] = {"/bin/du", "-ah", "/opt", "--exclude=/opt/usr", NULL};
		ret = run_command_write_fd_timeout(du_args[0], du_args, NULL, out_fd, NULL, 0, TIMEOUT_DEFAULT);
		if (ret < 0)
			goto exit_close;
	}
	fprintf_fd(out_fd, "\n==== System timezone (ls -al /opt/etc/localtime)\n");
	char *ls_args[] = {"/bin/ls", "-al", "/opt/etc/localtime", NULL};
	ret = run_command_write_fd_timeout(ls_args[0], ls_args, NULL, out_fd, NULL, 0, TIMEOUT_DEFAULT);
	if (ret < 0)
		goto exit_close;

	fprintf_fd(out_fd, "\n==== System summary (/usr/bin/top -bcH -n 1)\n");
	char *top_args[] = {"/bin/top", "-bcH", "-n", "1", NULL};
	char *top_env[] = {"COLUMNS=200", NULL};
	ret = run_command_write_fd_timeout(top_args[0], top_args, top_env, out_fd, NULL, 0, TIMEOUT_DEFAULT);
	if (ret < 0)
		goto exit_close;

	fprintf_fd(out_fd, "\n==== Current processes (/bin/ps auxfw)\n");
	char *ps_args[] = {"/bin/ps", "auxfw", NULL};
	ret = run_command_write_fd_timeout(ps_args[0], ps_args, NULL, out_fd, NULL, 0, TIMEOUT_DEFAULT);
	if (ret < 0)
		goto exit_close;

	fprintf_fd(out_fd, "\n==== System memory statistics (/usr/bin/memps -v)\n");
	char *memps_args[] = {"/bin/memps", "-v", NULL};
	ret = run_command_write_fd_timeout(memps_args[0], memps_args, NULL, out_fd, NULL, 0, TIMEOUT_DEFAULT);
	if (ret < 0)
		goto exit_close;

	if (is_root) {
		fprintf_fd(out_fd, "\n==== System configuration (/usr/bin/vconftool get memory, db, file)\n");
		char *get_mem_args[] = {"/bin/vconftool", "get", "memory/", "-r", NULL};
		ret = run_command_write_fd_timeout(get_mem_args[0], get_mem_args, NULL, out_fd, NULL, 0, TIMEOUT_DEFAULT);
		if (ret < 0)
			goto exit_close;

		char *get_db_args[] = {"/bin/vconftool", "get", "db/", "-r", NULL};
		ret = run_command_write_fd_timeout(get_db_args[0], get_db_args, NULL, out_fd, NULL, 0, TIMEOUT_DEFAULT);
		if (ret < 0)
			goto exit_close;

		char *get_file_args[] = {"/bin/vconftool", "get", "file/", "-r", NULL};
		ret = run_command_write_fd_timeout(get_file_args[0], get_file_args, NULL, out_fd, NULL, 0, TIMEOUT_DEFAULT);
		if (ret < 0)
			goto exit_close;
	}

	if (arg_dmesg && is_root) {
		fprintf_fd(out_fd, "\n==== Kernel messages (TZ=UTC /bin/dmesg -T)\n");
		char *dmesg_args[] = {"/bin/dmesg", "-T", NULL};
		char *dmesg_env[] = {"TZ=UTC", NULL};
		ret = run_command_write_fd_timeout(dmesg_args[0], dmesg_args, dmesg_env, out_fd, NULL, 0, TIMEOUT_DEFAULT);
		if (ret < 0)
			goto exit_close;
	}

	if (arg_dlog) {
		fprintf_fd(out_fd, "\n==== Log messages\n");
		char *dlogutil_args[] = {"/bin/dlogutil", "-d", "-v", "threadtime", "-u", "16384", NULL};
		ret = run_command_write_fd_timeout(dlogutil_args[0], dlogutil_args, NULL, out_fd, NULL, 0, TIMEOUT_DEFAULT);
		if (ret < 0)
			goto exit_close;
	}

	if (arg_journal) {
		fprintf_fd(out_fd, "\n==== Journal messages\n");
		char *journalctl_args[] = {"/bin/journalctl", "-b", "-n", "1024", NULL};
		ret = run_command_write_fd_timeout(journalctl_args[0], journalctl_args, NULL, out_fd, NULL, 0, TIMEOUT_DEFAULT);
		if (ret < 0)
			goto exit_close;
	}

exit_close:
	if (arg_file)
		close(out_fd);
exit:
	return ret;
}