summaryrefslogtreecommitdiff
path: root/src/dump_systemstate/dump_systemstate.c
blob: f971350ea205abe99a95b5c71ef4195eb661009e (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
/*
 * dump_systemstate
 *
 * Copyright (c) 2012 - 2019 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 "dump_systemstate.h"
#include "extras.h"
#include "shared/util.h"
#include "shared/log.h"
#include "shared/spawn.h"

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

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] [-j] [-p] [-e] [-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"
			"  -p: dump list of installed packages\n"
			"  -e: dump extras defined in the config\n"
			"      at  " DUMP_SYSTEMSTATE_CONFIG_DIR_PROGRAMS_PATH "\n"
			"      and " DUMP_SYSTEMSTATE_CONFIG_DIR_FILES_PATH    "\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, exit_code = 0;
	const char *arg_file = NULL;
	int out_fd = -1;
	bool arg_dlog = false;
	bool arg_dmesg = false;
	bool arg_extras = false;
	bool arg_journal = false;
	bool arg_pkgs = false;
	char timestr[80];
	time_t cur_time;
	struct tm gm_tm;
	struct tm loc_tm;

	while ((c = getopt(argc, argv, "hf:kdjep")) != -1) {
		switch (c) {
		case 'd':
			arg_dlog = true;
			break;
		case 'k':
			arg_dmesg = true;
			break;
		case 'e':
			arg_extras = true;
			break;
		case 'j':
			arg_journal = true;
			break;
		case 'p':
			arg_pkgs = 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");
			exit_code = EXIT_ERR;
			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(out_fd, (char *)dump_item[i].path);
		if (ret < 0) {
			fprintf_fd(out_fd, "Unable to copy file.\n");
			exit_code |= EXIT_FILEERR;
		}
	}
	fprintf_fd(out_fd, "\n");

	if (arg_extras)
		exit_code |= handle_extra_dir(out_fd, DUMP_SYSTEMSTATE_CONFIG_DIR_FILES_PATH, handle_extra_file);

#define spawn_wait_checked(av, env) \
	do { \
		int err; \
		spawn_param_s param = { .fn = spawn_setstdout, .u.int_val = out_fd }; \
		if (!spawn_wait(av, env, &param, DEFAULT_COMMAND_TIMEOUT_MS, &err) || err != 0) { \
			exit_code |= EXIT_CMDERR; \
			fprintf_fd(out_fd, "\nCommand failed with error code: %d", err); \
		} \
	} while (0)

	fprintf_fd(out_fd, "\n==== System disk space usage (/bin/df -h)\n");
	char *df_args[] = {"/bin/df", "-h", NULL};
	spawn_wait_checked(df_args, NULL);

	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};
		spawn_wait_checked(du_args, NULL);
	}
	fprintf_fd(out_fd, "\n==== System timezone (ls -al /opt/etc/localtime)\n");
	char *ls_args[] = {"/bin/ls", "-al", "/opt/etc/localtime", NULL};
	spawn_wait_checked(ls_args, NULL);

	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};
	spawn_wait_checked(top_args, top_env);

	fprintf_fd(out_fd, "\n==== Current processes (/bin/ps auxfw)\n");
	char *ps_args[] = {"/bin/ps", "auxfw", NULL};
	spawn_wait_checked(ps_args, NULL);

	fprintf_fd(out_fd, "\n==== System memory statistics (/usr/bin/memps -v)\n");
	char *memps_args[] = {"/bin/memps", "-v", NULL};
	spawn_wait_checked(memps_args, NULL);

	if (is_root) {
		fprintf_fd(out_fd, "\n==== System configuration (/usr/bin/buxton2ctl dump memory, system)\n");
		char *get_mem_args[] = {"/bin/buxton2ctl", "dump", "memory",  NULL};
		spawn_wait_checked(get_mem_args, NULL);

		char *get_sys_args[] = {"/bin/buxton2ctl", "dump", "system",  NULL};
		spawn_wait_checked(get_sys_args, NULL);
	}

	if (arg_pkgs) {
		fprintf_fd(out_fd, "\n==== Installed packages (/usr/bin/pkgcmd -l)\n");
		char *pkgcmd_args[] = {"/usr/bin/pkgcmd", "-l", "--global", NULL};  // see TODO file
		spawn_wait_checked(pkgcmd_args, NULL);
	}

	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};
		spawn_wait_checked(dmesg_args, dmesg_env);
	}

	if (arg_dlog) {
		fprintf_fd(out_fd, "\n==== Log messages\n");
		char *dlogutil_args[] = {"/bin/dlogutil", "-d", "-v", "threadtime", "-u", "16384", NULL};
		spawn_wait_checked(dlogutil_args, NULL);
	}

	if (arg_journal) {
		fprintf_fd(out_fd, "\n==== Journal messages\n");
		char *journalctl_args[] = {"/bin/journalctl", "-b", "-n", "1024", NULL};
		spawn_wait_checked(journalctl_args, NULL);
	}

	if (arg_extras)
		exit_code |= handle_extra_dir(out_fd, DUMP_SYSTEMSTATE_CONFIG_DIR_PROGRAMS_PATH, handle_extra_program);

#undef spawn_wait_checked

	if (arg_file)
		close(out_fd);
exit:
	return exit_code;

}