summaryrefslogtreecommitdiffstats
path: root/src/lib/process-stat.c
blob: fae57c7c4080cdcc14189c0b479939baf17b2d15 (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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/* Copyright (c) 2008-2021 Dovecot authors, see the included COPYING file */

#include "lib.h"
#include "process-stat.h"
#include "time-util.h"
#include <limits.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/time.h>
#include <time.h>
#include <sys/resource.h>
#include <stdio.h>

#define PROC_STAT_PATH "/proc/self/stat"
#define PROC_STATUS_PATH "/proc/self/status"
#define PROC_IO_PATH "/proc/self/io"

static const uint64_t stat_undefined = 0xFFFFFFFFFFFFFFFF;

struct key_val {
	const char *key;
	uint64_t *value;
	unsigned int idx;
};

static int parse_field(const char *line, struct key_val *field)
{
	if (str_begins(line, field->key))
		return str_to_uint64(line + strlen(field->key), field->value);
	return -1;
}

static void buffer_parse(const char *buf, struct key_val *fields)
{
	const char *const *tmp;
	tmp = t_strsplit(buf, "\n");
	unsigned int tmp_count = str_array_length(tmp);
	for (; fields->key != NULL; fields++) {
		if (fields->idx >= tmp_count ||
		    parse_field(tmp[fields->idx], fields) < 0)
			*fields->value = stat_undefined;
	}
}

static int open_fd(const char *path, struct event *event)
{
	int fd;
	uid_t uid;

	fd = open(path, O_RDONLY);

	if (fd == -1 && errno == EACCES) {
		uid = geteuid();
		/* kludge: if we're running with permissions temporarily
		   dropped, get them temporarily back so we can open
		   /proc/self/io. */
		if (seteuid(0) == 0) {
			fd = open(path, O_RDONLY);
			if (seteuid(uid) < 0)
				i_fatal("seteuid(%s) failed", dec2str(uid));
		}
		errno = EACCES;
	}
	if (fd == -1) {
		if (errno == ENOENT || errno == EACCES)
			e_debug(event, "open(%s) failed: %m", path);
		else
			e_error(event, "open(%s) failed: %m", path);
	}
	return fd;
}

static int
read_file(int fd, const char *path, char *buf_r, size_t buf_size, struct event *event)
{
	ssize_t ret;
	ret = read(fd, buf_r, buf_size);
	if (ret <= 0) {
		if (ret == -1)
			e_error(event, "read(%s) failed: %m", path);
		else
			e_error(event, "read(%s) returned EOF", path);
	} else if (ret == (ssize_t)buf_size) {
		e_error(event, "%s is larger than expected", path);
		buf_r[buf_size - 1] = '\0';
	} else {
		buf_r[ret] = '\0';
	}
	i_close_fd(&fd);
	return ret <= 0 ? -1 : 0;
}

static int parse_key_val_file(const char *path,
			      struct key_val *fields,
			      struct event *event)
{
	char buf[2048];
	int fd;

	fd = open_fd(path, event);
	if (fd == -1 || read_file(fd, path, buf, sizeof(buf), event) < 0) {
		for (; fields->key != NULL; fields++)
			*fields->value = stat_undefined;
		return -1;
	}
	buffer_parse(buf, fields);
	return 0;
}

static int parse_proc_io(struct process_stat *stat_r, struct event *event)
{
	struct key_val fields[] = {
		{ "rchar: ", &stat_r->rchar, 0 },
		{ "wchar: ", &stat_r->wchar, 1 },
		{ "syscr: ", &stat_r->syscr, 2 },
		{ "syscw: ", &stat_r->syscw, 3 },
		{ NULL, NULL, 0 },
	};
	if (stat_r->proc_io_failed ||
	    parse_key_val_file(PROC_IO_PATH, fields, event) < 0) {
		stat_r->proc_io_failed = TRUE;
		return -1;
	}
	return 0;
}

static int parse_proc_status(struct process_stat *stat_r, struct event *event)
{
	struct key_val fields [] = {
		{ "voluntary_ctxt_switches:\t", &stat_r->vol_cs, 53 },
		{ "nonvoluntary_ctxt_switches:\t", &stat_r->invol_cs, 54 },
		{ NULL, NULL, 0 },
	};
	if (stat_r->proc_status_failed ||
	    parse_key_val_file(PROC_STATUS_PATH, fields, event) < 0) {
		stat_r->proc_status_failed = TRUE;
		return -1;
	}
	return 0;
}

static int stat_get_rusage(struct process_stat *stat_r)
{
	struct rusage usage;

	if (getrusage(RUSAGE_SELF, &usage) < 0)
		return -1;
	stat_r->utime = timeval_to_usecs(&usage.ru_utime);
	stat_r->stime = timeval_to_usecs(&usage.ru_stime);
	stat_r->minor_faults = usage.ru_minflt;
	stat_r->major_faults = usage.ru_majflt;
	stat_r->vol_cs = usage.ru_nvcsw;
	stat_r->invol_cs = usage.ru_nivcsw;
	return 0;
}

static int parse_stat_file(struct process_stat *stat_r, struct event *event)
{
	int fd = -1;
	char buf[1024];
	unsigned int i;
	const char *const *tmp;
	struct {
		uint64_t *value;
		unsigned int idx;
	} fields[] = {
		{ &stat_r->minor_faults, 9 },
		{ &stat_r->major_faults, 11 },
		{ &stat_r->utime, 13 },
		{ &stat_r->stime, 14 },
		{ &stat_r->vsz, 22 },
		{ &stat_r->rss, 23 },
	};
	if (!stat_r->proc_stat_failed)
		fd = open_fd(PROC_STAT_PATH, event);
	if (fd == -1) {
		stat_r->proc_stat_failed = TRUE;
		/* vsz and rss are not provided by getrusage(), setting to undefined */
		stat_r->vsz = stat_undefined;
		stat_r->rss = stat_undefined;
		if (stat_r->rusage_failed)
			return -1;
		if (stat_get_rusage(stat_r) < 0) {
			e_error(event, "getrusage() failed: %m");
			stat_r->rusage_failed = TRUE;
			return -1;
		}
		return 0;
	}
	if (read_file(fd, PROC_STAT_PATH, buf, sizeof(buf), event) < 0) {
		stat_r->proc_stat_failed = TRUE;
		return -1;
	}
	tmp = t_strsplit(buf, " ");
	unsigned int tmp_count = str_array_length(tmp);

	for (i = 0; i < N_ELEMENTS(fields); i++) {
		if (fields[i].idx >= tmp_count ||
		    str_to_uint64(tmp[fields[i].idx], fields[i].value) < 0)
			*fields[i].value = stat_undefined;
	}
	/* rss is provided in pages, convert to bytes */
	stat_r->rss *= sysconf(_SC_PAGESIZE);
	return 0;
}

static int parse_all_stats(struct process_stat *stat_r, struct event *event)
{
	bool has_fields = FALSE;

	if (parse_stat_file(stat_r, event) == 0)
		has_fields = TRUE;
	if (parse_proc_io(stat_r, event) == 0)
		has_fields = TRUE;
	if ((!stat_r->proc_stat_failed || stat_r->rusage_failed) &&
	    parse_proc_status(stat_r, event) == 0)
		has_fields = TRUE;

	if (has_fields)
		return 0;
	return -1;
}

void process_stat_read_start(struct process_stat *stat_r, struct event *event)
{
	i_zero(stat_r);
	(void)parse_all_stats(stat_r, event);
}

void process_stat_read_finish(struct process_stat *stat, struct event *event)
{
	unsigned int i;
	struct process_stat new_stat;
	i_zero(&new_stat);
	new_stat.proc_io_failed = stat->proc_io_failed;
	new_stat.proc_status_failed = stat->proc_status_failed;
	new_stat.proc_stat_failed = stat->proc_stat_failed;
	new_stat.rusage_failed = stat->rusage_failed;
	if (parse_all_stats(&new_stat, event) < 0) {
		i_zero(stat);
		return;
	}
	stat->vsz = new_stat.vsz == stat_undefined ? 0 : new_stat.vsz;
	stat->rss = new_stat.rss == stat_undefined ? 0 : new_stat.rss;

	unsigned int cumulative_field_offsets[] = {
		offsetof(struct process_stat, utime),
		offsetof(struct process_stat, stime),
		offsetof(struct process_stat, minor_faults),
		offsetof(struct process_stat, major_faults),
		offsetof(struct process_stat, vol_cs),
		offsetof(struct process_stat, invol_cs),
		offsetof(struct process_stat, rchar),
		offsetof(struct process_stat, wchar),
		offsetof(struct process_stat, syscr),
		offsetof(struct process_stat, syscw),
	};
	for (i = 0; i < N_ELEMENTS(cumulative_field_offsets); i++) {
		uint64_t *old_value = PTR_OFFSET(stat, cumulative_field_offsets[i]);
		uint64_t *new_value = PTR_OFFSET(&new_stat, cumulative_field_offsets[i]);
		if (*old_value == stat_undefined || *new_value == stat_undefined)
			*old_value = 0;
		else
			*old_value = *new_value > *old_value ?
				(*new_value - *old_value) : 0;
	}
}