1110acbc6SDaniel Lezcano // SPDX-License-Identifier: GPL-2.0-only
2110acbc6SDaniel Lezcano // Copyright (C) 2022, Linaro Ltd - Daniel Lezcano <daniel.lezcano@linaro.org>
3110acbc6SDaniel Lezcano #define _GNU_SOURCE
4110acbc6SDaniel Lezcano #include <dirent.h>
5110acbc6SDaniel Lezcano #include <fcntl.h>
6110acbc6SDaniel Lezcano #include <getopt.h>
7110acbc6SDaniel Lezcano #include <regex.h>
8110acbc6SDaniel Lezcano #include <signal.h>
9110acbc6SDaniel Lezcano #include <stdio.h>
10110acbc6SDaniel Lezcano #include <stdlib.h>
11110acbc6SDaniel Lezcano #include <string.h>
12110acbc6SDaniel Lezcano #include <sys/stat.h>
13110acbc6SDaniel Lezcano #include <sys/signalfd.h>
14110acbc6SDaniel Lezcano #include <sys/timerfd.h>
15110acbc6SDaniel Lezcano #include <sys/types.h>
16110acbc6SDaniel Lezcano #include <sys/wait.h>
17110acbc6SDaniel Lezcano #include <time.h>
18110acbc6SDaniel Lezcano #include <unistd.h>
19110acbc6SDaniel Lezcano #include <linux/thermal.h>
20110acbc6SDaniel Lezcano 
21110acbc6SDaniel Lezcano #include <libconfig.h>
22110acbc6SDaniel Lezcano #include "thermal-tools.h"
23110acbc6SDaniel Lezcano 
24110acbc6SDaniel Lezcano #define CLASS_THERMAL "/sys/class/thermal"
25110acbc6SDaniel Lezcano 
26110acbc6SDaniel Lezcano enum {
27110acbc6SDaniel Lezcano 	THERMOMETER_SUCCESS = 0,
28110acbc6SDaniel Lezcano 	THERMOMETER_OPTION_ERROR,
29110acbc6SDaniel Lezcano 	THERMOMETER_LOG_ERROR,
30110acbc6SDaniel Lezcano 	THERMOMETER_CONFIG_ERROR,
31110acbc6SDaniel Lezcano 	THERMOMETER_TIME_ERROR,
32110acbc6SDaniel Lezcano 	THERMOMETER_INIT_ERROR,
33110acbc6SDaniel Lezcano 	THERMOMETER_RUNTIME_ERROR
34110acbc6SDaniel Lezcano };
35110acbc6SDaniel Lezcano 
36110acbc6SDaniel Lezcano struct options {
37110acbc6SDaniel Lezcano 	int loglvl;
38110acbc6SDaniel Lezcano 	int logopt;
39110acbc6SDaniel Lezcano 	int overwrite;
40110acbc6SDaniel Lezcano 	int duration;
41110acbc6SDaniel Lezcano 	const char *config;
42110acbc6SDaniel Lezcano 	char postfix[PATH_MAX];
43110acbc6SDaniel Lezcano 	char output[PATH_MAX];
44110acbc6SDaniel Lezcano };
45110acbc6SDaniel Lezcano 
46110acbc6SDaniel Lezcano struct tz_regex {
47110acbc6SDaniel Lezcano 	regex_t regex;
48110acbc6SDaniel Lezcano 	int polling;
49110acbc6SDaniel Lezcano };
50110acbc6SDaniel Lezcano 
51110acbc6SDaniel Lezcano struct configuration {
52110acbc6SDaniel Lezcano 	struct tz_regex *tz_regex;
53110acbc6SDaniel Lezcano 	int nr_tz_regex;
54110acbc6SDaniel Lezcano 
55110acbc6SDaniel Lezcano };
56110acbc6SDaniel Lezcano 
57110acbc6SDaniel Lezcano struct tz {
58110acbc6SDaniel Lezcano 	FILE *file_out;
59110acbc6SDaniel Lezcano 	int fd_temp;
60110acbc6SDaniel Lezcano 	int fd_timer;
61110acbc6SDaniel Lezcano 	int polling;
62110acbc6SDaniel Lezcano 	const char *name;
63110acbc6SDaniel Lezcano };
64110acbc6SDaniel Lezcano 
65110acbc6SDaniel Lezcano struct thermometer {
66110acbc6SDaniel Lezcano 	struct tz *tz;
67110acbc6SDaniel Lezcano 	int nr_tz;
68110acbc6SDaniel Lezcano };
69110acbc6SDaniel Lezcano 
configuration_tz_match(const char * expr,struct configuration * config)70110acbc6SDaniel Lezcano static struct tz_regex *configuration_tz_match(const char *expr,
71110acbc6SDaniel Lezcano 					       struct configuration *config)
72110acbc6SDaniel Lezcano {
73110acbc6SDaniel Lezcano 	int i;
74110acbc6SDaniel Lezcano 
75110acbc6SDaniel Lezcano 	for (i = 0; i < config->nr_tz_regex; i++) {
76110acbc6SDaniel Lezcano 
77110acbc6SDaniel Lezcano 		if (!regexec(&config->tz_regex[i].regex, expr, 0, NULL, 0))
78110acbc6SDaniel Lezcano 			return &config->tz_regex[i];
79110acbc6SDaniel Lezcano 	}
80110acbc6SDaniel Lezcano 
81110acbc6SDaniel Lezcano 	return NULL;
82110acbc6SDaniel Lezcano }
83110acbc6SDaniel Lezcano 
configuration_default_init(struct configuration * config)84110acbc6SDaniel Lezcano static int configuration_default_init(struct configuration *config)
85110acbc6SDaniel Lezcano {
86110acbc6SDaniel Lezcano 	config->tz_regex = realloc(config->tz_regex, sizeof(*config->tz_regex) *
87110acbc6SDaniel Lezcano 				   (config->nr_tz_regex + 1));
88110acbc6SDaniel Lezcano 
89110acbc6SDaniel Lezcano 	if (regcomp(&config->tz_regex[config->nr_tz_regex].regex, ".*",
90110acbc6SDaniel Lezcano 		    REG_NOSUB | REG_EXTENDED)) {
91110acbc6SDaniel Lezcano 		ERROR("Invalid regular expression\n");
92110acbc6SDaniel Lezcano 		return -1;
93110acbc6SDaniel Lezcano 	}
94110acbc6SDaniel Lezcano 
95110acbc6SDaniel Lezcano 	config->tz_regex[config->nr_tz_regex].polling = 250;
96110acbc6SDaniel Lezcano 	config->nr_tz_regex = 1;
97110acbc6SDaniel Lezcano 
98110acbc6SDaniel Lezcano 	return 0;
99110acbc6SDaniel Lezcano }
100110acbc6SDaniel Lezcano 
configuration_init(const char * path,struct configuration * config)101110acbc6SDaniel Lezcano static int configuration_init(const char *path, struct configuration *config)
102110acbc6SDaniel Lezcano {
103110acbc6SDaniel Lezcano 	config_t cfg;
104110acbc6SDaniel Lezcano 
105110acbc6SDaniel Lezcano 	config_setting_t *tz;
106110acbc6SDaniel Lezcano 	int i, length;
107110acbc6SDaniel Lezcano 
108110acbc6SDaniel Lezcano 	if (path && access(path, F_OK)) {
109110acbc6SDaniel Lezcano 		ERROR("'%s' is not accessible\n", path);
110110acbc6SDaniel Lezcano 		return -1;
111110acbc6SDaniel Lezcano 	}
112110acbc6SDaniel Lezcano 
113110acbc6SDaniel Lezcano 	if (!path && !config->nr_tz_regex) {
114110acbc6SDaniel Lezcano 		INFO("No thermal zones configured, using wildcard for all of them\n");
115110acbc6SDaniel Lezcano 		return configuration_default_init(config);
116110acbc6SDaniel Lezcano 	}
117110acbc6SDaniel Lezcano 
118110acbc6SDaniel Lezcano 	config_init(&cfg);
119110acbc6SDaniel Lezcano 
120110acbc6SDaniel Lezcano 	if (!config_read_file(&cfg, path)) {
121110acbc6SDaniel Lezcano 		ERROR("Failed to parse %s:%d - %s\n", config_error_file(&cfg),
122110acbc6SDaniel Lezcano 		      config_error_line(&cfg), config_error_text(&cfg));
123110acbc6SDaniel Lezcano 
124110acbc6SDaniel Lezcano 		return -1;
125110acbc6SDaniel Lezcano 	}
126110acbc6SDaniel Lezcano 
127110acbc6SDaniel Lezcano 	tz = config_lookup(&cfg, "thermal-zones");
128110acbc6SDaniel Lezcano 	if (!tz) {
129110acbc6SDaniel Lezcano 		ERROR("No thermal zone configured to be monitored\n");
130110acbc6SDaniel Lezcano 		return -1;
131110acbc6SDaniel Lezcano 	}
132110acbc6SDaniel Lezcano 
133110acbc6SDaniel Lezcano 	length = config_setting_length(tz);
134110acbc6SDaniel Lezcano 
135110acbc6SDaniel Lezcano 	INFO("Found %d thermal zone(s) regular expression\n", length);
136110acbc6SDaniel Lezcano 
137110acbc6SDaniel Lezcano 	for (i = 0; i < length; i++) {
138110acbc6SDaniel Lezcano 
139110acbc6SDaniel Lezcano 		config_setting_t *node;
140110acbc6SDaniel Lezcano 		const char *name;
141110acbc6SDaniel Lezcano 		int polling;
142110acbc6SDaniel Lezcano 
143110acbc6SDaniel Lezcano 		node = config_setting_get_elem(tz, i);
144110acbc6SDaniel Lezcano 		if (!node) {
145110acbc6SDaniel Lezcano 			ERROR("Missing node name '%d'\n", i);
146110acbc6SDaniel Lezcano 			return -1;
147*cb4487d2SJiapeng Chong 		}
148110acbc6SDaniel Lezcano 
149110acbc6SDaniel Lezcano 		if (!config_setting_lookup_string(node, "name", &name)) {
150110acbc6SDaniel Lezcano 			ERROR("Thermal zone name not found\n");
151110acbc6SDaniel Lezcano 			return -1;
152110acbc6SDaniel Lezcano 		}
153110acbc6SDaniel Lezcano 
154110acbc6SDaniel Lezcano 		if (!config_setting_lookup_int(node, "polling", &polling)) {
155110acbc6SDaniel Lezcano 			ERROR("Polling value not found");
156110acbc6SDaniel Lezcano 			return -1;
157110acbc6SDaniel Lezcano 		}
158110acbc6SDaniel Lezcano 
159110acbc6SDaniel Lezcano 		config->tz_regex = realloc(config->tz_regex, sizeof(*config->tz_regex) *
160110acbc6SDaniel Lezcano 					(config->nr_tz_regex + 1));
161110acbc6SDaniel Lezcano 
162110acbc6SDaniel Lezcano 		if (regcomp(&config->tz_regex[config->nr_tz_regex].regex, name,
163110acbc6SDaniel Lezcano 			    REG_NOSUB | REG_EXTENDED)) {
164110acbc6SDaniel Lezcano 			ERROR("Invalid regular expression '%s'\n", name);
165110acbc6SDaniel Lezcano 			continue;
166110acbc6SDaniel Lezcano 		}
167110acbc6SDaniel Lezcano 
168110acbc6SDaniel Lezcano 		config->tz_regex[config->nr_tz_regex].polling = polling;
169110acbc6SDaniel Lezcano 		config->nr_tz_regex++;
170110acbc6SDaniel Lezcano 
171110acbc6SDaniel Lezcano 		INFO("Thermal zone regular expression '%s' with polling %d\n",
172110acbc6SDaniel Lezcano 		     name, polling);
173110acbc6SDaniel Lezcano 	}
174110acbc6SDaniel Lezcano 
175110acbc6SDaniel Lezcano 	return 0;
176110acbc6SDaniel Lezcano }
177110acbc6SDaniel Lezcano 
usage(const char * cmd)178110acbc6SDaniel Lezcano static void usage(const char *cmd)
179110acbc6SDaniel Lezcano {
180110acbc6SDaniel Lezcano 	printf("%s Version: %s\n", cmd, VERSION);
181110acbc6SDaniel Lezcano 	printf("Usage: %s [options]\n", cmd);
182110acbc6SDaniel Lezcano 	printf("\t-h, --help\t\tthis help\n");
183110acbc6SDaniel Lezcano 	printf("\t-o, --output <dir>\toutput directory for temperature capture\n");
184110acbc6SDaniel Lezcano 	printf("\t-c, --config <file>\tconfiguration file\n");
185110acbc6SDaniel Lezcano 	printf("\t-d, --duration <seconds>\tcapture duration\n");
186110acbc6SDaniel Lezcano 	printf("\t-l, --loglevel <level>\tlog level: ");
187110acbc6SDaniel Lezcano 	printf("DEBUG, INFO, NOTICE, WARN, ERROR\n");
188110acbc6SDaniel Lezcano 	printf("\t-p, --postfix <string>\tpostfix to be happened at the end of the files\n");
189110acbc6SDaniel Lezcano 	printf("\t-s, --syslog\t\toutput to syslog\n");
190110acbc6SDaniel Lezcano 	printf("\t-w, --overwrite\t\toverwrite the temperature capture files if they exist\n");
191110acbc6SDaniel Lezcano 	printf("\n");
192110acbc6SDaniel Lezcano 	exit(0);
193110acbc6SDaniel Lezcano }
194110acbc6SDaniel Lezcano 
options_init(int argc,char * argv[],struct options * options)195110acbc6SDaniel Lezcano static int options_init(int argc, char *argv[], struct options *options)
196110acbc6SDaniel Lezcano {
197110acbc6SDaniel Lezcano 	int opt;
198110acbc6SDaniel Lezcano 	time_t now = time(NULL);
199110acbc6SDaniel Lezcano 
200110acbc6SDaniel Lezcano 	struct option long_options[] = {
201110acbc6SDaniel Lezcano 		{ "help",	no_argument, NULL, 'h' },
202110acbc6SDaniel Lezcano 		{ "config",	required_argument, NULL, 'c' },
203110acbc6SDaniel Lezcano 		{ "duration",	required_argument, NULL, 'd' },
204110acbc6SDaniel Lezcano 		{ "loglevel",	required_argument, NULL, 'l' },
205110acbc6SDaniel Lezcano 		{ "postfix",	required_argument, NULL, 'p' },
206110acbc6SDaniel Lezcano 		{ "output",	required_argument, NULL, 'o' },
207110acbc6SDaniel Lezcano 		{ "syslog",	required_argument, NULL, 's' },
208110acbc6SDaniel Lezcano 		{ "overwrite",	no_argument, NULL, 'w' },
209110acbc6SDaniel Lezcano 		{ 0, 0, 0, 0 }
210110acbc6SDaniel Lezcano 	};
211110acbc6SDaniel Lezcano 
212110acbc6SDaniel Lezcano 	strftime(options->postfix, sizeof(options->postfix),
213110acbc6SDaniel Lezcano 		 "-%Y-%m-%d_%H:%M:%S", gmtime(&now));
214110acbc6SDaniel Lezcano 
215110acbc6SDaniel Lezcano 	while (1) {
216110acbc6SDaniel Lezcano 
217110acbc6SDaniel Lezcano 		int optindex = 0;
218110acbc6SDaniel Lezcano 
219110acbc6SDaniel Lezcano 		opt = getopt_long(argc, argv, "ho:c:d:l:p:sw", long_options, &optindex);
220110acbc6SDaniel Lezcano 		if (opt == -1)
221110acbc6SDaniel Lezcano 			break;
222110acbc6SDaniel Lezcano 
223110acbc6SDaniel Lezcano 		switch (opt) {
224110acbc6SDaniel Lezcano 		case 'c':
225110acbc6SDaniel Lezcano 			options->config = optarg;
226110acbc6SDaniel Lezcano 			break;
227110acbc6SDaniel Lezcano 		case 'd':
228110acbc6SDaniel Lezcano 			options->duration = atoi(optarg) * 1000;
229110acbc6SDaniel Lezcano 			break;
230110acbc6SDaniel Lezcano 		case 'l':
231110acbc6SDaniel Lezcano 			options->loglvl = log_str2level(optarg);
232110acbc6SDaniel Lezcano 			break;
233110acbc6SDaniel Lezcano 		case 'h':
234110acbc6SDaniel Lezcano 			usage(basename(argv[0]));
235110acbc6SDaniel Lezcano 			break;
236110acbc6SDaniel Lezcano 		case 'p':
237110acbc6SDaniel Lezcano 			strcpy(options->postfix, optarg);
238110acbc6SDaniel Lezcano 			break;
239110acbc6SDaniel Lezcano 		case 'o':
240110acbc6SDaniel Lezcano 			strcpy(options->output, optarg);
241110acbc6SDaniel Lezcano 			break;
242110acbc6SDaniel Lezcano 		case 's':
243110acbc6SDaniel Lezcano 			options->logopt = TO_SYSLOG;
244110acbc6SDaniel Lezcano 			break;
245110acbc6SDaniel Lezcano 		case 'w':
246110acbc6SDaniel Lezcano 			options->overwrite = 1;
247110acbc6SDaniel Lezcano 			break;
248110acbc6SDaniel Lezcano 		default: /* '?' */
249110acbc6SDaniel Lezcano 			ERROR("Usage: %s --help\n", argv[0]);
250110acbc6SDaniel Lezcano 			return -1;
251110acbc6SDaniel Lezcano 		}
252110acbc6SDaniel Lezcano 	}
253110acbc6SDaniel Lezcano 
254110acbc6SDaniel Lezcano 	return 0;
255110acbc6SDaniel Lezcano }
256110acbc6SDaniel Lezcano 
thermometer_add_tz(const char * path,const char * name,int polling,struct thermometer * thermometer)257110acbc6SDaniel Lezcano static int thermometer_add_tz(const char *path, const char *name, int polling,
258110acbc6SDaniel Lezcano 			      struct thermometer *thermometer)
259110acbc6SDaniel Lezcano {
260110acbc6SDaniel Lezcano 	int fd;
261110acbc6SDaniel Lezcano 	char tz_path[PATH_MAX];
262110acbc6SDaniel Lezcano 
263110acbc6SDaniel Lezcano 	sprintf(tz_path, CLASS_THERMAL"/%s/temp", path);
264110acbc6SDaniel Lezcano 
265110acbc6SDaniel Lezcano 	fd = open(tz_path, O_RDONLY);
266110acbc6SDaniel Lezcano 	if (fd < 0) {
267110acbc6SDaniel Lezcano 		ERROR("Failed to open '%s': %m\n", tz_path);
268110acbc6SDaniel Lezcano 		return -1;
269110acbc6SDaniel Lezcano 	}
270110acbc6SDaniel Lezcano 
271110acbc6SDaniel Lezcano 	thermometer->tz = realloc(thermometer->tz,
272110acbc6SDaniel Lezcano 				  sizeof(*thermometer->tz) * (thermometer->nr_tz + 1));
273110acbc6SDaniel Lezcano 	if (!thermometer->tz) {
274110acbc6SDaniel Lezcano 		ERROR("Failed to allocate thermometer->tz\n");
275110acbc6SDaniel Lezcano 		return -1;
276110acbc6SDaniel Lezcano 	}
277110acbc6SDaniel Lezcano 
278110acbc6SDaniel Lezcano 	thermometer->tz[thermometer->nr_tz].fd_temp = fd;
279110acbc6SDaniel Lezcano 	thermometer->tz[thermometer->nr_tz].name = strdup(name);
280110acbc6SDaniel Lezcano 	thermometer->tz[thermometer->nr_tz].polling = polling;
281110acbc6SDaniel Lezcano 	thermometer->nr_tz++;
282110acbc6SDaniel Lezcano 
283110acbc6SDaniel Lezcano 	INFO("Added thermal zone '%s->%s (polling:%d)'\n", path, name, polling);
284110acbc6SDaniel Lezcano 
285110acbc6SDaniel Lezcano 	return 0;
286110acbc6SDaniel Lezcano }
287110acbc6SDaniel Lezcano 
thermometer_init(struct configuration * config,struct thermometer * thermometer)288110acbc6SDaniel Lezcano static int thermometer_init(struct configuration *config,
289110acbc6SDaniel Lezcano 			    struct thermometer *thermometer)
290110acbc6SDaniel Lezcano {
291110acbc6SDaniel Lezcano 	DIR *dir;
292110acbc6SDaniel Lezcano 	struct dirent *dirent;
293110acbc6SDaniel Lezcano 	struct tz_regex *tz_regex;
294110acbc6SDaniel Lezcano 	const char *tz_dirname = "thermal_zone";
295110acbc6SDaniel Lezcano 
296110acbc6SDaniel Lezcano 	if (mainloop_init()) {
297110acbc6SDaniel Lezcano 		ERROR("Failed to start mainloop\n");
298110acbc6SDaniel Lezcano 		return -1;
299110acbc6SDaniel Lezcano 	}
300110acbc6SDaniel Lezcano 
301110acbc6SDaniel Lezcano 	dir = opendir(CLASS_THERMAL);
302110acbc6SDaniel Lezcano 	if (!dir) {
303110acbc6SDaniel Lezcano 		ERROR("failed to open '%s'\n", CLASS_THERMAL);
304110acbc6SDaniel Lezcano 		return -1;
305110acbc6SDaniel Lezcano 	}
306110acbc6SDaniel Lezcano 
307110acbc6SDaniel Lezcano 	while ((dirent = readdir(dir))) {
308110acbc6SDaniel Lezcano 		char tz_type[THERMAL_NAME_LENGTH];
309110acbc6SDaniel Lezcano 		char tz_path[PATH_MAX];
310110acbc6SDaniel Lezcano 		FILE *tz_file;
311110acbc6SDaniel Lezcano 
312110acbc6SDaniel Lezcano 		if (strncmp(dirent->d_name, tz_dirname, strlen(tz_dirname)))
313110acbc6SDaniel Lezcano 			continue;
314110acbc6SDaniel Lezcano 
315110acbc6SDaniel Lezcano 		sprintf(tz_path, CLASS_THERMAL"/%s/type", dirent->d_name);
316110acbc6SDaniel Lezcano 
317110acbc6SDaniel Lezcano 		tz_file = fopen(tz_path, "r");
318110acbc6SDaniel Lezcano 		if (!tz_file) {
319110acbc6SDaniel Lezcano 			ERROR("Failed to open '%s': %m", tz_path);
320110acbc6SDaniel Lezcano 			continue;
321110acbc6SDaniel Lezcano 		}
322110acbc6SDaniel Lezcano 
323110acbc6SDaniel Lezcano 		fscanf(tz_file, "%s", tz_type);
324110acbc6SDaniel Lezcano 
325110acbc6SDaniel Lezcano 		fclose(tz_file);
326110acbc6SDaniel Lezcano 
327110acbc6SDaniel Lezcano 		tz_regex = configuration_tz_match(tz_type, config);
328110acbc6SDaniel Lezcano 		if (!tz_regex)
329110acbc6SDaniel Lezcano 			continue;
330110acbc6SDaniel Lezcano 
331110acbc6SDaniel Lezcano 		if (thermometer_add_tz(dirent->d_name, tz_type,
332110acbc6SDaniel Lezcano 				       tz_regex->polling, thermometer))
333110acbc6SDaniel Lezcano 			continue;
334110acbc6SDaniel Lezcano 	}
335110acbc6SDaniel Lezcano 
336110acbc6SDaniel Lezcano 	closedir(dir);
337110acbc6SDaniel Lezcano 
338110acbc6SDaniel Lezcano 	return 0;
339110acbc6SDaniel Lezcano }
340110acbc6SDaniel Lezcano 
timer_temperature_callback(int fd,void * arg)341110acbc6SDaniel Lezcano static int timer_temperature_callback(int fd, void *arg)
342110acbc6SDaniel Lezcano {
343110acbc6SDaniel Lezcano 	struct tz *tz = arg;
344110acbc6SDaniel Lezcano 	char buf[16] = { 0 };
345110acbc6SDaniel Lezcano 
346110acbc6SDaniel Lezcano 	pread(tz->fd_temp, buf, sizeof(buf), 0);
347110acbc6SDaniel Lezcano 
348110acbc6SDaniel Lezcano 	fprintf(tz->file_out, "%ld %s", getuptimeofday_ms(), buf);
349110acbc6SDaniel Lezcano 
350110acbc6SDaniel Lezcano 	read(fd, buf, sizeof(buf));
351110acbc6SDaniel Lezcano 
352110acbc6SDaniel Lezcano 	return 0;
353110acbc6SDaniel Lezcano }
354110acbc6SDaniel Lezcano 
thermometer_start(struct thermometer * thermometer,struct options * options)355110acbc6SDaniel Lezcano static int thermometer_start(struct thermometer *thermometer,
356110acbc6SDaniel Lezcano 			     struct options *options)
357110acbc6SDaniel Lezcano {
358110acbc6SDaniel Lezcano 	struct itimerspec timer_it = { 0 };
359110acbc6SDaniel Lezcano 	char *path;
360110acbc6SDaniel Lezcano 	FILE *f;
361110acbc6SDaniel Lezcano 	int i;
362110acbc6SDaniel Lezcano 
363110acbc6SDaniel Lezcano 	INFO("Capturing %d thermal zone(s) temperature...\n", thermometer->nr_tz);
364110acbc6SDaniel Lezcano 
365110acbc6SDaniel Lezcano 	if (access(options->output, F_OK) && mkdir(options->output, 0700)) {
366110acbc6SDaniel Lezcano 		ERROR("Failed to create directory '%s'\n", options->output);
367110acbc6SDaniel Lezcano 		return -1;
368110acbc6SDaniel Lezcano 	}
369110acbc6SDaniel Lezcano 
370110acbc6SDaniel Lezcano 	for (i = 0; i < thermometer->nr_tz; i++) {
371110acbc6SDaniel Lezcano 
372110acbc6SDaniel Lezcano 		asprintf(&path, "%s/%s%s", options->output,
373110acbc6SDaniel Lezcano 			 thermometer->tz[i].name, options->postfix);
374110acbc6SDaniel Lezcano 
375110acbc6SDaniel Lezcano 		if (!options->overwrite && !access(path, F_OK)) {
376110acbc6SDaniel Lezcano 			ERROR("'%s' already exists\n", path);
377110acbc6SDaniel Lezcano 			return -1;
378110acbc6SDaniel Lezcano 		}
379110acbc6SDaniel Lezcano 
380110acbc6SDaniel Lezcano 		f = fopen(path, "w");
381110acbc6SDaniel Lezcano 		if (!f) {
382110acbc6SDaniel Lezcano 			ERROR("Failed to create '%s':%m\n", path);
383110acbc6SDaniel Lezcano 			return -1;
384110acbc6SDaniel Lezcano 		}
385110acbc6SDaniel Lezcano 
386110acbc6SDaniel Lezcano 		fprintf(f, "timestamp(ms) %s(°mC)\n", thermometer->tz[i].name);
387110acbc6SDaniel Lezcano 
388110acbc6SDaniel Lezcano 		thermometer->tz[i].file_out = f;
389110acbc6SDaniel Lezcano 
390110acbc6SDaniel Lezcano 		DEBUG("Created '%s' file for thermal zone '%s'\n", path, thermometer->tz[i].name);
391110acbc6SDaniel Lezcano 
392110acbc6SDaniel Lezcano 		/*
393110acbc6SDaniel Lezcano 		 * Create polling timer
394110acbc6SDaniel Lezcano 		 */
395110acbc6SDaniel Lezcano 		thermometer->tz[i].fd_timer = timerfd_create(CLOCK_MONOTONIC, 0);
396110acbc6SDaniel Lezcano 		if (thermometer->tz[i].fd_timer < 0) {
397110acbc6SDaniel Lezcano 			ERROR("Failed to create timer for '%s': %m\n",
398110acbc6SDaniel Lezcano 			      thermometer->tz[i].name);
399110acbc6SDaniel Lezcano 			return -1;
400110acbc6SDaniel Lezcano 		}
401110acbc6SDaniel Lezcano 
402110acbc6SDaniel Lezcano 		DEBUG("Watching '%s' every %d ms\n",
403110acbc6SDaniel Lezcano 		      thermometer->tz[i].name, thermometer->tz[i].polling);
404110acbc6SDaniel Lezcano 
405110acbc6SDaniel Lezcano 		timer_it.it_interval = timer_it.it_value =
406110acbc6SDaniel Lezcano 			msec_to_timespec(thermometer->tz[i].polling);
407110acbc6SDaniel Lezcano 
408110acbc6SDaniel Lezcano 		if (timerfd_settime(thermometer->tz[i].fd_timer, 0,
409110acbc6SDaniel Lezcano 				    &timer_it, NULL) < 0)
410110acbc6SDaniel Lezcano 			return -1;
411110acbc6SDaniel Lezcano 
412110acbc6SDaniel Lezcano 		if (mainloop_add(thermometer->tz[i].fd_timer,
413110acbc6SDaniel Lezcano 				 timer_temperature_callback,
414110acbc6SDaniel Lezcano 				 &thermometer->tz[i]))
415110acbc6SDaniel Lezcano 			return -1;
416110acbc6SDaniel Lezcano 	}
417110acbc6SDaniel Lezcano 
418110acbc6SDaniel Lezcano 	return 0;
419110acbc6SDaniel Lezcano }
420110acbc6SDaniel Lezcano 
thermometer_execute(int argc,char * argv[],char * const envp[],pid_t * pid)421110acbc6SDaniel Lezcano static int thermometer_execute(int argc, char *argv[], char *const envp[], pid_t *pid)
422110acbc6SDaniel Lezcano {
423110acbc6SDaniel Lezcano 	if (!argc)
424110acbc6SDaniel Lezcano 		return 0;
425110acbc6SDaniel Lezcano 
426110acbc6SDaniel Lezcano 	*pid = fork();
427110acbc6SDaniel Lezcano 	if (*pid < 0) {
428110acbc6SDaniel Lezcano 		ERROR("Failed to fork process: %m");
429110acbc6SDaniel Lezcano 		return -1;
430110acbc6SDaniel Lezcano 	}
431110acbc6SDaniel Lezcano 
432110acbc6SDaniel Lezcano 	if (!(*pid)) {
433110acbc6SDaniel Lezcano 		execvpe(argv[0], argv, envp);
434110acbc6SDaniel Lezcano 		exit(1);
435110acbc6SDaniel Lezcano 	}
436110acbc6SDaniel Lezcano 
437110acbc6SDaniel Lezcano 	return 0;
438110acbc6SDaniel Lezcano }
439110acbc6SDaniel Lezcano 
kill_process(__maybe_unused int fd,void * arg)440110acbc6SDaniel Lezcano static int kill_process(__maybe_unused int fd, void *arg)
441110acbc6SDaniel Lezcano {
442110acbc6SDaniel Lezcano 	pid_t pid = *(pid_t *)arg;
443110acbc6SDaniel Lezcano 
444110acbc6SDaniel Lezcano 	if (kill(pid, SIGTERM))
445110acbc6SDaniel Lezcano 		ERROR("Failed to send SIGTERM signal to '%d': %p\n", pid);
446110acbc6SDaniel Lezcano 	else if (waitpid(pid, NULL, 0))
447110acbc6SDaniel Lezcano 		ERROR("Failed to wait pid '%d': %p\n", pid);
448110acbc6SDaniel Lezcano 
449110acbc6SDaniel Lezcano 	mainloop_exit();
450110acbc6SDaniel Lezcano 
451110acbc6SDaniel Lezcano 	return 0;
452110acbc6SDaniel Lezcano }
453110acbc6SDaniel Lezcano 
exit_mainloop(__maybe_unused int fd,__maybe_unused void * arg)454110acbc6SDaniel Lezcano static int exit_mainloop(__maybe_unused int fd, __maybe_unused void *arg)
455110acbc6SDaniel Lezcano {
456110acbc6SDaniel Lezcano 	mainloop_exit();
457110acbc6SDaniel Lezcano 	return 0;
458110acbc6SDaniel Lezcano }
459110acbc6SDaniel Lezcano 
thermometer_wait(struct options * options,pid_t pid)460110acbc6SDaniel Lezcano static int thermometer_wait(struct options *options, pid_t pid)
461110acbc6SDaniel Lezcano {
462110acbc6SDaniel Lezcano 	int fd;
463110acbc6SDaniel Lezcano 	sigset_t mask;
464110acbc6SDaniel Lezcano 
465110acbc6SDaniel Lezcano 	/*
466110acbc6SDaniel Lezcano 	 * If there is a duration specified, we will exit the mainloop
467110acbc6SDaniel Lezcano 	 * and gracefully close all the files which will flush the
468110acbc6SDaniel Lezcano 	 * file system cache
469110acbc6SDaniel Lezcano 	 */
470110acbc6SDaniel Lezcano 	if (options->duration) {
471110acbc6SDaniel Lezcano 		struct itimerspec timer_it = { 0 };
472110acbc6SDaniel Lezcano 
473110acbc6SDaniel Lezcano 		timer_it.it_value = msec_to_timespec(options->duration);
474110acbc6SDaniel Lezcano 
475110acbc6SDaniel Lezcano 		fd = timerfd_create(CLOCK_MONOTONIC, 0);
476110acbc6SDaniel Lezcano 		if (fd < 0) {
477110acbc6SDaniel Lezcano 			ERROR("Failed to create duration timer: %m\n");
478110acbc6SDaniel Lezcano 			return -1;
479110acbc6SDaniel Lezcano 		}
480110acbc6SDaniel Lezcano 
481110acbc6SDaniel Lezcano 		if (timerfd_settime(fd, 0, &timer_it, NULL)) {
482110acbc6SDaniel Lezcano 			ERROR("Failed to set timer time: %m\n");
483110acbc6SDaniel Lezcano 			return -1;
484110acbc6SDaniel Lezcano 		}
485110acbc6SDaniel Lezcano 
486110acbc6SDaniel Lezcano 		if (mainloop_add(fd, pid < 0 ? exit_mainloop : kill_process, &pid)) {
487110acbc6SDaniel Lezcano 			ERROR("Failed to set timer exit mainloop callback\n");
488110acbc6SDaniel Lezcano 			return -1;
489110acbc6SDaniel Lezcano 		}
490110acbc6SDaniel Lezcano 	}
491110acbc6SDaniel Lezcano 
492110acbc6SDaniel Lezcano 	/*
493110acbc6SDaniel Lezcano 	 * We want to catch any keyboard interrupt, as well as child
494110acbc6SDaniel Lezcano 	 * signals if any in order to exit properly
495110acbc6SDaniel Lezcano 	 */
496110acbc6SDaniel Lezcano 	sigemptyset(&mask);
497110acbc6SDaniel Lezcano 	sigaddset(&mask, SIGINT);
498110acbc6SDaniel Lezcano 	sigaddset(&mask, SIGQUIT);
499110acbc6SDaniel Lezcano 	sigaddset(&mask, SIGCHLD);
500110acbc6SDaniel Lezcano 
501110acbc6SDaniel Lezcano 	if (sigprocmask(SIG_BLOCK, &mask, NULL)) {
502110acbc6SDaniel Lezcano 		ERROR("Failed to set sigprocmask: %m\n");
503110acbc6SDaniel Lezcano 		return -1;
504110acbc6SDaniel Lezcano 	}
505110acbc6SDaniel Lezcano 
506110acbc6SDaniel Lezcano 	fd = signalfd(-1, &mask, 0);
507110acbc6SDaniel Lezcano 	if (fd < 0) {
508110acbc6SDaniel Lezcano 		ERROR("Failed to set the signalfd: %m\n");
509110acbc6SDaniel Lezcano 		return -1;
510110acbc6SDaniel Lezcano 	}
511110acbc6SDaniel Lezcano 
512110acbc6SDaniel Lezcano 	if (mainloop_add(fd, exit_mainloop, NULL)) {
513110acbc6SDaniel Lezcano 		ERROR("Failed to set timer exit mainloop callback\n");
514110acbc6SDaniel Lezcano 		return -1;
515110acbc6SDaniel Lezcano 	}
516110acbc6SDaniel Lezcano 
517110acbc6SDaniel Lezcano 	return mainloop(-1);
518110acbc6SDaniel Lezcano }
519110acbc6SDaniel Lezcano 
thermometer_stop(struct thermometer * thermometer)520110acbc6SDaniel Lezcano static int thermometer_stop(struct thermometer *thermometer)
521110acbc6SDaniel Lezcano {
522110acbc6SDaniel Lezcano 	int i;
523110acbc6SDaniel Lezcano 
524110acbc6SDaniel Lezcano 	INFO("Closing/flushing output files\n");
525110acbc6SDaniel Lezcano 
526110acbc6SDaniel Lezcano 	for (i = 0; i < thermometer->nr_tz; i++)
527110acbc6SDaniel Lezcano 		fclose(thermometer->tz[i].file_out);
528110acbc6SDaniel Lezcano 
529110acbc6SDaniel Lezcano 	return 0;
530110acbc6SDaniel Lezcano }
531110acbc6SDaniel Lezcano 
main(int argc,char * argv[],char * const envp[])532110acbc6SDaniel Lezcano int main(int argc, char *argv[], char *const envp[])
533110acbc6SDaniel Lezcano {
534110acbc6SDaniel Lezcano 	struct options options = {
535110acbc6SDaniel Lezcano 		.loglvl = LOG_DEBUG,
536110acbc6SDaniel Lezcano 		.logopt = TO_STDOUT,
537110acbc6SDaniel Lezcano 		.output = ".",
538110acbc6SDaniel Lezcano 	};
539110acbc6SDaniel Lezcano 	struct configuration config = { 0 };
540110acbc6SDaniel Lezcano 	struct thermometer thermometer = { 0 };
541110acbc6SDaniel Lezcano 
542110acbc6SDaniel Lezcano 	pid_t pid = -1;
543110acbc6SDaniel Lezcano 
544110acbc6SDaniel Lezcano 	if (options_init(argc, argv, &options))
545110acbc6SDaniel Lezcano 		return THERMOMETER_OPTION_ERROR;
546110acbc6SDaniel Lezcano 
547110acbc6SDaniel Lezcano 	if (log_init(options.loglvl, argv[0], options.logopt))
548110acbc6SDaniel Lezcano 		return THERMOMETER_LOG_ERROR;
549110acbc6SDaniel Lezcano 
550110acbc6SDaniel Lezcano 	if (configuration_init(options.config, &config))
551110acbc6SDaniel Lezcano 		return THERMOMETER_CONFIG_ERROR;
552110acbc6SDaniel Lezcano 
553110acbc6SDaniel Lezcano 	if (uptimeofday_init())
554110acbc6SDaniel Lezcano 		return THERMOMETER_TIME_ERROR;
555110acbc6SDaniel Lezcano 
556110acbc6SDaniel Lezcano 	if (thermometer_init(&config, &thermometer))
557110acbc6SDaniel Lezcano 		return THERMOMETER_INIT_ERROR;
558110acbc6SDaniel Lezcano 
559110acbc6SDaniel Lezcano 	if (thermometer_start(&thermometer, &options))
560110acbc6SDaniel Lezcano 		return THERMOMETER_RUNTIME_ERROR;
561110acbc6SDaniel Lezcano 
562110acbc6SDaniel Lezcano 	if (thermometer_execute(argc - optind, &argv[optind], envp, &pid))
563110acbc6SDaniel Lezcano 		return THERMOMETER_RUNTIME_ERROR;
564110acbc6SDaniel Lezcano 
565110acbc6SDaniel Lezcano 	if (thermometer_wait(&options, pid))
566110acbc6SDaniel Lezcano 		return THERMOMETER_RUNTIME_ERROR;
567110acbc6SDaniel Lezcano 
568110acbc6SDaniel Lezcano 	if (thermometer_stop(&thermometer))
569110acbc6SDaniel Lezcano 		return THERMOMETER_RUNTIME_ERROR;
570110acbc6SDaniel Lezcano 
571110acbc6SDaniel Lezcano 	return THERMOMETER_SUCCESS;
572110acbc6SDaniel Lezcano }
573